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