wmm.c
Go to the documentation of this file.
00001 /*
00002  * hostapd / WMM (Wi-Fi Multimedia)
00003  * Copyright 2002-2003, Instant802 Networks, Inc.
00004  * Copyright 2005-2006, Devicescape Software, Inc.
00005  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License version 2 as
00009  * published by the Free Software Foundation.
00010  *
00011  * Alternatively, this software may be distributed under the terms of BSD
00012  * license.
00013  *
00014  * See README and COPYING for more details.
00015  */
00016 
00017 #include "utils/includes.h"
00018 
00019 #include "utils/common.h"
00020 #include "common/ieee802_11_defs.h"
00021 #include "common/ieee802_11_common.h"
00022 #include "hostapd.h"
00023 #include "ieee802_11.h"
00024 #include "sta_info.h"
00025 #include "ap_config.h"
00026 #include "wmm.h"
00027 
00028 
00029 /* TODO: maintain separate sequence and fragment numbers for each AC
00030  * TODO: IGMP snooping to track which multicasts to forward - and use QOS-DATA
00031  * if only WMM stations are receiving a certain group */
00032 
00033 
00034 static inline u8 wmm_aci_aifsn(int aifsn, int acm, int aci)
00035 {
00036         u8 ret;
00037         ret = (aifsn << WMM_AC_AIFNS_SHIFT) & WMM_AC_AIFSN_MASK;
00038         if (acm)
00039                 ret |= WMM_AC_ACM;
00040         ret |= (aci << WMM_AC_ACI_SHIFT) & WMM_AC_ACI_MASK;
00041         return ret;
00042 }
00043 
00044 
00045 static inline u8 wmm_ecw(int ecwmin, int ecwmax)
00046 {
00047         return ((ecwmin << WMM_AC_ECWMIN_SHIFT) & WMM_AC_ECWMIN_MASK) |
00048                 ((ecwmax << WMM_AC_ECWMAX_SHIFT) & WMM_AC_ECWMAX_MASK);
00049 }
00050 
00051 
00052 /*
00053  * Add WMM Parameter Element to Beacon, Probe Response, and (Re)Association
00054  * Response frames.
00055  */
00056 u8 * hostapd_eid_wmm(struct hostapd_data *hapd, u8 *eid)
00057 {
00058         u8 *pos = eid;
00059         struct wmm_parameter_element *wmm =
00060                 (struct wmm_parameter_element *) (pos + 2);
00061         int e;
00062 
00063         if (!hapd->conf->wmm_enabled)
00064                 return eid;
00065         eid[0] = WLAN_EID_VENDOR_SPECIFIC;
00066         wmm->oui[0] = 0x00;
00067         wmm->oui[1] = 0x50;
00068         wmm->oui[2] = 0xf2;
00069         wmm->oui_type = WMM_OUI_TYPE;
00070         wmm->oui_subtype = WMM_OUI_SUBTYPE_PARAMETER_ELEMENT;
00071         wmm->version = WMM_VERSION;
00072         wmm->qos_info = hapd->parameter_set_count & 0xf;
00073 
00074         if (hapd->conf->wmm_uapsd)
00075                 wmm->qos_info |= 0x80;
00076 
00077         /* fill in a parameter set record for each AC */
00078         for (e = 0; e < 4; e++) {
00079                 struct wmm_ac_parameter *ac = &wmm->ac[e];
00080                 struct hostapd_wmm_ac_params *acp =
00081                         &hapd->iconf->wmm_ac_params[e];
00082 
00083                 ac->aci_aifsn = wmm_aci_aifsn(acp->aifs,
00084                                               acp->admission_control_mandatory,
00085                                               e);
00086                 ac->cw = wmm_ecw(acp->cwmin, acp->cwmax);
00087                 ac->txop_limit = host_to_le16(acp->txop_limit);
00088         }
00089 
00090         pos = (u8 *) (wmm + 1);
00091         eid[1] = pos - eid - 2; /* element length */
00092 
00093         return pos;
00094 }
00095 
00096 
00097 /* This function is called when a station sends an association request with
00098  * WMM info element. The function returns zero on success or non-zero on any
00099  * error in WMM element. eid does not include Element ID and Length octets. */
00100 int hostapd_eid_wmm_valid(struct hostapd_data *hapd, const u8 *eid, size_t len)
00101 {
00102         struct wmm_information_element *wmm;
00103 
00104         wpa_hexdump(MSG_MSGDUMP, "WMM IE", eid, len);
00105 
00106         if (len < sizeof(struct wmm_information_element)) {
00107                 wpa_printf(MSG_DEBUG, "Too short WMM IE (len=%lu)",
00108                            (unsigned long) len);
00109                 return -1;
00110         }
00111 
00112         wmm = (struct wmm_information_element *) eid;
00113         wpa_printf(MSG_DEBUG, "Validating WMM IE: OUI %02x:%02x:%02x  "
00114                    "OUI type %d  OUI sub-type %d  version %d  QoS info 0x%x",
00115                    wmm->oui[0], wmm->oui[1], wmm->oui[2], wmm->oui_type,
00116                    wmm->oui_subtype, wmm->version, wmm->qos_info);
00117         if (wmm->oui_subtype != WMM_OUI_SUBTYPE_INFORMATION_ELEMENT ||
00118             wmm->version != WMM_VERSION) {
00119                 wpa_printf(MSG_DEBUG, "Unsupported WMM IE Subtype/Version");
00120                 return -1;
00121         }
00122 
00123         return 0;
00124 }
00125 
00126 
00127 static void wmm_send_action(struct hostapd_data *hapd, const u8 *addr,
00128                             const struct wmm_tspec_element *tspec,
00129                             u8 action_code, u8 dialogue_token, u8 status_code)
00130 {
00131         u8 buf[256];
00132         struct ieee80211_mgmt *m = (struct ieee80211_mgmt *) buf;
00133         struct wmm_tspec_element *t = (struct wmm_tspec_element *)
00134                 m->u.action.u.wmm_action.variable;
00135         int len;
00136 
00137         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
00138                        HOSTAPD_LEVEL_DEBUG,
00139                        "action response - reason %d", status_code);
00140         os_memset(buf, 0, sizeof(buf));
00141         m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
00142                                         WLAN_FC_STYPE_ACTION);
00143         os_memcpy(m->da, addr, ETH_ALEN);
00144         os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
00145         os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
00146         m->u.action.category = WLAN_ACTION_WMM;
00147         m->u.action.u.wmm_action.action_code = action_code;
00148         m->u.action.u.wmm_action.dialog_token = dialogue_token;
00149         m->u.action.u.wmm_action.status_code = status_code;
00150         os_memcpy(t, tspec, sizeof(struct wmm_tspec_element));
00151         len = ((u8 *) (t + 1)) - buf;
00152 
00153         if (hapd->drv.send_mgmt_frame(hapd, m, len) < 0)
00154                 perror("wmm_send_action: send");
00155 }
00156 
00157 
00158 int wmm_process_tspec(struct wmm_tspec_element *tspec)
00159 {
00160         int medium_time, pps, duration;
00161         int up, psb, dir, tid;
00162         u16 val, surplus;
00163 
00164         up = (tspec->ts_info[1] >> 3) & 0x07;
00165         psb = (tspec->ts_info[1] >> 2) & 0x01;
00166         dir = (tspec->ts_info[0] >> 5) & 0x03;
00167         tid = (tspec->ts_info[0] >> 1) & 0x0f;
00168         wpa_printf(MSG_DEBUG, "WMM: TS Info: UP=%d PSB=%d Direction=%d TID=%d",
00169                    up, psb, dir, tid);
00170         val = le_to_host16(tspec->nominal_msdu_size);
00171         wpa_printf(MSG_DEBUG, "WMM: Nominal MSDU Size: %d%s",
00172                    val & 0x7fff, val & 0x8000 ? " (fixed)" : "");
00173         wpa_printf(MSG_DEBUG, "WMM: Mean Data Rate: %u bps",
00174                    le_to_host32(tspec->mean_data_rate));
00175         wpa_printf(MSG_DEBUG, "WMM: Minimum PHY Rate: %u bps",
00176                    le_to_host32(tspec->minimum_phy_rate));
00177         val = le_to_host16(tspec->surplus_bandwidth_allowance);
00178         wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance: %u.%04u",
00179                    val >> 13, 10000 * (val & 0x1fff) / 0x2000);
00180 
00181         val = le_to_host16(tspec->nominal_msdu_size);
00182         if (val == 0) {
00183                 wpa_printf(MSG_DEBUG, "WMM: Invalid Nominal MSDU Size (0)");
00184                 return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
00185         }
00186         /* pps = Ceiling((Mean Data Rate / 8) / Nominal MSDU Size) */
00187         pps = ((le_to_host32(tspec->mean_data_rate) / 8) + val - 1) / val;
00188         wpa_printf(MSG_DEBUG, "WMM: Packets-per-second estimate for TSPEC: %d",
00189                    pps);
00190 
00191         if (le_to_host32(tspec->minimum_phy_rate) < 1000000) {
00192                 wpa_printf(MSG_DEBUG, "WMM: Too small Minimum PHY Rate");
00193                 return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
00194         }
00195 
00196         duration = (le_to_host16(tspec->nominal_msdu_size) & 0x7fff) * 8 /
00197                 (le_to_host32(tspec->minimum_phy_rate) / 1000000) +
00198                 50 /* FIX: proper SIFS + ACK duration */;
00199 
00200         /* unsigned binary number with an implicit binary point after the
00201          * leftmost 3 bits, i.e., 0x2000 = 1.0 */
00202         surplus = le_to_host16(tspec->surplus_bandwidth_allowance);
00203         if (surplus <= 0x2000) {
00204                 wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance not "
00205                            "greater than unity");
00206                 return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
00207         }
00208 
00209         medium_time = surplus * pps * duration / 0x2000;
00210         wpa_printf(MSG_DEBUG, "WMM: Estimated medium time: %u", medium_time);
00211 
00212         /*
00213          * TODO: store list of granted (and still active) TSPECs and check
00214          * whether there is available medium time for this request. For now,
00215          * just refuse requests that would by themselves take very large
00216          * portion of the available bandwidth.
00217          */
00218         if (medium_time > 750000) {
00219                 wpa_printf(MSG_DEBUG, "WMM: Refuse TSPEC request for over "
00220                            "75%% of available bandwidth");
00221                 return WMM_ADDTS_STATUS_REFUSED;
00222         }
00223 
00224         /* Convert to 32 microseconds per second unit */
00225         tspec->medium_time = host_to_le16(medium_time / 32);
00226 
00227         return WMM_ADDTS_STATUS_ADMISSION_ACCEPTED;
00228 }
00229 
00230 
00231 static void wmm_addts_req(struct hostapd_data *hapd,
00232                           const struct ieee80211_mgmt *mgmt,
00233                           struct wmm_tspec_element *tspec, size_t len)
00234 {
00235         const u8 *end = ((const u8 *) mgmt) + len;
00236         int res;
00237 
00238         if ((const u8 *) (tspec + 1) > end) {
00239                 wpa_printf(MSG_DEBUG, "WMM: TSPEC overflow in ADDTS Request");
00240                 return;
00241         }
00242 
00243         wpa_printf(MSG_DEBUG, "WMM: ADDTS Request (Dialog Token %d) for TSPEC "
00244                    "from " MACSTR,
00245                    mgmt->u.action.u.wmm_action.dialog_token,
00246                    MAC2STR(mgmt->sa));
00247 
00248         res = wmm_process_tspec(tspec);
00249         wpa_printf(MSG_DEBUG, "WMM: ADDTS processing result: %d", res);
00250 
00251         wmm_send_action(hapd, mgmt->sa, tspec, WMM_ACTION_CODE_ADDTS_RESP,
00252                         mgmt->u.action.u.wmm_action.dialog_token, res);
00253 }
00254 
00255 
00256 void hostapd_wmm_action(struct hostapd_data *hapd,
00257                         const struct ieee80211_mgmt *mgmt, size_t len)
00258 {
00259         int action_code;
00260         int left = len - IEEE80211_HDRLEN - 4;
00261         const u8 *pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 4;
00262         struct ieee802_11_elems elems;
00263         struct sta_info *sta = ap_get_sta(hapd, mgmt->sa);
00264 
00265         /* check that the request comes from a valid station */
00266         if (!sta ||
00267             (sta->flags & (WLAN_STA_ASSOC | WLAN_STA_WMM)) !=
00268             (WLAN_STA_ASSOC | WLAN_STA_WMM)) {
00269                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
00270                                HOSTAPD_LEVEL_DEBUG,
00271                                "wmm action received is not from associated wmm"
00272                                " station");
00273                 /* TODO: respond with action frame refused status code */
00274                 return;
00275         }
00276 
00277         /* extract the tspec info element */
00278         if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed) {
00279                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
00280                                HOSTAPD_LEVEL_DEBUG,
00281                                "hostapd_wmm_action - could not parse wmm "
00282                                "action");
00283                 /* TODO: respond with action frame invalid parameters status
00284                  * code */
00285                 return;
00286         }
00287 
00288         if (!elems.wmm_tspec ||
00289             elems.wmm_tspec_len != (sizeof(struct wmm_tspec_element) - 2)) {
00290                 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
00291                                HOSTAPD_LEVEL_DEBUG,
00292                                "hostapd_wmm_action - missing or wrong length "
00293                                "tspec");
00294                 /* TODO: respond with action frame invalid parameters status
00295                  * code */
00296                 return;
00297         }
00298 
00299         /* TODO: check the request is for an AC with ACM set, if not, refuse
00300          * request */
00301 
00302         action_code = mgmt->u.action.u.wmm_action.action_code;
00303         switch (action_code) {
00304         case WMM_ACTION_CODE_ADDTS_REQ:
00305                 wmm_addts_req(hapd, mgmt, (struct wmm_tspec_element *)
00306                               (elems.wmm_tspec - 2), len);
00307                 return;
00308 #if 0
00309         /* TODO: needed for client implementation */
00310         case WMM_ACTION_CODE_ADDTS_RESP:
00311                 wmm_setup_request(hapd, mgmt, len);
00312                 return;
00313         /* TODO: handle station teardown requests */
00314         case WMM_ACTION_CODE_DELTS:
00315                 wmm_teardown(hapd, mgmt, len);
00316                 return;
00317 #endif
00318         }
00319 
00320         hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
00321                        HOSTAPD_LEVEL_DEBUG,
00322                        "hostapd_wmm_action - unknown action code %d",
00323                        action_code);
00324 }


wpa_supplicant_node
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:33:21