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