blob: fd033b59d22b5cabbde996c083a1afa75431b322 [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 Shmidt61d9df32012-08-29 16:22:06 -070021
22#define MAX_TFS_IE_LEN 1024
Dmitry Shmidt44c95782013-05-17 09:51:35 -070023#define WNM_MAX_NEIGHBOR_REPORT 10
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070024
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070025
26/* get the TFS IE from driver */
27static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
28 u16 *buf_len, enum wnm_oper oper)
29{
30 wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
31
32 return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
33}
34
35
36/* set the TFS IE to driver */
37static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
38 const u8 *addr, u8 *buf, u16 *buf_len,
39 enum wnm_oper oper)
40{
41 wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
42
43 return wpa_drv_wnm_oper(wpa_s, oper, addr, buf, buf_len);
44}
45
46
47/* MLME-SLEEPMODE.request */
48int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080049 u8 action, u16 intval, struct wpabuf *tfs_req)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070050{
51 struct ieee80211_mgmt *mgmt;
52 int res;
53 size_t len;
54 struct wnm_sleep_element *wnmsleep_ie;
55 u8 *wnmtfs_ie;
56 u8 wnmsleep_ie_len;
57 u16 wnmtfs_ie_len; /* possibly multiple IE(s) */
58 enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
59 WNM_SLEEP_TFS_REQ_IE_NONE;
60
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080061 wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
62 "action=%s to " MACSTR,
63 action == 0 ? "enter" : "exit",
64 MAC2STR(wpa_s->bssid));
65
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070066 /* WNM-Sleep Mode IE */
67 wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
68 wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
69 if (wnmsleep_ie == NULL)
70 return -1;
71 wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
72 wnmsleep_ie->len = wnmsleep_ie_len - 2;
73 wnmsleep_ie->action_type = action;
74 wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080075 wnmsleep_ie->intval = host_to_le16(intval);
76 wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
77 (u8 *) wnmsleep_ie, wnmsleep_ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070078
79 /* TFS IE(s) */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080080 if (tfs_req) {
81 wnmtfs_ie_len = wpabuf_len(tfs_req);
82 wnmtfs_ie = os_malloc(wnmtfs_ie_len);
83 if (wnmtfs_ie == NULL) {
84 os_free(wnmsleep_ie);
85 return -1;
86 }
87 os_memcpy(wnmtfs_ie, wpabuf_head(tfs_req), wnmtfs_ie_len);
88 } else {
89 wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
90 if (wnmtfs_ie == NULL) {
91 os_free(wnmsleep_ie);
92 return -1;
93 }
94 if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
95 tfs_oper)) {
96 wnmtfs_ie_len = 0;
97 os_free(wnmtfs_ie);
98 wnmtfs_ie = NULL;
99 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700100 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800101 wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
102 (u8 *) wnmtfs_ie, wnmtfs_ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700103
104 mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len);
105 if (mgmt == NULL) {
106 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
107 "WNM-Sleep Request action frame");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800108 os_free(wnmsleep_ie);
109 os_free(wnmtfs_ie);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700110 return -1;
111 }
112
113 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
114 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
115 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
116 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
117 WLAN_FC_STYPE_ACTION);
118 mgmt->u.action.category = WLAN_ACTION_WNM;
119 mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800120 mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700121 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
122 wnmsleep_ie_len);
123 /* copy TFS IE here */
124 if (wnmtfs_ie_len > 0) {
125 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
126 wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
127 }
128
129 len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
130 wnmtfs_ie_len;
131
132 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
133 wpa_s->own_addr, wpa_s->bssid,
134 &mgmt->u.action.category, len, 0);
135 if (res < 0)
136 wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
137 "(action=%d, intval=%d)", action, intval);
138
139 os_free(wnmsleep_ie);
140 os_free(wnmtfs_ie);
141 os_free(mgmt);
142
143 return res;
144}
145
146
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800147static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
148 u8 *tfsresp_ie_start,
149 u8 *tfsresp_ie_end)
150{
151 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
152 wpa_s->bssid, NULL, NULL);
153 /* remove GTK/IGTK ?? */
154
155 /* set the TFS Resp IE(s) */
156 if (tfsresp_ie_start && tfsresp_ie_end &&
157 tfsresp_ie_end - tfsresp_ie_start >= 0) {
158 u16 tfsresp_ie_len;
159 tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
160 tfsresp_ie_start;
161 wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
162 /* pass the TFS Resp IE(s) to driver for processing */
163 if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
164 tfsresp_ie_start,
165 &tfsresp_ie_len,
166 WNM_SLEEP_TFS_RESP_IE_SET))
167 wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
168 }
169}
170
171
172static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
173 const u8 *frm, u16 key_len_total)
174{
175 u8 *ptr, *end;
176 u8 gtk_len;
177
178 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM, wpa_s->bssid,
179 NULL, NULL);
180
181 /* Install GTK/IGTK */
182
183 /* point to key data field */
184 ptr = (u8 *) frm + 1 + 1 + 2;
185 end = ptr + key_len_total;
186 wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
187
Jouni Malinen1e9857b2015-10-25 15:45:50 +0200188 if (key_len_total && !wpa_sm_pmf_enabled(wpa_s->wpa)) {
189 wpa_msg(wpa_s, MSG_INFO,
190 "WNM: Ignore Key Data in WNM-Sleep Mode Response - PMF not enabled");
191 return;
192 }
193
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800194 while (ptr + 1 < end) {
195 if (ptr + 2 + ptr[1] > end) {
196 wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
197 "length");
198 if (end > ptr) {
199 wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
200 ptr, end - ptr);
201 }
202 break;
203 }
204 if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
205 if (ptr[1] < 11 + 5) {
206 wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
207 "subelem");
208 break;
209 }
210 gtk_len = *(ptr + 4);
211 if (ptr[1] < 11 + gtk_len ||
212 gtk_len < 5 || gtk_len > 32) {
213 wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
214 "subelem");
215 break;
216 }
217 wpa_wnmsleep_install_key(
218 wpa_s->wpa,
219 WNM_SLEEP_SUBELEM_GTK,
220 ptr);
221 ptr += 13 + gtk_len;
222#ifdef CONFIG_IEEE80211W
223 } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
224 if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
225 wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
226 "subelem");
227 break;
228 }
229 wpa_wnmsleep_install_key(wpa_s->wpa,
230 WNM_SLEEP_SUBELEM_IGTK, ptr);
231 ptr += 10 + WPA_IGTK_LEN;
232#endif /* CONFIG_IEEE80211W */
233 } else
234 break; /* skip the loop */
235 }
236}
237
238
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700239static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
240 const u8 *frm, int len)
241{
242 /*
243 * Action [1] | Diaglog Token [1] | Key Data Len [2] | Key Data |
244 * WNM-Sleep Mode IE | TFS Response IE
245 */
246 u8 *pos = (u8 *) frm; /* point to action field */
247 u16 key_len_total = le_to_host16(*((u16 *)(frm+2)));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700248 struct wnm_sleep_element *wnmsleep_ie = NULL;
249 /* multiple TFS Resp IE (assuming consecutive) */
250 u8 *tfsresp_ie_start = NULL;
251 u8 *tfsresp_ie_end = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700252
253 wpa_printf(MSG_DEBUG, "action=%d token = %d key_len_total = %d",
254 frm[0], frm[1], key_len_total);
255 pos += 4 + key_len_total;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800256 if (pos > frm + len) {
257 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
258 return;
259 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700260 while (pos - frm < len) {
261 u8 ie_len = *(pos + 1);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800262 if (pos + 2 + ie_len > frm + len) {
263 wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
264 break;
265 }
266 wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700267 if (*pos == WLAN_EID_WNMSLEEP)
268 wnmsleep_ie = (struct wnm_sleep_element *) pos;
269 else if (*pos == WLAN_EID_TFS_RESP) {
270 if (!tfsresp_ie_start)
271 tfsresp_ie_start = pos;
272 tfsresp_ie_end = pos;
273 } else
274 wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
275 pos += ie_len + 2;
276 }
277
278 if (!wnmsleep_ie) {
279 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
280 return;
281 }
282
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800283 if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
284 wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700285 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
286 "frame (action=%d, intval=%d)",
287 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800288 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
289 wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
290 tfsresp_ie_end);
291 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
292 wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700293 }
294 } else {
295 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
296 "(action=%d, intval=%d)",
297 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800298 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700299 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
300 wpa_s->bssid, NULL, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800301 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700302 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
303 wpa_s->bssid, NULL, NULL);
304 }
305}
306
307
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700308void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
309{
310 int i;
311
312 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
313 os_free(wpa_s->wnm_neighbor_report_elements[i].tsf_info);
314 os_free(wpa_s->wnm_neighbor_report_elements[i].con_coun_str);
315 os_free(wpa_s->wnm_neighbor_report_elements[i].bss_tran_can);
316 os_free(wpa_s->wnm_neighbor_report_elements[i].bss_term_dur);
317 os_free(wpa_s->wnm_neighbor_report_elements[i].bearing);
318 os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
319 os_free(wpa_s->wnm_neighbor_report_elements[i].rrm_cap);
320 os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
321 }
322
323 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
479 if (scan_res == NULL || num_neigh_rep == 0)
480 return 0;
481
482 for (i = 0; i < num_neigh_rep; i++) {
483 for (j = 0; j < scan_res->num; j++) {
484 /* Check for a better RSSI AP */
485 if (os_memcmp(scan_res->res[j]->bssid,
486 neigh_rep[i].bssid, ETH_ALEN) == 0 &&
487 scan_res->res[j]->level >
488 wpa_s->current_bss->level) {
489 /* Got a BSSID with better RSSI value */
490 os_memcpy(bssid_to_connect, neigh_rep[i].bssid,
491 ETH_ALEN);
492 return 1;
493 }
494 }
495 }
496
497 return 0;
498}
499
500
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700501static void wnm_send_bss_transition_mgmt_resp(
502 struct wpa_supplicant *wpa_s, u8 dialog_token,
503 enum bss_trans_mgmt_status_code status, u8 delay,
504 const u8 *target_bssid)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800505{
506 u8 buf[1000], *pos;
507 struct ieee80211_mgmt *mgmt;
508 size_t len;
509
510 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Response "
511 "to " MACSTR " dialog_token=%u status=%u delay=%d",
512 MAC2STR(wpa_s->bssid), dialog_token, status, delay);
513
514 mgmt = (struct ieee80211_mgmt *) buf;
515 os_memset(&buf, 0, sizeof(buf));
516 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
517 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
518 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
519 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
520 WLAN_FC_STYPE_ACTION);
521 mgmt->u.action.category = WLAN_ACTION_WNM;
522 mgmt->u.action.u.bss_tm_resp.action = WNM_BSS_TRANS_MGMT_RESP;
523 mgmt->u.action.u.bss_tm_resp.dialog_token = dialog_token;
524 mgmt->u.action.u.bss_tm_resp.status_code = status;
525 mgmt->u.action.u.bss_tm_resp.bss_termination_delay = delay;
526 pos = mgmt->u.action.u.bss_tm_resp.variable;
527 if (target_bssid) {
528 os_memcpy(pos, target_bssid, ETH_ALEN);
529 pos += ETH_ALEN;
530 }
531
532 len = pos - (u8 *) &mgmt->u.action.category;
533
534 wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
535 wpa_s->own_addr, wpa_s->bssid,
536 &mgmt->u.action.category, len, 0);
537}
538
539
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700540void wnm_scan_response(struct wpa_supplicant *wpa_s,
541 struct wpa_scan_results *scan_res)
542{
543 u8 bssid[ETH_ALEN];
544
545 if (scan_res == NULL) {
546 wpa_printf(MSG_ERROR, "Scan result is NULL");
547 goto send_bss_resp_fail;
548 }
549
550 /* Compare the Neighbor Report and scan results */
551 if (compare_scan_neighbor_results(wpa_s, scan_res,
552 wpa_s->wnm_neighbor_report_elements,
553 wpa_s->wnm_num_neighbor_report,
554 bssid) == 1) {
555 /* Associate to the network */
556 struct wpa_bss *bss;
557 struct wpa_ssid *ssid = wpa_s->current_ssid;
558
559 bss = wpa_bss_get_bssid(wpa_s, bssid);
560 if (!bss) {
561 wpa_printf(MSG_DEBUG, "WNM: Target AP not found from "
562 "BSS table");
563 goto send_bss_resp_fail;
564 }
565
566 /* Send the BSS Management Response - Accept */
567 if (wpa_s->wnm_reply) {
568 wnm_send_bss_transition_mgmt_resp(wpa_s,
569 wpa_s->wnm_dialog_token,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700570 WNM_BSS_TM_ACCEPT,
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700571 0, NULL);
572 }
573
574 wpa_s->reassociate = 1;
575 wpa_supplicant_connect(wpa_s, bss, ssid);
576 wnm_deallocate_memory(wpa_s);
577 return;
578 }
579
580 /* Send reject response for all the failures */
581send_bss_resp_fail:
582 wnm_deallocate_memory(wpa_s);
583 if (wpa_s->wnm_reply) {
584 wnm_send_bss_transition_mgmt_resp(wpa_s,
585 wpa_s->wnm_dialog_token,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700586 WNM_BSS_TM_REJECT_UNSPECIFIED,
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700587 0, NULL);
588 }
589 return;
590}
591
592
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800593static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
594 const u8 *pos, const u8 *end,
595 int reply)
596{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800597 if (pos + 5 > end)
598 return;
599
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700600 wpa_s->wnm_dialog_token = pos[0];
601 wpa_s->wnm_mode = pos[1];
602 wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
603 wpa_s->wnm_validity_interval = pos[4];
604 wpa_s->wnm_reply = reply;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800605
606 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
607 "dialog_token=%u request_mode=0x%x "
608 "disassoc_timer=%u validity_interval=%u",
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700609 wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
610 wpa_s->wnm_dissoc_timer, wpa_s->wnm_validity_interval);
611
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800612 pos += 5;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700613
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700614 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700615 if (pos + 12 > end) {
616 wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
617 return;
618 }
619 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800620 pos += 12; /* BSS Termination Duration */
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700621 }
622
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700623 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800624 char url[256];
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700625 unsigned int beacon_int;
626
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800627 if (pos + 1 > end || pos + 1 + pos[0] > end) {
628 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
629 "Management Request (URL)");
630 return;
631 }
632 os_memcpy(url, pos + 1, pos[0]);
633 url[pos[0]] = '\0';
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700634 pos += 1 + pos[0];
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700635
636 if (wpa_s->current_bss)
637 beacon_int = wpa_s->current_bss->beacon_int;
638 else
639 beacon_int = 100; /* best guess */
640
641 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
642 wpa_sm_pmf_enabled(wpa_s->wpa),
643 wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800644 }
645
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700646 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800647 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700648 "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
649 if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800650 /* TODO: mark current BSS less preferred for
651 * selection */
652 wpa_printf(MSG_DEBUG, "Trying to find another BSS");
653 wpa_supplicant_req_scan(wpa_s, 0, 0);
654 }
655 }
656
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700657 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700658 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
659 wpa_s->wnm_num_neighbor_report = 0;
660 os_free(wpa_s->wnm_neighbor_report_elements);
661 wpa_s->wnm_neighbor_report_elements = os_zalloc(
662 WNM_MAX_NEIGHBOR_REPORT *
663 sizeof(struct neighbor_report));
664 if (wpa_s->wnm_neighbor_report_elements == NULL)
665 return;
666
667 while (pos + 2 <= end &&
668 wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
669 {
670 u8 tag = *pos++;
671 u8 len = *pos++;
672
673 wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
674 tag);
675 if (pos + len > end) {
676 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
677 return;
678 }
679 wnm_parse_neighbor_report(
680 wpa_s, pos, len,
681 &wpa_s->wnm_neighbor_report_elements[
682 wpa_s->wnm_num_neighbor_report]);
683
684 pos += len;
685 wpa_s->wnm_num_neighbor_report++;
686 }
687
688 wpa_s->scan_res_handler = wnm_scan_response;
689 wpa_supplicant_req_scan(wpa_s, 0, 0);
690 } else if (reply) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700691 enum bss_trans_mgmt_status_code status;
692 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
693 status = WNM_BSS_TM_ACCEPT;
694 else {
695 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
696 status = WNM_BSS_TM_REJECT_UNSPECIFIED;
697 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700698 wnm_send_bss_transition_mgmt_resp(wpa_s,
699 wpa_s->wnm_dialog_token,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700700 status, 0, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800701 }
702}
703
704
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700705int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
706 u8 query_reason)
707{
708 u8 buf[1000], *pos;
709 struct ieee80211_mgmt *mgmt;
710 size_t len;
711 int ret;
712
713 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
714 MACSTR " query_reason=%u",
715 MAC2STR(wpa_s->bssid), query_reason);
716
717 mgmt = (struct ieee80211_mgmt *) buf;
718 os_memset(&buf, 0, sizeof(buf));
719 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
720 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
721 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
722 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
723 WLAN_FC_STYPE_ACTION);
724 mgmt->u.action.category = WLAN_ACTION_WNM;
725 mgmt->u.action.u.bss_tm_query.action = WNM_BSS_TRANS_MGMT_QUERY;
726 mgmt->u.action.u.bss_tm_query.dialog_token = 0;
727 mgmt->u.action.u.bss_tm_query.query_reason = query_reason;
728 pos = mgmt->u.action.u.bss_tm_query.variable;
729
730 len = pos - (u8 *) &mgmt->u.action.category;
731
732 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
733 wpa_s->own_addr, wpa_s->bssid,
734 &mgmt->u.action.category, len, 0);
735
736 return ret;
737}
738
739
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700740void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
741 struct rx_action *action)
742{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800743 const u8 *pos, *end;
744 u8 act;
745
746 if (action->data == NULL || action->len == 0)
747 return;
748
749 pos = action->data;
750 end = pos + action->len;
751 act = *pos++;
752
753 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
754 act, MAC2STR(action->sa));
755 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
756 os_memcmp(action->sa, wpa_s->bssid, ETH_ALEN) != 0) {
757 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
758 "frame");
759 return;
760 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700761
762 switch (act) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800763 case WNM_BSS_TRANS_MGMT_REQ:
764 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
765 !(action->da[0] & 0x01));
766 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700767 case WNM_SLEEP_MODE_RESP:
768 ieee802_11_rx_wnmsleep_resp(wpa_s, action->data, action->len);
769 break;
770 default:
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700771 wpa_printf(MSG_ERROR, "WNM: Unknown request");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700772 break;
773 }
774}