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 9497f97b1fSJohannes Berg static void cleanup_single_sta(struct sta_info *sta) 95b22cfcfcSEliad Peller { 96b22cfcfcSEliad Peller int ac, i; 97b22cfcfcSEliad Peller struct tid_ampdu_tx *tid_tx; 98b22cfcfcSEliad Peller struct ieee80211_sub_if_data *sdata = sta->sdata; 99b22cfcfcSEliad Peller struct ieee80211_local *local = sdata->local; 100d012a605SMarco Porsch struct ps_data *ps; 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. 107*051007d9SJohannes Berg * 108*051007d9SJohannes Berg * Note though that this still uses the sdata and even 109*051007d9SJohannes Berg * calls the driver in AP and mesh mode, so interfaces 110*051007d9SJohannes Berg * of those types mush use call sta_info_flush_cleanup() 111*051007d9SJohannes Berg * (typically via sta_info_flush()) before deconfiguring 112*051007d9SJohannes Berg * the driver. 113*051007d9SJohannes Berg * 114*051007d9SJohannes Berg * In station mode, nothing happens here so it doesn't 115*051007d9SJohannes Berg * have to (and doesn't) do that, this is intentional to 116*051007d9SJohannes Berg * speed up roaming. 117b22cfcfcSEliad Peller */ 118b22cfcfcSEliad Peller 119b22cfcfcSEliad Peller if (test_sta_flag(sta, WLAN_STA_PS_STA)) { 120d012a605SMarco Porsch if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 121d012a605SMarco Porsch sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 122d012a605SMarco Porsch ps = &sdata->bss->ps; 123d012a605SMarco Porsch else 124d012a605SMarco Porsch return; 125b22cfcfcSEliad Peller 126b22cfcfcSEliad Peller clear_sta_flag(sta, WLAN_STA_PS_STA); 127b22cfcfcSEliad Peller 128d012a605SMarco Porsch atomic_dec(&ps->num_sta_ps); 129b22cfcfcSEliad Peller sta_info_recalc_tim(sta); 130b22cfcfcSEliad Peller } 131b22cfcfcSEliad Peller 132b22cfcfcSEliad Peller for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 133b22cfcfcSEliad Peller local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]); 1341f98ab7fSFelix Fietkau ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]); 1351f98ab7fSFelix Fietkau ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]); 136b22cfcfcSEliad Peller } 137b22cfcfcSEliad Peller 138b22cfcfcSEliad Peller #ifdef CONFIG_MAC80211_MESH 139b22cfcfcSEliad Peller if (ieee80211_vif_is_mesh(&sdata->vif)) { 140b22cfcfcSEliad Peller mesh_accept_plinks_update(sdata); 141b22cfcfcSEliad Peller mesh_plink_deactivate(sta); 142b22cfcfcSEliad Peller del_timer_sync(&sta->plink_timer); 143b22cfcfcSEliad Peller } 144b22cfcfcSEliad Peller #endif 145b22cfcfcSEliad Peller 146b22cfcfcSEliad Peller cancel_work_sync(&sta->drv_unblock_wk); 147b22cfcfcSEliad Peller 148b22cfcfcSEliad Peller /* 149b22cfcfcSEliad Peller * Destroy aggregation state here. It would be nice to wait for the 150b22cfcfcSEliad Peller * driver to finish aggregation stop and then clean up, but for now 151b22cfcfcSEliad Peller * drivers have to handle aggregation stop being requested, followed 152b22cfcfcSEliad Peller * directly by station destruction. 153b22cfcfcSEliad Peller */ 1545a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 155b22cfcfcSEliad Peller tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]); 156b22cfcfcSEliad Peller if (!tid_tx) 157b22cfcfcSEliad Peller continue; 1581f98ab7fSFelix Fietkau ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending); 159b22cfcfcSEliad Peller kfree(tid_tx); 160b22cfcfcSEliad Peller } 161b22cfcfcSEliad Peller 162b22cfcfcSEliad Peller sta_info_free(local, sta); 163b22cfcfcSEliad Peller } 164b22cfcfcSEliad Peller 16597f97b1fSJohannes Berg void ieee80211_cleanup_sdata_stas(struct ieee80211_sub_if_data *sdata) 16697f97b1fSJohannes Berg { 16797f97b1fSJohannes Berg struct sta_info *sta; 16897f97b1fSJohannes Berg 16997f97b1fSJohannes Berg spin_lock_bh(&sdata->cleanup_stations_lock); 17097f97b1fSJohannes Berg while (!list_empty(&sdata->cleanup_stations)) { 17197f97b1fSJohannes Berg sta = list_first_entry(&sdata->cleanup_stations, 17297f97b1fSJohannes Berg struct sta_info, list); 17397f97b1fSJohannes Berg list_del(&sta->list); 17497f97b1fSJohannes Berg spin_unlock_bh(&sdata->cleanup_stations_lock); 17597f97b1fSJohannes Berg 17697f97b1fSJohannes Berg cleanup_single_sta(sta); 17797f97b1fSJohannes Berg 17897f97b1fSJohannes Berg spin_lock_bh(&sdata->cleanup_stations_lock); 17997f97b1fSJohannes Berg } 18097f97b1fSJohannes Berg 18197f97b1fSJohannes Berg spin_unlock_bh(&sdata->cleanup_stations_lock); 18297f97b1fSJohannes Berg } 18397f97b1fSJohannes Berg 184b22cfcfcSEliad Peller static void free_sta_rcu(struct rcu_head *h) 185b22cfcfcSEliad Peller { 186b22cfcfcSEliad Peller struct sta_info *sta = container_of(h, struct sta_info, rcu_head); 18797f97b1fSJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 188b22cfcfcSEliad Peller 18997f97b1fSJohannes Berg spin_lock(&sdata->cleanup_stations_lock); 19097f97b1fSJohannes Berg list_add_tail(&sta->list, &sdata->cleanup_stations); 19197f97b1fSJohannes Berg spin_unlock(&sdata->cleanup_stations_lock); 19297f97b1fSJohannes Berg 19397f97b1fSJohannes Berg ieee80211_queue_work(&sdata->local->hw, &sdata->cleanup_stations_wk); 194b22cfcfcSEliad Peller } 195b22cfcfcSEliad Peller 196d0709a65SJohannes Berg /* protected by RCU */ 197abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata, 198abe60632SJohannes Berg const u8 *addr) 19943ba7e95SJohannes Berg { 200abe60632SJohannes Berg struct ieee80211_local *local = sdata->local; 20143ba7e95SJohannes Berg struct sta_info *sta; 20243ba7e95SJohannes Berg 2030379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 2040379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 20543ba7e95SJohannes Berg while (sta) { 206abe60632SJohannes Berg if (sta->sdata == sdata && 207b203ca39SJoe Perches ether_addr_equal(sta->sta.addr, addr)) 20843ba7e95SJohannes Berg break; 2090379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 2100379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 21143ba7e95SJohannes Berg } 21243ba7e95SJohannes Berg return sta; 21343ba7e95SJohannes Berg } 21443ba7e95SJohannes Berg 2150e5ded5aSFelix Fietkau /* 2160e5ded5aSFelix Fietkau * Get sta info either from the specified interface 2170e5ded5aSFelix Fietkau * or from one of its vlans 2180e5ded5aSFelix Fietkau */ 2190e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata, 2200e5ded5aSFelix Fietkau const u8 *addr) 2210e5ded5aSFelix Fietkau { 2220e5ded5aSFelix Fietkau struct ieee80211_local *local = sdata->local; 2230e5ded5aSFelix Fietkau struct sta_info *sta; 2240e5ded5aSFelix Fietkau 2250379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 2260379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 2270e5ded5aSFelix Fietkau while (sta) { 2280e5ded5aSFelix Fietkau if ((sta->sdata == sdata || 229a2c1e3daSJohannes Berg (sta->sdata->bss && sta->sdata->bss == sdata->bss)) && 230b203ca39SJoe Perches ether_addr_equal(sta->sta.addr, addr)) 2310e5ded5aSFelix Fietkau break; 2320379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 2330379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 2340e5ded5aSFelix Fietkau } 2350e5ded5aSFelix Fietkau return sta; 2360e5ded5aSFelix Fietkau } 2370e5ded5aSFelix Fietkau 2383b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, 2393b53fde8SJohannes Berg int idx) 240ee385855SLuis Carlos Cobo { 2413b53fde8SJohannes Berg struct ieee80211_local *local = sdata->local; 242ee385855SLuis Carlos Cobo struct sta_info *sta; 243ee385855SLuis Carlos Cobo int i = 0; 244ee385855SLuis Carlos Cobo 245d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) { 2463b53fde8SJohannes Berg if (sdata != sta->sdata) 2472a8ca29aSLuis Carlos Cobo continue; 248ee385855SLuis Carlos Cobo if (i < idx) { 249ee385855SLuis Carlos Cobo ++i; 250ee385855SLuis Carlos Cobo continue; 251ee385855SLuis Carlos Cobo } 2522a8ca29aSLuis Carlos Cobo return sta; 253ee385855SLuis Carlos Cobo } 254ee385855SLuis Carlos Cobo 255ee385855SLuis Carlos Cobo return NULL; 256ee385855SLuis Carlos Cobo } 257f0706e82SJiri Benc 25893e5deb1SJohannes Berg /** 259d9a7ddb0SJohannes Berg * sta_info_free - free STA 26093e5deb1SJohannes Berg * 2616ef307bcSRandy Dunlap * @local: pointer to the global information 26293e5deb1SJohannes Berg * @sta: STA info to free 26393e5deb1SJohannes Berg * 26493e5deb1SJohannes Berg * This function must undo everything done by sta_info_alloc() 265d9a7ddb0SJohannes Berg * that may happen before sta_info_insert(). It may only be 266d9a7ddb0SJohannes Berg * called when sta_info_insert() has not been attempted (and 267d9a7ddb0SJohannes Berg * if that fails, the station is freed anyway.) 26893e5deb1SJohannes Berg */ 269d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta) 27093e5deb1SJohannes Berg { 271889cbb91SJohannes Berg if (sta->rate_ctrl) 2724b7679a5SJohannes Berg rate_control_free_sta(sta); 27393e5deb1SJohannes Berg 274bdcbd8e0SJohannes Berg sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr); 27593e5deb1SJohannes Berg 27693e5deb1SJohannes Berg kfree(sta); 27793e5deb1SJohannes Berg } 27893e5deb1SJohannes Berg 2794d33960bSJohannes Berg /* Caller must hold local->sta_mtx */ 280d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local, 281d0709a65SJohannes Berg struct sta_info *sta) 282f0706e82SJiri Benc { 2834d33960bSJohannes Berg lockdep_assert_held(&local->sta_mtx); 28417741cdcSJohannes Berg sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)]; 285cf778b00SEric Dumazet rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta); 286f0706e82SJiri Benc } 287f0706e82SJiri Benc 288af818581SJohannes Berg static void sta_unblock(struct work_struct *wk) 289af818581SJohannes Berg { 290af818581SJohannes Berg struct sta_info *sta; 291af818581SJohannes Berg 292af818581SJohannes Berg sta = container_of(wk, struct sta_info, drv_unblock_wk); 293af818581SJohannes Berg 294af818581SJohannes Berg if (sta->dead) 295af818581SJohannes Berg return; 296af818581SJohannes Berg 29754420473SHelmut Schaa if (!test_sta_flag(sta, WLAN_STA_PS_STA)) { 29854420473SHelmut Schaa local_bh_disable(); 299af818581SJohannes Berg ieee80211_sta_ps_deliver_wakeup(sta); 30054420473SHelmut Schaa local_bh_enable(); 30154420473SHelmut Schaa } else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) { 302c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 303ce662b44SJohannes Berg 304ce662b44SJohannes Berg local_bh_disable(); 305af818581SJohannes Berg ieee80211_sta_ps_deliver_poll_response(sta); 306ce662b44SJohannes Berg local_bh_enable(); 307c2c98fdeSJohannes Berg } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) { 308c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 309ce662b44SJohannes Berg 310ce662b44SJohannes Berg local_bh_disable(); 31147086fc5SJohannes Berg ieee80211_sta_ps_deliver_uapsd(sta); 312ce662b44SJohannes Berg local_bh_enable(); 31350a9432dSJohannes Berg } else 314c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 315af818581SJohannes Berg } 316af818581SJohannes Berg 317af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local, 318af65cd96SJohannes Berg struct sta_info *sta, gfp_t gfp) 319af65cd96SJohannes Berg { 320af65cd96SJohannes Berg if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) 321af65cd96SJohannes Berg return 0; 322af65cd96SJohannes Berg 323889cbb91SJohannes Berg sta->rate_ctrl = local->rate_ctrl; 324af65cd96SJohannes Berg sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, 325af65cd96SJohannes Berg &sta->sta, gfp); 326889cbb91SJohannes Berg if (!sta->rate_ctrl_priv) 327af65cd96SJohannes Berg return -ENOMEM; 328af65cd96SJohannes Berg 329af65cd96SJohannes Berg return 0; 330af65cd96SJohannes Berg } 331af65cd96SJohannes Berg 33273651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, 33356544160SJohannes Berg const u8 *addr, gfp_t gfp) 334f0706e82SJiri Benc { 335d0709a65SJohannes Berg struct ieee80211_local *local = sdata->local; 336f0706e82SJiri Benc struct sta_info *sta; 337ebe27c91SMohammed Shafi Shajakhan struct timespec uptime; 33816c5f15cSRon Rindjunsky int i; 339f0706e82SJiri Benc 34017741cdcSJohannes Berg sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp); 341f0706e82SJiri Benc if (!sta) 34273651ee6SJohannes Berg return NULL; 343f0706e82SJiri Benc 34407346f81SJohannes Berg spin_lock_init(&sta->lock); 345af818581SJohannes Berg INIT_WORK(&sta->drv_unblock_wk, sta_unblock); 34667c282c0SJohannes Berg INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work); 347a93e3644SJohannes Berg mutex_init(&sta->ampdu_mlme.mtx); 34807346f81SJohannes Berg 34917741cdcSJohannes Berg memcpy(sta->sta.addr, addr, ETH_ALEN); 350d0709a65SJohannes Berg sta->local = local; 351d0709a65SJohannes Berg sta->sdata = sdata; 3528bc8aecdSFelix Fietkau sta->last_rx = jiffies; 353f0706e82SJiri Benc 35471ec375cSJohannes Berg sta->sta_state = IEEE80211_STA_NONE; 35571ec375cSJohannes Berg 356ebe27c91SMohammed Shafi Shajakhan do_posix_clock_monotonic_gettime(&uptime); 357ebe27c91SMohammed Shafi Shajakhan sta->last_connected = uptime.tv_sec; 358541a45a1SBruno Randolf ewma_init(&sta->avg_signal, 1024, 8); 359541a45a1SBruno Randolf 360af65cd96SJohannes Berg if (sta_prepare_rate_control(local, sta, gfp)) { 361f0706e82SJiri Benc kfree(sta); 36273651ee6SJohannes Berg return NULL; 363f0706e82SJiri Benc } 364f0706e82SJiri Benc 3655a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 366a622ab72SJohannes Berg /* 367a622ab72SJohannes Berg * timer_to_tid must be initialized with identity mapping 368a622ab72SJohannes Berg * to enable session_timer's data differentiation. See 369a622ab72SJohannes Berg * sta_rx_agg_session_timer_expired for usage. 370a622ab72SJohannes Berg */ 37116c5f15cSRon Rindjunsky sta->timer_to_tid[i] = i; 37216c5f15cSRon Rindjunsky } 373948d887dSJohannes Berg for (i = 0; i < IEEE80211_NUM_ACS; i++) { 374948d887dSJohannes Berg skb_queue_head_init(&sta->ps_tx_buf[i]); 375948d887dSJohannes Berg skb_queue_head_init(&sta->tx_filtered[i]); 376948d887dSJohannes Berg } 37773651ee6SJohannes Berg 3785a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) 3794be929beSAlexey Dobriyan sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); 380cccaec98SSenthil Balasubramanian 381bdcbd8e0SJohannes Berg sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr); 38273651ee6SJohannes Berg 38303e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH 38457cf8043SJavier Cardona sta->plink_state = NL80211_PLINK_LISTEN; 38503e4497eSJohannes Berg init_timer(&sta->plink_timer); 38603e4497eSJohannes Berg #endif 38703e4497eSJohannes Berg 38873651ee6SJohannes Berg return sta; 38973651ee6SJohannes Berg } 39073651ee6SJohannes Berg 3918c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta) 39234e89507SJohannes Berg { 39334e89507SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 39434e89507SJohannes Berg 39503e4497eSJohannes Berg /* 39603e4497eSJohannes Berg * Can't be a WARN_ON because it can be triggered through a race: 39703e4497eSJohannes Berg * something inserts a STA (on one CPU) without holding the RTNL 39803e4497eSJohannes Berg * and another CPU turns off the net device. 39903e4497eSJohannes Berg */ 4008c71df7aSGuy Eilam if (unlikely(!ieee80211_sdata_running(sdata))) 4018c71df7aSGuy Eilam return -ENETDOWN; 40203e4497eSJohannes Berg 403b203ca39SJoe Perches if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) || 4048c71df7aSGuy Eilam is_multicast_ether_addr(sta->sta.addr))) 4058c71df7aSGuy Eilam return -EINVAL; 4068c71df7aSGuy Eilam 4078c71df7aSGuy Eilam return 0; 40893e5deb1SJohannes Berg } 40944213b5eSJohannes Berg 410f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local, 411f09603a2SJohannes Berg struct ieee80211_sub_if_data *sdata, 412f09603a2SJohannes Berg struct sta_info *sta) 413f09603a2SJohannes Berg { 414f09603a2SJohannes Berg enum ieee80211_sta_state state; 415f09603a2SJohannes Berg int err = 0; 416f09603a2SJohannes Berg 417f09603a2SJohannes Berg for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) { 418f09603a2SJohannes Berg err = drv_sta_state(local, sdata, sta, state, state + 1); 419f09603a2SJohannes Berg if (err) 420f09603a2SJohannes Berg break; 421f09603a2SJohannes Berg } 422f09603a2SJohannes Berg 423f09603a2SJohannes Berg if (!err) { 424a4ec45a4SJohannes Berg /* 425a4ec45a4SJohannes Berg * Drivers using legacy sta_add/sta_remove callbacks only 426a4ec45a4SJohannes Berg * get uploaded set to true after sta_add is called. 427a4ec45a4SJohannes Berg */ 428a4ec45a4SJohannes Berg if (!local->ops->sta_add) 429f09603a2SJohannes Berg sta->uploaded = true; 430f09603a2SJohannes Berg return 0; 431f09603a2SJohannes Berg } 432f09603a2SJohannes Berg 433f09603a2SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { 434bdcbd8e0SJohannes Berg sdata_info(sdata, 435bdcbd8e0SJohannes Berg "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n", 436bdcbd8e0SJohannes Berg sta->sta.addr, state + 1, err); 437f09603a2SJohannes Berg err = 0; 438f09603a2SJohannes Berg } 439f09603a2SJohannes Berg 440f09603a2SJohannes Berg /* unwind on error */ 441f09603a2SJohannes Berg for (; state > IEEE80211_STA_NOTEXIST; state--) 442f09603a2SJohannes Berg WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1)); 443f09603a2SJohannes Berg 444f09603a2SJohannes Berg return err; 445f09603a2SJohannes Berg } 446f09603a2SJohannes Berg 44734e89507SJohannes Berg /* 4488c71df7aSGuy Eilam * should be called with sta_mtx locked 4498c71df7aSGuy Eilam * this function replaces the mutex lock 4508c71df7aSGuy Eilam * with a RCU lock 4518c71df7aSGuy Eilam */ 4524d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU) 4538c71df7aSGuy Eilam { 4548c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 4558c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 4567852e361SJohannes Berg struct station_info sinfo; 4578c71df7aSGuy Eilam int err = 0; 4588c71df7aSGuy Eilam 4598c71df7aSGuy Eilam lockdep_assert_held(&local->sta_mtx); 4608c71df7aSGuy Eilam 4617852e361SJohannes Berg /* check if STA exists already */ 4627852e361SJohannes Berg if (sta_info_get_bss(sdata, sta->sta.addr)) { 4634d33960bSJohannes Berg err = -EEXIST; 4644d33960bSJohannes Berg goto out_err; 46534e89507SJohannes Berg } 46634e89507SJohannes Berg 4674d33960bSJohannes Berg /* notify driver */ 468f09603a2SJohannes Berg err = sta_info_insert_drv_state(local, sdata, sta); 469f09603a2SJohannes Berg if (err) 470f09603a2SJohannes Berg goto out_err; 4714d33960bSJohannes Berg 4724d33960bSJohannes Berg local->num_sta++; 4734d33960bSJohannes Berg local->sta_generation++; 4744d33960bSJohannes Berg smp_mb(); 4754d33960bSJohannes Berg 4764d33960bSJohannes Berg /* make the station visible */ 4774d33960bSJohannes Berg sta_info_hash_add(local, sta); 4784d33960bSJohannes Berg 479794454ceSArik Nemtsov list_add_rcu(&sta->list, &local->sta_list); 48083d5cc01SJohannes Berg 48183d5cc01SJohannes Berg set_sta_flag(sta, WLAN_STA_INSERTED); 4824d33960bSJohannes Berg 4834d33960bSJohannes Berg ieee80211_sta_debugfs_add(sta); 4844d33960bSJohannes Berg rate_control_add_sta_debugfs(sta); 4854d33960bSJohannes Berg 4864d33960bSJohannes Berg memset(&sinfo, 0, sizeof(sinfo)); 4874d33960bSJohannes Berg sinfo.filled = 0; 4884d33960bSJohannes Berg sinfo.generation = local->sta_generation; 4894d33960bSJohannes Berg cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL); 490d0709a65SJohannes Berg 491bdcbd8e0SJohannes Berg sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr); 492f0706e82SJiri Benc 49334e89507SJohannes Berg /* move reference to rcu-protected */ 49434e89507SJohannes Berg rcu_read_lock(); 49534e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 496e9f207f0SJiri Benc 49773651ee6SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 49873651ee6SJohannes Berg mesh_accept_plinks_update(sdata); 49973651ee6SJohannes Berg 50073651ee6SJohannes Berg return 0; 5014d33960bSJohannes Berg out_err: 5024d33960bSJohannes Berg mutex_unlock(&local->sta_mtx); 5034d33960bSJohannes Berg rcu_read_lock(); 5044d33960bSJohannes Berg return err; 5058c71df7aSGuy Eilam } 5068c71df7aSGuy Eilam 5078c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) 5088c71df7aSGuy Eilam { 5098c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 5108c71df7aSGuy Eilam int err = 0; 5118c71df7aSGuy Eilam 5124d33960bSJohannes Berg might_sleep(); 5134d33960bSJohannes Berg 5148c71df7aSGuy Eilam err = sta_info_insert_check(sta); 5158c71df7aSGuy Eilam if (err) { 5168c71df7aSGuy Eilam rcu_read_lock(); 5178c71df7aSGuy Eilam goto out_free; 5188c71df7aSGuy Eilam } 5198c71df7aSGuy Eilam 520004c872eSJohannes Berg mutex_lock(&local->sta_mtx); 521004c872eSJohannes Berg 5224d33960bSJohannes Berg err = sta_info_insert_finish(sta); 5238c71df7aSGuy Eilam if (err) 524004c872eSJohannes Berg goto out_free; 525d0709a65SJohannes Berg 526004c872eSJohannes Berg return 0; 52793e5deb1SJohannes Berg out_free: 52893e5deb1SJohannes Berg BUG_ON(!err); 529d9a7ddb0SJohannes Berg sta_info_free(local, sta); 53093e5deb1SJohannes Berg return err; 531f0706e82SJiri Benc } 532f0706e82SJiri Benc 53334e89507SJohannes Berg int sta_info_insert(struct sta_info *sta) 53434e89507SJohannes Berg { 53534e89507SJohannes Berg int err = sta_info_insert_rcu(sta); 53634e89507SJohannes Berg 53734e89507SJohannes Berg rcu_read_unlock(); 53834e89507SJohannes Berg 53934e89507SJohannes Berg return err; 54034e89507SJohannes Berg } 54134e89507SJohannes Berg 542d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id) 543004c872eSJohannes Berg { 544004c872eSJohannes Berg /* 545004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 546004c872eSJohannes Berg * so this line may not be changed to use the __set_bit() format. 547004c872eSJohannes Berg */ 548d012a605SMarco Porsch tim[id / 8] |= (1 << (id % 8)); 549004c872eSJohannes Berg } 550004c872eSJohannes Berg 551d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id) 552004c872eSJohannes Berg { 553004c872eSJohannes Berg /* 554004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 555004c872eSJohannes Berg * so this line may not be changed to use the __clear_bit() format. 556004c872eSJohannes Berg */ 557d012a605SMarco Porsch tim[id / 8] &= ~(1 << (id % 8)); 558004c872eSJohannes Berg } 559004c872eSJohannes Berg 560948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac) 561004c872eSJohannes Berg { 562948d887dSJohannes Berg /* If we ever support TIDs > 7, this obviously needs to be adjusted */ 563948d887dSJohannes Berg switch (ac) { 564948d887dSJohannes Berg case IEEE80211_AC_VO: 565948d887dSJohannes Berg return BIT(6) | BIT(7); 566948d887dSJohannes Berg case IEEE80211_AC_VI: 567948d887dSJohannes Berg return BIT(4) | BIT(5); 568948d887dSJohannes Berg case IEEE80211_AC_BE: 569948d887dSJohannes Berg return BIT(0) | BIT(3); 570948d887dSJohannes Berg case IEEE80211_AC_BK: 571948d887dSJohannes Berg return BIT(1) | BIT(2); 572948d887dSJohannes Berg default: 573948d887dSJohannes Berg WARN_ON(1); 574948d887dSJohannes Berg return 0; 575d0709a65SJohannes Berg } 576004c872eSJohannes Berg } 577004c872eSJohannes Berg 578c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta) 579004c872eSJohannes Berg { 580c868cb35SJohannes Berg struct ieee80211_local *local = sta->local; 581d012a605SMarco Porsch struct ps_data *ps; 582d0709a65SJohannes Berg unsigned long flags; 583948d887dSJohannes Berg bool indicate_tim = false; 584948d887dSJohannes Berg u8 ignore_for_tim = sta->sta.uapsd_queues; 585948d887dSJohannes Berg int ac; 586d012a605SMarco Porsch u16 id; 587004c872eSJohannes Berg 588d012a605SMarco Porsch if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 589d012a605SMarco Porsch sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 590c868cb35SJohannes Berg if (WARN_ON_ONCE(!sta->sdata->bss)) 591c868cb35SJohannes Berg return; 5923e122be0SJohannes Berg 593d012a605SMarco Porsch ps = &sta->sdata->bss->ps; 594d012a605SMarco Porsch id = sta->sta.aid; 595d012a605SMarco Porsch } else { 596d012a605SMarco Porsch return; 597d012a605SMarco Porsch } 598d012a605SMarco Porsch 599c868cb35SJohannes Berg /* No need to do anything if the driver does all */ 600c868cb35SJohannes Berg if (local->hw.flags & IEEE80211_HW_AP_LINK_PS) 601c868cb35SJohannes Berg return; 602004c872eSJohannes Berg 603c868cb35SJohannes Berg if (sta->dead) 604c868cb35SJohannes Berg goto done; 6053e122be0SJohannes Berg 606948d887dSJohannes Berg /* 607948d887dSJohannes Berg * If all ACs are delivery-enabled then we should build 608948d887dSJohannes Berg * the TIM bit for all ACs anyway; if only some are then 609948d887dSJohannes Berg * we ignore those and build the TIM bit using only the 610948d887dSJohannes Berg * non-enabled ones. 611948d887dSJohannes Berg */ 612948d887dSJohannes Berg if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1) 613948d887dSJohannes Berg ignore_for_tim = 0; 614948d887dSJohannes Berg 615948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 616948d887dSJohannes Berg unsigned long tids; 617948d887dSJohannes Berg 618948d887dSJohannes Berg if (ignore_for_tim & BIT(ac)) 619948d887dSJohannes Berg continue; 620948d887dSJohannes Berg 621948d887dSJohannes Berg indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) || 622948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac]); 623948d887dSJohannes Berg if (indicate_tim) 624948d887dSJohannes Berg break; 625948d887dSJohannes Berg 626948d887dSJohannes Berg tids = ieee80211_tids_for_ac(ac); 627948d887dSJohannes Berg 628948d887dSJohannes Berg indicate_tim |= 629948d887dSJohannes Berg sta->driver_buffered_tids & tids; 630004c872eSJohannes Berg } 631004c872eSJohannes Berg 632c868cb35SJohannes Berg done: 6334d33960bSJohannes Berg spin_lock_irqsave(&local->tim_lock, flags); 634004c872eSJohannes Berg 635948d887dSJohannes Berg if (indicate_tim) 636d012a605SMarco Porsch __bss_tim_set(ps->tim, id); 637c868cb35SJohannes Berg else 638d012a605SMarco Porsch __bss_tim_clear(ps->tim, id); 6393e122be0SJohannes Berg 640c868cb35SJohannes Berg if (local->ops->set_tim) { 641c868cb35SJohannes Berg local->tim_in_locked_section = true; 642948d887dSJohannes Berg drv_set_tim(local, &sta->sta, indicate_tim); 643c868cb35SJohannes Berg local->tim_in_locked_section = false; 644004c872eSJohannes Berg } 645004c872eSJohannes Berg 6464d33960bSJohannes Berg spin_unlock_irqrestore(&local->tim_lock, flags); 647004c872eSJohannes Berg } 648004c872eSJohannes Berg 649cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb) 650f0706e82SJiri Benc { 651e039fa4aSJohannes Berg struct ieee80211_tx_info *info; 652f0706e82SJiri Benc int timeout; 653f0706e82SJiri Benc 654f0706e82SJiri Benc if (!skb) 655cd0b8d89SJohannes Berg return false; 656f0706e82SJiri Benc 657e039fa4aSJohannes Berg info = IEEE80211_SKB_CB(skb); 658f0706e82SJiri Benc 659f0706e82SJiri Benc /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 66057c4d7b4SJohannes Berg timeout = (sta->listen_interval * 66157c4d7b4SJohannes Berg sta->sdata->vif.bss_conf.beacon_int * 66257c4d7b4SJohannes Berg 32 / 15625) * HZ; 663f0706e82SJiri Benc if (timeout < STA_TX_BUFFER_EXPIRE) 664f0706e82SJiri Benc timeout = STA_TX_BUFFER_EXPIRE; 665e039fa4aSJohannes Berg return time_after(jiffies, info->control.jiffies + timeout); 666f0706e82SJiri Benc } 667f0706e82SJiri Benc 668f0706e82SJiri Benc 669948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local, 670948d887dSJohannes Berg struct sta_info *sta, int ac) 671f0706e82SJiri Benc { 672f0706e82SJiri Benc unsigned long flags; 673f0706e82SJiri Benc struct sk_buff *skb; 674f0706e82SJiri Benc 67560750397SJohannes Berg /* 67660750397SJohannes Berg * First check for frames that should expire on the filtered 67760750397SJohannes Berg * queue. Frames here were rejected by the driver and are on 67860750397SJohannes Berg * a separate queue to avoid reordering with normal PS-buffered 67960750397SJohannes Berg * frames. They also aren't accounted for right now in the 68060750397SJohannes Berg * total_ps_buffered counter. 68160750397SJohannes Berg */ 682f0706e82SJiri Benc for (;;) { 683948d887dSJohannes Berg spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 684948d887dSJohannes Berg skb = skb_peek(&sta->tx_filtered[ac]); 68557c4d7b4SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 686948d887dSJohannes Berg skb = __skb_dequeue(&sta->tx_filtered[ac]); 687836341a7SJohannes Berg else 688f0706e82SJiri Benc skb = NULL; 689948d887dSJohannes Berg spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 690f0706e82SJiri Benc 69160750397SJohannes Berg /* 69260750397SJohannes Berg * Frames are queued in order, so if this one 69360750397SJohannes Berg * hasn't expired yet we can stop testing. If 69460750397SJohannes Berg * we actually reached the end of the queue we 69560750397SJohannes Berg * also need to stop, of course. 69660750397SJohannes Berg */ 69760750397SJohannes Berg if (!skb) 69860750397SJohannes Berg break; 699d4fa14cdSFelix Fietkau ieee80211_free_txskb(&local->hw, skb); 70060750397SJohannes Berg } 70160750397SJohannes Berg 70260750397SJohannes Berg /* 70360750397SJohannes Berg * Now also check the normal PS-buffered queue, this will 70460750397SJohannes Berg * only find something if the filtered queue was emptied 70560750397SJohannes Berg * since the filtered frames are all before the normal PS 70660750397SJohannes Berg * buffered frames. 70760750397SJohannes Berg */ 708f0706e82SJiri Benc for (;;) { 709948d887dSJohannes Berg spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 710948d887dSJohannes Berg skb = skb_peek(&sta->ps_tx_buf[ac]); 711f0706e82SJiri Benc if (sta_info_buffer_expired(sta, skb)) 712948d887dSJohannes Berg skb = __skb_dequeue(&sta->ps_tx_buf[ac]); 713f0706e82SJiri Benc else 714f0706e82SJiri Benc skb = NULL; 715948d887dSJohannes Berg spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 716f0706e82SJiri Benc 71760750397SJohannes Berg /* 71860750397SJohannes Berg * frames are queued in order, so if this one 71960750397SJohannes Berg * hasn't expired yet (or we reached the end of 72060750397SJohannes Berg * the queue) we can stop testing 72160750397SJohannes Berg */ 722836341a7SJohannes Berg if (!skb) 723836341a7SJohannes Berg break; 724836341a7SJohannes Berg 725f0706e82SJiri Benc local->total_ps_buffered--; 726bdcbd8e0SJohannes Berg ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n", 727bdcbd8e0SJohannes Berg sta->sta.addr); 728d4fa14cdSFelix Fietkau ieee80211_free_txskb(&local->hw, skb); 729f0706e82SJiri Benc } 7303393a608SJuuso Oikarinen 73160750397SJohannes Berg /* 73260750397SJohannes Berg * Finally, recalculate the TIM bit for this station -- it might 73360750397SJohannes Berg * now be clear because the station was too slow to retrieve its 73460750397SJohannes Berg * frames. 73560750397SJohannes Berg */ 73660750397SJohannes Berg sta_info_recalc_tim(sta); 73760750397SJohannes Berg 73860750397SJohannes Berg /* 73960750397SJohannes Berg * Return whether there are any frames still buffered, this is 74060750397SJohannes Berg * used to check whether the cleanup timer still needs to run, 74160750397SJohannes Berg * if there are no frames we don't need to rearm the timer. 74260750397SJohannes Berg */ 743948d887dSJohannes Berg return !(skb_queue_empty(&sta->ps_tx_buf[ac]) && 744948d887dSJohannes Berg skb_queue_empty(&sta->tx_filtered[ac])); 745948d887dSJohannes Berg } 746948d887dSJohannes Berg 747948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 748948d887dSJohannes Berg struct sta_info *sta) 749948d887dSJohannes Berg { 750948d887dSJohannes Berg bool have_buffered = false; 751948d887dSJohannes Berg int ac; 752948d887dSJohannes Berg 753948d887dSJohannes Berg /* This is only necessary for stations on BSS interfaces */ 754948d887dSJohannes Berg if (!sta->sdata->bss) 755948d887dSJohannes Berg return false; 756948d887dSJohannes Berg 757948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 758948d887dSJohannes Berg have_buffered |= 759948d887dSJohannes Berg sta_info_cleanup_expire_buffered_ac(local, sta, ac); 760948d887dSJohannes Berg 761948d887dSJohannes Berg return have_buffered; 762f0706e82SJiri Benc } 763f0706e82SJiri Benc 76483d5cc01SJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta) 76534e89507SJohannes Berg { 76634e89507SJohannes Berg struct ieee80211_local *local; 76734e89507SJohannes Berg struct ieee80211_sub_if_data *sdata; 768b22cfcfcSEliad Peller int ret, i; 76934e89507SJohannes Berg 77034e89507SJohannes Berg might_sleep(); 77134e89507SJohannes Berg 77234e89507SJohannes Berg if (!sta) 77334e89507SJohannes Berg return -ENOENT; 77434e89507SJohannes Berg 77534e89507SJohannes Berg local = sta->local; 77634e89507SJohannes Berg sdata = sta->sdata; 77734e89507SJohannes Berg 77883d5cc01SJohannes Berg lockdep_assert_held(&local->sta_mtx); 77983d5cc01SJohannes Berg 780098a6070SJohannes Berg /* 781098a6070SJohannes Berg * Before removing the station from the driver and 782098a6070SJohannes Berg * rate control, it might still start new aggregation 783098a6070SJohannes Berg * sessions -- block that to make sure the tear-down 784098a6070SJohannes Berg * will be sufficient. 785098a6070SJohannes Berg */ 786c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_BLOCK_BA); 787582bb505SJohannes Berg ieee80211_sta_tear_down_BA_sessions(sta, false); 788098a6070SJohannes Berg 78934e89507SJohannes Berg ret = sta_info_hash_del(local, sta); 79034e89507SJohannes Berg if (ret) 79134e89507SJohannes Berg return ret; 79234e89507SJohannes Berg 793794454ceSArik Nemtsov list_del_rcu(&sta->list); 7944d33960bSJohannes Berg 7958cb23153SJohannes Berg mutex_lock(&local->key_mtx); 796e31b8213SJohannes Berg for (i = 0; i < NUM_DEFAULT_KEYS; i++) 79740b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i])); 798e31b8213SJohannes Berg if (sta->ptk) 79940b275b6SJohannes Berg __ieee80211_key_free(key_mtx_dereference(local, sta->ptk)); 8008cb23153SJohannes Berg mutex_unlock(&local->key_mtx); 80134e89507SJohannes Berg 80234e89507SJohannes Berg sta->dead = true; 80334e89507SJohannes Berg 80434e89507SJohannes Berg local->num_sta--; 80534e89507SJohannes Berg local->sta_generation++; 80634e89507SJohannes Berg 80734e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 808a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(sdata->u.vlan.sta, NULL); 80934e89507SJohannes Berg 81083d5cc01SJohannes Berg while (sta->sta_state > IEEE80211_STA_NONE) { 811f09603a2SJohannes Berg ret = sta_info_move_state(sta, sta->sta_state - 1); 812f09603a2SJohannes Berg if (ret) { 81383d5cc01SJohannes Berg WARN_ON_ONCE(1); 81483d5cc01SJohannes Berg break; 81583d5cc01SJohannes Berg } 81683d5cc01SJohannes Berg } 817d9a7ddb0SJohannes Berg 818f09603a2SJohannes Berg if (sta->uploaded) { 819f09603a2SJohannes Berg ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE, 820f09603a2SJohannes Berg IEEE80211_STA_NOTEXIST); 821f09603a2SJohannes Berg WARN_ON_ONCE(ret != 0); 822f09603a2SJohannes Berg } 82334e89507SJohannes Berg 824bdcbd8e0SJohannes Berg sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr); 825bdcbd8e0SJohannes Berg 826ec15e68bSJouni Malinen cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL); 827ec15e68bSJouni Malinen 82834e89507SJohannes Berg rate_control_remove_sta_debugfs(sta); 82934e89507SJohannes Berg ieee80211_sta_debugfs_remove(sta); 83034e89507SJohannes Berg 831b22cfcfcSEliad Peller call_rcu(&sta->rcu_head, free_sta_rcu); 83234e89507SJohannes Berg 83334e89507SJohannes Berg return 0; 83434e89507SJohannes Berg } 83534e89507SJohannes Berg 83634e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) 83734e89507SJohannes Berg { 83834e89507SJohannes Berg struct sta_info *sta; 83934e89507SJohannes Berg int ret; 84034e89507SJohannes Berg 84134e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 8427852e361SJohannes Berg sta = sta_info_get(sdata, addr); 84334e89507SJohannes Berg ret = __sta_info_destroy(sta); 84434e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 84534e89507SJohannes Berg 84634e89507SJohannes Berg return ret; 84734e89507SJohannes Berg } 84834e89507SJohannes Berg 84934e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, 85034e89507SJohannes Berg const u8 *addr) 85134e89507SJohannes Berg { 85234e89507SJohannes Berg struct sta_info *sta; 85334e89507SJohannes Berg int ret; 85434e89507SJohannes Berg 85534e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 8567852e361SJohannes Berg sta = sta_info_get_bss(sdata, addr); 85734e89507SJohannes Berg ret = __sta_info_destroy(sta); 85834e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 85934e89507SJohannes Berg 86034e89507SJohannes Berg return ret; 86134e89507SJohannes Berg } 862f0706e82SJiri Benc 863f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data) 864f0706e82SJiri Benc { 865f0706e82SJiri Benc struct ieee80211_local *local = (struct ieee80211_local *) data; 866f0706e82SJiri Benc struct sta_info *sta; 8673393a608SJuuso Oikarinen bool timer_needed = false; 868f0706e82SJiri Benc 869d0709a65SJohannes Berg rcu_read_lock(); 870d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) 8713393a608SJuuso Oikarinen if (sta_info_cleanup_expire_buffered(local, sta)) 8723393a608SJuuso Oikarinen timer_needed = true; 873d0709a65SJohannes Berg rcu_read_unlock(); 874f0706e82SJiri Benc 8755bb644a0SJohannes Berg if (local->quiescing) 8765bb644a0SJohannes Berg return; 8775bb644a0SJohannes Berg 8783393a608SJuuso Oikarinen if (!timer_needed) 8793393a608SJuuso Oikarinen return; 8803393a608SJuuso Oikarinen 88126d59535SJohannes Berg mod_timer(&local->sta_cleanup, 88226d59535SJohannes Berg round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 883f0706e82SJiri Benc } 884f0706e82SJiri Benc 885f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local) 886f0706e82SJiri Benc { 8874d33960bSJohannes Berg spin_lock_init(&local->tim_lock); 88834e89507SJohannes Berg mutex_init(&local->sta_mtx); 889f0706e82SJiri Benc INIT_LIST_HEAD(&local->sta_list); 890f0706e82SJiri Benc 891b24b8a24SPavel Emelyanov setup_timer(&local->sta_cleanup, sta_info_cleanup, 892b24b8a24SPavel Emelyanov (unsigned long)local); 893f0706e82SJiri Benc } 894f0706e82SJiri Benc 895f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local) 896f0706e82SJiri Benc { 897a56f992cSJohannes Berg del_timer_sync(&local->sta_cleanup); 898f0706e82SJiri Benc } 899f0706e82SJiri Benc 900*051007d9SJohannes Berg 901*051007d9SJohannes Berg int sta_info_flush_defer(struct ieee80211_sub_if_data *sdata) 902f0706e82SJiri Benc { 903b998e8bbSJohannes Berg struct ieee80211_local *local = sdata->local; 904f0706e82SJiri Benc struct sta_info *sta, *tmp; 90544213b5eSJohannes Berg int ret = 0; 906f0706e82SJiri Benc 907d0709a65SJohannes Berg might_sleep(); 908d0709a65SJohannes Berg 90934e89507SJohannes Berg mutex_lock(&local->sta_mtx); 91034e89507SJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 911b998e8bbSJohannes Berg if (sdata == sta->sdata) { 91234e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 91334316837SJohannes Berg ret++; 91434316837SJohannes Berg } 91534e89507SJohannes Berg } 91634e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 91744213b5eSJohannes Berg 918*051007d9SJohannes Berg return ret; 919*051007d9SJohannes Berg } 920*051007d9SJohannes Berg 921*051007d9SJohannes Berg void sta_info_flush_cleanup(struct ieee80211_sub_if_data *sdata) 922*051007d9SJohannes Berg { 92397f97b1fSJohannes Berg rcu_barrier(); 92497f97b1fSJohannes Berg 92597f97b1fSJohannes Berg ieee80211_cleanup_sdata_stas(sdata); 92697f97b1fSJohannes Berg cancel_work_sync(&sdata->cleanup_stations_wk); 927f0706e82SJiri Benc } 928dc6676b7SJohannes Berg 92924723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 93024723d1bSJohannes Berg unsigned long exp_time) 93124723d1bSJohannes Berg { 93224723d1bSJohannes Berg struct ieee80211_local *local = sdata->local; 93324723d1bSJohannes Berg struct sta_info *sta, *tmp; 93424723d1bSJohannes Berg 93534e89507SJohannes Berg mutex_lock(&local->sta_mtx); 936e46a2cf9SMohammed Shafi Shajakhan 937e46a2cf9SMohammed Shafi Shajakhan list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 938ec2b774eSMarek Lindner if (sdata != sta->sdata) 939ec2b774eSMarek Lindner continue; 940ec2b774eSMarek Lindner 94124723d1bSJohannes Berg if (time_after(jiffies, sta->last_rx + exp_time)) { 942eea57d42SMohammed Shafi Shajakhan sta_dbg(sta->sdata, "expiring inactive STA %pM\n", 943bdcbd8e0SJohannes Berg sta->sta.addr); 94434e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 94524723d1bSJohannes Berg } 946e46a2cf9SMohammed Shafi Shajakhan } 947e46a2cf9SMohammed Shafi Shajakhan 94834e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 94924723d1bSJohannes Berg } 95017741cdcSJohannes Berg 951686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 952686b9cb9SBen Greear const u8 *addr, 953686b9cb9SBen Greear const u8 *localaddr) 95417741cdcSJohannes Berg { 955abe60632SJohannes Berg struct sta_info *sta, *nxt; 95617741cdcSJohannes Berg 957686b9cb9SBen Greear /* 958686b9cb9SBen Greear * Just return a random station if localaddr is NULL 959686b9cb9SBen Greear * ... first in list. 960686b9cb9SBen Greear */ 961f7c65594SJohannes Berg for_each_sta_info(hw_to_local(hw), addr, sta, nxt) { 962686b9cb9SBen Greear if (localaddr && 963b203ca39SJoe Perches !ether_addr_equal(sta->sdata->vif.addr, localaddr)) 964686b9cb9SBen Greear continue; 965f7c65594SJohannes Berg if (!sta->uploaded) 966f7c65594SJohannes Berg return NULL; 96717741cdcSJohannes Berg return &sta->sta; 968f7c65594SJohannes Berg } 969f7c65594SJohannes Berg 970abe60632SJohannes Berg return NULL; 97117741cdcSJohannes Berg } 972686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); 9735ed176e1SJohannes Berg 9745ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, 9755ed176e1SJohannes Berg const u8 *addr) 9765ed176e1SJohannes Berg { 977f7c65594SJohannes Berg struct sta_info *sta; 9785ed176e1SJohannes Berg 9795ed176e1SJohannes Berg if (!vif) 9805ed176e1SJohannes Berg return NULL; 9815ed176e1SJohannes Berg 982f7c65594SJohannes Berg sta = sta_info_get_bss(vif_to_sdata(vif), addr); 983f7c65594SJohannes Berg if (!sta) 984f7c65594SJohannes Berg return NULL; 9855ed176e1SJohannes Berg 986f7c65594SJohannes Berg if (!sta->uploaded) 987f7c65594SJohannes Berg return NULL; 988f7c65594SJohannes Berg 989f7c65594SJohannes Berg return &sta->sta; 9905ed176e1SJohannes Berg } 99117741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta); 992af818581SJohannes Berg 99350a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta) 99450a9432dSJohannes Berg { 99550a9432dSJohannes Berg struct sta_info *sta = _sta; 996608383bfSHelmut Schaa struct ieee80211_sub_if_data *sdata = sta->sdata; 997d012a605SMarco Porsch struct ps_data *ps; 998d012a605SMarco Porsch 999d012a605SMarco Porsch if (sdata->vif.type == NL80211_IFTYPE_AP || 1000d012a605SMarco Porsch sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 1001d012a605SMarco Porsch ps = &sdata->bss->ps; 1002d012a605SMarco Porsch else 1003d012a605SMarco Porsch return; 100450a9432dSJohannes Berg 1005c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 1006608383bfSHelmut Schaa if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA)) 1007d012a605SMarco Porsch atomic_dec(&ps->num_sta_ps); 100850a9432dSJohannes Berg } 100950a9432dSJohannes Berg 1010af818581SJohannes Berg /* powersave support code */ 1011af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) 1012af818581SJohannes Berg { 1013af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1014af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 1015948d887dSJohannes Berg struct sk_buff_head pending; 1016948d887dSJohannes Berg int filtered = 0, buffered = 0, ac; 1017987c285cSArik Nemtsov unsigned long flags; 1018af818581SJohannes Berg 1019c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_SP); 102047086fc5SJohannes Berg 10215a306f58SJohannes Berg BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1); 1022948d887dSJohannes Berg sta->driver_buffered_tids = 0; 1023948d887dSJohannes Berg 1024d057e5a3SArik Nemtsov if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS)) 102512375ef9SJohannes Berg drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 1026af818581SJohannes Berg 1027948d887dSJohannes Berg skb_queue_head_init(&pending); 1028af818581SJohannes Berg 1029af818581SJohannes Berg /* Send all buffered frames to the station */ 1030948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1031948d887dSJohannes Berg int count = skb_queue_len(&pending), tmp; 1032948d887dSJohannes Berg 1033987c285cSArik Nemtsov spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 1034948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending); 1035987c285cSArik Nemtsov spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 1036948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1037948d887dSJohannes Berg filtered += tmp - count; 1038948d887dSJohannes Berg count = tmp; 1039948d887dSJohannes Berg 1040987c285cSArik Nemtsov spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 1041948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending); 1042987c285cSArik Nemtsov spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 1043948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1044948d887dSJohannes Berg buffered += tmp - count; 1045948d887dSJohannes Berg } 1046948d887dSJohannes Berg 1047948d887dSJohannes Berg ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta); 1048948d887dSJohannes Berg 1049af818581SJohannes Berg local->total_ps_buffered -= buffered; 1050af818581SJohannes Berg 1051c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1052c868cb35SJohannes Berg 1053bdcbd8e0SJohannes Berg ps_dbg(sdata, 1054bdcbd8e0SJohannes Berg "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n", 1055bdcbd8e0SJohannes Berg sta->sta.addr, sta->sta.aid, filtered, buffered); 1056af818581SJohannes Berg } 1057af818581SJohannes Berg 1058ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata, 1059ce662b44SJohannes Berg struct sta_info *sta, int tid, 106040b96408SJohannes Berg enum ieee80211_frame_release_type reason) 1061ce662b44SJohannes Berg { 1062ce662b44SJohannes Berg struct ieee80211_local *local = sdata->local; 1063ce662b44SJohannes Berg struct ieee80211_qos_hdr *nullfunc; 1064ce662b44SJohannes Berg struct sk_buff *skb; 1065ce662b44SJohannes Berg int size = sizeof(*nullfunc); 1066ce662b44SJohannes Berg __le16 fc; 1067c2c98fdeSJohannes Berg bool qos = test_sta_flag(sta, WLAN_STA_WME); 1068ce662b44SJohannes Berg struct ieee80211_tx_info *info; 106955de908aSJohannes Berg struct ieee80211_chanctx_conf *chanctx_conf; 1070ce662b44SJohannes Berg 1071ce662b44SJohannes Berg if (qos) { 1072ce662b44SJohannes Berg fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1073ce662b44SJohannes Berg IEEE80211_STYPE_QOS_NULLFUNC | 1074ce662b44SJohannes Berg IEEE80211_FCTL_FROMDS); 1075ce662b44SJohannes Berg } else { 1076ce662b44SJohannes Berg size -= 2; 1077ce662b44SJohannes Berg fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1078ce662b44SJohannes Berg IEEE80211_STYPE_NULLFUNC | 1079ce662b44SJohannes Berg IEEE80211_FCTL_FROMDS); 1080ce662b44SJohannes Berg } 1081ce662b44SJohannes Berg 1082ce662b44SJohannes Berg skb = dev_alloc_skb(local->hw.extra_tx_headroom + size); 1083ce662b44SJohannes Berg if (!skb) 1084ce662b44SJohannes Berg return; 1085ce662b44SJohannes Berg 1086ce662b44SJohannes Berg skb_reserve(skb, local->hw.extra_tx_headroom); 1087ce662b44SJohannes Berg 1088ce662b44SJohannes Berg nullfunc = (void *) skb_put(skb, size); 1089ce662b44SJohannes Berg nullfunc->frame_control = fc; 1090ce662b44SJohannes Berg nullfunc->duration_id = 0; 1091ce662b44SJohannes Berg memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN); 1092ce662b44SJohannes Berg memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN); 1093ce662b44SJohannes Berg memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN); 1094ce662b44SJohannes Berg 1095ce662b44SJohannes Berg skb->priority = tid; 1096ce662b44SJohannes Berg skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]); 109759b66255SJohannes Berg if (qos) { 1098ce662b44SJohannes Berg nullfunc->qos_ctrl = cpu_to_le16(tid); 1099ce662b44SJohannes Berg 110040b96408SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_UAPSD) 1101ce662b44SJohannes Berg nullfunc->qos_ctrl |= 1102ce662b44SJohannes Berg cpu_to_le16(IEEE80211_QOS_CTL_EOSP); 1103ce662b44SJohannes Berg } 1104ce662b44SJohannes Berg 1105ce662b44SJohannes Berg info = IEEE80211_SKB_CB(skb); 1106ce662b44SJohannes Berg 1107ce662b44SJohannes Berg /* 1108ce662b44SJohannes Berg * Tell TX path to send this frame even though the 1109ce662b44SJohannes Berg * STA may still remain is PS mode after this frame 1110deeaee19SJohannes Berg * exchange. Also set EOSP to indicate this packet 1111deeaee19SJohannes Berg * ends the poll/service period. 1112ce662b44SJohannes Berg */ 111302f2f1a9SJohannes Berg info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER | 1114deeaee19SJohannes Berg IEEE80211_TX_STATUS_EOSP | 1115ce662b44SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 1116ce662b44SJohannes Berg 111740b96408SJohannes Berg drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false); 111840b96408SJohannes Berg 111955de908aSJohannes Berg rcu_read_lock(); 112055de908aSJohannes Berg chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf); 112155de908aSJohannes Berg if (WARN_ON(!chanctx_conf)) { 112255de908aSJohannes Berg rcu_read_unlock(); 112355de908aSJohannes Berg kfree_skb(skb); 112455de908aSJohannes Berg return; 112555de908aSJohannes Berg } 112655de908aSJohannes Berg 11274bf88530SJohannes Berg ieee80211_xmit(sdata, skb, chanctx_conf->def.chan->band); 112855de908aSJohannes Berg rcu_read_unlock(); 1129ce662b44SJohannes Berg } 1130ce662b44SJohannes Berg 113147086fc5SJohannes Berg static void 113247086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta, 113347086fc5SJohannes Berg int n_frames, u8 ignored_acs, 113447086fc5SJohannes Berg enum ieee80211_frame_release_type reason) 1135af818581SJohannes Berg { 1136af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1137af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 11384049e09aSJohannes Berg bool found = false; 1139948d887dSJohannes Berg bool more_data = false; 1140948d887dSJohannes Berg int ac; 11414049e09aSJohannes Berg unsigned long driver_release_tids = 0; 114247086fc5SJohannes Berg struct sk_buff_head frames; 114347086fc5SJohannes Berg 1144deeaee19SJohannes Berg /* Service or PS-Poll period starts */ 1145c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_SP); 1146deeaee19SJohannes Berg 114747086fc5SJohannes Berg __skb_queue_head_init(&frames); 1148af818581SJohannes Berg 1149948d887dSJohannes Berg /* 115047086fc5SJohannes Berg * Get response frame(s) and more data bit for it. 1151948d887dSJohannes Berg */ 1152948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 11534049e09aSJohannes Berg unsigned long tids; 11544049e09aSJohannes Berg 115547086fc5SJohannes Berg if (ignored_acs & BIT(ac)) 1156948d887dSJohannes Berg continue; 1157948d887dSJohannes Berg 11584049e09aSJohannes Berg tids = ieee80211_tids_for_ac(ac); 11594049e09aSJohannes Berg 11604049e09aSJohannes Berg if (!found) { 11614049e09aSJohannes Berg driver_release_tids = sta->driver_buffered_tids & tids; 11624049e09aSJohannes Berg if (driver_release_tids) { 11634049e09aSJohannes Berg found = true; 11644049e09aSJohannes Berg } else { 116547086fc5SJohannes Berg struct sk_buff *skb; 116647086fc5SJohannes Berg 116747086fc5SJohannes Berg while (n_frames > 0) { 1168948d887dSJohannes Berg skb = skb_dequeue(&sta->tx_filtered[ac]); 1169948d887dSJohannes Berg if (!skb) { 117047086fc5SJohannes Berg skb = skb_dequeue( 117147086fc5SJohannes Berg &sta->ps_tx_buf[ac]); 1172af818581SJohannes Berg if (skb) 1173af818581SJohannes Berg local->total_ps_buffered--; 1174af818581SJohannes Berg } 117547086fc5SJohannes Berg if (!skb) 117647086fc5SJohannes Berg break; 117747086fc5SJohannes Berg n_frames--; 11784049e09aSJohannes Berg found = true; 117947086fc5SJohannes Berg __skb_queue_tail(&frames, skb); 118047086fc5SJohannes Berg } 1181948d887dSJohannes Berg } 1182948d887dSJohannes Berg 11834049e09aSJohannes Berg /* 11844049e09aSJohannes Berg * If the driver has data on more than one TID then 11854049e09aSJohannes Berg * certainly there's more data if we release just a 11864049e09aSJohannes Berg * single frame now (from a single TID). 11874049e09aSJohannes Berg */ 118847086fc5SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_PSPOLL && 118947086fc5SJohannes Berg hweight16(driver_release_tids) > 1) { 11904049e09aSJohannes Berg more_data = true; 11914049e09aSJohannes Berg driver_release_tids = 11924049e09aSJohannes Berg BIT(ffs(driver_release_tids) - 1); 11934049e09aSJohannes Berg break; 11944049e09aSJohannes Berg } 11954049e09aSJohannes Berg } 1196948d887dSJohannes Berg 1197948d887dSJohannes Berg if (!skb_queue_empty(&sta->tx_filtered[ac]) || 1198948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac])) { 1199948d887dSJohannes Berg more_data = true; 1200948d887dSJohannes Berg break; 1201948d887dSJohannes Berg } 1202948d887dSJohannes Berg } 1203af818581SJohannes Berg 12044049e09aSJohannes Berg if (!found) { 1205ce662b44SJohannes Berg int tid; 12064049e09aSJohannes Berg 1207ce662b44SJohannes Berg /* 1208ce662b44SJohannes Berg * For PS-Poll, this can only happen due to a race condition 1209ce662b44SJohannes Berg * when we set the TIM bit and the station notices it, but 1210ce662b44SJohannes Berg * before it can poll for the frame we expire it. 1211ce662b44SJohannes Berg * 1212ce662b44SJohannes Berg * For uAPSD, this is said in the standard (11.2.1.5 h): 1213ce662b44SJohannes Berg * At each unscheduled SP for a non-AP STA, the AP shall 1214ce662b44SJohannes Berg * attempt to transmit at least one MSDU or MMPDU, but no 1215ce662b44SJohannes Berg * more than the value specified in the Max SP Length field 1216ce662b44SJohannes Berg * in the QoS Capability element from delivery-enabled ACs, 1217ce662b44SJohannes Berg * that are destined for the non-AP STA. 1218ce662b44SJohannes Berg * 1219ce662b44SJohannes Berg * Since we have no other MSDU/MMPDU, transmit a QoS null frame. 1220ce662b44SJohannes Berg */ 1221ce662b44SJohannes Berg 1222ce662b44SJohannes Berg /* This will evaluate to 1, 3, 5 or 7. */ 1223ce662b44SJohannes Berg tid = 7 - ((ffs(~ignored_acs) - 1) << 1); 1224ce662b44SJohannes Berg 122540b96408SJohannes Berg ieee80211_send_null_response(sdata, sta, tid, reason); 12264049e09aSJohannes Berg return; 12274049e09aSJohannes Berg } 12284049e09aSJohannes Berg 122947086fc5SJohannes Berg if (!driver_release_tids) { 123047086fc5SJohannes Berg struct sk_buff_head pending; 123147086fc5SJohannes Berg struct sk_buff *skb; 123240b96408SJohannes Berg int num = 0; 123340b96408SJohannes Berg u16 tids = 0; 123447086fc5SJohannes Berg 123547086fc5SJohannes Berg skb_queue_head_init(&pending); 123647086fc5SJohannes Berg 123747086fc5SJohannes Berg while ((skb = __skb_dequeue(&frames))) { 1238af818581SJohannes Berg struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 123947086fc5SJohannes Berg struct ieee80211_hdr *hdr = (void *) skb->data; 124040b96408SJohannes Berg u8 *qoshdr = NULL; 124140b96408SJohannes Berg 124240b96408SJohannes Berg num++; 1243af818581SJohannes Berg 1244af818581SJohannes Berg /* 124547086fc5SJohannes Berg * Tell TX path to send this frame even though the 124647086fc5SJohannes Berg * STA may still remain is PS mode after this frame 124747086fc5SJohannes Berg * exchange. 1248af818581SJohannes Berg */ 124902f2f1a9SJohannes Berg info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER; 1250af818581SJohannes Berg 125147086fc5SJohannes Berg /* 125247086fc5SJohannes Berg * Use MoreData flag to indicate whether there are 125347086fc5SJohannes Berg * more buffered frames for this STA 125447086fc5SJohannes Berg */ 125524b9c373SJanusz.Dziedzic@tieto.com if (more_data || !skb_queue_empty(&frames)) 125647086fc5SJohannes Berg hdr->frame_control |= 125747086fc5SJohannes Berg cpu_to_le16(IEEE80211_FCTL_MOREDATA); 125824b9c373SJanusz.Dziedzic@tieto.com else 125924b9c373SJanusz.Dziedzic@tieto.com hdr->frame_control &= 126024b9c373SJanusz.Dziedzic@tieto.com cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 1261af818581SJohannes Berg 126240b96408SJohannes Berg if (ieee80211_is_data_qos(hdr->frame_control) || 126340b96408SJohannes Berg ieee80211_is_qos_nullfunc(hdr->frame_control)) 126440b96408SJohannes Berg qoshdr = ieee80211_get_qos_ctl(hdr); 126540b96408SJohannes Berg 126652a3f20cSMarco Porsch /* end service period after last frame */ 126752a3f20cSMarco Porsch if (skb_queue_empty(&frames)) { 126840b96408SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_UAPSD && 126952a3f20cSMarco Porsch qoshdr) 127040b96408SJohannes Berg *qoshdr |= IEEE80211_QOS_CTL_EOSP; 1271deeaee19SJohannes Berg 127247086fc5SJohannes Berg info->flags |= IEEE80211_TX_STATUS_EOSP | 127347086fc5SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 127452a3f20cSMarco Porsch } 127547086fc5SJohannes Berg 127640b96408SJohannes Berg if (qoshdr) 127740b96408SJohannes Berg tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK); 127840b96408SJohannes Berg else 127940b96408SJohannes Berg tids |= BIT(0); 128040b96408SJohannes Berg 128147086fc5SJohannes Berg __skb_queue_tail(&pending, skb); 128247086fc5SJohannes Berg } 128347086fc5SJohannes Berg 128440b96408SJohannes Berg drv_allow_buffered_frames(local, sta, tids, num, 128540b96408SJohannes Berg reason, more_data); 128640b96408SJohannes Berg 128747086fc5SJohannes Berg ieee80211_add_pending_skbs(local, &pending); 1288af818581SJohannes Berg 1289c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1290af818581SJohannes Berg } else { 1291af818581SJohannes Berg /* 12924049e09aSJohannes Berg * We need to release a frame that is buffered somewhere in the 12934049e09aSJohannes Berg * driver ... it'll have to handle that. 12944049e09aSJohannes Berg * Note that, as per the comment above, it'll also have to see 12954049e09aSJohannes Berg * if there is more than just one frame on the specific TID that 12964049e09aSJohannes Berg * we're releasing from, and it needs to set the more-data bit 12974049e09aSJohannes Berg * accordingly if we tell it that there's no more data. If we do 12984049e09aSJohannes Berg * tell it there's more data, then of course the more-data bit 12994049e09aSJohannes Berg * needs to be set anyway. 1300af818581SJohannes Berg */ 13014049e09aSJohannes Berg drv_release_buffered_frames(local, sta, driver_release_tids, 130247086fc5SJohannes Berg n_frames, reason, more_data); 13034049e09aSJohannes Berg 13044049e09aSJohannes Berg /* 13054049e09aSJohannes Berg * Note that we don't recalculate the TIM bit here as it would 13064049e09aSJohannes Berg * most likely have no effect at all unless the driver told us 13074049e09aSJohannes Berg * that the TID became empty before returning here from the 13084049e09aSJohannes Berg * release function. 13094049e09aSJohannes Berg * Either way, however, when the driver tells us that the TID 13104049e09aSJohannes Berg * became empty we'll do the TIM recalculation. 13114049e09aSJohannes Berg */ 1312af818581SJohannes Berg } 1313af818581SJohannes Berg } 1314af818581SJohannes Berg 1315af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 1316af818581SJohannes Berg { 131747086fc5SJohannes Berg u8 ignore_for_response = sta->sta.uapsd_queues; 1318af818581SJohannes Berg 1319af818581SJohannes Berg /* 132047086fc5SJohannes Berg * If all ACs are delivery-enabled then we should reply 132147086fc5SJohannes Berg * from any of them, if only some are enabled we reply 132247086fc5SJohannes Berg * only from the non-enabled ones. 1323af818581SJohannes Berg */ 132447086fc5SJohannes Berg if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1) 132547086fc5SJohannes Berg ignore_for_response = 0; 1326af818581SJohannes Berg 132747086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response, 132847086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_PSPOLL); 1329af818581SJohannes Berg } 133047086fc5SJohannes Berg 133147086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta) 133247086fc5SJohannes Berg { 133347086fc5SJohannes Berg int n_frames = sta->sta.max_sp; 133447086fc5SJohannes Berg u8 delivery_enabled = sta->sta.uapsd_queues; 133547086fc5SJohannes Berg 133647086fc5SJohannes Berg /* 133747086fc5SJohannes Berg * If we ever grow support for TSPEC this might happen if 133847086fc5SJohannes Berg * the TSPEC update from hostapd comes in between a trigger 133947086fc5SJohannes Berg * frame setting WLAN_STA_UAPSD in the RX path and this 134047086fc5SJohannes Berg * actually getting called. 134147086fc5SJohannes Berg */ 134247086fc5SJohannes Berg if (!delivery_enabled) 134347086fc5SJohannes Berg return; 134447086fc5SJohannes Berg 134547086fc5SJohannes Berg switch (sta->sta.max_sp) { 134647086fc5SJohannes Berg case 1: 134747086fc5SJohannes Berg n_frames = 2; 134847086fc5SJohannes Berg break; 134947086fc5SJohannes Berg case 2: 135047086fc5SJohannes Berg n_frames = 4; 135147086fc5SJohannes Berg break; 135247086fc5SJohannes Berg case 3: 135347086fc5SJohannes Berg n_frames = 6; 135447086fc5SJohannes Berg break; 135547086fc5SJohannes Berg case 0: 135647086fc5SJohannes Berg /* XXX: what is a good value? */ 135747086fc5SJohannes Berg n_frames = 8; 135847086fc5SJohannes Berg break; 135947086fc5SJohannes Berg } 136047086fc5SJohannes Berg 136147086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled, 136247086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_UAPSD); 1363af818581SJohannes Berg } 1364af818581SJohannes Berg 1365af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 1366af818581SJohannes Berg struct ieee80211_sta *pubsta, bool block) 1367af818581SJohannes Berg { 1368af818581SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1369af818581SJohannes Berg 1370b5878a2dSJohannes Berg trace_api_sta_block_awake(sta->local, pubsta, block); 1371b5878a2dSJohannes Berg 1372af818581SJohannes Berg if (block) 1373c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_PS_DRIVER); 1374c2c98fdeSJohannes Berg else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) 1375af818581SJohannes Berg ieee80211_queue_work(hw, &sta->drv_unblock_wk); 1376af818581SJohannes Berg } 1377af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake); 1378dcf55fb5SFelix Fietkau 137937fbd908SJohannes Berg void ieee80211_sta_eosp_irqsafe(struct ieee80211_sta *pubsta) 138037fbd908SJohannes Berg { 138137fbd908SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 138237fbd908SJohannes Berg struct ieee80211_local *local = sta->local; 138337fbd908SJohannes Berg struct sk_buff *skb; 138437fbd908SJohannes Berg struct skb_eosp_msg_data *data; 138537fbd908SJohannes Berg 138637fbd908SJohannes Berg trace_api_eosp(local, pubsta); 138737fbd908SJohannes Berg 138837fbd908SJohannes Berg skb = alloc_skb(0, GFP_ATOMIC); 138937fbd908SJohannes Berg if (!skb) { 139037fbd908SJohannes Berg /* too bad ... but race is better than loss */ 139137fbd908SJohannes Berg clear_sta_flag(sta, WLAN_STA_SP); 139237fbd908SJohannes Berg return; 139337fbd908SJohannes Berg } 139437fbd908SJohannes Berg 139537fbd908SJohannes Berg data = (void *)skb->cb; 139637fbd908SJohannes Berg memcpy(data->sta, pubsta->addr, ETH_ALEN); 139737fbd908SJohannes Berg memcpy(data->iface, sta->sdata->vif.addr, ETH_ALEN); 139837fbd908SJohannes Berg skb->pkt_type = IEEE80211_EOSP_MSG; 139937fbd908SJohannes Berg skb_queue_tail(&local->skb_queue, skb); 140037fbd908SJohannes Berg tasklet_schedule(&local->tasklet); 140137fbd908SJohannes Berg } 140237fbd908SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp_irqsafe); 140337fbd908SJohannes Berg 1404042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, 1405042ec453SJohannes Berg u8 tid, bool buffered) 1406dcf55fb5SFelix Fietkau { 1407dcf55fb5SFelix Fietkau struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1408dcf55fb5SFelix Fietkau 14095a306f58SJohannes Berg if (WARN_ON(tid >= IEEE80211_NUM_TIDS)) 1410042ec453SJohannes Berg return; 1411042ec453SJohannes Berg 1412948d887dSJohannes Berg if (buffered) 1413948d887dSJohannes Berg set_bit(tid, &sta->driver_buffered_tids); 1414948d887dSJohannes Berg else 1415948d887dSJohannes Berg clear_bit(tid, &sta->driver_buffered_tids); 1416948d887dSJohannes Berg 1417c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1418dcf55fb5SFelix Fietkau } 1419042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered); 1420d9a7ddb0SJohannes Berg 142183d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta, 1422d9a7ddb0SJohannes Berg enum ieee80211_sta_state new_state) 1423d9a7ddb0SJohannes Berg { 14248bf11d8dSJohannes Berg might_sleep(); 1425d9a7ddb0SJohannes Berg 1426d9a7ddb0SJohannes Berg if (sta->sta_state == new_state) 1427d9a7ddb0SJohannes Berg return 0; 1428d9a7ddb0SJohannes Berg 1429f09603a2SJohannes Berg /* check allowed transitions first */ 1430f09603a2SJohannes Berg 1431d9a7ddb0SJohannes Berg switch (new_state) { 1432d9a7ddb0SJohannes Berg case IEEE80211_STA_NONE: 1433f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_AUTH) 1434d9a7ddb0SJohannes Berg return -EINVAL; 1435d9a7ddb0SJohannes Berg break; 1436d9a7ddb0SJohannes Berg case IEEE80211_STA_AUTH: 1437f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_NONE && 1438f09603a2SJohannes Berg sta->sta_state != IEEE80211_STA_ASSOC) 1439d9a7ddb0SJohannes Berg return -EINVAL; 1440d9a7ddb0SJohannes Berg break; 1441d9a7ddb0SJohannes Berg case IEEE80211_STA_ASSOC: 1442f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_AUTH && 1443f09603a2SJohannes Berg sta->sta_state != IEEE80211_STA_AUTHORIZED) 1444d9a7ddb0SJohannes Berg return -EINVAL; 1445d9a7ddb0SJohannes Berg break; 1446d9a7ddb0SJohannes Berg case IEEE80211_STA_AUTHORIZED: 1447f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_ASSOC) 1448d9a7ddb0SJohannes Berg return -EINVAL; 1449d9a7ddb0SJohannes Berg break; 1450d9a7ddb0SJohannes Berg default: 1451d9a7ddb0SJohannes Berg WARN(1, "invalid state %d", new_state); 1452d9a7ddb0SJohannes Berg return -EINVAL; 1453d9a7ddb0SJohannes Berg } 1454d9a7ddb0SJohannes Berg 1455bdcbd8e0SJohannes Berg sta_dbg(sta->sdata, "moving STA %pM to state %d\n", 1456bdcbd8e0SJohannes Berg sta->sta.addr, new_state); 1457f09603a2SJohannes Berg 1458f09603a2SJohannes Berg /* 1459f09603a2SJohannes Berg * notify the driver before the actual changes so it can 1460f09603a2SJohannes Berg * fail the transition 1461f09603a2SJohannes Berg */ 1462f09603a2SJohannes Berg if (test_sta_flag(sta, WLAN_STA_INSERTED)) { 1463f09603a2SJohannes Berg int err = drv_sta_state(sta->local, sta->sdata, sta, 1464f09603a2SJohannes Berg sta->sta_state, new_state); 1465f09603a2SJohannes Berg if (err) 1466f09603a2SJohannes Berg return err; 1467f09603a2SJohannes Berg } 1468f09603a2SJohannes Berg 1469f09603a2SJohannes Berg /* reflect the change in all state variables */ 1470f09603a2SJohannes Berg 1471f09603a2SJohannes Berg switch (new_state) { 1472f09603a2SJohannes Berg case IEEE80211_STA_NONE: 1473f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_AUTH) 1474f09603a2SJohannes Berg clear_bit(WLAN_STA_AUTH, &sta->_flags); 1475f09603a2SJohannes Berg break; 1476f09603a2SJohannes Berg case IEEE80211_STA_AUTH: 1477f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_NONE) 1478f09603a2SJohannes Berg set_bit(WLAN_STA_AUTH, &sta->_flags); 1479f09603a2SJohannes Berg else if (sta->sta_state == IEEE80211_STA_ASSOC) 1480f09603a2SJohannes Berg clear_bit(WLAN_STA_ASSOC, &sta->_flags); 1481f09603a2SJohannes Berg break; 1482f09603a2SJohannes Berg case IEEE80211_STA_ASSOC: 1483f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_AUTH) { 1484f09603a2SJohannes Berg set_bit(WLAN_STA_ASSOC, &sta->_flags); 1485f09603a2SJohannes Berg } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) { 14867e3ed02cSFelix Fietkau if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 14877e3ed02cSFelix Fietkau (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 14887e3ed02cSFelix Fietkau !sta->sdata->u.vlan.sta)) 14897e3ed02cSFelix Fietkau atomic_dec(&sta->sdata->bss->num_mcast_sta); 1490f09603a2SJohannes Berg clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 1491f09603a2SJohannes Berg } 1492f09603a2SJohannes Berg break; 1493f09603a2SJohannes Berg case IEEE80211_STA_AUTHORIZED: 1494f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_ASSOC) { 14957e3ed02cSFelix Fietkau if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 14967e3ed02cSFelix Fietkau (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 14977e3ed02cSFelix Fietkau !sta->sdata->u.vlan.sta)) 14987e3ed02cSFelix Fietkau atomic_inc(&sta->sdata->bss->num_mcast_sta); 1499f09603a2SJohannes Berg set_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 1500f09603a2SJohannes Berg } 1501f09603a2SJohannes Berg break; 1502f09603a2SJohannes Berg default: 1503f09603a2SJohannes Berg break; 1504f09603a2SJohannes Berg } 1505f09603a2SJohannes Berg 1506d9a7ddb0SJohannes Berg sta->sta_state = new_state; 1507d9a7ddb0SJohannes Berg 1508d9a7ddb0SJohannes Berg return 0; 1509d9a7ddb0SJohannes Berg } 1510