sta_info.c 53 KB

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