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