sta_info.c 38 KB

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