Commit 7574247e59 for frr

commit 7574247e598194774d66bb0604d4e40eef071b48
Author: Rajesh Varatharaj <rvaratharaj@nvidia.com>
Date:   Fri Aug 7 09:24:06 2026 -0700

    zebra: add clear ipv6 nd prefix command

    FRR advertises SLAAC prefixes with the RFC defaults, that is
    RTADV_VALID_LIFETIME 2592000 and RTADV_PREFERRED_LIFETIME 604800. A host
    re-cabled to a different port can be left holding a prefix that is wrong
    for its new location, and there is no way to retire that prefix short of
    waiting out the 30 day valid lifetime.

    Two gaps made this unrecoverable. rtadv_prefix_reset() sends exactly one
    deprecating RA when a prefix is deleted, and RA is unacknowledged
    multicast, so a host that misses that packet keeps the address for the
    full lifetime. Separately there was no way to deprecate a prefix on a
    link that does not currently advertise it, which is precisely the
    re-cabling case: the host sits on a new port while still carrying the
    old port's prefix, so the RA that retires it has to go out on the new
    port.

    Add a per-interface flush list. Entries hold the prefix by value rather
    than as a struct rtadv_prefix pointer, so an entry survives the prefix
    being freed and recreated underneath it, which frr-reload does on any
    lifetime change and the delete path does outright. rtadv_send_packet()
    puts a prefix option with the flush lifetimes in for every entry, and
    leaves those prefixes out of the advertised list that follows. A prefix
    the interface advertises keeps its own flags; one it does not gets
    on-link and autonomous. The configured lifetimes are never modified, so
    there is nothing to restore afterwards and no race against a concurrent
    reload.

    The flush options go in ahead of the advertised prefixes, so an
    interface with enough prefixes to fill the packet cannot crowd one out.
    The prefix, source link-layer address and MTU options that follow are
    now checked against max_len, as RDNSS and DNSSL already were.

    A flush queued by the delete path is dropped when the prefix is added
    back, and fast retransmit then sends the real lifetimes, so a reload
    that deletes and re-adds a prefix does not leave hosts deprecating it.
    A flush started by the clear command keeps running.

    A private timer paces the burst at MIN_DELAY_BETWEEN_RAS, the RFC 4861
    6.2.6 floor for consecutive multicast RAs. The first RA is sent inline,
    so a burst of N spans (N - 1) periods: three seconds at the default
    count of two, six for the three sent on prefix deletion. A prefix queued
    while a burst is already running joins it on the next tick instead of
    sending inline, so it does not use up the other prefixes' RAs early.
    Only the burst's own RAs count against it. Periodic, fast-retransmit
    and solicited RAs sent in the meantime carry the same zero lifetimes,
    but counting them would let them use the burst up well inside the
    pacing. The existing fast-retransmit counter is deliberately not
    reused: it is shared with link up and RA enable, only two of the
    transmit paths are accounted through it, and it is skipped entirely
    when fast retransmit is disabled.

    Two related gaps are left alone: the inline first RA can still fall
    less than three seconds after a periodic one, and the fast-retransmit
    path continues to run at one second. Closing either properly needs the
    interface to record when it last sent a multicast RA.

    rtadv_prefix_reset() now queues RTADV_MAX_FINAL_RTR_ADVERTS RAs instead
    of sending one, which makes prefix removal reliable as well.

    The command is:

      clear ipv6 nd prefix X:X::X:X/M interface IFNAME [vrf NAME]
          [count (1-3)] [valid-lifetime (0-4294967295)]
          [preferred-lifetime (0-4294967295)]

    It changes no configuration. Note that a host does not drop the address
    straight away: RFC 4862 5.5.3 floors the stored valid lifetime at two
    hours, so the address is deprecated at once and removed after that
    window. That behaviour is expected.

    Signed-off-by: Rajesh Varatharaj <rvaratharaj@nvidia.com>

diff --git a/doc/user/ipv6.rst b/doc/user/ipv6.rst
index 3f881132ff..7a76486149 100644
--- a/doc/user/ipv6.rst
+++ b/doc/user/ipv6.rst
@@ -22,6 +22,37 @@ Router Advertisement
    Show configured route advertisement interfaces. VRF subcommand only
    applicable for netns-based vrfs.

+.. clicmd:: clear ipv6 nd prefix X:X::X:X/M interface IFNAME [vrf NAME] [count (1-3)] [valid-lifetime (0-4294967295)] [preferred-lifetime (0-4294967295)]
+
+   Send a short burst of router advertisements on the interface that carry the
+   prefix with the given lifetimes, so that hosts stop using addresses they
+   formed from it instead of waiting out the valid lifetime advertised earlier,
+   30 days by default. The configuration is not changed, so a prefix the
+   interface goes on advertising is announced with its configured lifetimes
+   again once the burst is over.
+
+   The prefix does not have to be one the interface advertises. A host that has
+   been moved to another port can still hold the prefix of the port it came
+   from, and the advertisement that retires it has to go out on the port the
+   host is now attached to. For such a prefix only a ``valid-lifetime`` of
+   ``0`` is accepted.
+
+   - ``count``: the number of advertisements to send, spaced 3 seconds apart
+     as RFC 4861 section 6.2.6 requires. Default: ``2``
+
+   - ``valid-lifetime`` and ``preferred-lifetime``: the lifetimes to advertise.
+     The preferred lifetime may not exceed the valid lifetime, since RFC 4861
+     section 4.6.2 has hosts discard such an option. Default: ``0``
+
+   The interface must be operationally up with router advertisements enabled,
+   and at most 16 prefixes can be in flight on an interface at a time. The
+   ``vrf`` keyword is only accepted with netns-based VRFs. While the burst is
+   in progress, ``show interface`` lists the prefix being flushed.
+
+   Note that RFC 4862 section 5.5.3 does not let a host lower the stored valid
+   lifetime of an address below two hours, so the address is deprecated at once
+   but only removed after that window.
+
 .. clicmd:: ipv6 nd suppress-ra

    Don't send router advertisement messages. The ``no`` form of this command
diff --git a/zebra/rtadv.c b/zebra/rtadv.c
index 2d7b6b9e8f..f6e7382ba5 100644
--- a/zebra/rtadv.c
+++ b/zebra/rtadv.c
@@ -223,9 +223,74 @@ static int rtadv_recv_packet(struct zebra_vrf *zvrf, int sock, uint8_t *buf,

 #define RTADV_MSG_SIZE 4096

-/* Send router advertisement packet. */
-static void rtadv_send_packet(int sock, struct interface *ifp,
-			      enum ipv6_nd_suppress_ra_status stop)
+/*
+ * Find the flush entry for a prefix, or NULL.  prefix_cmp() masks both sides,
+ * so a caller need not pre-mask the lookup key.
+ */
+static struct rtadv_flush *rtadv_flush_find(struct zebra_if *zif, const struct prefix_ipv6 *p)
+{
+	uint8_t i;
+
+	for (i = 0; i < zif->rtadv.flush_count; i++)
+		if (prefix_cmp(&zif->rtadv.flush[i].prefix, p) == 0)
+			return &zif->rtadv.flush[i];
+
+	return NULL;
+}
+
+/*
+ * One bit per flush slot, so the cap cannot grow past the width of the
+ * bitmask without the extra entries being silently dropped from it.
+ */
+_Static_assert(RTADV_MAX_FLUSH_PREFIXES <= 16,
+	       "flush bitmask is uint16_t; widen it before raising the cap");
+
+/*
+ * Charge one of a burst's RAs against the flush entries whose option it
+ * carried, then drop the ones that are finished.  A failed send reports none,
+ * so a transient sendmsg() failure cannot burn a prefix's remaining RAs.
+ */
+static void rtadv_flush_consume(struct zebra_if *zif, uint16_t sent)
+{
+	uint8_t i, keep = 0;
+
+	for (i = 0; i < zif->rtadv.flush_count; i++)
+		if ((sent & (1U << i)) && zif->rtadv.flush[i].remaining)
+			zif->rtadv.flush[i].remaining--;
+
+	/* Compact in a second pass: indices in @sent must stay valid above. */
+	for (i = 0; i < zif->rtadv.flush_count; i++) {
+		if (!zif->rtadv.flush[i].remaining)
+			continue;
+		if (keep != i)
+			zif->rtadv.flush[keep] = zif->rtadv.flush[i];
+		keep++;
+	}
+
+	zif->rtadv.flush_count = keep;
+}
+
+/* The L, A and R bits of a prefix's information option. */
+static uint8_t rtadv_prefix_flags(const struct rtadv_prefix *rprefix)
+{
+	uint8_t flags = 0;
+
+	if (rprefix->AdvOnLinkFlag)
+		flags |= ND_OPT_PI_FLAG_ONLINK;
+	if (rprefix->AdvAutonomousFlag)
+		flags |= ND_OPT_PI_FLAG_AUTO;
+	if (rprefix->AdvRouterAddressFlag)
+		flags |= ND_OPT_PI_FLAG_RADDR;
+
+	return flags;
+}
+
+/*
+ * Send router advertisement packet.  Returns the flush entries whose option
+ * went out in it, none if the send failed.
+ */
+static uint16_t rtadv_send_packet(int sock, struct interface *ifp,
+				  enum ipv6_nd_suppress_ra_status stop)
 {
 	struct msghdr msg = { 0 };
 	struct iovec iov = { 0 };
@@ -244,6 +309,9 @@ static void rtadv_send_packet(int sock, struct interface *ifp,
 				    0,    0,    0, 0, 0, 0, 0, 1};
 	struct listnode *node;
 	uint16_t pkt_RouterLifetime;
+	/* Which flush entries got a prefix option into this packet. */
+	uint16_t flush_done = 0;
+	uint8_t fidx;

 	/* Logging of packet. */
 	if (IS_ZEBRA_DEBUG_PACKET)
@@ -356,24 +424,96 @@ static void rtadv_send_packet(int sock, struct interface *ifp,
 		len += sizeof(struct nd_opt_adv_interval);
 	}

+	/*
+	 * There is no limit on the number of configurable recursive DNS
+	 * servers or search list entries, and flushed prefixes add options of
+	 * their own. We don't want the RA message to exceed the link's MTU
+	 * (risking fragmentation) or even blow the stack buffer allocated for
+	 * it.
+	 */
+	size_t max_len = MIN(ifp->mtu6 - 40, sizeof(buf));
+
+	/*
+	 * Every pending flush goes in ahead of the advertised prefixes.  Behind
+	 * them, an interface carrying enough prefixes to fill the packet would
+	 * leave no room under max_len for some of the flush options, which is
+	 * exactly the case the flush exists to handle.  The prefix loop below
+	 * skips the ones that went in here.
+	 *
+	 * A flushed prefix this interface advertises keeps its own flags.  One
+	 * it does not advertise is the mis-cabling case: a host moved to a new
+	 * port still holds the old port's prefix, so the RA that retires it has
+	 * to go out here, on the link the host is actually attached to.  That
+	 * option needs the Autonomous flag or hosts ignore it for address
+	 * autoconfiguration and the deprecation accomplishes nothing.
+	 *
+	 * The flush lifetimes go on the wire without touching the configured
+	 * values, so the config stays authoritative and there is nothing to
+	 * restore afterwards.
+	 */
+	for (fidx = 0; fidx < zif->rtadv.flush_count; fidx++) {
+		struct rtadv_flush *flush = &zif->rtadv.flush[fidx];
+		struct rtadv_prefix ref = {};
+		struct nd_opt_prefix_info *pinfo;
+
+		if (len + sizeof(struct nd_opt_prefix_info) > max_len) {
+			zlog_err("%s(%s:%u): Tx RA: no room for flush of %pFX, prefix not retired",
+				 ifp->name, ifp->vrf->name, ifp->ifindex,
+				 (const struct prefix *)&flush->prefix);
+			goto no_more_opts;
+		}
+
+		ref.prefix = flush->prefix;
+		rprefix = rtadv_prefixes_find(zif->rtadv.prefixes, &ref);
+
+		pinfo = (struct nd_opt_prefix_info *)(buf + len);
+
+		pinfo->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
+		pinfo->nd_opt_pi_len = 4;
+		pinfo->nd_opt_pi_prefix_len = flush->prefix.prefixlen;
+		pinfo->nd_opt_pi_flags_reserved = ND_OPT_PI_FLAG_ONLINK | ND_OPT_PI_FLAG_AUTO;
+		if (rprefix)
+			pinfo->nd_opt_pi_flags_reserved = rtadv_prefix_flags(rprefix);
+		pinfo->nd_opt_pi_valid_time = htonl(flush->AdvValidLifetime);
+		pinfo->nd_opt_pi_preferred_time = htonl(flush->AdvPreferredLifetime);
+		pinfo->nd_opt_pi_reserved2 = 0;
+
+		IPV6_ADDR_COPY(&pinfo->nd_opt_pi_prefix, &flush->prefix.prefix);
+
+		len += sizeof(struct nd_opt_prefix_info);
+		flush_done |= 1U << fidx;
+
+		if (IS_ZEBRA_DEBUG_SEND)
+			zlog_debug("%s(%s:%u): Tx RA: flushing %s %pFX, valid %u preferred %u",
+				   ifp->name, ifp->vrf->name, ifp->ifindex,
+				   rprefix ? "advertised" : "unadvertised",
+				   (const struct prefix *)&flush->prefix, flush->AdvValidLifetime,
+				   flush->AdvPreferredLifetime);
+	}
+
 	/* Fill in prefix. */
 	frr_each (rtadv_prefixes, zif->rtadv.prefixes, rprefix) {
 		struct nd_opt_prefix_info *pinfo;

+		/* Already went in above, with the flush lifetimes. */
+		if (rtadv_flush_find(zif, &rprefix->prefix))
+			continue;
+
+		/* Flush options go in first, so the prefixes can run out of room too. */
+		if (len + sizeof(struct nd_opt_prefix_info) > max_len) {
+			zlog_warn("%s(%s:%u): Tx RA: prefix %pFX would exceed MTU, omitting it and the rest",
+				  ifp->name, ifp->vrf->name, ifp->ifindex,
+				  (const struct prefix *)&rprefix->prefix);
+			goto no_more_opts;
+		}
+
 		pinfo = (struct nd_opt_prefix_info *)(buf + len);

 		pinfo->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
 		pinfo->nd_opt_pi_len = 4;
 		pinfo->nd_opt_pi_prefix_len = rprefix->prefix.prefixlen;

-		pinfo->nd_opt_pi_flags_reserved = 0;
-		if (rprefix->AdvOnLinkFlag)
-			pinfo->nd_opt_pi_flags_reserved |=
-				ND_OPT_PI_FLAG_ONLINK;
-		if (rprefix->AdvAutonomousFlag)
-			pinfo->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
-		if (rprefix->AdvRouterAddressFlag)
-			pinfo->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_RADDR;
+		pinfo->nd_opt_pi_flags_reserved = rtadv_prefix_flags(rprefix);

 		pinfo->nd_opt_pi_valid_time = htonl(rprefix->AdvValidLifetime);
 		pinfo->nd_opt_pi_preferred_time =
@@ -388,6 +528,14 @@ static void rtadv_send_packet(int sock, struct interface *ifp,

 	/* Hardware address. */
 	if (ifp->hw_addr_len != 0) {
+		size_t opt_len = ((ifp->hw_addr_len + 9) >> 3) << 3;
+
+		if (len + opt_len > max_len) {
+			zlog_warn("%s(%s:%u): Tx RA: link-layer address option would exceed MTU, omitting it",
+				  ifp->name, ifp->vrf->name, ifp->ifindex);
+			goto no_more_opts;
+		}
+
 		buf[len++] = ND_OPT_SOURCE_LINKADDR;

 		/* Option length should be rounded up to next octet if
@@ -405,6 +553,13 @@ static void rtadv_send_packet(int sock, struct interface *ifp,
 	/* MTU */
 	if (zif->rtadv.AdvLinkMTU) {
 		struct nd_opt_mtu *opt = (struct nd_opt_mtu *)(buf + len);
+
+		if (len + sizeof(*opt) > max_len) {
+			zlog_warn("%s(%s:%u): Tx RA: MTU option would exceed MTU, omitting it",
+				  ifp->name, ifp->vrf->name, ifp->ifindex);
+			goto no_more_opts;
+		}
+
 		opt->nd_opt_mtu_type = ND_OPT_MTU;
 		opt->nd_opt_mtu_len = 1;
 		opt->nd_opt_mtu_reserved = 0;
@@ -412,14 +567,6 @@ static void rtadv_send_packet(int sock, struct interface *ifp,
 		len += sizeof(struct nd_opt_mtu);
 	}

-	/*
-	 * There is no limit on the number of configurable recursive DNS
-	 * servers or search list entries. We don't want the RA message
-	 * to exceed the link's MTU (risking fragmentation) or even
-	 * blow the stack buffer allocated for it.
-	 */
-	size_t max_len = MIN(ifp->mtu6 - 40, sizeof(buf));
-
 	/* Recursive DNS servers */
 	struct rtadv_rdnss *rdnss;

@@ -555,6 +702,171 @@ no_more_opts:
 			     safe_strerror(errno));
 	} else
 		zif->ra_sent++;
+
+	return ret < 0 ? 0 : flush_done;
+}
+
+static void rtadv_flush_timer(struct event *event);
+
+/*
+ * Schedule the next RA of a flush burst.  event_add_timer_msec() no-ops when
+ * the timer is already pending, so a burst that is running simply picks up any
+ * newly queued prefix on its next tick.
+ */
+static void rtadv_flush_arm(struct zebra_if *zif)
+{
+	event_add_timer_msec(zrouter.master, rtadv_flush_timer, zif->ifp, RTADV_FLUSH_PERIOD_MS,
+			     &zif->rtadv.flush_timer);
+}
+
+/* Emit one RA now, if the interface is in a state to carry it. */
+static void rtadv_flush_send_now(struct zebra_if *zif)
+{
+	struct zebra_vrf *zvrf;
+	uint16_t sent;
+
+	if (!if_is_operative(zif->ifp))
+		return;
+
+	zvrf = rtadv_interface_get_zvrf(zif->ifp);
+	if (!zvrf || zvrf->rtadv.sock < 0)
+		return;
+
+	/*
+	 * Never put a live Router Lifetime on a link whose RAs the operator
+	 * has turned off.  RA_SUPPRESS still carries the prefix options that
+	 * do the deprecating, with Router Lifetime 0.
+	 */
+	sent = rtadv_send_packet(zvrf->rtadv.sock, zif->ifp,
+				 zif->rtadv.AdvSendAdvertisements ? RA_ENABLE : RA_SUPPRESS);
+
+	/*
+	 * Only the burst's own RAs count against it.  Periodic, fast-retransmit
+	 * and solicited RAs carry the flush options as well, but charging them
+	 * would let them use the burst up well inside the pacing.
+	 */
+	rtadv_flush_consume(zif, sent);
+}
+
+static void rtadv_flush_timer(struct event *event)
+{
+	struct interface *ifp = EVENT_ARG(event);
+	struct zebra_if *zif = ifp->info;
+	time_t now = monotime(NULL);
+	uint8_t i, keep = 0;
+
+	if (!zif)
+		return;
+
+	/*
+	 * Drop entries past their backstop.  An entry is only charged once its
+	 * prefix option reaches the wire, so one that never fits -- the RA is
+	 * already at the MTU/buffer limit -- or whose sends keep failing would
+	 * hold its slot indefinitely and emit stale zero-lifetime RAs if the
+	 * prefix were later re-added.  Interface down and RA suppression clear
+	 * the queue outright, so they do not rely on this.
+	 */
+	for (i = 0; i < zif->rtadv.flush_count; i++) {
+		struct rtadv_flush *flush = &zif->rtadv.flush[i];
+
+		if (flush->deadline <= now) {
+			zlog_warn("%s(%s:%u): flush of %pFX expired with %u RA(s) unsent",
+				  ifp->name, ifp->vrf->name, ifp->ifindex,
+				  (const struct prefix *)&flush->prefix, flush->remaining);
+			continue;
+		}
+
+		if (keep != i)
+			zif->rtadv.flush[keep] = *flush;
+		keep++;
+	}
+	zif->rtadv.flush_count = keep;
+
+	if (!zif->rtadv.flush_count)
+		return;
+
+	rtadv_flush_send_now(zif);
+
+	/* The send above may have retired the last entry. */
+	if (zif->rtadv.flush_count)
+		rtadv_flush_arm(zif);
+}
+
+/*
+ * Queue a prefix to be flushed and start the burst.  Re-queueing a prefix that
+ * is already in flight refreshes it instead of adding a duplicate, so repeated
+ * invocations are idempotent.
+ *
+ * Returns false when this interface already has RTADV_MAX_FLUSH_PREFIXES in
+ * flight.
+ */
+static bool rtadv_flush_enqueue(struct zebra_if *zif, const struct prefix_ipv6 *p, uint32_t valid,
+				uint32_t preferred, uint8_t count, bool on_delete)
+{
+	struct rtadv_flush *flush;
+
+	flush = rtadv_flush_find(zif, p);
+	if (!flush) {
+		if (zif->rtadv.flush_count >= RTADV_MAX_FLUSH_PREFIXES)
+			return false;
+
+		flush = &zif->rtadv.flush[zif->rtadv.flush_count++];
+		flush->prefix = *p;
+		/*
+		 * str2prefix_ipv6() does not mask host bits and the option is
+		 * copied verbatim onto the wire, so mask here.
+		 */
+		apply_mask_ipv6(&flush->prefix);
+		flush->on_delete = true;
+	}
+
+	/* Once the operator has cleared it, keep going even if the prefix comes back. */
+	if (!on_delete)
+		flush->on_delete = false;
+
+	flush->AdvValidLifetime = valid;
+	flush->AdvPreferredLifetime = preferred;
+	flush->remaining = count;
+	flush->deadline = monotime(NULL) + RTADV_FLUSH_DEADLINE_SECS;
+
+	/* Send the first RA now, unless a running burst will pick this one up. */
+	if (!event_is_scheduled(zif->rtadv.flush_timer))
+		rtadv_flush_send_now(zif);
+
+	if (zif->rtadv.flush_count)
+		rtadv_flush_arm(zif);
+
+	return true;
+}
+
+/* Abandon any in-flight flush on this interface. */
+static void rtadv_flush_clear(struct zebra_if *zif)
+{
+	event_cancel(&zif->rtadv.flush_timer);
+	zif->rtadv.flush_count = 0;
+}
+
+/* Stop deprecating a removed prefix that has been added back. */
+static void rtadv_flush_cancel(struct zebra_if *zif, const struct prefix_ipv6 *p)
+{
+	struct rtadv_flush *flush = rtadv_flush_find(zif, p);
+	uint8_t i;
+
+	if (!flush || !flush->on_delete)
+		return;
+
+	i = flush - zif->rtadv.flush;
+	zif->rtadv.flush_count--;
+	memmove(flush, flush + 1, (zif->rtadv.flush_count - i) * sizeof(*flush));
+	if (!zif->rtadv.flush_count)
+		event_cancel(&zif->rtadv.flush_timer);
+
+	/* Hosts may already have seen zero lifetimes for it, so resend the real ones. */
+	if (zif->rtadv.AdvSendAdvertisements && zif->rtadv.MaxRtrAdvInterval >= 1000 &&
+	    zif->rtadv.UseFastRexmit) {
+		zif->rtadv.inFastRexmit = 1;
+		zif->rtadv.NumFastReXmitsRemain = RTADV_NUM_FAST_REXMITS;
+	}
 }

 /* RFC 4861 6.2.5: send up to MAX_FINAL_RTR_ADVERTISEMENTS (3) final RAs. */
@@ -1441,6 +1753,10 @@ static struct rtadv_prefix *rtadv_prefix_set(struct zebra_if *zif,
 {
 	struct rtadv_prefix *rprefix;

+	/* A prefix added back after removal must not keep being deprecated. */
+	if (!rtadv_prefixes_find(zif->rtadv.prefixes, rp))
+		rtadv_flush_cancel(zif, &rp->prefix);
+
 	rprefix = rtadv_prefix_get(zif->rtadv.prefixes, &rp->prefix);

 	/*
@@ -1510,22 +1826,29 @@ static void rtadv_prefix_reset(struct zebra_if *zif, struct rtadv_prefix *rp,
 		}

 		/*
-		 * Best-effort: before dropping a SLAAC prefix, tell hosts to
-		 * stop using it by advertising it once with preferred/valid
-		 * lifetime 0 (deprecate).  Skipped on global RA cease, which
-		 * already signals router-lifetime 0 per RFC 4861 6.2.5.
+		 * Before dropping a SLAAC prefix, tell hosts to stop using it:
+		 * re-announce it with zero lifetimes for up to
+		 * MAX_FINAL_RTR_ADVERTISEMENTS RAs (RFC 4861 6.2.5).  RA is
+		 * unacknowledged multicast, so one packet is not enough -- a
+		 * host that misses it keeps the address for the whole
+		 * AdvValidLifetime, 30 days by default.
+		 *
+		 * The flush entry holds the prefix by value, so the burst
+		 * carries on via the synthetic prefix-option path once the
+		 * entry below has been freed.
+		 *
+		 * Only on a link that is advertising and operationally up,
+		 * since nothing else can carry the burst.  Skipped on global
+		 * RA cease (deprecate=false), which already signals
+		 * router-lifetime 0 per RFC 4861 6.2.5.
 		 */
-		if (deprecate && zif->rtadv.AdvSendAdvertisements &&
-		    rprefix->AdvAutonomousFlag) {
-			struct zebra_vrf *zvrf =
-				rtadv_interface_get_zvrf(zif->ifp);
-
-			rprefix->AdvPreferredLifetime = 0;
-			rprefix->AdvValidLifetime = 0;
-			if (zvrf)
-				rtadv_send_packet(zvrf->rtadv.sock, zif->ifp,
-						  RA_ENABLE);
-		}
+		if (deprecate && zif->rtadv.AdvSendAdvertisements && if_is_operative(zif->ifp) &&
+		    rprefix->AdvAutonomousFlag &&
+		    !rtadv_flush_enqueue(zif, &rprefix->prefix, 0, 0, RTADV_MAX_FINAL_RTR_ADVERTS,
+					 true))
+			zlog_warn("%s(%s:%u): no room to deprecate %pFX before removal",
+				  zif->ifp->name, zif->ifp->vrf->name, zif->ifp->ifindex,
+				  (const struct prefix *)&rprefix->prefix);

 		rtadv_prefixes_del(zif->rtadv.prefixes, rprefix);
 		rtadv_prefix_free(rprefix);
@@ -1635,6 +1958,13 @@ void ipv6_nd_suppress_ra_set(struct interface *ifp,
 			/* Try to delete from the ra wheel */
 			wheel_remove_item(zrouter.ra_wheel, ifp);
 			rtadv_send_packet(zvrf->rtadv.sock, ifp, RA_SUPPRESS);
+			/*
+			 * Cleared after the suppress RA, which still carries
+			 * prefix options and so can do the deprecating for us.
+			 * Nothing further goes out on this interface, so an
+			 * in-flight flush cannot make progress from here.
+			 */
+			rtadv_flush_clear(zif);
 			zif->rtadv.AdvSendAdvertisements = 0;
 			zif->rtadv.AdvIntervalTimer = 0;

@@ -1802,12 +2132,23 @@ void rtadv_stop_ra(struct interface *ifp, bool if_down_event)
 	event_cancel(&zif->icmpv6_join_timer);

 	if (if_down_event) {
+		/* No further RA can go out, so abandon any in-flight flush. */
+		rtadv_flush_clear(zif);
 		/* Nothing to do more, return */
 		return;
 	}

 	if (zif->rtadv.AdvSendAdvertisements)
 		rtadv_send_packet(zvrf->rtadv.sock, ifp, RA_SUPPRESS);
+
+	/*
+	 * Cleared after the send, not before.  The suppress RA is the last
+	 * packet this link will see and it still carries prefix options, so it
+	 * is the ideal vehicle for a zero-lifetime one.  Clearing first would
+	 * have it re-advertise the configured 30-day lifetime and undo the
+	 * flush we just performed.
+	 */
+	rtadv_flush_clear(zif);
 }

 /*
@@ -2050,6 +2391,7 @@ static int nd_dump_vty(struct vty *vty, json_object *json_if, struct interface *
 	struct zebra_if *zif;
 	struct rtadvconf *rtadv;
 	int interval;
+	uint8_t fidx;

 	zif = (struct zebra_if *)ifp->info;
 	rtadv = &zif->rtadv;
@@ -2113,6 +2455,19 @@ static int nd_dump_vty(struct vty *vty, json_object *json_if, struct interface *
 				"  ND router advertisements with Adv. Interval option.\n");
 	}

+	/*
+	 * Without this, the configured lifetimes shown above would contradict
+	 * what is actually on the wire while a flush is in flight.
+	 */
+	if (!json_if)
+		for (fidx = 0; fidx < rtadv->flush_count; fidx++)
+			vty_out(vty,
+				"  ND flushing %pFX with valid %u preferred %u, %u RA(s) left\n",
+				(const struct prefix *)&rtadv->flush[fidx].prefix,
+				rtadv->flush[fidx].AdvValidLifetime,
+				rtadv->flush[fidx].AdvPreferredLifetime,
+				rtadv->flush[fidx].remaining);
+
 	if (json_if && rtadv->AdvSendAdvertisements) {
 		json_object_int_add(json_if, "ndAdvertisedReachableTimeMsecs",
 				    rtadv->AdvReachableTime);
@@ -2170,6 +2525,28 @@ static int nd_dump_vty(struct vty *vty, json_object *json_if, struct interface *
 						"ndRouterAdvertisementsWithAdvIntervalOption", true);
 	}

+	if (json_if && rtadv->flush_count) {
+		json_object *json_flushes;
+
+		json_flushes = json_object_new_array();
+		json_object_object_add(json_if, "ndFlush", json_flushes);
+
+		for (fidx = 0; fidx < rtadv->flush_count; fidx++) {
+			json_object *json_flush = json_object_new_object();
+			char buf[PREFIX_STRLEN];
+
+			prefix2str(&rtadv->flush[fidx].prefix, buf, sizeof(buf));
+			json_object_string_add(json_flush, "prefix", buf);
+			json_object_int_add(json_flush, "validLifetime",
+					    rtadv->flush[fidx].AdvValidLifetime);
+			json_object_int_add(json_flush, "preferredLifetime",
+					    rtadv->flush[fidx].AdvPreferredLifetime);
+			json_object_int_add(json_flush, "raRemaining",
+					    rtadv->flush[fidx].remaining);
+			json_object_array_add(json_flushes, json_flush);
+		}
+	}
+
 	return 0;
 }

@@ -2283,6 +2660,13 @@ void rtadv_if_fini(struct zebra_if *zif)

 	rtadv = &zif->rtadv;

+	/*
+	 * Must come first: the flush timer holds this interface as its argument
+	 * and rtadv_if_fini() runs from the interface-delete hook, just before
+	 * the interface is freed.
+	 */
+	rtadv_flush_clear(zif);
+
 	while ((rp = rtadv_prefixes_pop(rtadv->prefixes)))
 		rtadv_prefix_free(rp);

@@ -2316,6 +2700,150 @@ void rtadv_vrf_terminate(struct zebra_vrf *zvrf)
 	adv_msec_if_clean(zvrf);
 }

+DEFPY(clear_ipv6_nd_prefix, clear_ipv6_nd_prefix_cmd,
+      "clear ipv6 nd prefix X:X::X:X/M$prefix interface IFNAME$ifname [vrf NAME$vrf_name] [count (1-3)$count] [valid-lifetime (0-4294967295)$valid_lifetime] [preferred-lifetime (0-4294967295)$preferred_lifetime]",
+      CLEAR_STR IP6_STR "Neighbor discovery\n"
+			"Prefix advertised to hosts\n"
+			"IPv6 prefix to stop hosts using\n"
+			"Interface to advertise on\n"
+			"Interface name\n" VRF_CMD_HELP_STR "Router Advertisements to send\n"
+			"Count, default 2\n"
+			"Valid lifetime to advertise\n"
+			"Seconds, default 0\n"
+			"Preferred lifetime to advertise\n"
+			"Seconds, default 0\n")
+{
+	struct vrf *vrf;
+	struct interface *ifp = NULL;
+	struct zebra_if *zif;
+	struct zebra_vrf *zvrf;
+	struct rtadv_prefix ref = {}, *rprefix;
+	struct prefix_ipv6 p;
+	uint32_t valid = valid_lifetime_str ? (uint32_t)valid_lifetime : 0;
+	uint32_t preferred = preferred_lifetime_str ? (uint32_t)preferred_lifetime : 0;
+	uint8_t ra_count = count_str ? (uint8_t)count : RTADV_FLUSH_DEFAULT_COUNT;
+
+	if (!vrf_is_backend_netns() && vrf_name) {
+		vty_out(vty, "%% VRF subcommand only applicable for netns-based vrfs.\n");
+		return CMD_WARNING;
+	}
+
+	/*
+	 * RFC 4861 4.6.2: hosts discard a prefix option whose preferred
+	 * lifetime exceeds its valid lifetime, so this would silently do
+	 * nothing.
+	 */
+	if (preferred > valid) {
+		vty_out(vty,
+			"%% preferred-lifetime %u exceeds valid-lifetime %u; hosts would ignore the advertisement\n",
+			preferred, valid);
+		return CMD_WARNING;
+	}
+
+	if (vrf_name) {
+		vrf = vrf_lookup_by_name(vrf_name);
+		if (vrf)
+			ifp = if_lookup_by_name(ifname, vrf->vrf_id);
+	} else if (vrf_is_backend_netns()) {
+		/*
+		 * With netns VRFs the same ifname can exist in more than one
+		 * namespace, so a scan would pick an arbitrary link.  Match
+		 * "show ipv6 nd ra-interfaces" and take the default VRF.
+		 */
+		ifp = if_lookup_by_name(ifname, VRF_DEFAULT);
+	} else {
+		/*
+		 * vrf-lite keeps every interface in one namespace, so the name
+		 * is unambiguous and the scan resolves it wherever it is
+		 * enslaved.  The vrf argument is rejected on this backend
+		 * above, so this is the only way to reach an interface that is
+		 * not in the default VRF.
+		 */
+		RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
+			ifp = if_lookup_by_name(ifname, vrf->vrf_id);
+			if (ifp)
+				break;
+		}
+	}
+
+	if (!ifp) {
+		vty_out(vty, "%% Interface %s not found\n", ifname);
+		return CMD_WARNING;
+	}
+
+	zif = ifp->info;
+	if (!zif) {
+		vty_out(vty, "%% Interface %s is not ready\n", ifp->name);
+		return CMD_WARNING;
+	}
+
+	/*
+	 * Everything below has to hold or the entry would sit in the list
+	 * without any RA to carry it out.
+	 */
+	if (ifp->ifindex == IFINDEX_INTERNAL || !if_is_operative(ifp)) {
+		vty_out(vty, "%% Interface %s is not operationally up\n", ifp->name);
+		return CMD_WARNING;
+	}
+
+	if (!zif->rtadv.AdvSendAdvertisements) {
+		vty_out(vty, "%% Router advertisements are not enabled on %s\n", ifp->name);
+		return CMD_WARNING;
+	}
+
+	/* Validate before rtadv_interface_get_zvrf(), which asserts in netns. */
+	zvrf = rtadv_interface_get_zvrf(ifp);
+	if (!zvrf || zvrf->rtadv.sock < 0) {
+		vty_out(vty, "%% No router advertisement socket for %s\n", ifp->name);
+		return CMD_WARNING;
+	}
+
+	p = *prefix;
+	apply_mask_ipv6(&p);
+
+	ref.prefix = p;
+	rprefix = rtadv_prefixes_find(zif->rtadv.prefixes, &ref);
+	if (rprefix && !rprefix->AdvAutonomousFlag)
+		zlog_warn("%s(%s:%u): %pFX is advertised with autoconfiguration off, so no host address derives from it",
+			  ifp->name, ifp->vrf->name, ifp->ifindex, (const struct prefix *)&p);
+
+	/*
+	 * A prefix this interface does not advertise goes out as a synthetic
+	 * option carrying the Autonomous flag, so a non-zero valid lifetime
+	 * would tell hosts to form an address from a prefix this link does not
+	 * serve, which is the opposite of retiring one.
+	 */
+	if (!rprefix && valid != 0) {
+		vty_out(vty,
+			"%% %s does not advertise %pFX, so only valid-lifetime 0 can retire it\n",
+			ifp->name, (const struct prefix *)&p);
+		return CMD_WARNING;
+	}
+
+	if (!rtadv_flush_enqueue(zif, &p, valid, preferred, ra_count, false)) {
+		vty_out(vty, "%% %s is already flushing %d prefixes\n", ifp->name,
+			RTADV_MAX_FLUSH_PREFIXES);
+		return CMD_WARNING;
+	}
+
+	/*
+	 * Silent on success, like every other clear command: any output here is
+	 * read as an error by callers that wrap this in an API, and "show
+	 * interface" already reports the in-flight flush.  The audit trail goes
+	 * to the log instead.
+	 *
+	 * Note for whoever reads that log: the host deprecates the derived
+	 * address at once, but RFC 4862 5.5.3(e) floors the valid lifetime it
+	 * stores at two hours, so the address stays present-but-deprecated for
+	 * that long.
+	 */
+	zlog_info("%s(%s:%u): flushing %pFX, valid %u preferred %u in %u RA(s)", ifp->name,
+		  ifp->vrf->name, ifp->ifindex, (const struct prefix *)&p, valid, preferred,
+		  ra_count);
+
+	return CMD_SUCCESS;
+}
+
 void rtadv_cmd_init(void)
 {
 	interfaces_configured_for_ra_from_bgp = 0;
@@ -2323,6 +2851,7 @@ void rtadv_cmd_init(void)
 	hook_register(zebra_if_extra_info, nd_dump_vty);

 	install_element(VIEW_NODE, &show_ipv6_nd_ra_if_cmd);
+	install_element(ENABLE_NODE, &clear_ipv6_nd_prefix_cmd);
 }

 #ifdef __linux__
diff --git a/zebra/rtadv.h b/zebra/rtadv.h
index 3e08d553c4..dcd1f00d9b 100644
--- a/zebra/rtadv.h
+++ b/zebra/rtadv.h
@@ -35,6 +35,64 @@ struct rtadv {
 PREDECL_RBTREE_UNIQ(rtadv_prefixes);
 PREDECL_SORTLIST_UNIQ(pref64_advs);

+/*
+ * A prefix being flushed: re-announced with short (normally zero) lifetimes for
+ * a bounded number of RAs, so hosts deprecate the derived SLAAC address now
+ * rather than waiting out AdvValidLifetime (30 days by default).
+ *
+ * The prefix is held by value, not as a struct rtadv_prefix pointer, for two
+ * reasons.  It survives the prefix entry being freed and recreated underneath
+ * us -- frr-reload does exactly that on any lifetime change, and the delete
+ * path frees it outright.  And it lets us flush a prefix that this interface
+ * does not advertise at all, which is the mis-cabling case: a host moved from
+ * one port to another still holds the old port's prefix, so the RA that retires
+ * it has to go out on the new port.
+ */
+struct rtadv_flush {
+	struct prefix_ipv6 prefix;
+
+	/* Lifetimes to advertise for this prefix while the flush is active. */
+	uint32_t AdvValidLifetime;
+	uint32_t AdvPreferredLifetime;
+
+	/* RAs left to send.  Decremented only when one is actually sent. */
+	uint8_t remaining;
+
+	/* Queued by removing the prefix, not by the clear command. */
+	bool on_delete;
+
+	/*
+	 * Wall-clock backstop.  Without it, a prefix deleted mid-flush leaves an
+	 * entry that never counts down (no RA carries it any more), and a much
+	 * later re-add would emit stale zero-lifetime RAs out of nowhere.
+	 */
+	time_t deadline;
+};
+
+#define RTADV_MAX_FLUSH_PREFIXES  16
+#define RTADV_FLUSH_DEADLINE_SECS 120
+
+/*
+ * Spacing between flush RAs.  RFC 4861 6.2.6 requires consecutive multicast
+ * RAs to be at least MIN_DELAY_BETWEEN_RAS apart, so the burst is paced at
+ * that floor rather than any faster.  The first RA goes out inline, so a
+ * burst of N spans (N - 1) periods: three seconds for the default count of
+ * two, six for the three sent on prefix deletion.  That is immaterial next
+ * to the thirty day lifetime the burst exists to cut short.
+ *
+ * It runs on a private timer rather than the fast-retransmit counter, which is
+ * shared with link-up and RA-enable, accounts for only two of the RA transmit
+ * paths, and would both miscount and clobber unrelated convergence.
+ */
+#define RTADV_FLUSH_PERIOD_MS MIN_DELAY_BETWEEN_RAS
+
+/*
+ * RAs sent by an operator-driven flush.  One is enough when it is not lost;
+ * the second covers a single drop, which is the realistic failure for
+ * unacknowledged multicast on an otherwise healthy link.
+ */
+#define RTADV_FLUSH_DEFAULT_COUNT 2
+
 /* Router advertisement parameter.  From RFC4861, RFC6275 and RFC4191. */
 struct rtadvconf {
 	/* A flag indicating whether or not the router sends periodic Router
@@ -215,6 +273,16 @@ struct rtadvconf {

 	/* LL pending default-router withdrawal (removed while link was down). */
 	struct in6_addr retract_ll;
+
+	/*
+	 * Prefixes currently being flushed, and the timer driving the burst.
+	 * A fixed array rather than an allocated list: the cap is structural,
+	 * there is nothing to free on interface teardown, and zebra_if is
+	 * XCALLOC'd so this starts out empty for free.
+	 */
+	struct rtadv_flush flush[RTADV_MAX_FLUSH_PREFIXES];
+	uint8_t flush_count;
+	struct event *flush_timer;
 };

 struct rtadv_rdnss {