sta_info.c 63 KB

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