Explicitly request literal mode after .Xr.
[netbsd-mini2440.git] / dist / tcpdump / print-dvmrp.c
blob3d3b0e63196574911cbbeda2224d618230add035
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 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-dvmrp.c,v 1.27 2003/11/19 09:42:04 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>
42 #include <stdlib.h>
44 #include "interface.h"
45 #include "extract.h"
46 #include "addrtoname.h"
49 * DVMRP message types and flag values shamelessly stolen from
50 * mrouted/dvmrp.h.
52 #define DVMRP_PROBE 1 /* for finding neighbors */
53 #define DVMRP_REPORT 2 /* for reporting some or all routes */
54 #define DVMRP_ASK_NEIGHBORS 3 /* sent by mapper, asking for a list */
55 /* of this router's neighbors */
56 #define DVMRP_NEIGHBORS 4 /* response to such a request */
57 #define DVMRP_ASK_NEIGHBORS2 5 /* as above, want new format reply */
58 #define DVMRP_NEIGHBORS2 6
59 #define DVMRP_PRUNE 7 /* prune message */
60 #define DVMRP_GRAFT 8 /* graft message */
61 #define DVMRP_GRAFT_ACK 9 /* graft acknowledgement */
64 * 'flags' byte values in DVMRP_NEIGHBORS2 reply.
66 #define DVMRP_NF_TUNNEL 0x01 /* neighbors reached via tunnel */
67 #define DVMRP_NF_SRCRT 0x02 /* tunnel uses IP source routing */
68 #define DVMRP_NF_DOWN 0x10 /* kernel state of interface */
69 #define DVMRP_NF_DISABLED 0x20 /* administratively disabled */
70 #define DVMRP_NF_QUERIER 0x40 /* I am the subnet's querier */
72 static int print_probe(const u_char *, const u_char *, u_int);
73 static int print_report(const u_char *, const u_char *, u_int);
74 static int print_neighbors(const u_char *, const u_char *, u_int);
75 static int print_neighbors2(const u_char *, const u_char *, u_int);
76 static int print_prune(const u_char *);
77 static int print_graft(const u_char *);
78 static int print_graft_ack(const u_char *);
80 static u_int32_t target_level;
82 void
83 dvmrp_print(register const u_char *bp, register u_int len)
85 register const u_char *ep;
86 register u_char type;
88 ep = (const u_char *)snapend;
89 if (bp >= ep)
90 return;
92 TCHECK(bp[1]);
93 type = bp[1];
95 /* Skip IGMP header */
96 bp += 8;
97 len -= 8;
99 switch (type) {
101 case DVMRP_PROBE:
102 printf(" Probe");
103 if (vflag) {
104 if (print_probe(bp, ep, len) < 0)
105 goto trunc;
107 break;
109 case DVMRP_REPORT:
110 printf(" Report");
111 if (vflag > 1) {
112 if (print_report(bp, ep, len) < 0)
113 goto trunc;
115 break;
117 case DVMRP_ASK_NEIGHBORS:
118 printf(" Ask-neighbors(old)");
119 break;
121 case DVMRP_NEIGHBORS:
122 printf(" Neighbors(old)");
123 if (print_neighbors(bp, ep, len) < 0)
124 goto trunc;
125 break;
127 case DVMRP_ASK_NEIGHBORS2:
128 printf(" Ask-neighbors2");
129 break;
131 case DVMRP_NEIGHBORS2:
132 printf(" Neighbors2");
134 * extract version and capabilities from IGMP group
135 * address field
137 bp -= 4;
138 TCHECK2(bp[0], 4);
139 target_level = (bp[0] << 24) | (bp[1] << 16) |
140 (bp[2] << 8) | bp[3];
141 bp += 4;
142 if (print_neighbors2(bp, ep, len) < 0)
143 goto trunc;
144 break;
146 case DVMRP_PRUNE:
147 printf(" Prune");
148 if (print_prune(bp) < 0)
149 goto trunc;
150 break;
152 case DVMRP_GRAFT:
153 printf(" Graft");
154 if (print_graft(bp) < 0)
155 goto trunc;
156 break;
158 case DVMRP_GRAFT_ACK:
159 printf(" Graft-ACK");
160 if (print_graft_ack(bp) < 0)
161 goto trunc;
162 break;
164 default:
165 printf(" [type %d]", type);
166 break;
168 return;
170 trunc:
171 printf("[|dvmrp]");
172 return;
175 static int
176 print_report(register const u_char *bp, register const u_char *ep,
177 register u_int len)
179 register u_int32_t mask, origin;
180 register int metric, done;
181 register u_int i, width;
183 while (len > 0) {
184 if (len < 3) {
185 printf(" [|]");
186 return (0);
188 TCHECK2(bp[0], 3);
189 mask = (u_int32_t)0xff << 24 | bp[0] << 16 | bp[1] << 8 | bp[2];
190 width = 1;
191 if (bp[0])
192 width = 2;
193 if (bp[1])
194 width = 3;
195 if (bp[2])
196 width = 4;
198 printf("\n\tMask %s", intoa(htonl(mask)));
199 bp += 3;
200 len -= 3;
201 do {
202 if (bp + width + 1 > ep) {
203 printf(" [|]");
204 return (0);
206 if (len < width + 1) {
207 printf("\n\t [Truncated Report]");
208 return (0);
210 origin = 0;
211 for (i = 0; i < width; ++i) {
212 TCHECK(*bp);
213 origin = origin << 8 | *bp++;
215 for ( ; i < 4; ++i)
216 origin <<= 8;
218 TCHECK(*bp);
219 metric = *bp++;
220 done = metric & 0x80;
221 metric &= 0x7f;
222 printf("\n\t %s metric %d", intoa(htonl(origin)),
223 metric);
224 len -= width + 1;
225 } while (!done);
227 return (0);
228 trunc:
229 return (-1);
232 static int
233 print_probe(register const u_char *bp, register const u_char *ep,
234 register u_int len)
236 register u_int32_t genid;
238 TCHECK2(bp[0], 4);
239 if ((len < 4) || ((bp + 4) > ep)) {
240 /* { (ctags) */
241 printf(" [|}");
242 return (0);
244 genid = (bp[0] << 24) | (bp[1] << 16) | (bp[2] << 8) | bp[3];
245 bp += 4;
246 len -= 4;
247 if (vflag > 1)
248 printf("\n\t");
249 else
250 printf(" ");
251 printf("genid %u", genid);
252 if (vflag < 2)
253 return (0);
255 while ((len > 0) && (bp < ep)) {
256 TCHECK2(bp[0], 4);
257 printf("\n\tneighbor %s", ipaddr_string(bp));
258 bp += 4; len -= 4;
260 return (0);
261 trunc:
262 return (-1);
265 static int
266 print_neighbors(register const u_char *bp, register const u_char *ep,
267 register u_int len)
269 const u_char *laddr;
270 register u_char metric;
271 register u_char thresh;
272 register int ncount;
274 while (len > 0 && bp < ep) {
275 TCHECK2(bp[0], 7);
276 laddr = bp;
277 bp += 4;
278 metric = *bp++;
279 thresh = *bp++;
280 ncount = *bp++;
281 len -= 7;
282 while (--ncount >= 0) {
283 TCHECK2(bp[0], 4);
284 printf(" [%s ->", ipaddr_string(laddr));
285 printf(" %s, (%d/%d)]",
286 ipaddr_string(bp), metric, thresh);
287 bp += 4;
288 len -= 4;
291 return (0);
292 trunc:
293 return (-1);
296 static int
297 print_neighbors2(register const u_char *bp, register const u_char *ep,
298 register u_int len)
300 const u_char *laddr;
301 register u_char metric, thresh, flags;
302 register int ncount;
304 printf(" (v %d.%d):",
305 (int)target_level & 0xff,
306 (int)(target_level >> 8) & 0xff);
308 while (len > 0 && bp < ep) {
309 TCHECK2(bp[0], 8);
310 laddr = bp;
311 bp += 4;
312 metric = *bp++;
313 thresh = *bp++;
314 flags = *bp++;
315 ncount = *bp++;
316 len -= 8;
317 while (--ncount >= 0 && (len >= 4) && (bp + 4) <= ep) {
318 printf(" [%s -> ", ipaddr_string(laddr));
319 printf("%s (%d/%d", ipaddr_string(bp),
320 metric, thresh);
321 if (flags & DVMRP_NF_TUNNEL)
322 printf("/tunnel");
323 if (flags & DVMRP_NF_SRCRT)
324 printf("/srcrt");
325 if (flags & DVMRP_NF_QUERIER)
326 printf("/querier");
327 if (flags & DVMRP_NF_DISABLED)
328 printf("/disabled");
329 if (flags & DVMRP_NF_DOWN)
330 printf("/down");
331 printf(")]");
332 bp += 4;
333 len -= 4;
335 if (ncount != -1) {
336 printf(" [|]");
337 return (0);
340 return (0);
341 trunc:
342 return (-1);
345 static int
346 print_prune(register const u_char *bp)
348 TCHECK2(bp[0], 12);
349 printf(" src %s grp %s", ipaddr_string(bp), ipaddr_string(bp + 4));
350 bp += 8;
351 (void)printf(" timer ");
352 relts_print(EXTRACT_32BITS(bp));
353 return (0);
354 trunc:
355 return (-1);
358 static int
359 print_graft(register const u_char *bp)
361 TCHECK2(bp[0], 8);
362 printf(" src %s grp %s", ipaddr_string(bp), ipaddr_string(bp + 4));
363 return (0);
364 trunc:
365 return (-1);
368 static int
369 print_graft_ack(register const u_char *bp)
371 TCHECK2(bp[0], 8);
372 printf(" src %s grp %s", ipaddr_string(bp), ipaddr_string(bp + 4));
373 return (0);
374 trunc:
375 return (-1);