]> The Tcpdump Group git mirrors - tcpdump/blob - print-bgp.c
Merge remote-tracking branch 'upstream/master'
[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_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_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_TCHECK2(pptr[0], 1);
684 addr_length = *pptr++;
685
686 /* Source address */
687 ND_TCHECK2(pptr[0], (addr_length >> 3));
688 total_length += (addr_length >> 3) + 1;
689 offset = strlen(buf);
690 if (addr_length) {
691 snprintf(buf + offset, buflen - offset, ", Source %s",
692 bgp_vpn_ip_print(ndo, pptr, addr_length));
693 pptr += (addr_length >> 3);
694 }
695
696 /* Group address length, encoded in bits */
697 ND_TCHECK2(pptr[0], 1);
698 addr_length = *pptr++;
699
700 /* Group address */
701 ND_TCHECK2(pptr[0], (addr_length >> 3));
702 total_length += (addr_length >> 3) + 1;
703 offset = strlen(buf);
704 if (addr_length) {
705 snprintf(buf + offset, buflen - offset, ", Group %s",
706 bgp_vpn_ip_print(ndo, pptr, addr_length));
707 pptr += (addr_length >> 3);
708 }
709
710 trunc:
711 return (total_length);
712 }
713
714 /* RDs and RTs share the same semantics
715 * we use bgp_vpn_rd_print for
716 * printing route targets inside a NLRI */
717 char *
718 bgp_vpn_rd_print(netdissect_options *ndo,
719 const u_char *pptr)
720 {
721 /* allocate space for the largest possible string */
722 static char rd[sizeof("xxxxxxxxxx:xxxxx (xxx.xxx.xxx.xxx:xxxxx)")];
723 char *pos = rd;
724
725 /* ok lets load the RD format */
726 switch (EXTRACT_16BITS(pptr)) {
727
728 /* 2-byte-AS:number fmt*/
729 case 0:
730 snprintf(pos, sizeof(rd) - (pos - rd), "%u:%u (= %u.%u.%u.%u)",
731 EXTRACT_16BITS(pptr+2),
732 EXTRACT_32BITS(pptr+4),
733 *(pptr+4), *(pptr+5), *(pptr+6), *(pptr+7));
734 break;
735 /* IP-address:AS fmt*/
736
737 case 1:
738 snprintf(pos, sizeof(rd) - (pos - rd), "%u.%u.%u.%u:%u",
739 *(pptr+2), *(pptr+3), *(pptr+4), *(pptr+5), EXTRACT_16BITS(pptr+6));
740 break;
741
742 /* 4-byte-AS:number fmt*/
743 case 2:
744 snprintf(pos, sizeof(rd) - (pos - rd), "%s:%u (%u.%u.%u.%u:%u)",
745 as_printf(ndo, astostr, sizeof(astostr), EXTRACT_32BITS(pptr+2)),
746 EXTRACT_16BITS(pptr+6), *(pptr+2), *(pptr+3), *(pptr+4),
747 *(pptr+5), EXTRACT_16BITS(pptr+6));
748 break;
749 default:
750 snprintf(pos, sizeof(rd) - (pos - rd), "unknown RD format");
751 break;
752 }
753 pos += strlen(pos);
754 *(pos) = '\0';
755 return (rd);
756 }
757
758 static int
759 decode_rt_routing_info(netdissect_options *ndo,
760 const u_char *pptr, char *buf, u_int buflen)
761 {
762 uint8_t route_target[8];
763 u_int plen;
764 char asbuf[sizeof(astostr)]; /* bgp_vpn_rd_print() overwrites astostr */
765
766 /* NLRI "prefix length" from RFC 2858 Section 4. */
767 ND_TCHECK(pptr[0]);
768 plen = pptr[0]; /* get prefix length */
769
770 /* NLRI "prefix" (ibid), valid lengths are { 0, 32, 33, ..., 96 } bits.
771 * RFC 4684 Section 4 defines the layout of "origin AS" and "route
772 * target" fields inside the "prefix" depending on its length.
773 */
774 if (0 == plen) {
775 /* Without "origin AS", without "route target". */
776 snprintf(buf, buflen, "default route target");
777 return 1;
778 }
779
780 if (32 > plen)
781 return -1;
782
783 /* With at least "origin AS", possibly with "route target". */
784 ND_TCHECK_32BITS(pptr + 1);
785 as_printf(ndo, asbuf, sizeof(asbuf), EXTRACT_32BITS(pptr + 1));
786
787 plen-=32; /* adjust prefix length */
788
789 if (64 < plen)
790 return -1;
791
792 /* From now on (plen + 7) / 8 evaluates to { 0, 1, 2, ..., 8 }
793 * and gives the number of octets in the variable-length "route
794 * target" field inside this NLRI "prefix". Look for it.
795 */
796 memset(&route_target, 0, sizeof(route_target));
797 ND_TCHECK2(pptr[5], (plen + 7) / 8);
798 memcpy(&route_target, &pptr[5], (plen + 7) / 8);
799 /* Which specification says to do this? */
800 if (plen % 8) {
801 ((u_char *)&route_target)[(plen + 7) / 8 - 1] &=
802 ((0xff00 >> (plen % 8)) & 0xff);
803 }
804 snprintf(buf, buflen, "origin AS: %s, route target %s",
805 asbuf,
806 bgp_vpn_rd_print(ndo, (u_char *)&route_target));
807
808 return 5 + (plen + 7) / 8;
809
810 trunc:
811 return -2;
812 }
813
814 static int
815 decode_labeled_vpn_prefix4(netdissect_options *ndo,
816 const u_char *pptr, char *buf, u_int buflen)
817 {
818 struct in_addr addr;
819 u_int plen;
820
821 ND_TCHECK(pptr[0]);
822 plen = pptr[0]; /* get prefix length */
823
824 if ((24+64) > plen)
825 return -1;
826
827 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
828
829 if (32 < plen)
830 return -1;
831
832 memset(&addr, 0, sizeof(addr));
833 ND_TCHECK2(pptr[12], (plen + 7) / 8);
834 memcpy(&addr, &pptr[12], (plen + 7) / 8);
835 if (plen % 8) {
836 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
837 ((0xff00 >> (plen % 8)) & 0xff);
838 }
839 /* the label may get offsetted by 4 bits so lets shift it right */
840 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
841 bgp_vpn_rd_print(ndo, pptr+4),
842 ipaddr_string(ndo, &addr),
843 plen,
844 EXTRACT_24BITS(pptr+1)>>4,
845 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
846
847 return 12 + (plen + 7) / 8;
848
849 trunc:
850 return -2;
851 }
852
853 /*
854 * +-------------------------------+
855 * | |
856 * | RD:IPv4-address (12 octets) |
857 * | |
858 * +-------------------------------+
859 * | MDT Group-address (4 octets) |
860 * +-------------------------------+
861 */
862
863 #define MDT_VPN_NLRI_LEN 16
864
865 static int
866 decode_mdt_vpn_nlri(netdissect_options *ndo,
867 const u_char *pptr, char *buf, u_int buflen)
868 {
869
870 const u_char *rd;
871 const u_char *vpn_ip;
872
873 ND_TCHECK(pptr[0]);
874
875 /* if the NLRI is not predefined length, quit.*/
876 if (*pptr != MDT_VPN_NLRI_LEN * 8)
877 return -1;
878 pptr++;
879
880 /* RD */
881 ND_TCHECK2(pptr[0], 8);
882 rd = pptr;
883 pptr+=8;
884
885 /* IPv4 address */
886 ND_TCHECK2(pptr[0], sizeof(struct in_addr));
887 vpn_ip = pptr;
888 pptr+=sizeof(struct in_addr);
889
890 /* MDT Group Address */
891 ND_TCHECK2(pptr[0], sizeof(struct in_addr));
892
893 snprintf(buf, buflen, "RD: %s, VPN IP Address: %s, MC Group Address: %s",
894 bgp_vpn_rd_print(ndo, rd), ipaddr_string(ndo, vpn_ip), ipaddr_string(ndo, pptr));
895
896 return MDT_VPN_NLRI_LEN + 1;
897
898 trunc:
899
900 return -2;
901 }
902
903 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI 1
904 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI 2
905 #define BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI 3
906 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF 4
907 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE 5
908 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN 6
909 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN 7
910
911 static const struct tok bgp_multicast_vpn_route_type_values[] = {
912 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI, "Intra-AS I-PMSI"},
913 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI, "Inter-AS I-PMSI"},
914 { BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI, "S-PMSI"},
915 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF, "Intra-AS Segment-Leaf"},
916 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE, "Source-Active"},
917 { BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN, "Shared Tree Join"},
918 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN, "Source Tree Join"},
919 { 0, NULL}
920 };
921
922 static int
923 decode_multicast_vpn(netdissect_options *ndo,
924 const u_char *pptr, char *buf, u_int buflen)
925 {
926 uint8_t route_type, route_length, addr_length, sg_length;
927 u_int offset;
928
929 ND_TCHECK2(pptr[0], 2);
930 route_type = *pptr++;
931 route_length = *pptr++;
932
933 snprintf(buf, buflen, "Route-Type: %s (%u), length: %u",
934 tok2str(bgp_multicast_vpn_route_type_values,
935 "Unknown", route_type),
936 route_type, route_length);
937
938 switch(route_type) {
939 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI:
940 ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN);
941 offset = strlen(buf);
942 snprintf(buf + offset, buflen - offset, ", RD: %s, Originator %s",
943 bgp_vpn_rd_print(ndo, pptr),
944 bgp_vpn_ip_print(ndo, pptr + BGP_VPN_RD_LEN,
945 (route_length - BGP_VPN_RD_LEN) << 3));
946 break;
947 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI:
948 ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN + 4);
949 offset = strlen(buf);
950 snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
951 bgp_vpn_rd_print(ndo, pptr),
952 as_printf(ndo, astostr, sizeof(astostr),
953 EXTRACT_32BITS(pptr + BGP_VPN_RD_LEN)));
954 break;
955
956 case BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI:
957 ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN);
958 offset = strlen(buf);
959 snprintf(buf + offset, buflen - offset, ", RD: %s",
960 bgp_vpn_rd_print(ndo, pptr));
961 pptr += BGP_VPN_RD_LEN;
962
963 sg_length = bgp_vpn_sg_print(ndo, pptr, buf, buflen);
964 addr_length = route_length - sg_length;
965
966 ND_TCHECK2(pptr[0], addr_length);
967 offset = strlen(buf);
968 snprintf(buf + offset, buflen - offset, ", Originator %s",
969 bgp_vpn_ip_print(ndo, pptr, addr_length << 3));
970 break;
971
972 case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE:
973 ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN);
974 offset = strlen(buf);
975 snprintf(buf + offset, buflen - offset, ", RD: %s",
976 bgp_vpn_rd_print(ndo, pptr));
977 pptr += BGP_VPN_RD_LEN;
978
979 bgp_vpn_sg_print(ndo, pptr, buf, buflen);
980 break;
981
982 case BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN: /* fall through */
983 case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN:
984 ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN + 4);
985 offset = strlen(buf);
986 snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
987 bgp_vpn_rd_print(ndo, pptr),
988 as_printf(ndo, astostr, sizeof(astostr),
989 EXTRACT_32BITS(pptr + BGP_VPN_RD_LEN)));
990 pptr += BGP_VPN_RD_LEN + 4;
991
992 bgp_vpn_sg_print(ndo, pptr, buf, buflen);
993 break;
994
995 /*
996 * no per route-type printing yet.
997 */
998 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF:
999 default:
1000 break;
1001 }
1002
1003 return route_length + 2;
1004
1005 trunc:
1006 return -2;
1007 }
1008
1009 /*
1010 * As I remember, some versions of systems have an snprintf() that
1011 * returns -1 if the buffer would have overflowed. If the return
1012 * value is negative, set buflen to 0, to indicate that we've filled
1013 * the buffer up.
1014 *
1015 * If the return value is greater than buflen, that means that
1016 * the buffer would have overflowed; again, set buflen to 0 in
1017 * that case.
1018 */
1019 #define UPDATE_BUF_BUFLEN(buf, buflen, stringlen) \
1020 if (stringlen<0) \
1021 buflen=0; \
1022 else if ((u_int)stringlen>buflen) \
1023 buflen=0; \
1024 else { \
1025 buflen-=stringlen; \
1026 buf+=stringlen; \
1027 }
1028
1029 static int
1030 decode_labeled_vpn_l2(netdissect_options *ndo,
1031 const u_char *pptr, char *buf, u_int buflen)
1032 {
1033 int plen,tlen,stringlen,tlv_type,tlv_len,ttlv_len;
1034
1035 ND_TCHECK2(pptr[0], 2);
1036 plen=EXTRACT_16BITS(pptr);
1037 tlen=plen;
1038 pptr+=2;
1039 /* Old and new L2VPN NLRI share AFI/SAFI
1040 * -> Assume a 12 Byte-length NLRI is auto-discovery-only
1041 * and > 17 as old format. Complain for the middle case
1042 */
1043 if (plen==12) {
1044 /* assume AD-only with RD, BGPNH */
1045 ND_TCHECK2(pptr[0],12);
1046 buf[0]='\0';
1047 stringlen=snprintf(buf, buflen, "RD: %s, BGPNH: %s",
1048 bgp_vpn_rd_print(ndo, pptr),
1049 ipaddr_string(ndo, pptr+8)
1050 );
1051 UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1052 pptr+=12;
1053 tlen-=12;
1054 return plen;
1055 } else if (plen>17) {
1056 /* assume old format */
1057 /* RD, ID, LBLKOFF, LBLBASE */
1058
1059 ND_TCHECK2(pptr[0],15);
1060 buf[0]='\0';
1061 stringlen=snprintf(buf, buflen, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
1062 bgp_vpn_rd_print(ndo, pptr),
1063 EXTRACT_16BITS(pptr+8),
1064 EXTRACT_16BITS(pptr+10),
1065 EXTRACT_24BITS(pptr+12)>>4); /* the label is offsetted by 4 bits so lets shift it right */
1066 UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1067 pptr+=15;
1068 tlen-=15;
1069
1070 /* ok now the variable part - lets read out TLVs*/
1071 while (tlen>0) {
1072 if (tlen < 3)
1073 return -1;
1074 ND_TCHECK2(pptr[0], 3);
1075 tlv_type=*pptr++;
1076 tlv_len=EXTRACT_16BITS(pptr);
1077 ttlv_len=tlv_len;
1078 pptr+=2;
1079
1080 switch(tlv_type) {
1081 case 1:
1082 if (buflen!=0) {
1083 stringlen=snprintf(buf,buflen, "\n\t\tcircuit status vector (%u) length: %u: 0x",
1084 tlv_type,
1085 tlv_len);
1086 UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1087 }
1088 ttlv_len=ttlv_len/8+1; /* how many bytes do we need to read ? */
1089 while (ttlv_len>0) {
1090 ND_TCHECK(pptr[0]);
1091 if (buflen!=0) {
1092 stringlen=snprintf(buf,buflen, "%02x",*pptr++);
1093 UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1094 }
1095 ttlv_len--;
1096 }
1097 break;
1098 default:
1099 if (buflen!=0) {
1100 stringlen=snprintf(buf,buflen, "\n\t\tunknown TLV #%u, length: %u",
1101 tlv_type,
1102 tlv_len);
1103 UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1104 }
1105 break;
1106 }
1107 tlen-=(tlv_len<<3); /* the tlv-length is expressed in bits so lets shift it right */
1108 }
1109 return plen+2;
1110
1111 } else {
1112 /* complain bitterly ? */
1113 /* fall through */
1114 goto trunc;
1115 }
1116
1117 trunc:
1118 return -2;
1119 }
1120
1121 int
1122 decode_prefix6(netdissect_options *ndo,
1123 const u_char *pd, u_int itemlen, char *buf, u_int buflen)
1124 {
1125 struct in6_addr addr;
1126 u_int plen, plenbytes;
1127
1128 ND_TCHECK(pd[0]);
1129 ITEMCHECK(1);
1130 plen = pd[0];
1131 if (128 < plen)
1132 return -1;
1133 itemlen -= 1;
1134
1135 memset(&addr, 0, sizeof(addr));
1136 plenbytes = (plen + 7) / 8;
1137 ND_TCHECK2(pd[1], plenbytes);
1138 ITEMCHECK(plenbytes);
1139 memcpy(&addr, &pd[1], plenbytes);
1140 if (plen % 8) {
1141 addr.s6_addr[plenbytes - 1] &=
1142 ((0xff00 >> (plen % 8)) & 0xff);
1143 }
1144 snprintf(buf, buflen, "%s/%d", ip6addr_string(ndo, &addr), plen);
1145 return 1 + plenbytes;
1146
1147 trunc:
1148 return -2;
1149
1150 badtlv:
1151 return -3;
1152 }
1153
1154 static int
1155 decode_labeled_prefix6(netdissect_options *ndo,
1156 const u_char *pptr, u_int itemlen, char *buf, u_int buflen)
1157 {
1158 struct in6_addr addr;
1159 u_int plen, plenbytes;
1160
1161 /* prefix length and label = 4 bytes */
1162 ND_TCHECK2(pptr[0], 4);
1163 ITEMCHECK(4);
1164 plen = pptr[0]; /* get prefix length */
1165
1166 if (24 > plen)
1167 return -1;
1168
1169 plen-=24; /* adjust prefixlen - labellength */
1170
1171 if (128 < plen)
1172 return -1;
1173 itemlen -= 4;
1174
1175 memset(&addr, 0, sizeof(addr));
1176 plenbytes = (plen + 7) / 8;
1177 ND_TCHECK2(pptr[4], plenbytes);
1178 memcpy(&addr, &pptr[4], plenbytes);
1179 if (plen % 8) {
1180 addr.s6_addr[plenbytes - 1] &=
1181 ((0xff00 >> (plen % 8)) & 0xff);
1182 }
1183 /* the label may get offsetted by 4 bits so lets shift it right */
1184 snprintf(buf, buflen, "%s/%d, label:%u %s",
1185 ip6addr_string(ndo, &addr),
1186 plen,
1187 EXTRACT_24BITS(pptr+1)>>4,
1188 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1189
1190 return 4 + plenbytes;
1191
1192 trunc:
1193 return -2;
1194
1195 badtlv:
1196 return -3;
1197 }
1198
1199 static int
1200 decode_labeled_vpn_prefix6(netdissect_options *ndo,
1201 const u_char *pptr, char *buf, u_int buflen)
1202 {
1203 struct in6_addr addr;
1204 u_int plen;
1205
1206 ND_TCHECK(pptr[0]);
1207 plen = pptr[0]; /* get prefix length */
1208
1209 if ((24+64) > plen)
1210 return -1;
1211
1212 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
1213
1214 if (128 < plen)
1215 return -1;
1216
1217 memset(&addr, 0, sizeof(addr));
1218 ND_TCHECK2(pptr[12], (plen + 7) / 8);
1219 memcpy(&addr, &pptr[12], (plen + 7) / 8);
1220 if (plen % 8) {
1221 addr.s6_addr[(plen + 7) / 8 - 1] &=
1222 ((0xff00 >> (plen % 8)) & 0xff);
1223 }
1224 /* the label may get offsetted by 4 bits so lets shift it right */
1225 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
1226 bgp_vpn_rd_print(ndo, pptr+4),
1227 ip6addr_string(ndo, &addr),
1228 plen,
1229 EXTRACT_24BITS(pptr+1)>>4,
1230 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1231
1232 return 12 + (plen + 7) / 8;
1233
1234 trunc:
1235 return -2;
1236 }
1237
1238 static int
1239 decode_clnp_prefix(netdissect_options *ndo,
1240 const u_char *pptr, char *buf, u_int buflen)
1241 {
1242 uint8_t addr[19];
1243 u_int plen;
1244
1245 ND_TCHECK(pptr[0]);
1246 plen = pptr[0]; /* get prefix length */
1247
1248 if (152 < plen)
1249 return -1;
1250
1251 memset(&addr, 0, sizeof(addr));
1252 ND_TCHECK2(pptr[4], (plen + 7) / 8);
1253 memcpy(&addr, &pptr[4], (plen + 7) / 8);
1254 if (plen % 8) {
1255 addr[(plen + 7) / 8 - 1] &=
1256 ((0xff00 >> (plen % 8)) & 0xff);
1257 }
1258 snprintf(buf, buflen, "%s/%d",
1259 isonsap_string(ndo, addr,(plen + 7) / 8),
1260 plen);
1261
1262 return 1 + (plen + 7) / 8;
1263
1264 trunc:
1265 return -2;
1266 }
1267
1268 static int
1269 decode_labeled_vpn_clnp_prefix(netdissect_options *ndo,
1270 const u_char *pptr, char *buf, u_int buflen)
1271 {
1272 uint8_t addr[19];
1273 u_int plen;
1274
1275 ND_TCHECK(pptr[0]);
1276 plen = pptr[0]; /* get prefix length */
1277
1278 if ((24+64) > plen)
1279 return -1;
1280
1281 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
1282
1283 if (152 < plen)
1284 return -1;
1285
1286 memset(&addr, 0, sizeof(addr));
1287 ND_TCHECK2(pptr[12], (plen + 7) / 8);
1288 memcpy(&addr, &pptr[12], (plen + 7) / 8);
1289 if (plen % 8) {
1290 addr[(plen + 7) / 8 - 1] &=
1291 ((0xff00 >> (plen % 8)) & 0xff);
1292 }
1293 /* the label may get offsetted by 4 bits so lets shift it right */
1294 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
1295 bgp_vpn_rd_print(ndo, pptr+4),
1296 isonsap_string(ndo, addr,(plen + 7) / 8),
1297 plen,
1298 EXTRACT_24BITS(pptr+1)>>4,
1299 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1300
1301 return 12 + (plen + 7) / 8;
1302
1303 trunc:
1304 return -2;
1305 }
1306
1307 /*
1308 * bgp_attr_get_as_size
1309 *
1310 * Try to find the size of the ASs encoded in an as-path. It is not obvious, as
1311 * both Old speakers that do not support 4 byte AS, and the new speakers that do
1312 * support, exchange AS-Path with the same path-attribute type value 0x02.
1313 */
1314 static int
1315 bgp_attr_get_as_size(netdissect_options *ndo,
1316 uint8_t bgpa_type, const u_char *pptr, int len)
1317 {
1318 const u_char *tptr = pptr;
1319
1320 /*
1321 * If the path attribute is the optional AS4 path type, then we already
1322 * know, that ASs must be encoded in 4 byte format.
1323 */
1324 if (bgpa_type == BGPTYPE_AS4_PATH) {
1325 return 4;
1326 }
1327
1328 /*
1329 * Let us assume that ASs are of 2 bytes in size, and check if the AS-Path
1330 * TLV is good. If not, ask the caller to try with AS encoded as 4 bytes
1331 * each.
1332 */
1333 while (tptr < pptr + len) {
1334 ND_TCHECK(tptr[0]);
1335
1336 /*
1337 * If we do not find a valid segment type, our guess might be wrong.
1338 */
1339 if (tptr[0] < BGP_AS_SEG_TYPE_MIN || tptr[0] > BGP_AS_SEG_TYPE_MAX) {
1340 goto trunc;
1341 }
1342 ND_TCHECK(tptr[1]);
1343 tptr += 2 + tptr[1] * 2;
1344 }
1345
1346 /*
1347 * If we correctly reached end of the AS path attribute data content,
1348 * then most likely ASs were indeed encoded as 2 bytes.
1349 */
1350 if (tptr == pptr + len) {
1351 return 2;
1352 }
1353
1354 trunc:
1355
1356 /*
1357 * We can come here, either we did not have enough data, or if we
1358 * try to decode 4 byte ASs in 2 byte format. Either way, return 4,
1359 * so that calller can try to decode each AS as of 4 bytes. If indeed
1360 * there was not enough data, it will crib and end the parse anyways.
1361 */
1362 return 4;
1363 }
1364
1365 /*
1366 * The only way to know that a BGP UPDATE message is using add path is
1367 * by checking if the capability is in the OPEN message which we may have missed.
1368 * So this function checks if it is possible that the update could contain add path
1369 * and if so it checks that standard BGP doesn't make sense.
1370 */
1371
1372 static int
1373 check_add_path(const u_char *pptr, u_int length, u_int max_prefix_length) {
1374
1375 u_int offset, prefix_length;
1376 if (length < 5) {
1377 return 0;
1378 }
1379
1380 /* check if it could be add path */
1381 for(offset = 0; offset < length;) {
1382 offset += 4;
1383 prefix_length = pptr[offset];
1384 /*
1385 * Add 4 to cover the path id
1386 * and check the prefix length isn't greater than 32/128.
1387 */
1388 if (prefix_length > max_prefix_length) {
1389 return 0;
1390 }
1391 /* Add 1 for the prefix_length byte and prefix_length to cover the address */
1392 offset += 1 + ((prefix_length + 7) / 8);
1393 }
1394 /* check we haven't gone past the end of the section */
1395 if (offset > length) {
1396 return 0;
1397 }
1398
1399 /* check it's not standard BGP */
1400 for(offset = 0; offset < length; ) {
1401 prefix_length = pptr[offset];
1402 /*
1403 * If the prefix_length is zero (0.0.0.0/0)
1404 * and since it's not the only address (length >= 5)
1405 * then it is add-path
1406 */
1407 if (prefix_length < 1 || prefix_length > max_prefix_length) {
1408 return 1;
1409 }
1410 offset += 1 + ((prefix_length + 7) / 8);
1411 }
1412 if (offset > length) {
1413 return 1;
1414 }
1415
1416 /* assume not add-path by default */
1417 return 0;
1418 }
1419
1420
1421 static int
1422 bgp_attr_print(netdissect_options *ndo,
1423 u_int atype, const u_char *pptr, u_int len)
1424 {
1425 int i;
1426 uint16_t af;
1427 uint8_t safi, snpa, nhlen;
1428 union { /* copy buffer for bandwidth values */
1429 float f;
1430 uint32_t i;
1431 } bw;
1432 int advance;
1433 u_int tlen;
1434 const u_char *tptr;
1435 char buf[MAXHOSTNAMELEN + 100];
1436 int as_size;
1437 int add_path4, add_path6, path_id;
1438
1439 tptr = pptr;
1440 tlen=len;
1441
1442 switch (atype) {
1443 case BGPTYPE_ORIGIN:
1444 if (len != 1)
1445 ND_PRINT((ndo, "invalid len"));
1446 else {
1447 ND_TCHECK(*tptr);
1448 ND_PRINT((ndo, "%s", tok2str(bgp_origin_values,
1449 "Unknown Origin Typecode",
1450 tptr[0])));
1451 }
1452 break;
1453
1454 /*
1455 * Process AS4 byte path and AS2 byte path attributes here.
1456 */
1457 case BGPTYPE_AS4_PATH:
1458 case BGPTYPE_AS_PATH:
1459 if (len % 2) {
1460 ND_PRINT((ndo, "invalid len"));
1461 break;
1462 }
1463 if (!len) {
1464 ND_PRINT((ndo, "empty"));
1465 break;
1466 }
1467
1468 /*
1469 * BGP updates exchanged between New speakers that support 4
1470 * byte AS, ASs are always encoded in 4 bytes. There is no
1471 * definitive way to find this, just by the packet's
1472 * contents. So, check for packet's TLV's sanity assuming
1473 * 2 bytes first, and it does not pass, assume that ASs are
1474 * encoded in 4 bytes format and move on.
1475 */
1476 as_size = bgp_attr_get_as_size(ndo, atype, pptr, len);
1477
1478 while (tptr < pptr + len) {
1479 ND_TCHECK(tptr[0]);
1480 ND_PRINT((ndo, "%s", tok2str(bgp_as_path_segment_open_values,
1481 "?", tptr[0])));
1482 ND_TCHECK(tptr[1]);
1483 for (i = 0; i < tptr[1] * as_size; i += as_size) {
1484 ND_TCHECK2(tptr[2 + i], as_size);
1485 ND_PRINT((ndo, "%s ",
1486 as_printf(ndo, astostr, sizeof(astostr),
1487 as_size == 2 ?
1488 EXTRACT_16BITS(&tptr[2 + i]) :
1489 EXTRACT_32BITS(&tptr[2 + i]))));
1490 }
1491 ND_TCHECK(tptr[0]);
1492 ND_PRINT((ndo, "%s", tok2str(bgp_as_path_segment_close_values,
1493 "?", tptr[0])));
1494 ND_TCHECK(tptr[1]);
1495 tptr += 2 + tptr[1] * as_size;
1496 }
1497 break;
1498 case BGPTYPE_NEXT_HOP:
1499 if (len != 4)
1500 ND_PRINT((ndo, "invalid len"));
1501 else {
1502 ND_TCHECK2(tptr[0], 4);
1503 ND_PRINT((ndo, "%s", ipaddr_string(ndo, tptr)));
1504 }
1505 break;
1506 case BGPTYPE_MULTI_EXIT_DISC:
1507 case BGPTYPE_LOCAL_PREF:
1508 if (len != 4)
1509 ND_PRINT((ndo, "invalid len"));
1510 else {
1511 ND_TCHECK2(tptr[0], 4);
1512 ND_PRINT((ndo, "%u", EXTRACT_32BITS(tptr)));
1513 }
1514 break;
1515 case BGPTYPE_ATOMIC_AGGREGATE:
1516 if (len != 0)
1517 ND_PRINT((ndo, "invalid len"));
1518 break;
1519 case BGPTYPE_AGGREGATOR:
1520
1521 /*
1522 * Depending on the AS encoded is of 2 bytes or of 4 bytes,
1523 * the length of this PA can be either 6 bytes or 8 bytes.
1524 */
1525 if (len != 6 && len != 8) {
1526 ND_PRINT((ndo, "invalid len"));
1527 break;
1528 }
1529 ND_TCHECK2(tptr[0], len);
1530 if (len == 6) {
1531 ND_PRINT((ndo, " AS #%s, origin %s",
1532 as_printf(ndo, astostr, sizeof(astostr), EXTRACT_16BITS(tptr)),
1533 ipaddr_string(ndo, tptr + 2)));
1534 } else {
1535 ND_PRINT((ndo, " AS #%s, origin %s",
1536 as_printf(ndo, astostr, sizeof(astostr),
1537 EXTRACT_32BITS(tptr)), ipaddr_string(ndo, tptr + 4)));
1538 }
1539 break;
1540 case BGPTYPE_AGGREGATOR4:
1541 if (len != 8) {
1542 ND_PRINT((ndo, "invalid len"));
1543 break;
1544 }
1545 ND_TCHECK2(tptr[0], 8);
1546 ND_PRINT((ndo, " AS #%s, origin %s",
1547 as_printf(ndo, astostr, sizeof(astostr), EXTRACT_32BITS(tptr)),
1548 ipaddr_string(ndo, tptr + 4)));
1549 break;
1550 case BGPTYPE_COMMUNITIES:
1551 if (len % 4) {
1552 ND_PRINT((ndo, "invalid len"));
1553 break;
1554 }
1555 while (tlen>0) {
1556 uint32_t comm;
1557 ND_TCHECK2(tptr[0], 4);
1558 comm = EXTRACT_32BITS(tptr);
1559 switch (comm) {
1560 case BGP_COMMUNITY_NO_EXPORT:
1561 ND_PRINT((ndo, " NO_EXPORT"));
1562 break;
1563 case BGP_COMMUNITY_NO_ADVERT:
1564 ND_PRINT((ndo, " NO_ADVERTISE"));
1565 break;
1566 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED:
1567 ND_PRINT((ndo, " NO_EXPORT_SUBCONFED"));
1568 break;
1569 default:
1570 ND_PRINT((ndo, "%u:%u%s",
1571 (comm >> 16) & 0xffff,
1572 comm & 0xffff,
1573 (tlen>4) ? ", " : ""));
1574 break;
1575 }
1576 tlen -=4;
1577 tptr +=4;
1578 }
1579 break;
1580 case BGPTYPE_ORIGINATOR_ID:
1581 if (len != 4) {
1582 ND_PRINT((ndo, "invalid len"));
1583 break;
1584 }
1585 ND_TCHECK2(tptr[0], 4);
1586 ND_PRINT((ndo, "%s",ipaddr_string(ndo, tptr)));
1587 break;
1588 case BGPTYPE_CLUSTER_LIST:
1589 if (len % 4) {
1590 ND_PRINT((ndo, "invalid len"));
1591 break;
1592 }
1593 while (tlen>0) {
1594 ND_TCHECK2(tptr[0], 4);
1595 ND_PRINT((ndo, "%s%s",
1596 ipaddr_string(ndo, tptr),
1597 (tlen>4) ? ", " : ""));
1598 tlen -=4;
1599 tptr +=4;
1600 }
1601 break;
1602 case BGPTYPE_MP_REACH_NLRI:
1603 ND_TCHECK2(tptr[0], 3);
1604 af = EXTRACT_16BITS(tptr);
1605 safi = tptr[2];
1606
1607 ND_PRINT((ndo, "\n\t AFI: %s (%u), %sSAFI: %s (%u)",
1608 tok2str(af_values, "Unknown AFI", af),
1609 af,
1610 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1611 tok2str(bgp_safi_values, "Unknown SAFI", safi),
1612 safi));
1613
1614 switch(af<<8 | safi) {
1615 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1616 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1617 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1618 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1619 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
1620 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1621 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1622 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1623 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN):
1624 case (AFNUM_INET<<8 | SAFNUM_MDT):
1625 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1626 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1627 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1628 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1629 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1630 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1631 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1632 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1633 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1634 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1635 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1636 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1637 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1638 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1639 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1640 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1641 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1642 break;
1643 default:
1644 ND_TCHECK2(tptr[0], tlen);
1645 ND_PRINT((ndo, "\n\t no AFI %u / SAFI %u decoder", af, safi));
1646 if (ndo->ndo_vflag <= 1)
1647 print_unknown_data(ndo, tptr, "\n\t ", tlen);
1648 goto done;
1649 break;
1650 }
1651
1652 tptr +=3;
1653
1654 ND_TCHECK(tptr[0]);
1655 nhlen = tptr[0];
1656 tlen = nhlen;
1657 tptr++;
1658
1659 if (tlen) {
1660 int nnh = 0;
1661 ND_PRINT((ndo, "\n\t nexthop: "));
1662 while (tlen > 0) {
1663 if ( nnh++ > 0 ) {
1664 ND_PRINT((ndo, ", " ));
1665 }
1666 switch(af<<8 | safi) {
1667 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1668 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1669 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1670 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1671 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
1672 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN):
1673 case (AFNUM_INET<<8 | SAFNUM_MDT):
1674 if (tlen < (int)sizeof(struct in_addr)) {
1675 ND_PRINT((ndo, "invalid len"));
1676 tlen = 0;
1677 } else {
1678 ND_TCHECK2(tptr[0], sizeof(struct in_addr));
1679 ND_PRINT((ndo, "%s",ipaddr_string(ndo, tptr)));
1680 tlen -= sizeof(struct in_addr);
1681 tptr += sizeof(struct in_addr);
1682 }
1683 break;
1684 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1685 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1686 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1687 if (tlen < (int)(sizeof(struct in_addr)+BGP_VPN_RD_LEN)) {
1688 ND_PRINT((ndo, "invalid len"));
1689 tlen = 0;
1690 } else {
1691 ND_TCHECK2(tptr[0], sizeof(struct in_addr)+BGP_VPN_RD_LEN);
1692 ND_PRINT((ndo, "RD: %s, %s",
1693 bgp_vpn_rd_print(ndo, tptr),
1694 ipaddr_string(ndo, tptr+BGP_VPN_RD_LEN)));
1695 tlen -= (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
1696 tptr += (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
1697 }
1698 break;
1699 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1700 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1701 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1702 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1703 if (tlen < (int)sizeof(struct in6_addr)) {
1704 ND_PRINT((ndo, "invalid len"));
1705 tlen = 0;
1706 } else {
1707 ND_TCHECK2(tptr[0], sizeof(struct in6_addr));
1708 ND_PRINT((ndo, "%s", ip6addr_string(ndo, tptr)));
1709 tlen -= sizeof(struct in6_addr);
1710 tptr += sizeof(struct in6_addr);
1711 }
1712 break;
1713 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1714 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1715 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1716 if (tlen < (int)(sizeof(struct in6_addr)+BGP_VPN_RD_LEN)) {
1717 ND_PRINT((ndo, "invalid len"));
1718 tlen = 0;
1719 } else {
1720 ND_TCHECK2(tptr[0], sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
1721 ND_PRINT((ndo, "RD: %s, %s",
1722 bgp_vpn_rd_print(ndo, tptr),
1723 ip6addr_string(ndo, tptr+BGP_VPN_RD_LEN)));
1724 tlen -= (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
1725 tptr += (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
1726 }
1727 break;
1728 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1729 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1730 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1731 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1732 if (tlen < (int)sizeof(struct in_addr)) {
1733 ND_PRINT((ndo, "invalid len"));
1734 tlen = 0;
1735 } else {
1736 ND_TCHECK2(tptr[0], sizeof(struct in_addr));
1737 ND_PRINT((ndo, "%s", ipaddr_string(ndo, tptr)));
1738 tlen -= (sizeof(struct in_addr));
1739 tptr += (sizeof(struct in_addr));
1740 }
1741 break;
1742 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1743 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1744 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1745 ND_TCHECK2(tptr[0], tlen);
1746 ND_PRINT((ndo, "%s", isonsap_string(ndo, tptr, tlen)));
1747 tptr += tlen;
1748 tlen = 0;
1749 break;
1750
1751 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1752 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1753 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1754 if (tlen < BGP_VPN_RD_LEN+1) {
1755 ND_PRINT((ndo, "invalid len"));
1756 tlen = 0;
1757 } else {
1758 ND_TCHECK2(tptr[0], tlen);
1759 ND_PRINT((ndo, "RD: %s, %s",
1760 bgp_vpn_rd_print(ndo, tptr),
1761 isonsap_string(ndo, tptr+BGP_VPN_RD_LEN,tlen-BGP_VPN_RD_LEN)));
1762 /* rfc986 mapped IPv4 address ? */
1763 if (EXTRACT_32BITS(tptr+BGP_VPN_RD_LEN) == 0x47000601)
1764 ND_PRINT((ndo, " = %s", ipaddr_string(ndo, tptr+BGP_VPN_RD_LEN+4)));
1765 /* rfc1888 mapped IPv6 address ? */
1766 else if (EXTRACT_24BITS(tptr+BGP_VPN_RD_LEN) == 0x350000)
1767 ND_PRINT((ndo, " = %s", ip6addr_string(ndo, tptr+BGP_VPN_RD_LEN+3)));
1768 tptr += tlen;
1769 tlen = 0;
1770 }
1771 break;
1772 default:
1773 ND_TCHECK2(tptr[0], tlen);
1774 ND_PRINT((ndo, "no AFI %u/SAFI %u decoder", af, safi));
1775 if (ndo->ndo_vflag <= 1)
1776 print_unknown_data(ndo, tptr, "\n\t ", tlen);
1777 tptr += tlen;
1778 tlen = 0;
1779 goto done;
1780 break;
1781 }
1782 }
1783 }
1784 ND_PRINT((ndo, ", nh-length: %u", nhlen));
1785 tptr += tlen;
1786
1787 ND_TCHECK(tptr[0]);
1788 snpa = tptr[0];
1789 tptr++;
1790
1791 if (snpa) {
1792 ND_PRINT((ndo, "\n\t %u SNPA", snpa));
1793 for (/*nothing*/; snpa > 0; snpa--) {
1794 ND_TCHECK(tptr[0]);
1795 ND_PRINT((ndo, "\n\t %d bytes", tptr[0]));
1796 tptr += tptr[0] + 1;
1797 }
1798 } else {
1799 ND_PRINT((ndo, ", no SNPA"));
1800 }
1801
1802 add_path4 = check_add_path(tptr, (len-(tptr - pptr)), 32);
1803 add_path6 = check_add_path(tptr, (len-(tptr - pptr)), 128);
1804
1805 while (tptr < pptr + len) {
1806 switch (af<<8 | safi) {
1807 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1808 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1809 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1810 if (add_path4) {
1811 path_id = EXTRACT_32BITS(tptr);
1812 tptr += 4;
1813 }
1814 advance = decode_prefix4(ndo, tptr, len, buf, sizeof(buf));
1815 if (advance == -1)
1816 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1817 else if (advance == -2)
1818 goto trunc;
1819 else if (advance == -3)
1820 break; /* bytes left, but not enough */
1821 else
1822 ND_PRINT((ndo, "\n\t %s", buf));
1823 if (add_path4) {
1824 ND_PRINT((ndo, " Path Id: %d", path_id));
1825 }
1826 break;
1827 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1828 advance = decode_labeled_prefix4(ndo, tptr, len, buf, sizeof(buf));
1829 if (advance == -1)
1830 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1831 else if (advance == -2)
1832 goto trunc;
1833 else if (advance == -3)
1834 break; /* bytes left, but not enough */
1835 else
1836 ND_PRINT((ndo, "\n\t %s", buf));
1837 break;
1838 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1839 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1840 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1841 advance = decode_labeled_vpn_prefix4(ndo, tptr, buf, sizeof(buf));
1842 if (advance == -1)
1843 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1844 else if (advance == -2)
1845 goto trunc;
1846 else
1847 ND_PRINT((ndo, "\n\t %s", buf));
1848 break;
1849 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
1850 advance = decode_rt_routing_info(ndo, tptr, buf, sizeof(buf));
1851 if (advance == -1)
1852 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1853 else if (advance == -2)
1854 goto trunc;
1855 else
1856 ND_PRINT((ndo, "\n\t %s", buf));
1857 break;
1858 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): /* fall through */
1859 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST_VPN):
1860 advance = decode_multicast_vpn(ndo, tptr, buf, sizeof(buf));
1861 if (advance == -1)
1862 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1863 else if (advance == -2)
1864 goto trunc;
1865 else
1866 ND_PRINT((ndo, "\n\t %s", buf));
1867 break;
1868
1869 case (AFNUM_INET<<8 | SAFNUM_MDT):
1870 advance = decode_mdt_vpn_nlri(ndo, tptr, buf, sizeof(buf));
1871 if (advance == -1)
1872 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1873 else if (advance == -2)
1874 goto trunc;
1875 else
1876 ND_PRINT((ndo, "\n\t %s", buf));
1877 break;
1878 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1879 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1880 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1881 if (add_path6) {
1882 path_id = EXTRACT_32BITS(tptr);
1883 tptr += 4;
1884 }
1885 advance = decode_prefix6(ndo, tptr, len, buf, sizeof(buf));
1886 if (advance == -1)
1887 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1888 else if (advance == -2)
1889 goto trunc;
1890 else if (advance == -3)
1891 break; /* bytes left, but not enough */
1892 else
1893 ND_PRINT((ndo, "\n\t %s", buf));
1894 if (add_path6) {
1895 ND_PRINT((ndo, " Path Id: %d", path_id));
1896 }
1897 break;
1898 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1899 advance = decode_labeled_prefix6(ndo, tptr, len, buf, sizeof(buf));
1900 if (advance == -1)
1901 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1902 else if (advance == -2)
1903 goto trunc;
1904 else if (advance == -3)
1905 break; /* bytes left, but not enough */
1906 else
1907 ND_PRINT((ndo, "\n\t %s", buf));
1908 break;
1909 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1910 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1911 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1912 advance = decode_labeled_vpn_prefix6(ndo, tptr, buf, sizeof(buf));
1913 if (advance == -1)
1914 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1915 else if (advance == -2)
1916 goto trunc;
1917 else
1918 ND_PRINT((ndo, "\n\t %s", buf));
1919 break;
1920 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1921 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1922 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1923 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1924 advance = decode_labeled_vpn_l2(ndo, tptr, buf, sizeof(buf));
1925 if (advance == -1)
1926 ND_PRINT((ndo, "\n\t (illegal length)"));
1927 else if (advance == -2)
1928 goto trunc;
1929 else
1930 ND_PRINT((ndo, "\n\t %s", buf));
1931 break;
1932 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1933 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1934 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1935 advance = decode_clnp_prefix(ndo, tptr, buf, sizeof(buf));
1936 if (advance == -1)
1937 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1938 else if (advance == -2)
1939 goto trunc;
1940 else
1941 ND_PRINT((ndo, "\n\t %s", buf));
1942 break;
1943 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1944 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1945 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1946 advance = decode_labeled_vpn_clnp_prefix(ndo, tptr, buf, sizeof(buf));
1947 if (advance == -1)
1948 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
1949 else if (advance == -2)
1950 goto trunc;
1951 else
1952 ND_PRINT((ndo, "\n\t %s", buf));
1953 break;
1954 default:
1955 ND_TCHECK2(*tptr,tlen);
1956 ND_PRINT((ndo, "\n\t no AFI %u / SAFI %u decoder", af, safi));
1957 if (ndo->ndo_vflag <= 1)
1958 print_unknown_data(ndo, tptr, "\n\t ", tlen);
1959 advance = 0;
1960 tptr = pptr + len;
1961 break;
1962 }
1963 if (advance < 0)
1964 break;
1965 tptr += advance;
1966 }
1967 done:
1968 break;
1969
1970 case BGPTYPE_MP_UNREACH_NLRI:
1971 ND_TCHECK2(tptr[0], BGP_MP_NLRI_MINSIZE);
1972 af = EXTRACT_16BITS(tptr);
1973 safi = tptr[2];
1974
1975 ND_PRINT((ndo, "\n\t AFI: %s (%u), %sSAFI: %s (%u)",
1976 tok2str(af_values, "Unknown AFI", af),
1977 af,
1978 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1979 tok2str(bgp_safi_values, "Unknown SAFI", safi),
1980 safi));
1981
1982 if (len == BGP_MP_NLRI_MINSIZE)
1983 ND_PRINT((ndo, "\n\t End-of-Rib Marker (empty NLRI)"));
1984
1985 tptr += 3;
1986
1987 add_path4 = check_add_path(tptr, (len-(tptr - pptr)), 32);
1988 add_path6 = check_add_path(tptr, (len-(tptr - pptr)), 128);
1989
1990 while (tptr < pptr + len) {
1991 switch (af<<8 | safi) {
1992 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1993 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1994 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1995 if (add_path4) {
1996 path_id = EXTRACT_32BITS(tptr);
1997 tptr += 4;
1998 }
1999 advance = decode_prefix4(ndo, tptr, len, buf, sizeof(buf));
2000 if (advance == -1)
2001 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2002 else if (advance == -2)
2003 goto trunc;
2004 else if (advance == -3)
2005 break; /* bytes left, but not enough */
2006 else
2007 ND_PRINT((ndo, "\n\t %s", buf));
2008 if (add_path4) {
2009 ND_PRINT((ndo, " Path Id: %d", path_id));
2010 }
2011 break;
2012 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
2013 advance = decode_labeled_prefix4(ndo, tptr, len, buf, sizeof(buf));
2014 if (advance == -1)
2015 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2016 else if (advance == -2)
2017 goto trunc;
2018 else if (advance == -3)
2019 break; /* bytes left, but not enough */
2020 else
2021 ND_PRINT((ndo, "\n\t %s", buf));
2022 break;
2023 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
2024 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
2025 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
2026 advance = decode_labeled_vpn_prefix4(ndo, tptr, buf, sizeof(buf));
2027 if (advance == -1)
2028 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2029 else if (advance == -2)
2030 goto trunc;
2031 else
2032 ND_PRINT((ndo, "\n\t %s", buf));
2033 break;
2034 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
2035 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
2036 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
2037 if (add_path6) {
2038 path_id = EXTRACT_32BITS(tptr);
2039 tptr += 4;
2040 }
2041 advance = decode_prefix6(ndo, tptr, len, buf, sizeof(buf));
2042 if (advance == -1)
2043 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2044 else if (advance == -2)
2045 goto trunc;
2046 else if (advance == -3)
2047 break; /* bytes left, but not enough */
2048 else
2049 ND_PRINT((ndo, "\n\t %s", buf));
2050 if (add_path6) {
2051 ND_PRINT((ndo, " Path Id: %d", path_id));
2052 }
2053 break;
2054 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
2055 advance = decode_labeled_prefix6(ndo, tptr, len, buf, sizeof(buf));
2056 if (advance == -1)
2057 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2058 else if (advance == -2)
2059 goto trunc;
2060 else if (advance == -3)
2061 break; /* bytes left, but not enough */
2062 else
2063 ND_PRINT((ndo, "\n\t %s", buf));
2064 break;
2065 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
2066 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
2067 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
2068 advance = decode_labeled_vpn_prefix6(ndo, tptr, buf, sizeof(buf));
2069 if (advance == -1)
2070 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2071 else if (advance == -2)
2072 goto trunc;
2073 else
2074 ND_PRINT((ndo, "\n\t %s", buf));
2075 break;
2076 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
2077 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
2078 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
2079 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
2080 advance = decode_labeled_vpn_l2(ndo, tptr, buf, sizeof(buf));
2081 if (advance == -1)
2082 ND_PRINT((ndo, "\n\t (illegal length)"));
2083 else if (advance == -2)
2084 goto trunc;
2085 else
2086 ND_PRINT((ndo, "\n\t %s", buf));
2087 break;
2088 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
2089 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
2090 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
2091 advance = decode_clnp_prefix(ndo, tptr, buf, sizeof(buf));
2092 if (advance == -1)
2093 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2094 else if (advance == -2)
2095 goto trunc;
2096 else
2097 ND_PRINT((ndo, "\n\t %s", buf));
2098 break;
2099 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
2100 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
2101 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
2102 advance = decode_labeled_vpn_clnp_prefix(ndo, tptr, buf, sizeof(buf));
2103 if (advance == -1)
2104 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2105 else if (advance == -2)
2106 goto trunc;
2107 else
2108 ND_PRINT((ndo, "\n\t %s", buf));
2109 break;
2110 case (AFNUM_INET<<8 | SAFNUM_MDT):
2111 advance = decode_mdt_vpn_nlri(ndo, tptr, buf, sizeof(buf));
2112 if (advance == -1)
2113 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2114 else if (advance == -2)
2115 goto trunc;
2116 else
2117 ND_PRINT((ndo, "\n\t %s", buf));
2118 break;
2119 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): /* fall through */
2120 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST_VPN):
2121 advance = decode_multicast_vpn(ndo, tptr, buf, sizeof(buf));
2122 if (advance == -1)
2123 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2124 else if (advance == -2)
2125 goto trunc;
2126 else
2127 ND_PRINT((ndo, "\n\t %s", buf));
2128 break;
2129 default:
2130 ND_TCHECK2(*(tptr-3),tlen);
2131 ND_PRINT((ndo, "no AFI %u / SAFI %u decoder", af, safi));
2132 if (ndo->ndo_vflag <= 1)
2133 print_unknown_data(ndo, tptr-3, "\n\t ", tlen);
2134 advance = 0;
2135 tptr = pptr + len;
2136 break;
2137 }
2138 if (advance < 0)
2139 break;
2140 tptr += advance;
2141 }
2142 break;
2143 case BGPTYPE_EXTD_COMMUNITIES:
2144 if (len % 8) {
2145 ND_PRINT((ndo, "invalid len"));
2146 break;
2147 }
2148 while (tlen>0) {
2149 uint16_t extd_comm;
2150
2151 ND_TCHECK2(tptr[0], 2);
2152 extd_comm=EXTRACT_16BITS(tptr);
2153
2154 ND_PRINT((ndo, "\n\t %s (0x%04x), Flags [%s]",
2155 tok2str(bgp_extd_comm_subtype_values,
2156 "unknown extd community typecode",
2157 extd_comm),
2158 extd_comm,
2159 bittok2str(bgp_extd_comm_flag_values, "none", extd_comm)));
2160
2161 ND_TCHECK2(*(tptr+2), 6);
2162 switch(extd_comm) {
2163 case BGP_EXT_COM_RT_0:
2164 case BGP_EXT_COM_RO_0:
2165 case BGP_EXT_COM_L2VPN_RT_0:
2166 ND_PRINT((ndo, ": %u:%u (= %s)",
2167 EXTRACT_16BITS(tptr+2),
2168 EXTRACT_32BITS(tptr+4),
2169 ipaddr_string(ndo, tptr+4)));
2170 break;
2171 case BGP_EXT_COM_RT_1:
2172 case BGP_EXT_COM_RO_1:
2173 case BGP_EXT_COM_L2VPN_RT_1:
2174 case BGP_EXT_COM_VRF_RT_IMP:
2175 ND_PRINT((ndo, ": %s:%u",
2176 ipaddr_string(ndo, tptr+2),
2177 EXTRACT_16BITS(tptr+6)));
2178 break;
2179 case BGP_EXT_COM_RT_2:
2180 case BGP_EXT_COM_RO_2:
2181 ND_PRINT((ndo, ": %s:%u",
2182 as_printf(ndo, astostr, sizeof(astostr),
2183 EXTRACT_32BITS(tptr+2)), EXTRACT_16BITS(tptr+6)));
2184 break;
2185 case BGP_EXT_COM_LINKBAND:
2186 bw.i = EXTRACT_32BITS(tptr+2);
2187 ND_PRINT((ndo, ": bandwidth: %.3f Mbps",
2188 bw.f*8/1000000));
2189 break;
2190 case BGP_EXT_COM_VPN_ORIGIN:
2191 case BGP_EXT_COM_VPN_ORIGIN2:
2192 case BGP_EXT_COM_VPN_ORIGIN3:
2193 case BGP_EXT_COM_VPN_ORIGIN4:
2194 case BGP_EXT_COM_OSPF_RID:
2195 case BGP_EXT_COM_OSPF_RID2:
2196 ND_PRINT((ndo, "%s", ipaddr_string(ndo, tptr+2)));
2197 break;
2198 case BGP_EXT_COM_OSPF_RTYPE:
2199 case BGP_EXT_COM_OSPF_RTYPE2:
2200 ND_PRINT((ndo, ": area:%s, router-type:%s, metric-type:%s%s",
2201 ipaddr_string(ndo, tptr+2),
2202 tok2str(bgp_extd_comm_ospf_rtype_values,
2203 "unknown (0x%02x)",
2204 *(tptr+6)),
2205 (*(tptr+7) & BGP_OSPF_RTYPE_METRIC_TYPE) ? "E2" : "",
2206 ((*(tptr+6) == BGP_OSPF_RTYPE_EXT) || (*(tptr+6) == BGP_OSPF_RTYPE_NSSA)) ? "E1" : ""));
2207 break;
2208 case BGP_EXT_COM_L2INFO:
2209 ND_PRINT((ndo, ": %s Control Flags [0x%02x]:MTU %u",
2210 tok2str(l2vpn_encaps_values,
2211 "unknown encaps",
2212 *(tptr+2)),
2213 *(tptr+3),
2214 EXTRACT_16BITS(tptr+4)));
2215 break;
2216 case BGP_EXT_COM_SOURCE_AS:
2217 ND_PRINT((ndo, ": AS %u", EXTRACT_16BITS(tptr+2)));
2218 break;
2219 default:
2220 ND_TCHECK2(*tptr,8);
2221 print_unknown_data(ndo, tptr, "\n\t ", 8);
2222 break;
2223 }
2224 tlen -=8;
2225 tptr +=8;
2226 }
2227 break;
2228
2229 case BGPTYPE_PMSI_TUNNEL:
2230 {
2231 uint8_t tunnel_type, flags;
2232
2233 ND_TCHECK2(tptr[0], 5);
2234 tunnel_type = *(tptr+1);
2235 flags = *tptr;
2236 tlen = len;
2237
2238 ND_PRINT((ndo, "\n\t Tunnel-type %s (%u), Flags [%s], MPLS Label %u",
2239 tok2str(bgp_pmsi_tunnel_values, "Unknown", tunnel_type),
2240 tunnel_type,
2241 bittok2str(bgp_pmsi_flag_values, "none", flags),
2242 EXTRACT_24BITS(tptr+2)>>4));
2243
2244 tptr +=5;
2245 tlen -= 5;
2246
2247 switch (tunnel_type) {
2248 case BGP_PMSI_TUNNEL_PIM_SM: /* fall through */
2249 case BGP_PMSI_TUNNEL_PIM_BIDIR:
2250 ND_TCHECK2(tptr[0], 8);
2251 ND_PRINT((ndo, "\n\t Sender %s, P-Group %s",
2252 ipaddr_string(ndo, tptr),
2253 ipaddr_string(ndo, tptr+4)));
2254 break;
2255
2256 case BGP_PMSI_TUNNEL_PIM_SSM:
2257 ND_TCHECK2(tptr[0], 8);
2258 ND_PRINT((ndo, "\n\t Root-Node %s, P-Group %s",
2259 ipaddr_string(ndo, tptr),
2260 ipaddr_string(ndo, tptr+4)));
2261 break;
2262 case BGP_PMSI_TUNNEL_INGRESS:
2263 ND_TCHECK2(tptr[0], 4);
2264 ND_PRINT((ndo, "\n\t Tunnel-Endpoint %s",
2265 ipaddr_string(ndo, tptr)));
2266 break;
2267 case BGP_PMSI_TUNNEL_LDP_P2MP: /* fall through */
2268 case BGP_PMSI_TUNNEL_LDP_MP2MP:
2269 ND_TCHECK2(tptr[0], 8);
2270 ND_PRINT((ndo, "\n\t Root-Node %s, LSP-ID 0x%08x",
2271 ipaddr_string(ndo, tptr),
2272 EXTRACT_32BITS(tptr+4)));
2273 break;
2274 case BGP_PMSI_TUNNEL_RSVP_P2MP:
2275 ND_TCHECK2(tptr[0], 8);
2276 ND_PRINT((ndo, "\n\t Extended-Tunnel-ID %s, P2MP-ID 0x%08x",
2277 ipaddr_string(ndo, tptr),
2278 EXTRACT_32BITS(tptr+4)));
2279 break;
2280 default:
2281 if (ndo->ndo_vflag <= 1) {
2282 print_unknown_data(ndo, tptr, "\n\t ", tlen);
2283 }
2284 }
2285 break;
2286 }
2287 case BGPTYPE_AIGP:
2288 {
2289 uint8_t type;
2290 uint16_t length;
2291
2292 tlen = len;
2293
2294 while (tlen >= 3) {
2295
2296 ND_TCHECK2(tptr[0], 3);
2297
2298 type = *tptr;
2299 length = EXTRACT_16BITS(tptr+1);
2300 tptr += 3;
2301 tlen -= 3;
2302
2303 ND_PRINT((ndo, "\n\t %s TLV (%u), length %u",
2304 tok2str(bgp_aigp_values, "Unknown", type),
2305 type, length));
2306
2307 if (length < 3)
2308 goto trunc;
2309 length -= 3;
2310
2311 /*
2312 * Check if we can read the TLV data.
2313 */
2314 ND_TCHECK2(tptr[3], length);
2315
2316 switch (type) {
2317
2318 case BGP_AIGP_TLV:
2319 if (length < 8)
2320 goto trunc;
2321 ND_PRINT((ndo, ", metric %" PRIu64,
2322 EXTRACT_64BITS(tptr)));
2323 break;
2324
2325 default:
2326 if (ndo->ndo_vflag <= 1) {
2327 print_unknown_data(ndo, tptr,"\n\t ", length);
2328 }
2329 }
2330
2331 tptr += length;
2332 tlen -= length;
2333 }
2334 break;
2335 }
2336 case BGPTYPE_ATTR_SET:
2337 ND_TCHECK2(tptr[0], 4);
2338 if (len < 4)
2339 goto trunc;
2340 ND_PRINT((ndo, "\n\t Origin AS: %s",
2341 as_printf(ndo, astostr, sizeof(astostr), EXTRACT_32BITS(tptr))));
2342 tptr+=4;
2343 len -=4;
2344
2345 while (len) {
2346 u_int aflags, alenlen, alen;
2347
2348 ND_TCHECK2(tptr[0], 2);
2349 if (len < 2)
2350 goto trunc;
2351 aflags = *tptr;
2352 atype = *(tptr + 1);
2353 tptr += 2;
2354 len -= 2;
2355 alenlen = bgp_attr_lenlen(aflags, tptr);
2356 ND_TCHECK2(tptr[0], alenlen);
2357 if (len < alenlen)
2358 goto trunc;
2359 alen = bgp_attr_len(aflags, tptr);
2360 tptr += alenlen;
2361 len -= alenlen;
2362
2363 ND_PRINT((ndo, "\n\t %s (%u), length: %u",
2364 tok2str(bgp_attr_values,
2365 "Unknown Attribute", atype),
2366 atype,
2367 alen));
2368
2369 if (aflags) {
2370 ND_PRINT((ndo, ", Flags [%s%s%s%s",
2371 aflags & 0x80 ? "O" : "",
2372 aflags & 0x40 ? "T" : "",
2373 aflags & 0x20 ? "P" : "",
2374 aflags & 0x10 ? "E" : ""));
2375 if (aflags & 0xf)
2376 ND_PRINT((ndo, "+%x", aflags & 0xf));
2377 ND_PRINT((ndo, "]: "));
2378 }
2379 /* FIXME check for recursion */
2380 if (!bgp_attr_print(ndo, atype, tptr, alen))
2381 return 0;
2382 tptr += alen;
2383 len -= alen;
2384 }
2385 break;
2386
2387 case BGPTYPE_LARGE_COMMUNITY:
2388 if (len == 0 || len % 12) {
2389 ND_PRINT((ndo, "invalid len"));
2390 break;
2391 }
2392 ND_PRINT((ndo, "\n\t "));
2393 while (len > 0) {
2394 ND_TCHECK2(*tptr, 12);
2395 ND_PRINT((ndo, "%u:%u:%u%s",
2396 EXTRACT_32BITS(tptr),
2397 EXTRACT_32BITS(tptr + 4),
2398 EXTRACT_32BITS(tptr + 8),
2399 (len > 12) ? ", " : ""));
2400 tptr += 12;
2401 len -= 12;
2402 }
2403 break;
2404 default:
2405 ND_TCHECK2(*pptr,len);
2406 ND_PRINT((ndo, "\n\t no Attribute %u decoder", atype)); /* we have no decoder for the attribute */
2407 if (ndo->ndo_vflag <= 1)
2408 print_unknown_data(ndo, pptr, "\n\t ", len);
2409 break;
2410 }
2411 if (ndo->ndo_vflag > 1 && len) { /* omit zero length attributes*/
2412 ND_TCHECK2(*pptr,len);
2413 print_unknown_data(ndo, pptr, "\n\t ", len);
2414 }
2415 return 1;
2416
2417 trunc:
2418 return 0;
2419 }
2420
2421 static void
2422 bgp_capabilities_print(netdissect_options *ndo,
2423 const u_char *opt, int caps_len)
2424 {
2425 int cap_type, cap_len, tcap_len, cap_offset;
2426 int i = 0;
2427
2428 while (i < caps_len) {
2429 ND_TCHECK2(opt[i], BGP_CAP_HEADER_SIZE);
2430 cap_type=opt[i];
2431 cap_len=opt[i+1];
2432 tcap_len=cap_len;
2433 ND_PRINT((ndo, "\n\t %s (%u), length: %u",
2434 tok2str(bgp_capcode_values, "Unknown",
2435 cap_type),
2436 cap_type,
2437 cap_len));
2438 ND_TCHECK2(opt[i+2], cap_len);
2439 switch (cap_type) {
2440 case BGP_CAPCODE_MP:
2441 ND_PRINT((ndo, "\n\t\tAFI %s (%u), SAFI %s (%u)",
2442 tok2str(af_values, "Unknown",
2443 EXTRACT_16BITS(opt+i+2)),
2444 EXTRACT_16BITS(opt+i+2),
2445 tok2str(bgp_safi_values, "Unknown",
2446 opt[i+5]),
2447 opt[i+5]));
2448 break;
2449 case BGP_CAPCODE_RESTART:
2450 ND_PRINT((ndo, "\n\t\tRestart Flags: [%s], Restart Time %us",
2451 ((opt[i+2])&0x80) ? "R" : "none",
2452 EXTRACT_16BITS(opt+i+2)&0xfff));
2453 tcap_len-=2;
2454 cap_offset=4;
2455 while(tcap_len>=4) {
2456 ND_PRINT((ndo, "\n\t\t AFI %s (%u), SAFI %s (%u), Forwarding state preserved: %s",
2457 tok2str(af_values,"Unknown",
2458 EXTRACT_16BITS(opt+i+cap_offset)),
2459 EXTRACT_16BITS(opt+i+cap_offset),
2460 tok2str(bgp_safi_values,"Unknown",
2461 opt[i+cap_offset+2]),
2462 opt[i+cap_offset+2],
2463 ((opt[i+cap_offset+3])&0x80) ? "yes" : "no" ));
2464 tcap_len-=4;
2465 cap_offset+=4;
2466 }
2467 break;
2468 case BGP_CAPCODE_RR:
2469 case BGP_CAPCODE_RR_CISCO:
2470 break;
2471 case BGP_CAPCODE_AS_NEW:
2472
2473 /*
2474 * Extract the 4 byte AS number encoded.
2475 */
2476 if (cap_len == 4) {
2477 ND_PRINT((ndo, "\n\t\t 4 Byte AS %s",
2478 as_printf(ndo, astostr, sizeof(astostr),
2479 EXTRACT_32BITS(opt + i + 2))));
2480 }
2481 break;
2482 case BGP_CAPCODE_ADD_PATH:
2483 cap_offset=2;
2484 if (tcap_len == 0) {
2485 ND_PRINT((ndo, " (bogus)")); /* length */
2486 break;
2487 }
2488 while (tcap_len > 0) {
2489 if (tcap_len < 4) {
2490 ND_PRINT((ndo, "\n\t\t(invalid)"));
2491 break;
2492 }
2493 ND_PRINT((ndo, "\n\t\tAFI %s (%u), SAFI %s (%u), Send/Receive: %s",
2494 tok2str(af_values,"Unknown",EXTRACT_16BITS(opt+i+cap_offset)),
2495 EXTRACT_16BITS(opt+i+cap_offset),
2496 tok2str(bgp_safi_values,"Unknown",opt[i+cap_offset+2]),
2497 opt[i+cap_offset+2],
2498 tok2str(bgp_add_path_recvsend,"Bogus (0x%02x)",opt[i+cap_offset+3])
2499 ));
2500 tcap_len-=4;
2501 cap_offset+=4;
2502 }
2503 break;
2504 default:
2505 ND_PRINT((ndo, "\n\t\tno decoder for Capability %u",
2506 cap_type));
2507 if (ndo->ndo_vflag <= 1)
2508 print_unknown_data(ndo, &opt[i+2], "\n\t\t", cap_len);
2509 break;
2510 }
2511 if (ndo->ndo_vflag > 1 && cap_len > 0) {
2512 print_unknown_data(ndo, &opt[i+2], "\n\t\t", cap_len);
2513 }
2514 i += BGP_CAP_HEADER_SIZE + cap_len;
2515 }
2516 return;
2517
2518 trunc:
2519 ND_PRINT((ndo, "[|BGP]"));
2520 }
2521
2522 static void
2523 bgp_open_print(netdissect_options *ndo,
2524 const u_char *dat, int length)
2525 {
2526 struct bgp_open bgpo;
2527 struct bgp_opt bgpopt;
2528 const u_char *opt;
2529 int i;
2530
2531 ND_TCHECK2(dat[0], BGP_OPEN_SIZE);
2532 memcpy(&bgpo, dat, BGP_OPEN_SIZE);
2533
2534 ND_PRINT((ndo, "\n\t Version %d, ", bgpo.bgpo_version));
2535 ND_PRINT((ndo, "my AS %s, ",
2536 as_printf(ndo, astostr, sizeof(astostr), ntohs(bgpo.bgpo_myas))));
2537 ND_PRINT((ndo, "Holdtime %us, ", ntohs(bgpo.bgpo_holdtime)));
2538 ND_PRINT((ndo, "ID %s", ipaddr_string(ndo, &bgpo.bgpo_id)));
2539 ND_PRINT((ndo, "\n\t Optional parameters, length: %u", bgpo.bgpo_optlen));
2540
2541 /* some little sanity checking */
2542 if (length < bgpo.bgpo_optlen+BGP_OPEN_SIZE)
2543 return;
2544
2545 /* ugly! */
2546 opt = &((const struct bgp_open *)dat)->bgpo_optlen;
2547 opt++;
2548
2549 i = 0;
2550 while (i < bgpo.bgpo_optlen) {
2551 ND_TCHECK2(opt[i], BGP_OPT_SIZE);
2552 memcpy(&bgpopt, &opt[i], BGP_OPT_SIZE);
2553 if (i + 2 + bgpopt.bgpopt_len > bgpo.bgpo_optlen) {
2554 ND_PRINT((ndo, "\n\t Option %d, length: %u", bgpopt.bgpopt_type, bgpopt.bgpopt_len));
2555 break;
2556 }
2557
2558 ND_PRINT((ndo, "\n\t Option %s (%u), length: %u",
2559 tok2str(bgp_opt_values,"Unknown",
2560 bgpopt.bgpopt_type),
2561 bgpopt.bgpopt_type,
2562 bgpopt.bgpopt_len));
2563
2564 /* now let's decode the options we know*/
2565 switch(bgpopt.bgpopt_type) {
2566
2567 case BGP_OPT_CAP:
2568 bgp_capabilities_print(ndo, &opt[i+BGP_OPT_SIZE],
2569 bgpopt.bgpopt_len);
2570 break;
2571
2572 case BGP_OPT_AUTH:
2573 default:
2574 ND_PRINT((ndo, "\n\t no decoder for option %u",
2575 bgpopt.bgpopt_type));
2576 break;
2577 }
2578 i += BGP_OPT_SIZE + bgpopt.bgpopt_len;
2579 }
2580 return;
2581 trunc:
2582 ND_PRINT((ndo, "[|BGP]"));
2583 }
2584
2585 static void
2586 bgp_update_print(netdissect_options *ndo,
2587 const u_char *dat, int length)
2588 {
2589 struct bgp bgp;
2590 const u_char *p;
2591 int withdrawn_routes_len;
2592 char buf[MAXHOSTNAMELEN + 100];
2593 int wpfx;
2594 int len;
2595 int i;
2596
2597 ND_TCHECK2(dat[0], BGP_SIZE);
2598 if (length < BGP_SIZE)
2599 goto trunc;
2600 memcpy(&bgp, dat, BGP_SIZE);
2601 p = dat + BGP_SIZE; /*XXX*/
2602 length -= BGP_SIZE;
2603
2604 /* Unfeasible routes */
2605 ND_TCHECK2(p[0], 2);
2606 if (length < 2)
2607 goto trunc;
2608 withdrawn_routes_len = EXTRACT_16BITS(p);
2609 p += 2;
2610 length -= 2;
2611 if (withdrawn_routes_len) {
2612 ND_TCHECK2(p[0], withdrawn_routes_len);
2613 if (length < withdrawn_routes_len)
2614 goto trunc;
2615 if (withdrawn_routes_len < 2)
2616 goto trunc;
2617
2618 ND_PRINT((ndo, "\n\t Withdrawn routes:"));
2619 int add_path, path_id;
2620 add_path = check_add_path(p, withdrawn_routes_len, 32);
2621 while(withdrawn_routes_len > 0) {
2622 if (add_path) {
2623 path_id = EXTRACT_32BITS(p);
2624 p += 4;
2625 length -= 4;
2626 withdrawn_routes_len -= 4;
2627 }
2628 wpfx = decode_prefix4(ndo, p, withdrawn_routes_len, buf, sizeof(buf));
2629 if (wpfx == -1) {
2630 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2631 break;
2632 } else if (wpfx == -2)
2633 goto trunc;
2634 else if (wpfx == -3)
2635 goto trunc; /* bytes left, but not enough */
2636 else {
2637 ND_PRINT((ndo, "\n\t %s", buf));
2638 if (add_path) {
2639 ND_PRINT((ndo, " Path Id: %d", path_id));
2640 }
2641 p += wpfx;
2642 length -= wpfx;
2643 withdrawn_routes_len -= wpfx;
2644 }
2645 }
2646 }
2647
2648 ND_TCHECK2(p[0], 2);
2649 if (length < 2)
2650 goto trunc;
2651 len = EXTRACT_16BITS(p);
2652 p += 2;
2653 length -= 2;
2654
2655 if (withdrawn_routes_len == 0 && len == 0 && length == 0) {
2656 /* No withdrawn routes, no path attributes, no NLRI */
2657 ND_PRINT((ndo, "\n\t End-of-Rib Marker (empty NLRI)"));
2658 return;
2659 }
2660
2661 if (len) {
2662 /* do something more useful!*/
2663 while (len) {
2664 int aflags, atype, alenlen, alen;
2665
2666 ND_TCHECK2(p[0], 2);
2667 if (len < 2)
2668 goto trunc;
2669 if (length < 2)
2670 goto trunc;
2671 aflags = *p;
2672 atype = *(p + 1);
2673 p += 2;
2674 len -= 2;
2675 length -= 2;
2676 alenlen = bgp_attr_lenlen(aflags, p);
2677 ND_TCHECK2(p[0], alenlen);
2678 if (len < alenlen)
2679 goto trunc;
2680 if (length < alenlen)
2681 goto trunc;
2682 alen = bgp_attr_len(aflags, p);
2683 p += alenlen;
2684 len -= alenlen;
2685 length -= alenlen;
2686
2687 ND_PRINT((ndo, "\n\t %s (%u), length: %u",
2688 tok2str(bgp_attr_values, "Unknown Attribute",
2689 atype),
2690 atype,
2691 alen));
2692
2693 if (aflags) {
2694 ND_PRINT((ndo, ", Flags [%s%s%s%s",
2695 aflags & 0x80 ? "O" : "",
2696 aflags & 0x40 ? "T" : "",
2697 aflags & 0x20 ? "P" : "",
2698 aflags & 0x10 ? "E" : ""));
2699 if (aflags & 0xf)
2700 ND_PRINT((ndo, "+%x", aflags & 0xf));
2701 ND_PRINT((ndo, "]: "));
2702 }
2703 if (len < alen)
2704 goto trunc;
2705 if (length < alen)
2706 goto trunc;
2707 if (!bgp_attr_print(ndo, atype, p, alen))
2708 goto trunc;
2709 p += alen;
2710 len -= alen;
2711 length -= alen;
2712 }
2713 }
2714
2715 if (length) {
2716 int add_path = check_add_path(p, length, 32);
2717 int path_id;
2718 ND_PRINT((ndo, "\n\t Updated routes:"));
2719 while (length > 0) {
2720 if (add_path) {
2721 path_id = EXTRACT_32BITS(p);
2722 p += 4;
2723 length -= 4;
2724 }
2725 i = decode_prefix4(ndo, p, length, buf, sizeof(buf));
2726 if (i == -1) {
2727 ND_PRINT((ndo, "\n\t (illegal prefix length)"));
2728 break;
2729 } else if (i == -2)
2730 goto trunc;
2731 else if (i == -3)
2732 goto trunc; /* bytes left, but not enough */
2733 else {
2734 ND_PRINT((ndo, "\n\t %s", buf));
2735 if (add_path) {
2736 ND_PRINT((ndo, " Path Id: %d", path_id));
2737 }
2738 p += i;
2739 length -= i;
2740 }
2741 }
2742 }
2743 return;
2744 trunc:
2745 ND_PRINT((ndo, "[|BGP]"));
2746 }
2747
2748 static void
2749 bgp_notification_print(netdissect_options *ndo,
2750 const u_char *dat, int length)
2751 {
2752 struct bgp_notification bgpn;
2753 const u_char *tptr;
2754 uint8_t shutdown_comm_length;
2755 uint8_t remainder_offset;
2756
2757 ND_TCHECK2(dat[0], BGP_NOTIFICATION_SIZE);
2758 memcpy(&bgpn, dat, BGP_NOTIFICATION_SIZE);
2759
2760 /* some little sanity checking */
2761 if (length<BGP_NOTIFICATION_SIZE)
2762 return;
2763
2764 ND_PRINT((ndo, ", %s (%u)",
2765 tok2str(bgp_notify_major_values, "Unknown Error",
2766 bgpn.bgpn_major),
2767 bgpn.bgpn_major));
2768
2769 switch (bgpn.bgpn_major) {
2770
2771 case BGP_NOTIFY_MAJOR_MSG:
2772 ND_PRINT((ndo, ", subcode %s (%u)",
2773 tok2str(bgp_notify_minor_msg_values, "Unknown",
2774 bgpn.bgpn_minor),
2775 bgpn.bgpn_minor));
2776 break;
2777 case BGP_NOTIFY_MAJOR_OPEN:
2778 ND_PRINT((ndo, ", subcode %s (%u)",
2779 tok2str(bgp_notify_minor_open_values, "Unknown",
2780 bgpn.bgpn_minor),
2781 bgpn.bgpn_minor));
2782 break;
2783 case BGP_NOTIFY_MAJOR_UPDATE:
2784 ND_PRINT((ndo, ", subcode %s (%u)",
2785 tok2str(bgp_notify_minor_update_values, "Unknown",
2786 bgpn.bgpn_minor),
2787 bgpn.bgpn_minor));
2788 break;
2789 case BGP_NOTIFY_MAJOR_FSM:
2790 ND_PRINT((ndo, " subcode %s (%u)",
2791 tok2str(bgp_notify_minor_fsm_values, "Unknown",
2792 bgpn.bgpn_minor),
2793 bgpn.bgpn_minor));
2794 break;
2795 case BGP_NOTIFY_MAJOR_CAP:
2796 ND_PRINT((ndo, " subcode %s (%u)",
2797 tok2str(bgp_notify_minor_cap_values, "Unknown",
2798 bgpn.bgpn_minor),
2799 bgpn.bgpn_minor));
2800 break;
2801 case BGP_NOTIFY_MAJOR_CEASE:
2802 ND_PRINT((ndo, ", subcode %s (%u)",
2803 tok2str(bgp_notify_minor_cease_values, "Unknown",
2804 bgpn.bgpn_minor),
2805 bgpn.bgpn_minor));
2806
2807 /* draft-ietf-idr-cease-subcode-02 mentions optionally 7 bytes
2808 * for the maxprefix subtype, which may contain AFI, SAFI and MAXPREFIXES
2809 */
2810 if(bgpn.bgpn_minor == BGP_NOTIFY_MINOR_CEASE_MAXPRFX && length >= BGP_NOTIFICATION_SIZE + 7) {
2811 tptr = dat + BGP_NOTIFICATION_SIZE;
2812 ND_TCHECK2(*tptr, 7);
2813 ND_PRINT((ndo, ", AFI %s (%u), SAFI %s (%u), Max Prefixes: %u",
2814 tok2str(af_values, "Unknown",
2815 EXTRACT_16BITS(tptr)),
2816 EXTRACT_16BITS(tptr),
2817 tok2str(bgp_safi_values, "Unknown", *(tptr+2)),
2818 *(tptr+2),
2819 EXTRACT_32BITS(tptr+3)));
2820 }
2821 /*
2822 * draft-ietf-idr-shutdown describes a method to send a communication
2823 * intended for human consumption regarding the Administrative Shutdown
2824 */
2825 if ((bgpn.bgpn_minor == BGP_NOTIFY_MINOR_CEASE_SHUT ||
2826 bgpn.bgpn_minor == BGP_NOTIFY_MINOR_CEASE_RESET) &&
2827 length >= BGP_NOTIFICATION_SIZE + 1) {
2828 tptr = dat + BGP_NOTIFICATION_SIZE;
2829 ND_TCHECK2(*tptr, 1);
2830 shutdown_comm_length = *(tptr);
2831 remainder_offset = 0;
2832 /* garbage, hexdump it all */
2833 if (shutdown_comm_length > BGP_NOTIFY_MINOR_CEASE_ADMIN_SHUTDOWN_LEN ||
2834 shutdown_comm_length > length - (BGP_NOTIFICATION_SIZE + 1)) {
2835 ND_PRINT((ndo, ", invalid Shutdown Communication length"));
2836 }
2837 else if (shutdown_comm_length == 0) {
2838 ND_PRINT((ndo, ", empty Shutdown Communication"));
2839 remainder_offset += 1;
2840 }
2841 /* a proper shutdown communication */
2842 else {
2843 ND_TCHECK2(*(tptr+1), shutdown_comm_length);
2844 ND_PRINT((ndo, ", Shutdown Communication (length: %u): \"", shutdown_comm_length));
2845 (void)fn_printn(ndo, tptr+1, shutdown_comm_length, NULL);
2846 ND_PRINT((ndo, "\""));
2847 remainder_offset += shutdown_comm_length + 1;
2848 }
2849 /* if there is trailing data, hexdump it */
2850 if(length - (remainder_offset + BGP_NOTIFICATION_SIZE) > 0) {
2851 ND_PRINT((ndo, ", Data: (length: %u)", length - (remainder_offset + BGP_NOTIFICATION_SIZE)));
2852 hex_print(ndo, "\n\t\t", tptr + remainder_offset, length - (remainder_offset + BGP_NOTIFICATION_SIZE));
2853 }
2854 }
2855 break;
2856 default:
2857 break;
2858 }
2859
2860 return;
2861 trunc:
2862 ND_PRINT((ndo, "[|BGP]"));
2863 }
2864
2865 static void
2866 bgp_route_refresh_print(netdissect_options *ndo,
2867 const u_char *pptr, int len)
2868 {
2869 const struct bgp_route_refresh *bgp_route_refresh_header;
2870
2871 ND_TCHECK2(pptr[0], BGP_ROUTE_REFRESH_SIZE);
2872
2873 /* some little sanity checking */
2874 if (len<BGP_ROUTE_REFRESH_SIZE)
2875 return;
2876
2877 bgp_route_refresh_header = (const struct bgp_route_refresh *)pptr;
2878
2879 ND_PRINT((ndo, "\n\t AFI %s (%u), SAFI %s (%u)",
2880 tok2str(af_values,"Unknown",
2881 /* this stinks but the compiler pads the structure
2882 * weird */
2883 EXTRACT_16BITS(&bgp_route_refresh_header->afi)),
2884 EXTRACT_16BITS(&bgp_route_refresh_header->afi),
2885 tok2str(bgp_safi_values,"Unknown",
2886 bgp_route_refresh_header->safi),
2887 bgp_route_refresh_header->safi));
2888
2889 if (ndo->ndo_vflag > 1) {
2890 ND_TCHECK2(*pptr, len);
2891 print_unknown_data(ndo, pptr, "\n\t ", len);
2892 }
2893
2894 return;
2895 trunc:
2896 ND_PRINT((ndo, "[|BGP]"));
2897 }
2898
2899 static int
2900 bgp_header_print(netdissect_options *ndo,
2901 const u_char *dat, int length)
2902 {
2903 struct bgp bgp;
2904
2905 ND_TCHECK2(dat[0], BGP_SIZE);
2906 memcpy(&bgp, dat, BGP_SIZE);
2907 ND_PRINT((ndo, "\n\t%s Message (%u), length: %u",
2908 tok2str(bgp_msg_values, "Unknown", bgp.bgp_type),
2909 bgp.bgp_type,
2910 length));
2911
2912 switch (bgp.bgp_type) {
2913 case BGP_OPEN:
2914 bgp_open_print(ndo, dat, length);
2915 break;
2916 case BGP_UPDATE:
2917 bgp_update_print(ndo, dat, length);
2918 break;
2919 case BGP_NOTIFICATION:
2920 bgp_notification_print(ndo, dat, length);
2921 break;
2922 case BGP_KEEPALIVE:
2923 break;
2924 case BGP_ROUTE_REFRESH:
2925 bgp_route_refresh_print(ndo, dat, length);
2926 break;
2927 default:
2928 /* we have no decoder for the BGP message */
2929 ND_TCHECK2(*dat, length);
2930 ND_PRINT((ndo, "\n\t no Message %u decoder", bgp.bgp_type));
2931 print_unknown_data(ndo, dat, "\n\t ", length);
2932 break;
2933 }
2934 return 1;
2935 trunc:
2936 ND_PRINT((ndo, "[|BGP]"));
2937 return 0;
2938 }
2939
2940 void
2941 bgp_print(netdissect_options *ndo,
2942 const u_char *dat, int length)
2943 {
2944 const u_char *p;
2945 const u_char *ep;
2946 const u_char *start;
2947 const u_char marker[] = {
2948 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2949 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2950 };
2951 struct bgp bgp;
2952 uint16_t hlen;
2953
2954 ep = dat + length;
2955 if (ndo->ndo_snapend < dat + length)
2956 ep = ndo->ndo_snapend;
2957
2958 ND_PRINT((ndo, ": BGP"));
2959
2960 if (ndo->ndo_vflag < 1) /* lets be less chatty */
2961 return;
2962
2963 p = dat;
2964 start = p;
2965 while (p < ep) {
2966 if (!ND_TTEST2(p[0], 1))
2967 break;
2968 if (p[0] != 0xff) {
2969 p++;
2970 continue;
2971 }
2972
2973 if (!ND_TTEST2(p[0], sizeof(marker)))
2974 break;
2975 if (memcmp(p, marker, sizeof(marker)) != 0) {
2976 p++;
2977 continue;
2978 }
2979
2980 /* found BGP header */
2981 ND_TCHECK2(p[0], BGP_SIZE); /*XXX*/
2982 memcpy(&bgp, p, BGP_SIZE);
2983
2984 if (start != p)
2985 ND_PRINT((ndo, " [|BGP]"));
2986
2987 hlen = ntohs(bgp.bgp_len);
2988 if (hlen < BGP_SIZE) {
2989 ND_PRINT((ndo, "\n[|BGP Bogus header length %u < %u]", hlen,
2990 BGP_SIZE));
2991 break;
2992 }
2993
2994 if (ND_TTEST2(p[0], hlen)) {
2995 if (!bgp_header_print(ndo, p, hlen))
2996 return;
2997 p += hlen;
2998 start = p;
2999 } else {
3000 ND_PRINT((ndo, "\n[|BGP %s]",
3001 tok2str(bgp_msg_values,
3002 "Unknown Message Type",
3003 bgp.bgp_type)));
3004 break;
3005 }
3006 }
3007
3008 return;
3009
3010 trunc:
3011 ND_PRINT((ndo, " [|BGP]"));
3012 }
3013
3014 /*
3015 * Local Variables:
3016 * c-style: whitesmith
3017 * c-basic-offset: 4
3018 * End:
3019 */