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