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 #include <linux/rtnetlink.h> 19 20 #include <net/mac80211.h> 21 #include "ieee80211_i.h" 22 #include "ieee80211_rate.h" 23 #include "sta_info.h" 24 #include "debugfs_sta.h" 25 #include "mesh.h" 26 27 /** 28 * DOC: STA information lifetime rules 29 * 30 * STA info structures (&struct sta_info) are managed in a hash table 31 * for faster lookup and a list for iteration. They are managed using 32 * RCU, i.e. access to the list and hash table is protected by RCU. 33 * 34 * STA info structures are always "alive" when they are added with 35 * @sta_info_add() [this may be changed in the future to allow allocating 36 * outside of a critical section!], they are then added to the hash 37 * table and list. Therefore, @sta_info_add() must also be RCU protected, 38 * also, the caller of @sta_info_add() cannot assume that it owns the 39 * structure. 40 * 41 * Because there are debugfs entries for each station, and adding those 42 * must be able to sleep, it is also possible to "pin" a station entry, 43 * that means it can be removed from the hash table but not be freed. 44 * See the comment in @__sta_info_unlink() for more information. 45 * 46 * In order to remove a STA info structure, the caller needs to first 47 * unlink it (@sta_info_unlink()) from the list and hash tables and 48 * then wait for an RCU synchronisation before it can be freed. Due to 49 * the pinning and the possibility of multiple callers trying to remove 50 * the same STA info at the same time, @sta_info_unlink() can clear the 51 * STA info pointer it is passed to indicate that the STA info is owned 52 * by somebody else now. 53 * 54 * If @sta_info_unlink() did not clear the pointer then the caller owns 55 * the STA info structure now and is responsible of destroying it with 56 * a call to @sta_info_destroy(), not before RCU synchronisation, of 57 * course. Note that sta_info_destroy() must be protected by the RTNL. 58 * 59 * In all other cases, there is no concept of ownership on a STA entry, 60 * each structure is owned by the global hash table/list until it is 61 * removed. All users of the structure need to be RCU protected so that 62 * the structure won't be freed before they are done using it. 63 */ 64 65 /* Caller must hold local->sta_lock */ 66 static int sta_info_hash_del(struct ieee80211_local *local, 67 struct sta_info *sta) 68 { 69 struct sta_info *s; 70 71 s = local->sta_hash[STA_HASH(sta->addr)]; 72 if (!s) 73 return -ENOENT; 74 if (s == sta) { 75 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->addr)], 76 s->hnext); 77 return 0; 78 } 79 80 while (s->hnext && s->hnext != sta) 81 s = s->hnext; 82 if (s->hnext) { 83 rcu_assign_pointer(s->hnext, sta->hnext); 84 return 0; 85 } 86 87 return -ENOENT; 88 } 89 90 /* protected by RCU */ 91 static struct sta_info *__sta_info_find(struct ieee80211_local *local, 92 u8 *addr) 93 { 94 struct sta_info *sta; 95 96 sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]); 97 while (sta) { 98 if (compare_ether_addr(sta->addr, addr) == 0) 99 break; 100 sta = rcu_dereference(sta->hnext); 101 } 102 return sta; 103 } 104 105 struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr) 106 { 107 return __sta_info_find(local, addr); 108 } 109 EXPORT_SYMBOL(sta_info_get); 110 111 struct sta_info *sta_info_get_by_idx(struct ieee80211_local *local, int idx, 112 struct net_device *dev) 113 { 114 struct sta_info *sta; 115 int i = 0; 116 117 list_for_each_entry_rcu(sta, &local->sta_list, list) { 118 if (i < idx) { 119 ++i; 120 continue; 121 } else if (!dev || dev == sta->sdata->dev) { 122 return sta; 123 } 124 } 125 126 return NULL; 127 } 128 129 void sta_info_destroy(struct sta_info *sta) 130 { 131 struct ieee80211_local *local = sta->local; 132 struct sk_buff *skb; 133 int i; 134 135 ASSERT_RTNL(); 136 might_sleep(); 137 138 rate_control_remove_sta_debugfs(sta); 139 ieee80211_sta_debugfs_remove(sta); 140 141 #ifdef CONFIG_MAC80211_MESH 142 if (ieee80211_vif_is_mesh(&sta->sdata->vif)) 143 mesh_plink_deactivate(sta); 144 #endif 145 146 /* 147 * NOTE: This will call synchronize_rcu() internally to 148 * make sure no key references can be in use. We rely on 149 * that here for the mesh code! 150 */ 151 ieee80211_key_free(sta->key); 152 WARN_ON(sta->key); 153 154 #ifdef CONFIG_MAC80211_MESH 155 if (ieee80211_vif_is_mesh(&sta->sdata->vif)) 156 del_timer_sync(&sta->plink_timer); 157 #endif 158 159 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) { 160 local->total_ps_buffered--; 161 dev_kfree_skb_any(skb); 162 } 163 164 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) 165 dev_kfree_skb_any(skb); 166 167 for (i = 0; i < STA_TID_NUM; i++) { 168 del_timer_sync(&sta->ampdu_mlme.tid_rx[i].session_timer); 169 del_timer_sync(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer); 170 } 171 rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv); 172 rate_control_put(sta->rate_ctrl); 173 174 kfree(sta); 175 } 176 177 178 /* Caller must hold local->sta_lock */ 179 static void sta_info_hash_add(struct ieee80211_local *local, 180 struct sta_info *sta) 181 { 182 sta->hnext = local->sta_hash[STA_HASH(sta->addr)]; 183 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->addr)], sta); 184 } 185 186 struct sta_info *sta_info_add(struct ieee80211_sub_if_data *sdata, 187 u8 *addr) 188 { 189 struct ieee80211_local *local = sdata->local; 190 struct sta_info *sta; 191 int i; 192 DECLARE_MAC_BUF(mac); 193 unsigned long flags; 194 195 sta = kzalloc(sizeof(*sta), GFP_ATOMIC); 196 if (!sta) 197 return ERR_PTR(-ENOMEM); 198 199 memcpy(sta->addr, addr, ETH_ALEN); 200 sta->local = local; 201 sta->sdata = sdata; 202 203 sta->rate_ctrl = rate_control_get(local->rate_ctrl); 204 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, 205 GFP_ATOMIC); 206 if (!sta->rate_ctrl_priv) { 207 rate_control_put(sta->rate_ctrl); 208 kfree(sta); 209 return ERR_PTR(-ENOMEM); 210 } 211 212 spin_lock_init(&sta->ampdu_mlme.ampdu_rx); 213 spin_lock_init(&sta->ampdu_mlme.ampdu_tx); 214 for (i = 0; i < STA_TID_NUM; i++) { 215 /* timer_to_tid must be initialized with identity mapping to 216 * enable session_timer's data differentiation. refer to 217 * sta_rx_agg_session_timer_expired for useage */ 218 sta->timer_to_tid[i] = i; 219 /* tid to tx queue: initialize according to HW (0 is valid) */ 220 sta->tid_to_tx_q[i] = local->hw.queues; 221 /* rx timers */ 222 sta->ampdu_mlme.tid_rx[i].session_timer.function = 223 sta_rx_agg_session_timer_expired; 224 sta->ampdu_mlme.tid_rx[i].session_timer.data = 225 (unsigned long)&sta->timer_to_tid[i]; 226 init_timer(&sta->ampdu_mlme.tid_rx[i].session_timer); 227 /* tx timers */ 228 sta->ampdu_mlme.tid_tx[i].addba_resp_timer.function = 229 sta_addba_resp_timer_expired; 230 sta->ampdu_mlme.tid_tx[i].addba_resp_timer.data = 231 (unsigned long)&sta->timer_to_tid[i]; 232 init_timer(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer); 233 } 234 skb_queue_head_init(&sta->ps_tx_buf); 235 skb_queue_head_init(&sta->tx_filtered); 236 spin_lock_irqsave(&local->sta_lock, flags); 237 /* check if STA exists already */ 238 if (__sta_info_find(local, addr)) { 239 spin_unlock_irqrestore(&local->sta_lock, flags); 240 return ERR_PTR(-EEXIST); 241 } 242 list_add(&sta->list, &local->sta_list); 243 local->num_sta++; 244 sta_info_hash_add(local, sta); 245 246 /* notify driver */ 247 if (local->ops->sta_notify) { 248 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN) 249 sdata = sdata->u.vlan.ap; 250 251 local->ops->sta_notify(local_to_hw(local), &sdata->vif, 252 STA_NOTIFY_ADD, addr); 253 } 254 255 spin_unlock_irqrestore(&local->sta_lock, flags); 256 257 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 258 printk(KERN_DEBUG "%s: Added STA %s\n", 259 wiphy_name(local->hw.wiphy), print_mac(mac, addr)); 260 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 261 262 #ifdef CONFIG_MAC80211_DEBUGFS 263 /* debugfs entry adding might sleep, so schedule process 264 * context task for adding entry for STAs that do not yet 265 * have one. */ 266 queue_work(local->hw.workqueue, &local->sta_debugfs_add); 267 #endif 268 269 return sta; 270 } 271 272 static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid) 273 { 274 /* 275 * This format has been mandated by the IEEE specifications, 276 * so this line may not be changed to use the __set_bit() format. 277 */ 278 bss->tim[aid / 8] |= (1 << (aid % 8)); 279 } 280 281 static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid) 282 { 283 /* 284 * This format has been mandated by the IEEE specifications, 285 * so this line may not be changed to use the __clear_bit() format. 286 */ 287 bss->tim[aid / 8] &= ~(1 << (aid % 8)); 288 } 289 290 static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss, 291 struct sta_info *sta) 292 { 293 if (bss) 294 __bss_tim_set(bss, sta->aid); 295 if (sta->local->ops->set_tim) { 296 sta->local->tim_in_locked_section = true; 297 sta->local->ops->set_tim(local_to_hw(sta->local), sta->aid, 1); 298 sta->local->tim_in_locked_section = false; 299 } 300 } 301 302 void sta_info_set_tim_bit(struct sta_info *sta) 303 { 304 unsigned long flags; 305 306 spin_lock_irqsave(&sta->local->sta_lock, flags); 307 __sta_info_set_tim_bit(sta->sdata->bss, sta); 308 spin_unlock_irqrestore(&sta->local->sta_lock, flags); 309 } 310 311 static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss, 312 struct sta_info *sta) 313 { 314 if (bss) 315 __bss_tim_clear(bss, sta->aid); 316 if (sta->local->ops->set_tim) { 317 sta->local->tim_in_locked_section = true; 318 sta->local->ops->set_tim(local_to_hw(sta->local), sta->aid, 0); 319 sta->local->tim_in_locked_section = false; 320 } 321 } 322 323 void sta_info_clear_tim_bit(struct sta_info *sta) 324 { 325 unsigned long flags; 326 327 spin_lock_irqsave(&sta->local->sta_lock, flags); 328 __sta_info_clear_tim_bit(sta->sdata->bss, sta); 329 spin_unlock_irqrestore(&sta->local->sta_lock, flags); 330 } 331 332 /* 333 * See comment in __sta_info_unlink, 334 * caller must hold local->sta_lock. 335 */ 336 static void __sta_info_pin(struct sta_info *sta) 337 { 338 WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL); 339 sta->pin_status = STA_INFO_PIN_STAT_PINNED; 340 } 341 342 /* 343 * See comment in __sta_info_unlink, returns sta if it 344 * needs to be destroyed. 345 */ 346 static struct sta_info *__sta_info_unpin(struct sta_info *sta) 347 { 348 struct sta_info *ret = NULL; 349 unsigned long flags; 350 351 spin_lock_irqsave(&sta->local->sta_lock, flags); 352 WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY && 353 sta->pin_status != STA_INFO_PIN_STAT_PINNED); 354 if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY) 355 ret = sta; 356 sta->pin_status = STA_INFO_PIN_STAT_NORMAL; 357 spin_unlock_irqrestore(&sta->local->sta_lock, flags); 358 359 return ret; 360 } 361 362 static void __sta_info_unlink(struct sta_info **sta) 363 { 364 struct ieee80211_local *local = (*sta)->local; 365 struct ieee80211_sub_if_data *sdata = (*sta)->sdata; 366 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 367 DECLARE_MAC_BUF(mbuf); 368 #endif 369 /* 370 * pull caller's reference if we're already gone. 371 */ 372 if (sta_info_hash_del(local, *sta)) { 373 *sta = NULL; 374 return; 375 } 376 377 /* 378 * Also pull caller's reference if the STA is pinned by the 379 * task that is adding the debugfs entries. In that case, we 380 * leave the STA "to be freed". 381 * 382 * The rules are not trivial, but not too complex either: 383 * (1) pin_status is only modified under the sta_lock 384 * (2) sta_info_debugfs_add_work() will set the status 385 * to PINNED when it found an item that needs a new 386 * debugfs directory created. In that case, that item 387 * must not be freed although all *RCU* users are done 388 * with it. Hence, we tell the caller of _unlink() 389 * that the item is already gone (as can happen when 390 * two tasks try to unlink/destroy at the same time) 391 * (3) We set the pin_status to DESTROY here when we 392 * find such an item. 393 * (4) sta_info_debugfs_add_work() will reset the pin_status 394 * from PINNED to NORMAL when it is done with the item, 395 * but will check for DESTROY before resetting it in 396 * which case it will free the item. 397 */ 398 if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) { 399 (*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY; 400 *sta = NULL; 401 return; 402 } 403 404 list_del(&(*sta)->list); 405 406 if ((*sta)->flags & WLAN_STA_PS) { 407 (*sta)->flags &= ~WLAN_STA_PS; 408 if (sdata->bss) 409 atomic_dec(&sdata->bss->num_sta_ps); 410 __sta_info_clear_tim_bit(sdata->bss, *sta); 411 } 412 413 local->num_sta--; 414 415 if (local->ops->sta_notify) { 416 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN) 417 sdata = sdata->u.vlan.ap; 418 419 local->ops->sta_notify(local_to_hw(local), &sdata->vif, 420 STA_NOTIFY_REMOVE, (*sta)->addr); 421 } 422 423 if (ieee80211_vif_is_mesh(&sdata->vif)) { 424 mesh_accept_plinks_update(sdata); 425 #ifdef CONFIG_MAC80211_MESH 426 del_timer(&(*sta)->plink_timer); 427 #endif 428 } 429 430 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 431 printk(KERN_DEBUG "%s: Removed STA %s\n", 432 wiphy_name(local->hw.wiphy), print_mac(mbuf, (*sta)->addr)); 433 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 434 } 435 436 void sta_info_unlink(struct sta_info **sta) 437 { 438 struct ieee80211_local *local = (*sta)->local; 439 unsigned long flags; 440 441 spin_lock_irqsave(&local->sta_lock, flags); 442 __sta_info_unlink(sta); 443 spin_unlock_irqrestore(&local->sta_lock, flags); 444 } 445 446 static inline int sta_info_buffer_expired(struct ieee80211_local *local, 447 struct sta_info *sta, 448 struct sk_buff *skb) 449 { 450 struct ieee80211_tx_packet_data *pkt_data; 451 int timeout; 452 453 if (!skb) 454 return 0; 455 456 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb; 457 458 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 459 timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 / 460 15625) * HZ; 461 if (timeout < STA_TX_BUFFER_EXPIRE) 462 timeout = STA_TX_BUFFER_EXPIRE; 463 return time_after(jiffies, pkt_data->jiffies + timeout); 464 } 465 466 467 static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 468 struct sta_info *sta) 469 { 470 unsigned long flags; 471 struct sk_buff *skb; 472 struct ieee80211_sub_if_data *sdata; 473 DECLARE_MAC_BUF(mac); 474 475 if (skb_queue_empty(&sta->ps_tx_buf)) 476 return; 477 478 for (;;) { 479 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags); 480 skb = skb_peek(&sta->ps_tx_buf); 481 if (sta_info_buffer_expired(local, sta, skb)) 482 skb = __skb_dequeue(&sta->ps_tx_buf); 483 else 484 skb = NULL; 485 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags); 486 487 if (!skb) 488 break; 489 490 sdata = sta->sdata; 491 local->total_ps_buffered--; 492 printk(KERN_DEBUG "Buffered frame expired (STA " 493 "%s)\n", print_mac(mac, sta->addr)); 494 dev_kfree_skb(skb); 495 496 if (skb_queue_empty(&sta->ps_tx_buf)) 497 sta_info_clear_tim_bit(sta); 498 } 499 } 500 501 502 static void sta_info_cleanup(unsigned long data) 503 { 504 struct ieee80211_local *local = (struct ieee80211_local *) data; 505 struct sta_info *sta; 506 507 rcu_read_lock(); 508 list_for_each_entry_rcu(sta, &local->sta_list, list) 509 sta_info_cleanup_expire_buffered(local, sta); 510 rcu_read_unlock(); 511 512 local->sta_cleanup.expires = 513 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL); 514 add_timer(&local->sta_cleanup); 515 } 516 517 #ifdef CONFIG_MAC80211_DEBUGFS 518 static void sta_info_debugfs_add_work(struct work_struct *work) 519 { 520 struct ieee80211_local *local = 521 container_of(work, struct ieee80211_local, sta_debugfs_add); 522 struct sta_info *sta, *tmp; 523 unsigned long flags; 524 525 while (1) { 526 sta = NULL; 527 528 spin_lock_irqsave(&local->sta_lock, flags); 529 list_for_each_entry(tmp, &local->sta_list, list) { 530 if (!tmp->debugfs.dir) { 531 sta = tmp; 532 __sta_info_pin(sta); 533 break; 534 } 535 } 536 spin_unlock_irqrestore(&local->sta_lock, flags); 537 538 if (!sta) 539 break; 540 541 ieee80211_sta_debugfs_add(sta); 542 rate_control_add_sta_debugfs(sta); 543 544 sta = __sta_info_unpin(sta); 545 546 if (sta) { 547 synchronize_rcu(); 548 sta_info_destroy(sta); 549 } 550 } 551 } 552 #endif 553 554 void sta_info_init(struct ieee80211_local *local) 555 { 556 spin_lock_init(&local->sta_lock); 557 INIT_LIST_HEAD(&local->sta_list); 558 559 setup_timer(&local->sta_cleanup, sta_info_cleanup, 560 (unsigned long)local); 561 local->sta_cleanup.expires = 562 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL); 563 564 #ifdef CONFIG_MAC80211_DEBUGFS 565 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work); 566 #endif 567 } 568 569 int sta_info_start(struct ieee80211_local *local) 570 { 571 add_timer(&local->sta_cleanup); 572 return 0; 573 } 574 575 void sta_info_stop(struct ieee80211_local *local) 576 { 577 del_timer(&local->sta_cleanup); 578 sta_info_flush(local, NULL); 579 } 580 581 /** 582 * sta_info_flush - flush matching STA entries from the STA table 583 * @local: local interface data 584 * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs 585 */ 586 void sta_info_flush(struct ieee80211_local *local, 587 struct ieee80211_sub_if_data *sdata) 588 { 589 struct sta_info *sta, *tmp; 590 LIST_HEAD(tmp_list); 591 unsigned long flags; 592 593 might_sleep(); 594 595 spin_lock_irqsave(&local->sta_lock, flags); 596 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 597 if (!sdata || sdata == sta->sdata) { 598 __sta_info_unlink(&sta); 599 if (sta) 600 list_add_tail(&sta->list, &tmp_list); 601 } 602 } 603 spin_unlock_irqrestore(&local->sta_lock, flags); 604 605 synchronize_rcu(); 606 607 list_for_each_entry_safe(sta, tmp, &tmp_list, list) 608 sta_info_destroy(sta); 609 } 610