sta_info.c 22 KB

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