sta_info.c 43 KB

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