From patchwork Wed Jun 28 20:20:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Prestwood X-Patchwork-Id: 13296209 Received: from mail-qk1-f169.google.com (mail-qk1-f169.google.com [209.85.222.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BBFEF1094D for ; Wed, 28 Jun 2023 20:20:40 +0000 (UTC) Received: by mail-qk1-f169.google.com with SMTP id af79cd13be357-7659924cd9bso14583985a.1 for ; Wed, 28 Jun 2023 13:20:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687983639; x=1690575639; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=r5fgcfEBuTalvon00iPLb5lYbeZ3/uquFPKvE+ppM/M=; b=YmvRnEYRvrJTOtohO58m7j4qOVFBbNFVfY+e7j/dRM4xiKfzGVpwE7P9Ua2VZDZp60 luz9DAr1dRpv3zdvGUSF2X8Geqzjcqu2H6W3rfLdkx3KGbKjy2zecEupvArwpLuAsdT/ BbA9j1RwdgUZXAQMm+jGKOk1QFpYWCmFFh5g4MNoT93z2pxSOVwJYlQfpNlWn93YyNZ/ CAciiYMJYgzhenPfnNnAJHdxPL5rzKgL+uUxkHMIgBI+h3g10EOSM/eSoDfrq8ElNc6m h79vUomXubI7eU4CJCBDPUoo1cSQoV0R/xWhVMz8BvDGlWxzKy+jZIsvoLCTyGtd9Yyw PLxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687983639; x=1690575639; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=r5fgcfEBuTalvon00iPLb5lYbeZ3/uquFPKvE+ppM/M=; b=cWsjxn9w9a7pELaqX1zvKEfK+uGziK1T3/X5r4snp6JJFUxKF9G9Qt3/xUf82gTa/J p3L2G5E0S7rD7pZ1csdEDROPUTw8EsnvY9GmhvkSxLZHHXK7YU4Ka7d0fD5dya+V1aKP J6S8eQ1z6V1ycoPx4C3HZWOSjeAPCLibP9SEsW9ss7xmGhINOtHayFCiOD24Kesv38yY 1phyhqQzjp1wZsI6R5DC57z3thfZ3p3MvCxut+8Cskcdv3Tp0BesljK7oGKbX/8L+2St lsXCFxU45xRahrN+tQaTY3PE73NHXUxe5kA7nf1BFcSajSA09QzgQPFsRxf493oX7TL1 y6/Q== X-Gm-Message-State: AC+VfDz+IZCPcVi9hxsTmwWiswgDjnC38dFfGraZJlYnPwjt/n/x4Cfu gpY5odOaZG23ef36sRluEXUVyMHi4BQ= X-Google-Smtp-Source: ACHHUZ7odYIbZShJBVFL/JSvT03quTIZe6m4MVCqAqnlBJm42eA3124B/Uz6xWEi+d0n1omqfYr4vw== X-Received: by 2002:a05:620a:4549:b0:763:b4d7:51c1 with SMTP id u9-20020a05620a454900b00763b4d751c1mr39147253qkp.50.1687983639312; Wed, 28 Jun 2023 13:20:39 -0700 (PDT) Received: from LOCLAP699.rst-01.locus (50-78-19-50-static.hfc.comcastbusiness.net. [50.78.19.50]) by smtp.gmail.com with ESMTPSA id i4-20020a05620a144400b0076728319575sm672557qkl.27.2023.06.28.13.20.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jun 2023 13:20:39 -0700 (PDT) From: James Prestwood To: iwd@lists.linux.dev Cc: James Prestwood Subject: [PATCH 1/3] hwsim: add ADD/DEL_MAC_ADDR events Date: Wed, 28 Jun 2023 13:20:31 -0700 Message-Id: <20230628202033.2320994-2-prestwoj@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230628202033.2320994-1-prestwoj@gmail.com> References: <20230628202033.2320994-1-prestwoj@gmail.com> Precedence: bulk X-Mailing-List: iwd@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 These events are important to support both moving radios to other namespaces, and to allow scan address randomization to work. --- tools/hwsim.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/hwsim.c b/tools/hwsim.c index 47352ad4..ecee0a0a 100644 --- a/tools/hwsim.c +++ b/tools/hwsim.c @@ -62,6 +62,8 @@ enum { HWSIM_CMD_NEW_RADIO, HWSIM_CMD_DEL_RADIO, HWSIM_CMD_GET_RADIO, + HWSIM_CMD_ADD_MAC_ADDR, + HWSIM_CMD_DEL_MAC_ADDR, __HWSIM_CMD_MAX, }; #define HWSIM_CMD_MAX (__HWSIM_CMD_MAX - 1) From patchwork Wed Jun 28 20:20:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Prestwood X-Patchwork-Id: 13296210 Received: from mail-qk1-f182.google.com (mail-qk1-f182.google.com [209.85.222.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE1DE10944 for ; Wed, 28 Jun 2023 20:20:41 +0000 (UTC) Received: by mail-qk1-f182.google.com with SMTP id af79cd13be357-76728ae3162so11748285a.3 for ; Wed, 28 Jun 2023 13:20:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687983640; x=1690575640; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=u7WxOu4wYHLJynf3/A8tn2f/MjX2cfJb431wLlrXQnA=; b=YRt7DJnTGsxycWdFRLYXTc1TUorGcLacpp3P8p+bAW7N47IgLlketQr4y/6kPWi8jL eryvCtDdAtSC2VeG6PMd/sEUskCF0D4wxekfinJuX+Yq0VLzH1EZOYt6rhr9lDsTv+9S L2ByUm8mMdBbI2bwseQB3wZw+Zjq+kSTsydhK2uQgsK3fu8Njh4bPpRUQet32KEYhQJ0 KCCp196imc0VjE2xLE/beDUnwCoCWsNshxomFWkrJoAMNgbbWhxvhWlvdrGXXMzHSidL c6xPuMTYzcEjgG5zisH0lN+SvhXB22M7ANefMWEWwkhkrnmIFjEvqdDFwkY8z5mpBc5x fCGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687983640; x=1690575640; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=u7WxOu4wYHLJynf3/A8tn2f/MjX2cfJb431wLlrXQnA=; b=XMV1ZjAqOtnG4vecYx1MHT7QgOJen/IVBkVlYBg1OR1iUkplAsTgl3w3PK2dUmvA/I f876aItLeoupgYV61vL1t4lx6XXrDl8AHFdV3CI7pt3LE+OscxC9WxXtbOx2BAug5Dth B/zZ6Wdh2beGPOl27f+frXj9J3fTSxP0eDTdq1eY0tWOAYIfc8XwBlGN0jlpiwj2rEQb Rm6mqmjbgPIsXy8eKIP+dunsdh7WZNtr2Tsqm8cWn8mlVF8ptU/8k34sdy6PiH+AdfeG d5I9WgB5vgLOs/v0aHMfg1oxICzdf88M9BwIqd6reTPaZ1WS4aRoiQ6SrybUPoyoQRWU QLwQ== X-Gm-Message-State: AC+VfDxfbGpoVmIiZaZkb6x2hocA8OhMvxsUcVn5jSzNcbI9eaBxiDe9 pM/KrE4YallfLdTzgqyCGgWfnCbQVJc= X-Google-Smtp-Source: ACHHUZ6AA/W8HOE3wMYmfjHFltKv7/7TBb9MwW9BsQ/HUmmpGoCUeMUYNqdo7lsWvfHtuDMYSedC4w== X-Received: by 2002:a05:620a:4884:b0:767:160c:5990 with SMTP id ea4-20020a05620a488400b00767160c5990mr4770181qkb.53.1687983640411; Wed, 28 Jun 2023 13:20:40 -0700 (PDT) Received: from LOCLAP699.rst-01.locus (50-78-19-50-static.hfc.comcastbusiness.net. [50.78.19.50]) by smtp.gmail.com with ESMTPSA id i4-20020a05620a144400b0076728319575sm672557qkl.27.2023.06.28.13.20.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jun 2023 13:20:40 -0700 (PDT) From: James Prestwood To: iwd@lists.linux.dev Cc: James Prestwood Subject: [PATCH 2/3] hwsim: move frame processing into a separate function Date: Wed, 28 Jun 2023 13:20:32 -0700 Message-Id: <20230628202033.2320994-3-prestwoj@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230628202033.2320994-1-prestwoj@gmail.com> References: <20230628202033.2320994-1-prestwoj@gmail.com> Precedence: bulk X-Mailing-List: iwd@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The ADD/DEL_MAC_ADDR events come through the unicast handler so move the frame processing to a separate function so these other events can be handled. --- tools/hwsim.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/tools/hwsim.c b/tools/hwsim.c index ecee0a0a..2fee9b19 100644 --- a/tools/hwsim.c +++ b/tools/hwsim.c @@ -1572,7 +1572,9 @@ static void process_frame(struct hwsim_frame *frame) hwsim_frame_unref(frame); } -static void unicast_handler(struct l_genl_msg *msg, void *user_data) + + +static void hwsim_frame_event(struct l_genl_msg *msg) { struct hwsim_frame *frame; const struct mmpdu_header *mpdu; @@ -1581,9 +1583,6 @@ static void unicast_handler(struct l_genl_msg *msg, void *user_data) const void *data; const uint8_t *transmitter = NULL, *freq = NULL, *flags = NULL; - if (l_genl_msg_get_command(msg) != HWSIM_CMD_FRAME) - return; - if (!l_genl_attr_init(&attr, msg)) return; @@ -1683,6 +1682,24 @@ static void unicast_handler(struct l_genl_msg *msg, void *user_data) process_frame(frame); } +static void hwsim_unicast_handler(struct l_genl_msg *msg, void *user_data) +{ + uint8_t cmd; + + if (l_genl_msg_get_error(msg) < 0) + return; + + cmd = l_genl_msg_get_command(msg); + + switch (cmd) { + case HWSIM_CMD_FRAME: + hwsim_frame_event(msg); + break; + default: + break; + } +} + static void radio_manager_create_callback(struct l_genl_msg *msg, void *user_data) { @@ -2939,8 +2956,9 @@ static void hwsim_ready(void) } if (!l_genl_add_unicast_watch(genl, "MAC80211_HWSIM", - unicast_handler, NULL, NULL)) { - l_error("Failed to set unicast handler"); + hwsim_unicast_handler, + NULL, NULL)) { + l_error("Failed to set hwsim unicast handler"); goto error; } From patchwork Wed Jun 28 20:20:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Prestwood X-Patchwork-Id: 13296211 Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com [209.85.222.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E9CCC1094D for ; Wed, 28 Jun 2023 20:20:42 +0000 (UTC) Received: by mail-qk1-f178.google.com with SMTP id af79cd13be357-765a169001eso11363685a.3 for ; Wed, 28 Jun 2023 13:20:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687983641; x=1690575641; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SqYFtqpJIjv73xYkZBWWjOxcCdTR2r+wujQNQdjD9Es=; b=A1ZI/NlS/QD+a7dqMf5ezKkNC6YhtjRPhk0ghZvP2+UQgNQHDJUOJaWZYc86uyh4i6 /Mp8UoSFesaq0+xz0RjPSc+jNn4o4ZEISkrFxfuM6pI+5g1iMF939S76E1hxZ4jt6mpM SkNKzhybkVzNZn4RHj5yZB2mtbZK6FrM0CZfqNtZuE8ieEyeKhOCvwcjURITQNuXP/Q/ FVfRS7Wwb6Qt6+p0CI52Zkz+wDXjJMrKHsPbvCzSBRUM7Gl20qx1Rrj8GdZGNNj3ABH7 E0Q00N/wI901rde/Uje1J9phM3SoRIJOfcJYJpguGd4fG0j2FYfWCBrAXoOVWVl95b7s kSaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687983641; x=1690575641; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SqYFtqpJIjv73xYkZBWWjOxcCdTR2r+wujQNQdjD9Es=; b=EWe+OGOKgGalhPDFSF7LaXNUz3MjgsF7DmNyZExwcii/AFuPVVE7MSCHIBni7GVOIT 57OU0ALgdIKL+TVPCTfwbniq2WIqMdwt4qybuYeXjn5CH9lX2H8ywxX5TvE2Jjt1UcD/ ln8MlKuhgXhHVAkM3TZpOePfJEhLEHmWw+hztegQjRKRzKWN4OPNQhFZW3RbE4S0Yxjt hsykoaxOE4AxmGZwKHNdI0lmciCKp6+uJvVnGfTjq64HNcsrdSIuTy8Clm0iO2l2yHah buh63/y8+uellvHpW2hLHZtYsDVXg/4VY3rX5Z8t6JYOwj3ZxnO0uNs9LZcTtPW6N0vW TB2w== X-Gm-Message-State: AC+VfDwTJvZMq/LGkgfXBtOL3f5F6PzkiwMfPvMr2dDvWZ3xRaSsmSAg Xc5zTU++N9Se1OG3waOBENa8xpYkMsc= X-Google-Smtp-Source: ACHHUZ7Uz9LcJVWqzrrdhIQcabo8u4aS6VhsjR5BXxx3P1S63+plmmDHW5KY8Q1uIRaKwaeMYrYJnw== X-Received: by 2002:a05:620a:bd3:b0:765:a678:977c with SMTP id s19-20020a05620a0bd300b00765a678977cmr11428832qki.67.1687983641465; Wed, 28 Jun 2023 13:20:41 -0700 (PDT) Received: from LOCLAP699.rst-01.locus (50-78-19-50-static.hfc.comcastbusiness.net. [50.78.19.50]) by smtp.gmail.com with ESMTPSA id i4-20020a05620a144400b0076728319575sm672557qkl.27.2023.06.28.13.20.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jun 2023 13:20:41 -0700 (PDT) From: James Prestwood To: iwd@lists.linux.dev Cc: James Prestwood Subject: [PATCH 3/3] hwsim: handle ADD/DEL_MAC_ADDR events Date: Wed, 28 Jun 2023 13:20:33 -0700 Message-Id: <20230628202033.2320994-4-prestwoj@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230628202033.2320994-1-prestwoj@gmail.com> References: <20230628202033.2320994-1-prestwoj@gmail.com> Precedence: bulk X-Mailing-List: iwd@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Handling these events notifies hwsim of address changes for interface creation/removal outside the initial namespace as well as address changes due to scanning address randomization. Interfaces that hwsim already knows about are still handled via nl80211. But any interfaces not known when ADD/DEL_MAC_ADDR events come will be treated specially. For ADD, a dummy interface object will be created and added to the queue. This lets the frame processing match the destination address correctly. This can happen both for scan randomization and interface creation outside of the initial namespace. For the DEL event we handle similarly and don't touch any interfaces found via nl80211 (i.e. have a 'name') but need to also be careful with the dummy interfaces that were created outside the initial namespace. We want to keep these around but scanning MAC changes can also delete them. This is why a reference count was added so scanning doesn't cause a removal. For example, the following sequence: ADD_MAC_ADDR (interface creation) ADD_MAC_ADDR (scanning started) DEL_MAC_ADDR (scanning done) --- tools/hwsim.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/tools/hwsim.c b/tools/hwsim.c index 2fee9b19..5cb28624 100644 --- a/tools/hwsim.c +++ b/tools/hwsim.c @@ -440,6 +440,7 @@ struct interface_info_rec { uint8_t addr[ETH_ALEN]; char *name; uint32_t iftype; + int ref; }; static struct l_queue *radio_info; @@ -515,6 +516,14 @@ static bool interface_info_match_id(const void *a, const void *b) return rec->id == id; } +static bool interface_info_match_addr(const void *a, const void *b) +{ + const struct interface_info_rec *rec = a; + const uint8_t *addr = b; + + return memcmp(rec->addr, addr, ETH_ALEN) == 0; +} + static const char *radio_get_path(const struct radio_info_rec *rec) { static char path[15]; @@ -1682,6 +1691,123 @@ static void hwsim_frame_event(struct l_genl_msg *msg) process_frame(frame); } +static bool get_tx_rx_addrs(struct l_genl_msg *msg, const uint8_t **tx_out, + const uint8_t **rx_out) +{ + struct l_genl_attr attr; + uint16_t type, len; + const void *data; + const uint8_t *tx = NULL, *rx = NULL; + + if (!l_genl_attr_init(&attr, msg)) + return false; + + while (l_genl_attr_next(&attr, &type, &len, &data)) { + switch (type) { + case HWSIM_ATTR_ADDR_TRANSMITTER: + if (len != ETH_ALEN) + return false; + + tx = data; + break; + case HWSIM_ATTR_ADDR_RECEIVER: + if (len != ETH_ALEN) + return false; + + rx = data; + break; + default: + break; + } + } + + if (!tx || !rx) + return false; + + *tx_out = tx; + *rx_out = rx; + + return true; +} + +static void hwsim_add_mac_event(struct l_genl_msg *msg) +{ + const uint8_t *tx = NULL, *rx = NULL; + struct radio_info_rec *radio_rec; + struct interface_info_rec *interface_rec; + + if (!get_tx_rx_addrs(msg, &tx, &rx)) + return; + + /* No radio matches the TX address, hwsim must not have created it */ + radio_rec = l_queue_find(radio_info, radio_info_match_addr1, tx); + if (!radio_rec) + return; + + interface_rec = l_queue_find(interface_info, + interface_info_match_addr, rx); + if (interface_rec) { + /* Existing interface, address changes handled via nl80211 */ + if (interface_rec->name) + return; + + /* + * Transient/dummy interface we already know about. This likely + * was created, then a scan changed the address temporarily. + * Reflect this change and increment the ref so the following + * DEL event doesn't destroy it + */ + __atomic_fetch_add(&interface_rec->ref, 1, __ATOMIC_SEQ_CST); + memcpy(interface_rec->addr, rx, ETH_ALEN); + return; + } + + /* + * Create a dummy interface entry for this address that only contains + * the radio and address. This is either a transient entry due to scan + * randomization or an interface created outside this namespace. + */ + interface_rec = l_new(struct interface_info_rec, 1); + interface_rec->radio_rec = radio_rec; + interface_rec->ref = 1; + memcpy(interface_rec->addr, rx, ETH_ALEN); + + l_queue_push_tail(interface_info, interface_rec); +} + +static void hwsim_del_mac_event(struct l_genl_msg *msg) +{ + const uint8_t *tx = NULL, *rx = NULL; + struct radio_info_rec *radio_rec; + struct interface_info_rec *interface_rec; + + if (!get_tx_rx_addrs(msg, &tx, &rx)) + return; + + /* No radio matches the TX address, hwsim must not have created it */ + radio_rec = l_queue_find(radio_info, radio_info_match_addr1, tx); + if (!radio_rec) + return; + + interface_rec = l_queue_find(interface_info, + interface_info_match_addr, rx); + if (!interface_rec) + return; + + /* + * This change is handled via nl80211 so we don't want to touch this + * interface here. + */ + if (interface_rec->name) + return; + + if (__atomic_sub_fetch(&interface_rec->ref, 1, __ATOMIC_SEQ_CST)) + return; + + l_queue_remove(interface_info, interface_rec); + l_free(interface_rec); +} + static void hwsim_unicast_handler(struct l_genl_msg *msg, void *user_data) { uint8_t cmd; @@ -1695,6 +1821,12 @@ static void hwsim_unicast_handler(struct l_genl_msg *msg, void *user_data) case HWSIM_CMD_FRAME: hwsim_frame_event(msg); break; + case HWSIM_CMD_ADD_MAC_ADDR: + hwsim_add_mac_event(msg); + break; + case HWSIM_CMD_DEL_MAC_ADDR: + hwsim_del_mac_event(msg); + break; default: break; }