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