wme.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 u8 wmm_oui[3] = { 0x00, 0x50, 0xf2 };
  25. static inline u8 wmm_aci_aifsn(int aifsn, int acm, int aci)
  26. {
  27. u8 ret;
  28. ret = (aifsn << WMM_AC_AIFNS_SHIFT) & WMM_AC_AIFSN_MASK;
  29. if (acm)
  30. ret |= WMM_AC_ACM;
  31. ret |= (aci << WMM_AC_ACI_SHIFT) & WMM_AC_ACI_MASK;
  32. return ret;
  33. }
  34. static inline u8 wmm_ecw(int ecwmin, int ecwmax)
  35. {
  36. return ((ecwmin << WMM_AC_ECWMIN_SHIFT) & WMM_AC_ECWMIN_MASK) |
  37. ((ecwmax << WMM_AC_ECWMAX_SHIFT) & WMM_AC_ECWMAX_MASK);
  38. }
  39. /*
  40. * Add WMM Parameter Element to Beacon, Probe Response, and (Re)Association
  41. * Response frames.
  42. */
  43. u8 * hostapd_eid_wmm(struct hostapd_data *hapd, u8 *eid)
  44. {
  45. u8 *pos = eid;
  46. struct wmm_parameter_element *wmm =
  47. (struct wmm_parameter_element *) (pos + 2);
  48. int e;
  49. if (!hapd->conf->wmm_enabled)
  50. return eid;
  51. eid[0] = WLAN_EID_VENDOR_SPECIFIC;
  52. wmm->oui[0] = 0x00;
  53. wmm->oui[1] = 0x50;
  54. wmm->oui[2] = 0xf2;
  55. wmm->oui_type = WMM_OUI_TYPE;
  56. wmm->oui_subtype = WMM_OUI_SUBTYPE_PARAMETER_ELEMENT;
  57. wmm->version = WMM_VERSION;
  58. wmm->qos_info = hapd->parameter_set_count & 0xf;
  59. /* fill in a parameter set record for each AC */
  60. for (e = 0; e < 4; e++) {
  61. struct wmm_ac_parameter *ac = &wmm->ac[e];
  62. struct hostapd_wmm_ac_params *acp =
  63. &hapd->iconf->wmm_ac_params[e];
  64. ac->aci_aifsn = wmm_aci_aifsn(acp->aifs,
  65. acp->admission_control_mandatory,
  66. e);
  67. ac->cw = wmm_ecw(acp->cwmin, acp->cwmax);
  68. ac->txop_limit = host_to_le16(acp->txop_limit);
  69. }
  70. pos = (u8 *) (wmm + 1);
  71. eid[1] = pos - eid - 2; /* element length */
  72. return pos;
  73. }
  74. /* This function is called when a station sends an association request with
  75. * WMM info element. The function returns zero on success or non-zero on any
  76. * error in WMM element. eid does not include Element ID and Length octets. */
  77. int hostapd_eid_wmm_valid(struct hostapd_data *hapd, u8 *eid, size_t len)
  78. {
  79. struct wmm_information_element *wmm;
  80. wpa_hexdump(MSG_MSGDUMP, "WMM IE", eid, len);
  81. if (len < sizeof(struct wmm_information_element)) {
  82. wpa_printf(MSG_DEBUG, "Too short WMM IE (len=%lu)",
  83. (unsigned long) len);
  84. return -1;
  85. }
  86. wmm = (struct wmm_information_element *) eid;
  87. wpa_printf(MSG_DEBUG, "Validating WMM IE: OUI %02x:%02x:%02x "
  88. "OUI type %d OUI sub-type %d version %d",
  89. wmm->oui[0], wmm->oui[1], wmm->oui[2], wmm->oui_type,
  90. wmm->oui_subtype, wmm->version);
  91. if (os_memcmp(wmm->oui, wmm_oui, sizeof(wmm_oui)) != 0 ||
  92. wmm->oui_type != WMM_OUI_TYPE ||
  93. wmm->oui_subtype != WMM_OUI_SUBTYPE_INFORMATION_ELEMENT ||
  94. wmm->version != WMM_VERSION) {
  95. wpa_printf(MSG_DEBUG, "Unsupported WMM IE OUI/Type/Subtype/"
  96. "Version");
  97. return -1;
  98. }
  99. return 0;
  100. }
  101. /* This function is called when a station sends an ACK frame for an AssocResp
  102. * frame (status=success) and the matching AssocReq contained a WMM element.
  103. */
  104. int hostapd_wmm_sta_config(struct hostapd_data *hapd, struct sta_info *sta)
  105. {
  106. /* update kernel STA data for WMM related items (WLAN_STA_WPA flag) */
  107. if (sta->flags & WLAN_STA_WMM)
  108. hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
  109. WLAN_STA_WMM, ~0);
  110. else
  111. hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
  112. 0, ~WLAN_STA_WMM);
  113. return 0;
  114. }
  115. static void wmm_send_action(struct hostapd_data *hapd, const u8 *addr,
  116. const struct wmm_tspec_element *tspec,
  117. u8 action_code, u8 dialogue_token, u8 status_code)
  118. {
  119. u8 buf[256];
  120. struct ieee80211_mgmt *m = (struct ieee80211_mgmt *) buf;
  121. struct wmm_tspec_element *t = (struct wmm_tspec_element *)
  122. m->u.action.u.wmm_action.variable;
  123. int len;
  124. hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
  125. HOSTAPD_LEVEL_DEBUG,
  126. "action response - reason %d", status_code);
  127. os_memset(buf, 0, sizeof(buf));
  128. m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  129. WLAN_FC_STYPE_ACTION);
  130. os_memcpy(m->da, addr, ETH_ALEN);
  131. os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
  132. os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
  133. m->u.action.category = WLAN_ACTION_WMM;
  134. m->u.action.u.wmm_action.action_code = action_code;
  135. m->u.action.u.wmm_action.dialog_token = dialogue_token;
  136. m->u.action.u.wmm_action.status_code = status_code;
  137. os_memcpy(t, tspec, sizeof(struct wmm_tspec_element));
  138. len = ((u8 *) (t + 1)) - buf;
  139. if (hostapd_send_mgmt_frame(hapd, m, len, 0) < 0)
  140. perror("wmm_send_action: send");
  141. }
  142. /* given frame data payload size in bytes, and data_rate in bits per second
  143. * returns time to complete frame exchange */
  144. /* FIX: should not use floating point types */
  145. static double wmm_frame_exchange_time(int bytes, int data_rate, int encryption,
  146. int cts_protection)
  147. {
  148. /* TODO: account for MAC/PHY headers correctly */
  149. /* TODO: account for encryption headers */
  150. /* TODO: account for WDS headers */
  151. /* TODO: account for CTS protection */
  152. /* TODO: account for SIFS + ACK at minimum PHY rate */
  153. return (bytes + 400) * 8.0 / data_rate;
  154. }
  155. static void wmm_addts_req(struct hostapd_data *hapd,
  156. struct ieee80211_mgmt *mgmt,
  157. struct wmm_tspec_element *tspec, size_t len)
  158. {
  159. /* FIX: should not use floating point types */
  160. double medium_time, pps;
  161. /* TODO: account for airtime and answer no to tspec setup requests
  162. * when none left!! */
  163. pps = (le_to_host32(tspec->mean_data_rate) / 8.0) /
  164. le_to_host16(tspec->nominal_msdu_size);
  165. medium_time = (le_to_host16(tspec->surplus_bandwidth_allowance) / 8) *
  166. pps *
  167. wmm_frame_exchange_time(le_to_host16(tspec->nominal_msdu_size),
  168. le_to_host32(tspec->minimum_phy_rate),
  169. 0, 0);
  170. tspec->medium_time = host_to_le16(medium_time * 1000000.0 / 32.0);
  171. wmm_send_action(hapd, mgmt->sa, tspec, WMM_ACTION_CODE_ADDTS_RESP,
  172. mgmt->u.action.u.wmm_action.dialog_token,
  173. WMM_ADDTS_STATUS_ADMISSION_ACCEPTED);
  174. }
  175. void hostapd_wmm_action(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
  176. size_t len)
  177. {
  178. int action_code;
  179. int left = len - IEEE80211_HDRLEN - 4;
  180. u8 *pos = ((u8 *) mgmt) + IEEE80211_HDRLEN + 4;
  181. struct ieee802_11_elems elems;
  182. struct sta_info *sta = ap_get_sta(hapd, mgmt->sa);
  183. /* check that the request comes from a valid station */
  184. if (!sta ||
  185. (sta->flags & (WLAN_STA_ASSOC | WLAN_STA_WMM)) !=
  186. (WLAN_STA_ASSOC | WLAN_STA_WMM)) {
  187. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  188. HOSTAPD_LEVEL_DEBUG,
  189. "wmm action received is not from associated wmm"
  190. " station");
  191. /* TODO: respond with action frame refused status code */
  192. return;
  193. }
  194. /* extract the tspec info element */
  195. if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed) {
  196. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  197. HOSTAPD_LEVEL_DEBUG,
  198. "hostapd_wmm_action - could not parse wmm "
  199. "action");
  200. /* TODO: respond with action frame invalid parameters status
  201. * code */
  202. return;
  203. }
  204. if (!elems.wmm_tspec ||
  205. elems.wmm_tspec_len != (sizeof(struct wmm_tspec_element) - 2)) {
  206. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  207. HOSTAPD_LEVEL_DEBUG,
  208. "hostapd_wmm_action - missing or wrong length "
  209. "tspec");
  210. /* TODO: respond with action frame invalid parameters status
  211. * code */
  212. return;
  213. }
  214. /* TODO: check the request is for an AC with ACM set, if not, refuse
  215. * request */
  216. action_code = mgmt->u.action.u.wmm_action.action_code;
  217. switch (action_code) {
  218. case WMM_ACTION_CODE_ADDTS_REQ:
  219. wmm_addts_req(hapd, mgmt, (struct wmm_tspec_element *)
  220. (elems.wmm_tspec - 2), len);
  221. return;
  222. #if 0
  223. /* TODO: needed for client implementation */
  224. case WMM_ACTION_CODE_ADDTS_RESP:
  225. wmm_setup_request(hapd, mgmt, len);
  226. return;
  227. /* TODO: handle station teardown requests */
  228. case WMM_ACTION_CODE_DELTS:
  229. wmm_teardown(hapd, mgmt, len);
  230. return;
  231. #endif
  232. }
  233. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  234. HOSTAPD_LEVEL_DEBUG,
  235. "hostapd_wmm_action - unknown action code %d",
  236. action_code);
  237. }