sta_info.c 53 KB

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