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