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