wmm.c 10 KB

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