]> The Tcpdump Group git mirrors - tcpdump/blob - print-bgp.c
"bgp_vpn_rd_print()" returns a pointer into a buffer of "char"s, so it
[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.45 2002-08-02 04:04:37 guy Exp $";
37 #endif
38
39 #include <tcpdump-stdinc.h>
40
41 #include <errno.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <netdb.h>
45
46 #include "interface.h"
47 #include "addrtoname.h"
48 #include "extract.h"
49
50 struct bgp {
51 u_int8_t bgp_marker[16];
52 u_int16_t bgp_len;
53 u_int8_t bgp_type;
54 };
55 #define BGP_SIZE 19 /* unaligned */
56
57 #define BGP_OPEN 1
58 #define BGP_UPDATE 2
59 #define BGP_NOTIFICATION 3
60 #define BGP_KEEPALIVE 4
61 #define BGP_ROUTE_REFRESH 5
62
63 static struct tok bgp_msg_values[] = {
64 { BGP_OPEN, "Open"},
65 { BGP_UPDATE, "Update"},
66 { BGP_NOTIFICATION, "Notification"},
67 { BGP_KEEPALIVE, "Keepalive"},
68 { BGP_ROUTE_REFRESH, "Route Refresh"},
69 { 0, NULL}
70 };
71
72 struct bgp_open {
73 u_int8_t bgpo_marker[16];
74 u_int16_t bgpo_len;
75 u_int8_t bgpo_type;
76 u_int8_t bgpo_version;
77 u_int16_t bgpo_myas;
78 u_int16_t bgpo_holdtime;
79 u_int32_t bgpo_id;
80 u_int8_t bgpo_optlen;
81 /* options should follow */
82 };
83 #define BGP_OPEN_SIZE 29 /* unaligned */
84
85 struct bgp_opt {
86 u_int8_t bgpopt_type;
87 u_int8_t bgpopt_len;
88 /* variable length */
89 };
90 #define BGP_OPT_SIZE 2 /* some compilers may pad to 4 bytes */
91
92 struct bgp_notification {
93 u_int8_t bgpn_marker[16];
94 u_int16_t bgpn_len;
95 u_int8_t bgpn_type;
96 u_int8_t bgpn_major;
97 u_int8_t bgpn_minor;
98 /* data should follow */
99 };
100 #define BGP_NOTIFICATION_SIZE 21 /* unaligned */
101
102 struct bgp_route_refresh {
103 u_int8_t bgp_marker[16];
104 u_int16_t len;
105 u_int8_t type;
106 u_int8_t afi[2]; /* the compiler messes this structure up */
107 u_int8_t res; /* when doing misaligned sequences of int8 and int16 */
108 u_int8_t safi; /* afi should be int16 - so we have to access it using */
109 }; /* EXTRACT_16BITS(&bgp_route_refresh->afi) (sigh) */
110 #define BGP_ROUTE_REFRESH_SIZE 23
111
112 struct bgp_attr {
113 u_int8_t bgpa_flags;
114 u_int8_t bgpa_type;
115 union {
116 u_int8_t len;
117 u_int16_t elen;
118 } bgpa_len;
119 #define bgp_attr_len(p) \
120 (((p)->bgpa_flags & 0x10) ? \
121 ntohs((p)->bgpa_len.elen) : (p)->bgpa_len.len)
122 #define bgp_attr_off(p) \
123 (((p)->bgpa_flags & 0x10) ? 4 : 3)
124 };
125
126 #define BGPTYPE_ORIGIN 1
127 #define BGPTYPE_AS_PATH 2
128 #define BGPTYPE_NEXT_HOP 3
129 #define BGPTYPE_MULTI_EXIT_DISC 4
130 #define BGPTYPE_LOCAL_PREF 5
131 #define BGPTYPE_ATOMIC_AGGREGATE 6
132 #define BGPTYPE_AGGREGATOR 7
133 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
134 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC1998 */
135 #define BGPTYPE_CLUSTER_LIST 10 /* RFC1998 */
136 #define BGPTYPE_DPA 11 /* draft-ietf-idr-bgp-dpa */
137 #define BGPTYPE_ADVERTISERS 12 /* RFC1863 */
138 #define BGPTYPE_RCID_PATH 13 /* RFC1863 */
139 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */
140 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */
141 #define BGPTYPE_EXTD_COMMUNITIES 16 /* draft-ietf-idr-bgp-ext-communities */
142
143 static struct tok bgp_attr_values[] = {
144 { BGPTYPE_ORIGIN, "Origin"},
145 { BGPTYPE_AS_PATH, "AS Path"},
146 { BGPTYPE_NEXT_HOP, "Next Hop"},
147 { BGPTYPE_MULTI_EXIT_DISC, "Multi Exit Discriminator"},
148 { BGPTYPE_LOCAL_PREF, "Local Preference"},
149 { BGPTYPE_ATOMIC_AGGREGATE, "Atomic Aggregate"},
150 { BGPTYPE_AGGREGATOR, "Aggregator"},
151 { BGPTYPE_COMMUNITIES, "Community"},
152 { BGPTYPE_ORIGINATOR_ID, "Originator ID"},
153 { BGPTYPE_CLUSTER_LIST, "Cluster List"},
154 { BGPTYPE_DPA, "DPA"},
155 { BGPTYPE_ADVERTISERS, "Advertisers"},
156 { BGPTYPE_RCID_PATH, "RCID Path / Cluster ID"},
157 { BGPTYPE_MP_REACH_NLRI, "Multi-Protocol Reach NLRI"},
158 { BGPTYPE_MP_UNREACH_NLRI, "Multi-Protocol Unreach NLRI"},
159 { BGPTYPE_EXTD_COMMUNITIES, "Extended Community"},
160 { 255, "Reserved for development"},
161 { 0, NULL}
162 };
163
164 #define BGP_OPT_AUTH 1
165 #define BGP_OPT_CAP 2
166
167
168 static struct tok bgp_opt_values[] = {
169 { BGP_OPT_AUTH, "Authentication Information"},
170 { BGP_OPT_CAP, "Capabilities Advertisement"},
171 { 0, NULL}
172 };
173
174 #define BGP_CAPCODE_MP 1
175 #define BGP_CAPCODE_RR 2
176 #define BGP_CAPCODE_RR_CISCO 128
177
178 static struct tok bgp_capcode_values[] = {
179 { BGP_CAPCODE_MP, "Multiprotocol Extensions"},
180 { BGP_CAPCODE_RR, "Route Refresh"},
181 { BGP_CAPCODE_RR_CISCO, "Route Refresh (Cisco)"},
182 { 0, NULL}
183 };
184
185 #define BGP_NOTIFY_MAJOR_MSG 1
186 #define BGP_NOTIFY_MAJOR_OPEN 2
187 #define BGP_NOTIFY_MAJOR_UPDATE 3
188 #define BGP_NOTIFY_MAJOR_HOLDTIME 4
189 #define BGP_NOTIFY_MAJOR_FSM 5
190 #define BGP_NOTIFY_MAJOR_CEASE 6
191
192 static struct tok bgp_notify_major_values[] = {
193 { BGP_NOTIFY_MAJOR_MSG, "Message Header Error"},
194 { BGP_NOTIFY_MAJOR_OPEN, "OPEN Message Error"},
195 { BGP_NOTIFY_MAJOR_UPDATE, "UPDATE Message Error"},
196 { BGP_NOTIFY_MAJOR_HOLDTIME,"Hold Timer Expired"},
197 { BGP_NOTIFY_MAJOR_FSM, "Finite State Machine Error"},
198 { BGP_NOTIFY_MAJOR_CEASE, "Cease"},
199 { 0, NULL}
200 };
201
202 static struct tok bgp_notify_minor_msg_values[] = {
203 { 1, "Connection Not Synchronized"},
204 { 2, "Bad Message Length"},
205 { 3, "Bad Message Type"},
206 { 0, NULL}
207 };
208
209 static struct tok bgp_notify_minor_open_values[] = {
210 { 1, "Unsupported Version Number"},
211 { 2, "Bad Peer AS"},
212 { 3, "Bad BGP Identifier"},
213 { 4, "Unsupported Optional Parameter"},
214 { 5, "Authentication Failure"},
215 { 6, "Unacceptable Hold Time"},
216 { 0, NULL}
217 };
218
219 static struct tok bgp_notify_minor_update_values[] = {
220 { 1, "Malformed Attribute List"},
221 { 2, "Unrecognized Well-known Attribute"},
222 { 3, "Missing Well-known Attribute"},
223 { 4, "Attribute Flags Error"},
224 { 5, "Attribute Length Error"},
225 { 6, "Invalid ORIGIN Attribute"},
226 { 7, "AS Routing Loop"},
227 { 8, "Invalid NEXT_HOP Attribute"},
228 { 9, "Optional Attribute Error"},
229 { 10, "Invalid Network Field"},
230 { 11, "Malformed AS_PATH"},
231 { 0, NULL}
232 };
233
234 static struct tok bgp_origin_values[] = {
235 { 0, "IGP"},
236 { 1, "EGP"},
237 { 2, "Incomplete"},
238 { 0, NULL}
239 };
240
241 /* Subsequent address family identifier, RFC2283 section 7 */
242 #define SAFNUM_RES 0
243 #define SAFNUM_UNICAST 1
244 #define SAFNUM_MULTICAST 2
245 #define SAFNUM_UNIMULTICAST 3
246 /* labeled BGP RFC3107 */
247 #define SAFNUM_LABUNICAST 4
248 /* Section 4.3.4 of draft-rosen-rfc2547bis-03.txt */
249 #define SAFNUM_VPNUNICAST 128
250 #define SAFNUM_VPNMULTICAST 129
251 #define SAFNUM_VPNUNIMULTICAST 130
252
253 #define BGP_VPN_RD_LEN 8
254
255 static struct tok bgp_safi_values[] = {
256 { SAFNUM_RES, "Reserved"},
257 { SAFNUM_UNICAST, "Unicast"},
258 { SAFNUM_MULTICAST, "Multicast"},
259 { SAFNUM_UNIMULTICAST, "Unicast+Multicast"},
260 { SAFNUM_LABUNICAST, "labeled Unicast"},
261 { SAFNUM_VPNUNICAST, "labeled VPN Unicast"},
262 { SAFNUM_VPNMULTICAST, "labeled VPN Multicast"},
263 { SAFNUM_VPNUNIMULTICAST, "labeled VPN Unicast+Multicast"},
264 { 0, NULL }
265 };
266
267 /* well-known community */
268 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
269 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
270 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
271
272 /* RFC1700 address family numbers */
273 #define AFNUM_INET 1
274 #define AFNUM_INET6 2
275 #define AFNUM_NSAP 3
276 #define AFNUM_HDLC 4
277 #define AFNUM_BBN1822 5
278 #define AFNUM_802 6
279 #define AFNUM_E163 7
280 #define AFNUM_E164 8
281 #define AFNUM_F69 9
282 #define AFNUM_X121 10
283 #define AFNUM_IPX 11
284 #define AFNUM_ATALK 12
285 #define AFNUM_DECNET 13
286 #define AFNUM_BANYAN 14
287 #define AFNUM_E164NSAP 15
288 /* draft-kompella-ppvpn-l2vpn */
289 #define AFNUM_L2VPN 196 /* still to be approved by IANA */
290
291 static struct tok bgp_afi_values[] = {
292 { 0, "Reserved"},
293 { AFNUM_INET, "IPv4"},
294 { AFNUM_INET6, "IPv6"},
295 { AFNUM_NSAP, "NSAP"},
296 { AFNUM_HDLC, "HDLC"},
297 { AFNUM_BBN1822, "BBN 1822"},
298 { AFNUM_802, "802"},
299 { AFNUM_E163, "E.163"},
300 { AFNUM_E164, "E.164"},
301 { AFNUM_F69, "F.69"},
302 { AFNUM_X121, "X.121"},
303 { AFNUM_IPX, "Novell IPX"},
304 { AFNUM_ATALK, "Appletalk"},
305 { AFNUM_DECNET, "Decnet IV"},
306 { AFNUM_BANYAN, "Banyan Vines"},
307 { AFNUM_E164NSAP, "E.164 with NSAP subaddress"},
308 { AFNUM_L2VPN, "Layer-2 VPN"},
309 { 0, NULL},
310 };
311
312 static struct tok bgp_extd_comm_subtype_values[] = {
313 { 2, "target"},
314 { 3, "origin"},
315 { 4, "link-BW"},
316 { 10, "layer2-info"},
317 { 0, NULL},
318 };
319
320 static struct tok bgp_l2vpn_encaps_values[] = {
321 { 0, "Reserved"},
322 { 1, "Frame Relay"},
323 { 2, "ATM AAL5 VCC transport"},
324 { 3, "ATM transparent cell transport"},
325 { 4, "Ethernet VLAN"},
326 { 5, "Ethernet"},
327 { 6, "Cisco-HDLC"},
328 { 7, "PPP"},
329 { 8, "CEM"},
330 { 9, "ATM VCC cell transport"},
331 { 10, "ATM VPC cell transport"},
332 { 11, "MPLS"},
333 { 12, "VPLS"},
334 { 64, "IP-interworking"},
335 { 0, NULL},
336 };
337
338 static int
339 decode_prefix4(const u_char *pptr, char *buf, u_int buflen)
340 {
341 struct in_addr addr;
342 u_int plen;
343
344 plen = pptr[0];
345 if (plen < 0 || 32 < plen)
346 return -1;
347
348 memset(&addr, 0, sizeof(addr));
349 memcpy(&addr, &pptr[1], (plen + 7) / 8);
350 if (plen % 8) {
351 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
352 ((0xff00 >> (plen % 8)) & 0xff);
353 }
354 snprintf(buf, buflen, "%s/%d", getname((u_char *)&addr), plen);
355 return 1 + (plen + 7) / 8;
356 }
357
358 static int
359 decode_labeled_prefix4(const u_char *pptr, char *buf, u_int buflen)
360 {
361 struct in_addr addr;
362 u_int plen;
363
364 plen = pptr[0]; /* get prefix length */
365
366 /* this is one of the weirdnesses of rfc3107
367 the label length (actually the label + COS bits)
368 is added to the prefix length;
369 we also do only read out just one label -
370 there is no real application for advertisement of
371 stacked labels in a a single BGP message
372 */
373
374 plen-=24; /* adjust prefixlen - labellength */
375
376 if (plen < 0 || 32 < plen)
377 return -1;
378
379 memset(&addr, 0, sizeof(addr));
380 memcpy(&addr, &pptr[4], (plen + 7) / 8);
381 if (plen % 8) {
382 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
383 ((0xff00 >> (plen % 8)) & 0xff);
384 }
385 /* the label may get offsetted by 4 bits so lets shift it right */
386 snprintf(buf, buflen, "%s/%d, label:%u %s",
387 getname((u_char *)&addr),
388 plen,
389 EXTRACT_24BITS(pptr+1)>>4,
390 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
391
392 return 4 + (plen + 7) / 8;
393 }
394
395
396 static char *
397 bgp_vpn_rd_print (const u_char *pptr) {
398
399 /* allocate space for the following string
400 * xxx.xxx.xxx.xxx:xxxxx
401 * 21 bytes plus one termination byte */
402 static char rd[22];
403 char *pos = rd;
404
405 /* ok lets load the RD format */
406 switch (EXTRACT_16BITS(pptr)) {
407 /* AS:IP-address fmt*/
408 case 0:
409 pos+=sprintf(pos, "%u:%u.%u.%u.%u",
410 EXTRACT_16BITS(pptr+2),
411 *(pptr+4),
412 *(pptr+5),
413 *(pptr+6),
414 *(pptr+7));
415 break;
416 /* IP-address:AS fmt*/
417 case 1:
418 pos+=sprintf(pos, "%u.%u.%u.%u:%u",
419 *(pptr+2),
420 *(pptr+3),
421 *(pptr+4),
422 *(pptr+5),
423 EXTRACT_16BITS(pptr+6));
424 break;
425 default:
426 pos+=sprintf(pos, "unknown RD format");
427 break;
428 }
429 *(pos) = '\0';
430 return (rd);
431 }
432
433 static int
434 decode_labeled_vpn_prefix4(const u_char *pptr, char *buf, u_int buflen)
435 {
436 struct in_addr addr;
437 u_int plen;
438
439 plen = pptr[0]; /* get prefix length */
440
441 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
442
443 if (plen < 0 || 32 < plen)
444 return -1;
445
446 memset(&addr, 0, sizeof(addr));
447 memcpy(&addr, &pptr[12], (plen + 7) / 8);
448 if (plen % 8) {
449 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
450 ((0xff00 >> (plen % 8)) & 0xff);
451 }
452 /* the label may get offsetted by 4 bits so lets shift it right */
453 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
454 bgp_vpn_rd_print(pptr+4),
455 getname((u_char *)&addr),
456 plen,
457 EXTRACT_24BITS(pptr+1)>>4,
458 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
459
460 return 12 + (plen + 7) / 8;
461 }
462
463 static int
464 decode_labeled_vpn_l2(const u_char *pptr, char *buf, u_int buflen)
465 {
466 int plen,tlen,strlen,tlv_type,tlv_len,ttlv_len;
467 plen=EXTRACT_16BITS(pptr);
468 tlen=plen;
469 pptr+=2;
470 strlen=snprintf(buf, buflen, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
471 bgp_vpn_rd_print(pptr),
472 EXTRACT_16BITS(pptr+8),
473 EXTRACT_16BITS(pptr+10),
474 EXTRACT_24BITS(pptr+12)>>4); /* the label is offsetted by 4 bits so lets shift it right */
475 pptr+=15;
476 tlen-=15;
477
478 /* ok now the variable part - lets read out TLVs*/
479 while (tlen>0) {
480 tlv_type=*pptr++;
481 tlv_len=EXTRACT_16BITS(pptr);
482 ttlv_len=tlv_len;
483 pptr+=2;
484
485 switch(tlv_type) {
486 case 1:
487 strlen+=snprintf(buf+strlen,buflen-strlen, "\n\t\tcircuit status vector (%u) length: %u: 0x",
488 tlv_type,
489 tlv_len);
490 ttlv_len=ttlv_len/8+1; /* how many bytes do we need to read ? */
491 while (ttlv_len>0) {
492 strlen+=snprintf(buf+strlen,buflen-strlen, "%02x",*pptr++);
493 ttlv_len--;
494 }
495 break;
496 default:
497 snprintf(buf+strlen,buflen-strlen, "\n\t\tunknown TLV #%u, length: %u",
498 tlv_type,
499 tlv_len);
500 break;
501 }
502 tlen-=(tlv_len<<3); /* the tlv-length is expressed in bits so lets shift it tright */
503 }
504 return plen+2;
505 }
506
507 #ifdef INET6
508 static int
509 decode_prefix6(const u_char *pd, char *buf, u_int buflen)
510 {
511 struct in6_addr addr;
512 u_int plen;
513
514 plen = pd[0];
515 if (plen < 0 || 128 < plen)
516 return -1;
517
518 memset(&addr, 0, sizeof(addr));
519 memcpy(&addr, &pd[1], (plen + 7) / 8);
520 if (plen % 8) {
521 addr.s6_addr[(plen + 7) / 8 - 1] &=
522 ((0xff00 >> (plen % 8)) & 0xff);
523 }
524 snprintf(buf, buflen, "%s/%d", getname6((u_char *)&addr), plen);
525 return 1 + (plen + 7) / 8;
526 }
527 #endif
528
529 static void
530 bgp_attr_print(const struct bgp_attr *attr, const u_char *pptr, int len)
531 {
532 int i;
533 u_int16_t af;
534 u_int8_t safi, snpa;
535 int advance;
536 int tlen;
537 const u_char *tptr;
538 char buf[MAXHOSTNAMELEN + 100];
539
540 tptr = pptr;
541 tlen=len;
542
543 switch (attr->bgpa_type) {
544 case BGPTYPE_ORIGIN:
545 if (len != 1)
546 printf("invalid len");
547 else
548 printf("%s", tok2str(bgp_origin_values, "Unknown Origin Typecode", tptr[0]));
549 break;
550 case BGPTYPE_AS_PATH:
551 if (len % 2) {
552 printf("invalid len");
553 break;
554 }
555 if (!len) {
556 printf("empty");
557 break;
558 }
559 while (tptr < pptr + len) {
560 /*
561 * under RFC1965, p[0] means:
562 * 1: AS_SET 2: AS_SEQUENCE
563 * 3: AS_CONFED_SET 4: AS_CONFED_SEQUENCE
564 */
565 if (tptr[0] == 3 || tptr[0] == 4)
566 printf("confed");
567 printf("%s", (tptr[0] & 1) ? "{" : "");
568 for (i = 0; i < tptr[1] * 2; i += 2) {
569 printf("%s%u", i == 0 ? "" : " ",
570 EXTRACT_16BITS(&tptr[2 + i]));
571 }
572 printf("%s", (tptr[0] & 1) ? "}" : "");
573 tptr += 2 + tptr[1] * 2;
574 }
575 break;
576 case BGPTYPE_NEXT_HOP:
577 if (len != 4)
578 printf("invalid len");
579 else
580 printf("%s", getname(tptr));
581 break;
582 case BGPTYPE_MULTI_EXIT_DISC:
583 case BGPTYPE_LOCAL_PREF:
584 if (len != 4)
585 printf("invalid len");
586 else
587 printf("%u", EXTRACT_32BITS(tptr));
588 break;
589 case BGPTYPE_ATOMIC_AGGREGATE:
590 if (len != 0)
591 printf("invalid len");
592 break;
593 case BGPTYPE_AGGREGATOR:
594 if (len != 6) {
595 printf("invalid len");
596 break;
597 }
598 printf(" AS #%u, origin %s", EXTRACT_16BITS(tptr),
599 getname(tptr + 2));
600 break;
601 case BGPTYPE_COMMUNITIES:
602 if (len % 4) {
603 printf("invalid len");
604 break;
605 }
606 while (tlen>0) {
607 u_int32_t comm;
608 comm = EXTRACT_32BITS(tptr);
609 switch (comm) {
610 case BGP_COMMUNITY_NO_EXPORT:
611 printf(" NO_EXPORT");
612 break;
613 case BGP_COMMUNITY_NO_ADVERT:
614 printf(" NO_ADVERTISE");
615 break;
616 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED:
617 printf(" NO_EXPORT_SUBCONFED");
618 break;
619 default:
620 printf("%u:%u%s",
621 (comm >> 16) & 0xffff,
622 comm & 0xffff,
623 (tlen>4) ? ", " : "");
624 break;
625 }
626 tlen -=4;
627 tptr +=4;
628 }
629 break;
630 case BGPTYPE_ORIGINATOR_ID:
631 if (len != 4) {
632 printf("invalid len");
633 break;
634 }
635 printf("%s",getname(tptr));
636 break;
637 case BGPTYPE_CLUSTER_LIST:
638 while (tlen>0) {
639 printf("%s%s",
640 getname(tptr),
641 (tlen>4) ? ", " : "");
642 tlen -=4;
643 tptr +=4;
644 }
645 break;
646 case BGPTYPE_MP_REACH_NLRI:
647 af = EXTRACT_16BITS(tptr);
648 safi = tptr[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 if (af == AFNUM_INET || af==AFNUM_L2VPN)
658 ;
659 #ifdef INET6
660 else if (af == AFNUM_INET6)
661 ;
662 #endif
663 else {
664 printf("\n\t no AFI %u decoder",af);
665 if (!vflag)
666 print_unknown_data(tptr,"\n\t ",tlen);
667 break;
668 }
669
670 tptr +=3;
671
672 tlen = tptr[0];
673 tptr++;
674
675 if (tlen) {
676 printf("\n\t nexthop: ");
677 while (tlen > 0) {
678 switch (af) {
679 case AFNUM_INET:
680 switch(safi) {
681 case SAFNUM_UNICAST:
682 case SAFNUM_MULTICAST:
683 case SAFNUM_UNIMULTICAST:
684 case SAFNUM_LABUNICAST:
685 printf("%s",getname(tptr));
686 tlen -= sizeof(struct in_addr);
687 tptr += sizeof(struct in_addr);
688 break;
689 case SAFNUM_VPNUNICAST:
690 case SAFNUM_VPNMULTICAST:
691 case SAFNUM_VPNUNIMULTICAST:
692 printf("RD: %s, %s",
693 bgp_vpn_rd_print(tptr),
694 getname(tptr+BGP_VPN_RD_LEN));
695 tlen -= (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
696 tptr += (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
697 break;
698 default:
699 printf("no SAFI %u decoder",safi);
700 if (!vflag)
701 print_unknown_data(tptr,"\n\t ",tlen);
702 break;
703 }
704 break;
705 #ifdef INET6
706 case AFNUM_INET6:
707 switch(safi) {
708 case SAFNUM_UNICAST:
709 case SAFNUM_MULTICAST:
710 case SAFNUM_UNIMULTICAST:
711 case SAFNUM_LABUNICAST:
712 printf("%s", getname6(tptr));
713 tlen -= sizeof(struct in6_addr);
714 tptr += sizeof(struct in6_addr);
715 break;
716 case SAFNUM_VPNUNICAST:
717 case SAFNUM_VPNMULTICAST:
718 case SAFNUM_VPNUNIMULTICAST:
719 printf("RD: %s, %s",
720 bgp_vpn_rd_print(tptr),
721 getname6(tptr+BGP_VPN_RD_LEN));
722 tlen -= (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
723 tptr += (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
724 break;
725 default:
726 printf("no SAFI %u decoder",safi);
727 if (!vflag)
728 print_unknown_data(tptr,"\n\t ",tlen);
729 break;
730 }
731 break;
732 #endif
733 case AFNUM_L2VPN:
734 switch(safi) {
735 case SAFNUM_VPNUNICAST:
736 case SAFNUM_VPNMULTICAST:
737 case SAFNUM_VPNUNIMULTICAST:
738 printf("%s", getname(tptr));
739 tlen -= (sizeof(struct in_addr));
740 tptr += (sizeof(struct in_addr));
741 break;
742 default:
743 printf("no SAFI %u decoder",safi);
744 if (!vflag)
745 print_unknown_data(tptr,"\n\t ",tlen);
746 break;
747 }
748 break;
749
750 default:
751 printf("no AFI %u decoder",af);
752 if (!vflag)
753 print_unknown_data(tptr,"\n\t ",tlen);
754 break;
755 }
756 }
757 }
758 tptr += tlen;
759
760 snpa = tptr[0];
761 tptr++;
762
763 if (snpa) {
764 printf("\n\t %u SNPA", snpa);
765 for (/*nothing*/; snpa > 0; snpa--) {
766 printf("\n\t %d bytes", tptr[0]);
767 tptr += tptr[0] + 1;
768 }
769 } else {
770 printf(", no SNPA");
771 }
772
773 while (len - (tptr - pptr) > 0) {
774 switch (af) {
775 case AFNUM_INET:
776 switch (safi) {
777 case SAFNUM_UNICAST:
778 case SAFNUM_MULTICAST:
779 case SAFNUM_UNIMULTICAST:
780 advance = decode_prefix4(tptr, buf, sizeof(buf));
781 printf("\n\t %s", buf);
782 break;
783 case SAFNUM_LABUNICAST:
784 advance = decode_labeled_prefix4(tptr, buf, sizeof(buf));
785 printf("\n\t %s", buf);
786 break;
787 case SAFNUM_VPNUNICAST:
788 case SAFNUM_VPNMULTICAST:
789 case SAFNUM_VPNUNIMULTICAST:
790 advance = decode_labeled_vpn_prefix4(tptr, buf, sizeof(buf));
791 printf("\n\t %s", buf);
792 break;
793 default:
794 printf("\n\t no SAFI %u decoder",safi);
795 if (!vflag)
796 print_unknown_data(tptr-3,"\n\t ",tlen);
797 advance = 0;
798 tptr = pptr + len;
799 break;
800 }
801 break;
802 #ifdef INET6
803 case AFNUM_INET6:
804 switch (safi) {
805 case SAFNUM_UNICAST:
806 case SAFNUM_MULTICAST:
807 case SAFNUM_UNIMULTICAST:
808 advance = decode_prefix6(tptr, buf, sizeof(buf));
809 printf("\n\t %s", buf);
810 break;
811 default:
812 printf("\n\t no SAFI %u decoder ",safi);
813 if (!vflag)
814 print_unknown_data(tptr-3,"\n\t ",tlen);
815 advance = 0;
816 tptr = pptr + len;
817 break;
818 }
819 break;
820 #endif
821 case AFNUM_L2VPN:
822 switch(safi) {
823 case SAFNUM_VPNUNICAST:
824 case SAFNUM_VPNMULTICAST:
825 case SAFNUM_VPNUNIMULTICAST:
826 advance = decode_labeled_vpn_l2(tptr, buf, sizeof(buf));
827 printf("\n\t %s", buf);
828 break;
829 default:
830 printf("no SAFI %u decoder",safi);
831 if (!vflag)
832 print_unknown_data(tptr,"\n\t ",tlen);
833 advance = 0;
834 tptr = pptr + len;
835 break;
836 }
837 break;
838
839
840 default:
841 printf("\n\t no AFI %u decoder ",af);
842 if (!vflag)
843 print_unknown_data(tptr-3,"\n\t ",tlen);
844 advance = 0;
845 tptr = pptr + len;
846 break;
847 }
848 tptr += advance;
849 }
850 break;
851
852 case BGPTYPE_MP_UNREACH_NLRI:
853 af = EXTRACT_16BITS(tptr);
854 safi = tptr[2];
855
856 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
857 tok2str(bgp_afi_values, "Unknown AFI", af),
858 af,
859 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
860 tok2str(bgp_safi_values, "Unknown SAFI", safi),
861 safi);
862
863 tptr += 3;
864
865 while (len - (tptr - pptr) > 0) {
866 switch (af) {
867 case AFNUM_INET:
868 switch (safi) {
869 case SAFNUM_UNICAST:
870 case SAFNUM_MULTICAST:
871 case SAFNUM_UNIMULTICAST:
872 advance = decode_prefix4(tptr, buf, sizeof(buf));
873 printf("\n\t %s", buf);
874 break;
875 case SAFNUM_LABUNICAST:
876 advance = decode_labeled_prefix4(tptr, buf, sizeof(buf));
877 printf("\n\t %s", buf);
878 break;
879 case SAFNUM_VPNUNICAST:
880 case SAFNUM_VPNMULTICAST:
881 case SAFNUM_VPNUNIMULTICAST:
882 advance = decode_labeled_vpn_prefix4(tptr, buf, sizeof(buf));
883 printf("\n\t %s", buf);
884 break;
885 default:
886 printf("\n\t no SAFI %u decoder",safi);
887 if (!vflag)
888 print_unknown_data(tptr-3,"\n\t ",tlen);
889 advance = 0;
890 tptr = pptr + len;
891 break;
892 }
893 break;
894
895 #ifdef INET6
896 case AFNUM_INET6:
897 switch (safi) {
898 case SAFNUM_UNICAST:
899 case SAFNUM_MULTICAST:
900 case SAFNUM_UNIMULTICAST:
901 advance = decode_prefix6(tptr, buf, sizeof(buf));
902 printf("\n\t %s", buf);
903 break;
904 default:
905 printf("\n\t no SAFI %u decoder",safi);
906 if (!vflag)
907 print_unknown_data(tptr-3,"\n\t ",tlen);
908 advance = 0;
909 tptr = pptr + len;
910 break;
911 }
912 break;
913 #endif
914
915 case AFNUM_L2VPN:
916 switch(safi) {
917 case SAFNUM_VPNUNICAST:
918 case SAFNUM_VPNMULTICAST:
919 case SAFNUM_VPNUNIMULTICAST:
920 advance = decode_labeled_vpn_l2(tptr, buf, sizeof(buf));
921 printf("\n\t %s", buf);
922 break;
923 default:
924 printf("no SAFI %u decoder",safi);
925 if (!vflag)
926 print_unknown_data(tptr-3,"\n\t ",tlen);
927 advance = 0;
928 tptr = pptr + len;
929 break;
930 }
931 break;
932
933 default:
934 printf("\n\t no AFI %u decoder",af);
935 if (!vflag)
936 print_unknown_data(tptr-3,"\n\t ",tlen);
937 advance = 0;
938 tptr = pptr + len;
939 break;
940 }
941
942 tptr += advance;
943 }
944 break;
945 case BGPTYPE_EXTD_COMMUNITIES:
946 if (len % 8) {
947 printf("invalid len");
948 break;
949 }
950 while (tlen>0) {
951 u_int8_t extd_comm,extd_comm_type,extd_comm_subtype;
952 extd_comm=*tptr;
953 extd_comm_type=extd_comm&0x3f;
954 extd_comm_subtype=*(tptr+1);
955 switch(extd_comm_type) {
956 case 0:
957 switch (extd_comm_subtype) {
958 case 2:
959 case 3:
960 case 4:
961 printf("\n\t %s%s%s:%u:%s",
962 (extd_comm&0x80) ? "vendor-specific: " : "",
963 (extd_comm&0x40) ? "non-transitive: " : "",
964 tok2str(bgp_extd_comm_subtype_values,
965 "unknown",
966 extd_comm_subtype&0x3f),
967 EXTRACT_16BITS(tptr+2),
968 getname(tptr+4));
969 break;
970 /* juniper has allocated typecode 10 to convey l2 information */
971 case 10:
972 printf("\n\t %s%s:%s:Control Flags [0x%02x]:MTU %u",
973 (extd_comm&0x40) ? "non-transitive: " : "",
974 tok2str(bgp_extd_comm_subtype_values,
975 "unknown",
976 extd_comm_subtype&0x3f),
977 tok2str(bgp_l2vpn_encaps_values,
978 "unknown encaps",
979 *(tptr+2)),
980 *(tptr+3),
981 EXTRACT_16BITS(tptr+4));
982 break;
983 }
984 break;
985 case 1:
986 printf("\n\t %s%s%s:%s:%u",
987 (extd_comm&0x80) ? "vendor-specific: " : "",
988 (extd_comm&0x40) ? "non-transitive: " : "",
989 tok2str(bgp_extd_comm_subtype_values,
990 "unknown",
991 extd_comm_subtype&0x3f),
992 getname(tptr+2),
993 EXTRACT_16BITS(tptr+6));
994 break;
995 case 2:
996 printf("\n\t %s%s%s:%u:%u",
997 (extd_comm&0x80) ? "vendor-specific: " : "",
998 (extd_comm&0x40) ? "non-transitive: " : "",
999 tok2str(bgp_extd_comm_subtype_values,
1000 "unknown",
1001 extd_comm_subtype&0x3f),
1002 EXTRACT_32BITS(tptr+2),
1003 EXTRACT_16BITS(tptr+6));
1004 break;
1005 default:
1006 printf("\n\t no typecode %u decoder",
1007 extd_comm_type);
1008 print_unknown_data(tptr,"\n\t ",8);
1009 break;
1010 }
1011 tlen -=8;
1012 tptr +=8;
1013 }
1014 break;
1015
1016 default:
1017 printf("\n\t no Attribute %u decoder",attr->bgpa_type); /* we have no decoder for the attribute */
1018 if (!vflag)
1019 print_unknown_data(pptr,"\n\t ",len);
1020 break;
1021 }
1022 if (vflag&&len) /* omit zero length attributes*/
1023 print_unknown_data(pptr,"\n\t ",len);
1024 }
1025
1026 static void
1027 bgp_open_print(const u_char *dat, int length)
1028 {
1029 struct bgp_open bgpo;
1030 struct bgp_opt bgpopt;
1031 int hlen;
1032 const u_char *opt;
1033 int i,cap_type,cap_len;
1034
1035 TCHECK2(dat[0], BGP_OPEN_SIZE);
1036 memcpy(&bgpo, dat, BGP_OPEN_SIZE);
1037 hlen = ntohs(bgpo.bgpo_len);
1038
1039 printf("\n\t Version %d, ", bgpo.bgpo_version);
1040 printf("my AS %u, ", ntohs(bgpo.bgpo_myas));
1041 printf("Holdtime %us, ", ntohs(bgpo.bgpo_holdtime));
1042 printf("ID %s", getname((u_char *)&bgpo.bgpo_id));
1043 printf("\n\t Optional parameters, length: %u", bgpo.bgpo_optlen);
1044
1045 /* ugly! */
1046 opt = &((const struct bgp_open *)dat)->bgpo_optlen;
1047 opt++;
1048
1049 i = 0;
1050 while (i < bgpo.bgpo_optlen) {
1051 TCHECK2(opt[i], BGP_OPT_SIZE);
1052 memcpy(&bgpopt, &opt[i], BGP_OPT_SIZE);
1053 if (i + 2 + bgpopt.bgpopt_len > bgpo.bgpo_optlen) {
1054 printf("\n\t Option %d, length: %u", bgpopt.bgpopt_type, bgpopt.bgpopt_len);
1055 break;
1056 }
1057
1058 printf("\n\t Option %s (%u), length: %u",
1059 tok2str(bgp_opt_values,"Unknown", bgpopt.bgpopt_type),
1060 bgpopt.bgpopt_type,
1061 bgpopt.bgpopt_len);
1062
1063 /* now lets decode the options we know*/
1064 switch(bgpopt.bgpopt_type) {
1065 case BGP_OPT_CAP:
1066 cap_type=opt[i+BGP_OPT_SIZE];
1067 cap_len=opt[i+BGP_OPT_SIZE+1];
1068 printf("\n\t %s, length: %u",
1069 tok2str(bgp_capcode_values,"Unknown", cap_type),
1070 cap_len);
1071 switch(cap_type) {
1072 case BGP_CAPCODE_MP:
1073 printf("\n\t\tAFI %s (%u), SAFI %s (%u)",
1074 tok2str(bgp_afi_values,"Unknown", EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+2)),
1075 EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+2),
1076 tok2str(bgp_safi_values,"Unknown", opt[i+BGP_OPT_SIZE+5]),
1077 opt[i+BGP_OPT_SIZE+5]);
1078 break;
1079 case BGP_CAPCODE_RR:
1080 case BGP_CAPCODE_RR_CISCO:
1081 break;
1082 default:
1083 printf("\n\t\tno decoder for Capability %u",
1084 cap_type);
1085 break;
1086 }
1087 break;
1088 case BGP_OPT_AUTH:
1089 default:
1090 printf("\n\t no decoder for option %u",
1091 bgpopt.bgpopt_type);
1092 break;
1093 }
1094
1095 i += BGP_OPT_SIZE + bgpopt.bgpopt_len;
1096 }
1097 return;
1098 trunc:
1099 printf("[|BGP]");
1100 }
1101
1102 static void
1103 bgp_update_print(const u_char *dat, int length)
1104 {
1105 struct bgp bgp;
1106 struct bgp_attr bgpa;
1107 int hlen;
1108 const u_char *p;
1109 int len;
1110 int i;
1111
1112 TCHECK2(dat[0], BGP_SIZE);
1113 memcpy(&bgp, dat, BGP_SIZE);
1114 hlen = ntohs(bgp.bgp_len);
1115 p = dat + BGP_SIZE; /*XXX*/
1116
1117 /* Unfeasible routes */
1118 len = EXTRACT_16BITS(p);
1119 if (len) {
1120 /*
1121 * Without keeping state from the original NLRI message,
1122 * it's not possible to tell if this a v4 or v6 route,
1123 * so only try to decode it if we're not v6 enabled.
1124 */
1125 #ifdef INET6
1126 printf("\n\t Withdrawn routes: %d bytes", len);
1127 #else
1128 char buf[MAXHOSTNAMELEN + 100];
1129
1130 TCHECK2(p[2], len);
1131 i = 2;
1132
1133 printf("\n\t Withdrawn routes:");
1134
1135 while(i < 2 + len) {
1136 i += decode_prefix4(&p[i], buf, sizeof(buf));
1137 printf("\n\t %s", buf);
1138 }
1139 #endif
1140 }
1141 p += 2 + len;
1142
1143 TCHECK2(p[0], 2);
1144 len = EXTRACT_16BITS(p);
1145 if (len) {
1146 /* do something more useful!*/
1147 i = 2;
1148 while (i < 2 + len) {
1149 int alen, aoff;
1150
1151 TCHECK2(p[i], sizeof(bgpa));
1152 memcpy(&bgpa, &p[i], sizeof(bgpa));
1153 alen = bgp_attr_len(&bgpa);
1154 aoff = bgp_attr_off(&bgpa);
1155
1156 printf("\n\t %s (%u), length: %u",
1157 tok2str(bgp_attr_values, "Unknown Attribute", bgpa.bgpa_type),
1158 bgpa.bgpa_type,
1159 alen);
1160
1161 if (bgpa.bgpa_flags) {
1162 printf(", flags [%s%s%s%s",
1163 bgpa.bgpa_flags & 0x80 ? "O" : "",
1164 bgpa.bgpa_flags & 0x40 ? "T" : "",
1165 bgpa.bgpa_flags & 0x20 ? "P" : "",
1166 bgpa.bgpa_flags & 0x10 ? "E" : "");
1167 if (bgpa.bgpa_flags & 0xf)
1168 printf("+%x", bgpa.bgpa_flags & 0xf);
1169 printf("]: ");
1170 }
1171 bgp_attr_print(&bgpa, &p[i + aoff], alen);
1172 i += aoff + alen;
1173 }
1174 }
1175 p += 2 + len;
1176
1177 if (dat + length > p) {
1178 printf("\n\t Updated routes:");
1179 while (dat + length > p) {
1180 char buf[MAXHOSTNAMELEN + 100];
1181 i = decode_prefix4(p, buf, sizeof(buf));
1182 printf("\n\t %s", buf);
1183 if (i < 0)
1184 break;
1185 p += i;
1186 }
1187 }
1188 return;
1189 trunc:
1190 printf("[|BGP]");
1191 }
1192
1193 static void
1194 bgp_notification_print(const u_char *dat, int length)
1195 {
1196 struct bgp_notification bgpn;
1197 int hlen;
1198
1199 TCHECK2(dat[0], BGP_NOTIFICATION_SIZE);
1200 memcpy(&bgpn, dat, BGP_NOTIFICATION_SIZE);
1201 hlen = ntohs(bgpn.bgpn_len);
1202
1203 printf(", Error - %s", tok2str(bgp_notify_major_values, "Unknown", bgpn.bgpn_major));
1204
1205 switch (bgpn.bgpn_major) {
1206
1207 case BGP_NOTIFY_MAJOR_MSG:
1208 printf(" subcode %s", tok2str(bgp_notify_minor_msg_values, "Unknown", bgpn.bgpn_minor));
1209 break;
1210 case BGP_NOTIFY_MAJOR_OPEN:
1211 printf(" subcode %s", tok2str(bgp_notify_minor_open_values, "Unknown", bgpn.bgpn_minor));
1212 break;
1213 case BGP_NOTIFY_MAJOR_UPDATE:
1214 printf(" subcode %s", tok2str(bgp_notify_minor_update_values, "Unknown", bgpn.bgpn_minor));
1215 break;
1216 default:
1217 break;
1218 }
1219
1220 return;
1221 trunc:
1222 printf("[|BGP]");
1223 }
1224
1225 static void
1226 bgp_route_refresh_print(const u_char *pptr, int len) {
1227
1228 const struct bgp_route_refresh *bgp_route_refresh_header;
1229 bgp_route_refresh_header = (const struct bgp_route_refresh *)pptr;
1230
1231 printf("\n\t AFI %s (%u), SAFI %s (%u)",
1232 tok2str(bgp_afi_values,"Unknown",
1233 EXTRACT_16BITS(&bgp_route_refresh_header->afi)), /* this stinks but the compiler pads the structure weird */
1234 EXTRACT_16BITS(&bgp_route_refresh_header->afi),
1235 tok2str(bgp_safi_values,"Unknown",
1236 bgp_route_refresh_header->safi),
1237 bgp_route_refresh_header->safi);
1238
1239 if (vflag)
1240 print_unknown_data(pptr,"\n\t ", len);
1241
1242 return;
1243 }
1244
1245 static void
1246 bgp_header_print(const u_char *dat, int length)
1247 {
1248 struct bgp bgp;
1249
1250 TCHECK2(dat[0], BGP_SIZE);
1251 memcpy(&bgp, dat, BGP_SIZE);
1252 printf("\n\t%s Message (%u), length: %u ",
1253 tok2str(bgp_msg_values, "Unknown", bgp.bgp_type),
1254 bgp.bgp_type,
1255 length);
1256
1257 switch (bgp.bgp_type) {
1258 case BGP_OPEN:
1259 bgp_open_print(dat, length);
1260 break;
1261 case BGP_UPDATE:
1262 bgp_update_print(dat, length);
1263 break;
1264 case BGP_NOTIFICATION:
1265 bgp_notification_print(dat, length);
1266 break;
1267 case BGP_KEEPALIVE:
1268 break;
1269 case BGP_ROUTE_REFRESH:
1270 bgp_route_refresh_print(dat, length);
1271 break;
1272 default:
1273 /* we have no decoder for the BGP message */
1274 printf("\n\t no Message %u decoder",bgp.bgp_type);
1275 print_unknown_data(dat,"\n\t ",length);
1276 break;
1277 }
1278 return;
1279 trunc:
1280 printf("[|BGP]");
1281 }
1282
1283 void
1284 bgp_print(const u_char *dat, int length)
1285 {
1286 const u_char *p;
1287 const u_char *ep;
1288 const u_char *start;
1289 const u_char marker[] = {
1290 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1291 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1292 };
1293 struct bgp bgp;
1294 u_int16_t hlen;
1295
1296 ep = dat + length;
1297 if (snapend < dat + length)
1298 ep = snapend;
1299
1300 printf(": BGP");
1301
1302 p = dat;
1303 start = p;
1304 while (p < snapend) {
1305 if (!TTEST2(p[0], 1))
1306 break;
1307 if (p[0] != 0xff) {
1308 p++;
1309 continue;
1310 }
1311
1312 if (!TTEST2(p[0], sizeof(marker)))
1313 break;
1314 if (memcmp(p, marker, sizeof(marker)) != 0) {
1315 p++;
1316 continue;
1317 }
1318
1319 /* found BGP header */
1320 TCHECK2(p[0], BGP_SIZE); /*XXX*/
1321 memcpy(&bgp, p, BGP_SIZE);
1322
1323 if (start != p)
1324 printf(" [|BGP]");
1325
1326 hlen = ntohs(bgp.bgp_len);
1327
1328 if (TTEST2(p[0], hlen)) {
1329 bgp_header_print(p, hlen);
1330 p += hlen;
1331 start = p;
1332 } else {
1333 printf("[|BGP %s]", tok2str(bgp_msg_values, "Unknown Message Type",bgp.bgp_type));
1334 break;
1335 }
1336 }
1337
1338 return;
1339
1340 trunc:
1341 printf(" [|BGP]");
1342 }
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353