sta_info.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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_key.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 void 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;
  37. if (memcmp(s->addr, sta->addr, ETH_ALEN) == 0) {
  38. local->sta_hash[STA_HASH(sta->addr)] = s->hnext;
  39. return;
  40. }
  41. while (s->hnext && memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
  42. s = s->hnext;
  43. if (s->hnext)
  44. s->hnext = s->hnext->hnext;
  45. else
  46. printk(KERN_ERR "%s: could not remove STA " MAC_FMT " from "
  47. "hash table\n", local->mdev->name, MAC_ARG(sta->addr));
  48. }
  49. static inline void __sta_info_get(struct sta_info *sta)
  50. {
  51. kref_get(&sta->kref);
  52. }
  53. struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr)
  54. {
  55. struct sta_info *sta;
  56. spin_lock_bh(&local->sta_lock);
  57. sta = local->sta_hash[STA_HASH(addr)];
  58. while (sta) {
  59. if (memcmp(sta->addr, addr, ETH_ALEN) == 0) {
  60. __sta_info_get(sta);
  61. break;
  62. }
  63. sta = sta->hnext;
  64. }
  65. spin_unlock_bh(&local->sta_lock);
  66. return sta;
  67. }
  68. EXPORT_SYMBOL(sta_info_get);
  69. int sta_info_min_txrate_get(struct ieee80211_local *local)
  70. {
  71. struct sta_info *sta;
  72. struct ieee80211_hw_mode *mode;
  73. int min_txrate = 9999999;
  74. int i;
  75. spin_lock_bh(&local->sta_lock);
  76. mode = local->oper_hw_mode;
  77. for (i = 0; i < STA_HASH_SIZE; i++) {
  78. sta = local->sta_hash[i];
  79. while (sta) {
  80. if (sta->txrate < min_txrate)
  81. min_txrate = sta->txrate;
  82. sta = sta->hnext;
  83. }
  84. }
  85. spin_unlock_bh(&local->sta_lock);
  86. if (min_txrate == 9999999)
  87. min_txrate = 0;
  88. return mode->rates[min_txrate].rate;
  89. }
  90. static void sta_info_release(struct kref *kref)
  91. {
  92. struct sta_info *sta = container_of(kref, struct sta_info, kref);
  93. struct ieee80211_local *local = sta->local;
  94. struct sk_buff *skb;
  95. /* free sta structure; it has already been removed from
  96. * hash table etc. external structures. Make sure that all
  97. * buffered frames are release (one might have been added
  98. * after sta_info_free() was called). */
  99. while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
  100. local->total_ps_buffered--;
  101. dev_kfree_skb_any(skb);
  102. }
  103. while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
  104. dev_kfree_skb_any(skb);
  105. }
  106. rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
  107. rate_control_put(sta->rate_ctrl);
  108. if (sta->key)
  109. ieee80211_debugfs_key_sta_del(sta->key, sta);
  110. kfree(sta);
  111. }
  112. void sta_info_put(struct sta_info *sta)
  113. {
  114. kref_put(&sta->kref, sta_info_release);
  115. }
  116. EXPORT_SYMBOL(sta_info_put);
  117. struct sta_info * sta_info_add(struct ieee80211_local *local,
  118. struct net_device *dev, u8 *addr, gfp_t gfp)
  119. {
  120. struct sta_info *sta;
  121. sta = kzalloc(sizeof(*sta), gfp);
  122. if (!sta)
  123. return NULL;
  124. kref_init(&sta->kref);
  125. sta->rate_ctrl = rate_control_get(local->rate_ctrl);
  126. sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, gfp);
  127. if (!sta->rate_ctrl_priv) {
  128. rate_control_put(sta->rate_ctrl);
  129. kref_put(&sta->kref, sta_info_release);
  130. kfree(sta);
  131. return NULL;
  132. }
  133. memcpy(sta->addr, addr, ETH_ALEN);
  134. sta->local = local;
  135. sta->dev = dev;
  136. skb_queue_head_init(&sta->ps_tx_buf);
  137. skb_queue_head_init(&sta->tx_filtered);
  138. __sta_info_get(sta); /* sta used by caller, decremented by
  139. * sta_info_put() */
  140. spin_lock_bh(&local->sta_lock);
  141. list_add(&sta->list, &local->sta_list);
  142. local->num_sta++;
  143. sta_info_hash_add(local, sta);
  144. spin_unlock_bh(&local->sta_lock);
  145. if (local->ops->sta_table_notification)
  146. local->ops->sta_table_notification(local_to_hw(local),
  147. local->num_sta);
  148. sta->key_idx_compression = HW_KEY_IDX_INVALID;
  149. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  150. printk(KERN_DEBUG "%s: Added STA " MAC_FMT "\n",
  151. local->mdev->name, MAC_ARG(addr));
  152. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  153. #ifdef CONFIG_MAC80211_DEBUGFS
  154. if (!in_interrupt()) {
  155. sta->debugfs_registered = 1;
  156. ieee80211_sta_debugfs_add(sta);
  157. rate_control_add_sta_debugfs(sta);
  158. } else {
  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. }
  164. #endif
  165. return sta;
  166. }
  167. static void finish_sta_info_free(struct ieee80211_local *local,
  168. struct sta_info *sta)
  169. {
  170. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  171. printk(KERN_DEBUG "%s: Removed STA " MAC_FMT "\n",
  172. local->mdev->name, MAC_ARG(sta->addr));
  173. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  174. if (sta->key) {
  175. ieee80211_debugfs_key_remove(sta->key);
  176. ieee80211_key_free(sta->key);
  177. sta->key = NULL;
  178. }
  179. rate_control_remove_sta_debugfs(sta);
  180. ieee80211_sta_debugfs_remove(sta);
  181. sta_info_put(sta);
  182. }
  183. static void sta_info_remove(struct sta_info *sta)
  184. {
  185. struct ieee80211_local *local = sta->local;
  186. struct ieee80211_sub_if_data *sdata;
  187. sta_info_hash_del(local, sta);
  188. list_del(&sta->list);
  189. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  190. if (sta->flags & WLAN_STA_PS) {
  191. sta->flags &= ~WLAN_STA_PS;
  192. if (sdata->bss)
  193. atomic_dec(&sdata->bss->num_sta_ps);
  194. }
  195. local->num_sta--;
  196. sta_info_remove_aid_ptr(sta);
  197. }
  198. void sta_info_free(struct sta_info *sta, int locked)
  199. {
  200. struct sk_buff *skb;
  201. struct ieee80211_local *local = sta->local;
  202. if (!locked) {
  203. spin_lock_bh(&local->sta_lock);
  204. sta_info_remove(sta);
  205. spin_unlock_bh(&local->sta_lock);
  206. } else {
  207. sta_info_remove(sta);
  208. }
  209. if (local->ops->sta_table_notification)
  210. local->ops->sta_table_notification(local_to_hw(local),
  211. local->num_sta);
  212. while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
  213. local->total_ps_buffered--;
  214. dev_kfree_skb_any(skb);
  215. }
  216. while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
  217. dev_kfree_skb_any(skb);
  218. }
  219. if (sta->key) {
  220. if (local->ops->set_key) {
  221. struct ieee80211_key_conf *key;
  222. key = ieee80211_key_data2conf(local, sta->key);
  223. if (key) {
  224. local->ops->set_key(local_to_hw(local),
  225. DISABLE_KEY,
  226. sta->addr, key, sta->aid);
  227. kfree(key);
  228. }
  229. }
  230. } else if (sta->key_idx_compression != HW_KEY_IDX_INVALID) {
  231. struct ieee80211_key_conf conf;
  232. memset(&conf, 0, sizeof(conf));
  233. conf.hw_key_idx = sta->key_idx_compression;
  234. conf.alg = ALG_NULL;
  235. conf.flags |= IEEE80211_KEY_FORCE_SW_ENCRYPT;
  236. local->ops->set_key(local_to_hw(local), DISABLE_KEY,
  237. sta->addr, &conf, sta->aid);
  238. sta->key_idx_compression = HW_KEY_IDX_INVALID;
  239. }
  240. #ifdef CONFIG_MAC80211_DEBUGFS
  241. if (in_atomic()) {
  242. list_add(&sta->list, &local->deleted_sta_list);
  243. queue_work(local->hw.workqueue, &local->sta_debugfs_add);
  244. } else
  245. #endif
  246. finish_sta_info_free(local, sta);
  247. }
  248. static inline int sta_info_buffer_expired(struct ieee80211_local *local,
  249. struct sta_info *sta,
  250. struct sk_buff *skb)
  251. {
  252. struct ieee80211_tx_packet_data *pkt_data;
  253. int timeout;
  254. if (!skb)
  255. return 0;
  256. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  257. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  258. timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
  259. 15625) * HZ;
  260. if (timeout < STA_TX_BUFFER_EXPIRE)
  261. timeout = STA_TX_BUFFER_EXPIRE;
  262. return time_after(jiffies, pkt_data->jiffies + timeout);
  263. }
  264. static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  265. struct sta_info *sta)
  266. {
  267. unsigned long flags;
  268. struct sk_buff *skb;
  269. if (skb_queue_empty(&sta->ps_tx_buf))
  270. return;
  271. for (;;) {
  272. spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
  273. skb = skb_peek(&sta->ps_tx_buf);
  274. if (sta_info_buffer_expired(local, sta, skb)) {
  275. skb = __skb_dequeue(&sta->ps_tx_buf);
  276. if (skb_queue_empty(&sta->ps_tx_buf))
  277. sta->flags &= ~WLAN_STA_TIM;
  278. } else
  279. skb = NULL;
  280. spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
  281. if (skb) {
  282. local->total_ps_buffered--;
  283. printk(KERN_DEBUG "Buffered frame expired (STA "
  284. MAC_FMT ")\n", MAC_ARG(sta->addr));
  285. dev_kfree_skb(skb);
  286. } else
  287. break;
  288. }
  289. }
  290. static void sta_info_cleanup(unsigned long data)
  291. {
  292. struct ieee80211_local *local = (struct ieee80211_local *) data;
  293. struct sta_info *sta;
  294. spin_lock_bh(&local->sta_lock);
  295. list_for_each_entry(sta, &local->sta_list, list) {
  296. __sta_info_get(sta);
  297. sta_info_cleanup_expire_buffered(local, sta);
  298. sta_info_put(sta);
  299. }
  300. spin_unlock_bh(&local->sta_lock);
  301. local->sta_cleanup.expires = jiffies + STA_INFO_CLEANUP_INTERVAL;
  302. add_timer(&local->sta_cleanup);
  303. }
  304. #ifdef CONFIG_MAC80211_DEBUGFS
  305. static void sta_info_debugfs_add_task(struct work_struct *work)
  306. {
  307. struct ieee80211_local *local =
  308. container_of(work, struct ieee80211_local, sta_debugfs_add);
  309. struct sta_info *sta, *tmp;
  310. while (1) {
  311. spin_lock_bh(&local->sta_lock);
  312. if (!list_empty(&local->deleted_sta_list)) {
  313. sta = list_entry(local->deleted_sta_list.next,
  314. struct sta_info, list);
  315. list_del(local->deleted_sta_list.next);
  316. } else
  317. sta = NULL;
  318. spin_unlock_bh(&local->sta_lock);
  319. if (!sta)
  320. break;
  321. finish_sta_info_free(local, sta);
  322. }
  323. while (1) {
  324. sta = NULL;
  325. spin_lock_bh(&local->sta_lock);
  326. list_for_each_entry(tmp, &local->sta_list, list) {
  327. if (!tmp->debugfs_registered) {
  328. sta = tmp;
  329. __sta_info_get(sta);
  330. break;
  331. }
  332. }
  333. spin_unlock_bh(&local->sta_lock);
  334. if (!sta)
  335. break;
  336. sta->debugfs_registered = 1;
  337. ieee80211_sta_debugfs_add(sta);
  338. rate_control_add_sta_debugfs(sta);
  339. sta_info_put(sta);
  340. }
  341. }
  342. #endif
  343. void sta_info_init(struct ieee80211_local *local)
  344. {
  345. spin_lock_init(&local->sta_lock);
  346. INIT_LIST_HEAD(&local->sta_list);
  347. INIT_LIST_HEAD(&local->deleted_sta_list);
  348. init_timer(&local->sta_cleanup);
  349. local->sta_cleanup.expires = jiffies + STA_INFO_CLEANUP_INTERVAL;
  350. local->sta_cleanup.data = (unsigned long) local;
  351. local->sta_cleanup.function = sta_info_cleanup;
  352. #ifdef CONFIG_MAC80211_DEBUGFS
  353. INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_task);
  354. #endif
  355. }
  356. int sta_info_start(struct ieee80211_local *local)
  357. {
  358. add_timer(&local->sta_cleanup);
  359. return 0;
  360. }
  361. void sta_info_stop(struct ieee80211_local *local)
  362. {
  363. struct sta_info *sta, *tmp;
  364. del_timer(&local->sta_cleanup);
  365. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  366. /* sta_info_free must be called with 0 as the last
  367. * parameter to ensure all debugfs sta entries are
  368. * unregistered. We don't need locking at this
  369. * point. */
  370. sta_info_free(sta, 0);
  371. }
  372. }
  373. void sta_info_remove_aid_ptr(struct sta_info *sta)
  374. {
  375. struct ieee80211_sub_if_data *sdata;
  376. if (sta->aid <= 0)
  377. return;
  378. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  379. if (sdata->local->ops->set_tim)
  380. sdata->local->ops->set_tim(local_to_hw(sdata->local),
  381. sta->aid, 0);
  382. if (sdata->bss)
  383. __bss_tim_clear(sdata->bss, sta->aid);
  384. }
  385. /**
  386. * sta_info_flush - flush matching STA entries from the STA table
  387. * @local: local interface data
  388. * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
  389. */
  390. void sta_info_flush(struct ieee80211_local *local, struct net_device *dev)
  391. {
  392. struct sta_info *sta, *tmp;
  393. spin_lock_bh(&local->sta_lock);
  394. list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
  395. if (!dev || dev == sta->dev)
  396. sta_info_free(sta, 1);
  397. spin_unlock_bh(&local->sta_lock);
  398. }