git: ded53e898c7b - stable/13 - rc/WPA: driver_bsd.c: backout upstream IFF_ change and add logging

From: Bjoern A. Zeeb <bz_at_FreeBSD.org>
Date: Mon, 26 Jun 2023 12:08:21 UTC
The branch stable/13 has been updated by bz:

URL: https://cgit.FreeBSD.org/src/commit/?id=ded53e898c7be6d610e94c1746fd22304f5c5988

commit ded53e898c7be6d610e94c1746fd22304f5c5988
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2023-03-23 22:37:12 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2023-06-26 09:00:55 +0000

    rc/WPA: driver_bsd.c: backout upstream IFF_ change and add logging
    
    This reverts the state to our old supplicant logic setting or clearing
    IFF_UP if needed.  In addition this adds logging for the cases in which
    we do (not) change the interface state.
    
    Depending on testing this seems to help bringing WiFi up or not log
    any needed changes (which would be the expected wpa_supplicant logic
    now).  People should look out for ``(changed)`` log entries (at least
    if debugging the issue; this way we will at least have data points).
    
    There is a hypothesis still pondered that the entire IFF_UP toggling
    only exploits a race in net80211 (see further discssussions for more
    debugging and alternative solutions see D38508 and D38753).
    That may also explain why the changes to the rc startup script [1]
    only helped partially for some people to no longer see the
    continuous CTRL-EVENT-SCAN-FAILED.
    
    It is highly likely that we will want further changes and until
    we know for sure that people are seeing ''(changed)'' events
    this should stay local.  Should we need to upstream this we'll
    likely need #ifdef __FreeBSD__ around this code.
    
    Remove ifconfig down/up workaround (bfb202c4554a) in
    rc.d/wpa_supplicant as it is no longer needed.
    
    [1] 5fcdc19a81115d975e238270754e28557a2fcfc5 and
        d06d7eb09131edea666bf049d6c0c55672726f76
    
    Sponsored by:   The FreeBSD Foundation
    Reviewed by:    cy, enweiwu (earlier)
    Differential Revision: https://reviews.freebsd.org/D38807
    
    (cherry picked from commit bfb202c4554a72383202a1a401d80721935b8c95)
    
    Reviewed by:    bz (for wireless)
    Differential Revision:  https://reviews.freebsd.org/D39257
    
    (cherry picked from commit 052211e08c0e227277d0c4dc603bba2253eb3d73)
---
 contrib/wpa/src/drivers/driver_bsd.c | 50 ++++++++++++++++++++++++++++--------
 libexec/rc/rc.d/wpa_supplicant       |  6 -----
 2 files changed, 40 insertions(+), 16 deletions(-)

diff --git a/contrib/wpa/src/drivers/driver_bsd.c b/contrib/wpa/src/drivers/driver_bsd.c
index 345bbb892ecf..d5ff51cee456 100644
--- a/contrib/wpa/src/drivers/driver_bsd.c
+++ b/contrib/wpa/src/drivers/driver_bsd.c
@@ -294,8 +294,9 @@ bsd_send_mlme_param(void *priv, const u8 op, const u16 reason, const u8 *addr)
 }
 
 static int
-bsd_get_iface_flags(struct bsd_driver_data *drv)
+bsd_ctrl_iface(void *priv, int enable)
 {
+	struct bsd_driver_data *drv = priv;
 	struct ifreq ifr;
 
 	os_memset(&ifr, 0, sizeof(ifr));
@@ -307,6 +308,33 @@ bsd_get_iface_flags(struct bsd_driver_data *drv)
 		return -1;
 	}
 	drv->flags = ifr.ifr_flags;
+
+
+	if (enable) {
+		if (ifr.ifr_flags & IFF_UP)
+			goto nochange;
+		ifr.ifr_flags |= IFF_UP;
+	} else {
+		if (!(ifr.ifr_flags & IFF_UP))
+			goto nochange;
+		ifr.ifr_flags &= ~IFF_UP;
+	}
+
+	if (ioctl(drv->global->sock, SIOCSIFFLAGS, &ifr) < 0) {
+		wpa_printf(MSG_ERROR, "ioctl[SIOCSIFFLAGS]: %s",
+			   strerror(errno));
+		return -1;
+	}
+
+	wpa_printf(MSG_DEBUG, "%s: if %s (changed) enable %d IFF_UP %d ",
+	    __func__, drv->ifname, enable, ((ifr.ifr_flags & IFF_UP) != 0));
+
+	drv->flags = ifr.ifr_flags;
+	return 0;
+
+nochange:
+	wpa_printf(MSG_DEBUG, "%s: if %s (no change) enable %d IFF_UP %d ",
+	    __func__, drv->ifname, enable, ((ifr.ifr_flags & IFF_UP) != 0));
 	return 0;
 }
 
@@ -526,7 +554,7 @@ bsd_set_ieee8021x(void *priv, struct wpa_bss_params *params)
 			   __func__);
 		return -1;
 	}
-	return 0;
+	return bsd_ctrl_iface(priv, 1);
 }
 
 static void
@@ -1030,7 +1058,8 @@ bsd_init(struct hostapd_data *hapd, struct wpa_init_params *params)
 	if (l2_packet_get_own_addr(drv->sock_xmit, params->own_addr))
 		goto bad;
 
-	if (bsd_get_iface_flags(drv) < 0)
+	/* mark down during setup */
+	if (bsd_ctrl_iface(drv, 0) < 0)
 		goto bad;
 
 	if (bsd_set_mediaopt(drv, IFM_OMASK, IFM_IEEE80211_HOSTAP) < 0) {
@@ -1055,12 +1084,13 @@ bsd_deinit(void *priv)
 {
 	struct bsd_driver_data *drv = priv;
 
+	if (drv->ifindex != 0)
+		bsd_ctrl_iface(drv, 0);
 	if (drv->sock_xmit != NULL)
 		l2_packet_deinit(drv->sock_xmit);
 	os_free(drv);
 }
 
-
 static int
 bsd_set_sta_authorized(void *priv, const u8 *addr,
 		       unsigned int total_flags, unsigned int flags_or,
@@ -1309,7 +1339,7 @@ wpa_driver_bsd_associate(void *priv, struct wpa_driver_associate_params *params)
 	 * NB: interface must be marked UP for association
 	 * or scanning (ap_scan=2)
 	 */
-	if (bsd_get_iface_flags(drv) < 0)
+	if (bsd_ctrl_iface(drv, 1) < 0)
 		return -1;
 
 	os_memset(&mlme, 0, sizeof(mlme));
@@ -1354,11 +1384,8 @@ wpa_driver_bsd_scan(void *priv, struct wpa_driver_scan_params *params)
 	}
 
 	/* NB: interface must be marked UP to do a scan */
-	if (!(drv->flags & IFF_UP)) {
-		wpa_printf(MSG_DEBUG, "%s: interface is not up, cannot scan",
-			   __func__);
+	if (bsd_ctrl_iface(drv, 1) < 0)
 		return -1;
-	}
 
 #ifdef IEEE80211_IOC_SCAN_MAX_SSID
 	os_memset(&sr, 0, sizeof(sr));
@@ -1664,7 +1691,7 @@ wpa_driver_bsd_init(void *ctx, const char *ifname, void *priv)
 		drv->capa.key_mgmt_iftype[i] = drv->capa.key_mgmt;
 
 	/* Down interface during setup. */
-	if (bsd_get_iface_flags(drv) < 0)
+	if (bsd_ctrl_iface(drv, 0) < 0)
 		goto fail;
 
 	/* Proven to work, lets go! */
@@ -1688,6 +1715,9 @@ wpa_driver_bsd_deinit(void *priv)
 	if (drv->ifindex != 0 && !drv->if_removed) {
 		wpa_driver_bsd_set_wpa(drv, 0);
 
+		/* NB: mark interface down */
+		bsd_ctrl_iface(drv, 0);
+
 		wpa_driver_bsd_set_wpa_internal(drv, drv->prev_wpa,
 						drv->prev_privacy);
 
diff --git a/libexec/rc/rc.d/wpa_supplicant b/libexec/rc/rc.d/wpa_supplicant
index e7a34e6c5f6e..8a86fec90e4d 100755
--- a/libexec/rc/rc.d/wpa_supplicant
+++ b/libexec/rc/rc.d/wpa_supplicant
@@ -12,7 +12,6 @@
 
 name="wpa_supplicant"
 desc="WPA/802.11i Supplicant for wireless network devices"
-start_postcmd="wpa_poststart"
 rcvar=
 
 ifn="$2"
@@ -28,11 +27,6 @@ is_ndis_interface()
 	esac
 }
 
-wpa_poststart() {
-	ifconfig ${ifn} down
-	ifconfig ${ifn} up
-}
-
 if is_wired_interface ${ifn} ; then
 	driver="wired"
 elif is_ndis_interface ${ifn} ; then