sta_info.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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. /* There could be some memory leaks because of ampdu tx pending queue
  688. * not being freed before destroying the station info.
  689. *
  690. * Make sure that such queues are purged before freeing the station
  691. * info.
  692. * TODO: We have to somehow postpone the full destruction
  693. * until the aggregation stop completes. Refer
  694. * http://thread.gmane.org/gmane.linux.kernel.wireless.general/81936
  695. */
  696. mutex_lock(&sta->ampdu_mlme.mtx);
  697. for (i = 0; i < STA_TID_NUM; i++) {
  698. tid_tx = rcu_dereference_protected_tid_tx(sta, i);
  699. if (!tid_tx)
  700. continue;
  701. if (skb_queue_len(&tid_tx->pending)) {
  702. #ifdef CONFIG_MAC80211_HT_DEBUG
  703. wiphy_debug(local->hw.wiphy, "TX A-MPDU purging %d "
  704. "packets for tid=%d\n",
  705. skb_queue_len(&tid_tx->pending), i);
  706. #endif /* CONFIG_MAC80211_HT_DEBUG */
  707. __skb_queue_purge(&tid_tx->pending);
  708. }
  709. kfree_rcu(tid_tx, rcu_head);
  710. }
  711. mutex_unlock(&sta->ampdu_mlme.mtx);
  712. sta_info_free(local, sta);
  713. return 0;
  714. }
  715. int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
  716. {
  717. struct sta_info *sta;
  718. int ret;
  719. mutex_lock(&sdata->local->sta_mtx);
  720. sta = sta_info_get_rx(sdata, addr);
  721. ret = __sta_info_destroy(sta);
  722. mutex_unlock(&sdata->local->sta_mtx);
  723. return ret;
  724. }
  725. int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
  726. const u8 *addr)
  727. {
  728. struct sta_info *sta;
  729. int ret;
  730. mutex_lock(&sdata->local->sta_mtx);
  731. sta = sta_info_get_bss_rx(sdata, addr);
  732. ret = __sta_info_destroy(sta);
  733. mutex_unlock(&sdata->local->sta_mtx);
  734. return ret;
  735. }
  736. static void sta_info_cleanup(unsigned long data)
  737. {
  738. struct ieee80211_local *local = (struct ieee80211_local *) data;
  739. struct sta_info *sta;
  740. bool timer_needed = false;
  741. rcu_read_lock();
  742. list_for_each_entry_rcu(sta, &local->sta_list, list)
  743. if (sta_info_cleanup_expire_buffered(local, sta))
  744. timer_needed = true;
  745. rcu_read_unlock();
  746. if (local->quiescing)
  747. return;
  748. if (!timer_needed)
  749. return;
  750. mod_timer(&local->sta_cleanup,
  751. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
  752. }
  753. void sta_info_init(struct ieee80211_local *local)
  754. {
  755. spin_lock_init(&local->tim_lock);
  756. mutex_init(&local->sta_mtx);
  757. INIT_LIST_HEAD(&local->sta_list);
  758. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  759. (unsigned long)local);
  760. }
  761. void sta_info_stop(struct ieee80211_local *local)
  762. {
  763. del_timer(&local->sta_cleanup);
  764. sta_info_flush(local, NULL);
  765. }
  766. /**
  767. * sta_info_flush - flush matching STA entries from the STA table
  768. *
  769. * Returns the number of removed STA entries.
  770. *
  771. * @local: local interface data
  772. * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
  773. */
  774. int sta_info_flush(struct ieee80211_local *local,
  775. struct ieee80211_sub_if_data *sdata)
  776. {
  777. struct sta_info *sta, *tmp;
  778. int ret = 0;
  779. might_sleep();
  780. mutex_lock(&local->sta_mtx);
  781. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  782. if (!sdata || sdata == sta->sdata)
  783. WARN_ON(__sta_info_destroy(sta));
  784. }
  785. mutex_unlock(&local->sta_mtx);
  786. return ret;
  787. }
  788. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  789. unsigned long exp_time)
  790. {
  791. struct ieee80211_local *local = sdata->local;
  792. struct sta_info *sta, *tmp;
  793. mutex_lock(&local->sta_mtx);
  794. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  795. if (sdata != sta->sdata)
  796. continue;
  797. if (time_after(jiffies, sta->last_rx + exp_time)) {
  798. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  799. printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
  800. sdata->name, sta->sta.addr);
  801. #endif
  802. WARN_ON(__sta_info_destroy(sta));
  803. }
  804. }
  805. mutex_unlock(&local->sta_mtx);
  806. }
  807. struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
  808. const u8 *addr,
  809. const u8 *localaddr)
  810. {
  811. struct sta_info *sta, *nxt;
  812. /*
  813. * Just return a random station if localaddr is NULL
  814. * ... first in list.
  815. */
  816. for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
  817. if (localaddr &&
  818. compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
  819. continue;
  820. if (!sta->uploaded)
  821. return NULL;
  822. return &sta->sta;
  823. }
  824. return NULL;
  825. }
  826. EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
  827. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
  828. const u8 *addr)
  829. {
  830. struct sta_info *sta;
  831. if (!vif)
  832. return NULL;
  833. sta = sta_info_get_bss(vif_to_sdata(vif), addr);
  834. if (!sta)
  835. return NULL;
  836. if (!sta->uploaded)
  837. return NULL;
  838. return &sta->sta;
  839. }
  840. EXPORT_SYMBOL(ieee80211_find_sta);
  841. static void clear_sta_ps_flags(void *_sta)
  842. {
  843. struct sta_info *sta = _sta;
  844. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  845. clear_sta_flag(sta, WLAN_STA_PS_STA);
  846. }
  847. /* powersave support code */
  848. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  849. {
  850. struct ieee80211_sub_if_data *sdata = sta->sdata;
  851. struct ieee80211_local *local = sdata->local;
  852. struct sk_buff_head pending;
  853. int filtered = 0, buffered = 0, ac;
  854. clear_sta_flag(sta, WLAN_STA_SP);
  855. BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1);
  856. sta->driver_buffered_tids = 0;
  857. if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
  858. drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
  859. skb_queue_head_init(&pending);
  860. /* Send all buffered frames to the station */
  861. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  862. int count = skb_queue_len(&pending), tmp;
  863. skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
  864. tmp = skb_queue_len(&pending);
  865. filtered += tmp - count;
  866. count = tmp;
  867. skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
  868. tmp = skb_queue_len(&pending);
  869. buffered += tmp - count;
  870. }
  871. ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
  872. local->total_ps_buffered -= buffered;
  873. sta_info_recalc_tim(sta);
  874. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  875. printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
  876. "since STA not sleeping anymore\n", sdata->name,
  877. sta->sta.addr, sta->sta.aid, filtered, buffered);
  878. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  879. }
  880. static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
  881. struct sta_info *sta, int tid,
  882. enum ieee80211_frame_release_type reason)
  883. {
  884. struct ieee80211_local *local = sdata->local;
  885. struct ieee80211_qos_hdr *nullfunc;
  886. struct sk_buff *skb;
  887. int size = sizeof(*nullfunc);
  888. __le16 fc;
  889. bool qos = test_sta_flag(sta, WLAN_STA_WME);
  890. struct ieee80211_tx_info *info;
  891. if (qos) {
  892. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  893. IEEE80211_STYPE_QOS_NULLFUNC |
  894. IEEE80211_FCTL_FROMDS);
  895. } else {
  896. size -= 2;
  897. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  898. IEEE80211_STYPE_NULLFUNC |
  899. IEEE80211_FCTL_FROMDS);
  900. }
  901. skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
  902. if (!skb)
  903. return;
  904. skb_reserve(skb, local->hw.extra_tx_headroom);
  905. nullfunc = (void *) skb_put(skb, size);
  906. nullfunc->frame_control = fc;
  907. nullfunc->duration_id = 0;
  908. memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
  909. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  910. memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
  911. skb->priority = tid;
  912. skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
  913. if (qos) {
  914. nullfunc->qos_ctrl = cpu_to_le16(tid);
  915. if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
  916. nullfunc->qos_ctrl |=
  917. cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
  918. }
  919. info = IEEE80211_SKB_CB(skb);
  920. /*
  921. * Tell TX path to send this frame even though the
  922. * STA may still remain is PS mode after this frame
  923. * exchange. Also set EOSP to indicate this packet
  924. * ends the poll/service period.
  925. */
  926. info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE |
  927. IEEE80211_TX_STATUS_EOSP |
  928. IEEE80211_TX_CTL_REQ_TX_STATUS;
  929. drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
  930. ieee80211_xmit(sdata, skb);
  931. }
  932. static void
  933. ieee80211_sta_ps_deliver_response(struct sta_info *sta,
  934. int n_frames, u8 ignored_acs,
  935. enum ieee80211_frame_release_type reason)
  936. {
  937. struct ieee80211_sub_if_data *sdata = sta->sdata;
  938. struct ieee80211_local *local = sdata->local;
  939. bool found = false;
  940. bool more_data = false;
  941. int ac;
  942. unsigned long driver_release_tids = 0;
  943. struct sk_buff_head frames;
  944. /* Service or PS-Poll period starts */
  945. set_sta_flag(sta, WLAN_STA_SP);
  946. __skb_queue_head_init(&frames);
  947. /*
  948. * Get response frame(s) and more data bit for it.
  949. */
  950. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  951. unsigned long tids;
  952. if (ignored_acs & BIT(ac))
  953. continue;
  954. tids = ieee80211_tids_for_ac(ac);
  955. if (!found) {
  956. driver_release_tids = sta->driver_buffered_tids & tids;
  957. if (driver_release_tids) {
  958. found = true;
  959. } else {
  960. struct sk_buff *skb;
  961. while (n_frames > 0) {
  962. skb = skb_dequeue(&sta->tx_filtered[ac]);
  963. if (!skb) {
  964. skb = skb_dequeue(
  965. &sta->ps_tx_buf[ac]);
  966. if (skb)
  967. local->total_ps_buffered--;
  968. }
  969. if (!skb)
  970. break;
  971. n_frames--;
  972. found = true;
  973. __skb_queue_tail(&frames, skb);
  974. }
  975. }
  976. /*
  977. * If the driver has data on more than one TID then
  978. * certainly there's more data if we release just a
  979. * single frame now (from a single TID).
  980. */
  981. if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
  982. hweight16(driver_release_tids) > 1) {
  983. more_data = true;
  984. driver_release_tids =
  985. BIT(ffs(driver_release_tids) - 1);
  986. break;
  987. }
  988. }
  989. if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
  990. !skb_queue_empty(&sta->ps_tx_buf[ac])) {
  991. more_data = true;
  992. break;
  993. }
  994. }
  995. if (!found) {
  996. int tid;
  997. /*
  998. * For PS-Poll, this can only happen due to a race condition
  999. * when we set the TIM bit and the station notices it, but
  1000. * before it can poll for the frame we expire it.
  1001. *
  1002. * For uAPSD, this is said in the standard (11.2.1.5 h):
  1003. * At each unscheduled SP for a non-AP STA, the AP shall
  1004. * attempt to transmit at least one MSDU or MMPDU, but no
  1005. * more than the value specified in the Max SP Length field
  1006. * in the QoS Capability element from delivery-enabled ACs,
  1007. * that are destined for the non-AP STA.
  1008. *
  1009. * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
  1010. */
  1011. /* This will evaluate to 1, 3, 5 or 7. */
  1012. tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
  1013. ieee80211_send_null_response(sdata, sta, tid, reason);
  1014. return;
  1015. }
  1016. if (!driver_release_tids) {
  1017. struct sk_buff_head pending;
  1018. struct sk_buff *skb;
  1019. int num = 0;
  1020. u16 tids = 0;
  1021. skb_queue_head_init(&pending);
  1022. while ((skb = __skb_dequeue(&frames))) {
  1023. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1024. struct ieee80211_hdr *hdr = (void *) skb->data;
  1025. u8 *qoshdr = NULL;
  1026. num++;
  1027. /*
  1028. * Tell TX path to send this frame even though the
  1029. * STA may still remain is PS mode after this frame
  1030. * exchange.
  1031. */
  1032. info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE;
  1033. /*
  1034. * Use MoreData flag to indicate whether there are
  1035. * more buffered frames for this STA
  1036. */
  1037. if (more_data || !skb_queue_empty(&frames))
  1038. hdr->frame_control |=
  1039. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1040. else
  1041. hdr->frame_control &=
  1042. cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  1043. if (ieee80211_is_data_qos(hdr->frame_control) ||
  1044. ieee80211_is_qos_nullfunc(hdr->frame_control))
  1045. qoshdr = ieee80211_get_qos_ctl(hdr);
  1046. /* set EOSP for the frame */
  1047. if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
  1048. qoshdr && skb_queue_empty(&frames))
  1049. *qoshdr |= IEEE80211_QOS_CTL_EOSP;
  1050. info->flags |= IEEE80211_TX_STATUS_EOSP |
  1051. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1052. if (qoshdr)
  1053. tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
  1054. else
  1055. tids |= BIT(0);
  1056. __skb_queue_tail(&pending, skb);
  1057. }
  1058. drv_allow_buffered_frames(local, sta, tids, num,
  1059. reason, more_data);
  1060. ieee80211_add_pending_skbs(local, &pending);
  1061. sta_info_recalc_tim(sta);
  1062. } else {
  1063. /*
  1064. * We need to release a frame that is buffered somewhere in the
  1065. * driver ... it'll have to handle that.
  1066. * Note that, as per the comment above, it'll also have to see
  1067. * if there is more than just one frame on the specific TID that
  1068. * we're releasing from, and it needs to set the more-data bit
  1069. * accordingly if we tell it that there's no more data. If we do
  1070. * tell it there's more data, then of course the more-data bit
  1071. * needs to be set anyway.
  1072. */
  1073. drv_release_buffered_frames(local, sta, driver_release_tids,
  1074. n_frames, reason, more_data);
  1075. /*
  1076. * Note that we don't recalculate the TIM bit here as it would
  1077. * most likely have no effect at all unless the driver told us
  1078. * that the TID became empty before returning here from the
  1079. * release function.
  1080. * Either way, however, when the driver tells us that the TID
  1081. * became empty we'll do the TIM recalculation.
  1082. */
  1083. }
  1084. }
  1085. void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
  1086. {
  1087. u8 ignore_for_response = sta->sta.uapsd_queues;
  1088. /*
  1089. * If all ACs are delivery-enabled then we should reply
  1090. * from any of them, if only some are enabled we reply
  1091. * only from the non-enabled ones.
  1092. */
  1093. if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
  1094. ignore_for_response = 0;
  1095. ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
  1096. IEEE80211_FRAME_RELEASE_PSPOLL);
  1097. }
  1098. void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
  1099. {
  1100. int n_frames = sta->sta.max_sp;
  1101. u8 delivery_enabled = sta->sta.uapsd_queues;
  1102. /*
  1103. * If we ever grow support for TSPEC this might happen if
  1104. * the TSPEC update from hostapd comes in between a trigger
  1105. * frame setting WLAN_STA_UAPSD in the RX path and this
  1106. * actually getting called.
  1107. */
  1108. if (!delivery_enabled)
  1109. return;
  1110. switch (sta->sta.max_sp) {
  1111. case 1:
  1112. n_frames = 2;
  1113. break;
  1114. case 2:
  1115. n_frames = 4;
  1116. break;
  1117. case 3:
  1118. n_frames = 6;
  1119. break;
  1120. case 0:
  1121. /* XXX: what is a good value? */
  1122. n_frames = 8;
  1123. break;
  1124. }
  1125. ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
  1126. IEEE80211_FRAME_RELEASE_UAPSD);
  1127. }
  1128. void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
  1129. struct ieee80211_sta *pubsta, bool block)
  1130. {
  1131. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1132. trace_api_sta_block_awake(sta->local, pubsta, block);
  1133. if (block)
  1134. set_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1135. else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
  1136. ieee80211_queue_work(hw, &sta->drv_unblock_wk);
  1137. }
  1138. EXPORT_SYMBOL(ieee80211_sta_block_awake);
  1139. void ieee80211_sta_eosp_irqsafe(struct ieee80211_sta *pubsta)
  1140. {
  1141. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1142. struct ieee80211_local *local = sta->local;
  1143. struct sk_buff *skb;
  1144. struct skb_eosp_msg_data *data;
  1145. trace_api_eosp(local, pubsta);
  1146. skb = alloc_skb(0, GFP_ATOMIC);
  1147. if (!skb) {
  1148. /* too bad ... but race is better than loss */
  1149. clear_sta_flag(sta, WLAN_STA_SP);
  1150. return;
  1151. }
  1152. data = (void *)skb->cb;
  1153. memcpy(data->sta, pubsta->addr, ETH_ALEN);
  1154. memcpy(data->iface, sta->sdata->vif.addr, ETH_ALEN);
  1155. skb->pkt_type = IEEE80211_EOSP_MSG;
  1156. skb_queue_tail(&local->skb_queue, skb);
  1157. tasklet_schedule(&local->tasklet);
  1158. }
  1159. EXPORT_SYMBOL(ieee80211_sta_eosp_irqsafe);
  1160. void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
  1161. u8 tid, bool buffered)
  1162. {
  1163. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1164. if (WARN_ON(tid >= STA_TID_NUM))
  1165. return;
  1166. if (buffered)
  1167. set_bit(tid, &sta->driver_buffered_tids);
  1168. else
  1169. clear_bit(tid, &sta->driver_buffered_tids);
  1170. sta_info_recalc_tim(sta);
  1171. }
  1172. EXPORT_SYMBOL(ieee80211_sta_set_buffered);
  1173. int sta_info_move_state_checked(struct sta_info *sta,
  1174. enum ieee80211_sta_state new_state)
  1175. {
  1176. might_sleep();
  1177. if (sta->sta_state == new_state)
  1178. return 0;
  1179. switch (new_state) {
  1180. case IEEE80211_STA_NONE:
  1181. if (sta->sta_state == IEEE80211_STA_AUTH)
  1182. clear_bit(WLAN_STA_AUTH, &sta->_flags);
  1183. else
  1184. return -EINVAL;
  1185. break;
  1186. case IEEE80211_STA_AUTH:
  1187. if (sta->sta_state == IEEE80211_STA_NONE)
  1188. set_bit(WLAN_STA_AUTH, &sta->_flags);
  1189. else if (sta->sta_state == IEEE80211_STA_ASSOC)
  1190. clear_bit(WLAN_STA_ASSOC, &sta->_flags);
  1191. else
  1192. return -EINVAL;
  1193. break;
  1194. case IEEE80211_STA_ASSOC:
  1195. if (sta->sta_state == IEEE80211_STA_AUTH) {
  1196. set_bit(WLAN_STA_ASSOC, &sta->_flags);
  1197. } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
  1198. if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
  1199. atomic_dec(&sta->sdata->u.ap.num_sta_authorized);
  1200. clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1201. } else
  1202. return -EINVAL;
  1203. break;
  1204. case IEEE80211_STA_AUTHORIZED:
  1205. if (sta->sta_state == IEEE80211_STA_ASSOC) {
  1206. if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
  1207. atomic_inc(&sta->sdata->u.ap.num_sta_authorized);
  1208. set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1209. } else
  1210. return -EINVAL;
  1211. break;
  1212. default:
  1213. WARN(1, "invalid state %d", new_state);
  1214. return -EINVAL;
  1215. }
  1216. printk(KERN_DEBUG "%s: moving STA %pM to state %d\n",
  1217. sta->sdata->name, sta->sta.addr, new_state);
  1218. sta->sta_state = new_state;
  1219. return 0;
  1220. }