sta_info.c 37 KB

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