]> The Tcpdump Group git mirrors - tcpdump/blob - print-babel.c
Remove some trailing spaces/tabs
[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 /* \summary: Babel Routing Protocol printer */
30 /* Specifications:
31 *
32 * RFC 6126
33 * RFC 7298
34 * RFC 7557
35 * draft-ietf-babel-rfc6126bis-17
36 * draft-ietf-babel-hmac-10
37 * draft-ietf-babel-source-specific-0
38 */
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include "netdissect-stdinc.h"
45
46 #include <stdio.h>
47 #include <string.h>
48
49 #include "netdissect.h"
50 #include "addrtoname.h"
51 #include "extract.h"
52
53 static void babel_print_v2(netdissect_options *, const u_char *cp, u_int length);
54
55 void
56 babel_print(netdissect_options *ndo,
57 const u_char *cp, u_int length)
58 {
59 ndo->ndo_protocol = "babel";
60 ND_PRINT("babel");
61
62 ND_TCHECK_4(cp);
63
64 if(GET_U_1(cp) != 42) {
65 ND_PRINT(" invalid header");
66 return;
67 } else {
68 ND_PRINT(" %u", GET_U_1(cp + 1));
69 }
70
71 switch(GET_U_1(cp + 1)) {
72 case 2:
73 babel_print_v2(ndo, cp, length);
74 break;
75 default:
76 ND_PRINT(" unknown version");
77 break;
78 }
79
80 return;
81
82 trunc:
83 nd_print_trunc(ndo);
84 return;
85 }
86
87 /* TLVs */
88 #define MESSAGE_PAD1 0
89 #define MESSAGE_PADN 1
90 #define MESSAGE_ACK_REQ 2
91 #define MESSAGE_ACK 3
92 #define MESSAGE_HELLO 4
93 #define MESSAGE_IHU 5
94 #define MESSAGE_ROUTER_ID 6
95 #define MESSAGE_NH 7
96 #define MESSAGE_UPDATE 8
97 #define MESSAGE_ROUTE_REQUEST 9
98 #define MESSAGE_SEQNO_REQUEST 10
99 #define MESSAGE_TSPC 11
100 #define MESSAGE_HMAC 12
101 #define MESSAGE_UPDATE_SRC_SPECIFIC 13 /* last appearance in draft-boutier-babel-source-specific-01 */
102 #define MESSAGE_REQUEST_SRC_SPECIFIC 14 /* idem */
103 #define MESSAGE_MH_REQUEST_SRC_SPECIFIC 15 /* idem */
104 #define MESSAGE_MAC 16
105 #define MESSAGE_PC 17
106 #define MESSAGE_CHALLENGE_REQUEST 18
107 #define MESSAGE_CHALLENGE_REPLY 19
108
109 /* sub-TLVs */
110 #define MESSAGE_SUB_PAD1 0
111 #define MESSAGE_SUB_PADN 1
112 #define MESSAGE_SUB_DIVERSITY 2
113 #define MESSAGE_SUB_TIMESTAMP 3
114
115 /* "Mandatory" bit in sub-TLV types */
116 #define MANDATORY_MASK 0x80
117
118 /* Flags for the Hello TLV */
119 #define UNICAST_MASK 0x8000
120
121 /* Diversity sub-TLV channel codes */
122 static const struct tok diversity_str[] = {
123 { 0, "reserved" },
124 { 255, "all" },
125 { 0, NULL }
126 };
127
128 static const char *
129 format_id(netdissect_options *ndo, const u_char *id)
130 {
131 static char buf[25];
132 snprintf(buf, 25, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
133 GET_U_1(id), GET_U_1(id + 1), GET_U_1(id + 2),
134 GET_U_1(id + 3), GET_U_1(id + 4), GET_U_1(id + 5),
135 GET_U_1(id + 6), GET_U_1(id + 7));
136 buf[24] = '\0';
137 return buf;
138 }
139
140 static const unsigned char v4prefix[16] =
141 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
142
143 static const char *
144 format_prefix(netdissect_options *ndo, const u_char *prefix, unsigned char plen)
145 {
146 static char buf[50];
147
148 /*
149 * prefix points to a buffer on the stack into which the prefix has
150 * been placed, so we can't use GET_IPADDR_STRING() or
151 * GET_IP6ADDR_STRING() on it.
152 */
153 if(plen >= 96 && memcmp(prefix, v4prefix, 12) == 0)
154 snprintf(buf, 50, "%s/%u", ipaddr_string(ndo, prefix + 12), plen - 96);
155 else
156 snprintf(buf, 50, "%s/%u", ip6addr_string(ndo, prefix), plen);
157 buf[49] = '\0';
158 return buf;
159 }
160
161 static const char *
162 format_address(netdissect_options *ndo, const u_char *prefix)
163 {
164 /*
165 * prefix points to a buffer on the stack into which the prefix has
166 * been placed, so we can't use GET_IPADDR_STRING() or
167 * GET_IP6ADDR_STRING() on it.
168 */
169 if(memcmp(prefix, v4prefix, 12) == 0)
170 return ipaddr_string(ndo, prefix + 12);
171 else
172 return ip6addr_string(ndo, prefix);
173 }
174
175 static const char *
176 format_interval(const uint16_t i)
177 {
178 static char buf[sizeof("000.00s")];
179
180 if (i == 0)
181 return "0.0s (bogus)";
182 snprintf(buf, sizeof(buf), "%u.%02us", i / 100, i % 100);
183 return buf;
184 }
185
186 static const char *
187 format_interval_update(const uint16_t i)
188 {
189 return i == 0xFFFF ? "infinity" : format_interval(i);
190 }
191
192 static const char *
193 format_timestamp(const uint32_t i)
194 {
195 static char buf[sizeof("0000.000000s")];
196 snprintf(buf, sizeof(buf), "%u.%06us", i / 1000000, i % 1000000);
197 return buf;
198 }
199
200 /* Return number of octets consumed from the input buffer (not the prefix length
201 * in bytes), or -1 for encoding error. */
202 static int
203 network_prefix(int ae, int plen, unsigned int omitted,
204 const unsigned char *p, const unsigned char *dp,
205 unsigned int len, unsigned char *p_r)
206 {
207 unsigned pb;
208 unsigned char prefix[16];
209 int consumed = 0;
210
211 if(plen >= 0)
212 pb = (plen + 7) / 8;
213 else if(ae == 1)
214 pb = 4;
215 else
216 pb = 16;
217
218 if(pb > 16)
219 return -1;
220
221 memset(prefix, 0, 16);
222
223 switch(ae) {
224 case 0: break;
225 case 1:
226 if(omitted > 4 || pb > 4 || (pb > omitted && len < pb - omitted))
227 return -1;
228 memcpy(prefix, v4prefix, 12);
229 if(omitted) {
230 if (dp == NULL) return -1;
231 memcpy(prefix, dp, 12 + omitted);
232 }
233 if(pb > omitted) {
234 memcpy(prefix + 12 + omitted, p, pb - omitted);
235 consumed = pb - omitted;
236 }
237 break;
238 case 2:
239 if(omitted > 16 || (pb > omitted && len < pb - omitted))
240 return -1;
241 if(omitted) {
242 if (dp == NULL) return -1;
243 memcpy(prefix, dp, omitted);
244 }
245 if(pb > omitted) {
246 memcpy(prefix + omitted, p, pb - omitted);
247 consumed = pb - omitted;
248 }
249 break;
250 case 3:
251 if(pb > 8 && len < pb - 8) return -1;
252 prefix[0] = 0xfe;
253 prefix[1] = 0x80;
254 if(pb > 8) {
255 memcpy(prefix + 8, p, pb - 8);
256 consumed = pb - 8;
257 }
258 break;
259 default:
260 return -1;
261 }
262
263 memcpy(p_r, prefix, 16);
264 return consumed;
265 }
266
267 static int
268 network_address(int ae, const unsigned char *a, unsigned int len,
269 unsigned char *a_r)
270 {
271 return network_prefix(ae, -1, 0, a, NULL, len, a_r);
272 }
273
274 /*
275 * Sub-TLVs consume the "extra data" of Babel TLVs (see Section 4.3 of RFC6126),
276 * their encoding is similar to the encoding of TLVs, but the type namespace is
277 * different:
278 *
279 * o Type 0 stands for Pad1 sub-TLV with the same encoding as the Pad1 TLV.
280 * o Type 1 stands for PadN sub-TLV with the same encoding as the PadN TLV.
281 * o Type 2 stands for Diversity sub-TLV, which propagates diversity routing
282 * data. Its body is a variable-length sequence of 8-bit unsigned integers,
283 * each representing per-hop number of interfering radio channel for the
284 * prefix. Channel 0 is invalid and must not be used in the sub-TLV, channel
285 * 255 interferes with any other channel.
286 * o Type 3 stands for Timestamp sub-TLV, used to compute RTT between
287 * neighbours. In the case of a Hello TLV, the body stores a 32-bits
288 * timestamp, while in the case of a IHU TLV, two 32-bits timestamps are
289 * stored.
290 *
291 * Sub-TLV types 0 and 1 are valid for any TLV type, whether sub-TLV type 2 is
292 * only valid for TLV type 8 (Update). Note that within an Update TLV a missing
293 * Diversity sub-TLV is not the same as a Diversity sub-TLV with an empty body.
294 * The former would mean a lack of any claims about the interference, and the
295 * latter would state that interference is definitely absent.
296 * A type 3 sub-TLV is valid both for Hello and IHU TLVs, though the exact
297 * semantic of the sub-TLV is different in each case.
298 */
299 static void
300 subtlvs_print(netdissect_options *ndo,
301 const u_char *cp, const u_char *ep, const uint8_t tlv_type)
302 {
303 uint8_t subtype, sublen;
304 const char *sep;
305 uint32_t t1, t2;
306
307 while (cp < ep) {
308 subtype = GET_U_1(cp);
309 cp++;
310 if(subtype == MESSAGE_SUB_PAD1) {
311 ND_PRINT(" sub-pad1");
312 continue;
313 }
314 if ((MANDATORY_MASK & subtype) != 0)
315 ND_PRINT(" (M)");
316 if(cp == ep)
317 goto invalid;
318 sublen = GET_U_1(cp);
319 cp++;
320 if(cp + sublen > ep)
321 goto invalid;
322
323 switch(subtype) {
324 case MESSAGE_SUB_PADN:
325 ND_PRINT(" sub-padn");
326 cp += sublen;
327 break;
328 case MESSAGE_SUB_DIVERSITY:
329 ND_PRINT(" sub-diversity");
330 if (sublen == 0) {
331 ND_PRINT(" empty");
332 break;
333 }
334 sep = " ";
335 while (sublen) {
336 ND_PRINT("%s%s", sep,
337 tok2str(diversity_str, "%u", GET_U_1(cp)));
338 cp++;
339 sep = "-";
340 sublen--;
341 }
342 if(tlv_type != MESSAGE_UPDATE &&
343 tlv_type != MESSAGE_UPDATE_SRC_SPECIFIC)
344 ND_PRINT(" (bogus)");
345 break;
346 case MESSAGE_SUB_TIMESTAMP:
347 ND_PRINT(" sub-timestamp");
348 if(tlv_type == MESSAGE_HELLO) {
349 if(sublen < 4)
350 goto invalid;
351 t1 = GET_BE_U_4(cp);
352 ND_PRINT(" %s", format_timestamp(t1));
353 } else if(tlv_type == MESSAGE_IHU) {
354 if(sublen < 8)
355 goto invalid;
356 t1 = GET_BE_U_4(cp);
357 ND_PRINT(" %s", format_timestamp(t1));
358 t2 = GET_BE_U_4(cp + 4);
359 ND_PRINT("|%s", format_timestamp(t2));
360 } else
361 ND_PRINT(" (bogus)");
362 cp += sublen;
363 break;
364 default:
365 ND_PRINT(" sub-unknown-0x%02x", subtype);
366 cp += sublen;
367 } /* switch */
368 } /* while */
369 return;
370
371 invalid:
372 nd_print_invalid(ndo);
373 }
374
375 #define ICHECK(i, l) \
376 if ((i) + (l) > tlvs_length || (i) + (l) > packet_length_remaining) \
377 goto invalid;
378
379 static int
380 babel_print_v2_tlvs(netdissect_options *ndo,
381 const u_char *cp, u_int tlvs_length,
382 u_int packet_length_remaining)
383 {
384 u_int i;
385 u_char v4_prefix[16] =
386 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0 };
387 u_char v6_prefix[16] = {0};
388
389 i = 0;
390 while(i < tlvs_length) {
391 const u_char *message;
392 uint8_t type;
393 u_int len;
394
395 message = cp + i;
396
397 ICHECK(i, 1);
398 ND_TCHECK_1(message);
399 if((type = GET_U_1(message)) == MESSAGE_PAD1) {
400 ND_PRINT(ndo->ndo_vflag ? "\n\tPad 1" : " pad1");
401 i += 1;
402 continue;
403 }
404
405 ICHECK(i, 2);
406 ND_TCHECK_2(message);
407 len = GET_U_1(message + 1);
408
409 ICHECK(i, 2 + len);
410 ND_TCHECK_LEN(message, 2 + len);
411
412 switch(type) {
413 case MESSAGE_PADN: {
414 if (!ndo->ndo_vflag)
415 ND_PRINT(" padN");
416 else
417 ND_PRINT("\n\tPad %u", len + 2);
418 }
419 break;
420
421 case MESSAGE_ACK_REQ: {
422 u_short nonce, interval;
423 if (!ndo->ndo_vflag)
424 ND_PRINT(" ack-req");
425 else {
426 ND_PRINT("\n\tAcknowledgment Request ");
427 if(len < 6) goto invalid;
428 nonce = GET_BE_U_2(message + 4);
429 interval = GET_BE_U_2(message + 6);
430 ND_PRINT("%04x %s", nonce, format_interval(interval));
431 }
432 }
433 break;
434
435 case MESSAGE_ACK: {
436 u_short nonce;
437 if (!ndo->ndo_vflag)
438 ND_PRINT(" ack");
439 else {
440 ND_PRINT("\n\tAcknowledgment ");
441 if(len < 2) goto invalid;
442 nonce = GET_BE_U_2(message + 2);
443 ND_PRINT("%04x", nonce);
444 }
445 }
446 break;
447
448 case MESSAGE_HELLO: {
449 u_short seqno, interval, unicast;
450 if (!ndo->ndo_vflag)
451 ND_PRINT(" hello");
452 else {
453 ND_PRINT("\n\tHello ");
454 if(len < 6) goto invalid;
455 unicast = (GET_BE_U_2(message + 2) & UNICAST_MASK);
456 seqno = GET_BE_U_2(message + 4);
457 interval = GET_BE_U_2(message + 6);
458 if(unicast)
459 ND_PRINT("(Unicast) ");
460 ND_PRINT("seqno %u ", seqno);
461 if(interval!=0)
462 ND_PRINT("interval %s", format_interval(interval));
463 else
464 ND_PRINT("unscheduled");
465 /* Extra data. */
466 if(len > 6)
467 subtlvs_print(ndo, message + 8, message + 2 + len, type);
468 }
469 }
470 break;
471
472 case MESSAGE_IHU: {
473 unsigned short rxcost, interval;
474 if (!ndo->ndo_vflag)
475 ND_PRINT(" ihu");
476 else {
477 u_char address[16];
478 u_char ae;
479 int rc;
480 ND_PRINT("\n\tIHU ");
481 if(len < 6) goto invalid;
482 rxcost = GET_BE_U_2(message + 4);
483 interval = GET_BE_U_2(message + 6);
484 ae = GET_U_1(message + 2);
485 rc = network_address(ae, message + 8,
486 len - 6, address);
487 if(rc < 0) { nd_print_trunc(ndo); break; }
488 ND_PRINT("%s rxcost %u interval %s",
489 ae == 0 ? "any" : format_address(ndo, address),
490 rxcost, format_interval(interval));
491 /* Extra data. */
492 if((u_int)rc < len - 6)
493 subtlvs_print(ndo, message + 8 + rc, message + 2 + len,
494 type);
495 }
496 }
497 break;
498
499 case MESSAGE_ROUTER_ID: {
500 if (!ndo->ndo_vflag)
501 ND_PRINT(" router-id");
502 else {
503 ND_PRINT("\n\tRouter Id");
504 if(len < 10) goto invalid;
505 ND_PRINT(" %s", format_id(ndo, message + 4));
506 }
507 }
508 break;
509
510 case MESSAGE_NH: {
511 if (!ndo->ndo_vflag)
512 ND_PRINT(" nh");
513 else {
514 int rc;
515 u_char ae;
516 u_char nh[16];
517 ND_PRINT("\n\tNext Hop");
518 if(len < 2) goto invalid;
519 ae = GET_U_1(message + 2);
520 rc = network_address(ae, message + 4,
521 len - 2, nh);
522 if(rc < 0) goto invalid;
523 ND_PRINT(" %s", ae == 0 ? "invalid AE 0" : format_address(ndo, nh));
524 }
525 }
526 break;
527
528 case MESSAGE_UPDATE: {
529 if (!ndo->ndo_vflag) {
530 ND_PRINT(" update");
531 if(len < 10)
532 goto invalid;
533 else
534 ND_PRINT("%s%s%s",
535 (GET_U_1(message + 3) & 0x80) ? "/prefix": "",
536 (GET_U_1(message + 3) & 0x40) ? "/id" : "",
537 (GET_U_1(message + 3) & 0x3f) ? "/unknown" : "");
538 } else {
539 u_short interval, seqno, metric;
540 u_char ae, plen;
541 int rc;
542 u_char prefix[16];
543 ND_PRINT("\n\tUpdate");
544 if(len < 10) goto invalid;
545 ae = GET_U_1(message + 2);
546 plen = GET_U_1(message + 4) + (GET_U_1(message + 2) == 1 ? 96 : 0);
547 rc = network_prefix(ae,
548 GET_U_1(message + 4),
549 GET_U_1(message + 5),
550 message + 12,
551 GET_U_1(message + 2) == 1 ? v4_prefix : v6_prefix,
552 len - 10, prefix);
553 if(rc < 0) goto invalid;
554 interval = GET_BE_U_2(message + 6);
555 seqno = GET_BE_U_2(message + 8);
556 metric = GET_BE_U_2(message + 10);
557 ND_PRINT("%s%s%s %s metric %u seqno %u interval %s",
558 (GET_U_1(message + 3) & 0x80) ? "/prefix": "",
559 (GET_U_1(message + 3) & 0x40) ? "/id" : "",
560 (GET_U_1(message + 3) & 0x3f) ? "/unknown" : "",
561 ae == 0 ? "any" : format_prefix(ndo, prefix, plen),
562 metric, seqno, format_interval_update(interval));
563 if(GET_U_1(message + 3) & 0x80) {
564 if(GET_U_1(message + 2) == 1)
565 memcpy(v4_prefix, prefix, 16);
566 else
567 memcpy(v6_prefix, prefix, 16);
568 }
569 /* extra data? */
570 if((u_int)rc < len - 10)
571 subtlvs_print(ndo, message + 12 + rc, message + 2 + len, type);
572 }
573 }
574 break;
575
576 case MESSAGE_ROUTE_REQUEST: {
577 if (!ndo->ndo_vflag)
578 ND_PRINT(" route-request");
579 else {
580 int rc;
581 u_char prefix[16], ae, plen;
582 ND_PRINT("\n\tRoute Request ");
583 if(len < 2) goto invalid;
584 ae = GET_U_1(message + 2);
585 plen = GET_U_1(message + 3) + (GET_U_1(message + 2) == 1 ? 96 : 0);
586 rc = network_prefix(ae,
587 GET_U_1(message + 3), 0,
588 message + 4, NULL, len - 2, prefix);
589 if(rc < 0) goto invalid;
590 ND_PRINT("for %s",
591 ae == 0 ? "any" : format_prefix(ndo, prefix, plen));
592 }
593 }
594 break;
595
596 case MESSAGE_SEQNO_REQUEST : {
597 if (!ndo->ndo_vflag)
598 ND_PRINT(" seqno-request");
599 else {
600 int rc;
601 u_short seqno;
602 u_char prefix[16], ae, plen;
603 ND_PRINT("\n\tSeqno Request ");
604 if(len < 14) goto invalid;
605 ae = GET_U_1(message + 2);
606 seqno = GET_BE_U_2(message + 4);
607 rc = network_prefix(ae,
608 GET_U_1(message + 3), 0,
609 message + 16, NULL, len - 14, prefix);
610 if(rc < 0) goto invalid;
611 plen = GET_U_1(message + 3) + (GET_U_1(message + 2) == 1 ? 96 : 0);
612 ND_PRINT("(%u hops) for %s seqno %u id %s",
613 GET_U_1(message + 6),
614 ae == 0 ? "invalid AE 0" : format_prefix(ndo, prefix, plen),
615 seqno, format_id(ndo, message + 8));
616 }
617 }
618 break;
619 case MESSAGE_TSPC :
620 if (!ndo->ndo_vflag)
621 ND_PRINT(" tspc");
622 else {
623 ND_PRINT("\n\tTS/PC ");
624 if(len < 6) goto invalid;
625 ND_PRINT("timestamp %u packetcounter %u",
626 GET_BE_U_4(message + 4),
627 GET_BE_U_2(message + 2));
628 }
629 break;
630 case MESSAGE_HMAC : {
631 if (!ndo->ndo_vflag)
632 ND_PRINT(" hmac");
633 else {
634 unsigned j;
635 ND_PRINT("\n\tHMAC ");
636 if(len < 18) goto invalid;
637 ND_PRINT("key-id %u digest-%u ", GET_BE_U_2(message + 2),
638 len - 2);
639 for (j = 0; j < len - 2; j++)
640 ND_PRINT("%02X", GET_U_1(message + j + 4));
641 }
642 }
643 break;
644
645 case MESSAGE_UPDATE_SRC_SPECIFIC : {
646 if(!ndo->ndo_vflag) {
647 ND_PRINT(" ss-update");
648 } else {
649 u_char prefix[16], src_prefix[16];
650 u_short interval, seqno, metric;
651 u_char ae, plen, src_plen, omitted;
652 int rc;
653 int parsed_len = 10;
654 ND_PRINT("\n\tSS-Update");
655 if(len < 10) goto invalid;
656 ae = GET_U_1(message + 2);
657 src_plen = GET_U_1(message + 3);
658 plen = GET_U_1(message + 4);
659 omitted = GET_U_1(message + 5);
660 interval = GET_BE_U_2(message + 6);
661 seqno = GET_BE_U_2(message + 8);
662 metric = GET_BE_U_2(message + 10);
663 rc = network_prefix(ae, plen, omitted, message + 2 + parsed_len,
664 ae == 1 ? v4_prefix : v6_prefix,
665 len - parsed_len, prefix);
666 if(rc < 0) goto invalid;
667 if(ae == 1)
668 plen += 96;
669 parsed_len += rc;
670 rc = network_prefix(ae, src_plen, 0, message + 2 + parsed_len,
671 NULL, len - parsed_len, src_prefix);
672 if(rc < 0) goto invalid;
673 if(ae == 1)
674 src_plen += 96;
675 parsed_len += rc;
676
677 ND_PRINT(" %s from", format_prefix(ndo, prefix, plen));
678 ND_PRINT(" %s metric %u seqno %u interval %s",
679 format_prefix(ndo, src_prefix, src_plen),
680 metric, seqno, format_interval_update(interval));
681 /* extra data? */
682 if((u_int)parsed_len < len)
683 subtlvs_print(ndo, message + 2 + parsed_len,
684 message + 2 + len, type);
685 }
686 }
687 break;
688
689 case MESSAGE_REQUEST_SRC_SPECIFIC : {
690 if(!ndo->ndo_vflag)
691 ND_PRINT(" ss-request");
692 else {
693 int rc, parsed_len = 3;
694 u_char ae, plen, src_plen, prefix[16], src_prefix[16];
695 ND_PRINT("\n\tSS-Request ");
696 if(len < 3) goto invalid;
697 ae = GET_U_1(message + 2);
698 plen = GET_U_1(message + 3);
699 src_plen = GET_U_1(message + 4);
700 rc = network_prefix(ae, plen, 0, message + 2 + parsed_len,
701 NULL, len - parsed_len, prefix);
702 if(rc < 0) goto invalid;
703 if(ae == 1)
704 plen += 96;
705 parsed_len += rc;
706 rc = network_prefix(ae, src_plen, 0, message + 2 + parsed_len,
707 NULL, len - parsed_len, src_prefix);
708 if(rc < 0) goto invalid;
709 if(ae == 1)
710 src_plen += 96;
711 parsed_len += rc;
712 if(ae == 0) {
713 ND_PRINT("for any");
714 } else {
715 ND_PRINT("for (%s, ", format_prefix(ndo, prefix, plen));
716 ND_PRINT("%s)", format_prefix(ndo, src_prefix, src_plen));
717 }
718 }
719 }
720 break;
721
722 case MESSAGE_MH_REQUEST_SRC_SPECIFIC : {
723 if(!ndo->ndo_vflag)
724 ND_PRINT(" ss-mh-request");
725 else {
726 int rc, parsed_len = 14;
727 u_short seqno;
728 u_char ae, plen, src_plen, prefix[16], src_prefix[16], hopc;
729 const u_char *router_id = NULL;
730 ND_PRINT("\n\tSS-MH-Request ");
731 if(len < 14) goto invalid;
732 ae = GET_U_1(message + 2);
733 plen = GET_U_1(message + 3);
734 seqno = GET_BE_U_2(message + 4);
735 hopc = GET_U_1(message + 6);
736 src_plen = GET_U_1(message + 7);
737 router_id = message + 8;
738 rc = network_prefix(ae, plen, 0, message + 2 + parsed_len,
739 NULL, len - parsed_len, prefix);
740 if(rc < 0) goto invalid;
741 if(ae == 1)
742 plen += 96;
743 parsed_len += rc;
744 rc = network_prefix(ae, src_plen, 0, message + 2 + parsed_len,
745 NULL, len - parsed_len, src_prefix);
746 if(rc < 0) goto invalid;
747 if(ae == 1)
748 src_plen += 96;
749 ND_PRINT("(%u hops) for (%s, ",
750 hopc, format_prefix(ndo, prefix, plen));
751 ND_PRINT("%s) seqno %u id %s",
752 format_prefix(ndo, src_prefix, src_plen),
753 seqno, format_id(ndo, router_id));
754 }
755 }
756 break;
757
758 case MESSAGE_MAC: {
759 if (!ndo->ndo_vflag)
760 ND_PRINT(" mac");
761 else {
762 ND_PRINT("\n\tMAC ");
763 ND_PRINT("len %u", len);
764 }
765 }
766 break;
767
768 case MESSAGE_PC: {
769 if (!ndo->ndo_vflag)
770 ND_PRINT(" pc");
771 else {
772 ND_PRINT("\n\tPC");
773 if(len < 4) goto invalid;
774 ND_PRINT(" value %u",
775 GET_BE_U_4(message + 2));
776 ND_PRINT(" index len %u", len-4);
777 }
778 }
779 break;
780
781 case MESSAGE_CHALLENGE_REQUEST: {
782 if (!ndo->ndo_vflag)
783 ND_PRINT(" challenge_request");
784 else {
785 ND_PRINT("\n\tChallenge Request");
786 if(len > 192) goto invalid;
787 ND_PRINT(" len %u", len);
788 }
789 }
790 break;
791
792 case MESSAGE_CHALLENGE_REPLY: {
793 if (!ndo->ndo_vflag)
794 ND_PRINT(" challenge_reply");
795 else {
796 ND_PRINT("\n\tChallenge Reply");
797 if (len > 192) goto invalid;
798 ND_PRINT(" len %u", len);
799 }
800 }
801 break;
802
803 default:
804 if (!ndo->ndo_vflag)
805 ND_PRINT(" unknown");
806 else
807 ND_PRINT("\n\tUnknown message type %u", type);
808 }
809 i += len + 2;
810 }
811
812 return 0; /* OK */
813
814 trunc:
815 return -1; /* packet truncated by capture process */
816
817 invalid:
818 return -2; /* packet is invalid */
819 }
820
821 static void
822 babel_print_v2(netdissect_options *ndo,
823 const u_char *cp, u_int length)
824 {
825 u_short bodylen;
826 int ret;
827
828 ND_TCHECK_4(cp);
829 if (length < 4)
830 goto invalid;
831 bodylen = GET_BE_U_2(cp + 2);
832 ND_PRINT(" (%u)", bodylen);
833 length -= 4;
834 cp += 4;
835
836 /* Process the TLVs in the body */
837 if (length < bodylen)
838 goto invalid;
839 ret = babel_print_v2_tlvs(ndo, cp, bodylen, length);
840 if (ret == -1)
841 goto trunc;
842 if (ret == -2)
843 goto invalid;
844 length -= bodylen;
845 cp += bodylen;
846
847 /* If there's a trailer, process the TLVs in the trailer */
848 if (length != 0) {
849 if(ndo->ndo_vflag) ND_PRINT("\n\t----");
850 else ND_PRINT(" |");
851 ret = babel_print_v2_tlvs(ndo, cp, length, length);
852 if (ret == -1)
853 goto trunc;
854 if (ret == -2)
855 goto invalid;
856 }
857 return;
858
859 trunc:
860 nd_print_trunc(ndo);
861 return;
862
863 invalid:
864 nd_print_invalid(ndo);
865 return;
866 }