blob: 9f52dee32def0563162849769934432fde16a6e5 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
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>
6 *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007 * This software may be distributed under the terms of the BSD license.
8 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009 */
10
11#include "utils/includes.h"
12
13#include "utils/common.h"
14#include "common/ieee802_11_defs.h"
15#include "common/ieee802_11_common.h"
16#include "hostapd.h"
17#include "ieee802_11.h"
18#include "sta_info.h"
19#include "ap_config.h"
20#include "ap_drv_ops.h"
21#include "wmm.h"
22
Hai Shalom81f62d82019-07-22 12:10:00 -070023#ifndef MIN
24#define MIN(a, b) (((a) < (b)) ? (a) : (b))
25#endif
26#ifndef MAX
27#define MAX(a, b) (((a) > (b)) ? (a) : (b))
28#endif
29
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070031static inline u8 wmm_aci_aifsn(int aifsn, int acm, int aci)
32{
33 u8 ret;
34 ret = (aifsn << WMM_AC_AIFNS_SHIFT) & WMM_AC_AIFSN_MASK;
35 if (acm)
36 ret |= WMM_AC_ACM;
37 ret |= (aci << WMM_AC_ACI_SHIFT) & WMM_AC_ACI_MASK;
38 return ret;
39}
40
41
42static inline u8 wmm_ecw(int ecwmin, int ecwmax)
43{
44 return ((ecwmin << WMM_AC_ECWMIN_SHIFT) & WMM_AC_ECWMIN_MASK) |
45 ((ecwmax << WMM_AC_ECWMAX_SHIFT) & WMM_AC_ECWMAX_MASK);
46}
47
48
Hai Shalom81f62d82019-07-22 12:10:00 -070049static void
50wmm_set_regulatory_limit(const struct hostapd_wmm_ac_params *wmm_conf,
51 struct hostapd_wmm_ac_params *wmm,
52 const struct hostapd_wmm_rule *wmm_reg)
53{
54 int ac;
55
56 for (ac = 0; ac < WMM_AC_NUM; ac++) {
57 wmm[ac].cwmin = MAX(wmm_conf[ac].cwmin, wmm_reg[ac].min_cwmin);
58 wmm[ac].cwmax = MAX(wmm_conf[ac].cwmax, wmm_reg[ac].min_cwmax);
59 wmm[ac].aifs = MAX(wmm_conf[ac].aifs, wmm_reg[ac].min_aifs);
60 wmm[ac].txop_limit =
61 MIN(wmm_conf[ac].txop_limit, wmm_reg[ac].max_txop);
62 wmm[ac].admission_control_mandatory =
63 wmm_conf[ac].admission_control_mandatory;
64 }
65}
66
67
68/*
69 * Calculate WMM regulatory limit if any.
70 */
71static void wmm_calc_regulatory_limit(struct hostapd_data *hapd,
72 struct hostapd_wmm_ac_params *acp)
73{
74 struct hostapd_hw_modes *mode = hapd->iface->current_mode;
75 int c;
76
77 os_memcpy(acp, hapd->iconf->wmm_ac_params,
78 sizeof(hapd->iconf->wmm_ac_params));
79
80 for (c = 0; mode && c < mode->num_channels; c++) {
81 struct hostapd_channel_data *chan = &mode->channels[c];
82
83 if (chan->freq != hapd->iface->freq)
84 continue;
85
86 if (chan->wmm_rules_valid)
87 wmm_set_regulatory_limit(hapd->iconf->wmm_ac_params,
88 acp, chan->wmm_rules);
89 break;
90 }
91
92 /*
93 * Check if we need to update set count. Since both were initialized to
94 * zero we can compare the whole array in one shot.
95 */
96 if (os_memcmp(acp, hapd->iface->prev_wmm,
97 sizeof(hapd->iconf->wmm_ac_params)) != 0) {
98 os_memcpy(hapd->iface->prev_wmm, acp,
99 sizeof(hapd->iconf->wmm_ac_params));
100 hapd->parameter_set_count++;
101 }
102}
103
104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105/*
106 * Add WMM Parameter Element to Beacon, Probe Response, and (Re)Association
107 * Response frames.
108 */
109u8 * hostapd_eid_wmm(struct hostapd_data *hapd, u8 *eid)
110{
111 u8 *pos = eid;
112 struct wmm_parameter_element *wmm =
113 (struct wmm_parameter_element *) (pos + 2);
Hai Shalom81f62d82019-07-22 12:10:00 -0700114 struct hostapd_wmm_ac_params wmmp[WMM_AC_NUM] = { 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700115 int e;
116
117 if (!hapd->conf->wmm_enabled)
118 return eid;
Hai Shalom81f62d82019-07-22 12:10:00 -0700119 wmm_calc_regulatory_limit(hapd, wmmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700120 eid[0] = WLAN_EID_VENDOR_SPECIFIC;
121 wmm->oui[0] = 0x00;
122 wmm->oui[1] = 0x50;
123 wmm->oui[2] = 0xf2;
124 wmm->oui_type = WMM_OUI_TYPE;
125 wmm->oui_subtype = WMM_OUI_SUBTYPE_PARAMETER_ELEMENT;
126 wmm->version = WMM_VERSION;
127 wmm->qos_info = hapd->parameter_set_count & 0xf;
128
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800129 if (hapd->conf->wmm_uapsd &&
130 (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 wmm->qos_info |= 0x80;
132
133 wmm->reserved = 0;
134
135 /* fill in a parameter set record for each AC */
136 for (e = 0; e < 4; e++) {
137 struct wmm_ac_parameter *ac = &wmm->ac[e];
Hai Shalom81f62d82019-07-22 12:10:00 -0700138 struct hostapd_wmm_ac_params *acp = &wmmp[e];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700139
140 ac->aci_aifsn = wmm_aci_aifsn(acp->aifs,
141 acp->admission_control_mandatory,
142 e);
143 ac->cw = wmm_ecw(acp->cwmin, acp->cwmax);
144 ac->txop_limit = host_to_le16(acp->txop_limit);
145 }
146
147 pos = (u8 *) (wmm + 1);
148 eid[1] = pos - eid - 2; /* element length */
149
150 return pos;
151}
152
153
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800154/*
155 * This function is called when a station sends an association request with
156 * WMM info element. The function returns 1 on success or 0 on any error in WMM
157 * element. eid does not include Element ID and Length octets.
158 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159int hostapd_eid_wmm_valid(struct hostapd_data *hapd, const u8 *eid, size_t len)
160{
161 struct wmm_information_element *wmm;
162
163 wpa_hexdump(MSG_MSGDUMP, "WMM IE", eid, len);
164
165 if (len < sizeof(struct wmm_information_element)) {
166 wpa_printf(MSG_DEBUG, "Too short WMM IE (len=%lu)",
167 (unsigned long) len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800168 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169 }
170
171 wmm = (struct wmm_information_element *) eid;
172 wpa_printf(MSG_DEBUG, "Validating WMM IE: OUI %02x:%02x:%02x "
173 "OUI type %d OUI sub-type %d version %d QoS info 0x%x",
174 wmm->oui[0], wmm->oui[1], wmm->oui[2], wmm->oui_type,
175 wmm->oui_subtype, wmm->version, wmm->qos_info);
176 if (wmm->oui_subtype != WMM_OUI_SUBTYPE_INFORMATION_ELEMENT ||
177 wmm->version != WMM_VERSION) {
178 wpa_printf(MSG_DEBUG, "Unsupported WMM IE Subtype/Version");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800179 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700180 }
181
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800182 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183}
184
185
186static void wmm_send_action(struct hostapd_data *hapd, const u8 *addr,
187 const struct wmm_tspec_element *tspec,
188 u8 action_code, u8 dialogue_token, u8 status_code)
189{
190 u8 buf[256];
191 struct ieee80211_mgmt *m = (struct ieee80211_mgmt *) buf;
192 struct wmm_tspec_element *t = (struct wmm_tspec_element *)
193 m->u.action.u.wmm_action.variable;
194 int len;
195
196 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
197 HOSTAPD_LEVEL_DEBUG,
198 "action response - reason %d", status_code);
199 os_memset(buf, 0, sizeof(buf));
200 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
201 WLAN_FC_STYPE_ACTION);
202 os_memcpy(m->da, addr, ETH_ALEN);
203 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
204 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
205 m->u.action.category = WLAN_ACTION_WMM;
206 m->u.action.u.wmm_action.action_code = action_code;
207 m->u.action.u.wmm_action.dialog_token = dialogue_token;
208 m->u.action.u.wmm_action.status_code = status_code;
209 os_memcpy(t, tspec, sizeof(struct wmm_tspec_element));
210 len = ((u8 *) (t + 1)) - buf;
211
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800212 if (hostapd_drv_send_mlme(hapd, m, len, 0) < 0)
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800213 wpa_printf(MSG_INFO, "wmm_send_action: send failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214}
215
216
217int wmm_process_tspec(struct wmm_tspec_element *tspec)
218{
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800219 u64 medium_time;
220 unsigned int pps, duration;
221 unsigned int up, psb, dir, tid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222 u16 val, surplus;
223
224 up = (tspec->ts_info[1] >> 3) & 0x07;
225 psb = (tspec->ts_info[1] >> 2) & 0x01;
226 dir = (tspec->ts_info[0] >> 5) & 0x03;
227 tid = (tspec->ts_info[0] >> 1) & 0x0f;
228 wpa_printf(MSG_DEBUG, "WMM: TS Info: UP=%d PSB=%d Direction=%d TID=%d",
229 up, psb, dir, tid);
230 val = le_to_host16(tspec->nominal_msdu_size);
231 wpa_printf(MSG_DEBUG, "WMM: Nominal MSDU Size: %d%s",
232 val & 0x7fff, val & 0x8000 ? " (fixed)" : "");
233 wpa_printf(MSG_DEBUG, "WMM: Mean Data Rate: %u bps",
234 le_to_host32(tspec->mean_data_rate));
235 wpa_printf(MSG_DEBUG, "WMM: Minimum PHY Rate: %u bps",
236 le_to_host32(tspec->minimum_phy_rate));
237 val = le_to_host16(tspec->surplus_bandwidth_allowance);
238 wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance: %u.%04u",
239 val >> 13, 10000 * (val & 0x1fff) / 0x2000);
240
241 val = le_to_host16(tspec->nominal_msdu_size);
242 if (val == 0) {
243 wpa_printf(MSG_DEBUG, "WMM: Invalid Nominal MSDU Size (0)");
244 return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
245 }
246 /* pps = Ceiling((Mean Data Rate / 8) / Nominal MSDU Size) */
247 pps = ((le_to_host32(tspec->mean_data_rate) / 8) + val - 1) / val;
248 wpa_printf(MSG_DEBUG, "WMM: Packets-per-second estimate for TSPEC: %d",
249 pps);
250
251 if (le_to_host32(tspec->minimum_phy_rate) < 1000000) {
252 wpa_printf(MSG_DEBUG, "WMM: Too small Minimum PHY Rate");
253 return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
254 }
255
256 duration = (le_to_host16(tspec->nominal_msdu_size) & 0x7fff) * 8 /
257 (le_to_host32(tspec->minimum_phy_rate) / 1000000) +
258 50 /* FIX: proper SIFS + ACK duration */;
259
260 /* unsigned binary number with an implicit binary point after the
261 * leftmost 3 bits, i.e., 0x2000 = 1.0 */
262 surplus = le_to_host16(tspec->surplus_bandwidth_allowance);
263 if (surplus <= 0x2000) {
264 wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance not "
265 "greater than unity");
266 return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
267 }
268
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800269 medium_time = (u64) surplus * pps * duration / 0x2000;
270 wpa_printf(MSG_DEBUG, "WMM: Estimated medium time: %lu",
271 (unsigned long) medium_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272
273 /*
274 * TODO: store list of granted (and still active) TSPECs and check
275 * whether there is available medium time for this request. For now,
276 * just refuse requests that would by themselves take very large
277 * portion of the available bandwidth.
278 */
279 if (medium_time > 750000) {
280 wpa_printf(MSG_DEBUG, "WMM: Refuse TSPEC request for over "
281 "75%% of available bandwidth");
282 return WMM_ADDTS_STATUS_REFUSED;
283 }
284
285 /* Convert to 32 microseconds per second unit */
286 tspec->medium_time = host_to_le16(medium_time / 32);
287
288 return WMM_ADDTS_STATUS_ADMISSION_ACCEPTED;
289}
290
291
292static void wmm_addts_req(struct hostapd_data *hapd,
293 const struct ieee80211_mgmt *mgmt,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800294 const struct wmm_tspec_element *tspec, size_t len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295{
296 const u8 *end = ((const u8 *) mgmt) + len;
297 int res;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800298 struct wmm_tspec_element tspec_resp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299
300 if ((const u8 *) (tspec + 1) > end) {
301 wpa_printf(MSG_DEBUG, "WMM: TSPEC overflow in ADDTS Request");
302 return;
303 }
304
305 wpa_printf(MSG_DEBUG, "WMM: ADDTS Request (Dialog Token %d) for TSPEC "
306 "from " MACSTR,
307 mgmt->u.action.u.wmm_action.dialog_token,
308 MAC2STR(mgmt->sa));
309
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800310 os_memcpy(&tspec_resp, tspec, sizeof(struct wmm_tspec_element));
311 res = wmm_process_tspec(&tspec_resp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312 wpa_printf(MSG_DEBUG, "WMM: ADDTS processing result: %d", res);
313
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800314 wmm_send_action(hapd, mgmt->sa, &tspec_resp, WMM_ACTION_CODE_ADDTS_RESP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315 mgmt->u.action.u.wmm_action.dialog_token, res);
316}
317
318
319void hostapd_wmm_action(struct hostapd_data *hapd,
320 const struct ieee80211_mgmt *mgmt, size_t len)
321{
322 int action_code;
323 int left = len - IEEE80211_HDRLEN - 4;
324 const u8 *pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 4;
325 struct ieee802_11_elems elems;
326 struct sta_info *sta = ap_get_sta(hapd, mgmt->sa);
327
328 /* check that the request comes from a valid station */
329 if (!sta ||
330 (sta->flags & (WLAN_STA_ASSOC | WLAN_STA_WMM)) !=
331 (WLAN_STA_ASSOC | WLAN_STA_WMM)) {
332 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
333 HOSTAPD_LEVEL_DEBUG,
334 "wmm action received is not from associated wmm"
335 " station");
336 /* TODO: respond with action frame refused status code */
337 return;
338 }
339
Dmitry Shmidtcc00d5d2015-05-04 10:34:12 -0700340 if (left < 0)
341 return; /* not a valid WMM Action frame */
342
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343 /* extract the tspec info element */
344 if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed) {
345 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
346 HOSTAPD_LEVEL_DEBUG,
347 "hostapd_wmm_action - could not parse wmm "
348 "action");
349 /* TODO: respond with action frame invalid parameters status
350 * code */
351 return;
352 }
353
354 if (!elems.wmm_tspec ||
355 elems.wmm_tspec_len != (sizeof(struct wmm_tspec_element) - 2)) {
356 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
357 HOSTAPD_LEVEL_DEBUG,
358 "hostapd_wmm_action - missing or wrong length "
359 "tspec");
360 /* TODO: respond with action frame invalid parameters status
361 * code */
362 return;
363 }
364
365 /* TODO: check the request is for an AC with ACM set, if not, refuse
366 * request */
367
368 action_code = mgmt->u.action.u.wmm_action.action_code;
369 switch (action_code) {
370 case WMM_ACTION_CODE_ADDTS_REQ:
371 wmm_addts_req(hapd, mgmt, (struct wmm_tspec_element *)
372 (elems.wmm_tspec - 2), len);
373 return;
374#if 0
375 /* TODO: needed for client implementation */
376 case WMM_ACTION_CODE_ADDTS_RESP:
377 wmm_setup_request(hapd, mgmt, len);
378 return;
379 /* TODO: handle station teardown requests */
380 case WMM_ACTION_CODE_DELTS:
381 wmm_teardown(hapd, mgmt, len);
382 return;
383#endif
384 }
385
386 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
387 HOSTAPD_LEVEL_DEBUG,
388 "hostapd_wmm_action - unknown action code %d",
389 action_code);
390}