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