]> The Tcpdump Group git mirrors - tcpdump/blob - print-babel.c
Merge git://github.com/the-tcpdump-group/tcpdump
[tcpdump] / print-babel.c
1 /*
2 * Copyright (c) 2007-2011 Grégoire Henry, Juliusz Chroboczek
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <tcpdump-stdinc.h>
34
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "addrtoname.h"
39 #include "interface.h"
40 #include "extract.h"
41
42 static void babel_print_v2(const u_char *cp, u_int length);
43
44 void
45 babel_print(const u_char *cp, u_int length) {
46 printf("babel");
47
48 TCHECK2(*cp, 4);
49
50 if(cp[0] != 42) {
51 printf(" malformed header");
52 return;
53 } else {
54 printf(" %d", cp[1]);
55 }
56
57 switch(cp[1]) {
58 case 2:
59 babel_print_v2(cp,length);
60 break;
61 default:
62 printf(" unknown version");
63 break;
64 }
65
66 return;
67
68 trunc:
69 printf(" [|babel]");
70 return;
71 }
72
73 /* TLVs */
74 #define MESSAGE_PAD1 0
75 #define MESSAGE_PADN 1
76 #define MESSAGE_ACK_REQ 2
77 #define MESSAGE_ACK 3
78 #define MESSAGE_HELLO 4
79 #define MESSAGE_IHU 5
80 #define MESSAGE_ROUTER_ID 6
81 #define MESSAGE_NH 7
82 #define MESSAGE_UPDATE 8
83 #define MESSAGE_REQUEST 9
84 #define MESSAGE_MH_REQUEST 10
85 #define MESSAGE_TSPC 11
86 #define MESSAGE_HMAC 12
87
88 /* sub-TLVs */
89 #define MESSAGE_SUB_PAD1 0
90 #define MESSAGE_SUB_PADN 1
91 #define MESSAGE_SUB_CHANINFO 2
92
93 /* ChanInfo sub-TLV channel codes */
94 static const struct tok chaninfo_str[] = {
95 { 0, "reserved" },
96 { 255, "all" },
97 { 0, NULL }
98 };
99
100 static const char *
101 format_id(const u_char *id)
102 {
103 static char buf[25];
104 snprintf(buf, 25, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
105 id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7]);
106 buf[24] = '\0';
107 return buf;
108 }
109
110 static const unsigned char v4prefix[16] =
111 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
112
113 static const char *
114 format_prefix(const u_char *prefix, unsigned char plen)
115 {
116 static char buf[50];
117 if(plen >= 96 && memcmp(prefix, v4prefix, 12) == 0)
118 snprintf(buf, 50, "%s/%u", ipaddr_string(prefix + 12), plen - 96);
119 else
120 #ifdef INET6
121 snprintf(buf, 50, "%s/%u", ip6addr_string(prefix), plen);
122 #else
123 snprintf(buf, 50, "IPv6 addresses not supported");
124 #endif
125 buf[49] = '\0';
126 return buf;
127 }
128
129 static const char *
130 format_address(const u_char *prefix)
131 {
132 if(memcmp(prefix, v4prefix, 12) == 0)
133 return ipaddr_string(prefix + 12);
134 else
135 #ifdef INET6
136 return ip6addr_string(prefix);
137 #else
138 return "IPv6 addresses not supported";
139 #endif
140 }
141
142 /* Return number of octets consumed from the input buffer (not the prefix length
143 * in bytes), or -1 for encoding error. */
144 static int
145 network_prefix(int ae, int plen, unsigned int omitted,
146 const unsigned char *p, const unsigned char *dp,
147 unsigned int len, unsigned char *p_r)
148 {
149 unsigned pb;
150 unsigned char prefix[16];
151 int consumed = 0;
152
153 if(plen >= 0)
154 pb = (plen + 7) / 8;
155 else if(ae == 1)
156 pb = 4;
157 else
158 pb = 16;
159
160 if(pb > 16)
161 return -1;
162
163 memset(prefix, 0, 16);
164
165 switch(ae) {
166 case 0: break;
167 case 1:
168 if(omitted > 4 || pb > 4 || (pb > omitted && len < pb - omitted))
169 return -1;
170 memcpy(prefix, v4prefix, 12);
171 if(omitted) {
172 if (dp == NULL) return -1;
173 memcpy(prefix, dp, 12 + omitted);
174 }
175 if(pb > omitted) {
176 memcpy(prefix + 12 + omitted, p, pb - omitted);
177 consumed = pb - omitted;
178 }
179 break;
180 case 2:
181 if(omitted > 16 || (pb > omitted && len < pb - omitted))
182 return -1;
183 if(omitted) {
184 if (dp == NULL) return -1;
185 memcpy(prefix, dp, omitted);
186 }
187 if(pb > omitted) {
188 memcpy(prefix + omitted, p, pb - omitted);
189 consumed = pb - omitted;
190 }
191 break;
192 case 3:
193 if(pb > 8 && len < pb - 8) return -1;
194 prefix[0] = 0xfe;
195 prefix[1] = 0x80;
196 if(pb > 8) {
197 memcpy(prefix + 8, p, pb - 8);
198 consumed = pb - 8;
199 }
200 break;
201 default:
202 return -1;
203 }
204
205 memcpy(p_r, prefix, 16);
206 return consumed;
207 }
208
209 static int
210 network_address(int ae, const unsigned char *a, unsigned int len,
211 unsigned char *a_r)
212 {
213 return network_prefix(ae, -1, 0, a, NULL, len, a_r);
214 }
215
216 /*
217 * Sub-TLVs consume the "extra data" of Babel TLVs (see Section 4.3 of RFC6126),
218 * their encoding is similar to the encoding of TLVs, but the type namespace is
219 * different:
220 *
221 * o Type 0 stands for Pad1 sub-TLV with the same encoding as the Pad1 TLV.
222 * o Type 1 stands for PadN sub-TLV with the same encoding as the PadN TLV.
223 * o Type 2 stands for ChanInfo sub-TLV, which propagates diversity routing
224 * data. Its body is a variable-length sequence of 8-bit unsigned integers,
225 * each representing per-hop number of interferring radio channel for the
226 * prefix. Channel 0 is invalid and must not be used in the sub-TLV, channel
227 * 255 interferes with any other channel.
228 *
229 * Sub-TLV types 0 and 1 are valid for any TLV type, whether sub-TLV type 2 is
230 * only valid for TLV type 8 (Update). Note that within an Update TLV a missing
231 * ChanInfo sub-TLV is not the same as a ChanInfo sub-TLV with an empty body.
232 * The former would mean a lack of any claims about the interference, and the
233 * latter would state that interference is definitely absent. */
234 static void
235 subtlvs_print(const u_char *cp, const u_char *ep, const uint8_t tlv_type) {
236 uint8_t subtype, sublen;
237 const char *sep;
238
239 while (cp < ep) {
240 subtype = *cp++;
241 if(subtype == MESSAGE_SUB_PAD1) {
242 printf(" sub-pad1");
243 continue;
244 }
245 if(cp == ep)
246 goto corrupt;
247 sublen = *cp++;
248 if(cp + sublen > ep)
249 goto corrupt;
250
251 switch(subtype) {
252 case MESSAGE_SUB_PADN:
253 printf(" sub-padn");
254 cp += sublen;
255 break;
256 case MESSAGE_SUB_CHANINFO:
257 printf(" sub-chaninfo");
258 if (sublen == 0) {
259 printf(" empty");
260 break;
261 }
262 sep = " ";
263 while(sublen--) {
264 printf("%s%s", sep, tok2str(chaninfo_str, "%u", *cp++));
265 sep = "-";
266 }
267 if(tlv_type != MESSAGE_UPDATE)
268 printf(" (bogus)");
269 break;
270 default:
271 printf(" sub-unknown-0x%02x", subtype);
272 cp += sublen;
273 } /* switch */
274 } /* while */
275 return;
276
277 corrupt:
278 printf(" (corrupt)");
279 }
280
281 #define ICHECK(i, l) \
282 if ((i) + (l) > bodylen || (i) + (l) > length) goto corrupt;
283
284 static void
285 babel_print_v2(const u_char *cp, u_int length) {
286 u_int i;
287 u_short bodylen;
288 u_char v4_prefix[16] =
289 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
290 u_char v6_prefix[16] = {0};
291
292 TCHECK2(*cp, 4);
293 if (length < 4)
294 goto corrupt;
295 bodylen = EXTRACT_16BITS(cp + 2);
296 printf(" (%u)", bodylen);
297
298 /* Process the TLVs in the body */
299 i = 0;
300 while(i < bodylen) {
301 const u_char *message;
302 u_int type, len;
303
304 message = cp + 4 + i;
305
306 TCHECK2(*message, 1);
307 if((type = message[0]) == MESSAGE_PAD1) {
308 printf(vflag ? "\n\tPad 1" : " pad1");
309 i += 1;
310 continue;
311 }
312
313 TCHECK2(*message, 2);
314 ICHECK(i, 2);
315 len = message[1];
316
317 TCHECK2(*message, 2 + len);
318 ICHECK(i, 2 + len);
319
320 switch(type) {
321 case MESSAGE_PADN: {
322 if(!vflag)
323 printf(" padN");
324 else
325 printf("\n\tPad %d", len + 2);
326 }
327 break;
328
329 case MESSAGE_ACK_REQ: {
330 u_short nonce, interval;
331 if(!vflag)
332 printf(" ack-req");
333 else {
334 printf("\n\tAcknowledgment Request ");
335 if(len < 6) goto corrupt;
336 nonce = EXTRACT_16BITS(message + 4);
337 interval = EXTRACT_16BITS(message + 6);
338 printf("%04x %d", nonce, interval);
339 }
340 }
341 break;
342
343 case MESSAGE_ACK: {
344 u_short nonce;
345 if(!vflag)
346 printf(" ack");
347 else {
348 printf("\n\tAcknowledgment ");
349 if(len < 2) goto corrupt;
350 nonce = EXTRACT_16BITS(message + 2);
351 printf("%04x", nonce);
352 }
353 }
354 break;
355
356 case MESSAGE_HELLO: {
357 u_short seqno, interval;
358 if(!vflag)
359 printf(" hello");
360 else {
361 printf("\n\tHello ");
362 if(len < 6) goto corrupt;
363 seqno = EXTRACT_16BITS(message + 4);
364 interval = EXTRACT_16BITS(message + 6);
365 printf("seqno %u interval %u", seqno, interval);
366 }
367 }
368 break;
369
370 case MESSAGE_IHU: {
371 unsigned short txcost, interval;
372 if(!vflag)
373 printf(" ihu");
374 else {
375 u_char address[16];
376 int rc;
377 printf("\n\tIHU ");
378 if(len < 6) goto corrupt;
379 txcost = EXTRACT_16BITS(message + 4);
380 interval = EXTRACT_16BITS(message + 6);
381 rc = network_address(message[2], message + 8, len - 6, address);
382 if(rc < 0) { printf("[|babel]"); break; }
383 printf("%s txcost %u interval %d",
384 format_address(address), txcost, interval);
385 }
386 }
387 break;
388
389 case MESSAGE_ROUTER_ID: {
390 if(!vflag)
391 printf(" router-id");
392 else {
393 printf("\n\tRouter Id");
394 if(len < 10) goto corrupt;
395 printf(" %s", format_id(message + 4));
396 }
397 }
398 break;
399
400 case MESSAGE_NH: {
401 if(!vflag)
402 printf(" nh");
403 else {
404 int rc;
405 u_char nh[16];
406 printf("\n\tNext Hop");
407 if(len < 2) goto corrupt;
408 rc = network_address(message[2], message + 4, len - 2, nh);
409 if(rc < 0) goto corrupt;
410 printf(" %s", format_address(nh));
411 }
412 }
413 break;
414
415 case MESSAGE_UPDATE: {
416 if(!vflag) {
417 printf(" update");
418 if(len < 1)
419 printf("/truncated");
420 else
421 printf("%s%s%s",
422 (message[3] & 0x80) ? "/prefix": "",
423 (message[3] & 0x40) ? "/id" : "",
424 (message[3] & 0x3f) ? "/unknown" : "");
425 } else {
426 u_short interval, seqno, metric;
427 u_char plen;
428 int rc;
429 u_char prefix[16];
430 printf("\n\tUpdate");
431 if(len < 10) goto corrupt;
432 plen = message[4] + (message[2] == 1 ? 96 : 0);
433 rc = network_prefix(message[2], message[4], message[5],
434 message + 12,
435 message[2] == 1 ? v4_prefix : v6_prefix,
436 len - 10, prefix);
437 if(rc < 0) goto corrupt;
438 interval = EXTRACT_16BITS(message + 6);
439 seqno = EXTRACT_16BITS(message + 8);
440 metric = EXTRACT_16BITS(message + 10);
441 printf("%s%s%s %s metric %u seqno %u interval %u",
442 (message[3] & 0x80) ? "/prefix": "",
443 (message[3] & 0x40) ? "/id" : "",
444 (message[3] & 0x3f) ? "/unknown" : "",
445 format_prefix(prefix, plen),
446 metric, seqno, interval);
447 if(message[3] & 0x80) {
448 if(message[2] == 1)
449 memcpy(v4_prefix, prefix, 16);
450 else
451 memcpy(v6_prefix, prefix, 16);
452 }
453 /* extra data? */
454 if(rc < len - 10)
455 subtlvs_print(message + 12 + rc, message + 2 + len, type);
456 }
457 }
458 break;
459
460 case MESSAGE_REQUEST: {
461 if(!vflag)
462 printf(" request");
463 else {
464 int rc;
465 u_char prefix[16], plen;
466 printf("\n\tRequest ");
467 if(len < 2) goto corrupt;
468 plen = message[3] + (message[2] == 1 ? 96 : 0);
469 rc = network_prefix(message[2], message[3], 0,
470 message + 4, NULL, len - 2, prefix);
471 if(rc < 0) goto corrupt;
472 plen = message[3] + (message[2] == 1 ? 96 : 0);
473 printf("for %s",
474 message[2] == 0 ? "any" : format_prefix(prefix, plen));
475 }
476 }
477 break;
478
479 case MESSAGE_MH_REQUEST : {
480 if(!vflag)
481 printf(" mh-request");
482 else {
483 int rc;
484 u_short seqno;
485 u_char prefix[16], plen;
486 printf("\n\tMH-Request ");
487 if(len < 14) goto corrupt;
488 seqno = EXTRACT_16BITS(message + 4);
489 rc = network_prefix(message[2], message[3], 0,
490 message + 16, NULL, len - 14, prefix);
491 if(rc < 0) goto corrupt;
492 plen = message[3] + (message[2] == 1 ? 96 : 0);
493 printf("(%u hops) for %s seqno %u id %s",
494 message[6], format_prefix(prefix, plen),
495 seqno, format_id(message + 8));
496 }
497 }
498 break;
499 case MESSAGE_TSPC :
500 if(!vflag)
501 printf(" tspc");
502 else {
503 printf("\n\tTS/PC ");
504 if(len < 6) goto corrupt;
505 printf("timestamp %u packetcounter %u", EXTRACT_32BITS (message + 4),
506 EXTRACT_16BITS(message + 2));
507 }
508 break;
509 case MESSAGE_HMAC : {
510 if(!vflag)
511 printf(" hmac");
512 else {
513 unsigned j;
514 printf("\n\tHMAC ");
515 if(len < 18) goto corrupt;
516 printf("key-id %u digest-%u ", EXTRACT_16BITS(message + 2), len - 2);
517 for (j = 0; j < len - 2; j++)
518 printf ("%02X", message[4 + j]);
519 }
520 }
521 break;
522 default:
523 if(!vflag)
524 printf(" unknown");
525 else
526 printf("\n\tUnknown message type %d", type);
527 }
528 i += len + 2;
529 }
530 return;
531
532 trunc:
533 printf(" [|babel]");
534 return;
535
536 corrupt:
537 printf(" (corrupt)");
538 return;
539 }