2 * Routines for BGP packet dissection.
3 * Copyright 1999, Jun-ichiro itojun Hagino <itojun@itojun.org>
8 * RFC1771 A Border Gateway Protocol 4 (BGP-4)
9 * RFC1966 BGP Route Reflection An alternative to full mesh IBGP
10 * RFC1997 BGP Communities Attribute
11 * RFC2283 Multiprotocol Extensions for BGP-4
14 * RFC1863 A BGP/IDRP Route Server alternative to a full mesh routing
15 * RFC1965 Autonomous System Confederations for BGP
16 * Destination Preference Attribute for BGP (work in progress)
18 * Ethereal - Network traffic analyzer
19 * By Gerald Combs <gerald@zing.org>
20 * Copyright 1998 Gerald Combs
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * as published by the Free Software Foundation; either version 2
25 * of the License, or (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
44 #ifdef HAVE_SYS_TYPES_H
45 # include <sys/types.h>
48 #ifdef HAVE_NETINET_IN_H
49 # include <netinet/in.h>
52 #ifdef NEED_SNPRINTF_H
58 # include "snprintf.h"
64 #include "packet-bgp.h"
65 #include "packet-ipv6.h"
67 #ifdef HAVE_ARPA_INET_H
68 #include <arpa/inet.h>
71 static const value_string bgptypevals[] = {
72 { BGP_OPEN, "OPEN Message" },
73 { BGP_UPDATE, "UPDATE Message" },
74 { BGP_NOTIFICATION, "NOTIFICATION Message" },
75 { BGP_KEEPALIVE, "KEEPALIVE Message" },
79 static const value_string bgpnotify_major[] = {
80 { 1, "Message Header Error" },
81 { 2, "OPEN Message Error" },
82 { 3, "UPDATE Message Error" },
83 { 4, "Hold Timer Expired" },
84 { 5, "Finite State Machine Error" },
89 static const value_string bgpnotify_minor_1[] = {
90 { 1, "Connection Not Synchronized" },
91 { 2, "Bad Message Length" },
92 { 3, "Bad Message Type" },
96 static const value_string bgpnotify_minor_2[] = {
97 { 1, "Unsupported Version Number" },
99 { 3, "Bad BGP Identifier" },
100 { 4, "Unsupported Optional Parameter" },
101 { 5, "Authentication Failure" },
102 { 6, "Unacceptable Hold Time" },
106 static const value_string bgpnotify_minor_3[] = {
107 { 1, "Malformed Attribute List" },
108 { 2, "Unrecognized Well-known Attribute" },
109 { 3, "Missing Well-known Attribute" },
110 { 4, "Attribute Flags Error" },
111 { 5, "Attribute Length Error" },
112 { 6, "Invalid ORIGIN Attribute" },
113 { 7, "AS Routing Loop" },
114 { 8, "Invalid NEXT_HOP Attribute" },
115 { 9, "Optional Attribute Error" },
116 { 10, "Invalid Network Field" },
117 { 11, "Malformed AS_PATH" },
121 static const value_string *bgpnotify_minor[] = {
122 NULL, bgpnotify_minor_1, bgpnotify_minor_2, bgpnotify_minor_3,
125 static const value_string bgpattr_origin[] = {
132 static const value_string as_segment_type[] = {
134 { 2, "AS_SEQUENCE" },
135 { 3, "AS_CONFED_SET" },
136 { 4, "AS_CONFED_SEQUENCE" },
140 static const value_string bgpattr_type[] = {
141 { BGPTYPE_ORIGIN, "ORIGIN" },
142 { BGPTYPE_AS_PATH, "AS_PATH" },
143 { BGPTYPE_NEXT_HOP, "NEXT_HOP" },
144 { BGPTYPE_MULTI_EXIT_DISC, "MULTI_EXIT_DISC" },
145 { BGPTYPE_LOCAL_PREF, "LOCAL_PREF" },
146 { BGPTYPE_ATOMIC_AGGREGATE, "ATOMIC_AGGREGATE" },
147 { BGPTYPE_AGGREGATOR, "AGGREGATOR" },
148 { BGPTYPE_COMMUNITIES, "COMMUNITIES" },
149 { BGPTYPE_ORIGINATOR_ID, "ORIGINATOR_ID" },
150 { BGPTYPE_CLUSTER_LIST, "CLUSTER_LIST" },
151 { BGPTYPE_MP_REACH_NLRI, "MP_REACH_NLRI" },
152 { BGPTYPE_MP_UNREACH_NLRI, "MP_UNREACH_NLRI" },
156 /* Subsequent address family identifier, RFC2283 section 7 */
157 static const value_string bgpattr_nlri_safi[] = {
161 { 3, "Unicast+Multicast" },
165 static const value_string afnumber[] = {
167 { AFNUM_INET, "IPv4" },
168 { AFNUM_INET6, "IPv6" },
169 { AFNUM_NSAP, "NSAP" },
170 { AFNUM_HDLC, "HDLC" },
171 { AFNUM_BBN1822, "BBN 1822" },
172 { AFNUM_802, "802" },
173 { AFNUM_E163, "E.163" },
174 { AFNUM_E164, "E.164" },
175 { AFNUM_F69, "F.69" },
176 { AFNUM_X121, "X.121" },
177 { AFNUM_IPX, "IPX" },
178 { AFNUM_ATALK, "Appletalk" },
179 { AFNUM_DECNET, "Decnet IV" },
180 { AFNUM_BANYAN, "Banyan Vines" },
181 { AFNUM_E164NSAP, "E.164 with NSAP subaddress" },
182 { 65535, "Reserved" },
186 static int proto_bgp = -1;
188 static gint ett_bgp = -1;
189 static gint ett_bgp_unfeas = -1;
190 static gint ett_bgp_attrs = -1;
191 static gint ett_bgp_attr = -1;
192 static gint ett_bgp_attr_flags = -1;
193 static gint ett_bgp_mp_reach_nlri = -1;
194 static gint ett_bgp_mp_unreach_nlri = -1;
195 static gint ett_bgp_nlri = -1;
196 static gint ett_bgp_open = -1;
197 static gint ett_bgp_update = -1;
198 static gint ett_bgp_notification = -1;
199 static gint ett_bgp_as_paths = -1;
200 static gint ett_bgp_communities = -1;
201 static gint ett_bgp_cluster_list = -1;
204 * Decode an IPv4 prefix.
207 decode_prefix4(const u_char *pd, char *buf, int buflen)
209 guint8 addr[4]; /* IP address */
210 int plen; /* prefix length */
211 int length; /* number of octets needed for prefix */
215 if (plen < 0 || 32 < plen)
217 length = (plen + 7) / 8;
220 memset(addr, 0, sizeof(addr));
221 memcpy(addr, &pd[1], length);
223 addr[length - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
225 /* hand back a formatted string */
226 snprintf(buf, buflen, "%s/%d", ip_to_str(addr), plen);
231 * Decode an IPv6 prefix.
234 decode_prefix6(const u_char *pd, char *buf, int buflen)
236 struct e_in6_addr addr; /* IPv6 address */
237 int plen; /* prefix length */
238 int length; /* number of octets needed for prefix */
242 if (plen < 0 || 128 < plen)
244 length = (plen + 7) / 8;
247 memset(&addr, 0, sizeof(addr));
248 memcpy(&addr, &pd[1], length);
250 addr.s6_addr[length - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
252 /* hand back a formatted string */
253 snprintf(buf, buflen, "%s/%d", ip6_to_str(&addr), plen);
258 * Dissect a BGP OPEN message.
261 dissect_bgp_open(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
263 struct bgp_open bgpo; /* BGP OPEN message */
264 int hlen; /* message length */
266 /* snarf OPEN message */
267 memcpy(&bgpo, &pd[offset], sizeof(bgpo));
268 hlen = ntohs(bgpo.bgpo_len);
270 proto_tree_add_text(tree,
271 offset + offsetof(struct bgp_open, bgpo_version), 1,
272 "Version: %u", bgpo.bgpo_version);
273 proto_tree_add_text(tree,
274 offset + offsetof(struct bgp_open, bgpo_myas), 2,
275 "My AS: %u", ntohs(bgpo.bgpo_myas));
276 proto_tree_add_text(tree,
277 offset + offsetof(struct bgp_open, bgpo_holdtime), 2,
278 "Hold time: %u", ntohs(bgpo.bgpo_holdtime));
279 proto_tree_add_text(tree,
280 offset + offsetof(struct bgp_open, bgpo_id), 4,
281 "BGP identifier: %s", ip_to_str((guint8 *)&bgpo.bgpo_id));
282 proto_tree_add_text(tree,
283 offset + offsetof(struct bgp_open, bgpo_optlen), 1,
284 "Optional parameters length: %u %s", bgpo.bgpo_optlen,
285 (bgpo.bgpo_optlen == 1) ? "byte" : "bytes");
287 if (hlen > sizeof(struct bgp_open)) {
289 openoff = ((char *)&bgpo.bgpo_optlen - (char *)&bgpo) + 1;
290 proto_tree_add_text(tree,
291 offset + openoff, hlen - openoff,
292 "Optional parameters");
297 * Dissect a BGP UPDATE message.
300 dissect_bgp_update(const u_char *pd, int offset, frame_data *fd,
303 struct bgp bgp; /* BGP header */
304 struct bgp_attr bgpa; /* path attributes */
305 int hlen; /* message length */
306 const u_char *p; /* packet offset pointer */
307 const u_char *q; /* tmp */
308 const u_char *end; /* message end */
310 proto_item *ti; /* tree item */
311 proto_tree *subtree; /* subtree for attibutes */
312 proto_tree *subtree2; /* subtree for attibutes */
313 proto_tree *subtree3; /* subtree for attibutes */
314 proto_tree *as_paths_tree; /* subtree for AS_PATHs */
315 proto_tree *as_path_tree; /* subtree for AS_PATH */
316 proto_tree *communities_tree; /* subtree for COMMUNITIES */
317 proto_tree *community_tree; /* subtree for a community */
318 proto_tree *cluster_list_tree; /* subtree for CLUSTER_LIST */
320 guint8 length; /* AS_PATH length */
321 guint8 type; /* AS_PATH type */
322 char *as_path_str = NULL; /* AS_PATH string */
323 char *communities_str = NULL; /* COMMUNITIES string */
324 char *cluster_list_str = NULL; /* CLUSTER_LIST string */
325 char junk_buf[256]; /* tmp */
328 /* snarf UPDATE message */
329 memcpy(&bgp, &pd[offset], sizeof(bgp));
330 hlen = ntohs(bgp.bgp_len);
331 p = &pd[offset + BGP_HEADER_SIZE]; /*XXX*/
333 /* check for withdrawals */
335 proto_tree_add_text(tree, p - pd, 2,
336 "Unfeasible routes length: %u %s", len, (len == 1) ? "byte" : "bytes");
339 /* parse unfeasible prefixes */
341 ti = proto_tree_add_text(tree, p - pd, len, "Withdrawn routes:");
342 subtree = proto_item_add_subtree(ti, ett_bgp_unfeas);
344 /* parse each prefixes */
347 i = decode_prefix4(p, junk_buf, sizeof(junk_buf));
348 proto_tree_add_text(subtree, p - pd, i, "%s", junk_buf);
356 /* check for advertisements */
358 proto_tree_add_text(tree, p - pd, 2, "Total path attribute length: %u %s",
359 len, (len == 1) ? "byte" : "bytes");
361 /* path attributes */
364 ti = proto_tree_add_text(tree, p - pd + 2, len, "Path attributes");
365 subtree = proto_item_add_subtree(ti, ett_bgp_attrs);
373 memcpy(&bgpa, &p[i], sizeof(bgpa));
374 /* check for the Extended Length bit */
375 if (bgpa.bgpa_flags & BGP_ATTR_FLAG_EXTENDED_LENGTH) {
376 alen = pntohs(&p[i + sizeof(bgpa)]);
377 aoff = sizeof(bgpa) + 2;
379 alen = p[i + sizeof(bgpa)];
380 aoff = sizeof(bgpa) + 1;
383 /* This is kind of ugly - similar code appears twice, but it
384 helps browsing attrs. */
385 /* the first switch prints things in the title of the subtree */
386 switch (bgpa.bgpa_type) {
389 goto default_attribute_top;
390 msg = val_to_str(p[i + aoff], bgpattr_origin, "Unknown");
391 ti = proto_tree_add_text(subtree, p - pd + i, alen + aoff,
393 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
394 msg, alen + aoff, (alen + aoff == 1) ? "byte" :
397 case BGPTYPE_AS_PATH:
399 (p + current attribute + 3 bytes to first tuple) */
400 end = p + alen + i + 3;
402 /* must be freed by second switch! */
403 /* "alen * 6" (5 digits + space) should be a good estimate
404 of how long the AS path string could be */
405 as_path_str = malloc((alen + 1) * 6);
406 if (as_path_str == NULL) break;
407 as_path_str[0] = '\0';
409 /* snarf each AS path */
412 if (type == AS_SET) {
413 snprintf(as_path_str, 2, "{");
417 /* ignore confederation types until we support them */
418 if (type == AS_CONFED_SET || type == AS_CONFED_SEQUENCE) {
423 /* snarf each value in path */
424 for (j = 0; j < length; j++) {
425 snprintf(junk_buf, sizeof(junk_buf), "%u%s", pntohs(q),
426 (type == AS_SET) ? ", " : " ");
427 strncat(as_path_str, junk_buf, sizeof(junk_buf));
431 /* cleanup end of string */
432 if (type == AS_SET) {
433 as_path_str[strlen(as_path_str) - 2] = '}';
436 as_path_str[strlen(as_path_str) - 1] = '\0';
440 /* check for empty AS_PATH */
442 strncpy(as_path_str, "empty", 6);
444 ti = proto_tree_add_text(subtree, p - pd + i, alen + aoff,
446 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
447 as_path_str, alen + aoff,
448 (alen + aoff == 1) ? "byte" : "bytes");
450 case BGPTYPE_NEXT_HOP:
452 goto default_attribute_top;
453 ti = proto_tree_add_text(subtree, p - pd + i, alen + aoff,
455 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
456 ip_to_str(&p[i + aoff]), alen + aoff, (alen + aoff == 1)
459 case BGPTYPE_MULTI_EXIT_DISC:
461 goto default_attribute_top;
462 ti = proto_tree_add_text(subtree, p - pd + i, alen + aoff,
464 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
465 pntohl(&p[i + aoff]), alen + aoff,
466 (alen + aoff == 1) ? "byte" : "bytes");
468 case BGPTYPE_LOCAL_PREF:
470 goto default_attribute_top;
471 ti = proto_tree_add_text(subtree, p - pd + i, alen + aoff,
473 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
474 pntohl(&p[i + aoff]), alen + aoff,
475 (alen + aoff == 1) ? "byte" : "bytes");
477 case BGPTYPE_ATOMIC_AGGREGATE:
479 goto default_attribute_top;
480 ti = proto_tree_add_text(subtree, p - pd + i, alen + aoff,
482 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
483 alen + aoff, (alen + aoff == 1) ? "byte" : "bytes");
485 case BGPTYPE_AGGREGATOR:
487 goto default_attribute_top;
488 ti = proto_tree_add_text(subtree, p - pd + i, alen + aoff,
489 "%s: AS: %u origin: %s (%u %s)",
490 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
491 pntohs(&p[i + aoff]),
492 ip_to_str(&p[i + aoff + 2]), alen + aoff,
493 (alen + aoff == 1) ? "byte" : "bytes");
495 case BGPTYPE_COMMUNITIES:
497 goto default_attribute_top;
500 (p + current attribute + 3 bytes to first tuple) */
501 end = p + alen + i + 3;
503 /* must be freed by second switch! */
504 /* "alen * 12" (5 digits, a :, 5 digits + space ) should be
505 a good estimate of how long the communities string could
507 communities_str = malloc((alen + 1) * 12);
508 if (communities_str == NULL) break;
509 communities_str[0] = '\0';
510 memset(junk_buf, 0, sizeof(junk_buf));
512 /* snarf each community */
514 /* check for well-known communities */
515 if (pntohl(q) == BGP_COMM_NO_EXPORT)
516 strncpy(junk_buf, "NO_EXPORT ", 10);
517 else if (pntohl(q) == BGP_COMM_NO_ADVERTISE)
518 strncpy(junk_buf, "NO_ADVERTISE ", 13);
519 else if (pntohl(q) == BGP_COMM_NO_EXPORT_SUBCONFED)
520 strncpy(junk_buf, "NO_EXPORT_SUBCONFED ", 20);
522 snprintf(junk_buf, sizeof(junk_buf), "%u:%u ",
528 strncat(communities_str, junk_buf, sizeof(junk_buf));
530 /* cleanup end of string */
531 communities_str[strlen(communities_str) - 1] = '\0';
533 ti = proto_tree_add_text(subtree, p - pd + i, alen + aoff,
535 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
536 communities_str, alen + aoff,
537 (alen + aoff == 1) ? "byte" : "bytes");
539 case BGPTYPE_ORIGINATOR_ID:
541 goto default_attribute_top;
542 ti = proto_tree_add_text(subtree, p - pd + i, alen + aoff,
544 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
545 ip_to_str(&p[i + aoff]), alen + aoff, (alen + aoff == 1)
548 case BGPTYPE_CLUSTER_LIST:
550 goto default_attribute_top;
553 (p + current attribute + 3 bytes to first tuple) */
554 end = p + alen + i + 3;
556 /* must be freed by second switch! */
557 /* "alen * 16" (12 digits, 3 dots + space ) should be
558 a good estimate of how long the cluster_list string could
560 cluster_list_str = malloc((alen + 1) * 16);
561 if (cluster_list_str == NULL) break;
562 cluster_list_str[0] = '\0';
563 memset(junk_buf, 0, sizeof(junk_buf));
565 /* snarf each cluster list */
567 snprintf(junk_buf, sizeof(junk_buf), "%s ", ip_to_str(q));
568 strncat(cluster_list_str, junk_buf, sizeof(junk_buf));
571 /* cleanup end of string */
572 cluster_list_str[strlen(cluster_list_str) - 1] = '\0';
574 ti = proto_tree_add_text(subtree, p - pd + i, alen + aoff,
576 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
577 cluster_list_str, alen + aoff,
578 (alen + aoff == 1) ? "byte" : "bytes");
581 default_attribute_top:
582 ti = proto_tree_add_text(subtree, p - pd + i, alen + aoff,
584 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
585 alen + aoff, (alen + aoff == 1) ? "byte" : "bytes");
586 } /* end of first switch */
587 subtree2 = proto_item_add_subtree(ti, ett_bgp_attr);
589 /* figure out flags */
591 if (bgpa.bgpa_flags & BGP_ATTR_FLAG_OPTIONAL) {
592 strncat(junk_buf, "Optional, ", 10);
595 strncat(junk_buf, "Well-known, ", 12);
597 if (bgpa.bgpa_flags & BGP_ATTR_FLAG_TRANSITIVE) {
598 strncat(junk_buf, "Transitive, ", 12);
601 strncat(junk_buf, "Non-transitive, ", 16);
603 if (bgpa.bgpa_flags & BGP_ATTR_FLAG_PARTIAL) {
604 strncat(junk_buf, "Partial, ", 9);
607 strncat(junk_buf, "Complete, ", 10);
609 if (bgpa.bgpa_flags & BGP_ATTR_FLAG_EXTENDED_LENGTH) {
610 strncat(junk_buf, "Extended Length, ", 17);
612 /* stomp last ", " */
613 j = strlen(junk_buf);
614 junk_buf[j - 2] = '\0';
615 ti = proto_tree_add_text(subtree2,
616 p - pd + i + offsetof(struct bgp_attr, bgpa_flags), 1,
617 "Flags: 0x%02x (%s)", bgpa.bgpa_flags, junk_buf);
618 subtree3 = proto_item_add_subtree(ti, ett_bgp_attr_flags);
620 /* add flag bitfield subtrees */
621 proto_tree_add_text(subtree3,
622 p - pd + i + offsetof(struct bgp_attr, bgpa_flags), 1,
623 "%s", decode_boolean_bitfield(bgpa.bgpa_flags,
624 BGP_ATTR_FLAG_OPTIONAL, 8, "Optional", "Well-known"));
625 proto_tree_add_text(subtree3,
626 p - pd + i + offsetof(struct bgp_attr, bgpa_flags), 1,
627 "%s", decode_boolean_bitfield(bgpa.bgpa_flags,
628 BGP_ATTR_FLAG_TRANSITIVE, 8, "Transitive",
630 proto_tree_add_text(subtree3,
631 p - pd + i + offsetof(struct bgp_attr, bgpa_flags), 1,
632 "%s", decode_boolean_bitfield(bgpa.bgpa_flags,
633 BGP_ATTR_FLAG_PARTIAL, 8, "Partial", "Complete"));
634 proto_tree_add_text(subtree3,
635 p - pd + i + offsetof(struct bgp_attr, bgpa_flags), 1,
636 "%s", decode_boolean_bitfield(bgpa.bgpa_flags,
637 BGP_ATTR_FLAG_EXTENDED_LENGTH, 8, "Extended length",
640 proto_tree_add_text(subtree2,
641 p - pd + i + offsetof(struct bgp_attr, bgpa_type), 1,
642 "Type code: %s (%u)",
643 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
646 proto_tree_add_text(subtree2, p - pd + i + sizeof(bgpa),
647 aoff - sizeof(bgpa), "Length: %d %s", alen,
648 (alen == 1) ? "byte" : "bytes");
650 /* the second switch prints things in the actual subtree of each
652 switch (bgpa.bgpa_type) {
655 proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
656 "Origin (invalid): %u %s", alen,
657 (alen == 1) ? "byte" : "bytes");
659 msg = val_to_str(p[i + aoff], bgpattr_origin, "Unknown");
660 proto_tree_add_text(subtree2, p - pd + i + aoff, 1,
661 "Origin: %s (%u)", msg, p[i + aoff]);
664 case BGPTYPE_AS_PATH:
665 /* check for empty AS_PATH */
671 ti = proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
672 "AS path: %s", as_path_str);
673 as_paths_tree = proto_item_add_subtree(ti, ett_bgp_as_paths);
676 (p + current attribute + 3 bytes to first tuple) */
677 end = p + alen + i + 3;
680 /* snarf each AS path tuple */
682 as_path_str[0] = '\0';
684 if (type == AS_SET) {
685 snprintf(as_path_str, 2, "{");
689 /* ignore confederation types until we support them */
690 if (type == AS_CONFED_SET || type == AS_CONFED_SEQUENCE) {
695 /* snarf each value in path, we're just going to reuse
696 as_path_str since we already have it malloced */
697 for (j = 0; j < length; j++) {
698 snprintf(junk_buf, sizeof(junk_buf), "%u%s", pntohs(q),
699 (type == AS_SET) ? ", " : " ");
700 strncat(as_path_str, junk_buf, sizeof(junk_buf));
704 /* cleanup end of string */
705 if (type == AS_SET) {
706 as_path_str[strlen(as_path_str) - 2] = '}';
709 as_path_str[strlen(as_path_str) - 1] = '\0';
712 /* length here means number of ASs, ie length * 2 bytes */
713 ti = proto_tree_add_text(as_paths_tree,
714 q - pd - length * 2 - 2,
715 length * 2 + 2, "AS path segment: %s", as_path_str);
716 as_path_tree = proto_item_add_subtree(ti, ett_bgp_as_paths);
717 proto_tree_add_text(as_path_tree, q - pd - length * 2 - 2,
718 1, "Path segment type: %s (%u)",
719 val_to_str(type, as_segment_type, "Unknown"), type);
720 proto_tree_add_text(as_path_tree, q - pd - length * 2 - 1,
721 1, "Path segment length: %u %s", length,
722 (length == 1) ? "AS" : "ASs");
723 proto_tree_add_text(as_path_tree, q - pd - length * 2,
724 length * 2, "Path segment value: %s", as_path_str);
729 case BGPTYPE_NEXT_HOP:
731 proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
732 "Next hop (invalid): %u %s", alen,
733 (alen == 1) ? "byte" : "bytes");
735 proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
736 "Next hop: %s", ip_to_str(&p[i + aoff]));
739 case BGPTYPE_MULTI_EXIT_DISC:
741 proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
742 "Multiple exit discriminator (invalid): %u %s",
743 alen, (alen == 1) ? "byte" : "bytes");
745 proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
746 "Multiple exit discriminator: %u",
747 pntohl(&p[i + aoff]));
750 case BGPTYPE_LOCAL_PREF:
752 proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
753 "Local preference (invalid): %u %s", alen,
754 (alen == 1) ? "byte" : "bytes");
756 proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
757 "Local preference: %u", pntohl(&p[i + aoff]));
760 case BGPTYPE_ATOMIC_AGGREGATE:
762 proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
763 "Atomic aggregate (invalid): %u %s", alen,
764 (alen == 1) ? "byte" : "bytes");
767 case BGPTYPE_AGGREGATOR:
769 proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
770 "Aggregator (invalid): %u %s", alen,
771 (alen == 1) ? "byte" : "bytes");
773 proto_tree_add_text(subtree2, p - pd + i + aoff, 2,
774 "Aggregator AS: %u", pntohs(&p[i + aoff]));
775 proto_tree_add_text(subtree2, p - pd + i + aoff + 2, 4,
776 "Aggregator origin: %s",
777 ip_to_str(&p[i + aoff + 2]));
780 case BGPTYPE_COMMUNITIES:
782 proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
783 "Communities (invalid): %u %s", alen,
784 (alen == 1) ? "byte" : "bytes");
785 free(communities_str);
789 ti = proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
790 "Communities: %s", communities_str);
791 communities_tree = proto_item_add_subtree(ti,
792 ett_bgp_communities);
795 (p + current attribute + 3 bytes to first tuple) */
796 end = p + alen + i + 3;
799 /* snarf each community */
801 /* check for reserved values */
802 if (pntohs(q) == FOURHEX0 || pntohs(q) == FOURHEXF) {
803 /* check for well-known communities */
804 if (pntohl(q) == BGP_COMM_NO_EXPORT)
805 proto_tree_add_text(communities_tree,
806 q - pd - 3 + aoff, 4,
807 "Community: NO_EXPORT (0x%x)", pntohl(q));
808 else if (pntohl(q) == BGP_COMM_NO_ADVERTISE)
809 proto_tree_add_text(communities_tree,
810 q - pd - 3 + aoff, 4,
811 "Community: NO_ADVERTISE (0x%x)", pntohl(q));
812 else if (pntohl(q) == BGP_COMM_NO_EXPORT_SUBCONFED)
813 proto_tree_add_text(communities_tree,
814 q - pd - 3 + aoff, 4,
815 "Community: NO_EXPORT_SUBCONFED (0x%x)",
818 proto_tree_add_text(communities_tree,
819 q - pd - 3 + aoff, 4,
820 "Community (reserved): 0x%x", pntohl(q));
824 ti = proto_tree_add_text(communities_tree,
825 q - pd - 3 + aoff, 4, "Community: %u:%u",
826 pntohs(q), pntohs(q + 2));
827 community_tree = proto_item_add_subtree(ti,
828 ett_bgp_communities);
829 proto_tree_add_text(community_tree, q - pd - 3 + aoff,
830 2, "Community AS: %u", pntohs(q));
831 proto_tree_add_text(community_tree, q - pd - 1 + aoff,
832 2, "Community value: %u", pntohs(q + 2));
838 free(communities_str);
840 case BGPTYPE_ORIGINATOR_ID:
842 proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
843 "Originator identifier (invalid): %u %s", alen,
844 (alen == 1) ? "byte" : "bytes");
846 proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
847 "Originator identifier: %s",
848 ip_to_str(&p[i + aoff]));
851 case BGPTYPE_MP_REACH_NLRI:
852 af = pntohs(&p[i + aoff]);
853 proto_tree_add_text(subtree2, p - pd + i + aoff, 2,
854 "Address family: %s (%u)",
855 val_to_str(af, afnumber, "Unknown"), af);
856 proto_tree_add_text(subtree2, p - pd + i + aoff + 2, 1,
857 "Subsequent address family identifier: %s (%u)",
858 val_to_str(p[i + aoff + 2], bgpattr_nlri_safi,
859 p[i + aoff + 2] >= 128 ? "Vendor specific" : "Unknown"),
861 ti = proto_tree_add_text(subtree2, p - pd + i + aoff + 3, 1,
862 "Next hop network address (%d %s)",
863 p[i + aoff + 3], (p[i + aoff + 3] == 1) ? "byte" :
865 if (af == AFNUM_INET || af == AFNUM_INET6) {
869 subtree3 = proto_item_add_subtree(ti, ett_bgp_mp_reach_nlri);
872 while (j < p[i + aoff + 3]) {
873 if (af == AFNUM_INET)
875 else if (af == AFNUM_INET6)
879 if (j + advance > p[i + aoff + 3])
882 if (af == AFNUM_INET)
883 s = ip_to_str(&p[i + aoff + 4 + j]);
885 s = ip6_to_str((struct e_in6_addr *)
886 &p[i + aoff + 4 + j]);
888 proto_tree_add_text(subtree3,
889 p - pd + i + aoff + 4 + j, advance,
895 alen -= (p[i + aoff + 3] + 4);
896 aoff += (p[i + aoff + 3] + 4);
899 ti = proto_tree_add_text(subtree2, p - pd + i + aoff, 1,
900 "Subnetwork points of attachment: %u", snpa);
903 subtree3 = proto_item_add_subtree(ti, ett_bgp_mp_reach_nlri);
904 for (/*nothing*/; snpa > 0; snpa--) {
905 proto_tree_add_text(subtree3, p - pd + i + aoff + off, 1,
906 "SNPA length: %u", p[i + aoff + off]);
908 proto_tree_add_text(subtree3, p - pd + i + aoff + off,
909 p[i + aoff + off - 1],
910 "SNPA (%u %s)", p[i + aoff + off - 1],
911 (p[i + aoff + off - 1] == 1) ? "byte" : "bytes");
912 off += p[i + aoff + off - 1];
917 ti = proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
918 "Network layer reachability information (%u %s)",
919 alen, (alen == 1) ? "byte" : "bytes");
921 subtree3 = proto_item_add_subtree(ti, ett_bgp_mp_unreach_nlri);
926 if (af == AFNUM_INET) {
927 advance = decode_prefix4(&p[i + aoff], buf,
929 } else if (af == AFNUM_INET6) {
930 advance = decode_prefix6(&p[i + aoff], buf,
938 proto_tree_add_text(subtree3, p - pd + i + aoff, advance,
939 "Network layer reachability information: %s", buf);
946 case BGPTYPE_MP_UNREACH_NLRI:
947 af = pntohs(&p[i + aoff]);
948 proto_tree_add_text(subtree2, p - pd + i + aoff, 2,
949 "Address family: %s (%u)",
950 val_to_str(af, afnumber, "Unknown"), af);
951 proto_tree_add_text(subtree2, p - pd + i + aoff + 2, 1,
952 "Subsequent address family identifier: %s (%u)",
953 val_to_str(p[i + aoff + 2], bgpattr_nlri_safi,
954 p[i + aoff + 2] >= 128 ? "Vendor specific" : "Unknown"),
956 ti = proto_tree_add_text(subtree2, p - pd + i + aoff + 3,
957 alen - 3, "Withdrawn routes (%u %s)", alen - 3,
958 (alen - 3 == 1) ? "byte" : "bytes");
963 subtree3 = proto_item_add_subtree(ti, ett_bgp_mp_unreach_nlri);
968 if (af == AFNUM_INET) {
969 advance = decode_prefix4(&p[i + aoff], buf,
971 } else if (af == AFNUM_INET6) {
972 advance = decode_prefix6(&p[i + aoff], buf,
980 proto_tree_add_text(subtree3, p - pd + i + aoff, advance,
981 "Withdrawn route: %s", buf);
988 case BGPTYPE_CLUSTER_LIST:
990 proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
991 "Cluster list (invalid): %u %s", alen,
992 (alen == 1) ? "byte" : "bytes");
993 free(cluster_list_str);
997 ti = proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
998 "Cluster list: %s", cluster_list_str);
999 cluster_list_tree = proto_item_add_subtree(ti,
1000 ett_bgp_cluster_list);
1003 (p + current attribute + 3 bytes to first tuple) */
1004 end = p + alen + i + 3;
1007 /* snarf each cluster identifier */
1009 ti = proto_tree_add_text(cluster_list_tree,
1010 q - pd - 3 + aoff, 4, "Cluster identifier: %s",
1016 free(cluster_list_str);
1019 proto_tree_add_text(subtree2, p - pd + i + aoff, alen,
1020 "Unknown (%d %s)", alen, (alen == 1) ? "byte" :
1023 } /* end of second switch */
1031 len = hlen - (p - &pd[offset]);
1033 /* parse prefixes */
1035 ti = proto_tree_add_text(tree, p - pd, len,
1036 "Network layer reachability information: %u %s", len,
1037 (len == 1) ? "byte" : "bytes");
1038 subtree = proto_item_add_subtree(ti, ett_bgp_nlri);
1041 i = decode_prefix4(p, junk_buf, sizeof(junk_buf));
1042 proto_tree_add_text(subtree, p - pd, i, "%s", junk_buf);
1050 * Dissect a BGP NOTIFICATION message.
1053 dissect_bgp_notification(const u_char *pd, int offset, frame_data *fd,
1056 struct bgp_notification bgpn; /* BGP NOTIFICATION message */
1057 int hlen; /* message length */
1058 char *p; /* string pointer */
1061 memcpy(&bgpn, &pd[offset], sizeof(bgpn));
1062 hlen = ntohs(bgpn.bgpn_len);
1064 /* print error code */
1065 proto_tree_add_text(tree,
1066 offset + offsetof(struct bgp_notification, bgpn_major), 1,
1067 "Error code: %s (%u)",
1068 val_to_str(bgpn.bgpn_major, bgpnotify_major, "Unknown"),
1071 /* print error subcode */
1072 if (bgpn.bgpn_major < array_length(bgpnotify_minor)
1073 && bgpnotify_minor[bgpn.bgpn_major] != NULL) {
1074 p = val_to_str(bgpn.bgpn_minor, bgpnotify_minor[bgpn.bgpn_major],
1076 } else if (bgpn.bgpn_minor == 0)
1080 proto_tree_add_text(tree,
1081 offset + offsetof(struct bgp_notification, bgpn_minor), 1,
1082 "Error subcode: %s (%u)", p, bgpn.bgpn_minor);
1084 /* only print if there is optional data */
1085 if (hlen > BGP_MIN_NOTIFICATION_MSG_SIZE) {
1086 proto_tree_add_text(tree, offset + BGP_MIN_NOTIFICATION_MSG_SIZE,
1087 hlen - BGP_MIN_NOTIFICATION_MSG_SIZE, "Data");
1092 * Dissect a BGP packet.
1095 dissect_bgp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
1097 proto_item *ti; /* tree item */
1098 proto_tree *bgp_tree; /* BGP packet tree */
1099 proto_tree *bgp1_tree; /* BGP message tree */
1100 const u_char *p; /* packet offset pointer */
1102 int found; /* number of BGP messages in packet */
1103 static u_char marker[] = { /* BGP message marker */
1104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1107 struct bgp bgp; /* BGP header */
1108 int hlen; /* BGP header length */
1109 char *typ; /* BGP message type */
1111 if (check_col(fd, COL_PROTOCOL))
1112 col_add_str(fd, COL_PROTOCOL, "BGP");
1118 /* run through the TCP packet looking for BGP headers */
1119 /* this is done twice, but this way each message type can be
1120 printed in the COL_INFO field */
1122 /* look for bgp header */
1127 CHECK_SIZE(i, sizeof(marker), l);
1128 if (memcmp(&p[i], marker, sizeof(marker)) != 0) {
1133 memcpy(&bgp, &p[i], sizeof(bgp));
1135 hlen = ntohs(bgp.bgp_len);
1136 typ = val_to_str(bgp.bgp_type, bgptypevals, "Unknown Message");
1138 if (check_col(fd, COL_INFO)) {
1140 col_add_fstr(fd, COL_INFO, "%s", typ);
1142 col_append_fstr(fd, COL_INFO, ", %s", typ);
1149 ti = proto_tree_add_text(tree, offset, END_OF_FRAME,
1150 "Border Gateway Protocol");
1151 bgp_tree = proto_item_add_subtree(ti, ett_bgp);
1156 /* now, run through the TCP packet again, this time dissect */
1157 /* each message that we find */
1159 /* look for bgp header */
1164 CHECK_SIZE(i, sizeof(marker), l);
1165 if (memcmp(&p[i], marker, sizeof(marker)) != 0) {
1170 memcpy(&bgp, &p[i], sizeof(bgp));
1171 hlen = ntohs(bgp.bgp_len);
1172 typ = val_to_str(bgp.bgp_type, bgptypevals, "Unknown Message");
1173 if (END_OF_FRAME < hlen) {
1174 ti = proto_tree_add_text(bgp_tree, offset + i, END_OF_FRAME,
1175 "%s (truncated)", typ);
1177 ti = proto_tree_add_text(bgp_tree, offset + i, hlen,
1180 /* add a different tree for each message type */
1181 switch (bgp.bgp_type) {
1183 bgp1_tree = proto_item_add_subtree(ti, ett_bgp_open);
1186 bgp1_tree = proto_item_add_subtree(ti, ett_bgp_update);
1188 case BGP_NOTIFICATION:
1189 bgp1_tree = proto_item_add_subtree(ti, ett_bgp_notification);
1192 bgp1_tree = proto_item_add_subtree(ti, ett_bgp);
1195 bgp1_tree = proto_item_add_subtree(ti, ett_bgp);
1199 proto_tree_add_text(bgp1_tree, offset + i, BGP_MARKER_SIZE,
1200 "Marker: 16 bytes");
1202 if (hlen < BGP_HEADER_SIZE || hlen > BGP_MAX_PACKET_SIZE) {
1203 proto_tree_add_text(bgp1_tree,
1204 offset + i + offsetof(struct bgp, bgp_len), 2,
1205 "Length (invalid): %u %s", hlen,
1206 (hlen == 1) ? "byte" : "bytes");
1208 proto_tree_add_text(bgp1_tree,
1209 offset + i + offsetof(struct bgp, bgp_len), 2,
1210 "Length: %u %s", hlen,
1211 (hlen == 1) ? "byte" : "bytes");
1214 proto_tree_add_text(bgp1_tree,
1215 offset + i + offsetof(struct bgp, bgp_type), 1,
1216 "Type: %s (%u)", typ, bgp.bgp_type);
1218 CHECK_SIZE(i, hlen, l);
1220 /* handle each message type */
1221 switch (bgp.bgp_type) {
1223 dissect_bgp_open(pd, offset + i, fd, bgp1_tree);
1226 dissect_bgp_update(pd, offset + i, fd, bgp1_tree);
1228 case BGP_NOTIFICATION:
1229 dissect_bgp_notification(pd, offset + i, fd, bgp1_tree);
1232 /* no data in KEEPALIVE messages */
1244 * Register ourselves.
1247 proto_register_bgp(void)
1249 static gint *ett[] = {
1254 &ett_bgp_attr_flags,
1255 &ett_bgp_mp_reach_nlri,
1256 &ett_bgp_mp_unreach_nlri,
1260 &ett_bgp_notification,
1262 &ett_bgp_communities,
1263 &ett_bgp_cluster_list,
1266 proto_bgp = proto_register_protocol("Border Gateway Protocol", "bgp");
1267 proto_register_subtree_array(ett, array_length(ett));