sta_info.c 37 KB

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