2 * hostapd / WMM (Wi-Fi Multimedia)
3 * Copyright 2002-2003, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Alternatively, this software may be distributed under the terms of BSD
14 * See README and COPYING for more details.
17 #include "utils/includes.h"
19 #include "utils/common.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/ieee802_11_common.h"
23 #include "ieee802_11.h"
25 #include "ap_config.h"
26 #include "ap_drv_ops.h"
30 /* TODO: maintain separate sequence and fragment numbers for each AC
31 * TODO: IGMP snooping to track which multicasts to forward - and use QOS-DATA
32 * if only WMM stations are receiving a certain group */
35 static inline u8 wmm_aci_aifsn(int aifsn, int acm, int aci)
38 ret = (aifsn << WMM_AC_AIFNS_SHIFT) & WMM_AC_AIFSN_MASK;
41 ret |= (aci << WMM_AC_ACI_SHIFT) & WMM_AC_ACI_MASK;
46 static inline u8 wmm_ecw(int ecwmin, int ecwmax)
48 return ((ecwmin << WMM_AC_ECWMIN_SHIFT) & WMM_AC_ECWMIN_MASK) |
49 ((ecwmax << WMM_AC_ECWMAX_SHIFT) & WMM_AC_ECWMAX_MASK);
54 * Add WMM Parameter Element to Beacon, Probe Response, and (Re)Association
57 u8 * hostapd_eid_wmm(struct hostapd_data *hapd, u8 *eid)
60 struct wmm_parameter_element *wmm =
61 (struct wmm_parameter_element *) (pos + 2);
64 if (!hapd->conf->wmm_enabled)
66 eid[0] = WLAN_EID_VENDOR_SPECIFIC;
70 wmm->oui_type = WMM_OUI_TYPE;
71 wmm->oui_subtype = WMM_OUI_SUBTYPE_PARAMETER_ELEMENT;
72 wmm->version = WMM_VERSION;
73 wmm->qos_info = hapd->parameter_set_count & 0xf;
75 if (hapd->conf->wmm_uapsd)
76 wmm->qos_info |= 0x80;
78 /* fill in a parameter set record for each AC */
79 for (e = 0; e < 4; e++) {
80 struct wmm_ac_parameter *ac = &wmm->ac[e];
81 struct hostapd_wmm_ac_params *acp =
82 &hapd->iconf->wmm_ac_params[e];
84 ac->aci_aifsn = wmm_aci_aifsn(acp->aifs,
85 acp->admission_control_mandatory,
87 ac->cw = wmm_ecw(acp->cwmin, acp->cwmax);
88 ac->txop_limit = host_to_le16(acp->txop_limit);
91 pos = (u8 *) (wmm + 1);
92 eid[1] = pos - eid - 2; /* element length */
98 /* This function is called when a station sends an association request with
99 * WMM info element. The function returns zero on success or non-zero on any
100 * error in WMM element. eid does not include Element ID and Length octets. */
101 int hostapd_eid_wmm_valid(struct hostapd_data *hapd, const u8 *eid, size_t len)
103 struct wmm_information_element *wmm;
105 wpa_hexdump(MSG_MSGDUMP, "WMM IE", eid, len);
107 if (len < sizeof(struct wmm_information_element)) {
108 wpa_printf(MSG_DEBUG, "Too short WMM IE (len=%lu)",
109 (unsigned long) len);
113 wmm = (struct wmm_information_element *) eid;
114 wpa_printf(MSG_DEBUG, "Validating WMM IE: OUI %02x:%02x:%02x "
115 "OUI type %d OUI sub-type %d version %d QoS info 0x%x",
116 wmm->oui[0], wmm->oui[1], wmm->oui[2], wmm->oui_type,
117 wmm->oui_subtype, wmm->version, wmm->qos_info);
118 if (wmm->oui_subtype != WMM_OUI_SUBTYPE_INFORMATION_ELEMENT ||
119 wmm->version != WMM_VERSION) {
120 wpa_printf(MSG_DEBUG, "Unsupported WMM IE Subtype/Version");
128 static void wmm_send_action(struct hostapd_data *hapd, const u8 *addr,
129 const struct wmm_tspec_element *tspec,
130 u8 action_code, u8 dialogue_token, u8 status_code)
133 struct ieee80211_mgmt *m = (struct ieee80211_mgmt *) buf;
134 struct wmm_tspec_element *t = (struct wmm_tspec_element *)
135 m->u.action.u.wmm_action.variable;
138 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
140 "action response - reason %d", status_code);
141 os_memset(buf, 0, sizeof(buf));
142 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
143 WLAN_FC_STYPE_ACTION);
144 os_memcpy(m->da, addr, ETH_ALEN);
145 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
146 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
147 m->u.action.category = WLAN_ACTION_WMM;
148 m->u.action.u.wmm_action.action_code = action_code;
149 m->u.action.u.wmm_action.dialog_token = dialogue_token;
150 m->u.action.u.wmm_action.status_code = status_code;
151 os_memcpy(t, tspec, sizeof(struct wmm_tspec_element));
152 len = ((u8 *) (t + 1)) - buf;
154 if (hostapd_drv_send_mlme(hapd, m, len) < 0)
155 perror("wmm_send_action: send");
159 int wmm_process_tspec(struct wmm_tspec_element *tspec)
161 int medium_time, pps, duration;
162 int up, psb, dir, tid;
165 up = (tspec->ts_info[1] >> 3) & 0x07;
166 psb = (tspec->ts_info[1] >> 2) & 0x01;
167 dir = (tspec->ts_info[0] >> 5) & 0x03;
168 tid = (tspec->ts_info[0] >> 1) & 0x0f;
169 wpa_printf(MSG_DEBUG, "WMM: TS Info: UP=%d PSB=%d Direction=%d TID=%d",
171 val = le_to_host16(tspec->nominal_msdu_size);
172 wpa_printf(MSG_DEBUG, "WMM: Nominal MSDU Size: %d%s",
173 val & 0x7fff, val & 0x8000 ? " (fixed)" : "");
174 wpa_printf(MSG_DEBUG, "WMM: Mean Data Rate: %u bps",
175 le_to_host32(tspec->mean_data_rate));
176 wpa_printf(MSG_DEBUG, "WMM: Minimum PHY Rate: %u bps",
177 le_to_host32(tspec->minimum_phy_rate));
178 val = le_to_host16(tspec->surplus_bandwidth_allowance);
179 wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance: %u.%04u",
180 val >> 13, 10000 * (val & 0x1fff) / 0x2000);
182 val = le_to_host16(tspec->nominal_msdu_size);
184 wpa_printf(MSG_DEBUG, "WMM: Invalid Nominal MSDU Size (0)");
185 return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
187 /* pps = Ceiling((Mean Data Rate / 8) / Nominal MSDU Size) */
188 pps = ((le_to_host32(tspec->mean_data_rate) / 8) + val - 1) / val;
189 wpa_printf(MSG_DEBUG, "WMM: Packets-per-second estimate for TSPEC: %d",
192 if (le_to_host32(tspec->minimum_phy_rate) < 1000000) {
193 wpa_printf(MSG_DEBUG, "WMM: Too small Minimum PHY Rate");
194 return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
197 duration = (le_to_host16(tspec->nominal_msdu_size) & 0x7fff) * 8 /
198 (le_to_host32(tspec->minimum_phy_rate) / 1000000) +
199 50 /* FIX: proper SIFS + ACK duration */;
201 /* unsigned binary number with an implicit binary point after the
202 * leftmost 3 bits, i.e., 0x2000 = 1.0 */
203 surplus = le_to_host16(tspec->surplus_bandwidth_allowance);
204 if (surplus <= 0x2000) {
205 wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance not "
206 "greater than unity");
207 return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
210 medium_time = surplus * pps * duration / 0x2000;
211 wpa_printf(MSG_DEBUG, "WMM: Estimated medium time: %u", medium_time);
214 * TODO: store list of granted (and still active) TSPECs and check
215 * whether there is available medium time for this request. For now,
216 * just refuse requests that would by themselves take very large
217 * portion of the available bandwidth.
219 if (medium_time > 750000) {
220 wpa_printf(MSG_DEBUG, "WMM: Refuse TSPEC request for over "
221 "75%% of available bandwidth");
222 return WMM_ADDTS_STATUS_REFUSED;
225 /* Convert to 32 microseconds per second unit */
226 tspec->medium_time = host_to_le16(medium_time / 32);
228 return WMM_ADDTS_STATUS_ADMISSION_ACCEPTED;
232 static void wmm_addts_req(struct hostapd_data *hapd,
233 const struct ieee80211_mgmt *mgmt,
234 struct wmm_tspec_element *tspec, size_t len)
236 const u8 *end = ((const u8 *) mgmt) + len;
239 if ((const u8 *) (tspec + 1) > end) {
240 wpa_printf(MSG_DEBUG, "WMM: TSPEC overflow in ADDTS Request");
244 wpa_printf(MSG_DEBUG, "WMM: ADDTS Request (Dialog Token %d) for TSPEC "
246 mgmt->u.action.u.wmm_action.dialog_token,
249 res = wmm_process_tspec(tspec);
250 wpa_printf(MSG_DEBUG, "WMM: ADDTS processing result: %d", res);
252 wmm_send_action(hapd, mgmt->sa, tspec, WMM_ACTION_CODE_ADDTS_RESP,
253 mgmt->u.action.u.wmm_action.dialog_token, res);
257 void hostapd_wmm_action(struct hostapd_data *hapd,
258 const struct ieee80211_mgmt *mgmt, size_t len)
261 int left = len - IEEE80211_HDRLEN - 4;
262 const u8 *pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 4;
263 struct ieee802_11_elems elems;
264 struct sta_info *sta = ap_get_sta(hapd, mgmt->sa);
266 /* check that the request comes from a valid station */
268 (sta->flags & (WLAN_STA_ASSOC | WLAN_STA_WMM)) !=
269 (WLAN_STA_ASSOC | WLAN_STA_WMM)) {
270 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
272 "wmm action received is not from associated wmm"
274 /* TODO: respond with action frame refused status code */
278 /* extract the tspec info element */
279 if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed) {
280 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
282 "hostapd_wmm_action - could not parse wmm "
284 /* TODO: respond with action frame invalid parameters status
289 if (!elems.wmm_tspec ||
290 elems.wmm_tspec_len != (sizeof(struct wmm_tspec_element) - 2)) {
291 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
293 "hostapd_wmm_action - missing or wrong length "
295 /* TODO: respond with action frame invalid parameters status
300 /* TODO: check the request is for an AC with ACM set, if not, refuse
303 action_code = mgmt->u.action.u.wmm_action.action_code;
304 switch (action_code) {
305 case WMM_ACTION_CODE_ADDTS_REQ:
306 wmm_addts_req(hapd, mgmt, (struct wmm_tspec_element *)
307 (elems.wmm_tspec - 2), len);
310 /* TODO: needed for client implementation */
311 case WMM_ACTION_CODE_ADDTS_RESP:
312 wmm_setup_request(hapd, mgmt, len);
314 /* TODO: handle station teardown requests */
315 case WMM_ACTION_CODE_DELTS:
316 wmm_teardown(hapd, mgmt, len);
321 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
323 "hostapd_wmm_action - unknown action code %d",