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