git: 1edc20b76953 - main - wpa: ctrl_iface set sendbuf size

From: Bjoern A. Zeeb <bz_at_FreeBSD.org>
Date: Sat, 02 Dec 2023 20:38:30 UTC
The branch main has been updated by bz:

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

commit 1edc20b76953d9ef571b0bcf89b206b98ed13d9b
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2023-11-12 20:33:41 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2023-12-02 20:37:36 +0000

    wpa: ctrl_iface set sendbuf size
    
    In order to avoid running into the default net.local.dgram.maxdgram
    of 2K currently when calling sendto(2) try to set the sndbuf size to
    the maximum ctrl message size.
    While on 14 and 15 this does not actually raise the limit anymore (and
    be7c095ac99ad29fd72b780c7d58949a38656c66 raised it for syslogd and this),
    FreeBSD 13 still requires this change and it will work as expected there.
    In addition we always ensure a large enough send buffer this way
    independent of kernel defaults.
    The problem occured, e.g., when the scan_list result had enough BSSIDs
    so the text output would exceed 2048 bytes.
    
    Sponsored by:   The FreeBSD Foundation
    MFC after:      3 days
    PR:             274990
    Reviewed by:    cy, adrian (with previous comment)
    Differential Revision: https://reviews.freebsd.org/D42558
---
 contrib/wpa/wpa_supplicant/ctrl_iface_unix.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/contrib/wpa/wpa_supplicant/ctrl_iface_unix.c b/contrib/wpa/wpa_supplicant/ctrl_iface_unix.c
index 639573dae75e..deeed2b11833 100644
--- a/contrib/wpa/wpa_supplicant/ctrl_iface_unix.c
+++ b/contrib/wpa/wpa_supplicant/ctrl_iface_unix.c
@@ -506,6 +506,10 @@ static int wpas_ctrl_iface_open_sock(struct wpa_supplicant *wpa_s,
 	struct group *grp;
 	char *endp;
 	int flags;
+#if defined(__FreeBSD__)
+	int optval, rc;
+	socklen_t optlen;
+#endif
 
 	buf = os_strdup(wpa_s->conf->ctrl_interface);
 	if (buf == NULL)
@@ -679,6 +683,22 @@ havesock:
 		}
 	}
 
+#if defined(__FreeBSD__)
+	/* Ensure we can send a full length message atomically. */
+	optval = 0;
+	optlen = sizeof(optval);
+	if (getsockopt(priv->sock, SOL_SOCKET, SO_SNDBUF, &optval, &optlen) == -1) {
+		wpa_printf(MSG_INFO, "failed to get sndbuf for sock=%d: %s",
+			   priv->sock, strerror(errno));
+	} else if (optval < CTRL_IFACE_MAX_LEN) {
+		optval = CTRL_IFACE_MAX_LEN;
+		if (setsockopt(priv->sock, SOL_SOCKET, SO_SNDBUF, &optval,
+			       sizeof(optval)) == -1)
+			wpa_printf(MSG_ERROR, "failed to set sndbuf for "
+				   "sock=%d: %s", priv->sock, strerror(errno));
+	}
+#endif
+
 	eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
 				 wpa_s, priv);
 	wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);