sta_info.c 80 KB

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