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