sta_info.c 11 KB

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