sta_info.c 21 KB

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