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