blob: 0619f6db3753df201c5cfc253722854b395a1a8a [file] [log] [blame]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001/*
2 * wpa_supplicant - WNM
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003 * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004 *
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"
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070013#include "common/wpa_ctrl.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070014#include "rsn_supp/wpa.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080015#include "wpa_supplicant_i.h"
16#include "driver_i.h"
17#include "scan.h"
Dmitry Shmidt44c95782013-05-17 09:51:35 -070018#include "ctrl_iface.h"
19#include "bss.h"
20#include "wnm_sta.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080021#include "hs20_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070022
23#define MAX_TFS_IE_LEN 1024
Dmitry Shmidt44c95782013-05-17 09:51:35 -070024#define WNM_MAX_NEIGHBOR_REPORT 10
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070025
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070026
27/* get the TFS IE from driver */
28static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
29 u16 *buf_len, enum wnm_oper oper)
30{
31 wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
32
33 return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
34}
35
36
37/* set the TFS IE to driver */
38static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
39 const u8 *addr, u8 *buf, u16 *buf_len,
40 enum wnm_oper oper)
41{
42 wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
43
44 return wpa_drv_wnm_oper(wpa_s, oper, addr, buf, buf_len);
45}
46
47
48/* MLME-SLEEPMODE.request */
49int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080050 u8 action, u16 intval, struct wpabuf *tfs_req)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070051{
52 struct ieee80211_mgmt *mgmt;
53 int res;
54 size_t len;
55 struct wnm_sleep_element *wnmsleep_ie;
56 u8 *wnmtfs_ie;
57 u8 wnmsleep_ie_len;
58 u16 wnmtfs_ie_len; /* possibly multiple IE(s) */
59 enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
60 WNM_SLEEP_TFS_REQ_IE_NONE;
61
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080062 wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
63 "action=%s to " MACSTR,
64 action == 0 ? "enter" : "exit",
65 MAC2STR(wpa_s->bssid));
66
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070067 /* WNM-Sleep Mode IE */
68 wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
69 wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
70 if (wnmsleep_ie == NULL)
71 return -1;
72 wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
73 wnmsleep_ie->len = wnmsleep_ie_len - 2;
74 wnmsleep_ie->action_type = action;
75 wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080076 wnmsleep_ie->intval = host_to_le16(intval);
77 wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
78 (u8 *) wnmsleep_ie, wnmsleep_ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070079
80 /* TFS IE(s) */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080081 if (tfs_req) {
82 wnmtfs_ie_len = wpabuf_len(tfs_req);
83 wnmtfs_ie = os_malloc(wnmtfs_ie_len);
84 if (wnmtfs_ie == NULL) {
85 os_free(wnmsleep_ie);
86 return -1;
87 }
88 os_memcpy(wnmtfs_ie, wpabuf_head(tfs_req), wnmtfs_ie_len);
89 } else {
90 wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
91 if (wnmtfs_ie == NULL) {
92 os_free(wnmsleep_ie);
93 return -1;
94 }
95 if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
96 tfs_oper)) {
97 wnmtfs_ie_len = 0;
98 os_free(wnmtfs_ie);
99 wnmtfs_ie = NULL;
100 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700101 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800102 wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
103 (u8 *) wnmtfs_ie, wnmtfs_ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700104
105 mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len);
106 if (mgmt == NULL) {
107 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
108 "WNM-Sleep Request action frame");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800109 os_free(wnmsleep_ie);
110 os_free(wnmtfs_ie);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700111 return -1;
112 }
113
114 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
115 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
116 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
117 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
118 WLAN_FC_STYPE_ACTION);
119 mgmt->u.action.category = WLAN_ACTION_WNM;
120 mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800121 mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700122 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
123 wnmsleep_ie_len);
124 /* copy TFS IE here */
125 if (wnmtfs_ie_len > 0) {
126 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
127 wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
128 }
129
130 len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
131 wnmtfs_ie_len;
132
133 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
134 wpa_s->own_addr, wpa_s->bssid,
135 &mgmt->u.action.category, len, 0);
136 if (res < 0)
137 wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
138 "(action=%d, intval=%d)", action, intval);
139
140 os_free(wnmsleep_ie);
141 os_free(wnmtfs_ie);
142 os_free(mgmt);
143
144 return res;
145}
146
147
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800148static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
149 u8 *tfsresp_ie_start,
150 u8 *tfsresp_ie_end)
151{
152 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
153 wpa_s->bssid, NULL, NULL);
154 /* remove GTK/IGTK ?? */
155
156 /* set the TFS Resp IE(s) */
157 if (tfsresp_ie_start && tfsresp_ie_end &&
158 tfsresp_ie_end - tfsresp_ie_start >= 0) {
159 u16 tfsresp_ie_len;
160 tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
161 tfsresp_ie_start;
162 wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
163 /* pass the TFS Resp IE(s) to driver for processing */
164 if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
165 tfsresp_ie_start,
166 &tfsresp_ie_len,
167 WNM_SLEEP_TFS_RESP_IE_SET))
168 wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
169 }
170}
171
172
173static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
174 const u8 *frm, u16 key_len_total)
175{
176 u8 *ptr, *end;
177 u8 gtk_len;
178
179 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM, wpa_s->bssid,
180 NULL, NULL);
181
182 /* Install GTK/IGTK */
183
184 /* point to key data field */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800185 ptr = (u8 *) frm + 1 + 2;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800186 end = ptr + key_len_total;
187 wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
188
189 while (ptr + 1 < end) {
190 if (ptr + 2 + ptr[1] > end) {
191 wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
192 "length");
193 if (end > ptr) {
194 wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
195 ptr, end - ptr);
196 }
197 break;
198 }
199 if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
200 if (ptr[1] < 11 + 5) {
201 wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
202 "subelem");
203 break;
204 }
205 gtk_len = *(ptr + 4);
206 if (ptr[1] < 11 + gtk_len ||
207 gtk_len < 5 || gtk_len > 32) {
208 wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
209 "subelem");
210 break;
211 }
212 wpa_wnmsleep_install_key(
213 wpa_s->wpa,
214 WNM_SLEEP_SUBELEM_GTK,
215 ptr);
216 ptr += 13 + gtk_len;
217#ifdef CONFIG_IEEE80211W
218 } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
219 if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
220 wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
221 "subelem");
222 break;
223 }
224 wpa_wnmsleep_install_key(wpa_s->wpa,
225 WNM_SLEEP_SUBELEM_IGTK, ptr);
226 ptr += 10 + WPA_IGTK_LEN;
227#endif /* CONFIG_IEEE80211W */
228 } else
229 break; /* skip the loop */
230 }
231}
232
233
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700234static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
235 const u8 *frm, int len)
236{
237 /*
238 * Action [1] | Diaglog Token [1] | Key Data Len [2] | Key Data |
239 * WNM-Sleep Mode IE | TFS Response IE
240 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800241 u8 *pos = (u8 *) frm; /* point to payload after the action field */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700242 u16 key_len_total = le_to_host16(*((u16 *)(frm+2)));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700243 struct wnm_sleep_element *wnmsleep_ie = NULL;
244 /* multiple TFS Resp IE (assuming consecutive) */
245 u8 *tfsresp_ie_start = NULL;
246 u8 *tfsresp_ie_end = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700247
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800248 wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
249 frm[0], key_len_total);
250 pos += 3 + key_len_total;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800251 if (pos > frm + len) {
252 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
253 return;
254 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700255 while (pos - frm < len) {
256 u8 ie_len = *(pos + 1);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800257 if (pos + 2 + ie_len > frm + len) {
258 wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
259 break;
260 }
261 wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700262 if (*pos == WLAN_EID_WNMSLEEP)
263 wnmsleep_ie = (struct wnm_sleep_element *) pos;
264 else if (*pos == WLAN_EID_TFS_RESP) {
265 if (!tfsresp_ie_start)
266 tfsresp_ie_start = pos;
267 tfsresp_ie_end = pos;
268 } else
269 wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
270 pos += ie_len + 2;
271 }
272
273 if (!wnmsleep_ie) {
274 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
275 return;
276 }
277
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800278 if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
279 wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700280 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
281 "frame (action=%d, intval=%d)",
282 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800283 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
284 wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
285 tfsresp_ie_end);
286 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
287 wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700288 }
289 } else {
290 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
291 "(action=%d, intval=%d)",
292 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800293 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700294 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
295 wpa_s->bssid, NULL, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800296 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700297 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
298 wpa_s->bssid, NULL, NULL);
299 }
300}
301
302
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700303void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
304{
305 int i;
306
307 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
308 os_free(wpa_s->wnm_neighbor_report_elements[i].tsf_info);
309 os_free(wpa_s->wnm_neighbor_report_elements[i].con_coun_str);
310 os_free(wpa_s->wnm_neighbor_report_elements[i].bss_tran_can);
311 os_free(wpa_s->wnm_neighbor_report_elements[i].bss_term_dur);
312 os_free(wpa_s->wnm_neighbor_report_elements[i].bearing);
313 os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
314 os_free(wpa_s->wnm_neighbor_report_elements[i].rrm_cap);
315 os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
316 }
317
318 os_free(wpa_s->wnm_neighbor_report_elements);
319 wpa_s->wnm_neighbor_report_elements = NULL;
320}
321
322
323static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
324 u8 id, u8 elen, const u8 *pos)
325{
326 switch (id) {
327 case WNM_NEIGHBOR_TSF:
328 if (elen < 2 + 2) {
329 wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
330 break;
331 }
332 rep->tsf_info = os_zalloc(sizeof(struct tsf_info));
333 if (rep->tsf_info == NULL)
334 break;
335 rep->tsf_info->present = 1;
336 os_memcpy(rep->tsf_info->tsf_offset, pos, 2);
337 os_memcpy(rep->tsf_info->beacon_interval, pos + 2, 2);
338 break;
339 case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
340 if (elen < 2) {
341 wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
342 "country string");
343 break;
344 }
345 rep->con_coun_str =
346 os_zalloc(sizeof(struct condensed_country_string));
347 if (rep->con_coun_str == NULL)
348 break;
349 rep->con_coun_str->present = 1;
350 os_memcpy(rep->con_coun_str->country_string, pos, 2);
351 break;
352 case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
353 if (elen < 1) {
354 wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
355 "candidate");
356 break;
357 }
358 rep->bss_tran_can =
359 os_zalloc(sizeof(struct bss_transition_candidate));
360 if (rep->bss_tran_can == NULL)
361 break;
362 rep->bss_tran_can->present = 1;
363 rep->bss_tran_can->preference = pos[0];
364 break;
365 case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
366 if (elen < 12) {
367 wpa_printf(MSG_DEBUG, "WNM: Too short BSS termination "
368 "duration");
369 break;
370 }
371 rep->bss_term_dur =
372 os_zalloc(sizeof(struct bss_termination_duration));
373 if (rep->bss_term_dur == NULL)
374 break;
375 rep->bss_term_dur->present = 1;
376 os_memcpy(rep->bss_term_dur->duration, pos, 12);
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 }
384 rep->bearing = os_zalloc(sizeof(struct bearing));
385 if (rep->bearing == NULL)
386 break;
387 rep->bearing->present = 1;
388 os_memcpy(rep->bearing->bearing, pos, 8);
389 break;
390 case WNM_NEIGHBOR_MEASUREMENT_PILOT:
391 if (elen < 2) {
392 wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
393 "pilot");
394 break;
395 }
396 rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
397 if (rep->meas_pilot == NULL)
398 break;
399 rep->meas_pilot->present = 1;
400 rep->meas_pilot->measurement_pilot = pos[0];
401 rep->meas_pilot->num_vendor_specific = pos[1];
402 os_memcpy(rep->meas_pilot->vendor_specific, pos + 2, elen - 2);
403 break;
404 case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
405 if (elen < 4) {
406 wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
407 "capabilities");
408 break;
409 }
410 rep->rrm_cap =
411 os_zalloc(sizeof(struct rrm_enabled_capabilities));
412 if (rep->rrm_cap == NULL)
413 break;
414 rep->rrm_cap->present = 1;
415 os_memcpy(rep->rrm_cap->capabilities, pos, 4);
416 break;
417 case WNM_NEIGHBOR_MULTIPLE_BSSID:
418 if (elen < 2) {
419 wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
420 break;
421 }
422 rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
423 if (rep->mul_bssid == NULL)
424 break;
425 rep->mul_bssid->present = 1;
426 rep->mul_bssid->max_bssid_indicator = pos[0];
427 rep->mul_bssid->num_vendor_specific = pos[1];
428 os_memcpy(rep->mul_bssid->vendor_specific, pos + 2, elen - 2);
429 break;
430 }
431}
432
433
434static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
435 const u8 *pos, u8 len,
436 struct neighbor_report *rep)
437{
438 u8 left = len;
439
440 if (left < 13) {
441 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
442 return;
443 }
444
445 os_memcpy(rep->bssid, pos, ETH_ALEN);
446 os_memcpy(rep->bssid_information, pos + ETH_ALEN, 4);
447 rep->regulatory_class = *(pos + 10);
448 rep->channel_number = *(pos + 11);
449 rep->phy_type = *(pos + 12);
450
451 pos += 13;
452 left -= 13;
453
454 while (left >= 2) {
455 u8 id, elen;
456
457 id = *pos++;
458 elen = *pos++;
459 wnm_parse_neighbor_report_elem(rep, id, elen, pos);
460 left -= 2 + elen;
461 pos += elen;
462 }
463}
464
465
466static int compare_scan_neighbor_results(struct wpa_supplicant *wpa_s,
467 struct wpa_scan_results *scan_res,
468 struct neighbor_report *neigh_rep,
469 u8 num_neigh_rep, u8 *bssid_to_connect)
470{
471
472 u8 i, j;
473
474 if (scan_res == NULL || num_neigh_rep == 0)
475 return 0;
476
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800477 wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
478 MAC2STR(wpa_s->bssid),
479 wpa_s->current_bss ? wpa_s->current_bss->level : 0);
480
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700481 for (i = 0; i < num_neigh_rep; i++) {
482 for (j = 0; j < scan_res->num; j++) {
483 /* Check for a better RSSI AP */
484 if (os_memcmp(scan_res->res[j]->bssid,
485 neigh_rep[i].bssid, ETH_ALEN) == 0 &&
486 scan_res->res[j]->level >
487 wpa_s->current_bss->level) {
488 /* Got a BSSID with better RSSI value */
489 os_memcpy(bssid_to_connect, neigh_rep[i].bssid,
490 ETH_ALEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800491 wpa_printf(MSG_DEBUG, "Found a BSS " MACSTR
492 " with better scan RSSI %d",
493 MAC2STR(scan_res->res[j]->bssid),
494 scan_res->res[j]->level);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700495 return 1;
496 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800497 wpa_printf(MSG_DEBUG, "scan_res[%d] " MACSTR
498 " RSSI %d", j,
499 MAC2STR(scan_res->res[j]->bssid),
500 scan_res->res[j]->level);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700501 }
502 }
503
504 return 0;
505}
506
507
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700508static void wnm_send_bss_transition_mgmt_resp(
509 struct wpa_supplicant *wpa_s, u8 dialog_token,
510 enum bss_trans_mgmt_status_code status, u8 delay,
511 const u8 *target_bssid)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800512{
513 u8 buf[1000], *pos;
514 struct ieee80211_mgmt *mgmt;
515 size_t len;
516
517 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Response "
518 "to " MACSTR " dialog_token=%u status=%u delay=%d",
519 MAC2STR(wpa_s->bssid), dialog_token, status, delay);
520
521 mgmt = (struct ieee80211_mgmt *) buf;
522 os_memset(&buf, 0, sizeof(buf));
523 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
524 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
525 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
526 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
527 WLAN_FC_STYPE_ACTION);
528 mgmt->u.action.category = WLAN_ACTION_WNM;
529 mgmt->u.action.u.bss_tm_resp.action = WNM_BSS_TRANS_MGMT_RESP;
530 mgmt->u.action.u.bss_tm_resp.dialog_token = dialog_token;
531 mgmt->u.action.u.bss_tm_resp.status_code = status;
532 mgmt->u.action.u.bss_tm_resp.bss_termination_delay = delay;
533 pos = mgmt->u.action.u.bss_tm_resp.variable;
534 if (target_bssid) {
535 os_memcpy(pos, target_bssid, ETH_ALEN);
536 pos += ETH_ALEN;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800537 } else if (status == WNM_BSS_TM_ACCEPT) {
538 /*
539 * P802.11-REVmc clarifies that the Target BSSID field is always
540 * present when status code is zero, so use a fake value here if
541 * no BSSID is yet known.
542 */
543 os_memset(pos, 0, ETH_ALEN);
544 pos += ETH_ALEN;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800545 }
546
547 len = pos - (u8 *) &mgmt->u.action.category;
548
549 wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
550 wpa_s->own_addr, wpa_s->bssid,
551 &mgmt->u.action.category, len, 0);
552}
553
554
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700555void wnm_scan_response(struct wpa_supplicant *wpa_s,
556 struct wpa_scan_results *scan_res)
557{
558 u8 bssid[ETH_ALEN];
559
560 if (scan_res == NULL) {
561 wpa_printf(MSG_ERROR, "Scan result is NULL");
562 goto send_bss_resp_fail;
563 }
564
565 /* Compare the Neighbor Report and scan results */
566 if (compare_scan_neighbor_results(wpa_s, scan_res,
567 wpa_s->wnm_neighbor_report_elements,
568 wpa_s->wnm_num_neighbor_report,
569 bssid) == 1) {
570 /* Associate to the network */
571 struct wpa_bss *bss;
572 struct wpa_ssid *ssid = wpa_s->current_ssid;
573
574 bss = wpa_bss_get_bssid(wpa_s, bssid);
575 if (!bss) {
576 wpa_printf(MSG_DEBUG, "WNM: Target AP not found from "
577 "BSS table");
578 goto send_bss_resp_fail;
579 }
580
581 /* Send the BSS Management Response - Accept */
582 if (wpa_s->wnm_reply) {
583 wnm_send_bss_transition_mgmt_resp(wpa_s,
584 wpa_s->wnm_dialog_token,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700585 WNM_BSS_TM_ACCEPT,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800586 0, bssid);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700587 }
588
589 wpa_s->reassociate = 1;
590 wpa_supplicant_connect(wpa_s, bss, ssid);
591 wnm_deallocate_memory(wpa_s);
592 return;
593 }
594
595 /* Send reject response for all the failures */
596send_bss_resp_fail:
597 wnm_deallocate_memory(wpa_s);
598 if (wpa_s->wnm_reply) {
599 wnm_send_bss_transition_mgmt_resp(wpa_s,
600 wpa_s->wnm_dialog_token,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700601 WNM_BSS_TM_REJECT_UNSPECIFIED,
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700602 0, NULL);
603 }
604 return;
605}
606
607
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800608static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
609 const u8 *pos, const u8 *end,
610 int reply)
611{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800612 if (pos + 5 > end)
613 return;
614
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700615 wpa_s->wnm_dialog_token = pos[0];
616 wpa_s->wnm_mode = pos[1];
617 wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
618 wpa_s->wnm_validity_interval = pos[4];
619 wpa_s->wnm_reply = reply;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800620
621 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
622 "dialog_token=%u request_mode=0x%x "
623 "disassoc_timer=%u validity_interval=%u",
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700624 wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
625 wpa_s->wnm_dissoc_timer, wpa_s->wnm_validity_interval);
626
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800627 pos += 5;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700628
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700629 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700630 if (pos + 12 > end) {
631 wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
632 return;
633 }
634 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800635 pos += 12; /* BSS Termination Duration */
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700636 }
637
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700638 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800639 char url[256];
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700640 unsigned int beacon_int;
641
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800642 if (pos + 1 > end || pos + 1 + pos[0] > end) {
643 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
644 "Management Request (URL)");
645 return;
646 }
647 os_memcpy(url, pos + 1, pos[0]);
648 url[pos[0]] = '\0';
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700649 pos += 1 + pos[0];
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700650
651 if (wpa_s->current_bss)
652 beacon_int = wpa_s->current_bss->beacon_int;
653 else
654 beacon_int = 100; /* best guess */
655
656 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
657 wpa_sm_pmf_enabled(wpa_s->wpa),
658 wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800659 }
660
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700661 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800662 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700663 "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
664 if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800665 /* TODO: mark current BSS less preferred for
666 * selection */
667 wpa_printf(MSG_DEBUG, "Trying to find another BSS");
668 wpa_supplicant_req_scan(wpa_s, 0, 0);
669 }
670 }
671
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700672 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700673 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
674 wpa_s->wnm_num_neighbor_report = 0;
675 os_free(wpa_s->wnm_neighbor_report_elements);
676 wpa_s->wnm_neighbor_report_elements = os_zalloc(
677 WNM_MAX_NEIGHBOR_REPORT *
678 sizeof(struct neighbor_report));
679 if (wpa_s->wnm_neighbor_report_elements == NULL)
680 return;
681
682 while (pos + 2 <= end &&
683 wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
684 {
685 u8 tag = *pos++;
686 u8 len = *pos++;
687
688 wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
689 tag);
690 if (pos + len > end) {
691 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
692 return;
693 }
694 wnm_parse_neighbor_report(
695 wpa_s, pos, len,
696 &wpa_s->wnm_neighbor_report_elements[
697 wpa_s->wnm_num_neighbor_report]);
698
699 pos += len;
700 wpa_s->wnm_num_neighbor_report++;
701 }
702
703 wpa_s->scan_res_handler = wnm_scan_response;
704 wpa_supplicant_req_scan(wpa_s, 0, 0);
705 } else if (reply) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700706 enum bss_trans_mgmt_status_code status;
707 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
708 status = WNM_BSS_TM_ACCEPT;
709 else {
710 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
711 status = WNM_BSS_TM_REJECT_UNSPECIFIED;
712 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700713 wnm_send_bss_transition_mgmt_resp(wpa_s,
714 wpa_s->wnm_dialog_token,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700715 status, 0, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800716 }
717}
718
719
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700720int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
721 u8 query_reason)
722{
723 u8 buf[1000], *pos;
724 struct ieee80211_mgmt *mgmt;
725 size_t len;
726 int ret;
727
728 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
729 MACSTR " query_reason=%u",
730 MAC2STR(wpa_s->bssid), query_reason);
731
732 mgmt = (struct ieee80211_mgmt *) buf;
733 os_memset(&buf, 0, sizeof(buf));
734 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
735 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
736 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
737 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
738 WLAN_FC_STYPE_ACTION);
739 mgmt->u.action.category = WLAN_ACTION_WNM;
740 mgmt->u.action.u.bss_tm_query.action = WNM_BSS_TRANS_MGMT_QUERY;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800741 mgmt->u.action.u.bss_tm_query.dialog_token = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700742 mgmt->u.action.u.bss_tm_query.query_reason = query_reason;
743 pos = mgmt->u.action.u.bss_tm_query.variable;
744
745 len = pos - (u8 *) &mgmt->u.action.category;
746
747 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
748 wpa_s->own_addr, wpa_s->bssid,
749 &mgmt->u.action.category, len, 0);
750
751 return ret;
752}
753
754
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800755static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
756 const u8 *sa, const u8 *data,
757 int len)
758{
759 const u8 *pos, *end, *next;
760 u8 ie, ie_len;
761
762 pos = data;
763 end = data + len;
764
765 while (pos + 1 < end) {
766 ie = *pos++;
767 ie_len = *pos++;
768 wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
769 ie, ie_len);
770 if (ie_len > end - pos) {
771 wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
772 "subelement");
773 break;
774 }
775 next = pos + ie_len;
776 if (ie_len < 4) {
777 pos = next;
778 continue;
779 }
780 wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
781 WPA_GET_BE24(pos), pos[3]);
782
783#ifdef CONFIG_HS20
784 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
785 WPA_GET_BE24(pos) == OUI_WFA &&
786 pos[3] == HS20_WNM_SUB_REM_NEEDED) {
787 /* Subscription Remediation subelement */
788 const u8 *ie_end;
789 u8 url_len;
790 char *url;
791 u8 osu_method;
792
793 wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
794 "subelement");
795 ie_end = pos + ie_len;
796 pos += 4;
797 url_len = *pos++;
798 if (url_len == 0) {
799 wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
800 url = NULL;
801 osu_method = 1;
802 } else {
803 if (pos + url_len + 1 > ie_end) {
804 wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
805 url_len,
806 (int) (ie_end - pos));
807 break;
808 }
809 url = os_malloc(url_len + 1);
810 if (url == NULL)
811 break;
812 os_memcpy(url, pos, url_len);
813 url[url_len] = '\0';
814 osu_method = pos[url_len];
815 }
816 hs20_rx_subscription_remediation(wpa_s, url,
817 osu_method);
818 os_free(url);
819 pos = next;
820 continue;
821 }
822
823 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
824 WPA_GET_BE24(pos) == OUI_WFA &&
825 pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
826 const u8 *ie_end;
827 u8 url_len;
828 char *url;
829 u8 code;
830 u16 reauth_delay;
831
832 ie_end = pos + ie_len;
833 pos += 4;
834 code = *pos++;
835 reauth_delay = WPA_GET_LE16(pos);
836 pos += 2;
837 url_len = *pos++;
838 wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
839 "Imminent - Reason Code %u "
840 "Re-Auth Delay %u URL Length %u",
841 code, reauth_delay, url_len);
842 if (pos + url_len > ie_end)
843 break;
844 url = os_malloc(url_len + 1);
845 if (url == NULL)
846 break;
847 os_memcpy(url, pos, url_len);
848 url[url_len] = '\0';
849 hs20_rx_deauth_imminent_notice(wpa_s, code,
850 reauth_delay, url);
851 os_free(url);
852 pos = next;
853 continue;
854 }
855#endif /* CONFIG_HS20 */
856
857 pos = next;
858 }
859}
860
861
862static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
863 const u8 *sa, const u8 *frm, int len)
864{
865 const u8 *pos, *end;
866 u8 dialog_token, type;
867
868 /* Dialog Token [1] | Type [1] | Subelements */
869
870 if (len < 2 || sa == NULL)
871 return;
872 end = frm + len;
873 pos = frm;
874 dialog_token = *pos++;
875 type = *pos++;
876
877 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
878 "(dialog_token %u type %u sa " MACSTR ")",
879 dialog_token, type, MAC2STR(sa));
880 wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
881 pos, end - pos);
882
883 if (wpa_s->wpa_state != WPA_COMPLETED ||
884 os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
885 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
886 "from our AP - ignore it");
887 return;
888 }
889
890 switch (type) {
891 case 1:
892 ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
893 break;
894 default:
895 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
896 "WNM-Notification type %u", type);
897 break;
898 }
899}
900
901
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700902void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800903 const struct ieee80211_mgmt *mgmt, size_t len)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700904{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800905 const u8 *pos, *end;
906 u8 act;
907
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800908 if (len < IEEE80211_HDRLEN + 2)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800909 return;
910
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800911 pos = &mgmt->u.action.category;
912 pos++;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800913 act = *pos++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800914 end = ((const u8 *) mgmt) + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800915
916 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800917 act, MAC2STR(mgmt->sa));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800918 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800919 os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800920 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
921 "frame");
922 return;
923 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700924
925 switch (act) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800926 case WNM_BSS_TRANS_MGMT_REQ:
927 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800928 !(mgmt->da[0] & 0x01));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800929 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700930 case WNM_SLEEP_MODE_RESP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800931 ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700932 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800933 case WNM_NOTIFICATION_REQ:
934 ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
935 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700936 default:
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700937 wpa_printf(MSG_ERROR, "WNM: Unknown request");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700938 break;
939 }
940}