sta_info.c 41 KB

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