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 23e28a4053SRui Paulo 24e28a4053SRui Paulo static inline u8 wmm_aci_aifsn(int aifsn, int acm, int aci) 25e28a4053SRui Paulo { 26e28a4053SRui Paulo u8 ret; 27e28a4053SRui Paulo ret = (aifsn << WMM_AC_AIFNS_SHIFT) & WMM_AC_AIFSN_MASK; 28e28a4053SRui Paulo if (acm) 29e28a4053SRui Paulo ret |= WMM_AC_ACM; 30e28a4053SRui Paulo ret |= (aci << WMM_AC_ACI_SHIFT) & WMM_AC_ACI_MASK; 31e28a4053SRui Paulo return ret; 32e28a4053SRui Paulo } 33e28a4053SRui Paulo 34e28a4053SRui Paulo 35e28a4053SRui Paulo static inline u8 wmm_ecw(int ecwmin, int ecwmax) 36e28a4053SRui Paulo { 37e28a4053SRui Paulo return ((ecwmin << WMM_AC_ECWMIN_SHIFT) & WMM_AC_ECWMIN_MASK) | 38e28a4053SRui Paulo ((ecwmax << WMM_AC_ECWMAX_SHIFT) & WMM_AC_ECWMAX_MASK); 39e28a4053SRui Paulo } 40e28a4053SRui Paulo 41e28a4053SRui Paulo 42e28a4053SRui Paulo /* 43e28a4053SRui Paulo * Add WMM Parameter Element to Beacon, Probe Response, and (Re)Association 44e28a4053SRui Paulo * Response frames. 45e28a4053SRui Paulo */ 46e28a4053SRui Paulo u8 * hostapd_eid_wmm(struct hostapd_data *hapd, u8 *eid) 47e28a4053SRui Paulo { 48e28a4053SRui Paulo u8 *pos = eid; 49e28a4053SRui Paulo struct wmm_parameter_element *wmm = 50e28a4053SRui Paulo (struct wmm_parameter_element *) (pos + 2); 51e28a4053SRui Paulo int e; 52e28a4053SRui Paulo 53e28a4053SRui Paulo if (!hapd->conf->wmm_enabled) 54e28a4053SRui Paulo return eid; 55e28a4053SRui Paulo eid[0] = WLAN_EID_VENDOR_SPECIFIC; 56e28a4053SRui Paulo wmm->oui[0] = 0x00; 57e28a4053SRui Paulo wmm->oui[1] = 0x50; 58e28a4053SRui Paulo wmm->oui[2] = 0xf2; 59e28a4053SRui Paulo wmm->oui_type = WMM_OUI_TYPE; 60e28a4053SRui Paulo wmm->oui_subtype = WMM_OUI_SUBTYPE_PARAMETER_ELEMENT; 61e28a4053SRui Paulo wmm->version = WMM_VERSION; 62e28a4053SRui Paulo wmm->qos_info = hapd->parameter_set_count & 0xf; 63e28a4053SRui Paulo 64f05cddf9SRui Paulo if (hapd->conf->wmm_uapsd && 65f05cddf9SRui Paulo (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD)) 66e28a4053SRui Paulo wmm->qos_info |= 0x80; 67e28a4053SRui Paulo 68f05cddf9SRui Paulo wmm->reserved = 0; 69f05cddf9SRui Paulo 70e28a4053SRui Paulo /* fill in a parameter set record for each AC */ 71e28a4053SRui Paulo for (e = 0; e < 4; e++) { 72e28a4053SRui Paulo struct wmm_ac_parameter *ac = &wmm->ac[e]; 73e28a4053SRui Paulo struct hostapd_wmm_ac_params *acp = 74e28a4053SRui Paulo &hapd->iconf->wmm_ac_params[e]; 75e28a4053SRui Paulo 76e28a4053SRui Paulo ac->aci_aifsn = wmm_aci_aifsn(acp->aifs, 77e28a4053SRui Paulo acp->admission_control_mandatory, 78e28a4053SRui Paulo e); 79e28a4053SRui Paulo ac->cw = wmm_ecw(acp->cwmin, acp->cwmax); 80e28a4053SRui Paulo ac->txop_limit = host_to_le16(acp->txop_limit); 81e28a4053SRui Paulo } 82e28a4053SRui Paulo 83e28a4053SRui Paulo pos = (u8 *) (wmm + 1); 84e28a4053SRui Paulo eid[1] = pos - eid - 2; /* element length */ 85e28a4053SRui Paulo 86e28a4053SRui Paulo return pos; 87e28a4053SRui Paulo } 88e28a4053SRui Paulo 89e28a4053SRui Paulo 90f05cddf9SRui Paulo /* 91f05cddf9SRui Paulo * This function is called when a station sends an association request with 92f05cddf9SRui Paulo * WMM info element. The function returns 1 on success or 0 on any error in WMM 93f05cddf9SRui Paulo * element. eid does not include Element ID and Length octets. 94f05cddf9SRui Paulo */ 95e28a4053SRui Paulo int hostapd_eid_wmm_valid(struct hostapd_data *hapd, const u8 *eid, size_t len) 96e28a4053SRui Paulo { 97e28a4053SRui Paulo struct wmm_information_element *wmm; 98e28a4053SRui Paulo 99e28a4053SRui Paulo wpa_hexdump(MSG_MSGDUMP, "WMM IE", eid, len); 100e28a4053SRui Paulo 101e28a4053SRui Paulo if (len < sizeof(struct wmm_information_element)) { 102e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "Too short WMM IE (len=%lu)", 103e28a4053SRui Paulo (unsigned long) len); 104f05cddf9SRui Paulo return 0; 105e28a4053SRui Paulo } 106e28a4053SRui Paulo 107e28a4053SRui Paulo wmm = (struct wmm_information_element *) eid; 108e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "Validating WMM IE: OUI %02x:%02x:%02x " 109e28a4053SRui Paulo "OUI type %d OUI sub-type %d version %d QoS info 0x%x", 110e28a4053SRui Paulo wmm->oui[0], wmm->oui[1], wmm->oui[2], wmm->oui_type, 111e28a4053SRui Paulo wmm->oui_subtype, wmm->version, wmm->qos_info); 112e28a4053SRui Paulo if (wmm->oui_subtype != WMM_OUI_SUBTYPE_INFORMATION_ELEMENT || 113e28a4053SRui Paulo wmm->version != WMM_VERSION) { 114e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "Unsupported WMM IE Subtype/Version"); 115f05cddf9SRui Paulo return 0; 116e28a4053SRui Paulo } 117e28a4053SRui Paulo 118f05cddf9SRui Paulo return 1; 119e28a4053SRui Paulo } 120e28a4053SRui Paulo 121e28a4053SRui Paulo 122e28a4053SRui Paulo static void wmm_send_action(struct hostapd_data *hapd, const u8 *addr, 123e28a4053SRui Paulo const struct wmm_tspec_element *tspec, 124e28a4053SRui Paulo u8 action_code, u8 dialogue_token, u8 status_code) 125e28a4053SRui Paulo { 126e28a4053SRui Paulo u8 buf[256]; 127e28a4053SRui Paulo struct ieee80211_mgmt *m = (struct ieee80211_mgmt *) buf; 128e28a4053SRui Paulo struct wmm_tspec_element *t = (struct wmm_tspec_element *) 129e28a4053SRui Paulo m->u.action.u.wmm_action.variable; 130e28a4053SRui Paulo int len; 131e28a4053SRui Paulo 132e28a4053SRui Paulo hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211, 133e28a4053SRui Paulo HOSTAPD_LEVEL_DEBUG, 134e28a4053SRui Paulo "action response - reason %d", status_code); 135e28a4053SRui Paulo os_memset(buf, 0, sizeof(buf)); 136e28a4053SRui Paulo m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, 137e28a4053SRui Paulo WLAN_FC_STYPE_ACTION); 138e28a4053SRui Paulo os_memcpy(m->da, addr, ETH_ALEN); 139e28a4053SRui Paulo os_memcpy(m->sa, hapd->own_addr, ETH_ALEN); 140e28a4053SRui Paulo os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN); 141e28a4053SRui Paulo m->u.action.category = WLAN_ACTION_WMM; 142e28a4053SRui Paulo m->u.action.u.wmm_action.action_code = action_code; 143e28a4053SRui Paulo m->u.action.u.wmm_action.dialog_token = dialogue_token; 144e28a4053SRui Paulo m->u.action.u.wmm_action.status_code = status_code; 145e28a4053SRui Paulo os_memcpy(t, tspec, sizeof(struct wmm_tspec_element)); 146e28a4053SRui Paulo len = ((u8 *) (t + 1)) - buf; 147e28a4053SRui Paulo 148f05cddf9SRui Paulo if (hostapd_drv_send_mlme(hapd, m, len, 0) < 0) 1495b9c547cSRui Paulo wpa_printf(MSG_INFO, "wmm_send_action: send failed"); 150e28a4053SRui Paulo } 151e28a4053SRui Paulo 152e28a4053SRui Paulo 153e28a4053SRui Paulo int wmm_process_tspec(struct wmm_tspec_element *tspec) 154e28a4053SRui Paulo { 155*85732ac8SCy Schubert u64 medium_time; 156*85732ac8SCy Schubert unsigned int pps, duration; 157*85732ac8SCy Schubert unsigned int up, psb, dir, tid; 158e28a4053SRui Paulo u16 val, surplus; 159e28a4053SRui Paulo 160e28a4053SRui Paulo up = (tspec->ts_info[1] >> 3) & 0x07; 161e28a4053SRui Paulo psb = (tspec->ts_info[1] >> 2) & 0x01; 162e28a4053SRui Paulo dir = (tspec->ts_info[0] >> 5) & 0x03; 163e28a4053SRui Paulo tid = (tspec->ts_info[0] >> 1) & 0x0f; 164e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WMM: TS Info: UP=%d PSB=%d Direction=%d TID=%d", 165e28a4053SRui Paulo up, psb, dir, tid); 166e28a4053SRui Paulo val = le_to_host16(tspec->nominal_msdu_size); 167e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WMM: Nominal MSDU Size: %d%s", 168e28a4053SRui Paulo val & 0x7fff, val & 0x8000 ? " (fixed)" : ""); 169e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WMM: Mean Data Rate: %u bps", 170e28a4053SRui Paulo le_to_host32(tspec->mean_data_rate)); 171e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WMM: Minimum PHY Rate: %u bps", 172e28a4053SRui Paulo le_to_host32(tspec->minimum_phy_rate)); 173e28a4053SRui Paulo val = le_to_host16(tspec->surplus_bandwidth_allowance); 174e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance: %u.%04u", 175e28a4053SRui Paulo val >> 13, 10000 * (val & 0x1fff) / 0x2000); 176e28a4053SRui Paulo 177e28a4053SRui Paulo val = le_to_host16(tspec->nominal_msdu_size); 178e28a4053SRui Paulo if (val == 0) { 179e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WMM: Invalid Nominal MSDU Size (0)"); 180e28a4053SRui Paulo return WMM_ADDTS_STATUS_INVALID_PARAMETERS; 181e28a4053SRui Paulo } 182e28a4053SRui Paulo /* pps = Ceiling((Mean Data Rate / 8) / Nominal MSDU Size) */ 183e28a4053SRui Paulo pps = ((le_to_host32(tspec->mean_data_rate) / 8) + val - 1) / val; 184e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WMM: Packets-per-second estimate for TSPEC: %d", 185e28a4053SRui Paulo pps); 186e28a4053SRui Paulo 187e28a4053SRui Paulo if (le_to_host32(tspec->minimum_phy_rate) < 1000000) { 188e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WMM: Too small Minimum PHY Rate"); 189e28a4053SRui Paulo return WMM_ADDTS_STATUS_INVALID_PARAMETERS; 190e28a4053SRui Paulo } 191e28a4053SRui Paulo 192e28a4053SRui Paulo duration = (le_to_host16(tspec->nominal_msdu_size) & 0x7fff) * 8 / 193e28a4053SRui Paulo (le_to_host32(tspec->minimum_phy_rate) / 1000000) + 194e28a4053SRui Paulo 50 /* FIX: proper SIFS + ACK duration */; 195e28a4053SRui Paulo 196e28a4053SRui Paulo /* unsigned binary number with an implicit binary point after the 197e28a4053SRui Paulo * leftmost 3 bits, i.e., 0x2000 = 1.0 */ 198e28a4053SRui Paulo surplus = le_to_host16(tspec->surplus_bandwidth_allowance); 199e28a4053SRui Paulo if (surplus <= 0x2000) { 200e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance not " 201e28a4053SRui Paulo "greater than unity"); 202e28a4053SRui Paulo return WMM_ADDTS_STATUS_INVALID_PARAMETERS; 203e28a4053SRui Paulo } 204e28a4053SRui Paulo 205*85732ac8SCy Schubert medium_time = (u64) surplus * pps * duration / 0x2000; 206*85732ac8SCy Schubert wpa_printf(MSG_DEBUG, "WMM: Estimated medium time: %lu", 207*85732ac8SCy Schubert (unsigned long) medium_time); 208e28a4053SRui Paulo 209e28a4053SRui Paulo /* 210e28a4053SRui Paulo * TODO: store list of granted (and still active) TSPECs and check 211e28a4053SRui Paulo * whether there is available medium time for this request. For now, 212e28a4053SRui Paulo * just refuse requests that would by themselves take very large 213e28a4053SRui Paulo * portion of the available bandwidth. 214e28a4053SRui Paulo */ 215e28a4053SRui Paulo if (medium_time > 750000) { 216e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WMM: Refuse TSPEC request for over " 217e28a4053SRui Paulo "75%% of available bandwidth"); 218e28a4053SRui Paulo return WMM_ADDTS_STATUS_REFUSED; 219e28a4053SRui Paulo } 220e28a4053SRui Paulo 221e28a4053SRui Paulo /* Convert to 32 microseconds per second unit */ 222e28a4053SRui Paulo tspec->medium_time = host_to_le16(medium_time / 32); 223e28a4053SRui Paulo 224e28a4053SRui Paulo return WMM_ADDTS_STATUS_ADMISSION_ACCEPTED; 225e28a4053SRui Paulo } 226e28a4053SRui Paulo 227e28a4053SRui Paulo 228e28a4053SRui Paulo static void wmm_addts_req(struct hostapd_data *hapd, 229e28a4053SRui Paulo const struct ieee80211_mgmt *mgmt, 230e28a4053SRui Paulo struct wmm_tspec_element *tspec, size_t len) 231e28a4053SRui Paulo { 232e28a4053SRui Paulo const u8 *end = ((const u8 *) mgmt) + len; 233e28a4053SRui Paulo int res; 234e28a4053SRui Paulo 235e28a4053SRui Paulo if ((const u8 *) (tspec + 1) > end) { 236e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WMM: TSPEC overflow in ADDTS Request"); 237e28a4053SRui Paulo return; 238e28a4053SRui Paulo } 239e28a4053SRui Paulo 240e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WMM: ADDTS Request (Dialog Token %d) for TSPEC " 241e28a4053SRui Paulo "from " MACSTR, 242e28a4053SRui Paulo mgmt->u.action.u.wmm_action.dialog_token, 243e28a4053SRui Paulo MAC2STR(mgmt->sa)); 244e28a4053SRui Paulo 245e28a4053SRui Paulo res = wmm_process_tspec(tspec); 246e28a4053SRui Paulo wpa_printf(MSG_DEBUG, "WMM: ADDTS processing result: %d", res); 247e28a4053SRui Paulo 248e28a4053SRui Paulo wmm_send_action(hapd, mgmt->sa, tspec, WMM_ACTION_CODE_ADDTS_RESP, 249e28a4053SRui Paulo mgmt->u.action.u.wmm_action.dialog_token, res); 250e28a4053SRui Paulo } 251e28a4053SRui Paulo 252e28a4053SRui Paulo 253e28a4053SRui Paulo void hostapd_wmm_action(struct hostapd_data *hapd, 254e28a4053SRui Paulo const struct ieee80211_mgmt *mgmt, size_t len) 255e28a4053SRui Paulo { 256e28a4053SRui Paulo int action_code; 257e28a4053SRui Paulo int left = len - IEEE80211_HDRLEN - 4; 258e28a4053SRui Paulo const u8 *pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 4; 259e28a4053SRui Paulo struct ieee802_11_elems elems; 260e28a4053SRui Paulo struct sta_info *sta = ap_get_sta(hapd, mgmt->sa); 261e28a4053SRui Paulo 262e28a4053SRui Paulo /* check that the request comes from a valid station */ 263e28a4053SRui Paulo if (!sta || 264e28a4053SRui Paulo (sta->flags & (WLAN_STA_ASSOC | WLAN_STA_WMM)) != 265e28a4053SRui Paulo (WLAN_STA_ASSOC | WLAN_STA_WMM)) { 266e28a4053SRui Paulo hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211, 267e28a4053SRui Paulo HOSTAPD_LEVEL_DEBUG, 268e28a4053SRui Paulo "wmm action received is not from associated wmm" 269e28a4053SRui Paulo " station"); 270e28a4053SRui Paulo /* TODO: respond with action frame refused status code */ 271e28a4053SRui Paulo return; 272e28a4053SRui Paulo } 273e28a4053SRui Paulo 274325151a3SRui Paulo if (left < 0) 275325151a3SRui Paulo return; /* not a valid WMM Action frame */ 276325151a3SRui Paulo 277e28a4053SRui Paulo /* extract the tspec info element */ 278e28a4053SRui Paulo if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed) { 279e28a4053SRui Paulo hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211, 280e28a4053SRui Paulo HOSTAPD_LEVEL_DEBUG, 281e28a4053SRui Paulo "hostapd_wmm_action - could not parse wmm " 282e28a4053SRui Paulo "action"); 283e28a4053SRui Paulo /* TODO: respond with action frame invalid parameters status 284e28a4053SRui Paulo * code */ 285e28a4053SRui Paulo return; 286e28a4053SRui Paulo } 287e28a4053SRui Paulo 288e28a4053SRui Paulo if (!elems.wmm_tspec || 289e28a4053SRui Paulo elems.wmm_tspec_len != (sizeof(struct wmm_tspec_element) - 2)) { 290e28a4053SRui Paulo hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211, 291e28a4053SRui Paulo HOSTAPD_LEVEL_DEBUG, 292e28a4053SRui Paulo "hostapd_wmm_action - missing or wrong length " 293e28a4053SRui Paulo "tspec"); 294e28a4053SRui Paulo /* TODO: respond with action frame invalid parameters status 295e28a4053SRui Paulo * code */ 296e28a4053SRui Paulo return; 297e28a4053SRui Paulo } 298e28a4053SRui Paulo 299e28a4053SRui Paulo /* TODO: check the request is for an AC with ACM set, if not, refuse 300e28a4053SRui Paulo * request */ 301e28a4053SRui Paulo 302e28a4053SRui Paulo action_code = mgmt->u.action.u.wmm_action.action_code; 303e28a4053SRui Paulo switch (action_code) { 304e28a4053SRui Paulo case WMM_ACTION_CODE_ADDTS_REQ: 305e28a4053SRui Paulo wmm_addts_req(hapd, mgmt, (struct wmm_tspec_element *) 306e28a4053SRui Paulo (elems.wmm_tspec - 2), len); 307e28a4053SRui Paulo return; 308e28a4053SRui Paulo #if 0 309e28a4053SRui Paulo /* TODO: needed for client implementation */ 310e28a4053SRui Paulo case WMM_ACTION_CODE_ADDTS_RESP: 311e28a4053SRui Paulo wmm_setup_request(hapd, mgmt, len); 312e28a4053SRui Paulo return; 313e28a4053SRui Paulo /* TODO: handle station teardown requests */ 314e28a4053SRui Paulo case WMM_ACTION_CODE_DELTS: 315e28a4053SRui Paulo wmm_teardown(hapd, mgmt, len); 316e28a4053SRui Paulo return; 317e28a4053SRui Paulo #endif 318e28a4053SRui Paulo } 319e28a4053SRui Paulo 320e28a4053SRui Paulo hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211, 321e28a4053SRui Paulo HOSTAPD_LEVEL_DEBUG, 322e28a4053SRui Paulo "hostapd_wmm_action - unknown action code %d", 323e28a4053SRui Paulo action_code); 324e28a4053SRui Paulo } 325