]> The Tcpdump Group git mirrors - tcpdump/blob - print-babel.c
remove redundant ND_TCHECK, let GET_ routines handle checks
[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 if((type = GET_U_1(message)) == MESSAGE_PAD1) {
399 ND_PRINT(ndo->ndo_vflag ? "\n\tPad 1" : " pad1");
400 i += 1;
401 continue;
402 }
403
404 ICHECK(i, 2);
405 ND_TCHECK_2(message);
406 len = GET_U_1(message + 1);
407
408 ICHECK(i, 2 + len);
409 ND_TCHECK_LEN(message, 2 + len);
410
411 switch(type) {
412 case MESSAGE_PADN: {
413 if (!ndo->ndo_vflag)
414 ND_PRINT(" padN");
415 else
416 ND_PRINT("\n\tPad %u", len + 2);
417 }
418 break;
419
420 case MESSAGE_ACK_REQ: {
421 u_short nonce, interval;
422 if (!ndo->ndo_vflag)
423 ND_PRINT(" ack-req");
424 else {
425 ND_PRINT("\n\tAcknowledgment Request ");
426 if(len < 6) goto invalid;
427 nonce = GET_BE_U_2(message + 4);
428 interval = GET_BE_U_2(message + 6);
429 ND_PRINT("%04x %s", nonce, format_interval(interval));
430 }
431 }
432 break;
433
434 case MESSAGE_ACK: {
435 u_short nonce;
436 if (!ndo->ndo_vflag)
437 ND_PRINT(" ack");
438 else {
439 ND_PRINT("\n\tAcknowledgment ");
440 if(len < 2) goto invalid;
441 nonce = GET_BE_U_2(message + 2);
442 ND_PRINT("%04x", nonce);
443 }
444 }
445 break;
446
447 case MESSAGE_HELLO: {
448 u_short seqno, interval, unicast;
449 if (!ndo->ndo_vflag)
450 ND_PRINT(" hello");
451 else {
452 ND_PRINT("\n\tHello ");
453 if(len < 6) goto invalid;
454 unicast = (GET_BE_U_2(message + 2) & UNICAST_MASK);
455 seqno = GET_BE_U_2(message + 4);
456 interval = GET_BE_U_2(message + 6);
457 if(unicast)
458 ND_PRINT("(Unicast) ");
459 ND_PRINT("seqno %u ", seqno);
460 if(interval!=0)
461 ND_PRINT("interval %s", format_interval(interval));
462 else
463 ND_PRINT("unscheduled");
464 /* Extra data. */
465 if(len > 6)
466 subtlvs_print(ndo, message + 8, message + 2 + len, type);
467 }
468 }
469 break;
470
471 case MESSAGE_IHU: {
472 unsigned short rxcost, interval;
473 if (!ndo->ndo_vflag)
474 ND_PRINT(" ihu");
475 else {
476 u_char address[16];
477 u_char ae;
478 int rc;
479 ND_PRINT("\n\tIHU ");
480 if(len < 6) goto invalid;
481 rxcost = GET_BE_U_2(message + 4);
482 interval = GET_BE_U_2(message + 6);
483 ae = GET_U_1(message + 2);
484 rc = network_address(ae, message + 8,
485 len - 6, address);
486 if(rc < 0) { nd_print_trunc(ndo); break; }
487 ND_PRINT("%s rxcost %u interval %s",
488 ae == 0 ? "any" : format_address(ndo, address),
489 rxcost, format_interval(interval));
490 /* Extra data. */
491 if((u_int)rc < len - 6)
492 subtlvs_print(ndo, message + 8 + rc, message + 2 + len,
493 type);
494 }
495 }
496 break;
497
498 case MESSAGE_ROUTER_ID: {
499 if (!ndo->ndo_vflag)
500 ND_PRINT(" router-id");
501 else {
502 ND_PRINT("\n\tRouter Id");
503 if(len < 10) goto invalid;
504 ND_PRINT(" %s", format_id(ndo, message + 4));
505 }
506 }
507 break;
508
509 case MESSAGE_NH: {
510 if (!ndo->ndo_vflag)
511 ND_PRINT(" nh");
512 else {
513 int rc;
514 u_char ae;
515 u_char nh[16];
516 ND_PRINT("\n\tNext Hop");
517 if(len < 2) goto invalid;
518 ae = GET_U_1(message + 2);
519 rc = network_address(ae, message + 4,
520 len - 2, nh);
521 if(rc < 0) goto invalid;
522 ND_PRINT(" %s", ae == 0 ? "invalid AE 0" : format_address(ndo, nh));
523 }
524 }
525 break;
526
527 case MESSAGE_UPDATE: {
528 if (!ndo->ndo_vflag) {
529 ND_PRINT(" update");
530 if(len < 10)
531 goto invalid;
532 else
533 ND_PRINT("%s%s%s",
534 (GET_U_1(message + 3) & 0x80) ? "/prefix": "",
535 (GET_U_1(message + 3) & 0x40) ? "/id" : "",
536 (GET_U_1(message + 3) & 0x3f) ? "/unknown" : "");
537 } else {
538 u_short interval, seqno, metric;
539 u_char ae, plen;
540 int rc;
541 u_char prefix[16];
542 ND_PRINT("\n\tUpdate");
543 if(len < 10) goto invalid;
544 ae = GET_U_1(message + 2);
545 plen = GET_U_1(message + 4) + (GET_U_1(message + 2) == 1 ? 96 : 0);
546 rc = network_prefix(ae,
547 GET_U_1(message + 4),
548 GET_U_1(message + 5),
549 message + 12,
550 GET_U_1(message + 2) == 1 ? v4_prefix : v6_prefix,
551 len - 10, prefix);
552 if(rc < 0) goto invalid;
553 interval = GET_BE_U_2(message + 6);
554 seqno = GET_BE_U_2(message + 8);
555 metric = GET_BE_U_2(message + 10);
556 ND_PRINT("%s%s%s %s metric %u seqno %u interval %s",
557 (GET_U_1(message + 3) & 0x80) ? "/prefix": "",
558 (GET_U_1(message + 3) & 0x40) ? "/id" : "",
559 (GET_U_1(message + 3) & 0x3f) ? "/unknown" : "",
560 ae == 0 ? "any" : format_prefix(ndo, prefix, plen),
561 metric, seqno, format_interval_update(interval));
562 if(GET_U_1(message + 3) & 0x80) {
563 if(GET_U_1(message + 2) == 1)
564 memcpy(v4_prefix, prefix, 16);
565 else
566 memcpy(v6_prefix, prefix, 16);
567 }
568 /* extra data? */
569 if((u_int)rc < len - 10)
570 subtlvs_print(ndo, message + 12 + rc, message + 2 + len, type);
571 }
572 }
573 break;
574
575 case MESSAGE_ROUTE_REQUEST: {
576 if (!ndo->ndo_vflag)
577 ND_PRINT(" route-request");
578 else {
579 int rc;
580 u_char prefix[16], ae, plen;
581 ND_PRINT("\n\tRoute Request ");
582 if(len < 2) goto invalid;
583 ae = GET_U_1(message + 2);
584 plen = GET_U_1(message + 3) + (GET_U_1(message + 2) == 1 ? 96 : 0);
585 rc = network_prefix(ae,
586 GET_U_1(message + 3), 0,
587 message + 4, NULL, len - 2, prefix);
588 if(rc < 0) goto invalid;
589 ND_PRINT("for %s",
590 ae == 0 ? "any" : format_prefix(ndo, prefix, plen));
591 }
592 }
593 break;
594
595 case MESSAGE_SEQNO_REQUEST : {
596 if (!ndo->ndo_vflag)
597 ND_PRINT(" seqno-request");
598 else {
599 int rc;
600 u_short seqno;
601 u_char prefix[16], ae, plen;
602 ND_PRINT("\n\tSeqno Request ");
603 if(len < 14) goto invalid;
604 ae = GET_U_1(message + 2);
605 seqno = GET_BE_U_2(message + 4);
606 rc = network_prefix(ae,
607 GET_U_1(message + 3), 0,
608 message + 16, NULL, len - 14, prefix);
609 if(rc < 0) goto invalid;
610 plen = GET_U_1(message + 3) + (GET_U_1(message + 2) == 1 ? 96 : 0);
611 ND_PRINT("(%u hops) for %s seqno %u id %s",
612 GET_U_1(message + 6),
613 ae == 0 ? "invalid AE 0" : format_prefix(ndo, prefix, plen),
614 seqno, format_id(ndo, message + 8));
615 }
616 }
617 break;
618 case MESSAGE_TSPC :
619 if (!ndo->ndo_vflag)
620 ND_PRINT(" tspc");
621 else {
622 ND_PRINT("\n\tTS/PC ");
623 if(len < 6) goto invalid;
624 ND_PRINT("timestamp %u packetcounter %u",
625 GET_BE_U_4(message + 4),
626 GET_BE_U_2(message + 2));
627 }
628 break;
629 case MESSAGE_HMAC : {
630 if (!ndo->ndo_vflag)
631 ND_PRINT(" hmac");
632 else {
633 unsigned j;
634 ND_PRINT("\n\tHMAC ");
635 if(len < 18) goto invalid;
636 ND_PRINT("key-id %u digest-%u ", GET_BE_U_2(message + 2),
637 len - 2);
638 for (j = 0; j < len - 2; j++)
639 ND_PRINT("%02X", GET_U_1(message + j + 4));
640 }
641 }
642 break;
643
644 case MESSAGE_UPDATE_SRC_SPECIFIC : {
645 if(!ndo->ndo_vflag) {
646 ND_PRINT(" ss-update");
647 } else {
648 u_char prefix[16], src_prefix[16];
649 u_short interval, seqno, metric;
650 u_char ae, plen, src_plen, omitted;
651 int rc;
652 int parsed_len = 10;
653 ND_PRINT("\n\tSS-Update");
654 if(len < 10) goto invalid;
655 ae = GET_U_1(message + 2);
656 src_plen = GET_U_1(message + 3);
657 plen = GET_U_1(message + 4);
658 omitted = GET_U_1(message + 5);
659 interval = GET_BE_U_2(message + 6);
660 seqno = GET_BE_U_2(message + 8);
661 metric = GET_BE_U_2(message + 10);
662 rc = network_prefix(ae, plen, omitted, message + 2 + parsed_len,
663 ae == 1 ? v4_prefix : v6_prefix,
664 len - parsed_len, prefix);
665 if(rc < 0) goto invalid;
666 if(ae == 1)
667 plen += 96;
668 parsed_len += rc;
669 rc = network_prefix(ae, src_plen, 0, message + 2 + parsed_len,
670 NULL, len - parsed_len, src_prefix);
671 if(rc < 0) goto invalid;
672 if(ae == 1)
673 src_plen += 96;
674 parsed_len += rc;
675
676 ND_PRINT(" %s from", format_prefix(ndo, prefix, plen));
677 ND_PRINT(" %s metric %u seqno %u interval %s",
678 format_prefix(ndo, src_prefix, src_plen),
679 metric, seqno, format_interval_update(interval));
680 /* extra data? */
681 if((u_int)parsed_len < len)
682 subtlvs_print(ndo, message + 2 + parsed_len,
683 message + 2 + len, type);
684 }
685 }
686 break;
687
688 case MESSAGE_REQUEST_SRC_SPECIFIC : {
689 if(!ndo->ndo_vflag)
690 ND_PRINT(" ss-request");
691 else {
692 int rc, parsed_len = 3;
693 u_char ae, plen, src_plen, prefix[16], src_prefix[16];
694 ND_PRINT("\n\tSS-Request ");
695 if(len < 3) goto invalid;
696 ae = GET_U_1(message + 2);
697 plen = GET_U_1(message + 3);
698 src_plen = GET_U_1(message + 4);
699 rc = network_prefix(ae, plen, 0, message + 2 + parsed_len,
700 NULL, len - parsed_len, prefix);
701 if(rc < 0) goto invalid;
702 if(ae == 1)
703 plen += 96;
704 parsed_len += rc;
705 rc = network_prefix(ae, src_plen, 0, message + 2 + parsed_len,
706 NULL, len - parsed_len, src_prefix);
707 if(rc < 0) goto invalid;
708 if(ae == 1)
709 src_plen += 96;
710 parsed_len += rc;
711 if(ae == 0) {
712 ND_PRINT("for any");
713 } else {
714 ND_PRINT("for (%s, ", format_prefix(ndo, prefix, plen));
715 ND_PRINT("%s)", format_prefix(ndo, src_prefix, src_plen));
716 }
717 }
718 }
719 break;
720
721 case MESSAGE_MH_REQUEST_SRC_SPECIFIC : {
722 if(!ndo->ndo_vflag)
723 ND_PRINT(" ss-mh-request");
724 else {
725 int rc, parsed_len = 14;
726 u_short seqno;
727 u_char ae, plen, src_plen, prefix[16], src_prefix[16], hopc;
728 const u_char *router_id = NULL;
729 ND_PRINT("\n\tSS-MH-Request ");
730 if(len < 14) goto invalid;
731 ae = GET_U_1(message + 2);
732 plen = GET_U_1(message + 3);
733 seqno = GET_BE_U_2(message + 4);
734 hopc = GET_U_1(message + 6);
735 src_plen = GET_U_1(message + 7);
736 router_id = message + 8;
737 rc = network_prefix(ae, plen, 0, message + 2 + parsed_len,
738 NULL, len - parsed_len, prefix);
739 if(rc < 0) goto invalid;
740 if(ae == 1)
741 plen += 96;
742 parsed_len += rc;
743 rc = network_prefix(ae, src_plen, 0, message + 2 + parsed_len,
744 NULL, len - parsed_len, src_prefix);
745 if(rc < 0) goto invalid;
746 if(ae == 1)
747 src_plen += 96;
748 ND_PRINT("(%u hops) for (%s, ",
749 hopc, format_prefix(ndo, prefix, plen));
750 ND_PRINT("%s) seqno %u id %s",
751 format_prefix(ndo, src_prefix, src_plen),
752 seqno, format_id(ndo, router_id));
753 }
754 }
755 break;
756
757 case MESSAGE_MAC: {
758 if (!ndo->ndo_vflag)
759 ND_PRINT(" mac");
760 else {
761 ND_PRINT("\n\tMAC ");
762 ND_PRINT("len %u", len);
763 }
764 }
765 break;
766
767 case MESSAGE_PC: {
768 if (!ndo->ndo_vflag)
769 ND_PRINT(" pc");
770 else {
771 ND_PRINT("\n\tPC");
772 if(len < 4) goto invalid;
773 ND_PRINT(" value %u",
774 GET_BE_U_4(message + 2));
775 ND_PRINT(" index len %u", len-4);
776 }
777 }
778 break;
779
780 case MESSAGE_CHALLENGE_REQUEST: {
781 if (!ndo->ndo_vflag)
782 ND_PRINT(" challenge_request");
783 else {
784 ND_PRINT("\n\tChallenge Request");
785 if(len > 192) goto invalid;
786 ND_PRINT(" len %u", len);
787 }
788 }
789 break;
790
791 case MESSAGE_CHALLENGE_REPLY: {
792 if (!ndo->ndo_vflag)
793 ND_PRINT(" challenge_reply");
794 else {
795 ND_PRINT("\n\tChallenge Reply");
796 if (len > 192) goto invalid;
797 ND_PRINT(" len %u", len);
798 }
799 }
800 break;
801
802 default:
803 if (!ndo->ndo_vflag)
804 ND_PRINT(" unknown");
805 else
806 ND_PRINT("\n\tUnknown message type %u", type);
807 }
808 i += len + 2;
809 }
810
811 return 0; /* OK */
812
813 trunc:
814 return -1; /* packet truncated by capture process */
815
816 invalid:
817 return -2; /* packet is invalid */
818 }
819
820 static void
821 babel_print_v2(netdissect_options *ndo,
822 const u_char *cp, u_int length)
823 {
824 u_short bodylen;
825 int ret;
826
827 ND_TCHECK_4(cp);
828 if (length < 4)
829 goto invalid;
830 bodylen = GET_BE_U_2(cp + 2);
831 ND_PRINT(" (%u)", bodylen);
832 length -= 4;
833 cp += 4;
834
835 /* Process the TLVs in the body */
836 if (length < bodylen)
837 goto invalid;
838 ret = babel_print_v2_tlvs(ndo, cp, bodylen, length);
839 if (ret == -1)
840 goto trunc;
841 if (ret == -2)
842 goto invalid;
843 length -= bodylen;
844 cp += bodylen;
845
846 /* If there's a trailer, process the TLVs in the trailer */
847 if (length != 0) {
848 if(ndo->ndo_vflag) ND_PRINT("\n\t----");
849 else ND_PRINT(" |");
850 ret = babel_print_v2_tlvs(ndo, cp, length, length);
851 if (ret == -1)
852 goto trunc;
853 if (ret == -2)
854 goto invalid;
855 }
856 return;
857
858 trunc:
859 nd_print_trunc(ndo);
860 return;
861
862 invalid:
863 nd_print_invalid(ndo);
864 return;
865 }