]> The Tcpdump Group git mirrors - tcpdump/blob - print-dvmrp.c
more consistent use of the length: output;
[tcpdump] / print-dvmrp.c
1 /*
2 * Copyright (c) 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-dvmrp.c,v 1.22 2002-08-01 08:53:05 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 #include <stdlib.h>
36
37 #include "interface.h"
38 #include "extract.h"
39 #include "addrtoname.h"
40
41 /*
42 * DVMRP message types and flag values shamelessly stolen from
43 * mrouted/dvmrp.h.
44 */
45 #define DVMRP_PROBE 1 /* for finding neighbors */
46 #define DVMRP_REPORT 2 /* for reporting some or all routes */
47 #define DVMRP_ASK_NEIGHBORS 3 /* sent by mapper, asking for a list */
48 /* of this router's neighbors */
49 #define DVMRP_NEIGHBORS 4 /* response to such a request */
50 #define DVMRP_ASK_NEIGHBORS2 5 /* as above, want new format reply */
51 #define DVMRP_NEIGHBORS2 6
52 #define DVMRP_PRUNE 7 /* prune message */
53 #define DVMRP_GRAFT 8 /* graft message */
54 #define DVMRP_GRAFT_ACK 9 /* graft acknowledgement */
55
56 /*
57 * 'flags' byte values in DVMRP_NEIGHBORS2 reply.
58 */
59 #define DVMRP_NF_TUNNEL 0x01 /* neighbors reached via tunnel */
60 #define DVMRP_NF_SRCRT 0x02 /* tunnel uses IP source routing */
61 #define DVMRP_NF_DOWN 0x10 /* kernel state of interface */
62 #define DVMRP_NF_DISABLED 0x20 /* administratively disabled */
63 #define DVMRP_NF_QUERIER 0x40 /* I am the subnet's querier */
64
65 static void print_probe(const u_char *, const u_char *, u_int);
66 static void print_report(const u_char *, const u_char *, u_int);
67 static void print_neighbors(const u_char *, const u_char *, u_int);
68 static void print_neighbors2(const u_char *, const u_char *, u_int);
69 static void print_prune(const u_char *, const u_char *, u_int);
70 static void print_graft(const u_char *, const u_char *, u_int);
71 static void print_graft_ack(const u_char *, const u_char *, u_int);
72
73 static u_int32_t target_level;
74
75 void
76 dvmrp_print(register const u_char *bp, register u_int len)
77 {
78 register const u_char *ep;
79 register u_char type;
80
81 ep = (const u_char *)snapend;
82 if (bp >= ep)
83 return;
84
85 type = bp[1];
86
87 /* Skip IGMP header */
88 bp += 8;
89 len -= 8;
90
91 switch (type) {
92
93 case DVMRP_PROBE:
94 printf(" Probe");
95 if (vflag)
96 print_probe(bp, ep, len);
97 break;
98
99 case DVMRP_REPORT:
100 printf(" Report");
101 if (vflag > 1)
102 print_report(bp, ep, len);
103 break;
104
105 case DVMRP_ASK_NEIGHBORS:
106 printf(" Ask-neighbors(old)");
107 break;
108
109 case DVMRP_NEIGHBORS:
110 printf(" Neighbors(old)");
111 print_neighbors(bp, ep, len);
112 break;
113
114 case DVMRP_ASK_NEIGHBORS2:
115 printf(" Ask-neighbors2");
116 break;
117
118 case DVMRP_NEIGHBORS2:
119 printf(" Neighbors2");
120 /*
121 * extract version and capabilities from IGMP group
122 * address field
123 */
124 bp -= 4;
125 target_level = (bp[0] << 24) | (bp[1] << 16) |
126 (bp[2] << 8) | bp[3];
127 bp += 4;
128 print_neighbors2(bp, ep, len);
129 break;
130
131 case DVMRP_PRUNE:
132 printf(" Prune");
133 print_prune(bp, ep, len);
134 break;
135
136 case DVMRP_GRAFT:
137 printf(" Graft");
138 print_graft(bp, ep, len);
139 break;
140
141 case DVMRP_GRAFT_ACK:
142 printf(" Graft-ACK");
143 print_graft_ack(bp, ep, len);
144 break;
145
146 default:
147 printf(" [type %d]", type);
148 break;
149 }
150 }
151
152 static void
153 print_report(register const u_char *bp, register const u_char *ep,
154 register u_int len)
155 {
156 register u_int32_t mask, origin;
157 register int metric, i, width, done;
158
159 while (len > 0) {
160 if (len < 3) {
161 printf(" [|]");
162 return;
163 }
164 mask = (u_int32_t)0xff << 24 | bp[0] << 16 | bp[1] << 8 | bp[2];
165 width = 1;
166 if (bp[0])
167 width = 2;
168 if (bp[1])
169 width = 3;
170 if (bp[2])
171 width = 4;
172
173 printf("\n\tMask %s", intoa(htonl(mask)));
174 bp += 3;
175 len -= 3;
176 do {
177 if (bp + width + 1 > ep) {
178 printf(" [|]");
179 return;
180 }
181 if (len < width + 1) {
182 printf("\n\t [Truncated Report]");
183 return;
184 }
185 origin = 0;
186 for (i = 0; i < width; ++i)
187 origin = origin << 8 | *bp++;
188 for ( ; i < 4; ++i)
189 origin <<= 8;
190
191 metric = *bp++;
192 done = metric & 0x80;
193 metric &= 0x7f;
194 printf("\n\t %s metric %d", intoa(htonl(origin)),
195 metric);
196 len -= width + 1;
197 } while (!done);
198 }
199 }
200
201 static void
202 print_probe(register const u_char *bp, register const u_char *ep,
203 register u_int len)
204 {
205 register u_int32_t genid;
206
207 TCHECK2(bp[0], 4);
208 if ((len < 4) || ((bp + 4) > ep)) {
209 /* { (ctags) */
210 printf(" [|}");
211 return;
212 }
213 genid = (bp[0] << 24) | (bp[1] << 16) | (bp[2] << 8) | bp[3];
214 bp += 4;
215 len -= 4;
216 if (vflag > 1)
217 printf("\n\t");
218 else
219 printf(" ");
220 printf("genid %u", genid);
221 if (vflag < 2)
222 return;
223
224 while ((len > 0) && (bp < ep)) {
225 TCHECK2(bp[0], 4);
226 printf("\n\tneighbor %s", ipaddr_string(bp));
227 bp += 4; len -= 4;
228 }
229 return;
230 trunc:
231 (void)printf("[|dvmrp]");
232 }
233
234 static void
235 print_neighbors(register const u_char *bp, register const u_char *ep,
236 register u_int len)
237 {
238 const u_char *laddr;
239 register u_char metric;
240 register u_char thresh;
241 register int ncount;
242
243 while (len > 0 && bp < ep) {
244 TCHECK2(bp[0], 7);
245 laddr = bp;
246 bp += 4;
247 metric = *bp++;
248 thresh = *bp++;
249 ncount = *bp++;
250 len -= 7;
251 while (--ncount >= 0) {
252 TCHECK2(bp[0], 4);
253 printf(" [%s ->", ipaddr_string(laddr));
254 printf(" %s, (%d/%d)]",
255 ipaddr_string(bp), metric, thresh);
256 bp += 4;
257 len -= 4;
258 }
259 }
260 return;
261 trunc:
262 (void)printf("[|dvmrp]");
263 }
264
265 static void
266 print_neighbors2(register const u_char *bp, register const u_char *ep,
267 register u_int len)
268 {
269 const u_char *laddr;
270 register u_char metric, thresh, flags;
271 register int ncount;
272
273 printf(" (v %d.%d):",
274 (int)target_level & 0xff,
275 (int)(target_level >> 8) & 0xff);
276
277 while (len > 0 && bp < ep) {
278 TCHECK2(bp[0], 8);
279 laddr = bp;
280 bp += 4;
281 metric = *bp++;
282 thresh = *bp++;
283 flags = *bp++;
284 ncount = *bp++;
285 len -= 8;
286 while (--ncount >= 0 && (len >= 4) && (bp + 4) <= ep) {
287 printf(" [%s -> ", ipaddr_string(laddr));
288 printf("%s (%d/%d", ipaddr_string(bp),
289 metric, thresh);
290 if (flags & DVMRP_NF_TUNNEL)
291 printf("/tunnel");
292 if (flags & DVMRP_NF_SRCRT)
293 printf("/srcrt");
294 if (flags & DVMRP_NF_QUERIER)
295 printf("/querier");
296 if (flags & DVMRP_NF_DISABLED)
297 printf("/disabled");
298 if (flags & DVMRP_NF_DOWN)
299 printf("/down");
300 printf(")]");
301 bp += 4;
302 len -= 4;
303 }
304 if (ncount != -1) {
305 printf(" [|]");
306 return;
307 }
308 }
309 return;
310 trunc:
311 (void)printf("[|dvmrp]");
312 }
313
314 static void
315 print_prune(register const u_char *bp, register const u_char *ep,
316 register u_int len)
317 {
318 TCHECK2(bp[0], 12);
319 printf(" src %s grp %s", ipaddr_string(bp), ipaddr_string(bp + 4));
320 bp += 8;
321 (void)printf(" timer ");
322 relts_print(EXTRACT_32BITS(bp));
323 return;
324 trunc:
325 (void)printf("[|dvmrp]");
326 }
327
328 static void
329 print_graft(register const u_char *bp, register const u_char *ep,
330 register u_int len)
331 {
332 TCHECK2(bp[0], 8);
333 printf(" src %s grp %s", ipaddr_string(bp), ipaddr_string(bp + 4));
334 return;
335 trunc:
336 (void)printf("[|dvmrp]");
337 }
338
339 static void
340 print_graft_ack(register const u_char *bp, register const u_char *ep,
341 register u_int len)
342 {
343 TCHECK2(bp[0], 8);
344 printf(" src %s grp %s", ipaddr_string(bp), ipaddr_string(bp + 4));
345 return;
346 trunc:
347 (void)printf("[|dvmrp]");
348 }