agg-rx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HT handling
  4. *
  5. * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
  6. * Copyright 2002-2005, Instant802 Networks, Inc.
  7. * Copyright 2005-2006, Devicescape Software, Inc.
  8. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  9. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  10. * Copyright 2007-2010, Intel Corporation
  11. * Copyright(c) 2015-2017 Intel Deutschland GmbH
  12. * Copyright (C) 2018-2023 Intel Corporation
  13. */
  14. /**
  15. * DOC: RX A-MPDU aggregation
  16. *
  17. * Aggregation on the RX side requires only implementing the
  18. * @ampdu_action callback that is invoked to start/stop any
  19. * block-ack sessions for RX aggregation.
  20. *
  21. * When RX aggregation is started by the peer, the driver is
  22. * notified via @ampdu_action function, with the
  23. * %IEEE80211_AMPDU_RX_START action, and may reject the request
  24. * in which case a negative response is sent to the peer, if it
  25. * accepts it a positive response is sent.
  26. *
  27. * While the session is active, the device/driver are required
  28. * to de-aggregate frames and pass them up one by one to mac80211,
  29. * which will handle the reorder buffer.
  30. *
  31. * When the aggregation session is stopped again by the peer or
  32. * ourselves, the driver's @ampdu_action function will be called
  33. * with the action %IEEE80211_AMPDU_RX_STOP. In this case, the
  34. * call must not fail.
  35. */
  36. #include <linux/ieee80211.h>
  37. #include <linux/slab.h>
  38. #include <linux/export.h>
  39. #include <net/mac80211.h>
  40. #include "ieee80211_i.h"
  41. #include "driver-ops.h"
  42. static void ieee80211_free_tid_rx(struct rcu_head *h)
  43. {
  44. struct tid_ampdu_rx *tid_rx =
  45. container_of(h, struct tid_ampdu_rx, rcu_head);
  46. int i;
  47. for (i = 0; i < tid_rx->buf_size; i++)
  48. __skb_queue_purge(&tid_rx->reorder_buf[i]);
  49. kfree(tid_rx->reorder_buf);
  50. kfree(tid_rx->reorder_time);
  51. kfree(tid_rx);
  52. }
  53. void __ieee80211_stop_rx_ba_session(struct sta_info *sta, u16 tid,
  54. u16 initiator, u16 reason, bool tx)
  55. {
  56. struct ieee80211_local *local = sta->local;
  57. struct tid_ampdu_rx *tid_rx;
  58. struct ieee80211_ampdu_params params = {
  59. .sta = &sta->sta,
  60. .action = IEEE80211_AMPDU_RX_STOP,
  61. .tid = tid,
  62. .amsdu = false,
  63. .timeout = 0,
  64. .ssn = 0,
  65. };
  66. lockdep_assert_wiphy(sta->local->hw.wiphy);
  67. tid_rx = rcu_dereference_protected(sta->ampdu_mlme.tid_rx[tid],
  68. lockdep_is_held(&sta->local->hw.wiphy->mtx));
  69. if (!test_bit(tid, sta->ampdu_mlme.agg_session_valid))
  70. return;
  71. RCU_INIT_POINTER(sta->ampdu_mlme.tid_rx[tid], NULL);
  72. __clear_bit(tid, sta->ampdu_mlme.agg_session_valid);
  73. ht_dbg(sta->sdata,
  74. "Rx BA session stop requested for %pM tid %u %s reason: %d\n",
  75. sta->sta.addr, tid,
  76. initiator == WLAN_BACK_RECIPIENT ? "recipient" : "initiator",
  77. (int)reason);
  78. if (drv_ampdu_action(local, sta->sdata, &params))
  79. sdata_info(sta->sdata,
  80. "HW problem - can not stop rx aggregation for %pM tid %d\n",
  81. sta->sta.addr, tid);
  82. /* check if this is a self generated aggregation halt */
  83. if (initiator == WLAN_BACK_RECIPIENT && tx)
  84. ieee80211_send_delba(sta->sdata, sta->sta.addr,
  85. tid, WLAN_BACK_RECIPIENT, reason);
  86. /*
  87. * return here in case tid_rx is not assigned - which will happen if
  88. * IEEE80211_HW_SUPPORTS_REORDERING_BUFFER is set.
  89. */
  90. if (!tid_rx)
  91. return;
  92. del_timer_sync(&tid_rx->session_timer);
  93. /* make sure ieee80211_sta_reorder_release() doesn't re-arm the timer */
  94. spin_lock_bh(&tid_rx->reorder_lock);
  95. tid_rx->removed = true;
  96. spin_unlock_bh(&tid_rx->reorder_lock);
  97. del_timer_sync(&tid_rx->reorder_timer);
  98. call_rcu(&tid_rx->rcu_head, ieee80211_free_tid_rx);
  99. }
  100. void ieee80211_stop_rx_ba_session(struct ieee80211_vif *vif, u16 ba_rx_bitmap,
  101. const u8 *addr)
  102. {
  103. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  104. struct sta_info *sta;
  105. int i;
  106. rcu_read_lock();
  107. sta = sta_info_get_bss(sdata, addr);
  108. if (!sta) {
  109. rcu_read_unlock();
  110. return;
  111. }
  112. for (i = 0; i < IEEE80211_NUM_TIDS; i++)
  113. if (ba_rx_bitmap & BIT(i))
  114. set_bit(i, sta->ampdu_mlme.tid_rx_stop_requested);
  115. wiphy_work_queue(sta->local->hw.wiphy, &sta->ampdu_mlme.work);
  116. rcu_read_unlock();
  117. }
  118. EXPORT_SYMBOL(ieee80211_stop_rx_ba_session);
  119. /*
  120. * After accepting the AddBA Request we activated a timer,
  121. * resetting it after each frame that arrives from the originator.
  122. */
  123. static void sta_rx_agg_session_timer_expired(struct timer_list *t)
  124. {
  125. struct tid_ampdu_rx *tid_rx = from_timer(tid_rx, t, session_timer);
  126. struct sta_info *sta = tid_rx->sta;
  127. u8 tid = tid_rx->tid;
  128. unsigned long timeout;
  129. timeout = tid_rx->last_rx + TU_TO_JIFFIES(tid_rx->timeout);
  130. if (time_is_after_jiffies(timeout)) {
  131. mod_timer(&tid_rx->session_timer, timeout);
  132. return;
  133. }
  134. ht_dbg(sta->sdata, "RX session timer expired on %pM tid %d\n",
  135. sta->sta.addr, tid);
  136. set_bit(tid, sta->ampdu_mlme.tid_rx_timer_expired);
  137. wiphy_work_queue(sta->local->hw.wiphy, &sta->ampdu_mlme.work);
  138. }
  139. static void sta_rx_agg_reorder_timer_expired(struct timer_list *t)
  140. {
  141. struct tid_ampdu_rx *tid_rx = from_timer(tid_rx, t, reorder_timer);
  142. rcu_read_lock();
  143. ieee80211_release_reorder_timeout(tid_rx->sta, tid_rx->tid);
  144. rcu_read_unlock();
  145. }
  146. static void ieee80211_add_addbaext(struct ieee80211_sub_if_data *sdata,
  147. struct sk_buff *skb,
  148. const struct ieee80211_addba_ext_ie *req,
  149. u16 buf_size)
  150. {
  151. struct ieee80211_addba_ext_ie *resp;
  152. u8 *pos;
  153. pos = skb_put_zero(skb, 2 + sizeof(struct ieee80211_addba_ext_ie));
  154. *pos++ = WLAN_EID_ADDBA_EXT;
  155. *pos++ = sizeof(struct ieee80211_addba_ext_ie);
  156. resp = (struct ieee80211_addba_ext_ie *)pos;
  157. resp->data = req->data & IEEE80211_ADDBA_EXT_NO_FRAG;
  158. resp->data |= u8_encode_bits(buf_size >> IEEE80211_ADDBA_EXT_BUF_SIZE_SHIFT,
  159. IEEE80211_ADDBA_EXT_BUF_SIZE_MASK);
  160. }
  161. static void ieee80211_send_addba_resp(struct sta_info *sta, u8 *da, u16 tid,
  162. u8 dialog_token, u16 status, u16 policy,
  163. u16 buf_size, u16 timeout,
  164. const struct ieee80211_addba_ext_ie *addbaext)
  165. {
  166. struct ieee80211_sub_if_data *sdata = sta->sdata;
  167. struct ieee80211_local *local = sdata->local;
  168. struct sk_buff *skb;
  169. struct ieee80211_mgmt *mgmt;
  170. bool amsdu = ieee80211_hw_check(&local->hw, SUPPORTS_AMSDU_IN_AMPDU);
  171. u16 capab;
  172. skb = dev_alloc_skb(sizeof(*mgmt) +
  173. 2 + sizeof(struct ieee80211_addba_ext_ie) +
  174. local->hw.extra_tx_headroom);
  175. if (!skb)
  176. return;
  177. skb_reserve(skb, local->hw.extra_tx_headroom);
  178. mgmt = ieee80211_mgmt_ba(skb, da, sdata);
  179. skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
  180. mgmt->u.action.category = WLAN_CATEGORY_BACK;
  181. mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
  182. mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
  183. capab = u16_encode_bits(amsdu, IEEE80211_ADDBA_PARAM_AMSDU_MASK);
  184. capab |= u16_encode_bits(policy, IEEE80211_ADDBA_PARAM_POLICY_MASK);
  185. capab |= u16_encode_bits(tid, IEEE80211_ADDBA_PARAM_TID_MASK);
  186. capab |= u16_encode_bits(buf_size, IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK);
  187. mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
  188. mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
  189. mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
  190. if (sta->sta.deflink.he_cap.has_he && addbaext)
  191. ieee80211_add_addbaext(sdata, skb, addbaext, buf_size);
  192. ieee80211_tx_skb(sdata, skb);
  193. }
  194. void __ieee80211_start_rx_ba_session(struct sta_info *sta,
  195. u8 dialog_token, u16 timeout,
  196. u16 start_seq_num, u16 ba_policy, u16 tid,
  197. u16 buf_size, bool tx, bool auto_seq,
  198. const struct ieee80211_addba_ext_ie *addbaext)
  199. {
  200. struct ieee80211_local *local = sta->sdata->local;
  201. struct tid_ampdu_rx *tid_agg_rx;
  202. struct ieee80211_ampdu_params params = {
  203. .sta = &sta->sta,
  204. .action = IEEE80211_AMPDU_RX_START,
  205. .tid = tid,
  206. .amsdu = false,
  207. .timeout = timeout,
  208. .ssn = start_seq_num,
  209. };
  210. int i, ret = -EOPNOTSUPP;
  211. u16 status = WLAN_STATUS_REQUEST_DECLINED;
  212. u16 max_buf_size;
  213. lockdep_assert_wiphy(sta->local->hw.wiphy);
  214. if (tid >= IEEE80211_FIRST_TSPEC_TSID) {
  215. ht_dbg(sta->sdata,
  216. "STA %pM requests BA session on unsupported tid %d\n",
  217. sta->sta.addr, tid);
  218. goto end;
  219. }
  220. if (!sta->sta.deflink.ht_cap.ht_supported &&
  221. !sta->sta.deflink.he_cap.has_he) {
  222. ht_dbg(sta->sdata,
  223. "STA %pM erroneously requests BA session on tid %d w/o HT\n",
  224. sta->sta.addr, tid);
  225. /* send a response anyway, it's an error case if we get here */
  226. goto end;
  227. }
  228. if (test_sta_flag(sta, WLAN_STA_BLOCK_BA)) {
  229. ht_dbg(sta->sdata,
  230. "Suspend in progress - Denying ADDBA request (%pM tid %d)\n",
  231. sta->sta.addr, tid);
  232. goto end;
  233. }
  234. if (sta->sta.deflink.eht_cap.has_eht)
  235. max_buf_size = IEEE80211_MAX_AMPDU_BUF_EHT;
  236. else if (sta->sta.deflink.he_cap.has_he)
  237. max_buf_size = IEEE80211_MAX_AMPDU_BUF_HE;
  238. else
  239. max_buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
  240. /* sanity check for incoming parameters:
  241. * check if configuration can support the BA policy
  242. * and if buffer size does not exceeds max value */
  243. /* XXX: check own ht delayed BA capability?? */
  244. if (((ba_policy != 1) &&
  245. (!(sta->sta.deflink.ht_cap.cap & IEEE80211_HT_CAP_DELAY_BA))) ||
  246. (buf_size > max_buf_size)) {
  247. status = WLAN_STATUS_INVALID_QOS_PARAM;
  248. ht_dbg_ratelimited(sta->sdata,
  249. "AddBA Req with bad params from %pM on tid %u. policy %d, buffer size %d\n",
  250. sta->sta.addr, tid, ba_policy, buf_size);
  251. goto end;
  252. }
  253. /* determine default buffer size */
  254. if (buf_size == 0)
  255. buf_size = max_buf_size;
  256. /* make sure the size doesn't exceed the maximum supported by the hw */
  257. if (buf_size > sta->sta.max_rx_aggregation_subframes)
  258. buf_size = sta->sta.max_rx_aggregation_subframes;
  259. params.buf_size = buf_size;
  260. ht_dbg(sta->sdata, "AddBA Req buf_size=%d for %pM\n",
  261. buf_size, sta->sta.addr);
  262. if (test_bit(tid, sta->ampdu_mlme.agg_session_valid)) {
  263. if (sta->ampdu_mlme.tid_rx_token[tid] == dialog_token) {
  264. struct tid_ampdu_rx *tid_rx;
  265. ht_dbg_ratelimited(sta->sdata,
  266. "updated AddBA Req from %pM on tid %u\n",
  267. sta->sta.addr, tid);
  268. /* We have no API to update the timeout value in the
  269. * driver so reject the timeout update if the timeout
  270. * changed. If it did not change, i.e., no real update,
  271. * just reply with success.
  272. */
  273. rcu_read_lock();
  274. tid_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  275. if (tid_rx && tid_rx->timeout == timeout)
  276. status = WLAN_STATUS_SUCCESS;
  277. else
  278. status = WLAN_STATUS_REQUEST_DECLINED;
  279. rcu_read_unlock();
  280. goto end;
  281. }
  282. ht_dbg_ratelimited(sta->sdata,
  283. "unexpected AddBA Req from %pM on tid %u\n",
  284. sta->sta.addr, tid);
  285. /* delete existing Rx BA session on the same tid */
  286. __ieee80211_stop_rx_ba_session(sta, tid, WLAN_BACK_RECIPIENT,
  287. WLAN_STATUS_UNSPECIFIED_QOS,
  288. false);
  289. }
  290. if (ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER)) {
  291. ret = drv_ampdu_action(local, sta->sdata, &params);
  292. ht_dbg(sta->sdata,
  293. "Rx A-MPDU request on %pM tid %d result %d\n",
  294. sta->sta.addr, tid, ret);
  295. if (!ret)
  296. status = WLAN_STATUS_SUCCESS;
  297. goto end;
  298. }
  299. /* prepare A-MPDU MLME for Rx aggregation */
  300. tid_agg_rx = kzalloc(sizeof(*tid_agg_rx), GFP_KERNEL);
  301. if (!tid_agg_rx)
  302. goto end;
  303. spin_lock_init(&tid_agg_rx->reorder_lock);
  304. /* rx timer */
  305. timer_setup(&tid_agg_rx->session_timer,
  306. sta_rx_agg_session_timer_expired, TIMER_DEFERRABLE);
  307. /* rx reorder timer */
  308. timer_setup(&tid_agg_rx->reorder_timer,
  309. sta_rx_agg_reorder_timer_expired, 0);
  310. /* prepare reordering buffer */
  311. tid_agg_rx->reorder_buf =
  312. kcalloc(buf_size, sizeof(struct sk_buff_head), GFP_KERNEL);
  313. tid_agg_rx->reorder_time =
  314. kcalloc(buf_size, sizeof(unsigned long), GFP_KERNEL);
  315. if (!tid_agg_rx->reorder_buf || !tid_agg_rx->reorder_time) {
  316. kfree(tid_agg_rx->reorder_buf);
  317. kfree(tid_agg_rx->reorder_time);
  318. kfree(tid_agg_rx);
  319. goto end;
  320. }
  321. for (i = 0; i < buf_size; i++)
  322. __skb_queue_head_init(&tid_agg_rx->reorder_buf[i]);
  323. ret = drv_ampdu_action(local, sta->sdata, &params);
  324. ht_dbg(sta->sdata, "Rx A-MPDU request on %pM tid %d result %d\n",
  325. sta->sta.addr, tid, ret);
  326. if (ret) {
  327. kfree(tid_agg_rx->reorder_buf);
  328. kfree(tid_agg_rx->reorder_time);
  329. kfree(tid_agg_rx);
  330. goto end;
  331. }
  332. /* update data */
  333. tid_agg_rx->ssn = start_seq_num;
  334. tid_agg_rx->head_seq_num = start_seq_num;
  335. tid_agg_rx->buf_size = buf_size;
  336. tid_agg_rx->timeout = timeout;
  337. tid_agg_rx->stored_mpdu_num = 0;
  338. tid_agg_rx->auto_seq = auto_seq;
  339. tid_agg_rx->started = false;
  340. tid_agg_rx->reorder_buf_filtered = 0;
  341. tid_agg_rx->tid = tid;
  342. tid_agg_rx->sta = sta;
  343. status = WLAN_STATUS_SUCCESS;
  344. /* activate it for RX */
  345. rcu_assign_pointer(sta->ampdu_mlme.tid_rx[tid], tid_agg_rx);
  346. if (timeout) {
  347. mod_timer(&tid_agg_rx->session_timer, TU_TO_EXP_TIME(timeout));
  348. tid_agg_rx->last_rx = jiffies;
  349. }
  350. end:
  351. if (status == WLAN_STATUS_SUCCESS) {
  352. __set_bit(tid, sta->ampdu_mlme.agg_session_valid);
  353. __clear_bit(tid, sta->ampdu_mlme.unexpected_agg);
  354. sta->ampdu_mlme.tid_rx_token[tid] = dialog_token;
  355. }
  356. if (tx)
  357. ieee80211_send_addba_resp(sta, sta->sta.addr, tid,
  358. dialog_token, status, 1, buf_size,
  359. timeout, addbaext);
  360. }
  361. void ieee80211_process_addba_request(struct ieee80211_local *local,
  362. struct sta_info *sta,
  363. struct ieee80211_mgmt *mgmt,
  364. size_t len)
  365. {
  366. u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num;
  367. struct ieee802_11_elems *elems = NULL;
  368. u8 dialog_token;
  369. int ies_len;
  370. /* extract session parameters from addba request frame */
  371. dialog_token = mgmt->u.action.u.addba_req.dialog_token;
  372. timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
  373. start_seq_num =
  374. le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
  375. capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
  376. ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
  377. tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
  378. buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
  379. ies_len = len - offsetof(struct ieee80211_mgmt,
  380. u.action.u.addba_req.variable);
  381. if (ies_len) {
  382. elems = ieee802_11_parse_elems(mgmt->u.action.u.addba_req.variable,
  383. ies_len, true, NULL);
  384. if (!elems || elems->parse_error)
  385. goto free;
  386. }
  387. if (sta->sta.deflink.eht_cap.has_eht && elems && elems->addba_ext_ie) {
  388. u8 buf_size_1k = u8_get_bits(elems->addba_ext_ie->data,
  389. IEEE80211_ADDBA_EXT_BUF_SIZE_MASK);
  390. buf_size |= buf_size_1k << IEEE80211_ADDBA_EXT_BUF_SIZE_SHIFT;
  391. }
  392. __ieee80211_start_rx_ba_session(sta, dialog_token, timeout,
  393. start_seq_num, ba_policy, tid,
  394. buf_size, true, false,
  395. elems ? elems->addba_ext_ie : NULL);
  396. free:
  397. kfree(elems);
  398. }
  399. void ieee80211_manage_rx_ba_offl(struct ieee80211_vif *vif,
  400. const u8 *addr, unsigned int tid)
  401. {
  402. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  403. struct sta_info *sta;
  404. rcu_read_lock();
  405. sta = sta_info_get_bss(sdata, addr);
  406. if (!sta)
  407. goto unlock;
  408. set_bit(tid, sta->ampdu_mlme.tid_rx_manage_offl);
  409. wiphy_work_queue(sta->local->hw.wiphy, &sta->ampdu_mlme.work);
  410. unlock:
  411. rcu_read_unlock();
  412. }
  413. EXPORT_SYMBOL(ieee80211_manage_rx_ba_offl);
  414. void ieee80211_rx_ba_timer_expired(struct ieee80211_vif *vif,
  415. const u8 *addr, unsigned int tid)
  416. {
  417. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  418. struct sta_info *sta;
  419. rcu_read_lock();
  420. sta = sta_info_get_bss(sdata, addr);
  421. if (!sta)
  422. goto unlock;
  423. set_bit(tid, sta->ampdu_mlme.tid_rx_timer_expired);
  424. wiphy_work_queue(sta->local->hw.wiphy, &sta->ampdu_mlme.work);
  425. unlock:
  426. rcu_read_unlock();
  427. }
  428. EXPORT_SYMBOL(ieee80211_rx_ba_timer_expired);