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