agg-rx.c 14 KB

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