blob: 1ca4c71faf77d2b16789d541250d05799b18ab5e [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 /*
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700238 * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700239 * 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 Shmidt21de2142014-04-08 10:50:52 -0700242 u16 key_len_total;
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 Shmidt21de2142014-04-08 10:50:52 -0700248 if (len < 3)
249 return;
250 key_len_total = WPA_GET_LE16(frm + 1);
251
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800252 wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
253 frm[0], key_len_total);
254 pos += 3 + key_len_total;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800255 if (pos > frm + len) {
256 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
257 return;
258 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700259 while (pos - frm < len) {
260 u8 ie_len = *(pos + 1);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800261 if (pos + 2 + ie_len > frm + len) {
262 wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
263 break;
264 }
265 wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700266 if (*pos == WLAN_EID_WNMSLEEP)
267 wnmsleep_ie = (struct wnm_sleep_element *) pos;
268 else if (*pos == WLAN_EID_TFS_RESP) {
269 if (!tfsresp_ie_start)
270 tfsresp_ie_start = pos;
271 tfsresp_ie_end = pos;
272 } else
273 wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
274 pos += ie_len + 2;
275 }
276
277 if (!wnmsleep_ie) {
278 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
279 return;
280 }
281
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800282 if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
283 wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700284 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
285 "frame (action=%d, intval=%d)",
286 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800287 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
288 wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
289 tfsresp_ie_end);
290 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
291 wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700292 }
293 } else {
294 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
295 "(action=%d, intval=%d)",
296 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800297 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700298 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
299 wpa_s->bssid, NULL, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800300 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700301 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
302 wpa_s->bssid, NULL, NULL);
303 }
304}
305
306
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700307void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
308{
309 int i;
310
311 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
312 os_free(wpa_s->wnm_neighbor_report_elements[i].tsf_info);
313 os_free(wpa_s->wnm_neighbor_report_elements[i].con_coun_str);
314 os_free(wpa_s->wnm_neighbor_report_elements[i].bss_tran_can);
315 os_free(wpa_s->wnm_neighbor_report_elements[i].bss_term_dur);
316 os_free(wpa_s->wnm_neighbor_report_elements[i].bearing);
317 os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
318 os_free(wpa_s->wnm_neighbor_report_elements[i].rrm_cap);
319 os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
320 }
321
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700322 wpa_s->wnm_num_neighbor_report = 0;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700323 os_free(wpa_s->wnm_neighbor_report_elements);
324 wpa_s->wnm_neighbor_report_elements = NULL;
325}
326
327
328static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
329 u8 id, u8 elen, const u8 *pos)
330{
331 switch (id) {
332 case WNM_NEIGHBOR_TSF:
333 if (elen < 2 + 2) {
334 wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
335 break;
336 }
337 rep->tsf_info = os_zalloc(sizeof(struct tsf_info));
338 if (rep->tsf_info == NULL)
339 break;
340 rep->tsf_info->present = 1;
341 os_memcpy(rep->tsf_info->tsf_offset, pos, 2);
342 os_memcpy(rep->tsf_info->beacon_interval, pos + 2, 2);
343 break;
344 case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
345 if (elen < 2) {
346 wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
347 "country string");
348 break;
349 }
350 rep->con_coun_str =
351 os_zalloc(sizeof(struct condensed_country_string));
352 if (rep->con_coun_str == NULL)
353 break;
354 rep->con_coun_str->present = 1;
355 os_memcpy(rep->con_coun_str->country_string, pos, 2);
356 break;
357 case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
358 if (elen < 1) {
359 wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
360 "candidate");
361 break;
362 }
363 rep->bss_tran_can =
364 os_zalloc(sizeof(struct bss_transition_candidate));
365 if (rep->bss_tran_can == NULL)
366 break;
367 rep->bss_tran_can->present = 1;
368 rep->bss_tran_can->preference = pos[0];
369 break;
370 case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
371 if (elen < 12) {
372 wpa_printf(MSG_DEBUG, "WNM: Too short BSS termination "
373 "duration");
374 break;
375 }
376 rep->bss_term_dur =
377 os_zalloc(sizeof(struct bss_termination_duration));
378 if (rep->bss_term_dur == NULL)
379 break;
380 rep->bss_term_dur->present = 1;
381 os_memcpy(rep->bss_term_dur->duration, pos, 12);
382 break;
383 case WNM_NEIGHBOR_BEARING:
384 if (elen < 8) {
385 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
386 "bearing");
387 break;
388 }
389 rep->bearing = os_zalloc(sizeof(struct bearing));
390 if (rep->bearing == NULL)
391 break;
392 rep->bearing->present = 1;
393 os_memcpy(rep->bearing->bearing, pos, 8);
394 break;
395 case WNM_NEIGHBOR_MEASUREMENT_PILOT:
396 if (elen < 2) {
397 wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
398 "pilot");
399 break;
400 }
401 rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
402 if (rep->meas_pilot == NULL)
403 break;
404 rep->meas_pilot->present = 1;
405 rep->meas_pilot->measurement_pilot = pos[0];
406 rep->meas_pilot->num_vendor_specific = pos[1];
407 os_memcpy(rep->meas_pilot->vendor_specific, pos + 2, elen - 2);
408 break;
409 case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
410 if (elen < 4) {
411 wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
412 "capabilities");
413 break;
414 }
415 rep->rrm_cap =
416 os_zalloc(sizeof(struct rrm_enabled_capabilities));
417 if (rep->rrm_cap == NULL)
418 break;
419 rep->rrm_cap->present = 1;
420 os_memcpy(rep->rrm_cap->capabilities, pos, 4);
421 break;
422 case WNM_NEIGHBOR_MULTIPLE_BSSID:
423 if (elen < 2) {
424 wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
425 break;
426 }
427 rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
428 if (rep->mul_bssid == NULL)
429 break;
430 rep->mul_bssid->present = 1;
431 rep->mul_bssid->max_bssid_indicator = pos[0];
432 rep->mul_bssid->num_vendor_specific = pos[1];
433 os_memcpy(rep->mul_bssid->vendor_specific, pos + 2, elen - 2);
434 break;
435 }
436}
437
438
439static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
440 const u8 *pos, u8 len,
441 struct neighbor_report *rep)
442{
443 u8 left = len;
444
445 if (left < 13) {
446 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
447 return;
448 }
449
450 os_memcpy(rep->bssid, pos, ETH_ALEN);
451 os_memcpy(rep->bssid_information, pos + ETH_ALEN, 4);
452 rep->regulatory_class = *(pos + 10);
453 rep->channel_number = *(pos + 11);
454 rep->phy_type = *(pos + 12);
455
456 pos += 13;
457 left -= 13;
458
459 while (left >= 2) {
460 u8 id, elen;
461
462 id = *pos++;
463 elen = *pos++;
464 wnm_parse_neighbor_report_elem(rep, id, elen, pos);
465 left -= 2 + elen;
466 pos += elen;
467 }
468}
469
470
471static int compare_scan_neighbor_results(struct wpa_supplicant *wpa_s,
472 struct wpa_scan_results *scan_res,
473 struct neighbor_report *neigh_rep,
474 u8 num_neigh_rep, u8 *bssid_to_connect)
475{
476
477 u8 i, j;
478
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800479 if (scan_res == NULL || num_neigh_rep == 0 || !wpa_s->current_bss)
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700480 return 0;
481
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800482 wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800483 MAC2STR(wpa_s->bssid), wpa_s->current_bss->level);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800484
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700485 for (i = 0; i < num_neigh_rep; i++) {
486 for (j = 0; j < scan_res->num; j++) {
487 /* Check for a better RSSI AP */
488 if (os_memcmp(scan_res->res[j]->bssid,
489 neigh_rep[i].bssid, ETH_ALEN) == 0 &&
490 scan_res->res[j]->level >
491 wpa_s->current_bss->level) {
492 /* Got a BSSID with better RSSI value */
493 os_memcpy(bssid_to_connect, neigh_rep[i].bssid,
494 ETH_ALEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800495 wpa_printf(MSG_DEBUG, "Found a BSS " MACSTR
496 " with better scan RSSI %d",
497 MAC2STR(scan_res->res[j]->bssid),
498 scan_res->res[j]->level);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700499 return 1;
500 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800501 wpa_printf(MSG_DEBUG, "scan_res[%d] " MACSTR
502 " RSSI %d", j,
503 MAC2STR(scan_res->res[j]->bssid),
504 scan_res->res[j]->level);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700505 }
506 }
507
508 return 0;
509}
510
511
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700512static void wnm_send_bss_transition_mgmt_resp(
513 struct wpa_supplicant *wpa_s, u8 dialog_token,
514 enum bss_trans_mgmt_status_code status, u8 delay,
515 const u8 *target_bssid)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800516{
517 u8 buf[1000], *pos;
518 struct ieee80211_mgmt *mgmt;
519 size_t len;
520
521 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Response "
522 "to " MACSTR " dialog_token=%u status=%u delay=%d",
523 MAC2STR(wpa_s->bssid), dialog_token, status, delay);
524
525 mgmt = (struct ieee80211_mgmt *) buf;
526 os_memset(&buf, 0, sizeof(buf));
527 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
528 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
529 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
530 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
531 WLAN_FC_STYPE_ACTION);
532 mgmt->u.action.category = WLAN_ACTION_WNM;
533 mgmt->u.action.u.bss_tm_resp.action = WNM_BSS_TRANS_MGMT_RESP;
534 mgmt->u.action.u.bss_tm_resp.dialog_token = dialog_token;
535 mgmt->u.action.u.bss_tm_resp.status_code = status;
536 mgmt->u.action.u.bss_tm_resp.bss_termination_delay = delay;
537 pos = mgmt->u.action.u.bss_tm_resp.variable;
538 if (target_bssid) {
539 os_memcpy(pos, target_bssid, ETH_ALEN);
540 pos += ETH_ALEN;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800541 } else if (status == WNM_BSS_TM_ACCEPT) {
542 /*
543 * P802.11-REVmc clarifies that the Target BSSID field is always
544 * present when status code is zero, so use a fake value here if
545 * no BSSID is yet known.
546 */
547 os_memset(pos, 0, ETH_ALEN);
548 pos += ETH_ALEN;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800549 }
550
551 len = pos - (u8 *) &mgmt->u.action.category;
552
553 wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
554 wpa_s->own_addr, wpa_s->bssid,
555 &mgmt->u.action.category, len, 0);
556}
557
558
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700559void wnm_scan_response(struct wpa_supplicant *wpa_s,
560 struct wpa_scan_results *scan_res)
561{
562 u8 bssid[ETH_ALEN];
563
564 if (scan_res == NULL) {
565 wpa_printf(MSG_ERROR, "Scan result is NULL");
566 goto send_bss_resp_fail;
567 }
568
569 /* Compare the Neighbor Report and scan results */
570 if (compare_scan_neighbor_results(wpa_s, scan_res,
571 wpa_s->wnm_neighbor_report_elements,
572 wpa_s->wnm_num_neighbor_report,
573 bssid) == 1) {
574 /* Associate to the network */
575 struct wpa_bss *bss;
576 struct wpa_ssid *ssid = wpa_s->current_ssid;
577
578 bss = wpa_bss_get_bssid(wpa_s, bssid);
579 if (!bss) {
580 wpa_printf(MSG_DEBUG, "WNM: Target AP not found from "
581 "BSS table");
582 goto send_bss_resp_fail;
583 }
584
585 /* Send the BSS Management Response - Accept */
586 if (wpa_s->wnm_reply) {
587 wnm_send_bss_transition_mgmt_resp(wpa_s,
588 wpa_s->wnm_dialog_token,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700589 WNM_BSS_TM_ACCEPT,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800590 0, bssid);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700591 }
592
593 wpa_s->reassociate = 1;
594 wpa_supplicant_connect(wpa_s, bss, ssid);
595 wnm_deallocate_memory(wpa_s);
596 return;
597 }
598
599 /* Send reject response for all the failures */
600send_bss_resp_fail:
601 wnm_deallocate_memory(wpa_s);
602 if (wpa_s->wnm_reply) {
603 wnm_send_bss_transition_mgmt_resp(wpa_s,
604 wpa_s->wnm_dialog_token,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700605 WNM_BSS_TM_REJECT_UNSPECIFIED,
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700606 0, NULL);
607 }
608 return;
609}
610
611
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800612static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
613 const u8 *pos, const u8 *end,
614 int reply)
615{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800616 if (pos + 5 > end)
617 return;
618
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700619 wpa_s->wnm_dialog_token = pos[0];
620 wpa_s->wnm_mode = pos[1];
621 wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
622 wpa_s->wnm_validity_interval = pos[4];
623 wpa_s->wnm_reply = reply;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800624
625 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
626 "dialog_token=%u request_mode=0x%x "
627 "disassoc_timer=%u validity_interval=%u",
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700628 wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
629 wpa_s->wnm_dissoc_timer, wpa_s->wnm_validity_interval);
630
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800631 pos += 5;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700632
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700633 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700634 if (pos + 12 > end) {
635 wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
636 return;
637 }
638 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800639 pos += 12; /* BSS Termination Duration */
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700640 }
641
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700642 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800643 char url[256];
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700644 unsigned int beacon_int;
645
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800646 if (pos + 1 > end || pos + 1 + pos[0] > end) {
647 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
648 "Management Request (URL)");
649 return;
650 }
651 os_memcpy(url, pos + 1, pos[0]);
652 url[pos[0]] = '\0';
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700653 pos += 1 + pos[0];
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700654
655 if (wpa_s->current_bss)
656 beacon_int = wpa_s->current_bss->beacon_int;
657 else
658 beacon_int = 100; /* best guess */
659
660 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
661 wpa_sm_pmf_enabled(wpa_s->wpa),
662 wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800663 }
664
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700665 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800666 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700667 "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
668 if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800669 /* TODO: mark current BSS less preferred for
670 * selection */
671 wpa_printf(MSG_DEBUG, "Trying to find another BSS");
672 wpa_supplicant_req_scan(wpa_s, 0, 0);
673 }
674 }
675
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700676 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700677 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
678 wpa_s->wnm_num_neighbor_report = 0;
679 os_free(wpa_s->wnm_neighbor_report_elements);
680 wpa_s->wnm_neighbor_report_elements = os_zalloc(
681 WNM_MAX_NEIGHBOR_REPORT *
682 sizeof(struct neighbor_report));
683 if (wpa_s->wnm_neighbor_report_elements == NULL)
684 return;
685
686 while (pos + 2 <= end &&
687 wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
688 {
689 u8 tag = *pos++;
690 u8 len = *pos++;
691
692 wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
693 tag);
694 if (pos + len > end) {
695 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
696 return;
697 }
698 wnm_parse_neighbor_report(
699 wpa_s, pos, len,
700 &wpa_s->wnm_neighbor_report_elements[
701 wpa_s->wnm_num_neighbor_report]);
702
703 pos += len;
704 wpa_s->wnm_num_neighbor_report++;
705 }
706
707 wpa_s->scan_res_handler = wnm_scan_response;
708 wpa_supplicant_req_scan(wpa_s, 0, 0);
709 } else if (reply) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700710 enum bss_trans_mgmt_status_code status;
711 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
712 status = WNM_BSS_TM_ACCEPT;
713 else {
714 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
715 status = WNM_BSS_TM_REJECT_UNSPECIFIED;
716 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700717 wnm_send_bss_transition_mgmt_resp(wpa_s,
718 wpa_s->wnm_dialog_token,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700719 status, 0, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800720 }
721}
722
723
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700724int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
725 u8 query_reason)
726{
727 u8 buf[1000], *pos;
728 struct ieee80211_mgmt *mgmt;
729 size_t len;
730 int ret;
731
732 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
733 MACSTR " query_reason=%u",
734 MAC2STR(wpa_s->bssid), query_reason);
735
736 mgmt = (struct ieee80211_mgmt *) buf;
737 os_memset(&buf, 0, sizeof(buf));
738 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
739 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
740 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
741 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
742 WLAN_FC_STYPE_ACTION);
743 mgmt->u.action.category = WLAN_ACTION_WNM;
744 mgmt->u.action.u.bss_tm_query.action = WNM_BSS_TRANS_MGMT_QUERY;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800745 mgmt->u.action.u.bss_tm_query.dialog_token = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700746 mgmt->u.action.u.bss_tm_query.query_reason = query_reason;
747 pos = mgmt->u.action.u.bss_tm_query.variable;
748
749 len = pos - (u8 *) &mgmt->u.action.category;
750
751 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
752 wpa_s->own_addr, wpa_s->bssid,
753 &mgmt->u.action.category, len, 0);
754
755 return ret;
756}
757
758
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800759static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
760 const u8 *sa, const u8 *data,
761 int len)
762{
763 const u8 *pos, *end, *next;
764 u8 ie, ie_len;
765
766 pos = data;
767 end = data + len;
768
769 while (pos + 1 < end) {
770 ie = *pos++;
771 ie_len = *pos++;
772 wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
773 ie, ie_len);
774 if (ie_len > end - pos) {
775 wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
776 "subelement");
777 break;
778 }
779 next = pos + ie_len;
780 if (ie_len < 4) {
781 pos = next;
782 continue;
783 }
784 wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
785 WPA_GET_BE24(pos), pos[3]);
786
787#ifdef CONFIG_HS20
788 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
789 WPA_GET_BE24(pos) == OUI_WFA &&
790 pos[3] == HS20_WNM_SUB_REM_NEEDED) {
791 /* Subscription Remediation subelement */
792 const u8 *ie_end;
793 u8 url_len;
794 char *url;
795 u8 osu_method;
796
797 wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
798 "subelement");
799 ie_end = pos + ie_len;
800 pos += 4;
801 url_len = *pos++;
802 if (url_len == 0) {
803 wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
804 url = NULL;
805 osu_method = 1;
806 } else {
807 if (pos + url_len + 1 > ie_end) {
808 wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
809 url_len,
810 (int) (ie_end - pos));
811 break;
812 }
813 url = os_malloc(url_len + 1);
814 if (url == NULL)
815 break;
816 os_memcpy(url, pos, url_len);
817 url[url_len] = '\0';
818 osu_method = pos[url_len];
819 }
820 hs20_rx_subscription_remediation(wpa_s, url,
821 osu_method);
822 os_free(url);
823 pos = next;
824 continue;
825 }
826
827 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
828 WPA_GET_BE24(pos) == OUI_WFA &&
829 pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
830 const u8 *ie_end;
831 u8 url_len;
832 char *url;
833 u8 code;
834 u16 reauth_delay;
835
836 ie_end = pos + ie_len;
837 pos += 4;
838 code = *pos++;
839 reauth_delay = WPA_GET_LE16(pos);
840 pos += 2;
841 url_len = *pos++;
842 wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
843 "Imminent - Reason Code %u "
844 "Re-Auth Delay %u URL Length %u",
845 code, reauth_delay, url_len);
846 if (pos + url_len > ie_end)
847 break;
848 url = os_malloc(url_len + 1);
849 if (url == NULL)
850 break;
851 os_memcpy(url, pos, url_len);
852 url[url_len] = '\0';
853 hs20_rx_deauth_imminent_notice(wpa_s, code,
854 reauth_delay, url);
855 os_free(url);
856 pos = next;
857 continue;
858 }
859#endif /* CONFIG_HS20 */
860
861 pos = next;
862 }
863}
864
865
866static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
867 const u8 *sa, const u8 *frm, int len)
868{
869 const u8 *pos, *end;
870 u8 dialog_token, type;
871
872 /* Dialog Token [1] | Type [1] | Subelements */
873
874 if (len < 2 || sa == NULL)
875 return;
876 end = frm + len;
877 pos = frm;
878 dialog_token = *pos++;
879 type = *pos++;
880
881 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
882 "(dialog_token %u type %u sa " MACSTR ")",
883 dialog_token, type, MAC2STR(sa));
884 wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
885 pos, end - pos);
886
887 if (wpa_s->wpa_state != WPA_COMPLETED ||
888 os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
889 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
890 "from our AP - ignore it");
891 return;
892 }
893
894 switch (type) {
895 case 1:
896 ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
897 break;
898 default:
899 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
900 "WNM-Notification type %u", type);
901 break;
902 }
903}
904
905
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700906void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800907 const struct ieee80211_mgmt *mgmt, size_t len)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700908{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800909 const u8 *pos, *end;
910 u8 act;
911
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800912 if (len < IEEE80211_HDRLEN + 2)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800913 return;
914
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800915 pos = &mgmt->u.action.category;
916 pos++;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800917 act = *pos++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800918 end = ((const u8 *) mgmt) + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800919
920 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800921 act, MAC2STR(mgmt->sa));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800922 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800923 os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800924 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
925 "frame");
926 return;
927 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700928
929 switch (act) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800930 case WNM_BSS_TRANS_MGMT_REQ:
931 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800932 !(mgmt->da[0] & 0x01));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800933 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700934 case WNM_SLEEP_MODE_RESP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800935 ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700936 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800937 case WNM_NOTIFICATION_REQ:
938 ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
939 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700940 default:
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700941 wpa_printf(MSG_ERROR, "WNM: Unknown request");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700942 break;
943 }
944}