Skip to contentSkip to conversation

anqp: Only register for frames on station interfaces

PATCH In review Andrew Zaborowski 2 participants 9 messages Nov 20, 2019 – Nov 21, 2019 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/6 anqp: Only register for frames on station interfaces

Check the iftype before registering ANQP on new interface.

Not that the check here and in rrm.c (which already checks the iftype) may need to be extended to run on NETDEV_WATCH_EVENT_UP because a device could be created with a different iftype and then have the iftype changed before powering up.

---
 src/anqp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
src/anqp.c +3 -1
index f3171456..6627bd81 100644
@@ -374,7 +374,9 @@ static void anqp_netdev_watch(struct netdev *netdev,
 {
 	switch (event) {
 	case NETDEV_WATCH_EVENT_NEW:
-		anqp_register_frame(netdev_get_ifindex(netdev));
+		if (netdev_get_iftype(netdev) == NETDEV_IFTYPE_STATION)
+			anqp_register_frame(netdev_get_ifindex(netdev));
+
 		return;
 	default:
 		break;
Signature · 1 line
2.20.1
#1 lore raw

PATCH 2/6 device: Move device creation from netdev.c to event watch

Create and destroy the device state struct and the DBus interfaces in a way more similar to the Station, AdHoc and AP interfaces. Drop netdev_get_device() and the device specific code in netdev that as far as I can tell wasn't needed.

---
 src/device.c | 42 +++++++++++++++++++++++++++++++-----------
 src/netdev.c | 16 ++++------------
 src/netdev.h |  2 --
 3 files changed, 35 insertions(+), 25 deletions(-)
src/device.c +31 -11
index e0e42c4d..4c963bc2 100644
@@ -289,31 +289,48 @@ static void device_netdev_notify(struct netdev *netdev,
 					enum netdev_watch_event event,
 					void *user_data)
 {
-	struct device *device = netdev_get_device(netdev);
+	struct device *device;
 	struct l_dbus *dbus = dbus_get_bus();
+	const char *path = netdev_get_path(netdev);
+
+	device = l_dbus_object_get_data(dbus, path, IWD_DEVICE_INTERFACE);
 
-	if (!device)
+	if (!device && event != NETDEV_WATCH_EVENT_NEW)
 		return;
 
 	switch (event) {
+	case NETDEV_WATCH_EVENT_NEW:
+		if (L_WARN_ON(device))
+			break;
+
+		if (netdev_get_iftype(netdev) == NETDEV_IFTYPE_P2P_CLIENT ||
+				netdev_get_iftype(netdev) ==
+				NETDEV_IFTYPE_P2P_GO)
+			return;
+
+		device_create(netdev_get_wiphy(netdev), netdev);
+		break;
+	case NETDEV_WATCH_EVENT_DEL:
+		l_dbus_unregister_object(dbus, path);
+		break;
 	case NETDEV_WATCH_EVENT_UP:
 		device->powered = true;
 
-		l_dbus_property_changed(dbus, netdev_get_path(device->netdev),
+		l_dbus_property_changed(dbus, path,
 					IWD_DEVICE_INTERFACE, "Powered");
 		break;
 	case NETDEV_WATCH_EVENT_DOWN:
 		device->powered = false;
 
-		l_dbus_property_changed(dbus, netdev_get_path(device->netdev),
+		l_dbus_property_changed(dbus, path,
 					IWD_DEVICE_INTERFACE, "Powered");
 		break;
 	case NETDEV_WATCH_EVENT_NAME_CHANGE:
-		l_dbus_property_changed(dbus, netdev_get_path(device->netdev),
+		l_dbus_property_changed(dbus, path,
 					IWD_DEVICE_INTERFACE, "Name");
 		break;
 	case NETDEV_WATCH_EVENT_ADDRESS_CHANGE:
-		l_dbus_property_changed(dbus, netdev_get_path(device->netdev),
+		l_dbus_property_changed(dbus, path,
 					IWD_DEVICE_INTERFACE, "Address");
 		break;
 	default:
@@ -380,12 +397,8 @@ struct device *device_create(struct wiphy *wiphy, struct netdev *netdev)
 
 void device_remove(struct device *device)
 {
-	struct l_dbus *dbus = dbus_get_bus();
-
 	l_debug("");
 
-	l_dbus_unregister_object(dbus, netdev_get_path(device->netdev));
-
 	scan_wdev_remove(netdev_get_wdev_id(device->netdev));
 
 	netdev_frame_watch_remove(device->netdev, device->ap_roam_watch);
@@ -394,12 +407,19 @@ void device_remove(struct device *device)
 	l_free(device);
 }
 
+static void destroy_device_interface(void *user_data)
+{
+	struct device *device = user_data;
+
+	device_remove(device);
+}
+
 static int device_init(void)
 {
 	if (!l_dbus_register_interface(dbus_get_bus(),
 					IWD_DEVICE_INTERFACE,
 					setup_device_interface,
-					NULL, false))
+					destroy_device_interface, false))
 		return false;
 
 	netdev_watch = netdev_watch_add(device_netdev_notify, NULL, NULL);
src/netdev.c +4 -12
index e3fcbc1b..3c1510b9 100644
@@ -50,7 +50,6 @@
 #include "src/eapol.h"
 #include "src/handshake.h"
 #include "src/crypto.h"
-#include "src/device.h"
 #include "src/scan.h"
 #include "src/netdev.h"
 #include "src/ft.h"
@@ -89,7 +88,6 @@ struct netdev {
 	char name[IFNAMSIZ];
 	uint32_t type;
 	uint8_t addr[ETH_ALEN];
-	struct device *device;
 	struct wiphy *wiphy;
 	unsigned int ifi_flags;
 	uint32_t frequency;
@@ -150,6 +148,7 @@ struct netdev {
 	bool expect_connect_failure : 1;
 	bool aborting : 1;
 	bool mac_randomize_once : 1;
+	bool events_ready : 1;
 };
 
 struct netdev_preauth_state {
@@ -306,11 +305,6 @@ struct handshake_state *netdev_get_handshake(struct netdev *netdev)
 	return netdev->handshake;
 }
 
-struct device *netdev_get_device(struct netdev *netdev)
-{
-	return netdev->device;
-}
-
 const char *netdev_get_path(struct netdev *netdev)
 {
 	static char path[256];
@@ -656,11 +650,9 @@ static void netdev_free(void *data)
 		netdev->qos_map_cmd_id = 0;
 	}
 
-	if (netdev->device) {
+	if (netdev->events_ready)
 		WATCHLIST_NOTIFY(&netdev_watches, netdev_watch_func_t,
 					netdev, NETDEV_WATCH_EVENT_DEL);
-		device_remove(netdev->device);
-	}
 
 	watchlist_destroy(&netdev->frame_watches);
 	watchlist_destroy(&netdev->station_watches);
@@ -4203,7 +4195,7 @@ static void netdev_newlink_notify(const struct ifinfomsg *ifi, int bytes)
 		}
 	}
 
-	if (!netdev->device) /* Did we send NETDEV_WATCH_EVENT_NEW yet? */
+	if (!netdev->events_ready) /* Did we send NETDEV_WATCH_EVENT_NEW yet? */
 		return;
 
 	new_up = netdev_get_is_up(netdev);
@@ -4269,9 +4261,9 @@ static void netdev_initial_up_cb(int error, uint16_t type, const void *data,
 
 	l_debug("Interface %i initialized", netdev->index);
 
-	netdev->device = device_create(netdev->wiphy, netdev);
 	WATCHLIST_NOTIFY(&netdev_watches, netdev_watch_func_t,
 				netdev, NETDEV_WATCH_EVENT_NEW);
+	netdev->events_ready = true;
 }
 
 static void netdev_set_mac_cb(int error, uint16_t type, const void *data,
src/netdev.h +0 -2
index 8028167d..032265f9 100644
@@ -23,7 +23,6 @@
 #include <stdbool.h>
 
 struct netdev;
-struct device;
 struct scan_bss;
 struct handshake_state;
 struct eapol_sm;
@@ -135,7 +134,6 @@ int netdev_set_4addr(struct netdev *netdev, bool use_4addr,
 bool netdev_get_4addr(struct netdev *netdev);
 const char *netdev_get_name(struct netdev *netdev);
 bool netdev_get_is_up(struct netdev *netdev);
-struct device *netdev_get_device(struct netdev *netdev);
 const char *netdev_get_path(struct netdev *netdev);
 
 struct handshake_state *netdev_handshake_state_new(struct netdev *netdev);
Signature · 1 line
2.20.1
#2 lore raw

PATCH 3/6 device: Make functions static, drop device.h

In reply to Andrew Zaborowski
---
 Makefile.am   |   2 +-
 src/adhoc.c   |   1 -
 src/ap.c      |   1 -
 src/device.c  | 113 +++++++++++++++++++++++++-------------------------
 src/device.h  |  30 --------------
 src/station.c |   1 -
 6 files changed, 57 insertions(+), 91 deletions(-)
 delete mode 100644 src/device.h
Makefile.am +1 -1
index f58006e7..8c69d9f4 100644
@@ -189,7 +189,7 @@ src_iwd_SOURCES = src/main.c linux/nl80211.h src/iwd.h src/missing.h \
 					src/plugin.h src/plugin.c \
 					src/netdev.h src/netdev.c \
 					src/wiphy.h src/wiphy.c \
-					src/device.h src/device.c \
+					src/device.c \
 					src/station.h src/station.c \
 					src/ie.h src/ie.c \
 					src/dbus.h src/dbus.c \
src/adhoc.c +0 -1
index a63c4669..52164a0a 100644
@@ -30,7 +30,6 @@
 
 #include "src/iwd.h"
 #include "src/module.h"
-#include "src/device.h"
 #include "src/netdev.h"
 #include "src/wiphy.h"
 #include "src/crypto.h"
src/ap.c +0 -1
index 2fca6ce2..c79fe9b5 100644
@@ -34,7 +34,6 @@
 #include "src/iwd.h"
 #include "src/module.h"
 #include "src/scan.h"
-#include "src/device.h"
 #include "src/netdev.h"
 #include "src/wiphy.h"
 #include "src/crypto.h"
src/device.c +56 -57

147 lines not on this page · raw message

src/device.h deleted +0 -30
deleted file mode 100644
index 31e472be..00000000
@@ -1,30 +0,0 @@
-/*
- *
- *  Wireless daemon for Linux
- *
- *  Copyright (C) 2013-2019  Intel Corporation. All rights reserved.
- *
- *  This library is free software; you can redistribute it and/or
- *  modify it under the terms of the GNU Lesser General Public
- *  License as published by the Free Software Foundation; either
- *  version 2.1 of the License, or (at your option) any later version.
- *
- *  This library is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- *  Lesser General Public License for more details.
- *
- *  You should have received a copy of the GNU Lesser General Public
- *  License along with this library; if not, write to the Free Software
- *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- *
- */
-
-#include <stdbool.h>
-
-struct wiphy;
-struct netdev;
-struct device;
-
-struct device *device_create(struct wiphy *wiphy, struct netdev *netdev);
-void device_remove(struct device *device);
src/station.c +0 -1
index 76cf20a1..b98aeb63 100644
@@ -37,7 +37,6 @@
 #include "src/iwd.h"
 #include "src/module.h"
 #include "src/common.h"
-#include "src/device.h"
 #include "src/watchlist.h"
 #include "src/scan.h"
 #include "src/netdev.h"
Signature · 1 line
2.20.1
#3 lore raw

PATCH 4/6 rtnlutil: Move rtnl_set_powered from netdev to rtnlutil

In reply to Andrew Zaborowski

This function fits with the other utilities in rtnlutil and netdev.c can slim down.

---
 src/netdev.c   | 46 ++++++++++++----------------------------------
 src/rtnlutil.c | 26 ++++++++++++++++++++++++++
 src/rtnlutil.h |  4 ++++
 3 files changed, 42 insertions(+), 34 deletions(-)
src/netdev.c +12 -34
index 3c1510b9..bdd9dc77 100644
@@ -343,31 +343,6 @@ static void netdev_set_powered_destroy(void *user_data)
 	netdev->set_powered_user_data = NULL;
 }
 
-static uint32_t rtnl_set_powered(int ifindex, bool powered,
-				l_netlink_command_func_t cb, void *user_data,
-				l_netlink_destroy_func_t destroy)
-{
-	struct ifinfomsg *rtmmsg;
-	size_t bufsize;
-	uint32_t id;
-
-	bufsize = NLMSG_ALIGN(sizeof(struct ifinfomsg));
-
-	rtmmsg = l_malloc(bufsize);
-	memset(rtmmsg, 0, bufsize);
-
-	rtmmsg->ifi_family = AF_UNSPEC;
-	rtmmsg->ifi_index = ifindex;
-	rtmmsg->ifi_change = IFF_UP;
-	rtmmsg->ifi_flags = powered ? IFF_UP : 0;
-
-	id = l_netlink_send(rtnl, RTM_SETLINK, 0, rtmmsg, bufsize,
-					cb, user_data, destroy);
-
-	l_free(rtmmsg);
-	return id;
-}
-
 int netdev_set_powered(struct netdev *netdev, bool powered,
 			netdev_command_cb_t callback, void *user_data,
 			netdev_destroy_func_t destroy)
@@ -377,7 +352,7 @@ int netdev_set_powered(struct netdev *netdev, bool powered,
 		return -EBUSY;
 
 	netdev->set_powered_cmd_id =
-		rtnl_set_powered(netdev->index, powered,
+		rtnl_set_powered(rtnl, netdev->index, powered,
 					netdev_set_powered_result, netdev,
 					netdev_set_powered_destroy);
 	if (!netdev->set_powered_cmd_id)
@@ -667,7 +642,7 @@ static void netdev_shutdown_one(void *data, void *user_data)
 	struct netdev *netdev = data;
 
 	if (netdev_get_is_up(netdev))
-		rtnl_set_powered(netdev->index, false, NULL, NULL, NULL);
+		rtnl_set_powered(rtnl, netdev->index, false, NULL, NULL, NULL);
 }
 
 static bool netdev_match(const void *a, const void *b)
@@ -3949,7 +3924,7 @@ static void netdev_set_iftype_cb(struct l_genl_msg *msg, void *user_data)
 		goto done;
 
 	netdev->set_powered_cmd_id =
-			rtnl_set_powered(netdev->index, true,
+			rtnl_set_powered(rtnl, netdev->index, true,
 					netdev_set_iftype_up_cb, req,
 					netdev_set_iftype_request_destroy);
 	if (!netdev->set_powered_cmd_id) {
@@ -4045,7 +4020,7 @@ int netdev_set_iftype(struct netdev *netdev, enum netdev_iftype type,
 		l_genl_msg_unref(msg);
 	} else {
 		netdev->set_powered_cmd_id =
-			rtnl_set_powered(netdev->index, false,
+			rtnl_set_powered(rtnl, netdev->index, false,
 					netdev_set_iftype_down_cb, req,
 					netdev_set_iftype_request_destroy);
 		if (netdev->set_powered_cmd_id)
@@ -4276,8 +4251,8 @@ static void netdev_set_mac_cb(int error, uint16_t type, const void *data,
 			strerror(-error));
 
 	netdev->set_powered_cmd_id =
-		rtnl_set_powered(netdev->index, true, netdev_initial_up_cb,
-					netdev, NULL);
+		rtnl_set_powered(rtnl, netdev->index, true,
+					netdev_initial_up_cb, netdev, NULL);
 }
 
 static void netdev_initial_down_cb(int error, uint16_t type, const void *data,
@@ -4308,8 +4283,8 @@ static void netdev_initial_down_cb(int error, uint16_t type, const void *data,
 	}
 
 	netdev->set_powered_cmd_id =
-		rtnl_set_powered(netdev->index, true, netdev_initial_up_cb,
-					netdev, NULL);
+		rtnl_set_powered(rtnl, netdev->index, true,
+					netdev_initial_up_cb, netdev, NULL);
 }
 
 static void netdev_getlink_cb(int error, uint16_t type, const void *data,
@@ -4367,7 +4342,8 @@ static void netdev_getlink_cb(int error, uint16_t type, const void *data,
 	cb = powered ? netdev_initial_down_cb : netdev_initial_up_cb;
 
 	netdev->set_powered_cmd_id =
-		rtnl_set_powered(ifi->ifi_index, !powered, cb, netdev, NULL);
+		rtnl_set_powered(rtnl, ifi->ifi_index, !powered, cb, netdev,
+					NULL);
 }
 
 static void netdev_frame_watch_free(struct watchlist_item *item)
@@ -4729,6 +4705,8 @@ static void netdev_link_notify(uint16_t type, const void *data, uint32_t len,
 	if (ifi->ifi_type != ARPHRD_ETHER)
 		return;
 
+	l_debug("event %u on ifindex %u", type, ifi->ifi_index);
+
 	bytes = len - NLMSG_ALIGN(sizeof(struct ifinfomsg));
 
 	switch (type) {
src/rtnlutil.c +26 -0
index 833f7103..cd5c11a0 100644
@@ -25,6 +25,7 @@
 #endif
 
 #include <sys/socket.h>
+#include <linux/if.h>
 #include <linux/rtnetlink.h>
 #include <arpa/inet.h>
 
@@ -130,6 +131,31 @@ uint32_t rtnl_set_mac(struct l_netlink *rtnl, int ifindex,
 	return id;
 }
 
+uint32_t rtnl_set_powered(struct l_netlink *rtnl, int ifindex, bool powered,
+				l_netlink_command_func_t cb, void *user_data,
+				l_netlink_destroy_func_t destroy)
+{
+	struct ifinfomsg *rtmmsg;
+	size_t bufsize;
+	uint32_t id;
+
+	bufsize = NLMSG_ALIGN(sizeof(struct ifinfomsg));
+
+	rtmmsg = l_malloc(bufsize);
+	memset(rtmmsg, 0, bufsize);
+
+	rtmmsg->ifi_family = AF_UNSPEC;
+	rtmmsg->ifi_index = ifindex;
+	rtmmsg->ifi_change = IFF_UP;
+	rtmmsg->ifi_flags = powered ? IFF_UP : 0;
+
+	id = l_netlink_send(rtnl, RTM_SETLINK, 0, rtmmsg, bufsize,
+					cb, user_data, destroy);
+	l_free(rtmmsg);
+
+	return id;
+}
+
 void rtnl_ifaddr_extract(const struct ifaddrmsg *ifa, int bytes,
 				char **label, char **ip, char **broadcast)
 {
src/rtnlutil.h +4 -0
index 383d562f..dc053783 100644
@@ -32,6 +32,10 @@ uint32_t rtnl_set_mac(struct l_netlink *rtnl, int ifindex,
 					void *user_data,
 					l_netlink_destroy_func_t destroy);
 
+uint32_t rtnl_set_powered(struct l_netlink *rtnl, int ifindex, bool powered,
+				l_netlink_command_func_t cb, void *user_data,
+				l_netlink_destroy_func_t destroy);
+
 void rtnl_ifaddr_extract(const struct ifaddrmsg *ifa, int bytes,
 				char **label, char **ip, char **broadcast);
 uint32_t rtnl_ifaddr_get(struct l_netlink *rtnl, l_netlink_command_func_t cb,
Signature · 1 line
2.20.1
#4 lore raw

PATCH 5/6 netdev: Power P2P interfaces up like other interfaces

In reply to Andrew Zaborowski

After all we will want to power these iftypes up on init just like station, etc.

---
 src/netdev.c | 5 -----
 1 file changed, 5 deletions(-)
src/netdev.c +0 -5
index bdd9dc77..eb8a7ce8 100644
@@ -4315,11 +4315,6 @@ static void netdev_getlink_cb(int error, uint16_t type, const void *data,
 
 	netdev_newlink_notify(ifi, bytes);
 
-	/* Don't do anything automatically for P2P interfaces */
-	if (netdev->type == NL80211_IFTYPE_P2P_CLIENT ||
-			netdev->type == NL80211_IFTYPE_P2P_GO)
-		return;
-
 	/*
 	 * If the interface is UP, reset it to ensure a clean state.
 	 * Otherwise, if we need to set a random mac, do so.  If not, just
Signature · 1 line
2.20.1
#5 lore raw

PATCH 6/6 netdev: Replace bool randomize_mac with specific address

In reply to Andrew Zaborowski

Allow netdev_create_from_genl callers to draw a random or non-random MAC and pass it in the parameter instead of a bool to tell us to generating the MAC locally. In P2P we are generating the MAC some time before creating the netdev to pass it to the peer during negotiation.

---
 src/manager.c | 22 ++++++++++++++++++----
 src/netdev.c  | 44 +++++++++++++++++++++-----------------------
 src/netdev.h  |  3 ++-
 3 files changed, 41 insertions(+), 28 deletions(-)
src/manager.c +18 -4
index 47874e11..4ad0f03c 100644
@@ -102,6 +102,9 @@ static void wiphy_setup_state_destroy(struct wiphy_setup_state *state)
 
 static bool manager_use_default(struct wiphy_setup_state *state)
 {
+	uint8_t addr_buf[6];
+	uint8_t *addr = NULL;
+
 	l_debug("");
 
 	if (!state->default_if_msg) {
@@ -110,13 +113,20 @@ static bool manager_use_default(struct wiphy_setup_state *state)
 		return false;
 	}
 
-	netdev_create_from_genl(state->default_if_msg, randomize);
+	if (randomize) {
+		wiphy_generate_random_address(state->wiphy, addr_buf);
+		addr = addr_buf;
+	}
+
+	netdev_create_from_genl(state->default_if_msg, addr);
 	return true;
 }
 
 static void manager_new_interface_cb(struct l_genl_msg *msg, void *user_data)
 {
 	struct wiphy_setup_state *state = user_data;
+	uint8_t addr_buf[6];
+	uint8_t *addr = NULL;
 
 	l_debug("");
 
@@ -134,9 +144,13 @@ static void manager_new_interface_cb(struct l_genl_msg *msg, void *user_data)
 		return;
 	}
 
-	netdev_create_from_genl(msg, randomize &&
-					!wiphy_has_feature(state->wiphy,
-						NL80211_FEATURE_MAC_ON_CREATE));
+	if (randomize && !wiphy_has_feature(state->wiphy,
+					NL80211_FEATURE_MAC_ON_CREATE)) {
+		wiphy_generate_random_address(state->wiphy, addr_buf);
+		addr = addr_buf;
+	}
+
+	netdev_create_from_genl(msg, addr);
 }
 
 static void manager_new_interface_done(void *user_data)
src/netdev.c +21 -23

94 lines not on this page · raw message

src/netdev.h +2 -1
index 032265f9..c5b6f139 100644
@@ -205,5 +205,6 @@ uint32_t netdev_station_watch_add(struct netdev *netdev,
 
 bool netdev_station_watch_remove(struct netdev *netdev, uint32_t id);
 
-struct netdev *netdev_create_from_genl(struct l_genl_msg *msg, bool random_mac);
+struct netdev *netdev_create_from_genl(struct l_genl_msg *msg,
+					const uint8_t addr[6]);
 bool netdev_destroy(struct netdev *netdev);
Signature · 1 line
2.20.1
#6 lore raw

Re: [PATCH 6/6] netdev: Replace bool randomize_mac with specific address

Hi Andrew,

Quoted 10 lines · Andrew Zaborowski

Allow netdev_create_from_genl callers to draw a random or non-random MAC and pass it in the parameter instead of a bool to tell us to generating the MAC locally. In P2P we are generating the MAC some time before creating the netdev to pass it to the peer during negotiation.

---
  src/manager.c | 22 ++++++++++++++++++----
  src/netdev.c  | 44 +++++++++++++++++++++-----------------------
  src/netdev.h  |  3 ++-
  3 files changed, 41 insertions(+), 28 deletions(-)

<snip>

src/netdev.c struct netdev { · line 128

Quoted 9 lines
src/netdev.c +1 -0
index eb8a7ce8..eea7267d 100644
@@ -125,6 +125,7 @@ struct netdev {
  	int8_t cur_rssi;
  	struct l_timeout *rssi_poll_timeout;
  	uint32_t rssi_poll_cmd_id;
+	uint8_t *set_mac_once;

So just nit-picking, but using uint8_t set_mac_once[6] would still use less space (at least on 64-bit) and also skip mallocing/freeing 6 byte array...

Set it to all zeros or something to act as a NULL value

Quoted 102 lines
src/netdev.c +20 -23
  	uint32_t set_powered_cmd_id;
  	netdev_command_cb_t set_powered_cb;
@@ -147,7 +148,6 @@ struct netdev {
  	bool ignore_connect_event : 1;
  	bool expect_connect_failure : 1;
  	bool aborting : 1;
-	bool mac_randomize_once : 1;
  	bool events_ready : 1;
  };
 
@@ -634,6 +634,7 @@ static void netdev_free(void *data)
 
  	l_io_destroy(netdev->pae_io);
 
+	l_free(netdev->set_mac_once);
  	l_free(netdev);
  }
 
@@ -4255,6 +4256,21 @@ static void netdev_set_mac_cb(int error, uint16_t type, const void *data,
  					netdev_initial_up_cb, netdev, NULL);
  }
 
+static bool netdev_check_set_mac(struct netdev *netdev)
+{
+	if (!netdev->set_mac_once)
+		return false;
+
+	l_debug("Setting initial address on ifindex: %d to: " MAC,
+		netdev->index, MAC_STR(netdev->set_mac_once));
+	netdev->set_powered_cmd_id =
+		rtnl_set_mac(rtnl, netdev->index, netdev->set_mac_once,
+				netdev_set_mac_cb, netdev, NULL);
+	l_free(netdev->set_mac_once);
+	netdev->set_mac_once = NULL;
+	return true;
+}
+
  static void netdev_initial_down_cb(int error, uint16_t type, const void *data,
  					uint32_t len, void *user_data)
  {
@@ -4270,17 +4286,8 @@ static void netdev_initial_down_cb(int error, uint16_t type, const void *data,
  		return;
  	}
 
-	if (netdev->mac_randomize_once) {
-		uint8_t addr[ETH_ALEN];
-
-		wiphy_generate_random_address(netdev->wiphy, addr);
-		l_debug("Setting initial random address on "
-			"ifindex: %d to: "MAC, netdev->index, MAC_STR(addr));
-		netdev->set_powered_cmd_id =
-			rtnl_set_mac(rtnl, netdev->index, addr,
-					netdev_set_mac_cb, netdev, NULL);
+	if (netdev_check_set_mac(netdev))
  		return;
-	}
 
  	netdev->set_powered_cmd_id =
  		rtnl_set_powered(rtnl, netdev->index, true,
@@ -4322,17 +4329,8 @@ static void netdev_getlink_cb(int error, uint16_t type, const void *data,
  	 */
  	powered = netdev_get_is_up(netdev);
 
-	if (!powered && netdev->mac_randomize_once) {
-		uint8_t addr[ETH_ALEN];
-
-		wiphy_generate_random_address(netdev->wiphy, addr);
-		l_debug("Setting initial random address on "
-			"ifindex: %d to: "MAC, netdev->index, MAC_STR(addr));
-		netdev->set_powered_cmd_id =
-			rtnl_set_mac(rtnl, netdev->index, addr,
-					netdev_set_mac_cb, netdev, NULL);
+	if (!powered && netdev_check_set_mac(netdev))
  		return;
-	}
 
  	cb = powered ? netdev_initial_down_cb : netdev_initial_up_cb;
 
@@ -4507,7 +4505,7 @@ error:
  	return NULL;
  }
 
-struct netdev *netdev_create_from_genl(struct l_genl_msg *msg, bool random_mac)
+struct netdev *netdev_create_from_genl(struct l_genl_msg *msg, const uint8_t set_mac[6])
  {
  	struct l_genl_attr attr;
  	uint16_t type, len;
@@ -4624,7 +4622,7 @@ struct netdev *netdev_create_from_genl(struct l_genl_msg *msg, bool random_mac)
  	memcpy(netdev->name, ifname, ifname_len);
  	netdev->wiphy = wiphy;
  	netdev->pae_over_nl80211 = pae_io == NULL;
-	netdev->mac_randomize_once = random_mac;
+	netdev->set_mac_once = set_mac ? l_memdup(set_mac, 6) : NULL;
 
  	if (pae_io) {
  		netdev->pae_io = pae_io;
src/netdev.h +0 -0
index 032265f9..c5b6f139 100644

src/netdev.h uint32_t netdev_station_watch_add(struct netdev *netdev, · line 209

Quoted 6 lines
src/netdev.h +2 -1
@@ -205,5 +205,6 @@ uint32_t netdev_station_watch_add(struct netdev *netdev,
 
  bool netdev_station_watch_remove(struct netdev *netdev, uint32_t id);
 
-struct netdev *netdev_create_from_genl(struct l_genl_msg *msg, bool random_mac);
+struct netdev *netdev_create_from_genl(struct l_genl_msg *msg,
+					const uint8_t addr[6]);

Nitpicking again. This syntax is pretty useless since addr is treated like a const uint8_t *. It only serves to confuse the reader. We should not be using this form at all.

Use const uint8_t addr[static 6] instead.

src/netdev.h uint32_t netdev_station_watch_add(struct netdev *netdev, · line 210

Quoted 2 lines
src/netdev.h +0 -0
  bool netdev_destroy(struct netdev *netdev);

Regards,
-Denis

#7 lore raw

Re: [PATCH 6/6] netdev: Replace bool randomize_mac with specific address

Hi Denis,

Quoted 28 lines · Denis Kenzior
Quoted 10 lines · Andrew Zaborowski

Allow netdev_create_from_genl callers to draw a random or non-random MAC and pass it in the parameter instead of a bool to tell us to generating the MAC locally. In P2P we are generating the MAC some time before creating the netdev to pass it to the peer during negotiation.

---
  src/manager.c | 22 ++++++++++++++++++----
  src/netdev.c  | 44 +++++++++++++++++++++-----------------------
  src/netdev.h  |  3 ++-
  3 files changed, 41 insertions(+), 28 deletions(-)

<snip>

src/netdev.c struct netdev { · line 128

Quoted 9 lines
src/netdev.c +1 -0
index eb8a7ce8..eea7267d 100644
@@ -125,6 +125,7 @@ struct netdev {
      int8_t cur_rssi;
      struct l_timeout *rssi_poll_timeout;
      uint32_t rssi_poll_cmd_id;
+     uint8_t *set_mac_once;

So just nit-picking, but using uint8_t set_mac_once[6] would still use less space (at least on 64-bit) and also skip mallocing/freeing 6 byte array...

Quoted 1 line

Set it to all zeros or something to act as a NULL value

Ok.

Quoted 114 lines
Quoted 103 lines
src/netdev.c +20 -23
      uint32_t set_powered_cmd_id;
      netdev_command_cb_t set_powered_cb;
@@ -147,7 +148,6 @@ struct netdev {
      bool ignore_connect_event : 1;
      bool expect_connect_failure : 1;
      bool aborting : 1;
-     bool mac_randomize_once : 1;
      bool events_ready : 1;
  };
 
@@ -634,6 +634,7 @@ static void netdev_free(void *data)
 
      l_io_destroy(netdev->pae_io);
 
+     l_free(netdev->set_mac_once);
      l_free(netdev);
  }
 
@@ -4255,6 +4256,21 @@ static void netdev_set_mac_cb(int error, uint16_t type, const void *data,
                                      netdev_initial_up_cb, netdev, NULL);
  }
 
+static bool netdev_check_set_mac(struct netdev *netdev)
+{
+     if (!netdev->set_mac_once)
+             return false;
+
+     l_debug("Setting initial address on ifindex: %d to: " MAC,
+             netdev->index, MAC_STR(netdev->set_mac_once));
+     netdev->set_powered_cmd_id =
+             rtnl_set_mac(rtnl, netdev->index, netdev->set_mac_once,
+                             netdev_set_mac_cb, netdev, NULL);
+     l_free(netdev->set_mac_once);
+     netdev->set_mac_once = NULL;
+     return true;
+}
+
  static void netdev_initial_down_cb(int error, uint16_t type, const void *data,
                                      uint32_t len, void *user_data)
  {
@@ -4270,17 +4286,8 @@ static void netdev_initial_down_cb(int error, uint16_t type, const void *data,
              return;
      }
 
-     if (netdev->mac_randomize_once) {
-             uint8_t addr[ETH_ALEN];
-
-             wiphy_generate_random_address(netdev->wiphy, addr);
-             l_debug("Setting initial random address on "
-                     "ifindex: %d to: "MAC, netdev->index, MAC_STR(addr));
-             netdev->set_powered_cmd_id =
-                     rtnl_set_mac(rtnl, netdev->index, addr,
-                                     netdev_set_mac_cb, netdev, NULL);
+     if (netdev_check_set_mac(netdev))
              return;
-     }
 
      netdev->set_powered_cmd_id =
              rtnl_set_powered(rtnl, netdev->index, true,
@@ -4322,17 +4329,8 @@ static void netdev_getlink_cb(int error, uint16_t type, const void *data,
       */
      powered = netdev_get_is_up(netdev);
 
-     if (!powered && netdev->mac_randomize_once) {
-             uint8_t addr[ETH_ALEN];
-
-             wiphy_generate_random_address(netdev->wiphy, addr);
-             l_debug("Setting initial random address on "
-                     "ifindex: %d to: "MAC, netdev->index, MAC_STR(addr));
-             netdev->set_powered_cmd_id =
-                     rtnl_set_mac(rtnl, netdev->index, addr,
-                                     netdev_set_mac_cb, netdev, NULL);
+     if (!powered && netdev_check_set_mac(netdev))
              return;
-     }
 
      cb = powered ? netdev_initial_down_cb : netdev_initial_up_cb;
 
@@ -4507,7 +4505,7 @@ error:
      return NULL;
  }
 
-struct netdev *netdev_create_from_genl(struct l_genl_msg *msg, bool random_mac)
+struct netdev *netdev_create_from_genl(struct l_genl_msg *msg, const uint8_t set_mac[6])
  {
      struct l_genl_attr attr;
      uint16_t type, len;
@@ -4624,7 +4622,7 @@ struct netdev *netdev_create_from_genl(struct l_genl_msg *msg, bool random_mac)
      memcpy(netdev->name, ifname, ifname_len);
      netdev->wiphy = wiphy;
      netdev->pae_over_nl80211 = pae_io == NULL;
-     netdev->mac_randomize_once = random_mac;
+     netdev->set_mac_once = set_mac ? l_memdup(set_mac, 6) : NULL;
 
      if (pae_io) {
              netdev->pae_io = pae_io;
src/netdev.h +0 -0
index 032265f9..c5b6f139 100644

src/netdev.h uint32_t netdev_station_watch_add(struct netdev *netdev, · line 209

Quoted 6 lines
src/netdev.h +2 -1
@@ -205,5 +205,6 @@ uint32_t netdev_station_watch_add(struct netdev *netdev,
 
  bool netdev_station_watch_remove(struct netdev *netdev, uint32_t id);
 
-struct netdev *netdev_create_from_genl(struct l_genl_msg *msg, bool random_mac);
+struct netdev *netdev_create_from_genl(struct l_genl_msg *msg,
+                                     const uint8_t addr[6]);

Nitpicking again. This syntax is pretty useless since addr is treated like a const uint8_t *. It only serves to confuse the reader. We should not be using this form at all.

Quoted 1 line

Use const uint8_t addr[static 6] instead.

This seems to prohibit passing a NULL, so we'd also need to pass all-zero arrays from the caller. I think I'd rather use const uint8_t *addr then.

Best regards

#8 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 10 lines · Andrew Zaborowski

Check the iftype before registering ANQP on new interface.

Not that the check here and in rrm.c (which already checks the iftype) may need to be extended to run on NETDEV_WATCH_EVENT_UP because a device could be created with a different iftype and then have the iftype changed before powering up.

---
  src/anqp.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

Patches 1-5 applied, thanks.

Regards,
-Denis