1f0706e82SJiri Benc /* 2f0706e82SJiri Benc * Copyright 2002-2005, Instant802 Networks, Inc. 3f0706e82SJiri Benc * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> 4f0706e82SJiri Benc * 5f0706e82SJiri Benc * This program is free software; you can redistribute it and/or modify 6f0706e82SJiri Benc * it under the terms of the GNU General Public License version 2 as 7f0706e82SJiri Benc * published by the Free Software Foundation. 8f0706e82SJiri Benc */ 9f0706e82SJiri Benc 10f0706e82SJiri Benc #include <linux/module.h> 11f0706e82SJiri Benc #include <linux/init.h> 12f0706e82SJiri Benc #include <linux/netdevice.h> 13f0706e82SJiri Benc #include <linux/types.h> 14f0706e82SJiri Benc #include <linux/slab.h> 15f0706e82SJiri Benc #include <linux/skbuff.h> 16f0706e82SJiri Benc #include <linux/if_arp.h> 170d174406SJohannes Berg #include <linux/timer.h> 18d0709a65SJohannes Berg #include <linux/rtnetlink.h> 19f0706e82SJiri Benc 20f0706e82SJiri Benc #include <net/mac80211.h> 21f0706e82SJiri Benc #include "ieee80211_i.h" 2224487981SJohannes Berg #include "driver-ops.h" 232c8dccc7SJohannes Berg #include "rate.h" 24f0706e82SJiri Benc #include "sta_info.h" 25e9f207f0SJiri Benc #include "debugfs_sta.h" 26ee385855SLuis Carlos Cobo #include "mesh.h" 27ce662b44SJohannes Berg #include "wme.h" 28f0706e82SJiri Benc 29d0709a65SJohannes Berg /** 30d0709a65SJohannes Berg * DOC: STA information lifetime rules 31d0709a65SJohannes Berg * 32d0709a65SJohannes Berg * STA info structures (&struct sta_info) are managed in a hash table 33d0709a65SJohannes Berg * for faster lookup and a list for iteration. They are managed using 34d0709a65SJohannes Berg * RCU, i.e. access to the list and hash table is protected by RCU. 35d0709a65SJohannes Berg * 3634e89507SJohannes Berg * Upon allocating a STA info structure with sta_info_alloc(), the caller 3734e89507SJohannes Berg * owns that structure. It must then insert it into the hash table using 3834e89507SJohannes Berg * either sta_info_insert() or sta_info_insert_rcu(); only in the latter 3934e89507SJohannes Berg * case (which acquires an rcu read section but must not be called from 4034e89507SJohannes Berg * within one) will the pointer still be valid after the call. Note that 4134e89507SJohannes Berg * the caller may not do much with the STA info before inserting it, in 4234e89507SJohannes Berg * particular, it may not start any mesh peer link management or add 4334e89507SJohannes Berg * encryption keys. 4493e5deb1SJohannes Berg * 4593e5deb1SJohannes Berg * When the insertion fails (sta_info_insert()) returns non-zero), the 4693e5deb1SJohannes Berg * structure will have been freed by sta_info_insert()! 47d0709a65SJohannes Berg * 4834e89507SJohannes Berg * Station entries are added by mac80211 when you establish a link with a 497e189a12SLuis R. Rodriguez * peer. This means different things for the different type of interfaces 507e189a12SLuis R. Rodriguez * we support. For a regular station this mean we add the AP sta when we 5125985edcSLucas De Marchi * receive an association response from the AP. For IBSS this occurs when 5234e89507SJohannes Berg * get to know about a peer on the same IBSS. For WDS we add the sta for 5325985edcSLucas De Marchi * the peer immediately upon device open. When using AP mode we add stations 5434e89507SJohannes Berg * for each respective station upon request from userspace through nl80211. 557e189a12SLuis R. Rodriguez * 5634e89507SJohannes Berg * In order to remove a STA info structure, various sta_info_destroy_*() 5734e89507SJohannes Berg * calls are available. 58d0709a65SJohannes Berg * 5934e89507SJohannes Berg * There is no concept of ownership on a STA entry, each structure is 6034e89507SJohannes Berg * owned by the global hash table/list until it is removed. All users of 6134e89507SJohannes Berg * the structure need to be RCU protected so that the structure won't be 6234e89507SJohannes Berg * freed before they are done using it. 63d0709a65SJohannes Berg */ 64f0706e82SJiri Benc 654d33960bSJohannes Berg /* Caller must hold local->sta_mtx */ 66be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local, 67f0706e82SJiri Benc struct sta_info *sta) 68f0706e82SJiri Benc { 69f0706e82SJiri Benc struct sta_info *s; 70f0706e82SJiri Benc 7140b275b6SJohannes Berg s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)], 724d33960bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 73f0706e82SJiri Benc if (!s) 74be8755e1SMichael Wu return -ENOENT; 75be8755e1SMichael Wu if (s == sta) { 76cf778b00SEric Dumazet rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], 77d0709a65SJohannes Berg s->hnext); 78be8755e1SMichael Wu return 0; 79f0706e82SJiri Benc } 80f0706e82SJiri Benc 8140b275b6SJohannes Berg while (rcu_access_pointer(s->hnext) && 8240b275b6SJohannes Berg rcu_access_pointer(s->hnext) != sta) 8340b275b6SJohannes Berg s = rcu_dereference_protected(s->hnext, 844d33960bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 8540b275b6SJohannes Berg if (rcu_access_pointer(s->hnext)) { 86cf778b00SEric Dumazet rcu_assign_pointer(s->hnext, sta->hnext); 87be8755e1SMichael Wu return 0; 88f0706e82SJiri Benc } 89f0706e82SJiri Benc 90be8755e1SMichael Wu return -ENOENT; 91f0706e82SJiri Benc } 92f0706e82SJiri Benc 93d0709a65SJohannes Berg /* protected by RCU */ 94abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata, 95abe60632SJohannes Berg const u8 *addr) 9643ba7e95SJohannes Berg { 97abe60632SJohannes Berg struct ieee80211_local *local = sdata->local; 9843ba7e95SJohannes Berg struct sta_info *sta; 9943ba7e95SJohannes Berg 1000379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1010379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 10243ba7e95SJohannes Berg while (sta) { 1032a33bee2SGuy Eilam if (sta->sdata == sdata && !sta->dummy && 1042a33bee2SGuy Eilam memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 1052a33bee2SGuy Eilam break; 1062a33bee2SGuy Eilam sta = rcu_dereference_check(sta->hnext, 1072a33bee2SGuy Eilam lockdep_is_held(&local->sta_mtx)); 1082a33bee2SGuy Eilam } 1092a33bee2SGuy Eilam return sta; 1102a33bee2SGuy Eilam } 1112a33bee2SGuy Eilam 1122a33bee2SGuy Eilam /* get a station info entry even if it is a dummy station*/ 1132a33bee2SGuy Eilam struct sta_info *sta_info_get_rx(struct ieee80211_sub_if_data *sdata, 1142a33bee2SGuy Eilam const u8 *addr) 1152a33bee2SGuy Eilam { 1162a33bee2SGuy Eilam struct ieee80211_local *local = sdata->local; 1172a33bee2SGuy Eilam struct sta_info *sta; 1182a33bee2SGuy Eilam 1192a33bee2SGuy Eilam sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1202a33bee2SGuy Eilam lockdep_is_held(&local->sta_mtx)); 1212a33bee2SGuy Eilam while (sta) { 122abe60632SJohannes Berg if (sta->sdata == sdata && 123abe60632SJohannes Berg memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 12443ba7e95SJohannes Berg break; 1250379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1260379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 12743ba7e95SJohannes Berg } 12843ba7e95SJohannes Berg return sta; 12943ba7e95SJohannes Berg } 13043ba7e95SJohannes Berg 1310e5ded5aSFelix Fietkau /* 1320e5ded5aSFelix Fietkau * Get sta info either from the specified interface 1330e5ded5aSFelix Fietkau * or from one of its vlans 1340e5ded5aSFelix Fietkau */ 1350e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata, 1360e5ded5aSFelix Fietkau const u8 *addr) 1370e5ded5aSFelix Fietkau { 1380e5ded5aSFelix Fietkau struct ieee80211_local *local = sdata->local; 1390e5ded5aSFelix Fietkau struct sta_info *sta; 1400e5ded5aSFelix Fietkau 1410379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1420379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1430e5ded5aSFelix Fietkau while (sta) { 1440e5ded5aSFelix Fietkau if ((sta->sdata == sdata || 145a2c1e3daSJohannes Berg (sta->sdata->bss && sta->sdata->bss == sdata->bss)) && 1462a33bee2SGuy Eilam !sta->dummy && 1472a33bee2SGuy Eilam memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 1482a33bee2SGuy Eilam break; 1492a33bee2SGuy Eilam sta = rcu_dereference_check(sta->hnext, 1502a33bee2SGuy Eilam lockdep_is_held(&local->sta_mtx)); 1512a33bee2SGuy Eilam } 1522a33bee2SGuy Eilam return sta; 1532a33bee2SGuy Eilam } 1542a33bee2SGuy Eilam 1552a33bee2SGuy Eilam /* 1562a33bee2SGuy Eilam * Get sta info either from the specified interface 1572a33bee2SGuy Eilam * or from one of its vlans (including dummy stations) 1582a33bee2SGuy Eilam */ 1592a33bee2SGuy Eilam struct sta_info *sta_info_get_bss_rx(struct ieee80211_sub_if_data *sdata, 1602a33bee2SGuy Eilam const u8 *addr) 1612a33bee2SGuy Eilam { 1622a33bee2SGuy Eilam struct ieee80211_local *local = sdata->local; 1632a33bee2SGuy Eilam struct sta_info *sta; 1642a33bee2SGuy Eilam 1652a33bee2SGuy Eilam sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1662a33bee2SGuy Eilam lockdep_is_held(&local->sta_mtx)); 1672a33bee2SGuy Eilam while (sta) { 1682a33bee2SGuy Eilam if ((sta->sdata == sdata || 1692a33bee2SGuy Eilam (sta->sdata->bss && sta->sdata->bss == sdata->bss)) && 1700e5ded5aSFelix Fietkau memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 1710e5ded5aSFelix Fietkau break; 1720379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1730379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1740e5ded5aSFelix Fietkau } 1750e5ded5aSFelix Fietkau return sta; 1760e5ded5aSFelix Fietkau } 1770e5ded5aSFelix Fietkau 1783b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, 1793b53fde8SJohannes Berg int idx) 180ee385855SLuis Carlos Cobo { 1813b53fde8SJohannes Berg struct ieee80211_local *local = sdata->local; 182ee385855SLuis Carlos Cobo struct sta_info *sta; 183ee385855SLuis Carlos Cobo int i = 0; 184ee385855SLuis Carlos Cobo 185d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) { 1863b53fde8SJohannes Berg if (sdata != sta->sdata) 1872a8ca29aSLuis Carlos Cobo continue; 188ee385855SLuis Carlos Cobo if (i < idx) { 189ee385855SLuis Carlos Cobo ++i; 190ee385855SLuis Carlos Cobo continue; 191ee385855SLuis Carlos Cobo } 1922a8ca29aSLuis Carlos Cobo return sta; 193ee385855SLuis Carlos Cobo } 194ee385855SLuis Carlos Cobo 195ee385855SLuis Carlos Cobo return NULL; 196ee385855SLuis Carlos Cobo } 197f0706e82SJiri Benc 19893e5deb1SJohannes Berg /** 199d9a7ddb0SJohannes Berg * sta_info_free - free STA 20093e5deb1SJohannes Berg * 2016ef307bcSRandy Dunlap * @local: pointer to the global information 20293e5deb1SJohannes Berg * @sta: STA info to free 20393e5deb1SJohannes Berg * 20493e5deb1SJohannes Berg * This function must undo everything done by sta_info_alloc() 205d9a7ddb0SJohannes Berg * that may happen before sta_info_insert(). It may only be 206d9a7ddb0SJohannes Berg * called when sta_info_insert() has not been attempted (and 207d9a7ddb0SJohannes Berg * if that fails, the station is freed anyway.) 20893e5deb1SJohannes Berg */ 209d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta) 21093e5deb1SJohannes Berg { 211af65cd96SJohannes Berg if (sta->rate_ctrl) { 2124b7679a5SJohannes Berg rate_control_free_sta(sta); 21393e5deb1SJohannes Berg rate_control_put(sta->rate_ctrl); 214af65cd96SJohannes Berg } 21593e5deb1SJohannes Berg 21693e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 2170fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr); 21893e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 21993e5deb1SJohannes Berg 22093e5deb1SJohannes Berg kfree(sta); 22193e5deb1SJohannes Berg } 22293e5deb1SJohannes Berg 2234d33960bSJohannes Berg /* Caller must hold local->sta_mtx */ 224d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local, 225d0709a65SJohannes Berg struct sta_info *sta) 226f0706e82SJiri Benc { 2274d33960bSJohannes Berg lockdep_assert_held(&local->sta_mtx); 22817741cdcSJohannes Berg sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)]; 229cf778b00SEric Dumazet rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta); 230f0706e82SJiri Benc } 231f0706e82SJiri Benc 232af818581SJohannes Berg static void sta_unblock(struct work_struct *wk) 233af818581SJohannes Berg { 234af818581SJohannes Berg struct sta_info *sta; 235af818581SJohannes Berg 236af818581SJohannes Berg sta = container_of(wk, struct sta_info, drv_unblock_wk); 237af818581SJohannes Berg 238af818581SJohannes Berg if (sta->dead) 239af818581SJohannes Berg return; 240af818581SJohannes Berg 241*54420473SHelmut Schaa if (!test_sta_flag(sta, WLAN_STA_PS_STA)) { 242*54420473SHelmut Schaa local_bh_disable(); 243af818581SJohannes Berg ieee80211_sta_ps_deliver_wakeup(sta); 244*54420473SHelmut Schaa local_bh_enable(); 245*54420473SHelmut Schaa } else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) { 246c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 247ce662b44SJohannes Berg 248ce662b44SJohannes Berg local_bh_disable(); 249af818581SJohannes Berg ieee80211_sta_ps_deliver_poll_response(sta); 250ce662b44SJohannes Berg local_bh_enable(); 251c2c98fdeSJohannes Berg } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) { 252c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 253ce662b44SJohannes Berg 254ce662b44SJohannes Berg local_bh_disable(); 25547086fc5SJohannes Berg ieee80211_sta_ps_deliver_uapsd(sta); 256ce662b44SJohannes Berg local_bh_enable(); 25750a9432dSJohannes Berg } else 258c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 259af818581SJohannes Berg } 260af818581SJohannes Berg 261af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local, 262af65cd96SJohannes Berg struct sta_info *sta, gfp_t gfp) 263af65cd96SJohannes Berg { 264af65cd96SJohannes Berg if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) 265af65cd96SJohannes Berg return 0; 266af65cd96SJohannes Berg 267af65cd96SJohannes Berg sta->rate_ctrl = rate_control_get(local->rate_ctrl); 268af65cd96SJohannes Berg sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, 269af65cd96SJohannes Berg &sta->sta, gfp); 270af65cd96SJohannes Berg if (!sta->rate_ctrl_priv) { 271af65cd96SJohannes Berg rate_control_put(sta->rate_ctrl); 272af65cd96SJohannes Berg return -ENOMEM; 273af65cd96SJohannes Berg } 274af65cd96SJohannes Berg 275af65cd96SJohannes Berg return 0; 276af65cd96SJohannes Berg } 277af65cd96SJohannes Berg 27873651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, 27956544160SJohannes Berg const u8 *addr, gfp_t gfp) 280f0706e82SJiri Benc { 281d0709a65SJohannes Berg struct ieee80211_local *local = sdata->local; 282f0706e82SJiri Benc struct sta_info *sta; 283ebe27c91SMohammed Shafi Shajakhan struct timespec uptime; 28416c5f15cSRon Rindjunsky int i; 285f0706e82SJiri Benc 28617741cdcSJohannes Berg sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp); 287f0706e82SJiri Benc if (!sta) 28873651ee6SJohannes Berg return NULL; 289f0706e82SJiri Benc 29007346f81SJohannes Berg spin_lock_init(&sta->lock); 291af818581SJohannes Berg INIT_WORK(&sta->drv_unblock_wk, sta_unblock); 29267c282c0SJohannes Berg INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work); 293a93e3644SJohannes Berg mutex_init(&sta->ampdu_mlme.mtx); 29407346f81SJohannes Berg 29517741cdcSJohannes Berg memcpy(sta->sta.addr, addr, ETH_ALEN); 296d0709a65SJohannes Berg sta->local = local; 297d0709a65SJohannes Berg sta->sdata = sdata; 2988bc8aecdSFelix Fietkau sta->last_rx = jiffies; 299f0706e82SJiri Benc 300ebe27c91SMohammed Shafi Shajakhan do_posix_clock_monotonic_gettime(&uptime); 301ebe27c91SMohammed Shafi Shajakhan sta->last_connected = uptime.tv_sec; 302541a45a1SBruno Randolf ewma_init(&sta->avg_signal, 1024, 8); 303541a45a1SBruno Randolf 304af65cd96SJohannes Berg if (sta_prepare_rate_control(local, sta, gfp)) { 305f0706e82SJiri Benc kfree(sta); 30673651ee6SJohannes Berg return NULL; 307f0706e82SJiri Benc } 308f0706e82SJiri Benc 30916c5f15cSRon Rindjunsky for (i = 0; i < STA_TID_NUM; i++) { 310a622ab72SJohannes Berg /* 311a622ab72SJohannes Berg * timer_to_tid must be initialized with identity mapping 312a622ab72SJohannes Berg * to enable session_timer's data differentiation. See 313a622ab72SJohannes Berg * sta_rx_agg_session_timer_expired for usage. 314a622ab72SJohannes Berg */ 31516c5f15cSRon Rindjunsky sta->timer_to_tid[i] = i; 31616c5f15cSRon Rindjunsky } 317948d887dSJohannes Berg for (i = 0; i < IEEE80211_NUM_ACS; i++) { 318948d887dSJohannes Berg skb_queue_head_init(&sta->ps_tx_buf[i]); 319948d887dSJohannes Berg skb_queue_head_init(&sta->tx_filtered[i]); 320948d887dSJohannes Berg } 32173651ee6SJohannes Berg 322cccaec98SSenthil Balasubramanian for (i = 0; i < NUM_RX_DATA_QUEUES; i++) 3234be929beSAlexey Dobriyan sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); 324cccaec98SSenthil Balasubramanian 32573651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 3260fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr); 32773651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 32873651ee6SJohannes Berg 32903e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH 33057cf8043SJavier Cardona sta->plink_state = NL80211_PLINK_LISTEN; 33103e4497eSJohannes Berg init_timer(&sta->plink_timer); 33203e4497eSJohannes Berg #endif 33303e4497eSJohannes Berg 33473651ee6SJohannes Berg return sta; 33573651ee6SJohannes Berg } 33673651ee6SJohannes Berg 3378c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta) 33834e89507SJohannes Berg { 33934e89507SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 34034e89507SJohannes Berg 34103e4497eSJohannes Berg /* 34203e4497eSJohannes Berg * Can't be a WARN_ON because it can be triggered through a race: 34303e4497eSJohannes Berg * something inserts a STA (on one CPU) without holding the RTNL 34403e4497eSJohannes Berg * and another CPU turns off the net device. 34503e4497eSJohannes Berg */ 3468c71df7aSGuy Eilam if (unlikely(!ieee80211_sdata_running(sdata))) 3478c71df7aSGuy Eilam return -ENETDOWN; 34803e4497eSJohannes Berg 34947846c9bSJohannes Berg if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 || 3508c71df7aSGuy Eilam is_multicast_ether_addr(sta->sta.addr))) 3518c71df7aSGuy Eilam return -EINVAL; 3528c71df7aSGuy Eilam 3538c71df7aSGuy Eilam return 0; 35493e5deb1SJohannes Berg } 35544213b5eSJohannes Berg 35634e89507SJohannes Berg /* 3578c71df7aSGuy Eilam * should be called with sta_mtx locked 3588c71df7aSGuy Eilam * this function replaces the mutex lock 3598c71df7aSGuy Eilam * with a RCU lock 3608c71df7aSGuy Eilam */ 3614d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU) 3628c71df7aSGuy Eilam { 3638c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 3648c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 3652a33bee2SGuy Eilam struct sta_info *exist_sta; 3662a33bee2SGuy Eilam bool dummy_reinsert = false; 3678c71df7aSGuy Eilam int err = 0; 3688c71df7aSGuy Eilam 3698c71df7aSGuy Eilam lockdep_assert_held(&local->sta_mtx); 3708c71df7aSGuy Eilam 3718c71df7aSGuy Eilam /* 3722a33bee2SGuy Eilam * check if STA exists already. 3734d33960bSJohannes Berg * only accept a scenario of a second call to sta_info_insert_finish 3742a33bee2SGuy Eilam * with a dummy station entry that was inserted earlier 3752a33bee2SGuy Eilam * in that case - assume that the dummy station flag should 3762a33bee2SGuy Eilam * be removed. 3772a33bee2SGuy Eilam */ 3782a33bee2SGuy Eilam exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr); 3792a33bee2SGuy Eilam if (exist_sta) { 3802a33bee2SGuy Eilam if (exist_sta == sta && sta->dummy) { 3812a33bee2SGuy Eilam dummy_reinsert = true; 3822a33bee2SGuy Eilam } else { 3834d33960bSJohannes Berg err = -EEXIST; 3844d33960bSJohannes Berg goto out_err; 38534e89507SJohannes Berg } 3862a33bee2SGuy Eilam } 38734e89507SJohannes Berg 3884d33960bSJohannes Berg if (!sta->dummy || dummy_reinsert) { 3894d33960bSJohannes Berg /* notify driver */ 3904d33960bSJohannes Berg err = drv_sta_add(local, sdata, &sta->sta); 39134e89507SJohannes Berg if (err) { 3924d33960bSJohannes Berg if (sdata->vif.type != NL80211_IFTYPE_ADHOC) 3934d33960bSJohannes Berg goto out_err; 3944d33960bSJohannes Berg printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to " 3954d33960bSJohannes Berg "driver (%d) - keeping it anyway.\n", 3964d33960bSJohannes Berg sdata->name, sta->sta.addr, err); 3974d33960bSJohannes Berg } else 3984d33960bSJohannes Berg sta->uploaded = true; 3994d33960bSJohannes Berg } 4004d33960bSJohannes Berg 4014d33960bSJohannes Berg if (!dummy_reinsert) { 4024d33960bSJohannes Berg local->num_sta++; 4034d33960bSJohannes Berg local->sta_generation++; 4044d33960bSJohannes Berg smp_mb(); 4054d33960bSJohannes Berg 4064d33960bSJohannes Berg /* make the station visible */ 4074d33960bSJohannes Berg sta_info_hash_add(local, sta); 4084d33960bSJohannes Berg 4094d33960bSJohannes Berg list_add(&sta->list, &local->sta_list); 4104d33960bSJohannes Berg } else { 4114d33960bSJohannes Berg sta->dummy = false; 4124d33960bSJohannes Berg } 4134d33960bSJohannes Berg 4144d33960bSJohannes Berg if (!sta->dummy) { 4154d33960bSJohannes Berg struct station_info sinfo; 4164d33960bSJohannes Berg 4174d33960bSJohannes Berg ieee80211_sta_debugfs_add(sta); 4184d33960bSJohannes Berg rate_control_add_sta_debugfs(sta); 4194d33960bSJohannes Berg 4204d33960bSJohannes Berg memset(&sinfo, 0, sizeof(sinfo)); 4214d33960bSJohannes Berg sinfo.filled = 0; 4224d33960bSJohannes Berg sinfo.generation = local->sta_generation; 4234d33960bSJohannes Berg cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL); 42432bfd35dSJohannes Berg } 425d0709a65SJohannes Berg 426f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 4272a33bee2SGuy Eilam wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n", 4282a33bee2SGuy Eilam sta->dummy ? "dummy " : "", sta->sta.addr); 429f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 430f0706e82SJiri Benc 43134e89507SJohannes Berg /* move reference to rcu-protected */ 43234e89507SJohannes Berg rcu_read_lock(); 43334e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 434e9f207f0SJiri Benc 43573651ee6SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 43673651ee6SJohannes Berg mesh_accept_plinks_update(sdata); 43773651ee6SJohannes Berg 43873651ee6SJohannes Berg return 0; 4394d33960bSJohannes Berg out_err: 4404d33960bSJohannes Berg mutex_unlock(&local->sta_mtx); 4414d33960bSJohannes Berg rcu_read_lock(); 4424d33960bSJohannes Berg return err; 4438c71df7aSGuy Eilam } 4448c71df7aSGuy Eilam 4458c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) 4468c71df7aSGuy Eilam { 4478c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 4488c71df7aSGuy Eilam int err = 0; 4498c71df7aSGuy Eilam 4504d33960bSJohannes Berg might_sleep(); 4514d33960bSJohannes Berg 4528c71df7aSGuy Eilam err = sta_info_insert_check(sta); 4538c71df7aSGuy Eilam if (err) { 4548c71df7aSGuy Eilam rcu_read_lock(); 4558c71df7aSGuy Eilam goto out_free; 4568c71df7aSGuy Eilam } 4578c71df7aSGuy Eilam 458004c872eSJohannes Berg mutex_lock(&local->sta_mtx); 459004c872eSJohannes Berg 4604d33960bSJohannes Berg err = sta_info_insert_finish(sta); 4618c71df7aSGuy Eilam if (err) 462004c872eSJohannes Berg goto out_free; 463d0709a65SJohannes Berg 464004c872eSJohannes Berg return 0; 46593e5deb1SJohannes Berg out_free: 46693e5deb1SJohannes Berg BUG_ON(!err); 467d9a7ddb0SJohannes Berg sta_info_free(local, sta); 46893e5deb1SJohannes Berg return err; 469f0706e82SJiri Benc } 470f0706e82SJiri Benc 47134e89507SJohannes Berg int sta_info_insert(struct sta_info *sta) 47234e89507SJohannes Berg { 47334e89507SJohannes Berg int err = sta_info_insert_rcu(sta); 47434e89507SJohannes Berg 47534e89507SJohannes Berg rcu_read_unlock(); 47634e89507SJohannes Berg 47734e89507SJohannes Berg return err; 47834e89507SJohannes Berg } 47934e89507SJohannes Berg 4802a33bee2SGuy Eilam /* Caller must hold sta->local->sta_mtx */ 4812a33bee2SGuy Eilam int sta_info_reinsert(struct sta_info *sta) 4822a33bee2SGuy Eilam { 4832a33bee2SGuy Eilam struct ieee80211_local *local = sta->local; 4842a33bee2SGuy Eilam int err = 0; 4852a33bee2SGuy Eilam 4862a33bee2SGuy Eilam err = sta_info_insert_check(sta); 4872a33bee2SGuy Eilam if (err) { 4882a33bee2SGuy Eilam mutex_unlock(&local->sta_mtx); 4892a33bee2SGuy Eilam return err; 4902a33bee2SGuy Eilam } 4912a33bee2SGuy Eilam 4922a33bee2SGuy Eilam might_sleep(); 4932a33bee2SGuy Eilam 4944d33960bSJohannes Berg err = sta_info_insert_finish(sta); 4952a33bee2SGuy Eilam rcu_read_unlock(); 4962a33bee2SGuy Eilam return err; 4972a33bee2SGuy Eilam } 4982a33bee2SGuy Eilam 499f0706e82SJiri Benc static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid) 500004c872eSJohannes Berg { 501004c872eSJohannes Berg /* 502004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 503004c872eSJohannes Berg * so this line may not be changed to use the __set_bit() format. 504004c872eSJohannes Berg */ 505004c872eSJohannes Berg bss->tim[aid / 8] |= (1 << (aid % 8)); 506004c872eSJohannes Berg } 507004c872eSJohannes Berg 508004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid) 509004c872eSJohannes Berg { 510004c872eSJohannes Berg /* 511004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 512004c872eSJohannes Berg * so this line may not be changed to use the __clear_bit() format. 513004c872eSJohannes Berg */ 514004c872eSJohannes Berg bss->tim[aid / 8] &= ~(1 << (aid % 8)); 515004c872eSJohannes Berg } 516004c872eSJohannes Berg 517948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac) 518004c872eSJohannes Berg { 519948d887dSJohannes Berg /* If we ever support TIDs > 7, this obviously needs to be adjusted */ 520948d887dSJohannes Berg switch (ac) { 521948d887dSJohannes Berg case IEEE80211_AC_VO: 522948d887dSJohannes Berg return BIT(6) | BIT(7); 523948d887dSJohannes Berg case IEEE80211_AC_VI: 524948d887dSJohannes Berg return BIT(4) | BIT(5); 525948d887dSJohannes Berg case IEEE80211_AC_BE: 526948d887dSJohannes Berg return BIT(0) | BIT(3); 527948d887dSJohannes Berg case IEEE80211_AC_BK: 528948d887dSJohannes Berg return BIT(1) | BIT(2); 529948d887dSJohannes Berg default: 530948d887dSJohannes Berg WARN_ON(1); 531948d887dSJohannes Berg return 0; 532d0709a65SJohannes Berg } 533004c872eSJohannes Berg } 534004c872eSJohannes Berg 535c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta) 536004c872eSJohannes Berg { 537c868cb35SJohannes Berg struct ieee80211_local *local = sta->local; 538c868cb35SJohannes Berg struct ieee80211_if_ap *bss = sta->sdata->bss; 539d0709a65SJohannes Berg unsigned long flags; 540948d887dSJohannes Berg bool indicate_tim = false; 541948d887dSJohannes Berg u8 ignore_for_tim = sta->sta.uapsd_queues; 542948d887dSJohannes Berg int ac; 543004c872eSJohannes Berg 544c868cb35SJohannes Berg if (WARN_ON_ONCE(!sta->sdata->bss)) 545c868cb35SJohannes Berg return; 5463e122be0SJohannes Berg 547c868cb35SJohannes Berg /* No need to do anything if the driver does all */ 548c868cb35SJohannes Berg if (local->hw.flags & IEEE80211_HW_AP_LINK_PS) 549c868cb35SJohannes Berg return; 550004c872eSJohannes Berg 551c868cb35SJohannes Berg if (sta->dead) 552c868cb35SJohannes Berg goto done; 5533e122be0SJohannes Berg 554948d887dSJohannes Berg /* 555948d887dSJohannes Berg * If all ACs are delivery-enabled then we should build 556948d887dSJohannes Berg * the TIM bit for all ACs anyway; if only some are then 557948d887dSJohannes Berg * we ignore those and build the TIM bit using only the 558948d887dSJohannes Berg * non-enabled ones. 559948d887dSJohannes Berg */ 560948d887dSJohannes Berg if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1) 561948d887dSJohannes Berg ignore_for_tim = 0; 562948d887dSJohannes Berg 563948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 564948d887dSJohannes Berg unsigned long tids; 565948d887dSJohannes Berg 566948d887dSJohannes Berg if (ignore_for_tim & BIT(ac)) 567948d887dSJohannes Berg continue; 568948d887dSJohannes Berg 569948d887dSJohannes Berg indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) || 570948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac]); 571948d887dSJohannes Berg if (indicate_tim) 572948d887dSJohannes Berg break; 573948d887dSJohannes Berg 574948d887dSJohannes Berg tids = ieee80211_tids_for_ac(ac); 575948d887dSJohannes Berg 576948d887dSJohannes Berg indicate_tim |= 577948d887dSJohannes Berg sta->driver_buffered_tids & tids; 578004c872eSJohannes Berg } 579004c872eSJohannes Berg 580c868cb35SJohannes Berg done: 5814d33960bSJohannes Berg spin_lock_irqsave(&local->tim_lock, flags); 582004c872eSJohannes Berg 583948d887dSJohannes Berg if (indicate_tim) 584c868cb35SJohannes Berg __bss_tim_set(bss, sta->sta.aid); 585c868cb35SJohannes Berg else 58617741cdcSJohannes Berg __bss_tim_clear(bss, sta->sta.aid); 5873e122be0SJohannes Berg 588c868cb35SJohannes Berg if (local->ops->set_tim) { 589c868cb35SJohannes Berg local->tim_in_locked_section = true; 590948d887dSJohannes Berg drv_set_tim(local, &sta->sta, indicate_tim); 591c868cb35SJohannes Berg local->tim_in_locked_section = false; 592004c872eSJohannes Berg } 593004c872eSJohannes Berg 5944d33960bSJohannes Berg spin_unlock_irqrestore(&local->tim_lock, flags); 595004c872eSJohannes Berg } 596004c872eSJohannes Berg 597cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb) 598f0706e82SJiri Benc { 599e039fa4aSJohannes Berg struct ieee80211_tx_info *info; 600f0706e82SJiri Benc int timeout; 601f0706e82SJiri Benc 602f0706e82SJiri Benc if (!skb) 603cd0b8d89SJohannes Berg return false; 604f0706e82SJiri Benc 605e039fa4aSJohannes Berg info = IEEE80211_SKB_CB(skb); 606f0706e82SJiri Benc 607f0706e82SJiri Benc /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 60857c4d7b4SJohannes Berg timeout = (sta->listen_interval * 60957c4d7b4SJohannes Berg sta->sdata->vif.bss_conf.beacon_int * 61057c4d7b4SJohannes Berg 32 / 15625) * HZ; 611f0706e82SJiri Benc if (timeout < STA_TX_BUFFER_EXPIRE) 612f0706e82SJiri Benc timeout = STA_TX_BUFFER_EXPIRE; 613e039fa4aSJohannes Berg return time_after(jiffies, info->control.jiffies + timeout); 614f0706e82SJiri Benc } 615f0706e82SJiri Benc 616f0706e82SJiri Benc 617948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local, 618948d887dSJohannes Berg struct sta_info *sta, int ac) 619f0706e82SJiri Benc { 620f0706e82SJiri Benc unsigned long flags; 621f0706e82SJiri Benc struct sk_buff *skb; 622f0706e82SJiri Benc 62360750397SJohannes Berg /* 62460750397SJohannes Berg * First check for frames that should expire on the filtered 62560750397SJohannes Berg * queue. Frames here were rejected by the driver and are on 62660750397SJohannes Berg * a separate queue to avoid reordering with normal PS-buffered 62760750397SJohannes Berg * frames. They also aren't accounted for right now in the 62860750397SJohannes Berg * total_ps_buffered counter. 62960750397SJohannes Berg */ 630f0706e82SJiri Benc for (;;) { 631948d887dSJohannes Berg spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 632948d887dSJohannes Berg skb = skb_peek(&sta->tx_filtered[ac]); 63357c4d7b4SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 634948d887dSJohannes Berg skb = __skb_dequeue(&sta->tx_filtered[ac]); 635836341a7SJohannes Berg else 636f0706e82SJiri Benc skb = NULL; 637948d887dSJohannes Berg spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 638f0706e82SJiri Benc 63960750397SJohannes Berg /* 64060750397SJohannes Berg * Frames are queued in order, so if this one 64160750397SJohannes Berg * hasn't expired yet we can stop testing. If 64260750397SJohannes Berg * we actually reached the end of the queue we 64360750397SJohannes Berg * also need to stop, of course. 64460750397SJohannes Berg */ 64560750397SJohannes Berg if (!skb) 64660750397SJohannes Berg break; 64760750397SJohannes Berg dev_kfree_skb(skb); 64860750397SJohannes Berg } 64960750397SJohannes Berg 65060750397SJohannes Berg /* 65160750397SJohannes Berg * Now also check the normal PS-buffered queue, this will 65260750397SJohannes Berg * only find something if the filtered queue was emptied 65360750397SJohannes Berg * since the filtered frames are all before the normal PS 65460750397SJohannes Berg * buffered frames. 65560750397SJohannes Berg */ 656f0706e82SJiri Benc for (;;) { 657948d887dSJohannes Berg spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 658948d887dSJohannes Berg skb = skb_peek(&sta->ps_tx_buf[ac]); 659f0706e82SJiri Benc if (sta_info_buffer_expired(sta, skb)) 660948d887dSJohannes Berg skb = __skb_dequeue(&sta->ps_tx_buf[ac]); 661f0706e82SJiri Benc else 662f0706e82SJiri Benc skb = NULL; 663948d887dSJohannes Berg spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 664f0706e82SJiri Benc 66560750397SJohannes Berg /* 66660750397SJohannes Berg * frames are queued in order, so if this one 66760750397SJohannes Berg * hasn't expired yet (or we reached the end of 66860750397SJohannes Berg * the queue) we can stop testing 66960750397SJohannes Berg */ 670836341a7SJohannes Berg if (!skb) 671836341a7SJohannes Berg break; 672836341a7SJohannes Berg 673f0706e82SJiri Benc local->total_ps_buffered--; 674f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 6750c68ae26SJohannes Berg printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n", 6760c68ae26SJohannes Berg sta->sta.addr); 677f4ea83ddSJohannes Berg #endif 678f0706e82SJiri Benc dev_kfree_skb(skb); 679f0706e82SJiri Benc } 6803393a608SJuuso Oikarinen 68160750397SJohannes Berg /* 68260750397SJohannes Berg * Finally, recalculate the TIM bit for this station -- it might 68360750397SJohannes Berg * now be clear because the station was too slow to retrieve its 68460750397SJohannes Berg * frames. 68560750397SJohannes Berg */ 68660750397SJohannes Berg sta_info_recalc_tim(sta); 68760750397SJohannes Berg 68860750397SJohannes Berg /* 68960750397SJohannes Berg * Return whether there are any frames still buffered, this is 69060750397SJohannes Berg * used to check whether the cleanup timer still needs to run, 69160750397SJohannes Berg * if there are no frames we don't need to rearm the timer. 69260750397SJohannes Berg */ 693948d887dSJohannes Berg return !(skb_queue_empty(&sta->ps_tx_buf[ac]) && 694948d887dSJohannes Berg skb_queue_empty(&sta->tx_filtered[ac])); 695948d887dSJohannes Berg } 696948d887dSJohannes Berg 697948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 698948d887dSJohannes Berg struct sta_info *sta) 699948d887dSJohannes Berg { 700948d887dSJohannes Berg bool have_buffered = false; 701948d887dSJohannes Berg int ac; 702948d887dSJohannes Berg 703948d887dSJohannes Berg /* This is only necessary for stations on BSS interfaces */ 704948d887dSJohannes Berg if (!sta->sdata->bss) 705948d887dSJohannes Berg return false; 706948d887dSJohannes Berg 707948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 708948d887dSJohannes Berg have_buffered |= 709948d887dSJohannes Berg sta_info_cleanup_expire_buffered_ac(local, sta, ac); 710948d887dSJohannes Berg 711948d887dSJohannes Berg return have_buffered; 712f0706e82SJiri Benc } 713f0706e82SJiri Benc 71434e89507SJohannes Berg static int __must_check __sta_info_destroy(struct sta_info *sta) 71534e89507SJohannes Berg { 71634e89507SJohannes Berg struct ieee80211_local *local; 71734e89507SJohannes Berg struct ieee80211_sub_if_data *sdata; 718948d887dSJohannes Berg int ret, i, ac; 71942624d49SYogesh Ashok Powar struct tid_ampdu_tx *tid_tx; 72034e89507SJohannes Berg 72134e89507SJohannes Berg might_sleep(); 72234e89507SJohannes Berg 72334e89507SJohannes Berg if (!sta) 72434e89507SJohannes Berg return -ENOENT; 72534e89507SJohannes Berg 72634e89507SJohannes Berg local = sta->local; 72734e89507SJohannes Berg sdata = sta->sdata; 72834e89507SJohannes Berg 729098a6070SJohannes Berg /* 730098a6070SJohannes Berg * Before removing the station from the driver and 731098a6070SJohannes Berg * rate control, it might still start new aggregation 732098a6070SJohannes Berg * sessions -- block that to make sure the tear-down 733098a6070SJohannes Berg * will be sufficient. 734098a6070SJohannes Berg */ 735c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_BLOCK_BA); 73653f73c09SJohannes Berg ieee80211_sta_tear_down_BA_sessions(sta, true); 737098a6070SJohannes Berg 73834e89507SJohannes Berg ret = sta_info_hash_del(local, sta); 73934e89507SJohannes Berg if (ret) 74034e89507SJohannes Berg return ret; 74134e89507SJohannes Berg 7424d33960bSJohannes Berg list_del(&sta->list); 7434d33960bSJohannes Berg 7448cb23153SJohannes Berg mutex_lock(&local->key_mtx); 745e31b8213SJohannes Berg for (i = 0; i < NUM_DEFAULT_KEYS; i++) 74640b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i])); 747e31b8213SJohannes Berg if (sta->ptk) 74840b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->ptk)); 7498cb23153SJohannes Berg mutex_unlock(&local->key_mtx); 75034e89507SJohannes Berg 75134e89507SJohannes Berg sta->dead = true; 75234e89507SJohannes Berg 753c2c98fdeSJohannes Berg if (test_sta_flag(sta, WLAN_STA_PS_STA) || 754c2c98fdeSJohannes Berg test_sta_flag(sta, WLAN_STA_PS_DRIVER)) { 75534e89507SJohannes Berg BUG_ON(!sdata->bss); 75634e89507SJohannes Berg 757c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_STA); 758c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 759c2c98fdeSJohannes Berg 76034e89507SJohannes Berg atomic_dec(&sdata->bss->num_sta_ps); 761c868cb35SJohannes Berg sta_info_recalc_tim(sta); 76234e89507SJohannes Berg } 76334e89507SJohannes Berg 76434e89507SJohannes Berg local->num_sta--; 76534e89507SJohannes Berg local->sta_generation++; 76634e89507SJohannes Berg 76734e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 768a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(sdata->u.vlan.sta, NULL); 76934e89507SJohannes Berg 770d9a7ddb0SJohannes Berg while (sta->sta_state > IEEE80211_STA_NONE) 771d9a7ddb0SJohannes Berg sta_info_move_state(sta, sta->sta_state - 1); 772d9a7ddb0SJohannes Berg 77334e89507SJohannes Berg if (sta->uploaded) { 77434e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 77534e89507SJohannes Berg sdata = container_of(sdata->bss, 77634e89507SJohannes Berg struct ieee80211_sub_if_data, 77734e89507SJohannes Berg u.ap); 77834e89507SJohannes Berg drv_sta_remove(local, sdata, &sta->sta); 77934e89507SJohannes Berg sdata = sta->sdata; 78034e89507SJohannes Berg } 78134e89507SJohannes Berg 782e64b3795SJohannes Berg /* 783e64b3795SJohannes Berg * At this point, after we wait for an RCU grace period, 784e64b3795SJohannes Berg * neither mac80211 nor the driver can reference this 785e64b3795SJohannes Berg * sta struct any more except by still existing timers 786e64b3795SJohannes Berg * associated with this station that we clean up below. 787e64b3795SJohannes Berg */ 788e64b3795SJohannes Berg synchronize_rcu(); 789e64b3795SJohannes Berg 790948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 791948d887dSJohannes Berg local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]); 792948d887dSJohannes Berg __skb_queue_purge(&sta->ps_tx_buf[ac]); 793948d887dSJohannes Berg __skb_queue_purge(&sta->tx_filtered[ac]); 794948d887dSJohannes Berg } 795c868cb35SJohannes Berg 79634e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH 797e64b3795SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 79834e89507SJohannes Berg mesh_accept_plinks_update(sdata); 79934e89507SJohannes Berg #endif 80034e89507SJohannes Berg 80134e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 8020fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr); 80334e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 80434e89507SJohannes Berg cancel_work_sync(&sta->drv_unblock_wk); 80534e89507SJohannes Berg 806ec15e68bSJouni Malinen cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL); 807ec15e68bSJouni Malinen 80834e89507SJohannes Berg rate_control_remove_sta_debugfs(sta); 80934e89507SJohannes Berg ieee80211_sta_debugfs_remove(sta); 81034e89507SJohannes Berg 81134e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH 81234e89507SJohannes Berg if (ieee80211_vif_is_mesh(&sta->sdata->vif)) { 81334e89507SJohannes Berg mesh_plink_deactivate(sta); 81434e89507SJohannes Berg del_timer_sync(&sta->plink_timer); 81534e89507SJohannes Berg } 81634e89507SJohannes Berg #endif 81734e89507SJohannes Berg 81842624d49SYogesh Ashok Powar /* There could be some memory leaks because of ampdu tx pending queue 81942624d49SYogesh Ashok Powar * not being freed before destroying the station info. 82042624d49SYogesh Ashok Powar * 82142624d49SYogesh Ashok Powar * Make sure that such queues are purged before freeing the station 82242624d49SYogesh Ashok Powar * info. 82342624d49SYogesh Ashok Powar * TODO: We have to somehow postpone the full destruction 82442624d49SYogesh Ashok Powar * until the aggregation stop completes. Refer 82542624d49SYogesh Ashok Powar * http://thread.gmane.org/gmane.linux.kernel.wireless.general/81936 82642624d49SYogesh Ashok Powar */ 82727bf8882SYogesh Ashok Powar 82827bf8882SYogesh Ashok Powar mutex_lock(&sta->ampdu_mlme.mtx); 82927bf8882SYogesh Ashok Powar 83042624d49SYogesh Ashok Powar for (i = 0; i < STA_TID_NUM; i++) { 83127bf8882SYogesh Ashok Powar tid_tx = rcu_dereference_protected_tid_tx(sta, i); 83227bf8882SYogesh Ashok Powar if (!tid_tx) 83342624d49SYogesh Ashok Powar continue; 83442624d49SYogesh Ashok Powar if (skb_queue_len(&tid_tx->pending)) { 83542624d49SYogesh Ashok Powar #ifdef CONFIG_MAC80211_HT_DEBUG 83642624d49SYogesh Ashok Powar wiphy_debug(local->hw.wiphy, "TX A-MPDU purging %d " 83742624d49SYogesh Ashok Powar "packets for tid=%d\n", 83842624d49SYogesh Ashok Powar skb_queue_len(&tid_tx->pending), i); 83942624d49SYogesh Ashok Powar #endif /* CONFIG_MAC80211_HT_DEBUG */ 84042624d49SYogesh Ashok Powar __skb_queue_purge(&tid_tx->pending); 84142624d49SYogesh Ashok Powar } 84242624d49SYogesh Ashok Powar kfree_rcu(tid_tx, rcu_head); 84342624d49SYogesh Ashok Powar } 84442624d49SYogesh Ashok Powar 84527bf8882SYogesh Ashok Powar mutex_unlock(&sta->ampdu_mlme.mtx); 84627bf8882SYogesh Ashok Powar 847d9a7ddb0SJohannes Berg sta_info_free(local, sta); 84834e89507SJohannes Berg 84934e89507SJohannes Berg return 0; 85034e89507SJohannes Berg } 85134e89507SJohannes Berg 85234e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) 85334e89507SJohannes Berg { 85434e89507SJohannes Berg struct sta_info *sta; 85534e89507SJohannes Berg int ret; 85634e89507SJohannes Berg 85734e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 8582a33bee2SGuy Eilam sta = sta_info_get_rx(sdata, addr); 85934e89507SJohannes Berg ret = __sta_info_destroy(sta); 86034e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 86134e89507SJohannes Berg 86234e89507SJohannes Berg return ret; 86334e89507SJohannes Berg } 86434e89507SJohannes Berg 86534e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, 86634e89507SJohannes Berg const u8 *addr) 86734e89507SJohannes Berg { 86834e89507SJohannes Berg struct sta_info *sta; 86934e89507SJohannes Berg int ret; 87034e89507SJohannes Berg 87134e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 8722a33bee2SGuy Eilam sta = sta_info_get_bss_rx(sdata, addr); 87334e89507SJohannes Berg ret = __sta_info_destroy(sta); 87434e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 87534e89507SJohannes Berg 87634e89507SJohannes Berg return ret; 87734e89507SJohannes Berg } 878f0706e82SJiri Benc 879f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data) 880f0706e82SJiri Benc { 881f0706e82SJiri Benc struct ieee80211_local *local = (struct ieee80211_local *) data; 882f0706e82SJiri Benc struct sta_info *sta; 8833393a608SJuuso Oikarinen bool timer_needed = false; 884f0706e82SJiri Benc 885d0709a65SJohannes Berg rcu_read_lock(); 886d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) 8873393a608SJuuso Oikarinen if (sta_info_cleanup_expire_buffered(local, sta)) 8883393a608SJuuso Oikarinen timer_needed = true; 889d0709a65SJohannes Berg rcu_read_unlock(); 890f0706e82SJiri Benc 8915bb644a0SJohannes Berg if (local->quiescing) 8925bb644a0SJohannes Berg return; 8935bb644a0SJohannes Berg 8943393a608SJuuso Oikarinen if (!timer_needed) 8953393a608SJuuso Oikarinen return; 8963393a608SJuuso Oikarinen 89726d59535SJohannes Berg mod_timer(&local->sta_cleanup, 89826d59535SJohannes Berg round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 899f0706e82SJiri Benc } 900f0706e82SJiri Benc 901f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local) 902f0706e82SJiri Benc { 9034d33960bSJohannes Berg spin_lock_init(&local->tim_lock); 90434e89507SJohannes Berg mutex_init(&local->sta_mtx); 905f0706e82SJiri Benc INIT_LIST_HEAD(&local->sta_list); 906f0706e82SJiri Benc 907b24b8a24SPavel Emelyanov setup_timer(&local->sta_cleanup, sta_info_cleanup, 908b24b8a24SPavel Emelyanov (unsigned long)local); 909f0706e82SJiri Benc } 910f0706e82SJiri Benc 911f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local) 912f0706e82SJiri Benc { 913f0706e82SJiri Benc del_timer(&local->sta_cleanup); 914be8755e1SMichael Wu sta_info_flush(local, NULL); 915f0706e82SJiri Benc } 916f0706e82SJiri Benc 917f0706e82SJiri Benc /** 918f0706e82SJiri Benc * sta_info_flush - flush matching STA entries from the STA table 91944213b5eSJohannes Berg * 92044213b5eSJohannes Berg * Returns the number of removed STA entries. 92144213b5eSJohannes Berg * 922f0706e82SJiri Benc * @local: local interface data 923d0709a65SJohannes Berg * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs 924f0706e82SJiri Benc */ 92544213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local, 926d0709a65SJohannes Berg struct ieee80211_sub_if_data *sdata) 927f0706e82SJiri Benc { 928f0706e82SJiri Benc struct sta_info *sta, *tmp; 92944213b5eSJohannes Berg int ret = 0; 930f0706e82SJiri Benc 931d0709a65SJohannes Berg might_sleep(); 932d0709a65SJohannes Berg 93334e89507SJohannes Berg mutex_lock(&local->sta_mtx); 93434e89507SJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 93534e89507SJohannes Berg if (!sdata || sdata == sta->sdata) 93634e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 93734e89507SJohannes Berg } 93834e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 93944213b5eSJohannes Berg 94044213b5eSJohannes Berg return ret; 941f0706e82SJiri Benc } 942dc6676b7SJohannes Berg 94324723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 94424723d1bSJohannes Berg unsigned long exp_time) 94524723d1bSJohannes Berg { 94624723d1bSJohannes Berg struct ieee80211_local *local = sdata->local; 94724723d1bSJohannes Berg struct sta_info *sta, *tmp; 94824723d1bSJohannes Berg 94934e89507SJohannes Berg mutex_lock(&local->sta_mtx); 950e46a2cf9SMohammed Shafi Shajakhan 951e46a2cf9SMohammed Shafi Shajakhan list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 952ec2b774eSMarek Lindner if (sdata != sta->sdata) 953ec2b774eSMarek Lindner continue; 954ec2b774eSMarek Lindner 95524723d1bSJohannes Berg if (time_after(jiffies, sta->last_rx + exp_time)) { 95624723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG 9570c68ae26SJohannes Berg printk(KERN_DEBUG "%s: expiring inactive STA %pM\n", 95847846c9bSJohannes Berg sdata->name, sta->sta.addr); 95924723d1bSJohannes Berg #endif 96034e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 96124723d1bSJohannes Berg } 962e46a2cf9SMohammed Shafi Shajakhan } 963e46a2cf9SMohammed Shafi Shajakhan 96434e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 96524723d1bSJohannes Berg } 96617741cdcSJohannes Berg 967686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 968686b9cb9SBen Greear const u8 *addr, 969686b9cb9SBen Greear const u8 *localaddr) 97017741cdcSJohannes Berg { 971abe60632SJohannes Berg struct sta_info *sta, *nxt; 97217741cdcSJohannes Berg 973686b9cb9SBen Greear /* 974686b9cb9SBen Greear * Just return a random station if localaddr is NULL 975686b9cb9SBen Greear * ... first in list. 976686b9cb9SBen Greear */ 977f7c65594SJohannes Berg for_each_sta_info(hw_to_local(hw), addr, sta, nxt) { 978686b9cb9SBen Greear if (localaddr && 979686b9cb9SBen Greear compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0) 980686b9cb9SBen Greear continue; 981f7c65594SJohannes Berg if (!sta->uploaded) 982f7c65594SJohannes Berg return NULL; 98317741cdcSJohannes Berg return &sta->sta; 984f7c65594SJohannes Berg } 985f7c65594SJohannes Berg 986abe60632SJohannes Berg return NULL; 98717741cdcSJohannes Berg } 988686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); 9895ed176e1SJohannes Berg 9905ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, 9915ed176e1SJohannes Berg const u8 *addr) 9925ed176e1SJohannes Berg { 993f7c65594SJohannes Berg struct sta_info *sta; 9945ed176e1SJohannes Berg 9955ed176e1SJohannes Berg if (!vif) 9965ed176e1SJohannes Berg return NULL; 9975ed176e1SJohannes Berg 998f7c65594SJohannes Berg sta = sta_info_get_bss(vif_to_sdata(vif), addr); 999f7c65594SJohannes Berg if (!sta) 1000f7c65594SJohannes Berg return NULL; 10015ed176e1SJohannes Berg 1002f7c65594SJohannes Berg if (!sta->uploaded) 1003f7c65594SJohannes Berg return NULL; 1004f7c65594SJohannes Berg 1005f7c65594SJohannes Berg return &sta->sta; 10065ed176e1SJohannes Berg } 100717741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta); 1008af818581SJohannes Berg 100950a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta) 101050a9432dSJohannes Berg { 101150a9432dSJohannes Berg struct sta_info *sta = _sta; 101250a9432dSJohannes Berg 1013c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 1014c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_STA); 101550a9432dSJohannes Berg } 101650a9432dSJohannes Berg 1017af818581SJohannes Berg /* powersave support code */ 1018af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) 1019af818581SJohannes Berg { 1020af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1021af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 1022948d887dSJohannes Berg struct sk_buff_head pending; 1023948d887dSJohannes Berg int filtered = 0, buffered = 0, ac; 1024af818581SJohannes Berg 1025c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_SP); 102647086fc5SJohannes Berg 1027948d887dSJohannes Berg BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1); 1028948d887dSJohannes Berg sta->driver_buffered_tids = 0; 1029948d887dSJohannes Berg 1030d057e5a3SArik Nemtsov if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS)) 103112375ef9SJohannes Berg drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 1032af818581SJohannes Berg 1033948d887dSJohannes Berg skb_queue_head_init(&pending); 1034af818581SJohannes Berg 1035af818581SJohannes Berg /* Send all buffered frames to the station */ 1036948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1037948d887dSJohannes Berg int count = skb_queue_len(&pending), tmp; 1038948d887dSJohannes Berg 1039948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending); 1040948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1041948d887dSJohannes Berg filtered += tmp - count; 1042948d887dSJohannes Berg count = tmp; 1043948d887dSJohannes Berg 1044948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending); 1045948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1046948d887dSJohannes Berg buffered += tmp - count; 1047948d887dSJohannes Berg } 1048948d887dSJohannes Berg 1049948d887dSJohannes Berg ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta); 1050948d887dSJohannes Berg 1051af818581SJohannes Berg local->total_ps_buffered -= buffered; 1052af818581SJohannes Berg 1053c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1054c868cb35SJohannes Berg 1055af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 1056af818581SJohannes Berg printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames " 105747846c9bSJohannes Berg "since STA not sleeping anymore\n", sdata->name, 1058948d887dSJohannes Berg sta->sta.addr, sta->sta.aid, filtered, buffered); 1059af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 1060af818581SJohannes Berg } 1061af818581SJohannes Berg 1062ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata, 1063ce662b44SJohannes Berg struct sta_info *sta, int tid, 106440b96408SJohannes Berg enum ieee80211_frame_release_type reason) 1065ce662b44SJohannes Berg { 1066ce662b44SJohannes Berg struct ieee80211_local *local = sdata->local; 1067ce662b44SJohannes Berg struct ieee80211_qos_hdr *nullfunc; 1068ce662b44SJohannes Berg struct sk_buff *skb; 1069ce662b44SJohannes Berg int size = sizeof(*nullfunc); 1070ce662b44SJohannes Berg __le16 fc; 1071c2c98fdeSJohannes Berg bool qos = test_sta_flag(sta, WLAN_STA_WME); 1072ce662b44SJohannes Berg struct ieee80211_tx_info *info; 1073ce662b44SJohannes Berg 1074ce662b44SJohannes Berg if (qos) { 1075ce662b44SJohannes Berg fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1076ce662b44SJohannes Berg IEEE80211_STYPE_QOS_NULLFUNC | 1077ce662b44SJohannes Berg IEEE80211_FCTL_FROMDS); 1078ce662b44SJohannes Berg } else { 1079ce662b44SJohannes Berg size -= 2; 1080ce662b44SJohannes Berg fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1081ce662b44SJohannes Berg IEEE80211_STYPE_NULLFUNC | 1082ce662b44SJohannes Berg IEEE80211_FCTL_FROMDS); 1083ce662b44SJohannes Berg } 1084ce662b44SJohannes Berg 1085ce662b44SJohannes Berg skb = dev_alloc_skb(local->hw.extra_tx_headroom + size); 1086ce662b44SJohannes Berg if (!skb) 1087ce662b44SJohannes Berg return; 1088ce662b44SJohannes Berg 1089ce662b44SJohannes Berg skb_reserve(skb, local->hw.extra_tx_headroom); 1090ce662b44SJohannes Berg 1091ce662b44SJohannes Berg nullfunc = (void *) skb_put(skb, size); 1092ce662b44SJohannes Berg nullfunc->frame_control = fc; 1093ce662b44SJohannes Berg nullfunc->duration_id = 0; 1094ce662b44SJohannes Berg memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN); 1095ce662b44SJohannes Berg memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN); 1096ce662b44SJohannes Berg memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN); 1097ce662b44SJohannes Berg 1098ce662b44SJohannes Berg skb->priority = tid; 1099ce662b44SJohannes Berg skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]); 110059b66255SJohannes Berg if (qos) { 1101ce662b44SJohannes Berg nullfunc->qos_ctrl = cpu_to_le16(tid); 1102ce662b44SJohannes Berg 110340b96408SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_UAPSD) 1104ce662b44SJohannes Berg nullfunc->qos_ctrl |= 1105ce662b44SJohannes Berg cpu_to_le16(IEEE80211_QOS_CTL_EOSP); 1106ce662b44SJohannes Berg } 1107ce662b44SJohannes Berg 1108ce662b44SJohannes Berg info = IEEE80211_SKB_CB(skb); 1109ce662b44SJohannes Berg 1110ce662b44SJohannes Berg /* 1111ce662b44SJohannes Berg * Tell TX path to send this frame even though the 1112ce662b44SJohannes Berg * STA may still remain is PS mode after this frame 1113deeaee19SJohannes Berg * exchange. Also set EOSP to indicate this packet 1114deeaee19SJohannes Berg * ends the poll/service period. 1115ce662b44SJohannes Berg */ 1116deeaee19SJohannes Berg info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE | 1117deeaee19SJohannes Berg IEEE80211_TX_STATUS_EOSP | 1118ce662b44SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 1119ce662b44SJohannes Berg 112040b96408SJohannes Berg drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false); 112140b96408SJohannes Berg 1122ce662b44SJohannes Berg ieee80211_xmit(sdata, skb); 1123ce662b44SJohannes Berg } 1124ce662b44SJohannes Berg 112547086fc5SJohannes Berg static void 112647086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta, 112747086fc5SJohannes Berg int n_frames, u8 ignored_acs, 112847086fc5SJohannes Berg enum ieee80211_frame_release_type reason) 1129af818581SJohannes Berg { 1130af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1131af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 11324049e09aSJohannes Berg bool found = false; 1133948d887dSJohannes Berg bool more_data = false; 1134948d887dSJohannes Berg int ac; 11354049e09aSJohannes Berg unsigned long driver_release_tids = 0; 113647086fc5SJohannes Berg struct sk_buff_head frames; 113747086fc5SJohannes Berg 1138deeaee19SJohannes Berg /* Service or PS-Poll period starts */ 1139c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_SP); 1140deeaee19SJohannes Berg 114147086fc5SJohannes Berg __skb_queue_head_init(&frames); 1142af818581SJohannes Berg 1143948d887dSJohannes Berg /* 114447086fc5SJohannes Berg * Get response frame(s) and more data bit for it. 1145948d887dSJohannes Berg */ 1146948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 11474049e09aSJohannes Berg unsigned long tids; 11484049e09aSJohannes Berg 114947086fc5SJohannes Berg if (ignored_acs & BIT(ac)) 1150948d887dSJohannes Berg continue; 1151948d887dSJohannes Berg 11524049e09aSJohannes Berg tids = ieee80211_tids_for_ac(ac); 11534049e09aSJohannes Berg 11544049e09aSJohannes Berg if (!found) { 11554049e09aSJohannes Berg driver_release_tids = sta->driver_buffered_tids & tids; 11564049e09aSJohannes Berg if (driver_release_tids) { 11574049e09aSJohannes Berg found = true; 11584049e09aSJohannes Berg } else { 115947086fc5SJohannes Berg struct sk_buff *skb; 116047086fc5SJohannes Berg 116147086fc5SJohannes Berg while (n_frames > 0) { 1162948d887dSJohannes Berg skb = skb_dequeue(&sta->tx_filtered[ac]); 1163948d887dSJohannes Berg if (!skb) { 116447086fc5SJohannes Berg skb = skb_dequeue( 116547086fc5SJohannes Berg &sta->ps_tx_buf[ac]); 1166af818581SJohannes Berg if (skb) 1167af818581SJohannes Berg local->total_ps_buffered--; 1168af818581SJohannes Berg } 116947086fc5SJohannes Berg if (!skb) 117047086fc5SJohannes Berg break; 117147086fc5SJohannes Berg n_frames--; 11724049e09aSJohannes Berg found = true; 117347086fc5SJohannes Berg __skb_queue_tail(&frames, skb); 117447086fc5SJohannes Berg } 1175948d887dSJohannes Berg } 1176948d887dSJohannes Berg 11774049e09aSJohannes Berg /* 11784049e09aSJohannes Berg * If the driver has data on more than one TID then 11794049e09aSJohannes Berg * certainly there's more data if we release just a 11804049e09aSJohannes Berg * single frame now (from a single TID). 11814049e09aSJohannes Berg */ 118247086fc5SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_PSPOLL && 118347086fc5SJohannes Berg hweight16(driver_release_tids) > 1) { 11844049e09aSJohannes Berg more_data = true; 11854049e09aSJohannes Berg driver_release_tids = 11864049e09aSJohannes Berg BIT(ffs(driver_release_tids) - 1); 11874049e09aSJohannes Berg break; 11884049e09aSJohannes Berg } 11894049e09aSJohannes Berg } 1190948d887dSJohannes Berg 1191948d887dSJohannes Berg if (!skb_queue_empty(&sta->tx_filtered[ac]) || 1192948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac])) { 1193948d887dSJohannes Berg more_data = true; 1194948d887dSJohannes Berg break; 1195948d887dSJohannes Berg } 1196948d887dSJohannes Berg } 1197af818581SJohannes Berg 11984049e09aSJohannes Berg if (!found) { 1199ce662b44SJohannes Berg int tid; 12004049e09aSJohannes Berg 1201ce662b44SJohannes Berg /* 1202ce662b44SJohannes Berg * For PS-Poll, this can only happen due to a race condition 1203ce662b44SJohannes Berg * when we set the TIM bit and the station notices it, but 1204ce662b44SJohannes Berg * before it can poll for the frame we expire it. 1205ce662b44SJohannes Berg * 1206ce662b44SJohannes Berg * For uAPSD, this is said in the standard (11.2.1.5 h): 1207ce662b44SJohannes Berg * At each unscheduled SP for a non-AP STA, the AP shall 1208ce662b44SJohannes Berg * attempt to transmit at least one MSDU or MMPDU, but no 1209ce662b44SJohannes Berg * more than the value specified in the Max SP Length field 1210ce662b44SJohannes Berg * in the QoS Capability element from delivery-enabled ACs, 1211ce662b44SJohannes Berg * that are destined for the non-AP STA. 1212ce662b44SJohannes Berg * 1213ce662b44SJohannes Berg * Since we have no other MSDU/MMPDU, transmit a QoS null frame. 1214ce662b44SJohannes Berg */ 1215ce662b44SJohannes Berg 1216ce662b44SJohannes Berg /* This will evaluate to 1, 3, 5 or 7. */ 1217ce662b44SJohannes Berg tid = 7 - ((ffs(~ignored_acs) - 1) << 1); 1218ce662b44SJohannes Berg 121940b96408SJohannes Berg ieee80211_send_null_response(sdata, sta, tid, reason); 12204049e09aSJohannes Berg return; 12214049e09aSJohannes Berg } 12224049e09aSJohannes Berg 122347086fc5SJohannes Berg if (!driver_release_tids) { 122447086fc5SJohannes Berg struct sk_buff_head pending; 122547086fc5SJohannes Berg struct sk_buff *skb; 122640b96408SJohannes Berg int num = 0; 122740b96408SJohannes Berg u16 tids = 0; 122847086fc5SJohannes Berg 122947086fc5SJohannes Berg skb_queue_head_init(&pending); 123047086fc5SJohannes Berg 123147086fc5SJohannes Berg while ((skb = __skb_dequeue(&frames))) { 1232af818581SJohannes Berg struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 123347086fc5SJohannes Berg struct ieee80211_hdr *hdr = (void *) skb->data; 123440b96408SJohannes Berg u8 *qoshdr = NULL; 123540b96408SJohannes Berg 123640b96408SJohannes Berg num++; 1237af818581SJohannes Berg 1238af818581SJohannes Berg /* 123947086fc5SJohannes Berg * Tell TX path to send this frame even though the 124047086fc5SJohannes Berg * STA may still remain is PS mode after this frame 124147086fc5SJohannes Berg * exchange. 1242af818581SJohannes Berg */ 124347086fc5SJohannes Berg info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE; 1244af818581SJohannes Berg 124547086fc5SJohannes Berg /* 124647086fc5SJohannes Berg * Use MoreData flag to indicate whether there are 124747086fc5SJohannes Berg * more buffered frames for this STA 124847086fc5SJohannes Berg */ 124924b9c373SJanusz.Dziedzic@tieto.com if (more_data || !skb_queue_empty(&frames)) 125047086fc5SJohannes Berg hdr->frame_control |= 125147086fc5SJohannes Berg cpu_to_le16(IEEE80211_FCTL_MOREDATA); 125224b9c373SJanusz.Dziedzic@tieto.com else 125324b9c373SJanusz.Dziedzic@tieto.com hdr->frame_control &= 125424b9c373SJanusz.Dziedzic@tieto.com cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 1255af818581SJohannes Berg 125640b96408SJohannes Berg if (ieee80211_is_data_qos(hdr->frame_control) || 125740b96408SJohannes Berg ieee80211_is_qos_nullfunc(hdr->frame_control)) 125840b96408SJohannes Berg qoshdr = ieee80211_get_qos_ctl(hdr); 125940b96408SJohannes Berg 126047086fc5SJohannes Berg /* set EOSP for the frame */ 126140b96408SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_UAPSD && 126240b96408SJohannes Berg qoshdr && skb_queue_empty(&frames)) 126340b96408SJohannes Berg *qoshdr |= IEEE80211_QOS_CTL_EOSP; 1264deeaee19SJohannes Berg 126547086fc5SJohannes Berg info->flags |= IEEE80211_TX_STATUS_EOSP | 126647086fc5SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 126747086fc5SJohannes Berg 126840b96408SJohannes Berg if (qoshdr) 126940b96408SJohannes Berg tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK); 127040b96408SJohannes Berg else 127140b96408SJohannes Berg tids |= BIT(0); 127240b96408SJohannes Berg 127347086fc5SJohannes Berg __skb_queue_tail(&pending, skb); 127447086fc5SJohannes Berg } 127547086fc5SJohannes Berg 127640b96408SJohannes Berg drv_allow_buffered_frames(local, sta, tids, num, 127740b96408SJohannes Berg reason, more_data); 127840b96408SJohannes Berg 127947086fc5SJohannes Berg ieee80211_add_pending_skbs(local, &pending); 1280af818581SJohannes Berg 1281c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1282af818581SJohannes Berg } else { 1283af818581SJohannes Berg /* 12844049e09aSJohannes Berg * We need to release a frame that is buffered somewhere in the 12854049e09aSJohannes Berg * driver ... it'll have to handle that. 12864049e09aSJohannes Berg * Note that, as per the comment above, it'll also have to see 12874049e09aSJohannes Berg * if there is more than just one frame on the specific TID that 12884049e09aSJohannes Berg * we're releasing from, and it needs to set the more-data bit 12894049e09aSJohannes Berg * accordingly if we tell it that there's no more data. If we do 12904049e09aSJohannes Berg * tell it there's more data, then of course the more-data bit 12914049e09aSJohannes Berg * needs to be set anyway. 1292af818581SJohannes Berg */ 12934049e09aSJohannes Berg drv_release_buffered_frames(local, sta, driver_release_tids, 129447086fc5SJohannes Berg n_frames, reason, more_data); 12954049e09aSJohannes Berg 12964049e09aSJohannes Berg /* 12974049e09aSJohannes Berg * Note that we don't recalculate the TIM bit here as it would 12984049e09aSJohannes Berg * most likely have no effect at all unless the driver told us 12994049e09aSJohannes Berg * that the TID became empty before returning here from the 13004049e09aSJohannes Berg * release function. 13014049e09aSJohannes Berg * Either way, however, when the driver tells us that the TID 13024049e09aSJohannes Berg * became empty we'll do the TIM recalculation. 13034049e09aSJohannes Berg */ 1304af818581SJohannes Berg } 1305af818581SJohannes Berg } 1306af818581SJohannes Berg 1307af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 1308af818581SJohannes Berg { 130947086fc5SJohannes Berg u8 ignore_for_response = sta->sta.uapsd_queues; 1310af818581SJohannes Berg 1311af818581SJohannes Berg /* 131247086fc5SJohannes Berg * If all ACs are delivery-enabled then we should reply 131347086fc5SJohannes Berg * from any of them, if only some are enabled we reply 131447086fc5SJohannes Berg * only from the non-enabled ones. 1315af818581SJohannes Berg */ 131647086fc5SJohannes Berg if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1) 131747086fc5SJohannes Berg ignore_for_response = 0; 1318af818581SJohannes Berg 131947086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response, 132047086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_PSPOLL); 1321af818581SJohannes Berg } 132247086fc5SJohannes Berg 132347086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta) 132447086fc5SJohannes Berg { 132547086fc5SJohannes Berg int n_frames = sta->sta.max_sp; 132647086fc5SJohannes Berg u8 delivery_enabled = sta->sta.uapsd_queues; 132747086fc5SJohannes Berg 132847086fc5SJohannes Berg /* 132947086fc5SJohannes Berg * If we ever grow support for TSPEC this might happen if 133047086fc5SJohannes Berg * the TSPEC update from hostapd comes in between a trigger 133147086fc5SJohannes Berg * frame setting WLAN_STA_UAPSD in the RX path and this 133247086fc5SJohannes Berg * actually getting called. 133347086fc5SJohannes Berg */ 133447086fc5SJohannes Berg if (!delivery_enabled) 133547086fc5SJohannes Berg return; 133647086fc5SJohannes Berg 133747086fc5SJohannes Berg switch (sta->sta.max_sp) { 133847086fc5SJohannes Berg case 1: 133947086fc5SJohannes Berg n_frames = 2; 134047086fc5SJohannes Berg break; 134147086fc5SJohannes Berg case 2: 134247086fc5SJohannes Berg n_frames = 4; 134347086fc5SJohannes Berg break; 134447086fc5SJohannes Berg case 3: 134547086fc5SJohannes Berg n_frames = 6; 134647086fc5SJohannes Berg break; 134747086fc5SJohannes Berg case 0: 134847086fc5SJohannes Berg /* XXX: what is a good value? */ 134947086fc5SJohannes Berg n_frames = 8; 135047086fc5SJohannes Berg break; 135147086fc5SJohannes Berg } 135247086fc5SJohannes Berg 135347086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled, 135447086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_UAPSD); 1355af818581SJohannes Berg } 1356af818581SJohannes Berg 1357af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 1358af818581SJohannes Berg struct ieee80211_sta *pubsta, bool block) 1359af818581SJohannes Berg { 1360af818581SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1361af818581SJohannes Berg 1362b5878a2dSJohannes Berg trace_api_sta_block_awake(sta->local, pubsta, block); 1363b5878a2dSJohannes Berg 1364af818581SJohannes Berg if (block) 1365c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_PS_DRIVER); 1366c2c98fdeSJohannes Berg else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) 1367af818581SJohannes Berg ieee80211_queue_work(hw, &sta->drv_unblock_wk); 1368af818581SJohannes Berg } 1369af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake); 1370dcf55fb5SFelix Fietkau 137137fbd908SJohannes Berg void ieee80211_sta_eosp_irqsafe(struct ieee80211_sta *pubsta) 137237fbd908SJohannes Berg { 137337fbd908SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 137437fbd908SJohannes Berg struct ieee80211_local *local = sta->local; 137537fbd908SJohannes Berg struct sk_buff *skb; 137637fbd908SJohannes Berg struct skb_eosp_msg_data *data; 137737fbd908SJohannes Berg 137837fbd908SJohannes Berg trace_api_eosp(local, pubsta); 137937fbd908SJohannes Berg 138037fbd908SJohannes Berg skb = alloc_skb(0, GFP_ATOMIC); 138137fbd908SJohannes Berg if (!skb) { 138237fbd908SJohannes Berg /* too bad ... but race is better than loss */ 138337fbd908SJohannes Berg clear_sta_flag(sta, WLAN_STA_SP); 138437fbd908SJohannes Berg return; 138537fbd908SJohannes Berg } 138637fbd908SJohannes Berg 138737fbd908SJohannes Berg data = (void *)skb->cb; 138837fbd908SJohannes Berg memcpy(data->sta, pubsta->addr, ETH_ALEN); 138937fbd908SJohannes Berg memcpy(data->iface, sta->sdata->vif.addr, ETH_ALEN); 139037fbd908SJohannes Berg skb->pkt_type = IEEE80211_EOSP_MSG; 139137fbd908SJohannes Berg skb_queue_tail(&local->skb_queue, skb); 139237fbd908SJohannes Berg tasklet_schedule(&local->tasklet); 139337fbd908SJohannes Berg } 139437fbd908SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp_irqsafe); 139537fbd908SJohannes Berg 1396042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, 1397042ec453SJohannes Berg u8 tid, bool buffered) 1398dcf55fb5SFelix Fietkau { 1399dcf55fb5SFelix Fietkau struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1400dcf55fb5SFelix Fietkau 1401948d887dSJohannes Berg if (WARN_ON(tid >= STA_TID_NUM)) 1402042ec453SJohannes Berg return; 1403042ec453SJohannes Berg 1404948d887dSJohannes Berg if (buffered) 1405948d887dSJohannes Berg set_bit(tid, &sta->driver_buffered_tids); 1406948d887dSJohannes Berg else 1407948d887dSJohannes Berg clear_bit(tid, &sta->driver_buffered_tids); 1408948d887dSJohannes Berg 1409c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1410dcf55fb5SFelix Fietkau } 1411042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered); 1412d9a7ddb0SJohannes Berg 1413d9a7ddb0SJohannes Berg int sta_info_move_state_checked(struct sta_info *sta, 1414d9a7ddb0SJohannes Berg enum ieee80211_sta_state new_state) 1415d9a7ddb0SJohannes Berg { 14168bf11d8dSJohannes Berg might_sleep(); 1417d9a7ddb0SJohannes Berg 1418d9a7ddb0SJohannes Berg if (sta->sta_state == new_state) 1419d9a7ddb0SJohannes Berg return 0; 1420d9a7ddb0SJohannes Berg 1421d9a7ddb0SJohannes Berg switch (new_state) { 1422d9a7ddb0SJohannes Berg case IEEE80211_STA_NONE: 1423d9a7ddb0SJohannes Berg if (sta->sta_state == IEEE80211_STA_AUTH) 1424d9a7ddb0SJohannes Berg clear_bit(WLAN_STA_AUTH, &sta->_flags); 1425d9a7ddb0SJohannes Berg else 1426d9a7ddb0SJohannes Berg return -EINVAL; 1427d9a7ddb0SJohannes Berg break; 1428d9a7ddb0SJohannes Berg case IEEE80211_STA_AUTH: 1429d9a7ddb0SJohannes Berg if (sta->sta_state == IEEE80211_STA_NONE) 1430d9a7ddb0SJohannes Berg set_bit(WLAN_STA_AUTH, &sta->_flags); 1431d9a7ddb0SJohannes Berg else if (sta->sta_state == IEEE80211_STA_ASSOC) 1432d9a7ddb0SJohannes Berg clear_bit(WLAN_STA_ASSOC, &sta->_flags); 1433d9a7ddb0SJohannes Berg else 1434d9a7ddb0SJohannes Berg return -EINVAL; 1435d9a7ddb0SJohannes Berg break; 1436d9a7ddb0SJohannes Berg case IEEE80211_STA_ASSOC: 143729623892SJohannes Berg if (sta->sta_state == IEEE80211_STA_AUTH) { 1438d9a7ddb0SJohannes Berg set_bit(WLAN_STA_ASSOC, &sta->_flags); 143929623892SJohannes Berg } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) { 144029623892SJohannes Berg if (sta->sdata->vif.type == NL80211_IFTYPE_AP) 144129623892SJohannes Berg atomic_dec(&sta->sdata->u.ap.num_sta_authorized); 1442d9a7ddb0SJohannes Berg clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 144329623892SJohannes Berg } else 1444d9a7ddb0SJohannes Berg return -EINVAL; 1445d9a7ddb0SJohannes Berg break; 1446d9a7ddb0SJohannes Berg case IEEE80211_STA_AUTHORIZED: 144729623892SJohannes Berg if (sta->sta_state == IEEE80211_STA_ASSOC) { 144829623892SJohannes Berg if (sta->sdata->vif.type == NL80211_IFTYPE_AP) 144929623892SJohannes Berg atomic_inc(&sta->sdata->u.ap.num_sta_authorized); 1450d9a7ddb0SJohannes Berg set_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 145129623892SJohannes Berg } else 1452d9a7ddb0SJohannes Berg return -EINVAL; 1453d9a7ddb0SJohannes Berg break; 1454d9a7ddb0SJohannes Berg default: 1455d9a7ddb0SJohannes Berg WARN(1, "invalid state %d", new_state); 1456d9a7ddb0SJohannes Berg return -EINVAL; 1457d9a7ddb0SJohannes Berg } 1458d9a7ddb0SJohannes Berg 1459d9a7ddb0SJohannes Berg printk(KERN_DEBUG "%s: moving STA %pM to state %d\n", 1460d9a7ddb0SJohannes Berg sta->sdata->name, sta->sta.addr, new_state); 1461d9a7ddb0SJohannes Berg sta->sta_state = new_state; 1462d9a7ddb0SJohannes Berg 1463d9a7ddb0SJohannes Berg return 0; 1464d9a7ddb0SJohannes Berg } 1465