sta_info.c 11 KB

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