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