sta_info.c 36 KB

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