xref: /netbsd-src/external/bsd/tcpdump/dist/print-bgp.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*
2  * Copyright (C) 1999 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * Extensively modified by Hannes Gredler (hannes@juniper.net) for more
30  * complete BGP support.
31  */
32 
33 #include <sys/cdefs.h>
34 #ifndef lint
35 __RCSID("$NetBSD: print-bgp.c,v 1.8 2017/02/05 04:05:05 spz 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 
765 	ND_TCHECK(pptr[0]);
766 	plen = pptr[0];   /* get prefix length */
767 
768 	if (0 == plen) {
769 		snprintf(buf, buflen, "default route target");
770 		return 1;
771 	}
772 
773 	if (32 > plen)
774 		return -1;
775 
776         plen-=32; /* adjust prefix length */
777 
778 	if (64 < plen)
779 		return -1;
780 
781 	memset(&route_target, 0, sizeof(route_target));
782 	ND_TCHECK2(pptr[1], (plen + 7) / 8);
783 	memcpy(&route_target, &pptr[1], (plen + 7) / 8);
784 	if (plen % 8) {
785 		((u_char *)&route_target)[(plen + 7) / 8 - 1] &=
786 			((0xff00 >> (plen % 8)) & 0xff);
787 	}
788 	snprintf(buf, buflen, "origin AS: %s, route target %s",
789 	    as_printf(ndo, astostr, sizeof(astostr), EXTRACT_32BITS(pptr+1)),
790 	    bgp_vpn_rd_print(ndo, (u_char *)&route_target));
791 
792 	return 5 + (plen + 7) / 8;
793 
794 trunc:
795 	return -2;
796 }
797 
798 static int
799 decode_labeled_vpn_prefix4(netdissect_options *ndo,
800                            const u_char *pptr, char *buf, u_int buflen)
801 {
802 	struct in_addr addr;
803 	u_int plen;
804 
805 	ND_TCHECK(pptr[0]);
806 	plen = pptr[0];   /* get prefix length */
807 
808 	if ((24+64) > plen)
809 		return -1;
810 
811         plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
812 
813 	if (32 < plen)
814 		return -1;
815 
816 	memset(&addr, 0, sizeof(addr));
817 	ND_TCHECK2(pptr[12], (plen + 7) / 8);
818 	memcpy(&addr, &pptr[12], (plen + 7) / 8);
819 	if (plen % 8) {
820 		((u_char *)&addr)[(plen + 7) / 8 - 1] &=
821 			((0xff00 >> (plen % 8)) & 0xff);
822 	}
823         /* the label may get offsetted by 4 bits so lets shift it right */
824 	snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
825                  bgp_vpn_rd_print(ndo, pptr+4),
826                  ipaddr_string(ndo, &addr),
827                  plen,
828                  EXTRACT_24BITS(pptr+1)>>4,
829                  ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
830 
831 	return 12 + (plen + 7) / 8;
832 
833 trunc:
834 	return -2;
835 }
836 
837 /*
838  * +-------------------------------+
839  * |                               |
840  * |  RD:IPv4-address (12 octets)  |
841  * |                               |
842  * +-------------------------------+
843  * |  MDT Group-address (4 octets) |
844  * +-------------------------------+
845  */
846 
847 #define MDT_VPN_NLRI_LEN 16
848 
849 static int
850 decode_mdt_vpn_nlri(netdissect_options *ndo,
851                     const u_char *pptr, char *buf, u_int buflen)
852 {
853 
854     const u_char *rd;
855     const u_char *vpn_ip;
856 
857     ND_TCHECK(pptr[0]);
858 
859     /* if the NLRI is not predefined length, quit.*/
860     if (*pptr != MDT_VPN_NLRI_LEN * 8)
861 	return -1;
862     pptr++;
863 
864     /* RD */
865     ND_TCHECK2(pptr[0], 8);
866     rd = pptr;
867     pptr+=8;
868 
869     /* IPv4 address */
870     ND_TCHECK2(pptr[0], sizeof(struct in_addr));
871     vpn_ip = pptr;
872     pptr+=sizeof(struct in_addr);
873 
874     /* MDT Group Address */
875     ND_TCHECK2(pptr[0], sizeof(struct in_addr));
876 
877     snprintf(buf, buflen, "RD: %s, VPN IP Address: %s, MC Group Address: %s",
878 	     bgp_vpn_rd_print(ndo, rd), ipaddr_string(ndo, vpn_ip), ipaddr_string(ndo, pptr));
879 
880     return MDT_VPN_NLRI_LEN + 1;
881 
882  trunc:
883 
884 return -2;
885 }
886 
887 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI   1
888 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI   2
889 #define BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI            3
890 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF 4
891 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE     5
892 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN  6
893 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN  7
894 
895 static const struct tok bgp_multicast_vpn_route_type_values[] = {
896     { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI, "Intra-AS I-PMSI"},
897     { BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI, "Inter-AS I-PMSI"},
898     { BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI, "S-PMSI"},
899     { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF, "Intra-AS Segment-Leaf"},
900     { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE, "Source-Active"},
901     { BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN, "Shared Tree Join"},
902     { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN, "Source Tree Join"},
903 };
904 
905 static int
906 decode_multicast_vpn(netdissect_options *ndo,
907                      const u_char *pptr, char *buf, u_int buflen)
908 {
909         uint8_t route_type, route_length, addr_length, sg_length;
910         u_int offset;
911 
912 	ND_TCHECK2(pptr[0], 2);
913         route_type = *pptr++;
914         route_length = *pptr++;
915 
916         snprintf(buf, buflen, "Route-Type: %s (%u), length: %u",
917                  tok2str(bgp_multicast_vpn_route_type_values,
918                          "Unknown", route_type),
919                  route_type, route_length);
920 
921         switch(route_type) {
922         case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI:
923             ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN);
924             offset = strlen(buf);
925             snprintf(buf + offset, buflen - offset, ", RD: %s, Originator %s",
926                      bgp_vpn_rd_print(ndo, pptr),
927                      bgp_vpn_ip_print(ndo, pptr + BGP_VPN_RD_LEN,
928                                       (route_length - BGP_VPN_RD_LEN) << 3));
929             break;
930         case BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI:
931             ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN + 4);
932             offset = strlen(buf);
933 	    snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
934 		bgp_vpn_rd_print(ndo, pptr),
935 		as_printf(ndo, astostr, sizeof(astostr),
936 		EXTRACT_32BITS(pptr + BGP_VPN_RD_LEN)));
937             break;
938 
939         case BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI:
940             ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN);
941             offset = strlen(buf);
942             snprintf(buf + offset, buflen - offset, ", RD: %s",
943                      bgp_vpn_rd_print(ndo, pptr));
944             pptr += BGP_VPN_RD_LEN;
945 
946             sg_length = bgp_vpn_sg_print(ndo, pptr, buf, buflen);
947             addr_length =  route_length - sg_length;
948 
949             ND_TCHECK2(pptr[0], addr_length);
950             offset = strlen(buf);
951             snprintf(buf + offset, buflen - offset, ", Originator %s",
952                      bgp_vpn_ip_print(ndo, pptr, addr_length << 3));
953             break;
954 
955         case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE:
956             ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN);
957             offset = strlen(buf);
958             snprintf(buf + offset, buflen - offset, ", RD: %s",
959                      bgp_vpn_rd_print(ndo, pptr));
960             pptr += BGP_VPN_RD_LEN;
961 
962             bgp_vpn_sg_print(ndo, pptr, buf, buflen);
963             break;
964 
965         case BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN: /* fall through */
966         case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN:
967             ND_TCHECK2(pptr[0], BGP_VPN_RD_LEN);
968             offset = strlen(buf);
969 	    snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
970 		bgp_vpn_rd_print(ndo, pptr),
971 		as_printf(ndo, astostr, sizeof(astostr),
972 		EXTRACT_32BITS(pptr + BGP_VPN_RD_LEN)));
973             pptr += BGP_VPN_RD_LEN;
974 
975             bgp_vpn_sg_print(ndo, pptr, buf, buflen);
976             break;
977 
978             /*
979              * no per route-type printing yet.
980              */
981         case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF:
982         default:
983             break;
984         }
985 
986         return route_length + 2;
987 
988 trunc:
989 	return -2;
990 }
991 
992 /*
993  * As I remember, some versions of systems have an snprintf() that
994  * returns -1 if the buffer would have overflowed.  If the return
995  * value is negative, set buflen to 0, to indicate that we've filled
996  * the buffer up.
997  *
998  * If the return value is greater than buflen, that means that
999  * the buffer would have overflowed; again, set buflen to 0 in
1000  * that case.
1001  */
1002 #define UPDATE_BUF_BUFLEN(buf, buflen, stringlen) \
1003     if (stringlen<0) \
1004        	buflen=0; \
1005     else if ((u_int)stringlen>buflen) \
1006         buflen=0; \
1007     else { \
1008         buflen-=stringlen; \
1009 	buf+=stringlen; \
1010     }
1011 
1012 static int
1013 decode_labeled_vpn_l2(netdissect_options *ndo,
1014                       const u_char *pptr, char *buf, u_int buflen)
1015 {
1016         int plen,tlen,stringlen,tlv_type,tlv_len,ttlv_len;
1017 
1018 	ND_TCHECK2(pptr[0], 2);
1019         plen=EXTRACT_16BITS(pptr);
1020         tlen=plen;
1021         pptr+=2;
1022 	/* Old and new L2VPN NLRI share AFI/SAFI
1023          *   -> Assume a 12 Byte-length NLRI is auto-discovery-only
1024          *      and > 17 as old format. Complain for the middle case
1025          */
1026         if (plen==12) {
1027 	    /* assume AD-only with RD, BGPNH */
1028 	    ND_TCHECK2(pptr[0],12);
1029 	    buf[0]='\0';
1030 	    stringlen=snprintf(buf, buflen, "RD: %s, BGPNH: %s",
1031 			       bgp_vpn_rd_print(ndo, pptr),
1032 			       ipaddr_string(ndo, pptr+8)
1033 			       );
1034 	    UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1035 	    pptr+=12;
1036 	    tlen-=12;
1037 	    return plen;
1038         } else if (plen>17) {
1039 	    /* assume old format */
1040 	    /* RD, ID, LBLKOFF, LBLBASE */
1041 
1042 	    ND_TCHECK2(pptr[0],15);
1043 	    buf[0]='\0';
1044 	    stringlen=snprintf(buf, buflen, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
1045 			       bgp_vpn_rd_print(ndo, pptr),
1046 			       EXTRACT_16BITS(pptr+8),
1047 			       EXTRACT_16BITS(pptr+10),
1048 			       EXTRACT_24BITS(pptr+12)>>4); /* the label is offsetted by 4 bits so lets shift it right */
1049 	    UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1050 	    pptr+=15;
1051 	    tlen-=15;
1052 
1053 	    /* ok now the variable part - lets read out TLVs*/
1054 	    while (tlen>0) {
1055 		if (tlen < 3)
1056 		    return -1;
1057 		ND_TCHECK2(pptr[0], 3);
1058 		tlv_type=*pptr++;
1059 		tlv_len=EXTRACT_16BITS(pptr);
1060 		ttlv_len=tlv_len;
1061 		pptr+=2;
1062 
1063 		switch(tlv_type) {
1064 		case 1:
1065 		    if (buflen!=0) {
1066 			stringlen=snprintf(buf,buflen, "\n\t\tcircuit status vector (%u) length: %u: 0x",
1067 					   tlv_type,
1068 					   tlv_len);
1069 			UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1070 		    }
1071 		    ttlv_len=ttlv_len/8+1; /* how many bytes do we need to read ? */
1072 		    while (ttlv_len>0) {
1073 			ND_TCHECK(pptr[0]);
1074 			if (buflen!=0) {
1075 			    stringlen=snprintf(buf,buflen, "%02x",*pptr++);
1076 			    UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1077 			}
1078 			ttlv_len--;
1079 		    }
1080 		    break;
1081 		default:
1082 		    if (buflen!=0) {
1083 			stringlen=snprintf(buf,buflen, "\n\t\tunknown TLV #%u, length: %u",
1084 					   tlv_type,
1085 					   tlv_len);
1086 			UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
1087 		    }
1088 		    break;
1089 		}
1090 		tlen-=(tlv_len<<3); /* the tlv-length is expressed in bits so lets shift it right */
1091 	    }
1092 	    return plen+2;
1093 
1094         } else {
1095 	    /* complain bitterly ? */
1096 	    /* fall through */
1097             goto trunc;
1098         }
1099 
1100 trunc:
1101         return -2;
1102 }
1103 
1104 int
1105 decode_prefix6(netdissect_options *ndo,
1106                const u_char *pd, u_int itemlen, char *buf, u_int buflen)
1107 {
1108 	struct in6_addr addr;
1109 	u_int plen, plenbytes;
1110 
1111 	ND_TCHECK(pd[0]);
1112 	ITEMCHECK(1);
1113 	plen = pd[0];
1114 	if (128 < plen)
1115 		return -1;
1116 	itemlen -= 1;
1117 
1118 	memset(&addr, 0, sizeof(addr));
1119 	plenbytes = (plen + 7) / 8;
1120 	ND_TCHECK2(pd[1], plenbytes);
1121 	ITEMCHECK(plenbytes);
1122 	memcpy(&addr, &pd[1], plenbytes);
1123 	if (plen % 8) {
1124 		addr.s6_addr[plenbytes - 1] &=
1125 			((0xff00 >> (plen % 8)) & 0xff);
1126 	}
1127 	snprintf(buf, buflen, "%s/%d", ip6addr_string(ndo, &addr), plen);
1128 	return 1 + plenbytes;
1129 
1130 trunc:
1131 	return -2;
1132 
1133 badtlv:
1134 	return -3;
1135 }
1136 
1137 static int
1138 decode_labeled_prefix6(netdissect_options *ndo,
1139                        const u_char *pptr, u_int itemlen, char *buf, u_int buflen)
1140 {
1141 	struct in6_addr addr;
1142 	u_int plen, plenbytes;
1143 
1144 	/* prefix length and label = 4 bytes */
1145 	ND_TCHECK2(pptr[0], 4);
1146 	ITEMCHECK(4);
1147 	plen = pptr[0]; /* get prefix length */
1148 
1149 	if (24 > plen)
1150 		return -1;
1151 
1152         plen-=24; /* adjust prefixlen - labellength */
1153 
1154 	if (128 < plen)
1155 		return -1;
1156 	itemlen -= 4;
1157 
1158 	memset(&addr, 0, sizeof(addr));
1159 	plenbytes = (plen + 7) / 8;
1160 	ND_TCHECK2(pptr[4], plenbytes);
1161 	memcpy(&addr, &pptr[4], plenbytes);
1162 	if (plen % 8) {
1163 		addr.s6_addr[plenbytes - 1] &=
1164 			((0xff00 >> (plen % 8)) & 0xff);
1165 	}
1166         /* the label may get offsetted by 4 bits so lets shift it right */
1167 	snprintf(buf, buflen, "%s/%d, label:%u %s",
1168                  ip6addr_string(ndo, &addr),
1169                  plen,
1170                  EXTRACT_24BITS(pptr+1)>>4,
1171                  ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1172 
1173 	return 4 + plenbytes;
1174 
1175 trunc:
1176 	return -2;
1177 
1178 badtlv:
1179 	return -3;
1180 }
1181 
1182 static int
1183 decode_labeled_vpn_prefix6(netdissect_options *ndo,
1184                            const u_char *pptr, char *buf, u_int buflen)
1185 {
1186 	struct in6_addr addr;
1187 	u_int plen;
1188 
1189 	ND_TCHECK(pptr[0]);
1190 	plen = pptr[0];   /* get prefix length */
1191 
1192 	if ((24+64) > plen)
1193 		return -1;
1194 
1195         plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
1196 
1197 	if (128 < plen)
1198 		return -1;
1199 
1200 	memset(&addr, 0, sizeof(addr));
1201 	ND_TCHECK2(pptr[12], (plen + 7) / 8);
1202 	memcpy(&addr, &pptr[12], (plen + 7) / 8);
1203 	if (plen % 8) {
1204 		addr.s6_addr[(plen + 7) / 8 - 1] &=
1205 			((0xff00 >> (plen % 8)) & 0xff);
1206 	}
1207         /* the label may get offsetted by 4 bits so lets shift it right */
1208 	snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
1209                  bgp_vpn_rd_print(ndo, pptr+4),
1210                  ip6addr_string(ndo, &addr),
1211                  plen,
1212                  EXTRACT_24BITS(pptr+1)>>4,
1213                  ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1214 
1215 	return 12 + (plen + 7) / 8;
1216 
1217 trunc:
1218 	return -2;
1219 }
1220 
1221 static int
1222 decode_clnp_prefix(netdissect_options *ndo,
1223                    const u_char *pptr, char *buf, u_int buflen)
1224 {
1225         uint8_t addr[19];
1226 	u_int plen;
1227 
1228 	ND_TCHECK(pptr[0]);
1229 	plen = pptr[0]; /* get prefix length */
1230 
1231 	if (152 < plen)
1232 		return -1;
1233 
1234 	memset(&addr, 0, sizeof(addr));
1235 	ND_TCHECK2(pptr[4], (plen + 7) / 8);
1236 	memcpy(&addr, &pptr[4], (plen + 7) / 8);
1237 	if (plen % 8) {
1238 		addr[(plen + 7) / 8 - 1] &=
1239 			((0xff00 >> (plen % 8)) & 0xff);
1240 	}
1241 	snprintf(buf, buflen, "%s/%d",
1242                  isonsap_string(ndo, addr,(plen + 7) / 8),
1243                  plen);
1244 
1245 	return 1 + (plen + 7) / 8;
1246 
1247 trunc:
1248 	return -2;
1249 }
1250 
1251 static int
1252 decode_labeled_vpn_clnp_prefix(netdissect_options *ndo,
1253                                const u_char *pptr, char *buf, u_int buflen)
1254 {
1255         uint8_t addr[19];
1256 	u_int plen;
1257 
1258 	ND_TCHECK(pptr[0]);
1259 	plen = pptr[0];   /* get prefix length */
1260 
1261 	if ((24+64) > plen)
1262 		return -1;
1263 
1264         plen-=(24+64); /* adjust prefixlen - labellength - RD len*/
1265 
1266 	if (152 < plen)
1267 		return -1;
1268 
1269 	memset(&addr, 0, sizeof(addr));
1270 	ND_TCHECK2(pptr[12], (plen + 7) / 8);
1271 	memcpy(&addr, &pptr[12], (plen + 7) / 8);
1272 	if (plen % 8) {
1273 		addr[(plen + 7) / 8 - 1] &=
1274 			((0xff00 >> (plen % 8)) & 0xff);
1275 	}
1276         /* the label may get offsetted by 4 bits so lets shift it right */
1277 	snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s",
1278                  bgp_vpn_rd_print(ndo, pptr+4),
1279                  isonsap_string(ndo, addr,(plen + 7) / 8),
1280                  plen,
1281                  EXTRACT_24BITS(pptr+1)>>4,
1282                  ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1283 
1284 	return 12 + (plen + 7) / 8;
1285 
1286 trunc:
1287 	return -2;
1288 }
1289 
1290 /*
1291  * bgp_attr_get_as_size
1292  *
1293  * Try to find the size of the ASs encoded in an as-path. It is not obvious, as
1294  * both Old speakers that do not support 4 byte AS, and the new speakers that do
1295  * support, exchange AS-Path with the same path-attribute type value 0x02.
1296  */
1297 static int
1298 bgp_attr_get_as_size(netdissect_options *ndo,
1299                      uint8_t bgpa_type, const u_char *pptr, int len)
1300 {
1301     const u_char *tptr = pptr;
1302 
1303     /*
1304      * If the path attribute is the optional AS4 path type, then we already
1305      * know, that ASs must be encoded in 4 byte format.
1306      */
1307     if (bgpa_type == BGPTYPE_AS4_PATH) {
1308         return 4;
1309     }
1310 
1311     /*
1312      * Let us assume that ASs are of 2 bytes in size, and check if the AS-Path
1313      * TLV is good. If not, ask the caller to try with AS encoded as 4 bytes
1314      * each.
1315      */
1316     while (tptr < pptr + len) {
1317         ND_TCHECK(tptr[0]);
1318 
1319         /*
1320          * If we do not find a valid segment type, our guess might be wrong.
1321          */
1322         if (tptr[0] < BGP_AS_SEG_TYPE_MIN || tptr[0] > BGP_AS_SEG_TYPE_MAX) {
1323             goto trunc;
1324         }
1325         ND_TCHECK(tptr[1]);
1326         tptr += 2 + tptr[1] * 2;
1327     }
1328 
1329     /*
1330      * If we correctly reached end of the AS path attribute data content,
1331      * then most likely ASs were indeed encoded as 2 bytes.
1332      */
1333     if (tptr == pptr + len) {
1334         return 2;
1335     }
1336 
1337 trunc:
1338 
1339     /*
1340      * We can come here, either we did not have enough data, or if we
1341      * try to decode 4 byte ASs in 2 byte format. Either way, return 4,
1342      * so that calller can try to decode each AS as of 4 bytes. If indeed
1343      * there was not enough data, it will crib and end the parse anyways.
1344      */
1345    return 4;
1346 }
1347 
1348 static int
1349 bgp_attr_print(netdissect_options *ndo,
1350                u_int atype, const u_char *pptr, u_int len)
1351 {
1352 	int i;
1353 	uint16_t af;
1354 	uint8_t safi, snpa, nhlen;
1355         union { /* copy buffer for bandwidth values */
1356             float f;
1357             uint32_t i;
1358         } bw;
1359 	int advance;
1360 	u_int tlen;
1361 	const u_char *tptr;
1362 	char buf[MAXHOSTNAMELEN + 100];
1363         int  as_size;
1364 
1365         tptr = pptr;
1366         tlen=len;
1367 
1368 	switch (atype) {
1369 	case BGPTYPE_ORIGIN:
1370 		if (len != 1)
1371 			ND_PRINT((ndo, "invalid len"));
1372 		else {
1373 			ND_TCHECK(*tptr);
1374 			ND_PRINT((ndo, "%s", tok2str(bgp_origin_values,
1375 						"Unknown Origin Typecode",
1376 						tptr[0])));
1377 		}
1378 		break;
1379 
1380         /*
1381          * Process AS4 byte path and AS2 byte path attributes here.
1382          */
1383 	case BGPTYPE_AS4_PATH:
1384 	case BGPTYPE_AS_PATH:
1385 		if (len % 2) {
1386 			ND_PRINT((ndo, "invalid len"));
1387 			break;
1388 		}
1389                 if (!len) {
1390 			ND_PRINT((ndo, "empty"));
1391 			break;
1392                 }
1393 
1394                 /*
1395                  * BGP updates exchanged between New speakers that support 4
1396                  * byte AS, ASs are always encoded in 4 bytes. There is no
1397                  * definitive way to find this, just by the packet's
1398                  * contents. So, check for packet's TLV's sanity assuming
1399                  * 2 bytes first, and it does not pass, assume that ASs are
1400                  * encoded in 4 bytes format and move on.
1401                  */
1402                 as_size = bgp_attr_get_as_size(ndo, atype, pptr, len);
1403 
1404 		while (tptr < pptr + len) {
1405 			ND_TCHECK(tptr[0]);
1406                         ND_PRINT((ndo, "%s", tok2str(bgp_as_path_segment_open_values,
1407 						"?", tptr[0])));
1408                         for (i = 0; i < tptr[1] * as_size; i += as_size) {
1409                             ND_TCHECK2(tptr[2 + i], as_size);
1410 			    ND_PRINT((ndo, "%s ",
1411 				as_printf(ndo, astostr, sizeof(astostr),
1412 				as_size == 2 ?
1413 				EXTRACT_16BITS(&tptr[2 + i]) :
1414 				EXTRACT_32BITS(&tptr[2 + i]))));
1415                         }
1416 			ND_TCHECK(tptr[0]);
1417                         ND_PRINT((ndo, "%s", tok2str(bgp_as_path_segment_close_values,
1418 						"?", tptr[0])));
1419                         ND_TCHECK(tptr[1]);
1420                         tptr += 2 + tptr[1] * as_size;
1421 		}
1422 		break;
1423 	case BGPTYPE_NEXT_HOP:
1424 		if (len != 4)
1425 			ND_PRINT((ndo, "invalid len"));
1426 		else {
1427 			ND_TCHECK2(tptr[0], 4);
1428 			ND_PRINT((ndo, "%s", ipaddr_string(ndo, tptr)));
1429 		}
1430 		break;
1431 	case BGPTYPE_MULTI_EXIT_DISC:
1432 	case BGPTYPE_LOCAL_PREF:
1433 		if (len != 4)
1434 			ND_PRINT((ndo, "invalid len"));
1435 		else {
1436 			ND_TCHECK2(tptr[0], 4);
1437 			ND_PRINT((ndo, "%u", EXTRACT_32BITS(tptr)));
1438 		}
1439 		break;
1440 	case BGPTYPE_ATOMIC_AGGREGATE:
1441 		if (len != 0)
1442 			ND_PRINT((ndo, "invalid len"));
1443 		break;
1444         case BGPTYPE_AGGREGATOR:
1445 
1446                 /*
1447                  * Depending on the AS encoded is of 2 bytes or of 4 bytes,
1448                  * the length of this PA can be either 6 bytes or 8 bytes.
1449                  */
1450                 if (len != 6 && len != 8) {
1451                     ND_PRINT((ndo, "invalid len"));
1452                     break;
1453                 }
1454                 ND_TCHECK2(tptr[0], len);
1455                 if (len == 6) {
1456 		    ND_PRINT((ndo, " AS #%s, origin %s",
1457 			as_printf(ndo, astostr, sizeof(astostr), EXTRACT_16BITS(tptr)),
1458 			ipaddr_string(ndo, tptr + 2)));
1459                 } else {
1460 		    ND_PRINT((ndo, " AS #%s, origin %s",
1461 			as_printf(ndo, astostr, sizeof(astostr),
1462 			EXTRACT_32BITS(tptr)), ipaddr_string(ndo, tptr + 4)));
1463                 }
1464                 break;
1465 	case BGPTYPE_AGGREGATOR4:
1466 		if (len != 8) {
1467 			ND_PRINT((ndo, "invalid len"));
1468 			break;
1469 		}
1470 		ND_TCHECK2(tptr[0], 8);
1471 		ND_PRINT((ndo, " AS #%s, origin %s",
1472 	   	    as_printf(ndo, astostr, sizeof(astostr), EXTRACT_32BITS(tptr)),
1473 		    ipaddr_string(ndo, tptr + 4)));
1474 		break;
1475 	case BGPTYPE_COMMUNITIES:
1476 		if (len % 4) {
1477 			ND_PRINT((ndo, "invalid len"));
1478 			break;
1479 		}
1480 		while (tlen>0) {
1481 			uint32_t comm;
1482 			ND_TCHECK2(tptr[0], 4);
1483 			comm = EXTRACT_32BITS(tptr);
1484 			switch (comm) {
1485 			case BGP_COMMUNITY_NO_EXPORT:
1486 				ND_PRINT((ndo, " NO_EXPORT"));
1487 				break;
1488 			case BGP_COMMUNITY_NO_ADVERT:
1489 				ND_PRINT((ndo, " NO_ADVERTISE"));
1490 				break;
1491 			case BGP_COMMUNITY_NO_EXPORT_SUBCONFED:
1492 				ND_PRINT((ndo, " NO_EXPORT_SUBCONFED"));
1493 				break;
1494 			default:
1495 				ND_PRINT((ndo, "%u:%u%s",
1496                                        (comm >> 16) & 0xffff,
1497                                        comm & 0xffff,
1498                                        (tlen>4) ? ", " : ""));
1499 				break;
1500 			}
1501                         tlen -=4;
1502                         tptr +=4;
1503 		}
1504 		break;
1505         case BGPTYPE_ORIGINATOR_ID:
1506 		if (len != 4) {
1507 			ND_PRINT((ndo, "invalid len"));
1508 			break;
1509 		}
1510 		ND_TCHECK2(tptr[0], 4);
1511                 ND_PRINT((ndo, "%s",ipaddr_string(ndo, tptr)));
1512                 break;
1513         case BGPTYPE_CLUSTER_LIST:
1514 		if (len % 4) {
1515 			ND_PRINT((ndo, "invalid len"));
1516 			break;
1517 		}
1518                 while (tlen>0) {
1519 			ND_TCHECK2(tptr[0], 4);
1520                         ND_PRINT((ndo, "%s%s",
1521                                ipaddr_string(ndo, tptr),
1522                                 (tlen>4) ? ", " : ""));
1523                         tlen -=4;
1524                         tptr +=4;
1525                 }
1526                 break;
1527 	case BGPTYPE_MP_REACH_NLRI:
1528 		ND_TCHECK2(tptr[0], 3);
1529 		af = EXTRACT_16BITS(tptr);
1530 		safi = tptr[2];
1531 
1532                 ND_PRINT((ndo, "\n\t    AFI: %s (%u), %sSAFI: %s (%u)",
1533                        tok2str(af_values, "Unknown AFI", af),
1534                        af,
1535                        (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1536                        tok2str(bgp_safi_values, "Unknown SAFI", safi),
1537                        safi));
1538 
1539                 switch(af<<8 | safi) {
1540                 case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1541                 case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1542                 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1543                 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1544                 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
1545                 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1546                 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1547                 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1548                 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN):
1549 		case (AFNUM_INET<<8 | SAFNUM_MDT):
1550                 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1551                 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1552                 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1553                 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1554                 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1555                 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1556                 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1557                 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1558                 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1559                 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1560                 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1561                 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1562                 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1563                 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1564                 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1565                 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1566                 case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1567                     break;
1568                 default:
1569                     ND_TCHECK2(tptr[0], tlen);
1570                     ND_PRINT((ndo, "\n\t    no AFI %u / SAFI %u decoder", af, safi));
1571                     if (ndo->ndo_vflag <= 1)
1572                         print_unknown_data(ndo, tptr, "\n\t    ", tlen);
1573                     goto done;
1574                     break;
1575                 }
1576 
1577                 tptr +=3;
1578 
1579 		ND_TCHECK(tptr[0]);
1580 		nhlen = tptr[0];
1581                 tlen = nhlen;
1582                 tptr++;
1583 
1584 		if (tlen) {
1585                     int nnh = 0;
1586                     ND_PRINT((ndo, "\n\t    nexthop: "));
1587                     while (tlen > 0) {
1588                         if ( nnh++ > 0 ) {
1589                             ND_PRINT((ndo,  ", " ));
1590                         }
1591                         switch(af<<8 | safi) {
1592                         case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1593                         case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1594                         case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1595                         case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1596                         case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
1597                         case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN):
1598                         case (AFNUM_INET<<8 | SAFNUM_MDT):
1599 			    if (tlen < (int)sizeof(struct in_addr)) {
1600                                 ND_PRINT((ndo, "invalid len"));
1601                                 tlen = 0;
1602                             } else {
1603                                 ND_TCHECK2(tptr[0], sizeof(struct in_addr));
1604                                 ND_PRINT((ndo, "%s",ipaddr_string(ndo, tptr)));
1605                                 tlen -= sizeof(struct in_addr);
1606                                 tptr += sizeof(struct in_addr);
1607                             }
1608                             break;
1609                         case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1610                         case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1611                         case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1612                             if (tlen < (int)(sizeof(struct in_addr)+BGP_VPN_RD_LEN)) {
1613                                 ND_PRINT((ndo, "invalid len"));
1614                                 tlen = 0;
1615                             } else {
1616                                 ND_TCHECK2(tptr[0], sizeof(struct in_addr)+BGP_VPN_RD_LEN);
1617                                 ND_PRINT((ndo, "RD: %s, %s",
1618                                        bgp_vpn_rd_print(ndo, tptr),
1619                                        ipaddr_string(ndo, tptr+BGP_VPN_RD_LEN)));
1620                                 tlen -= (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
1621                                 tptr += (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
1622                             }
1623                             break;
1624                         case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1625                         case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1626                         case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1627                         case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1628                             if (tlen < (int)sizeof(struct in6_addr)) {
1629                                 ND_PRINT((ndo, "invalid len"));
1630                                 tlen = 0;
1631                             } else {
1632                                 ND_TCHECK2(tptr[0], sizeof(struct in6_addr));
1633                                 ND_PRINT((ndo, "%s", ip6addr_string(ndo, tptr)));
1634                                 tlen -= sizeof(struct in6_addr);
1635                                 tptr += sizeof(struct in6_addr);
1636                             }
1637                             break;
1638                         case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1639                         case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1640                         case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1641                             if (tlen < (int)(sizeof(struct in6_addr)+BGP_VPN_RD_LEN)) {
1642                                 ND_PRINT((ndo, "invalid len"));
1643                                 tlen = 0;
1644                             } else {
1645                                 ND_TCHECK2(tptr[0], sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
1646                                 ND_PRINT((ndo, "RD: %s, %s",
1647                                        bgp_vpn_rd_print(ndo, tptr),
1648                                        ip6addr_string(ndo, tptr+BGP_VPN_RD_LEN)));
1649                                 tlen -= (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
1650                                 tptr += (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
1651                             }
1652                             break;
1653                         case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1654                         case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1655                         case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1656                         case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1657                             if (tlen < (int)sizeof(struct in_addr)) {
1658                                 ND_PRINT((ndo, "invalid len"));
1659                                 tlen = 0;
1660                             } else {
1661                                 ND_TCHECK2(tptr[0], sizeof(struct in_addr));
1662                                 ND_PRINT((ndo, "%s", ipaddr_string(ndo, tptr)));
1663                                 tlen -= (sizeof(struct in_addr));
1664                                 tptr += (sizeof(struct in_addr));
1665                             }
1666                             break;
1667                         case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1668                         case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1669                         case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1670                             ND_TCHECK2(tptr[0], tlen);
1671                             ND_PRINT((ndo, "%s", isonsap_string(ndo, tptr, tlen)));
1672                             tptr += tlen;
1673                             tlen = 0;
1674                             break;
1675 
1676                         case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1677                         case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1678                         case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1679                             if (tlen < BGP_VPN_RD_LEN+1) {
1680                                 ND_PRINT((ndo, "invalid len"));
1681                                 tlen = 0;
1682                             } else {
1683                                 ND_TCHECK2(tptr[0], tlen);
1684                                 ND_PRINT((ndo, "RD: %s, %s",
1685                                        bgp_vpn_rd_print(ndo, tptr),
1686                                        isonsap_string(ndo, tptr+BGP_VPN_RD_LEN,tlen-BGP_VPN_RD_LEN)));
1687                                 /* rfc986 mapped IPv4 address ? */
1688                                 if (EXTRACT_32BITS(tptr+BGP_VPN_RD_LEN) ==  0x47000601)
1689                                     ND_PRINT((ndo, " = %s", ipaddr_string(ndo, tptr+BGP_VPN_RD_LEN+4)));
1690                                 /* rfc1888 mapped IPv6 address ? */
1691                                 else if (EXTRACT_24BITS(tptr+BGP_VPN_RD_LEN) ==  0x350000)
1692                                     ND_PRINT((ndo, " = %s", ip6addr_string(ndo, tptr+BGP_VPN_RD_LEN+3)));
1693                                 tptr += tlen;
1694                                 tlen = 0;
1695                             }
1696                             break;
1697                         default:
1698                             ND_TCHECK2(tptr[0], tlen);
1699                             ND_PRINT((ndo, "no AFI %u/SAFI %u decoder", af, safi));
1700                             if (ndo->ndo_vflag <= 1)
1701                                 print_unknown_data(ndo, tptr, "\n\t    ", tlen);
1702                             tptr += tlen;
1703                             tlen = 0;
1704                             goto done;
1705                             break;
1706                         }
1707                     }
1708 		}
1709 		ND_PRINT((ndo, ", nh-length: %u", nhlen));
1710 		tptr += tlen;
1711 
1712 		ND_TCHECK(tptr[0]);
1713 		snpa = tptr[0];
1714 		tptr++;
1715 
1716 		if (snpa) {
1717 			ND_PRINT((ndo, "\n\t    %u SNPA", snpa));
1718 			for (/*nothing*/; snpa > 0; snpa--) {
1719 				ND_TCHECK(tptr[0]);
1720 				ND_PRINT((ndo, "\n\t      %d bytes", tptr[0]));
1721 				tptr += tptr[0] + 1;
1722 			}
1723 		} else {
1724 			ND_PRINT((ndo, ", no SNPA"));
1725                 }
1726 
1727 		while (len - (tptr - pptr) > 0) {
1728                     switch (af<<8 | safi) {
1729                     case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1730                     case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1731                     case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1732                         advance = decode_prefix4(ndo, tptr, len, buf, sizeof(buf));
1733                         if (advance == -1)
1734                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1735                         else if (advance == -2)
1736                             goto trunc;
1737                         else if (advance == -3)
1738                             break; /* bytes left, but not enough */
1739                         else
1740                             ND_PRINT((ndo, "\n\t      %s", buf));
1741                         break;
1742                     case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1743                         advance = decode_labeled_prefix4(ndo, tptr, len, buf, sizeof(buf));
1744                         if (advance == -1)
1745                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1746                         else if (advance == -2)
1747                             goto trunc;
1748                         else if (advance == -3)
1749                             break; /* bytes left, but not enough */
1750                         else
1751                             ND_PRINT((ndo, "\n\t      %s", buf));
1752                         break;
1753                     case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1754                     case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1755                     case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1756                         advance = decode_labeled_vpn_prefix4(ndo, tptr, buf, sizeof(buf));
1757                         if (advance == -1)
1758                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1759                         else if (advance == -2)
1760                             goto trunc;
1761                         else
1762                             ND_PRINT((ndo, "\n\t      %s", buf));
1763                         break;
1764                     case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
1765                         advance = decode_rt_routing_info(ndo, tptr, buf, sizeof(buf));
1766                         if (advance == -1)
1767                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1768                         else if (advance == -2)
1769                             goto trunc;
1770                         else
1771                             ND_PRINT((ndo, "\n\t      %s", buf));
1772                         break;
1773                     case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): /* fall through */
1774                     case (AFNUM_INET6<<8 | SAFNUM_MULTICAST_VPN):
1775                         advance = decode_multicast_vpn(ndo, tptr, buf, sizeof(buf));
1776                         if (advance == -1)
1777                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1778                         else if (advance == -2)
1779                             goto trunc;
1780                         else
1781                             ND_PRINT((ndo, "\n\t      %s", buf));
1782                         break;
1783 
1784 		    case (AFNUM_INET<<8 | SAFNUM_MDT):
1785 		      advance = decode_mdt_vpn_nlri(ndo, tptr, buf, sizeof(buf));
1786 		      if (advance == -1)
1787                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1788                         else if (advance == -2)
1789                             goto trunc;
1790                         else
1791                             ND_PRINT((ndo, "\n\t      %s", buf));
1792 		       break;
1793                     case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1794                     case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1795                     case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1796                         advance = decode_prefix6(ndo, tptr, len, buf, sizeof(buf));
1797                         if (advance == -1)
1798                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1799                         else if (advance == -2)
1800                             goto trunc;
1801                         else if (advance == -3)
1802                             break; /* bytes left, but not enough */
1803                         else
1804                             ND_PRINT((ndo, "\n\t      %s", buf));
1805                         break;
1806                     case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1807                         advance = decode_labeled_prefix6(ndo, tptr, len, buf, sizeof(buf));
1808                         if (advance == -1)
1809                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1810                         else if (advance == -2)
1811                             goto trunc;
1812                         else if (advance == -3)
1813                             break; /* bytes left, but not enough */
1814                         else
1815                             ND_PRINT((ndo, "\n\t      %s", buf));
1816                         break;
1817                     case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1818                     case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1819                     case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1820                         advance = decode_labeled_vpn_prefix6(ndo, tptr, buf, sizeof(buf));
1821                         if (advance == -1)
1822                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1823                         else if (advance == -2)
1824                             goto trunc;
1825                         else
1826                             ND_PRINT((ndo, "\n\t      %s", buf));
1827                         break;
1828                     case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1829                     case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1830                     case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1831                     case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1832                         advance = decode_labeled_vpn_l2(ndo, tptr, buf, sizeof(buf));
1833                         if (advance == -1)
1834                             ND_PRINT((ndo, "\n\t    (illegal length)"));
1835                         else if (advance == -2)
1836                             goto trunc;
1837                         else
1838                             ND_PRINT((ndo, "\n\t      %s", buf));
1839                         break;
1840                     case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1841                     case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1842                     case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1843                         advance = decode_clnp_prefix(ndo, tptr, buf, sizeof(buf));
1844                         if (advance == -1)
1845                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1846                         else if (advance == -2)
1847                             goto trunc;
1848                         else
1849                             ND_PRINT((ndo, "\n\t      %s", buf));
1850                         break;
1851                     case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1852                     case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1853                     case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1854                         advance = decode_labeled_vpn_clnp_prefix(ndo, tptr, buf, sizeof(buf));
1855                         if (advance == -1)
1856                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1857                         else if (advance == -2)
1858                             goto trunc;
1859                         else
1860                             ND_PRINT((ndo, "\n\t      %s", buf));
1861                         break;
1862                     default:
1863                         ND_TCHECK2(*tptr,tlen);
1864                         ND_PRINT((ndo, "\n\t    no AFI %u / SAFI %u decoder", af, safi));
1865                         if (ndo->ndo_vflag <= 1)
1866                             print_unknown_data(ndo, tptr, "\n\t    ", tlen);
1867                         advance = 0;
1868                         tptr = pptr + len;
1869                         break;
1870                     }
1871                     if (advance < 0)
1872                         break;
1873                     tptr += advance;
1874 		}
1875         done:
1876 		break;
1877 
1878 	case BGPTYPE_MP_UNREACH_NLRI:
1879 		ND_TCHECK2(tptr[0], BGP_MP_NLRI_MINSIZE);
1880 		af = EXTRACT_16BITS(tptr);
1881 		safi = tptr[2];
1882 
1883                 ND_PRINT((ndo, "\n\t    AFI: %s (%u), %sSAFI: %s (%u)",
1884                        tok2str(af_values, "Unknown AFI", af),
1885                        af,
1886                        (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1887                        tok2str(bgp_safi_values, "Unknown SAFI", safi),
1888                        safi));
1889 
1890                 if (len == BGP_MP_NLRI_MINSIZE)
1891                     ND_PRINT((ndo, "\n\t      End-of-Rib Marker (empty NLRI)"));
1892 
1893 		tptr += 3;
1894 
1895 		while (len - (tptr - pptr) > 0) {
1896                     switch (af<<8 | safi) {
1897                     case (AFNUM_INET<<8 | SAFNUM_UNICAST):
1898                     case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
1899                     case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
1900                         advance = decode_prefix4(ndo, tptr, len, buf, sizeof(buf));
1901                         if (advance == -1)
1902                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1903                         else if (advance == -2)
1904                             goto trunc;
1905                         else if (advance == -3)
1906                             break; /* bytes left, but not enough */
1907                         else
1908                             ND_PRINT((ndo, "\n\t      %s", buf));
1909                         break;
1910                     case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
1911                         advance = decode_labeled_prefix4(ndo, tptr, len, buf, sizeof(buf));
1912                         if (advance == -1)
1913                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1914                         else if (advance == -2)
1915                             goto trunc;
1916                         else if (advance == -3)
1917                             break; /* bytes left, but not enough */
1918                         else
1919                             ND_PRINT((ndo, "\n\t      %s", buf));
1920                         break;
1921                     case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
1922                     case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
1923                     case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
1924                         advance = decode_labeled_vpn_prefix4(ndo, tptr, buf, sizeof(buf));
1925                         if (advance == -1)
1926                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1927                         else if (advance == -2)
1928                             goto trunc;
1929                         else
1930                             ND_PRINT((ndo, "\n\t      %s", buf));
1931                         break;
1932                     case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
1933                     case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
1934                     case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
1935                         advance = decode_prefix6(ndo, tptr, len, buf, sizeof(buf));
1936                         if (advance == -1)
1937                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1938                         else if (advance == -2)
1939                             goto trunc;
1940                         else if (advance == -3)
1941                             break; /* bytes left, but not enough */
1942                         else
1943                             ND_PRINT((ndo, "\n\t      %s", buf));
1944                         break;
1945                     case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
1946                         advance = decode_labeled_prefix6(ndo, tptr, len, buf, sizeof(buf));
1947                         if (advance == -1)
1948                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1949                         else if (advance == -2)
1950                             goto trunc;
1951                         else if (advance == -3)
1952                             break; /* bytes left, but not enough */
1953                         else
1954                             ND_PRINT((ndo, "\n\t      %s", buf));
1955                         break;
1956                     case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
1957                     case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
1958                     case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
1959                         advance = decode_labeled_vpn_prefix6(ndo, tptr, buf, sizeof(buf));
1960                         if (advance == -1)
1961                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1962                         else if (advance == -2)
1963                             goto trunc;
1964                         else
1965                             ND_PRINT((ndo, "\n\t      %s", buf));
1966                         break;
1967                     case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
1968                     case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
1969                     case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
1970                     case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
1971                         advance = decode_labeled_vpn_l2(ndo, tptr, buf, sizeof(buf));
1972                         if (advance == -1)
1973                             ND_PRINT((ndo, "\n\t    (illegal length)"));
1974                         else if (advance == -2)
1975                             goto trunc;
1976                         else
1977                             ND_PRINT((ndo, "\n\t      %s", buf));
1978                         break;
1979                     case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
1980                     case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
1981                     case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
1982                         advance = decode_clnp_prefix(ndo, tptr, buf, sizeof(buf));
1983                         if (advance == -1)
1984                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1985                         else if (advance == -2)
1986                             goto trunc;
1987                         else
1988                             ND_PRINT((ndo, "\n\t      %s", buf));
1989                         break;
1990                     case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
1991                     case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
1992                     case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
1993                         advance = decode_labeled_vpn_clnp_prefix(ndo, tptr, buf, sizeof(buf));
1994                         if (advance == -1)
1995                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
1996                         else if (advance == -2)
1997                             goto trunc;
1998                         else
1999                             ND_PRINT((ndo, "\n\t      %s", buf));
2000                         break;
2001 		    case (AFNUM_INET<<8 | SAFNUM_MDT):
2002 		      advance = decode_mdt_vpn_nlri(ndo, tptr, buf, sizeof(buf));
2003 		      if (advance == -1)
2004                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
2005                         else if (advance == -2)
2006                             goto trunc;
2007                         else
2008                             ND_PRINT((ndo, "\n\t      %s", buf));
2009 		       break;
2010                     case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): /* fall through */
2011                     case (AFNUM_INET6<<8 | SAFNUM_MULTICAST_VPN):
2012                         advance = decode_multicast_vpn(ndo, tptr, buf, sizeof(buf));
2013                         if (advance == -1)
2014                             ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
2015                         else if (advance == -2)
2016                             goto trunc;
2017                         else
2018                             ND_PRINT((ndo, "\n\t      %s", buf));
2019                         break;
2020                     default:
2021                         ND_TCHECK2(*(tptr-3),tlen);
2022                         ND_PRINT((ndo, "no AFI %u / SAFI %u decoder", af, safi));
2023                         if (ndo->ndo_vflag <= 1)
2024                             print_unknown_data(ndo, tptr-3, "\n\t    ", tlen);
2025                         advance = 0;
2026                         tptr = pptr + len;
2027                         break;
2028                     }
2029                     if (advance < 0)
2030                         break;
2031                     tptr += advance;
2032 		}
2033 		break;
2034         case BGPTYPE_EXTD_COMMUNITIES:
2035 		if (len % 8) {
2036 			ND_PRINT((ndo, "invalid len"));
2037 			break;
2038 		}
2039                 while (tlen>0) {
2040                     uint16_t extd_comm;
2041 
2042                     ND_TCHECK2(tptr[0], 2);
2043                     extd_comm=EXTRACT_16BITS(tptr);
2044 
2045 		    ND_PRINT((ndo, "\n\t    %s (0x%04x), Flags [%s]",
2046 			   tok2str(bgp_extd_comm_subtype_values,
2047 				      "unknown extd community typecode",
2048 				      extd_comm),
2049 			   extd_comm,
2050 			   bittok2str(bgp_extd_comm_flag_values, "none", extd_comm)));
2051 
2052                     ND_TCHECK2(*(tptr+2), 6);
2053                     switch(extd_comm) {
2054                     case BGP_EXT_COM_RT_0:
2055                     case BGP_EXT_COM_RO_0:
2056                     case BGP_EXT_COM_L2VPN_RT_0:
2057                         ND_PRINT((ndo, ": %u:%u (= %s)",
2058                                EXTRACT_16BITS(tptr+2),
2059                                EXTRACT_32BITS(tptr+4),
2060                                ipaddr_string(ndo, tptr+4)));
2061                         break;
2062                     case BGP_EXT_COM_RT_1:
2063                     case BGP_EXT_COM_RO_1:
2064                     case BGP_EXT_COM_L2VPN_RT_1:
2065                     case BGP_EXT_COM_VRF_RT_IMP:
2066                         ND_PRINT((ndo, ": %s:%u",
2067                                ipaddr_string(ndo, tptr+2),
2068                                EXTRACT_16BITS(tptr+6)));
2069                         break;
2070                     case BGP_EXT_COM_RT_2:
2071                     case BGP_EXT_COM_RO_2:
2072 			ND_PRINT((ndo, ": %s:%u",
2073 			    as_printf(ndo, astostr, sizeof(astostr),
2074 			    EXTRACT_32BITS(tptr+2)), EXTRACT_16BITS(tptr+6)));
2075 			break;
2076                     case BGP_EXT_COM_LINKBAND:
2077 		        bw.i = EXTRACT_32BITS(tptr+2);
2078                         ND_PRINT((ndo, ": bandwidth: %.3f Mbps",
2079                                bw.f*8/1000000));
2080                         break;
2081                     case BGP_EXT_COM_VPN_ORIGIN:
2082                     case BGP_EXT_COM_VPN_ORIGIN2:
2083                     case BGP_EXT_COM_VPN_ORIGIN3:
2084                     case BGP_EXT_COM_VPN_ORIGIN4:
2085                     case BGP_EXT_COM_OSPF_RID:
2086                     case BGP_EXT_COM_OSPF_RID2:
2087                         ND_PRINT((ndo, "%s", ipaddr_string(ndo, tptr+2)));
2088                         break;
2089                     case BGP_EXT_COM_OSPF_RTYPE:
2090                     case BGP_EXT_COM_OSPF_RTYPE2:
2091                         ND_PRINT((ndo, ": area:%s, router-type:%s, metric-type:%s%s",
2092                                ipaddr_string(ndo, tptr+2),
2093                                tok2str(bgp_extd_comm_ospf_rtype_values,
2094 					  "unknown (0x%02x)",
2095 					  *(tptr+6)),
2096                                (*(tptr+7) &  BGP_OSPF_RTYPE_METRIC_TYPE) ? "E2" : "",
2097                                ((*(tptr+6) == BGP_OSPF_RTYPE_EXT) || (*(tptr+6) == BGP_OSPF_RTYPE_NSSA)) ? "E1" : ""));
2098                         break;
2099                     case BGP_EXT_COM_L2INFO:
2100                         ND_PRINT((ndo, ": %s Control Flags [0x%02x]:MTU %u",
2101                                tok2str(l2vpn_encaps_values,
2102 					  "unknown encaps",
2103 					  *(tptr+2)),
2104                                        *(tptr+3),
2105                                EXTRACT_16BITS(tptr+4)));
2106                         break;
2107                     case BGP_EXT_COM_SOURCE_AS:
2108                         ND_PRINT((ndo, ": AS %u", EXTRACT_16BITS(tptr+2)));
2109                         break;
2110                     default:
2111                         ND_TCHECK2(*tptr,8);
2112                         print_unknown_data(ndo, tptr, "\n\t      ", 8);
2113                         break;
2114                     }
2115                     tlen -=8;
2116                     tptr +=8;
2117                 }
2118                 break;
2119 
2120         case BGPTYPE_PMSI_TUNNEL:
2121         {
2122                 uint8_t tunnel_type, flags;
2123 
2124                 tunnel_type = *(tptr+1);
2125                 flags = *tptr;
2126                 tlen = len;
2127 
2128                 ND_TCHECK2(tptr[0], 5);
2129                 ND_PRINT((ndo, "\n\t    Tunnel-type %s (%u), Flags [%s], MPLS Label %u",
2130                        tok2str(bgp_pmsi_tunnel_values, "Unknown", tunnel_type),
2131                        tunnel_type,
2132                        bittok2str(bgp_pmsi_flag_values, "none", flags),
2133                        EXTRACT_24BITS(tptr+2)>>4));
2134 
2135                 tptr +=5;
2136                 tlen -= 5;
2137 
2138                 switch (tunnel_type) {
2139                 case BGP_PMSI_TUNNEL_PIM_SM: /* fall through */
2140                 case BGP_PMSI_TUNNEL_PIM_BIDIR:
2141                     ND_TCHECK2(tptr[0], 8);
2142                     ND_PRINT((ndo, "\n\t      Sender %s, P-Group %s",
2143                            ipaddr_string(ndo, tptr),
2144                            ipaddr_string(ndo, tptr+4)));
2145                     break;
2146 
2147                 case BGP_PMSI_TUNNEL_PIM_SSM:
2148                     ND_TCHECK2(tptr[0], 8);
2149                     ND_PRINT((ndo, "\n\t      Root-Node %s, P-Group %s",
2150                            ipaddr_string(ndo, tptr),
2151                            ipaddr_string(ndo, tptr+4)));
2152                     break;
2153                 case BGP_PMSI_TUNNEL_INGRESS:
2154                     ND_TCHECK2(tptr[0], 4);
2155                     ND_PRINT((ndo, "\n\t      Tunnel-Endpoint %s",
2156                            ipaddr_string(ndo, tptr)));
2157                     break;
2158                 case BGP_PMSI_TUNNEL_LDP_P2MP: /* fall through */
2159                 case BGP_PMSI_TUNNEL_LDP_MP2MP:
2160                     ND_TCHECK2(tptr[0], 8);
2161                     ND_PRINT((ndo, "\n\t      Root-Node %s, LSP-ID 0x%08x",
2162                            ipaddr_string(ndo, tptr),
2163                            EXTRACT_32BITS(tptr+4)));
2164                     break;
2165                 case BGP_PMSI_TUNNEL_RSVP_P2MP:
2166                     ND_TCHECK2(tptr[0], 8);
2167                     ND_PRINT((ndo, "\n\t      Extended-Tunnel-ID %s, P2MP-ID 0x%08x",
2168                            ipaddr_string(ndo, tptr),
2169                            EXTRACT_32BITS(tptr+4)));
2170                     break;
2171                 default:
2172                     if (ndo->ndo_vflag <= 1) {
2173                         print_unknown_data(ndo, tptr, "\n\t      ", tlen);
2174                     }
2175                 }
2176                 break;
2177         }
2178 	case BGPTYPE_AIGP:
2179 	{
2180 		uint8_t type;
2181 		uint16_t length;
2182 
2183 		ND_TCHECK2(tptr[0], 3);
2184 
2185 		tlen = len;
2186 
2187 		while (tlen >= 3) {
2188 
2189 		    type = *tptr;
2190 		    length = EXTRACT_16BITS(tptr+1);
2191 
2192 		    ND_PRINT((ndo, "\n\t    %s TLV (%u), length %u",
2193 			      tok2str(bgp_aigp_values, "Unknown", type),
2194 			      type, length));
2195 
2196 		    /*
2197 		     * Check if we can read the TLV data.
2198 		     */
2199 		    ND_TCHECK2(tptr[3], length - 3);
2200 
2201 		    switch (type) {
2202 
2203 		    case BGP_AIGP_TLV:
2204 		        ND_TCHECK2(tptr[3], 8);
2205 			ND_PRINT((ndo, ", metric %" PRIu64,
2206 				  EXTRACT_64BITS(tptr+3)));
2207 			break;
2208 
2209 		    default:
2210 			if (ndo->ndo_vflag <= 1) {
2211 			    print_unknown_data(ndo, tptr+3,"\n\t      ", length-3);
2212 			}
2213 		    }
2214 
2215 		    tptr += length;
2216 		    tlen -= length;
2217 		}
2218 		break;
2219 	}
2220         case BGPTYPE_ATTR_SET:
2221                 ND_TCHECK2(tptr[0], 4);
2222                 if (len < 4)
2223                 	goto trunc;
2224 		ND_PRINT((ndo, "\n\t    Origin AS: %s",
2225 		    as_printf(ndo, astostr, sizeof(astostr), EXTRACT_32BITS(tptr))));
2226 		tptr+=4;
2227                 len -=4;
2228 
2229                 while (len) {
2230                     u_int aflags, alenlen, alen;
2231 
2232                     ND_TCHECK2(tptr[0], 2);
2233                     if (len < 2)
2234                         goto trunc;
2235                     aflags = *tptr;
2236                     atype = *(tptr + 1);
2237                     tptr += 2;
2238                     len -= 2;
2239                     alenlen = bgp_attr_lenlen(aflags, tptr);
2240                     ND_TCHECK2(tptr[0], alenlen);
2241                     if (len < alenlen)
2242                         goto trunc;
2243                     alen = bgp_attr_len(aflags, tptr);
2244                     tptr += alenlen;
2245                     len -= alenlen;
2246 
2247                     ND_PRINT((ndo, "\n\t      %s (%u), length: %u",
2248                            tok2str(bgp_attr_values,
2249                                       "Unknown Attribute", atype),
2250                            atype,
2251                            alen));
2252 
2253                     if (aflags) {
2254                         ND_PRINT((ndo, ", Flags [%s%s%s%s",
2255                                aflags & 0x80 ? "O" : "",
2256                                aflags & 0x40 ? "T" : "",
2257                                aflags & 0x20 ? "P" : "",
2258                                aflags & 0x10 ? "E" : ""));
2259                         if (aflags & 0xf)
2260                             ND_PRINT((ndo, "+%x", aflags & 0xf));
2261                         ND_PRINT((ndo, "]: "));
2262                     }
2263                     /* FIXME check for recursion */
2264                     if (!bgp_attr_print(ndo, atype, tptr, alen))
2265                         return 0;
2266                     tptr += alen;
2267                     len -= alen;
2268 		}
2269                 break;
2270 
2271 	case BGPTYPE_LARGE_COMMUNITY:
2272 		if (len == 0 || len % 12) {
2273 			ND_PRINT((ndo, "invalid len"));
2274 			break;
2275 		}
2276 		ND_PRINT((ndo, "\n\t    "));
2277 		while (len > 0) {
2278 			ND_TCHECK2(*tptr, 12);
2279 			ND_PRINT((ndo, "%u:%u:%u%s",
2280 				 EXTRACT_32BITS(tptr),
2281 				 EXTRACT_32BITS(tptr + 4),
2282 				 EXTRACT_32BITS(tptr + 8),
2283 				 (len > 12) ? ", " : ""));
2284                         tptr += 12;
2285                         len -= 12;
2286 		}
2287 		break;
2288 	default:
2289 	    ND_TCHECK2(*pptr,len);
2290             ND_PRINT((ndo, "\n\t    no Attribute %u decoder", atype)); /* we have no decoder for the attribute */
2291             if (ndo->ndo_vflag <= 1)
2292                 print_unknown_data(ndo, pptr, "\n\t    ", len);
2293             break;
2294 	}
2295         if (ndo->ndo_vflag > 1 && len) { /* omit zero length attributes*/
2296             ND_TCHECK2(*pptr,len);
2297             print_unknown_data(ndo, pptr, "\n\t    ", len);
2298         }
2299         return 1;
2300 
2301 trunc:
2302         return 0;
2303 }
2304 
2305 static void
2306 bgp_capabilities_print(netdissect_options *ndo,
2307                        const u_char *opt, int caps_len)
2308 {
2309 	int cap_type, cap_len, tcap_len, cap_offset;
2310         int i = 0;
2311 
2312         while (i < caps_len) {
2313                 ND_TCHECK2(opt[i], BGP_CAP_HEADER_SIZE);
2314                 cap_type=opt[i];
2315                 cap_len=opt[i+1];
2316                 tcap_len=cap_len;
2317                 ND_PRINT((ndo, "\n\t      %s (%u), length: %u",
2318                        tok2str(bgp_capcode_values, "Unknown",
2319                                   cap_type),
2320                        cap_type,
2321                        cap_len));
2322                 ND_TCHECK2(opt[i+2], cap_len);
2323                 switch (cap_type) {
2324                 case BGP_CAPCODE_MP:
2325                     ND_PRINT((ndo, "\n\t\tAFI %s (%u), SAFI %s (%u)",
2326                            tok2str(af_values, "Unknown",
2327                                       EXTRACT_16BITS(opt+i+2)),
2328                            EXTRACT_16BITS(opt+i+2),
2329                            tok2str(bgp_safi_values, "Unknown",
2330                                       opt[i+5]),
2331                            opt[i+5]));
2332                     break;
2333                 case BGP_CAPCODE_RESTART:
2334                     ND_PRINT((ndo, "\n\t\tRestart Flags: [%s], Restart Time %us",
2335                            ((opt[i+2])&0x80) ? "R" : "none",
2336                            EXTRACT_16BITS(opt+i+2)&0xfff));
2337                     tcap_len-=2;
2338                     cap_offset=4;
2339                     while(tcap_len>=4) {
2340                         ND_PRINT((ndo, "\n\t\t  AFI %s (%u), SAFI %s (%u), Forwarding state preserved: %s",
2341                                tok2str(af_values,"Unknown",
2342                                           EXTRACT_16BITS(opt+i+cap_offset)),
2343                                EXTRACT_16BITS(opt+i+cap_offset),
2344                                tok2str(bgp_safi_values,"Unknown",
2345                                           opt[i+cap_offset+2]),
2346                                opt[i+cap_offset+2],
2347                                ((opt[i+cap_offset+3])&0x80) ? "yes" : "no" ));
2348                         tcap_len-=4;
2349                         cap_offset+=4;
2350                     }
2351                     break;
2352                 case BGP_CAPCODE_RR:
2353                 case BGP_CAPCODE_RR_CISCO:
2354                     break;
2355                 case BGP_CAPCODE_AS_NEW:
2356 
2357                     /*
2358                      * Extract the 4 byte AS number encoded.
2359                      */
2360                     if (cap_len == 4) {
2361                         ND_PRINT((ndo, "\n\t\t 4 Byte AS %s",
2362                             as_printf(ndo, astostr, sizeof(astostr),
2363                             EXTRACT_32BITS(opt + i + 2))));
2364                     }
2365                     break;
2366                 case BGP_CAPCODE_ADD_PATH:
2367                     cap_offset=2;
2368                     if (tcap_len == 0) {
2369                         ND_PRINT((ndo, " (bogus)")); /* length */
2370                         break;
2371                     }
2372                     while (tcap_len > 0) {
2373                         if (tcap_len < 4) {
2374                             ND_PRINT((ndo, "\n\t\t(invalid)"));
2375                             break;
2376                         }
2377                         ND_PRINT((ndo, "\n\t\tAFI %s (%u), SAFI %s (%u), Send/Receive: %s",
2378                                   tok2str(af_values,"Unknown",EXTRACT_16BITS(opt+i+cap_offset)),
2379                                   EXTRACT_16BITS(opt+i+cap_offset),
2380                                   tok2str(bgp_safi_values,"Unknown",opt[i+cap_offset+2]),
2381                                   opt[i+cap_offset+2],
2382                                   tok2str(bgp_add_path_recvsend,"Bogus (0x%02x)",opt[i+cap_offset+3])
2383                         ));
2384                         tcap_len-=4;
2385                         cap_offset+=4;
2386                     }
2387                     break;
2388                 default:
2389                     ND_PRINT((ndo, "\n\t\tno decoder for Capability %u",
2390                            cap_type));
2391                     if (ndo->ndo_vflag <= 1)
2392                         print_unknown_data(ndo, &opt[i+2], "\n\t\t", cap_len);
2393                     break;
2394                 }
2395                 if (ndo->ndo_vflag > 1 && cap_len > 0) {
2396                     print_unknown_data(ndo, &opt[i+2], "\n\t\t", cap_len);
2397                 }
2398                 i += BGP_CAP_HEADER_SIZE + cap_len;
2399         }
2400         return;
2401 
2402 trunc:
2403 	ND_PRINT((ndo, "[|BGP]"));
2404 }
2405 
2406 static void
2407 bgp_open_print(netdissect_options *ndo,
2408                const u_char *dat, int length)
2409 {
2410 	struct bgp_open bgpo;
2411 	struct bgp_opt bgpopt;
2412 	const u_char *opt;
2413 	int i;
2414 
2415 	ND_TCHECK2(dat[0], BGP_OPEN_SIZE);
2416 	memcpy(&bgpo, dat, BGP_OPEN_SIZE);
2417 
2418 	ND_PRINT((ndo, "\n\t  Version %d, ", bgpo.bgpo_version));
2419 	ND_PRINT((ndo, "my AS %s, ",
2420 	    as_printf(ndo, astostr, sizeof(astostr), ntohs(bgpo.bgpo_myas))));
2421 	ND_PRINT((ndo, "Holdtime %us, ", ntohs(bgpo.bgpo_holdtime)));
2422 	ND_PRINT((ndo, "ID %s", ipaddr_string(ndo, &bgpo.bgpo_id)));
2423 	ND_PRINT((ndo, "\n\t  Optional parameters, length: %u", bgpo.bgpo_optlen));
2424 
2425         /* some little sanity checking */
2426         if (length < bgpo.bgpo_optlen+BGP_OPEN_SIZE)
2427             return;
2428 
2429 	/* ugly! */
2430 	opt = &((const struct bgp_open *)dat)->bgpo_optlen;
2431 	opt++;
2432 
2433 	i = 0;
2434 	while (i < bgpo.bgpo_optlen) {
2435 		ND_TCHECK2(opt[i], BGP_OPT_SIZE);
2436 		memcpy(&bgpopt, &opt[i], BGP_OPT_SIZE);
2437 		if (i + 2 + bgpopt.bgpopt_len > bgpo.bgpo_optlen) {
2438 			ND_PRINT((ndo, "\n\t     Option %d, length: %u", bgpopt.bgpopt_type, bgpopt.bgpopt_len));
2439 			break;
2440 		}
2441 
2442 		ND_PRINT((ndo, "\n\t    Option %s (%u), length: %u",
2443 		       tok2str(bgp_opt_values,"Unknown",
2444 				  bgpopt.bgpopt_type),
2445 		       bgpopt.bgpopt_type,
2446 		       bgpopt.bgpopt_len));
2447 
2448 		/* now let's decode the options we know*/
2449 		switch(bgpopt.bgpopt_type) {
2450 
2451 		case BGP_OPT_CAP:
2452 			bgp_capabilities_print(ndo, &opt[i+BGP_OPT_SIZE],
2453 			    bgpopt.bgpopt_len);
2454 			break;
2455 
2456 		case BGP_OPT_AUTH:
2457 		default:
2458 		       ND_PRINT((ndo, "\n\t      no decoder for option %u",
2459 			   bgpopt.bgpopt_type));
2460 		       break;
2461 		}
2462 		i += BGP_OPT_SIZE + bgpopt.bgpopt_len;
2463 	}
2464 	return;
2465 trunc:
2466 	ND_PRINT((ndo, "[|BGP]"));
2467 }
2468 
2469 static void
2470 bgp_update_print(netdissect_options *ndo,
2471                  const u_char *dat, int length)
2472 {
2473 	struct bgp bgp;
2474 	const u_char *p;
2475 	int withdrawn_routes_len;
2476 	int len;
2477 	int i;
2478 
2479 	ND_TCHECK2(dat[0], BGP_SIZE);
2480 	if (length < BGP_SIZE)
2481 		goto trunc;
2482 	memcpy(&bgp, dat, BGP_SIZE);
2483 	p = dat + BGP_SIZE;	/*XXX*/
2484 	length -= BGP_SIZE;
2485 
2486 	/* Unfeasible routes */
2487 	ND_TCHECK2(p[0], 2);
2488 	if (length < 2)
2489 		goto trunc;
2490 	withdrawn_routes_len = EXTRACT_16BITS(p);
2491 	p += 2;
2492 	length -= 2;
2493 	if (withdrawn_routes_len) {
2494 		/*
2495 		 * Without keeping state from the original NLRI message,
2496 		 * it's not possible to tell if this a v4 or v6 route,
2497 		 * so only try to decode it if we're not v6 enabled.
2498 	         */
2499 		ND_TCHECK2(p[0], withdrawn_routes_len);
2500 		if (length < withdrawn_routes_len)
2501 			goto trunc;
2502 		ND_PRINT((ndo, "\n\t  Withdrawn routes: %d bytes", withdrawn_routes_len));
2503 		p += withdrawn_routes_len;
2504 		length -= withdrawn_routes_len;
2505 	}
2506 
2507 	ND_TCHECK2(p[0], 2);
2508 	if (length < 2)
2509 		goto trunc;
2510 	len = EXTRACT_16BITS(p);
2511 	p += 2;
2512 	length -= 2;
2513 
2514         if (withdrawn_routes_len == 0 && len == 0 && length == 0) {
2515             /* No withdrawn routes, no path attributes, no NLRI */
2516             ND_PRINT((ndo, "\n\t  End-of-Rib Marker (empty NLRI)"));
2517             return;
2518         }
2519 
2520 	if (len) {
2521 		/* do something more useful!*/
2522 		while (len) {
2523 			int aflags, atype, alenlen, alen;
2524 
2525 			ND_TCHECK2(p[0], 2);
2526 			if (len < 2)
2527 			    goto trunc;
2528 			if (length < 2)
2529 			    goto trunc;
2530 			aflags = *p;
2531 			atype = *(p + 1);
2532 			p += 2;
2533 			len -= 2;
2534 			length -= 2;
2535 			alenlen = bgp_attr_lenlen(aflags, p);
2536 			ND_TCHECK2(p[0], alenlen);
2537 			if (len < alenlen)
2538 			    goto trunc;
2539 			if (length < alenlen)
2540 			    goto trunc;
2541 			alen = bgp_attr_len(aflags, p);
2542 			p += alenlen;
2543 			len -= alenlen;
2544 			length -= alenlen;
2545 
2546 			ND_PRINT((ndo, "\n\t  %s (%u), length: %u",
2547                               tok2str(bgp_attr_values, "Unknown Attribute",
2548 					 atype),
2549                               atype,
2550                               alen));
2551 
2552 			if (aflags) {
2553 				ND_PRINT((ndo, ", Flags [%s%s%s%s",
2554 					aflags & 0x80 ? "O" : "",
2555 					aflags & 0x40 ? "T" : "",
2556 					aflags & 0x20 ? "P" : "",
2557 					aflags & 0x10 ? "E" : ""));
2558 				if (aflags & 0xf)
2559 					ND_PRINT((ndo, "+%x", aflags & 0xf));
2560 				ND_PRINT((ndo, "]: "));
2561 			}
2562 			if (len < alen)
2563 				goto trunc;
2564 			if (length < alen)
2565 				goto trunc;
2566 			if (!bgp_attr_print(ndo, atype, p, alen))
2567 				goto trunc;
2568 			p += alen;
2569 			len -= alen;
2570 			length -= alen;
2571 		}
2572 	}
2573 
2574 	if (length) {
2575 		/*
2576 		 * XXX - what if they're using the "Advertisement of
2577 		 * Multiple Paths in BGP" feature:
2578 		 *
2579 		 * https://datatracker.ietf.org/doc/draft-ietf-idr-add-paths/
2580 		 *
2581 		 * http://tools.ietf.org/html/draft-ietf-idr-add-paths-06
2582 		 */
2583 		ND_PRINT((ndo, "\n\t  Updated routes:"));
2584 		while (length) {
2585 			char buf[MAXHOSTNAMELEN + 100];
2586 			i = decode_prefix4(ndo, p, length, buf, sizeof(buf));
2587 			if (i == -1) {
2588 				ND_PRINT((ndo, "\n\t    (illegal prefix length)"));
2589 				break;
2590 			} else if (i == -2)
2591 				goto trunc;
2592 			else if (i == -3)
2593 				goto trunc; /* bytes left, but not enough */
2594 			else {
2595 				ND_PRINT((ndo, "\n\t    %s", buf));
2596 				p += i;
2597 				length -= i;
2598 			}
2599 		}
2600 	}
2601 	return;
2602 trunc:
2603 	ND_PRINT((ndo, "[|BGP]"));
2604 }
2605 
2606 static void
2607 bgp_notification_print(netdissect_options *ndo,
2608                        const u_char *dat, int length)
2609 {
2610 	struct bgp_notification bgpn;
2611 	const u_char *tptr;
2612 
2613 	ND_TCHECK2(dat[0], BGP_NOTIFICATION_SIZE);
2614 	memcpy(&bgpn, dat, BGP_NOTIFICATION_SIZE);
2615 
2616         /* some little sanity checking */
2617         if (length<BGP_NOTIFICATION_SIZE)
2618             return;
2619 
2620 	ND_PRINT((ndo, ", %s (%u)",
2621 	       tok2str(bgp_notify_major_values, "Unknown Error",
2622 			  bgpn.bgpn_major),
2623 	       bgpn.bgpn_major));
2624 
2625         switch (bgpn.bgpn_major) {
2626 
2627         case BGP_NOTIFY_MAJOR_MSG:
2628             ND_PRINT((ndo, ", subcode %s (%u)",
2629 		   tok2str(bgp_notify_minor_msg_values, "Unknown",
2630 			      bgpn.bgpn_minor),
2631 		   bgpn.bgpn_minor));
2632             break;
2633         case BGP_NOTIFY_MAJOR_OPEN:
2634             ND_PRINT((ndo, ", subcode %s (%u)",
2635 		   tok2str(bgp_notify_minor_open_values, "Unknown",
2636 			      bgpn.bgpn_minor),
2637 		   bgpn.bgpn_minor));
2638             break;
2639         case BGP_NOTIFY_MAJOR_UPDATE:
2640             ND_PRINT((ndo, ", subcode %s (%u)",
2641 		   tok2str(bgp_notify_minor_update_values, "Unknown",
2642 			      bgpn.bgpn_minor),
2643 		   bgpn.bgpn_minor));
2644             break;
2645         case BGP_NOTIFY_MAJOR_FSM:
2646             ND_PRINT((ndo, " subcode %s (%u)",
2647 		   tok2str(bgp_notify_minor_fsm_values, "Unknown",
2648 			      bgpn.bgpn_minor),
2649 		   bgpn.bgpn_minor));
2650             break;
2651         case BGP_NOTIFY_MAJOR_CAP:
2652             ND_PRINT((ndo, " subcode %s (%u)",
2653 		   tok2str(bgp_notify_minor_cap_values, "Unknown",
2654 			      bgpn.bgpn_minor),
2655 		   bgpn.bgpn_minor));
2656             break;
2657         case BGP_NOTIFY_MAJOR_CEASE:
2658             ND_PRINT((ndo, ", subcode %s (%u)",
2659 		   tok2str(bgp_notify_minor_cease_values, "Unknown",
2660 			      bgpn.bgpn_minor),
2661 		   bgpn.bgpn_minor));
2662 
2663 	    /* draft-ietf-idr-cease-subcode-02 mentions optionally 7 bytes
2664              * for the maxprefix subtype, which may contain AFI, SAFI and MAXPREFIXES
2665              */
2666 	    if(bgpn.bgpn_minor == BGP_NOTIFY_MINOR_CEASE_MAXPRFX && length >= BGP_NOTIFICATION_SIZE + 7) {
2667 		tptr = dat + BGP_NOTIFICATION_SIZE;
2668 		ND_TCHECK2(*tptr, 7);
2669 		ND_PRINT((ndo, ", AFI %s (%u), SAFI %s (%u), Max Prefixes: %u",
2670 		       tok2str(af_values, "Unknown",
2671 				  EXTRACT_16BITS(tptr)),
2672 		       EXTRACT_16BITS(tptr),
2673 		       tok2str(bgp_safi_values, "Unknown", *(tptr+2)),
2674 		       *(tptr+2),
2675 		       EXTRACT_32BITS(tptr+3)));
2676 	    }
2677             break;
2678         default:
2679             break;
2680         }
2681 
2682 	return;
2683 trunc:
2684 	ND_PRINT((ndo, "[|BGP]"));
2685 }
2686 
2687 static void
2688 bgp_route_refresh_print(netdissect_options *ndo,
2689                         const u_char *pptr, int len)
2690 {
2691         const struct bgp_route_refresh *bgp_route_refresh_header;
2692 
2693 	ND_TCHECK2(pptr[0], BGP_ROUTE_REFRESH_SIZE);
2694 
2695         /* some little sanity checking */
2696         if (len<BGP_ROUTE_REFRESH_SIZE)
2697             return;
2698 
2699         bgp_route_refresh_header = (const struct bgp_route_refresh *)pptr;
2700 
2701         ND_PRINT((ndo, "\n\t  AFI %s (%u), SAFI %s (%u)",
2702                tok2str(af_values,"Unknown",
2703 			  /* this stinks but the compiler pads the structure
2704 			   * weird */
2705 			  EXTRACT_16BITS(&bgp_route_refresh_header->afi)),
2706                EXTRACT_16BITS(&bgp_route_refresh_header->afi),
2707                tok2str(bgp_safi_values,"Unknown",
2708 			  bgp_route_refresh_header->safi),
2709                bgp_route_refresh_header->safi));
2710 
2711         if (ndo->ndo_vflag > 1) {
2712             ND_TCHECK2(*pptr, len);
2713             print_unknown_data(ndo, pptr, "\n\t  ", len);
2714         }
2715 
2716         return;
2717 trunc:
2718 	ND_PRINT((ndo, "[|BGP]"));
2719 }
2720 
2721 static int
2722 bgp_header_print(netdissect_options *ndo,
2723                  const u_char *dat, int length)
2724 {
2725 	struct bgp bgp;
2726 
2727 	ND_TCHECK2(dat[0], BGP_SIZE);
2728 	memcpy(&bgp, dat, BGP_SIZE);
2729 	ND_PRINT((ndo, "\n\t%s Message (%u), length: %u",
2730                tok2str(bgp_msg_values, "Unknown", bgp.bgp_type),
2731                bgp.bgp_type,
2732                length));
2733 
2734 	switch (bgp.bgp_type) {
2735 	case BGP_OPEN:
2736 		bgp_open_print(ndo, dat, length);
2737 		break;
2738 	case BGP_UPDATE:
2739 		bgp_update_print(ndo, dat, length);
2740 		break;
2741 	case BGP_NOTIFICATION:
2742 		bgp_notification_print(ndo, dat, length);
2743 		break;
2744         case BGP_KEEPALIVE:
2745                 break;
2746         case BGP_ROUTE_REFRESH:
2747                 bgp_route_refresh_print(ndo, dat, length);
2748                 break;
2749         default:
2750                 /* we have no decoder for the BGP message */
2751                 ND_TCHECK2(*dat, length);
2752                 ND_PRINT((ndo, "\n\t  no Message %u decoder", bgp.bgp_type));
2753                 print_unknown_data(ndo, dat, "\n\t  ", length);
2754                 break;
2755 	}
2756 	return 1;
2757 trunc:
2758 	ND_PRINT((ndo, "[|BGP]"));
2759 	return 0;
2760 }
2761 
2762 void
2763 bgp_print(netdissect_options *ndo,
2764           const u_char *dat, int length)
2765 {
2766 	const u_char *p;
2767 	const u_char *ep;
2768 	const u_char *start;
2769 	const u_char marker[] = {
2770 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2771 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2772 	};
2773 	struct bgp bgp;
2774 	uint16_t hlen;
2775 
2776 	ep = dat + length;
2777 	if (ndo->ndo_snapend < dat + length)
2778 		ep = ndo->ndo_snapend;
2779 
2780 	ND_PRINT((ndo, ": BGP"));
2781 
2782         if (ndo->ndo_vflag < 1) /* lets be less chatty */
2783                 return;
2784 
2785 	p = dat;
2786 	start = p;
2787 	while (p < ep) {
2788 		if (!ND_TTEST2(p[0], 1))
2789 			break;
2790 		if (p[0] != 0xff) {
2791 			p++;
2792 			continue;
2793 		}
2794 
2795 		if (!ND_TTEST2(p[0], sizeof(marker)))
2796 			break;
2797 		if (memcmp(p, marker, sizeof(marker)) != 0) {
2798 			p++;
2799 			continue;
2800 		}
2801 
2802 		/* found BGP header */
2803 		ND_TCHECK2(p[0], BGP_SIZE);	/*XXX*/
2804 		memcpy(&bgp, p, BGP_SIZE);
2805 
2806 		if (start != p)
2807 			ND_PRINT((ndo, " [|BGP]"));
2808 
2809 		hlen = ntohs(bgp.bgp_len);
2810 		if (hlen < BGP_SIZE) {
2811 			ND_PRINT((ndo, "\n[|BGP Bogus header length %u < %u]", hlen,
2812 			    BGP_SIZE));
2813 			break;
2814 		}
2815 
2816 		if (ND_TTEST2(p[0], hlen)) {
2817 			if (!bgp_header_print(ndo, p, hlen))
2818 				return;
2819 			p += hlen;
2820 			start = p;
2821 		} else {
2822 			ND_PRINT((ndo, "\n[|BGP %s]",
2823 			       tok2str(bgp_msg_values,
2824 					  "Unknown Message Type",
2825 					  bgp.bgp_type)));
2826 			break;
2827 		}
2828 	}
2829 
2830 	return;
2831 
2832 trunc:
2833 	ND_PRINT((ndo, " [|BGP]"));
2834 }
2835 
2836 /*
2837  * Local Variables:
2838  * c-style: whitesmith
2839  * c-basic-offset: 4
2840  * End:
2841  */
2842