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