1f0706e82SJiri Benc /* 2f0706e82SJiri Benc * Copyright 2002-2005, Instant802 Networks, Inc. 3f0706e82SJiri Benc * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> 4f0706e82SJiri Benc * 5f0706e82SJiri Benc * This program is free software; you can redistribute it and/or modify 6f0706e82SJiri Benc * it under the terms of the GNU General Public License version 2 as 7f0706e82SJiri Benc * published by the Free Software Foundation. 8f0706e82SJiri Benc */ 9f0706e82SJiri Benc 10f0706e82SJiri Benc #include <linux/module.h> 11f0706e82SJiri Benc #include <linux/init.h> 12f0706e82SJiri Benc #include <linux/netdevice.h> 13f0706e82SJiri Benc #include <linux/types.h> 14f0706e82SJiri Benc #include <linux/slab.h> 15f0706e82SJiri Benc #include <linux/skbuff.h> 16f0706e82SJiri Benc #include <linux/if_arp.h> 170d174406SJohannes Berg #include <linux/timer.h> 18d0709a65SJohannes Berg #include <linux/rtnetlink.h> 19f0706e82SJiri Benc 20f0706e82SJiri Benc #include <net/mac80211.h> 21f0706e82SJiri Benc #include "ieee80211_i.h" 2224487981SJohannes Berg #include "driver-ops.h" 232c8dccc7SJohannes Berg #include "rate.h" 24f0706e82SJiri Benc #include "sta_info.h" 25e9f207f0SJiri Benc #include "debugfs_sta.h" 26ee385855SLuis Carlos Cobo #include "mesh.h" 27ce662b44SJohannes Berg #include "wme.h" 28f0706e82SJiri Benc 29d0709a65SJohannes Berg /** 30d0709a65SJohannes Berg * DOC: STA information lifetime rules 31d0709a65SJohannes Berg * 32d0709a65SJohannes Berg * STA info structures (&struct sta_info) are managed in a hash table 33d0709a65SJohannes Berg * for faster lookup and a list for iteration. They are managed using 34d0709a65SJohannes Berg * RCU, i.e. access to the list and hash table is protected by RCU. 35d0709a65SJohannes Berg * 3634e89507SJohannes Berg * Upon allocating a STA info structure with sta_info_alloc(), the caller 3734e89507SJohannes Berg * owns that structure. It must then insert it into the hash table using 3834e89507SJohannes Berg * either sta_info_insert() or sta_info_insert_rcu(); only in the latter 3934e89507SJohannes Berg * case (which acquires an rcu read section but must not be called from 4034e89507SJohannes Berg * within one) will the pointer still be valid after the call. Note that 4134e89507SJohannes Berg * the caller may not do much with the STA info before inserting it, in 4234e89507SJohannes Berg * particular, it may not start any mesh peer link management or add 4334e89507SJohannes Berg * encryption keys. 4493e5deb1SJohannes Berg * 4593e5deb1SJohannes Berg * When the insertion fails (sta_info_insert()) returns non-zero), the 4693e5deb1SJohannes Berg * structure will have been freed by sta_info_insert()! 47d0709a65SJohannes Berg * 4834e89507SJohannes Berg * Station entries are added by mac80211 when you establish a link with a 497e189a12SLuis R. Rodriguez * peer. This means different things for the different type of interfaces 507e189a12SLuis R. Rodriguez * we support. For a regular station this mean we add the AP sta when we 5125985edcSLucas De Marchi * receive an association response from the AP. For IBSS this occurs when 5234e89507SJohannes Berg * get to know about a peer on the same IBSS. For WDS we add the sta for 5325985edcSLucas De Marchi * the peer immediately upon device open. When using AP mode we add stations 5434e89507SJohannes Berg * for each respective station upon request from userspace through nl80211. 557e189a12SLuis R. Rodriguez * 5634e89507SJohannes Berg * In order to remove a STA info structure, various sta_info_destroy_*() 5734e89507SJohannes Berg * calls are available. 58d0709a65SJohannes Berg * 5934e89507SJohannes Berg * There is no concept of ownership on a STA entry, each structure is 6034e89507SJohannes Berg * owned by the global hash table/list until it is removed. All users of 6134e89507SJohannes Berg * the structure need to be RCU protected so that the structure won't be 6234e89507SJohannes Berg * freed before they are done using it. 63d0709a65SJohannes Berg */ 64f0706e82SJiri Benc 65f0706e82SJiri Benc /* Caller must hold local->sta_lock */ 66be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local, 67f0706e82SJiri Benc struct sta_info *sta) 68f0706e82SJiri Benc { 69f0706e82SJiri Benc struct sta_info *s; 70f0706e82SJiri Benc 7140b275b6SJohannes Berg s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)], 7240b275b6SJohannes Berg lockdep_is_held(&local->sta_lock)); 73f0706e82SJiri Benc if (!s) 74be8755e1SMichael Wu return -ENOENT; 75be8755e1SMichael Wu if (s == sta) { 7617741cdcSJohannes Berg rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], 77d0709a65SJohannes Berg s->hnext); 78be8755e1SMichael Wu return 0; 79f0706e82SJiri Benc } 80f0706e82SJiri Benc 8140b275b6SJohannes Berg while (rcu_access_pointer(s->hnext) && 8240b275b6SJohannes Berg rcu_access_pointer(s->hnext) != sta) 8340b275b6SJohannes Berg s = rcu_dereference_protected(s->hnext, 8440b275b6SJohannes Berg lockdep_is_held(&local->sta_lock)); 8540b275b6SJohannes Berg if (rcu_access_pointer(s->hnext)) { 86d0709a65SJohannes Berg rcu_assign_pointer(s->hnext, sta->hnext); 87be8755e1SMichael Wu return 0; 88f0706e82SJiri Benc } 89f0706e82SJiri Benc 90be8755e1SMichael Wu return -ENOENT; 91f0706e82SJiri Benc } 92f0706e82SJiri Benc 93d0709a65SJohannes Berg /* protected by RCU */ 94abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata, 95abe60632SJohannes Berg const u8 *addr) 9643ba7e95SJohannes Berg { 97abe60632SJohannes Berg struct ieee80211_local *local = sdata->local; 9843ba7e95SJohannes Berg struct sta_info *sta; 9943ba7e95SJohannes Berg 1000379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1010379185bSJohannes Berg lockdep_is_held(&local->sta_lock) || 1020379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 10343ba7e95SJohannes Berg while (sta) { 1042a33bee2SGuy Eilam if (sta->sdata == sdata && !sta->dummy && 1052a33bee2SGuy Eilam memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 1062a33bee2SGuy Eilam break; 1072a33bee2SGuy Eilam sta = rcu_dereference_check(sta->hnext, 1082a33bee2SGuy Eilam lockdep_is_held(&local->sta_lock) || 1092a33bee2SGuy Eilam lockdep_is_held(&local->sta_mtx)); 1102a33bee2SGuy Eilam } 1112a33bee2SGuy Eilam return sta; 1122a33bee2SGuy Eilam } 1132a33bee2SGuy Eilam 1142a33bee2SGuy Eilam /* get a station info entry even if it is a dummy station*/ 1152a33bee2SGuy Eilam struct sta_info *sta_info_get_rx(struct ieee80211_sub_if_data *sdata, 1162a33bee2SGuy Eilam const u8 *addr) 1172a33bee2SGuy Eilam { 1182a33bee2SGuy Eilam struct ieee80211_local *local = sdata->local; 1192a33bee2SGuy Eilam struct sta_info *sta; 1202a33bee2SGuy Eilam 1212a33bee2SGuy Eilam sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1222a33bee2SGuy Eilam lockdep_is_held(&local->sta_lock) || 1232a33bee2SGuy Eilam lockdep_is_held(&local->sta_mtx)); 1242a33bee2SGuy Eilam while (sta) { 125abe60632SJohannes Berg if (sta->sdata == sdata && 126abe60632SJohannes Berg memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 12743ba7e95SJohannes Berg break; 1280379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1290379185bSJohannes Berg lockdep_is_held(&local->sta_lock) || 1300379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 13143ba7e95SJohannes Berg } 13243ba7e95SJohannes Berg return sta; 13343ba7e95SJohannes Berg } 13443ba7e95SJohannes Berg 1350e5ded5aSFelix Fietkau /* 1360e5ded5aSFelix Fietkau * Get sta info either from the specified interface 1370e5ded5aSFelix Fietkau * or from one of its vlans 1380e5ded5aSFelix Fietkau */ 1390e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata, 1400e5ded5aSFelix Fietkau const u8 *addr) 1410e5ded5aSFelix Fietkau { 1420e5ded5aSFelix Fietkau struct ieee80211_local *local = sdata->local; 1430e5ded5aSFelix Fietkau struct sta_info *sta; 1440e5ded5aSFelix Fietkau 1450379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1460379185bSJohannes Berg lockdep_is_held(&local->sta_lock) || 1470379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1480e5ded5aSFelix Fietkau while (sta) { 1490e5ded5aSFelix Fietkau if ((sta->sdata == sdata || 150a2c1e3daSJohannes Berg (sta->sdata->bss && sta->sdata->bss == sdata->bss)) && 1512a33bee2SGuy Eilam !sta->dummy && 1522a33bee2SGuy Eilam memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 1532a33bee2SGuy Eilam break; 1542a33bee2SGuy Eilam sta = rcu_dereference_check(sta->hnext, 1552a33bee2SGuy Eilam lockdep_is_held(&local->sta_lock) || 1562a33bee2SGuy Eilam lockdep_is_held(&local->sta_mtx)); 1572a33bee2SGuy Eilam } 1582a33bee2SGuy Eilam return sta; 1592a33bee2SGuy Eilam } 1602a33bee2SGuy Eilam 1612a33bee2SGuy Eilam /* 1622a33bee2SGuy Eilam * Get sta info either from the specified interface 1632a33bee2SGuy Eilam * or from one of its vlans (including dummy stations) 1642a33bee2SGuy Eilam */ 1652a33bee2SGuy Eilam struct sta_info *sta_info_get_bss_rx(struct ieee80211_sub_if_data *sdata, 1662a33bee2SGuy Eilam const u8 *addr) 1672a33bee2SGuy Eilam { 1682a33bee2SGuy Eilam struct ieee80211_local *local = sdata->local; 1692a33bee2SGuy Eilam struct sta_info *sta; 1702a33bee2SGuy Eilam 1712a33bee2SGuy Eilam sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1722a33bee2SGuy Eilam lockdep_is_held(&local->sta_lock) || 1732a33bee2SGuy Eilam lockdep_is_held(&local->sta_mtx)); 1742a33bee2SGuy Eilam while (sta) { 1752a33bee2SGuy Eilam if ((sta->sdata == sdata || 1762a33bee2SGuy Eilam (sta->sdata->bss && sta->sdata->bss == sdata->bss)) && 1770e5ded5aSFelix Fietkau memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 1780e5ded5aSFelix Fietkau break; 1790379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1800379185bSJohannes Berg lockdep_is_held(&local->sta_lock) || 1810379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1820e5ded5aSFelix Fietkau } 1830e5ded5aSFelix Fietkau return sta; 1840e5ded5aSFelix Fietkau } 1850e5ded5aSFelix Fietkau 1863b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, 1873b53fde8SJohannes Berg int idx) 188ee385855SLuis Carlos Cobo { 1893b53fde8SJohannes Berg struct ieee80211_local *local = sdata->local; 190ee385855SLuis Carlos Cobo struct sta_info *sta; 191ee385855SLuis Carlos Cobo int i = 0; 192ee385855SLuis Carlos Cobo 193d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) { 1943b53fde8SJohannes Berg if (sdata != sta->sdata) 1952a8ca29aSLuis Carlos Cobo continue; 196ee385855SLuis Carlos Cobo if (i < idx) { 197ee385855SLuis Carlos Cobo ++i; 198ee385855SLuis Carlos Cobo continue; 199ee385855SLuis Carlos Cobo } 2002a8ca29aSLuis Carlos Cobo return sta; 201ee385855SLuis Carlos Cobo } 202ee385855SLuis Carlos Cobo 203ee385855SLuis Carlos Cobo return NULL; 204ee385855SLuis Carlos Cobo } 205f0706e82SJiri Benc 20693e5deb1SJohannes Berg /** 20793e5deb1SJohannes Berg * __sta_info_free - internal STA free helper 20893e5deb1SJohannes Berg * 2096ef307bcSRandy Dunlap * @local: pointer to the global information 21093e5deb1SJohannes Berg * @sta: STA info to free 21193e5deb1SJohannes Berg * 21293e5deb1SJohannes Berg * This function must undo everything done by sta_info_alloc() 21393e5deb1SJohannes Berg * that may happen before sta_info_insert(). 21493e5deb1SJohannes Berg */ 21593e5deb1SJohannes Berg static void __sta_info_free(struct ieee80211_local *local, 21693e5deb1SJohannes Berg struct sta_info *sta) 21793e5deb1SJohannes Berg { 218af65cd96SJohannes Berg if (sta->rate_ctrl) { 2194b7679a5SJohannes Berg rate_control_free_sta(sta); 22093e5deb1SJohannes Berg rate_control_put(sta->rate_ctrl); 221af65cd96SJohannes Berg } 22293e5deb1SJohannes Berg 22393e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 2240fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr); 22593e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 22693e5deb1SJohannes Berg 22793e5deb1SJohannes Berg kfree(sta); 22893e5deb1SJohannes Berg } 22993e5deb1SJohannes Berg 230d0709a65SJohannes Berg /* Caller must hold local->sta_lock */ 231d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local, 232d0709a65SJohannes Berg struct sta_info *sta) 233f0706e82SJiri Benc { 23417741cdcSJohannes Berg sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)]; 23517741cdcSJohannes Berg rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta); 236f0706e82SJiri Benc } 237f0706e82SJiri Benc 238af818581SJohannes Berg static void sta_unblock(struct work_struct *wk) 239af818581SJohannes Berg { 240af818581SJohannes Berg struct sta_info *sta; 241af818581SJohannes Berg 242af818581SJohannes Berg sta = container_of(wk, struct sta_info, drv_unblock_wk); 243af818581SJohannes Berg 244af818581SJohannes Berg if (sta->dead) 245af818581SJohannes Berg return; 246af818581SJohannes Berg 247af818581SJohannes Berg if (!test_sta_flags(sta, WLAN_STA_PS_STA)) 248af818581SJohannes Berg ieee80211_sta_ps_deliver_wakeup(sta); 24950a9432dSJohannes Berg else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL)) { 25050a9432dSJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER); 251ce662b44SJohannes Berg 252ce662b44SJohannes Berg local_bh_disable(); 253af818581SJohannes Berg ieee80211_sta_ps_deliver_poll_response(sta); 254ce662b44SJohannes Berg local_bh_enable(); 25547086fc5SJohannes Berg } else if (test_and_clear_sta_flags(sta, WLAN_STA_UAPSD)) { 25647086fc5SJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER); 257ce662b44SJohannes Berg 258ce662b44SJohannes Berg local_bh_disable(); 25947086fc5SJohannes Berg ieee80211_sta_ps_deliver_uapsd(sta); 260ce662b44SJohannes Berg local_bh_enable(); 26150a9432dSJohannes Berg } else 26250a9432dSJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER); 263af818581SJohannes Berg } 264af818581SJohannes Berg 265af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local, 266af65cd96SJohannes Berg struct sta_info *sta, gfp_t gfp) 267af65cd96SJohannes Berg { 268af65cd96SJohannes Berg if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) 269af65cd96SJohannes Berg return 0; 270af65cd96SJohannes Berg 271af65cd96SJohannes Berg sta->rate_ctrl = rate_control_get(local->rate_ctrl); 272af65cd96SJohannes Berg sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, 273af65cd96SJohannes Berg &sta->sta, gfp); 274af65cd96SJohannes Berg if (!sta->rate_ctrl_priv) { 275af65cd96SJohannes Berg rate_control_put(sta->rate_ctrl); 276af65cd96SJohannes Berg return -ENOMEM; 277af65cd96SJohannes Berg } 278af65cd96SJohannes Berg 279af65cd96SJohannes Berg return 0; 280af65cd96SJohannes Berg } 281af65cd96SJohannes Berg 28273651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, 28373651ee6SJohannes Berg u8 *addr, gfp_t gfp) 284f0706e82SJiri Benc { 285d0709a65SJohannes Berg struct ieee80211_local *local = sdata->local; 286f0706e82SJiri Benc struct sta_info *sta; 287ebe27c91SMohammed Shafi Shajakhan struct timespec uptime; 28816c5f15cSRon Rindjunsky int i; 289f0706e82SJiri Benc 29017741cdcSJohannes Berg sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp); 291f0706e82SJiri Benc if (!sta) 29273651ee6SJohannes Berg return NULL; 293f0706e82SJiri Benc 29407346f81SJohannes Berg spin_lock_init(&sta->lock); 2955a9f7b04SJohannes Berg spin_lock_init(&sta->flaglock); 296af818581SJohannes Berg INIT_WORK(&sta->drv_unblock_wk, sta_unblock); 29767c282c0SJohannes Berg INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work); 298a93e3644SJohannes Berg mutex_init(&sta->ampdu_mlme.mtx); 29907346f81SJohannes Berg 30017741cdcSJohannes Berg memcpy(sta->sta.addr, addr, ETH_ALEN); 301d0709a65SJohannes Berg sta->local = local; 302d0709a65SJohannes Berg sta->sdata = sdata; 3038bc8aecdSFelix Fietkau sta->last_rx = jiffies; 304f0706e82SJiri Benc 305ebe27c91SMohammed Shafi Shajakhan do_posix_clock_monotonic_gettime(&uptime); 306ebe27c91SMohammed Shafi Shajakhan sta->last_connected = uptime.tv_sec; 307541a45a1SBruno Randolf ewma_init(&sta->avg_signal, 1024, 8); 308541a45a1SBruno Randolf 309af65cd96SJohannes Berg if (sta_prepare_rate_control(local, sta, gfp)) { 310f0706e82SJiri Benc kfree(sta); 31173651ee6SJohannes Berg return NULL; 312f0706e82SJiri Benc } 313f0706e82SJiri Benc 31416c5f15cSRon Rindjunsky for (i = 0; i < STA_TID_NUM; i++) { 315a622ab72SJohannes Berg /* 316a622ab72SJohannes Berg * timer_to_tid must be initialized with identity mapping 317a622ab72SJohannes Berg * to enable session_timer's data differentiation. See 318a622ab72SJohannes Berg * sta_rx_agg_session_timer_expired for usage. 319a622ab72SJohannes Berg */ 32016c5f15cSRon Rindjunsky sta->timer_to_tid[i] = i; 32116c5f15cSRon Rindjunsky } 322948d887dSJohannes Berg for (i = 0; i < IEEE80211_NUM_ACS; i++) { 323948d887dSJohannes Berg skb_queue_head_init(&sta->ps_tx_buf[i]); 324948d887dSJohannes Berg skb_queue_head_init(&sta->tx_filtered[i]); 325948d887dSJohannes Berg } 32673651ee6SJohannes Berg 327cccaec98SSenthil Balasubramanian for (i = 0; i < NUM_RX_DATA_QUEUES; i++) 3284be929beSAlexey Dobriyan sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); 329cccaec98SSenthil Balasubramanian 33073651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 3310fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr); 33273651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 33373651ee6SJohannes Berg 33403e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH 33557cf8043SJavier Cardona sta->plink_state = NL80211_PLINK_LISTEN; 33603e4497eSJohannes Berg init_timer(&sta->plink_timer); 33703e4497eSJohannes Berg #endif 33803e4497eSJohannes Berg 33973651ee6SJohannes Berg return sta; 34073651ee6SJohannes Berg } 34173651ee6SJohannes Berg 3422a33bee2SGuy Eilam static int sta_info_finish_insert(struct sta_info *sta, 3432a33bee2SGuy Eilam bool async, bool dummy_reinsert) 34473651ee6SJohannes Berg { 34573651ee6SJohannes Berg struct ieee80211_local *local = sta->local; 34673651ee6SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 34798b62183SJohannes Berg struct station_info sinfo; 34873651ee6SJohannes Berg unsigned long flags; 34993e5deb1SJohannes Berg int err = 0; 35073651ee6SJohannes Berg 35146a5ebafSJohannes Berg lockdep_assert_held(&local->sta_mtx); 35234e89507SJohannes Berg 3532a33bee2SGuy Eilam if (!sta->dummy || dummy_reinsert) { 35434e89507SJohannes Berg /* notify driver */ 35534e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 35634e89507SJohannes Berg sdata = container_of(sdata->bss, 35734e89507SJohannes Berg struct ieee80211_sub_if_data, 35834e89507SJohannes Berg u.ap); 35934e89507SJohannes Berg err = drv_sta_add(local, sdata, &sta->sta); 36034e89507SJohannes Berg if (err) { 36134e89507SJohannes Berg if (!async) 36234e89507SJohannes Berg return err; 3632a33bee2SGuy Eilam printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to " 3642a33bee2SGuy Eilam "driver (%d) - keeping it anyway.\n", 36534e89507SJohannes Berg sdata->name, sta->sta.addr, err); 36634e89507SJohannes Berg } else { 36734e89507SJohannes Berg sta->uploaded = true; 36834e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 36934e89507SJohannes Berg if (async) 3700fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, 3710fb9a9ecSJoe Perches "Finished adding IBSS STA %pM\n", 3720fb9a9ecSJoe Perches sta->sta.addr); 37334e89507SJohannes Berg #endif 37434e89507SJohannes Berg } 37534e89507SJohannes Berg 37634e89507SJohannes Berg sdata = sta->sdata; 3772a33bee2SGuy Eilam } 37834e89507SJohannes Berg 3792a33bee2SGuy Eilam if (!dummy_reinsert) { 38034e89507SJohannes Berg if (!async) { 38134e89507SJohannes Berg local->num_sta++; 38234e89507SJohannes Berg local->sta_generation++; 38334e89507SJohannes Berg smp_mb(); 38434e89507SJohannes Berg 38534e89507SJohannes Berg /* make the station visible */ 38634e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 38734e89507SJohannes Berg sta_info_hash_add(local, sta); 38834e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 38934e89507SJohannes Berg } 39034e89507SJohannes Berg 39134e89507SJohannes Berg list_add(&sta->list, &local->sta_list); 3922a33bee2SGuy Eilam } else { 3932a33bee2SGuy Eilam sta->dummy = false; 3942a33bee2SGuy Eilam } 39534e89507SJohannes Berg 3962a33bee2SGuy Eilam if (!sta->dummy) { 39734e89507SJohannes Berg ieee80211_sta_debugfs_add(sta); 39834e89507SJohannes Berg rate_control_add_sta_debugfs(sta); 39934e89507SJohannes Berg 400f612cedfSJouni Malinen memset(&sinfo, 0, sizeof(sinfo)); 40134e89507SJohannes Berg sinfo.filled = 0; 40234e89507SJohannes Berg sinfo.generation = local->sta_generation; 40334e89507SJohannes Berg cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL); 4042a33bee2SGuy Eilam } 40534e89507SJohannes Berg 40634e89507SJohannes Berg return 0; 40734e89507SJohannes Berg } 40834e89507SJohannes Berg 40934e89507SJohannes Berg static void sta_info_finish_pending(struct ieee80211_local *local) 41034e89507SJohannes Berg { 41134e89507SJohannes Berg struct sta_info *sta; 41234e89507SJohannes Berg unsigned long flags; 41334e89507SJohannes Berg 41434e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 41534e89507SJohannes Berg while (!list_empty(&local->sta_pending_list)) { 41634e89507SJohannes Berg sta = list_first_entry(&local->sta_pending_list, 41734e89507SJohannes Berg struct sta_info, list); 41834e89507SJohannes Berg list_del(&sta->list); 41934e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 42034e89507SJohannes Berg 4212a33bee2SGuy Eilam sta_info_finish_insert(sta, true, false); 42234e89507SJohannes Berg 42334e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 42434e89507SJohannes Berg } 42534e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 42634e89507SJohannes Berg } 42734e89507SJohannes Berg 42834e89507SJohannes Berg static void sta_info_finish_work(struct work_struct *work) 42934e89507SJohannes Berg { 43034e89507SJohannes Berg struct ieee80211_local *local = 43134e89507SJohannes Berg container_of(work, struct ieee80211_local, sta_finish_work); 43234e89507SJohannes Berg 43334e89507SJohannes Berg mutex_lock(&local->sta_mtx); 43434e89507SJohannes Berg sta_info_finish_pending(local); 43534e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 43634e89507SJohannes Berg } 43734e89507SJohannes Berg 4388c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta) 43934e89507SJohannes Berg { 44034e89507SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 44134e89507SJohannes Berg 44203e4497eSJohannes Berg /* 44303e4497eSJohannes Berg * Can't be a WARN_ON because it can be triggered through a race: 44403e4497eSJohannes Berg * something inserts a STA (on one CPU) without holding the RTNL 44503e4497eSJohannes Berg * and another CPU turns off the net device. 44603e4497eSJohannes Berg */ 4478c71df7aSGuy Eilam if (unlikely(!ieee80211_sdata_running(sdata))) 4488c71df7aSGuy Eilam return -ENETDOWN; 44903e4497eSJohannes Berg 45047846c9bSJohannes Berg if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 || 4518c71df7aSGuy Eilam is_multicast_ether_addr(sta->sta.addr))) 4528c71df7aSGuy Eilam return -EINVAL; 4538c71df7aSGuy Eilam 4548c71df7aSGuy Eilam return 0; 45593e5deb1SJohannes Berg } 45644213b5eSJohannes Berg 4578c71df7aSGuy Eilam static int sta_info_insert_ibss(struct sta_info *sta) __acquires(RCU) 4588c71df7aSGuy Eilam { 4598c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 4608c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 4618c71df7aSGuy Eilam unsigned long flags; 4628c71df7aSGuy Eilam 463d0709a65SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 46443ba7e95SJohannes Berg /* check if STA exists already */ 4652a33bee2SGuy Eilam if (sta_info_get_bss_rx(sdata, sta->sta.addr)) { 466d0709a65SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 46734e89507SJohannes Berg rcu_read_lock(); 4688c71df7aSGuy Eilam return -EEXIST; 46943ba7e95SJohannes Berg } 47034e89507SJohannes Berg 471f0706e82SJiri Benc local->num_sta++; 47234e89507SJohannes Berg local->sta_generation++; 47334e89507SJohannes Berg smp_mb(); 474f0706e82SJiri Benc sta_info_hash_add(local, sta); 47532bfd35dSJohannes Berg 47634e89507SJohannes Berg list_add_tail(&sta->list, &local->sta_pending_list); 47732bfd35dSJohannes Berg 47834e89507SJohannes Berg rcu_read_lock(); 47934e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 48034e89507SJohannes Berg 48134e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 4820fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n", 4830fb9a9ecSJoe Perches sta->sta.addr); 48434e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 48534e89507SJohannes Berg 48634e89507SJohannes Berg ieee80211_queue_work(&local->hw, &local->sta_finish_work); 48734e89507SJohannes Berg 48834e89507SJohannes Berg return 0; 48934e89507SJohannes Berg } 49034e89507SJohannes Berg 49134e89507SJohannes Berg /* 4928c71df7aSGuy Eilam * should be called with sta_mtx locked 4938c71df7aSGuy Eilam * this function replaces the mutex lock 4948c71df7aSGuy Eilam * with a RCU lock 4958c71df7aSGuy Eilam */ 4968c71df7aSGuy Eilam static int sta_info_insert_non_ibss(struct sta_info *sta) __acquires(RCU) 4978c71df7aSGuy Eilam { 4988c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 4998c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 5008c71df7aSGuy Eilam unsigned long flags; 5012a33bee2SGuy Eilam struct sta_info *exist_sta; 5022a33bee2SGuy Eilam bool dummy_reinsert = false; 5038c71df7aSGuy Eilam int err = 0; 5048c71df7aSGuy Eilam 5058c71df7aSGuy Eilam lockdep_assert_held(&local->sta_mtx); 5068c71df7aSGuy Eilam 5078c71df7aSGuy Eilam /* 50834e89507SJohannes Berg * On first glance, this will look racy, because the code 5098c71df7aSGuy Eilam * in this function, which inserts a station with sleeping, 51034e89507SJohannes Berg * unlocks the sta_lock between checking existence in the 51134e89507SJohannes Berg * hash table and inserting into it. 51234e89507SJohannes Berg * 51334e89507SJohannes Berg * However, it is not racy against itself because it keeps 5148c71df7aSGuy Eilam * the mutex locked. 51534e89507SJohannes Berg */ 51634e89507SJohannes Berg 51734e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 5182a33bee2SGuy Eilam /* 5192a33bee2SGuy Eilam * check if STA exists already. 5202a33bee2SGuy Eilam * only accept a scenario of a second call to sta_info_insert_non_ibss 5212a33bee2SGuy Eilam * with a dummy station entry that was inserted earlier 5222a33bee2SGuy Eilam * in that case - assume that the dummy station flag should 5232a33bee2SGuy Eilam * be removed. 5242a33bee2SGuy Eilam */ 5252a33bee2SGuy Eilam exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr); 5262a33bee2SGuy Eilam if (exist_sta) { 5272a33bee2SGuy Eilam if (exist_sta == sta && sta->dummy) { 5282a33bee2SGuy Eilam dummy_reinsert = true; 5292a33bee2SGuy Eilam } else { 53034e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 53138a679a5SJouni Malinen mutex_unlock(&local->sta_mtx); 53234e89507SJohannes Berg rcu_read_lock(); 5338c71df7aSGuy Eilam return -EEXIST; 53434e89507SJohannes Berg } 5352a33bee2SGuy Eilam } 53634e89507SJohannes Berg 53734e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 53834e89507SJohannes Berg 5392a33bee2SGuy Eilam err = sta_info_finish_insert(sta, false, dummy_reinsert); 54034e89507SJohannes Berg if (err) { 54134e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 54234e89507SJohannes Berg rcu_read_lock(); 5438c71df7aSGuy Eilam return err; 54432bfd35dSJohannes Berg } 545d0709a65SJohannes Berg 546f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 5472a33bee2SGuy Eilam wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n", 5482a33bee2SGuy Eilam sta->dummy ? "dummy " : "", sta->sta.addr); 549f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 550f0706e82SJiri Benc 55134e89507SJohannes Berg /* move reference to rcu-protected */ 55234e89507SJohannes Berg rcu_read_lock(); 55334e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 554e9f207f0SJiri Benc 55573651ee6SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 55673651ee6SJohannes Berg mesh_accept_plinks_update(sdata); 55773651ee6SJohannes Berg 55873651ee6SJohannes Berg return 0; 5598c71df7aSGuy Eilam } 5608c71df7aSGuy Eilam 5618c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) 5628c71df7aSGuy Eilam { 5638c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 5648c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 5658c71df7aSGuy Eilam int err = 0; 5668c71df7aSGuy Eilam 5678c71df7aSGuy Eilam err = sta_info_insert_check(sta); 5688c71df7aSGuy Eilam if (err) { 5698c71df7aSGuy Eilam rcu_read_lock(); 5708c71df7aSGuy Eilam goto out_free; 5718c71df7aSGuy Eilam } 5728c71df7aSGuy Eilam 5738c71df7aSGuy Eilam /* 5748c71df7aSGuy Eilam * In ad-hoc mode, we sometimes need to insert stations 5758c71df7aSGuy Eilam * from tasklet context from the RX path. To avoid races, 5768c71df7aSGuy Eilam * always do so in that case -- see the comment below. 5778c71df7aSGuy Eilam */ 5788c71df7aSGuy Eilam if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { 5798c71df7aSGuy Eilam err = sta_info_insert_ibss(sta); 5808c71df7aSGuy Eilam if (err) 5818c71df7aSGuy Eilam goto out_free; 5828c71df7aSGuy Eilam 5838c71df7aSGuy Eilam return 0; 5848c71df7aSGuy Eilam } 5858c71df7aSGuy Eilam 5868c71df7aSGuy Eilam /* 5878c71df7aSGuy Eilam * It might seem that the function called below is in race against 5888c71df7aSGuy Eilam * the function call above that atomically inserts the station... That, 5898c71df7aSGuy Eilam * however, is not true because the above code can only 5908c71df7aSGuy Eilam * be invoked for IBSS interfaces, and the below code will 5918c71df7aSGuy Eilam * not be -- and the two do not race against each other as 5928c71df7aSGuy Eilam * the hash table also keys off the interface. 5938c71df7aSGuy Eilam */ 5948c71df7aSGuy Eilam 5958c71df7aSGuy Eilam might_sleep(); 5968c71df7aSGuy Eilam 5978c71df7aSGuy Eilam mutex_lock(&local->sta_mtx); 5988c71df7aSGuy Eilam 5998c71df7aSGuy Eilam err = sta_info_insert_non_ibss(sta); 6008c71df7aSGuy Eilam if (err) 6018c71df7aSGuy Eilam goto out_free; 6028c71df7aSGuy Eilam 6038c71df7aSGuy Eilam return 0; 60493e5deb1SJohannes Berg out_free: 60593e5deb1SJohannes Berg BUG_ON(!err); 60693e5deb1SJohannes Berg __sta_info_free(local, sta); 60793e5deb1SJohannes Berg return err; 608f0706e82SJiri Benc } 609f0706e82SJiri Benc 61034e89507SJohannes Berg int sta_info_insert(struct sta_info *sta) 61134e89507SJohannes Berg { 61234e89507SJohannes Berg int err = sta_info_insert_rcu(sta); 61334e89507SJohannes Berg 61434e89507SJohannes Berg rcu_read_unlock(); 61534e89507SJohannes Berg 61634e89507SJohannes Berg return err; 61734e89507SJohannes Berg } 61834e89507SJohannes Berg 6192a33bee2SGuy Eilam /* Caller must hold sta->local->sta_mtx */ 6202a33bee2SGuy Eilam int sta_info_reinsert(struct sta_info *sta) 6212a33bee2SGuy Eilam { 6222a33bee2SGuy Eilam struct ieee80211_local *local = sta->local; 6232a33bee2SGuy Eilam int err = 0; 6242a33bee2SGuy Eilam 6252a33bee2SGuy Eilam err = sta_info_insert_check(sta); 6262a33bee2SGuy Eilam if (err) { 6272a33bee2SGuy Eilam mutex_unlock(&local->sta_mtx); 6282a33bee2SGuy Eilam return err; 6292a33bee2SGuy Eilam } 6302a33bee2SGuy Eilam 6312a33bee2SGuy Eilam might_sleep(); 6322a33bee2SGuy Eilam 6332a33bee2SGuy Eilam err = sta_info_insert_non_ibss(sta); 6342a33bee2SGuy Eilam rcu_read_unlock(); 6352a33bee2SGuy Eilam return err; 6362a33bee2SGuy Eilam } 6372a33bee2SGuy Eilam 638004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid) 639004c872eSJohannes Berg { 640004c872eSJohannes Berg /* 641004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 642004c872eSJohannes Berg * so this line may not be changed to use the __set_bit() format. 643004c872eSJohannes Berg */ 644004c872eSJohannes Berg bss->tim[aid / 8] |= (1 << (aid % 8)); 645004c872eSJohannes Berg } 646004c872eSJohannes Berg 647004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid) 648004c872eSJohannes Berg { 649004c872eSJohannes Berg /* 650004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 651004c872eSJohannes Berg * so this line may not be changed to use the __clear_bit() format. 652004c872eSJohannes Berg */ 653004c872eSJohannes Berg bss->tim[aid / 8] &= ~(1 << (aid % 8)); 654004c872eSJohannes Berg } 655004c872eSJohannes Berg 656948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac) 657948d887dSJohannes Berg { 658948d887dSJohannes Berg /* If we ever support TIDs > 7, this obviously needs to be adjusted */ 659948d887dSJohannes Berg switch (ac) { 660948d887dSJohannes Berg case IEEE80211_AC_VO: 661948d887dSJohannes Berg return BIT(6) | BIT(7); 662948d887dSJohannes Berg case IEEE80211_AC_VI: 663948d887dSJohannes Berg return BIT(4) | BIT(5); 664948d887dSJohannes Berg case IEEE80211_AC_BE: 665948d887dSJohannes Berg return BIT(0) | BIT(3); 666948d887dSJohannes Berg case IEEE80211_AC_BK: 667948d887dSJohannes Berg return BIT(1) | BIT(2); 668948d887dSJohannes Berg default: 669948d887dSJohannes Berg WARN_ON(1); 670948d887dSJohannes Berg return 0; 671948d887dSJohannes Berg } 672948d887dSJohannes Berg } 673948d887dSJohannes Berg 674c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta) 675004c872eSJohannes Berg { 676c868cb35SJohannes Berg struct ieee80211_local *local = sta->local; 677c868cb35SJohannes Berg struct ieee80211_if_ap *bss = sta->sdata->bss; 678d0709a65SJohannes Berg unsigned long flags; 679948d887dSJohannes Berg bool indicate_tim = false; 680948d887dSJohannes Berg u8 ignore_for_tim = sta->sta.uapsd_queues; 681948d887dSJohannes Berg int ac; 682004c872eSJohannes Berg 683c868cb35SJohannes Berg if (WARN_ON_ONCE(!sta->sdata->bss)) 684c868cb35SJohannes Berg return; 6853e122be0SJohannes Berg 686c868cb35SJohannes Berg /* No need to do anything if the driver does all */ 687c868cb35SJohannes Berg if (local->hw.flags & IEEE80211_HW_AP_LINK_PS) 688c868cb35SJohannes Berg return; 689004c872eSJohannes Berg 690c868cb35SJohannes Berg if (sta->dead) 691c868cb35SJohannes Berg goto done; 6923e122be0SJohannes Berg 693948d887dSJohannes Berg /* 694948d887dSJohannes Berg * If all ACs are delivery-enabled then we should build 695948d887dSJohannes Berg * the TIM bit for all ACs anyway; if only some are then 696948d887dSJohannes Berg * we ignore those and build the TIM bit using only the 697948d887dSJohannes Berg * non-enabled ones. 698948d887dSJohannes Berg */ 699948d887dSJohannes Berg if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1) 700948d887dSJohannes Berg ignore_for_tim = 0; 701948d887dSJohannes Berg 702948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 703948d887dSJohannes Berg unsigned long tids; 704948d887dSJohannes Berg 705948d887dSJohannes Berg if (ignore_for_tim & BIT(ac)) 706948d887dSJohannes Berg continue; 707948d887dSJohannes Berg 708948d887dSJohannes Berg indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) || 709948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac]); 710948d887dSJohannes Berg if (indicate_tim) 711948d887dSJohannes Berg break; 712948d887dSJohannes Berg 713948d887dSJohannes Berg tids = ieee80211_tids_for_ac(ac); 714948d887dSJohannes Berg 715948d887dSJohannes Berg indicate_tim |= 716948d887dSJohannes Berg sta->driver_buffered_tids & tids; 717948d887dSJohannes Berg } 718c868cb35SJohannes Berg 719c868cb35SJohannes Berg done: 720c868cb35SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 721c868cb35SJohannes Berg 722948d887dSJohannes Berg if (indicate_tim) 723c868cb35SJohannes Berg __bss_tim_set(bss, sta->sta.aid); 724c868cb35SJohannes Berg else 72517741cdcSJohannes Berg __bss_tim_clear(bss, sta->sta.aid); 7263e122be0SJohannes Berg 727c868cb35SJohannes Berg if (local->ops->set_tim) { 728c868cb35SJohannes Berg local->tim_in_locked_section = true; 729948d887dSJohannes Berg drv_set_tim(local, &sta->sta, indicate_tim); 730c868cb35SJohannes Berg local->tim_in_locked_section = false; 731004c872eSJohannes Berg } 732004c872eSJohannes Berg 733c868cb35SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 734004c872eSJohannes Berg } 735004c872eSJohannes Berg 736cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb) 737f0706e82SJiri Benc { 738e039fa4aSJohannes Berg struct ieee80211_tx_info *info; 739f0706e82SJiri Benc int timeout; 740f0706e82SJiri Benc 741f0706e82SJiri Benc if (!skb) 742cd0b8d89SJohannes Berg return false; 743f0706e82SJiri Benc 744e039fa4aSJohannes Berg info = IEEE80211_SKB_CB(skb); 745f0706e82SJiri Benc 746f0706e82SJiri Benc /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 74757c4d7b4SJohannes Berg timeout = (sta->listen_interval * 74857c4d7b4SJohannes Berg sta->sdata->vif.bss_conf.beacon_int * 74957c4d7b4SJohannes Berg 32 / 15625) * HZ; 750f0706e82SJiri Benc if (timeout < STA_TX_BUFFER_EXPIRE) 751f0706e82SJiri Benc timeout = STA_TX_BUFFER_EXPIRE; 752e039fa4aSJohannes Berg return time_after(jiffies, info->control.jiffies + timeout); 753f0706e82SJiri Benc } 754f0706e82SJiri Benc 755f0706e82SJiri Benc 756948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local, 757948d887dSJohannes Berg struct sta_info *sta, int ac) 758f0706e82SJiri Benc { 759f0706e82SJiri Benc unsigned long flags; 760f0706e82SJiri Benc struct sk_buff *skb; 761f0706e82SJiri Benc 76260750397SJohannes Berg /* 76360750397SJohannes Berg * First check for frames that should expire on the filtered 76460750397SJohannes Berg * queue. Frames here were rejected by the driver and are on 76560750397SJohannes Berg * a separate queue to avoid reordering with normal PS-buffered 76660750397SJohannes Berg * frames. They also aren't accounted for right now in the 76760750397SJohannes Berg * total_ps_buffered counter. 76860750397SJohannes Berg */ 76960750397SJohannes Berg for (;;) { 770948d887dSJohannes Berg spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 771948d887dSJohannes Berg skb = skb_peek(&sta->tx_filtered[ac]); 77260750397SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 773948d887dSJohannes Berg skb = __skb_dequeue(&sta->tx_filtered[ac]); 77460750397SJohannes Berg else 77560750397SJohannes Berg skb = NULL; 776948d887dSJohannes Berg spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 77760750397SJohannes Berg 77860750397SJohannes Berg /* 77960750397SJohannes Berg * Frames are queued in order, so if this one 78060750397SJohannes Berg * hasn't expired yet we can stop testing. If 78160750397SJohannes Berg * we actually reached the end of the queue we 78260750397SJohannes Berg * also need to stop, of course. 78360750397SJohannes Berg */ 78460750397SJohannes Berg if (!skb) 78560750397SJohannes Berg break; 78660750397SJohannes Berg dev_kfree_skb(skb); 78760750397SJohannes Berg } 78860750397SJohannes Berg 78960750397SJohannes Berg /* 79060750397SJohannes Berg * Now also check the normal PS-buffered queue, this will 79160750397SJohannes Berg * only find something if the filtered queue was emptied 79260750397SJohannes Berg * since the filtered frames are all before the normal PS 79360750397SJohannes Berg * buffered frames. 79460750397SJohannes Berg */ 795f0706e82SJiri Benc for (;;) { 796948d887dSJohannes Berg spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 797948d887dSJohannes Berg skb = skb_peek(&sta->ps_tx_buf[ac]); 79857c4d7b4SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 799948d887dSJohannes Berg skb = __skb_dequeue(&sta->ps_tx_buf[ac]); 800836341a7SJohannes Berg else 801f0706e82SJiri Benc skb = NULL; 802948d887dSJohannes Berg spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 803f0706e82SJiri Benc 80460750397SJohannes Berg /* 80560750397SJohannes Berg * frames are queued in order, so if this one 80660750397SJohannes Berg * hasn't expired yet (or we reached the end of 80760750397SJohannes Berg * the queue) we can stop testing 80860750397SJohannes Berg */ 809836341a7SJohannes Berg if (!skb) 810836341a7SJohannes Berg break; 811836341a7SJohannes Berg 812f0706e82SJiri Benc local->total_ps_buffered--; 813f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 8140c68ae26SJohannes Berg printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n", 8150c68ae26SJohannes Berg sta->sta.addr); 816f4ea83ddSJohannes Berg #endif 817f0706e82SJiri Benc dev_kfree_skb(skb); 818f0706e82SJiri Benc } 8193393a608SJuuso Oikarinen 82060750397SJohannes Berg /* 82160750397SJohannes Berg * Finally, recalculate the TIM bit for this station -- it might 82260750397SJohannes Berg * now be clear because the station was too slow to retrieve its 82360750397SJohannes Berg * frames. 82460750397SJohannes Berg */ 82560750397SJohannes Berg sta_info_recalc_tim(sta); 82660750397SJohannes Berg 82760750397SJohannes Berg /* 82860750397SJohannes Berg * Return whether there are any frames still buffered, this is 82960750397SJohannes Berg * used to check whether the cleanup timer still needs to run, 83060750397SJohannes Berg * if there are no frames we don't need to rearm the timer. 83160750397SJohannes Berg */ 832948d887dSJohannes Berg return !(skb_queue_empty(&sta->ps_tx_buf[ac]) && 833948d887dSJohannes Berg skb_queue_empty(&sta->tx_filtered[ac])); 834948d887dSJohannes Berg } 835948d887dSJohannes Berg 836948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 837948d887dSJohannes Berg struct sta_info *sta) 838948d887dSJohannes Berg { 839948d887dSJohannes Berg bool have_buffered = false; 840948d887dSJohannes Berg int ac; 841948d887dSJohannes Berg 842948d887dSJohannes Berg /* This is only necessary for stations on BSS interfaces */ 843948d887dSJohannes Berg if (!sta->sdata->bss) 844948d887dSJohannes Berg return false; 845948d887dSJohannes Berg 846948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 847948d887dSJohannes Berg have_buffered |= 848948d887dSJohannes Berg sta_info_cleanup_expire_buffered_ac(local, sta, ac); 849948d887dSJohannes Berg 850948d887dSJohannes Berg return have_buffered; 851f0706e82SJiri Benc } 852f0706e82SJiri Benc 85334e89507SJohannes Berg static int __must_check __sta_info_destroy(struct sta_info *sta) 85434e89507SJohannes Berg { 85534e89507SJohannes Berg struct ieee80211_local *local; 85634e89507SJohannes Berg struct ieee80211_sub_if_data *sdata; 85734e89507SJohannes Berg unsigned long flags; 858948d887dSJohannes Berg int ret, i, ac; 85934e89507SJohannes Berg 86034e89507SJohannes Berg might_sleep(); 86134e89507SJohannes Berg 86234e89507SJohannes Berg if (!sta) 86334e89507SJohannes Berg return -ENOENT; 86434e89507SJohannes Berg 86534e89507SJohannes Berg local = sta->local; 86634e89507SJohannes Berg sdata = sta->sdata; 86734e89507SJohannes Berg 868098a6070SJohannes Berg /* 869098a6070SJohannes Berg * Before removing the station from the driver and 870098a6070SJohannes Berg * rate control, it might still start new aggregation 871098a6070SJohannes Berg * sessions -- block that to make sure the tear-down 872098a6070SJohannes Berg * will be sufficient. 873098a6070SJohannes Berg */ 874098a6070SJohannes Berg set_sta_flags(sta, WLAN_STA_BLOCK_BA); 87553f73c09SJohannes Berg ieee80211_sta_tear_down_BA_sessions(sta, true); 876098a6070SJohannes Berg 87734e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 87834e89507SJohannes Berg ret = sta_info_hash_del(local, sta); 87934e89507SJohannes Berg /* this might still be the pending list ... which is fine */ 88034e89507SJohannes Berg if (!ret) 88134e89507SJohannes Berg list_del(&sta->list); 88234e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 88334e89507SJohannes Berg if (ret) 88434e89507SJohannes Berg return ret; 88534e89507SJohannes Berg 8868cb23153SJohannes Berg mutex_lock(&local->key_mtx); 887e31b8213SJohannes Berg for (i = 0; i < NUM_DEFAULT_KEYS; i++) 88840b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i])); 889e31b8213SJohannes Berg if (sta->ptk) 89040b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->ptk)); 8918cb23153SJohannes Berg mutex_unlock(&local->key_mtx); 89234e89507SJohannes Berg 89334e89507SJohannes Berg sta->dead = true; 89434e89507SJohannes Berg 89534e89507SJohannes Berg if (test_and_clear_sta_flags(sta, 89634e89507SJohannes Berg WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) { 89734e89507SJohannes Berg BUG_ON(!sdata->bss); 89834e89507SJohannes Berg 89934e89507SJohannes Berg atomic_dec(&sdata->bss->num_sta_ps); 900c868cb35SJohannes Berg sta_info_recalc_tim(sta); 90134e89507SJohannes Berg } 90234e89507SJohannes Berg 90334e89507SJohannes Berg local->num_sta--; 90434e89507SJohannes Berg local->sta_generation++; 90534e89507SJohannes Berg 90634e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 90734e89507SJohannes Berg rcu_assign_pointer(sdata->u.vlan.sta, NULL); 90834e89507SJohannes Berg 90934e89507SJohannes Berg if (sta->uploaded) { 91034e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 91134e89507SJohannes Berg sdata = container_of(sdata->bss, 91234e89507SJohannes Berg struct ieee80211_sub_if_data, 91334e89507SJohannes Berg u.ap); 91434e89507SJohannes Berg drv_sta_remove(local, sdata, &sta->sta); 91534e89507SJohannes Berg sdata = sta->sdata; 91634e89507SJohannes Berg } 91734e89507SJohannes Berg 918e64b3795SJohannes Berg /* 919e64b3795SJohannes Berg * At this point, after we wait for an RCU grace period, 920e64b3795SJohannes Berg * neither mac80211 nor the driver can reference this 921e64b3795SJohannes Berg * sta struct any more except by still existing timers 922e64b3795SJohannes Berg * associated with this station that we clean up below. 923e64b3795SJohannes Berg */ 924e64b3795SJohannes Berg synchronize_rcu(); 925e64b3795SJohannes Berg 926948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 927948d887dSJohannes Berg local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]); 928948d887dSJohannes Berg __skb_queue_purge(&sta->ps_tx_buf[ac]); 929948d887dSJohannes Berg __skb_queue_purge(&sta->tx_filtered[ac]); 930948d887dSJohannes Berg } 931c868cb35SJohannes Berg 93234e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH 933e64b3795SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 93434e89507SJohannes Berg mesh_accept_plinks_update(sdata); 93534e89507SJohannes Berg #endif 93634e89507SJohannes Berg 93734e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 9380fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr); 93934e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 94034e89507SJohannes Berg cancel_work_sync(&sta->drv_unblock_wk); 94134e89507SJohannes Berg 942ec15e68bSJouni Malinen cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL); 943ec15e68bSJouni Malinen 94434e89507SJohannes Berg rate_control_remove_sta_debugfs(sta); 94534e89507SJohannes Berg ieee80211_sta_debugfs_remove(sta); 94634e89507SJohannes Berg 94734e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH 94834e89507SJohannes Berg if (ieee80211_vif_is_mesh(&sta->sdata->vif)) { 94934e89507SJohannes Berg mesh_plink_deactivate(sta); 95034e89507SJohannes Berg del_timer_sync(&sta->plink_timer); 95134e89507SJohannes Berg } 95234e89507SJohannes Berg #endif 95334e89507SJohannes Berg 95434e89507SJohannes Berg __sta_info_free(local, sta); 95534e89507SJohannes Berg 95634e89507SJohannes Berg return 0; 95734e89507SJohannes Berg } 95834e89507SJohannes Berg 95934e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) 96034e89507SJohannes Berg { 96134e89507SJohannes Berg struct sta_info *sta; 96234e89507SJohannes Berg int ret; 96334e89507SJohannes Berg 96434e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 9652a33bee2SGuy Eilam sta = sta_info_get_rx(sdata, addr); 96634e89507SJohannes Berg ret = __sta_info_destroy(sta); 96734e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 96834e89507SJohannes Berg 96934e89507SJohannes Berg return ret; 97034e89507SJohannes Berg } 97134e89507SJohannes Berg 97234e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, 97334e89507SJohannes Berg const u8 *addr) 97434e89507SJohannes Berg { 97534e89507SJohannes Berg struct sta_info *sta; 97634e89507SJohannes Berg int ret; 97734e89507SJohannes Berg 97834e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 9792a33bee2SGuy Eilam sta = sta_info_get_bss_rx(sdata, addr); 98034e89507SJohannes Berg ret = __sta_info_destroy(sta); 98134e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 98234e89507SJohannes Berg 98334e89507SJohannes Berg return ret; 98434e89507SJohannes Berg } 985f0706e82SJiri Benc 986f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data) 987f0706e82SJiri Benc { 988f0706e82SJiri Benc struct ieee80211_local *local = (struct ieee80211_local *) data; 989f0706e82SJiri Benc struct sta_info *sta; 9903393a608SJuuso Oikarinen bool timer_needed = false; 991f0706e82SJiri Benc 992d0709a65SJohannes Berg rcu_read_lock(); 993d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) 9943393a608SJuuso Oikarinen if (sta_info_cleanup_expire_buffered(local, sta)) 9953393a608SJuuso Oikarinen timer_needed = true; 996d0709a65SJohannes Berg rcu_read_unlock(); 997f0706e82SJiri Benc 9985bb644a0SJohannes Berg if (local->quiescing) 9995bb644a0SJohannes Berg return; 10005bb644a0SJohannes Berg 10013393a608SJuuso Oikarinen if (!timer_needed) 10023393a608SJuuso Oikarinen return; 10033393a608SJuuso Oikarinen 100426d59535SJohannes Berg mod_timer(&local->sta_cleanup, 100526d59535SJohannes Berg round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 1006f0706e82SJiri Benc } 1007f0706e82SJiri Benc 1008f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local) 1009f0706e82SJiri Benc { 1010d0709a65SJohannes Berg spin_lock_init(&local->sta_lock); 101134e89507SJohannes Berg mutex_init(&local->sta_mtx); 1012f0706e82SJiri Benc INIT_LIST_HEAD(&local->sta_list); 101334e89507SJohannes Berg INIT_LIST_HEAD(&local->sta_pending_list); 101434e89507SJohannes Berg INIT_WORK(&local->sta_finish_work, sta_info_finish_work); 1015f0706e82SJiri Benc 1016b24b8a24SPavel Emelyanov setup_timer(&local->sta_cleanup, sta_info_cleanup, 1017b24b8a24SPavel Emelyanov (unsigned long)local); 1018f0706e82SJiri Benc } 1019f0706e82SJiri Benc 1020f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local) 1021f0706e82SJiri Benc { 1022f0706e82SJiri Benc del_timer(&local->sta_cleanup); 1023be8755e1SMichael Wu sta_info_flush(local, NULL); 1024f0706e82SJiri Benc } 1025f0706e82SJiri Benc 1026f0706e82SJiri Benc /** 1027f0706e82SJiri Benc * sta_info_flush - flush matching STA entries from the STA table 102844213b5eSJohannes Berg * 102944213b5eSJohannes Berg * Returns the number of removed STA entries. 103044213b5eSJohannes Berg * 1031f0706e82SJiri Benc * @local: local interface data 1032d0709a65SJohannes Berg * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs 1033f0706e82SJiri Benc */ 103444213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local, 1035d0709a65SJohannes Berg struct ieee80211_sub_if_data *sdata) 1036f0706e82SJiri Benc { 1037f0706e82SJiri Benc struct sta_info *sta, *tmp; 103844213b5eSJohannes Berg int ret = 0; 1039f0706e82SJiri Benc 1040d0709a65SJohannes Berg might_sleep(); 1041d0709a65SJohannes Berg 104234e89507SJohannes Berg mutex_lock(&local->sta_mtx); 1043d0709a65SJohannes Berg 104434e89507SJohannes Berg sta_info_finish_pending(local); 104534e89507SJohannes Berg 104634e89507SJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 104734e89507SJohannes Berg if (!sdata || sdata == sta->sdata) 104834e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 104934e89507SJohannes Berg } 105034e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 105144213b5eSJohannes Berg 105244213b5eSJohannes Berg return ret; 1053f0706e82SJiri Benc } 1054dc6676b7SJohannes Berg 105524723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 105624723d1bSJohannes Berg unsigned long exp_time) 105724723d1bSJohannes Berg { 105824723d1bSJohannes Berg struct ieee80211_local *local = sdata->local; 105924723d1bSJohannes Berg struct sta_info *sta, *tmp; 106024723d1bSJohannes Berg 106134e89507SJohannes Berg mutex_lock(&local->sta_mtx); 106224723d1bSJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) 106324723d1bSJohannes Berg if (time_after(jiffies, sta->last_rx + exp_time)) { 106424723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG 10650c68ae26SJohannes Berg printk(KERN_DEBUG "%s: expiring inactive STA %pM\n", 106647846c9bSJohannes Berg sdata->name, sta->sta.addr); 106724723d1bSJohannes Berg #endif 106834e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 106924723d1bSJohannes Berg } 107034e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 107124723d1bSJohannes Berg } 107217741cdcSJohannes Berg 1073686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 1074686b9cb9SBen Greear const u8 *addr, 1075686b9cb9SBen Greear const u8 *localaddr) 107617741cdcSJohannes Berg { 1077abe60632SJohannes Berg struct sta_info *sta, *nxt; 107817741cdcSJohannes Berg 1079686b9cb9SBen Greear /* 1080686b9cb9SBen Greear * Just return a random station if localaddr is NULL 1081686b9cb9SBen Greear * ... first in list. 1082686b9cb9SBen Greear */ 1083f7c65594SJohannes Berg for_each_sta_info(hw_to_local(hw), addr, sta, nxt) { 1084686b9cb9SBen Greear if (localaddr && 1085686b9cb9SBen Greear compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0) 1086686b9cb9SBen Greear continue; 1087f7c65594SJohannes Berg if (!sta->uploaded) 1088f7c65594SJohannes Berg return NULL; 108917741cdcSJohannes Berg return &sta->sta; 1090f7c65594SJohannes Berg } 1091f7c65594SJohannes Berg 1092abe60632SJohannes Berg return NULL; 109317741cdcSJohannes Berg } 1094686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); 10955ed176e1SJohannes Berg 10965ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, 10975ed176e1SJohannes Berg const u8 *addr) 10985ed176e1SJohannes Berg { 1099f7c65594SJohannes Berg struct sta_info *sta; 11005ed176e1SJohannes Berg 11015ed176e1SJohannes Berg if (!vif) 11025ed176e1SJohannes Berg return NULL; 11035ed176e1SJohannes Berg 1104f7c65594SJohannes Berg sta = sta_info_get_bss(vif_to_sdata(vif), addr); 1105f7c65594SJohannes Berg if (!sta) 1106f7c65594SJohannes Berg return NULL; 11075ed176e1SJohannes Berg 1108f7c65594SJohannes Berg if (!sta->uploaded) 1109f7c65594SJohannes Berg return NULL; 1110f7c65594SJohannes Berg 1111f7c65594SJohannes Berg return &sta->sta; 11125ed176e1SJohannes Berg } 111317741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta); 1114af818581SJohannes Berg 111550a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta) 111650a9432dSJohannes Berg { 111750a9432dSJohannes Berg struct sta_info *sta = _sta; 111850a9432dSJohannes Berg 111950a9432dSJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER | WLAN_STA_PS_STA); 112050a9432dSJohannes Berg } 112150a9432dSJohannes Berg 1122af818581SJohannes Berg /* powersave support code */ 1123af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) 1124af818581SJohannes Berg { 1125af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1126af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 1127948d887dSJohannes Berg struct sk_buff_head pending; 1128948d887dSJohannes Berg int filtered = 0, buffered = 0, ac; 1129af818581SJohannes Berg 113047086fc5SJohannes Berg clear_sta_flags(sta, WLAN_STA_SP); 113147086fc5SJohannes Berg 1132948d887dSJohannes Berg BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1); 1133948d887dSJohannes Berg sta->driver_buffered_tids = 0; 1134948d887dSJohannes Berg 1135d057e5a3SArik Nemtsov if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS)) 113612375ef9SJohannes Berg drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 1137af818581SJohannes Berg 1138948d887dSJohannes Berg skb_queue_head_init(&pending); 1139948d887dSJohannes Berg 1140af818581SJohannes Berg /* Send all buffered frames to the station */ 1141948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1142948d887dSJohannes Berg int count = skb_queue_len(&pending), tmp; 1143948d887dSJohannes Berg 1144948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending); 1145948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1146948d887dSJohannes Berg filtered += tmp - count; 1147948d887dSJohannes Berg count = tmp; 1148948d887dSJohannes Berg 1149948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending); 1150948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1151948d887dSJohannes Berg buffered += tmp - count; 1152948d887dSJohannes Berg } 1153948d887dSJohannes Berg 1154948d887dSJohannes Berg ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta); 1155948d887dSJohannes Berg 1156af818581SJohannes Berg local->total_ps_buffered -= buffered; 1157af818581SJohannes Berg 1158c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1159c868cb35SJohannes Berg 1160af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 1161af818581SJohannes Berg printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames " 116247846c9bSJohannes Berg "since STA not sleeping anymore\n", sdata->name, 1163948d887dSJohannes Berg sta->sta.addr, sta->sta.aid, filtered, buffered); 1164af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 1165af818581SJohannes Berg } 1166af818581SJohannes Berg 1167ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata, 1168ce662b44SJohannes Berg struct sta_info *sta, int tid, 1169ce662b44SJohannes Berg bool uapsd) 1170ce662b44SJohannes Berg { 1171ce662b44SJohannes Berg struct ieee80211_local *local = sdata->local; 1172ce662b44SJohannes Berg struct ieee80211_qos_hdr *nullfunc; 1173ce662b44SJohannes Berg struct sk_buff *skb; 1174ce662b44SJohannes Berg int size = sizeof(*nullfunc); 1175ce662b44SJohannes Berg __le16 fc; 1176ce662b44SJohannes Berg bool qos = test_sta_flags(sta, WLAN_STA_WME); 1177ce662b44SJohannes Berg struct ieee80211_tx_info *info; 1178ce662b44SJohannes Berg 1179ce662b44SJohannes Berg if (qos) { 1180ce662b44SJohannes Berg fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1181ce662b44SJohannes Berg IEEE80211_STYPE_QOS_NULLFUNC | 1182ce662b44SJohannes Berg IEEE80211_FCTL_FROMDS); 1183ce662b44SJohannes Berg } else { 1184ce662b44SJohannes Berg size -= 2; 1185ce662b44SJohannes Berg fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1186ce662b44SJohannes Berg IEEE80211_STYPE_NULLFUNC | 1187ce662b44SJohannes Berg IEEE80211_FCTL_FROMDS); 1188ce662b44SJohannes Berg } 1189ce662b44SJohannes Berg 1190ce662b44SJohannes Berg skb = dev_alloc_skb(local->hw.extra_tx_headroom + size); 1191ce662b44SJohannes Berg if (!skb) 1192ce662b44SJohannes Berg return; 1193ce662b44SJohannes Berg 1194ce662b44SJohannes Berg skb_reserve(skb, local->hw.extra_tx_headroom); 1195ce662b44SJohannes Berg 1196ce662b44SJohannes Berg nullfunc = (void *) skb_put(skb, size); 1197ce662b44SJohannes Berg nullfunc->frame_control = fc; 1198ce662b44SJohannes Berg nullfunc->duration_id = 0; 1199ce662b44SJohannes Berg memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN); 1200ce662b44SJohannes Berg memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN); 1201ce662b44SJohannes Berg memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN); 1202ce662b44SJohannes Berg 1203ce662b44SJohannes Berg if (qos) { 1204ce662b44SJohannes Berg skb->priority = tid; 1205ce662b44SJohannes Berg 1206ce662b44SJohannes Berg skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]); 1207ce662b44SJohannes Berg 1208ce662b44SJohannes Berg nullfunc->qos_ctrl = cpu_to_le16(tid); 1209ce662b44SJohannes Berg 1210ce662b44SJohannes Berg if (uapsd) 1211ce662b44SJohannes Berg nullfunc->qos_ctrl |= 1212ce662b44SJohannes Berg cpu_to_le16(IEEE80211_QOS_CTL_EOSP); 1213ce662b44SJohannes Berg } 1214ce662b44SJohannes Berg 1215ce662b44SJohannes Berg info = IEEE80211_SKB_CB(skb); 1216ce662b44SJohannes Berg 1217ce662b44SJohannes Berg /* 1218ce662b44SJohannes Berg * Tell TX path to send this frame even though the 1219ce662b44SJohannes Berg * STA may still remain is PS mode after this frame 1220*deeaee19SJohannes Berg * exchange. Also set EOSP to indicate this packet 1221*deeaee19SJohannes Berg * ends the poll/service period. 1222ce662b44SJohannes Berg */ 1223*deeaee19SJohannes Berg info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE | 1224*deeaee19SJohannes Berg IEEE80211_TX_STATUS_EOSP | 1225ce662b44SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 1226ce662b44SJohannes Berg 1227ce662b44SJohannes Berg ieee80211_xmit(sdata, skb); 1228ce662b44SJohannes Berg } 1229ce662b44SJohannes Berg 123047086fc5SJohannes Berg static void 123147086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta, 123247086fc5SJohannes Berg int n_frames, u8 ignored_acs, 123347086fc5SJohannes Berg enum ieee80211_frame_release_type reason) 1234af818581SJohannes Berg { 1235af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1236af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 12374049e09aSJohannes Berg bool found = false; 1238948d887dSJohannes Berg bool more_data = false; 1239948d887dSJohannes Berg int ac; 12404049e09aSJohannes Berg unsigned long driver_release_tids = 0; 124147086fc5SJohannes Berg struct sk_buff_head frames; 124247086fc5SJohannes Berg 1243*deeaee19SJohannes Berg /* Service or PS-Poll period starts */ 1244*deeaee19SJohannes Berg set_sta_flags(sta, WLAN_STA_SP); 1245*deeaee19SJohannes Berg 124647086fc5SJohannes Berg __skb_queue_head_init(&frames); 1247af818581SJohannes Berg 1248948d887dSJohannes Berg /* 124947086fc5SJohannes Berg * Get response frame(s) and more data bit for it. 1250948d887dSJohannes Berg */ 1251948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 12524049e09aSJohannes Berg unsigned long tids; 12534049e09aSJohannes Berg 125447086fc5SJohannes Berg if (ignored_acs & BIT(ac)) 1255948d887dSJohannes Berg continue; 1256948d887dSJohannes Berg 12574049e09aSJohannes Berg tids = ieee80211_tids_for_ac(ac); 12584049e09aSJohannes Berg 12594049e09aSJohannes Berg if (!found) { 12604049e09aSJohannes Berg driver_release_tids = sta->driver_buffered_tids & tids; 12614049e09aSJohannes Berg if (driver_release_tids) { 12624049e09aSJohannes Berg found = true; 12634049e09aSJohannes Berg } else { 126447086fc5SJohannes Berg struct sk_buff *skb; 126547086fc5SJohannes Berg 126647086fc5SJohannes Berg while (n_frames > 0) { 1267948d887dSJohannes Berg skb = skb_dequeue(&sta->tx_filtered[ac]); 1268948d887dSJohannes Berg if (!skb) { 126947086fc5SJohannes Berg skb = skb_dequeue( 127047086fc5SJohannes Berg &sta->ps_tx_buf[ac]); 1271af818581SJohannes Berg if (skb) 1272af818581SJohannes Berg local->total_ps_buffered--; 1273af818581SJohannes Berg } 127447086fc5SJohannes Berg if (!skb) 127547086fc5SJohannes Berg break; 127647086fc5SJohannes Berg n_frames--; 12774049e09aSJohannes Berg found = true; 127847086fc5SJohannes Berg __skb_queue_tail(&frames, skb); 127947086fc5SJohannes Berg } 1280948d887dSJohannes Berg } 1281948d887dSJohannes Berg 12824049e09aSJohannes Berg /* 12834049e09aSJohannes Berg * If the driver has data on more than one TID then 12844049e09aSJohannes Berg * certainly there's more data if we release just a 12854049e09aSJohannes Berg * single frame now (from a single TID). 12864049e09aSJohannes Berg */ 128747086fc5SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_PSPOLL && 128847086fc5SJohannes Berg hweight16(driver_release_tids) > 1) { 12894049e09aSJohannes Berg more_data = true; 12904049e09aSJohannes Berg driver_release_tids = 12914049e09aSJohannes Berg BIT(ffs(driver_release_tids) - 1); 12924049e09aSJohannes Berg break; 12934049e09aSJohannes Berg } 12944049e09aSJohannes Berg } 1295948d887dSJohannes Berg 1296948d887dSJohannes Berg if (!skb_queue_empty(&sta->tx_filtered[ac]) || 1297948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac])) { 1298948d887dSJohannes Berg more_data = true; 1299948d887dSJohannes Berg break; 1300948d887dSJohannes Berg } 1301948d887dSJohannes Berg } 1302af818581SJohannes Berg 13034049e09aSJohannes Berg if (!found) { 1304ce662b44SJohannes Berg int tid; 13054049e09aSJohannes Berg 1306ce662b44SJohannes Berg /* 1307ce662b44SJohannes Berg * For PS-Poll, this can only happen due to a race condition 1308ce662b44SJohannes Berg * when we set the TIM bit and the station notices it, but 1309ce662b44SJohannes Berg * before it can poll for the frame we expire it. 1310ce662b44SJohannes Berg * 1311ce662b44SJohannes Berg * For uAPSD, this is said in the standard (11.2.1.5 h): 1312ce662b44SJohannes Berg * At each unscheduled SP for a non-AP STA, the AP shall 1313ce662b44SJohannes Berg * attempt to transmit at least one MSDU or MMPDU, but no 1314ce662b44SJohannes Berg * more than the value specified in the Max SP Length field 1315ce662b44SJohannes Berg * in the QoS Capability element from delivery-enabled ACs, 1316ce662b44SJohannes Berg * that are destined for the non-AP STA. 1317ce662b44SJohannes Berg * 1318ce662b44SJohannes Berg * Since we have no other MSDU/MMPDU, transmit a QoS null frame. 1319ce662b44SJohannes Berg */ 1320ce662b44SJohannes Berg 1321ce662b44SJohannes Berg /* This will evaluate to 1, 3, 5 or 7. */ 1322ce662b44SJohannes Berg tid = 7 - ((ffs(~ignored_acs) - 1) << 1); 1323ce662b44SJohannes Berg 1324ce662b44SJohannes Berg ieee80211_send_null_response(sdata, sta, tid, 1325ce662b44SJohannes Berg reason == IEEE80211_FRAME_RELEASE_UAPSD); 13264049e09aSJohannes Berg return; 13274049e09aSJohannes Berg } 13284049e09aSJohannes Berg 132947086fc5SJohannes Berg if (!driver_release_tids) { 133047086fc5SJohannes Berg struct sk_buff_head pending; 133147086fc5SJohannes Berg struct sk_buff *skb; 133247086fc5SJohannes Berg 133347086fc5SJohannes Berg skb_queue_head_init(&pending); 133447086fc5SJohannes Berg 133547086fc5SJohannes Berg while ((skb = __skb_dequeue(&frames))) { 1336af818581SJohannes Berg struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 133747086fc5SJohannes Berg struct ieee80211_hdr *hdr = (void *) skb->data; 1338af818581SJohannes Berg 1339af818581SJohannes Berg /* 134047086fc5SJohannes Berg * Tell TX path to send this frame even though the 134147086fc5SJohannes Berg * STA may still remain is PS mode after this frame 134247086fc5SJohannes Berg * exchange. 1343af818581SJohannes Berg */ 134447086fc5SJohannes Berg info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE; 1345af818581SJohannes Berg 134647086fc5SJohannes Berg /* 134747086fc5SJohannes Berg * Use MoreData flag to indicate whether there are 134847086fc5SJohannes Berg * more buffered frames for this STA 134947086fc5SJohannes Berg */ 1350948d887dSJohannes Berg if (!more_data) 135147086fc5SJohannes Berg hdr->frame_control &= 135247086fc5SJohannes Berg cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 1353af818581SJohannes Berg else 135447086fc5SJohannes Berg hdr->frame_control |= 135547086fc5SJohannes Berg cpu_to_le16(IEEE80211_FCTL_MOREDATA); 1356af818581SJohannes Berg 135747086fc5SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_UAPSD && 135847086fc5SJohannes Berg skb_queue_empty(&frames)) { 135947086fc5SJohannes Berg /* set EOSP for the frame */ 136047086fc5SJohannes Berg u8 *p = ieee80211_get_qos_ctl(hdr); 136147086fc5SJohannes Berg *p |= IEEE80211_QOS_CTL_EOSP; 1362*deeaee19SJohannes Berg } 1363*deeaee19SJohannes Berg 136447086fc5SJohannes Berg info->flags |= IEEE80211_TX_STATUS_EOSP | 136547086fc5SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 136647086fc5SJohannes Berg 136747086fc5SJohannes Berg __skb_queue_tail(&pending, skb); 136847086fc5SJohannes Berg } 136947086fc5SJohannes Berg 137047086fc5SJohannes Berg ieee80211_add_pending_skbs(local, &pending); 1371af818581SJohannes Berg 1372c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1373af818581SJohannes Berg } else { 1374af818581SJohannes Berg /* 13754049e09aSJohannes Berg * We need to release a frame that is buffered somewhere in the 13764049e09aSJohannes Berg * driver ... it'll have to handle that. 13774049e09aSJohannes Berg * Note that, as per the comment above, it'll also have to see 13784049e09aSJohannes Berg * if there is more than just one frame on the specific TID that 13794049e09aSJohannes Berg * we're releasing from, and it needs to set the more-data bit 13804049e09aSJohannes Berg * accordingly if we tell it that there's no more data. If we do 13814049e09aSJohannes Berg * tell it there's more data, then of course the more-data bit 13824049e09aSJohannes Berg * needs to be set anyway. 1383af818581SJohannes Berg */ 13844049e09aSJohannes Berg drv_release_buffered_frames(local, sta, driver_release_tids, 138547086fc5SJohannes Berg n_frames, reason, more_data); 13864049e09aSJohannes Berg 13874049e09aSJohannes Berg /* 13884049e09aSJohannes Berg * Note that we don't recalculate the TIM bit here as it would 13894049e09aSJohannes Berg * most likely have no effect at all unless the driver told us 13904049e09aSJohannes Berg * that the TID became empty before returning here from the 13914049e09aSJohannes Berg * release function. 13924049e09aSJohannes Berg * Either way, however, when the driver tells us that the TID 13934049e09aSJohannes Berg * became empty we'll do the TIM recalculation. 13944049e09aSJohannes Berg */ 1395af818581SJohannes Berg } 1396af818581SJohannes Berg } 1397af818581SJohannes Berg 139847086fc5SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 139947086fc5SJohannes Berg { 140047086fc5SJohannes Berg u8 ignore_for_response = sta->sta.uapsd_queues; 140147086fc5SJohannes Berg 140247086fc5SJohannes Berg /* 140347086fc5SJohannes Berg * If all ACs are delivery-enabled then we should reply 140447086fc5SJohannes Berg * from any of them, if only some are enabled we reply 140547086fc5SJohannes Berg * only from the non-enabled ones. 140647086fc5SJohannes Berg */ 140747086fc5SJohannes Berg if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1) 140847086fc5SJohannes Berg ignore_for_response = 0; 140947086fc5SJohannes Berg 141047086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response, 141147086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_PSPOLL); 141247086fc5SJohannes Berg } 141347086fc5SJohannes Berg 141447086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta) 141547086fc5SJohannes Berg { 141647086fc5SJohannes Berg int n_frames = sta->sta.max_sp; 141747086fc5SJohannes Berg u8 delivery_enabled = sta->sta.uapsd_queues; 141847086fc5SJohannes Berg 141947086fc5SJohannes Berg /* 142047086fc5SJohannes Berg * If we ever grow support for TSPEC this might happen if 142147086fc5SJohannes Berg * the TSPEC update from hostapd comes in between a trigger 142247086fc5SJohannes Berg * frame setting WLAN_STA_UAPSD in the RX path and this 142347086fc5SJohannes Berg * actually getting called. 142447086fc5SJohannes Berg */ 142547086fc5SJohannes Berg if (!delivery_enabled) 142647086fc5SJohannes Berg return; 142747086fc5SJohannes Berg 142847086fc5SJohannes Berg switch (sta->sta.max_sp) { 142947086fc5SJohannes Berg case 1: 143047086fc5SJohannes Berg n_frames = 2; 143147086fc5SJohannes Berg break; 143247086fc5SJohannes Berg case 2: 143347086fc5SJohannes Berg n_frames = 4; 143447086fc5SJohannes Berg break; 143547086fc5SJohannes Berg case 3: 143647086fc5SJohannes Berg n_frames = 6; 143747086fc5SJohannes Berg break; 143847086fc5SJohannes Berg case 0: 143947086fc5SJohannes Berg /* XXX: what is a good value? */ 144047086fc5SJohannes Berg n_frames = 8; 144147086fc5SJohannes Berg break; 144247086fc5SJohannes Berg } 144347086fc5SJohannes Berg 144447086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled, 144547086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_UAPSD); 144647086fc5SJohannes Berg } 144747086fc5SJohannes Berg 1448af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 1449af818581SJohannes Berg struct ieee80211_sta *pubsta, bool block) 1450af818581SJohannes Berg { 1451af818581SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1452af818581SJohannes Berg 1453b5878a2dSJohannes Berg trace_api_sta_block_awake(sta->local, pubsta, block); 1454b5878a2dSJohannes Berg 1455af818581SJohannes Berg if (block) 1456af818581SJohannes Berg set_sta_flags(sta, WLAN_STA_PS_DRIVER); 145750a9432dSJohannes Berg else if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) 1458af818581SJohannes Berg ieee80211_queue_work(hw, &sta->drv_unblock_wk); 1459af818581SJohannes Berg } 1460af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake); 1461dcf55fb5SFelix Fietkau 1462042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, 1463042ec453SJohannes Berg u8 tid, bool buffered) 1464dcf55fb5SFelix Fietkau { 1465dcf55fb5SFelix Fietkau struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1466dcf55fb5SFelix Fietkau 1467948d887dSJohannes Berg if (WARN_ON(tid >= STA_TID_NUM)) 1468042ec453SJohannes Berg return; 1469042ec453SJohannes Berg 1470948d887dSJohannes Berg if (buffered) 1471948d887dSJohannes Berg set_bit(tid, &sta->driver_buffered_tids); 1472948d887dSJohannes Berg else 1473948d887dSJohannes Berg clear_bit(tid, &sta->driver_buffered_tids); 1474948d887dSJohannes Berg 1475c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1476dcf55fb5SFelix Fietkau } 1477042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered); 1478