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