1 /*
2  * wpa_supplicant - Radio Measurements
3  * Copyright (c) 2003-2016, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_common.h"
14 #include "wpa_supplicant_i.h"
15 #include "driver_i.h"
16 #include "bss.h"
17 #include "scan.h"
18 #include "p2p_supplicant.h"
19 
20 
wpas_rrm_neighbor_rep_timeout_handler(void * data,void * user_ctx)21 static void wpas_rrm_neighbor_rep_timeout_handler(void *data, void *user_ctx)
22 {
23 	struct rrm_data *rrm = data;
24 
25 	if (!rrm->notify_neighbor_rep) {
26 		wpa_printf(MSG_ERROR,
27 			   "RRM: Unexpected neighbor report timeout");
28 		return;
29 	}
30 
31 	wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report - NONE");
32 	rrm->notify_neighbor_rep(rrm->neighbor_rep_cb_ctx, NULL);
33 
34 	rrm->notify_neighbor_rep = NULL;
35 	rrm->neighbor_rep_cb_ctx = NULL;
36 }
37 
38 
39 /*
40  * wpas_rrm_reset - Clear and reset all RRM data in wpa_supplicant
41  * @wpa_s: Pointer to wpa_supplicant
42  */
wpas_rrm_reset(struct wpa_supplicant * wpa_s)43 void wpas_rrm_reset(struct wpa_supplicant *wpa_s)
44 {
45 	wpa_s->rrm.rrm_used = 0;
46 
47 	eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
48 			     NULL);
49 	if (wpa_s->rrm.notify_neighbor_rep)
50 		wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
51 	wpa_s->rrm.next_neighbor_rep_token = 1;
52 	wpas_clear_beacon_rep_data(wpa_s);
53 }
54 
55 
56 /*
57  * wpas_rrm_process_neighbor_rep - Handle incoming neighbor report
58  * @wpa_s: Pointer to wpa_supplicant
59  * @report: Neighbor report buffer, prefixed by a 1-byte dialog token
60  * @report_len: Length of neighbor report buffer
61  */
wpas_rrm_process_neighbor_rep(struct wpa_supplicant * wpa_s,const u8 * report,size_t report_len)62 void wpas_rrm_process_neighbor_rep(struct wpa_supplicant *wpa_s,
63 				   const u8 *report, size_t report_len)
64 {
65 	struct wpabuf *neighbor_rep;
66 
67 	wpa_hexdump(MSG_DEBUG, "RRM: New Neighbor Report", report, report_len);
68 	if (report_len < 1)
69 		return;
70 
71 	if (report[0] != wpa_s->rrm.next_neighbor_rep_token - 1) {
72 		wpa_printf(MSG_DEBUG,
73 			   "RRM: Discarding neighbor report with token %d (expected %d)",
74 			   report[0], wpa_s->rrm.next_neighbor_rep_token - 1);
75 		return;
76 	}
77 
78 	eloop_cancel_timeout(wpas_rrm_neighbor_rep_timeout_handler, &wpa_s->rrm,
79 			     NULL);
80 
81 	if (!wpa_s->rrm.notify_neighbor_rep) {
82 		wpa_printf(MSG_ERROR, "RRM: Unexpected neighbor report");
83 		return;
84 	}
85 
86 	/* skipping the first byte, which is only an id (dialog token) */
87 	neighbor_rep = wpabuf_alloc(report_len - 1);
88 	if (!neighbor_rep) {
89 		wpas_rrm_neighbor_rep_timeout_handler(&wpa_s->rrm, NULL);
90 		return;
91 	}
92 	wpabuf_put_data(neighbor_rep, report + 1, report_len - 1);
93 	wpa_printf(MSG_DEBUG, "RRM: Notifying neighbor report (token = %d)",
94 		   report[0]);
95 	wpa_s->rrm.notify_neighbor_rep(wpa_s->rrm.neighbor_rep_cb_ctx,
96 				       neighbor_rep);
97 	wpa_s->rrm.notify_neighbor_rep = NULL;
98 	wpa_s->rrm.neighbor_rep_cb_ctx = NULL;
99 }
100 
101 
102 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
103 /* Workaround different, undefined for Windows, error codes used here */
104 #define ENOTCONN -1
105 #define EOPNOTSUPP -1
106 #define ECANCELED -1
107 #endif
108 
109 /* Measurement Request element + Location Subject + Maximum Age subelement */
110 #define MEASURE_REQUEST_LCI_LEN (3 + 1 + 4)
111 /* Measurement Request element + Location Civic Request */
112 #define MEASURE_REQUEST_CIVIC_LEN (3 + 5)
113 
114 
115 /**
116  * wpas_rrm_send_neighbor_rep_request - Request a neighbor report from our AP
117  * @wpa_s: Pointer to wpa_supplicant
118  * @ssid: if not null, this is sent in the request. Otherwise, no SSID IE
119  *	  is sent in the request.
120  * @lci: if set, neighbor request will include LCI request
121  * @civic: if set, neighbor request will include civic location request
122  * @cb: Callback function to be called once the requested report arrives, or
123  *	timed out after RRM_NEIGHBOR_REPORT_TIMEOUT seconds.
124  *	In the former case, 'neighbor_rep' is a newly allocated wpabuf, and it's
125  *	the requester's responsibility to free it.
126  *	In the latter case NULL will be sent in 'neighbor_rep'.
127  * @cb_ctx: Context value to send the callback function
128  * Returns: 0 in case of success, negative error code otherwise
129  *
130  * In case there is a previous request which has not been answered yet, the
131  * new request fails. The caller may retry after RRM_NEIGHBOR_REPORT_TIMEOUT.
132  * Request must contain a callback function.
133  */
wpas_rrm_send_neighbor_rep_request(struct wpa_supplicant * wpa_s,const struct wpa_ssid_value * ssid,int lci,int civic,void (* cb)(void * ctx,struct wpabuf * neighbor_rep),void * cb_ctx)134 int wpas_rrm_send_neighbor_rep_request(struct wpa_supplicant *wpa_s,
135 				       const struct wpa_ssid_value *ssid,
136 				       int lci, int civic,
137 				       void (*cb)(void *ctx,
138 						  struct wpabuf *neighbor_rep),
139 				       void *cb_ctx)
140 {
141 	struct wpabuf *buf;
142 	const u8 *rrm_ie;
143 
144 	if (wpa_s->wpa_state != WPA_COMPLETED || wpa_s->current_ssid == NULL) {
145 		wpa_printf(MSG_DEBUG, "RRM: No connection, no RRM.");
146 		return -ENOTCONN;
147 	}
148 
149 	if (!wpa_s->rrm.rrm_used) {
150 		wpa_printf(MSG_DEBUG, "RRM: No RRM in current connection.");
151 		return -EOPNOTSUPP;
152 	}
153 
154 	rrm_ie = wpa_bss_get_ie(wpa_s->current_bss,
155 				WLAN_EID_RRM_ENABLED_CAPABILITIES);
156 	if (!rrm_ie || !(wpa_s->current_bss->caps & IEEE80211_CAP_RRM) ||
157 	    !(rrm_ie[2] & WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
158 		wpa_printf(MSG_DEBUG,
159 			   "RRM: No network support for Neighbor Report.");
160 		return -EOPNOTSUPP;
161 	}
162 
163 	/* Refuse if there's a live request */
164 	if (wpa_s->rrm.notify_neighbor_rep) {
165 		wpa_printf(MSG_DEBUG,
166 			   "RRM: Currently handling previous Neighbor Report.");
167 		return -EBUSY;
168 	}
169 
170 	/* 3 = action category + action code + dialog token */
171 	buf = wpabuf_alloc(3 + (ssid ? 2 + ssid->ssid_len : 0) +
172 			   (lci ? 2 + MEASURE_REQUEST_LCI_LEN : 0) +
173 			   (civic ? 2 + MEASURE_REQUEST_CIVIC_LEN : 0));
174 	if (buf == NULL) {
175 		wpa_printf(MSG_DEBUG,
176 			   "RRM: Failed to allocate Neighbor Report Request");
177 		return -ENOMEM;
178 	}
179 
180 	wpa_printf(MSG_DEBUG, "RRM: Neighbor report request (for %s), token=%d",
181 		   (ssid ? wpa_ssid_txt(ssid->ssid, ssid->ssid_len) : ""),
182 		   wpa_s->rrm.next_neighbor_rep_token);
183 
184 	wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
185 	wpabuf_put_u8(buf, WLAN_RRM_NEIGHBOR_REPORT_REQUEST);
186 	wpabuf_put_u8(buf, wpa_s->rrm.next_neighbor_rep_token);
187 	if (ssid) {
188 		wpabuf_put_u8(buf, WLAN_EID_SSID);
189 		wpabuf_put_u8(buf, ssid->ssid_len);
190 		wpabuf_put_data(buf, ssid->ssid, ssid->ssid_len);
191 	}
192 
193 	if (lci) {
194 		/* IEEE P802.11-REVmc/D5.0 9.4.2.21 */
195 		wpabuf_put_u8(buf, WLAN_EID_MEASURE_REQUEST);
196 		wpabuf_put_u8(buf, MEASURE_REQUEST_LCI_LEN);
197 
198 		/*
199 		 * Measurement token; nonzero number that is unique among the
200 		 * Measurement Request elements in a particular frame.
201 		 */
202 		wpabuf_put_u8(buf, 1); /* Measurement Token */
203 
204 		/*
205 		 * Parallel, Enable, Request, and Report bits are 0, Duration is
206 		 * reserved.
207 		 */
208 		wpabuf_put_u8(buf, 0); /* Measurement Request Mode */
209 		wpabuf_put_u8(buf, MEASURE_TYPE_LCI); /* Measurement Type */
210 
211 		/* IEEE P802.11-REVmc/D5.0 9.4.2.21.10 - LCI request */
212 		/* Location Subject */
213 		wpabuf_put_u8(buf, LOCATION_SUBJECT_REMOTE);
214 
215 		/* Optional Subelements */
216 		/*
217 		 * IEEE P802.11-REVmc/D5.0 Figure 9-170
218 		 * The Maximum Age subelement is required, otherwise the AP can
219 		 * send only data that was determined after receiving the
220 		 * request. Setting it here to unlimited age.
221 		 */
222 		wpabuf_put_u8(buf, LCI_REQ_SUBELEM_MAX_AGE);
223 		wpabuf_put_u8(buf, 2);
224 		wpabuf_put_le16(buf, 0xffff);
225 	}
226 
227 	if (civic) {
228 		/* IEEE P802.11-REVmc/D5.0 9.4.2.21 */
229 		wpabuf_put_u8(buf, WLAN_EID_MEASURE_REQUEST);
230 		wpabuf_put_u8(buf, MEASURE_REQUEST_CIVIC_LEN);
231 
232 		/*
233 		 * Measurement token; nonzero number that is unique among the
234 		 * Measurement Request elements in a particular frame.
235 		 */
236 		wpabuf_put_u8(buf, 2); /* Measurement Token */
237 
238 		/*
239 		 * Parallel, Enable, Request, and Report bits are 0, Duration is
240 		 * reserved.
241 		 */
242 		wpabuf_put_u8(buf, 0); /* Measurement Request Mode */
243 		/* Measurement Type */
244 		wpabuf_put_u8(buf, MEASURE_TYPE_LOCATION_CIVIC);
245 
246 		/* IEEE P802.11-REVmc/D5.0 9.4.2.21.14:
247 		 * Location Civic request */
248 		/* Location Subject */
249 		wpabuf_put_u8(buf, LOCATION_SUBJECT_REMOTE);
250 		wpabuf_put_u8(buf, 0); /* Civic Location Type: IETF RFC 4776 */
251 		/* Location Service Interval Units: Seconds */
252 		wpabuf_put_u8(buf, 0);
253 		/* Location Service Interval: 0 - Only one report is requested
254 		 */
255 		wpabuf_put_le16(buf, 0);
256 		/* No optional subelements */
257 	}
258 
259 	wpa_s->rrm.next_neighbor_rep_token++;
260 
261 	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
262 				wpa_s->own_addr, wpa_s->bssid,
263 				wpabuf_head(buf), wpabuf_len(buf), 0) < 0) {
264 		wpa_printf(MSG_DEBUG,
265 			   "RRM: Failed to send Neighbor Report Request");
266 		wpabuf_free(buf);
267 		return -ECANCELED;
268 	}
269 
270 	wpa_s->rrm.neighbor_rep_cb_ctx = cb_ctx;
271 	wpa_s->rrm.notify_neighbor_rep = cb;
272 	eloop_register_timeout(RRM_NEIGHBOR_REPORT_TIMEOUT, 0,
273 			       wpas_rrm_neighbor_rep_timeout_handler,
274 			       &wpa_s->rrm, NULL);
275 
276 	wpabuf_free(buf);
277 	return 0;
278 }
279 
280 
wpas_rrm_report_elem(struct wpabuf ** buf,u8 token,u8 mode,u8 type,const u8 * data,size_t data_len)281 static int wpas_rrm_report_elem(struct wpabuf **buf, u8 token, u8 mode, u8 type,
282 				const u8 *data, size_t data_len)
283 {
284 	if (wpabuf_resize(buf, 5 + data_len))
285 		return -1;
286 
287 	wpabuf_put_u8(*buf, WLAN_EID_MEASURE_REPORT);
288 	wpabuf_put_u8(*buf, 3 + data_len);
289 	wpabuf_put_u8(*buf, token);
290 	wpabuf_put_u8(*buf, mode);
291 	wpabuf_put_u8(*buf, type);
292 
293 	if (data_len)
294 		wpabuf_put_data(*buf, data, data_len);
295 
296 	return 0;
297 }
298 
299 
300 static int
wpas_rrm_build_lci_report(struct wpa_supplicant * wpa_s,const struct rrm_measurement_request_element * req,struct wpabuf ** buf)301 wpas_rrm_build_lci_report(struct wpa_supplicant *wpa_s,
302 			  const struct rrm_measurement_request_element *req,
303 			  struct wpabuf **buf)
304 {
305 	u8 subject;
306 	u16 max_age = 0;
307 	struct os_reltime t, diff;
308 	unsigned long diff_l;
309 	const u8 *subelem;
310 	const u8 *request = req->variable;
311 	size_t len = req->len - 3;
312 
313 	if (len < 1)
314 		return -1;
315 
316 	if (!wpa_s->lci)
317 		goto reject;
318 
319 	subject = *request++;
320 	len--;
321 
322 	wpa_printf(MSG_DEBUG, "Measurement request location subject=%u",
323 		   subject);
324 
325 	if (subject != LOCATION_SUBJECT_REMOTE) {
326 		wpa_printf(MSG_INFO,
327 			   "Not building LCI report - bad location subject");
328 		return 0;
329 	}
330 
331 	/* Subelements are formatted exactly like elements */
332 	wpa_hexdump(MSG_DEBUG, "LCI request subelements", request, len);
333 	subelem = get_ie(request, len, LCI_REQ_SUBELEM_MAX_AGE);
334 	if (subelem && subelem[1] == 2)
335 		max_age = WPA_GET_LE16(subelem + 2);
336 
337 	if (os_get_reltime(&t))
338 		goto reject;
339 
340 	os_reltime_sub(&t, &wpa_s->lci_time, &diff);
341 	/* LCI age is calculated in 10th of a second units. */
342 	diff_l = diff.sec * 10 + diff.usec / 100000;
343 
344 	if (max_age != 0xffff && max_age < diff_l)
345 		goto reject;
346 
347 	if (wpas_rrm_report_elem(buf, req->token,
348 				 MEASUREMENT_REPORT_MODE_ACCEPT, req->type,
349 				 wpabuf_head_u8(wpa_s->lci),
350 				 wpabuf_len(wpa_s->lci)) < 0) {
351 		wpa_printf(MSG_DEBUG, "Failed to add LCI report element");
352 		return -1;
353 	}
354 
355 	return 0;
356 
357 reject:
358 	if (wpas_rrm_report_elem(buf, req->token,
359 				 MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
360 				 req->type, NULL, 0) < 0) {
361 		wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
362 		return -1;
363 	}
364 
365 	return 0;
366 }
367 
368 
wpas_rrm_send_msr_report_mpdu(struct wpa_supplicant * wpa_s,const u8 * data,size_t len)369 static void wpas_rrm_send_msr_report_mpdu(struct wpa_supplicant *wpa_s,
370 					  const u8 *data, size_t len)
371 {
372 	struct wpabuf *report = wpabuf_alloc(len + 3);
373 
374 	if (!report)
375 		return;
376 
377 	wpabuf_put_u8(report, WLAN_ACTION_RADIO_MEASUREMENT);
378 	wpabuf_put_u8(report, WLAN_RRM_RADIO_MEASUREMENT_REPORT);
379 	wpabuf_put_u8(report, wpa_s->rrm.token);
380 
381 	wpabuf_put_data(report, data, len);
382 
383 	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
384 				wpa_s->own_addr, wpa_s->bssid,
385 				wpabuf_head(report), wpabuf_len(report), 0)) {
386 		wpa_printf(MSG_ERROR,
387 			   "RRM: Radio measurement report failed: Sending Action frame failed");
388 	}
389 
390 	wpabuf_free(report);
391 }
392 
393 
wpas_rrm_send_msr_report(struct wpa_supplicant * wpa_s,struct wpabuf * buf)394 static void wpas_rrm_send_msr_report(struct wpa_supplicant *wpa_s,
395 				     struct wpabuf *buf)
396 {
397 	int len = wpabuf_len(buf);
398 	const u8 *pos = wpabuf_head_u8(buf), *next = pos;
399 
400 #define MPDU_REPORT_LEN (int) (IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN - 3)
401 
402 	while (len) {
403 		int send_len = (len > MPDU_REPORT_LEN) ? next - pos : len;
404 
405 		if (send_len == len ||
406 		    (send_len + next[1] + 2) > MPDU_REPORT_LEN) {
407 			wpas_rrm_send_msr_report_mpdu(wpa_s, pos, send_len);
408 			len -= send_len;
409 			pos = next;
410 		}
411 
412 		next += next[1] + 2;
413 	}
414 #undef MPDU_REPORT_LEN
415 }
416 
417 
wpas_add_channel(u8 op_class,u8 chan,u8 num_primary_channels,int * freqs)418 static int wpas_add_channel(u8 op_class, u8 chan, u8 num_primary_channels,
419 			    int *freqs)
420 {
421 	size_t i;
422 
423 	for (i = 0; i < num_primary_channels; i++) {
424 		u8 primary_chan = chan - (2 * num_primary_channels - 2) + i * 4;
425 
426 		freqs[i] = ieee80211_chan_to_freq(NULL, op_class, primary_chan);
427 		/* ieee80211_chan_to_freq() is not really meant for this
428 		 * conversion of 20 MHz primary channel numbers for wider VHT
429 		 * channels, so handle those as special cases here for now. */
430 		if (freqs[i] < 0 &&
431 		    (op_class == 128 || op_class == 129 || op_class == 130))
432 			freqs[i] = 5000 + 5 * primary_chan;
433 		if (freqs[i] < 0) {
434 			wpa_printf(MSG_DEBUG,
435 				   "Beacon Report: Invalid channel %u",
436 				   chan);
437 			return -1;
438 		}
439 	}
440 
441 	return 0;
442 }
443 
444 
wpas_add_channels(const struct oper_class_map * op,struct hostapd_hw_modes * mode,int active,const u8 * channels,const u8 size)445 static int * wpas_add_channels(const struct oper_class_map *op,
446 			       struct hostapd_hw_modes *mode, int active,
447 			       const u8 *channels, const u8 size)
448 {
449 	int *freqs, *next_freq;
450 	u8 num_primary_channels, i;
451 	u8 num_chans;
452 
453 	num_chans = channels ? size :
454 		(op->max_chan - op->min_chan) / op->inc + 1;
455 
456 	if (op->bw == BW80 || op->bw == BW80P80)
457 		num_primary_channels = 4;
458 	else if (op->bw == BW160)
459 		num_primary_channels = 8;
460 	else
461 		num_primary_channels = 1;
462 
463 	/* one extra place for the zero-terminator */
464 	freqs = os_calloc(num_chans * num_primary_channels + 1, sizeof(*freqs));
465 	if (!freqs) {
466 		wpa_printf(MSG_ERROR,
467 			   "Beacon Report: Failed to allocate freqs array");
468 		return NULL;
469 	}
470 
471 	next_freq = freqs;
472 	for  (i = 0; i < num_chans; i++) {
473 		u8 chan = channels ? channels[i] : op->min_chan + i * op->inc;
474 		enum chan_allowed res = verify_channel(mode, chan, op->bw);
475 
476 		if (res == NOT_ALLOWED || (res == NO_IR && active))
477 			continue;
478 
479 		if (wpas_add_channel(op->op_class, chan, num_primary_channels,
480 				     next_freq) < 0) {
481 			os_free(freqs);
482 			return NULL;
483 		}
484 
485 		next_freq += num_primary_channels;
486 	}
487 
488 	if (!freqs[0]) {
489 		os_free(freqs);
490 		return NULL;
491 	}
492 
493 	return freqs;
494 }
495 
496 
wpas_op_class_freqs(const struct oper_class_map * op,struct hostapd_hw_modes * mode,int active)497 static int * wpas_op_class_freqs(const struct oper_class_map *op,
498 				 struct hostapd_hw_modes *mode, int active)
499 {
500 	u8 channels_80mhz[] = { 42, 58, 106, 122, 138, 155 };
501 	u8 channels_160mhz[] = { 50, 114 };
502 
503 	/*
504 	 * When adding all channels in the operating class, 80 + 80 MHz
505 	 * operating classes are like 80 MHz channels because we add all valid
506 	 * channels anyway.
507 	 */
508 	if (op->bw == BW80 || op->bw == BW80P80)
509 		return wpas_add_channels(op, mode, active, channels_80mhz,
510 					 ARRAY_SIZE(channels_80mhz));
511 
512 	if (op->bw == BW160)
513 		return wpas_add_channels(op, mode, active, channels_160mhz,
514 					 ARRAY_SIZE(channels_160mhz));
515 
516 	return wpas_add_channels(op, mode, active, NULL, 0);
517 }
518 
519 
wpas_channel_report_freqs(struct wpa_supplicant * wpa_s,int active,const char * country,const u8 * subelems,size_t len)520 static int * wpas_channel_report_freqs(struct wpa_supplicant *wpa_s, int active,
521 				       const char *country, const u8 *subelems,
522 				       size_t len)
523 {
524 	int *freqs = NULL, *new_freqs;
525 	const u8 *end = subelems + len;
526 
527 	while (end - subelems > 2) {
528 		const struct oper_class_map *op;
529 		const u8 *ap_chan_elem, *pos;
530 		u8 left;
531 		struct hostapd_hw_modes *mode;
532 
533 		ap_chan_elem = get_ie(subelems, end - subelems,
534 				      WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL);
535 		if (!ap_chan_elem)
536 			break;
537 		pos = ap_chan_elem + 2;
538 		left = ap_chan_elem[1];
539 		if (left < 1)
540 			break;
541 		subelems = ap_chan_elem + 2 + left;
542 
543 		op = get_oper_class(country, *pos);
544 		if (!op) {
545 			wpa_printf(MSG_DEBUG,
546 				   "Beacon request: unknown operating class in AP Channel Report subelement %u",
547 				   *pos);
548 			goto out;
549 		}
550 		pos++;
551 		left--;
552 
553 		mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode);
554 		if (!mode)
555 			continue;
556 
557 		/*
558 		 * For 80 + 80 MHz operating classes, this AP Channel Report
559 		 * element should be followed by another element specifying
560 		 * the second 80 MHz channel. For now just add this 80 MHz
561 		 * channel, the second 80 MHz channel will be added when the
562 		 * next element is parsed.
563 		 * TODO: Verify that this AP Channel Report element is followed
564 		 * by a corresponding AP Channel Report element as specified in
565 		 * IEEE Std 802.11-2016, 11.11.9.1.
566 		 */
567 		new_freqs = wpas_add_channels(op, mode, active, pos, left);
568 		if (new_freqs)
569 			int_array_concat(&freqs, new_freqs);
570 
571 		os_free(new_freqs);
572 	}
573 
574 	return freqs;
575 out:
576 	os_free(freqs);
577 	return NULL;
578 }
579 
580 
wpas_beacon_request_freqs(struct wpa_supplicant * wpa_s,u8 op_class,u8 chan,int active,const u8 * subelems,size_t len)581 static int * wpas_beacon_request_freqs(struct wpa_supplicant *wpa_s,
582 				       u8 op_class, u8 chan, int active,
583 				       const u8 *subelems, size_t len)
584 {
585 	int *freqs = NULL, *ext_freqs = NULL;
586 	struct hostapd_hw_modes *mode;
587 	const char *country = NULL;
588 	const struct oper_class_map *op;
589 	const u8 *elem;
590 
591 	if (!wpa_s->current_bss)
592 		return NULL;
593 	elem = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
594 	if (elem && elem[1] >= 2)
595 		country = (const char *) (elem + 2);
596 
597 	op = get_oper_class(country, op_class);
598 	if (!op) {
599 		wpa_printf(MSG_DEBUG,
600 			   "Beacon request: invalid operating class %d",
601 			   op_class);
602 		return NULL;
603 	}
604 
605 	mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode);
606 	if (!mode)
607 		return NULL;
608 
609 	switch (chan) {
610 	case 0:
611 		freqs = wpas_op_class_freqs(op, mode, active);
612 		if (!freqs)
613 			return NULL;
614 		break;
615 	case 255:
616 		/* freqs will be added from AP channel subelements */
617 		break;
618 	default:
619 		freqs = wpas_add_channels(op, mode, active, &chan, 1);
620 		if (!freqs)
621 			return NULL;
622 		break;
623 	}
624 
625 	ext_freqs = wpas_channel_report_freqs(wpa_s, active, country, subelems,
626 					      len);
627 	if (ext_freqs) {
628 		int_array_concat(&freqs, ext_freqs);
629 		os_free(ext_freqs);
630 	}
631 
632 	return freqs;
633 }
634 
635 
wpas_get_op_chan_phy(int freq,const u8 * ies,size_t ies_len,u8 * op_class,u8 * chan,u8 * phy_type)636 static int wpas_get_op_chan_phy(int freq, const u8 *ies, size_t ies_len,
637 				u8 *op_class, u8 *chan, u8 *phy_type)
638 {
639 	const u8 *ie;
640 	int sec_chan = 0, vht = 0;
641 	struct ieee80211_ht_operation *ht_oper = NULL;
642 	struct ieee80211_vht_operation *vht_oper = NULL;
643 	u8 seg0, seg1;
644 
645 	ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
646 	if (ie && ie[1] >= sizeof(struct ieee80211_ht_operation)) {
647 		u8 sec_chan_offset;
648 
649 		ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
650 		sec_chan_offset = ht_oper->ht_param &
651 			HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
652 		if (sec_chan_offset == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
653 			sec_chan = 1;
654 		else if (sec_chan_offset ==
655 			 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
656 			sec_chan = -1;
657 	}
658 
659 	ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
660 	if (ie && ie[1] >= sizeof(struct ieee80211_vht_operation)) {
661 		vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
662 
663 		switch (vht_oper->vht_op_info_chwidth) {
664 		case 1:
665 			seg0 = vht_oper->vht_op_info_chan_center_freq_seg0_idx;
666 			seg1 = vht_oper->vht_op_info_chan_center_freq_seg1_idx;
667 			if (seg1 && abs(seg1 - seg0) == 8)
668 				vht = VHT_CHANWIDTH_160MHZ;
669 			else if (seg1)
670 				vht = VHT_CHANWIDTH_80P80MHZ;
671 			else
672 				vht = VHT_CHANWIDTH_80MHZ;
673 			break;
674 		case 2:
675 			vht = VHT_CHANWIDTH_160MHZ;
676 			break;
677 		case 3:
678 			vht = VHT_CHANWIDTH_80P80MHZ;
679 			break;
680 		default:
681 			vht = VHT_CHANWIDTH_USE_HT;
682 			break;
683 		}
684 	}
685 
686 	if (ieee80211_freq_to_channel_ext(freq, sec_chan, vht, op_class,
687 					  chan) == NUM_HOSTAPD_MODES) {
688 		wpa_printf(MSG_DEBUG,
689 			   "Cannot determine operating class and channel");
690 		return -1;
691 	}
692 
693 	*phy_type = ieee80211_get_phy_type(freq, ht_oper != NULL,
694 					   vht_oper != NULL);
695 	if (*phy_type == PHY_TYPE_UNSPECIFIED) {
696 		wpa_printf(MSG_DEBUG, "Cannot determine phy type");
697 		return -1;
698 	}
699 
700 	return 0;
701 }
702 
703 
wpas_beacon_rep_add_frame_body(struct bitfield * eids,enum beacon_report_detail detail,struct wpa_bss * bss,u8 * buf,size_t buf_len)704 static int wpas_beacon_rep_add_frame_body(struct bitfield *eids,
705 					  enum beacon_report_detail detail,
706 					  struct wpa_bss *bss, u8 *buf,
707 					  size_t buf_len)
708 {
709 	u8 *ies = (u8 *) (bss + 1);
710 	size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
711 	u8 *pos = buf;
712 	int rem_len;
713 
714 	rem_len = 255 - sizeof(struct rrm_measurement_beacon_report) -
715 		sizeof(struct rrm_measurement_report_element) - 2;
716 
717 	if (detail > BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
718 		wpa_printf(MSG_DEBUG,
719 			   "Beacon Request: Invalid reporting detail: %d",
720 			   detail);
721 		return -1;
722 	}
723 
724 	if (detail == BEACON_REPORT_DETAIL_NONE)
725 		return 0;
726 
727 	/*
728 	 * Minimal frame body subelement size: EID(1) + length(1) + TSF(8) +
729 	 * beacon interval(2) + capabilities(2) = 14 bytes
730 	 */
731 	if (buf_len < 14)
732 		return 0;
733 
734 	*pos++ = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY;
735 	/* The length will be filled later */
736 	pos++;
737 	WPA_PUT_LE64(pos, bss->tsf);
738 	pos += sizeof(bss->tsf);
739 	WPA_PUT_LE16(pos, bss->beacon_int);
740 	pos += 2;
741 	WPA_PUT_LE16(pos, bss->caps);
742 	pos += 2;
743 
744 	rem_len -= pos - buf;
745 
746 	/*
747 	 * According to IEEE Std 802.11-2016, 9.4.2.22.7, if the reported frame
748 	 * body subelement causes the element to exceed the maximum element
749 	 * size, the subelement is truncated so that the last IE is a complete
750 	 * IE. So even when required to report all IEs, add elements one after
751 	 * the other and stop once there is no more room in the measurement
752 	 * element.
753 	 */
754 	while (ies_len > 2 && 2U + ies[1] <= ies_len && rem_len > 0) {
755 		if (detail == BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS ||
756 		    (eids && bitfield_is_set(eids, ies[0]))) {
757 			u8 eid = ies[0], elen = ies[1];
758 
759 			if ((eid == WLAN_EID_TIM || eid == WLAN_EID_RSN) &&
760 			    elen > 4)
761 				elen = 4;
762 			/*
763 			 * TODO: Truncate IBSS DFS element as described in
764 			 * IEEE Std 802.11-2016, 9.4.2.22.7.
765 			 */
766 
767 			if (2 + elen > buf + buf_len - pos ||
768 			    2 + elen > rem_len)
769 				break;
770 
771 			*pos++ = ies[0];
772 			*pos++ = elen;
773 			os_memcpy(pos, ies + 2, elen);
774 			pos += elen;
775 			rem_len -= 2 + elen;
776 		}
777 
778 		ies_len -= 2 + ies[1];
779 		ies += 2 + ies[1];
780 	}
781 
782 	/* Now the length is known */
783 	buf[1] = pos - buf - 2;
784 	return pos - buf;
785 }
786 
787 
wpas_add_beacon_rep(struct wpa_supplicant * wpa_s,struct wpabuf ** wpa_buf,struct wpa_bss * bss,u64 start,u64 parent_tsf)788 static int wpas_add_beacon_rep(struct wpa_supplicant *wpa_s,
789 			       struct wpabuf **wpa_buf, struct wpa_bss *bss,
790 			       u64 start, u64 parent_tsf)
791 {
792 	struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
793 	u8 *ie = (u8 *) (bss + 1);
794 	size_t ie_len = bss->ie_len + bss->beacon_ie_len;
795 	int ret;
796 	u8 buf[2000];
797 	struct rrm_measurement_beacon_report *rep;
798 
799 	if (os_memcmp(data->bssid, broadcast_ether_addr, ETH_ALEN) != 0 &&
800 	    os_memcmp(data->bssid, bss->bssid, ETH_ALEN) != 0)
801 		return 0;
802 
803 	if (data->ssid_len &&
804 	    (data->ssid_len != bss->ssid_len ||
805 	     os_memcmp(data->ssid, bss->ssid, bss->ssid_len) != 0))
806 		return 0;
807 
808 	rep = (struct rrm_measurement_beacon_report *) buf;
809 	if (wpas_get_op_chan_phy(bss->freq, ie, ie_len, &rep->op_class,
810 				 &rep->channel, &rep->report_info) < 0)
811 		return 0;
812 
813 	rep->start_time = host_to_le64(start);
814 	rep->duration = host_to_le16(data->scan_params.duration);
815 	rep->rcpi = rssi_to_rcpi(bss->level);
816 	rep->rsni = 255; /* 255 indicates that RSNI is not available */
817 	os_memcpy(rep->bssid, bss->bssid, ETH_ALEN);
818 	rep->antenna_id = 0; /* unknown */
819 	rep->parent_tsf = host_to_le32(parent_tsf);
820 
821 	ret = wpas_beacon_rep_add_frame_body(data->eids, data->report_detail,
822 					     bss, rep->variable,
823 					     sizeof(buf) - sizeof(*rep));
824 	if (ret < 0)
825 		return -1;
826 
827 	return wpas_rrm_report_elem(wpa_buf, wpa_s->beacon_rep_data.token,
828 				    MEASUREMENT_REPORT_MODE_ACCEPT,
829 				    MEASURE_TYPE_BEACON, buf,
830 				    ret + sizeof(*rep));
831 }
832 
833 
wpas_beacon_rep_no_results(struct wpa_supplicant * wpa_s,struct wpabuf ** buf)834 static int wpas_beacon_rep_no_results(struct wpa_supplicant *wpa_s,
835 				      struct wpabuf **buf)
836 {
837 	return wpas_rrm_report_elem(buf, wpa_s->beacon_rep_data.token,
838 				    MEASUREMENT_REPORT_MODE_ACCEPT,
839 				    MEASURE_TYPE_BEACON, NULL, 0);
840 }
841 
842 
wpas_beacon_rep_table(struct wpa_supplicant * wpa_s,struct wpabuf ** buf)843 static void wpas_beacon_rep_table(struct wpa_supplicant *wpa_s,
844 				  struct wpabuf **buf)
845 {
846 	size_t i;
847 
848 	for (i = 0; i < wpa_s->last_scan_res_used; i++) {
849 		if (wpas_add_beacon_rep(wpa_s, buf, wpa_s->last_scan_res[i],
850 					0, 0) < 0)
851 			break;
852 	}
853 
854 	if (!(*buf))
855 		wpas_beacon_rep_no_results(wpa_s, buf);
856 
857 	wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", *buf);
858 }
859 
860 
wpas_rrm_refuse_request(struct wpa_supplicant * wpa_s)861 static void wpas_rrm_refuse_request(struct wpa_supplicant *wpa_s)
862 {
863 	struct wpabuf *buf = NULL;
864 
865 	if (wpas_rrm_report_elem(&buf, wpa_s->beacon_rep_data.token,
866 				 MEASUREMENT_REPORT_MODE_REJECT_REFUSED,
867 				 MEASURE_TYPE_BEACON, NULL, 0)) {
868 		wpa_printf(MSG_ERROR, "RRM: Memory allocation failed");
869 		wpabuf_free(buf);
870 		return;
871 	}
872 
873 	wpas_rrm_send_msr_report(wpa_s, buf);
874 	wpas_clear_beacon_rep_data(wpa_s);
875 	wpabuf_free(buf);
876 }
877 
878 
wpas_rrm_scan_timeout(void * eloop_ctx,void * timeout_ctx)879 static void wpas_rrm_scan_timeout(void *eloop_ctx, void *timeout_ctx)
880 {
881 	struct wpa_supplicant *wpa_s = eloop_ctx;
882 	struct wpa_driver_scan_params *params =
883 		&wpa_s->beacon_rep_data.scan_params;
884 	u16 prev_duration = params->duration;
885 
886 	if (!wpa_s->current_bss)
887 		return;
888 
889 	if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL) &&
890 	    params->duration) {
891 		wpa_printf(MSG_DEBUG,
892 			   "RRM: Cannot set scan duration due to missing driver support");
893 		params->duration = 0;
894 	}
895 	os_get_reltime(&wpa_s->beacon_rep_scan);
896 	if (wpa_s->scanning || wpas_p2p_in_progress(wpa_s) ||
897 	    wpa_supplicant_trigger_scan(wpa_s, params))
898 		wpas_rrm_refuse_request(wpa_s);
899 	params->duration = prev_duration;
900 }
901 
902 
wpas_rm_handle_beacon_req_subelem(struct wpa_supplicant * wpa_s,struct beacon_rep_data * data,u8 sid,u8 slen,const u8 * subelem)903 static int wpas_rm_handle_beacon_req_subelem(struct wpa_supplicant *wpa_s,
904 					     struct beacon_rep_data *data,
905 					     u8 sid, u8 slen, const u8 *subelem)
906 {
907 	u8 report_info, i;
908 
909 	switch (sid) {
910 	case WLAN_BEACON_REQUEST_SUBELEM_SSID:
911 		if (!slen) {
912 			wpa_printf(MSG_DEBUG,
913 				   "SSID subelement with zero length - wildcard SSID");
914 			break;
915 		}
916 
917 		if (slen > SSID_MAX_LEN) {
918 			wpa_printf(MSG_DEBUG,
919 				   "Invalid SSID subelement length: %u", slen);
920 			return -1;
921 		}
922 
923 		data->ssid_len = slen;
924 		os_memcpy(data->ssid, subelem, data->ssid_len);
925 		break;
926 	case WLAN_BEACON_REQUEST_SUBELEM_INFO:
927 		if (slen != 2) {
928 			wpa_printf(MSG_DEBUG,
929 				   "Invalid reporting information subelement length: %u",
930 				   slen);
931 			return -1;
932 		}
933 
934 		report_info = subelem[0];
935 		if (report_info != 0) {
936 			wpa_printf(MSG_DEBUG,
937 				   "reporting information=%u is not supported",
938 				   report_info);
939 			return 0;
940 		}
941 		break;
942 	case WLAN_BEACON_REQUEST_SUBELEM_DETAIL:
943 		if (slen != 1) {
944 			wpa_printf(MSG_DEBUG,
945 				   "Invalid reporting detail subelement length: %u",
946 				   slen);
947 			return -1;
948 		}
949 
950 		data->report_detail = subelem[0];
951 		if (data->report_detail >
952 		    BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
953 			wpa_printf(MSG_DEBUG, "Invalid reporting detail: %u",
954 				   subelem[0]);
955 			return 0;
956 		}
957 
958 		break;
959 	case WLAN_BEACON_REQUEST_SUBELEM_REQUEST:
960 		if (data->report_detail !=
961 		    BEACON_REPORT_DETAIL_REQUESTED_ONLY) {
962 			wpa_printf(MSG_DEBUG,
963 				   "Beacon request: request subelement is present but report detail is %u",
964 				   data->report_detail);
965 			return -1;
966 		}
967 
968 		if (!slen) {
969 			wpa_printf(MSG_DEBUG,
970 				   "Invalid request subelement length: %u",
971 				   slen);
972 			return -1;
973 		}
974 
975 		if (data->eids) {
976 			wpa_printf(MSG_DEBUG,
977 				   "Beacon Request: Request subelement appears more than once");
978 			return -1;
979 		}
980 
981 		data->eids = bitfield_alloc(255);
982 		if (!data->eids) {
983 			wpa_printf(MSG_DEBUG, "Failed to allocate EIDs bitmap");
984 			return -1;
985 		}
986 
987 		for (i = 0; i < slen; i++)
988 			bitfield_set(data->eids, subelem[i]);
989 		break;
990 	case WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL:
991 		/* Skip - it will be processed when freqs are added */
992 		break;
993 	default:
994 		wpa_printf(MSG_DEBUG,
995 			   "Beacon request: Unknown subelement id %u", sid);
996 		break;
997 	}
998 
999 	return 1;
1000 }
1001 
1002 
1003 /**
1004  * Returns 0 if the next element can be processed, 1 if some operation was
1005  * triggered, and -1 if processing failed (i.e., the element is in invalid
1006  * format or an internal error occurred).
1007  */
1008 static int
wpas_rm_handle_beacon_req(struct wpa_supplicant * wpa_s,u8 elem_token,int duration_mandatory,const struct rrm_measurement_beacon_request * req,size_t len,struct wpabuf ** buf)1009 wpas_rm_handle_beacon_req(struct wpa_supplicant *wpa_s,
1010 			  u8 elem_token, int duration_mandatory,
1011 			  const struct rrm_measurement_beacon_request *req,
1012 			  size_t len, struct wpabuf **buf)
1013 {
1014 	struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
1015 	struct wpa_driver_scan_params *params = &data->scan_params;
1016 	const u8 *subelems;
1017 	size_t elems_len;
1018 	u16 rand_interval;
1019 	u32 interval_usec;
1020 	u32 _rand;
1021 	int ret = 0, res;
1022 
1023 	if (len < sizeof(*req))
1024 		return -1;
1025 
1026 	if (req->mode != BEACON_REPORT_MODE_PASSIVE &&
1027 	    req->mode != BEACON_REPORT_MODE_ACTIVE &&
1028 	    req->mode != BEACON_REPORT_MODE_TABLE)
1029 		return 0;
1030 
1031 	subelems = req->variable;
1032 	elems_len = len - sizeof(*req);
1033 	rand_interval = le_to_host16(req->rand_interval);
1034 
1035 	os_free(params->freqs);
1036 	os_memset(params, 0, sizeof(*params));
1037 
1038 	data->token = elem_token;
1039 
1040 	/* default reporting detail is all fixed length fields and all
1041 	 * elements */
1042 	data->report_detail = BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS;
1043 	os_memcpy(data->bssid, req->bssid, ETH_ALEN);
1044 
1045 	while (elems_len >= 2) {
1046 		if (subelems[1] > elems_len - 2) {
1047 			wpa_printf(MSG_DEBUG,
1048 				   "Beacon Request: Truncated subelement");
1049 			ret = -1;
1050 			goto out;
1051 		}
1052 
1053 		res = wpas_rm_handle_beacon_req_subelem(
1054 			wpa_s, data, subelems[0], subelems[1], &subelems[2]);
1055 		if (res != 1) {
1056 			ret = res;
1057 			goto out;
1058 		}
1059 
1060 		elems_len -= 2 + subelems[1];
1061 		subelems += 2 + subelems[1];
1062 	}
1063 
1064 	if (req->mode == BEACON_REPORT_MODE_TABLE) {
1065 		wpas_beacon_rep_table(wpa_s, buf);
1066 		goto out;
1067 	}
1068 
1069 	params->freqs = wpas_beacon_request_freqs(
1070 		wpa_s, req->oper_class, req->channel,
1071 		req->mode == BEACON_REPORT_MODE_ACTIVE,
1072 		req->variable, len - sizeof(*req));
1073 	if (!params->freqs) {
1074 		wpa_printf(MSG_DEBUG, "Beacon request: No valid channels");
1075 		goto out;
1076 	}
1077 
1078 	params->duration = le_to_host16(req->duration);
1079 	params->duration_mandatory = duration_mandatory;
1080 	if (!params->duration) {
1081 		wpa_printf(MSG_DEBUG, "Beacon request: Duration is 0");
1082 		ret = -1;
1083 		goto out;
1084 	}
1085 
1086 	params->only_new_results = 1;
1087 
1088 	if (req->mode == BEACON_REPORT_MODE_ACTIVE) {
1089 		params->ssids[params->num_ssids].ssid = data->ssid;
1090 		params->ssids[params->num_ssids++].ssid_len = data->ssid_len;
1091 	}
1092 
1093 	if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
1094 		_rand = os_random();
1095 	interval_usec = (_rand % (rand_interval + 1)) * 1024;
1096 	eloop_register_timeout(0, interval_usec, wpas_rrm_scan_timeout, wpa_s,
1097 			       NULL);
1098 	return 1;
1099 out:
1100 	wpas_clear_beacon_rep_data(wpa_s);
1101 	return ret;
1102 }
1103 
1104 
1105 static int
wpas_rrm_handle_msr_req_element(struct wpa_supplicant * wpa_s,const struct rrm_measurement_request_element * req,struct wpabuf ** buf)1106 wpas_rrm_handle_msr_req_element(
1107 	struct wpa_supplicant *wpa_s,
1108 	const struct rrm_measurement_request_element *req,
1109 	struct wpabuf **buf)
1110 {
1111 	int duration_mandatory;
1112 
1113 	wpa_printf(MSG_DEBUG, "Measurement request type %d token %d",
1114 		   req->type, req->token);
1115 
1116 	if (req->mode & MEASUREMENT_REQUEST_MODE_ENABLE) {
1117 		/* Enable bit is not supported for now */
1118 		wpa_printf(MSG_DEBUG, "RRM: Enable bit not supported, ignore");
1119 		return 0;
1120 	}
1121 
1122 	if ((req->mode & MEASUREMENT_REQUEST_MODE_PARALLEL) &&
1123 	    req->type > MEASURE_TYPE_RPI_HIST) {
1124 		/* Parallel measurements are not supported for now */
1125 		wpa_printf(MSG_DEBUG,
1126 			   "RRM: Parallel measurements are not supported, reject");
1127 		goto reject;
1128 	}
1129 
1130 	duration_mandatory =
1131 		!!(req->mode & MEASUREMENT_REQUEST_MODE_DURATION_MANDATORY);
1132 
1133 	switch (req->type) {
1134 	case MEASURE_TYPE_LCI:
1135 		return wpas_rrm_build_lci_report(wpa_s, req, buf);
1136 	case MEASURE_TYPE_BEACON:
1137 		if (duration_mandatory &&
1138 		    !(wpa_s->drv_rrm_flags &
1139 		      WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL)) {
1140 			wpa_printf(MSG_DEBUG,
1141 				   "RRM: Driver does not support dwell time configuration - reject beacon report with mandatory duration");
1142 			goto reject;
1143 		}
1144 		return wpas_rm_handle_beacon_req(wpa_s, req->token,
1145 						 duration_mandatory,
1146 						 (const void *) req->variable,
1147 						 req->len - 3, buf);
1148 	default:
1149 		wpa_printf(MSG_INFO,
1150 			   "RRM: Unsupported radio measurement type %u",
1151 			   req->type);
1152 		break;
1153 	}
1154 
1155 reject:
1156 	if (wpas_rrm_report_elem(buf, req->token,
1157 				 MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
1158 				 req->type, NULL, 0) < 0) {
1159 		wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
1160 		return -1;
1161 	}
1162 
1163 	return 0;
1164 }
1165 
1166 
1167 static struct wpabuf *
wpas_rrm_process_msr_req_elems(struct wpa_supplicant * wpa_s,const u8 * pos,size_t len)1168 wpas_rrm_process_msr_req_elems(struct wpa_supplicant *wpa_s, const u8 *pos,
1169 			       size_t len)
1170 {
1171 	struct wpabuf *buf = NULL;
1172 
1173 	while (len) {
1174 		const struct rrm_measurement_request_element *req;
1175 		int res;
1176 
1177 		if (len < 2) {
1178 			wpa_printf(MSG_DEBUG, "RRM: Truncated element");
1179 			goto out;
1180 		}
1181 
1182 		req = (const struct rrm_measurement_request_element *) pos;
1183 		if (req->eid != WLAN_EID_MEASURE_REQUEST) {
1184 			wpa_printf(MSG_DEBUG,
1185 				   "RRM: Expected Measurement Request element, but EID is %u",
1186 				   req->eid);
1187 			goto out;
1188 		}
1189 
1190 		if (req->len < 3) {
1191 			wpa_printf(MSG_DEBUG, "RRM: Element length too short");
1192 			goto out;
1193 		}
1194 
1195 		if (req->len > len - 2) {
1196 			wpa_printf(MSG_DEBUG, "RRM: Element length too long");
1197 			goto out;
1198 		}
1199 
1200 		res = wpas_rrm_handle_msr_req_element(wpa_s, req, &buf);
1201 		if (res < 0)
1202 			goto out;
1203 
1204 		pos += req->len + 2;
1205 		len -= req->len + 2;
1206 	}
1207 
1208 	return buf;
1209 
1210 out:
1211 	wpabuf_free(buf);
1212 	return NULL;
1213 }
1214 
1215 
wpas_rrm_handle_radio_measurement_request(struct wpa_supplicant * wpa_s,const u8 * src,const u8 * frame,size_t len)1216 void wpas_rrm_handle_radio_measurement_request(struct wpa_supplicant *wpa_s,
1217 					       const u8 *src,
1218 					       const u8 *frame, size_t len)
1219 {
1220 	struct wpabuf *report;
1221 
1222 	if (wpa_s->wpa_state != WPA_COMPLETED) {
1223 		wpa_printf(MSG_INFO,
1224 			   "RRM: Ignoring radio measurement request: Not associated");
1225 		return;
1226 	}
1227 
1228 	if (!wpa_s->rrm.rrm_used) {
1229 		wpa_printf(MSG_INFO,
1230 			   "RRM: Ignoring radio measurement request: Not RRM network");
1231 		return;
1232 	}
1233 
1234 	if (len < 3) {
1235 		wpa_printf(MSG_INFO,
1236 			   "RRM: Ignoring too short radio measurement request");
1237 		return;
1238 	}
1239 
1240 	wpa_s->rrm.token = *frame;
1241 
1242 	/* Number of repetitions is not supported */
1243 
1244 	report = wpas_rrm_process_msr_req_elems(wpa_s, frame + 3, len - 3);
1245 	if (!report)
1246 		return;
1247 
1248 	wpas_rrm_send_msr_report(wpa_s, report);
1249 	wpabuf_free(report);
1250 }
1251 
1252 
wpas_rrm_handle_link_measurement_request(struct wpa_supplicant * wpa_s,const u8 * src,const u8 * frame,size_t len,int rssi)1253 void wpas_rrm_handle_link_measurement_request(struct wpa_supplicant *wpa_s,
1254 					      const u8 *src,
1255 					      const u8 *frame, size_t len,
1256 					      int rssi)
1257 {
1258 	struct wpabuf *buf;
1259 	const struct rrm_link_measurement_request *req;
1260 	struct rrm_link_measurement_report report;
1261 
1262 	if (wpa_s->wpa_state != WPA_COMPLETED) {
1263 		wpa_printf(MSG_INFO,
1264 			   "RRM: Ignoring link measurement request. Not associated");
1265 		return;
1266 	}
1267 
1268 	if (!wpa_s->rrm.rrm_used) {
1269 		wpa_printf(MSG_INFO,
1270 			   "RRM: Ignoring link measurement request. Not RRM network");
1271 		return;
1272 	}
1273 
1274 	if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)) {
1275 		wpa_printf(MSG_INFO,
1276 			   "RRM: Measurement report failed. TX power insertion not supported");
1277 		return;
1278 	}
1279 
1280 	req = (const struct rrm_link_measurement_request *) frame;
1281 	if (len < sizeof(*req)) {
1282 		wpa_printf(MSG_INFO,
1283 			   "RRM: Link measurement report failed. Request too short");
1284 		return;
1285 	}
1286 
1287 	os_memset(&report, 0, sizeof(report));
1288 	report.dialog_token = req->dialog_token;
1289 	report.tpc.eid = WLAN_EID_TPC_REPORT;
1290 	report.tpc.len = 2;
1291 	/* Note: The driver is expected to update report.tpc.tx_power and
1292 	 * report.tpc.link_margin subfields when sending out this frame.
1293 	 * Similarly, the driver would need to update report.rx_ant_id and
1294 	 * report.tx_ant_id subfields. */
1295 	report.rsni = 255; /* 255 indicates that RSNI is not available */
1296 	report.rcpi = rssi_to_rcpi(rssi);
1297 
1298 	/* action_category + action_code */
1299 	buf = wpabuf_alloc(2 + sizeof(report));
1300 	if (buf == NULL) {
1301 		wpa_printf(MSG_ERROR,
1302 			   "RRM: Link measurement report failed. Buffer allocation failed");
1303 		return;
1304 	}
1305 
1306 	wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
1307 	wpabuf_put_u8(buf, WLAN_RRM_LINK_MEASUREMENT_REPORT);
1308 	wpabuf_put_data(buf, &report, sizeof(report));
1309 	wpa_hexdump_buf(MSG_DEBUG, "RRM: Link measurement report", buf);
1310 
1311 	if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, src,
1312 				wpa_s->own_addr, wpa_s->bssid,
1313 				wpabuf_head(buf), wpabuf_len(buf), 0)) {
1314 		wpa_printf(MSG_ERROR,
1315 			   "RRM: Link measurement report failed. Send action failed");
1316 	}
1317 	wpabuf_free(buf);
1318 }
1319 
1320 
wpas_beacon_rep_scan_process(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res,struct scan_info * info)1321 int wpas_beacon_rep_scan_process(struct wpa_supplicant *wpa_s,
1322 				 struct wpa_scan_results *scan_res,
1323 				 struct scan_info *info)
1324 {
1325 	size_t i = 0;
1326 	struct wpabuf *buf = NULL;
1327 
1328 	if (!wpa_s->beacon_rep_data.token)
1329 		return 0;
1330 
1331 	if (!wpa_s->current_bss)
1332 		goto out;
1333 
1334 	/* If the measurement was aborted, don't report partial results */
1335 	if (info->aborted)
1336 		goto out;
1337 
1338 	wpa_printf(MSG_DEBUG, "RRM: TSF BSSID: " MACSTR " current BSS: " MACSTR,
1339 		   MAC2STR(info->scan_start_tsf_bssid),
1340 		   MAC2STR(wpa_s->current_bss->bssid));
1341 	if ((wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
1342 	    os_memcmp(info->scan_start_tsf_bssid, wpa_s->current_bss->bssid,
1343 		      ETH_ALEN) != 0) {
1344 		wpa_printf(MSG_DEBUG,
1345 			   "RRM: Ignore scan results due to mismatching TSF BSSID");
1346 		goto out;
1347 	}
1348 
1349 	for (i = 0; i < scan_res->num; i++) {
1350 		struct wpa_bss *bss =
1351 			wpa_bss_get_bssid(wpa_s, scan_res->res[i]->bssid);
1352 
1353 		if (!bss)
1354 			continue;
1355 
1356 		if ((wpa_s->drv_rrm_flags &
1357 		     WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
1358 		    os_memcmp(scan_res->res[i]->tsf_bssid,
1359 			      wpa_s->current_bss->bssid, ETH_ALEN) != 0) {
1360 			wpa_printf(MSG_DEBUG,
1361 				   "RRM: Ignore scan result for " MACSTR
1362 				   " due to mismatching TSF BSSID" MACSTR,
1363 				   MAC2STR(scan_res->res[i]->bssid),
1364 				   MAC2STR(scan_res->res[i]->tsf_bssid));
1365 			continue;
1366 		}
1367 
1368 		if (!(wpa_s->drv_rrm_flags &
1369 		      WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT)) {
1370 			struct os_reltime update_time, diff;
1371 
1372 			/* For now, allow 8 ms older results due to some
1373 			 * unknown issue with cfg80211 BSS table updates during
1374 			 * a scan with the current BSS.
1375 			 * TODO: Fix this more properly to avoid having to have
1376 			 * this type of hacks in place. */
1377 			calculate_update_time(&scan_res->fetch_time,
1378 					      scan_res->res[i]->age,
1379 					      &update_time);
1380 			os_reltime_sub(&wpa_s->beacon_rep_scan,
1381 				       &update_time, &diff);
1382 			if (os_reltime_before(&update_time,
1383 					      &wpa_s->beacon_rep_scan) &&
1384 			    (diff.sec || diff.usec >= 8000)) {
1385 				wpa_printf(MSG_DEBUG,
1386 					   "RRM: Ignore scan result for " MACSTR
1387 					   " due to old update (age(ms) %u, calculated age %u.%06u seconds)",
1388 					   MAC2STR(scan_res->res[i]->bssid),
1389 					   scan_res->res[i]->age,
1390 					   (unsigned int) diff.sec,
1391 					   (unsigned int) diff.usec);
1392 				continue;
1393 			}
1394 		}
1395 
1396 		/*
1397 		 * Don't report results that were not received during the
1398 		 * current measurement.
1399 		 */
1400 		if (info->scan_start_tsf > scan_res->res[i]->parent_tsf)
1401 			continue;
1402 
1403 		if (wpas_add_beacon_rep(wpa_s, &buf, bss, info->scan_start_tsf,
1404 					scan_res->res[i]->parent_tsf) < 0)
1405 			break;
1406 	}
1407 
1408 	if (!buf && wpas_beacon_rep_no_results(wpa_s, &buf))
1409 		goto out;
1410 
1411 	wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", buf);
1412 
1413 	wpas_rrm_send_msr_report(wpa_s, buf);
1414 	wpabuf_free(buf);
1415 
1416 out:
1417 	wpas_clear_beacon_rep_data(wpa_s);
1418 	return 1;
1419 }
1420 
1421 
wpas_clear_beacon_rep_data(struct wpa_supplicant * wpa_s)1422 void wpas_clear_beacon_rep_data(struct wpa_supplicant *wpa_s)
1423 {
1424 	struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
1425 
1426 	eloop_cancel_timeout(wpas_rrm_scan_timeout, wpa_s, NULL);
1427 	bitfield_free(data->eids);
1428 	os_free(data->scan_params.freqs);
1429 	os_memset(data, 0, sizeof(*data));
1430 }
1431