sta_info.c 80 KB

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