sta_info.c 53 KB

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