wme.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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) < 0)
  136. perror("wmm_send_action: send");
  137. }
  138. /* given frame data payload size in bytes, and data_rate in bits per second
  139. * returns time to complete frame exchange */
  140. /* FIX: should not use floating point types */
  141. static double wmm_frame_exchange_time(int bytes, int data_rate, int encryption,
  142. int cts_protection)
  143. {
  144. /* TODO: account for MAC/PHY headers correctly */
  145. /* TODO: account for encryption headers */
  146. /* TODO: account for WDS headers */
  147. /* TODO: account for CTS protection */
  148. /* TODO: account for SIFS + ACK at minimum PHY rate */
  149. return (bytes + 400) * 8.0 / data_rate;
  150. }
  151. static void wmm_addts_req(struct hostapd_data *hapd,
  152. struct ieee80211_mgmt *mgmt,
  153. struct wmm_tspec_element *tspec, size_t len)
  154. {
  155. /* FIX: should not use floating point types */
  156. double medium_time, pps;
  157. /* TODO: account for airtime and answer no to tspec setup requests
  158. * when none left!! */
  159. pps = (le_to_host32(tspec->mean_data_rate) / 8.0) /
  160. le_to_host16(tspec->nominal_msdu_size);
  161. medium_time = (le_to_host16(tspec->surplus_bandwidth_allowance) / 8) *
  162. pps *
  163. wmm_frame_exchange_time(le_to_host16(tspec->nominal_msdu_size),
  164. le_to_host32(tspec->minimum_phy_rate),
  165. 0, 0);
  166. tspec->medium_time = host_to_le16(medium_time * 1000000.0 / 32.0);
  167. wmm_send_action(hapd, mgmt->sa, tspec, WMM_ACTION_CODE_ADDTS_RESP,
  168. mgmt->u.action.u.wmm_action.dialog_token,
  169. WMM_ADDTS_STATUS_ADMISSION_ACCEPTED);
  170. }
  171. void hostapd_wmm_action(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
  172. size_t len)
  173. {
  174. int action_code;
  175. int left = len - IEEE80211_HDRLEN - 4;
  176. u8 *pos = ((u8 *) mgmt) + IEEE80211_HDRLEN + 4;
  177. struct ieee802_11_elems elems;
  178. struct sta_info *sta = ap_get_sta(hapd, mgmt->sa);
  179. /* check that the request comes from a valid station */
  180. if (!sta ||
  181. (sta->flags & (WLAN_STA_ASSOC | WLAN_STA_WMM)) !=
  182. (WLAN_STA_ASSOC | WLAN_STA_WMM)) {
  183. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  184. HOSTAPD_LEVEL_DEBUG,
  185. "wmm action received is not from associated wmm"
  186. " station");
  187. /* TODO: respond with action frame refused status code */
  188. return;
  189. }
  190. /* extract the tspec info element */
  191. if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed) {
  192. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  193. HOSTAPD_LEVEL_DEBUG,
  194. "hostapd_wmm_action - could not parse wmm "
  195. "action");
  196. /* TODO: respond with action frame invalid parameters status
  197. * code */
  198. return;
  199. }
  200. if (!elems.wmm_tspec ||
  201. elems.wmm_tspec_len != (sizeof(struct wmm_tspec_element) - 2)) {
  202. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  203. HOSTAPD_LEVEL_DEBUG,
  204. "hostapd_wmm_action - missing or wrong length "
  205. "tspec");
  206. /* TODO: respond with action frame invalid parameters status
  207. * code */
  208. return;
  209. }
  210. /* TODO: check the request is for an AC with ACM set, if not, refuse
  211. * request */
  212. action_code = mgmt->u.action.u.wmm_action.action_code;
  213. switch (action_code) {
  214. case WMM_ACTION_CODE_ADDTS_REQ:
  215. wmm_addts_req(hapd, mgmt, (struct wmm_tspec_element *)
  216. (elems.wmm_tspec - 2), len);
  217. return;
  218. #if 0
  219. /* TODO: needed for client implementation */
  220. case WMM_ACTION_CODE_ADDTS_RESP:
  221. wmm_setup_request(hapd, mgmt, len);
  222. return;
  223. /* TODO: handle station teardown requests */
  224. case WMM_ACTION_CODE_DELTS:
  225. wmm_teardown(hapd, mgmt, len);
  226. return;
  227. #endif
  228. }
  229. hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
  230. HOSTAPD_LEVEL_DEBUG,
  231. "hostapd_wmm_action - unknown action code %d",
  232. action_code);
  233. }