blob: 8f630f51d9bd1d622709b9fba53a196b60cb92dc [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
Johannes Bergd98ad832014-09-03 15:24:57 +03004 * Copyright 2013-2014 Intel Mobile Communications GmbH
Jiri Bencf0706e82007-05-05 11:45:53 -07005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12#include <linux/init.h>
Felix Fietkau888d04d2012-03-01 15:22:09 +010013#include <linux/etherdevice.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070014#include <linux/netdevice.h>
15#include <linux/types.h>
16#include <linux/slab.h>
17#include <linux/skbuff.h>
18#include <linux/if_arp.h>
Johannes Berg0d174402007-12-17 15:07:43 +010019#include <linux/timer.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010020#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070021
22#include <net/mac80211.h>
23#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020024#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040025#include "rate.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070026#include "sta_info.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070027#include "debugfs_sta.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010028#include "mesh.h"
Johannes Bergce662b442011-09-29 16:04:34 +020029#include "wme.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070030
Johannes Bergd0709a62008-02-25 16:27:46 +010031/**
32 * DOC: STA information lifetime rules
33 *
34 * STA info structures (&struct sta_info) are managed in a hash table
35 * for faster lookup and a list for iteration. They are managed using
36 * RCU, i.e. access to the list and hash table is protected by RCU.
37 *
Johannes Berg34e89502010-02-03 13:59:58 +010038 * Upon allocating a STA info structure with sta_info_alloc(), the caller
39 * owns that structure. It must then insert it into the hash table using
40 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
41 * case (which acquires an rcu read section but must not be called from
42 * within one) will the pointer still be valid after the call. Note that
43 * the caller may not do much with the STA info before inserting it, in
44 * particular, it may not start any mesh peer link management or add
45 * encryption keys.
Johannes Berg93e5deb2008-04-01 15:21:00 +020046 *
47 * When the insertion fails (sta_info_insert()) returns non-zero), the
48 * structure will have been freed by sta_info_insert()!
Johannes Bergd0709a62008-02-25 16:27:46 +010049 *
Johannes Berg34e89502010-02-03 13:59:58 +010050 * Station entries are added by mac80211 when you establish a link with a
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040051 * peer. This means different things for the different type of interfaces
52 * we support. For a regular station this mean we add the AP sta when we
Lucas De Marchi25985ed2011-03-30 22:57:33 -030053 * receive an association response from the AP. For IBSS this occurs when
Johannes Berg34e89502010-02-03 13:59:58 +010054 * get to know about a peer on the same IBSS. For WDS we add the sta for
Lucas De Marchi25985ed2011-03-30 22:57:33 -030055 * the peer immediately upon device open. When using AP mode we add stations
Johannes Berg34e89502010-02-03 13:59:58 +010056 * for each respective station upon request from userspace through nl80211.
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040057 *
Johannes Berg34e89502010-02-03 13:59:58 +010058 * In order to remove a STA info structure, various sta_info_destroy_*()
59 * calls are available.
Johannes Bergd0709a62008-02-25 16:27:46 +010060 *
Johannes Berg34e89502010-02-03 13:59:58 +010061 * There is no concept of ownership on a STA entry, each structure is
62 * owned by the global hash table/list until it is removed. All users of
63 * the structure need to be RCU protected so that the structure won't be
64 * freed before they are done using it.
Johannes Bergd0709a62008-02-25 16:27:46 +010065 */
Jiri Bencf0706e82007-05-05 11:45:53 -070066
Johannes Berg7bedd0c2015-02-13 21:55:15 +010067static const struct rhashtable_params sta_rht_params = {
68 .nelem_hint = 3, /* start small */
Johannes Bergcaf22d32015-04-24 11:10:10 +020069 .automatic_shrinking = true,
Johannes Berg7bedd0c2015-02-13 21:55:15 +010070 .head_offset = offsetof(struct sta_info, hash_node),
Johannes Bergac100ce2015-06-16 16:22:12 +020071 .key_offset = offsetof(struct sta_info, addr),
Johannes Berg7bedd0c2015-02-13 21:55:15 +010072 .key_len = ETH_ALEN,
73 .hashfn = sta_addr_hash,
Johannes Bergebd82b32015-04-23 17:26:06 +020074 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
Johannes Berg7bedd0c2015-02-13 21:55:15 +010075};
76
Johannes Berg4d339602011-12-15 11:24:20 +010077/* Caller must hold local->sta_mtx */
Michael Wube8755e2007-07-27 15:43:23 +020078static int sta_info_hash_del(struct ieee80211_local *local,
79 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070080{
Johannes Berg7bedd0c2015-02-13 21:55:15 +010081 return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
82 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -070083}
84
Johannes Berg5108ca82014-02-17 20:49:03 +010085static void __cleanup_single_sta(struct sta_info *sta)
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030086{
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030087 int ac, i;
88 struct tid_ampdu_tx *tid_tx;
89 struct ieee80211_sub_if_data *sdata = sta->sdata;
90 struct ieee80211_local *local = sdata->local;
Marco Porschd012a602012-10-10 12:39:50 -070091 struct ps_data *ps;
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030092
Johannes Berge3685e02014-02-20 11:19:58 +010093 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
Johannes Berg5ac2e352014-05-27 16:32:27 +020094 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
95 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
Marco Porschd012a602012-10-10 12:39:50 -070096 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
97 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
98 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +010099 else if (ieee80211_vif_is_mesh(&sdata->vif))
100 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700101 else
102 return;
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300103
104 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Berge3685e02014-02-20 11:19:58 +0100105 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200106 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300107
Marco Porschd012a602012-10-10 12:39:50 -0700108 atomic_dec(&ps->num_sta_ps);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300109 }
110
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100111 if (sta->sta.txq[0]) {
112 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
113 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
114 int n = skb_queue_len(&txqi->queue);
115
116 ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
117 atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
118 }
119 }
120
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300121 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
122 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
Felix Fietkau1f98ab72012-11-10 03:44:14 +0100123 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
124 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300125 }
126
Thomas Pedersen45b50282013-02-06 10:17:21 -0800127 if (ieee80211_vif_is_mesh(&sdata->vif))
128 mesh_sta_cleanup(sta);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300129
Johannes Berg5ac2e352014-05-27 16:32:27 +0200130 cancel_work_sync(&sta->drv_deliver_wk);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300131
132 /*
133 * Destroy aggregation state here. It would be nice to wait for the
134 * driver to finish aggregation stop and then clean up, but for now
135 * drivers have to handle aggregation stop being requested, followed
136 * directly by station destruction.
137 */
Johannes Berg5a306f52012-11-14 23:22:21 +0100138 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berg661eb382013-06-12 22:47:56 +0200139 kfree(sta->ampdu_mlme.tid_start_tx[i]);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300140 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
141 if (!tid_tx)
142 continue;
Felix Fietkau1f98ab72012-11-10 03:44:14 +0100143 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300144 kfree(tid_tx);
145 }
Johannes Berg5108ca82014-02-17 20:49:03 +0100146}
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300147
Johannes Berg5108ca82014-02-17 20:49:03 +0100148static void cleanup_single_sta(struct sta_info *sta)
149{
150 struct ieee80211_sub_if_data *sdata = sta->sdata;
151 struct ieee80211_local *local = sdata->local;
152
153 __cleanup_single_sta(sta);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300154 sta_info_free(local, sta);
155}
156
Johannes Bergd0709a62008-02-25 16:27:46 +0100157/* protected by RCU */
Johannes Bergabe60632009-11-25 17:46:18 +0100158struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
159 const u8 *addr)
Johannes Berg43ba7e92008-02-21 14:09:30 +0100160{
Johannes Bergabe60632009-11-25 17:46:18 +0100161 struct ieee80211_local *local = sdata->local;
Johannes Berg60f4b622015-04-23 14:02:30 +0200162 struct sta_info *sta;
163 struct rhash_head *tmp;
164 const struct bucket_table *tbl;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100165
Johannes Berg60f4b622015-04-23 14:02:30 +0200166 rcu_read_lock();
167 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
168
169 for_each_sta_info(local, tbl, addr, sta, tmp) {
170 if (sta->sdata == sdata) {
171 rcu_read_unlock();
172 /* this is safe as the caller must already hold
173 * another rcu read section or the mutex
174 */
175 return sta;
176 }
177 }
178 rcu_read_unlock();
179 return NULL;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100180}
181
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100182/*
183 * Get sta info either from the specified interface
184 * or from one of its vlans
185 */
186struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
187 const u8 *addr)
188{
189 struct ieee80211_local *local = sdata->local;
190 struct sta_info *sta;
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100191 struct rhash_head *tmp;
192 const struct bucket_table *tbl;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100193
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100194 rcu_read_lock();
195 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
196
197 for_each_sta_info(local, tbl, addr, sta, tmp) {
198 if (sta->sdata == sdata ||
199 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
200 rcu_read_unlock();
201 /* this is safe as the caller must already hold
202 * another rcu read section or the mutex
203 */
204 return sta;
205 }
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100206 }
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100207 rcu_read_unlock();
208 return NULL;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100209}
210
Johannes Berg3b53fde82009-11-16 12:00:37 +0100211struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
212 int idx)
Luis Carlos Coboee385852008-02-23 15:17:11 +0100213{
Johannes Berg3b53fde82009-11-16 12:00:37 +0100214 struct ieee80211_local *local = sdata->local;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100215 struct sta_info *sta;
216 int i = 0;
217
Johannes Bergd0709a62008-02-25 16:27:46 +0100218 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg3b53fde82009-11-16 12:00:37 +0100219 if (sdata != sta->sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800220 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100221 if (i < idx) {
222 ++i;
223 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100224 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800225 return sta;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100226 }
Luis Carlos Coboee385852008-02-23 15:17:11 +0100227
228 return NULL;
229}
Jiri Bencf0706e82007-05-05 11:45:53 -0700230
Johannes Berg93e5deb2008-04-01 15:21:00 +0200231/**
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100232 * sta_info_free - free STA
Johannes Berg93e5deb2008-04-01 15:21:00 +0200233 *
Randy Dunlap6ef307b2008-07-03 13:52:18 -0700234 * @local: pointer to the global information
Johannes Berg93e5deb2008-04-01 15:21:00 +0200235 * @sta: STA info to free
236 *
237 * This function must undo everything done by sta_info_alloc()
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100238 * that may happen before sta_info_insert(). It may only be
239 * called when sta_info_insert() has not been attempted (and
240 * if that fails, the station is freed anyway.)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200241 */
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100242void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200243{
Johannes Berg889cbb92012-01-17 10:33:29 +0100244 if (sta->rate_ctrl)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100245 rate_control_free_sta(sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200246
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200247 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200248
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100249 if (sta->sta.txq[0])
250 kfree(to_txq_info(sta->sta.txq[0]));
Felix Fietkau53d04522014-05-27 22:33:57 +0200251 kfree(rcu_dereference_raw(sta->sta.rates));
Johannes Berg433f5bc2015-06-17 10:31:00 +0200252#ifdef CONFIG_MAC80211_MESH
253 kfree(sta->mesh);
254#endif
Johannes Berg93e5deb2008-04-01 15:21:00 +0200255 kfree(sta);
256}
257
Johannes Berg4d339602011-12-15 11:24:20 +0100258/* Caller must hold local->sta_mtx */
Johannes Bergd0709a62008-02-25 16:27:46 +0100259static void sta_info_hash_add(struct ieee80211_local *local,
260 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700261{
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100262 rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
263 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -0700264}
Jiri Bencf0706e82007-05-05 11:45:53 -0700265
Johannes Berg5ac2e352014-05-27 16:32:27 +0200266static void sta_deliver_ps_frames(struct work_struct *wk)
Johannes Bergaf818582009-11-06 11:35:50 +0100267{
268 struct sta_info *sta;
269
Johannes Berg5ac2e352014-05-27 16:32:27 +0200270 sta = container_of(wk, struct sta_info, drv_deliver_wk);
Johannes Bergaf818582009-11-06 11:35:50 +0100271
272 if (sta->dead)
273 return;
274
Johannes Berg5ac2e352014-05-27 16:32:27 +0200275 local_bh_disable();
276 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
Johannes Bergaf818582009-11-06 11:35:50 +0100277 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200278 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
Johannes Bergaf818582009-11-06 11:35:50 +0100279 ieee80211_sta_ps_deliver_poll_response(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200280 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
Johannes Berg47086fc2011-09-29 16:04:33 +0200281 ieee80211_sta_ps_deliver_uapsd(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200282 local_bh_enable();
Johannes Bergaf818582009-11-06 11:35:50 +0100283}
284
Johannes Bergaf65cd962009-11-17 18:18:36 +0100285static int sta_prepare_rate_control(struct ieee80211_local *local,
286 struct sta_info *sta, gfp_t gfp)
287{
Johannes Berg30686bf2015-06-02 21:39:54 +0200288 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
Johannes Bergaf65cd962009-11-17 18:18:36 +0100289 return 0;
290
Johannes Berg889cbb92012-01-17 10:33:29 +0100291 sta->rate_ctrl = local->rate_ctrl;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100292 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
Johannes Berg35c347a2015-03-05 16:10:08 +0100293 sta, gfp);
Johannes Berg889cbb92012-01-17 10:33:29 +0100294 if (!sta->rate_ctrl_priv)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100295 return -ENOMEM;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100296
297 return 0;
298}
299
Johannes Berg73651ee2008-02-25 16:27:47 +0100300struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
Johannes Berg56544162011-12-14 13:28:46 +0100301 const u8 *addr, gfp_t gfp)
Jiri Bencf0706e82007-05-05 11:45:53 -0700302{
Johannes Bergd0709a62008-02-25 16:27:46 +0100303 struct ieee80211_local *local = sdata->local;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100304 struct ieee80211_hw *hw = &local->hw;
Jiri Bencf0706e82007-05-05 11:45:53 -0700305 struct sta_info *sta;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200306 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700307
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100308 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
Jiri Bencf0706e82007-05-05 11:45:53 -0700309 if (!sta)
Johannes Berg73651ee2008-02-25 16:27:47 +0100310 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700311
Johannes Berg07346f812008-05-03 01:02:02 +0200312 spin_lock_init(&sta->lock);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200313 spin_lock_init(&sta->ps_lock);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200314 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
Johannes Berg67c282c2010-06-10 10:21:43 +0200315 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
Johannes Berga93e3642010-06-10 10:21:46 +0200316 mutex_init(&sta->ampdu_mlme.mtx);
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800317#ifdef CONFIG_MAC80211_MESH
Johannes Berg433f5bc2015-06-17 10:31:00 +0200318 if (ieee80211_vif_is_mesh(&sdata->vif)) {
319 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
320 if (!sta->mesh)
321 goto free;
322 spin_lock_init(&sta->mesh->plink_lock);
323 if (ieee80211_vif_is_mesh(&sdata->vif) &&
324 !sdata->u.mesh.user_mpm)
325 init_timer(&sta->mesh->plink_timer);
326 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
327 }
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800328#endif
Johannes Berg07346f812008-05-03 01:02:02 +0200329
Johannes Bergac100ce2015-06-16 16:22:12 +0200330 memcpy(sta->addr, addr, ETH_ALEN);
Johannes Berg17741cd2008-09-11 00:02:02 +0200331 memcpy(sta->sta.addr, addr, ETH_ALEN);
Johannes Bergd0709a62008-02-25 16:27:46 +0100332 sta->local = local;
333 sta->sdata = sdata;
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200334 sta->rx_stats.last_rx = jiffies;
Jiri Bencf0706e82007-05-05 11:45:53 -0700335
Johannes Berg71ec3752012-01-20 13:55:20 +0100336 sta->sta_state = IEEE80211_STA_NONE;
337
Liad Kaufmanb6da9112014-11-19 13:47:38 +0200338 /* Mark TID as unreserved */
339 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
340
Arnd Bergmann84b00602015-09-30 13:26:36 +0200341 sta->last_connected = ktime_get_seconds();
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200342 ewma_signal_init(&sta->rx_stats.avg_signal);
343 for (i = 0; i < ARRAY_SIZE(sta->rx_stats.chain_signal_avg); i++)
344 ewma_signal_init(&sta->rx_stats.chain_signal_avg[i]);
Bruno Randolf541a45a2010-12-02 19:12:43 +0900345
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100346 if (local->ops->wake_tx_queue) {
347 void *txq_data;
348 int size = sizeof(struct txq_info) +
349 ALIGN(hw->txq_data_size, sizeof(void *));
350
351 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
352 if (!txq_data)
353 goto free;
354
355 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
356 struct txq_info *txq = txq_data + i * size;
357
358 ieee80211_init_tx_queue(sdata, sta, txq, i);
359 }
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100360 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700361
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100362 if (sta_prepare_rate_control(local, sta, gfp))
363 goto free_txq;
364
Johannes Berg5a306f52012-11-14 23:22:21 +0100365 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berga622ab72010-06-10 10:21:39 +0200366 /*
367 * timer_to_tid must be initialized with identity mapping
368 * to enable session_timer's data differentiation. See
369 * sta_rx_agg_session_timer_expired for usage.
370 */
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200371 sta->timer_to_tid[i] = i;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200372 }
Johannes Berg948d8872011-09-29 16:04:29 +0200373 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
374 skb_queue_head_init(&sta->ps_tx_buf[i]);
375 skb_queue_head_init(&sta->tx_filtered[i]);
376 }
Johannes Berg73651ee2008-02-25 16:27:47 +0100377
Johannes Berg5a306f52012-11-14 23:22:21 +0100378 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700379 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
Senthil Balasubramaniancccaec92009-05-14 18:42:08 +0530380
Johannes Bergaf0ed692013-02-12 14:21:00 +0100381 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300382 if (sdata->vif.type == NL80211_IFTYPE_AP ||
383 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
384 struct ieee80211_supported_band *sband =
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100385 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300386 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
387 IEEE80211_HT_CAP_SM_PS_SHIFT;
388 /*
389 * Assume that hostapd advertises our caps in the beacon and
390 * this is the known_smps_mode for a station that just assciated
391 */
392 switch (smps) {
393 case WLAN_HT_SMPS_CONTROL_DISABLED:
394 sta->known_smps_mode = IEEE80211_SMPS_OFF;
395 break;
396 case WLAN_HT_SMPS_CONTROL_STATIC:
397 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
398 break;
399 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
400 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
401 break;
402 default:
403 WARN_ON(1);
404 }
405 }
Johannes Bergaf0ed692013-02-12 14:21:00 +0100406
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200407 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
Johannes Bergef04a292014-01-06 15:56:59 +0100408
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100409 return sta;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100410
411free_txq:
412 if (sta->sta.txq[0])
413 kfree(to_txq_info(sta->sta.txq[0]));
414free:
Johannes Berg433f5bc2015-06-17 10:31:00 +0200415#ifdef CONFIG_MAC80211_MESH
416 kfree(sta->mesh);
417#endif
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100418 kfree(sta);
419 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +0100420}
421
Guy Eilam8c71df72011-08-17 15:18:14 +0300422static int sta_info_insert_check(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100423{
Johannes Berg34e89502010-02-03 13:59:58 +0100424 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg34e89502010-02-03 13:59:58 +0100425
Johannes Berg03e44972008-02-27 09:56:40 +0100426 /*
427 * Can't be a WARN_ON because it can be triggered through a race:
428 * something inserts a STA (on one CPU) without holding the RTNL
429 * and another CPU turns off the net device.
430 */
Guy Eilam8c71df72011-08-17 15:18:14 +0300431 if (unlikely(!ieee80211_sdata_running(sdata)))
432 return -ENETDOWN;
Johannes Berg03e44972008-02-27 09:56:40 +0100433
Joe Perchesb203ca32012-05-08 18:56:52 +0000434 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
Guy Eilam8c71df72011-08-17 15:18:14 +0300435 is_multicast_ether_addr(sta->sta.addr)))
436 return -EINVAL;
437
Johannes Berg31104892015-10-22 17:35:19 +0200438 /* Strictly speaking this isn't necessary as we hold the mutex, but
439 * the rhashtable code can't really deal with that distinction. We
440 * do require the mutex for correctness though.
441 */
442 rcu_read_lock();
443 lockdep_assert_held(&sdata->local->sta_mtx);
444 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
445 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
446 rcu_read_unlock();
447 return -ENOTUNIQ;
448 }
449 rcu_read_unlock();
450
Guy Eilam8c71df72011-08-17 15:18:14 +0300451 return 0;
452}
453
Johannes Bergf09603a2012-01-20 13:55:21 +0100454static int sta_info_insert_drv_state(struct ieee80211_local *local,
455 struct ieee80211_sub_if_data *sdata,
456 struct sta_info *sta)
457{
458 enum ieee80211_sta_state state;
459 int err = 0;
460
461 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
462 err = drv_sta_state(local, sdata, sta, state, state + 1);
463 if (err)
464 break;
465 }
466
467 if (!err) {
Johannes Berga4ec45a2012-01-20 13:55:22 +0100468 /*
469 * Drivers using legacy sta_add/sta_remove callbacks only
470 * get uploaded set to true after sta_add is called.
471 */
472 if (!local->ops->sta_add)
473 sta->uploaded = true;
Johannes Bergf09603a2012-01-20 13:55:21 +0100474 return 0;
475 }
476
477 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200478 sdata_info(sdata,
479 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
480 sta->sta.addr, state + 1, err);
Johannes Bergf09603a2012-01-20 13:55:21 +0100481 err = 0;
482 }
483
484 /* unwind on error */
485 for (; state > IEEE80211_STA_NOTEXIST; state--)
486 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
487
488 return err;
489}
490
Guy Eilam8c71df72011-08-17 15:18:14 +0300491/*
492 * should be called with sta_mtx locked
493 * this function replaces the mutex lock
494 * with a RCU lock
495 */
Johannes Berg4d339602011-12-15 11:24:20 +0100496static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
Guy Eilam8c71df72011-08-17 15:18:14 +0300497{
498 struct ieee80211_local *local = sta->local;
499 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg7852e362012-01-20 13:55:24 +0100500 struct station_info sinfo;
Guy Eilam8c71df72011-08-17 15:18:14 +0300501 int err = 0;
502
503 lockdep_assert_held(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +0100504
Johannes Berg7852e362012-01-20 13:55:24 +0100505 /* check if STA exists already */
506 if (sta_info_get_bss(sdata, sta->sta.addr)) {
507 err = -EEXIST;
508 goto out_err;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100509 }
Johannes Berg32bfd352007-12-19 01:31:26 +0100510
Johannes Berg7852e362012-01-20 13:55:24 +0100511 local->num_sta++;
512 local->sta_generation++;
513 smp_mb();
Johannes Berg4d339602011-12-15 11:24:20 +0100514
Johannes Berg5108ca82014-02-17 20:49:03 +0100515 /* simplify things and don't accept BA sessions yet */
516 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
517
Johannes Berg7852e362012-01-20 13:55:24 +0100518 /* make the station visible */
519 sta_info_hash_add(local, sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100520
Arik Nemtsov2bad77482014-10-22 12:32:16 +0300521 list_add_tail_rcu(&sta->list, &local->sta_list);
Johannes Berg83d5cc02012-01-12 09:31:10 +0100522
Johannes Berg5108ca82014-02-17 20:49:03 +0100523 /* notify driver */
524 err = sta_info_insert_drv_state(local, sdata, sta);
525 if (err)
526 goto out_remove;
527
Johannes Berg7852e362012-01-20 13:55:24 +0100528 set_sta_flag(sta, WLAN_STA_INSERTED);
Johannes Berg5108ca82014-02-17 20:49:03 +0100529 /* accept BA sessions now */
530 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg4d339602011-12-15 11:24:20 +0100531
Eliad Peller21f659b2013-11-11 20:14:01 +0200532 ieee80211_recalc_min_chandef(sdata);
Johannes Berg7852e362012-01-20 13:55:24 +0100533 ieee80211_sta_debugfs_add(sta);
534 rate_control_add_sta_debugfs(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100535
Johannes Berg7852e362012-01-20 13:55:24 +0100536 memset(&sinfo, 0, sizeof(sinfo));
537 sinfo.filled = 0;
538 sinfo.generation = local->sta_generation;
539 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
Johannes Bergd0709a62008-02-25 16:27:46 +0100540
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200541 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700542
Johannes Berg34e89502010-02-03 13:59:58 +0100543 /* move reference to rcu-protected */
544 rcu_read_lock();
545 mutex_unlock(&local->sta_mtx);
Jiri Bence9f207f2007-05-05 11:46:38 -0700546
Johannes Berg73651ee2008-02-25 16:27:47 +0100547 if (ieee80211_vif_is_mesh(&sdata->vif))
548 mesh_accept_plinks_update(sdata);
549
550 return 0;
Johannes Berg5108ca82014-02-17 20:49:03 +0100551 out_remove:
552 sta_info_hash_del(local, sta);
553 list_del_rcu(&sta->list);
554 local->num_sta--;
555 synchronize_net();
556 __cleanup_single_sta(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100557 out_err:
558 mutex_unlock(&local->sta_mtx);
559 rcu_read_lock();
560 return err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300561}
562
563int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
564{
565 struct ieee80211_local *local = sta->local;
Zhao, Gang308f7fc2014-04-21 12:53:00 +0800566 int err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300567
Johannes Berg4d339602011-12-15 11:24:20 +0100568 might_sleep();
569
Johannes Berg31104892015-10-22 17:35:19 +0200570 mutex_lock(&local->sta_mtx);
571
Guy Eilam8c71df72011-08-17 15:18:14 +0300572 err = sta_info_insert_check(sta);
573 if (err) {
Johannes Berg31104892015-10-22 17:35:19 +0200574 mutex_unlock(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700575 rcu_read_lock();
576 goto out_free;
577 }
578
Johannes Berg4d339602011-12-15 11:24:20 +0100579 err = sta_info_insert_finish(sta);
Guy Eilam8c71df72011-08-17 15:18:14 +0300580 if (err)
Johannes Berg004c8722008-02-20 11:21:35 +0100581 goto out_free;
Johannes Bergd0709a62008-02-25 16:27:46 +0100582
Johannes Berg004c8722008-02-20 11:21:35 +0100583 return 0;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200584 out_free:
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100585 sta_info_free(local, sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200586 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -0700587}
588
Johannes Berg34e89502010-02-03 13:59:58 +0100589int sta_info_insert(struct sta_info *sta)
590{
591 int err = sta_info_insert_rcu(sta);
592
593 rcu_read_unlock();
594
595 return err;
596}
597
Marco Porschd012a602012-10-10 12:39:50 -0700598static inline void __bss_tim_set(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100599{
600 /*
601 * This format has been mandated by the IEEE specifications,
602 * so this line may not be changed to use the __set_bit() format.
603 */
Marco Porschd012a602012-10-10 12:39:50 -0700604 tim[id / 8] |= (1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100605}
606
Marco Porschd012a602012-10-10 12:39:50 -0700607static inline void __bss_tim_clear(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100608{
609 /*
610 * This format has been mandated by the IEEE specifications,
611 * so this line may not be changed to use the __clear_bit() format.
612 */
Marco Porschd012a602012-10-10 12:39:50 -0700613 tim[id / 8] &= ~(1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100614}
615
Ilan Peer3d5839b2013-03-05 15:27:20 +0200616static inline bool __bss_tim_get(u8 *tim, u16 id)
617{
618 /*
619 * This format has been mandated by the IEEE specifications,
620 * so this line may not be changed to use the test_bit() format.
621 */
622 return tim[id / 8] & (1 << (id % 8));
623}
624
Johannes Berg948d8872011-09-29 16:04:29 +0200625static unsigned long ieee80211_tids_for_ac(int ac)
Johannes Berg004c8722008-02-20 11:21:35 +0100626{
Johannes Berg948d8872011-09-29 16:04:29 +0200627 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
628 switch (ac) {
629 case IEEE80211_AC_VO:
630 return BIT(6) | BIT(7);
631 case IEEE80211_AC_VI:
632 return BIT(4) | BIT(5);
633 case IEEE80211_AC_BE:
634 return BIT(0) | BIT(3);
635 case IEEE80211_AC_BK:
636 return BIT(1) | BIT(2);
637 default:
638 WARN_ON(1);
639 return 0;
Johannes Bergd0709a62008-02-25 16:27:46 +0100640 }
Johannes Berg004c8722008-02-20 11:21:35 +0100641}
642
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100643static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
Johannes Berg004c8722008-02-20 11:21:35 +0100644{
Johannes Bergc868cb352011-09-29 16:04:27 +0200645 struct ieee80211_local *local = sta->local;
Marco Porschd012a602012-10-10 12:39:50 -0700646 struct ps_data *ps;
Johannes Berg948d8872011-09-29 16:04:29 +0200647 bool indicate_tim = false;
648 u8 ignore_for_tim = sta->sta.uapsd_queues;
649 int ac;
Bob Copelanda69bd8e2015-07-14 08:31:58 -0400650 u16 id = sta->sta.aid;
Johannes Berg004c8722008-02-20 11:21:35 +0100651
Marco Porschd012a602012-10-10 12:39:50 -0700652 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
653 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
654 if (WARN_ON_ONCE(!sta->sdata->bss))
655 return;
656
657 ps = &sta->sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100658#ifdef CONFIG_MAC80211_MESH
659 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
660 ps = &sta->sdata->u.mesh.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100661#endif
Marco Porschd012a602012-10-10 12:39:50 -0700662 } else {
Johannes Bergc868cb352011-09-29 16:04:27 +0200663 return;
Marco Porschd012a602012-10-10 12:39:50 -0700664 }
Johannes Berg3e122be2008-07-09 14:40:34 +0200665
Johannes Bergc868cb352011-09-29 16:04:27 +0200666 /* No need to do anything if the driver does all */
Johannes Berg30686bf2015-06-02 21:39:54 +0200667 if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
Johannes Bergc868cb352011-09-29 16:04:27 +0200668 return;
Johannes Berg004c8722008-02-20 11:21:35 +0100669
Johannes Bergc868cb352011-09-29 16:04:27 +0200670 if (sta->dead)
671 goto done;
Johannes Berg3e122be2008-07-09 14:40:34 +0200672
Johannes Berg948d8872011-09-29 16:04:29 +0200673 /*
674 * If all ACs are delivery-enabled then we should build
675 * the TIM bit for all ACs anyway; if only some are then
676 * we ignore those and build the TIM bit using only the
677 * non-enabled ones.
678 */
679 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
680 ignore_for_tim = 0;
Johannes Berg3e122be2008-07-09 14:40:34 +0200681
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100682 if (ignore_pending)
683 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
684
Johannes Berg948d8872011-09-29 16:04:29 +0200685 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
686 unsigned long tids;
687
688 if (ignore_for_tim & BIT(ac))
689 continue;
690
691 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
692 !skb_queue_empty(&sta->ps_tx_buf[ac]);
693 if (indicate_tim)
694 break;
695
696 tids = ieee80211_tids_for_ac(ac);
697
698 indicate_tim |=
699 sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100700 indicate_tim |=
701 sta->txq_buffered_tids & tids;
Johannes Bergd0709a62008-02-25 16:27:46 +0100702 }
Johannes Berg004c8722008-02-20 11:21:35 +0100703
Johannes Bergc868cb352011-09-29 16:04:27 +0200704 done:
Johannes Berg65f704a2013-02-13 17:39:53 +0100705 spin_lock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100706
Ilan Peer3d5839b2013-03-05 15:27:20 +0200707 if (indicate_tim == __bss_tim_get(ps->tim, id))
708 goto out_unlock;
709
Johannes Berg948d8872011-09-29 16:04:29 +0200710 if (indicate_tim)
Marco Porschd012a602012-10-10 12:39:50 -0700711 __bss_tim_set(ps->tim, id);
Johannes Bergc868cb352011-09-29 16:04:27 +0200712 else
Marco Porschd012a602012-10-10 12:39:50 -0700713 __bss_tim_clear(ps->tim, id);
Johannes Berg3e122be2008-07-09 14:40:34 +0200714
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100715 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
Johannes Bergc868cb352011-09-29 16:04:27 +0200716 local->tim_in_locked_section = true;
Johannes Berg948d8872011-09-29 16:04:29 +0200717 drv_set_tim(local, &sta->sta, indicate_tim);
Johannes Bergc868cb352011-09-29 16:04:27 +0200718 local->tim_in_locked_section = false;
Johannes Berg004c8722008-02-20 11:21:35 +0100719 }
Johannes Berg004c8722008-02-20 11:21:35 +0100720
Ilan Peer3d5839b2013-03-05 15:27:20 +0200721out_unlock:
Johannes Berg65f704a2013-02-13 17:39:53 +0100722 spin_unlock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100723}
724
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100725void sta_info_recalc_tim(struct sta_info *sta)
726{
727 __sta_info_recalc_tim(sta, false);
728}
729
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200730static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700731{
Johannes Berge039fa42008-05-15 12:55:29 +0200732 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700733 int timeout;
734
735 if (!skb)
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200736 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700737
Johannes Berge039fa42008-05-15 12:55:29 +0200738 info = IEEE80211_SKB_CB(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700739
740 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200741 timeout = (sta->listen_interval *
742 sta->sdata->vif.bss_conf.beacon_int *
743 32 / 15625) * HZ;
Jiri Bencf0706e82007-05-05 11:45:53 -0700744 if (timeout < STA_TX_BUFFER_EXPIRE)
745 timeout = STA_TX_BUFFER_EXPIRE;
Johannes Berge039fa42008-05-15 12:55:29 +0200746 return time_after(jiffies, info->control.jiffies + timeout);
Jiri Bencf0706e82007-05-05 11:45:53 -0700747}
748
749
Johannes Berg948d8872011-09-29 16:04:29 +0200750static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
751 struct sta_info *sta, int ac)
Jiri Bencf0706e82007-05-05 11:45:53 -0700752{
753 unsigned long flags;
754 struct sk_buff *skb;
755
Johannes Berg60750392011-09-29 16:04:28 +0200756 /*
757 * First check for frames that should expire on the filtered
758 * queue. Frames here were rejected by the driver and are on
759 * a separate queue to avoid reordering with normal PS-buffered
760 * frames. They also aren't accounted for right now in the
761 * total_ps_buffered counter.
762 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700763 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200764 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
765 skb = skb_peek(&sta->tx_filtered[ac]);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200766 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200767 skb = __skb_dequeue(&sta->tx_filtered[ac]);
Johannes Berg836341a2008-02-20 02:07:21 +0100768 else
Jiri Bencf0706e82007-05-05 11:45:53 -0700769 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200770 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700771
Johannes Berg60750392011-09-29 16:04:28 +0200772 /*
773 * Frames are queued in order, so if this one
774 * hasn't expired yet we can stop testing. If
775 * we actually reached the end of the queue we
776 * also need to stop, of course.
777 */
778 if (!skb)
779 break;
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200780 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg60750392011-09-29 16:04:28 +0200781 }
782
783 /*
784 * Now also check the normal PS-buffered queue, this will
785 * only find something if the filtered queue was emptied
786 * since the filtered frames are all before the normal PS
787 * buffered frames.
788 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700789 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200790 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
791 skb = skb_peek(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700792 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200793 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700794 else
795 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200796 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700797
Johannes Berg60750392011-09-29 16:04:28 +0200798 /*
799 * frames are queued in order, so if this one
800 * hasn't expired yet (or we reached the end of
801 * the queue) we can stop testing
802 */
Johannes Berg836341a2008-02-20 02:07:21 +0100803 if (!skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700804 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100805
Johannes Berg836341a2008-02-20 02:07:21 +0100806 local->total_ps_buffered--;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200807 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
808 sta->sta.addr);
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200809 ieee80211_free_txskb(&local->hw, skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700810 }
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300811
Johannes Berg60750392011-09-29 16:04:28 +0200812 /*
813 * Finally, recalculate the TIM bit for this station -- it might
814 * now be clear because the station was too slow to retrieve its
815 * frames.
816 */
817 sta_info_recalc_tim(sta);
818
819 /*
820 * Return whether there are any frames still buffered, this is
821 * used to check whether the cleanup timer still needs to run,
822 * if there are no frames we don't need to rearm the timer.
823 */
Johannes Berg948d8872011-09-29 16:04:29 +0200824 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
825 skb_queue_empty(&sta->tx_filtered[ac]));
826}
827
828static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
829 struct sta_info *sta)
830{
831 bool have_buffered = false;
832 int ac;
833
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100834 /* This is only necessary for stations on BSS/MBSS interfaces */
835 if (!sta->sdata->bss &&
836 !ieee80211_vif_is_mesh(&sta->sdata->vif))
Johannes Berg948d8872011-09-29 16:04:29 +0200837 return false;
838
839 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
840 have_buffered |=
841 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
842
843 return have_buffered;
Jiri Bencf0706e82007-05-05 11:45:53 -0700844}
845
Johannes Bergd7782072013-12-04 23:12:31 +0100846static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100847{
848 struct ieee80211_local *local;
849 struct ieee80211_sub_if_data *sdata;
Johannes Berg6d10e462013-03-06 23:09:11 +0100850 int ret;
Johannes Berg34e89502010-02-03 13:59:58 +0100851
852 might_sleep();
853
854 if (!sta)
855 return -ENOENT;
856
857 local = sta->local;
858 sdata = sta->sdata;
859
Johannes Berg83d5cc02012-01-12 09:31:10 +0100860 lockdep_assert_held(&local->sta_mtx);
861
Johannes Berg098a6072010-04-06 11:18:47 +0200862 /*
863 * Before removing the station from the driver and
864 * rate control, it might still start new aggregation
865 * sessions -- block that to make sure the tear-down
866 * will be sufficient.
867 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200868 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Bergc82c4a82012-07-18 13:31:31 +0200869 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
Johannes Berg098a6072010-04-06 11:18:47 +0200870
Johannes Berg34e89502010-02-03 13:59:58 +0100871 ret = sta_info_hash_del(local, sta);
Johannes Bergb01711b2013-12-04 20:25:27 +0100872 if (WARN_ON(ret))
Johannes Berg34e89502010-02-03 13:59:58 +0100873 return ret;
874
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200875 /*
876 * for TDLS peers, make sure to return to the base channel before
877 * removal.
878 */
879 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
880 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
881 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
882 }
883
Arik Nemtsov794454c2012-06-03 23:32:32 +0300884 list_del_rcu(&sta->list);
Johannes Berg4d339602011-12-15 11:24:20 +0100885
Johannes Berg6a9d1b92013-12-04 22:39:17 +0100886 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
887
Johannes Berga710c812013-12-04 20:11:06 +0100888 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
889 rcu_access_pointer(sdata->u.vlan.sta) == sta)
890 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
891
Johannes Bergd7782072013-12-04 23:12:31 +0100892 return 0;
893}
894
895static void __sta_info_destroy_part2(struct sta_info *sta)
896{
897 struct ieee80211_local *local = sta->local;
898 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg6f7a8d22014-11-14 20:32:57 +0100899 struct station_info sinfo = {};
Johannes Bergd7782072013-12-04 23:12:31 +0100900 int ret;
901
902 /*
903 * NOTE: This assumes at least synchronize_net() was done
904 * after _part1 and before _part2!
905 */
906
907 might_sleep();
908 lockdep_assert_held(&local->sta_mtx);
909
Johannes Bergc8782072013-12-04 23:05:45 +0100910 /* now keys can no longer be reached */
Johannes Berg6d10e462013-03-06 23:09:11 +0100911 ieee80211_free_sta_keys(local, sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100912
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100913 /* disable TIM bit - last chance to tell driver */
914 __sta_info_recalc_tim(sta, true);
915
Johannes Berg34e89502010-02-03 13:59:58 +0100916 sta->dead = true;
917
Johannes Berg34e89502010-02-03 13:59:58 +0100918 local->num_sta--;
919 local->sta_generation++;
920
Johannes Berg83d5cc02012-01-12 09:31:10 +0100921 while (sta->sta_state > IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100922 ret = sta_info_move_state(sta, sta->sta_state - 1);
923 if (ret) {
Johannes Berg83d5cc02012-01-12 09:31:10 +0100924 WARN_ON_ONCE(1);
925 break;
926 }
927 }
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100928
Johannes Bergf09603a2012-01-20 13:55:21 +0100929 if (sta->uploaded) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100930 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
931 IEEE80211_STA_NOTEXIST);
932 WARN_ON_ONCE(ret != 0);
933 }
Johannes Berg34e89502010-02-03 13:59:58 +0100934
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200935 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
936
Johannes Berg6f7a8d22014-11-14 20:32:57 +0100937 sta_set_sinfo(sta, &sinfo);
938 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
Jouni Malinenec15e682011-03-23 15:29:52 +0200939
Johannes Berg34e89502010-02-03 13:59:58 +0100940 rate_control_remove_sta_debugfs(sta);
941 ieee80211_sta_debugfs_remove(sta);
Eliad Peller21f659b2013-11-11 20:14:01 +0200942 ieee80211_recalc_min_chandef(sdata);
Johannes Berg34e89502010-02-03 13:59:58 +0100943
Johannes Bergd34ba212013-12-04 22:46:11 +0100944 cleanup_single_sta(sta);
Johannes Bergd7782072013-12-04 23:12:31 +0100945}
946
947int __must_check __sta_info_destroy(struct sta_info *sta)
948{
949 int err = __sta_info_destroy_part1(sta);
950
951 if (err)
952 return err;
953
954 synchronize_net();
955
956 __sta_info_destroy_part2(sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100957
958 return 0;
959}
960
961int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
962{
963 struct sta_info *sta;
964 int ret;
965
966 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100967 sta = sta_info_get(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100968 ret = __sta_info_destroy(sta);
969 mutex_unlock(&sdata->local->sta_mtx);
970
971 return ret;
972}
973
974int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
975 const u8 *addr)
976{
977 struct sta_info *sta;
978 int ret;
979
980 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100981 sta = sta_info_get_bss(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100982 ret = __sta_info_destroy(sta);
983 mutex_unlock(&sdata->local->sta_mtx);
984
985 return ret;
986}
Jiri Bencf0706e82007-05-05 11:45:53 -0700987
988static void sta_info_cleanup(unsigned long data)
989{
990 struct ieee80211_local *local = (struct ieee80211_local *) data;
991 struct sta_info *sta;
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300992 bool timer_needed = false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700993
Johannes Bergd0709a62008-02-25 16:27:46 +0100994 rcu_read_lock();
995 list_for_each_entry_rcu(sta, &local->sta_list, list)
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300996 if (sta_info_cleanup_expire_buffered(local, sta))
997 timer_needed = true;
Johannes Bergd0709a62008-02-25 16:27:46 +0100998 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -0700999
Johannes Berg5bb644a2009-05-17 11:40:42 +02001000 if (local->quiescing)
1001 return;
1002
Juuso Oikarinen3393a602010-04-19 10:12:52 +03001003 if (!timer_needed)
1004 return;
1005
Johannes Berg26d59532011-04-01 13:52:48 +02001006 mod_timer(&local->sta_cleanup,
1007 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
Jiri Bencf0706e82007-05-05 11:45:53 -07001008}
1009
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001010u32 sta_addr_hash(const void *key, u32 length, u32 seed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001011{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001012 return jhash(key, ETH_ALEN, seed);
1013}
1014
1015int sta_info_init(struct ieee80211_local *local)
1016{
1017 int err;
1018
1019 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
1020 if (err)
1021 return err;
1022
Johannes Berg4d339602011-12-15 11:24:20 +01001023 spin_lock_init(&local->tim_lock);
Johannes Berg34e89502010-02-03 13:59:58 +01001024 mutex_init(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07001025 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001026
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001027 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1028 (unsigned long)local);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001029 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001030}
1031
1032void sta_info_stop(struct ieee80211_local *local)
1033{
Johannes Berga56f9922012-12-13 23:08:52 +01001034 del_timer_sync(&local->sta_cleanup);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001035 rhashtable_destroy(&local->sta_hash);
Jiri Bencf0706e82007-05-05 11:45:53 -07001036}
1037
Johannes Berg051007d2012-12-13 23:49:02 +01001038
Johannes Berge7162512013-12-04 23:18:37 +01001039int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
Jiri Bencf0706e82007-05-05 11:45:53 -07001040{
Johannes Bergb998e8b2012-12-13 23:07:46 +01001041 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001042 struct sta_info *sta, *tmp;
Johannes Bergd7782072013-12-04 23:12:31 +01001043 LIST_HEAD(free_list);
Johannes Berg44213b52008-02-25 16:27:49 +01001044 int ret = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001045
Johannes Bergd0709a62008-02-25 16:27:46 +01001046 might_sleep();
1047
Johannes Berge7162512013-12-04 23:18:37 +01001048 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1049 WARN_ON(vlans && !sdata->bss);
1050
Johannes Berg34e89502010-02-03 13:59:58 +01001051 mutex_lock(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +01001052 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Johannes Berge7162512013-12-04 23:18:37 +01001053 if (sdata == sta->sdata ||
1054 (vlans && sdata->bss == sta->sdata->bss)) {
Johannes Bergd7782072013-12-04 23:12:31 +01001055 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1056 list_add(&sta->free_list, &free_list);
Johannes Berg34316832012-02-25 21:40:46 +01001057 ret++;
1058 }
Johannes Berg34e89502010-02-03 13:59:58 +01001059 }
Johannes Bergd7782072013-12-04 23:12:31 +01001060
1061 if (!list_empty(&free_list)) {
1062 synchronize_net();
1063 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1064 __sta_info_destroy_part2(sta);
1065 }
Johannes Berg34e89502010-02-03 13:59:58 +01001066 mutex_unlock(&local->sta_mtx);
Johannes Berg44213b52008-02-25 16:27:49 +01001067
Johannes Berg051007d2012-12-13 23:49:02 +01001068 return ret;
1069}
1070
Johannes Berg24723d12008-09-11 00:01:46 +02001071void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1072 unsigned long exp_time)
1073{
1074 struct ieee80211_local *local = sdata->local;
1075 struct sta_info *sta, *tmp;
Johannes Berg24723d12008-09-11 00:01:46 +02001076
Johannes Berg34e89502010-02-03 13:59:58 +01001077 mutex_lock(&local->sta_mtx);
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301078
1079 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Marek Lindnerec2b7742011-12-20 23:16:52 +08001080 if (sdata != sta->sdata)
1081 continue;
1082
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001083 if (time_after(jiffies, sta->rx_stats.last_rx + exp_time)) {
Mohammed Shafi Shajakhaneea57d42012-10-08 21:33:47 +05301084 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1085 sta->sta.addr);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001086
1087 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1088 test_sta_flag(sta, WLAN_STA_PS_STA))
1089 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1090
Johannes Berg34e89502010-02-03 13:59:58 +01001091 WARN_ON(__sta_info_destroy(sta));
Johannes Berg24723d12008-09-11 00:01:46 +02001092 }
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301093 }
1094
Johannes Berg34e89502010-02-03 13:59:58 +01001095 mutex_unlock(&local->sta_mtx);
Johannes Berg24723d12008-09-11 00:01:46 +02001096}
Johannes Berg17741cd2008-09-11 00:02:02 +02001097
Ben Greear686b9cb2010-09-23 09:44:36 -07001098struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001099 const u8 *addr,
1100 const u8 *localaddr)
Johannes Berg17741cd2008-09-11 00:02:02 +02001101{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001102 struct ieee80211_local *local = hw_to_local(hw);
1103 struct sta_info *sta;
1104 struct rhash_head *tmp;
1105 const struct bucket_table *tbl;
1106
1107 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
Johannes Berg17741cd2008-09-11 00:02:02 +02001108
Ben Greear686b9cb2010-09-23 09:44:36 -07001109 /*
1110 * Just return a random station if localaddr is NULL
1111 * ... first in list.
1112 */
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001113 for_each_sta_info(local, tbl, addr, sta, tmp) {
Ben Greear686b9cb2010-09-23 09:44:36 -07001114 if (localaddr &&
Joe Perchesb203ca32012-05-08 18:56:52 +00001115 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
Ben Greear686b9cb2010-09-23 09:44:36 -07001116 continue;
Johannes Bergf7c65592010-04-30 13:48:36 +02001117 if (!sta->uploaded)
1118 return NULL;
Johannes Bergabe60632009-11-25 17:46:18 +01001119 return &sta->sta;
Johannes Bergf7c65592010-04-30 13:48:36 +02001120 }
1121
Johannes Bergabe60632009-11-25 17:46:18 +01001122 return NULL;
Johannes Berg17741cd2008-09-11 00:02:02 +02001123}
Ben Greear686b9cb2010-09-23 09:44:36 -07001124EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
Johannes Berg5ed176e2009-11-04 14:42:28 +01001125
1126struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1127 const u8 *addr)
1128{
Johannes Bergf7c65592010-04-30 13:48:36 +02001129 struct sta_info *sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001130
1131 if (!vif)
1132 return NULL;
1133
Johannes Bergf7c65592010-04-30 13:48:36 +02001134 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1135 if (!sta)
1136 return NULL;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001137
Johannes Bergf7c65592010-04-30 13:48:36 +02001138 if (!sta->uploaded)
1139 return NULL;
1140
1141 return &sta->sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001142}
Johannes Berg17741cd2008-09-11 00:02:02 +02001143EXPORT_SYMBOL(ieee80211_find_sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001144
Johannes Berge3685e02014-02-20 11:19:58 +01001145/* powersave support code */
1146void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
Johannes Berg50a94322010-11-16 11:50:28 -08001147{
Helmut Schaa608383b2012-01-30 15:18:00 +01001148 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berge3685e02014-02-20 11:19:58 +01001149 struct ieee80211_local *local = sdata->local;
1150 struct sk_buff_head pending;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001151 int filtered = 0, buffered = 0, ac, i;
Johannes Berge3685e02014-02-20 11:19:58 +01001152 unsigned long flags;
Marco Porschd012a602012-10-10 12:39:50 -07001153 struct ps_data *ps;
1154
Felix Fietkau3918edb2014-07-25 16:20:23 +02001155 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1156 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1157 u.ap);
1158
1159 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porschd012a602012-10-10 12:39:50 -07001160 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001161 else if (ieee80211_vif_is_mesh(&sdata->vif))
1162 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07001163 else
1164 return;
Johannes Berg50a94322010-11-16 11:50:28 -08001165
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001166 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg47086fc2011-09-29 16:04:33 +02001167
Johannes Berg5a306f52012-11-14 23:22:21 +01001168 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
Johannes Berg948d8872011-09-29 16:04:29 +02001169 sta->driver_buffered_tids = 0;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001170 sta->txq_buffered_tids = 0;
Johannes Berg948d8872011-09-29 16:04:29 +02001171
Johannes Berg30686bf2015-06-02 21:39:54 +02001172 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001173 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001174
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001175 if (sta->sta.txq[0]) {
1176 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1177 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1178
1179 if (!skb_queue_len(&txqi->queue))
1180 continue;
1181
1182 drv_wake_tx_queue(local, txqi);
1183 }
1184 }
1185
Johannes Berg948d8872011-09-29 16:04:29 +02001186 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001187
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001188 /* sync with ieee80211_tx_h_unicast_ps_buf */
1189 spin_lock(&sta->ps_lock);
Johannes Bergaf818582009-11-06 11:35:50 +01001190 /* Send all buffered frames to the station */
Johannes Berg948d8872011-09-29 16:04:29 +02001191 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1192 int count = skb_queue_len(&pending), tmp;
1193
Arik Nemtsov987c2852012-11-05 10:27:52 +02001194 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001195 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001196 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001197 tmp = skb_queue_len(&pending);
1198 filtered += tmp - count;
1199 count = tmp;
1200
Arik Nemtsov987c2852012-11-05 10:27:52 +02001201 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001202 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001203 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001204 tmp = skb_queue_len(&pending);
1205 buffered += tmp - count;
1206 }
1207
Johannes Berge3685e02014-02-20 11:19:58 +01001208 ieee80211_add_pending_skbs(local, &pending);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001209
1210 /* now we're no longer in the deliver code */
1211 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1212
1213 /* The station might have polled and then woken up before we responded,
1214 * so clear these flags now to avoid them sticking around.
1215 */
1216 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1217 clear_sta_flag(sta, WLAN_STA_UAPSD);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001218 spin_unlock(&sta->ps_lock);
Johannes Berg948d8872011-09-29 16:04:29 +02001219
Johannes Berge3685e02014-02-20 11:19:58 +01001220 atomic_dec(&ps->num_sta_ps);
1221
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001222 /* This station just woke up and isn't aware of our SMPS state */
Chun-Yeow Yeoh062f1d62014-04-22 18:19:25 +08001223 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1224 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001225 sdata->smps_mode) &&
1226 sta->known_smps_mode != sdata->bss->req_smps &&
1227 sta_info_tx_streams(sta) != 1) {
1228 ht_dbg(sdata,
1229 "%pM just woke up and MIMO capable - update SMPS\n",
1230 sta->sta.addr);
1231 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1232 sta->sta.addr,
1233 sdata->vif.bss_conf.bssid);
1234 }
1235
Johannes Bergaf818582009-11-06 11:35:50 +01001236 local->total_ps_buffered -= buffered;
1237
Johannes Bergc868cb352011-09-29 16:04:27 +02001238 sta_info_recalc_tim(sta);
1239
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001240 ps_dbg(sdata,
1241 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1242 sta->sta.addr, sta->sta.aid, filtered, buffered);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001243
1244 ieee80211_check_fast_xmit(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001245}
1246
Johannes Bergce662b442011-09-29 16:04:34 +02001247static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1248 struct sta_info *sta, int tid,
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001249 enum ieee80211_frame_release_type reason,
1250 bool call_driver)
Johannes Bergce662b442011-09-29 16:04:34 +02001251{
1252 struct ieee80211_local *local = sdata->local;
1253 struct ieee80211_qos_hdr *nullfunc;
1254 struct sk_buff *skb;
1255 int size = sizeof(*nullfunc);
1256 __le16 fc;
Johannes Berga74a8c82014-07-22 14:50:47 +02001257 bool qos = sta->sta.wme;
Johannes Bergce662b442011-09-29 16:04:34 +02001258 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02001259 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergce662b442011-09-29 16:04:34 +02001260
1261 if (qos) {
1262 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1263 IEEE80211_STYPE_QOS_NULLFUNC |
1264 IEEE80211_FCTL_FROMDS);
1265 } else {
1266 size -= 2;
1267 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1268 IEEE80211_STYPE_NULLFUNC |
1269 IEEE80211_FCTL_FROMDS);
1270 }
1271
1272 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1273 if (!skb)
1274 return;
1275
1276 skb_reserve(skb, local->hw.extra_tx_headroom);
1277
1278 nullfunc = (void *) skb_put(skb, size);
1279 nullfunc->frame_control = fc;
1280 nullfunc->duration_id = 0;
1281 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1282 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1283 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
Johannes Berg864a6042014-03-04 13:46:53 +01001284 nullfunc->seq_ctrl = 0;
Johannes Bergce662b442011-09-29 16:04:34 +02001285
Johannes Berg59b66252011-10-13 13:19:19 +02001286 skb->priority = tid;
1287 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
Johannes Bergce662b442011-09-29 16:04:34 +02001288 if (qos) {
Johannes Bergce662b442011-09-29 16:04:34 +02001289 nullfunc->qos_ctrl = cpu_to_le16(tid);
1290
Johannes Berg40b96402011-09-29 16:04:38 +02001291 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
Johannes Bergce662b442011-09-29 16:04:34 +02001292 nullfunc->qos_ctrl |=
1293 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1294 }
1295
1296 info = IEEE80211_SKB_CB(skb);
1297
1298 /*
1299 * Tell TX path to send this frame even though the
1300 * STA may still remain is PS mode after this frame
Johannes Bergdeeaee12011-09-29 16:04:35 +02001301 * exchange. Also set EOSP to indicate this packet
1302 * ends the poll/service period.
Johannes Bergce662b442011-09-29 16:04:34 +02001303 */
Johannes Berg02f2f1a2012-02-27 12:18:30 +01001304 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
Johannes Bergdeeaee12011-09-29 16:04:35 +02001305 IEEE80211_TX_STATUS_EOSP |
1306 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergce662b442011-09-29 16:04:34 +02001307
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301308 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1309
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001310 if (call_driver)
1311 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1312 reason, false);
Johannes Berg40b96402011-09-29 16:04:38 +02001313
Johannes Berg89afe612013-02-13 15:39:57 +01001314 skb->dev = sdata->dev;
1315
Johannes Berg55de9082012-07-26 17:24:39 +02001316 rcu_read_lock();
1317 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1318 if (WARN_ON(!chanctx_conf)) {
1319 rcu_read_unlock();
1320 kfree_skb(skb);
1321 return;
1322 }
1323
Johannes Berg73c4e192014-11-09 18:50:09 +02001324 info->band = chanctx_conf->def.chan->band;
Johannes Berg7c107702015-03-20 14:18:27 +01001325 ieee80211_xmit(sdata, sta, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001326 rcu_read_unlock();
Johannes Bergce662b442011-09-29 16:04:34 +02001327}
1328
Johannes Berg0a1cb802014-01-09 11:05:31 +01001329static int find_highest_prio_tid(unsigned long tids)
1330{
1331 /* lower 3 TIDs aren't ordered perfectly */
1332 if (tids & 0xF8)
1333 return fls(tids) - 1;
1334 /* TID 0 is BE just like TID 3 */
1335 if (tids & BIT(0))
1336 return 0;
1337 return fls(tids) - 1;
1338}
1339
Johannes Berg47086fc2011-09-29 16:04:33 +02001340static void
1341ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1342 int n_frames, u8 ignored_acs,
1343 enum ieee80211_frame_release_type reason)
Johannes Bergaf818582009-11-06 11:35:50 +01001344{
1345 struct ieee80211_sub_if_data *sdata = sta->sdata;
1346 struct ieee80211_local *local = sdata->local;
Johannes Berg948d8872011-09-29 16:04:29 +02001347 bool more_data = false;
1348 int ac;
Johannes Berg4049e092011-09-29 16:04:32 +02001349 unsigned long driver_release_tids = 0;
Johannes Berg47086fc2011-09-29 16:04:33 +02001350 struct sk_buff_head frames;
1351
Johannes Bergdeeaee12011-09-29 16:04:35 +02001352 /* Service or PS-Poll period starts */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001353 set_sta_flag(sta, WLAN_STA_SP);
Johannes Bergdeeaee12011-09-29 16:04:35 +02001354
Johannes Berg47086fc2011-09-29 16:04:33 +02001355 __skb_queue_head_init(&frames);
Johannes Bergaf818582009-11-06 11:35:50 +01001356
Johannes Bergf9f760b2014-01-08 17:45:07 +01001357 /* Get response frame(s) and more data bit for the last one. */
Johannes Berg948d8872011-09-29 16:04:29 +02001358 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg4049e092011-09-29 16:04:32 +02001359 unsigned long tids;
1360
Johannes Berg47086fc2011-09-29 16:04:33 +02001361 if (ignored_acs & BIT(ac))
Johannes Berg948d8872011-09-29 16:04:29 +02001362 continue;
1363
Johannes Berg4049e092011-09-29 16:04:32 +02001364 tids = ieee80211_tids_for_ac(ac);
1365
Johannes Bergf9f760b2014-01-08 17:45:07 +01001366 /* if we already have frames from software, then we can't also
1367 * release from hardware queues
1368 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001369 if (skb_queue_empty(&frames)) {
Johannes Bergf9f760b2014-01-08 17:45:07 +01001370 driver_release_tids |= sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001371 driver_release_tids |= sta->txq_buffered_tids & tids;
1372 }
Johannes Berg47086fc2011-09-29 16:04:33 +02001373
Johannes Bergf9f760b2014-01-08 17:45:07 +01001374 if (driver_release_tids) {
1375 /* If the driver has data on more than one TID then
Johannes Berg4049e092011-09-29 16:04:32 +02001376 * certainly there's more data if we release just a
Johannes Bergf9f760b2014-01-08 17:45:07 +01001377 * single frame now (from a single TID). This will
1378 * only happen for PS-Poll.
Johannes Berg4049e092011-09-29 16:04:32 +02001379 */
Johannes Berg47086fc2011-09-29 16:04:33 +02001380 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1381 hweight16(driver_release_tids) > 1) {
Johannes Berg4049e092011-09-29 16:04:32 +02001382 more_data = true;
1383 driver_release_tids =
Johannes Berg0a1cb802014-01-09 11:05:31 +01001384 BIT(find_highest_prio_tid(
1385 driver_release_tids));
Johannes Berg4049e092011-09-29 16:04:32 +02001386 break;
Johannes Berg948d8872011-09-29 16:04:29 +02001387 }
Johannes Bergf9f760b2014-01-08 17:45:07 +01001388 } else {
1389 struct sk_buff *skb;
1390
1391 while (n_frames > 0) {
1392 skb = skb_dequeue(&sta->tx_filtered[ac]);
1393 if (!skb) {
1394 skb = skb_dequeue(
1395 &sta->ps_tx_buf[ac]);
1396 if (skb)
1397 local->total_ps_buffered--;
1398 }
1399 if (!skb)
1400 break;
1401 n_frames--;
1402 __skb_queue_tail(&frames, skb);
1403 }
Johannes Berg948d8872011-09-29 16:04:29 +02001404 }
1405
Johannes Bergf9f760b2014-01-08 17:45:07 +01001406 /* If we have more frames buffered on this AC, then set the
1407 * more-data bit and abort the loop since we can't send more
1408 * data from other ACs before the buffered frames from this.
1409 */
Johannes Berg948d8872011-09-29 16:04:29 +02001410 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1411 !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1412 more_data = true;
1413 break;
1414 }
Johannes Bergaf818582009-11-06 11:35:50 +01001415 }
Johannes Bergaf818582009-11-06 11:35:50 +01001416
Johannes Bergf9f760b2014-01-08 17:45:07 +01001417 if (skb_queue_empty(&frames) && !driver_release_tids) {
Johannes Bergce662b442011-09-29 16:04:34 +02001418 int tid;
Johannes Berg4049e092011-09-29 16:04:32 +02001419
Johannes Bergce662b442011-09-29 16:04:34 +02001420 /*
1421 * For PS-Poll, this can only happen due to a race condition
1422 * when we set the TIM bit and the station notices it, but
1423 * before it can poll for the frame we expire it.
1424 *
1425 * For uAPSD, this is said in the standard (11.2.1.5 h):
1426 * At each unscheduled SP for a non-AP STA, the AP shall
1427 * attempt to transmit at least one MSDU or MMPDU, but no
1428 * more than the value specified in the Max SP Length field
1429 * in the QoS Capability element from delivery-enabled ACs,
1430 * that are destined for the non-AP STA.
1431 *
1432 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1433 */
1434
1435 /* This will evaluate to 1, 3, 5 or 7. */
1436 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1437
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001438 ieee80211_send_null_response(sdata, sta, tid, reason, true);
Johannes Bergf9f760b2014-01-08 17:45:07 +01001439 } else if (!driver_release_tids) {
Johannes Berg47086fc2011-09-29 16:04:33 +02001440 struct sk_buff_head pending;
1441 struct sk_buff *skb;
Johannes Berg40b96402011-09-29 16:04:38 +02001442 int num = 0;
1443 u16 tids = 0;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001444 bool need_null = false;
Johannes Bergaf818582009-11-06 11:35:50 +01001445
Johannes Berg47086fc2011-09-29 16:04:33 +02001446 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001447
Johannes Berg47086fc2011-09-29 16:04:33 +02001448 while ((skb = __skb_dequeue(&frames))) {
1449 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1450 struct ieee80211_hdr *hdr = (void *) skb->data;
Johannes Berg40b96402011-09-29 16:04:38 +02001451 u8 *qoshdr = NULL;
1452
1453 num++;
Johannes Bergaf818582009-11-06 11:35:50 +01001454
Johannes Berg47086fc2011-09-29 16:04:33 +02001455 /*
1456 * Tell TX path to send this frame even though the
1457 * STA may still remain is PS mode after this frame
1458 * exchange.
1459 */
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301460 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1461 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
Johannes Bergaf818582009-11-06 11:35:50 +01001462
Johannes Berg47086fc2011-09-29 16:04:33 +02001463 /*
1464 * Use MoreData flag to indicate whether there are
1465 * more buffered frames for this STA
1466 */
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001467 if (more_data || !skb_queue_empty(&frames))
Johannes Berg47086fc2011-09-29 16:04:33 +02001468 hdr->frame_control |=
1469 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001470 else
1471 hdr->frame_control &=
1472 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
Johannes Berg47086fc2011-09-29 16:04:33 +02001473
Johannes Berg40b96402011-09-29 16:04:38 +02001474 if (ieee80211_is_data_qos(hdr->frame_control) ||
1475 ieee80211_is_qos_nullfunc(hdr->frame_control))
1476 qoshdr = ieee80211_get_qos_ctl(hdr);
1477
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001478 tids |= BIT(skb->priority);
1479
1480 __skb_queue_tail(&pending, skb);
1481
1482 /* end service period after last frame or add one */
1483 if (!skb_queue_empty(&frames))
1484 continue;
1485
1486 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1487 /* for PS-Poll, there's only one frame */
1488 info->flags |= IEEE80211_TX_STATUS_EOSP |
1489 IEEE80211_TX_CTL_REQ_TX_STATUS;
1490 break;
1491 }
1492
1493 /* For uAPSD, things are a bit more complicated. If the
1494 * last frame has a QoS header (i.e. is a QoS-data or
1495 * QoS-nulldata frame) then just set the EOSP bit there
1496 * and be done.
1497 * If the frame doesn't have a QoS header (which means
1498 * it should be a bufferable MMPDU) then we can't set
1499 * the EOSP bit in the QoS header; add a QoS-nulldata
1500 * frame to the list to send it after the MMPDU.
1501 *
1502 * Note that this code is only in the mac80211-release
1503 * code path, we assume that the driver will not buffer
1504 * anything but QoS-data frames, or if it does, will
1505 * create the QoS-nulldata frame by itself if needed.
1506 *
1507 * Cf. 802.11-2012 10.2.1.10 (c).
1508 */
1509 if (qoshdr) {
1510 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
Johannes Berg47086fc2011-09-29 16:04:33 +02001511
Marco Porsch52a3f202012-03-16 15:30:26 +01001512 info->flags |= IEEE80211_TX_STATUS_EOSP |
1513 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001514 } else {
1515 /* The standard isn't completely clear on this
1516 * as it says the more-data bit should be set
1517 * if there are more BUs. The QoS-Null frame
1518 * we're about to send isn't buffered yet, we
1519 * only create it below, but let's pretend it
1520 * was buffered just in case some clients only
1521 * expect more-data=0 when eosp=1.
1522 */
1523 hdr->frame_control |=
1524 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1525 need_null = true;
1526 num++;
Marco Porsch52a3f202012-03-16 15:30:26 +01001527 }
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001528 break;
Johannes Berg47086fc2011-09-29 16:04:33 +02001529 }
1530
Johannes Berg40b96402011-09-29 16:04:38 +02001531 drv_allow_buffered_frames(local, sta, tids, num,
1532 reason, more_data);
1533
Johannes Berg47086fc2011-09-29 16:04:33 +02001534 ieee80211_add_pending_skbs(local, &pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001535
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001536 if (need_null)
1537 ieee80211_send_null_response(
1538 sdata, sta, find_highest_prio_tid(tids),
1539 reason, false);
1540
Johannes Bergc868cb352011-09-29 16:04:27 +02001541 sta_info_recalc_tim(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001542 } else {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001543 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1544 int tid;
1545
Johannes Bergaf818582009-11-06 11:35:50 +01001546 /*
Johannes Berg4049e092011-09-29 16:04:32 +02001547 * We need to release a frame that is buffered somewhere in the
1548 * driver ... it'll have to handle that.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001549 * Note that the driver also has to check the number of frames
1550 * on the TIDs we're releasing from - if there are more than
1551 * n_frames it has to set the more-data bit (if we didn't ask
1552 * it to set it anyway due to other buffered frames); if there
1553 * are fewer than n_frames it has to make sure to adjust that
1554 * to allow the service period to end properly.
Johannes Bergaf818582009-11-06 11:35:50 +01001555 */
Johannes Berg4049e092011-09-29 16:04:32 +02001556 drv_release_buffered_frames(local, sta, driver_release_tids,
Johannes Berg47086fc2011-09-29 16:04:33 +02001557 n_frames, reason, more_data);
Johannes Berg4049e092011-09-29 16:04:32 +02001558
1559 /*
1560 * Note that we don't recalculate the TIM bit here as it would
1561 * most likely have no effect at all unless the driver told us
Johannes Bergf9f760b2014-01-08 17:45:07 +01001562 * that the TID(s) became empty before returning here from the
Johannes Berg4049e092011-09-29 16:04:32 +02001563 * release function.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001564 * Either way, however, when the driver tells us that the TID(s)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001565 * became empty or we find that a txq became empty, we'll do the
1566 * TIM recalculation.
Johannes Berg4049e092011-09-29 16:04:32 +02001567 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001568
1569 if (!sta->sta.txq[0])
1570 return;
1571
1572 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1573 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1574
1575 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1576 continue;
1577
1578 sta_info_recalc_tim(sta);
1579 break;
1580 }
Johannes Bergaf818582009-11-06 11:35:50 +01001581 }
1582}
1583
Johannes Bergaf818582009-11-06 11:35:50 +01001584void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1585{
Johannes Berg47086fc2011-09-29 16:04:33 +02001586 u8 ignore_for_response = sta->sta.uapsd_queues;
Johannes Bergaf818582009-11-06 11:35:50 +01001587
Johannes Berg47086fc2011-09-29 16:04:33 +02001588 /*
1589 * If all ACs are delivery-enabled then we should reply
1590 * from any of them, if only some are enabled we reply
1591 * only from the non-enabled ones.
1592 */
1593 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1594 ignore_for_response = 0;
1595
1596 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1597 IEEE80211_FRAME_RELEASE_PSPOLL);
1598}
1599
1600void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1601{
1602 int n_frames = sta->sta.max_sp;
1603 u8 delivery_enabled = sta->sta.uapsd_queues;
1604
1605 /*
1606 * If we ever grow support for TSPEC this might happen if
1607 * the TSPEC update from hostapd comes in between a trigger
1608 * frame setting WLAN_STA_UAPSD in the RX path and this
1609 * actually getting called.
1610 */
1611 if (!delivery_enabled)
1612 return;
1613
Johannes Berg47086fc2011-09-29 16:04:33 +02001614 switch (sta->sta.max_sp) {
1615 case 1:
1616 n_frames = 2;
1617 break;
1618 case 2:
1619 n_frames = 4;
1620 break;
1621 case 3:
1622 n_frames = 6;
1623 break;
1624 case 0:
1625 /* XXX: what is a good value? */
Andrei Otcheretianski13a80982014-11-04 11:33:04 +02001626 n_frames = 128;
Johannes Berg47086fc2011-09-29 16:04:33 +02001627 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001628 }
Johannes Bergaf818582009-11-06 11:35:50 +01001629
Johannes Berg47086fc2011-09-29 16:04:33 +02001630 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1631 IEEE80211_FRAME_RELEASE_UAPSD);
Johannes Bergaf818582009-11-06 11:35:50 +01001632}
1633
1634void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1635 struct ieee80211_sta *pubsta, bool block)
1636{
1637 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1638
Johannes Bergb5878a22010-04-07 16:48:40 +02001639 trace_api_sta_block_awake(sta->local, pubsta, block);
1640
Johannes Berg5ac2e352014-05-27 16:32:27 +02001641 if (block) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001642 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001643 ieee80211_clear_fast_xmit(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001644 return;
1645 }
1646
1647 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1648 return;
1649
1650 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1651 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1652 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1653 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1654 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1655 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1656 /* must be asleep in this case */
1657 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1658 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1659 } else {
1660 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001661 ieee80211_check_fast_xmit(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001662 }
Johannes Bergaf818582009-11-06 11:35:50 +01001663}
1664EXPORT_SYMBOL(ieee80211_sta_block_awake);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001665
Johannes Berge9437892013-02-15 21:38:08 +01001666void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
Johannes Berg37fbd902011-09-29 16:04:39 +02001667{
1668 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1669 struct ieee80211_local *local = sta->local;
Johannes Berg37fbd902011-09-29 16:04:39 +02001670
1671 trace_api_eosp(local, pubsta);
1672
Johannes Berge9437892013-02-15 21:38:08 +01001673 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg37fbd902011-09-29 16:04:39 +02001674}
Johannes Berge9437892013-02-15 21:38:08 +01001675EXPORT_SYMBOL(ieee80211_sta_eosp);
Johannes Berg37fbd902011-09-29 16:04:39 +02001676
Johannes Berg042ec452011-09-29 16:04:26 +02001677void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1678 u8 tid, bool buffered)
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001679{
1680 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1681
Johannes Berg5a306f52012-11-14 23:22:21 +01001682 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
Johannes Berg042ec452011-09-29 16:04:26 +02001683 return;
1684
Johannes Berg1b000782013-12-19 10:47:48 +01001685 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1686
Johannes Berg948d8872011-09-29 16:04:29 +02001687 if (buffered)
1688 set_bit(tid, &sta->driver_buffered_tids);
1689 else
1690 clear_bit(tid, &sta->driver_buffered_tids);
1691
Johannes Bergc868cb352011-09-29 16:04:27 +02001692 sta_info_recalc_tim(sta);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001693}
Johannes Berg042ec452011-09-29 16:04:26 +02001694EXPORT_SYMBOL(ieee80211_sta_set_buffered);
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001695
Johannes Berg83d5cc02012-01-12 09:31:10 +01001696int sta_info_move_state(struct sta_info *sta,
1697 enum ieee80211_sta_state new_state)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001698{
Johannes Berg8bf11d82011-12-15 11:17:37 +01001699 might_sleep();
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001700
1701 if (sta->sta_state == new_state)
1702 return 0;
1703
Johannes Bergf09603a2012-01-20 13:55:21 +01001704 /* check allowed transitions first */
1705
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001706 switch (new_state) {
1707 case IEEE80211_STA_NONE:
Johannes Bergf09603a2012-01-20 13:55:21 +01001708 if (sta->sta_state != IEEE80211_STA_AUTH)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001709 return -EINVAL;
1710 break;
1711 case IEEE80211_STA_AUTH:
Johannes Bergf09603a2012-01-20 13:55:21 +01001712 if (sta->sta_state != IEEE80211_STA_NONE &&
1713 sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001714 return -EINVAL;
1715 break;
1716 case IEEE80211_STA_ASSOC:
Johannes Bergf09603a2012-01-20 13:55:21 +01001717 if (sta->sta_state != IEEE80211_STA_AUTH &&
1718 sta->sta_state != IEEE80211_STA_AUTHORIZED)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001719 return -EINVAL;
1720 break;
1721 case IEEE80211_STA_AUTHORIZED:
Johannes Bergf09603a2012-01-20 13:55:21 +01001722 if (sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001723 return -EINVAL;
1724 break;
1725 default:
1726 WARN(1, "invalid state %d", new_state);
1727 return -EINVAL;
1728 }
1729
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001730 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1731 sta->sta.addr, new_state);
Johannes Bergf09603a2012-01-20 13:55:21 +01001732
1733 /*
1734 * notify the driver before the actual changes so it can
1735 * fail the transition
1736 */
1737 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1738 int err = drv_sta_state(sta->local, sta->sdata, sta,
1739 sta->sta_state, new_state);
1740 if (err)
1741 return err;
1742 }
1743
1744 /* reflect the change in all state variables */
1745
1746 switch (new_state) {
1747 case IEEE80211_STA_NONE:
1748 if (sta->sta_state == IEEE80211_STA_AUTH)
1749 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1750 break;
1751 case IEEE80211_STA_AUTH:
1752 if (sta->sta_state == IEEE80211_STA_NONE)
1753 set_bit(WLAN_STA_AUTH, &sta->_flags);
1754 else if (sta->sta_state == IEEE80211_STA_ASSOC)
1755 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1756 break;
1757 case IEEE80211_STA_ASSOC:
1758 if (sta->sta_state == IEEE80211_STA_AUTH) {
1759 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1760 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001761 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1762 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1763 !sta->sdata->u.vlan.sta))
1764 atomic_dec(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001765 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001766 ieee80211_clear_fast_xmit(sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001767 }
1768 break;
1769 case IEEE80211_STA_AUTHORIZED:
1770 if (sta->sta_state == IEEE80211_STA_ASSOC) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001771 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1772 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1773 !sta->sdata->u.vlan.sta))
1774 atomic_inc(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001775 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001776 ieee80211_check_fast_xmit(sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001777 }
1778 break;
1779 default:
1780 break;
1781 }
1782
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001783 sta->sta_state = new_state;
1784
1785 return 0;
1786}
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001787
1788u8 sta_info_tx_streams(struct sta_info *sta)
1789{
1790 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1791 u8 rx_streams;
1792
1793 if (!sta->sta.ht_cap.ht_supported)
1794 return 1;
1795
1796 if (sta->sta.vht_cap.vht_supported) {
1797 int i;
1798 u16 tx_mcs_map =
1799 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1800
1801 for (i = 7; i >= 0; i--)
1802 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1803 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1804 return i + 1;
1805 }
1806
1807 if (ht_cap->mcs.rx_mask[3])
1808 rx_streams = 4;
1809 else if (ht_cap->mcs.rx_mask[2])
1810 rx_streams = 3;
1811 else if (ht_cap->mcs.rx_mask[1])
1812 rx_streams = 2;
1813 else
1814 rx_streams = 1;
1815
1816 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1817 return rx_streams;
1818
1819 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1820 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1821}
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001822
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001823static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
1824{
1825 rinfo->flags = 0;
1826
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001827 if (sta->rx_stats.last_rate_flag & RX_FLAG_HT) {
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001828 rinfo->flags |= RATE_INFO_FLAGS_MCS;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001829 rinfo->mcs = sta->rx_stats.last_rate_idx;
1830 } else if (sta->rx_stats.last_rate_flag & RX_FLAG_VHT) {
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001831 rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001832 rinfo->nss = sta->rx_stats.last_rate_vht_nss;
1833 rinfo->mcs = sta->rx_stats.last_rate_idx;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001834 } else {
1835 struct ieee80211_supported_band *sband;
1836 int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
1837 u16 brate;
1838
1839 sband = sta->local->hw.wiphy->bands[
1840 ieee80211_get_sdata_band(sta->sdata)];
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001841 brate = sband->bitrates[sta->rx_stats.last_rate_idx].bitrate;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001842 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
1843 }
1844
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001845 if (sta->rx_stats.last_rate_flag & RX_FLAG_SHORT_GI)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001846 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
1847
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001848 if (sta->rx_stats.last_rate_flag & RX_FLAG_5MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001849 rinfo->bw = RATE_INFO_BW_5;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001850 else if (sta->rx_stats.last_rate_flag & RX_FLAG_10MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001851 rinfo->bw = RATE_INFO_BW_10;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001852 else if (sta->rx_stats.last_rate_flag & RX_FLAG_40MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001853 rinfo->bw = RATE_INFO_BW_40;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001854 else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_80MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001855 rinfo->bw = RATE_INFO_BW_80;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001856 else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_160MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001857 rinfo->bw = RATE_INFO_BW_160;
1858 else
1859 rinfo->bw = RATE_INFO_BW_20;
1860}
1861
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001862void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1863{
1864 struct ieee80211_sub_if_data *sdata = sta->sdata;
1865 struct ieee80211_local *local = sdata->local;
John W. Linville9a244402014-07-25 10:22:36 -04001866 struct rate_control_ref *ref = NULL;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001867 u32 thr = 0;
1868 int i, ac;
1869
John W. Linville9a244402014-07-25 10:22:36 -04001870 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1871 ref = local->rate_ctrl;
1872
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001873 sinfo->generation = sdata->local->sta_generation;
1874
Johannes Berg225b8182015-01-21 21:09:02 +01001875 /* do before driver, so beacon filtering drivers have a
1876 * chance to e.g. just add the number of filtered beacons
1877 * (or just modify the value entirely, of course)
1878 */
1879 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1880 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1881
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001882 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1883
Johannes Berg319090b2014-11-17 14:08:11 +01001884 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1885 BIT(NL80211_STA_INFO_STA_FLAGS) |
1886 BIT(NL80211_STA_INFO_BSS_PARAM) |
1887 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
Johannes Berg976bd9e2015-10-16 17:18:11 +02001888 BIT(NL80211_STA_INFO_RX_DROP_MISC);
1889
1890 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1891 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
1892 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
1893 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001894
Arnd Bergmann84b00602015-09-30 13:26:36 +02001895 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001896 sinfo->inactive_time =
1897 jiffies_to_msecs(jiffies - sta->rx_stats.last_rx);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001898
Johannes Berg319090b2014-11-17 14:08:11 +01001899 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1900 BIT(NL80211_STA_INFO_TX_BYTES)))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001901 sinfo->tx_bytes = 0;
1902 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001903 sinfo->tx_bytes += sta->tx_stats.bytes[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01001904 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001905 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001906
Johannes Berg319090b2014-11-17 14:08:11 +01001907 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001908 sinfo->tx_packets = 0;
1909 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001910 sinfo->tx_packets += sta->tx_stats.packets[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01001911 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001912 }
1913
Johannes Berg319090b2014-11-17 14:08:11 +01001914 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1915 BIT(NL80211_STA_INFO_RX_BYTES)))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001916 sinfo->rx_bytes = sta->rx_stats.bytes;
Johannes Berg319090b2014-11-17 14:08:11 +01001917 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001918 }
1919
Johannes Berg319090b2014-11-17 14:08:11 +01001920 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001921 sinfo->rx_packets = sta->rx_stats.packets;
Johannes Berg319090b2014-11-17 14:08:11 +01001922 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001923 }
1924
Johannes Berg319090b2014-11-17 14:08:11 +01001925 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001926 sinfo->tx_retries = sta->status_stats.retry_count;
Johannes Berg319090b2014-11-17 14:08:11 +01001927 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001928 }
1929
Johannes Berg319090b2014-11-17 14:08:11 +01001930 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001931 sinfo->tx_failed = sta->status_stats.retry_failed;
Johannes Berg319090b2014-11-17 14:08:11 +01001932 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001933 }
1934
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001935 sinfo->rx_dropped_misc = sta->rx_stats.dropped;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001936
Johannes Berg225b8182015-01-21 21:09:02 +01001937 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1938 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1939 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1940 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1941 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1942 }
1943
Johannes Berg30686bf2015-06-02 21:39:54 +02001944 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
1945 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
Johannes Berg319090b2014-11-17 14:08:11 +01001946 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001947 sinfo->signal = (s8)sta->rx_stats.last_signal;
Johannes Berg319090b2014-11-17 14:08:11 +01001948 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001949 }
1950
Johannes Berg319090b2014-11-17 14:08:11 +01001951 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
Johannes Berg40d9a382015-07-13 12:26:46 +02001952 sinfo->signal_avg =
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001953 -ewma_signal_read(&sta->rx_stats.avg_signal);
Johannes Berg319090b2014-11-17 14:08:11 +01001954 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001955 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001956 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001957
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001958 if (sta->rx_stats.chains &&
Johannes Berg319090b2014-11-17 14:08:11 +01001959 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1960 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1961 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1962 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001963
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001964 sinfo->chains = sta->rx_stats.chains;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001965 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001966 sinfo->chain_signal[i] =
1967 sta->rx_stats.chain_signal_last[i];
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001968 sinfo->chain_signal_avg[i] =
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001969 -ewma_signal_read(&sta->rx_stats.chain_signal_avg[i]);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001970 }
1971 }
1972
Johannes Berg319090b2014-11-17 14:08:11 +01001973 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001974 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
1975 &sinfo->txrate);
Johannes Berg319090b2014-11-17 14:08:11 +01001976 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001977 }
1978
Johannes Berg319090b2014-11-17 14:08:11 +01001979 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001980 sta_set_rate_info_rx(sta, &sinfo->rxrate);
Johannes Berg319090b2014-11-17 14:08:11 +01001981 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001982 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001983
Johannes Berg79c892b2014-11-21 14:26:31 +01001984 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
1985 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
1986 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
1987
1988 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
1989 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001990 tidstats->rx_msdu = sta->rx_stats.msdu[i];
Johannes Berg79c892b2014-11-21 14:26:31 +01001991 }
1992
1993 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
1994 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001995 tidstats->tx_msdu = sta->tx_stats.msdu[i];
Johannes Berg79c892b2014-11-21 14:26:31 +01001996 }
1997
1998 if (!(tidstats->filled &
1999 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02002000 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Berg79c892b2014-11-21 14:26:31 +01002001 tidstats->filled |=
2002 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002003 tidstats->tx_msdu_retries =
2004 sta->status_stats.msdu_retries[i];
Johannes Berg79c892b2014-11-21 14:26:31 +01002005 }
2006
2007 if (!(tidstats->filled &
2008 BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02002009 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Berg79c892b2014-11-21 14:26:31 +01002010 tidstats->filled |=
2011 BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002012 tidstats->tx_msdu_failed =
2013 sta->status_stats.msdu_failed[i];
Johannes Berg79c892b2014-11-21 14:26:31 +01002014 }
2015 }
2016
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002017 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2018#ifdef CONFIG_MAC80211_MESH
Johannes Berg319090b2014-11-17 14:08:11 +01002019 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2020 BIT(NL80211_STA_INFO_PLID) |
2021 BIT(NL80211_STA_INFO_PLINK_STATE) |
2022 BIT(NL80211_STA_INFO_LOCAL_PM) |
2023 BIT(NL80211_STA_INFO_PEER_PM) |
2024 BIT(NL80211_STA_INFO_NONPEER_PM);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002025
Johannes Berg433f5bc2015-06-17 10:31:00 +02002026 sinfo->llid = sta->mesh->llid;
2027 sinfo->plid = sta->mesh->plid;
2028 sinfo->plink_state = sta->mesh->plink_state;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002029 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
Johannes Berg319090b2014-11-17 14:08:11 +01002030 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
Johannes Berg433f5bc2015-06-17 10:31:00 +02002031 sinfo->t_offset = sta->mesh->t_offset;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002032 }
Johannes Berg433f5bc2015-06-17 10:31:00 +02002033 sinfo->local_pm = sta->mesh->local_pm;
2034 sinfo->peer_pm = sta->mesh->peer_pm;
2035 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002036#endif
2037 }
2038
2039 sinfo->bss_param.flags = 0;
2040 if (sdata->vif.bss_conf.use_cts_prot)
2041 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2042 if (sdata->vif.bss_conf.use_short_preamble)
2043 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2044 if (sdata->vif.bss_conf.use_short_slot)
2045 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
Emmanuel Grumbach785e21a2014-09-03 15:25:04 +03002046 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002047 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2048
2049 sinfo->sta_flags.set = 0;
2050 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2051 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2052 BIT(NL80211_STA_FLAG_WME) |
2053 BIT(NL80211_STA_FLAG_MFP) |
2054 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2055 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2056 BIT(NL80211_STA_FLAG_TDLS_PEER);
2057 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2058 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2059 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2060 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
Johannes Berga74a8c82014-07-22 14:50:47 +02002061 if (sta->sta.wme)
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002062 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2063 if (test_sta_flag(sta, WLAN_STA_MFP))
2064 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2065 if (test_sta_flag(sta, WLAN_STA_AUTH))
2066 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2067 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2068 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2069 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2070 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2071
2072 /* check if the driver has a SW RC implementation */
2073 if (ref && ref->ops->get_expected_throughput)
2074 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2075 else
2076 thr = drv_get_expected_throughput(local, &sta->sta);
2077
2078 if (thr != 0) {
Johannes Berg319090b2014-11-17 14:08:11 +01002079 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002080 sinfo->expected_throughput = thr;
2081 }
2082}