sta_info.c 37 KB

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