sta_info.c 39 KB

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