]> The Tcpdump Group git mirrors - tcpdump/blob - print-bgp.c
basic support for extended communtities
[tcpdump] / print-bgp.c
1 /*
2 * Copyright (C) 1999 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #ifndef lint
35 static const char rcsid[] =
36 "@(#) $Header: /tcpdump/master/tcpdump/print-bgp.c,v 1.38 2002-07-22 02:55:20 hannes Exp $";
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43
44 #include <netinet/in.h>
45
46 #include <errno.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <netdb.h>
50
51 #include "interface.h"
52 #include "addrtoname.h"
53 #include "extract.h"
54
55 struct bgp {
56 u_int8_t bgp_marker[16];
57 u_int16_t bgp_len;
58 u_int8_t bgp_type;
59 };
60 #define BGP_SIZE 19 /* unaligned */
61
62 #define BGP_OPEN 1
63 #define BGP_UPDATE 2
64 #define BGP_NOTIFICATION 3
65 #define BGP_KEEPALIVE 4
66 #define BGP_ROUTE_REFRESH 5
67
68 static struct tok bgp_msg_values[] = {
69 { BGP_OPEN, "Open"},
70 { BGP_UPDATE, "Update"},
71 { BGP_NOTIFICATION, "Notification"},
72 { BGP_KEEPALIVE, "Keepalive"},
73 { BGP_ROUTE_REFRESH, "Route Refresh"},
74 { 0, NULL}
75 };
76
77 struct bgp_open {
78 u_int8_t bgpo_marker[16];
79 u_int16_t bgpo_len;
80 u_int8_t bgpo_type;
81 u_int8_t bgpo_version;
82 u_int16_t bgpo_myas;
83 u_int16_t bgpo_holdtime;
84 u_int32_t bgpo_id;
85 u_int8_t bgpo_optlen;
86 /* options should follow */
87 };
88 #define BGP_OPEN_SIZE 29 /* unaligned */
89
90 struct bgp_opt {
91 u_int8_t bgpopt_type;
92 u_int8_t bgpopt_len;
93 /* variable length */
94 };
95 #define BGP_OPT_SIZE 2 /* some compilers may pad to 4 bytes */
96
97 struct bgp_notification {
98 u_int8_t bgpn_marker[16];
99 u_int16_t bgpn_len;
100 u_int8_t bgpn_type;
101 u_int8_t bgpn_major;
102 u_int8_t bgpn_minor;
103 /* data should follow */
104 };
105 #define BGP_NOTIFICATION_SIZE 21 /* unaligned */
106
107 struct bgp_attr {
108 u_int8_t bgpa_flags;
109 u_int8_t bgpa_type;
110 union {
111 u_int8_t len;
112 u_int16_t elen;
113 } bgpa_len;
114 #define bgp_attr_len(p) \
115 (((p)->bgpa_flags & 0x10) ? \
116 ntohs((p)->bgpa_len.elen) : (p)->bgpa_len.len)
117 #define bgp_attr_off(p) \
118 (((p)->bgpa_flags & 0x10) ? 4 : 3)
119 };
120
121 #define BGPTYPE_ORIGIN 1
122 #define BGPTYPE_AS_PATH 2
123 #define BGPTYPE_NEXT_HOP 3
124 #define BGPTYPE_MULTI_EXIT_DISC 4
125 #define BGPTYPE_LOCAL_PREF 5
126 #define BGPTYPE_ATOMIC_AGGREGATE 6
127 #define BGPTYPE_AGGREGATOR 7
128 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
129 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC1998 */
130 #define BGPTYPE_CLUSTER_LIST 10 /* RFC1998 */
131 #define BGPTYPE_DPA 11 /* draft-ietf-idr-bgp-dpa */
132 #define BGPTYPE_ADVERTISERS 12 /* RFC1863 */
133 #define BGPTYPE_RCID_PATH 13 /* RFC1863 */
134 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */
135 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */
136 #define BGPTYPE_EXTD_COMMUNITIES 16 /* draft-ietf-idr-bgp-ext-communities */
137
138 static struct tok bgp_attr_values[] = {
139 { BGPTYPE_ORIGIN, "Origin"},
140 { BGPTYPE_AS_PATH, "AS Path"},
141 { BGPTYPE_NEXT_HOP, "Next Hop"},
142 { BGPTYPE_MULTI_EXIT_DISC, "Multi Exit Discriminator"},
143 { BGPTYPE_LOCAL_PREF, "Local Preference"},
144 { BGPTYPE_ATOMIC_AGGREGATE, "Atomic Aggregate"},
145 { BGPTYPE_AGGREGATOR, "Aggregator"},
146 { BGPTYPE_COMMUNITIES, "Community"},
147 { BGPTYPE_ORIGINATOR_ID, "Originator ID"},
148 { BGPTYPE_CLUSTER_LIST, "Cluster List"},
149 { BGPTYPE_DPA, "DPA"},
150 { BGPTYPE_ADVERTISERS, "Advertisers"},
151 { BGPTYPE_RCID_PATH, "RCID Path / Cluster ID"},
152 { BGPTYPE_MP_REACH_NLRI, "Multi-Protocol Reach NLRI"},
153 { BGPTYPE_MP_UNREACH_NLRI, "Multi-Protocol Unreach NLRI"},
154 { BGPTYPE_EXTD_COMMUNITIES, "Extended Community"},
155 { 255, "Reserved for development"},
156 { 0, NULL}
157 };
158
159 #define BGP_OPT_AUTH 1
160 #define BGP_OPT_CAP 2
161
162
163 static struct tok bgp_opt_values[] = {
164 { BGP_OPT_AUTH, "Authentication Information"},
165 { BGP_OPT_CAP, "Capabilities Advertisement"},
166 { 0, NULL}
167 };
168
169 #define BGP_CAPCODE_MP 1
170 #define BGP_CAPCODE_RR 2
171 #define BGP_CAPCODE_RR_CISCO 128
172
173 static struct tok bgp_capcode_values[] = {
174 { BGP_CAPCODE_MP, "Multiprotocol Extensions"},
175 { BGP_CAPCODE_RR, "Route Refresh"},
176 { BGP_CAPCODE_RR_CISCO, "Route Refresh (Cisco)"},
177 { 0, NULL}
178 };
179
180 #define BGP_NOTIFY_MAJOR_MSG 1
181 #define BGP_NOTIFY_MAJOR_OPEN 2
182 #define BGP_NOTIFY_MAJOR_UPDATE 3
183 #define BGP_NOTIFY_MAJOR_HOLDTIME 4
184 #define BGP_NOTIFY_MAJOR_FSM 5
185 #define BGP_NOTIFY_MAJOR_CEASE 6
186
187 static struct tok bgp_notify_major_values[] = {
188 { BGP_NOTIFY_MAJOR_MSG, "Message Header Error"},
189 { BGP_NOTIFY_MAJOR_OPEN, "OPEN Message Error"},
190 { BGP_NOTIFY_MAJOR_UPDATE, "UPDATE Message Error"},
191 { BGP_NOTIFY_MAJOR_HOLDTIME,"Hold Timer Expired"},
192 { BGP_NOTIFY_MAJOR_FSM, "Finite State Machine Error"},
193 { BGP_NOTIFY_MAJOR_CEASE, "Cease"},
194 { 0, NULL}
195 };
196
197 static struct tok bgp_notify_minor_msg_values[] = {
198 { 1, "Connection Not Synchronized"},
199 { 2, "Bad Message Length"},
200 { 3, "Bad Message Type"},
201 { 0, NULL}
202 };
203
204 static struct tok bgp_notify_minor_open_values[] = {
205 { 1, "Unsupported Version Number"},
206 { 2, "Bad Peer AS"},
207 { 3, "Bad BGP Identifier"},
208 { 4, "Unsupported Optional Parameter"},
209 { 5, "Authentication Failure"},
210 { 6, "Unacceptable Hold Time"},
211 { 0, NULL}
212 };
213
214 static struct tok bgp_notify_minor_update_values[] = {
215 { 1, "Malformed Attribute List"},
216 { 2, "Unrecognized Well-known Attribute"},
217 { 3, "Missing Well-known Attribute"},
218 { 4, "Attribute Flags Error"},
219 { 5, "Attribute Length Error"},
220 { 6, "Invalid ORIGIN Attribute"},
221 { 7, "AS Routing Loop"},
222 { 8, "Invalid NEXT_HOP Attribute"},
223 { 9, "Optional Attribute Error"},
224 { 10, "Invalid Network Field"},
225 { 11, "Malformed AS_PATH"},
226 { 0, NULL}
227 };
228
229 static struct tok bgp_origin_values[] = {
230 { 0, "IGP"},
231 { 1, "EGP"},
232 { 2, "Incomplete"},
233 { 0, NULL}
234 };
235
236 /* Subsequent address family identifier, RFC2283 section 7 */
237 #define SAFNUM_RES 0
238 #define SAFNUM_UNICAST 1
239 #define SAFNUM_MULTICAST 2
240 #define SAFNUM_UNIMULTICAST 3
241 /* labeled BGP RFC3107 */
242 #define SAFNUM_LABUNICAST 4
243 /* Section 4.3.4 of draft-rosen-rfc2547bis-03.txt */
244 #define SAFNUM_VPNUNICAST 128
245 #define SAFNUM_VPNMULTICAST 129
246 #define SAFNUM_VPNUNIMULTICAST 130
247
248 static struct tok bgp_safi_values[] = {
249 { SAFNUM_RES, "Reserved"},
250 { SAFNUM_UNICAST, "Unicast"},
251 { SAFNUM_MULTICAST, "Multicast"},
252 { SAFNUM_UNIMULTICAST, "Unicast+Multicast"},
253 { SAFNUM_LABUNICAST, "labeled Unicast"},
254 { SAFNUM_VPNUNICAST, "labeled VPN Unicast"},
255 { SAFNUM_VPNMULTICAST, "labeled VPN Multicast"},
256 { SAFNUM_VPNUNIMULTICAST, "labeled VPN Unicast+Multicast"},
257 { 0, NULL }
258 };
259
260 /* well-known community */
261 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
262 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
263 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
264
265 /* RFC1700 address family numbers */
266 #define AFNUM_INET 1
267 #define AFNUM_INET6 2
268 #define AFNUM_NSAP 3
269 #define AFNUM_HDLC 4
270 #define AFNUM_BBN1822 5
271 #define AFNUM_802 6
272 #define AFNUM_E163 7
273 #define AFNUM_E164 8
274 #define AFNUM_F69 9
275 #define AFNUM_X121 10
276 #define AFNUM_IPX 11
277 #define AFNUM_ATALK 12
278 #define AFNUM_DECNET 13
279 #define AFNUM_BANYAN 14
280 #define AFNUM_E164NSAP 15
281 /* draft-kompella-ppvpn-l2vpn */
282 #define AFNUM_L2VPN 196 /* still to be approved by IANA */
283
284 static struct tok bgp_afi_values[] = {
285 { 0, "Reserved"},
286 { AFNUM_INET, "IPv4"},
287 { AFNUM_INET6, "IPv6"},
288 { AFNUM_NSAP, "NSAP"},
289 { AFNUM_HDLC, "HDLC"},
290 { AFNUM_BBN1822, "BBN 1822"},
291 { AFNUM_802, "802"},
292 { AFNUM_E163, "E.163"},
293 { AFNUM_E164, "E.164"},
294 { AFNUM_F69, "F.69"},
295 { AFNUM_X121, "X.121"},
296 { AFNUM_IPX, "Novell IPX"},
297 { AFNUM_ATALK, "Appletalk"},
298 { AFNUM_DECNET, "Decnet IV"},
299 { AFNUM_BANYAN, "Banyan Vines"},
300 { AFNUM_E164NSAP, "E.164 with NSAP subaddress"},
301 { AFNUM_L2VPN, "Layer-2 VPN"},
302 { 0, NULL},
303 };
304
305 static struct tok bgp_extd_comm_subtype_values[] = {
306 { 2, "target"},
307 { 3, "origin"},
308 { 4, "link-BW"},
309 { 0, NULL},
310 };
311
312 static int
313 decode_prefix4(const u_char *pd, char *buf, u_int buflen)
314 {
315 struct in_addr addr;
316 u_int plen;
317
318 plen = pd[0];
319 if (plen < 0 || 32 < plen)
320 return -1;
321
322 memset(&addr, 0, sizeof(addr));
323 memcpy(&addr, &pd[1], (plen + 7) / 8);
324 if (plen % 8) {
325 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
326 ((0xff00 >> (plen % 8)) & 0xff);
327 }
328 snprintf(buf, buflen, "%s/%d", getname((u_char *)&addr), plen);
329 return 1 + (plen + 7) / 8;
330 }
331
332 static int
333 decode_labeled_prefix4(const u_char *pd, char *buf, u_int buflen)
334 {
335 struct in_addr addr;
336 u_int plen;
337
338 plen = pd[0]; /* get prefix length */
339
340 /* this is one of the weirdnesses of rfc3107
341 the label length (actually the label + COS bits)
342 is added of the prefix length;
343 we also do only read out just one label -
344 there is no real application for advertisment of
345 stacked labels in a a single BGP message
346 */
347
348 plen-=24; /* adjust prefixlen - labellength */
349
350 if (plen < 0 || 32 < plen)
351 return -1;
352
353 memset(&addr, 0, sizeof(addr));
354 memcpy(&addr, &pd[4], (plen + 7) / 8);
355 if (plen % 8) {
356 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
357 ((0xff00 >> (plen % 8)) & 0xff);
358 }
359 /* the label may get offsetted by 4 bits so lets shift it right */
360 snprintf(buf, buflen, "%s/%d label:%u %s",
361 getname((u_char *)&addr),
362 plen,
363 EXTRACT_24BITS(pd+1)>>4,
364 ((pd[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
365
366 return 4 + (plen + 7) / 8;
367 }
368
369 #ifdef INET6
370 static int
371 decode_prefix6(const u_char *pd, char *buf, u_int buflen)
372 {
373 struct in6_addr addr;
374 u_int plen;
375
376 plen = pd[0];
377 if (plen < 0 || 128 < plen)
378 return -1;
379
380 memset(&addr, 0, sizeof(addr));
381 memcpy(&addr, &pd[1], (plen + 7) / 8);
382 if (plen % 8) {
383 addr.s6_addr[(plen + 7) / 8 - 1] &=
384 ((0xff00 >> (plen % 8)) & 0xff);
385 }
386 snprintf(buf, buflen, "%s/%d", getname6((u_char *)&addr), plen);
387 return 1 + (plen + 7) / 8;
388 }
389 #endif
390
391 static void
392 bgp_attr_print(const struct bgp_attr *attr, const u_char *dat, int len)
393 {
394 int i;
395 u_int16_t af;
396 u_int8_t safi, snpa;
397 int advance;
398 int tlen;
399 const u_char *p;
400 char buf[MAXHOSTNAMELEN + 100];
401
402 p = dat;
403 tlen=len;
404
405 switch (attr->bgpa_type) {
406 case BGPTYPE_ORIGIN:
407 if (len != 1)
408 printf("invalid len");
409 else
410 printf("%s", tok2str(bgp_origin_values, "Unknown Origin Typecode", p[0]));
411 break;
412 case BGPTYPE_AS_PATH:
413 if (len % 2) {
414 printf("invalid len");
415 break;
416 }
417 if (!len) {
418 printf("empty");
419 break;
420 }
421 while (p < dat + len) {
422 /*
423 * under RFC1965, p[0] means:
424 * 1: AS_SET 2: AS_SEQUENCE
425 * 3: AS_CONFED_SET 4: AS_CONFED_SEQUENCE
426 */
427 if (p[0] == 3 || p[0] == 4)
428 printf("confed");
429 printf("%s", (p[0] & 1) ? "{" : "");
430 for (i = 0; i < p[1] * 2; i += 2) {
431 printf("%s%u", i == 0 ? "" : " ",
432 EXTRACT_16BITS(&p[2 + i]));
433 }
434 printf("%s", (p[0] & 1) ? "}" : "");
435 p += 2 + p[1] * 2;
436 }
437 break;
438 case BGPTYPE_NEXT_HOP:
439 if (len != 4)
440 printf("invalid len");
441 else
442 printf("%s", getname(p));
443 break;
444 case BGPTYPE_MULTI_EXIT_DISC:
445 case BGPTYPE_LOCAL_PREF:
446 if (len != 4)
447 printf("invalid len");
448 else
449 printf("%u", EXTRACT_32BITS(p));
450 break;
451 case BGPTYPE_ATOMIC_AGGREGATE:
452 if (len != 0)
453 printf("invalid len");
454 break;
455 case BGPTYPE_AGGREGATOR:
456 if (len != 6) {
457 printf("invalid len");
458 break;
459 }
460 printf(" AS #%u, origin %s", EXTRACT_16BITS(p),
461 getname(p + 2));
462 break;
463 case BGPTYPE_COMMUNITIES:
464 if (len % 4) {
465 printf("invalid len");
466 break;
467 }
468 while (tlen>0) {
469 u_int32_t comm;
470 comm = EXTRACT_32BITS(p);
471 switch (comm) {
472 case BGP_COMMUNITY_NO_EXPORT:
473 printf(" NO_EXPORT");
474 break;
475 case BGP_COMMUNITY_NO_ADVERT:
476 printf(" NO_ADVERTISE");
477 break;
478 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED:
479 printf(" NO_EXPORT_SUBCONFED");
480 break;
481 default:
482 printf("%u:%u%s",
483 (comm >> 16) & 0xffff,
484 comm & 0xffff,
485 (tlen>4) ? ", " : "");
486 break;
487 }
488 tlen -=4;
489 p +=4;
490 }
491 break;
492 case BGPTYPE_ORIGINATOR_ID:
493 if (len != 4) {
494 printf("invalid len");
495 break;
496 }
497 printf("%s",getname(p));
498 break;
499 case BGPTYPE_CLUSTER_LIST:
500 while (tlen>0) {
501 printf("%s%s",
502 getname(p),
503 (tlen>4) ? ", " : "");
504 tlen -=4;
505 p +=4;
506 }
507 break;
508 case BGPTYPE_MP_REACH_NLRI:
509 af = EXTRACT_16BITS(p);
510 safi = p[2];
511
512 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
513 tok2str(bgp_afi_values, "Unknown AFI", af),
514 af,
515 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
516 tok2str(bgp_safi_values, "Unknown SAFI", safi),
517 safi);
518
519 if (af == AFNUM_INET)
520 ;
521 #ifdef INET6
522 else if (af == AFNUM_INET6)
523 ;
524 #endif
525 else {
526 printf("\n\t no AFI %u decoder",af);
527 print_unknown_data(p,"\n\t ",tlen);
528 break;
529 }
530
531 p +=3;
532 tlen = p[0];
533 if (tlen) {
534 printf("\n\t nexthop: ");
535 i = 0;
536 while (i < tlen) {
537 switch (af) {
538 case AFNUM_INET:
539 switch(safi) {
540 case SAFNUM_UNICAST:
541 case SAFNUM_MULTICAST:
542 case SAFNUM_UNIMULTICAST:
543 case SAFNUM_LABUNICAST:
544 printf("%s", getname(p + 1 + i));
545 i += sizeof(struct in_addr);
546 break;
547 default:
548 printf("no SAFI %u decoder",safi);
549 print_unknown_data(p,"\n\t ",tlen);
550 i = tlen;
551 break;
552 }
553 break;
554 #ifdef INET6
555 case AFNUM_INET6:
556 switch(safi) {
557 case SAFNUM_UNICAST:
558 case SAFNUM_MULTICAST:
559 case SAFNUM_UNIMULTICAST:
560 case SAFNUM_LABUNICAST:
561 printf("%s", getname6(p + 1 + i));
562 i += sizeof(struct in6_addr);
563 break;
564 default:
565 printf("no SAFI %u decoder",safi);
566 print_unknown_data(p,"\n\t ",tlen);
567 i = tlen;
568 break;
569 }
570 #endif
571 default:
572 printf("no AFI %u decoder",af);
573 print_unknown_data(p,"\n\t ",tlen);
574 i = tlen; /*exit loop*/
575 break;
576 }
577 }
578 }
579 p += 1 + tlen;
580
581 snpa = p[0];
582 p++;
583 if (snpa) {
584 printf("\n\t %u SNPA", snpa);
585 for (/*nothing*/; snpa > 0; snpa--) {
586 printf("\n\t %d bytes", p[0]);
587 p += p[0] + 1;
588 }
589 } else {
590 printf(", no SNPA");
591 }
592
593 while (len - (p - dat) > 0) {
594 switch (af) {
595 case AFNUM_INET:
596 switch (safi) {
597 case SAFNUM_UNICAST:
598 case SAFNUM_MULTICAST:
599 case SAFNUM_UNIMULTICAST:
600 advance = decode_prefix4(p, buf, sizeof(buf));
601 printf("\n\t %s", buf);
602 break;
603 case SAFNUM_LABUNICAST:
604 advance = decode_labeled_prefix4(p, buf, sizeof(buf));
605 printf("\n\t %s", buf);
606 break;
607 default:
608 printf("\n\t no SAFI %u decoder",safi);
609 print_unknown_data(p-3,"\n\t ",tlen);
610 advance = 0;
611 p = dat + len;
612 break;
613 }
614 break;
615 #ifdef INET6
616 case AFNUM_INET6:
617 switch (safi) {
618 case SAFNUM_UNICAST:
619 case SAFNUM_MULTICAST:
620 case SAFNUM_UNIMULTICAST:
621 advance = decode_prefix6(p, buf, sizeof(buf));
622 printf("\n\t %s", buf);
623 break;
624 default:
625 printf("\n\t no SAFI %u decoder ",safi);
626 print_unknown_data(p-3,"\n\t ",tlen);
627 advance = 0;
628 p = dat + len;
629 break;
630 }
631 break;
632 #endif
633 default:
634 printf("\n\t no AFI %u decoder ",af);
635 print_unknown_data(p-3,"\n\t ",tlen);
636 advance = 0;
637 p = dat + len;
638 break;
639 }
640
641 p += advance;
642 }
643
644 break;
645
646 case BGPTYPE_MP_UNREACH_NLRI:
647 af = EXTRACT_16BITS(p);
648 safi = p[2];
649
650 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
651 tok2str(bgp_afi_values, "Unknown AFI", af),
652 af,
653 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
654 tok2str(bgp_safi_values, "Unknown SAFI", safi),
655 safi);
656
657 p += 3;
658
659 while (len - (p - dat) > 0) {
660 switch (af) {
661 case AFNUM_INET:
662 switch (safi) {
663 case SAFNUM_UNICAST:
664 case SAFNUM_MULTICAST:
665 case SAFNUM_UNIMULTICAST:
666 advance = decode_prefix4(p, buf, sizeof(buf));
667 printf("\n\t %s", buf);
668 break;
669 case SAFNUM_LABUNICAST:
670 advance = decode_labeled_prefix4(p, buf, sizeof(buf));
671 printf("\n\t %s", buf);
672 break;
673 default:
674 printf("\n\t no SAFI %u decoder",safi);
675 print_unknown_data(p-3,"\n\t ",tlen);
676 advance = 0;
677 p = dat + len;
678 break;
679 }
680 break;
681
682 #ifdef INET6
683 case AFNUM_INET6:
684 switch (safi) {
685 case SAFNUM_UNICAST:
686 case SAFNUM_MULTICAST:
687 case SAFNUM_UNIMULTICAST:
688 advance = decode_prefix6(p, buf, sizeof(buf));
689 printf("\n\t %s", buf);
690 break;
691 default:
692 printf("\n\t no SAFI %u decoder",safi);
693 print_unknown_data(p-3,"\n\t ",tlen);
694 advance = 0;
695 p = dat + len;
696 break;
697 }
698 break;
699 #endif
700 default:
701 printf("\n\t no AFI %u decoder",af);
702 print_unknown_data(p-3,"\n\t ",tlen);
703 advance = 0;
704 p = dat + len;
705 break;
706 }
707
708 p += advance;
709 }
710 break;
711 case BGPTYPE_EXTD_COMMUNITIES:
712 if (len % 8) {
713 printf("invalid len");
714 break;
715 }
716 while (tlen>0) {
717 u_int8_t extd_comm,extd_comm_type,extd_comm_subtype;
718 extd_comm=*p;
719 extd_comm_type=extd_comm&0x3f;
720 extd_comm_subtype=*(p+1);
721 switch(extd_comm_type) {
722 case 0:
723 printf("%s%s%s:%u:%s%s",
724 (extd_comm&0x80) ? "vendor-specific: " : "",
725 (extd_comm&0x40) ? "non-transitive:" : "",
726 tok2str(bgp_extd_comm_subtype_values,
727 "unknown",
728 extd_comm_subtype&0x3f),
729 EXTRACT_16BITS(p+2),
730 getname(p+4),
731 (tlen>8) ? ", " : "");
732 break;
733 case 1:
734 printf("%s%s%s:%s:%u%s",
735 (extd_comm&0x80) ? "vendor-specific: " : "",
736 (extd_comm&0x40) ? "non-transitive:" : "",
737 tok2str(bgp_extd_comm_subtype_values,
738 "unknown",
739 extd_comm_subtype&0x3f),
740 getname(p+2),
741 EXTRACT_16BITS(p+6),
742 (tlen>8) ? ", " : "");
743 break;
744 case 2:
745 printf("%s%s%s:%u:%u%s",
746 (extd_comm&0x80) ? "vendor-specific: " : "",
747 (extd_comm&0x40) ? "non-transitive:" : "",
748 tok2str(bgp_extd_comm_subtype_values,
749 "unknown",
750 extd_comm_subtype&0x3f),
751 EXTRACT_32BITS(p+2),
752 EXTRACT_16BITS(p+6),
753 (tlen>8) ? ", " : "");
754 break;
755
756 default:
757 printf("\n\t no typecode %u decoder",
758 extd_comm_type);
759 print_unknown_data(p,"\n\t ",8);
760 break;
761 }
762 tlen -=8;
763 p +=8;
764 }
765 break;
766
767 default:
768 printf("\n\t no Attribute %u decoder",attr->bgpa_type); /* we have no decoder for the attribute */
769 print_unknown_data(p,"\n\t ",tlen);
770 break;
771 }
772 }
773
774 static void
775 bgp_open_print(const u_char *dat, int length)
776 {
777 struct bgp_open bgpo;
778 struct bgp_opt bgpopt;
779 int hlen;
780 const u_char *opt;
781 int i,cap_type,cap_len;
782
783 TCHECK2(dat[0], BGP_OPEN_SIZE);
784 memcpy(&bgpo, dat, BGP_OPEN_SIZE);
785 hlen = ntohs(bgpo.bgpo_len);
786
787 printf("\n\t Version %d, ", bgpo.bgpo_version);
788 printf("my AS %u, ", ntohs(bgpo.bgpo_myas));
789 printf("Holdtime %us, ", ntohs(bgpo.bgpo_holdtime));
790 printf("ID %s", getname((u_char *)&bgpo.bgpo_id));
791 printf("\n\t Optional parameters, length %u", bgpo.bgpo_optlen);
792
793 /* ugly! */
794 opt = &((const struct bgp_open *)dat)->bgpo_optlen;
795 opt++;
796
797 i = 0;
798 while (i < bgpo.bgpo_optlen) {
799 TCHECK2(opt[i], BGP_OPT_SIZE);
800 memcpy(&bgpopt, &opt[i], BGP_OPT_SIZE);
801 if (i + 2 + bgpopt.bgpopt_len > bgpo.bgpo_optlen) {
802 printf("\n\t Option %d, length %d", bgpopt.bgpopt_type, bgpopt.bgpopt_len);
803 break;
804 }
805
806 printf("\n\t Option %s (%u), length %d",
807 tok2str(bgp_opt_values,"Unknown", bgpopt.bgpopt_type),
808 bgpopt.bgpopt_type,
809 bgpopt.bgpopt_len);
810
811 /* now lets decode the options we know*/
812 switch(bgpopt.bgpopt_type) {
813 case BGP_OPT_CAP:
814 cap_type=opt[i+BGP_OPT_SIZE];
815 cap_len=opt[i+BGP_OPT_SIZE+1];
816 printf("\n\t %s, length %u",
817 tok2str(bgp_capcode_values,"Unknown", cap_type),
818 cap_len);
819 switch(cap_type) {
820 case BGP_CAPCODE_MP:
821 printf("\n\t\tAFI %s (%u), SAFI %s (%u)",
822 tok2str(bgp_afi_values,"Unknown", EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+2)),
823 EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+2),
824 tok2str(bgp_safi_values,"Unknown", opt[i+BGP_OPT_SIZE+5]),
825 opt[i+BGP_OPT_SIZE+5]);
826 break;
827 case BGP_CAPCODE_RR:
828 case BGP_CAPCODE_RR_CISCO:
829 break;
830 default:
831 printf("\n\t\tno decoder for Capability %u",
832 cap_type);
833 break;
834 }
835 break;
836 case BGP_OPT_AUTH:
837 default:
838 printf("\n\t no decoder for option %u",
839 bgpopt.bgpopt_type);
840 break;
841 }
842
843 i += BGP_OPT_SIZE + bgpopt.bgpopt_len;
844 }
845 return;
846 trunc:
847 printf("[|BGP]");
848 }
849
850 static void
851 bgp_update_print(const u_char *dat, int length)
852 {
853 struct bgp bgp;
854 struct bgp_attr bgpa;
855 int hlen;
856 const u_char *p;
857 int len;
858 int i;
859
860 TCHECK2(dat[0], BGP_SIZE);
861 memcpy(&bgp, dat, BGP_SIZE);
862 hlen = ntohs(bgp.bgp_len);
863 p = dat + BGP_SIZE; /*XXX*/
864
865 /* Unfeasible routes */
866 len = EXTRACT_16BITS(p);
867 if (len) {
868 /*
869 * Without keeping state from the original NLRI message,
870 * it's not possible to tell if this a v4 or v6 route,
871 * so only try to decode it if we're not v6 enabled.
872 */
873 #ifdef INET6
874 printf("\n\t Withdrawn routes: %d bytes", len);
875 #else
876 char buf[MAXHOSTNAMELEN + 100];
877
878 TCHECK2(p[2], len);
879 i = 2;
880
881 printf("\n\t Withdrawn routes:");
882
883 while(i < 2 + len) {
884 i += decode_prefix4(&p[i], buf, sizeof(buf));
885 printf("\n\t %s", buf);
886 }
887 #endif
888 }
889 p += 2 + len;
890
891 TCHECK2(p[0], 2);
892 len = EXTRACT_16BITS(p);
893 if (len) {
894 /* do something more useful!*/
895 i = 2;
896 while (i < 2 + len) {
897 int alen, aoff;
898
899 TCHECK2(p[i], sizeof(bgpa));
900 memcpy(&bgpa, &p[i], sizeof(bgpa));
901 alen = bgp_attr_len(&bgpa);
902 aoff = bgp_attr_off(&bgpa);
903
904 printf("\n\t %s (%u), length: %u",
905 tok2str(bgp_attr_values, "Unknown Attribute", bgpa.bgpa_type),
906 bgpa.bgpa_type,
907 alen);
908
909 if (bgpa.bgpa_flags) {
910 printf(", flags [%s%s%s%s",
911 bgpa.bgpa_flags & 0x80 ? "O" : "",
912 bgpa.bgpa_flags & 0x40 ? "T" : "",
913 bgpa.bgpa_flags & 0x20 ? "P" : "",
914 bgpa.bgpa_flags & 0x10 ? "E" : "");
915 if (bgpa.bgpa_flags & 0xf)
916 printf("+%x", bgpa.bgpa_flags & 0xf);
917 printf("]: ");
918 }
919 bgp_attr_print(&bgpa, &p[i + aoff], alen);
920 i += aoff + alen;
921 }
922 }
923 p += 2 + len;
924
925 if (dat + length > p) {
926 printf("\n\t Updated routes:");
927 while (dat + length > p) {
928 char buf[MAXHOSTNAMELEN + 100];
929 i = decode_prefix4(p, buf, sizeof(buf));
930 printf("\n\t %s", buf);
931 if (i < 0)
932 break;
933 p += i;
934 }
935 }
936 return;
937 trunc:
938 printf("[|BGP]");
939 }
940
941 static void
942 bgp_notification_print(const u_char *dat, int length)
943 {
944 struct bgp_notification bgpn;
945 int hlen;
946
947 TCHECK2(dat[0], BGP_NOTIFICATION_SIZE);
948 memcpy(&bgpn, dat, BGP_NOTIFICATION_SIZE);
949 hlen = ntohs(bgpn.bgpn_len);
950
951 printf(", Error - %s", tok2str(bgp_notify_major_values, "Unknown", bgpn.bgpn_major));
952
953 switch (bgpn.bgpn_major) {
954
955 case BGP_NOTIFY_MAJOR_MSG:
956 printf(" subcode %s", tok2str(bgp_notify_minor_msg_values, "Unknown", bgpn.bgpn_minor));
957 break;
958 case BGP_NOTIFY_MAJOR_OPEN:
959 printf(" subcode %s", tok2str(bgp_notify_minor_open_values, "Unknown", bgpn.bgpn_minor));
960 break;
961 case BGP_NOTIFY_MAJOR_UPDATE:
962 printf(" subcode %s", tok2str(bgp_notify_minor_update_values, "Unknown", bgpn.bgpn_minor));
963 break;
964 default:
965 break;
966 }
967
968 return;
969 trunc:
970 printf("[|BGP]");
971 }
972
973 static void
974 bgp_header_print(const u_char *dat, int length)
975 {
976 struct bgp bgp;
977
978 TCHECK2(dat[0], BGP_SIZE);
979 memcpy(&bgp, dat, BGP_SIZE);
980 printf("\n\t%s Message (%u), length: %u ",
981 tok2str(bgp_msg_values, "Unknown", bgp.bgp_type),
982 bgp.bgp_type,
983 length);
984
985 switch (bgp.bgp_type) {
986 case BGP_OPEN:
987 bgp_open_print(dat, length);
988 break;
989 case BGP_UPDATE:
990 bgp_update_print(dat, length);
991 break;
992 case BGP_NOTIFICATION:
993 bgp_notification_print(dat, length);
994 break;
995 case BGP_KEEPALIVE:
996 break;
997 default:
998 /* we have no decoder for the BGP message */
999 printf("\n\t no Message %u decoder",bgp.bgp_type);
1000 print_unknown_data(dat,"\n\t ",length);
1001 break;
1002 }
1003 return;
1004 trunc:
1005 printf("[|BGP]");
1006 }
1007
1008 void
1009 bgp_print(const u_char *dat, int length)
1010 {
1011 const u_char *p;
1012 const u_char *ep;
1013 const u_char *start;
1014 const u_char marker[] = {
1015 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1016 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1017 };
1018 struct bgp bgp;
1019 u_int16_t hlen;
1020
1021 ep = dat + length;
1022 if (snapend < dat + length)
1023 ep = snapend;
1024
1025 printf(": BGP");
1026
1027 p = dat;
1028 start = p;
1029 while (p < snapend) {
1030 if (!TTEST2(p[0], 1))
1031 break;
1032 if (p[0] != 0xff) {
1033 p++;
1034 continue;
1035 }
1036
1037 if (!TTEST2(p[0], sizeof(marker)))
1038 break;
1039 if (memcmp(p, marker, sizeof(marker)) != 0) {
1040 p++;
1041 continue;
1042 }
1043
1044 /* found BGP header */
1045 TCHECK2(p[0], BGP_SIZE); /*XXX*/
1046 memcpy(&bgp, p, BGP_SIZE);
1047
1048 if (start != p)
1049 printf(" [|BGP]");
1050
1051 hlen = ntohs(bgp.bgp_len);
1052
1053 if (TTEST2(p[0], hlen)) {
1054 bgp_header_print(p, hlen);
1055 p += hlen;
1056 start = p;
1057 } else {
1058 printf("[|BGP %s]", tok2str(bgp_msg_values, "Unknown Message Type",bgp.bgp_type));
1059 break;
1060 }
1061 }
1062
1063 return;
1064
1065 trunc:
1066 printf(" [|BGP]");
1067 }
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078