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