eiwd

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

nl80211util.c (19336B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2018-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 #include <errno.h>
     28 #include <linux/if_ether.h>
     29 #include <ell/ell.h>
     30 
     31 #include "linux/nl80211.h"
     32 
     33 #include "src/nl80211util.h"
     34 #include "src/band.h"
     35 #include "src/ie.h"
     36 #include "src/handshake.h"
     37 #include "src/util.h"
     38 
     39 typedef bool (*attr_handler)(const void *data, uint16_t len, void *o);
     40 typedef attr_handler (*handler_for_type)(int type);
     41 
     42 static bool extract_ifindex(const void *data, uint16_t len, void *o)
     43 {
     44 	uint32_t *out = o;
     45 
     46 	if (len != 4)
     47 		return false;
     48 
     49 	/* ifindex cannot be 0 */
     50 	if (l_get_u32(data) == 0)
     51 		return false;
     52 
     53 	*out = l_get_u32(data);
     54 	return true;
     55 }
     56 
     57 static bool extract_name(const void *data, uint16_t len, void *o)
     58 {
     59 	const char **out = o;
     60 
     61 	if (len < 1)
     62 		return false;
     63 
     64 	if (!memchr(data + 1, 0, len - 1))
     65 		return false;
     66 
     67 	*out = data;
     68 	return true;
     69 }
     70 
     71 static bool extract_2_chars(const void *data, uint16_t len, void *o)
     72 {
     73 	char *out = o;
     74 	const char *in = data;
     75 
     76 	if (len != 3 || in[2] != 0)
     77 		return false;
     78 
     79 	out[0] = in[0];
     80 	out[1] = in[1];
     81 	return true;
     82 }
     83 
     84 static bool extract_mac(const void *data, uint16_t len, void *o)
     85 {
     86 	const uint8_t **out = o;
     87 
     88 	if (len != 6)
     89 		return false;
     90 
     91 	*out = data;
     92 	return true;
     93 }
     94 
     95 static bool extract_uint64(const void *data, uint16_t len, void *o)
     96 {
     97 	uint64_t *out = o;
     98 
     99 	if (len != 8)
    100 		return false;
    101 
    102 	*out = l_get_u64(data);
    103 	return true;
    104 }
    105 
    106 static bool extract_uint32(const void *data, uint16_t len, void *o)
    107 {
    108 	uint32_t *out = o;
    109 
    110 	if (len != 4)
    111 		return false;
    112 
    113 	*out = l_get_u32(data);
    114 	return true;
    115 }
    116 
    117 static bool extract_flag(const void *data, uint16_t len, void *o)
    118 {
    119 	if (len != 0)
    120 		return false;
    121 
    122 	return true;
    123 }
    124 
    125 static bool extract_iovec(const void *data, uint16_t len, void *o)
    126 {
    127 	struct iovec *iov = o;
    128 
    129 	iov->iov_base = (void *) data;
    130 	iov->iov_len = len;
    131 
    132 	return true;
    133 }
    134 
    135 static bool extract_ssid(const void *data, uint16_t len, void *o)
    136 {
    137 	if (!len || len > SSID_MAX_SIZE)
    138 		return false;
    139 
    140 	return extract_iovec(data, len, o);
    141 }
    142 
    143 static bool extract_nested(const void *data, uint16_t len, void *o)
    144 {
    145 	const struct l_genl_attr *outer = data;
    146 	struct l_genl_attr *nested = o;
    147 
    148 	return l_genl_attr_recurse(outer, nested);
    149 }
    150 
    151 static bool extract_u8(const void *data, uint16_t len, void *o)
    152 {
    153 	uint8_t *out = o;
    154 
    155 	if (len != 1)
    156 		return false;
    157 
    158 	*out = l_get_u8(data);
    159 	return true;
    160 }
    161 
    162 static attr_handler handler_for_nl80211(int type)
    163 {
    164 	switch (type) {
    165 	case NL80211_ATTR_IFINDEX:
    166 		return extract_ifindex;
    167 	case NL80211_ATTR_WIPHY:
    168 	case NL80211_ATTR_IFTYPE:
    169 	case NL80211_ATTR_KEY_TYPE:
    170 	case NL80211_ATTR_SCAN_FLAGS:
    171 		return extract_uint32;
    172 	case NL80211_ATTR_WDEV:
    173 	case NL80211_ATTR_COOKIE:
    174 		return extract_uint64;
    175 	case NL80211_ATTR_IFNAME:
    176 	case NL80211_ATTR_WIPHY_NAME:
    177 		return extract_name;
    178 	case NL80211_ATTR_REG_ALPHA2:
    179 		return extract_2_chars;
    180 	case NL80211_ATTR_MAC:
    181 	case NL80211_ATTR_BSSID:
    182 		return extract_mac;
    183 	case NL80211_ATTR_ACK:
    184 		return extract_flag;
    185 	case NL80211_ATTR_WIPHY_FREQ:
    186 	case NL80211_ATTR_WIPHY_FREQ_OFFSET:
    187 	case NL80211_ATTR_WIPHY_CHANNEL_TYPE:
    188 	case NL80211_ATTR_CHANNEL_WIDTH:
    189 	case NL80211_ATTR_CENTER_FREQ1:
    190 	case NL80211_ATTR_CENTER_FREQ2:
    191 	case NL80211_ATTR_AKM_SUITES:
    192 	case NL80211_ATTR_EXTERNAL_AUTH_ACTION:
    193 	case NL80211_ATTR_TIMEOUT:
    194 		return extract_uint32;
    195 	case NL80211_ATTR_FRAME:
    196 		return extract_iovec;
    197 	case NL80211_ATTR_SSID:
    198 		return extract_ssid;
    199 	case NL80211_ATTR_WIPHY_BANDS:
    200 	case NL80211_ATTR_SURVEY_INFO:
    201 	case NL80211_ATTR_KEY:
    202 		return extract_nested;
    203 	case NL80211_ATTR_KEY_IDX:
    204 		return extract_u8;
    205 	default:
    206 		break;
    207 	}
    208 
    209 	return NULL;
    210 }
    211 
    212 static attr_handler handler_for_survey_info(int type)
    213 {
    214 	switch (type) {
    215 	case NL80211_SURVEY_INFO_NOISE:
    216 		return extract_u8;
    217 	case NL80211_SURVEY_INFO_FREQUENCY:
    218 		return extract_uint32;
    219 	default:
    220 		break;
    221 	}
    222 
    223 	return NULL;
    224 }
    225 
    226 static attr_handler handler_for_key(int type)
    227 {
    228 	switch (type) {
    229 	case NL80211_KEY_SEQ:
    230 		return extract_iovec;
    231 	default:
    232 		break;
    233 	}
    234 
    235 	return NULL;
    236 }
    237 
    238 struct attr_entry {
    239 	uint16_t type;
    240 	void *data;
    241 	attr_handler handler;
    242 	bool present : 1;
    243 };
    244 
    245 static int parse_attrs(struct l_genl_attr *attr, handler_for_type handler,
    246 			int tag, va_list args)
    247 {
    248 	struct l_queue *entries;
    249 	const struct l_queue_entry *e;
    250 	struct attr_entry *entry;
    251 	uint16_t type;
    252 	uint16_t len;
    253 	const void *data;
    254 	int ret;
    255 
    256 	entries = l_queue_new();
    257 	ret = -ENOSYS;
    258 
    259 	while (tag != NL80211_ATTR_UNSPEC) {
    260 		entry = l_new(struct attr_entry, 1);
    261 
    262 		entry->type = tag;
    263 		entry->data = va_arg(args, void *);
    264 		entry->handler = handler(tag);
    265 		l_queue_push_tail(entries, entry);
    266 
    267 		if (!entry->handler)
    268 			goto done;
    269 
    270 		tag = va_arg(args, enum nl80211_attrs);
    271 	}
    272 
    273 	while (l_genl_attr_next(attr, &type, &len, &data)) {
    274 		for (e = l_queue_get_entries(entries); e; e = e->next) {
    275 			entry = e->data;
    276 
    277 			if (entry->type == type)
    278 				break;
    279 		}
    280 
    281 		if (!e)
    282 			continue;
    283 
    284 		if (entry->present) {
    285 			ret = -EALREADY;
    286 			goto done;
    287 		}
    288 
    289 		/* For nested attributes use the outer attribute as data */
    290 		if (entry->handler == extract_nested)
    291 			data = attr;
    292 
    293 		if (!entry->handler(data, len, entry->data)) {
    294 			ret = -EINVAL;
    295 			goto done;
    296 		}
    297 
    298 		entry->present = true;
    299 	}
    300 
    301 	ret = -ENOENT;
    302 
    303 	for (e = l_queue_get_entries(entries); e; e = e->next) {
    304 		entry = e->data;
    305 
    306 		if (entry->handler == extract_flag) {
    307 			*(bool *) entry->data = entry->present;
    308 			continue;
    309 		}
    310 
    311 		if (entry->present)
    312 			continue;
    313 
    314 		goto done;
    315 	}
    316 
    317 	ret = 0;
    318 
    319 done:
    320 	l_queue_destroy(entries, l_free);
    321 	return ret;
    322 }
    323 
    324 int nl80211_parse_attrs(struct l_genl_msg *msg, int tag, ...)
    325 {
    326 	struct l_genl_attr attr;
    327 	va_list args;
    328 	int ret;
    329 
    330 	if (!l_genl_attr_init(&attr, msg))
    331 		return -EINVAL;
    332 
    333 	va_start(args, tag);
    334 
    335 	ret = parse_attrs(&attr, handler_for_nl80211, tag, args);
    336 
    337 	va_end(args);
    338 
    339 	return ret;
    340 }
    341 
    342 int nl80211_parse_nested(struct l_genl_attr *attr, int type, int tag, ...)
    343 {
    344 	handler_for_type handler;
    345 	va_list args;
    346 	int ret;
    347 
    348 	switch (type) {
    349 	case NL80211_ATTR_SURVEY_INFO:
    350 		handler = handler_for_survey_info;
    351 		break;
    352 	case NL80211_ATTR_KEY:
    353 		handler = handler_for_key;
    354 		break;
    355 	default:
    356 		return -ENOTSUP;
    357 	}
    358 
    359 	va_start(args, tag);
    360 
    361 	ret = parse_attrs(attr, handler, tag, args);
    362 
    363 	va_end(args);
    364 
    365 	return ret;
    366 }
    367 
    368 struct l_genl_msg *nl80211_build_deauthenticate(uint32_t ifindex,
    369 						const uint8_t addr[static 6],
    370 						uint16_t reason_code)
    371 {
    372 	struct l_genl_msg *msg;
    373 
    374 	msg = l_genl_msg_new_sized(NL80211_CMD_DEAUTHENTICATE, 128);
    375 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &ifindex);
    376 	l_genl_msg_append_attr(msg, NL80211_ATTR_REASON_CODE, 2, &reason_code);
    377 	l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    378 
    379 	return msg;
    380 }
    381 
    382 struct l_genl_msg *nl80211_build_disconnect(uint32_t ifindex,
    383 							uint16_t reason_code)
    384 {
    385 	struct l_genl_msg *msg;
    386 
    387 	msg = l_genl_msg_new_sized(NL80211_CMD_DISCONNECT, 64);
    388 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &ifindex);
    389 	l_genl_msg_append_attr(msg, NL80211_ATTR_REASON_CODE, 2, &reason_code);
    390 
    391 	return msg;
    392 }
    393 
    394 struct l_genl_msg *nl80211_build_del_station(uint32_t ifindex,
    395 						const uint8_t addr[static 6],
    396 						uint16_t reason_code,
    397 						uint8_t subtype)
    398 {
    399 	struct l_genl_msg *msg;
    400 
    401 	msg = l_genl_msg_new_sized(NL80211_CMD_DEL_STATION, 64);
    402 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &ifindex);
    403 	l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, 6, addr);
    404 	l_genl_msg_append_attr(msg, NL80211_ATTR_MGMT_SUBTYPE, 1, &subtype);
    405 	l_genl_msg_append_attr(msg, NL80211_ATTR_REASON_CODE, 2, &reason_code);
    406 
    407 	return msg;
    408 }
    409 
    410 struct l_genl_msg *nl80211_build_new_key_group(uint32_t ifindex, uint32_t cipher,
    411 					uint8_t key_id, const uint8_t *key,
    412 					size_t key_len, const uint8_t *ctr,
    413 					size_t ctr_len, const uint8_t *addr)
    414 {
    415 	struct l_genl_msg *msg;
    416 	uint32_t type = NL80211_KEYTYPE_GROUP;
    417 
    418 	msg = l_genl_msg_new_sized(NL80211_CMD_NEW_KEY, 512);
    419 
    420 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &ifindex);
    421 
    422 	if (addr)
    423 		l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    424 
    425 	l_genl_msg_enter_nested(msg, NL80211_ATTR_KEY);
    426 	l_genl_msg_append_attr(msg, NL80211_KEY_DATA, key_len, key);
    427 	l_genl_msg_append_attr(msg, NL80211_KEY_CIPHER, 4, &cipher);
    428 	l_genl_msg_append_attr(msg, NL80211_KEY_IDX, 1, &key_id);
    429 
    430 	if (ctr)
    431 		l_genl_msg_append_attr(msg, NL80211_KEY_SEQ, ctr_len, ctr);
    432 
    433 	l_genl_msg_append_attr(msg, NL80211_KEY_TYPE, 4, &type);
    434 	l_genl_msg_enter_nested(msg, NL80211_KEY_DEFAULT_TYPES);
    435 	l_genl_msg_append_attr(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST,
    436 					0, NULL);
    437 	l_genl_msg_leave_nested(msg);
    438 	l_genl_msg_leave_nested(msg);
    439 
    440 	return msg;
    441 }
    442 
    443 struct l_genl_msg *nl80211_build_new_key_pairwise(uint32_t ifindex,
    444 						uint32_t cipher,
    445 						const uint8_t addr[static 6],
    446 						const uint8_t *tk,
    447 						size_t tk_len,
    448 						uint8_t key_id)
    449 {
    450 	struct l_genl_msg *msg;
    451 
    452 	msg = l_genl_msg_new_sized(NL80211_CMD_NEW_KEY, 512);
    453 
    454 	l_genl_msg_append_attr(msg, NL80211_ATTR_KEY_DATA, tk_len, tk);
    455 	l_genl_msg_append_attr(msg, NL80211_ATTR_KEY_CIPHER, 4, &cipher);
    456 	l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    457 	l_genl_msg_append_attr(msg, NL80211_ATTR_KEY_IDX, 1, &key_id);
    458 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &ifindex);
    459 
    460 	return msg;
    461 }
    462 
    463 struct l_genl_msg *nl80211_build_new_rx_key_pairwise(uint32_t ifindex,
    464 						uint32_t cipher,
    465 						const uint8_t addr[static 6],
    466 						const uint8_t *tk,
    467 						size_t tk_len,
    468 						uint8_t key_id)
    469 {
    470 	uint8_t key_mode = NL80211_KEY_NO_TX;
    471 	uint32_t key_type = NL80211_KEYTYPE_PAIRWISE;
    472 	struct l_genl_msg *msg;
    473 
    474 	msg = l_genl_msg_new_sized(NL80211_CMD_NEW_KEY, 512);
    475 
    476 	l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    477 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &ifindex);
    478 
    479 	l_genl_msg_enter_nested(msg, NL80211_ATTR_KEY);
    480 
    481 	l_genl_msg_append_attr(msg, NL80211_KEY_DATA, tk_len, tk);
    482 	l_genl_msg_append_attr(msg, NL80211_KEY_CIPHER, 4, &cipher);
    483 	l_genl_msg_append_attr(msg, NL80211_KEY_IDX, 1, &key_id);
    484 	l_genl_msg_append_attr(msg, NL80211_KEY_MODE, 1, &key_mode);
    485 	l_genl_msg_append_attr(msg, NL80211_KEY_TYPE, 4, &key_type);
    486 
    487 	l_genl_msg_leave_nested(msg);
    488 
    489 	return msg;
    490 }
    491 
    492 struct l_genl_msg *nl80211_build_rekey_offload(uint32_t ifindex,
    493 						const uint8_t *kek,
    494 						const uint8_t *kck,
    495 						uint64_t replay_ctr)
    496 {
    497 	struct l_genl_msg *msg;
    498 
    499 	msg = l_genl_msg_new_sized(NL80211_CMD_SET_REKEY_OFFLOAD, 512);
    500 
    501 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &ifindex);
    502 
    503 	l_genl_msg_enter_nested(msg, NL80211_ATTR_REKEY_DATA);
    504 	l_genl_msg_append_attr(msg, NL80211_REKEY_DATA_KEK,
    505 					NL80211_KEK_LEN, kek);
    506 	l_genl_msg_append_attr(msg, NL80211_REKEY_DATA_KCK,
    507 					NL80211_KCK_LEN, kck);
    508 	l_genl_msg_append_attr(msg, NL80211_REKEY_DATA_REPLAY_CTR,
    509 			NL80211_REPLAY_CTR_LEN, &replay_ctr);
    510 
    511 	l_genl_msg_leave_nested(msg);
    512 
    513 	return msg;
    514 }
    515 
    516 static struct l_genl_msg *nl80211_build_set_station(uint32_t ifindex,
    517 					const uint8_t *addr,
    518 					struct nl80211_sta_flag_update *flags)
    519 {
    520 	struct l_genl_msg *msg;
    521 
    522 	msg = l_genl_msg_new_sized(NL80211_CMD_SET_STATION, 512);
    523 
    524 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &ifindex);
    525 	l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
    526 	l_genl_msg_append_attr(msg, NL80211_ATTR_STA_FLAGS2,
    527 				sizeof(struct nl80211_sta_flag_update), flags);
    528 
    529 	return msg;
    530 }
    531 
    532 struct l_genl_msg *nl80211_build_set_station_authorized(uint32_t ifindex,
    533 							const uint8_t *addr)
    534 {
    535 	struct nl80211_sta_flag_update flags = {
    536 		.mask = (1 << NL80211_STA_FLAG_AUTHORIZED),
    537 		.set = (1 << NL80211_STA_FLAG_AUTHORIZED),
    538 	};
    539 
    540 	return nl80211_build_set_station(ifindex, addr, &flags);
    541 }
    542 
    543 struct l_genl_msg *nl80211_build_set_station_associated(uint32_t ifindex,
    544 							const uint8_t *addr)
    545 {
    546 	struct nl80211_sta_flag_update flags = {
    547 		.mask = (1 << NL80211_STA_FLAG_AUTHENTICATED) |
    548 			(1 << NL80211_STA_FLAG_ASSOCIATED),
    549 		.set = (1 << NL80211_STA_FLAG_AUTHENTICATED) |
    550 			(1 << NL80211_STA_FLAG_ASSOCIATED),
    551 	};
    552 
    553 	return nl80211_build_set_station(ifindex, addr, &flags);
    554 }
    555 
    556 struct l_genl_msg *nl80211_build_set_station_unauthorized(uint32_t ifindex,
    557 							const uint8_t *addr)
    558 {
    559 	struct nl80211_sta_flag_update flags = {
    560 		.mask = (1 << NL80211_STA_FLAG_AUTHORIZED),
    561 		.set = 0,
    562 	};
    563 
    564 	return nl80211_build_set_station(ifindex, addr, &flags);
    565 }
    566 
    567 struct l_genl_msg *nl80211_build_set_key(uint32_t ifindex, uint8_t key_index)
    568 {
    569 	struct l_genl_msg *msg;
    570 
    571 	msg = l_genl_msg_new_sized(NL80211_CMD_SET_KEY, 128);
    572 
    573 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &ifindex);
    574 
    575 	l_genl_msg_enter_nested(msg, NL80211_ATTR_KEY);
    576 	l_genl_msg_append_attr(msg, NL80211_KEY_IDX, 1, &key_index);
    577 	l_genl_msg_append_attr(msg, NL80211_KEY_DEFAULT, 0, NULL);
    578 	l_genl_msg_enter_nested(msg, NL80211_KEY_DEFAULT_TYPES);
    579 	l_genl_msg_append_attr(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST,
    580 				0, NULL);
    581 	l_genl_msg_leave_nested(msg);
    582 	l_genl_msg_leave_nested(msg);
    583 
    584 	return msg;
    585 }
    586 
    587 struct l_genl_msg *nl80211_build_get_key(uint32_t ifindex, uint8_t key_index)
    588 {
    589 	struct l_genl_msg *msg;
    590 
    591 	msg = l_genl_msg_new_sized(NL80211_CMD_GET_KEY, 128);
    592 
    593 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &ifindex);
    594 	l_genl_msg_append_attr(msg, NL80211_ATTR_KEY_IDX, 1, &key_index);
    595 
    596 	return msg;
    597 }
    598 
    599 const void *nl80211_parse_get_key_seq(struct l_genl_msg *msg)
    600 {
    601 	struct l_genl_attr nested;
    602 	struct iovec iov;
    603 
    604 	if (l_genl_msg_get_error(msg) < 0 ||
    605 			nl80211_parse_attrs(msg, NL80211_ATTR_KEY, &nested,
    606 						NL80211_ATTR_UNSPEC) < 0) {
    607 		l_error("GET_KEY failed for the GTK: %i",
    608 			l_genl_msg_get_error(msg));
    609 		return NULL;
    610 	}
    611 
    612 	if (nl80211_parse_nested(&nested, NL80211_ATTR_KEY, NL80211_KEY_SEQ,
    613 					&iov, NL80211_ATTR_UNSPEC)) {
    614 		l_error("Can't recurse into ATTR_KEY in GET_KEY reply");
    615 		return NULL;
    616 	}
    617 
    618 	if (iov.iov_len != 6) {
    619 		l_error("KEY_SEQ length != 6 in GET_KEY reply");
    620 		return NULL;
    621 	}
    622 
    623 	return iov.iov_base;
    624 }
    625 
    626 struct l_genl_msg *nl80211_build_cmd_frame(uint32_t ifindex,
    627 						uint16_t frame_type,
    628 						const uint8_t *addr,
    629 						const uint8_t *to,
    630 						uint32_t freq,
    631 						struct iovec *iov,
    632 						size_t iov_len)
    633 {
    634 	struct l_genl_msg *msg;
    635 	struct iovec iovs[iov_len + 1];
    636 	uint8_t hdr[24];
    637 
    638 	memset(hdr, 0, 24);
    639 
    640 	l_put_le16(frame_type, hdr + 0);
    641 	memcpy(hdr + 4, to, 6);
    642 	memcpy(hdr + 10, addr, 6);
    643 	memcpy(hdr + 16, to, 6);
    644 
    645 	iovs[0].iov_base = hdr;
    646 	iovs[0].iov_len = sizeof(hdr);
    647 	memcpy(iovs + 1, iov, sizeof(*iov) * iov_len);
    648 
    649 	msg = l_genl_msg_new_sized(NL80211_CMD_FRAME, 128 + 512);
    650 
    651 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &ifindex);
    652 
    653 	if (freq)
    654 		l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY_FREQ, 4, &freq);
    655 	l_genl_msg_append_attrv(msg, NL80211_ATTR_FRAME, iovs, iov_len + 1);
    656 
    657 	return msg;
    658 }
    659 
    660 struct l_genl_msg *nl80211_build_external_auth(uint32_t ifindex,
    661 					uint16_t status_code,
    662 					const uint8_t *ssid, size_t ssid_len,
    663 					const uint8_t bssid[static 6])
    664 {
    665 	struct l_genl_msg *msg = l_genl_msg_new(NL80211_CMD_EXTERNAL_AUTH);
    666 
    667 	l_genl_msg_append_attr(msg, NL80211_ATTR_IFINDEX, 4, &ifindex);
    668 	l_genl_msg_append_attr(msg, NL80211_ATTR_STATUS_CODE, 2, &status_code);
    669 	l_genl_msg_append_attr(msg, NL80211_ATTR_SSID, ssid_len, ssid);
    670 	l_genl_msg_append_attr(msg, NL80211_ATTR_BSSID, 6, bssid);
    671 
    672 	return msg;
    673 }
    674 
    675 int nl80211_parse_chandef(struct l_genl_msg *msg, struct band_chandef *out)
    676 {
    677 	struct band_chandef t;
    678 
    679 	if (nl80211_parse_attrs(msg,
    680 			NL80211_ATTR_WIPHY_FREQ, &t.frequency,
    681 			NL80211_ATTR_CHANNEL_WIDTH, &t.channel_width,
    682 			NL80211_ATTR_CENTER_FREQ1, &t.center1_frequency,
    683 			NL80211_ATTR_UNSPEC) < 0)
    684 		return -ENOENT;
    685 
    686 	t.center2_frequency = 0;
    687 
    688 	/* Try to parse CENTER_FREQ2, but it is given only for 80P80 */
    689 	if (t.channel_width == NL80211_CHAN_WIDTH_80P80 &&
    690 			nl80211_parse_attrs(msg,
    691 				NL80211_ATTR_CENTER_FREQ2, &t.center2_frequency,
    692 				NL80211_ATTR_UNSPEC) < 0)
    693 		return -ENOENT;
    694 
    695 	memcpy(out, &t, sizeof(t));
    696 	return 0;
    697 }
    698 
    699 int nl80211_parse_supported_frequencies(struct l_genl_attr *band_freqs,
    700 					struct scan_freq_set *supported_list,
    701 					struct band *band)
    702 {
    703 	uint16_t type, len;
    704 	const void *data;
    705 	struct l_genl_attr attr;
    706 	struct l_genl_attr nested;
    707 	uint8_t channel;
    708 
    709 	if (!l_genl_attr_recurse(band_freqs, &nested))
    710 		return -EBADMSG;
    711 
    712 	while (l_genl_attr_next(&nested, NULL, NULL, NULL)) {
    713 		uint32_t freq = 0;
    714 		struct band_freq_attrs freq_attr = { 0 };
    715 		enum band_freq out_band;
    716 
    717 		if (!l_genl_attr_recurse(&nested, &attr))
    718 			continue;
    719 
    720 		while (l_genl_attr_next(&attr, &type, &len, &data)) {
    721 			switch (type) {
    722 			case NL80211_FREQUENCY_ATTR_FREQ:
    723 				freq = *((uint32_t *) data);
    724 				freq_attr.supported = true;
    725 				break;
    726 			case NL80211_FREQUENCY_ATTR_DISABLED:
    727 				freq_attr.disabled = true;
    728 				break;
    729 			case NL80211_FREQUENCY_ATTR_NO_IR:
    730 				freq_attr.no_ir = true;
    731 				break;
    732 			case NL80211_FREQUENCY_ATTR_NO_HT40_MINUS:
    733 				freq_attr.no_ht40_minus = true;
    734 				break;
    735 			case NL80211_FREQUENCY_ATTR_NO_HT40_PLUS:
    736 				freq_attr.no_ht40_plus = true;
    737 				break;
    738 			case NL80211_FREQUENCY_ATTR_NO_80MHZ:
    739 				freq_attr.no_80mhz = true;
    740 				break;
    741 			case NL80211_FREQUENCY_ATTR_NO_160MHZ:
    742 				freq_attr.no_160mhz = true;
    743 				break;
    744 			case NL80211_FREQUENCY_ATTR_NO_HE:
    745 				freq_attr.no_he = true;
    746 				break;
    747 			case NL80211_FREQUENCY_ATTR_MAX_TX_POWER:
    748 				freq_attr.tx_power = *((uint32_t *) data) / 100;
    749 				break;
    750 			}
    751 		}
    752 
    753 		if (!freq)
    754 			continue;
    755 
    756 		channel = band_freq_to_channel(freq, &out_band);
    757 		if (!channel)
    758 			continue;
    759 
    760 		if (L_WARN_ON(out_band != band->freq))
    761 			continue;
    762 
    763 		if (L_WARN_ON(channel > band->freqs_len))
    764 			continue;
    765 
    766 		if (supported_list)
    767 			scan_freq_set_add(supported_list, freq);
    768 
    769 		band->freq_attrs[channel] = freq_attr;
    770 	}
    771 
    772 	return 0;
    773 }
    774 
    775 void nl80211_append_rsn_attributes(struct l_genl_msg *msg,
    776 						struct handshake_state *hs)
    777 {
    778 	uint32_t nl_cipher;
    779 	uint32_t nl_akm;
    780 	uint32_t wpa_version;
    781 
    782 	nl_cipher = ie_rsn_cipher_suite_to_cipher(hs->pairwise_cipher);
    783 	L_WARN_ON(!nl_cipher);
    784 	l_genl_msg_append_attr(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
    785 					4, &nl_cipher);
    786 
    787 	nl_cipher = ie_rsn_cipher_suite_to_cipher(hs->group_cipher);
    788 	L_WARN_ON(!nl_cipher);
    789 	l_genl_msg_append_attr(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
    790 					4, &nl_cipher);
    791 
    792 	if (hs->mfp) {
    793 		uint32_t use_mfp = NL80211_MFP_REQUIRED;
    794 
    795 		l_genl_msg_append_attr(msg, NL80211_ATTR_USE_MFP, 4, &use_mfp);
    796 	}
    797 
    798 	nl_akm = ie_rsn_akm_suite_to_akm(hs->akm_suite);
    799 	L_WARN_ON(!nl_akm);
    800 	l_genl_msg_append_attr(msg, NL80211_ATTR_AKM_SUITES, 4, &nl_akm);
    801 
    802 	if (IE_AKM_IS_SAE(hs->akm_suite))
    803 		wpa_version = NL80211_WPA_VERSION_3;
    804 	else if (hs->wpa_ie)
    805 		wpa_version = NL80211_WPA_VERSION_1;
    806 	else
    807 		wpa_version = NL80211_WPA_VERSION_2;
    808 
    809 	l_genl_msg_append_attr(msg, NL80211_ATTR_WPA_VERSIONS,
    810 						4, &wpa_version);
    811 }