1/*
2 * hostapd - WNM
3 * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
4 *
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 "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
14#include "ap/hostapd.h"
15#include "ap/sta_info.h"
16#include "ap/ap_config.h"
17#include "ap/ap_drv_ops.h"
18#include "ap/wpa_auth.h"
19#include "wnm_ap.h"
20
21#define MAX_TFS_IE_LEN  1024
22
23
24/* get the TFS IE from driver */
25static int ieee80211_11_get_tfs_ie(struct hostapd_data *hapd, const u8 *addr,
26				   u8 *buf, u16 *buf_len, enum wnm_oper oper)
27{
28	wpa_printf(MSG_DEBUG, "%s: TFS get operation %d", __func__, oper);
29
30	return hostapd_drv_wnm_oper(hapd, oper, addr, buf, buf_len);
31}
32
33
34/* set the TFS IE to driver */
35static int ieee80211_11_set_tfs_ie(struct hostapd_data *hapd, const u8 *addr,
36				   u8 *buf, u16 *buf_len, enum wnm_oper oper)
37{
38	wpa_printf(MSG_DEBUG, "%s: TFS set operation %d", __func__, oper);
39
40	return hostapd_drv_wnm_oper(hapd, oper, addr, buf, buf_len);
41}
42
43
44/* MLME-SLEEPMODE.response */
45static int ieee802_11_send_wnmsleep_resp(struct hostapd_data *hapd,
46					 const u8 *addr, u8 dialog_token,
47					 u8 action_type, u16 intval)
48{
49	struct ieee80211_mgmt *mgmt;
50	int res;
51	size_t len;
52	size_t gtk_elem_len = 0;
53	size_t igtk_elem_len = 0;
54	struct wnm_sleep_element wnmsleep_ie;
55	u8 *wnmtfs_ie;
56	u8 wnmsleep_ie_len;
57	u16 wnmtfs_ie_len;
58	u8 *pos;
59	struct sta_info *sta;
60	enum wnm_oper tfs_oper = action_type == WNM_SLEEP_MODE_ENTER ?
61		WNM_SLEEP_TFS_RESP_IE_ADD : WNM_SLEEP_TFS_RESP_IE_NONE;
62
63	sta = ap_get_sta(hapd, addr);
64	if (sta == NULL) {
65		wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
66		return -EINVAL;
67	}
68
69	/* WNM-Sleep Mode IE */
70	os_memset(&wnmsleep_ie, 0, sizeof(struct wnm_sleep_element));
71	wnmsleep_ie_len = sizeof(struct wnm_sleep_element);
72	wnmsleep_ie.eid = WLAN_EID_WNMSLEEP;
73	wnmsleep_ie.len = wnmsleep_ie_len - 2;
74	wnmsleep_ie.action_type = action_type;
75	wnmsleep_ie.status = WNM_STATUS_SLEEP_ACCEPT;
76	wnmsleep_ie.intval = host_to_le16(intval);
77
78	/* TFS IE(s) */
79	wnmtfs_ie = os_zalloc(MAX_TFS_IE_LEN);
80	if (wnmtfs_ie == NULL)
81		return -1;
82	if (ieee80211_11_get_tfs_ie(hapd, addr, wnmtfs_ie, &wnmtfs_ie_len,
83				    tfs_oper)) {
84		wnmtfs_ie_len = 0;
85		os_free(wnmtfs_ie);
86		wnmtfs_ie = NULL;
87	}
88
89#define MAX_GTK_SUBELEM_LEN 45
90#define MAX_IGTK_SUBELEM_LEN 26
91	mgmt = os_zalloc(sizeof(*mgmt) + wnmsleep_ie_len +
92			 MAX_GTK_SUBELEM_LEN + MAX_IGTK_SUBELEM_LEN);
93	if (mgmt == NULL) {
94		wpa_printf(MSG_DEBUG, "MLME: Failed to allocate buffer for "
95			   "WNM-Sleep Response action frame");
96		return -1;
97	}
98	os_memcpy(mgmt->da, addr, ETH_ALEN);
99	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
100	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
101	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
102					   WLAN_FC_STYPE_ACTION);
103	mgmt->u.action.category = WLAN_ACTION_WNM;
104	mgmt->u.action.u.wnm_sleep_resp.action = WNM_SLEEP_MODE_RESP;
105	mgmt->u.action.u.wnm_sleep_resp.dialogtoken = dialog_token;
106	pos = (u8 *)mgmt->u.action.u.wnm_sleep_resp.variable;
107	/* add key data if MFP is enabled */
108	if (!wpa_auth_uses_mfp(sta->wpa_sm) ||
109	    action_type != WNM_SLEEP_MODE_EXIT) {
110		mgmt->u.action.u.wnm_sleep_resp.keydata_len = 0;
111	} else {
112		gtk_elem_len = wpa_wnmsleep_gtk_subelem(sta->wpa_sm, pos);
113		pos += gtk_elem_len;
114		wpa_printf(MSG_DEBUG, "Pass 4, gtk_len = %d",
115			   (int) gtk_elem_len);
116#ifdef CONFIG_IEEE80211W
117		res = wpa_wnmsleep_igtk_subelem(sta->wpa_sm, pos);
118		if (res < 0) {
119			os_free(wnmtfs_ie);
120			os_free(mgmt);
121			return -1;
122		}
123		igtk_elem_len = res;
124		pos += igtk_elem_len;
125		wpa_printf(MSG_DEBUG, "Pass 4 igtk_len = %d",
126			   (int) igtk_elem_len);
127#endif /* CONFIG_IEEE80211W */
128
129		WPA_PUT_LE16((u8 *)
130			     &mgmt->u.action.u.wnm_sleep_resp.keydata_len,
131			     gtk_elem_len + igtk_elem_len);
132	}
133	os_memcpy(pos, &wnmsleep_ie, wnmsleep_ie_len);
134	/* copy TFS IE here */
135	pos += wnmsleep_ie_len;
136	if (wnmtfs_ie)
137		os_memcpy(pos, wnmtfs_ie, wnmtfs_ie_len);
138
139	len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_resp) + gtk_elem_len +
140		igtk_elem_len + wnmsleep_ie_len + wnmtfs_ie_len;
141
142	/* In driver, response frame should be forced to sent when STA is in
143	 * PS mode */
144	res = hostapd_drv_send_action(hapd, hapd->iface->freq, 0,
145				      mgmt->da, &mgmt->u.action.category, len);
146
147	if (!res) {
148		wpa_printf(MSG_DEBUG, "Successfully send WNM-Sleep Response "
149			   "frame");
150
151		/* when entering wnmsleep
152		 * 1. pause the node in driver
153		 * 2. mark the node so that AP won't update GTK/IGTK during
154		 * WNM Sleep
155		 */
156		if (wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT &&
157		    wnmsleep_ie.action_type == WNM_SLEEP_MODE_ENTER) {
158			sta->flags |= WLAN_STA_WNM_SLEEP_MODE;
159			hostapd_drv_wnm_oper(hapd, WNM_SLEEP_ENTER_CONFIRM,
160					     addr, NULL, NULL);
161			wpa_set_wnmsleep(sta->wpa_sm, 1);
162		}
163		/* when exiting wnmsleep
164		 * 1. unmark the node
165		 * 2. start GTK/IGTK update if MFP is not used
166		 * 3. unpause the node in driver
167		 */
168		if ((wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT ||
169		     wnmsleep_ie.status ==
170		     WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) &&
171		    wnmsleep_ie.action_type == WNM_SLEEP_MODE_EXIT) {
172			sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
173			wpa_set_wnmsleep(sta->wpa_sm, 0);
174			hostapd_drv_wnm_oper(hapd, WNM_SLEEP_EXIT_CONFIRM,
175					     addr, NULL, NULL);
176			if (!wpa_auth_uses_mfp(sta->wpa_sm))
177				wpa_wnmsleep_rekey_gtk(sta->wpa_sm);
178		}
179	} else
180		wpa_printf(MSG_DEBUG, "Fail to send WNM-Sleep Response frame");
181
182#undef MAX_GTK_SUBELEM_LEN
183#undef MAX_IGTK_SUBELEM_LEN
184	os_free(wnmtfs_ie);
185	os_free(mgmt);
186	return res;
187}
188
189
190static void ieee802_11_rx_wnmsleep_req(struct hostapd_data *hapd,
191				       const u8 *addr, const u8 *frm, int len)
192{
193	/* Dialog Token [1] | WNM-Sleep Mode IE | TFS Response IE */
194	const u8 *pos = frm;
195	u8 dialog_token;
196	struct wnm_sleep_element *wnmsleep_ie = NULL;
197	/* multiple TFS Req IE (assuming consecutive) */
198	u8 *tfsreq_ie_start = NULL;
199	u8 *tfsreq_ie_end = NULL;
200	u16 tfsreq_ie_len = 0;
201
202	dialog_token = *pos++;
203	while (pos + 1 < frm + len) {
204		u8 ie_len = pos[1];
205		if (pos + 2 + ie_len > frm + len)
206			break;
207		if (*pos == WLAN_EID_WNMSLEEP)
208			wnmsleep_ie = (struct wnm_sleep_element *) pos;
209		else if (*pos == WLAN_EID_TFS_REQ) {
210			if (!tfsreq_ie_start)
211				tfsreq_ie_start = (u8 *) pos;
212			tfsreq_ie_end = (u8 *) pos;
213		} else
214			wpa_printf(MSG_DEBUG, "WNM: EID %d not recognized",
215				   *pos);
216		pos += ie_len + 2;
217	}
218
219	if (!wnmsleep_ie) {
220		wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
221		return;
222	}
223
224	if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_ENTER &&
225	    tfsreq_ie_start && tfsreq_ie_end &&
226	    tfsreq_ie_end - tfsreq_ie_start >= 0) {
227		tfsreq_ie_len = (tfsreq_ie_end + tfsreq_ie_end[1] + 2) -
228			tfsreq_ie_start;
229		wpa_printf(MSG_DEBUG, "TFS Req IE(s) found");
230		/* pass the TFS Req IE(s) to driver for processing */
231		if (ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
232					    &tfsreq_ie_len,
233					    WNM_SLEEP_TFS_REQ_IE_SET))
234			wpa_printf(MSG_DEBUG, "Fail to set TFS Req IE");
235	}
236
237	ieee802_11_send_wnmsleep_resp(hapd, addr, dialog_token,
238				      wnmsleep_ie->action_type,
239				      le_to_host16(wnmsleep_ie->intval));
240
241	if (wnmsleep_ie->action_type == WNM_SLEEP_MODE_EXIT) {
242		/* clear the tfs after sending the resp frame */
243		ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
244					&tfsreq_ie_len, WNM_SLEEP_TFS_IE_DEL);
245	}
246}
247
248
249static int ieee802_11_send_bss_trans_mgmt_request(struct hostapd_data *hapd,
250						  const u8 *addr,
251						  u8 dialog_token,
252						  const char *url)
253{
254	struct ieee80211_mgmt *mgmt;
255	size_t url_len, len;
256	u8 *pos;
257	int res;
258
259	if (url)
260		url_len = os_strlen(url);
261	else
262		url_len = 0;
263
264	mgmt = os_zalloc(sizeof(*mgmt) + (url_len ? 1 + url_len : 0));
265	if (mgmt == NULL)
266		return -1;
267	os_memcpy(mgmt->da, addr, ETH_ALEN);
268	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
269	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
270	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
271					   WLAN_FC_STYPE_ACTION);
272	mgmt->u.action.category = WLAN_ACTION_WNM;
273	mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
274	mgmt->u.action.u.bss_tm_req.dialog_token = dialog_token;
275	mgmt->u.action.u.bss_tm_req.req_mode = 0;
276	mgmt->u.action.u.bss_tm_req.disassoc_timer = host_to_le16(0);
277	mgmt->u.action.u.bss_tm_req.validity_interval = 1;
278	pos = mgmt->u.action.u.bss_tm_req.variable;
279	if (url) {
280		*pos++ += url_len;
281		os_memcpy(pos, url, url_len);
282		pos += url_len;
283	}
284
285	wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Request to "
286		   MACSTR " dialog_token=%u req_mode=0x%x disassoc_timer=%u "
287		   "validity_interval=%u",
288		   MAC2STR(addr), dialog_token,
289		   mgmt->u.action.u.bss_tm_req.req_mode,
290		   le_to_host16(mgmt->u.action.u.bss_tm_req.disassoc_timer),
291		   mgmt->u.action.u.bss_tm_req.validity_interval);
292
293	len = pos - &mgmt->u.action.category;
294	res = hostapd_drv_send_action(hapd, hapd->iface->freq, 0,
295				      mgmt->da, &mgmt->u.action.category, len);
296	os_free(mgmt);
297	return res;
298}
299
300
301static void ieee802_11_rx_bss_trans_mgmt_query(struct hostapd_data *hapd,
302					       const u8 *addr, const u8 *frm,
303					       size_t len)
304{
305	u8 dialog_token, reason;
306	const u8 *pos, *end;
307
308	if (len < 2) {
309		wpa_printf(MSG_DEBUG, "WNM: Ignore too short BSS Transition Management Query from "
310			   MACSTR, MAC2STR(addr));
311		return;
312	}
313
314	pos = frm;
315	end = pos + len;
316	dialog_token = *pos++;
317	reason = *pos++;
318
319	wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Query from "
320		   MACSTR " dialog_token=%u reason=%u",
321		   MAC2STR(addr), dialog_token, reason);
322
323	wpa_hexdump(MSG_DEBUG, "WNM: BSS Transition Candidate List Entries",
324		    pos, end - pos);
325
326	ieee802_11_send_bss_trans_mgmt_request(hapd, addr, dialog_token, NULL);
327}
328
329
330static void ieee802_11_rx_bss_trans_mgmt_resp(struct hostapd_data *hapd,
331					      const u8 *addr, const u8 *frm,
332					      size_t len)
333{
334	u8 dialog_token, status_code, bss_termination_delay;
335	const u8 *pos, *end;
336
337	if (len < 3) {
338		wpa_printf(MSG_DEBUG, "WNM: Ignore too short BSS Transition Management Response from "
339			   MACSTR, MAC2STR(addr));
340		return;
341	}
342
343	pos = frm;
344	end = pos + len;
345	dialog_token = *pos++;
346	status_code = *pos++;
347	bss_termination_delay = *pos++;
348
349	wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management Response from "
350		   MACSTR " dialog_token=%u status_code=%u "
351		   "bss_termination_delay=%u", MAC2STR(addr), dialog_token,
352		   status_code, bss_termination_delay);
353
354	if (status_code == WNM_BSS_TM_ACCEPT) {
355		if (end - pos < ETH_ALEN) {
356			wpa_printf(MSG_DEBUG, "WNM: not enough room for Target BSSID field");
357			return;
358		}
359		wpa_printf(MSG_DEBUG, "WNM: Target BSSID: " MACSTR,
360			   MAC2STR(pos));
361		pos += ETH_ALEN;
362	}
363
364	wpa_hexdump(MSG_DEBUG, "WNM: BSS Transition Candidate List Entries",
365		    pos, end - pos);
366}
367
368
369int ieee802_11_rx_wnm_action_ap(struct hostapd_data *hapd,
370				const struct ieee80211_mgmt *mgmt, size_t len)
371{
372	u8 action;
373	const u8 *payload;
374	size_t plen;
375
376	if (len < IEEE80211_HDRLEN + 2)
377		return -1;
378
379	payload = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 1;
380	action = *payload++;
381	plen = len - IEEE80211_HDRLEN - 2;
382
383	switch (action) {
384	case WNM_BSS_TRANS_MGMT_QUERY:
385		ieee802_11_rx_bss_trans_mgmt_query(hapd, mgmt->sa, payload,
386						   plen);
387		return 0;
388	case WNM_BSS_TRANS_MGMT_RESP:
389		ieee802_11_rx_bss_trans_mgmt_resp(hapd, mgmt->sa, payload,
390						  plen);
391		return 0;
392	case WNM_SLEEP_MODE_REQ:
393		ieee802_11_rx_wnmsleep_req(hapd, mgmt->sa, payload, plen);
394		return 0;
395	}
396
397	wpa_printf(MSG_DEBUG, "WNM: Unsupported WNM Action %u from " MACSTR,
398		   action, MAC2STR(mgmt->sa));
399	return -1;
400}
401
402
403int wnm_send_disassoc_imminent(struct hostapd_data *hapd,
404			       struct sta_info *sta, int disassoc_timer)
405{
406	u8 buf[1000], *pos;
407	struct ieee80211_mgmt *mgmt;
408
409	os_memset(buf, 0, sizeof(buf));
410	mgmt = (struct ieee80211_mgmt *) buf;
411	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
412					   WLAN_FC_STYPE_ACTION);
413	os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
414	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
415	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
416	mgmt->u.action.category = WLAN_ACTION_WNM;
417	mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
418	mgmt->u.action.u.bss_tm_req.dialog_token = 1;
419	mgmt->u.action.u.bss_tm_req.req_mode =
420		WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
421	mgmt->u.action.u.bss_tm_req.disassoc_timer =
422		host_to_le16(disassoc_timer);
423	mgmt->u.action.u.bss_tm_req.validity_interval = 0;
424
425	pos = mgmt->u.action.u.bss_tm_req.variable;
426
427	wpa_printf(MSG_DEBUG, "WNM: Send BSS Transition Management Request frame to indicate imminent disassociation (disassoc_timer=%d) to "
428		   MACSTR, disassoc_timer, MAC2STR(sta->addr));
429	if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
430		wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
431			   "Management Request frame");
432		return -1;
433	}
434
435	return 0;
436}
437
438
439int wnm_send_ess_disassoc_imminent(struct hostapd_data *hapd,
440				   struct sta_info *sta, const char *url,
441				   int disassoc_timer)
442{
443	u8 buf[1000], *pos;
444	struct ieee80211_mgmt *mgmt;
445	size_t url_len;
446
447	os_memset(buf, 0, sizeof(buf));
448	mgmt = (struct ieee80211_mgmt *) buf;
449	mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
450					   WLAN_FC_STYPE_ACTION);
451	os_memcpy(mgmt->da, sta->addr, ETH_ALEN);
452	os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
453	os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
454	mgmt->u.action.category = WLAN_ACTION_WNM;
455	mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
456	mgmt->u.action.u.bss_tm_req.dialog_token = 1;
457	mgmt->u.action.u.bss_tm_req.req_mode =
458		WNM_BSS_TM_REQ_DISASSOC_IMMINENT |
459		WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
460	mgmt->u.action.u.bss_tm_req.disassoc_timer =
461		host_to_le16(disassoc_timer);
462	mgmt->u.action.u.bss_tm_req.validity_interval = 0x01;
463
464	pos = mgmt->u.action.u.bss_tm_req.variable;
465
466	/* Session Information URL */
467	url_len = os_strlen(url);
468	if (url_len > 255)
469		return -1;
470	*pos++ = url_len;
471	os_memcpy(pos, url, url_len);
472	pos += url_len;
473
474	if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
475		wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
476			   "Management Request frame");
477		return -1;
478	}
479
480	/* send disassociation frame after time-out */
481	if (disassoc_timer) {
482		int timeout, beacon_int;
483
484		/*
485		 * Prevent STA from reconnecting using cached PMKSA to force
486		 * full authentication with the authentication server (which may
487		 * decide to reject the connection),
488		 */
489		wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
490
491		beacon_int = hapd->iconf->beacon_int;
492		if (beacon_int < 1)
493			beacon_int = 100; /* best guess */
494		/* Calculate timeout in ms based on beacon_int in TU */
495		timeout = disassoc_timer * beacon_int * 128 / 125;
496		wpa_printf(MSG_DEBUG, "Disassociation timer for " MACSTR
497			   " set to %d ms", MAC2STR(sta->addr), timeout);
498
499		sta->timeout_next = STA_DISASSOC_FROM_CLI;
500		eloop_cancel_timeout(ap_handle_timer, hapd, sta);
501		eloop_register_timeout(timeout / 1000,
502				       timeout % 1000 * 1000,
503				       ap_handle_timer, hapd, sta);
504	}
505
506	return 0;
507}
508