sta_info.c 18 KB

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