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