sta_info.c 55 KB

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