wnm_ap.c revision 61d9df3e62aaa0e87ad05452fcb95142159a17b6
1/*
2 * hostapd - WNM
3 * Copyright (c) 2011-2012, 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 "common/ieee802_11_defs.h"
13#include "ap/hostapd.h"
14#include "ap/sta_info.h"
15#include "ap/ap_config.h"
16#include "ap/ap_drv_ops.h"
17#include "ap/wpa_auth.h"
18#include "wnm_ap.h"
19
20#define MAX_TFS_IE_LEN  1024
21
22#ifdef CONFIG_IEEE80211V
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 == 0 ? WNM_SLEEP_TFS_RESP_IE_ADD :
61		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 = 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) || action_type != 1){
109		mgmt->u.action.u.wnm_sleep_resp.keydata_len = 0;
110	} else {
111		gtk_elem_len = wpa_wnmsleep_gtk_subelem(sta->wpa_sm, pos);
112		pos += gtk_elem_len;
113		wpa_printf(MSG_DEBUG, "Pass 4, gtk_len = %d",
114			   (int) gtk_elem_len);
115#ifdef CONFIG_IEEE80211W
116		res = wpa_wnmsleep_igtk_subelem(sta->wpa_sm, pos);
117		if (res < 0) {
118			os_free(wnmtfs_ie);
119			os_free(mgmt);
120			return -1;
121		}
122		igtk_elem_len = res;
123		pos += igtk_elem_len;
124		wpa_printf(MSG_DEBUG, "Pass 4 igtk_len = %d",
125			   (int) igtk_elem_len);
126#endif /* CONFIG_IEEE80211W */
127
128		WPA_PUT_LE16((u8 *)
129			     &mgmt->u.action.u.wnm_sleep_resp.keydata_len,
130			     gtk_elem_len + igtk_elem_len);
131	}
132	os_memcpy(pos, &wnmsleep_ie, wnmsleep_ie_len);
133	/* copy TFS IE here */
134	pos += wnmsleep_ie_len;
135	os_memcpy(pos, wnmtfs_ie, wnmtfs_ie_len);
136
137	len = 1 + sizeof(mgmt->u.action.u.wnm_sleep_resp) + gtk_elem_len +
138		igtk_elem_len + wnmsleep_ie_len + wnmtfs_ie_len;
139
140	/* In driver, response frame should be forced to sent when STA is in
141	 * PS mode */
142	res = hostapd_drv_send_action(hapd, hapd->iface->freq, 0,
143				      mgmt->da, &mgmt->u.action.category, len);
144
145	if (!res) {
146		wpa_printf(MSG_DEBUG, "Successfully send WNM-Sleep Response "
147			   "frame");
148
149		/* when entering wnmsleep
150		 * 1. pause the node in driver
151		 * 2. mark the node so that AP won't update GTK/IGTK during
152		 * WNM Sleep
153		 */
154		if (wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT &&
155		    wnmsleep_ie.action_type == 0) {
156			hostapd_drv_wnm_oper(hapd, WNM_SLEEP_ENTER_CONFIRM,
157					     addr, NULL, NULL);
158			wpa_set_wnmsleep(sta->wpa_sm, 1);
159		}
160		/* when exiting wnmsleep
161		 * 1. unmark the node
162		 * 2. start GTK/IGTK update if MFP is not used
163		 * 3. unpause the node in driver
164		 */
165		if (wnmsleep_ie.status == WNM_STATUS_SLEEP_ACCEPT &&
166		    wnmsleep_ie.action_type == 1) {
167			wpa_set_wnmsleep(sta->wpa_sm, 0);
168			hostapd_drv_wnm_oper(hapd, WNM_SLEEP_EXIT_CONFIRM,
169					     addr, NULL, NULL);
170			if (wpa_auth_uses_mfp(sta->wpa_sm) && action_type == 1)
171				wpa_wnmsleep_rekey_gtk(sta->wpa_sm);
172		}
173	} else
174		wpa_printf(MSG_DEBUG, "Fail to send WNM-Sleep Response frame");
175
176#undef MAX_GTK_SUBELEM_LEN
177#undef MAX_IGTK_SUBELEM_LEN
178	os_free(wnmtfs_ie);
179	os_free(mgmt);
180	return res;
181}
182
183
184static void ieee802_11_rx_wnmsleep_req(struct hostapd_data *hapd,
185				       const u8 *addr, const u8 *frm, int len)
186{
187	/*
188	 * Action [1] | Dialog Token [1] | WNM-Sleep Mode IE |
189	 * TFS Response IE
190	 */
191	u8 *pos = (u8 *) frm; /* point to action field */
192	u8 dialog_token = pos[1];
193	struct wnm_sleep_element *wnmsleep_ie = NULL;
194	/* multiple TFS Req IE (assuming consecutive) */
195	u8 *tfsreq_ie_start = NULL;
196	u8 *tfsreq_ie_end = NULL;
197	u16 tfsreq_ie_len = 0;
198
199	pos += 1 + 1;
200	while (pos - frm < len - 1) {
201		u8 ie_len = *(pos+1);
202		if (*pos == WLAN_EID_WNMSLEEP)
203			wnmsleep_ie = (struct wnm_sleep_element *)pos;
204		else if (*pos == WLAN_EID_TFS_REQ) {
205			if (!tfsreq_ie_start)
206				tfsreq_ie_start = pos;
207			tfsreq_ie_end = pos;
208		} else
209			wpa_printf(MSG_DEBUG, "EID %d not recognized", *pos);
210		pos += ie_len + 2;
211	}
212
213	if (!wnmsleep_ie) {
214		wpa_printf(MSG_DEBUG, "No WNM-Sleep IE found");
215		return;
216	}
217
218	if (wnmsleep_ie->action_type == 0 && tfsreq_ie_start &&
219	    tfsreq_ie_end && tfsreq_ie_end - tfsreq_ie_start >= 0) {
220		tfsreq_ie_len = (tfsreq_ie_end + tfsreq_ie_end[1] + 2) -
221			tfsreq_ie_start;
222		wpa_printf(MSG_DEBUG, "TFS Req IE(s) found");
223		/* pass the TFS Req IE(s) to driver for processing */
224		if (ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
225					    &tfsreq_ie_len,
226					    WNM_SLEEP_TFS_REQ_IE_SET))
227			wpa_printf(MSG_DEBUG, "Fail to set TFS Req IE");
228	}
229
230	ieee802_11_send_wnmsleep_resp(hapd, addr, dialog_token,
231				      wnmsleep_ie->action_type,
232				      wnmsleep_ie->intval);
233
234	if (wnmsleep_ie->action_type == 1) {
235		/* clear the tfs after sending the resp frame */
236		ieee80211_11_set_tfs_ie(hapd, addr, tfsreq_ie_start,
237					&tfsreq_ie_len, WNM_SLEEP_TFS_IE_DEL);
238	}
239}
240
241
242void ieee802_11_rx_wnm_action_ap(struct hostapd_data *hapd,
243				 struct rx_action *action)
244{
245	u8 *pos = (u8 *) action->data + 1; /* point to the action field */
246	u8 act = *pos;
247
248	switch (act) {
249	case WNM_SLEEP_MODE_REQ:
250		ieee802_11_rx_wnmsleep_req(hapd, action->sa, action->data + 1,
251					   action->len);
252		break;
253	default:
254		break;
255	}
256}
257
258#endif /* CONFIG_IEEE80211V */
259