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