eiwd

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

offchannel.c (8990B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2021  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 
     29 #include <ell/ell.h>
     30 
     31 #include "linux/nl80211.h"
     32 
     33 #include "src/offchannel.h"
     34 #include "src/wiphy.h"
     35 #include "src/nl80211util.h"
     36 #include "src/iwd.h"
     37 #include "src/module.h"
     38 
     39 struct offchannel_info {
     40 	uint64_t wdev_id;
     41 	uint32_t freq;
     42 	uint32_t duration;
     43 
     44 	uint32_t roc_cmd_id;
     45 	uint64_t roc_cookie;
     46 	uint64_t early_cookie;
     47 
     48 	offchannel_started_cb_t started;
     49 	offchannel_destroy_cb_t destroy;
     50 	void *user_data;
     51 	int error;
     52 
     53 	struct wiphy_radio_work_item work;
     54 
     55 	bool needs_cancel : 1;
     56 };
     57 
     58 static struct l_genl_family *nl80211;
     59 static struct l_queue *offchannel_list;
     60 
     61 static bool match_id(const void *a, const void *user_data)
     62 {
     63 	const struct offchannel_info *info = a;
     64 	uint32_t id = L_PTR_TO_UINT(user_data);
     65 
     66 	return id == info->work.id;
     67 }
     68 
     69 static void offchannel_cancel_roc(struct offchannel_info *info)
     70 {
     71 	struct l_genl_msg *msg;
     72 
     73 	msg = l_genl_msg_new(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
     74 
     75 	l_genl_msg_append_attr(msg, NL80211_ATTR_WDEV, 8, &info->wdev_id);
     76 	l_genl_msg_append_attr(msg, NL80211_ATTR_COOKIE, 8, &info->roc_cookie);
     77 
     78 	/* Nothing much can be done if this fails */
     79 	if (!l_genl_family_send(nl80211, msg, NULL, NULL, NULL))
     80 		l_genl_msg_unref(msg);
     81 }
     82 
     83 static void offchannel_roc_cb(struct l_genl_msg *msg, void *user_data)
     84 {
     85 	struct offchannel_info *info = user_data;
     86 
     87 	info->error = l_genl_msg_get_error(msg);
     88 	info->roc_cmd_id = 0;
     89 
     90 	if (info->error < 0) {
     91 		l_debug("Error from CMD_REMAIN_ON_CHANNEL (%d)", info->error);
     92 		goto work_done;
     93 	}
     94 
     95 	info->error = nl80211_parse_attrs(msg, NL80211_ATTR_COOKIE,
     96 					&info->roc_cookie, NL80211_ATTR_UNSPEC);
     97 	if (info->error < 0) {
     98 		l_error("Could not parse ROC cookie");
     99 		goto work_done;
    100 	}
    101 
    102 	/*
    103 	 * If the request was cancelled prior to kernel sending the ACK,
    104 	 * cancel now.
    105 	 *
    106 	 * If the ROC event came before the ACK, call back now since the
    107 	 * callback was skipped in the notify event. There is the potential that
    108 	 * an external process issued the ROC, but if the cookies don't match
    109 	 * here we can be sure it wasn't for us. In this case the notify event
    110 	 * will behave as normal and call started().
    111 	 */
    112 	if (info->needs_cancel)
    113 		offchannel_cancel_roc(info);
    114 	else if (info->early_cookie == info->roc_cookie && info->started)
    115 		info->started(info->user_data);
    116 
    117 	return;
    118 
    119 work_done:
    120 	wiphy_radio_work_done(wiphy_find_by_wdev(info->wdev_id), info->work.id);
    121 }
    122 
    123 static bool offchannel_work_ready(struct wiphy_radio_work_item *item)
    124 {
    125 	struct l_genl_msg *msg;
    126 	struct offchannel_info *info = l_container_of(item,
    127 						struct offchannel_info, work);
    128 
    129 	msg = l_genl_msg_new(NL80211_CMD_REMAIN_ON_CHANNEL);
    130 
    131 	l_genl_msg_append_attr(msg, NL80211_ATTR_WDEV, 8, &info->wdev_id);
    132 	l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY_FREQ, 4, &info->freq);
    133 	l_genl_msg_append_attr(msg, NL80211_ATTR_DURATION, 4, &info->duration);
    134 
    135 	info->roc_cmd_id = l_genl_family_send(nl80211, msg, offchannel_roc_cb,
    136 						info, NULL);
    137 	if (!info->roc_cmd_id) {
    138 		info->error = -EIO;
    139 		l_genl_msg_unref(msg);
    140 		return true;
    141 	}
    142 
    143 	l_queue_push_head(offchannel_list, info);
    144 
    145 	return false;
    146 }
    147 
    148 static void offchannel_work_destroy(struct wiphy_radio_work_item *item)
    149 {
    150 	struct offchannel_info *info = l_container_of(item,
    151 						struct offchannel_info, work);
    152 
    153 	if (info->destroy)
    154 		info->destroy(info->error, info->user_data);
    155 
    156 	l_queue_remove(offchannel_list, info);
    157 	l_free(info);
    158 }
    159 
    160 static const struct wiphy_radio_work_item_ops offchannel_work_ops = {
    161 	.do_work = offchannel_work_ready,
    162 	.destroy = offchannel_work_destroy,
    163 };
    164 
    165 uint32_t offchannel_start(uint64_t wdev_id, int priority, uint32_t freq,
    166 			uint32_t duration, offchannel_started_cb_t started,
    167 			void *user_data, offchannel_destroy_cb_t destroy)
    168 {
    169 	struct offchannel_info *info = l_new(struct offchannel_info, 1);
    170 
    171 	info->wdev_id = wdev_id;
    172 	info->freq = freq;
    173 	info->duration = duration;
    174 	info->started = started;
    175 	info->destroy = destroy;
    176 	info->user_data = user_data;
    177 	/*
    178 	 * Set error as cancelled in case this work gets cancelled prior to
    179 	 * the wiphy work starting.
    180 	 */
    181 	info->error = -ECANCELED;
    182 
    183 	return wiphy_radio_work_insert(wiphy_find_by_wdev(wdev_id), &info->work,
    184 					priority, &offchannel_work_ops);
    185 }
    186 
    187 void offchannel_cancel(uint64_t wdev_id, uint32_t id)
    188 {
    189 	struct wiphy *wiphy = wiphy_find_by_wdev(wdev_id);
    190 	struct offchannel_info *info;
    191 	int ret;
    192 
    193 	if (!wiphy)
    194 		return;
    195 
    196 	/*
    197 	 * Exit if work does not exist, if it hasn't started 'done' the work,
    198 	 * otherwise decide how the work needs to be canceled.
    199 	 */
    200 	ret = wiphy_radio_work_is_running(wiphy, id);
    201 	if (ret < 0)
    202 		return;
    203 	else if (ret == false)
    204 		goto work_done;
    205 
    206 
    207 	info = l_queue_find(offchannel_list, match_id, L_UINT_TO_PTR(id));
    208 	if (!info)
    209 		return;
    210 
    211 	if (info->roc_cmd_id) {
    212 		/*
    213 		 * If the command hasn't left the genl queue it can be cancelled
    214 		 * without any further action. Otherwise command has been sent
    215 		 * to the kernel and we must wait until ROC starts and cancel at
    216 		 * that time.
    217 		 */
    218 		if (!l_genl_family_request_sent(nl80211, info->roc_cmd_id)) {
    219 			l_genl_family_cancel(nl80211, info->roc_cmd_id);
    220 			info->roc_cmd_id = 0;
    221 			goto work_done;
    222 		}
    223 
    224 		/* Lets the ROC callback know it needs to cancel the request */
    225 		info->needs_cancel = true;
    226 
    227 		goto destroy;
    228 	}
    229 
    230 	/*
    231 	 * Something weird must have happened on the kernel side. This error
    232 	 * will already be handled in offchannel_roc_cb but warn here to inform
    233 	 * the user.
    234 	 */
    235 	if (L_WARN_ON(!info->roc_cookie))
    236 		return;
    237 
    238 	/*
    239 	 * At this point we know ROC has at least been queued (potentially not
    240 	 * started) and can be cancelled. The work will be completed once the
    241 	 * kernel sends the cancel ROC event.
    242 	 */
    243 	offchannel_cancel_roc(info);
    244 
    245 destroy:
    246 	if (info->destroy)
    247 		info->destroy(-ECANCELED, info->user_data);
    248 
    249 	info->destroy = NULL;
    250 	info->started = NULL;
    251 	info->user_data = NULL;
    252 
    253 	return;
    254 
    255 work_done:
    256 	wiphy_radio_work_done(wiphy, id);
    257 }
    258 
    259 static void offchannel_mlme_notify(struct l_genl_msg *msg, void *user_data)
    260 {
    261 	struct offchannel_info *info = NULL;
    262 	const struct l_queue_entry *e;
    263 	uint64_t wdev_id;
    264 	uint64_t cookie;
    265 	uint8_t cmd;
    266 
    267 	cmd = l_genl_msg_get_command(msg);
    268 
    269 	if (cmd != NL80211_CMD_REMAIN_ON_CHANNEL &&
    270 			cmd != NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
    271 		return;
    272 
    273 	if (nl80211_parse_attrs(msg, NL80211_ATTR_WDEV, &wdev_id,
    274 					NL80211_ATTR_COOKIE, &cookie,
    275 					NL80211_ATTR_UNSPEC) < 0)
    276 		return;
    277 
    278 	for (e = l_queue_get_entries(offchannel_list); e; e = e->next) {
    279 		struct offchannel_info *i = e->data;
    280 
    281 		if (i->wdev_id != wdev_id)
    282 			continue;
    283 
    284 		/* Normal case, kernel has ACK'ed */
    285 		if (i->roc_cookie == cookie) {
    286 			info = i;
    287 			break;
    288 		}
    289 
    290 		/*
    291 		 * If there is a pending request and no ACK yet this could be:
    292 		 *  - an early event coming prior to the ACK
    293 		 *  - an event coming from an external ROC request (we just
    294 		 *    happened to have also sent an ROC request).
    295 		 *
    296 		 * We can't tell where the event originated until we receive our
    297 		 * ACK so set early_cookie to track it.
    298 		 */
    299 		if (i->roc_cmd_id != 0 && l_genl_family_request_sent(nl80211,
    300 							i->roc_cmd_id)) {
    301 			i->early_cookie = cookie;
    302 			return;
    303 		}
    304 	}
    305 
    306 	if (!info)
    307 		return;
    308 
    309 	switch (cmd) {
    310 	case NL80211_CMD_REMAIN_ON_CHANNEL:
    311 		if (info->started)
    312 			info->started(info->user_data);
    313 
    314 		break;
    315 	case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
    316 		info->error = 0;
    317 
    318 		wiphy_radio_work_done(wiphy_find_by_wdev(info->wdev_id),
    319 					info->work.id);
    320 		break;
    321 	default:
    322 		return;
    323 	}
    324 }
    325 
    326 static int offchannel_init(void)
    327 {
    328 	struct l_genl *genl = iwd_get_genl();
    329 
    330 	nl80211 = l_genl_family_new(genl, NL80211_GENL_NAME);
    331 	if (!nl80211) {
    332 		l_error("Failed to obtain nl80211");
    333 		return -EIO;
    334 	}
    335 
    336 	if (!l_genl_family_register(nl80211, "mlme", offchannel_mlme_notify,
    337 								NULL, NULL)) {
    338 		l_error("Failed to register for MLME");
    339 		l_genl_family_free(nl80211);
    340 		nl80211 = NULL;
    341 
    342 		return -EIO;
    343 	}
    344 
    345 	offchannel_list = l_queue_new();
    346 
    347 	return 0;
    348 }
    349 
    350 static void offchannel_exit(void)
    351 {
    352 	l_debug("");
    353 
    354 	l_genl_family_free(nl80211);
    355 	nl80211 = NULL;
    356 
    357 	l_queue_destroy(offchannel_list, l_free);
    358 }
    359 
    360 IWD_MODULE(offchannel, offchannel_init, offchannel_exit);
    361 IWD_MODULE_DEPENDS(offchannel, wiphy);