wmm_ac.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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 "utils/eloop.h"
  13. #include "common/ieee802_11_common.h"
  14. #include "wpa_supplicant_i.h"
  15. #include "bss.h"
  16. #include "driver_i.h"
  17. #include "wmm_ac.h"
  18. static void wmm_ac_addts_req_timeout(void *eloop_ctx, void *timeout_ctx);
  19. static const enum wmm_ac up_to_ac[8] = {
  20. WMM_AC_BK,
  21. WMM_AC_BE,
  22. WMM_AC_BE,
  23. WMM_AC_BK,
  24. WMM_AC_VI,
  25. WMM_AC_VI,
  26. WMM_AC_VO,
  27. WMM_AC_VO
  28. };
  29. static inline u8 wmm_ac_get_tsid(const struct wmm_tspec_element *tspec)
  30. {
  31. return (tspec->ts_info[0] >> 1) & 0x0f;
  32. }
  33. static u8 wmm_ac_get_direction(const struct wmm_tspec_element *tspec)
  34. {
  35. return (tspec->ts_info[0] >> 5) & 0x03;
  36. }
  37. static u8 wmm_ac_get_user_priority(const struct wmm_tspec_element *tspec)
  38. {
  39. return (tspec->ts_info[1] >> 3) & 0x07;
  40. }
  41. static u8 wmm_ac_direction_to_idx(u8 direction)
  42. {
  43. switch (direction) {
  44. case WMM_AC_DIR_UPLINK:
  45. return TS_DIR_IDX_UPLINK;
  46. case WMM_AC_DIR_DOWNLINK:
  47. return TS_DIR_IDX_DOWNLINK;
  48. case WMM_AC_DIR_BIDIRECTIONAL:
  49. return TS_DIR_IDX_BIDI;
  50. default:
  51. wpa_printf(MSG_ERROR, "Invalid direction: %d", direction);
  52. return WMM_AC_DIR_UPLINK;
  53. }
  54. }
  55. static int wmm_ac_add_ts(struct wpa_supplicant *wpa_s, const u8 *addr,
  56. const struct wmm_tspec_element *tspec)
  57. {
  58. struct wmm_tspec_element *_tspec;
  59. int ret;
  60. u16 admitted_time = le_to_host16(tspec->medium_time);
  61. u8 up = wmm_ac_get_user_priority(tspec);
  62. u8 ac = up_to_ac[up];
  63. u8 dir = wmm_ac_get_direction(tspec);
  64. u8 tsid = wmm_ac_get_tsid(tspec);
  65. enum ts_dir_idx idx = wmm_ac_direction_to_idx(dir);
  66. /* should have been verified before, but double-check here */
  67. if (wpa_s->tspecs[ac][idx]) {
  68. wpa_printf(MSG_ERROR,
  69. "WMM AC: tspec (ac=%d, dir=%d) already exists!",
  70. ac, dir);
  71. return -1;
  72. }
  73. /* copy tspec */
  74. _tspec = os_malloc(sizeof(*_tspec));
  75. if (!_tspec)
  76. return -1;
  77. /* store the admitted TSPEC */
  78. os_memcpy(_tspec, tspec, sizeof(*_tspec));
  79. if (dir != WMM_AC_DIR_DOWNLINK) {
  80. ret = wpa_drv_add_ts(wpa_s, tsid, addr, up, admitted_time);
  81. wpa_printf(MSG_DEBUG,
  82. "WMM AC: Add TS: addr=" MACSTR
  83. " TSID=%u admitted time=%u, ret=%d",
  84. MAC2STR(addr), tsid, admitted_time, ret);
  85. if (ret < 0) {
  86. os_free(_tspec);
  87. return -1;
  88. }
  89. }
  90. wpa_s->tspecs[ac][idx] = _tspec;
  91. wpa_printf(MSG_DEBUG, "Traffic stream was created successfully");
  92. wpa_msg(wpa_s, MSG_INFO, WMM_AC_EVENT_TSPEC_ADDED
  93. "tsid=%d addr=" MACSTR " admitted_time=%d",
  94. tsid, MAC2STR(addr), admitted_time);
  95. return 0;
  96. }
  97. static void wmm_ac_del_ts_idx(struct wpa_supplicant *wpa_s, u8 ac,
  98. enum ts_dir_idx dir)
  99. {
  100. struct wmm_tspec_element *tspec = wpa_s->tspecs[ac][dir];
  101. u8 tsid;
  102. if (!tspec)
  103. return;
  104. tsid = wmm_ac_get_tsid(tspec);
  105. wpa_printf(MSG_DEBUG, "WMM AC: Del TS ac=%d tsid=%d", ac, tsid);
  106. /* update the driver in case of uplink/bidi */
  107. if (wmm_ac_get_direction(tspec) != WMM_AC_DIR_DOWNLINK)
  108. wpa_drv_del_ts(wpa_s, tsid, wpa_s->bssid);
  109. wpa_msg(wpa_s, MSG_INFO, WMM_AC_EVENT_TSPEC_REMOVED
  110. "tsid=%d addr=" MACSTR, tsid, MAC2STR(wpa_s->bssid));
  111. os_free(wpa_s->tspecs[ac][dir]);
  112. wpa_s->tspecs[ac][dir] = NULL;
  113. }
  114. static void wmm_ac_del_req(struct wpa_supplicant *wpa_s, int failed)
  115. {
  116. struct wmm_ac_addts_request *req = wpa_s->addts_request;
  117. if (!req)
  118. return;
  119. if (failed)
  120. wpa_msg(wpa_s, MSG_INFO, WMM_AC_EVENT_TSPEC_REQ_FAILED
  121. "tsid=%u", wmm_ac_get_tsid(&req->tspec));
  122. eloop_cancel_timeout(wmm_ac_addts_req_timeout, wpa_s, req);
  123. wpa_s->addts_request = NULL;
  124. os_free(req);
  125. }
  126. static void wmm_ac_addts_req_timeout(void *eloop_ctx, void *timeout_ctx)
  127. {
  128. struct wpa_supplicant *wpa_s = eloop_ctx;
  129. struct wmm_ac_addts_request *addts_req = timeout_ctx;
  130. wpa_printf(MSG_DEBUG,
  131. "Timeout getting ADDTS response (tsid=%d up=%d)",
  132. wmm_ac_get_tsid(&addts_req->tspec),
  133. wmm_ac_get_user_priority(&addts_req->tspec));
  134. wmm_ac_del_req(wpa_s, 1);
  135. }
  136. static int wmm_ac_send_addts_request(struct wpa_supplicant *wpa_s,
  137. const struct wmm_ac_addts_request *req)
  138. {
  139. struct wpabuf *buf;
  140. int ret;
  141. wpa_printf(MSG_DEBUG, "Sending ADDTS Request to " MACSTR,
  142. MAC2STR(req->address));
  143. /* category + action code + dialog token + status + sizeof(tspec) */
  144. buf = wpabuf_alloc(4 + sizeof(req->tspec));
  145. if (!buf) {
  146. wpa_printf(MSG_ERROR, "WMM AC: Allocation error");
  147. return -1;
  148. }
  149. wpabuf_put_u8(buf, WLAN_ACTION_WMM);
  150. wpabuf_put_u8(buf, WMM_ACTION_CODE_ADDTS_REQ);
  151. wpabuf_put_u8(buf, req->dialog_token);
  152. wpabuf_put_u8(buf, 0); /* status code */
  153. wpabuf_put_data(buf, &req->tspec, sizeof(req->tspec));
  154. ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, req->address,
  155. wpa_s->own_addr, wpa_s->bssid,
  156. wpabuf_head(buf), wpabuf_len(buf), 0);
  157. if (ret) {
  158. wpa_printf(MSG_WARNING,
  159. "WMM AC: Failed to send ADDTS Request");
  160. }
  161. wpabuf_free(buf);
  162. return ret;
  163. }
  164. static int wmm_ac_send_delts(struct wpa_supplicant *wpa_s,
  165. const struct wmm_tspec_element *tspec,
  166. const u8 *address)
  167. {
  168. struct wpabuf *buf;
  169. int ret;
  170. /* category + action code + dialog token + status + sizeof(tspec) */
  171. buf = wpabuf_alloc(4 + sizeof(*tspec));
  172. if (!buf)
  173. return -1;
  174. wpa_printf(MSG_DEBUG, "Sending DELTS to " MACSTR, MAC2STR(address));
  175. /* category + action code + dialog token + status + sizeof(tspec) */
  176. wpabuf_put_u8(buf, WLAN_ACTION_WMM);
  177. wpabuf_put_u8(buf, WMM_ACTION_CODE_DELTS);
  178. wpabuf_put_u8(buf, 0); /* Dialog Token (not used) */
  179. wpabuf_put_u8(buf, 0); /* Status Code (not used) */
  180. wpabuf_put_data(buf, tspec, sizeof(*tspec));
  181. ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, address,
  182. wpa_s->own_addr, wpa_s->bssid,
  183. wpabuf_head(buf), wpabuf_len(buf), 0);
  184. if (ret)
  185. wpa_printf(MSG_WARNING, "Failed to send DELTS frame");
  186. wpabuf_free(buf);
  187. return ret;
  188. }
  189. /* return the AC using the given TSPEC tid */
  190. static int wmm_ac_find_tsid(struct wpa_supplicant *wpa_s, u8 tsid,
  191. enum ts_dir_idx *dir)
  192. {
  193. int ac;
  194. enum ts_dir_idx idx;
  195. for (ac = 0; ac < WMM_AC_NUM; ac++) {
  196. for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
  197. if (wpa_s->tspecs[ac][idx] &&
  198. wmm_ac_get_tsid(wpa_s->tspecs[ac][idx]) == tsid) {
  199. if (dir)
  200. *dir = idx;
  201. return ac;
  202. }
  203. }
  204. }
  205. return -1;
  206. }
  207. static struct wmm_ac_addts_request *
  208. wmm_ac_build_addts_req(struct wpa_supplicant *wpa_s,
  209. const struct wmm_ac_ts_setup_params *params,
  210. const u8 *address)
  211. {
  212. struct wmm_ac_addts_request *addts_req;
  213. struct wmm_tspec_element *tspec;
  214. u8 ac = up_to_ac[params->user_priority];
  215. u8 uapsd = wpa_s->wmm_ac_assoc_info->ac_params[ac].uapsd;
  216. addts_req = os_zalloc(sizeof(*addts_req));
  217. if (!addts_req)
  218. return NULL;
  219. tspec = &addts_req->tspec;
  220. os_memcpy(addts_req->address, address, ETH_ALEN);
  221. /* The dialog token cannot be zero */
  222. if (++wpa_s->wmm_ac_last_dialog_token == 0)
  223. wpa_s->wmm_ac_last_dialog_token++;
  224. addts_req->dialog_token = wpa_s->wmm_ac_last_dialog_token;
  225. tspec->eid = WLAN_EID_VENDOR_SPECIFIC;
  226. tspec->length = sizeof(*tspec) - 2; /* reduce eid and length */
  227. tspec->oui[0] = 0x00;
  228. tspec->oui[1] = 0x50;
  229. tspec->oui[2] = 0xf2;
  230. tspec->oui_type = WMM_OUI_TYPE;
  231. tspec->oui_subtype = WMM_OUI_SUBTYPE_TSPEC_ELEMENT;
  232. tspec->version = WMM_VERSION;
  233. tspec->ts_info[0] = params->tsid << 1;
  234. tspec->ts_info[0] |= params->direction << 5;
  235. tspec->ts_info[0] |= WMM_AC_ACCESS_POLICY_EDCA << 7;
  236. tspec->ts_info[1] = uapsd << 2;
  237. tspec->ts_info[1] |= params->user_priority << 3;
  238. tspec->ts_info[2] = 0;
  239. tspec->nominal_msdu_size = host_to_le16(params->nominal_msdu_size);
  240. if (params->fixed_nominal_msdu)
  241. tspec->nominal_msdu_size |=
  242. host_to_le16(WMM_AC_FIXED_MSDU_SIZE);
  243. tspec->mean_data_rate = host_to_le32(params->mean_data_rate);
  244. tspec->minimum_phy_rate = host_to_le32(params->minimum_phy_rate);
  245. tspec->surplus_bandwidth_allowance =
  246. host_to_le16(params->surplus_bandwidth_allowance);
  247. return addts_req;
  248. }
  249. static int param_in_range(const char *name, long value,
  250. long min_val, long max_val)
  251. {
  252. if (value < min_val || (max_val >= 0 && value > max_val)) {
  253. wpa_printf(MSG_DEBUG,
  254. "WMM AC: param %s (%ld) is out of range (%ld-%ld)",
  255. name, value, min_val, max_val);
  256. return 0;
  257. }
  258. return 1;
  259. }
  260. static int wmm_ac_should_replace_ts(struct wpa_supplicant *wpa_s,
  261. u8 tsid, u8 ac, u8 dir)
  262. {
  263. enum ts_dir_idx idx;
  264. int cur_ac, existing_ts = 0, replace_ts = 0;
  265. cur_ac = wmm_ac_find_tsid(wpa_s, tsid, &idx);
  266. if (cur_ac >= 0) {
  267. if (cur_ac != ac) {
  268. wpa_printf(MSG_DEBUG,
  269. "WMM AC: TSID %i already exists on different ac (%d)",
  270. tsid, cur_ac);
  271. return -1;
  272. }
  273. /* same tsid - this tspec will replace the current one */
  274. replace_ts |= BIT(idx);
  275. }
  276. for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
  277. if (wpa_s->tspecs[ac][idx])
  278. existing_ts |= BIT(idx);
  279. }
  280. switch (dir) {
  281. case WMM_AC_DIR_UPLINK:
  282. /* replace existing uplink/bidi tspecs */
  283. replace_ts |= existing_ts & (BIT(TS_DIR_IDX_UPLINK) |
  284. BIT(TS_DIR_IDX_BIDI));
  285. break;
  286. case WMM_AC_DIR_DOWNLINK:
  287. /* replace existing downlink/bidi tspecs */
  288. replace_ts |= existing_ts & (BIT(TS_DIR_IDX_DOWNLINK) |
  289. BIT(TS_DIR_IDX_BIDI));
  290. break;
  291. case WMM_AC_DIR_BIDIRECTIONAL:
  292. /* replace all existing tspecs */
  293. replace_ts |= existing_ts;
  294. break;
  295. default:
  296. return -1;
  297. }
  298. return replace_ts;
  299. }
  300. static int wmm_ac_ts_req_is_valid(struct wpa_supplicant *wpa_s,
  301. const struct wmm_ac_ts_setup_params *params)
  302. {
  303. enum wmm_ac req_ac;
  304. #define PARAM_IN_RANGE(field, min_value, max_value) \
  305. param_in_range(#field, params->field, min_value, max_value)
  306. if (!PARAM_IN_RANGE(tsid, 0, WMM_AC_MAX_TID) ||
  307. !PARAM_IN_RANGE(user_priority, 0, WMM_AC_MAX_USER_PRIORITY) ||
  308. !PARAM_IN_RANGE(nominal_msdu_size, 1, WMM_AC_MAX_NOMINAL_MSDU) ||
  309. !PARAM_IN_RANGE(mean_data_rate, 1, -1) ||
  310. !PARAM_IN_RANGE(minimum_phy_rate, 1, -1) ||
  311. !PARAM_IN_RANGE(surplus_bandwidth_allowance, WMM_AC_MIN_SBA_UNITY,
  312. -1))
  313. return 0;
  314. #undef PARAM_IN_RANGE
  315. if (!(params->direction == WMM_TSPEC_DIRECTION_UPLINK ||
  316. params->direction == WMM_TSPEC_DIRECTION_DOWNLINK ||
  317. params->direction == WMM_TSPEC_DIRECTION_BI_DIRECTIONAL)) {
  318. wpa_printf(MSG_DEBUG, "WMM AC: invalid TS direction: %d",
  319. params->direction);
  320. return 0;
  321. }
  322. req_ac = up_to_ac[params->user_priority];
  323. /* Requested accesss category must have acm */
  324. if (!wpa_s->wmm_ac_assoc_info->ac_params[req_ac].acm) {
  325. wpa_printf(MSG_DEBUG, "WMM AC: AC %d is not ACM", req_ac);
  326. return 0;
  327. }
  328. if (wmm_ac_should_replace_ts(wpa_s, params->tsid, req_ac,
  329. params->direction) < 0)
  330. return 0;
  331. return 1;
  332. }
  333. static struct wmm_ac_assoc_data *
  334. wmm_ac_process_param_elem(struct wpa_supplicant *wpa_s, const u8 *ies,
  335. size_t ies_len)
  336. {
  337. struct ieee802_11_elems elems;
  338. struct wmm_parameter_element *wmm_params;
  339. struct wmm_ac_assoc_data *assoc_data;
  340. int i;
  341. /* Parsing WMM Parameter Element */
  342. if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) != ParseOK) {
  343. wpa_printf(MSG_DEBUG, "WMM AC: could not parse assoc ies");
  344. return NULL;
  345. }
  346. if (!elems.wmm) {
  347. wpa_printf(MSG_DEBUG, "WMM AC: No WMM IE");
  348. return NULL;
  349. }
  350. if (elems.wmm_len != sizeof(*wmm_params)) {
  351. wpa_printf(MSG_DEBUG, "WMM AC: Invalid WMM ie length");
  352. return NULL;
  353. }
  354. wmm_params = (struct wmm_parameter_element *)(elems.wmm);
  355. assoc_data = os_zalloc(sizeof(*assoc_data));
  356. if (!assoc_data)
  357. return NULL;
  358. for (i = 0; i < WMM_AC_NUM; i++)
  359. assoc_data->ac_params[i].acm =
  360. !!(wmm_params->ac[i].aci_aifsn & WMM_AC_ACM);
  361. wpa_printf(MSG_DEBUG,
  362. "WMM AC: AC mandatory: AC_BE=%u AC_BK=%u AC_VI=%u AC_VO=%u",
  363. assoc_data->ac_params[WMM_AC_BE].acm,
  364. assoc_data->ac_params[WMM_AC_BK].acm,
  365. assoc_data->ac_params[WMM_AC_VI].acm,
  366. assoc_data->ac_params[WMM_AC_VO].acm);
  367. return assoc_data;
  368. }
  369. static int wmm_ac_init(struct wpa_supplicant *wpa_s, const u8 *ies,
  370. size_t ies_len, const struct wmm_params *wmm_params)
  371. {
  372. struct wmm_ac_assoc_data *assoc_data;
  373. u8 ac;
  374. if (wpa_s->wmm_ac_assoc_info) {
  375. wpa_printf(MSG_ERROR, "WMM AC: Already initialized");
  376. return -1;
  377. }
  378. if (!ies) {
  379. wpa_printf(MSG_ERROR, "WMM AC: Missing IEs");
  380. return -1;
  381. }
  382. if (!(wmm_params->info_bitmap & WMM_PARAMS_UAPSD_QUEUES_INFO)) {
  383. wpa_printf(MSG_DEBUG, "WMM AC: Missing U-APSD configuration");
  384. return -1;
  385. }
  386. os_memset(wpa_s->tspecs, 0, sizeof(wpa_s->tspecs));
  387. wpa_s->wmm_ac_last_dialog_token = 0;
  388. wpa_s->addts_request = NULL;
  389. assoc_data = wmm_ac_process_param_elem(wpa_s, ies, ies_len);
  390. if (!assoc_data)
  391. return -1;
  392. wpa_printf(MSG_DEBUG, "WMM AC: U-APSD queues=0x%x",
  393. wmm_params->uapsd_queues);
  394. for (ac = 0; ac < WMM_AC_NUM; ac++) {
  395. assoc_data->ac_params[ac].uapsd =
  396. !!(wmm_params->uapsd_queues & BIT(ac));
  397. }
  398. wpa_s->wmm_ac_assoc_info = assoc_data;
  399. return 0;
  400. }
  401. static void wmm_ac_del_ts(struct wpa_supplicant *wpa_s, u8 ac, int dir_bitmap)
  402. {
  403. enum ts_dir_idx idx;
  404. for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
  405. if (!(dir_bitmap & BIT(idx)))
  406. continue;
  407. wmm_ac_del_ts_idx(wpa_s, ac, idx);
  408. }
  409. }
  410. static void wmm_ac_deinit(struct wpa_supplicant *wpa_s)
  411. {
  412. int i;
  413. for (i = 0; i < WMM_AC_NUM; i++)
  414. wmm_ac_del_ts(wpa_s, i, TS_DIR_IDX_ALL);
  415. /* delete pending add_ts requset */
  416. wmm_ac_del_req(wpa_s, 1);
  417. os_free(wpa_s->wmm_ac_assoc_info);
  418. wpa_s->wmm_ac_assoc_info = NULL;
  419. }
  420. void wmm_ac_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *ies,
  421. size_t ies_len, const struct wmm_params *wmm_params)
  422. {
  423. if (wmm_ac_init(wpa_s, ies, ies_len, wmm_params))
  424. return;
  425. wpa_printf(MSG_DEBUG,
  426. "WMM AC: Valid WMM association, WMM AC is enabled");
  427. }
  428. void wmm_ac_notify_disassoc(struct wpa_supplicant *wpa_s)
  429. {
  430. if (!wpa_s->wmm_ac_assoc_info)
  431. return;
  432. wmm_ac_deinit(wpa_s);
  433. wpa_printf(MSG_DEBUG, "WMM AC: WMM AC is disabled");
  434. }
  435. int wpas_wmm_ac_delts(struct wpa_supplicant *wpa_s, u8 tsid)
  436. {
  437. struct wmm_tspec_element tspec;
  438. int ac;
  439. enum ts_dir_idx dir;
  440. if (!wpa_s->wmm_ac_assoc_info) {
  441. wpa_printf(MSG_DEBUG,
  442. "WMM AC: Failed to delete TS, WMM AC is disabled");
  443. return -1;
  444. }
  445. ac = wmm_ac_find_tsid(wpa_s, tsid, &dir);
  446. if (ac < 0) {
  447. wpa_printf(MSG_DEBUG, "WMM AC: TS does not exist");
  448. return -1;
  449. }
  450. tspec = *wpa_s->tspecs[ac][dir];
  451. wmm_ac_del_ts_idx(wpa_s, ac, dir);
  452. wmm_ac_send_delts(wpa_s, &tspec, wpa_s->bssid);
  453. return 0;
  454. }
  455. int wpas_wmm_ac_addts(struct wpa_supplicant *wpa_s,
  456. struct wmm_ac_ts_setup_params *params)
  457. {
  458. struct wmm_ac_addts_request *addts_req;
  459. if (!wpa_s->wmm_ac_assoc_info) {
  460. wpa_printf(MSG_DEBUG,
  461. "WMM AC: Cannot add TS - missing assoc data");
  462. return -1;
  463. }
  464. if (wpa_s->addts_request) {
  465. wpa_printf(MSG_DEBUG,
  466. "WMM AC: can't add TS - ADDTS request is already pending");
  467. return -1;
  468. }
  469. /*
  470. * we can setup downlink TS even without driver support.
  471. * however, we need driver support for the other directions.
  472. */
  473. if (params->direction != WMM_AC_DIR_DOWNLINK &&
  474. !wpa_s->wmm_ac_supported) {
  475. wpa_printf(MSG_DEBUG,
  476. "Cannot set uplink/bidi TS without driver support");
  477. return -1;
  478. }
  479. if (!wmm_ac_ts_req_is_valid(wpa_s, params))
  480. return -1;
  481. wpa_printf(MSG_DEBUG, "WMM AC: TS setup request (addr=" MACSTR
  482. " tsid=%u user priority=%u direction=%d)",
  483. MAC2STR(wpa_s->bssid), params->tsid,
  484. params->user_priority, params->direction);
  485. addts_req = wmm_ac_build_addts_req(wpa_s, params, wpa_s->bssid);
  486. if (!addts_req)
  487. return -1;
  488. if (wmm_ac_send_addts_request(wpa_s, addts_req))
  489. goto err;
  490. /* save as pending and set ADDTS resp timeout to 1 second */
  491. wpa_s->addts_request = addts_req;
  492. eloop_register_timeout(1, 0, wmm_ac_addts_req_timeout,
  493. wpa_s, addts_req);
  494. return 0;
  495. err:
  496. os_free(addts_req);
  497. return -1;
  498. }
  499. static void wmm_ac_handle_delts(struct wpa_supplicant *wpa_s, const u8 *sa,
  500. const struct wmm_tspec_element *tspec)
  501. {
  502. int ac;
  503. u8 tsid;
  504. enum ts_dir_idx idx;
  505. tsid = wmm_ac_get_tsid(tspec);
  506. wpa_printf(MSG_DEBUG,
  507. "WMM AC: DELTS frame has been received TSID=%u addr="
  508. MACSTR, tsid, MAC2STR(sa));
  509. ac = wmm_ac_find_tsid(wpa_s, tsid, &idx);
  510. if (ac < 0) {
  511. wpa_printf(MSG_DEBUG,
  512. "WMM AC: Ignoring DELTS frame - TSID does not exist");
  513. return;
  514. }
  515. wmm_ac_del_ts_idx(wpa_s, ac, idx);
  516. wpa_printf(MSG_DEBUG,
  517. "TS was deleted successfully (tsid=%u address=" MACSTR ")",
  518. tsid, MAC2STR(sa));
  519. }
  520. static void wmm_ac_handle_addts_resp(struct wpa_supplicant *wpa_s, const u8 *sa,
  521. const u8 resp_dialog_token, const u8 status_code,
  522. const struct wmm_tspec_element *tspec)
  523. {
  524. struct wmm_ac_addts_request *req = wpa_s->addts_request;
  525. u8 ac, tsid, up, dir;
  526. int replace_tspecs;
  527. tsid = wmm_ac_get_tsid(tspec);
  528. dir = wmm_ac_get_direction(tspec);
  529. up = wmm_ac_get_user_priority(tspec);
  530. ac = up_to_ac[up];
  531. /* make sure we have a matching addts request */
  532. if (!req || req->dialog_token != resp_dialog_token) {
  533. wpa_printf(MSG_DEBUG,
  534. "WMM AC: no req with dialog=%u, ignoring frame",
  535. resp_dialog_token);
  536. return;
  537. }
  538. /* make sure the params are the same */
  539. if (os_memcmp(req->address, sa, ETH_ALEN) != 0 ||
  540. tsid != wmm_ac_get_tsid(&req->tspec) ||
  541. up != wmm_ac_get_user_priority(&req->tspec) ||
  542. dir != wmm_ac_get_direction(&req->tspec)) {
  543. wpa_printf(MSG_DEBUG,
  544. "WMM AC: ADDTS params do not match, ignoring frame");
  545. return;
  546. }
  547. /* delete pending request */
  548. wmm_ac_del_req(wpa_s, 0);
  549. wpa_printf(MSG_DEBUG,
  550. "ADDTS response status=%d tsid=%u up=%u direction=%u",
  551. status_code, tsid, up, dir);
  552. if (status_code != WMM_ADDTS_STATUS_ADMISSION_ACCEPTED) {
  553. wpa_printf(MSG_INFO, "WMM AC: ADDTS request was rejected");
  554. goto err_msg;
  555. }
  556. replace_tspecs = wmm_ac_should_replace_ts(wpa_s, tsid, ac, dir);
  557. if (replace_tspecs < 0)
  558. goto err_delts;
  559. wpa_printf(MSG_DEBUG, "ts idx replace bitmap: 0x%x", replace_tspecs);
  560. /* when replacing tspecs - delete first */
  561. wmm_ac_del_ts(wpa_s, ac, replace_tspecs);
  562. /* Creating a new traffic stream */
  563. wpa_printf(MSG_DEBUG,
  564. "WMM AC: adding a new TS with TSID=%u address="MACSTR
  565. " medium time=%u access category=%d dir=%d ",
  566. tsid, MAC2STR(sa),
  567. le_to_host16(tspec->medium_time), ac, dir);
  568. if (wmm_ac_add_ts(wpa_s, sa, tspec))
  569. goto err_delts;
  570. return;
  571. err_delts:
  572. /* ask the ap to delete the tspec */
  573. wmm_ac_send_delts(wpa_s, tspec, sa);
  574. err_msg:
  575. wpa_msg(wpa_s, MSG_INFO, WMM_AC_EVENT_TSPEC_REQ_FAILED "tsid=%u",
  576. tsid);
  577. }
  578. void wmm_ac_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  579. const u8 *sa, const u8 *data, size_t len)
  580. {
  581. u8 action;
  582. u8 dialog_token;
  583. u8 status_code;
  584. struct ieee802_11_elems elems;
  585. struct wmm_tspec_element *tspec;
  586. if (wpa_s->wmm_ac_assoc_info == NULL) {
  587. wpa_printf(MSG_DEBUG,
  588. "WMM AC: WMM AC is disabled, ignoring action frame");
  589. return;
  590. }
  591. action = data[0];
  592. if (action != WMM_ACTION_CODE_ADDTS_RESP &&
  593. action != WMM_ACTION_CODE_DELTS) {
  594. wpa_printf(MSG_DEBUG,
  595. "WMM AC: Unknown action (%d), ignoring action frame",
  596. action);
  597. return;
  598. }
  599. /* WMM AC action frame */
  600. if (os_memcmp(da, wpa_s->own_addr, ETH_ALEN) != 0) {
  601. wpa_printf(MSG_DEBUG, "WMM AC: frame destination addr="MACSTR
  602. " is other than ours, ignoring frame", MAC2STR(da));
  603. return;
  604. }
  605. if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0) {
  606. wpa_printf(MSG_DEBUG, "WMM AC: ignore frame with sa " MACSTR
  607. " different other than our bssid", MAC2STR(da));
  608. return;
  609. }
  610. if (len < 2 + sizeof(struct wmm_tspec_element)) {
  611. wpa_printf(MSG_DEBUG,
  612. "WMM AC: Short ADDTS response ignored (len=%lu)",
  613. (unsigned long) len);
  614. return;
  615. }
  616. data++;
  617. len--;
  618. dialog_token = data[0];
  619. status_code = data[1];
  620. if (ieee802_11_parse_elems(data + 2, len - 2, &elems, 1) != ParseOK) {
  621. wpa_printf(MSG_DEBUG,
  622. "WMM AC: Could not parse WMM AC action from " MACSTR,
  623. MAC2STR(sa));
  624. return;
  625. }
  626. /* the struct also contains the type and value, so decrease it */
  627. if (elems.wmm_tspec_len != sizeof(struct wmm_tspec_element) - 2) {
  628. wpa_printf(MSG_DEBUG, "WMM AC: missing or wrong length TSPEC");
  629. return;
  630. }
  631. tspec = (struct wmm_tspec_element *)(elems.wmm_tspec - 2);
  632. wpa_printf(MSG_DEBUG, "WMM AC: RX WMM AC Action from " MACSTR,
  633. MAC2STR(sa));
  634. wpa_hexdump(MSG_MSGDUMP, "WMM AC: WMM AC Action content", data, len);
  635. switch (action) {
  636. case WMM_ACTION_CODE_ADDTS_RESP:
  637. wmm_ac_handle_addts_resp(wpa_s, sa, dialog_token, status_code,
  638. tspec);
  639. break;
  640. case WMM_ACTION_CODE_DELTS:
  641. wmm_ac_handle_delts(wpa_s, sa, tspec);
  642. break;
  643. default:
  644. break;
  645. }
  646. }
  647. static const char * get_ac_str(u8 ac)
  648. {
  649. switch (ac) {
  650. case WMM_AC_BE:
  651. return "BE";
  652. case WMM_AC_BK:
  653. return "BK";
  654. case WMM_AC_VI:
  655. return "VI";
  656. case WMM_AC_VO:
  657. return "VO";
  658. default:
  659. return "N/A";
  660. }
  661. }
  662. static const char * get_direction_str(u8 direction)
  663. {
  664. switch (direction) {
  665. case WMM_AC_DIR_DOWNLINK:
  666. return "Downlink";
  667. case WMM_AC_DIR_UPLINK:
  668. return "Uplink";
  669. case WMM_AC_DIR_BIDIRECTIONAL:
  670. return "Bi-directional";
  671. default:
  672. return "N/A";
  673. }
  674. }
  675. int wpas_wmm_ac_status(struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  676. {
  677. struct wmm_ac_assoc_data *assoc_info = wpa_s->wmm_ac_assoc_info;
  678. enum ts_dir_idx idx;
  679. int pos = 0;
  680. u8 ac;
  681. if (!assoc_info) {
  682. return wpa_scnprintf(buf, buflen - pos,
  683. "Not associated to a WMM AP, WMM AC is Disabled\n");
  684. }
  685. pos += wpa_scnprintf(buf + pos, buflen - pos, "WMM AC is Enabled\n");
  686. for (ac = 0; ac < WMM_AC_NUM; ac++) {
  687. int ts_count = 0;
  688. pos += wpa_scnprintf(buf + pos, buflen - pos,
  689. "%s: acm=%d uapsd=%d\n",
  690. get_ac_str(ac),
  691. assoc_info->ac_params[ac].acm,
  692. assoc_info->ac_params[ac].uapsd);
  693. for (idx = 0; idx < TS_DIR_IDX_COUNT; idx++) {
  694. struct wmm_tspec_element *tspec;
  695. u8 dir, tsid;
  696. const char *dir_str;
  697. tspec = wpa_s->tspecs[ac][idx];
  698. if (!tspec)
  699. continue;
  700. ts_count++;
  701. dir = wmm_ac_get_direction(tspec);
  702. dir_str = get_direction_str(dir);
  703. tsid = wmm_ac_get_tsid(tspec);
  704. pos += wpa_scnprintf(buf + pos, buflen - pos,
  705. "\tTSID = %u\n"
  706. "\tAddress = "MACSTR"\n"
  707. "\tWMM AC dir = %s\n"
  708. "\tTotal admitted time = %u\n\n",
  709. tsid,
  710. MAC2STR(wpa_s->bssid),
  711. dir_str,
  712. le_to_host16(tspec->medium_time));
  713. }
  714. if (!ts_count) {
  715. pos += wpa_scnprintf(buf + pos, buflen - pos,
  716. "\t(No Traffic Stream)\n\n");
  717. }
  718. }
  719. return pos;
  720. }