eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

netdev.c (175094B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2013-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #define _GNU_SOURCE
     28 #include <stdlib.h>
     29 #include <alloca.h>
     30 #include <stdio.h>
     31 #include <linux/rtnetlink.h>
     32 #include <net/if_arp.h>
     33 #include <linux/if.h>
     34 #include <linux/if_packet.h>
     35 #include <linux/if_ether.h>
     36 #include <linux/filter.h>
     37 #include <arpa/inet.h>
     38 #include <sys/socket.h>
     39 #include <errno.h>
     40 
     41 #include <ell/ell.h>
     42 
     43 #include "ell/useful.h"
     44 
     45 #include "linux/nl80211.h"
     46 
     47 #include "src/iwd.h"
     48 #include "src/module.h"
     49 #include "src/wiphy.h"
     50 #include "src/ie.h"
     51 #include "src/mpdu.h"
     52 #include "src/eapol.h"
     53 #include "src/handshake.h"
     54 #include "src/crypto.h"
     55 #include "src/scan.h"
     56 #include "src/netdev.h"
     57 #include "src/ft.h"
     58 #include "src/util.h"
     59 #include "src/watchlist.h"
     60 #include "src/sae.h"
     61 #include "src/nl80211util.h"
     62 #include "src/nl80211cmd.h"
     63 #include "src/owe.h"
     64 #include "src/fils.h"
     65 #include "src/auth-proto.h"
     66 #include "src/frame-xchg.h"
     67 #include "src/diagnostic.h"
     68 #include "src/band.h"
     69 #include "src/pmksa.h"
     70 
     71 #ifndef ENOTSUPP
     72 #define ENOTSUPP 524
     73 #endif
     74 
     75 enum connection_type {
     76 	CONNECTION_TYPE_SOFTMAC,
     77 	CONNECTION_TYPE_FULLMAC,
     78 	CONNECTION_TYPE_SAE_OFFLOAD,
     79 	CONNECTION_TYPE_PSK_OFFLOAD,
     80 	CONNECTION_TYPE_8021X_OFFLOAD,
     81 };
     82 
     83 static uint32_t unicast_watch;
     84 
     85 struct netdev_handshake_state {
     86 	struct handshake_state super;
     87 	uint32_t pairwise_new_key_cmd_id;
     88 	uint32_t group_new_key_cmd_id;
     89 	uint32_t group_management_new_key_cmd_id;
     90 	uint32_t set_station_cmd_id;
     91 	uint32_t set_pmk_cmd_id;
     92 	uint32_t pairwise_set_key_tx_cmd_id;
     93 	bool ptk_installed;
     94 	bool gtk_installed;
     95 	bool igtk_installed;
     96 	bool complete;
     97 	struct netdev *netdev;
     98 	enum connection_type type;
     99 };
    100 
    101 struct netdev_ext_key_info {
    102 	uint16_t proto;
    103 	bool noencrypt;
    104 	struct eapol_frame frame[];
    105 };
    106 
    107 struct netdev {
    108 	uint32_t index;
    109 	uint64_t wdev_id;
    110 	char name[IFNAMSIZ];
    111 	uint32_t type;
    112 	uint8_t addr[ETH_ALEN];
    113 	struct wiphy *wiphy;
    114 	unsigned int ifi_flags;
    115 	uint32_t frequency;
    116 
    117 	netdev_event_func_t event_filter;
    118 	netdev_connect_cb_t connect_cb;
    119 	netdev_disconnect_cb_t disconnect_cb;
    120 	netdev_neighbor_report_cb_t neighbor_report_cb;
    121 	netdev_command_cb_t adhoc_cb;
    122 	void *user_data;
    123 	struct eapol_sm *sm;
    124 	struct auth_proto *ap;
    125 	struct owe_sm *owe_sm;
    126 	struct handshake_state *handshake;
    127 	uint32_t connect_cmd_id;
    128 	uint32_t disconnect_cmd_id;
    129 	uint32_t join_adhoc_cmd_id;
    130 	uint32_t leave_adhoc_cmd_id;
    131 	uint32_t set_interface_cmd_id;
    132 	uint32_t rekey_offload_cmd_id;
    133 	uint32_t qos_map_cmd_id;
    134 	uint32_t mac_change_cmd_id;
    135 	uint32_t get_oci_cmd_id;
    136 	uint32_t get_link_cmd_id;
    137 	uint32_t power_save_cmd_id;
    138 	uint32_t set_cqm_cmd_id;
    139 	enum netdev_result result;
    140 	uint16_t last_code; /* reason or status, depending on result */
    141 	struct l_timeout *neighbor_report_timeout;
    142 	struct l_timeout *sa_query_timeout;
    143 	struct l_timeout *sa_query_delay;
    144 	struct l_timeout *group_handshake_timeout;
    145 	uint16_t sa_query_id;
    146 	int8_t rssi_levels[16];
    147 	uint8_t rssi_levels_num;
    148 	uint8_t cur_rssi_level_idx;
    149 	int8_t cur_rssi;
    150 	struct l_timeout *rssi_poll_timeout;
    151 	uint32_t rssi_poll_cmd_id;
    152 	uint8_t set_mac_once[6];
    153 
    154 	struct scan_bss *fw_roam_bss;
    155 
    156 	uint32_t set_powered_cmd_id;
    157 	netdev_command_cb_t set_powered_cb;
    158 	void *set_powered_user_data;
    159 	netdev_destroy_func_t set_powered_destroy;
    160 
    161 	uint32_t get_station_cmd_id;
    162 	netdev_get_station_cb_t get_station_cb;
    163 	void *get_station_data;
    164 	netdev_destroy_func_t get_station_destroy;
    165 
    166 	struct l_idle *disconnect_idle;
    167 
    168 	struct watchlist station_watches;
    169 
    170 	struct l_io *pae_io;  /* for drivers without EAPoL over NL80211 */
    171 
    172 	struct l_genl_msg *connect_cmd;
    173 	struct l_genl_msg *auth_cmd;
    174 	struct wiphy_radio_work_item work;
    175 
    176 	struct netdev_ext_key_info *ext_key_info;
    177 
    178 	int low_signal_threshold;
    179 	int low_signal_threshold_5ghz;
    180 
    181 	bool connected : 1;
    182 	bool associated : 1;
    183 	bool operational : 1;
    184 	bool rekey_offload_support : 1;
    185 	bool pae_over_nl80211 : 1;
    186 	bool in_ft : 1;
    187 	bool cur_rssi_low : 1;
    188 	bool use_4addr : 1;
    189 	bool ignore_connect_event : 1;
    190 	bool expect_connect_failure : 1;
    191 	bool aborting : 1;
    192 	bool events_ready : 1;
    193 	bool retry_auth : 1;
    194 	bool in_reassoc : 1;
    195 	bool privacy : 1;
    196 	bool cqm_poll_fallback : 1;
    197 	bool external_auth : 1;
    198 };
    199 
    200 struct netdev_preauth_state {
    201 	netdev_preauthenticate_cb_t cb;
    202 	void *user_data;
    203 	struct netdev *netdev;
    204 };
    205 
    206 struct netdev_watch {
    207 	uint32_t id;
    208 	netdev_watch_func_t callback;
    209 	void *user_data;
    210 };
    211 
    212 static struct l_netlink *rtnl = NULL;
    213 static struct l_genl_family *nl80211;
    214 static struct l_queue *netdev_list;
    215 static struct watchlist netdev_watches;
    216 static bool mac_per_ssid;
    217 /* Threshold RSSI for roaming to trigger, configurable in main.conf */
    218 static int LOW_SIGNAL_THRESHOLD;
    219 static int LOW_SIGNAL_THRESHOLD_5GHZ;
    220 static int CRITICAL_SIGNAL_THRESHOLD;
    221 static int CRITICAL_SIGNAL_THRESHOLD_5GHZ;
    222 
    223 static unsigned int iov_ie_append(struct iovec *iov,
    224 					unsigned int n_iov, unsigned int c,
    225 					const uint8_t *ie, size_t len)
    226 {
    227 	if (L_WARN_ON(c >= n_iov))
    228 		return n_iov;
    229 
    230 	if (!ie)
    231 		return c;
    232 
    233 	iov[c].iov_base = (void *) ie;
    234 	iov[c].iov_len = len;
    235 
    236 	return c + 1u;
    237 }
    238 
    239 const char *netdev_iftype_to_string(uint32_t iftype)
    240 {
    241 	switch (iftype) {
    242 	case NL80211_IFTYPE_ADHOC:
    243 		return "ad-hoc";
    244 	case NL80211_IFTYPE_STATION:
    245 		return "station";
    246 	case NL80211_IFTYPE_AP:
    247 		return "ap";
    248 	case NL80211_IFTYPE_P2P_CLIENT:
    249 		return "p2p-client";
    250 	case NL80211_IFTYPE_P2P_GO:
    251 		return "p2p-go";
    252 	case NL80211_IFTYPE_P2P_DEVICE:
    253 		return "p2p-device";
    254 	default:
    255 		break;
    256 	}
    257 
    258 	return NULL;
    259 }
    260 
    261 static inline bool is_offload(struct handshake_state *hs)
    262 {
    263 	struct netdev_handshake_state *nhs =
    264 		l_container_of(hs, struct netdev_handshake_state, super);
    265 
    266 	if (!nhs)
    267 		return false;
    268 
    269 	switch (nhs->type) {
    270 	case CONNECTION_TYPE_SOFTMAC:
    271 	case CONNECTION_TYPE_FULLMAC:
    272 	/*
    273 	 * 8021x offload does not quite fit into the same category of PSK
    274 	 * offload. First the netdev_connect_event comes prior to EAP meaning
    275 	 * the handshake is not done at this point. In addition it still
    276 	 * requires EAP take place in userspace meaning IWD needs an eapol_sm.
    277 	 * Because of this, and our prior use of 'is_offload', it does not fit
    278 	 * into the same category and will need to be handled specially.
    279 	 */
    280 	case CONNECTION_TYPE_8021X_OFFLOAD:
    281 		return false;
    282 	case CONNECTION_TYPE_SAE_OFFLOAD:
    283 	case CONNECTION_TYPE_PSK_OFFLOAD:
    284 		return true;
    285 	}
    286 
    287 	return false;
    288 }
    289 
    290 static unsigned int netdev_populate_common_ies(struct netdev *netdev,
    291 					struct handshake_state *hs,
    292 					struct l_genl_msg *msg,
    293 					struct iovec *iov,
    294 					unsigned int n_iov,
    295 					unsigned int c_iov)
    296 {
    297 	const uint8_t *extended_capabilities;
    298 	const uint8_t *rm_enabled_capabilities;
    299 
    300 	extended_capabilities = wiphy_get_extended_capabilities(netdev->wiphy,
    301 								netdev->type);
    302 	c_iov = iov_ie_append(iov, n_iov, c_iov, extended_capabilities,
    303 				IE_LEN(extended_capabilities));
    304 
    305 	rm_enabled_capabilities =
    306 		wiphy_get_rm_enabled_capabilities(netdev->wiphy);
    307 	c_iov = iov_ie_append(iov, n_iov, c_iov, rm_enabled_capabilities,
    308 				IE_LEN(rm_enabled_capabilities));
    309 
    310 	if (rm_enabled_capabilities)
    311 		l_genl_msg_append_attr(msg, NL80211_ATTR_USE_RRM, 0, NULL);
    312 
    313 	c_iov = iov_ie_append(iov, n_iov, c_iov,
    314 				hs->vendor_ies, hs->vendor_ies_len);
    315 
    316 	c_iov = iov_ie_append(iov, n_iov, c_iov, hs->fils_ip_req_ie,
    317 				IE_LEN(hs->fils_ip_req_ie));
    318 
    319 	return c_iov;
    320 }
    321 
    322 /* Cancels ongoing GTK/IGTK related commands (if any) */
    323 static void netdev_handshake_state_cancel_rekey(
    324 					struct netdev_handshake_state *nhs)
    325 {
    326 	if (nhs->group_new_key_cmd_id) {
    327 		l_genl_family_cancel(nl80211, nhs->group_new_key_cmd_id);
    328 		nhs->group_new_key_cmd_id = 0;
    329 	}
    330 
    331 	if (nhs->group_management_new_key_cmd_id) {
    332 		l_genl_family_cancel(nl80211,
    333 					nhs->group_management_new_key_cmd_id);
    334 		nhs->group_management_new_key_cmd_id = 0;
    335 	}
    336 }
    337 
    338 static void netdev_handshake_state_cancel_all(
    339 					struct netdev_handshake_state *nhs)
    340 {
    341 	if (nhs->pairwise_new_key_cmd_id) {
    342 		l_genl_family_cancel(nl80211, nhs->pairwise_new_key_cmd_id);
    343 		nhs->pairwise_new_key_cmd_id = 0;
    344 	}
    345 
    346 	netdev_handshake_state_cancel_rekey(nhs);
    347 
    348 	if (nhs->set_station_cmd_id) {
    349 		l_genl_family_cancel(nl80211, nhs->set_station_cmd_id);
    350 		nhs->set_station_cmd_id = 0;
    351 	}
    352 
    353 	if (nhs->set_pmk_cmd_id) {
    354 		l_genl_family_cancel(nl80211, nhs->set_pmk_cmd_id);
    355 		nhs->set_pmk_cmd_id = 0;
    356 	}
    357 
    358 	if (nhs->pairwise_set_key_tx_cmd_id) {
    359 		l_genl_family_cancel(nl80211, nhs->pairwise_set_key_tx_cmd_id);
    360 		nhs->pairwise_set_key_tx_cmd_id = 0;
    361 	}
    362 }
    363 
    364 static void netdev_handshake_state_free(struct handshake_state *hs)
    365 {
    366 	struct netdev_handshake_state *nhs =
    367 		l_container_of(hs, struct netdev_handshake_state, super);
    368 
    369 	netdev_handshake_state_cancel_all(nhs);
    370 	l_free(nhs);
    371 }
    372 
    373 struct handshake_state *netdev_handshake_state_new(struct netdev *netdev)
    374 {
    375 	struct netdev_handshake_state *nhs;
    376 
    377 	nhs = l_new(struct netdev_handshake_state, 1);
    378 
    379 	nhs->super.ifindex = netdev->index;
    380 	nhs->super.free = netdev_handshake_state_free;
    381 	nhs->super.refcount = 1;
    382 
    383 	nhs->netdev = netdev;
    384 	/*
    385 	 * Since GTK/IGTK are optional (NO_GROUP_TRAFFIC), we set them as
    386 	 * 'installed' upon initialization. If/When the gtk/igtk callback is
    387 	 * called they will get set to false until we have received a successful
    388 	 * callback from nl80211. From these callbacks we can check that all
    389 	 * the keys have been installed, and only then trigger the handshake
    390 	 * complete callback.
    391 	 */
    392 	nhs->gtk_installed = true;
    393 	nhs->igtk_installed = true;
    394 
    395 	return &nhs->super;
    396 }
    397 
    398 struct wiphy *netdev_get_wiphy(struct netdev *netdev)
    399 {
    400 	return netdev->wiphy;
    401 }
    402 
    403 const uint8_t *netdev_get_address(struct netdev *netdev)
    404 {
    405 	return netdev->addr;
    406 }
    407 
    408 uint32_t netdev_get_ifindex(struct netdev *netdev)
    409 {
    410 	return netdev->index;
    411 }
    412 
    413 uint64_t netdev_get_wdev_id(struct netdev *netdev)
    414 {
    415 	return netdev->wdev_id;
    416 }
    417 
    418 enum netdev_iftype netdev_get_iftype(struct netdev *netdev)
    419 {
    420 	return netdev->type;
    421 }
    422 
    423 const char *netdev_get_name(struct netdev *netdev)
    424 {
    425 	return netdev->name;
    426 }
    427 
    428 bool netdev_foreach_name(bool (*visit)(const char *, void *), void *data)
    429 {
    430 	const struct l_queue_entry *e;
    431 
    432 	for (e = l_queue_get_entries(netdev_list); e; e = e->next) {
    433 		struct netdev *netdev = e->data;
    434 
    435 		if (!netdev->events_ready || netdev->type == NETDEV_IFTYPE_P2P_CLIENT ||
    436 				netdev->type == NETDEV_IFTYPE_P2P_GO)
    437 			continue;
    438 		if (!visit(netdev->name, data))
    439 			return false;
    440 	}
    441 	return true;
    442 }
    443 
    444 bool netdev_get_is_up(struct netdev *netdev)
    445 {
    446 	bool powered = (netdev->ifi_flags & IFF_UP) != 0;
    447 
    448 	/*
    449 	 * If we are in the middle of changing the MAC we are in somewhat of a
    450 	 * no mans land. Technically the iface may be down, but since we are
    451 	 * not emitting any netdev DOWN events we want netdev_get_is_up to
    452 	 * reflect the same state. Once MAC changing finishes any pending
    453 	 * DOWN events will be emitted.
    454 	 */
    455 	if (netdev->mac_change_cmd_id && !powered)
    456 		return true;
    457 
    458 	return powered;
    459 }
    460 
    461 struct handshake_state *netdev_get_handshake(struct netdev *netdev)
    462 {
    463 	return netdev->handshake;
    464 }
    465 
    466 const char *netdev_get_path(struct netdev *netdev)
    467 {
    468 	static char path[256];
    469 
    470 	L_WARN_ON(snprintf(path, sizeof(path), "%s/%u",
    471 				wiphy_get_path(netdev->wiphy),
    472 				netdev->index) >= (int) sizeof(path));
    473 	path[sizeof(path) - 1] = '\0';
    474 
    475 	return path;
    476 }
    477 
    478 uint8_t netdev_get_rssi_level_idx(struct netdev *netdev)
    479 {
    480 	return netdev->cur_rssi_level_idx;
    481 }
    482 
    483 int netdev_get_low_signal_threshold(uint32_t frequency)
    484 {
    485 	if (frequency > 4000)
    486 		return LOW_SIGNAL_THRESHOLD_5GHZ;
    487 
    488 	return LOW_SIGNAL_THRESHOLD;
    489 }
    490 
    491 static void netdev_set_powered_result(int error, uint16_t type,
    492 					const void *data,
    493 					uint32_t len, void *user_data)
    494 {
    495 	struct netdev *netdev = user_data;
    496 
    497 	if (netdev->set_powered_cb)
    498 		netdev->set_powered_cb(netdev, error,
    499 						netdev->set_powered_user_data);
    500 
    501 	netdev->set_powered_cb = NULL;
    502 }
    503 
    504 static void netdev_set_powered_destroy(void *user_data)
    505 {
    506 	struct netdev *netdev = user_data;
    507 
    508 	netdev->set_powered_cmd_id = 0;
    509 
    510 	if (netdev->set_powered_destroy)
    511 		netdev->set_powered_destroy(netdev->set_powered_user_data);
    512 
    513 	netdev->set_powered_destroy = NULL;
    514 	netdev->set_powered_user_data = NULL;
    515 }
    516 
    517 int netdev_set_powered(struct netdev *netdev, bool powered,
    518 			netdev_command_cb_t callback, void *user_data,
    519 			netdev_destroy_func_t destroy)
    520 {
    521 	if (netdev->set_powered_cmd_id ||
    522 			netdev->set_interface_cmd_id)
    523 		return -EBUSY;
    524 
    525 	netdev->set_powered_cmd_id =
    526 		l_rtnl_set_powered(rtnl, netdev->index, powered,
    527 					netdev_set_powered_result, netdev,
    528 					netdev_set_powered_destroy);
    529 	if (!netdev->set_powered_cmd_id)
    530 		return -EIO;
    531 
    532 	netdev->set_powered_cb = callback;
    533 	netdev->set_powered_user_data = user_data;
    534 	netdev->set_powered_destroy = destroy;
    535 
    536 	return 0;
    537 }
    538 
    539 static bool netdev_parse_bitrate(struct l_genl_attr *attr,
    540 					enum diagnostic_mcs_type *type_out,
    541 					uint32_t *rate_out,
    542 					uint8_t *mcs_out)
    543 {
    544 	uint16_t type, len;
    545 	const void *data;
    546 	uint32_t rate = 0;
    547 	uint8_t mcs = 0;
    548 	enum diagnostic_mcs_type mcs_type = DIAGNOSTIC_MCS_TYPE_NONE;
    549 
    550 	while (l_genl_attr_next(attr, &type, &len, &data)) {
    551 		switch (type) {
    552 		case NL80211_RATE_INFO_BITRATE32:
    553 			if (len != 4)
    554 				return false;
    555 
    556 			rate = l_get_u32(data);
    557 
    558 			break;
    559 
    560 		case NL80211_RATE_INFO_MCS:
    561 			if (len != 1)
    562 				return false;
    563 
    564 			mcs = l_get_u8(data);
    565 			mcs_type = DIAGNOSTIC_MCS_TYPE_HT;
    566 
    567 			break;
    568 
    569 		case NL80211_RATE_INFO_VHT_MCS:
    570 			if (len != 1)
    571 				return false;
    572 
    573 			mcs = l_get_u8(data);
    574 			mcs_type = DIAGNOSTIC_MCS_TYPE_VHT;
    575 
    576 			break;
    577 
    578 		case NL80211_RATE_INFO_HE_MCS:
    579 			if (len != 1)
    580 				return false;
    581 
    582 			mcs = l_get_u8(data);
    583 			mcs_type = DIAGNOSTIC_MCS_TYPE_HE;
    584 
    585 			break;
    586 		}
    587 	}
    588 
    589 	if (!rate)
    590 		return false;
    591 
    592 	*type_out = mcs_type;
    593 	*rate_out = rate;
    594 
    595 	if (mcs_type != DIAGNOSTIC_MCS_TYPE_NONE)
    596 		*mcs_out = mcs;
    597 
    598 	return true;
    599 }
    600 
    601 static bool netdev_parse_sta_info(struct l_genl_attr *attr,
    602 					struct diagnostic_station_info *info)
    603 {
    604 	uint16_t type, len;
    605 	const void *data;
    606 	struct l_genl_attr nested;
    607 
    608 	while (l_genl_attr_next(attr, &type, &len, &data)) {
    609 		switch (type) {
    610 		case NL80211_STA_INFO_SIGNAL:
    611 			if (len != 1)
    612 				return false;
    613 
    614 			info->cur_rssi = *(const int8_t *) data;
    615 			info->have_cur_rssi = true;
    616 
    617 			break;
    618 		case NL80211_STA_INFO_SIGNAL_AVG:
    619 			if (len != 1)
    620 				return false;
    621 
    622 			info->avg_rssi = *(const int8_t *) data;
    623 			info->have_avg_rssi = true;
    624 
    625 			break;
    626 		case NL80211_STA_INFO_RX_BITRATE:
    627 			if (!l_genl_attr_recurse(attr, &nested))
    628 				return false;
    629 
    630 			if (!netdev_parse_bitrate(&nested, &info->rx_mcs_type,
    631 							&info->rx_bitrate,
    632 							&info->rx_mcs))
    633 				continue;
    634 
    635 			info->have_rx_bitrate = true;
    636 
    637 			if (info->rx_mcs_type != DIAGNOSTIC_MCS_TYPE_NONE)
    638 				info->have_rx_mcs = true;
    639 
    640 			break;
    641 
    642 		case NL80211_STA_INFO_TX_BITRATE:
    643 			if (!l_genl_attr_recurse(attr, &nested))
    644 				return false;
    645 
    646 			if (!netdev_parse_bitrate(&nested, &info->tx_mcs_type,
    647 							&info->tx_bitrate,
    648 							&info->tx_mcs))
    649 				continue;
    650 
    651 			info->have_tx_bitrate = true;
    652 
    653 			if (info->tx_mcs_type != DIAGNOSTIC_MCS_TYPE_NONE)
    654 				info->have_tx_mcs = true;
    655 
    656 			break;
    657 		case NL80211_STA_INFO_EXPECTED_THROUGHPUT:
    658 			if (len != 4)
    659 				return false;
    660 
    661 			info->expected_throughput = l_get_u32(data);
    662 			info->have_expected_throughput = true;
    663 
    664 			break;
    665 		case NL80211_STA_INFO_INACTIVE_TIME:
    666 			if (len != 4)
    667 				return false;
    668 
    669 			info->inactive_time = l_get_u32(data);
    670 			info->have_inactive_time = true;
    671 
    672 			break;
    673 		case NL80211_STA_INFO_CONNECTED_TIME:
    674 			if (len != 4)
    675 				return false;
    676 
    677 			info->connected_time = l_get_u32(data);
    678 			info->have_connected_time = true;
    679 
    680 			break;
    681 		}
    682 	}
    683 
    684 	return true;
    685 }
    686 
    687 static void netdev_set_rssi_level_idx(struct netdev *netdev)
    688 {
    689 	uint8_t new_level;
    690 
    691 	for (new_level = 0; new_level < netdev->rssi_levels_num; new_level++)
    692 		if (netdev->cur_rssi >= netdev->rssi_levels[new_level])
    693 			break;
    694 
    695 	netdev->cur_rssi_level_idx = new_level;
    696 }
    697 
    698 static void netdev_cqm_event_rssi_value(struct netdev *netdev, int rssi_val)
    699 {
    700 	bool new_rssi_low;
    701 	uint8_t prev_rssi_level_idx = netdev->cur_rssi_level_idx;
    702 	int threshold = netdev->frequency > 4000 ?
    703 					netdev->low_signal_threshold_5ghz :
    704 					netdev->low_signal_threshold;
    705 
    706 	if (!netdev->connected)
    707 		return;
    708 
    709 	if (rssi_val > 127)
    710 		rssi_val = 127;
    711 	else if (rssi_val < -127)
    712 		rssi_val = -127;
    713 
    714 	netdev->cur_rssi = rssi_val;
    715 
    716 	if (!netdev->event_filter)
    717 		return;
    718 
    719 	new_rssi_low = rssi_val < threshold;
    720 	if (netdev->cur_rssi_low != new_rssi_low) {
    721 		int event = new_rssi_low ?
    722 			NETDEV_EVENT_RSSI_THRESHOLD_LOW :
    723 			NETDEV_EVENT_RSSI_THRESHOLD_HIGH;
    724 
    725 		netdev->cur_rssi_low = new_rssi_low;
    726 		netdev->event_filter(netdev, event, NULL, netdev->user_data);
    727 	}
    728 
    729 	if (!netdev->rssi_levels_num)
    730 		return;
    731 
    732 	netdev_set_rssi_level_idx(netdev);
    733 	if (netdev->cur_rssi_level_idx != prev_rssi_level_idx)
    734 		netdev->event_filter(netdev, NETDEV_EVENT_RSSI_LEVEL_NOTIFY,
    735 					&netdev->cur_rssi_level_idx,
    736 					netdev->user_data);
    737 }
    738 
    739 static void netdev_rssi_poll_cb(struct l_genl_msg *msg, void *user_data)
    740 {
    741 	struct netdev *netdev = user_data;
    742 	struct l_genl_attr attr, nested;
    743 	uint16_t type, len;
    744 	const void *data;
    745 	bool found;
    746 	struct diagnostic_station_info info;
    747 	uint8_t prev_rssi_level_idx = netdev->cur_rssi_level_idx;
    748 
    749 	netdev->rssi_poll_cmd_id = 0;
    750 
    751 	if (!l_genl_attr_init(&attr, msg))
    752 		goto done;
    753 
    754 	found = false;
    755 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
    756 		if (type != NL80211_ATTR_STA_INFO)
    757 			continue;
    758 
    759 		if (!l_genl_attr_recurse(&attr, &nested))
    760 			goto done;
    761 
    762 		if (!netdev_parse_sta_info(&nested, &info))
    763 			goto done;
    764 
    765 		found = true;
    766 		break;
    767 	}
    768 
    769 	if (!found || !info.have_cur_rssi)
    770 		goto done;
    771 
    772 	netdev->cur_rssi = info.cur_rssi;
    773 
    774 	/*
    775 	 * If the CMD_SET_CQM call failed RSSI polling was started. In this case
    776 	 * we should behave just like its a CQM event and check both the RSSI
    777 	 * level indexes and the HIGH/LOW thresholds.
    778 	 */
    779 	if (netdev->cqm_poll_fallback) {
    780 		netdev_cqm_event_rssi_value(netdev, info.cur_rssi);
    781 		goto done;
    782 	}
    783 
    784 	/* Otherwise just update the level notifications, CQM events work */
    785 	netdev_set_rssi_level_idx(netdev);
    786 	if (netdev->cur_rssi_level_idx != prev_rssi_level_idx)
    787 		netdev->event_filter(netdev, NETDEV_EVENT_RSSI_LEVEL_NOTIFY,
    788 					&netdev->cur_rssi_level_idx,
    789 					netdev->user_data);
    790 
    791 done:
    792 	/* Rearm timer */
    793 	l_timeout_modify(netdev->rssi_poll_timeout, 6);
    794 }
    795 
    796 static void netdev_rssi_poll(struct l_timeout *timeout, void *user_data)
    797 {
    798 	struct netdev *netdev = user_data;
    799 	struct l_genl_msg *msg;
    800 
    801 	msg = l_genl_msg_new_sized(NL80211_CMD_GET_STATION, 64);
    802 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
    803 	l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN,
    804 							netdev->handshake->aa);
    805 
    806 	netdev->rssi_poll_cmd_id = l_genl_family_send(nl80211, msg,
    807 							netdev_rssi_poll_cb,
    808 							netdev, NULL);
    809 }
    810 
    811 /* To be called whenever operational or rssi_levels_num are updated */
    812 static void netdev_rssi_polling_update(struct netdev *netdev)
    813 {
    814 	if (!netdev->cqm_poll_fallback && wiphy_has_ext_feature(netdev->wiphy,
    815 					NL80211_EXT_FEATURE_CQM_RSSI_LIST))
    816 		return;
    817 
    818 	if (netdev->operational && (netdev->rssi_levels_num > 0 ||
    819 					netdev->cqm_poll_fallback)) {
    820 		if (netdev->rssi_poll_timeout)
    821 			return;
    822 
    823 		netdev->rssi_poll_timeout =
    824 			l_timeout_create(1, netdev_rssi_poll, netdev, NULL);
    825 	} else {
    826 		if (!netdev->rssi_poll_timeout)
    827 			return;
    828 
    829 		l_timeout_remove(netdev->rssi_poll_timeout);
    830 		netdev->rssi_poll_timeout = NULL;
    831 
    832 		if (netdev->rssi_poll_cmd_id) {
    833 			l_genl_family_cancel(nl80211, netdev->rssi_poll_cmd_id);
    834 			netdev->rssi_poll_cmd_id = 0;
    835 		}
    836 	}
    837 }
    838 
    839 static void netdev_preauth_destroy(void *data)
    840 {
    841 	struct netdev_preauth_state *state = data;
    842 
    843 	if (state->cb)
    844 		state->cb(state->netdev, NETDEV_RESULT_ABORTED, NULL,
    845 				state->user_data);
    846 
    847 	l_free(state);
    848 }
    849 
    850 static void netdev_connect_free(struct netdev *netdev)
    851 {
    852 	if (netdev->work.id)
    853 		wiphy_radio_work_done(netdev->wiphy, netdev->work.id);
    854 
    855 	if (netdev->sm) {
    856 		eapol_sm_free(netdev->sm);
    857 		netdev->sm = NULL;
    858 	}
    859 
    860 	if (netdev->ap) {
    861 		auth_proto_free(netdev->ap);
    862 		netdev->ap = NULL;
    863 	}
    864 
    865 	if (netdev->owe_sm) {
    866 		owe_sm_free(netdev->owe_sm);
    867 		netdev->owe_sm = NULL;
    868 	}
    869 
    870 	eapol_preauth_cancel(netdev->index);
    871 
    872 	if (netdev->handshake) {
    873 		handshake_state_unref(netdev->handshake);
    874 		netdev->handshake = NULL;
    875 	}
    876 
    877 	if (netdev->ext_key_info) {
    878 		l_free(netdev->ext_key_info);
    879 		netdev->ext_key_info = NULL;
    880 	}
    881 
    882 	if (netdev->neighbor_report_cb) {
    883 		netdev->neighbor_report_cb(netdev, -ENOTCONN, NULL, 0,
    884 						netdev->user_data);
    885 		netdev->neighbor_report_cb = NULL;
    886 		l_timeout_remove(netdev->neighbor_report_timeout);
    887 	}
    888 
    889 	if (netdev->sa_query_timeout) {
    890 		l_timeout_remove(netdev->sa_query_timeout);
    891 		netdev->sa_query_timeout = NULL;
    892 	}
    893 
    894 	if (netdev->sa_query_delay) {
    895 		l_timeout_remove(netdev->sa_query_delay);
    896 		netdev->sa_query_delay = NULL;
    897 	}
    898 
    899 	if (netdev->group_handshake_timeout) {
    900 		l_timeout_remove(netdev->group_handshake_timeout);
    901 		netdev->group_handshake_timeout = NULL;
    902 	}
    903 
    904 	netdev->associated = false;
    905 	netdev->operational = false;
    906 	netdev->connected = false;
    907 	netdev->connect_cb = NULL;
    908 	netdev->event_filter = NULL;
    909 	netdev->user_data = NULL;
    910 	netdev->result = NETDEV_RESULT_OK;
    911 	netdev->last_code = 0;
    912 	netdev->in_ft = false;
    913 	netdev->in_reassoc = false;
    914 	netdev->ignore_connect_event = false;
    915 	netdev->expect_connect_failure = false;
    916 	netdev->cur_rssi_low = false;
    917 	netdev->privacy = false;
    918 	netdev->external_auth = false;
    919 
    920 	if (netdev->connect_cmd) {
    921 		l_genl_msg_unref(netdev->connect_cmd);
    922 		netdev->connect_cmd = NULL;
    923 	}
    924 
    925 	netdev_rssi_polling_update(netdev);
    926 
    927 	if (netdev->connect_cmd_id) {
    928 		l_genl_family_cancel(nl80211, netdev->connect_cmd_id);
    929 		netdev->connect_cmd_id = 0;
    930 	} else if (netdev->disconnect_cmd_id) {
    931 		l_genl_family_cancel(nl80211, netdev->disconnect_cmd_id);
    932 		netdev->disconnect_cmd_id = 0;
    933 	}
    934 
    935 	if (netdev->get_oci_cmd_id) {
    936 		l_genl_family_cancel(nl80211, netdev->get_oci_cmd_id);
    937 		netdev->get_oci_cmd_id = 0;
    938 	}
    939 
    940 	/* Reset thresholds back to default */
    941 	netdev->low_signal_threshold = LOW_SIGNAL_THRESHOLD;
    942 	netdev->low_signal_threshold_5ghz = LOW_SIGNAL_THRESHOLD_5GHZ;
    943 }
    944 
    945 static void netdev_connect_failed(struct netdev *netdev,
    946 					enum netdev_result result,
    947 					uint16_t status_or_reason)
    948 {
    949 	netdev_connect_cb_t connect_cb = netdev->connect_cb;
    950 	void *connect_data = netdev->user_data;
    951 
    952 	/* Done this way to allow re-entrant netdev_connect calls */
    953 	netdev_connect_free(netdev);
    954 
    955 	if (connect_cb)
    956 		connect_cb(netdev, result, &status_or_reason, connect_data);
    957 }
    958 
    959 static void netdev_connect_failed_cb(struct l_genl_msg *msg, void *user_data)
    960 {
    961 	struct netdev *netdev = user_data;
    962 
    963 	netdev->disconnect_cmd_id = 0;
    964 	netdev_connect_failed(netdev, netdev->result, netdev->last_code);
    965 }
    966 
    967 static void netdev_send_and_fail_connection(struct netdev *netdev,
    968 						enum netdev_result result,
    969 						uint16_t status_code,
    970 						struct l_genl_msg *msg)
    971 {
    972 	netdev->result = result;
    973 	netdev->last_code = status_code;
    974 
    975 	netdev->disconnect_cmd_id =
    976 		l_genl_family_send(nl80211, msg, netdev_connect_failed_cb,
    977 					netdev, NULL);
    978 }
    979 
    980 static void netdev_disconnect_and_fail_connection(struct netdev *netdev,
    981 						enum netdev_result result,
    982 						uint16_t status_code)
    983 {
    984 	struct l_genl_msg *msg = nl80211_build_disconnect(netdev->index,
    985 						MMPDU_REASON_CODE_UNSPECIFIED);
    986 
    987 	netdev_send_and_fail_connection(netdev, result, status_code, msg);
    988 }
    989 
    990 static void netdev_deauth_and_fail_connection(struct netdev *netdev,
    991 						enum netdev_result result,
    992 						uint16_t status_code)
    993 {
    994 	struct l_genl_msg *msg = nl80211_build_deauthenticate(netdev->index,
    995 						netdev->handshake->aa,
    996 						MMPDU_REASON_CODE_UNSPECIFIED);
    997 
    998 	netdev_send_and_fail_connection(netdev, result, status_code, msg);
    999 }
   1000 
   1001 /*
   1002  * If we have a connection callback pending, either through netdev_connect
   1003  * or netdev_reassociate, then invoke that callback with the @result and
   1004  * @status_or_reason.  Otherwise, invoke the event callback with the @event
   1005  * and @status_or_reason.
   1006  *
   1007  * This is useful for situations where handshaking or setting keys somehow
   1008  * fails (perhaps due to rekeying), or if the device is removed / brought
   1009  * down when keys are being set as a result of a rekey
   1010  */
   1011 static void netdev_disconnected(struct netdev *netdev,
   1012 					enum netdev_result result,
   1013 					enum netdev_event event,
   1014 					uint16_t status_or_reason)
   1015 {
   1016 	netdev_event_func_t event_filter = netdev->event_filter;
   1017 	void *event_data = netdev->user_data;
   1018 
   1019 	if (netdev->connect_cb) {
   1020 		netdev_connect_failed(netdev, result, status_or_reason);
   1021 		return;
   1022 	}
   1023 
   1024 	netdev_connect_free(netdev);
   1025 
   1026 	if (event_filter)
   1027 		event_filter(netdev, event, &status_or_reason, event_data);
   1028 }
   1029 
   1030 static void netdev_disconnect_by_sme_cb(struct l_genl_msg *msg, void *user_data)
   1031 {
   1032 	struct netdev *netdev = user_data;
   1033 
   1034 	netdev->disconnect_cmd_id = 0;
   1035 	netdev_disconnected(netdev, netdev->result,
   1036 			NETDEV_EVENT_DISCONNECT_BY_SME, netdev->last_code);
   1037 }
   1038 
   1039 static void netdev_disconnect_by_sme(struct netdev *netdev,
   1040 					enum netdev_result result,
   1041 					uint16_t reason_code)
   1042 {
   1043 	struct l_genl_msg *msg = nl80211_build_disconnect(netdev->index,
   1044 								reason_code);
   1045 
   1046 	netdev->result = result;
   1047 	netdev->last_code = reason_code;
   1048 
   1049 	netdev->disconnect_cmd_id = l_genl_family_send(nl80211, msg,
   1050 						netdev_disconnect_by_sme_cb,
   1051 						netdev, NULL);
   1052 }
   1053 
   1054 static void netdev_free(void *data)
   1055 {
   1056 	struct netdev *netdev = data;
   1057 
   1058 	l_debug("Freeing netdev %s[%d]", netdev->name, netdev->index);
   1059 
   1060 	netdev->ifi_flags &= ~IFF_UP;
   1061 
   1062 	if (netdev->events_ready)
   1063 		WATCHLIST_NOTIFY(&netdev_watches, netdev_watch_func_t,
   1064 					netdev, NETDEV_WATCH_EVENT_DEL);
   1065 
   1066 	if (netdev->neighbor_report_cb) {
   1067 		netdev->neighbor_report_cb(netdev, -ENODEV, NULL, 0,
   1068 						netdev->user_data);
   1069 		netdev->neighbor_report_cb = NULL;
   1070 		l_timeout_remove(netdev->neighbor_report_timeout);
   1071 	}
   1072 
   1073 	if (netdev->connected || netdev->connect_cmd_id || netdev->work.id)
   1074 		netdev_connect_free(netdev);
   1075 
   1076 	if (netdev->disconnect_cmd_id) {
   1077 		l_genl_family_cancel(nl80211, netdev->disconnect_cmd_id);
   1078 		netdev->disconnect_cmd_id = 0;
   1079 
   1080 		if (netdev->disconnect_cb)
   1081 			netdev->disconnect_cb(netdev, true, netdev->user_data);
   1082 
   1083 		netdev->disconnect_cb = NULL;
   1084 		netdev->user_data = NULL;
   1085 	}
   1086 
   1087 	if (netdev->disconnect_idle) {
   1088 		l_idle_remove(netdev->disconnect_idle);
   1089 		netdev->disconnect_idle = NULL;
   1090 	}
   1091 
   1092 	if (netdev->join_adhoc_cmd_id) {
   1093 		l_genl_family_cancel(nl80211, netdev->join_adhoc_cmd_id);
   1094 		netdev->join_adhoc_cmd_id = 0;
   1095 	}
   1096 
   1097 	if (netdev->leave_adhoc_cmd_id) {
   1098 		l_genl_family_cancel(nl80211, netdev->leave_adhoc_cmd_id);
   1099 		netdev->leave_adhoc_cmd_id = 0;
   1100 	}
   1101 
   1102 	if (netdev->set_powered_cmd_id) {
   1103 		l_netlink_cancel(rtnl, netdev->set_powered_cmd_id);
   1104 		netdev->set_powered_cmd_id = 0;
   1105 	}
   1106 
   1107 	if (netdev->rekey_offload_cmd_id) {
   1108 		l_genl_family_cancel(nl80211, netdev->rekey_offload_cmd_id);
   1109 		netdev->rekey_offload_cmd_id = 0;
   1110 	}
   1111 
   1112 	if (netdev->qos_map_cmd_id) {
   1113 		l_genl_family_cancel(nl80211, netdev->qos_map_cmd_id);
   1114 		netdev->qos_map_cmd_id = 0;
   1115 	}
   1116 
   1117 	if (netdev->mac_change_cmd_id) {
   1118 		l_netlink_cancel(rtnl, netdev->mac_change_cmd_id);
   1119 		netdev->mac_change_cmd_id = 0;
   1120 	}
   1121 
   1122 	if (netdev->get_station_cmd_id) {
   1123 		l_genl_family_cancel(nl80211, netdev->get_station_cmd_id);
   1124 		netdev->get_station_cmd_id = 0;
   1125 	}
   1126 
   1127 	if (netdev->set_cqm_cmd_id) {
   1128 		l_genl_family_cancel(nl80211, netdev->set_cqm_cmd_id);
   1129 		netdev->set_cqm_cmd_id = 0;
   1130 	}
   1131 
   1132 	if (netdev->fw_roam_bss)
   1133 		scan_bss_free(netdev->fw_roam_bss);
   1134 
   1135 	if (netdev->get_link_cmd_id) {
   1136 		l_netlink_cancel(rtnl, netdev->get_link_cmd_id);
   1137 		netdev->get_link_cmd_id = 0;
   1138 	}
   1139 
   1140 	if (netdev->rssi_poll_timeout)
   1141 		l_timeout_remove(netdev->rssi_poll_timeout);
   1142 
   1143 	scan_wdev_remove(netdev->wdev_id);
   1144 	frame_watch_wdev_remove(netdev->wdev_id);
   1145 
   1146 	watchlist_destroy(&netdev->station_watches);
   1147 
   1148 	l_io_destroy(netdev->pae_io);
   1149 
   1150 	l_free(netdev);
   1151 }
   1152 
   1153 static void netdev_shutdown_one(void *data, void *user_data)
   1154 {
   1155 	struct netdev *netdev = data;
   1156 
   1157 	if (netdev_get_is_up(netdev))
   1158 		l_rtnl_set_powered(rtnl, netdev->index, false,
   1159 					NULL, NULL, NULL);
   1160 }
   1161 
   1162 static bool netdev_match(const void *a, const void *b)
   1163 {
   1164 	const struct netdev *netdev = a;
   1165 	uint32_t ifindex = L_PTR_TO_UINT(b);
   1166 
   1167 	return (netdev->index == ifindex);
   1168 }
   1169 
   1170 struct netdev *netdev_find(int ifindex)
   1171 {
   1172 	return l_queue_find(netdev_list, netdev_match, L_UINT_TO_PTR(ifindex));
   1173 }
   1174 
   1175 static void netdev_cqm_event_rssi_threshold(struct netdev *netdev,
   1176 						uint32_t rssi_event)
   1177 {
   1178 	int event;
   1179 
   1180 	if (!netdev->operational)
   1181 		return;
   1182 
   1183 	if (rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
   1184 			rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH)
   1185 		return;
   1186 
   1187 	if (!netdev->event_filter)
   1188 		return;
   1189 
   1190 	netdev->cur_rssi_low =
   1191 		(rssi_event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW);
   1192 	event = netdev->cur_rssi_low ? NETDEV_EVENT_RSSI_THRESHOLD_LOW :
   1193 		NETDEV_EVENT_RSSI_THRESHOLD_HIGH;
   1194 
   1195 	netdev->event_filter(netdev, event, NULL, netdev->user_data);
   1196 }
   1197 
   1198 static void netdev_cqm_event(struct l_genl_msg *msg, struct netdev *netdev)
   1199 {
   1200 	struct l_genl_attr attr;
   1201 	struct l_genl_attr nested;
   1202 	uint16_t type, len;
   1203 	const void *data;
   1204 	uint32_t *rssi_event = NULL;
   1205 	int32_t *rssi_val = NULL;
   1206 	uint32_t *pkt_event = NULL;
   1207 	bool beacon_loss = false;
   1208 
   1209 	if (!l_genl_attr_init(&attr, msg))
   1210 		return;
   1211 
   1212 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
   1213 		switch (type) {
   1214 		case NL80211_ATTR_CQM:
   1215 			if (!l_genl_attr_recurse(&attr, &nested))
   1216 				return;
   1217 
   1218 			while (l_genl_attr_next(&nested, &type, &len, &data)) {
   1219 				switch (type) {
   1220 				case NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT:
   1221 					if (len != 4)
   1222 						continue;
   1223 
   1224 					rssi_event = (uint32_t *) data;
   1225 					break;
   1226 
   1227 				case NL80211_ATTR_CQM_PKT_LOSS_EVENT:
   1228 					if (len != 4)
   1229 						continue;
   1230 
   1231 					pkt_event = (uint32_t *) data;
   1232 					break;
   1233 
   1234 				case NL80211_ATTR_CQM_BEACON_LOSS_EVENT:
   1235 					beacon_loss = true;
   1236 					break;
   1237 
   1238 				case NL80211_ATTR_CQM_RSSI_LEVEL:
   1239 					if (len != 4)
   1240 						continue;
   1241 
   1242 					rssi_val = (int32_t *) data;
   1243 					break;
   1244 				default:
   1245 					l_debug("Unknown CQM event: %d", type);
   1246 				}
   1247 			}
   1248 
   1249 			break;
   1250 		}
   1251 	}
   1252 
   1253 	if (rssi_event) {
   1254 		if (rssi_val) {
   1255 			l_debug("Signal change event (above=%d signal=%d)",
   1256 							*rssi_event, *rssi_val);
   1257 			netdev_cqm_event_rssi_value(netdev, *rssi_val);
   1258 		} else {
   1259 			l_debug("Signal change event (above=%d)", *rssi_event);
   1260 			netdev_cqm_event_rssi_threshold(netdev, *rssi_event);
   1261 		}
   1262 	} else if (pkt_event && netdev->event_filter)
   1263 		netdev->event_filter(netdev, NETDEV_EVENT_PACKET_LOSS_NOTIFY,
   1264 					pkt_event, netdev->user_data);
   1265 	else if (beacon_loss && netdev->event_filter)
   1266 		netdev->event_filter(netdev, NETDEV_EVENT_BEACON_LOSS_NOTIFY,
   1267 					NULL, netdev->user_data);
   1268 }
   1269 
   1270 static void netdev_rekey_offload_event(struct l_genl_msg *msg,
   1271 					struct netdev *netdev)
   1272 {
   1273 	struct l_genl_attr attr;
   1274 	struct l_genl_attr nested;
   1275 	uint16_t type, len;
   1276 	const void *data;
   1277 	uint64_t replay_ctr;
   1278 
   1279 	if (!l_genl_attr_init(&attr, msg))
   1280 		return;
   1281 
   1282 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
   1283 		if (type != NL80211_ATTR_REKEY_DATA)
   1284 			continue;
   1285 
   1286 		if (!l_genl_attr_recurse(&attr, &nested))
   1287 			return;
   1288 
   1289 		while (l_genl_attr_next(&nested, &type, &len, &data)) {
   1290 			if (type != NL80211_REKEY_DATA_REPLAY_CTR)
   1291 				continue;
   1292 
   1293 			if (len != sizeof(uint64_t)) {
   1294 				l_warn("Invalid replay_ctr");
   1295 				return;
   1296 			}
   1297 
   1298 			replay_ctr = *((uint64_t *) data);
   1299 			__eapol_update_replay_counter(netdev->index,
   1300 							netdev->addr,
   1301 							netdev->handshake->aa,
   1302 							replay_ctr);
   1303 			return;
   1304 		}
   1305 	}
   1306 }
   1307 
   1308 static void netdev_disconnect_event(struct l_genl_msg *msg,
   1309 							struct netdev *netdev)
   1310 {
   1311 	struct l_genl_attr attr;
   1312 	uint16_t type, len;
   1313 	const void *data;
   1314 	uint16_t reason_code = 0;
   1315 	bool disconnect_by_ap = false;
   1316 	enum netdev_event event;
   1317 
   1318 	l_debug("");
   1319 
   1320 	if (!netdev->connected || netdev->disconnect_cmd_id > 0)
   1321 		return;
   1322 
   1323 	if (!l_genl_attr_init(&attr, msg)) {
   1324 		l_error("attr init failed");
   1325 		return;
   1326 	}
   1327 
   1328 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
   1329 		switch (type) {
   1330 		case NL80211_ATTR_REASON_CODE:
   1331 			if (len != sizeof(uint16_t))
   1332 				l_warn("Invalid reason code attribute");
   1333 			else
   1334 				reason_code = *((uint16_t *) data);
   1335 
   1336 			break;
   1337 
   1338 		case NL80211_ATTR_DISCONNECTED_BY_AP:
   1339 			disconnect_by_ap = true;
   1340 			break;
   1341 		}
   1342 	}
   1343 
   1344 	/*
   1345 	 * Only ignore this event if issued by the kernel since this is
   1346 	 * normal when using CMD_AUTH/ASSOC.
   1347 	 */
   1348 	if (!disconnect_by_ap && (netdev->in_ft || netdev->in_reassoc))
   1349 		return;
   1350 
   1351 	l_info("Received Deauthentication event, reason: %hu, from_ap: %s",
   1352 			reason_code, disconnect_by_ap ? "true" : "false");
   1353 
   1354 	event = disconnect_by_ap ? NETDEV_EVENT_DISCONNECT_BY_AP :
   1355 				NETDEV_EVENT_DISCONNECT_BY_SME;
   1356 
   1357 	netdev_disconnected(netdev, NETDEV_RESULT_DISCONNECTED,
   1358 				event, reason_code);
   1359 }
   1360 
   1361 static void netdev_cmd_disconnect_cb(struct l_genl_msg *msg, void *user_data)
   1362 {
   1363 	struct netdev *netdev = user_data;
   1364 	void *disconnect_data;
   1365 	netdev_disconnect_cb_t disconnect_cb;
   1366 	bool r;
   1367 
   1368 	netdev->disconnect_cmd_id = 0;
   1369 	netdev->aborting = false;
   1370 
   1371 	if (!netdev->disconnect_cb) {
   1372 		netdev->user_data = NULL;
   1373 		return;
   1374 	}
   1375 
   1376 	disconnect_data = netdev->user_data;
   1377 	disconnect_cb = netdev->disconnect_cb;
   1378 	netdev->user_data = NULL;
   1379 	netdev->disconnect_cb = NULL;
   1380 
   1381 	if (l_genl_msg_get_error(msg) < 0)
   1382 		r = false;
   1383 	else
   1384 		r = true;
   1385 
   1386 	disconnect_cb(netdev, r, disconnect_data);
   1387 }
   1388 
   1389 static void netdev_deauthenticate_event(struct l_genl_msg *msg,
   1390 							struct netdev *netdev)
   1391 {
   1392 	const struct mmpdu_header *hdr = NULL;
   1393 	const struct mmpdu_deauthentication *deauth;
   1394 	uint16_t reason_code;
   1395 	struct iovec iov;
   1396 
   1397 	l_debug("");
   1398 
   1399 	/*
   1400 	 * If we got to the association phase, process the connect event
   1401 	 * instead
   1402 	 */
   1403 	if (!netdev->connected || netdev->associated)
   1404 		return;
   1405 
   1406 	/*
   1407 	 * Handle the bizarre case of AP accepting authentication, then
   1408 	 * deauthenticating immediately afterwards
   1409 	 */
   1410 
   1411 	if (L_WARN_ON(nl80211_parse_attrs(msg, NL80211_ATTR_FRAME, &iov,
   1412 						NL80211_ATTR_UNSPEC) < 0))
   1413 		return;
   1414 
   1415 	hdr = mpdu_validate(iov.iov_base, iov.iov_len);
   1416 	if (L_WARN_ON(!hdr))
   1417 		return;
   1418 
   1419 	/* Ignore any locally generated frames */
   1420 	if (!memcmp(hdr->address_2, netdev->addr, sizeof(netdev->addr)))
   1421 		return;
   1422 
   1423 	deauth = mmpdu_body(hdr);
   1424 	reason_code = L_LE16_TO_CPU(deauth->reason_code);
   1425 
   1426 	l_info("deauth event, src="MAC" dest="MAC" bssid="MAC" reason=%u",
   1427 			MAC_STR(hdr->address_2), MAC_STR(hdr->address_1),
   1428 			MAC_STR(hdr->address_3), reason_code);
   1429 
   1430 	netdev_connect_failed(netdev, NETDEV_RESULT_AUTHENTICATION_FAILED,
   1431 					reason_code);
   1432 }
   1433 
   1434 static void netdev_operstate_cb(int error, uint16_t type,
   1435 					const void *data,
   1436 					uint32_t len, void *user_data)
   1437 {
   1438 	if (!error)
   1439 		return;
   1440 
   1441 	l_debug("netdev: %u, error: %s", L_PTR_TO_UINT(user_data),
   1442 							strerror(-error));
   1443 }
   1444 
   1445 static void netdev_connect_ok(struct netdev *netdev)
   1446 {
   1447 	l_debug("");
   1448 
   1449 	l_rtnl_set_linkmode_and_operstate(rtnl, netdev->index,
   1450 					IF_LINK_MODE_DORMANT, IF_OPER_UP,
   1451 					netdev_operstate_cb,
   1452 					L_UINT_TO_PTR(netdev->index), NULL);
   1453 
   1454 	netdev->operational = true;
   1455 
   1456 	if (netdev->fw_roam_bss) {
   1457 		if (netdev->event_filter)
   1458 			netdev->event_filter(netdev, NETDEV_EVENT_ROAMED,
   1459 						netdev->fw_roam_bss,
   1460 						netdev->user_data);
   1461 		else
   1462 			scan_bss_free(netdev->fw_roam_bss);
   1463 
   1464 		netdev->fw_roam_bss = NULL;
   1465 	} else if (netdev->connect_cb) {
   1466 		netdev->connect_cb(netdev, NETDEV_RESULT_OK, NULL,
   1467 					netdev->user_data);
   1468 		netdev->connect_cb = NULL;
   1469 		netdev->in_ft = false;
   1470 	} else
   1471 		l_warn("Connection event without a connect callback!");
   1472 
   1473 	netdev_rssi_polling_update(netdev);
   1474 
   1475 	if (netdev->work.id)
   1476 		wiphy_radio_work_done(netdev->wiphy, netdev->work.id);
   1477 }
   1478 
   1479 static void netdev_setting_keys_failed(struct netdev_handshake_state *nhs,
   1480 						int err)
   1481 {
   1482 	struct netdev *netdev = nhs->netdev;
   1483 	struct l_genl_msg *msg;
   1484 
   1485 	/*
   1486 	 * Something went wrong with our sequence:
   1487 	 * 1. new_key(ptk)
   1488 	 * 2. new_key(gtk) [optional]
   1489 	 * 3. new_key(igtk) [optional]
   1490 	 * 4. rekey offload [optional]
   1491 	 * 5. set_station
   1492 	 *
   1493 	 * Cancel all pending commands, then de-authenticate
   1494 	 */
   1495 	netdev_handshake_state_cancel_all(nhs);
   1496 
   1497 	if (netdev->rekey_offload_cmd_id) {
   1498 		l_genl_family_cancel(nl80211, netdev->rekey_offload_cmd_id);
   1499 		netdev->rekey_offload_cmd_id = 0;
   1500 	}
   1501 
   1502 	if (netdev->group_handshake_timeout) {
   1503 		l_timeout_remove(netdev->group_handshake_timeout);
   1504 		netdev->group_handshake_timeout = NULL;
   1505 	}
   1506 
   1507 	switch (netdev->type) {
   1508 	case NL80211_IFTYPE_STATION:
   1509 	case NL80211_IFTYPE_P2P_CLIENT:
   1510 		/*
   1511 		 * If we failed due to the netdev being brought down,
   1512 		 * just abort the connection and do not try to send a
   1513 		 * CMD_DISCONNECT
   1514 		 */
   1515 		if (err == -ENETDOWN) {
   1516 			netdev_disconnected(netdev, NETDEV_RESULT_ABORTED,
   1517 					NETDEV_EVENT_DISCONNECT_BY_SME,
   1518 					MMPDU_STATUS_CODE_UNSPECIFIED);
   1519 			return;
   1520 		}
   1521 
   1522 		netdev_disconnect_by_sme(netdev,
   1523 					NETDEV_RESULT_KEY_SETTING_FAILED,
   1524 					MMPDU_REASON_CODE_UNSPECIFIED);
   1525 		break;
   1526 	case NL80211_IFTYPE_AP:
   1527 		if (err == -ENETDOWN)
   1528 			return;
   1529 
   1530 		msg = nl80211_build_del_station(netdev->index,
   1531 				nhs->super.spa, MMPDU_REASON_CODE_UNSPECIFIED,
   1532 				MPDU_MANAGEMENT_SUBTYPE_DEAUTHENTICATION);
   1533 		if (!l_genl_family_send(nl80211, msg, NULL, NULL, NULL))
   1534 			l_error("error sending DEL_STATION");
   1535 
   1536 		break;
   1537 	}
   1538 
   1539 	handshake_event(&nhs->super, HANDSHAKE_EVENT_SETTING_KEYS_FAILED, &err);
   1540 }
   1541 
   1542 static bool netdev_match_addr(const void *a, const void *b)
   1543 {
   1544 	const struct netdev *netdev = a;
   1545 	const uint8_t *addr = b;
   1546 
   1547 	return memcmp(netdev->addr, addr, ETH_ALEN) == 0;
   1548 }
   1549 
   1550 static struct netdev *netdev_find_by_address(const uint8_t *addr)
   1551 {
   1552 	return l_queue_find(netdev_list, netdev_match_addr, addr);
   1553 }
   1554 
   1555 static void netdev_pmksa_driver_add(const struct pmksa *pmksa)
   1556 {
   1557 	struct l_genl_msg *msg;
   1558 	struct netdev *netdev = netdev_find_by_address(pmksa->spa);
   1559 	uint32_t expiration = (uint32_t)pmksa->expiration;
   1560 
   1561 	if (!netdev)
   1562 		return;
   1563 
   1564 	/* Only need to set the PMKSA into the kernel for fullmac drivers */
   1565 	if (wiphy_supports_cmds_auth_assoc(netdev->wiphy))
   1566 		return;
   1567 
   1568 	l_debug("Adding PMKSA to kernel");
   1569 
   1570 	msg = l_genl_msg_new(NL80211_CMD_SET_PMKSA);
   1571 
   1572 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   1573 	l_genl_msg_append_attr(msg, NL80211_ATTR_PMKID, 16, pmksa->pmkid);
   1574 	l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, pmksa->aa);
   1575 	l_genl_msg_append_attr(msg, NL80211_ATTR_SSID,
   1576 				pmksa->ssid_len, pmksa->ssid);
   1577 	l_genl_msg_append_attr(msg, NL80211_ATTR_PMK_LIFETIME, 4, &expiration);
   1578 	l_genl_msg_append_attr(msg, NL80211_ATTR_PMK,
   1579 				pmksa->pmk_len, pmksa->pmk);
   1580 
   1581 	if (!l_genl_family_send(nl80211, msg, NULL, NULL, NULL))
   1582 		l_error("error sending SET_PMKSA");
   1583 }
   1584 
   1585 static void netdev_pmksa_driver_remove(const struct pmksa *pmksa)
   1586 {
   1587 	struct l_genl_msg *msg;
   1588 	struct netdev *netdev = netdev_find_by_address(pmksa->spa);
   1589 
   1590 	if (!netdev)
   1591 		return;
   1592 
   1593 	/* Only need to set the PMKSA into the kernel for fullmac drivers */
   1594 	if (wiphy_supports_cmds_auth_assoc(netdev->wiphy))
   1595 		return;
   1596 
   1597 	l_debug("Removing PMKSA from kernel");
   1598 
   1599 	msg = l_genl_msg_new(NL80211_CMD_DEL_PMKSA);
   1600 
   1601 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   1602 	l_genl_msg_append_attr(msg, NL80211_ATTR_PMKID, 16, pmksa->pmkid);
   1603 	l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, pmksa->aa);
   1604 	l_genl_msg_append_attr(msg, NL80211_ATTR_SSID,
   1605 				pmksa->ssid_len, pmksa->ssid);
   1606 
   1607 	if (!l_genl_family_send(nl80211, msg, NULL, NULL, NULL))
   1608 		l_error("error sending DEL_PMKSA");
   1609 }
   1610 
   1611 static void netdev_flush_pmksa(struct netdev *netdev)
   1612 {
   1613 	struct l_genl_msg *msg;
   1614 
   1615 	/*
   1616 	* We only utilize the kernel's PMKSA cache for fullmac cards,
   1617 	* so no need to flush if this is a softmac.
   1618 	*/
   1619 	if (wiphy_supports_cmds_auth_assoc(netdev->wiphy))
   1620 		return;
   1621 
   1622 	msg = l_genl_msg_new(NL80211_CMD_FLUSH_PMKSA);
   1623 
   1624 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   1625 
   1626 	if (!l_genl_family_send(nl80211, msg, NULL, NULL, NULL))
   1627 		l_error("Failed to flush PMKSA for %u", netdev->index);
   1628 }
   1629 
   1630 static void netdev_pmksa_driver_flush(void)
   1631 {
   1632 	const struct l_queue_entry *e;
   1633 
   1634 	for (e = l_queue_get_entries(netdev_list); e; e = e->next) {
   1635 		struct netdev *netdev = e->data;
   1636 
   1637 		netdev_flush_pmksa(netdev);
   1638 	}
   1639 }
   1640 
   1641 static void try_handshake_complete(struct netdev_handshake_state *nhs)
   1642 {
   1643 	l_debug("ptk_installed: %u, gtk_installed: %u, igtk_installed: %u",
   1644 			nhs->ptk_installed,
   1645 			nhs->gtk_installed,
   1646 			nhs->igtk_installed);
   1647 
   1648 	if (nhs->ptk_installed && nhs->gtk_installed && nhs->igtk_installed) {
   1649 		l_debug("nhs->complete: %u", nhs->complete);
   1650 
   1651 		if (nhs->complete) {
   1652 			handshake_event(&nhs->super,
   1653 					HANDSHAKE_EVENT_REKEY_COMPLETE);
   1654 			return;
   1655 		}
   1656 
   1657 		nhs->complete = true;
   1658 
   1659 		l_debug("Invoking handshake_event()");
   1660 
   1661 		handshake_state_cache_pmksa(&nhs->super);
   1662 
   1663 		if (handshake_event(&nhs->super, HANDSHAKE_EVENT_COMPLETE))
   1664 			return;
   1665 
   1666 		if (nhs->netdev->type == NL80211_IFTYPE_STATION ||
   1667 				nhs->netdev->type == NL80211_IFTYPE_P2P_CLIENT)
   1668 			netdev_connect_ok(nhs->netdev);
   1669 	}
   1670 }
   1671 
   1672 static void netdev_set_station_cb(struct l_genl_msg *msg, void *user_data)
   1673 {
   1674 	struct netdev_handshake_state *nhs = user_data;
   1675 	struct netdev *netdev = nhs->netdev;
   1676 	int err;
   1677 
   1678 	l_debug("");
   1679 
   1680 	nhs->set_station_cmd_id = 0;
   1681 	nhs->ptk_installed = true;
   1682 
   1683 	if (netdev->type == NL80211_IFTYPE_STATION && !netdev->connected)
   1684 		return;
   1685 
   1686 	err = l_genl_msg_get_error(msg);
   1687 	if (err == -EOPNOTSUPP || err == -ENOTSUPP)
   1688 		goto done;
   1689 
   1690 	if (err < 0) {
   1691 		const char *ext_error = l_genl_msg_get_extended_error(msg);
   1692 
   1693 		l_error("Set Station failed for ifindex %d:%s", netdev->index,
   1694 				ext_error ? ext_error : strerror(-err));
   1695 
   1696 		netdev_setting_keys_failed(nhs, err);
   1697 		return;
   1698 	}
   1699 
   1700 done:
   1701 	try_handshake_complete(nhs);
   1702 }
   1703 
   1704 static void netdev_new_group_key_cb(struct l_genl_msg *msg, void *data)
   1705 {
   1706 	struct netdev_handshake_state *nhs = data;
   1707 	struct netdev *netdev = nhs->netdev;
   1708 	int err = l_genl_msg_get_error(msg);
   1709 
   1710 	l_debug("ifindex: %u, err: %d", netdev->index, err);
   1711 	nhs->group_new_key_cmd_id = 0;
   1712 
   1713 	if (err < 0) {
   1714 		const char *ext_error = l_genl_msg_get_extended_error(msg);
   1715 
   1716 		l_error("New Key for Group Key failed for ifindex: %d:%s",
   1717 				netdev->index,
   1718 				ext_error ? ext_error : strerror(-err));
   1719 
   1720 		netdev_setting_keys_failed(nhs, err);
   1721 		return;
   1722 	}
   1723 
   1724 	nhs->gtk_installed = true;
   1725 	try_handshake_complete(nhs);
   1726 }
   1727 
   1728 static void netdev_new_group_management_key_cb(struct l_genl_msg *msg,
   1729 					void *data)
   1730 {
   1731 	struct netdev_handshake_state *nhs = data;
   1732 	struct netdev *netdev = nhs->netdev;
   1733 	int err = l_genl_msg_get_error(msg);
   1734 
   1735 	l_debug("ifindex: %u, err: %d", netdev->index, err);
   1736 	nhs->group_management_new_key_cmd_id = 0;
   1737 
   1738 	if (err < 0) {
   1739 		const char *ext_error = l_genl_msg_get_extended_error(msg);
   1740 
   1741 		l_error("New Key for Group Mgmt failed for ifindex: %d:%s",
   1742 				netdev->index,
   1743 				ext_error ? ext_error : strerror(-err));
   1744 
   1745 		netdev_setting_keys_failed(nhs, err);
   1746 		return;
   1747 	}
   1748 
   1749 	nhs->igtk_installed = true;
   1750 	try_handshake_complete(nhs);
   1751 }
   1752 
   1753 static bool netdev_copy_tk(uint8_t *tk_buf, const uint8_t *tk,
   1754 				uint32_t cipher, bool authenticator)
   1755 {
   1756 	switch (cipher) {
   1757 	case CRYPTO_CIPHER_CCMP:
   1758 	case CRYPTO_CIPHER_GCMP:
   1759 	case CRYPTO_CIPHER_GCMP_256:
   1760 	case CRYPTO_CIPHER_CCMP_256:
   1761 		/*
   1762 		 * 802.11-2020 12.8.3 Mapping PTK to CCMP keys:
   1763 		 * "A STA shall use the temporal key as the CCMP key
   1764 		 * for MPDUs between the two communicating STAs."
   1765 		 *
   1766 		 * Similar verbiage in 12.8.8
   1767 		 */
   1768 		memcpy(tk_buf, tk, crypto_cipher_key_len(cipher));
   1769 		break;
   1770 	case CRYPTO_CIPHER_TKIP:
   1771 		/*
   1772 		 * 802.11-2016 12.8.1 Mapping PTK to TKIP keys:
   1773 		 * "A STA shall use bits 0-127 of the temporal key as its
   1774 		 * input to the TKIP Phase 1 and Phase 2 mixing functions.
   1775 		 *
   1776 		 * A STA shall use bits 128-191 of the temporal key as
   1777 		 * the michael key for MSDUs from the Authenticator's STA
   1778 		 * to the Supplicant's STA.
   1779 		 *
   1780 		 * A STA shall use bits 192-255 of the temporal key as
   1781 		 * the michael key for MSDUs from the Supplicant's STA
   1782 		 * to the Authenticator's STA."
   1783 		 */
   1784 		if (authenticator) {
   1785 			memcpy(tk_buf + NL80211_TKIP_DATA_OFFSET_ENCR_KEY,
   1786 					tk, 16);
   1787 			memcpy(tk_buf + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY,
   1788 					tk + 16, 8);
   1789 			memcpy(tk_buf + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
   1790 					tk + 24, 8);
   1791 		} else {
   1792 			memcpy(tk_buf + NL80211_TKIP_DATA_OFFSET_ENCR_KEY,
   1793 					tk, 16);
   1794 			memcpy(tk_buf + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
   1795 					tk + 16, 8);
   1796 			memcpy(tk_buf + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY,
   1797 					tk + 24, 8);
   1798 		}
   1799 		break;
   1800 	default:
   1801 		l_error("Unexpected cipher: %x", cipher);
   1802 		return false;
   1803 	}
   1804 
   1805 	return true;
   1806 }
   1807 
   1808 static const uint8_t *netdev_choose_key_address(
   1809 					struct netdev_handshake_state *nhs)
   1810 {
   1811 	return (nhs->super.authenticator) ? nhs->super.spa : nhs->super.aa;
   1812 }
   1813 
   1814 static void netdev_set_gtk(struct handshake_state *hs, uint16_t key_index,
   1815 				const uint8_t *gtk, uint8_t gtk_len,
   1816 				const uint8_t *rsc, uint8_t rsc_len,
   1817 				uint32_t cipher)
   1818 {
   1819 	struct netdev_handshake_state *nhs =
   1820 		l_container_of(hs, struct netdev_handshake_state, super);
   1821 	struct netdev *netdev = nhs->netdev;
   1822 	uint8_t gtk_buf[32];
   1823 	struct l_genl_msg *msg;
   1824 	const uint8_t *addr = (netdev->type == NL80211_IFTYPE_ADHOC) ?
   1825 				nhs->super.aa : NULL;
   1826 
   1827 	nhs->gtk_installed = false;
   1828 
   1829 	l_debug("ifindex=%d key_idx=%u", netdev->index, key_index);
   1830 
   1831 	if (crypto_cipher_key_len(cipher) != gtk_len) {
   1832 		l_error("Unexpected key length: %d", gtk_len);
   1833 		netdev_setting_keys_failed(nhs, -ERANGE);
   1834 		return;
   1835 	}
   1836 
   1837 	if (!netdev_copy_tk(gtk_buf, gtk, cipher, hs->authenticator)) {
   1838 		netdev_setting_keys_failed(nhs, -ENOENT);
   1839 		return;
   1840 	}
   1841 
   1842 	if (hs->wait_for_gtk) {
   1843 		l_timeout_remove(netdev->group_handshake_timeout);
   1844 		netdev->group_handshake_timeout = NULL;
   1845 	}
   1846 
   1847 	msg = nl80211_build_new_key_group(netdev->index, cipher, key_index,
   1848 					gtk_buf, gtk_len, rsc, rsc_len, addr);
   1849 
   1850 	nhs->group_new_key_cmd_id =
   1851 		l_genl_family_send(nl80211, msg, netdev_new_group_key_cb,
   1852 						nhs, NULL);
   1853 
   1854 	if (nhs->group_new_key_cmd_id > 0)
   1855 		return;
   1856 
   1857 	l_genl_msg_unref(msg);
   1858 	netdev_setting_keys_failed(nhs, -EIO);
   1859 }
   1860 
   1861 static void netdev_set_igtk(struct handshake_state *hs, uint16_t key_index,
   1862 				const uint8_t *igtk, uint8_t igtk_len,
   1863 				const uint8_t *ipn, uint8_t ipn_len,
   1864 				uint32_t cipher)
   1865 {
   1866 	struct netdev_handshake_state *nhs =
   1867 		l_container_of(hs, struct netdev_handshake_state, super);
   1868 	uint8_t igtk_buf[32];
   1869 	struct netdev *netdev = nhs->netdev;
   1870 	struct l_genl_msg *msg;
   1871 
   1872 	nhs->igtk_installed = false;
   1873 
   1874 	l_debug("ifindex=%d key_idx=%u", netdev->index, key_index);
   1875 
   1876 	if (crypto_cipher_key_len(cipher) != igtk_len) {
   1877 		l_error("Unexpected key length: %d", igtk_len);
   1878 		netdev_setting_keys_failed(nhs, -ERANGE);
   1879 		return;
   1880 	}
   1881 
   1882 	switch (cipher) {
   1883 	case CRYPTO_CIPHER_BIP_CMAC:
   1884 	case CRYPTO_CIPHER_BIP_GMAC:
   1885 	case CRYPTO_CIPHER_BIP_GMAC_256:
   1886 	case CRYPTO_CIPHER_BIP_CMAC_256:
   1887 		memcpy(igtk_buf, igtk, igtk_len);
   1888 		break;
   1889 	default:
   1890 		l_error("Unexpected cipher: %x", cipher);
   1891 		netdev_setting_keys_failed(nhs, -ENOENT);
   1892 		return;
   1893 	}
   1894 
   1895 	if (key_index == 0x0400 || key_index == 0x0500) {
   1896 		l_warn("Received an invalid IGTK key index (%04hx)"
   1897 				" that is likely in"
   1898 				" big endian format.  Trying to fix and"
   1899 				" proceed anyway", key_index);
   1900 		key_index = bswap_16(key_index);
   1901 	}
   1902 
   1903 	msg = nl80211_build_new_key_group(netdev->index, cipher, key_index,
   1904 					igtk_buf, igtk_len, ipn, ipn_len, NULL);
   1905 
   1906 	nhs->group_management_new_key_cmd_id =
   1907 			l_genl_family_send(nl80211, msg,
   1908 				netdev_new_group_management_key_cb,
   1909 				nhs, NULL);
   1910 
   1911 	if (nhs->group_management_new_key_cmd_id > 0)
   1912 		return;
   1913 
   1914 	l_genl_msg_unref(msg);
   1915 	netdev_setting_keys_failed(nhs, -EIO);
   1916 }
   1917 
   1918 static struct l_genl_msg *netdev_build_cmd_set_key_tx(struct netdev *netdev)
   1919 {
   1920 	uint8_t key_mode = NL80211_KEY_SET_TX;
   1921 	struct l_genl_msg *msg = l_genl_msg_new_sized(NL80211_CMD_SET_KEY, 512);
   1922 
   1923 	l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN,
   1924 					netdev->handshake->aa);
   1925 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   1926 
   1927 	l_genl_msg_enter_nested(msg, NL80211_ATTR_KEY);
   1928 	l_genl_msg_append_attr(msg, NL80211_KEY_IDX, 1,
   1929 				&netdev->handshake->active_tk_index);
   1930 	l_genl_msg_append_attr(msg, NL80211_KEY_MODE, 1, &key_mode);
   1931 	l_genl_msg_leave_nested(msg);
   1932 
   1933 	return msg;
   1934 }
   1935 
   1936 static void netdev_new_pairwise_key_cb(struct l_genl_msg *msg, void *data)
   1937 {
   1938 	struct netdev_handshake_state *nhs = data;
   1939 	struct netdev *netdev = nhs->netdev;
   1940 	const uint8_t *addr = netdev_choose_key_address(nhs);
   1941 	int err = l_genl_msg_get_error(msg);
   1942 
   1943 	nhs->pairwise_new_key_cmd_id = 0;
   1944 
   1945 	if (err < 0) {
   1946 		const char *ext_error = l_genl_msg_get_extended_error(msg);
   1947 
   1948 		l_error("New Key for Pairwise Key failed for ifindex: %d:%s",
   1949 				netdev->index,
   1950 				ext_error ? ext_error : strerror(-err));
   1951 		goto error;
   1952 	}
   1953 
   1954 	/*
   1955 	 * Set the AUTHORIZED flag using a SET_STATION command even if
   1956 	 * we're already operational, it will not hurt during re-keying
   1957 	 * and is necessary after an FT.
   1958 	 */
   1959 	msg = nl80211_build_set_station_authorized(netdev->index, addr);
   1960 
   1961 	nhs->set_station_cmd_id =
   1962 		l_genl_family_send(nl80211, msg, netdev_set_station_cb,
   1963 					nhs, NULL);
   1964 	if (nhs->set_station_cmd_id > 0)
   1965 		return;
   1966 
   1967 	l_genl_msg_unref(msg);
   1968 	err = -EIO;
   1969 error:
   1970 	netdev_setting_keys_failed(nhs, err);
   1971 }
   1972 
   1973 static struct l_genl_msg *netdev_build_control_port_frame(struct netdev *netdev,
   1974 							const uint8_t *to,
   1975 							uint16_t proto,
   1976 							bool unencrypted,
   1977 							const void *body,
   1978 							size_t body_len)
   1979 {
   1980 	struct l_genl_msg *msg;
   1981 
   1982 	msg = l_genl_msg_new_sized(NL80211_CMD_CONTROL_PORT_FRAME,
   1983 							128 + body_len);
   1984 
   1985 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   1986 	l_genl_msg_append_attr(msg, NL80211_ATTR_FRAME, body_len, body);
   1987 	l_genl_msg_append_attr(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, 2,
   1988 				&proto);
   1989 	l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, to);
   1990 
   1991 	if (unencrypted)
   1992 		l_genl_msg_append_attr(msg,
   1993 				NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT, 0, NULL);
   1994 
   1995 	return msg;
   1996 }
   1997 
   1998 static void netdev_control_port_frame_cb(struct l_genl_msg *msg,
   1999 							void *user_data)
   2000 {
   2001 	int err = l_genl_msg_get_error(msg);
   2002 	const char *ext_error;
   2003 
   2004 	if (err >= 0)
   2005 		return;
   2006 
   2007 	ext_error = l_genl_msg_get_extended_error(msg);
   2008 	l_error("CMD_CONTROL_PORT failed: %s",
   2009 			ext_error ? ext_error : strerror(-err));
   2010 }
   2011 
   2012 static int netdev_control_port_write_pae(struct netdev *netdev,
   2013 						const uint8_t *dest,
   2014 						uint16_t proto,
   2015 						const struct eapol_frame *ef,
   2016 						bool noencrypt)
   2017 {
   2018 	int fd = l_io_get_fd(netdev->pae_io);
   2019 	struct sockaddr_ll sll;
   2020 	size_t frame_size = sizeof(struct eapol_header) +
   2021 					L_BE16_TO_CPU(ef->header.packet_len);
   2022 	ssize_t r;
   2023 
   2024 	memset(&sll, 0, sizeof(sll));
   2025 	sll.sll_family = AF_PACKET;
   2026 	sll.sll_ifindex = netdev->index;
   2027 	sll.sll_protocol = htons(proto);
   2028 	sll.sll_halen = ETH_ALEN;
   2029 	memcpy(sll.sll_addr, dest, ETH_ALEN);
   2030 
   2031 	r = sendto(fd, ef, frame_size, 0,
   2032 			(struct sockaddr *) &sll, sizeof(sll));
   2033 	if (r < 0)
   2034 		l_error("EAPoL write socket: %s", strerror(errno));
   2035 
   2036 	return r;
   2037 }
   2038 
   2039 static int netdev_control_port_frame(uint32_t ifindex,
   2040 					const uint8_t *dest, uint16_t proto,
   2041 					const struct eapol_frame *ef,
   2042 					bool noencrypt,
   2043 					void *user_data)
   2044 {
   2045 	struct l_genl_msg *msg;
   2046 	struct netdev *netdev;
   2047 	size_t frame_size;
   2048 
   2049 	netdev = netdev_find(ifindex);
   2050 	if (!netdev)
   2051 		return -ENOENT;
   2052 
   2053 	frame_size = sizeof(struct eapol_header) +
   2054 			L_BE16_TO_CPU(ef->header.packet_len);
   2055 
   2056 	if (!netdev->pae_over_nl80211)
   2057 		return netdev_control_port_write_pae(netdev, dest, proto,
   2058 							ef, noencrypt);
   2059 
   2060 	msg = netdev_build_control_port_frame(netdev, dest, proto, noencrypt,
   2061 						ef, frame_size);
   2062 	if (!msg)
   2063 		return -ENOMEM;
   2064 
   2065 	if (!l_genl_family_send(nl80211, msg, netdev_control_port_frame_cb,
   2066 				netdev, NULL)) {
   2067 		l_genl_msg_unref(msg);
   2068 		return -EINVAL;
   2069 	}
   2070 
   2071 	return 0;
   2072 }
   2073 
   2074 static int netdev_set_key_tx(struct netdev *netdev)
   2075 {
   2076 	struct netdev_handshake_state *nhs = l_container_of(netdev->handshake,
   2077 					struct netdev_handshake_state, super);
   2078 	struct l_genl_msg *msg = netdev_build_cmd_set_key_tx(netdev);
   2079 
   2080 	nhs->pairwise_set_key_tx_cmd_id = l_genl_family_send(nl80211, msg,
   2081 						netdev_new_pairwise_key_cb,
   2082 						nhs, NULL);
   2083 	if (nhs->pairwise_set_key_tx_cmd_id > 0)
   2084 		return 0;
   2085 
   2086 	l_genl_msg_unref(msg);
   2087 
   2088 	return -EIO;
   2089 }
   2090 
   2091 static void netdev_new_rx_pairwise_key_cb(struct l_genl_msg *msg, void *data)
   2092 {
   2093 	struct netdev_handshake_state *nhs = data;
   2094 	struct netdev *netdev = nhs->netdev;
   2095 	struct netdev_ext_key_info *info = netdev->ext_key_info;
   2096 	int err = l_genl_msg_get_error(msg);
   2097 
   2098 	nhs->pairwise_new_key_cmd_id = 0;
   2099 
   2100 	if (err < 0) {
   2101 		const char *ext_error = l_genl_msg_get_extended_error(msg);
   2102 
   2103 		l_error("New Key for RX Pairwise Key failed for ifindex: %d:%s",
   2104 				netdev->index,
   2105 				ext_error ? ext_error : strerror(-err));
   2106 		goto error;
   2107 	}
   2108 
   2109 	if (!info)
   2110 		return;
   2111 
   2112 	err = netdev_control_port_write_pae(netdev, nhs->super.aa, info->proto,
   2113 						info->frame, info->noencrypt);
   2114 	l_free(netdev->ext_key_info);
   2115 	netdev->ext_key_info = NULL;
   2116 
   2117 	if (err < 0)
   2118 		goto error;
   2119 
   2120 	err = netdev_set_key_tx(netdev);
   2121 	if (err < 0)
   2122 		goto error;
   2123 
   2124 	return;
   2125 
   2126 error:
   2127 	netdev_setting_keys_failed(nhs, err);
   2128 }
   2129 
   2130 static void netdev_group_timeout_cb(struct l_timeout *timeout, void *user_data)
   2131 {
   2132 	struct netdev_handshake_state *nhs = user_data;
   2133 
   2134 	/*
   2135 	 * There was a problem with the ptk, this should have triggered a key
   2136 	 * setting failure event already.
   2137 	 */
   2138 	if (!nhs->ptk_installed)
   2139 		return;
   2140 
   2141 	/*
   2142 	 * If this happens, we never completed the group handshake. We can still
   2143 	 * complete the connection, but we will not have group traffic.
   2144 	 */
   2145 	l_warn("completing connection with no group traffic on ifindex %d",
   2146 			nhs->netdev->index);
   2147 
   2148 	nhs->complete = true;
   2149 
   2150 	if (handshake_event(&nhs->super, HANDSHAKE_EVENT_COMPLETE))
   2151 		return;
   2152 
   2153 	netdev_connect_ok(nhs->netdev);
   2154 }
   2155 
   2156 static void netdev_set_tk(struct handshake_state *hs, uint8_t key_index,
   2157 				const uint8_t *tk, uint32_t cipher)
   2158 {
   2159 	struct netdev_handshake_state *nhs =
   2160 		l_container_of(hs, struct netdev_handshake_state, super);
   2161 	uint8_t tk_buf[32];
   2162 	struct netdev *netdev = nhs->netdev;
   2163 	struct l_genl_msg *msg;
   2164 	const uint8_t *addr = netdev_choose_key_address(nhs);
   2165 	int err;
   2166 
   2167 	nhs->ptk_installed = false;
   2168 
   2169 	/*
   2170 	 * WPA1 does the group handshake after the 4-way finishes so we can't
   2171 	 * rely on the gtk/igtk being set immediately after the ptk. Since
   2172 	 * 'gtk_installed' is initially set to true (to handle NO_GROUP_TRAFFIC)
   2173 	 * we must set it false so we don't notify that the connection was
   2174 	 * successful until we get the gtk/igtk callbacks. Note that we do not
   2175 	 * need to set igtk_installed false because the igtk could not happen at
   2176 	 * all.
   2177 	 */
   2178 	if (hs->wait_for_gtk) {
   2179 		nhs->gtk_installed = false;
   2180 
   2181 		netdev->group_handshake_timeout = l_timeout_create(2,
   2182 					netdev_group_timeout_cb, nhs, NULL);
   2183 	}
   2184 
   2185 	/*
   2186 	 * 802.11 Section 4.10.4.3:
   2187 	 * Because in an IBSS there are two 4-way handshakes between
   2188 	 * any two Supplicants and Authenticators, the pairwise key used
   2189 	 * between any two STAs is from the 4-way handshake initiated
   2190 	 * by the STA Authenticator with the higher MAC address...
   2191 	 */
   2192 	if (netdev->type == NL80211_IFTYPE_ADHOC &&
   2193 			memcmp(nhs->super.aa, nhs->super.spa, 6) < 0) {
   2194 		nhs->ptk_installed = true;
   2195 		try_handshake_complete(nhs);
   2196 		return;
   2197 	}
   2198 
   2199 	l_debug("ifindex=%d key_idx=%u", netdev->index, key_index);
   2200 
   2201 	err = -ENOENT;
   2202 	if (!netdev_copy_tk(tk_buf, tk, cipher, hs->authenticator))
   2203 		goto invalid_key;
   2204 
   2205 	msg = nl80211_build_new_key_pairwise(netdev->index, cipher, addr,
   2206 					tk_buf, crypto_cipher_key_len(cipher),
   2207 					key_index);
   2208 	nhs->pairwise_new_key_cmd_id =
   2209 		l_genl_family_send(nl80211, msg, netdev_new_pairwise_key_cb,
   2210 						nhs, NULL);
   2211 	if (nhs->pairwise_new_key_cmd_id > 0)
   2212 		return;
   2213 
   2214 	err = -EIO;
   2215 	l_genl_msg_unref(msg);
   2216 invalid_key:
   2217 	netdev_setting_keys_failed(nhs, err);
   2218 }
   2219 
   2220 static void netdev_set_ext_tk(struct handshake_state *hs, uint8_t key_idx,
   2221 				const uint8_t *tk, uint32_t cipher,
   2222 				const struct eapol_frame *step4, uint16_t proto,
   2223 				bool noencrypt)
   2224 {
   2225 	struct netdev_handshake_state *nhs =
   2226 		l_container_of(hs, struct netdev_handshake_state, super);
   2227 	uint8_t tk_buf[32];
   2228 	struct netdev *netdev = nhs->netdev;
   2229 	struct l_genl_msg *msg;
   2230 	const uint8_t *addr = netdev_choose_key_address(nhs);
   2231 	int err;
   2232 	size_t frame_size = sizeof(struct eapol_header) +
   2233 				L_BE16_TO_CPU(step4->header.packet_len);
   2234 
   2235 	err = -ENOENT;
   2236 	if (!netdev_copy_tk(tk_buf, tk, cipher, hs->authenticator))
   2237 		goto error;
   2238 
   2239 	msg = nl80211_build_new_rx_key_pairwise(netdev->index, cipher, addr,
   2240 					tk_buf, crypto_cipher_key_len(cipher),
   2241 					hs->active_tk_index);
   2242 	nhs->pairwise_new_key_cmd_id =
   2243 		l_genl_family_send(nl80211, msg, netdev_new_rx_pairwise_key_cb,
   2244 						nhs, NULL);
   2245 
   2246 	if (!nhs->pairwise_new_key_cmd_id)
   2247 		goto io_error;
   2248 
   2249 	/*
   2250 	 * Without control port we cannot guarantee the order that messages go
   2251 	 * out and must wait for NEW_KEY to call back before sending message 4
   2252 	 */
   2253 	if (!netdev->pae_over_nl80211) {
   2254 		netdev->ext_key_info = l_malloc(
   2255 					sizeof(struct netdev_ext_key_info) +
   2256 					frame_size);
   2257 		memcpy(netdev->ext_key_info->frame, step4, frame_size);
   2258 		netdev->ext_key_info->proto = proto;
   2259 		netdev->ext_key_info->noencrypt = noencrypt;
   2260 		return;
   2261 	}
   2262 
   2263 	/*
   2264 	 * Otherwise, order of messages will be guaranteed. Therefore we can
   2265 	 * send send message 4, and set the TK to TX (below) without waiting for
   2266 	 * any callbacks
   2267 	 */
   2268 	err = netdev_control_port_frame(netdev->index, netdev->handshake->aa,
   2269 					proto, step4, noencrypt, NULL);
   2270 	if (err < 0)
   2271 		goto error;
   2272 
   2273 	/* Then toggle to RX + TX */
   2274 	err = netdev_set_key_tx(netdev);
   2275 	if (err < 0)
   2276 		goto error;
   2277 
   2278 	return;
   2279 
   2280 io_error:
   2281 	err = -EIO;
   2282 	l_genl_msg_unref(msg);
   2283 
   2284 error:
   2285 	netdev_setting_keys_failed(nhs, err);
   2286 }
   2287 
   2288 static void netdev_set_pmk_cb(struct l_genl_msg *msg, void *user_data)
   2289 {
   2290 	struct netdev_handshake_state *nhs = user_data;
   2291 	struct netdev *netdev = nhs->netdev;
   2292 	int err = l_genl_msg_get_error(msg);
   2293 
   2294 	nhs->set_pmk_cmd_id = 0;
   2295 
   2296 	if (err < 0) {
   2297 		l_error("Error with SET_PMK/SET_STATION");
   2298 		netdev_setting_keys_failed(nhs, err);
   2299 		return;
   2300 	}
   2301 
   2302 	if (handshake_event(netdev->handshake, HANDSHAKE_EVENT_SETTING_KEYS))
   2303 		return;
   2304 
   2305 	netdev_connect_ok(netdev);
   2306 }
   2307 
   2308 static void netdev_set_pmk(struct handshake_state *hs, const uint8_t *pmk,
   2309 				size_t pmk_len)
   2310 {
   2311 	struct l_genl_msg *msg;
   2312 	struct netdev_handshake_state *nhs = l_container_of(hs,
   2313 				struct netdev_handshake_state, super);
   2314 	struct netdev *netdev = nhs->netdev;
   2315 
   2316 	/* Only relevant for 8021x offload */
   2317 	if (nhs->type != CONNECTION_TYPE_8021X_OFFLOAD)
   2318 		return;
   2319 
   2320 	msg = l_genl_msg_new(NL80211_CMD_SET_PMK);
   2321 
   2322 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   2323 	l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, 6, netdev->handshake->aa);
   2324 	l_genl_msg_append_attr(msg, NL80211_ATTR_PMK,
   2325 				netdev->handshake->pmk_len,
   2326 				netdev->handshake->pmk);
   2327 
   2328 	nhs->set_pmk_cmd_id = l_genl_family_send(nl80211, msg,
   2329 							netdev_set_pmk_cb,
   2330 							nhs, NULL);
   2331 	if (!nhs->set_pmk_cmd_id) {
   2332 		l_error("Failed to set SET_PMK");
   2333 		netdev_setting_keys_failed(nhs, -EIO);
   2334 		return;
   2335 	}
   2336 }
   2337 
   2338 void netdev_handshake_failed(struct handshake_state *hs, uint16_t reason_code)
   2339 {
   2340 	struct netdev_handshake_state *nhs =
   2341 		l_container_of(hs, struct netdev_handshake_state, super);
   2342 	struct netdev *netdev = nhs->netdev;
   2343 	struct l_genl_msg *msg;
   2344 
   2345 	l_error("4-Way handshake failed for ifindex: %d, reason: %u",
   2346 				netdev->index, reason_code);
   2347 
   2348 	netdev->sm = NULL;
   2349 
   2350 	switch (netdev->type) {
   2351 	case NL80211_IFTYPE_STATION:
   2352 	case NL80211_IFTYPE_P2P_CLIENT:
   2353 		netdev_disconnect_by_sme(netdev, NETDEV_RESULT_HANDSHAKE_FAILED,
   2354 						reason_code);
   2355 		break;
   2356 	case NL80211_IFTYPE_AP:
   2357 	case NL80211_IFTYPE_P2P_GO:
   2358 		msg = nl80211_build_del_station(netdev->index, nhs->super.spa,
   2359 				reason_code,
   2360 				MPDU_MANAGEMENT_SUBTYPE_DEAUTHENTICATION);
   2361 		if (!l_genl_family_send(nl80211, msg, NULL, NULL, NULL))
   2362 			l_error("error sending DEL_STATION");
   2363 	}
   2364 
   2365 	if (netdev->work.id)
   2366 		wiphy_radio_work_done(netdev->wiphy, netdev->work.id);
   2367 }
   2368 
   2369 static void hardware_rekey_cb(struct l_genl_msg *msg, void *data)
   2370 {
   2371 	struct netdev *netdev = data;
   2372 	int err;
   2373 
   2374 	netdev->rekey_offload_cmd_id = 0;
   2375 
   2376 	err = l_genl_msg_get_error(msg);
   2377 	if (err < 0) {
   2378 		if (err == -EOPNOTSUPP) {
   2379 			l_error("hardware_rekey not supported");
   2380 			netdev->rekey_offload_support = false;
   2381 		}
   2382 
   2383 		/*
   2384 		 * TODO: Ignore all other errors for now, until WoWLAN is
   2385 		 * supported properly
   2386 		 */
   2387 	}
   2388 }
   2389 
   2390 static void netdev_set_rekey_offload(uint32_t ifindex,
   2391 					const uint8_t *kek,
   2392 					const uint8_t *kck,
   2393 					uint64_t replay_counter,
   2394 					void *user_data)
   2395 {
   2396 	struct netdev *netdev;
   2397 	struct l_genl_msg *msg;
   2398 
   2399 	netdev = netdev_find(ifindex);
   2400 	if (!netdev)
   2401 		return;
   2402 
   2403 	if (netdev->type != NL80211_IFTYPE_STATION)
   2404 		return;
   2405 
   2406 	if (!netdev->rekey_offload_support)
   2407 		return;
   2408 
   2409 	l_debug("%d", netdev->index);
   2410 	msg = nl80211_build_rekey_offload(ifindex, kek, kck, replay_counter);
   2411 	netdev->rekey_offload_cmd_id = l_genl_family_send(nl80211, msg,
   2412 							hardware_rekey_cb,
   2413 							netdev, NULL);
   2414 }
   2415 
   2416 static void netdev_qos_map_cb(struct l_genl_msg *msg, void *user_data)
   2417 {
   2418 	struct netdev *netdev = user_data;
   2419 	int err = l_genl_msg_get_error(msg);
   2420 	const char *ext_error;
   2421 
   2422 	netdev->qos_map_cmd_id = 0;
   2423 
   2424 	if (err >= 0)
   2425 		return;
   2426 
   2427 	ext_error = l_genl_msg_get_extended_error(msg);
   2428 	l_error("Could not set QoS Map in kernel: %s",
   2429 			ext_error ? ext_error : strerror(-err));
   2430 }
   2431 
   2432 /*
   2433  * TODO: Fix this in the kernel:
   2434  *
   2435  * The QoS Map is really of no use to IWD. The kernel requires it to map QoS
   2436  * network values properly to what it puts into the IP header. The way we have
   2437  * to let the kernel know is to receive the IE, then give it right back...
   2438  *
   2439  * The kernel/driver/firmware *could* simply obtain this information as the
   2440  * frame comes in and not require userspace to forward it back... but that's a
   2441  * battle for another day.
   2442  */
   2443 static void netdev_send_qos_map_set(struct netdev *netdev,
   2444 					const uint8_t *qos_set, size_t qos_len)
   2445 {
   2446 	struct l_genl_msg *msg;
   2447 
   2448 	if (!wiphy_supports_qos_set_map(netdev->wiphy)) {
   2449 		l_warn("AP sent QoS Map, but capability was not advertised!");
   2450 		return;
   2451 	}
   2452 
   2453 	/*
   2454 	 * Since this IE comes in on either a management frame or during
   2455 	 * Association response we could have potentially already set this.
   2456 	 */
   2457 	if (netdev->qos_map_cmd_id)
   2458 		return;
   2459 
   2460 	msg = l_genl_msg_new_sized(NL80211_CMD_SET_QOS_MAP, 128 + qos_len);
   2461 
   2462 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   2463 	l_genl_msg_append_attr(msg, NL80211_ATTR_QOS_MAP, qos_len, qos_set);
   2464 
   2465 	netdev->qos_map_cmd_id = l_genl_family_send(nl80211, msg,
   2466 						netdev_qos_map_cb,
   2467 						netdev, NULL);
   2468 }
   2469 
   2470 static void netdev_get_oci_cb(struct l_genl_msg *msg, void *user_data)
   2471 {
   2472 	struct netdev *netdev = user_data;
   2473 	int err = l_genl_msg_get_error(msg);
   2474 	_auto_(l_free) struct band_chandef *chandef =
   2475 						l_new(struct band_chandef, 1);
   2476 
   2477 	netdev->get_oci_cmd_id = 0;
   2478 
   2479 	if (err < 0) {
   2480 		const char *ext_error = l_genl_msg_get_extended_error(msg);
   2481 
   2482 		l_error("Could not get OCI info: %s",
   2483 				ext_error ? ext_error : strerror(-err));
   2484 		goto done;
   2485 	}
   2486 
   2487 	if (nl80211_parse_chandef(msg, chandef) < 0) {
   2488 		l_debug("Couldn't parse operating channel info.");
   2489 		goto done;
   2490 	}
   2491 
   2492 	l_debug("Obtained OCI: freq: %u, width: %u, center1: %u, center2: %u",
   2493 			chandef->frequency, chandef->channel_width,
   2494 			chandef->center1_frequency, chandef->center2_frequency);
   2495 
   2496 	handshake_state_set_chandef(netdev->handshake, l_steal_ptr(chandef));
   2497 
   2498 done:
   2499 	if (netdev->ap) {
   2500 		/*
   2501 		 * Can't do much here. IWD assumes every kernel/driver supports
   2502 		 * this. There is no way of detecting support either.
   2503 		 */
   2504 		if (L_WARN_ON(err < 0))
   2505 			netdev_connect_failed(netdev,
   2506 					NETDEV_RESULT_AUTHENTICATION_FAILED,
   2507 					MMPDU_STATUS_CODE_UNSPECIFIED);
   2508 		else
   2509 			auth_proto_rx_oci(netdev->ap);
   2510 
   2511 		return;
   2512 	}
   2513 
   2514 	L_WARN_ON(!eapol_start(netdev->sm));
   2515 }
   2516 
   2517 static int netdev_get_oci(void *user_data)
   2518 {
   2519 	struct netdev *netdev = user_data;
   2520 	struct l_genl_msg *msg =
   2521 			l_genl_msg_new_sized(NL80211_CMD_GET_INTERFACE, 64);
   2522 
   2523 	l_debug("");
   2524 
   2525 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   2526 
   2527 	netdev->get_oci_cmd_id = l_genl_family_send(nl80211, msg,
   2528 						netdev_get_oci_cb, netdev,
   2529 						NULL);
   2530 	if (!netdev->get_oci_cmd_id) {
   2531 		l_genl_msg_unref(msg);
   2532 		return -EIO;
   2533 	}
   2534 
   2535 	return 0;
   2536 }
   2537 
   2538 static void parse_request_ies(struct netdev *netdev, const uint8_t *ies,
   2539 				size_t ies_len)
   2540 {
   2541 	struct ie_tlv_iter iter;
   2542 	const void *data;
   2543 	const uint8_t *rsnxe = NULL;
   2544 
   2545 	l_debug("");
   2546 
   2547 	/*
   2548 	 * The driver may have modified the IEs we passed to CMD_CONNECT
   2549 	 * before sending them out, the actual IE sent is reflected in the
   2550 	 * ATTR_REQ_IE sequence.  These are the values EAPoL will need to use.
   2551 	 */
   2552 	ie_tlv_iter_init(&iter, ies, ies_len);
   2553 
   2554 	while (ie_tlv_iter_next(&iter)) {
   2555 		data = ie_tlv_iter_get_data(&iter);
   2556 
   2557 		switch (ie_tlv_iter_get_tag(&iter)) {
   2558 		case IE_TYPE_RSN:
   2559 			handshake_state_set_supplicant_ie(netdev->handshake,
   2560 								data - 2);
   2561 			break;
   2562 		case IE_TYPE_RSNX:
   2563 			if (!rsnxe)
   2564 				rsnxe = data - 2;
   2565 			break;
   2566 		case IE_TYPE_VENDOR_SPECIFIC:
   2567 			if (!is_ie_wpa_ie(data, ie_tlv_iter_get_length(&iter)))
   2568 				break;
   2569 
   2570 			handshake_state_set_supplicant_ie(netdev->handshake,
   2571 								data - 2);
   2572 			break;
   2573 		case IE_TYPE_MOBILITY_DOMAIN:
   2574 			handshake_state_set_mde(netdev->handshake, data - 2);
   2575 			break;
   2576 		}
   2577 	}
   2578 
   2579 	/* RSNXE element might be omitted when FTing */
   2580 	handshake_state_set_supplicant_rsnxe(netdev->handshake, rsnxe);
   2581 }
   2582 
   2583 static void netdev_driver_connected(struct netdev *netdev)
   2584 {
   2585 	netdev->connected = true;
   2586 
   2587 	/*
   2588 	 * We register the eapol state machine here, in case the PAE
   2589 	 * socket receives EAPoL packets before the nl80211 socket
   2590 	 * receives the connected event.  The logical sequence of
   2591 	 * events can be reversed (e.g. connect_event, then PAE data)
   2592 	 * due to scheduling
   2593 	 */
   2594 	if (netdev->sm)
   2595 		eapol_register(netdev->sm);
   2596 }
   2597 
   2598 static struct l_genl_msg *netdev_build_cmd_connect(struct netdev *netdev,
   2599 						struct handshake_state *hs,
   2600 						const uint8_t *prev_bssid)
   2601 {
   2602 	struct netdev_handshake_state *nhs =
   2603 		l_container_of(hs, struct netdev_handshake_state, super);
   2604 	/*
   2605 	 * Choose Open system auth type if PMKSA caching is used for an SAE AKM:
   2606 	 *
   2607 	 * IEEE 802.11-2020 Table 9-151
   2608 	 *   - SAE authentication:
   2609 	 *       3 (SAE) for SAE Authentication
   2610 	 *       0 (open) for PMKSA caching
   2611 	 *   - FT authentication over SAE:
   2612 	 *       3 (SAE) for FT Initial Mobility Domain Association
   2613 	 *       0 (open) for FT Initial Mobility Domain Association over
   2614 	 *         PMKSA caching
   2615 	 */
   2616 	uint32_t auth_type = IE_AKM_IS_SAE(hs->akm_suite) && !hs->have_pmksa ?
   2617 					NL80211_AUTHTYPE_SAE :
   2618 					NL80211_AUTHTYPE_OPEN_SYSTEM;
   2619 	enum mpdu_management_subtype subtype = prev_bssid ?
   2620 				MPDU_MANAGEMENT_SUBTYPE_REASSOCIATION_REQUEST :
   2621 				MPDU_MANAGEMENT_SUBTYPE_ASSOCIATION_REQUEST;
   2622 	struct l_genl_msg *msg;
   2623 	struct iovec iov[64];
   2624 	unsigned int n_iov = L_ARRAY_SIZE(iov);
   2625 	unsigned int c_iov = 0;
   2626 	bool is_rsn = hs->supplicant_ie != NULL;
   2627 	uint8_t owe_dh_ie[5 + L_ECC_SCALAR_MAX_BYTES];
   2628 	size_t dh_ie_len;
   2629 
   2630 	msg = l_genl_msg_new_sized(NL80211_CMD_CONNECT, 512);
   2631 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   2632 	l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY_FREQ,
   2633 							4, &netdev->frequency);
   2634 	l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, hs->aa);
   2635 	l_genl_msg_append_attr(msg, NL80211_ATTR_SSID, hs->ssid_len, hs->ssid);
   2636 	l_genl_msg_append_attr(msg, NL80211_ATTR_AUTH_TYPE, 4, &auth_type);
   2637 
   2638 	switch (nhs->type) {
   2639 	case CONNECTION_TYPE_SOFTMAC:
   2640 		break;
   2641 	case CONNECTION_TYPE_FULLMAC:
   2642 		l_genl_msg_append_attr(msg,
   2643 				NL80211_ATTR_EXTERNAL_AUTH_SUPPORT, 0, NULL);
   2644 		break;
   2645 	case CONNECTION_TYPE_SAE_OFFLOAD:
   2646 		l_genl_msg_append_attr(msg, NL80211_ATTR_SAE_PASSWORD,
   2647 					strlen(hs->passphrase), hs->passphrase);
   2648 		break;
   2649 	case CONNECTION_TYPE_PSK_OFFLOAD:
   2650 		l_genl_msg_append_attr(msg, NL80211_ATTR_PMK, 32, hs->pmk);
   2651 		break;
   2652 	case CONNECTION_TYPE_8021X_OFFLOAD:
   2653 		l_genl_msg_append_attr(msg, NL80211_ATTR_WANT_1X_4WAY_HS,
   2654 					0, NULL);
   2655 	}
   2656 
   2657 	if (prev_bssid)
   2658 		l_genl_msg_append_attr(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
   2659 						prev_bssid);
   2660 
   2661 	if (netdev->privacy)
   2662 		l_genl_msg_append_attr(msg, NL80211_ATTR_PRIVACY, 0, NULL);
   2663 
   2664 	l_genl_msg_append_attr(msg, NL80211_ATTR_SOCKET_OWNER, 0, NULL);
   2665 
   2666 	if (is_rsn) {
   2667 		nl80211_append_rsn_attributes(msg, hs);
   2668 		c_iov = iov_ie_append(iov, n_iov, c_iov, hs->supplicant_ie,
   2669 					IE_LEN(hs->supplicant_ie));
   2670 	}
   2671 
   2672 	if (is_rsn || hs->settings_8021x) {
   2673 		l_genl_msg_append_attr(msg, NL80211_ATTR_CONTROL_PORT,
   2674 						0, NULL);
   2675 
   2676 		if (netdev->pae_over_nl80211)
   2677 			l_genl_msg_append_attr(msg,
   2678 					NL80211_ATTR_CONTROL_PORT_OVER_NL80211,
   2679 					0, NULL);
   2680 	}
   2681 
   2682 	if (netdev->owe_sm) {
   2683 		owe_build_dh_ie(netdev->owe_sm, owe_dh_ie, &dh_ie_len);
   2684 		c_iov = iov_ie_append(iov, n_iov, c_iov, owe_dh_ie, dh_ie_len);
   2685 	}
   2686 
   2687 	c_iov = iov_ie_append(iov, n_iov, c_iov, hs->mde, IE_LEN(hs->mde));
   2688 	c_iov = netdev_populate_common_ies(netdev, hs, msg, iov, n_iov, c_iov);
   2689 
   2690 	mpdu_sort_ies(subtype, iov, c_iov);
   2691 
   2692 	if (c_iov)
   2693 		l_genl_msg_append_attrv(msg, NL80211_ATTR_IE, iov, c_iov);
   2694 
   2695 	return msg;
   2696 }
   2697 
   2698 static void netdev_cmd_connect_cb(struct l_genl_msg *msg, void *user_data)
   2699 {
   2700 	struct netdev *netdev = user_data;
   2701 
   2702 	netdev->connect_cmd_id = 0;
   2703 
   2704 	if (l_genl_msg_get_error(msg) >= 0) {
   2705 		/*
   2706 		 * connected should be false if the connect event hasn't come
   2707 		 * in yet.  i.e. the CMD_CONNECT ack arrived first (typical).
   2708 		 * Mark the connection as 'connected'
   2709 		 */
   2710 		if (!netdev->connected)
   2711 			netdev_driver_connected(netdev);
   2712 
   2713 		return;
   2714 	}
   2715 
   2716 	netdev_connect_failed(netdev, NETDEV_RESULT_ASSOCIATION_FAILED,
   2717 				MMPDU_STATUS_CODE_UNSPECIFIED);
   2718 }
   2719 
   2720 static bool netdev_retry_owe(struct netdev *netdev)
   2721 {
   2722 	struct l_genl_msg *connect_cmd;
   2723 
   2724 	if (!owe_next_group(netdev->owe_sm))
   2725 		return false;
   2726 
   2727 	if (netdev->event_filter)
   2728 		netdev->event_filter(netdev, NETDEV_EVENT_ECC_GROUP_RETRY,
   2729 					NULL, netdev->user_data);
   2730 
   2731 	connect_cmd = netdev_build_cmd_connect(netdev, netdev->handshake, NULL);
   2732 
   2733 	netdev->connect_cmd_id = l_genl_family_send(nl80211, connect_cmd,
   2734 						netdev_cmd_connect_cb, netdev,
   2735 						NULL);
   2736 
   2737 	if (netdev->connect_cmd_id > 0)
   2738 		return true;
   2739 
   2740 	l_genl_msg_unref(connect_cmd);
   2741 	return false;
   2742 }
   2743 
   2744 static void netdev_connect_event(struct l_genl_msg *msg, struct netdev *netdev)
   2745 {
   2746 	struct l_genl_attr attr;
   2747 	uint16_t type, len;
   2748 	const void *data;
   2749 	const uint16_t *status_code = NULL;
   2750 	const uint8_t *ies = NULL;
   2751 	size_t ies_len = 0;
   2752 	struct ie_tlv_iter iter;
   2753 	const uint8_t *resp_ies = NULL;
   2754 	size_t resp_ies_len = 0;
   2755 	struct handshake_state *hs = netdev->handshake;
   2756 	bool timeout = false;
   2757 	uint32_t timeout_reason = 0;
   2758 
   2759 	l_debug("");
   2760 
   2761 	if (netdev->aborting)
   2762 		return;
   2763 
   2764 	if (netdev->ignore_connect_event)
   2765 		return;
   2766 
   2767 	l_debug("aborting and ignore_connect_event not set, proceed");
   2768 
   2769 	/* Work around mwifiex which sends a Connect Event prior to the Ack */
   2770 	if (netdev->connect_cmd_id)
   2771 		netdev_driver_connected(netdev);
   2772 
   2773 	if (!netdev->connected) {
   2774 		l_warn("Unexpected connection related event -- "
   2775 				"is another supplicant running?");
   2776 		return;
   2777 	}
   2778 
   2779 	if (!l_genl_attr_init(&attr, msg)) {
   2780 		l_debug("attr init failed");
   2781 		goto error;
   2782 	}
   2783 
   2784 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
   2785 		switch (type) {
   2786 		case NL80211_ATTR_TIMED_OUT:
   2787 			timeout = true;
   2788 			break;
   2789 		case NL80211_ATTR_TIMEOUT_REASON:
   2790 			if (len != 4)
   2791 				break;
   2792 
   2793 			timeout_reason = l_get_u32(data);
   2794 			break;
   2795 		case NL80211_ATTR_STATUS_CODE:
   2796 			if (len == sizeof(uint16_t))
   2797 				status_code = data;
   2798 			break;
   2799 		case NL80211_ATTR_REQ_IE:
   2800 			ies = data;
   2801 			ies_len = len;
   2802 			break;
   2803 		case NL80211_ATTR_RESP_IE:
   2804 			resp_ies = data;
   2805 			resp_ies_len = len;
   2806 			break;
   2807 		}
   2808 	}
   2809 
   2810 	if (timeout) {
   2811 		iwd_notice(IWD_NOTICE_CONNECT_TIMEOUT, "reason: %u",
   2812 				timeout_reason);
   2813 		goto error;
   2814 	}
   2815 
   2816 	if (netdev->expect_connect_failure) {
   2817 		/*
   2818 		 * The kernel may think we are connected when we are actually
   2819 		 * expecting a failure here, e.g. if Authenticate/Associate had
   2820 		 * previously failed. If so we need to deauth to let the kernel
   2821 		 * know.
   2822 		 */
   2823 		if (status_code && *status_code == 0)
   2824 			goto deauth;
   2825 		else
   2826 			goto error;
   2827 	}
   2828 
   2829 	l_debug("expect_connect_failure not set, proceed");
   2830 
   2831 	if (netdev->owe_sm && status_code && *status_code ==
   2832 				MMPDU_STATUS_CODE_UNSUPP_FINITE_CYCLIC_GROUP) {
   2833 		if (!netdev_retry_owe(netdev))
   2834 			goto error;
   2835 
   2836 		return;
   2837 	}
   2838 
   2839 	/* AP Rejected the authenticate / associate */
   2840 	if (!status_code || *status_code != 0)
   2841 		goto error;
   2842 
   2843 	if (!ies)
   2844 		goto process_resp_ies;
   2845 
   2846 	parse_request_ies(netdev, ies, ies_len);
   2847 
   2848 process_resp_ies:
   2849 	if (resp_ies) {
   2850 		const uint8_t *fte = NULL;
   2851 		const uint8_t *qos_set = NULL;
   2852 		const uint8_t *owe_dh = NULL;
   2853 		size_t owe_dh_len = 0;
   2854 		size_t qos_len = 0;
   2855 		struct ie_ft_info ft_info;
   2856 		struct ie_rsn_info info;
   2857 		bool owe_akm_found = false;
   2858 
   2859 		ie_tlv_iter_init(&iter, resp_ies, resp_ies_len);
   2860 
   2861 		while (ie_tlv_iter_next(&iter)) {
   2862 			data = ie_tlv_iter_get_data(&iter);
   2863 
   2864 			switch (ie_tlv_iter_get_tag(&iter)) {
   2865 			case IE_TYPE_FAST_BSS_TRANSITION:
   2866 				fte = data - 2;
   2867 				break;
   2868 			case IE_TYPE_QOS_MAP_SET:
   2869 				qos_set = data;
   2870 				qos_len = ie_tlv_iter_get_length(&iter);
   2871 				break;
   2872 			case IE_TYPE_FILS_IP_ADDRESS:
   2873 				if (hs->fils_ip_resp_ie) {
   2874 					l_debug("Duplicate response FILS IP "
   2875 						"Address Assignment IE");
   2876 					l_free(hs->fils_ip_resp_ie);
   2877 				}
   2878 
   2879 				hs->fils_ip_resp_ie = l_memdup(data - 3,
   2880 					ie_tlv_iter_get_length(&iter) + 3);
   2881 				break;
   2882 			case IE_TYPE_OWE_DH_PARAM:
   2883 				if (!netdev->owe_sm)
   2884 					continue;
   2885 
   2886 				owe_dh = data;
   2887 				owe_dh_len = ie_tlv_iter_get_length(&iter);
   2888 
   2889 				break;
   2890 
   2891 			case IE_TYPE_RSN:
   2892 				if (!netdev->owe_sm)
   2893 					continue;
   2894 
   2895 				if (ie_parse_rsne(&iter, &info) < 0) {
   2896 					l_error("could not parse RSN IE");
   2897 					goto deauth;
   2898 				}
   2899 
   2900 				/*
   2901 				 * RFC 8110 Section 4.2
   2902 				 * An AP agreeing to do OWE MUST include the
   2903 				 * OWE AKM in the RSN element portion of the
   2904 				 * 802.11 association response.
   2905 				 */
   2906 				if (info.akm_suites != IE_RSN_AKM_SUITE_OWE) {
   2907 					l_error("OWE AKM not included");
   2908 					goto deauth;
   2909 				}
   2910 
   2911 				owe_akm_found = true;
   2912 
   2913 				break;
   2914 			}
   2915 		}
   2916 
   2917 		if (netdev->owe_sm) {
   2918 			if (!owe_dh) {
   2919 				l_error("OWE DH element not found");
   2920 				goto deauth;
   2921 			}
   2922 
   2923 			if (!owe_akm_found)
   2924 				l_warn("OWE AKM was not included in the RSNE. "
   2925 					"This AP is out of spec!");
   2926 
   2927 			if (L_WARN_ON(owe_process_dh_ie(netdev->owe_sm, owe_dh,
   2928 							owe_dh_len) != 0))
   2929 				goto deauth;
   2930 
   2931 			owe_sm_free(netdev->owe_sm);
   2932 			netdev->owe_sm = NULL;
   2933 		}
   2934 
   2935 		if (fte) {
   2936 			uint32_t kck_len =
   2937 				handshake_state_get_kck_len(hs);
   2938 			/*
   2939 			 * If we are here, then most likely we have a FullMac
   2940 			 * hw performing initial mobility association.  We need
   2941 			 * to set the FTE element or the handshake will fail
   2942 			 * The firmware accepted the FTE element, so do not
   2943 			 * sanitize the contents and just assume they're okay.
   2944 			 */
   2945 			if (ie_parse_fast_bss_transition_from_data(fte,
   2946 					fte[1] + 2, kck_len, &ft_info) >= 0) {
   2947 				handshake_state_set_authenticator_fte(hs, fte);
   2948 				handshake_state_set_kh_ids(hs,
   2949 							ft_info.r0khid,
   2950 							ft_info.r0khid_len,
   2951 							ft_info.r1khid);
   2952 			} else {
   2953 				l_info("CMD_CONNECT Succeeded, but parsing FTE"
   2954 					" failed.  Expect handshake failure");
   2955 			}
   2956 		}
   2957 
   2958 		if (qos_set)
   2959 			netdev_send_qos_map_set(netdev, qos_set, qos_len);
   2960 	}
   2961 
   2962 	l_debug("Request / Response IEs parsed");
   2963 
   2964 	if (netdev->sm) {
   2965 		if (!hs->chandef) {
   2966 			if (netdev_get_oci(netdev) < 0)
   2967 				goto deauth;
   2968 		} else if (!eapol_start(netdev->sm))
   2969 			goto deauth;
   2970 
   2971 		return;
   2972 	}
   2973 
   2974 	/* Allow station to sync the PSK to disk */
   2975 	if (is_offload(hs) && handshake_event(hs, HANDSHAKE_EVENT_SETTING_KEYS))
   2976 		return;
   2977 
   2978 	netdev_connect_ok(netdev);
   2979 	return;
   2980 
   2981 error:
   2982 	netdev_connect_failed(netdev, NETDEV_RESULT_ASSOCIATION_FAILED,
   2983 			(status_code) ? *status_code :
   2984 			MMPDU_STATUS_CODE_UNSPECIFIED);
   2985 	return;
   2986 
   2987 deauth:
   2988 	netdev_disconnect_and_fail_connection(netdev,
   2989 					NETDEV_RESULT_ASSOCIATION_FAILED,
   2990 					MMPDU_STATUS_CODE_UNSPECIFIED);
   2991 }
   2992 
   2993 static struct l_genl_msg *netdev_build_cmd_associate_common(
   2994 							struct netdev *netdev)
   2995 {
   2996 	struct handshake_state *hs = netdev->handshake;
   2997 	bool is_rsn = hs->supplicant_ie != NULL;
   2998 	struct l_genl_msg *msg;
   2999 
   3000 	msg = l_genl_msg_new_sized(NL80211_CMD_ASSOCIATE, 600);
   3001 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   3002 	l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY_FREQ, 4,
   3003 							&netdev->frequency);
   3004 	l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, hs->aa);
   3005 	l_genl_msg_append_attr(msg, NL80211_ATTR_SSID, hs->ssid_len, hs->ssid);
   3006 	l_genl_msg_append_attr(msg, NL80211_ATTR_SOCKET_OWNER, 0, NULL);
   3007 
   3008 	if (is_rsn)
   3009 		nl80211_append_rsn_attributes(msg, hs);
   3010 
   3011 	if (is_rsn || hs->settings_8021x) {
   3012 		l_genl_msg_append_attr(msg, NL80211_ATTR_CONTROL_PORT,
   3013 						0, NULL);
   3014 
   3015 		if (netdev->pae_over_nl80211)
   3016 			l_genl_msg_append_attr(msg,
   3017 					NL80211_ATTR_CONTROL_PORT_OVER_NL80211,
   3018 					0, NULL);
   3019 	}
   3020 
   3021 	return msg;
   3022 }
   3023 
   3024 static void netdev_cmd_ft_reassociate_cb(struct l_genl_msg *msg,
   3025 						void *user_data)
   3026 {
   3027 	struct netdev *netdev = user_data;
   3028 	int err = l_genl_msg_get_error(msg);
   3029 
   3030 	netdev->connect_cmd_id = 0;
   3031 
   3032 	l_debug("%d", err);
   3033 
   3034 	if (err >= 0)
   3035 		return;
   3036 
   3037 	/*
   3038 	 * TODO: It is possible to not trigger a disconnect here and maintain
   3039 	 *       the current connection. The issue is that IWD has already
   3040 	 *       modified the handshake and we've lost all reference to the old
   3041 	 *       BSS keys.
   3042 	 *
   3043 	 *       This could be remedied in the future by creating an entirely
   3044 	 *       new handshake_state object for the association and only when
   3045 	 *       the ack indicates success do we clear out the old object.
   3046 	 */
   3047 	netdev_disconnect_and_fail_connection(netdev,
   3048 					NETDEV_RESULT_ASSOCIATION_FAILED,
   3049 					MMPDU_STATUS_CODE_UNSPECIFIED);
   3050 }
   3051 
   3052 static bool kernel_will_retry_auth(uint16_t status_code,
   3053 					uint16_t alg, uint16_t trans)
   3054 {
   3055 	/*
   3056 	 * Kernel keeps re-trying auth frames until told to stop
   3057 	 * when authentication succeeds and under certain SAE-related
   3058 	 * circumstances.  Detect these cases.
   3059 	 */
   3060 
   3061 	if (status_code == 0)
   3062 		return true;
   3063 
   3064 	if (alg != MMPDU_AUTH_ALGO_SAE)
   3065 		return false;
   3066 
   3067 	if (status_code == MMPDU_STATUS_CODE_ANTI_CLOGGING_TOKEN_REQ)
   3068 		return true;
   3069 
   3070 	if (trans == 1 && (status_code == MMPDU_STATUS_CODE_SAE_PK ||
   3071 			status_code == MMPDU_STATUS_CODE_SAE_HASH_TO_ELEMENT))
   3072 		return true;
   3073 
   3074 	return false;
   3075 }
   3076 
   3077 static void netdev_ensure_eapol_registered(struct netdev *netdev)
   3078 {
   3079 	if (netdev->sm)
   3080 		return;
   3081 
   3082 	netdev->sm = eapol_sm_new(netdev->handshake);
   3083 	eapol_register(netdev->sm);
   3084 }
   3085 
   3086 static void netdev_authenticate_event(struct l_genl_msg *msg,
   3087 							struct netdev *netdev)
   3088 {
   3089 	struct l_genl_attr attr;
   3090 	uint16_t type, len;
   3091 	const void *data;
   3092 	const uint8_t *frame = NULL;
   3093 	size_t frame_len = 0;
   3094 	int ret;
   3095 	uint16_t status_code = MMPDU_STATUS_CODE_UNSPECIFIED;
   3096 
   3097 	l_debug("");
   3098 
   3099 	if (netdev->aborting)
   3100 		return;
   3101 
   3102 	if (!netdev->connected) {
   3103 		l_warn("Unexpected connection related event -- "
   3104 				"is another supplicant running?");
   3105 		return;
   3106 	}
   3107 
   3108 	if (netdev->event_filter)
   3109 		netdev->event_filter(netdev, NETDEV_EVENT_AUTHENTICATING,
   3110 					NULL, netdev->user_data);
   3111 
   3112 	/*
   3113 	 * During Fast Transition we use the authenticate event to start the
   3114 	 * reassociation step because the FTE necessary before we can build
   3115 	 * the FT Associate command is included in the attached frame and is
   3116 	 * not available in the Authenticate command callback.
   3117 	 */
   3118 	if (!netdev->ap)
   3119 		return;
   3120 
   3121 	if (!l_genl_attr_init(&attr, msg)) {
   3122 		l_debug("attr init failed");
   3123 
   3124 		goto auth_error;
   3125 	}
   3126 
   3127 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
   3128 		switch (type) {
   3129 		case NL80211_ATTR_TIMED_OUT:
   3130 			iwd_notice(IWD_NOTICE_AUTH_TIMEOUT);
   3131 
   3132 			if (auth_proto_auth_timeout(netdev->ap))
   3133 				return;
   3134 
   3135 			goto auth_error;
   3136 
   3137 		case NL80211_ATTR_FRAME:
   3138 			if (frame)
   3139 				goto auth_error;
   3140 
   3141 			frame = data;
   3142 			frame_len = len;
   3143 			break;
   3144 		}
   3145 	}
   3146 
   3147 	if (L_WARN_ON(!frame))
   3148 		goto auth_error;
   3149 
   3150 	if (netdev->ap) {
   3151 		const struct mmpdu_header *hdr;
   3152 		const struct mmpdu_authentication *auth;
   3153 		bool retry;
   3154 
   3155 		hdr = mpdu_validate(frame, frame_len);
   3156 		if (L_WARN_ON(!hdr))
   3157 			goto auth_error;
   3158 
   3159 		auth = mmpdu_body(hdr);
   3160 		status_code = L_CPU_TO_LE16(auth->status);
   3161 
   3162 		ret = auth_proto_rx_authenticate(netdev->ap, frame, frame_len);
   3163 
   3164 		/*
   3165 		 * Allows station to persist settings so it does not retry
   3166 		 * the higher order ECC group again
   3167 		 */
   3168 		if (status_code ==
   3169 				MMPDU_STATUS_CODE_UNSUPP_FINITE_CYCLIC_GROUP &&
   3170 				netdev->event_filter)
   3171 			netdev->event_filter(netdev,
   3172 						NETDEV_EVENT_ECC_GROUP_RETRY,
   3173 						NULL, netdev->user_data);
   3174 
   3175 		/* We have sent another CMD_AUTHENTICATE / CMD_ASSOCIATE */
   3176 		if (ret == 0) {
   3177 			netdev_ensure_eapol_registered(netdev);
   3178 			return;
   3179 		}
   3180 
   3181 		if (ret == -EAGAIN)
   3182 			return;
   3183 
   3184 		retry = kernel_will_retry_auth(status_code,
   3185 				L_CPU_TO_LE16(auth->algorithm),
   3186 				L_CPU_TO_LE16(auth->transaction_sequence));
   3187 
   3188 		/*
   3189 		 * Spec wants us to silently drop these frames,
   3190 		 * if the kernel will keep retrying, let it
   3191 		 */
   3192 		if ((ret == -ENOMSG || ret == -EBADMSG) && retry)
   3193 			return;
   3194 
   3195 		if (ret > 0)
   3196 			status_code = (uint16_t)ret;
   3197 
   3198 		/*
   3199 		 * We have encountered a fatal error, if the kernel wants
   3200 		 * to keep retrying, tell it to stop
   3201 		 */
   3202 		if (retry) {
   3203 			netdev_deauth_and_fail_connection(netdev,
   3204 					NETDEV_RESULT_ASSOCIATION_FAILED,
   3205 					MMPDU_STATUS_CODE_UNSPECIFIED);
   3206 			return;
   3207 		}
   3208 	}
   3209 
   3210 auth_error:
   3211 	netdev_connect_failed(netdev, NETDEV_RESULT_AUTHENTICATION_FAILED,
   3212 				status_code);
   3213 }
   3214 
   3215 static void netdev_associate_event(struct l_genl_msg *msg,
   3216 							struct netdev *netdev)
   3217 {
   3218 	struct l_genl_attr attr;
   3219 	uint16_t type, len;
   3220 	const void *data;
   3221 	size_t frame_len = 0;
   3222 	const uint8_t *frame = NULL;
   3223 	uint16_t status_code = MMPDU_STATUS_CODE_UNSPECIFIED;
   3224 	int ret;
   3225 	const struct mmpdu_header *hdr;
   3226 	const struct mmpdu_association_response *assoc;
   3227 
   3228 	l_debug("");
   3229 
   3230 	if (!netdev->connected || netdev->aborting)
   3231 		return;
   3232 
   3233 	if (netdev->event_filter)
   3234 		netdev->event_filter(netdev, NETDEV_EVENT_ASSOCIATING,
   3235 					NULL, netdev->user_data);
   3236 
   3237 	if (!netdev->ap && !netdev->in_ft) {
   3238 		netdev->associated = true;
   3239 		netdev->in_reassoc = false;
   3240 		return;
   3241 	}
   3242 
   3243 	if (!l_genl_attr_init(&attr, msg)) {
   3244 		l_debug("attr init failed");
   3245 		return;
   3246 	}
   3247 
   3248 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
   3249 		switch (type) {
   3250 		case NL80211_ATTR_TIMED_OUT:
   3251 			iwd_notice(IWD_NOTICE_ASSOC_TIMEOUT);
   3252 
   3253 			if (auth_proto_assoc_timeout(netdev->ap))
   3254 				return;
   3255 
   3256 			/*
   3257 			 * There will be no connect event when Associate times
   3258 			 * out. The failed connection must be explicitly
   3259 			 * initiated here.
   3260 			 */
   3261 			netdev_connect_failed(netdev,
   3262 					NETDEV_RESULT_ASSOCIATION_FAILED,
   3263 					status_code);
   3264 			return;
   3265 
   3266 		case NL80211_ATTR_FRAME:
   3267 			frame = data;
   3268 			frame_len = len;
   3269 
   3270 			break;
   3271 		}
   3272 	}
   3273 
   3274 	if (L_WARN_ON(!frame))
   3275 		goto assoc_failed;
   3276 
   3277 	hdr = mpdu_validate(frame, frame_len);
   3278 	if (L_WARN_ON(!hdr))
   3279 		goto assoc_failed;
   3280 
   3281 	assoc = mmpdu_body(hdr);
   3282 	status_code = L_CPU_TO_LE16(assoc->status_code);
   3283 
   3284 	if (netdev->ap)
   3285 		ret = auth_proto_rx_associate(netdev->ap, frame,
   3286 							frame_len);
   3287 	else
   3288 		ret = __ft_rx_associate(netdev->index, frame,
   3289 							frame_len);
   3290 	if (ret == 0) {
   3291 		bool fils = !!(netdev->handshake->akm_suite &
   3292 				(IE_RSN_AKM_SUITE_FILS_SHA256 |
   3293 				 IE_RSN_AKM_SUITE_FILS_SHA384 |
   3294 				 IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384 |
   3295 				 IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256));
   3296 
   3297 		if (netdev->ap) {
   3298 			auth_proto_free(netdev->ap);
   3299 			netdev->ap = NULL;
   3300 		}
   3301 
   3302 		/* Just in case this was a retry */
   3303 		netdev->ignore_connect_event = false;
   3304 
   3305 		/*
   3306 		 * If in FT and/or FILS we don't force an initial 4-way
   3307 		 * handshake and instead just keep the EAPoL state
   3308 		 * machine for the rekeys.
   3309 		 */
   3310 		if (netdev->in_ft || fils)
   3311 			eapol_sm_set_require_handshake(netdev->sm,
   3312 							false);
   3313 
   3314 		netdev->in_reassoc = false;
   3315 		netdev->associated = true;
   3316 		return;
   3317 	} else if (ret == -EAGAIN) {
   3318 		/*
   3319 		 * Here to support OWE retries. OWE will retry
   3320 		 * internally, but a connect event will still be emitted
   3321 		 */
   3322 		netdev->ignore_connect_event = true;
   3323 		return;
   3324 	} else if (ret > 0)
   3325 		status_code = (uint16_t)ret;
   3326 
   3327 assoc_failed:
   3328 	netdev->result = NETDEV_RESULT_ASSOCIATION_FAILED;
   3329 	netdev->last_code = status_code;
   3330 	netdev->expect_connect_failure = true;
   3331 }
   3332 
   3333 static struct l_genl_msg *netdev_build_cmd_authenticate(struct netdev *netdev,
   3334 							uint32_t auth_type)
   3335 {
   3336 	struct handshake_state *hs = netdev->handshake;
   3337 	struct l_genl_msg *msg;
   3338 
   3339 	msg = l_genl_msg_new_sized(NL80211_CMD_AUTHENTICATE, 512);
   3340 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   3341 	l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY_FREQ,
   3342 						4, &netdev->frequency);
   3343 	l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN,
   3344 				netdev->handshake->aa);
   3345 	l_genl_msg_append_attr(msg, NL80211_ATTR_SSID, hs->ssid_len, hs->ssid);
   3346 	l_genl_msg_append_attr(msg, NL80211_ATTR_AUTH_TYPE, 4, &auth_type);
   3347 
   3348 	return msg;
   3349 }
   3350 
   3351 static void netdev_scan_cb(struct l_genl_msg *msg, void *user_data)
   3352 {
   3353 	struct netdev *netdev = user_data;
   3354 
   3355 	if (l_genl_msg_get_error(msg) < 0) {
   3356 		netdev_connect_failed(netdev,
   3357 					NETDEV_RESULT_AUTHENTICATION_FAILED,
   3358 					MMPDU_STATUS_CODE_UNSPECIFIED);
   3359 		return;
   3360 	}
   3361 
   3362 	netdev->retry_auth = true;
   3363 }
   3364 
   3365 static void netdev_auth_cb(struct l_genl_msg *msg, void *user_data)
   3366 {
   3367 	struct netdev *netdev = user_data;
   3368 	struct handshake_state *hs = netdev->handshake;
   3369 	int err = l_genl_msg_get_error(msg);
   3370 	struct l_genl_msg *scan_msg;
   3371 
   3372 	if (!err) {
   3373 		l_genl_msg_unref(netdev->auth_cmd);
   3374 		netdev->auth_cmd = NULL;
   3375 		return;
   3376 	}
   3377 
   3378 	l_debug("Error during auth: %d", err);
   3379 
   3380 	if (!netdev->auth_cmd || err != -ENOENT)
   3381 		goto failed;
   3382 
   3383 	/* Kernel can't find the BSS in its cache, scan and retry */
   3384 	scan_msg = scan_build_trigger_scan_bss(netdev->index, netdev->wiphy,
   3385 						netdev->frequency,
   3386 						hs->ssid, hs->ssid_len);
   3387 
   3388 	if (l_genl_family_send(nl80211, scan_msg,
   3389 					netdev_scan_cb, netdev, NULL) > 0)
   3390 		return;
   3391 
   3392 	l_genl_msg_unref(scan_msg);
   3393 failed:
   3394 	netdev_connect_failed(netdev, NETDEV_RESULT_AUTHENTICATION_FAILED,
   3395 					MMPDU_STATUS_CODE_UNSPECIFIED);
   3396 }
   3397 
   3398 static void netdev_new_scan_results_event(struct l_genl_msg *msg,
   3399 							struct netdev *netdev)
   3400 {
   3401 	if (!netdev->retry_auth)
   3402 		return;
   3403 
   3404 	l_debug("");
   3405 
   3406 	if (!l_genl_family_send(nl80211, netdev->auth_cmd,
   3407 					netdev_auth_cb, netdev, NULL)) {
   3408 		netdev_connect_failed(netdev,
   3409 					NETDEV_RESULT_AUTHENTICATION_FAILED,
   3410 					MMPDU_STATUS_CODE_UNSPECIFIED);
   3411 		return;
   3412 	}
   3413 
   3414 	netdev->auth_cmd = NULL;
   3415 }
   3416 
   3417 static void netdev_assoc_cb(struct l_genl_msg *msg, void *user_data)
   3418 {
   3419 	struct netdev *netdev = user_data;
   3420 	int err = l_genl_msg_get_error(msg);
   3421 
   3422 	if (err < 0) {
   3423 		l_error("Error sending CMD_ASSOCIATE (%d)", err);
   3424 
   3425 		netdev_connect_failed(netdev, NETDEV_RESULT_ASSOCIATION_FAILED,
   3426 					MMPDU_STATUS_CODE_UNSPECIFIED);
   3427 	}
   3428 }
   3429 
   3430 static void netdev_sae_tx_authenticate(const uint8_t *body,
   3431 					size_t body_len, void *user_data)
   3432 {
   3433 	struct netdev *netdev = user_data;
   3434 	struct l_genl_msg *msg;
   3435 
   3436 	msg = netdev_build_cmd_authenticate(netdev, NL80211_AUTHTYPE_SAE);
   3437 	l_genl_msg_append_attr(msg, NL80211_ATTR_AUTH_DATA, body_len, body);
   3438 
   3439 	if (!l_genl_family_send(nl80211, msg, netdev_auth_cb, netdev, NULL)) {
   3440 		l_genl_msg_unref(msg);
   3441 		netdev_connect_failed(netdev,
   3442 					NETDEV_RESULT_AUTHENTICATION_FAILED,
   3443 					MMPDU_STATUS_CODE_UNSPECIFIED);
   3444 		return;
   3445 	}
   3446 
   3447 	/*
   3448 	 * Sometimes due to the way the scheduling works out, netdev_auth_cb
   3449 	 * is sent after the SAE Authentication reply from the AP arrives.
   3450 	 * Do not leak auth_cmd if this occurs.  Note that if auth_cmd is not
   3451 	 * NULL and we are here, there's no further reason to save off auth_cmd.
   3452 	 * This is done only if the kernel's cache lacks the BSS we are trying
   3453 	 * to communicate with.
   3454 	 */
   3455 	if (netdev->auth_cmd) {
   3456 		l_genl_msg_unref(netdev->auth_cmd);
   3457 		netdev->auth_cmd = NULL;
   3458 	} else
   3459 		netdev->auth_cmd = l_genl_msg_ref(msg);
   3460 }
   3461 
   3462 static void netdev_sae_tx_associate(void *user_data)
   3463 {
   3464 	struct netdev *netdev = user_data;
   3465 	struct handshake_state *hs = netdev->handshake;
   3466 	struct l_genl_msg *msg;
   3467 	struct iovec iov[64];
   3468 	unsigned int n_iov = L_ARRAY_SIZE(iov);
   3469 	unsigned int n_used = 0;
   3470 	enum mpdu_management_subtype subtype =
   3471 				MPDU_MANAGEMENT_SUBTYPE_ASSOCIATION_REQUEST;
   3472 
   3473 	msg = netdev_build_cmd_associate_common(netdev);
   3474 
   3475 	n_used = iov_ie_append(iov, n_iov, n_used, hs->supplicant_ie,
   3476 						IE_LEN(hs->supplicant_ie));
   3477 	n_used = iov_ie_append(iov, n_iov, n_used, hs->mde, IE_LEN(hs->mde));
   3478 	n_used = iov_ie_append(iov, n_iov, n_used, hs->supplicant_rsnxe,
   3479 					IE_LEN(hs->supplicant_rsnxe));
   3480 	n_used = netdev_populate_common_ies(netdev, hs, msg,
   3481 							iov, n_iov, n_used);
   3482 	mpdu_sort_ies(subtype, iov, n_used);
   3483 
   3484 	l_genl_msg_append_attrv(msg, NL80211_ATTR_IE, iov, n_used);
   3485 
   3486 	/* If doing a non-FT Reassociation */
   3487 	if (netdev->in_reassoc)
   3488 		l_genl_msg_append_attr(msg, NL80211_ATTR_PREV_BSSID, 6,
   3489 					netdev->ap->prev_bssid);
   3490 
   3491 	if (!l_genl_family_send(nl80211, msg, netdev_assoc_cb, netdev, NULL)) {
   3492 		l_genl_msg_unref(msg);
   3493 		netdev_connect_failed(netdev, NETDEV_RESULT_ASSOCIATION_FAILED,
   3494 					MMPDU_STATUS_CODE_UNSPECIFIED);
   3495 	}
   3496 }
   3497 
   3498 static void netdev_fils_tx_authenticate(const uint8_t *body,
   3499 					size_t body_len,
   3500 					void *user_data)
   3501 {
   3502 	struct netdev *netdev = user_data;
   3503 	struct l_genl_msg *msg;
   3504 
   3505 	msg = netdev_build_cmd_authenticate(netdev, NL80211_AUTHTYPE_FILS_SK);
   3506 
   3507 	l_genl_msg_append_attr(msg, NL80211_ATTR_AUTH_DATA, body_len, body);
   3508 
   3509 	if (!l_genl_family_send(nl80211, msg, netdev_auth_cb,
   3510 							netdev, NULL)) {
   3511 		l_genl_msg_unref(msg);
   3512 		netdev_connect_failed(netdev,
   3513 					NETDEV_RESULT_AUTHENTICATION_FAILED,
   3514 					MMPDU_STATUS_CODE_UNSPECIFIED);
   3515 		return;
   3516 	}
   3517 
   3518 	/* See comment in netdev_sae_tx_authenticate */
   3519 	if (netdev->auth_cmd) {
   3520 		l_genl_msg_unref(netdev->auth_cmd);
   3521 		netdev->auth_cmd = NULL;
   3522 	} else
   3523 		netdev->auth_cmd = l_genl_msg_ref(msg);
   3524 }
   3525 
   3526 static void netdev_fils_tx_associate(struct iovec *fils_iov, size_t n_fils_iov,
   3527 					const uint8_t *kek, size_t kek_len,
   3528 					const uint8_t *nonces,
   3529 					size_t nonces_len,
   3530 					void *user_data)
   3531 {
   3532 	struct netdev *netdev = user_data;
   3533 	struct handshake_state *hs = netdev->handshake;
   3534 	struct l_genl_msg *msg;
   3535 	struct iovec iov[64];
   3536 	unsigned int n_iov = L_ARRAY_SIZE(iov);
   3537 	unsigned int c_iov = 0;
   3538 	enum mpdu_management_subtype subtype =
   3539 				MPDU_MANAGEMENT_SUBTYPE_ASSOCIATION_REQUEST;
   3540 
   3541 	msg = netdev_build_cmd_associate_common(netdev);
   3542 	c_iov = netdev_populate_common_ies(netdev, hs, msg, iov, n_iov, c_iov);
   3543 
   3544 	if (!L_WARN_ON(n_iov - c_iov < n_fils_iov)) {
   3545 		memcpy(iov + c_iov, fils_iov, sizeof(*fils_iov) * n_fils_iov);
   3546 		c_iov += n_fils_iov;
   3547 	}
   3548 
   3549 	mpdu_sort_ies(subtype, iov, c_iov);
   3550 
   3551 	l_genl_msg_append_attrv(msg, NL80211_ATTR_IE, iov, c_iov);
   3552 
   3553 	l_genl_msg_append_attr(msg, NL80211_ATTR_FILS_KEK, kek_len, kek);
   3554 	l_genl_msg_append_attr(msg, NL80211_ATTR_FILS_NONCES,
   3555 							nonces_len, nonces);
   3556 
   3557 	/* If doing a non-FT Reassociation */
   3558 	if (netdev->in_reassoc)
   3559 		l_genl_msg_append_attr(msg, NL80211_ATTR_PREV_BSSID, 6,
   3560 					netdev->ap->prev_bssid);
   3561 
   3562 	if (!l_genl_family_send(nl80211, msg, netdev_assoc_cb,
   3563 							netdev, NULL)) {
   3564 		l_genl_msg_unref(msg);
   3565 		netdev_connect_failed(netdev, NETDEV_RESULT_ASSOCIATION_FAILED,
   3566 					MMPDU_STATUS_CODE_UNSPECIFIED);
   3567 	}
   3568 }
   3569 
   3570 static void netdev_external_auth_frame_cb(struct l_genl_msg *msg,
   3571 							void *user_data)
   3572 {
   3573 	int error = l_genl_msg_get_error(msg);
   3574 
   3575 	if (error < 0)
   3576 		l_debug("Failed to send External Auth Frame: %s(%d)",
   3577 				strerror(-error), -error);
   3578 }
   3579 
   3580 static void netdev_external_auth_sae_tx_authenticate(const uint8_t *body,
   3581 					size_t body_len, void *user_data)
   3582 {
   3583 	struct netdev *netdev = user_data;
   3584 	struct handshake_state *hs = netdev->handshake;
   3585 	uint16_t frame_type = MPDU_MANAGEMENT_SUBTYPE_AUTHENTICATION << 4;
   3586 	struct iovec iov[2];
   3587 	struct l_genl_msg *msg;
   3588 	uint8_t algorithm[2] = { 0x03, 0x00 };
   3589 
   3590 	l_debug("");
   3591 
   3592 	iov[0].iov_base = &algorithm;
   3593 	iov[0].iov_len = sizeof(algorithm);
   3594 	iov[1].iov_base = (void *) body;
   3595 	iov[1].iov_len = body_len;
   3596 
   3597 	msg = nl80211_build_cmd_frame(netdev->index, frame_type,
   3598 					hs->spa, hs->aa, 0, iov, 2);
   3599 
   3600 	if (l_genl_family_send(nl80211, msg, netdev_external_auth_frame_cb,
   3601 				netdev, NULL) > 0)
   3602 		return;
   3603 
   3604 	l_genl_msg_unref(msg);
   3605 }
   3606 
   3607 static void netdev_external_auth_cb(struct l_genl_msg *msg, void *user_data)
   3608 {
   3609 	int error = l_genl_msg_get_error(msg);
   3610 
   3611 	if (error < 0)
   3612 		l_debug("Failed to send External Auth: %s(%d)",
   3613 				strerror(-error), -error);
   3614 }
   3615 
   3616 static void netdev_send_external_auth(struct netdev *netdev,
   3617 							uint16_t status_code)
   3618 {
   3619 	struct handshake_state *hs = netdev->handshake;
   3620 	struct l_genl_msg *msg =
   3621 		nl80211_build_external_auth(netdev->index, status_code,
   3622 						hs->ssid, hs->ssid_len, hs->aa);
   3623 
   3624 	if (l_genl_family_send(nl80211, msg, netdev_external_auth_cb,
   3625 				netdev, NULL) > 0)
   3626 		return;
   3627 
   3628 	l_genl_msg_unref(msg);
   3629 }
   3630 
   3631 static void netdev_external_auth_sae_tx_associate(void *user_data)
   3632 {
   3633 	struct netdev *netdev = user_data;
   3634 
   3635 	l_debug("");
   3636 
   3637 	netdev_send_external_auth(netdev, MMPDU_STATUS_CODE_SUCCESS);
   3638 	netdev_ensure_eapol_registered(netdev);
   3639 
   3640 	/*
   3641 	 * Free the auth proto now. With external auth there is no associate
   3642 	 * event which is where this normally gets cleaned up.
   3643 	 */
   3644 	auth_proto_free(netdev->ap);
   3645 	netdev->ap = NULL;
   3646 }
   3647 
   3648 struct rtnl_data {
   3649 	struct netdev *netdev;
   3650 	uint8_t addr[ETH_ALEN];
   3651 	int ref;
   3652 };
   3653 
   3654 static int netdev_begin_connection(struct netdev *netdev)
   3655 {
   3656 	struct netdev_handshake_state *nhs =
   3657 		l_container_of(netdev->handshake,
   3658 				struct netdev_handshake_state, super);
   3659 
   3660 	if (netdev->connect_cmd) {
   3661 		netdev->connect_cmd_id = l_genl_family_send(nl80211,
   3662 						netdev->connect_cmd,
   3663 						netdev_cmd_connect_cb, netdev,
   3664 						NULL);
   3665 
   3666 		if (!netdev->connect_cmd_id)
   3667 			goto failed;
   3668 
   3669 		netdev->connect_cmd = NULL;
   3670 	}
   3671 
   3672 	/*
   3673 	 * Set the supplicant address now, this may have already been done for
   3674 	 * a non-randomized address connect, but if we are randomizing we need
   3675 	 * to set it again as the address should have now changed.
   3676 	 */
   3677 	handshake_state_set_supplicant_address(netdev->handshake, netdev->addr);
   3678 
   3679 	if (netdev->ap && nhs->type == CONNECTION_TYPE_SOFTMAC) {
   3680 		if (!auth_proto_start(netdev->ap))
   3681 			goto failed;
   3682 
   3683 		/*
   3684 		 * set connected since the auth protocols cannot do
   3685 		 * so internally
   3686 		 */
   3687 		netdev->connected = true;
   3688 	}
   3689 
   3690 	return 0;
   3691 
   3692 failed:
   3693 	netdev_connect_failed(netdev, NETDEV_RESULT_ABORTED,
   3694 					MMPDU_STATUS_CODE_UNSPECIFIED);
   3695 	return -EIO;
   3696 }
   3697 
   3698 static void netdev_mac_change_failed(struct netdev *netdev, int error)
   3699 {
   3700 	l_error("Error setting mac address on %d: %s", netdev->index,
   3701 			strerror(-error));
   3702 
   3703 	/*
   3704 	 * If the interface is down and we failed to up it we need to notify
   3705 	 * any watchers since we have been skipping the notification while
   3706 	 * mac_change_cmd_id was set.
   3707 	 */
   3708 	if (!netdev_get_is_up(netdev)) {
   3709 		WATCHLIST_NOTIFY(&netdev_watches, netdev_watch_func_t,
   3710 				netdev, NETDEV_WATCH_EVENT_DOWN);
   3711 
   3712 		netdev_connect_failed(netdev, NETDEV_RESULT_ABORTED,
   3713 				MMPDU_STATUS_CODE_UNSPECIFIED);
   3714 		return;
   3715 	}
   3716 
   3717 	/* If the interface is up we can still try and connect */
   3718 	l_info("Failed to change the MAC, continuing with connection");
   3719 
   3720 	if (netdev_begin_connection(netdev) < 0)
   3721 		l_error("netdev_begin_connection() error in mac_change_failed");
   3722 }
   3723 
   3724 static void netdev_mac_destroy(void *user_data)
   3725 {
   3726 	struct rtnl_data *req = user_data;
   3727 
   3728 	req->ref--;
   3729 
   3730 	/* still pending requests? */
   3731 	if (req->ref)
   3732 		return;
   3733 
   3734 	l_free(req);
   3735 }
   3736 
   3737 static void netdev_mac_power_up_cb(int error, uint16_t type,
   3738 					const void *data, uint32_t len,
   3739 					void *user_data)
   3740 {
   3741 	struct rtnl_data *req = user_data;
   3742 	struct netdev *netdev = req->netdev;
   3743 
   3744 	netdev->mac_change_cmd_id = 0;
   3745 
   3746 	if (error) {
   3747 		l_error("Error changing per-network MAC on interface %u: %s",
   3748 			netdev->index, strerror(-error));
   3749 		netdev_mac_change_failed(netdev, error);
   3750 		return;
   3751 	}
   3752 
   3753 	/* Pick up where we left off in netdev_connect_commmon */
   3754 	if (netdev_begin_connection(netdev) < 0)
   3755 		l_error("netdev_begin_connection() error in mac_power_up_cb");
   3756 }
   3757 
   3758 static void netdev_mac_power_down_cb(int error, uint16_t type,
   3759 					const void *data, uint32_t len,
   3760 					void *user_data)
   3761 {
   3762 	struct rtnl_data *req = user_data;
   3763 	struct netdev *netdev = req->netdev;
   3764 
   3765 	netdev->mac_change_cmd_id = 0;
   3766 
   3767 	if (error) {
   3768 		l_error("Error taking interface %u down for per-network MAC "
   3769 			"generation: %s", netdev->index, strerror(-error));
   3770 		netdev_mac_change_failed(netdev, error);
   3771 		return;
   3772 	}
   3773 
   3774 	netdev->mac_change_cmd_id = l_rtnl_set_mac(rtnl, netdev->index,
   3775 					req->addr, true,
   3776 					netdev_mac_power_up_cb, req,
   3777 					netdev_mac_destroy);
   3778 	if (!netdev->mac_change_cmd_id) {
   3779 		netdev_mac_change_failed(netdev, -EIO);
   3780 		return;
   3781 	}
   3782 
   3783 	req->ref++;
   3784 }
   3785 
   3786 /*
   3787  * TODO: There are some potential race conditions that are being ignored. There
   3788  *       is nothing that IWD itself can do to solve these, they require kernel
   3789  *       changes:
   3790  *
   3791  * 1. A perfectly timed ifdown could be ignored. If an external process
   3792  *    brings down an interface just before calling this function we would only
   3793  *    get a single newlink event since there is no state change doing a second
   3794  *    ifdown (nor an error from the kernel). This newlink event would be ignored
   3795  *    since IWD thinks its from our own doing. This would result in IWD changing
   3796  *    the MAC and bringing the interface back up which would look very strange
   3797  *    and unexpected to someone who just tried to ifdown an interface.
   3798  *
   3799  * 2. A perfectly timed ifup could result in a failed connection. If an external
   3800  *    process ifup's just after IWD ifdown's but before changing the MAC this
   3801  *    would cause the MAC change to fail. This failure would result in a failed
   3802  *    connection.
   3803  *
   3804  * Returns 0 if a MAC change procedure was started.
   3805  * Returns -EALREADY if the requested MAC matched our current MAC
   3806  * Returns -EIO if there was an IO error when powering down
   3807  */
   3808 static int netdev_start_powered_mac_change(struct netdev *netdev)
   3809 {
   3810 	struct rtnl_data *req;
   3811 	uint8_t new_addr[6];
   3812 	bool powered = wiphy_has_ext_feature(netdev->wiphy,
   3813 				NL80211_EXT_FEATURE_POWERED_ADDR_CHANGE);
   3814 
   3815 	/* No address set in handshake, use per-network MAC generation */
   3816 	if (l_memeqzero(netdev->handshake->spa, ETH_ALEN))
   3817 		wiphy_generate_address_from_ssid(netdev->wiphy,
   3818 					netdev->handshake->ssid,
   3819 					netdev->handshake->ssid_len,
   3820 					new_addr);
   3821 	else
   3822 		memcpy(new_addr, netdev->handshake->spa, ETH_ALEN);
   3823 
   3824 	/*
   3825 	 * MAC has already been changed previously, no need to again
   3826 	 */
   3827 	if (!memcmp(new_addr, netdev->addr, sizeof(new_addr)))
   3828 		return -EALREADY;
   3829 
   3830 	req = l_new(struct rtnl_data, 1);
   3831 	req->netdev = netdev;
   3832 	/* This message will need to be unreffed upon any error */
   3833 	req->ref++;
   3834 	memcpy(req->addr, new_addr, sizeof(req->addr));
   3835 
   3836 	if (powered)
   3837 		netdev->mac_change_cmd_id = l_rtnl_set_mac(rtnl, netdev->index,
   3838 						req->addr, false,
   3839 						netdev_mac_power_up_cb, req,
   3840 						netdev_mac_destroy);
   3841 	else
   3842 		netdev->mac_change_cmd_id = l_rtnl_set_powered(rtnl,
   3843 						netdev->index, false,
   3844 						netdev_mac_power_down_cb, req,
   3845 						netdev_mac_destroy);
   3846 
   3847 	if (!netdev->mac_change_cmd_id) {
   3848 		l_free(req);
   3849 
   3850 		return -EIO;
   3851 	}
   3852 
   3853 	l_debug("Setting generated address on ifindex: %d to: "MAC" (%s)",
   3854 					netdev->index, MAC_STR(req->addr),
   3855 					powered ? "powered" : "power-down");
   3856 
   3857 	return 0;
   3858 }
   3859 
   3860 static struct l_genl_msg *netdev_build_cmd_cqm_rssi_update(
   3861 							struct netdev *netdev,
   3862 							const int8_t *levels,
   3863 							size_t levels_num)
   3864 {
   3865 	struct l_genl_msg *msg;
   3866 	uint32_t hyst = 5;
   3867 	int thold_count;
   3868 	int32_t thold_list[levels_num + 2];
   3869 	int threshold = netdev->frequency > 4000 ?
   3870 					netdev->low_signal_threshold_5ghz :
   3871 					netdev->low_signal_threshold;
   3872 
   3873 	if (levels_num == 0) {
   3874 		thold_list[0] = threshold;
   3875 		thold_count = 1;
   3876 	} else {
   3877 		/*
   3878 		 * Build the list of all the threshold values we care about:
   3879 		 *  - the low/high level threshold,
   3880 		 *  - the value ranges requested by
   3881 		 *    netdev_set_rssi_report_levels
   3882 		 */
   3883 		unsigned int i;
   3884 		bool low_sig_added = false;
   3885 
   3886 		thold_count = 0;
   3887 		for (i = 0; i < levels_num; i++) {
   3888 			int32_t val = levels[levels_num - i - 1];
   3889 
   3890 			if (i && thold_list[thold_count - 1] >= val)
   3891 				return NULL;
   3892 
   3893 			if (val >= threshold && !low_sig_added) {
   3894 				thold_list[thold_count++] = threshold;
   3895 				low_sig_added = true;
   3896 
   3897 				/* Duplicate values are not allowed */
   3898 				if (val == threshold)
   3899 					continue;
   3900 			}
   3901 
   3902 			thold_list[thold_count++] = val;
   3903 		}
   3904 	}
   3905 
   3906 	msg = l_genl_msg_new_sized(NL80211_CMD_SET_CQM, 32 + thold_count * 4);
   3907 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   3908 	l_genl_msg_enter_nested(msg, NL80211_ATTR_CQM);
   3909 	l_genl_msg_append_attr(msg, NL80211_ATTR_CQM_RSSI_THOLD,
   3910 				thold_count * 4, thold_list);
   3911 	l_genl_msg_append_attr(msg, NL80211_ATTR_CQM_RSSI_HYST, 4, &hyst);
   3912 	l_genl_msg_leave_nested(msg);
   3913 
   3914 	return msg;
   3915 }
   3916 
   3917 static void netdev_cmd_set_cqm_cb(struct l_genl_msg *msg, void *user_data)
   3918 {
   3919 	struct netdev *netdev = user_data;
   3920 	int err = l_genl_msg_get_error(msg);
   3921 	const char *ext_error;
   3922 
   3923 	netdev->set_cqm_cmd_id = 0;
   3924 
   3925 	if (err >= 0) {
   3926 		/*
   3927 		 * Looking at some driver code it appears that the -ENOTSUP CQM
   3928 		 * failure could be transient. Just in case, reset the fallback
   3929 		 * flag if CQM happens to start working again.
   3930 		 */
   3931 		if (netdev->cqm_poll_fallback) {
   3932 			l_debug("CMD_SET_CQM succeeded, stop polling fallback");
   3933 
   3934 			if (netdev->rssi_poll_timeout) {
   3935 				l_timeout_remove(netdev->rssi_poll_timeout);
   3936 				netdev->rssi_poll_timeout = NULL;
   3937 			}
   3938 
   3939 			netdev->cqm_poll_fallback = false;
   3940 		}
   3941 
   3942 		return;
   3943 	}
   3944 
   3945 	/*
   3946 	 * Some drivers enable beacon filtering but also use software CQM which
   3947 	 * mac80211 detects and returns -ENOTSUP. There is no way to check this
   3948 	 * ahead of time so if we see this start polling in order to get RSSI
   3949 	 * updates.
   3950 	 */
   3951 	if (err == -ENOTSUP) {
   3952 		l_debug("CMD_SET_CQM not supported, falling back to polling");
   3953 		netdev->cqm_poll_fallback = true;
   3954 
   3955 		if (netdev->rssi_poll_timeout)
   3956 			return;
   3957 
   3958 		netdev->rssi_poll_timeout = l_timeout_create(1,
   3959 						netdev_rssi_poll, netdev, NULL);
   3960 
   3961 		return;
   3962 	}
   3963 
   3964 	ext_error = l_genl_msg_get_extended_error(msg);
   3965 	l_error("CMD_SET_CQM failed: %s",
   3966 			ext_error ? ext_error : strerror(-err));
   3967 }
   3968 
   3969 static int netdev_cqm_rssi_update(struct netdev *netdev)
   3970 {
   3971 	struct l_genl_msg *msg;
   3972 	struct netdev_handshake_state *nhs = l_container_of(netdev->handshake,
   3973 				struct netdev_handshake_state, super);
   3974 
   3975 	/*
   3976 	 * Fullmac cards handle roaming in firmware, there is no need to set
   3977 	 * CQM thresholds
   3978 	 */
   3979 	if (nhs->type == CONNECTION_TYPE_FULLMAC)
   3980 		return 0;
   3981 
   3982 	l_debug("");
   3983 
   3984 	if (netdev->set_cqm_cmd_id)
   3985 		return -EBUSY;
   3986 
   3987 	if (!wiphy_has_ext_feature(netdev->wiphy,
   3988 					NL80211_EXT_FEATURE_CQM_RSSI_LIST))
   3989 		msg = netdev_build_cmd_cqm_rssi_update(netdev, NULL, 0);
   3990 	else
   3991 		msg = netdev_build_cmd_cqm_rssi_update(netdev,
   3992 						netdev->rssi_levels,
   3993 						netdev->rssi_levels_num);
   3994 	if (!msg)
   3995 		return -EINVAL;
   3996 
   3997 	netdev->set_cqm_cmd_id = l_genl_family_send(nl80211, msg,
   3998 						netdev_cmd_set_cqm_cb,
   3999 						netdev, NULL);
   4000 	if (!netdev->set_cqm_cmd_id) {
   4001 		l_genl_msg_unref(msg);
   4002 		return -EIO;
   4003 	}
   4004 
   4005 	return 0;
   4006 }
   4007 
   4008 static int netdev_set_signal_thresholds(struct netdev *netdev, int threshold,
   4009 					int threshold_5ghz)
   4010 {
   4011 	int current = netdev->frequency > 4000 ?
   4012 					netdev->low_signal_threshold_5ghz :
   4013 					netdev->low_signal_threshold;
   4014 	int new = netdev->frequency > 4000 ? threshold_5ghz : threshold;
   4015 
   4016 	if (current == new)
   4017 		return -EALREADY;
   4018 
   4019 	l_debug("changing low signal threshold to %d", new);
   4020 
   4021 	netdev->low_signal_threshold = threshold;
   4022 	netdev->low_signal_threshold_5ghz = threshold_5ghz;
   4023 
   4024 	return netdev_cqm_rssi_update(netdev);
   4025 }
   4026 
   4027 int netdev_lower_signal_threshold(struct netdev *netdev)
   4028 {
   4029 	return netdev_set_signal_thresholds(netdev, CRITICAL_SIGNAL_THRESHOLD,
   4030 						CRITICAL_SIGNAL_THRESHOLD_5GHZ);
   4031 }
   4032 
   4033 int netdev_raise_signal_threshold(struct netdev *netdev)
   4034 {
   4035 	return netdev_set_signal_thresholds(netdev, LOW_SIGNAL_THRESHOLD,
   4036 						LOW_SIGNAL_THRESHOLD_5GHZ);
   4037 }
   4038 
   4039 static bool netdev_connection_work_ready(struct wiphy_radio_work_item *item)
   4040 {
   4041 	struct netdev *netdev = l_container_of(item, struct netdev, work);
   4042 
   4043 	netdev->retry_auth = false;
   4044 
   4045 	if (mac_per_ssid) {
   4046 		int ret = netdev_start_powered_mac_change(netdev);
   4047 
   4048 		if (!ret)
   4049 			return false;
   4050 		else if (ret != -EALREADY)
   4051 			goto failed;
   4052 	}
   4053 
   4054 	if (netdev_begin_connection(netdev) < 0)
   4055 		return true;
   4056 
   4057 	return false;
   4058 
   4059 failed:
   4060 	netdev_connect_failed(netdev, NETDEV_RESULT_ABORTED,
   4061 				MMPDU_STATUS_CODE_UNSPECIFIED);
   4062 
   4063 	return true;
   4064 }
   4065 
   4066 static void netdev_connection_work_destroy(struct wiphy_radio_work_item *item)
   4067 {
   4068 	struct netdev *netdev = l_container_of(item, struct netdev, work);
   4069 
   4070 	if (netdev->auth_cmd) {
   4071 		l_genl_msg_unref(netdev->auth_cmd);
   4072 		netdev->auth_cmd = NULL;
   4073 	}
   4074 
   4075 	netdev->retry_auth = false;
   4076 }
   4077 
   4078 static const struct wiphy_radio_work_item_ops connect_work_ops = {
   4079 	.do_work = netdev_connection_work_ready,
   4080 	.destroy = netdev_connection_work_destroy,
   4081 };
   4082 
   4083 static int netdev_handshake_state_setup_connection_type(
   4084 						struct handshake_state *hs)
   4085 {
   4086 	struct netdev_handshake_state *nhs = l_container_of(hs,
   4087 				struct netdev_handshake_state, super);
   4088 	struct wiphy *wiphy = nhs->netdev->wiphy;
   4089 	bool softmac = wiphy_supports_cmds_auth_assoc(wiphy);
   4090 	bool canroam = wiphy_supports_firmware_roam(wiphy);
   4091 
   4092 	if (hs->supplicant_ie == NULL)
   4093 		goto softmac;
   4094 
   4095 	/*
   4096 	 * Sanity check that any FT AKMs are set only on softmac or on
   4097 	 * devices that support firmware roam
   4098 	 */
   4099 	if (L_WARN_ON(IE_AKM_IS_FT(hs->akm_suite) && !softmac && !canroam))
   4100 		return -ENOTSUP;
   4101 
   4102 	switch (hs->akm_suite) {
   4103 	case IE_RSN_AKM_SUITE_PSK:
   4104 	case IE_RSN_AKM_SUITE_FT_USING_PSK:
   4105 	case IE_RSN_AKM_SUITE_PSK_SHA256:
   4106 		if (wiphy_has_ext_feature(wiphy,
   4107 				NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK))
   4108 			goto psk_offload;
   4109 
   4110 	/* fall through */
   4111 	case IE_RSN_AKM_SUITE_OWE:
   4112 		if (softmac)
   4113 			goto softmac;
   4114 
   4115 		goto fullmac;
   4116 	case IE_RSN_AKM_SUITE_8021X:
   4117 	case IE_RSN_AKM_SUITE_FT_OVER_8021X:
   4118 	case IE_RSN_AKM_SUITE_8021X_SHA256:
   4119 	case IE_RSN_AKM_SUITE_8021X_SUITE_B_SHA256:
   4120 	case IE_RSN_AKM_SUITE_8021X_SUITE_B_SHA384:
   4121 	case IE_RSN_AKM_SUITE_FT_OVER_8021X_SHA384:
   4122 		if (wiphy_has_ext_feature(wiphy,
   4123 				NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
   4124 			goto offload_1x;
   4125 
   4126 		if (softmac)
   4127 			goto softmac;
   4128 
   4129 		goto fullmac;
   4130 	case IE_RSN_AKM_SUITE_SAE_SHA256:
   4131 	case IE_RSN_AKM_SUITE_FT_OVER_SAE_SHA256:
   4132 		if (wiphy_has_ext_feature(wiphy,
   4133 					NL80211_EXT_FEATURE_SAE_OFFLOAD))
   4134 			goto sae_offload;
   4135 
   4136 		if (softmac && wiphy_has_feature(wiphy, NL80211_FEATURE_SAE))
   4137 			goto softmac;
   4138 
   4139 		/* FullMAC uses EXTERNAL_AUTH and reuses this feature bit */
   4140 		if (wiphy_has_feature(wiphy, NL80211_FEATURE_SAE))
   4141 			goto fullmac;
   4142 
   4143 		return -ENOTSUP;
   4144 	case IE_RSN_AKM_SUITE_FILS_SHA256:
   4145 	case IE_RSN_AKM_SUITE_FILS_SHA384:
   4146 	case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256:
   4147 	case IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384:
   4148 		/* FILS has no offload in any upstream driver */
   4149 		if (softmac && wiphy_has_ext_feature(wiphy,
   4150 						NL80211_EXT_FEATURE_FILS_STA))
   4151 			goto softmac;
   4152 
   4153 		return -ENOTSUP;
   4154 	case IE_RSN_AKM_SUITE_TDLS:
   4155 	case IE_RSN_AKM_SUITE_AP_PEER_KEY_SHA256:
   4156 	case IE_RSN_AKM_SUITE_OSEN:
   4157 		return -ENOTSUP;
   4158 	}
   4159 
   4160 	return -ENOTSUP;
   4161 
   4162 softmac:
   4163 	nhs->type = CONNECTION_TYPE_SOFTMAC;
   4164 	return 0;
   4165 fullmac:
   4166 	nhs->type = CONNECTION_TYPE_FULLMAC;
   4167 	return 0;
   4168 sae_offload:
   4169 	nhs->type = CONNECTION_TYPE_SAE_OFFLOAD;
   4170 	return 0;
   4171 psk_offload:
   4172 	nhs->type = CONNECTION_TYPE_PSK_OFFLOAD;
   4173 	return 0;
   4174 offload_1x:
   4175 	nhs->type = CONNECTION_TYPE_8021X_OFFLOAD;
   4176 	return 0;
   4177 }
   4178 
   4179 static void netdev_connect_common(struct netdev *netdev,
   4180 					const struct scan_bss *bss,
   4181 					const struct scan_bss *prev_bss,
   4182 					struct handshake_state *hs,
   4183 					netdev_event_func_t event_filter,
   4184 					netdev_connect_cb_t cb, void *user_data)
   4185 {
   4186 	struct netdev_handshake_state *nhs = l_container_of(hs,
   4187 				struct netdev_handshake_state, super);
   4188 	struct l_genl_msg *cmd_connect = NULL;
   4189 	struct eapol_sm *sm = NULL;
   4190 	bool is_rsn = hs->supplicant_ie != NULL;
   4191 	const uint8_t *prev_bssid = prev_bss ? prev_bss->addr : NULL;
   4192 
   4193 	netdev->frequency = bss->frequency;
   4194 	netdev->privacy = bss->capability & IE_BSS_CAP_PRIVACY;
   4195 	handshake_state_set_authenticator_address(hs, bss->addr);
   4196 
   4197 	if (!is_rsn)
   4198 		goto build_cmd_connect;
   4199 
   4200 	/* For OWE, always use the CMD_CONNECT path */
   4201 	if (IE_AKM_IS_OWE(hs->akm_suite)) {
   4202 		netdev->owe_sm = owe_sm_new(hs);
   4203 		goto build_cmd_connect;
   4204 	}
   4205 
   4206 	if (IE_AKM_IS_FILS(hs->akm_suite)) {
   4207 		netdev->ap = fils_sm_new(hs, netdev_fils_tx_authenticate,
   4208 						netdev_fils_tx_associate,
   4209 						netdev_get_oci,
   4210 						netdev);
   4211 		goto done;
   4212 	}
   4213 
   4214 	/*
   4215 	 * If SAE, and we have a valid PMKSA cache we can skip the entire SAE
   4216 	 * protocol and authenticate using the cached keys.
   4217 	 */
   4218 	if (IE_AKM_IS_SAE(hs->akm_suite) && hs->have_pmksa) {
   4219 		l_debug("Skipping SAE by using PMKSA cache");
   4220 		goto build_cmd_connect;
   4221 	}
   4222 
   4223 	if (!IE_AKM_IS_SAE(hs->akm_suite) ||
   4224 				nhs->type == CONNECTION_TYPE_SAE_OFFLOAD)
   4225 		goto build_cmd_connect;
   4226 
   4227 	if (nhs->type == CONNECTION_TYPE_SOFTMAC)
   4228 		netdev->ap = sae_sm_new(hs, netdev_sae_tx_authenticate,
   4229 					netdev_sae_tx_associate,
   4230 					netdev);
   4231 	else {
   4232 		netdev->ap =
   4233 			sae_sm_new(hs, netdev_external_auth_sae_tx_authenticate,
   4234 					netdev_external_auth_sae_tx_associate,
   4235 					netdev);
   4236 		sae_sm_force_default_group(netdev->ap);
   4237 		sae_sm_force_hunt_and_peck(netdev->ap);
   4238 	}
   4239 
   4240 	if (sae_sm_is_h2e(netdev->ap)) {
   4241 		uint8_t own_rsnxe[20];
   4242 
   4243 		if (wiphy_get_rsnxe(netdev->wiphy,
   4244 				own_rsnxe, sizeof(own_rsnxe))) {
   4245 			set_bit(own_rsnxe + 2, IE_RSNX_SAE_H2E);
   4246 			handshake_state_set_supplicant_rsnxe(hs,
   4247 							own_rsnxe);
   4248 		}
   4249 	}
   4250 
   4251 	if (nhs->type == CONNECTION_TYPE_SOFTMAC)
   4252 		goto done;
   4253 
   4254 build_cmd_connect:
   4255 	cmd_connect = netdev_build_cmd_connect(netdev, hs, prev_bssid);
   4256 
   4257 	if (!is_offload(hs) && (is_rsn || hs->settings_8021x)) {
   4258 		sm = eapol_sm_new(hs);
   4259 
   4260 		if (nhs->type == CONNECTION_TYPE_8021X_OFFLOAD)
   4261 			eapol_sm_set_require_handshake(sm, false);
   4262 	}
   4263 done:
   4264 	netdev->connect_cmd = cmd_connect;
   4265 	netdev->event_filter = event_filter;
   4266 	netdev->connect_cb = cb;
   4267 	netdev->user_data = user_data;
   4268 	netdev->handshake = hs;
   4269 	netdev->sm = sm;
   4270 	netdev->cur_rssi = bss->signal_strength / 100;
   4271 	netdev->low_signal_threshold = LOW_SIGNAL_THRESHOLD;
   4272 	netdev->low_signal_threshold_5ghz = LOW_SIGNAL_THRESHOLD_5GHZ;
   4273 
   4274 	if (netdev->rssi_levels_num)
   4275 		netdev_set_rssi_level_idx(netdev);
   4276 
   4277 	netdev_cqm_rssi_update(netdev);
   4278 
   4279 	if (!wiphy_has_ext_feature(netdev->wiphy,
   4280 					NL80211_EXT_FEATURE_CAN_REPLACE_PTK0))
   4281 		handshake_state_set_no_rekey(hs, true);
   4282 
   4283 	wiphy_radio_work_insert(netdev->wiphy, &netdev->work,
   4284 				WIPHY_WORK_PRIORITY_CONNECT, &connect_work_ops);
   4285 }
   4286 
   4287 int netdev_connect(struct netdev *netdev, const struct scan_bss *bss,
   4288 				struct handshake_state *hs,
   4289 				netdev_event_func_t event_filter,
   4290 				netdev_connect_cb_t cb, void *user_data)
   4291 {
   4292 	if (!(netdev->ifi_flags & IFF_UP))
   4293 		return -ENETDOWN;
   4294 
   4295 	if (netdev->type != NL80211_IFTYPE_STATION &&
   4296 			netdev->type != NL80211_IFTYPE_P2P_CLIENT)
   4297 		return -ENOTSUP;
   4298 
   4299 	if (netdev->connected || netdev->connect_cmd_id || netdev->work.id)
   4300 		return -EISCONN;
   4301 
   4302 	if (netdev_handshake_state_setup_connection_type(hs) < 0)
   4303 		return -ENOTSUP;
   4304 
   4305 	netdev_connect_common(netdev, bss, NULL, hs,
   4306 				event_filter, cb, user_data);
   4307 
   4308 	return 0;
   4309 }
   4310 
   4311 static void disconnect_idle(struct l_idle *idle, void *user_data)
   4312 {
   4313 	struct netdev *netdev = user_data;
   4314 
   4315 	l_idle_remove(idle);
   4316 	netdev->disconnect_idle = NULL;
   4317 
   4318 	netdev->disconnect_cb(netdev, true, netdev->user_data);
   4319 }
   4320 
   4321 int netdev_disconnect(struct netdev *netdev,
   4322 				netdev_disconnect_cb_t cb, void *user_data)
   4323 {
   4324 	struct l_genl_msg *disconnect;
   4325 	bool send_disconnect = true;
   4326 	bool deauth = false;
   4327 	uint8_t aa[6];
   4328 
   4329 	if (!(netdev->ifi_flags & IFF_UP))
   4330 		return -ENETDOWN;
   4331 
   4332 	if (netdev->type != NL80211_IFTYPE_STATION &&
   4333 			netdev->type != NL80211_IFTYPE_P2P_CLIENT)
   4334 		return -ENOTSUP;
   4335 
   4336 	if (netdev->disconnect_cmd_id)
   4337 		return -EINPROGRESS;
   4338 
   4339 	/* Only perform this if we haven't successfully fully associated yet */
   4340 	if (!netdev->operational) {
   4341 		/*
   4342 		 * Three possibilities here:
   4343 		 * 1. We do not actually have a connect in progress (work.id
   4344 		 *    is zero), then we can bail out early with an error.
   4345 		 * 2. We have sent CMD_CONNECT but not fully connected. The
   4346 		 *    CMD_CONNECT needs to be canceled and a disconnect or
   4347 		 *    deauth should be sent.
   4348 		 * 3. Queued up the connect work, but haven't sent CMD_CONNECT
   4349 		 *    to the kernel. This case we do not need to send a
   4350 		 *    disconnect.
   4351 		 */
   4352 		if (!netdev->work.id)
   4353 			return -ENOTCONN;
   4354 
   4355 		if (netdev->connect_cmd_id) {
   4356 			l_genl_family_cancel(nl80211, netdev->connect_cmd_id);
   4357 			netdev->connect_cmd_id = 0;
   4358 		} else if (!wiphy_radio_work_is_running(netdev->wiphy,
   4359 							netdev->work.id))
   4360 			send_disconnect = false;
   4361 
   4362 		if (netdev->handshake && !netdev->associated) {
   4363 			memcpy(aa, netdev->handshake->aa, 6);
   4364 			deauth = true;
   4365 		}
   4366 
   4367 		netdev_connect_failed(netdev, NETDEV_RESULT_ABORTED,
   4368 					MMPDU_REASON_CODE_UNSPECIFIED);
   4369 	} else {
   4370 		netdev_connect_free(netdev);
   4371 	}
   4372 
   4373 	if (send_disconnect) {
   4374 		if (deauth)
   4375 			disconnect = nl80211_build_deauthenticate(
   4376 					netdev->index, aa,
   4377 					MMPDU_REASON_CODE_DEAUTH_LEAVING);
   4378 		else
   4379 			disconnect = nl80211_build_disconnect(netdev->index,
   4380 					MMPDU_REASON_CODE_DEAUTH_LEAVING);
   4381 
   4382 		netdev->disconnect_cmd_id = l_genl_family_send(nl80211,
   4383 					disconnect, netdev_cmd_disconnect_cb,
   4384 					netdev, NULL);
   4385 
   4386 		if (!netdev->disconnect_cmd_id) {
   4387 			l_genl_msg_unref(disconnect);
   4388 			return -EIO;
   4389 		}
   4390 
   4391 		netdev->disconnect_cb = cb;
   4392 		netdev->user_data = user_data;
   4393 		netdev->aborting = true;
   4394 	} else if (cb) {
   4395 		netdev->disconnect_cb = cb;
   4396 		netdev->user_data = user_data;
   4397 		netdev->disconnect_idle = l_idle_create(disconnect_idle,
   4398 							netdev, NULL);
   4399 	}
   4400 
   4401 	return 0;
   4402 }
   4403 
   4404 int netdev_reassociate(struct netdev *netdev, const struct scan_bss *target_bss,
   4405 			const struct scan_bss *orig_bss,
   4406 			struct handshake_state *hs,
   4407 			netdev_event_func_t event_filter,
   4408 			netdev_connect_cb_t cb, void *user_data)
   4409 {
   4410 	struct handshake_state *old_hs;
   4411 	struct eapol_sm *old_sm;
   4412 
   4413 	old_sm = netdev->sm;
   4414 	old_hs = netdev->handshake;
   4415 
   4416 	if (netdev_handshake_state_setup_connection_type(hs) < 0)
   4417 		return -ENOTSUP;
   4418 
   4419 	netdev->associated = false;
   4420 	netdev->operational = false;
   4421 	netdev->connected = false;
   4422 	netdev->in_reassoc = true;
   4423 
   4424 	netdev_connect_common(netdev, target_bss, orig_bss, hs,
   4425 					event_filter, cb, user_data);
   4426 
   4427 	if (netdev->ap)
   4428 		memcpy(netdev->ap->prev_bssid, orig_bss->addr, ETH_ALEN);
   4429 
   4430 	netdev_rssi_polling_update(netdev);
   4431 
   4432 	if (old_sm)
   4433 		eapol_sm_free(old_sm);
   4434 
   4435 	if (old_hs)
   4436 		handshake_state_unref(old_hs);
   4437 
   4438 	return 0;
   4439 }
   4440 
   4441 static void netdev_join_adhoc_cb(struct l_genl_msg *msg, void *user_data)
   4442 {
   4443 	struct netdev *netdev = user_data;
   4444 
   4445 	netdev->join_adhoc_cmd_id = 0;
   4446 
   4447 	if (netdev->adhoc_cb)
   4448 		netdev->adhoc_cb(netdev, l_genl_msg_get_error(msg),
   4449 				netdev->user_data);
   4450 }
   4451 
   4452 int netdev_join_adhoc(struct netdev *netdev, const char *ssid,
   4453 			struct iovec *extra_ie, size_t extra_ie_elems,
   4454 			bool control_port, netdev_command_cb_t cb,
   4455 			void *user_data)
   4456 {
   4457 	struct l_genl_msg *cmd;
   4458 	uint32_t ifindex = netdev->index;
   4459 	uint32_t ch_freq = band_channel_to_freq(6, BAND_FREQ_2_4_GHZ);
   4460 	uint32_t ch_type = NL80211_CHAN_HT20;
   4461 
   4462 	if (netdev->type != NL80211_IFTYPE_ADHOC) {
   4463 		l_error("iftype is invalid for adhoc: %u",
   4464 				netdev_get_iftype(netdev));
   4465 		return -ENOTSUP;
   4466 	}
   4467 
   4468 	if (netdev->join_adhoc_cmd_id || netdev->leave_adhoc_cmd_id)
   4469 		return -EBUSY;
   4470 
   4471 	netdev->adhoc_cb = cb;
   4472 	netdev->user_data = user_data;
   4473 
   4474 	cmd = l_genl_msg_new_sized(NL80211_CMD_JOIN_IBSS, 128);
   4475 
   4476 	l_genl_msg_append_attr(cmd, NL80211_ATTR_IFINDEX, 4, &ifindex);
   4477 	l_genl_msg_append_attr(cmd, NL80211_ATTR_SSID, strlen(ssid), ssid);
   4478 	l_genl_msg_append_attr(cmd, NL80211_ATTR_WIPHY_FREQ, 4, &ch_freq);
   4479 	l_genl_msg_append_attr(cmd, NL80211_ATTR_WIPHY_CHANNEL_TYPE, 4,
   4480 			&ch_type);
   4481 	l_genl_msg_append_attrv(cmd, NL80211_ATTR_IE, extra_ie, extra_ie_elems);
   4482 	l_genl_msg_append_attr(cmd, NL80211_ATTR_SOCKET_OWNER, 0, NULL);
   4483 
   4484 	if (control_port) {
   4485 		l_genl_msg_append_attr(cmd, NL80211_ATTR_CONTROL_PORT, 0, NULL);
   4486 
   4487 		if (netdev->pae_over_nl80211)
   4488 			l_genl_msg_append_attr(cmd,
   4489 					NL80211_ATTR_CONTROL_PORT_OVER_NL80211,
   4490 					0, NULL);
   4491 	}
   4492 
   4493 	netdev->join_adhoc_cmd_id = l_genl_family_send(nl80211, cmd,
   4494 			netdev_join_adhoc_cb, netdev, NULL);
   4495 
   4496 	if (!netdev->join_adhoc_cmd_id) {
   4497 		netdev->adhoc_cb = NULL;
   4498 		netdev->user_data = NULL;
   4499 		return -EIO;
   4500 	}
   4501 
   4502 	return 0;
   4503 }
   4504 
   4505 static void netdev_leave_adhoc_cb(struct l_genl_msg *msg, void *user_data)
   4506 {
   4507 	struct netdev *netdev = user_data;
   4508 
   4509 	netdev->leave_adhoc_cmd_id = 0;
   4510 
   4511 	if (netdev->adhoc_cb)
   4512 		netdev->adhoc_cb(netdev, l_genl_msg_get_error(msg),
   4513 				netdev->user_data);
   4514 
   4515 	netdev->adhoc_cb = NULL;
   4516 }
   4517 
   4518 int netdev_leave_adhoc(struct netdev *netdev, netdev_command_cb_t cb,
   4519 			void *user_data)
   4520 {
   4521 	struct l_genl_msg *cmd;
   4522 
   4523 	if (netdev->type != NL80211_IFTYPE_ADHOC) {
   4524 		l_error("iftype is invalid for adhoc: %u",
   4525 				netdev_get_iftype(netdev));
   4526 		return -ENOTSUP;
   4527 	}
   4528 
   4529 	if (netdev->join_adhoc_cmd_id || netdev->leave_adhoc_cmd_id)
   4530 		return -EBUSY;
   4531 
   4532 	netdev->adhoc_cb = cb;
   4533 	netdev->user_data = user_data;
   4534 
   4535 	cmd = l_genl_msg_new_sized(NL80211_CMD_LEAVE_IBSS, 64);
   4536 
   4537 	l_genl_msg_append_attr(cmd, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   4538 
   4539 	netdev->leave_adhoc_cmd_id = l_genl_family_send(nl80211, cmd,
   4540 						netdev_leave_adhoc_cb, netdev,
   4541 						NULL);
   4542 
   4543 	if (!netdev->leave_adhoc_cmd_id)
   4544 		return -EIO;
   4545 
   4546 	return 0;
   4547 }
   4548 
   4549 static uint32_t netdev_send_action_framev(struct netdev *netdev,
   4550 					const uint8_t *to,
   4551 					struct iovec *iov, size_t iov_len,
   4552 					uint32_t freq,
   4553 					l_genl_msg_func_t callback,
   4554 					void *user_data)
   4555 {
   4556 	uint32_t id;
   4557 	struct l_genl_msg *msg = nl80211_build_cmd_frame(netdev->index,
   4558 								0x00d0,
   4559 								netdev->addr,
   4560 								to, freq,
   4561 								iov, iov_len);
   4562 
   4563 	id = l_genl_family_send(nl80211, msg, callback, user_data, NULL);
   4564 
   4565 	if (!id)
   4566 		l_genl_msg_unref(msg);
   4567 
   4568 	return id;
   4569 }
   4570 
   4571 static uint32_t netdev_send_action_frame(struct netdev *netdev,
   4572 					const uint8_t *to,
   4573 					const uint8_t *body, size_t body_len,
   4574 					uint32_t freq,
   4575 					l_genl_msg_func_t callback,
   4576 					void *user_data)
   4577 {
   4578 	struct iovec iov[1];
   4579 
   4580 	iov[0].iov_base = (void *)body;
   4581 	iov[0].iov_len = body_len;
   4582 
   4583 	return netdev_send_action_framev(netdev, to, iov, 1, freq, callback,
   4584 						user_data);
   4585 }
   4586 
   4587 static void netdev_ft_frame_cb(struct l_genl_msg *msg, void *user_data)
   4588 {
   4589 	if (l_genl_msg_get_error(msg) < 0)
   4590 		l_debug("Failed to send FT-Frame");
   4591 }
   4592 
   4593 static int netdev_tx_ft_frame(uint32_t ifindex, uint16_t frame_type,
   4594 					uint32_t frequency, const uint8_t *dest,
   4595 					struct iovec *iov, size_t iov_len)
   4596 {
   4597 	struct netdev *netdev = netdev_find(ifindex);
   4598 	struct l_genl_msg *msg = nl80211_build_cmd_frame(netdev->index,
   4599 							frame_type,
   4600 							netdev->addr, dest,
   4601 							frequency,
   4602 							iov, iov_len);
   4603 
   4604 	/*
   4605 	 * Even though the kernel is doing offchannel for Authentication this
   4606 	 * flag is still required otherwise the kernel gives -EBUSY.
   4607 	 */
   4608 	l_genl_msg_append_attr(msg, NL80211_ATTR_OFFCHANNEL_TX_OK, 0, NULL);
   4609 
   4610 	if (!l_genl_family_send(nl80211, msg, netdev_ft_frame_cb,
   4611 				netdev, NULL)) {
   4612 		l_genl_msg_unref(msg);
   4613 		return -EIO;
   4614 	}
   4615 
   4616 	return 0;
   4617 }
   4618 
   4619 int netdev_ft_reassociate(struct netdev *netdev,
   4620 				const struct scan_bss *target_bss,
   4621 				const struct scan_bss *orig_bss,
   4622 				netdev_event_func_t event_filter,
   4623 				netdev_connect_cb_t cb, void *user_data)
   4624 {
   4625 	struct handshake_state *hs = netdev->handshake;
   4626 	struct netdev_handshake_state *nhs;
   4627 	struct l_genl_msg *msg;
   4628 	struct iovec iov[64];
   4629 	unsigned int n_iov = L_ARRAY_SIZE(iov);
   4630 	unsigned int c_iov = 0;
   4631 	enum mpdu_management_subtype subtype =
   4632 				MPDU_MANAGEMENT_SUBTYPE_REASSOCIATION_REQUEST;
   4633 
   4634 	/*
   4635 	 * At this point there is no going back with FT so reset all the flags
   4636 	 * needed to associate with a new BSS.
   4637 	 */
   4638 	netdev->frequency = target_bss->frequency;
   4639 	netdev->handshake->active_tk_index = 0;
   4640 	netdev->associated = false;
   4641 	netdev->operational = false;
   4642 	netdev->in_ft = true;
   4643 	netdev->event_filter = event_filter;
   4644 	netdev->connect_cb = cb;
   4645 	netdev->user_data = user_data;
   4646 	netdev->low_signal_threshold = LOW_SIGNAL_THRESHOLD;
   4647 	netdev->low_signal_threshold_5ghz = LOW_SIGNAL_THRESHOLD_5GHZ;
   4648 
   4649 	/*
   4650 	 * Cancel commands that could be running because of EAPoL activity
   4651 	 * like re-keying, this way the callbacks for those commands don't
   4652 	 * have to check if failures resulted from the transition.
   4653 	 */
   4654 	nhs = l_container_of(netdev->handshake,
   4655 				struct netdev_handshake_state, super);
   4656 
   4657 	/* reset key states just as we do in initialization */
   4658 	nhs->complete = false;
   4659 	nhs->ptk_installed = false;
   4660 	nhs->gtk_installed = true;
   4661 	nhs->igtk_installed = true;
   4662 
   4663 	if (nhs->group_new_key_cmd_id) {
   4664 		l_genl_family_cancel(nl80211, nhs->group_new_key_cmd_id);
   4665 		nhs->group_new_key_cmd_id = 0;
   4666 	}
   4667 
   4668 	if (nhs->group_management_new_key_cmd_id) {
   4669 		l_genl_family_cancel(nl80211,
   4670 			nhs->group_management_new_key_cmd_id);
   4671 		nhs->group_management_new_key_cmd_id = 0;
   4672 	}
   4673 
   4674 	if (netdev->rekey_offload_cmd_id) {
   4675 		l_genl_family_cancel(nl80211, netdev->rekey_offload_cmd_id);
   4676 		netdev->rekey_offload_cmd_id = 0;
   4677 	}
   4678 
   4679 	netdev_rssi_polling_update(netdev);
   4680 	netdev_cqm_rssi_update(netdev);
   4681 
   4682 	if (netdev->sm) {
   4683 		eapol_sm_free(netdev->sm);
   4684 		netdev->sm = NULL;
   4685 
   4686 		netdev_ensure_eapol_registered(netdev);
   4687 	}
   4688 
   4689 	msg = netdev_build_cmd_associate_common(netdev);
   4690 
   4691 	c_iov = netdev_populate_common_ies(netdev, hs, msg, iov, n_iov, c_iov);
   4692 
   4693 	if (hs->supplicant_ie)
   4694 		c_iov = iov_ie_append(iov, n_iov, c_iov, hs->supplicant_ie,
   4695 					IE_LEN(hs->supplicant_ie));
   4696 
   4697 	if (hs->supplicant_fte)
   4698 		c_iov = iov_ie_append(iov, n_iov, c_iov, hs->supplicant_fte,
   4699 					IE_LEN(hs->supplicant_fte));
   4700 
   4701 	if (hs->mde)
   4702 		c_iov = iov_ie_append(iov, n_iov, c_iov, hs->mde,
   4703 					IE_LEN(hs->mde));
   4704 
   4705 	mpdu_sort_ies(subtype, iov, c_iov);
   4706 
   4707 	l_genl_msg_append_attr(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
   4708 				orig_bss->addr);
   4709 	l_genl_msg_append_attrv(msg, NL80211_ATTR_IE, iov, c_iov);
   4710 
   4711 	netdev->connect_cmd_id = l_genl_family_send(nl80211, msg,
   4712 						netdev_cmd_ft_reassociate_cb,
   4713 						netdev, NULL);
   4714 	if (!netdev->connect_cmd_id) {
   4715 		l_genl_msg_unref(msg);
   4716 
   4717 		return -EIO;
   4718 	}
   4719 
   4720 	return 0;
   4721 }
   4722 
   4723 static void netdev_ft_response_frame_event(const struct mmpdu_header *hdr,
   4724 					const void *body, size_t body_len,
   4725 					int rssi, void *user_data)
   4726 {
   4727 	struct netdev *netdev = user_data;
   4728 
   4729 	if (!netdev->connected)
   4730 		return;
   4731 
   4732 	__ft_rx_action(netdev->index, (const uint8_t *)hdr,
   4733 			mmpdu_header_len(hdr) + body_len);
   4734 }
   4735 
   4736 static void netdev_ft_auth_response_frame_event(const struct mmpdu_header *hdr,
   4737 					const void *body, size_t body_len,
   4738 					int rssi, void *user_data)
   4739 {
   4740 	struct netdev *netdev = user_data;
   4741 
   4742 	if (!netdev->connected)
   4743 		return;
   4744 
   4745 	__ft_rx_authenticate(netdev->index, (const uint8_t *)hdr,
   4746 			mmpdu_header_len(hdr) + body_len);
   4747 }
   4748 
   4749 static void netdev_qos_map_frame_event(const struct mmpdu_header *hdr,
   4750 					const void *body, size_t body_len,
   4751 					int rssi, void *user_data)
   4752 {
   4753 	struct netdev *netdev = user_data;
   4754 
   4755 	/* No point telling the kernel */
   4756 	if (!netdev->connected)
   4757 		return;
   4758 
   4759 	if (memcmp(netdev->handshake->aa, hdr->address_2, ETH_ALEN))
   4760 		return;
   4761 
   4762 	if (body_len < 5)
   4763 		return;
   4764 
   4765 	if (l_get_u8(body + 2) != IE_TYPE_QOS_MAP_SET)
   4766 		return;
   4767 
   4768 	netdev_send_qos_map_set(netdev, body + 4, body_len - 4);
   4769 }
   4770 
   4771 static void netdev_sae_external_auth_frame_event(const struct mmpdu_header *hdr,
   4772 					const void *body, size_t body_len,
   4773 					int rssi, void *user_data)
   4774 {
   4775 	struct netdev *netdev = user_data;
   4776 	const struct mmpdu_authentication *auth;
   4777 	uint16_t status_code = MMPDU_STATUS_CODE_UNSPECIFIED;
   4778 	int ret;
   4779 
   4780 	if (!netdev->external_auth)
   4781 		return;
   4782 
   4783 	if (!netdev->ap)
   4784 		return;
   4785 
   4786 	auth = mmpdu_body(hdr);
   4787 	/*
   4788 	 * Allows station to persist settings so it does not retry
   4789 	 * the higher order ECC group again
   4790 	 */
   4791 	if (L_CPU_TO_LE16(auth->status) ==
   4792 			MMPDU_STATUS_CODE_UNSUPP_FINITE_CYCLIC_GROUP &&
   4793 			netdev->event_filter)
   4794 		netdev->event_filter(netdev, NETDEV_EVENT_ECC_GROUP_RETRY,
   4795 					NULL, netdev->user_data);
   4796 
   4797 	ret = auth_proto_rx_authenticate(netdev->ap, (const void *) hdr,
   4798 					mmpdu_header_len(hdr) + body_len);
   4799 
   4800 	switch (ret) {
   4801 	case 0:
   4802 	case -EAGAIN:
   4803 		return;
   4804 	case -ENOMSG:
   4805 	case -EBADMSG:
   4806 		return;
   4807 	default:
   4808 		break;
   4809 	}
   4810 
   4811 	if (ret > 0)
   4812 		status_code = (uint16_t)ret;
   4813 
   4814 	netdev_send_external_auth(netdev, status_code);
   4815 }
   4816 
   4817 static void netdev_preauth_cb(const uint8_t *pmk, void *user_data)
   4818 {
   4819 	struct netdev_preauth_state *preauth = user_data;
   4820 	netdev_preauthenticate_cb_t cb = preauth->cb;
   4821 
   4822 	preauth->cb = NULL;
   4823 
   4824 	cb(preauth->netdev,
   4825 		pmk ? NETDEV_RESULT_OK : NETDEV_RESULT_HANDSHAKE_FAILED,
   4826 		pmk, preauth->user_data);
   4827 }
   4828 
   4829 int netdev_preauthenticate(struct netdev *netdev,
   4830 				const struct scan_bss *target_bss,
   4831 				netdev_preauthenticate_cb_t cb, void *user_data)
   4832 {
   4833 	struct netdev_preauth_state *preauth;
   4834 
   4835 	if (!netdev->operational)
   4836 		return -ENOTCONN;
   4837 
   4838 	preauth = l_new(struct netdev_preauth_state, 1);
   4839 
   4840 	if (!eapol_preauth_start(target_bss->addr, netdev->handshake,
   4841 					netdev_preauth_cb, preauth,
   4842 					netdev_preauth_destroy)) {
   4843 		l_free(preauth);
   4844 
   4845 		return -EIO;
   4846 	}
   4847 
   4848 	preauth->cb = cb;
   4849 	preauth->user_data = user_data;
   4850 	preauth->netdev = netdev;
   4851 
   4852 	return 0;
   4853 }
   4854 
   4855 static void netdev_neighbor_report_req_cb(struct l_genl_msg *msg,
   4856 						void *user_data)
   4857 {
   4858 	struct netdev *netdev = user_data;
   4859 
   4860 	if (!netdev->neighbor_report_cb)
   4861 		return;
   4862 
   4863 	if (l_genl_msg_get_error(msg) < 0) {
   4864 		netdev->neighbor_report_cb(netdev, l_genl_msg_get_error(msg),
   4865 						NULL, 0, netdev->user_data);
   4866 
   4867 		netdev->neighbor_report_cb = NULL;
   4868 
   4869 		l_timeout_remove(netdev->neighbor_report_timeout);
   4870 	}
   4871 }
   4872 
   4873 static void netdev_neighbor_report_timeout(struct l_timeout *timeout,
   4874 						void *user_data)
   4875 {
   4876 	struct netdev *netdev = user_data;
   4877 
   4878 	netdev->neighbor_report_cb(netdev, -ETIMEDOUT, NULL, 0,
   4879 					netdev->user_data);
   4880 
   4881 	netdev->neighbor_report_cb = NULL;
   4882 
   4883 	l_timeout_remove(netdev->neighbor_report_timeout);
   4884 }
   4885 
   4886 int netdev_neighbor_report_req(struct netdev *netdev,
   4887 				netdev_neighbor_report_cb_t cb)
   4888 {
   4889 	const uint8_t action_frame[] = {
   4890 		0x05, /* Category: Radio Measurement */
   4891 		0x04, /* Radio Measurement Action: Neighbor Report Request */
   4892 		0x01, /* Dialog Token: a non-zero value (unused) */
   4893 	};
   4894 
   4895 	if (netdev->neighbor_report_cb || !netdev->connected)
   4896 		return -EBUSY;
   4897 
   4898 	if (!netdev_send_action_frame(netdev, netdev->handshake->aa,
   4899 					action_frame, sizeof(action_frame),
   4900 					netdev->frequency,
   4901 					netdev_neighbor_report_req_cb,
   4902 					netdev))
   4903 		return -EIO;
   4904 
   4905 	netdev->neighbor_report_cb = cb;
   4906 
   4907 	/* Set a 3-second timeout */
   4908 	netdev->neighbor_report_timeout =
   4909 		l_timeout_create(3, netdev_neighbor_report_timeout,
   4910 					netdev, NULL);
   4911 
   4912 	return 0;
   4913 }
   4914 
   4915 static void netdev_neighbor_report_frame_event(const struct mmpdu_header *hdr,
   4916 					const void *body, size_t body_len,
   4917 					int rssi, void *user_data)
   4918 {
   4919 	struct netdev *netdev = user_data;
   4920 
   4921 	if (body_len < 3) {
   4922 		l_debug("Neighbor Report frame too short");
   4923 		return;
   4924 	}
   4925 
   4926 	if (!netdev->neighbor_report_cb)
   4927 		return;
   4928 
   4929 	/*
   4930 	 * Don't use the dialog token (byte 3), return the first Neighbor
   4931 	 * Report Response received.
   4932 	 *
   4933 	 * Byte 1 is 0x05 for Radio Measurement, byte 2 is 0x05 for
   4934 	 * Neighbor Report.
   4935 	 */
   4936 
   4937 	netdev->neighbor_report_cb(netdev, 0, body + 3, body_len - 3,
   4938 					netdev->user_data);
   4939 	netdev->neighbor_report_cb = NULL;
   4940 
   4941 	l_timeout_remove(netdev->neighbor_report_timeout);
   4942 }
   4943 
   4944 static void netdev_sa_query_resp_cb(struct l_genl_msg *msg, void *user_data)
   4945 {
   4946 	int err = l_genl_msg_get_error(msg);
   4947 	const char *ext_error;
   4948 
   4949 	if (err >= 0)
   4950 		return;
   4951 
   4952 	ext_error = l_genl_msg_get_extended_error(msg);
   4953 	l_debug("error sending SA Query request: %s",
   4954 			ext_error ? ext_error : strerror(-err));
   4955 }
   4956 
   4957 static int netdev_build_oci(struct netdev *netdev, uint8_t *out)
   4958 {
   4959 	out[0] = IE_TYPE_EXTENSION;
   4960 	out[1] = 4;
   4961 	out[2] = IE_TYPE_OCI & 0xff;
   4962 
   4963 	return oci_from_chandef(netdev->handshake->chandef, out + 3);
   4964 }
   4965 
   4966 static void netdev_sa_query_timeout(struct l_timeout *timeout,
   4967 		void *user_data)
   4968 {
   4969 	struct netdev *netdev = user_data;
   4970 
   4971 	l_info("SA Query timed out, connection is invalid.  Disconnecting...");
   4972 
   4973 	l_timeout_remove(netdev->sa_query_timeout);
   4974 	netdev->sa_query_timeout = NULL;
   4975 
   4976 	netdev_disconnect_by_sme(netdev, NETDEV_RESULT_ABORTED,
   4977 					MMPDU_REASON_CODE_PREV_AUTH_NOT_VALID);
   4978 }
   4979 
   4980 static void netdev_sa_query_req_cb(struct l_genl_msg *msg, void *user_data)
   4981 {
   4982 	struct netdev *netdev = user_data;
   4983 	int err = l_genl_msg_get_error(msg);
   4984 	const char *ext_error;
   4985 
   4986 	if (err >= 0)
   4987 		return;
   4988 
   4989 	ext_error = l_genl_msg_get_extended_error(msg);
   4990 	l_debug("error sending SA Query request: %s",
   4991 			ext_error ? ext_error : strerror(-err));
   4992 
   4993 	l_timeout_remove(netdev->sa_query_timeout);
   4994 	netdev->sa_query_timeout = NULL;
   4995 }
   4996 
   4997 static bool netdev_send_sa_query_request(struct netdev *netdev)
   4998 {
   4999 	uint8_t req[10];
   5000 	uint8_t *ptr = req;
   5001 
   5002 	ptr[0] = 0x08; /* Category: SA Query */
   5003 	ptr[1] = 0x00; /* SA Query Action: Request */
   5004 
   5005 	/* Transaction ID */
   5006 	l_getrandom(ptr + 2, 2);
   5007 
   5008 	ptr += 4;
   5009 
   5010 	if (netdev->handshake->supplicant_ocvc &&
   5011 					netdev->handshake->authenticator_ocvc) {
   5012 		if (netdev_build_oci(netdev, ptr) < 0) {
   5013 			l_debug("Could not build OCI");
   5014 			return false;
   5015 		}
   5016 
   5017 		ptr += 6;
   5018 	}
   5019 
   5020 	if (!netdev_send_action_frame(netdev, netdev->handshake->aa, req,
   5021 			ptr - req, netdev->frequency,
   5022 			netdev_sa_query_req_cb, netdev)) {
   5023 		l_error("error sending SA Query action frame");
   5024 		return false;
   5025 	}
   5026 
   5027 	netdev->sa_query_id = l_get_u16(req + 2);
   5028 	netdev->sa_query_timeout = l_timeout_create(3,
   5029 			netdev_sa_query_timeout, netdev, NULL);
   5030 
   5031 	return true;
   5032 }
   5033 
   5034 static void netdev_sa_query_req_frame_event(const struct mmpdu_header *hdr,
   5035 					const void *body, size_t body_len,
   5036 					int rssi, void *user_data)
   5037 {
   5038 	uint8_t sa_resp[10];
   5039 	uint8_t *ptr = sa_resp;
   5040 	uint16_t transaction;
   5041 	const uint8_t *oci;
   5042 	struct netdev *netdev = user_data;
   5043 	bool ocvc;
   5044 
   5045 	if (body_len < 4) {
   5046 		l_debug("SA Query request too short");
   5047 		return;
   5048 	}
   5049 
   5050 	if (!netdev->connected)
   5051 		return;
   5052 
   5053 	ocvc = netdev->handshake->supplicant_ocvc &&
   5054 					netdev->handshake->authenticator_ocvc;
   5055 
   5056 	/* only care about SA Queries from our connected AP */
   5057 	if (memcmp(hdr->address_2, netdev->handshake->aa, 6))
   5058 		return;
   5059 
   5060 	transaction = l_get_u16(body + 2);
   5061 
   5062 	body_len -= 4;
   5063 
   5064 	if (ocvc) {
   5065 		/*
   5066 		 * IEEE 802.11 Section 11.13
   5067 		 *
   5068 		 * "A STA that supports the SA Query procedure and receives an
   5069 		 * SA Query Request frame shall respond with an SA Query
   5070 		 * Response frame if none of the following are true...
   5071 		 * - OCI element is not present in the request or
   5072 		 * - Operating channel information indicated does not match the
   5073 		 *   current channel information (see 12.2.9)."
   5074 		 */
   5075 		if (ie_parse_oci(body + 4, body_len, &oci) < 0) {
   5076 			l_debug("Could not parse OCI");
   5077 			return;
   5078 		}
   5079 
   5080 		if (oci_verify(oci, netdev->handshake->chandef) < 0) {
   5081 			l_debug("Could not verify OCI");
   5082 			return;
   5083 		}
   5084 	}
   5085 
   5086 	ptr[0] = 0x08;	/* SA Query */
   5087 	ptr[1] = 0x01;	/* Response */
   5088 	memcpy(ptr + 2, &transaction, 2);
   5089 
   5090 	ptr += 4;
   5091 
   5092 	/*
   5093 	 * IEEE 802.11 Section 11.13
   5094 	 *
   5095 	 * "A STA that responds with an SA Query Response frame to a STA that
   5096 	 * indicated OCVC capability shall include OCI element in the response
   5097 	 * frame if dot11RSNAOperatingChannelValidationActivated is true"
   5098 	 */
   5099 	if (ocvc) {
   5100 		if (netdev_build_oci(netdev, ptr) < 0) {
   5101 			l_debug("Could not build OCI");
   5102 			return;
   5103 		}
   5104 
   5105 		ptr += 6;
   5106 	}
   5107 
   5108 	l_info("received SA Query request from "MAC", transaction=%u",
   5109 			MAC_STR(hdr->address_2), transaction);
   5110 
   5111 	if (!netdev_send_action_frame(netdev, netdev->handshake->aa,
   5112 			sa_resp, ptr - sa_resp,
   5113 			netdev->frequency,
   5114 			netdev_sa_query_resp_cb, netdev)) {
   5115 		l_error("error sending SA Query response");
   5116 		return;
   5117 	}
   5118 }
   5119 
   5120 static void netdev_sa_query_resp_frame_event(const struct mmpdu_header *hdr,
   5121 					const void *body, size_t body_len,
   5122 					int rssi, void *user_data)
   5123 {
   5124 	struct netdev *netdev = user_data;
   5125 	const uint8_t *ptr = body;
   5126 	const uint8_t *oci;
   5127 
   5128 	if (!netdev->connected)
   5129 		return;
   5130 
   5131 	if (body_len < 4) {
   5132 		l_debug("SA Query frame too short");
   5133 		return;
   5134 	}
   5135 
   5136 	ptr += 2;
   5137 
   5138 	l_debug("SA Query src="MAC" dest="MAC" bssid="MAC" transaction=%u",
   5139 			MAC_STR(hdr->address_2), MAC_STR(hdr->address_1),
   5140 			MAC_STR(hdr->address_3), l_get_u16(ptr));
   5141 
   5142 	if (!netdev->sa_query_timeout) {
   5143 		l_debug("no SA Query request sent");
   5144 		return;
   5145 	}
   5146 
   5147 	/* check if this is from our connected BSS */
   5148 	if (memcmp(hdr->address_2, netdev->handshake->aa, 6)) {
   5149 		l_debug("received SA Query from non-connected AP");
   5150 		return;
   5151 	}
   5152 
   5153 	if (memcmp(ptr, &netdev->sa_query_id, 2)) {
   5154 		l_debug("SA Query transaction ID's did not match");
   5155 		return;
   5156 	}
   5157 
   5158 	if (!(netdev->handshake->supplicant_ocvc &&
   5159 				netdev->handshake->authenticator_ocvc))
   5160 		goto keep_alive;
   5161 
   5162 	ptr += 2;
   5163 	body_len -= 4;
   5164 
   5165 	/*
   5166 	 * IEEE 802.11 Section 11.13
   5167 	 *
   5168 	 * "When a non-AP or non-PCP STA receives the SA Query Response frame
   5169 	 * from a STA that indicated OCVC capability, it shall ensure that OCI
   5170 	 * element is present in the response and the channel information in the
   5171 	 * OCI element matches current operating channel parameters
   5172 	 * (see 12.2.9). Otherwise, the receiving STA shall deem the response
   5173 	 * as invalid and discard it"
   5174 	 */
   5175 	if (ie_parse_oci(ptr, body_len, &oci) < 0) {
   5176 		l_debug("Invalid OCI element");
   5177 		return;
   5178 	}
   5179 
   5180 	if (oci_verify(oci, netdev->handshake->chandef) < 0) {
   5181 		l_debug("Could not verify OCI element");
   5182 		return;
   5183 	}
   5184 
   5185 keep_alive:
   5186 	l_info("SA Query response from connected BSS received, "
   5187 			"keeping the connection active");
   5188 
   5189 	l_timeout_remove(netdev->sa_query_timeout);
   5190 	netdev->sa_query_timeout = NULL;
   5191 }
   5192 
   5193 static void netdev_unprot_disconnect_event(struct l_genl_msg *msg,
   5194 		struct netdev *netdev)
   5195 {
   5196 	const struct mmpdu_header *hdr = NULL;
   5197 	struct l_genl_attr attr;
   5198 	uint16_t type;
   5199 	uint16_t len;
   5200 	const void *data;
   5201 	uint16_t reason_code;
   5202 
   5203 	if (!netdev->connected)
   5204 		return;
   5205 
   5206 	/* ignore excessive disassociate requests */
   5207 	if (netdev->sa_query_timeout) {
   5208 		l_debug("SA Query already in progress, ignoring");
   5209 		return;
   5210 	}
   5211 
   5212 	if (!l_genl_attr_init(&attr, msg))
   5213 		return;
   5214 
   5215 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
   5216 		switch (type) {
   5217 		case NL80211_ATTR_FRAME:
   5218 			hdr = mpdu_validate(data, len);
   5219 			break;
   5220 		}
   5221 	}
   5222 
   5223 	/* check that ATTR_FRAME was actually included */
   5224 	if (!hdr)
   5225 		return;
   5226 
   5227 	reason_code = l_get_le16(mmpdu_body(hdr));
   5228 
   5229 	l_info("unprotected disconnect event, src="MAC" dest="MAC
   5230 			 " bssid="MAC" reason=%u",
   5231 			MAC_STR(hdr->address_2), MAC_STR(hdr->address_1),
   5232 			MAC_STR(hdr->address_3), reason_code);
   5233 
   5234 	if (memcmp(hdr->address_2, netdev->handshake->aa, 6)) {
   5235 		l_debug("received invalid disassociate frame");
   5236 		return;
   5237 	}
   5238 
   5239 	if (reason_code != MMPDU_REASON_CODE_CLASS2_FRAME_FROM_NONAUTH_STA &&
   5240 			reason_code !=
   5241 			MMPDU_REASON_CODE_CLASS3_FRAME_FROM_NONASSOC_STA) {
   5242 		l_debug("invalid reason code %u", reason_code);
   5243 		return;
   5244 	}
   5245 
   5246 	netdev_send_sa_query_request(netdev);
   5247 }
   5248 
   5249 static void netdev_station_event(struct l_genl_msg *msg,
   5250 					struct netdev *netdev, bool added)
   5251 {
   5252 	struct l_genl_attr attr;
   5253 	uint16_t type;
   5254 	uint16_t len;
   5255 	const void *data;
   5256 	const uint8_t *mac = NULL;
   5257 
   5258 	if (netdev_get_iftype(netdev) != NETDEV_IFTYPE_ADHOC)
   5259 		return;
   5260 
   5261 	if (!l_genl_attr_init(&attr, msg))
   5262 		return;
   5263 
   5264 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
   5265 		switch (type) {
   5266 		case NL80211_ATTR_MAC:
   5267 			mac = data;
   5268 			break;
   5269 		}
   5270 	}
   5271 
   5272 	if (!mac) {
   5273 		l_error("%s station event did not include MAC attribute",
   5274 				added ? "new" : "del");
   5275 		return;
   5276 	}
   5277 
   5278 	WATCHLIST_NOTIFY(&netdev->station_watches,
   5279 			netdev_station_watch_func_t, netdev, mac, added);
   5280 }
   5281 
   5282 static struct netdev *netdev_from_message(struct l_genl_msg *msg)
   5283 {
   5284 	uint32_t ifindex;
   5285 
   5286 	if (nl80211_parse_attrs(msg, NL80211_ATTR_IFINDEX, &ifindex,
   5287 					NL80211_ATTR_UNSPEC) < 0)
   5288 		return NULL;
   5289 
   5290 	return netdev_find(ifindex);
   5291 }
   5292 
   5293 static void netdev_scan_notify(struct l_genl_msg *msg, void *user_data)
   5294 {
   5295 	struct netdev *netdev;
   5296 
   5297 	netdev = netdev_from_message(msg);
   5298 	if (!netdev)
   5299 		return;
   5300 
   5301 	switch (l_genl_msg_get_command(msg)) {
   5302 	case NL80211_CMD_NEW_SCAN_RESULTS:
   5303 		netdev_new_scan_results_event(msg, netdev);
   5304 		break;
   5305 	}
   5306 }
   5307 
   5308 static bool match_addr(const void *a, const void *b)
   5309 {
   5310 	const struct scan_bss *bss = a;
   5311 
   5312 	return memcmp(bss->addr, b, 6) == 0;
   5313 }
   5314 
   5315 static bool netdev_get_fw_scan_cb(int err, struct l_queue *bss_list,
   5316 					const struct scan_freq_set *freqs,
   5317 					void *user_data)
   5318 {
   5319 	struct netdev *netdev = user_data;
   5320 	struct scan_bss *bss = NULL;
   5321 
   5322 	/*
   5323 	 * If we happened to be disconnected prior to  GET_SCAN coming back
   5324 	 * just bail out now. This disconnect should already have been handled.
   5325 	 */
   5326 	if (!netdev->connected)
   5327 		return false;
   5328 
   5329 	if (err < 0) {
   5330 		l_error("Failed to get scan after roam (%d)", err);
   5331 		return false;
   5332 	}
   5333 
   5334 	/*
   5335 	 * We don't actually need the entire list since we only provide
   5336 	 * station with the roamed BSS. We can remove the BSS we want and by
   5337 	 * returning false scan will keep ownership of the list.
   5338 	 */
   5339 	bss = l_queue_remove_if(bss_list, match_addr, netdev->handshake->aa);
   5340 
   5341 	if (!bss) {
   5342 		l_error("Roam target BSS not found in scan results");
   5343 		return false;
   5344 	}
   5345 
   5346 	netdev->fw_roam_bss = bss;
   5347 
   5348 	handshake_state_set_authenticator_ie(netdev->handshake, bss->rsne);
   5349 
   5350 	if (is_offload(netdev->handshake)) {
   5351 		netdev_connect_ok(netdev);
   5352 		return false;
   5353 	}
   5354 
   5355 	if (netdev->sm)
   5356 		L_WARN_ON(!eapol_start(netdev->sm));
   5357 
   5358 	return false;
   5359 }
   5360 
   5361 /*
   5362  * CMD_ROAM indicates that the driver has already roamed/associated with a new
   5363  * AP. This event is nearly identical to the CMD_CONNECT event which is why
   5364  * netdev_connect_event will handle all the parsing of IE's just as it does
   5365  * normally.
   5366  *
   5367  * Using GET_SCAN we can grab all the required scan_bss data, create that object
   5368  * and provide it to station.
   5369  *
   5370  * The current handshake/netdev_handshake objects are reused after being
   5371  * reset to allow eapol to happen again without it thinking this is a re-key.
   5372  */
   5373 static void netdev_roam_event(struct l_genl_msg *msg, struct netdev *netdev)
   5374 {
   5375 	struct netdev_handshake_state *nhs =
   5376 			l_container_of(netdev->handshake,
   5377 					struct netdev_handshake_state,
   5378 					super);
   5379 	struct l_genl_attr attr;
   5380 	uint16_t type, len;
   5381 	const void *data;
   5382 	const uint8_t *mac = NULL;
   5383 
   5384 	l_debug("");
   5385 
   5386 	netdev->operational = false;
   5387 
   5388 	if (L_WARN_ON(!l_genl_attr_init(&attr, msg)))
   5389 		return;
   5390 
   5391 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
   5392 		switch (type) {
   5393 		case NL80211_ATTR_MAC:
   5394 			mac = data;
   5395 			break;
   5396 		case NL80211_ATTR_REQ_IE:
   5397 			parse_request_ies(netdev, data, len);
   5398 			break;
   5399 		}
   5400 	}
   5401 
   5402 	if (!mac) {
   5403 		l_error("Failed to parse ATTR_MAC from CMD_ROAM");
   5404 		return;
   5405 	}
   5406 
   5407 	/* Handshake completed in firmware, just get the roamed BSS */
   5408 	if (is_offload(netdev->handshake))
   5409 		goto get_fw_scan;
   5410 
   5411 	/* Reset handshake state */
   5412 	nhs->complete = false;
   5413 	nhs->ptk_installed = false;
   5414 	nhs->gtk_installed = true;
   5415 	nhs->igtk_installed = true;
   5416 	netdev->handshake->ptk_complete = false;
   5417 
   5418 get_fw_scan:
   5419 	handshake_state_set_authenticator_address(netdev->handshake, mac);
   5420 
   5421 	if (L_WARN_ON(!scan_get_firmware_scan(netdev->wdev_id,
   5422 					netdev_get_fw_scan_cb,
   5423 					netdev, NULL)))
   5424 		return;
   5425 
   5426 	if (netdev->event_filter)
   5427 		netdev->event_filter(netdev, NETDEV_EVENT_ROAMING,
   5428 					NULL, netdev->user_data);
   5429 }
   5430 
   5431 static void netdev_send_sa_query_delay(struct l_timeout *timeout,
   5432 					void *user_data)
   5433 {
   5434 	struct netdev *netdev = user_data;
   5435 
   5436 	netdev_send_sa_query_request(netdev);
   5437 
   5438 	l_timeout_remove(netdev->sa_query_delay);
   5439 	netdev->sa_query_delay = NULL;
   5440 }
   5441 
   5442 static void netdev_channel_switch_event(struct l_genl_msg *msg,
   5443 					struct netdev *netdev)
   5444 {
   5445 	_auto_(l_free) struct band_chandef *chandef = NULL;
   5446 
   5447 	if (netdev->type != NL80211_IFTYPE_STATION)
   5448 		return;
   5449 
   5450 	if (L_WARN_ON(!netdev->connected))
   5451 		return;
   5452 
   5453 	chandef = l_new(struct band_chandef, 1);
   5454 
   5455 	if (nl80211_parse_chandef(msg, chandef) < 0) {
   5456 		l_debug("Couldn't parse operating channel info.");
   5457 		return;
   5458 	}
   5459 
   5460 	netdev->frequency = chandef->frequency;
   5461 
   5462 	l_debug("Channel switch event, frequency: %u", netdev->frequency);
   5463 
   5464 	handshake_state_set_chandef(netdev->handshake, l_steal_ptr(chandef));
   5465 
   5466 	/*
   5467 	 * IEEE 802.11-2020 11.9.3.2
   5468 	 * "If the STA chooses to perform the specified switch and
   5469 	 * dot11RSNAOperatingChannelValidationActivated is true and the AP has
   5470 	 * indicated OCVC capability, after switching to the new channel the STA
   5471 	 * shall wait a random delay uniformly-distributed in the range between
   5472 	 * zero and 5000us, and then initiate the SA query procedure"
   5473 	 */
   5474 	if (netdev->handshake->supplicant_ocvc &&
   5475 					netdev->handshake->authenticator_ocvc)
   5476 		netdev->sa_query_delay = l_timeout_create_ms(
   5477 						l_getrandom_uint32() % 5,
   5478 						netdev_send_sa_query_delay,
   5479 						netdev, NULL);
   5480 
   5481 	if (!netdev->event_filter)
   5482 		return;
   5483 
   5484 	netdev->event_filter(netdev, NETDEV_EVENT_CHANNEL_SWITCHED,
   5485 				&netdev->frequency, netdev->user_data);
   5486 }
   5487 
   5488 static void netdev_michael_mic_failure(struct l_genl_msg *msg,
   5489 					struct netdev *netdev)
   5490 {
   5491 	uint8_t idx;
   5492 	uint32_t type;
   5493 
   5494 	if (nl80211_parse_attrs(msg, NL80211_ATTR_KEY_IDX, &idx,
   5495 				NL80211_ATTR_KEY_TYPE, &type,
   5496 				NL80211_ATTR_UNSPEC) < 0)
   5497 		return;
   5498 
   5499 	l_debug("ifindex=%u key_idx=%u type=%u", netdev->index, idx, type);
   5500 }
   5501 
   5502 #define MAX_COMEBACK_DELAY 1200
   5503 
   5504 static void netdev_assoc_comeback(struct l_genl_msg *msg,
   5505 					struct netdev *netdev)
   5506 {
   5507 	const uint8_t *mac;
   5508 	uint32_t timeout;
   5509 
   5510 	if (L_WARN_ON(!netdev->connected))
   5511 		return;
   5512 
   5513 	if (nl80211_parse_attrs(msg, NL80211_ATTR_MAC, &mac,
   5514 				NL80211_ATTR_TIMEOUT, &timeout,
   5515 				NL80211_ATTR_UNSPEC) < 0)
   5516 		return;
   5517 
   5518 	if (L_WARN_ON(memcmp(mac, netdev->handshake->aa, ETH_ALEN)))
   5519 		return;
   5520 
   5521 	if (timeout <= MAX_COMEBACK_DELAY) {
   5522 		l_debug(MAC" requested an association comeback delay of %u TU",
   5523 			MAC_STR(netdev->handshake->aa), timeout);
   5524 		return;
   5525 	}
   5526 
   5527 	l_debug("Comeback delay of %u exceeded maximum of %u, deauthenticating",
   5528 		timeout, MAX_COMEBACK_DELAY);
   5529 
   5530 	netdev_deauth_and_fail_connection(netdev,
   5531 					NETDEV_RESULT_ASSOCIATION_FAILED,
   5532 					MMPDU_STATUS_CODE_REFUSED_TEMPORARILY);
   5533 }
   5534 
   5535 static void netdev_mlme_notify(struct l_genl_msg *msg, void *user_data)
   5536 {
   5537 	struct netdev *netdev = NULL;
   5538 	uint8_t cmd;
   5539 
   5540 	cmd = l_genl_msg_get_command(msg);
   5541 	l_debug("MLME notification %s(%u)", nl80211cmd_to_string(cmd), cmd);
   5542 
   5543 	netdev = netdev_from_message(msg);
   5544 	if (!netdev)
   5545 		return;
   5546 
   5547 	switch (cmd) {
   5548 	case NL80211_CMD_AUTHENTICATE:
   5549 		netdev_authenticate_event(msg, netdev);
   5550 		break;
   5551 	case NL80211_CMD_DEAUTHENTICATE:
   5552 		netdev_deauthenticate_event(msg, netdev);
   5553 		break;
   5554 	case NL80211_CMD_ASSOCIATE:
   5555 		netdev_associate_event(msg, netdev);
   5556 		break;
   5557 	case NL80211_CMD_ROAM:
   5558 		netdev_roam_event(msg, netdev);
   5559 		break;
   5560 	case NL80211_CMD_CH_SWITCH_NOTIFY:
   5561 		netdev_channel_switch_event(msg, netdev);
   5562 		break;
   5563 	case NL80211_CMD_CONNECT:
   5564 		netdev_connect_event(msg, netdev);
   5565 		break;
   5566 	case NL80211_CMD_DISCONNECT:
   5567 		netdev_disconnect_event(msg, netdev);
   5568 		break;
   5569 	case NL80211_CMD_NOTIFY_CQM:
   5570 		netdev_cqm_event(msg, netdev);
   5571 		break;
   5572 	case NL80211_CMD_SET_REKEY_OFFLOAD:
   5573 		netdev_rekey_offload_event(msg, netdev);
   5574 		break;
   5575 	case NL80211_CMD_UNPROT_DEAUTHENTICATE:
   5576 	case NL80211_CMD_UNPROT_DISASSOCIATE:
   5577 		netdev_unprot_disconnect_event(msg, netdev);
   5578 		break;
   5579 	case NL80211_CMD_NEW_STATION:
   5580 		netdev_station_event(msg, netdev, true);
   5581 		break;
   5582 	case NL80211_CMD_DEL_STATION:
   5583 		netdev_station_event(msg, netdev, false);
   5584 		break;
   5585 	case NL80211_CMD_MICHAEL_MIC_FAILURE:
   5586 		netdev_michael_mic_failure(msg, netdev);
   5587 		break;
   5588 	case NL80211_CMD_ASSOC_COMEBACK:
   5589 		netdev_assoc_comeback(msg, netdev);
   5590 		break;
   5591 	}
   5592 }
   5593 
   5594 static void netdev_pae_destroy(void *user_data)
   5595 {
   5596 	struct netdev *netdev = user_data;
   5597 
   5598 	netdev->pae_io = NULL;
   5599 }
   5600 
   5601 static bool netdev_pae_read(struct l_io *io, void *user_data)
   5602 {
   5603 	int fd = l_io_get_fd(io);
   5604 	struct sockaddr_ll sll;
   5605 	socklen_t sll_len;
   5606 	ssize_t bytes;
   5607 	uint8_t frame[IEEE80211_MAX_DATA_LEN];
   5608 
   5609 	memset(&sll, 0, sizeof(sll));
   5610 	sll_len = sizeof(sll);
   5611 
   5612 	bytes = recvfrom(fd, frame, sizeof(frame), 0,
   5613 				(struct sockaddr *) &sll, &sll_len);
   5614 	if (bytes <= 0) {
   5615 		l_error("EAPoL read socket: %s", strerror(errno));
   5616 		return false;
   5617 	}
   5618 
   5619 	if (sll.sll_halen != ETH_ALEN)
   5620 		return true;
   5621 
   5622 	__eapol_rx_packet(sll.sll_ifindex, sll.sll_addr,
   5623 				ntohs(sll.sll_protocol), frame, bytes, false);
   5624 
   5625 	return true;
   5626 }
   5627 
   5628 static void netdev_control_port_frame_event(struct l_genl_msg *msg,
   5629 							struct netdev *netdev)
   5630 {
   5631 	struct l_genl_attr attr;
   5632 	uint16_t type;
   5633 	uint16_t len;
   5634 	const void *data;
   5635 	const uint8_t *frame = NULL;
   5636 	uint16_t frame_len = 0;
   5637 	const uint8_t *src = NULL;
   5638 	uint16_t proto = 0;
   5639 	bool unencrypted = false;
   5640 
   5641 	l_debug("");
   5642 
   5643 	if (!l_genl_attr_init(&attr, msg))
   5644 		return;
   5645 
   5646 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
   5647 		switch (type) {
   5648 		case NL80211_ATTR_FRAME:
   5649 			if (frame)
   5650 				return;
   5651 
   5652 			frame = data;
   5653 			frame_len = len;
   5654 			break;
   5655 		case NL80211_ATTR_MAC:
   5656 			if (src)
   5657 				return;
   5658 
   5659 			src = data;
   5660 			break;
   5661 		case NL80211_ATTR_CONTROL_PORT_ETHERTYPE:
   5662 			if (len != sizeof(proto))
   5663 				return;
   5664 
   5665 			proto = *((const uint16_t *) data);
   5666 			break;
   5667 		case NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT:
   5668 			unencrypted = true;
   5669 			break;
   5670 		}
   5671 	}
   5672 
   5673 	if (!src || !frame || !proto)
   5674 		return;
   5675 
   5676 	__eapol_rx_packet(netdev->index, src, proto,
   5677 						frame, frame_len, unencrypted);
   5678 }
   5679 
   5680 static void netdev_external_auth_event(struct l_genl_msg *msg,
   5681 							struct netdev *netdev)
   5682 {
   5683 	const uint8_t *bssid;
   5684 	struct iovec ssid;
   5685 	uint32_t akm;
   5686 	uint32_t action;
   5687 	struct handshake_state *hs = netdev->handshake;
   5688 
   5689 	if (L_WARN_ON(nl80211_parse_attrs(msg, NL80211_ATTR_AKM_SUITES, &akm,
   5690 				NL80211_ATTR_EXTERNAL_AUTH_ACTION, &action,
   5691 				NL80211_ATTR_BSSID, &bssid,
   5692 				NL80211_ATTR_SSID, &ssid,
   5693 				NL80211_ATTR_UNSPEC) < 0))
   5694 		return;
   5695 
   5696 	if (!L_IN_SET(action, NL80211_EXTERNAL_AUTH_START,
   5697 				NL80211_EXTERNAL_AUTH_ABORT))
   5698 		return;
   5699 
   5700 	/* kernel sends SAE_SHA256 AKM in BE order for legacy reasons */
   5701 	if (!L_IN_SET(akm, CRYPTO_AKM_SAE_SHA256, CRYPTO_AKM_FT_OVER_SAE_SHA256,
   5702 				L_CPU_TO_BE32(CRYPTO_AKM_SAE_SHA256))) {
   5703 		l_warn("Unknown AKM: %08x", akm);
   5704 		return;
   5705 	}
   5706 
   5707 	if (action == NL80211_EXTERNAL_AUTH_ABORT) {
   5708 		l_warn("External Auth Aborted");
   5709 		goto error;
   5710 	}
   5711 
   5712 	if (hs->ssid_len != ssid.iov_len ||
   5713 			memcmp(hs->ssid, ssid.iov_base, hs->ssid_len)) {
   5714 		l_warn("Target SSID mismatch");
   5715 		goto error;
   5716 	}
   5717 
   5718 	if (memcmp(hs->aa, bssid, ETH_ALEN)) {
   5719 		l_warn("Target BSSID mismatch");
   5720 		goto error;
   5721 	}
   5722 
   5723 	if (auth_proto_start(netdev->ap)) {
   5724 		netdev->external_auth = true;
   5725 		return;
   5726 	}
   5727 
   5728 error:
   5729 	netdev_send_external_auth(netdev, MMPDU_STATUS_CODE_UNSPECIFIED);
   5730 }
   5731 
   5732 static void netdev_unicast_notify(struct l_genl_msg *msg, void *user_data)
   5733 {
   5734 	struct netdev *netdev = NULL;
   5735 	struct l_genl_attr attr;
   5736 	uint16_t type, len;
   5737 	const void *data;
   5738 	uint8_t cmd;
   5739 
   5740 	cmd = l_genl_msg_get_command(msg);
   5741 	if (!cmd)
   5742 		return;
   5743 
   5744 	l_debug("Unicast notification %s(%u)", nl80211cmd_to_string(cmd), cmd);
   5745 
   5746 	if (!l_genl_attr_init(&attr, msg))
   5747 		return;
   5748 
   5749 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
   5750 		switch (type) {
   5751 		case NL80211_ATTR_IFINDEX:
   5752 			if (len != sizeof(uint32_t)) {
   5753 				l_warn("Invalid interface index attribute");
   5754 				return;
   5755 			}
   5756 
   5757 			netdev = netdev_find(*((uint32_t *) data));
   5758 			break;
   5759 		}
   5760 	}
   5761 
   5762 	if (!netdev)
   5763 		return;
   5764 
   5765 	switch (cmd) {
   5766 	case NL80211_CMD_CONTROL_PORT_FRAME:
   5767 		netdev_control_port_frame_event(msg, netdev);
   5768 		break;
   5769 	case NL80211_CMD_EXTERNAL_AUTH:
   5770 		netdev_external_auth_event(msg, netdev);
   5771 		break;
   5772 	}
   5773 }
   5774 
   5775 int netdev_set_rssi_report_levels(struct netdev *netdev, const int8_t *levels,
   5776 					size_t levels_num)
   5777 {
   5778 	struct l_genl_msg *cmd_set_cqm;
   5779 
   5780 	l_debug("ifindex: %d, num_levels: %zu", netdev->index, levels_num);
   5781 
   5782 	if (levels_num > L_ARRAY_SIZE(netdev->rssi_levels))
   5783 		return -ENOSPC;
   5784 
   5785 	if (!wiphy_has_ext_feature(netdev->wiphy,
   5786 					NL80211_EXT_FEATURE_CQM_RSSI_LIST))
   5787 		goto done;
   5788 
   5789 	cmd_set_cqm = netdev_build_cmd_cqm_rssi_update(netdev, levels,
   5790 							levels_num);
   5791 	if (!cmd_set_cqm)
   5792 		return -EINVAL;
   5793 
   5794 	netdev->set_cqm_cmd_id = l_genl_family_send(nl80211, cmd_set_cqm,
   5795 							netdev_cmd_set_cqm_cb,
   5796 							netdev, NULL);
   5797 	if (!netdev->set_cqm_cmd_id) {
   5798 		l_genl_msg_unref(cmd_set_cqm);
   5799 		return -EIO;
   5800 	}
   5801 
   5802 done:
   5803 	netdev->rssi_levels_num = levels_num;
   5804 
   5805 	if (levels_num) {
   5806 		memcpy(netdev->rssi_levels, levels, levels_num);
   5807 
   5808 		if (netdev->connected)
   5809 			netdev_set_rssi_level_idx(netdev);
   5810 	}
   5811 
   5812 	netdev_rssi_polling_update(netdev);
   5813 
   5814 	return 0;
   5815 }
   5816 
   5817 static void netdev_get_station_cb(struct l_genl_msg *msg, void *user_data)
   5818 {
   5819 	struct netdev *netdev = user_data;
   5820 	struct l_genl_attr attr, nested;
   5821 	uint16_t type, len;
   5822 	const void *data;
   5823 	struct diagnostic_station_info info;
   5824 
   5825 	netdev->get_station_cmd_id = 0;
   5826 
   5827 	if (!l_genl_attr_init(&attr, msg))
   5828 		goto parse_error;
   5829 
   5830 	memset(&info, 0, sizeof(info));
   5831 
   5832 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
   5833 		switch (type) {
   5834 		case NL80211_ATTR_STA_INFO:
   5835 			if (!l_genl_attr_recurse(&attr, &nested))
   5836 				goto parse_error;
   5837 
   5838 			if (!netdev_parse_sta_info(&nested, &info))
   5839 				goto parse_error;
   5840 
   5841 			break;
   5842 
   5843 		case NL80211_ATTR_MAC:
   5844 			if (len != 6)
   5845 				goto parse_error;
   5846 
   5847 			memcpy(info.addr, data, 6);
   5848 
   5849 			break;
   5850 		}
   5851 	}
   5852 
   5853 	if (netdev->get_station_cb)
   5854 		netdev->get_station_cb(&info, netdev->get_station_data);
   5855 
   5856 	return;
   5857 
   5858 parse_error:
   5859 	if (netdev->get_station_cb)
   5860 		netdev->get_station_cb(NULL, netdev->get_station_data);
   5861 }
   5862 
   5863 static void netdev_get_station_destroy(void *user_data)
   5864 {
   5865 	struct netdev *netdev = user_data;
   5866 
   5867 	netdev->get_station_cmd_id = 0;
   5868 
   5869 	if (netdev->get_station_destroy)
   5870 		netdev->get_station_destroy(netdev->get_station_data);
   5871 }
   5872 
   5873 int netdev_get_station(struct netdev *netdev, const uint8_t *mac,
   5874 			netdev_get_station_cb_t cb, void *user_data,
   5875 			netdev_destroy_func_t destroy)
   5876 {
   5877 	struct l_genl_msg *msg;
   5878 
   5879 	if (netdev->get_station_cmd_id)
   5880 		return -EBUSY;
   5881 
   5882 	msg = l_genl_msg_new_sized(NL80211_CMD_GET_STATION, 64);
   5883 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   5884 	l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, mac);
   5885 
   5886 	netdev->get_station_cmd_id = l_genl_family_send(nl80211, msg,
   5887 						netdev_get_station_cb, netdev,
   5888 						netdev_get_station_destroy);
   5889 	if (!netdev->get_station_cmd_id) {
   5890 		l_genl_msg_unref(msg);
   5891 		return -EIO;
   5892 	}
   5893 
   5894 	netdev->get_station_cb = cb;
   5895 	netdev->get_station_data = user_data;
   5896 	netdev->get_station_destroy = destroy;
   5897 
   5898 	return 0;
   5899 }
   5900 
   5901 int netdev_get_current_station(struct netdev *netdev,
   5902 			netdev_get_station_cb_t cb, void *user_data,
   5903 			netdev_destroy_func_t destroy)
   5904 {
   5905 	if (!netdev->handshake)
   5906 		return -ENOTCONN;
   5907 
   5908 	return netdev_get_station(netdev, netdev->handshake->aa, cb,
   5909 					user_data, destroy);
   5910 }
   5911 
   5912 int netdev_get_all_stations(struct netdev *netdev, netdev_get_station_cb_t cb,
   5913 				void *user_data, netdev_destroy_func_t destroy)
   5914 {
   5915 	struct l_genl_msg *msg;
   5916 
   5917 	if (netdev->get_station_cmd_id)
   5918 		return -EBUSY;
   5919 
   5920 	msg = l_genl_msg_new_sized(NL80211_CMD_GET_STATION, 64);
   5921 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   5922 
   5923 	netdev->get_station_cmd_id = l_genl_family_dump(nl80211, msg,
   5924 						netdev_get_station_cb, netdev,
   5925 						netdev_get_station_destroy);
   5926 	if (!netdev->get_station_cmd_id) {
   5927 		l_genl_msg_unref(msg);
   5928 		return -EIO;
   5929 	}
   5930 
   5931 	netdev->get_station_cb = cb;
   5932 	netdev->get_station_data = user_data;
   5933 	netdev->get_station_destroy = destroy;
   5934 
   5935 	return 0;
   5936 }
   5937 
   5938 static void netdev_add_station_frame_watches(struct netdev *netdev)
   5939 {
   5940 	static const uint8_t action_neighbor_report_prefix[2] = { 0x05, 0x05 };
   5941 	static const uint8_t action_sa_query_resp_prefix[2] = { 0x08, 0x01 };
   5942 	static const uint8_t action_sa_query_req_prefix[2] = { 0x08, 0x00 };
   5943 	static const uint8_t action_ft_response_prefix[] =  { 0x06, 0x02 };
   5944 	static const uint8_t auth_ft_response_prefix[] = { 0x02, 0x00 };
   5945 	static const uint8_t action_qos_map_prefix[] = { 0x01, 0x04 };
   5946 	static const uint8_t auth_sae_prefix[] = { 0x03, 0x00 };
   5947 	uint64_t wdev = netdev->wdev_id;
   5948 
   5949 	/* Subscribe to Management -> Action -> RM -> Neighbor Report frames */
   5950 	frame_watch_add(wdev, 0, 0x00d0, action_neighbor_report_prefix,
   5951 			sizeof(action_neighbor_report_prefix), false,
   5952 			netdev_neighbor_report_frame_event, netdev, NULL);
   5953 
   5954 	frame_watch_add(wdev, 0, 0x00d0, action_sa_query_resp_prefix,
   5955 			sizeof(action_sa_query_resp_prefix), false,
   5956 			netdev_sa_query_resp_frame_event, netdev, NULL);
   5957 
   5958 	frame_watch_add(wdev, 0, 0x00d0, action_sa_query_req_prefix,
   5959 			sizeof(action_sa_query_req_prefix), false,
   5960 			netdev_sa_query_req_frame_event, netdev, NULL);
   5961 
   5962 	frame_watch_add(wdev, 0, 0x00d0, action_ft_response_prefix,
   5963 			sizeof(action_ft_response_prefix), false,
   5964 			netdev_ft_response_frame_event, netdev, NULL);
   5965 
   5966 	frame_watch_add(wdev, 0, 0x00b0, auth_ft_response_prefix,
   5967 			sizeof(auth_ft_response_prefix), false,
   5968 			netdev_ft_auth_response_frame_event, netdev, NULL);
   5969 
   5970 	if (wiphy_supports_qos_set_map(netdev->wiphy))
   5971 		frame_watch_add(wdev, 0, 0x00d0, action_qos_map_prefix,
   5972 				sizeof(action_qos_map_prefix), false,
   5973 				netdev_qos_map_frame_event, netdev, NULL);
   5974 
   5975 	if (!wiphy_supports_cmds_auth_assoc(netdev->wiphy) &&
   5976 			wiphy_has_feature(netdev->wiphy, NL80211_FEATURE_SAE))
   5977 		frame_watch_add(wdev, 0, 0x00b0,
   5978 				auth_sae_prefix, sizeof(auth_sae_prefix),
   5979 				false, netdev_sae_external_auth_frame_event,
   5980 				netdev, NULL);
   5981 }
   5982 
   5983 static void netdev_setup_interface(struct netdev *netdev)
   5984 {
   5985 	switch (netdev->type) {
   5986 	case NL80211_IFTYPE_STATION:
   5987 		netdev_add_station_frame_watches(netdev);
   5988 		break;
   5989 	default:
   5990 		break;
   5991 	}
   5992 }
   5993 
   5994 static void netdev_set_interface_event(struct l_genl_msg *msg,
   5995 							struct netdev *netdev)
   5996 {
   5997 	uint32_t iftype;
   5998 	uint64_t wdev_id;
   5999 
   6000 	if (nl80211_parse_attrs(msg, NL80211_ATTR_IFTYPE, &iftype,
   6001 					NL80211_ATTR_WDEV, &wdev_id,
   6002 					NL80211_ATTR_UNSPEC) < 0)
   6003 		return;
   6004 
   6005 	if (iftype == netdev->type)
   6006 		return;
   6007 
   6008 	l_debug("Interface type changed from %s to %s",
   6009 			netdev_iftype_to_string(netdev->type),
   6010 			netdev_iftype_to_string(iftype));
   6011 	netdev->type = iftype;
   6012 	frame_watch_wdev_remove(wdev_id);
   6013 
   6014 	netdev_setup_interface(netdev);
   6015 
   6016 	WATCHLIST_NOTIFY(&netdev_watches, netdev_watch_func_t,
   6017 				netdev, NETDEV_WATCH_EVENT_IFTYPE_CHANGE);
   6018 }
   6019 
   6020 static void netdev_config_notify(struct l_genl_msg *msg, void *user_data)
   6021 {
   6022 	struct netdev *netdev;
   6023 
   6024 	netdev = netdev_from_message(msg);
   6025 	if (!netdev)
   6026 		return;
   6027 
   6028 	switch (l_genl_msg_get_command(msg)) {
   6029 	case NL80211_CMD_SET_INTERFACE:
   6030 		netdev_set_interface_event(msg, netdev);
   6031 		break;
   6032 	}
   6033 }
   6034 
   6035 static struct l_genl_msg *netdev_build_cmd_set_interface(struct netdev *netdev,
   6036 							uint32_t iftype)
   6037 {
   6038 	struct l_genl_msg *msg =
   6039 		l_genl_msg_new_sized(NL80211_CMD_SET_INTERFACE, 32);
   6040 
   6041 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   6042 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFTYPE, 4, &iftype);
   6043 
   6044 	return msg;
   6045 }
   6046 
   6047 struct netdev_set_iftype_request {
   6048 	netdev_command_cb_t cb;
   6049 	void *user_data;
   6050 	netdev_destroy_func_t destroy;
   6051 	uint32_t pending_type;
   6052 	uint32_t ref;
   6053 	struct netdev *netdev;
   6054 	bool bring_up;
   6055 };
   6056 
   6057 static void netdev_set_iftype_request_destroy(void *user_data)
   6058 {
   6059 	struct netdev_set_iftype_request *req = user_data;
   6060 	struct netdev *netdev = req->netdev;
   6061 
   6062 	req->ref--;
   6063 	if (req->ref)
   6064 		return;
   6065 
   6066 	netdev->set_powered_cmd_id = 0;
   6067 	netdev->set_interface_cmd_id = 0;
   6068 
   6069 	if (req->destroy)
   6070 		req->destroy(req->user_data);
   6071 
   6072 	l_free(req);
   6073 }
   6074 
   6075 static void netdev_set_iftype_up_cb(int error, uint16_t type,
   6076 					const void *data,
   6077 					uint32_t len, void *user_data)
   6078 {
   6079 	struct netdev_set_iftype_request *req = user_data;
   6080 	struct netdev *netdev = req->netdev;
   6081 
   6082 	if (req->cb)
   6083 		req->cb(netdev, error, req->user_data);
   6084 }
   6085 
   6086 static void netdev_set_iftype_cb(struct l_genl_msg *msg, void *user_data)
   6087 {
   6088 	struct netdev_set_iftype_request *req = user_data;
   6089 	struct netdev *netdev = req->netdev;
   6090 	int error = l_genl_msg_get_error(msg);
   6091 
   6092 	if (error != 0)
   6093 		goto done;
   6094 
   6095 	/* If the netdev was down originally, we're done */
   6096 	if (!req->bring_up)
   6097 		goto done;
   6098 
   6099 	netdev->set_powered_cmd_id =
   6100 			l_rtnl_set_powered(rtnl, netdev->index, true,
   6101 					netdev_set_iftype_up_cb, req,
   6102 					netdev_set_iftype_request_destroy);
   6103 	if (!netdev->set_powered_cmd_id) {
   6104 		error = -EIO;
   6105 		goto done;
   6106 	}
   6107 
   6108 	req->ref++;
   6109 	netdev->set_interface_cmd_id = 0;
   6110 	return;
   6111 
   6112 done:
   6113 	if (req->cb)
   6114 		req->cb(netdev, error, req->user_data);
   6115 }
   6116 
   6117 static void netdev_set_iftype_down_cb(int error, uint16_t type,
   6118 					const void *data,
   6119 					uint32_t len, void *user_data)
   6120 {
   6121 	struct netdev_set_iftype_request *req = user_data;
   6122 	struct netdev *netdev = req->netdev;
   6123 	struct l_genl_msg *msg;
   6124 
   6125 	if (error != 0)
   6126 		goto error;
   6127 
   6128 	msg = netdev_build_cmd_set_interface(netdev, req->pending_type);
   6129 	netdev->set_interface_cmd_id =
   6130 		l_genl_family_send(nl80211, msg, netdev_set_iftype_cb, req,
   6131 					netdev_set_iftype_request_destroy);
   6132 	if (!netdev->set_interface_cmd_id) {
   6133 		l_genl_msg_unref(msg);
   6134 		error = -EIO;
   6135 		goto error;
   6136 	}
   6137 
   6138 	req->ref++;
   6139 	netdev->set_powered_cmd_id = 0;
   6140 	return;
   6141 
   6142 error:
   6143 	if (req->cb)
   6144 		req->cb(netdev, error, req->user_data);
   6145 }
   6146 
   6147 int netdev_set_iftype(struct netdev *netdev, enum netdev_iftype type,
   6148 			netdev_command_cb_t cb, void *user_data,
   6149 			netdev_destroy_func_t destroy)
   6150 {
   6151 	uint32_t iftype;
   6152 	struct netdev_set_iftype_request *req;
   6153 
   6154 	switch (type) {
   6155 	case NETDEV_IFTYPE_AP:
   6156 		iftype = NL80211_IFTYPE_AP;
   6157 		break;
   6158 	case NETDEV_IFTYPE_ADHOC:
   6159 		iftype = NL80211_IFTYPE_ADHOC;
   6160 		break;
   6161 	case NETDEV_IFTYPE_STATION:
   6162 		iftype = NL80211_IFTYPE_STATION;
   6163 		break;
   6164 	default:
   6165 		l_error("unsupported iftype %u", type);
   6166 		return -EINVAL;
   6167 	}
   6168 
   6169 	if (netdev->set_powered_cmd_id ||
   6170 			netdev->set_interface_cmd_id)
   6171 		return -EBUSY;
   6172 
   6173 	req = l_new(struct netdev_set_iftype_request, 1);
   6174 	req->cb = cb;
   6175 	req->user_data = user_data;
   6176 	req->destroy = destroy;
   6177 	req->pending_type = iftype;
   6178 	req->netdev = netdev;
   6179 	req->ref = 1;
   6180 	req->bring_up = netdev_get_is_up(netdev);
   6181 
   6182 	if (!req->bring_up) {
   6183 		struct l_genl_msg *msg =
   6184 			netdev_build_cmd_set_interface(netdev, iftype);
   6185 
   6186 		netdev->set_interface_cmd_id =
   6187 			l_genl_family_send(nl80211, msg,
   6188 					netdev_set_iftype_cb, req,
   6189 					netdev_set_iftype_request_destroy);
   6190 		if (netdev->set_interface_cmd_id)
   6191 			return 0;
   6192 
   6193 		l_genl_msg_unref(msg);
   6194 	} else {
   6195 		netdev->set_powered_cmd_id =
   6196 			l_rtnl_set_powered(rtnl, netdev->index, false,
   6197 					netdev_set_iftype_down_cb, req,
   6198 					netdev_set_iftype_request_destroy);
   6199 		if (netdev->set_powered_cmd_id)
   6200 			return 0;
   6201 	}
   6202 
   6203 	l_free(req);
   6204 	return -EIO;
   6205 }
   6206 
   6207 static void netdev_bridge_port_event(const struct ifinfomsg *ifi, int bytes,
   6208 					bool added)
   6209 {
   6210 	struct netdev *netdev;
   6211 	struct rtattr *attr;
   6212 	uint32_t master = 0;
   6213 
   6214 	netdev = netdev_find(ifi->ifi_index);
   6215 	if (!netdev)
   6216 		return;
   6217 
   6218 	for (attr = IFLA_RTA(ifi); RTA_OK(attr, bytes);
   6219 			attr = RTA_NEXT(attr, bytes)) {
   6220 		switch (attr->rta_type) {
   6221 		case IFLA_MASTER:
   6222 			memcpy(&master, RTA_DATA(attr), sizeof(master));
   6223 			break;
   6224 		}
   6225 	}
   6226 
   6227 	l_debug("netdev: %d %s bridge: %d", ifi->ifi_index,
   6228 		(added ? "added to" : "removed from"), master);
   6229 }
   6230 
   6231 struct set_4addr_cb_data {
   6232 	struct netdev *netdev;
   6233 	bool value;
   6234 	netdev_command_cb_t callback;
   6235 	void *user_data;
   6236 	netdev_destroy_func_t destroy;
   6237 };
   6238 
   6239 static void netdev_set_4addr_cb(struct l_genl_msg *msg, void *user_data)
   6240 {
   6241 	struct set_4addr_cb_data *cb_data = user_data;
   6242 	int error = l_genl_msg_get_error(msg);
   6243 
   6244 	if (!cb_data)
   6245 		return;
   6246 
   6247 	/* cache the value that has just been set */
   6248 	if (!error)
   6249 		cb_data->netdev->use_4addr = cb_data->value;
   6250 
   6251 	cb_data->callback(cb_data->netdev, error, cb_data->user_data);
   6252 }
   6253 
   6254 static void netdev_set_4addr_destroy(void *user_data)
   6255 {
   6256 	struct set_4addr_cb_data *cb_data = user_data;
   6257 
   6258 	if (!cb_data)
   6259 		return;
   6260 
   6261 	if (cb_data->destroy)
   6262 		cb_data->destroy(cb_data->user_data);
   6263 
   6264 	l_free(cb_data);
   6265 }
   6266 
   6267 int netdev_set_4addr(struct netdev *netdev, bool use_4addr,
   6268 			netdev_command_cb_t cb, void *user_data,
   6269 			netdev_destroy_func_t destroy)
   6270 {
   6271 	struct set_4addr_cb_data *cb_data = NULL;
   6272 	uint8_t attr_4addr = (use_4addr ? 1 : 0);
   6273 	struct l_genl_msg *msg;
   6274 
   6275 	l_debug("netdev: %d use_4addr: %d", netdev->index, use_4addr);
   6276 
   6277 	msg = l_genl_msg_new_sized(NL80211_CMD_SET_INTERFACE, 32);
   6278 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   6279 	l_genl_msg_append_attr(msg, NL80211_ATTR_4ADDR, 1, &attr_4addr);
   6280 
   6281 	if (cb) {
   6282 		cb_data = l_new(struct set_4addr_cb_data, 1);
   6283 		cb_data->netdev = netdev;
   6284 		cb_data->value = use_4addr;
   6285 		cb_data->callback = cb;
   6286 		cb_data->user_data = user_data;
   6287 		cb_data->destroy = destroy;
   6288 	}
   6289 
   6290 	if (!l_genl_family_send(nl80211, msg, netdev_set_4addr_cb, cb_data,
   6291 				netdev_set_4addr_destroy)) {
   6292 		l_error("CMD_SET_INTERFACE (4addr) failed");
   6293 
   6294 		l_genl_msg_unref(msg);
   6295 		l_free(cb_data);
   6296 
   6297 		return -EIO;
   6298 	}
   6299 
   6300 	return 0;
   6301 }
   6302 
   6303 bool netdev_get_4addr(struct netdev *netdev)
   6304 {
   6305 	return netdev->use_4addr;
   6306 }
   6307 
   6308 static void netdev_newlink_notify(const struct ifinfomsg *ifi, int bytes)
   6309 {
   6310 	struct netdev *netdev;
   6311 	bool old_up, new_up;
   6312 	char old_name[IFNAMSIZ];
   6313 	uint8_t old_addr[ETH_ALEN];
   6314 	struct rtattr *attr;
   6315 	uint8_t *operstate = NULL;
   6316 	uint8_t *linkmode = NULL;
   6317 
   6318 	if (ifi->ifi_family == AF_BRIDGE) {
   6319 		netdev_bridge_port_event(ifi, bytes, true);
   6320 		return;
   6321 	}
   6322 
   6323 	netdev = netdev_find(ifi->ifi_index);
   6324 	if (!netdev)
   6325 		return;
   6326 
   6327 	old_up = netdev_get_is_up(netdev);
   6328 	strcpy(old_name, netdev->name);
   6329 	memcpy(old_addr, netdev->addr, ETH_ALEN);
   6330 
   6331 	netdev->ifi_flags = ifi->ifi_flags;
   6332 
   6333 	for (attr = IFLA_RTA(ifi); RTA_OK(attr, bytes);
   6334 			attr = RTA_NEXT(attr, bytes)) {
   6335 		switch (attr->rta_type) {
   6336 		case IFLA_IFNAME:
   6337 			strcpy(netdev->name, RTA_DATA(attr));
   6338 			break;
   6339 		case IFLA_ADDRESS:
   6340 			if (RTA_PAYLOAD(attr) < ETH_ALEN)
   6341 				break;
   6342 
   6343 			memcpy(netdev->addr, RTA_DATA(attr), ETH_ALEN);
   6344 			break;
   6345 		case IFLA_OPERSTATE:
   6346 			operstate = RTA_DATA(attr);
   6347 			break;
   6348 		case IFLA_LINKMODE:
   6349 			linkmode = RTA_DATA(attr);
   6350 			break;
   6351 		}
   6352 	}
   6353 
   6354 	if (!netdev->events_ready) /* Did we send NETDEV_WATCH_EVENT_NEW yet? */
   6355 		return;
   6356 
   6357 	/*
   6358 	 * Sometimes the driver sends the Association / Connect event on the
   6359 	 * nl80211 interface before the driver is ready to accept IF_OPER_UP
   6360 	 * setting on the rtnl interface.  This results in our initial
   6361 	 * IF_OPER_UP setting being ignored.  In this case the driver will
   6362 	 * send a New Link event with a stale OperState.  Detect this case and
   6363 	 * try to re-set IF_OPER_UP.
   6364 	 */
   6365 	if (linkmode && *linkmode == 1 &&
   6366 			operstate && *operstate == IF_OPER_DORMANT &&
   6367 			netdev->operational) {
   6368 		l_debug("Retrying setting OperState to IF_OPER_UP");
   6369 		l_rtnl_set_linkmode_and_operstate(rtnl, netdev->index,
   6370 					IF_LINK_MODE_DORMANT, IF_OPER_UP,
   6371 					netdev_operstate_cb,
   6372 					L_UINT_TO_PTR(netdev->index), NULL);
   6373 	}
   6374 
   6375 	new_up = netdev_get_is_up(netdev);
   6376 
   6377 	if (!new_up)
   6378 		netdev_connect_free(netdev);
   6379 
   6380 	/*
   6381 	 * If mac_change_cmd_id is set we are in the process of changing the
   6382 	 * MAC address and this event is a result of powering down/up. In this
   6383 	 * case we do not want to emit a netdev DOWN/UP event as this would
   6384 	 * cause other modules to behave as such. We do, however, want to emit
   6385 	 * address changes so other modules get the new MAC address updated.
   6386 	 */
   6387 	if (old_up != new_up && !netdev->mac_change_cmd_id)
   6388 		WATCHLIST_NOTIFY(&netdev_watches, netdev_watch_func_t,
   6389 				netdev, new_up ? NETDEV_WATCH_EVENT_UP :
   6390 						NETDEV_WATCH_EVENT_DOWN);
   6391 
   6392 	if (strcmp(old_name, netdev->name))
   6393 		WATCHLIST_NOTIFY(&netdev_watches, netdev_watch_func_t,
   6394 				netdev, NETDEV_WATCH_EVENT_NAME_CHANGE);
   6395 
   6396 	if (memcmp(old_addr, netdev->addr, ETH_ALEN))
   6397 		WATCHLIST_NOTIFY(&netdev_watches, netdev_watch_func_t,
   6398 				netdev, NETDEV_WATCH_EVENT_ADDRESS_CHANGE);
   6399 }
   6400 
   6401 static void netdev_dellink_notify(const struct ifinfomsg *ifi, int bytes)
   6402 {
   6403 	struct netdev *netdev;
   6404 
   6405 	if (ifi->ifi_family == AF_BRIDGE) {
   6406 		netdev_bridge_port_event(ifi, bytes, false);
   6407 		return;
   6408 	}
   6409 
   6410 	netdev = l_queue_remove_if(netdev_list, netdev_match,
   6411 						L_UINT_TO_PTR(ifi->ifi_index));
   6412 	if (!netdev)
   6413 		return;
   6414 
   6415 	netdev_free(netdev);
   6416 }
   6417 
   6418 static void netdev_disable_ps_cb(struct l_genl_msg *msg, void *user_data)
   6419 {
   6420 	struct netdev *netdev = user_data;
   6421 	int err = l_genl_msg_get_error(msg);
   6422 
   6423 	netdev->power_save_cmd_id = 0;
   6424 
   6425 	/* Can't do anything about it but inform the user */
   6426 	if (err < 0)
   6427 		l_error("Failed to disable power save for ifindex %u (%s: %d)",
   6428 				netdev->index, strerror(-err), err);
   6429 	else
   6430 		l_debug("Disabled power save for ifindex %u", netdev->index);
   6431 
   6432 	WATCHLIST_NOTIFY(&netdev_watches, netdev_watch_func_t,
   6433 				netdev, NETDEV_WATCH_EVENT_NEW);
   6434 	netdev->events_ready = true;
   6435 }
   6436 
   6437 static bool netdev_disable_power_save(struct netdev *netdev)
   6438 {
   6439 	struct l_genl_msg *msg = l_genl_msg_new(NL80211_CMD_SET_POWER_SAVE);
   6440 	uint32_t disabled = NL80211_PS_DISABLED;
   6441 
   6442 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &netdev->index);
   6443 	l_genl_msg_append_attr(msg, NL80211_ATTR_PS_STATE, 4, &disabled);
   6444 
   6445 	netdev->power_save_cmd_id = l_genl_family_send(nl80211, msg,
   6446 							netdev_disable_ps_cb,
   6447 							netdev, NULL);
   6448 	if (!netdev->power_save_cmd_id) {
   6449 		l_error("Failed to send SET_POWER_SAVE (-EIO)");
   6450 		return false;
   6451 	}
   6452 
   6453 	return true;
   6454 }
   6455 
   6456 static void netdev_initial_up_cb(int error, uint16_t type, const void *data,
   6457 					uint32_t len, void *user_data)
   6458 {
   6459 	struct netdev *netdev = user_data;
   6460 
   6461 	netdev->set_powered_cmd_id = 0;
   6462 
   6463 	if (!error)
   6464 		netdev->ifi_flags |= IFF_UP;
   6465 	else {
   6466 		l_error("Error bringing interface %i up: %s", netdev->index,
   6467 			strerror(-error));
   6468 
   6469 		if (error != -ERFKILL)
   6470 			return;
   6471 	}
   6472 
   6473 	l_rtnl_set_linkmode_and_operstate(rtnl, netdev->index,
   6474 					IF_LINK_MODE_DORMANT, IF_OPER_DOWN,
   6475 					netdev_operstate_cb,
   6476 					L_UINT_TO_PTR(netdev->index), NULL);
   6477 
   6478 	/*
   6479 	 * we don't know the initial status of the 4addr property on this
   6480 	 * netdev, therefore we set it to zero by default.
   6481 	 */
   6482 	netdev_set_4addr(netdev, netdev->use_4addr, NULL, NULL, NULL);
   6483 
   6484 	l_debug("Interface %i initialized", netdev->index);
   6485 
   6486 	scan_wdev_add(netdev->wdev_id);
   6487 
   6488 	if (wiphy_power_save_disabled(netdev->wiphy)) {
   6489 		/* Wait to issue EVENT_NEW until power save is disabled */
   6490 		if (netdev_disable_power_save(netdev))
   6491 			return;
   6492 	}
   6493 
   6494 	WATCHLIST_NOTIFY(&netdev_watches, netdev_watch_func_t,
   6495 				netdev, NETDEV_WATCH_EVENT_NEW);
   6496 	netdev->events_ready = true;
   6497 }
   6498 
   6499 static bool netdev_check_set_mac(struct netdev *netdev)
   6500 {
   6501 	if (l_memeqzero(netdev->set_mac_once, 6))
   6502 		return false;
   6503 
   6504 	l_debug("Setting initial address on ifindex: %d to: " MAC,
   6505 		netdev->index, MAC_STR(netdev->set_mac_once));
   6506 	netdev->set_powered_cmd_id =
   6507 		l_rtnl_set_mac(rtnl, netdev->index, netdev->set_mac_once, true,
   6508 				netdev_initial_up_cb, netdev, NULL);
   6509 	memset(netdev->set_mac_once, 0, 6);
   6510 	return true;
   6511 }
   6512 
   6513 static void netdev_initial_down_cb(int error, uint16_t type, const void *data,
   6514 					uint32_t len, void *user_data)
   6515 {
   6516 	struct netdev *netdev = user_data;
   6517 
   6518 	if (!error)
   6519 		netdev->ifi_flags &= ~IFF_UP;
   6520 	else {
   6521 		l_error("Error taking interface %i down: %s", netdev->index,
   6522 			strerror(-error));
   6523 
   6524 		netdev->set_powered_cmd_id = 0;
   6525 		return;
   6526 	}
   6527 
   6528 	if (netdev_check_set_mac(netdev))
   6529 		return;
   6530 
   6531 	netdev->set_powered_cmd_id =
   6532 		l_rtnl_set_powered(rtnl, netdev->index, true,
   6533 					netdev_initial_up_cb, netdev, NULL);
   6534 }
   6535 
   6536 static void netdev_getlink_cb(int error, uint16_t type, const void *data,
   6537 			uint32_t len, void *user_data)
   6538 {
   6539 	struct netdev *netdev = user_data;
   6540 	const struct ifinfomsg *ifi = data;
   6541 	unsigned int bytes;
   6542 	l_netlink_command_func_t cb;
   6543 	bool powered;
   6544 
   6545 	netdev->get_link_cmd_id = 0;
   6546 
   6547 	if (error != 0) {
   6548 		l_error("RTM_GETLINK error %i: %s", error, strerror(-error));
   6549 		return;
   6550 	}
   6551 
   6552 	if (ifi->ifi_type != ARPHRD_ETHER || type != RTM_NEWLINK) {
   6553 		l_debug("Non-ethernet address or not newlink message -- "
   6554 			"ifi_type: %i, type: %i", ifi->ifi_type, type);
   6555 		return;
   6556 	}
   6557 
   6558 	if (L_WARN_ON((uint32_t)ifi->ifi_index != netdev->index))
   6559 		return;
   6560 
   6561 	bytes = len - NLMSG_ALIGN(sizeof(struct ifinfomsg));
   6562 
   6563 	netdev_newlink_notify(ifi, bytes);
   6564 
   6565 	/*
   6566 	 * If the interface is UP, reset it to ensure a clean state.
   6567 	 * Otherwise, if we need to set a random mac, do so.  If not, just
   6568 	 * bring the interface UP.
   6569 	 */
   6570 	powered = netdev_get_is_up(netdev);
   6571 
   6572 	if (!powered && netdev_check_set_mac(netdev))
   6573 		return;
   6574 
   6575 	cb = powered ? netdev_initial_down_cb : netdev_initial_up_cb;
   6576 
   6577 	netdev->set_powered_cmd_id =
   6578 		l_rtnl_set_powered(rtnl, ifi->ifi_index, !powered, cb, netdev,
   6579 					NULL);
   6580 }
   6581 
   6582 static struct l_io *pae_open(uint32_t ifindex)
   6583 {
   6584 	/*
   6585 	 * BPF filter to match skb->dev->type == 1 (ARPHRD_ETHER) and
   6586 	 * match skb->protocol == 0x888e (PAE) or 0x88c7 (preauthentication).
   6587 	 */
   6588 	struct sock_filter pae_filter[] = {
   6589 		{ 0x20,  0,  0, 0xfffff008 },	/* ld #ifidx		*/
   6590 		{ 0x15,  0,  6, 0x00000000 },	/* jne #0, drop		*/
   6591 		{ 0x28,  0,  0, 0xfffff01c },	/* ldh #hatype		*/
   6592 		{ 0x15,  0,  4, 0x00000001 },	/* jne #1, drop		*/
   6593 		{ 0x28,  0,  0, 0xfffff000 },	/* ldh #proto		*/
   6594 		{ 0x15,  1,  0, 0x0000888e },	/* je  #0x888e, keep	*/
   6595 		{ 0x15,  0,  1, 0x000088c7 },	/* jne #0x88c7, drop	*/
   6596 		{ 0x06,  0,  0, 0xffffffff },	/* keep: ret #-1	*/
   6597 		{ 0x06,  0,  0, 0000000000 },	/* drop: ret #0		*/
   6598 	};
   6599 
   6600 	const struct sock_fprog pae_fprog = {
   6601 		.len = L_ARRAY_SIZE(pae_filter),
   6602 		.filter = pae_filter
   6603 	};
   6604 
   6605 	struct l_io *io;
   6606 	int fd;
   6607 
   6608 	fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
   6609 							htons(ETH_P_ALL));
   6610 	if (fd < 0)
   6611 		return NULL;
   6612 
   6613 	/*
   6614 	 * Here we modify the k value in the BPF program above to match the
   6615 	 * given ifindex.  We do it this way instead of using bind to attach
   6616 	 * to a specific interface index to avoid having to re-open the fd
   6617 	 * whenever the device is powered down / up
   6618 	 */
   6619 
   6620 	pae_filter[1].k = ifindex;
   6621 
   6622 	if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER,
   6623 					&pae_fprog, sizeof(pae_fprog)) < 0)
   6624 		goto error;
   6625 
   6626 	io = l_io_new(fd);
   6627 	l_io_set_close_on_destroy(io, true);
   6628 
   6629 	return io;
   6630 
   6631 error:
   6632 	close(fd);
   6633 	return NULL;
   6634 }
   6635 
   6636 static void netdev_get_link(struct netdev *netdev)
   6637 {
   6638 	struct ifinfomsg ifi;
   6639 	struct l_netlink_message *nlm =
   6640 		l_netlink_message_new_sized(RTM_GETLINK, 0, sizeof(ifi));
   6641 
   6642 	memset(&ifi, 0, sizeof(ifi));
   6643 	ifi.ifi_family = AF_UNSPEC;
   6644 	ifi.ifi_index = netdev->index;
   6645 
   6646 	l_netlink_message_add_header(nlm, &ifi, sizeof(ifi));
   6647 
   6648 	netdev->get_link_cmd_id = l_netlink_send(rtnl, nlm, netdev_getlink_cb,
   6649 						netdev, NULL);
   6650 	L_WARN_ON(netdev->get_link_cmd_id == 0);
   6651 }
   6652 
   6653 struct netdev *netdev_create_from_genl(struct l_genl_msg *msg,
   6654 					const uint8_t *set_mac)
   6655 {
   6656 	const char *ifname;
   6657 	const uint8_t *ifaddr;
   6658 	uint32_t ifindex;
   6659 	uint32_t iftype;
   6660 	uint64_t wdev;
   6661 	uint32_t wiphy_id;
   6662 	struct netdev *netdev;
   6663 	struct wiphy *wiphy = NULL;
   6664 	struct l_io *pae_io = NULL;
   6665 
   6666 	if (nl80211_parse_attrs(msg, NL80211_ATTR_IFINDEX, &ifindex,
   6667 					NL80211_ATTR_WDEV, &wdev,
   6668 					NL80211_ATTR_IFNAME, &ifname,
   6669 					NL80211_ATTR_WIPHY, &wiphy_id,
   6670 					NL80211_ATTR_IFTYPE, &iftype,
   6671 					NL80211_ATTR_MAC, &ifaddr,
   6672 					NL80211_ATTR_UNSPEC) < 0) {
   6673 		l_warn("Required attributes missing");
   6674 		return NULL;
   6675 	}
   6676 
   6677 	wiphy = wiphy_find(wiphy_id);
   6678 	if (!wiphy) {
   6679 		l_warn("No wiphy: %d", wiphy_id);
   6680 		return NULL;
   6681 	}
   6682 
   6683 	if (netdev_find(ifindex)) {
   6684 		l_debug("Skipping duplicate netdev %s[%d]", ifname, ifindex);
   6685 		return NULL;
   6686 	}
   6687 
   6688 	if (!wiphy_control_port_enabled(wiphy)) {
   6689 		pae_io = pae_open(ifindex);
   6690 		if (!pae_io) {
   6691 			l_error("Unable to open PAE interface");
   6692 			return NULL;
   6693 		}
   6694 	}
   6695 
   6696 	netdev = l_new(struct netdev, 1);
   6697 	netdev->index = ifindex;
   6698 	netdev->wdev_id = wdev;
   6699 	netdev->type = iftype;
   6700 	netdev->rekey_offload_support = true;
   6701 	memcpy(netdev->addr, ifaddr, sizeof(netdev->addr));
   6702 	l_strlcpy(netdev->name, ifname, IFNAMSIZ);
   6703 	netdev->wiphy = wiphy;
   6704 	netdev->pae_over_nl80211 = pae_io == NULL;
   6705 	netdev->low_signal_threshold = LOW_SIGNAL_THRESHOLD;
   6706 	netdev->low_signal_threshold_5ghz = LOW_SIGNAL_THRESHOLD_5GHZ;
   6707 
   6708 	if (set_mac)
   6709 		memcpy(netdev->set_mac_once, set_mac, 6);
   6710 
   6711 	if (pae_io) {
   6712 		netdev->pae_io = pae_io;
   6713 		l_io_set_read_handler(netdev->pae_io, netdev_pae_read, netdev,
   6714 							netdev_pae_destroy);
   6715 	}
   6716 
   6717 	watchlist_init(&netdev->station_watches, NULL);
   6718 
   6719 	l_queue_push_tail(netdev_list, netdev);
   6720 
   6721 	l_debug("Created interface %s[%d %" PRIx64 "]", netdev->name,
   6722 		netdev->index, netdev->wdev_id);
   6723 
   6724 	netdev_setup_interface(netdev);
   6725 
   6726 	netdev_get_link(netdev);
   6727 
   6728 	/*
   6729 	 * Call the netdev-specific variant to flush only this devices PMKSA
   6730 	 * cache in the kernel. This will make IWD's cache and the kernel's
   6731 	 * cache consistent, i.e. no entries
   6732 	 *
   6733 	 * TODO: If we ever are storing PMKSA's on disk we would first need to
   6734 	 *       flush, then add all the PMKSA entries at this time.
   6735 	 */
   6736 	netdev_flush_pmksa(netdev);
   6737 
   6738 	return netdev;
   6739 }
   6740 
   6741 bool netdev_destroy(struct netdev *netdev)
   6742 {
   6743 	if (!l_queue_remove(netdev_list, netdev))
   6744 		return false;
   6745 
   6746 	netdev_free(netdev);
   6747 	return true;
   6748 }
   6749 
   6750 static void netdev_link_notify(uint16_t type, const void *data, uint32_t len,
   6751 							void *user_data)
   6752 {
   6753 	const struct ifinfomsg *ifi = data;
   6754 	unsigned int bytes;
   6755 
   6756 	if (ifi->ifi_type != ARPHRD_ETHER)
   6757 		return;
   6758 
   6759 	l_debug("event %u on ifindex %u", type, ifi->ifi_index);
   6760 
   6761 	bytes = len - NLMSG_ALIGN(sizeof(struct ifinfomsg));
   6762 
   6763 	switch (type) {
   6764 	case RTM_NEWLINK:
   6765 		netdev_newlink_notify(ifi, bytes);
   6766 		break;
   6767 	case RTM_DELLINK:
   6768 		netdev_dellink_notify(ifi, bytes);
   6769 		break;
   6770 	}
   6771 }
   6772 
   6773 uint32_t netdev_station_watch_add(struct netdev *netdev,
   6774 			netdev_station_watch_func_t func, void *user_data)
   6775 {
   6776 	return watchlist_add(&netdev->station_watches, func, user_data, NULL);
   6777 }
   6778 
   6779 bool netdev_station_watch_remove(struct netdev *netdev, uint32_t id)
   6780 {
   6781 	return watchlist_remove(&netdev->station_watches, id);
   6782 }
   6783 
   6784 uint32_t netdev_watch_add(netdev_watch_func_t func,
   6785 				void *user_data, netdev_destroy_func_t destroy)
   6786 {
   6787 	return watchlist_add(&netdev_watches, func, user_data, destroy);
   6788 }
   6789 
   6790 bool netdev_watch_remove(uint32_t id)
   6791 {
   6792 	return watchlist_remove(&netdev_watches, id);
   6793 }
   6794 
   6795 static int netdev_init(void)
   6796 {
   6797 	struct l_genl *genl = iwd_get_genl();
   6798 	const struct l_settings *settings = iwd_get_config();
   6799 	const char *rand_addr_str;
   6800 
   6801 	if (rtnl)
   6802 		return -EALREADY;
   6803 
   6804 	rtnl = iwd_get_rtnl();
   6805 
   6806 	if (!l_netlink_register(rtnl, RTNLGRP_LINK,
   6807 				netdev_link_notify, NULL, NULL)) {
   6808 		l_error("Failed to register for RTNL link notifications");
   6809 		goto fail_netlink;
   6810 	}
   6811 
   6812 	nl80211 = l_genl_family_new(genl, NL80211_GENL_NAME);
   6813 	if (!nl80211) {
   6814 		l_error("Failed to obtain nl80211");
   6815 		goto fail_netlink;
   6816 	}
   6817 
   6818 	if (!l_settings_get_int(settings, NETDEV_ROAM_THRESHOLD,
   6819 					&LOW_SIGNAL_THRESHOLD))
   6820 		LOW_SIGNAL_THRESHOLD = -70;
   6821 
   6822 	if (!l_settings_get_int(settings, NETDEV_ROAM_THRESHOLD_5G,
   6823 					&LOW_SIGNAL_THRESHOLD_5GHZ))
   6824 		LOW_SIGNAL_THRESHOLD_5GHZ = -76;
   6825 
   6826 	if (!l_settings_get_int(settings, NETDEV_CRITICAL_ROAM_THRESHOLD,
   6827 					&CRITICAL_SIGNAL_THRESHOLD))
   6828 		CRITICAL_SIGNAL_THRESHOLD = -80;
   6829 
   6830 	if (!l_settings_get_int(settings, NETDEV_CRITICAL_ROAM_THRESHOLD_5G,
   6831 					&CRITICAL_SIGNAL_THRESHOLD_5GHZ))
   6832 		CRITICAL_SIGNAL_THRESHOLD_5GHZ = -82;
   6833 
   6834 	rand_addr_str = l_settings_get_value(settings,
   6835 						NETDEV_ADDRESS_RANDOMIZATION);
   6836 	if (rand_addr_str && !strcmp(rand_addr_str, "network"))
   6837 		mac_per_ssid = true;
   6838 
   6839 	watchlist_init(&netdev_watches, NULL);
   6840 	netdev_list = l_queue_new();
   6841 
   6842 	__handshake_set_install_tk_func(netdev_set_tk);
   6843 	__handshake_set_install_gtk_func(netdev_set_gtk);
   6844 	__handshake_set_install_igtk_func(netdev_set_igtk);
   6845 	__handshake_set_install_ext_tk_func(netdev_set_ext_tk);
   6846 
   6847 	__eapol_set_rekey_offload_func(netdev_set_rekey_offload);
   6848 	__eapol_set_tx_packet_func(netdev_control_port_frame);
   6849 	__eapol_set_install_pmk_func(netdev_set_pmk);
   6850 
   6851 	__ft_set_tx_frame_func(netdev_tx_ft_frame);
   6852 
   6853 	__pmksa_set_driver_callbacks(netdev_pmksa_driver_add,
   6854 					netdev_pmksa_driver_remove,
   6855 					netdev_pmksa_driver_flush);
   6856 
   6857 	unicast_watch = l_genl_add_unicast_watch(genl, NL80211_GENL_NAME,
   6858 						netdev_unicast_notify,
   6859 						NULL, NULL);
   6860 	if (!unicast_watch)
   6861 		l_error("Registering for unicast notification failed");
   6862 
   6863 	if (!l_genl_family_register(nl80211, "mlme", netdev_mlme_notify,
   6864 								NULL, NULL))
   6865 		l_error("Registering for MLME notification failed");
   6866 
   6867 	if (!l_genl_family_register(nl80211, "scan", netdev_scan_notify,
   6868 								NULL, NULL))
   6869 		l_error("Registering for scan notifications failed");
   6870 
   6871 	if (!l_genl_family_register(nl80211, "config", netdev_config_notify,
   6872 								NULL, NULL))
   6873 		l_error("Registering for config notifications failed");
   6874 
   6875 	return 0;
   6876 
   6877 fail_netlink:
   6878 	rtnl = NULL;
   6879 
   6880 	return -EIO;
   6881 }
   6882 
   6883 static void netdev_exit(void)
   6884 {
   6885 	struct l_genl *genl = iwd_get_genl();
   6886 
   6887 	if (!rtnl)
   6888 		return;
   6889 
   6890 	l_genl_remove_unicast_watch(genl, unicast_watch);
   6891 
   6892 	watchlist_destroy(&netdev_watches);
   6893 	l_queue_destroy(netdev_list, netdev_free);
   6894 	netdev_list = NULL;
   6895 
   6896 	l_genl_family_free(nl80211);
   6897 	nl80211 = NULL;
   6898 
   6899 	rtnl = NULL;
   6900 }
   6901 
   6902 void netdev_shutdown(void)
   6903 {
   6904 	struct netdev *netdev;
   6905 
   6906 	if (!rtnl)
   6907 		return;
   6908 
   6909 	l_queue_foreach(netdev_list, netdev_shutdown_one, NULL);
   6910 
   6911 	while ((netdev = l_queue_peek_head(netdev_list))) {
   6912 		netdev_free(netdev);
   6913 		l_queue_pop_head(netdev_list);
   6914 	}
   6915 }
   6916 
   6917 IWD_MODULE(netdev, netdev_init, netdev_exit);
   6918 IWD_MODULE_DEPENDS(netdev, eapol);
   6919 IWD_MODULE_DEPENDS(netdev, frame_xchg);
   6920 IWD_MODULE_DEPENDS(netdev, wiphy);
   6921 IWD_MODULE_DEPENDS(netdev, ft);