sta_info.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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 "driver-ops.h"
  21. #include "rate.h"
  22. #include "sta_info.h"
  23. #include "debugfs_sta.h"
  24. #include "mesh.h"
  25. /**
  26. * DOC: STA information lifetime rules
  27. *
  28. * STA info structures (&struct sta_info) are managed in a hash table
  29. * for faster lookup and a list for iteration. They are managed using
  30. * RCU, i.e. access to the list and hash table is protected by RCU.
  31. *
  32. * Upon allocating a STA info structure with sta_info_alloc(), the caller owns
  33. * that structure. It must then either destroy it using sta_info_destroy()
  34. * (which is pretty useless) or insert it into the hash table using
  35. * sta_info_insert() which demotes the reference from ownership to a regular
  36. * RCU-protected reference; if the function is called without protection by an
  37. * RCU critical section the reference is instantly invalidated. Note that the
  38. * caller may not do much with the STA info before inserting it, in particular,
  39. * it may not start any mesh peer link management or add encryption keys.
  40. *
  41. * When the insertion fails (sta_info_insert()) returns non-zero), the
  42. * structure will have been freed by sta_info_insert()!
  43. *
  44. * Because there are debugfs entries for each station, and adding those
  45. * must be able to sleep, it is also possible to "pin" a station entry,
  46. * that means it can be removed from the hash table but not be freed.
  47. * See the comment in __sta_info_unlink() for more information, this is
  48. * an internal capability only.
  49. *
  50. * In order to remove a STA info structure, the caller needs to first
  51. * unlink it (sta_info_unlink()) from the list and hash tables and
  52. * then destroy it; sta_info_destroy() will wait for an RCU grace period
  53. * to elapse before actually freeing it. Due to the pinning and the
  54. * possibility of multiple callers trying to remove the same STA info at
  55. * the same time, sta_info_unlink() can clear the STA info pointer it is
  56. * passed to indicate that the STA info is owned by somebody else now.
  57. *
  58. * If sta_info_unlink() did not clear the pointer then the caller owns
  59. * the STA info structure now and is responsible of destroying it with
  60. * a call to sta_info_destroy().
  61. *
  62. * In all other cases, there is no concept of ownership on a STA entry,
  63. * each structure is owned by the global hash table/list until it is
  64. * removed. All users of the structure need to be RCU protected so that
  65. * the structure won't be freed before they are done using it.
  66. */
  67. /* Caller must hold local->sta_lock */
  68. static int sta_info_hash_del(struct ieee80211_local *local,
  69. struct sta_info *sta)
  70. {
  71. struct sta_info *s;
  72. s = local->sta_hash[STA_HASH(sta->sta.addr)];
  73. if (!s)
  74. return -ENOENT;
  75. if (s == sta) {
  76. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
  77. s->hnext);
  78. return 0;
  79. }
  80. while (s->hnext && s->hnext != sta)
  81. s = s->hnext;
  82. if (s->hnext) {
  83. rcu_assign_pointer(s->hnext, sta->hnext);
  84. return 0;
  85. }
  86. return -ENOENT;
  87. }
  88. /* protected by RCU */
  89. struct sta_info *sta_info_get(struct ieee80211_local *local, const u8 *addr)
  90. {
  91. struct sta_info *sta;
  92. sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
  93. while (sta) {
  94. if (memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
  95. break;
  96. sta = rcu_dereference(sta->hnext);
  97. }
  98. return sta;
  99. }
  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. /**
  117. * __sta_info_free - internal STA free helper
  118. *
  119. * @local: pointer to the global information
  120. * @sta: STA info to free
  121. *
  122. * This function must undo everything done by sta_info_alloc()
  123. * that may happen before sta_info_insert().
  124. */
  125. static void __sta_info_free(struct ieee80211_local *local,
  126. struct sta_info *sta)
  127. {
  128. rate_control_free_sta(sta);
  129. rate_control_put(sta->rate_ctrl);
  130. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  131. printk(KERN_DEBUG "%s: Destroyed STA %pM\n",
  132. wiphy_name(local->hw.wiphy), sta->sta.addr);
  133. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  134. kfree(sta);
  135. }
  136. void sta_info_destroy(struct sta_info *sta)
  137. {
  138. struct ieee80211_local *local;
  139. struct sk_buff *skb;
  140. int i;
  141. might_sleep();
  142. if (!sta)
  143. return;
  144. local = sta->local;
  145. rate_control_remove_sta_debugfs(sta);
  146. ieee80211_sta_debugfs_remove(sta);
  147. #ifdef CONFIG_MAC80211_MESH
  148. if (ieee80211_vif_is_mesh(&sta->sdata->vif))
  149. mesh_plink_deactivate(sta);
  150. #endif
  151. /*
  152. * We have only unlinked the key, and actually destroying it
  153. * may mean it is removed from hardware which requires that
  154. * the key->sta pointer is still valid, so flush the key todo
  155. * list here.
  156. *
  157. * ieee80211_key_todo() will synchronize_rcu() so after this
  158. * nothing can reference this sta struct any more.
  159. */
  160. ieee80211_key_todo();
  161. #ifdef CONFIG_MAC80211_MESH
  162. if (ieee80211_vif_is_mesh(&sta->sdata->vif))
  163. del_timer_sync(&sta->plink_timer);
  164. #endif
  165. while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
  166. local->total_ps_buffered--;
  167. dev_kfree_skb_any(skb);
  168. }
  169. while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
  170. dev_kfree_skb_any(skb);
  171. for (i = 0; i < STA_TID_NUM; i++) {
  172. struct tid_ampdu_rx *tid_rx;
  173. struct tid_ampdu_tx *tid_tx;
  174. spin_lock_bh(&sta->lock);
  175. tid_rx = sta->ampdu_mlme.tid_rx[i];
  176. /* Make sure timer won't free the tid_rx struct, see below */
  177. if (tid_rx)
  178. tid_rx->shutdown = true;
  179. spin_unlock_bh(&sta->lock);
  180. /*
  181. * Outside spinlock - shutdown is true now so that the timer
  182. * won't free tid_rx, we have to do that now. Can't let the
  183. * timer do it because we have to sync the timer outside the
  184. * lock that it takes itself.
  185. */
  186. if (tid_rx) {
  187. del_timer_sync(&tid_rx->session_timer);
  188. kfree(tid_rx);
  189. }
  190. /*
  191. * No need to do such complications for TX agg sessions, the
  192. * path leading to freeing the tid_tx struct goes via a call
  193. * from the driver, and thus needs to look up the sta struct
  194. * again, which cannot be found when we get here. Hence, we
  195. * just need to delete the timer and free the aggregation
  196. * info; we won't be telling the peer about it then but that
  197. * doesn't matter if we're not talking to it again anyway.
  198. */
  199. tid_tx = sta->ampdu_mlme.tid_tx[i];
  200. if (tid_tx) {
  201. del_timer_sync(&tid_tx->addba_resp_timer);
  202. /*
  203. * STA removed while aggregation session being
  204. * started? Bit odd, but purge frames anyway.
  205. */
  206. skb_queue_purge(&tid_tx->pending);
  207. kfree(tid_tx);
  208. }
  209. }
  210. __sta_info_free(local, sta);
  211. }
  212. /* Caller must hold local->sta_lock */
  213. static void sta_info_hash_add(struct ieee80211_local *local,
  214. struct sta_info *sta)
  215. {
  216. sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
  217. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
  218. }
  219. struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
  220. u8 *addr, gfp_t gfp)
  221. {
  222. struct ieee80211_local *local = sdata->local;
  223. struct sta_info *sta;
  224. int i;
  225. sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
  226. if (!sta)
  227. return NULL;
  228. spin_lock_init(&sta->lock);
  229. spin_lock_init(&sta->flaglock);
  230. memcpy(sta->sta.addr, addr, ETH_ALEN);
  231. sta->local = local;
  232. sta->sdata = sdata;
  233. sta->rate_ctrl = rate_control_get(local->rate_ctrl);
  234. sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
  235. &sta->sta, gfp);
  236. if (!sta->rate_ctrl_priv) {
  237. rate_control_put(sta->rate_ctrl);
  238. kfree(sta);
  239. return NULL;
  240. }
  241. for (i = 0; i < STA_TID_NUM; i++) {
  242. /* timer_to_tid must be initialized with identity mapping to
  243. * enable session_timer's data differentiation. refer to
  244. * sta_rx_agg_session_timer_expired for useage */
  245. sta->timer_to_tid[i] = i;
  246. /* rx */
  247. sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
  248. sta->ampdu_mlme.tid_rx[i] = NULL;
  249. /* tx */
  250. sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
  251. sta->ampdu_mlme.tid_tx[i] = NULL;
  252. sta->ampdu_mlme.addba_req_num[i] = 0;
  253. }
  254. skb_queue_head_init(&sta->ps_tx_buf);
  255. skb_queue_head_init(&sta->tx_filtered);
  256. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  257. printk(KERN_DEBUG "%s: Allocated STA %pM\n",
  258. wiphy_name(local->hw.wiphy), sta->sta.addr);
  259. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  260. #ifdef CONFIG_MAC80211_MESH
  261. sta->plink_state = PLINK_LISTEN;
  262. init_timer(&sta->plink_timer);
  263. #endif
  264. return sta;
  265. }
  266. int sta_info_insert(struct sta_info *sta)
  267. {
  268. struct ieee80211_local *local = sta->local;
  269. struct ieee80211_sub_if_data *sdata = sta->sdata;
  270. unsigned long flags;
  271. int err = 0;
  272. /*
  273. * Can't be a WARN_ON because it can be triggered through a race:
  274. * something inserts a STA (on one CPU) without holding the RTNL
  275. * and another CPU turns off the net device.
  276. */
  277. if (unlikely(!netif_running(sdata->dev))) {
  278. err = -ENETDOWN;
  279. goto out_free;
  280. }
  281. if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->dev->dev_addr) == 0 ||
  282. is_multicast_ether_addr(sta->sta.addr))) {
  283. err = -EINVAL;
  284. goto out_free;
  285. }
  286. spin_lock_irqsave(&local->sta_lock, flags);
  287. /* check if STA exists already */
  288. if (sta_info_get(local, sta->sta.addr)) {
  289. spin_unlock_irqrestore(&local->sta_lock, flags);
  290. err = -EEXIST;
  291. goto out_free;
  292. }
  293. list_add(&sta->list, &local->sta_list);
  294. local->num_sta++;
  295. sta_info_hash_add(local, sta);
  296. /* notify driver */
  297. if (local->ops->sta_notify) {
  298. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  299. sdata = container_of(sdata->bss,
  300. struct ieee80211_sub_if_data,
  301. u.ap);
  302. drv_sta_notify(local, &sdata->vif, 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. drv_set_tim(sta->local, &sta->sta, true);
  351. sta->local->tim_in_locked_section = false;
  352. }
  353. }
  354. void sta_info_set_tim_bit(struct sta_info *sta)
  355. {
  356. unsigned long flags;
  357. BUG_ON(!sta->sdata->bss);
  358. spin_lock_irqsave(&sta->local->sta_lock, flags);
  359. __sta_info_set_tim_bit(sta->sdata->bss, sta);
  360. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  361. }
  362. static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
  363. struct sta_info *sta)
  364. {
  365. BUG_ON(!bss);
  366. __bss_tim_clear(bss, sta->sta.aid);
  367. if (sta->local->ops->set_tim) {
  368. sta->local->tim_in_locked_section = true;
  369. drv_set_tim(sta->local, &sta->sta, false);
  370. sta->local->tim_in_locked_section = false;
  371. }
  372. }
  373. void sta_info_clear_tim_bit(struct sta_info *sta)
  374. {
  375. unsigned long flags;
  376. BUG_ON(!sta->sdata->bss);
  377. spin_lock_irqsave(&sta->local->sta_lock, flags);
  378. __sta_info_clear_tim_bit(sta->sdata->bss, sta);
  379. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  380. }
  381. static void __sta_info_unlink(struct sta_info **sta)
  382. {
  383. struct ieee80211_local *local = (*sta)->local;
  384. struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
  385. /*
  386. * pull caller's reference if we're already gone.
  387. */
  388. if (sta_info_hash_del(local, *sta)) {
  389. *sta = NULL;
  390. return;
  391. }
  392. if ((*sta)->key) {
  393. ieee80211_key_free((*sta)->key);
  394. WARN_ON((*sta)->key);
  395. }
  396. list_del(&(*sta)->list);
  397. if (test_and_clear_sta_flags(*sta, WLAN_STA_PS)) {
  398. BUG_ON(!sdata->bss);
  399. atomic_dec(&sdata->bss->num_sta_ps);
  400. __sta_info_clear_tim_bit(sdata->bss, *sta);
  401. }
  402. local->num_sta--;
  403. if (local->ops->sta_notify) {
  404. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  405. sdata = container_of(sdata->bss,
  406. struct ieee80211_sub_if_data,
  407. u.ap);
  408. drv_sta_notify(local, &sdata->vif, STA_NOTIFY_REMOVE,
  409. &(*sta)->sta);
  410. }
  411. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  412. mesh_accept_plinks_update(sdata);
  413. #ifdef CONFIG_MAC80211_MESH
  414. del_timer(&(*sta)->plink_timer);
  415. #endif
  416. }
  417. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  418. printk(KERN_DEBUG "%s: Removed STA %pM\n",
  419. wiphy_name(local->hw.wiphy), (*sta)->sta.addr);
  420. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  421. /*
  422. * Finally, pull caller's reference if the STA is pinned by the
  423. * task that is adding the debugfs entries. In that case, we
  424. * leave the STA "to be freed".
  425. *
  426. * The rules are not trivial, but not too complex either:
  427. * (1) pin_status is only modified under the sta_lock
  428. * (2) STAs may only be pinned under the RTNL so that
  429. * sta_info_flush() is guaranteed to actually destroy
  430. * all STAs that are active for a given interface, this
  431. * is required for correctness because otherwise we
  432. * could notify a driver that an interface is going
  433. * away and only after that (!) notify it about a STA
  434. * on that interface going away.
  435. * (3) sta_info_debugfs_add_work() will set the status
  436. * to PINNED when it found an item that needs a new
  437. * debugfs directory created. In that case, that item
  438. * must not be freed although all *RCU* users are done
  439. * with it. Hence, we tell the caller of _unlink()
  440. * that the item is already gone (as can happen when
  441. * two tasks try to unlink/destroy at the same time)
  442. * (4) We set the pin_status to DESTROY here when we
  443. * find such an item.
  444. * (5) sta_info_debugfs_add_work() will reset the pin_status
  445. * from PINNED to NORMAL when it is done with the item,
  446. * but will check for DESTROY before resetting it in
  447. * which case it will free the item.
  448. */
  449. if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
  450. (*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
  451. *sta = NULL;
  452. return;
  453. }
  454. }
  455. void sta_info_unlink(struct sta_info **sta)
  456. {
  457. struct ieee80211_local *local = (*sta)->local;
  458. unsigned long flags;
  459. spin_lock_irqsave(&local->sta_lock, flags);
  460. __sta_info_unlink(sta);
  461. spin_unlock_irqrestore(&local->sta_lock, flags);
  462. }
  463. static int sta_info_buffer_expired(struct sta_info *sta,
  464. struct sk_buff *skb)
  465. {
  466. struct ieee80211_tx_info *info;
  467. int timeout;
  468. if (!skb)
  469. return 0;
  470. info = IEEE80211_SKB_CB(skb);
  471. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  472. timeout = (sta->listen_interval *
  473. sta->sdata->vif.bss_conf.beacon_int *
  474. 32 / 15625) * HZ;
  475. if (timeout < STA_TX_BUFFER_EXPIRE)
  476. timeout = STA_TX_BUFFER_EXPIRE;
  477. return time_after(jiffies, info->control.jiffies + timeout);
  478. }
  479. static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  480. struct sta_info *sta)
  481. {
  482. unsigned long flags;
  483. struct sk_buff *skb;
  484. struct ieee80211_sub_if_data *sdata;
  485. if (skb_queue_empty(&sta->ps_tx_buf))
  486. return;
  487. for (;;) {
  488. spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
  489. skb = skb_peek(&sta->ps_tx_buf);
  490. if (sta_info_buffer_expired(sta, skb))
  491. skb = __skb_dequeue(&sta->ps_tx_buf);
  492. else
  493. skb = NULL;
  494. spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
  495. if (!skb)
  496. break;
  497. sdata = sta->sdata;
  498. local->total_ps_buffered--;
  499. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  500. printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
  501. sta->sta.addr);
  502. #endif
  503. dev_kfree_skb(skb);
  504. if (skb_queue_empty(&sta->ps_tx_buf))
  505. sta_info_clear_tim_bit(sta);
  506. }
  507. }
  508. static void sta_info_cleanup(unsigned long data)
  509. {
  510. struct ieee80211_local *local = (struct ieee80211_local *) data;
  511. struct sta_info *sta;
  512. rcu_read_lock();
  513. list_for_each_entry_rcu(sta, &local->sta_list, list)
  514. sta_info_cleanup_expire_buffered(local, sta);
  515. rcu_read_unlock();
  516. local->sta_cleanup.expires =
  517. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
  518. add_timer(&local->sta_cleanup);
  519. }
  520. #ifdef CONFIG_MAC80211_DEBUGFS
  521. /*
  522. * See comment in __sta_info_unlink,
  523. * caller must hold local->sta_lock.
  524. */
  525. static void __sta_info_pin(struct sta_info *sta)
  526. {
  527. WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
  528. sta->pin_status = STA_INFO_PIN_STAT_PINNED;
  529. }
  530. /*
  531. * See comment in __sta_info_unlink, returns sta if it
  532. * needs to be destroyed.
  533. */
  534. static struct sta_info *__sta_info_unpin(struct sta_info *sta)
  535. {
  536. struct sta_info *ret = NULL;
  537. unsigned long flags;
  538. spin_lock_irqsave(&sta->local->sta_lock, flags);
  539. WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
  540. sta->pin_status != STA_INFO_PIN_STAT_PINNED);
  541. if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
  542. ret = sta;
  543. sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
  544. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  545. return ret;
  546. }
  547. static void sta_info_debugfs_add_work(struct work_struct *work)
  548. {
  549. struct ieee80211_local *local =
  550. container_of(work, struct ieee80211_local, sta_debugfs_add);
  551. struct sta_info *sta, *tmp;
  552. unsigned long flags;
  553. /* We need to keep the RTNL across the whole pinned status. */
  554. rtnl_lock();
  555. while (1) {
  556. sta = NULL;
  557. spin_lock_irqsave(&local->sta_lock, flags);
  558. list_for_each_entry(tmp, &local->sta_list, list) {
  559. /*
  560. * debugfs.add_has_run will be set by
  561. * ieee80211_sta_debugfs_add regardless
  562. * of what else it does.
  563. */
  564. if (!tmp->debugfs.add_has_run) {
  565. sta = tmp;
  566. __sta_info_pin(sta);
  567. break;
  568. }
  569. }
  570. spin_unlock_irqrestore(&local->sta_lock, flags);
  571. if (!sta)
  572. break;
  573. ieee80211_sta_debugfs_add(sta);
  574. rate_control_add_sta_debugfs(sta);
  575. sta = __sta_info_unpin(sta);
  576. sta_info_destroy(sta);
  577. }
  578. rtnl_unlock();
  579. }
  580. #endif
  581. void sta_info_init(struct ieee80211_local *local)
  582. {
  583. spin_lock_init(&local->sta_lock);
  584. INIT_LIST_HEAD(&local->sta_list);
  585. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  586. (unsigned long)local);
  587. local->sta_cleanup.expires =
  588. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
  589. #ifdef CONFIG_MAC80211_DEBUGFS
  590. INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
  591. #endif
  592. }
  593. int sta_info_start(struct ieee80211_local *local)
  594. {
  595. add_timer(&local->sta_cleanup);
  596. return 0;
  597. }
  598. void sta_info_stop(struct ieee80211_local *local)
  599. {
  600. del_timer(&local->sta_cleanup);
  601. #ifdef CONFIG_MAC80211_DEBUGFS
  602. /*
  603. * Make sure the debugfs adding work isn't pending after this
  604. * because we're about to be destroyed. It doesn't matter
  605. * whether it ran or not since we're going to flush all STAs
  606. * anyway.
  607. */
  608. cancel_work_sync(&local->sta_debugfs_add);
  609. #endif
  610. sta_info_flush(local, NULL);
  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. spin_lock_irqsave(&local->sta_lock, flags);
  629. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  630. if (!sdata || sdata == sta->sdata) {
  631. __sta_info_unlink(&sta);
  632. if (sta) {
  633. list_add_tail(&sta->list, &tmp_list);
  634. ret++;
  635. }
  636. }
  637. }
  638. spin_unlock_irqrestore(&local->sta_lock, flags);
  639. list_for_each_entry_safe(sta, tmp, &tmp_list, list)
  640. sta_info_destroy(sta);
  641. return ret;
  642. }
  643. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  644. unsigned long exp_time)
  645. {
  646. struct ieee80211_local *local = sdata->local;
  647. struct sta_info *sta, *tmp;
  648. LIST_HEAD(tmp_list);
  649. unsigned long flags;
  650. spin_lock_irqsave(&local->sta_lock, flags);
  651. list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
  652. if (time_after(jiffies, sta->last_rx + exp_time)) {
  653. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  654. printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
  655. sdata->dev->name, sta->sta.addr);
  656. #endif
  657. __sta_info_unlink(&sta);
  658. if (sta)
  659. list_add(&sta->list, &tmp_list);
  660. }
  661. spin_unlock_irqrestore(&local->sta_lock, flags);
  662. list_for_each_entry_safe(sta, tmp, &tmp_list, list)
  663. sta_info_destroy(sta);
  664. }
  665. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_hw *hw,
  666. const u8 *addr)
  667. {
  668. struct sta_info *sta = sta_info_get(hw_to_local(hw), addr);
  669. if (!sta)
  670. return NULL;
  671. return &sta->sta;
  672. }
  673. EXPORT_SYMBOL(ieee80211_find_sta);