sta_info.c 52 KB

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