eiwd

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

rrm.c (23025B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 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 <stdint.h>
     28 #include <linux/if_ether.h>
     29 
     30 #include <ell/ell.h>
     31 
     32 #include "ell/useful.h"
     33 #include "src/module.h"
     34 #include "src/mpdu.h"
     35 #include "src/netdev.h"
     36 #include "src/iwd.h"
     37 #include "src/ie.h"
     38 #include "src/util.h"
     39 #include "src/station.h"
     40 #include "src/scan.h"
     41 #include "src/nl80211util.h"
     42 #include "src/wiphy.h"
     43 #include "src/frame-xchg.h"
     44 #include "src/band.h"
     45 
     46 #include "linux/nl80211.h"
     47 
     48 /* Limit requests per second */
     49 #define MAX_REQUESTS_PER_SEC		2ULL
     50 /* Microseconds between requests */
     51 #define MIN_MICROS_BETWEEN_REQUESTS	(1000000ULL / MAX_REQUESTS_PER_SEC)
     52 
     53 /* 802.11-2016 Table 9-90 */
     54 #define REPORT_DETAIL_NO_FIELDS_OR_ELEMS		0
     55 #define REPORT_DETAIL_ALL_FIELDS_AND_ANY_REQUEST_ELEMS	1
     56 #define REPORT_DETAIL_ALL_FIELDS_AND_ELEMS		2
     57 
     58 /* 802.11-2016 Table 9-192 */
     59 #define REPORT_REJECT_LATE	(1 << 0)
     60 #define REPORT_REJECT_INCAPABLE	(1 << 1)
     61 #define REPORT_REJECT_REFUSED	(1 << 2)
     62 
     63 /* 802.11-2016 Table 9-87 */
     64 enum rrm_beacon_req_mode {
     65 	RRM_BEACON_REQ_MODE_PASSIVE =	0,
     66 	RRM_BEACON_REQ_MODE_ACTIVE =	1,
     67 	RRM_BEACON_REQ_MODE_TABLE =	2,
     68 };
     69 
     70 /* 802.11-2016 Table 9-88 */
     71 enum rrm_beacon_req_subelem_id {
     72 	RRM_BEACON_REQ_SUBELEM_ID_SSID			= 0,
     73 	RRM_BEACON_REQ_SUBELEM_ID_BEACON_REPORTING	= 1,
     74 	RRM_BEACON_REQ_SUBELEM_ID_REPORTING_DETAIL	= 2,
     75 	/* 3 - 9 reserved */
     76 	RRM_BEACON_REQ_SUBELEM_ID_REQUEST		= 10,
     77 	RRM_BEACON_REQ_SUBELEM_ID_EXT_REQUEST		= 11,
     78 	/* 12 - 50 reserved */
     79 	RRM_BEACON_REQ_SUBELEM_ID_AP_CHAN_REPORT	= 51,
     80 	/* 52 - 162 reserved */
     81 	RRM_BEACON_REQ_SUBELEM_ID_WIDE_BAND_SWITCH	= 163,
     82 	/* 164 - 220 reserved */
     83 	RRM_BEACON_REQ_SUBELEM_ID_VENDOR		= 221,
     84 	/* 222 - 255 reserved */
     85 };
     86 
     87 /* 802.11-2016 Annex C - dot11PHYType */
     88 enum rrm_phy_type {
     89 	RRM_PHY_TYPE_DSSS	= 2,
     90 	RRM_PHY_TYPE_OFDM	= 4,
     91 	RRM_PHY_TYPE_HRDSSS	= 5,
     92 	RRM_PHY_TYPE_ERP	= 6,
     93 	RRM_PHY_TYPE_HT		= 7,
     94 	RRM_PHY_TYPE_DMG	= 8,
     95 	RRM_PHY_TYPE_VHT	= 9,
     96 	RRM_PHY_TYPE_TVHT	= 10,
     97 };
     98 
     99 struct rrm_request_info {
    100 	uint8_t dialog_token;	/* dialog token in Radio Measurement Request */
    101 	uint8_t mtoken;		/* token in measurement request element */
    102 	uint8_t mode;
    103 	uint8_t type;		/* request type (only beacon supported) */
    104 };
    105 
    106 struct rrm_beacon_req_info {
    107 	struct rrm_request_info info;
    108 	uint8_t oper_class;
    109 	uint8_t channel;	/* The single channel provided in request */
    110 	uint16_t duration;
    111 	uint8_t bssid[6];		/* Request filtered by BSSID */
    112 	char ssid[SSID_MAX_SIZE + 1];	/* Request filtered by SSID */
    113 	bool has_ssid;
    114 	uint32_t scan_id;
    115 	uint64_t scan_start_time;
    116 };
    117 
    118 /* Per-netdev state */
    119 struct rrm_state {
    120 	struct station *station;
    121 	uint32_t watch_id;
    122 	uint32_t ifindex;
    123 	uint64_t wdev_id;
    124 	struct rrm_request_info *pending;
    125 
    126 	uint64_t last_request;
    127 };
    128 
    129 /* 802.11, Section 9.4.2.22.7 */
    130 struct rrm_beacon_report {
    131 	uint8_t oper_class;
    132 	uint8_t channel;
    133 	__le64 scan_start_time;
    134 	__le16 duration;
    135 	uint8_t frame_info;
    136 	uint8_t rcpi;
    137 	uint8_t rsni;
    138 	uint8_t bssid[6];
    139 	uint8_t antenna_id;
    140 	__le32 parent_tsf;
    141 	uint8_t subelements[];
    142 } __attribute__ ((packed));
    143 
    144 static struct l_queue *states;
    145 static struct l_genl_family *nl80211;
    146 static uint32_t netdev_watch;
    147 
    148 static void rrm_info_destroy(void *data)
    149 {
    150 	struct rrm_request_info *info = data;
    151 	/* TODO: once more request types are added, check type */
    152 	struct rrm_beacon_req_info *beacon = l_container_of(info,
    153 						struct rrm_beacon_req_info,
    154 						info);
    155 
    156 	l_free(beacon);
    157 }
    158 
    159 static uint8_t rrm_phy_type(struct scan_bss *bss)
    160 {
    161 	if (bss->vht_capable)
    162 		return RRM_PHY_TYPE_VHT;
    163 
    164 	if (bss->ht_capable)
    165 		return RRM_PHY_TYPE_HT;
    166 
    167 	/*
    168 	 * Default to 802.11g phy type. You can get quite fancy here determining
    169 	 * the phy type by looking at the frequency and operator class among
    170 	 * other things. Since 802.11a/b are so old, defaulting to 802.11g just
    171 	 * removes a lot of complexity. Above, HT/VHT are easy as all you need
    172 	 * to look for is the presence of the IE.
    173 	 */
    174 	return RRM_PHY_TYPE_ERP;
    175 }
    176 
    177 static void rrm_send_response_cb(struct l_genl_msg *msg, void *user_data)
    178 {
    179 	int err = l_genl_msg_get_error(msg);
    180 
    181 	if (err < 0)
    182 		l_error("Error sending response: %d", err);
    183 }
    184 
    185 static bool rrm_send_response(struct rrm_state *rrm,
    186 				const uint8_t *frame, size_t len)
    187 {
    188 	struct netdev *netdev = netdev_find(rrm->ifindex);
    189 	const uint8_t *own_addr = netdev_get_address(netdev);
    190 	struct scan_bss *bss = station_get_connected_bss(rrm->station);
    191 	struct l_genl_msg *msg;
    192 	struct iovec iov;
    193 
    194 	iov.iov_base = (void *)frame;
    195 	iov.iov_len = len;
    196 
    197 	msg = nl80211_build_cmd_frame(rrm->ifindex, 0x00d0, own_addr, bss->addr,
    198 					bss->frequency, &iov, 1);
    199 
    200 	if (!l_genl_family_send(nl80211, msg, rrm_send_response_cb,
    201 					NULL, NULL)) {
    202 		l_genl_msg_unref(msg);
    203 		l_error("Failed to send report for "MAC,
    204 				MAC_STR(bss->addr));
    205 		return false;
    206 	}
    207 
    208 	return true;
    209 }
    210 
    211 static void rrm_reject_measurement_request(struct rrm_state *rrm,
    212 						uint8_t mode)
    213 {
    214 	struct rrm_request_info *info = rrm->pending;
    215 	uint8_t frame[8];
    216 
    217 	frame[0] = 0x05; /* Category: Radio Measurement */
    218 	frame[1] = 0x01; /* Action: Radio Measurement Report */
    219 	frame[2] = info->dialog_token;
    220 	frame[3] = IE_TYPE_MEASUREMENT_REPORT;
    221 	frame[4] = 3;
    222 	frame[5] = info->mtoken;
    223 	frame[6] = mode;
    224 	frame[7] = info->type;
    225 
    226 	if (!rrm_send_response(rrm, frame, sizeof(frame)))
    227 		l_error("failed to send rejection");
    228 
    229 	rrm_info_destroy(info);
    230 	rrm->pending = NULL;
    231 }
    232 
    233 static void rrm_build_measurement_report(struct rrm_request_info *info,
    234 				const void *report, size_t report_len,
    235 				uint8_t *to)
    236 {
    237 	*to++ = IE_TYPE_MEASUREMENT_REPORT;
    238 	*to++ = 3 + report_len;
    239 	*to++ = info->mtoken;
    240 	*to++ = 0;
    241 	*to++ = info->type;
    242 
    243 	if (report)
    244 		memcpy(to, report, report_len);
    245 }
    246 
    247 /* 802.11 Table 9-154 */
    248 static uint8_t mdb_to_rcpi(int32_t mdb)
    249 {
    250 	if (mdb <= -10950)
    251 		return 0;
    252 	else if (mdb >= -10950 && mdb < 0)
    253 		return (2 * (mdb + 11000)) / 100;
    254 	else
    255 		return 220;
    256 }
    257 
    258 /*
    259  * 802.11-2016 11.11.9.1 Beacon report
    260  *
    261  * "If the stored beacon information is based on a measurement made by
    262  *  the reporting STA, and if the actual measurement start time,
    263  *  measurement duration, and Parent TSF are available for this
    264  *  measurement, then the beacon report shall include the actual
    265  *  measurement start time, measurement duration, and Parent TSF;
    266  *  otherwise the actual measurement start time, measurement duration,
    267  *  and Parent TSF shall be set to 0. The RCPI and RSNI for that stored
    268  *  beacon measurement may be included in the beacon report; otherwise
    269  *  the beacon report shall indicate that RCPI and RSNI measurements
    270  *  are not available"
    271  *
    272  * Since accurate timing is unreliable we are setting start/duration/TSF time to
    273  * zero for all cases (table, passive, active).
    274  */
    275 static size_t build_report_for_bss(struct rrm_beacon_req_info *beacon,
    276 					struct scan_bss *bss,
    277 					uint8_t *to)
    278 {
    279 	struct rrm_beacon_report *report = (struct rrm_beacon_report *) to;
    280 
    281 	report->oper_class = beacon->oper_class;
    282 	report->channel = band_freq_to_channel(bss->frequency, NULL);
    283 	report->scan_start_time = L_CPU_TO_LE64(beacon->scan_start_time);
    284 	report->duration = L_CPU_TO_LE16(beacon->duration);
    285 	report->frame_info = rrm_phy_type(bss);
    286 	report->rcpi = mdb_to_rcpi(bss->signal_strength);
    287 
    288 	/* RSNI not available (could get this from GET_SURVEY) */
    289 	report->rsni = 255;
    290 	memcpy(report->bssid, bss->addr, 6);
    291 	/* Antenna identifier unknown */
    292 	report->antenna_id = 0;
    293 
    294 	/*
    295 	 * 802.11 9.4.2.22.7 Beacon report
    296 	 *
    297 	 * "The Parent TSF field contains the lower 4 octets of the measuring
    298 	 *  STA's TSF timer value"
    299 	 */
    300 	report->parent_tsf = L_CPU_TO_LE32(bss->parent_tsf);
    301 
    302 	/*
    303 	 * TODO: Support optional subelements
    304 	 *
    305 	 * (see "TODO: Support Reported Frame Body..." below)
    306 	 */
    307 
    308 	return sizeof(struct rrm_beacon_report);
    309 }
    310 
    311 static bool bss_in_request_range(struct rrm_beacon_req_info *beacon,
    312 					struct scan_bss *bss)
    313 {
    314 	uint8_t channel = band_freq_to_channel(bss->frequency, NULL);
    315 
    316 	/* Must be a table measurement */
    317 	if (beacon->channel == 0 || beacon->channel == 255)
    318 		return true;
    319 
    320 	if (beacon->channel == channel)
    321 		return true;
    322 
    323 	return false;
    324 }
    325 
    326 static bool rrm_report_beacon_results(struct rrm_state *rrm,
    327 					struct l_queue *bss_list)
    328 {
    329 	struct rrm_beacon_req_info *beacon = l_container_of(rrm->pending,
    330 						struct rrm_beacon_req_info,
    331 						info);
    332 	bool wildcard = util_is_broadcast_address(beacon->bssid);
    333 	const struct l_queue_entry *entry;
    334 	uint8_t frame[512];
    335 	uint8_t *ptr = frame;
    336 
    337 	*ptr++ = 0x05; /* Category: Radio Measurement */
    338 	*ptr++ = 0x01; /* Action: Radio Measurement Report */
    339 	*ptr++ = beacon->info.dialog_token;
    340 
    341 	for (entry = l_queue_get_entries(bss_list); entry;
    342 							entry = entry->next) {
    343 		struct scan_bss *bss = entry->data;
    344 		uint8_t report[257];
    345 		size_t report_len;
    346 
    347 		/* If request included a specific BSSID match only this BSS */
    348 		if (!wildcard && memcmp(bss->addr, beacon->bssid, 6) != 0)
    349 			continue;
    350 
    351 		/* If request was for a certain SSID, match only this SSID */
    352 		if (beacon->has_ssid && strncmp(beacon->ssid,
    353 							(const char *)bss->ssid,
    354 							sizeof(bss->ssid)) != 0)
    355 			continue;
    356 
    357 		/*
    358 		 * The kernel may have returned a cached scan, so we have to
    359 		 * sort out any non-matching frequencies before building the
    360 		 * report
    361 		 */
    362 		if (!bss_in_request_range(beacon, bss))
    363 			continue;
    364 
    365 		report_len = build_report_for_bss(beacon, bss, report);
    366 
    367 		rrm_build_measurement_report(&beacon->info, report,
    368 						report_len, ptr);
    369 
    370 		ptr += report_len + 5;
    371 	}
    372 
    373 	rrm_info_destroy(&beacon->info);
    374 	rrm->pending = NULL;
    375 
    376 	return rrm_send_response(rrm, frame, ptr - frame);
    377 }
    378 
    379 static void rrm_handle_beacon_table(struct rrm_state *rrm,
    380 					struct rrm_beacon_req_info *beacon)
    381 {
    382 	struct l_queue *bss_list;
    383 
    384 	bss_list = station_get_bss_list(rrm->station);
    385 	if (!bss_list) {
    386 		rrm_reject_measurement_request(rrm, REPORT_REJECT_INCAPABLE);
    387 		return;
    388 	}
    389 
    390 	if (!rrm_report_beacon_results(rrm, bss_list))
    391 		l_error("Error reporting beacon table results");
    392 }
    393 
    394 static bool rrm_scan_results(int err, struct l_queue *bss_list,
    395 				const struct scan_freq_set *freqs,
    396 				void *userdata)
    397 {
    398 	struct rrm_state *rrm = userdata;
    399 	struct rrm_beacon_req_info *beacon = l_container_of(rrm->pending,
    400 						struct rrm_beacon_req_info,
    401 						info);
    402 
    403 	beacon->scan_id = 0;
    404 
    405 	l_debug("RRM scan results for %u APs", l_queue_length(bss_list));
    406 
    407 	rrm_report_beacon_results(rrm, bss_list);
    408 	/* We aren't saving this BSS list */
    409 	return false;
    410 }
    411 
    412 static void rrm_scan_triggered(int err, void *userdata)
    413 {
    414 	struct rrm_state *rrm = userdata;
    415 	struct rrm_beacon_req_info *beacon = l_container_of(rrm->pending,
    416 						struct rrm_beacon_req_info,
    417 						info);
    418 
    419 	if (err < 0) {
    420 		l_error("Could not start RRM scan");
    421 		rrm_reject_measurement_request(rrm, REPORT_REJECT_INCAPABLE);
    422 		return;
    423 	}
    424 
    425 	beacon->scan_start_time = scan_get_triggered_time(rrm->wdev_id,
    426 							beacon->scan_id);
    427 }
    428 
    429 static void rrm_handle_beacon_scan(struct rrm_state *rrm,
    430 					struct rrm_beacon_req_info *beacon,
    431 					bool passive)
    432 {
    433 	struct scan_freq_set *freqs = scan_freq_set_new();
    434 	struct scan_parameters params = {
    435 		.freqs = freqs,
    436 		.flush = true,
    437 		.duration = beacon->duration,
    438 		.duration_mandatory = test_bit(&beacon->info.mode, 4),
    439 	};
    440 	enum band_freq band = band_oper_class_to_band(NULL, beacon->oper_class);
    441 	uint32_t freq;
    442 
    443 	freq = band_channel_to_freq(beacon->channel, band);
    444 	scan_freq_set_add(freqs, freq);
    445 
    446 	if (!wiphy_constrain_freq_set(wiphy_find_by_wdev(rrm->wdev_id), freqs))
    447 		goto free_freqs;
    448 
    449 	if (passive)
    450 		beacon->scan_id = scan_passive_full(rrm->wdev_id, &params,
    451 						rrm_scan_triggered,
    452 						rrm_scan_results, rrm,
    453 						NULL);
    454 	else
    455 		beacon->scan_id = scan_active_full(rrm->wdev_id, &params,
    456 						rrm_scan_triggered,
    457 						rrm_scan_results, rrm,
    458 						NULL);
    459 
    460 free_freqs:
    461 	scan_freq_set_free(freqs);
    462 
    463 	if (beacon->scan_id)
    464 		return;
    465 
    466 	rrm_reject_measurement_request(rrm, REPORT_REJECT_INCAPABLE);
    467 }
    468 
    469 static bool rrm_verify_beacon_request(const uint8_t *request, size_t len)
    470 {
    471 	if (len < 13)
    472 		return false;
    473 
    474 	if (request[6] != RRM_BEACON_REQ_MODE_TABLE) {
    475 		/*
    476 		 * Rejecting any iterative measurements, only accepting explicit
    477 		 * channels and operating classes except for table measurements.
    478 		 */
    479 		if (request[0] == 0 || request[0] == 255 ||
    480 					request[1] == 0 || request[1] == 255)
    481 			return false;
    482 
    483 		/*
    484 		 * Not handling random interval requests. We can omit this
    485 		 * check for table requests since we just return whatever we
    486 		 * have cached.
    487 		 */
    488 		if (!l_memeqzero(request + 2, 2))
    489 			return false;
    490 	}
    491 
    492 	/* Check this is a valid operating class */
    493 	if (!band_oper_class_to_band(NULL, request[0]))
    494 		return false;
    495 
    496 	return true;
    497 }
    498 
    499 static void rrm_handle_beacon_request(struct rrm_state *rrm,
    500 					uint8_t dialog_token,
    501 					const uint8_t *request, size_t len)
    502 {
    503 	struct wiphy *wiphy = station_get_wiphy(rrm->station);
    504 	struct rrm_beacon_req_info *beacon;
    505 	struct ie_tlv_iter iter;
    506 	/*
    507 	 * 802.11-2016 - Table 9-90
    508 	 *
    509 	 * "All fixed-length fields and elements (default, used when Reporting
    510 	 *  Detail subelement is not included in a Beacon request)"
    511 	 */
    512 	uint8_t detail = REPORT_DETAIL_NO_FIELDS_OR_ELEMS;
    513 
    514 	beacon = l_new(struct rrm_beacon_req_info, 1);
    515 
    516 	beacon->info.dialog_token = dialog_token;
    517 	beacon->info.mtoken = request[0];
    518 	beacon->info.mode = request[1];
    519 	beacon->info.type = request[2];
    520 
    521 	rrm->pending = &beacon->info;
    522 	/*
    523 	 * 802.11-2016 11.11.8
    524 	 *
    525 	 * "A STA may also refuse to enable triggered autonomous
    526 	 * reporting. In this case a Measurement Report element shall be
    527 	 * returned to the requesting STA with the refused bit set to 1"
    528 	 *
    529 	 * At least for the time being, we will not support autonomous
    530 	 * reporting, so decline any request to do so.
    531 	 */
    532 	if (test_bit(&beacon->info.mode, 1))
    533 		goto reject_refused;
    534 
    535 	/*
    536 	 * Some drivers (non mac80211) do not allow setting a duration/mandatory
    537 	 * bit in scan requests. The actual duration value can be ignored in
    538 	 * this case but if the requests includes the duration mandatory bit we
    539 	 * must reject this request.
    540 	 */
    541 	if (!wiphy_has_ext_feature(wiphy, NL80211_EXT_FEATURE_SET_SCAN_DWELL)
    542 			&& test_bit(&beacon->info.mode, 4))
    543 		goto reject_incapable;
    544 
    545 	/* advance to beacon request */
    546 	request += 3;
    547 	len -= 3;
    548 
    549 	if (!rrm_verify_beacon_request(request, len))
    550 		goto reject_refused;
    551 
    552 	beacon->oper_class = request[0];
    553 	beacon->channel = request[1];
    554 	beacon->duration = l_get_le16(request + 4);
    555 	memcpy(beacon->bssid, request + 7, 6);
    556 
    557 	ie_tlv_iter_init(&iter, request + 13, len - 13);
    558 
    559 	while (ie_tlv_iter_next(&iter)) {
    560 		uint8_t length = ie_tlv_iter_get_length(&iter);
    561 		const unsigned char *data = ie_tlv_iter_get_data(&iter);
    562 
    563 		switch (ie_tlv_iter_get_tag(&iter)) {
    564 		case RRM_BEACON_REQ_SUBELEM_ID_SSID:
    565 			if (beacon->has_ssid)
    566 				continue;
    567 			/*
    568 			 * Zero length is wildcard SSID, which has the same
    569 			 * effect as no SSID.
    570 			 */
    571 			if (length > 0 && length <= 32) {
    572 				memcpy(beacon->ssid, data, length);
    573 				beacon->has_ssid = true;
    574 			}
    575 
    576 			break;
    577 		case RRM_BEACON_REQ_SUBELEM_ID_REPORTING_DETAIL:
    578 			if (length < 1) {
    579 				l_error("Invalid length in reporting detail");
    580 				goto reject_refused;
    581 			}
    582 
    583 			detail = l_get_u8(data);
    584 			break;
    585 		case RRM_BEACON_REQ_SUBELEM_ID_BEACON_REPORTING:
    586 			if (length < 2) {
    587 				l_error("Invalid length in Beacon Reporting");
    588 				goto reject_refused;
    589 			}
    590 
    591 			/*
    592 			 * 802.11-2016 9.4.2.21.7
    593 			 *
    594 			 * "The Beacon reporting subelement is optionally
    595 			 *  present in a Beacon request for repeated
    596 			 *  measurements; otherwise it is not present"
    597 			 *
    598 			 * However, some implementations send an all-zero
    599 			 * Beacon Reporting subelement.  Reporting Condition
    600 			 * of zero is 'default, used when the Beacon Reporting
    601 			 * subelement is not included in a Beacon request).'
    602 			 * Treat such elements as if they're not present.
    603 			 *
    604 			 * Otherwise, since repeated measurements are not
    605 			 * supported we can reject this request now.
    606 			 */
    607 			if (l_get_u8(data) != 0)
    608 				goto reject_incapable;
    609 
    610 			break;
    611 		case RRM_BEACON_REQ_SUBELEM_ID_AP_CHAN_REPORT:
    612 			/*
    613 			 * Only supporting single channel requests
    614 			 */
    615 			goto reject_incapable;
    616 		}
    617 	}
    618 
    619 	/*
    620 	 * TODO: Support Reported Frame Body of 1 and 2. This requires that all
    621 	 * fixed length fields are available from the scan request. Currently
    622 	 * scan.c parses out only the details we care about. There is also
    623 	 * limitations on length, and some IEs are treated specially and
    624 	 * truncated. This adds quite a bit of complexity. For now skip these
    625 	 * types of frame body reports.
    626 	 */
    627 	if (detail != REPORT_DETAIL_NO_FIELDS_OR_ELEMS) {
    628 		l_debug("Unsupported report detail");
    629 		goto reject_incapable;
    630 	}
    631 
    632 	/* Mode */
    633 	switch (request[6]) {
    634 	case RRM_BEACON_REQ_MODE_PASSIVE:
    635 		rrm_handle_beacon_scan(rrm, beacon, true);
    636 		return;
    637 	case RRM_BEACON_REQ_MODE_ACTIVE:
    638 		rrm_handle_beacon_scan(rrm, beacon, false);
    639 		return;
    640 	case RRM_BEACON_REQ_MODE_TABLE:
    641 		rrm_handle_beacon_table(rrm, beacon);
    642 		return;
    643 	default:
    644 		l_error("Unknown beacon mode %u", request[6]);
    645 		/* fall through to refused */
    646 	}
    647 
    648 reject_refused:
    649 	rrm_reject_measurement_request(rrm, REPORT_REJECT_REFUSED);
    650 	return;
    651 
    652 reject_incapable:
    653 	rrm_reject_measurement_request(rrm, REPORT_REJECT_INCAPABLE);
    654 }
    655 
    656 static void rrm_cancel_pending(struct rrm_state *rrm)
    657 {
    658 	if (rrm->pending) {
    659 		struct rrm_beacon_req_info *beacon;
    660 
    661 		beacon = l_container_of(rrm->pending,
    662 						struct rrm_beacon_req_info,
    663 						info);
    664 		if (beacon->scan_id)
    665 			scan_cancel(rrm->wdev_id, beacon->scan_id);
    666 
    667 		rrm_info_destroy(rrm->pending);
    668 		rrm->pending = NULL;
    669 	}
    670 }
    671 
    672 static void rrm_station_watch_cb(enum station_state state, void *userdata)
    673 {
    674 	struct rrm_state *rrm = userdata;
    675 
    676 	switch (state) {
    677 	case STATION_STATE_DISCONNECTING:
    678 	case STATION_STATE_DISCONNECTED:
    679 		rrm_cancel_pending(rrm);
    680 		break;
    681 	default:
    682 		return;
    683 	}
    684 }
    685 
    686 static void rrm_station_watch_destroy(void *user_data)
    687 {
    688 	struct rrm_state *rrm = user_data;
    689 
    690 	l_debug("");
    691 
    692 	rrm_cancel_pending(rrm);
    693 	rrm->watch_id = 0;
    694 	rrm->station = NULL;
    695 }
    696 
    697 static void rrm_frame_watch_cb(const struct mmpdu_header *mpdu,
    698 				const void *body, size_t body_len,
    699 				int rssi, void *user_data)
    700 {
    701 	struct rrm_state *rrm = user_data;
    702 	const uint8_t *request = body;
    703 	uint8_t dialog_token;
    704 	struct ie_tlv_iter iter;
    705 	struct scan_bss *bss;
    706 
    707 	if (!rrm->station) {
    708 		/*
    709 		 * Most likely this is the first RRM request, find the station
    710 		 * interface and save it off for future use
    711 		 */
    712 		rrm->station = station_find(rrm->ifindex);
    713 
    714 		if (!rrm->station) {
    715 			l_error("station interface could not be found");
    716 			return;
    717 		}
    718 
    719 		rrm->watch_id = station_add_state_watch(rrm->station,
    720 						rrm_station_watch_cb, rrm,
    721 						rrm_station_watch_destroy);
    722 	}
    723 
    724 	/*
    725 	 * Ignore if not connected or already have an outstanding request
    726 	 */
    727 	if (station_get_state(rrm->station) != STATION_STATE_CONNECTED ||
    728 				rrm->pending)
    729 		return;
    730 
    731 	bss = station_get_connected_bss(rrm->station);
    732 
    733 	if (memcmp(bss->addr, mpdu->address_2, 6))
    734 		return;
    735 
    736 	if (body_len < 5)
    737 		return;
    738 
    739 	if (request[0] != 0x05)
    740 		return;
    741 
    742 	if (request[1] != 0x00)
    743 		return;
    744 
    745 	/*
    746 	 * We have reached our max requests per second, no point in continuing
    747 	 */
    748 	if (l_time_now() - rrm->last_request < MIN_MICROS_BETWEEN_REQUESTS) {
    749 		l_debug("Max requests per second reached, ignoring request");
    750 		return;
    751 	}
    752 
    753 	dialog_token = request[2];
    754 
    755 	/* Update time regardless of success */
    756 	rrm->last_request = l_time_now();
    757 
    758 	ie_tlv_iter_init(&iter, request + 5, body_len - 5);
    759 
    760 	while (ie_tlv_iter_next(&iter)) {
    761 		const uint8_t *req;
    762 		size_t req_len;
    763 
    764 		if (ie_tlv_iter_get_tag(&iter) != IE_TYPE_MEASUREMENT_REQUEST)
    765 			continue;
    766 
    767 		req = ie_tlv_iter_get_data(&iter);
    768 		req_len = ie_tlv_iter_get_length(&iter);
    769 
    770 		if (req_len < 3)
    771 			return;
    772 
    773 		switch (req[2]) {
    774 		case 5: /* beacon */
    775 			rrm_handle_beacon_request(rrm, dialog_token, req,
    776 							req_len);
    777 			break;
    778 		default:
    779 			return;
    780 		}
    781 	}
    782 }
    783 
    784 static void rrm_state_destroy(void *data)
    785 {
    786 	struct rrm_state *rrm = data;
    787 
    788 	l_warn("RRM states still exist on exit!");
    789 
    790 	l_free(rrm);
    791 }
    792 
    793 static void rrm_add_frame_watches(struct rrm_state *rrm)
    794 {
    795 	static const uint16_t frame_type = 0x00d0;
    796 	static const uint8_t prefix[] = { 0x05, 0x00 };
    797 
    798 	l_debug("");
    799 
    800 	frame_watch_add(rrm->wdev_id, 0, frame_type, prefix, sizeof(prefix),
    801 					false, rrm_frame_watch_cb, rrm, NULL);
    802 }
    803 
    804 static struct rrm_state *rrm_new_state(struct netdev *netdev)
    805 {
    806 	struct rrm_state *rrm;
    807 
    808 	rrm = l_new(struct rrm_state, 1);
    809 
    810 	rrm->last_request = l_time_now();
    811 	rrm->ifindex = netdev_get_ifindex(netdev);
    812 	rrm->wdev_id = netdev_get_wdev_id(netdev);
    813 
    814 	l_queue_push_head(states, rrm);
    815 
    816 	return rrm;
    817 }
    818 
    819 static bool match_ifindex(const void *a, const void *b)
    820 {
    821 	const struct rrm_state *rrm = a;
    822 	uint32_t ifindex = L_PTR_TO_UINT(b);
    823 
    824 	return rrm->ifindex == ifindex;
    825 }
    826 
    827 static void rrm_netdev_watch(struct netdev *netdev,
    828 				enum netdev_watch_event event, void *user_data)
    829 {
    830 	struct rrm_state *rrm;
    831 	uint32_t ifindex = netdev_get_ifindex(netdev);
    832 
    833 	switch (event) {
    834 	case NETDEV_WATCH_EVENT_NEW:
    835 		rrm = rrm_new_state(netdev);
    836 
    837 		if (netdev_get_iftype(netdev) == NETDEV_IFTYPE_STATION)
    838 			rrm_add_frame_watches(rrm);
    839 
    840 		break;
    841 	case NETDEV_WATCH_EVENT_DEL:
    842 		/*
    843 		 * This event is triggered by the netdev being removed, which
    844 		 * causes all frame watches to be unregistered by the kernel.
    845 		 * Given the above, there's no need to unregister anything
    846 		 * manually.
    847 		 */
    848 		rrm = l_queue_remove_if(states, match_ifindex,
    849 						L_UINT_TO_PTR(ifindex));
    850 		if (rrm) {
    851 			if (rrm->station && rrm->watch_id)
    852 				station_remove_state_watch(rrm->station,
    853 								rrm->watch_id);
    854 
    855 			l_free(rrm);
    856 		}
    857 
    858 		break;
    859 	case NETDEV_WATCH_EVENT_IFTYPE_CHANGE:
    860 		rrm = l_queue_find(states, match_ifindex,
    861 						L_UINT_TO_PTR(ifindex));
    862 
    863 		if (rrm && netdev_get_iftype(netdev) == NETDEV_IFTYPE_STATION)
    864 			rrm_add_frame_watches(rrm);
    865 
    866 		break;
    867 	default:
    868 		break;
    869 	}
    870 }
    871 
    872 static int rrm_init(void)
    873 {
    874 	struct l_genl *genl = iwd_get_genl();
    875 
    876 	states = l_queue_new();
    877 
    878 	nl80211 = l_genl_family_new(genl, NL80211_GENL_NAME);
    879 
    880 	netdev_watch = netdev_watch_add(rrm_netdev_watch, NULL, NULL);
    881 
    882 	return 0;
    883 }
    884 
    885 static void rrm_exit(void)
    886 {
    887 	l_genl_family_free(nl80211);
    888 	nl80211 = NULL;
    889 
    890 	netdev_watch_remove(netdev_watch);
    891 
    892 	l_queue_destroy(states, rrm_state_destroy);
    893 }
    894 
    895 IWD_MODULE(rrm, rrm_init, rrm_exit);
    896 IWD_MODULE_DEPENDS(rrm, netdev);
    897 IWD_MODULE_DEPENDS(rrm, frame_xchg);