sta_info.c 57 KB

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