00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <linux/module.h>
00011 #include <linux/init.h>
00012 #include <linux/netdevice.h>
00013 #include <linux/types.h>
00014 #include <linux/slab.h>
00015 #include <linux/skbuff.h>
00016 #include <linux/if_arp.h>
00017 #include <linux/timer.h>
00018 #include <linux/rtnetlink.h>
00019
00020 #include <net/mac80211.h>
00021 #include "ieee80211_i.h"
00022 #include "driver-ops.h"
00023 #include "rate.h"
00024 #include "sta_info.h"
00025 #include "debugfs_sta.h"
00026 #include "mesh.h"
00027 #include "wme.h"
00028
00065
00066 static int sta_info_hash_del(struct ieee80211_local *local,
00067 struct sta_info *sta)
00068 {
00069 struct sta_info *s;
00070
00071 s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
00072 lockdep_is_held(&local->sta_lock));
00073 if (!s)
00074 return -ENOENT;
00075 if (s == sta) {
00076 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
00077 s->hnext);
00078 return 0;
00079 }
00080
00081 while (rcu_access_pointer(s->hnext) &&
00082 rcu_access_pointer(s->hnext) != sta)
00083 s = rcu_dereference_protected(s->hnext,
00084 lockdep_is_held(&local->sta_lock));
00085 if (rcu_access_pointer(s->hnext)) {
00086 rcu_assign_pointer(s->hnext, sta->hnext);
00087 return 0;
00088 }
00089
00090 return -ENOENT;
00091 }
00092
00093
00094 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
00095 const u8 *addr)
00096 {
00097 struct ieee80211_local *local = sdata->local;
00098 struct sta_info *sta;
00099
00100 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
00101 lockdep_is_held(&local->sta_lock) ||
00102 lockdep_is_held(&local->sta_mtx));
00103 while (sta) {
00104 if (sta->sdata == sdata && !sta->dummy &&
00105 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
00106 break;
00107 sta = rcu_dereference_check(sta->hnext,
00108 lockdep_is_held(&local->sta_lock) ||
00109 lockdep_is_held(&local->sta_mtx));
00110 }
00111 return sta;
00112 }
00113
00114
00115 struct sta_info *sta_info_get_rx(struct ieee80211_sub_if_data *sdata,
00116 const u8 *addr)
00117 {
00118 struct ieee80211_local *local = sdata->local;
00119 struct sta_info *sta;
00120
00121 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
00122 lockdep_is_held(&local->sta_lock) ||
00123 lockdep_is_held(&local->sta_mtx));
00124 while (sta) {
00125 if (sta->sdata == sdata &&
00126 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
00127 break;
00128 sta = rcu_dereference_check(sta->hnext,
00129 lockdep_is_held(&local->sta_lock) ||
00130 lockdep_is_held(&local->sta_mtx));
00131 }
00132 return sta;
00133 }
00134
00135
00136
00137
00138
00139 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
00140 const u8 *addr)
00141 {
00142 struct ieee80211_local *local = sdata->local;
00143 struct sta_info *sta;
00144
00145 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
00146 lockdep_is_held(&local->sta_lock) ||
00147 lockdep_is_held(&local->sta_mtx));
00148 while (sta) {
00149 if ((sta->sdata == sdata ||
00150 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
00151 !sta->dummy &&
00152 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
00153 break;
00154 sta = rcu_dereference_check(sta->hnext,
00155 lockdep_is_held(&local->sta_lock) ||
00156 lockdep_is_held(&local->sta_mtx));
00157 }
00158 return sta;
00159 }
00160
00161
00162
00163
00164
00165 struct sta_info *sta_info_get_bss_rx(struct ieee80211_sub_if_data *sdata,
00166 const u8 *addr)
00167 {
00168 struct ieee80211_local *local = sdata->local;
00169 struct sta_info *sta;
00170
00171 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
00172 lockdep_is_held(&local->sta_lock) ||
00173 lockdep_is_held(&local->sta_mtx));
00174 while (sta) {
00175 if ((sta->sdata == sdata ||
00176 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
00177 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
00178 break;
00179 sta = rcu_dereference_check(sta->hnext,
00180 lockdep_is_held(&local->sta_lock) ||
00181 lockdep_is_held(&local->sta_mtx));
00182 }
00183 return sta;
00184 }
00185
00186 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
00187 int idx)
00188 {
00189 struct ieee80211_local *local = sdata->local;
00190 struct sta_info *sta;
00191 int i = 0;
00192
00193 list_for_each_entry_rcu(sta, &local->sta_list, list) {
00194 if (sdata != sta->sdata)
00195 continue;
00196 if (i < idx) {
00197 ++i;
00198 continue;
00199 }
00200 return sta;
00201 }
00202
00203 return NULL;
00204 }
00205
00215 static void __sta_info_free(struct ieee80211_local *local,
00216 struct sta_info *sta)
00217 {
00218 if (sta->rate_ctrl) {
00219 rate_control_free_sta(sta);
00220 rate_control_put(sta->rate_ctrl);
00221 }
00222
00223 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
00224 wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr);
00225 #endif
00226
00227 kfree(sta);
00228 }
00229
00230
00231 static void sta_info_hash_add(struct ieee80211_local *local,
00232 struct sta_info *sta)
00233 {
00234 sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
00235 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
00236 }
00237
00238 static void sta_unblock(struct work_struct *wk)
00239 {
00240 struct sta_info *sta;
00241
00242 sta = container_of(wk, struct sta_info, drv_unblock_wk);
00243
00244 if (sta->dead)
00245 return;
00246
00247 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
00248 ieee80211_sta_ps_deliver_wakeup(sta);
00249 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
00250 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
00251
00252 local_bh_disable();
00253 ieee80211_sta_ps_deliver_poll_response(sta);
00254 local_bh_enable();
00255 } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
00256 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
00257
00258 local_bh_disable();
00259 ieee80211_sta_ps_deliver_uapsd(sta);
00260 local_bh_enable();
00261 } else
00262 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
00263 }
00264
00265 static int sta_prepare_rate_control(struct ieee80211_local *local,
00266 struct sta_info *sta, gfp_t gfp)
00267 {
00268 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
00269 return 0;
00270
00271 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
00272 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
00273 &sta->sta, gfp);
00274 if (!sta->rate_ctrl_priv) {
00275 rate_control_put(sta->rate_ctrl);
00276 return -ENOMEM;
00277 }
00278
00279 return 0;
00280 }
00281
00282 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
00283 u8 *addr, gfp_t gfp)
00284 {
00285 struct ieee80211_local *local = sdata->local;
00286 struct sta_info *sta;
00287 struct timespec uptime;
00288 int i;
00289
00290 sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
00291 if (!sta)
00292 return NULL;
00293
00294 spin_lock_init(&sta->lock);
00295 INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
00296 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
00297 mutex_init(&sta->ampdu_mlme.mtx);
00298
00299 memcpy(sta->sta.addr, addr, ETH_ALEN);
00300 sta->local = local;
00301 sta->sdata = sdata;
00302 sta->last_rx = jiffies;
00303
00304 do_posix_clock_monotonic_gettime(&uptime);
00305 sta->last_connected = uptime.tv_sec;
00306 ewma_init(&sta->avg_signal, 1024, 8);
00307
00308 if (sta_prepare_rate_control(local, sta, gfp)) {
00309 kfree(sta);
00310 return NULL;
00311 }
00312
00313 for (i = 0; i < STA_TID_NUM; i++) {
00314
00315
00316
00317
00318
00319 sta->timer_to_tid[i] = i;
00320 }
00321 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
00322 skb_queue_head_init(&sta->ps_tx_buf[i]);
00323 skb_queue_head_init(&sta->tx_filtered[i]);
00324 }
00325
00326 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
00327 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
00328
00329 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
00330 wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
00331 #endif
00332
00333 #ifdef CONFIG_MAC80211_MESH
00334 sta->plink_state = NL80211_PLINK_LISTEN;
00335 init_timer(&sta->plink_timer);
00336 #endif
00337
00338 return sta;
00339 }
00340
00341 static int sta_info_finish_insert(struct sta_info *sta,
00342 bool async, bool dummy_reinsert)
00343 {
00344 struct ieee80211_local *local = sta->local;
00345 struct ieee80211_sub_if_data *sdata = sta->sdata;
00346 struct station_info sinfo;
00347 unsigned long flags;
00348 int err = 0;
00349
00350 lockdep_assert_held(&local->sta_mtx);
00351
00352 if (!sta->dummy || dummy_reinsert) {
00353
00354 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
00355 sdata = container_of(sdata->bss,
00356 struct ieee80211_sub_if_data,
00357 u.ap);
00358 err = drv_sta_add(local, sdata, &sta->sta);
00359 if (err) {
00360 if (!async)
00361 return err;
00362 printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to "
00363 "driver (%d) - keeping it anyway.\n",
00364 sdata->name, sta->sta.addr, err);
00365 } else {
00366 sta->uploaded = true;
00367 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
00368 if (async)
00369 wiphy_debug(local->hw.wiphy,
00370 "Finished adding IBSS STA %pM\n",
00371 sta->sta.addr);
00372 #endif
00373 }
00374
00375 sdata = sta->sdata;
00376 }
00377
00378 if (!dummy_reinsert) {
00379 if (!async) {
00380 local->num_sta++;
00381 local->sta_generation++;
00382 smp_mb();
00383
00384
00385 spin_lock_irqsave(&local->sta_lock, flags);
00386 sta_info_hash_add(local, sta);
00387 spin_unlock_irqrestore(&local->sta_lock, flags);
00388 }
00389
00390 list_add(&sta->list, &local->sta_list);
00391 } else {
00392 sta->dummy = false;
00393 }
00394
00395 if (!sta->dummy) {
00396 ieee80211_sta_debugfs_add(sta);
00397 rate_control_add_sta_debugfs(sta);
00398
00399 memset(&sinfo, 0, sizeof(sinfo));
00400 sinfo.filled = 0;
00401 sinfo.generation = local->sta_generation;
00402 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
00403 }
00404
00405 return 0;
00406 }
00407
00408 static void sta_info_finish_pending(struct ieee80211_local *local)
00409 {
00410 struct sta_info *sta;
00411 unsigned long flags;
00412
00413 spin_lock_irqsave(&local->sta_lock, flags);
00414 while (!list_empty(&local->sta_pending_list)) {
00415 sta = list_first_entry(&local->sta_pending_list,
00416 struct sta_info, list);
00417 list_del(&sta->list);
00418 spin_unlock_irqrestore(&local->sta_lock, flags);
00419
00420 sta_info_finish_insert(sta, true, false);
00421
00422 spin_lock_irqsave(&local->sta_lock, flags);
00423 }
00424 spin_unlock_irqrestore(&local->sta_lock, flags);
00425 }
00426
00427 static void sta_info_finish_work(struct work_struct *work)
00428 {
00429 struct ieee80211_local *local =
00430 container_of(work, struct ieee80211_local, sta_finish_work);
00431
00432 mutex_lock(&local->sta_mtx);
00433 sta_info_finish_pending(local);
00434 mutex_unlock(&local->sta_mtx);
00435 }
00436
00437 static int sta_info_insert_check(struct sta_info *sta)
00438 {
00439 struct ieee80211_sub_if_data *sdata = sta->sdata;
00440
00441
00442
00443
00444
00445
00446 if (unlikely(!ieee80211_sdata_running(sdata)))
00447 return -ENETDOWN;
00448
00449 if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
00450 is_multicast_ether_addr(sta->sta.addr)))
00451 return -EINVAL;
00452
00453 return 0;
00454 }
00455
00456 static int sta_info_insert_ibss(struct sta_info *sta) __acquires(RCU)
00457 {
00458 struct ieee80211_local *local = sta->local;
00459 struct ieee80211_sub_if_data *sdata = sta->sdata;
00460 unsigned long flags;
00461
00462 spin_lock_irqsave(&local->sta_lock, flags);
00463
00464 if (sta_info_get_bss_rx(sdata, sta->sta.addr)) {
00465 spin_unlock_irqrestore(&local->sta_lock, flags);
00466 rcu_read_lock();
00467 return -EEXIST;
00468 }
00469
00470 local->num_sta++;
00471 local->sta_generation++;
00472 smp_mb();
00473 sta_info_hash_add(local, sta);
00474
00475 list_add_tail(&sta->list, &local->sta_pending_list);
00476
00477 rcu_read_lock();
00478 spin_unlock_irqrestore(&local->sta_lock, flags);
00479
00480 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
00481 wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n",
00482 sta->sta.addr);
00483 #endif
00484
00485 ieee80211_queue_work(&local->hw, &local->sta_finish_work);
00486
00487 return 0;
00488 }
00489
00490
00491
00492
00493
00494
00495 static int sta_info_insert_non_ibss(struct sta_info *sta) __acquires(RCU)
00496 {
00497 struct ieee80211_local *local = sta->local;
00498 struct ieee80211_sub_if_data *sdata = sta->sdata;
00499 unsigned long flags;
00500 struct sta_info *exist_sta;
00501 bool dummy_reinsert = false;
00502 int err = 0;
00503
00504 lockdep_assert_held(&local->sta_mtx);
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516 spin_lock_irqsave(&local->sta_lock, flags);
00517
00518
00519
00520
00521
00522
00523
00524 exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr);
00525 if (exist_sta) {
00526 if (exist_sta == sta && sta->dummy) {
00527 dummy_reinsert = true;
00528 } else {
00529 spin_unlock_irqrestore(&local->sta_lock, flags);
00530 mutex_unlock(&local->sta_mtx);
00531 rcu_read_lock();
00532 return -EEXIST;
00533 }
00534 }
00535
00536 spin_unlock_irqrestore(&local->sta_lock, flags);
00537
00538 err = sta_info_finish_insert(sta, false, dummy_reinsert);
00539 if (err) {
00540 mutex_unlock(&local->sta_mtx);
00541 rcu_read_lock();
00542 return err;
00543 }
00544
00545 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
00546 wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n",
00547 sta->dummy ? "dummy " : "", sta->sta.addr);
00548 #endif
00549
00550
00551 rcu_read_lock();
00552 mutex_unlock(&local->sta_mtx);
00553
00554 if (ieee80211_vif_is_mesh(&sdata->vif))
00555 mesh_accept_plinks_update(sdata);
00556
00557 return 0;
00558 }
00559
00560 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
00561 {
00562 struct ieee80211_local *local = sta->local;
00563 struct ieee80211_sub_if_data *sdata = sta->sdata;
00564 int err = 0;
00565
00566 err = sta_info_insert_check(sta);
00567 if (err) {
00568 rcu_read_lock();
00569 goto out_free;
00570 }
00571
00572
00573
00574
00575
00576
00577 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
00578 err = sta_info_insert_ibss(sta);
00579 if (err)
00580 goto out_free;
00581
00582 return 0;
00583 }
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594 might_sleep();
00595
00596 mutex_lock(&local->sta_mtx);
00597
00598 err = sta_info_insert_non_ibss(sta);
00599 if (err)
00600 goto out_free;
00601
00602 return 0;
00603 out_free:
00604 BUG_ON(!err);
00605 __sta_info_free(local, sta);
00606 return err;
00607 }
00608
00609 int sta_info_insert(struct sta_info *sta)
00610 {
00611 int err = sta_info_insert_rcu(sta);
00612
00613 rcu_read_unlock();
00614
00615 return err;
00616 }
00617
00618
00619 int sta_info_reinsert(struct sta_info *sta)
00620 {
00621 struct ieee80211_local *local = sta->local;
00622 int err = 0;
00623
00624 err = sta_info_insert_check(sta);
00625 if (err) {
00626 mutex_unlock(&local->sta_mtx);
00627 return err;
00628 }
00629
00630 might_sleep();
00631
00632 err = sta_info_insert_non_ibss(sta);
00633 rcu_read_unlock();
00634 return err;
00635 }
00636
00637 static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
00638 {
00639
00640
00641
00642
00643 bss->tim[aid / 8] |= (1 << (aid % 8));
00644 }
00645
00646 static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
00647 {
00648
00649
00650
00651
00652 bss->tim[aid / 8] &= ~(1 << (aid % 8));
00653 }
00654
00655 static unsigned long ieee80211_tids_for_ac(int ac)
00656 {
00657
00658 switch (ac) {
00659 case IEEE80211_AC_VO:
00660 return BIT(6) | BIT(7);
00661 case IEEE80211_AC_VI:
00662 return BIT(4) | BIT(5);
00663 case IEEE80211_AC_BE:
00664 return BIT(0) | BIT(3);
00665 case IEEE80211_AC_BK:
00666 return BIT(1) | BIT(2);
00667 default:
00668 WARN_ON(1);
00669 return 0;
00670 }
00671 }
00672
00673 void sta_info_recalc_tim(struct sta_info *sta)
00674 {
00675 struct ieee80211_local *local = sta->local;
00676 struct ieee80211_if_ap *bss = sta->sdata->bss;
00677 unsigned long flags;
00678 bool indicate_tim = false;
00679 u8 ignore_for_tim = sta->sta.uapsd_queues;
00680 int ac;
00681
00682 if (WARN_ON_ONCE(!sta->sdata->bss))
00683 return;
00684
00685
00686 if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
00687 return;
00688
00689 if (sta->dead)
00690 goto done;
00691
00692
00693
00694
00695
00696
00697
00698 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
00699 ignore_for_tim = 0;
00700
00701 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
00702 unsigned long tids;
00703
00704 if (ignore_for_tim & BIT(ac))
00705 continue;
00706
00707 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
00708 !skb_queue_empty(&sta->ps_tx_buf[ac]);
00709 if (indicate_tim)
00710 break;
00711
00712 tids = ieee80211_tids_for_ac(ac);
00713
00714 indicate_tim |=
00715 sta->driver_buffered_tids & tids;
00716 }
00717
00718 done:
00719 spin_lock_irqsave(&local->sta_lock, flags);
00720
00721 if (indicate_tim)
00722 __bss_tim_set(bss, sta->sta.aid);
00723 else
00724 __bss_tim_clear(bss, sta->sta.aid);
00725
00726 if (local->ops->set_tim) {
00727 local->tim_in_locked_section = true;
00728 drv_set_tim(local, &sta->sta, indicate_tim);
00729 local->tim_in_locked_section = false;
00730 }
00731
00732 spin_unlock_irqrestore(&local->sta_lock, flags);
00733 }
00734
00735 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
00736 {
00737 struct ieee80211_tx_info *info;
00738 int timeout;
00739
00740 if (!skb)
00741 return false;
00742
00743 info = IEEE80211_SKB_CB(skb);
00744
00745
00746 timeout = (sta->listen_interval *
00747 sta->sdata->vif.bss_conf.beacon_int *
00748 32 / 15625) * HZ;
00749 if (timeout < STA_TX_BUFFER_EXPIRE)
00750 timeout = STA_TX_BUFFER_EXPIRE;
00751 return time_after(jiffies, info->control.jiffies + timeout);
00752 }
00753
00754
00755 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
00756 struct sta_info *sta, int ac)
00757 {
00758 unsigned long flags;
00759 struct sk_buff *skb;
00760
00761
00762
00763
00764
00765
00766
00767
00768 for (;;) {
00769 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
00770 skb = skb_peek(&sta->tx_filtered[ac]);
00771 if (sta_info_buffer_expired(sta, skb))
00772 skb = __skb_dequeue(&sta->tx_filtered[ac]);
00773 else
00774 skb = NULL;
00775 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
00776
00777
00778
00779
00780
00781
00782
00783 if (!skb)
00784 break;
00785 dev_kfree_skb(skb);
00786 }
00787
00788
00789
00790
00791
00792
00793
00794 for (;;) {
00795 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
00796 skb = skb_peek(&sta->ps_tx_buf[ac]);
00797 if (sta_info_buffer_expired(sta, skb))
00798 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
00799 else
00800 skb = NULL;
00801 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
00802
00803
00804
00805
00806
00807
00808 if (!skb)
00809 break;
00810
00811 local->total_ps_buffered--;
00812 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
00813 printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
00814 sta->sta.addr);
00815 #endif
00816 dev_kfree_skb(skb);
00817 }
00818
00819
00820
00821
00822
00823
00824 sta_info_recalc_tim(sta);
00825
00826
00827
00828
00829
00830
00831 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
00832 skb_queue_empty(&sta->tx_filtered[ac]));
00833 }
00834
00835 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
00836 struct sta_info *sta)
00837 {
00838 bool have_buffered = false;
00839 int ac;
00840
00841
00842 if (!sta->sdata->bss)
00843 return false;
00844
00845 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
00846 have_buffered |=
00847 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
00848
00849 return have_buffered;
00850 }
00851
00852 static int __must_check __sta_info_destroy(struct sta_info *sta)
00853 {
00854 struct ieee80211_local *local;
00855 struct ieee80211_sub_if_data *sdata;
00856 unsigned long flags;
00857 int ret, i, ac;
00858
00859 might_sleep();
00860
00861 if (!sta)
00862 return -ENOENT;
00863
00864 local = sta->local;
00865 sdata = sta->sdata;
00866
00867
00868
00869
00870
00871
00872
00873 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
00874 ieee80211_sta_tear_down_BA_sessions(sta, true);
00875
00876 spin_lock_irqsave(&local->sta_lock, flags);
00877 ret = sta_info_hash_del(local, sta);
00878
00879 if (!ret)
00880 list_del(&sta->list);
00881 spin_unlock_irqrestore(&local->sta_lock, flags);
00882 if (ret)
00883 return ret;
00884
00885 mutex_lock(&local->key_mtx);
00886 for (i = 0; i < NUM_DEFAULT_KEYS; i++)
00887 __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
00888 if (sta->ptk)
00889 __ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
00890 mutex_unlock(&local->key_mtx);
00891
00892 sta->dead = true;
00893
00894 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
00895 test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
00896 BUG_ON(!sdata->bss);
00897
00898 clear_sta_flag(sta, WLAN_STA_PS_STA);
00899 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
00900
00901 atomic_dec(&sdata->bss->num_sta_ps);
00902 sta_info_recalc_tim(sta);
00903 }
00904
00905 local->num_sta--;
00906 local->sta_generation++;
00907
00908 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
00909 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
00910
00911 if (sta->uploaded) {
00912 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
00913 sdata = container_of(sdata->bss,
00914 struct ieee80211_sub_if_data,
00915 u.ap);
00916 drv_sta_remove(local, sdata, &sta->sta);
00917 sdata = sta->sdata;
00918 }
00919
00920
00921
00922
00923
00924
00925
00926 synchronize_rcu();
00927
00928 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
00929 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
00930 __skb_queue_purge(&sta->ps_tx_buf[ac]);
00931 __skb_queue_purge(&sta->tx_filtered[ac]);
00932 }
00933
00934 #ifdef CONFIG_MAC80211_MESH
00935 if (ieee80211_vif_is_mesh(&sdata->vif))
00936 mesh_accept_plinks_update(sdata);
00937 #endif
00938
00939 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
00940 wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
00941 #endif
00942 cancel_work_sync(&sta->drv_unblock_wk);
00943
00944 cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
00945
00946 rate_control_remove_sta_debugfs(sta);
00947 ieee80211_sta_debugfs_remove(sta);
00948
00949 #ifdef CONFIG_MAC80211_MESH
00950 if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
00951 mesh_plink_deactivate(sta);
00952 del_timer_sync(&sta->plink_timer);
00953 }
00954 #endif
00955
00956 __sta_info_free(local, sta);
00957
00958 return 0;
00959 }
00960
00961 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
00962 {
00963 struct sta_info *sta;
00964 int ret;
00965
00966 mutex_lock(&sdata->local->sta_mtx);
00967 sta = sta_info_get_rx(sdata, addr);
00968 ret = __sta_info_destroy(sta);
00969 mutex_unlock(&sdata->local->sta_mtx);
00970
00971 return ret;
00972 }
00973
00974 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
00975 const u8 *addr)
00976 {
00977 struct sta_info *sta;
00978 int ret;
00979
00980 mutex_lock(&sdata->local->sta_mtx);
00981 sta = sta_info_get_bss_rx(sdata, addr);
00982 ret = __sta_info_destroy(sta);
00983 mutex_unlock(&sdata->local->sta_mtx);
00984
00985 return ret;
00986 }
00987
00988 static void sta_info_cleanup(unsigned long data)
00989 {
00990 struct ieee80211_local *local = (struct ieee80211_local *) data;
00991 struct sta_info *sta;
00992 bool timer_needed = false;
00993
00994 rcu_read_lock();
00995 list_for_each_entry_rcu(sta, &local->sta_list, list)
00996 if (sta_info_cleanup_expire_buffered(local, sta))
00997 timer_needed = true;
00998 rcu_read_unlock();
00999
01000 if (local->quiescing)
01001 return;
01002
01003 if (!timer_needed)
01004 return;
01005
01006 mod_timer(&local->sta_cleanup,
01007 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
01008 }
01009
01010 void sta_info_init(struct ieee80211_local *local)
01011 {
01012 spin_lock_init(&local->sta_lock);
01013 mutex_init(&local->sta_mtx);
01014 INIT_LIST_HEAD(&local->sta_list);
01015 INIT_LIST_HEAD(&local->sta_pending_list);
01016 INIT_WORK(&local->sta_finish_work, sta_info_finish_work);
01017
01018 setup_timer(&local->sta_cleanup, sta_info_cleanup,
01019 (unsigned long)local);
01020 }
01021
01022 void sta_info_stop(struct ieee80211_local *local)
01023 {
01024 del_timer(&local->sta_cleanup);
01025 sta_info_flush(local, NULL);
01026 }
01027
01036 int sta_info_flush(struct ieee80211_local *local,
01037 struct ieee80211_sub_if_data *sdata)
01038 {
01039 struct sta_info *sta, *tmp;
01040 int ret = 0;
01041
01042 might_sleep();
01043
01044 mutex_lock(&local->sta_mtx);
01045
01046 sta_info_finish_pending(local);
01047
01048 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
01049 if (!sdata || sdata == sta->sdata)
01050 WARN_ON(__sta_info_destroy(sta));
01051 }
01052 mutex_unlock(&local->sta_mtx);
01053
01054 return ret;
01055 }
01056
01057 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
01058 unsigned long exp_time)
01059 {
01060 struct ieee80211_local *local = sdata->local;
01061 struct sta_info *sta, *tmp;
01062
01063 mutex_lock(&local->sta_mtx);
01064 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
01065 if (time_after(jiffies, sta->last_rx + exp_time)) {
01066 #ifdef CONFIG_MAC80211_IBSS_DEBUG
01067 printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
01068 sdata->name, sta->sta.addr);
01069 #endif
01070 WARN_ON(__sta_info_destroy(sta));
01071 }
01072 mutex_unlock(&local->sta_mtx);
01073 }
01074
01075 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
01076 const u8 *addr,
01077 const u8 *localaddr)
01078 {
01079 struct sta_info *sta, *nxt;
01080
01081
01082
01083
01084
01085 for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
01086 if (localaddr &&
01087 compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
01088 continue;
01089 if (!sta->uploaded)
01090 return NULL;
01091 return &sta->sta;
01092 }
01093
01094 return NULL;
01095 }
01096 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
01097
01098 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
01099 const u8 *addr)
01100 {
01101 struct sta_info *sta;
01102
01103 if (!vif)
01104 return NULL;
01105
01106 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
01107 if (!sta)
01108 return NULL;
01109
01110 if (!sta->uploaded)
01111 return NULL;
01112
01113 return &sta->sta;
01114 }
01115 EXPORT_SYMBOL(ieee80211_find_sta);
01116
01117 static void clear_sta_ps_flags(void *_sta)
01118 {
01119 struct sta_info *sta = _sta;
01120
01121 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
01122 clear_sta_flag(sta, WLAN_STA_PS_STA);
01123 }
01124
01125
01126 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
01127 {
01128 struct ieee80211_sub_if_data *sdata = sta->sdata;
01129 struct ieee80211_local *local = sdata->local;
01130 struct sk_buff_head pending;
01131 int filtered = 0, buffered = 0, ac;
01132
01133 clear_sta_flag(sta, WLAN_STA_SP);
01134
01135 BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1);
01136 sta->driver_buffered_tids = 0;
01137
01138 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
01139 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
01140
01141 skb_queue_head_init(&pending);
01142
01143
01144 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
01145 int count = skb_queue_len(&pending), tmp;
01146
01147 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
01148 tmp = skb_queue_len(&pending);
01149 filtered += tmp - count;
01150 count = tmp;
01151
01152 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
01153 tmp = skb_queue_len(&pending);
01154 buffered += tmp - count;
01155 }
01156
01157 ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
01158
01159 local->total_ps_buffered -= buffered;
01160
01161 sta_info_recalc_tim(sta);
01162
01163 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
01164 printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
01165 "since STA not sleeping anymore\n", sdata->name,
01166 sta->sta.addr, sta->sta.aid, filtered, buffered);
01167 #endif
01168 }
01169
01170 static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
01171 struct sta_info *sta, int tid,
01172 enum ieee80211_frame_release_type reason)
01173 {
01174 struct ieee80211_local *local = sdata->local;
01175 struct ieee80211_qos_hdr *nullfunc;
01176 struct sk_buff *skb;
01177 int size = sizeof(*nullfunc);
01178 __le16 fc;
01179 bool qos = test_sta_flag(sta, WLAN_STA_WME);
01180 struct ieee80211_tx_info *info;
01181
01182 if (qos) {
01183 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
01184 IEEE80211_STYPE_QOS_NULLFUNC |
01185 IEEE80211_FCTL_FROMDS);
01186 } else {
01187 size -= 2;
01188 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
01189 IEEE80211_STYPE_NULLFUNC |
01190 IEEE80211_FCTL_FROMDS);
01191 }
01192
01193 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
01194 if (!skb)
01195 return;
01196
01197 skb_reserve(skb, local->hw.extra_tx_headroom);
01198
01199 nullfunc = (void *) skb_put(skb, size);
01200 nullfunc->frame_control = fc;
01201 nullfunc->duration_id = 0;
01202 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
01203 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
01204 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
01205
01206 skb->priority = tid;
01207 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
01208 if (qos) {
01209 nullfunc->qos_ctrl = cpu_to_le16(tid);
01210
01211 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
01212 nullfunc->qos_ctrl |=
01213 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
01214 }
01215
01216 info = IEEE80211_SKB_CB(skb);
01217
01218
01219
01220
01221
01222
01223
01224 info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE |
01225 IEEE80211_TX_STATUS_EOSP |
01226 IEEE80211_TX_CTL_REQ_TX_STATUS;
01227
01228 drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
01229
01230 ieee80211_xmit(sdata, skb);
01231 }
01232
01233 static void
01234 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
01235 int n_frames, u8 ignored_acs,
01236 enum ieee80211_frame_release_type reason)
01237 {
01238 struct ieee80211_sub_if_data *sdata = sta->sdata;
01239 struct ieee80211_local *local = sdata->local;
01240 bool found = false;
01241 bool more_data = false;
01242 int ac;
01243 unsigned long driver_release_tids = 0;
01244 struct sk_buff_head frames;
01245
01246
01247 set_sta_flag(sta, WLAN_STA_SP);
01248
01249 __skb_queue_head_init(&frames);
01250
01251
01252
01253
01254 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
01255 unsigned long tids;
01256
01257 if (ignored_acs & BIT(ac))
01258 continue;
01259
01260 tids = ieee80211_tids_for_ac(ac);
01261
01262 if (!found) {
01263 driver_release_tids = sta->driver_buffered_tids & tids;
01264 if (driver_release_tids) {
01265 found = true;
01266 } else {
01267 struct sk_buff *skb;
01268
01269 while (n_frames > 0) {
01270 skb = skb_dequeue(&sta->tx_filtered[ac]);
01271 if (!skb) {
01272 skb = skb_dequeue(
01273 &sta->ps_tx_buf[ac]);
01274 if (skb)
01275 local->total_ps_buffered--;
01276 }
01277 if (!skb)
01278 break;
01279 n_frames--;
01280 found = true;
01281 __skb_queue_tail(&frames, skb);
01282 }
01283 }
01284
01285
01286
01287
01288
01289
01290 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
01291 hweight16(driver_release_tids) > 1) {
01292 more_data = true;
01293 driver_release_tids =
01294 BIT(ffs(driver_release_tids) - 1);
01295 break;
01296 }
01297 }
01298
01299 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
01300 !skb_queue_empty(&sta->ps_tx_buf[ac])) {
01301 more_data = true;
01302 break;
01303 }
01304 }
01305
01306 if (!found) {
01307 int tid;
01308
01309
01310
01311
01312
01313
01314
01315
01316
01317
01318
01319
01320
01321
01322
01323
01324
01325 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
01326
01327 ieee80211_send_null_response(sdata, sta, tid, reason);
01328 return;
01329 }
01330
01331 if (!driver_release_tids) {
01332 struct sk_buff_head pending;
01333 struct sk_buff *skb;
01334 int num = 0;
01335 u16 tids = 0;
01336
01337 skb_queue_head_init(&pending);
01338
01339 while ((skb = __skb_dequeue(&frames))) {
01340 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
01341 struct ieee80211_hdr *hdr = (void *) skb->data;
01342 u8 *qoshdr = NULL;
01343
01344 num++;
01345
01346
01347
01348
01349
01350
01351 info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE;
01352
01353
01354
01355
01356
01357 if (more_data || !skb_queue_empty(&frames))
01358 hdr->frame_control |=
01359 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
01360 else
01361 hdr->frame_control &=
01362 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
01363
01364 if (ieee80211_is_data_qos(hdr->frame_control) ||
01365 ieee80211_is_qos_nullfunc(hdr->frame_control))
01366 qoshdr = ieee80211_get_qos_ctl(hdr);
01367
01368
01369 if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
01370 qoshdr && skb_queue_empty(&frames))
01371 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
01372
01373 info->flags |= IEEE80211_TX_STATUS_EOSP |
01374 IEEE80211_TX_CTL_REQ_TX_STATUS;
01375
01376 if (qoshdr)
01377 tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
01378 else
01379 tids |= BIT(0);
01380
01381 __skb_queue_tail(&pending, skb);
01382 }
01383
01384 drv_allow_buffered_frames(local, sta, tids, num,
01385 reason, more_data);
01386
01387 ieee80211_add_pending_skbs(local, &pending);
01388
01389 sta_info_recalc_tim(sta);
01390 } else {
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401 drv_release_buffered_frames(local, sta, driver_release_tids,
01402 n_frames, reason, more_data);
01403
01404
01405
01406
01407
01408
01409
01410
01411
01412 }
01413 }
01414
01415 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
01416 {
01417 u8 ignore_for_response = sta->sta.uapsd_queues;
01418
01419
01420
01421
01422
01423
01424 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
01425 ignore_for_response = 0;
01426
01427 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
01428 IEEE80211_FRAME_RELEASE_PSPOLL);
01429 }
01430
01431 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
01432 {
01433 int n_frames = sta->sta.max_sp;
01434 u8 delivery_enabled = sta->sta.uapsd_queues;
01435
01436
01437
01438
01439
01440
01441
01442 if (!delivery_enabled)
01443 return;
01444
01445 switch (sta->sta.max_sp) {
01446 case 1:
01447 n_frames = 2;
01448 break;
01449 case 2:
01450 n_frames = 4;
01451 break;
01452 case 3:
01453 n_frames = 6;
01454 break;
01455 case 0:
01456
01457 n_frames = 8;
01458 break;
01459 }
01460
01461 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
01462 IEEE80211_FRAME_RELEASE_UAPSD);
01463 }
01464
01465 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
01466 struct ieee80211_sta *pubsta, bool block)
01467 {
01468 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
01469
01470 trace_api_sta_block_awake(sta->local, pubsta, block);
01471
01472 if (block)
01473 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
01474 else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
01475 ieee80211_queue_work(hw, &sta->drv_unblock_wk);
01476 }
01477 EXPORT_SYMBOL(ieee80211_sta_block_awake);
01478
01479 void ieee80211_sta_eosp_irqsafe(struct ieee80211_sta *pubsta)
01480 {
01481 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
01482 struct ieee80211_local *local = sta->local;
01483 struct sk_buff *skb;
01484 struct skb_eosp_msg_data *data;
01485
01486 trace_api_eosp(local, pubsta);
01487
01488 skb = alloc_skb(0, GFP_ATOMIC);
01489 if (!skb) {
01490
01491 clear_sta_flag(sta, WLAN_STA_SP);
01492 return;
01493 }
01494
01495 data = (void *)skb->cb;
01496 memcpy(data->sta, pubsta->addr, ETH_ALEN);
01497 memcpy(data->iface, sta->sdata->vif.addr, ETH_ALEN);
01498 skb->pkt_type = IEEE80211_EOSP_MSG;
01499 skb_queue_tail(&local->skb_queue, skb);
01500 tasklet_schedule(&local->tasklet);
01501 }
01502 EXPORT_SYMBOL(ieee80211_sta_eosp_irqsafe);
01503
01504 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
01505 u8 tid, bool buffered)
01506 {
01507 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
01508
01509 if (WARN_ON(tid >= STA_TID_NUM))
01510 return;
01511
01512 if (buffered)
01513 set_bit(tid, &sta->driver_buffered_tids);
01514 else
01515 clear_bit(tid, &sta->driver_buffered_tids);
01516
01517 sta_info_recalc_tim(sta);
01518 }
01519 EXPORT_SYMBOL(ieee80211_sta_set_buffered);