2 * Copyright (C) 1999 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * Extensively modified by Hannes Gredler (hannes@juniper.net) for more
30 * complete BGP support.
37 #include <tcpdump-stdinc.h>
42 #include "interface.h"
43 #include "decode_prefix.h"
44 #include "addrtoname.h"
51 u_int8_t bgp_marker
[16];
55 #define BGP_SIZE 19 /* unaligned */
59 #define BGP_NOTIFICATION 3
60 #define BGP_KEEPALIVE 4
61 #define BGP_ROUTE_REFRESH 5
63 static const struct tok bgp_msg_values
[] = {
65 { BGP_UPDATE
, "Update"},
66 { BGP_NOTIFICATION
, "Notification"},
67 { BGP_KEEPALIVE
, "Keepalive"},
68 { BGP_ROUTE_REFRESH
, "Route Refresh"},
73 u_int8_t bgpo_marker
[16];
76 u_int8_t bgpo_version
;
78 u_int16_t bgpo_holdtime
;
81 /* options should follow */
83 #define BGP_OPEN_SIZE 29 /* unaligned */
90 #define BGP_OPT_SIZE 2 /* some compilers may pad to 4 bytes */
91 #define BGP_CAP_HEADER_SIZE 2 /* some compilers may pad to 4 bytes */
93 struct bgp_notification
{
94 u_int8_t bgpn_marker
[16];
100 #define BGP_NOTIFICATION_SIZE 21 /* unaligned */
102 struct bgp_route_refresh
{
103 u_int8_t bgp_marker
[16];
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
112 #define bgp_attr_lenlen(flags, p) \
113 (((flags) & 0x10) ? 2 : 1)
114 #define bgp_attr_len(flags, p) \
115 (((flags) & 0x10) ? EXTRACT_16BITS(p) : *(p))
117 #define BGPTYPE_ORIGIN 1
118 #define BGPTYPE_AS_PATH 2
119 #define BGPTYPE_NEXT_HOP 3
120 #define BGPTYPE_MULTI_EXIT_DISC 4
121 #define BGPTYPE_LOCAL_PREF 5
122 #define BGPTYPE_ATOMIC_AGGREGATE 6
123 #define BGPTYPE_AGGREGATOR 7
124 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
125 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC1998 */
126 #define BGPTYPE_CLUSTER_LIST 10 /* RFC1998 */
127 #define BGPTYPE_DPA 11 /* draft-ietf-idr-bgp-dpa */
128 #define BGPTYPE_ADVERTISERS 12 /* RFC1863 */
129 #define BGPTYPE_RCID_PATH 13 /* RFC1863 */
130 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */
131 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */
132 #define BGPTYPE_EXTD_COMMUNITIES 16 /* draft-ietf-idr-bgp-ext-communities */
133 #define BGPTYPE_AS4_PATH 17 /* RFC4893 */
134 #define BGPTYPE_AGGREGATOR4 18 /* RFC4893 */
135 #define BGPTYPE_PMSI_TUNNEL 22 /* draft-ietf-l3vpn-2547bis-mcast-bgp-02.txt */
136 #define BGPTYPE_ATTR_SET 128 /* draft-marques-ppvpn-ibgp */
138 #define BGP_MP_NLRI_MINSIZE 3 /* End of RIB Marker detection */
140 static const struct tok bgp_attr_values
[] = {
141 { BGPTYPE_ORIGIN
, "Origin"},
142 { BGPTYPE_AS_PATH
, "AS Path"},
143 { BGPTYPE_AS4_PATH
, "AS4 Path"},
144 { BGPTYPE_NEXT_HOP
, "Next Hop"},
145 { BGPTYPE_MULTI_EXIT_DISC
, "Multi Exit Discriminator"},
146 { BGPTYPE_LOCAL_PREF
, "Local Preference"},
147 { BGPTYPE_ATOMIC_AGGREGATE
, "Atomic Aggregate"},
148 { BGPTYPE_AGGREGATOR
, "Aggregator"},
149 { BGPTYPE_AGGREGATOR4
, "Aggregator4"},
150 { BGPTYPE_COMMUNITIES
, "Community"},
151 { BGPTYPE_ORIGINATOR_ID
, "Originator ID"},
152 { BGPTYPE_CLUSTER_LIST
, "Cluster List"},
153 { BGPTYPE_DPA
, "DPA"},
154 { BGPTYPE_ADVERTISERS
, "Advertisers"},
155 { BGPTYPE_RCID_PATH
, "RCID Path / Cluster ID"},
156 { BGPTYPE_MP_REACH_NLRI
, "Multi-Protocol Reach NLRI"},
157 { BGPTYPE_MP_UNREACH_NLRI
, "Multi-Protocol Unreach NLRI"},
158 { BGPTYPE_EXTD_COMMUNITIES
, "Extended Community"},
159 { BGPTYPE_PMSI_TUNNEL
, "PMSI Tunnel"},
160 { BGPTYPE_ATTR_SET
, "Attribute Set"},
161 { 255, "Reserved for development"},
166 #define BGP_AS_SEQUENCE 2
167 #define BGP_CONFED_AS_SEQUENCE 3 /* draft-ietf-idr-rfc3065bis-01 */
168 #define BGP_CONFED_AS_SET 4 /* draft-ietf-idr-rfc3065bis-01 */
170 #define BGP_AS_SEG_TYPE_MIN BGP_AS_SET
171 #define BGP_AS_SEG_TYPE_MAX BGP_CONFED_AS_SET
173 static const struct tok bgp_as_path_segment_open_values
[] = {
174 { BGP_AS_SEQUENCE
, ""},
176 { BGP_CONFED_AS_SEQUENCE
, "( "},
177 { BGP_CONFED_AS_SET
, "({ "},
181 static const struct tok bgp_as_path_segment_close_values
[] = {
182 { BGP_AS_SEQUENCE
, ""},
184 { BGP_CONFED_AS_SEQUENCE
, ")"},
185 { BGP_CONFED_AS_SET
, "})"},
189 #define BGP_OPT_AUTH 1
190 #define BGP_OPT_CAP 2
193 static const struct tok bgp_opt_values
[] = {
194 { BGP_OPT_AUTH
, "Authentication Information"},
195 { BGP_OPT_CAP
, "Capabilities Advertisement"},
199 #define BGP_CAPCODE_MP 1
200 #define BGP_CAPCODE_RR 2
201 #define BGP_CAPCODE_ORF 3 /* XXX */
202 #define BGP_CAPCODE_RESTART 64 /* draft-ietf-idr-restart-05 */
203 #define BGP_CAPCODE_AS_NEW 65 /* XXX */
204 #define BGP_CAPCODE_DYN_CAP 67 /* XXX */
205 #define BGP_CAPCODE_RR_CISCO 128
207 static const struct tok bgp_capcode_values
[] = {
208 { BGP_CAPCODE_MP
, "Multiprotocol Extensions"},
209 { BGP_CAPCODE_RR
, "Route Refresh"},
210 { BGP_CAPCODE_ORF
, "Cooperative Route Filtering"},
211 { BGP_CAPCODE_RESTART
, "Graceful Restart"},
212 { BGP_CAPCODE_AS_NEW
, "32-Bit AS Number"},
213 { BGP_CAPCODE_DYN_CAP
, "Dynamic Capability"},
214 { BGP_CAPCODE_RR_CISCO
, "Route Refresh (Cisco)"},
218 #define BGP_NOTIFY_MAJOR_MSG 1
219 #define BGP_NOTIFY_MAJOR_OPEN 2
220 #define BGP_NOTIFY_MAJOR_UPDATE 3
221 #define BGP_NOTIFY_MAJOR_HOLDTIME 4
222 #define BGP_NOTIFY_MAJOR_FSM 5
223 #define BGP_NOTIFY_MAJOR_CEASE 6
224 #define BGP_NOTIFY_MAJOR_CAP 7
226 static const struct tok bgp_notify_major_values
[] = {
227 { BGP_NOTIFY_MAJOR_MSG
, "Message Header Error"},
228 { BGP_NOTIFY_MAJOR_OPEN
, "OPEN Message Error"},
229 { BGP_NOTIFY_MAJOR_UPDATE
, "UPDATE Message Error"},
230 { BGP_NOTIFY_MAJOR_HOLDTIME
,"Hold Timer Expired"},
231 { BGP_NOTIFY_MAJOR_FSM
, "Finite State Machine Error"},
232 { BGP_NOTIFY_MAJOR_CEASE
, "Cease"},
233 { BGP_NOTIFY_MAJOR_CAP
, "Capability Message Error"},
237 /* draft-ietf-idr-cease-subcode-02 */
238 #define BGP_NOTIFY_MINOR_CEASE_MAXPRFX 1
239 static const struct tok bgp_notify_minor_cease_values
[] = {
240 { BGP_NOTIFY_MINOR_CEASE_MAXPRFX
, "Maximum Number of Prefixes Reached"},
241 { 2, "Administratively Shutdown"},
242 { 3, "Peer Unconfigured"},
243 { 4, "Administratively Reset"},
244 { 5, "Connection Rejected"},
245 { 6, "Other Configuration Change"},
246 { 7, "Connection Collision Resolution"},
250 static const struct tok bgp_notify_minor_msg_values
[] = {
251 { 1, "Connection Not Synchronized"},
252 { 2, "Bad Message Length"},
253 { 3, "Bad Message Type"},
257 static const struct tok bgp_notify_minor_open_values
[] = {
258 { 1, "Unsupported Version Number"},
260 { 3, "Bad BGP Identifier"},
261 { 4, "Unsupported Optional Parameter"},
262 { 5, "Authentication Failure"},
263 { 6, "Unacceptable Hold Time"},
264 { 7, "Capability Message Error"},
268 static const struct tok bgp_notify_minor_update_values
[] = {
269 { 1, "Malformed Attribute List"},
270 { 2, "Unrecognized Well-known Attribute"},
271 { 3, "Missing Well-known Attribute"},
272 { 4, "Attribute Flags Error"},
273 { 5, "Attribute Length Error"},
274 { 6, "Invalid ORIGIN Attribute"},
275 { 7, "AS Routing Loop"},
276 { 8, "Invalid NEXT_HOP Attribute"},
277 { 9, "Optional Attribute Error"},
278 { 10, "Invalid Network Field"},
279 { 11, "Malformed AS_PATH"},
283 static const struct tok bgp_notify_minor_cap_values
[] = {
284 { 1, "Invalid Action Value" },
285 { 2, "Invalid Capability Length" },
286 { 3, "Malformed Capability Value" },
287 { 4, "Unsupported Capability Code" },
291 static const struct tok bgp_origin_values
[] = {
298 #define BGP_PMSI_TUNNEL_RSVP_P2MP 1
299 #define BGP_PMSI_TUNNEL_LDP_P2MP 2
300 #define BGP_PMSI_TUNNEL_PIM_SSM 3
301 #define BGP_PMSI_TUNNEL_PIM_SM 4
302 #define BGP_PMSI_TUNNEL_PIM_BIDIR 5
303 #define BGP_PMSI_TUNNEL_INGRESS 6
304 #define BGP_PMSI_TUNNEL_LDP_MP2MP 7
306 static const struct tok bgp_pmsi_tunnel_values
[] = {
307 { BGP_PMSI_TUNNEL_RSVP_P2MP
, "RSVP-TE P2MP LSP"},
308 { BGP_PMSI_TUNNEL_LDP_P2MP
, "LDP P2MP LSP"},
309 { BGP_PMSI_TUNNEL_PIM_SSM
, "PIM-SSM Tree"},
310 { BGP_PMSI_TUNNEL_PIM_SM
, "PIM-SM Tree"},
311 { BGP_PMSI_TUNNEL_PIM_BIDIR
, "PIM-Bidir Tree"},
312 { BGP_PMSI_TUNNEL_INGRESS
, "Ingress Replication"},
313 { BGP_PMSI_TUNNEL_LDP_MP2MP
, "LDP MP2MP LSP"},
317 static const struct tok bgp_pmsi_flag_values
[] = {
318 { 0x01, "Leaf Information required"},
323 /* Subsequent address family identifier, RFC2283 section 7 */
325 #define SAFNUM_UNICAST 1
326 #define SAFNUM_MULTICAST 2
327 #define SAFNUM_UNIMULTICAST 3
328 /* labeled BGP RFC3107 */
329 #define SAFNUM_LABUNICAST 4
330 /* draft-ietf-l3vpn-2547bis-mcast-bgp-02.txt */
331 #define SAFNUM_MULTICAST_VPN 5
332 #define SAFNUM_TUNNEL 64 /* XXX */
333 #define SAFNUM_VPLS 65 /* XXX */
334 /* draft-nalawade-idr-mdt-safi-03 */
335 #define SAFNUM_MDT 66
336 /* Section 4.3.4 of draft-rosen-rfc2547bis-03.txt */
337 #define SAFNUM_VPNUNICAST 128
338 #define SAFNUM_VPNMULTICAST 129
339 #define SAFNUM_VPNUNIMULTICAST 130
340 /* draft-marques-ppvpn-rt-constrain-01.txt */
341 #define SAFNUM_RT_ROUTING_INFO 132
343 #define BGP_VPN_RD_LEN 8
345 static const struct tok bgp_safi_values
[] = {
346 { SAFNUM_RES
, "Reserved"},
347 { SAFNUM_UNICAST
, "Unicast"},
348 { SAFNUM_MULTICAST
, "Multicast"},
349 { SAFNUM_UNIMULTICAST
, "Unicast+Multicast"},
350 { SAFNUM_LABUNICAST
, "labeled Unicast"},
351 { SAFNUM_TUNNEL
, "Tunnel"},
352 { SAFNUM_VPLS
, "VPLS"},
353 { SAFNUM_MDT
, "MDT"},
354 { SAFNUM_VPNUNICAST
, "labeled VPN Unicast"},
355 { SAFNUM_VPNMULTICAST
, "labeled VPN Multicast"},
356 { SAFNUM_VPNUNIMULTICAST
, "labeled VPN Unicast+Multicast"},
357 { SAFNUM_RT_ROUTING_INFO
, "Route Target Routing Information"},
358 { SAFNUM_MULTICAST_VPN
, "Multicast VPN"},
362 /* well-known community */
363 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
364 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
365 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
367 /* Extended community type - draft-ietf-idr-bgp-ext-communities-05 */
368 #define BGP_EXT_COM_RT_0 0x0002 /* Route Target,Format AS(2bytes):AN(4bytes) */
369 #define BGP_EXT_COM_RT_1 0x0102 /* Route Target,Format IP address:AN(2bytes) */
370 #define BGP_EXT_COM_RT_2 0x0202 /* Route Target,Format AN(4bytes):local(2bytes) */
371 #define BGP_EXT_COM_RO_0 0x0003 /* Route Origin,Format AS(2bytes):AN(4bytes) */
372 #define BGP_EXT_COM_RO_1 0x0103 /* Route Origin,Format IP address:AN(2bytes) */
373 #define BGP_EXT_COM_RO_2 0x0203 /* Route Origin,Format AN(4bytes):local(2bytes) */
374 #define BGP_EXT_COM_LINKBAND 0x4004 /* Link Bandwidth,Format AS(2B):Bandwidth(4B) */
375 /* rfc2547 bgp-mpls-vpns */
376 #define BGP_EXT_COM_VPN_ORIGIN 0x0005 /* OSPF Domain ID / VPN of Origin - draft-rosen-vpns-ospf-bgp-mpls */
377 #define BGP_EXT_COM_VPN_ORIGIN2 0x0105 /* duplicate - keep for backwards compatability */
378 #define BGP_EXT_COM_VPN_ORIGIN3 0x0205 /* duplicate - keep for backwards compatability */
379 #define BGP_EXT_COM_VPN_ORIGIN4 0x8005 /* duplicate - keep for backwards compatability */
381 #define BGP_EXT_COM_OSPF_RTYPE 0x0306 /* OSPF Route Type,Format Area(4B):RouteType(1B):Options(1B) */
382 #define BGP_EXT_COM_OSPF_RTYPE2 0x8000 /* duplicate - keep for backwards compatability */
384 #define BGP_EXT_COM_OSPF_RID 0x0107 /* OSPF Router ID,Format RouterID(4B):Unused(2B) */
385 #define BGP_EXT_COM_OSPF_RID2 0x8001 /* duplicate - keep for backwards compatability */
387 #define BGP_EXT_COM_L2INFO 0x800a /* draft-kompella-ppvpn-l2vpn */
389 #define BGP_EXT_COM_SOURCE_AS 0x0009 /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */
390 #define BGP_EXT_COM_VRF_RT_IMP 0x010b /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */
391 #define BGP_EXT_COM_L2VPN_RT_0 0x000a /* L2VPN Identifier,Format AS(2bytes):AN(4bytes) */
392 #define BGP_EXT_COM_L2VPN_RT_1 0xF10a /* L2VPN Identifier,Format IP address:AN(2bytes) */
395 /* http://www.cisco.com/en/US/tech/tk436/tk428/technologies_tech_note09186a00801eb09a.shtml */
396 #define BGP_EXT_COM_EIGRP_GEN 0x8800
397 #define BGP_EXT_COM_EIGRP_METRIC_AS_DELAY 0x8801
398 #define BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW 0x8802
399 #define BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU 0x8803
400 #define BGP_EXT_COM_EIGRP_EXT_REMAS_REMID 0x8804
401 #define BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC 0x8805
403 static const struct tok bgp_extd_comm_flag_values
[] = {
404 { 0x8000, "vendor-specific"},
405 { 0x4000, "non-transitive"},
409 static const struct tok bgp_extd_comm_subtype_values
[] = {
410 { BGP_EXT_COM_RT_0
, "target"},
411 { BGP_EXT_COM_RT_1
, "target"},
412 { BGP_EXT_COM_RT_2
, "target"},
413 { BGP_EXT_COM_RO_0
, "origin"},
414 { BGP_EXT_COM_RO_1
, "origin"},
415 { BGP_EXT_COM_RO_2
, "origin"},
416 { BGP_EXT_COM_LINKBAND
, "link-BW"},
417 { BGP_EXT_COM_VPN_ORIGIN
, "ospf-domain"},
418 { BGP_EXT_COM_VPN_ORIGIN2
, "ospf-domain"},
419 { BGP_EXT_COM_VPN_ORIGIN3
, "ospf-domain"},
420 { BGP_EXT_COM_VPN_ORIGIN4
, "ospf-domain"},
421 { BGP_EXT_COM_OSPF_RTYPE
, "ospf-route-type"},
422 { BGP_EXT_COM_OSPF_RTYPE2
, "ospf-route-type"},
423 { BGP_EXT_COM_OSPF_RID
, "ospf-router-id"},
424 { BGP_EXT_COM_OSPF_RID2
, "ospf-router-id"},
425 { BGP_EXT_COM_L2INFO
, "layer2-info"},
426 { BGP_EXT_COM_EIGRP_GEN
, "eigrp-general-route (flag, tag)" },
427 { BGP_EXT_COM_EIGRP_METRIC_AS_DELAY
, "eigrp-route-metric (AS, delay)" },
428 { BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW
, "eigrp-route-metric (reliability, nexthop, bandwidth)" },
429 { BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU
, "eigrp-route-metric (load, MTU)" },
430 { BGP_EXT_COM_EIGRP_EXT_REMAS_REMID
, "eigrp-external-route (remote-AS, remote-ID)" },
431 { BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC
, "eigrp-external-route (remote-proto, remote-metric)" },
432 { BGP_EXT_COM_SOURCE_AS
, "source-AS" },
433 { BGP_EXT_COM_VRF_RT_IMP
, "vrf-route-import"},
434 { BGP_EXT_COM_L2VPN_RT_0
, "l2vpn-id"},
435 { BGP_EXT_COM_L2VPN_RT_1
, "l2vpn-id"},
439 /* OSPF codes for BGP_EXT_COM_OSPF_RTYPE draft-rosen-vpns-ospf-bgp-mpls */
440 #define BGP_OSPF_RTYPE_RTR 1 /* OSPF Router LSA */
441 #define BGP_OSPF_RTYPE_NET 2 /* OSPF Network LSA */
442 #define BGP_OSPF_RTYPE_SUM 3 /* OSPF Summary LSA */
443 #define BGP_OSPF_RTYPE_EXT 5 /* OSPF External LSA, note that ASBR doesn't apply to MPLS-VPN */
444 #define BGP_OSPF_RTYPE_NSSA 7 /* OSPF NSSA External*/
445 #define BGP_OSPF_RTYPE_SHAM 129 /* OSPF-MPLS-VPN Sham link */
446 #define BGP_OSPF_RTYPE_METRIC_TYPE 0x1 /* LSB of RTYPE Options Field */
448 static const struct tok bgp_extd_comm_ospf_rtype_values
[] = {
449 { BGP_OSPF_RTYPE_RTR
, "Router" },
450 { BGP_OSPF_RTYPE_NET
, "Network" },
451 { BGP_OSPF_RTYPE_SUM
, "Summary" },
452 { BGP_OSPF_RTYPE_EXT
, "External" },
453 { BGP_OSPF_RTYPE_NSSA
,"NSSA External" },
454 { BGP_OSPF_RTYPE_SHAM
,"MPLS-VPN Sham" },
458 #define TOKBUFSIZE 128
459 static char astostr
[20];
464 * Convert an AS number into a string and return string pointer.
466 * Bepending on bflag is set or not, AS number is converted into ASDOT notation
467 * or plain number notation.
471 as_printf (char *str
, int size
, u_int asnum
)
473 if (!bflag
|| asnum
<= 0xFFFF) {
474 snprintf(str
, size
, "%u", asnum
);
476 snprintf(str
, size
, "%u.%u", asnum
>> 16, asnum
& 0xFFFF);
481 #define ITEMCHECK(minlen) if (itemlen < minlen) goto badtlv;
484 decode_prefix4(const u_char
*pptr
, u_int itemlen
, char *buf
, u_int buflen
)
487 u_int plen
, plenbytes
;
496 memset(&addr
, 0, sizeof(addr
));
497 plenbytes
= (plen
+ 7) / 8;
498 TCHECK2(pptr
[1], plenbytes
);
499 ITEMCHECK(plenbytes
);
500 memcpy(&addr
, &pptr
[1], plenbytes
);
502 ((u_char
*)&addr
)[plenbytes
- 1] &=
503 ((0xff00 >> (plen
% 8)) & 0xff);
505 snprintf(buf
, buflen
, "%s/%d", getname((u_char
*)&addr
), plen
);
506 return 1 + plenbytes
;
516 decode_labeled_prefix4(const u_char
*pptr
, u_int itemlen
, char *buf
, u_int buflen
)
519 u_int plen
, plenbytes
;
521 /* prefix length and label = 4 bytes */
524 plen
= pptr
[0]; /* get prefix length */
526 /* this is one of the weirdnesses of rfc3107
527 the label length (actually the label + COS bits)
528 is added to the prefix length;
529 we also do only read out just one label -
530 there is no real application for advertisement of
531 stacked labels in a a single BGP message
537 plen
-=24; /* adjust prefixlen - labellength */
543 memset(&addr
, 0, sizeof(addr
));
544 plenbytes
= (plen
+ 7) / 8;
545 TCHECK2(pptr
[4], plenbytes
);
546 ITEMCHECK(plenbytes
);
547 memcpy(&addr
, &pptr
[4], plenbytes
);
549 ((u_char
*)&addr
)[plenbytes
- 1] &=
550 ((0xff00 >> (plen
% 8)) & 0xff);
552 /* the label may get offsetted by 4 bits so lets shift it right */
553 snprintf(buf
, buflen
, "%s/%d, label:%u %s",
554 getname((u_char
*)&addr
),
556 EXTRACT_24BITS(pptr
+1)>>4,
557 ((pptr
[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
559 return 4 + plenbytes
;
571 * print an ipv4 or ipv6 address into a buffer dependend on address length.
574 bgp_vpn_ip_print (const u_char
*pptr
, u_int addr_length
) {
576 /* worst case string is s fully formatted v6 address */
577 static char addr
[sizeof("1234:5678:89ab:cdef:1234:5678:89ab:cdef")];
580 switch(addr_length
) {
581 case (sizeof(struct in_addr
) << 3): /* 32 */
582 TCHECK2(pptr
[0], sizeof(struct in_addr
));
583 snprintf(pos
, sizeof(addr
), "%s", ipaddr_string(pptr
));
586 case (sizeof(struct in6_addr
) << 3): /* 128 */
587 TCHECK2(pptr
[0], sizeof(struct in6_addr
));
588 snprintf(pos
, sizeof(addr
), "%s", ip6addr_string(pptr
));
592 snprintf(pos
, sizeof(addr
), "bogus address length %u", addr_length
);
605 * print an multicast s,g entry into a buffer.
606 * the s,g entry is encoded like this.
608 * +-----------------------------------+
609 * | Multicast Source Length (1 octet) |
610 * +-----------------------------------+
611 * | Multicast Source (Variable) |
612 * +-----------------------------------+
613 * | Multicast Group Length (1 octet) |
614 * +-----------------------------------+
615 * | Multicast Group (Variable) |
616 * +-----------------------------------+
618 * return the number of bytes read from the wire.
621 bgp_vpn_sg_print (const u_char
*pptr
, char *buf
, u_int buflen
) {
623 u_int8_t addr_length
;
624 u_int total_length
, offset
;
628 /* Source address length, encoded in bits */
630 addr_length
= *pptr
++;
633 TCHECK2(pptr
[0], (addr_length
>> 3));
634 total_length
+= (addr_length
>> 3) + 1;
635 offset
= strlen(buf
);
637 snprintf(buf
+ offset
, buflen
- offset
, ", Source %s",
638 bgp_vpn_ip_print(pptr
, addr_length
));
639 pptr
+= (addr_length
>> 3);
642 /* Group address length, encoded in bits */
644 addr_length
= *pptr
++;
647 TCHECK2(pptr
[0], (addr_length
>> 3));
648 total_length
+= (addr_length
>> 3) + 1;
649 offset
= strlen(buf
);
651 snprintf(buf
+ offset
, buflen
- offset
, ", Group %s",
652 bgp_vpn_ip_print(pptr
, addr_length
));
653 pptr
+= (addr_length
>> 3);
657 return (total_length
);
661 /* RDs and RTs share the same semantics
662 * we use bgp_vpn_rd_print for
663 * printing route targets inside a NLRI */
665 bgp_vpn_rd_print (const u_char
*pptr
) {
667 /* allocate space for the largest possible string */
668 static char rd
[sizeof("xxxxxxxxxx:xxxxx (xxx.xxx.xxx.xxx:xxxxx)")];
671 /* ok lets load the RD format */
672 switch (EXTRACT_16BITS(pptr
)) {
674 /* 2-byte-AS:number fmt*/
676 snprintf(pos
, sizeof(rd
) - (pos
- rd
), "%u:%u (= %u.%u.%u.%u)",
677 EXTRACT_16BITS(pptr
+2),
678 EXTRACT_32BITS(pptr
+4),
679 *(pptr
+4), *(pptr
+5), *(pptr
+6), *(pptr
+7));
681 /* IP-address:AS fmt*/
684 snprintf(pos
, sizeof(rd
) - (pos
- rd
), "%u.%u.%u.%u:%u",
685 *(pptr
+2), *(pptr
+3), *(pptr
+4), *(pptr
+5), EXTRACT_16BITS(pptr
+6));
688 /* 4-byte-AS:number fmt*/
690 snprintf(pos
, sizeof(rd
) - (pos
- rd
), "%s:%u (%u.%u.%u.%u:%u)",
691 as_printf(astostr
, sizeof(astostr
), EXTRACT_32BITS(pptr
+2)),
692 EXTRACT_16BITS(pptr
+6), *(pptr
+2), *(pptr
+3), *(pptr
+4),
693 *(pptr
+5), EXTRACT_16BITS(pptr
+6));
696 snprintf(pos
, sizeof(rd
) - (pos
- rd
), "unknown RD format");
705 decode_rt_routing_info(const u_char
*pptr
, char *buf
, u_int buflen
)
707 u_int8_t route_target
[8];
711 plen
= pptr
[0]; /* get prefix length */
714 return 1; /* default route target */
719 plen
-=32; /* adjust prefix length */
724 memset(&route_target
, 0, sizeof(route_target
));
725 TCHECK2(pptr
[1], (plen
+ 7) / 8);
726 memcpy(&route_target
, &pptr
[1], (plen
+ 7) / 8);
728 ((u_char
*)&route_target
)[(plen
+ 7) / 8 - 1] &=
729 ((0xff00 >> (plen
% 8)) & 0xff);
731 snprintf(buf
, buflen
, "origin AS: %s, route target %s",
732 as_printf(astostr
, sizeof(astostr
), EXTRACT_32BITS(pptr
+1)),
733 bgp_vpn_rd_print((u_char
*)&route_target
));
735 return 5 + (plen
+ 7) / 8;
742 decode_labeled_vpn_prefix4(const u_char
*pptr
, char *buf
, u_int buflen
)
748 plen
= pptr
[0]; /* get prefix length */
753 plen
-=(24+64); /* adjust prefixlen - labellength - RD len*/
758 memset(&addr
, 0, sizeof(addr
));
759 TCHECK2(pptr
[12], (plen
+ 7) / 8);
760 memcpy(&addr
, &pptr
[12], (plen
+ 7) / 8);
762 ((u_char
*)&addr
)[(plen
+ 7) / 8 - 1] &=
763 ((0xff00 >> (plen
% 8)) & 0xff);
765 /* the label may get offsetted by 4 bits so lets shift it right */
766 snprintf(buf
, buflen
, "RD: %s, %s/%d, label:%u %s",
767 bgp_vpn_rd_print(pptr
+4),
768 getname((u_char
*)&addr
),
770 EXTRACT_24BITS(pptr
+1)>>4,
771 ((pptr
[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
773 return 12 + (plen
+ 7) / 8;
780 * +-------------------------------+
782 * | RD:IPv4-address (12 octets) |
784 * +-------------------------------+
785 * | MDT Group-address (4 octets) |
786 * +-------------------------------+
789 #define MDT_VPN_NLRI_LEN 16
792 decode_mdt_vpn_nlri(const u_char
*pptr
, char *buf
, u_int buflen
)
796 const u_char
*vpn_ip
;
800 /* if the NLRI is not predefined length, quit.*/
801 if (*pptr
!= MDT_VPN_NLRI_LEN
* NBBY
)
811 TCHECK2(pptr
[0], sizeof(struct in_addr
));
813 pptr
+=sizeof(struct in_addr
);
815 /* MDT Group Address */
816 TCHECK2(pptr
[0], sizeof(struct in_addr
));
818 snprintf(buf
, buflen
, "RD: %s, VPN IP Address: %s, MC Group Address: %s",
819 bgp_vpn_rd_print(rd
), ipaddr_string(vpn_ip
), ipaddr_string(pptr
));
821 return MDT_VPN_NLRI_LEN
+ 1;
828 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI 1
829 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI 2
830 #define BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI 3
831 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF 4
832 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE 5
833 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN 6
834 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN 7
836 static const struct tok bgp_multicast_vpn_route_type_values
[] = {
837 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI
, "Intra-AS I-PMSI"},
838 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI
, "Inter-AS I-PMSI"},
839 { BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI
, "S-PMSI"},
840 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF
, "Intra-AS Segment-Leaf"},
841 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE
, "Source-Active"},
842 { BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN
, "Shared Tree Join"},
843 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN
, "Source Tree Join"},
847 decode_multicast_vpn(const u_char
*pptr
, char *buf
, u_int buflen
)
849 u_int8_t route_type
, route_length
, addr_length
, sg_length
;
853 route_type
= *pptr
++;
854 route_length
= *pptr
++;
856 snprintf(buf
, buflen
, "Route-Type: %s (%u), length: %u",
857 tok2str(bgp_multicast_vpn_route_type_values
,
858 "Unknown", route_type
),
859 route_type
, route_length
);
862 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI
:
863 TCHECK2(pptr
[0], BGP_VPN_RD_LEN
);
864 offset
= strlen(buf
);
865 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s, Originator %s",
866 bgp_vpn_rd_print(pptr
),
867 bgp_vpn_ip_print(pptr
+ BGP_VPN_RD_LEN
,
868 (route_length
- BGP_VPN_RD_LEN
) << 3));
870 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI
:
871 TCHECK2(pptr
[0], BGP_VPN_RD_LEN
+ 4);
872 offset
= strlen(buf
);
873 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s, Source-AS %s",
874 bgp_vpn_rd_print(pptr
),
875 as_printf(astostr
, sizeof(astostr
),
876 EXTRACT_32BITS(pptr
+ BGP_VPN_RD_LEN
)));
879 case BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI
:
880 TCHECK2(pptr
[0], BGP_VPN_RD_LEN
);
881 offset
= strlen(buf
);
882 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s",
883 bgp_vpn_rd_print(pptr
));
884 pptr
+= BGP_VPN_RD_LEN
;
886 sg_length
= bgp_vpn_sg_print(pptr
, buf
, buflen
);
887 addr_length
= route_length
- sg_length
;
889 TCHECK2(pptr
[0], addr_length
);
890 offset
= strlen(buf
);
891 snprintf(buf
+ offset
, buflen
- offset
, ", Originator %s",
892 bgp_vpn_ip_print(pptr
, addr_length
<< 3));
895 case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE
:
896 TCHECK2(pptr
[0], BGP_VPN_RD_LEN
);
897 offset
= strlen(buf
);
898 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s",
899 bgp_vpn_rd_print(pptr
));
900 pptr
+= BGP_VPN_RD_LEN
;
902 bgp_vpn_sg_print(pptr
, buf
, buflen
);
905 case BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN
: /* fall through */
906 case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN
:
907 TCHECK2(pptr
[0], BGP_VPN_RD_LEN
);
908 offset
= strlen(buf
);
909 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s, Source-AS %s",
910 bgp_vpn_rd_print(pptr
),
911 as_printf(astostr
, sizeof(astostr
),
912 EXTRACT_32BITS(pptr
+ BGP_VPN_RD_LEN
)));
913 pptr
+= BGP_VPN_RD_LEN
;
915 bgp_vpn_sg_print(pptr
, buf
, buflen
);
919 * no per route-type printing yet.
921 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF
:
926 return route_length
+ 2;
933 * As I remember, some versions of systems have an snprintf() that
934 * returns -1 if the buffer would have overflowed. If the return
935 * value is negative, set buflen to 0, to indicate that we've filled
938 * If the return value is greater than buflen, that means that
939 * the buffer would have overflowed; again, set buflen to 0 in
942 #define UPDATE_BUF_BUFLEN(buf, buflen, strlen) \
945 else if ((u_int)strlen>buflen) \
953 decode_labeled_vpn_l2(const u_char
*pptr
, char *buf
, u_int buflen
)
955 int plen
,tlen
,strlen
,tlv_type
,tlv_len
,ttlv_len
;
958 plen
=EXTRACT_16BITS(pptr
);
961 /* Old and new L2VPN NLRI share AFI/SAFI
962 * -> Assume a 12 Byte-length NLRI is auto-discovery-only
963 * and > 17 as old format. Complain for the middle case
966 /* assume AD-only with RD, BGPNH */
969 strlen
=snprintf(buf
, buflen
, "RD: %s, BGPNH: %s",
970 bgp_vpn_rd_print(pptr
),
971 /* need something like getname() here */
974 UPDATE_BUF_BUFLEN(buf
, buflen
, strlen
);
978 } else if (plen
>17) {
979 /* assume old format */
980 /* RD, ID, LBLKOFF, LBLBASE */
984 strlen
=snprintf(buf
, buflen
, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
985 bgp_vpn_rd_print(pptr
),
986 EXTRACT_16BITS(pptr
+8),
987 EXTRACT_16BITS(pptr
+10),
988 EXTRACT_24BITS(pptr
+12)>>4); /* the label is offsetted by 4 bits so lets shift it right */
989 UPDATE_BUF_BUFLEN(buf
, buflen
, strlen
);
993 /* ok now the variable part - lets read out TLVs*/
999 tlv_len
=EXTRACT_16BITS(pptr
);
1006 strlen
=snprintf(buf
,buflen
, "\n\t\tcircuit status vector (%u) length: %u: 0x",
1009 UPDATE_BUF_BUFLEN(buf
, buflen
, strlen
);
1011 ttlv_len
=ttlv_len
/8+1; /* how many bytes do we need to read ? */
1012 while (ttlv_len
>0) {
1015 strlen
=snprintf(buf
,buflen
, "%02x",*pptr
++);
1016 UPDATE_BUF_BUFLEN(buf
, buflen
, strlen
);
1023 strlen
=snprintf(buf
,buflen
, "\n\t\tunknown TLV #%u, length: %u",
1026 UPDATE_BUF_BUFLEN(buf
, buflen
, strlen
);
1030 tlen
-=(tlv_len
<<3); /* the tlv-length is expressed in bits so lets shift it right */
1035 /* complain bitterly ? */
1046 decode_prefix6(const u_char
*pd
, u_int itemlen
, char *buf
, u_int buflen
)
1048 struct in6_addr addr
;
1049 u_int plen
, plenbytes
;
1058 memset(&addr
, 0, sizeof(addr
));
1059 plenbytes
= (plen
+ 7) / 8;
1060 TCHECK2(pd
[1], plenbytes
);
1061 ITEMCHECK(plenbytes
);
1062 memcpy(&addr
, &pd
[1], plenbytes
);
1064 addr
.s6_addr
[plenbytes
- 1] &=
1065 ((0xff00 >> (plen
% 8)) & 0xff);
1067 snprintf(buf
, buflen
, "%s/%d", getname6((u_char
*)&addr
), plen
);
1068 return 1 + plenbytes
;
1078 decode_labeled_prefix6(const u_char
*pptr
, u_int itemlen
, char *buf
, u_int buflen
)
1080 struct in6_addr addr
;
1081 u_int plen
, plenbytes
;
1083 /* prefix length and label = 4 bytes */
1084 TCHECK2(pptr
[0], 4);
1086 plen
= pptr
[0]; /* get prefix length */
1091 plen
-=24; /* adjust prefixlen - labellength */
1097 memset(&addr
, 0, sizeof(addr
));
1098 plenbytes
= (plen
+ 7) / 8;
1099 TCHECK2(pptr
[4], plenbytes
);
1100 memcpy(&addr
, &pptr
[4], plenbytes
);
1102 addr
.s6_addr
[plenbytes
- 1] &=
1103 ((0xff00 >> (plen
% 8)) & 0xff);
1105 /* the label may get offsetted by 4 bits so lets shift it right */
1106 snprintf(buf
, buflen
, "%s/%d, label:%u %s",
1107 getname6((u_char
*)&addr
),
1109 EXTRACT_24BITS(pptr
+1)>>4,
1110 ((pptr
[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1112 return 4 + plenbytes
;
1122 decode_labeled_vpn_prefix6(const u_char
*pptr
, char *buf
, u_int buflen
)
1124 struct in6_addr addr
;
1128 plen
= pptr
[0]; /* get prefix length */
1133 plen
-=(24+64); /* adjust prefixlen - labellength - RD len*/
1138 memset(&addr
, 0, sizeof(addr
));
1139 TCHECK2(pptr
[12], (plen
+ 7) / 8);
1140 memcpy(&addr
, &pptr
[12], (plen
+ 7) / 8);
1142 addr
.s6_addr
[(plen
+ 7) / 8 - 1] &=
1143 ((0xff00 >> (plen
% 8)) & 0xff);
1145 /* the label may get offsetted by 4 bits so lets shift it right */
1146 snprintf(buf
, buflen
, "RD: %s, %s/%d, label:%u %s",
1147 bgp_vpn_rd_print(pptr
+4),
1148 getname6((u_char
*)&addr
),
1150 EXTRACT_24BITS(pptr
+1)>>4,
1151 ((pptr
[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1153 return 12 + (plen
+ 7) / 8;
1161 decode_clnp_prefix(const u_char
*pptr
, char *buf
, u_int buflen
)
1167 plen
= pptr
[0]; /* get prefix length */
1172 memset(&addr
, 0, sizeof(addr
));
1173 TCHECK2(pptr
[4], (plen
+ 7) / 8);
1174 memcpy(&addr
, &pptr
[4], (plen
+ 7) / 8);
1176 addr
[(plen
+ 7) / 8 - 1] &=
1177 ((0xff00 >> (plen
% 8)) & 0xff);
1179 snprintf(buf
, buflen
, "%s/%d",
1180 isonsap_string(addr
,(plen
+ 7) / 8),
1183 return 1 + (plen
+ 7) / 8;
1190 decode_labeled_vpn_clnp_prefix(const u_char
*pptr
, char *buf
, u_int buflen
)
1196 plen
= pptr
[0]; /* get prefix length */
1201 plen
-=(24+64); /* adjust prefixlen - labellength - RD len*/
1206 memset(&addr
, 0, sizeof(addr
));
1207 TCHECK2(pptr
[12], (plen
+ 7) / 8);
1208 memcpy(&addr
, &pptr
[12], (plen
+ 7) / 8);
1210 addr
[(plen
+ 7) / 8 - 1] &=
1211 ((0xff00 >> (plen
% 8)) & 0xff);
1213 /* the label may get offsetted by 4 bits so lets shift it right */
1214 snprintf(buf
, buflen
, "RD: %s, %s/%d, label:%u %s",
1215 bgp_vpn_rd_print(pptr
+4),
1216 isonsap_string(addr
,(plen
+ 7) / 8),
1218 EXTRACT_24BITS(pptr
+1)>>4,
1219 ((pptr
[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1221 return 12 + (plen
+ 7) / 8;
1228 * bgp_attr_get_as_size
1230 * Try to find the size of the ASs encoded in an as-path. It is not obvious, as
1231 * both Old speakers that do not support 4 byte AS, and the new speakers that do
1232 * support, exchange AS-Path with the same path-attribute type value 0x02.
1235 bgp_attr_get_as_size (u_int8_t bgpa_type
, const u_char
*pptr
, int len
)
1237 const u_char
*tptr
= pptr
;
1240 * If the path attribute is the optional AS4 path type, then we already
1241 * know, that ASs must be encoded in 4 byte format.
1243 if (bgpa_type
== BGPTYPE_AS4_PATH
) {
1248 * Let us assume that ASs are of 2 bytes in size, and check if the AS-Path
1249 * TLV is good. If not, ask the caller to try with AS encoded as 4 bytes
1252 while (tptr
< pptr
+ len
) {
1256 * If we do not find a valid segment type, our guess might be wrong.
1258 if (tptr
[0] < BGP_AS_SEG_TYPE_MIN
|| tptr
[0] > BGP_AS_SEG_TYPE_MAX
) {
1262 tptr
+= 2 + tptr
[1] * 2;
1266 * If we correctly reached end of the AS path attribute data content,
1267 * then most likely ASs were indeed encoded as 2 bytes.
1269 if (tptr
== pptr
+ len
) {
1276 * We can come here, either we did not have enough data, or if we
1277 * try to decode 4 byte ASs in 2 byte format. Either way, return 4,
1278 * so that calller can try to decode each AS as of 4 bytes. If indeed
1279 * there was not enough data, it will crib and end the parse anyways.
1285 bgp_attr_print(u_int atype
, const u_char
*pptr
, u_int len
)
1289 u_int8_t safi
, snpa
, nhlen
;
1290 union { /* copy buffer for bandwidth values */
1297 char buf
[MAXHOSTNAMELEN
+ 100];
1298 char tokbuf
[TOKBUFSIZE
];
1305 case BGPTYPE_ORIGIN
:
1307 printf("invalid len");
1310 printf("%s", tok2strbuf(bgp_origin_values
,
1311 "Unknown Origin Typecode",
1313 tokbuf
, sizeof(tokbuf
)));
1319 * Process AS4 byte path and AS2 byte path attributes here.
1321 case BGPTYPE_AS4_PATH
:
1322 case BGPTYPE_AS_PATH
:
1324 printf("invalid len");
1333 * BGP updates exchanged between New speakers that support 4
1334 * byte AS, ASs are always encoded in 4 bytes. There is no
1335 * definitive way to find this, just by the packet's
1336 * contents. So, check for packet's TLV's sanity assuming
1337 * 2 bytes first, and it does not pass, assume that ASs are
1338 * encoded in 4 bytes format and move on.
1340 as_size
= bgp_attr_get_as_size(atype
, pptr
, len
);
1342 while (tptr
< pptr
+ len
) {
1344 printf("%s", tok2strbuf(bgp_as_path_segment_open_values
,
1346 tokbuf
, sizeof(tokbuf
)));
1347 for (i
= 0; i
< tptr
[1] * as_size
; i
+= as_size
) {
1348 TCHECK2(tptr
[2 + i
], as_size
);
1350 as_printf(astostr
, sizeof(astostr
),
1352 EXTRACT_16BITS(&tptr
[2 + i
]) :
1353 EXTRACT_32BITS(&tptr
[2 + i
])));
1356 printf("%s", tok2strbuf(bgp_as_path_segment_close_values
,
1358 tokbuf
, sizeof(tokbuf
)));
1360 tptr
+= 2 + tptr
[1] * as_size
;
1363 case BGPTYPE_NEXT_HOP
:
1365 printf("invalid len");
1367 TCHECK2(tptr
[0], 4);
1368 printf("%s", getname(tptr
));
1371 case BGPTYPE_MULTI_EXIT_DISC
:
1372 case BGPTYPE_LOCAL_PREF
:
1374 printf("invalid len");
1376 TCHECK2(tptr
[0], 4);
1377 printf("%u", EXTRACT_32BITS(tptr
));
1380 case BGPTYPE_ATOMIC_AGGREGATE
:
1382 printf("invalid len");
1384 case BGPTYPE_AGGREGATOR
:
1387 * Depending on the AS encoded is of 2 bytes or of 4 bytes,
1388 * the length of this PA can be either 6 bytes or 8 bytes.
1390 if (len
!= 6 && len
!= 8) {
1391 printf("invalid len");
1394 TCHECK2(tptr
[0], len
);
1396 printf(" AS #%s, origin %s",
1397 as_printf(astostr
, sizeof(astostr
), EXTRACT_16BITS(tptr
)),
1400 printf(" AS #%s, origin %s",
1401 as_printf(astostr
, sizeof(astostr
),
1402 EXTRACT_32BITS(tptr
)), getname(tptr
+ 4));
1405 case BGPTYPE_AGGREGATOR4
:
1407 printf("invalid len");
1410 TCHECK2(tptr
[0], 8);
1411 printf(" AS #%s, origin %s",
1412 as_printf(astostr
, sizeof(astostr
), EXTRACT_32BITS(tptr
)),
1415 case BGPTYPE_COMMUNITIES
:
1417 printf("invalid len");
1422 TCHECK2(tptr
[0], 4);
1423 comm
= EXTRACT_32BITS(tptr
);
1425 case BGP_COMMUNITY_NO_EXPORT
:
1426 printf(" NO_EXPORT");
1428 case BGP_COMMUNITY_NO_ADVERT
:
1429 printf(" NO_ADVERTISE");
1431 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED
:
1432 printf(" NO_EXPORT_SUBCONFED");
1436 (comm
>> 16) & 0xffff,
1438 (tlen
>4) ? ", " : "");
1445 case BGPTYPE_ORIGINATOR_ID
:
1447 printf("invalid len");
1450 TCHECK2(tptr
[0], 4);
1451 printf("%s",getname(tptr
));
1453 case BGPTYPE_CLUSTER_LIST
:
1455 printf("invalid len");
1459 TCHECK2(tptr
[0], 4);
1462 (tlen
>4) ? ", " : "");
1467 case BGPTYPE_MP_REACH_NLRI
:
1468 TCHECK2(tptr
[0], 3);
1469 af
= EXTRACT_16BITS(tptr
);
1472 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
1473 tok2strbuf(af_values
, "Unknown AFI", af
,
1474 tokbuf
, sizeof(tokbuf
)),
1476 (safi
>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1477 tok2strbuf(bgp_safi_values
, "Unknown SAFI", safi
,
1478 tokbuf
, sizeof(tokbuf
)),
1481 switch(af
<<8 | safi
) {
1482 case (AFNUM_INET
<<8 | SAFNUM_UNICAST
):
1483 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST
):
1484 case (AFNUM_INET
<<8 | SAFNUM_UNIMULTICAST
):
1485 case (AFNUM_INET
<<8 | SAFNUM_LABUNICAST
):
1486 case (AFNUM_INET
<<8 | SAFNUM_RT_ROUTING_INFO
):
1487 case (AFNUM_INET
<<8 | SAFNUM_VPNUNICAST
):
1488 case (AFNUM_INET
<<8 | SAFNUM_VPNMULTICAST
):
1489 case (AFNUM_INET
<<8 | SAFNUM_VPNUNIMULTICAST
):
1490 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST_VPN
):
1491 case (AFNUM_INET
<<8 | SAFNUM_MDT
):
1493 case (AFNUM_INET6
<<8 | SAFNUM_UNICAST
):
1494 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST
):
1495 case (AFNUM_INET6
<<8 | SAFNUM_UNIMULTICAST
):
1496 case (AFNUM_INET6
<<8 | SAFNUM_LABUNICAST
):
1497 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNICAST
):
1498 case (AFNUM_INET6
<<8 | SAFNUM_VPNMULTICAST
):
1499 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNIMULTICAST
):
1501 case (AFNUM_NSAP
<<8 | SAFNUM_UNICAST
):
1502 case (AFNUM_NSAP
<<8 | SAFNUM_MULTICAST
):
1503 case (AFNUM_NSAP
<<8 | SAFNUM_UNIMULTICAST
):
1504 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNICAST
):
1505 case (AFNUM_NSAP
<<8 | SAFNUM_VPNMULTICAST
):
1506 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNIMULTICAST
):
1507 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNICAST
):
1508 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNMULTICAST
):
1509 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNIMULTICAST
):
1510 case (AFNUM_VPLS
<<8 | SAFNUM_VPLS
):
1513 TCHECK2(tptr
[0], tlen
);
1514 printf("\n\t no AFI %u / SAFI %u decoder",af
,safi
);
1516 print_unknown_data(gndo
,tptr
,"\n\t ",tlen
);
1530 printf("\n\t nexthop: ");
1535 switch(af
<<8 | safi
) {
1536 case (AFNUM_INET
<<8 | SAFNUM_UNICAST
):
1537 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST
):
1538 case (AFNUM_INET
<<8 | SAFNUM_UNIMULTICAST
):
1539 case (AFNUM_INET
<<8 | SAFNUM_LABUNICAST
):
1540 case (AFNUM_INET
<<8 | SAFNUM_RT_ROUTING_INFO
):
1541 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST_VPN
):
1542 case (AFNUM_INET
<<8 | SAFNUM_MDT
):
1543 if (tlen
< (int)sizeof(struct in_addr
)) {
1544 printf("invalid len");
1547 TCHECK2(tptr
[0], sizeof(struct in_addr
));
1548 printf("%s",getname(tptr
));
1549 tlen
-= sizeof(struct in_addr
);
1550 tptr
+= sizeof(struct in_addr
);
1553 case (AFNUM_INET
<<8 | SAFNUM_VPNUNICAST
):
1554 case (AFNUM_INET
<<8 | SAFNUM_VPNMULTICAST
):
1555 case (AFNUM_INET
<<8 | SAFNUM_VPNUNIMULTICAST
):
1556 if (tlen
< (int)(sizeof(struct in_addr
)+BGP_VPN_RD_LEN
)) {
1557 printf("invalid len");
1560 TCHECK2(tptr
[0], sizeof(struct in_addr
)+BGP_VPN_RD_LEN
);
1561 printf("RD: %s, %s",
1562 bgp_vpn_rd_print(tptr
),
1563 getname(tptr
+BGP_VPN_RD_LEN
));
1564 tlen
-= (sizeof(struct in_addr
)+BGP_VPN_RD_LEN
);
1565 tptr
+= (sizeof(struct in_addr
)+BGP_VPN_RD_LEN
);
1569 case (AFNUM_INET6
<<8 | SAFNUM_UNICAST
):
1570 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST
):
1571 case (AFNUM_INET6
<<8 | SAFNUM_UNIMULTICAST
):
1572 case (AFNUM_INET6
<<8 | SAFNUM_LABUNICAST
):
1573 if (tlen
< (int)sizeof(struct in6_addr
)) {
1574 printf("invalid len");
1577 TCHECK2(tptr
[0], sizeof(struct in6_addr
));
1578 printf("%s", getname6(tptr
));
1579 tlen
-= sizeof(struct in6_addr
);
1580 tptr
+= sizeof(struct in6_addr
);
1583 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNICAST
):
1584 case (AFNUM_INET6
<<8 | SAFNUM_VPNMULTICAST
):
1585 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNIMULTICAST
):
1586 if (tlen
< (int)(sizeof(struct in6_addr
)+BGP_VPN_RD_LEN
)) {
1587 printf("invalid len");
1590 TCHECK2(tptr
[0], sizeof(struct in6_addr
)+BGP_VPN_RD_LEN
);
1591 printf("RD: %s, %s",
1592 bgp_vpn_rd_print(tptr
),
1593 getname6(tptr
+BGP_VPN_RD_LEN
));
1594 tlen
-= (sizeof(struct in6_addr
)+BGP_VPN_RD_LEN
);
1595 tptr
+= (sizeof(struct in6_addr
)+BGP_VPN_RD_LEN
);
1599 case (AFNUM_VPLS
<<8 | SAFNUM_VPLS
):
1600 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNICAST
):
1601 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNMULTICAST
):
1602 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNIMULTICAST
):
1603 if (tlen
< (int)sizeof(struct in_addr
)) {
1604 printf("invalid len");
1607 TCHECK2(tptr
[0], sizeof(struct in_addr
));
1608 printf("%s", getname(tptr
));
1609 tlen
-= (sizeof(struct in_addr
));
1610 tptr
+= (sizeof(struct in_addr
));
1613 case (AFNUM_NSAP
<<8 | SAFNUM_UNICAST
):
1614 case (AFNUM_NSAP
<<8 | SAFNUM_MULTICAST
):
1615 case (AFNUM_NSAP
<<8 | SAFNUM_UNIMULTICAST
):
1616 TCHECK2(tptr
[0], tlen
);
1617 printf("%s",isonsap_string(tptr
,tlen
));
1622 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNICAST
):
1623 case (AFNUM_NSAP
<<8 | SAFNUM_VPNMULTICAST
):
1624 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNIMULTICAST
):
1625 if (tlen
< BGP_VPN_RD_LEN
+1) {
1626 printf("invalid len");
1629 TCHECK2(tptr
[0], tlen
);
1630 printf("RD: %s, %s",
1631 bgp_vpn_rd_print(tptr
),
1632 isonsap_string(tptr
+BGP_VPN_RD_LEN
,tlen
-BGP_VPN_RD_LEN
));
1633 /* rfc986 mapped IPv4 address ? */
1634 if (EXTRACT_32BITS(tptr
+BGP_VPN_RD_LEN
) == 0x47000601)
1635 printf(" = %s", getname(tptr
+BGP_VPN_RD_LEN
+4));
1637 /* rfc1888 mapped IPv6 address ? */
1638 else if (EXTRACT_24BITS(tptr
+BGP_VPN_RD_LEN
) == 0x350000)
1639 printf(" = %s", getname6(tptr
+BGP_VPN_RD_LEN
+3));
1646 TCHECK2(tptr
[0], tlen
);
1647 printf("no AFI %u/SAFI %u decoder",af
,safi
);
1649 print_unknown_data(gndo
,tptr
,"\n\t ",tlen
);
1657 printf(", nh-length: %u", nhlen
);
1665 printf("\n\t %u SNPA", snpa
);
1666 for (/*nothing*/; snpa
> 0; snpa
--) {
1668 printf("\n\t %d bytes", tptr
[0]);
1669 tptr
+= tptr
[0] + 1;
1672 printf(", no SNPA");
1675 while (len
- (tptr
- pptr
) > 0) {
1676 switch (af
<<8 | safi
) {
1677 case (AFNUM_INET
<<8 | SAFNUM_UNICAST
):
1678 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST
):
1679 case (AFNUM_INET
<<8 | SAFNUM_UNIMULTICAST
):
1680 advance
= decode_prefix4(tptr
, len
, buf
, sizeof(buf
));
1682 printf("\n\t (illegal prefix length)");
1683 else if (advance
== -2)
1685 else if (advance
== -3)
1686 break; /* bytes left, but not enough */
1688 printf("\n\t %s", buf
);
1690 case (AFNUM_INET
<<8 | SAFNUM_LABUNICAST
):
1691 advance
= decode_labeled_prefix4(tptr
, len
, buf
, sizeof(buf
));
1693 printf("\n\t (illegal prefix length)");
1694 else if (advance
== -2)
1696 else if (advance
== -3)
1697 break; /* bytes left, but not enough */
1699 printf("\n\t %s", buf
);
1701 case (AFNUM_INET
<<8 | SAFNUM_VPNUNICAST
):
1702 case (AFNUM_INET
<<8 | SAFNUM_VPNMULTICAST
):
1703 case (AFNUM_INET
<<8 | SAFNUM_VPNUNIMULTICAST
):
1704 advance
= decode_labeled_vpn_prefix4(tptr
, buf
, sizeof(buf
));
1706 printf("\n\t (illegal prefix length)");
1707 else if (advance
== -2)
1710 printf("\n\t %s", buf
);
1712 case (AFNUM_INET
<<8 | SAFNUM_RT_ROUTING_INFO
):
1713 advance
= decode_rt_routing_info(tptr
, buf
, sizeof(buf
));
1715 printf("\n\t (illegal prefix length)");
1716 else if (advance
== -2)
1719 printf("\n\t %s", buf
);
1721 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST_VPN
): /* fall through */
1722 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST_VPN
):
1723 advance
= decode_multicast_vpn(tptr
, buf
, sizeof(buf
));
1725 printf("\n\t (illegal prefix length)");
1726 else if (advance
== -2)
1729 printf("\n\t %s", buf
);
1732 case (AFNUM_INET
<<8 | SAFNUM_MDT
):
1733 advance
= decode_mdt_vpn_nlri(tptr
, buf
, sizeof(buf
));
1735 printf("\n\t (illegal prefix length)");
1736 else if (advance
== -2)
1739 printf("\n\t %s", buf
);
1742 case (AFNUM_INET6
<<8 | SAFNUM_UNICAST
):
1743 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST
):
1744 case (AFNUM_INET6
<<8 | SAFNUM_UNIMULTICAST
):
1745 advance
= decode_prefix6(tptr
, len
, buf
, sizeof(buf
));
1747 printf("\n\t (illegal prefix length)");
1748 else if (advance
== -2)
1750 else if (advance
== -3)
1751 break; /* bytes left, but not enough */
1753 printf("\n\t %s", buf
);
1755 case (AFNUM_INET6
<<8 | SAFNUM_LABUNICAST
):
1756 advance
= decode_labeled_prefix6(tptr
, len
, buf
, sizeof(buf
));
1758 printf("\n\t (illegal prefix length)");
1759 else if (advance
== -2)
1761 else if (advance
== -3)
1762 break; /* bytes left, but not enough */
1764 printf("\n\t %s", buf
);
1766 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNICAST
):
1767 case (AFNUM_INET6
<<8 | SAFNUM_VPNMULTICAST
):
1768 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNIMULTICAST
):
1769 advance
= decode_labeled_vpn_prefix6(tptr
, buf
, sizeof(buf
));
1771 printf("\n\t (illegal prefix length)");
1772 else if (advance
== -2)
1775 printf("\n\t %s", buf
);
1778 case (AFNUM_VPLS
<<8 | SAFNUM_VPLS
):
1779 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNICAST
):
1780 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNMULTICAST
):
1781 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNIMULTICAST
):
1782 advance
= decode_labeled_vpn_l2(tptr
, buf
, sizeof(buf
));
1784 printf("\n\t (illegal length)");
1785 else if (advance
== -2)
1788 printf("\n\t %s", buf
);
1790 case (AFNUM_NSAP
<<8 | SAFNUM_UNICAST
):
1791 case (AFNUM_NSAP
<<8 | SAFNUM_MULTICAST
):
1792 case (AFNUM_NSAP
<<8 | SAFNUM_UNIMULTICAST
):
1793 advance
= decode_clnp_prefix(tptr
, buf
, sizeof(buf
));
1795 printf("\n\t (illegal prefix length)");
1796 else if (advance
== -2)
1799 printf("\n\t %s", buf
);
1801 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNICAST
):
1802 case (AFNUM_NSAP
<<8 | SAFNUM_VPNMULTICAST
):
1803 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNIMULTICAST
):
1804 advance
= decode_labeled_vpn_clnp_prefix(tptr
, buf
, sizeof(buf
));
1806 printf("\n\t (illegal prefix length)");
1807 else if (advance
== -2)
1810 printf("\n\t %s", buf
);
1813 TCHECK2(*tptr
,tlen
);
1814 printf("\n\t no AFI %u / SAFI %u decoder",af
,safi
);
1816 print_unknown_data(gndo
,tptr
,"\n\t ",tlen
);
1828 case BGPTYPE_MP_UNREACH_NLRI
:
1829 TCHECK2(tptr
[0], BGP_MP_NLRI_MINSIZE
);
1830 af
= EXTRACT_16BITS(tptr
);
1833 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
1834 tok2strbuf(af_values
, "Unknown AFI", af
,
1835 tokbuf
, sizeof(tokbuf
)),
1837 (safi
>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1838 tok2strbuf(bgp_safi_values
, "Unknown SAFI", safi
,
1839 tokbuf
, sizeof(tokbuf
)),
1842 if (len
== BGP_MP_NLRI_MINSIZE
)
1843 printf("\n\t End-of-Rib Marker (empty NLRI)");
1847 while (len
- (tptr
- pptr
) > 0) {
1848 switch (af
<<8 | safi
) {
1849 case (AFNUM_INET
<<8 | SAFNUM_UNICAST
):
1850 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST
):
1851 case (AFNUM_INET
<<8 | SAFNUM_UNIMULTICAST
):
1852 advance
= decode_prefix4(tptr
, len
, buf
, sizeof(buf
));
1854 printf("\n\t (illegal prefix length)");
1855 else if (advance
== -2)
1857 else if (advance
== -3)
1858 break; /* bytes left, but not enough */
1860 printf("\n\t %s", buf
);
1862 case (AFNUM_INET
<<8 | SAFNUM_LABUNICAST
):
1863 advance
= decode_labeled_prefix4(tptr
, len
, buf
, sizeof(buf
));
1865 printf("\n\t (illegal prefix length)");
1866 else if (advance
== -2)
1868 else if (advance
== -3)
1869 break; /* bytes left, but not enough */
1871 printf("\n\t %s", buf
);
1873 case (AFNUM_INET
<<8 | SAFNUM_VPNUNICAST
):
1874 case (AFNUM_INET
<<8 | SAFNUM_VPNMULTICAST
):
1875 case (AFNUM_INET
<<8 | SAFNUM_VPNUNIMULTICAST
):
1876 advance
= decode_labeled_vpn_prefix4(tptr
, buf
, sizeof(buf
));
1878 printf("\n\t (illegal prefix length)");
1879 else if (advance
== -2)
1882 printf("\n\t %s", buf
);
1885 case (AFNUM_INET6
<<8 | SAFNUM_UNICAST
):
1886 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST
):
1887 case (AFNUM_INET6
<<8 | SAFNUM_UNIMULTICAST
):
1888 advance
= decode_prefix6(tptr
, len
, buf
, sizeof(buf
));
1890 printf("\n\t (illegal prefix length)");
1891 else if (advance
== -2)
1893 else if (advance
== -3)
1894 break; /* bytes left, but not enough */
1896 printf("\n\t %s", buf
);
1898 case (AFNUM_INET6
<<8 | SAFNUM_LABUNICAST
):
1899 advance
= decode_labeled_prefix6(tptr
, len
, buf
, sizeof(buf
));
1901 printf("\n\t (illegal prefix length)");
1902 else if (advance
== -2)
1904 else if (advance
== -3)
1905 break; /* bytes left, but not enough */
1907 printf("\n\t %s", buf
);
1909 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNICAST
):
1910 case (AFNUM_INET6
<<8 | SAFNUM_VPNMULTICAST
):
1911 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNIMULTICAST
):
1912 advance
= decode_labeled_vpn_prefix6(tptr
, buf
, sizeof(buf
));
1914 printf("\n\t (illegal prefix length)");
1915 else if (advance
== -2)
1918 printf("\n\t %s", buf
);
1921 case (AFNUM_VPLS
<<8 | SAFNUM_VPLS
):
1922 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNICAST
):
1923 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNMULTICAST
):
1924 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNIMULTICAST
):
1925 advance
= decode_labeled_vpn_l2(tptr
, buf
, sizeof(buf
));
1927 printf("\n\t (illegal length)");
1928 else if (advance
== -2)
1931 printf("\n\t %s", buf
);
1933 case (AFNUM_NSAP
<<8 | SAFNUM_UNICAST
):
1934 case (AFNUM_NSAP
<<8 | SAFNUM_MULTICAST
):
1935 case (AFNUM_NSAP
<<8 | SAFNUM_UNIMULTICAST
):
1936 advance
= decode_clnp_prefix(tptr
, buf
, sizeof(buf
));
1938 printf("\n\t (illegal prefix length)");
1939 else if (advance
== -2)
1942 printf("\n\t %s", buf
);
1944 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNICAST
):
1945 case (AFNUM_NSAP
<<8 | SAFNUM_VPNMULTICAST
):
1946 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNIMULTICAST
):
1947 advance
= decode_labeled_vpn_clnp_prefix(tptr
, buf
, sizeof(buf
));
1949 printf("\n\t (illegal prefix length)");
1950 else if (advance
== -2)
1953 printf("\n\t %s", buf
);
1955 case (AFNUM_INET
<<8 | SAFNUM_MDT
):
1956 advance
= decode_mdt_vpn_nlri(tptr
, buf
, sizeof(buf
));
1958 printf("\n\t (illegal prefix length)");
1959 else if (advance
== -2)
1962 printf("\n\t %s", buf
);
1964 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST_VPN
): /* fall through */
1965 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST_VPN
):
1966 advance
= decode_multicast_vpn(tptr
, buf
, sizeof(buf
));
1968 printf("\n\t (illegal prefix length)");
1969 else if (advance
== -2)
1972 printf("\n\t %s", buf
);
1975 TCHECK2(*(tptr
-3),tlen
);
1976 printf("no AFI %u / SAFI %u decoder",af
,safi
);
1978 print_unknown_data(gndo
,tptr
-3,"\n\t ",tlen
);
1988 case BGPTYPE_EXTD_COMMUNITIES
:
1990 printf("invalid len");
1994 u_int16_t extd_comm
;
1996 TCHECK2(tptr
[0], 2);
1997 extd_comm
=EXTRACT_16BITS(tptr
);
1999 printf("\n\t %s (0x%04x), Flags [%s]",
2000 tok2strbuf(bgp_extd_comm_subtype_values
,
2001 "unknown extd community typecode",
2002 extd_comm
, tokbuf
, sizeof(tokbuf
)),
2004 bittok2str(bgp_extd_comm_flag_values
, "none", extd_comm
));
2006 TCHECK2(*(tptr
+2), 6);
2008 case BGP_EXT_COM_RT_0
:
2009 case BGP_EXT_COM_RO_0
:
2010 case BGP_EXT_COM_L2VPN_RT_0
:
2011 printf(": %u:%u (= %s)",
2012 EXTRACT_16BITS(tptr
+2),
2013 EXTRACT_32BITS(tptr
+4),
2016 case BGP_EXT_COM_RT_1
:
2017 case BGP_EXT_COM_RO_1
:
2018 case BGP_EXT_COM_L2VPN_RT_1
:
2019 case BGP_EXT_COM_VRF_RT_IMP
:
2022 EXTRACT_16BITS(tptr
+6));
2024 case BGP_EXT_COM_RT_2
:
2025 case BGP_EXT_COM_RO_2
:
2027 as_printf(astostr
, sizeof(astostr
),
2028 EXTRACT_32BITS(tptr
+2)), EXTRACT_16BITS(tptr
+6));
2030 case BGP_EXT_COM_LINKBAND
:
2031 bw
.i
= EXTRACT_32BITS(tptr
+2);
2032 printf(": bandwidth: %.3f Mbps",
2035 case BGP_EXT_COM_VPN_ORIGIN
:
2036 case BGP_EXT_COM_VPN_ORIGIN2
:
2037 case BGP_EXT_COM_VPN_ORIGIN3
:
2038 case BGP_EXT_COM_VPN_ORIGIN4
:
2039 case BGP_EXT_COM_OSPF_RID
:
2040 case BGP_EXT_COM_OSPF_RID2
:
2041 printf("%s", getname(tptr
+2));
2043 case BGP_EXT_COM_OSPF_RTYPE
:
2044 case BGP_EXT_COM_OSPF_RTYPE2
:
2045 printf(": area:%s, router-type:%s, metric-type:%s%s",
2047 tok2strbuf(bgp_extd_comm_ospf_rtype_values
,
2050 tokbuf
, sizeof(tokbuf
)),
2051 (*(tptr
+7) & BGP_OSPF_RTYPE_METRIC_TYPE
) ? "E2" : "",
2052 ((*(tptr
+6) == BGP_OSPF_RTYPE_EXT
) || (*(tptr
+6) == BGP_OSPF_RTYPE_NSSA
)) ? "E1" : "");
2054 case BGP_EXT_COM_L2INFO
:
2055 printf(": %s Control Flags [0x%02x]:MTU %u",
2056 tok2strbuf(l2vpn_encaps_values
,
2059 tokbuf
, sizeof(tokbuf
)),
2061 EXTRACT_16BITS(tptr
+4));
2063 case BGP_EXT_COM_SOURCE_AS
:
2064 printf(": AS %u", EXTRACT_16BITS(tptr
+2));
2068 print_unknown_data(gndo
,tptr
,"\n\t ",8);
2076 case BGPTYPE_PMSI_TUNNEL
:
2078 u_int8_t tunnel_type
, flags
;
2080 tunnel_type
= *(tptr
+1);
2084 TCHECK2(tptr
[0], 5);
2085 printf("\n\t Tunnel-type %s (%u), Flags [%s], MPLS Label %u",
2086 tok2str(bgp_pmsi_tunnel_values
, "Unknown", tunnel_type
),
2088 bittok2str(bgp_pmsi_flag_values
, "none", flags
),
2089 EXTRACT_24BITS(tptr
+2)>>4);
2094 switch (tunnel_type
) {
2095 case BGP_PMSI_TUNNEL_PIM_SM
: /* fall through */
2096 case BGP_PMSI_TUNNEL_PIM_BIDIR
:
2097 TCHECK2(tptr
[0], 8);
2098 printf("\n\t Sender %s, P-Group %s",
2099 ipaddr_string(tptr
),
2100 ipaddr_string(tptr
+4));
2103 case BGP_PMSI_TUNNEL_PIM_SSM
:
2104 TCHECK2(tptr
[0], 8);
2105 printf("\n\t Root-Node %s, P-Group %s",
2106 ipaddr_string(tptr
),
2107 ipaddr_string(tptr
+4));
2109 case BGP_PMSI_TUNNEL_INGRESS
:
2110 TCHECK2(tptr
[0], 4);
2111 printf("\n\t Tunnel-Endpoint %s",
2112 ipaddr_string(tptr
));
2114 case BGP_PMSI_TUNNEL_LDP_P2MP
: /* fall through */
2115 case BGP_PMSI_TUNNEL_LDP_MP2MP
:
2116 TCHECK2(tptr
[0], 8);
2117 printf("\n\t Root-Node %s, LSP-ID 0x%08x",
2118 ipaddr_string(tptr
),
2119 EXTRACT_32BITS(tptr
+4));
2121 case BGP_PMSI_TUNNEL_RSVP_P2MP
:
2122 TCHECK2(tptr
[0], 8);
2123 printf("\n\t Extended-Tunnel-ID %s, P2MP-ID 0x%08x",
2124 ipaddr_string(tptr
),
2125 EXTRACT_32BITS(tptr
+4));
2129 print_unknown_data(gndo
,tptr
,"\n\t ",tlen
);
2134 case BGPTYPE_ATTR_SET
:
2135 TCHECK2(tptr
[0], 4);
2138 printf("\n\t Origin AS: %s",
2139 as_printf(astostr
, sizeof(astostr
), EXTRACT_32BITS(tptr
)));
2144 u_int aflags
, atype
, alenlen
, alen
;
2146 TCHECK2(tptr
[0], 2);
2150 atype
= *(tptr
+ 1);
2153 alenlen
= bgp_attr_lenlen(aflags
, tptr
);
2154 TCHECK2(tptr
[0], alenlen
);
2157 alen
= bgp_attr_len(aflags
, tptr
);
2161 printf("\n\t %s (%u), length: %u",
2162 tok2strbuf(bgp_attr_values
,
2163 "Unknown Attribute", atype
,
2164 tokbuf
, sizeof(tokbuf
)),
2169 printf(", Flags [%s%s%s%s",
2170 aflags
& 0x80 ? "O" : "",
2171 aflags
& 0x40 ? "T" : "",
2172 aflags
& 0x20 ? "P" : "",
2173 aflags
& 0x10 ? "E" : "");
2175 printf("+%x", aflags
& 0xf);
2178 /* FIXME check for recursion */
2179 if (!bgp_attr_print(atype
, tptr
, alen
))
2189 printf("\n\t no Attribute %u decoder",atype
); /* we have no decoder for the attribute */
2191 print_unknown_data(gndo
,pptr
,"\n\t ",len
);
2194 if (vflag
> 1 && len
) { /* omit zero length attributes*/
2196 print_unknown_data(gndo
,pptr
,"\n\t ",len
);
2205 bgp_capabilities_print(const u_char
*opt
, int caps_len
)
2207 char tokbuf
[TOKBUFSIZE
];
2208 char tokbuf2
[TOKBUFSIZE
];
2209 int cap_type
, cap_len
, tcap_len
, cap_offset
;
2212 while (i
< caps_len
) {
2213 TCHECK2(opt
[i
], BGP_CAP_HEADER_SIZE
);
2217 printf("\n\t %s (%u), length: %u",
2218 tok2strbuf(bgp_capcode_values
, "Unknown",
2219 cap_type
, tokbuf
, sizeof(tokbuf
)),
2222 TCHECK2(opt
[i
+2], cap_len
);
2224 case BGP_CAPCODE_MP
:
2225 printf("\n\t\tAFI %s (%u), SAFI %s (%u)",
2226 tok2strbuf(af_values
, "Unknown",
2227 EXTRACT_16BITS(opt
+i
+2),
2228 tokbuf
, sizeof(tokbuf
)),
2229 EXTRACT_16BITS(opt
+i
+2),
2230 tok2strbuf(bgp_safi_values
, "Unknown",
2232 tokbuf
, sizeof(tokbuf
)),
2235 case BGP_CAPCODE_RESTART
:
2236 printf("\n\t\tRestart Flags: [%s], Restart Time %us",
2237 ((opt
[i
+2])&0x80) ? "R" : "none",
2238 EXTRACT_16BITS(opt
+i
+2)&0xfff);
2241 while(tcap_len
>=4) {
2242 printf("\n\t\t AFI %s (%u), SAFI %s (%u), Forwarding state preserved: %s",
2243 tok2strbuf(af_values
,"Unknown",
2244 EXTRACT_16BITS(opt
+i
+cap_offset
),
2245 tokbuf
, sizeof(tokbuf
)),
2246 EXTRACT_16BITS(opt
+i
+cap_offset
),
2247 tok2strbuf(bgp_safi_values
,"Unknown",
2248 opt
[i
+cap_offset
+2],
2249 tokbuf2
, sizeof(tokbuf2
)),
2250 opt
[i
+cap_offset
+2],
2251 ((opt
[i
+cap_offset
+3])&0x80) ? "yes" : "no" );
2256 case BGP_CAPCODE_RR
:
2257 case BGP_CAPCODE_RR_CISCO
:
2259 case BGP_CAPCODE_AS_NEW
:
2262 * Extract the 4 byte AS number encoded.
2265 printf("\n\t\t 4 Byte AS %s",
2266 as_printf(astostr
, sizeof(astostr
),
2267 EXTRACT_32BITS(opt
+ i
+ 2)));
2271 printf("\n\t\tno decoder for Capability %u",
2274 print_unknown_data(gndo
,&opt
[i
+2],"\n\t\t",cap_len
);
2277 if (vflag
> 1 && cap_len
> 0) {
2278 print_unknown_data(gndo
,&opt
[i
+2],"\n\t\t",cap_len
);
2280 i
+= BGP_CAP_HEADER_SIZE
+ cap_len
;
2289 bgp_open_print(const u_char
*dat
, int length
)
2291 struct bgp_open bgpo
;
2292 struct bgp_opt bgpopt
;
2295 char tokbuf
[TOKBUFSIZE
];
2297 TCHECK2(dat
[0], BGP_OPEN_SIZE
);
2298 memcpy(&bgpo
, dat
, BGP_OPEN_SIZE
);
2300 printf("\n\t Version %d, ", bgpo
.bgpo_version
);
2301 printf("my AS %s, ",
2302 as_printf(astostr
, sizeof(astostr
), ntohs(bgpo
.bgpo_myas
)));
2303 printf("Holdtime %us, ", ntohs(bgpo
.bgpo_holdtime
));
2304 printf("ID %s", getname((u_char
*)&bgpo
.bgpo_id
));
2305 printf("\n\t Optional parameters, length: %u", bgpo
.bgpo_optlen
);
2307 /* some little sanity checking */
2308 if (length
< bgpo
.bgpo_optlen
+BGP_OPEN_SIZE
)
2312 opt
= &((const struct bgp_open
*)dat
)->bgpo_optlen
;
2316 while (i
< bgpo
.bgpo_optlen
) {
2317 TCHECK2(opt
[i
], BGP_OPT_SIZE
);
2318 memcpy(&bgpopt
, &opt
[i
], BGP_OPT_SIZE
);
2319 if (i
+ 2 + bgpopt
.bgpopt_len
> bgpo
.bgpo_optlen
) {
2320 printf("\n\t Option %d, length: %u", bgpopt
.bgpopt_type
, bgpopt
.bgpopt_len
);
2324 printf("\n\t Option %s (%u), length: %u",
2325 tok2strbuf(bgp_opt_values
,"Unknown",
2327 tokbuf
, sizeof(tokbuf
)),
2331 /* now let's decode the options we know*/
2332 switch(bgpopt
.bgpopt_type
) {
2335 bgp_capabilities_print(&opt
[i
+BGP_OPT_SIZE
],
2341 printf("\n\t no decoder for option %u",
2342 bgpopt
.bgpopt_type
);
2345 i
+= BGP_OPT_SIZE
+ bgpopt
.bgpopt_len
;
2353 bgp_update_print(const u_char
*dat
, int length
)
2357 int withdrawn_routes_len
;
2360 char tokbuf
[TOKBUFSIZE
];
2362 char buf
[MAXHOSTNAMELEN
+ 100];
2366 TCHECK2(dat
[0], BGP_SIZE
);
2367 if (length
< BGP_SIZE
)
2369 memcpy(&bgp
, dat
, BGP_SIZE
);
2370 p
= dat
+ BGP_SIZE
; /*XXX*/
2373 /* Unfeasible routes */
2377 withdrawn_routes_len
= EXTRACT_16BITS(p
);
2380 if (withdrawn_routes_len
) {
2382 * Without keeping state from the original NLRI message,
2383 * it's not possible to tell if this a v4 or v6 route,
2384 * so only try to decode it if we're not v6 enabled.
2386 TCHECK2(p
[0], withdrawn_routes_len
);
2387 if (length
< withdrawn_routes_len
)
2390 printf("\n\t Withdrawn routes: %d bytes", withdrawn_routes_len
);
2391 p
+= withdrawn_routes_len
;
2392 length
-= withdrawn_routes_len
;
2394 if (withdrawn_routes_len
< 2)
2397 withdrawn_routes_len
-= 2;
2400 printf("\n\t Withdrawn routes:");
2402 while(withdrawn_routes_len
> 0) {
2403 wpfx
= decode_prefix4(p
, withdrawn_routes_len
, buf
, sizeof(buf
));
2405 printf("\n\t (illegal prefix length)");
2407 } else if (wpfx
== -2)
2409 else if (wpfx
== -3)
2410 goto trunc
; /* bytes left, but not enough */
2412 printf("\n\t %s", buf
);
2415 withdrawn_routes_len
-= wpfx
;
2424 len
= EXTRACT_16BITS(p
);
2428 if (withdrawn_routes_len
== 0 && len
== 0 && length
== 0) {
2429 /* No withdrawn routes, no path attributes, no NLRI */
2430 printf("\n\t End-of-Rib Marker (empty NLRI)");
2435 /* do something more useful!*/
2437 int aflags
, atype
, alenlen
, alen
;
2449 alenlen
= bgp_attr_lenlen(aflags
, p
);
2450 TCHECK2(p
[0], alenlen
);
2453 if (length
< alenlen
)
2455 alen
= bgp_attr_len(aflags
, p
);
2460 printf("\n\t %s (%u), length: %u",
2461 tok2strbuf(bgp_attr_values
, "Unknown Attribute",
2463 tokbuf
, sizeof(tokbuf
)),
2468 printf(", Flags [%s%s%s%s",
2469 aflags
& 0x80 ? "O" : "",
2470 aflags
& 0x40 ? "T" : "",
2471 aflags
& 0x20 ? "P" : "",
2472 aflags
& 0x10 ? "E" : "");
2474 printf("+%x", aflags
& 0xf);
2481 if (!bgp_attr_print(atype
, p
, alen
))
2491 * XXX - what if they're using the "Advertisement of
2492 * Multiple Paths in BGP" feature:
2494 * https://datatracker.ietf.org/doc/draft-ietf-idr-add-paths/
2496 * http://tools.ietf.org/html/draft-ietf-idr-add-paths-06
2498 printf("\n\t Updated routes:");
2500 char buf
[MAXHOSTNAMELEN
+ 100];
2501 i
= decode_prefix4(p
, length
, buf
, sizeof(buf
));
2503 printf("\n\t (illegal prefix length)");
2508 goto trunc
; /* bytes left, but not enough */
2510 printf("\n\t %s", buf
);
2522 bgp_notification_print(const u_char
*dat
, int length
)
2524 struct bgp_notification bgpn
;
2526 char tokbuf
[TOKBUFSIZE
];
2527 char tokbuf2
[TOKBUFSIZE
];
2529 TCHECK2(dat
[0], BGP_NOTIFICATION_SIZE
);
2530 memcpy(&bgpn
, dat
, BGP_NOTIFICATION_SIZE
);
2532 /* some little sanity checking */
2533 if (length
<BGP_NOTIFICATION_SIZE
)
2537 tok2strbuf(bgp_notify_major_values
, "Unknown Error",
2538 bgpn
.bgpn_major
, tokbuf
, sizeof(tokbuf
)),
2541 switch (bgpn
.bgpn_major
) {
2543 case BGP_NOTIFY_MAJOR_MSG
:
2544 printf(", subcode %s (%u)",
2545 tok2strbuf(bgp_notify_minor_msg_values
, "Unknown",
2546 bgpn
.bgpn_minor
, tokbuf
, sizeof(tokbuf
)),
2549 case BGP_NOTIFY_MAJOR_OPEN
:
2550 printf(", subcode %s (%u)",
2551 tok2strbuf(bgp_notify_minor_open_values
, "Unknown",
2552 bgpn
.bgpn_minor
, tokbuf
, sizeof(tokbuf
)),
2555 case BGP_NOTIFY_MAJOR_UPDATE
:
2556 printf(", subcode %s (%u)",
2557 tok2strbuf(bgp_notify_minor_update_values
, "Unknown",
2558 bgpn
.bgpn_minor
, tokbuf
, sizeof(tokbuf
)),
2561 case BGP_NOTIFY_MAJOR_CAP
:
2562 printf(" subcode %s (%u)",
2563 tok2strbuf(bgp_notify_minor_cap_values
, "Unknown",
2564 bgpn
.bgpn_minor
, tokbuf
, sizeof(tokbuf
)),
2566 case BGP_NOTIFY_MAJOR_CEASE
:
2567 printf(", subcode %s (%u)",
2568 tok2strbuf(bgp_notify_minor_cease_values
, "Unknown",
2569 bgpn
.bgpn_minor
, tokbuf
, sizeof(tokbuf
)),
2572 /* draft-ietf-idr-cease-subcode-02 mentions optionally 7 bytes
2573 * for the maxprefix subtype, which may contain AFI, SAFI and MAXPREFIXES
2575 if(bgpn
.bgpn_minor
== BGP_NOTIFY_MINOR_CEASE_MAXPRFX
&& length
>= BGP_NOTIFICATION_SIZE
+ 7) {
2576 tptr
= dat
+ BGP_NOTIFICATION_SIZE
;
2578 printf(", AFI %s (%u), SAFI %s (%u), Max Prefixes: %u",
2579 tok2strbuf(af_values
, "Unknown",
2580 EXTRACT_16BITS(tptr
), tokbuf
, sizeof(tokbuf
)),
2581 EXTRACT_16BITS(tptr
),
2582 tok2strbuf(bgp_safi_values
, "Unknown", *(tptr
+2),
2583 tokbuf2
, sizeof(tokbuf
)),
2585 EXTRACT_32BITS(tptr
+3));
2598 bgp_route_refresh_print(const u_char
*pptr
, int len
) {
2600 const struct bgp_route_refresh
*bgp_route_refresh_header
;
2601 char tokbuf
[TOKBUFSIZE
];
2602 char tokbuf2
[TOKBUFSIZE
];
2604 TCHECK2(pptr
[0], BGP_ROUTE_REFRESH_SIZE
);
2606 /* some little sanity checking */
2607 if (len
<BGP_ROUTE_REFRESH_SIZE
)
2610 bgp_route_refresh_header
= (const struct bgp_route_refresh
*)pptr
;
2612 printf("\n\t AFI %s (%u), SAFI %s (%u)",
2613 tok2strbuf(af_values
,"Unknown",
2614 /* this stinks but the compiler pads the structure
2616 EXTRACT_16BITS(&bgp_route_refresh_header
->afi
),
2617 tokbuf
, sizeof(tokbuf
)),
2618 EXTRACT_16BITS(&bgp_route_refresh_header
->afi
),
2619 tok2strbuf(bgp_safi_values
,"Unknown",
2620 bgp_route_refresh_header
->safi
,
2621 tokbuf2
, sizeof(tokbuf2
)),
2622 bgp_route_refresh_header
->safi
);
2625 TCHECK2(*pptr
, len
);
2626 print_unknown_data(gndo
,pptr
,"\n\t ", len
);
2635 bgp_header_print(const u_char
*dat
, int length
)
2638 char tokbuf
[TOKBUFSIZE
];
2640 TCHECK2(dat
[0], BGP_SIZE
);
2641 memcpy(&bgp
, dat
, BGP_SIZE
);
2642 printf("\n\t%s Message (%u), length: %u",
2643 tok2strbuf(bgp_msg_values
, "Unknown", bgp
.bgp_type
,
2644 tokbuf
, sizeof(tokbuf
)),
2648 switch (bgp
.bgp_type
) {
2650 bgp_open_print(dat
, length
);
2653 bgp_update_print(dat
, length
);
2655 case BGP_NOTIFICATION
:
2656 bgp_notification_print(dat
, length
);
2660 case BGP_ROUTE_REFRESH
:
2661 bgp_route_refresh_print(dat
, length
);
2664 /* we have no decoder for the BGP message */
2665 TCHECK2(*dat
, length
);
2666 printf("\n\t no Message %u decoder",bgp
.bgp_type
);
2667 print_unknown_data(gndo
,dat
,"\n\t ",length
);
2677 bgp_print(const u_char
*dat
, int length
)
2681 const u_char
*start
;
2682 const u_char marker
[] = {
2683 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2684 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2688 char tokbuf
[TOKBUFSIZE
];
2691 if (snapend
< dat
+ length
)
2694 printf(": BGP, length: %u",length
);
2696 if (vflag
< 1) /* lets be less chatty */
2702 if (!TTEST2(p
[0], 1))
2709 if (!TTEST2(p
[0], sizeof(marker
)))
2711 if (memcmp(p
, marker
, sizeof(marker
)) != 0) {
2716 /* found BGP header */
2717 TCHECK2(p
[0], BGP_SIZE
); /*XXX*/
2718 memcpy(&bgp
, p
, BGP_SIZE
);
2723 hlen
= ntohs(bgp
.bgp_len
);
2724 if (hlen
< BGP_SIZE
) {
2725 printf("\n[|BGP Bogus header length %u < %u]", hlen
,
2730 if (TTEST2(p
[0], hlen
)) {
2731 if (!bgp_header_print(p
, hlen
))
2736 printf("\n[|BGP %s]",
2737 tok2strbuf(bgp_msg_values
,
2738 "Unknown Message Type",
2740 tokbuf
, sizeof(tokbuf
)));
2753 * c-style: whitesmith