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"
42 static void babel_print_v2(const u_char *cp, u_int length);
45 babel_print(const u_char *cp, u_int length) {
51 printf(" malformed header");
59 babel_print_v2(cp,length);
62 printf(" unknown version");
73 #define MESSAGE_PAD1 0
74 #define MESSAGE_PADN 1
75 #define MESSAGE_ACK_REQ 2
77 #define MESSAGE_HELLO 4
79 #define MESSAGE_ROUTER_ID 6
81 #define MESSAGE_UPDATE 8
82 #define MESSAGE_REQUEST 9
83 #define MESSAGE_MH_REQUEST 10
86 format_id(const u_char *id)
89 snprintf(buf, 25, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
90 id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7]);
95 static const unsigned char v4prefix[16] =
96 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
99 format_prefix(const u_char *prefix, unsigned char plen)
102 if(plen >= 96 && memcmp(prefix, v4prefix, 12) == 0)
103 snprintf(buf, 50, "%s/%u", ipaddr_string(prefix + 12), plen - 96);
105 snprintf(buf, 50, "%s/%u", ip6addr_string(prefix), plen);
111 format_address(const u_char *prefix)
113 if(memcmp(prefix, v4prefix, 12) == 0)
114 return ipaddr_string(prefix + 12);
116 return ip6addr_string(prefix);
120 network_prefix(int ae, int plen, unsigned int omitted,
121 const unsigned char *p, const unsigned char *dp,
122 unsigned int len, unsigned char *p_r)
125 unsigned char prefix[16];
137 memset(prefix, 0, 16);
142 if(omitted > 4 || pb > 4 || (pb > omitted && len < pb - omitted))
144 memcpy(prefix, v4prefix, 12);
146 if (dp == NULL) return -1;
147 memcpy(prefix, dp, 12 + omitted);
149 if(pb > omitted) memcpy(prefix + 12 + omitted, p, pb - omitted);
152 if(omitted > 16 || (pb > omitted && len < pb - omitted))
155 if (dp == NULL) return -1;
156 memcpy(prefix, dp, omitted);
158 if(pb > omitted) memcpy(prefix + omitted, p, pb - omitted);
161 if(pb > 8 && len < pb - 8) return -1;
164 if(pb > 8) memcpy(prefix + 8, p, pb - 8);
170 memcpy(p_r, prefix, 16);
175 network_address(int ae, const unsigned char *a, unsigned int len,
178 return network_prefix(ae, -1, 0, a, NULL, len, a_r);
181 #define ICHECK(i, l) \
182 if ((i) + (l) > bodylen || (i) + (l) > length) goto corrupt;
185 babel_print_v2(const u_char *cp, u_int length) {
188 u_char v4_prefix[16] =
189 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
190 u_char v6_prefix[16] = {0};
195 bodylen = EXTRACT_16BITS(cp + 2);
196 printf(" (%u)", bodylen);
198 /* Process the TLVs in the body */
201 const u_char *message;
204 message = cp + 4 + i;
205 TCHECK2(*message, 2);
210 TCHECK2(*message, 2 + len);
226 printf("\n\tPad %d", len + 2);
230 case MESSAGE_ACK_REQ: {
231 u_short nonce, interval;
235 printf("\n\tAcknowledgment Request ");
236 if(len < 6) goto corrupt;
237 nonce = EXTRACT_16BITS(message + 4);
238 interval = EXTRACT_16BITS(message + 6);
239 printf("%04x %d", nonce, interval);
249 printf("\n\tAcknowledgment ");
250 if(len < 2) goto corrupt;
251 nonce = EXTRACT_16BITS(message + 2);
252 printf("%04x", nonce);
257 case MESSAGE_HELLO: {
258 u_short seqno, interval;
262 printf("\n\tHello ");
263 if(len < 6) goto corrupt;
264 seqno = EXTRACT_16BITS(message + 4);
265 interval = EXTRACT_16BITS(message + 6);
266 printf("seqno %u interval %u", seqno, interval);
272 unsigned short txcost, interval;
279 if(len < 6) goto corrupt;
280 txcost = EXTRACT_16BITS(message + 4);
281 interval = EXTRACT_16BITS(message + 6);
282 rc = network_address(message[2], message + 8, len - 6, address);
283 if(rc < 0) { printf("[|babel]"); break; }
284 printf("%s txcost %u interval %d",
285 format_address(address), txcost, interval);
290 case MESSAGE_ROUTER_ID: {
292 printf(" router-id");
294 printf("\n\tRouter Id");
295 if(len < 10) goto corrupt;
296 printf(" %s", format_id(message + 4));
307 printf("\n\tNext Hop");
308 if(len < 2) goto corrupt;
309 rc = network_address(message[2], message + 4, len - 2, nh);
310 if(rc < 0) goto corrupt;
311 printf(" %s", format_address(nh));
316 case MESSAGE_UPDATE: {
320 printf("/truncated");
323 (message[3] & 0x80) ? "/prefix": "",
324 (message[3] & 0x40) ? "/id" : "",
325 (message[3] & 0x3f) ? "/unknown" : "");
327 u_short interval, seqno, metric;
331 printf("\n\tUpdate");
332 if(len < 10) goto corrupt;
333 plen = message[4] + (message[2] == 1 ? 96 : 0);
334 rc = network_prefix(message[2], message[4], message[5],
336 message[2] == 1 ? v4_prefix : v6_prefix,
338 if(rc < 0) goto corrupt;
339 interval = EXTRACT_16BITS(message + 6);
340 seqno = EXTRACT_16BITS(message + 8);
341 metric = EXTRACT_16BITS(message + 10);
342 printf("%s%s%s %s metric %u seqno %u interval %u",
343 (message[3] & 0x80) ? "/prefix": "",
344 (message[3] & 0x40) ? "/id" : "",
345 (message[3] & 0x3f) ? "/unknown" : "",
346 format_prefix(prefix, plen),
347 metric, seqno, interval);
348 if(message[3] & 0x80) {
350 memcpy(v4_prefix, prefix, 16);
352 memcpy(v6_prefix, prefix, 16);
358 case MESSAGE_REQUEST: {
363 u_char prefix[16], plen;
364 printf("\n\tRequest ");
365 if(len < 2) goto corrupt;
366 plen = message[3] + (message[2] == 1 ? 96 : 0);
367 rc = network_prefix(message[2], message[3], 0,
368 message + 4, NULL, len - 2, prefix);
369 if(rc < 0) goto corrupt;
370 plen = message[3] + (message[2] == 1 ? 96 : 0);
372 message[2] == 0 ? "any" : format_prefix(prefix, plen));
377 case MESSAGE_MH_REQUEST : {
379 printf(" mh-request");
383 u_char prefix[16], plen;
384 printf("\n\tMH-Request ");
385 if(len < 14) goto corrupt;
386 seqno = EXTRACT_16BITS(message + 4);
387 rc = network_prefix(message[2], message[3], 0,
388 message + 16, NULL, len - 14, prefix);
389 if(rc < 0) goto corrupt;
390 plen = message[3] + (message[2] == 1 ? 96 : 0);
391 printf("(%u hops) for %s seqno %u id %s",
392 message[6], format_prefix(prefix, plen),
393 seqno, format_id(message + 8));
401 printf("\n\tUnknown message type %d", type);
412 printf(" (corrupt)");