sta_info.c
Go to the documentation of this file.
00001 /*
00002  * Copyright 2002-2005, Instant802 Networks, Inc.
00003  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
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 /* Caller must hold local->sta_lock */
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 /* protected by RCU */
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 /* get a station info entry even if it is a dummy station*/
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  * Get sta info either from the specified interface
00137  * or from one of its vlans
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  * Get sta info either from the specified interface
00163  * or from one of its vlans (including dummy stations)
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 /* CONFIG_MAC80211_VERBOSE_DEBUG */
00226 
00227         kfree(sta);
00228 }
00229 
00230 /* Caller must hold local->sta_lock */
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                  * timer_to_tid must be initialized with identity mapping
00316                  * to enable session_timer's data differentiation. See
00317                  * sta_rx_agg_session_timer_expired for usage.
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 /* CONFIG_MAC80211_VERBOSE_DEBUG */
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                 /* notify driver */
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                         /* make the station visible */
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          * Can't be a WARN_ON because it can be triggered through a race:
00443          * something inserts a STA (on one CPU) without holding the RTNL
00444          * and another CPU turns off the net device.
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         /* check if STA exists already */
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 /* CONFIG_MAC80211_VERBOSE_DEBUG */
00484 
00485         ieee80211_queue_work(&local->hw, &local->sta_finish_work);
00486 
00487         return 0;
00488 }
00489 
00490 /*
00491  * should be called with sta_mtx locked
00492  * this function replaces the mutex lock
00493  * with a RCU lock
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          * On first glance, this will look racy, because the code
00508          * in this function, which inserts a station with sleeping,
00509          * unlocks the sta_lock between checking existence in the
00510          * hash table and inserting into it.
00511          *
00512          * However, it is not racy against itself because it keeps
00513          * the mutex locked.
00514          */
00515 
00516         spin_lock_irqsave(&local->sta_lock, flags);
00517         /*
00518          * check if STA exists already.
00519          * only accept a scenario of a second call to sta_info_insert_non_ibss
00520          * with a dummy station entry that was inserted earlier
00521          * in that case - assume that the dummy station flag should
00522          * be removed.
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 /* CONFIG_MAC80211_VERBOSE_DEBUG */
00549 
00550         /* move reference to rcu-protected */
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          * In ad-hoc mode, we sometimes need to insert stations
00574          * from tasklet context from the RX path. To avoid races,
00575          * always do so in that case -- see the comment below.
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          * It might seem that the function called below is in race against
00587          * the function call above that atomically inserts the station... That,
00588          * however, is not true because the above code can only
00589          * be invoked for IBSS interfaces, and the below code will
00590          * not be -- and the two do not race against each other as
00591          * the hash table also keys off the interface.
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 /* Caller must hold sta->local->sta_mtx */
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          * This format has been mandated by the IEEE specifications,
00641          * so this line may not be changed to use the __set_bit() format.
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          * This format has been mandated by the IEEE specifications,
00650          * so this line may not be changed to use the __clear_bit() format.
00651          */
00652         bss->tim[aid / 8] &= ~(1 << (aid % 8));
00653 }
00654 
00655 static unsigned long ieee80211_tids_for_ac(int ac)
00656 {
00657         /* If we ever support TIDs > 7, this obviously needs to be adjusted */
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         /* No need to do anything if the driver does all */
00686         if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
00687                 return;
00688 
00689         if (sta->dead)
00690                 goto done;
00691 
00692         /*
00693          * If all ACs are delivery-enabled then we should build
00694          * the TIM bit for all ACs anyway; if only some are then
00695          * we ignore those and build the TIM bit using only the
00696          * non-enabled ones.
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         /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
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          * First check for frames that should expire on the filtered
00763          * queue. Frames here were rejected by the driver and are on
00764          * a separate queue to avoid reordering with normal PS-buffered
00765          * frames. They also aren't accounted for right now in the
00766          * total_ps_buffered counter.
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                  * Frames are queued in order, so if this one
00779                  * hasn't expired yet we can stop testing. If
00780                  * we actually reached the end of the queue we
00781                  * also need to stop, of course.
00782                  */
00783                 if (!skb)
00784                         break;
00785                 dev_kfree_skb(skb);
00786         }
00787 
00788         /*
00789          * Now also check the normal PS-buffered queue, this will
00790          * only find something if the filtered queue was emptied
00791          * since the filtered frames are all before the normal PS
00792          * buffered frames.
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                  * frames are queued in order, so if this one
00805                  * hasn't expired yet (or we reached the end of
00806                  * the queue) we can stop testing
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          * Finally, recalculate the TIM bit for this station -- it might
00821          * now be clear because the station was too slow to retrieve its
00822          * frames.
00823          */
00824         sta_info_recalc_tim(sta);
00825 
00826         /*
00827          * Return whether there are any frames still buffered, this is
00828          * used to check whether the cleanup timer still needs to run,
00829          * if there are no frames we don't need to rearm the timer.
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         /* This is only necessary for stations on BSS interfaces */
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          * Before removing the station from the driver and
00869          * rate control, it might still start new aggregation
00870          * sessions -- block that to make sure the tear-down
00871          * will be sufficient.
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         /* this might still be the pending list ... which is fine */
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          * At this point, after we wait for an RCU grace period,
00922          * neither mac80211 nor the driver can reference this
00923          * sta struct any more except by still existing timers
00924          * associated with this station that we clean up below.
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 /* CONFIG_MAC80211_VERBOSE_DEBUG */
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          * Just return a random station if localaddr is NULL
01083          * ... first in list.
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 /* powersave support code */
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         /* Send all buffered frames to the station */
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 /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
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          * Tell TX path to send this frame even though the
01220          * STA may still remain is PS mode after this frame
01221          * exchange. Also set EOSP to indicate this packet
01222          * ends the poll/service period.
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         /* Service or PS-Poll period starts */
01247         set_sta_flag(sta, WLAN_STA_SP);
01248 
01249         __skb_queue_head_init(&frames);
01250 
01251         /*
01252          * Get response frame(s) and more data bit for it.
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                          * If the driver has data on more than one TID then
01287                          * certainly there's more data if we release just a
01288                          * single frame now (from a single TID).
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                  * For PS-Poll, this can only happen due to a race condition
01311                  * when we set the TIM bit and the station notices it, but
01312                  * before it can poll for the frame we expire it.
01313                  *
01314                  * For uAPSD, this is said in the standard (11.2.1.5 h):
01315                  *      At each unscheduled SP for a non-AP STA, the AP shall
01316                  *      attempt to transmit at least one MSDU or MMPDU, but no
01317                  *      more than the value specified in the Max SP Length field
01318                  *      in the QoS Capability element from delivery-enabled ACs,
01319                  *      that are destined for the non-AP STA.
01320                  *
01321                  * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
01322                  */
01323 
01324                 /* This will evaluate to 1, 3, 5 or 7. */
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                          * Tell TX path to send this frame even though the
01348                          * STA may still remain is PS mode after this frame
01349                          * exchange.
01350                          */
01351                         info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE;
01352 
01353                         /*
01354                          * Use MoreData flag to indicate whether there are
01355                          * more buffered frames for this STA
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                         /* set EOSP for the frame */
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                  * We need to release a frame that is buffered somewhere in the
01393                  * driver ... it'll have to handle that.
01394                  * Note that, as per the comment above, it'll also have to see
01395                  * if there is more than just one frame on the specific TID that
01396                  * we're releasing from, and it needs to set the more-data bit
01397                  * accordingly if we tell it that there's no more data. If we do
01398                  * tell it there's more data, then of course the more-data bit
01399                  * needs to be set anyway.
01400                  */
01401                 drv_release_buffered_frames(local, sta, driver_release_tids,
01402                                             n_frames, reason, more_data);
01403 
01404                 /*
01405                  * Note that we don't recalculate the TIM bit here as it would
01406                  * most likely have no effect at all unless the driver told us
01407                  * that the TID became empty before returning here from the
01408                  * release function.
01409                  * Either way, however, when the driver tells us that the TID
01410                  * became empty we'll do the TIM recalculation.
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          * If all ACs are delivery-enabled then we should reply
01421          * from any of them, if only some are enabled we reply
01422          * only from the non-enabled ones.
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          * If we ever grow support for TSPEC this might happen if
01438          * the TSPEC update from hostapd comes in between a trigger
01439          * frame setting WLAN_STA_UAPSD in the RX path and this
01440          * actually getting called.
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                 /* XXX: what is a good value? */
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                 /* too bad ... but race is better than loss */
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);


ros_rt_wmp
Author(s): Danilo Tardioli, dantard@unizar.es
autogenerated on Fri Jan 3 2014 12:07:55