sta_info.c 36 KB

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