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@gredler.at) for more
30 * complete BGP support.
33 /* \summary: Border Gateway Protocol (BGP) printer */
35 /* specification: RFC 4271 */
41 #include "netdissect-stdinc.h"
46 #include "netdissect.h"
47 #include "addrtoname.h"
53 nd_byte bgp_marker
[16];
57 #define BGP_SIZE 19 /* unaligned */
61 #define BGP_NOTIFICATION 3
62 #define BGP_KEEPALIVE 4
63 #define BGP_ROUTE_REFRESH 5
65 static const struct tok bgp_msg_values
[] = {
67 { BGP_UPDATE
, "Update"},
68 { BGP_NOTIFICATION
, "Notification"},
69 { BGP_KEEPALIVE
, "Keepalive"},
70 { BGP_ROUTE_REFRESH
, "Route Refresh"},
75 nd_byte bgpo_marker
[16];
78 nd_uint8_t bgpo_version
;
79 nd_uint16_t bgpo_myas
;
80 nd_uint16_t bgpo_holdtime
;
82 nd_uint8_t bgpo_optlen
;
83 nd_uint8_t bgpo_opttype
; /* RFC9072 */
84 nd_uint16_t bgpo_optlen_extended
; /* RFC9072 */
85 /* options should follow */
87 #define BGP_OPEN_SIZE 29 /* unaligned */
90 nd_uint8_t bgpopt_type
;
91 nd_uint16_t bgpopt_len
; /* Can be one or two bytes, depending on RFC9072 */
94 #define BGP_OPT_SIZE 2 /* some compilers may pad to 4 bytes */
95 #define BGP_CAP_HEADER_SIZE 2 /* some compilers may pad to 4 bytes */
97 struct bgp_notification
{
98 nd_byte bgpn_marker
[16];
100 nd_uint8_t bgpn_type
;
101 nd_uint8_t bgpn_major
;
102 nd_uint8_t bgpn_minor
;
104 #define BGP_NOTIFICATION_SIZE 21 /* unaligned */
106 struct bgp_route_refresh
{
107 nd_byte bgp_marker
[16];
109 nd_uint8_t type
; /* No padding after this; afi is, in fact, not aligned */
114 #define BGP_ROUTE_REFRESH_SIZE 23
115 #define BGP_ROUTE_REFRESH_SUBTYPE_NORMAL 0
116 #define BGP_ROUTE_REFRESH_SUBTYPE_BORR 1
117 #define BGP_ROUTE_REFRESH_SUBTYPE_EORR 2
118 #define BGP_ROUTE_REFRESH_SUBTYPE_RESERVED 255
120 static const struct tok bgp_route_refresh_subtype_values
[] = {
121 {BGP_ROUTE_REFRESH_SUBTYPE_NORMAL
, "Normal route refresh request"},
122 {BGP_ROUTE_REFRESH_SUBTYPE_BORR
,
123 "Demarcation of the beginning of a route refresh"},
124 {BGP_ROUTE_REFRESH_SUBTYPE_EORR
,
125 "Demarcation of the ending of a route refresh"},
128 #define bgp_attr_lenlen(flags, p) \
129 (((flags) & 0x10) ? 2U : 1U)
130 #define bgp_attr_len(flags, p) \
131 (((flags) & 0x10) ? GET_BE_U_2(p) : GET_U_1(p))
133 #define BGPTYPE_ORIGIN 1
134 #define BGPTYPE_AS_PATH 2
135 #define BGPTYPE_NEXT_HOP 3
136 #define BGPTYPE_MULTI_EXIT_DISC 4
137 #define BGPTYPE_LOCAL_PREF 5
138 #define BGPTYPE_ATOMIC_AGGREGATE 6
139 #define BGPTYPE_AGGREGATOR 7
140 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
141 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC4456 */
142 #define BGPTYPE_CLUSTER_LIST 10 /* RFC4456 */
143 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC4760 */
144 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC4760 */
145 #define BGPTYPE_EXTD_COMMUNITIES 16 /* RFC4360 */
146 #define BGPTYPE_AS4_PATH 17 /* RFC6793 */
147 #define BGPTYPE_AGGREGATOR4 18 /* RFC6793 */
148 #define BGPTYPE_PMSI_TUNNEL 22 /* RFC6514 */
149 #define BGPTYPE_TUNNEL_ENCAP 23 /* RFC5512 */
150 #define BGPTYPE_TRAFFIC_ENG 24 /* RFC5543 */
151 #define BGPTYPE_IPV6_EXTD_COMMUNITIES 25 /* RFC5701 */
152 #define BGPTYPE_AIGP 26 /* RFC7311 */
153 #define BGPTYPE_PE_DISTINGUISHER_LABEL 27 /* RFC6514 */
154 #define BGPTYPE_ENTROPY_LABEL 28 /* RFC6790 */
155 #define BGPTYPE_LARGE_COMMUNITY 32 /* draft-ietf-idr-large-community-05 */
156 #define BGPTYPE_ATTR_SET 128 /* RFC6368 */
158 #define BGP_MP_NLRI_MINSIZE 3 /* End of RIB Marker detection */
160 static const struct tok bgp_attr_values
[] = {
161 { BGPTYPE_ORIGIN
, "Origin"},
162 { BGPTYPE_AS_PATH
, "AS Path"},
163 { BGPTYPE_AS4_PATH
, "AS4 Path"},
164 { BGPTYPE_NEXT_HOP
, "Next Hop"},
165 { BGPTYPE_MULTI_EXIT_DISC
, "Multi Exit Discriminator"},
166 { BGPTYPE_LOCAL_PREF
, "Local Preference"},
167 { BGPTYPE_ATOMIC_AGGREGATE
, "Atomic Aggregate"},
168 { BGPTYPE_AGGREGATOR
, "Aggregator"},
169 { BGPTYPE_AGGREGATOR4
, "Aggregator4"},
170 { BGPTYPE_COMMUNITIES
, "Community"},
171 { BGPTYPE_ORIGINATOR_ID
, "Originator ID"},
172 { BGPTYPE_CLUSTER_LIST
, "Cluster List"},
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_PMSI_TUNNEL
, "PMSI Tunnel"},
177 { BGPTYPE_TUNNEL_ENCAP
, "Tunnel Encapsulation"},
178 { BGPTYPE_TRAFFIC_ENG
, "Traffic Engineering"},
179 { BGPTYPE_IPV6_EXTD_COMMUNITIES
, "IPv6 Extended Community"},
180 { BGPTYPE_AIGP
, "Accumulated IGP Metric"},
181 { BGPTYPE_PE_DISTINGUISHER_LABEL
, "PE Distinguisher Label"},
182 { BGPTYPE_ENTROPY_LABEL
, "Entropy Label"},
183 { BGPTYPE_LARGE_COMMUNITY
, "Large Community"},
184 { BGPTYPE_ATTR_SET
, "Attribute Set"},
185 { 255, "Reserved for development"},
190 #define BGP_AS_SEQUENCE 2
191 #define BGP_CONFED_AS_SEQUENCE 3 /* draft-ietf-idr-rfc3065bis-01 */
192 #define BGP_CONFED_AS_SET 4 /* draft-ietf-idr-rfc3065bis-01 */
194 #define BGP_AS_SEG_TYPE_MIN BGP_AS_SET
195 #define BGP_AS_SEG_TYPE_MAX BGP_CONFED_AS_SET
197 static const struct tok bgp_as_path_segment_open_values
[] = {
198 { BGP_AS_SEQUENCE
, ""},
200 { BGP_CONFED_AS_SEQUENCE
, "( "},
201 { BGP_CONFED_AS_SET
, "({ "},
205 static const struct tok bgp_as_path_segment_close_values
[] = {
206 { BGP_AS_SEQUENCE
, ""},
208 { BGP_CONFED_AS_SEQUENCE
, ")"},
209 { BGP_CONFED_AS_SET
, "})"},
213 #define BGP_OPT_AUTH 1
214 #define BGP_OPT_CAP 2
216 static const struct tok bgp_opt_values
[] = {
217 { BGP_OPT_AUTH
, "Authentication Information"},
218 { BGP_OPT_CAP
, "Capabilities Advertisement"},
222 #define BGP_CAPCODE_MP 1 /* RFC2858 */
223 #define BGP_CAPCODE_RR 2 /* RFC2918 */
224 #define BGP_CAPCODE_ORF 3 /* RFC5291 */
225 #define BGP_CAPCODE_MR 4 /* RFC3107 */
226 #define BGP_CAPCODE_EXT_NH 5 /* RFC5549 */
227 #define BGP_CAPCODE_EXT_MSG 6 /* RFC8654 */
228 #define BGP_CAPCODE_ML 8 /* RFC8277 */
229 #define BGP_CAPCODE_RESTART 64 /* RFC4724 */
230 #define BGP_CAPCODE_AS_NEW 65 /* RFC6793 */
231 #define BGP_CAPCODE_DYN_CAP 67 /* draft-ietf-idr-dynamic-cap */
232 #define BGP_CAPCODE_MULTISESS 68 /* draft-ietf-idr-bgp-multisession */
233 #define BGP_CAPCODE_ADD_PATH 69 /* RFC7911 */
234 #define BGP_CAPCODE_ENH_RR 70 /* RFC7313 */
235 #define BGP_CAPCODE_LLGR 71 /* draft-uttaro-idr-bgp-persistence-05 */
236 #define BGP_CAPCODE_RR_CISCO 128
238 static const struct tok bgp_capcode_values
[] = {
239 { BGP_CAPCODE_MP
, "Multiprotocol Extensions"},
240 { BGP_CAPCODE_RR
, "Route Refresh"},
241 { BGP_CAPCODE_ORF
, "Cooperative Route Filtering"},
242 { BGP_CAPCODE_MR
, "Multiple Routes to a Destination"},
243 { BGP_CAPCODE_EXT_NH
, "Extended Next Hop Encoding"},
244 { BGP_CAPCODE_EXT_MSG
, "BGP Extended Message"},
245 { BGP_CAPCODE_ML
, "Multiple Labels"},
246 { BGP_CAPCODE_RESTART
, "Graceful Restart"},
247 { BGP_CAPCODE_AS_NEW
, "32-Bit AS Number"},
248 { BGP_CAPCODE_DYN_CAP
, "Dynamic Capability"},
249 { BGP_CAPCODE_MULTISESS
, "Multisession BGP"},
250 { BGP_CAPCODE_ADD_PATH
, "Multiple Paths"},
251 { BGP_CAPCODE_ENH_RR
, "Enhanced Route Refresh"},
252 { BGP_CAPCODE_LLGR
, "Long-lived Graceful Restart"},
253 { BGP_CAPCODE_RR_CISCO
, "Route Refresh (Cisco)"},
257 #define BGP_NOTIFY_MAJOR_MSG 1
258 #define BGP_NOTIFY_MAJOR_OPEN 2
259 #define BGP_NOTIFY_MAJOR_UPDATE 3
260 #define BGP_NOTIFY_MAJOR_HOLDTIME 4
261 #define BGP_NOTIFY_MAJOR_FSM 5
262 #define BGP_NOTIFY_MAJOR_CEASE 6
263 #define BGP_NOTIFY_MAJOR_CAP 7
265 static const struct tok bgp_notify_major_values
[] = {
266 { BGP_NOTIFY_MAJOR_MSG
, "Message Header Error"},
267 { BGP_NOTIFY_MAJOR_OPEN
, "OPEN Message Error"},
268 { BGP_NOTIFY_MAJOR_UPDATE
, "UPDATE Message Error"},
269 { BGP_NOTIFY_MAJOR_HOLDTIME
,"Hold Timer Expired"},
270 { BGP_NOTIFY_MAJOR_FSM
, "Finite State Machine Error"},
271 { BGP_NOTIFY_MAJOR_CEASE
, "Cease"},
272 { BGP_NOTIFY_MAJOR_CAP
, "Capability Message Error"},
277 #define BGP_NOTIFY_MINOR_CEASE_MAXPRFX 1
279 #define BGP_NOTIFY_MINOR_CEASE_SHUT 2
280 #define BGP_NOTIFY_MINOR_CEASE_RESET 4
282 #define BGP_NOTIFY_MINOR_CEASE_HARDRESET 9
283 static const struct tok bgp_notify_minor_cease_values
[] = {
284 { BGP_NOTIFY_MINOR_CEASE_MAXPRFX
, "Maximum Number of Prefixes Reached"},
285 { BGP_NOTIFY_MINOR_CEASE_SHUT
, "Administrative Shutdown"},
286 { 3, "Peer Unconfigured"},
287 { BGP_NOTIFY_MINOR_CEASE_RESET
, "Administrative Reset"},
288 { 5, "Connection Rejected"},
289 { 6, "Other Configuration Change"},
290 { 7, "Connection Collision Resolution"},
291 { 8, "Out of Resources"},
292 { BGP_NOTIFY_MINOR_CEASE_HARDRESET
, "Hard Reset"},
297 static const struct tok bgp_notify_minor_msg_values
[] = {
298 { 1, "Connection Not Synchronized"},
299 { 2, "Bad Message Length"},
300 { 3, "Bad Message Type"},
304 static const struct tok bgp_notify_minor_open_values
[] = {
305 { 1, "Unsupported Version Number"},
307 { 3, "Bad BGP Identifier"},
308 { 4, "Unsupported Optional Parameter"},
309 { 5, "Authentication Failure"},
310 { 6, "Unacceptable Hold Time"},
311 { 7, "Capability Message Error"},
315 static const struct tok bgp_notify_minor_update_values
[] = {
316 { 1, "Malformed Attribute List"},
317 { 2, "Unrecognized Well-known Attribute"},
318 { 3, "Missing Well-known Attribute"},
319 { 4, "Attribute Flags Error"},
320 { 5, "Attribute Length Error"},
321 { 6, "Invalid ORIGIN Attribute"},
322 { 7, "AS Routing Loop"},
323 { 8, "Invalid NEXT_HOP Attribute"},
324 { 9, "Optional Attribute Error"},
325 { 10, "Invalid Network Field"},
326 { 11, "Malformed AS_PATH"},
330 static const struct tok bgp_notify_minor_fsm_values
[] = {
331 { 0, "Unspecified Error"},
332 { 1, "In OpenSent State"},
333 { 2, "In OpenConfirm State"},
334 { 3, "In Established State"},
338 static const struct tok bgp_notify_minor_cap_values
[] = {
339 { 1, "Invalid Action Value" },
340 { 2, "Invalid Capability Length" },
341 { 3, "Malformed Capability Value" },
342 { 4, "Unsupported Capability Code" },
346 static const struct tok bgp_origin_values
[] = {
353 #define BGP_PMSI_TUNNEL_RSVP_P2MP 1
354 #define BGP_PMSI_TUNNEL_LDP_P2MP 2
355 #define BGP_PMSI_TUNNEL_PIM_SSM 3
356 #define BGP_PMSI_TUNNEL_PIM_SM 4
357 #define BGP_PMSI_TUNNEL_PIM_BIDIR 5
358 #define BGP_PMSI_TUNNEL_INGRESS 6
359 #define BGP_PMSI_TUNNEL_LDP_MP2MP 7
361 static const struct tok bgp_pmsi_tunnel_values
[] = {
362 { BGP_PMSI_TUNNEL_RSVP_P2MP
, "RSVP-TE P2MP LSP"},
363 { BGP_PMSI_TUNNEL_LDP_P2MP
, "LDP P2MP LSP"},
364 { BGP_PMSI_TUNNEL_PIM_SSM
, "PIM-SSM Tree"},
365 { BGP_PMSI_TUNNEL_PIM_SM
, "PIM-SM Tree"},
366 { BGP_PMSI_TUNNEL_PIM_BIDIR
, "PIM-Bidir Tree"},
367 { BGP_PMSI_TUNNEL_INGRESS
, "Ingress Replication"},
368 { BGP_PMSI_TUNNEL_LDP_MP2MP
, "LDP MP2MP LSP"},
372 static const struct tok bgp_pmsi_flag_values
[] = {
373 { 0x01, "Leaf Information required"},
377 #define BGP_AIGP_TLV 1
379 static const struct tok bgp_aigp_values
[] = {
380 { BGP_AIGP_TLV
, "AIGP"},
384 /* Subsequent address family identifier, RFC2283 section 7 */
386 #define SAFNUM_UNICAST 1
387 #define SAFNUM_MULTICAST 2
388 #define SAFNUM_UNIMULTICAST 3 /* deprecated now */
389 /* labeled BGP RFC3107 */
390 #define SAFNUM_LABUNICAST 4
392 #define SAFNUM_MULTICAST_VPN 5
393 /* draft-nalawade-kapoor-tunnel-safi */
394 #define SAFNUM_TUNNEL 64
396 #define SAFNUM_VPLS 65
398 #define SAFNUM_MDT 66
400 #define SAFNUM_EVPN 70
402 #define SAFNUM_VPNUNICAST 128
404 #define SAFNUM_VPNMULTICAST 129
405 #define SAFNUM_VPNUNIMULTICAST 130 /* deprecated now */
407 #define SAFNUM_RT_ROUTING_INFO 132
409 #define BGP_VPN_RD_LEN 8
411 static const struct tok bgp_safi_values
[] = {
412 { SAFNUM_RES
, "Reserved"},
413 { SAFNUM_UNICAST
, "Unicast"},
414 { SAFNUM_MULTICAST
, "Multicast"},
415 { SAFNUM_UNIMULTICAST
, "Unicast+Multicast"},
416 { SAFNUM_LABUNICAST
, "labeled Unicast"},
417 { SAFNUM_TUNNEL
, "Tunnel"},
418 { SAFNUM_VPLS
, "VPLS"},
419 { SAFNUM_MDT
, "MDT"},
420 { SAFNUM_EVPN
, "EVPN"},
421 { SAFNUM_VPNUNICAST
, "labeled VPN Unicast"},
422 { SAFNUM_VPNMULTICAST
, "labeled VPN Multicast"},
423 { SAFNUM_VPNUNIMULTICAST
, "labeled VPN Unicast+Multicast"},
424 { SAFNUM_RT_ROUTING_INFO
, "Route Target Routing Information"},
425 { SAFNUM_MULTICAST_VPN
, "Multicast VPN"},
429 static const struct tok bgp_graceful_restart_comm_flag_values
[] = {
435 /* well-known community */
436 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
437 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
438 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
440 /* Extended community type - RFC 4360 */
441 #define BGP_EXT_COM_RT_0 0x0002 /* Route Target,Format AS(2bytes):AN(4bytes) */
442 #define BGP_EXT_COM_RT_1 0x0102 /* Route Target,Format IP address:AN(2bytes) */
443 #define BGP_EXT_COM_RT_2 0x0202 /* Route Target,Format AN(4bytes):local(2bytes) */
444 #define BGP_EXT_COM_RO_0 0x0003 /* Route Origin,Format AS(2bytes):AN(4bytes) */
445 #define BGP_EXT_COM_RO_1 0x0103 /* Route Origin,Format IP address:AN(2bytes) */
446 #define BGP_EXT_COM_RO_2 0x0203 /* Route Origin,Format AN(4bytes):local(2bytes) */
447 #define BGP_EXT_COM_LINKBAND 0x4004 /* Link Bandwidth,Format AS(2B):Bandwidth(4B) */
448 /* rfc2547 bgp-mpls-vpns */
449 #define BGP_EXT_COM_VPN_ORIGIN 0x0005 /* OSPF Domain ID / VPN of Origin - draft-rosen-vpns-ospf-bgp-mpls */
450 #define BGP_EXT_COM_VPN_ORIGIN2 0x0105 /* duplicate - keep for backwards compatibility */
451 #define BGP_EXT_COM_VPN_ORIGIN3 0x0205 /* duplicate - keep for backwards compatibility */
452 #define BGP_EXT_COM_VPN_ORIGIN4 0x8005 /* duplicate - keep for backwards compatibility */
454 #define BGP_EXT_COM_OSPF_RTYPE 0x0306 /* OSPF Route Type,Format Area(4B):RouteType(1B):Options(1B) */
455 #define BGP_EXT_COM_OSPF_RTYPE2 0x8000 /* duplicate - keep for backwards compatibility */
456 #define BGP_EXT_COM_ENCAP 0x030c /* rfc5512 */
458 #define BGP_EXT_COM_OSPF_RID 0x0107 /* OSPF Router ID,Format RouterID(4B):Unused(2B) */
459 #define BGP_EXT_COM_OSPF_RID2 0x8001 /* duplicate - keep for backwards compatibility */
461 #define BGP_EXT_COM_L2INFO 0x800a /* draft-kompella-ppvpn-l2vpn */
463 #define BGP_EXT_COM_SOURCE_AS 0x0009 /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */
464 #define BGP_EXT_COM_VRF_RT_IMP 0x010b /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */
465 #define BGP_EXT_COM_L2VPN_RT_0 0x000a /* L2VPN Identifier,Format AS(2bytes):AN(4bytes) */
466 #define BGP_EXT_COM_L2VPN_RT_1 0xF10a /* L2VPN Identifier,Format IP address:AN(2bytes) */
468 /* https://www.cisco.com/en/US/tech/tk436/tk428/technologies_tech_note09186a00801eb09a.shtml */
469 #define BGP_EXT_COM_EIGRP_GEN 0x8800
470 #define BGP_EXT_COM_EIGRP_METRIC_AS_DELAY 0x8801
471 #define BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW 0x8802
472 #define BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU 0x8803
473 #define BGP_EXT_COM_EIGRP_EXT_REMAS_REMID 0x8804
474 #define BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC 0x8805
476 /* Optional Parameters */
477 #define BGP_OPEN_NON_EXT_OPT_TYPE_EXTENDED_LENGTH 255 /* Non-Ext OP Type */
479 static const struct tok bgp_extd_comm_flag_values
[] = {
480 { 0x8000, "vendor-specific"},
481 { 0x4000, "non-transitive"},
485 static const struct tok bgp_extd_comm_subtype_values
[] = {
486 { BGP_EXT_COM_RT_0
, "target"},
487 { BGP_EXT_COM_RT_1
, "target"},
488 { BGP_EXT_COM_RT_2
, "target"},
489 { BGP_EXT_COM_RO_0
, "origin"},
490 { BGP_EXT_COM_RO_1
, "origin"},
491 { BGP_EXT_COM_RO_2
, "origin"},
492 { BGP_EXT_COM_LINKBAND
, "link-BW"},
493 { BGP_EXT_COM_VPN_ORIGIN
, "ospf-domain"},
494 { BGP_EXT_COM_VPN_ORIGIN2
, "ospf-domain"},
495 { BGP_EXT_COM_VPN_ORIGIN3
, "ospf-domain"},
496 { BGP_EXT_COM_VPN_ORIGIN4
, "ospf-domain"},
497 { BGP_EXT_COM_OSPF_RTYPE
, "ospf-route-type"},
498 { BGP_EXT_COM_OSPF_RTYPE2
, "ospf-route-type"},
499 { BGP_EXT_COM_ENCAP
, "encapsulation"},
500 { BGP_EXT_COM_OSPF_RID
, "ospf-router-id"},
501 { BGP_EXT_COM_OSPF_RID2
, "ospf-router-id"},
502 { BGP_EXT_COM_L2INFO
, "layer2-info"},
503 { BGP_EXT_COM_EIGRP_GEN
, "eigrp-general-route (flag, tag)" },
504 { BGP_EXT_COM_EIGRP_METRIC_AS_DELAY
, "eigrp-route-metric (AS, delay)" },
505 { BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW
, "eigrp-route-metric (reliability, nexthop, bandwidth)" },
506 { BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU
, "eigrp-route-metric (load, MTU)" },
507 { BGP_EXT_COM_EIGRP_EXT_REMAS_REMID
, "eigrp-external-route (remote-AS, remote-ID)" },
508 { BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC
, "eigrp-external-route (remote-proto, remote-metric)" },
509 { BGP_EXT_COM_SOURCE_AS
, "source-AS" },
510 { BGP_EXT_COM_VRF_RT_IMP
, "vrf-route-import"},
511 { BGP_EXT_COM_L2VPN_RT_0
, "l2vpn-id"},
512 { BGP_EXT_COM_L2VPN_RT_1
, "l2vpn-id"},
516 /* RFC RFC5512 BGP Tunnel Encapsulation Attribute Tunnel Types */
517 #define BGP_ENCAP_TUNNEL_L2TPV3_IP 1
518 #define BGP_ENCAP_TUNNEL_GRE 2
519 #define BGP_ENCAP_TUNNEL_TRANSMIT 3
520 #define BGP_ENCAP_TUNNEL_IPSEC 4
521 #define BGP_ENCAP_TUNNEL_IP_IPSEC 5
522 #define BGP_ENCAP_TUNNEL_MPLS_IP 6
523 #define BGP_ENCAP_TUNNEL_IP_IP 7
524 #define BGP_ENCAP_TUNNEL_VXLAN 8
525 #define BGP_ENCAP_TUNNEL_NVGRE 9
526 #define BGP_ENCAP_TUNNEL_MPLS 10
527 #define BGP_ENCAP_TUNNEL_MPLS_GRE 11
528 #define BGP_ENCAP_TUNNEL_VXLAN_GPE 12
529 #define BGP_ENCAP_TUNNEL_MPLS_UDP 13
530 #define BGP_ENCAP_TUNNEL_IPV6 14
531 #define BGP_ENCAP_TUNNEL_SR_TE 15
532 #define BGP_ENCAP_TUNNEL_BARE 16
533 #define BGP_ENCAP_TUNNEL_SR 17
535 static const struct tok bgp_extd_comm_encap_tunnel_values
[] = {
536 { BGP_ENCAP_TUNNEL_L2TPV3_IP
, "L2TPv3 over IP"},
537 { BGP_ENCAP_TUNNEL_GRE
, "GRE"},
538 { BGP_ENCAP_TUNNEL_TRANSMIT
, "Transmit Tunnel"},
539 { BGP_ENCAP_TUNNEL_IPSEC
, "IPsec"},
540 { BGP_ENCAP_TUNNEL_IP_IPSEC
, "IP in IP with IPsec"},
541 { BGP_ENCAP_TUNNEL_MPLS_IP
, "MPLS in IP with IPsec"},
542 { BGP_ENCAP_TUNNEL_IP_IP
, "IP in IP"},
543 { BGP_ENCAP_TUNNEL_VXLAN
, "VXLAN"},
544 { BGP_ENCAP_TUNNEL_NVGRE
, "NVGRE"},
545 { BGP_ENCAP_TUNNEL_MPLS
, "MPLS"},
546 { BGP_ENCAP_TUNNEL_MPLS_GRE
, "MPLS in GRE"},
547 { BGP_ENCAP_TUNNEL_VXLAN_GPE
, "VXLAN GPE"},
548 { BGP_ENCAP_TUNNEL_MPLS_UDP
, "MPLS in UDP"},
549 { BGP_ENCAP_TUNNEL_IPV6
, "IPv6"},
550 { BGP_ENCAP_TUNNEL_SR_TE
, "SR TE"},
551 { BGP_ENCAP_TUNNEL_BARE
, "Bare"},
552 { BGP_ENCAP_TUNNEL_SR
, "SR"},
556 /* OSPF codes for BGP_EXT_COM_OSPF_RTYPE draft-rosen-vpns-ospf-bgp-mpls */
557 #define BGP_OSPF_RTYPE_RTR 1 /* OSPF Router LSA */
558 #define BGP_OSPF_RTYPE_NET 2 /* OSPF Network LSA */
559 #define BGP_OSPF_RTYPE_SUM 3 /* OSPF Summary LSA */
560 #define BGP_OSPF_RTYPE_EXT 5 /* OSPF External LSA, note that ASBR doesn't apply to MPLS-VPN */
561 #define BGP_OSPF_RTYPE_NSSA 7 /* OSPF NSSA External*/
562 #define BGP_OSPF_RTYPE_SHAM 129 /* OSPF-MPLS-VPN Sham link */
563 #define BGP_OSPF_RTYPE_METRIC_TYPE 0x1 /* LSB of RTYPE Options Field */
565 static const struct tok bgp_extd_comm_ospf_rtype_values
[] = {
566 { BGP_OSPF_RTYPE_RTR
, "Router" },
567 { BGP_OSPF_RTYPE_NET
, "Network" },
568 { BGP_OSPF_RTYPE_SUM
, "Summary" },
569 { BGP_OSPF_RTYPE_EXT
, "External" },
570 { BGP_OSPF_RTYPE_NSSA
,"NSSA External" },
571 { BGP_OSPF_RTYPE_SHAM
,"MPLS-VPN Sham" },
575 /* ADD-PATH Send/Receive field values */
576 static const struct tok bgp_add_path_recvsend
[] = {
583 #define AS_STR_SIZE sizeof("xxxxx.xxxxx")
588 * Convert an AS number into a string and return string pointer.
590 * Depending on bflag is set or not, AS number is converted into ASDOT notation
591 * or plain number notation.
595 as_printf(netdissect_options
*ndo
,
596 char *str
, size_t size
, u_int asnum
)
598 if (!ndo
->ndo_bflag
|| asnum
<= 0xFFFF) {
599 snprintf(str
, size
, "%u", asnum
);
601 snprintf(str
, size
, "%u.%u", asnum
>> 16, asnum
& 0xFFFF);
606 #define ITEMCHECK(minlen) if (itemlen < minlen) goto badtlv;
609 decode_prefix4(netdissect_options
*ndo
,
610 const u_char
*pptr
, u_int itemlen
, char *buf
, size_t buflen
)
613 u_int plen
, plenbytes
;
616 plen
= GET_U_1(pptr
);
621 memset(&addr
, 0, sizeof(addr
));
622 plenbytes
= (plen
+ 7) / 8;
623 ITEMCHECK(plenbytes
);
624 GET_CPY_BYTES(&addr
, pptr
+ 1, plenbytes
);
626 ((u_char
*)&addr
)[plenbytes
- 1] &= ((0xff00 >> (plen
% 8)) & 0xff);
628 snprintf(buf
, buflen
, "%s/%u", ipaddr_string(ndo
, (const u_char
*)&addr
), plen
);
629 return 1 + plenbytes
;
636 decode_labeled_prefix4(netdissect_options
*ndo
,
637 const u_char
*pptr
, u_int itemlen
, char *buf
,
641 u_int plen
, plenbytes
;
643 /* prefix length and label = 4 bytes */
646 plen
= GET_U_1(pptr
); /* get prefix length */
648 /* this is one of the weirdnesses of rfc3107
649 the label length (actually the label + COS bits)
650 is added to the prefix length;
651 we also do only read out just one label -
652 there is no real application for advertisement of
653 stacked labels in a single BGP message
659 plen
-=24; /* adjust prefixlen - labellength */
665 memset(&addr
, 0, sizeof(addr
));
666 plenbytes
= (plen
+ 7) / 8;
667 ITEMCHECK(plenbytes
);
668 GET_CPY_BYTES(&addr
, pptr
+ 4, plenbytes
);
670 ((u_char
*)&addr
)[plenbytes
- 1] &= ((0xff00 >> (plen
% 8)) & 0xff);
672 /* the label may get offsetted by 4 bits so lets shift it right */
673 snprintf(buf
, buflen
, "%s/%u, label:%u %s",
674 ipaddr_string(ndo
, (const u_char
*)&addr
),
676 GET_BE_U_3(pptr
+ 1)>>4,
677 ((GET_U_1(pptr
+ 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
679 return 4 + plenbytes
;
691 * print an ipv4 or ipv6 address into a buffer dependent on address length.
694 bgp_vpn_ip_print(netdissect_options
*ndo
,
695 const u_char
*pptr
, u_int addr_length
)
698 /* worst case string is s fully formatted v6 address */
699 static char addr
[sizeof("1234:5678:89ab:cdef:1234:5678:89ab:cdef")];
702 switch(addr_length
) {
703 case (sizeof(nd_ipv4
) << 3): /* 32 */
704 snprintf(pos
, sizeof(addr
), "%s", GET_IPADDR_STRING(pptr
));
706 case (sizeof(nd_ipv6
) << 3): /* 128 */
707 snprintf(pos
, sizeof(addr
), "%s", GET_IP6ADDR_STRING(pptr
));
710 snprintf(pos
, sizeof(addr
), "bogus address length %u", addr_length
);
722 * print an multicast s,g entry into a buffer.
723 * the s,g entry is encoded like this.
725 * +-----------------------------------+
726 * | Multicast Source Length (1 octet) |
727 * +-----------------------------------+
728 * | Multicast Source (Variable) |
729 * +-----------------------------------+
730 * | Multicast Group Length (1 octet) |
731 * +-----------------------------------+
732 * | Multicast Group (Variable) |
733 * +-----------------------------------+
735 * return the number of bytes read from the wire.
738 bgp_vpn_sg_print(netdissect_options
*ndo
,
739 const u_char
*pptr
, char *buf
, size_t buflen
)
742 u_int total_length
, offset
;
746 /* Source address length, encoded in bits */
747 addr_length
= GET_U_1(pptr
);
751 ND_TCHECK_LEN(pptr
, (addr_length
>> 3));
752 total_length
+= (addr_length
>> 3) + 1;
753 offset
= (u_int
)strlen(buf
);
755 snprintf(buf
+ offset
, buflen
- offset
, ", Source %s",
756 bgp_vpn_ip_print(ndo
, pptr
, addr_length
));
757 pptr
+= (addr_length
>> 3);
760 /* Group address length, encoded in bits */
761 addr_length
= GET_U_1(pptr
);
765 ND_TCHECK_LEN(pptr
, (addr_length
>> 3));
766 total_length
+= (addr_length
>> 3) + 1;
767 offset
= (u_int
)strlen(buf
);
769 snprintf(buf
+ offset
, buflen
- offset
, ", Group %s",
770 bgp_vpn_ip_print(ndo
, pptr
, addr_length
));
771 pptr
+= (addr_length
>> 3);
775 return (total_length
);
778 /* Print an RFC 4364 Route Distinguisher */
780 bgp_vpn_rd_print(netdissect_options
*ndo
,
783 /* allocate space for the largest possible string */
784 static char rd
[sizeof("xxxxx.xxxxx:xxxxx (xxx.xxx.xxx.xxx:xxxxx)")];
786 /* allocate space for the largest possible string */
787 char astostr
[AS_STR_SIZE
];
789 /* ok lets load the RD format */
790 switch (GET_BE_U_2(pptr
)) {
793 /* 2-byte-AS:number fmt */
794 snprintf(pos
, sizeof(rd
) - (pos
- rd
), "%u:%u (= %u.%u.%u.%u)",
795 GET_BE_U_2(pptr
+ 2),
796 GET_BE_U_4(pptr
+ 4),
797 GET_U_1(pptr
+ 4), GET_U_1(pptr
+ 5),
798 GET_U_1(pptr
+ 6), GET_U_1(pptr
+ 7));
802 /* IP-address:AS fmt */
803 snprintf(pos
, sizeof(rd
) - (pos
- rd
), "%u.%u.%u.%u:%u",
804 GET_U_1(pptr
+ 2), GET_U_1(pptr
+ 3),
805 GET_U_1(pptr
+ 4), GET_U_1(pptr
+ 5),
806 GET_BE_U_2(pptr
+ 6));
810 /* 4-byte-AS:number fmt */
811 snprintf(pos
, sizeof(rd
) - (pos
- rd
), "%s:%u (%u.%u.%u.%u:%u)",
812 as_printf(ndo
, astostr
, sizeof(astostr
), GET_BE_U_4(pptr
+ 2)),
813 GET_BE_U_2(pptr
+ 6), GET_U_1(pptr
+ 2),
814 GET_U_1(pptr
+ 3), GET_U_1(pptr
+ 4),
815 GET_U_1(pptr
+ 5), GET_BE_U_2(pptr
+ 6));
818 snprintf(pos
, sizeof(rd
) - (pos
- rd
), "unknown RD format");
827 * Print an RFC 4360 Extended Community.
830 bgp_extended_community_print(netdissect_options
*ndo
,
833 union { /* copy buffer for bandwidth values */
837 /* allocate space for the largest possible string */
838 char astostr
[AS_STR_SIZE
];
840 switch (GET_BE_U_2(pptr
)) {
842 case BGP_EXT_COM_RT_0
:
843 case BGP_EXT_COM_RO_0
:
844 case BGP_EXT_COM_L2VPN_RT_0
:
845 ND_PRINT("%u:%u (= %s)",
846 GET_BE_U_2(pptr
+ 2),
847 GET_BE_U_4(pptr
+ 4),
848 GET_IPADDR_STRING(pptr
+4));
851 case BGP_EXT_COM_RT_1
:
852 case BGP_EXT_COM_RO_1
:
853 case BGP_EXT_COM_L2VPN_RT_1
:
854 case BGP_EXT_COM_VRF_RT_IMP
:
856 GET_IPADDR_STRING(pptr
+2),
857 GET_BE_U_2(pptr
+ 6));
860 case BGP_EXT_COM_RT_2
:
861 case BGP_EXT_COM_RO_2
:
863 as_printf(ndo
, astostr
, sizeof(astostr
),
864 GET_BE_U_4(pptr
+ 2)), GET_BE_U_2(pptr
+ 6));
867 case BGP_EXT_COM_LINKBAND
:
868 bw
.i
= GET_BE_U_4(pptr
+ 4);
869 ND_PRINT("bandwidth: %.3f Mbps",
873 case BGP_EXT_COM_VPN_ORIGIN
:
874 case BGP_EXT_COM_VPN_ORIGIN2
:
875 case BGP_EXT_COM_VPN_ORIGIN3
:
876 case BGP_EXT_COM_VPN_ORIGIN4
:
877 case BGP_EXT_COM_OSPF_RID
:
878 case BGP_EXT_COM_OSPF_RID2
:
879 ND_PRINT("%s", GET_IPADDR_STRING(pptr
+2));
882 case BGP_EXT_COM_OSPF_RTYPE
:
883 case BGP_EXT_COM_OSPF_RTYPE2
:
884 ND_PRINT("area:%s, router-type:%s, metric-type:%s%s",
885 GET_IPADDR_STRING(pptr
+2),
886 tok2str(bgp_extd_comm_ospf_rtype_values
,
888 GET_U_1((pptr
+ 6))),
889 (GET_U_1(pptr
+ 7) & BGP_OSPF_RTYPE_METRIC_TYPE
) ? "E2" : "",
890 ((GET_U_1(pptr
+ 6) == BGP_OSPF_RTYPE_EXT
) || (GET_U_1(pptr
+ 6) == BGP_OSPF_RTYPE_NSSA
)) ? "E1" : "");
893 case BGP_EXT_COM_L2INFO
:
894 ND_PRINT("%s Control Flags [0x%02x]:MTU %u",
895 tok2str(l2vpn_encaps_values
,
897 GET_U_1((pptr
+ 2))),
899 GET_BE_U_2(pptr
+ 4));
902 case BGP_EXT_COM_SOURCE_AS
:
903 ND_PRINT("AS %u", GET_BE_U_2(pptr
+ 2));
906 case BGP_EXT_COM_ENCAP
:
907 ND_PRINT("Tunnel type: %s", tok2str(bgp_extd_comm_encap_tunnel_values
,
909 GET_BE_U_2(pptr
+ 6)));
913 ND_PRINT("%02x%02x%02x%02x%02x%02x",
925 * RFC4684 (Section 4)/RFC2858 (Section 4).
926 * RTC membership prefix is structured as follows
927 * [prefix-len] [origin-as] [route-target]
928 * The route-target is encoded as RT ext-comms.
929 * Prefix-len may be 0, 32..96
931 * Note that pptr is not packet data - it is
932 * a buffer owned by our caller - therefore GET_*
933 * macros can not be used.
936 bgp_rt_prefix_print(netdissect_options
*ndo
,
940 /* allocate space for the largest possible string */
941 char rtc_prefix_in_hex
[sizeof("0000 0000 0000 0000")] = "";
942 u_int rtc_prefix_in_hex_len
= 0;
943 static char output
[61]; /* max response string */
944 /* allocate space for the largest possible string */
945 char astostr
[AS_STR_SIZE
];
946 uint16_t ec_type
= 0;
951 snprintf(output
, sizeof(output
), "route-target: 0:0/0");
955 /* hex representation of the prefix */
956 octet_count
= (plen
+7)/8;
957 for (i
=0; i
<octet_count
; i
++) {
958 rtc_prefix_in_hex_len
+= snprintf(rtc_prefix_in_hex
+rtc_prefix_in_hex_len
,
959 sizeof(rtc_prefix_in_hex
)-rtc_prefix_in_hex_len
,
961 ((i
%2 == 1) && (i
<octet_count
-1)) ? " " : "");
966 * The prefix is too short to include the full ext-comm type,
967 * so we have no way to parse it further.
969 snprintf(output
, sizeof(output
), "route-target: partial-type: (%s/%d)",
970 rtc_prefix_in_hex
, plen
);
975 * get the ext-comm type
976 * Note: pptr references a static 8 octet buffer with unused bits set to 0,
977 * hence EXTRACT_*() macros are safe.
979 ec_type
= EXTRACT_BE_U_2(pptr
);
981 case BGP_EXT_COM_RT_0
:
982 /* 2-byte-AS:number fmt */
983 snprintf(output
, sizeof(output
), "route-target: %u:%u/%d (%s)",
984 EXTRACT_BE_U_2(pptr
+2),
985 EXTRACT_BE_U_4(pptr
+4),
986 plen
, rtc_prefix_in_hex
);
989 case BGP_EXT_COM_RT_1
:
990 /* IP-address:AS fmt */
991 snprintf(output
, sizeof(output
), "route-target: %u.%u.%u.%u:%u/%d (%s)",
992 *(pptr
+2), *(pptr
+3), *(pptr
+4), *(pptr
+5),
993 EXTRACT_BE_U_2(pptr
+6), plen
, rtc_prefix_in_hex
);
996 case BGP_EXT_COM_RT_2
:
997 /* 4-byte-AS:number fmt */
998 snprintf(output
, sizeof(output
), "route-target: %s:%u/%d (%s)",
999 as_printf(ndo
, astostr
, sizeof(astostr
), EXTRACT_BE_U_4(pptr
+2)),
1000 EXTRACT_BE_U_2(pptr
+6), plen
, rtc_prefix_in_hex
);
1004 snprintf(output
, sizeof(output
), "route target: unknown-type(%04x) (%s/%d)",
1006 rtc_prefix_in_hex
, plen
);
1014 decode_rt_routing_info(netdissect_options
*ndo
,
1017 uint8_t route_target
[8];
1019 /* allocate space for the largest possible string */
1020 char astostr
[AS_STR_SIZE
];
1023 /* NLRI "prefix length" from RFC 2858 Section 4. */
1024 plen
= GET_U_1(pptr
); /* get prefix length */
1026 /* NLRI "prefix" (ibid), valid lengths are { 0, 32, 33, ..., 96 } bits.
1027 * RFC 4684 Section 4 defines the layout of "origin AS" and "route
1028 * target" fields inside the "prefix" depending on its length.
1031 /* Without "origin AS", without "route target". */
1032 ND_PRINT("\n\t default route target");
1037 ND_PRINT("\n\t (illegal prefix length)");
1041 /* With at least "origin AS", possibly with "route target". */
1042 as_printf(ndo
, astostr
, sizeof(astostr
), GET_BE_U_4(pptr
+ 1));
1044 plen
-= 32; /* adjust prefix length */
1047 ND_PRINT("\n\t (illegal prefix length)");
1051 /* From now on (plen + 7) / 8 evaluates to { 0, 1, 2, ..., 8 }
1052 * and gives the number of octets in the variable-length "route
1053 * target" field inside this NLRI "prefix". Look for it.
1055 memset(&route_target
, 0, sizeof(route_target
));
1056 num_octets
= (plen
+ 7) / 8;
1057 GET_CPY_BYTES(&route_target
, pptr
+ 5, num_octets
);
1058 /* If mask-len is not on octet boundary, ensure all extra bits are 0 */
1060 ((u_char
*)&route_target
)[num_octets
- 1] &=
1061 ((0xff00 >> (plen
% 8)) & 0xff);
1063 ND_PRINT("\n\t origin AS: %s, %s",
1065 bgp_rt_prefix_print(ndo
, (u_char
*)&route_target
, plen
));
1067 return 5 + num_octets
;
1071 decode_labeled_vpn_prefix4(netdissect_options
*ndo
,
1072 const u_char
*pptr
, char *buf
, size_t buflen
)
1077 plen
= GET_U_1(pptr
); /* get prefix length */
1082 plen
-= (24+64); /* adjust prefixlen - labellength - RD len*/
1087 memset(&addr
, 0, sizeof(addr
));
1088 GET_CPY_BYTES(&addr
, pptr
+ 12, (plen
+ 7) / 8);
1090 ((u_char
*)&addr
)[(plen
+ 7) / 8 - 1] &=
1091 ((0xff00 >> (plen
% 8)) & 0xff);
1093 /* the label may get offsetted by 4 bits so lets shift it right */
1094 snprintf(buf
, buflen
, "RD: %s, %s/%u, label:%u %s",
1095 bgp_vpn_rd_print(ndo
, pptr
+4),
1096 ipaddr_string(ndo
, (const u_char
*)&addr
),
1098 GET_BE_U_3(pptr
+ 1)>>4,
1099 ((GET_U_1(pptr
+ 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1101 return 12 + (plen
+ 7) / 8;
1105 * +-------------------------------+
1107 * | RD:IPv4-address (12 octets) |
1109 * +-------------------------------+
1110 * | MDT Group-address (4 octets) |
1111 * +-------------------------------+
1114 #define MDT_VPN_NLRI_LEN 16
1117 decode_mdt_vpn_nlri(netdissect_options
*ndo
,
1118 const u_char
*pptr
, char *buf
, size_t buflen
)
1121 const u_char
*vpn_ip
;
1123 /* if the NLRI is not predefined length, quit.*/
1124 if (GET_U_1(pptr
) != MDT_VPN_NLRI_LEN
* 8)
1135 pptr
+= sizeof(nd_ipv4
);
1137 /* MDT Group Address */
1138 snprintf(buf
, buflen
, "RD: %s, VPN IP Address: %s, MC Group Address: %s",
1139 bgp_vpn_rd_print(ndo
, rd
), GET_IPADDR_STRING(vpn_ip
), GET_IPADDR_STRING(pptr
));
1141 return MDT_VPN_NLRI_LEN
+ 1;
1147 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI 1
1148 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI 2
1149 #define BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI 3
1150 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF 4
1151 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE 5
1152 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN 6
1153 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN 7
1155 static const struct tok bgp_multicast_vpn_route_type_values
[] = {
1156 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI
, "Intra-AS I-PMSI"},
1157 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI
, "Inter-AS I-PMSI"},
1158 { BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI
, "S-PMSI"},
1159 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF
, "Intra-AS Segment-Leaf"},
1160 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE
, "Source-Active"},
1161 { BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN
, "Shared Tree Join"},
1162 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN
, "Source Tree Join"},
1167 decode_multicast_vpn(netdissect_options
*ndo
,
1168 const u_char
*pptr
, char *buf
, size_t buflen
)
1170 /* allocate space for the largest possible string */
1171 char astostr
[AS_STR_SIZE
];
1172 uint8_t route_type
, route_length
;
1173 u_int addr_length
, sg_length
;
1176 route_type
= GET_U_1(pptr
);
1178 route_length
= GET_U_1(pptr
);
1181 snprintf(buf
, buflen
, "Route-Type: %s (%u), length: %u",
1182 tok2str(bgp_multicast_vpn_route_type_values
,
1183 "Unknown", route_type
),
1184 route_type
, route_length
);
1186 switch(route_type
) {
1187 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI
:
1188 ND_TCHECK_LEN(pptr
, BGP_VPN_RD_LEN
);
1189 offset
= (u_int
)strlen(buf
);
1190 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s, Originator %s",
1191 bgp_vpn_rd_print(ndo
, pptr
),
1192 bgp_vpn_ip_print(ndo
, pptr
+ BGP_VPN_RD_LEN
,
1193 (route_length
- BGP_VPN_RD_LEN
) << 3));
1195 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI
:
1196 ND_TCHECK_LEN(pptr
, BGP_VPN_RD_LEN
+ 4);
1197 offset
= (u_int
)strlen(buf
);
1198 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s, Source-AS %s",
1199 bgp_vpn_rd_print(ndo
, pptr
),
1200 as_printf(ndo
, astostr
, sizeof(astostr
),
1201 GET_BE_U_4(pptr
+ BGP_VPN_RD_LEN
)));
1204 case BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI
:
1205 ND_TCHECK_LEN(pptr
, BGP_VPN_RD_LEN
);
1206 offset
= (u_int
)strlen(buf
);
1207 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s",
1208 bgp_vpn_rd_print(ndo
, pptr
));
1209 pptr
+= BGP_VPN_RD_LEN
;
1211 sg_length
= bgp_vpn_sg_print(ndo
, pptr
, buf
, buflen
);
1212 addr_length
= route_length
- sg_length
;
1214 ND_TCHECK_LEN(pptr
, addr_length
);
1215 offset
= (u_int
)strlen(buf
);
1216 snprintf(buf
+ offset
, buflen
- offset
, ", Originator %s",
1217 bgp_vpn_ip_print(ndo
, pptr
, addr_length
<< 3));
1220 case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE
:
1221 ND_TCHECK_LEN(pptr
, BGP_VPN_RD_LEN
);
1222 offset
= (u_int
)strlen(buf
);
1223 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s",
1224 bgp_vpn_rd_print(ndo
, pptr
));
1225 pptr
+= BGP_VPN_RD_LEN
;
1227 bgp_vpn_sg_print(ndo
, pptr
, buf
, buflen
);
1230 case BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN
: /* fall through */
1231 case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN
:
1232 ND_TCHECK_LEN(pptr
, BGP_VPN_RD_LEN
+ 4);
1233 offset
= (u_int
)strlen(buf
);
1234 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s, Source-AS %s",
1235 bgp_vpn_rd_print(ndo
, pptr
),
1236 as_printf(ndo
, astostr
, sizeof(astostr
),
1237 GET_BE_U_4(pptr
+ BGP_VPN_RD_LEN
)));
1238 pptr
+= BGP_VPN_RD_LEN
+ 4;
1240 bgp_vpn_sg_print(ndo
, pptr
, buf
, buflen
);
1244 * no per route-type printing yet.
1246 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF
:
1251 return route_length
+ 2;
1258 * As I remember, some versions of systems have an snprintf() that
1259 * returns -1 if the buffer would have overflowed. If the return
1260 * value is negative, set buflen to 0, to indicate that we've filled
1263 * If the return value is greater than buflen, that means that
1264 * the buffer would have overflowed; again, set buflen to 0 in
1267 #define UPDATE_BUF_BUFLEN(buf, buflen, stringlen) \
1270 else if ((u_int)stringlen>buflen) \
1273 buflen-=stringlen; \
1278 decode_labeled_vpn_l2(netdissect_options
*ndo
,
1279 const u_char
*pptr
, char *buf
, size_t buflen
)
1281 u_int plen
, tlen
, tlv_type
, tlv_len
, ttlv_len
;
1284 plen
= GET_BE_U_2(pptr
);
1287 /* Old and new L2VPN NLRI share AFI/SAFI
1288 * -> Assume a 12 Byte-length NLRI is auto-discovery-only
1289 * and > 17 as old format. Complain for the middle case
1292 /* assume AD-only with RD, BGPNH */
1293 ND_TCHECK_LEN(pptr
, 12);
1295 stringlen
= snprintf(buf
, buflen
, "RD: %s, BGPNH: %s",
1296 bgp_vpn_rd_print(ndo
, pptr
),
1297 GET_IPADDR_STRING(pptr
+8));
1298 UPDATE_BUF_BUFLEN(buf
, buflen
, stringlen
);
1302 } else if (plen
> 17) {
1303 /* assume old format */
1304 /* RD, ID, LBLKOFF, LBLBASE */
1306 ND_TCHECK_LEN(pptr
, 15);
1308 stringlen
= snprintf(buf
, buflen
, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
1309 bgp_vpn_rd_print(ndo
, pptr
),
1310 GET_BE_U_2(pptr
+ 8),
1311 GET_BE_U_2(pptr
+ 10),
1312 GET_BE_U_3(pptr
+ 12)>>4); /* the label is offsetted by 4 bits so lets shift it right */
1313 UPDATE_BUF_BUFLEN(buf
, buflen
, stringlen
);
1317 /* ok now the variable part - lets read out TLVs*/
1321 stringlen
=snprintf(buf
,buflen
, "\n\t\tran past the end");
1322 UPDATE_BUF_BUFLEN(buf
, buflen
, stringlen
);
1326 tlv_type
= GET_U_1(pptr
);
1328 tlv_len
= GET_BE_U_2(pptr
); /* length, in *bits* */
1329 ttlv_len
= (tlv_len
+ 7)/8; /* length, in *bytes* */
1335 stringlen
=snprintf(buf
,buflen
, "\n\t\tcircuit status vector (%u) length: %u: 0x",
1338 UPDATE_BUF_BUFLEN(buf
, buflen
, stringlen
);
1340 while (ttlv_len
!= 0) {
1343 stringlen
=snprintf(buf
,buflen
, " (ran past the end)");
1344 UPDATE_BUF_BUFLEN(buf
, buflen
, stringlen
);
1350 stringlen
=snprintf(buf
,buflen
, "%02x",
1353 UPDATE_BUF_BUFLEN(buf
, buflen
, stringlen
);
1361 stringlen
=snprintf(buf
,buflen
, "\n\t\tunknown TLV #%u, length: %u",
1364 UPDATE_BUF_BUFLEN(buf
, buflen
, stringlen
);
1366 if (tlen
< ttlv_len
) {
1368 stringlen
=snprintf(buf
,buflen
, " (ran past the end)");
1369 UPDATE_BUF_BUFLEN(buf
, buflen
, stringlen
);
1379 /* complain bitterly ? */
1389 decode_prefix6(netdissect_options
*ndo
,
1390 const u_char
*pd
, u_int itemlen
, char *buf
, size_t buflen
)
1393 u_int plen
, plenbytes
;
1401 memset(&addr
, 0, sizeof(addr
));
1402 plenbytes
= (plen
+ 7) / 8;
1403 ITEMCHECK(plenbytes
);
1404 GET_CPY_BYTES(&addr
, pd
+ 1, plenbytes
);
1406 addr
[plenbytes
- 1] &=
1407 ((0xff00 >> (plen
% 8)) & 0xff);
1409 snprintf(buf
, buflen
, "%s/%u", ip6addr_string(ndo
, (const u_char
*)&addr
), plen
);
1410 return 1 + plenbytes
;
1417 decode_labeled_prefix6(netdissect_options
*ndo
,
1418 const u_char
*pptr
, u_int itemlen
, char *buf
, size_t buflen
)
1421 u_int plen
, plenbytes
;
1423 /* prefix length and label = 4 bytes */
1426 plen
= GET_U_1(pptr
); /* get prefix length */
1431 plen
-= 24; /* adjust prefixlen - labellength */
1437 memset(&addr
, 0, sizeof(addr
));
1438 plenbytes
= (plen
+ 7) / 8;
1439 GET_CPY_BYTES(&addr
, pptr
+ 4, plenbytes
);
1441 addr
[plenbytes
- 1] &=
1442 ((0xff00 >> (plen
% 8)) & 0xff);
1444 /* the label may get offsetted by 4 bits so lets shift it right */
1445 snprintf(buf
, buflen
, "%s/%u, label:%u %s",
1446 ip6addr_string(ndo
, (const u_char
*)&addr
),
1448 GET_BE_U_3(pptr
+ 1)>>4,
1449 ((GET_U_1(pptr
+ 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1451 return 4 + plenbytes
;
1461 decode_labeled_vpn_prefix6(netdissect_options
*ndo
,
1462 const u_char
*pptr
, char *buf
, size_t buflen
)
1467 plen
= GET_U_1(pptr
); /* get prefix length */
1472 plen
-= (24+64); /* adjust prefixlen - labellength - RD len*/
1477 memset(&addr
, 0, sizeof(addr
));
1478 GET_CPY_BYTES(&addr
, pptr
+ 12, (plen
+ 7) / 8);
1480 addr
[(plen
+ 7) / 8 - 1] &=
1481 ((0xff00 >> (plen
% 8)) & 0xff);
1483 /* the label may get offsetted by 4 bits so lets shift it right */
1484 snprintf(buf
, buflen
, "RD: %s, %s/%u, label:%u %s",
1485 bgp_vpn_rd_print(ndo
, pptr
+4),
1486 ip6addr_string(ndo
, (const u_char
*)&addr
),
1488 GET_BE_U_3(pptr
+ 1)>>4,
1489 ((GET_U_1(pptr
+ 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1491 return 12 + (plen
+ 7) / 8;
1495 decode_clnp_prefix(netdissect_options
*ndo
,
1496 const u_char
*pptr
, char *buf
, size_t buflen
)
1501 plen
= GET_U_1(pptr
); /* get prefix length */
1506 memset(&addr
, 0, sizeof(addr
));
1507 GET_CPY_BYTES(&addr
, pptr
+ 4, (plen
+ 7) / 8);
1509 addr
[(plen
+ 7) / 8 - 1] &=
1510 ((0xff00 >> (plen
% 8)) & 0xff);
1512 /* Cannot use GET_ISONSAP_STRING (not packet buffer pointer) */
1513 snprintf(buf
, buflen
, "%s/%u",
1514 isonsap_string(ndo
, addr
,(plen
+ 7) / 8),
1517 return 1 + (plen
+ 7) / 8;
1521 decode_labeled_vpn_clnp_prefix(netdissect_options
*ndo
,
1522 const u_char
*pptr
, char *buf
, size_t buflen
)
1527 plen
= GET_U_1(pptr
); /* get prefix length */
1532 plen
-= (24+64); /* adjust prefixlen - labellength - RD len*/
1537 memset(&addr
, 0, sizeof(addr
));
1538 GET_CPY_BYTES(&addr
, pptr
+ 12, (plen
+ 7) / 8);
1540 addr
[(plen
+ 7) / 8 - 1] &= ((0xff00 >> (plen
% 8)) & 0xff);
1542 /* the label may get offsetted by 4 bits so lets shift it right */
1543 /* Cannot use GET_ISONSAP_STRING (not packet buffer pointer) */
1544 snprintf(buf
, buflen
, "RD: %s, %s/%u, label:%u %s",
1545 bgp_vpn_rd_print(ndo
, pptr
+4),
1546 isonsap_string(ndo
, addr
,(plen
+ 7) / 8),
1548 GET_BE_U_3(pptr
+ 1)>>4,
1549 ((GET_U_1(pptr
+ 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1551 return 12 + (plen
+ 7) / 8;
1555 * bgp_attr_get_as_size
1557 * Try to find the size of the ASs encoded in an as-path. It is not obvious, as
1558 * both Old speakers that do not support 4 byte AS, and the new speakers that do
1559 * support, exchange AS-Path with the same path-attribute type value 0x02.
1562 bgp_attr_get_as_size(netdissect_options
*ndo
,
1563 uint8_t bgpa_type
, const u_char
*pptr
, u_int len
)
1565 const u_char
*tptr
= pptr
;
1568 * If the path attribute is the optional AS4 path type, then we already
1569 * know, that ASs must be encoded in 4 byte format.
1571 if (bgpa_type
== BGPTYPE_AS4_PATH
) {
1576 * Let us assume that ASs are of 2 bytes in size, and check if the AS-Path
1577 * TLV is good. If not, ask the caller to try with AS encoded as 4 bytes
1580 while (tptr
< pptr
+ len
) {
1582 * If we do not find a valid segment type, our guess might be wrong.
1584 if (GET_U_1(tptr
) < BGP_AS_SEG_TYPE_MIN
|| GET_U_1(tptr
) > BGP_AS_SEG_TYPE_MAX
) {
1587 tptr
+= 2 + GET_U_1(tptr
+ 1) * 2;
1591 * If we correctly reached end of the AS path attribute data content,
1592 * then most likely ASs were indeed encoded as 2 bytes.
1594 if (tptr
== pptr
+ len
) {
1601 * We can come here, either we did not have enough data, or if we
1602 * try to decode 4 byte ASs in 2 byte format. Either way, return 4,
1603 * so that calller can try to decode each AS as of 4 bytes. If indeed
1604 * there was not enough data, it will crib and end the parse anyways.
1610 * The only way to know that a BGP UPDATE message is using add path is
1611 * by checking if the capability is in the OPEN message which we may have missed.
1612 * So this function checks if it is possible that the update could contain add path
1613 * and if so it checks that standard BGP doesn't make sense.
1616 check_add_path(netdissect_options
*ndo
, const u_char
*pptr
, u_int length
,
1617 u_int max_prefix_length
)
1619 u_int offset
, prefix_length
;
1626 * Scan through the NLRI information under the assumpetion that
1627 * it doesn't have path IDs.
1629 for (offset
= 0; offset
< length
;) {
1631 if (!ND_TTEST_1(pptr
+ offset
)) {
1632 /* We ran out of captured data; quit scanning. */
1635 prefix_length
= GET_U_1(pptr
+ offset
);
1637 * Add 4 to cover the path id
1638 * and check the prefix length isn't greater than 32/128.
1640 if (prefix_length
> max_prefix_length
) {
1643 /* Add 1 for the prefix_length byte and prefix_length to cover the address */
1644 offset
+= 1 + ((prefix_length
+ 7) / 8);
1646 /* check we haven't gone past the end of the section */
1647 if (offset
> length
) {
1651 /* check it's not standard BGP */
1652 for (offset
= 0; offset
< length
; ) {
1653 if (!ND_TTEST_1(pptr
+ offset
)) {
1654 /* We ran out of captured data; quit scanning. */
1657 prefix_length
= GET_U_1(pptr
+ offset
);
1659 * If the prefix_length is zero (0.0.0.0/0)
1660 * and since it's not the only address (length >= 5)
1661 * then it is add-path
1663 if (prefix_length
< 1 || prefix_length
> max_prefix_length
) {
1666 offset
+= 1 + ((prefix_length
+ 7) / 8);
1668 if (offset
> length
) {
1672 /* assume not add-path by default */
1677 bgp_mp_af_print(netdissect_options
*ndo
,
1678 const u_char
*tptr
, u_int tlen
,
1679 uint16_t *afp
, uint8_t *safip
)
1684 af
= GET_BE_U_2(tptr
);
1686 safi
= GET_U_1(tptr
+ 2);
1689 ND_PRINT("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
1690 tok2str(af_values
, "Unknown AFI", af
),
1692 (safi
>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1693 tok2str(bgp_safi_values
, "Unknown SAFI", safi
),
1696 switch(af
<<8 | safi
) {
1697 case (AFNUM_INET
<<8 | SAFNUM_UNICAST
):
1698 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST
):
1699 case (AFNUM_INET
<<8 | SAFNUM_UNIMULTICAST
):
1700 case (AFNUM_INET
<<8 | SAFNUM_LABUNICAST
):
1701 case (AFNUM_INET
<<8 | SAFNUM_RT_ROUTING_INFO
):
1702 case (AFNUM_INET
<<8 | SAFNUM_VPNUNICAST
):
1703 case (AFNUM_INET
<<8 | SAFNUM_VPNMULTICAST
):
1704 case (AFNUM_INET
<<8 | SAFNUM_VPNUNIMULTICAST
):
1705 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST_VPN
):
1706 case (AFNUM_INET
<<8 | SAFNUM_MDT
):
1707 case (AFNUM_INET6
<<8 | SAFNUM_UNICAST
):
1708 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST
):
1709 case (AFNUM_INET6
<<8 | SAFNUM_UNIMULTICAST
):
1710 case (AFNUM_INET6
<<8 | SAFNUM_LABUNICAST
):
1711 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNICAST
):
1712 case (AFNUM_INET6
<<8 | SAFNUM_VPNMULTICAST
):
1713 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNIMULTICAST
):
1714 case (AFNUM_NSAP
<<8 | SAFNUM_UNICAST
):
1715 case (AFNUM_NSAP
<<8 | SAFNUM_MULTICAST
):
1716 case (AFNUM_NSAP
<<8 | SAFNUM_UNIMULTICAST
):
1717 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNICAST
):
1718 case (AFNUM_NSAP
<<8 | SAFNUM_VPNMULTICAST
):
1719 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNIMULTICAST
):
1720 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNICAST
):
1721 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNMULTICAST
):
1722 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNIMULTICAST
):
1723 case (AFNUM_VPLS
<<8 | SAFNUM_VPLS
):
1726 ND_TCHECK_LEN(tptr
, tlen
);
1727 ND_PRINT("\n\t no AFI %u / SAFI %u decoder", af
, safi
);
1728 if (ndo
->ndo_vflag
<= 1)
1729 print_unknown_data(ndo
, tptr
, "\n\t ", tlen
);
1738 bgp_nlri_print(netdissect_options
*ndo
, uint16_t af
, uint8_t safi
,
1739 const u_char
*tptr
, u_int len
,
1740 char *buf
, size_t buflen
,
1741 int add_path4
, int add_path6
)
1746 switch (af
<<8 | safi
) {
1747 case (AFNUM_INET
<<8 | SAFNUM_UNICAST
):
1748 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST
):
1749 case (AFNUM_INET
<<8 | SAFNUM_UNIMULTICAST
):
1751 path_id
= GET_BE_U_4(tptr
);
1754 advance
= decode_prefix4(ndo
, tptr
, len
, buf
, buflen
);
1756 ND_PRINT("\n\t (illegal prefix length)");
1757 else if (advance
== -2)
1758 break; /* bytes left, but not enough */
1760 ND_PRINT("\n\t %s", buf
);
1762 ND_PRINT(" Path Id: %u", path_id
);
1766 case (AFNUM_INET
<<8 | SAFNUM_LABUNICAST
):
1767 advance
= decode_labeled_prefix4(ndo
, tptr
, len
, buf
, buflen
);
1769 ND_PRINT("\n\t (illegal prefix length)");
1770 else if (advance
== -2)
1772 else if (advance
== -3)
1773 break; /* bytes left, but not enough */
1775 ND_PRINT("\n\t %s", buf
);
1777 case (AFNUM_INET
<<8 | SAFNUM_VPNUNICAST
):
1778 case (AFNUM_INET
<<8 | SAFNUM_VPNMULTICAST
):
1779 case (AFNUM_INET
<<8 | SAFNUM_VPNUNIMULTICAST
):
1780 advance
= decode_labeled_vpn_prefix4(ndo
, tptr
, buf
, buflen
);
1782 ND_PRINT("\n\t (illegal prefix length)");
1784 ND_PRINT("\n\t %s", buf
);
1786 case (AFNUM_INET
<<8 | SAFNUM_RT_ROUTING_INFO
):
1787 advance
= decode_rt_routing_info(ndo
, tptr
);
1789 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST_VPN
): /* fall through */
1790 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST_VPN
):
1791 advance
= decode_multicast_vpn(ndo
, tptr
, buf
, buflen
);
1793 ND_PRINT("\n\t (illegal prefix length)");
1794 else if (advance
== -2)
1797 ND_PRINT("\n\t %s", buf
);
1800 case (AFNUM_INET
<<8 | SAFNUM_MDT
):
1801 advance
= decode_mdt_vpn_nlri(ndo
, tptr
, buf
, buflen
);
1803 ND_PRINT("\n\t (illegal prefix length)");
1804 else if (advance
== -2)
1807 ND_PRINT("\n\t %s", buf
);
1809 case (AFNUM_INET6
<<8 | SAFNUM_UNICAST
):
1810 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST
):
1811 case (AFNUM_INET6
<<8 | SAFNUM_UNIMULTICAST
):
1813 path_id
= GET_BE_U_4(tptr
);
1816 advance
= decode_prefix6(ndo
, tptr
, len
, buf
, buflen
);
1818 ND_PRINT("\n\t (illegal prefix length)");
1819 else if (advance
== -2)
1820 break; /* bytes left, but not enough */
1822 ND_PRINT("\n\t %s", buf
);
1824 ND_PRINT(" Path Id: %u", path_id
);
1828 case (AFNUM_INET6
<<8 | SAFNUM_LABUNICAST
):
1829 advance
= decode_labeled_prefix6(ndo
, tptr
, len
, buf
, buflen
);
1831 ND_PRINT("\n\t (illegal prefix length)");
1832 else if (advance
== -2)
1834 else if (advance
== -3)
1835 break; /* bytes left, but not enough */
1837 ND_PRINT("\n\t %s", buf
);
1839 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNICAST
):
1840 case (AFNUM_INET6
<<8 | SAFNUM_VPNMULTICAST
):
1841 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNIMULTICAST
):
1842 advance
= decode_labeled_vpn_prefix6(ndo
, tptr
, buf
, buflen
);
1844 ND_PRINT("\n\t (illegal prefix length)");
1846 ND_PRINT("\n\t %s", buf
);
1848 case (AFNUM_VPLS
<<8 | SAFNUM_VPLS
):
1849 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNICAST
):
1850 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNMULTICAST
):
1851 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNIMULTICAST
):
1852 advance
= decode_labeled_vpn_l2(ndo
, tptr
, buf
, buflen
);
1854 ND_PRINT("\n\t (illegal length)");
1855 else if (advance
== -2)
1858 ND_PRINT("\n\t %s", buf
);
1860 case (AFNUM_NSAP
<<8 | SAFNUM_UNICAST
):
1861 case (AFNUM_NSAP
<<8 | SAFNUM_MULTICAST
):
1862 case (AFNUM_NSAP
<<8 | SAFNUM_UNIMULTICAST
):
1863 advance
= decode_clnp_prefix(ndo
, tptr
, buf
, buflen
);
1865 ND_PRINT("\n\t (illegal prefix length)");
1867 ND_PRINT("\n\t %s", buf
);
1869 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNICAST
):
1870 case (AFNUM_NSAP
<<8 | SAFNUM_VPNMULTICAST
):
1871 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNIMULTICAST
):
1872 advance
= decode_labeled_vpn_clnp_prefix(ndo
, tptr
, buf
, buflen
);
1874 ND_PRINT("\n\t (illegal prefix length)");
1876 ND_PRINT("\n\t %s", buf
);
1880 * This should not happen, we should have been protected
1881 * by bgp_mp_af_print()'s return value.
1883 ND_PRINT("\n\t ERROR: no AFI %u / SAFI %u decoder", af
, safi
);
1888 trunc
: /* we rely on the caller to recognize -2 return value */
1892 static const struct tok bgp_flags
[] = {
1901 bgp_attr_print(netdissect_options
*ndo
,
1902 uint8_t atype
, const u_char
*pptr
, u_int len
,
1903 const unsigned attr_set_level
)
1905 /* allocate space for the largest possible string */
1906 char astostr
[AS_STR_SIZE
];
1909 uint8_t safi
, snpa
, nhlen
;
1913 char buf
[MAXHOSTNAMELEN
+ 100];
1915 int add_path4
, add_path6
;
1922 case BGPTYPE_ORIGIN
:
1924 ND_PRINT("invalid len");
1926 ND_PRINT("%s", tok2str(bgp_origin_values
,
1927 "Unknown Origin Typecode",
1933 * Process AS4 byte path and AS2 byte path attributes here.
1935 case BGPTYPE_AS4_PATH
:
1936 case BGPTYPE_AS_PATH
:
1938 ND_PRINT("invalid len");
1947 * BGP updates exchanged between New speakers that support 4
1948 * byte AS, ASs are always encoded in 4 bytes. There is no
1949 * definitive way to find this, just by the packet's
1950 * contents. So, check for packet's TLV's sanity assuming
1951 * 2 bytes first, and it does not pass, assume that ASs are
1952 * encoded in 4 bytes format and move on.
1954 as_size
= bgp_attr_get_as_size(ndo
, atype
, pptr
, len
);
1956 while (tptr
< pptr
+ len
) {
1957 ND_PRINT("%s", tok2str(bgp_as_path_segment_open_values
,
1958 "?", GET_U_1(tptr
)));
1959 for (i
= 0; i
< GET_U_1(tptr
+ 1) * as_size
; i
+= as_size
) {
1960 ND_TCHECK_LEN(tptr
+ 2 + i
, as_size
);
1962 as_printf(ndo
, astostr
, sizeof(astostr
),
1964 GET_BE_U_2(tptr
+ i
+ 2) :
1965 GET_BE_U_4(tptr
+ i
+ 2)));
1967 ND_PRINT("%s", tok2str(bgp_as_path_segment_close_values
,
1968 "?", GET_U_1(tptr
)));
1969 tptr
+= 2 + GET_U_1(tptr
+ 1) * as_size
;
1972 case BGPTYPE_NEXT_HOP
:
1974 ND_PRINT("invalid len");
1976 ND_PRINT("%s", GET_IPADDR_STRING(tptr
));
1979 case BGPTYPE_MULTI_EXIT_DISC
:
1980 case BGPTYPE_LOCAL_PREF
:
1982 ND_PRINT("invalid len");
1984 ND_PRINT("%u", GET_BE_U_4(tptr
));
1987 case BGPTYPE_ATOMIC_AGGREGATE
:
1989 ND_PRINT("invalid len");
1991 case BGPTYPE_AGGREGATOR
:
1994 * Depending on the AS encoded is of 2 bytes or of 4 bytes,
1995 * the length of this PA can be either 6 bytes or 8 bytes.
1997 if (len
!= 6 && len
!= 8) {
1998 ND_PRINT("invalid len");
2001 ND_TCHECK_LEN(tptr
, len
);
2003 ND_PRINT(" AS #%s, origin %s",
2004 as_printf(ndo
, astostr
, sizeof(astostr
), GET_BE_U_2(tptr
)),
2005 GET_IPADDR_STRING(tptr
+ 2));
2007 ND_PRINT(" AS #%s, origin %s",
2008 as_printf(ndo
, astostr
, sizeof(astostr
),
2009 GET_BE_U_4(tptr
)), GET_IPADDR_STRING(tptr
+ 4));
2012 case BGPTYPE_AGGREGATOR4
:
2014 ND_PRINT("invalid len");
2017 ND_PRINT(" AS #%s, origin %s",
2018 as_printf(ndo
, astostr
, sizeof(astostr
), GET_BE_U_4(tptr
)),
2019 GET_IPADDR_STRING(tptr
+ 4));
2021 case BGPTYPE_COMMUNITIES
:
2023 ND_PRINT("invalid len");
2031 comm
= GET_BE_U_4(tptr
);
2033 case BGP_COMMUNITY_NO_EXPORT
:
2034 ND_PRINT(" NO_EXPORT");
2036 case BGP_COMMUNITY_NO_ADVERT
:
2037 ND_PRINT(" NO_ADVERTISE");
2039 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED
:
2040 ND_PRINT(" NO_EXPORT_SUBCONFED");
2044 (comm
>> 16) & 0xffff,
2046 (tlen
>4) ? ", " : "");
2053 case BGPTYPE_ORIGINATOR_ID
:
2055 ND_PRINT("invalid len");
2058 ND_PRINT("%s",GET_IPADDR_STRING(tptr
));
2060 case BGPTYPE_CLUSTER_LIST
:
2062 ND_PRINT("invalid len");
2069 GET_IPADDR_STRING(tptr
),
2070 (tlen
>4) ? ", " : "");
2075 case BGPTYPE_MP_REACH_NLRI
:
2079 ret
= bgp_mp_af_print(ndo
, tptr
, tlen
, &af
, &safi
);
2091 nhlen
= GET_U_1(tptr
);
2097 uint8_t tnhlen
= nhlen
;
2100 ND_PRINT("\n\t nexthop: ");
2101 while (tnhlen
!= 0) {
2105 switch(af
<<8 | safi
) {
2106 case (AFNUM_INET
<<8 | SAFNUM_UNICAST
):
2107 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST
):
2108 case (AFNUM_INET
<<8 | SAFNUM_UNIMULTICAST
):
2109 case (AFNUM_INET
<<8 | SAFNUM_LABUNICAST
):
2110 case (AFNUM_INET
<<8 | SAFNUM_RT_ROUTING_INFO
):
2111 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST_VPN
):
2112 case (AFNUM_INET
<<8 | SAFNUM_MDT
):
2113 if (tnhlen
< sizeof(nd_ipv4
)) {
2114 ND_PRINT("invalid len");
2119 ND_PRINT("%s",GET_IPADDR_STRING(tptr
));
2120 tptr
+= sizeof(nd_ipv4
);
2121 tnhlen
-= sizeof(nd_ipv4
);
2122 tlen
-= sizeof(nd_ipv4
);
2125 case (AFNUM_INET
<<8 | SAFNUM_VPNUNICAST
):
2126 case (AFNUM_INET
<<8 | SAFNUM_VPNMULTICAST
):
2127 case (AFNUM_INET
<<8 | SAFNUM_VPNUNIMULTICAST
):
2128 if (tnhlen
< sizeof(nd_ipv4
)+BGP_VPN_RD_LEN
) {
2129 ND_PRINT("invalid len");
2134 ND_PRINT("RD: %s, %s",
2135 bgp_vpn_rd_print(ndo
, tptr
),
2136 GET_IPADDR_STRING(tptr
+BGP_VPN_RD_LEN
));
2137 tptr
+= (sizeof(nd_ipv4
)+BGP_VPN_RD_LEN
);
2138 tlen
-= (sizeof(nd_ipv4
)+BGP_VPN_RD_LEN
);
2139 tnhlen
-= (sizeof(nd_ipv4
)+BGP_VPN_RD_LEN
);
2142 case (AFNUM_INET6
<<8 | SAFNUM_UNICAST
):
2143 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST
):
2144 case (AFNUM_INET6
<<8 | SAFNUM_UNIMULTICAST
):
2145 case (AFNUM_INET6
<<8 | SAFNUM_LABUNICAST
):
2146 if (tnhlen
< sizeof(nd_ipv6
)) {
2147 ND_PRINT("invalid len");
2152 ND_PRINT("%s", GET_IP6ADDR_STRING(tptr
));
2153 tptr
+= sizeof(nd_ipv6
);
2154 tlen
-= sizeof(nd_ipv6
);
2155 tnhlen
-= sizeof(nd_ipv6
);
2158 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNICAST
):
2159 case (AFNUM_INET6
<<8 | SAFNUM_VPNMULTICAST
):
2160 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNIMULTICAST
):
2161 if (tnhlen
< sizeof(nd_ipv6
)+BGP_VPN_RD_LEN
) {
2162 ND_PRINT("invalid len");
2167 ND_PRINT("RD: %s, %s",
2168 bgp_vpn_rd_print(ndo
, tptr
),
2169 GET_IP6ADDR_STRING(tptr
+BGP_VPN_RD_LEN
));
2170 tptr
+= (sizeof(nd_ipv6
)+BGP_VPN_RD_LEN
);
2171 tlen
-= (sizeof(nd_ipv6
)+BGP_VPN_RD_LEN
);
2172 tnhlen
-= (sizeof(nd_ipv6
)+BGP_VPN_RD_LEN
);
2175 case (AFNUM_VPLS
<<8 | SAFNUM_VPLS
):
2176 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNICAST
):
2177 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNMULTICAST
):
2178 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNIMULTICAST
):
2179 if (tnhlen
< sizeof(nd_ipv4
)) {
2180 ND_PRINT("invalid len");
2185 ND_PRINT("%s", GET_IPADDR_STRING(tptr
));
2186 tptr
+= (sizeof(nd_ipv4
));
2187 tlen
-= (sizeof(nd_ipv4
));
2188 tnhlen
-= (sizeof(nd_ipv4
));
2191 case (AFNUM_NSAP
<<8 | SAFNUM_UNICAST
):
2192 case (AFNUM_NSAP
<<8 | SAFNUM_MULTICAST
):
2193 case (AFNUM_NSAP
<<8 | SAFNUM_UNIMULTICAST
):
2194 ND_PRINT("%s", GET_ISONSAP_STRING(tptr
, tnhlen
));
2200 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNICAST
):
2201 case (AFNUM_NSAP
<<8 | SAFNUM_VPNMULTICAST
):
2202 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNIMULTICAST
):
2203 if (tnhlen
< BGP_VPN_RD_LEN
+1) {
2204 ND_PRINT("invalid len");
2209 ND_TCHECK_LEN(tptr
, tnhlen
);
2210 ND_PRINT("RD: %s, %s",
2211 bgp_vpn_rd_print(ndo
, tptr
),
2212 GET_ISONSAP_STRING(tptr
+BGP_VPN_RD_LEN
,tnhlen
-BGP_VPN_RD_LEN
));
2213 /* rfc986 mapped IPv4 address ? */
2214 if (GET_BE_U_4(tptr
+ BGP_VPN_RD_LEN
) == 0x47000601)
2215 ND_PRINT(" = %s", GET_IPADDR_STRING(tptr
+BGP_VPN_RD_LEN
+4));
2216 /* rfc1888 mapped IPv6 address ? */
2217 else if (GET_BE_U_3(tptr
+ BGP_VPN_RD_LEN
) == 0x350000)
2218 ND_PRINT(" = %s", GET_IP6ADDR_STRING(tptr
+BGP_VPN_RD_LEN
+3));
2226 * bgp_mp_af_print() should have saved us from
2227 * an unsupported AFI/SAFI.
2229 ND_PRINT("ERROR: no AFI %u/SAFI %u nexthop decoder", af
, safi
);
2238 ND_PRINT(", nh-length: %u", nhlen
);
2240 /* As per RFC 2858; this is reserved in RFC 4760 */
2243 snpa
= GET_U_1(tptr
);
2248 ND_PRINT("\n\t %u SNPA", snpa
);
2249 for (/*nothing*/; snpa
!= 0; snpa
--) {
2253 snpalen
= GET_U_1(tptr
);
2254 ND_PRINT("\n\t %u bytes", snpalen
);
2259 ND_TCHECK_LEN(tptr
, snpalen
);
2264 ND_PRINT(", no SNPA");
2267 add_path4
= check_add_path(ndo
, tptr
, (len
-ND_BYTES_BETWEEN(tptr
, pptr
)), 32);
2268 add_path6
= check_add_path(ndo
, tptr
, (len
-ND_BYTES_BETWEEN(tptr
, pptr
)), 128);
2270 while (tptr
< pptr
+ len
) {
2271 advance
= bgp_nlri_print(ndo
, af
, safi
, tptr
, len
, buf
, sizeof(buf
),
2272 add_path4
, add_path6
);
2281 case BGPTYPE_MP_UNREACH_NLRI
:
2282 ND_TCHECK_LEN(tptr
, BGP_MP_NLRI_MINSIZE
);
2283 ret
= bgp_mp_af_print(ndo
, tptr
, tlen
, &af
, &safi
);
2289 if (len
== BGP_MP_NLRI_MINSIZE
)
2290 ND_PRINT("\n\t End-of-Rib Marker (empty NLRI)");
2294 add_path4
= check_add_path(ndo
, tptr
, (len
-ND_BYTES_BETWEEN(tptr
, pptr
)), 32);
2295 add_path6
= check_add_path(ndo
, tptr
, (len
-ND_BYTES_BETWEEN(tptr
, pptr
)), 128);
2297 while (tptr
< pptr
+ len
) {
2298 advance
= bgp_nlri_print(ndo
, af
, safi
, tptr
, len
, buf
, sizeof(buf
),
2299 add_path4
, add_path6
);
2307 case BGPTYPE_EXTD_COMMUNITIES
:
2309 ND_PRINT("invalid len");
2318 extd_comm
=GET_BE_U_2(tptr
);
2320 ND_PRINT("\n\t %s (0x%04x), Flags [%s]",
2321 tok2str(bgp_extd_comm_subtype_values
,
2322 "unknown extd community typecode",
2325 bittok2str(bgp_extd_comm_flag_values
, "none", extd_comm
));
2331 bgp_extended_community_print(ndo
, tptr
);
2337 case BGPTYPE_PMSI_TUNNEL
:
2339 uint8_t tunnel_type
, flags
;
2344 flags
= GET_U_1(tptr
);
2345 tunnel_type
= GET_U_1(tptr
+ 1);
2347 ND_PRINT("\n\t Tunnel-type %s (%u), Flags [%s], MPLS Label %u",
2348 tok2str(bgp_pmsi_tunnel_values
, "Unknown", tunnel_type
),
2350 bittok2str(bgp_pmsi_flag_values
, "none", flags
),
2351 GET_BE_U_3(tptr
+ 2)>>4);
2356 switch (tunnel_type
) {
2357 case BGP_PMSI_TUNNEL_PIM_SM
: /* fall through */
2358 case BGP_PMSI_TUNNEL_PIM_BIDIR
:
2359 ND_PRINT("\n\t Sender %s, P-Group %s",
2360 GET_IPADDR_STRING(tptr
),
2361 GET_IPADDR_STRING(tptr
+4));
2364 case BGP_PMSI_TUNNEL_PIM_SSM
:
2365 ND_PRINT("\n\t Root-Node %s, P-Group %s",
2366 GET_IPADDR_STRING(tptr
),
2367 GET_IPADDR_STRING(tptr
+4));
2369 case BGP_PMSI_TUNNEL_INGRESS
:
2370 ND_PRINT("\n\t Tunnel-Endpoint %s",
2371 GET_IPADDR_STRING(tptr
));
2373 case BGP_PMSI_TUNNEL_LDP_P2MP
: /* fall through */
2374 case BGP_PMSI_TUNNEL_LDP_MP2MP
:
2375 ND_PRINT("\n\t Root-Node %s, LSP-ID 0x%08x",
2376 GET_IPADDR_STRING(tptr
),
2377 GET_BE_U_4(tptr
+ 4));
2379 case BGP_PMSI_TUNNEL_RSVP_P2MP
:
2380 ND_PRINT("\n\t Extended-Tunnel-ID %s, P2MP-ID 0x%08x",
2381 GET_IPADDR_STRING(tptr
),
2382 GET_BE_U_4(tptr
+ 4));
2385 if (ndo
->ndo_vflag
<= 1) {
2386 print_unknown_data(ndo
, tptr
, "\n\t ", tlen
);
2397 type
= GET_U_1(tptr
);
2398 length
= GET_BE_U_2(tptr
+ 1);
2402 ND_PRINT("\n\t %s TLV (%u), length %u",
2403 tok2str(bgp_aigp_values
, "Unknown", type
),
2411 * Check if we can read the TLV data.
2421 ND_PRINT(", metric %" PRIu64
,
2426 if (ndo
->ndo_vflag
<= 1) {
2427 print_unknown_data(ndo
, tptr
,"\n\t ", length
);
2436 case BGPTYPE_ATTR_SET
:
2440 ND_PRINT("\n\t Origin AS: %s",
2441 as_printf(ndo
, astostr
, sizeof(astostr
), GET_BE_U_4(tptr
)));
2446 u_int aflags
, alenlen
, alen
;
2450 ND_PRINT(" [path attr too short]");
2454 aflags
= GET_U_1(tptr
);
2455 atype
= GET_U_1(tptr
+ 1);
2458 alenlen
= bgp_attr_lenlen(aflags
, tptr
);
2459 ND_TCHECK_LEN(tptr
, alenlen
);
2460 if (len
< alenlen
) {
2461 ND_PRINT(" [path attr too short]");
2465 alen
= bgp_attr_len(aflags
, tptr
);
2469 ND_PRINT("\n\t %s (%u), length: %u",
2470 tok2str(bgp_attr_values
,
2471 "Unknown Attribute", atype
),
2476 ND_PRINT(", Flags [%s",
2477 bittok2str_nosep(bgp_flags
, "", aflags
));
2479 ND_PRINT("+%x", aflags
& 0xf);
2484 ND_PRINT(" [path attr too short]");
2489 * The protocol encoding per se allows ATTR_SET to be nested
2490 * as many times as the message can accommodate. This printer
2491 * used to be able to recurse into ATTR_SET contents until the
2492 * stack exhaustion, but now there is a limit on that (if live
2493 * protocol exchange goes that many levels deep, something is
2494 * probably wrong anyway). Feel free to refine this value if
2495 * you can find the spec with respective normative text.
2497 if (attr_set_level
== 10)
2498 ND_PRINT("(too many nested levels, not recursing)");
2499 else if (!bgp_attr_print(ndo
, atype
, tptr
, alen
, attr_set_level
+ 1))
2506 case BGPTYPE_LARGE_COMMUNITY
:
2507 if (len
== 0 || len
% 12) {
2508 ND_PRINT("invalid len");
2513 ND_PRINT("%u:%u:%u%s",
2515 GET_BE_U_4(tptr
+ 4),
2516 GET_BE_U_4(tptr
+ 8),
2517 (len
> 12) ? ", " : "");
2520 * len will always be a multiple of 12, as per the above,
2521 * so this will never underflow.
2527 ND_TCHECK_LEN(pptr
, len
);
2528 ND_PRINT("\n\t no Attribute %u decoder", atype
); /* we have no decoder for the attribute */
2529 if (ndo
->ndo_vflag
<= 1)
2530 print_unknown_data(ndo
, pptr
, "\n\t ", len
);
2534 if (ndo
->ndo_vflag
> 1 && len
) { /* omit zero length attributes*/
2535 ND_TCHECK_LEN(pptr
, len
);
2536 print_unknown_data(ndo
, pptr
, "\n\t ", len
);
2545 bgp_capabilities_print(netdissect_options
*ndo
,
2546 const u_char
*opt
, u_int caps_len
)
2548 /* allocate space for the largest possible string */
2549 char astostr
[AS_STR_SIZE
];
2550 u_int cap_type
, cap_len
, tcap_len
, cap_offset
;
2553 while (i
< caps_len
) {
2554 ND_TCHECK_LEN(opt
+ i
, BGP_CAP_HEADER_SIZE
);
2555 cap_type
=GET_U_1(opt
+ i
);
2556 cap_len
=GET_U_1(opt
+ i
+ 1);
2557 ND_PRINT("\n\t %s (%u), length: %u",
2558 tok2str(bgp_capcode_values
, "Unknown", cap_type
),
2561 ND_TCHECK_LEN(opt
+ 2 + i
, cap_len
);
2563 case BGP_CAPCODE_MP
:
2564 /* AFI (16 bits), Reserved (8 bits), SAFI (8 bits) */
2566 ND_PRINT(" (too short, < 4)");
2569 ND_PRINT("\n\t\tAFI %s (%u), SAFI %s (%u)",
2570 tok2str(af_values
, "Unknown", GET_BE_U_2(opt
+ i
+ 2)),
2571 GET_BE_U_2(opt
+ i
+ 2),
2572 tok2str(bgp_safi_values
, "Unknown", GET_U_1(opt
+ i
+ 5)),
2573 GET_U_1(opt
+ i
+ 5));
2575 case BGP_CAPCODE_ML
:
2578 while (tcap_len
>= 4) {
2579 ND_PRINT( "\n\t\tAFI %s (%u), SAFI %s (%u), Count: %u",
2580 tok2str(af_values
, "Unknown",
2581 GET_BE_U_2(opt
+ i
+ cap_offset
)),
2582 GET_BE_U_2(opt
+ i
+ cap_offset
),
2583 tok2str(bgp_safi_values
, "Unknown",
2584 GET_U_1(opt
+ i
+ cap_offset
+ 2)),
2585 GET_U_1(opt
+ i
+ cap_offset
+ 2),
2586 GET_U_1(opt
+ i
+ cap_offset
+ 3));
2591 case BGP_CAPCODE_RESTART
:
2592 /* Restart Flags (4 bits), Restart Time in seconds (12 bits) */
2594 ND_PRINT(" (too short, < 2)");
2598 ND_PRINT("\n\t\tRestart Flags: [%s], Restart Time %us",
2599 bittok2str(bgp_graceful_restart_comm_flag_values
,
2600 "none", GET_U_1(opt
+ i
+ 2) >> 4),
2601 GET_BE_U_2(opt
+ i
+ 2)&0xfff);
2604 while(tcap_len
>=4) {
2605 ND_PRINT("\n\t\t AFI %s (%u), SAFI %s (%u), Forwarding state preserved: %s",
2606 tok2str(af_values
,"Unknown",
2607 GET_BE_U_2(opt
+ i
+ cap_offset
)),
2608 GET_BE_U_2(opt
+ i
+ cap_offset
),
2609 tok2str(bgp_safi_values
,"Unknown",
2610 GET_U_1(opt
+ i
+ cap_offset
+ 2)),
2611 GET_U_1(opt
+ (i
+ cap_offset
+ 2)),
2612 ((GET_U_1(opt
+ (i
+ cap_offset
+ 3)))&0x80) ? "yes" : "no" );
2617 case BGP_CAPCODE_RR
:
2618 case BGP_CAPCODE_LLGR
:
2619 case BGP_CAPCODE_RR_CISCO
:
2620 case BGP_CAPCODE_EXT_MSG
:
2621 case BGP_CAPCODE_ENH_RR
:
2623 case BGP_CAPCODE_AS_NEW
:
2625 * Extract the 4 byte AS number encoded.
2628 ND_PRINT(" (too short, < 4)");
2631 ND_PRINT("\n\t\t 4 Byte AS %s",
2632 as_printf(ndo
, astostr
, sizeof(astostr
),
2633 GET_BE_U_4(opt
+ i
+ 2)));
2635 case BGP_CAPCODE_ADD_PATH
:
2637 ND_PRINT(" (bogus)"); /* length */
2642 while (tcap_len
!= 0) {
2644 nd_print_invalid(ndo
);
2647 ND_PRINT("\n\t\tAFI %s (%u), SAFI %s (%u), Send/Receive: %s",
2648 tok2str(af_values
,"Unknown",GET_BE_U_2(opt
+ i
+ cap_offset
)),
2649 GET_BE_U_2(opt
+ i
+ cap_offset
),
2650 tok2str(bgp_safi_values
,"Unknown",GET_U_1(opt
+ i
+ cap_offset
+ 2)),
2651 GET_U_1(opt
+ (i
+ cap_offset
+ 2)),
2652 tok2str(bgp_add_path_recvsend
,"Bogus (0x%02x)",GET_U_1(opt
+ i
+ cap_offset
+ 3))
2659 ND_PRINT("\n\t\tno decoder for Capability %u",
2661 if (ndo
->ndo_vflag
<= 1)
2662 print_unknown_data(ndo
, opt
+ i
+ 2, "\n\t\t",
2666 if (ndo
->ndo_vflag
> 1 && cap_len
!= 0) {
2667 print_unknown_data(ndo
, opt
+ i
+ 2, "\n\t\t", cap_len
);
2669 i
+= BGP_CAP_HEADER_SIZE
+ cap_len
;
2674 nd_print_trunc(ndo
);
2678 bgp_open_print(netdissect_options
*ndo
,
2679 const u_char
*dat
, u_int length
)
2681 /* allocate space for the largest possible string */
2682 char astostr
[AS_STR_SIZE
];
2683 const struct bgp_open
*bgp_open_header
;
2686 const struct bgp_opt
*bgpopt
;
2689 uint8_t extended_opt_params
= 0;
2690 u_int open_size
= BGP_OPEN_SIZE
;
2691 u_int opt_size
= BGP_OPT_SIZE
;
2693 ND_TCHECK_LEN(dat
, BGP_OPEN_SIZE
);
2694 if (length
< BGP_OPEN_SIZE
)
2697 bgp_open_header
= (const struct bgp_open
*)dat
;
2699 ND_PRINT("\n\t Version %u, ",
2700 GET_U_1(bgp_open_header
->bgpo_version
));
2701 ND_PRINT("my AS %s, ",
2702 as_printf(ndo
, astostr
, sizeof(astostr
), GET_BE_U_2(bgp_open_header
->bgpo_myas
)));
2703 ND_PRINT("Holdtime %us, ",
2704 GET_BE_U_2(bgp_open_header
->bgpo_holdtime
));
2705 ND_PRINT("ID %s", GET_IPADDR_STRING(bgp_open_header
->bgpo_id
));
2706 optslen
= GET_U_1(bgp_open_header
->bgpo_optlen
);
2707 opsttype
= GET_U_1(bgp_open_header
->bgpo_opttype
);
2708 if (opsttype
== BGP_OPEN_NON_EXT_OPT_TYPE_EXTENDED_LENGTH
) {
2709 optslen
= GET_BE_U_2(bgp_open_header
->bgpo_optlen_extended
);
2710 extended_opt_params
= 1;
2714 ND_PRINT("\n\t Optional parameters%s, length: %u",
2715 extended_opt_params
? " (Extended)" : "", optslen
);
2717 opt
= dat
+ open_size
;
2718 length
-= open_size
;
2721 while (i
< optslen
) {
2725 ND_TCHECK_LEN(opt
+ i
, opt_size
);
2726 if (length
< opt_size
+ i
)
2728 bgpopt
= (const struct bgp_opt
*)(opt
+ i
);
2729 opt_type
= GET_U_1(bgpopt
->bgpopt_type
);
2730 opt_len
= extended_opt_params
? GET_BE_U_2(bgpopt
->bgpopt_len
)
2731 : GET_U_1(bgpopt
->bgpopt_len
);
2732 if (opt_size
+ i
+ opt_len
> optslen
) {
2733 ND_PRINT("\n\t Option %u, length: %u, goes past the end of the options",
2738 ND_PRINT("\n\t Option %s (%u), length: %u",
2739 tok2str(bgp_opt_values
,"Unknown",opt_type
),
2743 /* now let's decode the options we know*/
2747 bgp_capabilities_print(ndo
, opt
+ opt_size
+ i
,
2753 ND_PRINT("\n\t no decoder for option %u",
2757 i
+= opt_size
+ opt_len
;
2761 nd_print_trunc(ndo
);
2765 bgp_update_print(netdissect_options
*ndo
,
2766 const u_char
*dat
, u_int length
)
2769 u_int withdrawn_routes_len
;
2770 char buf
[MAXHOSTNAMELEN
+ 100];
2777 ND_TCHECK_LEN(dat
, BGP_SIZE
);
2778 if (length
< BGP_SIZE
)
2783 /* Unfeasible routes */
2787 withdrawn_routes_len
= GET_BE_U_2(p
);
2790 if (withdrawn_routes_len
> 1) {
2792 * Without keeping state from the original NLRI message,
2793 * it's not possible to tell if this a v4 or v6 route,
2794 * so only try to decode it if we're not v6 enabled.
2796 ND_TCHECK_LEN(p
, withdrawn_routes_len
);
2797 if (length
< withdrawn_routes_len
)
2799 ND_PRINT("\n\t Withdrawn routes:");
2800 add_path
= check_add_path(ndo
, p
, withdrawn_routes_len
, 32);
2801 while (withdrawn_routes_len
!= 0) {
2803 if (withdrawn_routes_len
< 4) {
2804 p
+= withdrawn_routes_len
;
2805 length
-= withdrawn_routes_len
;
2808 path_id
= GET_BE_U_4(p
);
2811 withdrawn_routes_len
-= 4;
2813 wpfx
= decode_prefix4(ndo
, p
, withdrawn_routes_len
, buf
, sizeof(buf
));
2815 ND_PRINT("\n\t (illegal prefix length)");
2817 } else if (wpfx
== -2)
2818 goto trunc
; /* bytes left, but not enough */
2820 ND_PRINT("\n\t %s", buf
);
2822 ND_PRINT(" Path Id: %u", path_id
);
2826 withdrawn_routes_len
-= wpfx
;
2830 ND_TCHECK_LEN(p
, withdrawn_routes_len
);
2831 if (length
< withdrawn_routes_len
)
2833 p
+= withdrawn_routes_len
;
2834 length
-= withdrawn_routes_len
;
2840 len
= GET_BE_U_2(p
);
2844 if (withdrawn_routes_len
== 0 && len
== 0 && length
== 0) {
2845 /* No withdrawn routes, no path attributes, no NLRI */
2846 ND_PRINT("\n\t End-of-Rib Marker (empty NLRI)");
2851 /* Make sure the path attributes don't go past the end of the packet */
2854 /* do something more useful!*/
2856 uint8_t aflags
, atype
, alenlen
;
2863 ND_PRINT("\n\t [path attrs too short]");
2868 aflags
= GET_U_1(p
);
2869 atype
= GET_U_1(p
+ 1);
2873 alenlen
= bgp_attr_lenlen(aflags
, p
);
2874 ND_TCHECK_LEN(p
, alenlen
);
2875 if (length
< alenlen
)
2877 if (len
< alenlen
) {
2878 ND_PRINT("\n\t [path attrs too short]");
2883 alen
= bgp_attr_len(aflags
, p
);
2888 ND_PRINT("\n\t %s (%u), length: %u",
2889 tok2str(bgp_attr_values
, "Unknown Attribute", atype
),
2894 ND_PRINT(", Flags [%s",
2895 bittok2str_nosep(bgp_flags
, "", aflags
));
2897 ND_PRINT("+%x", aflags
& 0xf);
2901 ND_PRINT(" [path attrs too short]");
2908 if (!bgp_attr_print(ndo
, atype
, p
, alen
, 0))
2917 add_path
= check_add_path(ndo
, p
, length
, 32);
2918 ND_PRINT("\n\t Updated routes:");
2919 while (length
!= 0) {
2924 path_id
= GET_BE_U_4(p
);
2928 i
= decode_prefix4(ndo
, p
, length
, buf
, sizeof(buf
));
2930 ND_PRINT("\n\t (illegal prefix length)");
2933 goto trunc
; /* bytes left, but not enough */
2935 ND_PRINT("\n\t %s", buf
);
2937 ND_PRINT(" Path Id: %u", path_id
);
2946 nd_print_trunc(ndo
);
2950 bgp_notification_print_code(netdissect_options
*ndo
,
2951 const u_char
*dat
, u_int length
,
2952 uint8_t bgpn_major
, uint8_t bgpn_minor
)
2954 ND_PRINT(", %s (%u)",
2955 tok2str(bgp_notify_major_values
, "Unknown Error",
2959 switch (bgpn_major
) {
2961 case BGP_NOTIFY_MAJOR_MSG
:
2962 ND_PRINT(", subcode %s (%u)",
2963 tok2str(bgp_notify_minor_msg_values
, "Unknown",
2967 case BGP_NOTIFY_MAJOR_OPEN
:
2968 ND_PRINT(", subcode %s (%u)",
2969 tok2str(bgp_notify_minor_open_values
, "Unknown",
2973 case BGP_NOTIFY_MAJOR_UPDATE
:
2974 ND_PRINT(", subcode %s (%u)",
2975 tok2str(bgp_notify_minor_update_values
, "Unknown",
2979 case BGP_NOTIFY_MAJOR_FSM
:
2980 ND_PRINT(" subcode %s (%u)",
2981 tok2str(bgp_notify_minor_fsm_values
, "Unknown",
2985 case BGP_NOTIFY_MAJOR_CAP
:
2986 ND_PRINT(" subcode %s (%u)",
2987 tok2str(bgp_notify_minor_cap_values
, "Unknown",
2991 case BGP_NOTIFY_MAJOR_CEASE
:
2992 ND_PRINT(", subcode %s (%u)",
2993 tok2str(bgp_notify_minor_cease_values
, "Unknown",
2997 /* RFC 4486 mentions optionally 7 bytes
2998 * for the maxprefix subtype, which may contain AFI, SAFI and MAXPREFIXES
3000 if(bgpn_minor
== BGP_NOTIFY_MINOR_CEASE_MAXPRFX
&& length
>= 7) {
3001 ND_PRINT(", AFI %s (%u), SAFI %s (%u), Max Prefixes: %u",
3002 tok2str(af_values
, "Unknown", GET_BE_U_2(dat
)),
3004 tok2str(bgp_safi_values
, "Unknown", GET_U_1((dat
+ 2))),
3006 GET_BE_U_4(dat
+ 3));
3009 * RFC 9003 describes a method to send a communication
3010 * intended for human consumption regarding the Administrative Shutdown
3012 if ((bgpn_minor
== BGP_NOTIFY_MINOR_CEASE_SHUT
||
3013 bgpn_minor
== BGP_NOTIFY_MINOR_CEASE_RESET
) &&
3015 uint8_t shutdown_comm_length
= GET_U_1(dat
);
3016 uint8_t remainder_offset
= 0;
3017 /* garbage, hexdump it all */
3018 if (shutdown_comm_length
> length
- 1) {
3019 ND_PRINT(", invalid Shutdown Communication length");
3021 else if (shutdown_comm_length
== 0) {
3022 ND_PRINT(", empty Shutdown Communication");
3023 remainder_offset
+= 1;
3025 /* a proper shutdown communication */
3027 ND_PRINT(", Shutdown Communication (length: %u): \"", shutdown_comm_length
);
3028 nd_printjn(ndo
, dat
+1, shutdown_comm_length
);
3030 remainder_offset
+= shutdown_comm_length
+ 1;
3032 /* if there is trailing data, hexdump it */
3033 if(length
- remainder_offset
> 0) {
3034 ND_PRINT(", Data: (length: %u)", length
- remainder_offset
);
3035 hex_print(ndo
, "\n\t\t", dat
+ remainder_offset
, length
- remainder_offset
);
3039 * RFC8538 describes the Hard Reset cease subcode, which contains another
3040 * notification code and subcode.
3042 if (bgpn_minor
== BGP_NOTIFY_MINOR_CEASE_HARDRESET
&& length
>= 2) {
3043 bgpn_major
= GET_U_1(dat
++);
3044 bgpn_minor
= GET_U_1(dat
++);
3046 bgp_notification_print_code(ndo
, dat
, length
, bgpn_major
, bgpn_minor
);
3050 if (bgpn_minor
!= 0) {
3051 ND_PRINT(", subcode %u", bgpn_minor
);
3060 bgp_notification_print(netdissect_options
*ndo
,
3061 const u_char
*dat
, u_int length
)
3063 const struct bgp_notification
*bgp_notification_header
;
3064 uint8_t bgpn_major
, bgpn_minor
;
3066 ND_TCHECK_LEN(dat
, BGP_NOTIFICATION_SIZE
);
3067 if (length
<BGP_NOTIFICATION_SIZE
)
3070 bgp_notification_header
= (const struct bgp_notification
*)dat
;
3071 bgpn_major
= GET_U_1(bgp_notification_header
->bgpn_major
);
3072 bgpn_minor
= GET_U_1(bgp_notification_header
->bgpn_minor
);
3073 bgp_notification_print_code(ndo
, dat
+ BGP_NOTIFICATION_SIZE
,
3074 length
- BGP_NOTIFICATION_SIZE
, bgpn_major
, bgpn_minor
);
3077 nd_print_trunc(ndo
);
3081 bgp_route_refresh_print(netdissect_options
*ndo
,
3082 const u_char
*pptr
, u_int len
)
3084 const struct bgp_route_refresh
*bgp_route_refresh_header
;
3086 ND_TCHECK_LEN(pptr
, BGP_ROUTE_REFRESH_SIZE
);
3088 /* some little sanity checking */
3089 if (len
<BGP_ROUTE_REFRESH_SIZE
)
3092 bgp_route_refresh_header
= (const struct bgp_route_refresh
*)pptr
;
3094 ND_PRINT("\n\t AFI %s (%u), SAFI %s (%u), Subtype %s (%u)",
3095 tok2str(af_values
, "Unknown",
3096 GET_BE_U_2(bgp_route_refresh_header
->afi
)),
3097 GET_BE_U_2(bgp_route_refresh_header
->afi
),
3098 tok2str(bgp_safi_values
, "Unknown",
3099 GET_U_1(bgp_route_refresh_header
->safi
)),
3100 GET_U_1(bgp_route_refresh_header
->safi
),
3101 tok2str(bgp_route_refresh_subtype_values
, "Unknown",
3102 GET_U_1(bgp_route_refresh_header
->subtype
)),
3103 GET_U_1(bgp_route_refresh_header
->subtype
));
3105 if (ndo
->ndo_vflag
> 1) {
3106 ND_TCHECK_LEN(pptr
, len
);
3107 print_unknown_data(ndo
, pptr
, "\n\t ", len
);
3112 nd_print_trunc(ndo
);
3116 bgp_pdu_print(netdissect_options
*ndo
,
3117 const u_char
*dat
, u_int length
)
3119 const struct bgp
*bgp_header
;
3122 ND_TCHECK_LEN(dat
, BGP_SIZE
);
3123 bgp_header
= (const struct bgp
*)dat
;
3124 bgp_type
= GET_U_1(bgp_header
->bgp_type
);
3126 ND_PRINT("\n\t%s Message (%u), length: %u",
3127 tok2str(bgp_msg_values
, "Unknown", bgp_type
),
3133 bgp_open_print(ndo
, dat
, length
);
3136 bgp_update_print(ndo
, dat
, length
);
3138 case BGP_NOTIFICATION
:
3139 bgp_notification_print(ndo
, dat
, length
);
3143 case BGP_ROUTE_REFRESH
:
3144 bgp_route_refresh_print(ndo
, dat
, length
);
3147 /* we have no decoder for the BGP message */
3148 ND_TCHECK_LEN(dat
, length
);
3149 ND_PRINT("\n\t no Message %u decoder", bgp_type
);
3150 print_unknown_data(ndo
, dat
, "\n\t ", length
);
3155 nd_print_trunc(ndo
);
3160 bgp_print(netdissect_options
*ndo
,
3161 const u_char
*dat
, u_int length _U_
)
3164 const u_char
*ep
= ndo
->ndo_snapend
;
3165 const u_char
*start
;
3166 const u_char marker
[] = {
3167 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
3168 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
3170 const struct bgp
*bgp_header
;
3173 ndo
->ndo_protocol
= "bgp";
3176 if (ndo
->ndo_vflag
< 1) /* lets be less chatty */
3184 if (GET_U_1(p
) != 0xff) {
3189 if (!ND_TTEST_LEN(p
, sizeof(marker
)))
3191 if (memcmp(p
, marker
, sizeof(marker
)) != 0) {
3196 /* found BGP header */
3197 ND_TCHECK_LEN(p
, BGP_SIZE
);
3198 bgp_header
= (const struct bgp
*)p
;
3201 nd_print_trunc(ndo
);
3203 hlen
= GET_BE_U_2(bgp_header
->bgp_len
);
3204 if (hlen
< BGP_SIZE
) {
3205 ND_PRINT("\nmessage length %u < %u", hlen
, BGP_SIZE
);
3206 nd_print_invalid(ndo
);
3210 if (ND_TTEST_LEN(p
, hlen
)) {
3211 if (!bgp_pdu_print(ndo
, p
, hlen
))
3216 ND_PRINT("\n[|BGP %s]",
3217 tok2str(bgp_msg_values
,
3218 "Unknown Message Type",
3219 GET_U_1(bgp_header
->bgp_type
)));
3227 nd_print_trunc(ndo
);