sta_info.c 23 KB

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