sta_info.c 78 KB

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