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