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