]> The Tcpdump Group git mirrors - tcpdump/blob - print-aodv.c
CI: Add warning exemptions for Sun C (suncc-5.14) on Solaris 10
[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 #include <config.h>
37
38 #include "netdissect-stdinc.h"
39
40 #include "netdissect.h"
41 #include "addrtoname.h"
42 #include "extract.h"
43
44 struct aodv_rreq {
45 nd_uint8_t rreq_type; /* AODV message type (1) */
46 nd_uint8_t rreq_flags; /* various flags */
47 nd_uint8_t rreq_zero0; /* reserved, set to zero */
48 nd_uint8_t rreq_hops; /* number of hops from originator */
49 nd_uint32_t rreq_id; /* request ID */
50 nd_ipv4 rreq_da; /* destination IPv4 address */
51 nd_uint32_t rreq_ds; /* destination sequence number */
52 nd_ipv4 rreq_oa; /* originator IPv4 address */
53 nd_uint32_t rreq_os; /* originator sequence number */
54 };
55 struct aodv_rreq6 {
56 nd_uint8_t rreq_type; /* AODV message type (1) */
57 nd_uint8_t rreq_flags; /* various flags */
58 nd_uint8_t rreq_zero0; /* reserved, set to zero */
59 nd_uint8_t rreq_hops; /* number of hops from originator */
60 nd_uint32_t rreq_id; /* request ID */
61 nd_ipv6 rreq_da; /* destination IPv6 address */
62 nd_uint32_t rreq_ds; /* destination sequence number */
63 nd_ipv6 rreq_oa; /* originator IPv6 address */
64 nd_uint32_t rreq_os; /* originator sequence number */
65 };
66
67 #define RREQ_JOIN 0x80 /* join (reserved for multicast */
68 #define RREQ_REPAIR 0x40 /* repair (reserved for multicast */
69 #define RREQ_GRAT 0x20 /* gratuitous RREP */
70 #define RREQ_DEST 0x10 /* destination only */
71 #define RREQ_UNKNOWN 0x08 /* unknown destination sequence num */
72 #define RREQ_FLAGS_MASK 0xF8 /* mask for rreq_flags */
73
74 struct aodv_rrep {
75 nd_uint8_t rrep_type; /* AODV message type (2) */
76 nd_uint8_t rrep_flags; /* various flags */
77 nd_uint8_t rrep_ps; /* prefix size */
78 nd_uint8_t rrep_hops; /* number of hops from o to d */
79 nd_ipv4 rrep_da; /* destination IPv4 address */
80 nd_uint32_t rrep_ds; /* destination sequence number */
81 nd_ipv4 rrep_oa; /* originator IPv4 address */
82 nd_uint32_t rrep_life; /* lifetime of this route */
83 };
84 struct aodv_rrep6 {
85 nd_uint8_t rrep_type; /* AODV message type (2) */
86 nd_uint8_t rrep_flags; /* various flags */
87 nd_uint8_t rrep_ps; /* prefix size */
88 nd_uint8_t rrep_hops; /* number of hops from o to d */
89 nd_ipv6 rrep_da; /* destination IPv6 address */
90 nd_uint32_t rrep_ds; /* destination sequence number */
91 nd_ipv6 rrep_oa; /* originator IPv6 address */
92 nd_uint32_t rrep_life; /* lifetime of this route */
93 };
94
95 #define RREP_REPAIR 0x80 /* repair (reserved for multicast */
96 #define RREP_ACK 0x40 /* acknowledgement required */
97 #define RREP_FLAGS_MASK 0xC0 /* mask for rrep_flags */
98 #define RREP_PREFIX_MASK 0x1F /* mask for prefix size */
99
100 struct rerr_unreach {
101 nd_ipv4 u_da; /* IPv4 address */
102 nd_uint32_t u_ds; /* sequence number */
103 };
104 struct rerr_unreach6 {
105 nd_ipv6 u_da; /* IPv6 address */
106 nd_uint32_t u_ds; /* sequence number */
107 };
108
109 struct aodv_rerr {
110 nd_uint8_t rerr_type; /* AODV message type (3 or 18) */
111 nd_uint8_t rerr_flags; /* various flags */
112 nd_uint8_t rerr_zero0; /* reserved, set to zero */
113 nd_uint8_t rerr_dc; /* destination count */
114 };
115
116 #define RERR_NODELETE 0x80 /* don't delete the link */
117 #define RERR_FLAGS_MASK 0x80 /* mask for rerr_flags */
118
119 struct aodv_rrep_ack {
120 nd_uint8_t ra_type;
121 nd_uint8_t ra_zero0;
122 };
123
124 #define AODV_RREQ 1 /* route request */
125 #define AODV_RREP 2 /* route response */
126 #define AODV_RERR 3 /* error report */
127 #define AODV_RREP_ACK 4 /* route response acknowledgement */
128 static const struct tok msg_type_str[] = {
129 { AODV_RREQ, "rreq" },
130 { AODV_RREP, "rrep" },
131 { AODV_RERR, "rerr" },
132 { AODV_RREP_ACK, "rrep-ack" },
133 { 0, NULL }
134 };
135
136 struct aodv_ext {
137 nd_uint8_t type; /* extension type */
138 nd_uint8_t length; /* extension length */
139 };
140
141 struct aodv_hello {
142 struct aodv_ext eh; /* extension header */
143 nd_uint32_t interval; /* expect my next hello in
144 * (n) ms
145 * NOTE: this is not aligned */
146 };
147
148 #define AODV_EXT_HELLO 1
149
150 static void
151 aodv_extension(netdissect_options *ndo,
152 const struct aodv_ext *ep, u_int length)
153 {
154 const struct aodv_hello *ah;
155 uint8_t ext_type, ext_length;
156
157 ext_type = GET_U_1(ep->type);
158 ext_length = GET_U_1(ep->length);
159 switch (ext_type) {
160 case AODV_EXT_HELLO:
161 ah = (const struct aodv_hello *)(const void *)ep;
162 ND_ICHECKMSG_ZU("ext data length", length, <,
163 sizeof(struct aodv_hello));
164 if (ext_length < 4) {
165 ND_PRINT("\n\text HELLO - bad length %u", ext_length);
166 goto invalid;
167 }
168 ND_PRINT("\n\text HELLO %u ms",
169 GET_BE_U_4(ah->interval));
170 break;
171
172 default:
173 ND_PRINT("\n\text %u %u", ext_type, ext_length);
174 break;
175 }
176 return;
177
178 invalid:
179 nd_print_invalid(ndo);
180 }
181
182 static void
183 aodv_rreq(netdissect_options *ndo, const u_char *dat, u_int length)
184 {
185 u_int i;
186 const struct aodv_rreq *ap = (const struct aodv_rreq *)dat;
187
188 ND_ICHECKMSG_ZU("message length", length, <, sizeof(*ap));
189 ND_PRINT(" %u %s%s%s%s%shops %u id 0x%08x\n"
190 "\tdst %s seq %u src %s seq %u", length,
191 GET_U_1(ap->rreq_type) & RREQ_JOIN ? "[J]" : "",
192 GET_U_1(ap->rreq_type) & RREQ_REPAIR ? "[R]" : "",
193 GET_U_1(ap->rreq_type) & RREQ_GRAT ? "[G]" : "",
194 GET_U_1(ap->rreq_type) & RREQ_DEST ? "[D]" : "",
195 GET_U_1(ap->rreq_type) & RREQ_UNKNOWN ? "[U] " : " ",
196 GET_U_1(ap->rreq_hops),
197 GET_BE_U_4(ap->rreq_id),
198 GET_IPADDR_STRING(ap->rreq_da),
199 GET_BE_U_4(ap->rreq_ds),
200 GET_IPADDR_STRING(ap->rreq_oa),
201 GET_BE_U_4(ap->rreq_os));
202 i = length - sizeof(*ap);
203 if (i >= sizeof(struct aodv_ext))
204 aodv_extension(ndo, (const struct aodv_ext *)(dat + sizeof(*ap)), i);
205 return;
206
207 invalid:
208 nd_print_invalid(ndo);
209 }
210
211 static void
212 aodv_rrep(netdissect_options *ndo, const u_char *dat, u_int length)
213 {
214 u_int i;
215 const struct aodv_rrep *ap = (const struct aodv_rrep *)dat;
216
217 ND_ICHECKMSG_ZU("message length", length, <, sizeof(*ap));
218 ND_PRINT(" %u %s%sprefix %u hops %u\n"
219 "\tdst %s dseq %u src %s %u ms", length,
220 GET_U_1(ap->rrep_type) & RREP_REPAIR ? "[R]" : "",
221 GET_U_1(ap->rrep_type) & RREP_ACK ? "[A] " : " ",
222 GET_U_1(ap->rrep_ps) & RREP_PREFIX_MASK,
223 GET_U_1(ap->rrep_hops),
224 GET_IPADDR_STRING(ap->rrep_da),
225 GET_BE_U_4(ap->rrep_ds),
226 GET_IPADDR_STRING(ap->rrep_oa),
227 GET_BE_U_4(ap->rrep_life));
228 i = length - sizeof(*ap);
229 if (i >= sizeof(struct aodv_ext))
230 aodv_extension(ndo, (const struct aodv_ext *)(dat + sizeof(*ap)), i);
231 return;
232
233 invalid:
234 nd_print_invalid(ndo);
235 }
236
237 static void
238 aodv_rerr(netdissect_options *ndo, const u_char *dat, u_int length)
239 {
240 u_int i, dc;
241 const struct aodv_rerr *ap = (const struct aodv_rerr *)dat;
242 const struct rerr_unreach *dp;
243
244 ND_ICHECKMSG_ZU("message length", length, <, sizeof(*ap));
245 ND_PRINT(" %s [items %u] [%u]:",
246 GET_U_1(ap->rerr_flags) & RERR_NODELETE ? "[D]" : "",
247 GET_U_1(ap->rerr_dc), length);
248 dp = (const struct rerr_unreach *)(dat + sizeof(*ap));
249 i = length - sizeof(*ap);
250 for (dc = GET_U_1(ap->rerr_dc); dc != 0; dc--) {
251 ND_ICHECKMSG_ZU("remaining length", i, <, sizeof(*dp));
252 ND_PRINT(" {%s}(%u)", GET_IPADDR_STRING(dp->u_da),
253 GET_BE_U_4(dp->u_ds));
254 dp++;
255 i -= sizeof(*dp);
256 }
257 return;
258
259 invalid:
260 nd_print_invalid(ndo);
261 }
262
263 static void
264 aodv_v6_rreq(netdissect_options *ndo, const u_char *dat, u_int length)
265 {
266 u_int i;
267 const struct aodv_rreq6 *ap = (const struct aodv_rreq6 *)dat;
268
269 ND_ICHECKMSG_ZU("message length", length, <, sizeof(*ap));
270 ND_PRINT(" %u %s%s%s%s%shops %u id 0x%08x\n"
271 "\tdst %s seq %u src %s seq %u", length,
272 GET_U_1(ap->rreq_type) & RREQ_JOIN ? "[J]" : "",
273 GET_U_1(ap->rreq_type) & RREQ_REPAIR ? "[R]" : "",
274 GET_U_1(ap->rreq_type) & RREQ_GRAT ? "[G]" : "",
275 GET_U_1(ap->rreq_type) & RREQ_DEST ? "[D]" : "",
276 GET_U_1(ap->rreq_type) & RREQ_UNKNOWN ? "[U] " : " ",
277 GET_U_1(ap->rreq_hops),
278 GET_BE_U_4(ap->rreq_id),
279 GET_IP6ADDR_STRING(ap->rreq_da),
280 GET_BE_U_4(ap->rreq_ds),
281 GET_IP6ADDR_STRING(ap->rreq_oa),
282 GET_BE_U_4(ap->rreq_os));
283 i = length - sizeof(*ap);
284 if (i >= sizeof(struct aodv_ext))
285 aodv_extension(ndo, (const struct aodv_ext *)(dat + sizeof(*ap)), i);
286 return;
287
288 invalid:
289 nd_print_invalid(ndo);
290 }
291
292 static void
293 aodv_v6_rrep(netdissect_options *ndo, const u_char *dat, u_int length)
294 {
295 u_int i;
296 const struct aodv_rrep6 *ap = (const struct aodv_rrep6 *)dat;
297
298 ND_ICHECKMSG_ZU("message length", length, <, sizeof(*ap));
299 ND_PRINT(" %u %s%sprefix %u hops %u\n"
300 "\tdst %s dseq %u src %s %u ms", length,
301 GET_U_1(ap->rrep_type) & RREP_REPAIR ? "[R]" : "",
302 GET_U_1(ap->rrep_type) & RREP_ACK ? "[A] " : " ",
303 GET_U_1(ap->rrep_ps) & RREP_PREFIX_MASK,
304 GET_U_1(ap->rrep_hops),
305 GET_IP6ADDR_STRING(ap->rrep_da),
306 GET_BE_U_4(ap->rrep_ds),
307 GET_IP6ADDR_STRING(ap->rrep_oa),
308 GET_BE_U_4(ap->rrep_life));
309 i = length - sizeof(*ap);
310 if (i >= sizeof(struct aodv_ext))
311 aodv_extension(ndo, (const struct aodv_ext *)(dat + sizeof(*ap)), i);
312 return;
313
314 invalid:
315 nd_print_invalid(ndo);
316 }
317
318 static void
319 aodv_v6_rerr(netdissect_options *ndo, const u_char *dat, u_int length)
320 {
321 u_int i, dc;
322 const struct aodv_rerr *ap = (const struct aodv_rerr *)dat;
323 const struct rerr_unreach6 *dp6;
324
325 ND_ICHECKMSG_ZU("message length", length, <, sizeof(*ap));
326 ND_PRINT(" %s [items %u] [%u]:",
327 GET_U_1(ap->rerr_flags) & RERR_NODELETE ? "[D]" : "",
328 GET_U_1(ap->rerr_dc), length);
329 dp6 = (const struct rerr_unreach6 *)(const void *)(ap + 1);
330 i = length - sizeof(*ap);
331 for (dc = GET_U_1(ap->rerr_dc); dc != 0; dc--) {
332 ND_ICHECKMSG_ZU("remaining length", i, <, sizeof(*dp6));
333 ND_PRINT(" {%s}(%u)", GET_IP6ADDR_STRING(dp6->u_da),
334 GET_BE_U_4(dp6->u_ds));
335 dp6++;
336 i -= sizeof(*dp6);
337 }
338 return;
339
340 invalid:
341 nd_print_invalid(ndo);
342 }
343
344 void
345 aodv_print(netdissect_options *ndo,
346 const u_char *dat, u_int length, int is_ip6)
347 {
348 uint8_t msg_type;
349
350 ndo->ndo_protocol = "aodv";
351 ND_PRINT(" aodv");
352
353 /*
354 * The message type is the first byte; make sure we have it
355 * and then fetch it.
356 */
357 msg_type = GET_U_1(dat);
358 ND_PRINT(" %s", tok2str(msg_type_str, "type %u", msg_type));
359
360 switch (msg_type) {
361
362 case AODV_RREQ:
363 if (is_ip6)
364 aodv_v6_rreq(ndo, dat, length);
365 else
366 aodv_rreq(ndo, dat, length);
367 break;
368
369 case AODV_RREP:
370 if (is_ip6)
371 aodv_v6_rrep(ndo, dat, length);
372 else
373 aodv_rrep(ndo, dat, length);
374 break;
375
376 case AODV_RERR:
377 if (is_ip6)
378 aodv_v6_rerr(ndo, dat, length);
379 else
380 aodv_rerr(ndo, dat, length);
381 break;
382
383 case AODV_RREP_ACK:
384 ND_PRINT(" %u", length);
385 break;
386
387 default:
388 ND_PRINT(" %u", length);
389 }
390 }