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