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