Commit ac65631deb for frr
commit ac65631deba551a2e2d58c0acc007bf35ea22f1a
Author: aprathik <aprathik@nvidia.com>
Date: Thu Nov 19 14:36:37 2026 +0530
bgpd: reset connections when their bound interface is deleted
A numbered IPv6 iBGP session can stay Established after its bound
VLAN device is deleted. When the VLAN returns with a new ifindex,
the peer's global nexthop can resolve again, but learned paths with
link-local nexthops still refer to the old ifindex. Those routes stay
out of the RIB until BGP resets the session.
Record the socket's device binding on the connection. When zebra
reports interface deletion, stop connections bound to that ifindex
in the same namespace. Normal FSM teardown releases the old paths,
and reconnecting lets BGP relearn them with the new interface scope.
Signed-off-by: Aprathi K <aprathik@nvidia.com>
diff --git a/bgpd/bgp_fsm.c b/bgpd/bgp_fsm.c
index a57338f83b..cf980b71c9 100644
--- a/bgpd/bgp_fsm.c
+++ b/bgpd/bgp_fsm.c
@@ -704,6 +704,7 @@ const char *const peer_down_str[] = {
"Cease: out of resources",
"Cease: hard reset",
"Cease: subcode unknown",
+ "Bound interface deleted",
};
static void bgp_graceful_restart_timer_off(struct peer_connection *connection)
@@ -2359,6 +2360,7 @@ enum bgp_fsm_state_progress bgp_stop(struct peer_connection *connection)
connection->fd = -1;
connection->dir = UNKNOWN;
}
+ connection->socket_binding_ifindex = IFINDEX_INTERNAL;
/* Reset capabilities. */
peer->cap = 0;
diff --git a/bgpd/bgp_network.c b/bgpd/bgp_network.c
index 64ac1b639c..f14fe8f534 100644
--- a/bgpd/bgp_network.c
+++ b/bgpd/bgp_network.c
@@ -38,7 +38,7 @@
extern struct zebra_privs_t bgpd_privs;
-static char *bgp_get_bound_name(struct peer_connection *connection);
+static char *bgp_get_bound_name(struct peer_connection *connection, ifindex_t *ifindex);
void bgp_dump_listener_info(struct vty *vty)
{
@@ -433,6 +433,8 @@ static void bgp_accept(struct event *event)
char buf[SU_ADDRSTRLEN];
struct bgp *bgp = NULL;
enum bgp_peer_active active;
+ ifindex_t ifindex = IFINDEX_INTERNAL;
+ const char *bound_name;
sockunion_init(&su);
@@ -554,8 +556,9 @@ static void bgp_accept(struct event *event)
sockopt_tcp_mss_set(bgp_sock, dynamic_peer->tcp_mss);
frr_with_privs (&bgpd_privs) {
- vrf_bind(dynamic_peer->bgp->vrf_id, bgp_sock,
- bgp_get_bound_name(incoming));
+ bound_name = bgp_get_bound_name(incoming, &ifindex);
+ if (vrf_bind(dynamic_peer->bgp->vrf_id, bgp_sock, bound_name) == 0)
+ incoming->socket_binding_ifindex = ifindex;
}
bgp_peer_connection_reg_with_nht(incoming);
bgp_fsm_change_status(incoming, Active);
@@ -714,7 +717,9 @@ static void bgp_accept(struct event *event)
doppelganger->host);
frr_with_privs(&bgpd_privs) {
- vrf_bind(bgp->vrf_id, bgp_sock, bgp_get_bound_name(incoming));
+ bound_name = bgp_get_bound_name(incoming, &ifindex);
+ if (vrf_bind(bgp->vrf_id, bgp_sock, bound_name) == 0)
+ incoming->socket_binding_ifindex = ifindex;
}
bgp_peer_connection_reg_with_nht(incoming);
bgp_fsm_change_status(incoming, Active);
@@ -752,11 +757,18 @@ static void bgp_accept(struct event *event)
bgp_nht_interface_events(doppelganger);
}
-/* BGP socket bind. */
-static char *bgp_get_bound_name(struct peer_connection *connection)
+/*
+ * BGP socket bind. Returns the name of the device the socket is to be bound to
+ * (NULL if none) and, when that device is known, its index in *ifindex. The
+ * caller initializes *ifindex to IFINDEX_INTERNAL.
+ */
+static char *bgp_get_bound_name(struct peer_connection *connection, ifindex_t *ifindex)
{
struct peer *peer = connection->peer;
+ struct vrf *vrf = vrf_lookup_by_id(peer->bgp->vrf_id);
+ struct interface *ifp;
struct connected *c;
+ char *name = NULL;
if (connection->su.sa.sa_family != AF_INET &&
connection->su.sa.sa_family != AF_INET6)
@@ -767,10 +779,9 @@ static char *bgp_get_bound_name(struct peer_connection *connection)
* VRF are the situations to bind.
*/
if (connection->su.sa.sa_family == AF_INET6 && peer->conf_if)
- return peer->conf_if;
-
- if (peer->ifname)
- return peer->ifname;
+ name = peer->conf_if;
+ else if (peer->ifname)
+ name = peer->ifname;
/*
* If a v6 neighbors address is covered by an interfaces connected prefix,
@@ -779,18 +790,42 @@ static char *bgp_get_bound_name(struct peer_connection *connection)
* some random source address to be choosen.
* Skip link-local neighbors: they match every interface.
*/
- if (connection->su.sa.sa_family == AF_INET6 &&
+ if (!name && connection->su.sa.sa_family == AF_INET6 &&
!IN6_IS_ADDR_LINKLOCAL(&connection->su.sin6.sin6_addr)) {
c = if_lookup_address(&connection->su.sin6.sin6_addr, connection->su.sa.sa_family,
peer->bgp->vrf_id);
if (c)
- return c->ifp->name;
+ name = c->ifp->name;
}
- if (peer->bgp->inst_type == BGP_INSTANCE_TYPE_VIEW || peer->bgp->vrf_id == VRF_DEFAULT)
- return NULL;
+ /*
+ * The netns backend has no VRF
+ * device (the VRF is a namespace and the socket is already created inside
+ * it), and neither do the default VRF and views, so there is nothing to
+ * bind to in those cases.
+ */
+ if (!name && peer->bgp->inst_type != BGP_INSTANCE_TYPE_VIEW &&
+ peer->bgp->vrf_id != VRF_DEFAULT && !vrf_is_backend_netns())
+ name = peer->bgp->name;
+
+ if (!name || !vrf)
+ return name;
+
+ ifp = if_lookup_by_name(name, vrf->vrf_id);
+ /* A VRF master device can belong to the default VRF. */
+ if (!ifp && !vrf_is_backend_netns())
+ ifp = if_lookup_by_name(name, VRF_DEFAULT);
+#ifdef SO_BINDTODEVICE
+ /*
+ * SO_BINDTODEVICE is what ties the socket to the device. Where it is
+ * unavailable the socket stays unbound, so there is no bound ifindex
+ * to record.
+ */
+ if (ifp)
+ *ifindex = ifp->ifindex;
+#endif
- return peer->bgp->name;
+ return name;
}
int bgp_update_address(struct interface *ifp, const union sockunion *dst,
@@ -857,6 +892,8 @@ static int bgp_update_source(struct peer_connection *connection)
enum connect_result bgp_connect(struct peer_connection *connection)
{
struct peer *peer = connection->peer;
+ ifindex_t ifindex = IFINDEX_INTERNAL;
+ const char *bound_name;
assert(!CHECK_FLAG(connection->thread_flags, PEER_THREAD_WRITES_ON));
assert(!CHECK_FLAG(connection->thread_flags, PEER_THREAD_READS_ON));
@@ -876,11 +913,15 @@ enum connect_result bgp_connect(struct peer_connection *connection)
return connect_error;
}
frr_with_privs(&bgpd_privs) {
+ bound_name = bgp_get_bound_name(connection, &ifindex);
+
/* Make socket for the peer. */
- connection->fd =
- vrf_sockunion_socket(&connection->su, peer->bgp->vrf_id,
- bgp_get_bound_name(connection));
+ connection->fd = vrf_sockunion_socket(&connection->su, peer->bgp->vrf_id,
+ bound_name);
connection->dir = CONNECTION_OUTGOING;
+ /* A valid fd means the bind, if any, succeeded. */
+ if (connection->fd >= 0)
+ connection->socket_binding_ifindex = ifindex;
}
if (connection->fd < 0) {
peer_set_last_reset(peer, PEER_DOWN_SOCKET_ERROR);
diff --git a/bgpd/bgp_zebra.c b/bgpd/bgp_zebra.c
index 3dfd508a62..6b68675892 100644
--- a/bgpd/bgp_zebra.c
+++ b/bgpd/bgp_zebra.c
@@ -210,10 +210,36 @@ static int bgp_ifp_destroy(struct interface *ifp)
* lib frees an unconfigured ifp once this hook returns. BGP views use
* the default VRF's interfaces without being its vrf->info.
*/
- for (ALL_LIST_ELEMENTS_RO(bm->bgp, bnode, nbgp))
- for (ALL_LIST_ELEMENTS_RO(nbgp->peer, node, peer))
+ for (ALL_LIST_ELEMENTS_RO(bm->bgp, bnode, nbgp)) {
+ bool same_netns = true;
+
+ switch (vrf_get_backend()) {
+ case VRF_BACKEND_NETNS:
+ /* Interface indexes are local to a network namespace. */
+ same_netns = nbgp->vrf_id == ifp->vrf->vrf_id;
+ break;
+ case VRF_BACKEND_VRF_LITE:
+ break;
+ }
+
+ for (ALL_LIST_ELEMENTS_RO(nbgp->peer, node, peer)) {
+ /*
+ * A socket bound to a device that is gone can never
+ * work again. Reset the session now instead of waiting
+ * for the hold timer. Sockets that are not bound to
+ * this device are left alone.
+ */
+ if (same_netns && peer->connection->fd >= 0 &&
+ ifp->ifindex != IFINDEX_INTERNAL &&
+ peer->connection->socket_binding_ifindex == ifp->ifindex) {
+ peer_set_last_reset(peer, PEER_DOWN_BOUND_IF_DEL);
+ BGP_EVENT_ADD(peer->connection, BGP_Stop);
+ }
+
if (peer->nexthop.ifp == ifp)
peer->nexthop.ifp = NULL;
+ }
+ }
if (bgp) {
bgp_update_interface_nbrs(bgp, ifp, NULL);
diff --git a/bgpd/bgpd.c b/bgpd/bgpd.c
index 61bd2f5598..95b23ea1b9 100644
--- a/bgpd/bgpd.c
+++ b/bgpd/bgpd.c
@@ -1398,6 +1398,7 @@ struct peer_connection *bgp_peer_connection_new(struct peer *peer, const union s
connection->peer = peer;
connection->fd = -1;
+ connection->socket_binding_ifindex = IFINDEX_INTERNAL;
connection->ibuf = stream_fifo_new();
connection->obuf = stream_fifo_new();
diff --git a/bgpd/bgpd.h b/bgpd/bgpd.h
index 46c56b33c5..ec3e667555 100644
--- a/bgpd/bgpd.h
+++ b/bgpd/bgpd.h
@@ -1563,6 +1563,11 @@ struct peer_connection {
int fd;
+ /* ifindex of the device the socket is bound to (SO_BINDTODEVICE), or
+ * IFINDEX_INTERNAL if the socket is not bound to an interface.
+ */
+ ifindex_t socket_binding_ifindex;
+
/* Thread flags */
_Atomic uint32_t thread_flags;
#define PEER_THREAD_WRITES_ON (1U << 0)
@@ -2277,6 +2282,7 @@ struct peer {
#define PEER_DOWN_CEASE_NO_RESOURCE 44U /* Out of resources */
#define PEER_DOWN_CEASE_HARD_RESET 45U /* Hard reset */
#define PEER_DOWN_CEASE_UNKNOWN 46U /* Subcode unknown */
+#define PEER_DOWN_BOUND_IF_DEL 47U /* Bound interface deleted */
/*
* Remember to update peer_down_str in bgp_fsm.c when you add