sta_info.c 38 KB

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