sta_info.c 22 KB

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