xref: /netbsd-src/external/bsd/tcpdump/dist/print-igmp.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996
3  *  The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  */
21 
22 #include <sys/cdefs.h>
23 #ifndef lint
24 #if 0
25 static const char rcsid[] _U_ =
26     "@(#) Header: /tcpdump/master/tcpdump/print-igmp.c,v 1.15 2004-03-24 00:59:16 guy Exp  (LBL)";
27 #else
28 __RCSID("$NetBSD: print-igmp.c,v 1.4 2013/12/31 17:33:31 christos Exp $");
29 #endif
30 #endif
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include <tcpdump-stdinc.h>
37 
38 #include <stdio.h>
39 #include <string.h>
40 
41 #include "interface.h"
42 #include "addrtoname.h"
43 #include "extract.h"            /* must come after interface.h */
44 
45 #ifndef IN_CLASSD
46 #define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000)
47 #endif
48 
49 /* (following from ipmulti/mrouted/prune.h) */
50 
51 /*
52  * The packet format for a traceroute request.
53  */
54 struct tr_query {
55     u_int32_t  tr_src;          /* traceroute source */
56     u_int32_t  tr_dst;          /* traceroute destination */
57     u_int32_t  tr_raddr;        /* traceroute response address */
58     u_int32_t  tr_rttlqid;      /* response ttl and qid */
59 };
60 
61 #define TR_GETTTL(x)        (int)(((x) >> 24) & 0xff)
62 #define TR_GETQID(x)        ((x) & 0x00ffffff)
63 
64 /*
65  * Traceroute response format.  A traceroute response has a tr_query at the
66  * beginning, followed by one tr_resp for each hop taken.
67  */
68 struct tr_resp {
69     u_int32_t tr_qarr;          /* query arrival time */
70     u_int32_t tr_inaddr;        /* incoming interface address */
71     u_int32_t tr_outaddr;       /* outgoing interface address */
72     u_int32_t tr_rmtaddr;       /* parent address in source tree */
73     u_int32_t tr_vifin;         /* input packet count on interface */
74     u_int32_t tr_vifout;        /* output packet count on interface */
75     u_int32_t tr_pktcnt;        /* total incoming packets for src-grp */
76     u_int8_t  tr_rproto;      /* routing proto deployed on router */
77     u_int8_t  tr_fttl;        /* ttl required to forward on outvif */
78     u_int8_t  tr_smask;       /* subnet mask for src addr */
79     u_int8_t  tr_rflags;      /* forwarding error codes */
80 };
81 
82 /* defs within mtrace */
83 #define TR_QUERY 1
84 #define TR_RESP 2
85 
86 /* fields for tr_rflags (forwarding error codes) */
87 #define TR_NO_ERR   0
88 #define TR_WRONG_IF 1
89 #define TR_PRUNED   2
90 #define TR_OPRUNED  3
91 #define TR_SCOPED   4
92 #define TR_NO_RTE   5
93 #define TR_NO_FWD   7
94 #define TR_NO_SPACE 0x81
95 #define TR_OLD_ROUTER   0x82
96 
97 /* fields for tr_rproto (routing protocol) */
98 #define TR_PROTO_DVMRP  1
99 #define TR_PROTO_MOSPF  2
100 #define TR_PROTO_PIM    3
101 #define TR_PROTO_CBT    4
102 
103 /* igmpv3 report types */
104 static const struct tok igmpv3report2str[] = {
105 	{ 1,	"is_in" },
106 	{ 2,	"is_ex" },
107 	{ 3,	"to_in" },
108 	{ 4,	"to_ex" },
109 	{ 5,	"allow" },
110 	{ 6,	"block" },
111 	{ 0,	NULL }
112 };
113 
114 static void
115 print_mtrace(register const u_char *bp, register u_int len)
116 {
117     register const struct tr_query *tr = (const struct tr_query *)(bp + 8);
118 
119     TCHECK(*tr);
120     if (len < 8 + sizeof (struct tr_query)) {
121 	(void)printf(" [invalid len %d]", len);
122 	return;
123     }
124     printf("mtrace %u: %s to %s reply-to %s",
125         TR_GETQID(EXTRACT_32BITS(&tr->tr_rttlqid)),
126         ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst),
127         ipaddr_string(&tr->tr_raddr));
128     if (IN_CLASSD(EXTRACT_32BITS(&tr->tr_raddr)))
129         printf(" with-ttl %d", TR_GETTTL(EXTRACT_32BITS(&tr->tr_rttlqid)));
130     return;
131 trunc:
132     (void)printf("[|igmp]");
133     return;
134 }
135 
136 static void
137 print_mresp(register const u_char *bp, register u_int len)
138 {
139     register const struct tr_query *tr = (const struct tr_query *)(bp + 8);
140 
141     TCHECK(*tr);
142     if (len < 8 + sizeof (struct tr_query)) {
143 	(void)printf(" [invalid len %d]", len);
144 	return;
145     }
146     printf("mresp %lu: %s to %s reply-to %s",
147         (u_long)TR_GETQID(EXTRACT_32BITS(&tr->tr_rttlqid)),
148         ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst),
149         ipaddr_string(&tr->tr_raddr));
150     if (IN_CLASSD(EXTRACT_32BITS(&tr->tr_raddr)))
151         printf(" with-ttl %d", TR_GETTTL(EXTRACT_32BITS(&tr->tr_rttlqid)));
152     return;
153 trunc:
154     (void)printf("[|igmp]");
155     return;
156 }
157 
158 static void
159 print_igmpv3_report(register const u_char *bp, register u_int len)
160 {
161     u_int group, nsrcs, ngroups;
162     register u_int i, j;
163 
164     /* Minimum len is 16, and should be a multiple of 4 */
165     if (len < 16 || len & 0x03) {
166 	(void)printf(" [invalid len %d]", len);
167 	return;
168     }
169     TCHECK2(bp[6], 2);
170     ngroups = EXTRACT_16BITS(&bp[6]);
171     (void)printf(", %d group record(s)", ngroups);
172     if (vflag > 0) {
173 	/* Print the group records */
174 	group = 8;
175         for (i=0; i<ngroups; i++) {
176 	    if (len < group+8) {
177 		(void)printf(" [invalid number of groups]");
178 		return;
179 	    }
180 	    TCHECK2(bp[group+4], 4);
181             (void)printf(" [gaddr %s", ipaddr_string(&bp[group+4]));
182 	    (void)printf(" %s", tok2str(igmpv3report2str, " [v3-report-#%d]",
183 								bp[group]));
184             nsrcs = EXTRACT_16BITS(&bp[group+2]);
185 	    /* Check the number of sources and print them */
186 	    if (len < group+8+(nsrcs<<2)) {
187 		(void)printf(" [invalid number of sources %d]", nsrcs);
188 		return;
189 	    }
190             if (vflag == 1)
191                 (void)printf(", %d source(s)", nsrcs);
192             else {
193 		/* Print the sources */
194                 (void)printf(" {");
195                 for (j=0; j<nsrcs; j++) {
196 		    TCHECK2(bp[group+8+(j<<2)], 4);
197 		    (void)printf(" %s", ipaddr_string(&bp[group+8+(j<<2)]));
198 		}
199                 (void)printf(" }");
200             }
201 	    /* Next group record */
202             group += 8 + (nsrcs << 2);
203 	    (void)printf("]");
204         }
205     }
206     return;
207 trunc:
208     (void)printf("[|igmp]");
209     return;
210 }
211 
212 static void
213 print_igmpv3_query(register const u_char *bp, register u_int len)
214 {
215     u_int mrc;
216     int mrt;
217     u_int nsrcs;
218     register u_int i;
219 
220     (void)printf(" v3");
221     /* Minimum len is 12, and should be a multiple of 4 */
222     if (len < 12 || len & 0x03) {
223 	(void)printf(" [invalid len %d]", len);
224 	return;
225     }
226     TCHECK(bp[1]);
227     mrc = bp[1];
228     if (mrc < 128) {
229 	mrt = mrc;
230     } else {
231         mrt = ((mrc & 0x0f) | 0x10) << (((mrc & 0x70) >> 4) + 3);
232     }
233     if (mrc != 100) {
234 	(void)printf(" [max resp time ");
235         if (mrt < 600) {
236             (void)printf("%.1fs", mrt * 0.1);
237         } else {
238             relts_print(mrt / 10);
239         }
240 	(void)printf("]");
241     }
242     TCHECK2(bp[4], 4);
243     if (EXTRACT_32BITS(&bp[4]) == 0)
244 	return;
245     (void)printf(" [gaddr %s", ipaddr_string(&bp[4]));
246     TCHECK2(bp[10], 2);
247     nsrcs = EXTRACT_16BITS(&bp[10]);
248     if (nsrcs > 0) {
249 	if (len < 12 + (nsrcs << 2))
250 	    (void)printf(" [invalid number of sources]");
251 	else if (vflag > 1) {
252 	    (void)printf(" {");
253 	    for (i=0; i<nsrcs; i++) {
254 		TCHECK2(bp[12+(i<<2)], 4);
255 		(void)printf(" %s", ipaddr_string(&bp[12+(i<<2)]));
256 	    }
257 	    (void)printf(" }");
258 	} else
259 	    (void)printf(", %d source(s)", nsrcs);
260     }
261     (void)printf("]");
262     return;
263 trunc:
264     (void)printf("[|igmp]");
265     return;
266 }
267 
268 void
269 igmp_print(register const u_char *bp, register u_int len)
270 {
271     struct cksum_vec vec[1];
272 
273     if (qflag) {
274         (void)printf("igmp");
275         return;
276     }
277 
278     TCHECK(bp[0]);
279     switch (bp[0]) {
280     case 0x11:
281         (void)printf("igmp query");
282 	if (len >= 12)
283 	    print_igmpv3_query(bp, len);
284 	else {
285             TCHECK(bp[1]);
286 	    if (bp[1]) {
287 		(void)printf(" v2");
288 		if (bp[1] != 100)
289 		    (void)printf(" [max resp time %d]", bp[1]);
290 	    } else
291 		(void)printf(" v1");
292             TCHECK2(bp[4], 4);
293 	    if (EXTRACT_32BITS(&bp[4]))
294                 (void)printf(" [gaddr %s]", ipaddr_string(&bp[4]));
295             if (len != 8)
296                 (void)printf(" [len %d]", len);
297 	}
298         break;
299     case 0x12:
300         TCHECK2(bp[4], 4);
301         (void)printf("igmp v1 report %s", ipaddr_string(&bp[4]));
302         if (len != 8)
303             (void)printf(" [len %d]", len);
304         break;
305     case 0x16:
306         TCHECK2(bp[4], 4);
307         (void)printf("igmp v2 report %s", ipaddr_string(&bp[4]));
308         break;
309     case 0x22:
310         (void)printf("igmp v3 report");
311 	print_igmpv3_report(bp, len);
312         break;
313     case 0x17:
314         TCHECK2(bp[4], 4);
315         (void)printf("igmp leave %s", ipaddr_string(&bp[4]));
316         break;
317     case 0x13:
318         (void)printf("igmp dvmrp");
319         if (len < 8)
320             (void)printf(" [len %d]", len);
321         else
322             dvmrp_print(bp, len);
323         break;
324     case 0x14:
325         (void)printf("igmp pimv1");
326         pimv1_print(bp, len);
327         break;
328     case 0x1e:
329         print_mresp(bp, len);
330         break;
331     case 0x1f:
332         print_mtrace(bp, len);
333         break;
334     default:
335         (void)printf("igmp-%d", bp[0]);
336         break;
337     }
338 
339     if (vflag && TTEST2(bp[0], len)) {
340         /* Check the IGMP checksum */
341         vec[0].ptr = bp;
342         vec[0].len = len;
343         if (in_cksum(vec, 1))
344             printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp[2]));
345     }
346     return;
347 trunc:
348     fputs("[|igmp]", stdout);
349 }
350