sta_info.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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->sta_generation++;
  306. local->num_sta++;
  307. sta_info_hash_add(local, sta);
  308. /* notify driver */
  309. if (local->ops->sta_notify) {
  310. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  311. sdata = container_of(sdata->bss,
  312. struct ieee80211_sub_if_data,
  313. u.ap);
  314. drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD, &sta->sta);
  315. }
  316. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  317. printk(KERN_DEBUG "%s: Inserted STA %pM\n",
  318. wiphy_name(local->hw.wiphy), sta->sta.addr);
  319. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  320. spin_unlock_irqrestore(&local->sta_lock, flags);
  321. #ifdef CONFIG_MAC80211_DEBUGFS
  322. /*
  323. * Debugfs entry adding might sleep, so schedule process
  324. * context task for adding entry for STAs that do not yet
  325. * have one.
  326. * NOTE: due to auto-freeing semantics this may only be done
  327. * if the insertion is successful!
  328. */
  329. schedule_work(&local->sta_debugfs_add);
  330. #endif
  331. if (ieee80211_vif_is_mesh(&sdata->vif))
  332. mesh_accept_plinks_update(sdata);
  333. return 0;
  334. out_free:
  335. BUG_ON(!err);
  336. __sta_info_free(local, sta);
  337. return err;
  338. }
  339. static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
  340. {
  341. /*
  342. * This format has been mandated by the IEEE specifications,
  343. * so this line may not be changed to use the __set_bit() format.
  344. */
  345. bss->tim[aid / 8] |= (1 << (aid % 8));
  346. }
  347. static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
  348. {
  349. /*
  350. * This format has been mandated by the IEEE specifications,
  351. * so this line may not be changed to use the __clear_bit() format.
  352. */
  353. bss->tim[aid / 8] &= ~(1 << (aid % 8));
  354. }
  355. static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
  356. struct sta_info *sta)
  357. {
  358. BUG_ON(!bss);
  359. __bss_tim_set(bss, sta->sta.aid);
  360. if (sta->local->ops->set_tim) {
  361. sta->local->tim_in_locked_section = true;
  362. drv_set_tim(sta->local, &sta->sta, true);
  363. sta->local->tim_in_locked_section = false;
  364. }
  365. }
  366. void sta_info_set_tim_bit(struct sta_info *sta)
  367. {
  368. unsigned long flags;
  369. BUG_ON(!sta->sdata->bss);
  370. spin_lock_irqsave(&sta->local->sta_lock, flags);
  371. __sta_info_set_tim_bit(sta->sdata->bss, sta);
  372. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  373. }
  374. static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
  375. struct sta_info *sta)
  376. {
  377. BUG_ON(!bss);
  378. __bss_tim_clear(bss, sta->sta.aid);
  379. if (sta->local->ops->set_tim) {
  380. sta->local->tim_in_locked_section = true;
  381. drv_set_tim(sta->local, &sta->sta, false);
  382. sta->local->tim_in_locked_section = false;
  383. }
  384. }
  385. void sta_info_clear_tim_bit(struct sta_info *sta)
  386. {
  387. unsigned long flags;
  388. BUG_ON(!sta->sdata->bss);
  389. spin_lock_irqsave(&sta->local->sta_lock, flags);
  390. __sta_info_clear_tim_bit(sta->sdata->bss, sta);
  391. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  392. }
  393. static void __sta_info_unlink(struct sta_info **sta)
  394. {
  395. struct ieee80211_local *local = (*sta)->local;
  396. struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
  397. /*
  398. * pull caller's reference if we're already gone.
  399. */
  400. if (sta_info_hash_del(local, *sta)) {
  401. *sta = NULL;
  402. return;
  403. }
  404. if ((*sta)->key) {
  405. ieee80211_key_free((*sta)->key);
  406. WARN_ON((*sta)->key);
  407. }
  408. list_del(&(*sta)->list);
  409. if (test_and_clear_sta_flags(*sta, WLAN_STA_PS)) {
  410. BUG_ON(!sdata->bss);
  411. atomic_dec(&sdata->bss->num_sta_ps);
  412. __sta_info_clear_tim_bit(sdata->bss, *sta);
  413. }
  414. local->num_sta--;
  415. local->sta_generation++;
  416. if (local->ops->sta_notify) {
  417. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  418. sdata = container_of(sdata->bss,
  419. struct ieee80211_sub_if_data,
  420. u.ap);
  421. drv_sta_notify(local, &sdata->vif, STA_NOTIFY_REMOVE,
  422. &(*sta)->sta);
  423. }
  424. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  425. mesh_accept_plinks_update(sdata);
  426. #ifdef CONFIG_MAC80211_MESH
  427. del_timer(&(*sta)->plink_timer);
  428. #endif
  429. }
  430. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  431. printk(KERN_DEBUG "%s: Removed STA %pM\n",
  432. wiphy_name(local->hw.wiphy), (*sta)->sta.addr);
  433. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  434. /*
  435. * Finally, pull caller's reference if the STA is pinned by the
  436. * task that is adding the debugfs entries. In that case, we
  437. * leave the STA "to be freed".
  438. *
  439. * The rules are not trivial, but not too complex either:
  440. * (1) pin_status is only modified under the sta_lock
  441. * (2) STAs may only be pinned under the RTNL so that
  442. * sta_info_flush() is guaranteed to actually destroy
  443. * all STAs that are active for a given interface, this
  444. * is required for correctness because otherwise we
  445. * could notify a driver that an interface is going
  446. * away and only after that (!) notify it about a STA
  447. * on that interface going away.
  448. * (3) sta_info_debugfs_add_work() will set the status
  449. * to PINNED when it found an item that needs a new
  450. * debugfs directory created. In that case, that item
  451. * must not be freed although all *RCU* users are done
  452. * with it. Hence, we tell the caller of _unlink()
  453. * that the item is already gone (as can happen when
  454. * two tasks try to unlink/destroy at the same time)
  455. * (4) We set the pin_status to DESTROY here when we
  456. * find such an item.
  457. * (5) sta_info_debugfs_add_work() will reset the pin_status
  458. * from PINNED to NORMAL when it is done with the item,
  459. * but will check for DESTROY before resetting it in
  460. * which case it will free the item.
  461. */
  462. if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
  463. (*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
  464. *sta = NULL;
  465. return;
  466. }
  467. }
  468. void sta_info_unlink(struct sta_info **sta)
  469. {
  470. struct ieee80211_local *local = (*sta)->local;
  471. unsigned long flags;
  472. spin_lock_irqsave(&local->sta_lock, flags);
  473. __sta_info_unlink(sta);
  474. spin_unlock_irqrestore(&local->sta_lock, flags);
  475. }
  476. static int sta_info_buffer_expired(struct sta_info *sta,
  477. struct sk_buff *skb)
  478. {
  479. struct ieee80211_tx_info *info;
  480. int timeout;
  481. if (!skb)
  482. return 0;
  483. info = IEEE80211_SKB_CB(skb);
  484. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  485. timeout = (sta->listen_interval *
  486. sta->sdata->vif.bss_conf.beacon_int *
  487. 32 / 15625) * HZ;
  488. if (timeout < STA_TX_BUFFER_EXPIRE)
  489. timeout = STA_TX_BUFFER_EXPIRE;
  490. return time_after(jiffies, info->control.jiffies + timeout);
  491. }
  492. static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  493. struct sta_info *sta)
  494. {
  495. unsigned long flags;
  496. struct sk_buff *skb;
  497. struct ieee80211_sub_if_data *sdata;
  498. if (skb_queue_empty(&sta->ps_tx_buf))
  499. return;
  500. for (;;) {
  501. spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
  502. skb = skb_peek(&sta->ps_tx_buf);
  503. if (sta_info_buffer_expired(sta, skb))
  504. skb = __skb_dequeue(&sta->ps_tx_buf);
  505. else
  506. skb = NULL;
  507. spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
  508. if (!skb)
  509. break;
  510. sdata = sta->sdata;
  511. local->total_ps_buffered--;
  512. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  513. printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
  514. sta->sta.addr);
  515. #endif
  516. dev_kfree_skb(skb);
  517. if (skb_queue_empty(&sta->ps_tx_buf))
  518. sta_info_clear_tim_bit(sta);
  519. }
  520. }
  521. static void sta_info_cleanup(unsigned long data)
  522. {
  523. struct ieee80211_local *local = (struct ieee80211_local *) data;
  524. struct sta_info *sta;
  525. rcu_read_lock();
  526. list_for_each_entry_rcu(sta, &local->sta_list, list)
  527. sta_info_cleanup_expire_buffered(local, sta);
  528. rcu_read_unlock();
  529. if (local->quiescing)
  530. return;
  531. local->sta_cleanup.expires =
  532. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
  533. add_timer(&local->sta_cleanup);
  534. }
  535. #ifdef CONFIG_MAC80211_DEBUGFS
  536. /*
  537. * See comment in __sta_info_unlink,
  538. * caller must hold local->sta_lock.
  539. */
  540. static void __sta_info_pin(struct sta_info *sta)
  541. {
  542. WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
  543. sta->pin_status = STA_INFO_PIN_STAT_PINNED;
  544. }
  545. /*
  546. * See comment in __sta_info_unlink, returns sta if it
  547. * needs to be destroyed.
  548. */
  549. static struct sta_info *__sta_info_unpin(struct sta_info *sta)
  550. {
  551. struct sta_info *ret = NULL;
  552. unsigned long flags;
  553. spin_lock_irqsave(&sta->local->sta_lock, flags);
  554. WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
  555. sta->pin_status != STA_INFO_PIN_STAT_PINNED);
  556. if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
  557. ret = sta;
  558. sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
  559. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  560. return ret;
  561. }
  562. static void sta_info_debugfs_add_work(struct work_struct *work)
  563. {
  564. struct ieee80211_local *local =
  565. container_of(work, struct ieee80211_local, sta_debugfs_add);
  566. struct sta_info *sta, *tmp;
  567. unsigned long flags;
  568. /* We need to keep the RTNL across the whole pinned status. */
  569. rtnl_lock();
  570. while (1) {
  571. sta = NULL;
  572. spin_lock_irqsave(&local->sta_lock, flags);
  573. list_for_each_entry(tmp, &local->sta_list, list) {
  574. /*
  575. * debugfs.add_has_run will be set by
  576. * ieee80211_sta_debugfs_add regardless
  577. * of what else it does.
  578. */
  579. if (!tmp->debugfs.add_has_run) {
  580. sta = tmp;
  581. __sta_info_pin(sta);
  582. break;
  583. }
  584. }
  585. spin_unlock_irqrestore(&local->sta_lock, flags);
  586. if (!sta)
  587. break;
  588. ieee80211_sta_debugfs_add(sta);
  589. rate_control_add_sta_debugfs(sta);
  590. sta = __sta_info_unpin(sta);
  591. sta_info_destroy(sta);
  592. }
  593. rtnl_unlock();
  594. }
  595. #endif
  596. void sta_info_init(struct ieee80211_local *local)
  597. {
  598. spin_lock_init(&local->sta_lock);
  599. INIT_LIST_HEAD(&local->sta_list);
  600. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  601. (unsigned long)local);
  602. local->sta_cleanup.expires =
  603. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
  604. #ifdef CONFIG_MAC80211_DEBUGFS
  605. INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
  606. #endif
  607. }
  608. int sta_info_start(struct ieee80211_local *local)
  609. {
  610. add_timer(&local->sta_cleanup);
  611. return 0;
  612. }
  613. void sta_info_stop(struct ieee80211_local *local)
  614. {
  615. del_timer(&local->sta_cleanup);
  616. #ifdef CONFIG_MAC80211_DEBUGFS
  617. /*
  618. * Make sure the debugfs adding work isn't pending after this
  619. * because we're about to be destroyed. It doesn't matter
  620. * whether it ran or not since we're going to flush all STAs
  621. * anyway.
  622. */
  623. cancel_work_sync(&local->sta_debugfs_add);
  624. #endif
  625. sta_info_flush(local, NULL);
  626. }
  627. /**
  628. * sta_info_flush - flush matching STA entries from the STA table
  629. *
  630. * Returns the number of removed STA entries.
  631. *
  632. * @local: local interface data
  633. * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
  634. */
  635. int sta_info_flush(struct ieee80211_local *local,
  636. struct ieee80211_sub_if_data *sdata)
  637. {
  638. struct sta_info *sta, *tmp;
  639. LIST_HEAD(tmp_list);
  640. int ret = 0;
  641. unsigned long flags;
  642. might_sleep();
  643. spin_lock_irqsave(&local->sta_lock, flags);
  644. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  645. if (!sdata || sdata == sta->sdata) {
  646. __sta_info_unlink(&sta);
  647. if (sta) {
  648. list_add_tail(&sta->list, &tmp_list);
  649. ret++;
  650. }
  651. }
  652. }
  653. spin_unlock_irqrestore(&local->sta_lock, flags);
  654. list_for_each_entry_safe(sta, tmp, &tmp_list, list)
  655. sta_info_destroy(sta);
  656. return ret;
  657. }
  658. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  659. unsigned long exp_time)
  660. {
  661. struct ieee80211_local *local = sdata->local;
  662. struct sta_info *sta, *tmp;
  663. LIST_HEAD(tmp_list);
  664. unsigned long flags;
  665. spin_lock_irqsave(&local->sta_lock, flags);
  666. list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
  667. if (time_after(jiffies, sta->last_rx + exp_time)) {
  668. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  669. printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
  670. sdata->dev->name, sta->sta.addr);
  671. #endif
  672. __sta_info_unlink(&sta);
  673. if (sta)
  674. list_add(&sta->list, &tmp_list);
  675. }
  676. spin_unlock_irqrestore(&local->sta_lock, flags);
  677. list_for_each_entry_safe(sta, tmp, &tmp_list, list)
  678. sta_info_destroy(sta);
  679. }
  680. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_hw *hw,
  681. const u8 *addr)
  682. {
  683. struct sta_info *sta = sta_info_get(hw_to_local(hw), addr);
  684. if (!sta)
  685. return NULL;
  686. return &sta->sta;
  687. }
  688. EXPORT_SYMBOL(ieee80211_find_sta);