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