]> The Tcpdump Group git mirrors - tcpdump/blob - print-bgp.c
Get rid of unneeded includes of <netinet/in_systm.h> and <netinet/ip.h>.
[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.29 2002-05-24 17:49:29 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
67 struct bgp_open {
68 u_int8_t bgpo_marker[16];
69 u_int16_t bgpo_len;
70 u_int8_t bgpo_type;
71 u_int8_t bgpo_version;
72 u_int16_t bgpo_myas;
73 u_int16_t bgpo_holdtime;
74 u_int32_t bgpo_id;
75 u_int8_t bgpo_optlen;
76 /* options should follow */
77 };
78 #define BGP_OPEN_SIZE 29 /* unaligned */
79
80 struct bgp_opt {
81 u_int8_t bgpopt_type;
82 u_int8_t bgpopt_len;
83 /* variable length */
84 };
85 #define BGP_OPT_SIZE 2 /* some compilers may pad to 4 bytes */
86
87 struct bgp_notification {
88 u_int8_t bgpn_marker[16];
89 u_int16_t bgpn_len;
90 u_int8_t bgpn_type;
91 u_int8_t bgpn_major;
92 u_int8_t bgpn_minor;
93 /* data should follow */
94 };
95 #define BGP_NOTIFICATION_SIZE 21 /* unaligned */
96
97 struct bgp_attr {
98 u_int8_t bgpa_flags;
99 u_int8_t bgpa_type;
100 union {
101 u_int8_t len;
102 u_int16_t elen;
103 } bgpa_len;
104 #define bgp_attr_len(p) \
105 (((p)->bgpa_flags & 0x10) ? \
106 ntohs((p)->bgpa_len.elen) : (p)->bgpa_len.len)
107 #define bgp_attr_off(p) \
108 (((p)->bgpa_flags & 0x10) ? 4 : 3)
109 };
110
111 #define BGPTYPE_ORIGIN 1
112 #define BGPTYPE_AS_PATH 2
113 #define BGPTYPE_NEXT_HOP 3
114 #define BGPTYPE_MULTI_EXIT_DISC 4
115 #define BGPTYPE_LOCAL_PREF 5
116 #define BGPTYPE_ATOMIC_AGGREGATE 6
117 #define BGPTYPE_AGGREGATOR 7
118 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
119 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC1998 */
120 #define BGPTYPE_CLUSTER_LIST 10 /* RFC1998 */
121 #define BGPTYPE_DPA 11 /* work in progress */
122 #define BGPTYPE_ADVERTISERS 12 /* RFC1863 */
123 #define BGPTYPE_RCID_PATH 13 /* RFC1863 */
124 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */
125 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */
126
127
128 static const char *bgptype[] = {
129 NULL, "OPEN", "UPDATE", "NOTIFICATION", "KEEPALIVE",
130 };
131 #define bgp_type(x) num_or_str(bgptype, sizeof(bgptype)/sizeof(bgptype[0]), (x))
132
133 static const char *bgpopt_type[] = {
134 NULL, "Authentication Information", "Capabilities Advertisement",
135 };
136 #define bgp_opttype(x) \
137 num_or_str(bgpopt_type, sizeof(bgpopt_type)/sizeof(bgpopt_type[0]), (x))
138
139 static const char *bgpnotify_major[] = {
140 NULL, "Message Header Error",
141 "OPEN Message Error", "UPDATE Message Error",
142 "Hold Timer Expired", "Finite State Machine Error",
143 "Cease",
144 };
145 #define bgp_notify_major(x) \
146 num_or_str(bgpnotify_major, \
147 sizeof(bgpnotify_major)/sizeof(bgpnotify_major[0]), (x))
148
149 static const char *bgpnotify_minor_1[] = {
150 NULL, "Connection Not Synchronized",
151 "Bad Message Length", "Bad Message Type",
152 };
153
154 static const char *bgpnotify_minor_2[] = {
155 NULL, "Unsupported Version Number",
156 "Bad Peer AS", "Bad BGP Identifier",
157 "Unsupported Optional Parameter", "Authentication Failure",
158 "Unacceptable Hold Time",
159 };
160
161 static const char *bgpnotify_minor_3[] = {
162 NULL, "Malformed Attribute List",
163 "Unrecognized Well-known Attribute", "Missing Well-known Attribute",
164 "Attribute Flags Error", "Attribute Length Error",
165 "Invalid ORIGIN Attribute", "AS Routing Loop",
166 "Invalid NEXT_HOP Attribute", "Optional Attribute Error",
167 "Invalid Network Field", "Malformed AS_PATH",
168 };
169
170 static const char **bgpnotify_minor[] = {
171 NULL, bgpnotify_minor_1, bgpnotify_minor_2, bgpnotify_minor_3,
172 };
173 static const int bgpnotify_minor_siz[] = {
174 0, sizeof(bgpnotify_minor_1)/sizeof(bgpnotify_minor_1[0]),
175 sizeof(bgpnotify_minor_2)/sizeof(bgpnotify_minor_2[0]),
176 sizeof(bgpnotify_minor_3)/sizeof(bgpnotify_minor_3[0]),
177 };
178
179 static const char *bgpattr_origin[] = {
180 "IGP", "EGP", "INCOMPLETE",
181 };
182 #define bgp_attr_origin(x) \
183 num_or_str(bgpattr_origin, \
184 sizeof(bgpattr_origin)/sizeof(bgpattr_origin[0]), (x))
185
186 static const char *bgpattr_type[] = {
187 NULL, "ORIGIN", "AS_PATH", "NEXT_HOP",
188 "MULTI_EXIT_DISC", "LOCAL_PREF", "ATOMIC_AGGREGATE", "AGGREGATOR",
189 "COMMUNITIES", "ORIGINATOR_ID", "CLUSTER_LIST", "DPA",
190 "ADVERTISERS", "RCID_PATH", "MP_REACH_NLRI", "MP_UNREACH_NLRI",
191 };
192 #define bgp_attr_type(x) \
193 num_or_str(bgpattr_type, \
194 sizeof(bgpattr_type)/sizeof(bgpattr_type[0]), (x))
195
196 /* Subsequent address family identifier, RFC2283 section 7 */
197 #define SAFNUM_RES 0
198 #define SAFNUM_UNICAST 1
199 #define SAFNUM_MULTICAST 2
200 #define SAFNUM_UNIMULTICAST 3
201 /* labeled BGP RFC3107 */
202 #define SAFNUM_LABUNICAST 4
203
204 static const char *bgpattr_nlri_safi[] = {
205 "Reserved", "Unicast", "Multicast", "Unicast+Multicast", "labeled Unicast"
206 };
207 #define bgp_attr_nlri_safi(x) \
208 num_or_str(bgpattr_nlri_safi, \
209 sizeof(bgpattr_nlri_safi)/sizeof(bgpattr_nlri_safi[0]), (x))
210
211 /* well-known community */
212 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
213 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
214 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
215
216 /* RFC1700 address family numbers */
217 #define AFNUM_INET 1
218 #define AFNUM_INET6 2
219 #define AFNUM_NSAP 3
220 #define AFNUM_HDLC 4
221 #define AFNUM_BBN1822 5
222 #define AFNUM_802 6
223 #define AFNUM_E163 7
224 #define AFNUM_E164 8
225 #define AFNUM_F69 9
226 #define AFNUM_X121 10
227 #define AFNUM_IPX 11
228 #define AFNUM_ATALK 12
229 #define AFNUM_DECNET 13
230 #define AFNUM_BANYAN 14
231 #define AFNUM_E164NSAP 15
232
233 static const char *afnumber[] = {
234 "Reserved", "IPv4", "IPv6", "NSAP", "HDLC",
235 "BBN 1822", "802", "E.163", "E.164", "F.69",
236 "X.121", "IPX", "Appletalk", "Decnet IV", "Banyan Vines",
237 "E.164 with NSAP subaddress",
238 };
239 #define af_name(x) \
240 (((x) == 65535) ? afnumber[0] : \
241 num_or_str(afnumber, \
242 sizeof(afnumber)/sizeof(afnumber[0]), (x)))
243
244
245 static const char *
246 num_or_str(const char **table, size_t siz, int value)
247 {
248 static char buf[20];
249 if (value < 0 || siz <= value || table[value] == NULL) {
250 snprintf(buf, sizeof(buf), "#%d", value);
251 return buf;
252 } else
253 return table[value];
254 }
255
256 static const char *
257 bgp_notify_minor(int major, int minor)
258 {
259 static const char **table;
260 int siz;
261 static char buf[20];
262 const char *p;
263
264 if (0 <= major
265 && major < sizeof(bgpnotify_minor)/sizeof(bgpnotify_minor[0])
266 && bgpnotify_minor[major]) {
267 table = bgpnotify_minor[major];
268 siz = bgpnotify_minor_siz[major];
269 if (0 <= minor && minor < siz && table[minor])
270 p = table[minor];
271 else
272 p = NULL;
273 } else
274 p = NULL;
275 if (p == NULL) {
276 snprintf(buf, sizeof(buf), "#%d", minor);
277 return buf;
278 } else
279 return p;
280 }
281
282 static int
283 decode_prefix4(const u_char *pd, char *buf, u_int buflen)
284 {
285 struct in_addr addr;
286 u_int plen;
287
288 plen = pd[0];
289 if (plen < 0 || 32 < plen)
290 return -1;
291
292 memset(&addr, 0, sizeof(addr));
293 memcpy(&addr, &pd[1], (plen + 7) / 8);
294 if (plen % 8) {
295 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
296 ((0xff00 >> (plen % 8)) & 0xff);
297 }
298 snprintf(buf, buflen, "%s/%d", getname((u_char *)&addr), plen);
299 return 1 + (plen + 7) / 8;
300 }
301
302 static int
303 decode_labeled_prefix4(const u_char *pd, char *buf, u_int buflen)
304 {
305 struct in_addr addr;
306 u_int plen;
307
308 plen = pd[0]; /* get prefix length */
309
310 /* this is one of the weirdnesses of rfc3107
311 the label length (actually the label + COS bits)
312 is added of the prefix length;
313 we also do only read out just one label -
314 there is no real application for advertisment of
315 stacked labels in a asingle BGP message
316 */
317
318 plen-=24; /* adjust prefixlen - labellength */
319
320 if (plen < 0 || 32 < plen)
321 return -1;
322
323 memset(&addr, 0, sizeof(addr));
324 memcpy(&addr, &pd[4], (plen + 7) / 8);
325 if (plen % 8) {
326 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
327 ((0xff00 >> (plen % 8)) & 0xff);
328 }
329 /* the label may get offsetted by 4 bits so lets shift it right */
330 snprintf(buf, buflen, "%s/%d label:%u %s",
331 getname((u_char *)&addr),
332 plen,
333 EXTRACT_24BITS(pd+1)>>4,
334 ((pd[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
335
336 return 4 + (plen + 7) / 8;
337 }
338
339 #ifdef INET6
340 static int
341 decode_prefix6(const u_char *pd, char *buf, u_int buflen)
342 {
343 struct in6_addr addr;
344 u_int plen;
345
346 plen = pd[0];
347 if (plen < 0 || 128 < plen)
348 return -1;
349
350 memset(&addr, 0, sizeof(addr));
351 memcpy(&addr, &pd[1], (plen + 7) / 8);
352 if (plen % 8) {
353 addr.s6_addr[(plen + 7) / 8 - 1] &=
354 ((0xff00 >> (plen % 8)) & 0xff);
355 }
356 snprintf(buf, buflen, "%s/%d", getname6((u_char *)&addr), plen);
357 return 1 + (plen + 7) / 8;
358 }
359 #endif
360
361 static void
362 bgp_attr_print(const struct bgp_attr *attr, const u_char *dat, int len)
363 {
364 int i;
365 u_int16_t af;
366 u_int8_t safi, snpa;
367 int advance;
368 int tlen;
369 const u_char *p;
370 char buf[MAXHOSTNAMELEN + 100];
371
372 p = dat;
373
374 switch (attr->bgpa_type) {
375 case BGPTYPE_ORIGIN:
376 if (len != 1)
377 printf(" invalid len");
378 else
379 printf(" %s", bgp_attr_origin(p[0]));
380 break;
381 case BGPTYPE_AS_PATH:
382 if (len % 2) {
383 printf(" invalid len");
384 break;
385 }
386 while (p < dat + len) {
387 /*
388 * under RFC1965, p[0] means:
389 * 1: AS_SET 2: AS_SEQUENCE
390 * 3: AS_CONFED_SET 4: AS_CONFED_SEQUENCE
391 */
392 printf(" ");
393 if (p[0] == 3 || p[0] == 4)
394 printf("confed");
395 printf("%s", (p[0] & 1) ? "{" : "");
396 for (i = 0; i < p[1] * 2; i += 2) {
397 printf("%s%u", i == 0 ? "" : " ",
398 EXTRACT_16BITS(&p[2 + i]));
399 }
400 printf("%s", (p[0] & 1) ? "}" : "");
401 p += 2 + p[1] * 2;
402 }
403 break;
404 case BGPTYPE_NEXT_HOP:
405 if (len != 4)
406 printf(" invalid len");
407 else
408 printf(" %s", getname(p));
409 break;
410 case BGPTYPE_MULTI_EXIT_DISC:
411 case BGPTYPE_LOCAL_PREF:
412 if (len != 4)
413 printf(" invalid len");
414 else
415 printf(" %u", EXTRACT_32BITS(p));
416 break;
417 case BGPTYPE_ATOMIC_AGGREGATE:
418 if (len != 0)
419 printf(" invalid len");
420 break;
421 case BGPTYPE_AGGREGATOR:
422 if (len != 6) {
423 printf(" invalid len");
424 break;
425 }
426 printf(" AS #%u, origin %s", EXTRACT_16BITS(p),
427 getname(p + 2));
428 break;
429 case BGPTYPE_COMMUNITIES:
430 if (len % 4) {
431 printf(" invalid len");
432 break;
433 }
434 for (i = 0; i < len; i += 4) {
435 u_int32_t comm;
436 comm = EXTRACT_32BITS(&p[i]);
437 switch (comm) {
438 case BGP_COMMUNITY_NO_EXPORT:
439 printf(" NO_EXPORT");
440 break;
441 case BGP_COMMUNITY_NO_ADVERT:
442 printf(" NO_ADVERTISE");
443 break;
444 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED:
445 printf(" NO_EXPORT_SUBCONFED");
446 break;
447 default:
448 printf(" (AS #%d value 0x%04x)",
449 (comm >> 16) & 0xffff, comm & 0xffff);
450 break;
451 }
452 }
453 break;
454 case BGPTYPE_MP_REACH_NLRI:
455 af = EXTRACT_16BITS(p);
456 safi = p[2];
457 if (safi >= 128)
458 printf(" %s vendor specific,", af_name(af));
459 else {
460 printf(" AFI %s SAFI %s,", af_name(af),
461 bgp_attr_nlri_safi(safi));
462 }
463 p += 3;
464
465 if (af == AFNUM_INET)
466 ;
467 #ifdef INET6
468 else if (af == AFNUM_INET6)
469 ;
470 #endif
471 else
472 break;
473
474 tlen = p[0];
475 if (tlen) {
476 printf(" nexthop");
477 i = 0;
478 while (i < tlen) {
479 switch (af) {
480 case AFNUM_INET:
481 printf(" %s", getname(p + 1 + i));
482 i += sizeof(struct in_addr);
483 break;
484 #ifdef INET6
485 case AFNUM_INET6:
486 printf(" %s", getname6(p + 1 + i));
487 i += sizeof(struct in6_addr);
488 break;
489 #endif
490 default:
491 printf(" (unknown af)");
492 i = tlen; /*exit loop*/
493 break;
494 }
495 }
496 printf(",");
497 }
498 p += 1 + tlen;
499
500 snpa = p[0];
501 p++;
502 if (snpa) {
503 printf(" %u snpa", snpa);
504 for (/*nothing*/; snpa > 0; snpa--) {
505 printf("(%d bytes)", p[0]);
506 p += p[0] + 1;
507 }
508 printf(",");
509 } else {
510 printf(" no spna,");
511 }
512
513 printf(" NLRI");
514 while (len - (p - dat) > 0) {
515 switch (af) {
516 case AFNUM_INET:
517 if(safi==SAFNUM_LABUNICAST) {
518 advance = decode_labeled_prefix4(p, buf, sizeof(buf));
519 } else {
520 advance = decode_prefix4(p, buf, sizeof(buf));
521 }
522 if (advance<0)
523 break;
524 printf(" %s", buf);
525 break;
526 #ifdef INET6
527 case AFNUM_INET6:
528 advance = decode_prefix6(p, buf, sizeof(buf));
529 printf(" %s", buf);
530 break;
531 #endif
532 default:
533 printf(" (unknown af)");
534 advance = 0;
535 p = dat + len;
536 break;
537 }
538
539 p += advance;
540 }
541
542 break;
543
544 case BGPTYPE_MP_UNREACH_NLRI:
545 af = EXTRACT_16BITS(p);
546 safi = p[2];
547 if (safi >= 128)
548 printf(" %s vendor specific,", af_name(af));
549 else {
550 printf(" AFI %s SAFI %s,", af_name(af),
551 bgp_attr_nlri_safi(safi));
552 }
553 p += 3;
554
555 printf(" Withdraw");
556 while (len - (p - dat) > 0) {
557 switch (af) {
558 case AFNUM_INET:
559 if(safi==SAFNUM_LABUNICAST) {
560 advance = decode_labeled_prefix4(p, buf, sizeof(buf));
561 } else {
562 advance = decode_prefix4(p, buf, sizeof(buf));
563 }
564 if (advance<0)
565 break;
566 printf(" %s", buf);
567 break;
568 #ifdef INET6
569 case AFNUM_INET6:
570 advance = decode_prefix6(p, buf, sizeof(buf));
571 printf(" %s", buf);
572 break;
573 #endif
574 default:
575 printf(" (unknown af)");
576 advance = 0;
577 p = dat + len;
578 break;
579 }
580
581 p += advance;
582 }
583 break;
584 default:
585 break;
586 }
587 }
588
589 static void
590 bgp_open_print(const u_char *dat, int length)
591 {
592 struct bgp_open bgpo;
593 struct bgp_opt bgpopt;
594 int hlen;
595 const u_char *opt;
596 int i;
597
598 TCHECK2(dat[0], BGP_OPEN_SIZE);
599 memcpy(&bgpo, dat, BGP_OPEN_SIZE);
600 hlen = ntohs(bgpo.bgpo_len);
601
602 printf(": Version %d,", bgpo.bgpo_version);
603 printf(" AS #%u,", ntohs(bgpo.bgpo_myas));
604 printf(" Holdtime %u,", ntohs(bgpo.bgpo_holdtime));
605 printf(" ID %s,", getname((u_char *)&bgpo.bgpo_id));
606 printf(" Option length %u", bgpo.bgpo_optlen);
607
608 /* ugly! */
609 opt = &((const struct bgp_open *)dat)->bgpo_optlen;
610 opt++;
611
612 i = 0;
613 while (i < bgpo.bgpo_optlen) {
614 TCHECK2(opt[i], BGP_OPT_SIZE);
615 memcpy(&bgpopt, &opt[i], BGP_OPT_SIZE);
616 if (i + 2 + bgpopt.bgpopt_len > bgpo.bgpo_optlen) {
617 printf(" [|opt %d %d]", bgpopt.bgpopt_len, bgpopt.bgpopt_type);
618 break;
619 }
620
621 printf(" (option %s, len=%d)", bgp_opttype(bgpopt.bgpopt_type),
622 bgpopt.bgpopt_len);
623 i += BGP_OPT_SIZE + bgpopt.bgpopt_len;
624 }
625 return;
626 trunc:
627 printf("[|BGP]");
628 }
629
630 static void
631 bgp_update_print(const u_char *dat, int length)
632 {
633 struct bgp bgp;
634 struct bgp_attr bgpa;
635 int hlen;
636 const u_char *p;
637 int len;
638 int i;
639 int newline;
640
641 TCHECK2(dat[0], BGP_SIZE);
642 memcpy(&bgp, dat, BGP_SIZE);
643 hlen = ntohs(bgp.bgp_len);
644 p = dat + BGP_SIZE; /*XXX*/
645 printf(":");
646
647 /* Unfeasible routes */
648 len = EXTRACT_16BITS(p);
649 if (len) {
650 /*
651 * Without keeping state from the original NLRI message,
652 * it's not possible to tell if this a v4 or v6 route,
653 * so only try to decode it if we're not v6 enabled.
654 */
655 #ifdef INET6
656 printf(" (Withdrawn routes: %d bytes)", len);
657 #else
658 char buf[MAXHOSTNAMELEN + 100];
659
660 TCHECK2(p[2], len);
661 i = 2;
662
663 printf(" (Withdrawn routes:");
664
665 while(i < 2 + len) {
666 i += decode_prefix4(&p[i], buf, sizeof(buf));
667 printf(" %s", buf);
668 }
669 printf(")\n");
670 #endif
671 }
672 p += 2 + len;
673
674 TCHECK2(p[0], 2);
675 len = EXTRACT_16BITS(p);
676 if (len) {
677 /* do something more useful!*/
678 i = 2;
679 printf(" (Path attributes:"); /* ) */
680 newline = 0;
681 while (i < 2 + len) {
682 int alen, aoff;
683
684 TCHECK2(p[i], sizeof(bgpa));
685 memcpy(&bgpa, &p[i], sizeof(bgpa));
686 alen = bgp_attr_len(&bgpa);
687 aoff = bgp_attr_off(&bgpa);
688
689 if (vflag && newline)
690 printf("\n\t\t");
691 else
692 printf(" ");
693 printf("("); /* ) */
694 printf("%s", bgp_attr_type(bgpa.bgpa_type));
695 if (bgpa.bgpa_flags) {
696 printf("[%s%s%s%s",
697 bgpa.bgpa_flags & 0x80 ? "O" : "",
698 bgpa.bgpa_flags & 0x40 ? "T" : "",
699 bgpa.bgpa_flags & 0x20 ? "P" : "",
700 bgpa.bgpa_flags & 0x10 ? "E" : "");
701 if (bgpa.bgpa_flags & 0xf)
702 printf("+%x", bgpa.bgpa_flags & 0xf);
703 printf("]");
704 }
705
706 bgp_attr_print(&bgpa, &p[i + aoff], alen);
707 newline = 1;
708
709 /* ( */
710 printf(")");
711
712 i += aoff + alen;
713 }
714
715 /* ( */
716 printf(")");
717 }
718 p += 2 + len;
719
720 if (len && dat + length > p)
721 printf("\n\t\t");
722 if (dat + length > p) {
723 printf("(NLRI:"); /* ) */
724 while (dat + length > p) {
725 char buf[MAXHOSTNAMELEN + 100];
726 i = decode_prefix4(p, buf, sizeof(buf));
727 printf(" %s", buf);
728 if (i < 0)
729 break;
730 p += i;
731 }
732
733 /* ( */
734 printf(")");
735 }
736 return;
737 trunc:
738 printf("[|BGP]");
739 }
740
741 static void
742 bgp_notification_print(const u_char *dat, int length)
743 {
744 struct bgp_notification bgpn;
745 int hlen;
746
747 TCHECK2(dat[0], BGP_NOTIFICATION_SIZE);
748 memcpy(&bgpn, dat, BGP_NOTIFICATION_SIZE);
749 hlen = ntohs(bgpn.bgpn_len);
750
751 printf(": error %s,", bgp_notify_major(bgpn.bgpn_major));
752 printf(" subcode %s",
753 bgp_notify_minor(bgpn.bgpn_major, bgpn.bgpn_minor));
754 return;
755 trunc:
756 printf("[|BGP]");
757 }
758
759 static void
760 bgp_header_print(const u_char *dat, int length)
761 {
762 struct bgp bgp;
763
764 TCHECK2(dat[0], BGP_SIZE);
765 memcpy(&bgp, dat, BGP_SIZE);
766 printf("(%s", bgp_type(bgp.bgp_type)); /* ) */
767
768 switch (bgp.bgp_type) {
769 case BGP_OPEN:
770 bgp_open_print(dat, length);
771 break;
772 case BGP_UPDATE:
773 bgp_update_print(dat, length);
774 break;
775 case BGP_NOTIFICATION:
776 bgp_notification_print(dat, length);
777 break;
778 }
779
780 /* ( */
781 printf(")");
782 return;
783 trunc:
784 printf("[|BGP]");
785 }
786
787 void
788 bgp_print(const u_char *dat, int length)
789 {
790 const u_char *p;
791 const u_char *ep;
792 const u_char *start;
793 const u_char marker[] = {
794 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
795 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
796 };
797 struct bgp bgp;
798 u_int16_t hlen;
799 int newline;
800
801 ep = dat + length;
802 if (snapend < dat + length)
803 ep = snapend;
804
805 printf(": BGP");
806
807 p = dat;
808 newline = 0;
809 start = p;
810 while (p < snapend) {
811 if (!TTEST2(p[0], 1))
812 break;
813 if (p[0] != 0xff) {
814 p++;
815 continue;
816 }
817
818 if (!TTEST2(p[0], sizeof(marker)))
819 break;
820 if (memcmp(p, marker, sizeof(marker)) != 0) {
821 p++;
822 continue;
823 }
824
825 /* found BGP header */
826 TCHECK2(p[0], BGP_SIZE); /*XXX*/
827 memcpy(&bgp, p, BGP_SIZE);
828
829 if (start != p)
830 printf(" [|BGP]");
831
832 hlen = ntohs(bgp.bgp_len);
833 if (vflag && newline)
834 printf("\n\t");
835 else
836 printf(" ");
837 if (TTEST2(p[0], hlen)) {
838 bgp_header_print(p, hlen);
839 newline = 1;
840 p += hlen;
841 start = p;
842 } else {
843 printf("[|BGP %s]", bgp_type(bgp.bgp_type));
844 break;
845 }
846 }
847
848 return;
849
850 trunc:
851 printf(" [|BGP]");
852 }