1 /*
2 * wpa_supplicant - WNM
3 * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "common/ieee802_11_common.h"
14 #include "common/wpa_ctrl.h"
15 #include "common/ocv.h"
16 #include "rsn_supp/wpa.h"
17 #include "config.h"
18 #include "wpa_supplicant_i.h"
19 #include "driver_i.h"
20 #include "scan.h"
21 #include "ctrl_iface.h"
22 #include "bss.h"
23 #include "wnm_sta.h"
24 #include "notify.h"
25 #include "hs20_supplicant.h"
26
27 #define MAX_TFS_IE_LEN 1024
28 #define WNM_MAX_NEIGHBOR_REPORT 10
29
30 #define WNM_SCAN_RESULT_AGE 2 /* 2 seconds */
31
32 /* get the TFS IE from driver */
ieee80211_11_get_tfs_ie(struct wpa_supplicant * wpa_s,u8 * buf,u16 * buf_len,enum wnm_oper oper)33 static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
34 u16 *buf_len, enum wnm_oper oper)
35 {
36 wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
37
38 return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
39 }
40
41
42 /* set the TFS IE to driver */
ieee80211_11_set_tfs_ie(struct wpa_supplicant * wpa_s,const u8 * addr,const u8 * buf,u16 buf_len,enum wnm_oper oper)43 static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
44 const u8 *addr, const u8 *buf, u16 buf_len,
45 enum wnm_oper oper)
46 {
47 u16 len = buf_len;
48
49 wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
50
51 return wpa_drv_wnm_oper(wpa_s, oper, addr, (u8 *) buf, &len);
52 }
53
54
55 /* MLME-SLEEPMODE.request */
ieee802_11_send_wnmsleep_req(struct wpa_supplicant * wpa_s,u8 action,u16 intval,struct wpabuf * tfs_req)56 int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
57 u8 action, u16 intval, struct wpabuf *tfs_req)
58 {
59 struct ieee80211_mgmt *mgmt;
60 int res;
61 size_t len;
62 struct wnm_sleep_element *wnmsleep_ie;
63 u8 *wnmtfs_ie, *oci_ie;
64 u8 wnmsleep_ie_len, oci_ie_len;
65 u16 wnmtfs_ie_len; /* possibly multiple IE(s) */
66 enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
67 WNM_SLEEP_TFS_REQ_IE_NONE;
68
69 wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
70 "action=%s to " MACSTR,
71 action == 0 ? "enter" : "exit",
72 MAC2STR(wpa_s->bssid));
73
74 /* WNM-Sleep Mode IE */
75 wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
76 wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
77 if (wnmsleep_ie == NULL)
78 return -1;
79 wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
80 wnmsleep_ie->len = wnmsleep_ie_len - 2;
81 wnmsleep_ie->action_type = action;
82 wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
83 wnmsleep_ie->intval = host_to_le16(intval);
84 wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
85 (u8 *) wnmsleep_ie, wnmsleep_ie_len);
86
87 /* TFS IE(s) */
88 if (tfs_req) {
89 wnmtfs_ie_len = wpabuf_len(tfs_req);
90 wnmtfs_ie = os_memdup(wpabuf_head(tfs_req), wnmtfs_ie_len);
91 if (wnmtfs_ie == NULL) {
92 os_free(wnmsleep_ie);
93 return -1;
94 }
95 } else {
96 wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
97 if (wnmtfs_ie == NULL) {
98 os_free(wnmsleep_ie);
99 return -1;
100 }
101 if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
102 tfs_oper)) {
103 wnmtfs_ie_len = 0;
104 os_free(wnmtfs_ie);
105 wnmtfs_ie = NULL;
106 }
107 }
108 wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
109 (u8 *) wnmtfs_ie, wnmtfs_ie_len);
110
111 oci_ie = NULL;
112 oci_ie_len = 0;
113 #ifdef CONFIG_OCV
114 if (action == WNM_SLEEP_MODE_EXIT && wpa_sm_ocv_enabled(wpa_s->wpa)) {
115 struct wpa_channel_info ci;
116
117 if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
118 wpa_printf(MSG_WARNING,
119 "Failed to get channel info for OCI element in WNM-Sleep Mode frame");
120 os_free(wnmsleep_ie);
121 os_free(wnmtfs_ie);
122 return -1;
123 }
124 #ifdef CONFIG_TESTING_OPTIONS
125 if (wpa_s->oci_freq_override_wnm_sleep) {
126 wpa_printf(MSG_INFO,
127 "TEST: Override OCI KDE frequency %d -> %d MHz",
128 ci.frequency,
129 wpa_s->oci_freq_override_wnm_sleep);
130 ci.frequency = wpa_s->oci_freq_override_wnm_sleep;
131 }
132 #endif /* CONFIG_TESTING_OPTIONS */
133
134 oci_ie_len = OCV_OCI_EXTENDED_LEN;
135 oci_ie = os_zalloc(oci_ie_len);
136 if (!oci_ie) {
137 wpa_printf(MSG_WARNING,
138 "Failed to allocate buffer for for OCI element in WNM-Sleep Mode frame");
139 os_free(wnmsleep_ie);
140 os_free(wnmtfs_ie);
141 return -1;
142 }
143
144 if (ocv_insert_extended_oci(&ci, oci_ie) < 0) {
145 os_free(wnmsleep_ie);
146 os_free(wnmtfs_ie);
147 os_free(oci_ie);
148 return -1;
149 }
150 }
151 #endif /* CONFIG_OCV */
152
153 mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len +
154 oci_ie_len);
155 if (mgmt == NULL) {
156 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
157 "WNM-Sleep Request action frame");
158 os_free(wnmsleep_ie);
159 os_free(wnmtfs_ie);
160 return -1;
161 }
162
163 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
164 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
165 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
166 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
167 WLAN_FC_STYPE_ACTION);
168 mgmt->u.action.category = WLAN_ACTION_WNM;
169 mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
170 mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
171 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
172 wnmsleep_ie_len);
173 /* copy TFS IE here */
174 if (wnmtfs_ie_len > 0) {
175 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
176 wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
177 }
178
179 #ifdef CONFIG_OCV
180 /* copy OCV OCI here */
181 if (oci_ie_len > 0) {
182 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
183 wnmsleep_ie_len + wnmtfs_ie_len, oci_ie, oci_ie_len);
184 }
185 #endif /* CONFIG_OCV */
186
187 len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
188 wnmtfs_ie_len + oci_ie_len;
189
190 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
191 wpa_s->own_addr, wpa_s->bssid,
192 &mgmt->u.action.category, len, 0);
193 if (res < 0)
194 wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
195 "(action=%d, intval=%d)", action, intval);
196 else
197 wpa_s->wnmsleep_used = 1;
198
199 os_free(wnmsleep_ie);
200 os_free(wnmtfs_ie);
201 os_free(oci_ie);
202 os_free(mgmt);
203
204 return res;
205 }
206
207
wnm_sleep_mode_enter_success(struct wpa_supplicant * wpa_s,const u8 * tfsresp_ie_start,const u8 * tfsresp_ie_end)208 static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
209 const u8 *tfsresp_ie_start,
210 const u8 *tfsresp_ie_end)
211 {
212 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
213 wpa_s->bssid, NULL, NULL);
214 /* remove GTK/IGTK ?? */
215
216 /* set the TFS Resp IE(s) */
217 if (tfsresp_ie_start && tfsresp_ie_end &&
218 tfsresp_ie_end - tfsresp_ie_start >= 0) {
219 u16 tfsresp_ie_len;
220 tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
221 tfsresp_ie_start;
222 wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
223 /* pass the TFS Resp IE(s) to driver for processing */
224 if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
225 tfsresp_ie_start,
226 tfsresp_ie_len,
227 WNM_SLEEP_TFS_RESP_IE_SET))
228 wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
229 }
230 }
231
232
wnm_sleep_mode_exit_success(struct wpa_supplicant * wpa_s,const u8 * frm,u16 key_len_total)233 static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
234 const u8 *frm, u16 key_len_total)
235 {
236 u8 *ptr, *end;
237 u8 gtk_len;
238
239 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM, wpa_s->bssid,
240 NULL, NULL);
241
242 /* Install GTK/IGTK */
243
244 /* point to key data field */
245 ptr = (u8 *) frm + 1 + 2;
246 end = ptr + key_len_total;
247 wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
248
249 if (key_len_total && !wpa_sm_pmf_enabled(wpa_s->wpa)) {
250 wpa_msg(wpa_s, MSG_INFO,
251 "WNM: Ignore Key Data in WNM-Sleep Mode Response - PMF not enabled");
252 return;
253 }
254
255 while (end - ptr > 1) {
256 if (2 + ptr[1] > end - ptr) {
257 wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
258 "length");
259 if (end > ptr) {
260 wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
261 ptr, end - ptr);
262 }
263 break;
264 }
265 if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
266 if (ptr[1] < 11 + 5) {
267 wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
268 "subelem");
269 break;
270 }
271 gtk_len = *(ptr + 4);
272 if (ptr[1] < 11 + gtk_len ||
273 gtk_len < 5 || gtk_len > 32) {
274 wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
275 "subelem");
276 break;
277 }
278 wpa_wnmsleep_install_key(
279 wpa_s->wpa,
280 WNM_SLEEP_SUBELEM_GTK,
281 ptr);
282 ptr += 13 + gtk_len;
283 } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
284 if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
285 wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
286 "subelem");
287 break;
288 }
289 wpa_wnmsleep_install_key(wpa_s->wpa,
290 WNM_SLEEP_SUBELEM_IGTK, ptr);
291 ptr += 10 + WPA_IGTK_LEN;
292 } else if (*ptr == WNM_SLEEP_SUBELEM_BIGTK) {
293 if (ptr[1] < 2 + 6 + WPA_BIGTK_LEN) {
294 wpa_printf(MSG_DEBUG,
295 "WNM: Too short BIGTK subelem");
296 break;
297 }
298 wpa_wnmsleep_install_key(wpa_s->wpa,
299 WNM_SLEEP_SUBELEM_BIGTK, ptr);
300 ptr += 10 + WPA_BIGTK_LEN;
301 } else
302 break; /* skip the loop */
303 }
304 }
305
306
ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant * wpa_s,const u8 * frm,int len)307 static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
308 const u8 *frm, int len)
309 {
310 /*
311 * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
312 * WNM-Sleep Mode IE | TFS Response IE
313 */
314 const u8 *pos = frm; /* point to payload after the action field */
315 u16 key_len_total;
316 struct wnm_sleep_element *wnmsleep_ie = NULL;
317 /* multiple TFS Resp IE (assuming consecutive) */
318 const u8 *tfsresp_ie_start = NULL;
319 const u8 *tfsresp_ie_end = NULL;
320 #ifdef CONFIG_OCV
321 const u8 *oci_ie = NULL;
322 u8 oci_ie_len = 0;
323 #endif /* CONFIG_OCV */
324 size_t left;
325
326 if (!wpa_s->wnmsleep_used) {
327 wpa_printf(MSG_DEBUG,
328 "WNM: Ignore WNM-Sleep Mode Response frame since WNM-Sleep Mode operation has not been requested");
329 return;
330 }
331
332 if (len < 3)
333 return;
334 key_len_total = WPA_GET_LE16(frm + 1);
335
336 wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
337 frm[0], key_len_total);
338 left = len - 3;
339 if (key_len_total > left) {
340 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
341 return;
342 }
343 pos += 3 + key_len_total;
344 while (pos - frm + 1 < len) {
345 u8 ie_len = *(pos + 1);
346 if (2 + ie_len > frm + len - pos) {
347 wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
348 break;
349 }
350 wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
351 if (*pos == WLAN_EID_WNMSLEEP && ie_len >= 4)
352 wnmsleep_ie = (struct wnm_sleep_element *) pos;
353 else if (*pos == WLAN_EID_TFS_RESP) {
354 if (!tfsresp_ie_start)
355 tfsresp_ie_start = pos;
356 tfsresp_ie_end = pos;
357 #ifdef CONFIG_OCV
358 } else if (*pos == WLAN_EID_EXTENSION && ie_len >= 1 &&
359 pos[2] == WLAN_EID_EXT_OCV_OCI) {
360 oci_ie = pos + 3;
361 oci_ie_len = ie_len - 1;
362 #endif /* CONFIG_OCV */
363 } else
364 wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
365 pos += ie_len + 2;
366 }
367
368 if (!wnmsleep_ie) {
369 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
370 return;
371 }
372
373 #ifdef CONFIG_OCV
374 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT &&
375 wpa_sm_ocv_enabled(wpa_s->wpa)) {
376 struct wpa_channel_info ci;
377
378 if (wpa_drv_channel_info(wpa_s, &ci) != 0) {
379 wpa_msg(wpa_s, MSG_WARNING,
380 "Failed to get channel info to validate received OCI in WNM-Sleep Mode frame");
381 return;
382 }
383
384 if (ocv_verify_tx_params(oci_ie, oci_ie_len, &ci,
385 channel_width_to_int(ci.chanwidth),
386 ci.seg1_idx) != OCI_SUCCESS) {
387 wpa_msg(wpa_s, MSG_WARNING, "WNM: OCV failed: %s",
388 ocv_errorstr);
389 return;
390 }
391 }
392 #endif /* CONFIG_OCV */
393
394 wpa_s->wnmsleep_used = 0;
395
396 if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
397 wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
398 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
399 "frame (action=%d, intval=%d)",
400 wnmsleep_ie->action_type, wnmsleep_ie->intval);
401 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
402 wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
403 tfsresp_ie_end);
404 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
405 wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
406 }
407 } else {
408 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
409 "(action=%d, intval=%d)",
410 wnmsleep_ie->action_type, wnmsleep_ie->intval);
411 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
412 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
413 wpa_s->bssid, NULL, NULL);
414 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
415 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
416 wpa_s->bssid, NULL, NULL);
417 }
418 }
419
420
wnm_deallocate_memory(struct wpa_supplicant * wpa_s)421 void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
422 {
423 int i;
424
425 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
426 os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
427 os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
428 }
429
430 wpa_s->wnm_num_neighbor_report = 0;
431 os_free(wpa_s->wnm_neighbor_report_elements);
432 wpa_s->wnm_neighbor_report_elements = NULL;
433
434 wpabuf_free(wpa_s->coloc_intf_elems);
435 wpa_s->coloc_intf_elems = NULL;
436 }
437
438
wnm_parse_neighbor_report_elem(struct neighbor_report * rep,u8 id,u8 elen,const u8 * pos)439 static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
440 u8 id, u8 elen, const u8 *pos)
441 {
442 switch (id) {
443 case WNM_NEIGHBOR_TSF:
444 if (elen < 2 + 2) {
445 wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
446 break;
447 }
448 rep->tsf_offset = WPA_GET_LE16(pos);
449 rep->beacon_int = WPA_GET_LE16(pos + 2);
450 rep->tsf_present = 1;
451 break;
452 case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
453 if (elen < 2) {
454 wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
455 "country string");
456 break;
457 }
458 os_memcpy(rep->country, pos, 2);
459 rep->country_present = 1;
460 break;
461 case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
462 if (elen < 1) {
463 wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
464 "candidate");
465 break;
466 }
467 rep->preference = pos[0];
468 rep->preference_present = 1;
469 break;
470 case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
471 if (elen < 10) {
472 wpa_printf(MSG_DEBUG,
473 "WNM: Too short BSS termination duration");
474 break;
475 }
476 rep->bss_term_tsf = WPA_GET_LE64(pos);
477 rep->bss_term_dur = WPA_GET_LE16(pos + 8);
478 rep->bss_term_present = 1;
479 break;
480 case WNM_NEIGHBOR_BEARING:
481 if (elen < 8) {
482 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
483 "bearing");
484 break;
485 }
486 rep->bearing = WPA_GET_LE16(pos);
487 rep->distance = WPA_GET_LE32(pos + 2);
488 rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
489 rep->bearing_present = 1;
490 break;
491 case WNM_NEIGHBOR_MEASUREMENT_PILOT:
492 if (elen < 1) {
493 wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
494 "pilot");
495 break;
496 }
497 os_free(rep->meas_pilot);
498 rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
499 if (rep->meas_pilot == NULL)
500 break;
501 rep->meas_pilot->measurement_pilot = pos[0];
502 rep->meas_pilot->subelem_len = elen - 1;
503 os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
504 break;
505 case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
506 if (elen < 5) {
507 wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
508 "capabilities");
509 break;
510 }
511 os_memcpy(rep->rm_capab, pos, 5);
512 rep->rm_capab_present = 1;
513 break;
514 case WNM_NEIGHBOR_MULTIPLE_BSSID:
515 if (elen < 1) {
516 wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
517 break;
518 }
519 os_free(rep->mul_bssid);
520 rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
521 if (rep->mul_bssid == NULL)
522 break;
523 rep->mul_bssid->max_bssid_indicator = pos[0];
524 rep->mul_bssid->subelem_len = elen - 1;
525 os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
526 break;
527 }
528 }
529
530
wnm_nei_get_chan(struct wpa_supplicant * wpa_s,u8 op_class,u8 chan)531 static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
532 {
533 struct wpa_bss *bss = wpa_s->current_bss;
534 const char *country = NULL;
535 int freq;
536
537 if (bss) {
538 const u8 *elem = wpa_bss_get_ie(bss, WLAN_EID_COUNTRY);
539
540 if (elem && elem[1] >= 2)
541 country = (const char *) (elem + 2);
542 }
543
544 freq = ieee80211_chan_to_freq(country, op_class, chan);
545 if (freq <= 0 && op_class == 0) {
546 /*
547 * Some APs do not advertise correct operating class
548 * information. Try to determine the most likely operating
549 * frequency based on the channel number.
550 */
551 if (chan >= 1 && chan <= 13)
552 freq = 2407 + chan * 5;
553 else if (chan == 14)
554 freq = 2484;
555 else if (chan >= 36 && chan <= 177)
556 freq = 5000 + chan * 5;
557 }
558 return freq;
559 }
560
561
wnm_parse_neighbor_report(struct wpa_supplicant * wpa_s,const u8 * pos,u8 len,struct neighbor_report * rep)562 static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
563 const u8 *pos, u8 len,
564 struct neighbor_report *rep)
565 {
566 u8 left = len;
567
568 if (left < 13) {
569 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
570 return;
571 }
572
573 os_memcpy(rep->bssid, pos, ETH_ALEN);
574 rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
575 rep->regulatory_class = *(pos + 10);
576 rep->channel_number = *(pos + 11);
577 rep->phy_type = *(pos + 12);
578
579 pos += 13;
580 left -= 13;
581
582 while (left >= 2) {
583 u8 id, elen;
584
585 id = *pos++;
586 elen = *pos++;
587 wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
588 left -= 2;
589 if (elen > left) {
590 wpa_printf(MSG_DEBUG,
591 "WNM: Truncated neighbor report subelement");
592 break;
593 }
594 wnm_parse_neighbor_report_elem(rep, id, elen, pos);
595 left -= elen;
596 pos += elen;
597 }
598
599 rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
600 rep->channel_number);
601 }
602
603
wnm_clear_acceptable(struct wpa_supplicant * wpa_s)604 static void wnm_clear_acceptable(struct wpa_supplicant *wpa_s)
605 {
606 unsigned int i;
607
608 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++)
609 wpa_s->wnm_neighbor_report_elements[i].acceptable = 0;
610 }
611
612
get_first_acceptable(struct wpa_supplicant * wpa_s)613 static struct wpa_bss * get_first_acceptable(struct wpa_supplicant *wpa_s)
614 {
615 unsigned int i;
616 struct neighbor_report *nei;
617
618 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
619 nei = &wpa_s->wnm_neighbor_report_elements[i];
620 if (nei->acceptable)
621 return wpa_bss_get_bssid(wpa_s, nei->bssid);
622 }
623
624 return NULL;
625 }
626
627
628 #ifdef CONFIG_MBO
629 static struct wpa_bss *
get_mbo_transition_candidate(struct wpa_supplicant * wpa_s,enum mbo_transition_reject_reason * reason)630 get_mbo_transition_candidate(struct wpa_supplicant *wpa_s,
631 enum mbo_transition_reject_reason *reason)
632 {
633 struct wpa_bss *target = NULL;
634 struct wpa_bss_trans_info params;
635 struct wpa_bss_candidate_info *info = NULL;
636 struct neighbor_report *nei = wpa_s->wnm_neighbor_report_elements;
637 u8 *first_candidate_bssid = NULL, *pos;
638 unsigned int i;
639
640 params.mbo_transition_reason = wpa_s->wnm_mbo_transition_reason;
641 params.n_candidates = 0;
642 params.bssid = os_calloc(wpa_s->wnm_num_neighbor_report, ETH_ALEN);
643 if (!params.bssid)
644 return NULL;
645
646 pos = params.bssid;
647 for (i = 0; i < wpa_s->wnm_num_neighbor_report; nei++, i++) {
648 if (nei->is_first)
649 first_candidate_bssid = nei->bssid;
650 if (!nei->acceptable)
651 continue;
652 os_memcpy(pos, nei->bssid, ETH_ALEN);
653 pos += ETH_ALEN;
654 params.n_candidates++;
655 }
656
657 if (!params.n_candidates)
658 goto end;
659
660 info = wpa_drv_get_bss_trans_status(wpa_s, ¶ms);
661 if (!info) {
662 /* If failed to get candidate BSS transition status from driver,
663 * get the first acceptable candidate from wpa_supplicant.
664 */
665 target = wpa_bss_get_bssid(wpa_s, params.bssid);
666 goto end;
667 }
668
669 /* Get the first acceptable candidate from driver */
670 for (i = 0; i < info->num; i++) {
671 if (info->candidates[i].is_accept) {
672 target = wpa_bss_get_bssid(wpa_s,
673 info->candidates[i].bssid);
674 goto end;
675 }
676 }
677
678 /* If Disassociation Imminent is set and driver rejects all the
679 * candidate select first acceptable candidate which has
680 * rssi > disassoc_imminent_rssi_threshold
681 */
682 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
683 for (i = 0; i < info->num; i++) {
684 target = wpa_bss_get_bssid(wpa_s,
685 info->candidates[i].bssid);
686 if (target &&
687 (target->level <
688 wpa_s->conf->disassoc_imminent_rssi_threshold))
689 continue;
690 goto end;
691 }
692 }
693
694 /* While sending BTM reject use reason code of the first candidate
695 * received in BTM request frame
696 */
697 if (reason) {
698 for (i = 0; i < info->num; i++) {
699 if (first_candidate_bssid &&
700 os_memcmp(first_candidate_bssid,
701 info->candidates[i].bssid, ETH_ALEN) == 0)
702 {
703 *reason = info->candidates[i].reject_reason;
704 break;
705 }
706 }
707 }
708
709 target = NULL;
710
711 end:
712 os_free(params.bssid);
713 if (info) {
714 os_free(info->candidates);
715 os_free(info);
716 }
717 return target;
718 }
719 #endif /* CONFIG_MBO */
720
721
722 static struct wpa_bss *
compare_scan_neighbor_results(struct wpa_supplicant * wpa_s,os_time_t age_secs,enum mbo_transition_reject_reason * reason)723 compare_scan_neighbor_results(struct wpa_supplicant *wpa_s, os_time_t age_secs,
724 enum mbo_transition_reject_reason *reason)
725 {
726 u8 i;
727 struct wpa_bss *bss = wpa_s->current_bss;
728 struct wpa_bss *target;
729
730 if (!bss)
731 return NULL;
732
733 wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
734 MAC2STR(wpa_s->bssid), bss->level);
735
736 wnm_clear_acceptable(wpa_s);
737
738 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
739 struct neighbor_report *nei;
740
741 nei = &wpa_s->wnm_neighbor_report_elements[i];
742 if (nei->preference_present && nei->preference == 0) {
743 wpa_printf(MSG_DEBUG, "Skip excluded BSS " MACSTR,
744 MAC2STR(nei->bssid));
745 continue;
746 }
747
748 target = wpa_bss_get_bssid(wpa_s, nei->bssid);
749 if (!target) {
750 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
751 " (pref %d) not found in scan results",
752 MAC2STR(nei->bssid),
753 nei->preference_present ? nei->preference :
754 -1);
755 continue;
756 }
757
758 if (age_secs) {
759 struct os_reltime now;
760
761 if (os_get_reltime(&now) == 0 &&
762 os_reltime_expired(&now, &target->last_update,
763 age_secs)) {
764 wpa_printf(MSG_DEBUG,
765 "Candidate BSS is more than %ld seconds old",
766 age_secs);
767 continue;
768 }
769 }
770
771 if (bss->ssid_len != target->ssid_len ||
772 os_memcmp(bss->ssid, target->ssid, bss->ssid_len) != 0) {
773 /*
774 * TODO: Could consider allowing transition to another
775 * ESS if PMF was enabled for the association.
776 */
777 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
778 " (pref %d) in different ESS",
779 MAC2STR(nei->bssid),
780 nei->preference_present ? nei->preference :
781 -1);
782 continue;
783 }
784
785 if (wpa_s->current_ssid &&
786 #ifndef LOS_WPA_PATCH
787 !wpa_scan_res_match(wpa_s, 0, target, wpa_s->current_ssid, 1, 0)) {
788 #else
789 !wpa_scan_res_match(wpa_s, 0, target, wpa_s->current_ssid, 1)) {
790 #endif
791 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
792 " (pref %d) does not match the current network profile",
793 MAC2STR(nei->bssid),
794 nei->preference_present ? nei->preference :
795 -1);
796 continue;
797 }
798
799 #ifndef EXT_CODE_CROP
800 if (wpa_is_bss_tmp_disallowed(wpa_s, target)) {
801 wpa_printf(MSG_DEBUG,
802 "MBO: Candidate BSS " MACSTR
803 " retry delay is not over yet",
804 MAC2STR(nei->bssid));
805 continue;
806 }
807 #endif
808
809 if (target->level < bss->level && target->level < -80) {
810 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
811 " (pref %d) does not have sufficient signal level (%d)",
812 MAC2STR(nei->bssid),
813 nei->preference_present ? nei->preference :
814 -1,
815 target->level);
816 continue;
817 }
818
819 nei->acceptable = 1;
820 }
821
822 #ifdef CONFIG_MBO
823 if (wpa_s->wnm_mbo_trans_reason_present)
824 target = get_mbo_transition_candidate(wpa_s, reason);
825 else
826 target = get_first_acceptable(wpa_s);
827 #else /* CONFIG_MBO */
828 target = get_first_acceptable(wpa_s);
829 #endif /* CONFIG_MBO */
830
831 if (target) {
832 wpa_printf(MSG_DEBUG,
833 "WNM: Found an acceptable preferred transition candidate BSS "
834 MACSTR " (RSSI %d)",
835 MAC2STR(target->bssid), target->level);
836 }
837
838 return target;
839 }
840
841
842 static int wpa_bss_ies_eq(struct wpa_bss *a, struct wpa_bss *b, u8 eid)
843 {
844 const u8 *ie_a, *ie_b;
845
846 if (!a || !b)
847 return 0;
848
849 ie_a = wpa_bss_get_ie(a, eid);
850 ie_b = wpa_bss_get_ie(b, eid);
851
852 if (!ie_a || !ie_b || ie_a[1] != ie_b[1])
853 return 0;
854
855 return os_memcmp(ie_a, ie_b, ie_a[1]) == 0;
856 }
857
858
859 static u32 wnm_get_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
860 {
861 u32 info = 0;
862
863 info |= NEI_REP_BSSID_INFO_AP_UNKNOWN_REACH;
864
865 /*
866 * Leave the security and key scope bits unset to indicate that the
867 * security information is not available.
868 */
869
870 if (bss->caps & WLAN_CAPABILITY_SPECTRUM_MGMT)
871 info |= NEI_REP_BSSID_INFO_SPECTRUM_MGMT;
872 if (bss->caps & WLAN_CAPABILITY_QOS)
873 info |= NEI_REP_BSSID_INFO_QOS;
874 if (bss->caps & WLAN_CAPABILITY_APSD)
875 info |= NEI_REP_BSSID_INFO_APSD;
876 if (bss->caps & WLAN_CAPABILITY_RADIO_MEASUREMENT)
877 info |= NEI_REP_BSSID_INFO_RM;
878 if (bss->caps & WLAN_CAPABILITY_DELAYED_BLOCK_ACK)
879 info |= NEI_REP_BSSID_INFO_DELAYED_BA;
880 if (bss->caps & WLAN_CAPABILITY_IMM_BLOCK_ACK)
881 info |= NEI_REP_BSSID_INFO_IMM_BA;
882 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_MOBILITY_DOMAIN))
883 info |= NEI_REP_BSSID_INFO_MOBILITY_DOMAIN;
884 if (wpa_bss_ies_eq(bss, wpa_s->current_bss, WLAN_EID_HT_CAP))
885 info |= NEI_REP_BSSID_INFO_HT;
886
887 return info;
888 }
889
890
891 static int wnm_add_nei_rep(struct wpabuf **buf, const u8 *bssid,
892 u32 bss_info, u8 op_class, u8 chan, u8 phy_type,
893 u8 pref)
894 {
895 if (wpabuf_len(*buf) + 18 >
896 IEEE80211_MAX_MMPDU_SIZE - IEEE80211_HDRLEN) {
897 wpa_printf(MSG_DEBUG,
898 "WNM: No room in frame for Neighbor Report element");
899 return -1;
900 }
901
902 if (wpabuf_resize(buf, 18) < 0) {
903 wpa_printf(MSG_DEBUG,
904 "WNM: Failed to allocate memory for Neighbor Report element");
905 return -1;
906 }
907
908 wpabuf_put_u8(*buf, WLAN_EID_NEIGHBOR_REPORT);
909 /* length: 13 for basic neighbor report + 3 for preference subelement */
910 wpabuf_put_u8(*buf, 16);
911 wpabuf_put_data(*buf, bssid, ETH_ALEN);
912 wpabuf_put_le32(*buf, bss_info);
913 wpabuf_put_u8(*buf, op_class);
914 wpabuf_put_u8(*buf, chan);
915 wpabuf_put_u8(*buf, phy_type);
916 wpabuf_put_u8(*buf, WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE);
917 wpabuf_put_u8(*buf, 1);
918 wpabuf_put_u8(*buf, pref);
919 return 0;
920 }
921
922
923 static int wnm_nei_rep_add_bss(struct wpa_supplicant *wpa_s,
924 struct wpa_bss *bss, struct wpabuf **buf,
925 u8 pref)
926 {
927 const u8 *ie;
928 u8 op_class, chan;
929 int sec_chan = 0, vht = 0;
930 enum phy_type phy_type;
931 u32 info;
932 struct ieee80211_ht_operation *ht_oper = NULL;
933 struct ieee80211_vht_operation *vht_oper = NULL;
934
935 ie = wpa_bss_get_ie(bss, WLAN_EID_HT_OPERATION);
936 if (ie && ie[1] >= 2) {
937 ht_oper = (struct ieee80211_ht_operation *) (ie + 2);
938
939 if (ht_oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
940 sec_chan = 1;
941 else if (ht_oper->ht_param &
942 HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
943 sec_chan = -1;
944 }
945
946 ie = wpa_bss_get_ie(bss, WLAN_EID_VHT_OPERATION);
947 if (ie && ie[1] >= 1) {
948 vht_oper = (struct ieee80211_vht_operation *) (ie + 2);
949
950 if (vht_oper->vht_op_info_chwidth == CHANWIDTH_80MHZ ||
951 vht_oper->vht_op_info_chwidth == CHANWIDTH_160MHZ ||
952 vht_oper->vht_op_info_chwidth == CHANWIDTH_80P80MHZ)
953 vht = vht_oper->vht_op_info_chwidth;
954 }
955
956 #ifndef EXT_CODE_CROP
957 if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, vht, &op_class, &chan) == NUM_HOSTAPD_MODES) {
958 #else
959 if (ieee80211_freq_to_channel_ext(bss->freq, sec_chan, &op_class, &chan) == NUM_HOSTAPD_MODES) {
960 #endif
961 wpa_printf(MSG_DEBUG,
962 "WNM: Cannot determine operating class and channel");
963 return -2;
964 }
965
966 #ifndef EXT_CODE_CROP
967 phy_type = ieee80211_get_phy_type(bss->freq, (ht_oper != NULL),
968 (vht_oper != NULL));
969 #else
970 phy_type = ieee80211_get_phy_type(bss->freq, (ht_oper != NULL));
971 #endif
972 if (phy_type == PHY_TYPE_UNSPECIFIED) {
973 wpa_printf(MSG_DEBUG,
974 "WNM: Cannot determine BSS phy type for Neighbor Report");
975 return -2;
976 }
977
978 info = wnm_get_bss_info(wpa_s, bss);
979
980 return wnm_add_nei_rep(buf, bss->bssid, info, op_class, chan, phy_type,
981 pref);
982 }
983
984
985 static void wnm_add_cand_list(struct wpa_supplicant *wpa_s, struct wpabuf **buf)
986 {
987 unsigned int i, pref = 255;
988 struct os_reltime now;
989 struct wpa_ssid *ssid = wpa_s->current_ssid;
990
991 if (!ssid)
992 return;
993
994 /*
995 * TODO: Define when scan results are no longer valid for the candidate
996 * list.
997 */
998 os_get_reltime(&now);
999 if (os_reltime_expired(&now, &wpa_s->last_scan, 10))
1000 return;
1001
1002 wpa_printf(MSG_DEBUG,
1003 "WNM: Add candidate list to BSS Transition Management Response frame");
1004 for (i = 0; i < wpa_s->last_scan_res_used && pref; i++) {
1005 struct wpa_bss *bss = wpa_s->last_scan_res[i];
1006 int res;
1007
1008 #ifndef LOS_WPA_PATCH
1009 if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1, 0)) {
1010 #else
1011 if (wpa_scan_res_match(wpa_s, i, bss, ssid, 1)) {
1012 #endif
1013 res = wnm_nei_rep_add_bss(wpa_s, bss, buf, pref--);
1014 if (res == -2)
1015 continue; /* could not build entry for BSS */
1016 if (res < 0)
1017 break; /* no more room for candidates */
1018 if (pref == 1)
1019 break;
1020 }
1021 }
1022
1023 wpa_hexdump_buf(MSG_DEBUG,
1024 "WNM: BSS Transition Management Response candidate list",
1025 *buf);
1026 }
1027
1028
1029 #define BTM_RESP_MIN_SIZE 5 + ETH_ALEN
1030
1031 static void wnm_send_bss_transition_mgmt_resp(
1032 struct wpa_supplicant *wpa_s, u8 dialog_token,
1033 enum bss_trans_mgmt_status_code status,
1034 enum mbo_transition_reject_reason reason,
1035 u8 delay, const u8 *target_bssid)
1036 {
1037 struct wpabuf *buf;
1038 int res;
1039
1040 wpa_printf(MSG_DEBUG,
1041 "WNM: Send BSS Transition Management Response to " MACSTR
1042 " dialog_token=%u status=%u reason=%u delay=%d",
1043 MAC2STR(wpa_s->bssid), dialog_token, status, reason, delay);
1044 if (!wpa_s->current_bss) {
1045 wpa_printf(MSG_DEBUG,
1046 "WNM: Current BSS not known - drop response");
1047 return;
1048 }
1049
1050 buf = wpabuf_alloc(BTM_RESP_MIN_SIZE);
1051 if (!buf) {
1052 wpa_printf(MSG_DEBUG,
1053 "WNM: Failed to allocate memory for BTM response");
1054 return;
1055 }
1056
1057 wpa_s->bss_tm_status = status;
1058 wpas_notify_bss_tm_status(wpa_s);
1059
1060 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1061 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_RESP);
1062 wpabuf_put_u8(buf, dialog_token);
1063 wpabuf_put_u8(buf, status);
1064 wpabuf_put_u8(buf, delay);
1065 if (target_bssid) {
1066 wpabuf_put_data(buf, target_bssid, ETH_ALEN);
1067 } else if (status == WNM_BSS_TM_ACCEPT) {
1068 /*
1069 * P802.11-REVmc clarifies that the Target BSSID field is always
1070 * present when status code is zero, so use a fake value here if
1071 * no BSSID is yet known.
1072 */
1073 wpabuf_put_data(buf, "\0\0\0\0\0\0", ETH_ALEN);
1074 }
1075
1076 if (status == WNM_BSS_TM_ACCEPT)
1077 wnm_add_cand_list(wpa_s, &buf);
1078
1079 #ifdef CONFIG_MBO
1080 if (status != WNM_BSS_TM_ACCEPT &&
1081 wpa_bss_get_vendor_ie(wpa_s->current_bss, MBO_IE_VENDOR_TYPE)) {
1082 u8 mbo[10];
1083 size_t ret;
1084
1085 ret = wpas_mbo_ie_bss_trans_reject(wpa_s, mbo, sizeof(mbo),
1086 reason);
1087 if (ret) {
1088 if (wpabuf_resize(&buf, ret) < 0) {
1089 wpabuf_free(buf);
1090 wpa_printf(MSG_DEBUG,
1091 "WNM: Failed to allocate memory for MBO IE");
1092 return;
1093 }
1094
1095 wpabuf_put_data(buf, mbo, ret);
1096 }
1097 }
1098 #endif /* CONFIG_MBO */
1099
1100 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1101 wpa_s->own_addr, wpa_s->bssid,
1102 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
1103 if (res < 0) {
1104 wpa_printf(MSG_DEBUG,
1105 "WNM: Failed to send BSS Transition Management Response");
1106 }
1107
1108 wpabuf_free(buf);
1109 }
1110
1111
1112 static void wnm_bss_tm_connect(struct wpa_supplicant *wpa_s,
1113 struct wpa_bss *bss, struct wpa_ssid *ssid,
1114 int after_new_scan)
1115 {
1116 struct wpa_radio_work *already_connecting;
1117
1118 wpa_dbg(wpa_s, MSG_DEBUG,
1119 "WNM: Transition to BSS " MACSTR
1120 " based on BSS Transition Management Request (old BSSID "
1121 MACSTR " after_new_scan=%d)",
1122 MAC2STR(bss->bssid), MAC2STR(wpa_s->bssid), after_new_scan);
1123
1124 /* Send the BSS Management Response - Accept */
1125 if (wpa_s->wnm_reply) {
1126 wpa_s->wnm_reply = 0;
1127 wpa_printf(MSG_DEBUG,
1128 "WNM: Sending successful BSS Transition Management Response");
1129 wnm_send_bss_transition_mgmt_resp(
1130 wpa_s, wpa_s->wnm_dialog_token, WNM_BSS_TM_ACCEPT,
1131 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1132 bss->bssid);
1133 }
1134
1135 if (bss == wpa_s->current_bss) {
1136 wpa_printf(MSG_DEBUG,
1137 "WNM: Already associated with the preferred candidate");
1138 wnm_deallocate_memory(wpa_s);
1139 return;
1140 }
1141
1142 already_connecting = radio_work_pending(wpa_s, "sme-connect");
1143 wpa_s->reassociate = 1;
1144 wpa_printf(MSG_DEBUG, "WNM: Issuing connect");
1145 wpa_supplicant_connect(wpa_s, bss, ssid);
1146
1147 /*
1148 * Indicate that a BSS transition is in progress so scan results that
1149 * come in before the 'sme-connect' radio work gets executed do not
1150 * override the original connection attempt.
1151 */
1152 if (!already_connecting && radio_work_pending(wpa_s, "sme-connect"))
1153 wpa_s->bss_trans_mgmt_in_progress = true;
1154 wnm_deallocate_memory(wpa_s);
1155 }
1156
1157
1158 int wnm_scan_process(struct wpa_supplicant *wpa_s, int reply_on_fail)
1159 {
1160 struct wpa_bss *bss;
1161 struct wpa_ssid *ssid = wpa_s->current_ssid;
1162 enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1163 enum mbo_transition_reject_reason reason =
1164 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED;
1165
1166 if (!wpa_s->wnm_neighbor_report_elements)
1167 return 0;
1168
1169 wpa_dbg(wpa_s, MSG_DEBUG,
1170 "WNM: Process scan results for BSS Transition Management");
1171 if (os_reltime_before(&wpa_s->wnm_cand_valid_until,
1172 &wpa_s->scan_trigger_time)) {
1173 wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
1174 wnm_deallocate_memory(wpa_s);
1175 return 0;
1176 }
1177
1178 if (!wpa_s->current_bss ||
1179 os_memcmp(wpa_s->wnm_cand_from_bss, wpa_s->current_bss->bssid,
1180 ETH_ALEN) != 0) {
1181 wpa_printf(MSG_DEBUG, "WNM: Stored BSS transition candidate list not from the current BSS - ignore it");
1182 return 0;
1183 }
1184
1185 /* Compare the Neighbor Report and scan results */
1186 bss = compare_scan_neighbor_results(wpa_s, 0, &reason);
1187 if (!bss) {
1188 wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
1189 status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
1190 goto send_bss_resp_fail;
1191 }
1192
1193 /* Associate to the network */
1194 wnm_bss_tm_connect(wpa_s, bss, ssid, 1);
1195 os_free_drv_scan_bss(bss);
1196 return 1;
1197
1198 send_bss_resp_fail:
1199 os_free_drv_scan_bss(bss);
1200 if (!reply_on_fail)
1201 return 0;
1202
1203 /* Send reject response for all the failures */
1204
1205 if (wpa_s->wnm_reply) {
1206 wpa_s->wnm_reply = 0;
1207 wnm_send_bss_transition_mgmt_resp(wpa_s,
1208 wpa_s->wnm_dialog_token,
1209 status, reason, 0, NULL);
1210 }
1211 wnm_deallocate_memory(wpa_s);
1212
1213 return 0;
1214 }
1215
1216
1217 static int cand_pref_compar(const void *a, const void *b)
1218 {
1219 const struct neighbor_report *aa = a;
1220 const struct neighbor_report *bb = b;
1221
1222 if (!aa->preference_present && !bb->preference_present)
1223 return 0;
1224 if (!aa->preference_present)
1225 return 1;
1226 if (!bb->preference_present)
1227 return -1;
1228 if (bb->preference > aa->preference)
1229 return 1;
1230 if (bb->preference < aa->preference)
1231 return -1;
1232 return 0;
1233 }
1234
1235
1236 static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
1237 {
1238 if (!wpa_s->wnm_neighbor_report_elements)
1239 return;
1240 qsort(wpa_s->wnm_neighbor_report_elements,
1241 wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
1242 cand_pref_compar);
1243 }
1244
1245
1246 static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
1247 {
1248 unsigned int i;
1249
1250 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
1251 if (!wpa_s->wnm_neighbor_report_elements)
1252 return;
1253 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1254 struct neighbor_report *nei;
1255
1256 nei = &wpa_s->wnm_neighbor_report_elements[i];
1257 wpa_printf(MSG_DEBUG, "%u: " MACSTR
1258 " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
1259 i, MAC2STR(nei->bssid), nei->bssid_info,
1260 nei->regulatory_class,
1261 nei->channel_number, nei->phy_type,
1262 nei->preference_present ? nei->preference : -1,
1263 nei->freq);
1264 }
1265 }
1266
1267
1268 static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
1269 {
1270 unsigned int i;
1271
1272 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1273 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1274 int j;
1275
1276 for (j = 0; j < mode->num_channels; j++) {
1277 struct hostapd_channel_data *chan;
1278
1279 chan = &mode->channels[j];
1280 if (chan->freq == freq &&
1281 !(chan->flag & HOSTAPD_CHAN_DISABLED))
1282 return 1;
1283 }
1284 }
1285
1286 return 0;
1287 }
1288
1289
1290 static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
1291 {
1292 int *freqs;
1293 int num_freqs = 0;
1294 unsigned int i;
1295
1296 if (!wpa_s->wnm_neighbor_report_elements)
1297 return;
1298
1299 if (wpa_s->hw.modes == NULL)
1300 return;
1301
1302 os_free(wpa_s->next_scan_freqs);
1303 wpa_s->next_scan_freqs = NULL;
1304
1305 freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
1306 if (freqs == NULL)
1307 return;
1308
1309 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1310 struct neighbor_report *nei;
1311
1312 nei = &wpa_s->wnm_neighbor_report_elements[i];
1313 if (nei->freq <= 0) {
1314 wpa_printf(MSG_DEBUG,
1315 "WNM: Unknown neighbor operating frequency for "
1316 MACSTR " - scan all channels",
1317 MAC2STR(nei->bssid));
1318 os_free(freqs);
1319 return;
1320 }
1321 if (chan_supported(wpa_s, nei->freq))
1322 add_freq(freqs, &num_freqs, nei->freq);
1323 }
1324
1325 if (num_freqs == 0) {
1326 os_free(freqs);
1327 return;
1328 }
1329
1330 wpa_printf(MSG_DEBUG,
1331 "WNM: Scan %d frequencies based on transition candidate list",
1332 num_freqs);
1333 wpa_s->next_scan_freqs = freqs;
1334 }
1335
1336
1337 static int wnm_fetch_scan_results(struct wpa_supplicant *wpa_s)
1338 {
1339 struct wpa_scan_results *scan_res;
1340 struct wpa_bss *bss;
1341 struct wpa_ssid *ssid = wpa_s->current_ssid;
1342 u8 i, found = 0;
1343 size_t j;
1344
1345 wpa_dbg(wpa_s, MSG_DEBUG,
1346 "WNM: Fetch current scan results from the driver for checking transition candidates");
1347 scan_res = wpa_drv_get_scan_results2(wpa_s);
1348 if (!scan_res) {
1349 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Failed to get scan results");
1350 return 0;
1351 }
1352
1353 if (scan_res->fetch_time.sec == 0)
1354 os_get_reltime(&scan_res->fetch_time);
1355
1356 #ifndef EXT_CODE_CROP
1357 filter_scan_res(wpa_s, scan_res);
1358 #endif
1359
1360 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
1361 struct neighbor_report *nei;
1362
1363 nei = &wpa_s->wnm_neighbor_report_elements[i];
1364 if (nei->preference_present && nei->preference == 0)
1365 continue;
1366
1367 for (j = 0; j < scan_res->num; j++) {
1368 struct wpa_scan_res *res;
1369 const u8 *ssid_ie;
1370
1371 res = scan_res->res[j];
1372 if (os_memcmp(nei->bssid, res->bssid, ETH_ALEN) != 0 ||
1373 res->age > WNM_SCAN_RESULT_AGE * 1000)
1374 continue;
1375 bss = wpa_s->current_bss;
1376 ssid_ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
1377 if (bss && ssid_ie && ssid_ie[1] &&
1378 (bss->ssid_len != ssid_ie[1] ||
1379 os_memcmp(bss->ssid, ssid_ie + 2,
1380 bss->ssid_len) != 0))
1381 continue; /* Skip entries for other ESSs */
1382
1383 /* Potential candidate found */
1384 found = 1;
1385 #ifndef EXT_CODE_CROP
1386 scan_snr(res);
1387 scan_est_throughput(wpa_s, res);
1388 #endif
1389 wpa_bss_update_scan_res(wpa_s, res,
1390 &scan_res->fetch_time);
1391 }
1392 }
1393
1394 wpa_scan_results_free(scan_res);
1395 if (!found) {
1396 wpa_dbg(wpa_s, MSG_DEBUG,
1397 "WNM: No transition candidate matches existing scan results");
1398 return 0;
1399 }
1400
1401 bss = compare_scan_neighbor_results(wpa_s, WNM_SCAN_RESULT_AGE, NULL);
1402 if (!bss) {
1403 wpa_dbg(wpa_s, MSG_DEBUG,
1404 "WNM: Comparison of scan results against transition candidates did not find matches");
1405 return 0;
1406 }
1407
1408 /* Associate to the network */
1409 wnm_bss_tm_connect(wpa_s, bss, ssid, 0);
1410 os_free_drv_scan_bss(bss);
1411 return 1;
1412 }
1413
1414
1415 static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
1416 const u8 *pos, const u8 *end,
1417 int reply)
1418 {
1419 unsigned int beacon_int;
1420 u8 valid_int;
1421 #ifdef CONFIG_MBO
1422 const u8 *vendor;
1423 #endif /* CONFIG_MBO */
1424
1425 if (wpa_s->disable_mbo_oce || wpa_s->conf->disable_btm)
1426 return;
1427
1428 if (end - pos < 5)
1429 return;
1430
1431 #ifdef CONFIG_MBO
1432 wpa_s->wnm_mbo_trans_reason_present = 0;
1433 wpa_s->wnm_mbo_transition_reason = 0;
1434 #endif /* CONFIG_MBO */
1435
1436 if (wpa_s->current_bss)
1437 beacon_int = wpa_s->current_bss->beacon_int;
1438 else
1439 beacon_int = 100; /* best guess */
1440
1441 wpa_s->wnm_dialog_token = pos[0];
1442 wpa_s->wnm_mode = pos[1];
1443 wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
1444 valid_int = pos[4];
1445 wpa_s->wnm_reply = reply;
1446
1447 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
1448 "dialog_token=%u request_mode=0x%x "
1449 "disassoc_timer=%u validity_interval=%u",
1450 wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
1451 wpa_s->wnm_dissoc_timer, valid_int);
1452
1453 #if defined(CONFIG_MBO) && defined(CONFIG_TESTING_OPTIONS)
1454 if (wpa_s->reject_btm_req_reason) {
1455 wpa_printf(MSG_INFO,
1456 "WNM: Testing - reject BSS Transition Management Request: reject_btm_req_reason=%d",
1457 wpa_s->reject_btm_req_reason);
1458 wnm_send_bss_transition_mgmt_resp(
1459 wpa_s, wpa_s->wnm_dialog_token,
1460 wpa_s->reject_btm_req_reason,
1461 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
1462 return;
1463 }
1464 #endif /* CONFIG_MBO && CONFIG_TESTING_OPTIONS */
1465
1466 pos += 5;
1467
1468 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
1469 if (end - pos < 12) {
1470 wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
1471 return;
1472 }
1473 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
1474 pos += 12; /* BSS Termination Duration */
1475 }
1476
1477 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
1478 char url[256];
1479
1480 if (end - pos < 1 || 1 + pos[0] > end - pos) {
1481 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
1482 "Management Request (URL)");
1483 return;
1484 }
1485 os_memcpy(url, pos + 1, pos[0]);
1486 url[pos[0]] = '\0';
1487 pos += 1 + pos[0];
1488
1489 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
1490 wpa_sm_pmf_enabled(wpa_s->wpa),
1491 wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
1492 }
1493
1494 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
1495 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
1496 "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
1497 if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
1498 /* TODO: mark current BSS less preferred for
1499 * selection */
1500 wpa_printf(MSG_DEBUG, "Trying to find another BSS");
1501 wpa_supplicant_req_scan(wpa_s, 0, 0);
1502 }
1503 }
1504
1505 #ifdef CONFIG_MBO
1506 vendor = get_ie(pos, end - pos, WLAN_EID_VENDOR_SPECIFIC);
1507 if (vendor)
1508 wpas_mbo_ie_trans_req(wpa_s, vendor + 2, vendor[1]);
1509 #endif /* CONFIG_MBO */
1510
1511 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
1512 unsigned int valid_ms;
1513
1514 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
1515 wnm_deallocate_memory(wpa_s);
1516 wpa_s->wnm_neighbor_report_elements = os_calloc(
1517 WNM_MAX_NEIGHBOR_REPORT,
1518 sizeof(struct neighbor_report));
1519 if (wpa_s->wnm_neighbor_report_elements == NULL)
1520 return;
1521
1522 while (end - pos >= 2 &&
1523 wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
1524 {
1525 u8 tag = *pos++;
1526 u8 len = *pos++;
1527
1528 wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
1529 tag);
1530 if (len > end - pos) {
1531 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
1532 return;
1533 }
1534 if (tag == WLAN_EID_NEIGHBOR_REPORT) {
1535 struct neighbor_report *rep;
1536 rep = &wpa_s->wnm_neighbor_report_elements[
1537 wpa_s->wnm_num_neighbor_report];
1538 wnm_parse_neighbor_report(wpa_s, pos, len, rep);
1539 wpa_s->wnm_num_neighbor_report++;
1540 #ifdef CONFIG_MBO
1541 if (wpa_s->wnm_mbo_trans_reason_present &&
1542 wpa_s->wnm_num_neighbor_report == 1) {
1543 rep->is_first = 1;
1544 wpa_printf(MSG_DEBUG,
1545 "WNM: First transition candidate is "
1546 MACSTR, MAC2STR(rep->bssid));
1547 }
1548 #endif /* CONFIG_MBO */
1549 }
1550
1551 pos += len;
1552 }
1553
1554 if (!wpa_s->wnm_num_neighbor_report) {
1555 wpa_printf(MSG_DEBUG,
1556 "WNM: Candidate list included bit is set, but no candidates found");
1557 wnm_send_bss_transition_mgmt_resp(
1558 wpa_s, wpa_s->wnm_dialog_token,
1559 WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES,
1560 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0,
1561 NULL);
1562 return;
1563 }
1564
1565 wnm_sort_cand_list(wpa_s);
1566 wnm_dump_cand_list(wpa_s);
1567 valid_ms = valid_int * beacon_int * 128 / 125;
1568 wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
1569 valid_ms);
1570 os_get_reltime(&wpa_s->wnm_cand_valid_until);
1571 wpa_s->wnm_cand_valid_until.sec += valid_ms / 1000;
1572 wpa_s->wnm_cand_valid_until.usec += (valid_ms % 1000) * 1000;
1573 wpa_s->wnm_cand_valid_until.sec +=
1574 wpa_s->wnm_cand_valid_until.usec / 1000000;
1575 wpa_s->wnm_cand_valid_until.usec %= 1000000;
1576 os_memcpy(wpa_s->wnm_cand_from_bss, wpa_s->bssid, ETH_ALEN);
1577
1578 /*
1579 * Fetch the latest scan results from the kernel and check for
1580 * candidates based on those results first. This can help in
1581 * finding more up-to-date information should the driver has
1582 * done some internal scanning operations after the last scan
1583 * result update in wpa_supplicant.
1584 */
1585 if (wnm_fetch_scan_results(wpa_s) > 0)
1586 return;
1587
1588 /*
1589 * Try to use previously received scan results, if they are
1590 * recent enough to use for a connection.
1591 */
1592 if (wpa_s->last_scan_res_used > 0) {
1593 struct os_reltime now;
1594
1595 os_get_reltime(&now);
1596 if (!os_reltime_expired(&now, &wpa_s->last_scan, 10)) {
1597 wpa_printf(MSG_DEBUG,
1598 "WNM: Try to use recent scan results");
1599 if (wnm_scan_process(wpa_s, 0) > 0)
1600 return;
1601 wpa_printf(MSG_DEBUG,
1602 "WNM: No match in previous scan results - try a new scan");
1603 }
1604 }
1605
1606 wnm_set_scan_freqs(wpa_s);
1607 if (wpa_s->wnm_num_neighbor_report == 1) {
1608 os_memcpy(wpa_s->next_scan_bssid,
1609 wpa_s->wnm_neighbor_report_elements[0].bssid,
1610 ETH_ALEN);
1611 wpa_printf(MSG_DEBUG,
1612 "WNM: Scan only for a specific BSSID since there is only a single candidate "
1613 MACSTR, MAC2STR(wpa_s->next_scan_bssid));
1614 }
1615 wpa_supplicant_req_scan(wpa_s, 0, 0);
1616 } else if (reply) {
1617 enum bss_trans_mgmt_status_code status;
1618 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
1619 status = WNM_BSS_TM_ACCEPT;
1620 else {
1621 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
1622 status = WNM_BSS_TM_REJECT_UNSPECIFIED;
1623 }
1624 wnm_send_bss_transition_mgmt_resp(
1625 wpa_s, wpa_s->wnm_dialog_token, status,
1626 MBO_TRANSITION_REJECT_REASON_UNSPECIFIED, 0, NULL);
1627 }
1628 }
1629
1630
1631 #define BTM_QUERY_MIN_SIZE 4
1632
1633 int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
1634 u8 query_reason,
1635 const char *btm_candidates,
1636 int cand_list)
1637 {
1638 struct wpabuf *buf;
1639 int ret;
1640
1641 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
1642 MACSTR " query_reason=%u%s",
1643 MAC2STR(wpa_s->bssid), query_reason,
1644 cand_list ? " candidate list" : "");
1645
1646 buf = wpabuf_alloc(BTM_QUERY_MIN_SIZE);
1647 if (!buf)
1648 return -1;
1649
1650 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1651 wpabuf_put_u8(buf, WNM_BSS_TRANS_MGMT_QUERY);
1652 wpabuf_put_u8(buf, 1);
1653 wpabuf_put_u8(buf, query_reason);
1654
1655 if (cand_list)
1656 wnm_add_cand_list(wpa_s, &buf);
1657
1658 if (btm_candidates) {
1659 const size_t max_len = 1000;
1660
1661 ret = wpabuf_resize(&buf, max_len);
1662 if (ret < 0) {
1663 wpabuf_free(buf);
1664 return ret;
1665 }
1666
1667 ret = ieee802_11_parse_candidate_list(btm_candidates,
1668 wpabuf_put(buf, 0),
1669 max_len);
1670 if (ret < 0) {
1671 wpabuf_free(buf);
1672 return ret;
1673 }
1674
1675 wpabuf_put(buf, ret);
1676 }
1677
1678 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1679 wpa_s->own_addr, wpa_s->bssid,
1680 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
1681
1682 wpabuf_free(buf);
1683 return ret;
1684 }
1685
1686
1687 static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
1688 const u8 *sa, const u8 *data,
1689 int len)
1690 {
1691 const u8 *pos, *end, *next;
1692 u8 ie, ie_len;
1693
1694 pos = data;
1695 end = data + len;
1696
1697 while (end - pos > 1) {
1698 ie = *pos++;
1699 ie_len = *pos++;
1700 wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
1701 ie, ie_len);
1702 if (ie_len > end - pos) {
1703 wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
1704 "subelement");
1705 break;
1706 }
1707 next = pos + ie_len;
1708 if (ie_len < 4) {
1709 pos = next;
1710 continue;
1711 }
1712 wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
1713 WPA_GET_BE24(pos), pos[3]);
1714
1715 #ifdef CONFIG_HS20
1716 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1717 WPA_GET_BE24(pos) == OUI_WFA &&
1718 pos[3] == HS20_WNM_SUB_REM_NEEDED) {
1719 /* Subscription Remediation subelement */
1720 const u8 *ie_end;
1721 u8 url_len;
1722 char *url;
1723 u8 osu_method;
1724
1725 wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
1726 "subelement");
1727 ie_end = pos + ie_len;
1728 pos += 4;
1729 url_len = *pos++;
1730 if (url_len == 0) {
1731 wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
1732 url = NULL;
1733 osu_method = 1;
1734 } else {
1735 if (url_len + 1 > ie_end - pos) {
1736 wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
1737 url_len,
1738 (int) (ie_end - pos));
1739 break;
1740 }
1741 url = os_malloc(url_len + 1);
1742 if (url == NULL)
1743 break;
1744 os_memcpy(url, pos, url_len);
1745 url[url_len] = '\0';
1746 osu_method = pos[url_len];
1747 }
1748 hs20_rx_subscription_remediation(wpa_s, url,
1749 osu_method);
1750 os_free(url);
1751 pos = next;
1752 continue;
1753 }
1754
1755 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
1756 WPA_GET_BE24(pos) == OUI_WFA &&
1757 pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
1758 const u8 *ie_end;
1759 u8 url_len;
1760 char *url;
1761 u8 code;
1762 u16 reauth_delay;
1763
1764 ie_end = pos + ie_len;
1765 pos += 4;
1766 code = *pos++;
1767 reauth_delay = WPA_GET_LE16(pos);
1768 pos += 2;
1769 url_len = *pos++;
1770 wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
1771 "Imminent - Reason Code %u "
1772 "Re-Auth Delay %u URL Length %u",
1773 code, reauth_delay, url_len);
1774 if (url_len > ie_end - pos)
1775 break;
1776 url = os_malloc(url_len + 1);
1777 if (url == NULL)
1778 break;
1779 os_memcpy(url, pos, url_len);
1780 url[url_len] = '\0';
1781 hs20_rx_deauth_imminent_notice(wpa_s, code,
1782 reauth_delay, url);
1783 os_free(url);
1784 pos = next;
1785 continue;
1786 }
1787
1788 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1789 WPA_GET_BE24(pos) == OUI_WFA &&
1790 pos[3] == HS20_WNM_T_C_ACCEPTANCE) {
1791 const u8 *ie_end;
1792 u8 url_len;
1793 char *url;
1794
1795 ie_end = pos + ie_len;
1796 pos += 4;
1797 url_len = *pos++;
1798 wpa_printf(MSG_DEBUG,
1799 "WNM: HS 2.0 Terms and Conditions Acceptance (URL Length %u)",
1800 url_len);
1801 if (url_len > ie_end - pos)
1802 break;
1803 url = os_malloc(url_len + 1);
1804 if (!url)
1805 break;
1806 os_memcpy(url, pos, url_len);
1807 url[url_len] = '\0';
1808 hs20_rx_t_c_acceptance(wpa_s, url);
1809 os_free(url);
1810 pos = next;
1811 continue;
1812 }
1813 #endif /* CONFIG_HS20 */
1814
1815 pos = next;
1816 }
1817 }
1818
1819
1820 static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
1821 const u8 *sa, const u8 *frm, int len)
1822 {
1823 const u8 *pos, *end;
1824 u8 dialog_token, type;
1825
1826 /* Dialog Token [1] | Type [1] | Subelements */
1827
1828 if (len < 2 || sa == NULL)
1829 return;
1830 end = frm + len;
1831 pos = frm;
1832 dialog_token = *pos++;
1833 type = *pos++;
1834
1835 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
1836 "(dialog_token %u type %u sa " MACSTR ")",
1837 dialog_token, type, MAC2STR(sa));
1838 wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
1839 pos, end - pos);
1840
1841 if (wpa_s->wpa_state != WPA_COMPLETED ||
1842 os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
1843 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
1844 "from our AP - ignore it");
1845 return;
1846 }
1847
1848 switch (type) {
1849 case 1:
1850 ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
1851 break;
1852 default:
1853 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
1854 "WNM-Notification type %u", type);
1855 break;
1856 }
1857 }
1858
1859
1860 static void ieee802_11_rx_wnm_coloc_intf_req(struct wpa_supplicant *wpa_s,
1861 const u8 *sa, const u8 *frm,
1862 int len)
1863 {
1864 u8 dialog_token, req_info, auto_report, timeout;
1865
1866 if (!wpa_s->conf->coloc_intf_reporting)
1867 return;
1868
1869 /* Dialog Token [1] | Request Info [1] */
1870
1871 if (len < 2)
1872 return;
1873 dialog_token = frm[0];
1874 req_info = frm[1];
1875 auto_report = req_info & 0x03;
1876 timeout = req_info >> 2;
1877
1878 wpa_dbg(wpa_s, MSG_DEBUG,
1879 "WNM: Received Collocated Interference Request (dialog_token %u auto_report %u timeout %u sa " MACSTR ")",
1880 dialog_token, auto_report, timeout, MAC2STR(sa));
1881
1882 if (dialog_token == 0)
1883 return; /* only nonzero values are used for request */
1884
1885 if (wpa_s->wpa_state != WPA_COMPLETED ||
1886 os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
1887 wpa_dbg(wpa_s, MSG_DEBUG,
1888 "WNM: Collocated Interference Request frame not from current AP - ignore it");
1889 return;
1890 }
1891
1892 wpa_msg(wpa_s, MSG_INFO, COLOC_INTF_REQ "%u %u %u",
1893 dialog_token, auto_report, timeout);
1894 wpa_s->coloc_intf_dialog_token = dialog_token;
1895 wpa_s->coloc_intf_auto_report = auto_report;
1896 wpa_s->coloc_intf_timeout = timeout;
1897 }
1898
1899
1900 void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
1901 const struct ieee80211_mgmt *mgmt, size_t len)
1902 {
1903 const u8 *pos, *end;
1904 u8 act;
1905
1906 if (len < IEEE80211_HDRLEN + 2)
1907 return;
1908
1909 pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
1910 act = *pos++;
1911 end = ((const u8 *) mgmt) + len;
1912
1913 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
1914 act, MAC2STR(mgmt->sa));
1915 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
1916 os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
1917 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
1918 "frame");
1919 return;
1920 }
1921
1922 switch (act) {
1923 case WNM_BSS_TRANS_MGMT_REQ:
1924 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
1925 !(mgmt->da[0] & 0x01));
1926 break;
1927 case WNM_SLEEP_MODE_RESP:
1928 ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
1929 break;
1930 case WNM_NOTIFICATION_REQ:
1931 ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
1932 break;
1933 case WNM_COLLOCATED_INTERFERENCE_REQ:
1934 ieee802_11_rx_wnm_coloc_intf_req(wpa_s, mgmt->sa, pos,
1935 end - pos);
1936 break;
1937 default:
1938 wpa_printf(MSG_ERROR, "WNM: Unknown request");
1939 break;
1940 }
1941 }
1942
1943
1944 int wnm_send_coloc_intf_report(struct wpa_supplicant *wpa_s, u8 dialog_token,
1945 const struct wpabuf *elems)
1946 {
1947 struct wpabuf *buf;
1948 int ret;
1949
1950 if (wpa_s->wpa_state < WPA_ASSOCIATED || !elems)
1951 return -1;
1952
1953 wpa_printf(MSG_DEBUG, "WNM: Send Collocated Interference Report to "
1954 MACSTR " (dialog token %u)",
1955 MAC2STR(wpa_s->bssid), dialog_token);
1956
1957 buf = wpabuf_alloc(3 + wpabuf_len(elems));
1958 if (!buf)
1959 return -1;
1960
1961 wpabuf_put_u8(buf, WLAN_ACTION_WNM);
1962 wpabuf_put_u8(buf, WNM_COLLOCATED_INTERFERENCE_REPORT);
1963 wpabuf_put_u8(buf, dialog_token);
1964 wpabuf_put_buf(buf, elems);
1965
1966 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
1967 wpa_s->own_addr, wpa_s->bssid,
1968 wpabuf_head_u8(buf), wpabuf_len(buf), 0);
1969 wpabuf_free(buf);
1970 return ret;
1971 }
1972
1973
1974 void wnm_set_coloc_intf_elems(struct wpa_supplicant *wpa_s,
1975 struct wpabuf *elems)
1976 {
1977 wpabuf_free(wpa_s->coloc_intf_elems);
1978 if (elems && wpabuf_len(elems) == 0) {
1979 wpabuf_free(elems);
1980 elems = NULL;
1981 }
1982 wpa_s->coloc_intf_elems = elems;
1983
1984 if (wpa_s->conf->coloc_intf_reporting && wpa_s->coloc_intf_elems &&
1985 wpa_s->coloc_intf_dialog_token &&
1986 (wpa_s->coloc_intf_auto_report == 1 ||
1987 wpa_s->coloc_intf_auto_report == 3)) {
1988 /* TODO: Check that there has not been less than
1989 * wpa_s->coloc_intf_timeout * 200 TU from the last report.
1990 */
1991 wnm_send_coloc_intf_report(wpa_s,
1992 wpa_s->coloc_intf_dialog_token,
1993 wpa_s->coloc_intf_elems);
1994 }
1995 }
1996
1997
1998 void wnm_clear_coloc_intf_reporting(struct wpa_supplicant *wpa_s)
1999 {
2000 #ifdef CONFIG_WNM
2001 wpa_s->coloc_intf_dialog_token = 0;
2002 wpa_s->coloc_intf_auto_report = 0;
2003 #endif /* CONFIG_WNM */
2004 }
2005