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 * 3503e4497eSJohannes Berg * Upon allocating a STA info structure with sta_info_alloc(), the caller owns 3603e4497eSJohannes Berg * that structure. It must then either destroy it using sta_info_destroy() 3703e4497eSJohannes Berg * (which is pretty useless) or insert it into the hash table using 3803e4497eSJohannes Berg * sta_info_insert() which demotes the reference from ownership to a regular 3903e4497eSJohannes Berg * RCU-protected reference; if the function is called without protection by an 4093e5deb1SJohannes Berg * RCU critical section the reference is instantly invalidated. Note that the 4193e5deb1SJohannes Berg * caller may not do much with the STA info before inserting it, in particular, 4293e5deb1SJohannes Berg * it may not start any mesh peer link management or add 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 * 477e189a12SLuis R. Rodriguez * sta entries are added by mac80211 when you establish a link with a 487e189a12SLuis R. Rodriguez * peer. This means different things for the different type of interfaces 497e189a12SLuis R. Rodriguez * we support. For a regular station this mean we add the AP sta when we 507e189a12SLuis R. Rodriguez * receive an assocation response from the AP. For IBSS this occurs when 517e189a12SLuis R. Rodriguez * we receive a probe response or a beacon from target IBSS network. For 527e189a12SLuis R. Rodriguez * WDS we add the sta for the peer imediately upon device open. When using 537e189a12SLuis R. Rodriguez * AP mode we add stations for each respective station upon request from 547e189a12SLuis R. Rodriguez * userspace through nl80211. 557e189a12SLuis R. Rodriguez * 56d0709a65SJohannes Berg * Because there are debugfs entries for each station, and adding those 57d0709a65SJohannes Berg * must be able to sleep, it is also possible to "pin" a station entry, 58d0709a65SJohannes Berg * that means it can be removed from the hash table but not be freed. 5993e5deb1SJohannes Berg * See the comment in __sta_info_unlink() for more information, this is 6093e5deb1SJohannes Berg * an internal capability only. 61d0709a65SJohannes Berg * 62d0709a65SJohannes Berg * In order to remove a STA info structure, the caller needs to first 63dbbea671SJohannes Berg * unlink it (sta_info_unlink()) from the list and hash tables and 643b96766fSJohannes Berg * then destroy it; sta_info_destroy() will wait for an RCU grace period 653b96766fSJohannes Berg * to elapse before actually freeing it. Due to the pinning and the 663b96766fSJohannes Berg * possibility of multiple callers trying to remove the same STA info at 673b96766fSJohannes Berg * the same time, sta_info_unlink() can clear the STA info pointer it is 683b96766fSJohannes Berg * passed to indicate that the STA info is owned by somebody else now. 69d0709a65SJohannes Berg * 70dbbea671SJohannes Berg * If sta_info_unlink() did not clear the pointer then the caller owns 71d0709a65SJohannes Berg * the STA info structure now and is responsible of destroying it with 723b96766fSJohannes Berg * a call to sta_info_destroy(). 73d0709a65SJohannes Berg * 74d0709a65SJohannes Berg * In all other cases, there is no concept of ownership on a STA entry, 75d0709a65SJohannes Berg * each structure is owned by the global hash table/list until it is 76d0709a65SJohannes Berg * removed. All users of the structure need to be RCU protected so that 77d0709a65SJohannes Berg * the structure won't be freed before they are done using it. 78d0709a65SJohannes Berg */ 79f0706e82SJiri Benc 80f0706e82SJiri Benc /* Caller must hold local->sta_lock */ 81be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local, 82f0706e82SJiri Benc struct sta_info *sta) 83f0706e82SJiri Benc { 84f0706e82SJiri Benc struct sta_info *s; 85f0706e82SJiri Benc 8617741cdcSJohannes Berg s = local->sta_hash[STA_HASH(sta->sta.addr)]; 87f0706e82SJiri Benc if (!s) 88be8755e1SMichael Wu return -ENOENT; 89be8755e1SMichael Wu if (s == sta) { 9017741cdcSJohannes Berg rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], 91d0709a65SJohannes Berg s->hnext); 92be8755e1SMichael Wu return 0; 93f0706e82SJiri Benc } 94f0706e82SJiri Benc 95be8755e1SMichael Wu while (s->hnext && s->hnext != sta) 96f0706e82SJiri Benc s = s->hnext; 97be8755e1SMichael Wu if (s->hnext) { 98d0709a65SJohannes Berg rcu_assign_pointer(s->hnext, sta->hnext); 99be8755e1SMichael Wu return 0; 100f0706e82SJiri Benc } 101f0706e82SJiri Benc 102be8755e1SMichael Wu return -ENOENT; 103f0706e82SJiri Benc } 104f0706e82SJiri Benc 105d0709a65SJohannes Berg /* protected by RCU */ 106abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata, 107abe60632SJohannes Berg const u8 *addr) 10843ba7e95SJohannes Berg { 109abe60632SJohannes Berg struct ieee80211_local *local = sdata->local; 11043ba7e95SJohannes Berg struct sta_info *sta; 11143ba7e95SJohannes Berg 112d0709a65SJohannes Berg sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]); 11343ba7e95SJohannes Berg while (sta) { 114abe60632SJohannes Berg if (sta->sdata == sdata && 115abe60632SJohannes Berg memcmp(sta->sta.addr, addr, ETH_ALEN) == 0) 11643ba7e95SJohannes Berg break; 117d0709a65SJohannes Berg sta = rcu_dereference(sta->hnext); 11843ba7e95SJohannes Berg } 11943ba7e95SJohannes Berg return sta; 12043ba7e95SJohannes Berg } 12143ba7e95SJohannes Berg 1223b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, 1233b53fde8SJohannes Berg int idx) 124ee385855SLuis Carlos Cobo { 1253b53fde8SJohannes Berg struct ieee80211_local *local = sdata->local; 126ee385855SLuis Carlos Cobo struct sta_info *sta; 127ee385855SLuis Carlos Cobo int i = 0; 128ee385855SLuis Carlos Cobo 129d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) { 1303b53fde8SJohannes Berg if (sdata != sta->sdata) 1312a8ca29aSLuis Carlos Cobo continue; 132ee385855SLuis Carlos Cobo if (i < idx) { 133ee385855SLuis Carlos Cobo ++i; 134ee385855SLuis Carlos Cobo continue; 135ee385855SLuis Carlos Cobo } 1362a8ca29aSLuis Carlos Cobo return sta; 137ee385855SLuis Carlos Cobo } 138ee385855SLuis Carlos Cobo 139ee385855SLuis Carlos Cobo return NULL; 140ee385855SLuis Carlos Cobo } 141f0706e82SJiri Benc 14293e5deb1SJohannes Berg /** 14393e5deb1SJohannes Berg * __sta_info_free - internal STA free helper 14493e5deb1SJohannes Berg * 1456ef307bcSRandy Dunlap * @local: pointer to the global information 14693e5deb1SJohannes Berg * @sta: STA info to free 14793e5deb1SJohannes Berg * 14893e5deb1SJohannes Berg * This function must undo everything done by sta_info_alloc() 14993e5deb1SJohannes Berg * that may happen before sta_info_insert(). 15093e5deb1SJohannes Berg */ 15193e5deb1SJohannes Berg static void __sta_info_free(struct ieee80211_local *local, 15293e5deb1SJohannes Berg struct sta_info *sta) 15393e5deb1SJohannes Berg { 154af65cd96SJohannes Berg if (sta->rate_ctrl) { 1554b7679a5SJohannes Berg rate_control_free_sta(sta); 15693e5deb1SJohannes Berg rate_control_put(sta->rate_ctrl); 157af65cd96SJohannes Berg } 15893e5deb1SJohannes Berg 15993e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 1600c68ae26SJohannes Berg printk(KERN_DEBUG "%s: Destroyed STA %pM\n", 1610c68ae26SJohannes Berg wiphy_name(local->hw.wiphy), sta->sta.addr); 16293e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 16393e5deb1SJohannes Berg 16493e5deb1SJohannes Berg kfree(sta); 16593e5deb1SJohannes Berg } 16693e5deb1SJohannes Berg 167d0709a65SJohannes Berg void sta_info_destroy(struct sta_info *sta) 168f0706e82SJiri Benc { 16997bff8ecSJohannes Berg struct ieee80211_local *local; 170f0706e82SJiri Benc struct sk_buff *skb; 17107db2183SRon Rindjunsky int i; 17273651ee6SJohannes Berg 17397bff8ecSJohannes Berg might_sleep(); 17497bff8ecSJohannes Berg 17573651ee6SJohannes Berg if (!sta) 17673651ee6SJohannes Berg return; 177f0706e82SJiri Benc 17897bff8ecSJohannes Berg local = sta->local; 179d0709a65SJohannes Berg 180af818581SJohannes Berg cancel_work_sync(&sta->drv_unblock_wk); 181af818581SJohannes Berg 182d0709a65SJohannes Berg rate_control_remove_sta_debugfs(sta); 183d0709a65SJohannes Berg ieee80211_sta_debugfs_remove(sta); 184d0709a65SJohannes Berg 185d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH 186d0709a65SJohannes Berg if (ieee80211_vif_is_mesh(&sta->sdata->vif)) 187d0709a65SJohannes Berg mesh_plink_deactivate(sta); 188d0709a65SJohannes Berg #endif 189d0709a65SJohannes Berg 190d0709a65SJohannes Berg /* 1913b96766fSJohannes Berg * We have only unlinked the key, and actually destroying it 1923b96766fSJohannes Berg * may mean it is removed from hardware which requires that 1933b96766fSJohannes Berg * the key->sta pointer is still valid, so flush the key todo 1943b96766fSJohannes Berg * list here. 1953b96766fSJohannes Berg * 1963b96766fSJohannes Berg * ieee80211_key_todo() will synchronize_rcu() so after this 1973b96766fSJohannes Berg * nothing can reference this sta struct any more. 198d0709a65SJohannes Berg */ 1993b96766fSJohannes Berg ieee80211_key_todo(); 200d0709a65SJohannes Berg 201d0709a65SJohannes Berg #ifdef CONFIG_MAC80211_MESH 202d0709a65SJohannes Berg if (ieee80211_vif_is_mesh(&sta->sdata->vif)) 203d0709a65SJohannes Berg del_timer_sync(&sta->plink_timer); 204d0709a65SJohannes Berg #endif 205d0709a65SJohannes Berg 206f0706e82SJiri Benc while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) { 207f0706e82SJiri Benc local->total_ps_buffered--; 208f0706e82SJiri Benc dev_kfree_skb_any(skb); 209f0706e82SJiri Benc } 210d0709a65SJohannes Berg 211d0709a65SJohannes Berg while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) 212f0706e82SJiri Benc dev_kfree_skb_any(skb); 213d0709a65SJohannes Berg 214fe3bf0f5SRon Rindjunsky for (i = 0; i < STA_TID_NUM; i++) { 21555687e38SJohannes Berg struct tid_ampdu_rx *tid_rx; 21655687e38SJohannes Berg struct tid_ampdu_tx *tid_tx; 21755687e38SJohannes Berg 21807346f81SJohannes Berg spin_lock_bh(&sta->lock); 21955687e38SJohannes Berg tid_rx = sta->ampdu_mlme.tid_rx[i]; 22055687e38SJohannes Berg /* Make sure timer won't free the tid_rx struct, see below */ 22155687e38SJohannes Berg if (tid_rx) 22255687e38SJohannes Berg tid_rx->shutdown = true; 22396f5e66eSJohannes Berg 22407346f81SJohannes Berg spin_unlock_bh(&sta->lock); 22555687e38SJohannes Berg 22655687e38SJohannes Berg /* 22755687e38SJohannes Berg * Outside spinlock - shutdown is true now so that the timer 22855687e38SJohannes Berg * won't free tid_rx, we have to do that now. Can't let the 22955687e38SJohannes Berg * timer do it because we have to sync the timer outside the 23055687e38SJohannes Berg * lock that it takes itself. 23155687e38SJohannes Berg */ 23255687e38SJohannes Berg if (tid_rx) { 23355687e38SJohannes Berg del_timer_sync(&tid_rx->session_timer); 23455687e38SJohannes Berg kfree(tid_rx); 23555687e38SJohannes Berg } 23655687e38SJohannes Berg 23755687e38SJohannes Berg /* 23855687e38SJohannes Berg * No need to do such complications for TX agg sessions, the 23955687e38SJohannes Berg * path leading to freeing the tid_tx struct goes via a call 24055687e38SJohannes Berg * from the driver, and thus needs to look up the sta struct 24155687e38SJohannes Berg * again, which cannot be found when we get here. Hence, we 24255687e38SJohannes Berg * just need to delete the timer and free the aggregation 24355687e38SJohannes Berg * info; we won't be telling the peer about it then but that 24455687e38SJohannes Berg * doesn't matter if we're not talking to it again anyway. 24555687e38SJohannes Berg */ 24655687e38SJohannes Berg tid_tx = sta->ampdu_mlme.tid_tx[i]; 24755687e38SJohannes Berg if (tid_tx) { 24855687e38SJohannes Berg del_timer_sync(&tid_tx->addba_resp_timer); 249cd8ffc80SJohannes Berg /* 250cd8ffc80SJohannes Berg * STA removed while aggregation session being 251cd8ffc80SJohannes Berg * started? Bit odd, but purge frames anyway. 252cd8ffc80SJohannes Berg */ 253cd8ffc80SJohannes Berg skb_queue_purge(&tid_tx->pending); 25455687e38SJohannes Berg kfree(tid_tx); 25555687e38SJohannes Berg } 256fe3bf0f5SRon Rindjunsky } 257cee24a3eSRon Rindjunsky 25893e5deb1SJohannes Berg __sta_info_free(local, sta); 259f0706e82SJiri Benc } 260f0706e82SJiri Benc 261f0706e82SJiri Benc 262d0709a65SJohannes Berg /* Caller must hold local->sta_lock */ 263d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local, 264d0709a65SJohannes Berg struct sta_info *sta) 265f0706e82SJiri Benc { 26617741cdcSJohannes Berg sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)]; 26717741cdcSJohannes Berg rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta); 268f0706e82SJiri Benc } 269f0706e82SJiri Benc 270af818581SJohannes Berg static void sta_unblock(struct work_struct *wk) 271af818581SJohannes Berg { 272af818581SJohannes Berg struct sta_info *sta; 273af818581SJohannes Berg 274af818581SJohannes Berg sta = container_of(wk, struct sta_info, drv_unblock_wk); 275af818581SJohannes Berg 276af818581SJohannes Berg if (sta->dead) 277af818581SJohannes Berg return; 278af818581SJohannes Berg 279af818581SJohannes Berg if (!test_sta_flags(sta, WLAN_STA_PS_STA)) 280af818581SJohannes Berg ieee80211_sta_ps_deliver_wakeup(sta); 281af818581SJohannes Berg else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL)) 282af818581SJohannes Berg ieee80211_sta_ps_deliver_poll_response(sta); 283af818581SJohannes Berg } 284af818581SJohannes Berg 285af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local, 286af65cd96SJohannes Berg struct sta_info *sta, gfp_t gfp) 287af65cd96SJohannes Berg { 288af65cd96SJohannes Berg if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) 289af65cd96SJohannes Berg return 0; 290af65cd96SJohannes Berg 291af65cd96SJohannes Berg sta->rate_ctrl = rate_control_get(local->rate_ctrl); 292af65cd96SJohannes Berg sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, 293af65cd96SJohannes Berg &sta->sta, gfp); 294af65cd96SJohannes Berg if (!sta->rate_ctrl_priv) { 295af65cd96SJohannes Berg rate_control_put(sta->rate_ctrl); 296af65cd96SJohannes Berg return -ENOMEM; 297af65cd96SJohannes Berg } 298af65cd96SJohannes Berg 299af65cd96SJohannes Berg return 0; 300af65cd96SJohannes Berg } 301af65cd96SJohannes Berg 30273651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, 30373651ee6SJohannes Berg u8 *addr, gfp_t gfp) 304f0706e82SJiri Benc { 305d0709a65SJohannes Berg struct ieee80211_local *local = sdata->local; 306f0706e82SJiri Benc struct sta_info *sta; 30716c5f15cSRon Rindjunsky int i; 308f0706e82SJiri Benc 30917741cdcSJohannes Berg sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp); 310f0706e82SJiri Benc if (!sta) 31173651ee6SJohannes Berg return NULL; 312f0706e82SJiri Benc 31307346f81SJohannes Berg spin_lock_init(&sta->lock); 3145a9f7b04SJohannes Berg spin_lock_init(&sta->flaglock); 315af818581SJohannes Berg INIT_WORK(&sta->drv_unblock_wk, sta_unblock); 31607346f81SJohannes Berg 31717741cdcSJohannes Berg memcpy(sta->sta.addr, addr, ETH_ALEN); 318d0709a65SJohannes Berg sta->local = local; 319d0709a65SJohannes Berg sta->sdata = sdata; 320f0706e82SJiri Benc 321af65cd96SJohannes Berg if (sta_prepare_rate_control(local, sta, gfp)) { 322f0706e82SJiri Benc kfree(sta); 32373651ee6SJohannes Berg return NULL; 324f0706e82SJiri Benc } 325f0706e82SJiri Benc 32616c5f15cSRon Rindjunsky for (i = 0; i < STA_TID_NUM; i++) { 32716c5f15cSRon Rindjunsky /* timer_to_tid must be initialized with identity mapping to 32816c5f15cSRon Rindjunsky * enable session_timer's data differentiation. refer to 32916c5f15cSRon Rindjunsky * sta_rx_agg_session_timer_expired for useage */ 33016c5f15cSRon Rindjunsky sta->timer_to_tid[i] = i; 331cee24a3eSRon Rindjunsky /* rx */ 332cee24a3eSRon Rindjunsky sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE; 333cee24a3eSRon Rindjunsky sta->ampdu_mlme.tid_rx[i] = NULL; 334cee24a3eSRon Rindjunsky /* tx */ 335cee24a3eSRon Rindjunsky sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE; 336cee24a3eSRon Rindjunsky sta->ampdu_mlme.tid_tx[i] = NULL; 337cee24a3eSRon Rindjunsky sta->ampdu_mlme.addba_req_num[i] = 0; 33816c5f15cSRon Rindjunsky } 339f0706e82SJiri Benc skb_queue_head_init(&sta->ps_tx_buf); 340f0706e82SJiri Benc skb_queue_head_init(&sta->tx_filtered); 34173651ee6SJohannes Berg 342cccaec98SSenthil Balasubramanian for (i = 0; i < NUM_RX_DATA_QUEUES; i++) 343cccaec98SSenthil Balasubramanian sta->last_seq_ctrl[i] = cpu_to_le16(USHORT_MAX); 344cccaec98SSenthil Balasubramanian 34573651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 3460c68ae26SJohannes Berg printk(KERN_DEBUG "%s: Allocated STA %pM\n", 3470c68ae26SJohannes Berg wiphy_name(local->hw.wiphy), sta->sta.addr); 34873651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 34973651ee6SJohannes Berg 35003e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH 351b4e08ea1SLuis Carlos Cobo sta->plink_state = PLINK_LISTEN; 35203e4497eSJohannes Berg init_timer(&sta->plink_timer); 35303e4497eSJohannes Berg #endif 35403e4497eSJohannes Berg 35573651ee6SJohannes Berg return sta; 35673651ee6SJohannes Berg } 35773651ee6SJohannes Berg 35873651ee6SJohannes Berg int sta_info_insert(struct sta_info *sta) 35973651ee6SJohannes Berg { 36073651ee6SJohannes Berg struct ieee80211_local *local = sta->local; 36173651ee6SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 362*98b62183SJohannes Berg struct station_info sinfo; 36373651ee6SJohannes Berg unsigned long flags; 36493e5deb1SJohannes Berg int err = 0; 36573651ee6SJohannes Berg 36603e4497eSJohannes Berg /* 36703e4497eSJohannes Berg * Can't be a WARN_ON because it can be triggered through a race: 36803e4497eSJohannes Berg * something inserts a STA (on one CPU) without holding the RTNL 36903e4497eSJohannes Berg * and another CPU turns off the net device. 37003e4497eSJohannes Berg */ 3719607e6b6SJohannes Berg if (unlikely(!ieee80211_sdata_running(sdata))) { 37293e5deb1SJohannes Berg err = -ENETDOWN; 37393e5deb1SJohannes Berg goto out_free; 37493e5deb1SJohannes Berg } 37503e4497eSJohannes Berg 37647846c9bSJohannes Berg if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 || 37717741cdcSJohannes Berg is_multicast_ether_addr(sta->sta.addr))) { 37893e5deb1SJohannes Berg err = -EINVAL; 37993e5deb1SJohannes Berg goto out_free; 38093e5deb1SJohannes Berg } 38144213b5eSJohannes Berg 382d0709a65SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 38343ba7e95SJohannes Berg /* check if STA exists already */ 384abe60632SJohannes Berg if (sta_info_get(sdata, sta->sta.addr)) { 385d0709a65SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 38693e5deb1SJohannes Berg err = -EEXIST; 38793e5deb1SJohannes Berg goto out_free; 38843ba7e95SJohannes Berg } 389f0706e82SJiri Benc list_add(&sta->list, &local->sta_list); 390f5ea9120SJohannes Berg local->sta_generation++; 391f0706e82SJiri Benc local->num_sta++; 392f0706e82SJiri Benc sta_info_hash_add(local, sta); 39332bfd35dSJohannes Berg 394d0709a65SJohannes Berg /* notify driver */ 395d0709a65SJohannes Berg if (local->ops->sta_notify) { 39605c914feSJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 3973e122be0SJohannes Berg sdata = container_of(sdata->bss, 3983e122be0SJohannes Berg struct ieee80211_sub_if_data, 3993e122be0SJohannes Berg u.ap); 40032bfd35dSJohannes Berg 40112375ef9SJohannes Berg drv_sta_notify(local, sdata, STA_NOTIFY_ADD, &sta->sta); 402fbc44bf7SJohannes Berg sdata = sta->sdata; 40332bfd35dSJohannes Berg } 404d0709a65SJohannes Berg 405f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 4060c68ae26SJohannes Berg printk(KERN_DEBUG "%s: Inserted STA %pM\n", 4070c68ae26SJohannes Berg wiphy_name(local->hw.wiphy), sta->sta.addr); 408f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 409f0706e82SJiri Benc 41073651ee6SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 41173651ee6SJohannes Berg 412*98b62183SJohannes Berg sinfo.filled = 0; 413*98b62183SJohannes Berg sinfo.generation = local->sta_generation; 414*98b62183SJohannes Berg cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_ATOMIC); 415*98b62183SJohannes Berg 416e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS 41793e5deb1SJohannes Berg /* 41893e5deb1SJohannes Berg * Debugfs entry adding might sleep, so schedule process 419e9f207f0SJiri Benc * context task for adding entry for STAs that do not yet 42093e5deb1SJohannes Berg * have one. 42193e5deb1SJohannes Berg * NOTE: due to auto-freeing semantics this may only be done 42293e5deb1SJohannes Berg * if the insertion is successful! 42393e5deb1SJohannes Berg */ 42449ec6fa2SJohannes Berg schedule_work(&local->sta_debugfs_add); 425e9f207f0SJiri Benc #endif 426e9f207f0SJiri Benc 42773651ee6SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 42873651ee6SJohannes Berg mesh_accept_plinks_update(sdata); 42973651ee6SJohannes Berg 43073651ee6SJohannes Berg return 0; 43193e5deb1SJohannes Berg out_free: 43293e5deb1SJohannes Berg BUG_ON(!err); 43393e5deb1SJohannes Berg __sta_info_free(local, sta); 43493e5deb1SJohannes Berg return err; 435f0706e82SJiri Benc } 436f0706e82SJiri Benc 437004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid) 438004c872eSJohannes Berg { 439004c872eSJohannes Berg /* 440004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 441004c872eSJohannes Berg * so this line may not be changed to use the __set_bit() format. 442004c872eSJohannes Berg */ 443004c872eSJohannes Berg bss->tim[aid / 8] |= (1 << (aid % 8)); 444004c872eSJohannes Berg } 445004c872eSJohannes Berg 446004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid) 447004c872eSJohannes Berg { 448004c872eSJohannes Berg /* 449004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 450004c872eSJohannes Berg * so this line may not be changed to use the __clear_bit() format. 451004c872eSJohannes Berg */ 452004c872eSJohannes Berg bss->tim[aid / 8] &= ~(1 << (aid % 8)); 453004c872eSJohannes Berg } 454004c872eSJohannes Berg 455004c872eSJohannes Berg static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss, 456004c872eSJohannes Berg struct sta_info *sta) 457004c872eSJohannes Berg { 4583e122be0SJohannes Berg BUG_ON(!bss); 4593e122be0SJohannes Berg 46017741cdcSJohannes Berg __bss_tim_set(bss, sta->sta.aid); 4613e122be0SJohannes Berg 462d0709a65SJohannes Berg if (sta->local->ops->set_tim) { 463d0709a65SJohannes Berg sta->local->tim_in_locked_section = true; 46424487981SJohannes Berg drv_set_tim(sta->local, &sta->sta, true); 465d0709a65SJohannes Berg sta->local->tim_in_locked_section = false; 466d0709a65SJohannes Berg } 467004c872eSJohannes Berg } 468004c872eSJohannes Berg 469004c872eSJohannes Berg void sta_info_set_tim_bit(struct sta_info *sta) 470004c872eSJohannes Berg { 471d0709a65SJohannes Berg unsigned long flags; 472004c872eSJohannes Berg 4733e122be0SJohannes Berg BUG_ON(!sta->sdata->bss); 4743e122be0SJohannes Berg 475d0709a65SJohannes Berg spin_lock_irqsave(&sta->local->sta_lock, flags); 476d0709a65SJohannes Berg __sta_info_set_tim_bit(sta->sdata->bss, sta); 477d0709a65SJohannes Berg spin_unlock_irqrestore(&sta->local->sta_lock, flags); 478004c872eSJohannes Berg } 479004c872eSJohannes Berg 480004c872eSJohannes Berg static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss, 481004c872eSJohannes Berg struct sta_info *sta) 482004c872eSJohannes Berg { 4833e122be0SJohannes Berg BUG_ON(!bss); 4843e122be0SJohannes Berg 48517741cdcSJohannes Berg __bss_tim_clear(bss, sta->sta.aid); 4863e122be0SJohannes Berg 487d0709a65SJohannes Berg if (sta->local->ops->set_tim) { 488d0709a65SJohannes Berg sta->local->tim_in_locked_section = true; 48924487981SJohannes Berg drv_set_tim(sta->local, &sta->sta, false); 490d0709a65SJohannes Berg sta->local->tim_in_locked_section = false; 491d0709a65SJohannes Berg } 492004c872eSJohannes Berg } 493004c872eSJohannes Berg 494004c872eSJohannes Berg void sta_info_clear_tim_bit(struct sta_info *sta) 495004c872eSJohannes Berg { 496d0709a65SJohannes Berg unsigned long flags; 497004c872eSJohannes Berg 4983e122be0SJohannes Berg BUG_ON(!sta->sdata->bss); 4993e122be0SJohannes Berg 500d0709a65SJohannes Berg spin_lock_irqsave(&sta->local->sta_lock, flags); 501d0709a65SJohannes Berg __sta_info_clear_tim_bit(sta->sdata->bss, sta); 502d0709a65SJohannes Berg spin_unlock_irqrestore(&sta->local->sta_lock, flags); 503004c872eSJohannes Berg } 504004c872eSJohannes Berg 50524723d1bSJohannes Berg static void __sta_info_unlink(struct sta_info **sta) 506d0709a65SJohannes Berg { 507d0709a65SJohannes Berg struct ieee80211_local *local = (*sta)->local; 508d0709a65SJohannes Berg struct ieee80211_sub_if_data *sdata = (*sta)->sdata; 509d0709a65SJohannes Berg /* 510d0709a65SJohannes Berg * pull caller's reference if we're already gone. 511d0709a65SJohannes Berg */ 512d0709a65SJohannes Berg if (sta_info_hash_del(local, *sta)) { 513d0709a65SJohannes Berg *sta = NULL; 514be8755e1SMichael Wu return; 515d0709a65SJohannes Berg } 516be8755e1SMichael Wu 5173b96766fSJohannes Berg if ((*sta)->key) { 5183b96766fSJohannes Berg ieee80211_key_free((*sta)->key); 5193b96766fSJohannes Berg WARN_ON((*sta)->key); 5203b96766fSJohannes Berg } 5213b96766fSJohannes Berg 5227d1559f1SJohannes Berg list_del(&(*sta)->list); 523af818581SJohannes Berg (*sta)->dead = true; 5247d1559f1SJohannes Berg 525af818581SJohannes Berg if (test_and_clear_sta_flags(*sta, 526af818581SJohannes Berg WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) { 5273e122be0SJohannes Berg BUG_ON(!sdata->bss); 5283e122be0SJohannes Berg 5297d1559f1SJohannes Berg atomic_dec(&sdata->bss->num_sta_ps); 5307d1559f1SJohannes Berg __sta_info_clear_tim_bit(sdata->bss, *sta); 5317d1559f1SJohannes Berg } 5327d1559f1SJohannes Berg 5337d1559f1SJohannes Berg local->num_sta--; 534f5ea9120SJohannes Berg local->sta_generation++; 5357d1559f1SJohannes Berg 536f14543eeSFelix Fietkau if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 537f14543eeSFelix Fietkau rcu_assign_pointer(sdata->u.vlan.sta, NULL); 538f14543eeSFelix Fietkau 5397d1559f1SJohannes Berg if (local->ops->sta_notify) { 54005c914feSJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 5413e122be0SJohannes Berg sdata = container_of(sdata->bss, 5423e122be0SJohannes Berg struct ieee80211_sub_if_data, 5433e122be0SJohannes Berg u.ap); 5447d1559f1SJohannes Berg 54512375ef9SJohannes Berg drv_sta_notify(local, sdata, STA_NOTIFY_REMOVE, 54624487981SJohannes Berg &(*sta)->sta); 547fbc44bf7SJohannes Berg sdata = (*sta)->sdata; 5487d1559f1SJohannes Berg } 5497d1559f1SJohannes Berg 5507d1559f1SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) { 5517d1559f1SJohannes Berg mesh_accept_plinks_update(sdata); 5527d1559f1SJohannes Berg #ifdef CONFIG_MAC80211_MESH 5537d1559f1SJohannes Berg del_timer(&(*sta)->plink_timer); 5547d1559f1SJohannes Berg #endif 5557d1559f1SJohannes Berg } 5567d1559f1SJohannes Berg 5577d1559f1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 5580c68ae26SJohannes Berg printk(KERN_DEBUG "%s: Removed STA %pM\n", 5590c68ae26SJohannes Berg wiphy_name(local->hw.wiphy), (*sta)->sta.addr); 5607d1559f1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 5617d1559f1SJohannes Berg 562d0709a65SJohannes Berg /* 5637d1559f1SJohannes Berg * Finally, pull caller's reference if the STA is pinned by the 564d0709a65SJohannes Berg * task that is adding the debugfs entries. In that case, we 565d0709a65SJohannes Berg * leave the STA "to be freed". 566d0709a65SJohannes Berg * 567d0709a65SJohannes Berg * The rules are not trivial, but not too complex either: 568d0709a65SJohannes Berg * (1) pin_status is only modified under the sta_lock 56949ec6fa2SJohannes Berg * (2) STAs may only be pinned under the RTNL so that 57049ec6fa2SJohannes Berg * sta_info_flush() is guaranteed to actually destroy 57149ec6fa2SJohannes Berg * all STAs that are active for a given interface, this 57249ec6fa2SJohannes Berg * is required for correctness because otherwise we 57349ec6fa2SJohannes Berg * could notify a driver that an interface is going 57449ec6fa2SJohannes Berg * away and only after that (!) notify it about a STA 57549ec6fa2SJohannes Berg * on that interface going away. 57649ec6fa2SJohannes Berg * (3) sta_info_debugfs_add_work() will set the status 577d0709a65SJohannes Berg * to PINNED when it found an item that needs a new 578d0709a65SJohannes Berg * debugfs directory created. In that case, that item 579d0709a65SJohannes Berg * must not be freed although all *RCU* users are done 580d0709a65SJohannes Berg * with it. Hence, we tell the caller of _unlink() 581d0709a65SJohannes Berg * that the item is already gone (as can happen when 582d0709a65SJohannes Berg * two tasks try to unlink/destroy at the same time) 58349ec6fa2SJohannes Berg * (4) We set the pin_status to DESTROY here when we 584d0709a65SJohannes Berg * find such an item. 58549ec6fa2SJohannes Berg * (5) sta_info_debugfs_add_work() will reset the pin_status 586d0709a65SJohannes Berg * from PINNED to NORMAL when it is done with the item, 587d0709a65SJohannes Berg * but will check for DESTROY before resetting it in 588d0709a65SJohannes Berg * which case it will free the item. 589d0709a65SJohannes Berg */ 590d0709a65SJohannes Berg if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) { 591d0709a65SJohannes Berg (*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY; 592d0709a65SJohannes Berg *sta = NULL; 593d0709a65SJohannes Berg return; 594d0709a65SJohannes Berg } 595d0709a65SJohannes Berg } 596d0709a65SJohannes Berg 597d0709a65SJohannes Berg void sta_info_unlink(struct sta_info **sta) 598d0709a65SJohannes Berg { 599d0709a65SJohannes Berg struct ieee80211_local *local = (*sta)->local; 600d0709a65SJohannes Berg unsigned long flags; 601d0709a65SJohannes Berg 602d0709a65SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 603d0709a65SJohannes Berg __sta_info_unlink(sta); 604d0709a65SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 605d0709a65SJohannes Berg } 606f0706e82SJiri Benc 60757c4d7b4SJohannes Berg static int sta_info_buffer_expired(struct sta_info *sta, 608f0706e82SJiri Benc struct sk_buff *skb) 609f0706e82SJiri Benc { 610e039fa4aSJohannes Berg struct ieee80211_tx_info *info; 611f0706e82SJiri Benc int timeout; 612f0706e82SJiri Benc 613f0706e82SJiri Benc if (!skb) 614f0706e82SJiri Benc return 0; 615f0706e82SJiri Benc 616e039fa4aSJohannes Berg info = IEEE80211_SKB_CB(skb); 617f0706e82SJiri Benc 618f0706e82SJiri Benc /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 61957c4d7b4SJohannes Berg timeout = (sta->listen_interval * 62057c4d7b4SJohannes Berg sta->sdata->vif.bss_conf.beacon_int * 62157c4d7b4SJohannes Berg 32 / 15625) * HZ; 622f0706e82SJiri Benc if (timeout < STA_TX_BUFFER_EXPIRE) 623f0706e82SJiri Benc timeout = STA_TX_BUFFER_EXPIRE; 624e039fa4aSJohannes Berg return time_after(jiffies, info->control.jiffies + timeout); 625f0706e82SJiri Benc } 626f0706e82SJiri Benc 627f0706e82SJiri Benc 628f0706e82SJiri Benc static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 629f0706e82SJiri Benc struct sta_info *sta) 630f0706e82SJiri Benc { 631f0706e82SJiri Benc unsigned long flags; 632f0706e82SJiri Benc struct sk_buff *skb; 633836341a7SJohannes Berg struct ieee80211_sub_if_data *sdata; 634f0706e82SJiri Benc 635f0706e82SJiri Benc if (skb_queue_empty(&sta->ps_tx_buf)) 636f0706e82SJiri Benc return; 637f0706e82SJiri Benc 638f0706e82SJiri Benc for (;;) { 639f0706e82SJiri Benc spin_lock_irqsave(&sta->ps_tx_buf.lock, flags); 640f0706e82SJiri Benc skb = skb_peek(&sta->ps_tx_buf); 64157c4d7b4SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 642f0706e82SJiri Benc skb = __skb_dequeue(&sta->ps_tx_buf); 643836341a7SJohannes Berg else 644f0706e82SJiri Benc skb = NULL; 645f0706e82SJiri Benc spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags); 646f0706e82SJiri Benc 647836341a7SJohannes Berg if (!skb) 648836341a7SJohannes Berg break; 649836341a7SJohannes Berg 650d0709a65SJohannes Berg sdata = sta->sdata; 651f0706e82SJiri Benc local->total_ps_buffered--; 652f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 6530c68ae26SJohannes Berg printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n", 6540c68ae26SJohannes Berg sta->sta.addr); 655f4ea83ddSJohannes Berg #endif 656f0706e82SJiri Benc dev_kfree_skb(skb); 657836341a7SJohannes Berg 658004c872eSJohannes Berg if (skb_queue_empty(&sta->ps_tx_buf)) 659004c872eSJohannes Berg sta_info_clear_tim_bit(sta); 660f0706e82SJiri Benc } 661f0706e82SJiri Benc } 662f0706e82SJiri Benc 663f0706e82SJiri Benc 664f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data) 665f0706e82SJiri Benc { 666f0706e82SJiri Benc struct ieee80211_local *local = (struct ieee80211_local *) data; 667f0706e82SJiri Benc struct sta_info *sta; 668f0706e82SJiri Benc 669d0709a65SJohannes Berg rcu_read_lock(); 670d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) 671f0706e82SJiri Benc sta_info_cleanup_expire_buffered(local, sta); 672d0709a65SJohannes Berg rcu_read_unlock(); 673f0706e82SJiri Benc 6745bb644a0SJohannes Berg if (local->quiescing) 6755bb644a0SJohannes Berg return; 6765bb644a0SJohannes Berg 6770d174406SJohannes Berg local->sta_cleanup.expires = 6780d174406SJohannes Berg round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL); 679f0706e82SJiri Benc add_timer(&local->sta_cleanup); 680f0706e82SJiri Benc } 681f0706e82SJiri Benc 682e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS 6834d6141c3SJiri Slaby /* 6844d6141c3SJiri Slaby * See comment in __sta_info_unlink, 6854d6141c3SJiri Slaby * caller must hold local->sta_lock. 6864d6141c3SJiri Slaby */ 6874d6141c3SJiri Slaby static void __sta_info_pin(struct sta_info *sta) 6884d6141c3SJiri Slaby { 6894d6141c3SJiri Slaby WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL); 6904d6141c3SJiri Slaby sta->pin_status = STA_INFO_PIN_STAT_PINNED; 6914d6141c3SJiri Slaby } 6924d6141c3SJiri Slaby 6934d6141c3SJiri Slaby /* 6944d6141c3SJiri Slaby * See comment in __sta_info_unlink, returns sta if it 6954d6141c3SJiri Slaby * needs to be destroyed. 6964d6141c3SJiri Slaby */ 6974d6141c3SJiri Slaby static struct sta_info *__sta_info_unpin(struct sta_info *sta) 6984d6141c3SJiri Slaby { 6994d6141c3SJiri Slaby struct sta_info *ret = NULL; 7004d6141c3SJiri Slaby unsigned long flags; 7014d6141c3SJiri Slaby 7024d6141c3SJiri Slaby spin_lock_irqsave(&sta->local->sta_lock, flags); 7034d6141c3SJiri Slaby WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY && 7044d6141c3SJiri Slaby sta->pin_status != STA_INFO_PIN_STAT_PINNED); 7054d6141c3SJiri Slaby if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY) 7064d6141c3SJiri Slaby ret = sta; 7074d6141c3SJiri Slaby sta->pin_status = STA_INFO_PIN_STAT_NORMAL; 7084d6141c3SJiri Slaby spin_unlock_irqrestore(&sta->local->sta_lock, flags); 7094d6141c3SJiri Slaby 7104d6141c3SJiri Slaby return ret; 7114d6141c3SJiri Slaby } 7124d6141c3SJiri Slaby 713d0709a65SJohannes Berg static void sta_info_debugfs_add_work(struct work_struct *work) 714e9f207f0SJiri Benc { 715e9f207f0SJiri Benc struct ieee80211_local *local = 716e9f207f0SJiri Benc container_of(work, struct ieee80211_local, sta_debugfs_add); 717e9f207f0SJiri Benc struct sta_info *sta, *tmp; 718d0709a65SJohannes Berg unsigned long flags; 719e9f207f0SJiri Benc 72049ec6fa2SJohannes Berg /* We need to keep the RTNL across the whole pinned status. */ 72149ec6fa2SJohannes Berg rtnl_lock(); 722e9f207f0SJiri Benc while (1) { 723e9f207f0SJiri Benc sta = NULL; 724d0709a65SJohannes Berg 725d0709a65SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 726e9f207f0SJiri Benc list_for_each_entry(tmp, &local->sta_list, list) { 72763044e9fSJohannes Berg /* 72863044e9fSJohannes Berg * debugfs.add_has_run will be set by 72963044e9fSJohannes Berg * ieee80211_sta_debugfs_add regardless 73063044e9fSJohannes Berg * of what else it does. 73163044e9fSJohannes Berg */ 73263044e9fSJohannes Berg if (!tmp->debugfs.add_has_run) { 733e9f207f0SJiri Benc sta = tmp; 734d0709a65SJohannes Berg __sta_info_pin(sta); 735e9f207f0SJiri Benc break; 736e9f207f0SJiri Benc } 737e9f207f0SJiri Benc } 738d0709a65SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 739e9f207f0SJiri Benc 740e9f207f0SJiri Benc if (!sta) 741e9f207f0SJiri Benc break; 742e9f207f0SJiri Benc 743e9f207f0SJiri Benc ieee80211_sta_debugfs_add(sta); 744e9f207f0SJiri Benc rate_control_add_sta_debugfs(sta); 745d0709a65SJohannes Berg 746d0709a65SJohannes Berg sta = __sta_info_unpin(sta); 747d0709a65SJohannes Berg sta_info_destroy(sta); 748e9f207f0SJiri Benc } 74949ec6fa2SJohannes Berg rtnl_unlock(); 750e9f207f0SJiri Benc } 751e9f207f0SJiri Benc #endif 752e9f207f0SJiri Benc 753f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local) 754f0706e82SJiri Benc { 755d0709a65SJohannes Berg spin_lock_init(&local->sta_lock); 756f0706e82SJiri Benc INIT_LIST_HEAD(&local->sta_list); 757f0706e82SJiri Benc 758b24b8a24SPavel Emelyanov setup_timer(&local->sta_cleanup, sta_info_cleanup, 759b24b8a24SPavel Emelyanov (unsigned long)local); 7600d174406SJohannes Berg local->sta_cleanup.expires = 7610d174406SJohannes Berg round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL); 762e9f207f0SJiri Benc 763e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS 764d0709a65SJohannes Berg INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work); 765e9f207f0SJiri Benc #endif 766f0706e82SJiri Benc } 767f0706e82SJiri Benc 768f0706e82SJiri Benc int sta_info_start(struct ieee80211_local *local) 769f0706e82SJiri Benc { 770f0706e82SJiri Benc add_timer(&local->sta_cleanup); 771f0706e82SJiri Benc return 0; 772f0706e82SJiri Benc } 773f0706e82SJiri Benc 774f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local) 775f0706e82SJiri Benc { 776f0706e82SJiri Benc del_timer(&local->sta_cleanup); 77749ec6fa2SJohannes Berg #ifdef CONFIG_MAC80211_DEBUGFS 77849ec6fa2SJohannes Berg /* 77949ec6fa2SJohannes Berg * Make sure the debugfs adding work isn't pending after this 78049ec6fa2SJohannes Berg * because we're about to be destroyed. It doesn't matter 78149ec6fa2SJohannes Berg * whether it ran or not since we're going to flush all STAs 78249ec6fa2SJohannes Berg * anyway. 78349ec6fa2SJohannes Berg */ 78449ec6fa2SJohannes Berg cancel_work_sync(&local->sta_debugfs_add); 78549ec6fa2SJohannes Berg #endif 786dc6676b7SJohannes Berg 787be8755e1SMichael Wu sta_info_flush(local, NULL); 788f0706e82SJiri Benc } 789f0706e82SJiri Benc 790f0706e82SJiri Benc /** 791f0706e82SJiri Benc * sta_info_flush - flush matching STA entries from the STA table 79244213b5eSJohannes Berg * 79344213b5eSJohannes Berg * Returns the number of removed STA entries. 79444213b5eSJohannes Berg * 795f0706e82SJiri Benc * @local: local interface data 796d0709a65SJohannes Berg * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs 797f0706e82SJiri Benc */ 79844213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local, 799d0709a65SJohannes Berg struct ieee80211_sub_if_data *sdata) 800f0706e82SJiri Benc { 801f0706e82SJiri Benc struct sta_info *sta, *tmp; 802be8755e1SMichael Wu LIST_HEAD(tmp_list); 80344213b5eSJohannes Berg int ret = 0; 804d0709a65SJohannes Berg unsigned long flags; 805f0706e82SJiri Benc 806d0709a65SJohannes Berg might_sleep(); 807d0709a65SJohannes Berg 808d0709a65SJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 809d0709a65SJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 810d0709a65SJohannes Berg if (!sdata || sdata == sta->sdata) { 811d0709a65SJohannes Berg __sta_info_unlink(&sta); 81244213b5eSJohannes Berg if (sta) { 813be8755e1SMichael Wu list_add_tail(&sta->list, &tmp_list); 81444213b5eSJohannes Berg ret++; 81544213b5eSJohannes Berg } 816be8755e1SMichael Wu } 817be8755e1SMichael Wu } 818d0709a65SJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 819d0709a65SJohannes Berg 820d0709a65SJohannes Berg list_for_each_entry_safe(sta, tmp, &tmp_list, list) 821d0709a65SJohannes Berg sta_info_destroy(sta); 82244213b5eSJohannes Berg 82344213b5eSJohannes Berg return ret; 824f0706e82SJiri Benc } 825dc6676b7SJohannes Berg 82624723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 82724723d1bSJohannes Berg unsigned long exp_time) 82824723d1bSJohannes Berg { 82924723d1bSJohannes Berg struct ieee80211_local *local = sdata->local; 83024723d1bSJohannes Berg struct sta_info *sta, *tmp; 83124723d1bSJohannes Berg LIST_HEAD(tmp_list); 83224723d1bSJohannes Berg unsigned long flags; 83324723d1bSJohannes Berg 83424723d1bSJohannes Berg spin_lock_irqsave(&local->sta_lock, flags); 83524723d1bSJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) 83624723d1bSJohannes Berg if (time_after(jiffies, sta->last_rx + exp_time)) { 83724723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG 8380c68ae26SJohannes Berg printk(KERN_DEBUG "%s: expiring inactive STA %pM\n", 83947846c9bSJohannes Berg sdata->name, sta->sta.addr); 84024723d1bSJohannes Berg #endif 84124723d1bSJohannes Berg __sta_info_unlink(&sta); 84224723d1bSJohannes Berg if (sta) 84324723d1bSJohannes Berg list_add(&sta->list, &tmp_list); 84424723d1bSJohannes Berg } 84524723d1bSJohannes Berg spin_unlock_irqrestore(&local->sta_lock, flags); 84624723d1bSJohannes Berg 84724723d1bSJohannes Berg list_for_each_entry_safe(sta, tmp, &tmp_list, list) 84824723d1bSJohannes Berg sta_info_destroy(sta); 84924723d1bSJohannes Berg } 85017741cdcSJohannes Berg 8515ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta_by_hw(struct ieee80211_hw *hw, 85217741cdcSJohannes Berg const u8 *addr) 85317741cdcSJohannes Berg { 854abe60632SJohannes Berg struct sta_info *sta, *nxt; 85517741cdcSJohannes Berg 856abe60632SJohannes Berg /* Just return a random station ... first in list ... */ 857abe60632SJohannes Berg for_each_sta_info(hw_to_local(hw), addr, sta, nxt) 85817741cdcSJohannes Berg return &sta->sta; 859abe60632SJohannes Berg return NULL; 86017741cdcSJohannes Berg } 8615ed176e1SJohannes Berg EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_hw); 8625ed176e1SJohannes Berg 8635ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, 8645ed176e1SJohannes Berg const u8 *addr) 8655ed176e1SJohannes Berg { 8665ed176e1SJohannes Berg struct ieee80211_sub_if_data *sdata; 8675ed176e1SJohannes Berg 8685ed176e1SJohannes Berg if (!vif) 8695ed176e1SJohannes Berg return NULL; 8705ed176e1SJohannes Berg 8715ed176e1SJohannes Berg sdata = vif_to_sdata(vif); 8725ed176e1SJohannes Berg 8735ed176e1SJohannes Berg return ieee80211_find_sta_by_hw(&sdata->local->hw, addr); 8745ed176e1SJohannes Berg } 87517741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta); 876af818581SJohannes Berg 877af818581SJohannes Berg /* powersave support code */ 878af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) 879af818581SJohannes Berg { 880af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 881af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 882af818581SJohannes Berg int sent, buffered; 883af818581SJohannes Berg 88412375ef9SJohannes Berg drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 885af818581SJohannes Berg 886af818581SJohannes Berg if (!skb_queue_empty(&sta->ps_tx_buf)) 887af818581SJohannes Berg sta_info_clear_tim_bit(sta); 888af818581SJohannes Berg 889af818581SJohannes Berg /* Send all buffered frames to the station */ 890af818581SJohannes Berg sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered); 891af818581SJohannes Berg buffered = ieee80211_add_pending_skbs(local, &sta->ps_tx_buf); 892af818581SJohannes Berg sent += buffered; 893af818581SJohannes Berg local->total_ps_buffered -= buffered; 894af818581SJohannes Berg 895af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 896af818581SJohannes Berg printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames " 89747846c9bSJohannes Berg "since STA not sleeping anymore\n", sdata->name, 898af818581SJohannes Berg sta->sta.addr, sta->sta.aid, sent - buffered, buffered); 899af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 900af818581SJohannes Berg } 901af818581SJohannes Berg 902af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 903af818581SJohannes Berg { 904af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 905af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 906af818581SJohannes Berg struct sk_buff *skb; 907af818581SJohannes Berg int no_pending_pkts; 908af818581SJohannes Berg 909af818581SJohannes Berg skb = skb_dequeue(&sta->tx_filtered); 910af818581SJohannes Berg if (!skb) { 911af818581SJohannes Berg skb = skb_dequeue(&sta->ps_tx_buf); 912af818581SJohannes Berg if (skb) 913af818581SJohannes Berg local->total_ps_buffered--; 914af818581SJohannes Berg } 915af818581SJohannes Berg no_pending_pkts = skb_queue_empty(&sta->tx_filtered) && 916af818581SJohannes Berg skb_queue_empty(&sta->ps_tx_buf); 917af818581SJohannes Berg 918af818581SJohannes Berg if (skb) { 919af818581SJohannes Berg struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 920af818581SJohannes Berg struct ieee80211_hdr *hdr = 921af818581SJohannes Berg (struct ieee80211_hdr *) skb->data; 922af818581SJohannes Berg 923af818581SJohannes Berg /* 924af818581SJohannes Berg * Tell TX path to send this frame even though the STA may 925af818581SJohannes Berg * still remain is PS mode after this frame exchange. 926af818581SJohannes Berg */ 927af818581SJohannes Berg info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE; 928af818581SJohannes Berg 929af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 930af818581SJohannes Berg printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n", 931af818581SJohannes Berg sta->sta.addr, sta->sta.aid, 932af818581SJohannes Berg skb_queue_len(&sta->ps_tx_buf)); 933af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 934af818581SJohannes Berg 935af818581SJohannes Berg /* Use MoreData flag to indicate whether there are more 936af818581SJohannes Berg * buffered frames for this STA */ 937af818581SJohannes Berg if (no_pending_pkts) 938af818581SJohannes Berg hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 939af818581SJohannes Berg else 940af818581SJohannes Berg hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA); 941af818581SJohannes Berg 942af818581SJohannes Berg ieee80211_add_pending_skb(local, skb); 943af818581SJohannes Berg 944af818581SJohannes Berg if (no_pending_pkts) 945af818581SJohannes Berg sta_info_clear_tim_bit(sta); 946af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG 947af818581SJohannes Berg } else { 948af818581SJohannes Berg /* 949af818581SJohannes Berg * FIXME: This can be the result of a race condition between 950af818581SJohannes Berg * us expiring a frame and the station polling for it. 951af818581SJohannes Berg * Should we send it a null-func frame indicating we 952af818581SJohannes Berg * have nothing buffered for it? 953af818581SJohannes Berg */ 954af818581SJohannes Berg printk(KERN_DEBUG "%s: STA %pM sent PS Poll even " 955af818581SJohannes Berg "though there are no buffered frames for it\n", 95647846c9bSJohannes Berg sdata->name, sta->sta.addr); 957af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */ 958af818581SJohannes Berg } 959af818581SJohannes Berg } 960af818581SJohannes Berg 961af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 962af818581SJohannes Berg struct ieee80211_sta *pubsta, bool block) 963af818581SJohannes Berg { 964af818581SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 965af818581SJohannes Berg 966af818581SJohannes Berg if (block) 967af818581SJohannes Berg set_sta_flags(sta, WLAN_STA_PS_DRIVER); 968af818581SJohannes Berg else 969af818581SJohannes Berg ieee80211_queue_work(hw, &sta->drv_unblock_wk); 970af818581SJohannes Berg } 971af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake); 972