sta_info.c 36 KB

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