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