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