]>
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 "addrtoname.h"
39 #include "interface.h"
41 static void babel_print_v2(const u_char
*cp
, u_int length
);
44 babel_print(const u_char
*cp
, u_int length
) {
50 printf(" malformed header");
58 babel_print_v2(cp
,length
);
61 printf(" unknown version");
73 struct __us
{ unsigned short x
__attribute__((packed
)); };
74 #define DO_NTOHS(_d, _s) \
75 do { _d = ntohs(((const struct __us*)(_s))->x); } while(0)
76 #define DO_HTONS(_d, _s) \
77 do { ((struct __us*)(_d))->x = htons(_s); } while(0)
79 #define DO_NTOHS(_d, _s) \
81 memcpy(&(_dd), (_s), 2); \
82 _d = ntohs(_dd); } while(0)
83 #define DO_HTONS(_d, _s) \
84 do { unsigned short _dd; \
86 memcpy((_d), &(_dd), 2); } while(0)
89 #define MESSAGE_PAD1 0
90 #define MESSAGE_PADN 1
91 #define MESSAGE_ACK_REQ 2
93 #define MESSAGE_HELLO 4
95 #define MESSAGE_ROUTER_ID 6
97 #define MESSAGE_UPDATE 8
98 #define MESSAGE_REQUEST 9
99 #define MESSAGE_MH_REQUEST 10
102 format_id(const u_char
*id
)
105 snprintf(buf
, 25, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
106 id
[0], id
[1], id
[2], id
[3], id
[4], id
[5], id
[6], id
[7]);
111 static const unsigned char v4prefix
[16] =
112 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
115 format_prefix(const u_char
*prefix
, unsigned char plen
)
118 if(plen
>= 96 && memcmp(prefix
, v4prefix
, 12) == 0)
119 snprintf(buf
, 50, "%s/%u", ipaddr_string(prefix
+ 12), plen
- 96);
121 snprintf(buf
, 50, "%s/%u", ip6addr_string(prefix
), plen
);
127 format_address(const u_char
*prefix
)
129 if(memcmp(prefix
, v4prefix
, 12) == 0)
130 return ipaddr_string(prefix
+ 12);
132 return ip6addr_string(prefix
);
136 network_prefix(int ae
, int plen
, unsigned int omitted
,
137 const unsigned char *p
, const unsigned char *dp
,
138 unsigned int len
, unsigned char *p_r
)
141 unsigned char prefix
[16];
153 memset(prefix
, 0, 16);
158 if(omitted
> 4 || pb
> 4 || (pb
> omitted
&& len
< pb
- omitted
))
160 memcpy(prefix
, v4prefix
, 12);
162 if (dp
== NULL
) return -1;
163 memcpy(prefix
, dp
, 12 + omitted
);
165 if(pb
> omitted
) memcpy(prefix
+ 12 + omitted
, p
, pb
- omitted
);
168 if(omitted
> 16 || (pb
> omitted
&& len
< pb
- omitted
))
171 if (dp
== NULL
) return -1;
172 memcpy(prefix
, dp
, omitted
);
174 if(pb
> omitted
) memcpy(prefix
+ omitted
, p
, pb
- omitted
);
177 if(pb
> 8 && len
< pb
- 8) return -1;
180 if(pb
> 8) memcpy(prefix
+ 8, p
, pb
- 8);
186 memcpy(p_r
, prefix
, 16);
191 network_address(int ae
, const unsigned char *a
, unsigned int len
,
194 return network_prefix(ae
, -1, 0, a
, NULL
, len
, a_r
);
198 babel_print_v2(const u_char
*cp
, u_int length
) {
201 u_char v4_prefix
[16] =
202 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
203 u_char v6_prefix
[16] = {0};
205 DO_NTOHS(bodylen
, cp
+ 2);
206 printf(" (%d)", bodylen
);
210 const u_char
*message
;
215 message
= cp
+ 4 + i
;
219 TCHECK2(*cp
, 4 + i
+ 2 + len
);
234 printf("\n\tPad %d", len
+ 2);
238 case MESSAGE_ACK_REQ
: {
239 u_short nonce
, interval
;
243 printf("\n\tAcknowledgment Request ");
244 if(len
< 6) goto corrupt
;
245 DO_NTOHS(nonce
, message
+ 4);
246 DO_NTOHS(interval
, message
+ 6);
247 printf("%04x %d", nonce
, interval
);
257 printf("\n\tAcknowledgment ");
258 if(len
< 2) goto corrupt
;
259 DO_NTOHS(nonce
, message
+ 2);
260 printf("%04x", nonce
);
265 case MESSAGE_HELLO
: {
266 u_short seqno
, interval
;
270 printf("\n\tHello ");
271 if(len
< 6) goto corrupt
;
272 DO_NTOHS(seqno
, message
+ 4);
273 DO_NTOHS(interval
, message
+ 6);
274 printf("seqno %u interval %u", seqno
, interval
);
280 unsigned short txcost
, interval
;
287 if(len
< 6) goto corrupt
;
288 DO_NTOHS(txcost
, message
+ 4);
289 DO_NTOHS(interval
, message
+ 6);
290 rc
= network_address(message
[2], message
+ 8, len
- 6, address
);
291 if(rc
< 0) { printf("[|babel]"); break; }
292 printf("%s txcost %u interval %d",
293 format_address(address
), txcost
, interval
);
298 case MESSAGE_ROUTER_ID
: {
300 printf(" router-id");
302 printf("\n\tRouter Id");
303 if(len
< 10) goto corrupt
;
304 printf(" %s", format_id(message
+ 4));
315 printf("\n\tNext Hop");
316 if(len
< 2) goto corrupt
;
317 rc
= network_address(message
[2], message
+ 4, len
- 2, nh
);
318 if(rc
< 0) goto corrupt
;
319 printf(" %s", format_address(nh
));
324 case MESSAGE_UPDATE
: {
328 printf("/truncated");
331 (message
[3] & 0x80) ? "/prefix": "",
332 (message
[3] & 0x40) ? "/id" : "",
333 (message
[3] & 0x3f) ? "/unknown" : "");
335 u_short interval
, seqno
, metric
;
339 printf("\n\tUpdate");
340 if(len
< 10) goto corrupt
;
341 plen
= message
[4] + (message
[2] == 1 ? 96 : 0);
342 rc
= network_prefix(message
[2], message
[4], message
[5],
344 message
[2] == 1 ? v4_prefix
: v6_prefix
,
346 if(rc
< 0) goto corrupt
;
347 DO_NTOHS(interval
, message
+ 6);
348 DO_NTOHS(seqno
, message
+ 8);
349 DO_NTOHS(metric
, message
+ 10);
350 printf("%s%s%s %s metric %u seqno %u interval %u",
351 (message
[3] & 0x80) ? "/prefix": "",
352 (message
[3] & 0x40) ? "/id" : "",
353 (message
[3] & 0x3f) ? "/unknown" : "",
354 format_prefix(prefix
, plen
),
355 metric
, seqno
, interval
);
356 if(message
[3] & 0x80) {
358 memcpy(v4_prefix
, prefix
, 16);
360 memcpy(v6_prefix
, prefix
, 16);
366 case MESSAGE_REQUEST
: {
371 u_char prefix
[16], plen
;
372 printf("\n\tRequest ");
373 if(len
< 2) goto corrupt
;
374 plen
= message
[3] + (message
[2] == 1 ? 96 : 0);
375 rc
= network_prefix(message
[2], message
[3], 0,
376 message
+ 4, NULL
, len
- 2, prefix
);
377 if(rc
< 0) goto corrupt
;
378 plen
= message
[3] + (message
[2] == 1 ? 96 : 0);
380 message
[2] == 0 ? "any" : format_prefix(prefix
, plen
));
385 case MESSAGE_MH_REQUEST
: {
387 printf(" mh-request");
391 u_char prefix
[16], plen
;
392 printf("\n\tMH-Request ");
393 if(len
< 14) goto corrupt
;
394 DO_NTOHS(seqno
, message
+ 4);
395 rc
= network_prefix(message
[2], message
[3], 0,
396 message
+ 16, NULL
, len
- 14, prefix
);
397 if(rc
< 0) goto corrupt
;
398 plen
= message
[3] + (message
[2] == 1 ? 96 : 0);
399 printf("(%u hops) for %s seqno %u id %s",
400 message
[6], format_prefix(prefix
, plen
),
401 seqno
, format_id(message
+ 8));
409 printf("\n\tUnknown message type %d", type
);
420 printf(" (corrupt)");