+// SPDX-License-Identifier: GPL-2.0-or-later
/* BGP attributes management routines.
* Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro
- *
- * This file is part of GNU Zebra.
- *
- * GNU Zebra is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2, or (at your option) any
- * later version.
- *
- * GNU Zebra is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; see the file COPYING; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
* such messages, conformant BGP speakers SHOULD use the "Treat-as-
* withdraw" error handling behavior as per [RFC7606].
*/
- if (peer->bgp->reject_as_sets && aspath_check_as_sets(attr->aspath)) {
+ if (peer->bgp && peer->bgp->reject_as_sets &&
+ aspath_check_as_sets(attr->aspath)) {
flog_err(EC_BGP_ATTR_MAL_AS_PATH,
"AS_SET and AS_CONFED_SET are deprecated from %pBP",
peer);
args->total);
}
- if (peer->discard_attrs[args->type])
+ if (peer->discard_attrs[args->type] || peer->withdraw_attrs[args->type])
goto atomic_ignore;
/* Set atomic aggregate flag. */
atomic_ignore:
stream_forward_getp(peer->curr, length);
- if (bgp_debug_update(peer, NULL, NULL, 1))
- zlog_debug("%pBP: Ignoring attribute %s", peer,
- lookup_msg(attr_str, args->type, NULL));
-
- return BGP_ATTR_PARSE_PROCEED;
+ return bgp_attr_ignore(peer, args->type);
}
/* Aggregator attribute */
args->total);
}
- if (peer->discard_attrs[args->type])
+ if (peer->discard_attrs[args->type] || peer->withdraw_attrs[args->type])
goto aggregator_ignore;
if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV))
aggregator_ignore:
stream_forward_getp(peer->curr, length);
- if (bgp_debug_update(peer, NULL, NULL, 1))
- zlog_debug("%pBP: Ignoring attribute %s", peer,
- lookup_msg(attr_str, args->type, NULL));
-
- return BGP_ATTR_PARSE_PROCEED;
+ return bgp_attr_ignore(peer, args->type);
}
/* New Aggregator attribute */
0);
}
- if (peer->discard_attrs[args->type])
+ if (peer->discard_attrs[args->type] || peer->withdraw_attrs[args->type])
goto as4_aggregator_ignore;
aggregator_as = stream_getl(peer->curr);
as4_aggregator_ignore:
stream_forward_getp(peer->curr, length);
- if (bgp_debug_update(peer, NULL, NULL, 1))
- zlog_debug("%pBP: Ignoring attribute %s", peer,
- lookup_msg(attr_str, args->type, NULL));
-
- return BGP_ATTR_PARSE_PROCEED;
+ return bgp_attr_ignore(peer, args->type);
}
/* Munge Aggregator and New-Aggregator, AS_PATH and NEW_AS_PATH.
args->total);
}
- if (peer->discard_attrs[args->type])
+ if (peer->discard_attrs[args->type] || peer->withdraw_attrs[args->type])
goto community_ignore;
bgp_attr_set_community(
community_ignore:
stream_forward_getp(peer->curr, length);
- if (bgp_debug_update(peer, NULL, NULL, 1))
- zlog_debug("%pBP: Ignoring attribute %s", peer,
- lookup_msg(attr_str, args->type, NULL));
-
- return BGP_ATTR_PARSE_PROCEED;
+ return bgp_attr_ignore(peer, args->type);
}
/* Originator ID attribute. */
args->total);
}
- if (peer->discard_attrs[args->type])
+ if (peer->discard_attrs[args->type] || peer->withdraw_attrs[args->type])
goto originator_id_ignore;
attr->originator_id.s_addr = stream_get_ipv4(peer->curr);
originator_id_ignore:
stream_forward_getp(peer->curr, length);
- if (bgp_debug_update(peer, NULL, NULL, 1))
- zlog_debug("%pBP: Ignoring attribute %s", peer,
- lookup_msg(attr_str, args->type, NULL));
-
- return BGP_ATTR_PARSE_PROCEED;
+ return bgp_attr_ignore(peer, args->type);
}
/* Cluster list attribute. */
args->total);
}
- if (peer->discard_attrs[args->type])
+ if (peer->discard_attrs[args->type] || peer->withdraw_attrs[args->type])
goto cluster_list_ignore;
bgp_attr_set_cluster(
cluster_list_ignore:
stream_forward_getp(peer->curr, length);
- if (bgp_debug_update(peer, NULL, NULL, 1))
- zlog_debug("%pBP: Ignoring attribute %s", peer,
- lookup_msg(attr_str, args->type, NULL));
-
- return BGP_ATTR_PARSE_PROCEED;
+ return bgp_attr_ignore(peer, args->type);
}
/* Multiprotocol reachability information parse. */
args->total);
}
- if (peer->discard_attrs[args->type])
+ if (peer->discard_attrs[args->type] || peer->withdraw_attrs[args->type])
goto large_community_ignore;
bgp_attr_set_lcommunity(
large_community_ignore:
stream_forward_getp(peer->curr, length);
- if (bgp_debug_update(peer, NULL, NULL, 1))
- zlog_debug("%pBP: Ignoring attribute %s", peer,
- lookup_msg(attr_str, args->type, NULL));
-
- return BGP_ATTR_PARSE_PROCEED;
+ return bgp_attr_ignore(peer, args->type);
}
/* Extended Community attribute. */
args->total);
}
- if (peer->discard_attrs[args->type])
+ if (peer->discard_attrs[args->type] || peer->withdraw_attrs[args->type])
goto ipv6_ext_community_ignore;
ipv6_ecomm = ecommunity_parse_ipv6(
ipv6_ext_community_ignore:
stream_forward_getp(peer->curr, length);
- if (bgp_debug_update(peer, NULL, NULL, 1))
- zlog_debug("%pBP: Ignoring attribute %s", peer,
- lookup_msg(attr_str, args->type, NULL));
-
- return BGP_ATTR_PARSE_PROCEED;
+ return bgp_attr_ignore(peer, args->type);
}
/* Parse Tunnel Encap attribute in an UPDATE */
goto aigp_ignore;
}
- if (peer->discard_attrs[args->type])
+ if (peer->discard_attrs[args->type] || peer->withdraw_attrs[args->type])
goto aigp_ignore;
if (!bgp_attr_aigp_valid(s, length))
aigp_ignore:
stream_forward_getp(peer->curr, length);
- if (bgp_debug_update(peer, NULL, NULL, 1))
- zlog_debug("%pBP: Ignoring attribute %s", peer,
- lookup_msg(attr_str, args->type, NULL));
-
- return BGP_ATTR_PARSE_PROCEED;
+ return bgp_attr_ignore(peer, args->type);
}
/* OTC attribute. */
args->total);
}
- if (peer->discard_attrs[args->type])
+ if (peer->discard_attrs[args->type] || peer->withdraw_attrs[args->type])
goto otc_ignore;
attr->otc = stream_getl(peer->curr);
otc_ignore:
stream_forward_getp(peer->curr, length);
- if (bgp_debug_update(peer, NULL, NULL, 1))
- zlog_debug("%pBP: Ignoring attribute %s", peer,
- lookup_msg(attr_str, args->type, NULL));
-
- return BGP_ATTR_PARSE_PROCEED;
+ return bgp_attr_ignore(peer, args->type);
}
/* BGP unknown attribute treatment. */
/* Forward read pointer of input stream. */
stream_forward_getp(peer->curr, length);
- if (peer->discard_attrs[type]) {
- if (bgp_debug_update(peer, NULL, NULL, 1))
- zlog_debug("%pBP: Ignoring attribute %s", peer,
- lookup_msg(attr_str, args->type, NULL));
-
- return BGP_ATTR_PARSE_PROCEED;
- }
+ if (peer->discard_attrs[type] || peer->withdraw_attrs[type])
+ return bgp_attr_ignore(peer, type);
/* If any of the mandatory well-known attributes are not recognized,
then the Error Subcode is set to Unrecognized Well-known
}
} break;
case SAFI_MPLS_VPN: {
+ if (attr->mp_nexthop_len ==
+ BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL)
+ stream_putc(s, attr->mp_nexthop_len);
+ else
+ stream_putc(s, BGP_ATTR_NHLEN_VPNV6_GLOBAL);
+ stream_putl(s, 0); /* RD = 0, per RFC */
+ stream_putl(s, 0);
+ stream_put(s, &attr->mp_nexthop_global,
+ IPV6_MAX_BYTELEN);
if (attr->mp_nexthop_len ==
BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL) {
- stream_putc(s, 48);
- stream_putl(s, 0); /* RD = 0, per RFC */
- stream_putl(s, 0);
- stream_put(s, &attr->mp_nexthop_global,
- IPV6_MAX_BYTELEN);
stream_putl(s, 0); /* RD = 0, per RFC */
stream_putl(s, 0);
stream_put(s, &attr->mp_nexthop_local,
IPV6_MAX_BYTELEN);
- } else {
- stream_putc(s, 24);
- stream_putl(s, 0); /* RD = 0, per RFC */
- stream_putl(s, 0);
- stream_put(s, &attr->mp_nexthop_global,
- IPV6_MAX_BYTELEN);
}
} break;
case SAFI_ENCAP:
peer_clear_soft(peer, afi, safi, BGP_CLEAR_SOFT_IN);
}
}
+
+void bgp_path_attribute_withdraw_vty(struct vty *vty, struct peer *peer,
+ const char *withdraw_attrs, bool set)
+{
+ int i, num_attributes;
+ char **attributes;
+ afi_t afi;
+ safi_t safi;
+
+ /* If `no` command specified without arbitrary attributes,
+ * then flush all.
+ */
+ if (!withdraw_attrs) {
+ for (i = 0; i < BGP_ATTR_MAX; i++)
+ peer->withdraw_attrs[i] = false;
+ goto withdraw_soft_clear;
+ }
+
+ if (withdraw_attrs) {
+ frrstr_split(withdraw_attrs, " ", &attributes, &num_attributes);
+
+ if (set)
+ for (i = 0; i < BGP_ATTR_MAX; i++)
+ peer->withdraw_attrs[i] = false;
+
+ for (i = 0; i < num_attributes; i++) {
+ uint8_t attr_num = strtoul(attributes[i], NULL, 10);
+
+ XFREE(MTYPE_TMP, attributes[i]);
+
+ /* Some of the attributes, just can't be ignored. */
+ if (attr_num == BGP_ATTR_ORIGIN ||
+ attr_num == BGP_ATTR_AS_PATH ||
+ attr_num == BGP_ATTR_NEXT_HOP ||
+ attr_num == BGP_ATTR_MULTI_EXIT_DISC ||
+ attr_num == BGP_ATTR_MP_REACH_NLRI ||
+ attr_num == BGP_ATTR_MP_UNREACH_NLRI ||
+ attr_num == BGP_ATTR_EXT_COMMUNITIES) {
+ vty_out(vty,
+ "%% Can't treat-as-withdraw path-attribute %s, ignoring.\n",
+ lookup_msg(attr_str, attr_num, NULL));
+ continue;
+ }
+
+ /* Ignore local-pref, originator-id, cluster-list only
+ * for eBGP.
+ */
+ if (peer->sort != BGP_PEER_EBGP &&
+ (attr_num == BGP_ATTR_LOCAL_PREF ||
+ attr_num == BGP_ATTR_ORIGINATOR_ID ||
+ attr_num == BGP_ATTR_CLUSTER_LIST)) {
+ vty_out(vty,
+ "%% Can treat-as-withdraw path-attribute %s only for eBGP, ignoring.\n",
+ lookup_msg(attr_str, attr_num, NULL));
+ continue;
+ }
+
+ peer->withdraw_attrs[attr_num] = set;
+ }
+ XFREE(MTYPE_TMP, attributes);
+ withdraw_soft_clear:
+ /* Configuring path attributes to be treated as withdraw will
+ * trigger
+ * an inbound Route Refresh to ensure that the routing table
+ * is up to date.
+ */
+ FOREACH_AFI_SAFI (afi, safi)
+ peer_clear_soft(peer, afi, safi, BGP_CLEAR_SOFT_IN);
+ }
+}
+
+enum bgp_attr_parse_ret bgp_attr_ignore(struct peer *peer, uint8_t type)
+{
+ bool discard = peer->discard_attrs[type];
+ bool withdraw = peer->withdraw_attrs[type];
+
+ if (bgp_debug_update(peer, NULL, NULL, 1) && (discard || withdraw))
+ zlog_debug("%pBP: Ignoring attribute %s (%s)", peer,
+ lookup_msg(attr_str, type, NULL),
+ withdraw ? "treat-as-withdraw" : "discard");
+
+ return withdraw ? BGP_ATTR_PARSE_WITHDRAW : BGP_ATTR_PARSE_PROCEED;
+}