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