2 * Routines for BGP packet dissection.
3 * Copyright 1999, Jun-ichiro itojun Hagino <itojun@itojun.org>
5 * $Id: packet-bgp.c,v 1.26 2000/08/13 14:08:02 deniel Exp $
8 * RFC1771 A Border Gateway Protocol 4 (BGP-4)
9 * RFC1965 Autonomous System Confederations for BGP
10 * RFC1966 BGP Route Reflection An alternative to full mesh IBGP
11 * RFC1997 BGP Communities Attribute
12 * RFC2283 Multiprotocol Extensions for BGP-4
15 * Destination Preference Attribute for BGP (work in progress)
16 * RFC1863 A BGP/IDRP Route Server alternative to a full mesh routing
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 HAVE_ARPA_INET_H
53 #include <arpa/inet.h>
59 #ifdef NEED_SNPRINTF_H
60 # include "snprintf.h"
64 #include "packet-bgp.h"
65 #include "packet-ipv6.h"
67 #define TCP_PORT_BGP 179
69 static const value_string bgptypevals[] = {
70 { BGP_OPEN, "OPEN Message" },
71 { BGP_UPDATE, "UPDATE Message" },
72 { BGP_NOTIFICATION, "NOTIFICATION Message" },
73 { BGP_KEEPALIVE, "KEEPALIVE Message" },
77 static const value_string bgpnotify_major[] = {
78 { 1, "Message Header Error" },
79 { 2, "OPEN Message Error" },
80 { 3, "UPDATE Message Error" },
81 { 4, "Hold Timer Expired" },
82 { 5, "Finite State Machine Error" },
87 static const value_string bgpnotify_minor_1[] = {
88 { 1, "Connection Not Synchronized" },
89 { 2, "Bad Message Length" },
90 { 3, "Bad Message Type" },
94 static const value_string bgpnotify_minor_2[] = {
95 { 1, "Unsupported Version Number" },
97 { 3, "Bad BGP Identifier" },
98 { 4, "Unsupported Optional Parameter" },
99 { 5, "Authentication Failure" },
100 { 6, "Unacceptable Hold Time" },
104 static const value_string bgpnotify_minor_3[] = {
105 { 1, "Malformed Attribute List" },
106 { 2, "Unrecognized Well-known Attribute" },
107 { 3, "Missing Well-known Attribute" },
108 { 4, "Attribute Flags Error" },
109 { 5, "Attribute Length Error" },
110 { 6, "Invalid ORIGIN Attribute" },
111 { 7, "AS Routing Loop" },
112 { 8, "Invalid NEXT_HOP Attribute" },
113 { 9, "Optional Attribute Error" },
114 { 10, "Invalid Network Field" },
115 { 11, "Malformed AS_PATH" },
119 static const value_string *bgpnotify_minor[] = {
120 NULL, bgpnotify_minor_1, bgpnotify_minor_2, bgpnotify_minor_3,
123 static const value_string bgpattr_origin[] = {
130 static const value_string as_segment_type[] = {
132 { 2, "AS_SEQUENCE" },
133 /* This is wrong according to the RFC... in the Zebra code they say that
134 cisco reversed it. Packet traces seem to agree. */
135 { 4, "AS_CONFED_SET" },
136 { 3, "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, NullTVB,
271 offset + offsetof(struct bgp_open, bgpo_version), 1,
272 "Version: %u", bgpo.bgpo_version);
273 proto_tree_add_text(tree, NullTVB,
274 offset + offsetof(struct bgp_open, bgpo_myas), 2,
275 "My AS: %u", ntohs(bgpo.bgpo_myas));
276 proto_tree_add_text(tree, NullTVB,
277 offset + offsetof(struct bgp_open, bgpo_holdtime), 2,
278 "Hold time: %u", ntohs(bgpo.bgpo_holdtime));
279 proto_tree_add_text(tree, NullTVB,
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, NullTVB,
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, NullTVB,
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, NullTVB, 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, NullTVB, 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, NullTVB, p - pd, i, "%s", junk_buf);
356 /* check for advertisements */
358 proto_tree_add_text(tree, NullTVB, p - pd, 2, "Total path attribute length: %u %s",
359 len, (len == 1) ? "byte" : "bytes");
361 /* path attributes */
363 ti = proto_tree_add_text(tree, NullTVB, p - pd + 2, len, "Path attributes");
364 subtree = proto_item_add_subtree(ti, ett_bgp_attrs);
372 memcpy(&bgpa, &p[i], sizeof(bgpa));
373 /* check for the Extended Length bit */
374 if (bgpa.bgpa_flags & BGP_ATTR_FLAG_EXTENDED_LENGTH) {
375 alen = pntohs(&p[i + sizeof(bgpa)]);
376 aoff = sizeof(bgpa) + 2;
378 alen = p[i + sizeof(bgpa)];
379 aoff = sizeof(bgpa) + 1;
382 /* This is kind of ugly - similar code appears twice, but it
383 helps browsing attrs. */
384 /* the first switch prints things in the title of the subtree */
385 switch (bgpa.bgpa_type) {
388 goto default_attribute_top;
389 msg = val_to_str(p[i + aoff], bgpattr_origin, "Unknown");
390 ti = proto_tree_add_text(subtree, NullTVB, p - pd + i, alen + aoff,
392 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
393 msg, alen + aoff, (alen + aoff == 1) ? "byte" :
396 case BGPTYPE_AS_PATH:
398 (p + current attribute + 3 bytes to first tuple) */
399 end = p + alen + i + 3;
401 /* must be freed by second switch! */
402 /* "alen * 6" (5 digits + space) should be a good estimate
403 of how long the AS path string could be */
404 as_path_str = malloc((alen + 1) * 6);
405 if (as_path_str == NULL) break;
406 as_path_str[0] = '\0';
408 /* snarf each AS path */
411 if (type == AS_SET) {
412 snprintf(as_path_str, 2, "{");
414 else if (type == AS_CONFED_SET) {
415 snprintf(as_path_str, 2, "[");
417 else if (type == AS_CONFED_SEQUENCE) {
418 snprintf(as_path_str, 2, "(");
422 /* snarf each value in path */
423 for (j = 0; j < length; j++) {
424 snprintf(junk_buf, sizeof(junk_buf), "%u%s", pntohs(q),
425 (type == AS_SET || type == AS_CONFED_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] = '}';
435 else if (type == AS_CONFED_SET) {
436 as_path_str[strlen(as_path_str) - 2] = ']';
438 else if (type == AS_CONFED_SEQUENCE) {
439 as_path_str[strlen(as_path_str) - 1] = ')';
442 as_path_str[strlen(as_path_str) - 1] = '\0';
446 /* check for empty AS_PATH */
448 strncpy(as_path_str, "empty", 6);
450 ti = proto_tree_add_text(subtree, NullTVB, p - pd + i, alen + aoff,
452 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
453 as_path_str, alen + aoff,
454 (alen + aoff == 1) ? "byte" : "bytes");
456 case BGPTYPE_NEXT_HOP:
458 goto default_attribute_top;
459 ti = proto_tree_add_text(subtree, NullTVB, p - pd + i, alen + aoff,
461 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
462 ip_to_str(&p[i + aoff]), alen + aoff, (alen + aoff == 1)
465 case BGPTYPE_MULTI_EXIT_DISC:
467 goto default_attribute_top;
468 ti = proto_tree_add_text(subtree, NullTVB, p - pd + i, alen + aoff,
470 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
471 pntohl(&p[i + aoff]), alen + aoff,
472 (alen + aoff == 1) ? "byte" : "bytes");
474 case BGPTYPE_LOCAL_PREF:
476 goto default_attribute_top;
477 ti = proto_tree_add_text(subtree, NullTVB, p - pd + i, alen + aoff,
479 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
480 pntohl(&p[i + aoff]), alen + aoff,
481 (alen + aoff == 1) ? "byte" : "bytes");
483 case BGPTYPE_ATOMIC_AGGREGATE:
485 goto default_attribute_top;
486 ti = proto_tree_add_text(subtree, NullTVB, p - pd + i, alen + aoff,
488 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
489 alen + aoff, (alen + aoff == 1) ? "byte" : "bytes");
491 case BGPTYPE_AGGREGATOR:
493 goto default_attribute_top;
494 ti = proto_tree_add_text(subtree, NullTVB, p - pd + i, alen + aoff,
495 "%s: AS: %u origin: %s (%u %s)",
496 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
497 pntohs(&p[i + aoff]),
498 ip_to_str(&p[i + aoff + 2]), alen + aoff,
499 (alen + aoff == 1) ? "byte" : "bytes");
501 case BGPTYPE_COMMUNITIES:
503 goto default_attribute_top;
506 (p + current attribute + 3 bytes to first tuple) */
507 end = p + alen + i + 3;
509 /* must be freed by second switch! */
510 /* "alen * 12" (5 digits, a :, 5 digits + space ) should be
511 a good estimate of how long the communities string could
513 communities_str = malloc((alen + 1) * 12);
514 if (communities_str == NULL) break;
515 communities_str[0] = '\0';
516 memset(junk_buf, 0, sizeof(junk_buf));
518 /* snarf each community */
520 /* check for well-known communities */
521 if (pntohl(q) == BGP_COMM_NO_EXPORT)
522 strncpy(junk_buf, "NO_EXPORT ", 10);
523 else if (pntohl(q) == BGP_COMM_NO_ADVERTISE)
524 strncpy(junk_buf, "NO_ADVERTISE ", 13);
525 else if (pntohl(q) == BGP_COMM_NO_EXPORT_SUBCONFED)
526 strncpy(junk_buf, "NO_EXPORT_SUBCONFED ", 20);
528 snprintf(junk_buf, sizeof(junk_buf), "%u:%u ",
534 strncat(communities_str, junk_buf, sizeof(junk_buf));
536 /* cleanup end of string */
537 communities_str[strlen(communities_str) - 1] = '\0';
539 ti = proto_tree_add_text(subtree, NullTVB, p - pd + i, alen + aoff,
541 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
542 communities_str, alen + aoff,
543 (alen + aoff == 1) ? "byte" : "bytes");
545 case BGPTYPE_ORIGINATOR_ID:
547 goto default_attribute_top;
548 ti = proto_tree_add_text(subtree, NullTVB, p - pd + i, alen + aoff,
550 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
551 ip_to_str(&p[i + aoff]), alen + aoff, (alen + aoff == 1)
554 case BGPTYPE_CLUSTER_LIST:
556 goto default_attribute_top;
559 (p + current attribute + 3 bytes to first tuple) */
560 end = p + alen + i + 3;
562 /* must be freed by second switch! */
563 /* "alen * 16" (12 digits, 3 dots + space ) should be
564 a good estimate of how long the cluster_list string could
566 cluster_list_str = malloc((alen + 1) * 16);
567 if (cluster_list_str == NULL) break;
568 cluster_list_str[0] = '\0';
569 memset(junk_buf, 0, sizeof(junk_buf));
571 /* snarf each cluster list */
573 snprintf(junk_buf, sizeof(junk_buf), "%s ", ip_to_str(q));
574 strncat(cluster_list_str, junk_buf, sizeof(junk_buf));
577 /* cleanup end of string */
578 cluster_list_str[strlen(cluster_list_str) - 1] = '\0';
580 ti = proto_tree_add_text(subtree, NullTVB, p - pd + i, alen + aoff,
582 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
583 cluster_list_str, alen + aoff,
584 (alen + aoff == 1) ? "byte" : "bytes");
587 default_attribute_top:
588 ti = proto_tree_add_text(subtree, NullTVB, p - pd + i, alen + aoff,
590 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
591 alen + aoff, (alen + aoff == 1) ? "byte" : "bytes");
592 } /* end of first switch */
593 subtree2 = proto_item_add_subtree(ti, ett_bgp_attr);
595 /* figure out flags */
597 if (bgpa.bgpa_flags & BGP_ATTR_FLAG_OPTIONAL) {
598 strncat(junk_buf, "Optional, ", 10);
601 strncat(junk_buf, "Well-known, ", 12);
603 if (bgpa.bgpa_flags & BGP_ATTR_FLAG_TRANSITIVE) {
604 strncat(junk_buf, "Transitive, ", 12);
607 strncat(junk_buf, "Non-transitive, ", 16);
609 if (bgpa.bgpa_flags & BGP_ATTR_FLAG_PARTIAL) {
610 strncat(junk_buf, "Partial, ", 9);
613 strncat(junk_buf, "Complete, ", 10);
615 if (bgpa.bgpa_flags & BGP_ATTR_FLAG_EXTENDED_LENGTH) {
616 strncat(junk_buf, "Extended Length, ", 17);
618 /* stomp last ", " */
619 j = strlen(junk_buf);
620 junk_buf[j - 2] = '\0';
621 ti = proto_tree_add_text(subtree2, NullTVB,
622 p - pd + i + offsetof(struct bgp_attr, bgpa_flags), 1,
623 "Flags: 0x%02x (%s)", bgpa.bgpa_flags, junk_buf);
624 subtree3 = proto_item_add_subtree(ti, ett_bgp_attr_flags);
626 /* add flag bitfield subtrees */
627 proto_tree_add_text(subtree3, NullTVB,
628 p - pd + i + offsetof(struct bgp_attr, bgpa_flags), 1,
629 "%s", decode_boolean_bitfield(bgpa.bgpa_flags,
630 BGP_ATTR_FLAG_OPTIONAL, 8, "Optional", "Well-known"));
631 proto_tree_add_text(subtree3, NullTVB,
632 p - pd + i + offsetof(struct bgp_attr, bgpa_flags), 1,
633 "%s", decode_boolean_bitfield(bgpa.bgpa_flags,
634 BGP_ATTR_FLAG_TRANSITIVE, 8, "Transitive",
636 proto_tree_add_text(subtree3, NullTVB,
637 p - pd + i + offsetof(struct bgp_attr, bgpa_flags), 1,
638 "%s", decode_boolean_bitfield(bgpa.bgpa_flags,
639 BGP_ATTR_FLAG_PARTIAL, 8, "Partial", "Complete"));
640 proto_tree_add_text(subtree3, NullTVB,
641 p - pd + i + offsetof(struct bgp_attr, bgpa_flags), 1,
642 "%s", decode_boolean_bitfield(bgpa.bgpa_flags,
643 BGP_ATTR_FLAG_EXTENDED_LENGTH, 8, "Extended length",
646 proto_tree_add_text(subtree2, NullTVB,
647 p - pd + i + offsetof(struct bgp_attr, bgpa_type), 1,
648 "Type code: %s (%u)",
649 val_to_str(bgpa.bgpa_type, bgpattr_type, "Unknown"),
652 proto_tree_add_text(subtree2, NullTVB, p - pd + i + sizeof(bgpa),
653 aoff - sizeof(bgpa), "Length: %d %s", alen,
654 (alen == 1) ? "byte" : "bytes");
656 /* the second switch prints things in the actual subtree of each
658 switch (bgpa.bgpa_type) {
661 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
662 "Origin (invalid): %u %s", alen,
663 (alen == 1) ? "byte" : "bytes");
665 msg = val_to_str(p[i + aoff], bgpattr_origin, "Unknown");
666 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, 1,
667 "Origin: %s (%u)", msg, p[i + aoff]);
670 case BGPTYPE_AS_PATH:
671 /* check for empty AS_PATH */
677 ti = proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
678 "AS path: %s", as_path_str);
679 as_paths_tree = proto_item_add_subtree(ti, ett_bgp_as_paths);
682 (p + current attribute + 3 bytes to first tuple) */
683 end = p + alen + i + 3;
686 /* snarf each AS path tuple, we have to step through each one
687 again to make a separate subtree so we can't just reuse
688 as_path_str from above */
690 as_path_str[0] = '\0';
692 if (type == AS_SET) {
693 snprintf(as_path_str, 2, "{");
695 else if (type == AS_CONFED_SET) {
696 snprintf(as_path_str, 2, "[");
698 else if (type == AS_CONFED_SEQUENCE) {
699 snprintf(as_path_str, 2, "(");
703 /* snarf each value in path, we're just going to reuse
704 as_path_str since we already have it malloced */
705 for (j = 0; j < length; j++) {
706 snprintf(junk_buf, sizeof(junk_buf), "%u%s", pntohs(q),
707 (type == AS_SET || type == AS_CONFED_SET)
709 strncat(as_path_str, junk_buf, sizeof(junk_buf));
713 /* cleanup end of string */
714 if (type == AS_SET) {
715 as_path_str[strlen(as_path_str) - 2] = '}';
717 else if (type == AS_CONFED_SET) {
718 as_path_str[strlen(as_path_str) - 2] = ']';
720 else if (type == AS_CONFED_SEQUENCE) {
721 as_path_str[strlen(as_path_str) - 1] = ')';
724 as_path_str[strlen(as_path_str) - 1] = '\0';
727 /* length here means number of ASs, ie length * 2 bytes */
728 ti = proto_tree_add_text(as_paths_tree, NullTVB,
729 q - pd - length * 2 - 2,
730 length * 2 + 2, "AS path segment: %s", as_path_str);
731 as_path_tree = proto_item_add_subtree(ti, ett_bgp_as_paths);
732 proto_tree_add_text(as_path_tree, NullTVB, q - pd - length * 2 - 2,
733 1, "Path segment type: %s (%u)",
734 val_to_str(type, as_segment_type, "Unknown"), type);
735 proto_tree_add_text(as_path_tree, NullTVB, q - pd - length * 2 - 1,
736 1, "Path segment length: %u %s", length,
737 (length == 1) ? "AS" : "ASs");
739 /* backup and reprint path segment value(s) only */
741 as_path_str[0] = '\0';
742 for (j = 0; j < length; j++) {
743 snprintf(junk_buf, sizeof(junk_buf), "%u ", pntohs(q));
744 strncat(as_path_str, junk_buf, sizeof(junk_buf));
747 as_path_str[strlen(as_path_str) - 1] = '\0';
749 proto_tree_add_text(as_path_tree, NullTVB, q - pd - length * 2,
750 length * 2, "Path segment value: %s", as_path_str);
755 case BGPTYPE_NEXT_HOP:
757 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
758 "Next hop (invalid): %u %s", alen,
759 (alen == 1) ? "byte" : "bytes");
761 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
762 "Next hop: %s", ip_to_str(&p[i + aoff]));
765 case BGPTYPE_MULTI_EXIT_DISC:
767 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
768 "Multiple exit discriminator (invalid): %u %s",
769 alen, (alen == 1) ? "byte" : "bytes");
771 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
772 "Multiple exit discriminator: %u",
773 pntohl(&p[i + aoff]));
776 case BGPTYPE_LOCAL_PREF:
778 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
779 "Local preference (invalid): %u %s", alen,
780 (alen == 1) ? "byte" : "bytes");
782 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
783 "Local preference: %u", pntohl(&p[i + aoff]));
786 case BGPTYPE_ATOMIC_AGGREGATE:
788 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
789 "Atomic aggregate (invalid): %u %s", alen,
790 (alen == 1) ? "byte" : "bytes");
793 case BGPTYPE_AGGREGATOR:
795 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
796 "Aggregator (invalid): %u %s", alen,
797 (alen == 1) ? "byte" : "bytes");
799 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, 2,
800 "Aggregator AS: %u", pntohs(&p[i + aoff]));
801 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff + 2, 4,
802 "Aggregator origin: %s",
803 ip_to_str(&p[i + aoff + 2]));
806 case BGPTYPE_COMMUNITIES:
808 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
809 "Communities (invalid): %u %s", alen,
810 (alen == 1) ? "byte" : "bytes");
811 free(communities_str);
815 ti = proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
816 "Communities: %s", communities_str);
817 communities_tree = proto_item_add_subtree(ti,
818 ett_bgp_communities);
821 (p + current attribute + 3 bytes to first tuple) */
822 end = p + alen + i + 3;
825 /* snarf each community */
827 /* check for reserved values */
828 if (pntohs(q) == FOURHEX0 || pntohs(q) == FOURHEXF) {
829 /* check for well-known communities */
830 if (pntohl(q) == BGP_COMM_NO_EXPORT)
831 proto_tree_add_text(communities_tree, NullTVB,
832 q - pd - 3 + aoff, 4,
833 "Community: NO_EXPORT (0x%x)", pntohl(q));
834 else if (pntohl(q) == BGP_COMM_NO_ADVERTISE)
835 proto_tree_add_text(communities_tree, NullTVB,
836 q - pd - 3 + aoff, 4,
837 "Community: NO_ADVERTISE (0x%x)", pntohl(q));
838 else if (pntohl(q) == BGP_COMM_NO_EXPORT_SUBCONFED)
839 proto_tree_add_text(communities_tree, NullTVB,
840 q - pd - 3 + aoff, 4,
841 "Community: NO_EXPORT_SUBCONFED (0x%x)",
844 proto_tree_add_text(communities_tree, NullTVB,
845 q - pd - 3 + aoff, 4,
846 "Community (reserved): 0x%x", pntohl(q));
850 ti = proto_tree_add_text(communities_tree, NullTVB,
851 q - pd - 3 + aoff, 4, "Community: %u:%u",
852 pntohs(q), pntohs(q + 2));
853 community_tree = proto_item_add_subtree(ti,
854 ett_bgp_communities);
855 proto_tree_add_text(community_tree, NullTVB, q - pd - 3 + aoff,
856 2, "Community AS: %u", pntohs(q));
857 proto_tree_add_text(community_tree, NullTVB, q - pd - 1 + aoff,
858 2, "Community value: %u", pntohs(q + 2));
864 free(communities_str);
866 case BGPTYPE_ORIGINATOR_ID:
868 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
869 "Originator identifier (invalid): %u %s", alen,
870 (alen == 1) ? "byte" : "bytes");
872 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
873 "Originator identifier: %s",
874 ip_to_str(&p[i + aoff]));
877 case BGPTYPE_MP_REACH_NLRI:
878 af = pntohs(&p[i + aoff]);
879 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, 2,
880 "Address family: %s (%u)",
881 val_to_str(af, afnumber, "Unknown"), af);
882 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff + 2, 1,
883 "Subsequent address family identifier: %s (%u)",
884 val_to_str(p[i + aoff + 2], bgpattr_nlri_safi,
885 p[i + aoff + 2] >= 128 ? "Vendor specific" : "Unknown"),
887 ti = proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff + 3, 1,
888 "Next hop network address (%d %s)",
889 p[i + aoff + 3], (p[i + aoff + 3] == 1) ? "byte" :
891 if (af == AFNUM_INET || af == AFNUM_INET6) {
895 subtree3 = proto_item_add_subtree(ti,
896 ett_bgp_mp_reach_nlri);
899 while (j < p[i + aoff + 3]) {
900 if (af == AFNUM_INET)
902 else if (af == AFNUM_INET6)
906 if (j + advance > p[i + aoff + 3])
909 if (af == AFNUM_INET)
910 s = ip_to_str(&p[i + aoff + 4 + j]);
912 s = ip6_to_str((struct e_in6_addr *)
913 &p[i + aoff + 4 + j]);
915 proto_tree_add_text(subtree3, NullTVB,
916 p - pd + i + aoff + 4 + j, advance,
922 alen -= (p[i + aoff + 3] + 4);
923 aoff += (p[i + aoff + 3] + 4);
926 ti = proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, 1,
927 "Subnetwork points of attachment: %u", snpa);
930 subtree3 = proto_item_add_subtree(ti,
931 ett_bgp_mp_reach_nlri);
932 for (/*nothing*/; snpa > 0; snpa--) {
933 proto_tree_add_text(subtree3, NullTVB, p - pd + i + aoff + off, 1,
934 "SNPA length: %u", p[i + aoff + off]);
936 proto_tree_add_text(subtree3, NullTVB, p - pd + i + aoff + off,
937 p[i + aoff + off - 1],
938 "SNPA (%u %s)", p[i + aoff + off - 1],
939 (p[i + aoff + off - 1] == 1) ? "byte" : "bytes");
940 off += p[i + aoff + off - 1];
945 ti = proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
946 "Network layer reachability information (%u %s)",
947 alen, (alen == 1) ? "byte" : "bytes");
949 subtree3 = proto_item_add_subtree(ti,
950 ett_bgp_mp_unreach_nlri);
955 if (af == AFNUM_INET) {
956 advance = decode_prefix4(&p[i + aoff], buf,
958 } else if (af == AFNUM_INET6) {
959 advance = decode_prefix6(&p[i + aoff], buf,
967 proto_tree_add_text(subtree3, NullTVB, p - pd + i + aoff, advance,
968 "Network layer reachability information: %s", buf);
975 case BGPTYPE_MP_UNREACH_NLRI:
976 af = pntohs(&p[i + aoff]);
977 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, 2,
978 "Address family: %s (%u)",
979 val_to_str(af, afnumber, "Unknown"), af);
980 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff + 2, 1,
981 "Subsequent address family identifier: %s (%u)",
982 val_to_str(p[i + aoff + 2], bgpattr_nlri_safi,
983 p[i + aoff + 2] >= 128 ? "Vendor specific" : "Unknown"),
985 ti = proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff + 3,
986 alen - 3, "Withdrawn routes (%u %s)", alen - 3,
987 (alen - 3 == 1) ? "byte" : "bytes");
992 subtree3 = proto_item_add_subtree(ti,
993 ett_bgp_mp_unreach_nlri);
998 if (af == AFNUM_INET) {
999 advance = decode_prefix4(&p[i + aoff], buf,
1001 } else if (af == AFNUM_INET6) {
1002 advance = decode_prefix6(&p[i + aoff], buf,
1010 proto_tree_add_text(subtree3, NullTVB, p - pd + i + aoff, advance,
1011 "Withdrawn route: %s", buf);
1018 case BGPTYPE_CLUSTER_LIST:
1019 if (alen % 4 != 0) {
1020 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
1021 "Cluster list (invalid): %u %s", alen,
1022 (alen == 1) ? "byte" : "bytes");
1023 free(cluster_list_str);
1027 ti = proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
1028 "Cluster list: %s", cluster_list_str);
1029 cluster_list_tree = proto_item_add_subtree(ti,
1030 ett_bgp_cluster_list);
1033 (p + current attribute + 3 bytes to first tuple) */
1034 end = p + alen + i + 3;
1037 /* snarf each cluster identifier */
1039 ti = proto_tree_add_text(cluster_list_tree, NullTVB,
1040 q - pd - 3 + aoff, 4, "Cluster identifier: %s",
1046 free(cluster_list_str);
1049 proto_tree_add_text(subtree2, NullTVB, p - pd + i + aoff, alen,
1050 "Unknown (%d %s)", alen, (alen == 1) ? "byte" :
1053 } /* end of second switch */
1060 len = hlen - (p - &pd[offset]);
1062 /* parse prefixes */
1064 ti = proto_tree_add_text(tree, NullTVB, p - pd, len,
1065 "Network layer reachability information: %u %s", len,
1066 (len == 1) ? "byte" : "bytes");
1067 subtree = proto_item_add_subtree(ti, ett_bgp_nlri);
1070 i = decode_prefix4(p, junk_buf, sizeof(junk_buf));
1071 proto_tree_add_text(subtree, NullTVB, p - pd, i, "%s", junk_buf);
1079 * Dissect a BGP NOTIFICATION message.
1082 dissect_bgp_notification(const u_char *pd, int offset, frame_data *fd,
1085 struct bgp_notification bgpn; /* BGP NOTIFICATION message */
1086 int hlen; /* message length */
1087 char *p; /* string pointer */
1090 memcpy(&bgpn, &pd[offset], sizeof(bgpn));
1091 hlen = ntohs(bgpn.bgpn_len);
1093 /* print error code */
1094 proto_tree_add_text(tree, NullTVB,
1095 offset + offsetof(struct bgp_notification, bgpn_major), 1,
1096 "Error code: %s (%u)",
1097 val_to_str(bgpn.bgpn_major, bgpnotify_major, "Unknown"),
1100 /* print error subcode */
1101 if (bgpn.bgpn_major < array_length(bgpnotify_minor)
1102 && bgpnotify_minor[bgpn.bgpn_major] != NULL) {
1103 p = val_to_str(bgpn.bgpn_minor, bgpnotify_minor[bgpn.bgpn_major],
1105 } else if (bgpn.bgpn_minor == 0)
1109 proto_tree_add_text(tree, NullTVB,
1110 offset + offsetof(struct bgp_notification, bgpn_minor), 1,
1111 "Error subcode: %s (%u)", p, bgpn.bgpn_minor);
1113 /* only print if there is optional data */
1114 if (hlen > BGP_MIN_NOTIFICATION_MSG_SIZE) {
1115 proto_tree_add_text(tree, NullTVB, offset + BGP_MIN_NOTIFICATION_MSG_SIZE,
1116 hlen - BGP_MIN_NOTIFICATION_MSG_SIZE, "Data");
1121 * Dissect a BGP packet.
1124 dissect_bgp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
1126 proto_item *ti; /* tree item */
1127 proto_tree *bgp_tree; /* BGP packet tree */
1128 proto_tree *bgp1_tree; /* BGP message tree */
1129 const u_char *p; /* packet offset pointer */
1131 int found; /* number of BGP messages in packet */
1132 static u_char marker[] = { /* BGP message marker */
1133 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1134 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1136 struct bgp bgp; /* BGP header */
1137 int hlen; /* BGP header length */
1138 char *typ; /* BGP message type */
1140 OLD_CHECK_DISPLAY_AS_DATA(proto_bgp, pd, offset, fd, tree);
1142 if (check_col(fd, COL_PROTOCOL))
1143 col_add_str(fd, COL_PROTOCOL, "BGP");
1149 /* run through the TCP packet looking for BGP headers */
1150 /* this is done twice, but this way each message type can be
1151 printed in the COL_INFO field */
1153 /* look for bgp header */
1158 CHECK_SIZE(i, sizeof(marker), l);
1159 if (memcmp(&p[i], marker, sizeof(marker)) != 0) {
1164 memcpy(&bgp, &p[i], sizeof(bgp));
1166 hlen = ntohs(bgp.bgp_len);
1167 typ = val_to_str(bgp.bgp_type, bgptypevals, "Unknown Message");
1169 if (check_col(fd, COL_INFO)) {
1171 col_add_fstr(fd, COL_INFO, "%s", typ);
1173 col_append_fstr(fd, COL_INFO, ", %s", typ);
1180 ti = proto_tree_add_text(tree, NullTVB, offset, END_OF_FRAME,
1181 "Border Gateway Protocol");
1182 bgp_tree = proto_item_add_subtree(ti, ett_bgp);
1187 /* now, run through the TCP packet again, this time dissect */
1188 /* each message that we find */
1190 /* look for bgp header */
1195 CHECK_SIZE(i, sizeof(marker), l);
1196 if (memcmp(&p[i], marker, sizeof(marker)) != 0) {
1201 memcpy(&bgp, &p[i], sizeof(bgp));
1202 hlen = ntohs(bgp.bgp_len);
1203 typ = val_to_str(bgp.bgp_type, bgptypevals, "Unknown Message");
1204 if (END_OF_FRAME < hlen) {
1205 ti = proto_tree_add_text(bgp_tree, NullTVB, offset + i, END_OF_FRAME,
1206 "%s (truncated)", typ);
1208 ti = proto_tree_add_text(bgp_tree, NullTVB, offset + i, hlen,
1211 /* add a different tree for each message type */
1212 switch (bgp.bgp_type) {
1214 bgp1_tree = proto_item_add_subtree(ti, ett_bgp_open);
1217 bgp1_tree = proto_item_add_subtree(ti, ett_bgp_update);
1219 case BGP_NOTIFICATION:
1220 bgp1_tree = proto_item_add_subtree(ti, ett_bgp_notification);
1223 bgp1_tree = proto_item_add_subtree(ti, ett_bgp);
1226 bgp1_tree = proto_item_add_subtree(ti, ett_bgp);
1230 proto_tree_add_text(bgp1_tree, NullTVB, offset + i, BGP_MARKER_SIZE,
1231 "Marker: 16 bytes");
1233 if (hlen < BGP_HEADER_SIZE || hlen > BGP_MAX_PACKET_SIZE) {
1234 proto_tree_add_text(bgp1_tree, NullTVB,
1235 offset + i + offsetof(struct bgp, bgp_len), 2,
1236 "Length (invalid): %u %s", hlen,
1237 (hlen == 1) ? "byte" : "bytes");
1239 proto_tree_add_text(bgp1_tree, NullTVB,
1240 offset + i + offsetof(struct bgp, bgp_len), 2,
1241 "Length: %u %s", hlen,
1242 (hlen == 1) ? "byte" : "bytes");
1245 proto_tree_add_text(bgp1_tree, NullTVB,
1246 offset + i + offsetof(struct bgp, bgp_type), 1,
1247 "Type: %s (%u)", typ, bgp.bgp_type);
1249 CHECK_SIZE(i, hlen, l);
1251 /* handle each message type */
1252 switch (bgp.bgp_type) {
1254 dissect_bgp_open(pd, offset + i, fd, bgp1_tree);
1257 dissect_bgp_update(pd, offset + i, fd, bgp1_tree);
1259 case BGP_NOTIFICATION:
1260 dissect_bgp_notification(pd, offset + i, fd, bgp1_tree);
1263 /* no data in KEEPALIVE messages */
1275 * Register ourselves.
1278 proto_register_bgp(void)
1280 static gint *ett[] = {
1285 &ett_bgp_attr_flags,
1286 &ett_bgp_mp_reach_nlri,
1287 &ett_bgp_mp_unreach_nlri,
1291 &ett_bgp_notification,
1293 &ett_bgp_communities,
1294 &ett_bgp_cluster_list,
1297 proto_bgp = proto_register_protocol("Border Gateway Protocol", "bgp");
1298 proto_register_subtree_array(ett, array_length(ett));
1302 proto_reg_handoff_bgp(void)
1304 old_dissector_add("tcp.port", TCP_PORT_BGP, dissect_bgp);