]> The Tcpdump Group git mirrors - tcpdump/blob - print-bgp.c
More EXTRACT_8BITS(), more ND_TCHECK_nBITS(), other cleanups.
[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_16BITS(&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_16BITS(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_24BITS(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_8BITS(pptr);
684 addr_length = EXTRACT_8BITS(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_8BITS(pptr);
699 addr_length = EXTRACT_8BITS(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_16BITS(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_16BITS(pptr + 2),
734 EXTRACT_BE_32BITS(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_16BITS(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_32BITS(pptr + 2)),
749 EXTRACT_BE_16BITS(pptr + 6), *(pptr+2), *(pptr+3), *(pptr+4),
750 *(pptr+5), EXTRACT_BE_16BITS(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_32BITS(pptr + 1);
788 as_printf(ndo, asbuf, sizeof(asbuf), EXTRACT_BE_32BITS(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_24BITS(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_8BITS(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_8BITS(pptr);
934 pptr++;
935 route_length = EXTRACT_8BITS(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_32BITS(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_32BITS(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_16BITS(pptr);
1041 plen=EXTRACT_BE_16BITS(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_16BITS(pptr + 8),
1069 EXTRACT_BE_16BITS(pptr + 10),
1070 EXTRACT_BE_24BITS(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_8BITS(pptr);
1081 pptr++;
1082 tlv_len=EXTRACT_BE_16BITS(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",EXTRACT_8BITS(pptr));
1099 pptr++;
1100 UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1101 }
1102 ttlv_len--;
1103 }
1104 break;
1105 default:
1106 if (buflen!=0) {
1107 stringlen=snprintf(buf,buflen, "\n\t\tunknown TLV #%u, length: %u",
1108 tlv_type,
1109 tlv_len);
1110 UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1111 }
1112 break;
1113 }
1114 tlen-=(tlv_len<<3); /* the tlv-length is expressed in bits so lets shift it right */
1115 }
1116 return plen+2;
1117
1118 } else {
1119 /* complain bitterly ? */
1120 /* fall through */
1121 goto trunc;
1122 }
1123
1124 trunc:
1125 return -2;
1126 }
1127
1128 int
1129 decode_prefix6(netdissect_options *ndo,
1130 const u_char *pd, u_int itemlen, char *buf, u_int buflen)
1131 {
1132 struct in6_addr addr;
1133 u_int plen, plenbytes;
1134
1135 ND_TCHECK(pd[0]);
1136 ITEMCHECK(1);
1137 plen = pd[0];
1138 if (128 < plen)
1139 return -1;
1140 itemlen -= 1;
1141
1142 memset(&addr, 0, sizeof(addr));
1143 plenbytes = (plen + 7) / 8;
1144 ND_TCHECK2(pd[1], plenbytes);
1145 ITEMCHECK(plenbytes);
1146 memcpy(&addr, &pd[1], plenbytes);
1147 if (plen % 8) {
1148 addr.s6_addr[plenbytes - 1] &=
1149 ((0xff00 >> (plen % 8)) & 0xff);
1150 }
1151 snprintf(buf, buflen, "%s/%d", ip6addr_string(ndo, &addr), plen);
1152 return 1 + plenbytes;
1153
1154 trunc:
1155 return -2;
1156
1157 badtlv:
1158 return -3;
1159 }
1160
1161 static int
1162 decode_labeled_prefix6(netdissect_options *ndo,
1163 const u_char *pptr, u_int itemlen, char *buf, u_int buflen)
1164 {
1165 struct in6_addr addr;
1166 u_int plen, plenbytes;
1167
1168 /* prefix length and label = 4 bytes */
1169 ND_TCHECK2(pptr[0], 4);
1170 ITEMCHECK(4);
1171 plen = pptr[0]; /* get prefix length */
1172
1173 if (24 > plen)
1174 return -1;
1175
1176 plen-=24; /* adjust prefixlen - labellength */
1177
1178 if (128 < plen)
1179 return -1;
1180 itemlen -= 4;
1181
1182 memset(&addr, 0, sizeof(addr));
1183 plenbytes = (plen + 7) / 8;
1184 ND_TCHECK2(pptr[4], plenbytes);
1185 memcpy(&addr, &pptr[4], plenbytes);
1186 if (plen % 8) {
1187 addr.s6_addr[plenbytes - 1] &=
1188 ((0xff00 >> (plen % 8)) & 0xff);
1189 }
1190 /* the label may get offsetted by 4 bits so lets shift it right */
1191 snprintf(buf, buflen, "%s/%d, label:%u %s",
1192 ip6addr_string(ndo, &addr),
1193 plen,
1194 EXTRACT_BE_24BITS(pptr + 1)>>4,
1195 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1196
1197 return 4 + plenbytes;
1198
1199 trunc:
1200 return -2;
1201
1202 badtlv:
1203 return -3;
1204 }
1205
1206 static int
1207 decode_labeled_vpn_prefix6(netdissect_options *ndo,
1208 const u_char *pptr, char *buf, u_int buflen)
1209 {
1210 struct in6_addr addr;
1211 u_int plen;
1212
1213 ND_TCHECK(pptr[0]);
1214 plen = pptr[0]; /* get prefix length */
1215
1216 if ((24+64) > plen)
1217 return -1;
1218
1219 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
1220
1221 if (128 < plen)
1222 return -1;
1223
1224 memset(&addr, 0, sizeof(addr));
1225 ND_TCHECK2(pptr[12], (plen + 7) / 8);
1226 memcpy(&addr, &pptr[12], (plen + 7) / 8);
1227 if (plen % 8) {
1228 addr.s6_addr[(plen + 7) / 8 - 1] &=
1229 ((0xff00 >> (plen % 8)) & 0xff);
1230 }
1231 /* the label may get offsetted by 4 bits so lets shift it right */
1232 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
1233 bgp_vpn_rd_print(ndo, pptr+4),
1234 ip6addr_string(ndo, &addr),
1235 plen,
1236 EXTRACT_BE_24BITS(pptr + 1)>>4,
1237 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1238
1239 return 12 + (plen + 7) / 8;
1240
1241 trunc:
1242 return -2;
1243 }
1244
1245 static int
1246 decode_clnp_prefix(netdissect_options *ndo,
1247 const u_char *pptr, char *buf, u_int buflen)
1248 {
1249 uint8_t addr[19];
1250 u_int plen;
1251
1252 ND_TCHECK(pptr[0]);
1253 plen = pptr[0]; /* get prefix length */
1254
1255 if (152 < plen)
1256 return -1;
1257
1258 memset(&addr, 0, sizeof(addr));
1259 ND_TCHECK2(pptr[4], (plen + 7) / 8);
1260 memcpy(&addr, &pptr[4], (plen + 7) / 8);
1261 if (plen % 8) {
1262 addr[(plen + 7) / 8 - 1] &=
1263 ((0xff00 >> (plen % 8)) & 0xff);
1264 }
1265 snprintf(buf, buflen, "%s/%d",
1266 isonsap_string(ndo, addr,(plen + 7) / 8),
1267 plen);
1268
1269 return 1 + (plen + 7) / 8;
1270
1271 trunc:
1272 return -2;
1273 }
1274
1275 static int
1276 decode_labeled_vpn_clnp_prefix(netdissect_options *ndo,
1277 const u_char *pptr, char *buf, u_int buflen)
1278 {
1279 uint8_t addr[19];
1280 u_int plen;
1281
1282 ND_TCHECK(pptr[0]);
1283 plen = pptr[0]; /* get prefix length */
1284
1285 if ((24+64) > plen)
1286 return -1;
1287
1288 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
1289
1290 if (152 < plen)
1291 return -1;
1292
1293 memset(&addr, 0, sizeof(addr));
1294 ND_TCHECK2(pptr[12], (plen + 7) / 8);
1295 memcpy(&addr, &pptr[12], (plen + 7) / 8);
1296 if (plen % 8) {
1297 addr[(plen + 7) / 8 - 1] &=
1298 ((0xff00 >> (plen % 8)) & 0xff);
1299 }
1300 /* the label may get offsetted by 4 bits so lets shift it right */
1301 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
1302 bgp_vpn_rd_print(ndo, pptr+4),
1303 isonsap_string(ndo, addr,(plen + 7) / 8),
1304 plen,
1305 EXTRACT_BE_24BITS(pptr + 1)>>4,
1306 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1307
1308 return 12 + (plen + 7) / 8;
1309
1310 trunc:
1311 return -2;
1312 }
1313
1314 /*
1315 * bgp_attr_get_as_size
1316 *
1317 * Try to find the size of the ASs encoded in an as-path. It is not obvious, as
1318 * both Old speakers that do not support 4 byte AS, and the new speakers that do
1319 * support, exchange AS-Path with the same path-attribute type value 0x02.
1320 */
1321 static int
1322 bgp_attr_get_as_size(netdissect_options *ndo,
1323 uint8_t bgpa_type, const u_char *pptr, int len)
1324 {
1325 const u_char *tptr = pptr;
1326
1327 /*
1328 * If the path attribute is the optional AS4 path type, then we already
1329 * know, that ASs must be encoded in 4 byte format.
1330 */
1331 if (bgpa_type == BGPTYPE_AS4_PATH) {
1332 return 4;
1333 }
1334
1335 /*
1336 * Let us assume that ASs are of 2 bytes in size, and check if the AS-Path
1337 * TLV is good. If not, ask the caller to try with AS encoded as 4 bytes
1338 * each.
1339 */
1340 while (tptr < pptr + len) {
1341 ND_TCHECK(tptr[0]);
1342
1343 /*
1344 * If we do not find a valid segment type, our guess might be wrong.
1345 */
1346 if (tptr[0] < BGP_AS_SEG_TYPE_MIN || tptr[0] > BGP_AS_SEG_TYPE_MAX) {
1347 goto trunc;
1348 }
1349 ND_TCHECK(tptr[1]);
1350 tptr += 2 + tptr[1] * 2;
1351 }
1352
1353 /*
1354 * If we correctly reached end of the AS path attribute data content,
1355 * then most likely ASs were indeed encoded as 2 bytes.
1356 */
1357 if (tptr == pptr + len) {
1358 return 2;
1359 }
1360
1361 trunc:
1362
1363 /*
1364 * We can come here, either we did not have enough data, or if we
1365 * try to decode 4 byte ASs in 2 byte format. Either way, return 4,
1366 * so that calller can try to decode each AS as of 4 bytes. If indeed
1367 * there was not enough data, it will crib and end the parse anyways.
1368 */
1369 return 4;
1370 }
1371
1372 static int
1373 bgp_attr_print(netdissect_options *ndo,
1374 u_int atype, const u_char *pptr, u_int len)
1375 {
1376 int i;
1377 uint16_t af;
1378 uint8_t safi, snpa, nhlen;
1379 union { /* copy buffer for bandwidth values */
1380 float f;
1381 uint32_t i;
1382 } bw;
1383 int advance;
1384 u_int tlen;
1385 const u_char *tptr;
1386 char buf[MAXHOSTNAMELEN + 100];
1387 int as_size;
1388
1389 tptr = pptr;
1390 tlen=len;
1391
1392 switch (atype) {
1393 case BGPTYPE_ORIGIN:
1394 if (len != 1)
1395 ND_PRINT((ndo, "invalid len"));
1396 else {
1397 ND_TCHECK(*tptr);
1398 ND_PRINT((ndo, "%s", tok2str(bgp_origin_values,
1399 "Unknown Origin Typecode",
1400 tptr[0])));
1401 }
1402 break;
1403
1404 /*
1405 * Process AS4 byte path and AS2 byte path attributes here.
1406 */
1407 case BGPTYPE_AS4_PATH:
1408 case BGPTYPE_AS_PATH:
1409 if (len % 2) {
1410 ND_PRINT((ndo, "invalid len"));
1411 break;
1412 }
1413 if (!len) {
1414 ND_PRINT((ndo, "empty"));
1415 break;
1416 }
1417
1418 /*
1419 * BGP updates exchanged between New speakers that support 4
1420 * byte AS, ASs are always encoded in 4 bytes. There is no
1421 * definitive way to find this, just by the packet's
1422 * contents. So, check for packet's TLV's sanity assuming
1423 * 2 bytes first, and it does not pass, assume that ASs are
1424 * encoded in 4 bytes format and move on.
1425 */
1426 as_size = bgp_attr_get_as_size(ndo, atype, pptr, len);
1427
1428 while (tptr < pptr + len) {
1429 ND_TCHECK(tptr[0]);
1430 ND_PRINT((ndo, "%s", tok2str(bgp_as_path_segment_open_values,
1431 "?", tptr[0])));
1432 ND_TCHECK(tptr[1]);
1433 for (i = 0; i < tptr[1] * as_size; i += as_size) {
1434 ND_TCHECK2(tptr[2 + i], as_size);
1435 ND_PRINT((ndo, "%s ",
1436 as_printf(ndo, astostr, sizeof(astostr),
1437 as_size == 2 ?
1438 EXTRACT_BE_16BITS(tptr + i + 2) :
1439 EXTRACT_BE_32BITS(tptr + i + 2))));
1440 }
1441 ND_TCHECK(tptr[0]);
1442 ND_PRINT((ndo, "%s", tok2str(bgp_as_path_segment_close_values,
1443 "?", tptr[0])));
1444 ND_TCHECK(tptr[1]);
1445 tptr += 2 + tptr[1] * as_size;
1446 }
1447 break;
1448 case BGPTYPE_NEXT_HOP:
1449 if (len != 4)
1450 ND_PRINT((ndo, "invalid len"));
1451 else {
1452 ND_TCHECK2(tptr[0], 4);
1453 ND_PRINT((ndo, "%s", ipaddr_string(ndo, tptr)));
1454 }
1455 break;
1456 case BGPTYPE_MULTI_EXIT_DISC:
1457 case BGPTYPE_LOCAL_PREF:
1458 if (len != 4)
1459 ND_PRINT((ndo, "invalid len"));
1460 else {
1461 ND_TCHECK_32BITS(tptr);
1462 ND_PRINT((ndo, "%u", EXTRACT_BE_32BITS(tptr)));
1463 }
1464 break;
1465 case BGPTYPE_ATOMIC_AGGREGATE:
1466 if (len != 0)
1467 ND_PRINT((ndo, "invalid len"));
1468 break;
1469 case BGPTYPE_AGGREGATOR:
1470
1471 /*
1472 * Depending on the AS encoded is of 2 bytes or of 4 bytes,
1473 * the length of this PA can be either 6 bytes or 8 bytes.
1474 */
1475 if (len != 6 && len != 8) {
1476 ND_PRINT((ndo, "invalid len"));
1477 break;
1478 }
1479 ND_TCHECK2(tptr[0], len);
1480 if (len == 6) {
1481 ND_PRINT((ndo, " AS #%s, origin %s",
1482 as_printf(ndo, astostr, sizeof(astostr), EXTRACT_BE_16BITS(tptr)),
1483 ipaddr_string(ndo, tptr + 2)));
1484 } else {
1485 ND_PRINT((ndo, " AS #%s, origin %s",
1486 as_printf(ndo, astostr, sizeof(astostr),
1487 EXTRACT_BE_32BITS(tptr)), ipaddr_string(ndo, tptr + 4)));
1488 }
1489 break;
1490 case BGPTYPE_AGGREGATOR4:
1491 if (len != 8) {
1492 ND_PRINT((ndo, "invalid len"));
1493 break;
1494 }
1495 ND_TCHECK2(tptr[0], 8);
1496 ND_PRINT((ndo, " AS #%s, origin %s",
1497 as_printf(ndo, astostr, sizeof(astostr), EXTRACT_BE_32BITS(tptr)),
1498 ipaddr_string(ndo, tptr + 4)));
1499 break;
1500 case BGPTYPE_COMMUNITIES:
1501 if (len % 4) {
1502 ND_PRINT((ndo, "invalid len"));
1503 break;
1504 }
1505 while (tlen>0) {
1506 uint32_t comm;
1507 ND_TCHECK_32BITS(tptr);
1508 comm = EXTRACT_BE_32BITS(tptr);
1509 switch (comm) {
1510 case BGP_COMMUNITY_NO_EXPORT:
1511 ND_PRINT((ndo, " NO_EXPORT"));
1512 break;
1513 case BGP_COMMUNITY_NO_ADVERT:
1514 ND_PRINT((ndo, " NO_ADVERTISE"));
1515 break;
1516 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED:
1517 ND_PRINT((ndo, " NO_EXPORT_SUBCONFED"));
1518 break;
1519 default:
1520 ND_PRINT((ndo, "%u:%u%s",
1521 (comm >> 16) & 0xffff,
1522 comm & 0xffff,
1523 (tlen>4) ? ", " : ""));
1524 break;
1525 }
1526 tlen -=4;
1527 tptr +=4;
1528 }
1529 break;
1530 case BGPTYPE_ORIGINATOR_ID:
1531 if (len != 4) {
1532 ND_PRINT((ndo, "invalid len"));
1533 break;
1534 }
1535 ND_TCHECK2(tptr[0], 4);
1536 ND_PRINT((ndo, "%s",ipaddr_string(ndo, tptr)));
1537 break;
1538 case BGPTYPE_CLUSTER_LIST:
1539 if (len % 4) {
1540 ND_PRINT((ndo, "invalid len"));
1541 break;
1542 }
1543 while (tlen>0) {
1544 ND_TCHECK2(tptr[0], 4);
1545 ND_PRINT((ndo, "%s%s",
1546 ipaddr_string(ndo, tptr),
1547 (tlen>4) ? ", " : ""));
1548 tlen -=4;
1549 tptr +=4;
1550 }
1551 break;
1552 case BGPTYPE_MP_REACH_NLRI:
1553 ND_TCHECK2(tptr[0], 3);
1554 af = EXTRACT_BE_16BITS(tptr);
1555 safi = EXTRACT_8BITS(tptr + 2);
1556
1557 ND_PRINT((ndo, "\n\t AFI: %s (%u), %sSAFI: %s (%u)",
1558 tok2str(af_values, "Unknown AFI", af),
1559 af,
1560 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1561 tok2str(bgp_safi_values, "Unknown SAFI", safi),
1562 safi));
1563
1564 switch(af<<8 | safi) {
1565 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1566 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1567 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1568 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1569 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
1570 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1571 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1572 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1573 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN):
1574 case (AFNUM_INET<<8 | SAFNUM_MDT):
1575 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1576 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1577 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1578 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1579 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1580 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1581 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1582 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1583 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1584 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1585 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1586 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1587 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1588 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1589 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1590 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1591 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1592 break;
1593 default:
1594 ND_TCHECK2(tptr[0], tlen);
1595 ND_PRINT((ndo, "\n\t no AFI %u / SAFI %u decoder", af, safi));
1596 if (ndo->ndo_vflag <= 1)
1597 print_unknown_data(ndo, tptr, "\n\t ", tlen);
1598 goto done;
1599 break;
1600 }
1601
1602 tptr +=3;
1603
1604 ND_TCHECK(tptr[0]);
1605 nhlen = tptr[0];
1606 tlen = nhlen;
1607 tptr++;
1608
1609 if (tlen) {
1610 int nnh = 0;
1611 ND_PRINT((ndo, "\n\t nexthop: "));
1612 while (tlen > 0) {
1613 if ( nnh++ > 0 ) {
1614 ND_PRINT((ndo, ", " ));
1615 }
1616 switch(af<<8 | safi) {
1617 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1618 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1619 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1620 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1621 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
1622 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN):
1623 case (AFNUM_INET<<8 | SAFNUM_MDT):
1624 if (tlen < (int)sizeof(struct in_addr)) {
1625 ND_PRINT((ndo, "invalid len"));
1626 tlen = 0;
1627 } else {
1628 ND_TCHECK2(tptr[0], sizeof(struct in_addr));
1629 ND_PRINT((ndo, "%s",ipaddr_string(ndo, tptr)));
1630 tlen -= sizeof(struct in_addr);
1631 tptr += sizeof(struct in_addr);
1632 }
1633 break;
1634 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1635 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1636 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1637 if (tlen < (int)(sizeof(struct in_addr)+BGP_VPN_RD_LEN)) {
1638 ND_PRINT((ndo, "invalid len"));
1639 tlen = 0;
1640 } else {
1641 ND_TCHECK2(tptr[0], sizeof(struct in_addr)+BGP_VPN_RD_LEN);
1642 ND_PRINT((ndo, "RD: %s, %s",
1643 bgp_vpn_rd_print(ndo, tptr),
1644 ipaddr_string(ndo, tptr+BGP_VPN_RD_LEN)));
1645 tlen -= (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
1646 tptr += (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
1647 }
1648 break;
1649 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1650 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1651 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1652 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1653 if (tlen < (int)sizeof(struct in6_addr)) {
1654 ND_PRINT((ndo, "invalid len"));
1655 tlen = 0;
1656 } else {
1657 ND_TCHECK2(tptr[0], sizeof(struct in6_addr));
1658 ND_PRINT((ndo, "%s", ip6addr_string(ndo, tptr)));
1659 tlen -= sizeof(struct in6_addr);
1660 tptr += sizeof(struct in6_addr);
1661 }
1662 break;
1663 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1664 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1665 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1666 if (tlen < (int)(sizeof(struct in6_addr)+BGP_VPN_RD_LEN)) {
1667 ND_PRINT((ndo, "invalid len"));
1668 tlen = 0;
1669 } else {
1670 ND_TCHECK2(tptr[0], sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
1671 ND_PRINT((ndo, "RD: %s, %s",
1672 bgp_vpn_rd_print(ndo, tptr),
1673 ip6addr_string(ndo, tptr+BGP_VPN_RD_LEN)));
1674 tlen -= (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
1675 tptr += (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
1676 }
1677 break;
1678 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1679 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1680 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1681 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1682 if (tlen < (int)sizeof(struct in_addr)) {
1683 ND_PRINT((ndo, "invalid len"));
1684 tlen = 0;
1685 } else {
1686 ND_TCHECK2(tptr[0], sizeof(struct in_addr));
1687 ND_PRINT((ndo, "%s", ipaddr_string(ndo, tptr)));
1688 tlen -= (sizeof(struct in_addr));
1689 tptr += (sizeof(struct in_addr));
1690 }
1691 break;
1692 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1693 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1694 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1695 ND_TCHECK2(tptr[0], tlen);
1696 ND_PRINT((ndo, "%s", isonsap_string(ndo, tptr, tlen)));
1697 tptr += tlen;
1698 tlen = 0;
1699 break;
1700
1701 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1702 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1703 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1704 if (tlen < BGP_VPN_RD_LEN+1) {
1705 ND_PRINT((ndo, "invalid len"));
1706 tlen = 0;
1707 } else {
1708 ND_TCHECK2(tptr[0], tlen);
1709 ND_PRINT((ndo, "RD: %s, %s",
1710 bgp_vpn_rd_print(ndo, tptr),
1711 isonsap_string(ndo, tptr+BGP_VPN_RD_LEN,tlen-BGP_VPN_RD_LEN)));
1712 /* rfc986 mapped IPv4 address ? */
1713 if (EXTRACT_BE_32BITS(tptr + BGP_VPN_RD_LEN) == 0x47000601)
1714 ND_PRINT((ndo, " = %s", ipaddr_string(ndo, tptr+BGP_VPN_RD_LEN+4)));
1715 /* rfc1888 mapped IPv6 address ? */
1716 else if (EXTRACT_BE_24BITS(tptr + BGP_VPN_RD_LEN) == 0x350000)
1717 ND_PRINT((ndo, " = %s", ip6addr_string(ndo, tptr+BGP_VPN_RD_LEN+3)));
1718 tptr += tlen;
1719 tlen = 0;
1720 }
1721 break;
1722 default:
1723 ND_TCHECK2(tptr[0], tlen);
1724 ND_PRINT((ndo, "no AFI %u/SAFI %u decoder", af, safi));
1725 if (ndo->ndo_vflag <= 1)
1726 print_unknown_data(ndo, tptr, "\n\t ", tlen);
1727 tptr += tlen;
1728 tlen = 0;
1729 goto done;
1730 break;
1731 }
1732 }
1733 }
1734 ND_PRINT((ndo, ", nh-length: %u", nhlen));
1735 tptr += tlen;
1736
1737 ND_TCHECK(tptr[0]);
1738 snpa = tptr[0];
1739 tptr++;
1740
1741 if (snpa) {
1742 ND_PRINT((ndo, "\n\t %u SNPA", snpa));
1743 for (/*nothing*/; snpa > 0; snpa--) {
1744 ND_TCHECK(tptr[0]);
1745 ND_PRINT((ndo, "\n\t %d bytes", tptr[0]));
1746 tptr += tptr[0] + 1;
1747 }
1748 } else {
1749 ND_PRINT((ndo, ", no SNPA"));
1750 }
1751
1752 while (tptr < pptr + len) {
1753 switch (af<<8 | safi) {
1754 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1755 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1756 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1757 advance = decode_prefix4(ndo, tptr, len, buf, sizeof(buf));
1758 if (advance == -1)
1759 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1760 else if (advance == -2)
1761 goto trunc;
1762 else if (advance == -3)
1763 break; /* bytes left, but not enough */
1764 else
1765 ND_PRINT((ndo, "\n\t %s", buf));
1766 break;
1767 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1768 advance = decode_labeled_prefix4(ndo, tptr, len, buf, sizeof(buf));
1769 if (advance == -1)
1770 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1771 else if (advance == -2)
1772 goto trunc;
1773 else if (advance == -3)
1774 break; /* bytes left, but not enough */
1775 else
1776 ND_PRINT((ndo, "\n\t %s", buf));
1777 break;
1778 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1779 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1780 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1781 advance = decode_labeled_vpn_prefix4(ndo, tptr, buf, sizeof(buf));
1782 if (advance == -1)
1783 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1784 else if (advance == -2)
1785 goto trunc;
1786 else
1787 ND_PRINT((ndo, "\n\t %s", buf));
1788 break;
1789 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
1790 advance = decode_rt_routing_info(ndo, tptr, buf, sizeof(buf));
1791 if (advance == -1)
1792 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1793 else if (advance == -2)
1794 goto trunc;
1795 else
1796 ND_PRINT((ndo, "\n\t %s", buf));
1797 break;
1798 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): /* fall through */
1799 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST_VPN):
1800 advance = decode_multicast_vpn(ndo, tptr, buf, sizeof(buf));
1801 if (advance == -1)
1802 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1803 else if (advance == -2)
1804 goto trunc;
1805 else
1806 ND_PRINT((ndo, "\n\t %s", buf));
1807 break;
1808
1809 case (AFNUM_INET<<8 | SAFNUM_MDT):
1810 advance = decode_mdt_vpn_nlri(ndo, tptr, buf, sizeof(buf));
1811 if (advance == -1)
1812 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1813 else if (advance == -2)
1814 goto trunc;
1815 else
1816 ND_PRINT((ndo, "\n\t %s", buf));
1817 break;
1818 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1819 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1820 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1821 advance = decode_prefix6(ndo, tptr, len, buf, sizeof(buf));
1822 if (advance == -1)
1823 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1824 else if (advance == -2)
1825 goto trunc;
1826 else if (advance == -3)
1827 break; /* bytes left, but not enough */
1828 else
1829 ND_PRINT((ndo, "\n\t %s", buf));
1830 break;
1831 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1832 advance = decode_labeled_prefix6(ndo, tptr, len, buf, sizeof(buf));
1833 if (advance == -1)
1834 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1835 else if (advance == -2)
1836 goto trunc;
1837 else if (advance == -3)
1838 break; /* bytes left, but not enough */
1839 else
1840 ND_PRINT((ndo, "\n\t %s", buf));
1841 break;
1842 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1843 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1844 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1845 advance = decode_labeled_vpn_prefix6(ndo, tptr, buf, sizeof(buf));
1846 if (advance == -1)
1847 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1848 else if (advance == -2)
1849 goto trunc;
1850 else
1851 ND_PRINT((ndo, "\n\t %s", buf));
1852 break;
1853 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1854 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1855 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1856 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1857 advance = decode_labeled_vpn_l2(ndo, tptr, buf, sizeof(buf));
1858 if (advance == -1)
1859 ND_PRINT((ndo, "\n\t (illegal length)"));
1860 else if (advance == -2)
1861 goto trunc;
1862 else
1863 ND_PRINT((ndo, "\n\t %s", buf));
1864 break;
1865 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1866 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1867 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1868 advance = decode_clnp_prefix(ndo, tptr, buf, sizeof(buf));
1869 if (advance == -1)
1870 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1871 else if (advance == -2)
1872 goto trunc;
1873 else
1874 ND_PRINT((ndo, "\n\t %s", buf));
1875 break;
1876 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1877 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1878 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1879 advance = decode_labeled_vpn_clnp_prefix(ndo, tptr, buf, sizeof(buf));
1880 if (advance == -1)
1881 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1882 else if (advance == -2)
1883 goto trunc;
1884 else
1885 ND_PRINT((ndo, "\n\t %s", buf));
1886 break;
1887 default:
1888 ND_TCHECK2(*tptr,tlen);
1889 ND_PRINT((ndo, "\n\t no AFI %u / SAFI %u decoder", af, safi));
1890 if (ndo->ndo_vflag <= 1)
1891 print_unknown_data(ndo, tptr, "\n\t ", tlen);
1892 advance = 0;
1893 tptr = pptr + len;
1894 break;
1895 }
1896 if (advance < 0)
1897 break;
1898 tptr += advance;
1899 }
1900 done:
1901 break;
1902
1903 case BGPTYPE_MP_UNREACH_NLRI:
1904 ND_TCHECK2(tptr[0], BGP_MP_NLRI_MINSIZE);
1905 af = EXTRACT_BE_16BITS(tptr);
1906 safi = tptr[2];
1907
1908 ND_PRINT((ndo, "\n\t AFI: %s (%u), %sSAFI: %s (%u)",
1909 tok2str(af_values, "Unknown AFI", af),
1910 af,
1911 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1912 tok2str(bgp_safi_values, "Unknown SAFI", safi),
1913 safi));
1914
1915 if (len == BGP_MP_NLRI_MINSIZE)
1916 ND_PRINT((ndo, "\n\t End-of-Rib Marker (empty NLRI)"));
1917
1918 tptr += 3;
1919
1920 while (tptr < pptr + len) {
1921 switch (af<<8 | safi) {
1922 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1923 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1924 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1925 advance = decode_prefix4(ndo, tptr, len, buf, sizeof(buf));
1926 if (advance == -1)
1927 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1928 else if (advance == -2)
1929 goto trunc;
1930 else if (advance == -3)
1931 break; /* bytes left, but not enough */
1932 else
1933 ND_PRINT((ndo, "\n\t %s", buf));
1934 break;
1935 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1936 advance = decode_labeled_prefix4(ndo, tptr, len, buf, sizeof(buf));
1937 if (advance == -1)
1938 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1939 else if (advance == -2)
1940 goto trunc;
1941 else if (advance == -3)
1942 break; /* bytes left, but not enough */
1943 else
1944 ND_PRINT((ndo, "\n\t %s", buf));
1945 break;
1946 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1947 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1948 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1949 advance = decode_labeled_vpn_prefix4(ndo, tptr, buf, sizeof(buf));
1950 if (advance == -1)
1951 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1952 else if (advance == -2)
1953 goto trunc;
1954 else
1955 ND_PRINT((ndo, "\n\t %s", buf));
1956 break;
1957 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1958 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1959 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1960 advance = decode_prefix6(ndo, tptr, len, buf, sizeof(buf));
1961 if (advance == -1)
1962 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1963 else if (advance == -2)
1964 goto trunc;
1965 else if (advance == -3)
1966 break; /* bytes left, but not enough */
1967 else
1968 ND_PRINT((ndo, "\n\t %s", buf));
1969 break;
1970 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1971 advance = decode_labeled_prefix6(ndo, tptr, len, buf, sizeof(buf));
1972 if (advance == -1)
1973 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1974 else if (advance == -2)
1975 goto trunc;
1976 else if (advance == -3)
1977 break; /* bytes left, but not enough */
1978 else
1979 ND_PRINT((ndo, "\n\t %s", buf));
1980 break;
1981 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1982 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1983 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1984 advance = decode_labeled_vpn_prefix6(ndo, tptr, buf, sizeof(buf));
1985 if (advance == -1)
1986 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1987 else if (advance == -2)
1988 goto trunc;
1989 else
1990 ND_PRINT((ndo, "\n\t %s", buf));
1991 break;
1992 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1993 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1994 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1995 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1996 advance = decode_labeled_vpn_l2(ndo, tptr, buf, sizeof(buf));
1997 if (advance == -1)
1998 ND_PRINT((ndo, "\n\t (illegal length)"));
1999 else if (advance == -2)
2000 goto trunc;
2001 else
2002 ND_PRINT((ndo, "\n\t %s", buf));
2003 break;
2004 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
2005 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
2006 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
2007 advance = decode_clnp_prefix(ndo, tptr, buf, sizeof(buf));
2008 if (advance == -1)
2009 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2010 else if (advance == -2)
2011 goto trunc;
2012 else
2013 ND_PRINT((ndo, "\n\t %s", buf));
2014 break;
2015 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
2016 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
2017 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
2018 advance = decode_labeled_vpn_clnp_prefix(ndo, tptr, buf, sizeof(buf));
2019 if (advance == -1)
2020 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2021 else if (advance == -2)
2022 goto trunc;
2023 else
2024 ND_PRINT((ndo, "\n\t %s", buf));
2025 break;
2026 case (AFNUM_INET<<8 | SAFNUM_MDT):
2027 advance = decode_mdt_vpn_nlri(ndo, tptr, buf, sizeof(buf));
2028 if (advance == -1)
2029 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2030 else if (advance == -2)
2031 goto trunc;
2032 else
2033 ND_PRINT((ndo, "\n\t %s", buf));
2034 break;
2035 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): /* fall through */
2036 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST_VPN):
2037 advance = decode_multicast_vpn(ndo, tptr, buf, sizeof(buf));
2038 if (advance == -1)
2039 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2040 else if (advance == -2)
2041 goto trunc;
2042 else
2043 ND_PRINT((ndo, "\n\t %s", buf));
2044 break;
2045 default:
2046 ND_TCHECK2(*(tptr-3),tlen);
2047 ND_PRINT((ndo, "no AFI %u / SAFI %u decoder", af, safi));
2048 if (ndo->ndo_vflag <= 1)
2049 print_unknown_data(ndo, tptr-3, "\n\t ", tlen);
2050 advance = 0;
2051 tptr = pptr + len;
2052 break;
2053 }
2054 if (advance < 0)
2055 break;
2056 tptr += advance;
2057 }
2058 break;
2059 case BGPTYPE_EXTD_COMMUNITIES:
2060 if (len % 8) {
2061 ND_PRINT((ndo, "invalid len"));
2062 break;
2063 }
2064 while (tlen>0) {
2065 uint16_t extd_comm;
2066
2067 ND_TCHECK_16BITS(tptr);
2068 extd_comm=EXTRACT_BE_16BITS(tptr);
2069
2070 ND_PRINT((ndo, "\n\t %s (0x%04x), Flags [%s]",
2071 tok2str(bgp_extd_comm_subtype_values,
2072 "unknown extd community typecode",
2073 extd_comm),
2074 extd_comm,
2075 bittok2str(bgp_extd_comm_flag_values, "none", extd_comm)));
2076
2077 ND_TCHECK2(*(tptr+2), 6);
2078 switch(extd_comm) {
2079 case BGP_EXT_COM_RT_0:
2080 case BGP_EXT_COM_RO_0:
2081 case BGP_EXT_COM_L2VPN_RT_0:
2082 ND_PRINT((ndo, ": %u:%u (= %s)",
2083 EXTRACT_BE_16BITS(tptr + 2),
2084 EXTRACT_BE_32BITS(tptr + 4),
2085 ipaddr_string(ndo, tptr+4)));
2086 break;
2087 case BGP_EXT_COM_RT_1:
2088 case BGP_EXT_COM_RO_1:
2089 case BGP_EXT_COM_L2VPN_RT_1:
2090 case BGP_EXT_COM_VRF_RT_IMP:
2091 ND_PRINT((ndo, ": %s:%u",
2092 ipaddr_string(ndo, tptr+2),
2093 EXTRACT_BE_16BITS(tptr + 6)));
2094 break;
2095 case BGP_EXT_COM_RT_2:
2096 case BGP_EXT_COM_RO_2:
2097 ND_PRINT((ndo, ": %s:%u",
2098 as_printf(ndo, astostr, sizeof(astostr),
2099 EXTRACT_BE_32BITS(tptr + 2)), EXTRACT_BE_16BITS(tptr + 6)));
2100 break;
2101 case BGP_EXT_COM_LINKBAND:
2102 bw.i = EXTRACT_BE_32BITS(tptr + 2);
2103 ND_PRINT((ndo, ": bandwidth: %.3f Mbps",
2104 bw.f*8/1000000));
2105 break;
2106 case BGP_EXT_COM_VPN_ORIGIN:
2107 case BGP_EXT_COM_VPN_ORIGIN2:
2108 case BGP_EXT_COM_VPN_ORIGIN3:
2109 case BGP_EXT_COM_VPN_ORIGIN4:
2110 case BGP_EXT_COM_OSPF_RID:
2111 case BGP_EXT_COM_OSPF_RID2:
2112 ND_PRINT((ndo, "%s", ipaddr_string(ndo, tptr+2)));
2113 break;
2114 case BGP_EXT_COM_OSPF_RTYPE:
2115 case BGP_EXT_COM_OSPF_RTYPE2:
2116 ND_PRINT((ndo, ": area:%s, router-type:%s, metric-type:%s%s",
2117 ipaddr_string(ndo, tptr+2),
2118 tok2str(bgp_extd_comm_ospf_rtype_values,
2119 "unknown (0x%02x)",
2120 EXTRACT_8BITS((tptr + 6))),
2121 (*(tptr+7) & BGP_OSPF_RTYPE_METRIC_TYPE) ? "E2" : "",
2122 ((*(tptr+6) == BGP_OSPF_RTYPE_EXT) || (*(tptr+6) == BGP_OSPF_RTYPE_NSSA)) ? "E1" : ""));
2123 break;
2124 case BGP_EXT_COM_L2INFO:
2125 ND_PRINT((ndo, ": %s Control Flags [0x%02x]:MTU %u",
2126 tok2str(l2vpn_encaps_values,
2127 "unknown encaps",
2128 EXTRACT_8BITS((tptr + 2))),
2129 EXTRACT_8BITS((tptr + 3)),
2130 EXTRACT_BE_16BITS(tptr + 4)));
2131 break;
2132 case BGP_EXT_COM_SOURCE_AS:
2133 ND_PRINT((ndo, ": AS %u", EXTRACT_BE_16BITS(tptr + 2)));
2134 break;
2135 default:
2136 ND_TCHECK2(*tptr,8);
2137 print_unknown_data(ndo, tptr, "\n\t ", 8);
2138 break;
2139 }
2140 tlen -=8;
2141 tptr +=8;
2142 }
2143 break;
2144
2145 case BGPTYPE_PMSI_TUNNEL:
2146 {
2147 uint8_t tunnel_type, flags;
2148
2149 ND_TCHECK2(tptr[0], 5);
2150 flags = EXTRACT_8BITS(tptr);
2151 tunnel_type = EXTRACT_8BITS(tptr+1);
2152 tlen = len;
2153
2154 ND_PRINT((ndo, "\n\t Tunnel-type %s (%u), Flags [%s], MPLS Label %u",
2155 tok2str(bgp_pmsi_tunnel_values, "Unknown", tunnel_type),
2156 tunnel_type,
2157 bittok2str(bgp_pmsi_flag_values, "none", flags),
2158 EXTRACT_BE_24BITS(tptr + 2)>>4));
2159
2160 tptr +=5;
2161 tlen -= 5;
2162
2163 switch (tunnel_type) {
2164 case BGP_PMSI_TUNNEL_PIM_SM: /* fall through */
2165 case BGP_PMSI_TUNNEL_PIM_BIDIR:
2166 ND_TCHECK2(tptr[0], 8);
2167 ND_PRINT((ndo, "\n\t Sender %s, P-Group %s",
2168 ipaddr_string(ndo, tptr),
2169 ipaddr_string(ndo, tptr+4)));
2170 break;
2171
2172 case BGP_PMSI_TUNNEL_PIM_SSM:
2173 ND_TCHECK2(tptr[0], 8);
2174 ND_PRINT((ndo, "\n\t Root-Node %s, P-Group %s",
2175 ipaddr_string(ndo, tptr),
2176 ipaddr_string(ndo, tptr+4)));
2177 break;
2178 case BGP_PMSI_TUNNEL_INGRESS:
2179 ND_TCHECK2(tptr[0], 4);
2180 ND_PRINT((ndo, "\n\t Tunnel-Endpoint %s",
2181 ipaddr_string(ndo, tptr)));
2182 break;
2183 case BGP_PMSI_TUNNEL_LDP_P2MP: /* fall through */
2184 case BGP_PMSI_TUNNEL_LDP_MP2MP:
2185 ND_TCHECK2(tptr[0], 8);
2186 ND_PRINT((ndo, "\n\t Root-Node %s, LSP-ID 0x%08x",
2187 ipaddr_string(ndo, tptr),
2188 EXTRACT_BE_32BITS(tptr + 4)));
2189 break;
2190 case BGP_PMSI_TUNNEL_RSVP_P2MP:
2191 ND_TCHECK2(tptr[0], 8);
2192 ND_PRINT((ndo, "\n\t Extended-Tunnel-ID %s, P2MP-ID 0x%08x",
2193 ipaddr_string(ndo, tptr),
2194 EXTRACT_BE_32BITS(tptr + 4)));
2195 break;
2196 default:
2197 if (ndo->ndo_vflag <= 1) {
2198 print_unknown_data(ndo, tptr, "\n\t ", tlen);
2199 }
2200 }
2201 break;
2202 }
2203 case BGPTYPE_AIGP:
2204 {
2205 uint8_t type;
2206 uint16_t length;
2207
2208 tlen = len;
2209
2210 while (tlen >= 3) {
2211
2212 ND_TCHECK2(tptr[0], 3);
2213
2214 type = EXTRACT_8BITS(tptr);
2215 length = EXTRACT_BE_16BITS(tptr + 1);
2216 tptr += 3;
2217 tlen -= 3;
2218
2219 ND_PRINT((ndo, "\n\t %s TLV (%u), length %u",
2220 tok2str(bgp_aigp_values, "Unknown", type),
2221 type, length));
2222
2223 if (length < 3)
2224 goto trunc;
2225 length -= 3;
2226
2227 /*
2228 * Check if we can read the TLV data.
2229 */
2230 ND_TCHECK2(tptr[3], length);
2231
2232 switch (type) {
2233
2234 case BGP_AIGP_TLV:
2235 if (length < 8)
2236 goto trunc;
2237 ND_PRINT((ndo, ", metric %" PRIu64,
2238 EXTRACT_BE_64BITS(tptr)));
2239 break;
2240
2241 default:
2242 if (ndo->ndo_vflag <= 1) {
2243 print_unknown_data(ndo, tptr,"\n\t ", length);
2244 }
2245 }
2246
2247 tptr += length;
2248 tlen -= length;
2249 }
2250 break;
2251 }
2252 case BGPTYPE_ATTR_SET:
2253 ND_TCHECK_32BITS(tptr);
2254 if (len < 4)
2255 goto trunc;
2256 ND_PRINT((ndo, "\n\t Origin AS: %s",
2257 as_printf(ndo, astostr, sizeof(astostr), EXTRACT_BE_32BITS(tptr))));
2258 tptr+=4;
2259 len -=4;
2260
2261 while (len) {
2262 u_int aflags, alenlen, alen;
2263
2264 ND_TCHECK2(tptr[0], 2);
2265 if (len < 2)
2266 goto trunc;
2267 aflags = EXTRACT_8BITS(tptr);
2268 atype = EXTRACT_8BITS(tptr + 1);
2269 tptr += 2;
2270 len -= 2;
2271 alenlen = bgp_attr_lenlen(aflags, tptr);
2272 ND_TCHECK2(tptr[0], alenlen);
2273 if (len < alenlen)
2274 goto trunc;
2275 alen = bgp_attr_len(aflags, tptr);
2276 tptr += alenlen;
2277 len -= alenlen;
2278
2279 ND_PRINT((ndo, "\n\t %s (%u), length: %u",
2280 tok2str(bgp_attr_values,
2281 "Unknown Attribute", atype),
2282 atype,
2283 alen));
2284
2285 if (aflags) {
2286 ND_PRINT((ndo, ", Flags [%s%s%s%s",
2287 aflags & 0x80 ? "O" : "",
2288 aflags & 0x40 ? "T" : "",
2289 aflags & 0x20 ? "P" : "",
2290 aflags & 0x10 ? "E" : ""));
2291 if (aflags & 0xf)
2292 ND_PRINT((ndo, "+%x", aflags & 0xf));
2293 ND_PRINT((ndo, "]: "));
2294 }
2295 /* FIXME check for recursion */
2296 if (!bgp_attr_print(ndo, atype, tptr, alen))
2297 return 0;
2298 tptr += alen;
2299 len -= alen;
2300 }
2301 break;
2302
2303 case BGPTYPE_LARGE_COMMUNITY:
2304 if (len == 0 || len % 12) {
2305 ND_PRINT((ndo, "invalid len"));
2306 break;
2307 }
2308 ND_PRINT((ndo, "\n\t "));
2309 while (len > 0) {
2310 ND_TCHECK2(*tptr, 12);
2311 ND_PRINT((ndo, "%u:%u:%u%s",
2312 EXTRACT_BE_32BITS(tptr),
2313 EXTRACT_BE_32BITS(tptr + 4),
2314 EXTRACT_BE_32BITS(tptr + 8),
2315 (len > 12) ? ", " : ""));
2316 tptr += 12;
2317 len -= 12;
2318 }
2319 break;
2320 default:
2321 ND_TCHECK2(*pptr,len);
2322 ND_PRINT((ndo, "\n\t no Attribute %u decoder", atype)); /* we have no decoder for the attribute */
2323 if (ndo->ndo_vflag <= 1)
2324 print_unknown_data(ndo, pptr, "\n\t ", len);
2325 break;
2326 }
2327 if (ndo->ndo_vflag > 1 && len) { /* omit zero length attributes*/
2328 ND_TCHECK2(*pptr,len);
2329 print_unknown_data(ndo, pptr, "\n\t ", len);
2330 }
2331 return 1;
2332
2333 trunc:
2334 return 0;
2335 }
2336
2337 static void
2338 bgp_capabilities_print(netdissect_options *ndo,
2339 const u_char *opt, int caps_len)
2340 {
2341 int cap_type, cap_len, tcap_len, cap_offset;
2342 int i = 0;
2343
2344 while (i < caps_len) {
2345 ND_TCHECK2(opt[i], BGP_CAP_HEADER_SIZE);
2346 cap_type=opt[i];
2347 cap_len=opt[i+1];
2348 tcap_len=cap_len;
2349 ND_PRINT((ndo, "\n\t %s (%u), length: %u",
2350 tok2str(bgp_capcode_values, "Unknown",
2351 cap_type),
2352 cap_type,
2353 cap_len));
2354 ND_TCHECK2(opt[i+2], cap_len);
2355 switch (cap_type) {
2356 case BGP_CAPCODE_MP:
2357 ND_PRINT((ndo, "\n\t\tAFI %s (%u), SAFI %s (%u)",
2358 tok2str(af_values, "Unknown",
2359 EXTRACT_BE_16BITS(opt + i + 2)),
2360 EXTRACT_BE_16BITS(opt + i + 2),
2361 tok2str(bgp_safi_values, "Unknown",
2362 opt[i+5]),
2363 opt[i+5]));
2364 break;
2365 case BGP_CAPCODE_RESTART:
2366 ND_PRINT((ndo, "\n\t\tRestart Flags: [%s], Restart Time %us",
2367 ((opt[i+2])&0x80) ? "R" : "none",
2368 EXTRACT_BE_16BITS(opt + i + 2)&0xfff));
2369 tcap_len-=2;
2370 cap_offset=4;
2371 while(tcap_len>=4) {
2372 ND_PRINT((ndo, "\n\t\t AFI %s (%u), SAFI %s (%u), Forwarding state preserved: %s",
2373 tok2str(af_values,"Unknown",
2374 EXTRACT_BE_16BITS(opt + i + cap_offset)),
2375 EXTRACT_BE_16BITS(opt + i + cap_offset),
2376 tok2str(bgp_safi_values,"Unknown",
2377 opt[i+cap_offset+2]),
2378 opt[i+cap_offset+2],
2379 ((opt[i+cap_offset+3])&0x80) ? "yes" : "no" ));
2380 tcap_len-=4;
2381 cap_offset+=4;
2382 }
2383 break;
2384 case BGP_CAPCODE_RR:
2385 case BGP_CAPCODE_RR_CISCO:
2386 break;
2387 case BGP_CAPCODE_AS_NEW:
2388
2389 /*
2390 * Extract the 4 byte AS number encoded.
2391 */
2392 if (cap_len == 4) {
2393 ND_PRINT((ndo, "\n\t\t 4 Byte AS %s",
2394 as_printf(ndo, astostr, sizeof(astostr),
2395 EXTRACT_BE_32BITS(opt + i + 2))));
2396 }
2397 break;
2398 case BGP_CAPCODE_ADD_PATH:
2399 cap_offset=2;
2400 if (tcap_len == 0) {
2401 ND_PRINT((ndo, " (bogus)")); /* length */
2402 break;
2403 }
2404 while (tcap_len > 0) {
2405 if (tcap_len < 4) {
2406 ND_PRINT((ndo, "\n\t\t(invalid)"));
2407 break;
2408 }
2409 ND_PRINT((ndo, "\n\t\tAFI %s (%u), SAFI %s (%u), Send/Receive: %s",
2410 tok2str(af_values,"Unknown",EXTRACT_BE_16BITS(opt + i + cap_offset)),
2411 EXTRACT_BE_16BITS(opt + i + cap_offset),
2412 tok2str(bgp_safi_values,"Unknown",opt[i+cap_offset+2]),
2413 opt[i+cap_offset+2],
2414 tok2str(bgp_add_path_recvsend,"Bogus (0x%02x)",opt[i+cap_offset+3])
2415 ));
2416 tcap_len-=4;
2417 cap_offset+=4;
2418 }
2419 break;
2420 default:
2421 ND_PRINT((ndo, "\n\t\tno decoder for Capability %u",
2422 cap_type));
2423 if (ndo->ndo_vflag <= 1)
2424 print_unknown_data(ndo, &opt[i+2], "\n\t\t", cap_len);
2425 break;
2426 }
2427 if (ndo->ndo_vflag > 1 && cap_len > 0) {
2428 print_unknown_data(ndo, &opt[i+2], "\n\t\t", cap_len);
2429 }
2430 i += BGP_CAP_HEADER_SIZE + cap_len;
2431 }
2432 return;
2433
2434 trunc:
2435 ND_PRINT((ndo, "[|BGP]"));
2436 }
2437
2438 static void
2439 bgp_open_print(netdissect_options *ndo,
2440 const u_char *dat, int length)
2441 {
2442 struct bgp_open bgpo;
2443 struct bgp_opt bgpopt;
2444 const u_char *opt;
2445 int i;
2446
2447 ND_TCHECK2(dat[0], BGP_OPEN_SIZE);
2448 memcpy(&bgpo, dat, BGP_OPEN_SIZE);
2449
2450 ND_PRINT((ndo, "\n\t Version %d, ", bgpo.bgpo_version));
2451 ND_PRINT((ndo, "my AS %s, ",
2452 as_printf(ndo, astostr, sizeof(astostr), ntohs(bgpo.bgpo_myas))));
2453 ND_PRINT((ndo, "Holdtime %us, ", ntohs(bgpo.bgpo_holdtime)));
2454 ND_PRINT((ndo, "ID %s", ipaddr_string(ndo, &bgpo.bgpo_id)));
2455 ND_PRINT((ndo, "\n\t Optional parameters, length: %u", bgpo.bgpo_optlen));
2456
2457 /* some little sanity checking */
2458 if (length < bgpo.bgpo_optlen+BGP_OPEN_SIZE)
2459 return;
2460
2461 /* ugly! */
2462 opt = &((const struct bgp_open *)dat)->bgpo_optlen;
2463 opt++;
2464
2465 i = 0;
2466 while (i < bgpo.bgpo_optlen) {
2467 ND_TCHECK2(opt[i], BGP_OPT_SIZE);
2468 memcpy(&bgpopt, &opt[i], BGP_OPT_SIZE);
2469 if (i + 2 + bgpopt.bgpopt_len > bgpo.bgpo_optlen) {
2470 ND_PRINT((ndo, "\n\t Option %d, length: %u", bgpopt.bgpopt_type, bgpopt.bgpopt_len));
2471 break;
2472 }
2473
2474 ND_PRINT((ndo, "\n\t Option %s (%u), length: %u",
2475 tok2str(bgp_opt_values,"Unknown",
2476 bgpopt.bgpopt_type),
2477 bgpopt.bgpopt_type,
2478 bgpopt.bgpopt_len));
2479
2480 /* now let's decode the options we know*/
2481 switch(bgpopt.bgpopt_type) {
2482
2483 case BGP_OPT_CAP:
2484 bgp_capabilities_print(ndo, &opt[i+BGP_OPT_SIZE],
2485 bgpopt.bgpopt_len);
2486 break;
2487
2488 case BGP_OPT_AUTH:
2489 default:
2490 ND_PRINT((ndo, "\n\t no decoder for option %u",
2491 bgpopt.bgpopt_type));
2492 break;
2493 }
2494 i += BGP_OPT_SIZE + bgpopt.bgpopt_len;
2495 }
2496 return;
2497 trunc:
2498 ND_PRINT((ndo, "[|BGP]"));
2499 }
2500
2501 static void
2502 bgp_update_print(netdissect_options *ndo,
2503 const u_char *dat, int length)
2504 {
2505 struct bgp bgp;
2506 const u_char *p;
2507 int withdrawn_routes_len;
2508 int len;
2509 int i;
2510
2511 ND_TCHECK2(dat[0], BGP_SIZE);
2512 if (length < BGP_SIZE)
2513 goto trunc;
2514 memcpy(&bgp, dat, BGP_SIZE);
2515 p = dat + BGP_SIZE; /*XXX*/
2516 length -= BGP_SIZE;
2517
2518 /* Unfeasible routes */
2519 ND_TCHECK_16BITS(p);
2520 if (length < 2)
2521 goto trunc;
2522 withdrawn_routes_len = EXTRACT_BE_16BITS(p);
2523 p += 2;
2524 length -= 2;
2525 if (withdrawn_routes_len) {
2526 /*
2527 * Without keeping state from the original NLRI message,
2528 * it's not possible to tell if this a v4 or v6 route,
2529 * so only try to decode it if we're not v6 enabled.
2530 */
2531 ND_TCHECK2(p[0], withdrawn_routes_len);
2532 if (length < withdrawn_routes_len)
2533 goto trunc;
2534 ND_PRINT((ndo, "\n\t Withdrawn routes: %d bytes", withdrawn_routes_len));
2535 p += withdrawn_routes_len;
2536 length -= withdrawn_routes_len;
2537 }
2538
2539 ND_TCHECK_16BITS(p);
2540 if (length < 2)
2541 goto trunc;
2542 len = EXTRACT_BE_16BITS(p);
2543 p += 2;
2544 length -= 2;
2545
2546 if (withdrawn_routes_len == 0 && len == 0 && length == 0) {
2547 /* No withdrawn routes, no path attributes, no NLRI */
2548 ND_PRINT((ndo, "\n\t End-of-Rib Marker (empty NLRI)"));
2549 return;
2550 }
2551
2552 if (len) {
2553 /* do something more useful!*/
2554 while (len) {
2555 int aflags, atype, alenlen, alen;
2556
2557 ND_TCHECK2(p[0], 2);
2558 if (len < 2)
2559 goto trunc;
2560 if (length < 2)
2561 goto trunc;
2562 aflags = EXTRACT_8BITS(p);
2563 atype = EXTRACT_8BITS(p + 1);
2564 p += 2;
2565 len -= 2;
2566 length -= 2;
2567 alenlen = bgp_attr_lenlen(aflags, p);
2568 ND_TCHECK2(p[0], alenlen);
2569 if (len < alenlen)
2570 goto trunc;
2571 if (length < alenlen)
2572 goto trunc;
2573 alen = bgp_attr_len(aflags, p);
2574 p += alenlen;
2575 len -= alenlen;
2576 length -= alenlen;
2577
2578 ND_PRINT((ndo, "\n\t %s (%u), length: %u",
2579 tok2str(bgp_attr_values, "Unknown Attribute",
2580 atype),
2581 atype,
2582 alen));
2583
2584 if (aflags) {
2585 ND_PRINT((ndo, ", Flags [%s%s%s%s",
2586 aflags & 0x80 ? "O" : "",
2587 aflags & 0x40 ? "T" : "",
2588 aflags & 0x20 ? "P" : "",
2589 aflags & 0x10 ? "E" : ""));
2590 if (aflags & 0xf)
2591 ND_PRINT((ndo, "+%x", aflags & 0xf));
2592 ND_PRINT((ndo, "]: "));
2593 }
2594 if (len < alen)
2595 goto trunc;
2596 if (length < alen)
2597 goto trunc;
2598 if (!bgp_attr_print(ndo, atype, p, alen))
2599 goto trunc;
2600 p += alen;
2601 len -= alen;
2602 length -= alen;
2603 }
2604 }
2605
2606 if (length) {
2607 /*
2608 * XXX - what if they're using the "Advertisement of
2609 * Multiple Paths in BGP" feature:
2610 *
2611 * https://datatracker.ietf.org/doc/draft-ietf-idr-add-paths/
2612 *
2613 * http://tools.ietf.org/html/draft-ietf-idr-add-paths-06
2614 */
2615 ND_PRINT((ndo, "\n\t Updated routes:"));
2616 while (length) {
2617 char buf[MAXHOSTNAMELEN + 100];
2618 i = decode_prefix4(ndo, p, length, buf, sizeof(buf));
2619 if (i == -1) {
2620 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2621 break;
2622 } else if (i == -2)
2623 goto trunc;
2624 else if (i == -3)
2625 goto trunc; /* bytes left, but not enough */
2626 else {
2627 ND_PRINT((ndo, "\n\t %s", buf));
2628 p += i;
2629 length -= i;
2630 }
2631 }
2632 }
2633 return;
2634 trunc:
2635 ND_PRINT((ndo, "[|BGP]"));
2636 }
2637
2638 static void
2639 bgp_notification_print(netdissect_options *ndo,
2640 const u_char *dat, int length)
2641 {
2642 struct bgp_notification bgpn;
2643 const u_char *tptr;
2644 uint8_t shutdown_comm_length;
2645 uint8_t remainder_offset;
2646
2647 ND_TCHECK2(dat[0], BGP_NOTIFICATION_SIZE);
2648 memcpy(&bgpn, dat, BGP_NOTIFICATION_SIZE);
2649
2650 /* some little sanity checking */
2651 if (length<BGP_NOTIFICATION_SIZE)
2652 return;
2653
2654 ND_PRINT((ndo, ", %s (%u)",
2655 tok2str(bgp_notify_major_values, "Unknown Error",
2656 bgpn.bgpn_major),
2657 bgpn.bgpn_major));
2658
2659 switch (bgpn.bgpn_major) {
2660
2661 case BGP_NOTIFY_MAJOR_MSG:
2662 ND_PRINT((ndo, ", subcode %s (%u)",
2663 tok2str(bgp_notify_minor_msg_values, "Unknown",
2664 bgpn.bgpn_minor),
2665 bgpn.bgpn_minor));
2666 break;
2667 case BGP_NOTIFY_MAJOR_OPEN:
2668 ND_PRINT((ndo, ", subcode %s (%u)",
2669 tok2str(bgp_notify_minor_open_values, "Unknown",
2670 bgpn.bgpn_minor),
2671 bgpn.bgpn_minor));
2672 break;
2673 case BGP_NOTIFY_MAJOR_UPDATE:
2674 ND_PRINT((ndo, ", subcode %s (%u)",
2675 tok2str(bgp_notify_minor_update_values, "Unknown",
2676 bgpn.bgpn_minor),
2677 bgpn.bgpn_minor));
2678 break;
2679 case BGP_NOTIFY_MAJOR_FSM:
2680 ND_PRINT((ndo, " subcode %s (%u)",
2681 tok2str(bgp_notify_minor_fsm_values, "Unknown",
2682 bgpn.bgpn_minor),
2683 bgpn.bgpn_minor));
2684 break;
2685 case BGP_NOTIFY_MAJOR_CAP:
2686 ND_PRINT((ndo, " subcode %s (%u)",
2687 tok2str(bgp_notify_minor_cap_values, "Unknown",
2688 bgpn.bgpn_minor),
2689 bgpn.bgpn_minor));
2690 break;
2691 case BGP_NOTIFY_MAJOR_CEASE:
2692 ND_PRINT((ndo, ", subcode %s (%u)",
2693 tok2str(bgp_notify_minor_cease_values, "Unknown",
2694 bgpn.bgpn_minor),
2695 bgpn.bgpn_minor));
2696
2697 /* draft-ietf-idr-cease-subcode-02 mentions optionally 7 bytes
2698 * for the maxprefix subtype, which may contain AFI, SAFI and MAXPREFIXES
2699 */
2700 if(bgpn.bgpn_minor == BGP_NOTIFY_MINOR_CEASE_MAXPRFX && length >= BGP_NOTIFICATION_SIZE + 7) {
2701 tptr = dat + BGP_NOTIFICATION_SIZE;
2702 ND_TCHECK2(*tptr, 7);
2703 ND_PRINT((ndo, ", AFI %s (%u), SAFI %s (%u), Max Prefixes: %u",
2704 tok2str(af_values, "Unknown",
2705 EXTRACT_BE_16BITS(tptr)),
2706 EXTRACT_BE_16BITS(tptr),
2707 tok2str(bgp_safi_values, "Unknown", EXTRACT_8BITS((tptr + 2))),
2708 EXTRACT_8BITS((tptr + 2)),
2709 EXTRACT_BE_32BITS(tptr + 3)));
2710 }
2711 /*
2712 * draft-ietf-idr-shutdown describes a method to send a communication
2713 * intended for human consumption regarding the Administrative Shutdown
2714 */
2715 if ((bgpn.bgpn_minor == BGP_NOTIFY_MINOR_CEASE_SHUT ||
2716 bgpn.bgpn_minor == BGP_NOTIFY_MINOR_CEASE_RESET) &&
2717 length >= BGP_NOTIFICATION_SIZE + 1) {
2718 tptr = dat + BGP_NOTIFICATION_SIZE;
2719 ND_TCHECK_8BITS(tptr);
2720 shutdown_comm_length = EXTRACT_8BITS(tptr);
2721 remainder_offset = 0;
2722 /* garbage, hexdump it all */
2723 if (shutdown_comm_length > BGP_NOTIFY_MINOR_CEASE_ADMIN_SHUTDOWN_LEN ||
2724 shutdown_comm_length > length - (BGP_NOTIFICATION_SIZE + 1)) {
2725 ND_PRINT((ndo, ", invalid Shutdown Communication length"));
2726 }
2727 else if (shutdown_comm_length == 0) {
2728 ND_PRINT((ndo, ", empty Shutdown Communication"));
2729 remainder_offset += 1;
2730 }
2731 /* a proper shutdown communication */
2732 else {
2733 ND_TCHECK2(*(tptr+1), shutdown_comm_length);
2734 ND_PRINT((ndo, ", Shutdown Communication (length: %u): \"", shutdown_comm_length));
2735 (void)fn_printn(ndo, tptr+1, shutdown_comm_length, NULL);
2736 ND_PRINT((ndo, "\""));
2737 remainder_offset += shutdown_comm_length + 1;
2738 }
2739 /* if there is trailing data, hexdump it */
2740 if(length - (remainder_offset + BGP_NOTIFICATION_SIZE) > 0) {
2741 ND_PRINT((ndo, ", Data: (length: %u)", length - (remainder_offset + BGP_NOTIFICATION_SIZE)));
2742 hex_print(ndo, "\n\t\t", tptr + remainder_offset, length - (remainder_offset + BGP_NOTIFICATION_SIZE));
2743 }
2744 }
2745 break;
2746 default:
2747 break;
2748 }
2749
2750 return;
2751 trunc:
2752 ND_PRINT((ndo, "[|BGP]"));
2753 }
2754
2755 static void
2756 bgp_route_refresh_print(netdissect_options *ndo,
2757 const u_char *pptr, int len)
2758 {
2759 const struct bgp_route_refresh *bgp_route_refresh_header;
2760
2761 ND_TCHECK2(pptr[0], BGP_ROUTE_REFRESH_SIZE);
2762
2763 /* some little sanity checking */
2764 if (len<BGP_ROUTE_REFRESH_SIZE)
2765 return;
2766
2767 bgp_route_refresh_header = (const struct bgp_route_refresh *)pptr;
2768
2769 ND_PRINT((ndo, "\n\t AFI %s (%u), SAFI %s (%u)",
2770 tok2str(af_values,"Unknown",
2771 /* this stinks but the compiler pads the structure
2772 * weird */
2773 EXTRACT_BE_16BITS(&bgp_route_refresh_header->afi)),
2774 EXTRACT_BE_16BITS(&bgp_route_refresh_header->afi),
2775 tok2str(bgp_safi_values,"Unknown",
2776 bgp_route_refresh_header->safi),
2777 bgp_route_refresh_header->safi));
2778
2779 if (ndo->ndo_vflag > 1) {
2780 ND_TCHECK2(*pptr, len);
2781 print_unknown_data(ndo, pptr, "\n\t ", len);
2782 }
2783
2784 return;
2785 trunc:
2786 ND_PRINT((ndo, "[|BGP]"));
2787 }
2788
2789 static int
2790 bgp_header_print(netdissect_options *ndo,
2791 const u_char *dat, int length)
2792 {
2793 struct bgp bgp;
2794
2795 ND_TCHECK2(dat[0], BGP_SIZE);
2796 memcpy(&bgp, dat, BGP_SIZE);
2797 ND_PRINT((ndo, "\n\t%s Message (%u), length: %u",
2798 tok2str(bgp_msg_values, "Unknown", bgp.bgp_type),
2799 bgp.bgp_type,
2800 length));
2801
2802 switch (bgp.bgp_type) {
2803 case BGP_OPEN:
2804 bgp_open_print(ndo, dat, length);
2805 break;
2806 case BGP_UPDATE:
2807 bgp_update_print(ndo, dat, length);
2808 break;
2809 case BGP_NOTIFICATION:
2810 bgp_notification_print(ndo, dat, length);
2811 break;
2812 case BGP_KEEPALIVE:
2813 break;
2814 case BGP_ROUTE_REFRESH:
2815 bgp_route_refresh_print(ndo, dat, length);
2816 break;
2817 default:
2818 /* we have no decoder for the BGP message */
2819 ND_TCHECK2(*dat, length);
2820 ND_PRINT((ndo, "\n\t no Message %u decoder", bgp.bgp_type));
2821 print_unknown_data(ndo, dat, "\n\t ", length);
2822 break;
2823 }
2824 return 1;
2825 trunc:
2826 ND_PRINT((ndo, "[|BGP]"));
2827 return 0;
2828 }
2829
2830 void
2831 bgp_print(netdissect_options *ndo,
2832 const u_char *dat, int length)
2833 {
2834 const u_char *p;
2835 const u_char *ep;
2836 const u_char *start;
2837 const u_char marker[] = {
2838 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2839 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2840 };
2841 struct bgp bgp;
2842 uint16_t hlen;
2843
2844 ep = dat + length;
2845 if (ndo->ndo_snapend < dat + length)
2846 ep = ndo->ndo_snapend;
2847
2848 ND_PRINT((ndo, ": BGP"));
2849
2850 if (ndo->ndo_vflag < 1) /* lets be less chatty */
2851 return;
2852
2853 p = dat;
2854 start = p;
2855 while (p < ep) {
2856 if (!ND_TTEST2(p[0], 1))
2857 break;
2858 if (p[0] != 0xff) {
2859 p++;
2860 continue;
2861 }
2862
2863 if (!ND_TTEST2(p[0], sizeof(marker)))
2864 break;
2865 if (memcmp(p, marker, sizeof(marker)) != 0) {
2866 p++;
2867 continue;
2868 }
2869
2870 /* found BGP header */
2871 ND_TCHECK2(p[0], BGP_SIZE); /*XXX*/
2872 memcpy(&bgp, p, BGP_SIZE);
2873
2874 if (start != p)
2875 ND_PRINT((ndo, " [|BGP]"));
2876
2877 hlen = ntohs(bgp.bgp_len);
2878 if (hlen < BGP_SIZE) {
2879 ND_PRINT((ndo, "\n[|BGP Bogus header length %u < %u]", hlen,
2880 BGP_SIZE));
2881 break;
2882 }
2883
2884 if (ND_TTEST2(p[0], hlen)) {
2885 if (!bgp_header_print(ndo, p, hlen))
2886 return;
2887 p += hlen;
2888 start = p;
2889 } else {
2890 ND_PRINT((ndo, "\n[|BGP %s]",
2891 tok2str(bgp_msg_values,
2892 "Unknown Message Type",
2893 bgp.bgp_type)));
2894 break;
2895 }
2896 }
2897
2898 return;
2899
2900 trunc:
2901 ND_PRINT((ndo, " [|BGP]"));
2902 }
2903
2904 /*
2905 * Local Variables:
2906 * c-style: whitesmith
2907 * c-basic-offset: 4
2908 * End:
2909 */