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