sta_info.c 41 KB

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