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