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 507e189a12SLuis R. Rodriguez * receive an assocation 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 5234e89507SJohannes Berg * the peer imediately 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 70*40b275b6SJohannes Berg s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)], 71*40b275b6SJohannes 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 80*40b275b6SJohannes Berg while (rcu_access_pointer(s->hnext) && 81*40b275b6SJohannes Berg rcu_access_pointer(s->hnext) != sta) 82*40b275b6SJohannes Berg s = rcu_dereference_protected(s->hnext, 83*40b275b6SJohannes Berg lockdep_is_held(&local->sta_lock)); 84*40b275b6SJohannes 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 rcu_read_lock_held() || 1010379185bSJohannes Berg lockdep_is_held(&local->sta_lock) || 1020379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 10343ba7e95SJohannes Berg while (sta) { 104abe60632SJohannes Berg if (sta->sdata == sdata && 105abe60632SJohannes Berg memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 10643ba7e95SJohannes Berg break; 1070379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1080379185bSJohannes Berg rcu_read_lock_held() || 1090379185bSJohannes Berg lockdep_is_held(&local->sta_lock) || 1100379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 11143ba7e95SJohannes Berg } 11243ba7e95SJohannes Berg return sta; 11343ba7e95SJohannes Berg } 11443ba7e95SJohannes Berg 1150e5ded5aSFelix Fietkau /* 1160e5ded5aSFelix Fietkau * Get sta info either from the specified interface 1170e5ded5aSFelix Fietkau * or from one of its vlans 1180e5ded5aSFelix Fietkau */ 1190e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata, 1200e5ded5aSFelix Fietkau const u8 *addr) 1210e5ded5aSFelix Fietkau { 1220e5ded5aSFelix Fietkau struct ieee80211_local *local = sdata->local; 1230e5ded5aSFelix Fietkau struct sta_info *sta; 1240e5ded5aSFelix Fietkau 1250379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1260379185bSJohannes Berg rcu_read_lock_held() || 1270379185bSJohannes Berg lockdep_is_held(&local->sta_lock) || 1280379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1290e5ded5aSFelix Fietkau while (sta) { 1300e5ded5aSFelix Fietkau if ((sta->sdata == sdata || 131a2c1e3daSJohannes Berg (sta->sdata->bss && sta->sdata->bss == sdata->bss)) && 1320e5ded5aSFelix Fietkau memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 1330e5ded5aSFelix Fietkau break; 1340379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1350379185bSJohannes Berg rcu_read_lock_held() || 1360379185bSJohannes Berg lockdep_is_held(&local->sta_lock) || 1370379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1380e5ded5aSFelix Fietkau } 1390e5ded5aSFelix Fietkau return sta; 1400e5ded5aSFelix Fietkau } 1410e5ded5aSFelix Fietkau 1423b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, 1433b53fde8SJohannes Berg int idx) 144ee385855SLuis Carlos Cobo { 1453b53fde8SJohannes Berg struct ieee80211_local *local = sdata->local; 146ee385855SLuis Carlos Cobo struct sta_info *sta; 147ee385855SLuis Carlos Cobo int i = 0; 148ee385855SLuis Carlos Cobo 149d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) { 1503b53fde8SJohannes Berg if (sdata != sta->sdata) 1512a8ca29aSLuis Carlos Cobo continue; 152ee385855SLuis Carlos Cobo if (i < idx) { 153ee385855SLuis Carlos Cobo ++i; 154ee385855SLuis Carlos Cobo continue; 155ee385855SLuis Carlos Cobo } 1562a8ca29aSLuis Carlos Cobo return sta; 157ee385855SLuis Carlos Cobo } 158ee385855SLuis Carlos Cobo 159ee385855SLuis Carlos Cobo return NULL; 160ee385855SLuis Carlos Cobo } 161f0706e82SJiri Benc 16293e5deb1SJohannes Berg /** 16393e5deb1SJohannes Berg * __sta_info_free - internal STA free helper 16493e5deb1SJohannes Berg * 1656ef307bcSRandy Dunlap * @local: pointer to the global information 16693e5deb1SJohannes Berg * @sta: STA info to free 16793e5deb1SJohannes Berg * 16893e5deb1SJohannes Berg * This function must undo everything done by sta_info_alloc() 16993e5deb1SJohannes Berg * that may happen before sta_info_insert(). 17093e5deb1SJohannes Berg */ 17193e5deb1SJohannes Berg static void __sta_info_free(struct ieee80211_local *local, 17293e5deb1SJohannes Berg struct sta_info *sta) 17393e5deb1SJohannes Berg { 174af65cd96SJohannes Berg if (sta->rate_ctrl) { 1754b7679a5SJohannes Berg rate_control_free_sta(sta); 17693e5deb1SJohannes Berg rate_control_put(sta->rate_ctrl); 177af65cd96SJohannes Berg } 17893e5deb1SJohannes Berg 17993e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 1800fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr); 18193e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 18293e5deb1SJohannes Berg 18393e5deb1SJohannes Berg kfree(sta); 18493e5deb1SJohannes Berg } 18593e5deb1SJohannes Berg 186d0709a65SJohannes Berg /* Caller must hold local->sta_lock */ 187d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local, 188d0709a65SJohannes Berg struct sta_info *sta) 189f0706e82SJiri Benc { 19017741cdcSJohannes Berg sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)]; 19117741cdcSJohannes Berg rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta); 192f0706e82SJiri Benc } 193f0706e82SJiri Benc 194af818581SJohannes Berg static void sta_unblock(struct work_struct *wk) 195af818581SJohannes Berg { 196af818581SJohannes Berg struct sta_info *sta; 197af818581SJohannes Berg 198af818581SJohannes Berg sta = container_of(wk, struct sta_info, drv_unblock_wk); 199af818581SJohannes Berg 200af818581SJohannes Berg if (sta->dead) 201af818581SJohannes Berg return; 202af818581SJohannes Berg 203af818581SJohannes Berg if (!test_sta_flags(sta, WLAN_STA_PS_STA)) 204af818581SJohannes Berg ieee80211_sta_ps_deliver_wakeup(sta); 20550a9432dSJohannes Berg else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL)) { 20650a9432dSJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER); 207af818581SJohannes Berg ieee80211_sta_ps_deliver_poll_response(sta); 20850a9432dSJohannes Berg } else 20950a9432dSJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER); 210af818581SJohannes Berg } 211af818581SJohannes Berg 212af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local, 213af65cd96SJohannes Berg struct sta_info *sta, gfp_t gfp) 214af65cd96SJohannes Berg { 215af65cd96SJohannes Berg if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) 216af65cd96SJohannes Berg return 0; 217af65cd96SJohannes Berg 218af65cd96SJohannes Berg sta->rate_ctrl = rate_control_get(local->rate_ctrl); 219af65cd96SJohannes Berg sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, 220af65cd96SJohannes Berg &sta->sta, gfp); 221af65cd96SJohannes Berg if (!sta->rate_ctrl_priv) { 222af65cd96SJohannes Berg rate_control_put(sta->rate_ctrl); 223af65cd96SJohannes Berg return -ENOMEM; 224af65cd96SJohannes Berg } 225af65cd96SJohannes Berg 226af65cd96SJohannes Berg return 0; 227af65cd96SJohannes Berg } 228af65cd96SJohannes Berg 22973651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, 23073651ee6SJohannes Berg u8 *addr, gfp_t gfp) 231f0706e82SJiri Benc { 232d0709a65SJohannes Berg struct ieee80211_local *local = sdata->local; 233f0706e82SJiri Benc struct sta_info *sta; 234ebe27c91SMohammed Shafi Shajakhan struct timespec uptime; 23516c5f15cSRon Rindjunsky int i; 236f0706e82SJiri Benc 23717741cdcSJohannes Berg sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp); 238f0706e82SJiri Benc if (!sta) 23973651ee6SJohannes Berg return NULL; 240f0706e82SJiri Benc 24107346f81SJohannes Berg spin_lock_init(&sta->lock); 2425a9f7b04SJohannes Berg spin_lock_init(&sta->flaglock); 243af818581SJohannes Berg INIT_WORK(&sta->drv_unblock_wk, sta_unblock); 24467c282c0SJohannes Berg INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work); 245a93e3644SJohannes Berg mutex_init(&sta->ampdu_mlme.mtx); 24607346f81SJohannes Berg 24717741cdcSJohannes Berg memcpy(sta->sta.addr, addr, ETH_ALEN); 248d0709a65SJohannes Berg sta->local = local; 249d0709a65SJohannes Berg sta->sdata = sdata; 2508bc8aecdSFelix Fietkau sta->last_rx = jiffies; 251f0706e82SJiri Benc 252ebe27c91SMohammed Shafi Shajakhan do_posix_clock_monotonic_gettime(&uptime); 253ebe27c91SMohammed Shafi Shajakhan sta->last_connected = uptime.tv_sec; 254541a45a1SBruno Randolf ewma_init(&sta->avg_signal, 1024, 8); 255541a45a1SBruno Randolf 256af65cd96SJohannes Berg if (sta_prepare_rate_control(local, sta, gfp)) { 257f0706e82SJiri Benc kfree(sta); 25873651ee6SJohannes Berg return NULL; 259f0706e82SJiri Benc } 260f0706e82SJiri Benc 26116c5f15cSRon Rindjunsky for (i = 0; i < STA_TID_NUM; i++) { 262a622ab72SJohannes Berg /* 263a622ab72SJohannes Berg * timer_to_tid must be initialized with identity mapping 264a622ab72SJohannes Berg * to enable session_timer's data differentiation. See 265a622ab72SJohannes Berg * sta_rx_agg_session_timer_expired for usage. 266a622ab72SJohannes Berg */ 26716c5f15cSRon Rindjunsky sta->timer_to_tid[i] = i; 26816c5f15cSRon Rindjunsky } 269f0706e82SJiri Benc skb_queue_head_init(&sta->ps_tx_buf); 270f0706e82SJiri Benc skb_queue_head_init(&sta->tx_filtered); 27173651ee6SJohannes Berg 272cccaec98SSenthil Balasubramanian for (i = 0; i < NUM_RX_DATA_QUEUES; i++) 2734be929beSAlexey Dobriyan sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); 274cccaec98SSenthil Balasubramanian 27573651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 2760fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr); 27773651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 27873651ee6SJohannes Berg 27903e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH 280b4e08ea1SLuis Carlos Cobo sta->plink_state = PLINK_LISTEN; 28103e4497eSJohannes Berg init_timer(&sta->plink_timer); 28203e4497eSJohannes Berg #endif 28303e4497eSJohannes Berg 28473651ee6SJohannes Berg return sta; 28573651ee6SJohannes Berg } 28673651ee6SJohannes Berg 28734e89507SJohannes Berg static int sta_info_finish_insert(struct sta_info *sta, bool async) 28873651ee6SJohannes Berg { 28973651ee6SJohannes Berg struct ieee80211_local *local = sta->local; 29073651ee6SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 29198b62183SJohannes Berg struct station_info sinfo; 29273651ee6SJohannes Berg unsigned long flags; 29393e5deb1SJohannes Berg int err = 0; 29473651ee6SJohannes Berg 29546a5ebafSJohannes Berg lockdep_assert_held(&local->sta_mtx); 29634e89507SJohannes Berg 29734e89507SJohannes Berg /* notify driver */ 29834e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 29934e89507SJohannes Berg sdata = container_of(sdata->bss, 30034e89507SJohannes Berg struct ieee80211_sub_if_data, 30134e89507SJohannes Berg u.ap); 30234e89507SJohannes Berg err = drv_sta_add(local, sdata, &sta->sta); 30334e89507SJohannes Berg if (err) { 30434e89507SJohannes Berg if (!async) 30534e89507SJohannes Berg return err; 30634e89507SJohannes Berg printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to driver (%d)" 30734e89507SJohannes Berg " - keeping it anyway.\n", 30834e89507SJohannes Berg sdata->name, sta->sta.addr, err); 30934e89507SJohannes Berg } else { 31034e89507SJohannes Berg sta->uploaded = true; 31134e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 31234e89507SJohannes Berg if (async) 3130fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, 3140fb9a9ecSJoe Perches "Finished adding IBSS STA %pM\n", 3150fb9a9ecSJoe Perches sta->sta.addr); 31634e89507SJohannes Berg #endif 31734e89507SJohannes Berg } 31834e89507SJohannes Berg 31934e89507SJohannes Berg sdata = sta->sdata; 32034e89507SJohannes Berg 32134e89507SJohannes Berg if (!async) { 32234e89507SJohannes Berg local->num_sta++; 32334e89507SJohannes Berg local->sta_generation++; 32434e89507SJohannes Berg smp_mb(); 32534e89507SJohannes Berg 32634e89507SJohannes Berg /* make the station visible */ 32734e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 32834e89507SJohannes Berg sta_info_hash_add(local, sta); 32934e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 33034e89507SJohannes Berg } 33134e89507SJohannes Berg 33234e89507SJohannes Berg list_add(&sta->list, &local->sta_list); 33334e89507SJohannes Berg 33434e89507SJohannes Berg ieee80211_sta_debugfs_add(sta); 33534e89507SJohannes Berg rate_control_add_sta_debugfs(sta); 33634e89507SJohannes Berg 33734e89507SJohannes Berg sinfo.filled = 0; 33834e89507SJohannes Berg sinfo.generation = local->sta_generation; 33934e89507SJohannes Berg cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL); 34034e89507SJohannes Berg 34134e89507SJohannes Berg 34234e89507SJohannes Berg return 0; 34334e89507SJohannes Berg } 34434e89507SJohannes Berg 34534e89507SJohannes Berg static void sta_info_finish_pending(struct ieee80211_local *local) 34634e89507SJohannes Berg { 34734e89507SJohannes Berg struct sta_info *sta; 34834e89507SJohannes Berg unsigned long flags; 34934e89507SJohannes Berg 35034e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 35134e89507SJohannes Berg while (!list_empty(&local->sta_pending_list)) { 35234e89507SJohannes Berg sta = list_first_entry(&local->sta_pending_list, 35334e89507SJohannes Berg struct sta_info, list); 35434e89507SJohannes Berg list_del(&sta->list); 35534e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 35634e89507SJohannes Berg 35734e89507SJohannes Berg sta_info_finish_insert(sta, true); 35834e89507SJohannes Berg 35934e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 36034e89507SJohannes Berg } 36134e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 36234e89507SJohannes Berg } 36334e89507SJohannes Berg 36434e89507SJohannes Berg static void sta_info_finish_work(struct work_struct *work) 36534e89507SJohannes Berg { 36634e89507SJohannes Berg struct ieee80211_local *local = 36734e89507SJohannes Berg container_of(work, struct ieee80211_local, sta_finish_work); 36834e89507SJohannes Berg 36934e89507SJohannes Berg mutex_lock(&local->sta_mtx); 37034e89507SJohannes Berg sta_info_finish_pending(local); 37134e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 37234e89507SJohannes Berg } 37334e89507SJohannes Berg 37434e89507SJohannes Berg int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) 37534e89507SJohannes Berg { 37634e89507SJohannes Berg struct ieee80211_local *local = sta->local; 37734e89507SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 37834e89507SJohannes Berg unsigned long flags; 37934e89507SJohannes Berg int err = 0; 38034e89507SJohannes Berg 38103e4497eSJohannes Berg /* 38203e4497eSJohannes Berg * Can't be a WARN_ON because it can be triggered through a race: 38303e4497eSJohannes Berg * something inserts a STA (on one CPU) without holding the RTNL 38403e4497eSJohannes Berg * and another CPU turns off the net device. 38503e4497eSJohannes Berg */ 3869607e6b6SJohannes Berg if (unlikely(!ieee80211_sdata_running(sdata))) { 38793e5deb1SJohannes Berg err = -ENETDOWN; 38834e89507SJohannes Berg rcu_read_lock(); 38993e5deb1SJohannes Berg goto out_free; 39093e5deb1SJohannes Berg } 39103e4497eSJohannes Berg 39247846c9bSJohannes Berg if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 || 39317741cdcSJohannes Berg is_multicast_ether_addr(sta->sta.addr))) { 39493e5deb1SJohannes Berg err = -EINVAL; 39534e89507SJohannes Berg rcu_read_lock(); 39693e5deb1SJohannes Berg goto out_free; 39793e5deb1SJohannes Berg } 39844213b5eSJohannes Berg 39934e89507SJohannes Berg /* 40034e89507SJohannes Berg * In ad-hoc mode, we sometimes need to insert stations 40134e89507SJohannes Berg * from tasklet context from the RX path. To avoid races, 40234e89507SJohannes Berg * always do so in that case -- see the comment below. 40334e89507SJohannes Berg */ 40434e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { 405d0709a65SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 40643ba7e95SJohannes Berg /* check if STA exists already */ 40734e89507SJohannes Berg if (sta_info_get_bss(sdata, sta->sta.addr)) { 408d0709a65SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 40934e89507SJohannes Berg rcu_read_lock(); 41093e5deb1SJohannes Berg err = -EEXIST; 41193e5deb1SJohannes Berg goto out_free; 41243ba7e95SJohannes Berg } 41334e89507SJohannes Berg 414f0706e82SJiri Benc local->num_sta++; 41534e89507SJohannes Berg local->sta_generation++; 41634e89507SJohannes Berg smp_mb(); 417f0706e82SJiri Benc sta_info_hash_add(local, sta); 41832bfd35dSJohannes Berg 41934e89507SJohannes Berg list_add_tail(&sta->list, &local->sta_pending_list); 42032bfd35dSJohannes Berg 42134e89507SJohannes Berg rcu_read_lock(); 42234e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 42334e89507SJohannes Berg 42434e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 4250fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n", 4260fb9a9ecSJoe Perches sta->sta.addr); 42734e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 42834e89507SJohannes Berg 42934e89507SJohannes Berg ieee80211_queue_work(&local->hw, &local->sta_finish_work); 43034e89507SJohannes Berg 43134e89507SJohannes Berg return 0; 43234e89507SJohannes Berg } 43334e89507SJohannes Berg 43434e89507SJohannes Berg /* 43534e89507SJohannes Berg * On first glance, this will look racy, because the code 43634e89507SJohannes Berg * below this point, which inserts a station with sleeping, 43734e89507SJohannes Berg * unlocks the sta_lock between checking existence in the 43834e89507SJohannes Berg * hash table and inserting into it. 43934e89507SJohannes Berg * 44034e89507SJohannes Berg * However, it is not racy against itself because it keeps 44134e89507SJohannes Berg * the mutex locked. It still seems to race against the 44234e89507SJohannes Berg * above code that atomically inserts the station... That, 44334e89507SJohannes Berg * however, is not true because the above code can only 44434e89507SJohannes Berg * be invoked for IBSS interfaces, and the below code will 44534e89507SJohannes Berg * not be -- and the two do not race against each other as 44634e89507SJohannes Berg * the hash table also keys off the interface. 44734e89507SJohannes Berg */ 44834e89507SJohannes Berg 44934e89507SJohannes Berg might_sleep(); 45034e89507SJohannes Berg 45134e89507SJohannes Berg mutex_lock(&local->sta_mtx); 45234e89507SJohannes Berg 45334e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 45434e89507SJohannes Berg /* check if STA exists already */ 45534e89507SJohannes Berg if (sta_info_get_bss(sdata, sta->sta.addr)) { 45634e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 45738a679a5SJouni Malinen mutex_unlock(&local->sta_mtx); 45834e89507SJohannes Berg rcu_read_lock(); 45934e89507SJohannes Berg err = -EEXIST; 46034e89507SJohannes Berg goto out_free; 46134e89507SJohannes Berg } 46234e89507SJohannes Berg 46334e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 46434e89507SJohannes Berg 46534e89507SJohannes Berg err = sta_info_finish_insert(sta, false); 46634e89507SJohannes Berg if (err) { 46734e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 46834e89507SJohannes Berg rcu_read_lock(); 46934e89507SJohannes Berg goto out_free; 47032bfd35dSJohannes Berg } 471d0709a65SJohannes Berg 472f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 4730fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Inserted STA %pM\n", sta->sta.addr); 474f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 475f0706e82SJiri Benc 47634e89507SJohannes Berg /* move reference to rcu-protected */ 47734e89507SJohannes Berg rcu_read_lock(); 47834e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 479e9f207f0SJiri Benc 48073651ee6SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 48173651ee6SJohannes Berg mesh_accept_plinks_update(sdata); 48273651ee6SJohannes Berg 48373651ee6SJohannes Berg return 0; 48493e5deb1SJohannes Berg out_free: 48593e5deb1SJohannes Berg BUG_ON(!err); 48693e5deb1SJohannes Berg __sta_info_free(local, sta); 48793e5deb1SJohannes Berg return err; 488f0706e82SJiri Benc } 489f0706e82SJiri Benc 49034e89507SJohannes Berg int sta_info_insert(struct sta_info *sta) 49134e89507SJohannes Berg { 49234e89507SJohannes Berg int err = sta_info_insert_rcu(sta); 49334e89507SJohannes Berg 49434e89507SJohannes Berg rcu_read_unlock(); 49534e89507SJohannes Berg 49634e89507SJohannes Berg return err; 49734e89507SJohannes Berg } 49834e89507SJohannes Berg 499004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid) 500004c872eSJohannes Berg { 501004c872eSJohannes Berg /* 502004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 503004c872eSJohannes Berg * so this line may not be changed to use the __set_bit() format. 504004c872eSJohannes Berg */ 505004c872eSJohannes Berg bss->tim[aid / 8] |= (1 << (aid % 8)); 506004c872eSJohannes Berg } 507004c872eSJohannes Berg 508004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid) 509004c872eSJohannes Berg { 510004c872eSJohannes Berg /* 511004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 512004c872eSJohannes Berg * so this line may not be changed to use the __clear_bit() format. 513004c872eSJohannes Berg */ 514004c872eSJohannes Berg bss->tim[aid / 8] &= ~(1 << (aid % 8)); 515004c872eSJohannes Berg } 516004c872eSJohannes Berg 517004c872eSJohannes Berg static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss, 518004c872eSJohannes Berg struct sta_info *sta) 519004c872eSJohannes Berg { 5203e122be0SJohannes Berg BUG_ON(!bss); 5213e122be0SJohannes Berg 52217741cdcSJohannes Berg __bss_tim_set(bss, sta->sta.aid); 5233e122be0SJohannes Berg 524d0709a65SJohannes Berg if (sta->local->ops->set_tim) { 525d0709a65SJohannes Berg sta->local->tim_in_locked_section = true; 52624487981SJohannes Berg drv_set_tim(sta->local, &sta->sta, true); 527d0709a65SJohannes Berg sta->local->tim_in_locked_section = false; 528d0709a65SJohannes Berg } 529004c872eSJohannes Berg } 530004c872eSJohannes Berg 531004c872eSJohannes Berg void sta_info_set_tim_bit(struct sta_info *sta) 532004c872eSJohannes Berg { 533d0709a65SJohannes Berg unsigned long flags; 534004c872eSJohannes Berg 5353e122be0SJohannes Berg BUG_ON(!sta->sdata->bss); 5363e122be0SJohannes Berg 537d0709a65SJohannes Berg spin_lock_irqsave(&sta->local->sta_lock, flags); 538d0709a65SJohannes Berg __sta_info_set_tim_bit(sta->sdata->bss, sta); 539d0709a65SJohannes Berg spin_unlock_irqrestore(&sta->local->sta_lock, flags); 540004c872eSJohannes Berg } 541004c872eSJohannes Berg 542004c872eSJohannes Berg static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss, 543004c872eSJohannes Berg struct sta_info *sta) 544004c872eSJohannes Berg { 5453e122be0SJohannes Berg BUG_ON(!bss); 5463e122be0SJohannes Berg 54717741cdcSJohannes Berg __bss_tim_clear(bss, sta->sta.aid); 5483e122be0SJohannes Berg 549d0709a65SJohannes Berg if (sta->local->ops->set_tim) { 550d0709a65SJohannes Berg sta->local->tim_in_locked_section = true; 55124487981SJohannes Berg drv_set_tim(sta->local, &sta->sta, false); 552d0709a65SJohannes Berg sta->local->tim_in_locked_section = false; 553d0709a65SJohannes Berg } 554004c872eSJohannes Berg } 555004c872eSJohannes Berg 556004c872eSJohannes Berg void sta_info_clear_tim_bit(struct sta_info *sta) 557004c872eSJohannes Berg { 558d0709a65SJohannes Berg unsigned long flags; 559004c872eSJohannes Berg 5603e122be0SJohannes Berg BUG_ON(!sta->sdata->bss); 5613e122be0SJohannes Berg 562d0709a65SJohannes Berg spin_lock_irqsave(&sta->local->sta_lock, flags); 563d0709a65SJohannes Berg __sta_info_clear_tim_bit(sta->sdata->bss, sta); 564d0709a65SJohannes Berg spin_unlock_irqrestore(&sta->local->sta_lock, flags); 565004c872eSJohannes Berg } 566004c872eSJohannes Berg 56757c4d7b4SJohannes Berg static int sta_info_buffer_expired(struct sta_info *sta, 568f0706e82SJiri Benc struct sk_buff *skb) 569f0706e82SJiri Benc { 570e039fa4aSJohannes Berg struct ieee80211_tx_info *info; 571f0706e82SJiri Benc int timeout; 572f0706e82SJiri Benc 573f0706e82SJiri Benc if (!skb) 574f0706e82SJiri Benc return 0; 575f0706e82SJiri Benc 576e039fa4aSJohannes Berg info = IEEE80211_SKB_CB(skb); 577f0706e82SJiri Benc 578f0706e82SJiri Benc /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 57957c4d7b4SJohannes Berg timeout = (sta->listen_interval * 58057c4d7b4SJohannes Berg sta->sdata->vif.bss_conf.beacon_int * 58157c4d7b4SJohannes Berg 32 / 15625) * HZ; 582f0706e82SJiri Benc if (timeout < STA_TX_BUFFER_EXPIRE) 583f0706e82SJiri Benc timeout = STA_TX_BUFFER_EXPIRE; 584e039fa4aSJohannes Berg return time_after(jiffies, info->control.jiffies + timeout); 585f0706e82SJiri Benc } 586f0706e82SJiri Benc 587f0706e82SJiri Benc 5883393a608SJuuso Oikarinen static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 589f0706e82SJiri Benc struct sta_info *sta) 590f0706e82SJiri Benc { 591f0706e82SJiri Benc unsigned long flags; 592f0706e82SJiri Benc struct sk_buff *skb; 593f0706e82SJiri Benc 594f0706e82SJiri Benc if (skb_queue_empty(&sta->ps_tx_buf)) 5953393a608SJuuso Oikarinen return false; 596f0706e82SJiri Benc 597f0706e82SJiri Benc for (;;) { 598f0706e82SJiri Benc spin_lock_irqsave(&sta->ps_tx_buf.lock, flags); 599f0706e82SJiri Benc skb = skb_peek(&sta->ps_tx_buf); 60057c4d7b4SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 601f0706e82SJiri Benc skb = __skb_dequeue(&sta->ps_tx_buf); 602836341a7SJohannes Berg else 603f0706e82SJiri Benc skb = NULL; 604f0706e82SJiri Benc spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags); 605f0706e82SJiri Benc 606836341a7SJohannes Berg if (!skb) 607836341a7SJohannes Berg break; 608836341a7SJohannes Berg 609f0706e82SJiri Benc local->total_ps_buffered--; 610f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 6110c68ae26SJohannes Berg printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n", 6120c68ae26SJohannes Berg sta->sta.addr); 613f4ea83ddSJohannes Berg #endif 614f0706e82SJiri Benc dev_kfree_skb(skb); 615836341a7SJohannes Berg 616dcf55fb5SFelix Fietkau if (skb_queue_empty(&sta->ps_tx_buf) && 617dcf55fb5SFelix Fietkau !test_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF)) 618004c872eSJohannes Berg sta_info_clear_tim_bit(sta); 619f0706e82SJiri Benc } 6203393a608SJuuso Oikarinen 6213393a608SJuuso Oikarinen return true; 622f0706e82SJiri Benc } 623f0706e82SJiri Benc 62434e89507SJohannes Berg static int __must_check __sta_info_destroy(struct sta_info *sta) 62534e89507SJohannes Berg { 62634e89507SJohannes Berg struct ieee80211_local *local; 62734e89507SJohannes Berg struct ieee80211_sub_if_data *sdata; 62834e89507SJohannes Berg struct sk_buff *skb; 62934e89507SJohannes Berg unsigned long flags; 630e31b8213SJohannes Berg int ret, i; 63134e89507SJohannes Berg 63234e89507SJohannes Berg might_sleep(); 63334e89507SJohannes Berg 63434e89507SJohannes Berg if (!sta) 63534e89507SJohannes Berg return -ENOENT; 63634e89507SJohannes Berg 63734e89507SJohannes Berg local = sta->local; 63834e89507SJohannes Berg sdata = sta->sdata; 63934e89507SJohannes Berg 640098a6070SJohannes Berg /* 641098a6070SJohannes Berg * Before removing the station from the driver and 642098a6070SJohannes Berg * rate control, it might still start new aggregation 643098a6070SJohannes Berg * sessions -- block that to make sure the tear-down 644098a6070SJohannes Berg * will be sufficient. 645098a6070SJohannes Berg */ 646098a6070SJohannes Berg set_sta_flags(sta, WLAN_STA_BLOCK_BA); 64753f73c09SJohannes Berg ieee80211_sta_tear_down_BA_sessions(sta, true); 648098a6070SJohannes Berg 64934e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 65034e89507SJohannes Berg ret = sta_info_hash_del(local, sta); 65134e89507SJohannes Berg /* this might still be the pending list ... which is fine */ 65234e89507SJohannes Berg if (!ret) 65334e89507SJohannes Berg list_del(&sta->list); 65434e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 65534e89507SJohannes Berg if (ret) 65634e89507SJohannes Berg return ret; 65734e89507SJohannes Berg 6588cb23153SJohannes Berg mutex_lock(&local->key_mtx); 659e31b8213SJohannes Berg for (i = 0; i < NUM_DEFAULT_KEYS; i++) 660*40b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i])); 661e31b8213SJohannes Berg if (sta->ptk) 662*40b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->ptk)); 6638cb23153SJohannes Berg mutex_unlock(&local->key_mtx); 66434e89507SJohannes Berg 66534e89507SJohannes Berg sta->dead = true; 66634e89507SJohannes Berg 66734e89507SJohannes Berg if (test_and_clear_sta_flags(sta, 66834e89507SJohannes Berg WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) { 66934e89507SJohannes Berg BUG_ON(!sdata->bss); 67034e89507SJohannes Berg 67134e89507SJohannes Berg atomic_dec(&sdata->bss->num_sta_ps); 67234e89507SJohannes Berg __sta_info_clear_tim_bit(sdata->bss, sta); 67334e89507SJohannes Berg } 67434e89507SJohannes Berg 67534e89507SJohannes Berg local->num_sta--; 67634e89507SJohannes Berg local->sta_generation++; 67734e89507SJohannes Berg 67834e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 67934e89507SJohannes Berg rcu_assign_pointer(sdata->u.vlan.sta, NULL); 68034e89507SJohannes Berg 68134e89507SJohannes Berg if (sta->uploaded) { 68234e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 68334e89507SJohannes Berg sdata = container_of(sdata->bss, 68434e89507SJohannes Berg struct ieee80211_sub_if_data, 68534e89507SJohannes Berg u.ap); 68634e89507SJohannes Berg drv_sta_remove(local, sdata, &sta->sta); 68734e89507SJohannes Berg sdata = sta->sdata; 68834e89507SJohannes Berg } 68934e89507SJohannes Berg 690e64b3795SJohannes Berg /* 691e64b3795SJohannes Berg * At this point, after we wait for an RCU grace period, 692e64b3795SJohannes Berg * neither mac80211 nor the driver can reference this 693e64b3795SJohannes Berg * sta struct any more except by still existing timers 694e64b3795SJohannes Berg * associated with this station that we clean up below. 695e64b3795SJohannes Berg */ 696e64b3795SJohannes Berg synchronize_rcu(); 697e64b3795SJohannes Berg 69834e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH 699e64b3795SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 70034e89507SJohannes Berg mesh_accept_plinks_update(sdata); 70134e89507SJohannes Berg #endif 70234e89507SJohannes Berg 70334e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 7040fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr); 70534e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 70634e89507SJohannes Berg cancel_work_sync(&sta->drv_unblock_wk); 70734e89507SJohannes Berg 708ec15e68bSJouni Malinen cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL); 709ec15e68bSJouni Malinen 71034e89507SJohannes Berg rate_control_remove_sta_debugfs(sta); 71134e89507SJohannes Berg ieee80211_sta_debugfs_remove(sta); 71234e89507SJohannes Berg 71334e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH 71434e89507SJohannes Berg if (ieee80211_vif_is_mesh(&sta->sdata->vif)) { 71534e89507SJohannes Berg mesh_plink_deactivate(sta); 71634e89507SJohannes Berg del_timer_sync(&sta->plink_timer); 71734e89507SJohannes Berg } 71834e89507SJohannes Berg #endif 71934e89507SJohannes Berg 72034e89507SJohannes Berg while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) { 72134e89507SJohannes Berg local->total_ps_buffered--; 72234e89507SJohannes Berg dev_kfree_skb_any(skb); 72334e89507SJohannes Berg } 72434e89507SJohannes Berg 72534e89507SJohannes Berg while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) 72634e89507SJohannes Berg dev_kfree_skb_any(skb); 72734e89507SJohannes Berg 72834e89507SJohannes Berg __sta_info_free(local, sta); 72934e89507SJohannes Berg 73034e89507SJohannes Berg return 0; 73134e89507SJohannes Berg } 73234e89507SJohannes Berg 73334e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) 73434e89507SJohannes Berg { 73534e89507SJohannes Berg struct sta_info *sta; 73634e89507SJohannes Berg int ret; 73734e89507SJohannes Berg 73834e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 73934e89507SJohannes Berg sta = sta_info_get(sdata, addr); 74034e89507SJohannes Berg ret = __sta_info_destroy(sta); 74134e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 74234e89507SJohannes Berg 74334e89507SJohannes Berg return ret; 74434e89507SJohannes Berg } 74534e89507SJohannes Berg 74634e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, 74734e89507SJohannes Berg const u8 *addr) 74834e89507SJohannes Berg { 74934e89507SJohannes Berg struct sta_info *sta; 75034e89507SJohannes Berg int ret; 75134e89507SJohannes Berg 75234e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 75334e89507SJohannes Berg sta = sta_info_get_bss(sdata, addr); 75434e89507SJohannes Berg ret = __sta_info_destroy(sta); 75534e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 75634e89507SJohannes Berg 75734e89507SJohannes Berg return ret; 75834e89507SJohannes Berg } 759f0706e82SJiri Benc 760f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data) 761f0706e82SJiri Benc { 762f0706e82SJiri Benc struct ieee80211_local *local = (struct ieee80211_local *) data; 763f0706e82SJiri Benc struct sta_info *sta; 7643393a608SJuuso Oikarinen bool timer_needed = false; 765f0706e82SJiri Benc 766d0709a65SJohannes Berg rcu_read_lock(); 767d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) 7683393a608SJuuso Oikarinen if (sta_info_cleanup_expire_buffered(local, sta)) 7693393a608SJuuso Oikarinen timer_needed = true; 770d0709a65SJohannes Berg rcu_read_unlock(); 771f0706e82SJiri Benc 7725bb644a0SJohannes Berg if (local->quiescing) 7735bb644a0SJohannes Berg return; 7745bb644a0SJohannes Berg 7753393a608SJuuso Oikarinen if (!timer_needed) 7763393a608SJuuso Oikarinen return; 7773393a608SJuuso Oikarinen 77826d59535SJohannes Berg mod_timer(&local->sta_cleanup, 77926d59535SJohannes Berg round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 780f0706e82SJiri Benc } 781f0706e82SJiri Benc 782f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local) 783f0706e82SJiri Benc { 784d0709a65SJohannes Berg spin_lock_init(&local->sta_lock); 78534e89507SJohannes Berg mutex_init(&local->sta_mtx); 786f0706e82SJiri Benc INIT_LIST_HEAD(&local->sta_list); 78734e89507SJohannes Berg INIT_LIST_HEAD(&local->sta_pending_list); 78834e89507SJohannes Berg INIT_WORK(&local->sta_finish_work, sta_info_finish_work); 789f0706e82SJiri Benc 790b24b8a24SPavel Emelyanov setup_timer(&local->sta_cleanup, sta_info_cleanup, 791b24b8a24SPavel Emelyanov (unsigned long)local); 792f0706e82SJiri Benc } 793f0706e82SJiri Benc 794f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local) 795f0706e82SJiri Benc { 796f0706e82SJiri Benc del_timer(&local->sta_cleanup); 797be8755e1SMichael Wu sta_info_flush(local, NULL); 798f0706e82SJiri Benc } 799f0706e82SJiri Benc 800f0706e82SJiri Benc /** 801f0706e82SJiri Benc * sta_info_flush - flush matching STA entries from the STA table 80244213b5eSJohannes Berg * 80344213b5eSJohannes Berg * Returns the number of removed STA entries. 80444213b5eSJohannes Berg * 805f0706e82SJiri Benc * @local: local interface data 806d0709a65SJohannes Berg * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs 807f0706e82SJiri Benc */ 80844213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local, 809d0709a65SJohannes Berg struct ieee80211_sub_if_data *sdata) 810f0706e82SJiri Benc { 811f0706e82SJiri Benc struct sta_info *sta, *tmp; 81244213b5eSJohannes Berg int ret = 0; 813f0706e82SJiri Benc 814d0709a65SJohannes Berg might_sleep(); 815d0709a65SJohannes Berg 81634e89507SJohannes Berg mutex_lock(&local->sta_mtx); 817d0709a65SJohannes Berg 81834e89507SJohannes Berg sta_info_finish_pending(local); 81934e89507SJohannes Berg 82034e89507SJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 82134e89507SJohannes Berg if (!sdata || sdata == sta->sdata) 82234e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 82334e89507SJohannes Berg } 82434e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 82544213b5eSJohannes Berg 82644213b5eSJohannes Berg return ret; 827f0706e82SJiri Benc } 828dc6676b7SJohannes Berg 82924723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 83024723d1bSJohannes Berg unsigned long exp_time) 83124723d1bSJohannes Berg { 83224723d1bSJohannes Berg struct ieee80211_local *local = sdata->local; 83324723d1bSJohannes Berg struct sta_info *sta, *tmp; 83424723d1bSJohannes Berg 83534e89507SJohannes Berg mutex_lock(&local->sta_mtx); 83624723d1bSJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) 83724723d1bSJohannes Berg if (time_after(jiffies, sta->last_rx + exp_time)) { 83824723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG 8390c68ae26SJohannes Berg printk(KERN_DEBUG "%s: expiring inactive STA %pM\n", 84047846c9bSJohannes Berg sdata->name, sta->sta.addr); 84124723d1bSJohannes Berg #endif 84234e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 84324723d1bSJohannes Berg } 84434e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 84524723d1bSJohannes Berg } 84617741cdcSJohannes Berg 847686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 848686b9cb9SBen Greear const u8 *addr, 849686b9cb9SBen Greear const u8 *localaddr) 85017741cdcSJohannes Berg { 851abe60632SJohannes Berg struct sta_info *sta, *nxt; 85217741cdcSJohannes Berg 853686b9cb9SBen Greear /* 854686b9cb9SBen Greear * Just return a random station if localaddr is NULL 855686b9cb9SBen Greear * ... first in list. 856686b9cb9SBen Greear */ 857f7c65594SJohannes Berg for_each_sta_info(hw_to_local(hw), addr, sta, nxt) { 858686b9cb9SBen Greear if (localaddr && 859686b9cb9SBen Greear compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0) 860686b9cb9SBen Greear continue; 861f7c65594SJohannes Berg if (!sta->uploaded) 862f7c65594SJohannes Berg return NULL; 86317741cdcSJohannes Berg return &sta->sta; 864f7c65594SJohannes Berg } 865f7c65594SJohannes Berg 866abe60632SJohannes Berg return NULL; 86717741cdcSJohannes Berg } 868686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); 8695ed176e1SJohannes Berg 8705ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, 8715ed176e1SJohannes Berg const u8 *addr) 8725ed176e1SJohannes Berg { 873f7c65594SJohannes Berg struct sta_info *sta; 8745ed176e1SJohannes Berg 8755ed176e1SJohannes Berg if (!vif) 8765ed176e1SJohannes Berg return NULL; 8775ed176e1SJohannes Berg 878f7c65594SJohannes Berg sta = sta_info_get_bss(vif_to_sdata(vif), addr); 879f7c65594SJohannes Berg if (!sta) 880f7c65594SJohannes Berg return NULL; 8815ed176e1SJohannes Berg 882f7c65594SJohannes Berg if (!sta->uploaded) 883f7c65594SJohannes Berg return NULL; 884f7c65594SJohannes Berg 885f7c65594SJohannes Berg return &sta->sta; 8865ed176e1SJohannes Berg } 88717741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta); 888af818581SJohannes Berg 88950a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta) 89050a9432dSJohannes Berg { 89150a9432dSJohannes Berg struct sta_info *sta = _sta; 89250a9432dSJohannes Berg 89350a9432dSJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER | WLAN_STA_PS_STA); 89450a9432dSJohannes Berg } 89550a9432dSJohannes Berg 896af818581SJohannes Berg /* powersave support code */ 897af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) 898af818581SJohannes Berg { 899af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 900af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 901af818581SJohannes Berg int sent, buffered; 902af818581SJohannes Berg 903dcf55fb5SFelix Fietkau clear_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF); 904d057e5a3SArik Nemtsov if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS)) 90512375ef9SJohannes Berg drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 906af818581SJohannes Berg 907af818581SJohannes Berg if (!skb_queue_empty(&sta->ps_tx_buf)) 908af818581SJohannes Berg sta_info_clear_tim_bit(sta); 909af818581SJohannes Berg 910af818581SJohannes Berg /* Send all buffered frames to the station */ 911af818581SJohannes Berg sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered); 91250a9432dSJohannes Berg buffered = ieee80211_add_pending_skbs_fn(local, &sta->ps_tx_buf, 91350a9432dSJohannes Berg clear_sta_ps_flags, sta); 914af818581SJohannes Berg sent += buffered; 915af818581SJohannes Berg local->total_ps_buffered -= buffered; 916af818581SJohannes Berg 917af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 918af818581SJohannes Berg printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames " 91947846c9bSJohannes Berg "since STA not sleeping anymore\n", sdata->name, 920af818581SJohannes Berg sta->sta.addr, sta->sta.aid, sent - buffered, buffered); 921af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 922af818581SJohannes Berg } 923af818581SJohannes Berg 924af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 925af818581SJohannes Berg { 926af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 927af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 928af818581SJohannes Berg struct sk_buff *skb; 929af818581SJohannes Berg int no_pending_pkts; 930af818581SJohannes Berg 931af818581SJohannes Berg skb = skb_dequeue(&sta->tx_filtered); 932af818581SJohannes Berg if (!skb) { 933af818581SJohannes Berg skb = skb_dequeue(&sta->ps_tx_buf); 934af818581SJohannes Berg if (skb) 935af818581SJohannes Berg local->total_ps_buffered--; 936af818581SJohannes Berg } 937af818581SJohannes Berg no_pending_pkts = skb_queue_empty(&sta->tx_filtered) && 938af818581SJohannes Berg skb_queue_empty(&sta->ps_tx_buf); 939af818581SJohannes Berg 940af818581SJohannes Berg if (skb) { 941af818581SJohannes Berg struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 942af818581SJohannes Berg struct ieee80211_hdr *hdr = 943af818581SJohannes Berg (struct ieee80211_hdr *) skb->data; 944af818581SJohannes Berg 945af818581SJohannes Berg /* 946af818581SJohannes Berg * Tell TX path to send this frame even though the STA may 947af818581SJohannes Berg * still remain is PS mode after this frame exchange. 948af818581SJohannes Berg */ 949af818581SJohannes Berg info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE; 950af818581SJohannes Berg 951af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 952af818581SJohannes Berg printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n", 953af818581SJohannes Berg sta->sta.addr, sta->sta.aid, 954af818581SJohannes Berg skb_queue_len(&sta->ps_tx_buf)); 955af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 956af818581SJohannes Berg 957af818581SJohannes Berg /* Use MoreData flag to indicate whether there are more 958af818581SJohannes Berg * buffered frames for this STA */ 959af818581SJohannes Berg if (no_pending_pkts) 960af818581SJohannes Berg hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 961af818581SJohannes Berg else 962af818581SJohannes Berg hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA); 963af818581SJohannes Berg 964af818581SJohannes Berg ieee80211_add_pending_skb(local, skb); 965af818581SJohannes Berg 966af818581SJohannes Berg if (no_pending_pkts) 967af818581SJohannes Berg sta_info_clear_tim_bit(sta); 968af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 969af818581SJohannes Berg } else { 970af818581SJohannes Berg /* 971af818581SJohannes Berg * FIXME: This can be the result of a race condition between 972af818581SJohannes Berg * us expiring a frame and the station polling for it. 973af818581SJohannes Berg * Should we send it a null-func frame indicating we 974af818581SJohannes Berg * have nothing buffered for it? 975af818581SJohannes Berg */ 976af818581SJohannes Berg printk(KERN_DEBUG "%s: STA %pM sent PS Poll even " 977af818581SJohannes Berg "though there are no buffered frames for it\n", 97847846c9bSJohannes Berg sdata->name, sta->sta.addr); 979af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 980af818581SJohannes Berg } 981af818581SJohannes Berg } 982af818581SJohannes Berg 983af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 984af818581SJohannes Berg struct ieee80211_sta *pubsta, bool block) 985af818581SJohannes Berg { 986af818581SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 987af818581SJohannes Berg 988b5878a2dSJohannes Berg trace_api_sta_block_awake(sta->local, pubsta, block); 989b5878a2dSJohannes Berg 990af818581SJohannes Berg if (block) 991af818581SJohannes Berg set_sta_flags(sta, WLAN_STA_PS_DRIVER); 99250a9432dSJohannes Berg else if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) 993af818581SJohannes Berg ieee80211_queue_work(hw, &sta->drv_unblock_wk); 994af818581SJohannes Berg } 995af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake); 996dcf55fb5SFelix Fietkau 997dcf55fb5SFelix Fietkau void ieee80211_sta_set_tim(struct ieee80211_sta *pubsta) 998dcf55fb5SFelix Fietkau { 999dcf55fb5SFelix Fietkau struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1000dcf55fb5SFelix Fietkau 1001dcf55fb5SFelix Fietkau set_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF); 1002dcf55fb5SFelix Fietkau sta_info_set_tim_bit(sta); 1003dcf55fb5SFelix Fietkau } 1004dcf55fb5SFelix Fietkau EXPORT_SYMBOL(ieee80211_sta_set_tim); 1005