sta_info.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  4. * Copyright 2013-2014 Intel Mobile Communications GmbH
  5. * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
  6. * Copyright (C) 2018-2020 Intel Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/types.h>
  17. #include <linux/slab.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/timer.h>
  21. #include <linux/rtnetlink.h>
  22. #include <net/codel.h>
  23. #include <net/mac80211.h>
  24. #include "ieee80211_i.h"
  25. #include "driver-ops.h"
  26. #include "rate.h"
  27. #include "sta_info.h"
  28. #include "debugfs_sta.h"
  29. #include "mesh.h"
  30. #include "wme.h"
  31. /**
  32. * DOC: STA information lifetime rules
  33. *
  34. * STA info structures (&struct sta_info) are managed in a hash table
  35. * for faster lookup and a list for iteration. They are managed using
  36. * RCU, i.e. access to the list and hash table is protected by RCU.
  37. *
  38. * Upon allocating a STA info structure with sta_info_alloc(), the caller
  39. * owns that structure. It must then insert it into the hash table using
  40. * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
  41. * case (which acquires an rcu read section but must not be called from
  42. * within one) will the pointer still be valid after the call. Note that
  43. * the caller may not do much with the STA info before inserting it, in
  44. * particular, it may not start any mesh peer link management or add
  45. * encryption keys.
  46. *
  47. * When the insertion fails (sta_info_insert()) returns non-zero), the
  48. * structure will have been freed by sta_info_insert()!
  49. *
  50. * Station entries are added by mac80211 when you establish a link with a
  51. * peer. This means different things for the different type of interfaces
  52. * we support. For a regular station this mean we add the AP sta when we
  53. * receive an association response from the AP. For IBSS this occurs when
  54. * get to know about a peer on the same IBSS. For WDS we add the sta for
  55. * the peer immediately upon device open. When using AP mode we add stations
  56. * for each respective station upon request from userspace through nl80211.
  57. *
  58. * In order to remove a STA info structure, various sta_info_destroy_*()
  59. * calls are available.
  60. *
  61. * There is no concept of ownership on a STA entry, each structure is
  62. * owned by the global hash table/list until it is removed. All users of
  63. * the structure need to be RCU protected so that the structure won't be
  64. * freed before they are done using it.
  65. */
  66. static const struct rhashtable_params sta_rht_params = {
  67. .nelem_hint = 3, /* start small */
  68. .automatic_shrinking = true,
  69. .head_offset = offsetof(struct sta_info, hash_node),
  70. .key_offset = offsetof(struct sta_info, addr),
  71. .key_len = ETH_ALEN,
  72. .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
  73. };
  74. /* Caller must hold local->sta_mtx */
  75. static int sta_info_hash_del(struct ieee80211_local *local,
  76. struct sta_info *sta)
  77. {
  78. return rhltable_remove(&local->sta_hash, &sta->hash_node,
  79. sta_rht_params);
  80. }
  81. static void __cleanup_single_sta(struct sta_info *sta)
  82. {
  83. int ac, i;
  84. struct tid_ampdu_tx *tid_tx;
  85. struct ieee80211_sub_if_data *sdata = sta->sdata;
  86. struct ieee80211_local *local = sdata->local;
  87. struct fq *fq = &local->fq;
  88. struct ps_data *ps;
  89. if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
  90. test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
  91. test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
  92. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  93. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  94. ps = &sdata->bss->ps;
  95. else if (ieee80211_vif_is_mesh(&sdata->vif))
  96. ps = &sdata->u.mesh.ps;
  97. else
  98. return;
  99. clear_sta_flag(sta, WLAN_STA_PS_STA);
  100. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  101. clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
  102. atomic_dec(&ps->num_sta_ps);
  103. }
  104. if (sta->sta.txq[0]) {
  105. for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
  106. struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
  107. spin_lock_bh(&fq->lock);
  108. ieee80211_txq_purge(local, txqi);
  109. spin_unlock_bh(&fq->lock);
  110. }
  111. }
  112. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  113. local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
  114. ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
  115. ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
  116. }
  117. if (ieee80211_vif_is_mesh(&sdata->vif))
  118. mesh_sta_cleanup(sta);
  119. cancel_work_sync(&sta->drv_deliver_wk);
  120. /*
  121. * Destroy aggregation state here. It would be nice to wait for the
  122. * driver to finish aggregation stop and then clean up, but for now
  123. * drivers have to handle aggregation stop being requested, followed
  124. * directly by station destruction.
  125. */
  126. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  127. kfree(sta->ampdu_mlme.tid_start_tx[i]);
  128. tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
  129. if (!tid_tx)
  130. continue;
  131. ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
  132. kfree(tid_tx);
  133. }
  134. }
  135. static void cleanup_single_sta(struct sta_info *sta)
  136. {
  137. struct ieee80211_sub_if_data *sdata = sta->sdata;
  138. struct ieee80211_local *local = sdata->local;
  139. __cleanup_single_sta(sta);
  140. sta_info_free(local, sta);
  141. }
  142. struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
  143. const u8 *addr)
  144. {
  145. return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
  146. }
  147. /* protected by RCU */
  148. struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
  149. const u8 *addr)
  150. {
  151. struct ieee80211_local *local = sdata->local;
  152. struct rhlist_head *tmp;
  153. struct sta_info *sta;
  154. rcu_read_lock();
  155. for_each_sta_info(local, addr, sta, tmp) {
  156. if (sta->sdata == sdata) {
  157. rcu_read_unlock();
  158. /* this is safe as the caller must already hold
  159. * another rcu read section or the mutex
  160. */
  161. return sta;
  162. }
  163. }
  164. rcu_read_unlock();
  165. return NULL;
  166. }
  167. /*
  168. * Get sta info either from the specified interface
  169. * or from one of its vlans
  170. */
  171. struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
  172. const u8 *addr)
  173. {
  174. struct ieee80211_local *local = sdata->local;
  175. struct rhlist_head *tmp;
  176. struct sta_info *sta;
  177. rcu_read_lock();
  178. for_each_sta_info(local, addr, sta, tmp) {
  179. if (sta->sdata == sdata ||
  180. (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
  181. rcu_read_unlock();
  182. /* this is safe as the caller must already hold
  183. * another rcu read section or the mutex
  184. */
  185. return sta;
  186. }
  187. }
  188. rcu_read_unlock();
  189. return NULL;
  190. }
  191. struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
  192. int idx)
  193. {
  194. struct ieee80211_local *local = sdata->local;
  195. struct sta_info *sta;
  196. int i = 0;
  197. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  198. if (sdata != sta->sdata)
  199. continue;
  200. if (i < idx) {
  201. ++i;
  202. continue;
  203. }
  204. return sta;
  205. }
  206. return NULL;
  207. }
  208. /**
  209. * sta_info_free - free STA
  210. *
  211. * @local: pointer to the global information
  212. * @sta: STA info to free
  213. *
  214. * This function must undo everything done by sta_info_alloc()
  215. * that may happen before sta_info_insert(). It may only be
  216. * called when sta_info_insert() has not been attempted (and
  217. * if that fails, the station is freed anyway.)
  218. */
  219. void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
  220. {
  221. /*
  222. * If we had used sta_info_pre_move_state() then we might not
  223. * have gone through the state transitions down again, so do
  224. * it here now (and warn if it's inserted).
  225. *
  226. * This will clear state such as fast TX/RX that may have been
  227. * allocated during state transitions.
  228. */
  229. while (sta->sta_state > IEEE80211_STA_NONE) {
  230. int ret;
  231. WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
  232. ret = sta_info_move_state(sta, sta->sta_state - 1);
  233. if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
  234. break;
  235. }
  236. if (sta->rate_ctrl)
  237. rate_control_free_sta(sta);
  238. sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
  239. if (sta->sta.txq[0])
  240. kfree(to_txq_info(sta->sta.txq[0]));
  241. kfree(rcu_dereference_raw(sta->sta.rates));
  242. #ifdef CONFIG_MAC80211_MESH
  243. kfree(sta->mesh);
  244. #endif
  245. free_percpu(sta->pcpu_rx_stats);
  246. kfree(sta);
  247. }
  248. /* Caller must hold local->sta_mtx */
  249. static int sta_info_hash_add(struct ieee80211_local *local,
  250. struct sta_info *sta)
  251. {
  252. return rhltable_insert(&local->sta_hash, &sta->hash_node,
  253. sta_rht_params);
  254. }
  255. static void sta_deliver_ps_frames(struct work_struct *wk)
  256. {
  257. struct sta_info *sta;
  258. sta = container_of(wk, struct sta_info, drv_deliver_wk);
  259. if (sta->dead)
  260. return;
  261. local_bh_disable();
  262. if (!test_sta_flag(sta, WLAN_STA_PS_STA))
  263. ieee80211_sta_ps_deliver_wakeup(sta);
  264. else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
  265. ieee80211_sta_ps_deliver_poll_response(sta);
  266. else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
  267. ieee80211_sta_ps_deliver_uapsd(sta);
  268. local_bh_enable();
  269. }
  270. static int sta_prepare_rate_control(struct ieee80211_local *local,
  271. struct sta_info *sta, gfp_t gfp)
  272. {
  273. if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
  274. return 0;
  275. sta->rate_ctrl = local->rate_ctrl;
  276. sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
  277. sta, gfp);
  278. if (!sta->rate_ctrl_priv)
  279. return -ENOMEM;
  280. return 0;
  281. }
  282. struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
  283. const u8 *addr, gfp_t gfp)
  284. {
  285. struct ieee80211_local *local = sdata->local;
  286. struct ieee80211_hw *hw = &local->hw;
  287. struct sta_info *sta;
  288. int i;
  289. sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
  290. if (!sta)
  291. return NULL;
  292. if (ieee80211_hw_check(hw, USES_RSS)) {
  293. sta->pcpu_rx_stats =
  294. alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
  295. if (!sta->pcpu_rx_stats)
  296. goto free;
  297. }
  298. spin_lock_init(&sta->lock);
  299. spin_lock_init(&sta->ps_lock);
  300. INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
  301. INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
  302. mutex_init(&sta->ampdu_mlme.mtx);
  303. #ifdef CONFIG_MAC80211_MESH
  304. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  305. sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
  306. if (!sta->mesh)
  307. goto free;
  308. sta->mesh->plink_sta = sta;
  309. spin_lock_init(&sta->mesh->plink_lock);
  310. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  311. !sdata->u.mesh.user_mpm)
  312. timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
  313. 0);
  314. sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
  315. }
  316. #endif
  317. memcpy(sta->addr, addr, ETH_ALEN);
  318. memcpy(sta->sta.addr, addr, ETH_ALEN);
  319. sta->sta.max_rx_aggregation_subframes =
  320. local->hw.max_rx_aggregation_subframes;
  321. sta->local = local;
  322. sta->sdata = sdata;
  323. sta->rx_stats.last_rx = jiffies;
  324. u64_stats_init(&sta->rx_stats.syncp);
  325. sta->sta_state = IEEE80211_STA_NONE;
  326. /* Mark TID as unreserved */
  327. sta->reserved_tid = IEEE80211_TID_UNRESERVED;
  328. sta->last_connected = ktime_get_seconds();
  329. ewma_signal_init(&sta->rx_stats_avg.signal);
  330. ewma_avg_signal_init(&sta->status_stats.avg_ack_signal);
  331. for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
  332. ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
  333. if (local->ops->wake_tx_queue) {
  334. void *txq_data;
  335. int size = sizeof(struct txq_info) +
  336. ALIGN(hw->txq_data_size, sizeof(void *));
  337. txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
  338. if (!txq_data)
  339. goto free;
  340. for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
  341. struct txq_info *txq = txq_data + i * size;
  342. ieee80211_txq_init(sdata, sta, txq, i);
  343. }
  344. }
  345. if (sta_prepare_rate_control(local, sta, gfp))
  346. goto free_txq;
  347. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  348. skb_queue_head_init(&sta->ps_tx_buf[i]);
  349. skb_queue_head_init(&sta->tx_filtered[i]);
  350. }
  351. for (i = 0; i < IEEE80211_NUM_TIDS; i++)
  352. sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
  353. sta->sta.smps_mode = IEEE80211_SMPS_OFF;
  354. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  355. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  356. struct ieee80211_supported_band *sband;
  357. u8 smps;
  358. sband = ieee80211_get_sband(sdata);
  359. if (!sband)
  360. goto free_txq;
  361. smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
  362. IEEE80211_HT_CAP_SM_PS_SHIFT;
  363. /*
  364. * Assume that hostapd advertises our caps in the beacon and
  365. * this is the known_smps_mode for a station that just assciated
  366. */
  367. switch (smps) {
  368. case WLAN_HT_SMPS_CONTROL_DISABLED:
  369. sta->known_smps_mode = IEEE80211_SMPS_OFF;
  370. break;
  371. case WLAN_HT_SMPS_CONTROL_STATIC:
  372. sta->known_smps_mode = IEEE80211_SMPS_STATIC;
  373. break;
  374. case WLAN_HT_SMPS_CONTROL_DYNAMIC:
  375. sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
  376. break;
  377. default:
  378. WARN_ON(1);
  379. }
  380. }
  381. sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
  382. sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
  383. sta->cparams.target = MS2TIME(20);
  384. sta->cparams.interval = MS2TIME(100);
  385. sta->cparams.ecn = true;
  386. sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
  387. return sta;
  388. free_txq:
  389. if (sta->sta.txq[0])
  390. kfree(to_txq_info(sta->sta.txq[0]));
  391. free:
  392. free_percpu(sta->pcpu_rx_stats);
  393. #ifdef CONFIG_MAC80211_MESH
  394. kfree(sta->mesh);
  395. #endif
  396. kfree(sta);
  397. return NULL;
  398. }
  399. static int sta_info_insert_check(struct sta_info *sta)
  400. {
  401. struct ieee80211_sub_if_data *sdata = sta->sdata;
  402. /*
  403. * Can't be a WARN_ON because it can be triggered through a race:
  404. * something inserts a STA (on one CPU) without holding the RTNL
  405. * and another CPU turns off the net device.
  406. */
  407. if (unlikely(!ieee80211_sdata_running(sdata)))
  408. return -ENETDOWN;
  409. if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
  410. is_multicast_ether_addr(sta->sta.addr)))
  411. return -EINVAL;
  412. /* The RCU read lock is required by rhashtable due to
  413. * asynchronous resize/rehash. We also require the mutex
  414. * for correctness.
  415. */
  416. rcu_read_lock();
  417. lockdep_assert_held(&sdata->local->sta_mtx);
  418. if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
  419. ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
  420. rcu_read_unlock();
  421. return -ENOTUNIQ;
  422. }
  423. rcu_read_unlock();
  424. return 0;
  425. }
  426. static int sta_info_insert_drv_state(struct ieee80211_local *local,
  427. struct ieee80211_sub_if_data *sdata,
  428. struct sta_info *sta)
  429. {
  430. enum ieee80211_sta_state state;
  431. int err = 0;
  432. for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
  433. err = drv_sta_state(local, sdata, sta, state, state + 1);
  434. if (err)
  435. break;
  436. }
  437. if (!err) {
  438. /*
  439. * Drivers using legacy sta_add/sta_remove callbacks only
  440. * get uploaded set to true after sta_add is called.
  441. */
  442. if (!local->ops->sta_add)
  443. sta->uploaded = true;
  444. return 0;
  445. }
  446. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  447. sdata_info(sdata,
  448. "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
  449. sta->sta.addr, state + 1, err);
  450. err = 0;
  451. }
  452. /* unwind on error */
  453. for (; state > IEEE80211_STA_NOTEXIST; state--)
  454. WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
  455. return err;
  456. }
  457. static void
  458. ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
  459. {
  460. struct ieee80211_local *local = sdata->local;
  461. bool allow_p2p_go_ps = sdata->vif.p2p;
  462. struct sta_info *sta;
  463. rcu_read_lock();
  464. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  465. if (sdata != sta->sdata ||
  466. !test_sta_flag(sta, WLAN_STA_ASSOC))
  467. continue;
  468. if (!sta->sta.support_p2p_ps) {
  469. allow_p2p_go_ps = false;
  470. break;
  471. }
  472. }
  473. rcu_read_unlock();
  474. if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
  475. sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
  476. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
  477. }
  478. }
  479. /*
  480. * should be called with sta_mtx locked
  481. * this function replaces the mutex lock
  482. * with a RCU lock
  483. */
  484. static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
  485. {
  486. struct ieee80211_local *local = sta->local;
  487. struct ieee80211_sub_if_data *sdata = sta->sdata;
  488. struct station_info *sinfo = NULL;
  489. int err = 0;
  490. lockdep_assert_held(&local->sta_mtx);
  491. /* check if STA exists already */
  492. if (sta_info_get_bss(sdata, sta->sta.addr)) {
  493. err = -EEXIST;
  494. goto out_err;
  495. }
  496. sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
  497. if (!sinfo) {
  498. err = -ENOMEM;
  499. goto out_err;
  500. }
  501. local->num_sta++;
  502. local->sta_generation++;
  503. smp_mb();
  504. /* simplify things and don't accept BA sessions yet */
  505. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  506. /* make the station visible */
  507. err = sta_info_hash_add(local, sta);
  508. if (err)
  509. goto out_drop_sta;
  510. list_add_tail_rcu(&sta->list, &local->sta_list);
  511. /* notify driver */
  512. err = sta_info_insert_drv_state(local, sdata, sta);
  513. if (err)
  514. goto out_remove;
  515. set_sta_flag(sta, WLAN_STA_INSERTED);
  516. if (sta->sta_state >= IEEE80211_STA_ASSOC) {
  517. ieee80211_recalc_min_chandef(sta->sdata);
  518. if (!sta->sta.support_p2p_ps)
  519. ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
  520. }
  521. /* accept BA sessions now */
  522. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  523. ieee80211_sta_debugfs_add(sta);
  524. rate_control_add_sta_debugfs(sta);
  525. sinfo->generation = local->sta_generation;
  526. cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
  527. kfree(sinfo);
  528. sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
  529. /* move reference to rcu-protected */
  530. rcu_read_lock();
  531. mutex_unlock(&local->sta_mtx);
  532. if (ieee80211_vif_is_mesh(&sdata->vif))
  533. mesh_accept_plinks_update(sdata);
  534. return 0;
  535. out_remove:
  536. sta_info_hash_del(local, sta);
  537. list_del_rcu(&sta->list);
  538. out_drop_sta:
  539. local->num_sta--;
  540. synchronize_net();
  541. cleanup_single_sta(sta);
  542. out_err:
  543. mutex_unlock(&local->sta_mtx);
  544. kfree(sinfo);
  545. rcu_read_lock();
  546. return err;
  547. }
  548. int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
  549. {
  550. struct ieee80211_local *local = sta->local;
  551. int err;
  552. might_sleep();
  553. mutex_lock(&local->sta_mtx);
  554. err = sta_info_insert_check(sta);
  555. if (err) {
  556. sta_info_free(local, sta);
  557. mutex_unlock(&local->sta_mtx);
  558. rcu_read_lock();
  559. return err;
  560. }
  561. return sta_info_insert_finish(sta);
  562. }
  563. int sta_info_insert(struct sta_info *sta)
  564. {
  565. int err = sta_info_insert_rcu(sta);
  566. rcu_read_unlock();
  567. return err;
  568. }
  569. static inline void __bss_tim_set(u8 *tim, u16 id)
  570. {
  571. /*
  572. * This format has been mandated by the IEEE specifications,
  573. * so this line may not be changed to use the __set_bit() format.
  574. */
  575. tim[id / 8] |= (1 << (id % 8));
  576. }
  577. static inline void __bss_tim_clear(u8 *tim, u16 id)
  578. {
  579. /*
  580. * This format has been mandated by the IEEE specifications,
  581. * so this line may not be changed to use the __clear_bit() format.
  582. */
  583. tim[id / 8] &= ~(1 << (id % 8));
  584. }
  585. static inline bool __bss_tim_get(u8 *tim, u16 id)
  586. {
  587. /*
  588. * This format has been mandated by the IEEE specifications,
  589. * so this line may not be changed to use the test_bit() format.
  590. */
  591. return tim[id / 8] & (1 << (id % 8));
  592. }
  593. static unsigned long ieee80211_tids_for_ac(int ac)
  594. {
  595. /* If we ever support TIDs > 7, this obviously needs to be adjusted */
  596. switch (ac) {
  597. case IEEE80211_AC_VO:
  598. return BIT(6) | BIT(7);
  599. case IEEE80211_AC_VI:
  600. return BIT(4) | BIT(5);
  601. case IEEE80211_AC_BE:
  602. return BIT(0) | BIT(3);
  603. case IEEE80211_AC_BK:
  604. return BIT(1) | BIT(2);
  605. default:
  606. WARN_ON(1);
  607. return 0;
  608. }
  609. }
  610. static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
  611. {
  612. struct ieee80211_local *local = sta->local;
  613. struct ps_data *ps;
  614. bool indicate_tim = false;
  615. u8 ignore_for_tim = sta->sta.uapsd_queues;
  616. int ac;
  617. u16 id = sta->sta.aid;
  618. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  619. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  620. if (WARN_ON_ONCE(!sta->sdata->bss))
  621. return;
  622. ps = &sta->sdata->bss->ps;
  623. #ifdef CONFIG_MAC80211_MESH
  624. } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
  625. ps = &sta->sdata->u.mesh.ps;
  626. #endif
  627. } else {
  628. return;
  629. }
  630. /* No need to do anything if the driver does all */
  631. if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
  632. return;
  633. if (sta->dead)
  634. goto done;
  635. /*
  636. * If all ACs are delivery-enabled then we should build
  637. * the TIM bit for all ACs anyway; if only some are then
  638. * we ignore those and build the TIM bit using only the
  639. * non-enabled ones.
  640. */
  641. if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
  642. ignore_for_tim = 0;
  643. if (ignore_pending)
  644. ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
  645. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  646. unsigned long tids;
  647. if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
  648. continue;
  649. indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
  650. !skb_queue_empty(&sta->ps_tx_buf[ac]);
  651. if (indicate_tim)
  652. break;
  653. tids = ieee80211_tids_for_ac(ac);
  654. indicate_tim |=
  655. sta->driver_buffered_tids & tids;
  656. indicate_tim |=
  657. sta->txq_buffered_tids & tids;
  658. }
  659. done:
  660. spin_lock_bh(&local->tim_lock);
  661. if (indicate_tim == __bss_tim_get(ps->tim, id))
  662. goto out_unlock;
  663. if (indicate_tim)
  664. __bss_tim_set(ps->tim, id);
  665. else
  666. __bss_tim_clear(ps->tim, id);
  667. if (local->ops->set_tim && !WARN_ON(sta->dead)) {
  668. local->tim_in_locked_section = true;
  669. drv_set_tim(local, &sta->sta, indicate_tim);
  670. local->tim_in_locked_section = false;
  671. }
  672. out_unlock:
  673. spin_unlock_bh(&local->tim_lock);
  674. }
  675. void sta_info_recalc_tim(struct sta_info *sta)
  676. {
  677. __sta_info_recalc_tim(sta, false);
  678. }
  679. static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
  680. {
  681. struct ieee80211_tx_info *info;
  682. int timeout;
  683. if (!skb)
  684. return false;
  685. info = IEEE80211_SKB_CB(skb);
  686. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  687. timeout = (sta->listen_interval *
  688. sta->sdata->vif.bss_conf.beacon_int *
  689. 32 / 15625) * HZ;
  690. if (timeout < STA_TX_BUFFER_EXPIRE)
  691. timeout = STA_TX_BUFFER_EXPIRE;
  692. return time_after(jiffies, info->control.jiffies + timeout);
  693. }
  694. static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
  695. struct sta_info *sta, int ac)
  696. {
  697. unsigned long flags;
  698. struct sk_buff *skb;
  699. /*
  700. * First check for frames that should expire on the filtered
  701. * queue. Frames here were rejected by the driver and are on
  702. * a separate queue to avoid reordering with normal PS-buffered
  703. * frames. They also aren't accounted for right now in the
  704. * total_ps_buffered counter.
  705. */
  706. for (;;) {
  707. spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
  708. skb = skb_peek(&sta->tx_filtered[ac]);
  709. if (sta_info_buffer_expired(sta, skb))
  710. skb = __skb_dequeue(&sta->tx_filtered[ac]);
  711. else
  712. skb = NULL;
  713. spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
  714. /*
  715. * Frames are queued in order, so if this one
  716. * hasn't expired yet we can stop testing. If
  717. * we actually reached the end of the queue we
  718. * also need to stop, of course.
  719. */
  720. if (!skb)
  721. break;
  722. ieee80211_free_txskb(&local->hw, skb);
  723. }
  724. /*
  725. * Now also check the normal PS-buffered queue, this will
  726. * only find something if the filtered queue was emptied
  727. * since the filtered frames are all before the normal PS
  728. * buffered frames.
  729. */
  730. for (;;) {
  731. spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
  732. skb = skb_peek(&sta->ps_tx_buf[ac]);
  733. if (sta_info_buffer_expired(sta, skb))
  734. skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
  735. else
  736. skb = NULL;
  737. spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
  738. /*
  739. * frames are queued in order, so if this one
  740. * hasn't expired yet (or we reached the end of
  741. * the queue) we can stop testing
  742. */
  743. if (!skb)
  744. break;
  745. local->total_ps_buffered--;
  746. ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
  747. sta->sta.addr);
  748. ieee80211_free_txskb(&local->hw, skb);
  749. }
  750. /*
  751. * Finally, recalculate the TIM bit for this station -- it might
  752. * now be clear because the station was too slow to retrieve its
  753. * frames.
  754. */
  755. sta_info_recalc_tim(sta);
  756. /*
  757. * Return whether there are any frames still buffered, this is
  758. * used to check whether the cleanup timer still needs to run,
  759. * if there are no frames we don't need to rearm the timer.
  760. */
  761. return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
  762. skb_queue_empty(&sta->tx_filtered[ac]));
  763. }
  764. static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  765. struct sta_info *sta)
  766. {
  767. bool have_buffered = false;
  768. int ac;
  769. /* This is only necessary for stations on BSS/MBSS interfaces */
  770. if (!sta->sdata->bss &&
  771. !ieee80211_vif_is_mesh(&sta->sdata->vif))
  772. return false;
  773. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  774. have_buffered |=
  775. sta_info_cleanup_expire_buffered_ac(local, sta, ac);
  776. return have_buffered;
  777. }
  778. static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
  779. {
  780. struct ieee80211_local *local;
  781. struct ieee80211_sub_if_data *sdata;
  782. int ret;
  783. might_sleep();
  784. if (!sta)
  785. return -ENOENT;
  786. local = sta->local;
  787. sdata = sta->sdata;
  788. lockdep_assert_held(&local->sta_mtx);
  789. /*
  790. * Before removing the station from the driver and
  791. * rate control, it might still start new aggregation
  792. * sessions -- block that to make sure the tear-down
  793. * will be sufficient.
  794. */
  795. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  796. ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
  797. /*
  798. * Before removing the station from the driver there might be pending
  799. * rx frames on RSS queues sent prior to the disassociation - wait for
  800. * all such frames to be processed.
  801. */
  802. drv_sync_rx_queues(local, sta);
  803. ret = sta_info_hash_del(local, sta);
  804. if (WARN_ON(ret))
  805. return ret;
  806. /*
  807. * for TDLS peers, make sure to return to the base channel before
  808. * removal.
  809. */
  810. if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
  811. drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
  812. clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
  813. }
  814. list_del_rcu(&sta->list);
  815. sta->removed = true;
  816. drv_sta_pre_rcu_remove(local, sta->sdata, sta);
  817. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  818. rcu_access_pointer(sdata->u.vlan.sta) == sta)
  819. RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
  820. return 0;
  821. }
  822. static void __sta_info_destroy_part2(struct sta_info *sta)
  823. {
  824. struct ieee80211_local *local = sta->local;
  825. struct ieee80211_sub_if_data *sdata = sta->sdata;
  826. struct station_info *sinfo;
  827. int ret;
  828. /*
  829. * NOTE: This assumes at least synchronize_net() was done
  830. * after _part1 and before _part2!
  831. */
  832. might_sleep();
  833. lockdep_assert_held(&local->sta_mtx);
  834. if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
  835. ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
  836. WARN_ON_ONCE(ret);
  837. }
  838. /* now keys can no longer be reached */
  839. ieee80211_free_sta_keys(local, sta);
  840. /* disable TIM bit - last chance to tell driver */
  841. __sta_info_recalc_tim(sta, true);
  842. sta->dead = true;
  843. local->num_sta--;
  844. local->sta_generation++;
  845. while (sta->sta_state > IEEE80211_STA_NONE) {
  846. ret = sta_info_move_state(sta, sta->sta_state - 1);
  847. if (ret) {
  848. WARN_ON_ONCE(1);
  849. break;
  850. }
  851. }
  852. if (sta->uploaded) {
  853. ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
  854. IEEE80211_STA_NOTEXIST);
  855. WARN_ON_ONCE(ret != 0);
  856. }
  857. sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
  858. sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
  859. if (sinfo)
  860. sta_set_sinfo(sta, sinfo, true);
  861. cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
  862. kfree(sinfo);
  863. rate_control_remove_sta_debugfs(sta);
  864. ieee80211_sta_debugfs_remove(sta);
  865. cleanup_single_sta(sta);
  866. }
  867. int __must_check __sta_info_destroy(struct sta_info *sta)
  868. {
  869. int err = __sta_info_destroy_part1(sta);
  870. if (err)
  871. return err;
  872. synchronize_net();
  873. __sta_info_destroy_part2(sta);
  874. return 0;
  875. }
  876. int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
  877. {
  878. struct sta_info *sta;
  879. int ret;
  880. mutex_lock(&sdata->local->sta_mtx);
  881. sta = sta_info_get(sdata, addr);
  882. ret = __sta_info_destroy(sta);
  883. mutex_unlock(&sdata->local->sta_mtx);
  884. return ret;
  885. }
  886. int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
  887. const u8 *addr)
  888. {
  889. struct sta_info *sta;
  890. int ret;
  891. mutex_lock(&sdata->local->sta_mtx);
  892. sta = sta_info_get_bss(sdata, addr);
  893. ret = __sta_info_destroy(sta);
  894. mutex_unlock(&sdata->local->sta_mtx);
  895. return ret;
  896. }
  897. static void sta_info_cleanup(struct timer_list *t)
  898. {
  899. struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
  900. struct sta_info *sta;
  901. bool timer_needed = false;
  902. rcu_read_lock();
  903. list_for_each_entry_rcu(sta, &local->sta_list, list)
  904. if (sta_info_cleanup_expire_buffered(local, sta))
  905. timer_needed = true;
  906. rcu_read_unlock();
  907. if (local->quiescing)
  908. return;
  909. if (!timer_needed)
  910. return;
  911. mod_timer(&local->sta_cleanup,
  912. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
  913. }
  914. int sta_info_init(struct ieee80211_local *local)
  915. {
  916. int err;
  917. err = rhltable_init(&local->sta_hash, &sta_rht_params);
  918. if (err)
  919. return err;
  920. spin_lock_init(&local->tim_lock);
  921. mutex_init(&local->sta_mtx);
  922. INIT_LIST_HEAD(&local->sta_list);
  923. timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
  924. return 0;
  925. }
  926. void sta_info_stop(struct ieee80211_local *local)
  927. {
  928. del_timer_sync(&local->sta_cleanup);
  929. rhltable_destroy(&local->sta_hash);
  930. }
  931. int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
  932. {
  933. struct ieee80211_local *local = sdata->local;
  934. struct sta_info *sta, *tmp;
  935. LIST_HEAD(free_list);
  936. int ret = 0;
  937. might_sleep();
  938. WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
  939. WARN_ON(vlans && !sdata->bss);
  940. mutex_lock(&local->sta_mtx);
  941. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  942. if (sdata == sta->sdata ||
  943. (vlans && sdata->bss == sta->sdata->bss)) {
  944. if (!WARN_ON(__sta_info_destroy_part1(sta)))
  945. list_add(&sta->free_list, &free_list);
  946. ret++;
  947. }
  948. }
  949. if (!list_empty(&free_list)) {
  950. synchronize_net();
  951. list_for_each_entry_safe(sta, tmp, &free_list, free_list)
  952. __sta_info_destroy_part2(sta);
  953. }
  954. mutex_unlock(&local->sta_mtx);
  955. return ret;
  956. }
  957. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  958. unsigned long exp_time)
  959. {
  960. struct ieee80211_local *local = sdata->local;
  961. struct sta_info *sta, *tmp;
  962. mutex_lock(&local->sta_mtx);
  963. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  964. unsigned long last_active = ieee80211_sta_last_active(sta);
  965. if (sdata != sta->sdata)
  966. continue;
  967. if (time_is_before_jiffies(last_active + exp_time)) {
  968. sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
  969. sta->sta.addr);
  970. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  971. test_sta_flag(sta, WLAN_STA_PS_STA))
  972. atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
  973. WARN_ON(__sta_info_destroy(sta));
  974. }
  975. }
  976. mutex_unlock(&local->sta_mtx);
  977. }
  978. struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
  979. const u8 *addr,
  980. const u8 *localaddr)
  981. {
  982. struct ieee80211_local *local = hw_to_local(hw);
  983. struct rhlist_head *tmp;
  984. struct sta_info *sta;
  985. /*
  986. * Just return a random station if localaddr is NULL
  987. * ... first in list.
  988. */
  989. for_each_sta_info(local, addr, sta, tmp) {
  990. if (localaddr &&
  991. !ether_addr_equal(sta->sdata->vif.addr, localaddr))
  992. continue;
  993. if (!sta->uploaded)
  994. return NULL;
  995. return &sta->sta;
  996. }
  997. return NULL;
  998. }
  999. EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
  1000. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
  1001. const u8 *addr)
  1002. {
  1003. struct sta_info *sta;
  1004. if (!vif)
  1005. return NULL;
  1006. sta = sta_info_get_bss(vif_to_sdata(vif), addr);
  1007. if (!sta)
  1008. return NULL;
  1009. if (!sta->uploaded)
  1010. return NULL;
  1011. return &sta->sta;
  1012. }
  1013. EXPORT_SYMBOL(ieee80211_find_sta);
  1014. /* powersave support code */
  1015. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  1016. {
  1017. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1018. struct ieee80211_local *local = sdata->local;
  1019. struct sk_buff_head pending;
  1020. int filtered = 0, buffered = 0, ac, i;
  1021. unsigned long flags;
  1022. struct ps_data *ps;
  1023. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1024. sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
  1025. u.ap);
  1026. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1027. ps = &sdata->bss->ps;
  1028. else if (ieee80211_vif_is_mesh(&sdata->vif))
  1029. ps = &sdata->u.mesh.ps;
  1030. else
  1031. return;
  1032. clear_sta_flag(sta, WLAN_STA_SP);
  1033. BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
  1034. sta->driver_buffered_tids = 0;
  1035. sta->txq_buffered_tids = 0;
  1036. if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
  1037. drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
  1038. if (sta->sta.txq[0]) {
  1039. for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
  1040. if (!txq_has_queue(sta->sta.txq[i]))
  1041. continue;
  1042. drv_wake_tx_queue(local, to_txq_info(sta->sta.txq[i]));
  1043. }
  1044. }
  1045. skb_queue_head_init(&pending);
  1046. /* sync with ieee80211_tx_h_unicast_ps_buf */
  1047. spin_lock(&sta->ps_lock);
  1048. /* Send all buffered frames to the station */
  1049. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  1050. int count = skb_queue_len(&pending), tmp;
  1051. spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
  1052. skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
  1053. spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
  1054. tmp = skb_queue_len(&pending);
  1055. filtered += tmp - count;
  1056. count = tmp;
  1057. spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
  1058. skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
  1059. spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
  1060. tmp = skb_queue_len(&pending);
  1061. buffered += tmp - count;
  1062. }
  1063. ieee80211_add_pending_skbs(local, &pending);
  1064. /* now we're no longer in the deliver code */
  1065. clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
  1066. /* The station might have polled and then woken up before we responded,
  1067. * so clear these flags now to avoid them sticking around.
  1068. */
  1069. clear_sta_flag(sta, WLAN_STA_PSPOLL);
  1070. clear_sta_flag(sta, WLAN_STA_UAPSD);
  1071. spin_unlock(&sta->ps_lock);
  1072. atomic_dec(&ps->num_sta_ps);
  1073. /* This station just woke up and isn't aware of our SMPS state */
  1074. if (!ieee80211_vif_is_mesh(&sdata->vif) &&
  1075. !ieee80211_smps_is_restrictive(sta->known_smps_mode,
  1076. sdata->smps_mode) &&
  1077. sta->known_smps_mode != sdata->bss->req_smps &&
  1078. sta_info_tx_streams(sta) != 1) {
  1079. ht_dbg(sdata,
  1080. "%pM just woke up and MIMO capable - update SMPS\n",
  1081. sta->sta.addr);
  1082. ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
  1083. sta->sta.addr,
  1084. sdata->vif.bss_conf.bssid);
  1085. }
  1086. local->total_ps_buffered -= buffered;
  1087. sta_info_recalc_tim(sta);
  1088. ps_dbg(sdata,
  1089. "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
  1090. sta->sta.addr, sta->sta.aid, filtered, buffered);
  1091. ieee80211_check_fast_xmit(sta);
  1092. }
  1093. static void ieee80211_send_null_response(struct sta_info *sta, int tid,
  1094. enum ieee80211_frame_release_type reason,
  1095. bool call_driver, bool more_data)
  1096. {
  1097. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1098. struct ieee80211_local *local = sdata->local;
  1099. struct ieee80211_qos_hdr *nullfunc;
  1100. struct sk_buff *skb;
  1101. int size = sizeof(*nullfunc);
  1102. __le16 fc;
  1103. bool qos = sta->sta.wme;
  1104. struct ieee80211_tx_info *info;
  1105. struct ieee80211_chanctx_conf *chanctx_conf;
  1106. /* Don't send NDPs when STA is connected HE */
  1107. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  1108. !(sdata->u.mgd.flags & IEEE80211_STA_DISABLE_HE))
  1109. return;
  1110. if (qos) {
  1111. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  1112. IEEE80211_STYPE_QOS_NULLFUNC |
  1113. IEEE80211_FCTL_FROMDS);
  1114. } else {
  1115. size -= 2;
  1116. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  1117. IEEE80211_STYPE_NULLFUNC |
  1118. IEEE80211_FCTL_FROMDS);
  1119. }
  1120. skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
  1121. if (!skb)
  1122. return;
  1123. skb_reserve(skb, local->hw.extra_tx_headroom);
  1124. nullfunc = skb_put(skb, size);
  1125. nullfunc->frame_control = fc;
  1126. nullfunc->duration_id = 0;
  1127. memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
  1128. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  1129. memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
  1130. nullfunc->seq_ctrl = 0;
  1131. skb->priority = tid;
  1132. skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
  1133. if (qos) {
  1134. nullfunc->qos_ctrl = cpu_to_le16(tid);
  1135. if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
  1136. nullfunc->qos_ctrl |=
  1137. cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
  1138. if (more_data)
  1139. nullfunc->frame_control |=
  1140. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1141. }
  1142. }
  1143. info = IEEE80211_SKB_CB(skb);
  1144. /*
  1145. * Tell TX path to send this frame even though the
  1146. * STA may still remain is PS mode after this frame
  1147. * exchange. Also set EOSP to indicate this packet
  1148. * ends the poll/service period.
  1149. */
  1150. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
  1151. IEEE80211_TX_STATUS_EOSP |
  1152. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1153. info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
  1154. if (call_driver)
  1155. drv_allow_buffered_frames(local, sta, BIT(tid), 1,
  1156. reason, false);
  1157. skb->dev = sdata->dev;
  1158. rcu_read_lock();
  1159. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  1160. if (WARN_ON(!chanctx_conf)) {
  1161. rcu_read_unlock();
  1162. kfree_skb(skb);
  1163. return;
  1164. }
  1165. info->band = chanctx_conf->def.chan->band;
  1166. ieee80211_xmit(sdata, sta, skb, 0);
  1167. rcu_read_unlock();
  1168. }
  1169. static int find_highest_prio_tid(unsigned long tids)
  1170. {
  1171. /* lower 3 TIDs aren't ordered perfectly */
  1172. if (tids & 0xF8)
  1173. return fls(tids) - 1;
  1174. /* TID 0 is BE just like TID 3 */
  1175. if (tids & BIT(0))
  1176. return 0;
  1177. return fls(tids) - 1;
  1178. }
  1179. /* Indicates if the MORE_DATA bit should be set in the last
  1180. * frame obtained by ieee80211_sta_ps_get_frames.
  1181. * Note that driver_release_tids is relevant only if
  1182. * reason = IEEE80211_FRAME_RELEASE_PSPOLL
  1183. */
  1184. static bool
  1185. ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
  1186. enum ieee80211_frame_release_type reason,
  1187. unsigned long driver_release_tids)
  1188. {
  1189. int ac;
  1190. /* If the driver has data on more than one TID then
  1191. * certainly there's more data if we release just a
  1192. * single frame now (from a single TID). This will
  1193. * only happen for PS-Poll.
  1194. */
  1195. if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
  1196. hweight16(driver_release_tids) > 1)
  1197. return true;
  1198. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  1199. if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
  1200. continue;
  1201. if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
  1202. !skb_queue_empty(&sta->ps_tx_buf[ac]))
  1203. return true;
  1204. }
  1205. return false;
  1206. }
  1207. static void
  1208. ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
  1209. enum ieee80211_frame_release_type reason,
  1210. struct sk_buff_head *frames,
  1211. unsigned long *driver_release_tids)
  1212. {
  1213. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1214. struct ieee80211_local *local = sdata->local;
  1215. int ac;
  1216. /* Get response frame(s) and more data bit for the last one. */
  1217. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  1218. unsigned long tids;
  1219. if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
  1220. continue;
  1221. tids = ieee80211_tids_for_ac(ac);
  1222. /* if we already have frames from software, then we can't also
  1223. * release from hardware queues
  1224. */
  1225. if (skb_queue_empty(frames)) {
  1226. *driver_release_tids |=
  1227. sta->driver_buffered_tids & tids;
  1228. *driver_release_tids |= sta->txq_buffered_tids & tids;
  1229. }
  1230. if (!*driver_release_tids) {
  1231. struct sk_buff *skb;
  1232. while (n_frames > 0) {
  1233. skb = skb_dequeue(&sta->tx_filtered[ac]);
  1234. if (!skb) {
  1235. skb = skb_dequeue(
  1236. &sta->ps_tx_buf[ac]);
  1237. if (skb)
  1238. local->total_ps_buffered--;
  1239. }
  1240. if (!skb)
  1241. break;
  1242. n_frames--;
  1243. __skb_queue_tail(frames, skb);
  1244. }
  1245. }
  1246. /* If we have more frames buffered on this AC, then abort the
  1247. * loop since we can't send more data from other ACs before
  1248. * the buffered frames from this.
  1249. */
  1250. if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
  1251. !skb_queue_empty(&sta->ps_tx_buf[ac]))
  1252. break;
  1253. }
  1254. }
  1255. static void
  1256. ieee80211_sta_ps_deliver_response(struct sta_info *sta,
  1257. int n_frames, u8 ignored_acs,
  1258. enum ieee80211_frame_release_type reason)
  1259. {
  1260. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1261. struct ieee80211_local *local = sdata->local;
  1262. unsigned long driver_release_tids = 0;
  1263. struct sk_buff_head frames;
  1264. bool more_data;
  1265. /* Service or PS-Poll period starts */
  1266. set_sta_flag(sta, WLAN_STA_SP);
  1267. __skb_queue_head_init(&frames);
  1268. ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
  1269. &frames, &driver_release_tids);
  1270. more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
  1271. if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
  1272. driver_release_tids =
  1273. BIT(find_highest_prio_tid(driver_release_tids));
  1274. if (skb_queue_empty(&frames) && !driver_release_tids) {
  1275. int tid, ac;
  1276. /*
  1277. * For PS-Poll, this can only happen due to a race condition
  1278. * when we set the TIM bit and the station notices it, but
  1279. * before it can poll for the frame we expire it.
  1280. *
  1281. * For uAPSD, this is said in the standard (11.2.1.5 h):
  1282. * At each unscheduled SP for a non-AP STA, the AP shall
  1283. * attempt to transmit at least one MSDU or MMPDU, but no
  1284. * more than the value specified in the Max SP Length field
  1285. * in the QoS Capability element from delivery-enabled ACs,
  1286. * that are destined for the non-AP STA.
  1287. *
  1288. * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
  1289. */
  1290. /* This will evaluate to 1, 3, 5 or 7. */
  1291. for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
  1292. if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
  1293. break;
  1294. tid = 7 - 2 * ac;
  1295. ieee80211_send_null_response(sta, tid, reason, true, false);
  1296. } else if (!driver_release_tids) {
  1297. struct sk_buff_head pending;
  1298. struct sk_buff *skb;
  1299. int num = 0;
  1300. u16 tids = 0;
  1301. bool need_null = false;
  1302. skb_queue_head_init(&pending);
  1303. while ((skb = __skb_dequeue(&frames))) {
  1304. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1305. struct ieee80211_hdr *hdr = (void *) skb->data;
  1306. u8 *qoshdr = NULL;
  1307. num++;
  1308. /*
  1309. * Tell TX path to send this frame even though the
  1310. * STA may still remain is PS mode after this frame
  1311. * exchange.
  1312. */
  1313. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
  1314. info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
  1315. /*
  1316. * Use MoreData flag to indicate whether there are
  1317. * more buffered frames for this STA
  1318. */
  1319. if (more_data || !skb_queue_empty(&frames))
  1320. hdr->frame_control |=
  1321. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1322. else
  1323. hdr->frame_control &=
  1324. cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  1325. if (ieee80211_is_data_qos(hdr->frame_control) ||
  1326. ieee80211_is_qos_nullfunc(hdr->frame_control))
  1327. qoshdr = ieee80211_get_qos_ctl(hdr);
  1328. tids |= BIT(skb->priority);
  1329. __skb_queue_tail(&pending, skb);
  1330. /* end service period after last frame or add one */
  1331. if (!skb_queue_empty(&frames))
  1332. continue;
  1333. if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
  1334. /* for PS-Poll, there's only one frame */
  1335. info->flags |= IEEE80211_TX_STATUS_EOSP |
  1336. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1337. break;
  1338. }
  1339. /* For uAPSD, things are a bit more complicated. If the
  1340. * last frame has a QoS header (i.e. is a QoS-data or
  1341. * QoS-nulldata frame) then just set the EOSP bit there
  1342. * and be done.
  1343. * If the frame doesn't have a QoS header (which means
  1344. * it should be a bufferable MMPDU) then we can't set
  1345. * the EOSP bit in the QoS header; add a QoS-nulldata
  1346. * frame to the list to send it after the MMPDU.
  1347. *
  1348. * Note that this code is only in the mac80211-release
  1349. * code path, we assume that the driver will not buffer
  1350. * anything but QoS-data frames, or if it does, will
  1351. * create the QoS-nulldata frame by itself if needed.
  1352. *
  1353. * Cf. 802.11-2012 10.2.1.10 (c).
  1354. */
  1355. if (qoshdr) {
  1356. *qoshdr |= IEEE80211_QOS_CTL_EOSP;
  1357. info->flags |= IEEE80211_TX_STATUS_EOSP |
  1358. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1359. } else {
  1360. /* The standard isn't completely clear on this
  1361. * as it says the more-data bit should be set
  1362. * if there are more BUs. The QoS-Null frame
  1363. * we're about to send isn't buffered yet, we
  1364. * only create it below, but let's pretend it
  1365. * was buffered just in case some clients only
  1366. * expect more-data=0 when eosp=1.
  1367. */
  1368. hdr->frame_control |=
  1369. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1370. need_null = true;
  1371. num++;
  1372. }
  1373. break;
  1374. }
  1375. drv_allow_buffered_frames(local, sta, tids, num,
  1376. reason, more_data);
  1377. ieee80211_add_pending_skbs(local, &pending);
  1378. if (need_null)
  1379. ieee80211_send_null_response(
  1380. sta, find_highest_prio_tid(tids),
  1381. reason, false, false);
  1382. sta_info_recalc_tim(sta);
  1383. } else {
  1384. int tid;
  1385. /*
  1386. * We need to release a frame that is buffered somewhere in the
  1387. * driver ... it'll have to handle that.
  1388. * Note that the driver also has to check the number of frames
  1389. * on the TIDs we're releasing from - if there are more than
  1390. * n_frames it has to set the more-data bit (if we didn't ask
  1391. * it to set it anyway due to other buffered frames); if there
  1392. * are fewer than n_frames it has to make sure to adjust that
  1393. * to allow the service period to end properly.
  1394. */
  1395. drv_release_buffered_frames(local, sta, driver_release_tids,
  1396. n_frames, reason, more_data);
  1397. /*
  1398. * Note that we don't recalculate the TIM bit here as it would
  1399. * most likely have no effect at all unless the driver told us
  1400. * that the TID(s) became empty before returning here from the
  1401. * release function.
  1402. * Either way, however, when the driver tells us that the TID(s)
  1403. * became empty or we find that a txq became empty, we'll do the
  1404. * TIM recalculation.
  1405. */
  1406. if (!sta->sta.txq[0])
  1407. return;
  1408. for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
  1409. if (!(driver_release_tids & BIT(tid)) ||
  1410. txq_has_queue(sta->sta.txq[tid]))
  1411. continue;
  1412. sta_info_recalc_tim(sta);
  1413. break;
  1414. }
  1415. }
  1416. }
  1417. void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
  1418. {
  1419. u8 ignore_for_response = sta->sta.uapsd_queues;
  1420. /*
  1421. * If all ACs are delivery-enabled then we should reply
  1422. * from any of them, if only some are enabled we reply
  1423. * only from the non-enabled ones.
  1424. */
  1425. if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
  1426. ignore_for_response = 0;
  1427. ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
  1428. IEEE80211_FRAME_RELEASE_PSPOLL);
  1429. }
  1430. void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
  1431. {
  1432. int n_frames = sta->sta.max_sp;
  1433. u8 delivery_enabled = sta->sta.uapsd_queues;
  1434. /*
  1435. * If we ever grow support for TSPEC this might happen if
  1436. * the TSPEC update from hostapd comes in between a trigger
  1437. * frame setting WLAN_STA_UAPSD in the RX path and this
  1438. * actually getting called.
  1439. */
  1440. if (!delivery_enabled)
  1441. return;
  1442. switch (sta->sta.max_sp) {
  1443. case 1:
  1444. n_frames = 2;
  1445. break;
  1446. case 2:
  1447. n_frames = 4;
  1448. break;
  1449. case 3:
  1450. n_frames = 6;
  1451. break;
  1452. case 0:
  1453. /* XXX: what is a good value? */
  1454. n_frames = 128;
  1455. break;
  1456. }
  1457. ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
  1458. IEEE80211_FRAME_RELEASE_UAPSD);
  1459. }
  1460. void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
  1461. struct ieee80211_sta *pubsta, bool block)
  1462. {
  1463. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1464. trace_api_sta_block_awake(sta->local, pubsta, block);
  1465. if (block) {
  1466. set_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1467. ieee80211_clear_fast_xmit(sta);
  1468. return;
  1469. }
  1470. if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
  1471. return;
  1472. if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
  1473. set_sta_flag(sta, WLAN_STA_PS_DELIVER);
  1474. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1475. ieee80211_queue_work(hw, &sta->drv_deliver_wk);
  1476. } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
  1477. test_sta_flag(sta, WLAN_STA_UAPSD)) {
  1478. /* must be asleep in this case */
  1479. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1480. ieee80211_queue_work(hw, &sta->drv_deliver_wk);
  1481. } else {
  1482. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1483. ieee80211_check_fast_xmit(sta);
  1484. }
  1485. }
  1486. EXPORT_SYMBOL(ieee80211_sta_block_awake);
  1487. void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
  1488. {
  1489. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1490. struct ieee80211_local *local = sta->local;
  1491. trace_api_eosp(local, pubsta);
  1492. clear_sta_flag(sta, WLAN_STA_SP);
  1493. }
  1494. EXPORT_SYMBOL(ieee80211_sta_eosp);
  1495. void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
  1496. {
  1497. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1498. enum ieee80211_frame_release_type reason;
  1499. bool more_data;
  1500. trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
  1501. reason = IEEE80211_FRAME_RELEASE_UAPSD;
  1502. more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
  1503. reason, 0);
  1504. ieee80211_send_null_response(sta, tid, reason, false, more_data);
  1505. }
  1506. EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
  1507. void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
  1508. u8 tid, bool buffered)
  1509. {
  1510. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1511. if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
  1512. return;
  1513. trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
  1514. if (buffered)
  1515. set_bit(tid, &sta->driver_buffered_tids);
  1516. else
  1517. clear_bit(tid, &sta->driver_buffered_tids);
  1518. sta_info_recalc_tim(sta);
  1519. }
  1520. EXPORT_SYMBOL(ieee80211_sta_set_buffered);
  1521. int sta_info_move_state(struct sta_info *sta,
  1522. enum ieee80211_sta_state new_state)
  1523. {
  1524. might_sleep();
  1525. if (sta->sta_state == new_state)
  1526. return 0;
  1527. /* check allowed transitions first */
  1528. switch (new_state) {
  1529. case IEEE80211_STA_NONE:
  1530. if (sta->sta_state != IEEE80211_STA_AUTH)
  1531. return -EINVAL;
  1532. break;
  1533. case IEEE80211_STA_AUTH:
  1534. if (sta->sta_state != IEEE80211_STA_NONE &&
  1535. sta->sta_state != IEEE80211_STA_ASSOC)
  1536. return -EINVAL;
  1537. break;
  1538. case IEEE80211_STA_ASSOC:
  1539. if (sta->sta_state != IEEE80211_STA_AUTH &&
  1540. sta->sta_state != IEEE80211_STA_AUTHORIZED)
  1541. return -EINVAL;
  1542. break;
  1543. case IEEE80211_STA_AUTHORIZED:
  1544. if (sta->sta_state != IEEE80211_STA_ASSOC)
  1545. return -EINVAL;
  1546. break;
  1547. default:
  1548. WARN(1, "invalid state %d", new_state);
  1549. return -EINVAL;
  1550. }
  1551. sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
  1552. sta->sta.addr, new_state);
  1553. /*
  1554. * notify the driver before the actual changes so it can
  1555. * fail the transition
  1556. */
  1557. if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
  1558. int err = drv_sta_state(sta->local, sta->sdata, sta,
  1559. sta->sta_state, new_state);
  1560. if (err)
  1561. return err;
  1562. }
  1563. /* reflect the change in all state variables */
  1564. switch (new_state) {
  1565. case IEEE80211_STA_NONE:
  1566. if (sta->sta_state == IEEE80211_STA_AUTH)
  1567. clear_bit(WLAN_STA_AUTH, &sta->_flags);
  1568. break;
  1569. case IEEE80211_STA_AUTH:
  1570. if (sta->sta_state == IEEE80211_STA_NONE) {
  1571. set_bit(WLAN_STA_AUTH, &sta->_flags);
  1572. } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
  1573. clear_bit(WLAN_STA_ASSOC, &sta->_flags);
  1574. ieee80211_recalc_min_chandef(sta->sdata);
  1575. if (!sta->sta.support_p2p_ps)
  1576. ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
  1577. }
  1578. break;
  1579. case IEEE80211_STA_ASSOC:
  1580. if (sta->sta_state == IEEE80211_STA_AUTH) {
  1581. set_bit(WLAN_STA_ASSOC, &sta->_flags);
  1582. ieee80211_recalc_min_chandef(sta->sdata);
  1583. if (!sta->sta.support_p2p_ps)
  1584. ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
  1585. } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
  1586. ieee80211_vif_dec_num_mcast(sta->sdata);
  1587. clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1588. ieee80211_clear_fast_xmit(sta);
  1589. ieee80211_clear_fast_rx(sta);
  1590. }
  1591. break;
  1592. case IEEE80211_STA_AUTHORIZED:
  1593. if (sta->sta_state == IEEE80211_STA_ASSOC) {
  1594. ieee80211_vif_inc_num_mcast(sta->sdata);
  1595. set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1596. ieee80211_check_fast_xmit(sta);
  1597. ieee80211_check_fast_rx(sta);
  1598. }
  1599. if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  1600. sta->sdata->vif.type == NL80211_IFTYPE_AP)
  1601. cfg80211_send_layer2_update(sta->sdata->dev,
  1602. sta->sta.addr);
  1603. break;
  1604. default:
  1605. break;
  1606. }
  1607. sta->sta_state = new_state;
  1608. return 0;
  1609. }
  1610. u8 sta_info_tx_streams(struct sta_info *sta)
  1611. {
  1612. struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
  1613. u8 rx_streams;
  1614. if (!sta->sta.ht_cap.ht_supported)
  1615. return 1;
  1616. if (sta->sta.vht_cap.vht_supported) {
  1617. int i;
  1618. u16 tx_mcs_map =
  1619. le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
  1620. for (i = 7; i >= 0; i--)
  1621. if ((tx_mcs_map & (0x3 << (i * 2))) !=
  1622. IEEE80211_VHT_MCS_NOT_SUPPORTED)
  1623. return i + 1;
  1624. }
  1625. if (ht_cap->mcs.rx_mask[3])
  1626. rx_streams = 4;
  1627. else if (ht_cap->mcs.rx_mask[2])
  1628. rx_streams = 3;
  1629. else if (ht_cap->mcs.rx_mask[1])
  1630. rx_streams = 2;
  1631. else
  1632. rx_streams = 1;
  1633. if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
  1634. return rx_streams;
  1635. return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
  1636. >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
  1637. }
  1638. static struct ieee80211_sta_rx_stats *
  1639. sta_get_last_rx_stats(struct sta_info *sta)
  1640. {
  1641. struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
  1642. struct ieee80211_local *local = sta->local;
  1643. int cpu;
  1644. if (!ieee80211_hw_check(&local->hw, USES_RSS))
  1645. return stats;
  1646. for_each_possible_cpu(cpu) {
  1647. struct ieee80211_sta_rx_stats *cpustats;
  1648. cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
  1649. if (time_after(cpustats->last_rx, stats->last_rx))
  1650. stats = cpustats;
  1651. }
  1652. return stats;
  1653. }
  1654. static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
  1655. struct rate_info *rinfo)
  1656. {
  1657. rinfo->bw = STA_STATS_GET(BW, rate);
  1658. switch (STA_STATS_GET(TYPE, rate)) {
  1659. case STA_STATS_RATE_TYPE_VHT:
  1660. rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
  1661. rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
  1662. rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
  1663. if (STA_STATS_GET(SGI, rate))
  1664. rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
  1665. break;
  1666. case STA_STATS_RATE_TYPE_HT:
  1667. rinfo->flags = RATE_INFO_FLAGS_MCS;
  1668. rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
  1669. if (STA_STATS_GET(SGI, rate))
  1670. rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
  1671. break;
  1672. case STA_STATS_RATE_TYPE_LEGACY: {
  1673. struct ieee80211_supported_band *sband;
  1674. u16 brate;
  1675. unsigned int shift;
  1676. int band = STA_STATS_GET(LEGACY_BAND, rate);
  1677. int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
  1678. sband = local->hw.wiphy->bands[band];
  1679. if (WARN_ON_ONCE(!sband->bitrates))
  1680. break;
  1681. brate = sband->bitrates[rate_idx].bitrate;
  1682. if (rinfo->bw == RATE_INFO_BW_5)
  1683. shift = 2;
  1684. else if (rinfo->bw == RATE_INFO_BW_10)
  1685. shift = 1;
  1686. else
  1687. shift = 0;
  1688. rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
  1689. break;
  1690. }
  1691. case STA_STATS_RATE_TYPE_HE:
  1692. rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
  1693. rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
  1694. rinfo->nss = STA_STATS_GET(HE_NSS, rate);
  1695. rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
  1696. rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
  1697. rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
  1698. break;
  1699. }
  1700. }
  1701. static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
  1702. {
  1703. u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
  1704. if (rate == STA_STATS_RATE_INVALID)
  1705. return -EINVAL;
  1706. sta_stats_decode_rate(sta->local, rate, rinfo);
  1707. return 0;
  1708. }
  1709. static void sta_set_tidstats(struct sta_info *sta,
  1710. struct cfg80211_tid_stats *tidstats,
  1711. int tid)
  1712. {
  1713. struct ieee80211_local *local = sta->local;
  1714. if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
  1715. unsigned int start;
  1716. do {
  1717. start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
  1718. tidstats->rx_msdu = sta->rx_stats.msdu[tid];
  1719. } while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
  1720. tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
  1721. }
  1722. if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
  1723. tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
  1724. tidstats->tx_msdu = sta->tx_stats.msdu[tid];
  1725. }
  1726. if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
  1727. ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
  1728. tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
  1729. tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
  1730. }
  1731. if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
  1732. ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
  1733. tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
  1734. tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
  1735. }
  1736. if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
  1737. spin_lock_bh(&local->fq.lock);
  1738. rcu_read_lock();
  1739. tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
  1740. ieee80211_fill_txq_stats(&tidstats->txq_stats,
  1741. to_txq_info(sta->sta.txq[tid]));
  1742. rcu_read_unlock();
  1743. spin_unlock_bh(&local->fq.lock);
  1744. }
  1745. }
  1746. static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
  1747. {
  1748. unsigned int start;
  1749. u64 value;
  1750. do {
  1751. start = u64_stats_fetch_begin(&rxstats->syncp);
  1752. value = rxstats->bytes;
  1753. } while (u64_stats_fetch_retry(&rxstats->syncp, start));
  1754. return value;
  1755. }
  1756. void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
  1757. bool tidstats)
  1758. {
  1759. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1760. struct ieee80211_local *local = sdata->local;
  1761. u32 thr = 0;
  1762. int i, ac, cpu;
  1763. struct ieee80211_sta_rx_stats *last_rxstats;
  1764. last_rxstats = sta_get_last_rx_stats(sta);
  1765. sinfo->generation = sdata->local->sta_generation;
  1766. /* do before driver, so beacon filtering drivers have a
  1767. * chance to e.g. just add the number of filtered beacons
  1768. * (or just modify the value entirely, of course)
  1769. */
  1770. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  1771. sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
  1772. drv_sta_statistics(local, sdata, &sta->sta, sinfo);
  1773. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
  1774. BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
  1775. BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
  1776. BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
  1777. BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
  1778. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  1779. sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
  1780. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
  1781. }
  1782. sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
  1783. sinfo->inactive_time =
  1784. jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
  1785. if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
  1786. BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
  1787. sinfo->tx_bytes = 0;
  1788. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  1789. sinfo->tx_bytes += sta->tx_stats.bytes[ac];
  1790. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
  1791. }
  1792. if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
  1793. sinfo->tx_packets = 0;
  1794. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  1795. sinfo->tx_packets += sta->tx_stats.packets[ac];
  1796. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
  1797. }
  1798. if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
  1799. BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
  1800. sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
  1801. if (sta->pcpu_rx_stats) {
  1802. for_each_possible_cpu(cpu) {
  1803. struct ieee80211_sta_rx_stats *cpurxs;
  1804. cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
  1805. sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
  1806. }
  1807. }
  1808. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
  1809. }
  1810. if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
  1811. sinfo->rx_packets = sta->rx_stats.packets;
  1812. if (sta->pcpu_rx_stats) {
  1813. for_each_possible_cpu(cpu) {
  1814. struct ieee80211_sta_rx_stats *cpurxs;
  1815. cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
  1816. sinfo->rx_packets += cpurxs->packets;
  1817. }
  1818. }
  1819. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
  1820. }
  1821. if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
  1822. sinfo->tx_retries = sta->status_stats.retry_count;
  1823. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
  1824. }
  1825. if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
  1826. sinfo->tx_failed = sta->status_stats.retry_failed;
  1827. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
  1828. }
  1829. sinfo->rx_dropped_misc = sta->rx_stats.dropped;
  1830. if (sta->pcpu_rx_stats) {
  1831. for_each_possible_cpu(cpu) {
  1832. struct ieee80211_sta_rx_stats *cpurxs;
  1833. cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
  1834. sinfo->rx_dropped_misc += cpurxs->dropped;
  1835. }
  1836. }
  1837. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  1838. !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
  1839. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
  1840. BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
  1841. sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
  1842. }
  1843. if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
  1844. ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
  1845. if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
  1846. sinfo->signal = (s8)last_rxstats->last_signal;
  1847. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
  1848. }
  1849. if (!sta->pcpu_rx_stats &&
  1850. !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
  1851. sinfo->signal_avg =
  1852. -ewma_signal_read(&sta->rx_stats_avg.signal);
  1853. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
  1854. }
  1855. }
  1856. /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
  1857. * the sta->rx_stats struct, so the check here is fine with and without
  1858. * pcpu statistics
  1859. */
  1860. if (last_rxstats->chains &&
  1861. !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
  1862. BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
  1863. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
  1864. if (!sta->pcpu_rx_stats)
  1865. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
  1866. sinfo->chains = last_rxstats->chains;
  1867. for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
  1868. sinfo->chain_signal[i] =
  1869. last_rxstats->chain_signal_last[i];
  1870. sinfo->chain_signal_avg[i] =
  1871. -ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
  1872. }
  1873. }
  1874. if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
  1875. sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
  1876. &sinfo->txrate);
  1877. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
  1878. }
  1879. if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
  1880. if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
  1881. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
  1882. }
  1883. if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
  1884. for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
  1885. struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
  1886. sta_set_tidstats(sta, tidstats, i);
  1887. }
  1888. }
  1889. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  1890. #ifdef CONFIG_MAC80211_MESH
  1891. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
  1892. BIT_ULL(NL80211_STA_INFO_PLID) |
  1893. BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
  1894. BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
  1895. BIT_ULL(NL80211_STA_INFO_PEER_PM) |
  1896. BIT_ULL(NL80211_STA_INFO_NONPEER_PM);
  1897. sinfo->llid = sta->mesh->llid;
  1898. sinfo->plid = sta->mesh->plid;
  1899. sinfo->plink_state = sta->mesh->plink_state;
  1900. if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
  1901. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
  1902. sinfo->t_offset = sta->mesh->t_offset;
  1903. }
  1904. sinfo->local_pm = sta->mesh->local_pm;
  1905. sinfo->peer_pm = sta->mesh->peer_pm;
  1906. sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
  1907. #endif
  1908. }
  1909. sinfo->bss_param.flags = 0;
  1910. if (sdata->vif.bss_conf.use_cts_prot)
  1911. sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
  1912. if (sdata->vif.bss_conf.use_short_preamble)
  1913. sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
  1914. if (sdata->vif.bss_conf.use_short_slot)
  1915. sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
  1916. sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
  1917. sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
  1918. sinfo->sta_flags.set = 0;
  1919. sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
  1920. BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
  1921. BIT(NL80211_STA_FLAG_WME) |
  1922. BIT(NL80211_STA_FLAG_MFP) |
  1923. BIT(NL80211_STA_FLAG_AUTHENTICATED) |
  1924. BIT(NL80211_STA_FLAG_ASSOCIATED) |
  1925. BIT(NL80211_STA_FLAG_TDLS_PEER);
  1926. if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
  1927. sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
  1928. if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
  1929. sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
  1930. if (sta->sta.wme)
  1931. sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
  1932. if (test_sta_flag(sta, WLAN_STA_MFP))
  1933. sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
  1934. if (test_sta_flag(sta, WLAN_STA_AUTH))
  1935. sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
  1936. if (test_sta_flag(sta, WLAN_STA_ASSOC))
  1937. sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
  1938. if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
  1939. sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
  1940. thr = sta_get_expected_throughput(sta);
  1941. if (thr != 0) {
  1942. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
  1943. sinfo->expected_throughput = thr;
  1944. }
  1945. if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
  1946. sta->status_stats.ack_signal_filled) {
  1947. sinfo->ack_signal = sta->status_stats.last_ack_signal;
  1948. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
  1949. }
  1950. if (ieee80211_hw_check(&sta->local->hw, REPORTS_TX_ACK_STATUS) &&
  1951. !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_DATA_ACK_SIGNAL_AVG))) {
  1952. sinfo->avg_ack_signal =
  1953. -(s8)ewma_avg_signal_read(
  1954. &sta->status_stats.avg_ack_signal);
  1955. sinfo->filled |=
  1956. BIT_ULL(NL80211_STA_INFO_DATA_ACK_SIGNAL_AVG);
  1957. }
  1958. }
  1959. u32 sta_get_expected_throughput(struct sta_info *sta)
  1960. {
  1961. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1962. struct ieee80211_local *local = sdata->local;
  1963. struct rate_control_ref *ref = NULL;
  1964. u32 thr = 0;
  1965. if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
  1966. ref = local->rate_ctrl;
  1967. /* check if the driver has a SW RC implementation */
  1968. if (ref && ref->ops->get_expected_throughput)
  1969. thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
  1970. else
  1971. thr = drv_get_expected_throughput(local, sta);
  1972. return thr;
  1973. }
  1974. unsigned long ieee80211_sta_last_active(struct sta_info *sta)
  1975. {
  1976. struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
  1977. if (!sta->status_stats.last_ack ||
  1978. time_after(stats->last_rx, sta->status_stats.last_ack))
  1979. return stats->last_rx;
  1980. return sta->status_stats.last_ack;
  1981. }
  1982. static void sta_update_codel_params(struct sta_info *sta, u32 thr)
  1983. {
  1984. if (!sta->sdata->local->ops->wake_tx_queue)
  1985. return;
  1986. if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
  1987. sta->cparams.target = MS2TIME(50);
  1988. sta->cparams.interval = MS2TIME(300);
  1989. sta->cparams.ecn = false;
  1990. } else {
  1991. sta->cparams.target = MS2TIME(20);
  1992. sta->cparams.interval = MS2TIME(100);
  1993. sta->cparams.ecn = true;
  1994. }
  1995. }
  1996. void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
  1997. u32 thr)
  1998. {
  1999. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  2000. sta_update_codel_params(sta, thr);
  2001. }