sta_info.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  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. /*
  94. * At this point, when being called as call_rcu callback,
  95. * neither mac80211 nor the driver can reference this
  96. * sta struct any more except by still existing timers
  97. * associated with this station that we clean up below.
  98. *
  99. * Note though that this still uses the sdata and even
  100. * calls the driver in AP and mesh mode, so interfaces
  101. * of those types mush use call sta_info_flush_cleanup()
  102. * (typically via sta_info_flush()) before deconfiguring
  103. * the driver.
  104. *
  105. * In station mode, nothing happens here so it doesn't
  106. * have to (and doesn't) do that, this is intentional to
  107. * speed up roaming.
  108. */
  109. if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
  110. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  111. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  112. ps = &sdata->bss->ps;
  113. else
  114. return;
  115. clear_sta_flag(sta, WLAN_STA_PS_STA);
  116. atomic_dec(&ps->num_sta_ps);
  117. sta_info_recalc_tim(sta);
  118. }
  119. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  120. local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
  121. ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
  122. ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
  123. }
  124. #ifdef CONFIG_MAC80211_MESH
  125. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  126. mesh_accept_plinks_update(sdata);
  127. mesh_plink_deactivate(sta);
  128. del_timer_sync(&sta->plink_timer);
  129. }
  130. #endif
  131. cancel_work_sync(&sta->drv_unblock_wk);
  132. /*
  133. * Destroy aggregation state here. It would be nice to wait for the
  134. * driver to finish aggregation stop and then clean up, but for now
  135. * drivers have to handle aggregation stop being requested, followed
  136. * directly by station destruction.
  137. */
  138. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  139. tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
  140. if (!tid_tx)
  141. continue;
  142. ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
  143. kfree(tid_tx);
  144. }
  145. sta_info_free(local, sta);
  146. }
  147. void ieee80211_cleanup_sdata_stas(struct ieee80211_sub_if_data *sdata)
  148. {
  149. struct sta_info *sta;
  150. spin_lock_bh(&sdata->cleanup_stations_lock);
  151. while (!list_empty(&sdata->cleanup_stations)) {
  152. sta = list_first_entry(&sdata->cleanup_stations,
  153. struct sta_info, list);
  154. list_del(&sta->list);
  155. spin_unlock_bh(&sdata->cleanup_stations_lock);
  156. cleanup_single_sta(sta);
  157. spin_lock_bh(&sdata->cleanup_stations_lock);
  158. }
  159. spin_unlock_bh(&sdata->cleanup_stations_lock);
  160. }
  161. static void free_sta_rcu(struct rcu_head *h)
  162. {
  163. struct sta_info *sta = container_of(h, struct sta_info, rcu_head);
  164. struct ieee80211_sub_if_data *sdata = sta->sdata;
  165. spin_lock(&sdata->cleanup_stations_lock);
  166. list_add_tail(&sta->list, &sdata->cleanup_stations);
  167. spin_unlock(&sdata->cleanup_stations_lock);
  168. ieee80211_queue_work(&sdata->local->hw, &sdata->cleanup_stations_wk);
  169. }
  170. /* protected by RCU */
  171. struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
  172. const u8 *addr)
  173. {
  174. struct ieee80211_local *local = sdata->local;
  175. struct sta_info *sta;
  176. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  177. lockdep_is_held(&local->sta_mtx));
  178. while (sta) {
  179. if (sta->sdata == sdata &&
  180. ether_addr_equal(sta->sta.addr, addr))
  181. break;
  182. sta = rcu_dereference_check(sta->hnext,
  183. lockdep_is_held(&local->sta_mtx));
  184. }
  185. return sta;
  186. }
  187. /*
  188. * Get sta info either from the specified interface
  189. * or from one of its vlans
  190. */
  191. struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
  192. const u8 *addr)
  193. {
  194. struct ieee80211_local *local = sdata->local;
  195. struct sta_info *sta;
  196. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  197. lockdep_is_held(&local->sta_mtx));
  198. while (sta) {
  199. if ((sta->sdata == sdata ||
  200. (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
  201. ether_addr_equal(sta->sta.addr, addr))
  202. break;
  203. sta = rcu_dereference_check(sta->hnext,
  204. lockdep_is_held(&local->sta_mtx));
  205. }
  206. return sta;
  207. }
  208. struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
  209. int idx)
  210. {
  211. struct ieee80211_local *local = sdata->local;
  212. struct sta_info *sta;
  213. int i = 0;
  214. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  215. if (sdata != sta->sdata)
  216. continue;
  217. if (i < idx) {
  218. ++i;
  219. continue;
  220. }
  221. return sta;
  222. }
  223. return NULL;
  224. }
  225. /**
  226. * sta_info_free - free STA
  227. *
  228. * @local: pointer to the global information
  229. * @sta: STA info to free
  230. *
  231. * This function must undo everything done by sta_info_alloc()
  232. * that may happen before sta_info_insert(). It may only be
  233. * called when sta_info_insert() has not been attempted (and
  234. * if that fails, the station is freed anyway.)
  235. */
  236. void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
  237. {
  238. if (sta->rate_ctrl)
  239. rate_control_free_sta(sta);
  240. sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
  241. kfree(sta);
  242. }
  243. /* Caller must hold local->sta_mtx */
  244. static void sta_info_hash_add(struct ieee80211_local *local,
  245. struct sta_info *sta)
  246. {
  247. lockdep_assert_held(&local->sta_mtx);
  248. sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
  249. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
  250. }
  251. static void sta_unblock(struct work_struct *wk)
  252. {
  253. struct sta_info *sta;
  254. sta = container_of(wk, struct sta_info, drv_unblock_wk);
  255. if (sta->dead)
  256. return;
  257. if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
  258. local_bh_disable();
  259. ieee80211_sta_ps_deliver_wakeup(sta);
  260. local_bh_enable();
  261. } else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
  262. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  263. local_bh_disable();
  264. ieee80211_sta_ps_deliver_poll_response(sta);
  265. local_bh_enable();
  266. } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
  267. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  268. local_bh_disable();
  269. ieee80211_sta_ps_deliver_uapsd(sta);
  270. local_bh_enable();
  271. } else
  272. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  273. }
  274. static int sta_prepare_rate_control(struct ieee80211_local *local,
  275. struct sta_info *sta, gfp_t gfp)
  276. {
  277. if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
  278. return 0;
  279. sta->rate_ctrl = local->rate_ctrl;
  280. sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
  281. &sta->sta, gfp);
  282. if (!sta->rate_ctrl_priv)
  283. return -ENOMEM;
  284. return 0;
  285. }
  286. struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
  287. const u8 *addr, gfp_t gfp)
  288. {
  289. struct ieee80211_local *local = sdata->local;
  290. struct sta_info *sta;
  291. struct timespec uptime;
  292. int i;
  293. sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
  294. if (!sta)
  295. return NULL;
  296. spin_lock_init(&sta->lock);
  297. INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
  298. INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
  299. mutex_init(&sta->ampdu_mlme.mtx);
  300. memcpy(sta->sta.addr, addr, ETH_ALEN);
  301. sta->local = local;
  302. sta->sdata = sdata;
  303. sta->last_rx = jiffies;
  304. sta->sta_state = IEEE80211_STA_NONE;
  305. do_posix_clock_monotonic_gettime(&uptime);
  306. sta->last_connected = uptime.tv_sec;
  307. ewma_init(&sta->avg_signal, 1024, 8);
  308. if (sta_prepare_rate_control(local, sta, gfp)) {
  309. kfree(sta);
  310. return NULL;
  311. }
  312. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  313. /*
  314. * timer_to_tid must be initialized with identity mapping
  315. * to enable session_timer's data differentiation. See
  316. * sta_rx_agg_session_timer_expired for usage.
  317. */
  318. sta->timer_to_tid[i] = i;
  319. }
  320. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  321. skb_queue_head_init(&sta->ps_tx_buf[i]);
  322. skb_queue_head_init(&sta->tx_filtered[i]);
  323. }
  324. for (i = 0; i < IEEE80211_NUM_TIDS; i++)
  325. sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
  326. sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
  327. #ifdef CONFIG_MAC80211_MESH
  328. sta->plink_state = NL80211_PLINK_LISTEN;
  329. init_timer(&sta->plink_timer);
  330. #endif
  331. return sta;
  332. }
  333. static int sta_info_insert_check(struct sta_info *sta)
  334. {
  335. struct ieee80211_sub_if_data *sdata = sta->sdata;
  336. /*
  337. * Can't be a WARN_ON because it can be triggered through a race:
  338. * something inserts a STA (on one CPU) without holding the RTNL
  339. * and another CPU turns off the net device.
  340. */
  341. if (unlikely(!ieee80211_sdata_running(sdata)))
  342. return -ENETDOWN;
  343. if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
  344. is_multicast_ether_addr(sta->sta.addr)))
  345. return -EINVAL;
  346. return 0;
  347. }
  348. static int sta_info_insert_drv_state(struct ieee80211_local *local,
  349. struct ieee80211_sub_if_data *sdata,
  350. struct sta_info *sta)
  351. {
  352. enum ieee80211_sta_state state;
  353. int err = 0;
  354. for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
  355. err = drv_sta_state(local, sdata, sta, state, state + 1);
  356. if (err)
  357. break;
  358. }
  359. if (!err) {
  360. /*
  361. * Drivers using legacy sta_add/sta_remove callbacks only
  362. * get uploaded set to true after sta_add is called.
  363. */
  364. if (!local->ops->sta_add)
  365. sta->uploaded = true;
  366. return 0;
  367. }
  368. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  369. sdata_info(sdata,
  370. "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
  371. sta->sta.addr, state + 1, err);
  372. err = 0;
  373. }
  374. /* unwind on error */
  375. for (; state > IEEE80211_STA_NOTEXIST; state--)
  376. WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
  377. return err;
  378. }
  379. /*
  380. * should be called with sta_mtx locked
  381. * this function replaces the mutex lock
  382. * with a RCU lock
  383. */
  384. static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
  385. {
  386. struct ieee80211_local *local = sta->local;
  387. struct ieee80211_sub_if_data *sdata = sta->sdata;
  388. struct station_info sinfo;
  389. int err = 0;
  390. lockdep_assert_held(&local->sta_mtx);
  391. /* check if STA exists already */
  392. if (sta_info_get_bss(sdata, sta->sta.addr)) {
  393. err = -EEXIST;
  394. goto out_err;
  395. }
  396. /* notify driver */
  397. err = sta_info_insert_drv_state(local, sdata, sta);
  398. if (err)
  399. goto out_err;
  400. local->num_sta++;
  401. local->sta_generation++;
  402. smp_mb();
  403. /* make the station visible */
  404. sta_info_hash_add(local, sta);
  405. list_add_rcu(&sta->list, &local->sta_list);
  406. set_sta_flag(sta, WLAN_STA_INSERTED);
  407. ieee80211_sta_debugfs_add(sta);
  408. rate_control_add_sta_debugfs(sta);
  409. memset(&sinfo, 0, sizeof(sinfo));
  410. sinfo.filled = 0;
  411. sinfo.generation = local->sta_generation;
  412. cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
  413. sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
  414. /* move reference to rcu-protected */
  415. rcu_read_lock();
  416. mutex_unlock(&local->sta_mtx);
  417. if (ieee80211_vif_is_mesh(&sdata->vif))
  418. mesh_accept_plinks_update(sdata);
  419. return 0;
  420. out_err:
  421. mutex_unlock(&local->sta_mtx);
  422. rcu_read_lock();
  423. return err;
  424. }
  425. int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
  426. {
  427. struct ieee80211_local *local = sta->local;
  428. int err = 0;
  429. might_sleep();
  430. err = sta_info_insert_check(sta);
  431. if (err) {
  432. rcu_read_lock();
  433. goto out_free;
  434. }
  435. mutex_lock(&local->sta_mtx);
  436. err = sta_info_insert_finish(sta);
  437. if (err)
  438. goto out_free;
  439. return 0;
  440. out_free:
  441. BUG_ON(!err);
  442. sta_info_free(local, sta);
  443. return err;
  444. }
  445. int sta_info_insert(struct sta_info *sta)
  446. {
  447. int err = sta_info_insert_rcu(sta);
  448. rcu_read_unlock();
  449. return err;
  450. }
  451. static inline void __bss_tim_set(u8 *tim, u16 id)
  452. {
  453. /*
  454. * This format has been mandated by the IEEE specifications,
  455. * so this line may not be changed to use the __set_bit() format.
  456. */
  457. tim[id / 8] |= (1 << (id % 8));
  458. }
  459. static inline void __bss_tim_clear(u8 *tim, u16 id)
  460. {
  461. /*
  462. * This format has been mandated by the IEEE specifications,
  463. * so this line may not be changed to use the __clear_bit() format.
  464. */
  465. tim[id / 8] &= ~(1 << (id % 8));
  466. }
  467. static unsigned long ieee80211_tids_for_ac(int ac)
  468. {
  469. /* If we ever support TIDs > 7, this obviously needs to be adjusted */
  470. switch (ac) {
  471. case IEEE80211_AC_VO:
  472. return BIT(6) | BIT(7);
  473. case IEEE80211_AC_VI:
  474. return BIT(4) | BIT(5);
  475. case IEEE80211_AC_BE:
  476. return BIT(0) | BIT(3);
  477. case IEEE80211_AC_BK:
  478. return BIT(1) | BIT(2);
  479. default:
  480. WARN_ON(1);
  481. return 0;
  482. }
  483. }
  484. void sta_info_recalc_tim(struct sta_info *sta)
  485. {
  486. struct ieee80211_local *local = sta->local;
  487. struct ps_data *ps;
  488. unsigned long flags;
  489. bool indicate_tim = false;
  490. u8 ignore_for_tim = sta->sta.uapsd_queues;
  491. int ac;
  492. u16 id;
  493. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  494. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  495. if (WARN_ON_ONCE(!sta->sdata->bss))
  496. return;
  497. ps = &sta->sdata->bss->ps;
  498. id = sta->sta.aid;
  499. } else {
  500. return;
  501. }
  502. /* No need to do anything if the driver does all */
  503. if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
  504. return;
  505. if (sta->dead)
  506. goto done;
  507. /*
  508. * If all ACs are delivery-enabled then we should build
  509. * the TIM bit for all ACs anyway; if only some are then
  510. * we ignore those and build the TIM bit using only the
  511. * non-enabled ones.
  512. */
  513. if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
  514. ignore_for_tim = 0;
  515. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  516. unsigned long tids;
  517. if (ignore_for_tim & BIT(ac))
  518. continue;
  519. indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
  520. !skb_queue_empty(&sta->ps_tx_buf[ac]);
  521. if (indicate_tim)
  522. break;
  523. tids = ieee80211_tids_for_ac(ac);
  524. indicate_tim |=
  525. sta->driver_buffered_tids & tids;
  526. }
  527. done:
  528. spin_lock_irqsave(&local->tim_lock, flags);
  529. if (indicate_tim)
  530. __bss_tim_set(ps->tim, id);
  531. else
  532. __bss_tim_clear(ps->tim, id);
  533. if (local->ops->set_tim) {
  534. local->tim_in_locked_section = true;
  535. drv_set_tim(local, &sta->sta, indicate_tim);
  536. local->tim_in_locked_section = false;
  537. }
  538. spin_unlock_irqrestore(&local->tim_lock, flags);
  539. }
  540. static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
  541. {
  542. struct ieee80211_tx_info *info;
  543. int timeout;
  544. if (!skb)
  545. return false;
  546. info = IEEE80211_SKB_CB(skb);
  547. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  548. timeout = (sta->listen_interval *
  549. sta->sdata->vif.bss_conf.beacon_int *
  550. 32 / 15625) * HZ;
  551. if (timeout < STA_TX_BUFFER_EXPIRE)
  552. timeout = STA_TX_BUFFER_EXPIRE;
  553. return time_after(jiffies, info->control.jiffies + timeout);
  554. }
  555. static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
  556. struct sta_info *sta, int ac)
  557. {
  558. unsigned long flags;
  559. struct sk_buff *skb;
  560. /*
  561. * First check for frames that should expire on the filtered
  562. * queue. Frames here were rejected by the driver and are on
  563. * a separate queue to avoid reordering with normal PS-buffered
  564. * frames. They also aren't accounted for right now in the
  565. * total_ps_buffered counter.
  566. */
  567. for (;;) {
  568. spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
  569. skb = skb_peek(&sta->tx_filtered[ac]);
  570. if (sta_info_buffer_expired(sta, skb))
  571. skb = __skb_dequeue(&sta->tx_filtered[ac]);
  572. else
  573. skb = NULL;
  574. spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
  575. /*
  576. * Frames are queued in order, so if this one
  577. * hasn't expired yet we can stop testing. If
  578. * we actually reached the end of the queue we
  579. * also need to stop, of course.
  580. */
  581. if (!skb)
  582. break;
  583. ieee80211_free_txskb(&local->hw, skb);
  584. }
  585. /*
  586. * Now also check the normal PS-buffered queue, this will
  587. * only find something if the filtered queue was emptied
  588. * since the filtered frames are all before the normal PS
  589. * buffered frames.
  590. */
  591. for (;;) {
  592. spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
  593. skb = skb_peek(&sta->ps_tx_buf[ac]);
  594. if (sta_info_buffer_expired(sta, skb))
  595. skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
  596. else
  597. skb = NULL;
  598. spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
  599. /*
  600. * frames are queued in order, so if this one
  601. * hasn't expired yet (or we reached the end of
  602. * the queue) we can stop testing
  603. */
  604. if (!skb)
  605. break;
  606. local->total_ps_buffered--;
  607. ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
  608. sta->sta.addr);
  609. ieee80211_free_txskb(&local->hw, skb);
  610. }
  611. /*
  612. * Finally, recalculate the TIM bit for this station -- it might
  613. * now be clear because the station was too slow to retrieve its
  614. * frames.
  615. */
  616. sta_info_recalc_tim(sta);
  617. /*
  618. * Return whether there are any frames still buffered, this is
  619. * used to check whether the cleanup timer still needs to run,
  620. * if there are no frames we don't need to rearm the timer.
  621. */
  622. return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
  623. skb_queue_empty(&sta->tx_filtered[ac]));
  624. }
  625. static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  626. struct sta_info *sta)
  627. {
  628. bool have_buffered = false;
  629. int ac;
  630. /* This is only necessary for stations on BSS interfaces */
  631. if (!sta->sdata->bss)
  632. return false;
  633. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  634. have_buffered |=
  635. sta_info_cleanup_expire_buffered_ac(local, sta, ac);
  636. return have_buffered;
  637. }
  638. int __must_check __sta_info_destroy(struct sta_info *sta)
  639. {
  640. struct ieee80211_local *local;
  641. struct ieee80211_sub_if_data *sdata;
  642. int ret, i;
  643. might_sleep();
  644. if (!sta)
  645. return -ENOENT;
  646. local = sta->local;
  647. sdata = sta->sdata;
  648. lockdep_assert_held(&local->sta_mtx);
  649. /*
  650. * Before removing the station from the driver and
  651. * rate control, it might still start new aggregation
  652. * sessions -- block that to make sure the tear-down
  653. * will be sufficient.
  654. */
  655. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  656. ieee80211_sta_tear_down_BA_sessions(sta, false);
  657. ret = sta_info_hash_del(local, sta);
  658. if (ret)
  659. return ret;
  660. list_del_rcu(&sta->list);
  661. mutex_lock(&local->key_mtx);
  662. for (i = 0; i < NUM_DEFAULT_KEYS; i++)
  663. __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
  664. if (sta->ptk)
  665. __ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
  666. mutex_unlock(&local->key_mtx);
  667. sta->dead = true;
  668. local->num_sta--;
  669. local->sta_generation++;
  670. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  671. RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
  672. while (sta->sta_state > IEEE80211_STA_NONE) {
  673. ret = sta_info_move_state(sta, sta->sta_state - 1);
  674. if (ret) {
  675. WARN_ON_ONCE(1);
  676. break;
  677. }
  678. }
  679. if (sta->uploaded) {
  680. ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
  681. IEEE80211_STA_NOTEXIST);
  682. WARN_ON_ONCE(ret != 0);
  683. }
  684. sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
  685. cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
  686. rate_control_remove_sta_debugfs(sta);
  687. ieee80211_sta_debugfs_remove(sta);
  688. call_rcu(&sta->rcu_head, free_sta_rcu);
  689. return 0;
  690. }
  691. int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
  692. {
  693. struct sta_info *sta;
  694. int ret;
  695. mutex_lock(&sdata->local->sta_mtx);
  696. sta = sta_info_get(sdata, addr);
  697. ret = __sta_info_destroy(sta);
  698. mutex_unlock(&sdata->local->sta_mtx);
  699. return ret;
  700. }
  701. int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
  702. const u8 *addr)
  703. {
  704. struct sta_info *sta;
  705. int ret;
  706. mutex_lock(&sdata->local->sta_mtx);
  707. sta = sta_info_get_bss(sdata, addr);
  708. ret = __sta_info_destroy(sta);
  709. mutex_unlock(&sdata->local->sta_mtx);
  710. return ret;
  711. }
  712. static void sta_info_cleanup(unsigned long data)
  713. {
  714. struct ieee80211_local *local = (struct ieee80211_local *) data;
  715. struct sta_info *sta;
  716. bool timer_needed = false;
  717. rcu_read_lock();
  718. list_for_each_entry_rcu(sta, &local->sta_list, list)
  719. if (sta_info_cleanup_expire_buffered(local, sta))
  720. timer_needed = true;
  721. rcu_read_unlock();
  722. if (local->quiescing)
  723. return;
  724. if (!timer_needed)
  725. return;
  726. mod_timer(&local->sta_cleanup,
  727. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
  728. }
  729. void sta_info_init(struct ieee80211_local *local)
  730. {
  731. spin_lock_init(&local->tim_lock);
  732. mutex_init(&local->sta_mtx);
  733. INIT_LIST_HEAD(&local->sta_list);
  734. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  735. (unsigned long)local);
  736. }
  737. void sta_info_stop(struct ieee80211_local *local)
  738. {
  739. del_timer_sync(&local->sta_cleanup);
  740. }
  741. int sta_info_flush_defer(struct ieee80211_sub_if_data *sdata)
  742. {
  743. struct ieee80211_local *local = sdata->local;
  744. struct sta_info *sta, *tmp;
  745. int ret = 0;
  746. might_sleep();
  747. mutex_lock(&local->sta_mtx);
  748. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  749. if (sdata == sta->sdata) {
  750. WARN_ON(__sta_info_destroy(sta));
  751. ret++;
  752. }
  753. }
  754. mutex_unlock(&local->sta_mtx);
  755. return ret;
  756. }
  757. void sta_info_flush_cleanup(struct ieee80211_sub_if_data *sdata)
  758. {
  759. rcu_barrier();
  760. ieee80211_cleanup_sdata_stas(sdata);
  761. cancel_work_sync(&sdata->cleanup_stations_wk);
  762. }
  763. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  764. unsigned long exp_time)
  765. {
  766. struct ieee80211_local *local = sdata->local;
  767. struct sta_info *sta, *tmp;
  768. mutex_lock(&local->sta_mtx);
  769. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  770. if (sdata != sta->sdata)
  771. continue;
  772. if (time_after(jiffies, sta->last_rx + exp_time)) {
  773. sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
  774. sta->sta.addr);
  775. WARN_ON(__sta_info_destroy(sta));
  776. }
  777. }
  778. mutex_unlock(&local->sta_mtx);
  779. }
  780. struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
  781. const u8 *addr,
  782. const u8 *localaddr)
  783. {
  784. struct sta_info *sta, *nxt;
  785. /*
  786. * Just return a random station if localaddr is NULL
  787. * ... first in list.
  788. */
  789. for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
  790. if (localaddr &&
  791. !ether_addr_equal(sta->sdata->vif.addr, localaddr))
  792. continue;
  793. if (!sta->uploaded)
  794. return NULL;
  795. return &sta->sta;
  796. }
  797. return NULL;
  798. }
  799. EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
  800. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
  801. const u8 *addr)
  802. {
  803. struct sta_info *sta;
  804. if (!vif)
  805. return NULL;
  806. sta = sta_info_get_bss(vif_to_sdata(vif), addr);
  807. if (!sta)
  808. return NULL;
  809. if (!sta->uploaded)
  810. return NULL;
  811. return &sta->sta;
  812. }
  813. EXPORT_SYMBOL(ieee80211_find_sta);
  814. static void clear_sta_ps_flags(void *_sta)
  815. {
  816. struct sta_info *sta = _sta;
  817. struct ieee80211_sub_if_data *sdata = sta->sdata;
  818. struct ps_data *ps;
  819. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  820. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  821. ps = &sdata->bss->ps;
  822. else
  823. return;
  824. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  825. if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA))
  826. atomic_dec(&ps->num_sta_ps);
  827. }
  828. /* powersave support code */
  829. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  830. {
  831. struct ieee80211_sub_if_data *sdata = sta->sdata;
  832. struct ieee80211_local *local = sdata->local;
  833. struct sk_buff_head pending;
  834. int filtered = 0, buffered = 0, ac;
  835. unsigned long flags;
  836. clear_sta_flag(sta, WLAN_STA_SP);
  837. BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
  838. sta->driver_buffered_tids = 0;
  839. if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
  840. drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
  841. skb_queue_head_init(&pending);
  842. /* Send all buffered frames to the station */
  843. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  844. int count = skb_queue_len(&pending), tmp;
  845. spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
  846. skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
  847. spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
  848. tmp = skb_queue_len(&pending);
  849. filtered += tmp - count;
  850. count = tmp;
  851. spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
  852. skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
  853. spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
  854. tmp = skb_queue_len(&pending);
  855. buffered += tmp - count;
  856. }
  857. ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
  858. local->total_ps_buffered -= buffered;
  859. sta_info_recalc_tim(sta);
  860. ps_dbg(sdata,
  861. "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
  862. sta->sta.addr, sta->sta.aid, filtered, buffered);
  863. }
  864. static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
  865. struct sta_info *sta, int tid,
  866. enum ieee80211_frame_release_type reason)
  867. {
  868. struct ieee80211_local *local = sdata->local;
  869. struct ieee80211_qos_hdr *nullfunc;
  870. struct sk_buff *skb;
  871. int size = sizeof(*nullfunc);
  872. __le16 fc;
  873. bool qos = test_sta_flag(sta, WLAN_STA_WME);
  874. struct ieee80211_tx_info *info;
  875. struct ieee80211_chanctx_conf *chanctx_conf;
  876. if (qos) {
  877. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  878. IEEE80211_STYPE_QOS_NULLFUNC |
  879. IEEE80211_FCTL_FROMDS);
  880. } else {
  881. size -= 2;
  882. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  883. IEEE80211_STYPE_NULLFUNC |
  884. IEEE80211_FCTL_FROMDS);
  885. }
  886. skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
  887. if (!skb)
  888. return;
  889. skb_reserve(skb, local->hw.extra_tx_headroom);
  890. nullfunc = (void *) skb_put(skb, size);
  891. nullfunc->frame_control = fc;
  892. nullfunc->duration_id = 0;
  893. memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
  894. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  895. memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
  896. skb->priority = tid;
  897. skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
  898. if (qos) {
  899. nullfunc->qos_ctrl = cpu_to_le16(tid);
  900. if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
  901. nullfunc->qos_ctrl |=
  902. cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
  903. }
  904. info = IEEE80211_SKB_CB(skb);
  905. /*
  906. * Tell TX path to send this frame even though the
  907. * STA may still remain is PS mode after this frame
  908. * exchange. Also set EOSP to indicate this packet
  909. * ends the poll/service period.
  910. */
  911. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
  912. IEEE80211_TX_STATUS_EOSP |
  913. IEEE80211_TX_CTL_REQ_TX_STATUS;
  914. drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
  915. rcu_read_lock();
  916. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  917. if (WARN_ON(!chanctx_conf)) {
  918. rcu_read_unlock();
  919. kfree_skb(skb);
  920. return;
  921. }
  922. ieee80211_xmit(sdata, skb, chanctx_conf->def.chan->band);
  923. rcu_read_unlock();
  924. }
  925. static void
  926. ieee80211_sta_ps_deliver_response(struct sta_info *sta,
  927. int n_frames, u8 ignored_acs,
  928. enum ieee80211_frame_release_type reason)
  929. {
  930. struct ieee80211_sub_if_data *sdata = sta->sdata;
  931. struct ieee80211_local *local = sdata->local;
  932. bool found = false;
  933. bool more_data = false;
  934. int ac;
  935. unsigned long driver_release_tids = 0;
  936. struct sk_buff_head frames;
  937. /* Service or PS-Poll period starts */
  938. set_sta_flag(sta, WLAN_STA_SP);
  939. __skb_queue_head_init(&frames);
  940. /*
  941. * Get response frame(s) and more data bit for it.
  942. */
  943. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  944. unsigned long tids;
  945. if (ignored_acs & BIT(ac))
  946. continue;
  947. tids = ieee80211_tids_for_ac(ac);
  948. if (!found) {
  949. driver_release_tids = sta->driver_buffered_tids & tids;
  950. if (driver_release_tids) {
  951. found = true;
  952. } else {
  953. struct sk_buff *skb;
  954. while (n_frames > 0) {
  955. skb = skb_dequeue(&sta->tx_filtered[ac]);
  956. if (!skb) {
  957. skb = skb_dequeue(
  958. &sta->ps_tx_buf[ac]);
  959. if (skb)
  960. local->total_ps_buffered--;
  961. }
  962. if (!skb)
  963. break;
  964. n_frames--;
  965. found = true;
  966. __skb_queue_tail(&frames, skb);
  967. }
  968. }
  969. /*
  970. * If the driver has data on more than one TID then
  971. * certainly there's more data if we release just a
  972. * single frame now (from a single TID).
  973. */
  974. if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
  975. hweight16(driver_release_tids) > 1) {
  976. more_data = true;
  977. driver_release_tids =
  978. BIT(ffs(driver_release_tids) - 1);
  979. break;
  980. }
  981. }
  982. if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
  983. !skb_queue_empty(&sta->ps_tx_buf[ac])) {
  984. more_data = true;
  985. break;
  986. }
  987. }
  988. if (!found) {
  989. int tid;
  990. /*
  991. * For PS-Poll, this can only happen due to a race condition
  992. * when we set the TIM bit and the station notices it, but
  993. * before it can poll for the frame we expire it.
  994. *
  995. * For uAPSD, this is said in the standard (11.2.1.5 h):
  996. * At each unscheduled SP for a non-AP STA, the AP shall
  997. * attempt to transmit at least one MSDU or MMPDU, but no
  998. * more than the value specified in the Max SP Length field
  999. * in the QoS Capability element from delivery-enabled ACs,
  1000. * that are destined for the non-AP STA.
  1001. *
  1002. * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
  1003. */
  1004. /* This will evaluate to 1, 3, 5 or 7. */
  1005. tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
  1006. ieee80211_send_null_response(sdata, sta, tid, reason);
  1007. return;
  1008. }
  1009. if (!driver_release_tids) {
  1010. struct sk_buff_head pending;
  1011. struct sk_buff *skb;
  1012. int num = 0;
  1013. u16 tids = 0;
  1014. skb_queue_head_init(&pending);
  1015. while ((skb = __skb_dequeue(&frames))) {
  1016. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1017. struct ieee80211_hdr *hdr = (void *) skb->data;
  1018. u8 *qoshdr = NULL;
  1019. num++;
  1020. /*
  1021. * Tell TX path to send this frame even though the
  1022. * STA may still remain is PS mode after this frame
  1023. * exchange.
  1024. */
  1025. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
  1026. /*
  1027. * Use MoreData flag to indicate whether there are
  1028. * more buffered frames for this STA
  1029. */
  1030. if (more_data || !skb_queue_empty(&frames))
  1031. hdr->frame_control |=
  1032. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1033. else
  1034. hdr->frame_control &=
  1035. cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  1036. if (ieee80211_is_data_qos(hdr->frame_control) ||
  1037. ieee80211_is_qos_nullfunc(hdr->frame_control))
  1038. qoshdr = ieee80211_get_qos_ctl(hdr);
  1039. /* end service period after last frame */
  1040. if (skb_queue_empty(&frames)) {
  1041. if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
  1042. qoshdr)
  1043. *qoshdr |= IEEE80211_QOS_CTL_EOSP;
  1044. info->flags |= IEEE80211_TX_STATUS_EOSP |
  1045. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1046. }
  1047. if (qoshdr)
  1048. tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
  1049. else
  1050. tids |= BIT(0);
  1051. __skb_queue_tail(&pending, skb);
  1052. }
  1053. drv_allow_buffered_frames(local, sta, tids, num,
  1054. reason, more_data);
  1055. ieee80211_add_pending_skbs(local, &pending);
  1056. sta_info_recalc_tim(sta);
  1057. } else {
  1058. /*
  1059. * We need to release a frame that is buffered somewhere in the
  1060. * driver ... it'll have to handle that.
  1061. * Note that, as per the comment above, it'll also have to see
  1062. * if there is more than just one frame on the specific TID that
  1063. * we're releasing from, and it needs to set the more-data bit
  1064. * accordingly if we tell it that there's no more data. If we do
  1065. * tell it there's more data, then of course the more-data bit
  1066. * needs to be set anyway.
  1067. */
  1068. drv_release_buffered_frames(local, sta, driver_release_tids,
  1069. n_frames, reason, more_data);
  1070. /*
  1071. * Note that we don't recalculate the TIM bit here as it would
  1072. * most likely have no effect at all unless the driver told us
  1073. * that the TID became empty before returning here from the
  1074. * release function.
  1075. * Either way, however, when the driver tells us that the TID
  1076. * became empty we'll do the TIM recalculation.
  1077. */
  1078. }
  1079. }
  1080. void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
  1081. {
  1082. u8 ignore_for_response = sta->sta.uapsd_queues;
  1083. /*
  1084. * If all ACs are delivery-enabled then we should reply
  1085. * from any of them, if only some are enabled we reply
  1086. * only from the non-enabled ones.
  1087. */
  1088. if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
  1089. ignore_for_response = 0;
  1090. ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
  1091. IEEE80211_FRAME_RELEASE_PSPOLL);
  1092. }
  1093. void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
  1094. {
  1095. int n_frames = sta->sta.max_sp;
  1096. u8 delivery_enabled = sta->sta.uapsd_queues;
  1097. /*
  1098. * If we ever grow support for TSPEC this might happen if
  1099. * the TSPEC update from hostapd comes in between a trigger
  1100. * frame setting WLAN_STA_UAPSD in the RX path and this
  1101. * actually getting called.
  1102. */
  1103. if (!delivery_enabled)
  1104. return;
  1105. switch (sta->sta.max_sp) {
  1106. case 1:
  1107. n_frames = 2;
  1108. break;
  1109. case 2:
  1110. n_frames = 4;
  1111. break;
  1112. case 3:
  1113. n_frames = 6;
  1114. break;
  1115. case 0:
  1116. /* XXX: what is a good value? */
  1117. n_frames = 8;
  1118. break;
  1119. }
  1120. ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
  1121. IEEE80211_FRAME_RELEASE_UAPSD);
  1122. }
  1123. void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
  1124. struct ieee80211_sta *pubsta, bool block)
  1125. {
  1126. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1127. trace_api_sta_block_awake(sta->local, pubsta, block);
  1128. if (block)
  1129. set_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1130. else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
  1131. ieee80211_queue_work(hw, &sta->drv_unblock_wk);
  1132. }
  1133. EXPORT_SYMBOL(ieee80211_sta_block_awake);
  1134. void ieee80211_sta_eosp_irqsafe(struct ieee80211_sta *pubsta)
  1135. {
  1136. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1137. struct ieee80211_local *local = sta->local;
  1138. struct sk_buff *skb;
  1139. struct skb_eosp_msg_data *data;
  1140. trace_api_eosp(local, pubsta);
  1141. skb = alloc_skb(0, GFP_ATOMIC);
  1142. if (!skb) {
  1143. /* too bad ... but race is better than loss */
  1144. clear_sta_flag(sta, WLAN_STA_SP);
  1145. return;
  1146. }
  1147. data = (void *)skb->cb;
  1148. memcpy(data->sta, pubsta->addr, ETH_ALEN);
  1149. memcpy(data->iface, sta->sdata->vif.addr, ETH_ALEN);
  1150. skb->pkt_type = IEEE80211_EOSP_MSG;
  1151. skb_queue_tail(&local->skb_queue, skb);
  1152. tasklet_schedule(&local->tasklet);
  1153. }
  1154. EXPORT_SYMBOL(ieee80211_sta_eosp_irqsafe);
  1155. void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
  1156. u8 tid, bool buffered)
  1157. {
  1158. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1159. if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
  1160. return;
  1161. if (buffered)
  1162. set_bit(tid, &sta->driver_buffered_tids);
  1163. else
  1164. clear_bit(tid, &sta->driver_buffered_tids);
  1165. sta_info_recalc_tim(sta);
  1166. }
  1167. EXPORT_SYMBOL(ieee80211_sta_set_buffered);
  1168. int sta_info_move_state(struct sta_info *sta,
  1169. enum ieee80211_sta_state new_state)
  1170. {
  1171. might_sleep();
  1172. if (sta->sta_state == new_state)
  1173. return 0;
  1174. /* check allowed transitions first */
  1175. switch (new_state) {
  1176. case IEEE80211_STA_NONE:
  1177. if (sta->sta_state != IEEE80211_STA_AUTH)
  1178. return -EINVAL;
  1179. break;
  1180. case IEEE80211_STA_AUTH:
  1181. if (sta->sta_state != IEEE80211_STA_NONE &&
  1182. sta->sta_state != IEEE80211_STA_ASSOC)
  1183. return -EINVAL;
  1184. break;
  1185. case IEEE80211_STA_ASSOC:
  1186. if (sta->sta_state != IEEE80211_STA_AUTH &&
  1187. sta->sta_state != IEEE80211_STA_AUTHORIZED)
  1188. return -EINVAL;
  1189. break;
  1190. case IEEE80211_STA_AUTHORIZED:
  1191. if (sta->sta_state != IEEE80211_STA_ASSOC)
  1192. return -EINVAL;
  1193. break;
  1194. default:
  1195. WARN(1, "invalid state %d", new_state);
  1196. return -EINVAL;
  1197. }
  1198. sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
  1199. sta->sta.addr, new_state);
  1200. /*
  1201. * notify the driver before the actual changes so it can
  1202. * fail the transition
  1203. */
  1204. if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
  1205. int err = drv_sta_state(sta->local, sta->sdata, sta,
  1206. sta->sta_state, new_state);
  1207. if (err)
  1208. return err;
  1209. }
  1210. /* reflect the change in all state variables */
  1211. switch (new_state) {
  1212. case IEEE80211_STA_NONE:
  1213. if (sta->sta_state == IEEE80211_STA_AUTH)
  1214. clear_bit(WLAN_STA_AUTH, &sta->_flags);
  1215. break;
  1216. case IEEE80211_STA_AUTH:
  1217. if (sta->sta_state == IEEE80211_STA_NONE)
  1218. set_bit(WLAN_STA_AUTH, &sta->_flags);
  1219. else if (sta->sta_state == IEEE80211_STA_ASSOC)
  1220. clear_bit(WLAN_STA_ASSOC, &sta->_flags);
  1221. break;
  1222. case IEEE80211_STA_ASSOC:
  1223. if (sta->sta_state == IEEE80211_STA_AUTH) {
  1224. set_bit(WLAN_STA_ASSOC, &sta->_flags);
  1225. } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
  1226. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  1227. (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1228. !sta->sdata->u.vlan.sta))
  1229. atomic_dec(&sta->sdata->bss->num_mcast_sta);
  1230. clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1231. }
  1232. break;
  1233. case IEEE80211_STA_AUTHORIZED:
  1234. if (sta->sta_state == IEEE80211_STA_ASSOC) {
  1235. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  1236. (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1237. !sta->sdata->u.vlan.sta))
  1238. atomic_inc(&sta->sdata->bss->num_mcast_sta);
  1239. set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1240. }
  1241. break;
  1242. default:
  1243. break;
  1244. }
  1245. sta->sta_state = new_state;
  1246. return 0;
  1247. }