eiwd

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

wiphy.c (69445B)


      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 <stdio.h>
     30 #include <errno.h>
     31 #include <linux/if_ether.h>
     32 #include <fnmatch.h>
     33 #include <unistd.h>
     34 #include <string.h>
     35 #include <limits.h>
     36 
     37 #include <ell/ell.h>
     38 
     39 #include "linux/nl80211.h"
     40 
     41 #include "ell/useful.h"
     42 #include "src/missing.h"
     43 #include "src/iwd.h"
     44 #include "src/module.h"
     45 #include "src/ie.h"
     46 #include "src/crypto.h"
     47 #include "src/scan.h"
     48 #include "src/netdev.h"
     49 #include "src/dbus.h"
     50 #include "src/rfkill.h"
     51 #include "src/wiphy.h"
     52 #include "src/storage.h"
     53 #include "src/util.h"
     54 #include "src/common.h"
     55 #include "src/watchlist.h"
     56 #include "src/nl80211util.h"
     57 #include "src/nl80211cmd.h"
     58 #include "src/band.h"
     59 
     60 #define EXT_CAP_LEN 10
     61 
     62 static struct l_genl_family *nl80211 = NULL;
     63 static struct l_hwdb *hwdb;
     64 static char **whitelist_filter;
     65 static char **blacklist_filter;
     66 static int mac_randomize_bytes = 6;
     67 static char regdom_country[2];
     68 static uint32_t work_ids;
     69 static unsigned int wiphy_dump_id;
     70 
     71 enum driver_flag {
     72 	/* Force the use of the default interface created by the kernel */
     73 	DEFAULT_IF = 0x1,
     74 	/*
     75 	 * Force the use of the PAE socket rather than control port, even if
     76 	 * control port is supported
     77 	 */
     78 	FORCE_PAE = 0x2,
     79 	/* Disable power save on the adapter during initialization */
     80 	POWER_SAVE_DISABLE = 0x4,
     81 	/* Don't use OWE when connecting to open networks */
     82 	OWE_DISABLE = 0x8,
     83 	/* Disables multicast RX frame registration */
     84 	MULTICAST_RX_DISABLE = 0x10,
     85 	/*
     86 	 * Don't use SAE (WPA3) when connecting to hybrid networks. This will
     87 	 * prevent IWD from connecting to WPA3-only networks
     88 	 */
     89 	SAE_DISABLE = 0x20,
     90 	/* Disables use of the NL80211_SCAN_FLAG_COLOCATED_6GHZ flag in scans */
     91 	COLOCATED_SCAN_DISABLE = 0x40,
     92 };
     93 
     94 struct driver_flag_name {
     95 	const char *name;
     96 	enum driver_flag flag;
     97 };
     98 
     99 struct driver_info {
    100 	const char *prefix;
    101 	unsigned int flags;
    102 };
    103 
    104 /*
    105  * The out-of-tree rtl88x2bu crashes the kernel hard if default interface is
    106  * destroyed.  It seems many other drivers are built from the same source code
    107  * so we set the DEFAULT_IF flag for all of them.  Unfortunately there are
    108  * in-tree drivers that also match these names and may be fine.
    109  */
    110 static const struct driver_info driver_infos[] = {
    111 	{ "rtl81*",          DEFAULT_IF },
    112 	{ "rtl87*",          DEFAULT_IF },
    113 	{ "rtl88*",          DEFAULT_IF },
    114 	{ "rtw_*",           DEFAULT_IF },
    115 	{ "brcmfmac",        DEFAULT_IF },
    116 	{ "bcmsdh_sdmmc",    DEFAULT_IF },
    117 };
    118 
    119 static const struct driver_flag_name driver_flag_names[] = {
    120 	{ "DefaultInterface",     DEFAULT_IF },
    121 	{ "ForcePae",             FORCE_PAE },
    122 	{ "PowerSaveDisable",     POWER_SAVE_DISABLE },
    123 	{ "OweDisable",           OWE_DISABLE },
    124 	{ "MulticastRxDisable",   MULTICAST_RX_DISABLE },
    125 	{ "SaeDisable",           SAE_DISABLE },
    126 	{ "ColocatedScanDisable", COLOCATED_SCAN_DISABLE },
    127 };
    128 
    129 struct wiphy {
    130 	uint32_t id;
    131 	char name[20];
    132 	uint8_t permanent_addr[ETH_ALEN];
    133 	uint32_t feature_flags;
    134 	uint8_t ext_features[(NUM_NL80211_EXT_FEATURES + 7) / 8];
    135 	uint8_t max_num_ssids_per_scan;
    136 	uint32_t max_roc_duration;
    137 	uint16_t max_scan_ie_len;
    138 	uint16_t supported_iftypes;
    139 	uint16_t supported_ciphers;
    140 	struct scan_freq_set *supported_freqs;
    141 	struct band *band_2g;
    142 	struct band *band_5g;
    143 	struct band *band_6g;
    144 	char *model_str;
    145 	char *vendor_str;
    146 	char *driver_str;
    147 	uint32_t driver_flags;
    148 	struct watchlist state_watches;
    149 	uint8_t extended_capabilities[EXT_CAP_LEN + 2]; /* max bitmap size + IE header */
    150 	uint8_t *iftype_extended_capabilities[NUM_NL80211_IFTYPES];
    151 	uint8_t rm_enabled_capabilities[7]; /* 5 size max + header */
    152 	struct l_genl_family *nl80211;
    153 	char regdom_country[2];
    154 	/* Work queue for this radio */
    155 	struct l_queue *work;
    156 	bool work_in_callback;
    157 	unsigned int get_reg_id;
    158 	unsigned int dump_id;
    159 
    160 	bool support_scheduled_scan:1;
    161 	bool support_rekey_offload:1;
    162 	bool support_qos_set_map:1;
    163 	bool support_cmds_auth_assoc:1;
    164 	bool support_fw_roam:1;
    165 	bool soft_rfkill : 1;
    166 	bool hard_rfkill : 1;
    167 	bool offchannel_tx_ok : 1;
    168 	bool blacklisted : 1;
    169 	bool registered : 1;
    170 	bool self_managed : 1;
    171 	bool ap_probe_resp_offload : 1;
    172 	bool supports_uapsd : 1;
    173 	bool supports_cmd_offchannel : 1;
    174 };
    175 
    176 static struct l_queue *wiphy_list = NULL;
    177 
    178 enum ie_rsn_cipher_suite wiphy_select_cipher(struct wiphy *wiphy, uint16_t mask)
    179 {
    180 	if (mask == IE_RSN_CIPHER_SUITE_NO_GROUP_TRAFFIC)
    181 		return IE_RSN_CIPHER_SUITE_NO_GROUP_TRAFFIC;
    182 
    183 	mask &= wiphy->supported_ciphers;
    184 
    185 	if (mask & IE_RSN_CIPHER_SUITE_GCMP_256)
    186 		return IE_RSN_CIPHER_SUITE_GCMP_256;
    187 
    188 	if (mask & IE_RSN_CIPHER_SUITE_CCMP_256)
    189 		return IE_RSN_CIPHER_SUITE_CCMP_256;
    190 
    191 	if (mask & IE_RSN_CIPHER_SUITE_GCMP)
    192 		return IE_RSN_CIPHER_SUITE_GCMP;
    193 
    194 	if (mask & IE_RSN_CIPHER_SUITE_CCMP)
    195 		return IE_RSN_CIPHER_SUITE_CCMP;
    196 
    197 	if (mask & IE_RSN_CIPHER_SUITE_TKIP)
    198 		return IE_RSN_CIPHER_SUITE_TKIP;
    199 
    200 	if (mask & IE_RSN_CIPHER_SUITE_BIP_GMAC_256)
    201 		return IE_RSN_CIPHER_SUITE_BIP_GMAC_256;
    202 
    203 	if (mask & IE_RSN_CIPHER_SUITE_BIP_CMAC_256)
    204 		return IE_RSN_CIPHER_SUITE_BIP_CMAC_256;
    205 
    206 	if (mask & IE_RSN_CIPHER_SUITE_BIP_GMAC)
    207 		return IE_RSN_CIPHER_SUITE_BIP_GMAC;
    208 
    209 	if (mask & IE_RSN_CIPHER_SUITE_BIP_CMAC)
    210 		return IE_RSN_CIPHER_SUITE_BIP_CMAC;
    211 
    212 	return 0;
    213 }
    214 
    215 uint16_t wiphy_get_supported_ciphers(struct wiphy *wiphy, uint16_t mask)
    216 {
    217 	return wiphy->supported_ciphers & mask;
    218 }
    219 
    220 static bool wiphy_can_connect_sae(struct wiphy *wiphy)
    221 {
    222 	if (wiphy->driver_flags & SAE_DISABLE)
    223 		return false;
    224 
    225 	/*
    226 	 * WPA3 Specification version 3, Section 2.2:
    227 	 * A STA shall not enable WEP and TKIP
    228 	 */
    229 	if (!(wiphy->supported_ciphers & IE_RSN_CIPHER_SUITE_CCMP)) {
    230 		l_debug("HW not CCMP capable, can't use SAE");
    231 		return false;
    232 	}
    233 
    234 	/*
    235 	 * WPA3 Specification version 3, Section 2.3:
    236 	 * A STA shall negotiate PMF when associating to an AP using SAE
    237 	 */
    238 	if (!(wiphy->supported_ciphers & IE_RSN_CIPHER_SUITE_BIP_CMAC)) {
    239 		l_debug("HW not MFP capable, can't use SAE");
    240 		return false;
    241 	}
    242 
    243 	/*
    244 	 * SAE support in the kernel is a complete mess in that there are 3
    245 	 * different ways the hardware can support SAE:
    246 	 *
    247 	 * 1. Cards which allow SAE in userspace, meaning they support both
    248 	 *    CMD_AUTHENTICATE and CMD_ASSOCIATE as well as advertise support
    249 	 *    for FEATURE_SAE (SoftMAC).
    250 	 *
    251 	 * 2. Cards which allow SAE to be offloaded to hardware. These cards
    252 	 *    do not support AUTH/ASSOC commands, do not advertise FEATURE_SAE,
    253 	 *    but advertise support for EXT_FEATURE_SAE_OFFLOAD. With these
    254 	 *    cards the entire SAE protocol as well as the subsequent 4-way
    255 	 *    handshake are all done in the driver/firmware (fullMAC).
    256 	 *
    257 	 * 3. Cards which allow SAE in userspace via CMD_EXTERNAL_AUTH.
    258 	 *    These cards do not support AUTH/ASSOC commands but do implement
    259 	 *    CMD_EXTERNAL_AUTH which is supposed to allow userspace to
    260 	 *    generate Authenticate frames as it would for case (1).
    261 	 */
    262 	if (wiphy_has_feature(wiphy, NL80211_FEATURE_SAE)) {
    263 		/* Case (1) */
    264 		if (wiphy->support_cmds_auth_assoc)
    265 			return true;
    266 
    267 		/* Case 3 */
    268 		iwd_notice(IWD_NOTICE_CONNECT_INFO,
    269 			"FullMAC driver: %s using SAE.  Expect EXTERNAL_AUTH",
    270 			wiphy->driver_str);
    271 
    272 		return true;
    273 	}
    274 
    275 	/* Case (2) */
    276 	if (wiphy_has_ext_feature(wiphy,
    277 				NL80211_EXT_FEATURE_SAE_OFFLOAD))
    278 		return true;
    279 
    280 	return false;
    281 }
    282 
    283 enum ie_rsn_akm_suite wiphy_select_akm(struct wiphy *wiphy,
    284 					const struct scan_bss *bss,
    285 					enum security security,
    286 					const struct ie_rsn_info *info,
    287 					bool fils_capable_hint)
    288 {
    289 	bool psk_offload = wiphy_has_ext_feature(wiphy,
    290 				NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK);
    291 
    292 	/*
    293 	 * If FT is available, use FT authentication to keep the door open
    294 	 * for fast transitions.  Otherwise use SHA256 version if present.
    295 	 */
    296 	if (security == SECURITY_8021X) {
    297 		if (wiphy_has_ext_feature(wiphy, NL80211_EXT_FEATURE_FILS_STA) &&
    298 				wiphy->support_cmds_auth_assoc &&
    299 				fils_capable_hint) {
    300 			if ((info->akm_suites &
    301 					IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384) &&
    302 					bss->rsne && bss->mde_present)
    303 				return IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA384;
    304 
    305 			if ((info->akm_suites &
    306 					IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256) &&
    307 					bss->rsne && bss->mde_present)
    308 				return IE_RSN_AKM_SUITE_FT_OVER_FILS_SHA256;
    309 
    310 			if (info->akm_suites & IE_RSN_AKM_SUITE_FILS_SHA384)
    311 				return IE_RSN_AKM_SUITE_FILS_SHA384;
    312 
    313 			if (info->akm_suites & IE_RSN_AKM_SUITE_FILS_SHA256)
    314 				return IE_RSN_AKM_SUITE_FILS_SHA256;
    315 		}
    316 
    317 		if ((info->akm_suites & IE_RSN_AKM_SUITE_FT_OVER_8021X) &&
    318 				bss->rsne && bss->mde_present &&
    319 				wiphy->support_cmds_auth_assoc)
    320 			return IE_RSN_AKM_SUITE_FT_OVER_8021X;
    321 
    322 		if (info->akm_suites & IE_RSN_AKM_SUITE_8021X_SHA256)
    323 			return IE_RSN_AKM_SUITE_8021X_SHA256;
    324 
    325 		if (info->akm_suites & IE_RSN_AKM_SUITE_8021X)
    326 			return IE_RSN_AKM_SUITE_8021X;
    327 	} else if (security == SECURITY_PSK) {
    328 		/*
    329 		 * Prefer connecting to SAE/WPA3 network, but only if SAE is
    330 		 * supported, we are MFP capable, and the AP has set the
    331 		 * MFPR/MFPC bits correctly. If any of these conditions are not
    332 		 * met, we can fallback to WPA2 (if the AKM is present).
    333 		 */
    334 		if (ie_rsne_is_wpa3_personal(info)) {
    335 			l_debug("Network is WPA3-Personal...");
    336 
    337 			if (!wiphy_can_connect_sae(wiphy)) {
    338 				l_debug("Can't use SAE, trying WPA2");
    339 				goto wpa2_personal;
    340 			}
    341 
    342 			if (info->akm_suites &
    343 					IE_RSN_AKM_SUITE_FT_OVER_SAE_SHA256)
    344 				return IE_RSN_AKM_SUITE_FT_OVER_SAE_SHA256;
    345 
    346 			if (info->akm_suites & IE_RSN_AKM_SUITE_SAE_SHA256)
    347 				return IE_RSN_AKM_SUITE_SAE_SHA256;
    348 		}
    349 
    350 wpa2_personal:
    351 		/*
    352 		 * Allow FT if either Auth/Assoc is supported OR if the card
    353 		 * supports PSK offload. Without Auth/Assoc, PSK offload is the
    354 		 * only mechanism to allow FT on these cards.
    355 		 */
    356 		if ((info->akm_suites & IE_RSN_AKM_SUITE_FT_USING_PSK) &&
    357 					bss->rsne && bss->mde_present) {
    358 			if (wiphy->support_cmds_auth_assoc ||
    359 					(psk_offload && wiphy->support_fw_roam))
    360 				return IE_RSN_AKM_SUITE_FT_USING_PSK;
    361 		}
    362 
    363 		if (info->akm_suites & IE_RSN_AKM_SUITE_PSK_SHA256)
    364 			return IE_RSN_AKM_SUITE_PSK_SHA256;
    365 
    366 		if (info->akm_suites & IE_RSN_AKM_SUITE_PSK)
    367 			return IE_RSN_AKM_SUITE_PSK;
    368 	} else if (security == SECURITY_NONE) {
    369 		if (info->akm_suites & IE_RSN_AKM_SUITE_OWE &&
    370 					!wiphy_owe_disabled(wiphy))
    371 			return IE_RSN_AKM_SUITE_OWE;
    372 	}
    373 
    374 	return 0;
    375 }
    376 
    377 static struct wiphy *wiphy_new(uint32_t id)
    378 {
    379 	struct wiphy *wiphy = l_new(struct wiphy, 1);
    380 
    381 	wiphy->id = id;
    382 	wiphy->supported_freqs = scan_freq_set_new();
    383 	watchlist_init(&wiphy->state_watches, NULL);
    384 	wiphy->extended_capabilities[0] = IE_TYPE_EXTENDED_CAPABILITIES;
    385 	wiphy->extended_capabilities[1] = EXT_CAP_LEN;
    386 
    387 	return wiphy;
    388 }
    389 
    390 static void destroy_work(void *user_data)
    391 {
    392 	struct wiphy_radio_work_item *work = user_data;
    393 
    394 	if (work->ops && work->ops->destroy)
    395 		work->ops->destroy(work);
    396 }
    397 
    398 static void wiphy_free(void *data)
    399 {
    400 	struct wiphy *wiphy = data;
    401 	uint32_t i;
    402 
    403 	l_debug("Freeing wiphy %s[%u]", wiphy->name, wiphy->id);
    404 
    405 	if (wiphy->dump_id)
    406 		l_genl_family_cancel(nl80211, wiphy->dump_id);
    407 
    408 	if (wiphy->get_reg_id)
    409 		l_genl_family_cancel(nl80211, wiphy->get_reg_id);
    410 
    411 	for (i = 0; i < NUM_NL80211_IFTYPES; i++)
    412 		l_free(wiphy->iftype_extended_capabilities[i]);
    413 
    414 	if (wiphy->band_2g) {
    415 		band_free(wiphy->band_2g);
    416 		wiphy->band_2g = NULL;
    417 	}
    418 
    419 	if (wiphy->band_5g) {
    420 		band_free(wiphy->band_5g);
    421 		wiphy->band_5g = NULL;
    422 	}
    423 
    424 	if (wiphy->band_6g) {
    425 		band_free(wiphy->band_6g);
    426 		wiphy->band_6g = NULL;
    427 	}
    428 
    429 	scan_freq_set_free(wiphy->supported_freqs);
    430 	watchlist_destroy(&wiphy->state_watches);
    431 	l_free(wiphy->model_str);
    432 	l_free(wiphy->vendor_str);
    433 	l_free(wiphy->driver_str);
    434 	l_genl_family_free(wiphy->nl80211);
    435 	l_queue_destroy(wiphy->work, destroy_work);
    436 	l_free(wiphy);
    437 }
    438 
    439 static bool wiphy_match(const void *a, const void *b)
    440 {
    441 	const struct wiphy *wiphy = a;
    442 	uint32_t id = L_PTR_TO_UINT(b);
    443 
    444 	return (wiphy->id == id);
    445 }
    446 
    447 struct wiphy *wiphy_find(int wiphy_id)
    448 {
    449 	return l_queue_find(wiphy_list, wiphy_match, L_UINT_TO_PTR(wiphy_id));
    450 }
    451 
    452 bool wiphy_is_blacklisted(const struct wiphy *wiphy)
    453 {
    454 	return wiphy->blacklisted;
    455 }
    456 
    457 static bool wiphy_is_managed(const char *phy)
    458 {
    459 	char *pattern;
    460 	unsigned int i;
    461 
    462 	if (!whitelist_filter)
    463 		goto check_blacklist;
    464 
    465 	for (i = 0; (pattern = whitelist_filter[i]); i++) {
    466 		if (fnmatch(pattern, phy, 0) != 0)
    467 			continue;
    468 
    469 		goto check_blacklist;
    470 	}
    471 
    472 	l_debug("whitelist filtered phy: %s", phy);
    473 	return false;
    474 
    475 check_blacklist:
    476 	if (!blacklist_filter)
    477 		return true;
    478 
    479 	for (i = 0; (pattern = blacklist_filter[i]); i++) {
    480 		if (fnmatch(pattern, phy, 0) == 0) {
    481 			l_debug("blacklist filtered ifname: %s", phy);
    482 			return false;
    483 		}
    484 	}
    485 
    486 	return true;
    487 }
    488 
    489 const char *wiphy_get_path(struct wiphy *wiphy)
    490 {
    491 	static char path[256];
    492 
    493 	L_WARN_ON(snprintf(path, sizeof(path), "%s/%d", IWD_BASE_PATH,
    494 				wiphy->id) >= (int) sizeof(path));
    495 	path[sizeof(path) - 1] = '\0';
    496 
    497 	return path;
    498 }
    499 
    500 uint32_t wiphy_get_id(struct wiphy *wiphy)
    501 {
    502 	return wiphy->id;
    503 }
    504 
    505 uint32_t wiphy_get_supported_bands(struct wiphy *wiphy)
    506 {
    507 	uint32_t bands = 0;
    508 
    509 	if (wiphy->band_2g)
    510 		bands |= BAND_FREQ_2_4_GHZ;
    511 
    512 	if (wiphy->band_5g)
    513 		bands |= BAND_FREQ_5_GHZ;
    514 
    515 	if (wiphy->band_6g)
    516 		bands |= BAND_FREQ_6_GHZ;
    517 
    518 	return bands;
    519 }
    520 
    521 const struct scan_freq_set *wiphy_get_supported_freqs(
    522 						const struct wiphy *wiphy)
    523 {
    524 	return wiphy->supported_freqs;
    525 }
    526 
    527 static struct band *wiphy_get_band(const struct wiphy *wiphy, enum band_freq band)
    528 {
    529 	switch (band) {
    530 	case BAND_FREQ_2_4_GHZ:
    531 		return wiphy->band_2g;
    532 	case BAND_FREQ_5_GHZ:
    533 		return wiphy->band_5g;
    534 	case BAND_FREQ_6_GHZ:
    535 		return wiphy->band_6g;
    536 	default:
    537 		return NULL;
    538 	}
    539 }
    540 
    541 const struct band_freq_attrs *wiphy_get_frequency_info(
    542 						const struct wiphy *wiphy,
    543 						uint32_t freq)
    544 {
    545 	struct band_freq_attrs *attr;
    546 	enum band_freq band;
    547 	uint8_t channel;
    548 	struct band *bandp;
    549 
    550 	channel = band_freq_to_channel(freq, &band);
    551 	if (!channel)
    552 		return NULL;
    553 
    554 	bandp = wiphy_get_band(wiphy, band);
    555 	if (!bandp)
    556 		return NULL;
    557 
    558 	attr = &bandp->freq_attrs[channel];
    559 	if (!attr->supported)
    560 		return NULL;
    561 
    562 	return attr;
    563 }
    564 
    565 const struct band_freq_attrs *wiphy_get_frequency_info_list(
    566 						const struct wiphy *wiphy,
    567 						enum band_freq band,
    568 						size_t *size)
    569 {
    570 	struct band *bandp;
    571 
    572 	bandp = wiphy_get_band(wiphy, band);
    573 	if (!bandp)
    574 		return NULL;
    575 
    576 	if (size)
    577 		*size = bandp->freqs_len;
    578 
    579 	return bandp->freq_attrs;
    580 }
    581 
    582 int wiphy_band_is_disabled(const struct wiphy *wiphy, enum band_freq band)
    583 {
    584 	struct band_freq_attrs attr;
    585 	unsigned int i;
    586 	struct band *bandp;
    587 
    588 	bandp = wiphy_get_band(wiphy, band);
    589 	if (!bandp)
    590 		return -ENOTSUP;
    591 
    592 	for (i = 0; i < bandp->freqs_len; i++) {
    593 		attr = bandp->freq_attrs[i];
    594 
    595 		if (!attr.supported)
    596 			continue;
    597 
    598 		if (!attr.disabled)
    599 			return 0;
    600 	}
    601 
    602 	return 1;
    603 }
    604 
    605 bool wiphy_supports_probe_resp_offload(struct wiphy *wiphy)
    606 {
    607 	return wiphy->ap_probe_resp_offload;
    608 }
    609 
    610 bool wiphy_can_transition_disable(struct wiphy *wiphy)
    611 {
    612 	/*
    613 	 * WPA3 Specification version 3, Section 2.2:
    614 	 * A STA shall not enable WEP and TKIP
    615 	 */
    616 	if (!(wiphy->supported_ciphers & IE_RSN_CIPHER_SUITE_CCMP))
    617 		return false;
    618 
    619 	if (!(wiphy->supported_ciphers & IE_RSN_CIPHER_SUITE_BIP_CMAC))
    620 		return false;
    621 
    622 	return true;
    623 }
    624 
    625 /* Catch all for the offload features */
    626 bool wiphy_can_offload(struct wiphy *wiphy)
    627 {
    628 	return wiphy_has_ext_feature(wiphy,
    629 				NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK) ||
    630 		wiphy_has_ext_feature(wiphy,
    631 				NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X) ||
    632 		wiphy_has_ext_feature(wiphy, NL80211_EXT_FEATURE_SAE_OFFLOAD);
    633 }
    634 
    635 bool wiphy_supports_ext_key_id(struct wiphy *wiphy)
    636 {
    637 	return wiphy_has_ext_feature(wiphy, NL80211_EXT_FEATURE_EXT_KEY_ID);
    638 }
    639 
    640 bool wiphy_supports_cmds_auth_assoc(struct wiphy *wiphy)
    641 {
    642 	return wiphy->support_cmds_auth_assoc;
    643 }
    644 
    645 bool wiphy_has_feature(struct wiphy *wiphy, uint32_t feature)
    646 {
    647 	return wiphy->feature_flags & feature;
    648 }
    649 
    650 bool wiphy_can_randomize_mac_addr(struct wiphy *wiphy)
    651 {
    652 	return wiphy_has_feature(wiphy, NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR);
    653 }
    654 
    655 bool wiphy_rrm_capable(struct wiphy *wiphy)
    656 {
    657 	if (wiphy_has_feature(wiphy,
    658 				NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
    659 			wiphy_has_feature(wiphy, NL80211_FEATURE_QUIET))
    660 		return true;
    661 
    662 	if (wiphy_has_ext_feature(wiphy, NL80211_EXT_FEATURE_RRM))
    663 		return true;
    664 
    665 	return false;
    666 }
    667 
    668 bool wiphy_has_ext_feature(const struct wiphy *wiphy, uint32_t feature)
    669 {
    670 	return feature < sizeof(wiphy->ext_features) * 8 &&
    671 		test_bit(wiphy->ext_features, feature);
    672 }
    673 
    674 uint8_t wiphy_get_max_num_ssids_per_scan(struct wiphy *wiphy)
    675 {
    676 	return wiphy->max_num_ssids_per_scan;
    677 }
    678 
    679 uint16_t wiphy_get_max_scan_ie_len(struct wiphy *wiphy)
    680 {
    681 	return wiphy->max_scan_ie_len;
    682 }
    683 
    684 uint32_t wiphy_get_max_roc_duration(struct wiphy *wiphy)
    685 {
    686 	return wiphy->max_roc_duration;
    687 }
    688 
    689 bool wiphy_supports_qos_set_map(struct wiphy *wiphy)
    690 {
    691 	return wiphy->support_qos_set_map;
    692 }
    693 
    694 bool wiphy_supports_firmware_roam(struct wiphy *wiphy)
    695 {
    696 	return wiphy->support_fw_roam;
    697 }
    698 
    699 const char *wiphy_get_driver(struct wiphy *wiphy)
    700 {
    701 	return wiphy->driver_str;
    702 }
    703 
    704 const char *wiphy_get_name(struct wiphy *wiphy)
    705 {
    706 	return wiphy->name;
    707 }
    708 
    709 bool wiphy_uses_default_if(struct wiphy *wiphy)
    710 {
    711 	if (!wiphy_get_driver(wiphy))
    712 		return true;
    713 
    714 	if (wiphy->driver_flags & DEFAULT_IF)
    715 		return true;
    716 
    717 	return false;
    718 }
    719 
    720 bool wiphy_control_port_enabled(struct wiphy *wiphy)
    721 {
    722 	const struct l_settings *settings = iwd_get_config();
    723 	bool enabled;
    724 
    725 	if (wiphy->driver_flags & FORCE_PAE)
    726 		return false;
    727 
    728 	if (!wiphy_has_ext_feature(wiphy,
    729 			NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
    730 		return false;
    731 
    732 	if (!l_settings_get_bool(settings, "General",
    733 					"ControlPortOverNL80211", &enabled))
    734 		enabled = true;
    735 
    736 	return enabled;
    737 }
    738 
    739 bool wiphy_power_save_disabled(struct wiphy *wiphy)
    740 {
    741 	if (wiphy->driver_flags & POWER_SAVE_DISABLE)
    742 		return true;
    743 
    744 	return false;
    745 }
    746 
    747 bool wiphy_owe_disabled(struct wiphy *wiphy)
    748 {
    749 	if (wiphy->driver_flags & OWE_DISABLE)
    750 		return true;
    751 
    752 	return false;
    753 }
    754 
    755 const uint8_t *wiphy_get_extended_capabilities(struct wiphy *wiphy,
    756 							uint32_t iftype)
    757 {
    758 	if (wiphy->iftype_extended_capabilities[iftype])
    759 		return wiphy->iftype_extended_capabilities[iftype];
    760 
    761 	return wiphy->extended_capabilities;
    762 }
    763 
    764 const uint8_t *wiphy_get_rm_enabled_capabilities(struct wiphy *wiphy)
    765 {
    766 	if (!wiphy_rrm_capable(wiphy))
    767 		return NULL;
    768 
    769 	return wiphy->rm_enabled_capabilities;
    770 }
    771 
    772 bool wiphy_get_rsnxe(const struct wiphy *wiphy, uint8_t *buf, size_t len)
    773 {
    774 	if (len < 3)
    775 		return false;
    776 
    777 	buf[0] = IE_TYPE_RSNX;
    778 	buf[1] = 1;
    779 
    780 	/*
    781 	 * Lower 4 bits of the first octet:
    782 	 * The length of the Extended RSN Capabilities field, in octets,
    783 	 * minus 1, i.e., n - 1.
    784 	 */
    785 	buf[2] = 0;
    786 
    787 	/* No other bits set for now */
    788 
    789 	return true;
    790 }
    791 
    792 static void wiphy_address_constrain(struct wiphy *wiphy, uint8_t addr[static 6])
    793 {
    794 	switch (mac_randomize_bytes) {
    795 	case 6:
    796 		/* Set the locally administered bit */
    797 		addr[0] |= 0x2;
    798 
    799 		/* Reset multicast bit */
    800 		addr[0] &= 0xfe;
    801 		break;
    802 	case 3:
    803 		memcpy(addr, wiphy->permanent_addr, 3);
    804 		break;
    805 	}
    806 
    807 	/*
    808 	 * Constrain the last NIC byte to 0x00 .. 0xfe, otherwise we might be
    809 	 * able to generate an address of 0xff 0xff 0xff which might be
    810 	 * interpreted as a vendor broadcast.  Similarly, 0x00 0x00 0x00 is
    811 	 * also not valid
    812 	 */
    813 	addr[5] &= 0xfe;
    814 	if (l_memeqzero(addr + 3, 3))
    815 		addr[5] = 0x01;
    816 }
    817 
    818 void wiphy_generate_random_address(struct wiphy *wiphy, uint8_t addr[static 6])
    819 {
    820 	switch (mac_randomize_bytes) {
    821 	case 6:
    822 		l_getrandom(addr, 6);
    823 		break;
    824 	case 3:
    825 		l_getrandom(addr + 3, 3);
    826 		break;
    827 	}
    828 
    829 	wiphy_address_constrain(wiphy, addr);
    830 }
    831 
    832 void wiphy_generate_address_from_ssid(struct wiphy *wiphy,
    833 					const uint8_t *ssid, size_t ssid_len,
    834 					uint8_t addr[static 6])
    835 {
    836 	struct l_checksum *sha = l_checksum_new(L_CHECKSUM_SHA256);
    837 
    838 	l_checksum_update(sha, ssid, ssid_len);
    839 	l_checksum_update(sha, wiphy->permanent_addr,
    840 				sizeof(wiphy->permanent_addr));
    841 	l_checksum_get_digest(sha, addr, mac_randomize_bytes);
    842 
    843 	l_checksum_free(sha);
    844 
    845 	wiphy_address_constrain(wiphy, addr);
    846 }
    847 
    848 bool wiphy_constrain_freq_set(const struct wiphy *wiphy,
    849 						struct scan_freq_set *set)
    850 {
    851 	struct band *bands[3] = { wiphy->band_2g,
    852 					wiphy->band_5g, wiphy->band_6g };
    853 	unsigned int b;
    854 	unsigned int i;
    855 
    856 	scan_freq_set_constrain(set, wiphy->supported_freqs);
    857 
    858 	for (b = 0; b < L_ARRAY_SIZE(bands); b++) {
    859 		struct band *band = bands[b];
    860 
    861 		if (!band)
    862 			continue;
    863 
    864 		for (i = 1; i <= band->freqs_len; i++) {
    865 			uint32_t freq;
    866 
    867 			if (!band->freq_attrs[i].supported)
    868 				continue;
    869 
    870 			if (!band->freq_attrs[i].disabled)
    871 				continue;
    872 
    873 			freq = band_channel_to_freq(i, band->freq);
    874 			if (!freq)
    875 				continue;
    876 
    877 			scan_freq_set_remove(set, freq);
    878 		}
    879 	}
    880 
    881 	if (!scan_freq_set_get_bands(set))
    882 		/* The set is empty. */
    883 		return false;
    884 
    885 	return true;
    886 }
    887 
    888 static char **wiphy_iftype_mask_to_str(uint16_t mask)
    889 {
    890 	char **ret = l_new(char *, __builtin_popcount(mask) + 1);
    891 	unsigned int i;
    892 	unsigned int j;
    893 
    894 	for (j = 0, i = 0; i < sizeof(mask) * 8; i++) {
    895 		const char *str;
    896 
    897 		if (!(mask & (1 << i)))
    898 			continue;
    899 
    900 		str = netdev_iftype_to_string(i + 1);
    901 		if (str)
    902 			ret[j++] = l_strdup(str);
    903 	}
    904 
    905 	return ret;
    906 }
    907 
    908 static char **wiphy_get_supported_iftypes(struct wiphy *wiphy, uint16_t mask)
    909 {
    910 	return wiphy_iftype_mask_to_str(wiphy->supported_iftypes & mask);
    911 }
    912 
    913 bool wiphy_supports_iftype(struct wiphy *wiphy, uint32_t iftype)
    914 {
    915 	if (iftype > sizeof(wiphy->supported_iftypes) * 8)
    916 		return false;
    917 
    918 	return wiphy->supported_iftypes & (1 << (iftype - 1));
    919 }
    920 
    921 const uint8_t *wiphy_get_supported_rates(struct wiphy *wiphy,
    922 						enum band_freq band,
    923 						unsigned int *out_num)
    924 {
    925 	struct band *bandp = wiphy_get_band(wiphy, band);
    926 
    927 	if (!bandp)
    928 		return NULL;
    929 
    930 	if (out_num)
    931 		*out_num = bandp->supported_rates_len;
    932 
    933 	return bandp->supported_rates;
    934 }
    935 
    936 void wiphy_get_reg_domain_country(struct wiphy *wiphy, char *out)
    937 {
    938 	char *country = wiphy->regdom_country;
    939 
    940 	if (!country[0])
    941 		/* Wiphy uses the global regulatory domain */
    942 		country = regdom_country;
    943 
    944 	out[0] = country[0];
    945 	out[1] = country[1];
    946 }
    947 
    948 bool wiphy_country_is_unknown(struct wiphy *wiphy)
    949 {
    950 	char cc[2];
    951 
    952 	wiphy_get_reg_domain_country(wiphy, cc);
    953 
    954 	/*
    955 	 * Treat OO and XX as an unknown country. Additional codes could be
    956 	 * added here if needed. The purpose of this is to know if we can
    957 	 * expect the disabled frequency list to be updated once a country is
    958 	 * known.
    959 	 */
    960 	return ((cc[0] == 'O' && cc[1] == 'O') ||
    961 			(cc[0] == 'X' && cc[1] == 'X'));
    962 }
    963 
    964 bool wiphy_supports_uapsd(const struct wiphy *wiphy)
    965 {
    966 	return wiphy->supports_uapsd;
    967 }
    968 
    969 bool wiphy_supports_cmd_offchannel(const struct wiphy *wiphy)
    970 {
    971 	return wiphy->supports_cmd_offchannel;
    972 }
    973 
    974 bool wiphy_supports_multicast_rx(const struct wiphy *wiphy)
    975 {
    976 	return wiphy_has_ext_feature(wiphy,
    977 				NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS) &&
    978 				!(wiphy->driver_flags & MULTICAST_RX_DISABLE);
    979 }
    980 
    981 bool wiphy_supports_colocated_flag(const struct wiphy *wiphy)
    982 {
    983 	return !(wiphy->driver_flags & COLOCATED_SCAN_DISABLE);
    984 }
    985 
    986 const uint8_t *wiphy_get_ht_capabilities(const struct wiphy *wiphy,
    987 						enum band_freq band,
    988 						size_t *size)
    989 {
    990 	static uint8_t ht_capa[26];
    991 	const struct band *bandp = wiphy_get_band(wiphy, band);
    992 
    993 	if (!bandp)
    994 		return NULL;
    995 
    996 	if (!bandp->ht_supported)
    997 		return NULL;
    998 
    999 	memset(ht_capa, 0, sizeof(ht_capa));
   1000 
   1001 	/*
   1002 	 * The kernel segments the HT capabilities element into multiple
   1003 	 * attributes. For convenience on the caller just combine them and
   1004 	 * return the full IE rather than adding 3 separate getters. This also
   1005 	 * provides a way to check if HT is supported.
   1006 	 */
   1007 	memcpy(ht_capa, bandp->ht_capabilities, 2);
   1008 	ht_capa[2] = bandp->ht_ampdu_params;
   1009 	memcpy(ht_capa + 3, bandp->ht_mcs_set, 16);
   1010 
   1011 	/*
   1012 	 * TODO: HT Extended capabilities, beamforming, and ASEL capabilities
   1013 	 * are not available to get from the kernel, leave as zero.
   1014 	 */
   1015 
   1016 	if (size)
   1017 		*size = sizeof(ht_capa);
   1018 
   1019 	return ht_capa;
   1020 }
   1021 
   1022 int wiphy_estimate_data_rate(struct wiphy *wiphy,
   1023 				const void *ies, uint16_t ies_len,
   1024 				const struct scan_bss *bss,
   1025 				uint64_t *out_data_rate)
   1026 {
   1027 	struct ie_tlv_iter iter;
   1028 	const void *supported_rates = NULL;
   1029 	const void *ext_supported_rates = NULL;
   1030 	const void *vht_capabilities = NULL;
   1031 	const void *vht_operation = NULL;
   1032 	const void *ht_capabilities = NULL;
   1033 	const void *ht_operation = NULL;
   1034 	const void *he_capabilities = NULL;
   1035 	const struct band *bandp;
   1036 	enum band_freq band;
   1037 	int ret;
   1038 
   1039 	if (band_freq_to_channel(bss->frequency, &band) == 0)
   1040 		return -ENOTSUP;
   1041 
   1042 	bandp = wiphy_get_band(wiphy, band);
   1043 	if (!bandp)
   1044 		return -ENOTSUP;
   1045 
   1046 	ie_tlv_iter_init(&iter, ies, ies_len);
   1047 
   1048 	while (ie_tlv_iter_next(&iter)) {
   1049 		uint16_t tag = ie_tlv_iter_get_tag(&iter);
   1050 
   1051 		switch (tag) {
   1052 		case IE_TYPE_SUPPORTED_RATES:
   1053 			if (iter.len > 8)
   1054 				continue;
   1055 
   1056 			supported_rates = iter.data - 2;
   1057 			break;
   1058 		case IE_TYPE_EXTENDED_SUPPORTED_RATES:
   1059 			ext_supported_rates = iter.data - 2;
   1060 			break;
   1061 		case IE_TYPE_HT_CAPABILITIES:
   1062 			if (iter.len != 26)
   1063 				continue;
   1064 
   1065 			ht_capabilities = iter.data - 2;
   1066 			break;
   1067 		case IE_TYPE_HT_OPERATION:
   1068 			if (iter.len != 22)
   1069 				continue;
   1070 
   1071 			ht_operation = iter.data - 2;
   1072 			break;
   1073 		case IE_TYPE_VHT_CAPABILITIES:
   1074 			if (iter.len != 12)
   1075 				continue;
   1076 
   1077 			vht_capabilities = iter.data - 2;
   1078 			break;
   1079 		case IE_TYPE_VHT_OPERATION:
   1080 			if (iter.len != 5)
   1081 				continue;
   1082 
   1083 			vht_operation = iter.data - 2;
   1084 			break;
   1085 		case IE_TYPE_HE_CAPABILITIES:
   1086 			if (!ie_validate_he_capabilities(iter.data, iter.len)) {
   1087 				l_warn("invalid HE capabilities for "MAC,
   1088 					MAC_STR(bss->addr));
   1089 				continue;
   1090 			}
   1091 
   1092 			he_capabilities = iter.data;
   1093 			break;
   1094 		default:
   1095 			break;
   1096 		}
   1097 	}
   1098 
   1099 	ret = band_estimate_he_rx_rate(bandp, he_capabilities,
   1100 					bss->signal_strength / 100,
   1101 					out_data_rate);
   1102 	if (!ret)
   1103 		return 0;
   1104 	else if (ret != -ENOTSUP && ret != -ENETUNREACH)
   1105 		l_warn("error parsing HE capabilities");
   1106 
   1107 	ret = band_estimate_vht_rx_rate(bandp, vht_capabilities, vht_operation,
   1108 					ht_capabilities, ht_operation,
   1109 					bss->signal_strength / 100,
   1110 					out_data_rate);
   1111 	if (!ret)
   1112 		return 0;
   1113 	else if (ret != -ENOTSUP && ret != -ENETUNREACH)
   1114 		l_warn("error parsing VHT capabilities");
   1115 
   1116 	ret = band_estimate_ht_rx_rate(bandp, ht_capabilities, ht_operation,
   1117 					bss->signal_strength / 100,
   1118 					out_data_rate);
   1119 	if (!ret)
   1120 		return 0;
   1121 	else if (ret != -ENOTSUP && ret != -ENETUNREACH)
   1122 		l_warn("error parsing HT capabilities");
   1123 
   1124 	ret = band_estimate_nonht_rate(bandp, supported_rates,
   1125 						ext_supported_rates,
   1126 						bss->signal_strength / 100,
   1127 						out_data_rate);
   1128 	if (ret != 0 && ret != -ENOTSUP && ret != -ENETUNREACH)
   1129 		l_warn("error parsing non-HT rates");
   1130 
   1131 	return ret;
   1132 }
   1133 
   1134 bool wiphy_regdom_is_updating(struct wiphy *wiphy)
   1135 {
   1136 	return wiphy->dump_id || (!wiphy->self_managed && wiphy_dump_id);
   1137 }
   1138 
   1139 uint32_t wiphy_state_watch_add(struct wiphy *wiphy,
   1140 				wiphy_state_watch_func_t func,
   1141 				void *user_data, wiphy_destroy_func_t destroy)
   1142 {
   1143 	return watchlist_add(&wiphy->state_watches, func, user_data, destroy);
   1144 }
   1145 
   1146 bool wiphy_state_watch_remove(struct wiphy *wiphy, uint32_t id)
   1147 {
   1148 	return watchlist_remove(&wiphy->state_watches, id);
   1149 }
   1150 
   1151 static void wiphy_print_mcs_indexes(const uint8_t *mcs)
   1152 {
   1153 	int i;
   1154 
   1155 	for (i = 0; i < 77; i++) {
   1156 		int start;
   1157 
   1158 		if (!test_bit(mcs, i))
   1159 			continue;
   1160 
   1161 		start = i;
   1162 
   1163 		while (i < 76 && test_bit(mcs, i + 1))
   1164 			i += 1;
   1165 
   1166 		if (start != i)
   1167 			l_info("\t\t\t%d-%d", start, i);
   1168 		else
   1169 			l_info("\t\t\t%d", start);
   1170 	}
   1171 }
   1172 
   1173 static void wiphy_print_mcs_info(const uint8_t *mcs_map,
   1174 						const char *prefix,
   1175 						uint8_t value0,
   1176 						uint8_t value1,
   1177 						uint8_t value2)
   1178 {
   1179 	int i;
   1180 
   1181 	for (i = 14; i >= 0; i -= 2) {
   1182 		uint8_t value;
   1183 		int mcs = bit_field(mcs_map[i / 8], i % 8, 2);
   1184 
   1185 		if (mcs == 0x3)
   1186 			continue;
   1187 
   1188 		switch (mcs) {
   1189 		case 0:
   1190 			value = value0;
   1191 			break;
   1192 		case 1:
   1193 			value = value1;
   1194 			break;
   1195 		case 2:
   1196 			value = value2;
   1197 			break;
   1198 		}
   1199 
   1200 		l_info("\t\t\tMax %s MCS: 0-%d for NSS: %d", prefix,
   1201 			value, i / 2 + 1);
   1202 		return;
   1203 	}
   1204 }
   1205 
   1206 static void wiphy_print_he_capabilities(struct band *band,
   1207 				const struct band_he_capabilities *he_cap)
   1208 {
   1209 	_auto_(l_strv_free) char **iftypes = NULL;
   1210 	_auto_(l_free) char *joined = NULL;
   1211 	uint8_t width_set = bit_field(he_cap->he_phy_capa[0], 1, 7);
   1212 
   1213 	iftypes = wiphy_iftype_mask_to_str(he_cap->iftypes);
   1214 	joined = l_strjoinv(iftypes, ' ');
   1215 
   1216 	l_info("\t\t\tInterface Types: %s", joined);
   1217 
   1218 	switch (band->freq) {
   1219 	case BAND_FREQ_2_4_GHZ:
   1220 		wiphy_print_mcs_info(he_cap->he_mcs_set,
   1221 					"HE RX <= 80MHz", 7, 9, 11);
   1222 		wiphy_print_mcs_info(he_cap->he_mcs_set + 2,
   1223 					"HE TX <= 80MHz", 7, 9, 11);
   1224 		break;
   1225 	case BAND_FREQ_5_GHZ:
   1226 	case BAND_FREQ_6_GHZ:
   1227 		wiphy_print_mcs_info(he_cap->he_mcs_set,
   1228 					"HE RX <= 80MHz", 7, 9, 11);
   1229 		wiphy_print_mcs_info(he_cap->he_mcs_set + 2,
   1230 					"HE TX <= 80MHz", 7, 9, 11);
   1231 
   1232 		if (test_bit(&width_set, 2)) {
   1233 			wiphy_print_mcs_info(he_cap->he_mcs_set + 4,
   1234 					"HE RX <= 160MHz", 7, 9, 11);
   1235 			wiphy_print_mcs_info(he_cap->he_mcs_set + 6,
   1236 					"HE TX <= 160MHz", 7, 9, 11);
   1237 		}
   1238 
   1239 		if (test_bit(&width_set, 3)) {
   1240 			wiphy_print_mcs_info(he_cap->he_mcs_set + 8,
   1241 					"HE RX <= 80+80MHz", 7, 9, 11);
   1242 			wiphy_print_mcs_info(he_cap->he_mcs_set + 10,
   1243 					"HE TX <= 80+80MHz", 7, 9, 11);
   1244 		}
   1245 
   1246 		break;
   1247 	}
   1248 }
   1249 
   1250 static void wiphy_print_band_info(struct band *band, const char *name)
   1251 {
   1252 	int i;
   1253 
   1254 	l_info("\t%s:", name);
   1255 	l_info("\t\tBitrates (non-HT):");
   1256 
   1257 	for (i = 0; i < band->supported_rates_len; i++)
   1258 		l_info("\t\t\t%2d.%d Mbps", band->supported_rates[i] / 2,
   1259 					band->supported_rates[i] % 2 * 5);
   1260 
   1261 	if (band->ht_supported) {
   1262 		uint8_t max_nss = bit_field(band->ht_mcs_set[12], 2, 2) + 1;
   1263 
   1264 		l_info("\t\tHT Capabilities:");
   1265 
   1266 		if (test_bit(band->ht_capabilities, 1))
   1267 			l_info("\t\t\tHT40");
   1268 		else
   1269 			l_info("\t\t\tHT20");
   1270 
   1271 		if (test_bit(band->ht_capabilities, 5))
   1272 			l_info("\t\t\tShort GI for 20Mhz");
   1273 
   1274 		if (test_bit(band->ht_capabilities, 6))
   1275 			l_info("\t\t\tShort GI for 40Mhz");
   1276 
   1277 		l_info("\t\tHT RX MCS indexes:");
   1278 		wiphy_print_mcs_indexes(band->ht_mcs_set);
   1279 
   1280 		if (test_bit(band->ht_mcs_set, 96)) {
   1281 			if (test_bit(band->ht_mcs_set, 97))
   1282 				l_info("\t\tHT TX MCS differ, max NSS: %d",
   1283 					max_nss);
   1284 		} else
   1285 			l_info("\t\tHT TX MCS set undefined");
   1286 	}
   1287 
   1288 	if (band->vht_supported) {
   1289 		l_info("\t\tVHT Capabilities:");
   1290 
   1291 		switch (bit_field(band->vht_capabilities[0], 2, 2)) {
   1292 		case 1:
   1293 			l_info("\t\t\t160 Mhz operation");
   1294 			break;
   1295 		case 2:
   1296 			l_info("\t\t\t160 Mhz, 80+80 Mhz operation");
   1297 			break;
   1298 		}
   1299 
   1300 		if (test_bit(band->vht_capabilities, 5))
   1301 			l_info("\t\t\tShort GI for 80Mhz");
   1302 
   1303 		if (test_bit(band->vht_capabilities, 6))
   1304 			l_info("\t\t\tShort GI for 160 and 80 + 80 Mhz");
   1305 
   1306 		wiphy_print_mcs_info(band->vht_mcs_set, "RX", 7, 8, 9);
   1307 		wiphy_print_mcs_info(band->vht_mcs_set + 4, "TX", 7, 8, 9);
   1308 	}
   1309 
   1310 	if (band->he_capabilities) {
   1311 		const struct l_queue_entry *entry;
   1312 
   1313 		l_info("\t\tHE Capabilities");
   1314 
   1315 		for (entry = l_queue_get_entries(band->he_capabilities);
   1316 						entry; entry = entry->next) {
   1317 			const struct band_he_capabilities *he_cap = entry->data;
   1318 
   1319 			wiphy_print_he_capabilities(band, he_cap);
   1320 		}
   1321 
   1322 	}
   1323 }
   1324 
   1325 static void wiphy_print_basic_info(struct wiphy *wiphy)
   1326 {
   1327 	char buf[2048];
   1328 
   1329 	l_info("Wiphy: %d, Name: %s", wiphy->id, wiphy->name);
   1330 	l_info("\tPermanent Address: "MAC, MAC_STR(wiphy->permanent_addr));
   1331 
   1332 	if (wiphy->band_2g)
   1333 		wiphy_print_band_info(wiphy->band_2g, "2.4GHz Band");
   1334 
   1335 	if (wiphy->band_5g)
   1336 		wiphy_print_band_info(wiphy->band_5g, "5GHz Band");
   1337 
   1338 	if (wiphy->band_6g)
   1339 		wiphy_print_band_info(wiphy->band_6g, "6GHz Band");
   1340 
   1341 	if (wiphy->supported_ciphers) {
   1342 		int n = 0;
   1343 		size_t len = 0;
   1344 		int i = sizeof(wiphy->supported_ciphers) * 8 - 1;
   1345 
   1346 		len += snprintf(buf, sizeof(buf), "\tCiphers:");
   1347 
   1348 		for (; i >= 0 && len < sizeof(buf); i--) {
   1349 			typeof(wiphy->supported_ciphers) cipher = 1 << i;
   1350 			const char *str;
   1351 
   1352 			if (cipher == IE_RSN_CIPHER_SUITE_WEP40 ||
   1353 					cipher == IE_RSN_CIPHER_SUITE_WEP104)
   1354 				continue;
   1355 
   1356 			if (!(wiphy->supported_ciphers & cipher))
   1357 				continue;
   1358 
   1359 			str = ie_rsn_cipher_suite_to_string(cipher);
   1360 			if (!str)
   1361 				continue;
   1362 
   1363 			len += snprintf(buf + len, sizeof(buf) - len, "%s%s",
   1364 					!n || (n % 4) ? " " : "\n\t\t ",
   1365 					str);
   1366 
   1367 			n += 1;
   1368 		}
   1369 
   1370 		l_info("%s", buf);
   1371 	}
   1372 
   1373 	if (wiphy->supported_iftypes) {
   1374 		char **iftypes = wiphy_get_supported_iftypes(wiphy, ~0);
   1375 		char *joined = l_strjoinv(iftypes, ' ');
   1376 
   1377 		l_info("\tSupported iftypes: %s", joined);
   1378 
   1379 		l_free(joined);
   1380 		l_strfreev(iftypes);
   1381 	}
   1382 
   1383 	if (wiphy->driver_flags) {
   1384 		char **flags = l_strv_new();
   1385 		char *joined;
   1386 
   1387 		if (wiphy->driver_flags & DEFAULT_IF)
   1388 			flags = l_strv_append(flags, "DefaultInterface");
   1389 
   1390 		if (wiphy->driver_flags & FORCE_PAE)
   1391 			flags = l_strv_append(flags, "ForcePae");
   1392 
   1393 		if (wiphy->driver_flags & POWER_SAVE_DISABLE)
   1394 			flags = l_strv_append(flags, "PowerSaveDisable");
   1395 
   1396 		if (wiphy->driver_flags & OWE_DISABLE)
   1397 			flags = l_strv_append(flags, "OweDisable");
   1398 
   1399 		if (wiphy->driver_flags & MULTICAST_RX_DISABLE)
   1400 			flags = l_strv_append(flags, "MulticastRxDisable");
   1401 
   1402 		if (wiphy->driver_flags & SAE_DISABLE)
   1403 			flags = l_strv_append(flags, "SaeDisable");
   1404 
   1405 		if (wiphy->driver_flags & COLOCATED_SCAN_DISABLE)
   1406 			flags = l_strv_append(flags, "ColocatedScanDisable");
   1407 
   1408 		joined = l_strjoinv(flags, ' ');
   1409 
   1410 		l_info("\tDriver Flags: %s", joined);
   1411 
   1412 		l_free(joined);
   1413 		l_strfreev(flags);
   1414 	}
   1415 }
   1416 
   1417 static void parse_supported_commands(struct wiphy *wiphy,
   1418 						struct l_genl_attr *attr)
   1419 {
   1420 	uint16_t type, len;
   1421 	const void *data;
   1422 	bool auth = false;
   1423 	bool assoc = false;
   1424 
   1425 	while (l_genl_attr_next(attr, &type, &len, &data)) {
   1426 		uint32_t cmd = *(uint32_t *)data;
   1427 
   1428 		switch (cmd) {
   1429 		case NL80211_CMD_START_SCHED_SCAN:
   1430 			wiphy->support_scheduled_scan = true;
   1431 			break;
   1432 		case NL80211_CMD_SET_REKEY_OFFLOAD:
   1433 			wiphy->support_rekey_offload = true;
   1434 			break;
   1435 		case NL80211_CMD_SET_QOS_MAP:
   1436 			wiphy->support_qos_set_map = true;
   1437 			break;
   1438 		case NL80211_CMD_AUTHENTICATE:
   1439 			auth = true;
   1440 			break;
   1441 		case NL80211_CMD_ASSOCIATE:
   1442 			assoc = true;
   1443 			break;
   1444 		case NL80211_CMD_REMAIN_ON_CHANNEL:
   1445 			wiphy->supports_cmd_offchannel = true;
   1446 			break;
   1447 		}
   1448 	}
   1449 
   1450 	if (auth && assoc)
   1451 		wiphy->support_cmds_auth_assoc = true;
   1452 }
   1453 
   1454 static void parse_supported_ciphers(struct wiphy *wiphy, const void *data,
   1455 						uint16_t len)
   1456 {
   1457 	while (len >= 4) {
   1458 		uint32_t cipher = *(uint32_t *)data;
   1459 
   1460 		switch (cipher) {
   1461 		case CRYPTO_CIPHER_CCMP:
   1462 			wiphy->supported_ciphers |= IE_RSN_CIPHER_SUITE_CCMP;
   1463 			break;
   1464 		case CRYPTO_CIPHER_TKIP:
   1465 			wiphy->supported_ciphers |= IE_RSN_CIPHER_SUITE_TKIP;
   1466 			break;
   1467 		case CRYPTO_CIPHER_WEP40:
   1468 			wiphy->supported_ciphers |= IE_RSN_CIPHER_SUITE_WEP40;
   1469 			break;
   1470 		case CRYPTO_CIPHER_WEP104:
   1471 			wiphy->supported_ciphers |= IE_RSN_CIPHER_SUITE_WEP104;
   1472 			break;
   1473 		case CRYPTO_CIPHER_BIP_CMAC:
   1474 			wiphy->supported_ciphers |=
   1475 				IE_RSN_CIPHER_SUITE_BIP_CMAC;
   1476 			break;
   1477 		case CRYPTO_CIPHER_GCMP:
   1478 			wiphy->supported_ciphers |= IE_RSN_CIPHER_SUITE_GCMP;
   1479 			break;
   1480 		case CRYPTO_CIPHER_GCMP_256:
   1481 			wiphy->supported_ciphers |=
   1482 				IE_RSN_CIPHER_SUITE_GCMP_256;
   1483 			break;
   1484 		case CRYPTO_CIPHER_CCMP_256:
   1485 			wiphy->supported_ciphers |=
   1486 				IE_RSN_CIPHER_SUITE_CCMP_256;
   1487 			break;
   1488 		case CRYPTO_CIPHER_BIP_GMAC:
   1489 			wiphy->supported_ciphers |=
   1490 				IE_RSN_CIPHER_SUITE_BIP_GMAC;
   1491 			break;
   1492 		case CRYPTO_CIPHER_BIP_GMAC_256:
   1493 			wiphy->supported_ciphers |=
   1494 				IE_RSN_CIPHER_SUITE_BIP_GMAC_256;
   1495 			break;
   1496 		case CRYPTO_CIPHER_BIP_CMAC_256:
   1497 			wiphy->supported_ciphers |=
   1498 				IE_RSN_CIPHER_SUITE_BIP_CMAC_256;
   1499 			break;
   1500 		default:	/* TODO: Support other ciphers */
   1501 			break;
   1502 		}
   1503 
   1504 		len -= 4;
   1505 		data += 4;
   1506 	}
   1507 }
   1508 
   1509 static int parse_supported_rates(struct l_genl_attr *attr, struct band *band)
   1510 {
   1511 	uint16_t type;
   1512 	uint16_t len;
   1513 	const void *data;
   1514 	struct l_genl_attr nested;
   1515 	int count = 0;
   1516 
   1517 	if (!l_genl_attr_recurse(attr, &nested))
   1518 		return -EBADMSG;
   1519 
   1520 	while (l_genl_attr_next(&nested, NULL, NULL, NULL)) {
   1521 		struct l_genl_attr nested2;
   1522 
   1523 		if (!l_genl_attr_recurse(&nested, &nested2))
   1524 			return -EBADMSG;
   1525 
   1526 		while (l_genl_attr_next(&nested2, &type, &len, &data)) {
   1527 			uint32_t rate;
   1528 
   1529 			if (type != NL80211_BITRATE_ATTR_RATE || len != 4)
   1530 				continue;
   1531 
   1532 			rate = l_get_u32(data);
   1533 
   1534 			if (rate % 5)
   1535 				continue;
   1536 
   1537 			/*
   1538 			 * Convert from the 100kb/s units reported by the
   1539 			 * kernel to the 500kb/s used in 802.11 IEs.
   1540 			 */
   1541 			rate /= 5;
   1542 
   1543 			/*
   1544 			 * Rates past 120 seem to be used for other purposes,
   1545 			 * BSS Membership Selector (HT/VHT), etc
   1546 			 */
   1547 			if (rate > 120)
   1548 				continue;
   1549 
   1550 			band->supported_rates[count++] = rate;
   1551 		}
   1552 	}
   1553 
   1554 	band->supported_rates_len = count;
   1555 
   1556 	return 0;
   1557 }
   1558 
   1559 static struct band *band_new_from_message(struct l_genl_attr *band)
   1560 {
   1561 	uint16_t type;
   1562 	struct l_genl_attr nested;
   1563 	uint16_t count = 0;
   1564 	struct band *ret;
   1565 	size_t toalloc;
   1566 
   1567 	/* First find the number of supported rates */
   1568 	while (l_genl_attr_next(band, &type, NULL, NULL)) {
   1569 		switch (type) {
   1570 		case NL80211_BAND_ATTR_RATES:
   1571 			if (!l_genl_attr_recurse(band, &nested))
   1572 				return NULL;
   1573 
   1574 			while (l_genl_attr_next(&nested, NULL, NULL, NULL))
   1575 				count++;
   1576 		}
   1577 	}
   1578 
   1579 	toalloc = sizeof(struct band) + count * sizeof(uint8_t);
   1580 	ret = l_malloc(toalloc);
   1581 	memset(ret, 0, toalloc);
   1582 
   1583 #if __GNUC__ == 11 && __GNUC_MINOR__ == 2
   1584 _Pragma("GCC diagnostic push")
   1585 _Pragma("GCC diagnostic ignored \"-Warray-bounds\"")
   1586 #endif
   1587 	memset(ret->vht_mcs_set, 0xff, sizeof(ret->vht_mcs_set));
   1588 #if __GNUC__ == 11 && __GNUC_MINOR__ == 2
   1589 _Pragma("GCC diagnostic pop")
   1590 #endif
   1591 
   1592 	return ret;
   1593 }
   1594 
   1595 static uint32_t get_iftypes(struct l_genl_attr *iftypes)
   1596 {
   1597 	uint16_t type;
   1598 	uint16_t len;
   1599 	uint32_t types = 0;
   1600 
   1601 	while (l_genl_attr_next(iftypes, &type, &len, NULL)) {
   1602 		if (len != 0)
   1603 			continue;
   1604 
   1605 		types |= (1 << (type - 1));
   1606 	}
   1607 
   1608 	return types;
   1609 }
   1610 
   1611 static void parse_iftype_attrs(struct band *band, struct l_genl_attr *types)
   1612 {
   1613 	uint16_t type;
   1614 	uint16_t len;
   1615 	const void *data;
   1616 	unsigned int count = 0;
   1617 	struct band_he_capabilities *he_cap =
   1618 					l_new(struct band_he_capabilities, 1);
   1619 
   1620 	while (l_genl_attr_next(types, &type, &len, &data)) {
   1621 		struct l_genl_attr iftypes;
   1622 
   1623 		switch (type) {
   1624 		case NL80211_BAND_IFTYPE_ATTR_IFTYPES:
   1625 			if (!l_genl_attr_recurse(types, &iftypes))
   1626 				goto parse_error;
   1627 
   1628 			he_cap->iftypes = get_iftypes(&iftypes);
   1629 			break;
   1630 		case NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY:
   1631 			if (len > sizeof(he_cap->he_phy_capa))
   1632 				continue;
   1633 
   1634 			memcpy(he_cap->he_phy_capa, data, len);
   1635 			count++;
   1636 			break;
   1637 		case NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET:
   1638 			if (len > sizeof(he_cap->he_mcs_set))
   1639 				continue;
   1640 
   1641 			memcpy(he_cap->he_mcs_set, data, len);
   1642 			count++;
   1643 			break;
   1644 		default:
   1645 			break;
   1646 		}
   1647 	}
   1648 
   1649 	/*
   1650 	 * Since the capabilities element indicates what values are present in
   1651 	 * the MCS set ensure both values are parsed
   1652 	 */
   1653 	if (count != 2 || !he_cap->iftypes)
   1654 		goto parse_error;
   1655 
   1656 	if (!band->he_capabilities)
   1657 		band->he_capabilities = l_queue_new();
   1658 
   1659 	l_queue_push_head(band->he_capabilities, he_cap);
   1660 
   1661 	return;
   1662 
   1663 parse_error:
   1664 	l_free(he_cap);
   1665 }
   1666 
   1667 static void parse_band_iftype_data(struct band *band, struct l_genl_attr *ifdata)
   1668 {
   1669 	while (l_genl_attr_next(ifdata, NULL, NULL, NULL)) {
   1670 		struct l_genl_attr types;
   1671 
   1672 		if (!l_genl_attr_recurse(ifdata, &types))
   1673 			continue;
   1674 
   1675 		parse_iftype_attrs(band, &types);
   1676 	}
   1677 }
   1678 
   1679 static void parse_supported_bands(struct wiphy *wiphy,
   1680 						struct l_genl_attr *bands)
   1681 {
   1682 	uint16_t type;
   1683 	uint16_t len;
   1684 	const void *data;
   1685 	struct l_genl_attr attr;
   1686 
   1687 	while (l_genl_attr_next(bands, &type, NULL, NULL)) {
   1688 		struct band **bandp;
   1689 		struct band *band;
   1690 		enum band_freq freq;
   1691 		size_t num_channels;
   1692 
   1693 		switch (type) {
   1694 		case NL80211_BAND_2GHZ:
   1695 			bandp = &wiphy->band_2g;
   1696 			freq = BAND_FREQ_2_4_GHZ;
   1697 			num_channels = 14;
   1698 			break;
   1699 		case NL80211_BAND_5GHZ:
   1700 			bandp = &wiphy->band_5g;
   1701 			freq = BAND_FREQ_5_GHZ;
   1702 			num_channels = 196;
   1703 			break;
   1704 		case NL80211_BAND_6GHZ:
   1705 			bandp = &wiphy->band_6g;
   1706 			freq = BAND_FREQ_6_GHZ;
   1707 			num_channels = 233;
   1708 			break;
   1709 		default:
   1710 			continue;
   1711 		}
   1712 
   1713 		if (!l_genl_attr_recurse(bands, &attr))
   1714 			continue;
   1715 
   1716 		if (*bandp == NULL) {
   1717 			band = band_new_from_message(&attr);
   1718 			if (!band)
   1719 				continue;
   1720 
   1721 			band->freq = freq;
   1722 			/*
   1723 			 * Since channels start at 1, allocate one extra in
   1724 			 * order to use channel indexes without arithmetic
   1725 			 */
   1726 			band->freq_attrs = l_new(struct band_freq_attrs,
   1727 							num_channels + 1);
   1728 			band->freqs_len = num_channels;
   1729 
   1730 			/* Reset iter to beginning */
   1731 			if (!l_genl_attr_recurse(bands, &attr)) {
   1732 				band_free(band);
   1733 				continue;
   1734 			}
   1735 		} else
   1736 			band = *bandp;
   1737 
   1738 		while (l_genl_attr_next(&attr, &type, &len, &data)) {
   1739 			struct l_genl_attr nested;
   1740 
   1741 			switch (type) {
   1742 			case NL80211_BAND_ATTR_FREQS:
   1743 				nl80211_parse_supported_frequencies(&attr,
   1744 							wiphy->supported_freqs,
   1745 							band);
   1746 				break;
   1747 
   1748 			case NL80211_BAND_ATTR_RATES:
   1749 				if (parse_supported_rates(&attr, band) < 0) {
   1750 					band_free(band);
   1751 					continue;
   1752 				}
   1753 
   1754 				break;
   1755 			case NL80211_BAND_ATTR_VHT_MCS_SET:
   1756 				if (L_WARN_ON(len != sizeof(band->vht_mcs_set)))
   1757 					continue;
   1758 
   1759 				memcpy(band->vht_mcs_set, data, len);
   1760 				band->vht_supported = true;
   1761 				break;
   1762 			case NL80211_BAND_ATTR_VHT_CAPA:
   1763 				if (L_WARN_ON(len !=
   1764 						sizeof(band->vht_capabilities)))
   1765 					continue;
   1766 
   1767 				memcpy(band->vht_capabilities, data, len);
   1768 				band->vht_supported = true;
   1769 				break;
   1770 			case NL80211_BAND_ATTR_HT_MCS_SET:
   1771 				if (L_WARN_ON(len != sizeof(band->ht_mcs_set)))
   1772 					continue;
   1773 
   1774 				memcpy(band->ht_mcs_set, data, len);
   1775 				band->ht_supported = true;
   1776 				break;
   1777 			case NL80211_BAND_ATTR_HT_CAPA:
   1778 				if (L_WARN_ON(len !=
   1779 						sizeof(band->ht_capabilities)))
   1780 					continue;
   1781 
   1782 				memcpy(band->ht_capabilities, data, len);
   1783 				band->ht_supported = true;
   1784 				break;
   1785 			/*
   1786 			 * AMPDU factor/density are part of A-MPDU Parameters,
   1787 			 * 802.11-2020 Section 9.4.2.55.3.
   1788 			 */
   1789 			case NL80211_BAND_ATTR_HT_AMPDU_FACTOR:
   1790 				if (L_WARN_ON(len != 1))
   1791 					continue;
   1792 
   1793 				band->ht_ampdu_params |= l_get_u8(data) & 0x3;
   1794 				break;
   1795 			case NL80211_BAND_ATTR_HT_AMPDU_DENSITY:
   1796 				if (L_WARN_ON(len != 1))
   1797 					continue;
   1798 
   1799 				band->ht_ampdu_params |=
   1800 						(l_get_u8(data) & 0x7) << 2;
   1801 				break;
   1802 			case NL80211_BAND_ATTR_IFTYPE_DATA:
   1803 				if (!l_genl_attr_recurse(&attr, &nested))
   1804 					continue;
   1805 
   1806 				parse_band_iftype_data(band, &nested);
   1807 				break;
   1808 			}
   1809 		}
   1810 
   1811 		if (*bandp == NULL)
   1812 			*bandp = band;
   1813 	}
   1814 }
   1815 
   1816 static void parse_supported_iftypes(struct wiphy *wiphy,
   1817 						struct l_genl_attr *attr)
   1818 {
   1819 	uint16_t type, len;
   1820 	const void *data;
   1821 
   1822 	while (l_genl_attr_next(attr, &type, &len, &data)) {
   1823 		/*
   1824 		 * NL80211_IFTYPE_UNSPECIFIED can be ignored, so we start
   1825 		 * at the first bit
   1826 		 */
   1827 		if (type > sizeof(wiphy->supported_iftypes) * 8) {
   1828 			l_warn("unsupported iftype: %u", type);
   1829 			continue;
   1830 		}
   1831 
   1832 		wiphy->supported_iftypes |= 1 << (type - 1);
   1833 	}
   1834 }
   1835 
   1836 static void parse_iftype_extended_capabilities(struct wiphy *wiphy,
   1837 						struct l_genl_attr *attr)
   1838 {
   1839 	uint16_t type;
   1840 	uint16_t len;
   1841 	const void *data;
   1842 	struct l_genl_attr nested;
   1843 
   1844 	while (l_genl_attr_next(attr, &type, &len, &data)) {
   1845 		uint32_t iftype;
   1846 
   1847 		if (!l_genl_attr_recurse(attr, &nested))
   1848 			continue;
   1849 
   1850 		if (!l_genl_attr_next(&nested, &type, &len, &data))
   1851 			continue;
   1852 
   1853 		if (type != NL80211_ATTR_IFTYPE)
   1854 			continue;
   1855 
   1856 		iftype = l_get_u32(data);
   1857 
   1858 		if (!l_genl_attr_next(&nested, &type, &len, &data))
   1859 			continue;
   1860 
   1861 		if (type != NL80211_ATTR_EXT_CAPA)
   1862 			continue;
   1863 
   1864 		wiphy->iftype_extended_capabilities[iftype] =
   1865 					l_new(uint8_t, EXT_CAP_LEN + 2);
   1866 		wiphy->iftype_extended_capabilities[iftype][0] =
   1867 					IE_TYPE_EXTENDED_CAPABILITIES;
   1868 		wiphy->iftype_extended_capabilities[iftype][1] =
   1869 					EXT_CAP_LEN;
   1870 		memcpy(wiphy->iftype_extended_capabilities[iftype] + 2,
   1871 				data, minsize(len, EXT_CAP_LEN));
   1872 	}
   1873 }
   1874 
   1875 static void wiphy_parse_attributes(struct wiphy *wiphy,
   1876 					struct l_genl_msg *msg)
   1877 {
   1878 	struct l_genl_attr attr;
   1879 	struct l_genl_attr nested;
   1880 	uint16_t type, len;
   1881 	const void *data;
   1882 
   1883 	if (!l_genl_attr_init(&attr, msg))
   1884 		return;
   1885 
   1886 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
   1887 		switch (type) {
   1888 		case NL80211_ATTR_FEATURE_FLAGS:
   1889 			if (len != sizeof(uint32_t))
   1890 				l_warn("Invalid feature flags attribute");
   1891 			else
   1892 				wiphy->feature_flags = *((uint32_t *) data);
   1893 
   1894 			break;
   1895 		case NL80211_ATTR_EXT_FEATURES:
   1896 			if (len > sizeof(wiphy->ext_features))
   1897 				len = sizeof(wiphy->ext_features);
   1898 
   1899 			memcpy(wiphy->ext_features, data, len);
   1900 			break;
   1901 		case NL80211_ATTR_SUPPORTED_COMMANDS:
   1902 			if (l_genl_attr_recurse(&attr, &nested))
   1903 				parse_supported_commands(wiphy, &nested);
   1904 
   1905 			break;
   1906 		case NL80211_ATTR_CIPHER_SUITES:
   1907 			parse_supported_ciphers(wiphy, data, len);
   1908 			break;
   1909 		case NL80211_ATTR_WIPHY_BANDS:
   1910 			if (l_genl_attr_recurse(&attr, &nested))
   1911 				parse_supported_bands(wiphy, &nested);
   1912 
   1913 			break;
   1914 		case NL80211_ATTR_MAX_NUM_SCAN_SSIDS:
   1915 			if (len != sizeof(uint8_t))
   1916 				l_warn("Invalid MAX_NUM_SCAN_SSIDS attribute");
   1917 			else
   1918 				wiphy->max_num_ssids_per_scan =
   1919 							*((uint8_t *) data);
   1920 			break;
   1921 		case NL80211_ATTR_MAX_SCAN_IE_LEN:
   1922 			if (len != sizeof(uint16_t))
   1923 				l_warn("Invalid MAX_SCAN_IE_LEN attribute");
   1924 			else
   1925 				wiphy->max_scan_ie_len = *((uint16_t *) data);
   1926 			break;
   1927 		case NL80211_ATTR_SUPPORTED_IFTYPES:
   1928 			if (l_genl_attr_recurse(&attr, &nested))
   1929 				parse_supported_iftypes(wiphy, &nested);
   1930 			break;
   1931 		case NL80211_ATTR_OFFCHANNEL_TX_OK:
   1932 			wiphy->offchannel_tx_ok = true;
   1933 			break;
   1934 		case NL80211_ATTR_EXT_CAPA:
   1935 			memcpy(wiphy->extended_capabilities + 2,
   1936 				data, minsize(EXT_CAP_LEN, len));
   1937 			break;
   1938 		case NL80211_ATTR_IFTYPE_EXT_CAPA:
   1939 			if (!l_genl_attr_recurse(&attr, &nested))
   1940 				break;
   1941 
   1942 			parse_iftype_extended_capabilities(wiphy, &nested);
   1943 			break;
   1944 		case NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION:
   1945 			if (len != 4)
   1946 				l_warn("Invalid MAX_ROC_DURATION attribute");
   1947 			else
   1948 				wiphy->max_roc_duration = *((uint32_t *) data);
   1949 			break;
   1950 		case NL80211_ATTR_ROAM_SUPPORT:
   1951 			wiphy->support_fw_roam = true;
   1952 			break;
   1953 		case NL80211_ATTR_WIPHY_SELF_MANAGED_REG:
   1954 			wiphy->self_managed = true;
   1955 			break;
   1956 		case NL80211_ATTR_PROBE_RESP_OFFLOAD:
   1957 			wiphy->ap_probe_resp_offload = true;
   1958 			break;
   1959 		case NL80211_ATTR_SUPPORT_AP_UAPSD:
   1960 			wiphy->supports_uapsd = true;
   1961 			break;
   1962 		}
   1963 	}
   1964 }
   1965 
   1966 static bool wiphy_get_driver_name(struct wiphy *wiphy)
   1967 {
   1968 	L_AUTO_FREE_VAR(char *, driver_link) = NULL;
   1969 	char driver_path[256];
   1970 	ssize_t len;
   1971 	unsigned int i;
   1972 	unsigned int j;
   1973 	const struct l_settings *config = iwd_get_config();
   1974 	char **flag_list;
   1975 	char *driver;
   1976 
   1977 	driver_link = l_strdup_printf("/sys/class/ieee80211/%s/device/driver",
   1978 					wiphy->name);
   1979 	len = readlink(driver_link, driver_path, sizeof(driver_path) - 1);
   1980 
   1981 	if (len == -1) {
   1982 		l_error("Can't read %s: %s", driver_link, strerror(errno));
   1983 		return false;
   1984 	}
   1985 
   1986 	driver_path[len] = '\0';
   1987 
   1988 	driver = memrchr(driver_path, '/', len);
   1989 	wiphy->driver_str = l_strdup(driver ? driver + 1 : driver_path);
   1990 
   1991 	for (i = 0; i < L_ARRAY_SIZE(driver_infos); i++)
   1992 		if (!fnmatch(driver_infos[i].prefix, wiphy->driver_str, 0))
   1993 			wiphy->driver_flags |= driver_infos[i].flags;
   1994 
   1995 	/* Check for any user-defined driver flags */
   1996 	if (!l_settings_has_group(config, "DriverQuirks"))
   1997 		return true;
   1998 
   1999 	for (i = 0; i < L_ARRAY_SIZE(driver_flag_names); i++) {
   2000 		flag_list = l_settings_get_string_list(config, "DriverQuirks",
   2001 						driver_flag_names[i].name, ',');
   2002 		if (!flag_list)
   2003 			continue;
   2004 
   2005 		for (j = 0; flag_list[j]; j++)
   2006 			if (!fnmatch(flag_list[j], wiphy->driver_str, 0))
   2007 				wiphy->driver_flags |=
   2008 						driver_flag_names[i].flag;
   2009 
   2010 		l_strv_free(flag_list);
   2011 	}
   2012 
   2013 	return true;
   2014 }
   2015 
   2016 static int wiphy_get_permanent_addr_from_sysfs(struct wiphy *wiphy)
   2017 {
   2018 	char addr[32];
   2019 	ssize_t len;
   2020 
   2021 	len = read_file(addr, sizeof(addr),
   2022 				"/sys/class/ieee80211/%s/macaddress",
   2023 				wiphy->name);
   2024 	if (len != 18) {
   2025 		if (len < 0)
   2026 			return -errno;
   2027 		return -EINVAL;
   2028 	}
   2029 
   2030 	/* Sysfs appends a \n at the end, strip it */
   2031 	addr[17] = '\0';
   2032 
   2033 	if (!util_string_to_address(addr, wiphy->permanent_addr))
   2034 		return -EINVAL;
   2035 
   2036 	return 0;
   2037 }
   2038 
   2039 static void wiphy_register(struct wiphy *wiphy)
   2040 {
   2041 #ifdef HAVE_DBUS
   2042 	struct l_dbus *dbus = dbus_get_bus();
   2043 #endif
   2044 
   2045 	wiphy->soft_rfkill = rfkill_get_soft_state(wiphy->id);
   2046 	wiphy->hard_rfkill = rfkill_get_hard_state(wiphy->id);
   2047 
   2048 	if (hwdb) {
   2049 		char modalias[128];
   2050 		ssize_t len;
   2051 		struct l_hwdb_entry *entries = NULL, *kv;
   2052 
   2053 		len = read_file(modalias, sizeof(modalias) - 1,
   2054 				"/sys/class/ieee80211/%s/device/modalias",
   2055 				wiphy->name);
   2056 
   2057 		if (len > 0) {
   2058 			modalias[len] = '\0';
   2059 			entries = l_hwdb_lookup(hwdb, "%s", modalias);
   2060 		}
   2061 
   2062 		for (kv = entries; kv; kv = kv->next) {
   2063 			if (!strcmp(kv->key, "ID_MODEL_FROM_DATABASE")) {
   2064 				if (wiphy->model_str)
   2065 					continue;
   2066 
   2067 				wiphy->model_str = l_strdup(kv->value);
   2068 			}
   2069 
   2070 			if (!strcmp(kv->key, "ID_VENDOR_FROM_DATABASE")) {
   2071 				if (wiphy->vendor_str)
   2072 					continue;
   2073 
   2074 				wiphy->vendor_str = l_strdup(kv->value);
   2075 			}
   2076 		}
   2077 
   2078 		l_hwdb_lookup_free(entries);
   2079 	}
   2080 
   2081 	wiphy_get_driver_name(wiphy);
   2082 
   2083 #ifdef HAVE_DBUS
   2084 	if (!l_dbus_object_add_interface(dbus, wiphy_get_path(wiphy),
   2085 					IWD_WIPHY_INTERFACE, wiphy))
   2086 		l_info("Unable to add the %s interface to %s",
   2087 				IWD_WIPHY_INTERFACE, wiphy_get_path(wiphy));
   2088 
   2089 	if (!l_dbus_object_add_interface(dbus, wiphy_get_path(wiphy),
   2090 					L_DBUS_INTERFACE_PROPERTIES, NULL))
   2091 		l_info("Unable to add the %s interface to %s",
   2092 				L_DBUS_INTERFACE_PROPERTIES,
   2093 				wiphy_get_path(wiphy));
   2094 #endif
   2095 
   2096 	wiphy->registered = true;
   2097 }
   2098 
   2099 struct wiphy *wiphy_create(uint32_t wiphy_id, const char *name)
   2100 {
   2101 	struct wiphy *wiphy;
   2102 	struct l_genl *genl = iwd_get_genl();
   2103 
   2104 	wiphy = wiphy_new(wiphy_id);
   2105 	l_strlcpy(wiphy->name, name, sizeof(wiphy->name));
   2106 	wiphy->nl80211 = l_genl_family_new(genl, NL80211_GENL_NAME);
   2107 	l_queue_push_head(wiphy_list, wiphy);
   2108 
   2109 	if (!wiphy_is_managed(name))
   2110 		wiphy->blacklisted = true;
   2111 
   2112 	wiphy->work = l_queue_new();
   2113 
   2114 	return wiphy;
   2115 }
   2116 
   2117 void wiphy_update_from_genl(struct wiphy *wiphy, struct l_genl_msg *msg)
   2118 {
   2119 	if (wiphy->blacklisted)
   2120 		return;
   2121 
   2122 	wiphy_parse_attributes(wiphy, msg);
   2123 }
   2124 
   2125 void wiphy_update_name(struct wiphy *wiphy, const char *name)
   2126 {
   2127 	bool updated = false;
   2128 
   2129 	if (strncmp(wiphy->name, name, sizeof(wiphy->name))) {
   2130 		l_strlcpy(wiphy->name, name, sizeof(wiphy->name));
   2131 		updated = true;
   2132 	}
   2133 
   2134 #ifdef HAVE_DBUS
   2135 	if (updated && wiphy->registered) {
   2136 		struct l_dbus *dbus = dbus_get_bus();
   2137 
   2138 		l_dbus_property_changed(dbus, wiphy_get_path(wiphy),
   2139 					IWD_WIPHY_INTERFACE, "Name");
   2140 	}
   2141 #endif
   2142 }
   2143 
   2144 static void wiphy_set_station_capability_bits(struct wiphy *wiphy)
   2145 {
   2146 	uint8_t *ext_capa;
   2147 	bool anqp_disabled;
   2148 
   2149 	/* No per-type capabilities exist for station, just copy the global */
   2150 	if (!wiphy->iftype_extended_capabilities[NL80211_IFTYPE_STATION]) {
   2151 		wiphy->iftype_extended_capabilities[NL80211_IFTYPE_STATION] =
   2152 					l_new(uint8_t, EXT_CAP_LEN + 2);
   2153 
   2154 		memcpy(wiphy->iftype_extended_capabilities[
   2155 						NL80211_IFTYPE_STATION],
   2156 						wiphy->extended_capabilities,
   2157 						EXT_CAP_LEN + 2);
   2158 	}
   2159 
   2160 	ext_capa = wiphy->iftype_extended_capabilities[NL80211_IFTYPE_STATION];
   2161 
   2162 	if (!l_settings_get_bool(iwd_get_config(), "General", "DisableANQP",
   2163 				&anqp_disabled))
   2164 		anqp_disabled = true;
   2165 
   2166 	/* Set BSS Transition Management */
   2167 	set_bit(ext_capa + 2, 19);
   2168 
   2169 	/* Set Interworking */
   2170 	if (!anqp_disabled)
   2171 		set_bit(ext_capa + 2, 31);
   2172 
   2173 	/* Set QoS Map */
   2174 	if (wiphy->support_qos_set_map)
   2175 		set_bit(ext_capa + 2, 32);
   2176 
   2177 	/* Set FILS */
   2178 	set_bit(ext_capa + 2, 72);
   2179 }
   2180 
   2181 static void wiphy_setup_rm_enabled_capabilities(struct wiphy *wiphy)
   2182 {
   2183 	/* Nothing to do */
   2184 	if (!wiphy_rrm_capable(wiphy))
   2185 		return;
   2186 
   2187 	wiphy->rm_enabled_capabilities[0] = IE_TYPE_RM_ENABLED_CAPABILITIES;
   2188 	wiphy->rm_enabled_capabilities[1] = 5;
   2189 	/* Bits: Passive (4), Active (5), and Beacon Table (6) capabilities */
   2190 	wiphy->rm_enabled_capabilities[2] = 0x70;
   2191 
   2192 	/*
   2193 	 * TODO: Support at least Link Measurement if TX_POWER_INSERTION is
   2194 	 * available
   2195 	 */
   2196 }
   2197 
   2198 static void wiphy_dump_done(void *user_data)
   2199 {
   2200 	struct wiphy *wiphy = user_data;
   2201 	const struct l_queue_entry *e;
   2202 
   2203 	/* This dump was canceled due to another dump */
   2204 	if ((wiphy && !wiphy->dump_id) || (!wiphy && !wiphy_dump_id))
   2205 		return;
   2206 
   2207 	if (wiphy) {
   2208 		wiphy->dump_id = 0;
   2209 
   2210 		WATCHLIST_NOTIFY(&wiphy->state_watches,
   2211 				wiphy_state_watch_func_t, wiphy,
   2212 				WIPHY_STATE_WATCH_EVENT_REGDOM_DONE);
   2213 
   2214 		return;
   2215 	}
   2216 
   2217 	wiphy_dump_id = 0;
   2218 
   2219 	for (e = l_queue_get_entries(wiphy_list); e; e = e->next) {
   2220 		wiphy = e->data;
   2221 
   2222 		if (wiphy->self_managed)
   2223 			continue;
   2224 
   2225 		WATCHLIST_NOTIFY(&wiphy->state_watches,
   2226 				wiphy_state_watch_func_t, wiphy,
   2227 				WIPHY_STATE_WATCH_EVENT_REGDOM_DONE);
   2228 	}
   2229 }
   2230 
   2231 /* We are dumping wiphy(s) due to a regulatory change */
   2232 static void wiphy_dump_callback(struct l_genl_msg *msg,
   2233 						void *user_data)
   2234 {
   2235 	struct wiphy *wiphy;
   2236 	uint32_t id;
   2237 	struct l_genl_attr bands;
   2238 	struct l_genl_attr attr;
   2239 	uint16_t type;
   2240 	struct band *band;
   2241 
   2242 	if (nl80211_parse_attrs(msg, NL80211_ATTR_WIPHY, &id,
   2243 					NL80211_ATTR_WIPHY_BANDS, &bands,
   2244 					NL80211_ATTR_UNSPEC) < 0)
   2245 		return;
   2246 
   2247 	wiphy = wiphy_find(id);
   2248 	if (L_WARN_ON(!wiphy))
   2249 		return;
   2250 
   2251 	/* Unregistered means the wiphy is blacklisted, don't bother parsing */
   2252 	if (!wiphy->registered)
   2253 		return;
   2254 
   2255 	while (l_genl_attr_next(&bands, &type, NULL, NULL)) {
   2256 		switch (type) {
   2257 		case NL80211_BAND_2GHZ:
   2258 			band = wiphy->band_2g;
   2259 			break;
   2260 		case NL80211_BAND_5GHZ:
   2261 			band = wiphy->band_5g;
   2262 			break;
   2263 		case NL80211_BAND_6GHZ:
   2264 			band = wiphy->band_6g;
   2265 			break;
   2266 		default:
   2267 			continue;
   2268 		}
   2269 
   2270 		if (L_WARN_ON(!band))
   2271 			continue;
   2272 
   2273 		if (!l_genl_attr_recurse(&bands, &attr))
   2274 			return;
   2275 
   2276 		while (l_genl_attr_next(&attr, &type, NULL, NULL)) {
   2277 			if (type != NL80211_BAND_ATTR_FREQS)
   2278 				continue;
   2279 
   2280 			/*
   2281 			 * Just write over the old list for each frequency. In
   2282 			 * theory no new frequencies should be added so there
   2283 			 * should never be any stale values.
   2284 			 */
   2285 			nl80211_parse_supported_frequencies(&attr, NULL, band);
   2286 		}
   2287 	}
   2288 }
   2289 
   2290 static bool wiphy_cancel_last_dump(struct wiphy *wiphy)
   2291 {
   2292 	unsigned int id = 0;
   2293 
   2294 	/*
   2295 	 * Zero command ID to signal that wiphy_dump_done doesn't need to do
   2296 	 * anything.
   2297 	 */
   2298 	if (wiphy && wiphy->dump_id) {
   2299 		id = wiphy->dump_id;
   2300 		wiphy->dump_id = 0;
   2301 	} else if (!wiphy && wiphy_dump_id) {
   2302 		id = wiphy_dump_id;
   2303 		wiphy_dump_id = 0;
   2304 	}
   2305 
   2306 	if (id) {
   2307 		l_debug("Canceling pending regdom wiphy dump (%s)",
   2308 					wiphy ? wiphy->name : "global");
   2309 
   2310 		l_genl_family_cancel(nl80211, id);
   2311 	}
   2312 
   2313 	return id != 0;
   2314 }
   2315 
   2316 static void wiphy_dump_after_regdom(struct wiphy *wiphy)
   2317 {
   2318 	const struct l_queue_entry *e;
   2319 	struct l_genl_msg *msg;
   2320 	unsigned int id;
   2321 	bool no_start_event;
   2322 
   2323 	msg = l_genl_msg_new_sized(NL80211_CMD_GET_WIPHY, 128);
   2324 
   2325 	if (wiphy)
   2326 		l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY, 4, &wiphy->id);
   2327 
   2328 	l_genl_msg_append_attr(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP, 0, NULL);
   2329 	id = l_genl_family_dump(nl80211, msg, wiphy_dump_callback,
   2330 						wiphy, wiphy_dump_done);
   2331 	if (!id) {
   2332 		l_error("Wiphy information dump failed");
   2333 		l_genl_msg_unref(msg);
   2334 		return;
   2335 	}
   2336 
   2337 	/*
   2338 	 * Another update while dumping wiphy. This next dump should supersede
   2339 	 * the first and not result in a DONE event until this new dump is
   2340 	 * finished. This is because the disabled frequencies are in an unknown
   2341 	 * state and could cause incorrect behavior by any watchers.
   2342 	 */
   2343 	no_start_event = wiphy_cancel_last_dump(wiphy);
   2344 
   2345 	/* Limited dump so just emit the event for this wiphy */
   2346 	if (wiphy) {
   2347 		wiphy->dump_id = id;
   2348 
   2349 		if (no_start_event)
   2350 			return;
   2351 
   2352 		WATCHLIST_NOTIFY(&wiphy->state_watches,
   2353 				wiphy_state_watch_func_t, wiphy,
   2354 				WIPHY_STATE_WATCH_EVENT_REGDOM_STARTED);
   2355 		return;
   2356 	}
   2357 
   2358 	wiphy_dump_id = id;
   2359 
   2360 	/* Otherwise for a global regdom change notify for all wiphy's */
   2361 	for (e = l_queue_get_entries(wiphy_list); e; e = e->next) {
   2362 		struct wiphy *w = e->data;
   2363 
   2364 		if (w->self_managed)
   2365 			continue;
   2366 
   2367 		if (no_start_event)
   2368 			continue;
   2369 
   2370 		WATCHLIST_NOTIFY(&w->state_watches, wiphy_state_watch_func_t,
   2371 				w, WIPHY_STATE_WATCH_EVENT_REGDOM_STARTED);
   2372 	}
   2373 }
   2374 
   2375 static bool wiphy_update_reg_domain(struct wiphy *wiphy, bool global,
   2376 					struct l_genl_msg *msg)
   2377 {
   2378 	char out_country[2];
   2379 	char *orig;
   2380 
   2381 	/*
   2382 	 * Write the new country code or XX if the reg domain is not a
   2383 	 * country domain.
   2384 	 */
   2385 	if (nl80211_parse_attrs(msg, NL80211_ATTR_REG_ALPHA2, out_country,
   2386 				NL80211_ATTR_UNSPEC) < 0)
   2387 		out_country[0] = out_country[1] = 'X';
   2388 
   2389 	if (global)
   2390 		/*
   2391 		 * Leave @wiphy->regdom_country as all zeros to mean that it
   2392 		 * uses the global @regdom_country, i.e. is not self-managed.
   2393 		 *
   2394 		 * Even if we're called because we queried a new wiphy's
   2395 		 * reg domain, use the value we received here to update our
   2396 		 * global @regdom_country in case this is the first opportunity
   2397 		 * we have to update it -- possibly because this is the first
   2398 		 * wiphy created (that is not self-managed anyway) and we
   2399 		 * haven't received any REG_CHANGE events yet.
   2400 		 */
   2401 		orig = regdom_country;
   2402 
   2403 	else
   2404 		orig = wiphy->regdom_country;
   2405 
   2406 	/*
   2407 	 * The kernel seems to send regdom updates even if the country didn't
   2408 	 * change. Skip these as there is no reason to re-dump.
   2409 	 */
   2410 	if (orig[0] == out_country[0] && orig[1] == out_country[1])
   2411 		return false;
   2412 
   2413 	l_debug("New reg domain country code for %s is %c%c",
   2414 		global ? "(global)" : wiphy->name,
   2415 		out_country[0], out_country[1]);
   2416 
   2417 	orig[0] = out_country[0];
   2418 	orig[1] = out_country[1];
   2419 
   2420 	return true;
   2421 }
   2422 
   2423 static void wiphy_get_reg_cb(struct l_genl_msg *msg, void *user_data)
   2424 {
   2425 	struct wiphy *wiphy = user_data;
   2426 	uint32_t tmp;
   2427 	bool global;
   2428 
   2429 	wiphy->get_reg_id = 0;
   2430 
   2431 	/*
   2432 	 * NL80211_CMD_GET_REG contains an NL80211_ATTR_WIPHY iff the wiphy
   2433 	 * uses a self-managed regulatory domain.
   2434 	 */
   2435 	global = nl80211_parse_attrs(msg, NL80211_ATTR_WIPHY, &tmp,
   2436 				NL80211_ATTR_UNSPEC) < 0;
   2437 
   2438 	wiphy_update_reg_domain(wiphy, global, msg);
   2439 }
   2440 
   2441 static void wiphy_get_reg_domain(struct wiphy *wiphy)
   2442 {
   2443 	struct l_genl_msg *msg;
   2444 
   2445 	msg = l_genl_msg_new(NL80211_CMD_GET_REG);
   2446 	l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY, 4, &wiphy->id);
   2447 
   2448 	wiphy->get_reg_id = l_genl_family_send(wiphy->nl80211, msg,
   2449 						wiphy_get_reg_cb, wiphy, NULL);
   2450 	if (!wiphy->get_reg_id) {
   2451 		l_error("Error sending NL80211_CMD_GET_REG for %s", wiphy->name);
   2452 		l_genl_msg_unref(msg);
   2453 	}
   2454 }
   2455 
   2456 void wiphy_create_complete(struct wiphy *wiphy)
   2457 {
   2458 	/*
   2459 	 * With really bad timing two wiphy dumps can occur (initial and a
   2460 	 * NEW_WIPHY event) and actually register twice. Ignoring/preventing the
   2461 	 * second dump is problematic since it _could_ be a legitimate event so
   2462 	 * instead just prevent it from registering twice.
   2463 	 */
   2464 	if (wiphy->registered)
   2465 		return;
   2466 
   2467 	wiphy_register(wiphy);
   2468 
   2469 	if (l_memeqzero(wiphy->permanent_addr, 6)) {
   2470 		int err = wiphy_get_permanent_addr_from_sysfs(wiphy);
   2471 
   2472 		if (err < 0)
   2473 			l_error("Can't read sysfs maccaddr for %s: %s",
   2474 					wiphy->name, strerror(-err));
   2475 	}
   2476 
   2477 	wiphy_set_station_capability_bits(wiphy);
   2478 	wiphy_setup_rm_enabled_capabilities(wiphy);
   2479 	wiphy_get_reg_domain(wiphy);
   2480 
   2481 	wiphy_print_basic_info(wiphy);
   2482 }
   2483 
   2484 bool wiphy_destroy(struct wiphy *wiphy)
   2485 {
   2486 	l_debug("");
   2487 
   2488 	if (!l_queue_remove(wiphy_list, wiphy))
   2489 		return false;
   2490 
   2491 #ifdef HAVE_DBUS
   2492 	if (wiphy->registered)
   2493 		l_dbus_unregister_object(dbus_get_bus(), wiphy_get_path(wiphy));
   2494 #endif
   2495 
   2496 	wiphy_free(wiphy);
   2497 	return true;
   2498 }
   2499 
   2500 static void wiphy_rfkill_cb(unsigned int wiphy_id, bool soft, bool hard,
   2501 				void *user_data)
   2502 {
   2503 	struct wiphy *wiphy = wiphy_find(wiphy_id);
   2504 #ifdef HAVE_DBUS
   2505 	struct l_dbus *dbus = dbus_get_bus();
   2506 #endif
   2507 	bool old_powered, new_powered;
   2508 	enum wiphy_state_watch_event event;
   2509 
   2510 	if (!wiphy)
   2511 		return;
   2512 
   2513 	old_powered = !wiphy->soft_rfkill && !wiphy->hard_rfkill;
   2514 
   2515 	wiphy->soft_rfkill = soft;
   2516 	wiphy->hard_rfkill = hard;
   2517 
   2518 	new_powered = !wiphy->soft_rfkill && !wiphy->hard_rfkill;
   2519 
   2520 	if (old_powered == new_powered)
   2521 		return;
   2522 
   2523 	event = new_powered ? WIPHY_STATE_WATCH_EVENT_POWERED :
   2524 				WIPHY_STATE_WATCH_EVENT_RFKILLED;
   2525 	WATCHLIST_NOTIFY(&wiphy->state_watches, wiphy_state_watch_func_t,
   2526 				wiphy, event);
   2527 
   2528 #ifdef HAVE_DBUS
   2529 	l_dbus_property_changed(dbus, wiphy_get_path(wiphy),
   2530 					IWD_WIPHY_INTERFACE, "Powered");
   2531 #endif
   2532 }
   2533 
   2534 static bool wiphy_property_get_powered(struct l_dbus *dbus,
   2535 					struct l_dbus_message *message,
   2536 					struct l_dbus_message_builder *builder,
   2537 					void *user_data)
   2538 {
   2539 	struct wiphy *wiphy = user_data;
   2540 	bool value = !wiphy->soft_rfkill && !wiphy->hard_rfkill;
   2541 
   2542 	l_dbus_message_builder_append_basic(builder, 'b', &value);
   2543 
   2544 	return true;
   2545 }
   2546 
   2547 static struct l_dbus_message *wiphy_property_set_powered(struct l_dbus *dbus,
   2548 					struct l_dbus_message *message,
   2549 					struct l_dbus_message_iter *new_value,
   2550 					l_dbus_property_complete_cb_t complete,
   2551 					void *user_data)
   2552 {
   2553 	struct wiphy *wiphy = user_data;
   2554 	bool old_powered, new_powered;
   2555 
   2556 	if (!l_dbus_message_iter_get_variant(new_value, "b", &new_powered))
   2557 		return dbus_error_invalid_args(message);
   2558 
   2559 	old_powered = !wiphy->soft_rfkill && !wiphy->hard_rfkill;
   2560 
   2561 	if (old_powered == new_powered)
   2562 		goto done;
   2563 
   2564 	if (wiphy->hard_rfkill)
   2565 		return dbus_error_not_available(message);
   2566 
   2567 	if (!rfkill_set_soft_state(wiphy->id, !new_powered))
   2568 		return dbus_error_failed(message);
   2569 
   2570 done:
   2571 	complete(dbus, message, NULL);
   2572 
   2573 	return NULL;
   2574 }
   2575 
   2576 static bool wiphy_property_get_model(struct l_dbus *dbus,
   2577 					struct l_dbus_message *message,
   2578 					struct l_dbus_message_builder *builder,
   2579 					void *user_data)
   2580 {
   2581 	struct wiphy *wiphy = user_data;
   2582 
   2583 	if (!wiphy->model_str)
   2584 		return false;
   2585 
   2586 	l_dbus_message_builder_append_basic(builder, 's', wiphy->model_str);
   2587 
   2588 	return true;
   2589 }
   2590 
   2591 static bool wiphy_property_get_vendor(struct l_dbus *dbus,
   2592 					struct l_dbus_message *message,
   2593 					struct l_dbus_message_builder *builder,
   2594 					void *user_data)
   2595 {
   2596 	struct wiphy *wiphy = user_data;
   2597 
   2598 	if (!wiphy->vendor_str)
   2599 		return false;
   2600 
   2601 	l_dbus_message_builder_append_basic(builder, 's', wiphy->vendor_str);
   2602 
   2603 	return true;
   2604 }
   2605 
   2606 static bool wiphy_property_get_name(struct l_dbus *dbus,
   2607 					struct l_dbus_message *message,
   2608 					struct l_dbus_message_builder *builder,
   2609 					void *user_data)
   2610 {
   2611 	struct wiphy *wiphy = user_data;
   2612 	char buf[20];
   2613 
   2614 	if (l_utf8_validate(wiphy->name, strlen(wiphy->name), NULL)) {
   2615 		l_dbus_message_builder_append_basic(builder, 's', wiphy->name);
   2616 		return true;
   2617 	}
   2618 
   2619 	/*
   2620 	 * In the highly unlikely scenario that the wiphy name is not utf8,
   2621 	 * we simply use the canonical name phy<index>.  The kernel guarantees
   2622 	 * that this name cannot be taken by any other wiphy, so this should
   2623 	 * be safe enough.
   2624 	 */
   2625 	sprintf(buf, "phy%d", wiphy->id);
   2626 	l_dbus_message_builder_append_basic(builder, 's', buf);
   2627 
   2628 	return true;
   2629 }
   2630 
   2631 #define WIPHY_MODE_MASK	( \
   2632 	(1 << (NL80211_IFTYPE_STATION - 1)) | \
   2633 	(1 << (NL80211_IFTYPE_AP - 1)) | \
   2634 	(1 << (NL80211_IFTYPE_ADHOC - 1)))
   2635 
   2636 static bool wiphy_property_get_supported_modes(struct l_dbus *dbus,
   2637 					struct l_dbus_message *message,
   2638 					struct l_dbus_message_builder *builder,
   2639 					void *user_data)
   2640 {
   2641 	struct wiphy *wiphy = user_data;
   2642 	unsigned int j = 0;
   2643 	char **iftypes = wiphy_get_supported_iftypes(wiphy, WIPHY_MODE_MASK);
   2644 
   2645 	l_dbus_message_builder_enter_array(builder, "s");
   2646 
   2647 	while (iftypes[j])
   2648 		l_dbus_message_builder_append_basic(builder, 's', iftypes[j++]);
   2649 
   2650 	l_dbus_message_builder_leave_array(builder);
   2651 	l_strfreev(iftypes);
   2652 
   2653 	return true;
   2654 }
   2655 
   2656 static void setup_wiphy_interface(struct l_dbus_interface *interface)
   2657 {
   2658 	l_dbus_interface_property(interface, "Powered", 0, "b",
   2659 					wiphy_property_get_powered,
   2660 					wiphy_property_set_powered);
   2661 	l_dbus_interface_property(interface, "Model", 0, "s",
   2662 					wiphy_property_get_model, NULL);
   2663 	l_dbus_interface_property(interface, "Vendor", 0, "s",
   2664 					wiphy_property_get_vendor, NULL);
   2665 	l_dbus_interface_property(interface, "Name", 0, "s",
   2666 					wiphy_property_get_name, NULL);
   2667 	l_dbus_interface_property(interface, "SupportedModes", 0, "as",
   2668 					wiphy_property_get_supported_modes,
   2669 					NULL);
   2670 }
   2671 
   2672 static void wiphy_reg_notify(struct l_genl_msg *msg, void *user_data)
   2673 {
   2674 	uint8_t cmd = l_genl_msg_get_command(msg);
   2675 	struct wiphy *wiphy = NULL;
   2676 	uint32_t wiphy_id;
   2677 
   2678 	l_debug("Notification of command %s(%u)",
   2679 		nl80211cmd_to_string(cmd), cmd);
   2680 
   2681 	switch (cmd) {
   2682 	case NL80211_CMD_REG_CHANGE:
   2683 		if (!wiphy_update_reg_domain(NULL, true, msg))
   2684 			return;
   2685 		break;
   2686 	case NL80211_CMD_WIPHY_REG_CHANGE:
   2687 		if (nl80211_parse_attrs(msg, NL80211_ATTR_WIPHY, &wiphy_id,
   2688 					NL80211_ATTR_UNSPEC) < 0)
   2689 			return;
   2690 
   2691 		wiphy = wiphy_find(wiphy_id);
   2692 		if (!wiphy)
   2693 			return;
   2694 
   2695 		if (!wiphy_update_reg_domain(wiphy, false, msg))
   2696 			return;
   2697 
   2698 		break;
   2699 	default:
   2700 		return;
   2701 	}
   2702 
   2703 	wiphy_dump_after_regdom(wiphy);
   2704 }
   2705 
   2706 static void wiphy_radio_work_next(struct wiphy *wiphy)
   2707 {
   2708 	struct wiphy_radio_work_item *work;
   2709 	bool done;
   2710 	uint32_t id;
   2711 
   2712 	work = l_queue_pop_head(wiphy->work);
   2713 	if (!work)
   2714 		return;
   2715 
   2716 	id = work->id;
   2717 
   2718 	l_debug("Starting work item %u", work->id);
   2719 
   2720 	wiphy->work_in_callback = true;
   2721 	done = work->ops->do_work(work);
   2722 	wiphy->work_in_callback = false;
   2723 
   2724 	if (done) {
   2725 		/* Item was rescheduled, don't destroy */
   2726 		if (work->id != id)
   2727 			goto next;
   2728 
   2729 		work->id = 0;
   2730 
   2731 		wiphy->work_in_callback = true;
   2732 		destroy_work(work);
   2733 		wiphy->work_in_callback = false;
   2734 
   2735 next:
   2736 		wiphy_radio_work_next(wiphy);
   2737 	} else {
   2738 		/*
   2739 		 * Ensures no other work item will get inserted before this one
   2740 		 * while the work is being done.
   2741 		 */
   2742 		work->priority = INT_MIN;
   2743 		l_queue_push_head(wiphy->work, work);
   2744 	}
   2745 }
   2746 
   2747 static int insert_by_priority(const void *a, const void *b, void *user_data)
   2748 {
   2749 	const struct wiphy_radio_work_item *new = a;
   2750 	const struct wiphy_radio_work_item *work = b;
   2751 
   2752 	if (work->priority <= new->priority)
   2753 		return 1;
   2754 
   2755 	return -1;
   2756 }
   2757 
   2758 uint32_t wiphy_radio_work_insert(struct wiphy *wiphy,
   2759 				struct wiphy_radio_work_item *item,
   2760 				int priority,
   2761 				const struct wiphy_radio_work_item_ops *ops)
   2762 {
   2763 	item->priority = priority;
   2764 	item->ops = ops;
   2765 	item->id = ++work_ids;
   2766 
   2767 	l_debug("Inserting work item %u", item->id);
   2768 
   2769 	l_queue_insert(wiphy->work, item, insert_by_priority, NULL);
   2770 
   2771 	if (l_queue_length(wiphy->work) == 1 && !wiphy->work_in_callback)
   2772 		wiphy_radio_work_next(wiphy);
   2773 
   2774 	return item->id;
   2775 }
   2776 
   2777 static bool match_id(const void *a, const void *b)
   2778 {
   2779 	const struct wiphy_radio_work_item *item = a;
   2780 
   2781 	if (item->id == L_PTR_TO_UINT(b))
   2782 		return true;
   2783 
   2784 	return false;
   2785 }
   2786 
   2787 void wiphy_radio_work_done(struct wiphy *wiphy, uint32_t id)
   2788 {
   2789 	struct wiphy_radio_work_item *item;
   2790 	bool next = false;
   2791 
   2792 	item = l_queue_peek_head(wiphy->work);
   2793 	if (!item)
   2794 		return;
   2795 
   2796 	if (item->id == id) {
   2797 		next = true;
   2798 		l_queue_pop_head(wiphy->work);
   2799 	} else
   2800 		item = l_queue_remove_if(wiphy->work, match_id,
   2801 						L_UINT_TO_PTR(id));
   2802 	if (!item)
   2803 		return;
   2804 
   2805 	l_debug("Work item %u done", id);
   2806 
   2807 	item->id = 0;
   2808 
   2809 	wiphy->work_in_callback = true;
   2810 	destroy_work(item);
   2811 	wiphy->work_in_callback = false;
   2812 
   2813 	if (next)
   2814 		wiphy_radio_work_next(wiphy);
   2815 }
   2816 
   2817 int wiphy_radio_work_is_running(struct wiphy *wiphy, uint32_t id)
   2818 {
   2819 	struct wiphy_radio_work_item *item = l_queue_find(wiphy->work, match_id,
   2820 							L_UINT_TO_PTR(id));
   2821 	if (!item)
   2822 		return -ENOENT;
   2823 
   2824 	return item == l_queue_peek_head(wiphy->work) ? 1 : 0;
   2825 }
   2826 
   2827 uint32_t wiphy_radio_work_reschedule(struct wiphy *wiphy,
   2828 					struct wiphy_radio_work_item *item)
   2829 {
   2830 	/*
   2831 	 * This should only be called from within the do_work callback, meaning
   2832 	 * the item should not be in the queue. Any re-insertion on a running
   2833 	 * item after do_work is not allowed.
   2834 	 */
   2835 	if (L_WARN_ON(wiphy_radio_work_is_running(wiphy, item->id) != -ENOENT))
   2836 		return 0;
   2837 
   2838 	work_ids++;
   2839 
   2840 	l_debug("Rescheduling work item %u, new id %u", item->id, work_ids);
   2841 
   2842 	item->id = work_ids;
   2843 
   2844 	l_queue_insert(wiphy->work, item, insert_by_priority, NULL);
   2845 
   2846 	return item->id;
   2847 }
   2848 
   2849 static int wiphy_init(void)
   2850 {
   2851 	struct l_genl *genl = iwd_get_genl();
   2852 	const struct l_settings *config = iwd_get_config();
   2853 	const char *whitelist = iwd_get_phy_whitelist();
   2854 	const char *blacklist = iwd_get_phy_blacklist();
   2855 	const char *s;
   2856 
   2857 	nl80211 = l_genl_family_new(genl, NL80211_GENL_NAME);
   2858 
   2859 	/*
   2860 	 * This is an extra sanity check so that no memory is leaked
   2861 	 * in case the generic netlink handling gets confused.
   2862 	 */
   2863 	if (wiphy_list) {
   2864 		l_warn("Destroying existing list of wiphy devices");
   2865 		l_queue_destroy(wiphy_list, NULL);
   2866 	}
   2867 
   2868 	wiphy_list = l_queue_new();
   2869 
   2870 	rfkill_watch_add(wiphy_rfkill_cb, NULL);
   2871 
   2872 #ifdef HAVE_DBUS
   2873 	if (!l_dbus_register_interface(dbus_get_bus(),
   2874 					IWD_WIPHY_INTERFACE,
   2875 					setup_wiphy_interface,
   2876 					NULL, false))
   2877 		l_error("Unable to register the %s interface",
   2878 				IWD_WIPHY_INTERFACE);
   2879 #endif
   2880 
   2881 	hwdb = l_hwdb_new_default();
   2882 
   2883 	if (whitelist)
   2884 		whitelist_filter = l_strsplit(whitelist, ',');
   2885 
   2886 	if (blacklist)
   2887 		blacklist_filter = l_strsplit(blacklist, ',');
   2888 
   2889 	s = l_settings_get_value(config, "General",
   2890 						"AddressRandomizationRange");
   2891 	if (s) {
   2892 		if (!strcmp(s, "nic"))
   2893 			mac_randomize_bytes = 3;
   2894 		else if (!strcmp(s, "full"))
   2895 			mac_randomize_bytes = 6;
   2896 		else
   2897 			l_warn("Invalid [General].AddressRandomizationRange"
   2898 				" value: %s", s);
   2899 	}
   2900 
   2901 	if (!l_genl_family_register(nl80211, NL80211_MULTICAST_GROUP_REG,
   2902 					wiphy_reg_notify, NULL, NULL))
   2903 		l_error("Registering for regulatory notifications failed");
   2904 
   2905 	return 0;
   2906 }
   2907 
   2908 static void wiphy_exit(void)
   2909 {
   2910 	l_strfreev(whitelist_filter);
   2911 	l_strfreev(blacklist_filter);
   2912 
   2913 	if (wiphy_dump_id) {
   2914 		l_genl_family_cancel(nl80211, wiphy_dump_id);
   2915 		wiphy_dump_id = 0;
   2916 	}
   2917 
   2918 	l_queue_destroy(wiphy_list, wiphy_free);
   2919 	wiphy_list = NULL;
   2920 
   2921 	l_genl_family_free(nl80211);
   2922 	nl80211 = NULL;
   2923 	mac_randomize_bytes = 6;
   2924 
   2925 #ifdef HAVE_DBUS
   2926 	l_dbus_unregister_interface(dbus_get_bus(), IWD_WIPHY_INTERFACE);
   2927 #endif
   2928 
   2929 	l_hwdb_unref(hwdb);
   2930 }
   2931 
   2932 IWD_MODULE(wiphy, wiphy_init, wiphy_exit);
   2933 IWD_MODULE_DEPENDS(wiphy, rfkill);