sta_info.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. /**
  26. * DOC: STA information lifetime rules
  27. *
  28. * STA info structures (&struct sta_info) are managed in a hash table
  29. * for faster lookup and a list for iteration. They are managed using
  30. * RCU, i.e. access to the list and hash table is protected by RCU.
  31. *
  32. * Upon allocating a STA info structure with sta_info_alloc(), the caller
  33. * owns that structure. It must then insert it into the hash table using
  34. * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
  35. * case (which acquires an rcu read section but must not be called from
  36. * within one) will the pointer still be valid after the call. Note that
  37. * the caller may not do much with the STA info before inserting it, in
  38. * particular, it may not start any mesh peer link management or add
  39. * encryption keys.
  40. *
  41. * When the insertion fails (sta_info_insert()) returns non-zero), the
  42. * structure will have been freed by sta_info_insert()!
  43. *
  44. * Station entries are added by mac80211 when you establish a link with a
  45. * peer. This means different things for the different type of interfaces
  46. * we support. For a regular station this mean we add the AP sta when we
  47. * receive an association response from the AP. For IBSS this occurs when
  48. * get to know about a peer on the same IBSS. For WDS we add the sta for
  49. * the peer immediately upon device open. When using AP mode we add stations
  50. * for each respective station upon request from userspace through nl80211.
  51. *
  52. * In order to remove a STA info structure, various sta_info_destroy_*()
  53. * calls are available.
  54. *
  55. * There is no concept of ownership on a STA entry, each structure is
  56. * owned by the global hash table/list until it is removed. All users of
  57. * the structure need to be RCU protected so that the structure won't be
  58. * freed before they are done using it.
  59. */
  60. /* Caller must hold local->sta_lock */
  61. static int sta_info_hash_del(struct ieee80211_local *local,
  62. struct sta_info *sta)
  63. {
  64. struct sta_info *s;
  65. s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
  66. lockdep_is_held(&local->sta_lock));
  67. if (!s)
  68. return -ENOENT;
  69. if (s == sta) {
  70. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
  71. s->hnext);
  72. return 0;
  73. }
  74. while (rcu_access_pointer(s->hnext) &&
  75. rcu_access_pointer(s->hnext) != sta)
  76. s = rcu_dereference_protected(s->hnext,
  77. lockdep_is_held(&local->sta_lock));
  78. if (rcu_access_pointer(s->hnext)) {
  79. rcu_assign_pointer(s->hnext, sta->hnext);
  80. return 0;
  81. }
  82. return -ENOENT;
  83. }
  84. /* protected by RCU */
  85. struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
  86. const u8 *addr)
  87. {
  88. struct ieee80211_local *local = sdata->local;
  89. struct sta_info *sta;
  90. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  91. lockdep_is_held(&local->sta_lock) ||
  92. lockdep_is_held(&local->sta_mtx));
  93. while (sta) {
  94. if (sta->sdata == sdata &&
  95. memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
  96. break;
  97. sta = rcu_dereference_check(sta->hnext,
  98. lockdep_is_held(&local->sta_lock) ||
  99. lockdep_is_held(&local->sta_mtx));
  100. }
  101. return sta;
  102. }
  103. /*
  104. * Get sta info either from the specified interface
  105. * or from one of its vlans
  106. */
  107. struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
  108. const u8 *addr)
  109. {
  110. struct ieee80211_local *local = sdata->local;
  111. struct sta_info *sta;
  112. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  113. lockdep_is_held(&local->sta_lock) ||
  114. lockdep_is_held(&local->sta_mtx));
  115. while (sta) {
  116. if ((sta->sdata == sdata ||
  117. (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
  118. memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
  119. break;
  120. sta = rcu_dereference_check(sta->hnext,
  121. lockdep_is_held(&local->sta_lock) ||
  122. lockdep_is_held(&local->sta_mtx));
  123. }
  124. return sta;
  125. }
  126. struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
  127. int idx)
  128. {
  129. struct ieee80211_local *local = sdata->local;
  130. struct sta_info *sta;
  131. int i = 0;
  132. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  133. if (sdata != sta->sdata)
  134. continue;
  135. if (i < idx) {
  136. ++i;
  137. continue;
  138. }
  139. return sta;
  140. }
  141. return NULL;
  142. }
  143. /**
  144. * __sta_info_free - internal STA free helper
  145. *
  146. * @local: pointer to the global information
  147. * @sta: STA info to free
  148. *
  149. * This function must undo everything done by sta_info_alloc()
  150. * that may happen before sta_info_insert().
  151. */
  152. static void __sta_info_free(struct ieee80211_local *local,
  153. struct sta_info *sta)
  154. {
  155. if (sta->rate_ctrl) {
  156. rate_control_free_sta(sta);
  157. rate_control_put(sta->rate_ctrl);
  158. }
  159. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  160. wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr);
  161. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  162. kfree(sta);
  163. }
  164. /* Caller must hold local->sta_lock */
  165. static void sta_info_hash_add(struct ieee80211_local *local,
  166. struct sta_info *sta)
  167. {
  168. sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
  169. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
  170. }
  171. static void sta_unblock(struct work_struct *wk)
  172. {
  173. struct sta_info *sta;
  174. sta = container_of(wk, struct sta_info, drv_unblock_wk);
  175. if (sta->dead)
  176. return;
  177. if (!test_sta_flags(sta, WLAN_STA_PS_STA))
  178. ieee80211_sta_ps_deliver_wakeup(sta);
  179. else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL)) {
  180. clear_sta_flags(sta, WLAN_STA_PS_DRIVER);
  181. ieee80211_sta_ps_deliver_poll_response(sta);
  182. } else
  183. clear_sta_flags(sta, WLAN_STA_PS_DRIVER);
  184. }
  185. static int sta_prepare_rate_control(struct ieee80211_local *local,
  186. struct sta_info *sta, gfp_t gfp)
  187. {
  188. if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
  189. return 0;
  190. sta->rate_ctrl = rate_control_get(local->rate_ctrl);
  191. sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
  192. &sta->sta, gfp);
  193. if (!sta->rate_ctrl_priv) {
  194. rate_control_put(sta->rate_ctrl);
  195. return -ENOMEM;
  196. }
  197. return 0;
  198. }
  199. struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
  200. u8 *addr, gfp_t gfp)
  201. {
  202. struct ieee80211_local *local = sdata->local;
  203. struct sta_info *sta;
  204. struct timespec uptime;
  205. int i;
  206. sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
  207. if (!sta)
  208. return NULL;
  209. spin_lock_init(&sta->lock);
  210. spin_lock_init(&sta->flaglock);
  211. INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
  212. INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
  213. mutex_init(&sta->ampdu_mlme.mtx);
  214. memcpy(sta->sta.addr, addr, ETH_ALEN);
  215. sta->local = local;
  216. sta->sdata = sdata;
  217. sta->last_rx = jiffies;
  218. do_posix_clock_monotonic_gettime(&uptime);
  219. sta->last_connected = uptime.tv_sec;
  220. ewma_init(&sta->avg_signal, 1024, 8);
  221. if (sta_prepare_rate_control(local, sta, gfp)) {
  222. kfree(sta);
  223. return NULL;
  224. }
  225. for (i = 0; i < STA_TID_NUM; i++) {
  226. /*
  227. * timer_to_tid must be initialized with identity mapping
  228. * to enable session_timer's data differentiation. See
  229. * sta_rx_agg_session_timer_expired for usage.
  230. */
  231. sta->timer_to_tid[i] = i;
  232. }
  233. skb_queue_head_init(&sta->ps_tx_buf);
  234. skb_queue_head_init(&sta->tx_filtered);
  235. for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
  236. sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
  237. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  238. wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
  239. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  240. #ifdef CONFIG_MAC80211_MESH
  241. sta->plink_state = NL80211_PLINK_LISTEN;
  242. init_timer(&sta->plink_timer);
  243. #endif
  244. return sta;
  245. }
  246. static int sta_info_finish_insert(struct sta_info *sta, bool async)
  247. {
  248. struct ieee80211_local *local = sta->local;
  249. struct ieee80211_sub_if_data *sdata = sta->sdata;
  250. struct station_info sinfo;
  251. unsigned long flags;
  252. int err = 0;
  253. lockdep_assert_held(&local->sta_mtx);
  254. /* notify driver */
  255. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  256. sdata = container_of(sdata->bss,
  257. struct ieee80211_sub_if_data,
  258. u.ap);
  259. err = drv_sta_add(local, sdata, &sta->sta);
  260. if (err) {
  261. if (!async)
  262. return err;
  263. printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to driver (%d)"
  264. " - keeping it anyway.\n",
  265. sdata->name, sta->sta.addr, err);
  266. } else {
  267. sta->uploaded = true;
  268. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  269. if (async)
  270. wiphy_debug(local->hw.wiphy,
  271. "Finished adding IBSS STA %pM\n",
  272. sta->sta.addr);
  273. #endif
  274. }
  275. sdata = sta->sdata;
  276. if (!async) {
  277. local->num_sta++;
  278. local->sta_generation++;
  279. smp_mb();
  280. /* make the station visible */
  281. spin_lock_irqsave(&local->sta_lock, flags);
  282. sta_info_hash_add(local, sta);
  283. spin_unlock_irqrestore(&local->sta_lock, flags);
  284. }
  285. list_add(&sta->list, &local->sta_list);
  286. ieee80211_sta_debugfs_add(sta);
  287. rate_control_add_sta_debugfs(sta);
  288. sinfo.filled = 0;
  289. sinfo.generation = local->sta_generation;
  290. cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
  291. return 0;
  292. }
  293. static void sta_info_finish_pending(struct ieee80211_local *local)
  294. {
  295. struct sta_info *sta;
  296. unsigned long flags;
  297. spin_lock_irqsave(&local->sta_lock, flags);
  298. while (!list_empty(&local->sta_pending_list)) {
  299. sta = list_first_entry(&local->sta_pending_list,
  300. struct sta_info, list);
  301. list_del(&sta->list);
  302. spin_unlock_irqrestore(&local->sta_lock, flags);
  303. sta_info_finish_insert(sta, true);
  304. spin_lock_irqsave(&local->sta_lock, flags);
  305. }
  306. spin_unlock_irqrestore(&local->sta_lock, flags);
  307. }
  308. static void sta_info_finish_work(struct work_struct *work)
  309. {
  310. struct ieee80211_local *local =
  311. container_of(work, struct ieee80211_local, sta_finish_work);
  312. mutex_lock(&local->sta_mtx);
  313. sta_info_finish_pending(local);
  314. mutex_unlock(&local->sta_mtx);
  315. }
  316. int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
  317. {
  318. struct ieee80211_local *local = sta->local;
  319. struct ieee80211_sub_if_data *sdata = sta->sdata;
  320. unsigned long flags;
  321. int err = 0;
  322. /*
  323. * Can't be a WARN_ON because it can be triggered through a race:
  324. * something inserts a STA (on one CPU) without holding the RTNL
  325. * and another CPU turns off the net device.
  326. */
  327. if (unlikely(!ieee80211_sdata_running(sdata))) {
  328. err = -ENETDOWN;
  329. rcu_read_lock();
  330. goto out_free;
  331. }
  332. if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
  333. is_multicast_ether_addr(sta->sta.addr))) {
  334. err = -EINVAL;
  335. rcu_read_lock();
  336. goto out_free;
  337. }
  338. /*
  339. * In ad-hoc mode, we sometimes need to insert stations
  340. * from tasklet context from the RX path. To avoid races,
  341. * always do so in that case -- see the comment below.
  342. */
  343. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  344. spin_lock_irqsave(&local->sta_lock, flags);
  345. /* check if STA exists already */
  346. if (sta_info_get_bss(sdata, sta->sta.addr)) {
  347. spin_unlock_irqrestore(&local->sta_lock, flags);
  348. rcu_read_lock();
  349. err = -EEXIST;
  350. goto out_free;
  351. }
  352. local->num_sta++;
  353. local->sta_generation++;
  354. smp_mb();
  355. sta_info_hash_add(local, sta);
  356. list_add_tail(&sta->list, &local->sta_pending_list);
  357. rcu_read_lock();
  358. spin_unlock_irqrestore(&local->sta_lock, flags);
  359. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  360. wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n",
  361. sta->sta.addr);
  362. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  363. ieee80211_queue_work(&local->hw, &local->sta_finish_work);
  364. return 0;
  365. }
  366. /*
  367. * On first glance, this will look racy, because the code
  368. * below this point, which inserts a station with sleeping,
  369. * unlocks the sta_lock between checking existence in the
  370. * hash table and inserting into it.
  371. *
  372. * However, it is not racy against itself because it keeps
  373. * the mutex locked. It still seems to race against the
  374. * above code that atomically inserts the station... That,
  375. * however, is not true because the above code can only
  376. * be invoked for IBSS interfaces, and the below code will
  377. * not be -- and the two do not race against each other as
  378. * the hash table also keys off the interface.
  379. */
  380. might_sleep();
  381. mutex_lock(&local->sta_mtx);
  382. spin_lock_irqsave(&local->sta_lock, flags);
  383. /* check if STA exists already */
  384. if (sta_info_get_bss(sdata, sta->sta.addr)) {
  385. spin_unlock_irqrestore(&local->sta_lock, flags);
  386. mutex_unlock(&local->sta_mtx);
  387. rcu_read_lock();
  388. err = -EEXIST;
  389. goto out_free;
  390. }
  391. spin_unlock_irqrestore(&local->sta_lock, flags);
  392. err = sta_info_finish_insert(sta, false);
  393. if (err) {
  394. mutex_unlock(&local->sta_mtx);
  395. rcu_read_lock();
  396. goto out_free;
  397. }
  398. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  399. wiphy_debug(local->hw.wiphy, "Inserted STA %pM\n", sta->sta.addr);
  400. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  401. /* move reference to rcu-protected */
  402. rcu_read_lock();
  403. mutex_unlock(&local->sta_mtx);
  404. if (ieee80211_vif_is_mesh(&sdata->vif))
  405. mesh_accept_plinks_update(sdata);
  406. return 0;
  407. out_free:
  408. BUG_ON(!err);
  409. __sta_info_free(local, sta);
  410. return err;
  411. }
  412. int sta_info_insert(struct sta_info *sta)
  413. {
  414. int err = sta_info_insert_rcu(sta);
  415. rcu_read_unlock();
  416. return err;
  417. }
  418. static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
  419. {
  420. /*
  421. * This format has been mandated by the IEEE specifications,
  422. * so this line may not be changed to use the __set_bit() format.
  423. */
  424. bss->tim[aid / 8] |= (1 << (aid % 8));
  425. }
  426. static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
  427. {
  428. /*
  429. * This format has been mandated by the IEEE specifications,
  430. * so this line may not be changed to use the __clear_bit() format.
  431. */
  432. bss->tim[aid / 8] &= ~(1 << (aid % 8));
  433. }
  434. static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
  435. struct sta_info *sta)
  436. {
  437. BUG_ON(!bss);
  438. __bss_tim_set(bss, sta->sta.aid);
  439. if (sta->local->ops->set_tim) {
  440. sta->local->tim_in_locked_section = true;
  441. drv_set_tim(sta->local, &sta->sta, true);
  442. sta->local->tim_in_locked_section = false;
  443. }
  444. }
  445. void sta_info_set_tim_bit(struct sta_info *sta)
  446. {
  447. unsigned long flags;
  448. BUG_ON(!sta->sdata->bss);
  449. spin_lock_irqsave(&sta->local->sta_lock, flags);
  450. __sta_info_set_tim_bit(sta->sdata->bss, sta);
  451. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  452. }
  453. static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
  454. struct sta_info *sta)
  455. {
  456. BUG_ON(!bss);
  457. __bss_tim_clear(bss, sta->sta.aid);
  458. if (sta->local->ops->set_tim) {
  459. sta->local->tim_in_locked_section = true;
  460. drv_set_tim(sta->local, &sta->sta, false);
  461. sta->local->tim_in_locked_section = false;
  462. }
  463. }
  464. void sta_info_clear_tim_bit(struct sta_info *sta)
  465. {
  466. unsigned long flags;
  467. BUG_ON(!sta->sdata->bss);
  468. spin_lock_irqsave(&sta->local->sta_lock, flags);
  469. __sta_info_clear_tim_bit(sta->sdata->bss, sta);
  470. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  471. }
  472. static int sta_info_buffer_expired(struct sta_info *sta,
  473. struct sk_buff *skb)
  474. {
  475. struct ieee80211_tx_info *info;
  476. int timeout;
  477. if (!skb)
  478. return 0;
  479. info = IEEE80211_SKB_CB(skb);
  480. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  481. timeout = (sta->listen_interval *
  482. sta->sdata->vif.bss_conf.beacon_int *
  483. 32 / 15625) * HZ;
  484. if (timeout < STA_TX_BUFFER_EXPIRE)
  485. timeout = STA_TX_BUFFER_EXPIRE;
  486. return time_after(jiffies, info->control.jiffies + timeout);
  487. }
  488. static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  489. struct sta_info *sta)
  490. {
  491. unsigned long flags;
  492. struct sk_buff *skb;
  493. if (skb_queue_empty(&sta->ps_tx_buf))
  494. return false;
  495. for (;;) {
  496. spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
  497. skb = skb_peek(&sta->ps_tx_buf);
  498. if (sta_info_buffer_expired(sta, skb))
  499. skb = __skb_dequeue(&sta->ps_tx_buf);
  500. else
  501. skb = NULL;
  502. spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
  503. if (!skb)
  504. break;
  505. local->total_ps_buffered--;
  506. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  507. printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
  508. sta->sta.addr);
  509. #endif
  510. dev_kfree_skb(skb);
  511. if (skb_queue_empty(&sta->ps_tx_buf) &&
  512. !test_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF))
  513. sta_info_clear_tim_bit(sta);
  514. }
  515. return true;
  516. }
  517. static int __must_check __sta_info_destroy(struct sta_info *sta)
  518. {
  519. struct ieee80211_local *local;
  520. struct ieee80211_sub_if_data *sdata;
  521. struct sk_buff *skb;
  522. unsigned long flags;
  523. int ret, i;
  524. might_sleep();
  525. if (!sta)
  526. return -ENOENT;
  527. local = sta->local;
  528. sdata = sta->sdata;
  529. /*
  530. * Before removing the station from the driver and
  531. * rate control, it might still start new aggregation
  532. * sessions -- block that to make sure the tear-down
  533. * will be sufficient.
  534. */
  535. set_sta_flags(sta, WLAN_STA_BLOCK_BA);
  536. ieee80211_sta_tear_down_BA_sessions(sta, true);
  537. spin_lock_irqsave(&local->sta_lock, flags);
  538. ret = sta_info_hash_del(local, sta);
  539. /* this might still be the pending list ... which is fine */
  540. if (!ret)
  541. list_del(&sta->list);
  542. spin_unlock_irqrestore(&local->sta_lock, flags);
  543. if (ret)
  544. return ret;
  545. mutex_lock(&local->key_mtx);
  546. for (i = 0; i < NUM_DEFAULT_KEYS; i++)
  547. __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
  548. if (sta->ptk)
  549. __ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
  550. mutex_unlock(&local->key_mtx);
  551. sta->dead = true;
  552. if (test_and_clear_sta_flags(sta,
  553. WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
  554. BUG_ON(!sdata->bss);
  555. atomic_dec(&sdata->bss->num_sta_ps);
  556. __sta_info_clear_tim_bit(sdata->bss, sta);
  557. }
  558. local->num_sta--;
  559. local->sta_generation++;
  560. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  561. rcu_assign_pointer(sdata->u.vlan.sta, NULL);
  562. if (sta->uploaded) {
  563. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  564. sdata = container_of(sdata->bss,
  565. struct ieee80211_sub_if_data,
  566. u.ap);
  567. drv_sta_remove(local, sdata, &sta->sta);
  568. sdata = sta->sdata;
  569. }
  570. /*
  571. * At this point, after we wait for an RCU grace period,
  572. * neither mac80211 nor the driver can reference this
  573. * sta struct any more except by still existing timers
  574. * associated with this station that we clean up below.
  575. */
  576. synchronize_rcu();
  577. #ifdef CONFIG_MAC80211_MESH
  578. if (ieee80211_vif_is_mesh(&sdata->vif))
  579. mesh_accept_plinks_update(sdata);
  580. #endif
  581. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  582. wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
  583. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  584. cancel_work_sync(&sta->drv_unblock_wk);
  585. cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
  586. rate_control_remove_sta_debugfs(sta);
  587. ieee80211_sta_debugfs_remove(sta);
  588. #ifdef CONFIG_MAC80211_MESH
  589. if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
  590. mesh_plink_deactivate(sta);
  591. del_timer_sync(&sta->plink_timer);
  592. }
  593. #endif
  594. while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
  595. local->total_ps_buffered--;
  596. dev_kfree_skb_any(skb);
  597. }
  598. while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
  599. dev_kfree_skb_any(skb);
  600. __sta_info_free(local, sta);
  601. return 0;
  602. }
  603. int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
  604. {
  605. struct sta_info *sta;
  606. int ret;
  607. mutex_lock(&sdata->local->sta_mtx);
  608. sta = sta_info_get(sdata, addr);
  609. ret = __sta_info_destroy(sta);
  610. mutex_unlock(&sdata->local->sta_mtx);
  611. return ret;
  612. }
  613. int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
  614. const u8 *addr)
  615. {
  616. struct sta_info *sta;
  617. int ret;
  618. mutex_lock(&sdata->local->sta_mtx);
  619. sta = sta_info_get_bss(sdata, addr);
  620. ret = __sta_info_destroy(sta);
  621. mutex_unlock(&sdata->local->sta_mtx);
  622. return ret;
  623. }
  624. static void sta_info_cleanup(unsigned long data)
  625. {
  626. struct ieee80211_local *local = (struct ieee80211_local *) data;
  627. struct sta_info *sta;
  628. bool timer_needed = false;
  629. rcu_read_lock();
  630. list_for_each_entry_rcu(sta, &local->sta_list, list)
  631. if (sta_info_cleanup_expire_buffered(local, sta))
  632. timer_needed = true;
  633. rcu_read_unlock();
  634. if (local->quiescing)
  635. return;
  636. if (!timer_needed)
  637. return;
  638. mod_timer(&local->sta_cleanup,
  639. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
  640. }
  641. void sta_info_init(struct ieee80211_local *local)
  642. {
  643. spin_lock_init(&local->sta_lock);
  644. mutex_init(&local->sta_mtx);
  645. INIT_LIST_HEAD(&local->sta_list);
  646. INIT_LIST_HEAD(&local->sta_pending_list);
  647. INIT_WORK(&local->sta_finish_work, sta_info_finish_work);
  648. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  649. (unsigned long)local);
  650. }
  651. void sta_info_stop(struct ieee80211_local *local)
  652. {
  653. del_timer(&local->sta_cleanup);
  654. sta_info_flush(local, NULL);
  655. }
  656. /**
  657. * sta_info_flush - flush matching STA entries from the STA table
  658. *
  659. * Returns the number of removed STA entries.
  660. *
  661. * @local: local interface data
  662. * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
  663. */
  664. int sta_info_flush(struct ieee80211_local *local,
  665. struct ieee80211_sub_if_data *sdata)
  666. {
  667. struct sta_info *sta, *tmp;
  668. int ret = 0;
  669. might_sleep();
  670. mutex_lock(&local->sta_mtx);
  671. sta_info_finish_pending(local);
  672. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  673. if (!sdata || sdata == sta->sdata)
  674. WARN_ON(__sta_info_destroy(sta));
  675. }
  676. mutex_unlock(&local->sta_mtx);
  677. return ret;
  678. }
  679. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  680. unsigned long exp_time)
  681. {
  682. struct ieee80211_local *local = sdata->local;
  683. struct sta_info *sta, *tmp;
  684. mutex_lock(&local->sta_mtx);
  685. list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
  686. if (time_after(jiffies, sta->last_rx + exp_time)) {
  687. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  688. printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
  689. sdata->name, sta->sta.addr);
  690. #endif
  691. WARN_ON(__sta_info_destroy(sta));
  692. }
  693. mutex_unlock(&local->sta_mtx);
  694. }
  695. struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
  696. const u8 *addr,
  697. const u8 *localaddr)
  698. {
  699. struct sta_info *sta, *nxt;
  700. /*
  701. * Just return a random station if localaddr is NULL
  702. * ... first in list.
  703. */
  704. for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
  705. if (localaddr &&
  706. compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
  707. continue;
  708. if (!sta->uploaded)
  709. return NULL;
  710. return &sta->sta;
  711. }
  712. return NULL;
  713. }
  714. EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
  715. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
  716. const u8 *addr)
  717. {
  718. struct sta_info *sta;
  719. if (!vif)
  720. return NULL;
  721. sta = sta_info_get_bss(vif_to_sdata(vif), addr);
  722. if (!sta)
  723. return NULL;
  724. if (!sta->uploaded)
  725. return NULL;
  726. return &sta->sta;
  727. }
  728. EXPORT_SYMBOL(ieee80211_find_sta);
  729. static void clear_sta_ps_flags(void *_sta)
  730. {
  731. struct sta_info *sta = _sta;
  732. clear_sta_flags(sta, WLAN_STA_PS_DRIVER | WLAN_STA_PS_STA);
  733. }
  734. /* powersave support code */
  735. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  736. {
  737. struct ieee80211_sub_if_data *sdata = sta->sdata;
  738. struct ieee80211_local *local = sdata->local;
  739. int sent, buffered;
  740. clear_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF);
  741. if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
  742. drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
  743. if (!skb_queue_empty(&sta->ps_tx_buf))
  744. sta_info_clear_tim_bit(sta);
  745. /* Send all buffered frames to the station */
  746. sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
  747. buffered = ieee80211_add_pending_skbs_fn(local, &sta->ps_tx_buf,
  748. clear_sta_ps_flags, sta);
  749. sent += buffered;
  750. local->total_ps_buffered -= buffered;
  751. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  752. printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
  753. "since STA not sleeping anymore\n", sdata->name,
  754. sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
  755. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  756. }
  757. void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
  758. {
  759. struct ieee80211_sub_if_data *sdata = sta->sdata;
  760. struct ieee80211_local *local = sdata->local;
  761. struct sk_buff *skb;
  762. int no_pending_pkts;
  763. skb = skb_dequeue(&sta->tx_filtered);
  764. if (!skb) {
  765. skb = skb_dequeue(&sta->ps_tx_buf);
  766. if (skb)
  767. local->total_ps_buffered--;
  768. }
  769. no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
  770. skb_queue_empty(&sta->ps_tx_buf);
  771. if (skb) {
  772. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  773. struct ieee80211_hdr *hdr =
  774. (struct ieee80211_hdr *) skb->data;
  775. /*
  776. * Tell TX path to send this frame even though the STA may
  777. * still remain is PS mode after this frame exchange.
  778. */
  779. info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
  780. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  781. printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
  782. sta->sta.addr, sta->sta.aid,
  783. skb_queue_len(&sta->ps_tx_buf));
  784. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  785. /* Use MoreData flag to indicate whether there are more
  786. * buffered frames for this STA */
  787. if (no_pending_pkts)
  788. hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  789. else
  790. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  791. ieee80211_add_pending_skb(local, skb);
  792. if (no_pending_pkts)
  793. sta_info_clear_tim_bit(sta);
  794. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  795. } else {
  796. /*
  797. * FIXME: This can be the result of a race condition between
  798. * us expiring a frame and the station polling for it.
  799. * Should we send it a null-func frame indicating we
  800. * have nothing buffered for it?
  801. */
  802. printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
  803. "though there are no buffered frames for it\n",
  804. sdata->name, sta->sta.addr);
  805. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  806. }
  807. }
  808. void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
  809. struct ieee80211_sta *pubsta, bool block)
  810. {
  811. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  812. trace_api_sta_block_awake(sta->local, pubsta, block);
  813. if (block)
  814. set_sta_flags(sta, WLAN_STA_PS_DRIVER);
  815. else if (test_sta_flags(sta, WLAN_STA_PS_DRIVER))
  816. ieee80211_queue_work(hw, &sta->drv_unblock_wk);
  817. }
  818. EXPORT_SYMBOL(ieee80211_sta_block_awake);
  819. void ieee80211_sta_set_tim(struct ieee80211_sta *pubsta)
  820. {
  821. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  822. set_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF);
  823. sta_info_set_tim_bit(sta);
  824. }
  825. EXPORT_SYMBOL(ieee80211_sta_set_tim);