sta_info.c 64 KB

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