eiwd

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

frame-xchg.c (36057B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2020  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 <stdarg.h>
     29 #include <sys/socket.h>
     30 #include <linux/genetlink.h>
     31 
     32 #include <ell/ell.h>
     33 
     34 #include "linux/nl80211.h"
     35 
     36 #include "ell/useful.h"
     37 #include "src/iwd.h"
     38 #include "src/module.h"
     39 #include "src/mpdu.h"
     40 #include "src/util.h"
     41 #include "src/watchlist.h"
     42 #include "src/nl80211util.h"
     43 #include "src/netdev.h"
     44 #include "src/frame-xchg.h"
     45 #include "src/wiphy.h"
     46 
     47 #ifndef SOL_NETLINK
     48 #define SOL_NETLINK 270
     49 #endif
     50 
     51 struct watch_group {
     52 	/*
     53 	 * Group IDs, except 0, are per wdev for user's convenience.
     54 	 * I.e. group 1 can be used for P2P discovery and be removed as
     55 	 * soon as the scan is over on a given device without interfering
     56 	 * with scans on other devices.  This means a module can name
     57 	 * all the groups it's going to need in a static enum.
     58 	 * Group 0 is the default group and uses the iwd_get_genl() netlink
     59 	 * socket shared with all its other users, meaning that it can not
     60 	 * be closed through the watch API.  It is to be used for watches
     61 	 * that don't need to be unregistered before the virtual interface
     62 	 * type change or destruction, which are the two events that
     63 	 * implicitly unregister all existing watches.
     64 	 */
     65 	uint32_t id;
     66 	uint64_t wdev_id;
     67 	uint32_t unicast_watch_id;
     68 	struct l_io *io;
     69 	uint32_t nl_pid;
     70 	uint32_t nl_seq;
     71 	struct l_queue *write_queue;
     72 	struct watchlist watches;
     73 };
     74 
     75 struct frame_watch {
     76 	uint64_t wdev_id;
     77 	uint16_t frame_type;
     78 	uint8_t *prefix;
     79 	size_t prefix_len;
     80 	struct watch_group *group;
     81 	struct watchlist_item super;
     82 };
     83 
     84 static struct l_queue *watch_groups;
     85 
     86 struct wdev_info {
     87 	uint64_t id;
     88 	uint32_t iftype;
     89 };
     90 
     91 static struct l_queue *wdevs;
     92 
     93 struct frame_xchg_data {
     94 	uint64_t wdev_id;
     95 	uint32_t freq;
     96 	struct mmpdu_header *tx_mpdu;
     97 	size_t tx_mpdu_len;
     98 	bool tx_acked;
     99 	uint64_t cookie;
    100 	bool have_cookie;
    101 	bool early_status;
    102 	struct {
    103 		struct mmpdu_header *mpdu;
    104 		const void *body;
    105 		size_t body_len;
    106 		int rssi;
    107 	} early_frame;
    108 	struct l_timeout *timeout;
    109 	struct l_queue *rx_watches;
    110 	frame_xchg_cb_t cb;
    111 	frame_xchg_destroy_func_t destroy;
    112 	void *user_data;
    113 	uint32_t group_id;
    114 	unsigned int retry_cnt;
    115 	unsigned int retry_interval;
    116 	unsigned int resp_timeout;
    117 	unsigned int retries_on_ack;
    118 	struct wiphy_radio_work_item work;
    119 	bool no_cck_rates;
    120 	unsigned int tx_cmd_id;
    121 };
    122 
    123 struct frame_xchg_watch_data {
    124 	struct frame_xchg_prefix *prefix;
    125 	frame_xchg_resp_cb_t cb;
    126 };
    127 
    128 static struct l_queue *frame_xchgs;
    129 static struct l_genl_family *nl80211;
    130 static uint32_t nl80211_id;
    131 
    132 struct frame_prefix_info {
    133 	uint16_t frame_type;
    134 	const uint8_t *body;
    135 	size_t body_len;
    136 	uint64_t wdev_id;
    137 };
    138 
    139 static bool frame_watch_match_prefix(const void *a, const void *b)
    140 {
    141 	const struct watchlist_item *item = a;
    142 	const struct frame_watch *watch =
    143 		l_container_of(item, struct frame_watch, super);
    144 	const struct frame_prefix_info *info = b;
    145 
    146 	return watch->frame_type == info->frame_type &&
    147 		watch->prefix_len <= info->body_len &&
    148 		(watch->prefix_len == 0 ||
    149 		 !memcmp(watch->prefix, info->body, watch->prefix_len)) &&
    150 		info->wdev_id == watch->wdev_id;
    151 }
    152 
    153 static void frame_watch_unicast_notify(struct l_genl_msg *msg, void *user_data)
    154 {
    155 	struct watch_group *group = user_data;
    156 	const uint64_t *wdev_id = NULL;
    157 	const uint32_t *ifindex = NULL;
    158 	struct l_genl_attr attr;
    159 	uint16_t type, len, frame_len;
    160 	const void *data;
    161 	const struct mmpdu_header *mpdu = NULL;
    162 	const uint8_t *body;
    163 	struct frame_prefix_info info;
    164 	int rssi = 0;	/* No-RSSI flag value */
    165 
    166 	if (l_genl_msg_get_command(msg) != NL80211_CMD_FRAME)
    167 		return;
    168 
    169 	if (!l_genl_attr_init(&attr, msg))
    170 		return;
    171 
    172 	while (l_genl_attr_next(&attr, &type, &len, &data)) {
    173 		switch (type) {
    174 		case NL80211_ATTR_WDEV:
    175 			if (len != 8)
    176 				break;
    177 
    178 			wdev_id = data;
    179 			break;
    180 
    181 		case NL80211_ATTR_IFINDEX:
    182 			if (len != 4)
    183 				break;
    184 
    185 			ifindex = data;
    186 			break;
    187 
    188 		case NL80211_ATTR_FRAME:
    189 			mpdu = mpdu_validate(data, len);
    190 			if (!mpdu) {
    191 				l_warn("Frame didn't validate as MMPDU");
    192 				return;
    193 			}
    194 
    195 			frame_len = len;
    196 			break;
    197 
    198 		case NL80211_ATTR_RX_SIGNAL_DBM:
    199 			if (len != 4)
    200 				break;
    201 
    202 			rssi = *(const int32_t *) data;
    203 		}
    204 	}
    205 
    206 	if (!wdev_id || (group->wdev_id && group->wdev_id != *wdev_id)) {
    207 		l_warn("Bad wdev attribute");
    208 		return;
    209 	}
    210 
    211 	if (!mpdu) {
    212 		l_warn("Missing frame data");
    213 		return;
    214 	}
    215 
    216 	body = mmpdu_body(mpdu);
    217 
    218 	if (ifindex) {
    219 		struct netdev *netdev = netdev_find(*ifindex);
    220 
    221 		if (netdev && memcmp(mpdu->address_1,
    222 					netdev_get_address(netdev), 6) &&
    223 				!util_is_broadcast_address(mpdu->address_1))
    224 			return;
    225 	}
    226 
    227 	/* Only match the frame type and subtype like the kernel does */
    228 #define FC_FTYPE_STYPE_MASK 0x00fc
    229 	info.frame_type = l_get_le16(mpdu) & FC_FTYPE_STYPE_MASK;
    230 	info.body = (const uint8_t *) body;
    231 	info.body_len = (const uint8_t *) mpdu + frame_len - body;
    232 	info.wdev_id = *wdev_id;
    233 
    234 	WATCHLIST_NOTIFY_MATCHES(&group->watches, frame_watch_match_prefix,
    235 					&info, frame_watch_cb_t, mpdu,
    236 					info.body, info.body_len, rssi);
    237 
    238 	/* Has frame_watch_group_destroy been called inside a frame CB? */
    239 	if (group->watches.pending_destroy)
    240 		l_free(group);
    241 }
    242 
    243 static void frame_watch_group_destroy(void *data)
    244 {
    245 	struct watch_group *group = data;
    246 
    247 	if (group->unicast_watch_id)
    248 		l_genl_remove_unicast_watch(iwd_get_genl(),
    249 						group->unicast_watch_id);
    250 
    251 	l_io_destroy(group->io);
    252 	l_queue_destroy(group->write_queue,
    253 			(l_queue_destroy_func_t) l_genl_msg_unref);
    254 
    255 	/*
    256 	 * We may be inside a frame notification but even then use
    257 	 * watchlist_destroy to prevent any more notifications from
    258 	 * being dispatched.
    259 	 */
    260 	watchlist_destroy(&group->watches);
    261 	if (group->watches.in_notify)
    262 		return;
    263 
    264 	l_free(group);
    265 }
    266 
    267 static void frame_watch_free(struct watchlist_item *item)
    268 {
    269 	struct frame_watch *watch =
    270 		l_container_of(item, struct frame_watch, super);
    271 
    272 	l_free(watch->prefix);
    273 	l_free(watch);
    274 }
    275 
    276 static const struct watchlist_ops frame_watch_ops = {
    277 	.item_free = frame_watch_free,
    278 };
    279 
    280 static bool frame_watch_group_io_write(struct l_io *io, void *user_data)
    281 {
    282 	struct watch_group *group = user_data;
    283 	struct l_genl_msg *msg;
    284 	const void *msg_data;
    285 	size_t msg_size;
    286 	ssize_t bytes_written;
    287 
    288 	msg = l_queue_pop_head(group->write_queue);
    289 	if (!msg)
    290 		return false;
    291 
    292 	msg_data = l_genl_msg_to_data(msg, nl80211_id, NLM_F_REQUEST,
    293 					++group->nl_seq, group->nl_pid,
    294 					&msg_size);
    295 	bytes_written = send(l_io_get_fd(group->io), msg_data, msg_size, 0);
    296 	l_genl_msg_unref(msg);
    297 
    298 	if (bytes_written < 0) {
    299 		l_error("Frame watch group socket write error: %s (%i)",
    300 			strerror(errno), errno);
    301 
    302 		l_queue_push_head(group->write_queue, msg);
    303 		return false;
    304 	}
    305 
    306 	return !l_queue_isempty(group->write_queue);
    307 }
    308 
    309 static bool frame_watch_group_io_read(struct l_io *io, void *user_data)
    310 {
    311 	struct watch_group *group = user_data;
    312 	struct cmsghdr *cmsg;
    313 	struct msghdr msg;
    314 	struct iovec iov;
    315 	unsigned char buf[8192];
    316 	unsigned char control[32];
    317 	ssize_t bytes_read;
    318 	struct nlmsghdr *nlmsg;
    319 	size_t nlmsg_len;
    320 	uint32_t nlmsg_group = 0;
    321 
    322 	memset(&iov, 0, sizeof(iov));
    323 	iov.iov_base = buf;
    324 	iov.iov_len = sizeof(buf);
    325 
    326 	memset(&msg, 0, sizeof(msg));
    327 	msg.msg_iov = &iov;
    328 	msg.msg_iovlen = 1;
    329 	msg.msg_control = control;
    330 	msg.msg_controllen = sizeof(control);
    331 
    332 	bytes_read = recvmsg(l_io_get_fd(group->io), &msg, 0);
    333 	if (bytes_read < 0) {
    334 		if (errno != EAGAIN && errno != EINTR) {
    335 			l_error("Frame watch group socket read error: %s (%i)",
    336 				strerror(errno), errno);
    337 			return false;
    338 		}
    339 
    340 		return true;
    341 	}
    342 
    343 	nlmsg_len = bytes_read;
    344 
    345 	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
    346 		struct nl_pktinfo pktinfo;
    347 
    348 		if (cmsg->cmsg_level != SOL_NETLINK)
    349 			continue;
    350 
    351 		if (cmsg->cmsg_type != NETLINK_PKTINFO)
    352 			continue;
    353 
    354 		memcpy(&pktinfo, CMSG_DATA(cmsg), sizeof(pktinfo));
    355 
    356 		nlmsg_group = pktinfo.group;
    357 	}
    358 
    359 	if (nlmsg_group) /* Ignore multicast */
    360 		return true;
    361 
    362 	for (nlmsg = iov.iov_base; NLMSG_OK(nlmsg, nlmsg_len);
    363 				nlmsg = NLMSG_NEXT(nlmsg, nlmsg_len)) {
    364 		struct l_genl_msg *genl_msg;
    365 
    366 		if (nlmsg->nlmsg_type != nl80211_id) /* Ignore other families */
    367 			continue;
    368 
    369 		if (nlmsg->nlmsg_seq)	/* Ignore responses */
    370 			continue;
    371 
    372 		genl_msg = l_genl_msg_new_from_data((void *) nlmsg,
    373 							nlmsg->nlmsg_len);
    374 		if (!genl_msg)
    375 			continue;
    376 
    377 		frame_watch_unicast_notify(genl_msg, group);
    378 		l_genl_msg_unref(genl_msg);
    379 	}
    380 
    381 	return true;
    382 }
    383 
    384 static struct watch_group *frame_watch_group_new(uint64_t wdev_id, uint32_t id)
    385 {
    386 	struct watch_group *group = l_new(struct watch_group, 1);
    387 	struct sockaddr_nl addr;
    388 	socklen_t addrlen = sizeof(addr);
    389 	int fd = -1, pktinfo = 1;
    390 
    391 	group->id = id;
    392 	group->wdev_id = wdev_id;
    393 	watchlist_init(&group->watches, &frame_watch_ops);
    394 
    395 	if (id == 0) {
    396 		group->unicast_watch_id = l_genl_add_unicast_watch(
    397 						iwd_get_genl(),
    398 						NL80211_GENL_NAME,
    399 						frame_watch_unicast_notify,
    400 						group, NULL);
    401 		if (unlikely(!group->unicast_watch_id)) {
    402 			l_error("Registering for unicast notification failed");
    403 			goto err;
    404 		}
    405 
    406 		return group;
    407 	}
    408 
    409 	/*
    410 	 * If we need to add groups using genl sockets other than our main
    411 	 * genl socket (iwd_get_genl()), use raw io instead of l_genl.  This
    412 	 * way we don't have to query the genl families -- we already know
    413 	 * nl80211 is present -- and the handling is simpler, and l_genl,
    414 	 * while it would work, is not meant to have multiple instances
    415 	 * per program.
    416 	 */
    417 	fd = socket(PF_NETLINK,
    418 			SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
    419 			NETLINK_GENERIC);
    420 	if (unlikely(fd < 0))
    421 		goto err;
    422 
    423 	memset(&addr, 0, sizeof(addr));
    424 	addr.nl_family = AF_NETLINK;
    425 	addr.nl_pid = 0;
    426 
    427 	if (unlikely(bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0))
    428 		goto err;
    429 
    430 	if (unlikely(getsockname(fd, (struct sockaddr *) &addr, &addrlen) < 0))
    431 		goto err;
    432 
    433 	group->nl_pid = addr.nl_pid;
    434 
    435 	if (unlikely(setsockopt(fd, SOL_NETLINK, NETLINK_PKTINFO,
    436 					&pktinfo, sizeof(pktinfo)) < 0))
    437 		goto err;
    438 
    439 	group->io = l_io_new(fd);
    440 	if (unlikely(!group->io))
    441 		goto err;
    442 
    443 	l_io_set_close_on_destroy(group->io, true);
    444 	l_io_set_read_handler(group->io, frame_watch_group_io_read, group,
    445 				NULL);
    446 	group->write_queue = l_queue_new();
    447 
    448 	return group;
    449 
    450 err:
    451 	if (fd > -1)
    452 		close(fd);
    453 
    454 	frame_watch_group_destroy(group);
    455 	return NULL;
    456 }
    457 
    458 struct watch_group_match_info {
    459 	uint64_t wdev_id;
    460 	uint32_t id;
    461 };
    462 
    463 static bool frame_watch_group_match(const void *a, const void *b)
    464 {
    465 	const struct watch_group *group = a;
    466 	const struct watch_group_match_info *info = b;
    467 
    468 	return group->wdev_id == info->wdev_id && group->id == info->id;
    469 }
    470 
    471 static struct watch_group *frame_watch_group_get(uint64_t wdev_id, uint32_t id)
    472 {
    473 	struct watch_group_match_info info = { id == 0 ? 0 : wdev_id, id };
    474 	struct watch_group *group = l_queue_find(watch_groups,
    475 						frame_watch_group_match, &info);
    476 
    477 	if (!group) {
    478 		group = frame_watch_group_new(wdev_id, id);
    479 		l_queue_push_tail(watch_groups, group);
    480 	}
    481 
    482 	return group;
    483 }
    484 
    485 static void frame_watch_register_cb(struct l_genl_msg *msg, void *user_data)
    486 {
    487 	if (l_genl_msg_get_error(msg) < 0)
    488 		l_error("Could not register frame watch type %04x: %i",
    489 			L_PTR_TO_UINT(user_data), l_genl_msg_get_error(msg));
    490 }
    491 
    492 static void frame_watch_notify_empty(const struct mmpdu_header *mpdu,
    493 					const void *body, size_t body_len,
    494 					int rssi, void *user_data)
    495 {
    496 }
    497 
    498 struct frame_duplicate_info {
    499 	uint64_t wdev_id;
    500 	uint16_t frame_type;
    501 	const uint8_t *prefix;
    502 	size_t prefix_len;
    503 	frame_watch_cb_t handler;
    504 	void *user_data;
    505 	bool duplicate : 1;
    506 	bool registered : 1;
    507 };
    508 
    509 static bool frame_watch_check_duplicate(void *data, void *user_data)
    510 {
    511 	struct watchlist_item *super = data;
    512 	struct frame_watch *watch =
    513 		l_container_of(super, struct frame_watch, super);
    514 	struct frame_duplicate_info *info = user_data;
    515 	int common_len = info->prefix_len < watch->prefix_len ?
    516 		info->prefix_len : watch->prefix_len;
    517 
    518 	if (info->wdev_id != watch->wdev_id ||
    519 			info->frame_type != watch->frame_type ||
    520 			(common_len &&
    521 			 memcmp(info->prefix, watch->prefix, common_len)))
    522 		/* No match */
    523 		return false;
    524 
    525 	if (info->prefix_len >= watch->prefix_len)
    526 		/*
    527 		 * A matching shorter prefix is already registered with
    528 		 * the kernel, no need to register the new prefix.
    529 		 */
    530 		info->registered = true;
    531 
    532 	/*
    533 	 * If we previously had a watch registered on this socket,
    534 	 * with the same or a more specific prefix, we can now forget
    535 	 * its entry as the new watch is going to hold enough
    536 	 * information to keep us from registering redundant prefixes
    537 	 * in the future.
    538 	 */
    539 	if (info->prefix_len <= watch->prefix_len &&
    540 			watch->super.notify == frame_watch_notify_empty)
    541 		goto drop;
    542 
    543 	if (info->handler != watch->super.notify ||
    544 			info->user_data != watch->super.notify_data)
    545 		return false;
    546 
    547 	/*
    548 	 * If we already have a watch with the exact same callback and
    549 	 * user_data and a matching prefix (longer or shorter), drop
    550 	 * either the existing watch, or the new watch, so as to preserve
    551 	 * the set of frames that trigger the callback but avoid
    552 	 * calling back twice with the same user_data.
    553 	 */
    554 	if (info->prefix_len >= watch->prefix_len) {
    555 		info->duplicate = true;
    556 		return false;
    557 	}
    558 
    559 drop:
    560 	/*
    561 	 * Drop the existing watch as a duplicate of the new one. If we are in
    562 	 * the watchlist notify loop, just mark this item as stale and it will
    563 	 * be cleaned up afterwards
    564 	 */
    565 	if (watch->group->watches.in_notify) {
    566 		super->id = 0;
    567 		watch->group->watches.stale_items = true;
    568 		return false;
    569 	}
    570 
    571 	frame_watch_free(&watch->super);
    572 	return true;
    573 }
    574 
    575 bool frame_watch_add(uint64_t wdev_id, uint32_t group_id, uint16_t frame_type,
    576 			const uint8_t *prefix, size_t prefix_len,
    577 			bool multicast_rx,
    578 			frame_watch_cb_t handler, void *user_data,
    579 			frame_xchg_destroy_func_t destroy)
    580 {
    581 	struct watch_group *group = frame_watch_group_get(wdev_id, group_id);
    582 	struct frame_watch *watch;
    583 	struct l_genl_msg *msg;
    584 	struct frame_duplicate_info info = {
    585 		wdev_id, frame_type, prefix, prefix_len,
    586 		handler, user_data, false, false
    587 	};
    588 
    589 	if (!group)
    590 		return false;
    591 
    592 	l_queue_foreach_remove(group->watches.items,
    593 				frame_watch_check_duplicate, &info);
    594 
    595 	if (info.duplicate)
    596 		return true;
    597 
    598 	watch = l_new(struct frame_watch, 1);
    599 	watch->frame_type = frame_type;
    600 	watch->prefix = prefix_len ? l_memdup(prefix, prefix_len) : NULL;
    601 	watch->prefix_len = prefix_len;
    602 	watch->wdev_id = wdev_id;
    603 	watch->group = group;
    604 	watchlist_link(&group->watches, &watch->super, handler, user_data,
    605 			destroy);
    606 
    607 	if (info.registered)
    608 		return true;
    609 
    610 	msg = l_genl_msg_new_sized(NL80211_CMD_REGISTER_FRAME, 32 + prefix_len);
    611 
    612 	l_genl_msg_append_attr(msg, NL80211_ATTR_WDEV, 8, &wdev_id);
    613 	l_genl_msg_append_attr(msg, NL80211_ATTR_FRAME_TYPE, 2, &frame_type);
    614 	l_genl_msg_append_attr(msg, NL80211_ATTR_FRAME_MATCH,
    615 				prefix_len, prefix);
    616 
    617 	if (multicast_rx)
    618 		l_genl_msg_append_attr(msg, NL80211_ATTR_RECEIVE_MULTICAST,
    619 					0, NULL);
    620 
    621 	if (group->id == 0)
    622 		l_genl_family_send(nl80211, msg, frame_watch_register_cb,
    623 					L_UINT_TO_PTR(frame_type), NULL);
    624 	else {
    625 		l_queue_push_tail(group->write_queue, msg);
    626 		l_io_set_write_handler(group->io, frame_watch_group_io_write,
    627 					group, NULL);
    628 	}
    629 
    630 	return true;
    631 }
    632 
    633 bool frame_watch_group_remove(uint64_t wdev_id, uint32_t group_id)
    634 {
    635 	struct watch_group_match_info info = { wdev_id, group_id };
    636 	struct watch_group *group = l_queue_remove_if(watch_groups,
    637 						frame_watch_group_match, &info);
    638 
    639 	if (!group)
    640 		return false;
    641 
    642 	frame_watch_group_destroy(group);
    643 	return true;
    644 }
    645 
    646 static bool frame_watch_item_remove_wdev(void *data, void *user_data)
    647 {
    648 	struct frame_watch *watch =
    649 		l_container_of(data, struct frame_watch, super);
    650 	const uint64_t *wdev_id = user_data;
    651 
    652 	if (watch->wdev_id != *wdev_id)
    653 		return false;
    654 
    655 	if (watch->super.destroy)
    656 		watch->super.destroy(watch->super.notify_data);
    657 
    658 	frame_watch_free(&watch->super);
    659 	return true;
    660 }
    661 
    662 static bool frame_watch_group_remove_wdev(void *data, void *user_data)
    663 {
    664 	struct watch_group *group = data;
    665 	const uint64_t *wdev_id = user_data;
    666 
    667 	if (group->wdev_id == *wdev_id) {
    668 		frame_watch_group_destroy(group);
    669 		return true;
    670 	}
    671 
    672 	if (group->id != 0)
    673 		return false;
    674 
    675 	/*
    676 	 * Have to be careful here because we're messing with watchlist
    677 	 * internals.
    678 	 */
    679 	l_queue_foreach_remove(group->watches.items,
    680 				frame_watch_item_remove_wdev, user_data);
    681 	return false;
    682 }
    683 
    684 bool frame_watch_wdev_remove(uint64_t wdev_id)
    685 {
    686 	return l_queue_foreach_remove(watch_groups, frame_watch_group_remove_wdev,
    687 					&wdev_id) > 0;
    688 }
    689 
    690 struct frame_watch_handler_check_info {
    691 	uint64_t wdev_id;
    692 	frame_watch_cb_t handler;
    693 	void *user_data;
    694 };
    695 
    696 static bool frame_watch_item_remove_by_handler(void *data, void *user_data)
    697 {
    698 	struct frame_watch *watch =
    699 		l_container_of(data, struct frame_watch, super);
    700 	struct frame_watch_handler_check_info *info = user_data;
    701 
    702 	if (watch->wdev_id != info->wdev_id ||
    703 			watch->super.notify != info->handler ||
    704 			watch->super.notify_data != info->user_data)
    705 		return false;
    706 
    707 	if (watch->super.destroy) {
    708 		watch->super.destroy(watch->super.notify_data);
    709 		watch->super.destroy = NULL;
    710 	}
    711 
    712 	/*
    713 	 * Keep the entry, with a dummy callback, in order to keep us from
    714 	 * re-registering its prefix in future frame_watch_add calls.  We
    715 	 * can drop the entry in some circumstances but checking the
    716 	 * conditions for this costs more than it is worth right now.
    717 	 */
    718 	watch->super.notify = frame_watch_notify_empty;
    719 	return false;
    720 }
    721 
    722 /*
    723  * Note this one doesn't interact with the kernel watches, only forgets our
    724  * struct frame_watch instances.
    725  *
    726  * Also note empty groups are not automatically destroyed because right now
    727  * this is not desired in frame_xchg_reset -- the only user of this function.
    728  */
    729 static bool frame_watch_remove_by_handler(uint64_t wdev_id, uint32_t group_id,
    730 						frame_watch_cb_t handler,
    731 						void *user_data)
    732 {
    733 	struct watch_group_match_info group_info = {
    734 				group_id == 0 ? 0 : wdev_id, group_id };
    735 	struct frame_watch_handler_check_info handler_info = {
    736 					wdev_id, handler, user_data };
    737 	struct watch_group *group = l_queue_find(watch_groups,
    738 						frame_watch_group_match,
    739 						&group_info);
    740 
    741 	if (!group)
    742 		return false;
    743 
    744 	return l_queue_foreach_remove(group->watches.items,
    745 					frame_watch_item_remove_by_handler,
    746 					&handler_info) > 0;
    747 }
    748 
    749 static bool frame_xchg_tx_retry(struct wiphy_radio_work_item *item);
    750 static bool frame_xchg_resp_handle(const struct mmpdu_header *mpdu,
    751 					const void *body, size_t body_len,
    752 					int rssi, void *user_data);
    753 static void frame_xchg_resp_cb(const struct mmpdu_header *mpdu,
    754 				const void *body, size_t body_len,
    755 				int rssi, void *user_data);
    756 
    757 static void frame_xchg_wait_cancel(struct frame_xchg_data *fx)
    758 {
    759 	struct l_genl_msg *msg;
    760 
    761 	if (!fx->have_cookie)
    762 		return;
    763 
    764 	l_debug("");
    765 
    766 	msg = l_genl_msg_new_sized(NL80211_CMD_FRAME_WAIT_CANCEL, 32);
    767 	l_genl_msg_append_attr(msg, NL80211_ATTR_WDEV, 8, &fx->wdev_id);
    768 	l_genl_msg_append_attr(msg, NL80211_ATTR_COOKIE, 8, &fx->cookie);
    769 	l_genl_family_send(nl80211, msg, NULL, NULL, NULL);
    770 
    771 	fx->have_cookie = false;
    772 }
    773 
    774 static void frame_xchg_reset(struct frame_xchg_data *fx)
    775 {
    776 	frame_xchg_wait_cancel(fx);
    777 
    778 	if (fx->timeout)
    779 		l_timeout_remove(fx->timeout);
    780 
    781 	if (fx->tx_cmd_id) {
    782 		l_genl_family_cancel(nl80211, fx->tx_cmd_id);
    783 		fx->tx_cmd_id = 0;
    784 	}
    785 
    786 	l_free(fx->early_frame.mpdu);
    787 	fx->early_frame.mpdu = NULL;
    788 	l_queue_destroy(fx->rx_watches, l_free);
    789 	fx->rx_watches = NULL;
    790 	l_free(fx->tx_mpdu);
    791 	fx->tx_mpdu = NULL;
    792 	frame_watch_remove_by_handler(fx->wdev_id, fx->group_id,
    793 					frame_xchg_resp_cb, fx);
    794 }
    795 
    796 static void frame_xchg_destroy(struct wiphy_radio_work_item *item)
    797 {
    798 	struct frame_xchg_data *fx = l_container_of(item,
    799 						struct frame_xchg_data, work);
    800 
    801 	if (fx->destroy)
    802 		fx->destroy(fx->user_data);
    803 
    804 	frame_xchg_reset(fx);
    805 	l_free(fx);
    806 }
    807 
    808 static void frame_xchg_done(struct frame_xchg_data *fx, int err)
    809 {
    810 	l_queue_remove(frame_xchgs, fx);
    811 
    812 	if (fx->cb)
    813 		fx->cb(err, fx->user_data);
    814 
    815 	wiphy_radio_work_done(wiphy_find_by_wdev(fx->wdev_id), fx->work.id);
    816 }
    817 
    818 static void frame_xchg_timeout_destroy(void *user_data)
    819 {
    820 	struct frame_xchg_data *fx = user_data;
    821 
    822 	fx->timeout = NULL;
    823 }
    824 
    825 static void frame_xchg_timeout_cb(struct l_timeout *timeout,
    826 					void *user_data)
    827 {
    828 	struct frame_xchg_data *fx = user_data;
    829 
    830 	l_timeout_remove(fx->timeout);
    831 	frame_xchg_tx_retry(&fx->work);
    832 }
    833 
    834 static void frame_xchg_listen_end_cb(struct l_timeout *timeout,
    835 					void *user_data)
    836 {
    837 	struct frame_xchg_data *fx = user_data;
    838 
    839 	if (!fx->retries_on_ack) {
    840 		frame_xchg_done(fx, 0);
    841 		return;
    842 	}
    843 
    844 	l_timeout_remove(fx->timeout);
    845 	fx->retries_on_ack--;
    846 	fx->retry_cnt = 0;
    847 	frame_xchg_tx_retry(&fx->work);
    848 }
    849 
    850 static void frame_xchg_tx_status(struct frame_xchg_data *fx, bool acked)
    851 {
    852 	if (!acked) {
    853 		frame_xchg_wait_cancel(fx);
    854 
    855 		if (!fx->retry_interval || fx->retry_cnt >= 15) {
    856 			if (!fx->resp_timeout)
    857 				fx->have_cookie = false;
    858 
    859 			l_error("Frame tx retry limit reached");
    860 			frame_xchg_done(fx, -ECOMM);
    861 			return;
    862 		}
    863 
    864 		l_free(fx->early_frame.mpdu);
    865 		fx->early_frame.mpdu = NULL;
    866 		fx->timeout = l_timeout_create_ms(fx->retry_interval,
    867 						frame_xchg_timeout_cb, fx,
    868 						frame_xchg_timeout_destroy);
    869 		return;
    870 	}
    871 
    872 	if (!fx->resp_timeout) {
    873 		/* No listen period to cancel */
    874 		fx->have_cookie = false;
    875 		frame_xchg_done(fx, 0);
    876 		return;
    877 	}
    878 
    879 	fx->tx_acked = true;
    880 
    881 	/* Process frames received early for strange drivers */
    882 	if (fx->early_frame.mpdu) {
    883 		/* The command is now over so no need to cancel it */
    884 		fx->have_cookie = false;
    885 
    886 		l_debug("Processing an early frame");
    887 
    888 		if (frame_xchg_resp_handle(fx->early_frame.mpdu,
    889 						fx->early_frame.body,
    890 						fx->early_frame.body_len,
    891 						fx->early_frame.rssi, fx))
    892 			return;
    893 
    894 		frame_xchg_listen_end_cb(NULL, fx);
    895 		return;
    896 	}
    897 
    898 	/* Txed frame ACKed, listen for response frames */
    899 	fx->timeout = l_timeout_create_ms(fx->resp_timeout,
    900 						frame_xchg_listen_end_cb, fx,
    901 						frame_xchg_timeout_destroy);
    902 }
    903 
    904 static void frame_xchg_tx_cb(struct l_genl_msg *msg, void *user_data)
    905 {
    906 	struct frame_xchg_data *fx = user_data;
    907 	int error = l_genl_msg_get_error(msg);
    908 	uint64_t cookie;
    909 	bool early_status;
    910 
    911 	fx->tx_cmd_id = 0;
    912 
    913 	if (error < 0) {
    914 		if (error == -EBUSY) {
    915 			fx->timeout = l_timeout_create_ms(fx->retry_interval,
    916 						frame_xchg_timeout_cb, fx,
    917 						frame_xchg_timeout_destroy);
    918 			return;
    919 		}
    920 
    921 		l_error("Frame tx failed: %s (%i)", strerror(-error), -error);
    922 		goto error;
    923 	}
    924 
    925 	if (L_WARN_ON(nl80211_parse_attrs(msg, NL80211_ATTR_COOKIE, &cookie,
    926 						NL80211_ATTR_UNSPEC) < 0)) {
    927 		error = -EINVAL;
    928 		goto error;
    929 	}
    930 
    931 	l_debug("Frame sent, cookie: %"PRIu64" obtained", cookie);
    932 
    933 	early_status = fx->early_status && cookie == fx->cookie;
    934 	fx->tx_acked = early_status && fx->tx_acked;
    935 	fx->have_cookie = true;
    936 	fx->cookie = cookie;
    937 
    938 	if (early_status)
    939 		frame_xchg_tx_status(fx, fx->tx_acked);
    940 
    941 	return;
    942 error:
    943 	frame_xchg_done(fx, error);
    944 }
    945 
    946 static bool frame_xchg_tx_retry(struct wiphy_radio_work_item *item)
    947 {
    948 	struct frame_xchg_data *fx = l_container_of(item,
    949 						struct frame_xchg_data, work);
    950 	struct l_genl_msg *msg;
    951 	uint32_t duration = fx->resp_timeout;
    952 
    953 	/*
    954 	 * TODO: in Station, AP, P2P-Client, GO or Ad-Hoc modes if we're
    955 	 * transmitting the frame on the BSS's operating channel we can skip
    956 	 * NL80211_ATTR_DURATION and we should still receive the frames
    957 	 * without potentially interfering with other operations.
    958 	 *
    959 	 * TODO: we may want to react to NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL
    960 	 * in the group socket's unicast handler.
    961 	 */
    962 
    963 	msg = l_genl_msg_new_sized(NL80211_CMD_FRAME, 128 + fx->tx_mpdu_len);
    964 	l_genl_msg_append_attr(msg, NL80211_ATTR_WDEV, 8, &fx->wdev_id);
    965 	l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY_FREQ, 4, &fx->freq);
    966 	l_genl_msg_append_attr(msg, NL80211_ATTR_OFFCHANNEL_TX_OK, 0, NULL);
    967 	l_genl_msg_append_attr(msg, NL80211_ATTR_FRAME,
    968 				fx->tx_mpdu_len, fx->tx_mpdu);
    969 
    970 	if (fx->no_cck_rates)
    971 		l_genl_msg_append_attr(msg, NL80211_ATTR_TX_NO_CCK_RATE, 0,
    972 					NULL);
    973 
    974 	if (duration)
    975 		l_genl_msg_append_attr(msg, NL80211_ATTR_DURATION, 4,
    976 					&duration);
    977 
    978 	fx->tx_cmd_id = l_genl_family_send(nl80211, msg, frame_xchg_tx_cb, fx,
    979 						NULL);
    980 	if (!fx->tx_cmd_id) {
    981 		l_error("Error sending frame");
    982 		l_genl_msg_unref(msg);
    983 		frame_xchg_done(fx, -EIO);
    984 		return true;
    985 	}
    986 
    987 	fx->tx_acked = false;
    988 	fx->have_cookie = false;
    989 	fx->early_status = false;
    990 	fx->retry_cnt++;
    991 
    992 	return false;
    993 }
    994 
    995 static bool frame_xchg_match_ptr(const void *a, const void *b)
    996 {
    997 	return a == b;
    998 }
    999 
   1000 static bool frame_xchg_resp_handle(const struct mmpdu_header *mpdu,
   1001 					const void *body, size_t body_len,
   1002 					int rssi, void *user_data)
   1003 {
   1004 	struct frame_xchg_data *fx = user_data;
   1005 	const struct l_queue_entry *entry;
   1006 	size_t hdr_len;
   1007 
   1008 	l_debug("");
   1009 
   1010 	if (memcmp(mpdu->address_1, fx->tx_mpdu->address_2, 6))
   1011 		return false;
   1012 
   1013 	if (memcmp(mpdu->address_2, fx->tx_mpdu->address_1, 6))
   1014 		return false;
   1015 
   1016 	/*
   1017 	 * BSSID (address_3) check not practical because some Linux
   1018 	 * drivers report all-zero values and some remote devices send
   1019 	 * wrong addresses.  But the frame callback is free to perform
   1020 	 * its own check.
   1021 	 */
   1022 
   1023 	for (entry = l_queue_get_entries(fx->rx_watches);
   1024 			entry; entry = entry->next) {
   1025 		struct frame_xchg_watch_data *watch = entry->data;
   1026 		bool done;
   1027 
   1028 		if (body_len < watch->prefix->len ||
   1029 				memcmp(body, watch->prefix->data,
   1030 					watch->prefix->len))
   1031 			continue;
   1032 
   1033 		if (!fx->tx_acked)
   1034 			goto early_frame;
   1035 
   1036 		done = watch->cb(mpdu, body, body_len, rssi, fx->user_data);
   1037 
   1038 		if (!l_queue_find(frame_xchgs, frame_xchg_match_ptr, fx))
   1039 			return true;
   1040 
   1041 		if (done) {
   1042 			/* NULL callback here since the caller is done */
   1043 			fx->cb = NULL;
   1044 			frame_xchg_done(fx, 0);
   1045 			return true;
   1046 		}
   1047 	}
   1048 
   1049 	return false;
   1050 
   1051 early_frame:
   1052 	/*
   1053 	 * Work around the strange order of events seen with the brcmfmac
   1054 	 * driver where we receive the response frames before the frame
   1055 	 * Tx status, which in turn is receive before the Tx callback with
   1056 	 * the operation cookie... rather then the reverse.
   1057 	 * Save the response frame to be processed in the Tx done callback.
   1058 	 */
   1059 	if (fx->early_frame.mpdu)
   1060 		return false;
   1061 
   1062 	hdr_len = (const uint8_t *) body - (const uint8_t *) mpdu;
   1063 	fx->early_frame.mpdu = l_memdup(mpdu, body_len + hdr_len);
   1064 	fx->early_frame.body = (const uint8_t *) fx->early_frame.mpdu + hdr_len;
   1065 	fx->early_frame.body_len = body_len;
   1066 	fx->early_frame.rssi = rssi;
   1067 	return false;
   1068 }
   1069 
   1070 static void frame_xchg_resp_cb(const struct mmpdu_header *mpdu,
   1071 				const void *body, size_t body_len,
   1072 				int rssi, void *user_data)
   1073 {
   1074 	frame_xchg_resp_handle(mpdu, body, body_len, rssi, user_data);
   1075 }
   1076 
   1077 static bool frame_xchg_match_running(const void *a, const void *b)
   1078 {
   1079 	const struct frame_xchg_data *fx = a;
   1080 	const uint64_t *wdev_id = b;
   1081 
   1082 	return fx->retry_cnt > 0 && fx->wdev_id == *wdev_id;
   1083 }
   1084 
   1085 /*
   1086  * Send an action frame described by @frame.  If @retry_interval is
   1087  * non-zero and we receive no ACK from @peer to any of the retransmissions
   1088  * done in the kernel (at a high rate), retry after @retry_interval
   1089  * milliseconds from the time the kernel gave up.  If no ACK is received
   1090  * after all the retransmissions, call @cb with a non-zero error number.
   1091  * Otherwise, if @resp_timeout is non-zero, remain on the same channel
   1092  * and report any response frames from the frame's destination address
   1093  * that match provided prefixes, to the corresponding callbacks.  Do so
   1094  * for @resp_timeout milliseconds from the ACK receival or until a frame
   1095  * callback returns @true.  Call @cb when @resp_timeout runs out and
   1096  * no frame callback returned @true, or immediately after the ACK if
   1097  * @resp_timeout was 0.  @frame is an iovec array terminated by an iovec
   1098  * struct with NULL-iov_base.
   1099  */
   1100 uint32_t frame_xchg_start(uint64_t wdev_id, struct iovec *frame, uint32_t freq,
   1101 			unsigned int retry_interval, unsigned int resp_timeout,
   1102 			unsigned int retries_on_ack, uint32_t group_id,
   1103 			frame_xchg_cb_t cb, void *user_data,
   1104 			frame_xchg_destroy_func_t destroy, ...)
   1105 {
   1106 	uint32_t id;
   1107 	va_list args;
   1108 
   1109 	va_start(args, destroy);
   1110 	id = frame_xchg_startv(wdev_id, frame, freq, retry_interval, resp_timeout,
   1111 				retries_on_ack, group_id, cb, user_data,
   1112 				destroy, args);
   1113 	va_end(args);
   1114 	return id;
   1115 }
   1116 
   1117 static const struct wiphy_radio_work_item_ops work_ops = {
   1118 	.do_work = frame_xchg_tx_retry,
   1119 	.destroy = frame_xchg_destroy,
   1120 };
   1121 
   1122 static bool frame_xchg_wdev_match(const void *a, const void *b)
   1123 {
   1124 	const struct wdev_info *wdev = a;
   1125 	const uint64_t *id = b;
   1126 
   1127 	return wdev->id == *id;
   1128 }
   1129 
   1130 uint32_t frame_xchg_startv(uint64_t wdev_id, struct iovec *frame, uint32_t freq,
   1131 			unsigned int retry_interval, unsigned int resp_timeout,
   1132 			unsigned int retries_on_ack, uint32_t group_id,
   1133 			frame_xchg_cb_t cb, void *user_data,
   1134 			frame_xchg_destroy_func_t destroy, va_list resp_args)
   1135 {
   1136 	struct frame_xchg_data *fx;
   1137 	size_t frame_len;
   1138 	struct iovec *iov;
   1139 	uint8_t *ptr;
   1140 	struct wdev_info *wdev;
   1141 
   1142 	for (frame_len = 0, iov = frame; iov->iov_base; iov++)
   1143 		frame_len += iov->iov_len;
   1144 
   1145 	/*
   1146 	 * This assumes that the first iovec at least contains the mmpdu_fc
   1147 	 * portion of the header used to calculate the minimum length.
   1148 	 */
   1149 	if (frame[0].iov_len >= 2 && frame_len <
   1150 				mmpdu_header_len((const struct mmpdu_header *)
   1151 				frame[0].iov_base)) {
   1152 		l_error("Frame too short");
   1153 		cb(-EMSGSIZE, user_data);
   1154 		return 0;
   1155 	}
   1156 
   1157 	fx = l_new(struct frame_xchg_data, 1);
   1158 
   1159 	if (!frame_xchgs)
   1160 		frame_xchgs = l_queue_new();
   1161 
   1162 	fx->wdev_id = wdev_id;
   1163 	fx->freq = freq;
   1164 	fx->retry_interval = retry_interval;
   1165 	fx->resp_timeout = resp_timeout;
   1166 	fx->retries_on_ack = retries_on_ack;
   1167 	fx->cb = cb;
   1168 	fx->destroy = destroy;
   1169 	fx->user_data = user_data;
   1170 	fx->group_id = group_id;
   1171 
   1172 	fx->tx_mpdu = l_malloc(frame_len);
   1173 	fx->tx_mpdu_len = frame_len;
   1174 	ptr = (uint8_t *) fx->tx_mpdu;
   1175 
   1176 	for (iov = frame; iov->iov_base; ptr += iov->iov_len, iov++)
   1177 		memcpy(ptr, iov->iov_base, iov->iov_len);
   1178 
   1179 	wdev = l_queue_find(wdevs, frame_xchg_wdev_match, &wdev_id);
   1180 	fx->no_cck_rates = wdev &&
   1181 		(wdev->iftype == NL80211_IFTYPE_P2P_DEVICE ||
   1182 		 wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
   1183 		 wdev->iftype == NL80211_IFTYPE_P2P_GO);
   1184 
   1185 	/*
   1186 	 * Subscribe to the response frames now instead of in the ACK
   1187 	 * callback to save ourselves race condition considerations.
   1188 	 */
   1189 	while (1) {
   1190 		struct frame_xchg_prefix *prefix;
   1191 		struct frame_xchg_watch_data *watch;
   1192 
   1193 		prefix = va_arg(resp_args, struct frame_xchg_prefix *);
   1194 		if (!prefix)
   1195 			break;
   1196 
   1197 		watch = l_new(struct frame_xchg_watch_data, 1);
   1198 		watch->prefix = prefix;
   1199 		watch->cb = va_arg(resp_args, void *);
   1200 		frame_watch_add(wdev_id, group_id, prefix->frame_type,
   1201 				prefix->data, prefix->len, false,
   1202 				frame_xchg_resp_cb, fx, NULL);
   1203 
   1204 		if (!fx->rx_watches)
   1205 			fx->rx_watches = l_queue_new();
   1206 
   1207 		l_queue_push_tail(fx->rx_watches, watch);
   1208 	}
   1209 
   1210 	fx->retry_cnt = 0;
   1211 
   1212 	l_queue_push_tail(frame_xchgs, fx);
   1213 
   1214 	/*
   1215 	 * TODO: Assume any offchannel frames are a high priority (0). This may
   1216 	 * need to be re-examined in the future if other operations (e.g.
   1217 	 * wait on channel) are introduced.
   1218 	 */
   1219 	return wiphy_radio_work_insert(wiphy_find_by_wdev(wdev_id),
   1220 					&fx->work, WIPHY_WORK_PRIORITY_FRAME,
   1221 					&work_ops);
   1222 }
   1223 
   1224 static bool frame_xchg_cancel_by_wdev(void *data, void *user_data)
   1225 {
   1226 	struct frame_xchg_data *fx = data;
   1227 	const uint64_t *wdev_id = user_data;
   1228 
   1229 	if (fx->wdev_id != *wdev_id)
   1230 		return false;
   1231 
   1232 	wiphy_radio_work_done(wiphy_find_by_wdev(fx->wdev_id), fx->work.id);
   1233 	return true;
   1234 }
   1235 
   1236 void frame_xchg_stop_wdev(uint64_t wdev_id)
   1237 {
   1238 	l_queue_foreach_remove(frame_xchgs, frame_xchg_cancel_by_wdev,
   1239 				&wdev_id);
   1240 }
   1241 
   1242 static bool frame_xchg_match_id(const void *a, const void *b)
   1243 {
   1244 	const struct frame_xchg_data *fx = a;
   1245 	const uint32_t *id = b;
   1246 
   1247 	return fx->work.id == *id;
   1248 }
   1249 
   1250 void frame_xchg_cancel(uint32_t id)
   1251 {
   1252 	struct frame_xchg_data *fx =
   1253 		l_queue_remove_if(frame_xchgs, frame_xchg_match_id, &id);
   1254 
   1255 	if (!fx)
   1256 		return;
   1257 
   1258 	wiphy_radio_work_done(wiphy_find_by_wdev(fx->wdev_id), id);
   1259 }
   1260 
   1261 static void frame_xchg_mlme_notify(struct l_genl_msg *msg, void *user_data)
   1262 {
   1263 	uint64_t wdev_id;
   1264 	struct frame_xchg_data *fx;
   1265 	uint64_t cookie;
   1266 	bool ack;
   1267 	uint8_t cmd = l_genl_msg_get_command(msg);
   1268 
   1269 	switch (cmd) {
   1270 	case NL80211_CMD_FRAME_TX_STATUS:
   1271 		if (nl80211_parse_attrs(msg, NL80211_ATTR_WDEV, &wdev_id,
   1272 					NL80211_ATTR_COOKIE, &cookie,
   1273 					NL80211_ATTR_ACK, &ack,
   1274 					NL80211_ATTR_UNSPEC) < 0)
   1275 			return;
   1276 
   1277 		fx = l_queue_find(frame_xchgs, frame_xchg_match_running,
   1278 					&wdev_id);
   1279 		if (!fx)
   1280 			return;
   1281 
   1282 		l_debug("Received %s for cookie: %"PRIu64,
   1283 				ack ? "an ACK" : "no ACK", cookie);
   1284 
   1285 		if (fx->have_cookie && cookie == fx->cookie && !fx->tx_acked)
   1286 			frame_xchg_tx_status(fx, ack);
   1287 		else if (!fx->have_cookie && !fx->tx_acked) {
   1288 			/*
   1289 			 * Save the information about the frame's ACK status
   1290 			 * to be processed in frame_xchg_tx_cb if we were
   1291 			 * called before it (happens on brcmfmac).
   1292 			 */
   1293 			fx->tx_acked = ack;
   1294 			fx->cookie = cookie;
   1295 			fx->early_status = true;
   1296 		}
   1297 
   1298 		break;
   1299 	}
   1300 }
   1301 
   1302 static void frame_xchg_config_notify(struct l_genl_msg *msg, void *user_data)
   1303 {
   1304 	uint64_t wdev_id;
   1305 	uint32_t iftype;
   1306 	struct wdev_info *wdev;
   1307 
   1308 	switch (l_genl_msg_get_command(msg)) {
   1309 	case NL80211_CMD_NEW_INTERFACE:
   1310 	case NL80211_CMD_SET_INTERFACE:
   1311 		if (nl80211_parse_attrs(msg, NL80211_ATTR_WDEV, &wdev_id,
   1312 					NL80211_ATTR_IFTYPE, &iftype,
   1313 					NL80211_ATTR_UNSPEC) < 0)
   1314 			break;
   1315 
   1316 		wdev = l_queue_find(wdevs, frame_xchg_wdev_match, &wdev_id);
   1317 
   1318 		if (!wdev) {
   1319 			wdev = l_new(struct wdev_info, 1);
   1320 			wdev->id = wdev_id;
   1321 
   1322 			if (!wdevs)
   1323 				wdevs = l_queue_new();
   1324 
   1325 			l_queue_push_tail(wdevs, wdev);
   1326 			break;
   1327 		}
   1328 
   1329 		wdev->iftype = iftype;
   1330 		break;
   1331 
   1332 	case NL80211_CMD_DEL_INTERFACE:
   1333 		if (nl80211_parse_attrs(msg, NL80211_ATTR_WDEV, &wdev_id,
   1334 					NL80211_ATTR_UNSPEC) < 0)
   1335 			break;
   1336 
   1337 		wdev = l_queue_remove_if(wdevs, frame_xchg_wdev_match, &wdev_id);
   1338 		if (!wdev)
   1339 			break;
   1340 
   1341 		l_free(wdev);
   1342 		frame_watch_wdev_remove(wdev_id);
   1343 		break;
   1344 	}
   1345 }
   1346 
   1347 static int frame_xchg_init(void)
   1348 {
   1349 	struct watch_group *default_group;
   1350 	const struct l_genl_family_info *info;
   1351 
   1352 	nl80211 = l_genl_family_new(iwd_get_genl(), NL80211_GENL_NAME);
   1353 	if (!nl80211) {
   1354 		l_error("Failed to obtain nl80211");
   1355 		return -EIO;
   1356 	}
   1357 
   1358 	info = l_genl_family_get_info(nl80211);
   1359 	nl80211_id = l_genl_family_info_get_id(info);
   1360 
   1361 	default_group = frame_watch_group_new(0, 0);
   1362 	if (!default_group)
   1363 		goto error;
   1364 
   1365 	if (!l_genl_family_register(nl80211, "config", frame_xchg_config_notify,
   1366 					NULL, NULL)) {
   1367 		l_error("Registering for config notifications failed");
   1368 		goto error;
   1369 	}
   1370 
   1371 	if (!l_genl_family_register(nl80211, "mlme", frame_xchg_mlme_notify,
   1372 					NULL, NULL)) {
   1373 		l_error("Registering for MLME notification failed");
   1374 		goto error;
   1375 	}
   1376 
   1377 	watch_groups = l_queue_new();
   1378 	l_queue_push_tail(watch_groups, default_group);
   1379 
   1380 	return 0;
   1381 
   1382 error:
   1383 	if (nl80211) {
   1384 		l_genl_family_free(nl80211);
   1385 		nl80211 = NULL;
   1386 	}
   1387 
   1388 	if (default_group)
   1389 		frame_watch_group_destroy(default_group);
   1390 
   1391 	return -EIO;
   1392 }
   1393 
   1394 static void destroy_xchg_data(void *user_data)
   1395 {
   1396 	struct frame_xchg_data *fx = user_data;
   1397 
   1398 	wiphy_radio_work_done(wiphy_find_by_wdev(fx->wdev_id), fx->work.id);
   1399 }
   1400 
   1401 static void frame_xchg_exit(void)
   1402 {
   1403 	struct l_queue *groups = watch_groups;
   1404 	struct l_queue *xchgs = frame_xchgs;
   1405 
   1406 	frame_xchgs = NULL;
   1407 	l_queue_destroy(xchgs, destroy_xchg_data);
   1408 
   1409 	watch_groups = NULL;
   1410 	l_queue_destroy(groups, frame_watch_group_destroy);
   1411 
   1412 	l_genl_family_free(nl80211);
   1413 	nl80211 = NULL;
   1414 
   1415 	l_queue_destroy(wdevs, l_free);
   1416 	wdevs = NULL;
   1417 }
   1418 
   1419 IWD_MODULE(frame_xchg, frame_xchg_init, frame_xchg_exit);
   1420 IWD_MODULE_DEPENDS(frame_xchg, wiphy)