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 644*c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta) 645004c872eSJohannes Berg { 646*c868cb35SJohannes Berg struct ieee80211_local *local = sta->local; 647*c868cb35SJohannes Berg struct ieee80211_if_ap *bss = sta->sdata->bss; 648d0709a65SJohannes Berg unsigned long flags; 649*c868cb35SJohannes Berg bool have_data = false; 650004c872eSJohannes Berg 651*c868cb35SJohannes Berg if (WARN_ON_ONCE(!sta->sdata->bss)) 652*c868cb35SJohannes Berg return; 6533e122be0SJohannes Berg 654*c868cb35SJohannes Berg /* No need to do anything if the driver does all */ 655*c868cb35SJohannes Berg if (local->hw.flags & IEEE80211_HW_AP_LINK_PS) 656*c868cb35SJohannes Berg return; 657004c872eSJohannes Berg 658*c868cb35SJohannes Berg if (sta->dead) 659*c868cb35SJohannes Berg goto done; 6603e122be0SJohannes Berg 661*c868cb35SJohannes Berg have_data = test_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF) || 662*c868cb35SJohannes Berg !skb_queue_empty(&sta->tx_filtered) || 663*c868cb35SJohannes Berg !skb_queue_empty(&sta->ps_tx_buf); 664*c868cb35SJohannes Berg 665*c868cb35SJohannes Berg done: 666*c868cb35SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 667*c868cb35SJohannes Berg 668*c868cb35SJohannes Berg if (have_data) 669*c868cb35SJohannes Berg __bss_tim_set(bss, sta->sta.aid); 670*c868cb35SJohannes Berg else 67117741cdcSJohannes Berg __bss_tim_clear(bss, sta->sta.aid); 6723e122be0SJohannes Berg 673*c868cb35SJohannes Berg if (local->ops->set_tim) { 674*c868cb35SJohannes Berg local->tim_in_locked_section = true; 675*c868cb35SJohannes Berg drv_set_tim(local, &sta->sta, have_data); 676*c868cb35SJohannes Berg local->tim_in_locked_section = false; 677004c872eSJohannes Berg } 678004c872eSJohannes Berg 679*c868cb35SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 680004c872eSJohannes Berg } 681004c872eSJohannes Berg 682cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb) 683f0706e82SJiri Benc { 684e039fa4aSJohannes Berg struct ieee80211_tx_info *info; 685f0706e82SJiri Benc int timeout; 686f0706e82SJiri Benc 687f0706e82SJiri Benc if (!skb) 688cd0b8d89SJohannes Berg return false; 689f0706e82SJiri Benc 690e039fa4aSJohannes Berg info = IEEE80211_SKB_CB(skb); 691f0706e82SJiri Benc 692f0706e82SJiri Benc /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 69357c4d7b4SJohannes Berg timeout = (sta->listen_interval * 69457c4d7b4SJohannes Berg sta->sdata->vif.bss_conf.beacon_int * 69557c4d7b4SJohannes Berg 32 / 15625) * HZ; 696f0706e82SJiri Benc if (timeout < STA_TX_BUFFER_EXPIRE) 697f0706e82SJiri Benc timeout = STA_TX_BUFFER_EXPIRE; 698e039fa4aSJohannes Berg return time_after(jiffies, info->control.jiffies + timeout); 699f0706e82SJiri Benc } 700f0706e82SJiri Benc 701f0706e82SJiri Benc 7023393a608SJuuso Oikarinen static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 703f0706e82SJiri Benc struct sta_info *sta) 704f0706e82SJiri Benc { 705f0706e82SJiri Benc unsigned long flags; 706f0706e82SJiri Benc struct sk_buff *skb; 707f0706e82SJiri Benc 708*c868cb35SJohannes Berg /* This is only necessary for stations on BSS interfaces */ 709*c868cb35SJohannes Berg if (!sta->sdata->bss) 710*c868cb35SJohannes Berg return false; 711*c868cb35SJohannes Berg 712f0706e82SJiri Benc for (;;) { 713f0706e82SJiri Benc spin_lock_irqsave(&sta->ps_tx_buf.lock, flags); 714f0706e82SJiri Benc skb = skb_peek(&sta->ps_tx_buf); 71557c4d7b4SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 716f0706e82SJiri Benc skb = __skb_dequeue(&sta->ps_tx_buf); 717836341a7SJohannes Berg else 718f0706e82SJiri Benc skb = NULL; 719f0706e82SJiri Benc spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags); 720f0706e82SJiri Benc 721836341a7SJohannes Berg if (!skb) 722836341a7SJohannes Berg break; 723836341a7SJohannes Berg 724f0706e82SJiri Benc local->total_ps_buffered--; 725f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 7260c68ae26SJohannes Berg printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n", 7270c68ae26SJohannes Berg sta->sta.addr); 728f4ea83ddSJohannes Berg #endif 729f0706e82SJiri Benc dev_kfree_skb(skb); 730836341a7SJohannes Berg 731*c868cb35SJohannes Berg /* if the queue is now empty recalc TIM bit */ 732*c868cb35SJohannes Berg if (skb_queue_empty(&sta->ps_tx_buf)) 733*c868cb35SJohannes Berg sta_info_recalc_tim(sta); 734f0706e82SJiri Benc } 7353393a608SJuuso Oikarinen 736cd0b8d89SJohannes Berg return !skb_queue_empty(&sta->ps_tx_buf); 737f0706e82SJiri Benc } 738f0706e82SJiri Benc 73934e89507SJohannes Berg static int __must_check __sta_info_destroy(struct sta_info *sta) 74034e89507SJohannes Berg { 74134e89507SJohannes Berg struct ieee80211_local *local; 74234e89507SJohannes Berg struct ieee80211_sub_if_data *sdata; 74334e89507SJohannes Berg unsigned long flags; 744e31b8213SJohannes Berg int ret, i; 74534e89507SJohannes Berg 74634e89507SJohannes Berg might_sleep(); 74734e89507SJohannes Berg 74834e89507SJohannes Berg if (!sta) 74934e89507SJohannes Berg return -ENOENT; 75034e89507SJohannes Berg 75134e89507SJohannes Berg local = sta->local; 75234e89507SJohannes Berg sdata = sta->sdata; 75334e89507SJohannes Berg 754098a6070SJohannes Berg /* 755098a6070SJohannes Berg * Before removing the station from the driver and 756098a6070SJohannes Berg * rate control, it might still start new aggregation 757098a6070SJohannes Berg * sessions -- block that to make sure the tear-down 758098a6070SJohannes Berg * will be sufficient. 759098a6070SJohannes Berg */ 760098a6070SJohannes Berg set_sta_flags(sta, WLAN_STA_BLOCK_BA); 76153f73c09SJohannes Berg ieee80211_sta_tear_down_BA_sessions(sta, true); 762098a6070SJohannes Berg 76334e89507SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 76434e89507SJohannes Berg ret = sta_info_hash_del(local, sta); 76534e89507SJohannes Berg /* this might still be the pending list ... which is fine */ 76634e89507SJohannes Berg if (!ret) 76734e89507SJohannes Berg list_del(&sta->list); 76834e89507SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 76934e89507SJohannes Berg if (ret) 77034e89507SJohannes Berg return ret; 77134e89507SJohannes Berg 7728cb23153SJohannes Berg mutex_lock(&local->key_mtx); 773e31b8213SJohannes Berg for (i = 0; i < NUM_DEFAULT_KEYS; i++) 77440b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i])); 775e31b8213SJohannes Berg if (sta->ptk) 77640b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->ptk)); 7778cb23153SJohannes Berg mutex_unlock(&local->key_mtx); 77834e89507SJohannes Berg 77934e89507SJohannes Berg sta->dead = true; 78034e89507SJohannes Berg 78134e89507SJohannes Berg if (test_and_clear_sta_flags(sta, 78234e89507SJohannes Berg WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) { 78334e89507SJohannes Berg BUG_ON(!sdata->bss); 78434e89507SJohannes Berg 78534e89507SJohannes Berg atomic_dec(&sdata->bss->num_sta_ps); 786*c868cb35SJohannes Berg sta_info_recalc_tim(sta); 78734e89507SJohannes Berg } 78834e89507SJohannes Berg 78934e89507SJohannes Berg local->num_sta--; 79034e89507SJohannes Berg local->sta_generation++; 79134e89507SJohannes Berg 79234e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 79334e89507SJohannes Berg rcu_assign_pointer(sdata->u.vlan.sta, NULL); 79434e89507SJohannes Berg 79534e89507SJohannes Berg if (sta->uploaded) { 79634e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 79734e89507SJohannes Berg sdata = container_of(sdata->bss, 79834e89507SJohannes Berg struct ieee80211_sub_if_data, 79934e89507SJohannes Berg u.ap); 80034e89507SJohannes Berg drv_sta_remove(local, sdata, &sta->sta); 80134e89507SJohannes Berg sdata = sta->sdata; 80234e89507SJohannes Berg } 80334e89507SJohannes Berg 804e64b3795SJohannes Berg /* 805e64b3795SJohannes Berg * At this point, after we wait for an RCU grace period, 806e64b3795SJohannes Berg * neither mac80211 nor the driver can reference this 807e64b3795SJohannes Berg * sta struct any more except by still existing timers 808e64b3795SJohannes Berg * associated with this station that we clean up below. 809e64b3795SJohannes Berg */ 810e64b3795SJohannes Berg synchronize_rcu(); 811e64b3795SJohannes Berg 812*c868cb35SJohannes Berg local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf); 813*c868cb35SJohannes Berg __skb_queue_purge(&sta->ps_tx_buf); 814*c868cb35SJohannes Berg __skb_queue_purge(&sta->tx_filtered); 815*c868cb35SJohannes Berg 81634e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH 817e64b3795SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 81834e89507SJohannes Berg mesh_accept_plinks_update(sdata); 81934e89507SJohannes Berg #endif 82034e89507SJohannes Berg 82134e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 8220fb9a9ecSJoe Perches wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr); 82334e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 82434e89507SJohannes Berg cancel_work_sync(&sta->drv_unblock_wk); 82534e89507SJohannes Berg 826ec15e68bSJouni Malinen cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL); 827ec15e68bSJouni Malinen 82834e89507SJohannes Berg rate_control_remove_sta_debugfs(sta); 82934e89507SJohannes Berg ieee80211_sta_debugfs_remove(sta); 83034e89507SJohannes Berg 83134e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH 83234e89507SJohannes Berg if (ieee80211_vif_is_mesh(&sta->sdata->vif)) { 83334e89507SJohannes Berg mesh_plink_deactivate(sta); 83434e89507SJohannes Berg del_timer_sync(&sta->plink_timer); 83534e89507SJohannes Berg } 83634e89507SJohannes Berg #endif 83734e89507SJohannes Berg 83834e89507SJohannes Berg __sta_info_free(local, sta); 83934e89507SJohannes Berg 84034e89507SJohannes Berg return 0; 84134e89507SJohannes Berg } 84234e89507SJohannes Berg 84334e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) 84434e89507SJohannes Berg { 84534e89507SJohannes Berg struct sta_info *sta; 84634e89507SJohannes Berg int ret; 84734e89507SJohannes Berg 84834e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 8492a33bee2SGuy Eilam sta = sta_info_get_rx(sdata, addr); 85034e89507SJohannes Berg ret = __sta_info_destroy(sta); 85134e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 85234e89507SJohannes Berg 85334e89507SJohannes Berg return ret; 85434e89507SJohannes Berg } 85534e89507SJohannes Berg 85634e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, 85734e89507SJohannes Berg const u8 *addr) 85834e89507SJohannes Berg { 85934e89507SJohannes Berg struct sta_info *sta; 86034e89507SJohannes Berg int ret; 86134e89507SJohannes Berg 86234e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 8632a33bee2SGuy Eilam sta = sta_info_get_bss_rx(sdata, addr); 86434e89507SJohannes Berg ret = __sta_info_destroy(sta); 86534e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 86634e89507SJohannes Berg 86734e89507SJohannes Berg return ret; 86834e89507SJohannes Berg } 869f0706e82SJiri Benc 870f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data) 871f0706e82SJiri Benc { 872f0706e82SJiri Benc struct ieee80211_local *local = (struct ieee80211_local *) data; 873f0706e82SJiri Benc struct sta_info *sta; 8743393a608SJuuso Oikarinen bool timer_needed = false; 875f0706e82SJiri Benc 876d0709a65SJohannes Berg rcu_read_lock(); 877d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) 8783393a608SJuuso Oikarinen if (sta_info_cleanup_expire_buffered(local, sta)) 8793393a608SJuuso Oikarinen timer_needed = true; 880d0709a65SJohannes Berg rcu_read_unlock(); 881f0706e82SJiri Benc 8825bb644a0SJohannes Berg if (local->quiescing) 8835bb644a0SJohannes Berg return; 8845bb644a0SJohannes Berg 8853393a608SJuuso Oikarinen if (!timer_needed) 8863393a608SJuuso Oikarinen return; 8873393a608SJuuso Oikarinen 88826d59535SJohannes Berg mod_timer(&local->sta_cleanup, 88926d59535SJohannes Berg round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 890f0706e82SJiri Benc } 891f0706e82SJiri Benc 892f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local) 893f0706e82SJiri Benc { 894d0709a65SJohannes Berg spin_lock_init(&local->sta_lock); 89534e89507SJohannes Berg mutex_init(&local->sta_mtx); 896f0706e82SJiri Benc INIT_LIST_HEAD(&local->sta_list); 89734e89507SJohannes Berg INIT_LIST_HEAD(&local->sta_pending_list); 89834e89507SJohannes Berg INIT_WORK(&local->sta_finish_work, sta_info_finish_work); 899f0706e82SJiri Benc 900b24b8a24SPavel Emelyanov setup_timer(&local->sta_cleanup, sta_info_cleanup, 901b24b8a24SPavel Emelyanov (unsigned long)local); 902f0706e82SJiri Benc } 903f0706e82SJiri Benc 904f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local) 905f0706e82SJiri Benc { 906f0706e82SJiri Benc del_timer(&local->sta_cleanup); 907be8755e1SMichael Wu sta_info_flush(local, NULL); 908f0706e82SJiri Benc } 909f0706e82SJiri Benc 910f0706e82SJiri Benc /** 911f0706e82SJiri Benc * sta_info_flush - flush matching STA entries from the STA table 91244213b5eSJohannes Berg * 91344213b5eSJohannes Berg * Returns the number of removed STA entries. 91444213b5eSJohannes Berg * 915f0706e82SJiri Benc * @local: local interface data 916d0709a65SJohannes Berg * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs 917f0706e82SJiri Benc */ 91844213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local, 919d0709a65SJohannes Berg struct ieee80211_sub_if_data *sdata) 920f0706e82SJiri Benc { 921f0706e82SJiri Benc struct sta_info *sta, *tmp; 92244213b5eSJohannes Berg int ret = 0; 923f0706e82SJiri Benc 924d0709a65SJohannes Berg might_sleep(); 925d0709a65SJohannes Berg 92634e89507SJohannes Berg mutex_lock(&local->sta_mtx); 927d0709a65SJohannes Berg 92834e89507SJohannes Berg sta_info_finish_pending(local); 92934e89507SJohannes Berg 93034e89507SJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 93134e89507SJohannes Berg if (!sdata || sdata == sta->sdata) 93234e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 93334e89507SJohannes Berg } 93434e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 93544213b5eSJohannes Berg 93644213b5eSJohannes Berg return ret; 937f0706e82SJiri Benc } 938dc6676b7SJohannes Berg 93924723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 94024723d1bSJohannes Berg unsigned long exp_time) 94124723d1bSJohannes Berg { 94224723d1bSJohannes Berg struct ieee80211_local *local = sdata->local; 94324723d1bSJohannes Berg struct sta_info *sta, *tmp; 94424723d1bSJohannes Berg 94534e89507SJohannes Berg mutex_lock(&local->sta_mtx); 94624723d1bSJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) 94724723d1bSJohannes Berg if (time_after(jiffies, sta->last_rx + exp_time)) { 94824723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG 9490c68ae26SJohannes Berg printk(KERN_DEBUG "%s: expiring inactive STA %pM\n", 95047846c9bSJohannes Berg sdata->name, sta->sta.addr); 95124723d1bSJohannes Berg #endif 95234e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 95324723d1bSJohannes Berg } 95434e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 95524723d1bSJohannes Berg } 95617741cdcSJohannes Berg 957686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 958686b9cb9SBen Greear const u8 *addr, 959686b9cb9SBen Greear const u8 *localaddr) 96017741cdcSJohannes Berg { 961abe60632SJohannes Berg struct sta_info *sta, *nxt; 96217741cdcSJohannes Berg 963686b9cb9SBen Greear /* 964686b9cb9SBen Greear * Just return a random station if localaddr is NULL 965686b9cb9SBen Greear * ... first in list. 966686b9cb9SBen Greear */ 967f7c65594SJohannes Berg for_each_sta_info(hw_to_local(hw), addr, sta, nxt) { 968686b9cb9SBen Greear if (localaddr && 969686b9cb9SBen Greear compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0) 970686b9cb9SBen Greear continue; 971f7c65594SJohannes Berg if (!sta->uploaded) 972f7c65594SJohannes Berg return NULL; 97317741cdcSJohannes Berg return &sta->sta; 974f7c65594SJohannes Berg } 975f7c65594SJohannes Berg 976abe60632SJohannes Berg return NULL; 97717741cdcSJohannes Berg } 978686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); 9795ed176e1SJohannes Berg 9805ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, 9815ed176e1SJohannes Berg const u8 *addr) 9825ed176e1SJohannes Berg { 983f7c65594SJohannes Berg struct sta_info *sta; 9845ed176e1SJohannes Berg 9855ed176e1SJohannes Berg if (!vif) 9865ed176e1SJohannes Berg return NULL; 9875ed176e1SJohannes Berg 988f7c65594SJohannes Berg sta = sta_info_get_bss(vif_to_sdata(vif), addr); 989f7c65594SJohannes Berg if (!sta) 990f7c65594SJohannes Berg return NULL; 9915ed176e1SJohannes Berg 992f7c65594SJohannes Berg if (!sta->uploaded) 993f7c65594SJohannes Berg return NULL; 994f7c65594SJohannes Berg 995f7c65594SJohannes Berg return &sta->sta; 9965ed176e1SJohannes Berg } 99717741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta); 998af818581SJohannes Berg 99950a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta) 100050a9432dSJohannes Berg { 100150a9432dSJohannes Berg struct sta_info *sta = _sta; 100250a9432dSJohannes Berg 100350a9432dSJohannes Berg clear_sta_flags(sta, WLAN_STA_PS_DRIVER | WLAN_STA_PS_STA); 100450a9432dSJohannes Berg } 100550a9432dSJohannes Berg 1006af818581SJohannes Berg /* powersave support code */ 1007af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) 1008af818581SJohannes Berg { 1009af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1010af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 1011af818581SJohannes Berg int sent, buffered; 1012af818581SJohannes Berg 1013dcf55fb5SFelix Fietkau clear_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF); 1014d057e5a3SArik Nemtsov if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS)) 101512375ef9SJohannes Berg drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 1016af818581SJohannes Berg 1017af818581SJohannes Berg /* Send all buffered frames to the station */ 1018af818581SJohannes Berg sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered); 101950a9432dSJohannes Berg buffered = ieee80211_add_pending_skbs_fn(local, &sta->ps_tx_buf, 102050a9432dSJohannes Berg clear_sta_ps_flags, sta); 1021af818581SJohannes Berg sent += buffered; 1022af818581SJohannes Berg local->total_ps_buffered -= buffered; 1023af818581SJohannes Berg 1024*c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1025*c868cb35SJohannes Berg 1026af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 1027af818581SJohannes Berg printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames " 102847846c9bSJohannes Berg "since STA not sleeping anymore\n", sdata->name, 1029af818581SJohannes Berg sta->sta.addr, sta->sta.aid, sent - buffered, buffered); 1030af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 1031af818581SJohannes Berg } 1032af818581SJohannes Berg 1033af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 1034af818581SJohannes Berg { 1035af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1036af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 1037af818581SJohannes Berg struct sk_buff *skb; 1038af818581SJohannes Berg int no_pending_pkts; 1039af818581SJohannes Berg 1040af818581SJohannes Berg skb = skb_dequeue(&sta->tx_filtered); 1041af818581SJohannes Berg if (!skb) { 1042af818581SJohannes Berg skb = skb_dequeue(&sta->ps_tx_buf); 1043af818581SJohannes Berg if (skb) 1044af818581SJohannes Berg local->total_ps_buffered--; 1045af818581SJohannes Berg } 1046af818581SJohannes Berg no_pending_pkts = skb_queue_empty(&sta->tx_filtered) && 1047af818581SJohannes Berg skb_queue_empty(&sta->ps_tx_buf); 1048af818581SJohannes Berg 1049af818581SJohannes Berg if (skb) { 1050af818581SJohannes Berg struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 1051af818581SJohannes Berg struct ieee80211_hdr *hdr = 1052af818581SJohannes Berg (struct ieee80211_hdr *) skb->data; 1053af818581SJohannes Berg 1054af818581SJohannes Berg /* 1055af818581SJohannes Berg * Tell TX path to send this frame even though the STA may 1056af818581SJohannes Berg * still remain is PS mode after this frame exchange. 1057af818581SJohannes Berg */ 1058af818581SJohannes Berg info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE; 1059af818581SJohannes Berg 1060af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 1061af818581SJohannes Berg printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n", 1062af818581SJohannes Berg sta->sta.addr, sta->sta.aid, 1063af818581SJohannes Berg skb_queue_len(&sta->ps_tx_buf)); 1064af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 1065af818581SJohannes Berg 1066af818581SJohannes Berg /* Use MoreData flag to indicate whether there are more 1067af818581SJohannes Berg * buffered frames for this STA */ 1068af818581SJohannes Berg if (no_pending_pkts) 1069af818581SJohannes Berg hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 1070af818581SJohannes Berg else 1071af818581SJohannes Berg hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA); 1072af818581SJohannes Berg 1073af818581SJohannes Berg ieee80211_add_pending_skb(local, skb); 1074af818581SJohannes Berg 1075*c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1076af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 1077af818581SJohannes Berg } else { 1078af818581SJohannes Berg /* 1079af818581SJohannes Berg * FIXME: This can be the result of a race condition between 1080af818581SJohannes Berg * us expiring a frame and the station polling for it. 1081af818581SJohannes Berg * Should we send it a null-func frame indicating we 1082af818581SJohannes Berg * have nothing buffered for it? 1083af818581SJohannes Berg */ 1084af818581SJohannes Berg printk(KERN_DEBUG "%s: STA %pM sent PS Poll even " 1085af818581SJohannes Berg "though there are no buffered frames for it\n", 108647846c9bSJohannes Berg sdata->name, sta->sta.addr); 1087af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 1088af818581SJohannes Berg } 1089af818581SJohannes Berg } 1090af818581SJohannes Berg 1091af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 1092af818581SJohannes Berg struct ieee80211_sta *pubsta, bool block) 1093af818581SJohannes Berg { 1094af818581SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1095af818581SJohannes Berg 1096b5878a2dSJohannes Berg trace_api_sta_block_awake(sta->local, pubsta, block); 1097b5878a2dSJohannes Berg 1098af818581SJohannes Berg if (block) 1099af818581SJohannes Berg set_sta_flags(sta, WLAN_STA_PS_DRIVER); 110050a9432dSJohannes Berg else if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) 1101af818581SJohannes Berg ieee80211_queue_work(hw, &sta->drv_unblock_wk); 1102af818581SJohannes Berg } 1103af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake); 1104dcf55fb5SFelix Fietkau 1105042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, 1106042ec453SJohannes Berg u8 tid, bool buffered) 1107dcf55fb5SFelix Fietkau { 1108dcf55fb5SFelix Fietkau struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1109dcf55fb5SFelix Fietkau 1110042ec453SJohannes Berg if (!buffered) 1111042ec453SJohannes Berg return; 1112042ec453SJohannes Berg 1113dcf55fb5SFelix Fietkau set_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF); 1114*c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1115dcf55fb5SFelix Fietkau } 1116042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered); 1117