sta_info.c 36 KB

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