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