change the signature that asn2wrs generates for functions to marm all parameters...
[obnox/wireshark/wip.git] / epan / dissectors / packet-bgp.c
1 /* packet-bgp.c
2  * Routines for BGP packet dissection.
3  * Copyright 1999, Jun-ichiro itojun Hagino <itojun@itojun.org>
4  *
5  * $Id$
6  *
7  * Supports:
8  * RFC1771 A Border Gateway Protocol 4 (BGP-4)
9  * RFC1965 Autonomous System Confederations for BGP
10  * RFC1997 BGP Communities Attribute
11  * RFC2547 BGP/MPLS VPNs
12  * RFC2796 BGP Route Reflection An alternative to full mesh IBGP
13  * RFC2842 Capabilities Advertisement with BGP-4
14  * RFC2858 Multiprotocol Extensions for BGP-4
15  * RFC2918 Route Refresh Capability for BGP-4
16  * RFC3107 Carrying Label Information in BGP-4
17  * draft-ietf-idr-as4bytes-06
18  * draft-ietf-idr-dynamic-cap-03
19  * draft-ietf-idr-bgp-ext-communities-05
20  *
21  * TODO:
22  * Destination Preference Attribute for BGP (work in progress)
23  * RFC1863 A BGP/IDRP Route Server alternative to a full mesh routing
24  *
25  * Wireshark - Network traffic analyzer
26  * By Gerald Combs <gerald@wireshark.org>
27  * Copyright 1998 Gerald Combs
28  *
29  * This program is free software; you can redistribute it and/or
30  * modify it under the terms of the GNU General Public License
31  * as published by the Free Software Foundation; either version 2
32  * of the License, or (at your option) any later version.
33  *
34  * This program is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  * GNU General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License
40  * along with this program; if not, write to the Free Software
41  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
42  */
43
44 #ifdef HAVE_CONFIG_H
45 # include "config.h"
46 #endif
47
48 #include <stdio.h>
49 #include <stdlib.h>
50
51 #include <string.h>
52 #include <glib.h>
53
54 #include <epan/packet.h>
55 #include <epan/addr_and_mask.h>
56 #include "packet-bgp.h"
57 #include "packet-ipv6.h"
58 #include "packet-frame.h"
59 #include <epan/afn.h>
60 #include <epan/prefs.h>
61 #include <epan/emem.h>
62
63 #define MAX_STR_LEN 256
64
65 static const value_string bgptypevals[] = {
66     { BGP_OPEN, "OPEN Message" },
67     { BGP_UPDATE, "UPDATE Message" },
68     { BGP_NOTIFICATION, "NOTIFICATION Message" },
69     { BGP_KEEPALIVE, "KEEPALIVE Message" },
70     { BGP_ROUTE_REFRESH, "ROUTE-REFRESH Message" },
71     { BGP_CAPABILITY, "CAPABILITY Message" },
72     { BGP_ROUTE_REFRESH_CISCO, "Cisco ROUTE-REFRESH Message" },
73     { 0, NULL },
74 };
75
76 static const value_string bgpnotify_major[] = {
77     { 1, "Message Header Error" },
78     { 2, "OPEN Message Error" },
79     { 3, "UPDATE Message Error" },
80     { 4, "Hold Timer Expired" },
81     { 5, "Finite State Machine Error" },
82     { 6, "Cease" },
83     { 7, "CAPABILITY Message Error" },
84     { 0, NULL },
85 };
86
87 static const value_string bgpnotify_minor_1[] = {
88     { 1, "Connection Not Synchronized" },
89     { 2, "Bad Message Length" },
90     { 3, "Bad Message Type" },
91     { 0, NULL },
92 };
93
94 static const value_string bgpnotify_minor_2[] = {
95     { 1, "Unsupported Version Number" },
96     { 2, "Bad Peer AS" },
97     { 3, "Bad BGP Identifier" },
98     { 4, "Unsupported Optional Parameter" },
99     { 5, "Authentication Failure" },
100     { 6, "Unacceptable Hold Time" },
101     { 7, "Unsupported Capability" },
102     { 0, NULL },
103 };
104
105 static const value_string bgpnotify_minor_3[] = {
106     { 1, "Malformed Attribute List" },
107     { 2, "Unrecognized Well-known Attribute" },
108     { 3, "Missing Well-known Attribute" },
109     { 4, "Attribute Flags Error" },
110     { 5, "Attribute Length Error" },
111     { 6, "Invalid ORIGIN Attribute" },
112     { 7, "AS Routing Loop" },
113     { 8, "Invalid NEXT_HOP Attribute" },
114     { 9, "Optional Attribute Error" },
115     { 10, "Invalid Network Field" },
116     { 11, "Malformed AS_PATH" },
117     { 0, NULL },
118 };
119
120 /* draft-ietf-idr-cease-subcode-02 */
121 static const value_string bgpnotify_minor_6[] = {
122     { 1,                        "Maximum Number of Prefixes Reached"},
123     { 2,                        "Administratively Shutdown"},
124     { 3,                        "Peer Unconfigured"},
125     { 4,                        "Administratively Reset"},
126     { 5,                        "Connection Rejected"},
127     { 6,                        "Other Configuration Change"},
128     { 7,                        "Connection Collision Resolution"},
129     { 0, NULL},
130 };
131
132 static const value_string bgpnotify_minor_7[] = {
133     { 1, "Invalid Action Value" },
134     { 2, "Invalid Capability Length" },
135     { 3, "Malformed Capability Value" },
136     { 4, "Unsupported Capability Code" },
137     { 0, NULL },
138 };
139
140 static const value_string *bgpnotify_minor[] = {
141     NULL,
142     bgpnotify_minor_1, /* open */
143     bgpnotify_minor_2, /* update */
144     bgpnotify_minor_3, /* notification */
145     NULL,              /* hold-timer expired */
146     NULL,              /* FSM error */
147     bgpnotify_minor_6, /* cease */
148     bgpnotify_minor_7  /* capability */
149 };
150
151 static const value_string bgpattr_origin[] = {
152     { 0, "IGP" },
153     { 1, "EGP" },
154     { 2, "INCOMPLETE" },
155     { 0, NULL },
156 };
157
158 static const value_string as_segment_type[] = {
159     { 1, "AS_SET" },
160     { 2, "AS_SEQUENCE" },
161 /* RFC1965 has the wrong values, corrected in  */
162 /* draft-ietf-idr-bgp-confed-rfc1965bis-01.txt */
163     { 4, "AS_CONFED_SET" },
164     { 3, "AS_CONFED_SEQUENCE" },
165     { 0, NULL },
166 };
167
168 static const value_string bgpattr_type[] = {
169     { BGPTYPE_ORIGIN, "ORIGIN" },
170     { BGPTYPE_AS_PATH, "AS_PATH" },
171     { BGPTYPE_NEXT_HOP, "NEXT_HOP" },
172     { BGPTYPE_MULTI_EXIT_DISC, "MULTI_EXIT_DISC" },
173     { BGPTYPE_LOCAL_PREF, "LOCAL_PREF" },
174     { BGPTYPE_ATOMIC_AGGREGATE, "ATOMIC_AGGREGATE" },
175     { BGPTYPE_AGGREGATOR, "AGGREGATOR" },
176     { BGPTYPE_COMMUNITIES, "COMMUNITIES" },
177     { BGPTYPE_ORIGINATOR_ID, "ORIGINATOR_ID" },
178     { BGPTYPE_CLUSTER_LIST, "CLUSTER_LIST" },
179     { BGPTYPE_MP_REACH_NLRI, "MP_REACH_NLRI" },
180     { BGPTYPE_MP_UNREACH_NLRI, "MP_UNREACH_NLRI" },
181     { BGPTYPE_EXTENDED_COMMUNITY, "EXTENDED_COMMUNITIES" },
182     { BGPTYPE_NEW_AS_PATH, "NEW_AS_PATH" },
183     { BGPTYPE_NEW_AGGREGATOR, "NEW_AGGREGATOR" },
184     { BGPTYPE_SAFI_SPECIFIC_ATTR, "SAFI_SPECIFIC_ATTRIBUTE" },
185     { 0, NULL },
186 };
187
188 static const value_string bgpext_com_type[] = {
189     { BGP_EXT_COM_RT_0, "Route Target" },
190     { BGP_EXT_COM_RT_1, "Route Target" },
191     { BGP_EXT_COM_RT_2, "Route Target" },
192     { BGP_EXT_COM_RO_0, "Route Origin" },
193     { BGP_EXT_COM_RO_1, "Route Origin" },
194     { BGP_EXT_COM_RO_2, "Route Origin" },
195     { BGP_EXT_COM_LINKBAND, "Link Bandwidth" },
196     { BGP_EXT_COM_VPN_ORIGIN, "OSPF Domain" },
197     { BGP_EXT_COM_OSPF_RTYPE, "OSPF Route Type" },
198     { BGP_EXT_COM_OSPF_RID, "OSPF Router ID" },
199     { BGP_EXT_COM_L2INFO, "Layer 2 Information" },
200     { 0, NULL },
201 };
202
203 static const value_string bgp_ssa_type[] = {
204     { BGP_SSA_L2TPv3 , "L2TPv3 Tunnel" },
205     { BGP_SSA_mGRE , "mGRE Tunnel" },
206     { BGP_SSA_IPSec , "IPSec Tunnel" },
207     { BGP_SSA_MPLS , "MPLS Tunnel" },
208     { 0, NULL },
209 };
210
211 static const value_string bgp_l2vpn_encaps[] = {
212     { 0,                      "Reserved"},
213     { 1,                      "Frame Relay"},
214     { 2,                      "ATM AAL5 VCC transport"},
215     { 3,                      "ATM transparent cell transport"},
216     { 4,                      "Ethernet VLAN"},
217     { 5,                      "Ethernet"},
218     { 6,                      "Cisco-HDLC"},
219     { 7,                      "PPP"},
220     { 8,                      "CEM"},
221     { 9,                      "ATM VCC cell transport"},
222     { 10,                     "ATM VPC cell transport"},
223     { 11,                     "MPLS"},
224     { 12,                     "VPLS"},
225     { 64,                     "IP-interworking"},
226     { 0, NULL},
227 };
228
229 static const value_string bgpext_ospf_rtype[] = {
230   { BGP_OSPF_RTYPE_RTR, "Router" },
231   { BGP_OSPF_RTYPE_NET, "Network" },
232   { BGP_OSPF_RTYPE_SUM, "Summary" },
233   { BGP_OSPF_RTYPE_EXT, "External" },
234   { BGP_OSPF_RTYPE_NSSA,"NSSA External" },
235   { BGP_OSPF_RTYPE_SHAM,"MPLS-VPN Sham" },
236   { 0, NULL },
237 };
238
239 /* Subsequent address family identifier, RFC2858 */
240 static const value_string bgpattr_nlri_safi[] = {
241     { 0, "Reserved" },
242     { SAFNUM_UNICAST, "Unicast" },
243     { SAFNUM_MULCAST, "Multicast" },
244     { SAFNUM_UNIMULC, "Unicast+Multicast" },
245     { SAFNUM_MPLS_LABEL, "Labeled Unicast"},
246     { SAFNUM_TUNNEL, "Tunnel"},
247     { SAFNUM_VPLS, "VPLS"},
248     { SAFNUM_LAB_VPNUNICAST, "Labeled VPN Unicast" },        /* draft-rosen-rfc2547bis-03 */
249     { SAFNUM_LAB_VPNMULCAST, "Labeled VPN Multicast" },
250     { SAFNUM_LAB_VPNUNIMULC, "Labeled VPN Unicast+Multicast" },
251     { 0, NULL },
252 };
253
254 /* ORF Type, draft-ietf-idr-route-filter-04.txt */
255 static const value_string orf_type_vals[] = {
256     { 2,        "Communities ORF-Type" },
257     { 3,        "Extended Communities ORF-Type" },
258     { 128,      "Cisco PrefixList ORF-Type" },
259     { 129,      "Cisco CommunityList ORF-Type" },
260     { 130,      "Cisco Extended CommunityList ORF-Type" },
261     { 131,      "Cisco AsPathList ORF-Type" },
262     { 0,        NULL },
263 };
264
265 /* ORF Send/Receive, draft-ietf-idr-route-filter-04.txt */
266 static const value_string orf_send_recv_vals[] = {
267     { 1,        "Receive" },
268     { 2,        "Send" },
269     { 3,        "Both" },
270     { 0,        NULL },
271 };
272
273 /* ORF Send/Receive, draft-ietf-idr-route-filter-04.txt */
274 static const value_string orf_when_vals[] = {
275     { 1,        "Immediate" },
276     { 2,        "Defer" },
277     { 0,        NULL },
278 };
279
280 static const value_string orf_entry_action_vals[] = {
281     { 0,        "Add" },
282     { 0x40,     "Remove" },
283     { 0x80,     "RemoveAll" },
284     { 0,        NULL },
285 };
286
287 static const value_string orf_entry_match_vals[] = {
288     { 0,        "Permit" },
289     { 0x20,     "Deny" },
290     { 0,        NULL },
291 };
292
293 static const value_string capability_vals[] = {
294     { BGP_CAPABILITY_RESERVED, "Reserved capability" },
295     { BGP_CAPABILITY_MULTIPROTOCOL, "Multiprotocol extensions capability" },
296     { BGP_CAPABILITY_ROUTE_REFRESH, "Route refresh capability" },
297     { BGP_CAPABILITY_COOPERATIVE_ROUTE_FILTERING, "Cooperative route filtering capability" },
298     { BGP_CAPABILITY_GRACEFUL_RESTART, "Graceful Restart capability" },
299     { BGP_CAPABILITY_4_OCTET_AS_NUMBER, "Support for 4-octet AS number capability" },
300     { BGP_CAPABILITY_DYNAMIC_CAPABILITY, "Support for Dynamic capability" },
301     { BGP_CAPABILITY_ROUTE_REFRESH_CISCO, "Route refresh capability" },
302     { BGP_CAPABILITY_ORF_CISCO, "Cooperative route filtering capability" },
303     { 0, NULL}
304 };
305
306 /* Capability Message action code */
307 static const value_string bgpcap_action[] = {
308     { 0, "advertising a capability" },
309     { 1, "removing a capability" },
310     { 0, NULL },
311 };
312
313
314 /* Maximal size of an IP address string */
315 #define MAX_SIZE_OF_IP_ADDR_STRING      16
316
317 static int proto_bgp = -1;
318 static int hf_bgp_type = -1;
319 static int hf_bgp_next_hop = -1;
320 static int hf_bgp_as_path = -1;
321 static int hf_bgp_cluster_identifier = -1;
322 static int hf_bgp_community_as = -1;
323 static int hf_bgp_community_value = -1;
324 static int hf_bgp_origin = -1;
325 static int hf_bgp_cluster_list = -1;
326 static int hf_bgp_originator_id = -1;
327 static int hf_bgp_ssa_t = -1;
328 static int hf_bgp_ssa_type = -1;
329 static int hf_bgp_ssa_len = -1;
330 static int hf_bgp_ssa_value = -1;
331 static int hf_bgp_ssa_l2tpv3_pref = -1;
332 static int hf_bgp_ssa_l2tpv3_s = -1;
333 static int hf_bgp_ssa_l2tpv3_unused = -1;
334 static int hf_bgp_ssa_l2tpv3_cookie_len = -1;
335 static int hf_bgp_ssa_l2tpv3_session_id = -1;
336 static int hf_bgp_ssa_l2tpv3_cookie = -1;
337 static int hf_bgp_local_pref = -1;
338 static int hf_bgp_multi_exit_disc = -1;
339 static int hf_bgp_aggregator_as = -1;
340 static int hf_bgp_aggregator_origin = -1;
341 static int hf_bgp_mp_reach_nlri_ipv4_prefix = -1;
342 static int hf_bgp_mp_unreach_nlri_ipv4_prefix = -1;
343 static int hf_bgp_mp_nlri_tnl_id = -1;
344 static int hf_bgp_withdrawn_prefix = -1;
345 static int hf_bgp_nlri_prefix = -1;
346
347 static gint ett_bgp = -1;
348 static gint ett_bgp_prefix = -1;
349 static gint ett_bgp_unfeas = -1;
350 static gint ett_bgp_attrs = -1;
351 static gint ett_bgp_attr = -1;
352 static gint ett_bgp_attr_flags = -1;
353 static gint ett_bgp_mp_nhna = -1;
354 static gint ett_bgp_mp_reach_nlri = -1;
355 static gint ett_bgp_mp_unreach_nlri = -1;
356 static gint ett_bgp_mp_snpa = -1;
357 static gint ett_bgp_nlri = -1;
358 static gint ett_bgp_open = -1;
359 static gint ett_bgp_update = -1;
360 static gint ett_bgp_notification = -1;
361 static gint ett_bgp_route_refresh = -1; /* ROUTE-REFRESH message tree */
362 static gint ett_bgp_capability = -1;
363 static gint ett_bgp_as_paths = -1;
364 static gint ett_bgp_as_path_segments = -1;
365 static gint ett_bgp_communities = -1;
366 static gint ett_bgp_cluster_list = -1;  /* cluster list tree          */
367 static gint ett_bgp_options = -1;       /* optional parameters tree   */
368 static gint ett_bgp_option = -1;        /* an optional parameter tree */
369 static gint ett_bgp_extended_communities = -1 ; /* extended communities list tree */
370 static gint ett_bgp_ssa = -1;           /* safi specific attribute */
371 static gint ett_bgp_ssa_subtree = -1;   /* safi specific attribute Subtrees */
372 static gint ett_bgp_orf = -1;           /* orf (outbound route filter) tree */
373 static gint ett_bgp_orf_entry = -1;             /* orf entry tree */
374
375 /* desegmentation */
376 static gboolean bgp_desegment = TRUE;
377
378 static gint bgp_asn_len = 0;
379
380 /*
381  * Decode an IPv4 prefix.
382  */
383 static int
384 decode_prefix4(proto_tree *tree, int hf_addr, tvbuff_t *tvb, gint offset,
385     guint16 tlen, const char *tag)
386 {
387     proto_item *ti;
388     proto_tree *prefix_tree;
389     union {
390        guint8 addr_bytes[4];
391        guint32 addr;
392     } ip_addr;        /* IP address                         */
393     guint8 plen;      /* prefix length                      */
394     int    length;    /* number of octets needed for prefix */
395
396     /* snarf length and prefix */
397     plen = tvb_get_guint8(tvb, offset);
398     length = ipv4_addr_and_mask(tvb, offset + 1, ip_addr.addr_bytes, plen);
399     if (length < 0) {
400         proto_tree_add_text(tree, tvb, offset, 1, "%s length %u invalid (> 32)",
401             tag, plen);
402         return -1;
403     }
404
405     /* put prefix into protocol tree */
406     ti = proto_tree_add_text(tree, tvb, offset,
407             tlen != 0 ? tlen : 1 + length, "%s/%u",
408         ip_to_str(ip_addr.addr_bytes), plen);
409     prefix_tree = proto_item_add_subtree(ti, ett_bgp_prefix);
410     proto_tree_add_text(prefix_tree, tvb, offset, 1, "%s prefix length: %u",
411         tag, plen);
412     if (hf_addr != -1) {
413         proto_tree_add_ipv4(prefix_tree, hf_addr, tvb, offset + 1, length,
414             ip_addr.addr);
415     } else {
416         proto_tree_add_text(prefix_tree, tvb, offset + 1, length,
417             "%s prefix: %s", tag, ip_to_str(ip_addr.addr_bytes));
418     }
419     return(1 + length);
420 }
421
422 /*
423  * Decode an IPv6 prefix.
424  */
425 static int
426 decode_prefix6(proto_tree *tree, int hf_addr, tvbuff_t *tvb, gint offset,
427     guint16 tlen, const char *tag)
428 {
429     proto_item        *ti;
430     proto_tree        *prefix_tree;
431     struct e_in6_addr addr;     /* IPv6 address                       */
432     int               plen;     /* prefix length                      */
433     int               length;   /* number of octets needed for prefix */
434
435     /* snarf length and prefix */
436     plen = tvb_get_guint8(tvb, offset);
437     length = ipv6_addr_and_mask(tvb, offset + 1, &addr, plen);
438     if (length < 0) {
439         proto_tree_add_text(tree, tvb, offset, 1, "%s length %u invalid",
440             tag, plen);
441         return -1;
442     }
443
444     /* put prefix into protocol tree */
445     ti = proto_tree_add_text(tree, tvb, offset,
446             tlen != 0 ? tlen : 1 + length, "%s/%u",
447             ip6_to_str(&addr), plen);
448     prefix_tree = proto_item_add_subtree(ti, ett_bgp_prefix);
449     proto_tree_add_text(prefix_tree, tvb, offset, 1, "%s prefix length: %u",
450         tag, plen);
451     if (hf_addr != -1) {
452         proto_tree_add_ipv6(prefix_tree, hf_addr, tvb, offset + 1, length,
453             addr.bytes);
454     } else {
455         proto_tree_add_text(prefix_tree, tvb, offset + 1, length,
456             "%s prefix: %s", tag, ip6_to_str(&addr));
457     }
458     return(1 + length);
459 }
460
461
462
463 /*
464  * Decode an MPLS label stack
465  * XXX - We should change *buf to **buf, use ep_alloc() and drop the buflen
466  * argument.
467  */
468 static guint
469 decode_MPLS_stack(tvbuff_t *tvb, gint offset, char *buf, size_t buflen)
470 {
471     guint32     label_entry;    /* an MPLS label enrty (label + COS field + stack bit   */
472     gint        index;          /* index for the label stack */
473     char       *bufptr;
474
475     index = offset ;
476     label_entry = 0x000000 ;
477
478     buf[0] = '\0' ;
479     bufptr=buf;
480
481     while ((label_entry & 0x000001) == 0) {
482
483         label_entry = tvb_get_ntoh24(tvb, index) ;
484
485         /* withdrawn routes may contain 0 or 0x800000 in the first label */
486         if((index-offset)==0&&(label_entry==0||label_entry==0x800000)) {
487             g_snprintf(bufptr, buflen-(bufptr-buf), "0 (withdrawn)");
488             return (1);
489         }
490
491         bufptr+=MIN(buflen-(bufptr-buf),
492                     (unsigned)g_snprintf(bufptr, buflen-(bufptr-buf), "%u%s", 
493                         (label_entry >> 4), 
494                         ((label_entry & 0x000001) == 0) ? "," : " (bottom)"));
495         
496         index += 3 ;
497
498         if ((label_entry & 0x000001) == 0) {
499             /* real MPLS multi-label stack in BGP? - maybe later; for now, it must be a bogus packet */
500             g_snprintf(bufptr, buflen-(bufptr-buf), " (BOGUS: Bottom of Stack NOT set!)");
501             break;
502         }
503     }
504
505     return((index - offset) / 3);
506 }
507
508 /*
509  * Decode a multiprotocol address
510  */
511
512 static int
513 mp_addr_to_str (guint16 afi, guint8 safi, tvbuff_t *tvb, gint offset, char *buf, int buf_len)
514 {
515     int                 length;                         /* length of the address in byte */
516     guint32             ip4addr,ip4addr2;               /* IPv4 address                 */
517     guint16             rd_type;                        /* Route Distinguisher type     */
518     struct e_in6_addr   ip6addr;                        /* IPv6 address                 */
519
520     buf[0]=0;
521
522     length = 0 ;
523     switch (afi) {
524         case AFNUM_INET:
525                 switch (safi) {
526                         case SAFNUM_UNICAST:
527                         case SAFNUM_MULCAST:
528                         case SAFNUM_UNIMULC:
529                         case SAFNUM_MPLS_LABEL:
530                         case SAFNUM_TUNNEL:
531                                 length = 4 ;
532                                 ip4addr = tvb_get_ipv4(tvb, offset);
533                                 g_snprintf(buf, buf_len, "%s", ip_to_str((guint8 *)&ip4addr));
534                                 break;
535                         case SAFNUM_LAB_VPNUNICAST:
536                         case SAFNUM_LAB_VPNMULCAST:
537                         case SAFNUM_LAB_VPNUNIMULC:
538                                 rd_type=tvb_get_ntohs(tvb,offset) ;
539                                 switch (rd_type) {
540                                         case FORMAT_AS2_LOC:
541                                                 length = 8 + sizeof(ip4addr);
542                                                 ip4addr = tvb_get_ipv4(tvb, offset + 8);   /* Next Hop */
543                                                 g_snprintf(buf, buf_len, "Empty Label Stack RD=%u:%u IPv4=%s",
544                                                                 tvb_get_ntohs(tvb, offset + 2),
545                                                                 tvb_get_ntohl(tvb, offset + 4),
546                                                                 ip_to_str((guint8 *)&ip4addr));
547                                                 break;
548                                         case FORMAT_IP_LOC:
549                                                 length = 8 + sizeof(ip4addr);
550                                                 ip4addr = tvb_get_ipv4(tvb, offset + 2);   /* IP part of the RD            */
551                                                 ip4addr2 = tvb_get_ipv4(tvb, offset + 8);  /* Next Hop   */
552                                                 g_snprintf(buf, buf_len, "Empty Label Stack RD=%s:%u IPv4=%s",
553                                                                 ip_to_str((guint8 *)&ip4addr),
554                                                                 tvb_get_ntohs(tvb, offset + 6),
555                                                                 ip_to_str((guint8 *)&ip4addr2));
556                                                 break ;
557                                         default:
558                                                 length = 0 ;
559                                                 g_snprintf(buf, buf_len, "Unknown (0x%04x) labeled VPN IPv4 address format",rd_type);
560                                                 break;
561                                 }
562                                 break;
563                         default:
564                             length = 0 ;
565                             g_snprintf(buf, buf_len, "Unknown SAFI (%u) for AFI %u", safi, afi);
566                             break;
567                 }
568                 break;
569         case AFNUM_INET6:
570                 switch (safi) {
571                         case SAFNUM_UNICAST:
572                         case SAFNUM_MULCAST:
573                         case SAFNUM_UNIMULC:
574                         case SAFNUM_MPLS_LABEL:
575                         case SAFNUM_TUNNEL:
576                             length = 16 ;
577                             tvb_get_ipv6(tvb, offset, &ip6addr);
578                             g_snprintf(buf, buf_len, "%s", ip6_to_str(&ip6addr));
579                             break;
580                         case SAFNUM_LAB_VPNUNICAST:
581                         case SAFNUM_LAB_VPNMULCAST:
582                         case SAFNUM_LAB_VPNUNIMULC:
583                                 rd_type=tvb_get_ntohs(tvb,offset) ;
584                                 switch (rd_type) {
585                                         case FORMAT_AS2_LOC:
586                                                 length = 8 + 16;
587                                                 tvb_get_ipv6(tvb, offset + 8, &ip6addr); /* Next Hop */
588                                                 g_snprintf(buf, buf_len, "Empty Label Stack RD=%u:%u IPv6=%s",
589                                                                 tvb_get_ntohs(tvb, offset + 2),
590                                                                 tvb_get_ntohl(tvb, offset + 4),
591                                                                 ip6_to_str(&ip6addr));
592                                                 break;
593                                         case FORMAT_IP_LOC:
594                                                 length = 8 + 16;
595                                                 ip4addr = tvb_get_ipv4(tvb, offset + 2);   /* IP part of the RD            */
596                                                 tvb_get_ipv6(tvb, offset + 8, &ip6addr); /* Next Hop */
597                                                 g_snprintf(buf, buf_len, "Empty Label Stack RD=%s:%u IPv6=%s",
598                                                                 ip_to_str((guint8 *)&ip4addr),
599                                                                 tvb_get_ntohs(tvb, offset + 6),
600                                                                 ip6_to_str(&ip6addr));
601                                                 break ;
602                                         default:
603                                                 length = 0 ;
604                                                 g_snprintf(buf, buf_len, "Unknown (0x%04x) labeled VPN IPv6 address format",rd_type);
605                                                 break;
606                                 }
607                                 break;
608                         default:
609                             length = 0 ;
610                             g_snprintf(buf, buf_len, "Unknown SAFI (%u) for AFI %u", safi, afi);
611                             break;
612                 }
613                 break;
614        case AFNUM_L2VPN:
615        case AFNUM_L2VPN_OLD:
616                 switch (safi) {
617                         case SAFNUM_LAB_VPNUNICAST: /* only labeles prefixes do make sense */
618                         case SAFNUM_LAB_VPNMULCAST:
619                         case SAFNUM_LAB_VPNUNIMULC:
620                         case SAFNUM_VPLS:
621                             length = 4; /* the next-hop is simply an ipv4 addr */
622                             ip4addr = tvb_get_ipv4(tvb, offset + 0);
623                             g_snprintf(buf, buf_len, "IPv4=%s",
624                                      ip_to_str((guint8 *)&ip4addr));
625                             break;
626                         default:
627                             length = 0 ;
628                             g_snprintf(buf, buf_len, "Unknown SAFI (%u) for AFI %u", safi, afi);
629                             break;
630                 }
631                 break;
632         default:
633                 length = 0 ;
634                 g_snprintf(buf, buf_len, "Unknown AFI (%u) value", afi);
635                 break;
636     }
637     return(length) ;
638 }
639
640 /*
641  * Decode a multiprotocol prefix
642  */
643 static int
644 decode_prefix_MP(proto_tree *tree, int hf_addr4, int hf_addr6,
645     guint16 afi, guint8 safi, tvbuff_t *tvb, gint offset, const char *tag)
646 {
647     int                 start_offset = offset;
648     proto_item          *ti;
649     proto_tree          *prefix_tree;
650     int                 total_length;       /* length of the entire item */
651     int                 length;             /* length of the prefix address, in bytes */
652     guint               plen;               /* length of the prefix address, in bits */
653     guint               labnum;             /* number of labels             */
654     guint16             tnl_id;             /* Tunnel Identifier */
655     int                 ce_id,labblk_off,labblk_size;
656     union {
657        guint8 addr_bytes[4];
658        guint32 addr;
659     } ip4addr, ip4addr2;                    /* IPv4 address                 */
660     struct e_in6_addr   ip6addr;            /* IPv6 address                 */
661     guint16             rd_type;            /* Route Distinguisher type     */
662     char                lab_stk[256];       /* label stack                  */
663
664     switch (afi) {
665
666     case AFNUM_INET:
667         switch (safi) {
668
669         case SAFNUM_UNICAST:
670         case SAFNUM_MULCAST:
671         case SAFNUM_UNIMULC:
672             total_length = decode_prefix4(tree, hf_addr4, tvb, offset, 0, tag);
673             if (total_length < 0)
674                 return -1;
675             break;
676
677         case SAFNUM_MPLS_LABEL:
678             plen =  tvb_get_guint8(tvb, offset);
679             labnum = decode_MPLS_stack(tvb, offset + 1, lab_stk, sizeof(lab_stk));
680
681             offset += (1 + labnum * 3);
682             if (plen <= (labnum * 3*8)) {
683                 proto_tree_add_text(tree, tvb, start_offset, 1,
684                         "%s Labeled IPv4 prefix length %u invalid",
685                         tag, plen);
686                 return -1;
687             }
688             plen -= (labnum * 3*8);
689             length = ipv4_addr_and_mask(tvb, offset, ip4addr.addr_bytes, plen);
690             if (length < 0) {
691                 proto_tree_add_text(tree, tvb, start_offset, 1,
692                         "%s Labeled IPv4 prefix length %u invalid",
693                         tag, plen + (labnum * 3*8));
694                 return -1;
695             }
696
697             ti = proto_tree_add_text(tree, tvb, start_offset,
698                     (offset + length) - start_offset,
699                     "Label Stack=%s IPv4=%s/%u",
700                     lab_stk, ip_to_str(ip4addr.addr_bytes), plen);
701             prefix_tree = proto_item_add_subtree(ti, ett_bgp_prefix);
702             proto_tree_add_text(prefix_tree, tvb, start_offset, 1, "%s Prefix length: %u",
703                 tag, plen + labnum * 3 * 8);
704             proto_tree_add_text(prefix_tree, tvb, start_offset + 1, 3 * labnum, "%s Label Stack: %s",
705                 tag, lab_stk);
706             if (hf_addr4 != -1) {
707                 proto_tree_add_ipv4(prefix_tree, hf_addr4, tvb, offset,
708                         length, ip4addr.addr);
709             } else {
710                 proto_tree_add_text(prefix_tree, tvb, offset, length,
711                         "%s IPv4 prefix: %s",
712                         tag, ip_to_str(ip4addr.addr_bytes));
713             }
714             total_length = (1 + labnum*3) + length;
715             break;
716
717         case SAFNUM_TUNNEL:
718             plen =  tvb_get_guint8(tvb, offset);
719             if (plen <= 16){
720                 proto_tree_add_text(tree, tvb, start_offset, 1,
721                         "%s Tunnel IPv4 prefix length %u invalid",
722                         tag, plen);
723                 return -1;
724             }
725             tnl_id = tvb_get_ntohs(tvb, offset + 1);
726             offset += 3; /* Length + Tunnel Id */
727             plen -= 16; /* 2-octet Identifier */
728             length = ipv4_addr_and_mask(tvb, offset, ip4addr.addr_bytes, plen);
729             if (length < 0) {
730                 proto_tree_add_text(tree, tvb, start_offset, 1,
731                         "%s Tunnel IPv4 prefix length %u invalid",
732                         tag, plen + 16);
733                 return -1;
734             }
735             ti = proto_tree_add_text(tree, tvb, start_offset,
736                         (offset + length) - start_offset,
737                         "Tunnel Identifier=0x%x IPv4=%s/%u",
738                         tnl_id, ip_to_str(ip4addr.addr_bytes), plen);
739             prefix_tree = proto_item_add_subtree(ti, ett_bgp_prefix);
740
741             proto_tree_add_text(prefix_tree, tvb, start_offset, 1, "%s Prefix length: %u",
742                 tag, plen + 16);
743             proto_tree_add_item(prefix_tree, hf_bgp_mp_nlri_tnl_id, tvb,
744                                 start_offset + 1, 2, FALSE);
745             if (hf_addr4 != -1) {
746                 proto_tree_add_ipv4(prefix_tree, hf_addr4, tvb, offset,
747                                 length, ip4addr.addr);
748             } else {
749                 proto_tree_add_text(prefix_tree, tvb, offset, length,
750                         "%s IPv4 prefix: %s",
751                         tag, ip_to_str(ip4addr.addr_bytes));
752             }
753             total_length = 1 + 2 + length; /* length field + Tunnel Id + IPv4 len */
754             break;
755
756         case SAFNUM_LAB_VPNUNICAST:
757         case SAFNUM_LAB_VPNMULCAST:
758         case SAFNUM_LAB_VPNUNIMULC:
759             plen =  tvb_get_guint8(tvb, offset);
760             labnum = decode_MPLS_stack(tvb, offset + 1, lab_stk, sizeof(lab_stk));
761
762             offset += (1 + labnum * 3);
763             if (plen <= (labnum * 3*8)) {
764                 proto_tree_add_text(tree, tvb, start_offset, 1,
765                         "%s Labeled VPN IPv4 prefix length %u invalid",
766                         tag, plen);
767                 return -1;
768             }
769             plen -= (labnum * 3*8);
770
771             rd_type = tvb_get_ntohs(tvb, offset);
772             if (plen < 8*8) {
773                 proto_tree_add_text(tree, tvb, start_offset, 1,
774                         "%s Labeled VPN IPv4 prefix length %u invalid",
775                         tag, plen + (labnum * 3*8));
776                 return -1;
777             }
778             plen -= 8*8;
779
780             switch (rd_type) {
781
782             case FORMAT_AS2_LOC: /* Code borrowed from the decode_prefix4 function */
783                 length = ipv4_addr_and_mask(tvb, offset + 8, ip4addr.addr_bytes, plen);
784                 if (length < 0) {
785                     proto_tree_add_text(tree, tvb, start_offset, 1,
786                             "%s Labeled VPN IPv4 prefix length %u invalid",
787                             tag, plen + (labnum * 3*8) + 8*8);
788                     return -1;
789                 }
790
791                 ti = proto_tree_add_text(tree, tvb, start_offset,
792                         (offset + 8 + length) - start_offset,
793                         "Label Stack=%s RD=%u:%u, IPv4=%s/%u",
794                         lab_stk,
795                         tvb_get_ntohs(tvb, offset + 2),
796                         tvb_get_ntohl(tvb, offset + 4),
797                         ip_to_str(ip4addr.addr_bytes), plen);
798                 prefix_tree = proto_item_add_subtree(ti, ett_bgp_prefix);
799                 proto_tree_add_text(prefix_tree, tvb, start_offset, 1, "%s Prefix length: %u",
800                         tag, plen + labnum * 3 * 8 + 8 * 8);
801                 proto_tree_add_text(prefix_tree, tvb, start_offset + 1, 3 * labnum,
802                         "%s Label Stack: %s", tag, lab_stk);
803                 proto_tree_add_text(prefix_tree, tvb, start_offset + 1 + 3 * labnum, 8,
804                         "%s Route Distinguisher: %u:%u", tag, tvb_get_ntohs(tvb, offset + 2),
805                         tvb_get_ntohl(tvb, offset + 4));
806                 if (hf_addr4 != -1) {
807                     proto_tree_add_ipv4(prefix_tree, hf_addr4, tvb,
808                             offset + 8, length, ip4addr.addr);
809                 } else {
810                     proto_tree_add_text(prefix_tree, tvb, offset + 8,
811                             length, "%s IPv4 prefix: %s", tag,
812                             ip_to_str(ip4addr.addr_bytes));
813                 }
814                 total_length = (1 + labnum * 3 + 8) + length;
815                 break;
816
817             case FORMAT_IP_LOC: /* Code borrowed from the decode_prefix4 function */
818                 tvb_memcpy(tvb, ip4addr.addr_bytes, offset + 2, 4);
819
820                 length = ipv4_addr_and_mask(tvb, offset + 8, ip4addr2.addr_bytes, plen);
821                 if (length < 0) {
822                         proto_tree_add_text(tree, tvb, start_offset, 1,
823                                 "%s Labeled VPN IPv4 prefix length %u invalid",
824                                 tag, plen + (labnum * 3*8) + 8*8);
825                         return -1;
826                 }
827
828                 ti = proto_tree_add_text(tree, tvb, start_offset,
829                         (offset + 8 + length) - start_offset,
830                         "Label Stack=%s RD=%s:%u, IPv4=%s/%u",
831                         lab_stk,
832                         ip_to_str(ip4addr.addr_bytes),
833                         tvb_get_ntohs(tvb, offset + 6),
834                         ip_to_str(ip4addr2.addr_bytes),
835                         plen);
836                 prefix_tree = proto_item_add_subtree(ti, ett_bgp_prefix);
837                 proto_tree_add_text(prefix_tree, tvb, start_offset, 1, "%s Prefix length: %u",
838                         tag, plen + labnum * 3 * 8 + 8 * 8);
839                 proto_tree_add_text(prefix_tree, tvb, start_offset + 1, 3 * labnum,
840                         "%s Label Stack: %s", tag, lab_stk);
841                 proto_tree_add_text(prefix_tree, tvb, start_offset + 1 + 3 * labnum, 8,
842                         "%s Route Distinguisher: %s:%u", tag, ip_to_str(ip4addr.addr_bytes),
843                         tvb_get_ntohs(tvb, offset + 6));
844                 if (hf_addr4 != -1) {
845                         proto_tree_add_ipv4(prefix_tree, hf_addr4, tvb,
846                                 offset + 8, length, ip4addr2.addr);
847                 } else {
848                         proto_tree_add_text(prefix_tree, tvb, offset + 8,
849                                 length, "%s IPv4 prefix: %s", tag,
850                                 ip_to_str(ip4addr2.addr_bytes));
851                 }
852                 total_length = (1 + labnum * 3 + 8) + length;
853                 break;
854
855             default:
856                 proto_tree_add_text(tree, tvb, start_offset,
857                         (offset - start_offset) + 2,
858                         "Unknown labeled VPN IPv4 address format %u", rd_type);
859                 return -1;
860             }
861             break;
862
863         default:
864             proto_tree_add_text(tree, tvb, start_offset, 0,
865                     "Unknown SAFI (%u) for AFI %u", safi, afi);
866             return -1;
867         }
868         break;
869
870     case AFNUM_INET6:
871         switch (safi) {
872
873         case SAFNUM_UNICAST:
874         case SAFNUM_MULCAST:
875         case SAFNUM_UNIMULC:
876             total_length = decode_prefix6(tree, hf_addr6, tvb, offset, 0, tag);
877             if (total_length < 0)
878                 return -1;
879             break;
880
881         case SAFNUM_MPLS_LABEL:
882             plen =  tvb_get_guint8(tvb, offset);
883             labnum = decode_MPLS_stack(tvb, offset + 1, lab_stk, sizeof(lab_stk));
884
885             offset += (1 + labnum * 3);
886             if (plen <= (labnum * 3*8)) {
887                 proto_tree_add_text(tree, tvb, start_offset, 1,
888                         "%s Labeled IPv6 prefix length %u invalid", tag, plen);
889                 return -1;
890             }
891             plen -= (labnum * 3*8);
892
893             length = ipv6_addr_and_mask(tvb, offset, &ip6addr, plen);
894             if (length < 0) {
895                 proto_tree_add_text(tree, tvb, start_offset, 1,
896                         "%s Labeled IPv6 prefix length %u invalid",
897                         tag, plen  + (labnum * 3*8));
898                 return -1;
899             }
900
901             ti = proto_tree_add_text(tree, tvb, start_offset,
902                  (offset + length) - start_offset,
903                  "Label Stack=%s, IPv6=%s/%u",
904                  lab_stk,
905                  ip6_to_str(&ip6addr), plen);
906             total_length = (1 + labnum * 3) + length;
907             break;
908
909         case SAFNUM_TUNNEL:
910             plen =  tvb_get_guint8(tvb, offset);
911             if (plen <= 16){
912                 proto_tree_add_text(tree, tvb, start_offset, 1,
913                         "%s Tunnel IPv6 prefix length %u invalid",
914                         tag, plen);
915                 return -1;
916             }
917             tnl_id = tvb_get_ntohs(tvb, offset + 1);
918             offset += 3; /* Length + Tunnel Id */
919             plen -= 16; /* 2-octet Identifier */
920             length = ipv6_addr_and_mask(tvb, offset, &ip6addr, plen);
921             if (length < 0) {
922                 proto_tree_add_text(tree, tvb, start_offset, 1,
923                         "%s Tunnel IPv6 prefix length %u invalid",
924                         tag, plen + 16);
925                 return -1;
926             }
927             ti = proto_tree_add_text(tree, tvb, start_offset,
928                         (offset + length) - start_offset,
929                         "Tunnel Identifier=0x%x IPv6=%s/%u",
930                         tnl_id, ip6_to_str(&ip6addr), plen);
931             total_length = (1 + 2) + length; /* length field + Tunnel Id + IPv4 len */
932             break;
933
934         case SAFNUM_LAB_VPNUNICAST:
935         case SAFNUM_LAB_VPNMULCAST:
936         case SAFNUM_LAB_VPNUNIMULC:
937             plen =  tvb_get_guint8(tvb, offset);
938             labnum = decode_MPLS_stack(tvb, offset + 1, lab_stk, sizeof(lab_stk));
939
940             offset += (1 + labnum * 3);
941             if (plen <= (labnum * 3*8)) {
942                 proto_tree_add_text(tree, tvb, start_offset, 1,
943                         "%s Labeled VPN IPv6 prefix length %u invalid", tag, plen);
944                 return -1;
945             }
946             plen -= (labnum * 3*8);
947
948             rd_type = tvb_get_ntohs(tvb,offset);
949             if (plen < 8*8) {
950                 proto_tree_add_text(tree, tvb, start_offset, 1,
951                         "%s Labeled VPN IPv6 prefix length %u invalid",
952                         tag, plen + (labnum * 3*8));
953                 return -1;
954             }
955             plen -= 8*8;
956
957             switch (rd_type) {
958
959             case FORMAT_AS2_LOC:
960                 length = ipv6_addr_and_mask(tvb, offset + 8, &ip6addr, plen);
961                 if (length < 0) {
962                     proto_tree_add_text(tree, tvb, start_offset, 1,
963                             "%s Labeled VPN IPv6 prefix length %u invalid",
964                             tag, plen + (labnum * 3*8) + 8*8);
965                     return -1;
966                 }
967
968                 ti = proto_tree_add_text(tree, tvb, start_offset,
969                         (offset + 8 + length) - start_offset,
970                         "Label Stack=%s RD=%u:%u, IPv6=%s/%u",
971                         lab_stk,
972                         tvb_get_ntohs(tvb, offset + 2),
973                         tvb_get_ntohl(tvb, offset + 4),
974                         ip6_to_str(&ip6addr), plen);
975                 total_length = (1 + labnum * 3 + 8) + length;
976                 break;
977
978             case FORMAT_IP_LOC: 
979                 tvb_memcpy(tvb, ip4addr.addr_bytes, offset + 2, 4);
980
981                 length = ipv6_addr_and_mask(tvb, offset + 8, &ip6addr, plen);
982                 if (length < 0) {
983                     proto_tree_add_text(tree, tvb, start_offset, 1,
984                             "%s Labeled VPN IPv6 prefix length %u invalid",
985                             tag, plen + (labnum * 3*8) + 8*8);
986                     return -1;
987                 }
988
989                 ti = proto_tree_add_text(tree, tvb, start_offset,
990                         (offset + 8 + length) - start_offset,
991                         "Label Stack=%s RD=%s:%u, IPv6=%s/%u",
992                         lab_stk,
993                         ip_to_str(ip4addr.addr_bytes),
994                         tvb_get_ntohs(tvb, offset + 6),
995                         ip6_to_str(&ip6addr), plen);
996                 total_length = (1 + labnum * 3 + 8) + length;
997                 break;
998
999             default:
1000                 proto_tree_add_text(tree, tvb, start_offset, 0,
1001                         "Unknown labeled VPN IPv6 address format %u", rd_type);
1002                 return -1;
1003             }
1004             break;
1005
1006         default:
1007             proto_tree_add_text(tree, tvb, start_offset, 0,
1008                     "Unknown SAFI (%u) for AFI %u", safi, afi);
1009             return -1;
1010         }
1011         break;
1012
1013     case AFNUM_L2VPN:
1014     case AFNUM_L2VPN_OLD:
1015         switch (safi) {
1016
1017         case SAFNUM_LAB_VPNUNICAST:
1018         case SAFNUM_LAB_VPNMULCAST:
1019         case SAFNUM_LAB_VPNUNIMULC:
1020         case SAFNUM_VPLS:
1021             plen =  tvb_get_ntohs(tvb,offset);
1022             rd_type=tvb_get_ntohs(tvb,offset+2);
1023             ce_id=tvb_get_ntohs(tvb,offset+10);
1024             labblk_off=tvb_get_ntohs(tvb,offset+12);
1025             labblk_size=tvb_get_ntohs(tvb,offset+14);
1026             labnum = decode_MPLS_stack(tvb, offset + 16, lab_stk, sizeof(lab_stk));
1027
1028             switch (rd_type) {
1029
1030             case FORMAT_AS2_LOC:
1031                 tvb_memcpy(tvb, ip4addr.addr_bytes, offset + 6, 4);
1032                 proto_tree_add_text(tree, tvb, start_offset,
1033                         (offset + plen + 1) - start_offset,
1034                         "RD: %u:%s, CE-ID: %u, Label-Block Offset: %u, "
1035                         "Label-Block Size: %u Label Base %s",
1036                         tvb_get_ntohs(tvb, offset + 4),
1037                         ip_to_str(ip4addr.addr_bytes),
1038                         ce_id,
1039                         labblk_size,
1040                         labblk_off,
1041                         lab_stk);
1042                 break;
1043
1044             case FORMAT_IP_LOC:
1045                 tvb_memcpy(tvb, ip4addr.addr_bytes, offset + 4, 4);
1046                 proto_tree_add_text(tree, tvb, offset,
1047                         (offset + plen + 1) - start_offset,
1048                         "RD: %s:%u, CE-ID: %u, Label-Block Offset: %u, "
1049                         "Label-Block Size: %u, Label Base %s",
1050                         ip_to_str(ip4addr.addr_bytes),
1051                         tvb_get_ntohs(tvb, offset + 8),
1052                         ce_id,
1053                         labblk_off,
1054                         labblk_size,
1055                         lab_stk);
1056                 break;
1057
1058             default:
1059                 proto_tree_add_text(tree, tvb, start_offset,
1060                         (offset - start_offset) + 2,
1061                         "Unknown labeled VPN address format %u", rd_type);
1062                 return -1;
1063             }
1064             /* FIXME there are subTLVs left to decode ... for now lets omit them */
1065             total_length = plen+2;
1066             break;
1067
1068         default:
1069             proto_tree_add_text(tree, tvb, start_offset, 0,
1070                     "Unknown SAFI (%u) for AFI %u", safi, afi);
1071             return -1;
1072         }
1073         break;
1074
1075     default:
1076         proto_tree_add_text(tree, tvb, start_offset, 0,
1077                 "Unknown AFI (%u) value", afi);
1078         return -1;
1079     }
1080     return(total_length);
1081 }
1082
1083 /*
1084  * Dissect a BGP capability.
1085  */
1086 static void
1087 dissect_bgp_capability_item(tvbuff_t *tvb, int *p, proto_tree *tree, int ctype, int clen)
1088 {
1089     proto_tree *subtree;
1090     proto_item *ti;
1091     guint8 orfnum;       /* number of ORFs */
1092     guint8 orftype;      /* ORF Type */
1093     guint8 orfsendrecv;  /* ORF Send/Receive */
1094     int    tclen;        /* capability length */
1095     int    i;
1096
1097     /* check the capability type */
1098     switch (ctype) {
1099     case BGP_CAPABILITY_RESERVED:
1100         proto_tree_add_text(tree, tvb, *p - 2, 1,
1101              "Capability code: %s (%d)", val_to_str(ctype,
1102              capability_vals, "Unknown capability"), ctype);
1103         proto_tree_add_text(tree, tvb, *p - 1,
1104              1, "Capability length: %u %s", clen,
1105              (clen == 1) ? "byte" : "bytes");
1106         if (clen != 0) {
1107             proto_tree_add_text(tree, tvb, *p,
1108                  clen, "Capability value: Unknown");
1109         }
1110         *p += clen;
1111         break;
1112     case BGP_CAPABILITY_MULTIPROTOCOL:
1113         proto_tree_add_text(tree, tvb, *p - 2, 1,
1114              "Capability code: %s (%d)", val_to_str(ctype,
1115              capability_vals, "Unknown capability"), ctype);
1116         if (clen != 4) {
1117             proto_tree_add_text(tree, tvb, *p - 1,
1118                  1, "Capability length: Invalid");
1119             proto_tree_add_text(tree, tvb, *p,
1120                  clen, "Capability value: Unknown");
1121         }
1122         else {
1123             proto_tree_add_text(tree, tvb, *p - 1,
1124                  1, "Capability length: %u %s", clen,
1125                  (clen == 1) ? "byte" : "bytes");
1126             ti = proto_tree_add_text(tree, tvb, *p, clen, "Capability value");
1127             subtree = proto_item_add_subtree(ti, ett_bgp_option);
1128             /* AFI */
1129             i = tvb_get_ntohs(tvb, *p);
1130             proto_tree_add_text(subtree, tvb, *p,
1131                  2, "Address family identifier: %s (%u)",
1132                  val_to_str(i, afn_vals, "Unknown"), i);
1133             *p += 2;
1134             /* Reserved */
1135             proto_tree_add_text(subtree, tvb, *p, 1, "Reserved: 1 byte");
1136             (*p)++;
1137             /* SAFI */
1138             i = tvb_get_guint8(tvb, *p);
1139             proto_tree_add_text(subtree, tvb, *p,
1140                  1, "Subsequent address family identifier: %s (%u)",
1141                  val_to_str(i, bgpattr_nlri_safi,
1142                  i >= 128 ? "Vendor specific" : "Unknown"), i);
1143             (*p)++;
1144         }
1145         break;
1146     case BGP_CAPABILITY_GRACEFUL_RESTART:
1147         proto_tree_add_text(tree, tvb, *p - 2, 1,
1148              "Capability code: %s (%d)", val_to_str(ctype,
1149              capability_vals, "Unknown capability"), ctype);
1150         if (clen < 6) {
1151             proto_tree_add_text(tree, tvb, *p,
1152                  clen, "Capability value: Invalid");
1153         }
1154         else {
1155             proto_tree_add_text(tree, tvb, *p - 1,
1156                  1, "Capability length: %u %s", clen,
1157                  (clen == 1) ? "byte" : "bytes");
1158             ti = proto_tree_add_text(tree, tvb, *p, clen, "Capability value");
1159             subtree = proto_item_add_subtree(ti, ett_bgp_option);
1160             /* Timers */
1161             i = tvb_get_ntohs(tvb, *p);
1162             proto_tree_add_text(subtree, tvb, *p,
1163                  2, "Restart Flags: [%s], Restart Time %us",
1164                  (i&0x8000) ? "R" : "none", i&0xfff);
1165             *p += 2;
1166             tclen = clen - 2;
1167             /*
1168              * what follows is alist of AFI/SAFI/flag triplets
1169              * read it until the TLV ends
1170              */
1171             while (tclen >=4) {
1172                 /* AFI */
1173                 i = tvb_get_ntohs(tvb, *p);
1174                 proto_tree_add_text(subtree, tvb, *p,
1175                      2, "Address family identifier: %s (%u)",
1176                      val_to_str(i, afn_vals, "Unknown"), i);
1177                 *p += 2;
1178                 /* SAFI */
1179                 i = tvb_get_guint8(tvb, *p);
1180                 proto_tree_add_text(subtree, tvb, *p,
1181                      1, "Subsequent address family identifier: %s (%u)",
1182                      val_to_str(i, bgpattr_nlri_safi,
1183                      i >= 128 ? "Vendor specific" : "Unknown"), i);
1184                 (*p)++;
1185                 /* flags */
1186                 i = tvb_get_guint8(tvb, *p);
1187                 proto_tree_add_text(subtree, tvb, *p, 1,
1188                      "Preserve forwarding state: %s",
1189                      (i&0x80) ? "yes" : "no");
1190                 (*p)++;
1191                 tclen-=4;
1192             }
1193         }
1194         *p += clen;
1195         break;
1196     case BGP_CAPABILITY_4_OCTET_AS_NUMBER:
1197         proto_tree_add_text(tree, tvb, *p - 2, 1,
1198              "Capability code: %s (%d)", val_to_str(ctype,
1199              capability_vals, "Unknown capability"), ctype);
1200         if (clen != 4) {
1201             proto_tree_add_text(tree, tvb, *p,
1202                  clen, "Capability value: Invalid");
1203         }
1204         else {
1205             proto_tree_add_text(tree, tvb, *p - 1,
1206                  1, "Capability length: %u %s", clen,
1207                  (clen == 1) ? "byte" : "bytes");
1208             ti = proto_tree_add_text(tree, tvb, *p, clen, "Capability value");
1209             subtree = proto_item_add_subtree(ti, ett_bgp_option);
1210             proto_tree_add_text(subtree, tvb, *p, 4,
1211                  "AS number: %d", tvb_get_ntohl(tvb, *p));
1212         }
1213         *p += clen;
1214         break;
1215     case BGP_CAPABILITY_DYNAMIC_CAPABILITY:
1216         proto_tree_add_text(tree, tvb, *p - 2, 1,
1217              "Capability code: %s (%d)", val_to_str(ctype,
1218              capability_vals, "Unknown capability"), ctype);
1219         proto_tree_add_text(tree, tvb, *p - 1, 1,
1220              "Capability length: %u %s", clen,
1221              (clen == 1) ? "byte" : "bytes");
1222         if (clen > 0) {
1223             ti = proto_tree_add_text(tree, tvb, *p, clen, "Capability value");
1224             subtree = proto_item_add_subtree(ti, ett_bgp_option);
1225             for (i = 0; (int)i <= clen; i++) {
1226                 proto_tree_add_text(subtree, tvb, *p, 1,
1227                      "Capability code: %s (%d)", val_to_str(ctype,
1228                      capability_vals, "Unknown capability"),
1229                      tvb_get_guint8(tvb, *p));
1230                 (*p)++;
1231             }
1232         }
1233         break;
1234     case BGP_CAPABILITY_ROUTE_REFRESH_CISCO:
1235     case BGP_CAPABILITY_ROUTE_REFRESH:
1236         proto_tree_add_text(tree, tvb, *p - 2, 1,
1237              "Capability code: %s (%d)", val_to_str(ctype,
1238              capability_vals, "Unknown capability"), ctype);
1239         if (clen != 0) {
1240             proto_tree_add_text(tree, tvb, *p,
1241                  clen, "Capability value: Invalid");
1242         }
1243         else {
1244             proto_tree_add_text(tree, tvb, *p - 1,
1245                  1, "Capability length: %u %s", clen,
1246                  (clen == 1) ? "byte" : "bytes");
1247         }
1248         *p += clen;
1249         break;
1250     case BGP_CAPABILITY_ORF_CISCO:
1251     case BGP_CAPABILITY_COOPERATIVE_ROUTE_FILTERING:
1252         proto_tree_add_text(tree, tvb, *p - 2, 1,
1253              "Capability code: %s (%d)", val_to_str(ctype,
1254              capability_vals, "Unknown capability"), ctype);
1255         proto_tree_add_text(tree, tvb, *p - 1,
1256              1, "Capability length: %u %s", clen,
1257              (clen == 1) ? "byte" : "bytes");
1258         ti = proto_tree_add_text(tree, tvb, *p, clen, "Capability value");
1259         subtree = proto_item_add_subtree(ti, ett_bgp_option);
1260         /* AFI */
1261         i = tvb_get_ntohs(tvb, *p);
1262         proto_tree_add_text(subtree, tvb, *p,
1263              2, "Address family identifier: %s (%u)",
1264              val_to_str(i, afn_vals, "Unknown"), i);
1265         *p += 2;
1266         /* Reserved */
1267         proto_tree_add_text(subtree, tvb, *p, 1, "Reserved: 1 byte");
1268         (*p)++;
1269         /* SAFI */
1270         i = tvb_get_guint8(tvb, *p);
1271         proto_tree_add_text(subtree, tvb, *p,
1272              1, "Subsequent address family identifier: %s (%u)",
1273              val_to_str(i, bgpattr_nlri_safi,
1274              i >= 128 ? "Vendor specific" : "Unknown"), i);
1275         (*p)++;
1276         /* Number of ORFs */
1277         orfnum = tvb_get_guint8(tvb, *p);
1278         proto_tree_add_text(subtree, tvb, *p, 1, "Number of ORFs: %u", orfnum);
1279         (*p)++;
1280         for (i=0; i<orfnum; i++) {
1281             /* ORF Type */
1282             orftype = tvb_get_guint8(tvb, *p);
1283             proto_tree_add_text(subtree, tvb, *p, 1, "ORF Type: %s (%u)",
1284                  val_to_str(orftype, orf_type_vals,"Unknown"), orftype);
1285             (*p)++;
1286             /* Send/Receive */
1287             orfsendrecv = tvb_get_guint8(tvb, *p);
1288             proto_tree_add_text(subtree, tvb, *p,
1289                  1, "Send/Receive: %s (%u)",
1290                  val_to_str(orfsendrecv, orf_send_recv_vals,
1291                  "Uknown"), orfsendrecv);
1292             (*p)++;
1293         }
1294         break;
1295         /* unknown capability */
1296     default:
1297         proto_tree_add_text(tree, tvb, *p - 2, 1,
1298              "Capability code: %s (%d)", val_to_str(ctype,
1299              capability_vals, "Unknown capability"), ctype);
1300         proto_tree_add_text(tree, tvb, *p - 2,
1301              1, "Capability code: %s (%d)",
1302              ctype >= 128 ? "Private use" : "Unknown", ctype);
1303         proto_tree_add_text(tree, tvb, *p - 1,
1304              1, "Capability length: %u %s", clen,
1305              (clen == 1) ? "byte" : "bytes");
1306         if (clen != 0) {
1307             proto_tree_add_text(tree, tvb, *p,
1308                  clen, "Capability value: Unknown");
1309         }
1310         *p += clen;
1311         break;
1312     }
1313 }
1314
1315
1316 /*
1317  * Dissect a BGP OPEN message.
1318  */
1319 static const value_string community_vals[] = {
1320         { BGP_COMM_NO_EXPORT,           "NO_EXPORT" },
1321         { BGP_COMM_NO_ADVERTISE,        "NO_ADVERTISE" },
1322         { BGP_COMM_NO_EXPORT_SUBCONFED, "NO_EXPORT_SUBCONFED" },
1323         { 0,                            NULL }
1324 };
1325
1326 static void
1327 dissect_bgp_open(tvbuff_t *tvb, proto_tree *tree)
1328 {
1329     struct bgp_open bgpo;      /* BGP OPEN message      */
1330     int             hlen;      /* message length        */
1331     int             ptype;     /* parameter type        */
1332     int             plen;      /* parameter length      */
1333     int             ctype;     /* capability type       */
1334     int             clen;      /* capability length     */
1335     int             cend;      /* capabilities end      */
1336     int             ostart;    /* options start         */
1337     int             oend;      /* options end           */
1338     int             p;         /* tvb offset counter    */
1339     proto_item      *ti;       /* tree item             */
1340     proto_tree      *subtree;  /* subtree for options   */
1341     proto_tree      *subtree1; /* subtree for an option */
1342     proto_tree      *subtree2; /* subtree for an option */
1343
1344     /* snarf OPEN message */
1345     tvb_memcpy(tvb, bgpo.bgpo_marker, 0, BGP_MIN_OPEN_MSG_SIZE);
1346     hlen = g_ntohs(bgpo.bgpo_len);
1347
1348     proto_tree_add_text(tree, tvb,
1349         offsetof(struct bgp_open, bgpo_version), 1,
1350         "Version: %u", bgpo.bgpo_version);
1351     proto_tree_add_text(tree, tvb,
1352         offsetof(struct bgp_open, bgpo_myas), 2,
1353         "My AS: %u", g_ntohs(bgpo.bgpo_myas));
1354     proto_tree_add_text(tree, tvb,
1355         offsetof(struct bgp_open, bgpo_holdtime), 2,
1356         "Hold time: %u", g_ntohs(bgpo.bgpo_holdtime));
1357     proto_tree_add_text(tree, tvb,
1358         offsetof(struct bgp_open, bgpo_id), 4,
1359         "BGP identifier: %s", ip_to_str((guint8 *)&bgpo.bgpo_id));
1360     proto_tree_add_text(tree, tvb,
1361         offsetof(struct bgp_open, bgpo_optlen), 1,
1362         "Optional parameters length: %u %s", bgpo.bgpo_optlen,
1363         (bgpo.bgpo_optlen == 1) ? "byte" : "bytes");
1364
1365     /* optional parameters */
1366     if (bgpo.bgpo_optlen > 0) {
1367         /* add a subtree and setup some offsets */
1368         ostart = BGP_MIN_OPEN_MSG_SIZE;
1369         ti = proto_tree_add_text(tree, tvb, ostart, bgpo.bgpo_optlen,
1370              "Optional parameters");
1371         subtree = proto_item_add_subtree(ti, ett_bgp_options);
1372         p = ostart;
1373         oend = p + bgpo.bgpo_optlen;
1374
1375         /* step through all of the optional parameters */
1376         while (p < oend) {
1377
1378             /* grab the type and length */
1379             ptype = tvb_get_guint8(tvb, p++);
1380             plen = tvb_get_guint8(tvb, p++);
1381
1382             /* check the type */
1383             switch (ptype) {
1384             case BGP_OPTION_AUTHENTICATION:
1385                 proto_tree_add_text(subtree, tvb, p - 2, 2 + plen,
1386                     "Authentication information (%u %s)", plen,
1387                     (plen == 1) ? "byte" : "bytes");
1388                 break;
1389             case BGP_OPTION_CAPABILITY:
1390                 /* grab the capability code */
1391                 cend = p - 1 + plen;
1392                 ctype = tvb_get_guint8(tvb, p++);
1393                 clen = tvb_get_guint8(tvb, p++);
1394                 ti = proto_tree_add_text(subtree, tvb, p - 4,
1395                      2 + plen, "Capabilities Advertisement (%u bytes)",
1396                      2 + plen);
1397                 subtree1 = proto_item_add_subtree(ti, ett_bgp_option);
1398                 proto_tree_add_text(subtree1, tvb, p - 4,
1399                      1, "Parameter type: Capabilities (2)");
1400                 proto_tree_add_text(subtree1, tvb, p - 3,
1401                      1, "Parameter length: %u %s", plen,
1402                      (plen == 1) ? "byte" : "bytes");
1403                 p -= 2;
1404
1405                 /* step through all of the capabilities */
1406                 while (p < cend) {
1407                     ctype = tvb_get_guint8(tvb, p++);
1408                     clen = tvb_get_guint8(tvb, p++);
1409
1410                     ti = proto_tree_add_text(subtree1, tvb, p - 2,
1411                          2 + clen, "%s (%u %s)", val_to_str(ctype,
1412                          capability_vals, "Unknown capability"),
1413                          2 + clen, (clen == 1) ? "byte" : "bytes");
1414                     subtree2 = proto_item_add_subtree(ti, ett_bgp_option);
1415                     dissect_bgp_capability_item(tvb, &p,
1416                            subtree2, ctype, clen);
1417                 }
1418                 break;
1419             default:
1420                 proto_tree_add_text(subtree, tvb, p - 2, 2 + plen,
1421                     "Unknown optional parameter");
1422                 break;
1423             }
1424         }
1425     }
1426 }
1427
1428 /*
1429  * Dissect a BGP UPDATE message.
1430  */
1431 static void
1432 dissect_bgp_update(tvbuff_t *tvb, proto_tree *tree)
1433 {
1434     struct bgp_attr bgpa;                       /* path attributes          */
1435     guint16         hlen;                       /* message length           */
1436     gint            o;                          /* packet offset            */
1437     gint            q;                          /* tmp                      */
1438     gint            end;                        /* message end              */
1439     guint16         ext_com;                    /* EXTENDED COMMUNITY type  */
1440     guint16         len;                        /* tmp                      */
1441     int             advance;                    /* tmp                      */
1442     proto_item      *ti;                        /* tree item                */
1443     proto_tree      *subtree;                   /* subtree for attributes   */
1444     proto_tree      *subtree2;                  /* subtree for attributes   */
1445     proto_tree      *subtree3;                  /* subtree for attributes   */
1446     proto_tree      *subtree4;                  /* subtree for attributes   */
1447     proto_tree      *as_paths_tree;             /* subtree for AS_PATHs     */
1448     proto_tree      *as_path_tree;              /* subtree for AS_PATH      */
1449     proto_tree      *as_path_segment_tree;      /* subtree for AS_PATH segments */
1450     proto_tree      *communities_tree;          /* subtree for COMMUNITIES  */
1451     proto_tree      *community_tree;            /* subtree for a community  */
1452     proto_tree      *cluster_list_tree;         /* subtree for CLUSTER_LIST */
1453     int             i, j;                       /* tmp                      */
1454     guint8          length;                     /* AS_PATH length           */
1455     guint8          type;                       /* AS_PATH type             */
1456     guint32         as_path_item;               /* item in AS_PATH segment  */
1457     static GString  *as_path_gstr = NULL;       /* AS_PATH GString          */
1458     static GString  *communities_gstr = NULL;   /* COMMUNITIES GString      */
1459     static GString  *cluster_list_gstr = NULL;  /* CLUSTER_LIST GString     */
1460     char            *junk_gbuf, *junk_gbuf_ptr; /* tmp                      */
1461     guint32         ipaddr;                     /* IPv4 address             */
1462     guint32         aggregator_as;
1463     guint16         ssa_type;                   /* SSA T + Type */
1464     guint16         ssa_len;                    /* SSA TLV Length */
1465     guint8          ssa_v3_len;                 /* SSA L2TPv3 Cookie Length */
1466     gfloat          linkband;                   /* Link bandwidth           */
1467
1468     hlen = tvb_get_ntohs(tvb, BGP_MARKER_SIZE);
1469     o = BGP_HEADER_SIZE;
1470     junk_gbuf=ep_alloc(MAX_STR_LEN);
1471     junk_gbuf[0]=0;
1472     junk_gbuf_ptr=junk_gbuf;
1473
1474     /* check for withdrawals */
1475     len = tvb_get_ntohs(tvb, o);
1476     proto_tree_add_text(tree, tvb, o, 2,
1477         "Unfeasible routes length: %u %s", len, (len == 1) ? "byte" : "bytes");
1478     o += 2;
1479
1480     /* parse unfeasible prefixes */
1481     if (len > 0) {
1482         ti = proto_tree_add_text(tree, tvb, o, len, "Withdrawn routes:");
1483         subtree = proto_item_add_subtree(ti, ett_bgp_unfeas);
1484
1485         /* parse each prefix */
1486         end = o + len;
1487         while (o < end) {
1488             i = decode_prefix4(subtree, hf_bgp_withdrawn_prefix, tvb, o, len,
1489                 "Withdrawn route");
1490             if (i < 0)
1491                 return;
1492             o += i;
1493         }
1494     }
1495
1496     /* check for advertisements */
1497     len = tvb_get_ntohs(tvb, o);
1498     proto_tree_add_text(tree, tvb, o, 2, "Total path attribute length: %u %s",
1499             len, (len == 1) ? "byte" : "bytes");
1500
1501     /* path attributes */
1502     if (len > 0) {
1503         ti = proto_tree_add_text(tree, tvb, o + 2, len, "Path attributes");
1504         subtree = proto_item_add_subtree(ti, ett_bgp_attrs);
1505         i = 2;
1506         while (i < len) {
1507             const char *msg;
1508             int     off;
1509             gint    k;
1510             guint16 alen, tlen, aoff; 
1511             guint16 af;
1512             guint8  saf, snpa;
1513             guint8  nexthop_len;
1514             guint8  asn_len = 0;
1515
1516             tvb_memcpy(tvb, (guint8 *)&bgpa, o + i, sizeof(bgpa));
1517             /* check for the Extended Length bit */
1518             if (bgpa.bgpa_flags & BGP_ATTR_FLAG_EXTENDED_LENGTH) {
1519                 alen = tvb_get_ntohs(tvb, o + i + sizeof(bgpa));
1520                 aoff = sizeof(bgpa) + 2;
1521             } else {
1522                 alen = tvb_get_guint8(tvb, o + i + sizeof(bgpa));
1523                 aoff = sizeof(bgpa) + 1;
1524             }
1525             tlen = alen;
1526
1527             /* This is kind of ugly - similar code appears twice, but it
1528                helps browsing attrs.                                      */
1529             /* the first switch prints things in the title of the subtree */
1530             switch (bgpa.bgpa_type) {
1531             case BGPTYPE_ORIGIN:
1532                 if (tlen != 1)
1533                     goto default_attribute_top;
1534                 msg = val_to_str(tvb_get_guint8(tvb, o + i + aoff), bgpattr_origin, "Unknown");
1535                 ti = proto_tree_add_text(subtree, tvb, o + i, tlen + aoff,
1536                         "%s: %s (%u %s)",
1537                         val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
1538                         msg, tlen + aoff, (tlen + aoff == 1) ? "byte" :
1539                         "bytes");
1540                 break;
1541             case BGPTYPE_AS_PATH:
1542             case BGPTYPE_NEW_AS_PATH:
1543                 /* (o + i + aoff) =
1544                    (o + current attribute + aoff bytes to first tuple) */
1545                 q = o + i + aoff;
1546                 end = q + tlen;
1547                 /* must be freed by second switch!                         */
1548                 /* "tlen * 11" (10 digits + space) should be a good estimate
1549                    of how long the AS path string could be                 */
1550                 if (as_path_gstr == NULL)
1551                     as_path_gstr = g_string_sized_new((tlen + 1) * 11);
1552                 if (as_path_gstr == NULL) break;
1553                 g_string_truncate(as_path_gstr, 0);
1554
1555                 /* estimate the length of the AS number */
1556                 if (bgpa.bgpa_type == BGPTYPE_NEW_AS_PATH) 
1557                     asn_len = 4;
1558                 else {
1559                     if (bgp_asn_len == 0) {
1560                         k = q;
1561                         while (k < end) {
1562                             k++;
1563                             length = tvb_get_guint8(tvb, k++);
1564                             k += length * 2;
1565                         }
1566                         asn_len = (k == end) ? 2 : 4;
1567                     }
1568                     else {
1569                         asn_len = bgp_asn_len;
1570                     }
1571                 }
1572
1573                 /* snarf each AS path */
1574                 while (q < end) {
1575                     type = tvb_get_guint8(tvb, q++);
1576                     if (as_path_gstr->len > 1 &&
1577                         as_path_gstr->str[as_path_gstr->len - 1] != ' ')
1578                         g_string_append_c(as_path_gstr, ' ');
1579                     if (type == AS_SET) {
1580                         g_string_append_c(as_path_gstr, '{');
1581                     }
1582                     else if (type == AS_CONFED_SET) {
1583                         g_string_append_c(as_path_gstr, '[');
1584                     }
1585                     else if (type == AS_CONFED_SEQUENCE) {
1586                         g_string_append_c(as_path_gstr, '(');
1587                     }
1588                     length = tvb_get_guint8(tvb, q++);
1589
1590                     /* snarf each value in path */
1591                     for (j = 0; j < length; j++) {
1592                         g_string_sprintfa(as_path_gstr, "%u%s",
1593                                 (asn_len == 2) ?
1594                                 tvb_get_ntohs(tvb, q) : tvb_get_ntohl(tvb, q),
1595                                 (type == AS_SET || type == AS_CONFED_SET) ?
1596                                 ", " : " ");
1597                         q += asn_len;
1598                     }
1599
1600                     /* cleanup end of string */
1601                     if (type == AS_SET) {
1602                         g_string_truncate(as_path_gstr, as_path_gstr->len - 2);
1603                         g_string_append_c(as_path_gstr, '}');
1604                     }
1605                     else if (type == AS_CONFED_SET) {
1606                         g_string_truncate(as_path_gstr, as_path_gstr->len - 2);
1607                         g_string_append_c(as_path_gstr, ']');
1608                     }
1609                     else if (type == AS_CONFED_SEQUENCE) {
1610                         g_string_truncate(as_path_gstr, as_path_gstr->len - 1);
1611                         g_string_append_c(as_path_gstr, ')');
1612                     }
1613                     else {
1614                         g_string_truncate(as_path_gstr, as_path_gstr->len - 1);
1615                     }
1616                 }
1617
1618                 /* check for empty AS_PATH */
1619                 if (tlen == 0)
1620                     g_string_sprintf(as_path_gstr, "empty");
1621
1622                 ti = proto_tree_add_text(subtree, tvb, o + i, tlen + aoff,
1623                         "%s: %s (%u %s)",
1624                         val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
1625                         as_path_gstr->str, tlen + aoff,
1626                         (tlen + aoff == 1) ? "byte" : "bytes");
1627                 break;
1628             case BGPTYPE_NEXT_HOP:
1629                 if (tlen != 4)
1630                     goto default_attribute_top;
1631                 ipaddr = tvb_get_ipv4(tvb, o + i + aoff);
1632                 ti = proto_tree_add_text(subtree, tvb, o + i, tlen + aoff,
1633                         "%s: %s (%u %s)",
1634                         val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
1635                         ip_to_str((guint8 *)&ipaddr),
1636                         tlen + aoff, (tlen + aoff == 1) ? "byte" : "bytes");
1637                 break;
1638             case BGPTYPE_MULTI_EXIT_DISC:
1639                 if (tlen != 4)
1640                     goto default_attribute_top;
1641                 ti = proto_tree_add_text(subtree, tvb, o + i, tlen + aoff,
1642                         "%s: %u (%u %s)",
1643                         val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
1644                         tvb_get_ntohl(tvb, o + i + aoff), tlen + aoff,
1645                         (tlen + aoff == 1) ? "byte" : "bytes");
1646                 break;
1647             case BGPTYPE_LOCAL_PREF:
1648                 if (tlen != 4)
1649                     goto default_attribute_top;
1650                 ti = proto_tree_add_text(subtree, tvb, o + i, tlen + aoff,
1651                         "%s: %u (%u %s)",
1652                         val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
1653                         tvb_get_ntohl(tvb, o + i + aoff), tlen + aoff,
1654                         (tlen + aoff == 1) ? "byte" : "bytes");
1655                 break;
1656             case BGPTYPE_ATOMIC_AGGREGATE:
1657                 if (tlen != 0)
1658                     goto default_attribute_top;
1659                 ti = proto_tree_add_text(subtree, tvb, o + i, tlen + aoff,
1660                         "%s (%u %s)",
1661                         val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
1662                         tlen + aoff, (tlen + aoff == 1) ? "byte" : "bytes");
1663                 break;
1664             case BGPTYPE_AGGREGATOR:
1665                 if (tlen != 6 && tlen != 8)
1666                     goto default_attribute_top;
1667             case BGPTYPE_NEW_AGGREGATOR:
1668                 if (bgpa.bgpa_type == BGPTYPE_NEW_AGGREGATOR && tlen != 8)
1669                     goto default_attribute_top;
1670                 asn_len = tlen - 4;
1671                 ipaddr = tvb_get_ipv4(tvb, o + i + aoff + asn_len);
1672                 ti = proto_tree_add_text(subtree, tvb, o + i, tlen + aoff,
1673                         "%s: AS: %u origin: %s (%u %s)",
1674                         val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
1675                         (asn_len == 2) ? tvb_get_ntohs(tvb, o + i + aoff) :
1676                         tvb_get_ntohl(tvb, o + i + aoff),
1677                         ip_to_str((guint8 *)&ipaddr),
1678                         tlen + aoff, (tlen + aoff == 1) ? "byte" : "bytes");
1679                 break;
1680             case BGPTYPE_COMMUNITIES:
1681                 if (tlen % 4 != 0)
1682                     goto default_attribute_top;
1683
1684                 /* (o + i + aoff) =
1685                    (o + current attribute + aoff bytes to first tuple) */
1686                 q = o + i + aoff;
1687                 end = q + tlen;
1688                 /* must be freed by second switch!                          */
1689                 /* "tlen * 12" (5 digits, a :, 5 digits + space ) should be
1690                    a good estimate of how long the communities string could
1691                    be                                                       */
1692                 if (communities_gstr == NULL)
1693                     communities_gstr = g_string_sized_new((tlen + 1) * 12);
1694                 if (communities_gstr == NULL) break;
1695                 g_string_truncate(communities_gstr, 0);
1696
1697                 /* snarf each community */
1698                 while (q < end) {
1699                     /* check for well-known communities */
1700                     if (tvb_get_ntohl(tvb, q) == BGP_COMM_NO_EXPORT)
1701                         g_string_append(communities_gstr, "NO_EXPORT ");
1702                     else if (tvb_get_ntohl(tvb, q) == BGP_COMM_NO_ADVERTISE)
1703                         g_string_append(communities_gstr, "NO_ADVERTISE ");
1704                     else if (tvb_get_ntohl(tvb, q) == BGP_COMM_NO_EXPORT_SUBCONFED)
1705                         g_string_append(communities_gstr, "NO_EXPORT_SUBCONFED ");
1706                     else {
1707                         g_string_sprintfa(communities_gstr, "%u:%u ",
1708                                 tvb_get_ntohs(tvb, q),
1709                                 tvb_get_ntohs(tvb, q + 2));
1710                     }
1711                     q += 4;
1712                 }
1713                 /* cleanup end of string */
1714                 g_string_truncate(communities_gstr, communities_gstr->len - 1);
1715
1716                 ti = proto_tree_add_text(subtree, tvb, o + i, tlen + aoff,
1717                         "%s: %s (%u %s)",
1718                         val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
1719                         communities_gstr->str, tlen + aoff,
1720                         (tlen + aoff == 1) ? "byte" : "bytes");
1721                 break;
1722             case BGPTYPE_ORIGINATOR_ID:
1723                 if (tlen != 4)
1724                     goto default_attribute_top;
1725                 ipaddr = tvb_get_ipv4(tvb, o + i + aoff);
1726                 ti = proto_tree_add_text(subtree, tvb, o + i, tlen + aoff,
1727                         "%s: %s (%u %s)",
1728                         val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
1729                         ip_to_str((guint8 *)&ipaddr),
1730                         tlen + aoff, (tlen + aoff == 1) ? "byte" : "bytes");
1731                 break;
1732             case BGPTYPE_CLUSTER_LIST:
1733                 if (tlen % 4 != 0)
1734                     goto default_attribute_top;
1735
1736                 /* (o + i + aoff) =
1737                    (o + current attribute + aoff bytes to first tuple) */
1738                 q = o + i + aoff;
1739                 end = q + tlen;
1740                 /* must be freed by second switch!                          */
1741                 /* "tlen * 16" (12 digits, 3 dots + space ) should be
1742                    a good estimate of how long the cluster_list string could
1743                    be                                                       */
1744                 if (cluster_list_gstr == NULL)
1745                     cluster_list_gstr = g_string_sized_new((tlen + 1) * 16);
1746                 if (cluster_list_gstr == NULL) break;
1747                 g_string_truncate(cluster_list_gstr, 0);
1748
1749                 /* snarf each cluster list */
1750                 while (q < end) {
1751                     ipaddr = tvb_get_ipv4(tvb, q);
1752                     g_string_sprintfa(cluster_list_gstr, "%s ", ip_to_str((guint8 *)&ipaddr));
1753                     q += 4;
1754                 }
1755                 /* cleanup end of string */
1756                 g_string_truncate(cluster_list_gstr, cluster_list_gstr->len - 1);
1757
1758                 ti = proto_tree_add_text(subtree, tvb, o + i, tlen + aoff,
1759                         "%s: %s (%u %s)",
1760                         val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
1761                         cluster_list_gstr->str, tlen + aoff,
1762                         (tlen + aoff == 1) ? "byte" : "bytes");
1763                 break;
1764             case BGPTYPE_EXTENDED_COMMUNITY:
1765                 if (tlen %8 != 0)
1766                     break;
1767                 ti = proto_tree_add_text(subtree,tvb,o+i,tlen+aoff,
1768                         "%s: (%u %s)",
1769                         val_to_str(bgpa.bgpa_type,bgpattr_type,"Unknown"),
1770                         tlen + aoff,
1771                         (tlen + aoff == 1) ? "byte" : "bytes");
1772                 break;
1773             case BGPTYPE_SAFI_SPECIFIC_ATTR:
1774                 ti = proto_tree_add_text(subtree,tvb,o+i,tlen+aoff,
1775                         "%s: (%u %s)",
1776                         val_to_str(bgpa.bgpa_type,bgpattr_type,"Unknown"),
1777                         tlen + aoff,
1778                         (tlen + aoff == 1) ? "byte" : "bytes");
1779                 break;
1780
1781             default:
1782             default_attribute_top:
1783                 ti = proto_tree_add_text(subtree, tvb, o + i, tlen + aoff,
1784                         "%s (%u %s)",
1785                         val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
1786                         tlen + aoff, (tlen + aoff == 1) ? "byte" : "bytes");
1787             } /* end of first switch */
1788             subtree2 = proto_item_add_subtree(ti, ett_bgp_attr);
1789
1790             /* figure out flags */
1791             junk_gbuf[0]=0;
1792             junk_gbuf_ptr=junk_gbuf;
1793             if (bgpa.bgpa_flags & BGP_ATTR_FLAG_OPTIONAL) {
1794                  junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), "Optional, ");
1795             }
1796             else {
1797                  junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), "Well-known, ");
1798             }
1799             if (bgpa.bgpa_flags & BGP_ATTR_FLAG_TRANSITIVE) {
1800                  junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), "Transitive, ");
1801             }
1802             else {
1803                  junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), "Non-transitive, ");
1804             }
1805             if (bgpa.bgpa_flags & BGP_ATTR_FLAG_PARTIAL) {
1806                  junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), "Partial");
1807             }
1808             else {
1809                  junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), "Complete");
1810             }
1811             if (bgpa.bgpa_flags & BGP_ATTR_FLAG_EXTENDED_LENGTH) {
1812                  junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), ", Extended Length");
1813             }
1814             ti = proto_tree_add_text(subtree2, tvb,
1815                     o + i + offsetof(struct bgp_attr, bgpa_flags), 1,
1816                     "Flags: 0x%02x (%s)", bgpa.bgpa_flags, junk_gbuf);
1817             subtree3 = proto_item_add_subtree(ti, ett_bgp_attr_flags);
1818
1819             /* add flag bitfield subtrees */
1820             proto_tree_add_text(subtree3, tvb,
1821                     o + i + offsetof(struct bgp_attr, bgpa_flags), 1,
1822                     "%s", decode_boolean_bitfield(bgpa.bgpa_flags,
1823                         BGP_ATTR_FLAG_OPTIONAL, 8, "Optional", "Well-known"));
1824             proto_tree_add_text(subtree3, tvb,
1825                     o + i + offsetof(struct bgp_attr, bgpa_flags), 1,
1826                     "%s", decode_boolean_bitfield(bgpa.bgpa_flags,
1827                         BGP_ATTR_FLAG_TRANSITIVE, 8, "Transitive",
1828                         "Non-transitive"));
1829             proto_tree_add_text(subtree3, tvb,
1830                     o + i + offsetof(struct bgp_attr, bgpa_flags), 1,
1831                     "%s", decode_boolean_bitfield(bgpa.bgpa_flags,
1832                         BGP_ATTR_FLAG_PARTIAL, 8, "Partial", "Complete"));
1833             proto_tree_add_text(subtree3, tvb,
1834                     o + i + offsetof(struct bgp_attr, bgpa_flags), 1,
1835                     "%s", decode_boolean_bitfield(bgpa.bgpa_flags,
1836                         BGP_ATTR_FLAG_EXTENDED_LENGTH, 8, "Extended length",
1837                         "Regular length"));
1838
1839             proto_tree_add_text(subtree2, tvb,
1840                     o + i + offsetof(struct bgp_attr, bgpa_type), 1,
1841                     "Type code: %s (%u)",
1842                     val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
1843                     bgpa.bgpa_type);
1844
1845             proto_tree_add_text(subtree2, tvb, o + i + sizeof(bgpa),
1846                     aoff - sizeof(bgpa), "Length: %d %s", tlen,
1847                     (tlen == 1) ? "byte" : "bytes");
1848
1849             /* the second switch prints things in the actual subtree of each
1850                attribute                                                     */
1851             switch (bgpa.bgpa_type) {
1852             case BGPTYPE_ORIGIN:
1853                 if (tlen != 1) {
1854                     proto_tree_add_text(subtree2, tvb, o + i + aoff, tlen,
1855                             "Origin (invalid): %u %s", tlen,
1856                              (tlen == 1) ? "byte" : "bytes");
1857                 } else {
1858                     proto_tree_add_item(subtree2, hf_bgp_origin, tvb,
1859                             o + i + aoff, 1, FALSE);
1860                 }
1861                 break;
1862             case BGPTYPE_AS_PATH:
1863             case BGPTYPE_NEW_AS_PATH:
1864                 ti = proto_tree_add_text(subtree2, tvb, o + i + aoff, tlen,
1865                         "AS path: %s", as_path_gstr->str);
1866                 as_paths_tree = proto_item_add_subtree(ti, ett_bgp_as_paths);
1867
1868                 /* (o + i + aoff) =
1869                    (o + current attribute + aoff bytes to first tuple) */
1870                 q = o + i + aoff;
1871                 end = q + tlen;
1872
1873                 /* snarf each AS path tuple, we have to step through each one
1874                    again to make a separate subtree so we can't just reuse
1875                    as_path_gstr from above */
1876                 /* XXX - Can we use some g_string*() trickery instead, e.g. 
1877                    g_string_erase()? */
1878                 while (q < end) {
1879                     g_string_truncate(as_path_gstr, 0);
1880                     type = tvb_get_guint8(tvb, q++);
1881                     if (type == AS_SET) {
1882                         g_string_append_c(as_path_gstr, '{');
1883                     }
1884                     else if (type == AS_CONFED_SET) {
1885                         g_string_append_c(as_path_gstr, '[');
1886                     }
1887                     else if (type == AS_CONFED_SEQUENCE) {
1888                         g_string_append_c(as_path_gstr, '(');
1889                     }
1890                     length = tvb_get_guint8(tvb, q++);
1891
1892                     /* snarf each value in path */
1893                     for (j = 0; j < length; j++) {
1894                         g_string_sprintfa(as_path_gstr, "%u%s",
1895                                 (asn_len == 2) ? 
1896                                 tvb_get_ntohs(tvb, q) : tvb_get_ntohl(tvb, q),
1897                                 (type == AS_SET || type == AS_CONFED_SET) ? ", " : " ");
1898                         q += asn_len;
1899                     }
1900
1901                     /* cleanup end of string */
1902                     if (type == AS_SET) {
1903                         g_string_truncate(as_path_gstr, as_path_gstr->len - 2);
1904                         g_string_append_c(as_path_gstr, '}');
1905                     }
1906                     else if (type == AS_CONFED_SET) {
1907                         g_string_truncate(as_path_gstr, as_path_gstr->len - 2);
1908                         g_string_append_c(as_path_gstr, ']');
1909                     }
1910                     else if (type == AS_CONFED_SEQUENCE) {
1911                         g_string_truncate(as_path_gstr, as_path_gstr->len - 1);
1912                         g_string_append_c(as_path_gstr, ')');
1913                     }
1914                     else {
1915                         g_string_truncate(as_path_gstr, as_path_gstr->len - 1);
1916                     }
1917
1918                     /* length here means number of ASs, ie length * 2 bytes */
1919                     ti = proto_tree_add_text(as_paths_tree, tvb,
1920                             q - length * asn_len - 2,
1921                             length * asn_len + 2, "AS path segment: %s", as_path_gstr->str);
1922                     as_path_tree = proto_item_add_subtree(ti, ett_bgp_as_paths);
1923                     proto_tree_add_text(as_path_tree, tvb, q - length * asn_len - 2,
1924                             1, "Path segment type: %s (%u)",
1925                             val_to_str(type, as_segment_type, "Unknown"), type);
1926                     proto_tree_add_text(as_path_tree, tvb, q - length * asn_len - 1,
1927                             1, "Path segment length: %u %s", length,
1928                             (length == 1) ? "AS" : "ASs");
1929
1930                     /* backup and reprint path segment value(s) only */
1931                     q -= asn_len * length;
1932                     ti = proto_tree_add_text(as_path_tree, tvb, q,
1933                             length * asn_len, "Path segment value:");
1934                     as_path_segment_tree = proto_item_add_subtree(ti,
1935                             ett_bgp_as_path_segments);
1936                     for (j = 0; j < length; j++) {
1937                         as_path_item = (asn_len == 2) ? 
1938                                 tvb_get_ntohs(tvb, q) : tvb_get_ntohl(tvb, q);
1939                         proto_item_append_text(ti, " %u", as_path_item);
1940                         proto_tree_add_uint_hidden(as_path_tree, hf_bgp_as_path, tvb,
1941                             q, asn_len, as_path_item);
1942                         q += asn_len;
1943                     }
1944                 }
1945
1946                 break;
1947             case BGPTYPE_NEXT_HOP:
1948                 if (tlen != 4) {
1949                     proto_tree_add_text(subtree2, tvb, o + i + aoff, tlen,
1950                             "Next hop (invalid): %u %s", tlen,
1951                             (tlen == 1) ? "byte" : "bytes");
1952                 } else {
1953                     proto_tree_add_item(subtree2, hf_bgp_next_hop, tvb,
1954                             o + i + aoff, tlen, FALSE);
1955                 }
1956                 break;
1957             case BGPTYPE_MULTI_EXIT_DISC:
1958                 if (tlen != 4) {
1959                     proto_tree_add_text(subtree2, tvb, o + i + aoff, tlen,
1960                             "Multiple exit discriminator (invalid): %u %s",
1961                             tlen, (tlen == 1) ? "byte" : "bytes");
1962                 } else {
1963                     proto_tree_add_item(subtree2, hf_bgp_multi_exit_disc, tvb,
1964                             o + i + aoff, tlen, FALSE);
1965                 }
1966                 break;
1967             case BGPTYPE_LOCAL_PREF:
1968                 if (tlen != 4) {
1969                     proto_tree_add_text(subtree2, tvb, o + i + aoff, tlen,
1970                             "Local preference (invalid): %u %s", tlen,
1971                              (tlen == 1) ? "byte" : "bytes");
1972                 } else {
1973                     proto_tree_add_item(subtree2, hf_bgp_local_pref, tvb,
1974                             o + i + aoff, tlen, FALSE);
1975                 }
1976                 break;
1977             case BGPTYPE_ATOMIC_AGGREGATE:
1978                 if (tlen != 0) {
1979                     proto_tree_add_text(subtree2, tvb, o + i + aoff, tlen,
1980                             "Atomic aggregate (invalid): %u %s", tlen,
1981                             (tlen == 1) ? "byte" : "bytes");
1982                 }
1983                 break;
1984             case BGPTYPE_AGGREGATOR:
1985                 if (tlen != 6 && tlen != 8) {
1986                     proto_tree_add_text(subtree2, tvb, o + i + aoff, tlen,
1987                             "Aggregator (invalid): %u %s", tlen,
1988                             (tlen == 1) ? "byte" : "bytes");
1989                     break;
1990                 }
1991             case BGPTYPE_NEW_AGGREGATOR:
1992                 if (bgpa.bgpa_type == BGPTYPE_NEW_AGGREGATOR && tlen != 8)
1993                     proto_tree_add_text(subtree2, tvb, o + i + aoff, tlen,
1994                             "Aggregator (invalid): %u %s", tlen,
1995                             (tlen == 1) ? "byte" : "bytes");
1996                 else {
1997                     asn_len = tlen - 4;
1998                     aggregator_as = (asn_len == 2) ?
1999                             tvb_get_ntohs(tvb, o + i + aoff) :
2000                             tvb_get_ntohl(tvb, o + i + aoff);
2001                     proto_tree_add_uint(subtree2, hf_bgp_aggregator_as, tvb,
2002                             o + i + aoff, asn_len, aggregator_as);
2003                     proto_tree_add_item(subtree2, hf_bgp_aggregator_origin, tvb,
2004                         o + i + aoff + asn_len, 4, FALSE);
2005                 }
2006                 break;
2007             case BGPTYPE_COMMUNITIES:
2008                 if (tlen % 4 != 0) {
2009                     proto_tree_add_text(subtree2, tvb, o + i + aoff, tlen,
2010                             "Communities (invalid): %u %s", tlen,
2011                             (tlen == 1) ? "byte" : "bytes");
2012                     break;
2013                 }
2014
2015                 ti = proto_tree_add_text(subtree2, tvb, o + i + aoff, tlen,
2016                         "Communities: %s", communities_gstr->str);
2017                 communities_tree = proto_item_add_subtree(ti,
2018                         ett_bgp_communities);
2019
2020                 /* (o + i + aoff) =
2021                    (o + current attribute + aoff bytes to first tuple) */
2022                 q = o + i + aoff;
2023                 end = q + tlen;
2024
2025                 /* snarf each community */
2026                 while (q < end) {
2027                     /* check for reserved values */
2028                     guint32 community = tvb_get_ntohl(tvb, q);
2029                     if ((community & 0xFFFF0000) == FOURHEX0 ||
2030                          (community & 0xFFFF0000) == FOURHEXF) {
2031                         proto_tree_add_text(communities_tree, tvb,
2032                                q - 3 + aoff, 4,
2033                                "Community: %s (0x%08x)",
2034                                val_to_str(community, community_vals, "(reserved)"),
2035                                community);
2036                     }
2037                     else {
2038                         ti = proto_tree_add_text(communities_tree, tvb,
2039                                 q - 3 + aoff, 4, "Community: %u:%u",
2040                                 tvb_get_ntohs(tvb, q), tvb_get_ntohs(tvb, q + 2));
2041                         community_tree = proto_item_add_subtree(ti,
2042                             ett_bgp_communities);
2043                         proto_tree_add_item(community_tree, hf_bgp_community_as,
2044                             tvb, q - 3 + aoff, 2, FALSE);
2045                         proto_tree_add_item(community_tree, hf_bgp_community_value,
2046                             tvb, q - 1 + aoff, 2, FALSE);
2047                     }
2048
2049                     q += 4;
2050                 }
2051
2052                 break;
2053             case BGPTYPE_ORIGINATOR_ID:
2054                 if (tlen != 4) {
2055                     proto_tree_add_text(subtree2, tvb, o + i + aoff, tlen,
2056                             "Originator identifier (invalid): %u %s", tlen,
2057                             (tlen == 1) ? "byte" : "bytes");
2058                 } else {
2059                     proto_tree_add_item(subtree2, hf_bgp_originator_id, tvb,
2060                             o + i + aoff, tlen, FALSE);
2061                 }
2062                 break;
2063            case BGPTYPE_MP_REACH_NLRI:
2064                 /*
2065                  * RFC 2545 specifies that there may be more than one
2066                  * address in the MP_REACH_NLRI attribute in section
2067                  * 3, "Constructing the Next Hop field".
2068                  *
2069                  * Yes, RFC 2858 says you can't do that, and, yes, RFC
2070                  * 2858 obsoletes RFC 2283, which says you can do that,
2071                  * but that doesn't mean we shouldn't dissect packets
2072                  * that conform to RFC 2283 but not RFC 2858, as some
2073                  * device on the network might implement the 2283-style
2074                  * BGP extensions rather than RFC 2858-style extensions.
2075                  */
2076                 af = tvb_get_ntohs(tvb, o + i + aoff);
2077                 proto_tree_add_text(subtree2, tvb, o + i + aoff, 2,
2078                     "Address family: %s (%u)",
2079                     val_to_str(af, afn_vals, "Unknown"), af);
2080                 saf = tvb_get_guint8(tvb, o + i + aoff + 2) ;
2081                 proto_tree_add_text(subtree2, tvb, o + i + aoff + 2, 1,
2082                     "Subsequent address family identifier: %s (%u)",
2083                     val_to_str(saf, bgpattr_nlri_safi, saf >= 128 ? "Vendor specific" : "Unknown"),
2084                     saf);
2085                 nexthop_len = tvb_get_guint8(tvb, o + i + aoff + 3);
2086                 ti = proto_tree_add_text(subtree2, tvb, o + i + aoff + 3,
2087                         nexthop_len + 1,
2088                         "Next hop network address (%d %s)",
2089                         nexthop_len, plurality(nexthop_len, "byte", "bytes"));
2090                 subtree3 = proto_item_add_subtree(ti, ett_bgp_mp_nhna);
2091
2092                 /*
2093                  * The addresses don't contain lengths, so if we
2094                  * don't understand the address family type, we
2095                  * cannot parse the subsequent addresses as we
2096                  * don't know how long they are.
2097                  */
2098                 switch (af) {
2099                 default:
2100                     proto_tree_add_text(subtree3, tvb, o + i + aoff + 4,
2101                         nexthop_len, "Unknown Address Family");
2102                     break;
2103
2104                 case AFNUM_INET:
2105                 case AFNUM_INET6:
2106                 case AFNUM_L2VPN:
2107                 case AFNUM_L2VPN_OLD:
2108
2109                     j = 0;
2110                     while (j < nexthop_len) {
2111                         advance = mp_addr_to_str(af, saf, tvb, o + i + aoff + 4 + j,
2112                                 junk_gbuf, MAX_STR_LEN) ;
2113                         if (advance == 0) /* catch if this is a unknown AFI type*/
2114                                 break;
2115                         if (j + advance > nexthop_len)
2116                                 break;
2117                         proto_tree_add_text(subtree3, tvb,o + i + aoff + 4 + j,
2118                                 advance, "Next hop: %s (%u)", junk_gbuf, advance);
2119                         j += advance;
2120                     }
2121                     break;
2122                 }
2123
2124                 tlen -= nexthop_len + 4;
2125                 aoff += nexthop_len + 4 ;
2126
2127                 off = 0;
2128                 snpa = tvb_get_guint8(tvb, o + i + aoff);
2129                 ti = proto_tree_add_text(subtree2, tvb, o + i + aoff, 1,
2130                         "Subnetwork points of attachment: %u", snpa);
2131                 off++;
2132                 if (snpa) {
2133                         subtree3 = proto_item_add_subtree(ti, ett_bgp_mp_snpa);
2134                         for (/*nothing*/; snpa > 0; snpa--) {
2135                                 proto_tree_add_text(subtree3, tvb, o + i + aoff + off, 1,
2136                                         "SNPA length: %u", tvb_get_guint8(tvb, o + i + aoff + off));
2137                                 off++;
2138                                 proto_tree_add_text(subtree3, tvb, o + i + aoff + off,
2139                                 tvb_get_guint8(tvb, o + i + aoff + off - 1),
2140                                         "SNPA (%u %s)", tvb_get_guint8(tvb, o + i + aoff + off - 1),
2141                                         (tvb_get_guint8(tvb, o + i + aoff + off - 1) == 1) ? "byte" : "bytes");
2142                                 off += tvb_get_guint8(tvb, o + i + aoff + off - 1);
2143                         }
2144                 }
2145                 tlen -= off;
2146                 aoff += off;
2147
2148                 ti = proto_tree_add_text(subtree2, tvb, o + i + aoff, tlen,
2149                         "Network layer reachability information (%u %s)",
2150                         tlen, (tlen == 1) ? "byte" : "bytes");
2151                 if (tlen)  {
2152                     subtree3 = proto_item_add_subtree(ti,ett_bgp_mp_reach_nlri);
2153                     if (af != AFNUM_INET && af != AFNUM_INET6 && af != AFNUM_L2VPN) {
2154                         proto_tree_add_text(subtree3, tvb, o + i + aoff,
2155                                 tlen, "Unknown Address Family");
2156                     } else {
2157                         while (tlen > 0) {
2158                                 advance = decode_prefix_MP(subtree3,
2159                                     hf_bgp_mp_reach_nlri_ipv4_prefix,
2160                                     -1,
2161                                     af, saf,
2162                                     tvb, o + i + aoff, "MP Reach NLRI");
2163                                 if (advance < 0)
2164                                     break;
2165                                 tlen -= advance;
2166                                 aoff += advance;
2167                         }
2168                     }
2169                 }
2170                 break;
2171            case BGPTYPE_MP_UNREACH_NLRI:
2172                 af = tvb_get_ntohs(tvb, o + i + aoff);
2173                 proto_tree_add_text(subtree2, tvb, o + i + aoff, 2,
2174                     "Address family: %s (%u)",
2175                     val_to_str(af, afn_vals, "Unknown"), af);
2176                 saf = tvb_get_guint8(tvb, o + i + aoff + 2) ;
2177                 proto_tree_add_text(subtree2, tvb, o + i + aoff + 2, 1,
2178                     "Subsequent address family identifier: %s (%u)",
2179                     val_to_str(saf, bgpattr_nlri_safi, saf >= 128 ? "Vendor specific" : "Unknown"),
2180                     saf);
2181                 ti = proto_tree_add_text(subtree2, tvb, o + i + aoff + 3,
2182                         tlen - 3, "Withdrawn routes (%u %s)", tlen - 3,
2183                         (tlen - 3 == 1) ? "byte" : "bytes");
2184
2185                 tlen -= 3;
2186                 aoff += 3;
2187                 if (tlen > 0) {
2188                     subtree3 = proto_item_add_subtree(ti,ett_bgp_mp_unreach_nlri);
2189
2190                     while (tlen > 0) {
2191                         advance = decode_prefix_MP(subtree3,
2192                                 hf_bgp_mp_unreach_nlri_ipv4_prefix,
2193                                 -1,
2194                                 af, saf,
2195                                 tvb, o + i + aoff, "MP Unreach NLRI");
2196                         if (advance < 0)
2197                             break;
2198                         tlen -= advance;
2199                         aoff += advance;
2200                     }
2201                 }
2202                 break;
2203             case BGPTYPE_CLUSTER_LIST:
2204                 if (tlen % 4 != 0) {
2205                     proto_tree_add_text(subtree2, tvb, o + i + aoff, tlen,
2206                             "Cluster list (invalid): %u %s", tlen,
2207                             (tlen == 1) ? "byte" : "bytes");
2208                     break;
2209                 }
2210
2211                 ti = proto_tree_add_text(subtree2, tvb, o + i + aoff, tlen,
2212                         "Cluster list: %s", cluster_list_gstr->str);
2213                 cluster_list_tree = proto_item_add_subtree(ti,
2214                         ett_bgp_cluster_list);
2215
2216                 /* (o + i + aoff) =
2217                    (o + current attribute + aoff bytes to first tuple) */
2218                 q = o + i + aoff;
2219                 end = q + tlen;
2220
2221                 /* snarf each cluster identifier */
2222                 while (q < end) {
2223                     proto_tree_add_item(cluster_list_tree, hf_bgp_cluster_list,
2224                             tvb, q - 3 + aoff, 4, FALSE);
2225                     q += 4;
2226                 }
2227
2228                 break;
2229             case BGPTYPE_EXTENDED_COMMUNITY:
2230                 if (tlen %8 != 0) {
2231                         proto_tree_add_text(subtree3, tvb, o + i + aoff, tlen, "Extended community (invalid) : %u %s", tlen,
2232                                 (tlen == 1) ? "byte" : "bytes") ;
2233                 } else {
2234                         q = o + i + aoff ;
2235                         end = o + i + aoff + tlen ;
2236                         ti = proto_tree_add_text(subtree2,tvb,q,tlen, "Carried Extended communities");
2237                         subtree3 = proto_item_add_subtree(ti,ett_bgp_extended_communities) ;
2238
2239                         while (q < end) {
2240                             ext_com = tvb_get_ntohs(tvb,q) ;
2241                             junk_gbuf[0]=0;
2242                             junk_gbuf_ptr=junk_gbuf;
2243                             junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), "%s",
2244                                                   val_to_str(ext_com,bgpext_com_type,"Unknown"));
2245                             switch (ext_com) {
2246                             case BGP_EXT_COM_RT_0:
2247                             case BGP_EXT_COM_RT_2:
2248                             case BGP_EXT_COM_RO_0:
2249                             case BGP_EXT_COM_RO_2:
2250                                 junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), ": %u%s%d",
2251                                                        tvb_get_ntohs(tvb,q+2),":",tvb_get_ntohl(tvb,q+4));
2252                                 proto_tree_add_text(subtree3,tvb,q,8, "%s",junk_gbuf);
2253                                 break ;
2254                             case BGP_EXT_COM_RT_1:
2255                             case BGP_EXT_COM_RO_1:
2256                                 ipaddr = tvb_get_ipv4(tvb,q+2);
2257                                 junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), ": %s%s%u",
2258                                                        ip_to_str((guint8 *)&ipaddr),":",tvb_get_ntohs(tvb,q+6));
2259                                 proto_tree_add_text(subtree3,tvb,q,8, "%s",junk_gbuf);
2260                                 break;
2261                             case BGP_EXT_COM_VPN_ORIGIN:
2262                             case BGP_EXT_COM_OSPF_RID:
2263                                 ipaddr = tvb_get_ipv4(tvb,q+2);
2264                                 junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), ": %s", ip_to_str((guint8 *)&ipaddr));
2265                                 proto_tree_add_text(subtree3,tvb,q,8, "%s",junk_gbuf);
2266                                 break;
2267                             case BGP_EXT_COM_OSPF_RTYPE:
2268                                 ipaddr = tvb_get_ipv4(tvb,q+2);
2269                                 junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), ": Area: %s, Type: %s", ip_to_str((guint8 *)&ipaddr),
2270                                          val_to_str(tvb_get_guint8(tvb,q+6),bgpext_ospf_rtype,"Unknown"));
2271                                 /* print OSPF Metric type if selected */
2272                                 /* always print E2 even if not external route -- receiving router should ignore */
2273                                 if ( (tvb_get_guint8(tvb,q+7)) & BGP_OSPF_RTYPE_METRIC_TYPE ) {
2274                                     junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), " E2");
2275                                 } else if ((tvb_get_guint8(tvb,q+6)==BGP_OSPF_RTYPE_EXT) || (tvb_get_guint8(tvb,q+6)==BGP_OSPF_RTYPE_NSSA)) {
2276                                     junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), " E1");
2277                                 } else {
2278                                     junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), ", no options");
2279                                 }
2280                                 proto_tree_add_text(subtree3,tvb,q,8, "%s",junk_gbuf);
2281                                 break;
2282                             case BGP_EXT_COM_LINKBAND:
2283                                 linkband = tvb_get_ntohieee_float(tvb,q+2);
2284                                 junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), ": %.3f Mbps",
2285                                                        linkband*8/1000000);
2286                                 proto_tree_add_text(subtree3,tvb,q,8, "%s",junk_gbuf);
2287                                 break;
2288                             case BGP_EXT_COM_L2INFO:
2289                                 junk_gbuf_ptr += g_snprintf(junk_gbuf_ptr, MAX_STR_LEN-(junk_gbuf_ptr-junk_gbuf), 
2290                                                        ": %s, Control Flags: %s%s%s%s%s, MTU: %u %s",
2291                                                        val_to_str(tvb_get_guint8(tvb,q+2),bgp_l2vpn_encaps,"Unknown"),
2292                                                        tvb_get_guint8(tvb,q+3) ? "" : "none",
2293                                                        tvb_get_ntohs(tvb,q+3)&0x08 ? "Q" : "",
2294                                                        tvb_get_ntohs(tvb,q+3)&0x04 ? "F" : "",
2295                                                        tvb_get_ntohs(tvb,q+3)&0x02 ? "C" : "",
2296                                                        tvb_get_ntohs(tvb,q+3)&0x01 ? "S" : "",
2297                                                        tvb_get_ntohs(tvb,q+4),
2298                                                        tvb_get_ntohs(tvb,q+4)==1 ? "byte" : "bytes");
2299                                 ti = proto_tree_add_text(subtree3,tvb,q,8, "%s",junk_gbuf);
2300
2301                                 subtree4 = proto_item_add_subtree(ti,ett_bgp_extended_communities) ;
2302                                 proto_tree_add_text(subtree4,tvb,q+2,1, "Encapsulation: %s",
2303                                                          val_to_str(tvb_get_guint8(tvb,q+2),bgp_l2vpn_encaps,"Unknown"));
2304                                 proto_tree_add_text(subtree4,tvb,q+3,1, "Control Flags: %s%sControl Word %s required, Sequenced delivery %s required",
2305                                                     tvb_get_ntohs(tvb,q+3)&0x08 ? "Q flag (Reserved) set" : "",
2306                                                     tvb_get_ntohs(tvb,q+3)&0x04 ? "F flag (reserved) set" : "",
2307                                                     tvb_get_ntohs(tvb,q+3)&0x02 ? "is" : "not",
2308                                                     tvb_get_ntohs(tvb,q+3)&0x01 ? "is" : "not");
2309                                 proto_tree_add_text(subtree4,tvb,q+4,2, "MTU: %u %s",
2310                                                     tvb_get_ntohs(tvb,q+4),
2311                                                     tvb_get_ntohs(tvb,q+4)==1 ? "byte" : "bytes");
2312                                 break;
2313                             default:
2314                                 proto_tree_add_text(subtree3,tvb,q,8, "%s",junk_gbuf);
2315                                 break ;
2316                             }
2317                             q = q + 8 ;
2318                         }
2319                 }
2320                 break;
2321             case BGPTYPE_SAFI_SPECIFIC_ATTR:
2322                 q = o + i + aoff;
2323                 end = o + i + aoff + tlen ;
2324
2325                 while(q < end) {
2326                     ssa_type = tvb_get_ntohs(tvb, q) & BGP_SSA_TYPE;
2327                     ssa_len = tvb_get_ntohs(tvb, q + 2);
2328
2329                     ti = proto_tree_add_text(subtree2, tvb, q, MIN(ssa_len + 4, end - q),
2330                             "%s Information",
2331                             val_to_str(ssa_type, bgp_ssa_type, "Unknown SSA"));
2332                     subtree3 = proto_item_add_subtree(ti, ett_bgp_ssa);
2333
2334                     proto_tree_add_item(subtree3, hf_bgp_ssa_t, tvb,
2335                             q, 1, FALSE);
2336                     proto_tree_add_item_hidden(subtree3, hf_bgp_ssa_type, tvb,
2337                             q, 2, FALSE);
2338                     proto_tree_add_text(subtree3, tvb, q, 2,
2339                             "Type: %s", val_to_str(ssa_type, bgp_ssa_type, "Unknown"));
2340                     if ((ssa_len == 0) || (q + ssa_len > end)) {
2341                         proto_tree_add_text(subtree3, tvb, q + 2, end - q - 2,
2342                                 "Invalid Length of %u", ssa_len);
2343                         break;
2344                     }
2345                     proto_tree_add_item(subtree3, hf_bgp_ssa_len, tvb,
2346                             q + 2, 2, FALSE);
2347
2348                     switch(ssa_type){
2349                     case BGP_SSA_L2TPv3:
2350                             proto_tree_add_item(subtree3, hf_bgp_ssa_l2tpv3_pref, tvb,
2351                                     q + 4, 2, FALSE);
2352
2353                             ti = proto_tree_add_text(subtree3, tvb, q + 6, 1, "Flags");
2354                             subtree4 = proto_item_add_subtree(ti, ett_bgp_ssa_subtree) ;
2355                             proto_tree_add_item(subtree4, hf_bgp_ssa_l2tpv3_s, tvb,
2356                                     q + 6, 1, FALSE);
2357                             proto_tree_add_item(subtree4, hf_bgp_ssa_l2tpv3_unused, tvb,
2358                                     q + 6, 1, FALSE);
2359
2360                             ssa_v3_len = tvb_get_guint8(tvb, q + 7);
2361                             if (ssa_v3_len + 8 == ssa_len){
2362                                 proto_tree_add_item(subtree3, hf_bgp_ssa_l2tpv3_cookie_len, tvb,
2363                                         q + 7, 1, FALSE);
2364                             } else {
2365                                 proto_tree_add_text(subtree3, tvb, q + 7, 1,
2366                                         "Invalid Cookie Length of %u", ssa_v3_len);
2367                                 break;
2368                             }
2369                             proto_tree_add_item(subtree3, hf_bgp_ssa_l2tpv3_session_id, tvb,
2370                                     q + 8, 4, FALSE);
2371                             if (ssa_v3_len)
2372                                     proto_tree_add_item(subtree3, hf_bgp_ssa_l2tpv3_cookie, tvb,
2373                                             q + 12, ssa_v3_len, FALSE);
2374                             break;
2375                     case BGP_SSA_mGRE:
2376                     case BGP_SSA_IPSec:
2377                     case BGP_SSA_MPLS:
2378                     default:
2379                             proto_tree_add_item(subtree3, hf_bgp_ssa_value, tvb,
2380                                     q + 4, ssa_len, FALSE);
2381                             break;
2382                     }
2383                     q = q + ssa_len + 4; /* 4 from type and length */
2384                 }
2385                 break;
2386
2387             default:
2388                 proto_tree_add_text(subtree2, tvb, o + i + aoff, tlen,
2389                         "Unknown (%d %s)", tlen, (tlen == 1) ? "byte" :
2390                         "bytes");
2391                 break;
2392             } /* end of second switch */
2393
2394             i += alen + aoff;
2395         }
2396
2397         o += 2 + len;
2398
2399         /* NLRI */
2400         len = hlen - o;
2401
2402         /* parse prefixes */
2403         if (len > 0) {
2404             ti = proto_tree_add_text(tree, tvb, o, len,
2405                    "Network layer reachability information: %u %s", len,
2406                    (len == 1) ? "byte" : "bytes");
2407             subtree = proto_item_add_subtree(ti, ett_bgp_nlri);
2408             end = o + len;
2409             while (o < end) {
2410                 i = decode_prefix4(subtree, hf_bgp_nlri_prefix, tvb, o, 0,
2411                     "NLRI");
2412                 if (i < 0)
2413                     return;
2414                 o += i;
2415             }
2416         }
2417     }
2418 }
2419
2420 /*
2421  * Dissect a BGP NOTIFICATION message.
2422  */
2423 static void
2424 dissect_bgp_notification(tvbuff_t *tvb, proto_tree *tree)
2425 {
2426     struct bgp_notification bgpn;   /* BGP NOTIFICATION message */
2427     int                     hlen;   /* message length           */
2428     const char              *p;     /* string pointer           */
2429
2430     /* snarf message */
2431     tvb_memcpy(tvb, bgpn.bgpn_marker, 0, BGP_MIN_NOTIFICATION_MSG_SIZE);
2432     hlen = g_ntohs(bgpn.bgpn_len);
2433
2434     /* print error code */
2435     proto_tree_add_text(tree, tvb,
2436         offsetof(struct bgp_notification, bgpn_major), 1,
2437         "Error code: %s (%u)",
2438         val_to_str(bgpn.bgpn_major, bgpnotify_major, "Unknown"),
2439         bgpn.bgpn_major);
2440
2441     /* print error subcode */
2442     if (bgpn.bgpn_major < array_length(bgpnotify_minor)
2443      && bgpnotify_minor[bgpn.bgpn_major] != NULL) {
2444         p = val_to_str(bgpn.bgpn_minor, bgpnotify_minor[bgpn.bgpn_major],
2445             "Unknown");
2446     } else if (bgpn.bgpn_minor == 0)
2447         p = "Unspecified";
2448     else
2449         p = "Unknown";
2450     proto_tree_add_text(tree, tvb,
2451         offsetof(struct bgp_notification, bgpn_minor), 1,
2452         "Error subcode: %s (%u)", p, bgpn.bgpn_minor);
2453
2454     /* only print if there is optional data */
2455     if (hlen > BGP_MIN_NOTIFICATION_MSG_SIZE) {
2456         proto_tree_add_text(tree, tvb, BGP_MIN_NOTIFICATION_MSG_SIZE,
2457             hlen - BGP_MIN_NOTIFICATION_MSG_SIZE, "Data");
2458     }
2459 }
2460
2461 /*
2462  * Dissect a BGP ROUTE-REFRESH message.
2463  */
2464 static void
2465 dissect_bgp_route_refresh(tvbuff_t *tvb, proto_tree *tree)
2466 {
2467     guint16         i;    /* tmp            */
2468     int             p;         /* tvb offset counter    */
2469     int             pend;       /* end of list of entries for one orf type */
2470     guint16         hlen;       /* tvb RR msg length */
2471     proto_item      *ti;       /* tree item             */
2472     proto_item      *ti1;       /* tree item             */
2473     proto_tree      *subtree;  /* tree for orf   */
2474     proto_tree      *subtree1; /* tree for orf entry */
2475     guint8          orftype;        /* ORF Type */
2476     guint8          orfwhen;        /* ORF flag: immediate, defer */
2477     guint16         orflen;         /* ORF len */
2478     guint8          entryflag;      /* ORF Entry flag: action(add,del,delall) match(permit,deny) */
2479     guint32         entryseq;       /* ORF Entry sequence number */
2480     int             entrylen;       /* ORF Entry length */
2481     guint8          pfx_ge;         /* ORF PrefixList mask lower bound */
2482     guint8          pfx_le;         /* ORF PrefixList mask upper bound */
2483     int             advance;        /* tmp                      */
2484
2485
2486 /*
2487 example 1
2488  00 1c 05       hlen=28
2489  00 01 00 01    afi,safi= ipv4-unicast
2490  02 80 00 01    defer, prefix-orf, len=1
2491     80            removeall
2492 example 2
2493  00 25 05       hlen=37
2494  00 01 00 01    afi,saif= ipv4-unicast
2495  01 80 00 0a    immediate, prefix-orf, len=10
2496     00            add
2497     00 00 00 05   seqno = 5
2498     12            ge = 18
2499     18            le = 24
2500     10 07 02      prefix = 7.2.0.0/16
2501 */
2502     hlen = tvb_get_ntohs(tvb, BGP_MARKER_SIZE);
2503     p = BGP_HEADER_SIZE;
2504     /* AFI */
2505     i = tvb_get_ntohs(tvb, p);
2506     proto_tree_add_text(tree, tvb, p, 2,
2507                         "Address family identifier: %s (%u)",
2508                         val_to_str(i, afn_vals, "Unknown"), i);
2509     p += 2;
2510     /* Reserved */
2511     proto_tree_add_text(tree, tvb, p, 1,
2512                         "Reserved: 1 byte");
2513     p++;
2514     /* SAFI */
2515     i = tvb_get_guint8(tvb, p);
2516     proto_tree_add_text(tree, tvb, p, 1,
2517                         "Subsequent address family identifier: %s (%u)",
2518                         val_to_str(i, bgpattr_nlri_safi,
2519                         i >= 128 ? "Vendor specific" : "Unknown"),
2520                         i);
2521     p++;
2522     if ( hlen == BGP_HEADER_SIZE + 4 )
2523         return;
2524     while (p < hlen) {
2525         /* ORF type */
2526         orfwhen = tvb_get_guint8(tvb, p);
2527         orftype = tvb_get_guint8(tvb, p+1);
2528         orflen = tvb_get_ntohs(tvb, p+2);
2529         ti = proto_tree_add_text(tree, tvb, p , orflen + 4 , "ORF information (%u bytes)", orflen + 4);
2530         subtree = proto_item_add_subtree(ti, ett_bgp_orf);
2531         proto_tree_add_text(subtree, tvb, p , 1, "ORF flag: %s", val_to_str(orfwhen, orf_when_vals,"UNKNOWN"));
2532         proto_tree_add_text(subtree, tvb, p+1 , 1, "ORF type: %s", val_to_str(orftype, orf_type_vals,"UNKNOWN"));
2533         proto_tree_add_text(subtree, tvb, p+2 , 2, "ORF len: %u %s", orflen, (orflen == 1) ? "byte" : "bytes");
2534         p += 4;
2535
2536         if (orftype != BGP_ORF_PREFIX_CISCO) {
2537             proto_tree_add_text(subtree, tvb, p, orflen,
2538                     "ORFEntry-Unknown (%u bytes)", orflen);
2539             p += orflen;
2540             continue;
2541         }
2542         pend = p + orflen;
2543         while (p < pend) {
2544             entryflag = tvb_get_guint8(tvb, p);
2545             if ((entryflag & BGP_ORF_ACTION) == BGP_ORF_REMOVEALL) {
2546                 ti1 = proto_tree_add_text(subtree, tvb, p, 1,
2547                         "ORFEntry-PrefixList (1 byte)");
2548                 subtree1 = proto_item_add_subtree(ti1, ett_bgp_orf_entry);
2549                 proto_tree_add_text(subtree1, tvb, p , 1, "RemoveAll");
2550                 p++;
2551             } else {
2552                 ti1 = proto_tree_add_text(subtree, tvb, p, -1,
2553                         "ORFEntry-PrefixList");
2554                 subtree1 = proto_item_add_subtree(ti1, ett_bgp_orf_entry);
2555                 proto_tree_add_text(subtree1, tvb, p, 1,
2556                         "ACTION: %s MATCH: %s",
2557                         val_to_str(entryflag&BGP_ORF_ACTION,
2558                             orf_entry_action_vals, "UNKNOWN"),
2559                         val_to_str(entryflag&BGP_ORF_MATCH,
2560                             orf_entry_match_vals, "UNKNOWN"));
2561                 p++;
2562                 entryseq = tvb_get_ntohl(tvb, p);
2563                 proto_tree_add_text(subtree1, tvb, p, 4,
2564                         "Entry Sequence No: %u", entryseq);
2565                 p += 4;
2566                 pfx_ge = tvb_get_guint8(tvb, p);
2567                 proto_tree_add_text(subtree1, tvb, p, 1,
2568                         "PrefixMask length lower bound: %u", pfx_ge);
2569                 p++;
2570                 pfx_le = tvb_get_guint8(tvb, p);
2571                 proto_tree_add_text(subtree1, tvb, p, 1,
2572                         "PrefixMask length upper bound: %u", pfx_le);
2573                 p++;
2574
2575                 advance = decode_prefix4(subtree1, -1, tvb, p, 0, "ORF");
2576                 if (advance < 0)
2577                         break;
2578                 entrylen = 7 + 1 + advance;
2579
2580                 proto_item_append_text(ti1, " (%u bytes)", entrylen);
2581                 proto_item_set_len(ti1, entrylen);
2582                 p += advance;
2583             }
2584         }
2585     }
2586 }
2587
2588 /*
2589  * Dissect a BGP CAPABILITY message.
2590  */
2591 static void
2592 dissect_bgp_capability(tvbuff_t *tvb, proto_tree *tree)
2593 {
2594     int offset = 0;
2595     proto_item *ti;
2596     proto_tree *subtree;
2597     guint8  action;
2598     int ctype;
2599     int clen;
2600     int mend;
2601
2602     mend = offset + tvb_get_ntohs(tvb, offset + BGP_MARKER_SIZE);
2603     offset += BGP_HEADER_SIZE;
2604     /* step through all of the capabilities */
2605     while (offset < mend) {
2606         action = tvb_get_guint8(tvb, offset++);
2607         ctype  = tvb_get_guint8(tvb, offset++);
2608         clen   = tvb_get_guint8(tvb, offset++);
2609
2610         ti = proto_tree_add_text(tree, tvb, offset - 2, 2 + clen, 
2611              "%s (%u %s)", val_to_str(ctype, capability_vals,
2612              "Unknown capability"), 2 + clen, (clen == 1) ? "byte" : "bytes");
2613         subtree = proto_item_add_subtree(ti, ett_bgp_option);
2614         proto_tree_add_text(subtree, tvb, offset-2, 1, "Action: %d (%s)",
2615             action, val_to_str(action, bgpcap_action, "Invalid action value"));
2616         dissect_bgp_capability_item(tvb, &offset, subtree, ctype, clen);
2617     }
2618 }
2619
2620 static void
2621 dissect_bgp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
2622     gboolean first)
2623 {
2624     guint16       bgp_len;       /* Message length             */
2625     guint8        bgp_type;      /* Message type               */
2626     const char    *typ;          /* Message type (string)      */
2627     proto_item    *ti;           /* tree item                        */
2628     proto_tree    *bgp_tree;     /* BGP packet tree                  */
2629     proto_tree    *bgp1_tree;    /* BGP message tree                 */
2630
2631     bgp_len = tvb_get_ntohs(tvb, BGP_MARKER_SIZE);
2632     bgp_type = tvb_get_guint8(tvb, BGP_MARKER_SIZE + 2);
2633     typ = val_to_str(bgp_type, bgptypevals, "Unknown message type (0x%02x)");
2634
2635     if (check_col(pinfo->cinfo, COL_INFO)) {
2636         if (first)
2637             col_add_fstr(pinfo->cinfo, COL_INFO, "%s", typ);
2638         else
2639             col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", typ);
2640     }
2641
2642     if (tree) {
2643         ti = proto_tree_add_item(tree, proto_bgp, tvb, 0, -1, FALSE);
2644         bgp_tree = proto_item_add_subtree(ti, ett_bgp);
2645
2646         ti = proto_tree_add_text(bgp_tree, tvb, 0, -1, "%s", typ);
2647
2648         /* add a different tree for each message type */
2649         switch (bgp_type) {
2650         case BGP_OPEN:
2651             bgp1_tree = proto_item_add_subtree(ti, ett_bgp_open);
2652             break;
2653         case BGP_UPDATE:
2654             bgp1_tree = proto_item_add_subtree(ti, ett_bgp_update);
2655             break;
2656         case BGP_NOTIFICATION:
2657             bgp1_tree = proto_item_add_subtree(ti, ett_bgp_notification);
2658             break;
2659         case BGP_KEEPALIVE:
2660             bgp1_tree = proto_item_add_subtree(ti, ett_bgp);
2661             break;
2662         case BGP_ROUTE_REFRESH_CISCO:
2663         case BGP_ROUTE_REFRESH:
2664             bgp1_tree = proto_item_add_subtree(ti, ett_bgp_route_refresh);
2665             break;
2666         case BGP_CAPABILITY:
2667             bgp1_tree = proto_item_add_subtree(ti, ett_bgp_capability);
2668             break;
2669         default:
2670             bgp1_tree = proto_item_add_subtree(ti, ett_bgp);
2671             break;
2672         }
2673
2674         proto_tree_add_text(bgp1_tree, tvb, 0, BGP_MARKER_SIZE,
2675                             "Marker: 16 bytes");
2676
2677         if (bgp_len < BGP_HEADER_SIZE || bgp_len > BGP_MAX_PACKET_SIZE) {
2678             proto_tree_add_text(bgp1_tree, tvb, BGP_MARKER_SIZE, 2,
2679                                 "Length (invalid): %u %s", bgp_len,
2680                                 (bgp_len == 1) ? "byte" : "bytes");
2681             return;
2682         } else {
2683             proto_tree_add_text(bgp1_tree, tvb, BGP_MARKER_SIZE, 2,
2684                                 "Length: %u %s", bgp_len,
2685                                 (bgp_len == 1) ? "byte" : "bytes");
2686         }
2687
2688         proto_tree_add_uint(bgp1_tree, hf_bgp_type, tvb,
2689                                    BGP_MARKER_SIZE + 2, 1,
2690                                    bgp_type);
2691
2692         switch (bgp_type) {
2693         case BGP_OPEN:
2694             dissect_bgp_open(tvb, bgp1_tree);
2695             break;
2696         case BGP_UPDATE:
2697             dissect_bgp_update(tvb, bgp1_tree);
2698             break;
2699         case BGP_NOTIFICATION:
2700             dissect_bgp_notification(tvb, bgp1_tree);
2701             break;
2702         case BGP_KEEPALIVE:
2703             /* no data in KEEPALIVE messages */
2704             break;
2705         case BGP_ROUTE_REFRESH_CISCO:
2706         case BGP_ROUTE_REFRESH:
2707             dissect_bgp_route_refresh(tvb, bgp1_tree);
2708             break;
2709         case BGP_CAPABILITY:
2710             dissect_bgp_capability(tvb, bgp1_tree);
2711             break;
2712         default:
2713             break;
2714         }
2715     }
2716 }
2717
2718 /*
2719  * Dissect a BGP packet.
2720  */
2721 static void
2722 dissect_bgp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2723 {
2724     volatile int  offset = 0;   /* offset into the tvbuff           */
2725     gint          reported_length_remaining;
2726     guint8        bgp_marker[BGP_MARKER_SIZE];    /* Marker (should be all ones */
2727     static guchar marker[] = {   /* BGP message marker               */
2728         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2729         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2730     };
2731     proto_item    *ti;           /* tree item                        */
2732     proto_tree    *bgp_tree;     /* BGP packet tree                  */
2733     guint16       bgp_len;       /* Message length             */
2734     int           offset_before;
2735     guint         length_remaining;
2736     guint         length;
2737     volatile gboolean first = TRUE;  /* TRUE for the first BGP message in packet */
2738     tvbuff_t      *next_tvb;
2739
2740     if (check_col(pinfo->cinfo, COL_PROTOCOL))
2741         col_set_str(pinfo->cinfo, COL_PROTOCOL, "BGP");
2742     if (check_col(pinfo->cinfo, COL_INFO))
2743         col_clear(pinfo->cinfo, COL_INFO);
2744
2745     /*
2746      * Scan through the TCP payload looking for a BGP marker.
2747      */
2748     while ((reported_length_remaining = tvb_reported_length_remaining(tvb, offset))
2749                 > 0) {
2750         /*
2751          * "reported_length_remaining" is the number of bytes of TCP payload
2752          * remaining.  If it's more than the length of a BGP marker,
2753          * we check only the number of bytes in a BGP marker.
2754          */
2755         if (reported_length_remaining > BGP_MARKER_SIZE)
2756             reported_length_remaining = BGP_MARKER_SIZE;
2757
2758         /*
2759          * OK, is there a BGP marker starting at the specified offset -
2760          * or, at least, the beginning of a BGP marker running to the end
2761          * of the TCP payload?
2762          *
2763          * This will throw an exception if the frame is short; that's what
2764          * we want.
2765          */
2766         tvb_memcpy(tvb, bgp_marker, offset, reported_length_remaining);
2767         if (memcmp(bgp_marker, marker, reported_length_remaining) == 0) {
2768             /*
2769              * Yes - stop scanning and start processing BGP packets.
2770              */
2771             break;
2772         }
2773
2774         /*
2775          * No - keep scanning through the tvbuff to try to find a marker.
2776          */
2777         offset++;
2778     }
2779
2780     /*
2781      * If we skipped any bytes, mark it as a BGP continuation.
2782      */
2783     if (offset > 0) {
2784         ti = proto_tree_add_item(tree, proto_bgp, tvb, 0, -1, FALSE);
2785         bgp_tree = proto_item_add_subtree(ti, ett_bgp);
2786
2787         proto_tree_add_text(bgp_tree, tvb, 0, offset, "Continuation");
2788     }
2789
2790     /*
2791      * Now process the BGP packets in the TCP payload.
2792      *
2793      * XXX - perhaps "tcp_dissect_pdus()" should take a starting
2794      * offset, in which case we can replace the loop below with
2795      * a call to "tcp_dissect_pdus()".
2796      */
2797     while (tvb_reported_length_remaining(tvb, offset) > 0) {
2798         /*
2799          * This will throw an exception if we don't have any data left.
2800          * That's what we want.  (See "tcp_dissect_pdus()", which is
2801          * similar.)
2802          */
2803         length_remaining = tvb_ensure_length_remaining(tvb, offset);
2804
2805         /*
2806          * Can we do reassembly?
2807          */
2808         if (bgp_desegment && pinfo->can_desegment) {
2809             /*
2810              * Yes - would a BGP header starting at this offset be split
2811              * across segment boundaries?
2812              */
2813             if (length_remaining < BGP_HEADER_SIZE) {
2814                 /*
2815                  * Yes.  Tell the TCP dissector where the data for this
2816                  * message starts in the data it handed us, and how many
2817                  * more bytes we need, and return.
2818                  */
2819                 pinfo->desegment_offset = offset;
2820                 pinfo->desegment_len = BGP_HEADER_SIZE - length_remaining;
2821                 return;
2822             }
2823         }
2824
2825         /*
2826          * Get the length and type from the BGP header.
2827          */
2828         bgp_len = tvb_get_ntohs(tvb, offset + BGP_MARKER_SIZE);
2829         if (bgp_len < BGP_HEADER_SIZE) {
2830             /*
2831              * The BGP length doesn't include the BGP header; report that
2832              * as an error.
2833              */
2834             show_reported_bounds_error(tvb, pinfo, tree);
2835             return;
2836         }
2837
2838         /*
2839          * Can we do reassembly?
2840          */
2841         if (bgp_desegment && pinfo->can_desegment) {
2842             /*
2843              * Yes - is the PDU split across segment boundaries?
2844              */
2845             if (length_remaining < bgp_len) {
2846                 /*
2847                  * Yes.  Tell the TCP dissector where the data for this
2848                  * message starts in the data it handed us, and how many
2849                  * more bytes we need, and return.
2850                  */
2851                 pinfo->desegment_offset = offset;
2852                 pinfo->desegment_len = bgp_len - length_remaining;
2853                 return;
2854             }
2855         }
2856
2857         /*
2858          * Construct a tvbuff containing the amount of the payload we have
2859          * available.  Make its reported length the amount of data in the PDU.
2860          *
2861          * XXX - if reassembly isn't enabled. the subdissector will throw a
2862          * BoundsError exception, rather than a ReportedBoundsError exception.
2863          * We really want a tvbuff where the length is "length", the reported
2864          * length is "plen", and the "if the snapshot length were infinite"
2865          * length is the minimum of the reported length of the tvbuff handed
2866          * to us and "plen", with a new type of exception thrown if the offset
2867          * is within the reported length but beyond that third length, with
2868          * that exception getting the "Unreassembled Packet" error.
2869          */
2870         length = length_remaining;
2871         if (length > bgp_len)
2872             length = bgp_len;
2873         next_tvb = tvb_new_subset(tvb, offset, length, bgp_len);
2874
2875         /*
2876          * Dissect the PDU.
2877          *
2878          * Catch the ReportedBoundsError exception; if this particular message
2879          * happens to get a ReportedBoundsError exception, that doesn't mean
2880          * that we should stop dissecting PDUs within this frame or chunk of
2881          * reassembled data.
2882          *
2883          * If it gets a BoundsError, we can stop, as there's nothing more to
2884          * see, so we just re-throw it.
2885          */
2886         TRY {
2887             dissect_bgp_pdu(next_tvb, pinfo, tree, first);
2888         }
2889         CATCH(BoundsError) {
2890             RETHROW;
2891         }
2892         CATCH(ReportedBoundsError) {
2893             show_reported_bounds_error(tvb, pinfo, tree);
2894         }
2895         ENDTRY;
2896
2897         first = FALSE;
2898
2899         /*
2900          * Step to the next PDU.
2901          * Make sure we don't overflow.
2902          */
2903         offset_before = offset;
2904         offset += bgp_len;
2905         if (offset <= offset_before)
2906             break;
2907     }
2908 }
2909
2910 /*
2911  * Register ourselves.
2912  */
2913 void
2914 proto_register_bgp(void)
2915 {
2916
2917     static hf_register_info hf[] = {
2918       { &hf_bgp_type,
2919         { "Type", "bgp.type", FT_UINT8, BASE_DEC,
2920           VALS(bgptypevals), 0x0, "BGP message type", HFILL }},
2921       { &hf_bgp_aggregator_as,
2922         { "Aggregator AS", "bgp.aggregator_as", FT_UINT16, BASE_DEC,
2923           NULL, 0x0, "", HFILL}},
2924       { &hf_bgp_aggregator_origin,
2925         { "Aggregator origin", "bgp.aggregator_origin", FT_IPv4, BASE_NONE,
2926           NULL, 0x0, "", HFILL}}, 
2927       { &hf_bgp_as_path,
2928         { "AS Path", "bgp.as_path", FT_UINT16, BASE_DEC,
2929           NULL, 0x0, "", HFILL}},
2930       { &hf_bgp_cluster_identifier,
2931         { "Cluster identifier", "bgp.cluster_identifier", FT_IPv4, BASE_NONE,
2932           NULL, 0x0, "", HFILL}},
2933       { &hf_bgp_community_as,
2934         { "Community AS", "bgp.community_as", FT_UINT16, BASE_DEC,
2935           NULL, 0x0, "", HFILL}},
2936       { &hf_bgp_community_value,
2937         { "Community value", "bgp.community_value", FT_UINT16, BASE_DEC,
2938           NULL, 0x0, "", HFILL}},
2939       { &hf_bgp_local_pref,
2940         { "Local preference", "bgp.local_pref", FT_UINT32, BASE_DEC,
2941           NULL, 0x0, "", HFILL}},
2942       { &hf_bgp_mp_reach_nlri_ipv4_prefix,
2943         { "MP Reach NLRI IPv4 prefix", "bgp.mp_reach_nlri_ipv4_prefix", FT_IPv4, BASE_NONE,
2944           NULL, 0x0, "", HFILL}},
2945       { &hf_bgp_mp_unreach_nlri_ipv4_prefix,
2946         { "MP Unreach NLRI IPv4 prefix", "bgp.mp_unreach_nlri_ipv4_prefix", FT_IPv4, BASE_NONE,
2947           NULL, 0x0, "", HFILL}}, 
2948       { &hf_bgp_mp_nlri_tnl_id,
2949         { "MP Reach NLRI Tunnel Identifier", "bgp.mp_nlri_tnl_id", FT_UINT16, BASE_HEX,
2950           NULL, 0x0, "", HFILL}},
2951       { &hf_bgp_multi_exit_disc,
2952         { "Multiple exit discriminator", "bgp.multi_exit_disc", FT_UINT32, BASE_DEC,
2953           NULL, 0x0, "", HFILL}},
2954       { &hf_bgp_next_hop,
2955         { "Next hop", "bgp.next_hop", FT_IPv4, BASE_NONE,
2956           NULL, 0x0, "", HFILL}}, 
2957       { &hf_bgp_nlri_prefix,
2958         { "NLRI prefix", "bgp.nlri_prefix", FT_IPv4, BASE_NONE,
2959           NULL, 0x0, "", HFILL}}, 
2960       { &hf_bgp_origin,
2961         { "Origin", "bgp.origin", FT_UINT8, BASE_DEC,
2962           VALS(bgpattr_origin), 0x0, "", HFILL}},
2963       { &hf_bgp_originator_id,
2964         { "Originator identifier", "bgp.originator_id", FT_IPv4, BASE_NONE,
2965           NULL, 0x0, "", HFILL}},
2966       { &hf_bgp_ssa_t,
2967         { "Transitive bit", "bgp.ssa_t", FT_BOOLEAN, 8,
2968           NULL, 0x80, "SSA Transitive bit", HFILL}},
2969       { &hf_bgp_ssa_type,
2970         { "SSA Type", "bgp.ssa_type", FT_UINT16, BASE_DEC,
2971           VALS(bgp_ssa_type), 0x7FFF, "SSA Type", HFILL}},
2972       { &hf_bgp_ssa_len,
2973         { "Length", "bgp.ssa_len", FT_UINT16, BASE_DEC,
2974           NULL, 0x0, "SSA Length", HFILL}},
2975       { &hf_bgp_ssa_value,
2976         { "Value", "bgp.ssa_value", FT_BYTES, BASE_HEX,
2977           NULL, 0x0, "SSA Value", HFILL}},
2978       { &hf_bgp_ssa_l2tpv3_pref,
2979         { "Preference", "bgp.ssa_l2tpv3_pref", FT_UINT16, BASE_DEC,
2980           NULL, 0x0, "Preference", HFILL}},
2981       { &hf_bgp_ssa_l2tpv3_s,
2982         { "Sequencing bit", "bgp.ssa_l2tpv3_s", FT_BOOLEAN, 8,
2983           NULL, 0x80, "Sequencing S-bit", HFILL}},
2984       { &hf_bgp_ssa_l2tpv3_unused,
2985         { "Unused", "bgp.ssa_l2tpv3_Unused", FT_BOOLEAN, 8,
2986           NULL, 0x7F, "Unused Flags", HFILL}},
2987       { &hf_bgp_ssa_l2tpv3_cookie_len,
2988         { "Cookie Length", "bgp.ssa_l2tpv3_cookie_len", FT_UINT8, BASE_DEC,
2989           NULL, 0x0, "Cookie Length", HFILL}},
2990       { &hf_bgp_ssa_l2tpv3_session_id,
2991         { "Session ID", "bgp.ssa_l2tpv3_session_id", FT_UINT32, BASE_DEC,
2992           NULL, 0x0, "Session ID", HFILL}},
2993       { &hf_bgp_ssa_l2tpv3_cookie,
2994         { "Cookie", "bgp.ssa_l2tpv3_cookie", FT_BYTES, BASE_HEX,
2995           NULL, 0x0, "Cookie", HFILL}},
2996       { &hf_bgp_withdrawn_prefix,
2997         { "Withdrawn prefix", "bgp.withdrawn_prefix", FT_IPv4, BASE_NONE,
2998           NULL, 0x0, "", HFILL}}, 
2999       { &hf_bgp_cluster_list,
3000         { "Cluster List", "bgp.cluster_list", FT_BYTES, BASE_HEX,
3001           NULL, 0x0, "", HFILL}}, 
3002     };
3003
3004     static gint *ett[] = {
3005       &ett_bgp,
3006       &ett_bgp_prefix,
3007       &ett_bgp_unfeas,
3008       &ett_bgp_attrs,
3009       &ett_bgp_attr,
3010       &ett_bgp_attr_flags,
3011       &ett_bgp_mp_nhna,
3012       &ett_bgp_mp_reach_nlri,
3013       &ett_bgp_mp_unreach_nlri,
3014       &ett_bgp_mp_snpa,
3015       &ett_bgp_nlri,
3016       &ett_bgp_open,
3017       &ett_bgp_update,
3018       &ett_bgp_notification,
3019       &ett_bgp_route_refresh,
3020       &ett_bgp_capability,
3021       &ett_bgp_as_paths,
3022       &ett_bgp_as_path_segments,
3023       &ett_bgp_communities,
3024       &ett_bgp_cluster_list,
3025       &ett_bgp_options,
3026       &ett_bgp_option,
3027       &ett_bgp_extended_communities,
3028       &ett_bgp_ssa,
3029       &ett_bgp_ssa_subtree,
3030       &ett_bgp_orf,
3031       &ett_bgp_orf_entry
3032     };
3033     module_t *bgp_module;
3034     static enum_val_t asn_len[] = {
3035         {"auto-detect", "Auto-detect", 0},
3036         {"2", "2 octet", 2},
3037         {"4", "4 octet", 4},
3038         {NULL, NULL, -1}
3039     };
3040
3041     proto_bgp = proto_register_protocol("Border Gateway Protocol",
3042                                         "BGP", "bgp");
3043     proto_register_field_array(proto_bgp, hf, array_length(hf));
3044     proto_register_subtree_array(ett, array_length(ett));
3045
3046     bgp_module = prefs_register_protocol(proto_bgp, NULL);
3047     prefs_register_bool_preference(bgp_module, "desegment",
3048       "Reassemble BGP messages spanning multiple TCP segments",
3049       "Whether the BGP dissector should reassemble messages spanning multiple TCP segments."
3050       " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
3051       &bgp_desegment);
3052     prefs_register_enum_preference(bgp_module, "asn_len",
3053       "Length of the AS number",
3054       "BGP dissector detect the length of the AS number in AS_PATH attributes automatically or manually (NOTE: Automatic detection is not 100% accurate)",
3055       &bgp_asn_len, asn_len, FALSE);
3056 }
3057
3058 void
3059 proto_reg_handoff_bgp(void)
3060 {
3061     dissector_handle_t bgp_handle;
3062
3063     bgp_handle = create_dissector_handle(dissect_bgp, proto_bgp);
3064     dissector_add("tcp.port", BGP_TCP_PORT, bgp_handle);
3065 }