sta_info.c 36 KB

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