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" 27f0706e82SJiri Benc 28d0709a65SJohannes Berg /** 29d0709a65SJohannes Berg * DOC: STA information lifetime rules 30d0709a65SJohannes Berg * 31d0709a65SJohannes Berg * STA info structures (&struct sta_info) are managed in a hash table 32d0709a65SJohannes Berg * for faster lookup and a list for iteration. They are managed using 33d0709a65SJohannes Berg * RCU, i.e. access to the list and hash table is protected by RCU. 34d0709a65SJohannes Berg * 3534e89507SJohannes Berg * Upon allocating a STA info structure with sta_info_alloc(), the caller 3634e89507SJohannes Berg * owns that structure. It must then insert it into the hash table using 3734e89507SJohannes Berg * either sta_info_insert() or sta_info_insert_rcu(); only in the latter 3834e89507SJohannes Berg * case (which acquires an rcu read section but must not be called from 3934e89507SJohannes Berg * within one) will the pointer still be valid after the call. Note that 4034e89507SJohannes Berg * the caller may not do much with the STA info before inserting it, in 4134e89507SJohannes Berg * particular, it may not start any mesh peer link management or add 4234e89507SJohannes Berg * encryption keys. 4393e5deb1SJohannes Berg * 4493e5deb1SJohannes Berg * When the insertion fails (sta_info_insert()) returns non-zero), the 4593e5deb1SJohannes Berg * structure will have been freed by sta_info_insert()! 46d0709a65SJohannes Berg * 4734e89507SJohannes Berg * Station entries are added by mac80211 when you establish a link with a 487e189a12SLuis R. Rodriguez * peer. This means different things for the different type of interfaces 497e189a12SLuis R. Rodriguez * we support. For a regular station this mean we add the AP sta when we 5025985edcSLucas De Marchi * receive an association response from the AP. For IBSS this occurs when 5134e89507SJohannes Berg * get to know about a peer on the same IBSS. For WDS we add the sta for 5225985edcSLucas De Marchi * the peer immediately upon device open. When using AP mode we add stations 5334e89507SJohannes Berg * for each respective station upon request from userspace through nl80211. 547e189a12SLuis R. Rodriguez * 5534e89507SJohannes Berg * In order to remove a STA info structure, various sta_info_destroy_*() 5634e89507SJohannes Berg * calls are available. 57d0709a65SJohannes Berg * 5834e89507SJohannes Berg * There is no concept of ownership on a STA entry, each structure is 5934e89507SJohannes Berg * owned by the global hash table/list until it is removed. All users of 6034e89507SJohannes Berg * the structure need to be RCU protected so that the structure won't be 6134e89507SJohannes Berg * freed before they are done using it. 62d0709a65SJohannes Berg */ 63f0706e82SJiri Benc 64f0706e82SJiri Benc /* Caller must hold local->sta_lock */ 65be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local, 66f0706e82SJiri Benc struct sta_info *sta) 67f0706e82SJiri Benc { 68f0706e82SJiri Benc struct sta_info *s; 69f0706e82SJiri Benc 7040b275b6SJohannes Berg s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)], 7140b275b6SJohannes Berg lockdep_is_held(&local->sta_lock)); 72f0706e82SJiri Benc if (!s) 73be8755e1SMichael Wu return -ENOENT; 74be8755e1SMichael Wu if (s == sta) { 7517741cdcSJohannes Berg rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], 76d0709a65SJohannes Berg s->hnext); 77be8755e1SMichael Wu return 0; 78f0706e82SJiri Benc } 79f0706e82SJiri Benc 8040b275b6SJohannes Berg while (rcu_access_pointer(s->hnext) && 8140b275b6SJohannes Berg rcu_access_pointer(s->hnext) != sta) 8240b275b6SJohannes Berg s = rcu_dereference_protected(s->hnext, 8340b275b6SJohannes Berg lockdep_is_held(&local->sta_lock)); 8440b275b6SJohannes Berg if (rcu_access_pointer(s->hnext)) { 85d0709a65SJohannes Berg rcu_assign_pointer(s->hnext, sta->hnext); 86be8755e1SMichael Wu return 0; 87f0706e82SJiri Benc } 88f0706e82SJiri Benc 89be8755e1SMichael Wu return -ENOENT; 90f0706e82SJiri Benc } 91f0706e82SJiri Benc 92d0709a65SJohannes Berg /* protected by RCU */ 93abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata, 94abe60632SJohannes Berg const u8 *addr) 9543ba7e95SJohannes Berg { 96abe60632SJohannes Berg struct ieee80211_local *local = sdata->local; 9743ba7e95SJohannes Berg struct sta_info *sta; 9843ba7e95SJohannes Berg 990379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1000379185bSJohannes Berg lockdep_is_held(&local->sta_lock) || 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_lock) || 1082a33bee2SGuy Eilam lockdep_is_held(&local->sta_mtx)); 1092a33bee2SGuy Eilam } 1102a33bee2SGuy Eilam return sta; 1112a33bee2SGuy Eilam } 1122a33bee2SGuy Eilam 1132a33bee2SGuy Eilam /* get a station info entry even if it is a dummy station*/ 1142a33bee2SGuy Eilam struct sta_info *sta_info_get_rx(struct ieee80211_sub_if_data *sdata, 1152a33bee2SGuy Eilam const u8 *addr) 1162a33bee2SGuy Eilam { 1172a33bee2SGuy Eilam struct ieee80211_local *local = sdata->local; 1182a33bee2SGuy Eilam struct sta_info *sta; 1192a33bee2SGuy Eilam 1202a33bee2SGuy Eilam sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1212a33bee2SGuy Eilam lockdep_is_held(&local->sta_lock) || 1222a33bee2SGuy Eilam lockdep_is_held(&local->sta_mtx)); 1232a33bee2SGuy Eilam while (sta) { 124abe60632SJohannes Berg if (sta->sdata == sdata && 125abe60632SJohannes Berg memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 12643ba7e95SJohannes Berg break; 1270379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1280379185bSJohannes Berg lockdep_is_held(&local->sta_lock) || 1290379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 13043ba7e95SJohannes Berg } 13143ba7e95SJohannes Berg return sta; 13243ba7e95SJohannes Berg } 13343ba7e95SJohannes Berg 1340e5ded5aSFelix Fietkau /* 1350e5ded5aSFelix Fietkau * Get sta info either from the specified interface 1360e5ded5aSFelix Fietkau * or from one of its vlans 1370e5ded5aSFelix Fietkau */ 1380e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata, 1390e5ded5aSFelix Fietkau const u8 *addr) 1400e5ded5aSFelix Fietkau { 1410e5ded5aSFelix Fietkau struct ieee80211_local *local = sdata->local; 1420e5ded5aSFelix Fietkau struct sta_info *sta; 1430e5ded5aSFelix Fietkau 1440379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1450379185bSJohannes Berg lockdep_is_held(&local->sta_lock) || 1460379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1470e5ded5aSFelix Fietkau while (sta) { 1480e5ded5aSFelix Fietkau if ((sta->sdata == sdata || 149a2c1e3daSJohannes Berg (sta->sdata->bss && sta->sdata->bss == sdata->bss)) && 1502a33bee2SGuy Eilam !sta->dummy && 1512a33bee2SGuy Eilam memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 1522a33bee2SGuy Eilam break; 1532a33bee2SGuy Eilam sta = rcu_dereference_check(sta->hnext, 1542a33bee2SGuy Eilam lockdep_is_held(&local->sta_lock) || 1552a33bee2SGuy Eilam lockdep_is_held(&local->sta_mtx)); 1562a33bee2SGuy Eilam } 1572a33bee2SGuy Eilam return sta; 1582a33bee2SGuy Eilam } 1592a33bee2SGuy Eilam 1602a33bee2SGuy Eilam /* 1612a33bee2SGuy Eilam * Get sta info either from the specified interface 1622a33bee2SGuy Eilam * or from one of its vlans (including dummy stations) 1632a33bee2SGuy Eilam */ 1642a33bee2SGuy Eilam struct sta_info *sta_info_get_bss_rx(struct ieee80211_sub_if_data *sdata, 1652a33bee2SGuy Eilam const u8 *addr) 1662a33bee2SGuy Eilam { 1672a33bee2SGuy Eilam struct ieee80211_local *local = sdata->local; 1682a33bee2SGuy Eilam struct sta_info *sta; 1692a33bee2SGuy Eilam 1702a33bee2SGuy Eilam sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1712a33bee2SGuy Eilam lockdep_is_held(&local->sta_lock) || 1722a33bee2SGuy Eilam lockdep_is_held(&local->sta_mtx)); 1732a33bee2SGuy Eilam while (sta) { 1742a33bee2SGuy Eilam if ((sta->sdata == sdata || 1752a33bee2SGuy Eilam (sta->sdata->bss && sta->sdata->bss == sdata->bss)) && 1760e5ded5aSFelix Fietkau memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 1770e5ded5aSFelix Fietkau break; 1780379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1790379185bSJohannes Berg lockdep_is_held(&local->sta_lock) || 1800379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1810e5ded5aSFelix Fietkau } 1820e5ded5aSFelix Fietkau return sta; 1830e5ded5aSFelix Fietkau } 1840e5ded5aSFelix Fietkau 1853b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, 1863b53fde8SJohannes Berg int idx) 187ee385855SLuis Carlos Cobo { 1883b53fde8SJohannes Berg struct ieee80211_local *local = sdata->local; 189ee385855SLuis Carlos Cobo struct sta_info *sta; 190ee385855SLuis Carlos Cobo int i = 0; 191ee385855SLuis Carlos Cobo 192d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) { 1933b53fde8SJohannes Berg if (sdata != sta->sdata) 1942a8ca29aSLuis Carlos Cobo continue; 195ee385855SLuis Carlos Cobo if (i < idx) { 196ee385855SLuis Carlos Cobo ++i; 197ee385855SLuis Carlos Cobo continue; 198ee385855SLuis Carlos Cobo } 1992a8ca29aSLuis Carlos Cobo return sta; 200ee385855SLuis Carlos Cobo } 201ee385855SLuis Carlos Cobo 202ee385855SLuis Carlos Cobo return NULL; 203ee385855SLuis Carlos Cobo } 204f0706e82SJiri Benc 20593e5deb1SJohannes Berg /** 20693e5deb1SJohannes Berg * __sta_info_free - internal STA free helper 20793e5deb1SJohannes Berg * 2086ef307bcSRandy Dunlap * @local: pointer to the global information 20993e5deb1SJohannes Berg * @sta: STA info to free 21093e5deb1SJohannes Berg * 21193e5deb1SJohannes Berg * This function must undo everything done by sta_info_alloc() 21293e5deb1SJohannes Berg * that may happen before sta_info_insert(). 21393e5deb1SJohannes Berg */ 21493e5deb1SJohannes Berg static void __sta_info_free(struct ieee80211_local *local, 21593e5deb1SJohannes Berg struct sta_info *sta) 21693e5deb1SJohannes Berg { 217af65cd96SJohannes Berg if (sta->rate_ctrl) { 2184b7679a5SJohannes Berg rate_control_free_sta(sta); 21993e5deb1SJohannes Berg rate_control_put(sta->rate_ctrl); 220af65cd96SJohannes Berg } 22193e5deb1SJohannes Berg 22293e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 2230fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr); 22493e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 22593e5deb1SJohannes Berg 22693e5deb1SJohannes Berg kfree(sta); 22793e5deb1SJohannes Berg } 22893e5deb1SJohannes Berg 229d0709a65SJohannes Berg /* Caller must hold local->sta_lock */ 230d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local, 231d0709a65SJohannes Berg struct sta_info *sta) 232f0706e82SJiri Benc { 23317741cdcSJohannes Berg sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)]; 23417741cdcSJohannes Berg rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta); 235f0706e82SJiri Benc } 236f0706e82SJiri Benc 237af818581SJohannes Berg static void sta_unblock(struct work_struct *wk) 238af818581SJohannes Berg { 239af818581SJohannes Berg struct sta_info *sta; 240af818581SJohannes Berg 241af818581SJohannes Berg sta = container_of(wk, struct sta_info, drv_unblock_wk); 242af818581SJohannes Berg 243af818581SJohannes Berg if (sta->dead) 244af818581SJohannes Berg return; 245af818581SJohannes Berg 246af818581SJohannes Berg if (!test_sta_flags(sta, WLAN_STA_PS_STA)) 247af818581SJohannes Berg ieee80211_sta_ps_deliver_wakeup(sta); 24850a9432dSJohannes Berg else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL)) { 24950a9432dSJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER); 250af818581SJohannes Berg ieee80211_sta_ps_deliver_poll_response(sta); 251*47086fc5SJohannes Berg } else if (test_and_clear_sta_flags(sta, WLAN_STA_UAPSD)) { 252*47086fc5SJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER); 253*47086fc5SJohannes Berg ieee80211_sta_ps_deliver_uapsd(sta); 25450a9432dSJohannes Berg } else 25550a9432dSJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER); 256af818581SJohannes Berg } 257af818581SJohannes Berg 258af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local, 259af65cd96SJohannes Berg struct sta_info *sta, gfp_t gfp) 260af65cd96SJohannes Berg { 261af65cd96SJohannes Berg if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) 262af65cd96SJohannes Berg return 0; 263af65cd96SJohannes Berg 264af65cd96SJohannes Berg sta->rate_ctrl = rate_control_get(local->rate_ctrl); 265af65cd96SJohannes Berg sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, 266af65cd96SJohannes Berg &sta->sta, gfp); 267af65cd96SJohannes Berg if (!sta->rate_ctrl_priv) { 268af65cd96SJohannes Berg rate_control_put(sta->rate_ctrl); 269af65cd96SJohannes Berg return -ENOMEM; 270af65cd96SJohannes Berg } 271af65cd96SJohannes Berg 272af65cd96SJohannes Berg return 0; 273af65cd96SJohannes Berg } 274af65cd96SJohannes Berg 27573651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, 27673651ee6SJohannes Berg u8 *addr, gfp_t gfp) 277f0706e82SJiri Benc { 278d0709a65SJohannes Berg struct ieee80211_local *local = sdata->local; 279f0706e82SJiri Benc struct sta_info *sta; 280ebe27c91SMohammed Shafi Shajakhan struct timespec uptime; 28116c5f15cSRon Rindjunsky int i; 282f0706e82SJiri Benc 28317741cdcSJohannes Berg sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp); 284f0706e82SJiri Benc if (!sta) 28573651ee6SJohannes Berg return NULL; 286f0706e82SJiri Benc 28707346f81SJohannes Berg spin_lock_init(&sta->lock); 2885a9f7b04SJohannes Berg spin_lock_init(&sta->flaglock); 289af818581SJohannes Berg INIT_WORK(&sta->drv_unblock_wk, sta_unblock); 29067c282c0SJohannes Berg INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work); 291a93e3644SJohannes Berg mutex_init(&sta->ampdu_mlme.mtx); 29207346f81SJohannes Berg 29317741cdcSJohannes Berg memcpy(sta->sta.addr, addr, ETH_ALEN); 294d0709a65SJohannes Berg sta->local = local; 295d0709a65SJohannes Berg sta->sdata = sdata; 2968bc8aecdSFelix Fietkau sta->last_rx = jiffies; 297f0706e82SJiri Benc 298ebe27c91SMohammed Shafi Shajakhan do_posix_clock_monotonic_gettime(&uptime); 299ebe27c91SMohammed Shafi Shajakhan sta->last_connected = uptime.tv_sec; 300541a45a1SBruno Randolf ewma_init(&sta->avg_signal, 1024, 8); 301541a45a1SBruno Randolf 302af65cd96SJohannes Berg if (sta_prepare_rate_control(local, sta, gfp)) { 303f0706e82SJiri Benc kfree(sta); 30473651ee6SJohannes Berg return NULL; 305f0706e82SJiri Benc } 306f0706e82SJiri Benc 30716c5f15cSRon Rindjunsky for (i = 0; i < STA_TID_NUM; i++) { 308a622ab72SJohannes Berg /* 309a622ab72SJohannes Berg * timer_to_tid must be initialized with identity mapping 310a622ab72SJohannes Berg * to enable session_timer's data differentiation. See 311a622ab72SJohannes Berg * sta_rx_agg_session_timer_expired for usage. 312a622ab72SJohannes Berg */ 31316c5f15cSRon Rindjunsky sta->timer_to_tid[i] = i; 31416c5f15cSRon Rindjunsky } 315948d887dSJohannes Berg for (i = 0; i < IEEE80211_NUM_ACS; i++) { 316948d887dSJohannes Berg skb_queue_head_init(&sta->ps_tx_buf[i]); 317948d887dSJohannes Berg skb_queue_head_init(&sta->tx_filtered[i]); 318948d887dSJohannes Berg } 31973651ee6SJohannes Berg 320cccaec98SSenthil Balasubramanian for (i = 0; i < NUM_RX_DATA_QUEUES; i++) 3214be929beSAlexey Dobriyan sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); 322cccaec98SSenthil Balasubramanian 32373651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 3240fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr); 32573651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 32673651ee6SJohannes Berg 32703e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH 32857cf8043SJavier Cardona sta->plink_state = NL80211_PLINK_LISTEN; 32903e4497eSJohannes Berg init_timer(&sta->plink_timer); 33003e4497eSJohannes Berg #endif 33103e4497eSJohannes Berg 33273651ee6SJohannes Berg return sta; 33373651ee6SJohannes Berg } 33473651ee6SJohannes Berg 3352a33bee2SGuy Eilam static int sta_info_finish_insert(struct sta_info *sta, 3362a33bee2SGuy Eilam bool async, bool dummy_reinsert) 33773651ee6SJohannes Berg { 33873651ee6SJohannes Berg struct ieee80211_local *local = sta->local; 33973651ee6SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 34098b62183SJohannes Berg struct station_info sinfo; 34173651ee6SJohannes Berg unsigned long flags; 34293e5deb1SJohannes Berg int err = 0; 34373651ee6SJohannes Berg 34446a5ebafSJohannes Berg lockdep_assert_held(&local->sta_mtx); 34534e89507SJohannes Berg 3462a33bee2SGuy Eilam if (!sta->dummy || dummy_reinsert) { 34734e89507SJohannes Berg /* notify driver */ 34834e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 34934e89507SJohannes Berg sdata = container_of(sdata->bss, 35034e89507SJohannes Berg struct ieee80211_sub_if_data, 35134e89507SJohannes Berg u.ap); 35234e89507SJohannes Berg err = drv_sta_add(local, sdata, &sta->sta); 35334e89507SJohannes Berg if (err) { 35434e89507SJohannes Berg if (!async) 35534e89507SJohannes Berg return err; 3562a33bee2SGuy Eilam printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to " 3572a33bee2SGuy Eilam "driver (%d) - keeping it anyway.\n", 35834e89507SJohannes Berg sdata->name, sta->sta.addr, err); 35934e89507SJohannes Berg } else { 36034e89507SJohannes Berg sta->uploaded = true; 36134e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 36234e89507SJohannes Berg if (async) 3630fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, 3640fb9a9ecSJoe Perches "Finished adding IBSS STA %pM\n", 3650fb9a9ecSJoe Perches sta->sta.addr); 36634e89507SJohannes Berg #endif 36734e89507SJohannes Berg } 36834e89507SJohannes Berg 36934e89507SJohannes Berg sdata = sta->sdata; 3702a33bee2SGuy Eilam } 37134e89507SJohannes Berg 3722a33bee2SGuy Eilam if (!dummy_reinsert) { 37334e89507SJohannes Berg if (!async) { 37434e89507SJohannes Berg local->num_sta++; 37534e89507SJohannes Berg local->sta_generation++; 37634e89507SJohannes Berg smp_mb(); 37734e89507SJohannes Berg 37834e89507SJohannes Berg /* make the station visible */ 37934e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 38034e89507SJohannes Berg sta_info_hash_add(local, sta); 38134e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 38234e89507SJohannes Berg } 38334e89507SJohannes Berg 38434e89507SJohannes Berg list_add(&sta->list, &local->sta_list); 3852a33bee2SGuy Eilam } else { 3862a33bee2SGuy Eilam sta->dummy = false; 3872a33bee2SGuy Eilam } 38834e89507SJohannes Berg 3892a33bee2SGuy Eilam if (!sta->dummy) { 39034e89507SJohannes Berg ieee80211_sta_debugfs_add(sta); 39134e89507SJohannes Berg rate_control_add_sta_debugfs(sta); 39234e89507SJohannes Berg 393f612cedfSJouni Malinen memset(&sinfo, 0, sizeof(sinfo)); 39434e89507SJohannes Berg sinfo.filled = 0; 39534e89507SJohannes Berg sinfo.generation = local->sta_generation; 39634e89507SJohannes Berg cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL); 3972a33bee2SGuy Eilam } 39834e89507SJohannes Berg 39934e89507SJohannes Berg return 0; 40034e89507SJohannes Berg } 40134e89507SJohannes Berg 40234e89507SJohannes Berg static void sta_info_finish_pending(struct ieee80211_local *local) 40334e89507SJohannes Berg { 40434e89507SJohannes Berg struct sta_info *sta; 40534e89507SJohannes Berg unsigned long flags; 40634e89507SJohannes Berg 40734e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 40834e89507SJohannes Berg while (!list_empty(&local->sta_pending_list)) { 40934e89507SJohannes Berg sta = list_first_entry(&local->sta_pending_list, 41034e89507SJohannes Berg struct sta_info, list); 41134e89507SJohannes Berg list_del(&sta->list); 41234e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 41334e89507SJohannes Berg 4142a33bee2SGuy Eilam sta_info_finish_insert(sta, true, false); 41534e89507SJohannes Berg 41634e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 41734e89507SJohannes Berg } 41834e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 41934e89507SJohannes Berg } 42034e89507SJohannes Berg 42134e89507SJohannes Berg static void sta_info_finish_work(struct work_struct *work) 42234e89507SJohannes Berg { 42334e89507SJohannes Berg struct ieee80211_local *local = 42434e89507SJohannes Berg container_of(work, struct ieee80211_local, sta_finish_work); 42534e89507SJohannes Berg 42634e89507SJohannes Berg mutex_lock(&local->sta_mtx); 42734e89507SJohannes Berg sta_info_finish_pending(local); 42834e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 42934e89507SJohannes Berg } 43034e89507SJohannes Berg 4318c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta) 43234e89507SJohannes Berg { 43334e89507SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 43434e89507SJohannes Berg 43503e4497eSJohannes Berg /* 43603e4497eSJohannes Berg * Can't be a WARN_ON because it can be triggered through a race: 43703e4497eSJohannes Berg * something inserts a STA (on one CPU) without holding the RTNL 43803e4497eSJohannes Berg * and another CPU turns off the net device. 43903e4497eSJohannes Berg */ 4408c71df7aSGuy Eilam if (unlikely(!ieee80211_sdata_running(sdata))) 4418c71df7aSGuy Eilam return -ENETDOWN; 44203e4497eSJohannes Berg 44347846c9bSJohannes Berg if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 || 4448c71df7aSGuy Eilam is_multicast_ether_addr(sta->sta.addr))) 4458c71df7aSGuy Eilam return -EINVAL; 4468c71df7aSGuy Eilam 4478c71df7aSGuy Eilam return 0; 44893e5deb1SJohannes Berg } 44944213b5eSJohannes Berg 4508c71df7aSGuy Eilam static int sta_info_insert_ibss(struct sta_info *sta) __acquires(RCU) 4518c71df7aSGuy Eilam { 4528c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 4538c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 4548c71df7aSGuy Eilam unsigned long flags; 4558c71df7aSGuy Eilam 456d0709a65SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 45743ba7e95SJohannes Berg /* check if STA exists already */ 4582a33bee2SGuy Eilam if (sta_info_get_bss_rx(sdata, sta->sta.addr)) { 459d0709a65SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 46034e89507SJohannes Berg rcu_read_lock(); 4618c71df7aSGuy Eilam return -EEXIST; 46243ba7e95SJohannes Berg } 46334e89507SJohannes Berg 464f0706e82SJiri Benc local->num_sta++; 46534e89507SJohannes Berg local->sta_generation++; 46634e89507SJohannes Berg smp_mb(); 467f0706e82SJiri Benc sta_info_hash_add(local, sta); 46832bfd35dSJohannes Berg 46934e89507SJohannes Berg list_add_tail(&sta->list, &local->sta_pending_list); 47032bfd35dSJohannes Berg 47134e89507SJohannes Berg rcu_read_lock(); 47234e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 47334e89507SJohannes Berg 47434e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 4750fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n", 4760fb9a9ecSJoe Perches sta->sta.addr); 47734e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 47834e89507SJohannes Berg 47934e89507SJohannes Berg ieee80211_queue_work(&local->hw, &local->sta_finish_work); 48034e89507SJohannes Berg 48134e89507SJohannes Berg return 0; 48234e89507SJohannes Berg } 48334e89507SJohannes Berg 48434e89507SJohannes Berg /* 4858c71df7aSGuy Eilam * should be called with sta_mtx locked 4868c71df7aSGuy Eilam * this function replaces the mutex lock 4878c71df7aSGuy Eilam * with a RCU lock 4888c71df7aSGuy Eilam */ 4898c71df7aSGuy Eilam static int sta_info_insert_non_ibss(struct sta_info *sta) __acquires(RCU) 4908c71df7aSGuy Eilam { 4918c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 4928c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 4938c71df7aSGuy Eilam unsigned long flags; 4942a33bee2SGuy Eilam struct sta_info *exist_sta; 4952a33bee2SGuy Eilam bool dummy_reinsert = false; 4968c71df7aSGuy Eilam int err = 0; 4978c71df7aSGuy Eilam 4988c71df7aSGuy Eilam lockdep_assert_held(&local->sta_mtx); 4998c71df7aSGuy Eilam 5008c71df7aSGuy Eilam /* 50134e89507SJohannes Berg * On first glance, this will look racy, because the code 5028c71df7aSGuy Eilam * in this function, which inserts a station with sleeping, 50334e89507SJohannes Berg * unlocks the sta_lock between checking existence in the 50434e89507SJohannes Berg * hash table and inserting into it. 50534e89507SJohannes Berg * 50634e89507SJohannes Berg * However, it is not racy against itself because it keeps 5078c71df7aSGuy Eilam * the mutex locked. 50834e89507SJohannes Berg */ 50934e89507SJohannes Berg 51034e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 5112a33bee2SGuy Eilam /* 5122a33bee2SGuy Eilam * check if STA exists already. 5132a33bee2SGuy Eilam * only accept a scenario of a second call to sta_info_insert_non_ibss 5142a33bee2SGuy Eilam * with a dummy station entry that was inserted earlier 5152a33bee2SGuy Eilam * in that case - assume that the dummy station flag should 5162a33bee2SGuy Eilam * be removed. 5172a33bee2SGuy Eilam */ 5182a33bee2SGuy Eilam exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr); 5192a33bee2SGuy Eilam if (exist_sta) { 5202a33bee2SGuy Eilam if (exist_sta == sta && sta->dummy) { 5212a33bee2SGuy Eilam dummy_reinsert = true; 5222a33bee2SGuy Eilam } else { 52334e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 52438a679a5SJouni Malinen mutex_unlock(&local->sta_mtx); 52534e89507SJohannes Berg rcu_read_lock(); 5268c71df7aSGuy Eilam return -EEXIST; 52734e89507SJohannes Berg } 5282a33bee2SGuy Eilam } 52934e89507SJohannes Berg 53034e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 53134e89507SJohannes Berg 5322a33bee2SGuy Eilam err = sta_info_finish_insert(sta, false, dummy_reinsert); 53334e89507SJohannes Berg if (err) { 53434e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 53534e89507SJohannes Berg rcu_read_lock(); 5368c71df7aSGuy Eilam return err; 53732bfd35dSJohannes Berg } 538d0709a65SJohannes Berg 539f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 5402a33bee2SGuy Eilam wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n", 5412a33bee2SGuy Eilam sta->dummy ? "dummy " : "", sta->sta.addr); 542f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 543f0706e82SJiri Benc 54434e89507SJohannes Berg /* move reference to rcu-protected */ 54534e89507SJohannes Berg rcu_read_lock(); 54634e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 547e9f207f0SJiri Benc 54873651ee6SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 54973651ee6SJohannes Berg mesh_accept_plinks_update(sdata); 55073651ee6SJohannes Berg 55173651ee6SJohannes Berg return 0; 5528c71df7aSGuy Eilam } 5538c71df7aSGuy Eilam 5548c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) 5558c71df7aSGuy Eilam { 5568c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 5578c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 5588c71df7aSGuy Eilam int err = 0; 5598c71df7aSGuy Eilam 5608c71df7aSGuy Eilam err = sta_info_insert_check(sta); 5618c71df7aSGuy Eilam if (err) { 5628c71df7aSGuy Eilam rcu_read_lock(); 5638c71df7aSGuy Eilam goto out_free; 5648c71df7aSGuy Eilam } 5658c71df7aSGuy Eilam 5668c71df7aSGuy Eilam /* 5678c71df7aSGuy Eilam * In ad-hoc mode, we sometimes need to insert stations 5688c71df7aSGuy Eilam * from tasklet context from the RX path. To avoid races, 5698c71df7aSGuy Eilam * always do so in that case -- see the comment below. 5708c71df7aSGuy Eilam */ 5718c71df7aSGuy Eilam if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { 5728c71df7aSGuy Eilam err = sta_info_insert_ibss(sta); 5738c71df7aSGuy Eilam if (err) 5748c71df7aSGuy Eilam goto out_free; 5758c71df7aSGuy Eilam 5768c71df7aSGuy Eilam return 0; 5778c71df7aSGuy Eilam } 5788c71df7aSGuy Eilam 5798c71df7aSGuy Eilam /* 5808c71df7aSGuy Eilam * It might seem that the function called below is in race against 5818c71df7aSGuy Eilam * the function call above that atomically inserts the station... That, 5828c71df7aSGuy Eilam * however, is not true because the above code can only 5838c71df7aSGuy Eilam * be invoked for IBSS interfaces, and the below code will 5848c71df7aSGuy Eilam * not be -- and the two do not race against each other as 5858c71df7aSGuy Eilam * the hash table also keys off the interface. 5868c71df7aSGuy Eilam */ 5878c71df7aSGuy Eilam 5888c71df7aSGuy Eilam might_sleep(); 5898c71df7aSGuy Eilam 5908c71df7aSGuy Eilam mutex_lock(&local->sta_mtx); 5918c71df7aSGuy Eilam 5928c71df7aSGuy Eilam err = sta_info_insert_non_ibss(sta); 5938c71df7aSGuy Eilam if (err) 5948c71df7aSGuy Eilam goto out_free; 5958c71df7aSGuy Eilam 5968c71df7aSGuy Eilam return 0; 59793e5deb1SJohannes Berg out_free: 59893e5deb1SJohannes Berg BUG_ON(!err); 59993e5deb1SJohannes Berg __sta_info_free(local, sta); 60093e5deb1SJohannes Berg return err; 601f0706e82SJiri Benc } 602f0706e82SJiri Benc 60334e89507SJohannes Berg int sta_info_insert(struct sta_info *sta) 60434e89507SJohannes Berg { 60534e89507SJohannes Berg int err = sta_info_insert_rcu(sta); 60634e89507SJohannes Berg 60734e89507SJohannes Berg rcu_read_unlock(); 60834e89507SJohannes Berg 60934e89507SJohannes Berg return err; 61034e89507SJohannes Berg } 61134e89507SJohannes Berg 6122a33bee2SGuy Eilam /* Caller must hold sta->local->sta_mtx */ 6132a33bee2SGuy Eilam int sta_info_reinsert(struct sta_info *sta) 6142a33bee2SGuy Eilam { 6152a33bee2SGuy Eilam struct ieee80211_local *local = sta->local; 6162a33bee2SGuy Eilam int err = 0; 6172a33bee2SGuy Eilam 6182a33bee2SGuy Eilam err = sta_info_insert_check(sta); 6192a33bee2SGuy Eilam if (err) { 6202a33bee2SGuy Eilam mutex_unlock(&local->sta_mtx); 6212a33bee2SGuy Eilam return err; 6222a33bee2SGuy Eilam } 6232a33bee2SGuy Eilam 6242a33bee2SGuy Eilam might_sleep(); 6252a33bee2SGuy Eilam 6262a33bee2SGuy Eilam err = sta_info_insert_non_ibss(sta); 6272a33bee2SGuy Eilam rcu_read_unlock(); 6282a33bee2SGuy Eilam return err; 6292a33bee2SGuy Eilam } 6302a33bee2SGuy Eilam 631004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid) 632004c872eSJohannes Berg { 633004c872eSJohannes Berg /* 634004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 635004c872eSJohannes Berg * so this line may not be changed to use the __set_bit() format. 636004c872eSJohannes Berg */ 637004c872eSJohannes Berg bss->tim[aid / 8] |= (1 << (aid % 8)); 638004c872eSJohannes Berg } 639004c872eSJohannes Berg 640004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid) 641004c872eSJohannes Berg { 642004c872eSJohannes Berg /* 643004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 644004c872eSJohannes Berg * so this line may not be changed to use the __clear_bit() format. 645004c872eSJohannes Berg */ 646004c872eSJohannes Berg bss->tim[aid / 8] &= ~(1 << (aid % 8)); 647004c872eSJohannes Berg } 648004c872eSJohannes Berg 649948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac) 650948d887dSJohannes Berg { 651948d887dSJohannes Berg /* If we ever support TIDs > 7, this obviously needs to be adjusted */ 652948d887dSJohannes Berg switch (ac) { 653948d887dSJohannes Berg case IEEE80211_AC_VO: 654948d887dSJohannes Berg return BIT(6) | BIT(7); 655948d887dSJohannes Berg case IEEE80211_AC_VI: 656948d887dSJohannes Berg return BIT(4) | BIT(5); 657948d887dSJohannes Berg case IEEE80211_AC_BE: 658948d887dSJohannes Berg return BIT(0) | BIT(3); 659948d887dSJohannes Berg case IEEE80211_AC_BK: 660948d887dSJohannes Berg return BIT(1) | BIT(2); 661948d887dSJohannes Berg default: 662948d887dSJohannes Berg WARN_ON(1); 663948d887dSJohannes Berg return 0; 664948d887dSJohannes Berg } 665948d887dSJohannes Berg } 666948d887dSJohannes Berg 667c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta) 668004c872eSJohannes Berg { 669c868cb35SJohannes Berg struct ieee80211_local *local = sta->local; 670c868cb35SJohannes Berg struct ieee80211_if_ap *bss = sta->sdata->bss; 671d0709a65SJohannes Berg unsigned long flags; 672948d887dSJohannes Berg bool indicate_tim = false; 673948d887dSJohannes Berg u8 ignore_for_tim = sta->sta.uapsd_queues; 674948d887dSJohannes Berg int ac; 675004c872eSJohannes Berg 676c868cb35SJohannes Berg if (WARN_ON_ONCE(!sta->sdata->bss)) 677c868cb35SJohannes Berg return; 6783e122be0SJohannes Berg 679c868cb35SJohannes Berg /* No need to do anything if the driver does all */ 680c868cb35SJohannes Berg if (local->hw.flags & IEEE80211_HW_AP_LINK_PS) 681c868cb35SJohannes Berg return; 682004c872eSJohannes Berg 683c868cb35SJohannes Berg if (sta->dead) 684c868cb35SJohannes Berg goto done; 6853e122be0SJohannes Berg 686948d887dSJohannes Berg /* 687948d887dSJohannes Berg * If all ACs are delivery-enabled then we should build 688948d887dSJohannes Berg * the TIM bit for all ACs anyway; if only some are then 689948d887dSJohannes Berg * we ignore those and build the TIM bit using only the 690948d887dSJohannes Berg * non-enabled ones. 691948d887dSJohannes Berg */ 692948d887dSJohannes Berg if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1) 693948d887dSJohannes Berg ignore_for_tim = 0; 694948d887dSJohannes Berg 695948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 696948d887dSJohannes Berg unsigned long tids; 697948d887dSJohannes Berg 698948d887dSJohannes Berg if (ignore_for_tim & BIT(ac)) 699948d887dSJohannes Berg continue; 700948d887dSJohannes Berg 701948d887dSJohannes Berg indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) || 702948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac]); 703948d887dSJohannes Berg if (indicate_tim) 704948d887dSJohannes Berg break; 705948d887dSJohannes Berg 706948d887dSJohannes Berg tids = ieee80211_tids_for_ac(ac); 707948d887dSJohannes Berg 708948d887dSJohannes Berg indicate_tim |= 709948d887dSJohannes Berg sta->driver_buffered_tids & tids; 710948d887dSJohannes Berg } 711c868cb35SJohannes Berg 712c868cb35SJohannes Berg done: 713c868cb35SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 714c868cb35SJohannes Berg 715948d887dSJohannes Berg if (indicate_tim) 716c868cb35SJohannes Berg __bss_tim_set(bss, sta->sta.aid); 717c868cb35SJohannes Berg else 71817741cdcSJohannes Berg __bss_tim_clear(bss, sta->sta.aid); 7193e122be0SJohannes Berg 720c868cb35SJohannes Berg if (local->ops->set_tim) { 721c868cb35SJohannes Berg local->tim_in_locked_section = true; 722948d887dSJohannes Berg drv_set_tim(local, &sta->sta, indicate_tim); 723c868cb35SJohannes Berg local->tim_in_locked_section = false; 724004c872eSJohannes Berg } 725004c872eSJohannes Berg 726c868cb35SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 727004c872eSJohannes Berg } 728004c872eSJohannes Berg 729cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb) 730f0706e82SJiri Benc { 731e039fa4aSJohannes Berg struct ieee80211_tx_info *info; 732f0706e82SJiri Benc int timeout; 733f0706e82SJiri Benc 734f0706e82SJiri Benc if (!skb) 735cd0b8d89SJohannes Berg return false; 736f0706e82SJiri Benc 737e039fa4aSJohannes Berg info = IEEE80211_SKB_CB(skb); 738f0706e82SJiri Benc 739f0706e82SJiri Benc /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 74057c4d7b4SJohannes Berg timeout = (sta->listen_interval * 74157c4d7b4SJohannes Berg sta->sdata->vif.bss_conf.beacon_int * 74257c4d7b4SJohannes Berg 32 / 15625) * HZ; 743f0706e82SJiri Benc if (timeout < STA_TX_BUFFER_EXPIRE) 744f0706e82SJiri Benc timeout = STA_TX_BUFFER_EXPIRE; 745e039fa4aSJohannes Berg return time_after(jiffies, info->control.jiffies + timeout); 746f0706e82SJiri Benc } 747f0706e82SJiri Benc 748f0706e82SJiri Benc 749948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local, 750948d887dSJohannes Berg struct sta_info *sta, int ac) 751f0706e82SJiri Benc { 752f0706e82SJiri Benc unsigned long flags; 753f0706e82SJiri Benc struct sk_buff *skb; 754f0706e82SJiri Benc 75560750397SJohannes Berg /* 75660750397SJohannes Berg * First check for frames that should expire on the filtered 75760750397SJohannes Berg * queue. Frames here were rejected by the driver and are on 75860750397SJohannes Berg * a separate queue to avoid reordering with normal PS-buffered 75960750397SJohannes Berg * frames. They also aren't accounted for right now in the 76060750397SJohannes Berg * total_ps_buffered counter. 76160750397SJohannes Berg */ 76260750397SJohannes Berg for (;;) { 763948d887dSJohannes Berg spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 764948d887dSJohannes Berg skb = skb_peek(&sta->tx_filtered[ac]); 76560750397SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 766948d887dSJohannes Berg skb = __skb_dequeue(&sta->tx_filtered[ac]); 76760750397SJohannes Berg else 76860750397SJohannes Berg skb = NULL; 769948d887dSJohannes Berg spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 77060750397SJohannes Berg 77160750397SJohannes Berg /* 77260750397SJohannes Berg * Frames are queued in order, so if this one 77360750397SJohannes Berg * hasn't expired yet we can stop testing. If 77460750397SJohannes Berg * we actually reached the end of the queue we 77560750397SJohannes Berg * also need to stop, of course. 77660750397SJohannes Berg */ 77760750397SJohannes Berg if (!skb) 77860750397SJohannes Berg break; 77960750397SJohannes Berg dev_kfree_skb(skb); 78060750397SJohannes Berg } 78160750397SJohannes Berg 78260750397SJohannes Berg /* 78360750397SJohannes Berg * Now also check the normal PS-buffered queue, this will 78460750397SJohannes Berg * only find something if the filtered queue was emptied 78560750397SJohannes Berg * since the filtered frames are all before the normal PS 78660750397SJohannes Berg * buffered frames. 78760750397SJohannes Berg */ 788f0706e82SJiri Benc for (;;) { 789948d887dSJohannes Berg spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 790948d887dSJohannes Berg skb = skb_peek(&sta->ps_tx_buf[ac]); 79157c4d7b4SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 792948d887dSJohannes Berg skb = __skb_dequeue(&sta->ps_tx_buf[ac]); 793836341a7SJohannes Berg else 794f0706e82SJiri Benc skb = NULL; 795948d887dSJohannes Berg spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 796f0706e82SJiri Benc 79760750397SJohannes Berg /* 79860750397SJohannes Berg * frames are queued in order, so if this one 79960750397SJohannes Berg * hasn't expired yet (or we reached the end of 80060750397SJohannes Berg * the queue) we can stop testing 80160750397SJohannes Berg */ 802836341a7SJohannes Berg if (!skb) 803836341a7SJohannes Berg break; 804836341a7SJohannes Berg 805f0706e82SJiri Benc local->total_ps_buffered--; 806f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 8070c68ae26SJohannes Berg printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n", 8080c68ae26SJohannes Berg sta->sta.addr); 809f4ea83ddSJohannes Berg #endif 810f0706e82SJiri Benc dev_kfree_skb(skb); 811f0706e82SJiri Benc } 8123393a608SJuuso Oikarinen 81360750397SJohannes Berg /* 81460750397SJohannes Berg * Finally, recalculate the TIM bit for this station -- it might 81560750397SJohannes Berg * now be clear because the station was too slow to retrieve its 81660750397SJohannes Berg * frames. 81760750397SJohannes Berg */ 81860750397SJohannes Berg sta_info_recalc_tim(sta); 81960750397SJohannes Berg 82060750397SJohannes Berg /* 82160750397SJohannes Berg * Return whether there are any frames still buffered, this is 82260750397SJohannes Berg * used to check whether the cleanup timer still needs to run, 82360750397SJohannes Berg * if there are no frames we don't need to rearm the timer. 82460750397SJohannes Berg */ 825948d887dSJohannes Berg return !(skb_queue_empty(&sta->ps_tx_buf[ac]) && 826948d887dSJohannes Berg skb_queue_empty(&sta->tx_filtered[ac])); 827948d887dSJohannes Berg } 828948d887dSJohannes Berg 829948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 830948d887dSJohannes Berg struct sta_info *sta) 831948d887dSJohannes Berg { 832948d887dSJohannes Berg bool have_buffered = false; 833948d887dSJohannes Berg int ac; 834948d887dSJohannes Berg 835948d887dSJohannes Berg /* This is only necessary for stations on BSS interfaces */ 836948d887dSJohannes Berg if (!sta->sdata->bss) 837948d887dSJohannes Berg return false; 838948d887dSJohannes Berg 839948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 840948d887dSJohannes Berg have_buffered |= 841948d887dSJohannes Berg sta_info_cleanup_expire_buffered_ac(local, sta, ac); 842948d887dSJohannes Berg 843948d887dSJohannes Berg return have_buffered; 844f0706e82SJiri Benc } 845f0706e82SJiri Benc 84634e89507SJohannes Berg static int __must_check __sta_info_destroy(struct sta_info *sta) 84734e89507SJohannes Berg { 84834e89507SJohannes Berg struct ieee80211_local *local; 84934e89507SJohannes Berg struct ieee80211_sub_if_data *sdata; 85034e89507SJohannes Berg unsigned long flags; 851948d887dSJohannes Berg int ret, i, ac; 85234e89507SJohannes Berg 85334e89507SJohannes Berg might_sleep(); 85434e89507SJohannes Berg 85534e89507SJohannes Berg if (!sta) 85634e89507SJohannes Berg return -ENOENT; 85734e89507SJohannes Berg 85834e89507SJohannes Berg local = sta->local; 85934e89507SJohannes Berg sdata = sta->sdata; 86034e89507SJohannes Berg 861098a6070SJohannes Berg /* 862098a6070SJohannes Berg * Before removing the station from the driver and 863098a6070SJohannes Berg * rate control, it might still start new aggregation 864098a6070SJohannes Berg * sessions -- block that to make sure the tear-down 865098a6070SJohannes Berg * will be sufficient. 866098a6070SJohannes Berg */ 867098a6070SJohannes Berg set_sta_flags(sta, WLAN_STA_BLOCK_BA); 86853f73c09SJohannes Berg ieee80211_sta_tear_down_BA_sessions(sta, true); 869098a6070SJohannes Berg 87034e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 87134e89507SJohannes Berg ret = sta_info_hash_del(local, sta); 87234e89507SJohannes Berg /* this might still be the pending list ... which is fine */ 87334e89507SJohannes Berg if (!ret) 87434e89507SJohannes Berg list_del(&sta->list); 87534e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 87634e89507SJohannes Berg if (ret) 87734e89507SJohannes Berg return ret; 87834e89507SJohannes Berg 8798cb23153SJohannes Berg mutex_lock(&local->key_mtx); 880e31b8213SJohannes Berg for (i = 0; i < NUM_DEFAULT_KEYS; i++) 88140b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i])); 882e31b8213SJohannes Berg if (sta->ptk) 88340b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->ptk)); 8848cb23153SJohannes Berg mutex_unlock(&local->key_mtx); 88534e89507SJohannes Berg 88634e89507SJohannes Berg sta->dead = true; 88734e89507SJohannes Berg 88834e89507SJohannes Berg if (test_and_clear_sta_flags(sta, 88934e89507SJohannes Berg WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) { 89034e89507SJohannes Berg BUG_ON(!sdata->bss); 89134e89507SJohannes Berg 89234e89507SJohannes Berg atomic_dec(&sdata->bss->num_sta_ps); 893c868cb35SJohannes Berg sta_info_recalc_tim(sta); 89434e89507SJohannes Berg } 89534e89507SJohannes Berg 89634e89507SJohannes Berg local->num_sta--; 89734e89507SJohannes Berg local->sta_generation++; 89834e89507SJohannes Berg 89934e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 90034e89507SJohannes Berg rcu_assign_pointer(sdata->u.vlan.sta, NULL); 90134e89507SJohannes Berg 90234e89507SJohannes Berg if (sta->uploaded) { 90334e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 90434e89507SJohannes Berg sdata = container_of(sdata->bss, 90534e89507SJohannes Berg struct ieee80211_sub_if_data, 90634e89507SJohannes Berg u.ap); 90734e89507SJohannes Berg drv_sta_remove(local, sdata, &sta->sta); 90834e89507SJohannes Berg sdata = sta->sdata; 90934e89507SJohannes Berg } 91034e89507SJohannes Berg 911e64b3795SJohannes Berg /* 912e64b3795SJohannes Berg * At this point, after we wait for an RCU grace period, 913e64b3795SJohannes Berg * neither mac80211 nor the driver can reference this 914e64b3795SJohannes Berg * sta struct any more except by still existing timers 915e64b3795SJohannes Berg * associated with this station that we clean up below. 916e64b3795SJohannes Berg */ 917e64b3795SJohannes Berg synchronize_rcu(); 918e64b3795SJohannes Berg 919948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 920948d887dSJohannes Berg local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]); 921948d887dSJohannes Berg __skb_queue_purge(&sta->ps_tx_buf[ac]); 922948d887dSJohannes Berg __skb_queue_purge(&sta->tx_filtered[ac]); 923948d887dSJohannes Berg } 924c868cb35SJohannes Berg 92534e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH 926e64b3795SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 92734e89507SJohannes Berg mesh_accept_plinks_update(sdata); 92834e89507SJohannes Berg #endif 92934e89507SJohannes Berg 93034e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 9310fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr); 93234e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 93334e89507SJohannes Berg cancel_work_sync(&sta->drv_unblock_wk); 93434e89507SJohannes Berg 935ec15e68bSJouni Malinen cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL); 936ec15e68bSJouni Malinen 93734e89507SJohannes Berg rate_control_remove_sta_debugfs(sta); 93834e89507SJohannes Berg ieee80211_sta_debugfs_remove(sta); 93934e89507SJohannes Berg 94034e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH 94134e89507SJohannes Berg if (ieee80211_vif_is_mesh(&sta->sdata->vif)) { 94234e89507SJohannes Berg mesh_plink_deactivate(sta); 94334e89507SJohannes Berg del_timer_sync(&sta->plink_timer); 94434e89507SJohannes Berg } 94534e89507SJohannes Berg #endif 94634e89507SJohannes Berg 94734e89507SJohannes Berg __sta_info_free(local, sta); 94834e89507SJohannes Berg 94934e89507SJohannes Berg return 0; 95034e89507SJohannes Berg } 95134e89507SJohannes Berg 95234e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) 95334e89507SJohannes Berg { 95434e89507SJohannes Berg struct sta_info *sta; 95534e89507SJohannes Berg int ret; 95634e89507SJohannes Berg 95734e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 9582a33bee2SGuy Eilam sta = sta_info_get_rx(sdata, addr); 95934e89507SJohannes Berg ret = __sta_info_destroy(sta); 96034e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 96134e89507SJohannes Berg 96234e89507SJohannes Berg return ret; 96334e89507SJohannes Berg } 96434e89507SJohannes Berg 96534e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, 96634e89507SJohannes Berg const u8 *addr) 96734e89507SJohannes Berg { 96834e89507SJohannes Berg struct sta_info *sta; 96934e89507SJohannes Berg int ret; 97034e89507SJohannes Berg 97134e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 9722a33bee2SGuy Eilam sta = sta_info_get_bss_rx(sdata, addr); 97334e89507SJohannes Berg ret = __sta_info_destroy(sta); 97434e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 97534e89507SJohannes Berg 97634e89507SJohannes Berg return ret; 97734e89507SJohannes Berg } 978f0706e82SJiri Benc 979f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data) 980f0706e82SJiri Benc { 981f0706e82SJiri Benc struct ieee80211_local *local = (struct ieee80211_local *) data; 982f0706e82SJiri Benc struct sta_info *sta; 9833393a608SJuuso Oikarinen bool timer_needed = false; 984f0706e82SJiri Benc 985d0709a65SJohannes Berg rcu_read_lock(); 986d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) 9873393a608SJuuso Oikarinen if (sta_info_cleanup_expire_buffered(local, sta)) 9883393a608SJuuso Oikarinen timer_needed = true; 989d0709a65SJohannes Berg rcu_read_unlock(); 990f0706e82SJiri Benc 9915bb644a0SJohannes Berg if (local->quiescing) 9925bb644a0SJohannes Berg return; 9935bb644a0SJohannes Berg 9943393a608SJuuso Oikarinen if (!timer_needed) 9953393a608SJuuso Oikarinen return; 9963393a608SJuuso Oikarinen 99726d59535SJohannes Berg mod_timer(&local->sta_cleanup, 99826d59535SJohannes Berg round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 999f0706e82SJiri Benc } 1000f0706e82SJiri Benc 1001f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local) 1002f0706e82SJiri Benc { 1003d0709a65SJohannes Berg spin_lock_init(&local->sta_lock); 100434e89507SJohannes Berg mutex_init(&local->sta_mtx); 1005f0706e82SJiri Benc INIT_LIST_HEAD(&local->sta_list); 100634e89507SJohannes Berg INIT_LIST_HEAD(&local->sta_pending_list); 100734e89507SJohannes Berg INIT_WORK(&local->sta_finish_work, sta_info_finish_work); 1008f0706e82SJiri Benc 1009b24b8a24SPavel Emelyanov setup_timer(&local->sta_cleanup, sta_info_cleanup, 1010b24b8a24SPavel Emelyanov (unsigned long)local); 1011f0706e82SJiri Benc } 1012f0706e82SJiri Benc 1013f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local) 1014f0706e82SJiri Benc { 1015f0706e82SJiri Benc del_timer(&local->sta_cleanup); 1016be8755e1SMichael Wu sta_info_flush(local, NULL); 1017f0706e82SJiri Benc } 1018f0706e82SJiri Benc 1019f0706e82SJiri Benc /** 1020f0706e82SJiri Benc * sta_info_flush - flush matching STA entries from the STA table 102144213b5eSJohannes Berg * 102244213b5eSJohannes Berg * Returns the number of removed STA entries. 102344213b5eSJohannes Berg * 1024f0706e82SJiri Benc * @local: local interface data 1025d0709a65SJohannes Berg * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs 1026f0706e82SJiri Benc */ 102744213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local, 1028d0709a65SJohannes Berg struct ieee80211_sub_if_data *sdata) 1029f0706e82SJiri Benc { 1030f0706e82SJiri Benc struct sta_info *sta, *tmp; 103144213b5eSJohannes Berg int ret = 0; 1032f0706e82SJiri Benc 1033d0709a65SJohannes Berg might_sleep(); 1034d0709a65SJohannes Berg 103534e89507SJohannes Berg mutex_lock(&local->sta_mtx); 1036d0709a65SJohannes Berg 103734e89507SJohannes Berg sta_info_finish_pending(local); 103834e89507SJohannes Berg 103934e89507SJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 104034e89507SJohannes Berg if (!sdata || sdata == sta->sdata) 104134e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 104234e89507SJohannes Berg } 104334e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 104444213b5eSJohannes Berg 104544213b5eSJohannes Berg return ret; 1046f0706e82SJiri Benc } 1047dc6676b7SJohannes Berg 104824723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 104924723d1bSJohannes Berg unsigned long exp_time) 105024723d1bSJohannes Berg { 105124723d1bSJohannes Berg struct ieee80211_local *local = sdata->local; 105224723d1bSJohannes Berg struct sta_info *sta, *tmp; 105324723d1bSJohannes Berg 105434e89507SJohannes Berg mutex_lock(&local->sta_mtx); 105524723d1bSJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) 105624723d1bSJohannes Berg if (time_after(jiffies, sta->last_rx + exp_time)) { 105724723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG 10580c68ae26SJohannes Berg printk(KERN_DEBUG "%s: expiring inactive STA %pM\n", 105947846c9bSJohannes Berg sdata->name, sta->sta.addr); 106024723d1bSJohannes Berg #endif 106134e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 106224723d1bSJohannes Berg } 106334e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 106424723d1bSJohannes Berg } 106517741cdcSJohannes Berg 1066686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 1067686b9cb9SBen Greear const u8 *addr, 1068686b9cb9SBen Greear const u8 *localaddr) 106917741cdcSJohannes Berg { 1070abe60632SJohannes Berg struct sta_info *sta, *nxt; 107117741cdcSJohannes Berg 1072686b9cb9SBen Greear /* 1073686b9cb9SBen Greear * Just return a random station if localaddr is NULL 1074686b9cb9SBen Greear * ... first in list. 1075686b9cb9SBen Greear */ 1076f7c65594SJohannes Berg for_each_sta_info(hw_to_local(hw), addr, sta, nxt) { 1077686b9cb9SBen Greear if (localaddr && 1078686b9cb9SBen Greear compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0) 1079686b9cb9SBen Greear continue; 1080f7c65594SJohannes Berg if (!sta->uploaded) 1081f7c65594SJohannes Berg return NULL; 108217741cdcSJohannes Berg return &sta->sta; 1083f7c65594SJohannes Berg } 1084f7c65594SJohannes Berg 1085abe60632SJohannes Berg return NULL; 108617741cdcSJohannes Berg } 1087686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); 10885ed176e1SJohannes Berg 10895ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, 10905ed176e1SJohannes Berg const u8 *addr) 10915ed176e1SJohannes Berg { 1092f7c65594SJohannes Berg struct sta_info *sta; 10935ed176e1SJohannes Berg 10945ed176e1SJohannes Berg if (!vif) 10955ed176e1SJohannes Berg return NULL; 10965ed176e1SJohannes Berg 1097f7c65594SJohannes Berg sta = sta_info_get_bss(vif_to_sdata(vif), addr); 1098f7c65594SJohannes Berg if (!sta) 1099f7c65594SJohannes Berg return NULL; 11005ed176e1SJohannes Berg 1101f7c65594SJohannes Berg if (!sta->uploaded) 1102f7c65594SJohannes Berg return NULL; 1103f7c65594SJohannes Berg 1104f7c65594SJohannes Berg return &sta->sta; 11055ed176e1SJohannes Berg } 110617741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta); 1107af818581SJohannes Berg 110850a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta) 110950a9432dSJohannes Berg { 111050a9432dSJohannes Berg struct sta_info *sta = _sta; 111150a9432dSJohannes Berg 111250a9432dSJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER | WLAN_STA_PS_STA); 111350a9432dSJohannes Berg } 111450a9432dSJohannes Berg 1115af818581SJohannes Berg /* powersave support code */ 1116af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) 1117af818581SJohannes Berg { 1118af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1119af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 1120948d887dSJohannes Berg struct sk_buff_head pending; 1121948d887dSJohannes Berg int filtered = 0, buffered = 0, ac; 1122af818581SJohannes Berg 1123*47086fc5SJohannes Berg clear_sta_flags(sta, WLAN_STA_SP); 1124*47086fc5SJohannes Berg 1125948d887dSJohannes Berg BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1); 1126948d887dSJohannes Berg sta->driver_buffered_tids = 0; 1127948d887dSJohannes Berg 1128d057e5a3SArik Nemtsov if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS)) 112912375ef9SJohannes Berg drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 1130af818581SJohannes Berg 1131948d887dSJohannes Berg skb_queue_head_init(&pending); 1132948d887dSJohannes Berg 1133af818581SJohannes Berg /* Send all buffered frames to the station */ 1134948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1135948d887dSJohannes Berg int count = skb_queue_len(&pending), tmp; 1136948d887dSJohannes Berg 1137948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending); 1138948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1139948d887dSJohannes Berg filtered += tmp - count; 1140948d887dSJohannes Berg count = tmp; 1141948d887dSJohannes Berg 1142948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending); 1143948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1144948d887dSJohannes Berg buffered += tmp - count; 1145948d887dSJohannes Berg } 1146948d887dSJohannes Berg 1147948d887dSJohannes Berg ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta); 1148948d887dSJohannes Berg 1149af818581SJohannes Berg local->total_ps_buffered -= buffered; 1150af818581SJohannes Berg 1151c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1152c868cb35SJohannes Berg 1153af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 1154af818581SJohannes Berg printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames " 115547846c9bSJohannes Berg "since STA not sleeping anymore\n", sdata->name, 1156948d887dSJohannes Berg sta->sta.addr, sta->sta.aid, filtered, buffered); 1157af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 1158af818581SJohannes Berg } 1159af818581SJohannes Berg 1160*47086fc5SJohannes Berg static void 1161*47086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta, 1162*47086fc5SJohannes Berg int n_frames, u8 ignored_acs, 1163*47086fc5SJohannes Berg enum ieee80211_frame_release_type reason) 1164af818581SJohannes Berg { 1165af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1166af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 11674049e09aSJohannes Berg bool found = false; 1168948d887dSJohannes Berg bool more_data = false; 1169948d887dSJohannes Berg int ac; 11704049e09aSJohannes Berg unsigned long driver_release_tids = 0; 1171*47086fc5SJohannes Berg struct sk_buff_head frames; 1172*47086fc5SJohannes Berg 1173*47086fc5SJohannes Berg __skb_queue_head_init(&frames); 1174af818581SJohannes Berg 1175948d887dSJohannes Berg /* 1176*47086fc5SJohannes Berg * Get response frame(s) and more data bit for it. 1177948d887dSJohannes Berg */ 1178948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 11794049e09aSJohannes Berg unsigned long tids; 11804049e09aSJohannes Berg 1181*47086fc5SJohannes Berg if (ignored_acs & BIT(ac)) 1182948d887dSJohannes Berg continue; 1183948d887dSJohannes Berg 11844049e09aSJohannes Berg tids = ieee80211_tids_for_ac(ac); 11854049e09aSJohannes Berg 11864049e09aSJohannes Berg if (!found) { 11874049e09aSJohannes Berg driver_release_tids = sta->driver_buffered_tids & tids; 11884049e09aSJohannes Berg if (driver_release_tids) { 11894049e09aSJohannes Berg found = true; 11904049e09aSJohannes Berg } else { 1191*47086fc5SJohannes Berg struct sk_buff *skb; 1192*47086fc5SJohannes Berg 1193*47086fc5SJohannes Berg while (n_frames > 0) { 1194948d887dSJohannes Berg skb = skb_dequeue(&sta->tx_filtered[ac]); 1195948d887dSJohannes Berg if (!skb) { 1196*47086fc5SJohannes Berg skb = skb_dequeue( 1197*47086fc5SJohannes Berg &sta->ps_tx_buf[ac]); 1198af818581SJohannes Berg if (skb) 1199af818581SJohannes Berg local->total_ps_buffered--; 1200af818581SJohannes Berg } 1201*47086fc5SJohannes Berg if (!skb) 1202*47086fc5SJohannes Berg break; 1203*47086fc5SJohannes Berg n_frames--; 12044049e09aSJohannes Berg found = true; 1205*47086fc5SJohannes Berg __skb_queue_tail(&frames, skb); 1206*47086fc5SJohannes Berg } 1207948d887dSJohannes Berg } 1208948d887dSJohannes Berg 12094049e09aSJohannes Berg /* 12104049e09aSJohannes Berg * If the driver has data on more than one TID then 12114049e09aSJohannes Berg * certainly there's more data if we release just a 12124049e09aSJohannes Berg * single frame now (from a single TID). 12134049e09aSJohannes Berg */ 1214*47086fc5SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_PSPOLL && 1215*47086fc5SJohannes Berg hweight16(driver_release_tids) > 1) { 12164049e09aSJohannes Berg more_data = true; 12174049e09aSJohannes Berg driver_release_tids = 12184049e09aSJohannes Berg BIT(ffs(driver_release_tids) - 1); 12194049e09aSJohannes Berg break; 12204049e09aSJohannes Berg } 12214049e09aSJohannes Berg } 1222948d887dSJohannes Berg 1223948d887dSJohannes Berg if (!skb_queue_empty(&sta->tx_filtered[ac]) || 1224948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac])) { 1225948d887dSJohannes Berg more_data = true; 1226948d887dSJohannes Berg break; 1227948d887dSJohannes Berg } 1228948d887dSJohannes Berg } 1229af818581SJohannes Berg 12304049e09aSJohannes Berg if (!found) { 12314049e09aSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 12324049e09aSJohannes Berg /* 12334049e09aSJohannes Berg * FIXME: This can be the result of a race condition between 12344049e09aSJohannes Berg * us expiring a frame and the station polling for it. 12354049e09aSJohannes Berg * Should we send it a null-func frame indicating we 12364049e09aSJohannes Berg * have nothing buffered for it? 12374049e09aSJohannes Berg */ 1238*47086fc5SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) 12394049e09aSJohannes Berg printk(KERN_DEBUG "%s: STA %pM sent PS Poll even " 12404049e09aSJohannes Berg "though there are no buffered frames for it\n", 12414049e09aSJohannes Berg sdata->name, sta->sta.addr); 12424049e09aSJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 12434049e09aSJohannes Berg 12444049e09aSJohannes Berg return; 12454049e09aSJohannes Berg } 12464049e09aSJohannes Berg 1247*47086fc5SJohannes Berg if (!driver_release_tids) { 1248*47086fc5SJohannes Berg struct sk_buff_head pending; 1249*47086fc5SJohannes Berg struct sk_buff *skb; 1250*47086fc5SJohannes Berg 1251*47086fc5SJohannes Berg skb_queue_head_init(&pending); 1252*47086fc5SJohannes Berg 1253*47086fc5SJohannes Berg while ((skb = __skb_dequeue(&frames))) { 1254af818581SJohannes Berg struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 1255*47086fc5SJohannes Berg struct ieee80211_hdr *hdr = (void *) skb->data; 1256af818581SJohannes Berg 1257af818581SJohannes Berg /* 1258*47086fc5SJohannes Berg * Tell TX path to send this frame even though the 1259*47086fc5SJohannes Berg * STA may still remain is PS mode after this frame 1260*47086fc5SJohannes Berg * exchange. 1261af818581SJohannes Berg */ 1262*47086fc5SJohannes Berg info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE; 1263af818581SJohannes Berg 1264*47086fc5SJohannes Berg /* 1265*47086fc5SJohannes Berg * Use MoreData flag to indicate whether there are 1266*47086fc5SJohannes Berg * more buffered frames for this STA 1267*47086fc5SJohannes Berg */ 1268948d887dSJohannes Berg if (!more_data) 1269*47086fc5SJohannes Berg hdr->frame_control &= 1270*47086fc5SJohannes Berg cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 1271af818581SJohannes Berg else 1272*47086fc5SJohannes Berg hdr->frame_control |= 1273*47086fc5SJohannes Berg cpu_to_le16(IEEE80211_FCTL_MOREDATA); 1274af818581SJohannes Berg 1275*47086fc5SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_UAPSD && 1276*47086fc5SJohannes Berg skb_queue_empty(&frames)) { 1277*47086fc5SJohannes Berg /* set EOSP for the frame */ 1278*47086fc5SJohannes Berg u8 *p = ieee80211_get_qos_ctl(hdr); 1279*47086fc5SJohannes Berg *p |= IEEE80211_QOS_CTL_EOSP; 1280*47086fc5SJohannes Berg info->flags |= IEEE80211_TX_STATUS_EOSP | 1281*47086fc5SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 1282*47086fc5SJohannes Berg } 1283*47086fc5SJohannes Berg 1284*47086fc5SJohannes Berg __skb_queue_tail(&pending, skb); 1285*47086fc5SJohannes Berg } 1286*47086fc5SJohannes Berg 1287*47086fc5SJohannes Berg ieee80211_add_pending_skbs(local, &pending); 1288af818581SJohannes Berg 1289c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1290af818581SJohannes Berg } else { 1291af818581SJohannes Berg /* 12924049e09aSJohannes Berg * We need to release a frame that is buffered somewhere in the 12934049e09aSJohannes Berg * driver ... it'll have to handle that. 12944049e09aSJohannes Berg * Note that, as per the comment above, it'll also have to see 12954049e09aSJohannes Berg * if there is more than just one frame on the specific TID that 12964049e09aSJohannes Berg * we're releasing from, and it needs to set the more-data bit 12974049e09aSJohannes Berg * accordingly if we tell it that there's no more data. If we do 12984049e09aSJohannes Berg * tell it there's more data, then of course the more-data bit 12994049e09aSJohannes Berg * needs to be set anyway. 1300af818581SJohannes Berg */ 13014049e09aSJohannes Berg drv_release_buffered_frames(local, sta, driver_release_tids, 1302*47086fc5SJohannes Berg n_frames, reason, more_data); 13034049e09aSJohannes Berg 13044049e09aSJohannes Berg /* 13054049e09aSJohannes Berg * Note that we don't recalculate the TIM bit here as it would 13064049e09aSJohannes Berg * most likely have no effect at all unless the driver told us 13074049e09aSJohannes Berg * that the TID became empty before returning here from the 13084049e09aSJohannes Berg * release function. 13094049e09aSJohannes Berg * Either way, however, when the driver tells us that the TID 13104049e09aSJohannes Berg * became empty we'll do the TIM recalculation. 13114049e09aSJohannes Berg */ 1312af818581SJohannes Berg } 1313af818581SJohannes Berg } 1314af818581SJohannes Berg 1315*47086fc5SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 1316*47086fc5SJohannes Berg { 1317*47086fc5SJohannes Berg u8 ignore_for_response = sta->sta.uapsd_queues; 1318*47086fc5SJohannes Berg 1319*47086fc5SJohannes Berg /* 1320*47086fc5SJohannes Berg * If all ACs are delivery-enabled then we should reply 1321*47086fc5SJohannes Berg * from any of them, if only some are enabled we reply 1322*47086fc5SJohannes Berg * only from the non-enabled ones. 1323*47086fc5SJohannes Berg */ 1324*47086fc5SJohannes Berg if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1) 1325*47086fc5SJohannes Berg ignore_for_response = 0; 1326*47086fc5SJohannes Berg 1327*47086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response, 1328*47086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_PSPOLL); 1329*47086fc5SJohannes Berg } 1330*47086fc5SJohannes Berg 1331*47086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta) 1332*47086fc5SJohannes Berg { 1333*47086fc5SJohannes Berg int n_frames = sta->sta.max_sp; 1334*47086fc5SJohannes Berg u8 delivery_enabled = sta->sta.uapsd_queues; 1335*47086fc5SJohannes Berg 1336*47086fc5SJohannes Berg /* 1337*47086fc5SJohannes Berg * If we ever grow support for TSPEC this might happen if 1338*47086fc5SJohannes Berg * the TSPEC update from hostapd comes in between a trigger 1339*47086fc5SJohannes Berg * frame setting WLAN_STA_UAPSD in the RX path and this 1340*47086fc5SJohannes Berg * actually getting called. 1341*47086fc5SJohannes Berg */ 1342*47086fc5SJohannes Berg if (!delivery_enabled) 1343*47086fc5SJohannes Berg return; 1344*47086fc5SJohannes Berg 1345*47086fc5SJohannes Berg /* Ohh, finally, the service period starts :-) */ 1346*47086fc5SJohannes Berg set_sta_flags(sta, WLAN_STA_SP); 1347*47086fc5SJohannes Berg 1348*47086fc5SJohannes Berg switch (sta->sta.max_sp) { 1349*47086fc5SJohannes Berg case 1: 1350*47086fc5SJohannes Berg n_frames = 2; 1351*47086fc5SJohannes Berg break; 1352*47086fc5SJohannes Berg case 2: 1353*47086fc5SJohannes Berg n_frames = 4; 1354*47086fc5SJohannes Berg break; 1355*47086fc5SJohannes Berg case 3: 1356*47086fc5SJohannes Berg n_frames = 6; 1357*47086fc5SJohannes Berg break; 1358*47086fc5SJohannes Berg case 0: 1359*47086fc5SJohannes Berg /* XXX: what is a good value? */ 1360*47086fc5SJohannes Berg n_frames = 8; 1361*47086fc5SJohannes Berg break; 1362*47086fc5SJohannes Berg } 1363*47086fc5SJohannes Berg 1364*47086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled, 1365*47086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_UAPSD); 1366*47086fc5SJohannes Berg } 1367*47086fc5SJohannes Berg 1368af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 1369af818581SJohannes Berg struct ieee80211_sta *pubsta, bool block) 1370af818581SJohannes Berg { 1371af818581SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1372af818581SJohannes Berg 1373b5878a2dSJohannes Berg trace_api_sta_block_awake(sta->local, pubsta, block); 1374b5878a2dSJohannes Berg 1375af818581SJohannes Berg if (block) 1376af818581SJohannes Berg set_sta_flags(sta, WLAN_STA_PS_DRIVER); 137750a9432dSJohannes Berg else if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) 1378af818581SJohannes Berg ieee80211_queue_work(hw, &sta->drv_unblock_wk); 1379af818581SJohannes Berg } 1380af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake); 1381dcf55fb5SFelix Fietkau 1382042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, 1383042ec453SJohannes Berg u8 tid, bool buffered) 1384dcf55fb5SFelix Fietkau { 1385dcf55fb5SFelix Fietkau struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1386dcf55fb5SFelix Fietkau 1387948d887dSJohannes Berg if (WARN_ON(tid >= STA_TID_NUM)) 1388042ec453SJohannes Berg return; 1389042ec453SJohannes Berg 1390948d887dSJohannes Berg if (buffered) 1391948d887dSJohannes Berg set_bit(tid, &sta->driver_buffered_tids); 1392948d887dSJohannes Berg else 1393948d887dSJohannes Berg clear_bit(tid, &sta->driver_buffered_tids); 1394948d887dSJohannes Berg 1395c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1396dcf55fb5SFelix Fietkau } 1397042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered); 1398