xref: /minix3/external/bsd/tcpdump/dist/print-igmp.c (revision b636d99d91c3d54204248f643c14627405d4afd1)
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 __RCSID("$NetBSD: print-igmp.c,v 1.5 2014/11/20 03:05:03 christos Exp $");
25 #endif
26 
27 #define NETDISSECT_REWORKED
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <tcpdump-stdinc.h>
33 
34 #include "interface.h"
35 #include "addrtoname.h"
36 #include "extract.h"            /* must come after interface.h */
37 
38 #ifndef IN_CLASSD
39 #define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000)
40 #endif
41 
42 static const char tstr[] = "[|igmp]";
43 
44 /* (following from ipmulti/mrouted/prune.h) */
45 
46 /*
47  * The packet format for a traceroute request.
48  */
49 struct tr_query {
50     uint32_t  tr_src;          /* traceroute source */
51     uint32_t  tr_dst;          /* traceroute destination */
52     uint32_t  tr_raddr;        /* traceroute response address */
53     uint32_t  tr_rttlqid;      /* response ttl and qid */
54 };
55 
56 #define TR_GETTTL(x)        (int)(((x) >> 24) & 0xff)
57 #define TR_GETQID(x)        ((x) & 0x00ffffff)
58 
59 /*
60  * Traceroute response format.  A traceroute response has a tr_query at the
61  * beginning, followed by one tr_resp for each hop taken.
62  */
63 struct tr_resp {
64     uint32_t tr_qarr;          /* query arrival time */
65     uint32_t tr_inaddr;        /* incoming interface address */
66     uint32_t tr_outaddr;       /* outgoing interface address */
67     uint32_t tr_rmtaddr;       /* parent address in source tree */
68     uint32_t tr_vifin;         /* input packet count on interface */
69     uint32_t tr_vifout;        /* output packet count on interface */
70     uint32_t tr_pktcnt;        /* total incoming packets for src-grp */
71     uint8_t  tr_rproto;      /* routing proto deployed on router */
72     uint8_t  tr_fttl;        /* ttl required to forward on outvif */
73     uint8_t  tr_smask;       /* subnet mask for src addr */
74     uint8_t  tr_rflags;      /* forwarding error codes */
75 };
76 
77 /* defs within mtrace */
78 #define TR_QUERY 1
79 #define TR_RESP 2
80 
81 /* fields for tr_rflags (forwarding error codes) */
82 #define TR_NO_ERR   0
83 #define TR_WRONG_IF 1
84 #define TR_PRUNED   2
85 #define TR_OPRUNED  3
86 #define TR_SCOPED   4
87 #define TR_NO_RTE   5
88 #define TR_NO_FWD   7
89 #define TR_NO_SPACE 0x81
90 #define TR_OLD_ROUTER   0x82
91 
92 /* fields for tr_rproto (routing protocol) */
93 #define TR_PROTO_DVMRP  1
94 #define TR_PROTO_MOSPF  2
95 #define TR_PROTO_PIM    3
96 #define TR_PROTO_CBT    4
97 
98 /* igmpv3 report types */
99 static const struct tok igmpv3report2str[] = {
100 	{ 1,	"is_in" },
101 	{ 2,	"is_ex" },
102 	{ 3,	"to_in" },
103 	{ 4,	"to_ex" },
104 	{ 5,	"allow" },
105 	{ 6,	"block" },
106 	{ 0,	NULL }
107 };
108 
109 static void
print_mtrace(netdissect_options * ndo,register const u_char * bp,register u_int len)110 print_mtrace(netdissect_options *ndo,
111              register const u_char *bp, register u_int len)
112 {
113     register const struct tr_query *tr = (const struct tr_query *)(bp + 8);
114 
115     ND_TCHECK(*tr);
116     if (len < 8 + sizeof (struct tr_query)) {
117 	ND_PRINT((ndo, " [invalid len %d]", len));
118 	return;
119     }
120     ND_PRINT((ndo, "mtrace %u: %s to %s reply-to %s",
121         TR_GETQID(EXTRACT_32BITS(&tr->tr_rttlqid)),
122         ipaddr_string(ndo, &tr->tr_src), ipaddr_string(ndo, &tr->tr_dst),
123         ipaddr_string(ndo, &tr->tr_raddr)));
124     if (IN_CLASSD(EXTRACT_32BITS(&tr->tr_raddr)))
125         ND_PRINT((ndo, " with-ttl %d", TR_GETTTL(EXTRACT_32BITS(&tr->tr_rttlqid))));
126     return;
127 trunc:
128     ND_PRINT((ndo, "%s", tstr));
129 }
130 
131 static void
print_mresp(netdissect_options * ndo,register const u_char * bp,register u_int len)132 print_mresp(netdissect_options *ndo,
133             register const u_char *bp, register u_int len)
134 {
135     register const struct tr_query *tr = (const struct tr_query *)(bp + 8);
136 
137     ND_TCHECK(*tr);
138     if (len < 8 + sizeof (struct tr_query)) {
139 	ND_PRINT((ndo, " [invalid len %d]", len));
140 	return;
141     }
142     ND_PRINT((ndo, "mresp %lu: %s to %s reply-to %s",
143         (u_long)TR_GETQID(EXTRACT_32BITS(&tr->tr_rttlqid)),
144         ipaddr_string(ndo, &tr->tr_src), ipaddr_string(ndo, &tr->tr_dst),
145         ipaddr_string(ndo, &tr->tr_raddr)));
146     if (IN_CLASSD(EXTRACT_32BITS(&tr->tr_raddr)))
147         ND_PRINT((ndo, " with-ttl %d", TR_GETTTL(EXTRACT_32BITS(&tr->tr_rttlqid))));
148     return;
149 trunc:
150     ND_PRINT((ndo, "%s", tstr));
151 }
152 
153 static void
print_igmpv3_report(netdissect_options * ndo,register const u_char * bp,register u_int len)154 print_igmpv3_report(netdissect_options *ndo,
155                     register const u_char *bp, register u_int len)
156 {
157     u_int group, nsrcs, ngroups;
158     register u_int i, j;
159 
160     /* Minimum len is 16, and should be a multiple of 4 */
161     if (len < 16 || len & 0x03) {
162 	ND_PRINT((ndo, " [invalid len %d]", len));
163 	return;
164     }
165     ND_TCHECK2(bp[6], 2);
166     ngroups = EXTRACT_16BITS(&bp[6]);
167     ND_PRINT((ndo, ", %d group record(s)", ngroups));
168     if (ndo->ndo_vflag > 0) {
169 	/* Print the group records */
170 	group = 8;
171         for (i=0; i<ngroups; i++) {
172 	    if (len < group+8) {
173 		ND_PRINT((ndo, " [invalid number of groups]"));
174 		return;
175 	    }
176 	    ND_TCHECK2(bp[group+4], 4);
177             ND_PRINT((ndo, " [gaddr %s", ipaddr_string(ndo, &bp[group+4])));
178 	    ND_PRINT((ndo, " %s", tok2str(igmpv3report2str, " [v3-report-#%d]",
179 								bp[group])));
180             nsrcs = EXTRACT_16BITS(&bp[group+2]);
181 	    /* Check the number of sources and print them */
182 	    if (len < group+8+(nsrcs<<2)) {
183 		ND_PRINT((ndo, " [invalid number of sources %d]", nsrcs));
184 		return;
185 	    }
186             if (ndo->ndo_vflag == 1)
187                 ND_PRINT((ndo, ", %d source(s)", nsrcs));
188             else {
189 		/* Print the sources */
190                 ND_PRINT((ndo, " {"));
191                 for (j=0; j<nsrcs; j++) {
192 		    ND_TCHECK2(bp[group+8+(j<<2)], 4);
193 		    ND_PRINT((ndo, " %s", ipaddr_string(ndo, &bp[group+8+(j<<2)])));
194 		}
195                 ND_PRINT((ndo, " }"));
196             }
197 	    /* Next group record */
198             group += 8 + (nsrcs << 2);
199 	    ND_PRINT((ndo, "]"));
200         }
201     }
202     return;
203 trunc:
204     ND_PRINT((ndo, "%s", tstr));
205 }
206 
207 static void
print_igmpv3_query(netdissect_options * ndo,register const u_char * bp,register u_int len)208 print_igmpv3_query(netdissect_options *ndo,
209                    register const u_char *bp, register u_int len)
210 {
211     u_int mrc;
212     int mrt;
213     u_int nsrcs;
214     register u_int i;
215 
216     ND_PRINT((ndo, " v3"));
217     /* Minimum len is 12, and should be a multiple of 4 */
218     if (len < 12 || len & 0x03) {
219 	ND_PRINT((ndo, " [invalid len %d]", len));
220 	return;
221     }
222     ND_TCHECK(bp[1]);
223     mrc = bp[1];
224     if (mrc < 128) {
225 	mrt = mrc;
226     } else {
227         mrt = ((mrc & 0x0f) | 0x10) << (((mrc & 0x70) >> 4) + 3);
228     }
229     if (mrc != 100) {
230 	ND_PRINT((ndo, " [max resp time "));
231         if (mrt < 600) {
232             ND_PRINT((ndo, "%.1fs", mrt * 0.1));
233         } else {
234             relts_print(ndo, mrt / 10);
235         }
236 	ND_PRINT((ndo, "]"));
237     }
238     ND_TCHECK2(bp[4], 4);
239     if (EXTRACT_32BITS(&bp[4]) == 0)
240 	return;
241     ND_PRINT((ndo, " [gaddr %s", ipaddr_string(ndo, &bp[4])));
242     ND_TCHECK2(bp[10], 2);
243     nsrcs = EXTRACT_16BITS(&bp[10]);
244     if (nsrcs > 0) {
245 	if (len < 12 + (nsrcs << 2))
246 	    ND_PRINT((ndo, " [invalid number of sources]"));
247 	else if (ndo->ndo_vflag > 1) {
248 	    ND_PRINT((ndo, " {"));
249 	    for (i=0; i<nsrcs; i++) {
250 		ND_TCHECK2(bp[12+(i<<2)], 4);
251 		ND_PRINT((ndo, " %s", ipaddr_string(ndo, &bp[12+(i<<2)])));
252 	    }
253 	    ND_PRINT((ndo, " }"));
254 	} else
255 	    ND_PRINT((ndo, ", %d source(s)", nsrcs));
256     }
257     ND_PRINT((ndo, "]"));
258     return;
259 trunc:
260     ND_PRINT((ndo, "%s", tstr));
261 }
262 
263 void
igmp_print(netdissect_options * ndo,register const u_char * bp,register u_int len)264 igmp_print(netdissect_options *ndo,
265            register const u_char *bp, register u_int len)
266 {
267     struct cksum_vec vec[1];
268 
269     if (ndo->ndo_qflag) {
270         ND_PRINT((ndo, "igmp"));
271         return;
272     }
273 
274     ND_TCHECK(bp[0]);
275     switch (bp[0]) {
276     case 0x11:
277         ND_PRINT((ndo, "igmp query"));
278 	if (len >= 12)
279 	    print_igmpv3_query(ndo, bp, len);
280 	else {
281             ND_TCHECK(bp[1]);
282 	    if (bp[1]) {
283 		ND_PRINT((ndo, " v2"));
284 		if (bp[1] != 100)
285 		    ND_PRINT((ndo, " [max resp time %d]", bp[1]));
286 	    } else
287 		ND_PRINT((ndo, " v1"));
288             ND_TCHECK2(bp[4], 4);
289 	    if (EXTRACT_32BITS(&bp[4]))
290                 ND_PRINT((ndo, " [gaddr %s]", ipaddr_string(ndo, &bp[4])));
291             if (len != 8)
292                 ND_PRINT((ndo, " [len %d]", len));
293 	}
294         break;
295     case 0x12:
296         ND_TCHECK2(bp[4], 4);
297         ND_PRINT((ndo, "igmp v1 report %s", ipaddr_string(ndo, &bp[4])));
298         if (len != 8)
299             ND_PRINT((ndo, " [len %d]", len));
300         break;
301     case 0x16:
302         ND_TCHECK2(bp[4], 4);
303         ND_PRINT((ndo, "igmp v2 report %s", ipaddr_string(ndo, &bp[4])));
304         break;
305     case 0x22:
306         ND_PRINT((ndo, "igmp v3 report"));
307 	print_igmpv3_report(ndo, bp, len);
308         break;
309     case 0x17:
310         ND_TCHECK2(bp[4], 4);
311         ND_PRINT((ndo, "igmp leave %s", ipaddr_string(ndo, &bp[4])));
312         break;
313     case 0x13:
314         ND_PRINT((ndo, "igmp dvmrp"));
315         if (len < 8)
316             ND_PRINT((ndo, " [len %d]", len));
317         else
318             dvmrp_print(ndo, bp, len);
319         break;
320     case 0x14:
321         ND_PRINT((ndo, "igmp pimv1"));
322         pimv1_print(ndo, bp, len);
323         break;
324     case 0x1e:
325         print_mresp(ndo, bp, len);
326         break;
327     case 0x1f:
328         print_mtrace(ndo, bp, len);
329         break;
330     default:
331         ND_PRINT((ndo, "igmp-%d", bp[0]));
332         break;
333     }
334 
335     if (ndo->ndo_vflag && ND_TTEST2(bp[0], len)) {
336         /* Check the IGMP checksum */
337         vec[0].ptr = bp;
338         vec[0].len = len;
339         if (in_cksum(vec, 1))
340             ND_PRINT((ndo, " bad igmp cksum %x!", EXTRACT_16BITS(&bp[2])));
341     }
342     return;
343 trunc:
344     ND_PRINT((ndo, "%s", tstr));
345 }
346