sta_info.c 38 KB

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