]> The Tcpdump Group git mirrors - tcpdump/blob - print-bgp.c
Fix a typo
[tcpdump] / print-bgp.c
1 /*
2 * Copyright (C) 1999 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * Extensively modified by Hannes Gredler (hannes@gredler.at) for more
30 * complete BGP support.
31 */
32
33 /* \summary: Border Gateway Protocol (BGP) printer */
34
35 /* specification: RFC 4271 */
36
37 #include <config.h>
38
39 #include "netdissect-stdinc.h"
40
41 #include <stdio.h>
42 #include <string.h>
43
44 #include "netdissect.h"
45 #include "addrtoname.h"
46 #include "extract.h"
47 #include "af.h"
48 #include "l2vpn.h"
49
50 struct bgp {
51 nd_byte bgp_marker[16];
52 nd_uint16_t bgp_len;
53 nd_uint8_t bgp_type;
54 };
55 #define BGP_SIZE 19 /* unaligned */
56
57 #define BGP_OPEN 1
58 #define BGP_UPDATE 2
59 #define BGP_NOTIFICATION 3
60 #define BGP_KEEPALIVE 4
61 #define BGP_ROUTE_REFRESH 5
62
63 static const struct tok bgp_msg_values[] = {
64 { BGP_OPEN, "Open"},
65 { BGP_UPDATE, "Update"},
66 { BGP_NOTIFICATION, "Notification"},
67 { BGP_KEEPALIVE, "Keepalive"},
68 { BGP_ROUTE_REFRESH, "Route Refresh"},
69 { 0, NULL}
70 };
71
72 struct bgp_open {
73 nd_byte bgpo_marker[16];
74 nd_uint16_t bgpo_len;
75 nd_uint8_t bgpo_type;
76 nd_uint8_t bgpo_version;
77 nd_uint16_t bgpo_myas;
78 nd_uint16_t bgpo_holdtime;
79 nd_uint32_t bgpo_id;
80 nd_uint8_t bgpo_optlen;
81 nd_uint8_t bgpo_opttype; /* RFC9072 */
82 nd_uint16_t bgpo_optlen_extended; /* RFC9072 */
83 /* options should follow */
84 };
85 #define BGP_OPEN_SIZE 29 /* unaligned */
86
87 struct bgp_opt {
88 nd_uint8_t bgpopt_type;
89 nd_uint16_t bgpopt_len; /* Can be one or two bytes, depending on RFC9072 */
90 /* variable length */
91 };
92 #define BGP_OPT_SIZE 2 /* some compilers may pad to 4 bytes */
93 #define BGP_CAP_HEADER_SIZE 2 /* some compilers may pad to 4 bytes */
94
95 struct bgp_notification {
96 nd_byte bgpn_marker[16];
97 nd_uint16_t bgpn_len;
98 nd_uint8_t bgpn_type;
99 nd_uint8_t bgpn_major;
100 nd_uint8_t bgpn_minor;
101 };
102 #define BGP_NOTIFICATION_SIZE 21 /* unaligned */
103
104 struct bgp_route_refresh_orf {
105 nd_uint8_t refresh;
106 nd_uint8_t type;
107 nd_uint16_t len;
108 };
109
110 struct bgp_route_refresh {
111 nd_byte bgp_marker[16];
112 nd_uint16_t len;
113 nd_uint8_t type; /* No padding after this; afi is, in fact, not aligned */
114 nd_uint16_t afi;
115 nd_uint8_t subtype;
116 nd_uint8_t safi;
117 };
118
119 static const struct tok bgp_orf_refresh_type[] = {
120 { 1, "Immediate"},
121 { 2, "Defer"},
122 { 0, NULL }
123 };
124
125 static const struct tok bgp_orf_type[] = {
126 { 64, "Address Prefix ORF"},
127 { 65, "CP-ORF"},
128 { 0, NULL }
129 };
130
131 #define BGP_ROUTE_REFRESH_SIZE 23
132 #define BGP_ROUTE_REFRESH_SIZE_ORF BGP_ROUTE_REFRESH_SIZE + 5
133 #define BGP_ROUTE_REFRESH_SUBTYPE_NORMAL 0
134 #define BGP_ROUTE_REFRESH_SUBTYPE_BORR 1
135 #define BGP_ROUTE_REFRESH_SUBTYPE_EORR 2
136 #define BGP_ROUTE_REFRESH_SUBTYPE_RESERVED 255
137
138 static const struct tok bgp_route_refresh_subtype_values[] = {
139 {BGP_ROUTE_REFRESH_SUBTYPE_NORMAL, "Normal route refresh request"},
140 {BGP_ROUTE_REFRESH_SUBTYPE_BORR,
141 "Demarcation of the beginning of a route refresh"},
142 {BGP_ROUTE_REFRESH_SUBTYPE_EORR,
143 "Demarcation of the ending of a route refresh"},
144 {0, NULL}};
145
146 #define bgp_attr_lenlen(flags, p) \
147 (((flags) & 0x10) ? 2U : 1U)
148 #define bgp_attr_len(flags, p) \
149 (((flags) & 0x10) ? GET_BE_U_2(p) : GET_U_1(p))
150
151 #define BGPTYPE_ORIGIN 1
152 #define BGPTYPE_AS_PATH 2
153 #define BGPTYPE_NEXT_HOP 3
154 #define BGPTYPE_MULTI_EXIT_DISC 4
155 #define BGPTYPE_LOCAL_PREF 5
156 #define BGPTYPE_ATOMIC_AGGREGATE 6
157 #define BGPTYPE_AGGREGATOR 7
158 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
159 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC4456 */
160 #define BGPTYPE_CLUSTER_LIST 10 /* RFC4456 */
161 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC4760 */
162 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC4760 */
163 #define BGPTYPE_EXTD_COMMUNITIES 16 /* RFC4360 */
164 #define BGPTYPE_AS4_PATH 17 /* RFC6793 */
165 #define BGPTYPE_AGGREGATOR4 18 /* RFC6793 */
166 #define BGPTYPE_PMSI_TUNNEL 22 /* RFC6514 */
167 #define BGPTYPE_TUNNEL_ENCAP 23 /* RFC5512 */
168 #define BGPTYPE_TRAFFIC_ENG 24 /* RFC5543 */
169 #define BGPTYPE_IPV6_EXTD_COMMUNITIES 25 /* RFC5701 */
170 #define BGPTYPE_AIGP 26 /* RFC7311 */
171 #define BGPTYPE_PE_DISTINGUISHER_LABEL 27 /* RFC6514 */
172 #define BGPTYPE_ENTROPY_LABEL 28 /* RFC6790 */
173 #define BGPTYPE_LARGE_COMMUNITY 32 /* draft-ietf-idr-large-community-05 */
174 #define BGPTYPE_BGPSEC_PATH 33 /* RFC8205 */
175 #define BGPTYPE_OTC 35 /* RFC9234 */
176 #define BGPTYPE_ATTR_SET 128 /* RFC6368 */
177
178 #define BGP_MP_NLRI_MINSIZE 3 /* End of RIB Marker detection */
179
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_BGPSEC_PATH, "BGPsec Path"},
205 { BGPTYPE_OTC, "Only to Customer (OTC)"},
206 { BGPTYPE_ATTR_SET, "Attribute Set"},
207 { 255, "Reserved for development"},
208 { 0, NULL}
209 };
210
211 #define BGP_AS_SET 1
212 #define BGP_AS_SEQUENCE 2
213 #define BGP_CONFED_AS_SEQUENCE 3 /* draft-ietf-idr-rfc3065bis-01 */
214 #define BGP_CONFED_AS_SET 4 /* draft-ietf-idr-rfc3065bis-01 */
215
216 #define BGP_AS_SEG_TYPE_MIN BGP_AS_SET
217 #define BGP_AS_SEG_TYPE_MAX BGP_CONFED_AS_SET
218
219 static const struct tok bgp_as_path_segment_open_values[] = {
220 { BGP_AS_SEQUENCE, ""},
221 { BGP_AS_SET, "{ "},
222 { BGP_CONFED_AS_SEQUENCE, "( "},
223 { BGP_CONFED_AS_SET, "({ "},
224 { 0, NULL}
225 };
226
227 static const struct tok bgp_as_path_segment_close_values[] = {
228 { BGP_AS_SEQUENCE, ""},
229 { BGP_AS_SET, "}"},
230 { BGP_CONFED_AS_SEQUENCE, ")"},
231 { BGP_CONFED_AS_SET, "})"},
232 { 0, NULL}
233 };
234
235 #define BGP_OPT_AUTH 1
236 #define BGP_OPT_CAP 2
237
238 static const struct tok bgp_opt_values[] = {
239 { BGP_OPT_AUTH, "Authentication Information"},
240 { BGP_OPT_CAP, "Capabilities Advertisement"},
241 { 0, NULL}
242 };
243
244 #define BGP_CAPCODE_MP 1 /* RFC2858 */
245 #define BGP_CAPCODE_RR 2 /* RFC2918 */
246 #define BGP_CAPCODE_ORF 3 /* RFC5291 */
247 #define BGP_CAPCODE_MR 4 /* RFC3107 */
248 #define BGP_CAPCODE_EXT_NH 5 /* RFC5549 */
249 #define BGP_CAPCODE_EXT_MSG 6 /* RFC8654 */
250 #define BGP_CAPCODE_BGPSEC 7 /* RFC8205 */
251 #define BGP_CAPCODE_ML 8 /* RFC8277 */
252 #define BGP_CAPCODE_ROLE 9 /* RFC9234 */
253 #define BGP_CAPCODE_RESTART 64 /* RFC4724 */
254 #define BGP_CAPCODE_AS_NEW 65 /* RFC6793 */
255 #define BGP_CAPCODE_DYN_CAP 67 /* draft-ietf-idr-dynamic-cap */
256 #define BGP_CAPCODE_MULTISESS 68 /* draft-ietf-idr-bgp-multisession */
257 #define BGP_CAPCODE_ADD_PATH 69 /* RFC7911 */
258 #define BGP_CAPCODE_ENH_RR 70 /* RFC7313 */
259 #define BGP_CAPCODE_LLGR 71 /* draft-uttaro-idr-bgp-persistence-05 */
260 #define BGP_CAPCODE_RR_CISCO 128
261
262 static const struct tok bgp_capcode_values[] = {
263 { BGP_CAPCODE_MP, "Multiprotocol Extensions"},
264 { BGP_CAPCODE_RR, "Route Refresh"},
265 { BGP_CAPCODE_ORF, "Cooperative Route Filtering"},
266 { BGP_CAPCODE_MR, "Multiple Routes to a Destination"},
267 { BGP_CAPCODE_EXT_NH, "Extended Next Hop Encoding"},
268 { BGP_CAPCODE_EXT_MSG, "BGP Extended Message"},
269 { BGP_CAPCODE_BGPSEC, "BGPsec"},
270 { BGP_CAPCODE_ML, "Multiple Labels"},
271 { BGP_CAPCODE_RESTART, "Graceful Restart"},
272 { BGP_CAPCODE_AS_NEW, "32-Bit AS Number"},
273 { BGP_CAPCODE_DYN_CAP, "Dynamic Capability"},
274 { BGP_CAPCODE_MULTISESS, "Multisession BGP"},
275 { BGP_CAPCODE_ADD_PATH, "Multiple Paths"},
276 { BGP_CAPCODE_ENH_RR, "Enhanced Route Refresh"},
277 { BGP_CAPCODE_LLGR, "Long-lived Graceful Restart"},
278 { BGP_CAPCODE_RR_CISCO, "Route Refresh (Cisco)"},
279 { BGP_CAPCODE_ROLE, "Role Capability"},
280 { 0, NULL}
281 };
282
283 #define BGP_NOTIFY_MAJOR_MSG 1
284 #define BGP_NOTIFY_MAJOR_OPEN 2
285 #define BGP_NOTIFY_MAJOR_UPDATE 3
286 #define BGP_NOTIFY_MAJOR_HOLDTIME 4
287 #define BGP_NOTIFY_MAJOR_FSM 5
288 #define BGP_NOTIFY_MAJOR_CEASE 6
289 #define BGP_NOTIFY_MAJOR_ROUTEREFRESH 7
290 #define BGP_NOTIFY_MAJOR_SENDHOLDTIME 8
291
292 static const struct tok bgp_notify_major_values[] = {
293 { BGP_NOTIFY_MAJOR_MSG, "Message Header Error"},
294 { BGP_NOTIFY_MAJOR_OPEN, "OPEN Message Error"},
295 { BGP_NOTIFY_MAJOR_UPDATE, "UPDATE Message Error"},
296 { BGP_NOTIFY_MAJOR_HOLDTIME,"Hold Timer Expired"},
297 { BGP_NOTIFY_MAJOR_FSM, "Finite State Machine Error"},
298 { BGP_NOTIFY_MAJOR_CEASE, "Cease"},
299 { BGP_NOTIFY_MAJOR_ROUTEREFRESH, "ROUTE-REFRESH Message Error"},
300 { BGP_NOTIFY_MAJOR_SENDHOLDTIME, "Send Hold Timer Expired"},
301 { 0, NULL}
302 };
303
304 /* RFC 4486 */
305 #define BGP_NOTIFY_MINOR_CEASE_MAXPRFX 1
306 /* RFC 9003 */
307 #define BGP_NOTIFY_MINOR_CEASE_SHUT 2
308 #define BGP_NOTIFY_MINOR_CEASE_RESET 4
309 /* RFC 8538 */
310 #define BGP_NOTIFY_MINOR_CEASE_HARDRESET 9
311 static const struct tok bgp_notify_minor_cease_values[] = {
312 { BGP_NOTIFY_MINOR_CEASE_MAXPRFX, "Maximum Number of Prefixes Reached"},
313 { BGP_NOTIFY_MINOR_CEASE_SHUT, "Administrative Shutdown"},
314 { 3, "Peer Unconfigured"},
315 { BGP_NOTIFY_MINOR_CEASE_RESET, "Administrative Reset"},
316 { 5, "Connection Rejected"},
317 { 6, "Other Configuration Change"},
318 { 7, "Connection Collision Resolution"},
319 { 8, "Out of Resources"},
320 { BGP_NOTIFY_MINOR_CEASE_HARDRESET, "Hard Reset"},
321 { 10, "BFD Down"},
322 { 0, NULL}
323 };
324
325 static const struct tok bgp_notify_minor_msg_values[] = {
326 { 1, "Connection Not Synchronized"},
327 { 2, "Bad Message Length"},
328 { 3, "Bad Message Type"},
329 { 0, NULL}
330 };
331
332 static const struct tok bgp_notify_minor_open_values[] = {
333 { 1, "Unsupported Version Number"},
334 { 2, "Bad Peer AS"},
335 { 3, "Bad BGP Identifier"},
336 { 4, "Unsupported Optional Parameter"},
337 { 5, "Authentication Failure"},
338 { 6, "Unacceptable Hold Time"},
339 { 7, "Capability Message Error"},
340 { 0, NULL}
341 };
342
343 static const struct tok bgp_notify_minor_update_values[] = {
344 { 1, "Malformed Attribute List"},
345 { 2, "Unrecognized Well-known Attribute"},
346 { 3, "Missing Well-known Attribute"},
347 { 4, "Attribute Flags Error"},
348 { 5, "Attribute Length Error"},
349 { 6, "Invalid ORIGIN Attribute"},
350 { 7, "AS Routing Loop"},
351 { 8, "Invalid NEXT_HOP Attribute"},
352 { 9, "Optional Attribute Error"},
353 { 10, "Invalid Network Field"},
354 { 11, "Malformed AS_PATH"},
355 { 0, NULL}
356 };
357
358 static const struct tok bgp_notify_minor_fsm_values[] = {
359 { 0, "Unspecified Error"},
360 { 1, "In OpenSent State"},
361 { 2, "In OpenConfirm State"},
362 { 3, "In Established State"},
363 { 0, NULL }
364 };
365
366 static const struct tok bgp_notify_minor_routerefresh_values[] = {
367 { 1, "Invalid Message Length" },
368 { 0, NULL }
369 };
370
371 static const struct tok bgp_origin_values[] = {
372 { 0, "IGP"},
373 { 1, "EGP"},
374 { 2, "Incomplete"},
375 { 0, NULL}
376 };
377
378 #define BGP_PMSI_TUNNEL_RSVP_P2MP 1
379 #define BGP_PMSI_TUNNEL_LDP_P2MP 2
380 #define BGP_PMSI_TUNNEL_PIM_SSM 3
381 #define BGP_PMSI_TUNNEL_PIM_SM 4
382 #define BGP_PMSI_TUNNEL_PIM_BIDIR 5
383 #define BGP_PMSI_TUNNEL_INGRESS 6
384 #define BGP_PMSI_TUNNEL_LDP_MP2MP 7
385
386 static const struct tok bgp_pmsi_tunnel_values[] = {
387 { BGP_PMSI_TUNNEL_RSVP_P2MP, "RSVP-TE P2MP LSP"},
388 { BGP_PMSI_TUNNEL_LDP_P2MP, "LDP P2MP LSP"},
389 { BGP_PMSI_TUNNEL_PIM_SSM, "PIM-SSM Tree"},
390 { BGP_PMSI_TUNNEL_PIM_SM, "PIM-SM Tree"},
391 { BGP_PMSI_TUNNEL_PIM_BIDIR, "PIM-Bidir Tree"},
392 { BGP_PMSI_TUNNEL_INGRESS, "Ingress Replication"},
393 { BGP_PMSI_TUNNEL_LDP_MP2MP, "LDP MP2MP LSP"},
394 { 0, NULL}
395 };
396
397 static const struct tok bgp_pmsi_flag_values[] = {
398 { 0x01, "Leaf Information required"},
399 { 0, NULL}
400 };
401
402 #define BGP_AIGP_TLV 1
403
404 static const struct tok bgp_aigp_values[] = {
405 { BGP_AIGP_TLV, "AIGP"},
406 { 0, NULL}
407 };
408
409 #define BGP_ROLE_PROVIDER 0
410 #define BGP_ROLE_RS 1
411 #define BGP_ROLE_RS_CLIENT 2
412 #define BGP_ROLE_CUSTOMER 3
413 #define BGP_ROLE_PEER 4
414
415 static const struct tok bgp_role_values[] = {
416 { BGP_ROLE_PROVIDER, "Provider"},
417 { BGP_ROLE_RS, "RS"},
418 { BGP_ROLE_RS_CLIENT, "RS-Client"},
419 { BGP_ROLE_CUSTOMER, "Customer"},
420 { BGP_ROLE_PEER, "Peer"},
421 { 0, NULL}
422 };
423
424 /* Subsequent address family identifier, RFC2283 section 7 */
425 #define SAFNUM_RES 0
426 #define SAFNUM_UNICAST 1
427 #define SAFNUM_MULTICAST 2
428 #define SAFNUM_UNIMULTICAST 3 /* deprecated now */
429 /* labeled BGP RFC3107 */
430 #define SAFNUM_LABUNICAST 4
431 /* RFC6514 */
432 #define SAFNUM_MULTICAST_VPN 5
433 /* draft-nalawade-kapoor-tunnel-safi */
434 #define SAFNUM_TUNNEL 64
435 /* RFC4761 */
436 #define SAFNUM_VPLS 65
437 /* RFC6037 */
438 #define SAFNUM_MDT 66
439 /* RFC7432 */
440 #define SAFNUM_EVPN 70
441 /* RFC4364 */
442 #define SAFNUM_VPNUNICAST 128
443 /* RFC6513 */
444 #define SAFNUM_VPNMULTICAST 129
445 #define SAFNUM_VPNUNIMULTICAST 130 /* deprecated now */
446 /* RFC4684 */
447 #define SAFNUM_RT_ROUTING_INFO 132
448
449 #define BGP_VPN_RD_LEN 8
450
451 static const struct tok bgp_safi_values[] = {
452 { SAFNUM_RES, "Reserved"},
453 { SAFNUM_UNICAST, "Unicast"},
454 { SAFNUM_MULTICAST, "Multicast"},
455 { SAFNUM_UNIMULTICAST, "Unicast+Multicast"},
456 { SAFNUM_LABUNICAST, "labeled Unicast"},
457 { SAFNUM_TUNNEL, "Tunnel"},
458 { SAFNUM_VPLS, "VPLS"},
459 { SAFNUM_MDT, "MDT"},
460 { SAFNUM_EVPN, "EVPN"},
461 { SAFNUM_VPNUNICAST, "labeled VPN Unicast"},
462 { SAFNUM_VPNMULTICAST, "labeled VPN Multicast"},
463 { SAFNUM_VPNUNIMULTICAST, "labeled VPN Unicast+Multicast"},
464 { SAFNUM_RT_ROUTING_INFO, "Route Target Routing Information"},
465 { SAFNUM_MULTICAST_VPN, "Multicast VPN"},
466 { 0, NULL }
467 };
468
469 static const struct tok bgp_graceful_restart_comm_flag_values[] = {
470 { 0x8, "R" },
471 { 0x4, "N" },
472 { 0, NULL }
473 };
474
475 /* well-known community */
476 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
477 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
478 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
479
480 /* Extended community type - RFC 4360 */
481 #define BGP_EXT_COM_RT_0 0x0002 /* Route Target,Format AS(2bytes):AN(4bytes) */
482 #define BGP_EXT_COM_RT_1 0x0102 /* Route Target,Format IP address:AN(2bytes) */
483 #define BGP_EXT_COM_RT_2 0x0202 /* Route Target,Format AN(4bytes):local(2bytes) */
484 #define BGP_EXT_COM_RO_0 0x0003 /* Route Origin,Format AS(2bytes):AN(4bytes) */
485 #define BGP_EXT_COM_RO_1 0x0103 /* Route Origin,Format IP address:AN(2bytes) */
486 #define BGP_EXT_COM_RO_2 0x0203 /* Route Origin,Format AN(4bytes):local(2bytes) */
487 #define BGP_EXT_COM_LINKBAND 0x4004 /* Link Bandwidth,Format AS(2B):Bandwidth(4B) */
488 #define BGP_EXT_COM_OVS 0x4300 /* BGP Prefix Origin Validation State Extended Community */
489 /* rfc2547 bgp-mpls-vpns */
490 #define BGP_EXT_COM_VPN_ORIGIN 0x0005 /* OSPF Domain ID / VPN of Origin - draft-rosen-vpns-ospf-bgp-mpls */
491 #define BGP_EXT_COM_VPN_ORIGIN2 0x0105 /* duplicate - keep for backwards compatibility */
492 #define BGP_EXT_COM_VPN_ORIGIN3 0x0205 /* duplicate - keep for backwards compatibility */
493 #define BGP_EXT_COM_VPN_ORIGIN4 0x8005 /* duplicate - keep for backwards compatibility */
494
495 #define BGP_EXT_COM_OSPF_RTYPE 0x0306 /* OSPF Route Type,Format Area(4B):RouteType(1B):Options(1B) */
496 #define BGP_EXT_COM_OSPF_RTYPE2 0x8000 /* duplicate - keep for backwards compatibility */
497 #define BGP_EXT_COM_ENCAP 0x030c /* rfc5512 */
498
499 #define BGP_EXT_COM_OSPF_RID 0x0107 /* OSPF Router ID,Format RouterID(4B):Unused(2B) */
500 #define BGP_EXT_COM_OSPF_RID2 0x8001 /* duplicate - keep for backwards compatibility */
501
502 #define BGP_EXT_COM_L2INFO 0x800a /* draft-kompella-ppvpn-l2vpn */
503
504 #define BGP_EXT_COM_SOURCE_AS 0x0009 /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */
505 #define BGP_EXT_COM_VRF_RT_IMP 0x010b /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */
506 #define BGP_EXT_COM_L2VPN_RT_0 0x000a /* L2VPN Identifier,Format AS(2bytes):AN(4bytes) */
507 #define BGP_EXT_COM_L2VPN_RT_1 0xF10a /* L2VPN Identifier,Format IP address:AN(2bytes) */
508
509 /* https://www.cisco.com/en/US/tech/tk436/tk428/technologies_tech_note09186a00801eb09a.shtml */
510 #define BGP_EXT_COM_EIGRP_GEN 0x8800
511 #define BGP_EXT_COM_EIGRP_METRIC_AS_DELAY 0x8801
512 #define BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW 0x8802
513 #define BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU 0x8803
514 #define BGP_EXT_COM_EIGRP_EXT_REMAS_REMID 0x8804
515 #define BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC 0x8805
516
517 /* Optional Parameters */
518 #define BGP_OPEN_NON_EXT_OPT_TYPE_EXTENDED_LENGTH 255 /* Non-Ext OP Type */
519
520 static const struct tok bgp_extd_comm_flag_values[] = {
521 { 0x8000, "vendor-specific"},
522 { 0x4000, "non-transitive"},
523 { 0, NULL},
524 };
525
526 /* rfc8097 */
527 static const struct tok bgp_prefix_origin_validation_state[] = {
528 { 0, "valid" },
529 { 1, "not found" },
530 { 2, "invalid" },
531 { 0, NULL },
532 };
533
534 static const struct tok bgp_extd_comm_subtype_values[] = {
535 { BGP_EXT_COM_RT_0, "target"},
536 { BGP_EXT_COM_RT_1, "target"},
537 { BGP_EXT_COM_RT_2, "target"},
538 { BGP_EXT_COM_RO_0, "origin"},
539 { BGP_EXT_COM_RO_1, "origin"},
540 { BGP_EXT_COM_RO_2, "origin"},
541 { BGP_EXT_COM_LINKBAND, "link-BW"},
542 { BGP_EXT_COM_OVS, "origin-validation-state"},
543 { BGP_EXT_COM_VPN_ORIGIN, "ospf-domain"},
544 { BGP_EXT_COM_VPN_ORIGIN2, "ospf-domain"},
545 { BGP_EXT_COM_VPN_ORIGIN3, "ospf-domain"},
546 { BGP_EXT_COM_VPN_ORIGIN4, "ospf-domain"},
547 { BGP_EXT_COM_OSPF_RTYPE, "ospf-route-type"},
548 { BGP_EXT_COM_OSPF_RTYPE2, "ospf-route-type"},
549 { BGP_EXT_COM_ENCAP, "encapsulation"},
550 { BGP_EXT_COM_OSPF_RID, "ospf-router-id"},
551 { BGP_EXT_COM_OSPF_RID2, "ospf-router-id"},
552 { BGP_EXT_COM_L2INFO, "layer2-info"},
553 { BGP_EXT_COM_EIGRP_GEN, "eigrp-general-route (flag, tag)" },
554 { BGP_EXT_COM_EIGRP_METRIC_AS_DELAY, "eigrp-route-metric (AS, delay)" },
555 { BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW, "eigrp-route-metric (reliability, nexthop, bandwidth)" },
556 { BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU, "eigrp-route-metric (load, MTU)" },
557 { BGP_EXT_COM_EIGRP_EXT_REMAS_REMID, "eigrp-external-route (remote-AS, remote-ID)" },
558 { BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC, "eigrp-external-route (remote-proto, remote-metric)" },
559 { BGP_EXT_COM_SOURCE_AS, "source-AS" },
560 { BGP_EXT_COM_VRF_RT_IMP, "vrf-route-import"},
561 { BGP_EXT_COM_L2VPN_RT_0, "l2vpn-id"},
562 { BGP_EXT_COM_L2VPN_RT_1, "l2vpn-id"},
563 { 0, NULL},
564 };
565
566 /* RFC RFC5512 BGP Tunnel Encapsulation Attribute Tunnel Types */
567 #define BGP_ENCAP_TUNNEL_L2TPV3_IP 1
568 #define BGP_ENCAP_TUNNEL_GRE 2
569 #define BGP_ENCAP_TUNNEL_TRANSMIT 3
570 #define BGP_ENCAP_TUNNEL_IPSEC 4
571 #define BGP_ENCAP_TUNNEL_IP_IPSEC 5
572 #define BGP_ENCAP_TUNNEL_MPLS_IP 6
573 #define BGP_ENCAP_TUNNEL_IP_IP 7
574 #define BGP_ENCAP_TUNNEL_VXLAN 8
575 #define BGP_ENCAP_TUNNEL_NVGRE 9
576 #define BGP_ENCAP_TUNNEL_MPLS 10
577 #define BGP_ENCAP_TUNNEL_MPLS_GRE 11
578 #define BGP_ENCAP_TUNNEL_VXLAN_GPE 12
579 #define BGP_ENCAP_TUNNEL_MPLS_UDP 13
580 #define BGP_ENCAP_TUNNEL_IPV6 14
581 #define BGP_ENCAP_TUNNEL_SR_TE 15
582 #define BGP_ENCAP_TUNNEL_BARE 16
583 #define BGP_ENCAP_TUNNEL_SR 17
584
585 static const struct tok bgp_extd_comm_encap_tunnel_values[] = {
586 { BGP_ENCAP_TUNNEL_L2TPV3_IP, "L2TPv3 over IP"},
587 { BGP_ENCAP_TUNNEL_GRE, "GRE"},
588 { BGP_ENCAP_TUNNEL_TRANSMIT, "Transmit Tunnel"},
589 { BGP_ENCAP_TUNNEL_IPSEC, "IPsec"},
590 { BGP_ENCAP_TUNNEL_IP_IPSEC, "IP in IP with IPsec"},
591 { BGP_ENCAP_TUNNEL_MPLS_IP, "MPLS in IP with IPsec"},
592 { BGP_ENCAP_TUNNEL_IP_IP, "IP in IP"},
593 { BGP_ENCAP_TUNNEL_VXLAN, "VXLAN"},
594 { BGP_ENCAP_TUNNEL_NVGRE, "NVGRE"},
595 { BGP_ENCAP_TUNNEL_MPLS, "MPLS"},
596 { BGP_ENCAP_TUNNEL_MPLS_GRE, "MPLS in GRE"},
597 { BGP_ENCAP_TUNNEL_VXLAN_GPE, "VXLAN GPE"},
598 { BGP_ENCAP_TUNNEL_MPLS_UDP, "MPLS in UDP"},
599 { BGP_ENCAP_TUNNEL_IPV6, "IPv6"},
600 { BGP_ENCAP_TUNNEL_SR_TE, "SR TE"},
601 { BGP_ENCAP_TUNNEL_BARE, "Bare"},
602 { BGP_ENCAP_TUNNEL_SR, "SR"},
603 { 0, NULL},
604 };
605
606 /* OSPF codes for BGP_EXT_COM_OSPF_RTYPE draft-rosen-vpns-ospf-bgp-mpls */
607 #define BGP_OSPF_RTYPE_RTR 1 /* OSPF Router LSA */
608 #define BGP_OSPF_RTYPE_NET 2 /* OSPF Network LSA */
609 #define BGP_OSPF_RTYPE_SUM 3 /* OSPF Summary LSA */
610 #define BGP_OSPF_RTYPE_EXT 5 /* OSPF External LSA, note that ASBR doesn't apply to MPLS-VPN */
611 #define BGP_OSPF_RTYPE_NSSA 7 /* OSPF NSSA External*/
612 #define BGP_OSPF_RTYPE_SHAM 129 /* OSPF-MPLS-VPN Sham link */
613 #define BGP_OSPF_RTYPE_METRIC_TYPE 0x1 /* LSB of RTYPE Options Field */
614
615 static const struct tok bgp_extd_comm_ospf_rtype_values[] = {
616 { BGP_OSPF_RTYPE_RTR, "Router" },
617 { BGP_OSPF_RTYPE_NET, "Network" },
618 { BGP_OSPF_RTYPE_SUM, "Summary" },
619 { BGP_OSPF_RTYPE_EXT, "External" },
620 { BGP_OSPF_RTYPE_NSSA,"NSSA External" },
621 { BGP_OSPF_RTYPE_SHAM,"MPLS-VPN Sham" },
622 { 0, NULL },
623 };
624
625 /* ADD-PATH Send/Receive field values */
626 static const struct tok bgp_add_path_recvsend[] = {
627 { 1, "Receive" },
628 { 2, "Send" },
629 { 3, "Both" },
630 { 0, NULL },
631 };
632
633 static const struct tok bgp_bgpsec_bitmap_str[] = {
634 { 1U << 0, "MBZ-0" },
635 { 1U << 1, "MBZ-1" },
636 { 1U << 2, "MBZ-2" },
637 { 1U << 3, "MBZ-3" },
638 { 1U << 4, "MBZ-4" },
639 { 1U << 5, "MBZ-5" },
640 { 1U << 6, "MBZ-6" },
641 { 1U << 7, "C" },
642 { 0, NULL}
643 };
644
645 #define AS_STR_SIZE sizeof("xxxxx.xxxxx")
646
647 /*
648 * as_printf
649 *
650 * Convert an AS number into a string and return string pointer.
651 *
652 * Depending on bflag is set or not, AS number is converted into ASDOT notation
653 * or plain number notation.
654 *
655 */
656 static char *
657 as_printf(netdissect_options *ndo,
658 char *str, size_t size, u_int asnum)
659 {
660 if (!ndo->ndo_bflag || asnum <= 0xFFFF) {
661 snprintf(str, size, "%u", asnum);
662 } else {
663 snprintf(str, size, "%u.%u", asnum >> 16, asnum & 0xFFFF);
664 }
665 return str;
666 }
667
668 #define ITEMCHECK(minlen) if (itemlen < minlen) goto badtlv;
669
670 int
671 decode_prefix4(netdissect_options *ndo,
672 const u_char *pptr, u_int itemlen, char *buf, size_t buflen)
673 {
674 nd_ipv4 addr;
675 u_int plen, plenbytes;
676
677 ITEMCHECK(1);
678 plen = GET_U_1(pptr);
679 if (32 < plen)
680 return -1;
681 itemlen -= 1;
682
683 memset(&addr, 0, sizeof(addr));
684 plenbytes = (plen + 7) / 8;
685 ITEMCHECK(plenbytes);
686 GET_CPY_BYTES(&addr, pptr + 1, plenbytes);
687 if (plen % 8) {
688 ((u_char *)&addr)[plenbytes - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
689 }
690 snprintf(buf, buflen, "%s/%u", ipaddr_string(ndo, (const u_char *)&addr), plen);
691 return 1 + plenbytes;
692
693 badtlv:
694 return -2;
695 }
696
697 static int
698 print_labeled_prefix4(netdissect_options *ndo,
699 const u_char *pptr, u_int itemlen)
700 {
701 nd_ipv4 addr;
702 u_int plen, plenbytes;
703
704 /* prefix length and label = 4 bytes */
705 ND_TCHECK_4(pptr);
706 ITEMCHECK(4);
707 plen = GET_U_1(pptr); /* get prefix length */
708
709 /* this is one of the weirdnesses of rfc3107
710 the label length (actually the label + COS bits)
711 is added to the prefix length;
712 we also do only read out just one label -
713 there is no real application for advertisement of
714 stacked labels in a single BGP message
715 */
716
717 if (24 > plen)
718 goto badplen;
719
720 plen-=24; /* adjust prefixlen - labellength */
721
722 if (32 < plen)
723 goto badplen;
724 itemlen -= 4;
725
726 memset(&addr, 0, sizeof(addr));
727 plenbytes = (plen + 7) / 8;
728 ITEMCHECK(plenbytes);
729 GET_CPY_BYTES(&addr, pptr + 4, plenbytes);
730 if (plen % 8) {
731 ((u_char *)&addr)[plenbytes - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
732 }
733 /* the label may get offsetted by 4 bits so lets shift it right */
734 ND_PRINT("\n\t %s/%u, label:%u %s",
735 ipaddr_string(ndo, (const u_char *)&addr),
736 plen,
737 GET_BE_U_3(pptr + 1)>>4,
738 ((GET_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
739
740 return 4 + plenbytes;
741
742 badplen:
743 ND_PRINT("\n\t (illegal prefix length)");
744 return -1;
745
746 trunc:
747 return -2;
748
749 badtlv:
750 return -3;
751 }
752
753 /*
754 * bgp_vpn_ip_print
755 *
756 * print an ipv4 or ipv6 address into a buffer dependent on address length.
757 */
758 static char *
759 bgp_vpn_ip_print(netdissect_options *ndo,
760 const u_char *pptr, u_int addr_length)
761 {
762
763 /* worst case string is s fully formatted v6 address */
764 static char addr[sizeof("1234:5678:89ab:cdef:1234:5678:89ab:cdef")];
765 char *pos = addr;
766
767 switch(addr_length) {
768 case (sizeof(nd_ipv4) << 3): /* 32 */
769 snprintf(pos, sizeof(addr), "%s", GET_IPADDR_STRING(pptr));
770 break;
771 case (sizeof(nd_ipv6) << 3): /* 128 */
772 snprintf(pos, sizeof(addr), "%s", GET_IP6ADDR_STRING(pptr));
773 break;
774 default:
775 snprintf(pos, sizeof(addr), "bogus address length %u", addr_length);
776 break;
777 }
778 pos += strlen(pos);
779
780 *(pos) = '\0';
781 return (addr);
782 }
783
784 /*
785 * bgp_vpn_sg_print
786 *
787 * print an multicast s,g entry into a buffer.
788 * the s,g entry is encoded like this.
789 *
790 * +-----------------------------------+
791 * | Multicast Source Length (1 octet) |
792 * +-----------------------------------+
793 * | Multicast Source (Variable) |
794 * +-----------------------------------+
795 * | Multicast Group Length (1 octet) |
796 * +-----------------------------------+
797 * | Multicast Group (Variable) |
798 * +-----------------------------------+
799 *
800 * return the number of bytes read from the wire.
801 */
802 static u_int
803 bgp_vpn_sg_print(netdissect_options *ndo, const u_char *pptr)
804 {
805 uint8_t addr_length;
806 u_int total_length;
807
808 total_length = 0;
809
810 /* Source address length, encoded in bits */
811 addr_length = GET_U_1(pptr);
812 pptr++;
813
814 /* Source address */
815 ND_TCHECK_LEN(pptr, (addr_length >> 3));
816 total_length += (addr_length >> 3) + 1;
817 if (addr_length) {
818 ND_PRINT(", Source %s",
819 bgp_vpn_ip_print(ndo, pptr, addr_length));
820 pptr += (addr_length >> 3);
821 }
822
823 /* Group address length, encoded in bits */
824 addr_length = GET_U_1(pptr);
825 pptr++;
826
827 /* Group address */
828 ND_TCHECK_LEN(pptr, (addr_length >> 3));
829 total_length += (addr_length >> 3) + 1;
830 if (addr_length) {
831 ND_PRINT(", Group %s",
832 bgp_vpn_ip_print(ndo, pptr, addr_length));
833 pptr += (addr_length >> 3);
834 }
835
836 trunc:
837 return (total_length);
838 }
839
840 /* Print an RFC 4364 Route Distinguisher */
841 const char *
842 bgp_vpn_rd_print(netdissect_options *ndo, const u_char *pptr)
843 {
844 /* allocate space for the largest possible string */
845 static char rd[sizeof("xxxxx.xxxxx:xxxxx (xxx.xxx.xxx.xxx:xxxxx)")];
846 char *pos = rd;
847 /* allocate space for the largest possible string */
848 char astostr[AS_STR_SIZE];
849
850 /* ok lets load the RD format */
851 switch (GET_BE_U_2(pptr)) {
852
853 case 0:
854 /* 2-byte-AS:number fmt */
855 snprintf(pos, sizeof(rd) - (pos - rd), "%u:%u (= %u.%u.%u.%u)",
856 GET_BE_U_2(pptr + 2),
857 GET_BE_U_4(pptr + 4),
858 GET_U_1(pptr + 4), GET_U_1(pptr + 5),
859 GET_U_1(pptr + 6), GET_U_1(pptr + 7));
860 break;
861
862 case 1:
863 /* IP-address:AS fmt */
864 snprintf(pos, sizeof(rd) - (pos - rd), "%u.%u.%u.%u:%u",
865 GET_U_1(pptr + 2), GET_U_1(pptr + 3),
866 GET_U_1(pptr + 4), GET_U_1(pptr + 5),
867 GET_BE_U_2(pptr + 6));
868 break;
869
870 case 2:
871 /* 4-byte-AS:number fmt */
872 snprintf(pos, sizeof(rd) - (pos - rd), "%s:%u (%u.%u.%u.%u:%u)",
873 as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_4(pptr + 2)),
874 GET_BE_U_2(pptr + 6), GET_U_1(pptr + 2),
875 GET_U_1(pptr + 3), GET_U_1(pptr + 4),
876 GET_U_1(pptr + 5), GET_BE_U_2(pptr + 6));
877 break;
878 default:
879 snprintf(pos, sizeof(rd) - (pos - rd), "unknown RD format");
880 break;
881 }
882 pos += strlen(pos);
883 *(pos) = '\0';
884 return (rd);
885 }
886
887 /*
888 * Print an RFC 4360 Extended Community.
889 */
890 static void
891 bgp_extended_community_print(netdissect_options *ndo,
892 const u_char *pptr)
893 {
894 /* allocate space for the largest possible string */
895 char astostr[AS_STR_SIZE];
896
897 switch (GET_BE_U_2(pptr)) {
898
899 case BGP_EXT_COM_RT_0:
900 case BGP_EXT_COM_RO_0:
901 case BGP_EXT_COM_L2VPN_RT_0:
902 ND_PRINT("%u:%u (= %s)",
903 GET_BE_U_2(pptr + 2),
904 GET_BE_U_4(pptr + 4),
905 GET_IPADDR_STRING(pptr+4));
906 break;
907
908 case BGP_EXT_COM_RT_1:
909 case BGP_EXT_COM_RO_1:
910 case BGP_EXT_COM_L2VPN_RT_1:
911 case BGP_EXT_COM_VRF_RT_IMP:
912 ND_PRINT("%s:%u",
913 GET_IPADDR_STRING(pptr+2),
914 GET_BE_U_2(pptr + 6));
915 break;
916
917 case BGP_EXT_COM_RT_2:
918 case BGP_EXT_COM_RO_2:
919 ND_PRINT("%s:%u",
920 as_printf(ndo, astostr, sizeof(astostr),
921 GET_BE_U_4(pptr + 2)), GET_BE_U_2(pptr + 6));
922 break;
923
924 case BGP_EXT_COM_LINKBAND:
925 ND_PRINT("bandwidth: %.3f Mbps",
926 GET_BE_F_4(pptr + 4)*8/1000000);
927 break;
928
929 case BGP_EXT_COM_OVS:
930 /* The Reserved field MUST be set to 0 and ignored upon the
931 * receipt of this community.
932 */
933 {
934 uint64_t reserved = GET_BE_U_5(pptr + 2);
935
936 if (reserved)
937 ND_PRINT("[the reserved field 0x%" PRIx64 " MUST be 0] ",
938 reserved);
939 ND_PRINT("ovs: %s",
940 tok2str(bgp_prefix_origin_validation_state,
941 "unknown origin validation state",
942 GET_U_1(pptr + 7)));
943 }
944 break;
945
946 case BGP_EXT_COM_VPN_ORIGIN:
947 case BGP_EXT_COM_VPN_ORIGIN2:
948 case BGP_EXT_COM_VPN_ORIGIN3:
949 case BGP_EXT_COM_VPN_ORIGIN4:
950 case BGP_EXT_COM_OSPF_RID:
951 case BGP_EXT_COM_OSPF_RID2:
952 ND_PRINT("%s", GET_IPADDR_STRING(pptr+2));
953 break;
954
955 case BGP_EXT_COM_OSPF_RTYPE:
956 case BGP_EXT_COM_OSPF_RTYPE2:
957 ND_PRINT("area:%s, router-type:%s, metric-type:%s%s",
958 GET_IPADDR_STRING(pptr+2),
959 tok2str(bgp_extd_comm_ospf_rtype_values,
960 "unknown (0x%02x)",
961 GET_U_1((pptr + 6))),
962 (GET_U_1(pptr + 7) & BGP_OSPF_RTYPE_METRIC_TYPE) ? "E2" : "",
963 ((GET_U_1(pptr + 6) == BGP_OSPF_RTYPE_EXT) || (GET_U_1(pptr + 6) == BGP_OSPF_RTYPE_NSSA)) ? "E1" : "");
964 break;
965
966 case BGP_EXT_COM_L2INFO:
967 ND_PRINT("%s Control Flags [0x%02x]:MTU %u",
968 tok2str(l2vpn_encaps_values,
969 "unknown encaps",
970 GET_U_1((pptr + 2))),
971 GET_U_1((pptr + 3)),
972 GET_BE_U_2(pptr + 4));
973 break;
974
975 case BGP_EXT_COM_SOURCE_AS:
976 ND_PRINT("AS %u", GET_BE_U_2(pptr + 2));
977 break;
978
979 case BGP_EXT_COM_ENCAP:
980 ND_PRINT("Tunnel type: %s", tok2str(bgp_extd_comm_encap_tunnel_values,
981 "unknown encaps",
982 GET_BE_U_2(pptr + 6)));
983 break;
984
985 default:
986 ND_PRINT("%02x%02x%02x%02x%02x%02x",
987 GET_U_1(pptr + 2),
988 GET_U_1(pptr + 3),
989 GET_U_1(pptr + 4),
990 GET_U_1(pptr + 5),
991 GET_U_1(pptr + 6),
992 GET_U_1(pptr + 7));
993 break;
994 }
995 }
996
997 /*
998 * RFC4684 (Section 4)/RFC2858 (Section 4).
999 * RTC membership prefix is structured as follows
1000 * [prefix-len] [origin-as] [route-target]
1001 * The route-target is encoded as RT ext-comms.
1002 * Prefix-len may be 0, 32..96
1003 *
1004 * Note that pptr is not packet data - it is
1005 * a buffer owned by our caller - therefore GET_*
1006 * macros can not be used.
1007 */
1008 static char *
1009 bgp_rt_prefix_print(netdissect_options *ndo,
1010 const u_char *pptr,
1011 u_int plen)
1012 {
1013 /* allocate space for the largest possible string */
1014 char rtc_prefix_in_hex[sizeof("0000 0000 0000 0000")] = "";
1015 u_int rtc_prefix_in_hex_len = 0;
1016 static char output[61]; /* max response string */
1017 /* allocate space for the largest possible string */
1018 char astostr[AS_STR_SIZE];
1019 uint16_t ec_type = 0;
1020 u_int octet_count;
1021 u_int i;
1022
1023 if (plen == 0) {
1024 snprintf(output, sizeof(output), "route-target: 0:0/0");
1025 return (output);
1026 }
1027
1028 /* hex representation of the prefix */
1029 octet_count = (plen+7)/8;
1030 for (i=0; i<octet_count; i++) {
1031 rtc_prefix_in_hex_len += snprintf(rtc_prefix_in_hex+rtc_prefix_in_hex_len,
1032 sizeof(rtc_prefix_in_hex)-rtc_prefix_in_hex_len,
1033 "%02x%s", *(pptr+i),
1034 ((i%2 == 1) && (i<octet_count-1)) ? " " : "");
1035 }
1036
1037 if (plen < 16) {
1038 /*
1039 * The prefix is too short to include the full ext-comm type,
1040 * so we have no way to parse it further.
1041 */
1042 snprintf(output, sizeof(output), "route-target: partial-type: (%s/%d)",
1043 rtc_prefix_in_hex, plen);
1044 return (output);
1045 }
1046
1047 /*
1048 * get the ext-comm type
1049 * Note: pptr references a static 8 octet buffer with unused bits set to 0,
1050 * hence EXTRACT_*() macros are safe.
1051 */
1052 ec_type = EXTRACT_BE_U_2(pptr);
1053 switch (ec_type) {
1054 case BGP_EXT_COM_RT_0:
1055 /* 2-byte-AS:number fmt */
1056 snprintf(output, sizeof(output), "route-target: %u:%u/%d (%s)",
1057 EXTRACT_BE_U_2(pptr+2),
1058 EXTRACT_BE_U_4(pptr+4),
1059 plen, rtc_prefix_in_hex);
1060 break;
1061
1062 case BGP_EXT_COM_RT_1:
1063 /* IP-address:AS fmt */
1064 snprintf(output, sizeof(output), "route-target: %u.%u.%u.%u:%u/%d (%s)",
1065 *(pptr+2), *(pptr+3), *(pptr+4), *(pptr+5),
1066 EXTRACT_BE_U_2(pptr+6), plen, rtc_prefix_in_hex);
1067 break;
1068
1069 case BGP_EXT_COM_RT_2:
1070 /* 4-byte-AS:number fmt */
1071 snprintf(output, sizeof(output), "route-target: %s:%u/%d (%s)",
1072 as_printf(ndo, astostr, sizeof(astostr), EXTRACT_BE_U_4(pptr+2)),
1073 EXTRACT_BE_U_2(pptr+6), plen, rtc_prefix_in_hex);
1074 break;
1075
1076 default:
1077 snprintf(output, sizeof(output), "route target: unknown-type(%04x) (%s/%d)",
1078 ec_type,
1079 rtc_prefix_in_hex, plen);
1080 break;
1081 }
1082 return (output);
1083 }
1084
1085 /* RFC 4684 */
1086 static int
1087 print_rt_routing_info(netdissect_options *ndo, const u_char *pptr)
1088 {
1089 uint8_t route_target[8];
1090 u_int plen;
1091 /* allocate space for the largest possible string */
1092 char astostr[AS_STR_SIZE];
1093 u_int num_octets;
1094
1095 /* NLRI "prefix length" from RFC 2858 Section 4. */
1096 plen = GET_U_1(pptr); /* get prefix length */
1097
1098 /* NLRI "prefix" (ibid), valid lengths are { 0, 32, 33, ..., 96 } bits.
1099 * RFC 4684 Section 4 defines the layout of "origin AS" and "route
1100 * target" fields inside the "prefix" depending on its length.
1101 */
1102 if (0 == plen) {
1103 /* Without "origin AS", without "route target". */
1104 ND_PRINT("\n\t default route target");
1105 return 1;
1106 }
1107
1108 if (32 > plen) {
1109 ND_PRINT("\n\t (illegal prefix length)");
1110 return -1;
1111 }
1112
1113 /* With at least "origin AS", possibly with "route target". */
1114 as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_4(pptr + 1));
1115
1116 plen -= 32; /* adjust prefix length */
1117
1118 if (64 < plen) {
1119 ND_PRINT("\n\t (illegal prefix length)");
1120 return -1;
1121 }
1122
1123 /* From now on (plen + 7) / 8 evaluates to { 0, 1, 2, ..., 8 }
1124 * and gives the number of octets in the variable-length "route
1125 * target" field inside this NLRI "prefix". Look for it.
1126 */
1127 memset(&route_target, 0, sizeof(route_target));
1128 num_octets = (plen + 7) / 8;
1129 GET_CPY_BYTES(&route_target, pptr + 5, num_octets);
1130 /* If mask-len is not on octet boundary, ensure all extra bits are 0 */
1131 if (plen % 8) {
1132 ((u_char *)&route_target)[num_octets - 1] &=
1133 ((0xff00 >> (plen % 8)) & 0xff);
1134 }
1135 ND_PRINT("\n\t origin AS: %s, %s",
1136 astostr,
1137 bgp_rt_prefix_print(ndo, (u_char *)&route_target, plen));
1138
1139 return 5 + num_octets;
1140 }
1141
1142 static int
1143 print_labeled_vpn_prefix4(netdissect_options *ndo, const u_char *pptr)
1144 {
1145 nd_ipv4 addr;
1146 u_int plen;
1147
1148 plen = GET_U_1(pptr); /* get prefix length */
1149
1150 if ((24+64) > plen)
1151 goto badplen;
1152
1153 plen -= (24+64); /* adjust prefixlen - labellength - RD len*/
1154
1155 if (32 < plen)
1156 goto badplen;
1157
1158 memset(&addr, 0, sizeof(addr));
1159 GET_CPY_BYTES(&addr, pptr + 12, (plen + 7) / 8);
1160 if (plen % 8) {
1161 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
1162 ((0xff00 >> (plen % 8)) & 0xff);
1163 }
1164 /* the label may get offsetted by 4 bits so lets shift it right */
1165 ND_PRINT("\n\t RD: %s, %s/%u, label:%u %s",
1166 bgp_vpn_rd_print(ndo, pptr+4),
1167 ipaddr_string(ndo, (const u_char *)&addr),
1168 plen,
1169 GET_BE_U_3(pptr + 1)>>4,
1170 ((GET_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1171
1172 return 12 + (plen + 7) / 8;
1173
1174 badplen:
1175 ND_PRINT("\n\t (illegal prefix length)");
1176 return -1;
1177 }
1178
1179 /*
1180 * +-------------------------------+
1181 * | |
1182 * | RD:IPv4-address (12 octets) |
1183 * | |
1184 * +-------------------------------+
1185 * | MDT Group-address (4 octets) |
1186 * +-------------------------------+
1187 */
1188
1189 #define MDT_VPN_NLRI_LEN 16
1190
1191 static int
1192 print_mdt_vpn_nlri(netdissect_options *ndo, const u_char *pptr)
1193 {
1194 const u_char *rd;
1195 const u_char *vpn_ip;
1196
1197 /* if the NLRI is not predefined length, quit.*/
1198 if (GET_U_1(pptr) != MDT_VPN_NLRI_LEN * 8) {
1199 ND_PRINT("\n\t (illegal prefix length)");
1200 return -1;
1201 }
1202 pptr++;
1203
1204 /* RD */
1205 ND_TCHECK_8(pptr);
1206 rd = pptr;
1207 pptr += 8;
1208
1209 /* IPv4 address */
1210 vpn_ip = pptr;
1211 pptr += sizeof(nd_ipv4);
1212
1213 /* MDT Group Address */
1214 ND_PRINT("\n\t RD: %s, VPN IP Address: %s, MC Group Address: %s",
1215 bgp_vpn_rd_print(ndo, rd),
1216 GET_IPADDR_STRING(vpn_ip),
1217 GET_IPADDR_STRING(pptr));
1218
1219 return MDT_VPN_NLRI_LEN + 1;
1220
1221 trunc:
1222 return -2;
1223 }
1224
1225 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI 1
1226 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI 2
1227 #define BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI 3
1228 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF 4
1229 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE 5
1230 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN 6
1231 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN 7
1232
1233 static const struct tok bgp_multicast_vpn_route_type_values[] = {
1234 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI, "Intra-AS I-PMSI"},
1235 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI, "Inter-AS I-PMSI"},
1236 { BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI, "S-PMSI"},
1237 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF, "Intra-AS Segment-Leaf"},
1238 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE, "Source-Active"},
1239 { BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN, "Shared Tree Join"},
1240 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN, "Source Tree Join"},
1241 { 0, NULL}
1242 };
1243
1244 static int
1245 print_multicast_vpn(netdissect_options *ndo, const u_char *pptr)
1246 {
1247 /* allocate space for the largest possible string */
1248 char astostr[AS_STR_SIZE];
1249 uint8_t route_type, route_length;
1250 u_int addr_length, sg_length;
1251
1252 route_type = GET_U_1(pptr);
1253 pptr++;
1254 route_length = GET_U_1(pptr);
1255 pptr++;
1256
1257 ND_PRINT("\n\t Route-Type: %s (%u), length: %u",
1258 tok2str(bgp_multicast_vpn_route_type_values,
1259 "Unknown", route_type),
1260 route_type, route_length);
1261
1262 switch(route_type) {
1263 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI:
1264 ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN);
1265 if (route_length < BGP_VPN_RD_LEN)
1266 goto trunc;
1267 ND_PRINT(", RD: %s, Originator %s",
1268 bgp_vpn_rd_print(ndo, pptr),
1269 bgp_vpn_ip_print(ndo, pptr + BGP_VPN_RD_LEN,
1270 (route_length - BGP_VPN_RD_LEN) << 3));
1271 break;
1272 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI:
1273 ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN + 4);
1274 ND_PRINT(", RD: %s, Source-AS %s",
1275 bgp_vpn_rd_print(ndo, pptr),
1276 as_printf(ndo, astostr, sizeof(astostr),
1277 GET_BE_U_4(pptr + BGP_VPN_RD_LEN)));
1278 break;
1279
1280 case BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI:
1281 ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN);
1282 ND_PRINT(", RD: %s", bgp_vpn_rd_print(ndo, pptr));
1283 pptr += BGP_VPN_RD_LEN;
1284
1285 sg_length = bgp_vpn_sg_print(ndo, pptr);
1286 addr_length = route_length - sg_length;
1287
1288 ND_TCHECK_LEN(pptr, addr_length);
1289 ND_PRINT(", Originator %s",
1290 bgp_vpn_ip_print(ndo, pptr, addr_length << 3));
1291 break;
1292
1293 case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE:
1294 ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN);
1295 ND_PRINT(", RD: %s", bgp_vpn_rd_print(ndo, pptr));
1296 pptr += BGP_VPN_RD_LEN;
1297
1298 bgp_vpn_sg_print(ndo, pptr);
1299 break;
1300
1301 case BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN: /* fall through */
1302 case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN:
1303 ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN + 4);
1304 ND_PRINT(", RD: %s, Source-AS %s",
1305 bgp_vpn_rd_print(ndo, pptr),
1306 as_printf(ndo, astostr, sizeof(astostr),
1307 GET_BE_U_4(pptr + BGP_VPN_RD_LEN)));
1308 pptr += BGP_VPN_RD_LEN + 4;
1309
1310 bgp_vpn_sg_print(ndo, pptr);
1311 break;
1312
1313 /*
1314 * no per route-type printing yet.
1315 */
1316 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF:
1317 default:
1318 break;
1319 }
1320
1321 return route_length + 2;
1322
1323 trunc:
1324 return -2;
1325 }
1326
1327 /*
1328 * As I remember, some versions of systems have an snprintf() that
1329 * returns -1 if the buffer would have overflowed. If the return
1330 * value is negative, set buflen to 0, to indicate that we've filled
1331 * the buffer up.
1332 *
1333 * If the return value is greater than buflen, that means that
1334 * the buffer would have overflowed; again, set buflen to 0 in
1335 * that case.
1336 */
1337 #define UPDATE_BUF_BUFLEN(buf, buflen, stringlen) \
1338 if (stringlen<0) \
1339 buflen=0; \
1340 else if ((u_int)stringlen>buflen) \
1341 buflen=0; \
1342 else { \
1343 buflen-=stringlen; \
1344 buf+=stringlen; \
1345 }
1346
1347 static int
1348 print_labeled_vpn_l2(netdissect_options *ndo, const u_char *pptr)
1349 {
1350 u_int plen, tlen, tlv_type, tlv_len, ttlv_len;
1351
1352 plen = GET_BE_U_2(pptr);
1353 tlen = plen;
1354 pptr += 2;
1355 /* Old and new L2VPN NLRI share AFI/SAFI
1356 * -> Assume a 12 Byte-length NLRI is auto-discovery-only
1357 * and > 17 as old format. Complain for the middle case
1358 */
1359 if (plen == 12) {
1360 /* assume AD-only with RD, BGPNH */
1361 ND_TCHECK_LEN(pptr, 12);
1362 ND_PRINT("\n\t RD: %s, BGPNH: %s",
1363 bgp_vpn_rd_print(ndo, pptr),
1364 GET_IPADDR_STRING(pptr+8));
1365 return plen + 2;
1366 } else if (plen > 17) {
1367 /* assume old format */
1368 /* RD, ID, LBLKOFF, LBLBASE */
1369
1370 ND_TCHECK_LEN(pptr, 15);
1371 ND_PRINT("\n\t RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
1372 bgp_vpn_rd_print(ndo, pptr),
1373 GET_BE_U_2(pptr + 8),
1374 GET_BE_U_2(pptr + 10),
1375 GET_BE_U_3(pptr + 12)>>4); /* the label is offsetted by 4 bits so lets shift it right */
1376 pptr += 15;
1377 tlen -= 15;
1378
1379 /* ok now the variable part - lets read out TLVs*/
1380 while (tlen != 0) {
1381 if (tlen < 3) {
1382 ND_PRINT("\n\t\tran past the end");
1383 return plen + 2;
1384 }
1385 tlv_type = GET_U_1(pptr);
1386 pptr++;
1387 tlv_len = GET_BE_U_2(pptr); /* length, in *bits* */
1388 ttlv_len = (tlv_len + 7)/8; /* length, in *bytes* */
1389 pptr += 2;
1390
1391 switch(tlv_type) {
1392 case 1:
1393 ND_PRINT("\n\t\tcircuit status vector (%u) length: %u: 0x",
1394 tlv_type,
1395 tlv_len);
1396 while (ttlv_len != 0) {
1397 if (tlen < 1) {
1398 ND_PRINT(" (ran past the end)");
1399 return plen + 2;
1400 }
1401 ND_TCHECK_1(pptr);
1402 ND_PRINT("%02x", GET_U_1(pptr));
1403 pptr++;
1404 ttlv_len--;
1405 tlen--;
1406 }
1407 break;
1408 default:
1409 ND_PRINT("\n\t\tunknown TLV #%u, length: %u",
1410 tlv_type,
1411 tlv_len);
1412 if (tlen < ttlv_len) {
1413 ND_PRINT(" (ran past the end)");
1414 return plen + 2;
1415 }
1416 tlen -= ttlv_len;
1417 break;
1418 }
1419 }
1420 return plen + 2;
1421 } else {
1422 /* complain bitterly ? */
1423 /* fall through */
1424 goto trunc;
1425 }
1426
1427 trunc:
1428 return -2;
1429 }
1430
1431 int
1432 decode_prefix6(netdissect_options *ndo,
1433 const u_char *pd, u_int itemlen, char *buf, size_t buflen)
1434 {
1435 nd_ipv6 addr;
1436 u_int plen, plenbytes;
1437
1438 ITEMCHECK(1);
1439 plen = GET_U_1(pd);
1440 if (128 < plen)
1441 return -1;
1442 itemlen -= 1;
1443
1444 memset(&addr, 0, sizeof(addr));
1445 plenbytes = (plen + 7) / 8;
1446 ITEMCHECK(plenbytes);
1447 GET_CPY_BYTES(&addr, pd + 1, plenbytes);
1448 if (plen % 8) {
1449 addr[plenbytes - 1] &=
1450 ((0xff00 >> (plen % 8)) & 0xff);
1451 }
1452 snprintf(buf, buflen, "%s/%u", ip6addr_string(ndo, (const u_char *)&addr), plen);
1453 return 1 + plenbytes;
1454
1455 badtlv:
1456 return -2;
1457 }
1458
1459 static int
1460 print_labeled_prefix6(netdissect_options *ndo,
1461 const u_char *pptr, u_int itemlen)
1462 {
1463 nd_ipv6 addr;
1464 u_int plen, plenbytes;
1465
1466 /* prefix length and label = 4 bytes */
1467 ND_TCHECK_4(pptr);
1468 ITEMCHECK(4);
1469 plen = GET_U_1(pptr); /* get prefix length */
1470
1471 if (24 > plen)
1472 goto badplen;
1473
1474 plen -= 24; /* adjust prefixlen - labellength */
1475
1476 if (128 < plen)
1477 goto badplen;
1478 itemlen -= 4;
1479
1480 memset(&addr, 0, sizeof(addr));
1481 plenbytes = (plen + 7) / 8;
1482 GET_CPY_BYTES(&addr, pptr + 4, plenbytes);
1483 if (plen % 8) {
1484 addr[plenbytes - 1] &=
1485 ((0xff00 >> (plen % 8)) & 0xff);
1486 }
1487 /* the label may get offsetted by 4 bits so lets shift it right */
1488 ND_PRINT("\n\t %s/%u, label:%u %s",
1489 ip6addr_string(ndo, (const u_char *)&addr),
1490 plen,
1491 GET_BE_U_3(pptr + 1)>>4,
1492 ((GET_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1493
1494 return 4 + plenbytes;
1495
1496 badplen:
1497 ND_PRINT("\n\t (illegal prefix length)");
1498 return -1;
1499
1500 trunc:
1501 return -2;
1502
1503 badtlv:
1504 return -3;
1505 }
1506
1507 static int
1508 print_labeled_vpn_prefix6(netdissect_options *ndo, const u_char *pptr)
1509 {
1510 nd_ipv6 addr;
1511 u_int plen;
1512
1513 plen = GET_U_1(pptr); /* get prefix length */
1514
1515 if ((24+64) > plen)
1516 goto badplen;
1517
1518 plen -= (24+64); /* adjust prefixlen - labellength - RD len*/
1519
1520 if (128 < plen)
1521 goto badplen;
1522
1523 memset(&addr, 0, sizeof(addr));
1524 GET_CPY_BYTES(&addr, pptr + 12, (plen + 7) / 8);
1525 if (plen % 8) {
1526 addr[(plen + 7) / 8 - 1] &=
1527 ((0xff00 >> (plen % 8)) & 0xff);
1528 }
1529 /* the label may get offsetted by 4 bits so lets shift it right */
1530 ND_PRINT("\n\t RD: %s, %s/%u, label:%u %s",
1531 bgp_vpn_rd_print(ndo, pptr+4),
1532 ip6addr_string(ndo, (const u_char *)&addr),
1533 plen,
1534 GET_BE_U_3(pptr + 1)>>4,
1535 ((GET_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1536
1537 return 12 + (plen + 7) / 8;
1538
1539 badplen:
1540 ND_PRINT("\n\t (illegal prefix length)");
1541 return -1;
1542 }
1543
1544 static int
1545 print_clnp_prefix(netdissect_options *ndo, const u_char *pptr)
1546 {
1547 uint8_t addr[19];
1548 u_int plen;
1549
1550 plen = GET_U_1(pptr); /* get prefix length */
1551
1552 if (152 < plen)
1553 goto badplen;
1554
1555 memset(&addr, 0, sizeof(addr));
1556 GET_CPY_BYTES(&addr, pptr + 4, (plen + 7) / 8);
1557 if (plen % 8) {
1558 addr[(plen + 7) / 8 - 1] &=
1559 ((0xff00 >> (plen % 8)) & 0xff);
1560 }
1561 /* Cannot use GET_ISONSAP_STRING (not packet buffer pointer) */
1562 ND_PRINT("\n\t %s/%u",
1563 isonsap_string(ndo, addr,(plen + 7) / 8),
1564 plen);
1565
1566 return 1 + (plen + 7) / 8;
1567
1568 badplen:
1569 ND_PRINT("\n\t (illegal prefix length)");
1570 return -1;
1571 }
1572
1573 static int
1574 print_labeled_vpn_clnp_prefix(netdissect_options *ndo, const u_char *pptr)
1575 {
1576 uint8_t addr[19];
1577 u_int plen;
1578
1579 plen = GET_U_1(pptr); /* get prefix length */
1580
1581 if ((24+64) > plen)
1582 goto badplen;
1583
1584 plen -= (24+64); /* adjust prefixlen - labellength - RD len*/
1585
1586 if (152 < plen)
1587 goto badplen;
1588
1589 memset(&addr, 0, sizeof(addr));
1590 GET_CPY_BYTES(&addr, pptr + 12, (plen + 7) / 8);
1591 if (plen % 8) {
1592 addr[(plen + 7) / 8 - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
1593 }
1594 /* the label may get offsetted by 4 bits so lets shift it right */
1595 /* Cannot use GET_ISONSAP_STRING (not packet buffer pointer) */
1596 ND_PRINT("\n\t RD: %s, %s/%u, label:%u %s",
1597 bgp_vpn_rd_print(ndo, pptr+4),
1598 isonsap_string(ndo, addr,(plen + 7) / 8),
1599 plen,
1600 GET_BE_U_3(pptr + 1)>>4,
1601 ((GET_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1602
1603 return 12 + (plen + 7) / 8;
1604
1605 badplen:
1606 ND_PRINT("\n\t (illegal prefix length)");
1607 return -1;
1608 }
1609
1610 /*
1611 * bgp_attr_get_as_size
1612 *
1613 * Try to find the size of the ASs encoded in an as-path. It is not obvious, as
1614 * both Old speakers that do not support 4 byte AS, and the new speakers that do
1615 * support, exchange AS-Path with the same path-attribute type value 0x02.
1616 */
1617 static u_int
1618 bgp_attr_get_as_size(netdissect_options *ndo,
1619 uint8_t bgpa_type, const u_char *pptr, u_int len)
1620 {
1621 const u_char *tptr = pptr;
1622
1623 /*
1624 * If the path attribute is the optional AS4 path type, then we already
1625 * know, that ASs must be encoded in 4 byte format.
1626 */
1627 if (bgpa_type == BGPTYPE_AS4_PATH) {
1628 return 4;
1629 }
1630
1631 /*
1632 * Let us assume that ASs are of 2 bytes in size, and check if the AS-Path
1633 * TLV is good. If not, ask the caller to try with AS encoded as 4 bytes
1634 * each.
1635 */
1636 while (tptr < pptr + len) {
1637 /*
1638 * If we do not find a valid segment type, our guess might be wrong.
1639 */
1640 if (GET_U_1(tptr) < BGP_AS_SEG_TYPE_MIN || GET_U_1(tptr) > BGP_AS_SEG_TYPE_MAX) {
1641 goto trunc;
1642 }
1643 tptr += 2 + GET_U_1(tptr + 1) * 2;
1644 }
1645
1646 /*
1647 * If we correctly reached end of the AS path attribute data content,
1648 * then most likely ASs were indeed encoded as 2 bytes.
1649 */
1650 if (tptr == pptr + len) {
1651 return 2;
1652 }
1653
1654 trunc:
1655
1656 /*
1657 * We can come here, either we did not have enough data, or if we
1658 * try to decode 4 byte ASs in 2 byte format. Either way, return 4,
1659 * so that caller can try to decode each AS as of 4 bytes. If indeed
1660 * there was not enough data, it will crib and end the parse anyways.
1661 */
1662 return 4;
1663 }
1664
1665 /*
1666 * The only way to know that a BGP UPDATE message is using add path is
1667 * by checking if the capability is in the OPEN message which we may have missed.
1668 * So this function checks if it is possible that the update could contain add path
1669 * and if so it checks that standard BGP doesn't make sense.
1670 */
1671 static int
1672 check_add_path(netdissect_options *ndo, const u_char *pptr, u_int length,
1673 u_int max_prefix_length)
1674 {
1675 u_int offset, prefix_length;
1676
1677 if (length < 5) {
1678 return 0;
1679 }
1680
1681 /*
1682 * Scan through the NLRI information under the assumption that
1683 * it doesn't have path IDs.
1684 */
1685 for (offset = 0; offset < length;) {
1686 offset += 4;
1687 if (!ND_TTEST_1(pptr + offset)) {
1688 /* We ran out of captured data; quit scanning. */
1689 break;
1690 }
1691 prefix_length = GET_U_1(pptr + offset);
1692 /*
1693 * Add 4 to cover the path id
1694 * and check the prefix length isn't greater than 32/128.
1695 */
1696 if (prefix_length > max_prefix_length) {
1697 return 0;
1698 }
1699 /* Add 1 for the prefix_length byte and prefix_length to cover the address */
1700 offset += 1 + ((prefix_length + 7) / 8);
1701 }
1702 /* check we haven't gone past the end of the section */
1703 if (offset > length) {
1704 return 0;
1705 }
1706
1707 /* check it's not standard BGP */
1708 for (offset = 0; offset < length; ) {
1709 if (!ND_TTEST_1(pptr + offset)) {
1710 /* We ran out of captured data; quit scanning. */
1711 break;
1712 }
1713 prefix_length = GET_U_1(pptr + offset);
1714 /*
1715 * If the prefix_length is zero (0.0.0.0/0)
1716 * and since it's not the only address (length >= 5)
1717 * then it is add-path
1718 */
1719 if (prefix_length < 1 || prefix_length > max_prefix_length) {
1720 return 1;
1721 }
1722 offset += 1 + ((prefix_length + 7) / 8);
1723 }
1724 if (offset > length) {
1725 return 1;
1726 }
1727
1728 /* assume not add-path by default */
1729 return 0;
1730 }
1731
1732 static int
1733 bgp_mp_af_print(netdissect_options *ndo,
1734 const u_char *tptr, u_int tlen,
1735 uint16_t *afp, uint8_t *safip)
1736 {
1737 uint16_t af;
1738 uint8_t safi;
1739
1740 af = GET_BE_U_2(tptr);
1741 *afp = af;
1742 safi = GET_U_1(tptr + 2);
1743 *safip = safi;
1744
1745 ND_PRINT("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
1746 tok2str(af_values, "Unknown AFI", af),
1747 af,
1748 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1749 tok2str(bgp_safi_values, "Unknown SAFI", safi),
1750 safi);
1751
1752 switch(af<<8 | safi) {
1753 case (AFNUM_IP<<8 | SAFNUM_UNICAST):
1754 case (AFNUM_IP<<8 | SAFNUM_MULTICAST):
1755 case (AFNUM_IP<<8 | SAFNUM_UNIMULTICAST):
1756 case (AFNUM_IP<<8 | SAFNUM_LABUNICAST):
1757 case (AFNUM_IP<<8 | SAFNUM_RT_ROUTING_INFO):
1758 case (AFNUM_IP<<8 | SAFNUM_VPNUNICAST):
1759 case (AFNUM_IP<<8 | SAFNUM_VPNMULTICAST):
1760 case (AFNUM_IP<<8 | SAFNUM_VPNUNIMULTICAST):
1761 case (AFNUM_IP<<8 | SAFNUM_MULTICAST_VPN):
1762 case (AFNUM_IP<<8 | SAFNUM_MDT):
1763 case (AFNUM_IP6<<8 | SAFNUM_UNICAST):
1764 case (AFNUM_IP6<<8 | SAFNUM_MULTICAST):
1765 case (AFNUM_IP6<<8 | SAFNUM_UNIMULTICAST):
1766 case (AFNUM_IP6<<8 | SAFNUM_LABUNICAST):
1767 case (AFNUM_IP6<<8 | SAFNUM_VPNUNICAST):
1768 case (AFNUM_IP6<<8 | SAFNUM_VPNMULTICAST):
1769 case (AFNUM_IP6<<8 | SAFNUM_VPNUNIMULTICAST):
1770 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1771 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1772 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1773 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1774 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1775 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1776 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1777 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1778 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1779 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1780 break;
1781 default:
1782 ND_TCHECK_LEN(tptr, tlen);
1783 ND_PRINT("\n\t no AFI %u / SAFI %u decoder", af, safi);
1784 if (ndo->ndo_vflag <= 1)
1785 print_unknown_data(ndo, tptr, "\n\t ", tlen);
1786 return -1;
1787 }
1788 return 0;
1789 trunc:
1790 return -2;
1791 }
1792
1793 static int
1794 bgp_nlri_print(netdissect_options *ndo, uint16_t af, uint8_t safi,
1795 const u_char *tptr, u_int len,
1796 int add_path4, int add_path6)
1797 {
1798 int advance;
1799 u_int path_id = 0;
1800 char buf[512];
1801
1802 switch (af<<8 | safi) {
1803 case (AFNUM_IP<<8 | SAFNUM_UNICAST):
1804 case (AFNUM_IP<<8 | SAFNUM_MULTICAST):
1805 case (AFNUM_IP<<8 | SAFNUM_UNIMULTICAST):
1806 if (add_path4) {
1807 path_id = GET_BE_U_4(tptr);
1808 tptr += 4;
1809 }
1810 advance = decode_prefix4(ndo, tptr, len, buf, sizeof(buf));
1811 if (advance == -1)
1812 ND_PRINT("\n\t (illegal prefix length)");
1813 else if (advance == -2)
1814 break; /* bytes left, but not enough */
1815 else
1816 ND_PRINT("\n\t %s", buf);
1817 if (add_path4) {
1818 ND_PRINT(" Path Id: %u", path_id);
1819 advance += 4;
1820 }
1821 break;
1822 case (AFNUM_IP<<8 | SAFNUM_LABUNICAST):
1823 advance = print_labeled_prefix4(ndo, tptr, len);
1824 if (advance == -2)
1825 goto trunc;
1826 break;
1827 case (AFNUM_IP<<8 | SAFNUM_VPNUNICAST):
1828 case (AFNUM_IP<<8 | SAFNUM_VPNMULTICAST):
1829 case (AFNUM_IP<<8 | SAFNUM_VPNUNIMULTICAST):
1830 advance = print_labeled_vpn_prefix4(ndo, tptr);
1831 break;
1832 case (AFNUM_IP<<8 | SAFNUM_RT_ROUTING_INFO):
1833 advance = print_rt_routing_info(ndo, tptr);
1834 break;
1835 case (AFNUM_IP<<8 | SAFNUM_MULTICAST_VPN): /* fall through */
1836 case (AFNUM_IP6<<8 | SAFNUM_MULTICAST_VPN):
1837 advance = print_multicast_vpn(ndo, tptr);
1838 if (advance == -2)
1839 goto trunc;
1840 break;
1841
1842 case (AFNUM_IP<<8 | SAFNUM_MDT):
1843 advance = print_mdt_vpn_nlri(ndo, tptr);
1844 if (advance == -2)
1845 goto trunc;
1846 break;
1847 case (AFNUM_IP6<<8 | SAFNUM_UNICAST):
1848 case (AFNUM_IP6<<8 | SAFNUM_MULTICAST):
1849 case (AFNUM_IP6<<8 | SAFNUM_UNIMULTICAST):
1850 if (add_path6) {
1851 path_id = GET_BE_U_4(tptr);
1852 tptr += 4;
1853 }
1854 advance = decode_prefix6(ndo, tptr, len, buf, sizeof(buf));
1855 if (advance == -1)
1856 ND_PRINT("\n\t (illegal prefix length)");
1857 else if (advance == -2)
1858 break; /* bytes left, but not enough */
1859 else
1860 ND_PRINT("\n\t %s", buf);
1861 if (add_path6) {
1862 ND_PRINT(" Path Id: %u", path_id);
1863 advance += 4;
1864 }
1865 break;
1866 case (AFNUM_IP6<<8 | SAFNUM_LABUNICAST):
1867 advance = print_labeled_prefix6(ndo, tptr, len);
1868 if (advance == -2)
1869 goto trunc;
1870 break;
1871 case (AFNUM_IP6<<8 | SAFNUM_VPNUNICAST):
1872 case (AFNUM_IP6<<8 | SAFNUM_VPNMULTICAST):
1873 case (AFNUM_IP6<<8 | SAFNUM_VPNUNIMULTICAST):
1874 advance = print_labeled_vpn_prefix6(ndo, tptr);
1875 break;
1876 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1877 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1878 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1879 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1880 advance = print_labeled_vpn_l2(ndo, tptr);
1881 if (advance == -2)
1882 goto trunc;
1883 break;
1884 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1885 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1886 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1887 advance = print_clnp_prefix(ndo, tptr);
1888 break;
1889 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1890 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1891 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1892 advance = print_labeled_vpn_clnp_prefix(ndo, tptr);
1893 break;
1894 default:
1895 /*
1896 * This should not happen, we should have been protected
1897 * by bgp_mp_af_print()'s return value.
1898 */
1899 ND_PRINT("\n\t ERROR: no AFI %u / SAFI %u decoder", af, safi);
1900 advance = -4;
1901 break;
1902 }
1903 return advance;
1904 trunc: /* we rely on the caller to recognize -2 return value */
1905 return -2;
1906 }
1907
1908 static const struct tok bgp_flags[] = {
1909 { 0x80, "O"},
1910 { 0x40, "T"},
1911 { 0x20, "P"},
1912 { 0x10, "E"},
1913 { 0, NULL }
1914 };
1915
1916 static int
1917 bgp_attr_print(netdissect_options *ndo,
1918 uint8_t atype, const u_char *pptr, u_int len,
1919 const unsigned attr_set_level)
1920 {
1921 /* allocate space for the largest possible string */
1922 char astostr[AS_STR_SIZE];
1923 u_int i;
1924 uint16_t af;
1925 uint8_t safi, snpa, nhlen;
1926 int advance;
1927 u_int tlen;
1928 const u_char *tptr;
1929 u_int as_size;
1930 int add_path4, add_path6;
1931 int ret;
1932
1933 tptr = pptr;
1934 tlen = len;
1935
1936 switch (atype) {
1937 case BGPTYPE_ORIGIN:
1938 if (len != 1)
1939 ND_PRINT("invalid len");
1940 else {
1941 ND_PRINT("%s", tok2str(bgp_origin_values,
1942 "Unknown Origin Typecode",
1943 GET_U_1(tptr)));
1944 }
1945 break;
1946
1947 /*
1948 * Process AS4 byte path and AS2 byte path attributes here.
1949 */
1950 case BGPTYPE_AS4_PATH:
1951 case BGPTYPE_AS_PATH:
1952 if (len % 2) {
1953 ND_PRINT("invalid len");
1954 break;
1955 }
1956 if (!len) {
1957 ND_PRINT("empty");
1958 break;
1959 }
1960
1961 /*
1962 * BGP updates exchanged between New speakers that support 4
1963 * byte AS, ASs are always encoded in 4 bytes. There is no
1964 * definitive way to find this, just by the packet's
1965 * contents. So, check for packet's TLV's sanity assuming
1966 * 2 bytes first, and it does not pass, assume that ASs are
1967 * encoded in 4 bytes format and move on.
1968 */
1969 as_size = bgp_attr_get_as_size(ndo, atype, pptr, len);
1970
1971 while (tptr < pptr + len) {
1972 ND_PRINT("%s", tok2str(bgp_as_path_segment_open_values,
1973 "?", GET_U_1(tptr)));
1974 for (i = 0; i < GET_U_1(tptr + 1) * as_size; i += as_size) {
1975 ND_TCHECK_LEN(tptr + 2 + i, as_size);
1976 ND_PRINT("%s ",
1977 as_printf(ndo, astostr, sizeof(astostr),
1978 as_size == 2 ?
1979 GET_BE_U_2(tptr + i + 2) :
1980 GET_BE_U_4(tptr + i + 2)));
1981 }
1982 ND_PRINT("%s", tok2str(bgp_as_path_segment_close_values,
1983 "?", GET_U_1(tptr)));
1984 tptr += 2 + GET_U_1(tptr + 1) * as_size;
1985 }
1986 break;
1987 case BGPTYPE_NEXT_HOP:
1988 if (len != 4)
1989 ND_PRINT("invalid len");
1990 else {
1991 ND_PRINT("%s", GET_IPADDR_STRING(tptr));
1992 }
1993 break;
1994 case BGPTYPE_MULTI_EXIT_DISC:
1995 case BGPTYPE_LOCAL_PREF:
1996 if (len != 4)
1997 ND_PRINT("invalid len");
1998 else {
1999 ND_PRINT("%u", GET_BE_U_4(tptr));
2000 }
2001 break;
2002 case BGPTYPE_ATOMIC_AGGREGATE:
2003 if (len != 0)
2004 ND_PRINT("invalid len");
2005 break;
2006 case BGPTYPE_AGGREGATOR:
2007
2008 /*
2009 * Depending on the AS encoded is of 2 bytes or of 4 bytes,
2010 * the length of this PA can be either 6 bytes or 8 bytes.
2011 */
2012 if (len != 6 && len != 8) {
2013 ND_PRINT("invalid len");
2014 break;
2015 }
2016 ND_TCHECK_LEN(tptr, len);
2017 if (len == 6) {
2018 ND_PRINT(" AS #%s, origin %s",
2019 as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_2(tptr)),
2020 GET_IPADDR_STRING(tptr + 2));
2021 } else {
2022 ND_PRINT(" AS #%s, origin %s",
2023 as_printf(ndo, astostr, sizeof(astostr),
2024 GET_BE_U_4(tptr)), GET_IPADDR_STRING(tptr + 4));
2025 }
2026 break;
2027 case BGPTYPE_AGGREGATOR4:
2028 if (len != 8) {
2029 ND_PRINT("invalid len");
2030 break;
2031 }
2032 ND_PRINT(" AS #%s, origin %s",
2033 as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_4(tptr)),
2034 GET_IPADDR_STRING(tptr + 4));
2035 break;
2036 case BGPTYPE_COMMUNITIES:
2037 if (len % 4) {
2038 ND_PRINT("invalid len");
2039 break;
2040 }
2041 while (tlen != 0) {
2042 uint32_t comm;
2043 ND_TCHECK_4(tptr);
2044 if (tlen < 4)
2045 goto trunc;
2046 comm = GET_BE_U_4(tptr);
2047 switch (comm) {
2048 case BGP_COMMUNITY_NO_EXPORT:
2049 ND_PRINT(" NO_EXPORT");
2050 break;
2051 case BGP_COMMUNITY_NO_ADVERT:
2052 ND_PRINT(" NO_ADVERTISE");
2053 break;
2054 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED:
2055 ND_PRINT(" NO_EXPORT_SUBCONFED");
2056 break;
2057 default:
2058 ND_PRINT("%u:%u%s",
2059 (comm >> 16) & 0xffff,
2060 comm & 0xffff,
2061 (tlen>4) ? ", " : "");
2062 break;
2063 }
2064 tlen -=4;
2065 tptr +=4;
2066 }
2067 break;
2068 case BGPTYPE_ORIGINATOR_ID:
2069 if (len != 4) {
2070 ND_PRINT("invalid len");
2071 break;
2072 }
2073 ND_PRINT("%s",GET_IPADDR_STRING(tptr));
2074 break;
2075 case BGPTYPE_CLUSTER_LIST:
2076 if (len % 4) {
2077 ND_PRINT("invalid len");
2078 break;
2079 }
2080 while (tlen != 0) {
2081 if (tlen < 4)
2082 goto trunc;
2083 ND_PRINT("%s%s",
2084 GET_IPADDR_STRING(tptr),
2085 (tlen>4) ? ", " : "");
2086 tlen -=4;
2087 tptr +=4;
2088 }
2089 break;
2090 case BGPTYPE_MP_REACH_NLRI:
2091 ND_TCHECK_3(tptr);
2092 if (tlen < 3)
2093 goto trunc;
2094 ret = bgp_mp_af_print(ndo, tptr, tlen, &af, &safi);
2095 if (ret == -2)
2096 goto trunc;
2097 if (ret < 0)
2098 break;
2099
2100 tptr += 3;
2101 tlen -= 3;
2102
2103 ND_TCHECK_1(tptr);
2104 if (tlen < 1)
2105 goto trunc;
2106 nhlen = GET_U_1(tptr);
2107 tptr++;
2108 tlen--;
2109
2110 if (nhlen) {
2111 u_int nnh = 0;
2112 uint8_t tnhlen = nhlen;
2113 if (tlen < tnhlen)
2114 goto trunc;
2115 ND_PRINT("\n\t nexthop: ");
2116 while (tnhlen != 0) {
2117 if (nnh++ > 0) {
2118 ND_PRINT(", " );
2119 }
2120 switch(af<<8 | safi) {
2121 case (AFNUM_IP<<8 | SAFNUM_UNICAST):
2122 case (AFNUM_IP<<8 | SAFNUM_MULTICAST):
2123 case (AFNUM_IP<<8 | SAFNUM_UNIMULTICAST):
2124 case (AFNUM_IP<<8 | SAFNUM_LABUNICAST):
2125 case (AFNUM_IP<<8 | SAFNUM_RT_ROUTING_INFO):
2126 case (AFNUM_IP<<8 | SAFNUM_MULTICAST_VPN):
2127 case (AFNUM_IP<<8 | SAFNUM_MDT):
2128 if (tnhlen < sizeof(nd_ipv4)) {
2129 ND_PRINT("invalid len");
2130 tptr += tnhlen;
2131 tlen -= tnhlen;
2132 tnhlen = 0;
2133 } else {
2134 ND_PRINT("%s",GET_IPADDR_STRING(tptr));
2135 tptr += sizeof(nd_ipv4);
2136 tnhlen -= sizeof(nd_ipv4);
2137 tlen -= sizeof(nd_ipv4);
2138 }
2139 break;
2140 case (AFNUM_IP<<8 | SAFNUM_VPNUNICAST):
2141 case (AFNUM_IP<<8 | SAFNUM_VPNMULTICAST):
2142 case (AFNUM_IP<<8 | SAFNUM_VPNUNIMULTICAST):
2143 if (tnhlen < sizeof(nd_ipv4)+BGP_VPN_RD_LEN) {
2144 ND_PRINT("invalid len");
2145 tptr += tnhlen;
2146 tlen -= tnhlen;
2147 tnhlen = 0;
2148 } else {
2149 ND_PRINT("RD: %s, %s",
2150 bgp_vpn_rd_print(ndo, tptr),
2151 GET_IPADDR_STRING(tptr+BGP_VPN_RD_LEN));
2152 tptr += (sizeof(nd_ipv4)+BGP_VPN_RD_LEN);
2153 tlen -= (sizeof(nd_ipv4)+BGP_VPN_RD_LEN);
2154 tnhlen -= (sizeof(nd_ipv4)+BGP_VPN_RD_LEN);
2155 }
2156 break;
2157 case (AFNUM_IP6<<8 | SAFNUM_UNICAST):
2158 case (AFNUM_IP6<<8 | SAFNUM_MULTICAST):
2159 case (AFNUM_IP6<<8 | SAFNUM_UNIMULTICAST):
2160 case (AFNUM_IP6<<8 | SAFNUM_LABUNICAST):
2161 if (tnhlen < sizeof(nd_ipv6)) {
2162 ND_PRINT("invalid len");
2163 tptr += tnhlen;
2164 tlen -= tnhlen;
2165 tnhlen = 0;
2166 } else {
2167 ND_PRINT("%s", GET_IP6ADDR_STRING(tptr));
2168 tptr += sizeof(nd_ipv6);
2169 tlen -= sizeof(nd_ipv6);
2170 tnhlen -= sizeof(nd_ipv6);
2171 }
2172 break;
2173 case (AFNUM_IP6<<8 | SAFNUM_VPNUNICAST):
2174 case (AFNUM_IP6<<8 | SAFNUM_VPNMULTICAST):
2175 case (AFNUM_IP6<<8 | SAFNUM_VPNUNIMULTICAST):
2176 if (tnhlen < sizeof(nd_ipv6)+BGP_VPN_RD_LEN) {
2177 ND_PRINT("invalid len");
2178 tptr += tnhlen;
2179 tlen -= tnhlen;
2180 tnhlen = 0;
2181 } else {
2182 ND_PRINT("RD: %s, %s",
2183 bgp_vpn_rd_print(ndo, tptr),
2184 GET_IP6ADDR_STRING(tptr+BGP_VPN_RD_LEN));
2185 tptr += (sizeof(nd_ipv6)+BGP_VPN_RD_LEN);
2186 tlen -= (sizeof(nd_ipv6)+BGP_VPN_RD_LEN);
2187 tnhlen -= (sizeof(nd_ipv6)+BGP_VPN_RD_LEN);
2188 }
2189 break;
2190 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
2191 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
2192 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
2193 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
2194 if (tnhlen < sizeof(nd_ipv4)) {
2195 ND_PRINT("invalid len");
2196 tptr += tnhlen;
2197 tlen -= tnhlen;
2198 tnhlen = 0;
2199 } else {
2200 ND_PRINT("%s", GET_IPADDR_STRING(tptr));
2201 tptr += (sizeof(nd_ipv4));
2202 tlen -= (sizeof(nd_ipv4));
2203 tnhlen -= (sizeof(nd_ipv4));
2204 }
2205 break;
2206 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
2207 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
2208 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
2209 ND_PRINT("%s", GET_ISONSAP_STRING(tptr, tnhlen));
2210 tptr += tnhlen;
2211 tlen -= tnhlen;
2212 tnhlen = 0;
2213 break;
2214
2215 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
2216 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
2217 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
2218 if (tnhlen < BGP_VPN_RD_LEN+1) {
2219 ND_PRINT("invalid len");
2220 tptr += tnhlen;
2221 tlen -= tnhlen;
2222 tnhlen = 0;
2223 } else {
2224 ND_TCHECK_LEN(tptr, tnhlen);
2225 ND_PRINT("RD: %s, %s",
2226 bgp_vpn_rd_print(ndo, tptr),
2227 GET_ISONSAP_STRING(tptr+BGP_VPN_RD_LEN,tnhlen-BGP_VPN_RD_LEN));
2228 /* rfc986 mapped IPv4 address ? */
2229 if (GET_BE_U_4(tptr + BGP_VPN_RD_LEN) == 0x47000601)
2230 ND_PRINT(" = %s", GET_IPADDR_STRING(tptr+BGP_VPN_RD_LEN+4));
2231 /* rfc1888 mapped IPv6 address ? */
2232 else if (GET_BE_U_3(tptr + BGP_VPN_RD_LEN) == 0x350000)
2233 ND_PRINT(" = %s", GET_IP6ADDR_STRING(tptr+BGP_VPN_RD_LEN+3));
2234 tptr += tnhlen;
2235 tlen -= tnhlen;
2236 tnhlen = 0;
2237 }
2238 break;
2239 default:
2240 /*
2241 * bgp_mp_af_print() should have saved us from
2242 * an unsupported AFI/SAFI.
2243 */
2244 ND_PRINT("ERROR: no AFI %u/SAFI %u nexthop decoder", af, safi);
2245 tptr += tnhlen;
2246 tlen -= tnhlen;
2247 tnhlen = 0;
2248 goto done;
2249 }
2250 }
2251 }
2252 ND_PRINT(", nh-length: %u", nhlen);
2253
2254 /* As per RFC 2858; this is reserved in RFC 4760 */
2255 if (tlen < 1)
2256 goto trunc;
2257 snpa = GET_U_1(tptr);
2258 tptr++;
2259 tlen--;
2260
2261 if (snpa) {
2262 ND_PRINT("\n\t %u SNPA", snpa);
2263 for (/*nothing*/; snpa != 0; snpa--) {
2264 uint8_t snpalen;
2265 if (tlen < 1)
2266 goto trunc;
2267 snpalen = GET_U_1(tptr);
2268 ND_PRINT("\n\t %u bytes", snpalen);
2269 tptr++;
2270 tlen--;
2271 if (tlen < snpalen)
2272 goto trunc;
2273 ND_TCHECK_LEN(tptr, snpalen);
2274 tptr += snpalen;
2275 tlen -= snpalen;
2276 }
2277 } else {
2278 ND_PRINT(", no SNPA");
2279 }
2280
2281 add_path4 = check_add_path(ndo, tptr,
2282 (len-ND_BYTES_BETWEEN(pptr, tptr)), 32);
2283 add_path6 = check_add_path(ndo, tptr,
2284 (len-ND_BYTES_BETWEEN(pptr, tptr)), 128);
2285
2286 while (tptr < pptr + len) {
2287 advance = bgp_nlri_print(ndo, af, safi, tptr, len,
2288 add_path4, add_path6);
2289 if (advance == -2)
2290 goto trunc;
2291 if (advance < 0)
2292 break;
2293 tptr += advance;
2294 }
2295 break;
2296
2297 case BGPTYPE_MP_UNREACH_NLRI:
2298 ND_TCHECK_LEN(tptr, BGP_MP_NLRI_MINSIZE);
2299 ret = bgp_mp_af_print(ndo, tptr, tlen, &af, &safi);
2300 if (ret == -2)
2301 goto trunc;
2302 if (ret < 0)
2303 break;
2304
2305 if (len == BGP_MP_NLRI_MINSIZE)
2306 ND_PRINT("\n\t End-of-Rib Marker (empty NLRI)");
2307
2308 tptr += 3;
2309
2310 add_path4 = check_add_path(ndo, tptr,
2311 (len-ND_BYTES_BETWEEN(pptr, tptr)), 32);
2312 add_path6 = check_add_path(ndo, tptr,
2313 (len-ND_BYTES_BETWEEN(pptr, tptr)), 128);
2314
2315 while (tptr < pptr + len) {
2316 advance = bgp_nlri_print(ndo, af, safi, tptr, len,
2317 add_path4, add_path6);
2318 if (advance == -2)
2319 goto trunc;
2320 if (advance < 0)
2321 break;
2322 tptr += advance;
2323 }
2324 break;
2325 case BGPTYPE_EXTD_COMMUNITIES:
2326 if (len % 8) {
2327 ND_PRINT("invalid len");
2328 break;
2329 }
2330 while (tlen != 0) {
2331 uint16_t extd_comm;
2332
2333 ND_TCHECK_2(tptr);
2334 if (tlen < 2)
2335 goto trunc;
2336 extd_comm=GET_BE_U_2(tptr);
2337
2338 ND_PRINT("\n\t %s (0x%04x), Flags [%s]",
2339 tok2str(bgp_extd_comm_subtype_values,
2340 "unknown extd community typecode",
2341 extd_comm),
2342 extd_comm,
2343 bittok2str(bgp_extd_comm_flag_values, "none", extd_comm));
2344
2345 ND_TCHECK_8(tptr);
2346 if (tlen < 8)
2347 goto trunc;
2348 ND_PRINT(": ");
2349 bgp_extended_community_print(ndo, tptr);
2350 tlen -= 8;
2351 tptr += 8;
2352 }
2353 break;
2354
2355 case BGPTYPE_PMSI_TUNNEL:
2356 {
2357 uint8_t tunnel_type, flags;
2358
2359 ND_TCHECK_5(tptr);
2360 if (tlen < 5)
2361 goto trunc;
2362 flags = GET_U_1(tptr);
2363 tunnel_type = GET_U_1(tptr + 1);
2364
2365 ND_PRINT("\n\t Tunnel-type %s (%u), Flags [%s], MPLS Label %u",
2366 tok2str(bgp_pmsi_tunnel_values, "Unknown", tunnel_type),
2367 tunnel_type,
2368 bittok2str(bgp_pmsi_flag_values, "none", flags),
2369 GET_BE_U_3(tptr + 2)>>4);
2370
2371 tptr +=5;
2372 tlen -= 5;
2373
2374 switch (tunnel_type) {
2375 case BGP_PMSI_TUNNEL_PIM_SM: /* fall through */
2376 case BGP_PMSI_TUNNEL_PIM_BIDIR:
2377 ND_PRINT("\n\t Sender %s, P-Group %s",
2378 GET_IPADDR_STRING(tptr),
2379 GET_IPADDR_STRING(tptr+4));
2380 break;
2381
2382 case BGP_PMSI_TUNNEL_PIM_SSM:
2383 ND_PRINT("\n\t Root-Node %s, P-Group %s",
2384 GET_IPADDR_STRING(tptr),
2385 GET_IPADDR_STRING(tptr+4));
2386 break;
2387 case BGP_PMSI_TUNNEL_INGRESS:
2388 ND_PRINT("\n\t Tunnel-Endpoint %s",
2389 GET_IPADDR_STRING(tptr));
2390 break;
2391 case BGP_PMSI_TUNNEL_LDP_P2MP: /* fall through */
2392 case BGP_PMSI_TUNNEL_LDP_MP2MP:
2393 ND_PRINT("\n\t Root-Node %s, LSP-ID 0x%08x",
2394 GET_IPADDR_STRING(tptr),
2395 GET_BE_U_4(tptr + 4));
2396 break;
2397 case BGP_PMSI_TUNNEL_RSVP_P2MP:
2398 ND_PRINT("\n\t Extended-Tunnel-ID %s, P2MP-ID 0x%08x",
2399 GET_IPADDR_STRING(tptr),
2400 GET_BE_U_4(tptr + 4));
2401 break;
2402 default:
2403 if (ndo->ndo_vflag <= 1) {
2404 print_unknown_data(ndo, tptr, "\n\t ", tlen);
2405 }
2406 }
2407 break;
2408 }
2409 case BGPTYPE_AIGP:
2410 {
2411 uint8_t type;
2412 uint16_t length;
2413
2414 while (tlen >= 3) {
2415 type = GET_U_1(tptr);
2416 length = GET_BE_U_2(tptr + 1);
2417 tptr += 3;
2418 tlen -= 3;
2419
2420 ND_PRINT("\n\t %s TLV (%u), length %u",
2421 tok2str(bgp_aigp_values, "Unknown", type),
2422 type, length);
2423
2424 if (length < 3)
2425 goto trunc;
2426 length -= 3;
2427
2428 /*
2429 * Check if we can read the TLV data.
2430 */
2431 if (tlen < length)
2432 goto trunc;
2433
2434 switch (type) {
2435
2436 case BGP_AIGP_TLV:
2437 if (length < 8)
2438 goto trunc;
2439 ND_PRINT(", metric %" PRIu64,
2440 GET_BE_U_8(tptr));
2441 break;
2442
2443 default:
2444 if (ndo->ndo_vflag <= 1) {
2445 print_unknown_data(ndo, tptr,"\n\t ", length);
2446 }
2447 }
2448
2449 tptr += length;
2450 tlen -= length;
2451 }
2452 break;
2453 }
2454 case BGPTYPE_ATTR_SET:
2455 ND_TCHECK_4(tptr);
2456 if (len < 4)
2457 goto trunc;
2458 ND_PRINT("\n\t Origin AS: %s",
2459 as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_4(tptr)));
2460 tptr += 4;
2461 len -= 4;
2462
2463 while (len) {
2464 u_int aflags, alenlen, alen;
2465
2466 ND_TCHECK_2(tptr);
2467 if (len < 2) {
2468 ND_PRINT(" [path attr too short]");
2469 tptr += len;
2470 break;
2471 }
2472 aflags = GET_U_1(tptr);
2473 atype = GET_U_1(tptr + 1);
2474 tptr += 2;
2475 len -= 2;
2476 alenlen = bgp_attr_lenlen(aflags, tptr);
2477 ND_TCHECK_LEN(tptr, alenlen);
2478 if (len < alenlen) {
2479 ND_PRINT(" [path attr too short]");
2480 tptr += len;
2481 break;
2482 }
2483 alen = bgp_attr_len(aflags, tptr);
2484 tptr += alenlen;
2485 len -= alenlen;
2486
2487 ND_PRINT("\n\t %s (%u), length: %u",
2488 tok2str(bgp_attr_values,
2489 "Unknown Attribute", atype),
2490 atype,
2491 alen);
2492
2493 if (aflags) {
2494 ND_PRINT(", Flags [%s",
2495 bittok2str_nosep(bgp_flags, "", aflags));
2496 if (aflags & 0xf)
2497 ND_PRINT("+%x", aflags & 0xf);
2498 ND_PRINT("]");
2499 }
2500 ND_PRINT(": ");
2501 if (len < alen) {
2502 ND_PRINT(" [path attr too short]");
2503 tptr += len;
2504 break;
2505 }
2506 /*
2507 * The protocol encoding per se allows ATTR_SET to be nested
2508 * as many times as the message can accommodate. This printer
2509 * used to be able to recurse into ATTR_SET contents until the
2510 * stack exhaustion, but now there is a limit on that (if live
2511 * protocol exchange goes that many levels deep, something is
2512 * probably wrong anyway). Feel free to refine this value if
2513 * you can find the spec with respective normative text.
2514 */
2515 if (attr_set_level == 10)
2516 ND_PRINT("(too many nested levels, not recursing)");
2517 else if (!bgp_attr_print(ndo, atype, tptr, alen, attr_set_level + 1))
2518 return 0;
2519 tptr += alen;
2520 len -= alen;
2521 }
2522 break;
2523
2524 case BGPTYPE_LARGE_COMMUNITY:
2525 if (len == 0 || len % 12) {
2526 ND_PRINT("invalid len");
2527 break;
2528 }
2529 ND_PRINT("\n\t ");
2530 while (len != 0) {
2531 ND_PRINT("%u:%u:%u%s",
2532 GET_BE_U_4(tptr),
2533 GET_BE_U_4(tptr + 4),
2534 GET_BE_U_4(tptr + 8),
2535 (len > 12) ? ", " : "");
2536 tptr += 12;
2537 /*
2538 * len will always be a multiple of 12, as per the above,
2539 * so this will never underflow.
2540 */
2541 len -= 12;
2542 }
2543 break;
2544 case BGPTYPE_BGPSEC_PATH:
2545 {
2546 uint16_t sblen, splen;
2547
2548 splen = GET_BE_U_2(tptr);
2549
2550 /*
2551 * A secure path has a minimum length of 8 bytes:
2552 * 2 bytes length field
2553 * 6 bytes per secure path segment
2554 */
2555 ND_ICHECKMSG_U("secure path length", splen, <, 8);
2556
2557 ND_PRINT("\n\t Secure Path Length: %u", splen);
2558
2559 tptr += 2;
2560 splen -= 2;
2561 /* Make sure the secure path length does not signal trailing bytes */
2562 if (splen % 6) {
2563 ND_PRINT(" [total segments length %u != N x 6]", splen);
2564 goto invalid;
2565 }
2566
2567 /* Parse secure path segments */
2568 while (splen != 0) {
2569 uint8_t pcount = GET_U_1(tptr);
2570 uint8_t flags = GET_U_1(tptr + 1);
2571 uint32_t asn = GET_BE_U_4(tptr + 2);
2572 ND_PRINT("\n\t Secure Path Segment: pCount: %u, Flags: [%s] (0x%02x), AS: %u",
2573 pcount,
2574 bittok2str(bgp_bgpsec_bitmap_str, "none", flags),
2575 flags,
2576 asn);
2577 tptr += 6;
2578 splen -= 6;
2579 }
2580
2581 sblen = GET_BE_U_2(tptr);
2582
2583 ND_PRINT("\n\t Signature Block: Length: %u, Algo ID: %u",
2584 sblen,
2585 GET_U_1(tptr + 2));
2586
2587 tptr += 3;
2588 sblen -= 3;
2589 /* Parse signature segments */
2590 while (sblen != 0) {
2591 uint16_t siglen;
2592
2593 ND_PRINT("\n\t Signature Segment:\n\t SKI: ");
2594 ND_ICHECKMSG_U("remaining length", sblen, <, 20);
2595 hex_print(ndo, "\n\t ", tptr, 20);
2596 tptr += 20;
2597 sblen -= 20;
2598 ND_ICHECKMSG_U("remaining length", sblen, <, 2);
2599 siglen = GET_BE_U_2(tptr);
2600 tptr += 2;
2601 sblen -= 2;
2602
2603 ND_PRINT("\n\t Length: %u", siglen);
2604 ND_ICHECKMSG_U("remaining length", sblen, <, siglen);
2605 ND_PRINT("\n\t Signature:");
2606 hex_print(ndo, "\n\t ", tptr, siglen);
2607 tptr += siglen;
2608 sblen -= siglen;
2609 }
2610 break;
2611 }
2612 case BGPTYPE_OTC:
2613 {
2614 if (len < 4) {
2615 ND_PRINT("invalid len");
2616 break;
2617 }
2618 ND_PRINT("\n\t OTC %u", GET_BE_U_4(pptr));
2619 break;
2620 }
2621 default:
2622 ND_TCHECK_LEN(pptr, len);
2623 ND_PRINT("\n\t no Attribute %u decoder", atype); /* we have no decoder for the attribute */
2624 if (ndo->ndo_vflag <= 1)
2625 print_unknown_data(ndo, pptr, "\n\t ", len);
2626 break;
2627 }
2628 done:
2629 if (ndo->ndo_vflag > 1 && len) { /* omit zero length attributes*/
2630 ND_TCHECK_LEN(pptr, len);
2631 print_unknown_data(ndo, pptr, "\n\t ", len);
2632 }
2633 return 1;
2634
2635 invalid:
2636 nd_print_invalid(ndo);
2637 return 1;
2638
2639 trunc:
2640 return 0;
2641 }
2642
2643 static void
2644 bgp_capabilities_print(netdissect_options *ndo,
2645 const u_char *opt, u_int caps_len)
2646 {
2647 /* allocate space for the largest possible string */
2648 char astostr[AS_STR_SIZE];
2649 u_int cap_type, cap_len, tcap_len, cap_offset;
2650 u_int i = 0;
2651
2652 while (i < caps_len) {
2653 ND_TCHECK_LEN(opt + i, BGP_CAP_HEADER_SIZE);
2654 cap_type=GET_U_1(opt + i);
2655 cap_len=GET_U_1(opt + i + 1);
2656 ND_PRINT("\n\t %s (%u), length: %u",
2657 tok2str(bgp_capcode_values, "Unknown", cap_type),
2658 cap_type,
2659 cap_len);
2660 ND_TCHECK_LEN(opt + 2 + i, cap_len);
2661 switch (cap_type) {
2662 case BGP_CAPCODE_MP:
2663 /* AFI (16 bits), Reserved (8 bits), SAFI (8 bits) */
2664 if (cap_len < 4) {
2665 ND_PRINT(" (too short, < 4)");
2666 return;
2667 }
2668 ND_PRINT("\n\t\tAFI %s (%u), SAFI %s (%u)",
2669 tok2str(af_values, "Unknown", GET_BE_U_2(opt + i + 2)),
2670 GET_BE_U_2(opt + i + 2),
2671 tok2str(bgp_safi_values, "Unknown", GET_U_1(opt + i + 5)),
2672 GET_U_1(opt + i + 5));
2673 break;
2674 case BGP_CAPCODE_BGPSEC:
2675 /* Version (4 bits), Direction (1 bit), Flags (3 bits), AFI (16 bits) */
2676 cap_offset = 1;
2677 /* The capability length [...] MUST be set to 3. */
2678 if (cap_len != 3) {
2679 ND_PRINT(" [%u != 3]", cap_len);
2680 return;
2681 }
2682
2683 ND_PRINT("\n\t\tVersion %u, Direction %s (%u), AFI %s (%u)",
2684 GET_U_1(opt + i + 2)&0xf0,
2685 (GET_U_1(opt + i + 2)&0x08) ? "Send" : "Receive",
2686 (GET_U_1(opt + i + 2)&0x08)>>3,
2687 bittok2str(af_values, "Unknown",
2688 GET_BE_U_2(opt + i + cap_offset + 2)),
2689 GET_BE_U_2(opt + i + cap_offset + 2));
2690 break;
2691 case BGP_CAPCODE_ML:
2692 cap_offset = 2;
2693 tcap_len = cap_len;
2694 while (tcap_len >= 4) {
2695 ND_PRINT( "\n\t\tAFI %s (%u), SAFI %s (%u), Count: %u",
2696 tok2str(af_values, "Unknown",
2697 GET_BE_U_2(opt + i + cap_offset)),
2698 GET_BE_U_2(opt + i + cap_offset),
2699 tok2str(bgp_safi_values, "Unknown",
2700 GET_U_1(opt + i + cap_offset + 2)),
2701 GET_U_1(opt + i + cap_offset + 2),
2702 GET_U_1(opt + i + cap_offset + 3));
2703 tcap_len -= 4;
2704 cap_offset += 4;
2705 }
2706 break;
2707 case BGP_CAPCODE_RESTART:
2708 /* Restart Flags (4 bits), Restart Time in seconds (12 bits) */
2709 if (cap_len < 2) {
2710 ND_PRINT(" (too short, < 2)");
2711 return;
2712 }
2713 tcap_len=cap_len;
2714 ND_PRINT("\n\t\tRestart Flags: [%s], Restart Time %us",
2715 bittok2str(bgp_graceful_restart_comm_flag_values,
2716 "none", GET_U_1(opt + i + 2) >> 4),
2717 GET_BE_U_2(opt + i + 2)&0xfff);
2718 tcap_len-=2;
2719 cap_offset=4;
2720 while(tcap_len>=4) {
2721 ND_PRINT("\n\t\t AFI %s (%u), SAFI %s (%u), Forwarding state preserved: %s",
2722 tok2str(af_values,"Unknown",
2723 GET_BE_U_2(opt + i + cap_offset)),
2724 GET_BE_U_2(opt + i + cap_offset),
2725 tok2str(bgp_safi_values,"Unknown",
2726 GET_U_1(opt + i + cap_offset + 2)),
2727 GET_U_1(opt + (i + cap_offset + 2)),
2728 ((GET_U_1(opt + (i + cap_offset + 3)))&0x80) ? "yes" : "no" );
2729 tcap_len -= 4;
2730 cap_offset += 4;
2731 }
2732 break;
2733 case BGP_CAPCODE_ROLE:
2734 if (cap_len < 1) {
2735 ND_PRINT(" (too short, < 1)");
2736 return;
2737 }
2738 ND_PRINT("\n\t\tRole name %s (%u)",
2739 tok2str(bgp_role_values, "Unassigned",
2740 GET_U_1(opt + i + 2)), GET_U_1(opt + i + 2));
2741 break;
2742 case BGP_CAPCODE_RR:
2743 case BGP_CAPCODE_LLGR:
2744 case BGP_CAPCODE_RR_CISCO:
2745 case BGP_CAPCODE_EXT_MSG:
2746 case BGP_CAPCODE_ENH_RR:
2747 break;
2748 case BGP_CAPCODE_AS_NEW:
2749 /*
2750 * Extract the 4 byte AS number encoded.
2751 */
2752 if (cap_len < 4) {
2753 ND_PRINT(" (too short, < 4)");
2754 return;
2755 }
2756 ND_PRINT("\n\t\t 4 Byte AS %s",
2757 as_printf(ndo, astostr, sizeof(astostr),
2758 GET_BE_U_4(opt + i + 2)));
2759 break;
2760 case BGP_CAPCODE_ADD_PATH:
2761 if (cap_len == 0) {
2762 ND_PRINT(" (bogus)"); /* length */
2763 break;
2764 }
2765 tcap_len=cap_len;
2766 cap_offset=2;
2767 while (tcap_len != 0) {
2768 if (tcap_len < 4) {
2769 nd_print_invalid(ndo);
2770 break;
2771 }
2772 ND_PRINT("\n\t\tAFI %s (%u), SAFI %s (%u), Send/Receive: %s",
2773 tok2str(af_values,"Unknown",GET_BE_U_2(opt + i + cap_offset)),
2774 GET_BE_U_2(opt + i + cap_offset),
2775 tok2str(bgp_safi_values,"Unknown",GET_U_1(opt + i + cap_offset + 2)),
2776 GET_U_1(opt + (i + cap_offset + 2)),
2777 tok2str(bgp_add_path_recvsend,"Bogus (0x%02x)",GET_U_1(opt + i + cap_offset + 3))
2778 );
2779 tcap_len -= 4;
2780 cap_offset += 4;
2781 }
2782 break;
2783 default:
2784 ND_PRINT("\n\t\tno decoder for Capability %u",
2785 cap_type);
2786 if (ndo->ndo_vflag <= 1)
2787 print_unknown_data(ndo, opt + i + 2, "\n\t\t",
2788 cap_len);
2789 break;
2790 }
2791 if (ndo->ndo_vflag > 1 && cap_len != 0) {
2792 print_unknown_data(ndo, opt + i + 2, "\n\t\t", cap_len);
2793 }
2794 i += BGP_CAP_HEADER_SIZE + cap_len;
2795 }
2796 return;
2797
2798 trunc:
2799 nd_print_trunc(ndo);
2800 }
2801
2802 static void
2803 bgp_open_print(netdissect_options *ndo,
2804 const u_char *dat, u_int length)
2805 {
2806 /* allocate space for the largest possible string */
2807 char astostr[AS_STR_SIZE];
2808 const struct bgp_open *bgp_open_header;
2809 u_int optslen;
2810 uint8_t opsttype;
2811 const struct bgp_opt *bgpopt;
2812 const u_char *opt;
2813 u_int i;
2814 uint8_t extended_opt_params = 0;
2815 u_int open_size = BGP_OPEN_SIZE;
2816 u_int opt_size = BGP_OPT_SIZE;
2817
2818 ND_TCHECK_LEN(dat, BGP_OPEN_SIZE);
2819 if (length < BGP_OPEN_SIZE)
2820 goto trunc;
2821
2822 bgp_open_header = (const struct bgp_open *)dat;
2823
2824 ND_PRINT("\n\t Version %u, ",
2825 GET_U_1(bgp_open_header->bgpo_version));
2826 ND_PRINT("my AS %s, ",
2827 as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_2(bgp_open_header->bgpo_myas)));
2828 ND_PRINT("Holdtime %us, ",
2829 GET_BE_U_2(bgp_open_header->bgpo_holdtime));
2830 ND_PRINT("ID %s", GET_IPADDR_STRING(bgp_open_header->bgpo_id));
2831 optslen = GET_U_1(bgp_open_header->bgpo_optlen);
2832 opsttype = GET_U_1(bgp_open_header->bgpo_opttype);
2833 if (opsttype == BGP_OPEN_NON_EXT_OPT_TYPE_EXTENDED_LENGTH) {
2834 optslen = GET_BE_U_2(bgp_open_header->bgpo_optlen_extended);
2835 extended_opt_params = 1;
2836 open_size += 3;
2837 opt_size += 1;
2838 }
2839 ND_PRINT("\n\t Optional parameters%s, length: %u",
2840 extended_opt_params ? " (Extended)" : "", optslen);
2841
2842 opt = dat + open_size;
2843 length -= open_size;
2844
2845 i = 0;
2846 while (i < optslen) {
2847 uint8_t opt_type;
2848 uint16_t opt_len;
2849
2850 ND_TCHECK_LEN(opt + i, opt_size);
2851 if (length < opt_size + i)
2852 goto trunc;
2853 bgpopt = (const struct bgp_opt *)(opt + i);
2854 opt_type = GET_U_1(bgpopt->bgpopt_type);
2855 opt_len = extended_opt_params ? GET_BE_U_2(bgpopt->bgpopt_len)
2856 : GET_U_1(bgpopt->bgpopt_len);
2857 if (opt_size + i + opt_len > optslen) {
2858 ND_PRINT("\n\t Option %u, length: %u, goes past the end of the options",
2859 opt_type, opt_len);
2860 break;
2861 }
2862
2863 ND_PRINT("\n\t Option %s (%u), length: %u",
2864 tok2str(bgp_opt_values,"Unknown",opt_type),
2865 opt_type,
2866 opt_len);
2867
2868 /* now let's decode the options we know*/
2869 switch(opt_type) {
2870
2871 case BGP_OPT_CAP:
2872 bgp_capabilities_print(ndo, opt + opt_size + i,
2873 opt_len);
2874 break;
2875
2876 case BGP_OPT_AUTH:
2877 default:
2878 ND_PRINT("\n\t no decoder for option %u",
2879 opt_type);
2880 break;
2881 }
2882 i += opt_size + opt_len;
2883 }
2884 return;
2885 trunc:
2886 nd_print_trunc(ndo);
2887 }
2888
2889 static void
2890 bgp_update_print(netdissect_options *ndo,
2891 const u_char *dat, u_int length)
2892 {
2893 const u_char *p;
2894 u_int withdrawn_routes_len;
2895 char buf[512];
2896 int wpfx;
2897 u_int len;
2898 int i;
2899 int add_path;
2900 u_int path_id = 0;
2901
2902 ND_TCHECK_LEN(dat, BGP_SIZE);
2903 if (length < BGP_SIZE)
2904 goto trunc;
2905 p = dat + BGP_SIZE;
2906 length -= BGP_SIZE;
2907
2908 /* Unfeasible routes */
2909 ND_TCHECK_2(p);
2910 if (length < 2)
2911 goto trunc;
2912 withdrawn_routes_len = GET_BE_U_2(p);
2913 p += 2;
2914 length -= 2;
2915 if (withdrawn_routes_len > 1) {
2916 /*
2917 * Without keeping state from the original NLRI message,
2918 * it's not possible to tell if this a v4 or v6 route,
2919 * so only try to decode it if we're not v6 enabled.
2920 */
2921 ND_TCHECK_LEN(p, withdrawn_routes_len);
2922 if (length < withdrawn_routes_len)
2923 goto trunc;
2924 ND_PRINT("\n\t Withdrawn routes:");
2925 add_path = check_add_path(ndo, p, withdrawn_routes_len, 32);
2926 while (withdrawn_routes_len != 0) {
2927 if (add_path) {
2928 if (withdrawn_routes_len < 4) {
2929 p += withdrawn_routes_len;
2930 length -= withdrawn_routes_len;
2931 break;
2932 }
2933 path_id = GET_BE_U_4(p);
2934 p += 4;
2935 length -= 4;
2936 withdrawn_routes_len -= 4;
2937 }
2938 wpfx = decode_prefix4(ndo, p, withdrawn_routes_len, buf, sizeof(buf));
2939 if (wpfx == -1) {
2940 ND_PRINT("\n\t (illegal prefix length)");
2941 break;
2942 } else if (wpfx == -2)
2943 goto trunc; /* bytes left, but not enough */
2944 else {
2945 ND_PRINT("\n\t %s", buf);
2946 if (add_path) {
2947 ND_PRINT(" Path Id: %u", path_id);
2948 }
2949 p += wpfx;
2950 length -= wpfx;
2951 withdrawn_routes_len -= wpfx;
2952 }
2953 }
2954 } else {
2955 ND_TCHECK_LEN(p, withdrawn_routes_len);
2956 if (length < withdrawn_routes_len)
2957 goto trunc;
2958 p += withdrawn_routes_len;
2959 length -= withdrawn_routes_len;
2960 }
2961
2962 ND_TCHECK_2(p);
2963 if (length < 2)
2964 goto trunc;
2965 len = GET_BE_U_2(p);
2966 p += 2;
2967 length -= 2;
2968
2969 if (withdrawn_routes_len == 0 && len == 0 && length == 0) {
2970 /* No withdrawn routes, no path attributes, no NLRI */
2971 ND_PRINT("\n\t End-of-Rib Marker (empty NLRI)");
2972 return;
2973 }
2974
2975 if (len) {
2976 /* Make sure the path attributes don't go past the end of the packet */
2977 if (length < len)
2978 goto trunc;
2979 /* do something more useful!*/
2980 while (len) {
2981 uint8_t aflags, atype, alenlen;
2982 uint16_t alen;
2983
2984 ND_TCHECK_2(p);
2985 if (length < 2)
2986 goto trunc;
2987 if (len < 2) {
2988 ND_PRINT("\n\t [path attrs too short]");
2989 p += len;
2990 length -= len;
2991 break;
2992 }
2993 aflags = GET_U_1(p);
2994 atype = GET_U_1(p + 1);
2995 p += 2;
2996 len -= 2;
2997 length -= 2;
2998 alenlen = bgp_attr_lenlen(aflags, p);
2999 ND_TCHECK_LEN(p, alenlen);
3000 if (length < alenlen)
3001 goto trunc;
3002 if (len < alenlen) {
3003 ND_PRINT("\n\t [path attrs too short]");
3004 p += len;
3005 length -= len;
3006 break;
3007 }
3008 alen = bgp_attr_len(aflags, p);
3009 p += alenlen;
3010 len -= alenlen;
3011 length -= alenlen;
3012
3013 ND_PRINT("\n\t %s (%u), length: %u",
3014 tok2str(bgp_attr_values, "Unknown Attribute", atype),
3015 atype,
3016 alen);
3017
3018 if (aflags) {
3019 ND_PRINT(", Flags [%s",
3020 bittok2str_nosep(bgp_flags, "", aflags));
3021 if (aflags & 0xf)
3022 ND_PRINT("+%x", aflags & 0xf);
3023 ND_PRINT("]: ");
3024 }
3025 if (len < alen) {
3026 ND_PRINT(" [path attrs too short]");
3027 p += len;
3028 length -= len;
3029 break;
3030 }
3031 if (length < alen)
3032 goto trunc;
3033 if (!bgp_attr_print(ndo, atype, p, alen, 0))
3034 goto trunc;
3035 p += alen;
3036 len -= alen;
3037 length -= alen;
3038 }
3039 }
3040
3041 if (length) {
3042 add_path = check_add_path(ndo, p, length, 32);
3043 ND_PRINT("\n\t Updated routes:");
3044 while (length != 0) {
3045 if (add_path) {
3046 ND_TCHECK_4(p);
3047 if (length < 4)
3048 goto trunc;
3049 path_id = GET_BE_U_4(p);
3050 p += 4;
3051 length -= 4;
3052 }
3053 i = decode_prefix4(ndo, p, length, buf, sizeof(buf));
3054 if (i == -1) {
3055 ND_PRINT("\n\t (illegal prefix length)");
3056 break;
3057 } else if (i == -2)
3058 goto trunc; /* bytes left, but not enough */
3059 else {
3060 ND_PRINT("\n\t %s", buf);
3061 if (add_path) {
3062 ND_PRINT(" Path Id: %u", path_id);
3063 }
3064 p += i;
3065 length -= i;
3066 }
3067 }
3068 }
3069 return;
3070 trunc:
3071 nd_print_trunc(ndo);
3072 }
3073
3074 static void
3075 bgp_notification_print_code(netdissect_options *ndo,
3076 const u_char *dat, u_int length,
3077 uint8_t bgpn_major, uint8_t bgpn_minor)
3078 {
3079 ND_PRINT(", %s (%u)",
3080 tok2str(bgp_notify_major_values, "Unknown Error",
3081 bgpn_major),
3082 bgpn_major);
3083
3084 switch (bgpn_major) {
3085
3086 case BGP_NOTIFY_MAJOR_MSG:
3087 ND_PRINT(", subcode %s (%u)",
3088 tok2str(bgp_notify_minor_msg_values, "Unknown",
3089 bgpn_minor),
3090 bgpn_minor);
3091 break;
3092 case BGP_NOTIFY_MAJOR_OPEN:
3093 ND_PRINT(", subcode %s (%u)",
3094 tok2str(bgp_notify_minor_open_values, "Unknown",
3095 bgpn_minor),
3096 bgpn_minor);
3097 break;
3098 case BGP_NOTIFY_MAJOR_UPDATE:
3099 ND_PRINT(", subcode %s (%u)",
3100 tok2str(bgp_notify_minor_update_values, "Unknown",
3101 bgpn_minor),
3102 bgpn_minor);
3103 break;
3104 case BGP_NOTIFY_MAJOR_FSM:
3105 ND_PRINT(" subcode %s (%u)",
3106 tok2str(bgp_notify_minor_fsm_values, "Unknown",
3107 bgpn_minor),
3108 bgpn_minor);
3109 break;
3110 case BGP_NOTIFY_MAJOR_ROUTEREFRESH:
3111 ND_PRINT(" subcode %s (%u)",
3112 tok2str(bgp_notify_minor_routerefresh_values, "Unknown",
3113 bgpn_minor),
3114 bgpn_minor);
3115 break;
3116 case BGP_NOTIFY_MAJOR_CEASE:
3117 ND_PRINT(", subcode %s (%u)",
3118 tok2str(bgp_notify_minor_cease_values, "Unknown",
3119 bgpn_minor),
3120 bgpn_minor);
3121
3122 /* RFC 4486 mentions optionally 7 bytes
3123 * for the maxprefix subtype, which may contain AFI, SAFI and MAXPREFIXES
3124 */
3125 if(bgpn_minor == BGP_NOTIFY_MINOR_CEASE_MAXPRFX && length >= 7) {
3126 ND_PRINT(", AFI %s (%u), SAFI %s (%u), Max Prefixes: %u",
3127 tok2str(af_values, "Unknown", GET_BE_U_2(dat)),
3128 GET_BE_U_2(dat),
3129 tok2str(bgp_safi_values, "Unknown", GET_U_1((dat + 2))),
3130 GET_U_1((dat + 2)),
3131 GET_BE_U_4(dat + 3));
3132 }
3133 /*
3134 * RFC 9003 describes a method to send a communication
3135 * intended for human consumption regarding the Administrative Shutdown
3136 */
3137 if ((bgpn_minor == BGP_NOTIFY_MINOR_CEASE_SHUT ||
3138 bgpn_minor == BGP_NOTIFY_MINOR_CEASE_RESET) &&
3139 length >= 1) {
3140 uint8_t shutdown_comm_length = GET_U_1(dat);
3141 uint8_t remainder_offset = 0;
3142 /* garbage, hexdump it all */
3143 if (shutdown_comm_length > length - 1) {
3144 ND_PRINT(", invalid Shutdown Communication length");
3145 } else if (shutdown_comm_length == 0) {
3146 ND_PRINT(", empty Shutdown Communication");
3147 remainder_offset += 1;
3148 }
3149 /* a proper shutdown communication */
3150 else {
3151 ND_PRINT(", Shutdown Communication (length: %u): \"", shutdown_comm_length);
3152 nd_printjn(ndo, dat+1, shutdown_comm_length);
3153 ND_PRINT("\"");
3154 remainder_offset += shutdown_comm_length + 1;
3155 }
3156 /* if there is trailing data, hexdump it */
3157 if(length - remainder_offset > 0) {
3158 ND_PRINT(", Data: (length: %u)", length - remainder_offset);
3159 hex_print(ndo, "\n\t\t", dat + remainder_offset, length - remainder_offset);
3160 }
3161 }
3162 /*
3163 * RFC8538 describes the Hard Reset cease subcode, which contains another
3164 * notification code and subcode.
3165 */
3166 if (bgpn_minor == BGP_NOTIFY_MINOR_CEASE_HARDRESET && length >= 2) {
3167 bgpn_major = GET_U_1(dat++);
3168 bgpn_minor = GET_U_1(dat++);
3169 length -= 2;
3170 bgp_notification_print_code(ndo, dat, length, bgpn_major, bgpn_minor);
3171 }
3172 break;
3173 default:
3174 if (bgpn_minor != 0) {
3175 ND_PRINT(", subcode %u", bgpn_minor);
3176 }
3177 break;
3178 }
3179
3180 return;
3181 }
3182
3183 static void
3184 bgp_notification_print(netdissect_options *ndo,
3185 const u_char *dat, u_int length)
3186 {
3187 const struct bgp_notification *bgp_notification_header;
3188 uint8_t bgpn_major, bgpn_minor;
3189
3190 ND_TCHECK_LEN(dat, BGP_NOTIFICATION_SIZE);
3191 if (length<BGP_NOTIFICATION_SIZE)
3192 return;
3193
3194 bgp_notification_header = (const struct bgp_notification *)dat;
3195 bgpn_major = GET_U_1(bgp_notification_header->bgpn_major);
3196 bgpn_minor = GET_U_1(bgp_notification_header->bgpn_minor);
3197 bgp_notification_print_code(ndo, dat + BGP_NOTIFICATION_SIZE,
3198 length - BGP_NOTIFICATION_SIZE, bgpn_major, bgpn_minor);
3199 return;
3200 trunc:
3201 nd_print_trunc(ndo);
3202 }
3203
3204 static void
3205 bgp_route_refresh_print(netdissect_options *ndo,
3206 const u_char *pptr, u_int len)
3207 {
3208 const struct bgp_route_refresh *bgp_route_refresh_header;
3209
3210 /* some little sanity checking */
3211 if (len<BGP_ROUTE_REFRESH_SIZE)
3212 return;
3213
3214 bgp_route_refresh_header = (const struct bgp_route_refresh *)pptr;
3215
3216 ND_PRINT("\n\t AFI %s (%u), SAFI %s (%u), Subtype %s (%u)",
3217 tok2str(af_values, "Unknown",
3218 GET_BE_U_2(bgp_route_refresh_header->afi)),
3219 GET_BE_U_2(bgp_route_refresh_header->afi),
3220 tok2str(bgp_safi_values, "Unknown",
3221 GET_U_1(bgp_route_refresh_header->safi)),
3222 GET_U_1(bgp_route_refresh_header->safi),
3223 tok2str(bgp_route_refresh_subtype_values, "Unknown",
3224 GET_U_1(bgp_route_refresh_header->subtype)),
3225 GET_U_1(bgp_route_refresh_header->subtype));
3226
3227 /* ORF */
3228 if (len >= BGP_ROUTE_REFRESH_SIZE_ORF) {
3229 const struct bgp_route_refresh_orf *orf_header;
3230
3231 orf_header =
3232 (const struct bgp_route_refresh_orf *)(pptr + BGP_ROUTE_REFRESH_SIZE);
3233
3234 ND_PRINT("\n\t ORF refresh %s (%u), ORF type %s (%u), ORF length %u",
3235 tok2str(bgp_orf_refresh_type, "Unknown",
3236 GET_U_1(orf_header->refresh)),
3237 GET_U_1(orf_header->refresh),
3238 tok2str(bgp_orf_type, "Unknown", GET_U_1(orf_header->type)),
3239 GET_U_1(orf_header->type), GET_BE_U_2(orf_header->len));
3240 }
3241
3242 if (ndo->ndo_vflag > 1) {
3243 ND_TCHECK_LEN(pptr, len);
3244 print_unknown_data(ndo, pptr, "\n\t ", len);
3245 }
3246
3247 return;
3248 trunc:
3249 nd_print_trunc(ndo);
3250 }
3251
3252 static int
3253 bgp_pdu_print(netdissect_options *ndo,
3254 const u_char *dat, u_int length)
3255 {
3256 const struct bgp *bgp_header;
3257 uint8_t bgp_type;
3258
3259 ND_TCHECK_LEN(dat, BGP_SIZE);
3260 bgp_header = (const struct bgp *)dat;
3261 bgp_type = GET_U_1(bgp_header->bgp_type);
3262
3263 ND_PRINT("\n\t%s Message (%u), length: %u",
3264 tok2str(bgp_msg_values, "Unknown", bgp_type),
3265 bgp_type,
3266 length);
3267
3268 switch (bgp_type) {
3269 case BGP_OPEN:
3270 bgp_open_print(ndo, dat, length);
3271 break;
3272 case BGP_UPDATE:
3273 bgp_update_print(ndo, dat, length);
3274 break;
3275 case BGP_NOTIFICATION:
3276 bgp_notification_print(ndo, dat, length);
3277 break;
3278 case BGP_KEEPALIVE:
3279 break;
3280 case BGP_ROUTE_REFRESH:
3281 bgp_route_refresh_print(ndo, dat, length);
3282 break;
3283 default:
3284 /* we have no decoder for the BGP message */
3285 ND_TCHECK_LEN(dat, length);
3286 ND_PRINT("\n\t no Message %u decoder", bgp_type);
3287 print_unknown_data(ndo, dat, "\n\t ", length);
3288 break;
3289 }
3290 return 1;
3291 trunc:
3292 nd_print_trunc(ndo);
3293 return 0;
3294 }
3295
3296 void
3297 bgp_print(netdissect_options *ndo,
3298 const u_char *dat, u_int length _U_)
3299 {
3300 const u_char *p;
3301 const u_char *ep = ndo->ndo_snapend;
3302 const u_char *start;
3303 const u_char marker[] = {
3304 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
3305 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
3306 };
3307 const struct bgp *bgp_header;
3308 uint16_t hlen;
3309
3310 ndo->ndo_protocol = "bgp";
3311 ND_PRINT(": BGP");
3312
3313 if (ndo->ndo_vflag < 1) /* lets be less chatty */
3314 return;
3315
3316 p = dat;
3317 start = p;
3318 while (p < ep) {
3319 if (!ND_TTEST_1(p))
3320 break;
3321 if (GET_U_1(p) != 0xff) {
3322 p++;
3323 continue;
3324 }
3325
3326 if (!ND_TTEST_LEN(p, sizeof(marker)))
3327 break;
3328 if (memcmp(p, marker, sizeof(marker)) != 0) {
3329 p++;
3330 continue;
3331 }
3332
3333 /* found BGP header */
3334 ND_TCHECK_LEN(p, BGP_SIZE);
3335 bgp_header = (const struct bgp *)p;
3336
3337 if (start != p)
3338 nd_print_trunc(ndo);
3339
3340 hlen = GET_BE_U_2(bgp_header->bgp_len);
3341 if (hlen < BGP_SIZE) {
3342 ND_PRINT("\nmessage length %u < %u", hlen, BGP_SIZE);
3343 nd_print_invalid(ndo);
3344 break;
3345 }
3346
3347 if (ND_TTEST_LEN(p, hlen)) {
3348 if (!bgp_pdu_print(ndo, p, hlen))
3349 return;
3350 p += hlen;
3351 start = p;
3352 } else {
3353 ND_PRINT("\n[|BGP %s]",
3354 tok2str(bgp_msg_values,
3355 "Unknown Message Type",
3356 GET_U_1(bgp_header->bgp_type)));
3357 break;
3358 }
3359 }
3360
3361 return;
3362
3363 trunc:
3364 nd_print_trunc(ndo);
3365 }