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