]> The Tcpdump Group git mirrors - tcpdump/blob - print-aodv.c
AODV: Modernize packet parsing style.
[tcpdump] / print-aodv.c
1 /*
2 * Copyright (c) 2003 Bruce M. Simpson <bms@spc.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bruce M. Simpson.
16 * 4. Neither the name of Bruce M. Simpson nor the names of co-
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bruce M. Simpson AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Bruce M. Simpson OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /* \summary: Ad hoc On-Demand Distance Vector (AODV) Routing printer */
34 /* specification: RFC 3561 */
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include "netdissect-stdinc.h"
41
42 #include "netdissect.h"
43 #include "addrtoname.h"
44 #include "extract.h"
45
46 struct aodv_rreq {
47 nd_uint8_t rreq_type; /* AODV message type (1) */
48 nd_uint8_t rreq_flags; /* various flags */
49 nd_uint8_t rreq_zero0; /* reserved, set to zero */
50 nd_uint8_t rreq_hops; /* number of hops from originator */
51 nd_uint32_t rreq_id; /* request ID */
52 nd_ipv4 rreq_da; /* destination IPv4 address */
53 nd_uint32_t rreq_ds; /* destination sequence number */
54 nd_ipv4 rreq_oa; /* originator IPv4 address */
55 nd_uint32_t rreq_os; /* originator sequence number */
56 };
57 struct aodv_rreq6 {
58 nd_uint8_t rreq_type; /* AODV message type (1) */
59 nd_uint8_t rreq_flags; /* various flags */
60 nd_uint8_t rreq_zero0; /* reserved, set to zero */
61 nd_uint8_t rreq_hops; /* number of hops from originator */
62 nd_uint32_t rreq_id; /* request ID */
63 nd_ipv6 rreq_da; /* destination IPv6 address */
64 nd_uint32_t rreq_ds; /* destination sequence number */
65 nd_ipv6 rreq_oa; /* originator IPv6 address */
66 nd_uint32_t rreq_os; /* originator sequence number */
67 };
68
69 #define RREQ_JOIN 0x80 /* join (reserved for multicast */
70 #define RREQ_REPAIR 0x40 /* repair (reserved for multicast */
71 #define RREQ_GRAT 0x20 /* gratuitous RREP */
72 #define RREQ_DEST 0x10 /* destination only */
73 #define RREQ_UNKNOWN 0x08 /* unknown destination sequence num */
74 #define RREQ_FLAGS_MASK 0xF8 /* mask for rreq_flags */
75
76 struct aodv_rrep {
77 nd_uint8_t rrep_type; /* AODV message type (2) */
78 nd_uint8_t rrep_flags; /* various flags */
79 nd_uint8_t rrep_ps; /* prefix size */
80 nd_uint8_t rrep_hops; /* number of hops from o to d */
81 nd_ipv4 rrep_da; /* destination IPv4 address */
82 nd_uint32_t rrep_ds; /* destination sequence number */
83 nd_ipv4 rrep_oa; /* originator IPv4 address */
84 nd_uint32_t rrep_life; /* lifetime of this route */
85 };
86 struct aodv_rrep6 {
87 nd_uint8_t rrep_type; /* AODV message type (2) */
88 nd_uint8_t rrep_flags; /* various flags */
89 nd_uint8_t rrep_ps; /* prefix size */
90 nd_uint8_t rrep_hops; /* number of hops from o to d */
91 nd_ipv6 rrep_da; /* destination IPv6 address */
92 nd_uint32_t rrep_ds; /* destination sequence number */
93 nd_ipv6 rrep_oa; /* originator IPv6 address */
94 nd_uint32_t rrep_life; /* lifetime of this route */
95 };
96
97 #define RREP_REPAIR 0x80 /* repair (reserved for multicast */
98 #define RREP_ACK 0x40 /* acknowledgement required */
99 #define RREP_FLAGS_MASK 0xC0 /* mask for rrep_flags */
100 #define RREP_PREFIX_MASK 0x1F /* mask for prefix size */
101
102 struct rerr_unreach {
103 nd_ipv4 u_da; /* IPv4 address */
104 nd_uint32_t u_ds; /* sequence number */
105 };
106 struct rerr_unreach6 {
107 nd_ipv6 u_da; /* IPv6 address */
108 nd_uint32_t u_ds; /* sequence number */
109 };
110
111 struct aodv_rerr {
112 nd_uint8_t rerr_type; /* AODV message type (3 or 18) */
113 nd_uint8_t rerr_flags; /* various flags */
114 nd_uint8_t rerr_zero0; /* reserved, set to zero */
115 nd_uint8_t rerr_dc; /* destination count */
116 };
117
118 #define RERR_NODELETE 0x80 /* don't delete the link */
119 #define RERR_FLAGS_MASK 0x80 /* mask for rerr_flags */
120
121 struct aodv_rrep_ack {
122 nd_uint8_t ra_type;
123 nd_uint8_t ra_zero0;
124 };
125
126 #define AODV_RREQ 1 /* route request */
127 #define AODV_RREP 2 /* route response */
128 #define AODV_RERR 3 /* error report */
129 #define AODV_RREP_ACK 4 /* route response acknowledgement */
130 static const struct tok msg_type_str[] = {
131 { AODV_RREQ, "rreq" },
132 { AODV_RREP, "rrep" },
133 { AODV_RERR, "rerr" },
134 { AODV_RREP_ACK, "rrep-ack" },
135 { 0, NULL }
136 };
137
138 struct aodv_ext {
139 nd_uint8_t type; /* extension type */
140 nd_uint8_t length; /* extension length */
141 };
142
143 struct aodv_hello {
144 struct aodv_ext eh; /* extension header */
145 nd_uint32_t interval; /* expect my next hello in
146 * (n) ms
147 * NOTE: this is not aligned */
148 };
149
150 #define AODV_EXT_HELLO 1
151
152 static void
153 aodv_extension(netdissect_options *ndo,
154 const struct aodv_ext *ep, u_int length)
155 {
156 const struct aodv_hello *ah;
157 uint8_t ext_type, ext_length;
158
159 ext_type = GET_U_1(ep->type);
160 ext_length = GET_U_1(ep->length);
161 switch (ext_type) {
162 case AODV_EXT_HELLO:
163 ah = (const struct aodv_hello *)(const void *)ep;
164 if (length < sizeof(struct aodv_hello)) {
165 ND_PRINT(" (ext data length %u < %zu)", length, sizeof(struct aodv_hello));
166 goto invalid;
167 }
168 if (ext_length < 4) {
169 ND_PRINT("\n\text HELLO - bad length %u", ext_length);
170 goto invalid;
171 }
172 ND_PRINT("\n\text HELLO %u ms",
173 GET_BE_U_4(ah->interval));
174 break;
175
176 default:
177 ND_PRINT("\n\text %u %u", ext_type, ext_length);
178 break;
179 }
180 return;
181
182 invalid:
183 nd_print_invalid(ndo);
184 }
185
186 static void
187 aodv_rreq(netdissect_options *ndo, const u_char *dat, u_int length)
188 {
189 u_int i;
190 const struct aodv_rreq *ap = (const struct aodv_rreq *)dat;
191
192 if (length < sizeof(*ap)) {
193 ND_PRINT(" (message length %u)", length);
194 goto invalid;
195 }
196 ND_PRINT(" %u %s%s%s%s%shops %u id 0x%08x\n"
197 "\tdst %s seq %u src %s seq %u", length,
198 GET_U_1(ap->rreq_type) & RREQ_JOIN ? "[J]" : "",
199 GET_U_1(ap->rreq_type) & RREQ_REPAIR ? "[R]" : "",
200 GET_U_1(ap->rreq_type) & RREQ_GRAT ? "[G]" : "",
201 GET_U_1(ap->rreq_type) & RREQ_DEST ? "[D]" : "",
202 GET_U_1(ap->rreq_type) & RREQ_UNKNOWN ? "[U] " : " ",
203 GET_U_1(ap->rreq_hops),
204 GET_BE_U_4(ap->rreq_id),
205 GET_IPADDR_STRING(ap->rreq_da),
206 GET_BE_U_4(ap->rreq_ds),
207 GET_IPADDR_STRING(ap->rreq_oa),
208 GET_BE_U_4(ap->rreq_os));
209 i = length - sizeof(*ap);
210 if (i >= sizeof(struct aodv_ext))
211 aodv_extension(ndo, (const struct aodv_ext *)(dat + sizeof(*ap)), i);
212 return;
213
214 invalid:
215 nd_print_invalid(ndo);
216 }
217
218 static void
219 aodv_rrep(netdissect_options *ndo, const u_char *dat, u_int length)
220 {
221 u_int i;
222 const struct aodv_rrep *ap = (const struct aodv_rrep *)dat;
223
224 if (length < sizeof(*ap)) {
225 ND_PRINT(" (message length %u)", length);
226 goto invalid;
227 }
228 ND_PRINT(" %u %s%sprefix %u hops %u\n"
229 "\tdst %s dseq %u src %s %u ms", length,
230 GET_U_1(ap->rrep_type) & RREP_REPAIR ? "[R]" : "",
231 GET_U_1(ap->rrep_type) & RREP_ACK ? "[A] " : " ",
232 GET_U_1(ap->rrep_ps) & RREP_PREFIX_MASK,
233 GET_U_1(ap->rrep_hops),
234 GET_IPADDR_STRING(ap->rrep_da),
235 GET_BE_U_4(ap->rrep_ds),
236 GET_IPADDR_STRING(ap->rrep_oa),
237 GET_BE_U_4(ap->rrep_life));
238 i = length - sizeof(*ap);
239 if (i >= sizeof(struct aodv_ext))
240 aodv_extension(ndo, (const struct aodv_ext *)(dat + sizeof(*ap)), i);
241 return;
242
243 invalid:
244 nd_print_invalid(ndo);
245 }
246
247 static void
248 aodv_rerr(netdissect_options *ndo, const u_char *dat, u_int length)
249 {
250 u_int i, dc;
251 const struct aodv_rerr *ap = (const struct aodv_rerr *)dat;
252 const struct rerr_unreach *dp;
253
254 if (length < sizeof(*ap)) {
255 ND_PRINT(" (message length %u)", length);
256 goto invalid;
257 }
258 ND_PRINT(" %s [items %u] [%u]:",
259 GET_U_1(ap->rerr_flags) & RERR_NODELETE ? "[D]" : "",
260 GET_U_1(ap->rerr_dc), length);
261 dp = (const struct rerr_unreach *)(dat + sizeof(*ap));
262 i = length - sizeof(*ap);
263 for (dc = GET_U_1(ap->rerr_dc); dc != 0; dc--) {
264 if (i < sizeof(*dp)) {
265 ND_PRINT(" (remaining length %u)", i);
266 goto invalid;
267 }
268 ND_PRINT(" {%s}(%u)", GET_IPADDR_STRING(dp->u_da),
269 GET_BE_U_4(dp->u_ds));
270 dp++;
271 i -= sizeof(*dp);
272 }
273 return;
274
275 invalid:
276 nd_print_invalid(ndo);
277 }
278
279 static void
280 aodv_v6_rreq(netdissect_options *ndo, const u_char *dat, u_int length)
281 {
282 u_int i;
283 const struct aodv_rreq6 *ap = (const struct aodv_rreq6 *)dat;
284
285 if (length < sizeof(*ap)) {
286 ND_PRINT(" (message length %u)", length);
287 goto invalid;
288 }
289 ND_PRINT(" %u %s%s%s%s%shops %u id 0x%08x\n"
290 "\tdst %s seq %u src %s seq %u", length,
291 GET_U_1(ap->rreq_type) & RREQ_JOIN ? "[J]" : "",
292 GET_U_1(ap->rreq_type) & RREQ_REPAIR ? "[R]" : "",
293 GET_U_1(ap->rreq_type) & RREQ_GRAT ? "[G]" : "",
294 GET_U_1(ap->rreq_type) & RREQ_DEST ? "[D]" : "",
295 GET_U_1(ap->rreq_type) & RREQ_UNKNOWN ? "[U] " : " ",
296 GET_U_1(ap->rreq_hops),
297 GET_BE_U_4(ap->rreq_id),
298 GET_IP6ADDR_STRING(ap->rreq_da),
299 GET_BE_U_4(ap->rreq_ds),
300 GET_IP6ADDR_STRING(ap->rreq_oa),
301 GET_BE_U_4(ap->rreq_os));
302 i = length - sizeof(*ap);
303 if (i >= sizeof(struct aodv_ext))
304 aodv_extension(ndo, (const struct aodv_ext *)(dat + sizeof(*ap)), i);
305 return;
306
307 invalid:
308 nd_print_invalid(ndo);
309 }
310
311 static void
312 aodv_v6_rrep(netdissect_options *ndo, const u_char *dat, u_int length)
313 {
314 u_int i;
315 const struct aodv_rrep6 *ap = (const struct aodv_rrep6 *)dat;
316
317 if (length < sizeof(*ap)) {
318 ND_PRINT(" (message length %u)", length);
319 goto invalid;
320 }
321 ND_PRINT(" %u %s%sprefix %u hops %u\n"
322 "\tdst %s dseq %u src %s %u ms", length,
323 GET_U_1(ap->rrep_type) & RREP_REPAIR ? "[R]" : "",
324 GET_U_1(ap->rrep_type) & RREP_ACK ? "[A] " : " ",
325 GET_U_1(ap->rrep_ps) & RREP_PREFIX_MASK,
326 GET_U_1(ap->rrep_hops),
327 GET_IP6ADDR_STRING(ap->rrep_da),
328 GET_BE_U_4(ap->rrep_ds),
329 GET_IP6ADDR_STRING(ap->rrep_oa),
330 GET_BE_U_4(ap->rrep_life));
331 i = length - sizeof(*ap);
332 if (i >= sizeof(struct aodv_ext))
333 aodv_extension(ndo, (const struct aodv_ext *)(dat + sizeof(*ap)), i);
334 return;
335
336 invalid:
337 nd_print_invalid(ndo);
338 }
339
340 static void
341 aodv_v6_rerr(netdissect_options *ndo, const u_char *dat, u_int length)
342 {
343 u_int i, dc;
344 const struct aodv_rerr *ap = (const struct aodv_rerr *)dat;
345 const struct rerr_unreach6 *dp6;
346
347 if (length < sizeof(*ap)) {
348 ND_PRINT(" (message length %u)", length);
349 goto invalid;
350 }
351 ND_PRINT(" %s [items %u] [%u]:",
352 GET_U_1(ap->rerr_flags) & RERR_NODELETE ? "[D]" : "",
353 GET_U_1(ap->rerr_dc), length);
354 dp6 = (const struct rerr_unreach6 *)(const void *)(ap + 1);
355 i = length - sizeof(*ap);
356 for (dc = GET_U_1(ap->rerr_dc); dc != 0; dc--) {
357 if (i < sizeof(*dp6)) {
358 ND_PRINT(" (remaining length %u)", i);
359 goto invalid;
360 }
361 ND_PRINT(" {%s}(%u)", GET_IP6ADDR_STRING(dp6->u_da),
362 GET_BE_U_4(dp6->u_ds));
363 dp6++;
364 i -= sizeof(*dp6);
365 }
366 return;
367
368 invalid:
369 nd_print_invalid(ndo);
370 }
371
372 void
373 aodv_print(netdissect_options *ndo,
374 const u_char *dat, u_int length, int is_ip6)
375 {
376 uint8_t msg_type;
377
378 ndo->ndo_protocol = "aodv";
379 ND_PRINT(" aodv");
380
381 /*
382 * The message type is the first byte; make sure we have it
383 * and then fetch it.
384 */
385 msg_type = GET_U_1(dat);
386 ND_PRINT(" %s", tok2str(msg_type_str, "type %u", msg_type));
387
388 switch (msg_type) {
389
390 case AODV_RREQ:
391 if (is_ip6)
392 aodv_v6_rreq(ndo, dat, length);
393 else
394 aodv_rreq(ndo, dat, length);
395 break;
396
397 case AODV_RREP:
398 if (is_ip6)
399 aodv_v6_rrep(ndo, dat, length);
400 else
401 aodv_rrep(ndo, dat, length);
402 break;
403
404 case AODV_RERR:
405 if (is_ip6)
406 aodv_v6_rerr(ndo, dat, length);
407 else
408 aodv_rerr(ndo, dat, length);
409 break;
410
411 case AODV_RREP_ACK:
412 ND_PRINT(" %u", length);
413 break;
414
415 default:
416 ND_PRINT(" %u", length);
417 }
418 }