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_orf
{
112 struct bgp_route_refresh
{
113 nd_byte bgp_marker
[16];
115 nd_uint8_t type
; /* No padding after this; afi is, in fact, not aligned */
121 static const struct tok bgp_orf_refresh_type
[] = {
127 static const struct tok bgp_orf_type
[] = {
128 { 64, "Address Prefix ORF"},
133 #define BGP_ROUTE_REFRESH_SIZE 23
134 #define BGP_ROUTE_REFRESH_SIZE_ORF BGP_ROUTE_REFRESH_SIZE + 5
135 #define BGP_ROUTE_REFRESH_SUBTYPE_NORMAL 0
136 #define BGP_ROUTE_REFRESH_SUBTYPE_BORR 1
137 #define BGP_ROUTE_REFRESH_SUBTYPE_EORR 2
138 #define BGP_ROUTE_REFRESH_SUBTYPE_RESERVED 255
140 static const struct tok bgp_route_refresh_subtype_values
[] = {
141 {BGP_ROUTE_REFRESH_SUBTYPE_NORMAL
, "Normal route refresh request"},
142 {BGP_ROUTE_REFRESH_SUBTYPE_BORR
,
143 "Demarcation of the beginning of a route refresh"},
144 {BGP_ROUTE_REFRESH_SUBTYPE_EORR
,
145 "Demarcation of the ending of a route refresh"},
148 #define bgp_attr_lenlen(flags, p) \
149 (((flags) & 0x10) ? 2U : 1U)
150 #define bgp_attr_len(flags, p) \
151 (((flags) & 0x10) ? GET_BE_U_2(p) : GET_U_1(p))
153 #define BGPTYPE_ORIGIN 1
154 #define BGPTYPE_AS_PATH 2
155 #define BGPTYPE_NEXT_HOP 3
156 #define BGPTYPE_MULTI_EXIT_DISC 4
157 #define BGPTYPE_LOCAL_PREF 5
158 #define BGPTYPE_ATOMIC_AGGREGATE 6
159 #define BGPTYPE_AGGREGATOR 7
160 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
161 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC4456 */
162 #define BGPTYPE_CLUSTER_LIST 10 /* RFC4456 */
163 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC4760 */
164 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC4760 */
165 #define BGPTYPE_EXTD_COMMUNITIES 16 /* RFC4360 */
166 #define BGPTYPE_AS4_PATH 17 /* RFC6793 */
167 #define BGPTYPE_AGGREGATOR4 18 /* RFC6793 */
168 #define BGPTYPE_PMSI_TUNNEL 22 /* RFC6514 */
169 #define BGPTYPE_TUNNEL_ENCAP 23 /* RFC5512 */
170 #define BGPTYPE_TRAFFIC_ENG 24 /* RFC5543 */
171 #define BGPTYPE_IPV6_EXTD_COMMUNITIES 25 /* RFC5701 */
172 #define BGPTYPE_AIGP 26 /* RFC7311 */
173 #define BGPTYPE_PE_DISTINGUISHER_LABEL 27 /* RFC6514 */
174 #define BGPTYPE_ENTROPY_LABEL 28 /* RFC6790 */
175 #define BGPTYPE_LARGE_COMMUNITY 32 /* draft-ietf-idr-large-community-05 */
176 #define BGPTYPE_ATTR_SET 128 /* RFC6368 */
178 #define BGP_MP_NLRI_MINSIZE 3 /* End of RIB Marker detection */
180 static const struct tok bgp_attr_values
[] = {
181 { BGPTYPE_ORIGIN
, "Origin"},
182 { BGPTYPE_AS_PATH
, "AS Path"},
183 { BGPTYPE_AS4_PATH
, "AS4 Path"},
184 { BGPTYPE_NEXT_HOP
, "Next Hop"},
185 { BGPTYPE_MULTI_EXIT_DISC
, "Multi Exit Discriminator"},
186 { BGPTYPE_LOCAL_PREF
, "Local Preference"},
187 { BGPTYPE_ATOMIC_AGGREGATE
, "Atomic Aggregate"},
188 { BGPTYPE_AGGREGATOR
, "Aggregator"},
189 { BGPTYPE_AGGREGATOR4
, "Aggregator4"},
190 { BGPTYPE_COMMUNITIES
, "Community"},
191 { BGPTYPE_ORIGINATOR_ID
, "Originator ID"},
192 { BGPTYPE_CLUSTER_LIST
, "Cluster List"},
193 { BGPTYPE_MP_REACH_NLRI
, "Multi-Protocol Reach NLRI"},
194 { BGPTYPE_MP_UNREACH_NLRI
, "Multi-Protocol Unreach NLRI"},
195 { BGPTYPE_EXTD_COMMUNITIES
, "Extended Community"},
196 { BGPTYPE_PMSI_TUNNEL
, "PMSI Tunnel"},
197 { BGPTYPE_TUNNEL_ENCAP
, "Tunnel Encapsulation"},
198 { BGPTYPE_TRAFFIC_ENG
, "Traffic Engineering"},
199 { BGPTYPE_IPV6_EXTD_COMMUNITIES
, "IPv6 Extended Community"},
200 { BGPTYPE_AIGP
, "Accumulated IGP Metric"},
201 { BGPTYPE_PE_DISTINGUISHER_LABEL
, "PE Distinguisher Label"},
202 { BGPTYPE_ENTROPY_LABEL
, "Entropy Label"},
203 { BGPTYPE_LARGE_COMMUNITY
, "Large Community"},
204 { BGPTYPE_ATTR_SET
, "Attribute Set"},
205 { 255, "Reserved for development"},
210 #define BGP_AS_SEQUENCE 2
211 #define BGP_CONFED_AS_SEQUENCE 3 /* draft-ietf-idr-rfc3065bis-01 */
212 #define BGP_CONFED_AS_SET 4 /* draft-ietf-idr-rfc3065bis-01 */
214 #define BGP_AS_SEG_TYPE_MIN BGP_AS_SET
215 #define BGP_AS_SEG_TYPE_MAX BGP_CONFED_AS_SET
217 static const struct tok bgp_as_path_segment_open_values
[] = {
218 { BGP_AS_SEQUENCE
, ""},
220 { BGP_CONFED_AS_SEQUENCE
, "( "},
221 { BGP_CONFED_AS_SET
, "({ "},
225 static const struct tok bgp_as_path_segment_close_values
[] = {
226 { BGP_AS_SEQUENCE
, ""},
228 { BGP_CONFED_AS_SEQUENCE
, ")"},
229 { BGP_CONFED_AS_SET
, "})"},
233 #define BGP_OPT_AUTH 1
234 #define BGP_OPT_CAP 2
236 static const struct tok bgp_opt_values
[] = {
237 { BGP_OPT_AUTH
, "Authentication Information"},
238 { BGP_OPT_CAP
, "Capabilities Advertisement"},
242 #define BGP_CAPCODE_MP 1 /* RFC2858 */
243 #define BGP_CAPCODE_RR 2 /* RFC2918 */
244 #define BGP_CAPCODE_ORF 3 /* RFC5291 */
245 #define BGP_CAPCODE_MR 4 /* RFC3107 */
246 #define BGP_CAPCODE_EXT_NH 5 /* RFC5549 */
247 #define BGP_CAPCODE_EXT_MSG 6 /* RFC8654 */
248 #define BGP_CAPCODE_ML 8 /* RFC8277 */
249 #define BGP_CAPCODE_RESTART 64 /* RFC4724 */
250 #define BGP_CAPCODE_AS_NEW 65 /* RFC6793 */
251 #define BGP_CAPCODE_DYN_CAP 67 /* draft-ietf-idr-dynamic-cap */
252 #define BGP_CAPCODE_MULTISESS 68 /* draft-ietf-idr-bgp-multisession */
253 #define BGP_CAPCODE_ADD_PATH 69 /* RFC7911 */
254 #define BGP_CAPCODE_ENH_RR 70 /* RFC7313 */
255 #define BGP_CAPCODE_LLGR 71 /* draft-uttaro-idr-bgp-persistence-05 */
256 #define BGP_CAPCODE_RR_CISCO 128
258 static const struct tok bgp_capcode_values
[] = {
259 { BGP_CAPCODE_MP
, "Multiprotocol Extensions"},
260 { BGP_CAPCODE_RR
, "Route Refresh"},
261 { BGP_CAPCODE_ORF
, "Cooperative Route Filtering"},
262 { BGP_CAPCODE_MR
, "Multiple Routes to a Destination"},
263 { BGP_CAPCODE_EXT_NH
, "Extended Next Hop Encoding"},
264 { BGP_CAPCODE_EXT_MSG
, "BGP Extended Message"},
265 { BGP_CAPCODE_ML
, "Multiple Labels"},
266 { BGP_CAPCODE_RESTART
, "Graceful Restart"},
267 { BGP_CAPCODE_AS_NEW
, "32-Bit AS Number"},
268 { BGP_CAPCODE_DYN_CAP
, "Dynamic Capability"},
269 { BGP_CAPCODE_MULTISESS
, "Multisession BGP"},
270 { BGP_CAPCODE_ADD_PATH
, "Multiple Paths"},
271 { BGP_CAPCODE_ENH_RR
, "Enhanced Route Refresh"},
272 { BGP_CAPCODE_LLGR
, "Long-lived Graceful Restart"},
273 { BGP_CAPCODE_RR_CISCO
, "Route Refresh (Cisco)"},
277 #define BGP_NOTIFY_MAJOR_MSG 1
278 #define BGP_NOTIFY_MAJOR_OPEN 2
279 #define BGP_NOTIFY_MAJOR_UPDATE 3
280 #define BGP_NOTIFY_MAJOR_HOLDTIME 4
281 #define BGP_NOTIFY_MAJOR_FSM 5
282 #define BGP_NOTIFY_MAJOR_CEASE 6
283 #define BGP_NOTIFY_MAJOR_CAP 7
285 static const struct tok bgp_notify_major_values
[] = {
286 { BGP_NOTIFY_MAJOR_MSG
, "Message Header Error"},
287 { BGP_NOTIFY_MAJOR_OPEN
, "OPEN Message Error"},
288 { BGP_NOTIFY_MAJOR_UPDATE
, "UPDATE Message Error"},
289 { BGP_NOTIFY_MAJOR_HOLDTIME
,"Hold Timer Expired"},
290 { BGP_NOTIFY_MAJOR_FSM
, "Finite State Machine Error"},
291 { BGP_NOTIFY_MAJOR_CEASE
, "Cease"},
292 { BGP_NOTIFY_MAJOR_CAP
, "Capability Message Error"},
297 #define BGP_NOTIFY_MINOR_CEASE_MAXPRFX 1
299 #define BGP_NOTIFY_MINOR_CEASE_SHUT 2
300 #define BGP_NOTIFY_MINOR_CEASE_RESET 4
302 #define BGP_NOTIFY_MINOR_CEASE_HARDRESET 9
303 static const struct tok bgp_notify_minor_cease_values
[] = {
304 { BGP_NOTIFY_MINOR_CEASE_MAXPRFX
, "Maximum Number of Prefixes Reached"},
305 { BGP_NOTIFY_MINOR_CEASE_SHUT
, "Administrative Shutdown"},
306 { 3, "Peer Unconfigured"},
307 { BGP_NOTIFY_MINOR_CEASE_RESET
, "Administrative Reset"},
308 { 5, "Connection Rejected"},
309 { 6, "Other Configuration Change"},
310 { 7, "Connection Collision Resolution"},
311 { 8, "Out of Resources"},
312 { BGP_NOTIFY_MINOR_CEASE_HARDRESET
, "Hard Reset"},
317 static const struct tok bgp_notify_minor_msg_values
[] = {
318 { 1, "Connection Not Synchronized"},
319 { 2, "Bad Message Length"},
320 { 3, "Bad Message Type"},
324 static const struct tok bgp_notify_minor_open_values
[] = {
325 { 1, "Unsupported Version Number"},
327 { 3, "Bad BGP Identifier"},
328 { 4, "Unsupported Optional Parameter"},
329 { 5, "Authentication Failure"},
330 { 6, "Unacceptable Hold Time"},
331 { 7, "Capability Message Error"},
335 static const struct tok bgp_notify_minor_update_values
[] = {
336 { 1, "Malformed Attribute List"},
337 { 2, "Unrecognized Well-known Attribute"},
338 { 3, "Missing Well-known Attribute"},
339 { 4, "Attribute Flags Error"},
340 { 5, "Attribute Length Error"},
341 { 6, "Invalid ORIGIN Attribute"},
342 { 7, "AS Routing Loop"},
343 { 8, "Invalid NEXT_HOP Attribute"},
344 { 9, "Optional Attribute Error"},
345 { 10, "Invalid Network Field"},
346 { 11, "Malformed AS_PATH"},
350 static const struct tok bgp_notify_minor_fsm_values
[] = {
351 { 0, "Unspecified Error"},
352 { 1, "In OpenSent State"},
353 { 2, "In OpenConfirm State"},
354 { 3, "In Established State"},
358 static const struct tok bgp_notify_minor_cap_values
[] = {
359 { 1, "Invalid Action Value" },
360 { 2, "Invalid Capability Length" },
361 { 3, "Malformed Capability Value" },
362 { 4, "Unsupported Capability Code" },
366 static const struct tok bgp_origin_values
[] = {
373 #define BGP_PMSI_TUNNEL_RSVP_P2MP 1
374 #define BGP_PMSI_TUNNEL_LDP_P2MP 2
375 #define BGP_PMSI_TUNNEL_PIM_SSM 3
376 #define BGP_PMSI_TUNNEL_PIM_SM 4
377 #define BGP_PMSI_TUNNEL_PIM_BIDIR 5
378 #define BGP_PMSI_TUNNEL_INGRESS 6
379 #define BGP_PMSI_TUNNEL_LDP_MP2MP 7
381 static const struct tok bgp_pmsi_tunnel_values
[] = {
382 { BGP_PMSI_TUNNEL_RSVP_P2MP
, "RSVP-TE P2MP LSP"},
383 { BGP_PMSI_TUNNEL_LDP_P2MP
, "LDP P2MP LSP"},
384 { BGP_PMSI_TUNNEL_PIM_SSM
, "PIM-SSM Tree"},
385 { BGP_PMSI_TUNNEL_PIM_SM
, "PIM-SM Tree"},
386 { BGP_PMSI_TUNNEL_PIM_BIDIR
, "PIM-Bidir Tree"},
387 { BGP_PMSI_TUNNEL_INGRESS
, "Ingress Replication"},
388 { BGP_PMSI_TUNNEL_LDP_MP2MP
, "LDP MP2MP LSP"},
392 static const struct tok bgp_pmsi_flag_values
[] = {
393 { 0x01, "Leaf Information required"},
397 #define BGP_AIGP_TLV 1
399 static const struct tok bgp_aigp_values
[] = {
400 { BGP_AIGP_TLV
, "AIGP"},
404 /* Subsequent address family identifier, RFC2283 section 7 */
406 #define SAFNUM_UNICAST 1
407 #define SAFNUM_MULTICAST 2
408 #define SAFNUM_UNIMULTICAST 3 /* deprecated now */
409 /* labeled BGP RFC3107 */
410 #define SAFNUM_LABUNICAST 4
412 #define SAFNUM_MULTICAST_VPN 5
413 /* draft-nalawade-kapoor-tunnel-safi */
414 #define SAFNUM_TUNNEL 64
416 #define SAFNUM_VPLS 65
418 #define SAFNUM_MDT 66
420 #define SAFNUM_EVPN 70
422 #define SAFNUM_VPNUNICAST 128
424 #define SAFNUM_VPNMULTICAST 129
425 #define SAFNUM_VPNUNIMULTICAST 130 /* deprecated now */
427 #define SAFNUM_RT_ROUTING_INFO 132
429 #define BGP_VPN_RD_LEN 8
431 static const struct tok bgp_safi_values
[] = {
432 { SAFNUM_RES
, "Reserved"},
433 { SAFNUM_UNICAST
, "Unicast"},
434 { SAFNUM_MULTICAST
, "Multicast"},
435 { SAFNUM_UNIMULTICAST
, "Unicast+Multicast"},
436 { SAFNUM_LABUNICAST
, "labeled Unicast"},
437 { SAFNUM_TUNNEL
, "Tunnel"},
438 { SAFNUM_VPLS
, "VPLS"},
439 { SAFNUM_MDT
, "MDT"},
440 { SAFNUM_EVPN
, "EVPN"},
441 { SAFNUM_VPNUNICAST
, "labeled VPN Unicast"},
442 { SAFNUM_VPNMULTICAST
, "labeled VPN Multicast"},
443 { SAFNUM_VPNUNIMULTICAST
, "labeled VPN Unicast+Multicast"},
444 { SAFNUM_RT_ROUTING_INFO
, "Route Target Routing Information"},
445 { SAFNUM_MULTICAST_VPN
, "Multicast VPN"},
449 static const struct tok bgp_graceful_restart_comm_flag_values
[] = {
455 /* well-known community */
456 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
457 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
458 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
460 /* Extended community type - RFC 4360 */
461 #define BGP_EXT_COM_RT_0 0x0002 /* Route Target,Format AS(2bytes):AN(4bytes) */
462 #define BGP_EXT_COM_RT_1 0x0102 /* Route Target,Format IP address:AN(2bytes) */
463 #define BGP_EXT_COM_RT_2 0x0202 /* Route Target,Format AN(4bytes):local(2bytes) */
464 #define BGP_EXT_COM_RO_0 0x0003 /* Route Origin,Format AS(2bytes):AN(4bytes) */
465 #define BGP_EXT_COM_RO_1 0x0103 /* Route Origin,Format IP address:AN(2bytes) */
466 #define BGP_EXT_COM_RO_2 0x0203 /* Route Origin,Format AN(4bytes):local(2bytes) */
467 #define BGP_EXT_COM_LINKBAND 0x4004 /* Link Bandwidth,Format AS(2B):Bandwidth(4B) */
468 /* rfc2547 bgp-mpls-vpns */
469 #define BGP_EXT_COM_VPN_ORIGIN 0x0005 /* OSPF Domain ID / VPN of Origin - draft-rosen-vpns-ospf-bgp-mpls */
470 #define BGP_EXT_COM_VPN_ORIGIN2 0x0105 /* duplicate - keep for backwards compatibility */
471 #define BGP_EXT_COM_VPN_ORIGIN3 0x0205 /* duplicate - keep for backwards compatibility */
472 #define BGP_EXT_COM_VPN_ORIGIN4 0x8005 /* duplicate - keep for backwards compatibility */
474 #define BGP_EXT_COM_OSPF_RTYPE 0x0306 /* OSPF Route Type,Format Area(4B):RouteType(1B):Options(1B) */
475 #define BGP_EXT_COM_OSPF_RTYPE2 0x8000 /* duplicate - keep for backwards compatibility */
476 #define BGP_EXT_COM_ENCAP 0x030c /* rfc5512 */
478 #define BGP_EXT_COM_OSPF_RID 0x0107 /* OSPF Router ID,Format RouterID(4B):Unused(2B) */
479 #define BGP_EXT_COM_OSPF_RID2 0x8001 /* duplicate - keep for backwards compatibility */
481 #define BGP_EXT_COM_L2INFO 0x800a /* draft-kompella-ppvpn-l2vpn */
483 #define BGP_EXT_COM_SOURCE_AS 0x0009 /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */
484 #define BGP_EXT_COM_VRF_RT_IMP 0x010b /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */
485 #define BGP_EXT_COM_L2VPN_RT_0 0x000a /* L2VPN Identifier,Format AS(2bytes):AN(4bytes) */
486 #define BGP_EXT_COM_L2VPN_RT_1 0xF10a /* L2VPN Identifier,Format IP address:AN(2bytes) */
488 /* https://www.cisco.com/en/US/tech/tk436/tk428/technologies_tech_note09186a00801eb09a.shtml */
489 #define BGP_EXT_COM_EIGRP_GEN 0x8800
490 #define BGP_EXT_COM_EIGRP_METRIC_AS_DELAY 0x8801
491 #define BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW 0x8802
492 #define BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU 0x8803
493 #define BGP_EXT_COM_EIGRP_EXT_REMAS_REMID 0x8804
494 #define BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC 0x8805
496 /* Optional Parameters */
497 #define BGP_OPEN_NON_EXT_OPT_TYPE_EXTENDED_LENGTH 255 /* Non-Ext OP Type */
499 static const struct tok bgp_extd_comm_flag_values
[] = {
500 { 0x8000, "vendor-specific"},
501 { 0x4000, "non-transitive"},
505 static const struct tok bgp_extd_comm_subtype_values
[] = {
506 { BGP_EXT_COM_RT_0
, "target"},
507 { BGP_EXT_COM_RT_1
, "target"},
508 { BGP_EXT_COM_RT_2
, "target"},
509 { BGP_EXT_COM_RO_0
, "origin"},
510 { BGP_EXT_COM_RO_1
, "origin"},
511 { BGP_EXT_COM_RO_2
, "origin"},
512 { BGP_EXT_COM_LINKBAND
, "link-BW"},
513 { BGP_EXT_COM_VPN_ORIGIN
, "ospf-domain"},
514 { BGP_EXT_COM_VPN_ORIGIN2
, "ospf-domain"},
515 { BGP_EXT_COM_VPN_ORIGIN3
, "ospf-domain"},
516 { BGP_EXT_COM_VPN_ORIGIN4
, "ospf-domain"},
517 { BGP_EXT_COM_OSPF_RTYPE
, "ospf-route-type"},
518 { BGP_EXT_COM_OSPF_RTYPE2
, "ospf-route-type"},
519 { BGP_EXT_COM_ENCAP
, "encapsulation"},
520 { BGP_EXT_COM_OSPF_RID
, "ospf-router-id"},
521 { BGP_EXT_COM_OSPF_RID2
, "ospf-router-id"},
522 { BGP_EXT_COM_L2INFO
, "layer2-info"},
523 { BGP_EXT_COM_EIGRP_GEN
, "eigrp-general-route (flag, tag)" },
524 { BGP_EXT_COM_EIGRP_METRIC_AS_DELAY
, "eigrp-route-metric (AS, delay)" },
525 { BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW
, "eigrp-route-metric (reliability, nexthop, bandwidth)" },
526 { BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU
, "eigrp-route-metric (load, MTU)" },
527 { BGP_EXT_COM_EIGRP_EXT_REMAS_REMID
, "eigrp-external-route (remote-AS, remote-ID)" },
528 { BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC
, "eigrp-external-route (remote-proto, remote-metric)" },
529 { BGP_EXT_COM_SOURCE_AS
, "source-AS" },
530 { BGP_EXT_COM_VRF_RT_IMP
, "vrf-route-import"},
531 { BGP_EXT_COM_L2VPN_RT_0
, "l2vpn-id"},
532 { BGP_EXT_COM_L2VPN_RT_1
, "l2vpn-id"},
536 /* RFC RFC5512 BGP Tunnel Encapsulation Attribute Tunnel Types */
537 #define BGP_ENCAP_TUNNEL_L2TPV3_IP 1
538 #define BGP_ENCAP_TUNNEL_GRE 2
539 #define BGP_ENCAP_TUNNEL_TRANSMIT 3
540 #define BGP_ENCAP_TUNNEL_IPSEC 4
541 #define BGP_ENCAP_TUNNEL_IP_IPSEC 5
542 #define BGP_ENCAP_TUNNEL_MPLS_IP 6
543 #define BGP_ENCAP_TUNNEL_IP_IP 7
544 #define BGP_ENCAP_TUNNEL_VXLAN 8
545 #define BGP_ENCAP_TUNNEL_NVGRE 9
546 #define BGP_ENCAP_TUNNEL_MPLS 10
547 #define BGP_ENCAP_TUNNEL_MPLS_GRE 11
548 #define BGP_ENCAP_TUNNEL_VXLAN_GPE 12
549 #define BGP_ENCAP_TUNNEL_MPLS_UDP 13
550 #define BGP_ENCAP_TUNNEL_IPV6 14
551 #define BGP_ENCAP_TUNNEL_SR_TE 15
552 #define BGP_ENCAP_TUNNEL_BARE 16
553 #define BGP_ENCAP_TUNNEL_SR 17
555 static const struct tok bgp_extd_comm_encap_tunnel_values
[] = {
556 { BGP_ENCAP_TUNNEL_L2TPV3_IP
, "L2TPv3 over IP"},
557 { BGP_ENCAP_TUNNEL_GRE
, "GRE"},
558 { BGP_ENCAP_TUNNEL_TRANSMIT
, "Transmit Tunnel"},
559 { BGP_ENCAP_TUNNEL_IPSEC
, "IPsec"},
560 { BGP_ENCAP_TUNNEL_IP_IPSEC
, "IP in IP with IPsec"},
561 { BGP_ENCAP_TUNNEL_MPLS_IP
, "MPLS in IP with IPsec"},
562 { BGP_ENCAP_TUNNEL_IP_IP
, "IP in IP"},
563 { BGP_ENCAP_TUNNEL_VXLAN
, "VXLAN"},
564 { BGP_ENCAP_TUNNEL_NVGRE
, "NVGRE"},
565 { BGP_ENCAP_TUNNEL_MPLS
, "MPLS"},
566 { BGP_ENCAP_TUNNEL_MPLS_GRE
, "MPLS in GRE"},
567 { BGP_ENCAP_TUNNEL_VXLAN_GPE
, "VXLAN GPE"},
568 { BGP_ENCAP_TUNNEL_MPLS_UDP
, "MPLS in UDP"},
569 { BGP_ENCAP_TUNNEL_IPV6
, "IPv6"},
570 { BGP_ENCAP_TUNNEL_SR_TE
, "SR TE"},
571 { BGP_ENCAP_TUNNEL_BARE
, "Bare"},
572 { BGP_ENCAP_TUNNEL_SR
, "SR"},
576 /* OSPF codes for BGP_EXT_COM_OSPF_RTYPE draft-rosen-vpns-ospf-bgp-mpls */
577 #define BGP_OSPF_RTYPE_RTR 1 /* OSPF Router LSA */
578 #define BGP_OSPF_RTYPE_NET 2 /* OSPF Network LSA */
579 #define BGP_OSPF_RTYPE_SUM 3 /* OSPF Summary LSA */
580 #define BGP_OSPF_RTYPE_EXT 5 /* OSPF External LSA, note that ASBR doesn't apply to MPLS-VPN */
581 #define BGP_OSPF_RTYPE_NSSA 7 /* OSPF NSSA External*/
582 #define BGP_OSPF_RTYPE_SHAM 129 /* OSPF-MPLS-VPN Sham link */
583 #define BGP_OSPF_RTYPE_METRIC_TYPE 0x1 /* LSB of RTYPE Options Field */
585 static const struct tok bgp_extd_comm_ospf_rtype_values
[] = {
586 { BGP_OSPF_RTYPE_RTR
, "Router" },
587 { BGP_OSPF_RTYPE_NET
, "Network" },
588 { BGP_OSPF_RTYPE_SUM
, "Summary" },
589 { BGP_OSPF_RTYPE_EXT
, "External" },
590 { BGP_OSPF_RTYPE_NSSA
,"NSSA External" },
591 { BGP_OSPF_RTYPE_SHAM
,"MPLS-VPN Sham" },
595 /* ADD-PATH Send/Receive field values */
596 static const struct tok bgp_add_path_recvsend
[] = {
603 #define AS_STR_SIZE sizeof("xxxxx.xxxxx")
608 * Convert an AS number into a string and return string pointer.
610 * Depending on bflag is set or not, AS number is converted into ASDOT notation
611 * or plain number notation.
615 as_printf(netdissect_options
*ndo
,
616 char *str
, size_t size
, u_int asnum
)
618 if (!ndo
->ndo_bflag
|| asnum
<= 0xFFFF) {
619 snprintf(str
, size
, "%u", asnum
);
621 snprintf(str
, size
, "%u.%u", asnum
>> 16, asnum
& 0xFFFF);
626 #define ITEMCHECK(minlen) if (itemlen < minlen) goto badtlv;
629 decode_prefix4(netdissect_options
*ndo
,
630 const u_char
*pptr
, u_int itemlen
, char *buf
, size_t buflen
)
633 u_int plen
, plenbytes
;
636 plen
= GET_U_1(pptr
);
641 memset(&addr
, 0, sizeof(addr
));
642 plenbytes
= (plen
+ 7) / 8;
643 ITEMCHECK(plenbytes
);
644 GET_CPY_BYTES(&addr
, pptr
+ 1, plenbytes
);
646 ((u_char
*)&addr
)[plenbytes
- 1] &= ((0xff00 >> (plen
% 8)) & 0xff);
648 snprintf(buf
, buflen
, "%s/%u", ipaddr_string(ndo
, (const u_char
*)&addr
), plen
);
649 return 1 + plenbytes
;
656 decode_labeled_prefix4(netdissect_options
*ndo
,
657 const u_char
*pptr
, u_int itemlen
, char *buf
,
661 u_int plen
, plenbytes
;
663 /* prefix length and label = 4 bytes */
666 plen
= GET_U_1(pptr
); /* get prefix length */
668 /* this is one of the weirdnesses of rfc3107
669 the label length (actually the label + COS bits)
670 is added to the prefix length;
671 we also do only read out just one label -
672 there is no real application for advertisement of
673 stacked labels in a single BGP message
679 plen
-=24; /* adjust prefixlen - labellength */
685 memset(&addr
, 0, sizeof(addr
));
686 plenbytes
= (plen
+ 7) / 8;
687 ITEMCHECK(plenbytes
);
688 GET_CPY_BYTES(&addr
, pptr
+ 4, plenbytes
);
690 ((u_char
*)&addr
)[plenbytes
- 1] &= ((0xff00 >> (plen
% 8)) & 0xff);
692 /* the label may get offsetted by 4 bits so lets shift it right */
693 snprintf(buf
, buflen
, "%s/%u, label:%u %s",
694 ipaddr_string(ndo
, (const u_char
*)&addr
),
696 GET_BE_U_3(pptr
+ 1)>>4,
697 ((GET_U_1(pptr
+ 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
699 return 4 + plenbytes
;
711 * print an ipv4 or ipv6 address into a buffer dependent on address length.
714 bgp_vpn_ip_print(netdissect_options
*ndo
,
715 const u_char
*pptr
, u_int addr_length
)
718 /* worst case string is s fully formatted v6 address */
719 static char addr
[sizeof("1234:5678:89ab:cdef:1234:5678:89ab:cdef")];
722 switch(addr_length
) {
723 case (sizeof(nd_ipv4
) << 3): /* 32 */
724 snprintf(pos
, sizeof(addr
), "%s", GET_IPADDR_STRING(pptr
));
726 case (sizeof(nd_ipv6
) << 3): /* 128 */
727 snprintf(pos
, sizeof(addr
), "%s", GET_IP6ADDR_STRING(pptr
));
730 snprintf(pos
, sizeof(addr
), "bogus address length %u", addr_length
);
742 * print an multicast s,g entry into a buffer.
743 * the s,g entry is encoded like this.
745 * +-----------------------------------+
746 * | Multicast Source Length (1 octet) |
747 * +-----------------------------------+
748 * | Multicast Source (Variable) |
749 * +-----------------------------------+
750 * | Multicast Group Length (1 octet) |
751 * +-----------------------------------+
752 * | Multicast Group (Variable) |
753 * +-----------------------------------+
755 * return the number of bytes read from the wire.
758 bgp_vpn_sg_print(netdissect_options
*ndo
,
759 const u_char
*pptr
, char *buf
, size_t buflen
)
762 u_int total_length
, offset
;
766 /* Source address length, encoded in bits */
767 addr_length
= GET_U_1(pptr
);
771 ND_TCHECK_LEN(pptr
, (addr_length
>> 3));
772 total_length
+= (addr_length
>> 3) + 1;
773 offset
= (u_int
)strlen(buf
);
775 snprintf(buf
+ offset
, buflen
- offset
, ", Source %s",
776 bgp_vpn_ip_print(ndo
, pptr
, addr_length
));
777 pptr
+= (addr_length
>> 3);
780 /* Group address length, encoded in bits */
781 addr_length
= GET_U_1(pptr
);
785 ND_TCHECK_LEN(pptr
, (addr_length
>> 3));
786 total_length
+= (addr_length
>> 3) + 1;
787 offset
= (u_int
)strlen(buf
);
789 snprintf(buf
+ offset
, buflen
- offset
, ", Group %s",
790 bgp_vpn_ip_print(ndo
, pptr
, addr_length
));
791 pptr
+= (addr_length
>> 3);
795 return (total_length
);
798 /* Print an RFC 4364 Route Distinguisher */
800 bgp_vpn_rd_print(netdissect_options
*ndo
,
803 /* allocate space for the largest possible string */
804 static char rd
[sizeof("xxxxx.xxxxx:xxxxx (xxx.xxx.xxx.xxx:xxxxx)")];
806 /* allocate space for the largest possible string */
807 char astostr
[AS_STR_SIZE
];
809 /* ok lets load the RD format */
810 switch (GET_BE_U_2(pptr
)) {
813 /* 2-byte-AS:number fmt */
814 snprintf(pos
, sizeof(rd
) - (pos
- rd
), "%u:%u (= %u.%u.%u.%u)",
815 GET_BE_U_2(pptr
+ 2),
816 GET_BE_U_4(pptr
+ 4),
817 GET_U_1(pptr
+ 4), GET_U_1(pptr
+ 5),
818 GET_U_1(pptr
+ 6), GET_U_1(pptr
+ 7));
822 /* IP-address:AS fmt */
823 snprintf(pos
, sizeof(rd
) - (pos
- rd
), "%u.%u.%u.%u:%u",
824 GET_U_1(pptr
+ 2), GET_U_1(pptr
+ 3),
825 GET_U_1(pptr
+ 4), GET_U_1(pptr
+ 5),
826 GET_BE_U_2(pptr
+ 6));
830 /* 4-byte-AS:number fmt */
831 snprintf(pos
, sizeof(rd
) - (pos
- rd
), "%s:%u (%u.%u.%u.%u:%u)",
832 as_printf(ndo
, astostr
, sizeof(astostr
), GET_BE_U_4(pptr
+ 2)),
833 GET_BE_U_2(pptr
+ 6), GET_U_1(pptr
+ 2),
834 GET_U_1(pptr
+ 3), GET_U_1(pptr
+ 4),
835 GET_U_1(pptr
+ 5), GET_BE_U_2(pptr
+ 6));
838 snprintf(pos
, sizeof(rd
) - (pos
- rd
), "unknown RD format");
847 * Print an RFC 4360 Extended Community.
850 bgp_extended_community_print(netdissect_options
*ndo
,
853 union { /* copy buffer for bandwidth values */
857 /* allocate space for the largest possible string */
858 char astostr
[AS_STR_SIZE
];
860 switch (GET_BE_U_2(pptr
)) {
862 case BGP_EXT_COM_RT_0
:
863 case BGP_EXT_COM_RO_0
:
864 case BGP_EXT_COM_L2VPN_RT_0
:
865 ND_PRINT("%u:%u (= %s)",
866 GET_BE_U_2(pptr
+ 2),
867 GET_BE_U_4(pptr
+ 4),
868 GET_IPADDR_STRING(pptr
+4));
871 case BGP_EXT_COM_RT_1
:
872 case BGP_EXT_COM_RO_1
:
873 case BGP_EXT_COM_L2VPN_RT_1
:
874 case BGP_EXT_COM_VRF_RT_IMP
:
876 GET_IPADDR_STRING(pptr
+2),
877 GET_BE_U_2(pptr
+ 6));
880 case BGP_EXT_COM_RT_2
:
881 case BGP_EXT_COM_RO_2
:
883 as_printf(ndo
, astostr
, sizeof(astostr
),
884 GET_BE_U_4(pptr
+ 2)), GET_BE_U_2(pptr
+ 6));
887 case BGP_EXT_COM_LINKBAND
:
888 bw
.i
= GET_BE_U_4(pptr
+ 4);
889 ND_PRINT("bandwidth: %.3f Mbps",
893 case BGP_EXT_COM_VPN_ORIGIN
:
894 case BGP_EXT_COM_VPN_ORIGIN2
:
895 case BGP_EXT_COM_VPN_ORIGIN3
:
896 case BGP_EXT_COM_VPN_ORIGIN4
:
897 case BGP_EXT_COM_OSPF_RID
:
898 case BGP_EXT_COM_OSPF_RID2
:
899 ND_PRINT("%s", GET_IPADDR_STRING(pptr
+2));
902 case BGP_EXT_COM_OSPF_RTYPE
:
903 case BGP_EXT_COM_OSPF_RTYPE2
:
904 ND_PRINT("area:%s, router-type:%s, metric-type:%s%s",
905 GET_IPADDR_STRING(pptr
+2),
906 tok2str(bgp_extd_comm_ospf_rtype_values
,
908 GET_U_1((pptr
+ 6))),
909 (GET_U_1(pptr
+ 7) & BGP_OSPF_RTYPE_METRIC_TYPE
) ? "E2" : "",
910 ((GET_U_1(pptr
+ 6) == BGP_OSPF_RTYPE_EXT
) || (GET_U_1(pptr
+ 6) == BGP_OSPF_RTYPE_NSSA
)) ? "E1" : "");
913 case BGP_EXT_COM_L2INFO
:
914 ND_PRINT("%s Control Flags [0x%02x]:MTU %u",
915 tok2str(l2vpn_encaps_values
,
917 GET_U_1((pptr
+ 2))),
919 GET_BE_U_2(pptr
+ 4));
922 case BGP_EXT_COM_SOURCE_AS
:
923 ND_PRINT("AS %u", GET_BE_U_2(pptr
+ 2));
926 case BGP_EXT_COM_ENCAP
:
927 ND_PRINT("Tunnel type: %s", tok2str(bgp_extd_comm_encap_tunnel_values
,
929 GET_BE_U_2(pptr
+ 6)));
933 ND_PRINT("%02x%02x%02x%02x%02x%02x",
945 * RFC4684 (Section 4)/RFC2858 (Section 4).
946 * RTC membership prefix is structured as follows
947 * [prefix-len] [origin-as] [route-target]
948 * The route-target is encoded as RT ext-comms.
949 * Prefix-len may be 0, 32..96
951 * Note that pptr is not packet data - it is
952 * a buffer owned by our caller - therefore GET_*
953 * macros can not be used.
956 bgp_rt_prefix_print(netdissect_options
*ndo
,
960 /* allocate space for the largest possible string */
961 char rtc_prefix_in_hex
[sizeof("0000 0000 0000 0000")] = "";
962 u_int rtc_prefix_in_hex_len
= 0;
963 static char output
[61]; /* max response string */
964 /* allocate space for the largest possible string */
965 char astostr
[AS_STR_SIZE
];
966 uint16_t ec_type
= 0;
971 snprintf(output
, sizeof(output
), "route-target: 0:0/0");
975 /* hex representation of the prefix */
976 octet_count
= (plen
+7)/8;
977 for (i
=0; i
<octet_count
; i
++) {
978 rtc_prefix_in_hex_len
+= snprintf(rtc_prefix_in_hex
+rtc_prefix_in_hex_len
,
979 sizeof(rtc_prefix_in_hex
)-rtc_prefix_in_hex_len
,
981 ((i
%2 == 1) && (i
<octet_count
-1)) ? " " : "");
986 * The prefix is too short to include the full ext-comm type,
987 * so we have no way to parse it further.
989 snprintf(output
, sizeof(output
), "route-target: partial-type: (%s/%d)",
990 rtc_prefix_in_hex
, plen
);
995 * get the ext-comm type
996 * Note: pptr references a static 8 octet buffer with unused bits set to 0,
997 * hence EXTRACT_*() macros are safe.
999 ec_type
= EXTRACT_BE_U_2(pptr
);
1001 case BGP_EXT_COM_RT_0
:
1002 /* 2-byte-AS:number fmt */
1003 snprintf(output
, sizeof(output
), "route-target: %u:%u/%d (%s)",
1004 EXTRACT_BE_U_2(pptr
+2),
1005 EXTRACT_BE_U_4(pptr
+4),
1006 plen
, rtc_prefix_in_hex
);
1009 case BGP_EXT_COM_RT_1
:
1010 /* IP-address:AS fmt */
1011 snprintf(output
, sizeof(output
), "route-target: %u.%u.%u.%u:%u/%d (%s)",
1012 *(pptr
+2), *(pptr
+3), *(pptr
+4), *(pptr
+5),
1013 EXTRACT_BE_U_2(pptr
+6), plen
, rtc_prefix_in_hex
);
1016 case BGP_EXT_COM_RT_2
:
1017 /* 4-byte-AS:number fmt */
1018 snprintf(output
, sizeof(output
), "route-target: %s:%u/%d (%s)",
1019 as_printf(ndo
, astostr
, sizeof(astostr
), EXTRACT_BE_U_4(pptr
+2)),
1020 EXTRACT_BE_U_2(pptr
+6), plen
, rtc_prefix_in_hex
);
1024 snprintf(output
, sizeof(output
), "route target: unknown-type(%04x) (%s/%d)",
1026 rtc_prefix_in_hex
, plen
);
1034 decode_rt_routing_info(netdissect_options
*ndo
,
1037 uint8_t route_target
[8];
1039 /* allocate space for the largest possible string */
1040 char astostr
[AS_STR_SIZE
];
1043 /* NLRI "prefix length" from RFC 2858 Section 4. */
1044 plen
= GET_U_1(pptr
); /* get prefix length */
1046 /* NLRI "prefix" (ibid), valid lengths are { 0, 32, 33, ..., 96 } bits.
1047 * RFC 4684 Section 4 defines the layout of "origin AS" and "route
1048 * target" fields inside the "prefix" depending on its length.
1051 /* Without "origin AS", without "route target". */
1052 ND_PRINT("\n\t default route target");
1057 ND_PRINT("\n\t (illegal prefix length)");
1061 /* With at least "origin AS", possibly with "route target". */
1062 as_printf(ndo
, astostr
, sizeof(astostr
), GET_BE_U_4(pptr
+ 1));
1064 plen
-= 32; /* adjust prefix length */
1067 ND_PRINT("\n\t (illegal prefix length)");
1071 /* From now on (plen + 7) / 8 evaluates to { 0, 1, 2, ..., 8 }
1072 * and gives the number of octets in the variable-length "route
1073 * target" field inside this NLRI "prefix". Look for it.
1075 memset(&route_target
, 0, sizeof(route_target
));
1076 num_octets
= (plen
+ 7) / 8;
1077 GET_CPY_BYTES(&route_target
, pptr
+ 5, num_octets
);
1078 /* If mask-len is not on octet boundary, ensure all extra bits are 0 */
1080 ((u_char
*)&route_target
)[num_octets
- 1] &=
1081 ((0xff00 >> (plen
% 8)) & 0xff);
1083 ND_PRINT("\n\t origin AS: %s, %s",
1085 bgp_rt_prefix_print(ndo
, (u_char
*)&route_target
, plen
));
1087 return 5 + num_octets
;
1091 decode_labeled_vpn_prefix4(netdissect_options
*ndo
,
1092 const u_char
*pptr
, char *buf
, size_t buflen
)
1097 plen
= GET_U_1(pptr
); /* get prefix length */
1102 plen
-= (24+64); /* adjust prefixlen - labellength - RD len*/
1107 memset(&addr
, 0, sizeof(addr
));
1108 GET_CPY_BYTES(&addr
, pptr
+ 12, (plen
+ 7) / 8);
1110 ((u_char
*)&addr
)[(plen
+ 7) / 8 - 1] &=
1111 ((0xff00 >> (plen
% 8)) & 0xff);
1113 /* the label may get offsetted by 4 bits so lets shift it right */
1114 snprintf(buf
, buflen
, "RD: %s, %s/%u, label:%u %s",
1115 bgp_vpn_rd_print(ndo
, pptr
+4),
1116 ipaddr_string(ndo
, (const u_char
*)&addr
),
1118 GET_BE_U_3(pptr
+ 1)>>4,
1119 ((GET_U_1(pptr
+ 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1121 return 12 + (plen
+ 7) / 8;
1125 * +-------------------------------+
1127 * | RD:IPv4-address (12 octets) |
1129 * +-------------------------------+
1130 * | MDT Group-address (4 octets) |
1131 * +-------------------------------+
1134 #define MDT_VPN_NLRI_LEN 16
1137 decode_mdt_vpn_nlri(netdissect_options
*ndo
,
1138 const u_char
*pptr
, char *buf
, size_t buflen
)
1141 const u_char
*vpn_ip
;
1143 /* if the NLRI is not predefined length, quit.*/
1144 if (GET_U_1(pptr
) != MDT_VPN_NLRI_LEN
* 8)
1155 pptr
+= sizeof(nd_ipv4
);
1157 /* MDT Group Address */
1158 snprintf(buf
, buflen
, "RD: %s, VPN IP Address: %s, MC Group Address: %s",
1159 bgp_vpn_rd_print(ndo
, rd
), GET_IPADDR_STRING(vpn_ip
), GET_IPADDR_STRING(pptr
));
1161 return MDT_VPN_NLRI_LEN
+ 1;
1167 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI 1
1168 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI 2
1169 #define BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI 3
1170 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF 4
1171 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE 5
1172 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN 6
1173 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN 7
1175 static const struct tok bgp_multicast_vpn_route_type_values
[] = {
1176 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI
, "Intra-AS I-PMSI"},
1177 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI
, "Inter-AS I-PMSI"},
1178 { BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI
, "S-PMSI"},
1179 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF
, "Intra-AS Segment-Leaf"},
1180 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE
, "Source-Active"},
1181 { BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN
, "Shared Tree Join"},
1182 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN
, "Source Tree Join"},
1187 decode_multicast_vpn(netdissect_options
*ndo
,
1188 const u_char
*pptr
, char *buf
, size_t buflen
)
1190 /* allocate space for the largest possible string */
1191 char astostr
[AS_STR_SIZE
];
1192 uint8_t route_type
, route_length
;
1193 u_int addr_length
, sg_length
;
1196 route_type
= GET_U_1(pptr
);
1198 route_length
= GET_U_1(pptr
);
1201 snprintf(buf
, buflen
, "Route-Type: %s (%u), length: %u",
1202 tok2str(bgp_multicast_vpn_route_type_values
,
1203 "Unknown", route_type
),
1204 route_type
, route_length
);
1206 switch(route_type
) {
1207 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI
:
1208 ND_TCHECK_LEN(pptr
, BGP_VPN_RD_LEN
);
1209 offset
= (u_int
)strlen(buf
);
1210 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s, Originator %s",
1211 bgp_vpn_rd_print(ndo
, pptr
),
1212 bgp_vpn_ip_print(ndo
, pptr
+ BGP_VPN_RD_LEN
,
1213 (route_length
- BGP_VPN_RD_LEN
) << 3));
1215 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI
:
1216 ND_TCHECK_LEN(pptr
, BGP_VPN_RD_LEN
+ 4);
1217 offset
= (u_int
)strlen(buf
);
1218 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s, Source-AS %s",
1219 bgp_vpn_rd_print(ndo
, pptr
),
1220 as_printf(ndo
, astostr
, sizeof(astostr
),
1221 GET_BE_U_4(pptr
+ BGP_VPN_RD_LEN
)));
1224 case BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI
:
1225 ND_TCHECK_LEN(pptr
, BGP_VPN_RD_LEN
);
1226 offset
= (u_int
)strlen(buf
);
1227 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s",
1228 bgp_vpn_rd_print(ndo
, pptr
));
1229 pptr
+= BGP_VPN_RD_LEN
;
1231 sg_length
= bgp_vpn_sg_print(ndo
, pptr
, buf
, buflen
);
1232 addr_length
= route_length
- sg_length
;
1234 ND_TCHECK_LEN(pptr
, addr_length
);
1235 offset
= (u_int
)strlen(buf
);
1236 snprintf(buf
+ offset
, buflen
- offset
, ", Originator %s",
1237 bgp_vpn_ip_print(ndo
, pptr
, addr_length
<< 3));
1240 case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE
:
1241 ND_TCHECK_LEN(pptr
, BGP_VPN_RD_LEN
);
1242 offset
= (u_int
)strlen(buf
);
1243 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s",
1244 bgp_vpn_rd_print(ndo
, pptr
));
1245 pptr
+= BGP_VPN_RD_LEN
;
1247 bgp_vpn_sg_print(ndo
, pptr
, buf
, buflen
);
1250 case BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN
: /* fall through */
1251 case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN
:
1252 ND_TCHECK_LEN(pptr
, BGP_VPN_RD_LEN
+ 4);
1253 offset
= (u_int
)strlen(buf
);
1254 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s, Source-AS %s",
1255 bgp_vpn_rd_print(ndo
, pptr
),
1256 as_printf(ndo
, astostr
, sizeof(astostr
),
1257 GET_BE_U_4(pptr
+ BGP_VPN_RD_LEN
)));
1258 pptr
+= BGP_VPN_RD_LEN
+ 4;
1260 bgp_vpn_sg_print(ndo
, pptr
, buf
, buflen
);
1264 * no per route-type printing yet.
1266 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF
:
1271 return route_length
+ 2;
1278 * As I remember, some versions of systems have an snprintf() that
1279 * returns -1 if the buffer would have overflowed. If the return
1280 * value is negative, set buflen to 0, to indicate that we've filled
1283 * If the return value is greater than buflen, that means that
1284 * the buffer would have overflowed; again, set buflen to 0 in
1287 #define UPDATE_BUF_BUFLEN(buf, buflen, stringlen) \
1290 else if ((u_int)stringlen>buflen) \
1293 buflen-=stringlen; \
1298 decode_labeled_vpn_l2(netdissect_options
*ndo
,
1299 const u_char
*pptr
, char *buf
, size_t buflen
)
1301 u_int plen
, tlen
, tlv_type
, tlv_len
, ttlv_len
;
1304 plen
= GET_BE_U_2(pptr
);
1307 /* Old and new L2VPN NLRI share AFI/SAFI
1308 * -> Assume a 12 Byte-length NLRI is auto-discovery-only
1309 * and > 17 as old format. Complain for the middle case
1312 /* assume AD-only with RD, BGPNH */
1313 ND_TCHECK_LEN(pptr
, 12);
1315 stringlen
= snprintf(buf
, buflen
, "RD: %s, BGPNH: %s",
1316 bgp_vpn_rd_print(ndo
, pptr
),
1317 GET_IPADDR_STRING(pptr
+8));
1318 UPDATE_BUF_BUFLEN(buf
, buflen
, stringlen
);
1322 } else if (plen
> 17) {
1323 /* assume old format */
1324 /* RD, ID, LBLKOFF, LBLBASE */
1326 ND_TCHECK_LEN(pptr
, 15);
1328 stringlen
= snprintf(buf
, buflen
, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
1329 bgp_vpn_rd_print(ndo
, pptr
),
1330 GET_BE_U_2(pptr
+ 8),
1331 GET_BE_U_2(pptr
+ 10),
1332 GET_BE_U_3(pptr
+ 12)>>4); /* the label is offsetted by 4 bits so lets shift it right */
1333 UPDATE_BUF_BUFLEN(buf
, buflen
, stringlen
);
1337 /* ok now the variable part - lets read out TLVs*/
1341 stringlen
=snprintf(buf
,buflen
, "\n\t\tran past the end");
1342 UPDATE_BUF_BUFLEN(buf
, buflen
, stringlen
);
1346 tlv_type
= GET_U_1(pptr
);
1348 tlv_len
= GET_BE_U_2(pptr
); /* length, in *bits* */
1349 ttlv_len
= (tlv_len
+ 7)/8; /* length, in *bytes* */
1355 stringlen
=snprintf(buf
,buflen
, "\n\t\tcircuit status vector (%u) length: %u: 0x",
1358 UPDATE_BUF_BUFLEN(buf
, buflen
, stringlen
);
1360 while (ttlv_len
!= 0) {
1363 stringlen
=snprintf(buf
,buflen
, " (ran past the end)");
1364 UPDATE_BUF_BUFLEN(buf
, buflen
, stringlen
);
1370 stringlen
=snprintf(buf
,buflen
, "%02x",
1373 UPDATE_BUF_BUFLEN(buf
, buflen
, stringlen
);
1381 stringlen
=snprintf(buf
,buflen
, "\n\t\tunknown TLV #%u, length: %u",
1384 UPDATE_BUF_BUFLEN(buf
, buflen
, stringlen
);
1386 if (tlen
< ttlv_len
) {
1388 stringlen
=snprintf(buf
,buflen
, " (ran past the end)");
1389 UPDATE_BUF_BUFLEN(buf
, buflen
, stringlen
);
1399 /* complain bitterly ? */
1409 decode_prefix6(netdissect_options
*ndo
,
1410 const u_char
*pd
, u_int itemlen
, char *buf
, size_t buflen
)
1413 u_int plen
, plenbytes
;
1421 memset(&addr
, 0, sizeof(addr
));
1422 plenbytes
= (plen
+ 7) / 8;
1423 ITEMCHECK(plenbytes
);
1424 GET_CPY_BYTES(&addr
, pd
+ 1, plenbytes
);
1426 addr
[plenbytes
- 1] &=
1427 ((0xff00 >> (plen
% 8)) & 0xff);
1429 snprintf(buf
, buflen
, "%s/%u", ip6addr_string(ndo
, (const u_char
*)&addr
), plen
);
1430 return 1 + plenbytes
;
1437 decode_labeled_prefix6(netdissect_options
*ndo
,
1438 const u_char
*pptr
, u_int itemlen
, char *buf
, size_t buflen
)
1441 u_int plen
, plenbytes
;
1443 /* prefix length and label = 4 bytes */
1446 plen
= GET_U_1(pptr
); /* get prefix length */
1451 plen
-= 24; /* adjust prefixlen - labellength */
1457 memset(&addr
, 0, sizeof(addr
));
1458 plenbytes
= (plen
+ 7) / 8;
1459 GET_CPY_BYTES(&addr
, pptr
+ 4, plenbytes
);
1461 addr
[plenbytes
- 1] &=
1462 ((0xff00 >> (plen
% 8)) & 0xff);
1464 /* the label may get offsetted by 4 bits so lets shift it right */
1465 snprintf(buf
, buflen
, "%s/%u, label:%u %s",
1466 ip6addr_string(ndo
, (const u_char
*)&addr
),
1468 GET_BE_U_3(pptr
+ 1)>>4,
1469 ((GET_U_1(pptr
+ 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1471 return 4 + plenbytes
;
1481 decode_labeled_vpn_prefix6(netdissect_options
*ndo
,
1482 const u_char
*pptr
, char *buf
, size_t buflen
)
1487 plen
= GET_U_1(pptr
); /* get prefix length */
1492 plen
-= (24+64); /* adjust prefixlen - labellength - RD len*/
1497 memset(&addr
, 0, sizeof(addr
));
1498 GET_CPY_BYTES(&addr
, pptr
+ 12, (plen
+ 7) / 8);
1500 addr
[(plen
+ 7) / 8 - 1] &=
1501 ((0xff00 >> (plen
% 8)) & 0xff);
1503 /* the label may get offsetted by 4 bits so lets shift it right */
1504 snprintf(buf
, buflen
, "RD: %s, %s/%u, label:%u %s",
1505 bgp_vpn_rd_print(ndo
, pptr
+4),
1506 ip6addr_string(ndo
, (const u_char
*)&addr
),
1508 GET_BE_U_3(pptr
+ 1)>>4,
1509 ((GET_U_1(pptr
+ 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1511 return 12 + (plen
+ 7) / 8;
1515 decode_clnp_prefix(netdissect_options
*ndo
,
1516 const u_char
*pptr
, char *buf
, size_t buflen
)
1521 plen
= GET_U_1(pptr
); /* get prefix length */
1526 memset(&addr
, 0, sizeof(addr
));
1527 GET_CPY_BYTES(&addr
, pptr
+ 4, (plen
+ 7) / 8);
1529 addr
[(plen
+ 7) / 8 - 1] &=
1530 ((0xff00 >> (plen
% 8)) & 0xff);
1532 /* Cannot use GET_ISONSAP_STRING (not packet buffer pointer) */
1533 snprintf(buf
, buflen
, "%s/%u",
1534 isonsap_string(ndo
, addr
,(plen
+ 7) / 8),
1537 return 1 + (plen
+ 7) / 8;
1541 decode_labeled_vpn_clnp_prefix(netdissect_options
*ndo
,
1542 const u_char
*pptr
, char *buf
, size_t buflen
)
1547 plen
= GET_U_1(pptr
); /* get prefix length */
1552 plen
-= (24+64); /* adjust prefixlen - labellength - RD len*/
1557 memset(&addr
, 0, sizeof(addr
));
1558 GET_CPY_BYTES(&addr
, pptr
+ 12, (plen
+ 7) / 8);
1560 addr
[(plen
+ 7) / 8 - 1] &= ((0xff00 >> (plen
% 8)) & 0xff);
1562 /* the label may get offsetted by 4 bits so lets shift it right */
1563 /* Cannot use GET_ISONSAP_STRING (not packet buffer pointer) */
1564 snprintf(buf
, buflen
, "RD: %s, %s/%u, label:%u %s",
1565 bgp_vpn_rd_print(ndo
, pptr
+4),
1566 isonsap_string(ndo
, addr
,(plen
+ 7) / 8),
1568 GET_BE_U_3(pptr
+ 1)>>4,
1569 ((GET_U_1(pptr
+ 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1571 return 12 + (plen
+ 7) / 8;
1575 * bgp_attr_get_as_size
1577 * Try to find the size of the ASs encoded in an as-path. It is not obvious, as
1578 * both Old speakers that do not support 4 byte AS, and the new speakers that do
1579 * support, exchange AS-Path with the same path-attribute type value 0x02.
1582 bgp_attr_get_as_size(netdissect_options
*ndo
,
1583 uint8_t bgpa_type
, const u_char
*pptr
, u_int len
)
1585 const u_char
*tptr
= pptr
;
1588 * If the path attribute is the optional AS4 path type, then we already
1589 * know, that ASs must be encoded in 4 byte format.
1591 if (bgpa_type
== BGPTYPE_AS4_PATH
) {
1596 * Let us assume that ASs are of 2 bytes in size, and check if the AS-Path
1597 * TLV is good. If not, ask the caller to try with AS encoded as 4 bytes
1600 while (tptr
< pptr
+ len
) {
1602 * If we do not find a valid segment type, our guess might be wrong.
1604 if (GET_U_1(tptr
) < BGP_AS_SEG_TYPE_MIN
|| GET_U_1(tptr
) > BGP_AS_SEG_TYPE_MAX
) {
1607 tptr
+= 2 + GET_U_1(tptr
+ 1) * 2;
1611 * If we correctly reached end of the AS path attribute data content,
1612 * then most likely ASs were indeed encoded as 2 bytes.
1614 if (tptr
== pptr
+ len
) {
1621 * We can come here, either we did not have enough data, or if we
1622 * try to decode 4 byte ASs in 2 byte format. Either way, return 4,
1623 * so that calller can try to decode each AS as of 4 bytes. If indeed
1624 * there was not enough data, it will crib and end the parse anyways.
1630 * The only way to know that a BGP UPDATE message is using add path is
1631 * by checking if the capability is in the OPEN message which we may have missed.
1632 * So this function checks if it is possible that the update could contain add path
1633 * and if so it checks that standard BGP doesn't make sense.
1636 check_add_path(netdissect_options
*ndo
, const u_char
*pptr
, u_int length
,
1637 u_int max_prefix_length
)
1639 u_int offset
, prefix_length
;
1646 * Scan through the NLRI information under the assumpetion that
1647 * it doesn't have path IDs.
1649 for (offset
= 0; offset
< length
;) {
1651 if (!ND_TTEST_1(pptr
+ offset
)) {
1652 /* We ran out of captured data; quit scanning. */
1655 prefix_length
= GET_U_1(pptr
+ offset
);
1657 * Add 4 to cover the path id
1658 * and check the prefix length isn't greater than 32/128.
1660 if (prefix_length
> max_prefix_length
) {
1663 /* Add 1 for the prefix_length byte and prefix_length to cover the address */
1664 offset
+= 1 + ((prefix_length
+ 7) / 8);
1666 /* check we haven't gone past the end of the section */
1667 if (offset
> length
) {
1671 /* check it's not standard BGP */
1672 for (offset
= 0; offset
< length
; ) {
1673 if (!ND_TTEST_1(pptr
+ offset
)) {
1674 /* We ran out of captured data; quit scanning. */
1677 prefix_length
= GET_U_1(pptr
+ offset
);
1679 * If the prefix_length is zero (0.0.0.0/0)
1680 * and since it's not the only address (length >= 5)
1681 * then it is add-path
1683 if (prefix_length
< 1 || prefix_length
> max_prefix_length
) {
1686 offset
+= 1 + ((prefix_length
+ 7) / 8);
1688 if (offset
> length
) {
1692 /* assume not add-path by default */
1697 bgp_mp_af_print(netdissect_options
*ndo
,
1698 const u_char
*tptr
, u_int tlen
,
1699 uint16_t *afp
, uint8_t *safip
)
1704 af
= GET_BE_U_2(tptr
);
1706 safi
= GET_U_1(tptr
+ 2);
1709 ND_PRINT("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
1710 tok2str(af_values
, "Unknown AFI", af
),
1712 (safi
>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1713 tok2str(bgp_safi_values
, "Unknown SAFI", safi
),
1716 switch(af
<<8 | safi
) {
1717 case (AFNUM_INET
<<8 | SAFNUM_UNICAST
):
1718 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST
):
1719 case (AFNUM_INET
<<8 | SAFNUM_UNIMULTICAST
):
1720 case (AFNUM_INET
<<8 | SAFNUM_LABUNICAST
):
1721 case (AFNUM_INET
<<8 | SAFNUM_RT_ROUTING_INFO
):
1722 case (AFNUM_INET
<<8 | SAFNUM_VPNUNICAST
):
1723 case (AFNUM_INET
<<8 | SAFNUM_VPNMULTICAST
):
1724 case (AFNUM_INET
<<8 | SAFNUM_VPNUNIMULTICAST
):
1725 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST_VPN
):
1726 case (AFNUM_INET
<<8 | SAFNUM_MDT
):
1727 case (AFNUM_INET6
<<8 | SAFNUM_UNICAST
):
1728 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST
):
1729 case (AFNUM_INET6
<<8 | SAFNUM_UNIMULTICAST
):
1730 case (AFNUM_INET6
<<8 | SAFNUM_LABUNICAST
):
1731 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNICAST
):
1732 case (AFNUM_INET6
<<8 | SAFNUM_VPNMULTICAST
):
1733 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNIMULTICAST
):
1734 case (AFNUM_NSAP
<<8 | SAFNUM_UNICAST
):
1735 case (AFNUM_NSAP
<<8 | SAFNUM_MULTICAST
):
1736 case (AFNUM_NSAP
<<8 | SAFNUM_UNIMULTICAST
):
1737 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNICAST
):
1738 case (AFNUM_NSAP
<<8 | SAFNUM_VPNMULTICAST
):
1739 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNIMULTICAST
):
1740 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNICAST
):
1741 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNMULTICAST
):
1742 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNIMULTICAST
):
1743 case (AFNUM_VPLS
<<8 | SAFNUM_VPLS
):
1746 ND_TCHECK_LEN(tptr
, tlen
);
1747 ND_PRINT("\n\t no AFI %u / SAFI %u decoder", af
, safi
);
1748 if (ndo
->ndo_vflag
<= 1)
1749 print_unknown_data(ndo
, tptr
, "\n\t ", tlen
);
1758 bgp_nlri_print(netdissect_options
*ndo
, uint16_t af
, uint8_t safi
,
1759 const u_char
*tptr
, u_int len
,
1760 char *buf
, size_t buflen
,
1761 int add_path4
, int add_path6
)
1766 switch (af
<<8 | safi
) {
1767 case (AFNUM_INET
<<8 | SAFNUM_UNICAST
):
1768 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST
):
1769 case (AFNUM_INET
<<8 | SAFNUM_UNIMULTICAST
):
1771 path_id
= GET_BE_U_4(tptr
);
1774 advance
= decode_prefix4(ndo
, tptr
, len
, buf
, buflen
);
1776 ND_PRINT("\n\t (illegal prefix length)");
1777 else if (advance
== -2)
1778 break; /* bytes left, but not enough */
1780 ND_PRINT("\n\t %s", buf
);
1782 ND_PRINT(" Path Id: %u", path_id
);
1786 case (AFNUM_INET
<<8 | SAFNUM_LABUNICAST
):
1787 advance
= decode_labeled_prefix4(ndo
, tptr
, len
, buf
, buflen
);
1789 ND_PRINT("\n\t (illegal prefix length)");
1790 else if (advance
== -2)
1792 else if (advance
== -3)
1793 break; /* bytes left, but not enough */
1795 ND_PRINT("\n\t %s", buf
);
1797 case (AFNUM_INET
<<8 | SAFNUM_VPNUNICAST
):
1798 case (AFNUM_INET
<<8 | SAFNUM_VPNMULTICAST
):
1799 case (AFNUM_INET
<<8 | SAFNUM_VPNUNIMULTICAST
):
1800 advance
= decode_labeled_vpn_prefix4(ndo
, tptr
, buf
, buflen
);
1802 ND_PRINT("\n\t (illegal prefix length)");
1804 ND_PRINT("\n\t %s", buf
);
1806 case (AFNUM_INET
<<8 | SAFNUM_RT_ROUTING_INFO
):
1807 advance
= decode_rt_routing_info(ndo
, tptr
);
1809 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST_VPN
): /* fall through */
1810 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST_VPN
):
1811 advance
= decode_multicast_vpn(ndo
, tptr
, buf
, buflen
);
1813 ND_PRINT("\n\t (illegal prefix length)");
1814 else if (advance
== -2)
1817 ND_PRINT("\n\t %s", buf
);
1820 case (AFNUM_INET
<<8 | SAFNUM_MDT
):
1821 advance
= decode_mdt_vpn_nlri(ndo
, tptr
, buf
, buflen
);
1823 ND_PRINT("\n\t (illegal prefix length)");
1824 else if (advance
== -2)
1827 ND_PRINT("\n\t %s", buf
);
1829 case (AFNUM_INET6
<<8 | SAFNUM_UNICAST
):
1830 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST
):
1831 case (AFNUM_INET6
<<8 | SAFNUM_UNIMULTICAST
):
1833 path_id
= GET_BE_U_4(tptr
);
1836 advance
= decode_prefix6(ndo
, tptr
, len
, buf
, buflen
);
1838 ND_PRINT("\n\t (illegal prefix length)");
1839 else if (advance
== -2)
1840 break; /* bytes left, but not enough */
1842 ND_PRINT("\n\t %s", buf
);
1844 ND_PRINT(" Path Id: %u", path_id
);
1848 case (AFNUM_INET6
<<8 | SAFNUM_LABUNICAST
):
1849 advance
= decode_labeled_prefix6(ndo
, tptr
, len
, buf
, buflen
);
1851 ND_PRINT("\n\t (illegal prefix length)");
1852 else if (advance
== -2)
1854 else if (advance
== -3)
1855 break; /* bytes left, but not enough */
1857 ND_PRINT("\n\t %s", buf
);
1859 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNICAST
):
1860 case (AFNUM_INET6
<<8 | SAFNUM_VPNMULTICAST
):
1861 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNIMULTICAST
):
1862 advance
= decode_labeled_vpn_prefix6(ndo
, tptr
, buf
, buflen
);
1864 ND_PRINT("\n\t (illegal prefix length)");
1866 ND_PRINT("\n\t %s", buf
);
1868 case (AFNUM_VPLS
<<8 | SAFNUM_VPLS
):
1869 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNICAST
):
1870 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNMULTICAST
):
1871 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNIMULTICAST
):
1872 advance
= decode_labeled_vpn_l2(ndo
, tptr
, buf
, buflen
);
1874 ND_PRINT("\n\t (illegal length)");
1875 else if (advance
== -2)
1878 ND_PRINT("\n\t %s", buf
);
1880 case (AFNUM_NSAP
<<8 | SAFNUM_UNICAST
):
1881 case (AFNUM_NSAP
<<8 | SAFNUM_MULTICAST
):
1882 case (AFNUM_NSAP
<<8 | SAFNUM_UNIMULTICAST
):
1883 advance
= decode_clnp_prefix(ndo
, tptr
, buf
, buflen
);
1885 ND_PRINT("\n\t (illegal prefix length)");
1887 ND_PRINT("\n\t %s", buf
);
1889 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNICAST
):
1890 case (AFNUM_NSAP
<<8 | SAFNUM_VPNMULTICAST
):
1891 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNIMULTICAST
):
1892 advance
= decode_labeled_vpn_clnp_prefix(ndo
, tptr
, buf
, buflen
);
1894 ND_PRINT("\n\t (illegal prefix length)");
1896 ND_PRINT("\n\t %s", buf
);
1900 * This should not happen, we should have been protected
1901 * by bgp_mp_af_print()'s return value.
1903 ND_PRINT("\n\t ERROR: no AFI %u / SAFI %u decoder", af
, safi
);
1908 trunc
: /* we rely on the caller to recognize -2 return value */
1912 static const struct tok bgp_flags
[] = {
1921 bgp_attr_print(netdissect_options
*ndo
,
1922 uint8_t atype
, const u_char
*pptr
, u_int len
,
1923 const unsigned attr_set_level
)
1925 /* allocate space for the largest possible string */
1926 char astostr
[AS_STR_SIZE
];
1929 uint8_t safi
, snpa
, nhlen
;
1933 char buf
[MAXHOSTNAMELEN
+ 100];
1935 int add_path4
, add_path6
;
1942 case BGPTYPE_ORIGIN
:
1944 ND_PRINT("invalid len");
1946 ND_PRINT("%s", tok2str(bgp_origin_values
,
1947 "Unknown Origin Typecode",
1953 * Process AS4 byte path and AS2 byte path attributes here.
1955 case BGPTYPE_AS4_PATH
:
1956 case BGPTYPE_AS_PATH
:
1958 ND_PRINT("invalid len");
1967 * BGP updates exchanged between New speakers that support 4
1968 * byte AS, ASs are always encoded in 4 bytes. There is no
1969 * definitive way to find this, just by the packet's
1970 * contents. So, check for packet's TLV's sanity assuming
1971 * 2 bytes first, and it does not pass, assume that ASs are
1972 * encoded in 4 bytes format and move on.
1974 as_size
= bgp_attr_get_as_size(ndo
, atype
, pptr
, len
);
1976 while (tptr
< pptr
+ len
) {
1977 ND_PRINT("%s", tok2str(bgp_as_path_segment_open_values
,
1978 "?", GET_U_1(tptr
)));
1979 for (i
= 0; i
< GET_U_1(tptr
+ 1) * as_size
; i
+= as_size
) {
1980 ND_TCHECK_LEN(tptr
+ 2 + i
, as_size
);
1982 as_printf(ndo
, astostr
, sizeof(astostr
),
1984 GET_BE_U_2(tptr
+ i
+ 2) :
1985 GET_BE_U_4(tptr
+ i
+ 2)));
1987 ND_PRINT("%s", tok2str(bgp_as_path_segment_close_values
,
1988 "?", GET_U_1(tptr
)));
1989 tptr
+= 2 + GET_U_1(tptr
+ 1) * as_size
;
1992 case BGPTYPE_NEXT_HOP
:
1994 ND_PRINT("invalid len");
1996 ND_PRINT("%s", GET_IPADDR_STRING(tptr
));
1999 case BGPTYPE_MULTI_EXIT_DISC
:
2000 case BGPTYPE_LOCAL_PREF
:
2002 ND_PRINT("invalid len");
2004 ND_PRINT("%u", GET_BE_U_4(tptr
));
2007 case BGPTYPE_ATOMIC_AGGREGATE
:
2009 ND_PRINT("invalid len");
2011 case BGPTYPE_AGGREGATOR
:
2014 * Depending on the AS encoded is of 2 bytes or of 4 bytes,
2015 * the length of this PA can be either 6 bytes or 8 bytes.
2017 if (len
!= 6 && len
!= 8) {
2018 ND_PRINT("invalid len");
2021 ND_TCHECK_LEN(tptr
, len
);
2023 ND_PRINT(" AS #%s, origin %s",
2024 as_printf(ndo
, astostr
, sizeof(astostr
), GET_BE_U_2(tptr
)),
2025 GET_IPADDR_STRING(tptr
+ 2));
2027 ND_PRINT(" AS #%s, origin %s",
2028 as_printf(ndo
, astostr
, sizeof(astostr
),
2029 GET_BE_U_4(tptr
)), GET_IPADDR_STRING(tptr
+ 4));
2032 case BGPTYPE_AGGREGATOR4
:
2034 ND_PRINT("invalid len");
2037 ND_PRINT(" AS #%s, origin %s",
2038 as_printf(ndo
, astostr
, sizeof(astostr
), GET_BE_U_4(tptr
)),
2039 GET_IPADDR_STRING(tptr
+ 4));
2041 case BGPTYPE_COMMUNITIES
:
2043 ND_PRINT("invalid len");
2051 comm
= GET_BE_U_4(tptr
);
2053 case BGP_COMMUNITY_NO_EXPORT
:
2054 ND_PRINT(" NO_EXPORT");
2056 case BGP_COMMUNITY_NO_ADVERT
:
2057 ND_PRINT(" NO_ADVERTISE");
2059 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED
:
2060 ND_PRINT(" NO_EXPORT_SUBCONFED");
2064 (comm
>> 16) & 0xffff,
2066 (tlen
>4) ? ", " : "");
2073 case BGPTYPE_ORIGINATOR_ID
:
2075 ND_PRINT("invalid len");
2078 ND_PRINT("%s",GET_IPADDR_STRING(tptr
));
2080 case BGPTYPE_CLUSTER_LIST
:
2082 ND_PRINT("invalid len");
2089 GET_IPADDR_STRING(tptr
),
2090 (tlen
>4) ? ", " : "");
2095 case BGPTYPE_MP_REACH_NLRI
:
2099 ret
= bgp_mp_af_print(ndo
, tptr
, tlen
, &af
, &safi
);
2111 nhlen
= GET_U_1(tptr
);
2117 uint8_t tnhlen
= nhlen
;
2120 ND_PRINT("\n\t nexthop: ");
2121 while (tnhlen
!= 0) {
2125 switch(af
<<8 | safi
) {
2126 case (AFNUM_INET
<<8 | SAFNUM_UNICAST
):
2127 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST
):
2128 case (AFNUM_INET
<<8 | SAFNUM_UNIMULTICAST
):
2129 case (AFNUM_INET
<<8 | SAFNUM_LABUNICAST
):
2130 case (AFNUM_INET
<<8 | SAFNUM_RT_ROUTING_INFO
):
2131 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST_VPN
):
2132 case (AFNUM_INET
<<8 | SAFNUM_MDT
):
2133 if (tnhlen
< sizeof(nd_ipv4
)) {
2134 ND_PRINT("invalid len");
2139 ND_PRINT("%s",GET_IPADDR_STRING(tptr
));
2140 tptr
+= sizeof(nd_ipv4
);
2141 tnhlen
-= sizeof(nd_ipv4
);
2142 tlen
-= sizeof(nd_ipv4
);
2145 case (AFNUM_INET
<<8 | SAFNUM_VPNUNICAST
):
2146 case (AFNUM_INET
<<8 | SAFNUM_VPNMULTICAST
):
2147 case (AFNUM_INET
<<8 | SAFNUM_VPNUNIMULTICAST
):
2148 if (tnhlen
< sizeof(nd_ipv4
)+BGP_VPN_RD_LEN
) {
2149 ND_PRINT("invalid len");
2154 ND_PRINT("RD: %s, %s",
2155 bgp_vpn_rd_print(ndo
, tptr
),
2156 GET_IPADDR_STRING(tptr
+BGP_VPN_RD_LEN
));
2157 tptr
+= (sizeof(nd_ipv4
)+BGP_VPN_RD_LEN
);
2158 tlen
-= (sizeof(nd_ipv4
)+BGP_VPN_RD_LEN
);
2159 tnhlen
-= (sizeof(nd_ipv4
)+BGP_VPN_RD_LEN
);
2162 case (AFNUM_INET6
<<8 | SAFNUM_UNICAST
):
2163 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST
):
2164 case (AFNUM_INET6
<<8 | SAFNUM_UNIMULTICAST
):
2165 case (AFNUM_INET6
<<8 | SAFNUM_LABUNICAST
):
2166 if (tnhlen
< sizeof(nd_ipv6
)) {
2167 ND_PRINT("invalid len");
2172 ND_PRINT("%s", GET_IP6ADDR_STRING(tptr
));
2173 tptr
+= sizeof(nd_ipv6
);
2174 tlen
-= sizeof(nd_ipv6
);
2175 tnhlen
-= sizeof(nd_ipv6
);
2178 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNICAST
):
2179 case (AFNUM_INET6
<<8 | SAFNUM_VPNMULTICAST
):
2180 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNIMULTICAST
):
2181 if (tnhlen
< sizeof(nd_ipv6
)+BGP_VPN_RD_LEN
) {
2182 ND_PRINT("invalid len");
2187 ND_PRINT("RD: %s, %s",
2188 bgp_vpn_rd_print(ndo
, tptr
),
2189 GET_IP6ADDR_STRING(tptr
+BGP_VPN_RD_LEN
));
2190 tptr
+= (sizeof(nd_ipv6
)+BGP_VPN_RD_LEN
);
2191 tlen
-= (sizeof(nd_ipv6
)+BGP_VPN_RD_LEN
);
2192 tnhlen
-= (sizeof(nd_ipv6
)+BGP_VPN_RD_LEN
);
2195 case (AFNUM_VPLS
<<8 | SAFNUM_VPLS
):
2196 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNICAST
):
2197 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNMULTICAST
):
2198 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNIMULTICAST
):
2199 if (tnhlen
< sizeof(nd_ipv4
)) {
2200 ND_PRINT("invalid len");
2205 ND_PRINT("%s", GET_IPADDR_STRING(tptr
));
2206 tptr
+= (sizeof(nd_ipv4
));
2207 tlen
-= (sizeof(nd_ipv4
));
2208 tnhlen
-= (sizeof(nd_ipv4
));
2211 case (AFNUM_NSAP
<<8 | SAFNUM_UNICAST
):
2212 case (AFNUM_NSAP
<<8 | SAFNUM_MULTICAST
):
2213 case (AFNUM_NSAP
<<8 | SAFNUM_UNIMULTICAST
):
2214 ND_PRINT("%s", GET_ISONSAP_STRING(tptr
, tnhlen
));
2220 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNICAST
):
2221 case (AFNUM_NSAP
<<8 | SAFNUM_VPNMULTICAST
):
2222 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNIMULTICAST
):
2223 if (tnhlen
< BGP_VPN_RD_LEN
+1) {
2224 ND_PRINT("invalid len");
2229 ND_TCHECK_LEN(tptr
, tnhlen
);
2230 ND_PRINT("RD: %s, %s",
2231 bgp_vpn_rd_print(ndo
, tptr
),
2232 GET_ISONSAP_STRING(tptr
+BGP_VPN_RD_LEN
,tnhlen
-BGP_VPN_RD_LEN
));
2233 /* rfc986 mapped IPv4 address ? */
2234 if (GET_BE_U_4(tptr
+ BGP_VPN_RD_LEN
) == 0x47000601)
2235 ND_PRINT(" = %s", GET_IPADDR_STRING(tptr
+BGP_VPN_RD_LEN
+4));
2236 /* rfc1888 mapped IPv6 address ? */
2237 else if (GET_BE_U_3(tptr
+ BGP_VPN_RD_LEN
) == 0x350000)
2238 ND_PRINT(" = %s", GET_IP6ADDR_STRING(tptr
+BGP_VPN_RD_LEN
+3));
2246 * bgp_mp_af_print() should have saved us from
2247 * an unsupported AFI/SAFI.
2249 ND_PRINT("ERROR: no AFI %u/SAFI %u nexthop decoder", af
, safi
);
2258 ND_PRINT(", nh-length: %u", nhlen
);
2260 /* As per RFC 2858; this is reserved in RFC 4760 */
2263 snpa
= GET_U_1(tptr
);
2268 ND_PRINT("\n\t %u SNPA", snpa
);
2269 for (/*nothing*/; snpa
!= 0; snpa
--) {
2273 snpalen
= GET_U_1(tptr
);
2274 ND_PRINT("\n\t %u bytes", snpalen
);
2279 ND_TCHECK_LEN(tptr
, snpalen
);
2284 ND_PRINT(", no SNPA");
2287 add_path4
= check_add_path(ndo
, tptr
, (len
-ND_BYTES_BETWEEN(tptr
, pptr
)), 32);
2288 add_path6
= check_add_path(ndo
, tptr
, (len
-ND_BYTES_BETWEEN(tptr
, pptr
)), 128);
2290 while (tptr
< pptr
+ len
) {
2291 advance
= bgp_nlri_print(ndo
, af
, safi
, tptr
, len
, buf
, sizeof(buf
),
2292 add_path4
, add_path6
);
2301 case BGPTYPE_MP_UNREACH_NLRI
:
2302 ND_TCHECK_LEN(tptr
, BGP_MP_NLRI_MINSIZE
);
2303 ret
= bgp_mp_af_print(ndo
, tptr
, tlen
, &af
, &safi
);
2309 if (len
== BGP_MP_NLRI_MINSIZE
)
2310 ND_PRINT("\n\t End-of-Rib Marker (empty NLRI)");
2314 add_path4
= check_add_path(ndo
, tptr
, (len
-ND_BYTES_BETWEEN(tptr
, pptr
)), 32);
2315 add_path6
= check_add_path(ndo
, tptr
, (len
-ND_BYTES_BETWEEN(tptr
, pptr
)), 128);
2317 while (tptr
< pptr
+ len
) {
2318 advance
= bgp_nlri_print(ndo
, af
, safi
, tptr
, len
, buf
, sizeof(buf
),
2319 add_path4
, add_path6
);
2327 case BGPTYPE_EXTD_COMMUNITIES
:
2329 ND_PRINT("invalid len");
2338 extd_comm
=GET_BE_U_2(tptr
);
2340 ND_PRINT("\n\t %s (0x%04x), Flags [%s]",
2341 tok2str(bgp_extd_comm_subtype_values
,
2342 "unknown extd community typecode",
2345 bittok2str(bgp_extd_comm_flag_values
, "none", extd_comm
));
2351 bgp_extended_community_print(ndo
, tptr
);
2357 case BGPTYPE_PMSI_TUNNEL
:
2359 uint8_t tunnel_type
, flags
;
2364 flags
= GET_U_1(tptr
);
2365 tunnel_type
= GET_U_1(tptr
+ 1);
2367 ND_PRINT("\n\t Tunnel-type %s (%u), Flags [%s], MPLS Label %u",
2368 tok2str(bgp_pmsi_tunnel_values
, "Unknown", tunnel_type
),
2370 bittok2str(bgp_pmsi_flag_values
, "none", flags
),
2371 GET_BE_U_3(tptr
+ 2)>>4);
2376 switch (tunnel_type
) {
2377 case BGP_PMSI_TUNNEL_PIM_SM
: /* fall through */
2378 case BGP_PMSI_TUNNEL_PIM_BIDIR
:
2379 ND_PRINT("\n\t Sender %s, P-Group %s",
2380 GET_IPADDR_STRING(tptr
),
2381 GET_IPADDR_STRING(tptr
+4));
2384 case BGP_PMSI_TUNNEL_PIM_SSM
:
2385 ND_PRINT("\n\t Root-Node %s, P-Group %s",
2386 GET_IPADDR_STRING(tptr
),
2387 GET_IPADDR_STRING(tptr
+4));
2389 case BGP_PMSI_TUNNEL_INGRESS
:
2390 ND_PRINT("\n\t Tunnel-Endpoint %s",
2391 GET_IPADDR_STRING(tptr
));
2393 case BGP_PMSI_TUNNEL_LDP_P2MP
: /* fall through */
2394 case BGP_PMSI_TUNNEL_LDP_MP2MP
:
2395 ND_PRINT("\n\t Root-Node %s, LSP-ID 0x%08x",
2396 GET_IPADDR_STRING(tptr
),
2397 GET_BE_U_4(tptr
+ 4));
2399 case BGP_PMSI_TUNNEL_RSVP_P2MP
:
2400 ND_PRINT("\n\t Extended-Tunnel-ID %s, P2MP-ID 0x%08x",
2401 GET_IPADDR_STRING(tptr
),
2402 GET_BE_U_4(tptr
+ 4));
2405 if (ndo
->ndo_vflag
<= 1) {
2406 print_unknown_data(ndo
, tptr
, "\n\t ", tlen
);
2417 type
= GET_U_1(tptr
);
2418 length
= GET_BE_U_2(tptr
+ 1);
2422 ND_PRINT("\n\t %s TLV (%u), length %u",
2423 tok2str(bgp_aigp_values
, "Unknown", type
),
2431 * Check if we can read the TLV data.
2441 ND_PRINT(", metric %" PRIu64
,
2446 if (ndo
->ndo_vflag
<= 1) {
2447 print_unknown_data(ndo
, tptr
,"\n\t ", length
);
2456 case BGPTYPE_ATTR_SET
:
2460 ND_PRINT("\n\t Origin AS: %s",
2461 as_printf(ndo
, astostr
, sizeof(astostr
), GET_BE_U_4(tptr
)));
2466 u_int aflags
, alenlen
, alen
;
2470 ND_PRINT(" [path attr too short]");
2474 aflags
= GET_U_1(tptr
);
2475 atype
= GET_U_1(tptr
+ 1);
2478 alenlen
= bgp_attr_lenlen(aflags
, tptr
);
2479 ND_TCHECK_LEN(tptr
, alenlen
);
2480 if (len
< alenlen
) {
2481 ND_PRINT(" [path attr too short]");
2485 alen
= bgp_attr_len(aflags
, tptr
);
2489 ND_PRINT("\n\t %s (%u), length: %u",
2490 tok2str(bgp_attr_values
,
2491 "Unknown Attribute", atype
),
2496 ND_PRINT(", Flags [%s",
2497 bittok2str_nosep(bgp_flags
, "", aflags
));
2499 ND_PRINT("+%x", aflags
& 0xf);
2504 ND_PRINT(" [path attr too short]");
2509 * The protocol encoding per se allows ATTR_SET to be nested
2510 * as many times as the message can accommodate. This printer
2511 * used to be able to recurse into ATTR_SET contents until the
2512 * stack exhaustion, but now there is a limit on that (if live
2513 * protocol exchange goes that many levels deep, something is
2514 * probably wrong anyway). Feel free to refine this value if
2515 * you can find the spec with respective normative text.
2517 if (attr_set_level
== 10)
2518 ND_PRINT("(too many nested levels, not recursing)");
2519 else if (!bgp_attr_print(ndo
, atype
, tptr
, alen
, attr_set_level
+ 1))
2526 case BGPTYPE_LARGE_COMMUNITY
:
2527 if (len
== 0 || len
% 12) {
2528 ND_PRINT("invalid len");
2533 ND_PRINT("%u:%u:%u%s",
2535 GET_BE_U_4(tptr
+ 4),
2536 GET_BE_U_4(tptr
+ 8),
2537 (len
> 12) ? ", " : "");
2540 * len will always be a multiple of 12, as per the above,
2541 * so this will never underflow.
2547 ND_TCHECK_LEN(pptr
, len
);
2548 ND_PRINT("\n\t no Attribute %u decoder", atype
); /* we have no decoder for the attribute */
2549 if (ndo
->ndo_vflag
<= 1)
2550 print_unknown_data(ndo
, pptr
, "\n\t ", len
);
2554 if (ndo
->ndo_vflag
> 1 && len
) { /* omit zero length attributes*/
2555 ND_TCHECK_LEN(pptr
, len
);
2556 print_unknown_data(ndo
, pptr
, "\n\t ", len
);
2565 bgp_capabilities_print(netdissect_options
*ndo
,
2566 const u_char
*opt
, u_int caps_len
)
2568 /* allocate space for the largest possible string */
2569 char astostr
[AS_STR_SIZE
];
2570 u_int cap_type
, cap_len
, tcap_len
, cap_offset
;
2573 while (i
< caps_len
) {
2574 ND_TCHECK_LEN(opt
+ i
, BGP_CAP_HEADER_SIZE
);
2575 cap_type
=GET_U_1(opt
+ i
);
2576 cap_len
=GET_U_1(opt
+ i
+ 1);
2577 ND_PRINT("\n\t %s (%u), length: %u",
2578 tok2str(bgp_capcode_values
, "Unknown", cap_type
),
2581 ND_TCHECK_LEN(opt
+ 2 + i
, cap_len
);
2583 case BGP_CAPCODE_MP
:
2584 /* AFI (16 bits), Reserved (8 bits), SAFI (8 bits) */
2586 ND_PRINT(" (too short, < 4)");
2589 ND_PRINT("\n\t\tAFI %s (%u), SAFI %s (%u)",
2590 tok2str(af_values
, "Unknown", GET_BE_U_2(opt
+ i
+ 2)),
2591 GET_BE_U_2(opt
+ i
+ 2),
2592 tok2str(bgp_safi_values
, "Unknown", GET_U_1(opt
+ i
+ 5)),
2593 GET_U_1(opt
+ i
+ 5));
2595 case BGP_CAPCODE_ML
:
2598 while (tcap_len
>= 4) {
2599 ND_PRINT( "\n\t\tAFI %s (%u), SAFI %s (%u), Count: %u",
2600 tok2str(af_values
, "Unknown",
2601 GET_BE_U_2(opt
+ i
+ cap_offset
)),
2602 GET_BE_U_2(opt
+ i
+ cap_offset
),
2603 tok2str(bgp_safi_values
, "Unknown",
2604 GET_U_1(opt
+ i
+ cap_offset
+ 2)),
2605 GET_U_1(opt
+ i
+ cap_offset
+ 2),
2606 GET_U_1(opt
+ i
+ cap_offset
+ 3));
2611 case BGP_CAPCODE_RESTART
:
2612 /* Restart Flags (4 bits), Restart Time in seconds (12 bits) */
2614 ND_PRINT(" (too short, < 2)");
2618 ND_PRINT("\n\t\tRestart Flags: [%s], Restart Time %us",
2619 bittok2str(bgp_graceful_restart_comm_flag_values
,
2620 "none", GET_U_1(opt
+ i
+ 2) >> 4),
2621 GET_BE_U_2(opt
+ i
+ 2)&0xfff);
2624 while(tcap_len
>=4) {
2625 ND_PRINT("\n\t\t AFI %s (%u), SAFI %s (%u), Forwarding state preserved: %s",
2626 tok2str(af_values
,"Unknown",
2627 GET_BE_U_2(opt
+ i
+ cap_offset
)),
2628 GET_BE_U_2(opt
+ i
+ cap_offset
),
2629 tok2str(bgp_safi_values
,"Unknown",
2630 GET_U_1(opt
+ i
+ cap_offset
+ 2)),
2631 GET_U_1(opt
+ (i
+ cap_offset
+ 2)),
2632 ((GET_U_1(opt
+ (i
+ cap_offset
+ 3)))&0x80) ? "yes" : "no" );
2637 case BGP_CAPCODE_RR
:
2638 case BGP_CAPCODE_LLGR
:
2639 case BGP_CAPCODE_RR_CISCO
:
2640 case BGP_CAPCODE_EXT_MSG
:
2641 case BGP_CAPCODE_ENH_RR
:
2643 case BGP_CAPCODE_AS_NEW
:
2645 * Extract the 4 byte AS number encoded.
2648 ND_PRINT(" (too short, < 4)");
2651 ND_PRINT("\n\t\t 4 Byte AS %s",
2652 as_printf(ndo
, astostr
, sizeof(astostr
),
2653 GET_BE_U_4(opt
+ i
+ 2)));
2655 case BGP_CAPCODE_ADD_PATH
:
2657 ND_PRINT(" (bogus)"); /* length */
2662 while (tcap_len
!= 0) {
2664 nd_print_invalid(ndo
);
2667 ND_PRINT("\n\t\tAFI %s (%u), SAFI %s (%u), Send/Receive: %s",
2668 tok2str(af_values
,"Unknown",GET_BE_U_2(opt
+ i
+ cap_offset
)),
2669 GET_BE_U_2(opt
+ i
+ cap_offset
),
2670 tok2str(bgp_safi_values
,"Unknown",GET_U_1(opt
+ i
+ cap_offset
+ 2)),
2671 GET_U_1(opt
+ (i
+ cap_offset
+ 2)),
2672 tok2str(bgp_add_path_recvsend
,"Bogus (0x%02x)",GET_U_1(opt
+ i
+ cap_offset
+ 3))
2679 ND_PRINT("\n\t\tno decoder for Capability %u",
2681 if (ndo
->ndo_vflag
<= 1)
2682 print_unknown_data(ndo
, opt
+ i
+ 2, "\n\t\t",
2686 if (ndo
->ndo_vflag
> 1 && cap_len
!= 0) {
2687 print_unknown_data(ndo
, opt
+ i
+ 2, "\n\t\t", cap_len
);
2689 i
+= BGP_CAP_HEADER_SIZE
+ cap_len
;
2694 nd_print_trunc(ndo
);
2698 bgp_open_print(netdissect_options
*ndo
,
2699 const u_char
*dat
, u_int length
)
2701 /* allocate space for the largest possible string */
2702 char astostr
[AS_STR_SIZE
];
2703 const struct bgp_open
*bgp_open_header
;
2706 const struct bgp_opt
*bgpopt
;
2709 uint8_t extended_opt_params
= 0;
2710 u_int open_size
= BGP_OPEN_SIZE
;
2711 u_int opt_size
= BGP_OPT_SIZE
;
2713 ND_TCHECK_LEN(dat
, BGP_OPEN_SIZE
);
2714 if (length
< BGP_OPEN_SIZE
)
2717 bgp_open_header
= (const struct bgp_open
*)dat
;
2719 ND_PRINT("\n\t Version %u, ",
2720 GET_U_1(bgp_open_header
->bgpo_version
));
2721 ND_PRINT("my AS %s, ",
2722 as_printf(ndo
, astostr
, sizeof(astostr
), GET_BE_U_2(bgp_open_header
->bgpo_myas
)));
2723 ND_PRINT("Holdtime %us, ",
2724 GET_BE_U_2(bgp_open_header
->bgpo_holdtime
));
2725 ND_PRINT("ID %s", GET_IPADDR_STRING(bgp_open_header
->bgpo_id
));
2726 optslen
= GET_U_1(bgp_open_header
->bgpo_optlen
);
2727 opsttype
= GET_U_1(bgp_open_header
->bgpo_opttype
);
2728 if (opsttype
== BGP_OPEN_NON_EXT_OPT_TYPE_EXTENDED_LENGTH
) {
2729 optslen
= GET_BE_U_2(bgp_open_header
->bgpo_optlen_extended
);
2730 extended_opt_params
= 1;
2734 ND_PRINT("\n\t Optional parameters%s, length: %u",
2735 extended_opt_params
? " (Extended)" : "", optslen
);
2737 opt
= dat
+ open_size
;
2738 length
-= open_size
;
2741 while (i
< optslen
) {
2745 ND_TCHECK_LEN(opt
+ i
, opt_size
);
2746 if (length
< opt_size
+ i
)
2748 bgpopt
= (const struct bgp_opt
*)(opt
+ i
);
2749 opt_type
= GET_U_1(bgpopt
->bgpopt_type
);
2750 opt_len
= extended_opt_params
? GET_BE_U_2(bgpopt
->bgpopt_len
)
2751 : GET_U_1(bgpopt
->bgpopt_len
);
2752 if (opt_size
+ i
+ opt_len
> optslen
) {
2753 ND_PRINT("\n\t Option %u, length: %u, goes past the end of the options",
2758 ND_PRINT("\n\t Option %s (%u), length: %u",
2759 tok2str(bgp_opt_values
,"Unknown",opt_type
),
2763 /* now let's decode the options we know*/
2767 bgp_capabilities_print(ndo
, opt
+ opt_size
+ i
,
2773 ND_PRINT("\n\t no decoder for option %u",
2777 i
+= opt_size
+ opt_len
;
2781 nd_print_trunc(ndo
);
2785 bgp_update_print(netdissect_options
*ndo
,
2786 const u_char
*dat
, u_int length
)
2789 u_int withdrawn_routes_len
;
2790 char buf
[MAXHOSTNAMELEN
+ 100];
2797 ND_TCHECK_LEN(dat
, BGP_SIZE
);
2798 if (length
< BGP_SIZE
)
2803 /* Unfeasible routes */
2807 withdrawn_routes_len
= GET_BE_U_2(p
);
2810 if (withdrawn_routes_len
> 1) {
2812 * Without keeping state from the original NLRI message,
2813 * it's not possible to tell if this a v4 or v6 route,
2814 * so only try to decode it if we're not v6 enabled.
2816 ND_TCHECK_LEN(p
, withdrawn_routes_len
);
2817 if (length
< withdrawn_routes_len
)
2819 ND_PRINT("\n\t Withdrawn routes:");
2820 add_path
= check_add_path(ndo
, p
, withdrawn_routes_len
, 32);
2821 while (withdrawn_routes_len
!= 0) {
2823 if (withdrawn_routes_len
< 4) {
2824 p
+= withdrawn_routes_len
;
2825 length
-= withdrawn_routes_len
;
2828 path_id
= GET_BE_U_4(p
);
2831 withdrawn_routes_len
-= 4;
2833 wpfx
= decode_prefix4(ndo
, p
, withdrawn_routes_len
, buf
, sizeof(buf
));
2835 ND_PRINT("\n\t (illegal prefix length)");
2837 } else if (wpfx
== -2)
2838 goto trunc
; /* bytes left, but not enough */
2840 ND_PRINT("\n\t %s", buf
);
2842 ND_PRINT(" Path Id: %u", path_id
);
2846 withdrawn_routes_len
-= wpfx
;
2850 ND_TCHECK_LEN(p
, withdrawn_routes_len
);
2851 if (length
< withdrawn_routes_len
)
2853 p
+= withdrawn_routes_len
;
2854 length
-= withdrawn_routes_len
;
2860 len
= GET_BE_U_2(p
);
2864 if (withdrawn_routes_len
== 0 && len
== 0 && length
== 0) {
2865 /* No withdrawn routes, no path attributes, no NLRI */
2866 ND_PRINT("\n\t End-of-Rib Marker (empty NLRI)");
2871 /* Make sure the path attributes don't go past the end of the packet */
2874 /* do something more useful!*/
2876 uint8_t aflags
, atype
, alenlen
;
2883 ND_PRINT("\n\t [path attrs too short]");
2888 aflags
= GET_U_1(p
);
2889 atype
= GET_U_1(p
+ 1);
2893 alenlen
= bgp_attr_lenlen(aflags
, p
);
2894 ND_TCHECK_LEN(p
, alenlen
);
2895 if (length
< alenlen
)
2897 if (len
< alenlen
) {
2898 ND_PRINT("\n\t [path attrs too short]");
2903 alen
= bgp_attr_len(aflags
, p
);
2908 ND_PRINT("\n\t %s (%u), length: %u",
2909 tok2str(bgp_attr_values
, "Unknown Attribute", atype
),
2914 ND_PRINT(", Flags [%s",
2915 bittok2str_nosep(bgp_flags
, "", aflags
));
2917 ND_PRINT("+%x", aflags
& 0xf);
2921 ND_PRINT(" [path attrs too short]");
2928 if (!bgp_attr_print(ndo
, atype
, p
, alen
, 0))
2937 add_path
= check_add_path(ndo
, p
, length
, 32);
2938 ND_PRINT("\n\t Updated routes:");
2939 while (length
!= 0) {
2944 path_id
= GET_BE_U_4(p
);
2948 i
= decode_prefix4(ndo
, p
, length
, buf
, sizeof(buf
));
2950 ND_PRINT("\n\t (illegal prefix length)");
2953 goto trunc
; /* bytes left, but not enough */
2955 ND_PRINT("\n\t %s", buf
);
2957 ND_PRINT(" Path Id: %u", path_id
);
2966 nd_print_trunc(ndo
);
2970 bgp_notification_print_code(netdissect_options
*ndo
,
2971 const u_char
*dat
, u_int length
,
2972 uint8_t bgpn_major
, uint8_t bgpn_minor
)
2974 ND_PRINT(", %s (%u)",
2975 tok2str(bgp_notify_major_values
, "Unknown Error",
2979 switch (bgpn_major
) {
2981 case BGP_NOTIFY_MAJOR_MSG
:
2982 ND_PRINT(", subcode %s (%u)",
2983 tok2str(bgp_notify_minor_msg_values
, "Unknown",
2987 case BGP_NOTIFY_MAJOR_OPEN
:
2988 ND_PRINT(", subcode %s (%u)",
2989 tok2str(bgp_notify_minor_open_values
, "Unknown",
2993 case BGP_NOTIFY_MAJOR_UPDATE
:
2994 ND_PRINT(", subcode %s (%u)",
2995 tok2str(bgp_notify_minor_update_values
, "Unknown",
2999 case BGP_NOTIFY_MAJOR_FSM
:
3000 ND_PRINT(" subcode %s (%u)",
3001 tok2str(bgp_notify_minor_fsm_values
, "Unknown",
3005 case BGP_NOTIFY_MAJOR_CAP
:
3006 ND_PRINT(" subcode %s (%u)",
3007 tok2str(bgp_notify_minor_cap_values
, "Unknown",
3011 case BGP_NOTIFY_MAJOR_CEASE
:
3012 ND_PRINT(", subcode %s (%u)",
3013 tok2str(bgp_notify_minor_cease_values
, "Unknown",
3017 /* RFC 4486 mentions optionally 7 bytes
3018 * for the maxprefix subtype, which may contain AFI, SAFI and MAXPREFIXES
3020 if(bgpn_minor
== BGP_NOTIFY_MINOR_CEASE_MAXPRFX
&& length
>= 7) {
3021 ND_PRINT(", AFI %s (%u), SAFI %s (%u), Max Prefixes: %u",
3022 tok2str(af_values
, "Unknown", GET_BE_U_2(dat
)),
3024 tok2str(bgp_safi_values
, "Unknown", GET_U_1((dat
+ 2))),
3026 GET_BE_U_4(dat
+ 3));
3029 * RFC 9003 describes a method to send a communication
3030 * intended for human consumption regarding the Administrative Shutdown
3032 if ((bgpn_minor
== BGP_NOTIFY_MINOR_CEASE_SHUT
||
3033 bgpn_minor
== BGP_NOTIFY_MINOR_CEASE_RESET
) &&
3035 uint8_t shutdown_comm_length
= GET_U_1(dat
);
3036 uint8_t remainder_offset
= 0;
3037 /* garbage, hexdump it all */
3038 if (shutdown_comm_length
> length
- 1) {
3039 ND_PRINT(", invalid Shutdown Communication length");
3041 else if (shutdown_comm_length
== 0) {
3042 ND_PRINT(", empty Shutdown Communication");
3043 remainder_offset
+= 1;
3045 /* a proper shutdown communication */
3047 ND_PRINT(", Shutdown Communication (length: %u): \"", shutdown_comm_length
);
3048 nd_printjn(ndo
, dat
+1, shutdown_comm_length
);
3050 remainder_offset
+= shutdown_comm_length
+ 1;
3052 /* if there is trailing data, hexdump it */
3053 if(length
- remainder_offset
> 0) {
3054 ND_PRINT(", Data: (length: %u)", length
- remainder_offset
);
3055 hex_print(ndo
, "\n\t\t", dat
+ remainder_offset
, length
- remainder_offset
);
3059 * RFC8538 describes the Hard Reset cease subcode, which contains another
3060 * notification code and subcode.
3062 if (bgpn_minor
== BGP_NOTIFY_MINOR_CEASE_HARDRESET
&& length
>= 2) {
3063 bgpn_major
= GET_U_1(dat
++);
3064 bgpn_minor
= GET_U_1(dat
++);
3066 bgp_notification_print_code(ndo
, dat
, length
, bgpn_major
, bgpn_minor
);
3070 if (bgpn_minor
!= 0) {
3071 ND_PRINT(", subcode %u", bgpn_minor
);
3080 bgp_notification_print(netdissect_options
*ndo
,
3081 const u_char
*dat
, u_int length
)
3083 const struct bgp_notification
*bgp_notification_header
;
3084 uint8_t bgpn_major
, bgpn_minor
;
3086 ND_TCHECK_LEN(dat
, BGP_NOTIFICATION_SIZE
);
3087 if (length
<BGP_NOTIFICATION_SIZE
)
3090 bgp_notification_header
= (const struct bgp_notification
*)dat
;
3091 bgpn_major
= GET_U_1(bgp_notification_header
->bgpn_major
);
3092 bgpn_minor
= GET_U_1(bgp_notification_header
->bgpn_minor
);
3093 bgp_notification_print_code(ndo
, dat
+ BGP_NOTIFICATION_SIZE
,
3094 length
- BGP_NOTIFICATION_SIZE
, bgpn_major
, bgpn_minor
);
3097 nd_print_trunc(ndo
);
3101 bgp_route_refresh_print(netdissect_options
*ndo
,
3102 const u_char
*pptr
, u_int len
)
3104 const struct bgp_route_refresh
*bgp_route_refresh_header
;
3106 /* some little sanity checking */
3107 if (len
<BGP_ROUTE_REFRESH_SIZE
)
3110 bgp_route_refresh_header
= (const struct bgp_route_refresh
*)pptr
;
3112 ND_PRINT("\n\t AFI %s (%u), SAFI %s (%u), Subtype %s (%u)",
3113 tok2str(af_values
, "Unknown",
3114 GET_BE_U_2(bgp_route_refresh_header
->afi
)),
3115 GET_BE_U_2(bgp_route_refresh_header
->afi
),
3116 tok2str(bgp_safi_values
, "Unknown",
3117 GET_U_1(bgp_route_refresh_header
->safi
)),
3118 GET_U_1(bgp_route_refresh_header
->safi
),
3119 tok2str(bgp_route_refresh_subtype_values
, "Unknown",
3120 GET_U_1(bgp_route_refresh_header
->subtype
)),
3121 GET_U_1(bgp_route_refresh_header
->subtype
));
3124 if (len
>= BGP_ROUTE_REFRESH_SIZE_ORF
) {
3125 const struct bgp_route_refresh_orf
*orf_header
;
3128 (const struct bgp_route_refresh_orf
*)(pptr
+ BGP_ROUTE_REFRESH_SIZE
);
3130 ND_PRINT("\n\t ORF refresh %s (%u), ORF type %s (%u), ORF length %u",
3131 tok2str(bgp_orf_refresh_type
, "Unknown",
3132 GET_U_1(orf_header
->refresh
)),
3133 GET_U_1(orf_header
->refresh
),
3134 tok2str(bgp_orf_type
, "Unknown", GET_U_1(orf_header
->type
)),
3135 GET_U_1(orf_header
->type
), GET_BE_U_2(orf_header
->len
));
3138 if (ndo
->ndo_vflag
> 1) {
3139 ND_TCHECK_LEN(pptr
, len
);
3140 print_unknown_data(ndo
, pptr
, "\n\t ", len
);
3145 nd_print_trunc(ndo
);
3149 bgp_pdu_print(netdissect_options
*ndo
,
3150 const u_char
*dat
, u_int length
)
3152 const struct bgp
*bgp_header
;
3155 ND_TCHECK_LEN(dat
, BGP_SIZE
);
3156 bgp_header
= (const struct bgp
*)dat
;
3157 bgp_type
= GET_U_1(bgp_header
->bgp_type
);
3159 ND_PRINT("\n\t%s Message (%u), length: %u",
3160 tok2str(bgp_msg_values
, "Unknown", bgp_type
),
3166 bgp_open_print(ndo
, dat
, length
);
3169 bgp_update_print(ndo
, dat
, length
);
3171 case BGP_NOTIFICATION
:
3172 bgp_notification_print(ndo
, dat
, length
);
3176 case BGP_ROUTE_REFRESH
:
3177 bgp_route_refresh_print(ndo
, dat
, length
);
3180 /* we have no decoder for the BGP message */
3181 ND_TCHECK_LEN(dat
, length
);
3182 ND_PRINT("\n\t no Message %u decoder", bgp_type
);
3183 print_unknown_data(ndo
, dat
, "\n\t ", length
);
3188 nd_print_trunc(ndo
);
3193 bgp_print(netdissect_options
*ndo
,
3194 const u_char
*dat
, u_int length _U_
)
3197 const u_char
*ep
= ndo
->ndo_snapend
;
3198 const u_char
*start
;
3199 const u_char marker
[] = {
3200 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
3201 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
3203 const struct bgp
*bgp_header
;
3206 ndo
->ndo_protocol
= "bgp";
3209 if (ndo
->ndo_vflag
< 1) /* lets be less chatty */
3217 if (GET_U_1(p
) != 0xff) {
3222 if (!ND_TTEST_LEN(p
, sizeof(marker
)))
3224 if (memcmp(p
, marker
, sizeof(marker
)) != 0) {
3229 /* found BGP header */
3230 ND_TCHECK_LEN(p
, BGP_SIZE
);
3231 bgp_header
= (const struct bgp
*)p
;
3234 nd_print_trunc(ndo
);
3236 hlen
= GET_BE_U_2(bgp_header
->bgp_len
);
3237 if (hlen
< BGP_SIZE
) {
3238 ND_PRINT("\nmessage length %u < %u", hlen
, BGP_SIZE
);
3239 nd_print_invalid(ndo
);
3243 if (ND_TTEST_LEN(p
, hlen
)) {
3244 if (!bgp_pdu_print(ndo
, p
, hlen
))
3249 ND_PRINT("\n[|BGP %s]",
3250 tok2str(bgp_msg_values
,
3251 "Unknown Message Type",
3252 GET_U_1(bgp_header
->bgp_type
)));
3260 nd_print_trunc(ndo
);