sta_info.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  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_INIT_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_INIT_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. rate_control_put(sta->rate_ctrl);
  195. }
  196. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  197. wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr);
  198. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  199. kfree(sta);
  200. }
  201. /* Caller must hold local->sta_mtx */
  202. static void sta_info_hash_add(struct ieee80211_local *local,
  203. struct sta_info *sta)
  204. {
  205. lockdep_assert_held(&local->sta_mtx);
  206. sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
  207. RCU_INIT_POINTER(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
  208. }
  209. static void sta_unblock(struct work_struct *wk)
  210. {
  211. struct sta_info *sta;
  212. sta = container_of(wk, struct sta_info, drv_unblock_wk);
  213. if (sta->dead)
  214. return;
  215. if (!test_sta_flag(sta, WLAN_STA_PS_STA))
  216. ieee80211_sta_ps_deliver_wakeup(sta);
  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 = rate_control_get(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. rate_control_put(sta->rate_ctrl);
  240. return -ENOMEM;
  241. }
  242. return 0;
  243. }
  244. struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
  245. const u8 *addr, gfp_t gfp)
  246. {
  247. struct ieee80211_local *local = sdata->local;
  248. struct sta_info *sta;
  249. struct timespec uptime;
  250. int i;
  251. sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
  252. if (!sta)
  253. return NULL;
  254. spin_lock_init(&sta->lock);
  255. INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
  256. INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
  257. mutex_init(&sta->ampdu_mlme.mtx);
  258. memcpy(sta->sta.addr, addr, ETH_ALEN);
  259. sta->local = local;
  260. sta->sdata = sdata;
  261. sta->last_rx = jiffies;
  262. do_posix_clock_monotonic_gettime(&uptime);
  263. sta->last_connected = uptime.tv_sec;
  264. ewma_init(&sta->avg_signal, 1024, 8);
  265. if (sta_prepare_rate_control(local, sta, gfp)) {
  266. kfree(sta);
  267. return NULL;
  268. }
  269. for (i = 0; i < STA_TID_NUM; i++) {
  270. /*
  271. * timer_to_tid must be initialized with identity mapping
  272. * to enable session_timer's data differentiation. See
  273. * sta_rx_agg_session_timer_expired for usage.
  274. */
  275. sta->timer_to_tid[i] = i;
  276. }
  277. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  278. skb_queue_head_init(&sta->ps_tx_buf[i]);
  279. skb_queue_head_init(&sta->tx_filtered[i]);
  280. }
  281. for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
  282. sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
  283. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  284. wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
  285. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  286. #ifdef CONFIG_MAC80211_MESH
  287. sta->plink_state = NL80211_PLINK_LISTEN;
  288. init_timer(&sta->plink_timer);
  289. #endif
  290. return sta;
  291. }
  292. static int sta_info_insert_check(struct sta_info *sta)
  293. {
  294. struct ieee80211_sub_if_data *sdata = sta->sdata;
  295. /*
  296. * Can't be a WARN_ON because it can be triggered through a race:
  297. * something inserts a STA (on one CPU) without holding the RTNL
  298. * and another CPU turns off the net device.
  299. */
  300. if (unlikely(!ieee80211_sdata_running(sdata)))
  301. return -ENETDOWN;
  302. if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
  303. is_multicast_ether_addr(sta->sta.addr)))
  304. return -EINVAL;
  305. return 0;
  306. }
  307. /*
  308. * should be called with sta_mtx locked
  309. * this function replaces the mutex lock
  310. * with a RCU lock
  311. */
  312. static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
  313. {
  314. struct ieee80211_local *local = sta->local;
  315. struct ieee80211_sub_if_data *sdata = sta->sdata;
  316. struct sta_info *exist_sta;
  317. bool dummy_reinsert = false;
  318. int err = 0;
  319. lockdep_assert_held(&local->sta_mtx);
  320. /*
  321. * check if STA exists already.
  322. * only accept a scenario of a second call to sta_info_insert_finish
  323. * with a dummy station entry that was inserted earlier
  324. * in that case - assume that the dummy station flag should
  325. * be removed.
  326. */
  327. exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr);
  328. if (exist_sta) {
  329. if (exist_sta == sta && sta->dummy) {
  330. dummy_reinsert = true;
  331. } else {
  332. err = -EEXIST;
  333. goto out_err;
  334. }
  335. }
  336. if (!sta->dummy || dummy_reinsert) {
  337. /* notify driver */
  338. err = drv_sta_add(local, sdata, &sta->sta);
  339. if (err) {
  340. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  341. goto out_err;
  342. printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to "
  343. "driver (%d) - keeping it anyway.\n",
  344. sdata->name, sta->sta.addr, err);
  345. } else
  346. sta->uploaded = true;
  347. }
  348. if (!dummy_reinsert) {
  349. local->num_sta++;
  350. local->sta_generation++;
  351. smp_mb();
  352. /* make the station visible */
  353. sta_info_hash_add(local, sta);
  354. list_add(&sta->list, &local->sta_list);
  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. static 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. /*
  616. * Before removing the station from the driver and
  617. * rate control, it might still start new aggregation
  618. * sessions -- block that to make sure the tear-down
  619. * will be sufficient.
  620. */
  621. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  622. ieee80211_sta_tear_down_BA_sessions(sta, true);
  623. ret = sta_info_hash_del(local, sta);
  624. if (ret)
  625. return ret;
  626. list_del(&sta->list);
  627. mutex_lock(&local->key_mtx);
  628. for (i = 0; i < NUM_DEFAULT_KEYS; i++)
  629. __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
  630. if (sta->ptk)
  631. __ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
  632. mutex_unlock(&local->key_mtx);
  633. sta->dead = true;
  634. if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
  635. test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
  636. BUG_ON(!sdata->bss);
  637. clear_sta_flag(sta, WLAN_STA_PS_STA);
  638. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  639. atomic_dec(&sdata->bss->num_sta_ps);
  640. sta_info_recalc_tim(sta);
  641. }
  642. local->num_sta--;
  643. local->sta_generation++;
  644. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  645. RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
  646. while (sta->sta_state > IEEE80211_STA_NONE)
  647. sta_info_move_state(sta, sta->sta_state - 1);
  648. if (sta->uploaded) {
  649. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  650. sdata = container_of(sdata->bss,
  651. struct ieee80211_sub_if_data,
  652. u.ap);
  653. drv_sta_remove(local, sdata, &sta->sta);
  654. sdata = sta->sdata;
  655. }
  656. /*
  657. * At this point, after we wait for an RCU grace period,
  658. * neither mac80211 nor the driver can reference this
  659. * sta struct any more except by still existing timers
  660. * associated with this station that we clean up below.
  661. */
  662. synchronize_rcu();
  663. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  664. local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
  665. __skb_queue_purge(&sta->ps_tx_buf[ac]);
  666. __skb_queue_purge(&sta->tx_filtered[ac]);
  667. }
  668. #ifdef CONFIG_MAC80211_MESH
  669. if (ieee80211_vif_is_mesh(&sdata->vif))
  670. mesh_accept_plinks_update(sdata);
  671. #endif
  672. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  673. wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
  674. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  675. cancel_work_sync(&sta->drv_unblock_wk);
  676. cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
  677. rate_control_remove_sta_debugfs(sta);
  678. ieee80211_sta_debugfs_remove(sta);
  679. #ifdef CONFIG_MAC80211_MESH
  680. if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
  681. mesh_plink_deactivate(sta);
  682. del_timer_sync(&sta->plink_timer);
  683. }
  684. #endif
  685. /* There could be some memory leaks because of ampdu tx pending queue
  686. * not being freed before destroying the station info.
  687. *
  688. * Make sure that such queues are purged before freeing the station
  689. * info.
  690. * TODO: We have to somehow postpone the full destruction
  691. * until the aggregation stop completes. Refer
  692. * http://thread.gmane.org/gmane.linux.kernel.wireless.general/81936
  693. */
  694. mutex_lock(&sta->ampdu_mlme.mtx);
  695. for (i = 0; i < STA_TID_NUM; i++) {
  696. tid_tx = rcu_dereference_protected_tid_tx(sta, i);
  697. if (!tid_tx)
  698. continue;
  699. if (skb_queue_len(&tid_tx->pending)) {
  700. #ifdef CONFIG_MAC80211_HT_DEBUG
  701. wiphy_debug(local->hw.wiphy, "TX A-MPDU purging %d "
  702. "packets for tid=%d\n",
  703. skb_queue_len(&tid_tx->pending), i);
  704. #endif /* CONFIG_MAC80211_HT_DEBUG */
  705. __skb_queue_purge(&tid_tx->pending);
  706. }
  707. kfree_rcu(tid_tx, rcu_head);
  708. }
  709. mutex_unlock(&sta->ampdu_mlme.mtx);
  710. sta_info_free(local, sta);
  711. return 0;
  712. }
  713. int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
  714. {
  715. struct sta_info *sta;
  716. int ret;
  717. mutex_lock(&sdata->local->sta_mtx);
  718. sta = sta_info_get_rx(sdata, addr);
  719. ret = __sta_info_destroy(sta);
  720. mutex_unlock(&sdata->local->sta_mtx);
  721. return ret;
  722. }
  723. int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
  724. const u8 *addr)
  725. {
  726. struct sta_info *sta;
  727. int ret;
  728. mutex_lock(&sdata->local->sta_mtx);
  729. sta = sta_info_get_bss_rx(sdata, addr);
  730. ret = __sta_info_destroy(sta);
  731. mutex_unlock(&sdata->local->sta_mtx);
  732. return ret;
  733. }
  734. static void sta_info_cleanup(unsigned long data)
  735. {
  736. struct ieee80211_local *local = (struct ieee80211_local *) data;
  737. struct sta_info *sta;
  738. bool timer_needed = false;
  739. rcu_read_lock();
  740. list_for_each_entry_rcu(sta, &local->sta_list, list)
  741. if (sta_info_cleanup_expire_buffered(local, sta))
  742. timer_needed = true;
  743. rcu_read_unlock();
  744. if (local->quiescing)
  745. return;
  746. if (!timer_needed)
  747. return;
  748. mod_timer(&local->sta_cleanup,
  749. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
  750. }
  751. void sta_info_init(struct ieee80211_local *local)
  752. {
  753. spin_lock_init(&local->tim_lock);
  754. mutex_init(&local->sta_mtx);
  755. INIT_LIST_HEAD(&local->sta_list);
  756. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  757. (unsigned long)local);
  758. }
  759. void sta_info_stop(struct ieee80211_local *local)
  760. {
  761. del_timer(&local->sta_cleanup);
  762. sta_info_flush(local, NULL);
  763. }
  764. /**
  765. * sta_info_flush - flush matching STA entries from the STA table
  766. *
  767. * Returns the number of removed STA entries.
  768. *
  769. * @local: local interface data
  770. * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
  771. */
  772. int sta_info_flush(struct ieee80211_local *local,
  773. struct ieee80211_sub_if_data *sdata)
  774. {
  775. struct sta_info *sta, *tmp;
  776. int ret = 0;
  777. might_sleep();
  778. mutex_lock(&local->sta_mtx);
  779. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  780. if (!sdata || sdata == sta->sdata)
  781. WARN_ON(__sta_info_destroy(sta));
  782. }
  783. mutex_unlock(&local->sta_mtx);
  784. return ret;
  785. }
  786. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  787. unsigned long exp_time)
  788. {
  789. struct ieee80211_local *local = sdata->local;
  790. struct sta_info *sta, *tmp;
  791. mutex_lock(&local->sta_mtx);
  792. list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
  793. if (time_after(jiffies, sta->last_rx + exp_time)) {
  794. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  795. printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
  796. sdata->name, sta->sta.addr);
  797. #endif
  798. WARN_ON(__sta_info_destroy(sta));
  799. }
  800. mutex_unlock(&local->sta_mtx);
  801. }
  802. struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
  803. const u8 *addr,
  804. const u8 *localaddr)
  805. {
  806. struct sta_info *sta, *nxt;
  807. /*
  808. * Just return a random station if localaddr is NULL
  809. * ... first in list.
  810. */
  811. for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
  812. if (localaddr &&
  813. compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
  814. continue;
  815. if (!sta->uploaded)
  816. return NULL;
  817. return &sta->sta;
  818. }
  819. return NULL;
  820. }
  821. EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
  822. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
  823. const u8 *addr)
  824. {
  825. struct sta_info *sta;
  826. if (!vif)
  827. return NULL;
  828. sta = sta_info_get_bss(vif_to_sdata(vif), addr);
  829. if (!sta)
  830. return NULL;
  831. if (!sta->uploaded)
  832. return NULL;
  833. return &sta->sta;
  834. }
  835. EXPORT_SYMBOL(ieee80211_find_sta);
  836. static void clear_sta_ps_flags(void *_sta)
  837. {
  838. struct sta_info *sta = _sta;
  839. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  840. clear_sta_flag(sta, WLAN_STA_PS_STA);
  841. }
  842. /* powersave support code */
  843. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  844. {
  845. struct ieee80211_sub_if_data *sdata = sta->sdata;
  846. struct ieee80211_local *local = sdata->local;
  847. struct sk_buff_head pending;
  848. int filtered = 0, buffered = 0, ac;
  849. clear_sta_flag(sta, WLAN_STA_SP);
  850. BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1);
  851. sta->driver_buffered_tids = 0;
  852. if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
  853. drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
  854. skb_queue_head_init(&pending);
  855. /* Send all buffered frames to the station */
  856. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  857. int count = skb_queue_len(&pending), tmp;
  858. skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
  859. tmp = skb_queue_len(&pending);
  860. filtered += tmp - count;
  861. count = tmp;
  862. skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
  863. tmp = skb_queue_len(&pending);
  864. buffered += tmp - count;
  865. }
  866. ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
  867. local->total_ps_buffered -= buffered;
  868. sta_info_recalc_tim(sta);
  869. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  870. printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
  871. "since STA not sleeping anymore\n", sdata->name,
  872. sta->sta.addr, sta->sta.aid, filtered, buffered);
  873. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  874. }
  875. static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
  876. struct sta_info *sta, int tid,
  877. enum ieee80211_frame_release_type reason)
  878. {
  879. struct ieee80211_local *local = sdata->local;
  880. struct ieee80211_qos_hdr *nullfunc;
  881. struct sk_buff *skb;
  882. int size = sizeof(*nullfunc);
  883. __le16 fc;
  884. bool qos = test_sta_flag(sta, WLAN_STA_WME);
  885. struct ieee80211_tx_info *info;
  886. if (qos) {
  887. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  888. IEEE80211_STYPE_QOS_NULLFUNC |
  889. IEEE80211_FCTL_FROMDS);
  890. } else {
  891. size -= 2;
  892. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  893. IEEE80211_STYPE_NULLFUNC |
  894. IEEE80211_FCTL_FROMDS);
  895. }
  896. skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
  897. if (!skb)
  898. return;
  899. skb_reserve(skb, local->hw.extra_tx_headroom);
  900. nullfunc = (void *) skb_put(skb, size);
  901. nullfunc->frame_control = fc;
  902. nullfunc->duration_id = 0;
  903. memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
  904. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  905. memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
  906. skb->priority = tid;
  907. skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
  908. if (qos) {
  909. nullfunc->qos_ctrl = cpu_to_le16(tid);
  910. if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
  911. nullfunc->qos_ctrl |=
  912. cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
  913. }
  914. info = IEEE80211_SKB_CB(skb);
  915. /*
  916. * Tell TX path to send this frame even though the
  917. * STA may still remain is PS mode after this frame
  918. * exchange. Also set EOSP to indicate this packet
  919. * ends the poll/service period.
  920. */
  921. info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE |
  922. IEEE80211_TX_STATUS_EOSP |
  923. IEEE80211_TX_CTL_REQ_TX_STATUS;
  924. drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
  925. ieee80211_xmit(sdata, skb);
  926. }
  927. static void
  928. ieee80211_sta_ps_deliver_response(struct sta_info *sta,
  929. int n_frames, u8 ignored_acs,
  930. enum ieee80211_frame_release_type reason)
  931. {
  932. struct ieee80211_sub_if_data *sdata = sta->sdata;
  933. struct ieee80211_local *local = sdata->local;
  934. bool found = false;
  935. bool more_data = false;
  936. int ac;
  937. unsigned long driver_release_tids = 0;
  938. struct sk_buff_head frames;
  939. /* Service or PS-Poll period starts */
  940. set_sta_flag(sta, WLAN_STA_SP);
  941. __skb_queue_head_init(&frames);
  942. /*
  943. * Get response frame(s) and more data bit for it.
  944. */
  945. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  946. unsigned long tids;
  947. if (ignored_acs & BIT(ac))
  948. continue;
  949. tids = ieee80211_tids_for_ac(ac);
  950. if (!found) {
  951. driver_release_tids = sta->driver_buffered_tids & tids;
  952. if (driver_release_tids) {
  953. found = true;
  954. } else {
  955. struct sk_buff *skb;
  956. while (n_frames > 0) {
  957. skb = skb_dequeue(&sta->tx_filtered[ac]);
  958. if (!skb) {
  959. skb = skb_dequeue(
  960. &sta->ps_tx_buf[ac]);
  961. if (skb)
  962. local->total_ps_buffered--;
  963. }
  964. if (!skb)
  965. break;
  966. n_frames--;
  967. found = true;
  968. __skb_queue_tail(&frames, skb);
  969. }
  970. }
  971. /*
  972. * If the driver has data on more than one TID then
  973. * certainly there's more data if we release just a
  974. * single frame now (from a single TID).
  975. */
  976. if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
  977. hweight16(driver_release_tids) > 1) {
  978. more_data = true;
  979. driver_release_tids =
  980. BIT(ffs(driver_release_tids) - 1);
  981. break;
  982. }
  983. }
  984. if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
  985. !skb_queue_empty(&sta->ps_tx_buf[ac])) {
  986. more_data = true;
  987. break;
  988. }
  989. }
  990. if (!found) {
  991. int tid;
  992. /*
  993. * For PS-Poll, this can only happen due to a race condition
  994. * when we set the TIM bit and the station notices it, but
  995. * before it can poll for the frame we expire it.
  996. *
  997. * For uAPSD, this is said in the standard (11.2.1.5 h):
  998. * At each unscheduled SP for a non-AP STA, the AP shall
  999. * attempt to transmit at least one MSDU or MMPDU, but no
  1000. * more than the value specified in the Max SP Length field
  1001. * in the QoS Capability element from delivery-enabled ACs,
  1002. * that are destined for the non-AP STA.
  1003. *
  1004. * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
  1005. */
  1006. /* This will evaluate to 1, 3, 5 or 7. */
  1007. tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
  1008. ieee80211_send_null_response(sdata, sta, tid, reason);
  1009. return;
  1010. }
  1011. if (!driver_release_tids) {
  1012. struct sk_buff_head pending;
  1013. struct sk_buff *skb;
  1014. int num = 0;
  1015. u16 tids = 0;
  1016. skb_queue_head_init(&pending);
  1017. while ((skb = __skb_dequeue(&frames))) {
  1018. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1019. struct ieee80211_hdr *hdr = (void *) skb->data;
  1020. u8 *qoshdr = NULL;
  1021. num++;
  1022. /*
  1023. * Tell TX path to send this frame even though the
  1024. * STA may still remain is PS mode after this frame
  1025. * exchange.
  1026. */
  1027. info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE;
  1028. /*
  1029. * Use MoreData flag to indicate whether there are
  1030. * more buffered frames for this STA
  1031. */
  1032. if (more_data || !skb_queue_empty(&frames))
  1033. hdr->frame_control |=
  1034. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1035. else
  1036. hdr->frame_control &=
  1037. cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  1038. if (ieee80211_is_data_qos(hdr->frame_control) ||
  1039. ieee80211_is_qos_nullfunc(hdr->frame_control))
  1040. qoshdr = ieee80211_get_qos_ctl(hdr);
  1041. /* set EOSP for the frame */
  1042. if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
  1043. qoshdr && skb_queue_empty(&frames))
  1044. *qoshdr |= IEEE80211_QOS_CTL_EOSP;
  1045. info->flags |= IEEE80211_TX_STATUS_EOSP |
  1046. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1047. if (qoshdr)
  1048. tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
  1049. else
  1050. tids |= BIT(0);
  1051. __skb_queue_tail(&pending, skb);
  1052. }
  1053. drv_allow_buffered_frames(local, sta, tids, num,
  1054. reason, more_data);
  1055. ieee80211_add_pending_skbs(local, &pending);
  1056. sta_info_recalc_tim(sta);
  1057. } else {
  1058. /*
  1059. * We need to release a frame that is buffered somewhere in the
  1060. * driver ... it'll have to handle that.
  1061. * Note that, as per the comment above, it'll also have to see
  1062. * if there is more than just one frame on the specific TID that
  1063. * we're releasing from, and it needs to set the more-data bit
  1064. * accordingly if we tell it that there's no more data. If we do
  1065. * tell it there's more data, then of course the more-data bit
  1066. * needs to be set anyway.
  1067. */
  1068. drv_release_buffered_frames(local, sta, driver_release_tids,
  1069. n_frames, reason, more_data);
  1070. /*
  1071. * Note that we don't recalculate the TIM bit here as it would
  1072. * most likely have no effect at all unless the driver told us
  1073. * that the TID became empty before returning here from the
  1074. * release function.
  1075. * Either way, however, when the driver tells us that the TID
  1076. * became empty we'll do the TIM recalculation.
  1077. */
  1078. }
  1079. }
  1080. void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
  1081. {
  1082. u8 ignore_for_response = sta->sta.uapsd_queues;
  1083. /*
  1084. * If all ACs are delivery-enabled then we should reply
  1085. * from any of them, if only some are enabled we reply
  1086. * only from the non-enabled ones.
  1087. */
  1088. if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
  1089. ignore_for_response = 0;
  1090. ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
  1091. IEEE80211_FRAME_RELEASE_PSPOLL);
  1092. }
  1093. void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
  1094. {
  1095. int n_frames = sta->sta.max_sp;
  1096. u8 delivery_enabled = sta->sta.uapsd_queues;
  1097. /*
  1098. * If we ever grow support for TSPEC this might happen if
  1099. * the TSPEC update from hostapd comes in between a trigger
  1100. * frame setting WLAN_STA_UAPSD in the RX path and this
  1101. * actually getting called.
  1102. */
  1103. if (!delivery_enabled)
  1104. return;
  1105. switch (sta->sta.max_sp) {
  1106. case 1:
  1107. n_frames = 2;
  1108. break;
  1109. case 2:
  1110. n_frames = 4;
  1111. break;
  1112. case 3:
  1113. n_frames = 6;
  1114. break;
  1115. case 0:
  1116. /* XXX: what is a good value? */
  1117. n_frames = 8;
  1118. break;
  1119. }
  1120. ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
  1121. IEEE80211_FRAME_RELEASE_UAPSD);
  1122. }
  1123. void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
  1124. struct ieee80211_sta *pubsta, bool block)
  1125. {
  1126. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1127. trace_api_sta_block_awake(sta->local, pubsta, block);
  1128. if (block)
  1129. set_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1130. else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
  1131. ieee80211_queue_work(hw, &sta->drv_unblock_wk);
  1132. }
  1133. EXPORT_SYMBOL(ieee80211_sta_block_awake);
  1134. void ieee80211_sta_eosp_irqsafe(struct ieee80211_sta *pubsta)
  1135. {
  1136. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1137. struct ieee80211_local *local = sta->local;
  1138. struct sk_buff *skb;
  1139. struct skb_eosp_msg_data *data;
  1140. trace_api_eosp(local, pubsta);
  1141. skb = alloc_skb(0, GFP_ATOMIC);
  1142. if (!skb) {
  1143. /* too bad ... but race is better than loss */
  1144. clear_sta_flag(sta, WLAN_STA_SP);
  1145. return;
  1146. }
  1147. data = (void *)skb->cb;
  1148. memcpy(data->sta, pubsta->addr, ETH_ALEN);
  1149. memcpy(data->iface, sta->sdata->vif.addr, ETH_ALEN);
  1150. skb->pkt_type = IEEE80211_EOSP_MSG;
  1151. skb_queue_tail(&local->skb_queue, skb);
  1152. tasklet_schedule(&local->tasklet);
  1153. }
  1154. EXPORT_SYMBOL(ieee80211_sta_eosp_irqsafe);
  1155. void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
  1156. u8 tid, bool buffered)
  1157. {
  1158. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1159. if (WARN_ON(tid >= STA_TID_NUM))
  1160. return;
  1161. if (buffered)
  1162. set_bit(tid, &sta->driver_buffered_tids);
  1163. else
  1164. clear_bit(tid, &sta->driver_buffered_tids);
  1165. sta_info_recalc_tim(sta);
  1166. }
  1167. EXPORT_SYMBOL(ieee80211_sta_set_buffered);
  1168. int sta_info_move_state_checked(struct sta_info *sta,
  1169. enum ieee80211_sta_state new_state)
  1170. {
  1171. might_sleep();
  1172. if (sta->sta_state == new_state)
  1173. return 0;
  1174. switch (new_state) {
  1175. case IEEE80211_STA_NONE:
  1176. if (sta->sta_state == IEEE80211_STA_AUTH)
  1177. clear_bit(WLAN_STA_AUTH, &sta->_flags);
  1178. else
  1179. return -EINVAL;
  1180. break;
  1181. case IEEE80211_STA_AUTH:
  1182. if (sta->sta_state == IEEE80211_STA_NONE)
  1183. set_bit(WLAN_STA_AUTH, &sta->_flags);
  1184. else if (sta->sta_state == IEEE80211_STA_ASSOC)
  1185. clear_bit(WLAN_STA_ASSOC, &sta->_flags);
  1186. else
  1187. return -EINVAL;
  1188. break;
  1189. case IEEE80211_STA_ASSOC:
  1190. if (sta->sta_state == IEEE80211_STA_AUTH) {
  1191. set_bit(WLAN_STA_ASSOC, &sta->_flags);
  1192. } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
  1193. if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
  1194. atomic_dec(&sta->sdata->u.ap.num_sta_authorized);
  1195. clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1196. } else
  1197. return -EINVAL;
  1198. break;
  1199. case IEEE80211_STA_AUTHORIZED:
  1200. if (sta->sta_state == IEEE80211_STA_ASSOC) {
  1201. if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
  1202. atomic_inc(&sta->sdata->u.ap.num_sta_authorized);
  1203. set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1204. } else
  1205. return -EINVAL;
  1206. break;
  1207. default:
  1208. WARN(1, "invalid state %d", new_state);
  1209. return -EINVAL;
  1210. }
  1211. printk(KERN_DEBUG "%s: moving STA %pM to state %d\n",
  1212. sta->sdata->name, sta->sta.addr, new_state);
  1213. sta->sta_state = new_state;
  1214. return 0;
  1215. }