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); 25150a9432dSJohannes Berg } else 25250a9432dSJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER); 253af818581SJohannes Berg } 254af818581SJohannes Berg 255af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local, 256af65cd96SJohannes Berg struct sta_info *sta, gfp_t gfp) 257af65cd96SJohannes Berg { 258af65cd96SJohannes Berg if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) 259af65cd96SJohannes Berg return 0; 260af65cd96SJohannes Berg 261af65cd96SJohannes Berg sta->rate_ctrl = rate_control_get(local->rate_ctrl); 262af65cd96SJohannes Berg sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, 263af65cd96SJohannes Berg &sta->sta, gfp); 264af65cd96SJohannes Berg if (!sta->rate_ctrl_priv) { 265af65cd96SJohannes Berg rate_control_put(sta->rate_ctrl); 266af65cd96SJohannes Berg return -ENOMEM; 267af65cd96SJohannes Berg } 268af65cd96SJohannes Berg 269af65cd96SJohannes Berg return 0; 270af65cd96SJohannes Berg } 271af65cd96SJohannes Berg 27273651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, 27373651ee6SJohannes Berg u8 *addr, gfp_t gfp) 274f0706e82SJiri Benc { 275d0709a65SJohannes Berg struct ieee80211_local *local = sdata->local; 276f0706e82SJiri Benc struct sta_info *sta; 277ebe27c91SMohammed Shafi Shajakhan struct timespec uptime; 27816c5f15cSRon Rindjunsky int i; 279f0706e82SJiri Benc 28017741cdcSJohannes Berg sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp); 281f0706e82SJiri Benc if (!sta) 28273651ee6SJohannes Berg return NULL; 283f0706e82SJiri Benc 28407346f81SJohannes Berg spin_lock_init(&sta->lock); 2855a9f7b04SJohannes Berg spin_lock_init(&sta->flaglock); 286af818581SJohannes Berg INIT_WORK(&sta->drv_unblock_wk, sta_unblock); 28767c282c0SJohannes Berg INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work); 288a93e3644SJohannes Berg mutex_init(&sta->ampdu_mlme.mtx); 28907346f81SJohannes Berg 29017741cdcSJohannes Berg memcpy(sta->sta.addr, addr, ETH_ALEN); 291d0709a65SJohannes Berg sta->local = local; 292d0709a65SJohannes Berg sta->sdata = sdata; 2938bc8aecdSFelix Fietkau sta->last_rx = jiffies; 294f0706e82SJiri Benc 295ebe27c91SMohammed Shafi Shajakhan do_posix_clock_monotonic_gettime(&uptime); 296ebe27c91SMohammed Shafi Shajakhan sta->last_connected = uptime.tv_sec; 297541a45a1SBruno Randolf ewma_init(&sta->avg_signal, 1024, 8); 298541a45a1SBruno Randolf 299af65cd96SJohannes Berg if (sta_prepare_rate_control(local, sta, gfp)) { 300f0706e82SJiri Benc kfree(sta); 30173651ee6SJohannes Berg return NULL; 302f0706e82SJiri Benc } 303f0706e82SJiri Benc 30416c5f15cSRon Rindjunsky for (i = 0; i < STA_TID_NUM; i++) { 305a622ab72SJohannes Berg /* 306a622ab72SJohannes Berg * timer_to_tid must be initialized with identity mapping 307a622ab72SJohannes Berg * to enable session_timer's data differentiation. See 308a622ab72SJohannes Berg * sta_rx_agg_session_timer_expired for usage. 309a622ab72SJohannes Berg */ 31016c5f15cSRon Rindjunsky sta->timer_to_tid[i] = i; 31116c5f15cSRon Rindjunsky } 312948d887dSJohannes Berg for (i = 0; i < IEEE80211_NUM_ACS; i++) { 313948d887dSJohannes Berg skb_queue_head_init(&sta->ps_tx_buf[i]); 314948d887dSJohannes Berg skb_queue_head_init(&sta->tx_filtered[i]); 315948d887dSJohannes Berg } 31673651ee6SJohannes Berg 317cccaec98SSenthil Balasubramanian for (i = 0; i < NUM_RX_DATA_QUEUES; i++) 3184be929beSAlexey Dobriyan sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); 319cccaec98SSenthil Balasubramanian 32073651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 3210fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr); 32273651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 32373651ee6SJohannes Berg 32403e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH 32557cf8043SJavier Cardona sta->plink_state = NL80211_PLINK_LISTEN; 32603e4497eSJohannes Berg init_timer(&sta->plink_timer); 32703e4497eSJohannes Berg #endif 32803e4497eSJohannes Berg 32973651ee6SJohannes Berg return sta; 33073651ee6SJohannes Berg } 33173651ee6SJohannes Berg 3322a33bee2SGuy Eilam static int sta_info_finish_insert(struct sta_info *sta, 3332a33bee2SGuy Eilam bool async, bool dummy_reinsert) 33473651ee6SJohannes Berg { 33573651ee6SJohannes Berg struct ieee80211_local *local = sta->local; 33673651ee6SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 33798b62183SJohannes Berg struct station_info sinfo; 33873651ee6SJohannes Berg unsigned long flags; 33993e5deb1SJohannes Berg int err = 0; 34073651ee6SJohannes Berg 34146a5ebafSJohannes Berg lockdep_assert_held(&local->sta_mtx); 34234e89507SJohannes Berg 3432a33bee2SGuy Eilam if (!sta->dummy || dummy_reinsert) { 34434e89507SJohannes Berg /* notify driver */ 34534e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 34634e89507SJohannes Berg sdata = container_of(sdata->bss, 34734e89507SJohannes Berg struct ieee80211_sub_if_data, 34834e89507SJohannes Berg u.ap); 34934e89507SJohannes Berg err = drv_sta_add(local, sdata, &sta->sta); 35034e89507SJohannes Berg if (err) { 35134e89507SJohannes Berg if (!async) 35234e89507SJohannes Berg return err; 3532a33bee2SGuy Eilam printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to " 3542a33bee2SGuy Eilam "driver (%d) - keeping it anyway.\n", 35534e89507SJohannes Berg sdata->name, sta->sta.addr, err); 35634e89507SJohannes Berg } else { 35734e89507SJohannes Berg sta->uploaded = true; 35834e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 35934e89507SJohannes Berg if (async) 3600fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, 3610fb9a9ecSJoe Perches "Finished adding IBSS STA %pM\n", 3620fb9a9ecSJoe Perches sta->sta.addr); 36334e89507SJohannes Berg #endif 36434e89507SJohannes Berg } 36534e89507SJohannes Berg 36634e89507SJohannes Berg sdata = sta->sdata; 3672a33bee2SGuy Eilam } 36834e89507SJohannes Berg 3692a33bee2SGuy Eilam if (!dummy_reinsert) { 37034e89507SJohannes Berg if (!async) { 37134e89507SJohannes Berg local->num_sta++; 37234e89507SJohannes Berg local->sta_generation++; 37334e89507SJohannes Berg smp_mb(); 37434e89507SJohannes Berg 37534e89507SJohannes Berg /* make the station visible */ 37634e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 37734e89507SJohannes Berg sta_info_hash_add(local, sta); 37834e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 37934e89507SJohannes Berg } 38034e89507SJohannes Berg 38134e89507SJohannes Berg list_add(&sta->list, &local->sta_list); 3822a33bee2SGuy Eilam } else { 3832a33bee2SGuy Eilam sta->dummy = false; 3842a33bee2SGuy Eilam } 38534e89507SJohannes Berg 3862a33bee2SGuy Eilam if (!sta->dummy) { 38734e89507SJohannes Berg ieee80211_sta_debugfs_add(sta); 38834e89507SJohannes Berg rate_control_add_sta_debugfs(sta); 38934e89507SJohannes Berg 390f612cedfSJouni Malinen memset(&sinfo, 0, sizeof(sinfo)); 39134e89507SJohannes Berg sinfo.filled = 0; 39234e89507SJohannes Berg sinfo.generation = local->sta_generation; 39334e89507SJohannes Berg cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL); 3942a33bee2SGuy Eilam } 39534e89507SJohannes Berg 39634e89507SJohannes Berg return 0; 39734e89507SJohannes Berg } 39834e89507SJohannes Berg 39934e89507SJohannes Berg static void sta_info_finish_pending(struct ieee80211_local *local) 40034e89507SJohannes Berg { 40134e89507SJohannes Berg struct sta_info *sta; 40234e89507SJohannes Berg unsigned long flags; 40334e89507SJohannes Berg 40434e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 40534e89507SJohannes Berg while (!list_empty(&local->sta_pending_list)) { 40634e89507SJohannes Berg sta = list_first_entry(&local->sta_pending_list, 40734e89507SJohannes Berg struct sta_info, list); 40834e89507SJohannes Berg list_del(&sta->list); 40934e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 41034e89507SJohannes Berg 4112a33bee2SGuy Eilam sta_info_finish_insert(sta, true, false); 41234e89507SJohannes Berg 41334e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 41434e89507SJohannes Berg } 41534e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 41634e89507SJohannes Berg } 41734e89507SJohannes Berg 41834e89507SJohannes Berg static void sta_info_finish_work(struct work_struct *work) 41934e89507SJohannes Berg { 42034e89507SJohannes Berg struct ieee80211_local *local = 42134e89507SJohannes Berg container_of(work, struct ieee80211_local, sta_finish_work); 42234e89507SJohannes Berg 42334e89507SJohannes Berg mutex_lock(&local->sta_mtx); 42434e89507SJohannes Berg sta_info_finish_pending(local); 42534e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 42634e89507SJohannes Berg } 42734e89507SJohannes Berg 4288c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta) 42934e89507SJohannes Berg { 43034e89507SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 43134e89507SJohannes Berg 43203e4497eSJohannes Berg /* 43303e4497eSJohannes Berg * Can't be a WARN_ON because it can be triggered through a race: 43403e4497eSJohannes Berg * something inserts a STA (on one CPU) without holding the RTNL 43503e4497eSJohannes Berg * and another CPU turns off the net device. 43603e4497eSJohannes Berg */ 4378c71df7aSGuy Eilam if (unlikely(!ieee80211_sdata_running(sdata))) 4388c71df7aSGuy Eilam return -ENETDOWN; 43903e4497eSJohannes Berg 44047846c9bSJohannes Berg if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 || 4418c71df7aSGuy Eilam is_multicast_ether_addr(sta->sta.addr))) 4428c71df7aSGuy Eilam return -EINVAL; 4438c71df7aSGuy Eilam 4448c71df7aSGuy Eilam return 0; 44593e5deb1SJohannes Berg } 44644213b5eSJohannes Berg 4478c71df7aSGuy Eilam static int sta_info_insert_ibss(struct sta_info *sta) __acquires(RCU) 4488c71df7aSGuy Eilam { 4498c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 4508c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 4518c71df7aSGuy Eilam unsigned long flags; 4528c71df7aSGuy Eilam 453d0709a65SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 45443ba7e95SJohannes Berg /* check if STA exists already */ 4552a33bee2SGuy Eilam if (sta_info_get_bss_rx(sdata, sta->sta.addr)) { 456d0709a65SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 45734e89507SJohannes Berg rcu_read_lock(); 4588c71df7aSGuy Eilam return -EEXIST; 45943ba7e95SJohannes Berg } 46034e89507SJohannes Berg 461f0706e82SJiri Benc local->num_sta++; 46234e89507SJohannes Berg local->sta_generation++; 46334e89507SJohannes Berg smp_mb(); 464f0706e82SJiri Benc sta_info_hash_add(local, sta); 46532bfd35dSJohannes Berg 46634e89507SJohannes Berg list_add_tail(&sta->list, &local->sta_pending_list); 46732bfd35dSJohannes Berg 46834e89507SJohannes Berg rcu_read_lock(); 46934e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 47034e89507SJohannes Berg 47134e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 4720fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n", 4730fb9a9ecSJoe Perches sta->sta.addr); 47434e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 47534e89507SJohannes Berg 47634e89507SJohannes Berg ieee80211_queue_work(&local->hw, &local->sta_finish_work); 47734e89507SJohannes Berg 47834e89507SJohannes Berg return 0; 47934e89507SJohannes Berg } 48034e89507SJohannes Berg 48134e89507SJohannes Berg /* 4828c71df7aSGuy Eilam * should be called with sta_mtx locked 4838c71df7aSGuy Eilam * this function replaces the mutex lock 4848c71df7aSGuy Eilam * with a RCU lock 4858c71df7aSGuy Eilam */ 4868c71df7aSGuy Eilam static int sta_info_insert_non_ibss(struct sta_info *sta) __acquires(RCU) 4878c71df7aSGuy Eilam { 4888c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 4898c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 4908c71df7aSGuy Eilam unsigned long flags; 4912a33bee2SGuy Eilam struct sta_info *exist_sta; 4922a33bee2SGuy Eilam bool dummy_reinsert = false; 4938c71df7aSGuy Eilam int err = 0; 4948c71df7aSGuy Eilam 4958c71df7aSGuy Eilam lockdep_assert_held(&local->sta_mtx); 4968c71df7aSGuy Eilam 4978c71df7aSGuy Eilam /* 49834e89507SJohannes Berg * On first glance, this will look racy, because the code 4998c71df7aSGuy Eilam * in this function, which inserts a station with sleeping, 50034e89507SJohannes Berg * unlocks the sta_lock between checking existence in the 50134e89507SJohannes Berg * hash table and inserting into it. 50234e89507SJohannes Berg * 50334e89507SJohannes Berg * However, it is not racy against itself because it keeps 5048c71df7aSGuy Eilam * the mutex locked. 50534e89507SJohannes Berg */ 50634e89507SJohannes Berg 50734e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 5082a33bee2SGuy Eilam /* 5092a33bee2SGuy Eilam * check if STA exists already. 5102a33bee2SGuy Eilam * only accept a scenario of a second call to sta_info_insert_non_ibss 5112a33bee2SGuy Eilam * with a dummy station entry that was inserted earlier 5122a33bee2SGuy Eilam * in that case - assume that the dummy station flag should 5132a33bee2SGuy Eilam * be removed. 5142a33bee2SGuy Eilam */ 5152a33bee2SGuy Eilam exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr); 5162a33bee2SGuy Eilam if (exist_sta) { 5172a33bee2SGuy Eilam if (exist_sta == sta && sta->dummy) { 5182a33bee2SGuy Eilam dummy_reinsert = true; 5192a33bee2SGuy Eilam } else { 52034e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 52138a679a5SJouni Malinen mutex_unlock(&local->sta_mtx); 52234e89507SJohannes Berg rcu_read_lock(); 5238c71df7aSGuy Eilam return -EEXIST; 52434e89507SJohannes Berg } 5252a33bee2SGuy Eilam } 52634e89507SJohannes Berg 52734e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 52834e89507SJohannes Berg 5292a33bee2SGuy Eilam err = sta_info_finish_insert(sta, false, dummy_reinsert); 53034e89507SJohannes Berg if (err) { 53134e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 53234e89507SJohannes Berg rcu_read_lock(); 5338c71df7aSGuy Eilam return err; 53432bfd35dSJohannes Berg } 535d0709a65SJohannes Berg 536f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 5372a33bee2SGuy Eilam wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n", 5382a33bee2SGuy Eilam sta->dummy ? "dummy " : "", sta->sta.addr); 539f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 540f0706e82SJiri Benc 54134e89507SJohannes Berg /* move reference to rcu-protected */ 54234e89507SJohannes Berg rcu_read_lock(); 54334e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 544e9f207f0SJiri Benc 54573651ee6SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 54673651ee6SJohannes Berg mesh_accept_plinks_update(sdata); 54773651ee6SJohannes Berg 54873651ee6SJohannes Berg return 0; 5498c71df7aSGuy Eilam } 5508c71df7aSGuy Eilam 5518c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) 5528c71df7aSGuy Eilam { 5538c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 5548c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 5558c71df7aSGuy Eilam int err = 0; 5568c71df7aSGuy Eilam 5578c71df7aSGuy Eilam err = sta_info_insert_check(sta); 5588c71df7aSGuy Eilam if (err) { 5598c71df7aSGuy Eilam rcu_read_lock(); 5608c71df7aSGuy Eilam goto out_free; 5618c71df7aSGuy Eilam } 5628c71df7aSGuy Eilam 5638c71df7aSGuy Eilam /* 5648c71df7aSGuy Eilam * In ad-hoc mode, we sometimes need to insert stations 5658c71df7aSGuy Eilam * from tasklet context from the RX path. To avoid races, 5668c71df7aSGuy Eilam * always do so in that case -- see the comment below. 5678c71df7aSGuy Eilam */ 5688c71df7aSGuy Eilam if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { 5698c71df7aSGuy Eilam err = sta_info_insert_ibss(sta); 5708c71df7aSGuy Eilam if (err) 5718c71df7aSGuy Eilam goto out_free; 5728c71df7aSGuy Eilam 5738c71df7aSGuy Eilam return 0; 5748c71df7aSGuy Eilam } 5758c71df7aSGuy Eilam 5768c71df7aSGuy Eilam /* 5778c71df7aSGuy Eilam * It might seem that the function called below is in race against 5788c71df7aSGuy Eilam * the function call above that atomically inserts the station... That, 5798c71df7aSGuy Eilam * however, is not true because the above code can only 5808c71df7aSGuy Eilam * be invoked for IBSS interfaces, and the below code will 5818c71df7aSGuy Eilam * not be -- and the two do not race against each other as 5828c71df7aSGuy Eilam * the hash table also keys off the interface. 5838c71df7aSGuy Eilam */ 5848c71df7aSGuy Eilam 5858c71df7aSGuy Eilam might_sleep(); 5868c71df7aSGuy Eilam 5878c71df7aSGuy Eilam mutex_lock(&local->sta_mtx); 5888c71df7aSGuy Eilam 5898c71df7aSGuy Eilam err = sta_info_insert_non_ibss(sta); 5908c71df7aSGuy Eilam if (err) 5918c71df7aSGuy Eilam goto out_free; 5928c71df7aSGuy Eilam 5938c71df7aSGuy Eilam return 0; 59493e5deb1SJohannes Berg out_free: 59593e5deb1SJohannes Berg BUG_ON(!err); 59693e5deb1SJohannes Berg __sta_info_free(local, sta); 59793e5deb1SJohannes Berg return err; 598f0706e82SJiri Benc } 599f0706e82SJiri Benc 60034e89507SJohannes Berg int sta_info_insert(struct sta_info *sta) 60134e89507SJohannes Berg { 60234e89507SJohannes Berg int err = sta_info_insert_rcu(sta); 60334e89507SJohannes Berg 60434e89507SJohannes Berg rcu_read_unlock(); 60534e89507SJohannes Berg 60634e89507SJohannes Berg return err; 60734e89507SJohannes Berg } 60834e89507SJohannes Berg 6092a33bee2SGuy Eilam /* Caller must hold sta->local->sta_mtx */ 6102a33bee2SGuy Eilam int sta_info_reinsert(struct sta_info *sta) 6112a33bee2SGuy Eilam { 6122a33bee2SGuy Eilam struct ieee80211_local *local = sta->local; 6132a33bee2SGuy Eilam int err = 0; 6142a33bee2SGuy Eilam 6152a33bee2SGuy Eilam err = sta_info_insert_check(sta); 6162a33bee2SGuy Eilam if (err) { 6172a33bee2SGuy Eilam mutex_unlock(&local->sta_mtx); 6182a33bee2SGuy Eilam return err; 6192a33bee2SGuy Eilam } 6202a33bee2SGuy Eilam 6212a33bee2SGuy Eilam might_sleep(); 6222a33bee2SGuy Eilam 6232a33bee2SGuy Eilam err = sta_info_insert_non_ibss(sta); 6242a33bee2SGuy Eilam rcu_read_unlock(); 6252a33bee2SGuy Eilam return err; 6262a33bee2SGuy Eilam } 6272a33bee2SGuy Eilam 628004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid) 629004c872eSJohannes Berg { 630004c872eSJohannes Berg /* 631004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 632004c872eSJohannes Berg * so this line may not be changed to use the __set_bit() format. 633004c872eSJohannes Berg */ 634004c872eSJohannes Berg bss->tim[aid / 8] |= (1 << (aid % 8)); 635004c872eSJohannes Berg } 636004c872eSJohannes Berg 637004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid) 638004c872eSJohannes Berg { 639004c872eSJohannes Berg /* 640004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 641004c872eSJohannes Berg * so this line may not be changed to use the __clear_bit() format. 642004c872eSJohannes Berg */ 643004c872eSJohannes Berg bss->tim[aid / 8] &= ~(1 << (aid % 8)); 644004c872eSJohannes Berg } 645004c872eSJohannes Berg 646948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac) 647948d887dSJohannes Berg { 648948d887dSJohannes Berg /* If we ever support TIDs > 7, this obviously needs to be adjusted */ 649948d887dSJohannes Berg switch (ac) { 650948d887dSJohannes Berg case IEEE80211_AC_VO: 651948d887dSJohannes Berg return BIT(6) | BIT(7); 652948d887dSJohannes Berg case IEEE80211_AC_VI: 653948d887dSJohannes Berg return BIT(4) | BIT(5); 654948d887dSJohannes Berg case IEEE80211_AC_BE: 655948d887dSJohannes Berg return BIT(0) | BIT(3); 656948d887dSJohannes Berg case IEEE80211_AC_BK: 657948d887dSJohannes Berg return BIT(1) | BIT(2); 658948d887dSJohannes Berg default: 659948d887dSJohannes Berg WARN_ON(1); 660948d887dSJohannes Berg return 0; 661948d887dSJohannes Berg } 662948d887dSJohannes Berg } 663948d887dSJohannes Berg 664c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta) 665004c872eSJohannes Berg { 666c868cb35SJohannes Berg struct ieee80211_local *local = sta->local; 667c868cb35SJohannes Berg struct ieee80211_if_ap *bss = sta->sdata->bss; 668d0709a65SJohannes Berg unsigned long flags; 669948d887dSJohannes Berg bool indicate_tim = false; 670948d887dSJohannes Berg u8 ignore_for_tim = sta->sta.uapsd_queues; 671948d887dSJohannes Berg int ac; 672004c872eSJohannes Berg 673c868cb35SJohannes Berg if (WARN_ON_ONCE(!sta->sdata->bss)) 674c868cb35SJohannes Berg return; 6753e122be0SJohannes Berg 676c868cb35SJohannes Berg /* No need to do anything if the driver does all */ 677c868cb35SJohannes Berg if (local->hw.flags & IEEE80211_HW_AP_LINK_PS) 678c868cb35SJohannes Berg return; 679004c872eSJohannes Berg 680c868cb35SJohannes Berg if (sta->dead) 681c868cb35SJohannes Berg goto done; 6823e122be0SJohannes Berg 683948d887dSJohannes Berg /* 684948d887dSJohannes Berg * If all ACs are delivery-enabled then we should build 685948d887dSJohannes Berg * the TIM bit for all ACs anyway; if only some are then 686948d887dSJohannes Berg * we ignore those and build the TIM bit using only the 687948d887dSJohannes Berg * non-enabled ones. 688948d887dSJohannes Berg */ 689948d887dSJohannes Berg if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1) 690948d887dSJohannes Berg ignore_for_tim = 0; 691948d887dSJohannes Berg 692948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 693948d887dSJohannes Berg unsigned long tids; 694948d887dSJohannes Berg 695948d887dSJohannes Berg if (ignore_for_tim & BIT(ac)) 696948d887dSJohannes Berg continue; 697948d887dSJohannes Berg 698948d887dSJohannes Berg indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) || 699948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac]); 700948d887dSJohannes Berg if (indicate_tim) 701948d887dSJohannes Berg break; 702948d887dSJohannes Berg 703948d887dSJohannes Berg tids = ieee80211_tids_for_ac(ac); 704948d887dSJohannes Berg 705948d887dSJohannes Berg indicate_tim |= 706948d887dSJohannes Berg sta->driver_buffered_tids & tids; 707948d887dSJohannes Berg } 708c868cb35SJohannes Berg 709c868cb35SJohannes Berg done: 710c868cb35SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 711c868cb35SJohannes Berg 712948d887dSJohannes Berg if (indicate_tim) 713c868cb35SJohannes Berg __bss_tim_set(bss, sta->sta.aid); 714c868cb35SJohannes Berg else 71517741cdcSJohannes Berg __bss_tim_clear(bss, sta->sta.aid); 7163e122be0SJohannes Berg 717c868cb35SJohannes Berg if (local->ops->set_tim) { 718c868cb35SJohannes Berg local->tim_in_locked_section = true; 719948d887dSJohannes Berg drv_set_tim(local, &sta->sta, indicate_tim); 720c868cb35SJohannes Berg local->tim_in_locked_section = false; 721004c872eSJohannes Berg } 722004c872eSJohannes Berg 723c868cb35SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 724004c872eSJohannes Berg } 725004c872eSJohannes Berg 726cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb) 727f0706e82SJiri Benc { 728e039fa4aSJohannes Berg struct ieee80211_tx_info *info; 729f0706e82SJiri Benc int timeout; 730f0706e82SJiri Benc 731f0706e82SJiri Benc if (!skb) 732cd0b8d89SJohannes Berg return false; 733f0706e82SJiri Benc 734e039fa4aSJohannes Berg info = IEEE80211_SKB_CB(skb); 735f0706e82SJiri Benc 736f0706e82SJiri Benc /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 73757c4d7b4SJohannes Berg timeout = (sta->listen_interval * 73857c4d7b4SJohannes Berg sta->sdata->vif.bss_conf.beacon_int * 73957c4d7b4SJohannes Berg 32 / 15625) * HZ; 740f0706e82SJiri Benc if (timeout < STA_TX_BUFFER_EXPIRE) 741f0706e82SJiri Benc timeout = STA_TX_BUFFER_EXPIRE; 742e039fa4aSJohannes Berg return time_after(jiffies, info->control.jiffies + timeout); 743f0706e82SJiri Benc } 744f0706e82SJiri Benc 745f0706e82SJiri Benc 746948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local, 747948d887dSJohannes Berg struct sta_info *sta, int ac) 748f0706e82SJiri Benc { 749f0706e82SJiri Benc unsigned long flags; 750f0706e82SJiri Benc struct sk_buff *skb; 751f0706e82SJiri Benc 75260750397SJohannes Berg /* 75360750397SJohannes Berg * First check for frames that should expire on the filtered 75460750397SJohannes Berg * queue. Frames here were rejected by the driver and are on 75560750397SJohannes Berg * a separate queue to avoid reordering with normal PS-buffered 75660750397SJohannes Berg * frames. They also aren't accounted for right now in the 75760750397SJohannes Berg * total_ps_buffered counter. 75860750397SJohannes Berg */ 75960750397SJohannes Berg for (;;) { 760948d887dSJohannes Berg spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 761948d887dSJohannes Berg skb = skb_peek(&sta->tx_filtered[ac]); 76260750397SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 763948d887dSJohannes Berg skb = __skb_dequeue(&sta->tx_filtered[ac]); 76460750397SJohannes Berg else 76560750397SJohannes Berg skb = NULL; 766948d887dSJohannes Berg spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 76760750397SJohannes Berg 76860750397SJohannes Berg /* 76960750397SJohannes Berg * Frames are queued in order, so if this one 77060750397SJohannes Berg * hasn't expired yet we can stop testing. If 77160750397SJohannes Berg * we actually reached the end of the queue we 77260750397SJohannes Berg * also need to stop, of course. 77360750397SJohannes Berg */ 77460750397SJohannes Berg if (!skb) 77560750397SJohannes Berg break; 77660750397SJohannes Berg dev_kfree_skb(skb); 77760750397SJohannes Berg } 77860750397SJohannes Berg 77960750397SJohannes Berg /* 78060750397SJohannes Berg * Now also check the normal PS-buffered queue, this will 78160750397SJohannes Berg * only find something if the filtered queue was emptied 78260750397SJohannes Berg * since the filtered frames are all before the normal PS 78360750397SJohannes Berg * buffered frames. 78460750397SJohannes Berg */ 785f0706e82SJiri Benc for (;;) { 786948d887dSJohannes Berg spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 787948d887dSJohannes Berg skb = skb_peek(&sta->ps_tx_buf[ac]); 78857c4d7b4SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 789948d887dSJohannes Berg skb = __skb_dequeue(&sta->ps_tx_buf[ac]); 790836341a7SJohannes Berg else 791f0706e82SJiri Benc skb = NULL; 792948d887dSJohannes Berg spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 793f0706e82SJiri Benc 79460750397SJohannes Berg /* 79560750397SJohannes Berg * frames are queued in order, so if this one 79660750397SJohannes Berg * hasn't expired yet (or we reached the end of 79760750397SJohannes Berg * the queue) we can stop testing 79860750397SJohannes Berg */ 799836341a7SJohannes Berg if (!skb) 800836341a7SJohannes Berg break; 801836341a7SJohannes Berg 802f0706e82SJiri Benc local->total_ps_buffered--; 803f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 8040c68ae26SJohannes Berg printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n", 8050c68ae26SJohannes Berg sta->sta.addr); 806f4ea83ddSJohannes Berg #endif 807f0706e82SJiri Benc dev_kfree_skb(skb); 808f0706e82SJiri Benc } 8093393a608SJuuso Oikarinen 81060750397SJohannes Berg /* 81160750397SJohannes Berg * Finally, recalculate the TIM bit for this station -- it might 81260750397SJohannes Berg * now be clear because the station was too slow to retrieve its 81360750397SJohannes Berg * frames. 81460750397SJohannes Berg */ 81560750397SJohannes Berg sta_info_recalc_tim(sta); 81660750397SJohannes Berg 81760750397SJohannes Berg /* 81860750397SJohannes Berg * Return whether there are any frames still buffered, this is 81960750397SJohannes Berg * used to check whether the cleanup timer still needs to run, 82060750397SJohannes Berg * if there are no frames we don't need to rearm the timer. 82160750397SJohannes Berg */ 822948d887dSJohannes Berg return !(skb_queue_empty(&sta->ps_tx_buf[ac]) && 823948d887dSJohannes Berg skb_queue_empty(&sta->tx_filtered[ac])); 824948d887dSJohannes Berg } 825948d887dSJohannes Berg 826948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 827948d887dSJohannes Berg struct sta_info *sta) 828948d887dSJohannes Berg { 829948d887dSJohannes Berg bool have_buffered = false; 830948d887dSJohannes Berg int ac; 831948d887dSJohannes Berg 832948d887dSJohannes Berg /* This is only necessary for stations on BSS interfaces */ 833948d887dSJohannes Berg if (!sta->sdata->bss) 834948d887dSJohannes Berg return false; 835948d887dSJohannes Berg 836948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 837948d887dSJohannes Berg have_buffered |= 838948d887dSJohannes Berg sta_info_cleanup_expire_buffered_ac(local, sta, ac); 839948d887dSJohannes Berg 840948d887dSJohannes Berg return have_buffered; 841f0706e82SJiri Benc } 842f0706e82SJiri Benc 84334e89507SJohannes Berg static int __must_check __sta_info_destroy(struct sta_info *sta) 84434e89507SJohannes Berg { 84534e89507SJohannes Berg struct ieee80211_local *local; 84634e89507SJohannes Berg struct ieee80211_sub_if_data *sdata; 84734e89507SJohannes Berg unsigned long flags; 848948d887dSJohannes Berg int ret, i, ac; 84934e89507SJohannes Berg 85034e89507SJohannes Berg might_sleep(); 85134e89507SJohannes Berg 85234e89507SJohannes Berg if (!sta) 85334e89507SJohannes Berg return -ENOENT; 85434e89507SJohannes Berg 85534e89507SJohannes Berg local = sta->local; 85634e89507SJohannes Berg sdata = sta->sdata; 85734e89507SJohannes Berg 858098a6070SJohannes Berg /* 859098a6070SJohannes Berg * Before removing the station from the driver and 860098a6070SJohannes Berg * rate control, it might still start new aggregation 861098a6070SJohannes Berg * sessions -- block that to make sure the tear-down 862098a6070SJohannes Berg * will be sufficient. 863098a6070SJohannes Berg */ 864098a6070SJohannes Berg set_sta_flags(sta, WLAN_STA_BLOCK_BA); 86553f73c09SJohannes Berg ieee80211_sta_tear_down_BA_sessions(sta, true); 866098a6070SJohannes Berg 86734e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 86834e89507SJohannes Berg ret = sta_info_hash_del(local, sta); 86934e89507SJohannes Berg /* this might still be the pending list ... which is fine */ 87034e89507SJohannes Berg if (!ret) 87134e89507SJohannes Berg list_del(&sta->list); 87234e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 87334e89507SJohannes Berg if (ret) 87434e89507SJohannes Berg return ret; 87534e89507SJohannes Berg 8768cb23153SJohannes Berg mutex_lock(&local->key_mtx); 877e31b8213SJohannes Berg for (i = 0; i < NUM_DEFAULT_KEYS; i++) 87840b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i])); 879e31b8213SJohannes Berg if (sta->ptk) 88040b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->ptk)); 8818cb23153SJohannes Berg mutex_unlock(&local->key_mtx); 88234e89507SJohannes Berg 88334e89507SJohannes Berg sta->dead = true; 88434e89507SJohannes Berg 88534e89507SJohannes Berg if (test_and_clear_sta_flags(sta, 88634e89507SJohannes Berg WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) { 88734e89507SJohannes Berg BUG_ON(!sdata->bss); 88834e89507SJohannes Berg 88934e89507SJohannes Berg atomic_dec(&sdata->bss->num_sta_ps); 890c868cb35SJohannes Berg sta_info_recalc_tim(sta); 89134e89507SJohannes Berg } 89234e89507SJohannes Berg 89334e89507SJohannes Berg local->num_sta--; 89434e89507SJohannes Berg local->sta_generation++; 89534e89507SJohannes Berg 89634e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 89734e89507SJohannes Berg rcu_assign_pointer(sdata->u.vlan.sta, NULL); 89834e89507SJohannes Berg 89934e89507SJohannes Berg if (sta->uploaded) { 90034e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 90134e89507SJohannes Berg sdata = container_of(sdata->bss, 90234e89507SJohannes Berg struct ieee80211_sub_if_data, 90334e89507SJohannes Berg u.ap); 90434e89507SJohannes Berg drv_sta_remove(local, sdata, &sta->sta); 90534e89507SJohannes Berg sdata = sta->sdata; 90634e89507SJohannes Berg } 90734e89507SJohannes Berg 908e64b3795SJohannes Berg /* 909e64b3795SJohannes Berg * At this point, after we wait for an RCU grace period, 910e64b3795SJohannes Berg * neither mac80211 nor the driver can reference this 911e64b3795SJohannes Berg * sta struct any more except by still existing timers 912e64b3795SJohannes Berg * associated with this station that we clean up below. 913e64b3795SJohannes Berg */ 914e64b3795SJohannes Berg synchronize_rcu(); 915e64b3795SJohannes Berg 916948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 917948d887dSJohannes Berg local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]); 918948d887dSJohannes Berg __skb_queue_purge(&sta->ps_tx_buf[ac]); 919948d887dSJohannes Berg __skb_queue_purge(&sta->tx_filtered[ac]); 920948d887dSJohannes Berg } 921c868cb35SJohannes Berg 92234e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH 923e64b3795SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 92434e89507SJohannes Berg mesh_accept_plinks_update(sdata); 92534e89507SJohannes Berg #endif 92634e89507SJohannes Berg 92734e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 9280fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr); 92934e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 93034e89507SJohannes Berg cancel_work_sync(&sta->drv_unblock_wk); 93134e89507SJohannes Berg 932ec15e68bSJouni Malinen cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL); 933ec15e68bSJouni Malinen 93434e89507SJohannes Berg rate_control_remove_sta_debugfs(sta); 93534e89507SJohannes Berg ieee80211_sta_debugfs_remove(sta); 93634e89507SJohannes Berg 93734e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH 93834e89507SJohannes Berg if (ieee80211_vif_is_mesh(&sta->sdata->vif)) { 93934e89507SJohannes Berg mesh_plink_deactivate(sta); 94034e89507SJohannes Berg del_timer_sync(&sta->plink_timer); 94134e89507SJohannes Berg } 94234e89507SJohannes Berg #endif 94334e89507SJohannes Berg 94434e89507SJohannes Berg __sta_info_free(local, sta); 94534e89507SJohannes Berg 94634e89507SJohannes Berg return 0; 94734e89507SJohannes Berg } 94834e89507SJohannes Berg 94934e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) 95034e89507SJohannes Berg { 95134e89507SJohannes Berg struct sta_info *sta; 95234e89507SJohannes Berg int ret; 95334e89507SJohannes Berg 95434e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 9552a33bee2SGuy Eilam sta = sta_info_get_rx(sdata, addr); 95634e89507SJohannes Berg ret = __sta_info_destroy(sta); 95734e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 95834e89507SJohannes Berg 95934e89507SJohannes Berg return ret; 96034e89507SJohannes Berg } 96134e89507SJohannes Berg 96234e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, 96334e89507SJohannes Berg const u8 *addr) 96434e89507SJohannes Berg { 96534e89507SJohannes Berg struct sta_info *sta; 96634e89507SJohannes Berg int ret; 96734e89507SJohannes Berg 96834e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 9692a33bee2SGuy Eilam sta = sta_info_get_bss_rx(sdata, addr); 97034e89507SJohannes Berg ret = __sta_info_destroy(sta); 97134e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 97234e89507SJohannes Berg 97334e89507SJohannes Berg return ret; 97434e89507SJohannes Berg } 975f0706e82SJiri Benc 976f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data) 977f0706e82SJiri Benc { 978f0706e82SJiri Benc struct ieee80211_local *local = (struct ieee80211_local *) data; 979f0706e82SJiri Benc struct sta_info *sta; 9803393a608SJuuso Oikarinen bool timer_needed = false; 981f0706e82SJiri Benc 982d0709a65SJohannes Berg rcu_read_lock(); 983d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) 9843393a608SJuuso Oikarinen if (sta_info_cleanup_expire_buffered(local, sta)) 9853393a608SJuuso Oikarinen timer_needed = true; 986d0709a65SJohannes Berg rcu_read_unlock(); 987f0706e82SJiri Benc 9885bb644a0SJohannes Berg if (local->quiescing) 9895bb644a0SJohannes Berg return; 9905bb644a0SJohannes Berg 9913393a608SJuuso Oikarinen if (!timer_needed) 9923393a608SJuuso Oikarinen return; 9933393a608SJuuso Oikarinen 99426d59535SJohannes Berg mod_timer(&local->sta_cleanup, 99526d59535SJohannes Berg round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 996f0706e82SJiri Benc } 997f0706e82SJiri Benc 998f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local) 999f0706e82SJiri Benc { 1000d0709a65SJohannes Berg spin_lock_init(&local->sta_lock); 100134e89507SJohannes Berg mutex_init(&local->sta_mtx); 1002f0706e82SJiri Benc INIT_LIST_HEAD(&local->sta_list); 100334e89507SJohannes Berg INIT_LIST_HEAD(&local->sta_pending_list); 100434e89507SJohannes Berg INIT_WORK(&local->sta_finish_work, sta_info_finish_work); 1005f0706e82SJiri Benc 1006b24b8a24SPavel Emelyanov setup_timer(&local->sta_cleanup, sta_info_cleanup, 1007b24b8a24SPavel Emelyanov (unsigned long)local); 1008f0706e82SJiri Benc } 1009f0706e82SJiri Benc 1010f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local) 1011f0706e82SJiri Benc { 1012f0706e82SJiri Benc del_timer(&local->sta_cleanup); 1013be8755e1SMichael Wu sta_info_flush(local, NULL); 1014f0706e82SJiri Benc } 1015f0706e82SJiri Benc 1016f0706e82SJiri Benc /** 1017f0706e82SJiri Benc * sta_info_flush - flush matching STA entries from the STA table 101844213b5eSJohannes Berg * 101944213b5eSJohannes Berg * Returns the number of removed STA entries. 102044213b5eSJohannes Berg * 1021f0706e82SJiri Benc * @local: local interface data 1022d0709a65SJohannes Berg * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs 1023f0706e82SJiri Benc */ 102444213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local, 1025d0709a65SJohannes Berg struct ieee80211_sub_if_data *sdata) 1026f0706e82SJiri Benc { 1027f0706e82SJiri Benc struct sta_info *sta, *tmp; 102844213b5eSJohannes Berg int ret = 0; 1029f0706e82SJiri Benc 1030d0709a65SJohannes Berg might_sleep(); 1031d0709a65SJohannes Berg 103234e89507SJohannes Berg mutex_lock(&local->sta_mtx); 1033d0709a65SJohannes Berg 103434e89507SJohannes Berg sta_info_finish_pending(local); 103534e89507SJohannes Berg 103634e89507SJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 103734e89507SJohannes Berg if (!sdata || sdata == sta->sdata) 103834e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 103934e89507SJohannes Berg } 104034e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 104144213b5eSJohannes Berg 104244213b5eSJohannes Berg return ret; 1043f0706e82SJiri Benc } 1044dc6676b7SJohannes Berg 104524723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 104624723d1bSJohannes Berg unsigned long exp_time) 104724723d1bSJohannes Berg { 104824723d1bSJohannes Berg struct ieee80211_local *local = sdata->local; 104924723d1bSJohannes Berg struct sta_info *sta, *tmp; 105024723d1bSJohannes Berg 105134e89507SJohannes Berg mutex_lock(&local->sta_mtx); 105224723d1bSJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) 105324723d1bSJohannes Berg if (time_after(jiffies, sta->last_rx + exp_time)) { 105424723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG 10550c68ae26SJohannes Berg printk(KERN_DEBUG "%s: expiring inactive STA %pM\n", 105647846c9bSJohannes Berg sdata->name, sta->sta.addr); 105724723d1bSJohannes Berg #endif 105834e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 105924723d1bSJohannes Berg } 106034e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 106124723d1bSJohannes Berg } 106217741cdcSJohannes Berg 1063686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 1064686b9cb9SBen Greear const u8 *addr, 1065686b9cb9SBen Greear const u8 *localaddr) 106617741cdcSJohannes Berg { 1067abe60632SJohannes Berg struct sta_info *sta, *nxt; 106817741cdcSJohannes Berg 1069686b9cb9SBen Greear /* 1070686b9cb9SBen Greear * Just return a random station if localaddr is NULL 1071686b9cb9SBen Greear * ... first in list. 1072686b9cb9SBen Greear */ 1073f7c65594SJohannes Berg for_each_sta_info(hw_to_local(hw), addr, sta, nxt) { 1074686b9cb9SBen Greear if (localaddr && 1075686b9cb9SBen Greear compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0) 1076686b9cb9SBen Greear continue; 1077f7c65594SJohannes Berg if (!sta->uploaded) 1078f7c65594SJohannes Berg return NULL; 107917741cdcSJohannes Berg return &sta->sta; 1080f7c65594SJohannes Berg } 1081f7c65594SJohannes Berg 1082abe60632SJohannes Berg return NULL; 108317741cdcSJohannes Berg } 1084686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); 10855ed176e1SJohannes Berg 10865ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, 10875ed176e1SJohannes Berg const u8 *addr) 10885ed176e1SJohannes Berg { 1089f7c65594SJohannes Berg struct sta_info *sta; 10905ed176e1SJohannes Berg 10915ed176e1SJohannes Berg if (!vif) 10925ed176e1SJohannes Berg return NULL; 10935ed176e1SJohannes Berg 1094f7c65594SJohannes Berg sta = sta_info_get_bss(vif_to_sdata(vif), addr); 1095f7c65594SJohannes Berg if (!sta) 1096f7c65594SJohannes Berg return NULL; 10975ed176e1SJohannes Berg 1098f7c65594SJohannes Berg if (!sta->uploaded) 1099f7c65594SJohannes Berg return NULL; 1100f7c65594SJohannes Berg 1101f7c65594SJohannes Berg return &sta->sta; 11025ed176e1SJohannes Berg } 110317741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta); 1104af818581SJohannes Berg 110550a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta) 110650a9432dSJohannes Berg { 110750a9432dSJohannes Berg struct sta_info *sta = _sta; 110850a9432dSJohannes Berg 110950a9432dSJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER | WLAN_STA_PS_STA); 111050a9432dSJohannes Berg } 111150a9432dSJohannes Berg 1112af818581SJohannes Berg /* powersave support code */ 1113af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) 1114af818581SJohannes Berg { 1115af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1116af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 1117948d887dSJohannes Berg struct sk_buff_head pending; 1118948d887dSJohannes Berg int filtered = 0, buffered = 0, ac; 1119af818581SJohannes Berg 1120948d887dSJohannes Berg BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1); 1121948d887dSJohannes Berg sta->driver_buffered_tids = 0; 1122948d887dSJohannes Berg 1123d057e5a3SArik Nemtsov if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS)) 112412375ef9SJohannes Berg drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 1125af818581SJohannes Berg 1126948d887dSJohannes Berg skb_queue_head_init(&pending); 1127948d887dSJohannes Berg 1128af818581SJohannes Berg /* Send all buffered frames to the station */ 1129948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1130948d887dSJohannes Berg int count = skb_queue_len(&pending), tmp; 1131948d887dSJohannes Berg 1132948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending); 1133948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1134948d887dSJohannes Berg filtered += tmp - count; 1135948d887dSJohannes Berg count = tmp; 1136948d887dSJohannes Berg 1137948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending); 1138948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1139948d887dSJohannes Berg buffered += tmp - count; 1140948d887dSJohannes Berg } 1141948d887dSJohannes Berg 1142948d887dSJohannes Berg ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta); 1143948d887dSJohannes Berg 1144af818581SJohannes Berg local->total_ps_buffered -= buffered; 1145af818581SJohannes Berg 1146c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1147c868cb35SJohannes Berg 1148af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 1149af818581SJohannes Berg printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames " 115047846c9bSJohannes Berg "since STA not sleeping anymore\n", sdata->name, 1151948d887dSJohannes Berg sta->sta.addr, sta->sta.aid, filtered, buffered); 1152af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 1153af818581SJohannes Berg } 1154af818581SJohannes Berg 1155af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 1156af818581SJohannes Berg { 1157af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1158af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 1159948d887dSJohannes Berg struct sk_buff *skb = NULL; 1160*4049e09aSJohannes Berg bool found = false; 1161948d887dSJohannes Berg bool more_data = false; 1162948d887dSJohannes Berg int ac; 1163*4049e09aSJohannes Berg unsigned long driver_release_tids = 0; 1164948d887dSJohannes Berg u8 ignore_for_response = sta->sta.uapsd_queues; 1165af818581SJohannes Berg 1166948d887dSJohannes Berg /* 1167948d887dSJohannes Berg * If all ACs are delivery-enabled then we should reply 1168948d887dSJohannes Berg * from any of them, if only some are enabled we reply 1169948d887dSJohannes Berg * only from the non-enabled ones. 1170948d887dSJohannes Berg */ 1171948d887dSJohannes Berg if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1) 1172948d887dSJohannes Berg ignore_for_response = 0; 1173948d887dSJohannes Berg 1174948d887dSJohannes Berg /* 1175948d887dSJohannes Berg * Get response frame and more data bit for it. 1176948d887dSJohannes Berg */ 1177948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1178*4049e09aSJohannes Berg unsigned long tids; 1179*4049e09aSJohannes Berg 1180948d887dSJohannes Berg if (ignore_for_response & BIT(ac)) 1181948d887dSJohannes Berg continue; 1182948d887dSJohannes Berg 1183*4049e09aSJohannes Berg tids = ieee80211_tids_for_ac(ac); 1184*4049e09aSJohannes Berg 1185*4049e09aSJohannes Berg if (!found) { 1186*4049e09aSJohannes Berg driver_release_tids = sta->driver_buffered_tids & tids; 1187*4049e09aSJohannes Berg if (driver_release_tids) { 1188*4049e09aSJohannes Berg found = true; 1189*4049e09aSJohannes Berg } else { 1190948d887dSJohannes Berg skb = skb_dequeue(&sta->tx_filtered[ac]); 1191948d887dSJohannes Berg if (!skb) { 1192948d887dSJohannes Berg skb = skb_dequeue(&sta->ps_tx_buf[ac]); 1193af818581SJohannes Berg if (skb) 1194af818581SJohannes Berg local->total_ps_buffered--; 1195af818581SJohannes Berg } 1196*4049e09aSJohannes Berg if (skb) 1197*4049e09aSJohannes Berg found = true; 1198948d887dSJohannes Berg } 1199948d887dSJohannes Berg 1200*4049e09aSJohannes Berg /* 1201*4049e09aSJohannes Berg * If the driver has data on more than one TID then 1202*4049e09aSJohannes Berg * certainly there's more data if we release just a 1203*4049e09aSJohannes Berg * single frame now (from a single TID). 1204*4049e09aSJohannes Berg */ 1205*4049e09aSJohannes Berg if (hweight16(driver_release_tids) > 1) { 1206*4049e09aSJohannes Berg more_data = true; 1207*4049e09aSJohannes Berg driver_release_tids = 1208*4049e09aSJohannes Berg BIT(ffs(driver_release_tids) - 1); 1209*4049e09aSJohannes Berg break; 1210*4049e09aSJohannes Berg } 1211*4049e09aSJohannes Berg } 1212948d887dSJohannes Berg 1213948d887dSJohannes Berg if (!skb_queue_empty(&sta->tx_filtered[ac]) || 1214948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac])) { 1215948d887dSJohannes Berg more_data = true; 1216948d887dSJohannes Berg break; 1217948d887dSJohannes Berg } 1218948d887dSJohannes Berg } 1219af818581SJohannes Berg 1220*4049e09aSJohannes Berg if (!found) { 1221*4049e09aSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 1222*4049e09aSJohannes Berg /* 1223*4049e09aSJohannes Berg * FIXME: This can be the result of a race condition between 1224*4049e09aSJohannes Berg * us expiring a frame and the station polling for it. 1225*4049e09aSJohannes Berg * Should we send it a null-func frame indicating we 1226*4049e09aSJohannes Berg * have nothing buffered for it? 1227*4049e09aSJohannes Berg */ 1228*4049e09aSJohannes Berg printk(KERN_DEBUG "%s: STA %pM sent PS Poll even " 1229*4049e09aSJohannes Berg "though there are no buffered frames for it\n", 1230*4049e09aSJohannes Berg sdata->name, sta->sta.addr); 1231*4049e09aSJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 1232*4049e09aSJohannes Berg 1233*4049e09aSJohannes Berg return; 1234*4049e09aSJohannes Berg } 1235*4049e09aSJohannes Berg 1236af818581SJohannes Berg if (skb) { 1237af818581SJohannes Berg struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 1238af818581SJohannes Berg struct ieee80211_hdr *hdr = 1239af818581SJohannes Berg (struct ieee80211_hdr *) skb->data; 1240af818581SJohannes Berg 1241af818581SJohannes Berg /* 1242af818581SJohannes Berg * Tell TX path to send this frame even though the STA may 1243af818581SJohannes Berg * still remain is PS mode after this frame exchange. 1244af818581SJohannes Berg */ 1245af818581SJohannes Berg info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE; 1246af818581SJohannes Berg 1247af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 1248948d887dSJohannes Berg printk(KERN_DEBUG "STA %pM aid %d: PS Poll\n", 1249948d887dSJohannes Berg sta->sta.addr, sta->sta.aid); 1250af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 1251af818581SJohannes Berg 1252af818581SJohannes Berg /* Use MoreData flag to indicate whether there are more 1253af818581SJohannes Berg * buffered frames for this STA */ 1254948d887dSJohannes Berg if (!more_data) 1255af818581SJohannes Berg hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 1256af818581SJohannes Berg else 1257af818581SJohannes Berg hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA); 1258af818581SJohannes Berg 1259af818581SJohannes Berg ieee80211_add_pending_skb(local, skb); 1260af818581SJohannes Berg 1261c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1262af818581SJohannes Berg } else { 1263af818581SJohannes Berg /* 1264*4049e09aSJohannes Berg * We need to release a frame that is buffered somewhere in the 1265*4049e09aSJohannes Berg * driver ... it'll have to handle that. 1266*4049e09aSJohannes Berg * Note that, as per the comment above, it'll also have to see 1267*4049e09aSJohannes Berg * if there is more than just one frame on the specific TID that 1268*4049e09aSJohannes Berg * we're releasing from, and it needs to set the more-data bit 1269*4049e09aSJohannes Berg * accordingly if we tell it that there's no more data. If we do 1270*4049e09aSJohannes Berg * tell it there's more data, then of course the more-data bit 1271*4049e09aSJohannes Berg * needs to be set anyway. 1272af818581SJohannes Berg */ 1273*4049e09aSJohannes Berg drv_release_buffered_frames(local, sta, driver_release_tids, 1274*4049e09aSJohannes Berg 1, IEEE80211_FRAME_RELEASE_PSPOLL, 1275*4049e09aSJohannes Berg more_data); 1276*4049e09aSJohannes Berg 1277*4049e09aSJohannes Berg /* 1278*4049e09aSJohannes Berg * Note that we don't recalculate the TIM bit here as it would 1279*4049e09aSJohannes Berg * most likely have no effect at all unless the driver told us 1280*4049e09aSJohannes Berg * that the TID became empty before returning here from the 1281*4049e09aSJohannes Berg * release function. 1282*4049e09aSJohannes Berg * Either way, however, when the driver tells us that the TID 1283*4049e09aSJohannes Berg * became empty we'll do the TIM recalculation. 1284*4049e09aSJohannes Berg */ 1285af818581SJohannes Berg } 1286af818581SJohannes Berg } 1287af818581SJohannes Berg 1288af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 1289af818581SJohannes Berg struct ieee80211_sta *pubsta, bool block) 1290af818581SJohannes Berg { 1291af818581SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1292af818581SJohannes Berg 1293b5878a2dSJohannes Berg trace_api_sta_block_awake(sta->local, pubsta, block); 1294b5878a2dSJohannes Berg 1295af818581SJohannes Berg if (block) 1296af818581SJohannes Berg set_sta_flags(sta, WLAN_STA_PS_DRIVER); 129750a9432dSJohannes Berg else if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) 1298af818581SJohannes Berg ieee80211_queue_work(hw, &sta->drv_unblock_wk); 1299af818581SJohannes Berg } 1300af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake); 1301dcf55fb5SFelix Fietkau 1302042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, 1303042ec453SJohannes Berg u8 tid, bool buffered) 1304dcf55fb5SFelix Fietkau { 1305dcf55fb5SFelix Fietkau struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1306dcf55fb5SFelix Fietkau 1307948d887dSJohannes Berg if (WARN_ON(tid >= STA_TID_NUM)) 1308042ec453SJohannes Berg return; 1309042ec453SJohannes Berg 1310948d887dSJohannes Berg if (buffered) 1311948d887dSJohannes Berg set_bit(tid, &sta->driver_buffered_tids); 1312948d887dSJohannes Berg else 1313948d887dSJohannes Berg clear_bit(tid, &sta->driver_buffered_tids); 1314948d887dSJohannes Berg 1315c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1316dcf55fb5SFelix Fietkau } 1317042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered); 1318