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
29 /* \summary: Babel Routing Protocol printer */
35 #include <netdissect-stdinc.h>
40 #include "netdissect.h"
41 #include "addrtoname.h"
44 static const char tstr
[] = "[|babel]";
46 static void babel_print_v2(netdissect_options
*, const u_char
*cp
, u_int length
);
49 babel_print(netdissect_options
*ndo
,
50 const u_char
*cp
, u_int length
)
52 ND_PRINT((ndo
, "babel"));
57 ND_PRINT((ndo
, " invalid header"));
60 ND_PRINT((ndo
, " %d", EXTRACT_U_1(cp
+ 1)));
65 babel_print_v2(ndo
, cp
, length
);
68 ND_PRINT((ndo
, " unknown version"));
75 ND_PRINT((ndo
, " %s", tstr
));
80 #define MESSAGE_PAD1 0
81 #define MESSAGE_PADN 1
82 #define MESSAGE_ACK_REQ 2
84 #define MESSAGE_HELLO 4
86 #define MESSAGE_ROUTER_ID 6
88 #define MESSAGE_UPDATE 8
89 #define MESSAGE_REQUEST 9
90 #define MESSAGE_MH_REQUEST 10
91 #define MESSAGE_TSPC 11
92 #define MESSAGE_HMAC 12
93 #define MESSAGE_UPDATE_SRC_SPECIFIC 13
94 #define MESSAGE_REQUEST_SRC_SPECIFIC 14
95 #define MESSAGE_MH_REQUEST_SRC_SPECIFIC 15
98 #define MESSAGE_SUB_PAD1 0
99 #define MESSAGE_SUB_PADN 1
100 #define MESSAGE_SUB_DIVERSITY 2
101 #define MESSAGE_SUB_TIMESTAMP 3
103 /* Diversity sub-TLV channel codes */
104 static const struct tok diversity_str
[] = {
111 format_id(const u_char
*id
)
114 snprintf(buf
, 25, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
115 EXTRACT_U_1(id
), EXTRACT_U_1(id
+ 1), EXTRACT_U_1(id
+ 2),
116 EXTRACT_U_1(id
+ 3), EXTRACT_U_1(id
+ 4), EXTRACT_U_1(id
+ 5),
117 EXTRACT_U_1(id
+ 6), EXTRACT_U_1(id
+ 7));
122 static const unsigned char v4prefix
[16] =
123 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
126 format_prefix(netdissect_options
*ndo
, const u_char
*prefix
, unsigned char plen
)
129 if(plen
>= 96 && memcmp(prefix
, v4prefix
, 12) == 0)
130 snprintf(buf
, 50, "%s/%u", ipaddr_string(ndo
, prefix
+ 12), plen
- 96);
132 snprintf(buf
, 50, "%s/%u", ip6addr_string(ndo
, prefix
), plen
);
138 format_address(netdissect_options
*ndo
, const u_char
*prefix
)
140 if(memcmp(prefix
, v4prefix
, 12) == 0)
141 return ipaddr_string(ndo
, prefix
+ 12);
143 return ip6addr_string(ndo
, prefix
);
147 format_interval(const uint16_t i
)
149 static char buf
[sizeof("000.00s")];
152 return "0.0s (bogus)";
153 snprintf(buf
, sizeof(buf
), "%u.%02us", i
/ 100, i
% 100);
158 format_interval_update(const uint16_t i
)
160 return i
== 0xFFFF ? "infinity" : format_interval(i
);
164 format_timestamp(const uint32_t i
)
166 static char buf
[sizeof("0000.000000s")];
167 snprintf(buf
, sizeof(buf
), "%u.%06us", i
/ 1000000, i
% 1000000);
171 /* Return number of octets consumed from the input buffer (not the prefix length
172 * in bytes), or -1 for encoding error. */
174 network_prefix(int ae
, int plen
, unsigned int omitted
,
175 const unsigned char *p
, const unsigned char *dp
,
176 unsigned int len
, unsigned char *p_r
)
179 unsigned char prefix
[16];
192 memset(prefix
, 0, 16);
197 if(omitted
> 4 || pb
> 4 || (pb
> omitted
&& len
< pb
- omitted
))
199 memcpy(prefix
, v4prefix
, 12);
201 if (dp
== NULL
) return -1;
202 memcpy(prefix
, dp
, 12 + omitted
);
205 memcpy(prefix
+ 12 + omitted
, p
, pb
- omitted
);
206 consumed
= pb
- omitted
;
210 if(omitted
> 16 || (pb
> omitted
&& len
< pb
- omitted
))
213 if (dp
== NULL
) return -1;
214 memcpy(prefix
, dp
, omitted
);
217 memcpy(prefix
+ omitted
, p
, pb
- omitted
);
218 consumed
= pb
- omitted
;
222 if(pb
> 8 && len
< pb
- 8) return -1;
226 memcpy(prefix
+ 8, p
, pb
- 8);
234 memcpy(p_r
, prefix
, 16);
239 network_address(int ae
, const unsigned char *a
, unsigned int len
,
242 return network_prefix(ae
, -1, 0, a
, NULL
, len
, a_r
);
246 * Sub-TLVs consume the "extra data" of Babel TLVs (see Section 4.3 of RFC6126),
247 * their encoding is similar to the encoding of TLVs, but the type namespace is
250 * o Type 0 stands for Pad1 sub-TLV with the same encoding as the Pad1 TLV.
251 * o Type 1 stands for PadN sub-TLV with the same encoding as the PadN TLV.
252 * o Type 2 stands for Diversity sub-TLV, which propagates diversity routing
253 * data. Its body is a variable-length sequence of 8-bit unsigned integers,
254 * each representing per-hop number of interferring radio channel for the
255 * prefix. Channel 0 is invalid and must not be used in the sub-TLV, channel
256 * 255 interferes with any other channel.
257 * o Type 3 stands for Timestamp sub-TLV, used to compute RTT between
258 * neighbours. In the case of a Hello TLV, the body stores a 32-bits
259 * timestamp, while in the case of a IHU TLV, two 32-bits timestamps are
262 * Sub-TLV types 0 and 1 are valid for any TLV type, whether sub-TLV type 2 is
263 * only valid for TLV type 8 (Update). Note that within an Update TLV a missing
264 * Diversity sub-TLV is not the same as a Diversity sub-TLV with an empty body.
265 * The former would mean a lack of any claims about the interference, and the
266 * latter would state that interference is definitely absent.
267 * A type 3 sub-TLV is valid both for Hello and IHU TLVs, though the exact
268 * semantic of the sub-TLV is different in each case.
271 subtlvs_print(netdissect_options
*ndo
,
272 const u_char
*cp
, const u_char
*ep
, const uint8_t tlv_type
)
274 uint8_t subtype
, sublen
;
279 subtype
= EXTRACT_U_1(cp
);
281 if(subtype
== MESSAGE_SUB_PAD1
) {
282 ND_PRINT((ndo
, " sub-pad1"));
287 sublen
= EXTRACT_U_1(cp
);
293 case MESSAGE_SUB_PADN
:
294 ND_PRINT((ndo
, " sub-padn"));
297 case MESSAGE_SUB_DIVERSITY
:
298 ND_PRINT((ndo
, " sub-diversity"));
300 ND_PRINT((ndo
, " empty"));
305 ND_PRINT((ndo
, "%s%s", sep
, tok2str(diversity_str
, "%u", EXTRACT_U_1(cp
))));
309 if(tlv_type
!= MESSAGE_UPDATE
&&
310 tlv_type
!= MESSAGE_UPDATE_SRC_SPECIFIC
)
311 ND_PRINT((ndo
, " (bogus)"));
313 case MESSAGE_SUB_TIMESTAMP
:
314 ND_PRINT((ndo
, " sub-timestamp"));
315 if(tlv_type
== MESSAGE_HELLO
) {
318 t1
= EXTRACT_BE_U_4(cp
);
319 ND_PRINT((ndo
, " %s", format_timestamp(t1
)));
320 } else if(tlv_type
== MESSAGE_IHU
) {
323 t1
= EXTRACT_BE_U_4(cp
);
324 ND_PRINT((ndo
, " %s", format_timestamp(t1
)));
325 t2
= EXTRACT_BE_U_4(cp
+ 4);
326 ND_PRINT((ndo
, "|%s", format_timestamp(t2
)));
328 ND_PRINT((ndo
, " (bogus)"));
332 ND_PRINT((ndo
, " sub-unknown-0x%02x", subtype
));
339 ND_PRINT((ndo
, "%s", istr
));
342 #define ICHECK(i, l) \
343 if ((i) + (l) > bodylen || (i) + (l) > length) goto invalid;
346 babel_print_v2(netdissect_options
*ndo
,
347 const u_char
*cp
, u_int length
)
351 u_char v4_prefix
[16] =
352 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
353 u_char v6_prefix
[16] = {0};
358 bodylen
= EXTRACT_BE_U_2(cp
+ 2);
359 ND_PRINT((ndo
, " (%u)", bodylen
));
361 /* Process the TLVs in the body */
364 const u_char
*message
;
367 message
= cp
+ 4 + i
;
369 ND_TCHECK_1(message
);
370 if((type
= message
[0]) == MESSAGE_PAD1
) {
371 ND_PRINT((ndo
, ndo
->ndo_vflag
? "\n\tPad 1" : " pad1"));
376 ND_TCHECK_2(message
);
380 ND_TCHECK2(*message
, 2 + len
);
386 ND_PRINT((ndo
, " padN"));
388 ND_PRINT((ndo
, "\n\tPad %d", len
+ 2));
392 case MESSAGE_ACK_REQ
: {
393 u_short nonce
, interval
;
395 ND_PRINT((ndo
, " ack-req"));
397 ND_PRINT((ndo
, "\n\tAcknowledgment Request "));
398 if(len
< 6) goto invalid
;
399 nonce
= EXTRACT_BE_U_2(message
+ 4);
400 interval
= EXTRACT_BE_U_2(message
+ 6);
401 ND_PRINT((ndo
, "%04x %s", nonce
, format_interval(interval
)));
409 ND_PRINT((ndo
, " ack"));
411 ND_PRINT((ndo
, "\n\tAcknowledgment "));
412 if(len
< 2) goto invalid
;
413 nonce
= EXTRACT_BE_U_2(message
+ 2);
414 ND_PRINT((ndo
, "%04x", nonce
));
419 case MESSAGE_HELLO
: {
420 u_short seqno
, interval
;
422 ND_PRINT((ndo
, " hello"));
424 ND_PRINT((ndo
, "\n\tHello "));
425 if(len
< 6) goto invalid
;
426 seqno
= EXTRACT_BE_U_2(message
+ 4);
427 interval
= EXTRACT_BE_U_2(message
+ 6);
428 ND_PRINT((ndo
, "seqno %u interval %s", seqno
, format_interval(interval
)));
431 subtlvs_print(ndo
, message
+ 8, message
+ 2 + len
, type
);
437 unsigned short txcost
, interval
;
439 ND_PRINT((ndo
, " ihu"));
443 ND_PRINT((ndo
, "\n\tIHU "));
444 if(len
< 6) goto invalid
;
445 txcost
= EXTRACT_BE_U_2(message
+ 4);
446 interval
= EXTRACT_BE_U_2(message
+ 6);
447 rc
= network_address(EXTRACT_U_1(message
+ 2), message
+ 8,
449 if(rc
< 0) { ND_PRINT((ndo
, "%s", tstr
)); break; }
450 ND_PRINT((ndo
, "%s txcost %u interval %s",
451 format_address(ndo
, address
), txcost
, format_interval(interval
)));
453 if((u_int
)rc
< len
- 6)
454 subtlvs_print(ndo
, message
+ 8 + rc
, message
+ 2 + len
,
460 case MESSAGE_ROUTER_ID
: {
462 ND_PRINT((ndo
, " router-id"));
464 ND_PRINT((ndo
, "\n\tRouter Id"));
465 if(len
< 10) goto invalid
;
466 ND_PRINT((ndo
, " %s", format_id(message
+ 4)));
473 ND_PRINT((ndo
, " nh"));
477 ND_PRINT((ndo
, "\n\tNext Hop"));
478 if(len
< 2) goto invalid
;
479 rc
= network_address(EXTRACT_U_1(message
+ 2), message
+ 4,
481 if(rc
< 0) goto invalid
;
482 ND_PRINT((ndo
, " %s", format_address(ndo
, nh
)));
487 case MESSAGE_UPDATE
: {
488 if (!ndo
->ndo_vflag
) {
489 ND_PRINT((ndo
, " update"));
491 ND_PRINT((ndo
, "/truncated"));
493 ND_PRINT((ndo
, "%s%s%s",
494 (EXTRACT_U_1(message
+ 3) & 0x80) ? "/prefix": "",
495 (EXTRACT_U_1(message
+ 3) & 0x40) ? "/id" : "",
496 (EXTRACT_U_1(message
+ 3) & 0x3f) ? "/unknown" : ""));
498 u_short interval
, seqno
, metric
;
502 ND_PRINT((ndo
, "\n\tUpdate"));
503 if(len
< 10) goto invalid
;
504 plen
= message
[4] + (message
[2] == 1 ? 96 : 0);
505 rc
= network_prefix(EXTRACT_U_1(message
+ 2),
506 EXTRACT_U_1(message
+ 4),
507 EXTRACT_U_1(message
+ 5),
509 message
[2] == 1 ? v4_prefix
: v6_prefix
,
511 if(rc
< 0) goto invalid
;
512 interval
= EXTRACT_BE_U_2(message
+ 6);
513 seqno
= EXTRACT_BE_U_2(message
+ 8);
514 metric
= EXTRACT_BE_U_2(message
+ 10);
515 ND_PRINT((ndo
, "%s%s%s %s metric %u seqno %u interval %s",
516 (EXTRACT_U_1(message
+ 3) & 0x80) ? "/prefix": "",
517 (EXTRACT_U_1(message
+ 3) & 0x40) ? "/id" : "",
518 (EXTRACT_U_1(message
+ 3) & 0x3f) ? "/unknown" : "",
519 format_prefix(ndo
, prefix
, plen
),
520 metric
, seqno
, format_interval_update(interval
)));
521 if(message
[3] & 0x80) {
523 memcpy(v4_prefix
, prefix
, 16);
525 memcpy(v6_prefix
, prefix
, 16);
528 if((u_int
)rc
< len
- 10)
529 subtlvs_print(ndo
, message
+ 12 + rc
, message
+ 2 + len
, type
);
534 case MESSAGE_REQUEST
: {
536 ND_PRINT((ndo
, " request"));
539 u_char prefix
[16], plen
;
540 ND_PRINT((ndo
, "\n\tRequest "));
541 if(len
< 2) goto invalid
;
542 plen
= message
[3] + (message
[2] == 1 ? 96 : 0);
543 rc
= network_prefix(EXTRACT_U_1(message
+ 2),
544 EXTRACT_U_1(message
+ 3), 0,
545 message
+ 4, NULL
, len
- 2, prefix
);
546 if(rc
< 0) goto invalid
;
547 ND_PRINT((ndo
, "for %s",
548 EXTRACT_U_1(message
+ 2) == 0 ? "any" : format_prefix(ndo
, prefix
, plen
)));
553 case MESSAGE_MH_REQUEST
: {
555 ND_PRINT((ndo
, " mh-request"));
559 u_char prefix
[16], plen
;
560 ND_PRINT((ndo
, "\n\tMH-Request "));
561 if(len
< 14) goto invalid
;
562 seqno
= EXTRACT_BE_U_2(message
+ 4);
563 rc
= network_prefix(EXTRACT_U_1(message
+ 2),
564 EXTRACT_U_1(message
+ 3), 0,
565 message
+ 16, NULL
, len
- 14, prefix
);
566 if(rc
< 0) goto invalid
;
567 plen
= message
[3] + (message
[2] == 1 ? 96 : 0);
568 ND_PRINT((ndo
, "(%u hops) for %s seqno %u id %s",
569 EXTRACT_U_1(message
+ 6), format_prefix(ndo
, prefix
, plen
),
570 seqno
, format_id(message
+ 8)));
576 ND_PRINT((ndo
, " tspc"));
578 ND_PRINT((ndo
, "\n\tTS/PC "));
579 if(len
< 6) goto invalid
;
580 ND_PRINT((ndo
, "timestamp %u packetcounter %u", EXTRACT_BE_U_4(message
+ 4),
581 EXTRACT_BE_U_2(message
+ 2)));
584 case MESSAGE_HMAC
: {
586 ND_PRINT((ndo
, " hmac"));
589 ND_PRINT((ndo
, "\n\tHMAC "));
590 if(len
< 18) goto invalid
;
591 ND_PRINT((ndo
, "key-id %u digest-%u ", EXTRACT_BE_U_2(message
+ 2), len
- 2));
592 for (j
= 0; j
< len
- 2; j
++)
593 ND_PRINT((ndo
, "%02X", EXTRACT_U_1(message
+ j
+ 4)));
598 case MESSAGE_UPDATE_SRC_SPECIFIC
: {
599 if(!ndo
->ndo_vflag
) {
600 ND_PRINT((ndo
, " ss-update"));
602 u_char prefix
[16], src_prefix
[16];
603 u_short interval
, seqno
, metric
;
604 u_char ae
, plen
, src_plen
, omitted
;
607 ND_PRINT((ndo
, "\n\tSS-Update"));
608 if(len
< 10) goto invalid
;
610 src_plen
= message
[3];
612 omitted
= message
[5];
613 interval
= EXTRACT_BE_U_2(message
+ 6);
614 seqno
= EXTRACT_BE_U_2(message
+ 8);
615 metric
= EXTRACT_BE_U_2(message
+ 10);
616 rc
= network_prefix(ae
, plen
, omitted
, message
+ 2 + parsed_len
,
617 ae
== 1 ? v4_prefix
: v6_prefix
,
618 len
- parsed_len
, prefix
);
619 if(rc
< 0) goto invalid
;
623 rc
= network_prefix(ae
, src_plen
, 0, message
+ 2 + parsed_len
,
624 NULL
, len
- parsed_len
, src_prefix
);
625 if(rc
< 0) goto invalid
;
630 ND_PRINT((ndo
, " %s from", format_prefix(ndo
, prefix
, plen
)));
631 ND_PRINT((ndo
, " %s metric %u seqno %u interval %s",
632 format_prefix(ndo
, src_prefix
, src_plen
),
633 metric
, seqno
, format_interval_update(interval
)));
635 if((u_int
)parsed_len
< len
)
636 subtlvs_print(ndo
, message
+ 2 + parsed_len
,
637 message
+ 2 + len
, type
);
642 case MESSAGE_REQUEST_SRC_SPECIFIC
: {
644 ND_PRINT((ndo
, " ss-request"));
646 int rc
, parsed_len
= 3;
647 u_char ae
, plen
, src_plen
, prefix
[16], src_prefix
[16];
648 ND_PRINT((ndo
, "\n\tSS-Request "));
649 if(len
< 3) goto invalid
;
652 src_plen
= message
[4];
653 rc
= network_prefix(ae
, plen
, 0, message
+ 2 + parsed_len
,
654 NULL
, len
- parsed_len
, prefix
);
655 if(rc
< 0) goto invalid
;
659 rc
= network_prefix(ae
, src_plen
, 0, message
+ 2 + parsed_len
,
660 NULL
, len
- parsed_len
, src_prefix
);
661 if(rc
< 0) goto invalid
;
666 ND_PRINT((ndo
, "for any"));
668 ND_PRINT((ndo
, "for (%s, ", format_prefix(ndo
, prefix
, plen
)));
669 ND_PRINT((ndo
, "%s)", format_prefix(ndo
, src_prefix
, src_plen
)));
675 case MESSAGE_MH_REQUEST_SRC_SPECIFIC
: {
677 ND_PRINT((ndo
, " ss-mh-request"));
679 int rc
, parsed_len
= 14;
681 u_char ae
, plen
, src_plen
, prefix
[16], src_prefix
[16], hopc
;
682 const u_char
*router_id
= NULL
;
683 ND_PRINT((ndo
, "\n\tSS-MH-Request "));
684 if(len
< 14) goto invalid
;
687 seqno
= EXTRACT_BE_U_2(message
+ 4);
689 src_plen
= message
[7];
690 router_id
= message
+ 8;
691 rc
= network_prefix(ae
, plen
, 0, message
+ 2 + parsed_len
,
692 NULL
, len
- parsed_len
, prefix
);
693 if(rc
< 0) goto invalid
;
697 rc
= network_prefix(ae
, src_plen
, 0, message
+ 2 + parsed_len
,
698 NULL
, len
- parsed_len
, src_prefix
);
699 if(rc
< 0) goto invalid
;
702 ND_PRINT((ndo
, "(%u hops) for (%s, ",
703 hopc
, format_prefix(ndo
, prefix
, plen
)));
704 ND_PRINT((ndo
, "%s) seqno %u id %s",
705 format_prefix(ndo
, src_prefix
, src_plen
),
706 seqno
, format_id(router_id
)));
713 ND_PRINT((ndo
, " unknown"));
715 ND_PRINT((ndo
, "\n\tUnknown message type %d", type
));
722 ND_PRINT((ndo
, " %s", tstr
));
726 ND_PRINT((ndo
, "%s", istr
));