Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / tcpdump / print-bgp.c
blob50cd4fa5c76ea241f91daf85a7dfca7b3f4ed7c0
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 1999 WIDE Project.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
31 * Extensively modified by Hannes Gredler (hannes@juniper.net) for more
32 * complete BGP support.
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
39 #include <sys/cdefs.h>
40 #ifndef lint
41 #if 0
42 static const char rcsid[] _U_ =
43 "@(#) Header: /tcpdump/master/tcpdump/print-bgp.c,v 1.91.2.12 2007/07/14 22:26:35 guy Exp";
44 #else
45 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
46 #endif
47 #endif
49 #include <tcpdump-stdinc.h>
51 #include <stdio.h>
52 #include <string.h>
54 #include "interface.h"
55 #include "decode_prefix.h"
56 #include "addrtoname.h"
57 #include "extract.h"
58 #include "bgp.h"
59 #include "l2vpn.h"
60 #include "af.h"
62 struct bgp {
63 u_int8_t bgp_marker[16];
64 u_int16_t bgp_len;
65 u_int8_t bgp_type;
67 #define BGP_SIZE 19 /* unaligned */
69 #define BGP_OPEN 1
70 #define BGP_UPDATE 2
71 #define BGP_NOTIFICATION 3
72 #define BGP_KEEPALIVE 4
73 #define BGP_ROUTE_REFRESH 5
75 static struct tok bgp_msg_values[] = {
76 { BGP_OPEN, "Open"},
77 { BGP_UPDATE, "Update"},
78 { BGP_NOTIFICATION, "Notification"},
79 { BGP_KEEPALIVE, "Keepalive"},
80 { BGP_ROUTE_REFRESH, "Route Refresh"},
81 { 0, NULL}
84 struct bgp_open {
85 u_int8_t bgpo_marker[16];
86 u_int16_t bgpo_len;
87 u_int8_t bgpo_type;
88 u_int8_t bgpo_version;
89 u_int16_t bgpo_myas;
90 u_int16_t bgpo_holdtime;
91 u_int32_t bgpo_id;
92 u_int8_t bgpo_optlen;
93 /* options should follow */
95 #define BGP_OPEN_SIZE 29 /* unaligned */
97 struct bgp_opt {
98 u_int8_t bgpopt_type;
99 u_int8_t bgpopt_len;
100 /* variable length */
102 #define BGP_OPT_SIZE 2 /* some compilers may pad to 4 bytes */
104 #define BGP_UPDATE_MINSIZE 23
106 struct bgp_notification {
107 u_int8_t bgpn_marker[16];
108 u_int16_t bgpn_len;
109 u_int8_t bgpn_type;
110 u_int8_t bgpn_major;
111 u_int8_t bgpn_minor;
113 #define BGP_NOTIFICATION_SIZE 21 /* unaligned */
115 struct bgp_route_refresh {
116 u_int8_t bgp_marker[16];
117 u_int16_t len;
118 u_int8_t type;
119 u_int8_t afi[2]; /* the compiler messes this structure up */
120 u_int8_t res; /* when doing misaligned sequences of int8 and int16 */
121 u_int8_t safi; /* afi should be int16 - so we have to access it using */
122 }; /* EXTRACT_16BITS(&bgp_route_refresh->afi) (sigh) */
123 #define BGP_ROUTE_REFRESH_SIZE 23
125 struct bgp_attr {
126 u_int8_t bgpa_flags;
127 u_int8_t bgpa_type;
128 union {
129 u_int8_t len;
130 u_int16_t elen;
131 } bgpa_len;
132 #define bgp_attr_len(p) \
133 (((p)->bgpa_flags & 0x10) ? \
134 EXTRACT_16BITS(&(p)->bgpa_len.elen) : (p)->bgpa_len.len)
135 #define bgp_attr_off(p) \
136 (((p)->bgpa_flags & 0x10) ? 4 : 3)
139 #define BGPTYPE_ORIGIN 1
140 #define BGPTYPE_AS_PATH 2
141 #define BGPTYPE_NEXT_HOP 3
142 #define BGPTYPE_MULTI_EXIT_DISC 4
143 #define BGPTYPE_LOCAL_PREF 5
144 #define BGPTYPE_ATOMIC_AGGREGATE 6
145 #define BGPTYPE_AGGREGATOR 7
146 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
147 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC1998 */
148 #define BGPTYPE_CLUSTER_LIST 10 /* RFC1998 */
149 #define BGPTYPE_DPA 11 /* draft-ietf-idr-bgp-dpa */
150 #define BGPTYPE_ADVERTISERS 12 /* RFC1863 */
151 #define BGPTYPE_RCID_PATH 13 /* RFC1863 */
152 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */
153 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */
154 #define BGPTYPE_EXTD_COMMUNITIES 16 /* draft-ietf-idr-bgp-ext-communities */
155 #define BGPTYPE_ATTR_SET 128 /* draft-marques-ppvpn-ibgp */
157 #define BGP_MP_NLRI_MINSIZE 3 /* End of RIB Marker detection */
159 static struct tok bgp_attr_values[] = {
160 { BGPTYPE_ORIGIN, "Origin"},
161 { BGPTYPE_AS_PATH, "AS Path"},
162 { BGPTYPE_NEXT_HOP, "Next Hop"},
163 { BGPTYPE_MULTI_EXIT_DISC, "Multi Exit Discriminator"},
164 { BGPTYPE_LOCAL_PREF, "Local Preference"},
165 { BGPTYPE_ATOMIC_AGGREGATE, "Atomic Aggregate"},
166 { BGPTYPE_AGGREGATOR, "Aggregator"},
167 { BGPTYPE_COMMUNITIES, "Community"},
168 { BGPTYPE_ORIGINATOR_ID, "Originator ID"},
169 { BGPTYPE_CLUSTER_LIST, "Cluster List"},
170 { BGPTYPE_DPA, "DPA"},
171 { BGPTYPE_ADVERTISERS, "Advertisers"},
172 { BGPTYPE_RCID_PATH, "RCID Path / Cluster ID"},
173 { BGPTYPE_MP_REACH_NLRI, "Multi-Protocol Reach NLRI"},
174 { BGPTYPE_MP_UNREACH_NLRI, "Multi-Protocol Unreach NLRI"},
175 { BGPTYPE_EXTD_COMMUNITIES, "Extended Community"},
176 { BGPTYPE_ATTR_SET, "Attribute Set"},
177 { 255, "Reserved for development"},
178 { 0, NULL}
181 #define BGP_AS_SET 1
182 #define BGP_AS_SEQUENCE 2
183 #define BGP_CONFED_AS_SEQUENCE 3 /* draft-ietf-idr-rfc3065bis-01 */
184 #define BGP_CONFED_AS_SET 4 /* draft-ietf-idr-rfc3065bis-01 */
186 static struct tok bgp_as_path_segment_open_values[] = {
187 { BGP_AS_SEQUENCE, ""},
188 { BGP_AS_SET, "{ "},
189 { BGP_CONFED_AS_SEQUENCE, "( "},
190 { BGP_CONFED_AS_SET, "({ "},
191 { 0, NULL}
194 static struct tok bgp_as_path_segment_close_values[] = {
195 { BGP_AS_SEQUENCE, ""},
196 { BGP_AS_SET, "}"},
197 { BGP_CONFED_AS_SEQUENCE, ")"},
198 { BGP_CONFED_AS_SET, "})"},
199 { 0, NULL}
202 #define BGP_OPT_AUTH 1
203 #define BGP_OPT_CAP 2
206 static struct tok bgp_opt_values[] = {
207 { BGP_OPT_AUTH, "Authentication Information"},
208 { BGP_OPT_CAP, "Capabilities Advertisement"},
209 { 0, NULL}
212 #define BGP_CAPCODE_MP 1
213 #define BGP_CAPCODE_RR 2
214 #define BGP_CAPCODE_ORF 3 /* XXX */
215 #define BGP_CAPCODE_RESTART 64 /* draft-ietf-idr-restart-05 */
216 #define BGP_CAPCODE_AS_NEW 65 /* XXX */
217 #define BGP_CAPCODE_DYN_CAP 67 /* XXX */
218 #define BGP_CAPCODE_RR_CISCO 128
220 static struct tok bgp_capcode_values[] = {
221 { BGP_CAPCODE_MP, "Multiprotocol Extensions"},
222 { BGP_CAPCODE_RR, "Route Refresh"},
223 { BGP_CAPCODE_ORF, "Cooperative Route Filtering"},
224 { BGP_CAPCODE_RESTART, "Graceful Restart"},
225 { BGP_CAPCODE_AS_NEW, "32-Bit AS Number"},
226 { BGP_CAPCODE_DYN_CAP, "Dynamic Capability"},
227 { BGP_CAPCODE_RR_CISCO, "Route Refresh (Cisco)"},
228 { 0, NULL}
231 #define BGP_NOTIFY_MAJOR_MSG 1
232 #define BGP_NOTIFY_MAJOR_OPEN 2
233 #define BGP_NOTIFY_MAJOR_UPDATE 3
234 #define BGP_NOTIFY_MAJOR_HOLDTIME 4
235 #define BGP_NOTIFY_MAJOR_FSM 5
236 #define BGP_NOTIFY_MAJOR_CEASE 6
237 #define BGP_NOTIFY_MAJOR_CAP 7
239 static struct tok bgp_notify_major_values[] = {
240 { BGP_NOTIFY_MAJOR_MSG, "Message Header Error"},
241 { BGP_NOTIFY_MAJOR_OPEN, "OPEN Message Error"},
242 { BGP_NOTIFY_MAJOR_UPDATE, "UPDATE Message Error"},
243 { BGP_NOTIFY_MAJOR_HOLDTIME,"Hold Timer Expired"},
244 { BGP_NOTIFY_MAJOR_FSM, "Finite State Machine Error"},
245 { BGP_NOTIFY_MAJOR_CEASE, "Cease"},
246 { BGP_NOTIFY_MAJOR_CAP, "Capability Message Error"},
247 { 0, NULL}
250 /* draft-ietf-idr-cease-subcode-02 */
251 #define BGP_NOTIFY_MINOR_CEASE_MAXPRFX 1
252 static struct tok bgp_notify_minor_cease_values[] = {
253 { BGP_NOTIFY_MINOR_CEASE_MAXPRFX, "Maximum Number of Prefixes Reached"},
254 { 2, "Administratively Shutdown"},
255 { 3, "Peer Unconfigured"},
256 { 4, "Administratively Reset"},
257 { 5, "Connection Rejected"},
258 { 6, "Other Configuration Change"},
259 { 7, "Connection Collision Resolution"},
260 { 0, NULL}
263 static struct tok bgp_notify_minor_msg_values[] = {
264 { 1, "Connection Not Synchronized"},
265 { 2, "Bad Message Length"},
266 { 3, "Bad Message Type"},
267 { 0, NULL}
270 static struct tok bgp_notify_minor_open_values[] = {
271 { 1, "Unsupported Version Number"},
272 { 2, "Bad Peer AS"},
273 { 3, "Bad BGP Identifier"},
274 { 4, "Unsupported Optional Parameter"},
275 { 5, "Authentication Failure"},
276 { 6, "Unacceptable Hold Time"},
277 { 0, NULL}
280 static struct tok bgp_notify_minor_update_values[] = {
281 { 1, "Malformed Attribute List"},
282 { 2, "Unrecognized Well-known Attribute"},
283 { 3, "Missing Well-known Attribute"},
284 { 4, "Attribute Flags Error"},
285 { 5, "Attribute Length Error"},
286 { 6, "Invalid ORIGIN Attribute"},
287 { 7, "AS Routing Loop"},
288 { 8, "Invalid NEXT_HOP Attribute"},
289 { 9, "Optional Attribute Error"},
290 { 10, "Invalid Network Field"},
291 { 11, "Malformed AS_PATH"},
292 { 0, NULL}
295 static struct tok bgp_notify_minor_cap_values[] = {
296 { 1, "Invalid Action Value" },
297 { 2, "Invalid Capability Length" },
298 { 3, "Malformed Capability Value" },
299 { 4, "Unsupported Capability Code" },
300 { 0, NULL }
303 static struct tok bgp_origin_values[] = {
304 { 0, "IGP"},
305 { 1, "EGP"},
306 { 2, "Incomplete"},
307 { 0, NULL}
310 /* Subsequent address family identifier, RFC2283 section 7 */
311 #define SAFNUM_RES 0
312 #define SAFNUM_UNICAST 1
313 #define SAFNUM_MULTICAST 2
314 #define SAFNUM_UNIMULTICAST 3
315 /* labeled BGP RFC3107 */
316 #define SAFNUM_LABUNICAST 4
317 #define SAFNUM_TUNNEL 64 /* XXX */
318 #define SAFNUM_VPLS 65 /* XXX */
319 #define SAFNUM_MDT 66 /* XXX */
320 /* Section 4.3.4 of draft-rosen-rfc2547bis-03.txt */
321 #define SAFNUM_VPNUNICAST 128
322 #define SAFNUM_VPNMULTICAST 129
323 #define SAFNUM_VPNUNIMULTICAST 130
324 /* draft-marques-ppvpn-rt-constrain-01.txt */
325 #define SAFNUM_RT_ROUTING_INFO 132
327 #define BGP_VPN_RD_LEN 8
329 static struct tok bgp_safi_values[] = {
330 { SAFNUM_RES, "Reserved"},
331 { SAFNUM_UNICAST, "Unicast"},
332 { SAFNUM_MULTICAST, "Multicast"},
333 { SAFNUM_UNIMULTICAST, "Unicast+Multicast"},
334 { SAFNUM_LABUNICAST, "labeled Unicast"},
335 { SAFNUM_TUNNEL, "Tunnel"},
336 { SAFNUM_VPLS, "VPLS"},
337 { SAFNUM_MDT, "MDT"},
338 { SAFNUM_VPNUNICAST, "labeled VPN Unicast"},
339 { SAFNUM_VPNMULTICAST, "labeled VPN Multicast"},
340 { SAFNUM_VPNUNIMULTICAST, "labeled VPN Unicast+Multicast"},
341 { SAFNUM_RT_ROUTING_INFO, "Route Target Routing Information"}, /* draft-marques-ppvpn-rt-constrain-01.txt */
342 { 0, NULL }
345 /* well-known community */
346 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
347 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
348 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
350 /* Extended community type - draft-ietf-idr-bgp-ext-communities-05 */
351 #define BGP_EXT_COM_RT_0 0x0002 /* Route Target,Format AS(2bytes):AN(4bytes) */
352 #define BGP_EXT_COM_RT_1 0x0102 /* Route Target,Format IP address:AN(2bytes) */
353 #define BGP_EXT_COM_RT_2 0x0202 /* Route Target,Format AN(4bytes):local(2bytes) */
354 #define BGP_EXT_COM_RO_0 0x0003 /* Route Origin,Format AS(2bytes):AN(4bytes) */
355 #define BGP_EXT_COM_RO_1 0x0103 /* Route Origin,Format IP address:AN(2bytes) */
356 #define BGP_EXT_COM_RO_2 0x0203 /* Route Origin,Format AN(4bytes):local(2bytes) */
357 #define BGP_EXT_COM_LINKBAND 0x4004 /* Link Bandwidth,Format AS(2B):Bandwidth(4B) */
358 /* rfc2547 bgp-mpls-vpns */
359 #define BGP_EXT_COM_CISCO_MCAST 0x0009 /* cisco proprietary */
361 #define BGP_EXT_COM_VPN_ORIGIN 0x0005 /* OSPF Domain ID / VPN of Origin - draft-rosen-vpns-ospf-bgp-mpls */
362 #define BGP_EXT_COM_VPN_ORIGIN2 0x0105 /* duplicate - keep for backwards compatability */
363 #define BGP_EXT_COM_VPN_ORIGIN3 0x0205 /* duplicate - keep for backwards compatability */
364 #define BGP_EXT_COM_VPN_ORIGIN4 0x8005 /* duplicate - keep for backwards compatability */
366 #define BGP_EXT_COM_OSPF_RTYPE 0x0306 /* OSPF Route Type,Format Area(4B):RouteType(1B):Options(1B) */
367 #define BGP_EXT_COM_OSPF_RTYPE2 0x8000 /* duplicate - keep for backwards compatability */
369 #define BGP_EXT_COM_OSPF_RID 0x0107 /* OSPF Router ID,Format RouterID(4B):Unused(2B) */
370 #define BGP_EXT_COM_OSPF_RID2 0x8001 /* duplicate - keep for backwards compatability */
372 #define BGP_EXT_COM_L2INFO 0x800a /* draft-kompella-ppvpn-l2vpn */
374 /* http://www.cisco.com/en/US/tech/tk436/tk428/technologies_tech_note09186a00801eb09a.shtml */
375 #define BGP_EXT_COM_EIGRP_GEN 0x8800
376 #define BGP_EXT_COM_EIGRP_METRIC_AS_DELAY 0x8801
377 #define BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW 0x8802
378 #define BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU 0x8803
379 #define BGP_EXT_COM_EIGRP_EXT_REMAS_REMID 0x8804
380 #define BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC 0x8805
382 static struct tok bgp_extd_comm_flag_values[] = {
383 { 0x8000, "vendor-specific"},
384 { 0x4000, "non-transitive"},
385 { 0, NULL},
388 static struct tok bgp_extd_comm_subtype_values[] = {
389 { BGP_EXT_COM_RT_0, "target"},
390 { BGP_EXT_COM_RT_1, "target"},
391 { BGP_EXT_COM_RT_2, "target"},
392 { BGP_EXT_COM_RO_0, "origin"},
393 { BGP_EXT_COM_RO_1, "origin"},
394 { BGP_EXT_COM_RO_2, "origin"},
395 { BGP_EXT_COM_LINKBAND, "link-BW"},
396 { BGP_EXT_COM_CISCO_MCAST, "mdt-group"},
397 { BGP_EXT_COM_VPN_ORIGIN, "ospf-domain"},
398 { BGP_EXT_COM_VPN_ORIGIN2, "ospf-domain"},
399 { BGP_EXT_COM_VPN_ORIGIN3, "ospf-domain"},
400 { BGP_EXT_COM_VPN_ORIGIN4, "ospf-domain"},
401 { BGP_EXT_COM_OSPF_RTYPE, "ospf-route-type"},
402 { BGP_EXT_COM_OSPF_RTYPE2, "ospf-route-type"},
403 { BGP_EXT_COM_OSPF_RID, "ospf-router-id"},
404 { BGP_EXT_COM_OSPF_RID2, "ospf-router-id"},
405 { BGP_EXT_COM_L2INFO, "layer2-info"},
406 { BGP_EXT_COM_EIGRP_GEN , "eigrp-general-route (flag, tag)" },
407 { BGP_EXT_COM_EIGRP_METRIC_AS_DELAY , "eigrp-route-metric (AS, delay)" },
408 { BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW , "eigrp-route-metric (reliability, nexthop, bandwidth)" },
409 { BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU , "eigrp-route-metric (load, MTU)" },
410 { BGP_EXT_COM_EIGRP_EXT_REMAS_REMID , "eigrp-external-route (remote-AS, remote-ID)" },
411 { BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC , "eigrp-external-route (remote-proto, remote-metric)" },
412 { 0, NULL},
415 /* OSPF codes for BGP_EXT_COM_OSPF_RTYPE draft-rosen-vpns-ospf-bgp-mpls */
416 #define BGP_OSPF_RTYPE_RTR 1 /* OSPF Router LSA */
417 #define BGP_OSPF_RTYPE_NET 2 /* OSPF Network LSA */
418 #define BGP_OSPF_RTYPE_SUM 3 /* OSPF Summary LSA */
419 #define BGP_OSPF_RTYPE_EXT 5 /* OSPF External LSA, note that ASBR doesn't apply to MPLS-VPN */
420 #define BGP_OSPF_RTYPE_NSSA 7 /* OSPF NSSA External*/
421 #define BGP_OSPF_RTYPE_SHAM 129 /* OSPF-MPLS-VPN Sham link */
422 #define BGP_OSPF_RTYPE_METRIC_TYPE 0x1 /* LSB of RTYPE Options Field */
424 static struct tok bgp_extd_comm_ospf_rtype_values[] = {
425 { BGP_OSPF_RTYPE_RTR, "Router" },
426 { BGP_OSPF_RTYPE_NET, "Network" },
427 { BGP_OSPF_RTYPE_SUM, "Summary" },
428 { BGP_OSPF_RTYPE_EXT, "External" },
429 { BGP_OSPF_RTYPE_NSSA,"NSSA External" },
430 { BGP_OSPF_RTYPE_SHAM,"MPLS-VPN Sham" },
431 { 0, NULL },
435 decode_prefix4(const u_char *pptr, char *buf, u_int buflen)
437 struct in_addr addr;
438 u_int plen;
440 TCHECK(pptr[0]);
441 plen = pptr[0];
442 if (32 < plen)
443 return -1;
445 memset(&addr, 0, sizeof(addr));
446 TCHECK2(pptr[1], (plen + 7) / 8);
447 memcpy(&addr, &pptr[1], (plen + 7) / 8);
448 if (plen % 8) {
449 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
450 ((0xff00 >> (plen % 8)) & 0xff);
452 snprintf(buf, buflen, "%s/%d", getname((u_char *)&addr), plen);
453 return 1 + (plen + 7) / 8;
455 trunc:
456 return -2;
459 static int
460 decode_labeled_prefix4(const u_char *pptr, char *buf, u_int buflen)
462 struct in_addr addr;
463 u_int plen;
465 TCHECK(pptr[0]);
466 plen = pptr[0]; /* get prefix length */
468 /* this is one of the weirdnesses of rfc3107
469 the label length (actually the label + COS bits)
470 is added to the prefix length;
471 we also do only read out just one label -
472 there is no real application for advertisement of
473 stacked labels in a a single BGP message
476 if (24 > plen)
477 return -1;
479 plen-=24; /* adjust prefixlen - labellength */
481 if (32 < plen)
482 return -1;
484 memset(&addr, 0, sizeof(addr));
485 TCHECK2(pptr[4], (plen + 7) / 8);
486 memcpy(&addr, &pptr[4], (plen + 7) / 8);
487 if (plen % 8) {
488 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
489 ((0xff00 >> (plen % 8)) & 0xff);
491 /* the label may get offsetted by 4 bits so lets shift it right */
492 snprintf(buf, buflen, "%s/%d, label:%u %s",
493 getname((u_char *)&addr),
494 plen,
495 EXTRACT_24BITS(pptr+1)>>4,
496 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
498 return 4 + (plen + 7) / 8;
500 trunc:
501 return -2;
504 /* RDs and RTs share the same semantics
505 * we use bgp_vpn_rd_print for
506 * printing route targets inside a NLRI */
507 char *
508 bgp_vpn_rd_print (const u_char *pptr) {
510 /* allocate space for the largest possible string */
511 static char rd[sizeof("xxxxxxxxxx:xxxxx (xxx.xxx.xxx.xxx:xxxxx)")];
512 char *pos = rd;
514 /* ok lets load the RD format */
515 switch (EXTRACT_16BITS(pptr)) {
517 /* AS:IP-address fmt*/
518 case 0:
519 snprintf(pos, sizeof(rd) - (pos - rd), "%u:%u.%u.%u.%u",
520 EXTRACT_16BITS(pptr+2), *(pptr+4), *(pptr+5), *(pptr+6), *(pptr+7));
521 break;
522 /* IP-address:AS fmt*/
524 case 1:
525 snprintf(pos, sizeof(rd) - (pos - rd), "%u.%u.%u.%u:%u",
526 *(pptr+2), *(pptr+3), *(pptr+4), *(pptr+5), EXTRACT_16BITS(pptr+6));
527 break;
529 /* 4-byte-AS:number fmt*/
530 case 2:
531 snprintf(pos, sizeof(rd) - (pos - rd), "%u:%u (%u.%u.%u.%u:%u)",
532 EXTRACT_32BITS(pptr+2), EXTRACT_16BITS(pptr+6),
533 *(pptr+2), *(pptr+3), *(pptr+4), *(pptr+5), EXTRACT_16BITS(pptr+6));
534 break;
535 default:
536 snprintf(pos, sizeof(rd) - (pos - rd), "unknown RD format");
537 break;
539 pos += strlen(pos);
540 *(pos) = '\0';
541 return (rd);
544 static int
545 decode_rt_routing_info(const u_char *pptr, char *buf, u_int buflen)
547 u_int8_t route_target[8];
548 u_int plen;
550 TCHECK(pptr[0]);
551 plen = pptr[0]; /* get prefix length */
553 if (0 == plen)
554 return 1; /* default route target */
556 if (32 > plen)
557 return -1;
559 plen-=32; /* adjust prefix length */
561 if (64 < plen)
562 return -1;
564 memset(&route_target, 0, sizeof(route_target));
565 TCHECK2(pptr[1], (plen + 7) / 8);
566 memcpy(&route_target, &pptr[1], (plen + 7) / 8);
567 if (plen % 8) {
568 ((u_char *)&route_target)[(plen + 7) / 8 - 1] &=
569 ((0xff00 >> (plen % 8)) & 0xff);
571 snprintf(buf, buflen, "origin AS: %u, route target %s",
572 EXTRACT_32BITS(pptr+1),
573 bgp_vpn_rd_print((u_char *)&route_target));
575 return 5 + (plen + 7) / 8;
577 trunc:
578 return -2;
581 static int
582 decode_labeled_vpn_prefix4(const u_char *pptr, char *buf, u_int buflen)
584 struct in_addr addr;
585 u_int plen;
587 TCHECK(pptr[0]);
588 plen = pptr[0]; /* get prefix length */
590 if ((24+64) > plen)
591 return -1;
593 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
595 if (32 < plen)
596 return -1;
598 memset(&addr, 0, sizeof(addr));
599 TCHECK2(pptr[12], (plen + 7) / 8);
600 memcpy(&addr, &pptr[12], (plen + 7) / 8);
601 if (plen % 8) {
602 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
603 ((0xff00 >> (plen % 8)) & 0xff);
605 /* the label may get offsetted by 4 bits so lets shift it right */
606 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
607 bgp_vpn_rd_print(pptr+4),
608 getname((u_char *)&addr),
609 plen,
610 EXTRACT_24BITS(pptr+1)>>4,
611 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
613 return 12 + (plen + 7) / 8;
615 trunc:
616 return -2;
620 * As I remember, some versions of systems have an snprintf() that
621 * returns -1 if the buffer would have overflowed. If the return
622 * value is negative, set buflen to 0, to indicate that we've filled
623 * the buffer up.
625 * If the return value is greater than buflen, that means that
626 * the buffer would have overflowed; again, set buflen to 0 in
627 * that case.
629 #define UPDATE_BUF_BUFLEN(buf, buflen, strlen) \
630 if (strlen<0) \
631 buflen=0; \
632 else if ((u_int)strlen>buflen) \
633 buflen=0; \
634 else { \
635 buflen-=strlen; \
636 buf+=strlen; \
639 static int
640 decode_labeled_vpn_l2(const u_char *pptr, char *buf, u_int buflen)
642 int plen,tlen,strlen,tlv_type,tlv_len,ttlv_len;
644 TCHECK2(pptr[0], 2);
645 plen=EXTRACT_16BITS(pptr);
646 tlen=plen;
647 pptr+=2;
648 TCHECK2(pptr[0],15);
649 buf[0]='\0';
650 strlen=snprintf(buf, buflen, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
651 bgp_vpn_rd_print(pptr),
652 EXTRACT_16BITS(pptr+8),
653 EXTRACT_16BITS(pptr+10),
654 EXTRACT_24BITS(pptr+12)>>4); /* the label is offsetted by 4 bits so lets shift it right */
655 UPDATE_BUF_BUFLEN(buf, buflen, strlen);
656 pptr+=15;
657 tlen-=15;
659 /* ok now the variable part - lets read out TLVs*/
660 while (tlen>0) {
661 if (tlen < 3)
662 return -1;
663 TCHECK2(pptr[0], 3);
664 tlv_type=*pptr++;
665 tlv_len=EXTRACT_16BITS(pptr);
666 ttlv_len=tlv_len;
667 pptr+=2;
669 switch(tlv_type) {
670 case 1:
671 if (buflen!=0) {
672 strlen=snprintf(buf,buflen, "\n\t\tcircuit status vector (%u) length: %u: 0x",
673 tlv_type,
674 tlv_len);
675 UPDATE_BUF_BUFLEN(buf, buflen, strlen);
677 ttlv_len=ttlv_len/8+1; /* how many bytes do we need to read ? */
678 while (ttlv_len>0) {
679 TCHECK(pptr[0]);
680 if (buflen!=0) {
681 strlen=snprintf(buf,buflen, "%02x",*pptr++);
682 UPDATE_BUF_BUFLEN(buf, buflen, strlen);
684 ttlv_len--;
686 break;
687 default:
688 if (buflen!=0) {
689 strlen=snprintf(buf,buflen, "\n\t\tunknown TLV #%u, length: %u",
690 tlv_type,
691 tlv_len);
692 UPDATE_BUF_BUFLEN(buf, buflen, strlen);
694 break;
696 tlen-=(tlv_len<<3); /* the tlv-length is expressed in bits so lets shift it right */
698 return plen+2;
700 trunc:
701 return -2;
704 #ifdef INET6
706 decode_prefix6(const u_char *pd, char *buf, u_int buflen)
708 struct in6_addr addr;
709 u_int plen;
711 TCHECK(pd[0]);
712 plen = pd[0];
713 if (128 < plen)
714 return -1;
716 memset(&addr, 0, sizeof(addr));
717 TCHECK2(pd[1], (plen + 7) / 8);
718 memcpy(&addr, &pd[1], (plen + 7) / 8);
719 if (plen % 8) {
720 addr.s6_addr[(plen + 7) / 8 - 1] &=
721 ((0xff00 >> (plen % 8)) & 0xff);
723 snprintf(buf, buflen, "%s/%d", getname6((u_char *)&addr), plen);
724 return 1 + (plen + 7) / 8;
726 trunc:
727 return -2;
730 static int
731 decode_labeled_prefix6(const u_char *pptr, char *buf, u_int buflen)
733 struct in6_addr addr;
734 u_int plen;
736 TCHECK(pptr[0]);
737 plen = pptr[0]; /* get prefix length */
739 if (24 > plen)
740 return -1;
742 plen-=24; /* adjust prefixlen - labellength */
744 if (128 < plen)
745 return -1;
747 memset(&addr, 0, sizeof(addr));
748 TCHECK2(pptr[4], (plen + 7) / 8);
749 memcpy(&addr, &pptr[4], (plen + 7) / 8);
750 if (plen % 8) {
751 addr.s6_addr[(plen + 7) / 8 - 1] &=
752 ((0xff00 >> (plen % 8)) & 0xff);
754 /* the label may get offsetted by 4 bits so lets shift it right */
755 snprintf(buf, buflen, "%s/%d, label:%u %s",
756 getname6((u_char *)&addr),
757 plen,
758 EXTRACT_24BITS(pptr+1)>>4,
759 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
761 return 4 + (plen + 7) / 8;
763 trunc:
764 return -2;
767 static int
768 decode_labeled_vpn_prefix6(const u_char *pptr, char *buf, u_int buflen)
770 struct in6_addr addr;
771 u_int plen;
773 TCHECK(pptr[0]);
774 plen = pptr[0]; /* get prefix length */
776 if ((24+64) > plen)
777 return -1;
779 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
781 if (128 < plen)
782 return -1;
784 memset(&addr, 0, sizeof(addr));
785 TCHECK2(pptr[12], (plen + 7) / 8);
786 memcpy(&addr, &pptr[12], (plen + 7) / 8);
787 if (plen % 8) {
788 addr.s6_addr[(plen + 7) / 8 - 1] &=
789 ((0xff00 >> (plen % 8)) & 0xff);
791 /* the label may get offsetted by 4 bits so lets shift it right */
792 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
793 bgp_vpn_rd_print(pptr+4),
794 getname6((u_char *)&addr),
795 plen,
796 EXTRACT_24BITS(pptr+1)>>4,
797 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
799 return 12 + (plen + 7) / 8;
801 trunc:
802 return -2;
804 #endif
806 static int
807 decode_clnp_prefix(const u_char *pptr, char *buf, u_int buflen)
809 u_int8_t addr[19];
810 u_int plen;
812 TCHECK(pptr[0]);
813 plen = pptr[0]; /* get prefix length */
815 if (152 < plen)
816 return -1;
818 memset(&addr, 0, sizeof(addr));
819 TCHECK2(pptr[4], (plen + 7) / 8);
820 memcpy(&addr, &pptr[4], (plen + 7) / 8);
821 if (plen % 8) {
822 addr[(plen + 7) / 8 - 1] &=
823 ((0xff00 >> (plen % 8)) & 0xff);
825 snprintf(buf, buflen, "%s/%d",
826 isonsap_string(addr,(plen + 7) / 8),
827 plen);
829 return 1 + (plen + 7) / 8;
831 trunc:
832 return -2;
835 static int
836 decode_labeled_vpn_clnp_prefix(const u_char *pptr, char *buf, u_int buflen)
838 u_int8_t addr[19];
839 u_int plen;
841 TCHECK(pptr[0]);
842 plen = pptr[0]; /* get prefix length */
844 if ((24+64) > plen)
845 return -1;
847 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
849 if (152 < plen)
850 return -1;
852 memset(&addr, 0, sizeof(addr));
853 TCHECK2(pptr[12], (plen + 7) / 8);
854 memcpy(&addr, &pptr[12], (plen + 7) / 8);
855 if (plen % 8) {
856 addr[(plen + 7) / 8 - 1] &=
857 ((0xff00 >> (plen % 8)) & 0xff);
859 /* the label may get offsetted by 4 bits so lets shift it right */
860 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
861 bgp_vpn_rd_print(pptr+4),
862 isonsap_string(addr,(plen + 7) / 8),
863 plen,
864 EXTRACT_24BITS(pptr+1)>>4,
865 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
867 return 12 + (plen + 7) / 8;
869 trunc:
870 return -2;
873 static int
874 bgp_attr_print(const struct bgp_attr *attr, const u_char *pptr, int len)
876 int i;
877 u_int16_t af;
878 u_int8_t safi, snpa, nhlen;
879 union { /* copy buffer for bandwidth values */
880 float f;
881 u_int32_t i;
882 } bw;
883 int advance;
884 int tlen;
885 const u_char *tptr;
886 char buf[MAXHOSTNAMELEN + 100];
887 char tokbuf[TOKBUFSIZE];
889 tptr = pptr;
890 tlen=len;
892 switch (attr->bgpa_type) {
893 case BGPTYPE_ORIGIN:
894 if (len != 1)
895 printf("invalid len");
896 else {
897 TCHECK(*tptr);
898 printf("%s", tok2strbuf(bgp_origin_values,
899 "Unknown Origin Typecode",
900 tptr[0],
901 tokbuf, sizeof(tokbuf)));
903 break;
905 case BGPTYPE_AS_PATH:
906 if (len % 2) {
907 printf("invalid len");
908 break;
910 if (!len) {
911 printf("empty");
912 break;
915 while (tptr < pptr + len) {
916 TCHECK(tptr[0]);
917 printf("%s", tok2strbuf(bgp_as_path_segment_open_values,
918 "?", tptr[0],
919 tokbuf, sizeof(tokbuf)));
920 for (i = 0; i < tptr[1] * 2; i += 2) {
921 TCHECK2(tptr[2 + i], 2);
922 printf("%u ", EXTRACT_16BITS(&tptr[2 + i]));
924 TCHECK(tptr[0]);
925 printf("%s", tok2strbuf(bgp_as_path_segment_close_values,
926 "?", tptr[0],
927 tokbuf, sizeof(tokbuf)));
928 TCHECK(tptr[1]);
929 tptr += 2 + tptr[1] * 2;
931 break;
932 case BGPTYPE_NEXT_HOP:
933 if (len != 4)
934 printf("invalid len");
935 else {
936 TCHECK2(tptr[0], 4);
937 printf("%s", getname(tptr));
939 break;
940 case BGPTYPE_MULTI_EXIT_DISC:
941 case BGPTYPE_LOCAL_PREF:
942 if (len != 4)
943 printf("invalid len");
944 else {
945 TCHECK2(tptr[0], 4);
946 printf("%u", EXTRACT_32BITS(tptr));
948 break;
949 case BGPTYPE_ATOMIC_AGGREGATE:
950 if (len != 0)
951 printf("invalid len");
952 break;
953 case BGPTYPE_AGGREGATOR:
954 if (len != 6) {
955 printf("invalid len");
956 break;
958 TCHECK2(tptr[0], 6);
959 printf(" AS #%u, origin %s", EXTRACT_16BITS(tptr),
960 getname(tptr + 2));
961 break;
962 case BGPTYPE_COMMUNITIES:
963 if (len % 4) {
964 printf("invalid len");
965 break;
967 while (tlen>0) {
968 u_int32_t comm;
969 TCHECK2(tptr[0], 4);
970 comm = EXTRACT_32BITS(tptr);
971 switch (comm) {
972 case BGP_COMMUNITY_NO_EXPORT:
973 printf(" NO_EXPORT");
974 break;
975 case BGP_COMMUNITY_NO_ADVERT:
976 printf(" NO_ADVERTISE");
977 break;
978 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED:
979 printf(" NO_EXPORT_SUBCONFED");
980 break;
981 default:
982 printf("%u:%u%s",
983 (comm >> 16) & 0xffff,
984 comm & 0xffff,
985 (tlen>4) ? ", " : "");
986 break;
988 tlen -=4;
989 tptr +=4;
991 break;
992 case BGPTYPE_ORIGINATOR_ID:
993 if (len != 4) {
994 printf("invalid len");
995 break;
997 TCHECK2(tptr[0], 4);
998 printf("%s",getname(tptr));
999 break;
1000 case BGPTYPE_CLUSTER_LIST:
1001 if (len % 4) {
1002 printf("invalid len");
1003 break;
1005 while (tlen>0) {
1006 TCHECK2(tptr[0], 4);
1007 printf("%s%s",
1008 getname(tptr),
1009 (tlen>4) ? ", " : "");
1010 tlen -=4;
1011 tptr +=4;
1013 break;
1014 case BGPTYPE_MP_REACH_NLRI:
1015 TCHECK2(tptr[0], 3);
1016 af = EXTRACT_16BITS(tptr);
1017 safi = tptr[2];
1019 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
1020 tok2strbuf(af_values, "Unknown AFI", af,
1021 tokbuf, sizeof(tokbuf)),
1023 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1024 tok2strbuf(bgp_safi_values, "Unknown SAFI", safi,
1025 tokbuf, sizeof(tokbuf)),
1026 safi);
1028 switch(af<<8 | safi) {
1029 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1030 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1031 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1032 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1033 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
1034 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1035 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1036 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1037 #ifdef INET6
1038 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1039 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1040 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1041 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1042 case (AFNUM_INET6<<8 | SAFNUM_RT_ROUTING_INFO):
1043 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1044 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1045 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1046 #endif
1047 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1048 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1049 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1050 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1051 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1052 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1053 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1054 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1055 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1056 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1057 break;
1058 default:
1059 TCHECK2(tptr[0], tlen);
1060 printf("\n\t no AFI %u / SAFI %u decoder",af,safi);
1061 if (vflag <= 1)
1062 print_unknown_data(tptr,"\n\t ",tlen);
1063 goto done;
1064 break;
1067 tptr +=3;
1069 TCHECK(tptr[0]);
1070 nhlen = tptr[0];
1071 tlen = nhlen;
1072 tptr++;
1074 if (tlen) {
1075 printf("\n\t nexthop: ");
1076 while (tlen > 0) {
1077 switch(af<<8 | safi) {
1078 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1079 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1080 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1081 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1082 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
1083 if (tlen < (int)sizeof(struct in_addr)) {
1084 printf("invalid len");
1085 tlen = 0;
1086 } else {
1087 TCHECK2(tptr[0], sizeof(struct in_addr));
1088 printf("%s",getname(tptr));
1089 tlen -= sizeof(struct in_addr);
1090 tptr += sizeof(struct in_addr);
1092 break;
1093 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1094 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1095 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1096 if (tlen < (int)(sizeof(struct in_addr)+BGP_VPN_RD_LEN)) {
1097 printf("invalid len");
1098 tlen = 0;
1099 } else {
1100 TCHECK2(tptr[0], sizeof(struct in_addr)+BGP_VPN_RD_LEN);
1101 printf("RD: %s, %s",
1102 bgp_vpn_rd_print(tptr),
1103 getname(tptr+BGP_VPN_RD_LEN));
1104 tlen -= (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
1105 tptr += (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
1107 break;
1108 #ifdef INET6
1109 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1110 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1111 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1112 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1113 case (AFNUM_INET6<<8 | SAFNUM_RT_ROUTING_INFO):
1114 if (tlen < (int)sizeof(struct in6_addr)) {
1115 printf("invalid len");
1116 tlen = 0;
1117 } else {
1118 TCHECK2(tptr[0], sizeof(struct in6_addr));
1119 printf("%s", getname6(tptr));
1120 tlen -= sizeof(struct in6_addr);
1121 tptr += sizeof(struct in6_addr);
1123 break;
1124 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1125 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1126 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1127 if (tlen < (int)(sizeof(struct in6_addr)+BGP_VPN_RD_LEN)) {
1128 printf("invalid len");
1129 tlen = 0;
1130 } else {
1131 TCHECK2(tptr[0], sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
1132 printf("RD: %s, %s",
1133 bgp_vpn_rd_print(tptr),
1134 getname6(tptr+BGP_VPN_RD_LEN));
1135 tlen -= (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
1136 tptr += (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
1138 break;
1139 #endif
1140 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1141 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1142 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1143 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1144 if (tlen < (int)sizeof(struct in_addr)) {
1145 printf("invalid len");
1146 tlen = 0;
1147 } else {
1148 TCHECK2(tptr[0], sizeof(struct in_addr));
1149 printf("%s", getname(tptr));
1150 tlen -= (sizeof(struct in_addr));
1151 tptr += (sizeof(struct in_addr));
1153 break;
1154 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1155 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1156 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1157 TCHECK2(tptr[0], tlen);
1158 printf("%s",isonsap_string(tptr,tlen));
1159 tptr += tlen;
1160 tlen = 0;
1161 break;
1163 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1164 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1165 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1166 if (tlen < BGP_VPN_RD_LEN+1) {
1167 printf("invalid len");
1168 tlen = 0;
1169 } else {
1170 TCHECK2(tptr[0], tlen);
1171 printf("RD: %s, %s",
1172 bgp_vpn_rd_print(tptr),
1173 isonsap_string(tptr+BGP_VPN_RD_LEN,tlen-BGP_VPN_RD_LEN));
1174 /* rfc986 mapped IPv4 address ? */
1175 if (EXTRACT_32BITS(tptr+BGP_VPN_RD_LEN) == 0x47000601)
1176 printf(" = %s", getname(tptr+BGP_VPN_RD_LEN+4));
1177 #ifdef INET6
1178 /* rfc1888 mapped IPv6 address ? */
1179 else if (EXTRACT_24BITS(tptr+BGP_VPN_RD_LEN) == 0x350000)
1180 printf(" = %s", getname6(tptr+BGP_VPN_RD_LEN+3));
1181 #endif
1182 tptr += tlen;
1183 tlen = 0;
1185 break;
1186 default:
1187 TCHECK2(tptr[0], tlen);
1188 printf("no AFI %u/SAFI %u decoder",af,safi);
1189 if (vflag <= 1)
1190 print_unknown_data(tptr,"\n\t ",tlen);
1191 tptr += tlen;
1192 tlen = 0;
1193 goto done;
1194 break;
1198 printf(", nh-length: %u", nhlen);
1199 tptr += tlen;
1201 TCHECK(tptr[0]);
1202 snpa = tptr[0];
1203 tptr++;
1205 if (snpa) {
1206 printf("\n\t %u SNPA", snpa);
1207 for (/*nothing*/; snpa > 0; snpa--) {
1208 TCHECK(tptr[0]);
1209 printf("\n\t %d bytes", tptr[0]);
1210 tptr += tptr[0] + 1;
1212 } else {
1213 printf(", no SNPA");
1216 while (len - (tptr - pptr) > 0) {
1217 switch (af<<8 | safi) {
1218 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1219 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1220 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1221 advance = decode_prefix4(tptr, buf, sizeof(buf));
1222 if (advance == -1)
1223 printf("\n\t (illegal prefix length)");
1224 else if (advance == -2)
1225 goto trunc;
1226 else
1227 printf("\n\t %s", buf);
1228 break;
1229 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1230 advance = decode_labeled_prefix4(tptr, buf, sizeof(buf));
1231 if (advance == -1)
1232 printf("\n\t (illegal prefix length)");
1233 else if (advance == -2)
1234 goto trunc;
1235 else
1236 printf("\n\t %s", buf);
1237 break;
1238 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1239 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1240 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1241 advance = decode_labeled_vpn_prefix4(tptr, buf, sizeof(buf));
1242 if (advance == -1)
1243 printf("\n\t (illegal prefix length)");
1244 else if (advance == -2)
1245 goto trunc;
1246 else
1247 printf("\n\t %s", buf);
1248 break;
1249 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
1250 advance = decode_rt_routing_info(tptr, buf, sizeof(buf));
1251 if (advance == -1)
1252 printf("\n\t (illegal prefix length)");
1253 else if (advance == -2)
1254 goto trunc;
1255 else
1256 printf("\n\t %s", buf);
1257 break;
1258 #ifdef INET6
1259 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1260 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1261 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1262 advance = decode_prefix6(tptr, buf, sizeof(buf));
1263 if (advance == -1)
1264 printf("\n\t (illegal prefix length)");
1265 else if (advance == -2)
1266 goto trunc;
1267 else
1268 printf("\n\t %s", buf);
1269 break;
1270 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1271 advance = decode_labeled_prefix6(tptr, buf, sizeof(buf));
1272 if (advance == -1)
1273 printf("\n\t (illegal prefix length)");
1274 else if (advance == -2)
1275 goto trunc;
1276 else
1277 printf("\n\t %s", buf);
1278 break;
1279 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1280 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1281 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1282 advance = decode_labeled_vpn_prefix6(tptr, buf, sizeof(buf));
1283 if (advance == -1)
1284 printf("\n\t (illegal prefix length)");
1285 else if (advance == -2)
1286 goto trunc;
1287 else
1288 printf("\n\t %s", buf);
1289 break;
1290 case (AFNUM_INET6<<8 | SAFNUM_RT_ROUTING_INFO):
1291 advance = decode_rt_routing_info(tptr, buf, sizeof(buf));
1292 if (advance == -1)
1293 printf("\n\t (illegal prefix length)");
1294 else if (advance == -2)
1295 goto trunc;
1296 else
1297 printf("\n\t %s", buf);
1298 break;
1299 #endif
1300 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1301 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1302 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1303 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1304 advance = decode_labeled_vpn_l2(tptr, buf, sizeof(buf));
1305 if (advance == -1)
1306 printf("\n\t (illegal length)");
1307 else if (advance == -2)
1308 goto trunc;
1309 else
1310 printf("\n\t %s", buf);
1311 break;
1312 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1313 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1314 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1315 advance = decode_clnp_prefix(tptr, buf, sizeof(buf));
1316 if (advance == -1)
1317 printf("\n\t (illegal prefix length)");
1318 else if (advance == -2)
1319 goto trunc;
1320 else
1321 printf("\n\t %s", buf);
1322 break;
1323 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1324 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1325 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1326 advance = decode_labeled_vpn_clnp_prefix(tptr, buf, sizeof(buf));
1327 if (advance == -1)
1328 printf("\n\t (illegal prefix length)");
1329 else if (advance == -2)
1330 goto trunc;
1331 else
1332 printf("\n\t %s", buf);
1333 break;
1334 default:
1335 TCHECK2(*tptr,tlen);
1336 printf("\n\t no AFI %u / SAFI %u decoder",af,safi);
1337 if (vflag <= 1)
1338 print_unknown_data(tptr,"\n\t ",tlen);
1339 advance = 0;
1340 tptr = pptr + len;
1341 break;
1343 if (advance < 0)
1344 break;
1345 tptr += advance;
1347 done:
1348 break;
1350 case BGPTYPE_MP_UNREACH_NLRI:
1351 TCHECK2(tptr[0], BGP_MP_NLRI_MINSIZE);
1352 af = EXTRACT_16BITS(tptr);
1353 safi = tptr[2];
1355 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
1356 tok2strbuf(af_values, "Unknown AFI", af,
1357 tokbuf, sizeof(tokbuf)),
1359 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1360 tok2strbuf(bgp_safi_values, "Unknown SAFI", safi,
1361 tokbuf, sizeof(tokbuf)),
1362 safi);
1364 if (len == BGP_MP_NLRI_MINSIZE)
1365 printf("\n\t End-of-Rib Marker (empty NLRI)");
1367 tptr += 3;
1369 while (len - (tptr - pptr) > 0) {
1370 switch (af<<8 | safi) {
1371 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1372 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1373 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1374 advance = decode_prefix4(tptr, buf, sizeof(buf));
1375 if (advance == -1)
1376 printf("\n\t (illegal prefix length)");
1377 else if (advance == -2)
1378 goto trunc;
1379 else
1380 printf("\n\t %s", buf);
1381 break;
1382 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1383 advance = decode_labeled_prefix4(tptr, buf, sizeof(buf));
1384 if (advance == -1)
1385 printf("\n\t (illegal prefix length)");
1386 else if (advance == -2)
1387 goto trunc;
1388 else
1389 printf("\n\t %s", buf);
1390 break;
1391 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1392 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1393 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1394 advance = decode_labeled_vpn_prefix4(tptr, buf, sizeof(buf));
1395 if (advance == -1)
1396 printf("\n\t (illegal prefix length)");
1397 else if (advance == -2)
1398 goto trunc;
1399 else
1400 printf("\n\t %s", buf);
1401 break;
1402 #ifdef INET6
1403 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1404 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1405 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1406 advance = decode_prefix6(tptr, buf, sizeof(buf));
1407 if (advance == -1)
1408 printf("\n\t (illegal prefix length)");
1409 else if (advance == -2)
1410 goto trunc;
1411 else
1412 printf("\n\t %s", buf);
1413 break;
1414 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1415 advance = decode_labeled_prefix6(tptr, buf, sizeof(buf));
1416 if (advance == -1)
1417 printf("\n\t (illegal prefix length)");
1418 else if (advance == -2)
1419 goto trunc;
1420 else
1421 printf("\n\t %s", buf);
1422 break;
1423 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1424 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1425 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1426 advance = decode_labeled_vpn_prefix6(tptr, buf, sizeof(buf));
1427 if (advance == -1)
1428 printf("\n\t (illegal prefix length)");
1429 else if (advance == -2)
1430 goto trunc;
1431 else
1432 printf("\n\t %s", buf);
1433 break;
1434 #endif
1435 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1436 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1437 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1438 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1439 advance = decode_labeled_vpn_l2(tptr, buf, sizeof(buf));
1440 if (advance == -1)
1441 printf("\n\t (illegal length)");
1442 else if (advance == -2)
1443 goto trunc;
1444 else
1445 printf("\n\t %s", buf);
1446 break;
1447 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1448 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1449 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1450 advance = decode_clnp_prefix(tptr, buf, sizeof(buf));
1451 if (advance == -1)
1452 printf("\n\t (illegal prefix length)");
1453 else if (advance == -2)
1454 goto trunc;
1455 else
1456 printf("\n\t %s", buf);
1457 break;
1458 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1459 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1460 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1461 advance = decode_labeled_vpn_clnp_prefix(tptr, buf, sizeof(buf));
1462 if (advance == -1)
1463 printf("\n\t (illegal prefix length)");
1464 else if (advance == -2)
1465 goto trunc;
1466 else
1467 printf("\n\t %s", buf);
1468 break;
1469 default:
1470 TCHECK2(*(tptr-3),tlen);
1471 printf("no AFI %u / SAFI %u decoder",af,safi);
1472 if (vflag <= 1)
1473 print_unknown_data(tptr-3,"\n\t ",tlen);
1474 advance = 0;
1475 tptr = pptr + len;
1476 break;
1478 if (advance < 0)
1479 break;
1480 tptr += advance;
1482 break;
1483 case BGPTYPE_EXTD_COMMUNITIES:
1484 if (len % 8) {
1485 printf("invalid len");
1486 break;
1488 while (tlen>0) {
1489 u_int16_t extd_comm;
1491 TCHECK2(tptr[0], 2);
1492 extd_comm=EXTRACT_16BITS(tptr);
1494 printf("\n\t %s (0x%04x), Flags [%s]",
1495 tok2strbuf(bgp_extd_comm_subtype_values,
1496 "unknown extd community typecode",
1497 extd_comm, tokbuf, sizeof(tokbuf)),
1498 extd_comm,
1499 bittok2str(bgp_extd_comm_flag_values, "none", extd_comm));
1501 TCHECK2(*(tptr+2), 6);
1502 switch(extd_comm) {
1503 case BGP_EXT_COM_RT_0:
1504 case BGP_EXT_COM_RO_0:
1505 printf(": %u:%s",
1506 EXTRACT_16BITS(tptr+2),
1507 getname(tptr+4));
1508 break;
1509 case BGP_EXT_COM_RT_1:
1510 case BGP_EXT_COM_RO_1:
1511 printf(": %s:%u",
1512 getname(tptr+2),
1513 EXTRACT_16BITS(tptr+6));
1514 break;
1515 case BGP_EXT_COM_RT_2:
1516 case BGP_EXT_COM_RO_2:
1517 printf(": %u:%u",
1518 EXTRACT_32BITS(tptr+2),
1519 EXTRACT_16BITS(tptr+6));
1520 break;
1521 case BGP_EXT_COM_LINKBAND:
1522 bw.i = EXTRACT_32BITS(tptr+2);
1523 printf(": bandwidth: %.3f Mbps",
1524 bw.f*8/1000000);
1525 break;
1526 case BGP_EXT_COM_CISCO_MCAST:
1527 printf(": AS %u, group %s",
1528 EXTRACT_16BITS(tptr+2),
1529 getname(tptr+4));
1530 break;
1531 case BGP_EXT_COM_VPN_ORIGIN:
1532 case BGP_EXT_COM_VPN_ORIGIN2:
1533 case BGP_EXT_COM_VPN_ORIGIN3:
1534 case BGP_EXT_COM_VPN_ORIGIN4:
1535 case BGP_EXT_COM_OSPF_RID:
1536 case BGP_EXT_COM_OSPF_RID2:
1537 printf("%s", getname(tptr+2));
1538 break;
1539 case BGP_EXT_COM_OSPF_RTYPE:
1540 case BGP_EXT_COM_OSPF_RTYPE2:
1541 printf(": area:%s, router-type:%s, metric-type:%s%s",
1542 getname(tptr+2),
1543 tok2strbuf(bgp_extd_comm_ospf_rtype_values,
1544 "unknown (0x%02x)",
1545 *(tptr+6),
1546 tokbuf, sizeof(tokbuf)),
1547 (*(tptr+7) & BGP_OSPF_RTYPE_METRIC_TYPE) ? "E2" : "",
1548 ((*(tptr+6) == BGP_OSPF_RTYPE_EXT) || (*(tptr+6) == BGP_OSPF_RTYPE_NSSA)) ? "E1" : "");
1549 break;
1550 case BGP_EXT_COM_L2INFO:
1551 printf(": %s Control Flags [0x%02x]:MTU %u",
1552 tok2strbuf(l2vpn_encaps_values,
1553 "unknown encaps",
1554 *(tptr+2),
1555 tokbuf, sizeof(tokbuf)),
1556 *(tptr+3),
1557 EXTRACT_16BITS(tptr+4));
1558 break;
1559 default:
1560 TCHECK2(*tptr,8);
1561 print_unknown_data(tptr,"\n\t ",8);
1562 break;
1564 tlen -=8;
1565 tptr +=8;
1567 break;
1569 case BGPTYPE_ATTR_SET:
1570 TCHECK2(tptr[0], 4);
1571 printf("\n\t Origin AS: %u", EXTRACT_32BITS(tptr));
1572 tptr+=4;
1573 len -=4;
1575 while (len >= 2 ) {
1576 int alen;
1577 struct bgp_attr bgpa;
1579 TCHECK2(tptr[0], sizeof(bgpa));
1580 memcpy(&bgpa, tptr, sizeof(bgpa));
1581 alen = bgp_attr_len(&bgpa);
1582 tptr += bgp_attr_off(&bgpa);
1583 len -= bgp_attr_off(&bgpa);
1585 printf("\n\t %s (%u), length: %u",
1586 tok2strbuf(bgp_attr_values,
1587 "Unknown Attribute", bgpa.bgpa_type,
1588 tokbuf, sizeof(tokbuf)),
1589 bgpa.bgpa_type,
1590 alen);
1592 if (bgpa.bgpa_flags) {
1593 printf(", Flags [%s%s%s%s",
1594 bgpa.bgpa_flags & 0x80 ? "O" : "",
1595 bgpa.bgpa_flags & 0x40 ? "T" : "",
1596 bgpa.bgpa_flags & 0x20 ? "P" : "",
1597 bgpa.bgpa_flags & 0x10 ? "E" : "");
1598 if (bgpa.bgpa_flags & 0xf)
1599 printf("+%x", bgpa.bgpa_flags & 0xf);
1600 printf("]: ");
1602 /* FIXME check for recursion */
1603 if (!bgp_attr_print(&bgpa, tptr, alen))
1604 return 0;
1605 tptr += alen;
1606 len -= alen;
1608 break;
1611 default:
1612 TCHECK2(*pptr,len);
1613 printf("\n\t no Attribute %u decoder",attr->bgpa_type); /* we have no decoder for the attribute */
1614 if (vflag <= 1)
1615 print_unknown_data(pptr,"\n\t ",len);
1616 break;
1618 if (vflag > 1 && len) { /* omit zero length attributes*/
1619 TCHECK2(*pptr,len);
1620 print_unknown_data(pptr,"\n\t ",len);
1622 return 1;
1624 trunc:
1625 return 0;
1628 static void
1629 bgp_open_print(const u_char *dat, int length)
1631 struct bgp_open bgpo;
1632 struct bgp_opt bgpopt;
1633 const u_char *opt;
1634 int i,cap_type,cap_len,tcap_len,cap_offset;
1635 char tokbuf[TOKBUFSIZE];
1636 char tokbuf2[TOKBUFSIZE];
1638 TCHECK2(dat[0], BGP_OPEN_SIZE);
1639 memcpy(&bgpo, dat, BGP_OPEN_SIZE);
1641 printf("\n\t Version %d, ", bgpo.bgpo_version);
1642 printf("my AS %u, ", ntohs(bgpo.bgpo_myas));
1643 printf("Holdtime %us, ", ntohs(bgpo.bgpo_holdtime));
1644 printf("ID %s", getname((u_char *)&bgpo.bgpo_id));
1645 printf("\n\t Optional parameters, length: %u", bgpo.bgpo_optlen);
1647 /* some little sanity checking */
1648 if (length < bgpo.bgpo_optlen+BGP_OPEN_SIZE)
1649 return;
1651 /* ugly! */
1652 opt = &((const struct bgp_open *)dat)->bgpo_optlen;
1653 opt++;
1655 i = 0;
1656 while (i < bgpo.bgpo_optlen) {
1657 TCHECK2(opt[i], BGP_OPT_SIZE);
1658 memcpy(&bgpopt, &opt[i], BGP_OPT_SIZE);
1659 if (i + 2 + bgpopt.bgpopt_len > bgpo.bgpo_optlen) {
1660 printf("\n\t Option %d, length: %u", bgpopt.bgpopt_type, bgpopt.bgpopt_len);
1661 break;
1664 printf("\n\t Option %s (%u), length: %u",
1665 tok2strbuf(bgp_opt_values,"Unknown",
1666 bgpopt.bgpopt_type,
1667 tokbuf, sizeof(tokbuf)),
1668 bgpopt.bgpopt_type,
1669 bgpopt.bgpopt_len);
1671 /* now lets decode the options we know*/
1672 switch(bgpopt.bgpopt_type) {
1673 case BGP_OPT_CAP:
1674 cap_type=opt[i+BGP_OPT_SIZE];
1675 cap_len=opt[i+BGP_OPT_SIZE+1];
1676 tcap_len=cap_len;
1677 printf("\n\t %s (%u), length: %u",
1678 tok2strbuf(bgp_capcode_values, "Unknown",
1679 cap_type, tokbuf, sizeof(tokbuf)),
1680 cap_type,
1681 cap_len);
1682 switch(cap_type) {
1683 case BGP_CAPCODE_MP:
1684 printf("\n\t\tAFI %s (%u), SAFI %s (%u)",
1685 tok2strbuf(af_values, "Unknown",
1686 EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+2),
1687 tokbuf, sizeof(tokbuf)),
1688 EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+2),
1689 tok2strbuf(bgp_safi_values, "Unknown",
1690 opt[i+BGP_OPT_SIZE+5],
1691 tokbuf, sizeof(tokbuf)),
1692 opt[i+BGP_OPT_SIZE+5]);
1693 break;
1694 case BGP_CAPCODE_RESTART:
1695 printf("\n\t\tRestart Flags: [%s], Restart Time %us",
1696 ((opt[i+BGP_OPT_SIZE+2])&0x80) ? "R" : "none",
1697 EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+2)&0xfff);
1698 tcap_len-=2;
1699 cap_offset=4;
1700 while(tcap_len>=4) {
1701 printf("\n\t\t AFI %s (%u), SAFI %s (%u), Forwarding state preserved: %s",
1702 tok2strbuf(af_values,"Unknown",
1703 EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+cap_offset),
1704 tokbuf, sizeof(tokbuf)),
1705 EXTRACT_16BITS(opt+i+BGP_OPT_SIZE+cap_offset),
1706 tok2strbuf(bgp_safi_values,"Unknown",
1707 opt[i+BGP_OPT_SIZE+cap_offset+2],
1708 tokbuf2, sizeof(tokbuf2)),
1709 opt[i+BGP_OPT_SIZE+cap_offset+2],
1710 ((opt[i+BGP_OPT_SIZE+cap_offset+3])&0x80) ? "yes" : "no" );
1711 tcap_len-=4;
1712 cap_offset+=4;
1714 break;
1715 case BGP_CAPCODE_RR:
1716 case BGP_CAPCODE_RR_CISCO:
1717 break;
1718 default:
1719 TCHECK2(opt[i+BGP_OPT_SIZE+2],cap_len);
1720 printf("\n\t\tno decoder for Capability %u",
1721 cap_type);
1722 if (vflag <= 1)
1723 print_unknown_data(&opt[i+BGP_OPT_SIZE+2],"\n\t\t",cap_len);
1724 break;
1726 if (vflag > 1) {
1727 TCHECK2(opt[i+BGP_OPT_SIZE+2],cap_len);
1728 print_unknown_data(&opt[i+BGP_OPT_SIZE+2],"\n\t\t",cap_len);
1730 break;
1731 case BGP_OPT_AUTH:
1732 default:
1733 printf("\n\t no decoder for option %u",
1734 bgpopt.bgpopt_type);
1735 break;
1738 i += BGP_OPT_SIZE + bgpopt.bgpopt_len;
1740 return;
1741 trunc:
1742 printf("[|BGP]");
1745 static void
1746 bgp_update_print(const u_char *dat, int length)
1748 struct bgp bgp;
1749 struct bgp_attr bgpa;
1750 const u_char *p;
1751 int len;
1752 int i;
1753 char tokbuf[TOKBUFSIZE];
1755 TCHECK2(dat[0], BGP_SIZE);
1756 memcpy(&bgp, dat, BGP_SIZE);
1757 p = dat + BGP_SIZE; /*XXX*/
1759 /* Unfeasible routes */
1760 len = EXTRACT_16BITS(p);
1761 if (len) {
1763 * Without keeping state from the original NLRI message,
1764 * it's not possible to tell if this a v4 or v6 route,
1765 * so only try to decode it if we're not v6 enabled.
1767 #ifdef INET6
1768 printf("\n\t Withdrawn routes: %d bytes", len);
1769 #else
1770 char buf[MAXHOSTNAMELEN + 100];
1771 int wpfx;
1773 TCHECK2(p[2], len);
1774 i = 2;
1776 printf("\n\t Withdrawn routes:");
1778 while(i < 2 + len) {
1779 wpfx = decode_prefix4(&p[i], buf, sizeof(buf));
1780 if (wpfx == -1) {
1781 printf("\n\t (illegal prefix length)");
1782 break;
1783 } else if (wpfx == -2)
1784 goto trunc;
1785 else {
1786 i += wpfx;
1787 printf("\n\t %s", buf);
1790 #endif
1792 p += 2 + len;
1794 TCHECK2(p[0], 2);
1795 len = EXTRACT_16BITS(p);
1797 if (len == 0 && length == BGP_UPDATE_MINSIZE) {
1798 printf("\n\t End-of-Rib Marker (empty NLRI)");
1799 return;
1802 if (len) {
1803 /* do something more useful!*/
1804 i = 2;
1805 while (i < 2 + len) {
1806 int alen, aoff;
1808 TCHECK2(p[i], sizeof(bgpa));
1809 memcpy(&bgpa, &p[i], sizeof(bgpa));
1810 alen = bgp_attr_len(&bgpa);
1811 aoff = bgp_attr_off(&bgpa);
1813 printf("\n\t %s (%u), length: %u",
1814 tok2strbuf(bgp_attr_values, "Unknown Attribute",
1815 bgpa.bgpa_type,
1816 tokbuf, sizeof(tokbuf)),
1817 bgpa.bgpa_type,
1818 alen);
1820 if (bgpa.bgpa_flags) {
1821 printf(", Flags [%s%s%s%s",
1822 bgpa.bgpa_flags & 0x80 ? "O" : "",
1823 bgpa.bgpa_flags & 0x40 ? "T" : "",
1824 bgpa.bgpa_flags & 0x20 ? "P" : "",
1825 bgpa.bgpa_flags & 0x10 ? "E" : "");
1826 if (bgpa.bgpa_flags & 0xf)
1827 printf("+%x", bgpa.bgpa_flags & 0xf);
1828 printf("]: ");
1830 if (!bgp_attr_print(&bgpa, &p[i + aoff], alen))
1831 goto trunc;
1832 i += aoff + alen;
1835 p += 2 + len;
1837 if (dat + length > p) {
1838 printf("\n\t Updated routes:");
1839 while (dat + length > p) {
1840 char buf[MAXHOSTNAMELEN + 100];
1841 i = decode_prefix4(p, buf, sizeof(buf));
1842 if (i == -1) {
1843 printf("\n\t (illegal prefix length)");
1844 break;
1845 } else if (i == -2)
1846 goto trunc;
1847 else {
1848 printf("\n\t %s", buf);
1849 p += i;
1853 return;
1854 trunc:
1855 printf("[|BGP]");
1858 static void
1859 bgp_notification_print(const u_char *dat, int length)
1861 struct bgp_notification bgpn;
1862 const u_char *tptr;
1863 char tokbuf[TOKBUFSIZE];
1864 char tokbuf2[TOKBUFSIZE];
1866 TCHECK2(dat[0], BGP_NOTIFICATION_SIZE);
1867 memcpy(&bgpn, dat, BGP_NOTIFICATION_SIZE);
1869 /* some little sanity checking */
1870 if (length<BGP_NOTIFICATION_SIZE)
1871 return;
1873 printf(", %s (%u)",
1874 tok2strbuf(bgp_notify_major_values, "Unknown Error",
1875 bgpn.bgpn_major, tokbuf, sizeof(tokbuf)),
1876 bgpn.bgpn_major);
1878 switch (bgpn.bgpn_major) {
1880 case BGP_NOTIFY_MAJOR_MSG:
1881 printf(", subcode %s (%u)",
1882 tok2strbuf(bgp_notify_minor_msg_values, "Unknown",
1883 bgpn.bgpn_minor, tokbuf, sizeof(tokbuf)),
1884 bgpn.bgpn_minor);
1885 break;
1886 case BGP_NOTIFY_MAJOR_OPEN:
1887 printf(", subcode %s (%u)",
1888 tok2strbuf(bgp_notify_minor_open_values, "Unknown",
1889 bgpn.bgpn_minor, tokbuf, sizeof(tokbuf)),
1890 bgpn.bgpn_minor);
1891 break;
1892 case BGP_NOTIFY_MAJOR_UPDATE:
1893 printf(", subcode %s (%u)",
1894 tok2strbuf(bgp_notify_minor_update_values, "Unknown",
1895 bgpn.bgpn_minor, tokbuf, sizeof(tokbuf)),
1896 bgpn.bgpn_minor);
1897 break;
1898 case BGP_NOTIFY_MAJOR_CAP:
1899 printf(" subcode %s (%u)",
1900 tok2strbuf(bgp_notify_minor_cap_values, "Unknown",
1901 bgpn.bgpn_minor, tokbuf, sizeof(tokbuf)),
1902 bgpn.bgpn_minor);
1903 case BGP_NOTIFY_MAJOR_CEASE:
1904 printf(", subcode %s (%u)",
1905 tok2strbuf(bgp_notify_minor_cease_values, "Unknown",
1906 bgpn.bgpn_minor, tokbuf, sizeof(tokbuf)),
1907 bgpn.bgpn_minor);
1909 /* draft-ietf-idr-cease-subcode-02 mentions optionally 7 bytes
1910 * for the maxprefix subtype, which may contain AFI, SAFI and MAXPREFIXES
1912 if(bgpn.bgpn_minor == BGP_NOTIFY_MINOR_CEASE_MAXPRFX && length >= BGP_NOTIFICATION_SIZE + 7) {
1913 tptr = dat + BGP_NOTIFICATION_SIZE;
1914 TCHECK2(*tptr, 7);
1915 printf(", AFI %s (%u), SAFI %s (%u), Max Prefixes: %u",
1916 tok2strbuf(af_values, "Unknown",
1917 EXTRACT_16BITS(tptr), tokbuf, sizeof(tokbuf)),
1918 EXTRACT_16BITS(tptr),
1919 tok2strbuf(bgp_safi_values, "Unknown", *(tptr+2),
1920 tokbuf2, sizeof(tokbuf)),
1921 *(tptr+2),
1922 EXTRACT_32BITS(tptr+3));
1924 break;
1925 default:
1926 break;
1929 return;
1930 trunc:
1931 printf("[|BGP]");
1934 static void
1935 bgp_route_refresh_print(const u_char *pptr, int len) {
1937 const struct bgp_route_refresh *bgp_route_refresh_header;
1938 char tokbuf[TOKBUFSIZE];
1939 char tokbuf2[TOKBUFSIZE];
1941 TCHECK2(pptr[0], BGP_ROUTE_REFRESH_SIZE);
1943 /* some little sanity checking */
1944 if (len<BGP_ROUTE_REFRESH_SIZE)
1945 return;
1947 bgp_route_refresh_header = (const struct bgp_route_refresh *)pptr;
1949 printf("\n\t AFI %s (%u), SAFI %s (%u)",
1950 tok2strbuf(af_values,"Unknown",
1951 /* this stinks but the compiler pads the structure
1952 * weird */
1953 EXTRACT_16BITS(&bgp_route_refresh_header->afi),
1954 tokbuf, sizeof(tokbuf)),
1955 EXTRACT_16BITS(&bgp_route_refresh_header->afi),
1956 tok2strbuf(bgp_safi_values,"Unknown",
1957 bgp_route_refresh_header->safi,
1958 tokbuf2, sizeof(tokbuf2)),
1959 bgp_route_refresh_header->safi);
1961 if (vflag > 1) {
1962 TCHECK2(*pptr, len);
1963 print_unknown_data(pptr,"\n\t ", len);
1966 return;
1967 trunc:
1968 printf("[|BGP]");
1971 static int
1972 bgp_header_print(const u_char *dat, int length)
1974 struct bgp bgp;
1975 char tokbuf[TOKBUFSIZE];
1977 TCHECK2(dat[0], BGP_SIZE);
1978 memcpy(&bgp, dat, BGP_SIZE);
1979 printf("\n\t%s Message (%u), length: %u",
1980 tok2strbuf(bgp_msg_values, "Unknown", bgp.bgp_type,
1981 tokbuf, sizeof(tokbuf)),
1982 bgp.bgp_type,
1983 length);
1985 switch (bgp.bgp_type) {
1986 case BGP_OPEN:
1987 bgp_open_print(dat, length);
1988 break;
1989 case BGP_UPDATE:
1990 bgp_update_print(dat, length);
1991 break;
1992 case BGP_NOTIFICATION:
1993 bgp_notification_print(dat, length);
1994 break;
1995 case BGP_KEEPALIVE:
1996 break;
1997 case BGP_ROUTE_REFRESH:
1998 bgp_route_refresh_print(dat, length);
1999 break;
2000 default:
2001 /* we have no decoder for the BGP message */
2002 TCHECK2(*dat, length);
2003 printf("\n\t no Message %u decoder",bgp.bgp_type);
2004 print_unknown_data(dat,"\n\t ",length);
2005 break;
2007 return 1;
2008 trunc:
2009 printf("[|BGP]");
2010 return 0;
2013 void
2014 bgp_print(const u_char *dat, int length)
2016 const u_char *p;
2017 const u_char *ep;
2018 const u_char *start;
2019 const u_char marker[] = {
2020 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2021 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2023 struct bgp bgp;
2024 u_int16_t hlen;
2025 char tokbuf[TOKBUFSIZE];
2027 ep = dat + length;
2028 if (snapend < dat + length)
2029 ep = snapend;
2031 printf(": BGP, length: %u",length);
2033 if (vflag < 1) /* lets be less chatty */
2034 return;
2036 p = dat;
2037 start = p;
2038 while (p < ep) {
2039 if (!TTEST2(p[0], 1))
2040 break;
2041 if (p[0] != 0xff) {
2042 p++;
2043 continue;
2046 if (!TTEST2(p[0], sizeof(marker)))
2047 break;
2048 if (memcmp(p, marker, sizeof(marker)) != 0) {
2049 p++;
2050 continue;
2053 /* found BGP header */
2054 TCHECK2(p[0], BGP_SIZE); /*XXX*/
2055 memcpy(&bgp, p, BGP_SIZE);
2057 if (start != p)
2058 printf(" [|BGP]");
2060 hlen = ntohs(bgp.bgp_len);
2061 if (hlen < BGP_SIZE) {
2062 printf("\n[|BGP Bogus header length %u < %u]", hlen,
2063 BGP_SIZE);
2064 break;
2067 if (TTEST2(p[0], hlen)) {
2068 if (!bgp_header_print(p, hlen))
2069 return;
2070 p += hlen;
2071 start = p;
2072 } else {
2073 printf("\n[|BGP %s]",
2074 tok2strbuf(bgp_msg_values,
2075 "Unknown Message Type",
2076 bgp.bgp_type,
2077 tokbuf, sizeof(tokbuf)));
2078 break;
2082 return;
2084 trunc:
2085 printf(" [|BGP]");