sta_info.c 63 KB

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