sta_info.c 39 KB

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