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