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> 12888d04dfSFelix Fietkau #include <linux/etherdevice.h> 13f0706e82SJiri Benc #include <linux/netdevice.h> 14f0706e82SJiri Benc #include <linux/types.h> 15f0706e82SJiri Benc #include <linux/slab.h> 16f0706e82SJiri Benc #include <linux/skbuff.h> 17f0706e82SJiri Benc #include <linux/if_arp.h> 180d174406SJohannes Berg #include <linux/timer.h> 19d0709a65SJohannes Berg #include <linux/rtnetlink.h> 20f0706e82SJiri Benc 21f0706e82SJiri Benc #include <net/mac80211.h> 22f0706e82SJiri Benc #include "ieee80211_i.h" 2324487981SJohannes Berg #include "driver-ops.h" 242c8dccc7SJohannes Berg #include "rate.h" 25f0706e82SJiri Benc #include "sta_info.h" 26e9f207f0SJiri Benc #include "debugfs_sta.h" 27ee385855SLuis Carlos Cobo #include "mesh.h" 28ce662b44SJohannes Berg #include "wme.h" 29f0706e82SJiri Benc 30d0709a65SJohannes Berg /** 31d0709a65SJohannes Berg * DOC: STA information lifetime rules 32d0709a65SJohannes Berg * 33d0709a65SJohannes Berg * STA info structures (&struct sta_info) are managed in a hash table 34d0709a65SJohannes Berg * for faster lookup and a list for iteration. They are managed using 35d0709a65SJohannes Berg * RCU, i.e. access to the list and hash table is protected by RCU. 36d0709a65SJohannes Berg * 3734e89507SJohannes Berg * Upon allocating a STA info structure with sta_info_alloc(), the caller 3834e89507SJohannes Berg * owns that structure. It must then insert it into the hash table using 3934e89507SJohannes Berg * either sta_info_insert() or sta_info_insert_rcu(); only in the latter 4034e89507SJohannes Berg * case (which acquires an rcu read section but must not be called from 4134e89507SJohannes Berg * within one) will the pointer still be valid after the call. Note that 4234e89507SJohannes Berg * the caller may not do much with the STA info before inserting it, in 4334e89507SJohannes Berg * particular, it may not start any mesh peer link management or add 4434e89507SJohannes Berg * encryption keys. 4593e5deb1SJohannes Berg * 4693e5deb1SJohannes Berg * When the insertion fails (sta_info_insert()) returns non-zero), the 4793e5deb1SJohannes Berg * structure will have been freed by sta_info_insert()! 48d0709a65SJohannes Berg * 4934e89507SJohannes Berg * Station entries are added by mac80211 when you establish a link with a 507e189a12SLuis R. Rodriguez * peer. This means different things for the different type of interfaces 517e189a12SLuis R. Rodriguez * we support. For a regular station this mean we add the AP sta when we 5225985edcSLucas De Marchi * receive an association response from the AP. For IBSS this occurs when 5334e89507SJohannes Berg * get to know about a peer on the same IBSS. For WDS we add the sta for 5425985edcSLucas De Marchi * the peer immediately upon device open. When using AP mode we add stations 5534e89507SJohannes Berg * for each respective station upon request from userspace through nl80211. 567e189a12SLuis R. Rodriguez * 5734e89507SJohannes Berg * In order to remove a STA info structure, various sta_info_destroy_*() 5834e89507SJohannes Berg * calls are available. 59d0709a65SJohannes Berg * 6034e89507SJohannes Berg * There is no concept of ownership on a STA entry, each structure is 6134e89507SJohannes Berg * owned by the global hash table/list until it is removed. All users of 6234e89507SJohannes Berg * the structure need to be RCU protected so that the structure won't be 6334e89507SJohannes Berg * freed before they are done using it. 64d0709a65SJohannes Berg */ 65f0706e82SJiri Benc 664d33960bSJohannes Berg /* Caller must hold local->sta_mtx */ 67be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local, 68f0706e82SJiri Benc struct sta_info *sta) 69f0706e82SJiri Benc { 70f0706e82SJiri Benc struct sta_info *s; 71f0706e82SJiri Benc 7240b275b6SJohannes Berg s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)], 734d33960bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 74f0706e82SJiri Benc if (!s) 75be8755e1SMichael Wu return -ENOENT; 76be8755e1SMichael Wu if (s == sta) { 77cf778b00SEric Dumazet rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], 78d0709a65SJohannes Berg s->hnext); 79be8755e1SMichael Wu return 0; 80f0706e82SJiri Benc } 81f0706e82SJiri Benc 8240b275b6SJohannes Berg while (rcu_access_pointer(s->hnext) && 8340b275b6SJohannes Berg rcu_access_pointer(s->hnext) != sta) 8440b275b6SJohannes Berg s = rcu_dereference_protected(s->hnext, 854d33960bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 8640b275b6SJohannes Berg if (rcu_access_pointer(s->hnext)) { 87cf778b00SEric Dumazet rcu_assign_pointer(s->hnext, sta->hnext); 88be8755e1SMichael Wu return 0; 89f0706e82SJiri Benc } 90f0706e82SJiri Benc 91be8755e1SMichael Wu return -ENOENT; 92f0706e82SJiri Benc } 93f0706e82SJiri Benc 94b22cfcfcSEliad Peller static void free_sta_work(struct work_struct *wk) 95b22cfcfcSEliad Peller { 96b22cfcfcSEliad Peller struct sta_info *sta = container_of(wk, struct sta_info, free_sta_wk); 97b22cfcfcSEliad Peller int ac, i; 98b22cfcfcSEliad Peller struct tid_ampdu_tx *tid_tx; 99b22cfcfcSEliad Peller struct ieee80211_sub_if_data *sdata = sta->sdata; 100b22cfcfcSEliad Peller struct ieee80211_local *local = sdata->local; 101b22cfcfcSEliad Peller 102b22cfcfcSEliad Peller /* 103b22cfcfcSEliad Peller * At this point, when being called as call_rcu callback, 104b22cfcfcSEliad Peller * neither mac80211 nor the driver can reference this 105b22cfcfcSEliad Peller * sta struct any more except by still existing timers 106b22cfcfcSEliad Peller * associated with this station that we clean up below. 107b22cfcfcSEliad Peller */ 108b22cfcfcSEliad Peller 109b22cfcfcSEliad Peller if (test_sta_flag(sta, WLAN_STA_PS_STA)) { 110b22cfcfcSEliad Peller BUG_ON(!sdata->bss); 111b22cfcfcSEliad Peller 112b22cfcfcSEliad Peller clear_sta_flag(sta, WLAN_STA_PS_STA); 113b22cfcfcSEliad Peller 114b22cfcfcSEliad Peller atomic_dec(&sdata->bss->num_sta_ps); 115b22cfcfcSEliad Peller sta_info_recalc_tim(sta); 116b22cfcfcSEliad Peller } 117b22cfcfcSEliad Peller 118b22cfcfcSEliad Peller for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 119b22cfcfcSEliad Peller local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]); 120b22cfcfcSEliad Peller __skb_queue_purge(&sta->ps_tx_buf[ac]); 121b22cfcfcSEliad Peller __skb_queue_purge(&sta->tx_filtered[ac]); 122b22cfcfcSEliad Peller } 123b22cfcfcSEliad Peller 124b22cfcfcSEliad Peller #ifdef CONFIG_MAC80211_MESH 125b22cfcfcSEliad Peller if (ieee80211_vif_is_mesh(&sdata->vif)) { 126b22cfcfcSEliad Peller mesh_accept_plinks_update(sdata); 127b22cfcfcSEliad Peller mesh_plink_deactivate(sta); 128b22cfcfcSEliad Peller del_timer_sync(&sta->plink_timer); 129b22cfcfcSEliad Peller } 130b22cfcfcSEliad Peller #endif 131b22cfcfcSEliad Peller 132b22cfcfcSEliad Peller cancel_work_sync(&sta->drv_unblock_wk); 133b22cfcfcSEliad Peller 134b22cfcfcSEliad Peller /* 135b22cfcfcSEliad Peller * Destroy aggregation state here. It would be nice to wait for the 136b22cfcfcSEliad Peller * driver to finish aggregation stop and then clean up, but for now 137b22cfcfcSEliad Peller * drivers have to handle aggregation stop being requested, followed 138b22cfcfcSEliad Peller * directly by station destruction. 139b22cfcfcSEliad Peller */ 140b22cfcfcSEliad Peller for (i = 0; i < STA_TID_NUM; i++) { 141b22cfcfcSEliad Peller tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]); 142b22cfcfcSEliad Peller if (!tid_tx) 143b22cfcfcSEliad Peller continue; 144b22cfcfcSEliad Peller __skb_queue_purge(&tid_tx->pending); 145b22cfcfcSEliad Peller kfree(tid_tx); 146b22cfcfcSEliad Peller } 147b22cfcfcSEliad Peller 148b22cfcfcSEliad Peller sta_info_free(local, sta); 149b22cfcfcSEliad Peller } 150b22cfcfcSEliad Peller 151b22cfcfcSEliad Peller static void free_sta_rcu(struct rcu_head *h) 152b22cfcfcSEliad Peller { 153b22cfcfcSEliad Peller struct sta_info *sta = container_of(h, struct sta_info, rcu_head); 154b22cfcfcSEliad Peller 155b22cfcfcSEliad Peller ieee80211_queue_work(&sta->local->hw, &sta->free_sta_wk); 156b22cfcfcSEliad Peller } 157b22cfcfcSEliad Peller 158d0709a65SJohannes Berg /* protected by RCU */ 159abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata, 160abe60632SJohannes Berg const u8 *addr) 16143ba7e95SJohannes Berg { 162abe60632SJohannes Berg struct ieee80211_local *local = sdata->local; 16343ba7e95SJohannes Berg struct sta_info *sta; 16443ba7e95SJohannes Berg 1650379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1660379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 16743ba7e95SJohannes Berg while (sta) { 168abe60632SJohannes Berg if (sta->sdata == sdata && 169b203ca39SJoe Perches ether_addr_equal(sta->sta.addr, addr)) 17043ba7e95SJohannes Berg break; 1710379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1720379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 17343ba7e95SJohannes Berg } 17443ba7e95SJohannes Berg return sta; 17543ba7e95SJohannes Berg } 17643ba7e95SJohannes Berg 1770e5ded5aSFelix Fietkau /* 1780e5ded5aSFelix Fietkau * Get sta info either from the specified interface 1790e5ded5aSFelix Fietkau * or from one of its vlans 1800e5ded5aSFelix Fietkau */ 1810e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata, 1820e5ded5aSFelix Fietkau const u8 *addr) 1830e5ded5aSFelix Fietkau { 1840e5ded5aSFelix Fietkau struct ieee80211_local *local = sdata->local; 1850e5ded5aSFelix Fietkau struct sta_info *sta; 1860e5ded5aSFelix Fietkau 1870379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1880379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1890e5ded5aSFelix Fietkau while (sta) { 1900e5ded5aSFelix Fietkau if ((sta->sdata == sdata || 191a2c1e3daSJohannes Berg (sta->sdata->bss && sta->sdata->bss == sdata->bss)) && 192b203ca39SJoe Perches ether_addr_equal(sta->sta.addr, addr)) 1930e5ded5aSFelix Fietkau break; 1940379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1950379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1960e5ded5aSFelix Fietkau } 1970e5ded5aSFelix Fietkau return sta; 1980e5ded5aSFelix Fietkau } 1990e5ded5aSFelix Fietkau 2003b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, 2013b53fde8SJohannes Berg int idx) 202ee385855SLuis Carlos Cobo { 2033b53fde8SJohannes Berg struct ieee80211_local *local = sdata->local; 204ee385855SLuis Carlos Cobo struct sta_info *sta; 205ee385855SLuis Carlos Cobo int i = 0; 206ee385855SLuis Carlos Cobo 207d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) { 2083b53fde8SJohannes Berg if (sdata != sta->sdata) 2092a8ca29aSLuis Carlos Cobo continue; 210ee385855SLuis Carlos Cobo if (i < idx) { 211ee385855SLuis Carlos Cobo ++i; 212ee385855SLuis Carlos Cobo continue; 213ee385855SLuis Carlos Cobo } 2142a8ca29aSLuis Carlos Cobo return sta; 215ee385855SLuis Carlos Cobo } 216ee385855SLuis Carlos Cobo 217ee385855SLuis Carlos Cobo return NULL; 218ee385855SLuis Carlos Cobo } 219f0706e82SJiri Benc 22093e5deb1SJohannes Berg /** 221d9a7ddb0SJohannes Berg * sta_info_free - free STA 22293e5deb1SJohannes Berg * 2236ef307bcSRandy Dunlap * @local: pointer to the global information 22493e5deb1SJohannes Berg * @sta: STA info to free 22593e5deb1SJohannes Berg * 22693e5deb1SJohannes Berg * This function must undo everything done by sta_info_alloc() 227d9a7ddb0SJohannes Berg * that may happen before sta_info_insert(). It may only be 228d9a7ddb0SJohannes Berg * called when sta_info_insert() has not been attempted (and 229d9a7ddb0SJohannes Berg * if that fails, the station is freed anyway.) 23093e5deb1SJohannes Berg */ 231d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta) 23293e5deb1SJohannes Berg { 233889cbb91SJohannes Berg if (sta->rate_ctrl) 2344b7679a5SJohannes Berg rate_control_free_sta(sta); 23593e5deb1SJohannes Berg 236bdcbd8e0SJohannes Berg sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr); 23793e5deb1SJohannes Berg 23893e5deb1SJohannes Berg kfree(sta); 23993e5deb1SJohannes Berg } 24093e5deb1SJohannes Berg 2414d33960bSJohannes Berg /* Caller must hold local->sta_mtx */ 242d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local, 243d0709a65SJohannes Berg struct sta_info *sta) 244f0706e82SJiri Benc { 2454d33960bSJohannes Berg lockdep_assert_held(&local->sta_mtx); 24617741cdcSJohannes Berg sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)]; 247cf778b00SEric Dumazet rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta); 248f0706e82SJiri Benc } 249f0706e82SJiri Benc 250af818581SJohannes Berg static void sta_unblock(struct work_struct *wk) 251af818581SJohannes Berg { 252af818581SJohannes Berg struct sta_info *sta; 253af818581SJohannes Berg 254af818581SJohannes Berg sta = container_of(wk, struct sta_info, drv_unblock_wk); 255af818581SJohannes Berg 256af818581SJohannes Berg if (sta->dead) 257af818581SJohannes Berg return; 258af818581SJohannes Berg 25954420473SHelmut Schaa if (!test_sta_flag(sta, WLAN_STA_PS_STA)) { 26054420473SHelmut Schaa local_bh_disable(); 261af818581SJohannes Berg ieee80211_sta_ps_deliver_wakeup(sta); 26254420473SHelmut Schaa local_bh_enable(); 26354420473SHelmut Schaa } else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) { 264c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 265ce662b44SJohannes Berg 266ce662b44SJohannes Berg local_bh_disable(); 267af818581SJohannes Berg ieee80211_sta_ps_deliver_poll_response(sta); 268ce662b44SJohannes Berg local_bh_enable(); 269c2c98fdeSJohannes Berg } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) { 270c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 271ce662b44SJohannes Berg 272ce662b44SJohannes Berg local_bh_disable(); 27347086fc5SJohannes Berg ieee80211_sta_ps_deliver_uapsd(sta); 274ce662b44SJohannes Berg local_bh_enable(); 27550a9432dSJohannes Berg } else 276c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 277af818581SJohannes Berg } 278af818581SJohannes Berg 279af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local, 280af65cd96SJohannes Berg struct sta_info *sta, gfp_t gfp) 281af65cd96SJohannes Berg { 282af65cd96SJohannes Berg if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) 283af65cd96SJohannes Berg return 0; 284af65cd96SJohannes Berg 285889cbb91SJohannes Berg sta->rate_ctrl = local->rate_ctrl; 286af65cd96SJohannes Berg sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, 287af65cd96SJohannes Berg &sta->sta, gfp); 288889cbb91SJohannes Berg if (!sta->rate_ctrl_priv) 289af65cd96SJohannes Berg return -ENOMEM; 290af65cd96SJohannes Berg 291af65cd96SJohannes Berg return 0; 292af65cd96SJohannes Berg } 293af65cd96SJohannes Berg 29473651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, 29556544160SJohannes Berg const u8 *addr, gfp_t gfp) 296f0706e82SJiri Benc { 297d0709a65SJohannes Berg struct ieee80211_local *local = sdata->local; 298f0706e82SJiri Benc struct sta_info *sta; 299ebe27c91SMohammed Shafi Shajakhan struct timespec uptime; 30016c5f15cSRon Rindjunsky int i; 301f0706e82SJiri Benc 30217741cdcSJohannes Berg sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp); 303f0706e82SJiri Benc if (!sta) 30473651ee6SJohannes Berg return NULL; 305f0706e82SJiri Benc 30607346f81SJohannes Berg spin_lock_init(&sta->lock); 307af818581SJohannes Berg INIT_WORK(&sta->drv_unblock_wk, sta_unblock); 308b22cfcfcSEliad Peller INIT_WORK(&sta->free_sta_wk, free_sta_work); 30967c282c0SJohannes Berg INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work); 310a93e3644SJohannes Berg mutex_init(&sta->ampdu_mlme.mtx); 31107346f81SJohannes Berg 31217741cdcSJohannes Berg memcpy(sta->sta.addr, addr, ETH_ALEN); 313d0709a65SJohannes Berg sta->local = local; 314d0709a65SJohannes Berg sta->sdata = sdata; 3158bc8aecdSFelix Fietkau sta->last_rx = jiffies; 316f0706e82SJiri Benc 31771ec375cSJohannes Berg sta->sta_state = IEEE80211_STA_NONE; 31871ec375cSJohannes Berg 319ebe27c91SMohammed Shafi Shajakhan do_posix_clock_monotonic_gettime(&uptime); 320ebe27c91SMohammed Shafi Shajakhan sta->last_connected = uptime.tv_sec; 321541a45a1SBruno Randolf ewma_init(&sta->avg_signal, 1024, 8); 322541a45a1SBruno Randolf 323af65cd96SJohannes Berg if (sta_prepare_rate_control(local, sta, gfp)) { 324f0706e82SJiri Benc kfree(sta); 32573651ee6SJohannes Berg return NULL; 326f0706e82SJiri Benc } 327f0706e82SJiri Benc 32816c5f15cSRon Rindjunsky for (i = 0; i < STA_TID_NUM; i++) { 329a622ab72SJohannes Berg /* 330a622ab72SJohannes Berg * timer_to_tid must be initialized with identity mapping 331a622ab72SJohannes Berg * to enable session_timer's data differentiation. See 332a622ab72SJohannes Berg * sta_rx_agg_session_timer_expired for usage. 333a622ab72SJohannes Berg */ 33416c5f15cSRon Rindjunsky sta->timer_to_tid[i] = i; 33516c5f15cSRon Rindjunsky } 336948d887dSJohannes Berg for (i = 0; i < IEEE80211_NUM_ACS; i++) { 337948d887dSJohannes Berg skb_queue_head_init(&sta->ps_tx_buf[i]); 338948d887dSJohannes Berg skb_queue_head_init(&sta->tx_filtered[i]); 339948d887dSJohannes Berg } 34073651ee6SJohannes Berg 341cccaec98SSenthil Balasubramanian for (i = 0; i < NUM_RX_DATA_QUEUES; i++) 3424be929beSAlexey Dobriyan sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); 343cccaec98SSenthil Balasubramanian 344bdcbd8e0SJohannes Berg sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr); 34573651ee6SJohannes Berg 34603e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH 34757cf8043SJavier Cardona sta->plink_state = NL80211_PLINK_LISTEN; 34803e4497eSJohannes Berg init_timer(&sta->plink_timer); 34903e4497eSJohannes Berg #endif 35003e4497eSJohannes Berg 35173651ee6SJohannes Berg return sta; 35273651ee6SJohannes Berg } 35373651ee6SJohannes Berg 3548c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta) 35534e89507SJohannes Berg { 35634e89507SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 35734e89507SJohannes Berg 35803e4497eSJohannes Berg /* 35903e4497eSJohannes Berg * Can't be a WARN_ON because it can be triggered through a race: 36003e4497eSJohannes Berg * something inserts a STA (on one CPU) without holding the RTNL 36103e4497eSJohannes Berg * and another CPU turns off the net device. 36203e4497eSJohannes Berg */ 3638c71df7aSGuy Eilam if (unlikely(!ieee80211_sdata_running(sdata))) 3648c71df7aSGuy Eilam return -ENETDOWN; 36503e4497eSJohannes Berg 366b203ca39SJoe Perches if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) || 3678c71df7aSGuy Eilam is_multicast_ether_addr(sta->sta.addr))) 3688c71df7aSGuy Eilam return -EINVAL; 3698c71df7aSGuy Eilam 3708c71df7aSGuy Eilam return 0; 37193e5deb1SJohannes Berg } 37244213b5eSJohannes Berg 373f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local, 374f09603a2SJohannes Berg struct ieee80211_sub_if_data *sdata, 375f09603a2SJohannes Berg struct sta_info *sta) 376f09603a2SJohannes Berg { 377f09603a2SJohannes Berg enum ieee80211_sta_state state; 378f09603a2SJohannes Berg int err = 0; 379f09603a2SJohannes Berg 380f09603a2SJohannes Berg for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) { 381f09603a2SJohannes Berg err = drv_sta_state(local, sdata, sta, state, state + 1); 382f09603a2SJohannes Berg if (err) 383f09603a2SJohannes Berg break; 384f09603a2SJohannes Berg } 385f09603a2SJohannes Berg 386f09603a2SJohannes Berg if (!err) { 387a4ec45a4SJohannes Berg /* 388a4ec45a4SJohannes Berg * Drivers using legacy sta_add/sta_remove callbacks only 389a4ec45a4SJohannes Berg * get uploaded set to true after sta_add is called. 390a4ec45a4SJohannes Berg */ 391a4ec45a4SJohannes Berg if (!local->ops->sta_add) 392f09603a2SJohannes Berg sta->uploaded = true; 393f09603a2SJohannes Berg return 0; 394f09603a2SJohannes Berg } 395f09603a2SJohannes Berg 396f09603a2SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { 397bdcbd8e0SJohannes Berg sdata_info(sdata, 398bdcbd8e0SJohannes Berg "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n", 399bdcbd8e0SJohannes Berg sta->sta.addr, state + 1, err); 400f09603a2SJohannes Berg err = 0; 401f09603a2SJohannes Berg } 402f09603a2SJohannes Berg 403f09603a2SJohannes Berg /* unwind on error */ 404f09603a2SJohannes Berg for (; state > IEEE80211_STA_NOTEXIST; state--) 405f09603a2SJohannes Berg WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1)); 406f09603a2SJohannes Berg 407f09603a2SJohannes Berg return err; 408f09603a2SJohannes Berg } 409f09603a2SJohannes Berg 41034e89507SJohannes Berg /* 4118c71df7aSGuy Eilam * should be called with sta_mtx locked 4128c71df7aSGuy Eilam * this function replaces the mutex lock 4138c71df7aSGuy Eilam * with a RCU lock 4148c71df7aSGuy Eilam */ 4154d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU) 4168c71df7aSGuy Eilam { 4178c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 4188c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 4197852e361SJohannes Berg struct station_info sinfo; 4208c71df7aSGuy Eilam int err = 0; 4218c71df7aSGuy Eilam 4228c71df7aSGuy Eilam lockdep_assert_held(&local->sta_mtx); 4238c71df7aSGuy Eilam 4247852e361SJohannes Berg /* check if STA exists already */ 4257852e361SJohannes Berg if (sta_info_get_bss(sdata, sta->sta.addr)) { 4264d33960bSJohannes Berg err = -EEXIST; 4274d33960bSJohannes Berg goto out_err; 42834e89507SJohannes Berg } 42934e89507SJohannes Berg 4304d33960bSJohannes Berg /* notify driver */ 431f09603a2SJohannes Berg err = sta_info_insert_drv_state(local, sdata, sta); 432f09603a2SJohannes Berg if (err) 433f09603a2SJohannes Berg goto out_err; 4344d33960bSJohannes Berg 4354d33960bSJohannes Berg local->num_sta++; 4364d33960bSJohannes Berg local->sta_generation++; 4374d33960bSJohannes Berg smp_mb(); 4384d33960bSJohannes Berg 4394d33960bSJohannes Berg /* make the station visible */ 4404d33960bSJohannes Berg sta_info_hash_add(local, sta); 4414d33960bSJohannes Berg 442794454ceSArik Nemtsov list_add_rcu(&sta->list, &local->sta_list); 44383d5cc01SJohannes Berg 44483d5cc01SJohannes Berg set_sta_flag(sta, WLAN_STA_INSERTED); 4454d33960bSJohannes Berg 4464d33960bSJohannes Berg ieee80211_sta_debugfs_add(sta); 4474d33960bSJohannes Berg rate_control_add_sta_debugfs(sta); 4484d33960bSJohannes Berg 4494d33960bSJohannes Berg memset(&sinfo, 0, sizeof(sinfo)); 4504d33960bSJohannes Berg sinfo.filled = 0; 4514d33960bSJohannes Berg sinfo.generation = local->sta_generation; 4524d33960bSJohannes Berg cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL); 453d0709a65SJohannes Berg 454bdcbd8e0SJohannes Berg sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr); 455f0706e82SJiri Benc 45634e89507SJohannes Berg /* move reference to rcu-protected */ 45734e89507SJohannes Berg rcu_read_lock(); 45834e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 459e9f207f0SJiri Benc 46073651ee6SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 46173651ee6SJohannes Berg mesh_accept_plinks_update(sdata); 46273651ee6SJohannes Berg 46373651ee6SJohannes Berg return 0; 4644d33960bSJohannes Berg out_err: 4654d33960bSJohannes Berg mutex_unlock(&local->sta_mtx); 4664d33960bSJohannes Berg rcu_read_lock(); 4674d33960bSJohannes Berg return err; 4688c71df7aSGuy Eilam } 4698c71df7aSGuy Eilam 4708c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) 4718c71df7aSGuy Eilam { 4728c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 4738c71df7aSGuy Eilam int err = 0; 4748c71df7aSGuy Eilam 4754d33960bSJohannes Berg might_sleep(); 4764d33960bSJohannes Berg 4778c71df7aSGuy Eilam err = sta_info_insert_check(sta); 4788c71df7aSGuy Eilam if (err) { 4798c71df7aSGuy Eilam rcu_read_lock(); 4808c71df7aSGuy Eilam goto out_free; 4818c71df7aSGuy Eilam } 4828c71df7aSGuy Eilam 483004c872eSJohannes Berg mutex_lock(&local->sta_mtx); 484004c872eSJohannes Berg 4854d33960bSJohannes Berg err = sta_info_insert_finish(sta); 4868c71df7aSGuy Eilam if (err) 487004c872eSJohannes Berg goto out_free; 488d0709a65SJohannes Berg 489004c872eSJohannes Berg return 0; 49093e5deb1SJohannes Berg out_free: 49193e5deb1SJohannes Berg BUG_ON(!err); 492d9a7ddb0SJohannes Berg sta_info_free(local, sta); 49393e5deb1SJohannes Berg return err; 494f0706e82SJiri Benc } 495f0706e82SJiri Benc 49634e89507SJohannes Berg int sta_info_insert(struct sta_info *sta) 49734e89507SJohannes Berg { 49834e89507SJohannes Berg int err = sta_info_insert_rcu(sta); 49934e89507SJohannes Berg 50034e89507SJohannes Berg rcu_read_unlock(); 50134e89507SJohannes Berg 50234e89507SJohannes Berg return err; 50334e89507SJohannes Berg } 50434e89507SJohannes Berg 505f0706e82SJiri Benc static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid) 506004c872eSJohannes Berg { 507004c872eSJohannes Berg /* 508004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 509004c872eSJohannes Berg * so this line may not be changed to use the __set_bit() format. 510004c872eSJohannes Berg */ 511004c872eSJohannes Berg bss->tim[aid / 8] |= (1 << (aid % 8)); 512004c872eSJohannes Berg } 513004c872eSJohannes Berg 514004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid) 515004c872eSJohannes Berg { 516004c872eSJohannes Berg /* 517004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 518004c872eSJohannes Berg * so this line may not be changed to use the __clear_bit() format. 519004c872eSJohannes Berg */ 520004c872eSJohannes Berg bss->tim[aid / 8] &= ~(1 << (aid % 8)); 521004c872eSJohannes Berg } 522004c872eSJohannes Berg 523948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac) 524004c872eSJohannes Berg { 525948d887dSJohannes Berg /* If we ever support TIDs > 7, this obviously needs to be adjusted */ 526948d887dSJohannes Berg switch (ac) { 527948d887dSJohannes Berg case IEEE80211_AC_VO: 528948d887dSJohannes Berg return BIT(6) | BIT(7); 529948d887dSJohannes Berg case IEEE80211_AC_VI: 530948d887dSJohannes Berg return BIT(4) | BIT(5); 531948d887dSJohannes Berg case IEEE80211_AC_BE: 532948d887dSJohannes Berg return BIT(0) | BIT(3); 533948d887dSJohannes Berg case IEEE80211_AC_BK: 534948d887dSJohannes Berg return BIT(1) | BIT(2); 535948d887dSJohannes Berg default: 536948d887dSJohannes Berg WARN_ON(1); 537948d887dSJohannes Berg return 0; 538d0709a65SJohannes Berg } 539004c872eSJohannes Berg } 540004c872eSJohannes Berg 541c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta) 542004c872eSJohannes Berg { 543c868cb35SJohannes Berg struct ieee80211_local *local = sta->local; 544c868cb35SJohannes Berg struct ieee80211_if_ap *bss = sta->sdata->bss; 545d0709a65SJohannes Berg unsigned long flags; 546948d887dSJohannes Berg bool indicate_tim = false; 547948d887dSJohannes Berg u8 ignore_for_tim = sta->sta.uapsd_queues; 548948d887dSJohannes Berg int ac; 549004c872eSJohannes Berg 550c868cb35SJohannes Berg if (WARN_ON_ONCE(!sta->sdata->bss)) 551c868cb35SJohannes Berg return; 5523e122be0SJohannes Berg 553c868cb35SJohannes Berg /* No need to do anything if the driver does all */ 554c868cb35SJohannes Berg if (local->hw.flags & IEEE80211_HW_AP_LINK_PS) 555c868cb35SJohannes Berg return; 556004c872eSJohannes Berg 557c868cb35SJohannes Berg if (sta->dead) 558c868cb35SJohannes Berg goto done; 5593e122be0SJohannes Berg 560948d887dSJohannes Berg /* 561948d887dSJohannes Berg * If all ACs are delivery-enabled then we should build 562948d887dSJohannes Berg * the TIM bit for all ACs anyway; if only some are then 563948d887dSJohannes Berg * we ignore those and build the TIM bit using only the 564948d887dSJohannes Berg * non-enabled ones. 565948d887dSJohannes Berg */ 566948d887dSJohannes Berg if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1) 567948d887dSJohannes Berg ignore_for_tim = 0; 568948d887dSJohannes Berg 569948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 570948d887dSJohannes Berg unsigned long tids; 571948d887dSJohannes Berg 572948d887dSJohannes Berg if (ignore_for_tim & BIT(ac)) 573948d887dSJohannes Berg continue; 574948d887dSJohannes Berg 575948d887dSJohannes Berg indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) || 576948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac]); 577948d887dSJohannes Berg if (indicate_tim) 578948d887dSJohannes Berg break; 579948d887dSJohannes Berg 580948d887dSJohannes Berg tids = ieee80211_tids_for_ac(ac); 581948d887dSJohannes Berg 582948d887dSJohannes Berg indicate_tim |= 583948d887dSJohannes Berg sta->driver_buffered_tids & tids; 584004c872eSJohannes Berg } 585004c872eSJohannes Berg 586c868cb35SJohannes Berg done: 5874d33960bSJohannes Berg spin_lock_irqsave(&local->tim_lock, flags); 588004c872eSJohannes Berg 589948d887dSJohannes Berg if (indicate_tim) 590c868cb35SJohannes Berg __bss_tim_set(bss, sta->sta.aid); 591c868cb35SJohannes Berg else 59217741cdcSJohannes Berg __bss_tim_clear(bss, sta->sta.aid); 5933e122be0SJohannes Berg 594c868cb35SJohannes Berg if (local->ops->set_tim) { 595c868cb35SJohannes Berg local->tim_in_locked_section = true; 596948d887dSJohannes Berg drv_set_tim(local, &sta->sta, indicate_tim); 597c868cb35SJohannes Berg local->tim_in_locked_section = false; 598004c872eSJohannes Berg } 599004c872eSJohannes Berg 6004d33960bSJohannes Berg spin_unlock_irqrestore(&local->tim_lock, flags); 601004c872eSJohannes Berg } 602004c872eSJohannes Berg 603cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb) 604f0706e82SJiri Benc { 605e039fa4aSJohannes Berg struct ieee80211_tx_info *info; 606f0706e82SJiri Benc int timeout; 607f0706e82SJiri Benc 608f0706e82SJiri Benc if (!skb) 609cd0b8d89SJohannes Berg return false; 610f0706e82SJiri Benc 611e039fa4aSJohannes Berg info = IEEE80211_SKB_CB(skb); 612f0706e82SJiri Benc 613f0706e82SJiri Benc /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 61457c4d7b4SJohannes Berg timeout = (sta->listen_interval * 61557c4d7b4SJohannes Berg sta->sdata->vif.bss_conf.beacon_int * 61657c4d7b4SJohannes Berg 32 / 15625) * HZ; 617f0706e82SJiri Benc if (timeout < STA_TX_BUFFER_EXPIRE) 618f0706e82SJiri Benc timeout = STA_TX_BUFFER_EXPIRE; 619e039fa4aSJohannes Berg return time_after(jiffies, info->control.jiffies + timeout); 620f0706e82SJiri Benc } 621f0706e82SJiri Benc 622f0706e82SJiri Benc 623948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local, 624948d887dSJohannes Berg struct sta_info *sta, int ac) 625f0706e82SJiri Benc { 626f0706e82SJiri Benc unsigned long flags; 627f0706e82SJiri Benc struct sk_buff *skb; 628f0706e82SJiri Benc 62960750397SJohannes Berg /* 63060750397SJohannes Berg * First check for frames that should expire on the filtered 63160750397SJohannes Berg * queue. Frames here were rejected by the driver and are on 63260750397SJohannes Berg * a separate queue to avoid reordering with normal PS-buffered 63360750397SJohannes Berg * frames. They also aren't accounted for right now in the 63460750397SJohannes Berg * total_ps_buffered counter. 63560750397SJohannes Berg */ 636f0706e82SJiri Benc for (;;) { 637948d887dSJohannes Berg spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 638948d887dSJohannes Berg skb = skb_peek(&sta->tx_filtered[ac]); 63957c4d7b4SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 640948d887dSJohannes Berg skb = __skb_dequeue(&sta->tx_filtered[ac]); 641836341a7SJohannes Berg else 642f0706e82SJiri Benc skb = NULL; 643948d887dSJohannes Berg spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 644f0706e82SJiri Benc 64560750397SJohannes Berg /* 64660750397SJohannes Berg * Frames are queued in order, so if this one 64760750397SJohannes Berg * hasn't expired yet we can stop testing. If 64860750397SJohannes Berg * we actually reached the end of the queue we 64960750397SJohannes Berg * also need to stop, of course. 65060750397SJohannes Berg */ 65160750397SJohannes Berg if (!skb) 65260750397SJohannes Berg break; 653*d4fa14cdSFelix Fietkau ieee80211_free_txskb(&local->hw, skb); 65460750397SJohannes Berg } 65560750397SJohannes Berg 65660750397SJohannes Berg /* 65760750397SJohannes Berg * Now also check the normal PS-buffered queue, this will 65860750397SJohannes Berg * only find something if the filtered queue was emptied 65960750397SJohannes Berg * since the filtered frames are all before the normal PS 66060750397SJohannes Berg * buffered frames. 66160750397SJohannes Berg */ 662f0706e82SJiri Benc for (;;) { 663948d887dSJohannes Berg spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 664948d887dSJohannes Berg skb = skb_peek(&sta->ps_tx_buf[ac]); 665f0706e82SJiri Benc if (sta_info_buffer_expired(sta, skb)) 666948d887dSJohannes Berg skb = __skb_dequeue(&sta->ps_tx_buf[ac]); 667f0706e82SJiri Benc else 668f0706e82SJiri Benc skb = NULL; 669948d887dSJohannes Berg spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 670f0706e82SJiri Benc 67160750397SJohannes Berg /* 67260750397SJohannes Berg * frames are queued in order, so if this one 67360750397SJohannes Berg * hasn't expired yet (or we reached the end of 67460750397SJohannes Berg * the queue) we can stop testing 67560750397SJohannes Berg */ 676836341a7SJohannes Berg if (!skb) 677836341a7SJohannes Berg break; 678836341a7SJohannes Berg 679f0706e82SJiri Benc local->total_ps_buffered--; 680bdcbd8e0SJohannes Berg ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n", 681bdcbd8e0SJohannes Berg sta->sta.addr); 682*d4fa14cdSFelix Fietkau ieee80211_free_txskb(&local->hw, skb); 683f0706e82SJiri Benc } 6843393a608SJuuso Oikarinen 68560750397SJohannes Berg /* 68660750397SJohannes Berg * Finally, recalculate the TIM bit for this station -- it might 68760750397SJohannes Berg * now be clear because the station was too slow to retrieve its 68860750397SJohannes Berg * frames. 68960750397SJohannes Berg */ 69060750397SJohannes Berg sta_info_recalc_tim(sta); 69160750397SJohannes Berg 69260750397SJohannes Berg /* 69360750397SJohannes Berg * Return whether there are any frames still buffered, this is 69460750397SJohannes Berg * used to check whether the cleanup timer still needs to run, 69560750397SJohannes Berg * if there are no frames we don't need to rearm the timer. 69660750397SJohannes Berg */ 697948d887dSJohannes Berg return !(skb_queue_empty(&sta->ps_tx_buf[ac]) && 698948d887dSJohannes Berg skb_queue_empty(&sta->tx_filtered[ac])); 699948d887dSJohannes Berg } 700948d887dSJohannes Berg 701948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 702948d887dSJohannes Berg struct sta_info *sta) 703948d887dSJohannes Berg { 704948d887dSJohannes Berg bool have_buffered = false; 705948d887dSJohannes Berg int ac; 706948d887dSJohannes Berg 707948d887dSJohannes Berg /* This is only necessary for stations on BSS interfaces */ 708948d887dSJohannes Berg if (!sta->sdata->bss) 709948d887dSJohannes Berg return false; 710948d887dSJohannes Berg 711948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 712948d887dSJohannes Berg have_buffered |= 713948d887dSJohannes Berg sta_info_cleanup_expire_buffered_ac(local, sta, ac); 714948d887dSJohannes Berg 715948d887dSJohannes Berg return have_buffered; 716f0706e82SJiri Benc } 717f0706e82SJiri Benc 71883d5cc01SJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta) 71934e89507SJohannes Berg { 72034e89507SJohannes Berg struct ieee80211_local *local; 72134e89507SJohannes Berg struct ieee80211_sub_if_data *sdata; 722b22cfcfcSEliad Peller int ret, i; 72334e89507SJohannes Berg 72434e89507SJohannes Berg might_sleep(); 72534e89507SJohannes Berg 72634e89507SJohannes Berg if (!sta) 72734e89507SJohannes Berg return -ENOENT; 72834e89507SJohannes Berg 72934e89507SJohannes Berg local = sta->local; 73034e89507SJohannes Berg sdata = sta->sdata; 73134e89507SJohannes Berg 73283d5cc01SJohannes Berg lockdep_assert_held(&local->sta_mtx); 73383d5cc01SJohannes Berg 734098a6070SJohannes Berg /* 735098a6070SJohannes Berg * Before removing the station from the driver and 736098a6070SJohannes Berg * rate control, it might still start new aggregation 737098a6070SJohannes Berg * sessions -- block that to make sure the tear-down 738098a6070SJohannes Berg * will be sufficient. 739098a6070SJohannes Berg */ 740c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_BLOCK_BA); 741582bb505SJohannes Berg ieee80211_sta_tear_down_BA_sessions(sta, false); 742098a6070SJohannes Berg 74334e89507SJohannes Berg ret = sta_info_hash_del(local, sta); 74434e89507SJohannes Berg if (ret) 74534e89507SJohannes Berg return ret; 74634e89507SJohannes Berg 747794454ceSArik Nemtsov list_del_rcu(&sta->list); 7484d33960bSJohannes Berg 7498cb23153SJohannes Berg mutex_lock(&local->key_mtx); 750e31b8213SJohannes Berg for (i = 0; i < NUM_DEFAULT_KEYS; i++) 75140b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i])); 752e31b8213SJohannes Berg if (sta->ptk) 75340b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->ptk)); 7548cb23153SJohannes Berg mutex_unlock(&local->key_mtx); 75534e89507SJohannes Berg 75634e89507SJohannes Berg sta->dead = true; 75734e89507SJohannes Berg 75834e89507SJohannes Berg local->num_sta--; 75934e89507SJohannes Berg local->sta_generation++; 76034e89507SJohannes Berg 76134e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 762a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(sdata->u.vlan.sta, NULL); 76334e89507SJohannes Berg 76483d5cc01SJohannes Berg while (sta->sta_state > IEEE80211_STA_NONE) { 765f09603a2SJohannes Berg ret = sta_info_move_state(sta, sta->sta_state - 1); 766f09603a2SJohannes Berg if (ret) { 76783d5cc01SJohannes Berg WARN_ON_ONCE(1); 76883d5cc01SJohannes Berg break; 76983d5cc01SJohannes Berg } 77083d5cc01SJohannes Berg } 771d9a7ddb0SJohannes Berg 772f09603a2SJohannes Berg if (sta->uploaded) { 773f09603a2SJohannes Berg ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE, 774f09603a2SJohannes Berg IEEE80211_STA_NOTEXIST); 775f09603a2SJohannes Berg WARN_ON_ONCE(ret != 0); 776f09603a2SJohannes Berg } 77734e89507SJohannes Berg 778bdcbd8e0SJohannes Berg sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr); 779bdcbd8e0SJohannes Berg 780ec15e68bSJouni Malinen cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL); 781ec15e68bSJouni Malinen 78234e89507SJohannes Berg rate_control_remove_sta_debugfs(sta); 78334e89507SJohannes Berg ieee80211_sta_debugfs_remove(sta); 78434e89507SJohannes Berg 785b22cfcfcSEliad Peller call_rcu(&sta->rcu_head, free_sta_rcu); 78634e89507SJohannes Berg 78734e89507SJohannes Berg return 0; 78834e89507SJohannes Berg } 78934e89507SJohannes Berg 79034e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) 79134e89507SJohannes Berg { 79234e89507SJohannes Berg struct sta_info *sta; 79334e89507SJohannes Berg int ret; 79434e89507SJohannes Berg 79534e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 7967852e361SJohannes Berg sta = sta_info_get(sdata, addr); 79734e89507SJohannes Berg ret = __sta_info_destroy(sta); 79834e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 79934e89507SJohannes Berg 80034e89507SJohannes Berg return ret; 80134e89507SJohannes Berg } 80234e89507SJohannes Berg 80334e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, 80434e89507SJohannes Berg const u8 *addr) 80534e89507SJohannes Berg { 80634e89507SJohannes Berg struct sta_info *sta; 80734e89507SJohannes Berg int ret; 80834e89507SJohannes Berg 80934e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 8107852e361SJohannes Berg sta = sta_info_get_bss(sdata, addr); 81134e89507SJohannes Berg ret = __sta_info_destroy(sta); 81234e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 81334e89507SJohannes Berg 81434e89507SJohannes Berg return ret; 81534e89507SJohannes Berg } 816f0706e82SJiri Benc 817f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data) 818f0706e82SJiri Benc { 819f0706e82SJiri Benc struct ieee80211_local *local = (struct ieee80211_local *) data; 820f0706e82SJiri Benc struct sta_info *sta; 8213393a608SJuuso Oikarinen bool timer_needed = false; 822f0706e82SJiri Benc 823d0709a65SJohannes Berg rcu_read_lock(); 824d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) 8253393a608SJuuso Oikarinen if (sta_info_cleanup_expire_buffered(local, sta)) 8263393a608SJuuso Oikarinen timer_needed = true; 827d0709a65SJohannes Berg rcu_read_unlock(); 828f0706e82SJiri Benc 8295bb644a0SJohannes Berg if (local->quiescing) 8305bb644a0SJohannes Berg return; 8315bb644a0SJohannes Berg 8323393a608SJuuso Oikarinen if (!timer_needed) 8333393a608SJuuso Oikarinen return; 8343393a608SJuuso Oikarinen 83526d59535SJohannes Berg mod_timer(&local->sta_cleanup, 83626d59535SJohannes Berg round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 837f0706e82SJiri Benc } 838f0706e82SJiri Benc 839f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local) 840f0706e82SJiri Benc { 8414d33960bSJohannes Berg spin_lock_init(&local->tim_lock); 84234e89507SJohannes Berg mutex_init(&local->sta_mtx); 843f0706e82SJiri Benc INIT_LIST_HEAD(&local->sta_list); 844f0706e82SJiri Benc 845b24b8a24SPavel Emelyanov setup_timer(&local->sta_cleanup, sta_info_cleanup, 846b24b8a24SPavel Emelyanov (unsigned long)local); 847f0706e82SJiri Benc } 848f0706e82SJiri Benc 849f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local) 850f0706e82SJiri Benc { 851f0706e82SJiri Benc del_timer(&local->sta_cleanup); 852be8755e1SMichael Wu sta_info_flush(local, NULL); 853f0706e82SJiri Benc } 854f0706e82SJiri Benc 855f0706e82SJiri Benc /** 856f0706e82SJiri Benc * sta_info_flush - flush matching STA entries from the STA table 85744213b5eSJohannes Berg * 85844213b5eSJohannes Berg * Returns the number of removed STA entries. 85944213b5eSJohannes Berg * 860f0706e82SJiri Benc * @local: local interface data 861d0709a65SJohannes Berg * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs 862f0706e82SJiri Benc */ 86344213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local, 864d0709a65SJohannes Berg struct ieee80211_sub_if_data *sdata) 865f0706e82SJiri Benc { 866f0706e82SJiri Benc struct sta_info *sta, *tmp; 86744213b5eSJohannes Berg int ret = 0; 868f0706e82SJiri Benc 869d0709a65SJohannes Berg might_sleep(); 870d0709a65SJohannes Berg 87134e89507SJohannes Berg mutex_lock(&local->sta_mtx); 87234e89507SJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 87334316837SJohannes Berg if (!sdata || sdata == sta->sdata) { 87434e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 87534316837SJohannes Berg ret++; 87634316837SJohannes Berg } 87734e89507SJohannes Berg } 87834e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 87944213b5eSJohannes Berg 88044213b5eSJohannes Berg return ret; 881f0706e82SJiri Benc } 882dc6676b7SJohannes Berg 88324723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 88424723d1bSJohannes Berg unsigned long exp_time) 88524723d1bSJohannes Berg { 88624723d1bSJohannes Berg struct ieee80211_local *local = sdata->local; 88724723d1bSJohannes Berg struct sta_info *sta, *tmp; 88824723d1bSJohannes Berg 88934e89507SJohannes Berg mutex_lock(&local->sta_mtx); 890e46a2cf9SMohammed Shafi Shajakhan 891e46a2cf9SMohammed Shafi Shajakhan list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 892ec2b774eSMarek Lindner if (sdata != sta->sdata) 893ec2b774eSMarek Lindner continue; 894ec2b774eSMarek Lindner 89524723d1bSJohannes Berg if (time_after(jiffies, sta->last_rx + exp_time)) { 896bdcbd8e0SJohannes Berg ibss_dbg(sdata, "expiring inactive STA %pM\n", 897bdcbd8e0SJohannes Berg sta->sta.addr); 89834e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 89924723d1bSJohannes Berg } 900e46a2cf9SMohammed Shafi Shajakhan } 901e46a2cf9SMohammed Shafi Shajakhan 90234e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 90324723d1bSJohannes Berg } 90417741cdcSJohannes Berg 905686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 906686b9cb9SBen Greear const u8 *addr, 907686b9cb9SBen Greear const u8 *localaddr) 90817741cdcSJohannes Berg { 909abe60632SJohannes Berg struct sta_info *sta, *nxt; 91017741cdcSJohannes Berg 911686b9cb9SBen Greear /* 912686b9cb9SBen Greear * Just return a random station if localaddr is NULL 913686b9cb9SBen Greear * ... first in list. 914686b9cb9SBen Greear */ 915f7c65594SJohannes Berg for_each_sta_info(hw_to_local(hw), addr, sta, nxt) { 916686b9cb9SBen Greear if (localaddr && 917b203ca39SJoe Perches !ether_addr_equal(sta->sdata->vif.addr, localaddr)) 918686b9cb9SBen Greear continue; 919f7c65594SJohannes Berg if (!sta->uploaded) 920f7c65594SJohannes Berg return NULL; 92117741cdcSJohannes Berg return &sta->sta; 922f7c65594SJohannes Berg } 923f7c65594SJohannes Berg 924abe60632SJohannes Berg return NULL; 92517741cdcSJohannes Berg } 926686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); 9275ed176e1SJohannes Berg 9285ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, 9295ed176e1SJohannes Berg const u8 *addr) 9305ed176e1SJohannes Berg { 931f7c65594SJohannes Berg struct sta_info *sta; 9325ed176e1SJohannes Berg 9335ed176e1SJohannes Berg if (!vif) 9345ed176e1SJohannes Berg return NULL; 9355ed176e1SJohannes Berg 936f7c65594SJohannes Berg sta = sta_info_get_bss(vif_to_sdata(vif), addr); 937f7c65594SJohannes Berg if (!sta) 938f7c65594SJohannes Berg return NULL; 9395ed176e1SJohannes Berg 940f7c65594SJohannes Berg if (!sta->uploaded) 941f7c65594SJohannes Berg return NULL; 942f7c65594SJohannes Berg 943f7c65594SJohannes Berg return &sta->sta; 9445ed176e1SJohannes Berg } 94517741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta); 946af818581SJohannes Berg 94750a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta) 94850a9432dSJohannes Berg { 94950a9432dSJohannes Berg struct sta_info *sta = _sta; 950608383bfSHelmut Schaa struct ieee80211_sub_if_data *sdata = sta->sdata; 95150a9432dSJohannes Berg 952c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 953608383bfSHelmut Schaa if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA)) 954608383bfSHelmut Schaa atomic_dec(&sdata->bss->num_sta_ps); 95550a9432dSJohannes Berg } 95650a9432dSJohannes Berg 957af818581SJohannes Berg /* powersave support code */ 958af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) 959af818581SJohannes Berg { 960af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 961af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 962948d887dSJohannes Berg struct sk_buff_head pending; 963948d887dSJohannes Berg int filtered = 0, buffered = 0, ac; 964af818581SJohannes Berg 965c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_SP); 96647086fc5SJohannes Berg 967948d887dSJohannes Berg BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1); 968948d887dSJohannes Berg sta->driver_buffered_tids = 0; 969948d887dSJohannes Berg 970d057e5a3SArik Nemtsov if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS)) 97112375ef9SJohannes Berg drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 972af818581SJohannes Berg 973948d887dSJohannes Berg skb_queue_head_init(&pending); 974af818581SJohannes Berg 975af818581SJohannes Berg /* Send all buffered frames to the station */ 976948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 977948d887dSJohannes Berg int count = skb_queue_len(&pending), tmp; 978948d887dSJohannes Berg 979948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending); 980948d887dSJohannes Berg tmp = skb_queue_len(&pending); 981948d887dSJohannes Berg filtered += tmp - count; 982948d887dSJohannes Berg count = tmp; 983948d887dSJohannes Berg 984948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending); 985948d887dSJohannes Berg tmp = skb_queue_len(&pending); 986948d887dSJohannes Berg buffered += tmp - count; 987948d887dSJohannes Berg } 988948d887dSJohannes Berg 989948d887dSJohannes Berg ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta); 990948d887dSJohannes Berg 991af818581SJohannes Berg local->total_ps_buffered -= buffered; 992af818581SJohannes Berg 993c868cb35SJohannes Berg sta_info_recalc_tim(sta); 994c868cb35SJohannes Berg 995bdcbd8e0SJohannes Berg ps_dbg(sdata, 996bdcbd8e0SJohannes Berg "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n", 997bdcbd8e0SJohannes Berg sta->sta.addr, sta->sta.aid, filtered, buffered); 998af818581SJohannes Berg } 999af818581SJohannes Berg 1000ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata, 1001ce662b44SJohannes Berg struct sta_info *sta, int tid, 100240b96408SJohannes Berg enum ieee80211_frame_release_type reason) 1003ce662b44SJohannes Berg { 1004ce662b44SJohannes Berg struct ieee80211_local *local = sdata->local; 1005ce662b44SJohannes Berg struct ieee80211_qos_hdr *nullfunc; 1006ce662b44SJohannes Berg struct sk_buff *skb; 1007ce662b44SJohannes Berg int size = sizeof(*nullfunc); 1008ce662b44SJohannes Berg __le16 fc; 1009c2c98fdeSJohannes Berg bool qos = test_sta_flag(sta, WLAN_STA_WME); 1010ce662b44SJohannes Berg struct ieee80211_tx_info *info; 1011ce662b44SJohannes Berg 1012ce662b44SJohannes Berg if (qos) { 1013ce662b44SJohannes Berg fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1014ce662b44SJohannes Berg IEEE80211_STYPE_QOS_NULLFUNC | 1015ce662b44SJohannes Berg IEEE80211_FCTL_FROMDS); 1016ce662b44SJohannes Berg } else { 1017ce662b44SJohannes Berg size -= 2; 1018ce662b44SJohannes Berg fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1019ce662b44SJohannes Berg IEEE80211_STYPE_NULLFUNC | 1020ce662b44SJohannes Berg IEEE80211_FCTL_FROMDS); 1021ce662b44SJohannes Berg } 1022ce662b44SJohannes Berg 1023ce662b44SJohannes Berg skb = dev_alloc_skb(local->hw.extra_tx_headroom + size); 1024ce662b44SJohannes Berg if (!skb) 1025ce662b44SJohannes Berg return; 1026ce662b44SJohannes Berg 1027ce662b44SJohannes Berg skb_reserve(skb, local->hw.extra_tx_headroom); 1028ce662b44SJohannes Berg 1029ce662b44SJohannes Berg nullfunc = (void *) skb_put(skb, size); 1030ce662b44SJohannes Berg nullfunc->frame_control = fc; 1031ce662b44SJohannes Berg nullfunc->duration_id = 0; 1032ce662b44SJohannes Berg memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN); 1033ce662b44SJohannes Berg memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN); 1034ce662b44SJohannes Berg memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN); 1035ce662b44SJohannes Berg 1036ce662b44SJohannes Berg skb->priority = tid; 1037ce662b44SJohannes Berg skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]); 103859b66255SJohannes Berg if (qos) { 1039ce662b44SJohannes Berg nullfunc->qos_ctrl = cpu_to_le16(tid); 1040ce662b44SJohannes Berg 104140b96408SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_UAPSD) 1042ce662b44SJohannes Berg nullfunc->qos_ctrl |= 1043ce662b44SJohannes Berg cpu_to_le16(IEEE80211_QOS_CTL_EOSP); 1044ce662b44SJohannes Berg } 1045ce662b44SJohannes Berg 1046ce662b44SJohannes Berg info = IEEE80211_SKB_CB(skb); 1047ce662b44SJohannes Berg 1048ce662b44SJohannes Berg /* 1049ce662b44SJohannes Berg * Tell TX path to send this frame even though the 1050ce662b44SJohannes Berg * STA may still remain is PS mode after this frame 1051deeaee19SJohannes Berg * exchange. Also set EOSP to indicate this packet 1052deeaee19SJohannes Berg * ends the poll/service period. 1053ce662b44SJohannes Berg */ 105402f2f1a9SJohannes Berg info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER | 1055deeaee19SJohannes Berg IEEE80211_TX_STATUS_EOSP | 1056ce662b44SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 1057ce662b44SJohannes Berg 105840b96408SJohannes Berg drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false); 105940b96408SJohannes Berg 1060ce662b44SJohannes Berg ieee80211_xmit(sdata, skb); 1061ce662b44SJohannes Berg } 1062ce662b44SJohannes Berg 106347086fc5SJohannes Berg static void 106447086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta, 106547086fc5SJohannes Berg int n_frames, u8 ignored_acs, 106647086fc5SJohannes Berg enum ieee80211_frame_release_type reason) 1067af818581SJohannes Berg { 1068af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1069af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 10704049e09aSJohannes Berg bool found = false; 1071948d887dSJohannes Berg bool more_data = false; 1072948d887dSJohannes Berg int ac; 10734049e09aSJohannes Berg unsigned long driver_release_tids = 0; 107447086fc5SJohannes Berg struct sk_buff_head frames; 107547086fc5SJohannes Berg 1076deeaee19SJohannes Berg /* Service or PS-Poll period starts */ 1077c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_SP); 1078deeaee19SJohannes Berg 107947086fc5SJohannes Berg __skb_queue_head_init(&frames); 1080af818581SJohannes Berg 1081948d887dSJohannes Berg /* 108247086fc5SJohannes Berg * Get response frame(s) and more data bit for it. 1083948d887dSJohannes Berg */ 1084948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 10854049e09aSJohannes Berg unsigned long tids; 10864049e09aSJohannes Berg 108747086fc5SJohannes Berg if (ignored_acs & BIT(ac)) 1088948d887dSJohannes Berg continue; 1089948d887dSJohannes Berg 10904049e09aSJohannes Berg tids = ieee80211_tids_for_ac(ac); 10914049e09aSJohannes Berg 10924049e09aSJohannes Berg if (!found) { 10934049e09aSJohannes Berg driver_release_tids = sta->driver_buffered_tids & tids; 10944049e09aSJohannes Berg if (driver_release_tids) { 10954049e09aSJohannes Berg found = true; 10964049e09aSJohannes Berg } else { 109747086fc5SJohannes Berg struct sk_buff *skb; 109847086fc5SJohannes Berg 109947086fc5SJohannes Berg while (n_frames > 0) { 1100948d887dSJohannes Berg skb = skb_dequeue(&sta->tx_filtered[ac]); 1101948d887dSJohannes Berg if (!skb) { 110247086fc5SJohannes Berg skb = skb_dequeue( 110347086fc5SJohannes Berg &sta->ps_tx_buf[ac]); 1104af818581SJohannes Berg if (skb) 1105af818581SJohannes Berg local->total_ps_buffered--; 1106af818581SJohannes Berg } 110747086fc5SJohannes Berg if (!skb) 110847086fc5SJohannes Berg break; 110947086fc5SJohannes Berg n_frames--; 11104049e09aSJohannes Berg found = true; 111147086fc5SJohannes Berg __skb_queue_tail(&frames, skb); 111247086fc5SJohannes Berg } 1113948d887dSJohannes Berg } 1114948d887dSJohannes Berg 11154049e09aSJohannes Berg /* 11164049e09aSJohannes Berg * If the driver has data on more than one TID then 11174049e09aSJohannes Berg * certainly there's more data if we release just a 11184049e09aSJohannes Berg * single frame now (from a single TID). 11194049e09aSJohannes Berg */ 112047086fc5SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_PSPOLL && 112147086fc5SJohannes Berg hweight16(driver_release_tids) > 1) { 11224049e09aSJohannes Berg more_data = true; 11234049e09aSJohannes Berg driver_release_tids = 11244049e09aSJohannes Berg BIT(ffs(driver_release_tids) - 1); 11254049e09aSJohannes Berg break; 11264049e09aSJohannes Berg } 11274049e09aSJohannes Berg } 1128948d887dSJohannes Berg 1129948d887dSJohannes Berg if (!skb_queue_empty(&sta->tx_filtered[ac]) || 1130948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac])) { 1131948d887dSJohannes Berg more_data = true; 1132948d887dSJohannes Berg break; 1133948d887dSJohannes Berg } 1134948d887dSJohannes Berg } 1135af818581SJohannes Berg 11364049e09aSJohannes Berg if (!found) { 1137ce662b44SJohannes Berg int tid; 11384049e09aSJohannes Berg 1139ce662b44SJohannes Berg /* 1140ce662b44SJohannes Berg * For PS-Poll, this can only happen due to a race condition 1141ce662b44SJohannes Berg * when we set the TIM bit and the station notices it, but 1142ce662b44SJohannes Berg * before it can poll for the frame we expire it. 1143ce662b44SJohannes Berg * 1144ce662b44SJohannes Berg * For uAPSD, this is said in the standard (11.2.1.5 h): 1145ce662b44SJohannes Berg * At each unscheduled SP for a non-AP STA, the AP shall 1146ce662b44SJohannes Berg * attempt to transmit at least one MSDU or MMPDU, but no 1147ce662b44SJohannes Berg * more than the value specified in the Max SP Length field 1148ce662b44SJohannes Berg * in the QoS Capability element from delivery-enabled ACs, 1149ce662b44SJohannes Berg * that are destined for the non-AP STA. 1150ce662b44SJohannes Berg * 1151ce662b44SJohannes Berg * Since we have no other MSDU/MMPDU, transmit a QoS null frame. 1152ce662b44SJohannes Berg */ 1153ce662b44SJohannes Berg 1154ce662b44SJohannes Berg /* This will evaluate to 1, 3, 5 or 7. */ 1155ce662b44SJohannes Berg tid = 7 - ((ffs(~ignored_acs) - 1) << 1); 1156ce662b44SJohannes Berg 115740b96408SJohannes Berg ieee80211_send_null_response(sdata, sta, tid, reason); 11584049e09aSJohannes Berg return; 11594049e09aSJohannes Berg } 11604049e09aSJohannes Berg 116147086fc5SJohannes Berg if (!driver_release_tids) { 116247086fc5SJohannes Berg struct sk_buff_head pending; 116347086fc5SJohannes Berg struct sk_buff *skb; 116440b96408SJohannes Berg int num = 0; 116540b96408SJohannes Berg u16 tids = 0; 116647086fc5SJohannes Berg 116747086fc5SJohannes Berg skb_queue_head_init(&pending); 116847086fc5SJohannes Berg 116947086fc5SJohannes Berg while ((skb = __skb_dequeue(&frames))) { 1170af818581SJohannes Berg struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 117147086fc5SJohannes Berg struct ieee80211_hdr *hdr = (void *) skb->data; 117240b96408SJohannes Berg u8 *qoshdr = NULL; 117340b96408SJohannes Berg 117440b96408SJohannes Berg num++; 1175af818581SJohannes Berg 1176af818581SJohannes Berg /* 117747086fc5SJohannes Berg * Tell TX path to send this frame even though the 117847086fc5SJohannes Berg * STA may still remain is PS mode after this frame 117947086fc5SJohannes Berg * exchange. 1180af818581SJohannes Berg */ 118102f2f1a9SJohannes Berg info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER; 1182af818581SJohannes Berg 118347086fc5SJohannes Berg /* 118447086fc5SJohannes Berg * Use MoreData flag to indicate whether there are 118547086fc5SJohannes Berg * more buffered frames for this STA 118647086fc5SJohannes Berg */ 118724b9c373SJanusz.Dziedzic@tieto.com if (more_data || !skb_queue_empty(&frames)) 118847086fc5SJohannes Berg hdr->frame_control |= 118947086fc5SJohannes Berg cpu_to_le16(IEEE80211_FCTL_MOREDATA); 119024b9c373SJanusz.Dziedzic@tieto.com else 119124b9c373SJanusz.Dziedzic@tieto.com hdr->frame_control &= 119224b9c373SJanusz.Dziedzic@tieto.com cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 1193af818581SJohannes Berg 119440b96408SJohannes Berg if (ieee80211_is_data_qos(hdr->frame_control) || 119540b96408SJohannes Berg ieee80211_is_qos_nullfunc(hdr->frame_control)) 119640b96408SJohannes Berg qoshdr = ieee80211_get_qos_ctl(hdr); 119740b96408SJohannes Berg 119852a3f20cSMarco Porsch /* end service period after last frame */ 119952a3f20cSMarco Porsch if (skb_queue_empty(&frames)) { 120040b96408SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_UAPSD && 120152a3f20cSMarco Porsch qoshdr) 120240b96408SJohannes Berg *qoshdr |= IEEE80211_QOS_CTL_EOSP; 1203deeaee19SJohannes Berg 120447086fc5SJohannes Berg info->flags |= IEEE80211_TX_STATUS_EOSP | 120547086fc5SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 120652a3f20cSMarco Porsch } 120747086fc5SJohannes Berg 120840b96408SJohannes Berg if (qoshdr) 120940b96408SJohannes Berg tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK); 121040b96408SJohannes Berg else 121140b96408SJohannes Berg tids |= BIT(0); 121240b96408SJohannes Berg 121347086fc5SJohannes Berg __skb_queue_tail(&pending, skb); 121447086fc5SJohannes Berg } 121547086fc5SJohannes Berg 121640b96408SJohannes Berg drv_allow_buffered_frames(local, sta, tids, num, 121740b96408SJohannes Berg reason, more_data); 121840b96408SJohannes Berg 121947086fc5SJohannes Berg ieee80211_add_pending_skbs(local, &pending); 1220af818581SJohannes Berg 1221c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1222af818581SJohannes Berg } else { 1223af818581SJohannes Berg /* 12244049e09aSJohannes Berg * We need to release a frame that is buffered somewhere in the 12254049e09aSJohannes Berg * driver ... it'll have to handle that. 12264049e09aSJohannes Berg * Note that, as per the comment above, it'll also have to see 12274049e09aSJohannes Berg * if there is more than just one frame on the specific TID that 12284049e09aSJohannes Berg * we're releasing from, and it needs to set the more-data bit 12294049e09aSJohannes Berg * accordingly if we tell it that there's no more data. If we do 12304049e09aSJohannes Berg * tell it there's more data, then of course the more-data bit 12314049e09aSJohannes Berg * needs to be set anyway. 1232af818581SJohannes Berg */ 12334049e09aSJohannes Berg drv_release_buffered_frames(local, sta, driver_release_tids, 123447086fc5SJohannes Berg n_frames, reason, more_data); 12354049e09aSJohannes Berg 12364049e09aSJohannes Berg /* 12374049e09aSJohannes Berg * Note that we don't recalculate the TIM bit here as it would 12384049e09aSJohannes Berg * most likely have no effect at all unless the driver told us 12394049e09aSJohannes Berg * that the TID became empty before returning here from the 12404049e09aSJohannes Berg * release function. 12414049e09aSJohannes Berg * Either way, however, when the driver tells us that the TID 12424049e09aSJohannes Berg * became empty we'll do the TIM recalculation. 12434049e09aSJohannes Berg */ 1244af818581SJohannes Berg } 1245af818581SJohannes Berg } 1246af818581SJohannes Berg 1247af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 1248af818581SJohannes Berg { 124947086fc5SJohannes Berg u8 ignore_for_response = sta->sta.uapsd_queues; 1250af818581SJohannes Berg 1251af818581SJohannes Berg /* 125247086fc5SJohannes Berg * If all ACs are delivery-enabled then we should reply 125347086fc5SJohannes Berg * from any of them, if only some are enabled we reply 125447086fc5SJohannes Berg * only from the non-enabled ones. 1255af818581SJohannes Berg */ 125647086fc5SJohannes Berg if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1) 125747086fc5SJohannes Berg ignore_for_response = 0; 1258af818581SJohannes Berg 125947086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response, 126047086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_PSPOLL); 1261af818581SJohannes Berg } 126247086fc5SJohannes Berg 126347086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta) 126447086fc5SJohannes Berg { 126547086fc5SJohannes Berg int n_frames = sta->sta.max_sp; 126647086fc5SJohannes Berg u8 delivery_enabled = sta->sta.uapsd_queues; 126747086fc5SJohannes Berg 126847086fc5SJohannes Berg /* 126947086fc5SJohannes Berg * If we ever grow support for TSPEC this might happen if 127047086fc5SJohannes Berg * the TSPEC update from hostapd comes in between a trigger 127147086fc5SJohannes Berg * frame setting WLAN_STA_UAPSD in the RX path and this 127247086fc5SJohannes Berg * actually getting called. 127347086fc5SJohannes Berg */ 127447086fc5SJohannes Berg if (!delivery_enabled) 127547086fc5SJohannes Berg return; 127647086fc5SJohannes Berg 127747086fc5SJohannes Berg switch (sta->sta.max_sp) { 127847086fc5SJohannes Berg case 1: 127947086fc5SJohannes Berg n_frames = 2; 128047086fc5SJohannes Berg break; 128147086fc5SJohannes Berg case 2: 128247086fc5SJohannes Berg n_frames = 4; 128347086fc5SJohannes Berg break; 128447086fc5SJohannes Berg case 3: 128547086fc5SJohannes Berg n_frames = 6; 128647086fc5SJohannes Berg break; 128747086fc5SJohannes Berg case 0: 128847086fc5SJohannes Berg /* XXX: what is a good value? */ 128947086fc5SJohannes Berg n_frames = 8; 129047086fc5SJohannes Berg break; 129147086fc5SJohannes Berg } 129247086fc5SJohannes Berg 129347086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled, 129447086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_UAPSD); 1295af818581SJohannes Berg } 1296af818581SJohannes Berg 1297af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 1298af818581SJohannes Berg struct ieee80211_sta *pubsta, bool block) 1299af818581SJohannes Berg { 1300af818581SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1301af818581SJohannes Berg 1302b5878a2dSJohannes Berg trace_api_sta_block_awake(sta->local, pubsta, block); 1303b5878a2dSJohannes Berg 1304af818581SJohannes Berg if (block) 1305c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_PS_DRIVER); 1306c2c98fdeSJohannes Berg else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) 1307af818581SJohannes Berg ieee80211_queue_work(hw, &sta->drv_unblock_wk); 1308af818581SJohannes Berg } 1309af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake); 1310dcf55fb5SFelix Fietkau 131137fbd908SJohannes Berg void ieee80211_sta_eosp_irqsafe(struct ieee80211_sta *pubsta) 131237fbd908SJohannes Berg { 131337fbd908SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 131437fbd908SJohannes Berg struct ieee80211_local *local = sta->local; 131537fbd908SJohannes Berg struct sk_buff *skb; 131637fbd908SJohannes Berg struct skb_eosp_msg_data *data; 131737fbd908SJohannes Berg 131837fbd908SJohannes Berg trace_api_eosp(local, pubsta); 131937fbd908SJohannes Berg 132037fbd908SJohannes Berg skb = alloc_skb(0, GFP_ATOMIC); 132137fbd908SJohannes Berg if (!skb) { 132237fbd908SJohannes Berg /* too bad ... but race is better than loss */ 132337fbd908SJohannes Berg clear_sta_flag(sta, WLAN_STA_SP); 132437fbd908SJohannes Berg return; 132537fbd908SJohannes Berg } 132637fbd908SJohannes Berg 132737fbd908SJohannes Berg data = (void *)skb->cb; 132837fbd908SJohannes Berg memcpy(data->sta, pubsta->addr, ETH_ALEN); 132937fbd908SJohannes Berg memcpy(data->iface, sta->sdata->vif.addr, ETH_ALEN); 133037fbd908SJohannes Berg skb->pkt_type = IEEE80211_EOSP_MSG; 133137fbd908SJohannes Berg skb_queue_tail(&local->skb_queue, skb); 133237fbd908SJohannes Berg tasklet_schedule(&local->tasklet); 133337fbd908SJohannes Berg } 133437fbd908SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp_irqsafe); 133537fbd908SJohannes Berg 1336042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, 1337042ec453SJohannes Berg u8 tid, bool buffered) 1338dcf55fb5SFelix Fietkau { 1339dcf55fb5SFelix Fietkau struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1340dcf55fb5SFelix Fietkau 1341948d887dSJohannes Berg if (WARN_ON(tid >= STA_TID_NUM)) 1342042ec453SJohannes Berg return; 1343042ec453SJohannes Berg 1344948d887dSJohannes Berg if (buffered) 1345948d887dSJohannes Berg set_bit(tid, &sta->driver_buffered_tids); 1346948d887dSJohannes Berg else 1347948d887dSJohannes Berg clear_bit(tid, &sta->driver_buffered_tids); 1348948d887dSJohannes Berg 1349c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1350dcf55fb5SFelix Fietkau } 1351042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered); 1352d9a7ddb0SJohannes Berg 135383d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta, 1354d9a7ddb0SJohannes Berg enum ieee80211_sta_state new_state) 1355d9a7ddb0SJohannes Berg { 13568bf11d8dSJohannes Berg might_sleep(); 1357d9a7ddb0SJohannes Berg 1358d9a7ddb0SJohannes Berg if (sta->sta_state == new_state) 1359d9a7ddb0SJohannes Berg return 0; 1360d9a7ddb0SJohannes Berg 1361f09603a2SJohannes Berg /* check allowed transitions first */ 1362f09603a2SJohannes Berg 1363d9a7ddb0SJohannes Berg switch (new_state) { 1364d9a7ddb0SJohannes Berg case IEEE80211_STA_NONE: 1365f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_AUTH) 1366d9a7ddb0SJohannes Berg return -EINVAL; 1367d9a7ddb0SJohannes Berg break; 1368d9a7ddb0SJohannes Berg case IEEE80211_STA_AUTH: 1369f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_NONE && 1370f09603a2SJohannes Berg sta->sta_state != IEEE80211_STA_ASSOC) 1371d9a7ddb0SJohannes Berg return -EINVAL; 1372d9a7ddb0SJohannes Berg break; 1373d9a7ddb0SJohannes Berg case IEEE80211_STA_ASSOC: 1374f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_AUTH && 1375f09603a2SJohannes Berg sta->sta_state != IEEE80211_STA_AUTHORIZED) 1376d9a7ddb0SJohannes Berg return -EINVAL; 1377d9a7ddb0SJohannes Berg break; 1378d9a7ddb0SJohannes Berg case IEEE80211_STA_AUTHORIZED: 1379f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_ASSOC) 1380d9a7ddb0SJohannes Berg return -EINVAL; 1381d9a7ddb0SJohannes Berg break; 1382d9a7ddb0SJohannes Berg default: 1383d9a7ddb0SJohannes Berg WARN(1, "invalid state %d", new_state); 1384d9a7ddb0SJohannes Berg return -EINVAL; 1385d9a7ddb0SJohannes Berg } 1386d9a7ddb0SJohannes Berg 1387bdcbd8e0SJohannes Berg sta_dbg(sta->sdata, "moving STA %pM to state %d\n", 1388bdcbd8e0SJohannes Berg sta->sta.addr, new_state); 1389f09603a2SJohannes Berg 1390f09603a2SJohannes Berg /* 1391f09603a2SJohannes Berg * notify the driver before the actual changes so it can 1392f09603a2SJohannes Berg * fail the transition 1393f09603a2SJohannes Berg */ 1394f09603a2SJohannes Berg if (test_sta_flag(sta, WLAN_STA_INSERTED)) { 1395f09603a2SJohannes Berg int err = drv_sta_state(sta->local, sta->sdata, sta, 1396f09603a2SJohannes Berg sta->sta_state, new_state); 1397f09603a2SJohannes Berg if (err) 1398f09603a2SJohannes Berg return err; 1399f09603a2SJohannes Berg } 1400f09603a2SJohannes Berg 1401f09603a2SJohannes Berg /* reflect the change in all state variables */ 1402f09603a2SJohannes Berg 1403f09603a2SJohannes Berg switch (new_state) { 1404f09603a2SJohannes Berg case IEEE80211_STA_NONE: 1405f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_AUTH) 1406f09603a2SJohannes Berg clear_bit(WLAN_STA_AUTH, &sta->_flags); 1407f09603a2SJohannes Berg break; 1408f09603a2SJohannes Berg case IEEE80211_STA_AUTH: 1409f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_NONE) 1410f09603a2SJohannes Berg set_bit(WLAN_STA_AUTH, &sta->_flags); 1411f09603a2SJohannes Berg else if (sta->sta_state == IEEE80211_STA_ASSOC) 1412f09603a2SJohannes Berg clear_bit(WLAN_STA_ASSOC, &sta->_flags); 1413f09603a2SJohannes Berg break; 1414f09603a2SJohannes Berg case IEEE80211_STA_ASSOC: 1415f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_AUTH) { 1416f09603a2SJohannes Berg set_bit(WLAN_STA_ASSOC, &sta->_flags); 1417f09603a2SJohannes Berg } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) { 14187e3ed02cSFelix Fietkau if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 14197e3ed02cSFelix Fietkau (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 14207e3ed02cSFelix Fietkau !sta->sdata->u.vlan.sta)) 14217e3ed02cSFelix Fietkau atomic_dec(&sta->sdata->bss->num_mcast_sta); 1422f09603a2SJohannes Berg clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 1423f09603a2SJohannes Berg } 1424f09603a2SJohannes Berg break; 1425f09603a2SJohannes Berg case IEEE80211_STA_AUTHORIZED: 1426f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_ASSOC) { 14277e3ed02cSFelix Fietkau if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 14287e3ed02cSFelix Fietkau (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 14297e3ed02cSFelix Fietkau !sta->sdata->u.vlan.sta)) 14307e3ed02cSFelix Fietkau atomic_inc(&sta->sdata->bss->num_mcast_sta); 1431f09603a2SJohannes Berg set_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 1432f09603a2SJohannes Berg } 1433f09603a2SJohannes Berg break; 1434f09603a2SJohannes Berg default: 1435f09603a2SJohannes Berg break; 1436f09603a2SJohannes Berg } 1437f09603a2SJohannes Berg 1438d9a7ddb0SJohannes Berg sta->sta_state = new_state; 1439d9a7ddb0SJohannes Berg 1440d9a7ddb0SJohannes Berg return 0; 1441d9a7ddb0SJohannes Berg } 1442