Commit f249432240 for frr

commit f249432240da5b51f6c3cc6732529c2fd483157a
Author: Donatas Abraitis <donatas@opensourcerouting.org>
Date:   Wed Nov 18 14:10:11 2026 +0200

    bgpd: Check maximum-prefix when a withdrawn path is counted again

    Since b381ed97a6 ("bgpd: Adjust received prefix count only when we
    validate the nexthop") bgp_maximum_prefix_overflow() is only called on
    the new-path branch of bgp_update(). A path that was withdrawn but is
    still attached to its dest has already left peer->pcount:

    - a path flagged BGP_PATH_REMOVED that the process queue has not reaped
      yet;
    - a path kept as a BGP_PATH_HISTORY entry under dampening.

    When such a prefix is announced again, the existing-path branch counts
    it back in through bgp_path_info_restore() or bgp_damp_update() and
    returns without checking the limit.

    A neighbor could exploit this by withdrawing its prefixes and
    re-announcing them, plus new ones, in the same UPDATE, keeping more
    prefixes than its configured maximum-prefix. With `bgp dampening`
    enabled, announcing and withdrawing prefixes in blocks no larger than
    the limit and then re-announcing all of them is not bounded by the
    UPDATE size or timing at all. In both cases the session stays up and
    %MAXPFXEXCEED is never logged.

    Record on entry to the existing-path branch whether the path is
    currently uncounted and, only in that case, run the maximum-prefix
    check at each exit where it has just been counted again: the
    same-attribute dampening shortcut, the dampening-suppressed early
    return and the main exit after bgp_aggregate_increment().
    bgp_rib_remove() under filtered: takes the path out of the count and
    the aggregates again.

    Updates for paths that are already counted are not affected.

    Fixes: ANT-2026-V31WXFWZ
    Fixes: b381ed97a6 ("bgpd: Adjust received prefix count only when we validate the nexthop")
    Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>

diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c
index c718116f31..165c751dd1 100644
--- a/bgpd/bgp_route.c
+++ b/bgpd/bgp_route.c
@@ -5782,6 +5782,20 @@ bool bgp_maximum_prefix_overflow(struct peer *peer, afi_t afi, safi_t safi,
 	return false;
 }

+/*
+ * A path that was withdrawn but is still attached to its dest (flagged
+ * BGP_PATH_REMOVED and not reaped yet, or kept as a BGP_PATH_HISTORY entry
+ * by dampening) is not part of the peer's prefix count. When an UPDATE
+ * brings it back (bgp_path_info_restore() or bgp_damp_update()), it is
+ * counted again, so it has to pass the same maximum-prefix check as a new
+ * path would.
+ */
+static bool bgp_maximum_prefix_overflow_recount(struct peer *peer, afi_t afi, safi_t safi,
+						bool recount)
+{
+	return recount && bgp_maximum_prefix_overflow(peer, afi, safi, 0);
+}
+
 /* Unconditionally remove the route from the RIB, without taking
  * damping into consideration (eg, because the session went down)
  */
@@ -6612,6 +6626,11 @@ void bgp_update(struct peer *peer, const struct prefix *p, uint32_t addpath_id,

 	/* If the update is implicit withdraw. */
 	if (pi) {
+		/* Withdrawn, but not yet reaped or kept as dampening history:
+		 * not counted in pcount until this update restores it.
+		 */
+		bool recount = !BGP_PATH_COUNTABLE(pi);
+
 		pi->uptime = monotime(NULL);
 		same_attr = attrhash_cmp(pi->attr, attr_new) &&
 			    bgp_path_info_extra_same(pi, &rmap_bpi);
@@ -6670,6 +6689,13 @@ void bgp_update(struct peer *peer, const struct prefix *p, uint32_t addpath_id,
 								safi);
 					bgp_process(bgp, dest, pi, afi, safi);
 				}
+
+				if (bgp_maximum_prefix_overflow_recount(peer, afi, orig_safi,
+									recount)) {
+					reason = "maximum-prefix overflow";
+					bgp_attr_unintern_clear_reuse(attr, &attr_new);
+					goto filtered;
+				}
 			} else /* Duplicate - odd */
 			{
 				peer->pcount_dup[afi][safi]++;
@@ -6925,6 +6951,13 @@ void bgp_update(struct peer *peer, const struct prefix *p, uint32_t addpath_id,
 			/* Now we do normal update dampening.  */
 			ret = bgp_damp_update(pi, dest, afi, safi);
 			if (ret == BGP_DAMP_SUPPRESSED) {
+				/* A suppressed path is counted as well */
+				if (bgp_maximum_prefix_overflow_recount(peer, afi, orig_safi,
+									recount)) {
+					reason = "maximum-prefix overflow";
+					goto filtered;
+				}
+
 				bgp_dest_unlock_node(dest);
 				if (p_evpn)
 					evpn_overlay_free(p_evpn);
@@ -6986,6 +7019,16 @@ void bgp_update(struct peer *peer, const struct prefix *p, uint32_t addpath_id,
 		/* Process change. */
 		bgp_aggregate_increment(bgp, p, pi, afi, safi);

+		/* The path may have been counted again above
+		 * (bgp_path_info_restore() or bgp_damp_update()).
+		 * bgp_rib_remove() under filtered: takes it out of the
+		 * count and the aggregates again.
+		 */
+		if (bgp_maximum_prefix_overflow_recount(peer, afi, orig_safi, recount)) {
+			reason = "maximum-prefix overflow";
+			goto filtered;
+		}
+
 		bgp_process(bgp, dest, pi, afi, safi);
 		bgp_dest_unlock_node(dest);