]> The Tcpdump Group git mirrors - tcpdump/blob - print-bgp.c
989987510d0fdf6b375e87b84fb1eb2ad359e0a0
[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.57 2002-12-15 08:33:23 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 EXTRACT_16BITS(&(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 static char *
436 bgp_vpn_rd_print (const u_char *pptr) {
437
438 /* allocate space for the following string
439 * xxx.xxx.xxx.xxx:xxxxx
440 * 21 bytes plus one termination byte */
441 static char rd[22];
442 char *pos = rd;
443
444 /* ok lets load the RD format */
445 switch (EXTRACT_16BITS(pptr)) {
446 /* AS:IP-address fmt*/
447 case 0:
448 pos+=sprintf(pos, "%u:%u.%u.%u.%u",
449 EXTRACT_16BITS(pptr+2),
450 *(pptr+4),
451 *(pptr+5),
452 *(pptr+6),
453 *(pptr+7));
454 break;
455 /* IP-address:AS fmt*/
456 case 1:
457 pos+=sprintf(pos, "%u.%u.%u.%u:%u",
458 *(pptr+2),
459 *(pptr+3),
460 *(pptr+4),
461 *(pptr+5),
462 EXTRACT_16BITS(pptr+6));
463 break;
464 default:
465 pos+=sprintf(pos, "unknown RD format");
466 break;
467 }
468 *(pos) = '\0';
469 return (rd);
470 }
471
472 static int
473 decode_labeled_vpn_prefix4(const u_char *pptr, char *buf, u_int buflen)
474 {
475 struct in_addr addr;
476 u_int plen;
477
478 plen = pptr[0]; /* get prefix length */
479
480 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
481
482 if (32 < plen)
483 return -1;
484
485 memset(&addr, 0, sizeof(addr));
486 memcpy(&addr, &pptr[12], (plen + 7) / 8);
487 if (plen % 8) {
488 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
489 ((0xff00 >> (plen % 8)) & 0xff);
490 }
491 /* the label may get offsetted by 4 bits so lets shift it right */
492 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
493 bgp_vpn_rd_print(pptr+4),
494 getname((u_char *)&addr),
495 plen,
496 EXTRACT_24BITS(pptr+1)>>4,
497 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
498
499 return 12 + (plen + 7) / 8;
500 }
501
502 static int
503 decode_labeled_vpn_l2(const u_char *pptr, char *buf, u_int buflen)
504 {
505 int plen,tlen,strlen,tlv_type,tlv_len,ttlv_len;
506 plen=EXTRACT_16BITS(pptr);
507 tlen=plen;
508 pptr+=2;
509 strlen=snprintf(buf, buflen, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
510 bgp_vpn_rd_print(pptr),
511 EXTRACT_16BITS(pptr+8),
512 EXTRACT_16BITS(pptr+10),
513 EXTRACT_24BITS(pptr+12)>>4); /* the label is offsetted by 4 bits so lets shift it right */
514 pptr+=15;
515 tlen-=15;
516
517 /* ok now the variable part - lets read out TLVs*/
518 while (tlen>0) {
519 tlv_type=*pptr++;
520 tlv_len=EXTRACT_16BITS(pptr);
521 ttlv_len=tlv_len;
522 pptr+=2;
523
524 switch(tlv_type) {
525 case 1:
526 strlen+=snprintf(buf+strlen,buflen-strlen, "\n\t\tcircuit status vector (%u) length: %u: 0x",
527 tlv_type,
528 tlv_len);
529 ttlv_len=ttlv_len/8+1; /* how many bytes do we need to read ? */
530 while (ttlv_len>0) {
531 strlen+=snprintf(buf+strlen,buflen-strlen, "%02x",*pptr++);
532 ttlv_len--;
533 }
534 break;
535 default:
536 snprintf(buf+strlen,buflen-strlen, "\n\t\tunknown TLV #%u, length: %u",
537 tlv_type,
538 tlv_len);
539 break;
540 }
541 tlen-=(tlv_len<<3); /* the tlv-length is expressed in bits so lets shift it tright */
542 }
543 return plen+2;
544 }
545
546 #ifdef INET6
547 static int
548 decode_prefix6(const u_char *pd, char *buf, u_int buflen)
549 {
550 struct in6_addr addr;
551 u_int plen;
552
553 plen = pd[0];
554 if (128 < plen)
555 return -1;
556
557 memset(&addr, 0, sizeof(addr));
558 memcpy(&addr, &pd[1], (plen + 7) / 8);
559 if (plen % 8) {
560 addr.s6_addr[(plen + 7) / 8 - 1] &=
561 ((0xff00 >> (plen % 8)) & 0xff);
562 }
563 snprintf(buf, buflen, "%s/%d", getname6((u_char *)&addr), plen);
564 return 1 + (plen + 7) / 8;
565 }
566
567 static int
568 decode_labeled_prefix6(const u_char *pptr, char *buf, u_int buflen)
569 {
570 struct in6_addr addr;
571 u_int plen;
572
573 plen = pptr[0]; /* get prefix length */
574 plen-=24; /* adjust prefixlen - labellength */
575
576 if (128 < plen)
577 return -1;
578
579 memset(&addr, 0, sizeof(addr));
580 memcpy(&addr, &pptr[4], (plen + 7) / 8);
581 if (plen % 8) {
582 addr.s6_addr[(plen + 7) / 8 - 1] &=
583 ((0xff00 >> (plen % 8)) & 0xff);
584 }
585 /* the label may get offsetted by 4 bits so lets shift it right */
586 snprintf(buf, buflen, "%s/%d, label:%u %s",
587 getname6((u_char *)&addr),
588 plen,
589 EXTRACT_24BITS(pptr+1)>>4,
590 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
591
592 return 4 + (plen + 7) / 8;
593 }
594
595 static int
596 decode_labeled_vpn_prefix6(const u_char *pptr, char *buf, u_int buflen)
597 {
598 struct in6_addr addr;
599 u_int plen;
600
601 plen = pptr[0]; /* get prefix length */
602
603 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
604
605 if (128 < plen)
606 return -1;
607
608 memset(&addr, 0, sizeof(addr));
609 memcpy(&addr, &pptr[12], (plen + 7) / 8);
610 if (plen % 8) {
611 addr.s6_addr[(plen + 7) / 8 - 1] &=
612 ((0xff00 >> (plen % 8)) & 0xff);
613 }
614 /* the label may get offsetted by 4 bits so lets shift it right */
615 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
616 bgp_vpn_rd_print(pptr+4),
617 getname6((u_char *)&addr),
618 plen,
619 EXTRACT_24BITS(pptr+1)>>4,
620 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
621
622 return 12 + (plen + 7) / 8;
623 }
624 #endif
625
626 static void
627 bgp_attr_print(const struct bgp_attr *attr, const u_char *pptr, int len)
628 {
629 int i;
630 u_int16_t af;
631 u_int8_t safi, snpa;
632 float bw; /* copy buffer for bandwidth values */
633 int advance;
634 int tlen;
635 const u_char *tptr;
636 char buf[MAXHOSTNAMELEN + 100];
637
638 tptr = pptr;
639 tlen=len;
640
641 switch (attr->bgpa_type) {
642 case BGPTYPE_ORIGIN:
643 if (len != 1)
644 printf("invalid len");
645 else
646 printf("%s", tok2str(bgp_origin_values, "Unknown Origin Typecode", tptr[0]));
647 break;
648 case BGPTYPE_AS_PATH:
649 if (len % 2) {
650 printf("invalid len");
651 break;
652 }
653 if (!len) {
654 printf("empty");
655 break;
656 }
657 while (tptr < pptr + len) {
658 /*
659 * under RFC1965, p[0] means:
660 * 1: AS_SET 2: AS_SEQUENCE
661 * 3: AS_CONFED_SET 4: AS_CONFED_SEQUENCE
662 */
663 if (tptr[0] == 3 || tptr[0] == 4)
664 printf("confed");
665 printf("%s", (tptr[0] & 1) ? "{" : "");
666 for (i = 0; i < tptr[1] * 2; i += 2) {
667 printf("%s%u", i == 0 ? "" : " ",
668 EXTRACT_16BITS(&tptr[2 + i]));
669 }
670 printf("%s", (tptr[0] & 1) ? "}" : "");
671 tptr += 2 + tptr[1] * 2;
672 }
673 break;
674 case BGPTYPE_NEXT_HOP:
675 if (len != 4)
676 printf("invalid len");
677 else
678 printf("%s", getname(tptr));
679 break;
680 case BGPTYPE_MULTI_EXIT_DISC:
681 case BGPTYPE_LOCAL_PREF:
682 if (len != 4)
683 printf("invalid len");
684 else
685 printf("%u", EXTRACT_32BITS(tptr));
686 break;
687 case BGPTYPE_ATOMIC_AGGREGATE:
688 if (len != 0)
689 printf("invalid len");
690 break;
691 case BGPTYPE_AGGREGATOR:
692 if (len != 6) {
693 printf("invalid len");
694 break;
695 }
696 printf(" AS #%u, origin %s", EXTRACT_16BITS(tptr),
697 getname(tptr + 2));
698 break;
699 case BGPTYPE_COMMUNITIES:
700 if (len % 4) {
701 printf("invalid len");
702 break;
703 }
704 while (tlen>0) {
705 u_int32_t comm;
706 comm = EXTRACT_32BITS(tptr);
707 switch (comm) {
708 case BGP_COMMUNITY_NO_EXPORT:
709 printf(" NO_EXPORT");
710 break;
711 case BGP_COMMUNITY_NO_ADVERT:
712 printf(" NO_ADVERTISE");
713 break;
714 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED:
715 printf(" NO_EXPORT_SUBCONFED");
716 break;
717 default:
718 printf("%u:%u%s",
719 (comm >> 16) & 0xffff,
720 comm & 0xffff,
721 (tlen>4) ? ", " : "");
722 break;
723 }
724 tlen -=4;
725 tptr +=4;
726 }
727 break;
728 case BGPTYPE_ORIGINATOR_ID:
729 if (len != 4) {
730 printf("invalid len");
731 break;
732 }
733 printf("%s",getname(tptr));
734 break;
735 case BGPTYPE_CLUSTER_LIST:
736 while (tlen>0) {
737 printf("%s%s",
738 getname(tptr),
739 (tlen>4) ? ", " : "");
740 tlen -=4;
741 tptr +=4;
742 }
743 break;
744 case BGPTYPE_MP_REACH_NLRI:
745 af = EXTRACT_16BITS(tptr);
746 safi = tptr[2];
747
748 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
749 tok2str(bgp_afi_values, "Unknown AFI", af),
750 af,
751 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
752 tok2str(bgp_safi_values, "Unknown SAFI", safi),
753 safi);
754
755 if (af == AFNUM_INET || af==AFNUM_L2VPN)
756 ;
757 #ifdef INET6
758 else if (af == AFNUM_INET6)
759 ;
760 #endif
761 else {
762 printf("\n\t no AFI %u decoder",af);
763 if (vflag <= 1)
764 print_unknown_data(tptr,"\n\t ",tlen);
765 break;
766 }
767
768 tptr +=3;
769
770 tlen = tptr[0];
771 tptr++;
772
773 if (tlen) {
774 printf("\n\t nexthop: ");
775 while (tlen > 0) {
776 switch (af) {
777 case AFNUM_INET:
778 switch(safi) {
779 case SAFNUM_UNICAST:
780 case SAFNUM_MULTICAST:
781 case SAFNUM_UNIMULTICAST:
782 case SAFNUM_LABUNICAST:
783 printf("%s",getname(tptr));
784 tlen -= sizeof(struct in_addr);
785 tptr += sizeof(struct in_addr);
786 break;
787 case SAFNUM_VPNUNICAST:
788 case SAFNUM_VPNMULTICAST:
789 case SAFNUM_VPNUNIMULTICAST:
790 printf("RD: %s, %s",
791 bgp_vpn_rd_print(tptr),
792 getname(tptr+BGP_VPN_RD_LEN));
793 tlen -= (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
794 tptr += (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
795 break;
796 default:
797 printf("no SAFI %u decoder",safi);
798 if (vflag <= 1)
799 print_unknown_data(tptr,"\n\t ",tlen);
800 break;
801 }
802 break;
803 #ifdef INET6
804 case AFNUM_INET6:
805 switch(safi) {
806 case SAFNUM_UNICAST:
807 case SAFNUM_MULTICAST:
808 case SAFNUM_UNIMULTICAST:
809 case SAFNUM_LABUNICAST:
810 printf("%s", getname6(tptr));
811 tlen -= sizeof(struct in6_addr);
812 tptr += sizeof(struct in6_addr);
813 break;
814 case SAFNUM_VPNUNICAST:
815 case SAFNUM_VPNMULTICAST:
816 case SAFNUM_VPNUNIMULTICAST:
817 printf("RD: %s, %s",
818 bgp_vpn_rd_print(tptr),
819 getname6(tptr+BGP_VPN_RD_LEN));
820 tlen -= (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
821 tptr += (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
822 break;
823 default:
824 printf("no SAFI %u decoder",safi);
825 if (vflag <= 1)
826 print_unknown_data(tptr,"\n\t ",tlen);
827 break;
828 }
829 break;
830 #endif
831 case AFNUM_L2VPN:
832 switch(safi) {
833 case SAFNUM_VPNUNICAST:
834 case SAFNUM_VPNMULTICAST:
835 case SAFNUM_VPNUNIMULTICAST:
836 printf("%s", getname(tptr));
837 tlen -= (sizeof(struct in_addr));
838 tptr += (sizeof(struct in_addr));
839 break;
840 default:
841 printf("no SAFI %u decoder",safi);
842 if (vflag <= 1)
843 print_unknown_data(tptr,"\n\t ",tlen);
844 break;
845 }
846 break;
847
848 default:
849 printf("no AFI %u decoder",af);
850 if (vflag <= 1)
851 print_unknown_data(tptr,"\n\t ",tlen);
852 break;
853 }
854 }
855 }
856 tptr += tlen;
857
858 snpa = tptr[0];
859 tptr++;
860
861 if (snpa) {
862 printf("\n\t %u SNPA", snpa);
863 for (/*nothing*/; snpa > 0; snpa--) {
864 printf("\n\t %d bytes", tptr[0]);
865 tptr += tptr[0] + 1;
866 }
867 } else {
868 printf(", no SNPA");
869 }
870
871 while (len - (tptr - pptr) > 0) {
872 switch (af) {
873 case AFNUM_INET:
874 switch (safi) {
875 case SAFNUM_UNICAST:
876 case SAFNUM_MULTICAST:
877 case SAFNUM_UNIMULTICAST:
878 advance = decode_prefix4(tptr, buf, sizeof(buf));
879 if (advance >= 0)
880 printf("\n\t %s", buf);
881 else
882 printf("\n\t (illegal prefix length)");
883 break;
884 case SAFNUM_LABUNICAST:
885 advance = decode_labeled_prefix4(tptr, buf, sizeof(buf));
886 printf("\n\t %s", buf);
887 break;
888 case SAFNUM_VPNUNICAST:
889 case SAFNUM_VPNMULTICAST:
890 case SAFNUM_VPNUNIMULTICAST:
891 advance = decode_labeled_vpn_prefix4(tptr, buf, sizeof(buf));
892 printf("\n\t %s", buf);
893 break;
894 default:
895 printf("\n\t no SAFI %u decoder",safi);
896 if (vflag <= 1)
897 print_unknown_data(tptr-3,"\n\t ",tlen);
898 advance = 0;
899 tptr = pptr + len;
900 break;
901 }
902 break;
903 #ifdef INET6
904 case AFNUM_INET6:
905 switch (safi) {
906 case SAFNUM_UNICAST:
907 case SAFNUM_MULTICAST:
908 case SAFNUM_UNIMULTICAST:
909 advance = decode_prefix6(tptr, buf, sizeof(buf));
910 printf("\n\t %s", buf);
911 break;
912 case SAFNUM_LABUNICAST:
913 advance = decode_labeled_prefix6(tptr, buf, sizeof(buf));
914 printf("\n\t %s", buf);
915 break;
916 case SAFNUM_VPNUNICAST:
917 case SAFNUM_VPNMULTICAST:
918 case SAFNUM_VPNUNIMULTICAST:
919 advance = decode_labeled_vpn_prefix6(tptr, buf, sizeof(buf));
920 printf("\n\t %s", buf);
921 break;
922 default:
923 printf("\n\t no SAFI %u decoder ",safi);
924 if (vflag <= 1)
925 print_unknown_data(tptr-3,"\n\t ",tlen);
926 advance = 0;
927 tptr = pptr + len;
928 break;
929 }
930 break;
931 #endif
932 case AFNUM_L2VPN:
933 switch(safi) {
934 case SAFNUM_VPNUNICAST:
935 case SAFNUM_VPNMULTICAST:
936 case SAFNUM_VPNUNIMULTICAST:
937 advance = decode_labeled_vpn_l2(tptr, buf, sizeof(buf));
938 printf("\n\t %s", buf);
939 break;
940 default:
941 printf("no SAFI %u decoder",safi);
942 if (vflag <= 1)
943 print_unknown_data(tptr,"\n\t ",tlen);
944 advance = 0;
945 tptr = pptr + len;
946 break;
947 }
948 break;
949
950
951 default:
952 printf("\n\t no AFI %u decoder ",af);
953 if (vflag <= 1)
954 print_unknown_data(tptr-3,"\n\t ",tlen);
955 advance = 0;
956 tptr = pptr + len;
957 break;
958 }
959 tptr += advance;
960 }
961 break;
962
963 case BGPTYPE_MP_UNREACH_NLRI:
964 af = EXTRACT_16BITS(tptr);
965 safi = tptr[2];
966
967 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
968 tok2str(bgp_afi_values, "Unknown AFI", af),
969 af,
970 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
971 tok2str(bgp_safi_values, "Unknown SAFI", safi),
972 safi);
973
974 tptr += 3;
975
976 while (len - (tptr - pptr) > 0) {
977 switch (af) {
978 case AFNUM_INET:
979 switch (safi) {
980 case SAFNUM_UNICAST:
981 case SAFNUM_MULTICAST:
982 case SAFNUM_UNIMULTICAST:
983 advance = decode_prefix4(tptr, buf, sizeof(buf));
984 if (advance >= 0)
985 printf("\n\t %s", buf);
986 else
987 printf("\n\t (illegal prefix length)");
988 break;
989 case SAFNUM_LABUNICAST:
990 advance = decode_labeled_prefix4(tptr, buf, sizeof(buf));
991 printf("\n\t %s", buf);
992 break;
993 case SAFNUM_VPNUNICAST:
994 case SAFNUM_VPNMULTICAST:
995 case SAFNUM_VPNUNIMULTICAST:
996 advance = decode_labeled_vpn_prefix4(tptr, buf, sizeof(buf));
997 printf("\n\t %s", buf);
998 break;
999 default:
1000 printf("\n\t no SAFI %u decoder",safi);
1001 if (vflag <= 1)
1002 print_unknown_data(tptr-3,"\n\t ",tlen);
1003 advance = 0;
1004 tptr = pptr + len;
1005 break;
1006 }
1007 break;
1008
1009 #ifdef INET6
1010 case AFNUM_INET6:
1011 switch (safi) {
1012 case SAFNUM_UNICAST:
1013 case SAFNUM_MULTICAST:
1014 case SAFNUM_UNIMULTICAST:
1015 advance = decode_prefix6(tptr, buf, sizeof(buf));
1016 printf("\n\t %s", buf);
1017 break;
1018 case SAFNUM_LABUNICAST:
1019 advance = decode_labeled_prefix6(tptr, buf, sizeof(buf));
1020 printf("\n\t %s", buf);
1021 break;
1022 case SAFNUM_VPNUNICAST:
1023 case SAFNUM_VPNMULTICAST:
1024 case SAFNUM_VPNUNIMULTICAST:
1025 advance = decode_labeled_vpn_prefix6(tptr, buf, sizeof(buf));
1026 printf("\n\t %s", buf);
1027 break;
1028 default:
1029 printf("\n\t no SAFI %u decoder",safi);
1030 if (vflag <= 1)
1031 print_unknown_data(tptr-3,"\n\t ",tlen);
1032 advance = 0;
1033 tptr = pptr + len;
1034 break;
1035 }
1036 break;
1037 #endif
1038
1039 case AFNUM_L2VPN:
1040 switch(safi) {
1041 case SAFNUM_VPNUNICAST:
1042 case SAFNUM_VPNMULTICAST:
1043 case SAFNUM_VPNUNIMULTICAST:
1044 advance = decode_labeled_vpn_l2(tptr, buf, sizeof(buf));
1045 printf("\n\t %s", buf);
1046 break;
1047 default:
1048 printf("no SAFI %u decoder",safi);
1049 if (vflag <= 1)
1050 print_unknown_data(tptr-3,"\n\t ",tlen);
1051 advance = 0;
1052 tptr = pptr + len;
1053 break;
1054 }
1055 break;
1056
1057 default:
1058 printf("\n\t no AFI %u decoder",af);
1059 if (vflag <= 1)
1060 print_unknown_data(tptr-3,"\n\t ",tlen);
1061 advance = 0;
1062 tptr = pptr + len;
1063 break;
1064 }
1065
1066 tptr += advance;
1067 }
1068 break;
1069 case BGPTYPE_EXTD_COMMUNITIES:
1070 if (len % 8) {
1071 printf("invalid len");
1072 break;
1073 }
1074 while (tlen>0) {
1075 u_int16_t extd_comm;
1076 extd_comm=EXTRACT_16BITS(tptr);
1077 switch(extd_comm) {
1078 case BGP_EXT_COM_RT_0:
1079 case BGP_EXT_COM_RO_0:
1080 printf("\n\t %s%s%s:%u:%s",
1081 (extd_comm&0x8000) ? "vendor-specific: " : "",
1082 (extd_comm&0x4000) ? "non-transitive: " : "",
1083 tok2str(bgp_extd_comm_subtype_values,
1084 "unknown",
1085 extd_comm),
1086 EXTRACT_16BITS(tptr+2),
1087 getname(tptr+4));
1088 break;
1089 case BGP_EXT_COM_RT_1:
1090 case BGP_EXT_COM_RO_1:
1091 printf("\n\t %s%s%s:%s:%u",
1092 (extd_comm&0x8000) ? "vendor-specific: " : "",
1093 (extd_comm&0x4000) ? "non-transitive: " : "",
1094 tok2str(bgp_extd_comm_subtype_values,
1095 "unknown",
1096 extd_comm),
1097 getname(tptr+2),
1098 EXTRACT_16BITS(tptr+6));
1099 break;
1100 case BGP_EXT_COM_LINKBAND:
1101 memcpy (&bw, tptr+2, 4);
1102 printf("\n\t %s%s%s:bandwidth: %.3f Mbps",
1103 (extd_comm&0x8000) ? "vendor-specific: " : "",
1104 (extd_comm&0x4000) ? "non-transitive: " : "",
1105 tok2str(bgp_extd_comm_subtype_values,
1106 "unknown",
1107 extd_comm),
1108 bw*8/1000000);
1109 break;
1110 case BGP_EXT_COM_VPN_ORIGIN:
1111 case BGP_EXT_COM_OSPF_RID:
1112 printf("\n\t %s%s%s:%s",
1113 (extd_comm&0x8000) ? "vendor-specific: " : "",
1114 (extd_comm&0x4000) ? "non-transitive: " : "",
1115 tok2str(bgp_extd_comm_subtype_values,
1116 "unknown",
1117 extd_comm),
1118 getname(tptr+2));
1119 break;
1120 case BGP_EXT_COM_OSPF_RTYPE:
1121 printf("\n\t %s%s%s, area:%s, router-type:%s, metric-type:%s%s",
1122 (extd_comm&0x8000) ? "vendor-specific: " : "",
1123 (extd_comm&0x4000) ? "non-transitive: " : "",
1124 tok2str(bgp_extd_comm_subtype_values,
1125 "unknown",
1126 extd_comm),
1127 getname(tptr+2),
1128 tok2str(bgp_extd_comm_ospf_rtype_values,
1129 "unknown",
1130 extd_comm),
1131 (*(tptr+7) & BGP_OSPF_RTYPE_METRIC_TYPE) ? "E2" : "",
1132 (*(tptr+6) == (BGP_OSPF_RTYPE_EXT ||BGP_OSPF_RTYPE_NSSA )) ? "E1" : "");
1133 break;
1134 case BGP_EXT_COM_L2INFO:
1135 printf("\n\t %s%s:%s:Control Flags [0x%02x]:MTU %u",
1136 (extd_comm&0x4000) ? "non-transitive: " : "",
1137 tok2str(bgp_extd_comm_subtype_values,
1138 "unknown",
1139 extd_comm),
1140 tok2str(bgp_l2vpn_encaps_values,
1141 "unknown encaps",
1142 *(tptr+2)),
1143 *(tptr+3),
1144 EXTRACT_16BITS(tptr+4));
1145 break;
1146 default:
1147 printf("\n\t no typecode %u decoder",
1148 extd_comm);
1149 print_unknown_data(tptr,"\n\t ",8);
1150 break;
1151 }
1152 tlen -=8;
1153 tptr +=8;
1154 }
1155 break;
1156
1157 default:
1158 printf("\n\t no Attribute %u decoder",attr->bgpa_type); /* we have no decoder for the attribute */
1159 if (vflag <= 1)
1160 print_unknown_data(pptr,"\n\t ",len);
1161 break;
1162 }
1163 if (vflag > 1 && len) /* omit zero length attributes*/
1164 print_unknown_data(pptr,"\n\t ",len);
1165 }
1166
1167 static void
1168 bgp_open_print(const u_char *dat, int length)
1169 {
1170 struct bgp_open bgpo;
1171 struct bgp_opt bgpopt;
1172 int hlen;
1173 const u_char *opt;
1174 int i,cap_type,cap_len,tcap_len,cap_offset;
1175
1176 TCHECK2(dat[0], BGP_OPEN_SIZE);
1177 memcpy(&bgpo, dat, BGP_OPEN_SIZE);
1178 hlen = ntohs(bgpo.bgpo_len);
1179
1180 printf("\n\t Version %d, ", bgpo.bgpo_version);
1181 printf("my AS %u, ", ntohs(bgpo.bgpo_myas));
1182 printf("Holdtime %us, ", ntohs(bgpo.bgpo_holdtime));
1183 printf("ID %s", getname((u_char *)&bgpo.bgpo_id));
1184 printf("\n\t Optional parameters, length: %u", bgpo.bgpo_optlen);
1185
1186 /* some little sanity checking */
1187 if (length < bgpo.bgpo_optlen+BGP_OPEN_SIZE)
1188 return;
1189
1190 /* ugly! */
1191 opt = &((const struct bgp_open *)dat)->bgpo_optlen;
1192 opt++;
1193
1194 i = 0;
1195 while (i < bgpo.bgpo_optlen) {
1196 TCHECK2(opt[i], BGP_OPT_SIZE);
1197 memcpy(&bgpopt, &opt[i], BGP_OPT_SIZE);
1198 if (i + 2 + bgpopt.bgpopt_len > bgpo.bgpo_optlen) {
1199 printf("\n\t Option %d, length: %u", bgpopt.bgpopt_type, bgpopt.bgpopt_len);
1200 break;
1201 }
1202
1203 printf("\n\t Option %s (%u), length: %u",
1204 tok2str(bgp_opt_values,"Unknown", bgpopt.bgpopt_type),
1205 bgpopt.bgpopt_type,
1206 bgpopt.bgpopt_len);
1207
1208 /* now lets decode the options we know*/
1209 switch(bgpopt.bgpopt_type) {
1210 case BGP_OPT_CAP:
1211 cap_type=opt[i+BGP_OPT_SIZE];
1212 cap_len=opt[i+BGP_OPT_SIZE+1];
1213 tcap_len=cap_len;
1214 printf("\n\t %s, length: %u",
1215 tok2str(bgp_capcode_values,"Unknown", cap_type),
1216 cap_len);
1217 switch(cap_type) {
1218 case BGP_CAPCODE_MP:
1219 printf("\n\t\tAFI %s (%u), SAFI %s (%u)",
1220 tok2str(bgp_afi_values,"Unknown", EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+2)),
1221 EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+2),
1222 tok2str(bgp_safi_values,"Unknown", opt[i+BGP_OPT_SIZE+5]),
1223 opt[i+BGP_OPT_SIZE+5]);
1224 break;
1225 case BGP_CAPCODE_RESTART:
1226 printf("\n\t\tRestart Flags: [%s], Restart Time %us",
1227 ((opt[i+BGP_OPT_SIZE+2])&0x80) ? "R" : "none",
1228 EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+2)&0xfff);
1229 tcap_len-=2;
1230 cap_offset=4;
1231 while(tcap_len>=4) {
1232 printf("\n\t\t AFI %s (%u), SAFI %s (%u), Forwarding state preserved: %s",
1233 tok2str(bgp_afi_values,"Unknown", EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+cap_offset)),
1234 EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+cap_offset),
1235 tok2str(bgp_safi_values,"Unknown", opt[i+BGP_OPT_SIZE+cap_offset+2]),
1236 opt[i+BGP_OPT_SIZE+cap_offset+2],
1237 ((opt[i+BGP_OPT_SIZE+cap_offset+3])&0x80) ? "yes" : "no" );
1238 tcap_len-=4;
1239 cap_offset+=4;
1240 }
1241 break;
1242 case BGP_CAPCODE_RR:
1243 case BGP_CAPCODE_RR_CISCO:
1244 break;
1245 default:
1246 printf("\n\t\tno decoder for Capability %u",
1247 cap_type);
1248 if (vflag <= 1)
1249 print_unknown_data(&opt[i+BGP_OPT_SIZE+2],"\n\t\t",cap_len);
1250 break;
1251 }
1252 if (vflag > 1)
1253 print_unknown_data(&opt[i+BGP_OPT_SIZE+2],"\n\t\t",cap_len);
1254 break;
1255 case BGP_OPT_AUTH:
1256 default:
1257 printf("\n\t no decoder for option %u",
1258 bgpopt.bgpopt_type);
1259 break;
1260 }
1261
1262 i += BGP_OPT_SIZE + bgpopt.bgpopt_len;
1263 }
1264 return;
1265 trunc:
1266 printf("[|BGP]");
1267 }
1268
1269 static void
1270 bgp_update_print(const u_char *dat, int length)
1271 {
1272 struct bgp bgp;
1273 struct bgp_attr bgpa;
1274 int hlen;
1275 const u_char *p;
1276 int len;
1277 int i;
1278
1279 TCHECK2(dat[0], BGP_SIZE);
1280 memcpy(&bgp, dat, BGP_SIZE);
1281 hlen = ntohs(bgp.bgp_len);
1282 p = dat + BGP_SIZE; /*XXX*/
1283
1284 /* Unfeasible routes */
1285 len = EXTRACT_16BITS(p);
1286 if (len) {
1287 /*
1288 * Without keeping state from the original NLRI message,
1289 * it's not possible to tell if this a v4 or v6 route,
1290 * so only try to decode it if we're not v6 enabled.
1291 */
1292 #ifdef INET6
1293 printf("\n\t Withdrawn routes: %d bytes", len);
1294 #else
1295 char buf[MAXHOSTNAMELEN + 100];
1296 int wpfx;
1297
1298 TCHECK2(p[2], len);
1299 i = 2;
1300
1301 printf("\n\t Withdrawn routes:");
1302
1303 while(i < 2 + len) {
1304 wpfx = decode_prefix4(&p[i], buf, sizeof(buf));
1305 if (wpfx >= 0) {
1306 i += wpfx;
1307 printf("\n\t %s", buf);
1308 } else {
1309 printf("\n\t (illegal prefix length)");
1310 break;
1311 }
1312 }
1313 #endif
1314 }
1315 p += 2 + len;
1316
1317 TCHECK2(p[0], 2);
1318 len = EXTRACT_16BITS(p);
1319 if (len) {
1320 /* do something more useful!*/
1321 i = 2;
1322 while (i < 2 + len) {
1323 int alen, aoff;
1324
1325 TCHECK2(p[i], sizeof(bgpa));
1326 memcpy(&bgpa, &p[i], sizeof(bgpa));
1327 alen = bgp_attr_len(&bgpa);
1328 aoff = bgp_attr_off(&bgpa);
1329
1330 printf("\n\t %s (%u), length: %u",
1331 tok2str(bgp_attr_values, "Unknown Attribute", bgpa.bgpa_type),
1332 bgpa.bgpa_type,
1333 alen);
1334
1335 if (bgpa.bgpa_flags) {
1336 printf(", flags [%s%s%s%s",
1337 bgpa.bgpa_flags & 0x80 ? "O" : "",
1338 bgpa.bgpa_flags & 0x40 ? "T" : "",
1339 bgpa.bgpa_flags & 0x20 ? "P" : "",
1340 bgpa.bgpa_flags & 0x10 ? "E" : "");
1341 if (bgpa.bgpa_flags & 0xf)
1342 printf("+%x", bgpa.bgpa_flags & 0xf);
1343 printf("]: ");
1344 }
1345 bgp_attr_print(&bgpa, &p[i + aoff], alen);
1346 i += aoff + alen;
1347 }
1348 }
1349 p += 2 + len;
1350
1351 if (dat + length > p) {
1352 printf("\n\t Updated routes:");
1353 while (dat + length > p) {
1354 char buf[MAXHOSTNAMELEN + 100];
1355 i = decode_prefix4(p, buf, sizeof(buf));
1356 if (i >= 0) {
1357 printf("\n\t %s", buf);
1358 p += i;
1359 } else {
1360 printf("\n\t (illegal prefix length)");
1361 break;
1362 }
1363 }
1364 }
1365 return;
1366 trunc:
1367 printf("[|BGP]");
1368 }
1369
1370 static void
1371 bgp_notification_print(const u_char *dat, int length)
1372 {
1373 struct bgp_notification bgpn;
1374 int hlen;
1375
1376 TCHECK2(dat[0], BGP_NOTIFICATION_SIZE);
1377 memcpy(&bgpn, dat, BGP_NOTIFICATION_SIZE);
1378 hlen = ntohs(bgpn.bgpn_len);
1379
1380 /* some little sanity checking */
1381 if (length<BGP_NOTIFICATION_SIZE)
1382 return;
1383
1384 printf(", Error - %s", tok2str(bgp_notify_major_values, "Unknown", bgpn.bgpn_major));
1385
1386 switch (bgpn.bgpn_major) {
1387
1388 case BGP_NOTIFY_MAJOR_MSG:
1389 printf(" subcode %s", tok2str(bgp_notify_minor_msg_values, "Unknown", bgpn.bgpn_minor));
1390 break;
1391 case BGP_NOTIFY_MAJOR_OPEN:
1392 printf(" subcode %s", tok2str(bgp_notify_minor_open_values, "Unknown", bgpn.bgpn_minor));
1393 break;
1394 case BGP_NOTIFY_MAJOR_UPDATE:
1395 printf(" subcode %s", tok2str(bgp_notify_minor_update_values, "Unknown", bgpn.bgpn_minor));
1396 break;
1397 default:
1398 break;
1399 }
1400
1401 return;
1402 trunc:
1403 printf("[|BGP]");
1404 }
1405
1406 static void
1407 bgp_route_refresh_print(const u_char *pptr, int len) {
1408
1409 const struct bgp_route_refresh *bgp_route_refresh_header;
1410 bgp_route_refresh_header = (const struct bgp_route_refresh *)pptr;
1411
1412 printf("\n\t AFI %s (%u), SAFI %s (%u)",
1413 tok2str(bgp_afi_values,"Unknown",
1414 EXTRACT_16BITS(&bgp_route_refresh_header->afi)), /* this stinks but the compiler pads the structure weird */
1415 EXTRACT_16BITS(&bgp_route_refresh_header->afi),
1416 tok2str(bgp_safi_values,"Unknown",
1417 bgp_route_refresh_header->safi),
1418 bgp_route_refresh_header->safi);
1419
1420 if (vflag > 1)
1421 print_unknown_data(pptr,"\n\t ", len);
1422
1423 return;
1424 }
1425
1426 static void
1427 bgp_header_print(const u_char *dat, int length)
1428 {
1429 struct bgp bgp;
1430
1431 TCHECK2(dat[0], BGP_SIZE);
1432 memcpy(&bgp, dat, BGP_SIZE);
1433 printf("\n\t%s Message (%u), length: %u",
1434 tok2str(bgp_msg_values, "Unknown", bgp.bgp_type),
1435 bgp.bgp_type,
1436 length);
1437
1438 switch (bgp.bgp_type) {
1439 case BGP_OPEN:
1440 bgp_open_print(dat, length);
1441 break;
1442 case BGP_UPDATE:
1443 bgp_update_print(dat, length);
1444 break;
1445 case BGP_NOTIFICATION:
1446 bgp_notification_print(dat, length);
1447 break;
1448 case BGP_KEEPALIVE:
1449 break;
1450 case BGP_ROUTE_REFRESH:
1451 bgp_route_refresh_print(dat, length);
1452 break;
1453 default:
1454 /* we have no decoder for the BGP message */
1455 printf("\n\t no Message %u decoder",bgp.bgp_type);
1456 print_unknown_data(dat,"\n\t ",length);
1457 break;
1458 }
1459 return;
1460 trunc:
1461 printf("[|BGP]");
1462 }
1463
1464 void
1465 bgp_print(const u_char *dat, int length)
1466 {
1467 const u_char *p;
1468 const u_char *ep;
1469 const u_char *start;
1470 const u_char marker[] = {
1471 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1472 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1473 };
1474 struct bgp bgp;
1475 u_int16_t hlen;
1476
1477 ep = dat + length;
1478 if (snapend < dat + length)
1479 ep = snapend;
1480
1481 printf(": BGP, length: %u",length);
1482
1483 if (vflag < 1) /* lets be less chatty */
1484 return;
1485
1486 p = dat;
1487 start = p;
1488 while (p < snapend) {
1489 if (!TTEST2(p[0], 1))
1490 break;
1491 if (p[0] != 0xff) {
1492 p++;
1493 continue;
1494 }
1495
1496 if (!TTEST2(p[0], sizeof(marker)))
1497 break;
1498 if (memcmp(p, marker, sizeof(marker)) != 0) {
1499 p++;
1500 continue;
1501 }
1502
1503 /* found BGP header */
1504 TCHECK2(p[0], BGP_SIZE); /*XXX*/
1505 memcpy(&bgp, p, BGP_SIZE);
1506
1507 if (start != p)
1508 printf(" [|BGP]");
1509
1510 hlen = ntohs(bgp.bgp_len);
1511
1512 if (TTEST2(p[0], hlen)) {
1513 bgp_header_print(p, hlen);
1514 p += hlen;
1515 start = p;
1516 } else {
1517 printf("[|BGP %s]", tok2str(bgp_msg_values, "Unknown Message Type",bgp.bgp_type));
1518 break;
1519 }
1520 }
1521
1522 return;
1523
1524 trunc:
1525 printf(" [|BGP]");
1526 }
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537