1f0706e82SJiri Benc /* 2f0706e82SJiri Benc * Copyright 2002-2005, Instant802 Networks, Inc. 3f0706e82SJiri Benc * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> 4f0706e82SJiri Benc * 5f0706e82SJiri Benc * This program is free software; you can redistribute it and/or modify 6f0706e82SJiri Benc * it under the terms of the GNU General Public License version 2 as 7f0706e82SJiri Benc * published by the Free Software Foundation. 8f0706e82SJiri Benc */ 9f0706e82SJiri Benc 10f0706e82SJiri Benc #include <linux/module.h> 11f0706e82SJiri Benc #include <linux/init.h> 12f0706e82SJiri Benc #include <linux/netdevice.h> 13f0706e82SJiri Benc #include <linux/types.h> 14f0706e82SJiri Benc #include <linux/slab.h> 15f0706e82SJiri Benc #include <linux/skbuff.h> 16f0706e82SJiri Benc #include <linux/if_arp.h> 170d174406SJohannes Berg #include <linux/timer.h> 18d0709a65SJohannes Berg #include <linux/rtnetlink.h> 19f0706e82SJiri Benc 20f0706e82SJiri Benc #include <net/mac80211.h> 21f0706e82SJiri Benc #include "ieee80211_i.h" 2224487981SJohannes Berg #include "driver-ops.h" 232c8dccc7SJohannes Berg #include "rate.h" 24f0706e82SJiri Benc #include "sta_info.h" 25e9f207f0SJiri Benc #include "debugfs_sta.h" 26ee385855SLuis Carlos Cobo #include "mesh.h" 27f0706e82SJiri Benc 28d0709a65SJohannes Berg /** 29d0709a65SJohannes Berg * DOC: STA information lifetime rules 30d0709a65SJohannes Berg * 31d0709a65SJohannes Berg * STA info structures (&struct sta_info) are managed in a hash table 32d0709a65SJohannes Berg * for faster lookup and a list for iteration. They are managed using 33d0709a65SJohannes Berg * RCU, i.e. access to the list and hash table is protected by RCU. 34d0709a65SJohannes Berg * 3534e89507SJohannes Berg * Upon allocating a STA info structure with sta_info_alloc(), the caller 3634e89507SJohannes Berg * owns that structure. It must then insert it into the hash table using 3734e89507SJohannes Berg * either sta_info_insert() or sta_info_insert_rcu(); only in the latter 3834e89507SJohannes Berg * case (which acquires an rcu read section but must not be called from 3934e89507SJohannes Berg * within one) will the pointer still be valid after the call. Note that 4034e89507SJohannes Berg * the caller may not do much with the STA info before inserting it, in 4134e89507SJohannes Berg * particular, it may not start any mesh peer link management or add 4234e89507SJohannes Berg * encryption keys. 4393e5deb1SJohannes Berg * 4493e5deb1SJohannes Berg * When the insertion fails (sta_info_insert()) returns non-zero), the 4593e5deb1SJohannes Berg * structure will have been freed by sta_info_insert()! 46d0709a65SJohannes Berg * 4734e89507SJohannes Berg * Station entries are added by mac80211 when you establish a link with a 487e189a12SLuis R. Rodriguez * peer. This means different things for the different type of interfaces 497e189a12SLuis R. Rodriguez * we support. For a regular station this mean we add the AP sta when we 5025985edcSLucas De Marchi * receive an association response from the AP. For IBSS this occurs when 5134e89507SJohannes Berg * get to know about a peer on the same IBSS. For WDS we add the sta for 5225985edcSLucas De Marchi * the peer immediately upon device open. When using AP mode we add stations 5334e89507SJohannes Berg * for each respective station upon request from userspace through nl80211. 547e189a12SLuis R. Rodriguez * 5534e89507SJohannes Berg * In order to remove a STA info structure, various sta_info_destroy_*() 5634e89507SJohannes Berg * calls are available. 57d0709a65SJohannes Berg * 5834e89507SJohannes Berg * There is no concept of ownership on a STA entry, each structure is 5934e89507SJohannes Berg * owned by the global hash table/list until it is removed. All users of 6034e89507SJohannes Berg * the structure need to be RCU protected so that the structure won't be 6134e89507SJohannes Berg * freed before they are done using it. 62d0709a65SJohannes Berg */ 63f0706e82SJiri Benc 64f0706e82SJiri Benc /* Caller must hold local->sta_lock */ 65be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local, 66f0706e82SJiri Benc struct sta_info *sta) 67f0706e82SJiri Benc { 68f0706e82SJiri Benc struct sta_info *s; 69f0706e82SJiri Benc 7040b275b6SJohannes Berg s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)], 7140b275b6SJohannes Berg lockdep_is_held(&local->sta_lock)); 72f0706e82SJiri Benc if (!s) 73be8755e1SMichael Wu return -ENOENT; 74be8755e1SMichael Wu if (s == sta) { 7517741cdcSJohannes Berg rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], 76d0709a65SJohannes Berg s->hnext); 77be8755e1SMichael Wu return 0; 78f0706e82SJiri Benc } 79f0706e82SJiri Benc 8040b275b6SJohannes Berg while (rcu_access_pointer(s->hnext) && 8140b275b6SJohannes Berg rcu_access_pointer(s->hnext) != sta) 8240b275b6SJohannes Berg s = rcu_dereference_protected(s->hnext, 8340b275b6SJohannes Berg lockdep_is_held(&local->sta_lock)); 8440b275b6SJohannes Berg if (rcu_access_pointer(s->hnext)) { 85d0709a65SJohannes Berg rcu_assign_pointer(s->hnext, sta->hnext); 86be8755e1SMichael Wu return 0; 87f0706e82SJiri Benc } 88f0706e82SJiri Benc 89be8755e1SMichael Wu return -ENOENT; 90f0706e82SJiri Benc } 91f0706e82SJiri Benc 92d0709a65SJohannes Berg /* protected by RCU */ 93abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata, 94abe60632SJohannes Berg const u8 *addr) 9543ba7e95SJohannes Berg { 96abe60632SJohannes Berg struct ieee80211_local *local = sdata->local; 9743ba7e95SJohannes Berg struct sta_info *sta; 9843ba7e95SJohannes Berg 990379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1000379185bSJohannes Berg lockdep_is_held(&local->sta_lock) || 1010379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 10243ba7e95SJohannes Berg while (sta) { 1032a33bee2SGuy Eilam if (sta->sdata == sdata && !sta->dummy && 1042a33bee2SGuy Eilam memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 1052a33bee2SGuy Eilam break; 1062a33bee2SGuy Eilam sta = rcu_dereference_check(sta->hnext, 1072a33bee2SGuy Eilam lockdep_is_held(&local->sta_lock) || 1082a33bee2SGuy Eilam lockdep_is_held(&local->sta_mtx)); 1092a33bee2SGuy Eilam } 1102a33bee2SGuy Eilam return sta; 1112a33bee2SGuy Eilam } 1122a33bee2SGuy Eilam 1132a33bee2SGuy Eilam /* get a station info entry even if it is a dummy station*/ 1142a33bee2SGuy Eilam struct sta_info *sta_info_get_rx(struct ieee80211_sub_if_data *sdata, 1152a33bee2SGuy Eilam const u8 *addr) 1162a33bee2SGuy Eilam { 1172a33bee2SGuy Eilam struct ieee80211_local *local = sdata->local; 1182a33bee2SGuy Eilam struct sta_info *sta; 1192a33bee2SGuy Eilam 1202a33bee2SGuy Eilam sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1212a33bee2SGuy Eilam lockdep_is_held(&local->sta_lock) || 1222a33bee2SGuy Eilam lockdep_is_held(&local->sta_mtx)); 1232a33bee2SGuy Eilam while (sta) { 124abe60632SJohannes Berg if (sta->sdata == sdata && 125abe60632SJohannes Berg memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 12643ba7e95SJohannes Berg break; 1270379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1280379185bSJohannes Berg lockdep_is_held(&local->sta_lock) || 1290379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 13043ba7e95SJohannes Berg } 13143ba7e95SJohannes Berg return sta; 13243ba7e95SJohannes Berg } 13343ba7e95SJohannes Berg 1340e5ded5aSFelix Fietkau /* 1350e5ded5aSFelix Fietkau * Get sta info either from the specified interface 1360e5ded5aSFelix Fietkau * or from one of its vlans 1370e5ded5aSFelix Fietkau */ 1380e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata, 1390e5ded5aSFelix Fietkau const u8 *addr) 1400e5ded5aSFelix Fietkau { 1410e5ded5aSFelix Fietkau struct ieee80211_local *local = sdata->local; 1420e5ded5aSFelix Fietkau struct sta_info *sta; 1430e5ded5aSFelix Fietkau 1440379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1450379185bSJohannes Berg lockdep_is_held(&local->sta_lock) || 1460379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1470e5ded5aSFelix Fietkau while (sta) { 1480e5ded5aSFelix Fietkau if ((sta->sdata == sdata || 149a2c1e3daSJohannes Berg (sta->sdata->bss && sta->sdata->bss == sdata->bss)) && 1502a33bee2SGuy Eilam !sta->dummy && 1512a33bee2SGuy Eilam memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 1522a33bee2SGuy Eilam break; 1532a33bee2SGuy Eilam sta = rcu_dereference_check(sta->hnext, 1542a33bee2SGuy Eilam lockdep_is_held(&local->sta_lock) || 1552a33bee2SGuy Eilam lockdep_is_held(&local->sta_mtx)); 1562a33bee2SGuy Eilam } 1572a33bee2SGuy Eilam return sta; 1582a33bee2SGuy Eilam } 1592a33bee2SGuy Eilam 1602a33bee2SGuy Eilam /* 1612a33bee2SGuy Eilam * Get sta info either from the specified interface 1622a33bee2SGuy Eilam * or from one of its vlans (including dummy stations) 1632a33bee2SGuy Eilam */ 1642a33bee2SGuy Eilam struct sta_info *sta_info_get_bss_rx(struct ieee80211_sub_if_data *sdata, 1652a33bee2SGuy Eilam const u8 *addr) 1662a33bee2SGuy Eilam { 1672a33bee2SGuy Eilam struct ieee80211_local *local = sdata->local; 1682a33bee2SGuy Eilam struct sta_info *sta; 1692a33bee2SGuy Eilam 1702a33bee2SGuy Eilam sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1712a33bee2SGuy Eilam lockdep_is_held(&local->sta_lock) || 1722a33bee2SGuy Eilam lockdep_is_held(&local->sta_mtx)); 1732a33bee2SGuy Eilam while (sta) { 1742a33bee2SGuy Eilam if ((sta->sdata == sdata || 1752a33bee2SGuy Eilam (sta->sdata->bss && sta->sdata->bss == sdata->bss)) && 1760e5ded5aSFelix Fietkau memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 1770e5ded5aSFelix Fietkau break; 1780379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1790379185bSJohannes Berg lockdep_is_held(&local->sta_lock) || 1800379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1810e5ded5aSFelix Fietkau } 1820e5ded5aSFelix Fietkau return sta; 1830e5ded5aSFelix Fietkau } 1840e5ded5aSFelix Fietkau 1853b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, 1863b53fde8SJohannes Berg int idx) 187ee385855SLuis Carlos Cobo { 1883b53fde8SJohannes Berg struct ieee80211_local *local = sdata->local; 189ee385855SLuis Carlos Cobo struct sta_info *sta; 190ee385855SLuis Carlos Cobo int i = 0; 191ee385855SLuis Carlos Cobo 192d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) { 1933b53fde8SJohannes Berg if (sdata != sta->sdata) 1942a8ca29aSLuis Carlos Cobo continue; 195ee385855SLuis Carlos Cobo if (i < idx) { 196ee385855SLuis Carlos Cobo ++i; 197ee385855SLuis Carlos Cobo continue; 198ee385855SLuis Carlos Cobo } 1992a8ca29aSLuis Carlos Cobo return sta; 200ee385855SLuis Carlos Cobo } 201ee385855SLuis Carlos Cobo 202ee385855SLuis Carlos Cobo return NULL; 203ee385855SLuis Carlos Cobo } 204f0706e82SJiri Benc 20593e5deb1SJohannes Berg /** 20693e5deb1SJohannes Berg * __sta_info_free - internal STA free helper 20793e5deb1SJohannes Berg * 2086ef307bcSRandy Dunlap * @local: pointer to the global information 20993e5deb1SJohannes Berg * @sta: STA info to free 21093e5deb1SJohannes Berg * 21193e5deb1SJohannes Berg * This function must undo everything done by sta_info_alloc() 21293e5deb1SJohannes Berg * that may happen before sta_info_insert(). 21393e5deb1SJohannes Berg */ 21493e5deb1SJohannes Berg static void __sta_info_free(struct ieee80211_local *local, 21593e5deb1SJohannes Berg struct sta_info *sta) 21693e5deb1SJohannes Berg { 217af65cd96SJohannes Berg if (sta->rate_ctrl) { 2184b7679a5SJohannes Berg rate_control_free_sta(sta); 21993e5deb1SJohannes Berg rate_control_put(sta->rate_ctrl); 220af65cd96SJohannes Berg } 22193e5deb1SJohannes Berg 22293e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 2230fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr); 22493e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 22593e5deb1SJohannes Berg 22693e5deb1SJohannes Berg kfree(sta); 22793e5deb1SJohannes Berg } 22893e5deb1SJohannes Berg 229d0709a65SJohannes Berg /* Caller must hold local->sta_lock */ 230d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local, 231d0709a65SJohannes Berg struct sta_info *sta) 232f0706e82SJiri Benc { 23317741cdcSJohannes Berg sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)]; 23417741cdcSJohannes Berg rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta); 235f0706e82SJiri Benc } 236f0706e82SJiri Benc 237af818581SJohannes Berg static void sta_unblock(struct work_struct *wk) 238af818581SJohannes Berg { 239af818581SJohannes Berg struct sta_info *sta; 240af818581SJohannes Berg 241af818581SJohannes Berg sta = container_of(wk, struct sta_info, drv_unblock_wk); 242af818581SJohannes Berg 243af818581SJohannes Berg if (sta->dead) 244af818581SJohannes Berg return; 245af818581SJohannes Berg 246af818581SJohannes Berg if (!test_sta_flags(sta, WLAN_STA_PS_STA)) 247af818581SJohannes Berg ieee80211_sta_ps_deliver_wakeup(sta); 24850a9432dSJohannes Berg else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL)) { 24950a9432dSJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER); 250af818581SJohannes Berg ieee80211_sta_ps_deliver_poll_response(sta); 25150a9432dSJohannes Berg } else 25250a9432dSJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER); 253af818581SJohannes Berg } 254af818581SJohannes Berg 255af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local, 256af65cd96SJohannes Berg struct sta_info *sta, gfp_t gfp) 257af65cd96SJohannes Berg { 258af65cd96SJohannes Berg if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) 259af65cd96SJohannes Berg return 0; 260af65cd96SJohannes Berg 261af65cd96SJohannes Berg sta->rate_ctrl = rate_control_get(local->rate_ctrl); 262af65cd96SJohannes Berg sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, 263af65cd96SJohannes Berg &sta->sta, gfp); 264af65cd96SJohannes Berg if (!sta->rate_ctrl_priv) { 265af65cd96SJohannes Berg rate_control_put(sta->rate_ctrl); 266af65cd96SJohannes Berg return -ENOMEM; 267af65cd96SJohannes Berg } 268af65cd96SJohannes Berg 269af65cd96SJohannes Berg return 0; 270af65cd96SJohannes Berg } 271af65cd96SJohannes Berg 27273651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, 27373651ee6SJohannes Berg u8 *addr, gfp_t gfp) 274f0706e82SJiri Benc { 275d0709a65SJohannes Berg struct ieee80211_local *local = sdata->local; 276f0706e82SJiri Benc struct sta_info *sta; 277ebe27c91SMohammed Shafi Shajakhan struct timespec uptime; 27816c5f15cSRon Rindjunsky int i; 279f0706e82SJiri Benc 28017741cdcSJohannes Berg sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp); 281f0706e82SJiri Benc if (!sta) 28273651ee6SJohannes Berg return NULL; 283f0706e82SJiri Benc 28407346f81SJohannes Berg spin_lock_init(&sta->lock); 2855a9f7b04SJohannes Berg spin_lock_init(&sta->flaglock); 286af818581SJohannes Berg INIT_WORK(&sta->drv_unblock_wk, sta_unblock); 28767c282c0SJohannes Berg INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work); 288a93e3644SJohannes Berg mutex_init(&sta->ampdu_mlme.mtx); 28907346f81SJohannes Berg 29017741cdcSJohannes Berg memcpy(sta->sta.addr, addr, ETH_ALEN); 291d0709a65SJohannes Berg sta->local = local; 292d0709a65SJohannes Berg sta->sdata = sdata; 2938bc8aecdSFelix Fietkau sta->last_rx = jiffies; 294f0706e82SJiri Benc 295ebe27c91SMohammed Shafi Shajakhan do_posix_clock_monotonic_gettime(&uptime); 296ebe27c91SMohammed Shafi Shajakhan sta->last_connected = uptime.tv_sec; 297541a45a1SBruno Randolf ewma_init(&sta->avg_signal, 1024, 8); 298541a45a1SBruno Randolf 299af65cd96SJohannes Berg if (sta_prepare_rate_control(local, sta, gfp)) { 300f0706e82SJiri Benc kfree(sta); 30173651ee6SJohannes Berg return NULL; 302f0706e82SJiri Benc } 303f0706e82SJiri Benc 30416c5f15cSRon Rindjunsky for (i = 0; i < STA_TID_NUM; i++) { 305a622ab72SJohannes Berg /* 306a622ab72SJohannes Berg * timer_to_tid must be initialized with identity mapping 307a622ab72SJohannes Berg * to enable session_timer's data differentiation. See 308a622ab72SJohannes Berg * sta_rx_agg_session_timer_expired for usage. 309a622ab72SJohannes Berg */ 31016c5f15cSRon Rindjunsky sta->timer_to_tid[i] = i; 31116c5f15cSRon Rindjunsky } 312f0706e82SJiri Benc skb_queue_head_init(&sta->ps_tx_buf); 313f0706e82SJiri Benc skb_queue_head_init(&sta->tx_filtered); 31473651ee6SJohannes Berg 315cccaec98SSenthil Balasubramanian for (i = 0; i < NUM_RX_DATA_QUEUES; i++) 3164be929beSAlexey Dobriyan sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); 317cccaec98SSenthil Balasubramanian 31873651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 3190fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr); 32073651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 32173651ee6SJohannes Berg 32203e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH 32357cf8043SJavier Cardona sta->plink_state = NL80211_PLINK_LISTEN; 32403e4497eSJohannes Berg init_timer(&sta->plink_timer); 32503e4497eSJohannes Berg #endif 32603e4497eSJohannes Berg 32773651ee6SJohannes Berg return sta; 32873651ee6SJohannes Berg } 32973651ee6SJohannes Berg 3302a33bee2SGuy Eilam static int sta_info_finish_insert(struct sta_info *sta, 3312a33bee2SGuy Eilam bool async, bool dummy_reinsert) 33273651ee6SJohannes Berg { 33373651ee6SJohannes Berg struct ieee80211_local *local = sta->local; 33473651ee6SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 33598b62183SJohannes Berg struct station_info sinfo; 33673651ee6SJohannes Berg unsigned long flags; 33793e5deb1SJohannes Berg int err = 0; 33873651ee6SJohannes Berg 33946a5ebafSJohannes Berg lockdep_assert_held(&local->sta_mtx); 34034e89507SJohannes Berg 3412a33bee2SGuy Eilam if (!sta->dummy || dummy_reinsert) { 34234e89507SJohannes Berg /* notify driver */ 34334e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 34434e89507SJohannes Berg sdata = container_of(sdata->bss, 34534e89507SJohannes Berg struct ieee80211_sub_if_data, 34634e89507SJohannes Berg u.ap); 34734e89507SJohannes Berg err = drv_sta_add(local, sdata, &sta->sta); 34834e89507SJohannes Berg if (err) { 34934e89507SJohannes Berg if (!async) 35034e89507SJohannes Berg return err; 3512a33bee2SGuy Eilam printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to " 3522a33bee2SGuy Eilam "driver (%d) - keeping it anyway.\n", 35334e89507SJohannes Berg sdata->name, sta->sta.addr, err); 35434e89507SJohannes Berg } else { 35534e89507SJohannes Berg sta->uploaded = true; 35634e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 35734e89507SJohannes Berg if (async) 3580fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, 3590fb9a9ecSJoe Perches "Finished adding IBSS STA %pM\n", 3600fb9a9ecSJoe Perches sta->sta.addr); 36134e89507SJohannes Berg #endif 36234e89507SJohannes Berg } 36334e89507SJohannes Berg 36434e89507SJohannes Berg sdata = sta->sdata; 3652a33bee2SGuy Eilam } 36634e89507SJohannes Berg 3672a33bee2SGuy Eilam if (!dummy_reinsert) { 36834e89507SJohannes Berg if (!async) { 36934e89507SJohannes Berg local->num_sta++; 37034e89507SJohannes Berg local->sta_generation++; 37134e89507SJohannes Berg smp_mb(); 37234e89507SJohannes Berg 37334e89507SJohannes Berg /* make the station visible */ 37434e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 37534e89507SJohannes Berg sta_info_hash_add(local, sta); 37634e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 37734e89507SJohannes Berg } 37834e89507SJohannes Berg 37934e89507SJohannes Berg list_add(&sta->list, &local->sta_list); 3802a33bee2SGuy Eilam } else { 3812a33bee2SGuy Eilam sta->dummy = false; 3822a33bee2SGuy Eilam } 38334e89507SJohannes Berg 3842a33bee2SGuy Eilam if (!sta->dummy) { 38534e89507SJohannes Berg ieee80211_sta_debugfs_add(sta); 38634e89507SJohannes Berg rate_control_add_sta_debugfs(sta); 38734e89507SJohannes Berg 388f612cedfSJouni Malinen memset(&sinfo, 0, sizeof(sinfo)); 38934e89507SJohannes Berg sinfo.filled = 0; 39034e89507SJohannes Berg sinfo.generation = local->sta_generation; 39134e89507SJohannes Berg cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL); 3922a33bee2SGuy Eilam } 39334e89507SJohannes Berg 39434e89507SJohannes Berg return 0; 39534e89507SJohannes Berg } 39634e89507SJohannes Berg 39734e89507SJohannes Berg static void sta_info_finish_pending(struct ieee80211_local *local) 39834e89507SJohannes Berg { 39934e89507SJohannes Berg struct sta_info *sta; 40034e89507SJohannes Berg unsigned long flags; 40134e89507SJohannes Berg 40234e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 40334e89507SJohannes Berg while (!list_empty(&local->sta_pending_list)) { 40434e89507SJohannes Berg sta = list_first_entry(&local->sta_pending_list, 40534e89507SJohannes Berg struct sta_info, list); 40634e89507SJohannes Berg list_del(&sta->list); 40734e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 40834e89507SJohannes Berg 4092a33bee2SGuy Eilam sta_info_finish_insert(sta, true, false); 41034e89507SJohannes Berg 41134e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 41234e89507SJohannes Berg } 41334e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 41434e89507SJohannes Berg } 41534e89507SJohannes Berg 41634e89507SJohannes Berg static void sta_info_finish_work(struct work_struct *work) 41734e89507SJohannes Berg { 41834e89507SJohannes Berg struct ieee80211_local *local = 41934e89507SJohannes Berg container_of(work, struct ieee80211_local, sta_finish_work); 42034e89507SJohannes Berg 42134e89507SJohannes Berg mutex_lock(&local->sta_mtx); 42234e89507SJohannes Berg sta_info_finish_pending(local); 42334e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 42434e89507SJohannes Berg } 42534e89507SJohannes Berg 4268c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta) 42734e89507SJohannes Berg { 42834e89507SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 42934e89507SJohannes Berg 43003e4497eSJohannes Berg /* 43103e4497eSJohannes Berg * Can't be a WARN_ON because it can be triggered through a race: 43203e4497eSJohannes Berg * something inserts a STA (on one CPU) without holding the RTNL 43303e4497eSJohannes Berg * and another CPU turns off the net device. 43403e4497eSJohannes Berg */ 4358c71df7aSGuy Eilam if (unlikely(!ieee80211_sdata_running(sdata))) 4368c71df7aSGuy Eilam return -ENETDOWN; 43703e4497eSJohannes Berg 43847846c9bSJohannes Berg if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 || 4398c71df7aSGuy Eilam is_multicast_ether_addr(sta->sta.addr))) 4408c71df7aSGuy Eilam return -EINVAL; 4418c71df7aSGuy Eilam 4428c71df7aSGuy Eilam return 0; 44393e5deb1SJohannes Berg } 44444213b5eSJohannes Berg 4458c71df7aSGuy Eilam static int sta_info_insert_ibss(struct sta_info *sta) __acquires(RCU) 4468c71df7aSGuy Eilam { 4478c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 4488c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 4498c71df7aSGuy Eilam unsigned long flags; 4508c71df7aSGuy Eilam 451d0709a65SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 45243ba7e95SJohannes Berg /* check if STA exists already */ 4532a33bee2SGuy Eilam if (sta_info_get_bss_rx(sdata, sta->sta.addr)) { 454d0709a65SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 45534e89507SJohannes Berg rcu_read_lock(); 4568c71df7aSGuy Eilam return -EEXIST; 45743ba7e95SJohannes Berg } 45834e89507SJohannes Berg 459f0706e82SJiri Benc local->num_sta++; 46034e89507SJohannes Berg local->sta_generation++; 46134e89507SJohannes Berg smp_mb(); 462f0706e82SJiri Benc sta_info_hash_add(local, sta); 46332bfd35dSJohannes Berg 46434e89507SJohannes Berg list_add_tail(&sta->list, &local->sta_pending_list); 46532bfd35dSJohannes Berg 46634e89507SJohannes Berg rcu_read_lock(); 46734e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 46834e89507SJohannes Berg 46934e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 4700fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n", 4710fb9a9ecSJoe Perches sta->sta.addr); 47234e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 47334e89507SJohannes Berg 47434e89507SJohannes Berg ieee80211_queue_work(&local->hw, &local->sta_finish_work); 47534e89507SJohannes Berg 47634e89507SJohannes Berg return 0; 47734e89507SJohannes Berg } 47834e89507SJohannes Berg 47934e89507SJohannes Berg /* 4808c71df7aSGuy Eilam * should be called with sta_mtx locked 4818c71df7aSGuy Eilam * this function replaces the mutex lock 4828c71df7aSGuy Eilam * with a RCU lock 4838c71df7aSGuy Eilam */ 4848c71df7aSGuy Eilam static int sta_info_insert_non_ibss(struct sta_info *sta) __acquires(RCU) 4858c71df7aSGuy Eilam { 4868c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 4878c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 4888c71df7aSGuy Eilam unsigned long flags; 4892a33bee2SGuy Eilam struct sta_info *exist_sta; 4902a33bee2SGuy Eilam bool dummy_reinsert = false; 4918c71df7aSGuy Eilam int err = 0; 4928c71df7aSGuy Eilam 4938c71df7aSGuy Eilam lockdep_assert_held(&local->sta_mtx); 4948c71df7aSGuy Eilam 4958c71df7aSGuy Eilam /* 49634e89507SJohannes Berg * On first glance, this will look racy, because the code 4978c71df7aSGuy Eilam * in this function, which inserts a station with sleeping, 49834e89507SJohannes Berg * unlocks the sta_lock between checking existence in the 49934e89507SJohannes Berg * hash table and inserting into it. 50034e89507SJohannes Berg * 50134e89507SJohannes Berg * However, it is not racy against itself because it keeps 5028c71df7aSGuy Eilam * the mutex locked. 50334e89507SJohannes Berg */ 50434e89507SJohannes Berg 50534e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 5062a33bee2SGuy Eilam /* 5072a33bee2SGuy Eilam * check if STA exists already. 5082a33bee2SGuy Eilam * only accept a scenario of a second call to sta_info_insert_non_ibss 5092a33bee2SGuy Eilam * with a dummy station entry that was inserted earlier 5102a33bee2SGuy Eilam * in that case - assume that the dummy station flag should 5112a33bee2SGuy Eilam * be removed. 5122a33bee2SGuy Eilam */ 5132a33bee2SGuy Eilam exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr); 5142a33bee2SGuy Eilam if (exist_sta) { 5152a33bee2SGuy Eilam if (exist_sta == sta && sta->dummy) { 5162a33bee2SGuy Eilam dummy_reinsert = true; 5172a33bee2SGuy Eilam } else { 51834e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 51938a679a5SJouni Malinen mutex_unlock(&local->sta_mtx); 52034e89507SJohannes Berg rcu_read_lock(); 5218c71df7aSGuy Eilam return -EEXIST; 52234e89507SJohannes Berg } 5232a33bee2SGuy Eilam } 52434e89507SJohannes Berg 52534e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 52634e89507SJohannes Berg 5272a33bee2SGuy Eilam err = sta_info_finish_insert(sta, false, dummy_reinsert); 52834e89507SJohannes Berg if (err) { 52934e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 53034e89507SJohannes Berg rcu_read_lock(); 5318c71df7aSGuy Eilam return err; 53232bfd35dSJohannes Berg } 533d0709a65SJohannes Berg 534f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 5352a33bee2SGuy Eilam wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n", 5362a33bee2SGuy Eilam sta->dummy ? "dummy " : "", sta->sta.addr); 537f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 538f0706e82SJiri Benc 53934e89507SJohannes Berg /* move reference to rcu-protected */ 54034e89507SJohannes Berg rcu_read_lock(); 54134e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 542e9f207f0SJiri Benc 54373651ee6SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 54473651ee6SJohannes Berg mesh_accept_plinks_update(sdata); 54573651ee6SJohannes Berg 54673651ee6SJohannes Berg return 0; 5478c71df7aSGuy Eilam } 5488c71df7aSGuy Eilam 5498c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) 5508c71df7aSGuy Eilam { 5518c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 5528c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 5538c71df7aSGuy Eilam int err = 0; 5548c71df7aSGuy Eilam 5558c71df7aSGuy Eilam err = sta_info_insert_check(sta); 5568c71df7aSGuy Eilam if (err) { 5578c71df7aSGuy Eilam rcu_read_lock(); 5588c71df7aSGuy Eilam goto out_free; 5598c71df7aSGuy Eilam } 5608c71df7aSGuy Eilam 5618c71df7aSGuy Eilam /* 5628c71df7aSGuy Eilam * In ad-hoc mode, we sometimes need to insert stations 5638c71df7aSGuy Eilam * from tasklet context from the RX path. To avoid races, 5648c71df7aSGuy Eilam * always do so in that case -- see the comment below. 5658c71df7aSGuy Eilam */ 5668c71df7aSGuy Eilam if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { 5678c71df7aSGuy Eilam err = sta_info_insert_ibss(sta); 5688c71df7aSGuy Eilam if (err) 5698c71df7aSGuy Eilam goto out_free; 5708c71df7aSGuy Eilam 5718c71df7aSGuy Eilam return 0; 5728c71df7aSGuy Eilam } 5738c71df7aSGuy Eilam 5748c71df7aSGuy Eilam /* 5758c71df7aSGuy Eilam * It might seem that the function called below is in race against 5768c71df7aSGuy Eilam * the function call above that atomically inserts the station... That, 5778c71df7aSGuy Eilam * however, is not true because the above code can only 5788c71df7aSGuy Eilam * be invoked for IBSS interfaces, and the below code will 5798c71df7aSGuy Eilam * not be -- and the two do not race against each other as 5808c71df7aSGuy Eilam * the hash table also keys off the interface. 5818c71df7aSGuy Eilam */ 5828c71df7aSGuy Eilam 5838c71df7aSGuy Eilam might_sleep(); 5848c71df7aSGuy Eilam 5858c71df7aSGuy Eilam mutex_lock(&local->sta_mtx); 5868c71df7aSGuy Eilam 5878c71df7aSGuy Eilam err = sta_info_insert_non_ibss(sta); 5888c71df7aSGuy Eilam if (err) 5898c71df7aSGuy Eilam goto out_free; 5908c71df7aSGuy Eilam 5918c71df7aSGuy Eilam return 0; 59293e5deb1SJohannes Berg out_free: 59393e5deb1SJohannes Berg BUG_ON(!err); 59493e5deb1SJohannes Berg __sta_info_free(local, sta); 59593e5deb1SJohannes Berg return err; 596f0706e82SJiri Benc } 597f0706e82SJiri Benc 59834e89507SJohannes Berg int sta_info_insert(struct sta_info *sta) 59934e89507SJohannes Berg { 60034e89507SJohannes Berg int err = sta_info_insert_rcu(sta); 60134e89507SJohannes Berg 60234e89507SJohannes Berg rcu_read_unlock(); 60334e89507SJohannes Berg 60434e89507SJohannes Berg return err; 60534e89507SJohannes Berg } 60634e89507SJohannes Berg 6072a33bee2SGuy Eilam /* Caller must hold sta->local->sta_mtx */ 6082a33bee2SGuy Eilam int sta_info_reinsert(struct sta_info *sta) 6092a33bee2SGuy Eilam { 6102a33bee2SGuy Eilam struct ieee80211_local *local = sta->local; 6112a33bee2SGuy Eilam int err = 0; 6122a33bee2SGuy Eilam 6132a33bee2SGuy Eilam err = sta_info_insert_check(sta); 6142a33bee2SGuy Eilam if (err) { 6152a33bee2SGuy Eilam mutex_unlock(&local->sta_mtx); 6162a33bee2SGuy Eilam return err; 6172a33bee2SGuy Eilam } 6182a33bee2SGuy Eilam 6192a33bee2SGuy Eilam might_sleep(); 6202a33bee2SGuy Eilam 6212a33bee2SGuy Eilam err = sta_info_insert_non_ibss(sta); 6222a33bee2SGuy Eilam rcu_read_unlock(); 6232a33bee2SGuy Eilam return err; 6242a33bee2SGuy Eilam } 6252a33bee2SGuy Eilam 626004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid) 627004c872eSJohannes Berg { 628004c872eSJohannes Berg /* 629004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 630004c872eSJohannes Berg * so this line may not be changed to use the __set_bit() format. 631004c872eSJohannes Berg */ 632004c872eSJohannes Berg bss->tim[aid / 8] |= (1 << (aid % 8)); 633004c872eSJohannes Berg } 634004c872eSJohannes Berg 635004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid) 636004c872eSJohannes Berg { 637004c872eSJohannes Berg /* 638004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 639004c872eSJohannes Berg * so this line may not be changed to use the __clear_bit() format. 640004c872eSJohannes Berg */ 641004c872eSJohannes Berg bss->tim[aid / 8] &= ~(1 << (aid % 8)); 642004c872eSJohannes Berg } 643004c872eSJohannes Berg 644004c872eSJohannes Berg static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss, 645004c872eSJohannes Berg struct sta_info *sta) 646004c872eSJohannes Berg { 6473e122be0SJohannes Berg BUG_ON(!bss); 6483e122be0SJohannes Berg 64917741cdcSJohannes Berg __bss_tim_set(bss, sta->sta.aid); 6503e122be0SJohannes Berg 651d0709a65SJohannes Berg if (sta->local->ops->set_tim) { 652d0709a65SJohannes Berg sta->local->tim_in_locked_section = true; 65324487981SJohannes Berg drv_set_tim(sta->local, &sta->sta, true); 654d0709a65SJohannes Berg sta->local->tim_in_locked_section = false; 655d0709a65SJohannes Berg } 656004c872eSJohannes Berg } 657004c872eSJohannes Berg 658004c872eSJohannes Berg void sta_info_set_tim_bit(struct sta_info *sta) 659004c872eSJohannes Berg { 660d0709a65SJohannes Berg unsigned long flags; 661004c872eSJohannes Berg 6623e122be0SJohannes Berg BUG_ON(!sta->sdata->bss); 6633e122be0SJohannes Berg 664d0709a65SJohannes Berg spin_lock_irqsave(&sta->local->sta_lock, flags); 665d0709a65SJohannes Berg __sta_info_set_tim_bit(sta->sdata->bss, sta); 666d0709a65SJohannes Berg spin_unlock_irqrestore(&sta->local->sta_lock, flags); 667004c872eSJohannes Berg } 668004c872eSJohannes Berg 669004c872eSJohannes Berg static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss, 670004c872eSJohannes Berg struct sta_info *sta) 671004c872eSJohannes Berg { 6723e122be0SJohannes Berg BUG_ON(!bss); 6733e122be0SJohannes Berg 67417741cdcSJohannes Berg __bss_tim_clear(bss, sta->sta.aid); 6753e122be0SJohannes Berg 676d0709a65SJohannes Berg if (sta->local->ops->set_tim) { 677d0709a65SJohannes Berg sta->local->tim_in_locked_section = true; 67824487981SJohannes Berg drv_set_tim(sta->local, &sta->sta, false); 679d0709a65SJohannes Berg sta->local->tim_in_locked_section = false; 680d0709a65SJohannes Berg } 681004c872eSJohannes Berg } 682004c872eSJohannes Berg 683004c872eSJohannes Berg void sta_info_clear_tim_bit(struct sta_info *sta) 684004c872eSJohannes Berg { 685d0709a65SJohannes Berg unsigned long flags; 686004c872eSJohannes Berg 6873e122be0SJohannes Berg BUG_ON(!sta->sdata->bss); 6883e122be0SJohannes Berg 689d0709a65SJohannes Berg spin_lock_irqsave(&sta->local->sta_lock, flags); 690d0709a65SJohannes Berg __sta_info_clear_tim_bit(sta->sdata->bss, sta); 691d0709a65SJohannes Berg spin_unlock_irqrestore(&sta->local->sta_lock, flags); 692004c872eSJohannes Berg } 693004c872eSJohannes Berg 694cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb) 695f0706e82SJiri Benc { 696e039fa4aSJohannes Berg struct ieee80211_tx_info *info; 697f0706e82SJiri Benc int timeout; 698f0706e82SJiri Benc 699f0706e82SJiri Benc if (!skb) 700cd0b8d89SJohannes Berg return false; 701f0706e82SJiri Benc 702e039fa4aSJohannes Berg info = IEEE80211_SKB_CB(skb); 703f0706e82SJiri Benc 704f0706e82SJiri Benc /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 70557c4d7b4SJohannes Berg timeout = (sta->listen_interval * 70657c4d7b4SJohannes Berg sta->sdata->vif.bss_conf.beacon_int * 70757c4d7b4SJohannes Berg 32 / 15625) * HZ; 708f0706e82SJiri Benc if (timeout < STA_TX_BUFFER_EXPIRE) 709f0706e82SJiri Benc timeout = STA_TX_BUFFER_EXPIRE; 710e039fa4aSJohannes Berg return time_after(jiffies, info->control.jiffies + timeout); 711f0706e82SJiri Benc } 712f0706e82SJiri Benc 713f0706e82SJiri Benc 7143393a608SJuuso Oikarinen static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 715f0706e82SJiri Benc struct sta_info *sta) 716f0706e82SJiri Benc { 717f0706e82SJiri Benc unsigned long flags; 718f0706e82SJiri Benc struct sk_buff *skb; 719f0706e82SJiri Benc 720f0706e82SJiri Benc for (;;) { 721f0706e82SJiri Benc spin_lock_irqsave(&sta->ps_tx_buf.lock, flags); 722f0706e82SJiri Benc skb = skb_peek(&sta->ps_tx_buf); 72357c4d7b4SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 724f0706e82SJiri Benc skb = __skb_dequeue(&sta->ps_tx_buf); 725836341a7SJohannes Berg else 726f0706e82SJiri Benc skb = NULL; 727f0706e82SJiri Benc spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags); 728f0706e82SJiri Benc 729836341a7SJohannes Berg if (!skb) 730836341a7SJohannes Berg break; 731836341a7SJohannes Berg 732f0706e82SJiri Benc local->total_ps_buffered--; 733f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 7340c68ae26SJohannes Berg printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n", 7350c68ae26SJohannes Berg sta->sta.addr); 736f4ea83ddSJohannes Berg #endif 737f0706e82SJiri Benc dev_kfree_skb(skb); 738836341a7SJohannes Berg 739dcf55fb5SFelix Fietkau if (skb_queue_empty(&sta->ps_tx_buf) && 740dcf55fb5SFelix Fietkau !test_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF)) 741004c872eSJohannes Berg sta_info_clear_tim_bit(sta); 742f0706e82SJiri Benc } 7433393a608SJuuso Oikarinen 744cd0b8d89SJohannes Berg return !skb_queue_empty(&sta->ps_tx_buf); 745f0706e82SJiri Benc } 746f0706e82SJiri Benc 74734e89507SJohannes Berg static int __must_check __sta_info_destroy(struct sta_info *sta) 74834e89507SJohannes Berg { 74934e89507SJohannes Berg struct ieee80211_local *local; 75034e89507SJohannes Berg struct ieee80211_sub_if_data *sdata; 75134e89507SJohannes Berg struct sk_buff *skb; 75234e89507SJohannes Berg unsigned long flags; 753e31b8213SJohannes Berg int ret, i; 75434e89507SJohannes Berg 75534e89507SJohannes Berg might_sleep(); 75634e89507SJohannes Berg 75734e89507SJohannes Berg if (!sta) 75834e89507SJohannes Berg return -ENOENT; 75934e89507SJohannes Berg 76034e89507SJohannes Berg local = sta->local; 76134e89507SJohannes Berg sdata = sta->sdata; 76234e89507SJohannes Berg 763098a6070SJohannes Berg /* 764098a6070SJohannes Berg * Before removing the station from the driver and 765098a6070SJohannes Berg * rate control, it might still start new aggregation 766098a6070SJohannes Berg * sessions -- block that to make sure the tear-down 767098a6070SJohannes Berg * will be sufficient. 768098a6070SJohannes Berg */ 769098a6070SJohannes Berg set_sta_flags(sta, WLAN_STA_BLOCK_BA); 77053f73c09SJohannes Berg ieee80211_sta_tear_down_BA_sessions(sta, true); 771098a6070SJohannes Berg 77234e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 77334e89507SJohannes Berg ret = sta_info_hash_del(local, sta); 77434e89507SJohannes Berg /* this might still be the pending list ... which is fine */ 77534e89507SJohannes Berg if (!ret) 77634e89507SJohannes Berg list_del(&sta->list); 77734e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 77834e89507SJohannes Berg if (ret) 77934e89507SJohannes Berg return ret; 78034e89507SJohannes Berg 7818cb23153SJohannes Berg mutex_lock(&local->key_mtx); 782e31b8213SJohannes Berg for (i = 0; i < NUM_DEFAULT_KEYS; i++) 78340b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i])); 784e31b8213SJohannes Berg if (sta->ptk) 78540b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->ptk)); 7868cb23153SJohannes Berg mutex_unlock(&local->key_mtx); 78734e89507SJohannes Berg 78834e89507SJohannes Berg sta->dead = true; 78934e89507SJohannes Berg 79034e89507SJohannes Berg if (test_and_clear_sta_flags(sta, 79134e89507SJohannes Berg WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) { 79234e89507SJohannes Berg BUG_ON(!sdata->bss); 79334e89507SJohannes Berg 79434e89507SJohannes Berg atomic_dec(&sdata->bss->num_sta_ps); 7954bae7d97SJohannes Berg sta_info_clear_tim_bit(sta); 79634e89507SJohannes Berg } 79734e89507SJohannes Berg 79834e89507SJohannes Berg local->num_sta--; 79934e89507SJohannes Berg local->sta_generation++; 80034e89507SJohannes Berg 80134e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 80234e89507SJohannes Berg rcu_assign_pointer(sdata->u.vlan.sta, NULL); 80334e89507SJohannes Berg 80434e89507SJohannes Berg if (sta->uploaded) { 80534e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 80634e89507SJohannes Berg sdata = container_of(sdata->bss, 80734e89507SJohannes Berg struct ieee80211_sub_if_data, 80834e89507SJohannes Berg u.ap); 80934e89507SJohannes Berg drv_sta_remove(local, sdata, &sta->sta); 81034e89507SJohannes Berg sdata = sta->sdata; 81134e89507SJohannes Berg } 81234e89507SJohannes Berg 813e64b3795SJohannes Berg /* 814e64b3795SJohannes Berg * At this point, after we wait for an RCU grace period, 815e64b3795SJohannes Berg * neither mac80211 nor the driver can reference this 816e64b3795SJohannes Berg * sta struct any more except by still existing timers 817e64b3795SJohannes Berg * associated with this station that we clean up below. 818e64b3795SJohannes Berg */ 819e64b3795SJohannes Berg synchronize_rcu(); 820e64b3795SJohannes Berg 82134e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH 822e64b3795SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 82334e89507SJohannes Berg mesh_accept_plinks_update(sdata); 82434e89507SJohannes Berg #endif 82534e89507SJohannes Berg 82634e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 8270fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr); 82834e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 82934e89507SJohannes Berg cancel_work_sync(&sta->drv_unblock_wk); 83034e89507SJohannes Berg 831ec15e68bSJouni Malinen cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL); 832ec15e68bSJouni Malinen 83334e89507SJohannes Berg rate_control_remove_sta_debugfs(sta); 83434e89507SJohannes Berg ieee80211_sta_debugfs_remove(sta); 83534e89507SJohannes Berg 83634e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH 83734e89507SJohannes Berg if (ieee80211_vif_is_mesh(&sta->sdata->vif)) { 83834e89507SJohannes Berg mesh_plink_deactivate(sta); 83934e89507SJohannes Berg del_timer_sync(&sta->plink_timer); 84034e89507SJohannes Berg } 84134e89507SJohannes Berg #endif 84234e89507SJohannes Berg 84334e89507SJohannes Berg while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) { 84434e89507SJohannes Berg local->total_ps_buffered--; 84534e89507SJohannes Berg dev_kfree_skb_any(skb); 84634e89507SJohannes Berg } 84734e89507SJohannes Berg 84834e89507SJohannes Berg while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) 84934e89507SJohannes Berg dev_kfree_skb_any(skb); 85034e89507SJohannes Berg 85134e89507SJohannes Berg __sta_info_free(local, sta); 85234e89507SJohannes Berg 85334e89507SJohannes Berg return 0; 85434e89507SJohannes Berg } 85534e89507SJohannes Berg 85634e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) 85734e89507SJohannes Berg { 85834e89507SJohannes Berg struct sta_info *sta; 85934e89507SJohannes Berg int ret; 86034e89507SJohannes Berg 86134e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 8622a33bee2SGuy Eilam sta = sta_info_get_rx(sdata, addr); 86334e89507SJohannes Berg ret = __sta_info_destroy(sta); 86434e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 86534e89507SJohannes Berg 86634e89507SJohannes Berg return ret; 86734e89507SJohannes Berg } 86834e89507SJohannes Berg 86934e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, 87034e89507SJohannes Berg const u8 *addr) 87134e89507SJohannes Berg { 87234e89507SJohannes Berg struct sta_info *sta; 87334e89507SJohannes Berg int ret; 87434e89507SJohannes Berg 87534e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 8762a33bee2SGuy Eilam sta = sta_info_get_bss_rx(sdata, addr); 87734e89507SJohannes Berg ret = __sta_info_destroy(sta); 87834e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 87934e89507SJohannes Berg 88034e89507SJohannes Berg return ret; 88134e89507SJohannes Berg } 882f0706e82SJiri Benc 883f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data) 884f0706e82SJiri Benc { 885f0706e82SJiri Benc struct ieee80211_local *local = (struct ieee80211_local *) data; 886f0706e82SJiri Benc struct sta_info *sta; 8873393a608SJuuso Oikarinen bool timer_needed = false; 888f0706e82SJiri Benc 889d0709a65SJohannes Berg rcu_read_lock(); 890d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) 8913393a608SJuuso Oikarinen if (sta_info_cleanup_expire_buffered(local, sta)) 8923393a608SJuuso Oikarinen timer_needed = true; 893d0709a65SJohannes Berg rcu_read_unlock(); 894f0706e82SJiri Benc 8955bb644a0SJohannes Berg if (local->quiescing) 8965bb644a0SJohannes Berg return; 8975bb644a0SJohannes Berg 8983393a608SJuuso Oikarinen if (!timer_needed) 8993393a608SJuuso Oikarinen return; 9003393a608SJuuso Oikarinen 90126d59535SJohannes Berg mod_timer(&local->sta_cleanup, 90226d59535SJohannes Berg round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 903f0706e82SJiri Benc } 904f0706e82SJiri Benc 905f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local) 906f0706e82SJiri Benc { 907d0709a65SJohannes Berg spin_lock_init(&local->sta_lock); 90834e89507SJohannes Berg mutex_init(&local->sta_mtx); 909f0706e82SJiri Benc INIT_LIST_HEAD(&local->sta_list); 91034e89507SJohannes Berg INIT_LIST_HEAD(&local->sta_pending_list); 91134e89507SJohannes Berg INIT_WORK(&local->sta_finish_work, sta_info_finish_work); 912f0706e82SJiri Benc 913b24b8a24SPavel Emelyanov setup_timer(&local->sta_cleanup, sta_info_cleanup, 914b24b8a24SPavel Emelyanov (unsigned long)local); 915f0706e82SJiri Benc } 916f0706e82SJiri Benc 917f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local) 918f0706e82SJiri Benc { 919f0706e82SJiri Benc del_timer(&local->sta_cleanup); 920be8755e1SMichael Wu sta_info_flush(local, NULL); 921f0706e82SJiri Benc } 922f0706e82SJiri Benc 923f0706e82SJiri Benc /** 924f0706e82SJiri Benc * sta_info_flush - flush matching STA entries from the STA table 92544213b5eSJohannes Berg * 92644213b5eSJohannes Berg * Returns the number of removed STA entries. 92744213b5eSJohannes Berg * 928f0706e82SJiri Benc * @local: local interface data 929d0709a65SJohannes Berg * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs 930f0706e82SJiri Benc */ 93144213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local, 932d0709a65SJohannes Berg struct ieee80211_sub_if_data *sdata) 933f0706e82SJiri Benc { 934f0706e82SJiri Benc struct sta_info *sta, *tmp; 93544213b5eSJohannes Berg int ret = 0; 936f0706e82SJiri Benc 937d0709a65SJohannes Berg might_sleep(); 938d0709a65SJohannes Berg 93934e89507SJohannes Berg mutex_lock(&local->sta_mtx); 940d0709a65SJohannes Berg 94134e89507SJohannes Berg sta_info_finish_pending(local); 94234e89507SJohannes Berg 94334e89507SJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 94434e89507SJohannes Berg if (!sdata || sdata == sta->sdata) 94534e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 94634e89507SJohannes Berg } 94734e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 94844213b5eSJohannes Berg 94944213b5eSJohannes Berg return ret; 950f0706e82SJiri Benc } 951dc6676b7SJohannes Berg 95224723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 95324723d1bSJohannes Berg unsigned long exp_time) 95424723d1bSJohannes Berg { 95524723d1bSJohannes Berg struct ieee80211_local *local = sdata->local; 95624723d1bSJohannes Berg struct sta_info *sta, *tmp; 95724723d1bSJohannes Berg 95834e89507SJohannes Berg mutex_lock(&local->sta_mtx); 95924723d1bSJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) 96024723d1bSJohannes Berg if (time_after(jiffies, sta->last_rx + exp_time)) { 96124723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG 9620c68ae26SJohannes Berg printk(KERN_DEBUG "%s: expiring inactive STA %pM\n", 96347846c9bSJohannes Berg sdata->name, sta->sta.addr); 96424723d1bSJohannes Berg #endif 96534e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 96624723d1bSJohannes Berg } 96734e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 96824723d1bSJohannes Berg } 96917741cdcSJohannes Berg 970686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 971686b9cb9SBen Greear const u8 *addr, 972686b9cb9SBen Greear const u8 *localaddr) 97317741cdcSJohannes Berg { 974abe60632SJohannes Berg struct sta_info *sta, *nxt; 97517741cdcSJohannes Berg 976686b9cb9SBen Greear /* 977686b9cb9SBen Greear * Just return a random station if localaddr is NULL 978686b9cb9SBen Greear * ... first in list. 979686b9cb9SBen Greear */ 980f7c65594SJohannes Berg for_each_sta_info(hw_to_local(hw), addr, sta, nxt) { 981686b9cb9SBen Greear if (localaddr && 982686b9cb9SBen Greear compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0) 983686b9cb9SBen Greear continue; 984f7c65594SJohannes Berg if (!sta->uploaded) 985f7c65594SJohannes Berg return NULL; 98617741cdcSJohannes Berg return &sta->sta; 987f7c65594SJohannes Berg } 988f7c65594SJohannes Berg 989abe60632SJohannes Berg return NULL; 99017741cdcSJohannes Berg } 991686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); 9925ed176e1SJohannes Berg 9935ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, 9945ed176e1SJohannes Berg const u8 *addr) 9955ed176e1SJohannes Berg { 996f7c65594SJohannes Berg struct sta_info *sta; 9975ed176e1SJohannes Berg 9985ed176e1SJohannes Berg if (!vif) 9995ed176e1SJohannes Berg return NULL; 10005ed176e1SJohannes Berg 1001f7c65594SJohannes Berg sta = sta_info_get_bss(vif_to_sdata(vif), addr); 1002f7c65594SJohannes Berg if (!sta) 1003f7c65594SJohannes Berg return NULL; 10045ed176e1SJohannes Berg 1005f7c65594SJohannes Berg if (!sta->uploaded) 1006f7c65594SJohannes Berg return NULL; 1007f7c65594SJohannes Berg 1008f7c65594SJohannes Berg return &sta->sta; 10095ed176e1SJohannes Berg } 101017741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta); 1011af818581SJohannes Berg 101250a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta) 101350a9432dSJohannes Berg { 101450a9432dSJohannes Berg struct sta_info *sta = _sta; 101550a9432dSJohannes Berg 101650a9432dSJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER | WLAN_STA_PS_STA); 101750a9432dSJohannes Berg } 101850a9432dSJohannes Berg 1019af818581SJohannes Berg /* powersave support code */ 1020af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) 1021af818581SJohannes Berg { 1022af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1023af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 1024af818581SJohannes Berg int sent, buffered; 1025af818581SJohannes Berg 1026dcf55fb5SFelix Fietkau clear_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF); 1027d057e5a3SArik Nemtsov if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS)) 102812375ef9SJohannes Berg drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 1029af818581SJohannes Berg 1030af818581SJohannes Berg if (!skb_queue_empty(&sta->ps_tx_buf)) 1031af818581SJohannes Berg sta_info_clear_tim_bit(sta); 1032af818581SJohannes Berg 1033af818581SJohannes Berg /* Send all buffered frames to the station */ 1034af818581SJohannes Berg sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered); 103550a9432dSJohannes Berg buffered = ieee80211_add_pending_skbs_fn(local, &sta->ps_tx_buf, 103650a9432dSJohannes Berg clear_sta_ps_flags, sta); 1037af818581SJohannes Berg sent += buffered; 1038af818581SJohannes Berg local->total_ps_buffered -= buffered; 1039af818581SJohannes Berg 1040af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 1041af818581SJohannes Berg printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames " 104247846c9bSJohannes Berg "since STA not sleeping anymore\n", sdata->name, 1043af818581SJohannes Berg sta->sta.addr, sta->sta.aid, sent - buffered, buffered); 1044af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 1045af818581SJohannes Berg } 1046af818581SJohannes Berg 1047af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 1048af818581SJohannes Berg { 1049af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1050af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 1051af818581SJohannes Berg struct sk_buff *skb; 1052af818581SJohannes Berg int no_pending_pkts; 1053af818581SJohannes Berg 1054af818581SJohannes Berg skb = skb_dequeue(&sta->tx_filtered); 1055af818581SJohannes Berg if (!skb) { 1056af818581SJohannes Berg skb = skb_dequeue(&sta->ps_tx_buf); 1057af818581SJohannes Berg if (skb) 1058af818581SJohannes Berg local->total_ps_buffered--; 1059af818581SJohannes Berg } 1060af818581SJohannes Berg no_pending_pkts = skb_queue_empty(&sta->tx_filtered) && 1061af818581SJohannes Berg skb_queue_empty(&sta->ps_tx_buf); 1062af818581SJohannes Berg 1063af818581SJohannes Berg if (skb) { 1064af818581SJohannes Berg struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 1065af818581SJohannes Berg struct ieee80211_hdr *hdr = 1066af818581SJohannes Berg (struct ieee80211_hdr *) skb->data; 1067af818581SJohannes Berg 1068af818581SJohannes Berg /* 1069af818581SJohannes Berg * Tell TX path to send this frame even though the STA may 1070af818581SJohannes Berg * still remain is PS mode after this frame exchange. 1071af818581SJohannes Berg */ 1072af818581SJohannes Berg info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE; 1073af818581SJohannes Berg 1074af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 1075af818581SJohannes Berg printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n", 1076af818581SJohannes Berg sta->sta.addr, sta->sta.aid, 1077af818581SJohannes Berg skb_queue_len(&sta->ps_tx_buf)); 1078af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 1079af818581SJohannes Berg 1080af818581SJohannes Berg /* Use MoreData flag to indicate whether there are more 1081af818581SJohannes Berg * buffered frames for this STA */ 1082af818581SJohannes Berg if (no_pending_pkts) 1083af818581SJohannes Berg hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 1084af818581SJohannes Berg else 1085af818581SJohannes Berg hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA); 1086af818581SJohannes Berg 1087af818581SJohannes Berg ieee80211_add_pending_skb(local, skb); 1088af818581SJohannes Berg 1089af818581SJohannes Berg if (no_pending_pkts) 1090af818581SJohannes Berg sta_info_clear_tim_bit(sta); 1091af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 1092af818581SJohannes Berg } else { 1093af818581SJohannes Berg /* 1094af818581SJohannes Berg * FIXME: This can be the result of a race condition between 1095af818581SJohannes Berg * us expiring a frame and the station polling for it. 1096af818581SJohannes Berg * Should we send it a null-func frame indicating we 1097af818581SJohannes Berg * have nothing buffered for it? 1098af818581SJohannes Berg */ 1099af818581SJohannes Berg printk(KERN_DEBUG "%s: STA %pM sent PS Poll even " 1100af818581SJohannes Berg "though there are no buffered frames for it\n", 110147846c9bSJohannes Berg sdata->name, sta->sta.addr); 1102af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 1103af818581SJohannes Berg } 1104af818581SJohannes Berg } 1105af818581SJohannes Berg 1106af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 1107af818581SJohannes Berg struct ieee80211_sta *pubsta, bool block) 1108af818581SJohannes Berg { 1109af818581SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1110af818581SJohannes Berg 1111b5878a2dSJohannes Berg trace_api_sta_block_awake(sta->local, pubsta, block); 1112b5878a2dSJohannes Berg 1113af818581SJohannes Berg if (block) 1114af818581SJohannes Berg set_sta_flags(sta, WLAN_STA_PS_DRIVER); 111550a9432dSJohannes Berg else if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) 1116af818581SJohannes Berg ieee80211_queue_work(hw, &sta->drv_unblock_wk); 1117af818581SJohannes Berg } 1118af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake); 1119dcf55fb5SFelix Fietkau 1120*042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, 1121*042ec453SJohannes Berg u8 tid, bool buffered) 1122dcf55fb5SFelix Fietkau { 1123dcf55fb5SFelix Fietkau struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1124dcf55fb5SFelix Fietkau 1125*042ec453SJohannes Berg if (!buffered) 1126*042ec453SJohannes Berg return; 1127*042ec453SJohannes Berg 1128dcf55fb5SFelix Fietkau set_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF); 1129dcf55fb5SFelix Fietkau sta_info_set_tim_bit(sta); 1130dcf55fb5SFelix Fietkau } 1131*042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered); 1132