sta_info.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/types.h>
  14. #include <linux/slab.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/timer.h>
  18. #include <linux/rtnetlink.h>
  19. #include <net/mac80211.h>
  20. #include "ieee80211_i.h"
  21. #include "driver-ops.h"
  22. #include "rate.h"
  23. #include "sta_info.h"
  24. #include "debugfs_sta.h"
  25. #include "mesh.h"
  26. #include "wme.h"
  27. /**
  28. * DOC: STA information lifetime rules
  29. *
  30. * STA info structures (&struct sta_info) are managed in a hash table
  31. * for faster lookup and a list for iteration. They are managed using
  32. * RCU, i.e. access to the list and hash table is protected by RCU.
  33. *
  34. * Upon allocating a STA info structure with sta_info_alloc(), the caller
  35. * owns that structure. It must then insert it into the hash table using
  36. * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
  37. * case (which acquires an rcu read section but must not be called from
  38. * within one) will the pointer still be valid after the call. Note that
  39. * the caller may not do much with the STA info before inserting it, in
  40. * particular, it may not start any mesh peer link management or add
  41. * encryption keys.
  42. *
  43. * When the insertion fails (sta_info_insert()) returns non-zero), the
  44. * structure will have been freed by sta_info_insert()!
  45. *
  46. * Station entries are added by mac80211 when you establish a link with a
  47. * peer. This means different things for the different type of interfaces
  48. * we support. For a regular station this mean we add the AP sta when we
  49. * receive an association response from the AP. For IBSS this occurs when
  50. * get to know about a peer on the same IBSS. For WDS we add the sta for
  51. * the peer immediately upon device open. When using AP mode we add stations
  52. * for each respective station upon request from userspace through nl80211.
  53. *
  54. * In order to remove a STA info structure, various sta_info_destroy_*()
  55. * calls are available.
  56. *
  57. * There is no concept of ownership on a STA entry, each structure is
  58. * owned by the global hash table/list until it is removed. All users of
  59. * the structure need to be RCU protected so that the structure won't be
  60. * freed before they are done using it.
  61. */
  62. /* Caller must hold local->sta_mtx */
  63. static int sta_info_hash_del(struct ieee80211_local *local,
  64. struct sta_info *sta)
  65. {
  66. struct sta_info *s;
  67. s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
  68. lockdep_is_held(&local->sta_mtx));
  69. if (!s)
  70. return -ENOENT;
  71. if (s == sta) {
  72. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
  73. s->hnext);
  74. return 0;
  75. }
  76. while (rcu_access_pointer(s->hnext) &&
  77. rcu_access_pointer(s->hnext) != sta)
  78. s = rcu_dereference_protected(s->hnext,
  79. lockdep_is_held(&local->sta_mtx));
  80. if (rcu_access_pointer(s->hnext)) {
  81. rcu_assign_pointer(s->hnext, sta->hnext);
  82. return 0;
  83. }
  84. return -ENOENT;
  85. }
  86. static void cleanup_single_sta(struct sta_info *sta)
  87. {
  88. int ac, i;
  89. struct tid_ampdu_tx *tid_tx;
  90. struct ieee80211_sub_if_data *sdata = sta->sdata;
  91. struct ieee80211_local *local = sdata->local;
  92. struct ps_data *ps;
  93. /*
  94. * At this point, when being called as call_rcu callback,
  95. * neither mac80211 nor the driver can reference this
  96. * sta struct any more except by still existing timers
  97. * associated with this station that we clean up below.
  98. *
  99. * Note though that this still uses the sdata and even
  100. * calls the driver in AP and mesh mode, so interfaces
  101. * of those types mush use call sta_info_flush_cleanup()
  102. * (typically via sta_info_flush()) before deconfiguring
  103. * the driver.
  104. *
  105. * In station mode, nothing happens here so it doesn't
  106. * have to (and doesn't) do that, this is intentional to
  107. * speed up roaming.
  108. */
  109. if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
  110. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  111. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  112. ps = &sdata->bss->ps;
  113. else if (ieee80211_vif_is_mesh(&sdata->vif))
  114. ps = &sdata->u.mesh.ps;
  115. else
  116. return;
  117. clear_sta_flag(sta, WLAN_STA_PS_STA);
  118. atomic_dec(&ps->num_sta_ps);
  119. sta_info_recalc_tim(sta);
  120. }
  121. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  122. local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
  123. ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
  124. ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
  125. }
  126. if (ieee80211_vif_is_mesh(&sdata->vif))
  127. mesh_sta_cleanup(sta);
  128. cancel_work_sync(&sta->drv_unblock_wk);
  129. /*
  130. * Destroy aggregation state here. It would be nice to wait for the
  131. * driver to finish aggregation stop and then clean up, but for now
  132. * drivers have to handle aggregation stop being requested, followed
  133. * directly by station destruction.
  134. */
  135. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  136. kfree(sta->ampdu_mlme.tid_start_tx[i]);
  137. tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
  138. if (!tid_tx)
  139. continue;
  140. ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
  141. kfree(tid_tx);
  142. }
  143. sta_info_free(local, sta);
  144. }
  145. void ieee80211_cleanup_sdata_stas(struct ieee80211_sub_if_data *sdata)
  146. {
  147. struct sta_info *sta;
  148. spin_lock_bh(&sdata->cleanup_stations_lock);
  149. while (!list_empty(&sdata->cleanup_stations)) {
  150. sta = list_first_entry(&sdata->cleanup_stations,
  151. struct sta_info, list);
  152. list_del(&sta->list);
  153. spin_unlock_bh(&sdata->cleanup_stations_lock);
  154. cleanup_single_sta(sta);
  155. spin_lock_bh(&sdata->cleanup_stations_lock);
  156. }
  157. spin_unlock_bh(&sdata->cleanup_stations_lock);
  158. }
  159. static void free_sta_rcu(struct rcu_head *h)
  160. {
  161. struct sta_info *sta = container_of(h, struct sta_info, rcu_head);
  162. struct ieee80211_sub_if_data *sdata = sta->sdata;
  163. spin_lock(&sdata->cleanup_stations_lock);
  164. list_add_tail(&sta->list, &sdata->cleanup_stations);
  165. spin_unlock(&sdata->cleanup_stations_lock);
  166. ieee80211_queue_work(&sdata->local->hw, &sdata->cleanup_stations_wk);
  167. }
  168. /* protected by RCU */
  169. struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
  170. const u8 *addr)
  171. {
  172. struct ieee80211_local *local = sdata->local;
  173. struct sta_info *sta;
  174. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  175. lockdep_is_held(&local->sta_mtx));
  176. while (sta) {
  177. if (sta->sdata == sdata &&
  178. ether_addr_equal(sta->sta.addr, addr))
  179. break;
  180. sta = rcu_dereference_check(sta->hnext,
  181. lockdep_is_held(&local->sta_mtx));
  182. }
  183. return sta;
  184. }
  185. /*
  186. * Get sta info either from the specified interface
  187. * or from one of its vlans
  188. */
  189. struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
  190. const u8 *addr)
  191. {
  192. struct ieee80211_local *local = sdata->local;
  193. struct sta_info *sta;
  194. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  195. lockdep_is_held(&local->sta_mtx));
  196. while (sta) {
  197. if ((sta->sdata == sdata ||
  198. (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
  199. ether_addr_equal(sta->sta.addr, addr))
  200. break;
  201. sta = rcu_dereference_check(sta->hnext,
  202. lockdep_is_held(&local->sta_mtx));
  203. }
  204. return sta;
  205. }
  206. struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
  207. int idx)
  208. {
  209. struct ieee80211_local *local = sdata->local;
  210. struct sta_info *sta;
  211. int i = 0;
  212. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  213. if (sdata != sta->sdata)
  214. continue;
  215. if (i < idx) {
  216. ++i;
  217. continue;
  218. }
  219. return sta;
  220. }
  221. return NULL;
  222. }
  223. /**
  224. * sta_info_free - free STA
  225. *
  226. * @local: pointer to the global information
  227. * @sta: STA info to free
  228. *
  229. * This function must undo everything done by sta_info_alloc()
  230. * that may happen before sta_info_insert(). It may only be
  231. * called when sta_info_insert() has not been attempted (and
  232. * if that fails, the station is freed anyway.)
  233. */
  234. void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
  235. {
  236. if (sta->rate_ctrl)
  237. rate_control_free_sta(sta);
  238. sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
  239. kfree(sta);
  240. }
  241. /* Caller must hold local->sta_mtx */
  242. static void sta_info_hash_add(struct ieee80211_local *local,
  243. struct sta_info *sta)
  244. {
  245. lockdep_assert_held(&local->sta_mtx);
  246. sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
  247. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
  248. }
  249. static void sta_unblock(struct work_struct *wk)
  250. {
  251. struct sta_info *sta;
  252. sta = container_of(wk, struct sta_info, drv_unblock_wk);
  253. if (sta->dead)
  254. return;
  255. if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
  256. local_bh_disable();
  257. ieee80211_sta_ps_deliver_wakeup(sta);
  258. local_bh_enable();
  259. } else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
  260. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  261. local_bh_disable();
  262. ieee80211_sta_ps_deliver_poll_response(sta);
  263. local_bh_enable();
  264. } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
  265. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  266. local_bh_disable();
  267. ieee80211_sta_ps_deliver_uapsd(sta);
  268. local_bh_enable();
  269. } else
  270. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  271. }
  272. static int sta_prepare_rate_control(struct ieee80211_local *local,
  273. struct sta_info *sta, gfp_t gfp)
  274. {
  275. if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
  276. return 0;
  277. sta->rate_ctrl = local->rate_ctrl;
  278. sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
  279. &sta->sta, gfp);
  280. if (!sta->rate_ctrl_priv)
  281. return -ENOMEM;
  282. return 0;
  283. }
  284. struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
  285. const u8 *addr, gfp_t gfp)
  286. {
  287. struct ieee80211_local *local = sdata->local;
  288. struct sta_info *sta;
  289. struct timespec uptime;
  290. int i;
  291. sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
  292. if (!sta)
  293. return NULL;
  294. spin_lock_init(&sta->lock);
  295. INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
  296. INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
  297. mutex_init(&sta->ampdu_mlme.mtx);
  298. #ifdef CONFIG_MAC80211_MESH
  299. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  300. !sdata->u.mesh.user_mpm)
  301. init_timer(&sta->plink_timer);
  302. sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
  303. #endif
  304. memcpy(sta->sta.addr, addr, ETH_ALEN);
  305. sta->local = local;
  306. sta->sdata = sdata;
  307. sta->last_rx = jiffies;
  308. sta->sta_state = IEEE80211_STA_NONE;
  309. do_posix_clock_monotonic_gettime(&uptime);
  310. sta->last_connected = uptime.tv_sec;
  311. ewma_init(&sta->avg_signal, 1024, 8);
  312. for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
  313. ewma_init(&sta->chain_signal_avg[i], 1024, 8);
  314. if (sta_prepare_rate_control(local, sta, gfp)) {
  315. kfree(sta);
  316. return NULL;
  317. }
  318. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  319. /*
  320. * timer_to_tid must be initialized with identity mapping
  321. * to enable session_timer's data differentiation. See
  322. * sta_rx_agg_session_timer_expired for usage.
  323. */
  324. sta->timer_to_tid[i] = i;
  325. }
  326. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  327. skb_queue_head_init(&sta->ps_tx_buf[i]);
  328. skb_queue_head_init(&sta->tx_filtered[i]);
  329. }
  330. for (i = 0; i < IEEE80211_NUM_TIDS; i++)
  331. sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
  332. sta->sta.smps_mode = IEEE80211_SMPS_OFF;
  333. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  334. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  335. struct ieee80211_supported_band *sband =
  336. local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
  337. u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
  338. IEEE80211_HT_CAP_SM_PS_SHIFT;
  339. /*
  340. * Assume that hostapd advertises our caps in the beacon and
  341. * this is the known_smps_mode for a station that just assciated
  342. */
  343. switch (smps) {
  344. case WLAN_HT_SMPS_CONTROL_DISABLED:
  345. sta->known_smps_mode = IEEE80211_SMPS_OFF;
  346. break;
  347. case WLAN_HT_SMPS_CONTROL_STATIC:
  348. sta->known_smps_mode = IEEE80211_SMPS_STATIC;
  349. break;
  350. case WLAN_HT_SMPS_CONTROL_DYNAMIC:
  351. sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
  352. break;
  353. default:
  354. WARN_ON(1);
  355. }
  356. }
  357. sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
  358. return sta;
  359. }
  360. static int sta_info_insert_check(struct sta_info *sta)
  361. {
  362. struct ieee80211_sub_if_data *sdata = sta->sdata;
  363. /*
  364. * Can't be a WARN_ON because it can be triggered through a race:
  365. * something inserts a STA (on one CPU) without holding the RTNL
  366. * and another CPU turns off the net device.
  367. */
  368. if (unlikely(!ieee80211_sdata_running(sdata)))
  369. return -ENETDOWN;
  370. if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
  371. is_multicast_ether_addr(sta->sta.addr)))
  372. return -EINVAL;
  373. return 0;
  374. }
  375. static int sta_info_insert_drv_state(struct ieee80211_local *local,
  376. struct ieee80211_sub_if_data *sdata,
  377. struct sta_info *sta)
  378. {
  379. enum ieee80211_sta_state state;
  380. int err = 0;
  381. for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
  382. err = drv_sta_state(local, sdata, sta, state, state + 1);
  383. if (err)
  384. break;
  385. }
  386. if (!err) {
  387. /*
  388. * Drivers using legacy sta_add/sta_remove callbacks only
  389. * get uploaded set to true after sta_add is called.
  390. */
  391. if (!local->ops->sta_add)
  392. sta->uploaded = true;
  393. return 0;
  394. }
  395. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  396. sdata_info(sdata,
  397. "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
  398. sta->sta.addr, state + 1, err);
  399. err = 0;
  400. }
  401. /* unwind on error */
  402. for (; state > IEEE80211_STA_NOTEXIST; state--)
  403. WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
  404. return err;
  405. }
  406. /*
  407. * should be called with sta_mtx locked
  408. * this function replaces the mutex lock
  409. * with a RCU lock
  410. */
  411. static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
  412. {
  413. struct ieee80211_local *local = sta->local;
  414. struct ieee80211_sub_if_data *sdata = sta->sdata;
  415. struct station_info sinfo;
  416. int err = 0;
  417. lockdep_assert_held(&local->sta_mtx);
  418. /* check if STA exists already */
  419. if (sta_info_get_bss(sdata, sta->sta.addr)) {
  420. err = -EEXIST;
  421. goto out_err;
  422. }
  423. /* notify driver */
  424. err = sta_info_insert_drv_state(local, sdata, sta);
  425. if (err)
  426. goto out_err;
  427. local->num_sta++;
  428. local->sta_generation++;
  429. smp_mb();
  430. /* make the station visible */
  431. sta_info_hash_add(local, sta);
  432. list_add_rcu(&sta->list, &local->sta_list);
  433. set_sta_flag(sta, WLAN_STA_INSERTED);
  434. ieee80211_sta_debugfs_add(sta);
  435. rate_control_add_sta_debugfs(sta);
  436. memset(&sinfo, 0, sizeof(sinfo));
  437. sinfo.filled = 0;
  438. sinfo.generation = local->sta_generation;
  439. cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
  440. sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
  441. /* move reference to rcu-protected */
  442. rcu_read_lock();
  443. mutex_unlock(&local->sta_mtx);
  444. if (ieee80211_vif_is_mesh(&sdata->vif))
  445. mesh_accept_plinks_update(sdata);
  446. return 0;
  447. out_err:
  448. mutex_unlock(&local->sta_mtx);
  449. rcu_read_lock();
  450. return err;
  451. }
  452. int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
  453. {
  454. struct ieee80211_local *local = sta->local;
  455. int err = 0;
  456. might_sleep();
  457. err = sta_info_insert_check(sta);
  458. if (err) {
  459. rcu_read_lock();
  460. goto out_free;
  461. }
  462. mutex_lock(&local->sta_mtx);
  463. err = sta_info_insert_finish(sta);
  464. if (err)
  465. goto out_free;
  466. return 0;
  467. out_free:
  468. BUG_ON(!err);
  469. sta_info_free(local, sta);
  470. return err;
  471. }
  472. int sta_info_insert(struct sta_info *sta)
  473. {
  474. int err = sta_info_insert_rcu(sta);
  475. rcu_read_unlock();
  476. return err;
  477. }
  478. static inline void __bss_tim_set(u8 *tim, u16 id)
  479. {
  480. /*
  481. * This format has been mandated by the IEEE specifications,
  482. * so this line may not be changed to use the __set_bit() format.
  483. */
  484. tim[id / 8] |= (1 << (id % 8));
  485. }
  486. static inline void __bss_tim_clear(u8 *tim, u16 id)
  487. {
  488. /*
  489. * This format has been mandated by the IEEE specifications,
  490. * so this line may not be changed to use the __clear_bit() format.
  491. */
  492. tim[id / 8] &= ~(1 << (id % 8));
  493. }
  494. static inline bool __bss_tim_get(u8 *tim, u16 id)
  495. {
  496. /*
  497. * This format has been mandated by the IEEE specifications,
  498. * so this line may not be changed to use the test_bit() format.
  499. */
  500. return tim[id / 8] & (1 << (id % 8));
  501. }
  502. static unsigned long ieee80211_tids_for_ac(int ac)
  503. {
  504. /* If we ever support TIDs > 7, this obviously needs to be adjusted */
  505. switch (ac) {
  506. case IEEE80211_AC_VO:
  507. return BIT(6) | BIT(7);
  508. case IEEE80211_AC_VI:
  509. return BIT(4) | BIT(5);
  510. case IEEE80211_AC_BE:
  511. return BIT(0) | BIT(3);
  512. case IEEE80211_AC_BK:
  513. return BIT(1) | BIT(2);
  514. default:
  515. WARN_ON(1);
  516. return 0;
  517. }
  518. }
  519. void sta_info_recalc_tim(struct sta_info *sta)
  520. {
  521. struct ieee80211_local *local = sta->local;
  522. struct ps_data *ps;
  523. bool indicate_tim = false;
  524. u8 ignore_for_tim = sta->sta.uapsd_queues;
  525. int ac;
  526. u16 id;
  527. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  528. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  529. if (WARN_ON_ONCE(!sta->sdata->bss))
  530. return;
  531. ps = &sta->sdata->bss->ps;
  532. id = sta->sta.aid;
  533. #ifdef CONFIG_MAC80211_MESH
  534. } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
  535. ps = &sta->sdata->u.mesh.ps;
  536. /* TIM map only for PLID <= IEEE80211_MAX_AID */
  537. id = le16_to_cpu(sta->plid) % IEEE80211_MAX_AID;
  538. #endif
  539. } else {
  540. return;
  541. }
  542. /* No need to do anything if the driver does all */
  543. if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
  544. return;
  545. if (sta->dead)
  546. goto done;
  547. /*
  548. * If all ACs are delivery-enabled then we should build
  549. * the TIM bit for all ACs anyway; if only some are then
  550. * we ignore those and build the TIM bit using only the
  551. * non-enabled ones.
  552. */
  553. if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
  554. ignore_for_tim = 0;
  555. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  556. unsigned long tids;
  557. if (ignore_for_tim & BIT(ac))
  558. continue;
  559. indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
  560. !skb_queue_empty(&sta->ps_tx_buf[ac]);
  561. if (indicate_tim)
  562. break;
  563. tids = ieee80211_tids_for_ac(ac);
  564. indicate_tim |=
  565. sta->driver_buffered_tids & tids;
  566. }
  567. done:
  568. spin_lock_bh(&local->tim_lock);
  569. if (indicate_tim == __bss_tim_get(ps->tim, id))
  570. goto out_unlock;
  571. if (indicate_tim)
  572. __bss_tim_set(ps->tim, id);
  573. else
  574. __bss_tim_clear(ps->tim, id);
  575. if (local->ops->set_tim) {
  576. local->tim_in_locked_section = true;
  577. drv_set_tim(local, &sta->sta, indicate_tim);
  578. local->tim_in_locked_section = false;
  579. }
  580. out_unlock:
  581. spin_unlock_bh(&local->tim_lock);
  582. }
  583. static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
  584. {
  585. struct ieee80211_tx_info *info;
  586. int timeout;
  587. if (!skb)
  588. return false;
  589. info = IEEE80211_SKB_CB(skb);
  590. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  591. timeout = (sta->listen_interval *
  592. sta->sdata->vif.bss_conf.beacon_int *
  593. 32 / 15625) * HZ;
  594. if (timeout < STA_TX_BUFFER_EXPIRE)
  595. timeout = STA_TX_BUFFER_EXPIRE;
  596. return time_after(jiffies, info->control.jiffies + timeout);
  597. }
  598. static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
  599. struct sta_info *sta, int ac)
  600. {
  601. unsigned long flags;
  602. struct sk_buff *skb;
  603. /*
  604. * First check for frames that should expire on the filtered
  605. * queue. Frames here were rejected by the driver and are on
  606. * a separate queue to avoid reordering with normal PS-buffered
  607. * frames. They also aren't accounted for right now in the
  608. * total_ps_buffered counter.
  609. */
  610. for (;;) {
  611. spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
  612. skb = skb_peek(&sta->tx_filtered[ac]);
  613. if (sta_info_buffer_expired(sta, skb))
  614. skb = __skb_dequeue(&sta->tx_filtered[ac]);
  615. else
  616. skb = NULL;
  617. spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
  618. /*
  619. * Frames are queued in order, so if this one
  620. * hasn't expired yet we can stop testing. If
  621. * we actually reached the end of the queue we
  622. * also need to stop, of course.
  623. */
  624. if (!skb)
  625. break;
  626. ieee80211_free_txskb(&local->hw, skb);
  627. }
  628. /*
  629. * Now also check the normal PS-buffered queue, this will
  630. * only find something if the filtered queue was emptied
  631. * since the filtered frames are all before the normal PS
  632. * buffered frames.
  633. */
  634. for (;;) {
  635. spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
  636. skb = skb_peek(&sta->ps_tx_buf[ac]);
  637. if (sta_info_buffer_expired(sta, skb))
  638. skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
  639. else
  640. skb = NULL;
  641. spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
  642. /*
  643. * frames are queued in order, so if this one
  644. * hasn't expired yet (or we reached the end of
  645. * the queue) we can stop testing
  646. */
  647. if (!skb)
  648. break;
  649. local->total_ps_buffered--;
  650. ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
  651. sta->sta.addr);
  652. ieee80211_free_txskb(&local->hw, skb);
  653. }
  654. /*
  655. * Finally, recalculate the TIM bit for this station -- it might
  656. * now be clear because the station was too slow to retrieve its
  657. * frames.
  658. */
  659. sta_info_recalc_tim(sta);
  660. /*
  661. * Return whether there are any frames still buffered, this is
  662. * used to check whether the cleanup timer still needs to run,
  663. * if there are no frames we don't need to rearm the timer.
  664. */
  665. return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
  666. skb_queue_empty(&sta->tx_filtered[ac]));
  667. }
  668. static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  669. struct sta_info *sta)
  670. {
  671. bool have_buffered = false;
  672. int ac;
  673. /* This is only necessary for stations on BSS/MBSS interfaces */
  674. if (!sta->sdata->bss &&
  675. !ieee80211_vif_is_mesh(&sta->sdata->vif))
  676. return false;
  677. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  678. have_buffered |=
  679. sta_info_cleanup_expire_buffered_ac(local, sta, ac);
  680. return have_buffered;
  681. }
  682. int __must_check __sta_info_destroy(struct sta_info *sta)
  683. {
  684. struct ieee80211_local *local;
  685. struct ieee80211_sub_if_data *sdata;
  686. int ret;
  687. might_sleep();
  688. if (!sta)
  689. return -ENOENT;
  690. local = sta->local;
  691. sdata = sta->sdata;
  692. lockdep_assert_held(&local->sta_mtx);
  693. /*
  694. * Before removing the station from the driver and
  695. * rate control, it might still start new aggregation
  696. * sessions -- block that to make sure the tear-down
  697. * will be sufficient.
  698. */
  699. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  700. ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
  701. ret = sta_info_hash_del(local, sta);
  702. if (ret)
  703. return ret;
  704. list_del_rcu(&sta->list);
  705. /* this always calls synchronize_net() */
  706. ieee80211_free_sta_keys(local, sta);
  707. sta->dead = true;
  708. local->num_sta--;
  709. local->sta_generation++;
  710. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  711. RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
  712. while (sta->sta_state > IEEE80211_STA_NONE) {
  713. ret = sta_info_move_state(sta, sta->sta_state - 1);
  714. if (ret) {
  715. WARN_ON_ONCE(1);
  716. break;
  717. }
  718. }
  719. if (sta->uploaded) {
  720. ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
  721. IEEE80211_STA_NOTEXIST);
  722. WARN_ON_ONCE(ret != 0);
  723. }
  724. sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
  725. cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
  726. rate_control_remove_sta_debugfs(sta);
  727. ieee80211_sta_debugfs_remove(sta);
  728. call_rcu(&sta->rcu_head, free_sta_rcu);
  729. return 0;
  730. }
  731. int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
  732. {
  733. struct sta_info *sta;
  734. int ret;
  735. mutex_lock(&sdata->local->sta_mtx);
  736. sta = sta_info_get(sdata, addr);
  737. ret = __sta_info_destroy(sta);
  738. mutex_unlock(&sdata->local->sta_mtx);
  739. return ret;
  740. }
  741. int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
  742. const u8 *addr)
  743. {
  744. struct sta_info *sta;
  745. int ret;
  746. mutex_lock(&sdata->local->sta_mtx);
  747. sta = sta_info_get_bss(sdata, addr);
  748. ret = __sta_info_destroy(sta);
  749. mutex_unlock(&sdata->local->sta_mtx);
  750. return ret;
  751. }
  752. static void sta_info_cleanup(unsigned long data)
  753. {
  754. struct ieee80211_local *local = (struct ieee80211_local *) data;
  755. struct sta_info *sta;
  756. bool timer_needed = false;
  757. rcu_read_lock();
  758. list_for_each_entry_rcu(sta, &local->sta_list, list)
  759. if (sta_info_cleanup_expire_buffered(local, sta))
  760. timer_needed = true;
  761. rcu_read_unlock();
  762. if (local->quiescing)
  763. return;
  764. if (!timer_needed)
  765. return;
  766. mod_timer(&local->sta_cleanup,
  767. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
  768. }
  769. void sta_info_init(struct ieee80211_local *local)
  770. {
  771. spin_lock_init(&local->tim_lock);
  772. mutex_init(&local->sta_mtx);
  773. INIT_LIST_HEAD(&local->sta_list);
  774. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  775. (unsigned long)local);
  776. }
  777. void sta_info_stop(struct ieee80211_local *local)
  778. {
  779. del_timer_sync(&local->sta_cleanup);
  780. }
  781. int sta_info_flush_defer(struct ieee80211_sub_if_data *sdata)
  782. {
  783. struct ieee80211_local *local = sdata->local;
  784. struct sta_info *sta, *tmp;
  785. int ret = 0;
  786. might_sleep();
  787. mutex_lock(&local->sta_mtx);
  788. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  789. if (sdata == sta->sdata) {
  790. WARN_ON(__sta_info_destroy(sta));
  791. ret++;
  792. }
  793. }
  794. mutex_unlock(&local->sta_mtx);
  795. return ret;
  796. }
  797. void sta_info_flush_cleanup(struct ieee80211_sub_if_data *sdata)
  798. {
  799. ieee80211_cleanup_sdata_stas(sdata);
  800. cancel_work_sync(&sdata->cleanup_stations_wk);
  801. }
  802. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  803. unsigned long exp_time)
  804. {
  805. struct ieee80211_local *local = sdata->local;
  806. struct sta_info *sta, *tmp;
  807. mutex_lock(&local->sta_mtx);
  808. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  809. if (sdata != sta->sdata)
  810. continue;
  811. if (time_after(jiffies, sta->last_rx + exp_time)) {
  812. sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
  813. sta->sta.addr);
  814. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  815. test_sta_flag(sta, WLAN_STA_PS_STA))
  816. atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
  817. WARN_ON(__sta_info_destroy(sta));
  818. }
  819. }
  820. mutex_unlock(&local->sta_mtx);
  821. }
  822. struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
  823. const u8 *addr,
  824. const u8 *localaddr)
  825. {
  826. struct sta_info *sta, *nxt;
  827. /*
  828. * Just return a random station if localaddr is NULL
  829. * ... first in list.
  830. */
  831. for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
  832. if (localaddr &&
  833. !ether_addr_equal(sta->sdata->vif.addr, localaddr))
  834. continue;
  835. if (!sta->uploaded)
  836. return NULL;
  837. return &sta->sta;
  838. }
  839. return NULL;
  840. }
  841. EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
  842. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
  843. const u8 *addr)
  844. {
  845. struct sta_info *sta;
  846. if (!vif)
  847. return NULL;
  848. sta = sta_info_get_bss(vif_to_sdata(vif), addr);
  849. if (!sta)
  850. return NULL;
  851. if (!sta->uploaded)
  852. return NULL;
  853. return &sta->sta;
  854. }
  855. EXPORT_SYMBOL(ieee80211_find_sta);
  856. static void clear_sta_ps_flags(void *_sta)
  857. {
  858. struct sta_info *sta = _sta;
  859. struct ieee80211_sub_if_data *sdata = sta->sdata;
  860. struct ps_data *ps;
  861. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  862. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  863. ps = &sdata->bss->ps;
  864. else if (ieee80211_vif_is_mesh(&sdata->vif))
  865. ps = &sdata->u.mesh.ps;
  866. else
  867. return;
  868. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  869. if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA))
  870. atomic_dec(&ps->num_sta_ps);
  871. }
  872. /* powersave support code */
  873. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  874. {
  875. struct ieee80211_sub_if_data *sdata = sta->sdata;
  876. struct ieee80211_local *local = sdata->local;
  877. struct sk_buff_head pending;
  878. int filtered = 0, buffered = 0, ac;
  879. unsigned long flags;
  880. clear_sta_flag(sta, WLAN_STA_SP);
  881. BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
  882. sta->driver_buffered_tids = 0;
  883. if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
  884. drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
  885. skb_queue_head_init(&pending);
  886. /* Send all buffered frames to the station */
  887. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  888. int count = skb_queue_len(&pending), tmp;
  889. spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
  890. skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
  891. spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
  892. tmp = skb_queue_len(&pending);
  893. filtered += tmp - count;
  894. count = tmp;
  895. spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
  896. skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
  897. spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
  898. tmp = skb_queue_len(&pending);
  899. buffered += tmp - count;
  900. }
  901. ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
  902. /* This station just woke up and isn't aware of our SMPS state */
  903. if (!ieee80211_smps_is_restrictive(sta->known_smps_mode,
  904. sdata->smps_mode) &&
  905. sta->known_smps_mode != sdata->bss->req_smps &&
  906. sta_info_tx_streams(sta) != 1) {
  907. ht_dbg(sdata,
  908. "%pM just woke up and MIMO capable - update SMPS\n",
  909. sta->sta.addr);
  910. ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
  911. sta->sta.addr,
  912. sdata->vif.bss_conf.bssid);
  913. }
  914. local->total_ps_buffered -= buffered;
  915. sta_info_recalc_tim(sta);
  916. ps_dbg(sdata,
  917. "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
  918. sta->sta.addr, sta->sta.aid, filtered, buffered);
  919. }
  920. static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
  921. struct sta_info *sta, int tid,
  922. enum ieee80211_frame_release_type reason)
  923. {
  924. struct ieee80211_local *local = sdata->local;
  925. struct ieee80211_qos_hdr *nullfunc;
  926. struct sk_buff *skb;
  927. int size = sizeof(*nullfunc);
  928. __le16 fc;
  929. bool qos = test_sta_flag(sta, WLAN_STA_WME);
  930. struct ieee80211_tx_info *info;
  931. struct ieee80211_chanctx_conf *chanctx_conf;
  932. if (qos) {
  933. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  934. IEEE80211_STYPE_QOS_NULLFUNC |
  935. IEEE80211_FCTL_FROMDS);
  936. } else {
  937. size -= 2;
  938. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  939. IEEE80211_STYPE_NULLFUNC |
  940. IEEE80211_FCTL_FROMDS);
  941. }
  942. skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
  943. if (!skb)
  944. return;
  945. skb_reserve(skb, local->hw.extra_tx_headroom);
  946. nullfunc = (void *) skb_put(skb, size);
  947. nullfunc->frame_control = fc;
  948. nullfunc->duration_id = 0;
  949. memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
  950. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  951. memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
  952. skb->priority = tid;
  953. skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
  954. if (qos) {
  955. nullfunc->qos_ctrl = cpu_to_le16(tid);
  956. if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
  957. nullfunc->qos_ctrl |=
  958. cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
  959. }
  960. info = IEEE80211_SKB_CB(skb);
  961. /*
  962. * Tell TX path to send this frame even though the
  963. * STA may still remain is PS mode after this frame
  964. * exchange. Also set EOSP to indicate this packet
  965. * ends the poll/service period.
  966. */
  967. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
  968. IEEE80211_TX_CTL_PS_RESPONSE |
  969. IEEE80211_TX_STATUS_EOSP |
  970. IEEE80211_TX_CTL_REQ_TX_STATUS;
  971. drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
  972. skb->dev = sdata->dev;
  973. rcu_read_lock();
  974. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  975. if (WARN_ON(!chanctx_conf)) {
  976. rcu_read_unlock();
  977. kfree_skb(skb);
  978. return;
  979. }
  980. ieee80211_xmit(sdata, skb, chanctx_conf->def.chan->band);
  981. rcu_read_unlock();
  982. }
  983. static void
  984. ieee80211_sta_ps_deliver_response(struct sta_info *sta,
  985. int n_frames, u8 ignored_acs,
  986. enum ieee80211_frame_release_type reason)
  987. {
  988. struct ieee80211_sub_if_data *sdata = sta->sdata;
  989. struct ieee80211_local *local = sdata->local;
  990. bool found = false;
  991. bool more_data = false;
  992. int ac;
  993. unsigned long driver_release_tids = 0;
  994. struct sk_buff_head frames;
  995. /* Service or PS-Poll period starts */
  996. set_sta_flag(sta, WLAN_STA_SP);
  997. __skb_queue_head_init(&frames);
  998. /*
  999. * Get response frame(s) and more data bit for it.
  1000. */
  1001. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  1002. unsigned long tids;
  1003. if (ignored_acs & BIT(ac))
  1004. continue;
  1005. tids = ieee80211_tids_for_ac(ac);
  1006. if (!found) {
  1007. driver_release_tids = sta->driver_buffered_tids & tids;
  1008. if (driver_release_tids) {
  1009. found = true;
  1010. } else {
  1011. struct sk_buff *skb;
  1012. while (n_frames > 0) {
  1013. skb = skb_dequeue(&sta->tx_filtered[ac]);
  1014. if (!skb) {
  1015. skb = skb_dequeue(
  1016. &sta->ps_tx_buf[ac]);
  1017. if (skb)
  1018. local->total_ps_buffered--;
  1019. }
  1020. if (!skb)
  1021. break;
  1022. n_frames--;
  1023. found = true;
  1024. __skb_queue_tail(&frames, skb);
  1025. }
  1026. }
  1027. /*
  1028. * If the driver has data on more than one TID then
  1029. * certainly there's more data if we release just a
  1030. * single frame now (from a single TID).
  1031. */
  1032. if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
  1033. hweight16(driver_release_tids) > 1) {
  1034. more_data = true;
  1035. driver_release_tids =
  1036. BIT(ffs(driver_release_tids) - 1);
  1037. break;
  1038. }
  1039. }
  1040. if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
  1041. !skb_queue_empty(&sta->ps_tx_buf[ac])) {
  1042. more_data = true;
  1043. break;
  1044. }
  1045. }
  1046. if (!found) {
  1047. int tid;
  1048. /*
  1049. * For PS-Poll, this can only happen due to a race condition
  1050. * when we set the TIM bit and the station notices it, but
  1051. * before it can poll for the frame we expire it.
  1052. *
  1053. * For uAPSD, this is said in the standard (11.2.1.5 h):
  1054. * At each unscheduled SP for a non-AP STA, the AP shall
  1055. * attempt to transmit at least one MSDU or MMPDU, but no
  1056. * more than the value specified in the Max SP Length field
  1057. * in the QoS Capability element from delivery-enabled ACs,
  1058. * that are destined for the non-AP STA.
  1059. *
  1060. * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
  1061. */
  1062. /* This will evaluate to 1, 3, 5 or 7. */
  1063. tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
  1064. ieee80211_send_null_response(sdata, sta, tid, reason);
  1065. return;
  1066. }
  1067. if (!driver_release_tids) {
  1068. struct sk_buff_head pending;
  1069. struct sk_buff *skb;
  1070. int num = 0;
  1071. u16 tids = 0;
  1072. skb_queue_head_init(&pending);
  1073. while ((skb = __skb_dequeue(&frames))) {
  1074. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1075. struct ieee80211_hdr *hdr = (void *) skb->data;
  1076. u8 *qoshdr = NULL;
  1077. num++;
  1078. /*
  1079. * Tell TX path to send this frame even though the
  1080. * STA may still remain is PS mode after this frame
  1081. * exchange.
  1082. */
  1083. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
  1084. IEEE80211_TX_CTL_PS_RESPONSE;
  1085. /*
  1086. * Use MoreData flag to indicate whether there are
  1087. * more buffered frames for this STA
  1088. */
  1089. if (more_data || !skb_queue_empty(&frames))
  1090. hdr->frame_control |=
  1091. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1092. else
  1093. hdr->frame_control &=
  1094. cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  1095. if (ieee80211_is_data_qos(hdr->frame_control) ||
  1096. ieee80211_is_qos_nullfunc(hdr->frame_control))
  1097. qoshdr = ieee80211_get_qos_ctl(hdr);
  1098. /* end service period after last frame */
  1099. if (skb_queue_empty(&frames)) {
  1100. if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
  1101. qoshdr)
  1102. *qoshdr |= IEEE80211_QOS_CTL_EOSP;
  1103. info->flags |= IEEE80211_TX_STATUS_EOSP |
  1104. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1105. }
  1106. if (qoshdr)
  1107. tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
  1108. else
  1109. tids |= BIT(0);
  1110. __skb_queue_tail(&pending, skb);
  1111. }
  1112. drv_allow_buffered_frames(local, sta, tids, num,
  1113. reason, more_data);
  1114. ieee80211_add_pending_skbs(local, &pending);
  1115. sta_info_recalc_tim(sta);
  1116. } else {
  1117. /*
  1118. * We need to release a frame that is buffered somewhere in the
  1119. * driver ... it'll have to handle that.
  1120. * Note that, as per the comment above, it'll also have to see
  1121. * if there is more than just one frame on the specific TID that
  1122. * we're releasing from, and it needs to set the more-data bit
  1123. * accordingly if we tell it that there's no more data. If we do
  1124. * tell it there's more data, then of course the more-data bit
  1125. * needs to be set anyway.
  1126. */
  1127. drv_release_buffered_frames(local, sta, driver_release_tids,
  1128. n_frames, reason, more_data);
  1129. /*
  1130. * Note that we don't recalculate the TIM bit here as it would
  1131. * most likely have no effect at all unless the driver told us
  1132. * that the TID became empty before returning here from the
  1133. * release function.
  1134. * Either way, however, when the driver tells us that the TID
  1135. * became empty we'll do the TIM recalculation.
  1136. */
  1137. }
  1138. }
  1139. void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
  1140. {
  1141. u8 ignore_for_response = sta->sta.uapsd_queues;
  1142. /*
  1143. * If all ACs are delivery-enabled then we should reply
  1144. * from any of them, if only some are enabled we reply
  1145. * only from the non-enabled ones.
  1146. */
  1147. if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
  1148. ignore_for_response = 0;
  1149. ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
  1150. IEEE80211_FRAME_RELEASE_PSPOLL);
  1151. }
  1152. void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
  1153. {
  1154. int n_frames = sta->sta.max_sp;
  1155. u8 delivery_enabled = sta->sta.uapsd_queues;
  1156. /*
  1157. * If we ever grow support for TSPEC this might happen if
  1158. * the TSPEC update from hostapd comes in between a trigger
  1159. * frame setting WLAN_STA_UAPSD in the RX path and this
  1160. * actually getting called.
  1161. */
  1162. if (!delivery_enabled)
  1163. return;
  1164. switch (sta->sta.max_sp) {
  1165. case 1:
  1166. n_frames = 2;
  1167. break;
  1168. case 2:
  1169. n_frames = 4;
  1170. break;
  1171. case 3:
  1172. n_frames = 6;
  1173. break;
  1174. case 0:
  1175. /* XXX: what is a good value? */
  1176. n_frames = 8;
  1177. break;
  1178. }
  1179. ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
  1180. IEEE80211_FRAME_RELEASE_UAPSD);
  1181. }
  1182. void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
  1183. struct ieee80211_sta *pubsta, bool block)
  1184. {
  1185. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1186. trace_api_sta_block_awake(sta->local, pubsta, block);
  1187. if (block)
  1188. set_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1189. else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
  1190. ieee80211_queue_work(hw, &sta->drv_unblock_wk);
  1191. }
  1192. EXPORT_SYMBOL(ieee80211_sta_block_awake);
  1193. void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
  1194. {
  1195. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1196. struct ieee80211_local *local = sta->local;
  1197. trace_api_eosp(local, pubsta);
  1198. clear_sta_flag(sta, WLAN_STA_SP);
  1199. }
  1200. EXPORT_SYMBOL(ieee80211_sta_eosp);
  1201. void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
  1202. u8 tid, bool buffered)
  1203. {
  1204. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1205. if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
  1206. return;
  1207. if (buffered)
  1208. set_bit(tid, &sta->driver_buffered_tids);
  1209. else
  1210. clear_bit(tid, &sta->driver_buffered_tids);
  1211. sta_info_recalc_tim(sta);
  1212. }
  1213. EXPORT_SYMBOL(ieee80211_sta_set_buffered);
  1214. int sta_info_move_state(struct sta_info *sta,
  1215. enum ieee80211_sta_state new_state)
  1216. {
  1217. might_sleep();
  1218. if (sta->sta_state == new_state)
  1219. return 0;
  1220. /* check allowed transitions first */
  1221. switch (new_state) {
  1222. case IEEE80211_STA_NONE:
  1223. if (sta->sta_state != IEEE80211_STA_AUTH)
  1224. return -EINVAL;
  1225. break;
  1226. case IEEE80211_STA_AUTH:
  1227. if (sta->sta_state != IEEE80211_STA_NONE &&
  1228. sta->sta_state != IEEE80211_STA_ASSOC)
  1229. return -EINVAL;
  1230. break;
  1231. case IEEE80211_STA_ASSOC:
  1232. if (sta->sta_state != IEEE80211_STA_AUTH &&
  1233. sta->sta_state != IEEE80211_STA_AUTHORIZED)
  1234. return -EINVAL;
  1235. break;
  1236. case IEEE80211_STA_AUTHORIZED:
  1237. if (sta->sta_state != IEEE80211_STA_ASSOC)
  1238. return -EINVAL;
  1239. break;
  1240. default:
  1241. WARN(1, "invalid state %d", new_state);
  1242. return -EINVAL;
  1243. }
  1244. sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
  1245. sta->sta.addr, new_state);
  1246. /*
  1247. * notify the driver before the actual changes so it can
  1248. * fail the transition
  1249. */
  1250. if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
  1251. int err = drv_sta_state(sta->local, sta->sdata, sta,
  1252. sta->sta_state, new_state);
  1253. if (err)
  1254. return err;
  1255. }
  1256. /* reflect the change in all state variables */
  1257. switch (new_state) {
  1258. case IEEE80211_STA_NONE:
  1259. if (sta->sta_state == IEEE80211_STA_AUTH)
  1260. clear_bit(WLAN_STA_AUTH, &sta->_flags);
  1261. break;
  1262. case IEEE80211_STA_AUTH:
  1263. if (sta->sta_state == IEEE80211_STA_NONE)
  1264. set_bit(WLAN_STA_AUTH, &sta->_flags);
  1265. else if (sta->sta_state == IEEE80211_STA_ASSOC)
  1266. clear_bit(WLAN_STA_ASSOC, &sta->_flags);
  1267. break;
  1268. case IEEE80211_STA_ASSOC:
  1269. if (sta->sta_state == IEEE80211_STA_AUTH) {
  1270. set_bit(WLAN_STA_ASSOC, &sta->_flags);
  1271. } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
  1272. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  1273. (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1274. !sta->sdata->u.vlan.sta))
  1275. atomic_dec(&sta->sdata->bss->num_mcast_sta);
  1276. clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1277. }
  1278. break;
  1279. case IEEE80211_STA_AUTHORIZED:
  1280. if (sta->sta_state == IEEE80211_STA_ASSOC) {
  1281. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  1282. (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1283. !sta->sdata->u.vlan.sta))
  1284. atomic_inc(&sta->sdata->bss->num_mcast_sta);
  1285. set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1286. }
  1287. break;
  1288. default:
  1289. break;
  1290. }
  1291. sta->sta_state = new_state;
  1292. return 0;
  1293. }
  1294. u8 sta_info_tx_streams(struct sta_info *sta)
  1295. {
  1296. struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
  1297. u8 rx_streams;
  1298. if (!sta->sta.ht_cap.ht_supported)
  1299. return 1;
  1300. if (sta->sta.vht_cap.vht_supported) {
  1301. int i;
  1302. u16 tx_mcs_map =
  1303. le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
  1304. for (i = 7; i >= 0; i--)
  1305. if ((tx_mcs_map & (0x3 << (i * 2))) !=
  1306. IEEE80211_VHT_MCS_NOT_SUPPORTED)
  1307. return i + 1;
  1308. }
  1309. if (ht_cap->mcs.rx_mask[3])
  1310. rx_streams = 4;
  1311. else if (ht_cap->mcs.rx_mask[2])
  1312. rx_streams = 3;
  1313. else if (ht_cap->mcs.rx_mask[1])
  1314. rx_streams = 2;
  1315. else
  1316. rx_streams = 1;
  1317. if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
  1318. return rx_streams;
  1319. return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
  1320. >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
  1321. }