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