Commit 32ab5ef51f for frr
commit 32ab5ef51f0152677cdcbf6b6bfd9e56e0bb7190
Author: Rajesh Varatharaj <rvaratharaj@nvidia.com>
Date: Mon Jul 20 16:18:05 2026 -0700
lib, zebra: withdraw stale default router on link-local change
A host-facing port can come up with its physical MAC and later have a
different one applied, such as an anycast-gateway MAC. rtadv advertises
a default router sourced from the phys-MAC-derived link-local and the
host installs a default via it. When the new MAC is applied, the kernel
removes that link-local and generates a new one, but nothing withdraws
the default the host already learned via the old LL. The host is left
with two RA defaults -- the old LL and the new one -- and the old
next-hop no longer exists, so per-flow ECMP blackholes to it until the
advertisement ages out.
A host keys a default router on the RA source link-local (RFC 4861
6.3.4), so the stale default is withdrawn by advertising from that same
(now removed) LL with a Router Lifetime of zero. Per RFC 4861 6.2.5:
"the router SHOULD transmit one or more (but not more than
MAX_FINAL_RTR_ADVERTISEMENTS) final multicast Router
Advertisements on the interface with a Router Lifetime field
of zero."
so on link-local delete of an advertising interface, send up to
MAX_FINAL_RTR_ADVERTISEMENTS (3) final Router-Lifetime-0 RAs from the
removed LL. Non-advertising interfaces (loopbacks, non-RA links) are
skipped so no spurious RA is emitted.
The old LL is already gone from the kernel and the RA must egress the
originating interface, so the send uses a throwaway socket, opened in
the interface's network namespace, with IPV6_FREEBIND and
SO_BINDTODEVICE -- not the shared RA socket, which is left untouched so
normal RAs on other ports keep working.
The delete happens while the MAC is changing, i.e. while the link is
momentarily down, so the retraction cannot always be sent at delete
time. Stash the removed LL and flush it on the next advertisement once
the link is operative. Even then the send can land while the interface
is up but its transmit queue is not yet running, where the qdisc drops
it and userspace sees ENOBUFS. The finals are independent, so a failed
send does not stop the ones after it, and if none goes out the LL is
re-armed for the next RA unless the error is a programming one (EINVAL,
EBADF, EAFNOSUPPORT).
With this, after the MAC change the host converges to a single RA
default per uplink, where before it kept the stale default and per-flow
ECMP blackholed.
Signed-off-by: Rajesh Varatharaj <rvaratharaj@nvidia.com>
diff --git a/lib/sockopt.c b/lib/sockopt.c
index 3eda386163..189a524815 100644
--- a/lib/sockopt.c
+++ b/lib/sockopt.c
@@ -110,6 +110,32 @@ int setsockopt_ipv6_pktinfo(int sock, int val)
return ret;
}
+int setsockopt_ipv6_freebind(int sock, int val)
+{
+ int ret = 0;
+
+#ifdef IPV6_FREEBIND
+ ret = setsockopt(sock, IPPROTO_IPV6, IPV6_FREEBIND, &val, sizeof(val));
+ if (ret < 0)
+ flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_FREEBIND : %s",
+ safe_strerror(errno));
+#endif /* IPV6_FREEBIND */
+ return ret;
+}
+
+int setsockopt_so_bindtodevice(int sock, const char *ifname)
+{
+ int ret = 0;
+
+#ifdef SO_BINDTODEVICE
+ ret = setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname) + 1);
+ if (ret < 0)
+ flog_err(EC_LIB_SOCKET, "can't setsockopt SO_BINDTODEVICE : %s",
+ safe_strerror(errno));
+#endif /* SO_BINDTODEVICE */
+ return ret;
+}
+
/* Set multicast hops val to the socket. */
int setsockopt_ipv6_multicast_hops(int sock, int val)
{
diff --git a/lib/sockopt.h b/lib/sockopt.h
index ed1bd70da5..48618f3dbf 100644
--- a/lib/sockopt.h
+++ b/lib/sockopt.h
@@ -23,6 +23,8 @@ extern int setsockopt_ipv6_unicast_hops(int sock, int val);
extern int setsockopt_ipv6_hoplimit(int sock, int val);
extern int setsockopt_ipv6_multicast_loop(int sock, int val);
extern int setsockopt_ipv6_tclass(int sock, int tclass);
+extern int setsockopt_ipv6_freebind(int sock, int val);
+extern int setsockopt_so_bindtodevice(int sock, const char *ifname);
#define SOPT_SIZE_CMSG_PKTINFO_IPV6() (sizeof(struct in6_pktinfo));
diff --git a/zebra/connected.c b/zebra/connected.c
index dc537097ff..d022b26992 100644
--- a/zebra/connected.c
+++ b/zebra/connected.c
@@ -720,6 +720,9 @@ void connected_delete_ipv6(struct interface *ifp,
/* Delete global ipv6 address from RA prefix list */
if (!IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
rtadv_delete_prefix(ifp->info, &p);
+ else
+ /* LL removed (e.g. MAC change): retract the default from it. */
+ rtadv_retract_router(ifp->info, &p.u.prefix6);
if (dest) {
memset(&d, 0, sizeof(d));
diff --git a/zebra/rtadv.c b/zebra/rtadv.c
index 67764f4323..2d7b6b9e8f 100644
--- a/zebra/rtadv.c
+++ b/zebra/rtadv.c
@@ -262,6 +262,14 @@ static void rtadv_send_packet(int sock, struct interface *ifp,
/* Fetch interface information. */
zif = ifp->info;
+ /* Flush a deferred LL retraction (link is up now). */
+ if (!IN6_IS_ADDR_UNSPECIFIED(&zif->rtadv.retract_ll)) {
+ struct in6_addr ll = zif->rtadv.retract_ll;
+
+ memset(&zif->rtadv.retract_ll, 0, sizeof(ll));
+ rtadv_retract_router(zif, &ll);
+ }
+
/* Make router advertisement message. */
rtadv = (struct nd_router_advert *)buf;
@@ -549,6 +557,121 @@ no_more_opts:
zif->ra_sent++;
}
+/* RFC 4861 6.2.5: send up to MAX_FINAL_RTR_ADVERTISEMENTS (3) final RAs. */
+#define RTADV_MAX_FINAL_RTR_ADVERTS 3
+
+/*
+ * Withdraw the default router we advertised from a just-removed link-local
+ * (e.g. MAC changed to the anycast gateway): send final Router-Lifetime-0 RAs
+ * sourced from the old LL (RFC 4861 6.2.5). The LL is already gone, so the send
+ * needs FREEBIND + a bind to the egress interface, done on a throwaway socket
+ * so it cannot disturb the shared RA socket / normal RAs on other ports.
+ */
+void rtadv_retract_router(struct zebra_if *zif, const struct in6_addr *lladdr)
+{
+ struct interface *ifp = zif->ifp;
+ struct nd_router_advert ra = {};
+ struct sockaddr_in6 addr = {};
+ struct in6_pktinfo *pkt;
+ struct msghdr msg = {};
+ struct cmsghdr *cmsgptr;
+ struct iovec iov;
+ char adata[RTADV_ADATA_SIZE] = { 0 };
+ uint8_t all_nodes_addr[] = { 0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
+ struct zebra_vrf *zvrf;
+ int sock = -1;
+ int err = 0;
+ int txerr = 0;
+ int sent = 0;
+ int i;
+
+ /* Only interfaces advertising a router send RAs (RFC 4861 6). */
+ if (!zif->rtadv.AdvSendAdvertisements)
+ return;
+
+ /* Link down (mid MAC-flip): defer to the next RA once it is up. */
+ if (!if_is_operative(ifp)) {
+ zif->rtadv.retract_ll = *lladdr;
+ return;
+ }
+
+ zvrf = rtadv_interface_get_zvrf(ifp);
+ if (!zvrf)
+ return;
+
+ /* Throwaway socket: keep FREEBIND + bind off the shared RA socket. */
+ frr_with_privs (&zserv_privs) {
+ sock = ns_socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6, zvrf->zns->ns_id);
+ err = errno;
+ if (sock >= 0) {
+ setsockopt_ipv6_multicast_hops(sock, 255);
+ setsockopt_ipv6_multicast_loop(sock, 0);
+ setsockopt_ipv6_freebind(sock, 1);
+ setsockopt_so_bindtodevice(sock, ifp->name);
+ }
+ }
+ if (sock < 0) {
+ flog_err_sys(EC_LIB_SOCKET, "%s(%u): retract RA socket: %s", ifp->name,
+ ifp->ifindex, safe_strerror(err));
+ return;
+ }
+
+ ra.nd_ra_type = ND_ROUTER_ADVERT;
+ ra.nd_ra_curhoplimit = zif->rtadv.AdvCurHopLimit;
+ ra.nd_ra_router_lifetime = htons(0);
+
+ addr.sin6_family = AF_INET6;
+ addr.sin6_port = htons(IPPROTO_ICMPV6);
+ IPV6_ADDR_COPY(&addr.sin6_addr, all_nodes_addr);
+
+ iov.iov_base = &ra;
+ iov.iov_len = sizeof(ra);
+ msg.msg_name = &addr;
+ msg.msg_namelen = sizeof(addr);
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+ msg.msg_control = adata;
+ msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
+
+ cmsgptr = CMSG_FIRSTHDR(&msg);
+ cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
+ cmsgptr->cmsg_level = IPPROTO_IPV6;
+ cmsgptr->cmsg_type = IPV6_PKTINFO;
+ pkt = (struct in6_pktinfo *)CMSG_DATA(cmsgptr);
+ IPV6_ADDR_COPY(&pkt->ipi6_addr, lladdr); /* source = the removed LL */
+ pkt->ipi6_ifindex = ifp->ifindex;
+
+ /*
+ * The three finals are independent (RFC 4861 6.2.5), so a failed send
+ * is no reason to skip the ones after it.
+ */
+ for (i = 0; i < RTADV_MAX_FINAL_RTR_ADVERTS; i++) {
+ if (sendmsg(sock, &msg, 0) < 0)
+ txerr = errno;
+ else
+ sent++;
+ }
+ if (sent == 0) {
+ /*
+ * Retry anything transient. Abandoning a retraction is only
+ * right for a programming error, and that set is closed.
+ */
+ if (txerr != EINVAL && txerr != EBADF && txerr != EAFNOSUPPORT) {
+ zif->rtadv.retract_ll = *lladdr;
+ if (IS_ZEBRA_DEBUG_PACKET)
+ zlog_debug("%s(%u): retract RA deferred (%s), will retry",
+ ifp->name, ifp->ifindex, safe_strerror(txerr));
+ } else
+ flog_err_sys(EC_LIB_SOCKET,
+ "%s(%u): Tx retract RA (lifetime 0) src %pI6 failed: %s",
+ ifp->name, ifp->ifindex, lladdr, safe_strerror(txerr));
+ } else if (IS_ZEBRA_DEBUG_PACKET)
+ zlog_debug("%s(%u): Tx %d retract RA (lifetime 0) src %pI6", ifp->name,
+ ifp->ifindex, sent, lladdr);
+
+ close(sock);
+}
+
static void start_icmpv6_join_timer(struct event *event)
{
struct interface *ifp = EVENT_ARG(event);
diff --git a/zebra/rtadv.h b/zebra/rtadv.h
index c5795b7752..3e08d553c4 100644
--- a/zebra/rtadv.h
+++ b/zebra/rtadv.h
@@ -212,6 +212,9 @@ struct rtadvconf {
#define RTADV_FAST_REXMIT_PERIOD 1 /* 1 sec */
#define RTADV_NUM_FAST_REXMITS 4 /* Fast Rexmit RA 4 times on certain events \
*/
+
+ /* LL pending default-router withdrawal (removed while link was down). */
+ struct in6_addr retract_ll;
};
struct rtadv_rdnss {
@@ -410,6 +413,7 @@ extern void rtadv_if_up(struct zebra_if *zif);
extern void rtadv_if_fini(struct zebra_if *zif);
extern void rtadv_add_prefix(struct zebra_if *zif, const struct prefix_ipv6 *p);
extern void rtadv_delete_prefix(struct zebra_if *zif, const struct prefix *p);
+extern void rtadv_retract_router(struct zebra_if *zif, const struct in6_addr *lladdr);
/* returns created prefix */
struct rtadv_prefix *rtadv_add_prefix_manual(struct zebra_if *zif,