sta_info.c 22 KB

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