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