Commit ae55b12e35 for frr

commit ae55b12e351ece601d91ed6c158eebe33870f546
Author: Gabriel Goller <g.goller@proxmox.com>
Date:   Mon Sep 7 13:22:02 2026 +0200

    bgpd: evpn: update the system IP when loopback addresses change

    For IPv6 tunnels, the system IP comes from the default VRF loopback.
    Previously, bgpd only looked it up when Zebra announced the L3VNI.
    If the loopback address arrived later, routes kept using the anycast
    IP until the L3VNI was flapped. Removing the address also left the
    old system IP in use.

    Look up the system IP again when loopback addresses are added or
    removed. If it changes, update the type-5 and SVI type-2 routes.
    Skip ::1, unspecified, link-local and multicast IPv6 addresses, and
    the tunnel source itself, which often sits on the same loopback.

    Use the same address selection code for L3VNI announcements and CLI
    changes, so an IPv4 setting does not replace an IPv6 system IP.

    Fixes: #23268
    Signed-off-by: Gabriel Goller <g.goller@proxmox.com>

diff --git a/bgpd/bgp_evpn.c b/bgpd/bgp_evpn.c
index cab70651d1..8e6ef14be2 100644
--- a/bgpd/bgp_evpn.c
+++ b/bgpd/bgp_evpn.c
@@ -8307,6 +8307,135 @@ static void link_l2vni_hash_to_l3vni(struct hash_bucket *bucket,
 		bgpevpn_link_to_l3vni(vpn);
 }

+/*
+ * Pick the address an L3VNI with an IPv6 tunnel source uses as its
+ * advertise-pip system IP. Remote VTEPs encapsulate towards it, so an
+ * address they cannot reach is of no use: the loopback carries ::1 before
+ * any address of the operator, and it is not link-local, so the generic
+ * global address lookup does not skip it. The anycast tunnel source often
+ * lives on the same loopback, and using it as the system IP would defeat
+ * advertise-pip.
+ */
+static bool bgp_evpn_get_pip_ipv6(struct interface *ifp, const struct in6_addr *vtep_ip,
+				  struct in6_addr *addr)
+{
+	struct connected *connected;
+	struct prefix *cp;
+
+	frr_each (if_connected, ifp->connected, connected) {
+		cp = connected->address;
+
+		if (cp->family != AF_INET6)
+			continue;
+
+		if (IN6_IS_ADDR_UNSPECIFIED(&cp->u.prefix6) ||
+		    IN6_IS_ADDR_LOOPBACK(&cp->u.prefix6) ||
+		    IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6) || IN6_IS_ADDR_MULTICAST(&cp->u.prefix6) ||
+		    IPV6_ADDR_SAME(&cp->u.prefix6, vtep_ip))
+			continue;
+
+		IPV6_ADDR_COPY(addr, &cp->u.prefix6);
+
+		return true;
+	}
+
+	return false;
+}
+
+/*
+ * Derive the advertise-pip system IP of a tenant VRF instance. A statically
+ * configured system IP takes precedence, otherwise the system IP follows the
+ * family of the L3VNI tunnel source: a global IPv6 address of the default VRF
+ * loopback for an IPv6 tunnel source, the EVPN instance router-id for an IPv4
+ * one.
+ *
+ * Returns true if the system IP changed.
+ */
+bool bgp_evpn_derive_pip_ip(struct bgp *bgp_vrf)
+{
+	struct ipaddr *pip_ip_static = &bgp_vrf->evpn_info->pip_ip_static;
+	struct ipaddr pip_ip = { .ipa_type = IPADDR_NONE };
+	struct bgp *bgp_evpn = bgp_get_evpn();
+	struct interface *ifp;
+	struct in6_addr addr;
+
+	/* Remote VTEPs encapsulate towards the system IP, so it can only stand
+	 * in for a tunnel source of its own family. The CLI takes an IPv4
+	 * address today, which leaves an IPv6 tunnel source to the loopback
+	 * lookup below. The tunnel source is unknown until zebra announces the
+	 * L3VNI, keep the configured address until then.
+	 */
+	if (!ipaddr_is_zero(pip_ip_static) &&
+	    (IS_IPADDR_NONE(&bgp_vrf->originator_ip) ||
+	     pip_ip_static->ipa_type == bgp_vrf->originator_ip.ipa_type)) {
+		pip_ip = *pip_ip_static;
+	} else if (IS_IPADDR_V6(&bgp_vrf->originator_ip)) {
+		ifp = if_get_vrf_loopback(VRF_DEFAULT);
+		if (ifp && bgp_evpn_get_pip_ipv6(ifp, &bgp_vrf->originator_ip.ipaddr_v6, &addr))
+			ipaddr_set_v6(&pip_ip, &addr);
+	} else if (bgp_evpn) {
+		ipaddr_set_v4(&pip_ip, bgp_evpn->router_id);
+	}
+
+	if (ipaddr_is_same(&bgp_vrf->evpn_info->pip_ip, &pip_ip))
+		return false;
+
+	if (bgp_debug_zebra(NULL))
+		zlog_debug("VRF %s vni %u advertise-pip primary ip %pIA replaced by %pIA",
+			   vrf_id_to_name(bgp_vrf->vrf_id), bgp_vrf->l3vni,
+			   &bgp_vrf->evpn_info->pip_ip, &pip_ip);
+
+	bgp_vrf->evpn_info->pip_ip = pip_ip;
+
+	return true;
+}
+
+/*
+ * The advertise-pip system IP of an L3VNI with an IPv6 tunnel source is taken
+ * from the default VRF loopback, which bgpd may well learn about only after
+ * zebra has announced the L3VNI. Re-derive it and re-advertise the routes
+ * carrying it whenever the addresses of that loopback change.
+ */
+void bgp_evpn_handle_pip_ip_change(struct interface *ifp)
+{
+	struct bgp *bgp_evpn = bgp_get_evpn();
+	struct listnode *node;
+	struct bgp *bgp_vrf;
+
+	if (ifp->vrf->vrf_id != VRF_DEFAULT || !if_is_loopback(ifp))
+		return;
+
+	for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp_vrf)) {
+		struct listnode *vni_node;
+		struct bgpevpn *vpn;
+
+		if (!bgp_vrf->l3vni || !IS_IPADDR_V6(&bgp_vrf->originator_ip))
+			continue;
+
+		/* nothing advertises the system IP unless PIP is in use, the
+		 * CLI derives it again when the feature is turned on
+		 */
+		if (!bgp_vrf->evpn_info->advertise_pip || !bgp_vrf->evpn_info->is_anycast_mac)
+			continue;
+
+		if (!bgp_evpn_derive_pip_ip(bgp_vrf))
+			continue;
+
+		update_advertise_vrf_routes(bgp_vrf);
+
+		/* the SVI MAC-IP routes of the L2VNIs carry it as well */
+		if (!bgp_evpn)
+			continue;
+
+		for (ALL_LIST_ELEMENTS_RO(bgp_vrf->l2vnis, vni_node, vpn)) {
+			if (!bgp_evpn_is_svi_macip_enabled(vpn))
+				continue;
+
+			update_routes_for_vni(bgp_evpn, vpn);
+		}
+	}
+}
+
 int bgp_evpn_local_l3vni_add(vni_t l3vni, vrf_id_t vrf_id, struct ethaddr *svi_rmac,
 			     struct ethaddr *vrr_rmac, struct ipaddr *originator_ip, int filter,
 			     ifindex_t svi_ifindex, bool is_anycast_mac)
@@ -8375,24 +8504,8 @@ int bgp_evpn_local_l3vni_add(vni_t l3vni, vrf_id_t vrf_id, struct ethaddr *svi_r
 	/* PIP user configured mac is not present use svi mac as sys mac */
 	if (is_zero_mac(&bgp_vrf->evpn_info->pip_rmac_static))
 		memcpy(&bgp_vrf->evpn_info->pip_rmac, svi_rmac, ETH_ALEN);
-	/* for v6 vtep_ip assign lo primary v6 address as pip,
-	 * for v4 vtep_ip bgp instance router-id as pip in bgp_evpn_init.
-	 */
-	if (IS_IPADDR_V6(&bgp_vrf->originator_ip)) {
-		struct interface *ifp;
-		struct in6_addr addr;
-
-		ifp = if_get_vrf_loopback(VRF_DEFAULT);
-		if (ifp && if_get_ipv6_global(ifp, &addr)) {
-			if (bgp_debug_zebra(NULL))
-				zlog_debug("%s vni %u ifp %s addr %pI6 copy as pip", __func__,
-					   bgp_vrf->l3vni, ifp->name, &addr);
-			ipaddr_set_v6(&bgp_vrf->evpn_info->pip_ip, &addr);
-		} else if (ifp)
-			if (bgp_debug_zebra(NULL))
-				zlog_debug("%s vni %u ifp %s v6 addr not found, skip pip assignment",
-					   __func__, bgp_vrf->l3vni, ifp->name);
-	}
+	/* the system IP follows the family of the L3VNI tunnel source */
+	bgp_evpn_derive_pip_ip(bgp_vrf);

 	if (bgp_debug_zebra(NULL))
 		zlog_debug("VRF %s vni %u pip %s IP %pIA RMAC %pEA sys RMAC %pEA static RMAC %pEA is_anycast_mac %s",
diff --git a/bgpd/bgp_evpn.h b/bgpd/bgp_evpn.h
index e144e97456..6a233fdc28 100644
--- a/bgpd/bgp_evpn.h
+++ b/bgpd/bgp_evpn.h
@@ -255,4 +255,6 @@ extern void bgp_zebra_evpn_pop_items_from_announce_fifo(struct bgpevpn *vpn);
 extern int install_uninstall_routes_for_vni(struct bgp *bgp, struct bgpevpn *vpn, bool install);
 extern void bgp_evpn_fill_rmac_nh_to_attr(struct bgp *bgp_vrf, struct attr *attr,
 					  struct prefix_evpn *evp, struct ipaddr *vtep_ip);
+extern bool bgp_evpn_derive_pip_ip(struct bgp *bgp_vrf);
+extern void bgp_evpn_handle_pip_ip_change(struct interface *ifp);
 #endif /* _QUAGGA_BGP_EVPN_H */
diff --git a/bgpd/bgp_evpn_vty.c b/bgpd/bgp_evpn_vty.c
index 3dd4b74772..55e414294f 100644
--- a/bgpd/bgp_evpn_vty.c
+++ b/bgpd/bgp_evpn_vty.c
@@ -4925,13 +4925,10 @@ DEFPY (bgp_evpn_advertise_pip_ip_mac,
 				return CMD_SUCCESS;

 			ipaddr_set_v4(&bgp_vrf->evpn_info->pip_ip_static, ip);
-			ipaddr_set_v4(&bgp_vrf->evpn_info->pip_ip, ip);
-		} else {
+		} else
 			bgp_vrf->evpn_info->pip_ip_static.ipaddr_v4.s_addr = INADDR_ANY;
-			/* default instance router-id assignemt */
-			if (bgp_evpn)
-				bgp_vrf->evpn_info->pip_ip.ipaddr_v4 = bgp_evpn->router_id;
-		}
+
+		bgp_evpn_derive_pip_ip(bgp_vrf);
 		/* parse sys mac */
 		if (!is_zero_mac(&mac->eth_addr)) {
 			/* Already configured with same MAC */
@@ -4999,11 +4996,8 @@ DEFPY (bgp_evpn_advertise_pip_ip_mac,
 		memset(&bgp_vrf->evpn_info->pip_rmac_static, 0, ETH_ALEN);
 		/* reset user configured sys IP */
 		bgp_vrf->evpn_info->pip_ip_static.ipaddr_v4.s_addr = INADDR_ANY;
-		/* Assign default PIP IP (bgp instance router-id) */
-		if (bgp_evpn)
-			bgp_vrf->evpn_info->pip_ip.ipaddr_v4 = bgp_evpn->router_id;
-		else
-			bgp_vrf->evpn_info->pip_ip.ipaddr_v4.s_addr = INADDR_ANY;
+		/* assign the default system IP again */
+		bgp_evpn_derive_pip_ip(bgp_vrf);
 	}

 	if (is_evpn_enabled()) {
diff --git a/bgpd/bgp_zebra.c b/bgpd/bgp_zebra.c
index 0fca942173..f895cce139 100644
--- a/bgpd/bgp_zebra.c
+++ b/bgpd/bgp_zebra.c
@@ -354,6 +354,8 @@ static int bgp_interface_address_add(ZAPI_CALLBACK_ARGS)

 	frrtrace(4, frr_bgp, interface_address_oper_zrecv, vrf_id, ifc->ifp->name, ifc->address, 1);

+	bgp_evpn_handle_pip_ip_change(ifc->ifp);
+
 	if (!bgp)
 		return 0;

@@ -438,6 +440,8 @@ static int bgp_interface_address_delete(ZAPI_CALLBACK_ARGS)

 	frrtrace(4, frr_bgp, interface_address_oper_zrecv, vrf_id, ifc->ifp->name, ifc->address, 2);

+	bgp_evpn_handle_pip_ip_change(ifc->ifp);
+
 	if (bgp && if_is_operative(ifc->ifp)) {
 		bgp_connected_delete(bgp, ifc);
 	}