xref: /freebsd/contrib/wpa/src/ap/wmm.c (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
1e28a4053SRui Paulo /*
2e28a4053SRui Paulo  * hostapd / WMM (Wi-Fi Multimedia)
3e28a4053SRui Paulo  * Copyright 2002-2003, Instant802 Networks, Inc.
4e28a4053SRui Paulo  * Copyright 2005-2006, Devicescape Software, Inc.
5e28a4053SRui Paulo  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
6e28a4053SRui Paulo  *
75b9c547cSRui Paulo  * This software may be distributed under the terms of the BSD license.
85b9c547cSRui Paulo  * See README for more details.
9e28a4053SRui Paulo  */
10e28a4053SRui Paulo 
11e28a4053SRui Paulo #include "utils/includes.h"
12e28a4053SRui Paulo 
13e28a4053SRui Paulo #include "utils/common.h"
14e28a4053SRui Paulo #include "common/ieee802_11_defs.h"
15e28a4053SRui Paulo #include "common/ieee802_11_common.h"
16e28a4053SRui Paulo #include "hostapd.h"
17e28a4053SRui Paulo #include "ieee802_11.h"
18e28a4053SRui Paulo #include "sta_info.h"
19e28a4053SRui Paulo #include "ap_config.h"
20f05cddf9SRui Paulo #include "ap_drv_ops.h"
21e28a4053SRui Paulo #include "wmm.h"
22e28a4053SRui Paulo 
23206b73d0SCy Schubert #ifndef MIN
24206b73d0SCy Schubert #define MIN(a, b) (((a) < (b)) ? (a) : (b))
25206b73d0SCy Schubert #endif
26206b73d0SCy Schubert #ifndef MAX
27206b73d0SCy Schubert #define MAX(a, b) (((a) > (b)) ? (a) : (b))
28206b73d0SCy Schubert #endif
29206b73d0SCy Schubert 
30e28a4053SRui Paulo 
31e28a4053SRui Paulo static inline u8 wmm_aci_aifsn(int aifsn, int acm, int aci)
32e28a4053SRui Paulo {
33e28a4053SRui Paulo 	u8 ret;
34e28a4053SRui Paulo 	ret = (aifsn << WMM_AC_AIFNS_SHIFT) & WMM_AC_AIFSN_MASK;
35e28a4053SRui Paulo 	if (acm)
36e28a4053SRui Paulo 		ret |= WMM_AC_ACM;
37e28a4053SRui Paulo 	ret |= (aci << WMM_AC_ACI_SHIFT) & WMM_AC_ACI_MASK;
38e28a4053SRui Paulo 	return ret;
39e28a4053SRui Paulo }
40e28a4053SRui Paulo 
41e28a4053SRui Paulo 
42e28a4053SRui Paulo static inline u8 wmm_ecw(int ecwmin, int ecwmax)
43e28a4053SRui Paulo {
44e28a4053SRui Paulo 	return ((ecwmin << WMM_AC_ECWMIN_SHIFT) & WMM_AC_ECWMIN_MASK) |
45e28a4053SRui Paulo 		((ecwmax << WMM_AC_ECWMAX_SHIFT) & WMM_AC_ECWMAX_MASK);
46e28a4053SRui Paulo }
47e28a4053SRui Paulo 
48e28a4053SRui Paulo 
49206b73d0SCy Schubert static void
50206b73d0SCy Schubert wmm_set_regulatory_limit(const struct hostapd_wmm_ac_params *wmm_conf,
51206b73d0SCy Schubert 			 struct hostapd_wmm_ac_params *wmm,
52206b73d0SCy Schubert 			 const struct hostapd_wmm_rule *wmm_reg)
53206b73d0SCy Schubert {
54206b73d0SCy Schubert 	int ac;
55206b73d0SCy Schubert 
56206b73d0SCy Schubert 	for (ac = 0; ac < WMM_AC_NUM; ac++) {
57206b73d0SCy Schubert 		wmm[ac].cwmin = MAX(wmm_conf[ac].cwmin, wmm_reg[ac].min_cwmin);
58206b73d0SCy Schubert 		wmm[ac].cwmax = MAX(wmm_conf[ac].cwmax, wmm_reg[ac].min_cwmax);
59206b73d0SCy Schubert 		wmm[ac].aifs = MAX(wmm_conf[ac].aifs, wmm_reg[ac].min_aifs);
60206b73d0SCy Schubert 		wmm[ac].txop_limit =
61206b73d0SCy Schubert 			MIN(wmm_conf[ac].txop_limit, wmm_reg[ac].max_txop);
62206b73d0SCy Schubert 		wmm[ac].admission_control_mandatory =
63206b73d0SCy Schubert 			wmm_conf[ac].admission_control_mandatory;
64206b73d0SCy Schubert 	}
65206b73d0SCy Schubert }
66206b73d0SCy Schubert 
67206b73d0SCy Schubert 
68206b73d0SCy Schubert /*
69206b73d0SCy Schubert  * Calculate WMM regulatory limit if any.
70206b73d0SCy Schubert  */
71206b73d0SCy Schubert static void wmm_calc_regulatory_limit(struct hostapd_data *hapd,
72206b73d0SCy Schubert 				      struct hostapd_wmm_ac_params *acp)
73206b73d0SCy Schubert {
74206b73d0SCy Schubert 	struct hostapd_hw_modes *mode = hapd->iface->current_mode;
75206b73d0SCy Schubert 	int c;
76206b73d0SCy Schubert 
77206b73d0SCy Schubert 	os_memcpy(acp, hapd->iconf->wmm_ac_params,
78206b73d0SCy Schubert 		  sizeof(hapd->iconf->wmm_ac_params));
79206b73d0SCy Schubert 
80206b73d0SCy Schubert 	for (c = 0; mode && c < mode->num_channels; c++) {
81206b73d0SCy Schubert 		struct hostapd_channel_data *chan = &mode->channels[c];
82206b73d0SCy Schubert 
83206b73d0SCy Schubert 		if (chan->freq != hapd->iface->freq)
84206b73d0SCy Schubert 			continue;
85206b73d0SCy Schubert 
86206b73d0SCy Schubert 		if (chan->wmm_rules_valid)
87206b73d0SCy Schubert 			wmm_set_regulatory_limit(hapd->iconf->wmm_ac_params,
88206b73d0SCy Schubert 						 acp, chan->wmm_rules);
89206b73d0SCy Schubert 		break;
90206b73d0SCy Schubert 	}
91206b73d0SCy Schubert 
92206b73d0SCy Schubert 	/*
93206b73d0SCy Schubert 	 * Check if we need to update set count. Since both were initialized to
94206b73d0SCy Schubert 	 * zero we can compare the whole array in one shot.
95206b73d0SCy Schubert 	 */
96206b73d0SCy Schubert 	if (os_memcmp(acp, hapd->iface->prev_wmm,
97206b73d0SCy Schubert 		      sizeof(hapd->iconf->wmm_ac_params)) != 0) {
98206b73d0SCy Schubert 		os_memcpy(hapd->iface->prev_wmm, acp,
99206b73d0SCy Schubert 			  sizeof(hapd->iconf->wmm_ac_params));
100206b73d0SCy Schubert 		hapd->parameter_set_count++;
101206b73d0SCy Schubert 	}
102206b73d0SCy Schubert }
103206b73d0SCy Schubert 
104206b73d0SCy Schubert 
105e28a4053SRui Paulo /*
106e28a4053SRui Paulo  * Add WMM Parameter Element to Beacon, Probe Response, and (Re)Association
107e28a4053SRui Paulo  * Response frames.
108e28a4053SRui Paulo  */
109e28a4053SRui Paulo u8 * hostapd_eid_wmm(struct hostapd_data *hapd, u8 *eid)
110e28a4053SRui Paulo {
111e28a4053SRui Paulo 	u8 *pos = eid;
112e28a4053SRui Paulo 	struct wmm_parameter_element *wmm =
113e28a4053SRui Paulo 		(struct wmm_parameter_element *) (pos + 2);
114*c1d255d3SCy Schubert 	struct hostapd_wmm_ac_params wmmp[WMM_AC_NUM];
115e28a4053SRui Paulo 	int e;
116e28a4053SRui Paulo 
117*c1d255d3SCy Schubert 	os_memset(wmmp, 0, sizeof(wmmp));
118*c1d255d3SCy Schubert 
119e28a4053SRui Paulo 	if (!hapd->conf->wmm_enabled)
120e28a4053SRui Paulo 		return eid;
121206b73d0SCy Schubert 	wmm_calc_regulatory_limit(hapd, wmmp);
122e28a4053SRui Paulo 	eid[0] = WLAN_EID_VENDOR_SPECIFIC;
123e28a4053SRui Paulo 	wmm->oui[0] = 0x00;
124e28a4053SRui Paulo 	wmm->oui[1] = 0x50;
125e28a4053SRui Paulo 	wmm->oui[2] = 0xf2;
126e28a4053SRui Paulo 	wmm->oui_type = WMM_OUI_TYPE;
127e28a4053SRui Paulo 	wmm->oui_subtype = WMM_OUI_SUBTYPE_PARAMETER_ELEMENT;
128e28a4053SRui Paulo 	wmm->version = WMM_VERSION;
129e28a4053SRui Paulo 	wmm->qos_info = hapd->parameter_set_count & 0xf;
130e28a4053SRui Paulo 
131f05cddf9SRui Paulo 	if (hapd->conf->wmm_uapsd &&
132f05cddf9SRui Paulo 	    (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
133e28a4053SRui Paulo 		wmm->qos_info |= 0x80;
134e28a4053SRui Paulo 
135f05cddf9SRui Paulo 	wmm->reserved = 0;
136f05cddf9SRui Paulo 
137e28a4053SRui Paulo 	/* fill in a parameter set record for each AC */
138e28a4053SRui Paulo 	for (e = 0; e < 4; e++) {
139e28a4053SRui Paulo 		struct wmm_ac_parameter *ac = &wmm->ac[e];
140206b73d0SCy Schubert 		struct hostapd_wmm_ac_params *acp = &wmmp[e];
141e28a4053SRui Paulo 
142e28a4053SRui Paulo 		ac->aci_aifsn = wmm_aci_aifsn(acp->aifs,
143e28a4053SRui Paulo 					      acp->admission_control_mandatory,
144e28a4053SRui Paulo 					      e);
145e28a4053SRui Paulo 		ac->cw = wmm_ecw(acp->cwmin, acp->cwmax);
146e28a4053SRui Paulo 		ac->txop_limit = host_to_le16(acp->txop_limit);
147e28a4053SRui Paulo 	}
148e28a4053SRui Paulo 
149e28a4053SRui Paulo 	pos = (u8 *) (wmm + 1);
150e28a4053SRui Paulo 	eid[1] = pos - eid - 2; /* element length */
151e28a4053SRui Paulo 
152e28a4053SRui Paulo 	return pos;
153e28a4053SRui Paulo }
154e28a4053SRui Paulo 
155e28a4053SRui Paulo 
156f05cddf9SRui Paulo /*
157f05cddf9SRui Paulo  * This function is called when a station sends an association request with
158f05cddf9SRui Paulo  * WMM info element. The function returns 1 on success or 0 on any error in WMM
159f05cddf9SRui Paulo  * element. eid does not include Element ID and Length octets.
160f05cddf9SRui Paulo  */
161e28a4053SRui Paulo int hostapd_eid_wmm_valid(struct hostapd_data *hapd, const u8 *eid, size_t len)
162e28a4053SRui Paulo {
163e28a4053SRui Paulo 	struct wmm_information_element *wmm;
164e28a4053SRui Paulo 
165e28a4053SRui Paulo 	wpa_hexdump(MSG_MSGDUMP, "WMM IE", eid, len);
166e28a4053SRui Paulo 
167e28a4053SRui Paulo 	if (len < sizeof(struct wmm_information_element)) {
168e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "Too short WMM IE (len=%lu)",
169e28a4053SRui Paulo 			   (unsigned long) len);
170f05cddf9SRui Paulo 		return 0;
171e28a4053SRui Paulo 	}
172e28a4053SRui Paulo 
173e28a4053SRui Paulo 	wmm = (struct wmm_information_element *) eid;
174e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "Validating WMM IE: OUI %02x:%02x:%02x  "
175e28a4053SRui Paulo 		   "OUI type %d  OUI sub-type %d  version %d  QoS info 0x%x",
176e28a4053SRui Paulo 		   wmm->oui[0], wmm->oui[1], wmm->oui[2], wmm->oui_type,
177e28a4053SRui Paulo 		   wmm->oui_subtype, wmm->version, wmm->qos_info);
178e28a4053SRui Paulo 	if (wmm->oui_subtype != WMM_OUI_SUBTYPE_INFORMATION_ELEMENT ||
179e28a4053SRui Paulo 	    wmm->version != WMM_VERSION) {
180e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "Unsupported WMM IE Subtype/Version");
181f05cddf9SRui Paulo 		return 0;
182e28a4053SRui Paulo 	}
183e28a4053SRui Paulo 
184f05cddf9SRui Paulo 	return 1;
185e28a4053SRui Paulo }
186e28a4053SRui Paulo 
187e28a4053SRui Paulo 
188e28a4053SRui Paulo static void wmm_send_action(struct hostapd_data *hapd, const u8 *addr,
189e28a4053SRui Paulo 			    const struct wmm_tspec_element *tspec,
190e28a4053SRui Paulo 			    u8 action_code, u8 dialogue_token, u8 status_code)
191e28a4053SRui Paulo {
192e28a4053SRui Paulo 	u8 buf[256];
193e28a4053SRui Paulo 	struct ieee80211_mgmt *m = (struct ieee80211_mgmt *) buf;
194e28a4053SRui Paulo 	struct wmm_tspec_element *t = (struct wmm_tspec_element *)
195e28a4053SRui Paulo 		m->u.action.u.wmm_action.variable;
196e28a4053SRui Paulo 	int len;
197e28a4053SRui Paulo 
198e28a4053SRui Paulo 	hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
199e28a4053SRui Paulo 		       HOSTAPD_LEVEL_DEBUG,
200e28a4053SRui Paulo 		       "action response - reason %d", status_code);
201e28a4053SRui Paulo 	os_memset(buf, 0, sizeof(buf));
202e28a4053SRui Paulo 	m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
203e28a4053SRui Paulo 					WLAN_FC_STYPE_ACTION);
204e28a4053SRui Paulo 	os_memcpy(m->da, addr, ETH_ALEN);
205e28a4053SRui Paulo 	os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
206e28a4053SRui Paulo 	os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
207e28a4053SRui Paulo 	m->u.action.category = WLAN_ACTION_WMM;
208e28a4053SRui Paulo 	m->u.action.u.wmm_action.action_code = action_code;
209e28a4053SRui Paulo 	m->u.action.u.wmm_action.dialog_token = dialogue_token;
210e28a4053SRui Paulo 	m->u.action.u.wmm_action.status_code = status_code;
211e28a4053SRui Paulo 	os_memcpy(t, tspec, sizeof(struct wmm_tspec_element));
212e28a4053SRui Paulo 	len = ((u8 *) (t + 1)) - buf;
213e28a4053SRui Paulo 
214*c1d255d3SCy Schubert 	if (hostapd_drv_send_mlme(hapd, m, len, 0, NULL, 0, 0) < 0)
2155b9c547cSRui Paulo 		wpa_printf(MSG_INFO, "wmm_send_action: send failed");
216e28a4053SRui Paulo }
217e28a4053SRui Paulo 
218e28a4053SRui Paulo 
219e28a4053SRui Paulo int wmm_process_tspec(struct wmm_tspec_element *tspec)
220e28a4053SRui Paulo {
22185732ac8SCy Schubert 	u64 medium_time;
22285732ac8SCy Schubert 	unsigned int pps, duration;
22385732ac8SCy Schubert 	unsigned int up, psb, dir, tid;
224e28a4053SRui Paulo 	u16 val, surplus;
225e28a4053SRui Paulo 
226e28a4053SRui Paulo 	up = (tspec->ts_info[1] >> 3) & 0x07;
227e28a4053SRui Paulo 	psb = (tspec->ts_info[1] >> 2) & 0x01;
228e28a4053SRui Paulo 	dir = (tspec->ts_info[0] >> 5) & 0x03;
229e28a4053SRui Paulo 	tid = (tspec->ts_info[0] >> 1) & 0x0f;
230e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "WMM: TS Info: UP=%d PSB=%d Direction=%d TID=%d",
231e28a4053SRui Paulo 		   up, psb, dir, tid);
232e28a4053SRui Paulo 	val = le_to_host16(tspec->nominal_msdu_size);
233e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "WMM: Nominal MSDU Size: %d%s",
234e28a4053SRui Paulo 		   val & 0x7fff, val & 0x8000 ? " (fixed)" : "");
235e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "WMM: Mean Data Rate: %u bps",
236e28a4053SRui Paulo 		   le_to_host32(tspec->mean_data_rate));
237e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "WMM: Minimum PHY Rate: %u bps",
238e28a4053SRui Paulo 		   le_to_host32(tspec->minimum_phy_rate));
239e28a4053SRui Paulo 	val = le_to_host16(tspec->surplus_bandwidth_allowance);
240e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance: %u.%04u",
241e28a4053SRui Paulo 		   val >> 13, 10000 * (val & 0x1fff) / 0x2000);
242e28a4053SRui Paulo 
243e28a4053SRui Paulo 	val = le_to_host16(tspec->nominal_msdu_size);
244e28a4053SRui Paulo 	if (val == 0) {
245e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "WMM: Invalid Nominal MSDU Size (0)");
246e28a4053SRui Paulo 		return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
247e28a4053SRui Paulo 	}
248e28a4053SRui Paulo 	/* pps = Ceiling((Mean Data Rate / 8) / Nominal MSDU Size) */
249e28a4053SRui Paulo 	pps = ((le_to_host32(tspec->mean_data_rate) / 8) + val - 1) / val;
250e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "WMM: Packets-per-second estimate for TSPEC: %d",
251e28a4053SRui Paulo 		   pps);
252e28a4053SRui Paulo 
253e28a4053SRui Paulo 	if (le_to_host32(tspec->minimum_phy_rate) < 1000000) {
254e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "WMM: Too small Minimum PHY Rate");
255e28a4053SRui Paulo 		return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
256e28a4053SRui Paulo 	}
257e28a4053SRui Paulo 
258e28a4053SRui Paulo 	duration = (le_to_host16(tspec->nominal_msdu_size) & 0x7fff) * 8 /
259e28a4053SRui Paulo 		(le_to_host32(tspec->minimum_phy_rate) / 1000000) +
260e28a4053SRui Paulo 		50 /* FIX: proper SIFS + ACK duration */;
261e28a4053SRui Paulo 
262e28a4053SRui Paulo 	/* unsigned binary number with an implicit binary point after the
263e28a4053SRui Paulo 	 * leftmost 3 bits, i.e., 0x2000 = 1.0 */
264e28a4053SRui Paulo 	surplus = le_to_host16(tspec->surplus_bandwidth_allowance);
265e28a4053SRui Paulo 	if (surplus <= 0x2000) {
266e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance not "
267e28a4053SRui Paulo 			   "greater than unity");
268e28a4053SRui Paulo 		return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
269e28a4053SRui Paulo 	}
270e28a4053SRui Paulo 
27185732ac8SCy Schubert 	medium_time = (u64) surplus * pps * duration / 0x2000;
27285732ac8SCy Schubert 	wpa_printf(MSG_DEBUG, "WMM: Estimated medium time: %lu",
27385732ac8SCy Schubert 		   (unsigned long) medium_time);
274e28a4053SRui Paulo 
275e28a4053SRui Paulo 	/*
276e28a4053SRui Paulo 	 * TODO: store list of granted (and still active) TSPECs and check
277e28a4053SRui Paulo 	 * whether there is available medium time for this request. For now,
278e28a4053SRui Paulo 	 * just refuse requests that would by themselves take very large
279e28a4053SRui Paulo 	 * portion of the available bandwidth.
280e28a4053SRui Paulo 	 */
281e28a4053SRui Paulo 	if (medium_time > 750000) {
282e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "WMM: Refuse TSPEC request for over "
283e28a4053SRui Paulo 			   "75%% of available bandwidth");
284e28a4053SRui Paulo 		return WMM_ADDTS_STATUS_REFUSED;
285e28a4053SRui Paulo 	}
286e28a4053SRui Paulo 
287e28a4053SRui Paulo 	/* Convert to 32 microseconds per second unit */
288e28a4053SRui Paulo 	tspec->medium_time = host_to_le16(medium_time / 32);
289e28a4053SRui Paulo 
290e28a4053SRui Paulo 	return WMM_ADDTS_STATUS_ADMISSION_ACCEPTED;
291e28a4053SRui Paulo }
292e28a4053SRui Paulo 
293e28a4053SRui Paulo 
294e28a4053SRui Paulo static void wmm_addts_req(struct hostapd_data *hapd,
295e28a4053SRui Paulo 			  const struct ieee80211_mgmt *mgmt,
296*c1d255d3SCy Schubert 			  const struct wmm_tspec_element *tspec, size_t len)
297e28a4053SRui Paulo {
298e28a4053SRui Paulo 	const u8 *end = ((const u8 *) mgmt) + len;
299e28a4053SRui Paulo 	int res;
300*c1d255d3SCy Schubert 	struct wmm_tspec_element tspec_resp;
301e28a4053SRui Paulo 
302e28a4053SRui Paulo 	if ((const u8 *) (tspec + 1) > end) {
303e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "WMM: TSPEC overflow in ADDTS Request");
304e28a4053SRui Paulo 		return;
305e28a4053SRui Paulo 	}
306e28a4053SRui Paulo 
307e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "WMM: ADDTS Request (Dialog Token %d) for TSPEC "
308e28a4053SRui Paulo 		   "from " MACSTR,
309e28a4053SRui Paulo 		   mgmt->u.action.u.wmm_action.dialog_token,
310e28a4053SRui Paulo 		   MAC2STR(mgmt->sa));
311e28a4053SRui Paulo 
312*c1d255d3SCy Schubert 	os_memcpy(&tspec_resp, tspec, sizeof(struct wmm_tspec_element));
313*c1d255d3SCy Schubert 	res = wmm_process_tspec(&tspec_resp);
314e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "WMM: ADDTS processing result: %d", res);
315e28a4053SRui Paulo 
316*c1d255d3SCy Schubert 	wmm_send_action(hapd, mgmt->sa, &tspec_resp, WMM_ACTION_CODE_ADDTS_RESP,
317e28a4053SRui Paulo 			mgmt->u.action.u.wmm_action.dialog_token, res);
318e28a4053SRui Paulo }
319e28a4053SRui Paulo 
320e28a4053SRui Paulo 
321e28a4053SRui Paulo void hostapd_wmm_action(struct hostapd_data *hapd,
322e28a4053SRui Paulo 			const struct ieee80211_mgmt *mgmt, size_t len)
323e28a4053SRui Paulo {
324e28a4053SRui Paulo 	int action_code;
325e28a4053SRui Paulo 	int left = len - IEEE80211_HDRLEN - 4;
326e28a4053SRui Paulo 	const u8 *pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 4;
327e28a4053SRui Paulo 	struct ieee802_11_elems elems;
328e28a4053SRui Paulo 	struct sta_info *sta = ap_get_sta(hapd, mgmt->sa);
329e28a4053SRui Paulo 
330e28a4053SRui Paulo 	/* check that the request comes from a valid station */
331e28a4053SRui Paulo 	if (!sta ||
332e28a4053SRui Paulo 	    (sta->flags & (WLAN_STA_ASSOC | WLAN_STA_WMM)) !=
333e28a4053SRui Paulo 	    (WLAN_STA_ASSOC | WLAN_STA_WMM)) {
334e28a4053SRui Paulo 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
335e28a4053SRui Paulo 			       HOSTAPD_LEVEL_DEBUG,
336e28a4053SRui Paulo 			       "wmm action received is not from associated wmm"
337e28a4053SRui Paulo 			       " station");
338e28a4053SRui Paulo 		/* TODO: respond with action frame refused status code */
339e28a4053SRui Paulo 		return;
340e28a4053SRui Paulo 	}
341e28a4053SRui Paulo 
342325151a3SRui Paulo 	if (left < 0)
343325151a3SRui Paulo 		return; /* not a valid WMM Action frame */
344325151a3SRui Paulo 
345e28a4053SRui Paulo 	/* extract the tspec info element */
346e28a4053SRui Paulo 	if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed) {
347e28a4053SRui Paulo 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
348e28a4053SRui Paulo 			       HOSTAPD_LEVEL_DEBUG,
349e28a4053SRui Paulo 			       "hostapd_wmm_action - could not parse wmm "
350e28a4053SRui Paulo 			       "action");
351e28a4053SRui Paulo 		/* TODO: respond with action frame invalid parameters status
352e28a4053SRui Paulo 		 * code */
353e28a4053SRui Paulo 		return;
354e28a4053SRui Paulo 	}
355e28a4053SRui Paulo 
356e28a4053SRui Paulo 	if (!elems.wmm_tspec ||
357e28a4053SRui Paulo 	    elems.wmm_tspec_len != (sizeof(struct wmm_tspec_element) - 2)) {
358e28a4053SRui Paulo 		hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
359e28a4053SRui Paulo 			       HOSTAPD_LEVEL_DEBUG,
360e28a4053SRui Paulo 			       "hostapd_wmm_action - missing or wrong length "
361e28a4053SRui Paulo 			       "tspec");
362e28a4053SRui Paulo 		/* TODO: respond with action frame invalid parameters status
363e28a4053SRui Paulo 		 * code */
364e28a4053SRui Paulo 		return;
365e28a4053SRui Paulo 	}
366e28a4053SRui Paulo 
367e28a4053SRui Paulo 	/* TODO: check the request is for an AC with ACM set, if not, refuse
368e28a4053SRui Paulo 	 * request */
369e28a4053SRui Paulo 
370e28a4053SRui Paulo 	action_code = mgmt->u.action.u.wmm_action.action_code;
371e28a4053SRui Paulo 	switch (action_code) {
372e28a4053SRui Paulo 	case WMM_ACTION_CODE_ADDTS_REQ:
373e28a4053SRui Paulo 		wmm_addts_req(hapd, mgmt, (struct wmm_tspec_element *)
374e28a4053SRui Paulo 			      (elems.wmm_tspec - 2), len);
375e28a4053SRui Paulo 		return;
376e28a4053SRui Paulo #if 0
377e28a4053SRui Paulo 	/* TODO: needed for client implementation */
378e28a4053SRui Paulo 	case WMM_ACTION_CODE_ADDTS_RESP:
379e28a4053SRui Paulo 		wmm_setup_request(hapd, mgmt, len);
380e28a4053SRui Paulo 		return;
381e28a4053SRui Paulo 	/* TODO: handle station teardown requests */
382e28a4053SRui Paulo 	case WMM_ACTION_CODE_DELTS:
383e28a4053SRui Paulo 		wmm_teardown(hapd, mgmt, len);
384e28a4053SRui Paulo 		return;
385e28a4053SRui Paulo #endif
386e28a4053SRui Paulo 	}
387e28a4053SRui Paulo 
388e28a4053SRui Paulo 	hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
389e28a4053SRui Paulo 		       HOSTAPD_LEVEL_DEBUG,
390e28a4053SRui Paulo 		       "hostapd_wmm_action - unknown action code %d",
391e28a4053SRui Paulo 		       action_code);
392e28a4053SRui Paulo }
393