sta_info.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 <net/mac80211.h>
  17. #include "ieee80211_i.h"
  18. #include "ieee80211_rate.h"
  19. #include "sta_info.h"
  20. #include "debugfs_sta.h"
  21. /* Caller must hold local->sta_lock */
  22. static void sta_info_hash_add(struct ieee80211_local *local,
  23. struct sta_info *sta)
  24. {
  25. sta->hnext = local->sta_hash[STA_HASH(sta->addr)];
  26. local->sta_hash[STA_HASH(sta->addr)] = sta;
  27. }
  28. /* Caller must hold local->sta_lock */
  29. static int sta_info_hash_del(struct ieee80211_local *local,
  30. struct sta_info *sta)
  31. {
  32. struct sta_info *s;
  33. s = local->sta_hash[STA_HASH(sta->addr)];
  34. if (!s)
  35. return -ENOENT;
  36. if (s == sta) {
  37. local->sta_hash[STA_HASH(sta->addr)] = s->hnext;
  38. return 0;
  39. }
  40. while (s->hnext && s->hnext != sta)
  41. s = s->hnext;
  42. if (s->hnext) {
  43. s->hnext = sta->hnext;
  44. return 0;
  45. }
  46. return -ENOENT;
  47. }
  48. struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr)
  49. {
  50. struct sta_info *sta;
  51. read_lock_bh(&local->sta_lock);
  52. sta = local->sta_hash[STA_HASH(addr)];
  53. while (sta) {
  54. if (memcmp(sta->addr, addr, ETH_ALEN) == 0) {
  55. __sta_info_get(sta);
  56. break;
  57. }
  58. sta = sta->hnext;
  59. }
  60. read_unlock_bh(&local->sta_lock);
  61. return sta;
  62. }
  63. EXPORT_SYMBOL(sta_info_get);
  64. int sta_info_min_txrate_get(struct ieee80211_local *local)
  65. {
  66. struct sta_info *sta;
  67. struct ieee80211_hw_mode *mode;
  68. int min_txrate = 9999999;
  69. int i;
  70. read_lock_bh(&local->sta_lock);
  71. mode = local->oper_hw_mode;
  72. for (i = 0; i < STA_HASH_SIZE; i++) {
  73. sta = local->sta_hash[i];
  74. while (sta) {
  75. if (sta->txrate < min_txrate)
  76. min_txrate = sta->txrate;
  77. sta = sta->hnext;
  78. }
  79. }
  80. read_unlock_bh(&local->sta_lock);
  81. if (min_txrate == 9999999)
  82. min_txrate = 0;
  83. return mode->rates[min_txrate].rate;
  84. }
  85. static void sta_info_release(struct kref *kref)
  86. {
  87. struct sta_info *sta = container_of(kref, struct sta_info, kref);
  88. struct ieee80211_local *local = sta->local;
  89. struct sk_buff *skb;
  90. /* free sta structure; it has already been removed from
  91. * hash table etc. external structures. Make sure that all
  92. * buffered frames are release (one might have been added
  93. * after sta_info_free() was called). */
  94. while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
  95. local->total_ps_buffered--;
  96. dev_kfree_skb_any(skb);
  97. }
  98. while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
  99. dev_kfree_skb_any(skb);
  100. }
  101. rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
  102. rate_control_put(sta->rate_ctrl);
  103. kfree(sta);
  104. }
  105. void sta_info_put(struct sta_info *sta)
  106. {
  107. kref_put(&sta->kref, sta_info_release);
  108. }
  109. EXPORT_SYMBOL(sta_info_put);
  110. struct sta_info * sta_info_add(struct ieee80211_local *local,
  111. struct net_device *dev, u8 *addr, gfp_t gfp)
  112. {
  113. struct sta_info *sta;
  114. DECLARE_MAC_BUF(mac);
  115. sta = kzalloc(sizeof(*sta), gfp);
  116. if (!sta)
  117. return NULL;
  118. kref_init(&sta->kref);
  119. sta->rate_ctrl = rate_control_get(local->rate_ctrl);
  120. sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, gfp);
  121. if (!sta->rate_ctrl_priv) {
  122. rate_control_put(sta->rate_ctrl);
  123. kfree(sta);
  124. return NULL;
  125. }
  126. memcpy(sta->addr, addr, ETH_ALEN);
  127. sta->local = local;
  128. sta->dev = dev;
  129. skb_queue_head_init(&sta->ps_tx_buf);
  130. skb_queue_head_init(&sta->tx_filtered);
  131. __sta_info_get(sta); /* sta used by caller, decremented by
  132. * sta_info_put() */
  133. write_lock_bh(&local->sta_lock);
  134. list_add(&sta->list, &local->sta_list);
  135. local->num_sta++;
  136. sta_info_hash_add(local, sta);
  137. if (local->ops->sta_notify)
  138. local->ops->sta_notify(local_to_hw(local), dev->ifindex,
  139. STA_NOTIFY_ADD, addr);
  140. write_unlock_bh(&local->sta_lock);
  141. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  142. printk(KERN_DEBUG "%s: Added STA %s\n",
  143. wiphy_name(local->hw.wiphy), print_mac(mac, addr));
  144. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  145. #ifdef CONFIG_MAC80211_DEBUGFS
  146. /* debugfs entry adding might sleep, so schedule process
  147. * context task for adding entry for STAs that do not yet
  148. * have one. */
  149. queue_work(local->hw.workqueue, &local->sta_debugfs_add);
  150. #endif
  151. return sta;
  152. }
  153. /* Caller must hold local->sta_lock */
  154. void sta_info_remove(struct sta_info *sta)
  155. {
  156. struct ieee80211_local *local = sta->local;
  157. struct ieee80211_sub_if_data *sdata;
  158. /* don't do anything if we've been removed already */
  159. if (sta_info_hash_del(local, sta))
  160. return;
  161. list_del(&sta->list);
  162. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  163. if (sta->flags & WLAN_STA_PS) {
  164. sta->flags &= ~WLAN_STA_PS;
  165. if (sdata->bss)
  166. atomic_dec(&sdata->bss->num_sta_ps);
  167. }
  168. local->num_sta--;
  169. sta_info_remove_aid_ptr(sta);
  170. }
  171. void sta_info_free(struct sta_info *sta)
  172. {
  173. struct sk_buff *skb;
  174. struct ieee80211_local *local = sta->local;
  175. DECLARE_MAC_BUF(mac);
  176. might_sleep();
  177. write_lock_bh(&local->sta_lock);
  178. sta_info_remove(sta);
  179. write_unlock_bh(&local->sta_lock);
  180. while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
  181. local->total_ps_buffered--;
  182. dev_kfree_skb(skb);
  183. }
  184. while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
  185. dev_kfree_skb(skb);
  186. }
  187. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  188. printk(KERN_DEBUG "%s: Removed STA %s\n",
  189. wiphy_name(local->hw.wiphy), print_mac(mac, sta->addr));
  190. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  191. ieee80211_key_free(sta->key);
  192. sta->key = NULL;
  193. if (local->ops->sta_notify)
  194. local->ops->sta_notify(local_to_hw(local), sta->dev->ifindex,
  195. STA_NOTIFY_REMOVE, sta->addr);
  196. rate_control_remove_sta_debugfs(sta);
  197. ieee80211_sta_debugfs_remove(sta);
  198. sta_info_put(sta);
  199. }
  200. static inline int sta_info_buffer_expired(struct ieee80211_local *local,
  201. struct sta_info *sta,
  202. struct sk_buff *skb)
  203. {
  204. struct ieee80211_tx_packet_data *pkt_data;
  205. int timeout;
  206. if (!skb)
  207. return 0;
  208. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  209. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  210. timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
  211. 15625) * HZ;
  212. if (timeout < STA_TX_BUFFER_EXPIRE)
  213. timeout = STA_TX_BUFFER_EXPIRE;
  214. return time_after(jiffies, pkt_data->jiffies + timeout);
  215. }
  216. static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  217. struct sta_info *sta)
  218. {
  219. unsigned long flags;
  220. struct sk_buff *skb;
  221. DECLARE_MAC_BUF(mac);
  222. if (skb_queue_empty(&sta->ps_tx_buf))
  223. return;
  224. for (;;) {
  225. spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
  226. skb = skb_peek(&sta->ps_tx_buf);
  227. if (sta_info_buffer_expired(local, sta, skb)) {
  228. skb = __skb_dequeue(&sta->ps_tx_buf);
  229. if (skb_queue_empty(&sta->ps_tx_buf))
  230. sta->flags &= ~WLAN_STA_TIM;
  231. } else
  232. skb = NULL;
  233. spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
  234. if (skb) {
  235. local->total_ps_buffered--;
  236. printk(KERN_DEBUG "Buffered frame expired (STA "
  237. "%s)\n", print_mac(mac, sta->addr));
  238. dev_kfree_skb(skb);
  239. } else
  240. break;
  241. }
  242. }
  243. static void sta_info_cleanup(unsigned long data)
  244. {
  245. struct ieee80211_local *local = (struct ieee80211_local *) data;
  246. struct sta_info *sta;
  247. read_lock_bh(&local->sta_lock);
  248. list_for_each_entry(sta, &local->sta_list, list) {
  249. __sta_info_get(sta);
  250. sta_info_cleanup_expire_buffered(local, sta);
  251. sta_info_put(sta);
  252. }
  253. read_unlock_bh(&local->sta_lock);
  254. local->sta_cleanup.expires = jiffies + STA_INFO_CLEANUP_INTERVAL;
  255. add_timer(&local->sta_cleanup);
  256. }
  257. #ifdef CONFIG_MAC80211_DEBUGFS
  258. static void sta_info_debugfs_add_task(struct work_struct *work)
  259. {
  260. struct ieee80211_local *local =
  261. container_of(work, struct ieee80211_local, sta_debugfs_add);
  262. struct sta_info *sta, *tmp;
  263. while (1) {
  264. sta = NULL;
  265. read_lock_bh(&local->sta_lock);
  266. list_for_each_entry(tmp, &local->sta_list, list) {
  267. if (!tmp->debugfs.dir) {
  268. sta = tmp;
  269. __sta_info_get(sta);
  270. break;
  271. }
  272. }
  273. read_unlock_bh(&local->sta_lock);
  274. if (!sta)
  275. break;
  276. ieee80211_sta_debugfs_add(sta);
  277. rate_control_add_sta_debugfs(sta);
  278. sta_info_put(sta);
  279. }
  280. }
  281. #endif
  282. void sta_info_init(struct ieee80211_local *local)
  283. {
  284. rwlock_init(&local->sta_lock);
  285. INIT_LIST_HEAD(&local->sta_list);
  286. init_timer(&local->sta_cleanup);
  287. local->sta_cleanup.expires = jiffies + STA_INFO_CLEANUP_INTERVAL;
  288. local->sta_cleanup.data = (unsigned long) local;
  289. local->sta_cleanup.function = sta_info_cleanup;
  290. #ifdef CONFIG_MAC80211_DEBUGFS
  291. INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_task);
  292. #endif
  293. }
  294. int sta_info_start(struct ieee80211_local *local)
  295. {
  296. add_timer(&local->sta_cleanup);
  297. return 0;
  298. }
  299. void sta_info_stop(struct ieee80211_local *local)
  300. {
  301. del_timer(&local->sta_cleanup);
  302. sta_info_flush(local, NULL);
  303. }
  304. void sta_info_remove_aid_ptr(struct sta_info *sta)
  305. {
  306. struct ieee80211_sub_if_data *sdata;
  307. if (sta->aid <= 0)
  308. return;
  309. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  310. if (sdata->local->ops->set_tim)
  311. sdata->local->ops->set_tim(local_to_hw(sdata->local),
  312. sta->aid, 0);
  313. if (sdata->bss)
  314. __bss_tim_clear(sdata->bss, sta->aid);
  315. }
  316. /**
  317. * sta_info_flush - flush matching STA entries from the STA table
  318. * @local: local interface data
  319. * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
  320. */
  321. void sta_info_flush(struct ieee80211_local *local, struct net_device *dev)
  322. {
  323. struct sta_info *sta, *tmp;
  324. LIST_HEAD(tmp_list);
  325. write_lock_bh(&local->sta_lock);
  326. list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
  327. if (!dev || dev == sta->dev) {
  328. __sta_info_get(sta);
  329. sta_info_remove(sta);
  330. list_add_tail(&sta->list, &tmp_list);
  331. }
  332. write_unlock_bh(&local->sta_lock);
  333. list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
  334. sta_info_free(sta);
  335. sta_info_put(sta);
  336. }
  337. }