wme.c 8.0 KB

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