print-igmp.c revision 1.4 1 1.1 christos /*
2 1.1 christos * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996
3 1.1 christos * The Regents of the University of California. All rights reserved.
4 1.1 christos *
5 1.1 christos * Redistribution and use in source and binary forms, with or without
6 1.1 christos * modification, are permitted provided that: (1) source code distributions
7 1.1 christos * retain the above copyright notice and this paragraph in its entirety, (2)
8 1.1 christos * distributions including binary code include the above copyright notice and
9 1.1 christos * this paragraph in its entirety in the documentation or other materials
10 1.1 christos * provided with the distribution, and (3) all advertising materials mentioning
11 1.1 christos * features or use of this software display the following acknowledgement:
12 1.1 christos * ``This product includes software developed by the University of California,
13 1.1 christos * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 1.1 christos * the University nor the names of its contributors may be used to endorse
15 1.1 christos * or promote products derived from this software without specific prior
16 1.1 christos * written permission.
17 1.1 christos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 1.1 christos * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 1.1 christos * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 1.1 christos */
21 1.1 christos
22 1.2 christos #include <sys/cdefs.h>
23 1.1 christos #ifndef lint
24 1.2 christos #if 0
25 1.1 christos static const char rcsid[] _U_ =
26 1.3 christos "@(#) Header: /tcpdump/master/tcpdump/print-igmp.c,v 1.15 2004-03-24 00:59:16 guy Exp (LBL)";
27 1.2 christos #else
28 1.4 christos __RCSID("$NetBSD: print-igmp.c,v 1.4 2013/12/31 17:33:31 christos Exp $");
29 1.2 christos #endif
30 1.1 christos #endif
31 1.1 christos
32 1.1 christos #ifdef HAVE_CONFIG_H
33 1.1 christos #include "config.h"
34 1.1 christos #endif
35 1.1 christos
36 1.1 christos #include <tcpdump-stdinc.h>
37 1.1 christos
38 1.1 christos #include <stdio.h>
39 1.1 christos #include <string.h>
40 1.1 christos
41 1.1 christos #include "interface.h"
42 1.1 christos #include "addrtoname.h"
43 1.1 christos #include "extract.h" /* must come after interface.h */
44 1.1 christos
45 1.1 christos #ifndef IN_CLASSD
46 1.1 christos #define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000)
47 1.1 christos #endif
48 1.1 christos
49 1.1 christos /* (following from ipmulti/mrouted/prune.h) */
50 1.1 christos
51 1.1 christos /*
52 1.1 christos * The packet format for a traceroute request.
53 1.1 christos */
54 1.1 christos struct tr_query {
55 1.1 christos u_int32_t tr_src; /* traceroute source */
56 1.1 christos u_int32_t tr_dst; /* traceroute destination */
57 1.1 christos u_int32_t tr_raddr; /* traceroute response address */
58 1.1 christos u_int32_t tr_rttlqid; /* response ttl and qid */
59 1.1 christos };
60 1.1 christos
61 1.1 christos #define TR_GETTTL(x) (int)(((x) >> 24) & 0xff)
62 1.1 christos #define TR_GETQID(x) ((x) & 0x00ffffff)
63 1.1 christos
64 1.1 christos /*
65 1.1 christos * Traceroute response format. A traceroute response has a tr_query at the
66 1.1 christos * beginning, followed by one tr_resp for each hop taken.
67 1.1 christos */
68 1.1 christos struct tr_resp {
69 1.1 christos u_int32_t tr_qarr; /* query arrival time */
70 1.1 christos u_int32_t tr_inaddr; /* incoming interface address */
71 1.1 christos u_int32_t tr_outaddr; /* outgoing interface address */
72 1.1 christos u_int32_t tr_rmtaddr; /* parent address in source tree */
73 1.1 christos u_int32_t tr_vifin; /* input packet count on interface */
74 1.1 christos u_int32_t tr_vifout; /* output packet count on interface */
75 1.1 christos u_int32_t tr_pktcnt; /* total incoming packets for src-grp */
76 1.1 christos u_int8_t tr_rproto; /* routing proto deployed on router */
77 1.1 christos u_int8_t tr_fttl; /* ttl required to forward on outvif */
78 1.1 christos u_int8_t tr_smask; /* subnet mask for src addr */
79 1.1 christos u_int8_t tr_rflags; /* forwarding error codes */
80 1.1 christos };
81 1.1 christos
82 1.1 christos /* defs within mtrace */
83 1.1 christos #define TR_QUERY 1
84 1.1 christos #define TR_RESP 2
85 1.1 christos
86 1.1 christos /* fields for tr_rflags (forwarding error codes) */
87 1.1 christos #define TR_NO_ERR 0
88 1.1 christos #define TR_WRONG_IF 1
89 1.1 christos #define TR_PRUNED 2
90 1.1 christos #define TR_OPRUNED 3
91 1.1 christos #define TR_SCOPED 4
92 1.1 christos #define TR_NO_RTE 5
93 1.1 christos #define TR_NO_FWD 7
94 1.1 christos #define TR_NO_SPACE 0x81
95 1.1 christos #define TR_OLD_ROUTER 0x82
96 1.1 christos
97 1.1 christos /* fields for tr_rproto (routing protocol) */
98 1.1 christos #define TR_PROTO_DVMRP 1
99 1.1 christos #define TR_PROTO_MOSPF 2
100 1.1 christos #define TR_PROTO_PIM 3
101 1.1 christos #define TR_PROTO_CBT 4
102 1.1 christos
103 1.1 christos /* igmpv3 report types */
104 1.4 christos static const struct tok igmpv3report2str[] = {
105 1.1 christos { 1, "is_in" },
106 1.1 christos { 2, "is_ex" },
107 1.1 christos { 3, "to_in" },
108 1.1 christos { 4, "to_ex" },
109 1.1 christos { 5, "allow" },
110 1.1 christos { 6, "block" },
111 1.1 christos { 0, NULL }
112 1.1 christos };
113 1.1 christos
114 1.1 christos static void
115 1.1 christos print_mtrace(register const u_char *bp, register u_int len)
116 1.1 christos {
117 1.1 christos register const struct tr_query *tr = (const struct tr_query *)(bp + 8);
118 1.1 christos
119 1.1 christos TCHECK(*tr);
120 1.1 christos if (len < 8 + sizeof (struct tr_query)) {
121 1.1 christos (void)printf(" [invalid len %d]", len);
122 1.1 christos return;
123 1.1 christos }
124 1.1 christos printf("mtrace %u: %s to %s reply-to %s",
125 1.1 christos TR_GETQID(EXTRACT_32BITS(&tr->tr_rttlqid)),
126 1.1 christos ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst),
127 1.1 christos ipaddr_string(&tr->tr_raddr));
128 1.1 christos if (IN_CLASSD(EXTRACT_32BITS(&tr->tr_raddr)))
129 1.1 christos printf(" with-ttl %d", TR_GETTTL(EXTRACT_32BITS(&tr->tr_rttlqid)));
130 1.1 christos return;
131 1.1 christos trunc:
132 1.1 christos (void)printf("[|igmp]");
133 1.1 christos return;
134 1.1 christos }
135 1.1 christos
136 1.1 christos static void
137 1.1 christos print_mresp(register const u_char *bp, register u_int len)
138 1.1 christos {
139 1.1 christos register const struct tr_query *tr = (const struct tr_query *)(bp + 8);
140 1.1 christos
141 1.1 christos TCHECK(*tr);
142 1.1 christos if (len < 8 + sizeof (struct tr_query)) {
143 1.1 christos (void)printf(" [invalid len %d]", len);
144 1.1 christos return;
145 1.1 christos }
146 1.1 christos printf("mresp %lu: %s to %s reply-to %s",
147 1.1 christos (u_long)TR_GETQID(EXTRACT_32BITS(&tr->tr_rttlqid)),
148 1.1 christos ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst),
149 1.1 christos ipaddr_string(&tr->tr_raddr));
150 1.1 christos if (IN_CLASSD(EXTRACT_32BITS(&tr->tr_raddr)))
151 1.1 christos printf(" with-ttl %d", TR_GETTTL(EXTRACT_32BITS(&tr->tr_rttlqid)));
152 1.1 christos return;
153 1.1 christos trunc:
154 1.1 christos (void)printf("[|igmp]");
155 1.1 christos return;
156 1.1 christos }
157 1.1 christos
158 1.1 christos static void
159 1.1 christos print_igmpv3_report(register const u_char *bp, register u_int len)
160 1.1 christos {
161 1.1 christos u_int group, nsrcs, ngroups;
162 1.1 christos register u_int i, j;
163 1.1 christos
164 1.1 christos /* Minimum len is 16, and should be a multiple of 4 */
165 1.1 christos if (len < 16 || len & 0x03) {
166 1.1 christos (void)printf(" [invalid len %d]", len);
167 1.1 christos return;
168 1.1 christos }
169 1.1 christos TCHECK2(bp[6], 2);
170 1.1 christos ngroups = EXTRACT_16BITS(&bp[6]);
171 1.1 christos (void)printf(", %d group record(s)", ngroups);
172 1.1 christos if (vflag > 0) {
173 1.1 christos /* Print the group records */
174 1.1 christos group = 8;
175 1.1 christos for (i=0; i<ngroups; i++) {
176 1.1 christos if (len < group+8) {
177 1.1 christos (void)printf(" [invalid number of groups]");
178 1.1 christos return;
179 1.1 christos }
180 1.1 christos TCHECK2(bp[group+4], 4);
181 1.1 christos (void)printf(" [gaddr %s", ipaddr_string(&bp[group+4]));
182 1.1 christos (void)printf(" %s", tok2str(igmpv3report2str, " [v3-report-#%d]",
183 1.1 christos bp[group]));
184 1.1 christos nsrcs = EXTRACT_16BITS(&bp[group+2]);
185 1.1 christos /* Check the number of sources and print them */
186 1.1 christos if (len < group+8+(nsrcs<<2)) {
187 1.1 christos (void)printf(" [invalid number of sources %d]", nsrcs);
188 1.1 christos return;
189 1.1 christos }
190 1.1 christos if (vflag == 1)
191 1.1 christos (void)printf(", %d source(s)", nsrcs);
192 1.1 christos else {
193 1.1 christos /* Print the sources */
194 1.1 christos (void)printf(" {");
195 1.1 christos for (j=0; j<nsrcs; j++) {
196 1.1 christos TCHECK2(bp[group+8+(j<<2)], 4);
197 1.1 christos (void)printf(" %s", ipaddr_string(&bp[group+8+(j<<2)]));
198 1.1 christos }
199 1.1 christos (void)printf(" }");
200 1.1 christos }
201 1.1 christos /* Next group record */
202 1.1 christos group += 8 + (nsrcs << 2);
203 1.1 christos (void)printf("]");
204 1.1 christos }
205 1.1 christos }
206 1.1 christos return;
207 1.1 christos trunc:
208 1.1 christos (void)printf("[|igmp]");
209 1.1 christos return;
210 1.1 christos }
211 1.1 christos
212 1.1 christos static void
213 1.1 christos print_igmpv3_query(register const u_char *bp, register u_int len)
214 1.1 christos {
215 1.1 christos u_int mrc;
216 1.1 christos int mrt;
217 1.1 christos u_int nsrcs;
218 1.1 christos register u_int i;
219 1.1 christos
220 1.1 christos (void)printf(" v3");
221 1.1 christos /* Minimum len is 12, and should be a multiple of 4 */
222 1.1 christos if (len < 12 || len & 0x03) {
223 1.1 christos (void)printf(" [invalid len %d]", len);
224 1.1 christos return;
225 1.1 christos }
226 1.1 christos TCHECK(bp[1]);
227 1.1 christos mrc = bp[1];
228 1.1 christos if (mrc < 128) {
229 1.1 christos mrt = mrc;
230 1.1 christos } else {
231 1.1 christos mrt = ((mrc & 0x0f) | 0x10) << (((mrc & 0x70) >> 4) + 3);
232 1.1 christos }
233 1.1 christos if (mrc != 100) {
234 1.1 christos (void)printf(" [max resp time ");
235 1.3 christos if (mrt < 600) {
236 1.3 christos (void)printf("%.1fs", mrt * 0.1);
237 1.3 christos } else {
238 1.3 christos relts_print(mrt / 10);
239 1.3 christos }
240 1.1 christos (void)printf("]");
241 1.1 christos }
242 1.1 christos TCHECK2(bp[4], 4);
243 1.1 christos if (EXTRACT_32BITS(&bp[4]) == 0)
244 1.1 christos return;
245 1.1 christos (void)printf(" [gaddr %s", ipaddr_string(&bp[4]));
246 1.1 christos TCHECK2(bp[10], 2);
247 1.1 christos nsrcs = EXTRACT_16BITS(&bp[10]);
248 1.1 christos if (nsrcs > 0) {
249 1.1 christos if (len < 12 + (nsrcs << 2))
250 1.1 christos (void)printf(" [invalid number of sources]");
251 1.1 christos else if (vflag > 1) {
252 1.1 christos (void)printf(" {");
253 1.1 christos for (i=0; i<nsrcs; i++) {
254 1.1 christos TCHECK2(bp[12+(i<<2)], 4);
255 1.1 christos (void)printf(" %s", ipaddr_string(&bp[12+(i<<2)]));
256 1.1 christos }
257 1.1 christos (void)printf(" }");
258 1.1 christos } else
259 1.1 christos (void)printf(", %d source(s)", nsrcs);
260 1.1 christos }
261 1.1 christos (void)printf("]");
262 1.1 christos return;
263 1.1 christos trunc:
264 1.1 christos (void)printf("[|igmp]");
265 1.1 christos return;
266 1.1 christos }
267 1.1 christos
268 1.1 christos void
269 1.1 christos igmp_print(register const u_char *bp, register u_int len)
270 1.1 christos {
271 1.3 christos struct cksum_vec vec[1];
272 1.3 christos
273 1.1 christos if (qflag) {
274 1.1 christos (void)printf("igmp");
275 1.1 christos return;
276 1.1 christos }
277 1.1 christos
278 1.1 christos TCHECK(bp[0]);
279 1.1 christos switch (bp[0]) {
280 1.1 christos case 0x11:
281 1.1 christos (void)printf("igmp query");
282 1.1 christos if (len >= 12)
283 1.1 christos print_igmpv3_query(bp, len);
284 1.1 christos else {
285 1.1 christos TCHECK(bp[1]);
286 1.1 christos if (bp[1]) {
287 1.1 christos (void)printf(" v2");
288 1.1 christos if (bp[1] != 100)
289 1.1 christos (void)printf(" [max resp time %d]", bp[1]);
290 1.1 christos } else
291 1.1 christos (void)printf(" v1");
292 1.1 christos TCHECK2(bp[4], 4);
293 1.1 christos if (EXTRACT_32BITS(&bp[4]))
294 1.1 christos (void)printf(" [gaddr %s]", ipaddr_string(&bp[4]));
295 1.1 christos if (len != 8)
296 1.1 christos (void)printf(" [len %d]", len);
297 1.1 christos }
298 1.1 christos break;
299 1.1 christos case 0x12:
300 1.1 christos TCHECK2(bp[4], 4);
301 1.1 christos (void)printf("igmp v1 report %s", ipaddr_string(&bp[4]));
302 1.1 christos if (len != 8)
303 1.1 christos (void)printf(" [len %d]", len);
304 1.1 christos break;
305 1.1 christos case 0x16:
306 1.1 christos TCHECK2(bp[4], 4);
307 1.1 christos (void)printf("igmp v2 report %s", ipaddr_string(&bp[4]));
308 1.1 christos break;
309 1.1 christos case 0x22:
310 1.1 christos (void)printf("igmp v3 report");
311 1.1 christos print_igmpv3_report(bp, len);
312 1.1 christos break;
313 1.1 christos case 0x17:
314 1.1 christos TCHECK2(bp[4], 4);
315 1.1 christos (void)printf("igmp leave %s", ipaddr_string(&bp[4]));
316 1.1 christos break;
317 1.1 christos case 0x13:
318 1.1 christos (void)printf("igmp dvmrp");
319 1.1 christos if (len < 8)
320 1.1 christos (void)printf(" [len %d]", len);
321 1.1 christos else
322 1.1 christos dvmrp_print(bp, len);
323 1.1 christos break;
324 1.1 christos case 0x14:
325 1.1 christos (void)printf("igmp pimv1");
326 1.1 christos pimv1_print(bp, len);
327 1.1 christos break;
328 1.1 christos case 0x1e:
329 1.1 christos print_mresp(bp, len);
330 1.1 christos break;
331 1.1 christos case 0x1f:
332 1.1 christos print_mtrace(bp, len);
333 1.1 christos break;
334 1.1 christos default:
335 1.1 christos (void)printf("igmp-%d", bp[0]);
336 1.1 christos break;
337 1.1 christos }
338 1.1 christos
339 1.1 christos if (vflag && TTEST2(bp[0], len)) {
340 1.1 christos /* Check the IGMP checksum */
341 1.3 christos vec[0].ptr = bp;
342 1.3 christos vec[0].len = len;
343 1.3 christos if (in_cksum(vec, 1))
344 1.1 christos printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp[2]));
345 1.1 christos }
346 1.1 christos return;
347 1.1 christos trunc:
348 1.1 christos fputs("[|igmp]", stdout);
349 1.1 christos }
350