sta_info.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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 "ieee80211_rate.h"
  21. #include "sta_info.h"
  22. #include "debugfs_sta.h"
  23. #include "mesh.h"
  24. /**
  25. * DOC: STA information lifetime rules
  26. *
  27. * STA info structures (&struct sta_info) are managed in a hash table
  28. * for faster lookup and a list for iteration. They are managed using
  29. * RCU, i.e. access to the list and hash table is protected by RCU.
  30. *
  31. * Upon allocating a STA info structure with sta_info_alloc(), the caller owns
  32. * that structure. It must then either destroy it using sta_info_destroy()
  33. * (which is pretty useless) or insert it into the hash table using
  34. * sta_info_insert() which demotes the reference from ownership to a regular
  35. * RCU-protected reference; if the function is called without protection by an
  36. * RCU critical section the reference is instantly invalidated. Note that the
  37. * caller may not do much with the STA info before inserting it, in particular,
  38. * it may not start any mesh peer link management or add encryption keys.
  39. *
  40. * When the insertion fails (sta_info_insert()) returns non-zero), the
  41. * structure will have been freed by sta_info_insert()!
  42. *
  43. * Because there are debugfs entries for each station, and adding those
  44. * must be able to sleep, it is also possible to "pin" a station entry,
  45. * that means it can be removed from the hash table but not be freed.
  46. * See the comment in __sta_info_unlink() for more information, this is
  47. * an internal capability only.
  48. *
  49. * In order to remove a STA info structure, the caller needs to first
  50. * unlink it (sta_info_unlink()) from the list and hash tables and
  51. * then destroy it while holding the RTNL; sta_info_destroy() will wait
  52. * for an RCU grace period to elapse before actually freeing it. Due to
  53. * the pinning and the possibility of multiple callers trying to remove
  54. * the same STA info at the same time, sta_info_unlink() can clear the
  55. * STA info pointer it is passed to indicate that the STA info is owned
  56. * by somebody else now.
  57. *
  58. * If sta_info_unlink() did not clear the pointer then the caller owns
  59. * the STA info structure now and is responsible of destroying it with
  60. * a call to sta_info_destroy(), not before RCU synchronisation, of
  61. * course. Note that sta_info_destroy() must be protected by the RTNL.
  62. *
  63. * In all other cases, there is no concept of ownership on a STA entry,
  64. * each structure is owned by the global hash table/list until it is
  65. * removed. All users of the structure need to be RCU protected so that
  66. * the structure won't be freed before they are done using it.
  67. */
  68. /* Caller must hold local->sta_lock */
  69. static int sta_info_hash_del(struct ieee80211_local *local,
  70. struct sta_info *sta)
  71. {
  72. struct sta_info *s;
  73. s = local->sta_hash[STA_HASH(sta->addr)];
  74. if (!s)
  75. return -ENOENT;
  76. if (s == sta) {
  77. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->addr)],
  78. s->hnext);
  79. return 0;
  80. }
  81. while (s->hnext && s->hnext != sta)
  82. s = s->hnext;
  83. if (s->hnext) {
  84. rcu_assign_pointer(s->hnext, sta->hnext);
  85. return 0;
  86. }
  87. return -ENOENT;
  88. }
  89. /* protected by RCU */
  90. static struct sta_info *__sta_info_find(struct ieee80211_local *local,
  91. u8 *addr)
  92. {
  93. struct sta_info *sta;
  94. sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
  95. while (sta) {
  96. if (compare_ether_addr(sta->addr, addr) == 0)
  97. break;
  98. sta = rcu_dereference(sta->hnext);
  99. }
  100. return sta;
  101. }
  102. struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr)
  103. {
  104. return __sta_info_find(local, addr);
  105. }
  106. EXPORT_SYMBOL(sta_info_get);
  107. struct sta_info *sta_info_get_by_idx(struct ieee80211_local *local, int idx,
  108. struct net_device *dev)
  109. {
  110. struct sta_info *sta;
  111. int i = 0;
  112. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  113. if (dev && dev != sta->sdata->dev)
  114. continue;
  115. if (i < idx) {
  116. ++i;
  117. continue;
  118. }
  119. return sta;
  120. }
  121. return NULL;
  122. }
  123. /**
  124. * __sta_info_free - internal STA free helper
  125. *
  126. * @sta: STA info to free
  127. *
  128. * This function must undo everything done by sta_info_alloc()
  129. * that may happen before sta_info_insert().
  130. */
  131. static void __sta_info_free(struct ieee80211_local *local,
  132. struct sta_info *sta)
  133. {
  134. DECLARE_MAC_BUF(mbuf);
  135. rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
  136. rate_control_put(sta->rate_ctrl);
  137. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  138. printk(KERN_DEBUG "%s: Destroyed STA %s\n",
  139. wiphy_name(local->hw.wiphy), print_mac(mbuf, sta->addr));
  140. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  141. kfree(sta);
  142. }
  143. void sta_info_destroy(struct sta_info *sta)
  144. {
  145. struct ieee80211_local *local;
  146. struct sk_buff *skb;
  147. int i;
  148. ASSERT_RTNL();
  149. might_sleep();
  150. if (!sta)
  151. return;
  152. local = sta->local;
  153. rate_control_remove_sta_debugfs(sta);
  154. ieee80211_sta_debugfs_remove(sta);
  155. #ifdef CONFIG_MAC80211_MESH
  156. if (ieee80211_vif_is_mesh(&sta->sdata->vif))
  157. mesh_plink_deactivate(sta);
  158. #endif
  159. if (sta->key) {
  160. /*
  161. * NOTE: This will call synchronize_rcu() internally to
  162. * make sure no key references can be in use. We rely on
  163. * that when we take this branch to make sure nobody can
  164. * reference this STA struct any longer!
  165. */
  166. ieee80211_key_free(sta->key);
  167. WARN_ON(sta->key);
  168. } else {
  169. /*
  170. * Make sure that nobody can reference this STA struct
  171. * any longer.
  172. */
  173. synchronize_rcu();
  174. }
  175. #ifdef CONFIG_MAC80211_MESH
  176. if (ieee80211_vif_is_mesh(&sta->sdata->vif))
  177. del_timer_sync(&sta->plink_timer);
  178. #endif
  179. while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
  180. local->total_ps_buffered--;
  181. dev_kfree_skb_any(skb);
  182. }
  183. while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
  184. dev_kfree_skb_any(skb);
  185. for (i = 0; i < STA_TID_NUM; i++) {
  186. spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
  187. if (sta->ampdu_mlme.tid_rx[i])
  188. del_timer_sync(&sta->ampdu_mlme.tid_rx[i]->session_timer);
  189. spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
  190. spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
  191. if (sta->ampdu_mlme.tid_tx[i])
  192. del_timer_sync(&sta->ampdu_mlme.tid_tx[i]->addba_resp_timer);
  193. spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
  194. }
  195. __sta_info_free(local, sta);
  196. }
  197. /* Caller must hold local->sta_lock */
  198. static void sta_info_hash_add(struct ieee80211_local *local,
  199. struct sta_info *sta)
  200. {
  201. sta->hnext = local->sta_hash[STA_HASH(sta->addr)];
  202. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->addr)], sta);
  203. }
  204. struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
  205. u8 *addr, gfp_t gfp)
  206. {
  207. struct ieee80211_local *local = sdata->local;
  208. struct sta_info *sta;
  209. int i;
  210. DECLARE_MAC_BUF(mbuf);
  211. sta = kzalloc(sizeof(*sta), gfp);
  212. if (!sta)
  213. return NULL;
  214. memcpy(sta->addr, addr, ETH_ALEN);
  215. sta->local = local;
  216. sta->sdata = sdata;
  217. sta->rate_ctrl = rate_control_get(local->rate_ctrl);
  218. sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
  219. gfp);
  220. if (!sta->rate_ctrl_priv) {
  221. rate_control_put(sta->rate_ctrl);
  222. kfree(sta);
  223. return NULL;
  224. }
  225. spin_lock_init(&sta->ampdu_mlme.ampdu_rx);
  226. spin_lock_init(&sta->ampdu_mlme.ampdu_tx);
  227. for (i = 0; i < STA_TID_NUM; i++) {
  228. /* timer_to_tid must be initialized with identity mapping to
  229. * enable session_timer's data differentiation. refer to
  230. * sta_rx_agg_session_timer_expired for useage */
  231. sta->timer_to_tid[i] = i;
  232. /* tid to tx queue: initialize according to HW (0 is valid) */
  233. sta->tid_to_tx_q[i] = local->hw.queues;
  234. /* rx */
  235. sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
  236. sta->ampdu_mlme.tid_rx[i] = NULL;
  237. /* tx */
  238. sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
  239. sta->ampdu_mlme.tid_tx[i] = NULL;
  240. sta->ampdu_mlme.addba_req_num[i] = 0;
  241. }
  242. skb_queue_head_init(&sta->ps_tx_buf);
  243. skb_queue_head_init(&sta->tx_filtered);
  244. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  245. printk(KERN_DEBUG "%s: Allocated STA %s\n",
  246. wiphy_name(local->hw.wiphy), print_mac(mbuf, sta->addr));
  247. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  248. #ifdef CONFIG_MAC80211_MESH
  249. sta->plink_state = PLINK_LISTEN;
  250. spin_lock_init(&sta->plink_lock);
  251. init_timer(&sta->plink_timer);
  252. #endif
  253. return sta;
  254. }
  255. int sta_info_insert(struct sta_info *sta)
  256. {
  257. struct ieee80211_local *local = sta->local;
  258. struct ieee80211_sub_if_data *sdata = sta->sdata;
  259. unsigned long flags;
  260. int err = 0;
  261. DECLARE_MAC_BUF(mac);
  262. /*
  263. * Can't be a WARN_ON because it can be triggered through a race:
  264. * something inserts a STA (on one CPU) without holding the RTNL
  265. * and another CPU turns off the net device.
  266. */
  267. if (unlikely(!netif_running(sdata->dev))) {
  268. err = -ENETDOWN;
  269. goto out_free;
  270. }
  271. if (WARN_ON(compare_ether_addr(sta->addr, sdata->dev->dev_addr) == 0 ||
  272. is_multicast_ether_addr(sta->addr))) {
  273. err = -EINVAL;
  274. goto out_free;
  275. }
  276. spin_lock_irqsave(&local->sta_lock, flags);
  277. /* check if STA exists already */
  278. if (__sta_info_find(local, sta->addr)) {
  279. spin_unlock_irqrestore(&local->sta_lock, flags);
  280. err = -EEXIST;
  281. goto out_free;
  282. }
  283. list_add(&sta->list, &local->sta_list);
  284. local->num_sta++;
  285. sta_info_hash_add(local, sta);
  286. /* notify driver */
  287. if (local->ops->sta_notify) {
  288. if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
  289. sdata = sdata->u.vlan.ap;
  290. local->ops->sta_notify(local_to_hw(local), &sdata->vif,
  291. STA_NOTIFY_ADD, sta->addr);
  292. }
  293. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  294. printk(KERN_DEBUG "%s: Inserted STA %s\n",
  295. wiphy_name(local->hw.wiphy), print_mac(mac, sta->addr));
  296. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  297. spin_unlock_irqrestore(&local->sta_lock, flags);
  298. #ifdef CONFIG_MAC80211_DEBUGFS
  299. /*
  300. * Debugfs entry adding might sleep, so schedule process
  301. * context task for adding entry for STAs that do not yet
  302. * have one.
  303. * NOTE: due to auto-freeing semantics this may only be done
  304. * if the insertion is successful!
  305. */
  306. queue_work(local->hw.workqueue, &local->sta_debugfs_add);
  307. #endif
  308. if (ieee80211_vif_is_mesh(&sdata->vif))
  309. mesh_accept_plinks_update(sdata);
  310. return 0;
  311. out_free:
  312. BUG_ON(!err);
  313. __sta_info_free(local, sta);
  314. return err;
  315. }
  316. static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
  317. {
  318. /*
  319. * This format has been mandated by the IEEE specifications,
  320. * so this line may not be changed to use the __set_bit() format.
  321. */
  322. bss->tim[aid / 8] |= (1 << (aid % 8));
  323. }
  324. static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
  325. {
  326. /*
  327. * This format has been mandated by the IEEE specifications,
  328. * so this line may not be changed to use the __clear_bit() format.
  329. */
  330. bss->tim[aid / 8] &= ~(1 << (aid % 8));
  331. }
  332. static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
  333. struct sta_info *sta)
  334. {
  335. if (bss)
  336. __bss_tim_set(bss, sta->aid);
  337. if (sta->local->ops->set_tim) {
  338. sta->local->tim_in_locked_section = true;
  339. sta->local->ops->set_tim(local_to_hw(sta->local), sta->aid, 1);
  340. sta->local->tim_in_locked_section = false;
  341. }
  342. }
  343. void sta_info_set_tim_bit(struct sta_info *sta)
  344. {
  345. unsigned long flags;
  346. spin_lock_irqsave(&sta->local->sta_lock, flags);
  347. __sta_info_set_tim_bit(sta->sdata->bss, sta);
  348. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  349. }
  350. static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
  351. struct sta_info *sta)
  352. {
  353. if (bss)
  354. __bss_tim_clear(bss, sta->aid);
  355. if (sta->local->ops->set_tim) {
  356. sta->local->tim_in_locked_section = true;
  357. sta->local->ops->set_tim(local_to_hw(sta->local), sta->aid, 0);
  358. sta->local->tim_in_locked_section = false;
  359. }
  360. }
  361. void sta_info_clear_tim_bit(struct sta_info *sta)
  362. {
  363. unsigned long flags;
  364. spin_lock_irqsave(&sta->local->sta_lock, flags);
  365. __sta_info_clear_tim_bit(sta->sdata->bss, sta);
  366. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  367. }
  368. /*
  369. * See comment in __sta_info_unlink,
  370. * caller must hold local->sta_lock.
  371. */
  372. static void __sta_info_pin(struct sta_info *sta)
  373. {
  374. WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
  375. sta->pin_status = STA_INFO_PIN_STAT_PINNED;
  376. }
  377. /*
  378. * See comment in __sta_info_unlink, returns sta if it
  379. * needs to be destroyed.
  380. */
  381. static struct sta_info *__sta_info_unpin(struct sta_info *sta)
  382. {
  383. struct sta_info *ret = NULL;
  384. unsigned long flags;
  385. spin_lock_irqsave(&sta->local->sta_lock, flags);
  386. WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
  387. sta->pin_status != STA_INFO_PIN_STAT_PINNED);
  388. if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
  389. ret = sta;
  390. sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
  391. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  392. return ret;
  393. }
  394. static void __sta_info_unlink(struct sta_info **sta)
  395. {
  396. struct ieee80211_local *local = (*sta)->local;
  397. struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
  398. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  399. DECLARE_MAC_BUF(mbuf);
  400. #endif
  401. /*
  402. * pull caller's reference if we're already gone.
  403. */
  404. if (sta_info_hash_del(local, *sta)) {
  405. *sta = NULL;
  406. return;
  407. }
  408. /*
  409. * Also pull caller's reference if the STA is pinned by the
  410. * task that is adding the debugfs entries. In that case, we
  411. * leave the STA "to be freed".
  412. *
  413. * The rules are not trivial, but not too complex either:
  414. * (1) pin_status is only modified under the sta_lock
  415. * (2) sta_info_debugfs_add_work() will set the status
  416. * to PINNED when it found an item that needs a new
  417. * debugfs directory created. In that case, that item
  418. * must not be freed although all *RCU* users are done
  419. * with it. Hence, we tell the caller of _unlink()
  420. * that the item is already gone (as can happen when
  421. * two tasks try to unlink/destroy at the same time)
  422. * (3) We set the pin_status to DESTROY here when we
  423. * find such an item.
  424. * (4) sta_info_debugfs_add_work() will reset the pin_status
  425. * from PINNED to NORMAL when it is done with the item,
  426. * but will check for DESTROY before resetting it in
  427. * which case it will free the item.
  428. */
  429. if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
  430. (*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
  431. *sta = NULL;
  432. return;
  433. }
  434. list_del(&(*sta)->list);
  435. if ((*sta)->flags & WLAN_STA_PS) {
  436. (*sta)->flags &= ~WLAN_STA_PS;
  437. if (sdata->bss)
  438. atomic_dec(&sdata->bss->num_sta_ps);
  439. __sta_info_clear_tim_bit(sdata->bss, *sta);
  440. }
  441. local->num_sta--;
  442. if (local->ops->sta_notify) {
  443. if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
  444. sdata = sdata->u.vlan.ap;
  445. local->ops->sta_notify(local_to_hw(local), &sdata->vif,
  446. STA_NOTIFY_REMOVE, (*sta)->addr);
  447. }
  448. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  449. mesh_accept_plinks_update(sdata);
  450. #ifdef CONFIG_MAC80211_MESH
  451. del_timer(&(*sta)->plink_timer);
  452. #endif
  453. }
  454. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  455. printk(KERN_DEBUG "%s: Removed STA %s\n",
  456. wiphy_name(local->hw.wiphy), print_mac(mbuf, (*sta)->addr));
  457. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  458. }
  459. void sta_info_unlink(struct sta_info **sta)
  460. {
  461. struct ieee80211_local *local = (*sta)->local;
  462. unsigned long flags;
  463. spin_lock_irqsave(&local->sta_lock, flags);
  464. __sta_info_unlink(sta);
  465. spin_unlock_irqrestore(&local->sta_lock, flags);
  466. }
  467. static inline int sta_info_buffer_expired(struct ieee80211_local *local,
  468. struct sta_info *sta,
  469. struct sk_buff *skb)
  470. {
  471. struct ieee80211_tx_packet_data *pkt_data;
  472. int timeout;
  473. if (!skb)
  474. return 0;
  475. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  476. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  477. timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
  478. 15625) * HZ;
  479. if (timeout < STA_TX_BUFFER_EXPIRE)
  480. timeout = STA_TX_BUFFER_EXPIRE;
  481. return time_after(jiffies, pkt_data->jiffies + timeout);
  482. }
  483. static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  484. struct sta_info *sta)
  485. {
  486. unsigned long flags;
  487. struct sk_buff *skb;
  488. struct ieee80211_sub_if_data *sdata;
  489. DECLARE_MAC_BUF(mac);
  490. if (skb_queue_empty(&sta->ps_tx_buf))
  491. return;
  492. for (;;) {
  493. spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
  494. skb = skb_peek(&sta->ps_tx_buf);
  495. if (sta_info_buffer_expired(local, sta, skb))
  496. skb = __skb_dequeue(&sta->ps_tx_buf);
  497. else
  498. skb = NULL;
  499. spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
  500. if (!skb)
  501. break;
  502. sdata = sta->sdata;
  503. local->total_ps_buffered--;
  504. printk(KERN_DEBUG "Buffered frame expired (STA "
  505. "%s)\n", print_mac(mac, sta->addr));
  506. dev_kfree_skb(skb);
  507. if (skb_queue_empty(&sta->ps_tx_buf))
  508. sta_info_clear_tim_bit(sta);
  509. }
  510. }
  511. static void sta_info_cleanup(unsigned long data)
  512. {
  513. struct ieee80211_local *local = (struct ieee80211_local *) data;
  514. struct sta_info *sta;
  515. rcu_read_lock();
  516. list_for_each_entry_rcu(sta, &local->sta_list, list)
  517. sta_info_cleanup_expire_buffered(local, sta);
  518. rcu_read_unlock();
  519. local->sta_cleanup.expires =
  520. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
  521. add_timer(&local->sta_cleanup);
  522. }
  523. #ifdef CONFIG_MAC80211_DEBUGFS
  524. static void sta_info_debugfs_add_work(struct work_struct *work)
  525. {
  526. struct ieee80211_local *local =
  527. container_of(work, struct ieee80211_local, sta_debugfs_add);
  528. struct sta_info *sta, *tmp;
  529. unsigned long flags;
  530. while (1) {
  531. sta = NULL;
  532. spin_lock_irqsave(&local->sta_lock, flags);
  533. list_for_each_entry(tmp, &local->sta_list, list) {
  534. if (!tmp->debugfs.dir) {
  535. sta = tmp;
  536. __sta_info_pin(sta);
  537. break;
  538. }
  539. }
  540. spin_unlock_irqrestore(&local->sta_lock, flags);
  541. if (!sta)
  542. break;
  543. ieee80211_sta_debugfs_add(sta);
  544. rate_control_add_sta_debugfs(sta);
  545. sta = __sta_info_unpin(sta);
  546. rtnl_lock();
  547. sta_info_destroy(sta);
  548. rtnl_unlock();
  549. }
  550. }
  551. #endif
  552. void sta_info_init(struct ieee80211_local *local)
  553. {
  554. spin_lock_init(&local->sta_lock);
  555. INIT_LIST_HEAD(&local->sta_list);
  556. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  557. (unsigned long)local);
  558. local->sta_cleanup.expires =
  559. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
  560. #ifdef CONFIG_MAC80211_DEBUGFS
  561. INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
  562. #endif
  563. }
  564. int sta_info_start(struct ieee80211_local *local)
  565. {
  566. add_timer(&local->sta_cleanup);
  567. return 0;
  568. }
  569. void sta_info_stop(struct ieee80211_local *local)
  570. {
  571. del_timer(&local->sta_cleanup);
  572. sta_info_flush(local, NULL);
  573. }
  574. /**
  575. * sta_info_flush - flush matching STA entries from the STA table
  576. *
  577. * Returns the number of removed STA entries.
  578. *
  579. * @local: local interface data
  580. * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
  581. */
  582. int sta_info_flush(struct ieee80211_local *local,
  583. struct ieee80211_sub_if_data *sdata)
  584. {
  585. struct sta_info *sta, *tmp;
  586. LIST_HEAD(tmp_list);
  587. int ret = 0;
  588. unsigned long flags;
  589. might_sleep();
  590. spin_lock_irqsave(&local->sta_lock, flags);
  591. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  592. if (!sdata || sdata == sta->sdata) {
  593. __sta_info_unlink(&sta);
  594. if (sta) {
  595. list_add_tail(&sta->list, &tmp_list);
  596. ret++;
  597. }
  598. }
  599. }
  600. spin_unlock_irqrestore(&local->sta_lock, flags);
  601. list_for_each_entry_safe(sta, tmp, &tmp_list, list)
  602. sta_info_destroy(sta);
  603. return ret;
  604. }