sta_info.c 37 KB

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