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