sta_info.c 38 KB

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