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