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