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