You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. wmm->qos_info |= 0x80;
  65. wmm->reserved = 0;
  66. /* fill in a parameter set record for each AC */
  67. for (e = 0; e < 4; e++) {
  68. struct wmm_ac_parameter *ac = &wmm->ac[e];
  69. struct hostapd_wmm_ac_params *acp =
  70. &hapd->iconf->wmm_ac_params[e];
  71. ac->aci_aifsn = wmm_aci_aifsn(acp->aifs,
  72. acp->admission_control_mandatory,
  73. e);
  74. ac->cw = wmm_ecw(acp->cwmin, acp->cwmax);
  75. ac->txop_limit = host_to_le16(acp->txop_limit);
  76. }
  77. pos = (u8 *) (wmm + 1);
  78. eid[1] = pos - eid - 2; /* element length */
  79. return pos;
  80. }
  81. /* This function is called when a station sends an association request with
  82. * WMM info element. The function returns zero on success or non-zero on any
  83. * error in WMM element. eid does not include Element ID and Length octets. */
  84. int hostapd_eid_wmm_valid(struct hostapd_data *hapd, const u8 *eid, size_t len)
  85. {
  86. struct wmm_information_element *wmm;
  87. wpa_hexdump(MSG_MSGDUMP, "WMM IE", eid, len);
  88. if (len < sizeof(struct wmm_information_element)) {
  89. wpa_printf(MSG_DEBUG, "Too short WMM IE (len=%lu)",
  90. (unsigned long) len);
  91. return -1;
  92. }
  93. wmm = (struct wmm_information_element *) eid;
  94. wpa_printf(MSG_DEBUG, "Validating WMM IE: OUI %02x:%02x:%02x "
  95. "OUI type %d OUI sub-type %d version %d QoS info 0x%x",
  96. wmm->oui[0], wmm->oui[1], wmm->oui[2], wmm->oui_type,
  97. wmm->oui_subtype, wmm->version, wmm->qos_info);
  98. if (wmm->oui_subtype != WMM_OUI_SUBTYPE_INFORMATION_ELEMENT ||
  99. wmm->version != WMM_VERSION) {
  100. wpa_printf(MSG_DEBUG, "Unsupported WMM IE Subtype/Version");
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. static void wmm_send_action(struct hostapd_data *hapd, const u8 *addr,
  106. const struct wmm_tspec_element *tspec,
  107. u8 action_code, u8 dialogue_token, u8 status_code)
  108. {
  109. u8 buf[256];
  110. struct ieee80211_mgmt *m = (struct ieee80211_mgmt *) buf;
  111. struct wmm_tspec_element *t = (struct wmm_tspec_element *)
  112. m->u.action.u.wmm_action.variable;
  113. int len;
  114. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  115. HOSTAPD_LEVEL_DEBUG,
  116. "action response - reason %d", status_code);
  117. os_memset(buf, 0, sizeof(buf));
  118. m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  119. WLAN_FC_STYPE_ACTION);
  120. os_memcpy(m->da, addr, ETH_ALEN);
  121. os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
  122. os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
  123. m->u.action.category = WLAN_ACTION_WMM;
  124. m->u.action.u.wmm_action.action_code = action_code;
  125. m->u.action.u.wmm_action.dialog_token = dialogue_token;
  126. m->u.action.u.wmm_action.status_code = status_code;
  127. os_memcpy(t, tspec, sizeof(struct wmm_tspec_element));
  128. len = ((u8 *) (t + 1)) - buf;
  129. if (hostapd_drv_send_mlme(hapd, m, len) < 0)
  130. perror("wmm_send_action: send");
  131. }
  132. int wmm_process_tspec(struct wmm_tspec_element *tspec)
  133. {
  134. int medium_time, pps, duration;
  135. int up, psb, dir, tid;
  136. u16 val, surplus;
  137. up = (tspec->ts_info[1] >> 3) & 0x07;
  138. psb = (tspec->ts_info[1] >> 2) & 0x01;
  139. dir = (tspec->ts_info[0] >> 5) & 0x03;
  140. tid = (tspec->ts_info[0] >> 1) & 0x0f;
  141. wpa_printf(MSG_DEBUG, "WMM: TS Info: UP=%d PSB=%d Direction=%d TID=%d",
  142. up, psb, dir, tid);
  143. val = le_to_host16(tspec->nominal_msdu_size);
  144. wpa_printf(MSG_DEBUG, "WMM: Nominal MSDU Size: %d%s",
  145. val & 0x7fff, val & 0x8000 ? " (fixed)" : "");
  146. wpa_printf(MSG_DEBUG, "WMM: Mean Data Rate: %u bps",
  147. le_to_host32(tspec->mean_data_rate));
  148. wpa_printf(MSG_DEBUG, "WMM: Minimum PHY Rate: %u bps",
  149. le_to_host32(tspec->minimum_phy_rate));
  150. val = le_to_host16(tspec->surplus_bandwidth_allowance);
  151. wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance: %u.%04u",
  152. val >> 13, 10000 * (val & 0x1fff) / 0x2000);
  153. val = le_to_host16(tspec->nominal_msdu_size);
  154. if (val == 0) {
  155. wpa_printf(MSG_DEBUG, "WMM: Invalid Nominal MSDU Size (0)");
  156. return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
  157. }
  158. /* pps = Ceiling((Mean Data Rate / 8) / Nominal MSDU Size) */
  159. pps = ((le_to_host32(tspec->mean_data_rate) / 8) + val - 1) / val;
  160. wpa_printf(MSG_DEBUG, "WMM: Packets-per-second estimate for TSPEC: %d",
  161. pps);
  162. if (le_to_host32(tspec->minimum_phy_rate) < 1000000) {
  163. wpa_printf(MSG_DEBUG, "WMM: Too small Minimum PHY Rate");
  164. return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
  165. }
  166. duration = (le_to_host16(tspec->nominal_msdu_size) & 0x7fff) * 8 /
  167. (le_to_host32(tspec->minimum_phy_rate) / 1000000) +
  168. 50 /* FIX: proper SIFS + ACK duration */;
  169. /* unsigned binary number with an implicit binary point after the
  170. * leftmost 3 bits, i.e., 0x2000 = 1.0 */
  171. surplus = le_to_host16(tspec->surplus_bandwidth_allowance);
  172. if (surplus <= 0x2000) {
  173. wpa_printf(MSG_DEBUG, "WMM: Surplus Bandwidth Allowance not "
  174. "greater than unity");
  175. return WMM_ADDTS_STATUS_INVALID_PARAMETERS;
  176. }
  177. medium_time = surplus * pps * duration / 0x2000;
  178. wpa_printf(MSG_DEBUG, "WMM: Estimated medium time: %u", medium_time);
  179. /*
  180. * TODO: store list of granted (and still active) TSPECs and check
  181. * whether there is available medium time for this request. For now,
  182. * just refuse requests that would by themselves take very large
  183. * portion of the available bandwidth.
  184. */
  185. if (medium_time > 750000) {
  186. wpa_printf(MSG_DEBUG, "WMM: Refuse TSPEC request for over "
  187. "75%% of available bandwidth");
  188. return WMM_ADDTS_STATUS_REFUSED;
  189. }
  190. /* Convert to 32 microseconds per second unit */
  191. tspec->medium_time = host_to_le16(medium_time / 32);
  192. return WMM_ADDTS_STATUS_ADMISSION_ACCEPTED;
  193. }
  194. static void wmm_addts_req(struct hostapd_data *hapd,
  195. const struct ieee80211_mgmt *mgmt,
  196. struct wmm_tspec_element *tspec, size_t len)
  197. {
  198. const u8 *end = ((const u8 *) mgmt) + len;
  199. int res;
  200. if ((const u8 *) (tspec + 1) > end) {
  201. wpa_printf(MSG_DEBUG, "WMM: TSPEC overflow in ADDTS Request");
  202. return;
  203. }
  204. wpa_printf(MSG_DEBUG, "WMM: ADDTS Request (Dialog Token %d) for TSPEC "
  205. "from " MACSTR,
  206. mgmt->u.action.u.wmm_action.dialog_token,
  207. MAC2STR(mgmt->sa));
  208. res = wmm_process_tspec(tspec);
  209. wpa_printf(MSG_DEBUG, "WMM: ADDTS processing result: %d", res);
  210. wmm_send_action(hapd, mgmt->sa, tspec, WMM_ACTION_CODE_ADDTS_RESP,
  211. mgmt->u.action.u.wmm_action.dialog_token, res);
  212. }
  213. void hostapd_wmm_action(struct hostapd_data *hapd,
  214. const struct ieee80211_mgmt *mgmt, size_t len)
  215. {
  216. int action_code;
  217. int left = len - IEEE80211_HDRLEN - 4;
  218. const u8 *pos = ((const u8 *) mgmt) + IEEE80211_HDRLEN + 4;
  219. struct ieee802_11_elems elems;
  220. struct sta_info *sta = ap_get_sta(hapd, mgmt->sa);
  221. /* check that the request comes from a valid station */
  222. if (!sta ||
  223. (sta->flags & (WLAN_STA_ASSOC | WLAN_STA_WMM)) !=
  224. (WLAN_STA_ASSOC | WLAN_STA_WMM)) {
  225. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  226. HOSTAPD_LEVEL_DEBUG,
  227. "wmm action received is not from associated wmm"
  228. " station");
  229. /* TODO: respond with action frame refused status code */
  230. return;
  231. }
  232. /* extract the tspec info element */
  233. if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed) {
  234. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  235. HOSTAPD_LEVEL_DEBUG,
  236. "hostapd_wmm_action - could not parse wmm "
  237. "action");
  238. /* TODO: respond with action frame invalid parameters status
  239. * code */
  240. return;
  241. }
  242. if (!elems.wmm_tspec ||
  243. elems.wmm_tspec_len != (sizeof(struct wmm_tspec_element) - 2)) {
  244. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  245. HOSTAPD_LEVEL_DEBUG,
  246. "hostapd_wmm_action - missing or wrong length "
  247. "tspec");
  248. /* TODO: respond with action frame invalid parameters status
  249. * code */
  250. return;
  251. }
  252. /* TODO: check the request is for an AC with ACM set, if not, refuse
  253. * request */
  254. action_code = mgmt->u.action.u.wmm_action.action_code;
  255. switch (action_code) {
  256. case WMM_ACTION_CODE_ADDTS_REQ:
  257. wmm_addts_req(hapd, mgmt, (struct wmm_tspec_element *)
  258. (elems.wmm_tspec - 2), len);
  259. return;
  260. #if 0
  261. /* TODO: needed for client implementation */
  262. case WMM_ACTION_CODE_ADDTS_RESP:
  263. wmm_setup_request(hapd, mgmt, len);
  264. return;
  265. /* TODO: handle station teardown requests */
  266. case WMM_ACTION_CODE_DELTS:
  267. wmm_teardown(hapd, mgmt, len);
  268. return;
  269. #endif
  270. }
  271. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  272. HOSTAPD_LEVEL_DEBUG,
  273. "hostapd_wmm_action - unknown action code %d",
  274. action_code);
  275. }