]> The Tcpdump Group git mirrors - tcpdump/blob - print-babel.c
bd8b098e8b9c0db9fdc22684b54cd42a5659c444
[tcpdump] / print-babel.c
1 /*
2 * Copyright (c) 2007-2011 Grégoire Henry, Juliusz Chroboczek
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the project nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #define NETDISSECT_REWORKED
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <tcpdump-stdinc.h>
35
36 #include <stdio.h>
37 #include <string.h>
38
39 #include "interface.h"
40 #include "addrtoname.h"
41 #include "extract.h"
42
43 static const char tstr[] = "[|babel]";
44
45 static void babel_print_v2(netdissect_options *, const u_char *cp, u_int length);
46
47 void
48 babel_print(netdissect_options *ndo,
49 const u_char *cp, u_int length) {
50 ND_PRINT((ndo, "babel"));
51
52 ND_TCHECK2(*cp, 4);
53
54 if(cp[0] != 42) {
55 ND_PRINT((ndo, " malformed header"));
56 return;
57 } else {
58 ND_PRINT((ndo, " %d", cp[1]));
59 }
60
61 switch(cp[1]) {
62 case 2:
63 babel_print_v2(ndo, cp, length);
64 break;
65 default:
66 ND_PRINT((ndo, " unknown version"));
67 break;
68 }
69
70 return;
71
72 trunc:
73 ND_PRINT((ndo, " %s", tstr));
74 return;
75 }
76
77 /* TLVs */
78 #define MESSAGE_PAD1 0
79 #define MESSAGE_PADN 1
80 #define MESSAGE_ACK_REQ 2
81 #define MESSAGE_ACK 3
82 #define MESSAGE_HELLO 4
83 #define MESSAGE_IHU 5
84 #define MESSAGE_ROUTER_ID 6
85 #define MESSAGE_NH 7
86 #define MESSAGE_UPDATE 8
87 #define MESSAGE_REQUEST 9
88 #define MESSAGE_MH_REQUEST 10
89 #define MESSAGE_TSPC 11
90 #define MESSAGE_HMAC 12
91
92 /* sub-TLVs */
93 #define MESSAGE_SUB_PAD1 0
94 #define MESSAGE_SUB_PADN 1
95 #define MESSAGE_SUB_DIVERSITY 2
96 #define MESSAGE_SUB_TIMESTAMP 3
97
98 /* Diversity sub-TLV channel codes */
99 static const struct tok diversity_str[] = {
100 { 0, "reserved" },
101 { 255, "all" },
102 { 0, NULL }
103 };
104
105 static const char *
106 format_id(const u_char *id)
107 {
108 static char buf[25];
109 snprintf(buf, 25, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
110 id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7]);
111 buf[24] = '\0';
112 return buf;
113 }
114
115 static const unsigned char v4prefix[16] =
116 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
117
118 static const char *
119 format_prefix(netdissect_options *ndo, const u_char *prefix, unsigned char plen)
120 {
121 static char buf[50];
122 if(plen >= 96 && memcmp(prefix, v4prefix, 12) == 0)
123 snprintf(buf, 50, "%s/%u", ipaddr_string(ndo, prefix + 12), plen - 96);
124 else
125 #ifdef INET6
126 snprintf(buf, 50, "%s/%u", ip6addr_string(ndo, prefix), plen);
127 #else
128 snprintf(buf, 50, "IPv6 addresses not supported");
129 #endif
130 buf[49] = '\0';
131 return buf;
132 }
133
134 static const char *
135 format_address(netdissect_options *ndo, const u_char *prefix)
136 {
137 if(memcmp(prefix, v4prefix, 12) == 0)
138 return ipaddr_string(ndo, prefix + 12);
139 else
140 #ifdef INET6
141 return ip6addr_string(ndo, prefix);
142 #else
143 return "IPv6 addresses not supported";
144 #endif
145 }
146
147 static const char *
148 format_interval(const uint16_t i)
149 {
150 static char buf[sizeof("000.00s")];
151
152 if (i == 0)
153 return "0.0s (bogus)";
154 snprintf(buf, sizeof(buf), "%u.%02us", i / 100, i % 100);
155 return buf;
156 }
157
158 static const char *
159 format_interval_update(const uint16_t i)
160 {
161 return i == 0xFFFF ? "infinity" : format_interval(i);
162 }
163
164 static const char *
165 format_timestamp(const uint32_t i)
166 {
167 /* Rotating buffer. */
168 static char buf[sizeof("0000.000000s")];
169 snprintf(buf, sizeof(buf), "%u.%06us", i / 1000000, i % 1000000);
170 return buf;
171 }
172
173 /* Return number of octets consumed from the input buffer (not the prefix length
174 * in bytes), or -1 for encoding error. */
175 static int
176 network_prefix(int ae, int plen, unsigned int omitted,
177 const unsigned char *p, const unsigned char *dp,
178 unsigned int len, unsigned char *p_r)
179 {
180 unsigned pb;
181 unsigned char prefix[16];
182 int consumed = 0;
183
184 if(plen >= 0)
185 pb = (plen + 7) / 8;
186 else if(ae == 1)
187 pb = 4;
188 else
189 pb = 16;
190
191 if(pb > 16)
192 return -1;
193
194 memset(prefix, 0, 16);
195
196 switch(ae) {
197 case 0: break;
198 case 1:
199 if(omitted > 4 || pb > 4 || (pb > omitted && len < pb - omitted))
200 return -1;
201 memcpy(prefix, v4prefix, 12);
202 if(omitted) {
203 if (dp == NULL) return -1;
204 memcpy(prefix, dp, 12 + omitted);
205 }
206 if(pb > omitted) {
207 memcpy(prefix + 12 + omitted, p, pb - omitted);
208 consumed = pb - omitted;
209 }
210 break;
211 case 2:
212 if(omitted > 16 || (pb > omitted && len < pb - omitted))
213 return -1;
214 if(omitted) {
215 if (dp == NULL) return -1;
216 memcpy(prefix, dp, omitted);
217 }
218 if(pb > omitted) {
219 memcpy(prefix + omitted, p, pb - omitted);
220 consumed = pb - omitted;
221 }
222 break;
223 case 3:
224 if(pb > 8 && len < pb - 8) return -1;
225 prefix[0] = 0xfe;
226 prefix[1] = 0x80;
227 if(pb > 8) {
228 memcpy(prefix + 8, p, pb - 8);
229 consumed = pb - 8;
230 }
231 break;
232 default:
233 return -1;
234 }
235
236 memcpy(p_r, prefix, 16);
237 return consumed;
238 }
239
240 static int
241 network_address(int ae, const unsigned char *a, unsigned int len,
242 unsigned char *a_r)
243 {
244 return network_prefix(ae, -1, 0, a, NULL, len, a_r);
245 }
246
247 /*
248 * Sub-TLVs consume the "extra data" of Babel TLVs (see Section 4.3 of RFC6126),
249 * their encoding is similar to the encoding of TLVs, but the type namespace is
250 * different:
251 *
252 * o Type 0 stands for Pad1 sub-TLV with the same encoding as the Pad1 TLV.
253 * o Type 1 stands for PadN sub-TLV with the same encoding as the PadN TLV.
254 * o Type 2 stands for Diversity sub-TLV, which propagates diversity routing
255 * data. Its body is a variable-length sequence of 8-bit unsigned integers,
256 * each representing per-hop number of interferring radio channel for the
257 * prefix. Channel 0 is invalid and must not be used in the sub-TLV, channel
258 * 255 interferes with any other channel.
259 * o Type 3 stands for Timestamp sub-TLV, used to compute RTT between
260 * neighbours. In the case of a Hello TLV, the body stores a 32-bits
261 * timestamp, while in the case of a IHU TLV, two 32-bits timestamps are
262 * stored.
263 *
264 * Sub-TLV types 0 and 1 are valid for any TLV type, whether sub-TLV type 2 is
265 * only valid for TLV type 8 (Update). Note that within an Update TLV a missing
266 * Diversity sub-TLV is not the same as a Diversity sub-TLV with an empty body.
267 * The former would mean a lack of any claims about the interference, and the
268 * latter would state that interference is definitely absent.
269 * A type 3 sub-TLV is valid both for Hello and IHU TLVs, though the exact
270 * semantic of the sub-TLV is different in each case.
271 */
272 static void
273 subtlvs_print(netdissect_options *ndo,
274 const u_char *cp, const u_char *ep, const uint8_t tlv_type) {
275 uint8_t subtype, sublen;
276 const char *sep;
277 uint32_t t1, t2;
278
279 while (cp < ep) {
280 subtype = *cp++;
281 if(subtype == MESSAGE_SUB_PAD1) {
282 ND_PRINT((ndo, " sub-pad1"));
283 continue;
284 }
285 if(cp == ep)
286 goto corrupt;
287 sublen = *cp++;
288 if(cp + sublen > ep)
289 goto corrupt;
290
291 switch(subtype) {
292 case MESSAGE_SUB_PADN:
293 ND_PRINT((ndo, " sub-padn"));
294 cp += sublen;
295 break;
296 case MESSAGE_SUB_DIVERSITY:
297 ND_PRINT((ndo, " sub-diversity"));
298 if (sublen == 0) {
299 ND_PRINT((ndo, " empty"));
300 break;
301 }
302 sep = " ";
303 while(sublen--) {
304 ND_PRINT((ndo, "%s%s", sep, tok2str(diversity_str, "%u", *cp++)));
305 sep = "-";
306 }
307 if(tlv_type != MESSAGE_UPDATE)
308 ND_PRINT((ndo, " (bogus)"));
309 break;
310 case MESSAGE_SUB_TIMESTAMP:
311 ND_PRINT((ndo, " sub-timestamp"));
312 if(tlv_type == MESSAGE_HELLO) {
313 if(sublen != 4)
314 goto corrupt;
315 t1 = EXTRACT_32BITS(cp);
316 ND_PRINT((ndo, " %s", format_timestamp(t1)));
317 } else if(tlv_type == MESSAGE_IHU) {
318 if(sublen != 8)
319 goto corrupt;
320 t1 = EXTRACT_32BITS(cp);
321 ND_PRINT((ndo, " %s", format_timestamp(t1)));
322 t2 = EXTRACT_32BITS(cp + 4);
323 ND_PRINT((ndo, "|%s", format_timestamp(t2)));
324 } else
325 ND_PRINT((ndo, " (bogus)"));
326 cp += sublen;
327 break;
328 default:
329 ND_PRINT((ndo, " sub-unknown-0x%02x", subtype));
330 cp += sublen;
331 } /* switch */
332 } /* while */
333 return;
334
335 corrupt:
336 ND_PRINT((ndo, " (corrupt)"));
337 }
338
339 #define ICHECK(i, l) \
340 if ((i) + (l) > bodylen || (i) + (l) > length) goto corrupt;
341
342 static void
343 babel_print_v2(netdissect_options *ndo,
344 const u_char *cp, u_int length) {
345 u_int i;
346 u_short bodylen;
347 u_char v4_prefix[16] =
348 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
349 u_char v6_prefix[16] = {0};
350
351 ND_TCHECK2(*cp, 4);
352 if (length < 4)
353 goto corrupt;
354 bodylen = EXTRACT_16BITS(cp + 2);
355 ND_PRINT((ndo, " (%u)", bodylen));
356
357 /* Process the TLVs in the body */
358 i = 0;
359 while(i < bodylen) {
360 const u_char *message;
361 u_int type, len;
362
363 message = cp + 4 + i;
364
365 ND_TCHECK2(*message, 1);
366 if((type = message[0]) == MESSAGE_PAD1) {
367 ND_PRINT((ndo, ndo->ndo_vflag ? "\n\tPad 1" : " pad1"));
368 i += 1;
369 continue;
370 }
371
372 ND_TCHECK2(*message, 2);
373 ICHECK(i, 2);
374 len = message[1];
375
376 ND_TCHECK2(*message, 2 + len);
377 ICHECK(i, 2 + len);
378
379 switch(type) {
380 case MESSAGE_PADN: {
381 if (!ndo->ndo_vflag)
382 ND_PRINT((ndo, " padN"));
383 else
384 ND_PRINT((ndo, "\n\tPad %d", len + 2));
385 }
386 break;
387
388 case MESSAGE_ACK_REQ: {
389 u_short nonce, interval;
390 if (!ndo->ndo_vflag)
391 ND_PRINT((ndo, " ack-req"));
392 else {
393 ND_PRINT((ndo, "\n\tAcknowledgment Request "));
394 if(len < 6) goto corrupt;
395 nonce = EXTRACT_16BITS(message + 4);
396 interval = EXTRACT_16BITS(message + 6);
397 ND_PRINT((ndo, "%04x %s", nonce, format_interval(interval)));
398 }
399 }
400 break;
401
402 case MESSAGE_ACK: {
403 u_short nonce;
404 if (!ndo->ndo_vflag)
405 ND_PRINT((ndo, " ack"));
406 else {
407 ND_PRINT((ndo, "\n\tAcknowledgment "));
408 if(len < 2) goto corrupt;
409 nonce = EXTRACT_16BITS(message + 2);
410 ND_PRINT((ndo, "%04x", nonce));
411 }
412 }
413 break;
414
415 case MESSAGE_HELLO: {
416 u_short seqno, interval;
417 if (!ndo->ndo_vflag)
418 ND_PRINT((ndo, " hello"));
419 else {
420 ND_PRINT((ndo, "\n\tHello "));
421 if(len < 6) goto corrupt;
422 seqno = EXTRACT_16BITS(message + 4);
423 interval = EXTRACT_16BITS(message + 6);
424 ND_PRINT((ndo, "seqno %u interval %s", seqno, format_interval(interval)));
425 /* Extra data. */
426 if(len > 6)
427 subtlvs_print(ndo, message + 8, message + 2 + len, type);
428 }
429 }
430 break;
431
432 case MESSAGE_IHU: {
433 unsigned short txcost, interval;
434 if (!ndo->ndo_vflag)
435 ND_PRINT((ndo, " ihu"));
436 else {
437 u_char address[16];
438 int rc;
439 ND_PRINT((ndo, "\n\tIHU "));
440 if(len < 6) goto corrupt;
441 txcost = EXTRACT_16BITS(message + 4);
442 interval = EXTRACT_16BITS(message + 6);
443 rc = network_address(message[2], message + 8, len - 6, address);
444 if(rc < 0) { ND_PRINT((ndo, "%s", tstr)); break; }
445 ND_PRINT((ndo, "%s txcost %u interval %s",
446 format_address(ndo, address), txcost, format_interval(interval)));
447 /* Extra data. */
448 if((u_int)rc < len - 6)
449 subtlvs_print(ndo, message + 8 + rc, message + 2 + len,
450 type);
451 }
452 }
453 break;
454
455 case MESSAGE_ROUTER_ID: {
456 if (!ndo->ndo_vflag)
457 ND_PRINT((ndo, " router-id"));
458 else {
459 ND_PRINT((ndo, "\n\tRouter Id"));
460 if(len < 10) goto corrupt;
461 ND_PRINT((ndo, " %s", format_id(message + 4)));
462 }
463 }
464 break;
465
466 case MESSAGE_NH: {
467 if (!ndo->ndo_vflag)
468 ND_PRINT((ndo, " nh"));
469 else {
470 int rc;
471 u_char nh[16];
472 ND_PRINT((ndo, "\n\tNext Hop"));
473 if(len < 2) goto corrupt;
474 rc = network_address(message[2], message + 4, len - 2, nh);
475 if(rc < 0) goto corrupt;
476 ND_PRINT((ndo, " %s", format_address(ndo, nh)));
477 }
478 }
479 break;
480
481 case MESSAGE_UPDATE: {
482 if (!ndo->ndo_vflag) {
483 ND_PRINT((ndo, " update"));
484 if(len < 1)
485 ND_PRINT((ndo, "/truncated"));
486 else
487 ND_PRINT((ndo, "%s%s%s",
488 (message[3] & 0x80) ? "/prefix": "",
489 (message[3] & 0x40) ? "/id" : "",
490 (message[3] & 0x3f) ? "/unknown" : ""));
491 } else {
492 u_short interval, seqno, metric;
493 u_char plen;
494 int rc;
495 u_char prefix[16];
496 ND_PRINT((ndo, "\n\tUpdate"));
497 if(len < 10) goto corrupt;
498 plen = message[4] + (message[2] == 1 ? 96 : 0);
499 rc = network_prefix(message[2], message[4], message[5],
500 message + 12,
501 message[2] == 1 ? v4_prefix : v6_prefix,
502 len - 10, prefix);
503 if(rc < 0) goto corrupt;
504 interval = EXTRACT_16BITS(message + 6);
505 seqno = EXTRACT_16BITS(message + 8);
506 metric = EXTRACT_16BITS(message + 10);
507 ND_PRINT((ndo, "%s%s%s %s metric %u seqno %u interval %s",
508 (message[3] & 0x80) ? "/prefix": "",
509 (message[3] & 0x40) ? "/id" : "",
510 (message[3] & 0x3f) ? "/unknown" : "",
511 format_prefix(ndo, prefix, plen),
512 metric, seqno, format_interval_update(interval)));
513 if(message[3] & 0x80) {
514 if(message[2] == 1)
515 memcpy(v4_prefix, prefix, 16);
516 else
517 memcpy(v6_prefix, prefix, 16);
518 }
519 /* extra data? */
520 if((u_int)rc < len - 10)
521 subtlvs_print(ndo, message + 12 + rc, message + 2 + len, type);
522 }
523 }
524 break;
525
526 case MESSAGE_REQUEST: {
527 if (!ndo->ndo_vflag)
528 ND_PRINT((ndo, " request"));
529 else {
530 int rc;
531 u_char prefix[16], plen;
532 ND_PRINT((ndo, "\n\tRequest "));
533 if(len < 2) goto corrupt;
534 plen = message[3] + (message[2] == 1 ? 96 : 0);
535 rc = network_prefix(message[2], message[3], 0,
536 message + 4, NULL, len - 2, prefix);
537 if(rc < 0) goto corrupt;
538 ND_PRINT((ndo, "for %s",
539 message[2] == 0 ? "any" : format_prefix(ndo, prefix, plen)));
540 }
541 }
542 break;
543
544 case MESSAGE_MH_REQUEST : {
545 if (!ndo->ndo_vflag)
546 ND_PRINT((ndo, " mh-request"));
547 else {
548 int rc;
549 u_short seqno;
550 u_char prefix[16], plen;
551 ND_PRINT((ndo, "\n\tMH-Request "));
552 if(len < 14) goto corrupt;
553 seqno = EXTRACT_16BITS(message + 4);
554 rc = network_prefix(message[2], message[3], 0,
555 message + 16, NULL, len - 14, prefix);
556 if(rc < 0) goto corrupt;
557 plen = message[3] + (message[2] == 1 ? 96 : 0);
558 ND_PRINT((ndo, "(%u hops) for %s seqno %u id %s",
559 message[6], format_prefix(ndo, prefix, plen),
560 seqno, format_id(message + 8)));
561 }
562 }
563 break;
564 case MESSAGE_TSPC :
565 if (!ndo->ndo_vflag)
566 ND_PRINT((ndo, " tspc"));
567 else {
568 ND_PRINT((ndo, "\n\tTS/PC "));
569 if(len < 6) goto corrupt;
570 ND_PRINT((ndo, "timestamp %u packetcounter %u", EXTRACT_32BITS (message + 4),
571 EXTRACT_16BITS(message + 2)));
572 }
573 break;
574 case MESSAGE_HMAC : {
575 if (!ndo->ndo_vflag)
576 ND_PRINT((ndo, " hmac"));
577 else {
578 unsigned j;
579 ND_PRINT((ndo, "\n\tHMAC "));
580 if(len < 18) goto corrupt;
581 ND_PRINT((ndo, "key-id %u digest-%u ", EXTRACT_16BITS(message + 2), len - 2));
582 for (j = 0; j < len - 2; j++)
583 ND_PRINT((ndo, "%02X", message[4 + j]));
584 }
585 }
586 break;
587 default:
588 if (!ndo->ndo_vflag)
589 ND_PRINT((ndo, " unknown"));
590 else
591 ND_PRINT((ndo, "\n\tUnknown message type %d", type));
592 }
593 i += len + 2;
594 }
595 return;
596
597 trunc:
598 ND_PRINT((ndo, " %s", tstr));
599 return;
600
601 corrupt:
602 ND_PRINT((ndo, " (corrupt)"));
603 return;
604 }