wme.c 10 KB

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