sta_info.c 55 KB

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