]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/rrm.c
RRM: Send reject/refuse response only to unicast measurement request
[thirdparty/hostap.git] / wpa_supplicant / rrm.c
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
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 */
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 */
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 */
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
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
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 (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
359 wpas_rrm_report_elem(buf, req->token,
360 MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
361 req->type, NULL, 0) < 0) {
362 wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
363 return -1;
364 }
365
366 return 0;
367 }
368
369
370 static void wpas_rrm_send_msr_report_mpdu(struct wpa_supplicant *wpa_s,
371 const u8 *data, size_t len)
372 {
373 struct wpabuf *report = wpabuf_alloc(len + 3);
374
375 if (!report)
376 return;
377
378 wpabuf_put_u8(report, WLAN_ACTION_RADIO_MEASUREMENT);
379 wpabuf_put_u8(report, WLAN_RRM_RADIO_MEASUREMENT_REPORT);
380 wpabuf_put_u8(report, wpa_s->rrm.token);
381
382 wpabuf_put_data(report, data, len);
383
384 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
385 wpa_s->own_addr, wpa_s->bssid,
386 wpabuf_head(report), wpabuf_len(report), 0)) {
387 wpa_printf(MSG_ERROR,
388 "RRM: Radio measurement report failed: Sending Action frame failed");
389 }
390
391 wpabuf_free(report);
392 }
393
394
395 static void wpas_rrm_send_msr_report(struct wpa_supplicant *wpa_s,
396 struct wpabuf *buf)
397 {
398 int len = wpabuf_len(buf);
399 const u8 *pos = wpabuf_head_u8(buf), *next = pos;
400
401 #define MPDU_REPORT_LEN (int) (IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN - 3)
402
403 while (len) {
404 int send_len = (len > MPDU_REPORT_LEN) ? next - pos : len;
405
406 if (send_len == len ||
407 (send_len + next[1] + 2) > MPDU_REPORT_LEN) {
408 wpas_rrm_send_msr_report_mpdu(wpa_s, pos, send_len);
409 len -= send_len;
410 pos = next;
411 }
412
413 if (len)
414 next += next[1] + 2;
415 }
416 #undef MPDU_REPORT_LEN
417 }
418
419
420 static int wpas_add_channel(u8 op_class, u8 chan, u8 num_primary_channels,
421 int *freqs)
422 {
423 size_t i;
424
425 for (i = 0; i < num_primary_channels; i++) {
426 u8 primary_chan = chan - (2 * num_primary_channels - 2) + i * 4;
427
428 freqs[i] = ieee80211_chan_to_freq(NULL, op_class, primary_chan);
429 /* ieee80211_chan_to_freq() is not really meant for this
430 * conversion of 20 MHz primary channel numbers for wider VHT
431 * channels, so handle those as special cases here for now. */
432 if (freqs[i] < 0 &&
433 (op_class == 128 || op_class == 129 || op_class == 130))
434 freqs[i] = 5000 + 5 * primary_chan;
435 if (freqs[i] < 0) {
436 wpa_printf(MSG_DEBUG,
437 "Beacon Report: Invalid channel %u",
438 chan);
439 return -1;
440 }
441 }
442
443 return 0;
444 }
445
446
447 static int * wpas_add_channels(const struct oper_class_map *op,
448 struct hostapd_hw_modes *mode, int active,
449 const u8 *channels, const u8 size)
450 {
451 int *freqs, *next_freq;
452 u8 num_primary_channels, i;
453 u8 num_chans;
454
455 num_chans = channels ? size :
456 (op->max_chan - op->min_chan) / op->inc + 1;
457
458 if (op->bw == BW80 || op->bw == BW80P80)
459 num_primary_channels = 4;
460 else if (op->bw == BW160)
461 num_primary_channels = 8;
462 else
463 num_primary_channels = 1;
464
465 /* one extra place for the zero-terminator */
466 freqs = os_calloc(num_chans * num_primary_channels + 1, sizeof(*freqs));
467 if (!freqs) {
468 wpa_printf(MSG_ERROR,
469 "Beacon Report: Failed to allocate freqs array");
470 return NULL;
471 }
472
473 next_freq = freqs;
474 for (i = 0; i < num_chans; i++) {
475 u8 chan = channels ? channels[i] : op->min_chan + i * op->inc;
476 enum chan_allowed res = verify_channel(mode, chan, op->bw);
477
478 if (res == NOT_ALLOWED || (res == NO_IR && active))
479 continue;
480
481 if (wpas_add_channel(op->op_class, chan, num_primary_channels,
482 next_freq) < 0) {
483 os_free(freqs);
484 return NULL;
485 }
486
487 next_freq += num_primary_channels;
488 }
489
490 if (!freqs[0]) {
491 os_free(freqs);
492 return NULL;
493 }
494
495 return freqs;
496 }
497
498
499 static int * wpas_op_class_freqs(const struct oper_class_map *op,
500 struct hostapd_hw_modes *mode, int active)
501 {
502 u8 channels_80mhz[] = { 42, 58, 106, 122, 138, 155 };
503 u8 channels_160mhz[] = { 50, 114 };
504
505 /*
506 * When adding all channels in the operating class, 80 + 80 MHz
507 * operating classes are like 80 MHz channels because we add all valid
508 * channels anyway.
509 */
510 if (op->bw == BW80 || op->bw == BW80P80)
511 return wpas_add_channels(op, mode, active, channels_80mhz,
512 ARRAY_SIZE(channels_80mhz));
513
514 if (op->bw == BW160)
515 return wpas_add_channels(op, mode, active, channels_160mhz,
516 ARRAY_SIZE(channels_160mhz));
517
518 return wpas_add_channels(op, mode, active, NULL, 0);
519 }
520
521
522 static int * wpas_channel_report_freqs(struct wpa_supplicant *wpa_s, int active,
523 const char *country, const u8 *subelems,
524 size_t len)
525 {
526 int *freqs = NULL, *new_freqs;
527 const u8 *end = subelems + len;
528
529 while (end - subelems > 2) {
530 const struct oper_class_map *op;
531 const u8 *ap_chan_elem, *pos;
532 u8 left;
533 struct hostapd_hw_modes *mode;
534
535 ap_chan_elem = get_ie(subelems, end - subelems,
536 WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL);
537 if (!ap_chan_elem)
538 break;
539 pos = ap_chan_elem + 2;
540 left = ap_chan_elem[1];
541 if (left < 1)
542 break;
543 subelems = ap_chan_elem + 2 + left;
544
545 op = get_oper_class(country, *pos);
546 if (!op) {
547 wpa_printf(MSG_DEBUG,
548 "Beacon request: unknown operating class in AP Channel Report subelement %u",
549 *pos);
550 goto out;
551 }
552 pos++;
553 left--;
554
555 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode);
556 if (!mode)
557 continue;
558
559 /*
560 * For 80 + 80 MHz operating classes, this AP Channel Report
561 * element should be followed by another element specifying
562 * the second 80 MHz channel. For now just add this 80 MHz
563 * channel, the second 80 MHz channel will be added when the
564 * next element is parsed.
565 * TODO: Verify that this AP Channel Report element is followed
566 * by a corresponding AP Channel Report element as specified in
567 * IEEE Std 802.11-2016, 11.11.9.1.
568 */
569 new_freqs = wpas_add_channels(op, mode, active, pos, left);
570 if (new_freqs)
571 int_array_concat(&freqs, new_freqs);
572
573 os_free(new_freqs);
574 }
575
576 return freqs;
577 out:
578 os_free(freqs);
579 return NULL;
580 }
581
582
583 static int * wpas_beacon_request_freqs(struct wpa_supplicant *wpa_s,
584 u8 op_class, u8 chan, int active,
585 const u8 *subelems, size_t len)
586 {
587 int *freqs = NULL, *ext_freqs = NULL;
588 struct hostapd_hw_modes *mode;
589 const char *country = NULL;
590 const struct oper_class_map *op;
591 const u8 *elem;
592
593 if (!wpa_s->current_bss)
594 return NULL;
595 elem = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_COUNTRY);
596 if (elem && elem[1] >= 2)
597 country = (const char *) (elem + 2);
598
599 op = get_oper_class(country, op_class);
600 if (!op) {
601 wpa_printf(MSG_DEBUG,
602 "Beacon request: invalid operating class %d",
603 op_class);
604 return NULL;
605 }
606
607 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op->mode);
608 if (!mode)
609 return NULL;
610
611 switch (chan) {
612 case 0:
613 freqs = wpas_op_class_freqs(op, mode, active);
614 if (!freqs)
615 return NULL;
616 break;
617 case 255:
618 /* freqs will be added from AP channel subelements */
619 break;
620 default:
621 freqs = wpas_add_channels(op, mode, active, &chan, 1);
622 if (!freqs)
623 return NULL;
624 break;
625 }
626
627 ext_freqs = wpas_channel_report_freqs(wpa_s, active, country, subelems,
628 len);
629 if (ext_freqs) {
630 int_array_concat(&freqs, ext_freqs);
631 os_free(ext_freqs);
632 }
633
634 return freqs;
635 }
636
637
638 static int wpas_get_op_chan_phy(int freq, const u8 *ies, size_t ies_len,
639 u8 *op_class, u8 *chan, u8 *phy_type)
640 {
641 const u8 *ie;
642 int sec_chan = 0, vht = 0;
643 struct ieee80211_ht_operation *ht_oper = NULL;
644 struct ieee80211_vht_operation *vht_oper = NULL;
645 u8 seg0, seg1;
646
647 ie = get_ie(ies, ies_len, WLAN_EID_HT_OPERATION);
648 if (ie && ie[1] >= sizeof(struct ieee80211_ht_operation)) {
649 u8 sec_chan_offset;
650
651 ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
652 sec_chan_offset = ht_oper->ht_param &
653 HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
654 if (sec_chan_offset == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
655 sec_chan = 1;
656 else if (sec_chan_offset ==
657 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
658 sec_chan = -1;
659 }
660
661 ie = get_ie(ies, ies_len, WLAN_EID_VHT_OPERATION);
662 if (ie && ie[1] >= sizeof(struct ieee80211_vht_operation)) {
663 vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
664
665 switch (vht_oper->vht_op_info_chwidth) {
666 case 1:
667 seg0 = vht_oper->vht_op_info_chan_center_freq_seg0_idx;
668 seg1 = vht_oper->vht_op_info_chan_center_freq_seg1_idx;
669 if (seg1 && abs(seg1 - seg0) == 8)
670 vht = VHT_CHANWIDTH_160MHZ;
671 else if (seg1)
672 vht = VHT_CHANWIDTH_80P80MHZ;
673 else
674 vht = VHT_CHANWIDTH_80MHZ;
675 break;
676 case 2:
677 vht = VHT_CHANWIDTH_160MHZ;
678 break;
679 case 3:
680 vht = VHT_CHANWIDTH_80P80MHZ;
681 break;
682 default:
683 vht = VHT_CHANWIDTH_USE_HT;
684 break;
685 }
686 }
687
688 if (ieee80211_freq_to_channel_ext(freq, sec_chan, vht, op_class,
689 chan) == NUM_HOSTAPD_MODES) {
690 wpa_printf(MSG_DEBUG,
691 "Cannot determine operating class and channel");
692 return -1;
693 }
694
695 *phy_type = ieee80211_get_phy_type(freq, ht_oper != NULL,
696 vht_oper != NULL);
697 if (*phy_type == PHY_TYPE_UNSPECIFIED) {
698 wpa_printf(MSG_DEBUG, "Cannot determine phy type");
699 return -1;
700 }
701
702 return 0;
703 }
704
705
706 static int wpas_beacon_rep_add_frame_body(struct bitfield *eids,
707 enum beacon_report_detail detail,
708 struct wpa_bss *bss, u8 *buf,
709 size_t buf_len)
710 {
711 u8 *ies = (u8 *) (bss + 1);
712 size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
713 u8 *pos = buf;
714 int rem_len;
715
716 rem_len = 255 - sizeof(struct rrm_measurement_beacon_report) -
717 sizeof(struct rrm_measurement_report_element) - 2;
718
719 if (detail > BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
720 wpa_printf(MSG_DEBUG,
721 "Beacon Request: Invalid reporting detail: %d",
722 detail);
723 return -1;
724 }
725
726 if (detail == BEACON_REPORT_DETAIL_NONE)
727 return 0;
728
729 /*
730 * Minimal frame body subelement size: EID(1) + length(1) + TSF(8) +
731 * beacon interval(2) + capabilities(2) = 14 bytes
732 */
733 if (buf_len < 14)
734 return 0;
735
736 *pos++ = WLAN_BEACON_REPORT_SUBELEM_FRAME_BODY;
737 /* The length will be filled later */
738 pos++;
739 WPA_PUT_LE64(pos, bss->tsf);
740 pos += sizeof(bss->tsf);
741 WPA_PUT_LE16(pos, bss->beacon_int);
742 pos += 2;
743 WPA_PUT_LE16(pos, bss->caps);
744 pos += 2;
745
746 rem_len -= pos - buf;
747
748 /*
749 * According to IEEE Std 802.11-2016, 9.4.2.22.7, if the reported frame
750 * body subelement causes the element to exceed the maximum element
751 * size, the subelement is truncated so that the last IE is a complete
752 * IE. So even when required to report all IEs, add elements one after
753 * the other and stop once there is no more room in the measurement
754 * element.
755 */
756 while (ies_len > 2 && 2U + ies[1] <= ies_len && rem_len > 0) {
757 if (detail == BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS ||
758 (eids && bitfield_is_set(eids, ies[0]))) {
759 u8 eid = ies[0], elen = ies[1];
760
761 if ((eid == WLAN_EID_TIM || eid == WLAN_EID_RSN) &&
762 elen > 4)
763 elen = 4;
764 /*
765 * TODO: Truncate IBSS DFS element as described in
766 * IEEE Std 802.11-2016, 9.4.2.22.7.
767 */
768
769 if (2 + elen > buf + buf_len - pos ||
770 2 + elen > rem_len)
771 break;
772
773 *pos++ = ies[0];
774 *pos++ = elen;
775 os_memcpy(pos, ies + 2, elen);
776 pos += elen;
777 rem_len -= 2 + elen;
778 }
779
780 ies_len -= 2 + ies[1];
781 ies += 2 + ies[1];
782 }
783
784 /* Now the length is known */
785 buf[1] = pos - buf - 2;
786 return pos - buf;
787 }
788
789
790 static int wpas_add_beacon_rep(struct wpa_supplicant *wpa_s,
791 struct wpabuf **wpa_buf, struct wpa_bss *bss,
792 u64 start, u64 parent_tsf)
793 {
794 struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
795 u8 *ie = (u8 *) (bss + 1);
796 size_t ie_len = bss->ie_len + bss->beacon_ie_len;
797 int ret;
798 u8 *buf;
799 struct rrm_measurement_beacon_report *rep;
800
801 if (os_memcmp(data->bssid, broadcast_ether_addr, ETH_ALEN) != 0 &&
802 os_memcmp(data->bssid, bss->bssid, ETH_ALEN) != 0)
803 return 0;
804
805 if (data->ssid_len &&
806 (data->ssid_len != bss->ssid_len ||
807 os_memcmp(data->ssid, bss->ssid, bss->ssid_len) != 0))
808 return 0;
809
810 /* Maximum element length: beacon report element + reported frame body
811 * subelement + all IEs of the reported beacon */
812 buf = os_malloc(sizeof(*rep) + 14 + ie_len);
813 if (!buf)
814 return -1;
815
816 rep = (struct rrm_measurement_beacon_report *) buf;
817 if (wpas_get_op_chan_phy(bss->freq, ie, ie_len, &rep->op_class,
818 &rep->channel, &rep->report_info) < 0) {
819 ret = 0;
820 goto out;
821 }
822
823 rep->start_time = host_to_le64(start);
824 rep->duration = host_to_le16(data->scan_params.duration);
825 rep->rcpi = rssi_to_rcpi(bss->level);
826 rep->rsni = 255; /* 255 indicates that RSNI is not available */
827 os_memcpy(rep->bssid, bss->bssid, ETH_ALEN);
828 rep->antenna_id = 0; /* unknown */
829 rep->parent_tsf = host_to_le32(parent_tsf);
830
831 ret = wpas_beacon_rep_add_frame_body(data->eids, data->report_detail,
832 bss, rep->variable, 14 + ie_len);
833 if (ret < 0)
834 goto out;
835
836 ret = wpas_rrm_report_elem(wpa_buf, wpa_s->beacon_rep_data.token,
837 MEASUREMENT_REPORT_MODE_ACCEPT,
838 MEASURE_TYPE_BEACON, buf,
839 ret + sizeof(*rep));
840 out:
841 os_free(buf);
842 return ret;
843 }
844
845
846 static int wpas_beacon_rep_no_results(struct wpa_supplicant *wpa_s,
847 struct wpabuf **buf)
848 {
849 return wpas_rrm_report_elem(buf, wpa_s->beacon_rep_data.token,
850 MEASUREMENT_REPORT_MODE_ACCEPT,
851 MEASURE_TYPE_BEACON, NULL, 0);
852 }
853
854
855 static void wpas_beacon_rep_table(struct wpa_supplicant *wpa_s,
856 struct wpabuf **buf)
857 {
858 size_t i;
859
860 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
861 if (wpas_add_beacon_rep(wpa_s, buf, wpa_s->last_scan_res[i],
862 0, 0) < 0)
863 break;
864 }
865
866 if (!(*buf))
867 wpas_beacon_rep_no_results(wpa_s, buf);
868
869 wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", *buf);
870 }
871
872
873 static void wpas_rrm_refuse_request(struct wpa_supplicant *wpa_s)
874 {
875 if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr)) {
876 struct wpabuf *buf = NULL;
877
878 if (wpas_rrm_report_elem(&buf, wpa_s->beacon_rep_data.token,
879 MEASUREMENT_REPORT_MODE_REJECT_REFUSED,
880 MEASURE_TYPE_BEACON, NULL, 0)) {
881 wpa_printf(MSG_ERROR, "RRM: Memory allocation failed");
882 wpabuf_free(buf);
883 return;
884 }
885
886 wpas_rrm_send_msr_report(wpa_s, buf);
887 wpabuf_free(buf);
888 }
889
890 wpas_clear_beacon_rep_data(wpa_s);
891 }
892
893
894 static void wpas_rrm_scan_timeout(void *eloop_ctx, void *timeout_ctx)
895 {
896 struct wpa_supplicant *wpa_s = eloop_ctx;
897 struct wpa_driver_scan_params *params =
898 &wpa_s->beacon_rep_data.scan_params;
899 u16 prev_duration = params->duration;
900
901 if (!wpa_s->current_bss)
902 return;
903
904 if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL) &&
905 params->duration) {
906 wpa_printf(MSG_DEBUG,
907 "RRM: Cannot set scan duration due to missing driver support");
908 params->duration = 0;
909 }
910 os_get_reltime(&wpa_s->beacon_rep_scan);
911 if (wpa_s->scanning || wpas_p2p_in_progress(wpa_s) ||
912 wpa_supplicant_trigger_scan(wpa_s, params))
913 wpas_rrm_refuse_request(wpa_s);
914 params->duration = prev_duration;
915 }
916
917
918 static int wpas_rm_handle_beacon_req_subelem(struct wpa_supplicant *wpa_s,
919 struct beacon_rep_data *data,
920 u8 sid, u8 slen, const u8 *subelem)
921 {
922 u8 report_info, i;
923
924 switch (sid) {
925 case WLAN_BEACON_REQUEST_SUBELEM_SSID:
926 if (!slen) {
927 wpa_printf(MSG_DEBUG,
928 "SSID subelement with zero length - wildcard SSID");
929 break;
930 }
931
932 if (slen > SSID_MAX_LEN) {
933 wpa_printf(MSG_DEBUG,
934 "Invalid SSID subelement length: %u", slen);
935 return -1;
936 }
937
938 data->ssid_len = slen;
939 os_memcpy(data->ssid, subelem, data->ssid_len);
940 break;
941 case WLAN_BEACON_REQUEST_SUBELEM_INFO:
942 if (slen != 2) {
943 wpa_printf(MSG_DEBUG,
944 "Invalid reporting information subelement length: %u",
945 slen);
946 return -1;
947 }
948
949 report_info = subelem[0];
950 if (report_info != 0) {
951 wpa_printf(MSG_DEBUG,
952 "reporting information=%u is not supported",
953 report_info);
954 return 0;
955 }
956 break;
957 case WLAN_BEACON_REQUEST_SUBELEM_DETAIL:
958 if (slen != 1) {
959 wpa_printf(MSG_DEBUG,
960 "Invalid reporting detail subelement length: %u",
961 slen);
962 return -1;
963 }
964
965 data->report_detail = subelem[0];
966 if (data->report_detail >
967 BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS) {
968 wpa_printf(MSG_DEBUG, "Invalid reporting detail: %u",
969 subelem[0]);
970 return 0;
971 }
972
973 break;
974 case WLAN_BEACON_REQUEST_SUBELEM_REQUEST:
975 if (data->report_detail !=
976 BEACON_REPORT_DETAIL_REQUESTED_ONLY) {
977 wpa_printf(MSG_DEBUG,
978 "Beacon request: request subelement is present but report detail is %u",
979 data->report_detail);
980 return -1;
981 }
982
983 if (!slen) {
984 wpa_printf(MSG_DEBUG,
985 "Invalid request subelement length: %u",
986 slen);
987 return -1;
988 }
989
990 if (data->eids) {
991 wpa_printf(MSG_DEBUG,
992 "Beacon Request: Request subelement appears more than once");
993 return -1;
994 }
995
996 data->eids = bitfield_alloc(255);
997 if (!data->eids) {
998 wpa_printf(MSG_DEBUG, "Failed to allocate EIDs bitmap");
999 return -1;
1000 }
1001
1002 for (i = 0; i < slen; i++)
1003 bitfield_set(data->eids, subelem[i]);
1004 break;
1005 case WLAN_BEACON_REQUEST_SUBELEM_AP_CHANNEL:
1006 /* Skip - it will be processed when freqs are added */
1007 break;
1008 default:
1009 wpa_printf(MSG_DEBUG,
1010 "Beacon request: Unknown subelement id %u", sid);
1011 break;
1012 }
1013
1014 return 1;
1015 }
1016
1017
1018 /**
1019 * Returns 0 if the next element can be processed, 1 if some operation was
1020 * triggered, and -1 if processing failed (i.e., the element is in invalid
1021 * format or an internal error occurred).
1022 */
1023 static int
1024 wpas_rm_handle_beacon_req(struct wpa_supplicant *wpa_s,
1025 u8 elem_token, int duration_mandatory,
1026 const struct rrm_measurement_beacon_request *req,
1027 size_t len, struct wpabuf **buf)
1028 {
1029 struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
1030 struct wpa_driver_scan_params *params = &data->scan_params;
1031 const u8 *subelems;
1032 size_t elems_len;
1033 u16 rand_interval;
1034 u32 interval_usec;
1035 u32 _rand;
1036 int ret = 0, res;
1037
1038 if (len < sizeof(*req))
1039 return -1;
1040
1041 if (req->mode != BEACON_REPORT_MODE_PASSIVE &&
1042 req->mode != BEACON_REPORT_MODE_ACTIVE &&
1043 req->mode != BEACON_REPORT_MODE_TABLE)
1044 return 0;
1045
1046 subelems = req->variable;
1047 elems_len = len - sizeof(*req);
1048 rand_interval = le_to_host16(req->rand_interval);
1049
1050 os_free(params->freqs);
1051 os_memset(params, 0, sizeof(*params));
1052
1053 data->token = elem_token;
1054
1055 /* default reporting detail is all fixed length fields and all
1056 * elements */
1057 data->report_detail = BEACON_REPORT_DETAIL_ALL_FIELDS_AND_ELEMENTS;
1058 os_memcpy(data->bssid, req->bssid, ETH_ALEN);
1059
1060 while (elems_len >= 2) {
1061 if (subelems[1] > elems_len - 2) {
1062 wpa_printf(MSG_DEBUG,
1063 "Beacon Request: Truncated subelement");
1064 ret = -1;
1065 goto out;
1066 }
1067
1068 res = wpas_rm_handle_beacon_req_subelem(
1069 wpa_s, data, subelems[0], subelems[1], &subelems[2]);
1070 if (res != 1) {
1071 ret = res;
1072 goto out;
1073 }
1074
1075 elems_len -= 2 + subelems[1];
1076 subelems += 2 + subelems[1];
1077 }
1078
1079 if (req->mode == BEACON_REPORT_MODE_TABLE) {
1080 wpas_beacon_rep_table(wpa_s, buf);
1081 goto out;
1082 }
1083
1084 params->freqs = wpas_beacon_request_freqs(
1085 wpa_s, req->oper_class, req->channel,
1086 req->mode == BEACON_REPORT_MODE_ACTIVE,
1087 req->variable, len - sizeof(*req));
1088 if (!params->freqs) {
1089 wpa_printf(MSG_DEBUG, "Beacon request: No valid channels");
1090 goto out;
1091 }
1092
1093 params->duration = le_to_host16(req->duration);
1094 params->duration_mandatory = duration_mandatory;
1095 if (!params->duration) {
1096 wpa_printf(MSG_DEBUG, "Beacon request: Duration is 0");
1097 ret = -1;
1098 goto out;
1099 }
1100
1101 params->only_new_results = 1;
1102
1103 if (req->mode == BEACON_REPORT_MODE_ACTIVE) {
1104 params->ssids[params->num_ssids].ssid = data->ssid;
1105 params->ssids[params->num_ssids++].ssid_len = data->ssid_len;
1106 }
1107
1108 if (os_get_random((u8 *) &_rand, sizeof(_rand)) < 0)
1109 _rand = os_random();
1110 interval_usec = (_rand % (rand_interval + 1)) * 1024;
1111 eloop_register_timeout(0, interval_usec, wpas_rrm_scan_timeout, wpa_s,
1112 NULL);
1113 return 1;
1114 out:
1115 wpas_clear_beacon_rep_data(wpa_s);
1116 return ret;
1117 }
1118
1119
1120 static int
1121 wpas_rrm_handle_msr_req_element(
1122 struct wpa_supplicant *wpa_s,
1123 const struct rrm_measurement_request_element *req,
1124 struct wpabuf **buf)
1125 {
1126 int duration_mandatory;
1127
1128 wpa_printf(MSG_DEBUG, "Measurement request type %d token %d",
1129 req->type, req->token);
1130
1131 if (req->mode & MEASUREMENT_REQUEST_MODE_ENABLE) {
1132 /* Enable bit is not supported for now */
1133 wpa_printf(MSG_DEBUG, "RRM: Enable bit not supported, ignore");
1134 return 0;
1135 }
1136
1137 if ((req->mode & MEASUREMENT_REQUEST_MODE_PARALLEL) &&
1138 req->type > MEASURE_TYPE_RPI_HIST) {
1139 /* Parallel measurements are not supported for now */
1140 wpa_printf(MSG_DEBUG,
1141 "RRM: Parallel measurements are not supported, reject");
1142 goto reject;
1143 }
1144
1145 duration_mandatory =
1146 !!(req->mode & MEASUREMENT_REQUEST_MODE_DURATION_MANDATORY);
1147
1148 switch (req->type) {
1149 case MEASURE_TYPE_LCI:
1150 return wpas_rrm_build_lci_report(wpa_s, req, buf);
1151 case MEASURE_TYPE_BEACON:
1152 if (duration_mandatory &&
1153 !(wpa_s->drv_rrm_flags &
1154 WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL)) {
1155 wpa_printf(MSG_DEBUG,
1156 "RRM: Driver does not support dwell time configuration - reject beacon report with mandatory duration");
1157 goto reject;
1158 }
1159 return wpas_rm_handle_beacon_req(wpa_s, req->token,
1160 duration_mandatory,
1161 (const void *) req->variable,
1162 req->len - 3, buf);
1163 default:
1164 wpa_printf(MSG_INFO,
1165 "RRM: Unsupported radio measurement type %u",
1166 req->type);
1167 break;
1168 }
1169
1170 reject:
1171 if (!is_multicast_ether_addr(wpa_s->rrm.dst_addr) &&
1172 wpas_rrm_report_elem(buf, req->token,
1173 MEASUREMENT_REPORT_MODE_REJECT_INCAPABLE,
1174 req->type, NULL, 0) < 0) {
1175 wpa_printf(MSG_DEBUG, "RRM: Failed to add report element");
1176 return -1;
1177 }
1178
1179 return 0;
1180 }
1181
1182
1183 static struct wpabuf *
1184 wpas_rrm_process_msr_req_elems(struct wpa_supplicant *wpa_s, const u8 *pos,
1185 size_t len)
1186 {
1187 struct wpabuf *buf = NULL;
1188
1189 while (len) {
1190 const struct rrm_measurement_request_element *req;
1191 int res;
1192
1193 if (len < 2) {
1194 wpa_printf(MSG_DEBUG, "RRM: Truncated element");
1195 goto out;
1196 }
1197
1198 req = (const struct rrm_measurement_request_element *) pos;
1199 if (req->eid != WLAN_EID_MEASURE_REQUEST) {
1200 wpa_printf(MSG_DEBUG,
1201 "RRM: Expected Measurement Request element, but EID is %u",
1202 req->eid);
1203 goto out;
1204 }
1205
1206 if (req->len < 3) {
1207 wpa_printf(MSG_DEBUG, "RRM: Element length too short");
1208 goto out;
1209 }
1210
1211 if (req->len > len - 2) {
1212 wpa_printf(MSG_DEBUG, "RRM: Element length too long");
1213 goto out;
1214 }
1215
1216 res = wpas_rrm_handle_msr_req_element(wpa_s, req, &buf);
1217 if (res < 0)
1218 goto out;
1219
1220 pos += req->len + 2;
1221 len -= req->len + 2;
1222 }
1223
1224 return buf;
1225
1226 out:
1227 wpabuf_free(buf);
1228 return NULL;
1229 }
1230
1231
1232 void wpas_rrm_handle_radio_measurement_request(struct wpa_supplicant *wpa_s,
1233 const u8 *src, const u8 *dst,
1234 const u8 *frame, size_t len)
1235 {
1236 struct wpabuf *report;
1237
1238 if (wpa_s->wpa_state != WPA_COMPLETED) {
1239 wpa_printf(MSG_INFO,
1240 "RRM: Ignoring radio measurement request: Not associated");
1241 return;
1242 }
1243
1244 if (!wpa_s->rrm.rrm_used) {
1245 wpa_printf(MSG_INFO,
1246 "RRM: Ignoring radio measurement request: Not RRM network");
1247 return;
1248 }
1249
1250 if (len < 3) {
1251 wpa_printf(MSG_INFO,
1252 "RRM: Ignoring too short radio measurement request");
1253 return;
1254 }
1255
1256 wpa_s->rrm.token = *frame;
1257 os_memcpy(wpa_s->rrm.dst_addr, dst, ETH_ALEN);
1258
1259 /* Number of repetitions is not supported */
1260
1261 report = wpas_rrm_process_msr_req_elems(wpa_s, frame + 3, len - 3);
1262 if (!report)
1263 return;
1264
1265 wpas_rrm_send_msr_report(wpa_s, report);
1266 wpabuf_free(report);
1267 }
1268
1269
1270 void wpas_rrm_handle_link_measurement_request(struct wpa_supplicant *wpa_s,
1271 const u8 *src,
1272 const u8 *frame, size_t len,
1273 int rssi)
1274 {
1275 struct wpabuf *buf;
1276 const struct rrm_link_measurement_request *req;
1277 struct rrm_link_measurement_report report;
1278
1279 if (wpa_s->wpa_state != WPA_COMPLETED) {
1280 wpa_printf(MSG_INFO,
1281 "RRM: Ignoring link measurement request. Not associated");
1282 return;
1283 }
1284
1285 if (!wpa_s->rrm.rrm_used) {
1286 wpa_printf(MSG_INFO,
1287 "RRM: Ignoring link measurement request. Not RRM network");
1288 return;
1289 }
1290
1291 if (!(wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION)) {
1292 wpa_printf(MSG_INFO,
1293 "RRM: Measurement report failed. TX power insertion not supported");
1294 return;
1295 }
1296
1297 req = (const struct rrm_link_measurement_request *) frame;
1298 if (len < sizeof(*req)) {
1299 wpa_printf(MSG_INFO,
1300 "RRM: Link measurement report failed. Request too short");
1301 return;
1302 }
1303
1304 os_memset(&report, 0, sizeof(report));
1305 report.dialog_token = req->dialog_token;
1306 report.tpc.eid = WLAN_EID_TPC_REPORT;
1307 report.tpc.len = 2;
1308 /* Note: The driver is expected to update report.tpc.tx_power and
1309 * report.tpc.link_margin subfields when sending out this frame.
1310 * Similarly, the driver would need to update report.rx_ant_id and
1311 * report.tx_ant_id subfields. */
1312 report.rsni = 255; /* 255 indicates that RSNI is not available */
1313 report.rcpi = rssi_to_rcpi(rssi);
1314
1315 /* action_category + action_code */
1316 buf = wpabuf_alloc(2 + sizeof(report));
1317 if (buf == NULL) {
1318 wpa_printf(MSG_ERROR,
1319 "RRM: Link measurement report failed. Buffer allocation failed");
1320 return;
1321 }
1322
1323 wpabuf_put_u8(buf, WLAN_ACTION_RADIO_MEASUREMENT);
1324 wpabuf_put_u8(buf, WLAN_RRM_LINK_MEASUREMENT_REPORT);
1325 wpabuf_put_data(buf, &report, sizeof(report));
1326 wpa_hexdump_buf(MSG_DEBUG, "RRM: Link measurement report", buf);
1327
1328 if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, src,
1329 wpa_s->own_addr, wpa_s->bssid,
1330 wpabuf_head(buf), wpabuf_len(buf), 0)) {
1331 wpa_printf(MSG_ERROR,
1332 "RRM: Link measurement report failed. Send action failed");
1333 }
1334 wpabuf_free(buf);
1335 }
1336
1337
1338 int wpas_beacon_rep_scan_process(struct wpa_supplicant *wpa_s,
1339 struct wpa_scan_results *scan_res,
1340 struct scan_info *info)
1341 {
1342 size_t i = 0;
1343 struct wpabuf *buf = NULL;
1344
1345 if (!wpa_s->beacon_rep_data.token)
1346 return 0;
1347
1348 if (!wpa_s->current_bss)
1349 goto out;
1350
1351 /* If the measurement was aborted, don't report partial results */
1352 if (info->aborted)
1353 goto out;
1354
1355 wpa_printf(MSG_DEBUG, "RRM: TSF BSSID: " MACSTR " current BSS: " MACSTR,
1356 MAC2STR(info->scan_start_tsf_bssid),
1357 MAC2STR(wpa_s->current_bss->bssid));
1358 if ((wpa_s->drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
1359 os_memcmp(info->scan_start_tsf_bssid, wpa_s->current_bss->bssid,
1360 ETH_ALEN) != 0) {
1361 wpa_printf(MSG_DEBUG,
1362 "RRM: Ignore scan results due to mismatching TSF BSSID");
1363 goto out;
1364 }
1365
1366 for (i = 0; i < scan_res->num; i++) {
1367 struct wpa_bss *bss =
1368 wpa_bss_get_bssid(wpa_s, scan_res->res[i]->bssid);
1369
1370 if (!bss)
1371 continue;
1372
1373 if ((wpa_s->drv_rrm_flags &
1374 WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT) &&
1375 os_memcmp(scan_res->res[i]->tsf_bssid,
1376 wpa_s->current_bss->bssid, ETH_ALEN) != 0) {
1377 wpa_printf(MSG_DEBUG,
1378 "RRM: Ignore scan result for " MACSTR
1379 " due to mismatching TSF BSSID" MACSTR,
1380 MAC2STR(scan_res->res[i]->bssid),
1381 MAC2STR(scan_res->res[i]->tsf_bssid));
1382 continue;
1383 }
1384
1385 /*
1386 * Don't report results that were not received during the
1387 * current measurement.
1388 */
1389 if (!(wpa_s->drv_rrm_flags &
1390 WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT)) {
1391 struct os_reltime update_time, diff;
1392
1393 /* For now, allow 8 ms older results due to some
1394 * unknown issue with cfg80211 BSS table updates during
1395 * a scan with the current BSS.
1396 * TODO: Fix this more properly to avoid having to have
1397 * this type of hacks in place. */
1398 calculate_update_time(&scan_res->fetch_time,
1399 scan_res->res[i]->age,
1400 &update_time);
1401 os_reltime_sub(&wpa_s->beacon_rep_scan,
1402 &update_time, &diff);
1403 if (os_reltime_before(&update_time,
1404 &wpa_s->beacon_rep_scan) &&
1405 (diff.sec || diff.usec >= 8000)) {
1406 wpa_printf(MSG_DEBUG,
1407 "RRM: Ignore scan result for " MACSTR
1408 " due to old update (age(ms) %u, calculated age %u.%06u seconds)",
1409 MAC2STR(scan_res->res[i]->bssid),
1410 scan_res->res[i]->age,
1411 (unsigned int) diff.sec,
1412 (unsigned int) diff.usec);
1413 continue;
1414 }
1415 } else if (info->scan_start_tsf >
1416 scan_res->res[i]->parent_tsf) {
1417 continue;
1418 }
1419
1420 if (wpas_add_beacon_rep(wpa_s, &buf, bss, info->scan_start_tsf,
1421 scan_res->res[i]->parent_tsf) < 0)
1422 break;
1423 }
1424
1425 if (!buf && wpas_beacon_rep_no_results(wpa_s, &buf))
1426 goto out;
1427
1428 wpa_hexdump_buf(MSG_DEBUG, "RRM: Radio Measurement report", buf);
1429
1430 wpas_rrm_send_msr_report(wpa_s, buf);
1431 wpabuf_free(buf);
1432
1433 out:
1434 wpas_clear_beacon_rep_data(wpa_s);
1435 return 1;
1436 }
1437
1438
1439 void wpas_clear_beacon_rep_data(struct wpa_supplicant *wpa_s)
1440 {
1441 struct beacon_rep_data *data = &wpa_s->beacon_rep_data;
1442
1443 eloop_cancel_timeout(wpas_rrm_scan_timeout, wpa_s, NULL);
1444 bitfield_free(data->eids);
1445 os_free(data->scan_params.freqs);
1446 os_memset(data, 0, sizeof(*data));
1447 }