sta_info.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/types.h>
  14. #include <linux/slab.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/timer.h>
  18. #include <linux/rtnetlink.h>
  19. #include <net/mac80211.h>
  20. #include "ieee80211_i.h"
  21. #include "driver-ops.h"
  22. #include "rate.h"
  23. #include "sta_info.h"
  24. #include "debugfs_sta.h"
  25. #include "mesh.h"
  26. #include "wme.h"
  27. /**
  28. * DOC: STA information lifetime rules
  29. *
  30. * STA info structures (&struct sta_info) are managed in a hash table
  31. * for faster lookup and a list for iteration. They are managed using
  32. * RCU, i.e. access to the list and hash table is protected by RCU.
  33. *
  34. * Upon allocating a STA info structure with sta_info_alloc(), the caller
  35. * owns that structure. It must then insert it into the hash table using
  36. * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
  37. * case (which acquires an rcu read section but must not be called from
  38. * within one) will the pointer still be valid after the call. Note that
  39. * the caller may not do much with the STA info before inserting it, in
  40. * particular, it may not start any mesh peer link management or add
  41. * encryption keys.
  42. *
  43. * When the insertion fails (sta_info_insert()) returns non-zero), the
  44. * structure will have been freed by sta_info_insert()!
  45. *
  46. * Station entries are added by mac80211 when you establish a link with a
  47. * peer. This means different things for the different type of interfaces
  48. * we support. For a regular station this mean we add the AP sta when we
  49. * receive an association response from the AP. For IBSS this occurs when
  50. * get to know about a peer on the same IBSS. For WDS we add the sta for
  51. * the peer immediately upon device open. When using AP mode we add stations
  52. * for each respective station upon request from userspace through nl80211.
  53. *
  54. * In order to remove a STA info structure, various sta_info_destroy_*()
  55. * calls are available.
  56. *
  57. * There is no concept of ownership on a STA entry, each structure is
  58. * owned by the global hash table/list until it is removed. All users of
  59. * the structure need to be RCU protected so that the structure won't be
  60. * freed before they are done using it.
  61. */
  62. /* Caller must hold local->sta_mtx */
  63. static int sta_info_hash_del(struct ieee80211_local *local,
  64. struct sta_info *sta)
  65. {
  66. struct sta_info *s;
  67. s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
  68. lockdep_is_held(&local->sta_mtx));
  69. if (!s)
  70. return -ENOENT;
  71. if (s == sta) {
  72. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
  73. s->hnext);
  74. return 0;
  75. }
  76. while (rcu_access_pointer(s->hnext) &&
  77. rcu_access_pointer(s->hnext) != sta)
  78. s = rcu_dereference_protected(s->hnext,
  79. lockdep_is_held(&local->sta_mtx));
  80. if (rcu_access_pointer(s->hnext)) {
  81. rcu_assign_pointer(s->hnext, sta->hnext);
  82. return 0;
  83. }
  84. return -ENOENT;
  85. }
  86. static void cleanup_single_sta(struct sta_info *sta)
  87. {
  88. int ac, i;
  89. struct tid_ampdu_tx *tid_tx;
  90. struct ieee80211_sub_if_data *sdata = sta->sdata;
  91. struct ieee80211_local *local = sdata->local;
  92. struct ps_data *ps;
  93. if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
  94. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  95. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  96. ps = &sdata->bss->ps;
  97. else if (ieee80211_vif_is_mesh(&sdata->vif))
  98. ps = &sdata->u.mesh.ps;
  99. else
  100. return;
  101. clear_sta_flag(sta, WLAN_STA_PS_STA);
  102. atomic_dec(&ps->num_sta_ps);
  103. sta_info_recalc_tim(sta);
  104. }
  105. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  106. local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
  107. ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
  108. ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
  109. }
  110. if (ieee80211_vif_is_mesh(&sdata->vif))
  111. mesh_sta_cleanup(sta);
  112. cancel_work_sync(&sta->drv_unblock_wk);
  113. /*
  114. * Destroy aggregation state here. It would be nice to wait for the
  115. * driver to finish aggregation stop and then clean up, but for now
  116. * drivers have to handle aggregation stop being requested, followed
  117. * directly by station destruction.
  118. */
  119. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  120. kfree(sta->ampdu_mlme.tid_start_tx[i]);
  121. tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
  122. if (!tid_tx)
  123. continue;
  124. ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
  125. kfree(tid_tx);
  126. }
  127. sta_info_free(local, sta);
  128. }
  129. /* protected by RCU */
  130. struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
  131. const u8 *addr)
  132. {
  133. struct ieee80211_local *local = sdata->local;
  134. struct sta_info *sta;
  135. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  136. lockdep_is_held(&local->sta_mtx));
  137. while (sta) {
  138. if (sta->sdata == sdata &&
  139. ether_addr_equal(sta->sta.addr, addr))
  140. break;
  141. sta = rcu_dereference_check(sta->hnext,
  142. lockdep_is_held(&local->sta_mtx));
  143. }
  144. return sta;
  145. }
  146. /*
  147. * Get sta info either from the specified interface
  148. * or from one of its vlans
  149. */
  150. struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
  151. const u8 *addr)
  152. {
  153. struct ieee80211_local *local = sdata->local;
  154. struct sta_info *sta;
  155. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  156. lockdep_is_held(&local->sta_mtx));
  157. while (sta) {
  158. if ((sta->sdata == sdata ||
  159. (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
  160. ether_addr_equal(sta->sta.addr, addr))
  161. break;
  162. sta = rcu_dereference_check(sta->hnext,
  163. lockdep_is_held(&local->sta_mtx));
  164. }
  165. return sta;
  166. }
  167. struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
  168. int idx)
  169. {
  170. struct ieee80211_local *local = sdata->local;
  171. struct sta_info *sta;
  172. int i = 0;
  173. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  174. if (sdata != sta->sdata)
  175. continue;
  176. if (i < idx) {
  177. ++i;
  178. continue;
  179. }
  180. return sta;
  181. }
  182. return NULL;
  183. }
  184. /**
  185. * sta_info_free - free STA
  186. *
  187. * @local: pointer to the global information
  188. * @sta: STA info to free
  189. *
  190. * This function must undo everything done by sta_info_alloc()
  191. * that may happen before sta_info_insert(). It may only be
  192. * called when sta_info_insert() has not been attempted (and
  193. * if that fails, the station is freed anyway.)
  194. */
  195. void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
  196. {
  197. int i;
  198. if (sta->rate_ctrl)
  199. rate_control_free_sta(sta);
  200. if (sta->tx_lat) {
  201. for (i = 0; i < IEEE80211_NUM_TIDS; i++)
  202. kfree(sta->tx_lat[i].bins);
  203. kfree(sta->tx_lat);
  204. }
  205. sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
  206. kfree(sta);
  207. }
  208. /* Caller must hold local->sta_mtx */
  209. static void sta_info_hash_add(struct ieee80211_local *local,
  210. struct sta_info *sta)
  211. {
  212. lockdep_assert_held(&local->sta_mtx);
  213. sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
  214. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
  215. }
  216. static void sta_unblock(struct work_struct *wk)
  217. {
  218. struct sta_info *sta;
  219. sta = container_of(wk, struct sta_info, drv_unblock_wk);
  220. if (sta->dead)
  221. return;
  222. if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
  223. local_bh_disable();
  224. ieee80211_sta_ps_deliver_wakeup(sta);
  225. local_bh_enable();
  226. } else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
  227. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  228. local_bh_disable();
  229. ieee80211_sta_ps_deliver_poll_response(sta);
  230. local_bh_enable();
  231. } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
  232. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  233. local_bh_disable();
  234. ieee80211_sta_ps_deliver_uapsd(sta);
  235. local_bh_enable();
  236. } else
  237. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  238. }
  239. static int sta_prepare_rate_control(struct ieee80211_local *local,
  240. struct sta_info *sta, gfp_t gfp)
  241. {
  242. if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
  243. return 0;
  244. sta->rate_ctrl = local->rate_ctrl;
  245. sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
  246. &sta->sta, gfp);
  247. if (!sta->rate_ctrl_priv)
  248. return -ENOMEM;
  249. return 0;
  250. }
  251. struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
  252. const u8 *addr, gfp_t gfp)
  253. {
  254. struct ieee80211_local *local = sdata->local;
  255. struct sta_info *sta;
  256. struct timespec uptime;
  257. struct ieee80211_tx_latency_bin_ranges *tx_latency;
  258. int i;
  259. sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
  260. if (!sta)
  261. return NULL;
  262. rcu_read_lock();
  263. tx_latency = rcu_dereference(local->tx_latency);
  264. /* init stations Tx latency statistics && TID bins */
  265. if (tx_latency) {
  266. sta->tx_lat = kzalloc(IEEE80211_NUM_TIDS *
  267. sizeof(struct ieee80211_tx_latency_stat),
  268. GFP_ATOMIC);
  269. if (!sta->tx_lat) {
  270. rcu_read_unlock();
  271. goto free;
  272. }
  273. if (tx_latency->n_ranges) {
  274. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  275. /* size of bins is size of the ranges +1 */
  276. sta->tx_lat[i].bin_count =
  277. tx_latency->n_ranges + 1;
  278. sta->tx_lat[i].bins =
  279. kcalloc(sta->tx_lat[i].bin_count,
  280. sizeof(u32), GFP_ATOMIC);
  281. if (!sta->tx_lat[i].bins) {
  282. rcu_read_unlock();
  283. goto free;
  284. }
  285. }
  286. }
  287. }
  288. rcu_read_unlock();
  289. spin_lock_init(&sta->lock);
  290. INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
  291. INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
  292. mutex_init(&sta->ampdu_mlme.mtx);
  293. #ifdef CONFIG_MAC80211_MESH
  294. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  295. !sdata->u.mesh.user_mpm)
  296. init_timer(&sta->plink_timer);
  297. sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
  298. #endif
  299. memcpy(sta->sta.addr, addr, ETH_ALEN);
  300. sta->local = local;
  301. sta->sdata = sdata;
  302. sta->last_rx = jiffies;
  303. sta->sta_state = IEEE80211_STA_NONE;
  304. do_posix_clock_monotonic_gettime(&uptime);
  305. sta->last_connected = uptime.tv_sec;
  306. ewma_init(&sta->avg_signal, 1024, 8);
  307. for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
  308. ewma_init(&sta->chain_signal_avg[i], 1024, 8);
  309. if (sta_prepare_rate_control(local, sta, gfp))
  310. goto free;
  311. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  312. /*
  313. * timer_to_tid must be initialized with identity mapping
  314. * to enable session_timer's data differentiation. See
  315. * sta_rx_agg_session_timer_expired for usage.
  316. */
  317. sta->timer_to_tid[i] = i;
  318. }
  319. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  320. skb_queue_head_init(&sta->ps_tx_buf[i]);
  321. skb_queue_head_init(&sta->tx_filtered[i]);
  322. }
  323. for (i = 0; i < IEEE80211_NUM_TIDS; i++)
  324. sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
  325. sta->sta.smps_mode = IEEE80211_SMPS_OFF;
  326. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  327. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  328. struct ieee80211_supported_band *sband =
  329. local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
  330. u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
  331. IEEE80211_HT_CAP_SM_PS_SHIFT;
  332. /*
  333. * Assume that hostapd advertises our caps in the beacon and
  334. * this is the known_smps_mode for a station that just assciated
  335. */
  336. switch (smps) {
  337. case WLAN_HT_SMPS_CONTROL_DISABLED:
  338. sta->known_smps_mode = IEEE80211_SMPS_OFF;
  339. break;
  340. case WLAN_HT_SMPS_CONTROL_STATIC:
  341. sta->known_smps_mode = IEEE80211_SMPS_STATIC;
  342. break;
  343. case WLAN_HT_SMPS_CONTROL_DYNAMIC:
  344. sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
  345. break;
  346. default:
  347. WARN_ON(1);
  348. }
  349. }
  350. sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
  351. return sta;
  352. free:
  353. if (sta->tx_lat) {
  354. for (i = 0; i < IEEE80211_NUM_TIDS; i++)
  355. kfree(sta->tx_lat[i].bins);
  356. kfree(sta->tx_lat);
  357. }
  358. kfree(sta);
  359. return NULL;
  360. }
  361. static int sta_info_insert_check(struct sta_info *sta)
  362. {
  363. struct ieee80211_sub_if_data *sdata = sta->sdata;
  364. /*
  365. * Can't be a WARN_ON because it can be triggered through a race:
  366. * something inserts a STA (on one CPU) without holding the RTNL
  367. * and another CPU turns off the net device.
  368. */
  369. if (unlikely(!ieee80211_sdata_running(sdata)))
  370. return -ENETDOWN;
  371. if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
  372. is_multicast_ether_addr(sta->sta.addr)))
  373. return -EINVAL;
  374. return 0;
  375. }
  376. static int sta_info_insert_drv_state(struct ieee80211_local *local,
  377. struct ieee80211_sub_if_data *sdata,
  378. struct sta_info *sta)
  379. {
  380. enum ieee80211_sta_state state;
  381. int err = 0;
  382. for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
  383. err = drv_sta_state(local, sdata, sta, state, state + 1);
  384. if (err)
  385. break;
  386. }
  387. if (!err) {
  388. /*
  389. * Drivers using legacy sta_add/sta_remove callbacks only
  390. * get uploaded set to true after sta_add is called.
  391. */
  392. if (!local->ops->sta_add)
  393. sta->uploaded = true;
  394. return 0;
  395. }
  396. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  397. sdata_info(sdata,
  398. "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
  399. sta->sta.addr, state + 1, err);
  400. err = 0;
  401. }
  402. /* unwind on error */
  403. for (; state > IEEE80211_STA_NOTEXIST; state--)
  404. WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
  405. return err;
  406. }
  407. /*
  408. * should be called with sta_mtx locked
  409. * this function replaces the mutex lock
  410. * with a RCU lock
  411. */
  412. static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
  413. {
  414. struct ieee80211_local *local = sta->local;
  415. struct ieee80211_sub_if_data *sdata = sta->sdata;
  416. struct station_info sinfo;
  417. int err = 0;
  418. lockdep_assert_held(&local->sta_mtx);
  419. /* check if STA exists already */
  420. if (sta_info_get_bss(sdata, sta->sta.addr)) {
  421. err = -EEXIST;
  422. goto out_err;
  423. }
  424. /* notify driver */
  425. err = sta_info_insert_drv_state(local, sdata, sta);
  426. if (err)
  427. goto out_err;
  428. local->num_sta++;
  429. local->sta_generation++;
  430. smp_mb();
  431. /* make the station visible */
  432. sta_info_hash_add(local, sta);
  433. list_add_rcu(&sta->list, &local->sta_list);
  434. set_sta_flag(sta, WLAN_STA_INSERTED);
  435. ieee80211_recalc_min_chandef(sdata);
  436. ieee80211_sta_debugfs_add(sta);
  437. rate_control_add_sta_debugfs(sta);
  438. memset(&sinfo, 0, sizeof(sinfo));
  439. sinfo.filled = 0;
  440. sinfo.generation = local->sta_generation;
  441. cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
  442. sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
  443. /* move reference to rcu-protected */
  444. rcu_read_lock();
  445. mutex_unlock(&local->sta_mtx);
  446. if (ieee80211_vif_is_mesh(&sdata->vif))
  447. mesh_accept_plinks_update(sdata);
  448. return 0;
  449. out_err:
  450. mutex_unlock(&local->sta_mtx);
  451. rcu_read_lock();
  452. return err;
  453. }
  454. int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
  455. {
  456. struct ieee80211_local *local = sta->local;
  457. int err = 0;
  458. might_sleep();
  459. err = sta_info_insert_check(sta);
  460. if (err) {
  461. rcu_read_lock();
  462. goto out_free;
  463. }
  464. mutex_lock(&local->sta_mtx);
  465. err = sta_info_insert_finish(sta);
  466. if (err)
  467. goto out_free;
  468. return 0;
  469. out_free:
  470. BUG_ON(!err);
  471. sta_info_free(local, sta);
  472. return err;
  473. }
  474. int sta_info_insert(struct sta_info *sta)
  475. {
  476. int err = sta_info_insert_rcu(sta);
  477. rcu_read_unlock();
  478. return err;
  479. }
  480. static inline void __bss_tim_set(u8 *tim, u16 id)
  481. {
  482. /*
  483. * This format has been mandated by the IEEE specifications,
  484. * so this line may not be changed to use the __set_bit() format.
  485. */
  486. tim[id / 8] |= (1 << (id % 8));
  487. }
  488. static inline void __bss_tim_clear(u8 *tim, u16 id)
  489. {
  490. /*
  491. * This format has been mandated by the IEEE specifications,
  492. * so this line may not be changed to use the __clear_bit() format.
  493. */
  494. tim[id / 8] &= ~(1 << (id % 8));
  495. }
  496. static inline bool __bss_tim_get(u8 *tim, u16 id)
  497. {
  498. /*
  499. * This format has been mandated by the IEEE specifications,
  500. * so this line may not be changed to use the test_bit() format.
  501. */
  502. return tim[id / 8] & (1 << (id % 8));
  503. }
  504. static unsigned long ieee80211_tids_for_ac(int ac)
  505. {
  506. /* If we ever support TIDs > 7, this obviously needs to be adjusted */
  507. switch (ac) {
  508. case IEEE80211_AC_VO:
  509. return BIT(6) | BIT(7);
  510. case IEEE80211_AC_VI:
  511. return BIT(4) | BIT(5);
  512. case IEEE80211_AC_BE:
  513. return BIT(0) | BIT(3);
  514. case IEEE80211_AC_BK:
  515. return BIT(1) | BIT(2);
  516. default:
  517. WARN_ON(1);
  518. return 0;
  519. }
  520. }
  521. void sta_info_recalc_tim(struct sta_info *sta)
  522. {
  523. struct ieee80211_local *local = sta->local;
  524. struct ps_data *ps;
  525. bool indicate_tim = false;
  526. u8 ignore_for_tim = sta->sta.uapsd_queues;
  527. int ac;
  528. u16 id;
  529. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  530. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  531. if (WARN_ON_ONCE(!sta->sdata->bss))
  532. return;
  533. ps = &sta->sdata->bss->ps;
  534. id = sta->sta.aid;
  535. #ifdef CONFIG_MAC80211_MESH
  536. } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
  537. ps = &sta->sdata->u.mesh.ps;
  538. /* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
  539. id = sta->plid % (IEEE80211_MAX_AID + 1);
  540. #endif
  541. } else {
  542. return;
  543. }
  544. /* No need to do anything if the driver does all */
  545. if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
  546. return;
  547. if (sta->dead)
  548. goto done;
  549. /*
  550. * If all ACs are delivery-enabled then we should build
  551. * the TIM bit for all ACs anyway; if only some are then
  552. * we ignore those and build the TIM bit using only the
  553. * non-enabled ones.
  554. */
  555. if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
  556. ignore_for_tim = 0;
  557. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  558. unsigned long tids;
  559. if (ignore_for_tim & BIT(ac))
  560. continue;
  561. indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
  562. !skb_queue_empty(&sta->ps_tx_buf[ac]);
  563. if (indicate_tim)
  564. break;
  565. tids = ieee80211_tids_for_ac(ac);
  566. indicate_tim |=
  567. sta->driver_buffered_tids & tids;
  568. }
  569. done:
  570. spin_lock_bh(&local->tim_lock);
  571. if (indicate_tim == __bss_tim_get(ps->tim, id))
  572. goto out_unlock;
  573. if (indicate_tim)
  574. __bss_tim_set(ps->tim, id);
  575. else
  576. __bss_tim_clear(ps->tim, id);
  577. if (local->ops->set_tim) {
  578. local->tim_in_locked_section = true;
  579. drv_set_tim(local, &sta->sta, indicate_tim);
  580. local->tim_in_locked_section = false;
  581. }
  582. out_unlock:
  583. spin_unlock_bh(&local->tim_lock);
  584. }
  585. static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
  586. {
  587. struct ieee80211_tx_info *info;
  588. int timeout;
  589. if (!skb)
  590. return false;
  591. info = IEEE80211_SKB_CB(skb);
  592. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  593. timeout = (sta->listen_interval *
  594. sta->sdata->vif.bss_conf.beacon_int *
  595. 32 / 15625) * HZ;
  596. if (timeout < STA_TX_BUFFER_EXPIRE)
  597. timeout = STA_TX_BUFFER_EXPIRE;
  598. return time_after(jiffies, info->control.jiffies + timeout);
  599. }
  600. static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
  601. struct sta_info *sta, int ac)
  602. {
  603. unsigned long flags;
  604. struct sk_buff *skb;
  605. /*
  606. * First check for frames that should expire on the filtered
  607. * queue. Frames here were rejected by the driver and are on
  608. * a separate queue to avoid reordering with normal PS-buffered
  609. * frames. They also aren't accounted for right now in the
  610. * total_ps_buffered counter.
  611. */
  612. for (;;) {
  613. spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
  614. skb = skb_peek(&sta->tx_filtered[ac]);
  615. if (sta_info_buffer_expired(sta, skb))
  616. skb = __skb_dequeue(&sta->tx_filtered[ac]);
  617. else
  618. skb = NULL;
  619. spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
  620. /*
  621. * Frames are queued in order, so if this one
  622. * hasn't expired yet we can stop testing. If
  623. * we actually reached the end of the queue we
  624. * also need to stop, of course.
  625. */
  626. if (!skb)
  627. break;
  628. ieee80211_free_txskb(&local->hw, skb);
  629. }
  630. /*
  631. * Now also check the normal PS-buffered queue, this will
  632. * only find something if the filtered queue was emptied
  633. * since the filtered frames are all before the normal PS
  634. * buffered frames.
  635. */
  636. for (;;) {
  637. spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
  638. skb = skb_peek(&sta->ps_tx_buf[ac]);
  639. if (sta_info_buffer_expired(sta, skb))
  640. skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
  641. else
  642. skb = NULL;
  643. spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
  644. /*
  645. * frames are queued in order, so if this one
  646. * hasn't expired yet (or we reached the end of
  647. * the queue) we can stop testing
  648. */
  649. if (!skb)
  650. break;
  651. local->total_ps_buffered--;
  652. ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
  653. sta->sta.addr);
  654. ieee80211_free_txskb(&local->hw, skb);
  655. }
  656. /*
  657. * Finally, recalculate the TIM bit for this station -- it might
  658. * now be clear because the station was too slow to retrieve its
  659. * frames.
  660. */
  661. sta_info_recalc_tim(sta);
  662. /*
  663. * Return whether there are any frames still buffered, this is
  664. * used to check whether the cleanup timer still needs to run,
  665. * if there are no frames we don't need to rearm the timer.
  666. */
  667. return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
  668. skb_queue_empty(&sta->tx_filtered[ac]));
  669. }
  670. static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  671. struct sta_info *sta)
  672. {
  673. bool have_buffered = false;
  674. int ac;
  675. /* This is only necessary for stations on BSS/MBSS interfaces */
  676. if (!sta->sdata->bss &&
  677. !ieee80211_vif_is_mesh(&sta->sdata->vif))
  678. return false;
  679. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  680. have_buffered |=
  681. sta_info_cleanup_expire_buffered_ac(local, sta, ac);
  682. return have_buffered;
  683. }
  684. static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
  685. {
  686. struct ieee80211_local *local;
  687. struct ieee80211_sub_if_data *sdata;
  688. int ret;
  689. might_sleep();
  690. if (!sta)
  691. return -ENOENT;
  692. local = sta->local;
  693. sdata = sta->sdata;
  694. lockdep_assert_held(&local->sta_mtx);
  695. /*
  696. * Before removing the station from the driver and
  697. * rate control, it might still start new aggregation
  698. * sessions -- block that to make sure the tear-down
  699. * will be sufficient.
  700. */
  701. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  702. ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
  703. ret = sta_info_hash_del(local, sta);
  704. if (WARN_ON(ret))
  705. return ret;
  706. list_del_rcu(&sta->list);
  707. drv_sta_pre_rcu_remove(local, sta->sdata, sta);
  708. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  709. rcu_access_pointer(sdata->u.vlan.sta) == sta)
  710. RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
  711. return 0;
  712. }
  713. static void __sta_info_destroy_part2(struct sta_info *sta)
  714. {
  715. struct ieee80211_local *local = sta->local;
  716. struct ieee80211_sub_if_data *sdata = sta->sdata;
  717. int ret;
  718. /*
  719. * NOTE: This assumes at least synchronize_net() was done
  720. * after _part1 and before _part2!
  721. */
  722. might_sleep();
  723. lockdep_assert_held(&local->sta_mtx);
  724. /* now keys can no longer be reached */
  725. ieee80211_free_sta_keys(local, sta);
  726. sta->dead = true;
  727. local->num_sta--;
  728. local->sta_generation++;
  729. while (sta->sta_state > IEEE80211_STA_NONE) {
  730. ret = sta_info_move_state(sta, sta->sta_state - 1);
  731. if (ret) {
  732. WARN_ON_ONCE(1);
  733. break;
  734. }
  735. }
  736. if (sta->uploaded) {
  737. ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
  738. IEEE80211_STA_NOTEXIST);
  739. WARN_ON_ONCE(ret != 0);
  740. }
  741. sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
  742. cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
  743. rate_control_remove_sta_debugfs(sta);
  744. ieee80211_sta_debugfs_remove(sta);
  745. ieee80211_recalc_min_chandef(sdata);
  746. cleanup_single_sta(sta);
  747. }
  748. int __must_check __sta_info_destroy(struct sta_info *sta)
  749. {
  750. int err = __sta_info_destroy_part1(sta);
  751. if (err)
  752. return err;
  753. synchronize_net();
  754. __sta_info_destroy_part2(sta);
  755. return 0;
  756. }
  757. int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
  758. {
  759. struct sta_info *sta;
  760. int ret;
  761. mutex_lock(&sdata->local->sta_mtx);
  762. sta = sta_info_get(sdata, addr);
  763. ret = __sta_info_destroy(sta);
  764. mutex_unlock(&sdata->local->sta_mtx);
  765. return ret;
  766. }
  767. int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
  768. const u8 *addr)
  769. {
  770. struct sta_info *sta;
  771. int ret;
  772. mutex_lock(&sdata->local->sta_mtx);
  773. sta = sta_info_get_bss(sdata, addr);
  774. ret = __sta_info_destroy(sta);
  775. mutex_unlock(&sdata->local->sta_mtx);
  776. return ret;
  777. }
  778. static void sta_info_cleanup(unsigned long data)
  779. {
  780. struct ieee80211_local *local = (struct ieee80211_local *) data;
  781. struct sta_info *sta;
  782. bool timer_needed = false;
  783. rcu_read_lock();
  784. list_for_each_entry_rcu(sta, &local->sta_list, list)
  785. if (sta_info_cleanup_expire_buffered(local, sta))
  786. timer_needed = true;
  787. rcu_read_unlock();
  788. if (local->quiescing)
  789. return;
  790. if (!timer_needed)
  791. return;
  792. mod_timer(&local->sta_cleanup,
  793. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
  794. }
  795. void sta_info_init(struct ieee80211_local *local)
  796. {
  797. spin_lock_init(&local->tim_lock);
  798. mutex_init(&local->sta_mtx);
  799. INIT_LIST_HEAD(&local->sta_list);
  800. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  801. (unsigned long)local);
  802. }
  803. void sta_info_stop(struct ieee80211_local *local)
  804. {
  805. del_timer_sync(&local->sta_cleanup);
  806. }
  807. int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
  808. {
  809. struct ieee80211_local *local = sdata->local;
  810. struct sta_info *sta, *tmp;
  811. LIST_HEAD(free_list);
  812. int ret = 0;
  813. might_sleep();
  814. WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
  815. WARN_ON(vlans && !sdata->bss);
  816. mutex_lock(&local->sta_mtx);
  817. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  818. if (sdata == sta->sdata ||
  819. (vlans && sdata->bss == sta->sdata->bss)) {
  820. if (!WARN_ON(__sta_info_destroy_part1(sta)))
  821. list_add(&sta->free_list, &free_list);
  822. ret++;
  823. }
  824. }
  825. if (!list_empty(&free_list)) {
  826. synchronize_net();
  827. list_for_each_entry_safe(sta, tmp, &free_list, free_list)
  828. __sta_info_destroy_part2(sta);
  829. }
  830. mutex_unlock(&local->sta_mtx);
  831. return ret;
  832. }
  833. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  834. unsigned long exp_time)
  835. {
  836. struct ieee80211_local *local = sdata->local;
  837. struct sta_info *sta, *tmp;
  838. mutex_lock(&local->sta_mtx);
  839. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  840. if (sdata != sta->sdata)
  841. continue;
  842. if (time_after(jiffies, sta->last_rx + exp_time)) {
  843. sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
  844. sta->sta.addr);
  845. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  846. test_sta_flag(sta, WLAN_STA_PS_STA))
  847. atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
  848. WARN_ON(__sta_info_destroy(sta));
  849. }
  850. }
  851. mutex_unlock(&local->sta_mtx);
  852. }
  853. struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
  854. const u8 *addr,
  855. const u8 *localaddr)
  856. {
  857. struct sta_info *sta, *nxt;
  858. /*
  859. * Just return a random station if localaddr is NULL
  860. * ... first in list.
  861. */
  862. for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
  863. if (localaddr &&
  864. !ether_addr_equal(sta->sdata->vif.addr, localaddr))
  865. continue;
  866. if (!sta->uploaded)
  867. return NULL;
  868. return &sta->sta;
  869. }
  870. return NULL;
  871. }
  872. EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
  873. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
  874. const u8 *addr)
  875. {
  876. struct sta_info *sta;
  877. if (!vif)
  878. return NULL;
  879. sta = sta_info_get_bss(vif_to_sdata(vif), addr);
  880. if (!sta)
  881. return NULL;
  882. if (!sta->uploaded)
  883. return NULL;
  884. return &sta->sta;
  885. }
  886. EXPORT_SYMBOL(ieee80211_find_sta);
  887. static void clear_sta_ps_flags(void *_sta)
  888. {
  889. struct sta_info *sta = _sta;
  890. struct ieee80211_sub_if_data *sdata = sta->sdata;
  891. struct ps_data *ps;
  892. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  893. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  894. ps = &sdata->bss->ps;
  895. else if (ieee80211_vif_is_mesh(&sdata->vif))
  896. ps = &sdata->u.mesh.ps;
  897. else
  898. return;
  899. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  900. if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA))
  901. atomic_dec(&ps->num_sta_ps);
  902. }
  903. /* powersave support code */
  904. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  905. {
  906. struct ieee80211_sub_if_data *sdata = sta->sdata;
  907. struct ieee80211_local *local = sdata->local;
  908. struct sk_buff_head pending;
  909. int filtered = 0, buffered = 0, ac;
  910. unsigned long flags;
  911. clear_sta_flag(sta, WLAN_STA_SP);
  912. BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
  913. sta->driver_buffered_tids = 0;
  914. if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
  915. drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
  916. skb_queue_head_init(&pending);
  917. /* Send all buffered frames to the station */
  918. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  919. int count = skb_queue_len(&pending), tmp;
  920. spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
  921. skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
  922. spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
  923. tmp = skb_queue_len(&pending);
  924. filtered += tmp - count;
  925. count = tmp;
  926. spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
  927. skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
  928. spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
  929. tmp = skb_queue_len(&pending);
  930. buffered += tmp - count;
  931. }
  932. ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
  933. /* This station just woke up and isn't aware of our SMPS state */
  934. if (!ieee80211_smps_is_restrictive(sta->known_smps_mode,
  935. sdata->smps_mode) &&
  936. sta->known_smps_mode != sdata->bss->req_smps &&
  937. sta_info_tx_streams(sta) != 1) {
  938. ht_dbg(sdata,
  939. "%pM just woke up and MIMO capable - update SMPS\n",
  940. sta->sta.addr);
  941. ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
  942. sta->sta.addr,
  943. sdata->vif.bss_conf.bssid);
  944. }
  945. local->total_ps_buffered -= buffered;
  946. sta_info_recalc_tim(sta);
  947. ps_dbg(sdata,
  948. "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
  949. sta->sta.addr, sta->sta.aid, filtered, buffered);
  950. }
  951. static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
  952. struct sta_info *sta, int tid,
  953. enum ieee80211_frame_release_type reason,
  954. bool call_driver)
  955. {
  956. struct ieee80211_local *local = sdata->local;
  957. struct ieee80211_qos_hdr *nullfunc;
  958. struct sk_buff *skb;
  959. int size = sizeof(*nullfunc);
  960. __le16 fc;
  961. bool qos = test_sta_flag(sta, WLAN_STA_WME);
  962. struct ieee80211_tx_info *info;
  963. struct ieee80211_chanctx_conf *chanctx_conf;
  964. if (qos) {
  965. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  966. IEEE80211_STYPE_QOS_NULLFUNC |
  967. IEEE80211_FCTL_FROMDS);
  968. } else {
  969. size -= 2;
  970. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  971. IEEE80211_STYPE_NULLFUNC |
  972. IEEE80211_FCTL_FROMDS);
  973. }
  974. skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
  975. if (!skb)
  976. return;
  977. skb_reserve(skb, local->hw.extra_tx_headroom);
  978. nullfunc = (void *) skb_put(skb, size);
  979. nullfunc->frame_control = fc;
  980. nullfunc->duration_id = 0;
  981. memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
  982. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  983. memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
  984. skb->priority = tid;
  985. skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
  986. if (qos) {
  987. nullfunc->qos_ctrl = cpu_to_le16(tid);
  988. if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
  989. nullfunc->qos_ctrl |=
  990. cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
  991. }
  992. info = IEEE80211_SKB_CB(skb);
  993. /*
  994. * Tell TX path to send this frame even though the
  995. * STA may still remain is PS mode after this frame
  996. * exchange. Also set EOSP to indicate this packet
  997. * ends the poll/service period.
  998. */
  999. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
  1000. IEEE80211_TX_CTL_PS_RESPONSE |
  1001. IEEE80211_TX_STATUS_EOSP |
  1002. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1003. if (call_driver)
  1004. drv_allow_buffered_frames(local, sta, BIT(tid), 1,
  1005. reason, false);
  1006. skb->dev = sdata->dev;
  1007. rcu_read_lock();
  1008. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  1009. if (WARN_ON(!chanctx_conf)) {
  1010. rcu_read_unlock();
  1011. kfree_skb(skb);
  1012. return;
  1013. }
  1014. ieee80211_xmit(sdata, skb, chanctx_conf->def.chan->band);
  1015. rcu_read_unlock();
  1016. }
  1017. static int find_highest_prio_tid(unsigned long tids)
  1018. {
  1019. /* lower 3 TIDs aren't ordered perfectly */
  1020. if (tids & 0xF8)
  1021. return fls(tids) - 1;
  1022. /* TID 0 is BE just like TID 3 */
  1023. if (tids & BIT(0))
  1024. return 0;
  1025. return fls(tids) - 1;
  1026. }
  1027. static void
  1028. ieee80211_sta_ps_deliver_response(struct sta_info *sta,
  1029. int n_frames, u8 ignored_acs,
  1030. enum ieee80211_frame_release_type reason)
  1031. {
  1032. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1033. struct ieee80211_local *local = sdata->local;
  1034. bool more_data = false;
  1035. int ac;
  1036. unsigned long driver_release_tids = 0;
  1037. struct sk_buff_head frames;
  1038. /* Service or PS-Poll period starts */
  1039. set_sta_flag(sta, WLAN_STA_SP);
  1040. __skb_queue_head_init(&frames);
  1041. /* Get response frame(s) and more data bit for the last one. */
  1042. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  1043. unsigned long tids;
  1044. if (ignored_acs & BIT(ac))
  1045. continue;
  1046. tids = ieee80211_tids_for_ac(ac);
  1047. /* if we already have frames from software, then we can't also
  1048. * release from hardware queues
  1049. */
  1050. if (skb_queue_empty(&frames))
  1051. driver_release_tids |= sta->driver_buffered_tids & tids;
  1052. if (driver_release_tids) {
  1053. /* If the driver has data on more than one TID then
  1054. * certainly there's more data if we release just a
  1055. * single frame now (from a single TID). This will
  1056. * only happen for PS-Poll.
  1057. */
  1058. if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
  1059. hweight16(driver_release_tids) > 1) {
  1060. more_data = true;
  1061. driver_release_tids =
  1062. BIT(find_highest_prio_tid(
  1063. driver_release_tids));
  1064. break;
  1065. }
  1066. } else {
  1067. struct sk_buff *skb;
  1068. while (n_frames > 0) {
  1069. skb = skb_dequeue(&sta->tx_filtered[ac]);
  1070. if (!skb) {
  1071. skb = skb_dequeue(
  1072. &sta->ps_tx_buf[ac]);
  1073. if (skb)
  1074. local->total_ps_buffered--;
  1075. }
  1076. if (!skb)
  1077. break;
  1078. n_frames--;
  1079. __skb_queue_tail(&frames, skb);
  1080. }
  1081. }
  1082. /* If we have more frames buffered on this AC, then set the
  1083. * more-data bit and abort the loop since we can't send more
  1084. * data from other ACs before the buffered frames from this.
  1085. */
  1086. if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
  1087. !skb_queue_empty(&sta->ps_tx_buf[ac])) {
  1088. more_data = true;
  1089. break;
  1090. }
  1091. }
  1092. if (skb_queue_empty(&frames) && !driver_release_tids) {
  1093. int tid;
  1094. /*
  1095. * For PS-Poll, this can only happen due to a race condition
  1096. * when we set the TIM bit and the station notices it, but
  1097. * before it can poll for the frame we expire it.
  1098. *
  1099. * For uAPSD, this is said in the standard (11.2.1.5 h):
  1100. * At each unscheduled SP for a non-AP STA, the AP shall
  1101. * attempt to transmit at least one MSDU or MMPDU, but no
  1102. * more than the value specified in the Max SP Length field
  1103. * in the QoS Capability element from delivery-enabled ACs,
  1104. * that are destined for the non-AP STA.
  1105. *
  1106. * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
  1107. */
  1108. /* This will evaluate to 1, 3, 5 or 7. */
  1109. tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
  1110. ieee80211_send_null_response(sdata, sta, tid, reason, true);
  1111. } else if (!driver_release_tids) {
  1112. struct sk_buff_head pending;
  1113. struct sk_buff *skb;
  1114. int num = 0;
  1115. u16 tids = 0;
  1116. bool need_null = false;
  1117. skb_queue_head_init(&pending);
  1118. while ((skb = __skb_dequeue(&frames))) {
  1119. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1120. struct ieee80211_hdr *hdr = (void *) skb->data;
  1121. u8 *qoshdr = NULL;
  1122. num++;
  1123. /*
  1124. * Tell TX path to send this frame even though the
  1125. * STA may still remain is PS mode after this frame
  1126. * exchange.
  1127. */
  1128. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
  1129. IEEE80211_TX_CTL_PS_RESPONSE;
  1130. /*
  1131. * Use MoreData flag to indicate whether there are
  1132. * more buffered frames for this STA
  1133. */
  1134. if (more_data || !skb_queue_empty(&frames))
  1135. hdr->frame_control |=
  1136. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1137. else
  1138. hdr->frame_control &=
  1139. cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  1140. if (ieee80211_is_data_qos(hdr->frame_control) ||
  1141. ieee80211_is_qos_nullfunc(hdr->frame_control))
  1142. qoshdr = ieee80211_get_qos_ctl(hdr);
  1143. tids |= BIT(skb->priority);
  1144. __skb_queue_tail(&pending, skb);
  1145. /* end service period after last frame or add one */
  1146. if (!skb_queue_empty(&frames))
  1147. continue;
  1148. if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
  1149. /* for PS-Poll, there's only one frame */
  1150. info->flags |= IEEE80211_TX_STATUS_EOSP |
  1151. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1152. break;
  1153. }
  1154. /* For uAPSD, things are a bit more complicated. If the
  1155. * last frame has a QoS header (i.e. is a QoS-data or
  1156. * QoS-nulldata frame) then just set the EOSP bit there
  1157. * and be done.
  1158. * If the frame doesn't have a QoS header (which means
  1159. * it should be a bufferable MMPDU) then we can't set
  1160. * the EOSP bit in the QoS header; add a QoS-nulldata
  1161. * frame to the list to send it after the MMPDU.
  1162. *
  1163. * Note that this code is only in the mac80211-release
  1164. * code path, we assume that the driver will not buffer
  1165. * anything but QoS-data frames, or if it does, will
  1166. * create the QoS-nulldata frame by itself if needed.
  1167. *
  1168. * Cf. 802.11-2012 10.2.1.10 (c).
  1169. */
  1170. if (qoshdr) {
  1171. *qoshdr |= IEEE80211_QOS_CTL_EOSP;
  1172. info->flags |= IEEE80211_TX_STATUS_EOSP |
  1173. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1174. } else {
  1175. /* The standard isn't completely clear on this
  1176. * as it says the more-data bit should be set
  1177. * if there are more BUs. The QoS-Null frame
  1178. * we're about to send isn't buffered yet, we
  1179. * only create it below, but let's pretend it
  1180. * was buffered just in case some clients only
  1181. * expect more-data=0 when eosp=1.
  1182. */
  1183. hdr->frame_control |=
  1184. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1185. need_null = true;
  1186. num++;
  1187. }
  1188. break;
  1189. }
  1190. drv_allow_buffered_frames(local, sta, tids, num,
  1191. reason, more_data);
  1192. ieee80211_add_pending_skbs(local, &pending);
  1193. if (need_null)
  1194. ieee80211_send_null_response(
  1195. sdata, sta, find_highest_prio_tid(tids),
  1196. reason, false);
  1197. sta_info_recalc_tim(sta);
  1198. } else {
  1199. /*
  1200. * We need to release a frame that is buffered somewhere in the
  1201. * driver ... it'll have to handle that.
  1202. * Note that the driver also has to check the number of frames
  1203. * on the TIDs we're releasing from - if there are more than
  1204. * n_frames it has to set the more-data bit (if we didn't ask
  1205. * it to set it anyway due to other buffered frames); if there
  1206. * are fewer than n_frames it has to make sure to adjust that
  1207. * to allow the service period to end properly.
  1208. */
  1209. drv_release_buffered_frames(local, sta, driver_release_tids,
  1210. n_frames, reason, more_data);
  1211. /*
  1212. * Note that we don't recalculate the TIM bit here as it would
  1213. * most likely have no effect at all unless the driver told us
  1214. * that the TID(s) became empty before returning here from the
  1215. * release function.
  1216. * Either way, however, when the driver tells us that the TID(s)
  1217. * became empty we'll do the TIM recalculation.
  1218. */
  1219. }
  1220. }
  1221. void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
  1222. {
  1223. u8 ignore_for_response = sta->sta.uapsd_queues;
  1224. /*
  1225. * If all ACs are delivery-enabled then we should reply
  1226. * from any of them, if only some are enabled we reply
  1227. * only from the non-enabled ones.
  1228. */
  1229. if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
  1230. ignore_for_response = 0;
  1231. ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
  1232. IEEE80211_FRAME_RELEASE_PSPOLL);
  1233. }
  1234. void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
  1235. {
  1236. int n_frames = sta->sta.max_sp;
  1237. u8 delivery_enabled = sta->sta.uapsd_queues;
  1238. /*
  1239. * If we ever grow support for TSPEC this might happen if
  1240. * the TSPEC update from hostapd comes in between a trigger
  1241. * frame setting WLAN_STA_UAPSD in the RX path and this
  1242. * actually getting called.
  1243. */
  1244. if (!delivery_enabled)
  1245. return;
  1246. switch (sta->sta.max_sp) {
  1247. case 1:
  1248. n_frames = 2;
  1249. break;
  1250. case 2:
  1251. n_frames = 4;
  1252. break;
  1253. case 3:
  1254. n_frames = 6;
  1255. break;
  1256. case 0:
  1257. /* XXX: what is a good value? */
  1258. n_frames = 8;
  1259. break;
  1260. }
  1261. ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
  1262. IEEE80211_FRAME_RELEASE_UAPSD);
  1263. }
  1264. void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
  1265. struct ieee80211_sta *pubsta, bool block)
  1266. {
  1267. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1268. trace_api_sta_block_awake(sta->local, pubsta, block);
  1269. if (block)
  1270. set_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1271. else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
  1272. ieee80211_queue_work(hw, &sta->drv_unblock_wk);
  1273. }
  1274. EXPORT_SYMBOL(ieee80211_sta_block_awake);
  1275. void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
  1276. {
  1277. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1278. struct ieee80211_local *local = sta->local;
  1279. trace_api_eosp(local, pubsta);
  1280. clear_sta_flag(sta, WLAN_STA_SP);
  1281. }
  1282. EXPORT_SYMBOL(ieee80211_sta_eosp);
  1283. void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
  1284. u8 tid, bool buffered)
  1285. {
  1286. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1287. if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
  1288. return;
  1289. trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
  1290. if (buffered)
  1291. set_bit(tid, &sta->driver_buffered_tids);
  1292. else
  1293. clear_bit(tid, &sta->driver_buffered_tids);
  1294. sta_info_recalc_tim(sta);
  1295. }
  1296. EXPORT_SYMBOL(ieee80211_sta_set_buffered);
  1297. int sta_info_move_state(struct sta_info *sta,
  1298. enum ieee80211_sta_state new_state)
  1299. {
  1300. might_sleep();
  1301. if (sta->sta_state == new_state)
  1302. return 0;
  1303. /* check allowed transitions first */
  1304. switch (new_state) {
  1305. case IEEE80211_STA_NONE:
  1306. if (sta->sta_state != IEEE80211_STA_AUTH)
  1307. return -EINVAL;
  1308. break;
  1309. case IEEE80211_STA_AUTH:
  1310. if (sta->sta_state != IEEE80211_STA_NONE &&
  1311. sta->sta_state != IEEE80211_STA_ASSOC)
  1312. return -EINVAL;
  1313. break;
  1314. case IEEE80211_STA_ASSOC:
  1315. if (sta->sta_state != IEEE80211_STA_AUTH &&
  1316. sta->sta_state != IEEE80211_STA_AUTHORIZED)
  1317. return -EINVAL;
  1318. break;
  1319. case IEEE80211_STA_AUTHORIZED:
  1320. if (sta->sta_state != IEEE80211_STA_ASSOC)
  1321. return -EINVAL;
  1322. break;
  1323. default:
  1324. WARN(1, "invalid state %d", new_state);
  1325. return -EINVAL;
  1326. }
  1327. sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
  1328. sta->sta.addr, new_state);
  1329. /*
  1330. * notify the driver before the actual changes so it can
  1331. * fail the transition
  1332. */
  1333. if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
  1334. int err = drv_sta_state(sta->local, sta->sdata, sta,
  1335. sta->sta_state, new_state);
  1336. if (err)
  1337. return err;
  1338. }
  1339. /* reflect the change in all state variables */
  1340. switch (new_state) {
  1341. case IEEE80211_STA_NONE:
  1342. if (sta->sta_state == IEEE80211_STA_AUTH)
  1343. clear_bit(WLAN_STA_AUTH, &sta->_flags);
  1344. break;
  1345. case IEEE80211_STA_AUTH:
  1346. if (sta->sta_state == IEEE80211_STA_NONE)
  1347. set_bit(WLAN_STA_AUTH, &sta->_flags);
  1348. else if (sta->sta_state == IEEE80211_STA_ASSOC)
  1349. clear_bit(WLAN_STA_ASSOC, &sta->_flags);
  1350. break;
  1351. case IEEE80211_STA_ASSOC:
  1352. if (sta->sta_state == IEEE80211_STA_AUTH) {
  1353. set_bit(WLAN_STA_ASSOC, &sta->_flags);
  1354. } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
  1355. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  1356. (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1357. !sta->sdata->u.vlan.sta))
  1358. atomic_dec(&sta->sdata->bss->num_mcast_sta);
  1359. clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1360. }
  1361. break;
  1362. case IEEE80211_STA_AUTHORIZED:
  1363. if (sta->sta_state == IEEE80211_STA_ASSOC) {
  1364. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  1365. (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1366. !sta->sdata->u.vlan.sta))
  1367. atomic_inc(&sta->sdata->bss->num_mcast_sta);
  1368. set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1369. }
  1370. break;
  1371. default:
  1372. break;
  1373. }
  1374. sta->sta_state = new_state;
  1375. return 0;
  1376. }
  1377. u8 sta_info_tx_streams(struct sta_info *sta)
  1378. {
  1379. struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
  1380. u8 rx_streams;
  1381. if (!sta->sta.ht_cap.ht_supported)
  1382. return 1;
  1383. if (sta->sta.vht_cap.vht_supported) {
  1384. int i;
  1385. u16 tx_mcs_map =
  1386. le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
  1387. for (i = 7; i >= 0; i--)
  1388. if ((tx_mcs_map & (0x3 << (i * 2))) !=
  1389. IEEE80211_VHT_MCS_NOT_SUPPORTED)
  1390. return i + 1;
  1391. }
  1392. if (ht_cap->mcs.rx_mask[3])
  1393. rx_streams = 4;
  1394. else if (ht_cap->mcs.rx_mask[2])
  1395. rx_streams = 3;
  1396. else if (ht_cap->mcs.rx_mask[1])
  1397. rx_streams = 2;
  1398. else
  1399. rx_streams = 1;
  1400. if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
  1401. return rx_streams;
  1402. return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
  1403. >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
  1404. }