sta_info.c 21 KB

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