Commit 6ca411aaa6 for frr

commit 6ca411aaa6c98613e92f47d61a868193b2217350
Author: Vincent Jardin <vjardin@free.fr>
Date:   Wed Nov 11 10:48:35 2026 +0100

    zebra: re-read the interface flags on macOS

    XNU sends RTM_IFINFO from the middle of a flags change.

    When an interface is set up, the message has IFF_UP without
    IFF_RUNNING while SIOCGIFFLAGS right after returns both !

      RTM_IFINFO index 6 flags 0x8823 UP addrs 0
      SIOCGIFFLAGS: flags 0x8863 UP RUNNING

    Nothing follows, so after
      ifconfig en0 down; ifconfig en0 up
    zebra can keep the interface up but not operative, and the protocols
    do not get it back until some other message makes zebra read the
    flags again.

    So on macOS, read the flags with SIOCGIFFLAGS on each RTM_IFINFO, and
    while an interface is up but not running, read them again every 200 ms
    for up to 5 s.

    Note that it is a workaround on an issue fixed decades ago
    on BSD kernels.

    Signed-off-by: Vincent Jardin <vjardin@free.fr>

diff --git a/zebra/kernel_socket.c b/zebra/kernel_socket.c
index a90ddc5fca..c98f3067a3 100644
--- a/zebra/kernel_socket.c
+++ b/zebra/kernel_socket.c
@@ -519,6 +519,49 @@ static enum zebra_link_type sdl_to_zebra_link_type(unsigned int sdlt)
 	}
 }

+#ifdef __APPLE__
+/*
+ * Check again at the flags of the interfaces that are up but not running,
+ * for a few seconds: see ifm_read().
+ * XNU workaround on an issue fixed on BSD kernel decades ago.
+ */
+#define KERNEL_FLAGS_RECHECK_MSEC 200
+#define KERNEL_FLAGS_RECHECK_MAX  25
+
+static struct event *t_flags_recheck;
+static int flags_recheck_left;
+
+static void kernel_flags_recheck(struct event *event)
+{
+	struct vrf *vrf;
+	struct interface *ifp;
+	bool pending = false;
+
+	RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
+		FOR_ALL_INTERFACES (vrf, ifp) {
+			if (ifp->ifindex == IFINDEX_INTERNAL || !CHECK_FLAG(ifp->flags, IFF_UP) ||
+			    if_is_operative(ifp))
+				continue;
+			if_get_flags(ifp);
+			if (CHECK_FLAG(ifp->flags, IFF_UP) && !if_is_operative(ifp))
+				pending = true;
+		}
+	}
+
+	if (pending && --flags_recheck_left > 0)
+		event_add_timer_msec(zrouter.master, kernel_flags_recheck, NULL,
+				     KERNEL_FLAGS_RECHECK_MSEC, &t_flags_recheck);
+}
+
+static void kernel_flags_recheck_start(void)
+{
+	flags_recheck_left = KERNEL_FLAGS_RECHECK_MAX;
+	if (!event_is_scheduled(t_flags_recheck))
+		event_add_timer_msec(zrouter.master, kernel_flags_recheck, NULL,
+				     KERNEL_FLAGS_RECHECK_MSEC, &t_flags_recheck);
+}
+#endif /* __APPLE__ */
+
 /*
  * Handle struct if_msghdr obtained from reading routing socket or
  * sysctl (from interface_list).  There may or may not be sockaddrs
@@ -717,7 +760,16 @@ int ifm_read(struct if_msghdr *ifm)

 		/* update flags and handle operative->inoperative transition, if
 		 * any */
+#ifdef __APPLE__
+		if (if_nametoindex(ifp->name) == (unsigned int)ifm->ifm_index) {
+			if_get_flags(ifp);
+			if (CHECK_FLAG(ifp->flags, IFF_UP) && !if_is_operative(ifp))
+				kernel_flags_recheck_start();
+		} else
+			if_flags_update(ifp, ifm->ifm_flags);
+#else
 		if_flags_update(ifp, ifm->ifm_flags);
+#endif /* __APPLE__ */

 #ifndef RTM_IFANNOUNCE
 		if (!if_is_up(ifp)) {
@@ -1640,6 +1692,9 @@ void kernel_router_init(void)
  */
 void kernel_router_terminate(void)
 {
+#ifdef __APPLE__
+	event_cancel(&t_flags_recheck);
+#endif
 }

 /*