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