blob: ffc9052f4ec306683d8fcb64a082cc1e2d6de571 [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 Shmidtfb45fd52015-01-05 13:08:17 -080013#include "common/ieee802_11_common.h"
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -070014#include "common/wpa_ctrl.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070015#include "rsn_supp/wpa.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080016#include "wpa_supplicant_i.h"
17#include "driver_i.h"
18#include "scan.h"
Dmitry Shmidt44c95782013-05-17 09:51:35 -070019#include "ctrl_iface.h"
20#include "bss.h"
21#include "wnm_sta.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080022#include "hs20_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070023
24#define MAX_TFS_IE_LEN 1024
Dmitry Shmidt44c95782013-05-17 09:51:35 -070025#define WNM_MAX_NEIGHBOR_REPORT 10
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070026
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070027
28/* get the TFS IE from driver */
29static int ieee80211_11_get_tfs_ie(struct wpa_supplicant *wpa_s, u8 *buf,
30 u16 *buf_len, enum wnm_oper oper)
31{
32 wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
33
34 return wpa_drv_wnm_oper(wpa_s, oper, wpa_s->bssid, buf, buf_len);
35}
36
37
38/* set the TFS IE to driver */
39static int ieee80211_11_set_tfs_ie(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080040 const u8 *addr, const u8 *buf, u16 buf_len,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070041 enum wnm_oper oper)
42{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080043 u16 len = buf_len;
44
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070045 wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
46
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080047 return wpa_drv_wnm_oper(wpa_s, oper, addr, (u8 *) buf, &len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070048}
49
50
51/* MLME-SLEEPMODE.request */
52int ieee802_11_send_wnmsleep_req(struct wpa_supplicant *wpa_s,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080053 u8 action, u16 intval, struct wpabuf *tfs_req)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070054{
55 struct ieee80211_mgmt *mgmt;
56 int res;
57 size_t len;
58 struct wnm_sleep_element *wnmsleep_ie;
59 u8 *wnmtfs_ie;
60 u8 wnmsleep_ie_len;
61 u16 wnmtfs_ie_len; /* possibly multiple IE(s) */
62 enum wnm_oper tfs_oper = action == 0 ? WNM_SLEEP_TFS_REQ_IE_ADD :
63 WNM_SLEEP_TFS_REQ_IE_NONE;
64
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080065 wpa_printf(MSG_DEBUG, "WNM: Request to send WNM-Sleep Mode Request "
66 "action=%s to " MACSTR,
67 action == 0 ? "enter" : "exit",
68 MAC2STR(wpa_s->bssid));
69
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070070 /* WNM-Sleep Mode IE */
71 wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
72 wnmsleep_ie = os_zalloc(sizeof(struct wnm_sleep_element));
73 if (wnmsleep_ie == NULL)
74 return -1;
75 wnmsleep_ie->eid = WLAN_EID_WNMSLEEP;
76 wnmsleep_ie->len = wnmsleep_ie_len - 2;
77 wnmsleep_ie->action_type = action;
78 wnmsleep_ie->status = WNM_STATUS_SLEEP_ACCEPT;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080079 wnmsleep_ie->intval = host_to_le16(intval);
80 wpa_hexdump(MSG_DEBUG, "WNM: WNM-Sleep Mode element",
81 (u8 *) wnmsleep_ie, wnmsleep_ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070082
83 /* TFS IE(s) */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080084 if (tfs_req) {
85 wnmtfs_ie_len = wpabuf_len(tfs_req);
86 wnmtfs_ie = os_malloc(wnmtfs_ie_len);
87 if (wnmtfs_ie == NULL) {
88 os_free(wnmsleep_ie);
89 return -1;
90 }
91 os_memcpy(wnmtfs_ie, wpabuf_head(tfs_req), wnmtfs_ie_len);
92 } else {
93 wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
94 if (wnmtfs_ie == NULL) {
95 os_free(wnmsleep_ie);
96 return -1;
97 }
98 if (ieee80211_11_get_tfs_ie(wpa_s, wnmtfs_ie, &wnmtfs_ie_len,
99 tfs_oper)) {
100 wnmtfs_ie_len = 0;
101 os_free(wnmtfs_ie);
102 wnmtfs_ie = NULL;
103 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700104 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800105 wpa_hexdump(MSG_DEBUG, "WNM: TFS Request element",
106 (u8 *) wnmtfs_ie, wnmtfs_ie_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700107
108 mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len + wnmtfs_ie_len);
109 if (mgmt == NULL) {
110 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
111 "WNM-Sleep Request action frame");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800112 os_free(wnmsleep_ie);
113 os_free(wnmtfs_ie);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700114 return -1;
115 }
116
117 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
118 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
119 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
120 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
121 WLAN_FC_STYPE_ACTION);
122 mgmt->u.action.category = WLAN_ACTION_WNM;
123 mgmt->u.action.u.wnm_sleep_req.action = WNM_SLEEP_MODE_REQ;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800124 mgmt->u.action.u.wnm_sleep_req.dialogtoken = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700125 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable, wnmsleep_ie,
126 wnmsleep_ie_len);
127 /* copy TFS IE here */
128 if (wnmtfs_ie_len > 0) {
129 os_memcpy(mgmt->u.action.u.wnm_sleep_req.variable +
130 wnmsleep_ie_len, wnmtfs_ie, wnmtfs_ie_len);
131 }
132
133 len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_req) + wnmsleep_ie_len +
134 wnmtfs_ie_len;
135
136 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
137 wpa_s->own_addr, wpa_s->bssid,
138 &mgmt->u.action.category, len, 0);
139 if (res < 0)
140 wpa_printf(MSG_DEBUG, "Failed to send WNM-Sleep Request "
141 "(action=%d, intval=%d)", action, intval);
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800142 else
143 wpa_s->wnmsleep_used = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700144
145 os_free(wnmsleep_ie);
146 os_free(wnmtfs_ie);
147 os_free(mgmt);
148
149 return res;
150}
151
152
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800153static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800154 const u8 *tfsresp_ie_start,
155 const u8 *tfsresp_ie_end)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800156{
157 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
158 wpa_s->bssid, NULL, NULL);
159 /* remove GTK/IGTK ?? */
160
161 /* set the TFS Resp IE(s) */
162 if (tfsresp_ie_start && tfsresp_ie_end &&
163 tfsresp_ie_end - tfsresp_ie_start >= 0) {
164 u16 tfsresp_ie_len;
165 tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
166 tfsresp_ie_start;
167 wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
168 /* pass the TFS Resp IE(s) to driver for processing */
169 if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
170 tfsresp_ie_start,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800171 tfsresp_ie_len,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800172 WNM_SLEEP_TFS_RESP_IE_SET))
173 wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
174 }
175}
176
177
178static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
179 const u8 *frm, u16 key_len_total)
180{
181 u8 *ptr, *end;
182 u8 gtk_len;
183
184 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM, wpa_s->bssid,
185 NULL, NULL);
186
187 /* Install GTK/IGTK */
188
189 /* point to key data field */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800190 ptr = (u8 *) frm + 1 + 2;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800191 end = ptr + key_len_total;
192 wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
193
Jouni Malinen17de68d2015-11-09 15:25:41 -0800194 if (key_len_total && !wpa_sm_pmf_enabled(wpa_s->wpa)) {
195 wpa_msg(wpa_s, MSG_INFO,
196 "WNM: Ignore Key Data in WNM-Sleep Mode Response - PMF not enabled");
197 return;
198 }
199
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800200 while (end - ptr > 1) {
201 if (2 + ptr[1] > end - ptr) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800202 wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
203 "length");
204 if (end > ptr) {
205 wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
206 ptr, end - ptr);
207 }
208 break;
209 }
210 if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
211 if (ptr[1] < 11 + 5) {
212 wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
213 "subelem");
214 break;
215 }
216 gtk_len = *(ptr + 4);
217 if (ptr[1] < 11 + gtk_len ||
218 gtk_len < 5 || gtk_len > 32) {
219 wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
220 "subelem");
221 break;
222 }
223 wpa_wnmsleep_install_key(
224 wpa_s->wpa,
225 WNM_SLEEP_SUBELEM_GTK,
226 ptr);
227 ptr += 13 + gtk_len;
228#ifdef CONFIG_IEEE80211W
229 } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
230 if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
231 wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
232 "subelem");
233 break;
234 }
235 wpa_wnmsleep_install_key(wpa_s->wpa,
236 WNM_SLEEP_SUBELEM_IGTK, ptr);
237 ptr += 10 + WPA_IGTK_LEN;
238#endif /* CONFIG_IEEE80211W */
239 } else
240 break; /* skip the loop */
241 }
242}
243
244
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700245static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
246 const u8 *frm, int len)
247{
248 /*
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700249 * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700250 * WNM-Sleep Mode IE | TFS Response IE
251 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800252 const u8 *pos = frm; /* point to payload after the action field */
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700253 u16 key_len_total;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700254 struct wnm_sleep_element *wnmsleep_ie = NULL;
255 /* multiple TFS Resp IE (assuming consecutive) */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800256 const u8 *tfsresp_ie_start = NULL;
257 const u8 *tfsresp_ie_end = NULL;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800258 size_t left;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700259
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800260 if (!wpa_s->wnmsleep_used) {
261 wpa_printf(MSG_DEBUG,
262 "WNM: Ignore WNM-Sleep Mode Response frame since WNM-Sleep Mode has not been used in this association");
263 return;
264 }
265
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700266 if (len < 3)
267 return;
268 key_len_total = WPA_GET_LE16(frm + 1);
269
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800270 wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
271 frm[0], key_len_total);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800272 left = len - 3;
273 if (key_len_total > left) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800274 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
275 return;
276 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800277 pos += 3 + key_len_total;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800278 while (pos - frm + 1 < len) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700279 u8 ie_len = *(pos + 1);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800280 if (2 + ie_len > frm + len - pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800281 wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
282 break;
283 }
284 wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800285 if (*pos == WLAN_EID_WNMSLEEP && ie_len >= 4)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700286 wnmsleep_ie = (struct wnm_sleep_element *) pos;
287 else if (*pos == WLAN_EID_TFS_RESP) {
288 if (!tfsresp_ie_start)
289 tfsresp_ie_start = pos;
290 tfsresp_ie_end = pos;
291 } else
292 wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
293 pos += ie_len + 2;
294 }
295
296 if (!wnmsleep_ie) {
297 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
298 return;
299 }
300
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800301 if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
302 wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700303 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
304 "frame (action=%d, intval=%d)",
305 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800306 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER) {
307 wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
308 tfsresp_ie_end);
309 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
310 wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700311 }
312 } else {
313 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
314 "(action=%d, intval=%d)",
315 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800316 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700317 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
318 wpa_s->bssid, NULL, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800319 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700320 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
321 wpa_s->bssid, NULL, NULL);
322 }
323}
324
325
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700326void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
327{
328 int i;
329
330 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700331 os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700332 os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
333 }
334
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700335 wpa_s->wnm_num_neighbor_report = 0;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700336 os_free(wpa_s->wnm_neighbor_report_elements);
337 wpa_s->wnm_neighbor_report_elements = NULL;
338}
339
340
341static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
342 u8 id, u8 elen, const u8 *pos)
343{
344 switch (id) {
345 case WNM_NEIGHBOR_TSF:
346 if (elen < 2 + 2) {
347 wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
348 break;
349 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800350 rep->tsf_offset = WPA_GET_LE16(pos);
351 rep->beacon_int = WPA_GET_LE16(pos + 2);
352 rep->tsf_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700353 break;
354 case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
355 if (elen < 2) {
356 wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
357 "country string");
358 break;
359 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800360 os_memcpy(rep->country, pos, 2);
361 rep->country_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700362 break;
363 case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
364 if (elen < 1) {
365 wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
366 "candidate");
367 break;
368 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800369 rep->preference = pos[0];
370 rep->preference_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700371 break;
372 case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800373 rep->bss_term_tsf = WPA_GET_LE64(pos);
374 rep->bss_term_dur = WPA_GET_LE16(pos + 8);
375 rep->bss_term_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700376 break;
377 case WNM_NEIGHBOR_BEARING:
378 if (elen < 8) {
379 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
380 "bearing");
381 break;
382 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800383 rep->bearing = WPA_GET_LE16(pos);
384 rep->distance = WPA_GET_LE32(pos + 2);
385 rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
386 rep->bearing_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700387 break;
388 case WNM_NEIGHBOR_MEASUREMENT_PILOT:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700389 if (elen < 1) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700390 wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
391 "pilot");
392 break;
393 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700394 os_free(rep->meas_pilot);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700395 rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
396 if (rep->meas_pilot == NULL)
397 break;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700398 rep->meas_pilot->measurement_pilot = pos[0];
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700399 rep->meas_pilot->subelem_len = elen - 1;
400 os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700401 break;
402 case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700403 if (elen < 5) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700404 wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
405 "capabilities");
406 break;
407 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800408 os_memcpy(rep->rm_capab, pos, 5);
409 rep->rm_capab_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700410 break;
411 case WNM_NEIGHBOR_MULTIPLE_BSSID:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700412 if (elen < 1) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700413 wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
414 break;
415 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700416 os_free(rep->mul_bssid);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700417 rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
418 if (rep->mul_bssid == NULL)
419 break;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700420 rep->mul_bssid->max_bssid_indicator = pos[0];
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700421 rep->mul_bssid->subelem_len = elen - 1;
422 os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700423 break;
424 }
425}
426
427
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800428static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
429{
430 struct wpa_bss *bss = wpa_s->current_bss;
431 const char *country = NULL;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800432 int freq;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800433
434 if (bss) {
435 const u8 *elem = wpa_bss_get_ie(bss, WLAN_EID_COUNTRY);
436
437 if (elem && elem[1] >= 2)
438 country = (const char *) (elem + 2);
439 }
440
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800441 freq = ieee80211_chan_to_freq(country, op_class, chan);
442 if (freq <= 0 && op_class == 0) {
443 /*
444 * Some APs do not advertise correct operating class
445 * information. Try to determine the most likely operating
446 * frequency based on the channel number.
447 */
448 if (chan >= 1 && chan <= 13)
449 freq = 2407 + chan * 5;
450 else if (chan == 14)
451 freq = 2484;
452 else if (chan >= 36 && chan <= 169)
453 freq = 5000 + chan * 5;
454 }
455 return freq;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800456}
457
458
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700459static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
460 const u8 *pos, u8 len,
461 struct neighbor_report *rep)
462{
463 u8 left = len;
464
465 if (left < 13) {
466 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
467 return;
468 }
469
470 os_memcpy(rep->bssid, pos, ETH_ALEN);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800471 rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700472 rep->regulatory_class = *(pos + 10);
473 rep->channel_number = *(pos + 11);
474 rep->phy_type = *(pos + 12);
475
476 pos += 13;
477 left -= 13;
478
479 while (left >= 2) {
480 u8 id, elen;
481
482 id = *pos++;
483 elen = *pos++;
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700484 wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
485 left -= 2;
486 if (elen > left) {
487 wpa_printf(MSG_DEBUG,
488 "WNM: Truncated neighbor report subelement");
489 break;
490 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700491 wnm_parse_neighbor_report_elem(rep, id, elen, pos);
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700492 left -= elen;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700493 pos += elen;
494 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800495
496 rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
497 rep->channel_number);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700498}
499
500
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800501static struct wpa_bss *
502compare_scan_neighbor_results(struct wpa_supplicant *wpa_s)
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700503{
504
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800505 u8 i;
506 struct wpa_bss *bss = wpa_s->current_bss;
507 struct wpa_bss *target;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700508
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800509 if (!bss)
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700510 return 0;
511
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800512 wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800513 MAC2STR(wpa_s->bssid), bss->level);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800514
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800515 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
516 struct neighbor_report *nei;
517
518 nei = &wpa_s->wnm_neighbor_report_elements[i];
519 if (nei->preference_present && nei->preference == 0) {
520 wpa_printf(MSG_DEBUG, "Skip excluded BSS " MACSTR,
521 MAC2STR(nei->bssid));
522 continue;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700523 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800524
525 target = wpa_bss_get_bssid(wpa_s, nei->bssid);
526 if (!target) {
527 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
528 " (pref %d) not found in scan results",
529 MAC2STR(nei->bssid),
530 nei->preference_present ? nei->preference :
531 -1);
532 continue;
533 }
534
535 if (bss->ssid_len != target->ssid_len ||
536 os_memcmp(bss->ssid, target->ssid, bss->ssid_len) != 0) {
537 /*
538 * TODO: Could consider allowing transition to another
539 * ESS if PMF was enabled for the association.
540 */
541 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
542 " (pref %d) in different ESS",
543 MAC2STR(nei->bssid),
544 nei->preference_present ? nei->preference :
545 -1);
546 continue;
547 }
548
549 if (target->level < bss->level && target->level < -80) {
550 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
551 " (pref %d) does not have sufficient signal level (%d)",
552 MAC2STR(nei->bssid),
553 nei->preference_present ? nei->preference :
554 -1,
555 target->level);
556 continue;
557 }
558
559 wpa_printf(MSG_DEBUG,
560 "WNM: Found an acceptable preferred transition candidate BSS "
561 MACSTR " (RSSI %d)",
562 MAC2STR(nei->bssid), target->level);
563 return target;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700564 }
565
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800566 return NULL;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700567}
568
569
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700570static void wnm_send_bss_transition_mgmt_resp(
571 struct wpa_supplicant *wpa_s, u8 dialog_token,
572 enum bss_trans_mgmt_status_code status, u8 delay,
573 const u8 *target_bssid)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800574{
575 u8 buf[1000], *pos;
576 struct ieee80211_mgmt *mgmt;
577 size_t len;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800578 int res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800579
580 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Response "
581 "to " MACSTR " dialog_token=%u status=%u delay=%d",
582 MAC2STR(wpa_s->bssid), dialog_token, status, delay);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800583 if (!wpa_s->current_bss) {
584 wpa_printf(MSG_DEBUG,
585 "WNM: Current BSS not known - drop response");
586 return;
587 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800588
589 mgmt = (struct ieee80211_mgmt *) buf;
590 os_memset(&buf, 0, sizeof(buf));
591 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
592 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
593 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
594 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
595 WLAN_FC_STYPE_ACTION);
596 mgmt->u.action.category = WLAN_ACTION_WNM;
597 mgmt->u.action.u.bss_tm_resp.action = WNM_BSS_TRANS_MGMT_RESP;
598 mgmt->u.action.u.bss_tm_resp.dialog_token = dialog_token;
599 mgmt->u.action.u.bss_tm_resp.status_code = status;
600 mgmt->u.action.u.bss_tm_resp.bss_termination_delay = delay;
601 pos = mgmt->u.action.u.bss_tm_resp.variable;
602 if (target_bssid) {
603 os_memcpy(pos, target_bssid, ETH_ALEN);
604 pos += ETH_ALEN;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800605 } else if (status == WNM_BSS_TM_ACCEPT) {
606 /*
607 * P802.11-REVmc clarifies that the Target BSSID field is always
608 * present when status code is zero, so use a fake value here if
609 * no BSSID is yet known.
610 */
611 os_memset(pos, 0, ETH_ALEN);
612 pos += ETH_ALEN;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800613 }
614
615 len = pos - (u8 *) &mgmt->u.action.category;
616
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800617 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
618 wpa_s->own_addr, wpa_s->bssid,
619 &mgmt->u.action.category, len, 0);
620 if (res < 0) {
621 wpa_printf(MSG_DEBUG,
622 "WNM: Failed to send BSS Transition Management Response");
623 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800624}
625
626
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800627int wnm_scan_process(struct wpa_supplicant *wpa_s, int reply_on_fail)
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700628{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800629 struct wpa_bss *bss;
630 struct wpa_ssid *ssid = wpa_s->current_ssid;
631 enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700632
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800633 if (!wpa_s->wnm_neighbor_report_elements)
634 return 0;
635
636 if (os_reltime_before(&wpa_s->wnm_cand_valid_until,
637 &wpa_s->scan_trigger_time)) {
638 wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
639 wnm_deallocate_memory(wpa_s);
640 return 0;
641 }
642
643 if (!wpa_s->current_bss ||
644 os_memcmp(wpa_s->wnm_cand_from_bss, wpa_s->current_bss->bssid,
645 ETH_ALEN) != 0) {
646 wpa_printf(MSG_DEBUG, "WNM: Stored BSS transition candidate list not from the current BSS - ignore it");
647 return 0;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700648 }
649
650 /* Compare the Neighbor Report and scan results */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800651 bss = compare_scan_neighbor_results(wpa_s);
652 if (!bss) {
653 wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
654 status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
655 goto send_bss_resp_fail;
656 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700657
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800658 /* Associate to the network */
659 /* Send the BSS Management Response - Accept */
660 if (wpa_s->wnm_reply) {
661 wpa_s->wnm_reply = 0;
662 wnm_send_bss_transition_mgmt_resp(wpa_s,
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700663 wpa_s->wnm_dialog_token,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700664 WNM_BSS_TM_ACCEPT,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800665 0, bss->bssid);
666 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700667
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800668 if (bss == wpa_s->current_bss) {
669 wpa_printf(MSG_DEBUG,
670 "WNM: Already associated with the preferred candidate");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800671 wnm_deallocate_memory(wpa_s);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800672 return 1;
673 }
674
675 wpa_s->reassociate = 1;
676 wpa_supplicant_connect(wpa_s, bss, ssid);
677 wnm_deallocate_memory(wpa_s);
678 return 1;
679
680send_bss_resp_fail:
681 if (!reply_on_fail)
682 return 0;
683
684 /* Send reject response for all the failures */
685
686 if (wpa_s->wnm_reply) {
687 wpa_s->wnm_reply = 0;
688 wnm_send_bss_transition_mgmt_resp(wpa_s,
689 wpa_s->wnm_dialog_token,
690 status, 0, NULL);
691 }
692 wnm_deallocate_memory(wpa_s);
693
694 return 0;
695}
696
697
698static int cand_pref_compar(const void *a, const void *b)
699{
700 const struct neighbor_report *aa = a;
701 const struct neighbor_report *bb = b;
702
703 if (!aa->preference_present && !bb->preference_present)
704 return 0;
705 if (!aa->preference_present)
706 return 1;
707 if (!bb->preference_present)
708 return -1;
709 if (bb->preference > aa->preference)
710 return 1;
711 if (bb->preference < aa->preference)
712 return -1;
713 return 0;
714}
715
716
717static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
718{
719 if (!wpa_s->wnm_neighbor_report_elements)
720 return;
721 qsort(wpa_s->wnm_neighbor_report_elements,
722 wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
723 cand_pref_compar);
724}
725
726
727static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
728{
729 unsigned int i;
730
731 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
732 if (!wpa_s->wnm_neighbor_report_elements)
733 return;
734 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
735 struct neighbor_report *nei;
736
737 nei = &wpa_s->wnm_neighbor_report_elements[i];
738 wpa_printf(MSG_DEBUG, "%u: " MACSTR
739 " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
740 i, MAC2STR(nei->bssid), nei->bssid_info,
741 nei->regulatory_class,
742 nei->channel_number, nei->phy_type,
743 nei->preference_present ? nei->preference : -1,
744 nei->freq);
745 }
746}
747
748
749static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
750{
751 unsigned int i;
752
753 for (i = 0; i < wpa_s->hw.num_modes; i++) {
754 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
755 int j;
756
757 for (j = 0; j < mode->num_channels; j++) {
758 struct hostapd_channel_data *chan;
759
760 chan = &mode->channels[j];
761 if (chan->freq == freq &&
762 !(chan->flag & HOSTAPD_CHAN_DISABLED))
763 return 1;
764 }
765 }
766
767 return 0;
768}
769
770
771static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
772{
773 int *freqs;
774 int num_freqs = 0;
775 unsigned int i;
776
777 if (!wpa_s->wnm_neighbor_report_elements)
778 return;
779
780 if (wpa_s->hw.modes == NULL)
781 return;
782
783 os_free(wpa_s->next_scan_freqs);
784 wpa_s->next_scan_freqs = NULL;
785
786 freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
787 if (freqs == NULL)
788 return;
789
790 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
791 struct neighbor_report *nei;
792
793 nei = &wpa_s->wnm_neighbor_report_elements[i];
794 if (nei->freq <= 0) {
795 wpa_printf(MSG_DEBUG,
796 "WNM: Unknown neighbor operating frequency for "
797 MACSTR " - scan all channels",
798 MAC2STR(nei->bssid));
799 os_free(freqs);
800 return;
801 }
802 if (chan_supported(wpa_s, nei->freq))
803 add_freq(freqs, &num_freqs, nei->freq);
804 }
805
806 if (num_freqs == 0) {
807 os_free(freqs);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700808 return;
809 }
810
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800811 wpa_printf(MSG_DEBUG,
812 "WNM: Scan %d frequencies based on transition candidate list",
813 num_freqs);
814 wpa_s->next_scan_freqs = freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700815}
816
817
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800818static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
819 const u8 *pos, const u8 *end,
820 int reply)
821{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800822 unsigned int beacon_int;
823 u8 valid_int;
824
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800825 if (end - pos < 5)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800826 return;
827
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800828 if (wpa_s->current_bss)
829 beacon_int = wpa_s->current_bss->beacon_int;
830 else
831 beacon_int = 100; /* best guess */
832
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700833 wpa_s->wnm_dialog_token = pos[0];
834 wpa_s->wnm_mode = pos[1];
835 wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800836 valid_int = pos[4];
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700837 wpa_s->wnm_reply = reply;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800838
839 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
840 "dialog_token=%u request_mode=0x%x "
841 "disassoc_timer=%u validity_interval=%u",
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700842 wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800843 wpa_s->wnm_dissoc_timer, valid_int);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700844
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800845 pos += 5;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700846
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700847 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800848 if (end - pos < 12) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700849 wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
850 return;
851 }
852 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800853 pos += 12; /* BSS Termination Duration */
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700854 }
855
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700856 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800857 char url[256];
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700858
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800859 if (end - pos < 1 || 1 + pos[0] > end - pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800860 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
861 "Management Request (URL)");
862 return;
863 }
864 os_memcpy(url, pos + 1, pos[0]);
865 url[pos[0]] = '\0';
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700866 pos += 1 + pos[0];
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700867
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700868 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
869 wpa_sm_pmf_enabled(wpa_s->wpa),
870 wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800871 }
872
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700873 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800874 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700875 "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
876 if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800877 /* TODO: mark current BSS less preferred for
878 * selection */
879 wpa_printf(MSG_DEBUG, "Trying to find another BSS");
880 wpa_supplicant_req_scan(wpa_s, 0, 0);
881 }
882 }
883
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700884 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800885 unsigned int valid_ms;
886
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700887 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800888 wnm_deallocate_memory(wpa_s);
889 wpa_s->wnm_neighbor_report_elements = os_calloc(
890 WNM_MAX_NEIGHBOR_REPORT,
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700891 sizeof(struct neighbor_report));
892 if (wpa_s->wnm_neighbor_report_elements == NULL)
893 return;
894
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800895 while (end - pos >= 2 &&
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700896 wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
897 {
898 u8 tag = *pos++;
899 u8 len = *pos++;
900
901 wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
902 tag);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800903 if (len > end - pos) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700904 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
905 return;
906 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700907 if (tag == WLAN_EID_NEIGHBOR_REPORT) {
908 struct neighbor_report *rep;
909 rep = &wpa_s->wnm_neighbor_report_elements[
910 wpa_s->wnm_num_neighbor_report];
911 wnm_parse_neighbor_report(wpa_s, pos, len, rep);
912 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700913
914 pos += len;
915 wpa_s->wnm_num_neighbor_report++;
916 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800917 wnm_sort_cand_list(wpa_s);
918 wnm_dump_cand_list(wpa_s);
919 valid_ms = valid_int * beacon_int * 128 / 125;
920 wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
921 valid_ms);
922 os_get_reltime(&wpa_s->wnm_cand_valid_until);
923 wpa_s->wnm_cand_valid_until.sec += valid_ms / 1000;
924 wpa_s->wnm_cand_valid_until.usec += (valid_ms % 1000) * 1000;
925 wpa_s->wnm_cand_valid_until.sec +=
926 wpa_s->wnm_cand_valid_until.usec / 1000000;
927 wpa_s->wnm_cand_valid_until.usec %= 1000000;
928 os_memcpy(wpa_s->wnm_cand_from_bss, wpa_s->bssid, ETH_ALEN);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700929
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800930 if (wpa_s->last_scan_res_used > 0) {
931 struct os_reltime now;
932
933 os_get_reltime(&now);
934 if (!os_reltime_expired(&now, &wpa_s->last_scan, 10)) {
935 wpa_printf(MSG_DEBUG,
936 "WNM: Try to use recent scan results");
937 if (wnm_scan_process(wpa_s, 0) > 0)
938 return;
939 wpa_printf(MSG_DEBUG,
940 "WNM: No match in previous scan results - try a new scan");
941 }
942 }
943
944 wnm_set_scan_freqs(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700945 wpa_supplicant_req_scan(wpa_s, 0, 0);
946 } else if (reply) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700947 enum bss_trans_mgmt_status_code status;
948 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
949 status = WNM_BSS_TM_ACCEPT;
950 else {
951 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
952 status = WNM_BSS_TM_REJECT_UNSPECIFIED;
953 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700954 wnm_send_bss_transition_mgmt_resp(wpa_s,
955 wpa_s->wnm_dialog_token,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700956 status, 0, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800957 }
958}
959
960
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700961int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
962 u8 query_reason)
963{
964 u8 buf[1000], *pos;
965 struct ieee80211_mgmt *mgmt;
966 size_t len;
967 int ret;
968
969 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
970 MACSTR " query_reason=%u",
971 MAC2STR(wpa_s->bssid), query_reason);
972
973 mgmt = (struct ieee80211_mgmt *) buf;
974 os_memset(&buf, 0, sizeof(buf));
975 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
976 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
977 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
978 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
979 WLAN_FC_STYPE_ACTION);
980 mgmt->u.action.category = WLAN_ACTION_WNM;
981 mgmt->u.action.u.bss_tm_query.action = WNM_BSS_TRANS_MGMT_QUERY;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800982 mgmt->u.action.u.bss_tm_query.dialog_token = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700983 mgmt->u.action.u.bss_tm_query.query_reason = query_reason;
984 pos = mgmt->u.action.u.bss_tm_query.variable;
985
986 len = pos - (u8 *) &mgmt->u.action.category;
987
988 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
989 wpa_s->own_addr, wpa_s->bssid,
990 &mgmt->u.action.category, len, 0);
991
992 return ret;
993}
994
995
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800996static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
997 const u8 *sa, const u8 *data,
998 int len)
999{
1000 const u8 *pos, *end, *next;
1001 u8 ie, ie_len;
1002
1003 pos = data;
1004 end = data + len;
1005
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001006 while (end - pos > 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001007 ie = *pos++;
1008 ie_len = *pos++;
1009 wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
1010 ie, ie_len);
1011 if (ie_len > end - pos) {
1012 wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
1013 "subelement");
1014 break;
1015 }
1016 next = pos + ie_len;
1017 if (ie_len < 4) {
1018 pos = next;
1019 continue;
1020 }
1021 wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
1022 WPA_GET_BE24(pos), pos[3]);
1023
1024#ifdef CONFIG_HS20
1025 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1026 WPA_GET_BE24(pos) == OUI_WFA &&
1027 pos[3] == HS20_WNM_SUB_REM_NEEDED) {
1028 /* Subscription Remediation subelement */
1029 const u8 *ie_end;
1030 u8 url_len;
1031 char *url;
1032 u8 osu_method;
1033
1034 wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
1035 "subelement");
1036 ie_end = pos + ie_len;
1037 pos += 4;
1038 url_len = *pos++;
1039 if (url_len == 0) {
1040 wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
1041 url = NULL;
1042 osu_method = 1;
1043 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001044 if (url_len + 1 > ie_end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001045 wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
1046 url_len,
1047 (int) (ie_end - pos));
1048 break;
1049 }
1050 url = os_malloc(url_len + 1);
1051 if (url == NULL)
1052 break;
1053 os_memcpy(url, pos, url_len);
1054 url[url_len] = '\0';
1055 osu_method = pos[url_len];
1056 }
1057 hs20_rx_subscription_remediation(wpa_s, url,
1058 osu_method);
1059 os_free(url);
1060 pos = next;
1061 continue;
1062 }
1063
1064 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
1065 WPA_GET_BE24(pos) == OUI_WFA &&
1066 pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
1067 const u8 *ie_end;
1068 u8 url_len;
1069 char *url;
1070 u8 code;
1071 u16 reauth_delay;
1072
1073 ie_end = pos + ie_len;
1074 pos += 4;
1075 code = *pos++;
1076 reauth_delay = WPA_GET_LE16(pos);
1077 pos += 2;
1078 url_len = *pos++;
1079 wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
1080 "Imminent - Reason Code %u "
1081 "Re-Auth Delay %u URL Length %u",
1082 code, reauth_delay, url_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001083 if (url_len > ie_end - pos)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001084 break;
1085 url = os_malloc(url_len + 1);
1086 if (url == NULL)
1087 break;
1088 os_memcpy(url, pos, url_len);
1089 url[url_len] = '\0';
1090 hs20_rx_deauth_imminent_notice(wpa_s, code,
1091 reauth_delay, url);
1092 os_free(url);
1093 pos = next;
1094 continue;
1095 }
1096#endif /* CONFIG_HS20 */
1097
1098 pos = next;
1099 }
1100}
1101
1102
1103static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
1104 const u8 *sa, const u8 *frm, int len)
1105{
1106 const u8 *pos, *end;
1107 u8 dialog_token, type;
1108
1109 /* Dialog Token [1] | Type [1] | Subelements */
1110
1111 if (len < 2 || sa == NULL)
1112 return;
1113 end = frm + len;
1114 pos = frm;
1115 dialog_token = *pos++;
1116 type = *pos++;
1117
1118 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
1119 "(dialog_token %u type %u sa " MACSTR ")",
1120 dialog_token, type, MAC2STR(sa));
1121 wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
1122 pos, end - pos);
1123
1124 if (wpa_s->wpa_state != WPA_COMPLETED ||
1125 os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
1126 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
1127 "from our AP - ignore it");
1128 return;
1129 }
1130
1131 switch (type) {
1132 case 1:
1133 ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
1134 break;
1135 default:
1136 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
1137 "WNM-Notification type %u", type);
1138 break;
1139 }
1140}
1141
1142
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001143void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001144 const struct ieee80211_mgmt *mgmt, size_t len)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001145{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001146 const u8 *pos, *end;
1147 u8 act;
1148
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001149 if (len < IEEE80211_HDRLEN + 2)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001150 return;
1151
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07001152 pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001153 act = *pos++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001154 end = ((const u8 *) mgmt) + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001155
1156 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001157 act, MAC2STR(mgmt->sa));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001158 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001159 os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001160 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
1161 "frame");
1162 return;
1163 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001164
1165 switch (act) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001166 case WNM_BSS_TRANS_MGMT_REQ:
1167 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001168 !(mgmt->da[0] & 0x01));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001169 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001170 case WNM_SLEEP_MODE_RESP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001171 ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001172 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001173 case WNM_NOTIFICATION_REQ:
1174 ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
1175 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001176 default:
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001177 wpa_printf(MSG_ERROR, "WNM: Unknown request");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001178 break;
1179 }
1180}