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