]>
The Tcpdump Group git mirrors - tcpdump/blob - print-babel.c
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 <tcpdump-stdinc.h>
38 #include "interface.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
, " malformed 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
93 #define MESSAGE_SUB_PAD1 0
94 #define MESSAGE_SUB_PADN 1
95 #define MESSAGE_SUB_DIVERSITY 2
96 #define MESSAGE_SUB_TIMESTAMP 3
98 /* Diversity sub-TLV channel codes */
99 static const struct tok diversity_str
[] = {
106 format_id(const u_char
*id
)
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]);
115 static const unsigned char v4prefix
[16] =
116 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
119 format_prefix(netdissect_options
*ndo
, const u_char
*prefix
, unsigned char plen
)
122 if(plen
>= 96 && memcmp(prefix
, v4prefix
, 12) == 0)
123 snprintf(buf
, 50, "%s/%u", ipaddr_string(ndo
, prefix
+ 12), plen
- 96);
126 snprintf(buf
, 50, "%s/%u", ip6addr_string(ndo
, prefix
), plen
);
128 snprintf(buf
, 50, "IPv6 addresses not supported");
135 format_address(netdissect_options
*ndo
, const u_char
*prefix
)
137 if(memcmp(prefix
, v4prefix
, 12) == 0)
138 return ipaddr_string(ndo
, prefix
+ 12);
141 return ip6addr_string(ndo
, prefix
);
143 return "IPv6 addresses not supported";
148 format_interval(const uint16_t i
)
150 static char buf
[sizeof("000.00s")];
153 return "0.0s (bogus)";
154 snprintf(buf
, sizeof(buf
), "%u.%02us", i
/ 100, i
% 100);
159 format_interval_update(const uint16_t i
)
161 return i
== 0xFFFF ? "infinity" : format_interval(i
);
165 format_timestamp(const uint32_t i
)
167 static char buf
[sizeof("0000.000000s")];
168 snprintf(buf
, sizeof(buf
), "%u.%06us", i
/ 1000000, i
% 1000000);
172 /* Return number of octets consumed from the input buffer (not the prefix length
173 * in bytes), or -1 for encoding error. */
175 network_prefix(int ae
, int plen
, unsigned int omitted
,
176 const unsigned char *p
, const unsigned char *dp
,
177 unsigned int len
, unsigned char *p_r
)
180 unsigned char prefix
[16];
193 memset(prefix
, 0, 16);
198 if(omitted
> 4 || pb
> 4 || (pb
> omitted
&& len
< pb
- omitted
))
200 memcpy(prefix
, v4prefix
, 12);
202 if (dp
== NULL
) return -1;
203 memcpy(prefix
, dp
, 12 + omitted
);
206 memcpy(prefix
+ 12 + omitted
, p
, pb
- omitted
);
207 consumed
= pb
- omitted
;
211 if(omitted
> 16 || (pb
> omitted
&& len
< pb
- omitted
))
214 if (dp
== NULL
) return -1;
215 memcpy(prefix
, dp
, omitted
);
218 memcpy(prefix
+ omitted
, p
, pb
- omitted
);
219 consumed
= pb
- omitted
;
223 if(pb
> 8 && len
< pb
- 8) return -1;
227 memcpy(prefix
+ 8, p
, pb
- 8);
235 memcpy(p_r
, prefix
, 16);
240 network_address(int ae
, const unsigned char *a
, unsigned int len
,
243 return network_prefix(ae
, -1, 0, a
, NULL
, len
, a_r
);
247 * Sub-TLVs consume the "extra data" of Babel TLVs (see Section 4.3 of RFC6126),
248 * their encoding is similar to the encoding of TLVs, but the type namespace is
251 * o Type 0 stands for Pad1 sub-TLV with the same encoding as the Pad1 TLV.
252 * o Type 1 stands for PadN sub-TLV with the same encoding as the PadN TLV.
253 * o Type 2 stands for Diversity sub-TLV, which propagates diversity routing
254 * data. Its body is a variable-length sequence of 8-bit unsigned integers,
255 * each representing per-hop number of interferring radio channel for the
256 * prefix. Channel 0 is invalid and must not be used in the sub-TLV, channel
257 * 255 interferes with any other channel.
258 * o Type 3 stands for Timestamp sub-TLV, used to compute RTT between
259 * neighbours. In the case of a Hello TLV, the body stores a 32-bits
260 * timestamp, while in the case of a IHU TLV, two 32-bits timestamps are
263 * Sub-TLV types 0 and 1 are valid for any TLV type, whether sub-TLV type 2 is
264 * only valid for TLV type 8 (Update). Note that within an Update TLV a missing
265 * Diversity sub-TLV is not the same as a Diversity sub-TLV with an empty body.
266 * The former would mean a lack of any claims about the interference, and the
267 * latter would state that interference is definitely absent.
268 * A type 3 sub-TLV is valid both for Hello and IHU TLVs, though the exact
269 * semantic of the sub-TLV is different in each case.
272 subtlvs_print(netdissect_options
*ndo
,
273 const u_char
*cp
, const u_char
*ep
, const uint8_t tlv_type
)
275 uint8_t subtype
, sublen
;
281 if(subtype
== MESSAGE_SUB_PAD1
) {
282 ND_PRINT((ndo
, " sub-pad1"));
292 case MESSAGE_SUB_PADN
:
293 ND_PRINT((ndo
, " sub-padn"));
296 case MESSAGE_SUB_DIVERSITY
:
297 ND_PRINT((ndo
, " sub-diversity"));
299 ND_PRINT((ndo
, " empty"));
304 ND_PRINT((ndo
, "%s%s", sep
, tok2str(diversity_str
, "%u", *cp
++)));
307 if(tlv_type
!= MESSAGE_UPDATE
)
308 ND_PRINT((ndo
, " (bogus)"));
310 case MESSAGE_SUB_TIMESTAMP
:
311 ND_PRINT((ndo
, " sub-timestamp"));
312 if(tlv_type
== MESSAGE_HELLO
) {
315 t1
= EXTRACT_32BITS(cp
);
316 ND_PRINT((ndo
, " %s", format_timestamp(t1
)));
317 } else if(tlv_type
== MESSAGE_IHU
) {
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
)));
325 ND_PRINT((ndo
, " (bogus)"));
329 ND_PRINT((ndo
, " sub-unknown-0x%02x", subtype
));
336 ND_PRINT((ndo
, " (corrupt)"));
339 #define ICHECK(i, l) \
340 if ((i) + (l) > bodylen || (i) + (l) > length) goto corrupt;
343 babel_print_v2(netdissect_options
*ndo
,
344 const u_char
*cp
, u_int length
)
348 u_char v4_prefix
[16] =
349 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
350 u_char v6_prefix
[16] = {0};
355 bodylen
= EXTRACT_16BITS(cp
+ 2);
356 ND_PRINT((ndo
, " (%u)", bodylen
));
358 /* Process the TLVs in the body */
361 const u_char
*message
;
364 message
= cp
+ 4 + i
;
366 ND_TCHECK2(*message
, 1);
367 if((type
= message
[0]) == MESSAGE_PAD1
) {
368 ND_PRINT((ndo
, ndo
->ndo_vflag
? "\n\tPad 1" : " pad1"));
373 ND_TCHECK2(*message
, 2);
377 ND_TCHECK2(*message
, 2 + len
);
383 ND_PRINT((ndo
, " padN"));
385 ND_PRINT((ndo
, "\n\tPad %d", len
+ 2));
389 case MESSAGE_ACK_REQ
: {
390 u_short nonce
, interval
;
392 ND_PRINT((ndo
, " ack-req"));
394 ND_PRINT((ndo
, "\n\tAcknowledgment Request "));
395 if(len
< 6) goto corrupt
;
396 nonce
= EXTRACT_16BITS(message
+ 4);
397 interval
= EXTRACT_16BITS(message
+ 6);
398 ND_PRINT((ndo
, "%04x %s", nonce
, format_interval(interval
)));
406 ND_PRINT((ndo
, " ack"));
408 ND_PRINT((ndo
, "\n\tAcknowledgment "));
409 if(len
< 2) goto corrupt
;
410 nonce
= EXTRACT_16BITS(message
+ 2);
411 ND_PRINT((ndo
, "%04x", nonce
));
416 case MESSAGE_HELLO
: {
417 u_short seqno
, interval
;
419 ND_PRINT((ndo
, " hello"));
421 ND_PRINT((ndo
, "\n\tHello "));
422 if(len
< 6) goto corrupt
;
423 seqno
= EXTRACT_16BITS(message
+ 4);
424 interval
= EXTRACT_16BITS(message
+ 6);
425 ND_PRINT((ndo
, "seqno %u interval %s", seqno
, format_interval(interval
)));
428 subtlvs_print(ndo
, message
+ 8, message
+ 2 + len
, type
);
434 unsigned short txcost
, interval
;
436 ND_PRINT((ndo
, " ihu"));
440 ND_PRINT((ndo
, "\n\tIHU "));
441 if(len
< 6) goto corrupt
;
442 txcost
= EXTRACT_16BITS(message
+ 4);
443 interval
= EXTRACT_16BITS(message
+ 6);
444 rc
= network_address(message
[2], message
+ 8, len
- 6, address
);
445 if(rc
< 0) { ND_PRINT((ndo
, "%s", tstr
)); break; }
446 ND_PRINT((ndo
, "%s txcost %u interval %s",
447 format_address(ndo
, address
), txcost
, format_interval(interval
)));
449 if((u_int
)rc
< len
- 6)
450 subtlvs_print(ndo
, message
+ 8 + rc
, message
+ 2 + len
,
456 case MESSAGE_ROUTER_ID
: {
458 ND_PRINT((ndo
, " router-id"));
460 ND_PRINT((ndo
, "\n\tRouter Id"));
461 if(len
< 10) goto corrupt
;
462 ND_PRINT((ndo
, " %s", format_id(message
+ 4)));
469 ND_PRINT((ndo
, " nh"));
473 ND_PRINT((ndo
, "\n\tNext Hop"));
474 if(len
< 2) goto corrupt
;
475 rc
= network_address(message
[2], message
+ 4, len
- 2, nh
);
476 if(rc
< 0) goto corrupt
;
477 ND_PRINT((ndo
, " %s", format_address(ndo
, nh
)));
482 case MESSAGE_UPDATE
: {
483 if (!ndo
->ndo_vflag
) {
484 ND_PRINT((ndo
, " update"));
486 ND_PRINT((ndo
, "/truncated"));
488 ND_PRINT((ndo
, "%s%s%s",
489 (message
[3] & 0x80) ? "/prefix": "",
490 (message
[3] & 0x40) ? "/id" : "",
491 (message
[3] & 0x3f) ? "/unknown" : ""));
493 u_short interval
, seqno
, metric
;
497 ND_PRINT((ndo
, "\n\tUpdate"));
498 if(len
< 10) goto corrupt
;
499 plen
= message
[4] + (message
[2] == 1 ? 96 : 0);
500 rc
= network_prefix(message
[2], message
[4], message
[5],
502 message
[2] == 1 ? v4_prefix
: v6_prefix
,
504 if(rc
< 0) goto corrupt
;
505 interval
= EXTRACT_16BITS(message
+ 6);
506 seqno
= EXTRACT_16BITS(message
+ 8);
507 metric
= EXTRACT_16BITS(message
+ 10);
508 ND_PRINT((ndo
, "%s%s%s %s metric %u seqno %u interval %s",
509 (message
[3] & 0x80) ? "/prefix": "",
510 (message
[3] & 0x40) ? "/id" : "",
511 (message
[3] & 0x3f) ? "/unknown" : "",
512 format_prefix(ndo
, prefix
, plen
),
513 metric
, seqno
, format_interval_update(interval
)));
514 if(message
[3] & 0x80) {
516 memcpy(v4_prefix
, prefix
, 16);
518 memcpy(v6_prefix
, prefix
, 16);
521 if((u_int
)rc
< len
- 10)
522 subtlvs_print(ndo
, message
+ 12 + rc
, message
+ 2 + len
, type
);
527 case MESSAGE_REQUEST
: {
529 ND_PRINT((ndo
, " request"));
532 u_char prefix
[16], plen
;
533 ND_PRINT((ndo
, "\n\tRequest "));
534 if(len
< 2) goto corrupt
;
535 plen
= message
[3] + (message
[2] == 1 ? 96 : 0);
536 rc
= network_prefix(message
[2], message
[3], 0,
537 message
+ 4, NULL
, len
- 2, prefix
);
538 if(rc
< 0) goto corrupt
;
539 ND_PRINT((ndo
, "for %s",
540 message
[2] == 0 ? "any" : format_prefix(ndo
, prefix
, plen
)));
545 case MESSAGE_MH_REQUEST
: {
547 ND_PRINT((ndo
, " mh-request"));
551 u_char prefix
[16], plen
;
552 ND_PRINT((ndo
, "\n\tMH-Request "));
553 if(len
< 14) goto corrupt
;
554 seqno
= EXTRACT_16BITS(message
+ 4);
555 rc
= network_prefix(message
[2], message
[3], 0,
556 message
+ 16, NULL
, len
- 14, prefix
);
557 if(rc
< 0) goto corrupt
;
558 plen
= message
[3] + (message
[2] == 1 ? 96 : 0);
559 ND_PRINT((ndo
, "(%u hops) for %s seqno %u id %s",
560 message
[6], format_prefix(ndo
, prefix
, plen
),
561 seqno
, format_id(message
+ 8)));
567 ND_PRINT((ndo
, " tspc"));
569 ND_PRINT((ndo
, "\n\tTS/PC "));
570 if(len
< 6) goto corrupt
;
571 ND_PRINT((ndo
, "timestamp %u packetcounter %u", EXTRACT_32BITS (message
+ 4),
572 EXTRACT_16BITS(message
+ 2)));
575 case MESSAGE_HMAC
: {
577 ND_PRINT((ndo
, " hmac"));
580 ND_PRINT((ndo
, "\n\tHMAC "));
581 if(len
< 18) goto corrupt
;
582 ND_PRINT((ndo
, "key-id %u digest-%u ", EXTRACT_16BITS(message
+ 2), len
- 2));
583 for (j
= 0; j
< len
- 2; j
++)
584 ND_PRINT((ndo
, "%02X", message
[4 + j
]));
590 ND_PRINT((ndo
, " unknown"));
592 ND_PRINT((ndo
, "\n\tUnknown message type %d", type
));
599 ND_PRINT((ndo
, " %s", tstr
));
603 ND_PRINT((ndo
, " (corrupt)"));