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