From patchwork Fri Dec 1 10:49:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Berg X-Patchwork-Id: 1870540 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=hostap-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4ShVGS5SZmz24DQ for ; Fri, 1 Dec 2023 21:50:56 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=BFaZIk2CWbYQtL6aQSgjlIhwcggVPJBTk8iLDdMc6wc=; b=wv7ZzW24/secIX x/Q7iY8Vxp+C8dcpw9TZHEpUSeWedAfSHQDCLySJwCNNcGDiNiYdDLBoHhUsRiv0ocvYlIuox/uKC YxG7jsi+7yQnwjRvAmV3eLdLYBVx8WmBxsDmmRkGzaXwbpBdIuNZWHrK0T3oJgETFtvUV8/VVjxWz vZtMG75goNz5FpPDxa0iqJIgvF3IcE8kZXTxoBlkiHKllcvgbkeTW8bxJSYVOEiymDL5K4r7nEi1L 0H21YyxjEKFSYgl8K3ZDd+TUpla83cBUAbs9IYlXj/BoWhv/0ztqe+eVEYMxyWx/4DV9IsjTGHbTO JZyzv4j2LS1CBwu6IZGQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r915s-00DGF0-1a; Fri, 01 Dec 2023 10:50:00 +0000 Received: from s3.sipsolutions.net ([2a01:4f8:242:246e::2] helo=sipsolutions.net) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r915p-00DGDE-1U for hostap@lists.infradead.org; Fri, 01 Dec 2023 10:49:58 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sipsolutions.net; s=mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Content-Type:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-To: Resent-Cc:Resent-Message-ID; bh=gCYgYqHnTsuL1EhCdX4RcAVkG0NIh0eg2vdMmu8RzJg=; t=1701427797; x=1702637397; b=Vtgsjr6kzgrjo8SfO9buclI0jS9tllCRVEPwi6CgnyByQrs XEBxGnavuj2xPUfwM3jtl/AZn53BNty9Ciin3i3XSPtQwCSeLAEfYO0sIjtdS8830OrpXsmSSFJCI ModLpUqA+MOBPs7nGdW+TsQHqK4PotutTC2+Y9Ewh117zvhwc4UO+PRCGw78FDmFL7A5kgIsEREMo 2rwdB/s9potbnUsA455NyNTxJDwe9zBls8TcBGaSNeb2yTb2KgoRfuyn2FEFUksek1mnCbLf7iuFC KMgKRWXuRyGFblr0yoxcQ0Hr4yCrbEhAdQL4v7KiOrHYjeKx1Epcj0+SbzYSfuRw==; Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.97) (envelope-from ) id 1r915n-0000000BBjV-15Ek; Fri, 01 Dec 2023 11:49:55 +0100 From: Johannes Berg To: linux-wireless@vger.kernel.org Cc: hostap@lists.infradead.org, netdev@vger.kernel.org, Johannes Berg Subject: [PATCH wpa_suppplicant 1/2] netlink: add netlink_process_one_event() Date: Fri, 1 Dec 2023 11:49:08 +0100 Message-ID: <20231201114952.c278cb7ac0c4.If32fddf88f23f3939bb73bc6926aad7f88804079@changeid> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231201104952.26254-4-johannes@sipsolutions.net> References: <346b21d87c69f817ea3c37caceb34f1f56255884.camel@sipsolutions.net> <20231201104952.26254-4-johannes@sipsolutions.net> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231201_024957_495652_9FF78CED X-CRM114-Status: GOOD ( 15.01 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Johannes Berg Add a new function to read and process a single netlink event with a timeout, to be used in driver_nl80211. Signed-off-by: Johannes Berg --- src/drivers/netlink.c | 43 +++++++++++++++++++++++++++++++++++++++---- src/drivers/netlink.h | 2 ++ 2 files changed, 41 insertions(+), 4 dele [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: hostap@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Hostap" Errors-To: hostap-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Johannes Berg Add a new function to read and process a single netlink event with a timeout, to be used in driver_nl80211. Signed-off-by: Johannes Berg --- src/drivers/netlink.c | 43 +++++++++++++++++++++++++++++++++++++++---- src/drivers/netlink.h | 2 ++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/drivers/netlink.c b/src/drivers/netlink.c index 7780479c3e91..bbfe86eee7a0 100644 --- a/src/drivers/netlink.c +++ b/src/drivers/netlink.c @@ -33,19 +33,20 @@ static void netlink_receive_link(struct netlink_data *netlink, } -static void netlink_receive(int sock, void *eloop_ctx, void *sock_ctx) +static void _netlink_process_one_event(struct netlink_data *netlink, + int wait_single) { - struct netlink_data *netlink = eloop_ctx; char buf[8192]; int left; struct sockaddr_nl from; socklen_t fromlen; struct nlmsghdr *h; - int max_events = 10; + int max_events = wait_single ? 1 : 10; try_again: fromlen = sizeof(from); - left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT, + left = recvfrom(netlink->sock, buf, sizeof(buf), + wait_single ? 0 : MSG_DONTWAIT, (struct sockaddr *) &from, &fromlen); if (left < 0) { if (errno != EINTR && errno != EAGAIN) @@ -88,6 +89,40 @@ try_again: } +void netlink_process_one_event(struct netlink_data *netlink, + unsigned int timeout_ms) +{ + if (timeout_ms) { + struct timeval timeout = { + .tv_sec = timeout_ms / 1000, + .tv_usec = 1000 * (timeout_ms % 1000), + }; + fd_set read_set; + int ret; + + FD_ZERO(&read_set); + FD_SET(netlink->sock, &read_set); + + ret = select(netlink->sock + 1, &read_set, NULL, NULL, + &timeout); + if (ret < 0) { + perror("select on netlink socket"); + return; + } + if (ret == 0) + return; + } + + _netlink_process_one_event(netlink, 1); +} + + +static void netlink_receive(int sock, void *eloop_ctx, void *sock_ctx) +{ + _netlink_process_one_event(eloop_ctx, 0); +} + + struct netlink_data * netlink_init(struct netlink_config *cfg) { struct netlink_data *netlink; diff --git a/src/drivers/netlink.h b/src/drivers/netlink.h index 3a7340e51534..faee28b722ea 100644 --- a/src/drivers/netlink.h +++ b/src/drivers/netlink.h @@ -21,6 +21,8 @@ struct netlink_config { }; struct netlink_data * netlink_init(struct netlink_config *cfg); +void netlink_process_one_event(struct netlink_data *netlink, + unsigned int timeout_ms); void netlink_deinit(struct netlink_data *netlink); int netlink_send_oper_ifla(struct netlink_data *netlink, int ifindex, int linkmode, int operstate); From patchwork Fri Dec 1 10:49:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Berg X-Patchwork-Id: 1870541 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=a7muxIwA; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=sipsolutions.net header.i=@sipsolutions.net header.a=rsa-sha256 header.s=mail header.b=IZVmFIRq; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=hostap-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4ShVGV57sdz24DR for ; Fri, 1 Dec 2023 21:50:58 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=lIllz7KChJuGkhDdUmP1YO56tVa7bjXnspUyGDkAlT4=; b=a7muxIwAwfsErB zuKcvKXhT5Ea80mmbzyECeT6JhP0L5cpqcDUl2pA0keRCvlchWWLO0o/BlkxOSHqop3XJ+lNHrvr5 2t+SKEhbiBmip85f9o7uzAyrf7rM6eIx2EuGwqFE5J6tkh+i9NTiUx4Zs0tJfJlD6aoa3h3I6wcbw wTa9H4y82Jz+p4jRKOqfDi6eUwzwdl4UQdiHuHqNSZF1vo6X33y/ByCJeYluwX669wPYJ2jlKzEZG PZKorF0X0WT7ec0XrTz9MP/B3PteRaPsG2IkJxfMQ2d91tWwJiPMI6+8kXEwk2LLTlFie2KMtEpfX 1nwewFVR8l9UZR+nOj0A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r915u-00DGGG-0E; Fri, 01 Dec 2023 10:50:02 +0000 Received: from s3.sipsolutions.net ([2a01:4f8:242:246e::2] helo=sipsolutions.net) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r915p-00DGDO-2e for hostap@lists.infradead.org; Fri, 01 Dec 2023 10:49:59 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sipsolutions.net; s=mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Content-Type:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-To: Resent-Cc:Resent-Message-ID; bh=rDc7S3TGwDRkuN19FCwRpWlUg/012zfS/c7UoSCfhOg=; t=1701427797; x=1702637397; b=IZVmFIRqD+MV+cUG1LDULwOPuvPcOAIdlktbIULhbcjOSpf EVHkXcM0fo4+UDPFGvo3OJRAzLiwGhszIrjUfcQWs5yLl+dczqDe2k30eNsqatSx3cDjufTFcemW2 lkhbhePz5UF4M/tpua+uLprwUVCRsMQyvsflon+cQTZI77yNWGWdD8qJ1vS09Qfzjfhng1Z0u1QvV Upf0syS/kY/Tyztk2MzZxTbLjs/D4SJITxPZWAIBDSFvIwnB4FEdT/MIh0tbM55luSyiNml2swqc3 0YnFMJKDFBi62Og00Jw2s1KKf7StSzWOAkE3CytxIiL/PB6Fp7E2WX4RGVYlITLA==; Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.97) (envelope-from ) id 1r915n-0000000BBjV-3rZk; Fri, 01 Dec 2023 11:49:56 +0100 From: Johannes Berg To: linux-wireless@vger.kernel.org Cc: hostap@lists.infradead.org, netdev@vger.kernel.org, Johannes Berg Subject: [PATCH wpa_suppplicant 2/2] driver_nl82011: wait for rtnetlink event with carrier_up_count Date: Fri, 1 Dec 2023 11:49:09 +0100 Message-ID: <20231201114952.420b40a5f188.I75677b755f36ca63f8289d84de29b212f4c37ec0@changeid> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231201104952.26254-4-johannes@sipsolutions.net> References: <346b21d87c69f817ea3c37caceb34f1f56255884.camel@sipsolutions.net> <20231201104952.26254-4-johannes@sipsolutions.net> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231201_024957_858659_B4FBB0B6 X-CRM114-Status: GOOD ( 19.84 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Johannes Berg There's a race (see the comment in the code) between the kernel and userspace that can lead to dropping TX packets after the associated event was already received. Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: hostap@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Hostap" Errors-To: hostap-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Johannes Berg There's a race (see the comment in the code) between the kernel and userspace that can lead to dropping TX packets after the associated event was already received. If the kernel indicates the carrier_up_count, wait for the corresponding rtnetlink event to reach that count. This fixes the race since the rtnetlink event is sent after the async processing that's needed in the kernel before a frame can be transmitted. Signed-off-by: Johannes Berg --- src/drivers/driver_nl80211.c | 7 ++++ src/drivers/driver_nl80211.h | 2 ++ src/drivers/driver_nl80211_event.c | 52 ++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/src/drivers/driver_nl80211.c b/src/drivers/driver_nl80211.c index 03d54222bb52..b442dee1e710 100644 --- a/src/drivers/driver_nl80211.c +++ b/src/drivers/driver_nl80211.c @@ -1365,6 +1365,7 @@ static void wpa_driver_nl80211_event_rtm_newlink(void *ctx, char ifname[IFNAMSIZ + 1]; char extra[100], *pos, *end; int init_failed; + u32 carrier_up_count = 0; extra[0] = '\0'; pos = extra; @@ -1396,6 +1397,9 @@ static void wpa_driver_nl80211_event_rtm_newlink(void *ctx, pos += os_snprintf(pos, end - pos, " linkmode=%u", nla_get_u32((struct nlattr *) attr)); break; + case IFLA_CARRIER_UP_COUNT: + carrier_up_count = nla_get_u32((struct nlattr *) attr); + break; } attr = RTA_NEXT(attr, attrlen); } @@ -1415,6 +1419,9 @@ static void wpa_driver_nl80211_event_rtm_newlink(void *ctx, if (init_failed) return; /* do not update interface state */ + if (carrier_up_count) + drv->carrier_up_count = carrier_up_count; + if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) { namebuf[0] = '\0'; if (if_indextoname(ifi->ifi_index, namebuf) && diff --git a/src/drivers/driver_nl80211.h b/src/drivers/driver_nl80211.h index f82f604e9017..874988715b21 100644 --- a/src/drivers/driver_nl80211.h +++ b/src/drivers/driver_nl80211.h @@ -201,6 +201,8 @@ struct wpa_driver_nl80211_data { unsigned int puncturing:1; unsigned int qca_ap_allowed_freqs:1; + u32 carrier_up_count; + u32 ignore_next_local_disconnect; u32 ignore_next_local_deauth; diff --git a/src/drivers/driver_nl80211_event.c b/src/drivers/driver_nl80211_event.c index 60b4fb51fcd8..4ff25b57ceeb 100644 --- a/src/drivers/driver_nl80211_event.c +++ b/src/drivers/driver_nl80211_event.c @@ -19,6 +19,7 @@ #include "common/ieee802_11_defs.h" #include "common/ieee802_11_common.h" #include "driver_nl80211.h" +#include "netlink.h" static void @@ -256,6 +257,49 @@ static void nl80211_parse_wmm_params(struct nlattr *wmm_attr, } +/* + * Wait for an RTM newlink event with corresponding carrier up count + * + * There's a race condition with mac80211 and the network stack, which + * mostly hits depending on scheduling in time-simulation (tests), + * which is that mac80211 will both indicate carrier on to the network + * stack and send the associated/... event to userspace. Now, the + * internal kernel function netif_carrier_ok() immediately returns + * true after this, however, the linkwatch work still needs to run + * and change the TX queue qdisc away from noop (which drops all TX + * packets). + * + * When the race happens, userspace (and in particular tests) can see + * the associated/... event and immediately try to send a frame, at a + * time that the linkwatch work hasn't run yet, causing the frame to + * be dropped. + * + * Thus, if the kernel indicated the current carrier_up_count in an + * event, wait here for an RTM newlink event for our interface, so in + * in addition to seeing the associated/... event, we also know the + * carrier state has actually changed sufficiently to send packets, + * if it was meant to change. + * + * This works because the event to userspace is also sent from the + * asynchronous linkwatch work. + */ +static void +nl80211_wait_for_carrier_up_count(struct wpa_driver_nl80211_data *drv, + u32 carrier_up_count) +{ +#define WRAPPED_U32_LESS(x, y) ((s32)(y) - (s32)(x) < 0) + + if (WRAPPED_U32_LESS(drv->carrier_up_count, carrier_up_count)) + netlink_process_one_event(drv->global->netlink, 100); + + if (WRAPPED_U32_LESS(drv->carrier_up_count, carrier_up_count)) + wpa_printf(MSG_ERROR, + "nl80211: %s: carrier up count %u not seen (got %u)\n", + drv->first_bss->ifname, carrier_up_count, + drv->carrier_up_count); +} + + static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv, const u8 *frame, size_t len, struct nlattr *wmm, struct nlattr *req_ie) @@ -3845,6 +3889,14 @@ static void do_process_drv_event(struct i802_bss *bss, int cmd, cmd == NL80211_CMD_SCAN_ABORTED)) nl80211_restore_ap_mode(bss); + /* see comment above wpa_driver_nl80211_own_ifname() */ + if (tb[NL80211_ATTR_CARRIER_UP_COUNT]) { + u32 carrier_up_count = + nla_get_u32(tb[NL80211_ATTR_CARRIER_UP_COUNT]); + + nl80211_wait_for_carrier_up_count(drv, carrier_up_count); + } + switch (cmd) { case NL80211_CMD_TRIGGER_SCAN: wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");