sta_info.c 53 KB

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