]> The Tcpdump Group git mirrors - tcpdump/blob - print-igmp.c
Merge branch 'master' into fix_udp_frag_badlen
[tcpdump] / print-igmp.c
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 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include "netdissect-stdinc.h"
37
38 #include "netdissect.h"
39 #include "addrtoname.h"
40 #include "extract.h"
41
42 #ifndef IN_CLASSD
43 #define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000)
44 #endif
45
46
47 /* (following from ipmulti/mrouted/prune.h) */
48
49 /*
50 * The packet format for a traceroute request.
51 */
52 struct tr_query {
53 nd_uint32_t tr_src; /* traceroute source */
54 nd_uint32_t tr_dst; /* traceroute destination */
55 nd_uint32_t tr_raddr; /* traceroute response address */
56 nd_uint8_t tr_rttl; /* response ttl */
57 nd_uint24_t tr_qid; /* qid */
58 };
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 nd_uint32_t tr_qarr; /* query arrival time */
66 nd_uint32_t tr_inaddr; /* incoming interface address */
67 nd_uint32_t tr_outaddr; /* outgoing interface address */
68 nd_uint32_t tr_rmtaddr; /* parent address in source tree */
69 nd_uint32_t tr_vifin; /* input packet count on interface */
70 nd_uint32_t tr_vifout; /* output packet count on interface */
71 nd_uint32_t tr_pktcnt; /* total incoming packets for src-grp */
72 nd_uint8_t tr_rproto; /* routing proto deployed on router */
73 nd_uint8_t tr_fttl; /* ttl required to forward on outvif */
74 nd_uint8_t tr_smask; /* subnet mask for src addr */
75 nd_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 const u_char *bp, u_int len)
113 {
114 const struct tr_query *tr = (const struct tr_query *)(bp + 8);
115
116 ND_TCHECK_SIZE(tr);
117 if (len < 8 + sizeof (struct tr_query)) {
118 ND_PRINT(" [invalid len %u]", len);
119 return;
120 }
121 ND_PRINT("mtrace %u: %s to %s reply-to %s",
122 GET_BE_U_3(tr->tr_qid),
123 GET_IPADDR_STRING(tr->tr_src), GET_IPADDR_STRING(tr->tr_dst),
124 GET_IPADDR_STRING(tr->tr_raddr));
125 if (IN_CLASSD(GET_BE_U_4(tr->tr_raddr)))
126 ND_PRINT(" with-ttl %u", GET_U_1(tr->tr_rttl));
127 return;
128 trunc:
129 nd_print_trunc(ndo);
130 }
131
132 static void
133 print_mresp(netdissect_options *ndo,
134 const u_char *bp, u_int len)
135 {
136 const struct tr_query *tr = (const struct tr_query *)(bp + 8);
137
138 ND_TCHECK_SIZE(tr);
139 if (len < 8 + sizeof (struct tr_query)) {
140 ND_PRINT(" [invalid len %u]", len);
141 return;
142 }
143 ND_PRINT("mresp %u: %s to %s reply-to %s",
144 GET_BE_U_3(tr->tr_qid),
145 GET_IPADDR_STRING(tr->tr_src), GET_IPADDR_STRING(tr->tr_dst),
146 GET_IPADDR_STRING(tr->tr_raddr));
147 if (IN_CLASSD(GET_BE_U_4(tr->tr_raddr)))
148 ND_PRINT(" with-ttl %u", GET_U_1(tr->tr_rttl));
149 return;
150 trunc:
151 nd_print_trunc(ndo);
152 }
153
154 static void
155 print_igmpv3_report(netdissect_options *ndo,
156 const u_char *bp, u_int len)
157 {
158 u_int group, nsrcs, ngroups;
159 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(" [invalid len %u]", len);
164 return;
165 }
166 ND_TCHECK_2(bp + 6);
167 ngroups = GET_BE_U_2(bp + 6);
168 ND_PRINT(", %u 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(" [invalid number of groups]");
175 return;
176 }
177 ND_TCHECK_4(bp + (group + 4));
178 ND_PRINT(" [gaddr %s", GET_IPADDR_STRING(bp + group + 4));
179 ND_PRINT(" %s", tok2str(igmpv3report2str, " [v3-report-#%u]",
180 GET_U_1(bp + group)));
181 nsrcs = GET_BE_U_2(bp + group + 2);
182 /* Check the number of sources and print them */
183 if (len < group+8+(nsrcs<<2)) {
184 ND_PRINT(" [invalid number of sources %u]", nsrcs);
185 return;
186 }
187 if (ndo->ndo_vflag == 1)
188 ND_PRINT(", %u source(s)", nsrcs);
189 else {
190 /* Print the sources */
191 ND_PRINT(" {");
192 for (j=0; j<nsrcs; j++) {
193 ND_TCHECK_4(bp + (group + 8 + (j << 2)));
194 ND_PRINT(" %s", GET_IPADDR_STRING(bp + group + 8 + (j << 2)));
195 }
196 ND_PRINT(" }");
197 }
198 /* Next group record */
199 group += 8 + (nsrcs << 2);
200 ND_PRINT("]");
201 }
202 }
203 return;
204 trunc:
205 nd_print_trunc(ndo);
206 }
207
208 static void
209 print_igmpv3_query(netdissect_options *ndo,
210 const u_char *bp, u_int len)
211 {
212 u_int mrc;
213 u_int mrt;
214 u_int nsrcs;
215 u_int i;
216
217 ND_PRINT(" v3");
218 /* Minimum len is 12, and should be a multiple of 4 */
219 if (len < 12 || len & 0x03) {
220 ND_PRINT(" [invalid len %u]", len);
221 return;
222 }
223 ND_TCHECK_1(bp + 1);
224 mrc = GET_U_1(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(" [max resp time ");
232 if (mrt < 600) {
233 ND_PRINT("%.1fs", mrt * 0.1);
234 } else {
235 unsigned_relts_print(ndo, mrt / 10);
236 }
237 ND_PRINT("]");
238 }
239 ND_TCHECK_4(bp + 4);
240 if (GET_BE_U_4(bp + 4) == 0)
241 return;
242 ND_PRINT(" [gaddr %s", GET_IPADDR_STRING(bp + 4));
243 ND_TCHECK_2(bp + 10);
244 nsrcs = GET_BE_U_2(bp + 10);
245 if (nsrcs > 0) {
246 if (len < 12 + (nsrcs << 2))
247 ND_PRINT(" [invalid number of sources]");
248 else if (ndo->ndo_vflag > 1) {
249 ND_PRINT(" {");
250 for (i=0; i<nsrcs; i++) {
251 ND_TCHECK_4(bp + (12 + (i << 2)));
252 ND_PRINT(" %s", GET_IPADDR_STRING(bp + 12 + (i << 2)));
253 }
254 ND_PRINT(" }");
255 } else
256 ND_PRINT(", %u source(s)", nsrcs);
257 }
258 ND_PRINT("]");
259 return;
260 trunc:
261 nd_print_trunc(ndo);
262 }
263
264 void
265 igmp_print(netdissect_options *ndo,
266 const u_char *bp, u_int len)
267 {
268 struct cksum_vec vec[1];
269
270 ndo->ndo_protocol = "igmp";
271 if (ndo->ndo_qflag) {
272 ND_PRINT("igmp");
273 return;
274 }
275
276 ND_TCHECK_1(bp);
277 switch (GET_U_1(bp)) {
278 case 0x11:
279 ND_PRINT("igmp query");
280 if (len >= 12)
281 print_igmpv3_query(ndo, bp, len);
282 else {
283 ND_TCHECK_1(bp + 1);
284 if (GET_U_1(bp + 1)) {
285 ND_PRINT(" v2");
286 if (GET_U_1(bp + 1) != 100)
287 ND_PRINT(" [max resp time %u]", GET_U_1(bp + 1));
288 } else
289 ND_PRINT(" v1");
290 ND_TCHECK_4(bp + 4);
291 if (GET_BE_U_4(bp + 4))
292 ND_PRINT(" [gaddr %s]", GET_IPADDR_STRING(bp + 4));
293 if (len != 8)
294 ND_PRINT(" [len %u]", len);
295 }
296 break;
297 case 0x12:
298 ND_TCHECK_4(bp + 4);
299 ND_PRINT("igmp v1 report %s", GET_IPADDR_STRING(bp + 4));
300 if (len != 8)
301 ND_PRINT(" [len %u]", len);
302 break;
303 case 0x16:
304 ND_TCHECK_4(bp + 4);
305 ND_PRINT("igmp v2 report %s", GET_IPADDR_STRING(bp + 4));
306 break;
307 case 0x22:
308 ND_PRINT("igmp v3 report");
309 print_igmpv3_report(ndo, bp, len);
310 break;
311 case 0x17:
312 ND_TCHECK_4(bp + 4);
313 ND_PRINT("igmp leave %s", GET_IPADDR_STRING(bp + 4));
314 break;
315 case 0x13:
316 ND_PRINT("igmp dvmrp");
317 if (len < 8)
318 ND_PRINT(" [len %u]", len);
319 else
320 dvmrp_print(ndo, bp, len);
321 break;
322 case 0x14:
323 ND_PRINT("igmp pimv1");
324 pimv1_print(ndo, bp, len);
325 break;
326 case 0x1e:
327 print_mresp(ndo, bp, len);
328 break;
329 case 0x1f:
330 print_mtrace(ndo, bp, len);
331 break;
332 default:
333 ND_PRINT("igmp-%u", GET_U_1(bp));
334 break;
335 }
336
337 if (ndo->ndo_vflag && len >= 4 && ND_TTEST_LEN(bp, len)) {
338 /* Check the IGMP checksum */
339 vec[0].ptr = bp;
340 vec[0].len = len;
341 if (in_cksum(vec, 1))
342 ND_PRINT(" bad igmp cksum %x!", GET_BE_U_2(bp + 2));
343 }
344 return;
345 trunc:
346 nd_print_trunc(ndo);
347 }