sta_info.c 38 KB

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