wmm_ac.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Wi-Fi Multimedia Admission Control (WMM-AC)
  3. * Copyright(c) 2014, Intel Mobile Communication GmbH.
  4. * Copyright(c) 2014, Intel Corporation. All rights reserved.
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "includes.h"
  10. #include "utils/common.h"
  11. #include "utils/list.h"
  12. #include "common/ieee802_11_common.h"
  13. #include "wpa_supplicant_i.h"
  14. #include "bss.h"
  15. #include "driver_i.h"
  16. #include "wmm_ac.h"
  17. static const enum wmm_ac up_to_ac[8] = {
  18. WMM_AC_BK,
  19. WMM_AC_BE,
  20. WMM_AC_BE,
  21. WMM_AC_BK,
  22. WMM_AC_VI,
  23. WMM_AC_VI,
  24. WMM_AC_VO,
  25. WMM_AC_VO
  26. };
  27. static inline u8 wmm_ac_get_tsid(const struct wmm_tspec_element *tspec)
  28. {
  29. return (tspec->ts_info[0] >> 1) & 0x0f;
  30. }
  31. static int wmm_ac_send_addts_request(struct wpa_supplicant *wpa_s,
  32. const struct wmm_ac_addts_request *req)
  33. {
  34. struct wpabuf *buf;
  35. int ret;
  36. wpa_printf(MSG_DEBUG, "Sending ADDTS Request to " MACSTR,
  37. MAC2STR(req->address));
  38. /* category + action code + dialog token + status + sizeof(tspec) */
  39. buf = wpabuf_alloc(4 + sizeof(req->tspec));
  40. if (!buf) {
  41. wpa_printf(MSG_ERROR, "WMM AC: Allocation error");
  42. return -1;
  43. }
  44. wpabuf_put_u8(buf, WLAN_ACTION_WMM);
  45. wpabuf_put_u8(buf, WMM_ACTION_CODE_ADDTS_REQ);
  46. wpabuf_put_u8(buf, req->dialog_token);
  47. wpabuf_put_u8(buf, 0); /* status code */
  48. wpabuf_put_data(buf, &req->tspec, sizeof(req->tspec));
  49. ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, req->address,
  50. wpa_s->own_addr, wpa_s->bssid,
  51. wpabuf_head(buf), wpabuf_len(buf), 0);
  52. if (ret) {
  53. wpa_printf(MSG_WARNING,
  54. "WMM AC: Failed to send ADDTS Request");
  55. }
  56. wpabuf_free(buf);
  57. return ret;
  58. }
  59. static int wmm_ac_send_delts(struct wpa_supplicant *wpa_s,
  60. const struct wmm_tspec_element *tspec,
  61. const u8 *address)
  62. {
  63. struct wpabuf *buf;
  64. int ret;
  65. /* category + action code + dialog token + status + sizeof(tspec) */
  66. buf = wpabuf_alloc(4 + sizeof(*tspec));
  67. if (!buf)
  68. return -1;
  69. wpa_printf(MSG_DEBUG, "Sending DELTS to " MACSTR, MAC2STR(address));
  70. /* category + action code + dialog token + status + sizeof(tspec) */
  71. wpabuf_put_u8(buf, WLAN_ACTION_WMM);
  72. wpabuf_put_u8(buf, WMM_ACTION_CODE_DELTS);
  73. wpabuf_put_u8(buf, 0); /* Dialog Token (not used) */
  74. wpabuf_put_u8(buf, 0); /* Status Code (not used) */
  75. wpabuf_put_data(buf, tspec, sizeof(*tspec));
  76. ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, address,
  77. wpa_s->own_addr, wpa_s->bssid,
  78. wpabuf_head(buf), wpabuf_len(buf), 0);
  79. if (ret)
  80. wpa_printf(MSG_WARNING, "Failed to send DELTS frame");
  81. wpabuf_free(buf);
  82. return ret;
  83. }
  84. /* return the AC using the given TSPEC tid */
  85. static int wmm_ac_find_tsid(struct wpa_supplicant *wpa_s, u8 tsid,
  86. enum ts_dir_idx *dir)
  87. {
  88. int ac;
  89. enum ts_dir_idx idx;
  90. for (ac = 0; ac < WMM_AC_NUM; ac++) {
  91. for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
  92. if (wpa_s->tspecs[ac][idx] &&
  93. wmm_ac_get_tsid(wpa_s->tspecs[ac][idx]) == tsid) {
  94. if (dir)
  95. *dir = idx;
  96. return ac;
  97. }
  98. }
  99. }
  100. return -1;
  101. }
  102. static struct wmm_ac_addts_request *
  103. wmm_ac_build_addts_req(struct wpa_supplicant *wpa_s,
  104. const struct wmm_ac_ts_setup_params *params,
  105. const u8 *address)
  106. {
  107. struct wmm_ac_addts_request *addts_req;
  108. struct wmm_tspec_element *tspec;
  109. u8 ac = up_to_ac[params->user_priority];
  110. u8 uapsd = wpa_s->wmm_ac_assoc_info->ac_params[ac].uapsd;
  111. addts_req = os_zalloc(sizeof(*addts_req));
  112. if (!addts_req)
  113. return NULL;
  114. tspec = &addts_req->tspec;
  115. os_memcpy(addts_req->address, address, ETH_ALEN);
  116. /* The dialog token cannot be zero */
  117. if (++wpa_s->wmm_ac_last_dialog_token == 0)
  118. wpa_s->wmm_ac_last_dialog_token++;
  119. addts_req->dialog_token = wpa_s->wmm_ac_last_dialog_token;
  120. tspec->eid = WLAN_EID_VENDOR_SPECIFIC;
  121. tspec->length = sizeof(*tspec) - 2; /* reduce eid and length */
  122. tspec->oui[0] = 0x00;
  123. tspec->oui[1] = 0x50;
  124. tspec->oui[2] = 0xf2;
  125. tspec->oui_type = WMM_OUI_TYPE;
  126. tspec->oui_subtype = WMM_OUI_SUBTYPE_TSPEC_ELEMENT;
  127. tspec->version = WMM_VERSION;
  128. tspec->ts_info[0] = params->tsid << 1;
  129. tspec->ts_info[0] |= params->direction << 5;
  130. tspec->ts_info[0] |= WMM_AC_ACCESS_POLICY_EDCA << 7;
  131. tspec->ts_info[1] = uapsd << 2;
  132. tspec->ts_info[1] |= params->user_priority << 3;
  133. tspec->ts_info[2] = 0;
  134. tspec->nominal_msdu_size = host_to_le16(params->nominal_msdu_size);
  135. if (params->fixed_nominal_msdu)
  136. tspec->nominal_msdu_size |=
  137. host_to_le16(WMM_AC_FIXED_MSDU_SIZE);
  138. tspec->mean_data_rate = host_to_le32(params->mean_data_rate);
  139. tspec->minimum_phy_rate = host_to_le32(params->minimum_phy_rate);
  140. tspec->surplus_bandwidth_allowance =
  141. host_to_le16(params->surplus_bandwidth_allowance);
  142. return addts_req;
  143. }
  144. static int param_in_range(const char *name, long value,
  145. long min_val, long max_val)
  146. {
  147. if (value < min_val || (max_val >= 0 && value > max_val)) {
  148. wpa_printf(MSG_DEBUG,
  149. "WMM AC: param %s (%ld) is out of range (%ld-%ld)",
  150. name, value, min_val, max_val);
  151. return 0;
  152. }
  153. return 1;
  154. }
  155. static int wmm_ac_should_replace_ts(struct wpa_supplicant *wpa_s,
  156. u8 tsid, u8 ac, u8 dir)
  157. {
  158. enum ts_dir_idx idx;
  159. int cur_ac, existing_ts = 0, replace_ts = 0;
  160. cur_ac = wmm_ac_find_tsid(wpa_s, tsid, &idx);
  161. if (cur_ac >= 0) {
  162. if (cur_ac != ac) {
  163. wpa_printf(MSG_DEBUG,
  164. "WMM AC: TSID %i already exists on different ac (%d)",
  165. tsid, cur_ac);
  166. return -1;
  167. }
  168. /* same tsid - this tspec will replace the current one */
  169. replace_ts |= BIT(idx);
  170. }
  171. for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
  172. if (wpa_s->tspecs[ac][idx])
  173. existing_ts |= BIT(idx);
  174. }
  175. switch (dir) {
  176. case WMM_AC_DIR_UPLINK:
  177. /* replace existing uplink/bidi tspecs */
  178. replace_ts |= existing_ts & (BIT(TS_DIR_IDX_UPLINK) |
  179. BIT(TS_DIR_IDX_BIDI));
  180. break;
  181. case WMM_AC_DIR_DOWNLINK:
  182. /* replace existing downlink/bidi tspecs */
  183. replace_ts |= existing_ts & (BIT(TS_DIR_IDX_DOWNLINK) |
  184. BIT(TS_DIR_IDX_BIDI));
  185. break;
  186. case WMM_AC_DIR_BIDIRECTIONAL:
  187. /* replace all existing tspecs */
  188. replace_ts |= existing_ts;
  189. break;
  190. default:
  191. return -1;
  192. }
  193. return replace_ts;
  194. }
  195. static int wmm_ac_ts_req_is_valid(struct wpa_supplicant *wpa_s,
  196. const struct wmm_ac_ts_setup_params *params)
  197. {
  198. enum wmm_ac req_ac;
  199. #define PARAM_IN_RANGE(field, min_value, max_value) \
  200. param_in_range(#field, params->field, min_value, max_value)
  201. if (!PARAM_IN_RANGE(tsid, 0, WMM_AC_MAX_TID) ||
  202. !PARAM_IN_RANGE(user_priority, 0, WMM_AC_MAX_USER_PRIORITY) ||
  203. !PARAM_IN_RANGE(nominal_msdu_size, 1, WMM_AC_MAX_NOMINAL_MSDU) ||
  204. !PARAM_IN_RANGE(mean_data_rate, 1, -1) ||
  205. !PARAM_IN_RANGE(minimum_phy_rate, 1, -1) ||
  206. !PARAM_IN_RANGE(surplus_bandwidth_allowance, WMM_AC_MIN_SBA_UNITY,
  207. -1))
  208. return 0;
  209. #undef PARAM_IN_RANGE
  210. if (!(params->direction == WMM_TSPEC_DIRECTION_UPLINK ||
  211. params->direction == WMM_TSPEC_DIRECTION_DOWNLINK ||
  212. params->direction == WMM_TSPEC_DIRECTION_BI_DIRECTIONAL)) {
  213. wpa_printf(MSG_DEBUG, "WMM AC: invalid TS direction: %d",
  214. params->direction);
  215. return 0;
  216. }
  217. req_ac = up_to_ac[params->user_priority];
  218. /* Requested accesss category must have acm */
  219. if (!wpa_s->wmm_ac_assoc_info->ac_params[req_ac].acm) {
  220. wpa_printf(MSG_DEBUG, "WMM AC: AC %d is not ACM", req_ac);
  221. return 0;
  222. }
  223. if (wmm_ac_should_replace_ts(wpa_s, params->tsid, req_ac,
  224. params->direction) < 0)
  225. return 0;
  226. return 1;
  227. }
  228. static struct wmm_ac_assoc_data *
  229. wmm_ac_process_param_elem(struct wpa_supplicant *wpa_s, const u8 *ies,
  230. size_t ies_len)
  231. {
  232. struct ieee802_11_elems elems;
  233. struct wmm_parameter_element *wmm_params;
  234. struct wmm_ac_assoc_data *assoc_data;
  235. int i;
  236. /* Parsing WMM Parameter Element */
  237. if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) != ParseOK) {
  238. wpa_printf(MSG_DEBUG, "WMM AC: could not parse assoc ies");
  239. return NULL;
  240. }
  241. if (!elems.wmm) {
  242. wpa_printf(MSG_DEBUG, "WMM AC: No WMM IE");
  243. return NULL;
  244. }
  245. if (elems.wmm_len != sizeof(*wmm_params)) {
  246. wpa_printf(MSG_WARNING, "WMM AC: Invalid WMM ie length");
  247. return NULL;
  248. }
  249. wmm_params = (struct wmm_parameter_element *)(elems.wmm);
  250. assoc_data = os_zalloc(sizeof(*assoc_data));
  251. if (!assoc_data)
  252. return NULL;
  253. for (i = 0; i < WMM_AC_NUM; i++)
  254. assoc_data->ac_params[i].acm =
  255. !!(wmm_params->ac[i].aci_aifsn & WMM_AC_ACM);
  256. wpa_printf(MSG_DEBUG,
  257. "WMM AC: AC mandatory: AC_BE=%u AC_BK=%u AC_VI=%u AC_VO=%u",
  258. assoc_data->ac_params[WMM_AC_BE].acm,
  259. assoc_data->ac_params[WMM_AC_BK].acm,
  260. assoc_data->ac_params[WMM_AC_VI].acm,
  261. assoc_data->ac_params[WMM_AC_VO].acm);
  262. return assoc_data;
  263. }
  264. static int wmm_ac_init(struct wpa_supplicant *wpa_s, const u8 *ies,
  265. size_t ies_len, const struct wmm_params *wmm_params)
  266. {
  267. struct wmm_ac_assoc_data *assoc_data;
  268. u8 ac;
  269. if (wpa_s->wmm_ac_assoc_info) {
  270. wpa_printf(MSG_ERROR, "WMM AC: Already initialized");
  271. return -1;
  272. }
  273. if (!ies) {
  274. wpa_printf(MSG_ERROR, "WMM AC: Missing IEs");
  275. return -1;
  276. }
  277. if (!(wmm_params->info_bitmap & WMM_PARAMS_UAPSD_QUEUES_INFO)) {
  278. wpa_printf(MSG_DEBUG, "WMM AC: Missing U-APSD configuration");
  279. return -1;
  280. }
  281. os_memset(wpa_s->tspecs, 0, sizeof(wpa_s->tspecs));
  282. wpa_s->wmm_ac_last_dialog_token = 0;
  283. assoc_data = wmm_ac_process_param_elem(wpa_s, ies, ies_len);
  284. if (!assoc_data)
  285. return -1;
  286. wpa_printf(MSG_DEBUG, "WMM AC: U-APSD queues=0x%x",
  287. wmm_params->uapsd_queues);
  288. for (ac = 0; ac < WMM_AC_NUM; ac++) {
  289. assoc_data->ac_params[ac].uapsd =
  290. !!(wmm_params->uapsd_queues & BIT(ac));
  291. }
  292. wpa_s->wmm_ac_assoc_info = assoc_data;
  293. return 0;
  294. }
  295. static void wmm_ac_del_ts(struct wpa_supplicant *wpa_s, u8 ac, int dir_bitmap)
  296. {
  297. enum ts_dir_idx idx;
  298. for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
  299. if (!(dir_bitmap & BIT(idx)))
  300. continue;
  301. if (!wpa_s->tspecs[ac][idx])
  302. continue;
  303. os_free(wpa_s->tspecs[ac][idx]);
  304. wpa_s->tspecs[ac][idx] = NULL;
  305. }
  306. }
  307. static void wmm_ac_deinit(struct wpa_supplicant *wpa_s)
  308. {
  309. int i;
  310. for (i = 0; i < WMM_AC_NUM; i++)
  311. wmm_ac_del_ts(wpa_s, i, TS_DIR_IDX_ALL);
  312. os_free(wpa_s->wmm_ac_assoc_info);
  313. wpa_s->wmm_ac_assoc_info = NULL;
  314. }
  315. void wmm_ac_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *ies,
  316. size_t ies_len, const struct wmm_params *wmm_params)
  317. {
  318. if (wmm_ac_init(wpa_s, ies, ies_len, wmm_params))
  319. return;
  320. wpa_printf(MSG_DEBUG,
  321. "WMM AC: Valid WMM association, WMM AC is enabled");
  322. }
  323. void wmm_ac_notify_disassoc(struct wpa_supplicant *wpa_s)
  324. {
  325. if (!wpa_s->wmm_ac_assoc_info)
  326. return;
  327. wmm_ac_deinit(wpa_s);
  328. wpa_printf(MSG_DEBUG, "WMM AC: WMM AC is disabled");
  329. }
  330. int wpas_wmm_ac_delts(struct wpa_supplicant *wpa_s, u8 tsid)
  331. {
  332. struct wmm_tspec_element *tspec;
  333. int ac;
  334. enum ts_dir_idx dir;
  335. if (!wpa_s->wmm_ac_assoc_info) {
  336. wpa_printf(MSG_DEBUG,
  337. "WMM AC: Failed to delete TS, WMM AC is disabled");
  338. return -1;
  339. }
  340. ac = wmm_ac_find_tsid(wpa_s, tsid, &dir);
  341. if (ac < 0) {
  342. wpa_printf(MSG_DEBUG, "WMM AC: TS does not exist");
  343. return -1;
  344. }
  345. tspec = wpa_s->tspecs[ac][dir];
  346. wmm_ac_send_delts(wpa_s, tspec, wpa_s->bssid);
  347. os_free(tspec);
  348. wpa_s->tspecs[ac][dir] = NULL;
  349. wpa_printf(MSG_DEBUG, "WMM AC: TS was deleted (TSID=%u addr=" MACSTR
  350. ")", tsid, MAC2STR(wpa_s->bssid));
  351. return 0;
  352. }
  353. int wpas_wmm_ac_addts(struct wpa_supplicant *wpa_s,
  354. struct wmm_ac_ts_setup_params *params)
  355. {
  356. struct wmm_ac_addts_request *addts_req;
  357. if (!wpa_s->wmm_ac_assoc_info) {
  358. wpa_printf(MSG_DEBUG,
  359. "WMM AC: Cannot add TS - missing assoc data");
  360. return -1;
  361. }
  362. /*
  363. * we can setup downlink TS even without driver support.
  364. * however, we need driver support for the other directions.
  365. */
  366. if (params->direction != WMM_AC_DIR_DOWNLINK &&
  367. !wpa_s->wmm_ac_supported) {
  368. wpa_printf(MSG_DEBUG,
  369. "Cannot set uplink/bidi TS without driver support");
  370. return -1;
  371. }
  372. if (!wmm_ac_ts_req_is_valid(wpa_s, params))
  373. return -1;
  374. wpa_printf(MSG_DEBUG, "WMM AC: TS setup request (addr=" MACSTR
  375. " tsid=%u user priority=%u direction=%d)",
  376. MAC2STR(wpa_s->bssid), params->tsid,
  377. params->user_priority, params->direction);
  378. addts_req = wmm_ac_build_addts_req(wpa_s, params, wpa_s->bssid);
  379. if (!addts_req)
  380. return -1;
  381. if (wmm_ac_send_addts_request(wpa_s, addts_req))
  382. goto err;
  383. /* TODO: wait for ADDTS response, etc. */
  384. os_free(addts_req);
  385. return 0;
  386. err:
  387. os_free(addts_req);
  388. return -1;
  389. }