sta_info.c 12 KB

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