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", 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 id
[0], id
[1], id
[2], id
[3], id
[4], id
[5], id
[6], id
[7]);
120 static const unsigned char v4prefix
[16] =
121 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
124 format_prefix(netdissect_options
*ndo
, const u_char
*prefix
, unsigned char plen
)
127 if(plen
>= 96 && memcmp(prefix
, v4prefix
, 12) == 0)
128 snprintf(buf
, 50, "%s/%u", ipaddr_string(ndo
, prefix
+ 12), plen
- 96);
130 snprintf(buf
, 50, "%s/%u", ip6addr_string(ndo
, prefix
), plen
);
136 format_address(netdissect_options
*ndo
, const u_char
*prefix
)
138 if(memcmp(prefix
, v4prefix
, 12) == 0)
139 return ipaddr_string(ndo
, prefix
+ 12);
141 return ip6addr_string(ndo
, prefix
);
145 format_interval(const uint16_t i
)
147 static char buf
[sizeof("000.00s")];
150 return "0.0s (bogus)";
151 snprintf(buf
, sizeof(buf
), "%u.%02us", i
/ 100, i
% 100);
156 format_interval_update(const uint16_t i
)
158 return i
== 0xFFFF ? "infinity" : format_interval(i
);
162 format_timestamp(const uint32_t i
)
164 static char buf
[sizeof("0000.000000s")];
165 snprintf(buf
, sizeof(buf
), "%u.%06us", i
/ 1000000, i
% 1000000);
169 /* Return number of octets consumed from the input buffer (not the prefix length
170 * in bytes), or -1 for encoding error. */
172 network_prefix(int ae
, int plen
, unsigned int omitted
,
173 const unsigned char *p
, const unsigned char *dp
,
174 unsigned int len
, unsigned char *p_r
)
177 unsigned char prefix
[16];
190 memset(prefix
, 0, 16);
195 if(omitted
> 4 || pb
> 4 || (pb
> omitted
&& len
< pb
- omitted
))
197 memcpy(prefix
, v4prefix
, 12);
199 if (dp
== NULL
) return -1;
200 memcpy(prefix
, dp
, 12 + omitted
);
203 memcpy(prefix
+ 12 + omitted
, p
, pb
- omitted
);
204 consumed
= pb
- omitted
;
208 if(omitted
> 16 || (pb
> omitted
&& len
< pb
- omitted
))
211 if (dp
== NULL
) return -1;
212 memcpy(prefix
, dp
, omitted
);
215 memcpy(prefix
+ omitted
, p
, pb
- omitted
);
216 consumed
= pb
- omitted
;
220 if(pb
> 8 && len
< pb
- 8) return -1;
224 memcpy(prefix
+ 8, p
, pb
- 8);
232 memcpy(p_r
, prefix
, 16);
237 network_address(int ae
, const unsigned char *a
, unsigned int len
,
240 return network_prefix(ae
, -1, 0, a
, NULL
, len
, a_r
);
244 * Sub-TLVs consume the "extra data" of Babel TLVs (see Section 4.3 of RFC6126),
245 * their encoding is similar to the encoding of TLVs, but the type namespace is
248 * o Type 0 stands for Pad1 sub-TLV with the same encoding as the Pad1 TLV.
249 * o Type 1 stands for PadN sub-TLV with the same encoding as the PadN TLV.
250 * o Type 2 stands for Diversity sub-TLV, which propagates diversity routing
251 * data. Its body is a variable-length sequence of 8-bit unsigned integers,
252 * each representing per-hop number of interferring radio channel for the
253 * prefix. Channel 0 is invalid and must not be used in the sub-TLV, channel
254 * 255 interferes with any other channel.
255 * o Type 3 stands for Timestamp sub-TLV, used to compute RTT between
256 * neighbours. In the case of a Hello TLV, the body stores a 32-bits
257 * timestamp, while in the case of a IHU TLV, two 32-bits timestamps are
260 * Sub-TLV types 0 and 1 are valid for any TLV type, whether sub-TLV type 2 is
261 * only valid for TLV type 8 (Update). Note that within an Update TLV a missing
262 * Diversity sub-TLV is not the same as a Diversity sub-TLV with an empty body.
263 * The former would mean a lack of any claims about the interference, and the
264 * latter would state that interference is definitely absent.
265 * A type 3 sub-TLV is valid both for Hello and IHU TLVs, though the exact
266 * semantic of the sub-TLV is different in each case.
269 subtlvs_print(netdissect_options
*ndo
,
270 const u_char
*cp
, const u_char
*ep
, const uint8_t tlv_type
)
272 uint8_t subtype
, sublen
;
277 subtype
= EXTRACT_U_1(cp
);
279 if(subtype
== MESSAGE_SUB_PAD1
) {
280 ND_PRINT((ndo
, " sub-pad1"));
285 sublen
= EXTRACT_U_1(cp
);
291 case MESSAGE_SUB_PADN
:
292 ND_PRINT((ndo
, " sub-padn"));
295 case MESSAGE_SUB_DIVERSITY
:
296 ND_PRINT((ndo
, " sub-diversity"));
298 ND_PRINT((ndo
, " empty"));
303 ND_PRINT((ndo
, "%s%s", sep
, tok2str(diversity_str
, "%u", EXTRACT_U_1(cp
))));
307 if(tlv_type
!= MESSAGE_UPDATE
&&
308 tlv_type
!= MESSAGE_UPDATE_SRC_SPECIFIC
)
309 ND_PRINT((ndo
, " (bogus)"));
311 case MESSAGE_SUB_TIMESTAMP
:
312 ND_PRINT((ndo
, " sub-timestamp"));
313 if(tlv_type
== MESSAGE_HELLO
) {
316 t1
= EXTRACT_BE_U_4(cp
);
317 ND_PRINT((ndo
, " %s", format_timestamp(t1
)));
318 } else if(tlv_type
== MESSAGE_IHU
) {
321 t1
= EXTRACT_BE_U_4(cp
);
322 ND_PRINT((ndo
, " %s", format_timestamp(t1
)));
323 t2
= EXTRACT_BE_U_4(cp
+ 4);
324 ND_PRINT((ndo
, "|%s", format_timestamp(t2
)));
326 ND_PRINT((ndo
, " (bogus)"));
330 ND_PRINT((ndo
, " sub-unknown-0x%02x", subtype
));
337 ND_PRINT((ndo
, "%s", istr
));
340 #define ICHECK(i, l) \
341 if ((i) + (l) > bodylen || (i) + (l) > length) goto invalid;
344 babel_print_v2(netdissect_options
*ndo
,
345 const u_char
*cp
, u_int length
)
349 u_char v4_prefix
[16] =
350 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
351 u_char v6_prefix
[16] = {0};
356 bodylen
= EXTRACT_BE_U_2(cp
+ 2);
357 ND_PRINT((ndo
, " (%u)", bodylen
));
359 /* Process the TLVs in the body */
362 const u_char
*message
;
365 message
= cp
+ 4 + i
;
367 ND_TCHECK_1(message
);
368 if((type
= message
[0]) == MESSAGE_PAD1
) {
369 ND_PRINT((ndo
, ndo
->ndo_vflag
? "\n\tPad 1" : " pad1"));
374 ND_TCHECK_2(message
);
378 ND_TCHECK2(*message
, 2 + len
);
384 ND_PRINT((ndo
, " padN"));
386 ND_PRINT((ndo
, "\n\tPad %d", len
+ 2));
390 case MESSAGE_ACK_REQ
: {
391 u_short nonce
, interval
;
393 ND_PRINT((ndo
, " ack-req"));
395 ND_PRINT((ndo
, "\n\tAcknowledgment Request "));
396 if(len
< 6) goto invalid
;
397 nonce
= EXTRACT_BE_U_2(message
+ 4);
398 interval
= EXTRACT_BE_U_2(message
+ 6);
399 ND_PRINT((ndo
, "%04x %s", nonce
, format_interval(interval
)));
407 ND_PRINT((ndo
, " ack"));
409 ND_PRINT((ndo
, "\n\tAcknowledgment "));
410 if(len
< 2) goto invalid
;
411 nonce
= EXTRACT_BE_U_2(message
+ 2);
412 ND_PRINT((ndo
, "%04x", nonce
));
417 case MESSAGE_HELLO
: {
418 u_short seqno
, interval
;
420 ND_PRINT((ndo
, " hello"));
422 ND_PRINT((ndo
, "\n\tHello "));
423 if(len
< 6) goto invalid
;
424 seqno
= EXTRACT_BE_U_2(message
+ 4);
425 interval
= EXTRACT_BE_U_2(message
+ 6);
426 ND_PRINT((ndo
, "seqno %u interval %s", seqno
, format_interval(interval
)));
429 subtlvs_print(ndo
, message
+ 8, message
+ 2 + len
, type
);
435 unsigned short txcost
, interval
;
437 ND_PRINT((ndo
, " ihu"));
441 ND_PRINT((ndo
, "\n\tIHU "));
442 if(len
< 6) goto invalid
;
443 txcost
= EXTRACT_BE_U_2(message
+ 4);
444 interval
= EXTRACT_BE_U_2(message
+ 6);
445 rc
= network_address(message
[2], message
+ 8, len
- 6, address
);
446 if(rc
< 0) { ND_PRINT((ndo
, "%s", tstr
)); break; }
447 ND_PRINT((ndo
, "%s txcost %u interval %s",
448 format_address(ndo
, address
), txcost
, format_interval(interval
)));
450 if((u_int
)rc
< len
- 6)
451 subtlvs_print(ndo
, message
+ 8 + rc
, message
+ 2 + len
,
457 case MESSAGE_ROUTER_ID
: {
459 ND_PRINT((ndo
, " router-id"));
461 ND_PRINT((ndo
, "\n\tRouter Id"));
462 if(len
< 10) goto invalid
;
463 ND_PRINT((ndo
, " %s", format_id(message
+ 4)));
470 ND_PRINT((ndo
, " nh"));
474 ND_PRINT((ndo
, "\n\tNext Hop"));
475 if(len
< 2) goto invalid
;
476 rc
= network_address(message
[2], message
+ 4, len
- 2, nh
);
477 if(rc
< 0) goto invalid
;
478 ND_PRINT((ndo
, " %s", format_address(ndo
, nh
)));
483 case MESSAGE_UPDATE
: {
484 if (!ndo
->ndo_vflag
) {
485 ND_PRINT((ndo
, " update"));
487 ND_PRINT((ndo
, "/truncated"));
489 ND_PRINT((ndo
, "%s%s%s",
490 (message
[3] & 0x80) ? "/prefix": "",
491 (message
[3] & 0x40) ? "/id" : "",
492 (message
[3] & 0x3f) ? "/unknown" : ""));
494 u_short interval
, seqno
, metric
;
498 ND_PRINT((ndo
, "\n\tUpdate"));
499 if(len
< 10) goto invalid
;
500 plen
= message
[4] + (message
[2] == 1 ? 96 : 0);
501 rc
= network_prefix(message
[2], message
[4], message
[5],
503 message
[2] == 1 ? v4_prefix
: v6_prefix
,
505 if(rc
< 0) goto invalid
;
506 interval
= EXTRACT_BE_U_2(message
+ 6);
507 seqno
= EXTRACT_BE_U_2(message
+ 8);
508 metric
= EXTRACT_BE_U_2(message
+ 10);
509 ND_PRINT((ndo
, "%s%s%s %s metric %u seqno %u interval %s",
510 (message
[3] & 0x80) ? "/prefix": "",
511 (message
[3] & 0x40) ? "/id" : "",
512 (message
[3] & 0x3f) ? "/unknown" : "",
513 format_prefix(ndo
, prefix
, plen
),
514 metric
, seqno
, format_interval_update(interval
)));
515 if(message
[3] & 0x80) {
517 memcpy(v4_prefix
, prefix
, 16);
519 memcpy(v6_prefix
, prefix
, 16);
522 if((u_int
)rc
< len
- 10)
523 subtlvs_print(ndo
, message
+ 12 + rc
, message
+ 2 + len
, type
);
528 case MESSAGE_REQUEST
: {
530 ND_PRINT((ndo
, " request"));
533 u_char prefix
[16], plen
;
534 ND_PRINT((ndo
, "\n\tRequest "));
535 if(len
< 2) goto invalid
;
536 plen
= message
[3] + (message
[2] == 1 ? 96 : 0);
537 rc
= network_prefix(message
[2], message
[3], 0,
538 message
+ 4, NULL
, len
- 2, prefix
);
539 if(rc
< 0) goto invalid
;
540 ND_PRINT((ndo
, "for %s",
541 message
[2] == 0 ? "any" : format_prefix(ndo
, prefix
, plen
)));
546 case MESSAGE_MH_REQUEST
: {
548 ND_PRINT((ndo
, " mh-request"));
552 u_char prefix
[16], plen
;
553 ND_PRINT((ndo
, "\n\tMH-Request "));
554 if(len
< 14) goto invalid
;
555 seqno
= EXTRACT_BE_U_2(message
+ 4);
556 rc
= network_prefix(message
[2], message
[3], 0,
557 message
+ 16, NULL
, len
- 14, prefix
);
558 if(rc
< 0) goto invalid
;
559 plen
= message
[3] + (message
[2] == 1 ? 96 : 0);
560 ND_PRINT((ndo
, "(%u hops) for %s seqno %u id %s",
561 message
[6], format_prefix(ndo
, prefix
, plen
),
562 seqno
, format_id(message
+ 8)));
568 ND_PRINT((ndo
, " tspc"));
570 ND_PRINT((ndo
, "\n\tTS/PC "));
571 if(len
< 6) goto invalid
;
572 ND_PRINT((ndo
, "timestamp %u packetcounter %u", EXTRACT_BE_U_4(message
+ 4),
573 EXTRACT_BE_U_2(message
+ 2)));
576 case MESSAGE_HMAC
: {
578 ND_PRINT((ndo
, " hmac"));
581 ND_PRINT((ndo
, "\n\tHMAC "));
582 if(len
< 18) goto invalid
;
583 ND_PRINT((ndo
, "key-id %u digest-%u ", EXTRACT_BE_U_2(message
+ 2), len
- 2));
584 for (j
= 0; j
< len
- 2; j
++)
585 ND_PRINT((ndo
, "%02X", message
[4 + j
]));
590 case MESSAGE_UPDATE_SRC_SPECIFIC
: {
591 if(!ndo
->ndo_vflag
) {
592 ND_PRINT((ndo
, " ss-update"));
594 u_char prefix
[16], src_prefix
[16];
595 u_short interval
, seqno
, metric
;
596 u_char ae
, plen
, src_plen
, omitted
;
599 ND_PRINT((ndo
, "\n\tSS-Update"));
600 if(len
< 10) goto invalid
;
602 src_plen
= message
[3];
604 omitted
= message
[5];
605 interval
= EXTRACT_BE_U_2(message
+ 6);
606 seqno
= EXTRACT_BE_U_2(message
+ 8);
607 metric
= EXTRACT_BE_U_2(message
+ 10);
608 rc
= network_prefix(ae
, plen
, omitted
, message
+ 2 + parsed_len
,
609 ae
== 1 ? v4_prefix
: v6_prefix
,
610 len
- parsed_len
, prefix
);
611 if(rc
< 0) goto invalid
;
615 rc
= network_prefix(ae
, src_plen
, 0, message
+ 2 + parsed_len
,
616 NULL
, len
- parsed_len
, src_prefix
);
617 if(rc
< 0) goto invalid
;
622 ND_PRINT((ndo
, " %s from", format_prefix(ndo
, prefix
, plen
)));
623 ND_PRINT((ndo
, " %s metric %u seqno %u interval %s",
624 format_prefix(ndo
, src_prefix
, src_plen
),
625 metric
, seqno
, format_interval_update(interval
)));
627 if((u_int
)parsed_len
< len
)
628 subtlvs_print(ndo
, message
+ 2 + parsed_len
,
629 message
+ 2 + len
, type
);
634 case MESSAGE_REQUEST_SRC_SPECIFIC
: {
636 ND_PRINT((ndo
, " ss-request"));
638 int rc
, parsed_len
= 3;
639 u_char ae
, plen
, src_plen
, prefix
[16], src_prefix
[16];
640 ND_PRINT((ndo
, "\n\tSS-Request "));
641 if(len
< 3) goto invalid
;
644 src_plen
= message
[4];
645 rc
= network_prefix(ae
, plen
, 0, message
+ 2 + parsed_len
,
646 NULL
, len
- parsed_len
, prefix
);
647 if(rc
< 0) goto invalid
;
651 rc
= network_prefix(ae
, src_plen
, 0, message
+ 2 + parsed_len
,
652 NULL
, len
- parsed_len
, src_prefix
);
653 if(rc
< 0) goto invalid
;
658 ND_PRINT((ndo
, "for any"));
660 ND_PRINT((ndo
, "for (%s, ", format_prefix(ndo
, prefix
, plen
)));
661 ND_PRINT((ndo
, "%s)", format_prefix(ndo
, src_prefix
, src_plen
)));
667 case MESSAGE_MH_REQUEST_SRC_SPECIFIC
: {
669 ND_PRINT((ndo
, " ss-mh-request"));
671 int rc
, parsed_len
= 14;
673 u_char ae
, plen
, src_plen
, prefix
[16], src_prefix
[16], hopc
;
674 const u_char
*router_id
= NULL
;
675 ND_PRINT((ndo
, "\n\tSS-MH-Request "));
676 if(len
< 14) goto invalid
;
679 seqno
= EXTRACT_BE_U_2(message
+ 4);
681 src_plen
= message
[7];
682 router_id
= message
+ 8;
683 rc
= network_prefix(ae
, plen
, 0, message
+ 2 + parsed_len
,
684 NULL
, len
- parsed_len
, prefix
);
685 if(rc
< 0) goto invalid
;
689 rc
= network_prefix(ae
, src_plen
, 0, message
+ 2 + parsed_len
,
690 NULL
, len
- parsed_len
, src_prefix
);
691 if(rc
< 0) goto invalid
;
694 ND_PRINT((ndo
, "(%u hops) for (%s, ",
695 hopc
, format_prefix(ndo
, prefix
, plen
)));
696 ND_PRINT((ndo
, "%s) seqno %u id %s",
697 format_prefix(ndo
, src_prefix
, src_plen
),
698 seqno
, format_id(router_id
)));
705 ND_PRINT((ndo
, " unknown"));
707 ND_PRINT((ndo
, "\n\tUnknown message type %d", type
));
714 ND_PRINT((ndo
, " %s", tstr
));
718 ND_PRINT((ndo
, "%s", istr
));