wme.c 8.0 KB

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