2 * Copyright (c) 2007-2011 Grégoire Henry, Juliusz Chroboczek
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
33 #include <netdissect-stdinc.h>
38 #include "netdissect.h"
39 #include "addrtoname.h"
42 static const char tstr
[] = "[|babel]";
44 static void babel_print_v2(netdissect_options
*, const u_char
*cp
, u_int length
);
47 babel_print(netdissect_options
*ndo
,
48 const u_char
*cp
, u_int length
)
50 ND_PRINT((ndo
, "babel"));
55 ND_PRINT((ndo
, " invalid header"));
58 ND_PRINT((ndo
, " %d", cp
[1]));
63 babel_print_v2(ndo
, cp
, length
);
66 ND_PRINT((ndo
, " unknown version"));
73 ND_PRINT((ndo
, " %s", tstr
));
78 #define MESSAGE_PAD1 0
79 #define MESSAGE_PADN 1
80 #define MESSAGE_ACK_REQ 2
82 #define MESSAGE_HELLO 4
84 #define MESSAGE_ROUTER_ID 6
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 #define MESSAGE_UPDATE_SRC_SPECIFIC 13
92 #define MESSAGE_REQUEST_SRC_SPECIFIC 14
93 #define MESSAGE_MH_REQUEST_SRC_SPECIFIC 15
96 #define MESSAGE_SUB_PAD1 0
97 #define MESSAGE_SUB_PADN 1
98 #define MESSAGE_SUB_DIVERSITY 2
99 #define MESSAGE_SUB_TIMESTAMP 3
101 /* Diversity sub-TLV channel codes */
102 static const struct tok diversity_str
[] = {
109 format_id(const u_char
*id
)
112 snprintf(buf
, 25, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
113 id
[0], id
[1], id
[2], id
[3], id
[4], id
[5], id
[6], id
[7]);
118 static const unsigned char v4prefix
[16] =
119 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
122 format_prefix(netdissect_options
*ndo
, const u_char
*prefix
, unsigned char plen
)
125 if(plen
>= 96 && memcmp(prefix
, v4prefix
, 12) == 0)
126 snprintf(buf
, 50, "%s/%u", ipaddr_string(ndo
, prefix
+ 12), plen
- 96);
128 snprintf(buf
, 50, "%s/%u", ip6addr_string(ndo
, prefix
), plen
);
134 format_address(netdissect_options
*ndo
, const u_char
*prefix
)
136 if(memcmp(prefix
, v4prefix
, 12) == 0)
137 return ipaddr_string(ndo
, prefix
+ 12);
139 return ip6addr_string(ndo
, prefix
);
143 format_interval(const uint16_t i
)
145 static char buf
[sizeof("000.00s")];
148 return "0.0s (bogus)";
149 snprintf(buf
, sizeof(buf
), "%u.%02us", i
/ 100, i
% 100);
154 format_interval_update(const uint16_t i
)
156 return i
== 0xFFFF ? "infinity" : format_interval(i
);
160 format_timestamp(const uint32_t i
)
162 static char buf
[sizeof("0000.000000s")];
163 snprintf(buf
, sizeof(buf
), "%u.%06us", i
/ 1000000, i
% 1000000);
167 /* Return number of octets consumed from the input buffer (not the prefix length
168 * in bytes), or -1 for encoding error. */
170 network_prefix(int ae
, int plen
, unsigned int omitted
,
171 const unsigned char *p
, const unsigned char *dp
,
172 unsigned int len
, unsigned char *p_r
)
175 unsigned char prefix
[16];
188 memset(prefix
, 0, 16);
193 if(omitted
> 4 || pb
> 4 || (pb
> omitted
&& len
< pb
- omitted
))
195 memcpy(prefix
, v4prefix
, 12);
197 if (dp
== NULL
) return -1;
198 memcpy(prefix
, dp
, 12 + omitted
);
201 memcpy(prefix
+ 12 + omitted
, p
, pb
- omitted
);
202 consumed
= pb
- omitted
;
206 if(omitted
> 16 || (pb
> omitted
&& len
< pb
- omitted
))
209 if (dp
== NULL
) return -1;
210 memcpy(prefix
, dp
, omitted
);
213 memcpy(prefix
+ omitted
, p
, pb
- omitted
);
214 consumed
= pb
- omitted
;
218 if(pb
> 8 && len
< pb
- 8) return -1;
222 memcpy(prefix
+ 8, p
, pb
- 8);
230 memcpy(p_r
, prefix
, 16);
235 network_address(int ae
, const unsigned char *a
, unsigned int len
,
238 return network_prefix(ae
, -1, 0, a
, NULL
, len
, a_r
);
242 * Sub-TLVs consume the "extra data" of Babel TLVs (see Section 4.3 of RFC6126),
243 * their encoding is similar to the encoding of TLVs, but the type namespace is
246 * o Type 0 stands for Pad1 sub-TLV with the same encoding as the Pad1 TLV.
247 * o Type 1 stands for PadN sub-TLV with the same encoding as the PadN TLV.
248 * o Type 2 stands for Diversity sub-TLV, which propagates diversity routing
249 * data. Its body is a variable-length sequence of 8-bit unsigned integers,
250 * each representing per-hop number of interferring radio channel for the
251 * prefix. Channel 0 is invalid and must not be used in the sub-TLV, channel
252 * 255 interferes with any other channel.
253 * o Type 3 stands for Timestamp sub-TLV, used to compute RTT between
254 * neighbours. In the case of a Hello TLV, the body stores a 32-bits
255 * timestamp, while in the case of a IHU TLV, two 32-bits timestamps are
258 * Sub-TLV types 0 and 1 are valid for any TLV type, whether sub-TLV type 2 is
259 * only valid for TLV type 8 (Update). Note that within an Update TLV a missing
260 * Diversity sub-TLV is not the same as a Diversity sub-TLV with an empty body.
261 * The former would mean a lack of any claims about the interference, and the
262 * latter would state that interference is definitely absent.
263 * A type 3 sub-TLV is valid both for Hello and IHU TLVs, though the exact
264 * semantic of the sub-TLV is different in each case.
267 subtlvs_print(netdissect_options
*ndo
,
268 const u_char
*cp
, const u_char
*ep
, const uint8_t tlv_type
)
270 uint8_t subtype
, sublen
;
276 if(subtype
== MESSAGE_SUB_PAD1
) {
277 ND_PRINT((ndo
, " sub-pad1"));
287 case MESSAGE_SUB_PADN
:
288 ND_PRINT((ndo
, " sub-padn"));
291 case MESSAGE_SUB_DIVERSITY
:
292 ND_PRINT((ndo
, " sub-diversity"));
294 ND_PRINT((ndo
, " empty"));
299 ND_PRINT((ndo
, "%s%s", sep
, tok2str(diversity_str
, "%u", *cp
++)));
302 if(tlv_type
!= MESSAGE_UPDATE
&&
303 tlv_type
!= MESSAGE_UPDATE_SRC_SPECIFIC
)
304 ND_PRINT((ndo
, " (bogus)"));
306 case MESSAGE_SUB_TIMESTAMP
:
307 ND_PRINT((ndo
, " sub-timestamp"));
308 if(tlv_type
== MESSAGE_HELLO
) {
311 t1
= EXTRACT_32BITS(cp
);
312 ND_PRINT((ndo
, " %s", format_timestamp(t1
)));
313 } else if(tlv_type
== MESSAGE_IHU
) {
316 t1
= EXTRACT_32BITS(cp
);
317 ND_PRINT((ndo
, " %s", format_timestamp(t1
)));
318 t2
= EXTRACT_32BITS(cp
+ 4);
319 ND_PRINT((ndo
, "|%s", format_timestamp(t2
)));
321 ND_PRINT((ndo
, " (bogus)"));
325 ND_PRINT((ndo
, " sub-unknown-0x%02x", subtype
));
332 ND_PRINT((ndo
, "%s", istr
));
335 #define ICHECK(i, l) \
336 if ((i) + (l) > bodylen || (i) + (l) > length) goto invalid;
339 babel_print_v2(netdissect_options
*ndo
,
340 const u_char
*cp
, u_int length
)
344 u_char v4_prefix
[16] =
345 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
346 u_char v6_prefix
[16] = {0};
351 bodylen
= EXTRACT_16BITS(cp
+ 2);
352 ND_PRINT((ndo
, " (%u)", bodylen
));
354 /* Process the TLVs in the body */
357 const u_char
*message
;
360 message
= cp
+ 4 + i
;
362 ND_TCHECK2(*message
, 1);
363 if((type
= message
[0]) == MESSAGE_PAD1
) {
364 ND_PRINT((ndo
, ndo
->ndo_vflag
? "\n\tPad 1" : " pad1"));
369 ND_TCHECK2(*message
, 2);
373 ND_TCHECK2(*message
, 2 + len
);
379 ND_PRINT((ndo
, " padN"));
381 ND_PRINT((ndo
, "\n\tPad %d", len
+ 2));
385 case MESSAGE_ACK_REQ
: {
386 u_short nonce
, interval
;
388 ND_PRINT((ndo
, " ack-req"));
390 ND_PRINT((ndo
, "\n\tAcknowledgment Request "));
391 if(len
< 6) goto invalid
;
392 nonce
= EXTRACT_16BITS(message
+ 4);
393 interval
= EXTRACT_16BITS(message
+ 6);
394 ND_PRINT((ndo
, "%04x %s", nonce
, format_interval(interval
)));
402 ND_PRINT((ndo
, " ack"));
404 ND_PRINT((ndo
, "\n\tAcknowledgment "));
405 if(len
< 2) goto invalid
;
406 nonce
= EXTRACT_16BITS(message
+ 2);
407 ND_PRINT((ndo
, "%04x", nonce
));
412 case MESSAGE_HELLO
: {
413 u_short seqno
, interval
;
415 ND_PRINT((ndo
, " hello"));
417 ND_PRINT((ndo
, "\n\tHello "));
418 if(len
< 6) goto invalid
;
419 seqno
= EXTRACT_16BITS(message
+ 4);
420 interval
= EXTRACT_16BITS(message
+ 6);
421 ND_PRINT((ndo
, "seqno %u interval %s", seqno
, format_interval(interval
)));
424 subtlvs_print(ndo
, message
+ 8, message
+ 2 + len
, type
);
430 unsigned short txcost
, interval
;
432 ND_PRINT((ndo
, " ihu"));
436 ND_PRINT((ndo
, "\n\tIHU "));
437 if(len
< 6) goto invalid
;
438 txcost
= EXTRACT_16BITS(message
+ 4);
439 interval
= EXTRACT_16BITS(message
+ 6);
440 rc
= network_address(message
[2], message
+ 8, len
- 6, address
);
441 if(rc
< 0) { ND_PRINT((ndo
, "%s", tstr
)); break; }
442 ND_PRINT((ndo
, "%s txcost %u interval %s",
443 format_address(ndo
, address
), txcost
, format_interval(interval
)));
445 if((u_int
)rc
< len
- 6)
446 subtlvs_print(ndo
, message
+ 8 + rc
, message
+ 2 + len
,
452 case MESSAGE_ROUTER_ID
: {
454 ND_PRINT((ndo
, " router-id"));
456 ND_PRINT((ndo
, "\n\tRouter Id"));
457 if(len
< 10) goto invalid
;
458 ND_PRINT((ndo
, " %s", format_id(message
+ 4)));
465 ND_PRINT((ndo
, " nh"));
469 ND_PRINT((ndo
, "\n\tNext Hop"));
470 if(len
< 2) goto invalid
;
471 rc
= network_address(message
[2], message
+ 4, len
- 2, nh
);
472 if(rc
< 0) goto invalid
;
473 ND_PRINT((ndo
, " %s", format_address(ndo
, nh
)));
478 case MESSAGE_UPDATE
: {
479 if (!ndo
->ndo_vflag
) {
480 ND_PRINT((ndo
, " update"));
482 ND_PRINT((ndo
, "/truncated"));
484 ND_PRINT((ndo
, "%s%s%s",
485 (message
[3] & 0x80) ? "/prefix": "",
486 (message
[3] & 0x40) ? "/id" : "",
487 (message
[3] & 0x3f) ? "/unknown" : ""));
489 u_short interval
, seqno
, metric
;
493 ND_PRINT((ndo
, "\n\tUpdate"));
494 if(len
< 10) goto invalid
;
495 plen
= message
[4] + (message
[2] == 1 ? 96 : 0);
496 rc
= network_prefix(message
[2], message
[4], message
[5],
498 message
[2] == 1 ? v4_prefix
: v6_prefix
,
500 if(rc
< 0) goto invalid
;
501 interval
= EXTRACT_16BITS(message
+ 6);
502 seqno
= EXTRACT_16BITS(message
+ 8);
503 metric
= EXTRACT_16BITS(message
+ 10);
504 ND_PRINT((ndo
, "%s%s%s %s metric %u seqno %u interval %s",
505 (message
[3] & 0x80) ? "/prefix": "",
506 (message
[3] & 0x40) ? "/id" : "",
507 (message
[3] & 0x3f) ? "/unknown" : "",
508 format_prefix(ndo
, prefix
, plen
),
509 metric
, seqno
, format_interval_update(interval
)));
510 if(message
[3] & 0x80) {
512 memcpy(v4_prefix
, prefix
, 16);
514 memcpy(v6_prefix
, prefix
, 16);
517 if((u_int
)rc
< len
- 10)
518 subtlvs_print(ndo
, message
+ 12 + rc
, message
+ 2 + len
, type
);
523 case MESSAGE_REQUEST
: {
525 ND_PRINT((ndo
, " request"));
528 u_char prefix
[16], plen
;
529 ND_PRINT((ndo
, "\n\tRequest "));
530 if(len
< 2) goto invalid
;
531 plen
= message
[3] + (message
[2] == 1 ? 96 : 0);
532 rc
= network_prefix(message
[2], message
[3], 0,
533 message
+ 4, NULL
, len
- 2, prefix
);
534 if(rc
< 0) goto invalid
;
535 ND_PRINT((ndo
, "for %s",
536 message
[2] == 0 ? "any" : format_prefix(ndo
, prefix
, plen
)));
541 case MESSAGE_MH_REQUEST
: {
543 ND_PRINT((ndo
, " mh-request"));
547 u_char prefix
[16], plen
;
548 ND_PRINT((ndo
, "\n\tMH-Request "));
549 if(len
< 14) goto invalid
;
550 seqno
= EXTRACT_16BITS(message
+ 4);
551 rc
= network_prefix(message
[2], message
[3], 0,
552 message
+ 16, NULL
, len
- 14, prefix
);
553 if(rc
< 0) goto invalid
;
554 plen
= message
[3] + (message
[2] == 1 ? 96 : 0);
555 ND_PRINT((ndo
, "(%u hops) for %s seqno %u id %s",
556 message
[6], format_prefix(ndo
, prefix
, plen
),
557 seqno
, format_id(message
+ 8)));
563 ND_PRINT((ndo
, " tspc"));
565 ND_PRINT((ndo
, "\n\tTS/PC "));
566 if(len
< 6) goto invalid
;
567 ND_PRINT((ndo
, "timestamp %u packetcounter %u", EXTRACT_32BITS (message
+ 4),
568 EXTRACT_16BITS(message
+ 2)));
571 case MESSAGE_HMAC
: {
573 ND_PRINT((ndo
, " hmac"));
576 ND_PRINT((ndo
, "\n\tHMAC "));
577 if(len
< 18) goto invalid
;
578 ND_PRINT((ndo
, "key-id %u digest-%u ", EXTRACT_16BITS(message
+ 2), len
- 2));
579 for (j
= 0; j
< len
- 2; j
++)
580 ND_PRINT((ndo
, "%02X", message
[4 + j
]));
585 case MESSAGE_UPDATE_SRC_SPECIFIC
: {
586 if(!ndo
->ndo_vflag
) {
587 ND_PRINT((ndo
, " ss-update"));
589 u_char prefix
[16], src_prefix
[16];
590 u_short interval
, seqno
, metric
;
591 u_char ae
, plen
, src_plen
, omitted
;
594 ND_PRINT((ndo
, "\n\tSS-Update"));
595 if(len
< 10) goto invalid
;
597 src_plen
= message
[3];
599 omitted
= message
[5];
600 interval
= EXTRACT_16BITS(message
+ 6);
601 seqno
= EXTRACT_16BITS(message
+ 8);
602 metric
= EXTRACT_16BITS(message
+ 10);
603 rc
= network_prefix(ae
, plen
, omitted
, message
+ 2 + parsed_len
,
604 ae
== 1 ? v4_prefix
: v6_prefix
,
605 len
- parsed_len
, prefix
);
606 if(rc
< 0) goto invalid
;
610 rc
= network_prefix(ae
, src_plen
, 0, message
+ 2 + parsed_len
,
611 NULL
, len
- parsed_len
, src_prefix
);
612 if(rc
< 0) goto invalid
;
617 ND_PRINT((ndo
, " %s from", format_prefix(ndo
, prefix
, plen
)));
618 ND_PRINT((ndo
, " %s metric %u seqno %u interval %s",
619 format_prefix(ndo
, src_prefix
, src_plen
),
620 metric
, seqno
, format_interval_update(interval
)));
622 if((u_int
)parsed_len
< len
)
623 subtlvs_print(ndo
, message
+ 2 + parsed_len
,
624 message
+ 2 + len
, type
);
629 case MESSAGE_REQUEST_SRC_SPECIFIC
: {
631 ND_PRINT((ndo
, " ss-request"));
633 int rc
, parsed_len
= 3;
634 u_char ae
, plen
, src_plen
, prefix
[16], src_prefix
[16];
635 ND_PRINT((ndo
, "\n\tSS-Request "));
636 if(len
< 3) goto invalid
;
639 src_plen
= message
[4];
640 rc
= network_prefix(ae
, plen
, 0, message
+ 2 + parsed_len
,
641 NULL
, len
- parsed_len
, prefix
);
642 if(rc
< 0) goto invalid
;
646 rc
= network_prefix(ae
, src_plen
, 0, message
+ 2 + parsed_len
,
647 NULL
, len
- parsed_len
, src_prefix
);
648 if(rc
< 0) goto invalid
;
653 ND_PRINT((ndo
, "for any"));
655 ND_PRINT((ndo
, "for (%s, ", format_prefix(ndo
, prefix
, plen
)));
656 ND_PRINT((ndo
, "%s)", format_prefix(ndo
, src_prefix
, src_plen
)));
662 case MESSAGE_MH_REQUEST_SRC_SPECIFIC
: {
664 ND_PRINT((ndo
, " ss-mh-request"));
666 int rc
, parsed_len
= 14;
668 u_char ae
, plen
, src_plen
, prefix
[16], src_prefix
[16], hopc
;
669 const u_char
*router_id
= NULL
;
670 ND_PRINT((ndo
, "\n\tSS-MH-Request "));
671 if(len
< 14) goto invalid
;
674 seqno
= EXTRACT_16BITS(message
+ 4);
676 src_plen
= message
[7];
677 router_id
= message
+ 8;
678 rc
= network_prefix(ae
, plen
, 0, message
+ 2 + parsed_len
,
679 NULL
, len
- parsed_len
, prefix
);
680 if(rc
< 0) goto invalid
;
684 rc
= network_prefix(ae
, src_plen
, 0, message
+ 2 + parsed_len
,
685 NULL
, len
- parsed_len
, src_prefix
);
686 if(rc
< 0) goto invalid
;
689 ND_PRINT((ndo
, "(%u hops) for (%s, ",
690 hopc
, format_prefix(ndo
, prefix
, plen
)));
691 ND_PRINT((ndo
, "%s) seqno %u id %s",
692 format_prefix(ndo
, src_prefix
, src_plen
),
693 seqno
, format_id(router_id
)));
700 ND_PRINT((ndo
, " unknown"));
702 ND_PRINT((ndo
, "\n\tUnknown message type %d", type
));
709 ND_PRINT((ndo
, " %s", tstr
));
713 ND_PRINT((ndo
, "%s", istr
));