blob: 9ab40c782d7f8d22cd511216d3bec041d73dab43 [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);
142
143 os_free(wnmsleep_ie);
144 os_free(wnmtfs_ie);
145 os_free(mgmt);
146
147 return res;
148}
149
150
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800151static void wnm_sleep_mode_enter_success(struct wpa_supplicant *wpa_s,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800152 const u8 *tfsresp_ie_start,
153 const u8 *tfsresp_ie_end)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800154{
155 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_CONFIRM,
156 wpa_s->bssid, NULL, NULL);
157 /* remove GTK/IGTK ?? */
158
159 /* set the TFS Resp IE(s) */
160 if (tfsresp_ie_start && tfsresp_ie_end &&
161 tfsresp_ie_end - tfsresp_ie_start >= 0) {
162 u16 tfsresp_ie_len;
163 tfsresp_ie_len = (tfsresp_ie_end + tfsresp_ie_end[1] + 2) -
164 tfsresp_ie_start;
165 wpa_printf(MSG_DEBUG, "TFS Resp IE(s) found");
166 /* pass the TFS Resp IE(s) to driver for processing */
167 if (ieee80211_11_set_tfs_ie(wpa_s, wpa_s->bssid,
168 tfsresp_ie_start,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800169 tfsresp_ie_len,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800170 WNM_SLEEP_TFS_RESP_IE_SET))
171 wpa_printf(MSG_DEBUG, "WNM: Fail to set TFS Resp IE");
172 }
173}
174
175
176static void wnm_sleep_mode_exit_success(struct wpa_supplicant *wpa_s,
177 const u8 *frm, u16 key_len_total)
178{
179 u8 *ptr, *end;
180 u8 gtk_len;
181
182 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_CONFIRM, wpa_s->bssid,
183 NULL, NULL);
184
185 /* Install GTK/IGTK */
186
187 /* point to key data field */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800188 ptr = (u8 *) frm + 1 + 2;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800189 end = ptr + key_len_total;
190 wpa_hexdump_key(MSG_DEBUG, "WNM: Key Data", ptr, key_len_total);
191
Jouni Malinen17de68d2015-11-09 15:25:41 -0800192 if (key_len_total && !wpa_sm_pmf_enabled(wpa_s->wpa)) {
193 wpa_msg(wpa_s, MSG_INFO,
194 "WNM: Ignore Key Data in WNM-Sleep Mode Response - PMF not enabled");
195 return;
196 }
197
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800198 while (end - ptr > 1) {
199 if (2 + ptr[1] > end - ptr) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800200 wpa_printf(MSG_DEBUG, "WNM: Invalid Key Data element "
201 "length");
202 if (end > ptr) {
203 wpa_hexdump(MSG_DEBUG, "WNM: Remaining data",
204 ptr, end - ptr);
205 }
206 break;
207 }
208 if (*ptr == WNM_SLEEP_SUBELEM_GTK) {
209 if (ptr[1] < 11 + 5) {
210 wpa_printf(MSG_DEBUG, "WNM: Too short GTK "
211 "subelem");
212 break;
213 }
214 gtk_len = *(ptr + 4);
215 if (ptr[1] < 11 + gtk_len ||
216 gtk_len < 5 || gtk_len > 32) {
217 wpa_printf(MSG_DEBUG, "WNM: Invalid GTK "
218 "subelem");
219 break;
220 }
221 wpa_wnmsleep_install_key(
222 wpa_s->wpa,
223 WNM_SLEEP_SUBELEM_GTK,
224 ptr);
225 ptr += 13 + gtk_len;
226#ifdef CONFIG_IEEE80211W
227 } else if (*ptr == WNM_SLEEP_SUBELEM_IGTK) {
228 if (ptr[1] < 2 + 6 + WPA_IGTK_LEN) {
229 wpa_printf(MSG_DEBUG, "WNM: Too short IGTK "
230 "subelem");
231 break;
232 }
233 wpa_wnmsleep_install_key(wpa_s->wpa,
234 WNM_SLEEP_SUBELEM_IGTK, ptr);
235 ptr += 10 + WPA_IGTK_LEN;
236#endif /* CONFIG_IEEE80211W */
237 } else
238 break; /* skip the loop */
239 }
240}
241
242
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700243static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
244 const u8 *frm, int len)
245{
246 /*
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700247 * Action [1] | Dialog Token [1] | Key Data Len [2] | Key Data |
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700248 * WNM-Sleep Mode IE | TFS Response IE
249 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800250 const u8 *pos = frm; /* point to payload after the action field */
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700251 u16 key_len_total;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700252 struct wnm_sleep_element *wnmsleep_ie = NULL;
253 /* multiple TFS Resp IE (assuming consecutive) */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800254 const u8 *tfsresp_ie_start = NULL;
255 const u8 *tfsresp_ie_end = NULL;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800256 size_t left;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700257
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700258 if (len < 3)
259 return;
260 key_len_total = WPA_GET_LE16(frm + 1);
261
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800262 wpa_printf(MSG_DEBUG, "WNM-Sleep Mode Response token=%u key_len_total=%d",
263 frm[0], key_len_total);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800264 left = len - 3;
265 if (key_len_total > left) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800266 wpa_printf(MSG_INFO, "WNM: Too short frame for Key Data field");
267 return;
268 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800269 pos += 3 + key_len_total;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800270 while (pos - frm + 1 < len) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700271 u8 ie_len = *(pos + 1);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800272 if (2 + ie_len > frm + len - pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800273 wpa_printf(MSG_INFO, "WNM: Invalid IE len %u", ie_len);
274 break;
275 }
276 wpa_hexdump(MSG_DEBUG, "WNM: Element", pos, 2 + ie_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800277 if (*pos == WLAN_EID_WNMSLEEP && ie_len >= 4)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700278 wnmsleep_ie = (struct wnm_sleep_element *) pos;
279 else if (*pos == WLAN_EID_TFS_RESP) {
280 if (!tfsresp_ie_start)
281 tfsresp_ie_start = pos;
282 tfsresp_ie_end = pos;
283 } else
284 wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
285 pos += ie_len + 2;
286 }
287
288 if (!wnmsleep_ie) {
289 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
290 return;
291 }
292
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800293 if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
294 wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700295 wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
296 "frame (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) {
299 wnm_sleep_mode_enter_success(wpa_s, tfsresp_ie_start,
300 tfsresp_ie_end);
301 } else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
302 wnm_sleep_mode_exit_success(wpa_s, frm, key_len_total);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700303 }
304 } else {
305 wpa_printf(MSG_DEBUG, "Reject recv WNM-Sleep Response frame "
306 "(action=%d, intval=%d)",
307 wnmsleep_ie->action_type, wnmsleep_ie->intval);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800308 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700309 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_ENTER_FAIL,
310 wpa_s->bssid, NULL, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800311 else if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700312 wpa_drv_wnm_oper(wpa_s, WNM_SLEEP_EXIT_FAIL,
313 wpa_s->bssid, NULL, NULL);
314 }
315}
316
317
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700318void wnm_deallocate_memory(struct wpa_supplicant *wpa_s)
319{
320 int i;
321
322 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700323 os_free(wpa_s->wnm_neighbor_report_elements[i].meas_pilot);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700324 os_free(wpa_s->wnm_neighbor_report_elements[i].mul_bssid);
325 }
326
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700327 wpa_s->wnm_num_neighbor_report = 0;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700328 os_free(wpa_s->wnm_neighbor_report_elements);
329 wpa_s->wnm_neighbor_report_elements = NULL;
330}
331
332
333static void wnm_parse_neighbor_report_elem(struct neighbor_report *rep,
334 u8 id, u8 elen, const u8 *pos)
335{
336 switch (id) {
337 case WNM_NEIGHBOR_TSF:
338 if (elen < 2 + 2) {
339 wpa_printf(MSG_DEBUG, "WNM: Too short TSF");
340 break;
341 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800342 rep->tsf_offset = WPA_GET_LE16(pos);
343 rep->beacon_int = WPA_GET_LE16(pos + 2);
344 rep->tsf_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700345 break;
346 case WNM_NEIGHBOR_CONDENSED_COUNTRY_STRING:
347 if (elen < 2) {
348 wpa_printf(MSG_DEBUG, "WNM: Too short condensed "
349 "country string");
350 break;
351 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800352 os_memcpy(rep->country, pos, 2);
353 rep->country_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700354 break;
355 case WNM_NEIGHBOR_BSS_TRANSITION_CANDIDATE:
356 if (elen < 1) {
357 wpa_printf(MSG_DEBUG, "WNM: Too short BSS transition "
358 "candidate");
359 break;
360 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800361 rep->preference = pos[0];
362 rep->preference_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700363 break;
364 case WNM_NEIGHBOR_BSS_TERMINATION_DURATION:
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800365 rep->bss_term_tsf = WPA_GET_LE64(pos);
366 rep->bss_term_dur = WPA_GET_LE16(pos + 8);
367 rep->bss_term_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700368 break;
369 case WNM_NEIGHBOR_BEARING:
370 if (elen < 8) {
371 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor "
372 "bearing");
373 break;
374 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800375 rep->bearing = WPA_GET_LE16(pos);
376 rep->distance = WPA_GET_LE32(pos + 2);
377 rep->rel_height = WPA_GET_LE16(pos + 2 + 4);
378 rep->bearing_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700379 break;
380 case WNM_NEIGHBOR_MEASUREMENT_PILOT:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700381 if (elen < 1) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700382 wpa_printf(MSG_DEBUG, "WNM: Too short measurement "
383 "pilot");
384 break;
385 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700386 os_free(rep->meas_pilot);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700387 rep->meas_pilot = os_zalloc(sizeof(struct measurement_pilot));
388 if (rep->meas_pilot == NULL)
389 break;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700390 rep->meas_pilot->measurement_pilot = pos[0];
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700391 rep->meas_pilot->subelem_len = elen - 1;
392 os_memcpy(rep->meas_pilot->subelems, pos + 1, elen - 1);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700393 break;
394 case WNM_NEIGHBOR_RRM_ENABLED_CAPABILITIES:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700395 if (elen < 5) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700396 wpa_printf(MSG_DEBUG, "WNM: Too short RRM enabled "
397 "capabilities");
398 break;
399 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800400 os_memcpy(rep->rm_capab, pos, 5);
401 rep->rm_capab_present = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700402 break;
403 case WNM_NEIGHBOR_MULTIPLE_BSSID:
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700404 if (elen < 1) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700405 wpa_printf(MSG_DEBUG, "WNM: Too short multiple BSSID");
406 break;
407 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700408 os_free(rep->mul_bssid);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700409 rep->mul_bssid = os_zalloc(sizeof(struct multiple_bssid));
410 if (rep->mul_bssid == NULL)
411 break;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700412 rep->mul_bssid->max_bssid_indicator = pos[0];
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700413 rep->mul_bssid->subelem_len = elen - 1;
414 os_memcpy(rep->mul_bssid->subelems, pos + 1, elen - 1);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700415 break;
416 }
417}
418
419
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800420static int wnm_nei_get_chan(struct wpa_supplicant *wpa_s, u8 op_class, u8 chan)
421{
422 struct wpa_bss *bss = wpa_s->current_bss;
423 const char *country = NULL;
424
425 if (bss) {
426 const u8 *elem = wpa_bss_get_ie(bss, WLAN_EID_COUNTRY);
427
428 if (elem && elem[1] >= 2)
429 country = (const char *) (elem + 2);
430 }
431
432 return ieee80211_chan_to_freq(country, op_class, chan);
433}
434
435
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700436static void wnm_parse_neighbor_report(struct wpa_supplicant *wpa_s,
437 const u8 *pos, u8 len,
438 struct neighbor_report *rep)
439{
440 u8 left = len;
441
442 if (left < 13) {
443 wpa_printf(MSG_DEBUG, "WNM: Too short neighbor report");
444 return;
445 }
446
447 os_memcpy(rep->bssid, pos, ETH_ALEN);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800448 rep->bssid_info = WPA_GET_LE32(pos + ETH_ALEN);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700449 rep->regulatory_class = *(pos + 10);
450 rep->channel_number = *(pos + 11);
451 rep->phy_type = *(pos + 12);
452
453 pos += 13;
454 left -= 13;
455
456 while (left >= 2) {
457 u8 id, elen;
458
459 id = *pos++;
460 elen = *pos++;
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700461 wpa_printf(MSG_DEBUG, "WNM: Subelement id=%u len=%u", id, elen);
462 left -= 2;
463 if (elen > left) {
464 wpa_printf(MSG_DEBUG,
465 "WNM: Truncated neighbor report subelement");
466 break;
467 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700468 wnm_parse_neighbor_report_elem(rep, id, elen, pos);
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700469 left -= elen;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700470 pos += elen;
471 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800472
473 rep->freq = wnm_nei_get_chan(wpa_s, rep->regulatory_class,
474 rep->channel_number);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700475}
476
477
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800478static struct wpa_bss *
479compare_scan_neighbor_results(struct wpa_supplicant *wpa_s)
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700480{
481
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800482 u8 i;
483 struct wpa_bss *bss = wpa_s->current_bss;
484 struct wpa_bss *target;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700485
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800486 if (!bss)
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700487 return 0;
488
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800489 wpa_printf(MSG_DEBUG, "WNM: Current BSS " MACSTR " RSSI %d",
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800490 MAC2STR(wpa_s->bssid), bss->level);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800491
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800492 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
493 struct neighbor_report *nei;
494
495 nei = &wpa_s->wnm_neighbor_report_elements[i];
496 if (nei->preference_present && nei->preference == 0) {
497 wpa_printf(MSG_DEBUG, "Skip excluded BSS " MACSTR,
498 MAC2STR(nei->bssid));
499 continue;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700500 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800501
502 target = wpa_bss_get_bssid(wpa_s, nei->bssid);
503 if (!target) {
504 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
505 " (pref %d) not found in scan results",
506 MAC2STR(nei->bssid),
507 nei->preference_present ? nei->preference :
508 -1);
509 continue;
510 }
511
512 if (bss->ssid_len != target->ssid_len ||
513 os_memcmp(bss->ssid, target->ssid, bss->ssid_len) != 0) {
514 /*
515 * TODO: Could consider allowing transition to another
516 * ESS if PMF was enabled for the association.
517 */
518 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
519 " (pref %d) in different ESS",
520 MAC2STR(nei->bssid),
521 nei->preference_present ? nei->preference :
522 -1);
523 continue;
524 }
525
526 if (target->level < bss->level && target->level < -80) {
527 wpa_printf(MSG_DEBUG, "Candidate BSS " MACSTR
528 " (pref %d) does not have sufficient signal level (%d)",
529 MAC2STR(nei->bssid),
530 nei->preference_present ? nei->preference :
531 -1,
532 target->level);
533 continue;
534 }
535
536 wpa_printf(MSG_DEBUG,
537 "WNM: Found an acceptable preferred transition candidate BSS "
538 MACSTR " (RSSI %d)",
539 MAC2STR(nei->bssid), target->level);
540 return target;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700541 }
542
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800543 return NULL;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700544}
545
546
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700547static void wnm_send_bss_transition_mgmt_resp(
548 struct wpa_supplicant *wpa_s, u8 dialog_token,
549 enum bss_trans_mgmt_status_code status, u8 delay,
550 const u8 *target_bssid)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800551{
552 u8 buf[1000], *pos;
553 struct ieee80211_mgmt *mgmt;
554 size_t len;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800555 int res;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800556
557 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Response "
558 "to " MACSTR " dialog_token=%u status=%u delay=%d",
559 MAC2STR(wpa_s->bssid), dialog_token, status, delay);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800560 if (!wpa_s->current_bss) {
561 wpa_printf(MSG_DEBUG,
562 "WNM: Current BSS not known - drop response");
563 return;
564 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800565
566 mgmt = (struct ieee80211_mgmt *) buf;
567 os_memset(&buf, 0, sizeof(buf));
568 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
569 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
570 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
571 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
572 WLAN_FC_STYPE_ACTION);
573 mgmt->u.action.category = WLAN_ACTION_WNM;
574 mgmt->u.action.u.bss_tm_resp.action = WNM_BSS_TRANS_MGMT_RESP;
575 mgmt->u.action.u.bss_tm_resp.dialog_token = dialog_token;
576 mgmt->u.action.u.bss_tm_resp.status_code = status;
577 mgmt->u.action.u.bss_tm_resp.bss_termination_delay = delay;
578 pos = mgmt->u.action.u.bss_tm_resp.variable;
579 if (target_bssid) {
580 os_memcpy(pos, target_bssid, ETH_ALEN);
581 pos += ETH_ALEN;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800582 } else if (status == WNM_BSS_TM_ACCEPT) {
583 /*
584 * P802.11-REVmc clarifies that the Target BSSID field is always
585 * present when status code is zero, so use a fake value here if
586 * no BSSID is yet known.
587 */
588 os_memset(pos, 0, ETH_ALEN);
589 pos += ETH_ALEN;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800590 }
591
592 len = pos - (u8 *) &mgmt->u.action.category;
593
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800594 res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
595 wpa_s->own_addr, wpa_s->bssid,
596 &mgmt->u.action.category, len, 0);
597 if (res < 0) {
598 wpa_printf(MSG_DEBUG,
599 "WNM: Failed to send BSS Transition Management Response");
600 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800601}
602
603
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800604int wnm_scan_process(struct wpa_supplicant *wpa_s, int reply_on_fail)
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700605{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800606 struct wpa_bss *bss;
607 struct wpa_ssid *ssid = wpa_s->current_ssid;
608 enum bss_trans_mgmt_status_code status = WNM_BSS_TM_REJECT_UNSPECIFIED;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700609
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800610 if (!wpa_s->wnm_neighbor_report_elements)
611 return 0;
612
613 if (os_reltime_before(&wpa_s->wnm_cand_valid_until,
614 &wpa_s->scan_trigger_time)) {
615 wpa_printf(MSG_DEBUG, "WNM: Previously stored BSS transition candidate list is not valid anymore - drop it");
616 wnm_deallocate_memory(wpa_s);
617 return 0;
618 }
619
620 if (!wpa_s->current_bss ||
621 os_memcmp(wpa_s->wnm_cand_from_bss, wpa_s->current_bss->bssid,
622 ETH_ALEN) != 0) {
623 wpa_printf(MSG_DEBUG, "WNM: Stored BSS transition candidate list not from the current BSS - ignore it");
624 return 0;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700625 }
626
627 /* Compare the Neighbor Report and scan results */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800628 bss = compare_scan_neighbor_results(wpa_s);
629 if (!bss) {
630 wpa_printf(MSG_DEBUG, "WNM: No BSS transition candidate match found");
631 status = WNM_BSS_TM_REJECT_NO_SUITABLE_CANDIDATES;
632 goto send_bss_resp_fail;
633 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700634
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800635 /* Associate to the network */
636 /* Send the BSS Management Response - Accept */
637 if (wpa_s->wnm_reply) {
638 wpa_s->wnm_reply = 0;
639 wnm_send_bss_transition_mgmt_resp(wpa_s,
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700640 wpa_s->wnm_dialog_token,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700641 WNM_BSS_TM_ACCEPT,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800642 0, bss->bssid);
643 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700644
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800645 if (bss == wpa_s->current_bss) {
646 wpa_printf(MSG_DEBUG,
647 "WNM: Already associated with the preferred candidate");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800648 wnm_deallocate_memory(wpa_s);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800649 return 1;
650 }
651
652 wpa_s->reassociate = 1;
653 wpa_supplicant_connect(wpa_s, bss, ssid);
654 wnm_deallocate_memory(wpa_s);
655 return 1;
656
657send_bss_resp_fail:
658 if (!reply_on_fail)
659 return 0;
660
661 /* Send reject response for all the failures */
662
663 if (wpa_s->wnm_reply) {
664 wpa_s->wnm_reply = 0;
665 wnm_send_bss_transition_mgmt_resp(wpa_s,
666 wpa_s->wnm_dialog_token,
667 status, 0, NULL);
668 }
669 wnm_deallocate_memory(wpa_s);
670
671 return 0;
672}
673
674
675static int cand_pref_compar(const void *a, const void *b)
676{
677 const struct neighbor_report *aa = a;
678 const struct neighbor_report *bb = b;
679
680 if (!aa->preference_present && !bb->preference_present)
681 return 0;
682 if (!aa->preference_present)
683 return 1;
684 if (!bb->preference_present)
685 return -1;
686 if (bb->preference > aa->preference)
687 return 1;
688 if (bb->preference < aa->preference)
689 return -1;
690 return 0;
691}
692
693
694static void wnm_sort_cand_list(struct wpa_supplicant *wpa_s)
695{
696 if (!wpa_s->wnm_neighbor_report_elements)
697 return;
698 qsort(wpa_s->wnm_neighbor_report_elements,
699 wpa_s->wnm_num_neighbor_report, sizeof(struct neighbor_report),
700 cand_pref_compar);
701}
702
703
704static void wnm_dump_cand_list(struct wpa_supplicant *wpa_s)
705{
706 unsigned int i;
707
708 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Candidate List");
709 if (!wpa_s->wnm_neighbor_report_elements)
710 return;
711 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
712 struct neighbor_report *nei;
713
714 nei = &wpa_s->wnm_neighbor_report_elements[i];
715 wpa_printf(MSG_DEBUG, "%u: " MACSTR
716 " info=0x%x op_class=%u chan=%u phy=%u pref=%d freq=%d",
717 i, MAC2STR(nei->bssid), nei->bssid_info,
718 nei->regulatory_class,
719 nei->channel_number, nei->phy_type,
720 nei->preference_present ? nei->preference : -1,
721 nei->freq);
722 }
723}
724
725
726static int chan_supported(struct wpa_supplicant *wpa_s, int freq)
727{
728 unsigned int i;
729
730 for (i = 0; i < wpa_s->hw.num_modes; i++) {
731 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
732 int j;
733
734 for (j = 0; j < mode->num_channels; j++) {
735 struct hostapd_channel_data *chan;
736
737 chan = &mode->channels[j];
738 if (chan->freq == freq &&
739 !(chan->flag & HOSTAPD_CHAN_DISABLED))
740 return 1;
741 }
742 }
743
744 return 0;
745}
746
747
748static void wnm_set_scan_freqs(struct wpa_supplicant *wpa_s)
749{
750 int *freqs;
751 int num_freqs = 0;
752 unsigned int i;
753
754 if (!wpa_s->wnm_neighbor_report_elements)
755 return;
756
757 if (wpa_s->hw.modes == NULL)
758 return;
759
760 os_free(wpa_s->next_scan_freqs);
761 wpa_s->next_scan_freqs = NULL;
762
763 freqs = os_calloc(wpa_s->wnm_num_neighbor_report + 1, sizeof(int));
764 if (freqs == NULL)
765 return;
766
767 for (i = 0; i < wpa_s->wnm_num_neighbor_report; i++) {
768 struct neighbor_report *nei;
769
770 nei = &wpa_s->wnm_neighbor_report_elements[i];
771 if (nei->freq <= 0) {
772 wpa_printf(MSG_DEBUG,
773 "WNM: Unknown neighbor operating frequency for "
774 MACSTR " - scan all channels",
775 MAC2STR(nei->bssid));
776 os_free(freqs);
777 return;
778 }
779 if (chan_supported(wpa_s, nei->freq))
780 add_freq(freqs, &num_freqs, nei->freq);
781 }
782
783 if (num_freqs == 0) {
784 os_free(freqs);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700785 return;
786 }
787
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800788 wpa_printf(MSG_DEBUG,
789 "WNM: Scan %d frequencies based on transition candidate list",
790 num_freqs);
791 wpa_s->next_scan_freqs = freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700792}
793
794
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800795static void ieee802_11_rx_bss_trans_mgmt_req(struct wpa_supplicant *wpa_s,
796 const u8 *pos, const u8 *end,
797 int reply)
798{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800799 unsigned int beacon_int;
800 u8 valid_int;
801
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800802 if (end - pos < 5)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800803 return;
804
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800805 if (wpa_s->current_bss)
806 beacon_int = wpa_s->current_bss->beacon_int;
807 else
808 beacon_int = 100; /* best guess */
809
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700810 wpa_s->wnm_dialog_token = pos[0];
811 wpa_s->wnm_mode = pos[1];
812 wpa_s->wnm_dissoc_timer = WPA_GET_LE16(pos + 2);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800813 valid_int = pos[4];
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700814 wpa_s->wnm_reply = reply;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800815
816 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Request: "
817 "dialog_token=%u request_mode=0x%x "
818 "disassoc_timer=%u validity_interval=%u",
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700819 wpa_s->wnm_dialog_token, wpa_s->wnm_mode,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800820 wpa_s->wnm_dissoc_timer, valid_int);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700821
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800822 pos += 5;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700823
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700824 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800825 if (end - pos < 12) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700826 wpa_printf(MSG_DEBUG, "WNM: Too short BSS TM Request");
827 return;
828 }
829 os_memcpy(wpa_s->wnm_bss_termination_duration, pos, 12);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800830 pos += 12; /* BSS Termination Duration */
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700831 }
832
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700833 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800834 char url[256];
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700835
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800836 if (end - pos < 1 || 1 + pos[0] > end - pos) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800837 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS Transition "
838 "Management Request (URL)");
839 return;
840 }
841 os_memcpy(url, pos + 1, pos[0]);
842 url[pos[0]] = '\0';
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700843 pos += 1 + pos[0];
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700844
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700845 wpa_msg(wpa_s, MSG_INFO, ESS_DISASSOC_IMMINENT "%d %u %s",
846 wpa_sm_pmf_enabled(wpa_s->wpa),
847 wpa_s->wnm_dissoc_timer * beacon_int * 128 / 125, url);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800848 }
849
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700850 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800851 wpa_msg(wpa_s, MSG_INFO, "WNM: Disassociation Imminent - "
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700852 "Disassociation Timer %u", wpa_s->wnm_dissoc_timer);
853 if (wpa_s->wnm_dissoc_timer && !wpa_s->scanning) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800854 /* TODO: mark current BSS less preferred for
855 * selection */
856 wpa_printf(MSG_DEBUG, "Trying to find another BSS");
857 wpa_supplicant_req_scan(wpa_s, 0, 0);
858 }
859 }
860
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700861 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800862 unsigned int valid_ms;
863
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700864 wpa_msg(wpa_s, MSG_INFO, "WNM: Preferred List Available");
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800865 wnm_deallocate_memory(wpa_s);
866 wpa_s->wnm_neighbor_report_elements = os_calloc(
867 WNM_MAX_NEIGHBOR_REPORT,
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700868 sizeof(struct neighbor_report));
869 if (wpa_s->wnm_neighbor_report_elements == NULL)
870 return;
871
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800872 while (end - pos >= 2 &&
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700873 wpa_s->wnm_num_neighbor_report < WNM_MAX_NEIGHBOR_REPORT)
874 {
875 u8 tag = *pos++;
876 u8 len = *pos++;
877
878 wpa_printf(MSG_DEBUG, "WNM: Neighbor report tag %u",
879 tag);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800880 if (len > end - pos) {
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700881 wpa_printf(MSG_DEBUG, "WNM: Truncated request");
882 return;
883 }
Dmitry Shmidtf940fbd2014-04-10 10:23:13 -0700884 if (tag == WLAN_EID_NEIGHBOR_REPORT) {
885 struct neighbor_report *rep;
886 rep = &wpa_s->wnm_neighbor_report_elements[
887 wpa_s->wnm_num_neighbor_report];
888 wnm_parse_neighbor_report(wpa_s, pos, len, rep);
889 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700890
891 pos += len;
892 wpa_s->wnm_num_neighbor_report++;
893 }
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800894 wnm_sort_cand_list(wpa_s);
895 wnm_dump_cand_list(wpa_s);
896 valid_ms = valid_int * beacon_int * 128 / 125;
897 wpa_printf(MSG_DEBUG, "WNM: Candidate list valid for %u ms",
898 valid_ms);
899 os_get_reltime(&wpa_s->wnm_cand_valid_until);
900 wpa_s->wnm_cand_valid_until.sec += valid_ms / 1000;
901 wpa_s->wnm_cand_valid_until.usec += (valid_ms % 1000) * 1000;
902 wpa_s->wnm_cand_valid_until.sec +=
903 wpa_s->wnm_cand_valid_until.usec / 1000000;
904 wpa_s->wnm_cand_valid_until.usec %= 1000000;
905 os_memcpy(wpa_s->wnm_cand_from_bss, wpa_s->bssid, ETH_ALEN);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700906
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800907 if (wpa_s->last_scan_res_used > 0) {
908 struct os_reltime now;
909
910 os_get_reltime(&now);
911 if (!os_reltime_expired(&now, &wpa_s->last_scan, 10)) {
912 wpa_printf(MSG_DEBUG,
913 "WNM: Try to use recent scan results");
914 if (wnm_scan_process(wpa_s, 0) > 0)
915 return;
916 wpa_printf(MSG_DEBUG,
917 "WNM: No match in previous scan results - try a new scan");
918 }
919 }
920
921 wnm_set_scan_freqs(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700922 wpa_supplicant_req_scan(wpa_s, 0, 0);
923 } else if (reply) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700924 enum bss_trans_mgmt_status_code status;
925 if (wpa_s->wnm_mode & WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT)
926 status = WNM_BSS_TM_ACCEPT;
927 else {
928 wpa_msg(wpa_s, MSG_INFO, "WNM: BSS Transition Management Request did not include candidates");
929 status = WNM_BSS_TM_REJECT_UNSPECIFIED;
930 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700931 wnm_send_bss_transition_mgmt_resp(wpa_s,
932 wpa_s->wnm_dialog_token,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700933 status, 0, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800934 }
935}
936
937
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700938int wnm_send_bss_transition_mgmt_query(struct wpa_supplicant *wpa_s,
939 u8 query_reason)
940{
941 u8 buf[1000], *pos;
942 struct ieee80211_mgmt *mgmt;
943 size_t len;
944 int ret;
945
946 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Query to "
947 MACSTR " query_reason=%u",
948 MAC2STR(wpa_s->bssid), query_reason);
949
950 mgmt = (struct ieee80211_mgmt *) buf;
951 os_memset(&buf, 0, sizeof(buf));
952 os_memcpy(mgmt->da, wpa_s->bssid, ETH_ALEN);
953 os_memcpy(mgmt->sa, wpa_s->own_addr, ETH_ALEN);
954 os_memcpy(mgmt->bssid, wpa_s->bssid, ETH_ALEN);
955 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
956 WLAN_FC_STYPE_ACTION);
957 mgmt->u.action.category = WLAN_ACTION_WNM;
958 mgmt->u.action.u.bss_tm_query.action = WNM_BSS_TRANS_MGMT_QUERY;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800959 mgmt->u.action.u.bss_tm_query.dialog_token = 1;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700960 mgmt->u.action.u.bss_tm_query.query_reason = query_reason;
961 pos = mgmt->u.action.u.bss_tm_query.variable;
962
963 len = pos - (u8 *) &mgmt->u.action.category;
964
965 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
966 wpa_s->own_addr, wpa_s->bssid,
967 &mgmt->u.action.category, len, 0);
968
969 return ret;
970}
971
972
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800973static void ieee802_11_rx_wnm_notif_req_wfa(struct wpa_supplicant *wpa_s,
974 const u8 *sa, const u8 *data,
975 int len)
976{
977 const u8 *pos, *end, *next;
978 u8 ie, ie_len;
979
980 pos = data;
981 end = data + len;
982
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800983 while (end - pos > 1) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800984 ie = *pos++;
985 ie_len = *pos++;
986 wpa_printf(MSG_DEBUG, "WNM: WFA subelement %u len %u",
987 ie, ie_len);
988 if (ie_len > end - pos) {
989 wpa_printf(MSG_DEBUG, "WNM: Not enough room for "
990 "subelement");
991 break;
992 }
993 next = pos + ie_len;
994 if (ie_len < 4) {
995 pos = next;
996 continue;
997 }
998 wpa_printf(MSG_DEBUG, "WNM: Subelement OUI %06x type %u",
999 WPA_GET_BE24(pos), pos[3]);
1000
1001#ifdef CONFIG_HS20
1002 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 5 &&
1003 WPA_GET_BE24(pos) == OUI_WFA &&
1004 pos[3] == HS20_WNM_SUB_REM_NEEDED) {
1005 /* Subscription Remediation subelement */
1006 const u8 *ie_end;
1007 u8 url_len;
1008 char *url;
1009 u8 osu_method;
1010
1011 wpa_printf(MSG_DEBUG, "WNM: Subscription Remediation "
1012 "subelement");
1013 ie_end = pos + ie_len;
1014 pos += 4;
1015 url_len = *pos++;
1016 if (url_len == 0) {
1017 wpa_printf(MSG_DEBUG, "WNM: No Server URL included");
1018 url = NULL;
1019 osu_method = 1;
1020 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001021 if (url_len + 1 > ie_end - pos) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001022 wpa_printf(MSG_DEBUG, "WNM: Not enough room for Server URL (len=%u) and Server Method (left %d)",
1023 url_len,
1024 (int) (ie_end - pos));
1025 break;
1026 }
1027 url = os_malloc(url_len + 1);
1028 if (url == NULL)
1029 break;
1030 os_memcpy(url, pos, url_len);
1031 url[url_len] = '\0';
1032 osu_method = pos[url_len];
1033 }
1034 hs20_rx_subscription_remediation(wpa_s, url,
1035 osu_method);
1036 os_free(url);
1037 pos = next;
1038 continue;
1039 }
1040
1041 if (ie == WLAN_EID_VENDOR_SPECIFIC && ie_len >= 8 &&
1042 WPA_GET_BE24(pos) == OUI_WFA &&
1043 pos[3] == HS20_WNM_DEAUTH_IMMINENT_NOTICE) {
1044 const u8 *ie_end;
1045 u8 url_len;
1046 char *url;
1047 u8 code;
1048 u16 reauth_delay;
1049
1050 ie_end = pos + ie_len;
1051 pos += 4;
1052 code = *pos++;
1053 reauth_delay = WPA_GET_LE16(pos);
1054 pos += 2;
1055 url_len = *pos++;
1056 wpa_printf(MSG_DEBUG, "WNM: HS 2.0 Deauthentication "
1057 "Imminent - Reason Code %u "
1058 "Re-Auth Delay %u URL Length %u",
1059 code, reauth_delay, url_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001060 if (url_len > ie_end - pos)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001061 break;
1062 url = os_malloc(url_len + 1);
1063 if (url == NULL)
1064 break;
1065 os_memcpy(url, pos, url_len);
1066 url[url_len] = '\0';
1067 hs20_rx_deauth_imminent_notice(wpa_s, code,
1068 reauth_delay, url);
1069 os_free(url);
1070 pos = next;
1071 continue;
1072 }
1073#endif /* CONFIG_HS20 */
1074
1075 pos = next;
1076 }
1077}
1078
1079
1080static void ieee802_11_rx_wnm_notif_req(struct wpa_supplicant *wpa_s,
1081 const u8 *sa, const u8 *frm, int len)
1082{
1083 const u8 *pos, *end;
1084 u8 dialog_token, type;
1085
1086 /* Dialog Token [1] | Type [1] | Subelements */
1087
1088 if (len < 2 || sa == NULL)
1089 return;
1090 end = frm + len;
1091 pos = frm;
1092 dialog_token = *pos++;
1093 type = *pos++;
1094
1095 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Received WNM-Notification Request "
1096 "(dialog_token %u type %u sa " MACSTR ")",
1097 dialog_token, type, MAC2STR(sa));
1098 wpa_hexdump(MSG_DEBUG, "WNM-Notification Request subelements",
1099 pos, end - pos);
1100
1101 if (wpa_s->wpa_state != WPA_COMPLETED ||
1102 os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
1103 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: WNM-Notification frame not "
1104 "from our AP - ignore it");
1105 return;
1106 }
1107
1108 switch (type) {
1109 case 1:
1110 ieee802_11_rx_wnm_notif_req_wfa(wpa_s, sa, pos, end - pos);
1111 break;
1112 default:
1113 wpa_dbg(wpa_s, MSG_DEBUG, "WNM: Ignore unknown "
1114 "WNM-Notification type %u", type);
1115 break;
1116 }
1117}
1118
1119
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001120void ieee802_11_rx_wnm_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001121 const struct ieee80211_mgmt *mgmt, size_t len)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001122{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001123 const u8 *pos, *end;
1124 u8 act;
1125
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001126 if (len < IEEE80211_HDRLEN + 2)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001127 return;
1128
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07001129 pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001130 act = *pos++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001131 end = ((const u8 *) mgmt) + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001132
1133 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001134 act, MAC2STR(mgmt->sa));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001135 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001136 os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001137 wpa_printf(MSG_DEBUG, "WNM: Ignore unexpected WNM Action "
1138 "frame");
1139 return;
1140 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001141
1142 switch (act) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001143 case WNM_BSS_TRANS_MGMT_REQ:
1144 ieee802_11_rx_bss_trans_mgmt_req(wpa_s, pos, end,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001145 !(mgmt->da[0] & 0x01));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001146 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001147 case WNM_SLEEP_MODE_RESP:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001148 ieee802_11_rx_wnmsleep_resp(wpa_s, pos, end - pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001149 break;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001150 case WNM_NOTIFICATION_REQ:
1151 ieee802_11_rx_wnm_notif_req(wpa_s, mgmt->sa, pos, end - pos);
1152 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001153 default:
Dmitry Shmidt44c95782013-05-17 09:51:35 -07001154 wpa_printf(MSG_ERROR, "WNM: Unknown request");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001155 break;
1156 }
1157}