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