]> The Tcpdump Group git mirrors - tcpdump/blob - print-dvmrp.c
Remove some now redundant ND_TCHECK_LEN(e, sizeof(nd_ipv6)) calls
[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 * See: RFC 1075 and draft-ietf-idmr-dvmrp-v3
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(netdissect_options *, const u_char *, const u_char *, u_int);
61 static int print_report(netdissect_options *, const u_char *, const u_char *, u_int);
62 static int print_neighbors(netdissect_options *, const u_char *, const u_char *, u_int);
63 static int print_neighbors2(netdissect_options *, const u_char *, const u_char *, u_int, uint8_t, uint8_t);
64 static int print_prune(netdissect_options *, const u_char *);
65 static int print_graft(netdissect_options *, const u_char *);
66 static int print_graft_ack(netdissect_options *, const u_char *);
67
68 void
69 dvmrp_print(netdissect_options *ndo,
70 const u_char *bp, u_int len)
71 {
72 const u_char *ep;
73 u_char type;
74 uint8_t major_version, minor_version;
75
76 ndo->ndo_protocol = "dvmrp";
77 ep = ndo->ndo_snapend;
78 if (bp >= ep)
79 return;
80
81 type = GET_U_1(bp + 1);
82
83 /* Skip IGMP header */
84 bp += 8;
85 len -= 8;
86
87 switch (type) {
88
89 case DVMRP_PROBE:
90 ND_PRINT(" Probe");
91 if (ndo->ndo_vflag) {
92 if (print_probe(ndo, bp, ep, len) < 0)
93 goto trunc;
94 }
95 break;
96
97 case DVMRP_REPORT:
98 ND_PRINT(" Report");
99 if (ndo->ndo_vflag > 1) {
100 if (print_report(ndo, bp, ep, len) < 0)
101 goto trunc;
102 }
103 break;
104
105 case DVMRP_ASK_NEIGHBORS:
106 ND_PRINT(" Ask-neighbors(old)");
107 break;
108
109 case DVMRP_NEIGHBORS:
110 ND_PRINT(" Neighbors(old)");
111 if (print_neighbors(ndo, bp, ep, len) < 0)
112 goto trunc;
113 break;
114
115 case DVMRP_ASK_NEIGHBORS2:
116 ND_PRINT(" Ask-neighbors2");
117 break;
118
119 case DVMRP_NEIGHBORS2:
120 ND_PRINT(" Neighbors2");
121 /*
122 * extract version from IGMP group address field
123 */
124 bp -= 4;
125 ND_TCHECK_4(bp);
126 major_version = GET_U_1(bp + 3);
127 minor_version = GET_U_1(bp + 2);
128 bp += 4;
129 if (print_neighbors2(ndo, bp, ep, len, major_version,
130 minor_version) < 0)
131 goto trunc;
132 break;
133
134 case DVMRP_PRUNE:
135 ND_PRINT(" Prune");
136 if (print_prune(ndo, bp) < 0)
137 goto trunc;
138 break;
139
140 case DVMRP_GRAFT:
141 ND_PRINT(" Graft");
142 if (print_graft(ndo, bp) < 0)
143 goto trunc;
144 break;
145
146 case DVMRP_GRAFT_ACK:
147 ND_PRINT(" Graft-ACK");
148 if (print_graft_ack(ndo, bp) < 0)
149 goto trunc;
150 break;
151
152 default:
153 ND_PRINT(" [type %u]", type);
154 break;
155 }
156 return;
157
158 trunc:
159 nd_print_trunc(ndo);
160 return;
161 }
162
163 static int
164 print_report(netdissect_options *ndo,
165 const u_char *bp, const u_char *ep,
166 u_int len)
167 {
168 uint32_t mask, origin;
169 u_int metric, done;
170 u_int i, width;
171
172 while (len > 0) {
173 if (len < 3) {
174 ND_PRINT(" [|]");
175 return (0);
176 }
177 ND_TCHECK_3(bp);
178 mask = (uint32_t)0xff << 24 | GET_U_1(bp) << 16 |
179 GET_U_1(bp + 1) << 8 | GET_U_1(bp + 2);
180 width = 1;
181 if (GET_U_1(bp))
182 width = 2;
183 if (GET_U_1(bp + 1))
184 width = 3;
185 if (GET_U_1(bp + 2))
186 width = 4;
187
188 ND_PRINT("\n\tMask %s", intoa(htonl(mask)));
189 bp += 3;
190 len -= 3;
191 do {
192 if (bp + width + 1 > ep) {
193 ND_PRINT(" [|]");
194 return (0);
195 }
196 if (len < width + 1) {
197 ND_PRINT("\n\t [Truncated Report]");
198 return (0);
199 }
200 origin = 0;
201 for (i = 0; i < width; ++i) {
202 origin = origin << 8 | GET_U_1(bp);
203 bp++;
204 }
205 for ( ; i < 4; ++i)
206 origin <<= 8;
207
208 metric = GET_U_1(bp);
209 bp++;
210 done = metric & 0x80;
211 metric &= 0x7f;
212 ND_PRINT("\n\t %s metric %u", 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 const u_char *bp, const u_char *ep,
225 u_int len)
226 {
227 uint32_t genid;
228
229 ND_TCHECK_4(bp);
230 if ((len < 4) || ((bp + 4) > ep)) {
231 /* { (ctags) */
232 ND_PRINT(" [|}");
233 return (0);
234 }
235 genid = GET_BE_U_4(bp);
236 bp += 4;
237 len -= 4;
238 ND_PRINT(ndo->ndo_vflag > 1 ? "\n\t" : " ");
239 ND_PRINT("genid %u", genid);
240 if (ndo->ndo_vflag < 2)
241 return (0);
242
243 while ((len > 0) && (bp < ep)) {
244 ND_TCHECK_4(bp);
245 ND_PRINT("\n\tneighbor %s", GET_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(netdissect_options *ndo,
255 const u_char *bp, const u_char *ep,
256 u_int len)
257 {
258 const u_char *laddr;
259 u_char metric;
260 u_char thresh;
261 int ncount;
262
263 while (len > 0 && bp < ep) {
264 ND_TCHECK_7(bp);
265 laddr = bp;
266 bp += 4;
267 metric = GET_U_1(bp);
268 bp++;
269 thresh = GET_U_1(bp);
270 bp++;
271 ncount = GET_U_1(bp);
272 bp++;
273 len -= 7;
274 while (--ncount >= 0) {
275 ND_TCHECK_4(bp);
276 ND_PRINT(" [%s ->", GET_IPADDR_STRING(laddr));
277 ND_PRINT(" %s, (%u/%u)]",
278 GET_IPADDR_STRING(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 const u_char *bp, const u_char *ep,
291 u_int len, uint8_t major_version,
292 uint8_t minor_version)
293 {
294 const u_char *laddr;
295 u_char metric, thresh, flags;
296 int ncount;
297
298 ND_PRINT(" (v %u.%u):", major_version, minor_version);
299
300 while (len > 0 && bp < ep) {
301 ND_TCHECK_8(bp);
302 laddr = bp;
303 bp += 4;
304 metric = GET_U_1(bp);
305 bp++;
306 thresh = GET_U_1(bp);
307 bp++;
308 flags = GET_U_1(bp);
309 bp++;
310 ncount = GET_U_1(bp);
311 bp++;
312 len -= 8;
313 while (--ncount >= 0 && (len >= 4) && (bp + 4) <= ep) {
314 ND_PRINT(" [%s -> ", GET_IPADDR_STRING(laddr));
315 ND_PRINT("%s (%u/%u", GET_IPADDR_STRING(bp),
316 metric, thresh);
317 if (flags & DVMRP_NF_TUNNEL)
318 ND_PRINT("/tunnel");
319 if (flags & DVMRP_NF_SRCRT)
320 ND_PRINT("/srcrt");
321 if (flags & DVMRP_NF_QUERIER)
322 ND_PRINT("/querier");
323 if (flags & DVMRP_NF_DISABLED)
324 ND_PRINT("/disabled");
325 if (flags & DVMRP_NF_DOWN)
326 ND_PRINT("/down");
327 ND_PRINT(")]");
328 bp += 4;
329 len -= 4;
330 }
331 if (ncount != -1) {
332 ND_PRINT(" [|]");
333 return (0);
334 }
335 }
336 return (0);
337 trunc:
338 return (-1);
339 }
340
341 static int
342 print_prune(netdissect_options *ndo,
343 const u_char *bp)
344 {
345 ND_TCHECK_LEN(bp, 12);
346 ND_PRINT(" src %s grp %s", GET_IPADDR_STRING(bp), GET_IPADDR_STRING(bp + 4));
347 bp += 8;
348 ND_PRINT(" timer ");
349 unsigned_relts_print(ndo, GET_BE_U_4(bp));
350 return (0);
351 trunc:
352 return (-1);
353 }
354
355 static int
356 print_graft(netdissect_options *ndo,
357 const u_char *bp)
358 {
359 ND_TCHECK_8(bp);
360 ND_PRINT(" src %s grp %s", GET_IPADDR_STRING(bp), GET_IPADDR_STRING(bp + 4));
361 return (0);
362 trunc:
363 return (-1);
364 }
365
366 static int
367 print_graft_ack(netdissect_options *ndo,
368 const u_char *bp)
369 {
370 ND_TCHECK_8(bp);
371 ND_PRINT(" src %s grp %s", GET_IPADDR_STRING(bp), GET_IPADDR_STRING(bp + 4));
372 return (0);
373 trunc:
374 return (-1);
375 }