3 * Copyright (c) 2011-2014, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
15 #include "ap/hostapd.h"
16 #include "ap/sta_info.h"
17 #include "ap/ap_config.h"
18 #include "ap/ap_drv_ops.h"
19 #include "ap/wpa_auth.h"
22 #define MAX_TFS_IE_LEN 1024
25 /* get the TFS IE from driver */
26 static int ieee80211_11_get_tfs_ie(struct hostapd_data *hapd, const u8 *addr,
27 u8 *buf, u16 *buf_len, enum wnm_oper oper)
29 wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
31 return hostapd_drv_wnm_oper(hapd, oper, addr, buf, buf_len);
35 /* set the TFS IE to driver */
36 static int ieee80211_11_set_tfs_ie(struct hostapd_data *hapd, const u8 *addr,
37 u8 *buf, u16 *buf_len, enum wnm_oper oper)
39 wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
41 return hostapd_drv_wnm_oper(hapd, oper, addr, buf, buf_len);
45 /* MLME-SLEEPMODE.response */
46 static int ieee802_11_send_wnmsleep_resp(struct hostapd_data *hapd,
47 const u8 *addr, u8 dialog_token,
48 u8 action_type, u16 intval)
50 struct ieee80211_mgmt *mgmt;
53 size_t gtk_elem_len = 0;
54 size_t igtk_elem_len = 0;
55 struct wnm_sleep_element wnmsleep_ie;
61 enum wnm_oper tfs_oper = action_type == WNM_SLEEP_MODE_ENTER ?
62 WNM_SLEEP_TFS_RESP_IE_ADD : WNM_SLEEP_TFS_RESP_IE_NONE;
64 sta = ap_get_sta(hapd, addr);
66 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
70 /* WNM-Sleep Mode IE */
71 os_memset(&wnmsleep_ie, 0, sizeof(struct wnm_sleep_element));
72 wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
73 wnmsleep_ie.eid = WLAN_EID_WNMSLEEP;
74 wnmsleep_ie.len = wnmsleep_ie_len - 2;
75 wnmsleep_ie.action_type = action_type;
76 wnmsleep_ie.status = WNM_STATUS_SLEEP_ACCEPT;
77 wnmsleep_ie.intval = host_to_le16(intval);
80 wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
81 if (wnmtfs_ie == NULL)
83 if (ieee80211_11_get_tfs_ie(hapd, addr, wnmtfs_ie, &wnmtfs_ie_len,
90 #define MAX_GTK_SUBELEM_LEN 45
91 #define MAX_IGTK_SUBELEM_LEN 26
92 mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len +
93 MAX_GTK_SUBELEM_LEN + MAX_IGTK_SUBELEM_LEN);
95 wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
96 "WNM-Sleep Response action frame");
99 os_memcpy(mgmt->da, addr, ETH_ALEN);
100 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
101 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
102 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
103 WLAN_FC_STYPE_ACTION);
104 mgmt->u.action.category = WLAN_ACTION_WNM;
105 mgmt->u.action.u.wnm_sleep_resp.action = WNM_SLEEP_MODE_RESP;
106 mgmt->u.action.u.wnm_sleep_resp.dialogtoken = dialog_token;
107 pos = (u8 *)mgmt->u.action.u.wnm_sleep_resp.variable;
108 /* add key data if MFP is enabled */
109 if (!wpa_auth_uses_mfp(sta->wpa_sm) ||
110 action_type != WNM_SLEEP_MODE_EXIT) {
111 mgmt->u.action.u.wnm_sleep_resp.keydata_len = 0;
113 gtk_elem_len = wpa_wnmsleep_gtk_subelem(sta->wpa_sm, pos);
115 wpa_printf(MSG_DEBUG, "Pass 4, gtk_len = %d",
117 #ifdef CONFIG_IEEE80211W
118 res = wpa_wnmsleep_igtk_subelem(sta->wpa_sm, pos);
125 pos += igtk_elem_len;
126 wpa_printf(MSG_DEBUG, "Pass 4 igtk_len = %d",
127 (int) igtk_elem_len);
128 #endif /* CONFIG_IEEE80211W */
131 &mgmt->u.action.u.wnm_sleep_resp.keydata_len,
132 gtk_elem_len + igtk_elem_len);
134 os_memcpy(pos, &wnmsleep_ie, wnmsleep_ie_len);
135 /* copy TFS IE here */
136 pos += wnmsleep_ie_len;
138 os_memcpy(pos, wnmtfs_ie, wnmtfs_ie_len);
140 len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_resp) + gtk_elem_len +
141 igtk_elem_len + wnmsleep_ie_len + wnmtfs_ie_len;
143 /* In driver, response frame should be forced to sent when STA is in
145 res = hostapd_drv_send_action(hapd, hapd->iface->freq, 0,
146 mgmt->da, &mgmt->u.action.category, len);
149 wpa_printf(MSG_DEBUG, "Successfully send WNM-Sleep Response "
152 /* when entering wnmsleep
153 * 1. pause the node in driver
154 * 2. mark the node so that AP won't update GTK/IGTK during
157 if (wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT &&
158 wnmsleep_ie.action_type == WNM_SLEEP_MODE_ENTER) {
159 sta->flags |= WLAN_STA_WNM_SLEEP_MODE;
160 hostapd_drv_wnm_oper(hapd, WNM_SLEEP_ENTER_CONFIRM,
162 wpa_set_wnmsleep(sta->wpa_sm, 1);
164 /* when exiting wnmsleep
166 * 2. start GTK/IGTK update if MFP is not used
167 * 3. unpause the node in driver
169 if ((wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT ||
170 wnmsleep_ie.status ==
171 WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) &&
172 wnmsleep_ie.action_type == WNM_SLEEP_MODE_EXIT) {
173 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
174 wpa_set_wnmsleep(sta->wpa_sm, 0);
175 hostapd_drv_wnm_oper(hapd, WNM_SLEEP_EXIT_CONFIRM,
177 if (!wpa_auth_uses_mfp(sta->wpa_sm))
178 wpa_wnmsleep_rekey_gtk(sta->wpa_sm);
181 wpa_printf(MSG_DEBUG, "Fail to send WNM-Sleep Response frame");
183 #undef MAX_GTK_SUBELEM_LEN
184 #undef MAX_IGTK_SUBELEM_LEN
191 static void ieee802_11_rx_wnmsleep_req(struct hostapd_data *hapd,
192 const u8 *addr, const u8 *frm, int len)
194 /* Dialog Token [1] | WNM-Sleep Mode IE | TFS Response IE */
197 struct wnm_sleep_element *wnmsleep_ie = NULL;
198 /* multiple TFS Req IE (assuming consecutive) */
199 u8 *tfsreq_ie_start = NULL;
200 u8 *tfsreq_ie_end = NULL;
201 u16 tfsreq_ie_len = 0;
203 dialog_token = *pos++;
204 while (pos + 1 < frm + len) {
206 if (pos + 2 + ie_len > frm + len)
208 if (*pos == WLAN_EID_WNMSLEEP)
209 wnmsleep_ie = (struct wnm_sleep_element *) pos;
210 else if (*pos == WLAN_EID_TFS_REQ) {
211 if (!tfsreq_ie_start)
212 tfsreq_ie_start = (u8 *) pos;
213 tfsreq_ie_end = (u8 *) pos;
215 wpa_printf(MSG_DEBUG, "WNM: EID %d not recognized",
221 wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
225 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER &&
226 tfsreq_ie_start && tfsreq_ie_end &&
227 tfsreq_ie_end - tfsreq_ie_start >= 0) {
228 tfsreq_ie_len = (tfsreq_ie_end + tfsreq_ie_end[1] + 2) -
230 wpa_printf(MSG_DEBUG, "TFS Req IE(s) found");
231 /* pass the TFS Req IE(s) to driver for processing */
232 if (ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
234 WNM_SLEEP_TFS_REQ_IE_SET))
235 wpa_printf(MSG_DEBUG, "Fail to set TFS Req IE");
238 ieee802_11_send_wnmsleep_resp(hapd, addr, dialog_token,
239 wnmsleep_ie->action_type,
240 le_to_host16(wnmsleep_ie->intval));
242 if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
243 /* clear the tfs after sending the resp frame */
244 ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
245 &tfsreq_ie_len, WNM_SLEEP_TFS_IE_DEL);
250 static int ieee802_11_send_bss_trans_mgmt_request(struct hostapd_data *hapd,
255 struct ieee80211_mgmt *mgmt;
261 url_len = os_strlen(url);
265 mgmt = os_zalloc(sizeof(*mgmt) + (url_len ? 1 + url_len : 0));
268 os_memcpy(mgmt->da, addr, ETH_ALEN);
269 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
270 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
271 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
272 WLAN_FC_STYPE_ACTION);
273 mgmt->u.action.category = WLAN_ACTION_WNM;
274 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
275 mgmt->u.action.u.bss_tm_req.dialog_token = dialog_token;
276 mgmt->u.action.u.bss_tm_req.req_mode = 0;
277 mgmt->u.action.u.bss_tm_req.disassoc_timer = host_to_le16(0);
278 mgmt->u.action.u.bss_tm_req.validity_interval = 1;
279 pos = mgmt->u.action.u.bss_tm_req.variable;
282 os_memcpy(pos, url, url_len);
286 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Request to "
287 MACSTR " dialog_token=%u req_mode=0x%x disassoc_timer=%u "
288 "validity_interval=%u",
289 MAC2STR(addr), dialog_token,
290 mgmt->u.action.u.bss_tm_req.req_mode,
291 le_to_host16(mgmt->u.action.u.bss_tm_req.disassoc_timer),
292 mgmt->u.action.u.bss_tm_req.validity_interval);
294 len = pos - &mgmt->u.action.category;
295 res = hostapd_drv_send_action(hapd, hapd->iface->freq, 0,
296 mgmt->da, &mgmt->u.action.category, len);
302 static void ieee802_11_rx_bss_trans_mgmt_query(struct hostapd_data *hapd,
303 const u8 *addr, const u8 *frm,
306 u8 dialog_token, reason;
310 wpa_printf(MSG_DEBUG, "WNM: Ignore too short BSS Transition Management Query from "
311 MACSTR, MAC2STR(addr));
317 dialog_token = *pos++;
320 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Query from "
321 MACSTR " dialog_token=%u reason=%u",
322 MAC2STR(addr), dialog_token, reason);
324 wpa_hexdump(MSG_DEBUG, "WNM: BSS Transition Candidate List Entries",
327 ieee802_11_send_bss_trans_mgmt_request(hapd, addr, dialog_token, NULL);
331 static void ieee802_11_rx_bss_trans_mgmt_resp(struct hostapd_data *hapd,
332 const u8 *addr, const u8 *frm,
335 u8 dialog_token, status_code, bss_termination_delay;
339 wpa_printf(MSG_DEBUG, "WNM: Ignore too short BSS Transition Management Response from "
340 MACSTR, MAC2STR(addr));
346 dialog_token = *pos++;
347 status_code = *pos++;
348 bss_termination_delay = *pos++;
350 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Response from "
351 MACSTR " dialog_token=%u status_code=%u "
352 "bss_termination_delay=%u", MAC2STR(addr), dialog_token,
353 status_code, bss_termination_delay);
355 if (status_code == WNM_BSS_TM_ACCEPT) {
356 if (end - pos < ETH_ALEN) {
357 wpa_printf(MSG_DEBUG, "WNM: not enough room for Target BSSID field");
360 wpa_printf(MSG_DEBUG, "WNM: Target BSSID: " MACSTR,
362 wpa_msg(hapd->msg_ctx, MSG_INFO, BSS_TM_RESP MACSTR
363 " status_code=%u bss_termination_delay=%u target_bssid="
365 MAC2STR(addr), status_code, bss_termination_delay,
369 wpa_msg(hapd->msg_ctx, MSG_INFO, BSS_TM_RESP MACSTR
370 " status_code=%u bss_termination_delay=%u",
371 MAC2STR(addr), status_code, bss_termination_delay);
374 wpa_hexdump(MSG_DEBUG, "WNM: BSS Transition Candidate List Entries",
379 int ieee802_11_rx_wnm_action_ap(struct hostapd_data *hapd,
380 const struct ieee80211_mgmt *mgmt, size_t len)
386 if (len < IEEE80211_HDRLEN + 2)
389 payload = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
391 plen = len - IEEE80211_HDRLEN - 2;
394 case WNM_BSS_TRANS_MGMT_QUERY:
395 ieee802_11_rx_bss_trans_mgmt_query(hapd, mgmt->sa, payload,
398 case WNM_BSS_TRANS_MGMT_RESP:
399 ieee802_11_rx_bss_trans_mgmt_resp(hapd, mgmt->sa, payload,
402 case WNM_SLEEP_MODE_REQ:
403 ieee802_11_rx_wnmsleep_req(hapd, mgmt->sa, payload, plen);
407 wpa_printf(MSG_DEBUG, "WNM: Unsupported WNM Action %u from " MACSTR,
408 action, MAC2STR(mgmt->sa));
413 int wnm_send_disassoc_imminent(struct hostapd_data *hapd,
414 struct sta_info *sta, int disassoc_timer)
417 struct ieee80211_mgmt *mgmt;
419 os_memset(buf, 0, sizeof(buf));
420 mgmt = (struct ieee80211_mgmt *) buf;
421 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
422 WLAN_FC_STYPE_ACTION);
423 os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
424 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
425 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
426 mgmt->u.action.category = WLAN_ACTION_WNM;
427 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
428 mgmt->u.action.u.bss_tm_req.dialog_token = 1;
429 mgmt->u.action.u.bss_tm_req.req_mode =
430 WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
431 mgmt->u.action.u.bss_tm_req.disassoc_timer =
432 host_to_le16(disassoc_timer);
433 mgmt->u.action.u.bss_tm_req.validity_interval = 0;
435 pos = mgmt->u.action.u.bss_tm_req.variable;
437 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Request frame to indicate imminent disassociation (disassoc_timer=%d) to "
438 MACSTR, disassoc_timer, MAC2STR(sta->addr));
439 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
440 wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
441 "Management Request frame");
449 static void set_disassoc_timer(struct hostapd_data *hapd, struct sta_info *sta,
452 int timeout, beacon_int;
455 * Prevent STA from reconnecting using cached PMKSA to force
456 * full authentication with the authentication server (which may
457 * decide to reject the connection),
459 wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
461 beacon_int = hapd->iconf->beacon_int;
463 beacon_int = 100; /* best guess */
464 /* Calculate timeout in ms based on beacon_int in TU */
465 timeout = disassoc_timer * beacon_int * 128 / 125;
466 wpa_printf(MSG_DEBUG, "Disassociation timer for " MACSTR
467 " set to %d ms", MAC2STR(sta->addr), timeout);
469 sta->timeout_next = STA_DISASSOC_FROM_CLI;
470 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
471 eloop_register_timeout(timeout / 1000,
472 timeout % 1000 * 1000,
473 ap_handle_timer, hapd, sta);
477 int wnm_send_ess_disassoc_imminent(struct hostapd_data *hapd,
478 struct sta_info *sta, const char *url,
482 struct ieee80211_mgmt *mgmt;
485 os_memset(buf, 0, sizeof(buf));
486 mgmt = (struct ieee80211_mgmt *) buf;
487 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
488 WLAN_FC_STYPE_ACTION);
489 os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
490 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
491 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
492 mgmt->u.action.category = WLAN_ACTION_WNM;
493 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
494 mgmt->u.action.u.bss_tm_req.dialog_token = 1;
495 mgmt->u.action.u.bss_tm_req.req_mode =
496 WNM_BSS_TM_REQ_DISASSOC_IMMINENT |
497 WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
498 mgmt->u.action.u.bss_tm_req.disassoc_timer =
499 host_to_le16(disassoc_timer);
500 mgmt->u.action.u.bss_tm_req.validity_interval = 0x01;
502 pos = mgmt->u.action.u.bss_tm_req.variable;
504 /* Session Information URL */
505 url_len = os_strlen(url);
509 os_memcpy(pos, url, url_len);
512 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
513 wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
514 "Management Request frame");
518 if (disassoc_timer) {
519 /* send disassociation frame after time-out */
520 set_disassoc_timer(hapd, sta, disassoc_timer);
527 int wnm_send_bss_tm_req(struct hostapd_data *hapd, struct sta_info *sta,
528 u8 req_mode, int disassoc_timer, u8 valid_int,
529 const u8 *bss_term_dur, const char *url,
530 const u8 *nei_rep, size_t nei_rep_len)
533 struct ieee80211_mgmt *mgmt;
536 wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Request to "
537 MACSTR " req_mode=0x%x disassoc_timer=%d valid_int=0x%x",
538 MAC2STR(sta->addr), req_mode, disassoc_timer, valid_int);
539 buf = os_zalloc(1000 + nei_rep_len);
542 mgmt = (struct ieee80211_mgmt *) buf;
543 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
544 WLAN_FC_STYPE_ACTION);
545 os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
546 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
547 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
548 mgmt->u.action.category = WLAN_ACTION_WNM;
549 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
550 mgmt->u.action.u.bss_tm_req.dialog_token = 1;
551 mgmt->u.action.u.bss_tm_req.req_mode = req_mode;
552 mgmt->u.action.u.bss_tm_req.disassoc_timer =
553 host_to_le16(disassoc_timer);
554 mgmt->u.action.u.bss_tm_req.validity_interval = valid_int;
556 pos = mgmt->u.action.u.bss_tm_req.variable;
558 if ((req_mode & WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED) &&
560 os_memcpy(pos, bss_term_dur, 12);
565 /* Session Information URL */
566 url_len = os_strlen(url);
573 os_memcpy(pos, url, url_len);
578 os_memcpy(pos, nei_rep, nei_rep_len);
582 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
583 wpa_printf(MSG_DEBUG,
584 "Failed to send BSS Transition Management Request frame");
590 if (disassoc_timer) {
591 /* send disassociation frame after time-out */
592 set_disassoc_timer(hapd, sta, disassoc_timer);