sta_info.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/types.h>
  13. #include <linux/slab.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/if_arp.h>
  16. #include <linux/timer.h>
  17. #include <linux/rtnetlink.h>
  18. #include <net/mac80211.h>
  19. #include "ieee80211_i.h"
  20. #include "rate.h"
  21. #include "sta_info.h"
  22. #include "debugfs_sta.h"
  23. #include "mesh.h"
  24. /**
  25. * DOC: STA information lifetime rules
  26. *
  27. * STA info structures (&struct sta_info) are managed in a hash table
  28. * for faster lookup and a list for iteration. They are managed using
  29. * RCU, i.e. access to the list and hash table is protected by RCU.
  30. *
  31. * Upon allocating a STA info structure with sta_info_alloc(), the caller owns
  32. * that structure. It must then either destroy it using sta_info_destroy()
  33. * (which is pretty useless) or insert it into the hash table using
  34. * sta_info_insert() which demotes the reference from ownership to a regular
  35. * RCU-protected reference; if the function is called without protection by an
  36. * RCU critical section the reference is instantly invalidated. Note that the
  37. * caller may not do much with the STA info before inserting it, in particular,
  38. * it may not start any mesh peer link management or add encryption keys.
  39. *
  40. * When the insertion fails (sta_info_insert()) returns non-zero), the
  41. * structure will have been freed by sta_info_insert()!
  42. *
  43. * Because there are debugfs entries for each station, and adding those
  44. * must be able to sleep, it is also possible to "pin" a station entry,
  45. * that means it can be removed from the hash table but not be freed.
  46. * See the comment in __sta_info_unlink() for more information, this is
  47. * an internal capability only.
  48. *
  49. * In order to remove a STA info structure, the caller needs to first
  50. * unlink it (sta_info_unlink()) from the list and hash tables and
  51. * then destroy it; sta_info_destroy() will wait for an RCU grace period
  52. * to elapse before actually freeing it. Due to the pinning and the
  53. * possibility of multiple callers trying to remove the same STA info at
  54. * the same time, sta_info_unlink() can clear the STA info pointer it is
  55. * passed to indicate that the STA info is owned by somebody else now.
  56. *
  57. * If sta_info_unlink() did not clear the pointer then the caller owns
  58. * the STA info structure now and is responsible of destroying it with
  59. * a call to sta_info_destroy().
  60. *
  61. * In all other cases, there is no concept of ownership on a STA entry,
  62. * each structure is owned by the global hash table/list until it is
  63. * removed. All users of the structure need to be RCU protected so that
  64. * the structure won't be freed before they are done using it.
  65. */
  66. /* Caller must hold local->sta_lock */
  67. static int sta_info_hash_del(struct ieee80211_local *local,
  68. struct sta_info *sta)
  69. {
  70. struct sta_info *s;
  71. s = local->sta_hash[STA_HASH(sta->sta.addr)];
  72. if (!s)
  73. return -ENOENT;
  74. if (s == sta) {
  75. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
  76. s->hnext);
  77. return 0;
  78. }
  79. while (s->hnext && s->hnext != sta)
  80. s = s->hnext;
  81. if (s->hnext) {
  82. rcu_assign_pointer(s->hnext, sta->hnext);
  83. return 0;
  84. }
  85. return -ENOENT;
  86. }
  87. /* protected by RCU */
  88. struct sta_info *sta_info_get(struct ieee80211_local *local, const u8 *addr)
  89. {
  90. struct sta_info *sta;
  91. sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
  92. while (sta) {
  93. if (memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
  94. break;
  95. sta = rcu_dereference(sta->hnext);
  96. }
  97. return sta;
  98. }
  99. struct sta_info *sta_info_get_by_idx(struct ieee80211_local *local, int idx,
  100. struct net_device *dev)
  101. {
  102. struct sta_info *sta;
  103. int i = 0;
  104. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  105. if (dev && dev != sta->sdata->dev)
  106. continue;
  107. if (i < idx) {
  108. ++i;
  109. continue;
  110. }
  111. return sta;
  112. }
  113. return NULL;
  114. }
  115. /**
  116. * __sta_info_free - internal STA free helper
  117. *
  118. * @local: pointer to the global information
  119. * @sta: STA info to free
  120. *
  121. * This function must undo everything done by sta_info_alloc()
  122. * that may happen before sta_info_insert().
  123. */
  124. static void __sta_info_free(struct ieee80211_local *local,
  125. struct sta_info *sta)
  126. {
  127. rate_control_free_sta(sta);
  128. rate_control_put(sta->rate_ctrl);
  129. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  130. printk(KERN_DEBUG "%s: Destroyed STA %pM\n",
  131. wiphy_name(local->hw.wiphy), sta->sta.addr);
  132. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  133. kfree(sta);
  134. }
  135. void sta_info_destroy(struct sta_info *sta)
  136. {
  137. struct ieee80211_local *local;
  138. struct sk_buff *skb;
  139. int i;
  140. might_sleep();
  141. if (!sta)
  142. return;
  143. local = sta->local;
  144. rate_control_remove_sta_debugfs(sta);
  145. ieee80211_sta_debugfs_remove(sta);
  146. #ifdef CONFIG_MAC80211_MESH
  147. if (ieee80211_vif_is_mesh(&sta->sdata->vif))
  148. mesh_plink_deactivate(sta);
  149. #endif
  150. /*
  151. * We have only unlinked the key, and actually destroying it
  152. * may mean it is removed from hardware which requires that
  153. * the key->sta pointer is still valid, so flush the key todo
  154. * list here.
  155. *
  156. * ieee80211_key_todo() will synchronize_rcu() so after this
  157. * nothing can reference this sta struct any more.
  158. */
  159. ieee80211_key_todo();
  160. #ifdef CONFIG_MAC80211_MESH
  161. if (ieee80211_vif_is_mesh(&sta->sdata->vif))
  162. del_timer_sync(&sta->plink_timer);
  163. #endif
  164. while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
  165. local->total_ps_buffered--;
  166. dev_kfree_skb_any(skb);
  167. }
  168. while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
  169. dev_kfree_skb_any(skb);
  170. for (i = 0; i < STA_TID_NUM; i++) {
  171. struct tid_ampdu_rx *tid_rx;
  172. struct tid_ampdu_tx *tid_tx;
  173. spin_lock_bh(&sta->lock);
  174. tid_rx = sta->ampdu_mlme.tid_rx[i];
  175. /* Make sure timer won't free the tid_rx struct, see below */
  176. if (tid_rx)
  177. tid_rx->shutdown = true;
  178. /*
  179. * The stop callback cannot find this station any more, but
  180. * it didn't complete its work -- start the queue if necessary
  181. */
  182. if (sta->ampdu_mlme.tid_state_tx[i] & HT_AGG_STATE_INITIATOR_MSK &&
  183. sta->ampdu_mlme.tid_state_tx[i] & HT_AGG_STATE_REQ_STOP_BA_MSK &&
  184. local->hw.ampdu_queues)
  185. ieee80211_wake_queue_by_reason(&local->hw,
  186. local->hw.queues + sta->tid_to_tx_q[i],
  187. IEEE80211_QUEUE_STOP_REASON_AGGREGATION);
  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. sta->tid_to_tx_q[i] = -1;
  256. /* rx */
  257. sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
  258. sta->ampdu_mlme.tid_rx[i] = NULL;
  259. /* tx */
  260. sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
  261. sta->ampdu_mlme.tid_tx[i] = NULL;
  262. sta->ampdu_mlme.addba_req_num[i] = 0;
  263. }
  264. skb_queue_head_init(&sta->ps_tx_buf);
  265. skb_queue_head_init(&sta->tx_filtered);
  266. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  267. printk(KERN_DEBUG "%s: Allocated STA %pM\n",
  268. wiphy_name(local->hw.wiphy), sta->sta.addr);
  269. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  270. #ifdef CONFIG_MAC80211_MESH
  271. sta->plink_state = PLINK_LISTEN;
  272. init_timer(&sta->plink_timer);
  273. #endif
  274. return sta;
  275. }
  276. int sta_info_insert(struct sta_info *sta)
  277. {
  278. struct ieee80211_local *local = sta->local;
  279. struct ieee80211_sub_if_data *sdata = sta->sdata;
  280. unsigned long flags;
  281. int err = 0;
  282. /*
  283. * Can't be a WARN_ON because it can be triggered through a race:
  284. * something inserts a STA (on one CPU) without holding the RTNL
  285. * and another CPU turns off the net device.
  286. */
  287. if (unlikely(!netif_running(sdata->dev))) {
  288. err = -ENETDOWN;
  289. goto out_free;
  290. }
  291. if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->dev->dev_addr) == 0 ||
  292. is_multicast_ether_addr(sta->sta.addr))) {
  293. err = -EINVAL;
  294. goto out_free;
  295. }
  296. spin_lock_irqsave(&local->sta_lock, flags);
  297. /* check if STA exists already */
  298. if (sta_info_get(local, sta->sta.addr)) {
  299. spin_unlock_irqrestore(&local->sta_lock, flags);
  300. err = -EEXIST;
  301. goto out_free;
  302. }
  303. list_add(&sta->list, &local->sta_list);
  304. local->num_sta++;
  305. sta_info_hash_add(local, sta);
  306. /* notify driver */
  307. if (local->ops->sta_notify) {
  308. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  309. sdata = container_of(sdata->bss,
  310. struct ieee80211_sub_if_data,
  311. u.ap);
  312. local->ops->sta_notify(local_to_hw(local), &sdata->vif,
  313. 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. sta->local->ops->set_tim(local_to_hw(sta->local),
  362. &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. sta->local->ops->set_tim(local_to_hw(sta->local),
  382. &sta->sta, false);
  383. sta->local->tim_in_locked_section = false;
  384. }
  385. }
  386. void sta_info_clear_tim_bit(struct sta_info *sta)
  387. {
  388. unsigned long flags;
  389. BUG_ON(!sta->sdata->bss);
  390. spin_lock_irqsave(&sta->local->sta_lock, flags);
  391. __sta_info_clear_tim_bit(sta->sdata->bss, sta);
  392. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  393. }
  394. static void __sta_info_unlink(struct sta_info **sta)
  395. {
  396. struct ieee80211_local *local = (*sta)->local;
  397. struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
  398. /*
  399. * pull caller's reference if we're already gone.
  400. */
  401. if (sta_info_hash_del(local, *sta)) {
  402. *sta = NULL;
  403. return;
  404. }
  405. if ((*sta)->key) {
  406. ieee80211_key_free((*sta)->key);
  407. WARN_ON((*sta)->key);
  408. }
  409. list_del(&(*sta)->list);
  410. if (test_and_clear_sta_flags(*sta, WLAN_STA_PS)) {
  411. BUG_ON(!sdata->bss);
  412. atomic_dec(&sdata->bss->num_sta_ps);
  413. __sta_info_clear_tim_bit(sdata->bss, *sta);
  414. }
  415. local->num_sta--;
  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. local->ops->sta_notify(local_to_hw(local), &sdata->vif,
  422. STA_NOTIFY_REMOVE, &(*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 inline int sta_info_buffer_expired(struct ieee80211_local *local,
  477. struct sta_info *sta,
  478. struct sk_buff *skb)
  479. {
  480. struct ieee80211_tx_info *info;
  481. int timeout;
  482. if (!skb)
  483. return 0;
  484. info = IEEE80211_SKB_CB(skb);
  485. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  486. timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
  487. 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(local, 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. 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. static void __ieee80211_run_pending_flush(struct ieee80211_local *local)
  595. {
  596. struct sta_info *sta;
  597. unsigned long flags;
  598. ASSERT_RTNL();
  599. spin_lock_irqsave(&local->sta_lock, flags);
  600. while (!list_empty(&local->sta_flush_list)) {
  601. sta = list_first_entry(&local->sta_flush_list,
  602. struct sta_info, list);
  603. list_del(&sta->list);
  604. spin_unlock_irqrestore(&local->sta_lock, flags);
  605. sta_info_destroy(sta);
  606. spin_lock_irqsave(&local->sta_lock, flags);
  607. }
  608. spin_unlock_irqrestore(&local->sta_lock, flags);
  609. }
  610. static void ieee80211_sta_flush_work(struct work_struct *work)
  611. {
  612. struct ieee80211_local *local =
  613. container_of(work, struct ieee80211_local, sta_flush_work);
  614. rtnl_lock();
  615. __ieee80211_run_pending_flush(local);
  616. rtnl_unlock();
  617. }
  618. void sta_info_init(struct ieee80211_local *local)
  619. {
  620. spin_lock_init(&local->sta_lock);
  621. INIT_LIST_HEAD(&local->sta_list);
  622. INIT_LIST_HEAD(&local->sta_flush_list);
  623. INIT_WORK(&local->sta_flush_work, ieee80211_sta_flush_work);
  624. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  625. (unsigned long)local);
  626. local->sta_cleanup.expires =
  627. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
  628. #ifdef CONFIG_MAC80211_DEBUGFS
  629. INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
  630. #endif
  631. }
  632. int sta_info_start(struct ieee80211_local *local)
  633. {
  634. add_timer(&local->sta_cleanup);
  635. return 0;
  636. }
  637. void sta_info_stop(struct ieee80211_local *local)
  638. {
  639. del_timer(&local->sta_cleanup);
  640. cancel_work_sync(&local->sta_flush_work);
  641. #ifdef CONFIG_MAC80211_DEBUGFS
  642. /*
  643. * Make sure the debugfs adding work isn't pending after this
  644. * because we're about to be destroyed. It doesn't matter
  645. * whether it ran or not since we're going to flush all STAs
  646. * anyway.
  647. */
  648. cancel_work_sync(&local->sta_debugfs_add);
  649. #endif
  650. rtnl_lock();
  651. sta_info_flush(local, NULL);
  652. __ieee80211_run_pending_flush(local);
  653. rtnl_unlock();
  654. }
  655. /**
  656. * sta_info_flush - flush matching STA entries from the STA table
  657. *
  658. * Returns the number of removed STA entries.
  659. *
  660. * @local: local interface data
  661. * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
  662. */
  663. int sta_info_flush(struct ieee80211_local *local,
  664. struct ieee80211_sub_if_data *sdata)
  665. {
  666. struct sta_info *sta, *tmp;
  667. LIST_HEAD(tmp_list);
  668. int ret = 0;
  669. unsigned long flags;
  670. might_sleep();
  671. ASSERT_RTNL();
  672. spin_lock_irqsave(&local->sta_lock, flags);
  673. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  674. if (!sdata || sdata == sta->sdata) {
  675. __sta_info_unlink(&sta);
  676. if (sta) {
  677. list_add_tail(&sta->list, &tmp_list);
  678. ret++;
  679. }
  680. }
  681. }
  682. spin_unlock_irqrestore(&local->sta_lock, flags);
  683. list_for_each_entry_safe(sta, tmp, &tmp_list, list)
  684. sta_info_destroy(sta);
  685. return ret;
  686. }
  687. /**
  688. * sta_info_flush_delayed - flush matching STA entries from the STA table
  689. *
  690. * This function unlinks all stations for a given interface and queues
  691. * them for freeing. Note that the workqueue function scheduled here has
  692. * to run before any new keys can be added to the system to avoid set_key()
  693. * callback ordering issues.
  694. *
  695. * @sdata: the interface
  696. */
  697. void sta_info_flush_delayed(struct ieee80211_sub_if_data *sdata)
  698. {
  699. struct ieee80211_local *local = sdata->local;
  700. struct sta_info *sta, *tmp;
  701. unsigned long flags;
  702. bool work = false;
  703. spin_lock_irqsave(&local->sta_lock, flags);
  704. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  705. if (sdata == sta->sdata) {
  706. __sta_info_unlink(&sta);
  707. if (sta) {
  708. list_add_tail(&sta->list,
  709. &local->sta_flush_list);
  710. work = true;
  711. }
  712. }
  713. }
  714. if (work)
  715. schedule_work(&local->sta_flush_work);
  716. spin_unlock_irqrestore(&local->sta_lock, flags);
  717. }
  718. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  719. unsigned long exp_time)
  720. {
  721. struct ieee80211_local *local = sdata->local;
  722. struct sta_info *sta, *tmp;
  723. LIST_HEAD(tmp_list);
  724. unsigned long flags;
  725. spin_lock_irqsave(&local->sta_lock, flags);
  726. list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
  727. if (time_after(jiffies, sta->last_rx + exp_time)) {
  728. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  729. printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
  730. sdata->dev->name, sta->sta.addr);
  731. #endif
  732. __sta_info_unlink(&sta);
  733. if (sta)
  734. list_add(&sta->list, &tmp_list);
  735. }
  736. spin_unlock_irqrestore(&local->sta_lock, flags);
  737. list_for_each_entry_safe(sta, tmp, &tmp_list, list)
  738. sta_info_destroy(sta);
  739. }
  740. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_hw *hw,
  741. const u8 *addr)
  742. {
  743. struct sta_info *sta = sta_info_get(hw_to_local(hw), addr);
  744. if (!sta)
  745. return NULL;
  746. return &sta->sta;
  747. }
  748. EXPORT_SYMBOL(ieee80211_find_sta);