sta_info.c 13 KB

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