blob: aec15d746aeae9fa1554f8f530f52eac75176a1a [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 */
69 .head_offset = offsetof(struct sta_info, hash_node),
70 .key_offset = offsetof(struct sta_info, sta.addr),
71 .key_len = ETH_ALEN,
72 .hashfn = sta_addr_hash,
Johannes Bergebd82b32015-04-23 17:26:06 +020073 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
Johannes Berg7bedd0c2015-02-13 21:55:15 +010074};
75
Johannes Berg4d339602011-12-15 11:24:20 +010076/* Caller must hold local->sta_mtx */
Michael Wube8755e2007-07-27 15:43:23 +020077static int sta_info_hash_del(struct ieee80211_local *local,
78 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070079{
Johannes Berg7bedd0c2015-02-13 21:55:15 +010080 return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
81 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -070082}
83
Johannes Berg5108ca82014-02-17 20:49:03 +010084static void __cleanup_single_sta(struct sta_info *sta)
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030085{
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030086 int ac, i;
87 struct tid_ampdu_tx *tid_tx;
88 struct ieee80211_sub_if_data *sdata = sta->sdata;
89 struct ieee80211_local *local = sdata->local;
Marco Porschd012a602012-10-10 12:39:50 -070090 struct ps_data *ps;
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030091
Johannes Berge3685e02014-02-20 11:19:58 +010092 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
Johannes Berg5ac2e352014-05-27 16:32:27 +020093 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
94 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
Marco Porschd012a602012-10-10 12:39:50 -070095 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
96 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
97 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +010098 else if (ieee80211_vif_is_mesh(&sdata->vif))
99 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700100 else
101 return;
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300102
103 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Berge3685e02014-02-20 11:19:58 +0100104 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200105 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300106
Marco Porschd012a602012-10-10 12:39:50 -0700107 atomic_dec(&ps->num_sta_ps);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300108 }
109
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100110 if (sta->sta.txq[0]) {
111 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
112 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
113 int n = skb_queue_len(&txqi->queue);
114
115 ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
116 atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
117 }
118 }
119
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300120 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
121 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
Felix Fietkau1f98ab72012-11-10 03:44:14 +0100122 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
123 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300124 }
125
Thomas Pedersen45b50282013-02-06 10:17:21 -0800126 if (ieee80211_vif_is_mesh(&sdata->vif))
127 mesh_sta_cleanup(sta);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300128
Johannes Berg5ac2e352014-05-27 16:32:27 +0200129 cancel_work_sync(&sta->drv_deliver_wk);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300130
131 /*
132 * Destroy aggregation state here. It would be nice to wait for the
133 * driver to finish aggregation stop and then clean up, but for now
134 * drivers have to handle aggregation stop being requested, followed
135 * directly by station destruction.
136 */
Johannes Berg5a306f52012-11-14 23:22:21 +0100137 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berg661eb382013-06-12 22:47:56 +0200138 kfree(sta->ampdu_mlme.tid_start_tx[i]);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300139 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
140 if (!tid_tx)
141 continue;
Felix Fietkau1f98ab72012-11-10 03:44:14 +0100142 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300143 kfree(tid_tx);
144 }
Johannes Berg5108ca82014-02-17 20:49:03 +0100145}
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300146
Johannes Berg5108ca82014-02-17 20:49:03 +0100147static void cleanup_single_sta(struct sta_info *sta)
148{
149 struct ieee80211_sub_if_data *sdata = sta->sdata;
150 struct ieee80211_local *local = sdata->local;
151
152 __cleanup_single_sta(sta);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300153 sta_info_free(local, sta);
154}
155
Johannes Bergd0709a62008-02-25 16:27:46 +0100156/* protected by RCU */
Johannes Bergabe60632009-11-25 17:46:18 +0100157struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
158 const u8 *addr)
Johannes Berg43ba7e92008-02-21 14:09:30 +0100159{
Johannes Bergabe60632009-11-25 17:46:18 +0100160 struct ieee80211_local *local = sdata->local;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100161
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100162 return rhashtable_lookup_fast(&local->sta_hash, addr, sta_rht_params);
Johannes Berg43ba7e92008-02-21 14:09:30 +0100163}
164
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100165/*
166 * Get sta info either from the specified interface
167 * or from one of its vlans
168 */
169struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
170 const u8 *addr)
171{
172 struct ieee80211_local *local = sdata->local;
173 struct sta_info *sta;
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100174 struct rhash_head *tmp;
175 const struct bucket_table *tbl;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100176
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100177 rcu_read_lock();
178 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
179
180 for_each_sta_info(local, tbl, addr, sta, tmp) {
181 if (sta->sdata == sdata ||
182 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
183 rcu_read_unlock();
184 /* this is safe as the caller must already hold
185 * another rcu read section or the mutex
186 */
187 return sta;
188 }
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100189 }
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100190 rcu_read_unlock();
191 return NULL;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100192}
193
Johannes Berg3b53fde82009-11-16 12:00:37 +0100194struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
195 int idx)
Luis Carlos Coboee385852008-02-23 15:17:11 +0100196{
Johannes Berg3b53fde82009-11-16 12:00:37 +0100197 struct ieee80211_local *local = sdata->local;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100198 struct sta_info *sta;
199 int i = 0;
200
Johannes Bergd0709a62008-02-25 16:27:46 +0100201 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg3b53fde82009-11-16 12:00:37 +0100202 if (sdata != sta->sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800203 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100204 if (i < idx) {
205 ++i;
206 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100207 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800208 return sta;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100209 }
Luis Carlos Coboee385852008-02-23 15:17:11 +0100210
211 return NULL;
212}
Jiri Bencf0706e82007-05-05 11:45:53 -0700213
Johannes Berg93e5deb2008-04-01 15:21:00 +0200214/**
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100215 * sta_info_free - free STA
Johannes Berg93e5deb2008-04-01 15:21:00 +0200216 *
Randy Dunlap6ef307b2008-07-03 13:52:18 -0700217 * @local: pointer to the global information
Johannes Berg93e5deb2008-04-01 15:21:00 +0200218 * @sta: STA info to free
219 *
220 * This function must undo everything done by sta_info_alloc()
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100221 * that may happen before sta_info_insert(). It may only be
222 * called when sta_info_insert() has not been attempted (and
223 * if that fails, the station is freed anyway.)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200224 */
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100225void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200226{
Johannes Berg889cbb92012-01-17 10:33:29 +0100227 if (sta->rate_ctrl)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100228 rate_control_free_sta(sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200229
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200230 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200231
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100232 if (sta->sta.txq[0])
233 kfree(to_txq_info(sta->sta.txq[0]));
Felix Fietkau53d04522014-05-27 22:33:57 +0200234 kfree(rcu_dereference_raw(sta->sta.rates));
Johannes Berg93e5deb2008-04-01 15:21:00 +0200235 kfree(sta);
236}
237
Johannes Berg4d339602011-12-15 11:24:20 +0100238/* Caller must hold local->sta_mtx */
Johannes Bergd0709a62008-02-25 16:27:46 +0100239static void sta_info_hash_add(struct ieee80211_local *local,
240 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700241{
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100242 rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
243 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -0700244}
Jiri Bencf0706e82007-05-05 11:45:53 -0700245
Johannes Berg5ac2e352014-05-27 16:32:27 +0200246static void sta_deliver_ps_frames(struct work_struct *wk)
Johannes Bergaf818582009-11-06 11:35:50 +0100247{
248 struct sta_info *sta;
249
Johannes Berg5ac2e352014-05-27 16:32:27 +0200250 sta = container_of(wk, struct sta_info, drv_deliver_wk);
Johannes Bergaf818582009-11-06 11:35:50 +0100251
252 if (sta->dead)
253 return;
254
Johannes Berg5ac2e352014-05-27 16:32:27 +0200255 local_bh_disable();
256 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
Johannes Bergaf818582009-11-06 11:35:50 +0100257 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200258 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
Johannes Bergaf818582009-11-06 11:35:50 +0100259 ieee80211_sta_ps_deliver_poll_response(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200260 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
Johannes Berg47086fc2011-09-29 16:04:33 +0200261 ieee80211_sta_ps_deliver_uapsd(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200262 local_bh_enable();
Johannes Bergaf818582009-11-06 11:35:50 +0100263}
264
Johannes Bergaf65cd962009-11-17 18:18:36 +0100265static int sta_prepare_rate_control(struct ieee80211_local *local,
266 struct sta_info *sta, gfp_t gfp)
267{
268 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
269 return 0;
270
Johannes Berg889cbb92012-01-17 10:33:29 +0100271 sta->rate_ctrl = local->rate_ctrl;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100272 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
Johannes Berg35c347a2015-03-05 16:10:08 +0100273 sta, gfp);
Johannes Berg889cbb92012-01-17 10:33:29 +0100274 if (!sta->rate_ctrl_priv)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100275 return -ENOMEM;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100276
277 return 0;
278}
279
Johannes Berg73651ee2008-02-25 16:27:47 +0100280struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
Johannes Berg56544162011-12-14 13:28:46 +0100281 const u8 *addr, gfp_t gfp)
Jiri Bencf0706e82007-05-05 11:45:53 -0700282{
Johannes Bergd0709a62008-02-25 16:27:46 +0100283 struct ieee80211_local *local = sdata->local;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100284 struct ieee80211_hw *hw = &local->hw;
Jiri Bencf0706e82007-05-05 11:45:53 -0700285 struct sta_info *sta;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +0530286 struct timespec uptime;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200287 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700288
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100289 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
Jiri Bencf0706e82007-05-05 11:45:53 -0700290 if (!sta)
Johannes Berg73651ee2008-02-25 16:27:47 +0100291 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700292
Johannes Berg07346f812008-05-03 01:02:02 +0200293 spin_lock_init(&sta->lock);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200294 spin_lock_init(&sta->ps_lock);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200295 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
Johannes Berg67c282c2010-06-10 10:21:43 +0200296 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
Johannes Berga93e3642010-06-10 10:21:46 +0200297 mutex_init(&sta->ampdu_mlme.mtx);
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800298#ifdef CONFIG_MAC80211_MESH
Bob Copeland48bf6be2015-04-13 17:26:28 -0400299 spin_lock_init(&sta->plink_lock);
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800300 if (ieee80211_vif_is_mesh(&sdata->vif) &&
301 !sdata->u.mesh.user_mpm)
302 init_timer(&sta->plink_timer);
Thomas Pedersen6c7c4cb2013-06-20 23:50:59 -0700303 sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800304#endif
Johannes Berg07346f812008-05-03 01:02:02 +0200305
Johannes Berg17741cd2008-09-11 00:02:02 +0200306 memcpy(sta->sta.addr, addr, ETH_ALEN);
Johannes Bergd0709a62008-02-25 16:27:46 +0100307 sta->local = local;
308 sta->sdata = sdata;
Felix Fietkau8bc8aec2011-03-21 20:01:00 +0100309 sta->last_rx = jiffies;
Jiri Bencf0706e82007-05-05 11:45:53 -0700310
Johannes Berg71ec3752012-01-20 13:55:20 +0100311 sta->sta_state = IEEE80211_STA_NONE;
312
Liad Kaufmanb6da9112014-11-19 13:47:38 +0200313 /* Mark TID as unreserved */
314 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
315
Thomas Gleixner18171522014-06-11 23:59:14 +0000316 ktime_get_ts(&uptime);
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +0530317 sta->last_connected = uptime.tv_sec;
Bruno Randolf541a45a2010-12-02 19:12:43 +0900318 ewma_init(&sta->avg_signal, 1024, 8);
Felix Fietkauef0621e2013-04-22 16:29:31 +0200319 for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
320 ewma_init(&sta->chain_signal_avg[i], 1024, 8);
Bruno Randolf541a45a2010-12-02 19:12:43 +0900321
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100322 if (local->ops->wake_tx_queue) {
323 void *txq_data;
324 int size = sizeof(struct txq_info) +
325 ALIGN(hw->txq_data_size, sizeof(void *));
326
327 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
328 if (!txq_data)
329 goto free;
330
331 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
332 struct txq_info *txq = txq_data + i * size;
333
334 ieee80211_init_tx_queue(sdata, sta, txq, i);
335 }
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100336 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700337
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100338 if (sta_prepare_rate_control(local, sta, gfp))
339 goto free_txq;
340
Johannes Berg5a306f52012-11-14 23:22:21 +0100341 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berga622ab72010-06-10 10:21:39 +0200342 /*
343 * timer_to_tid must be initialized with identity mapping
344 * to enable session_timer's data differentiation. See
345 * sta_rx_agg_session_timer_expired for usage.
346 */
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200347 sta->timer_to_tid[i] = i;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200348 }
Johannes Berg948d8872011-09-29 16:04:29 +0200349 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
350 skb_queue_head_init(&sta->ps_tx_buf[i]);
351 skb_queue_head_init(&sta->tx_filtered[i]);
352 }
Johannes Berg73651ee2008-02-25 16:27:47 +0100353
Johannes Berg5a306f52012-11-14 23:22:21 +0100354 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700355 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
Senthil Balasubramaniancccaec92009-05-14 18:42:08 +0530356
Johannes Bergaf0ed692013-02-12 14:21:00 +0100357 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300358 if (sdata->vif.type == NL80211_IFTYPE_AP ||
359 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
360 struct ieee80211_supported_band *sband =
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100361 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300362 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
363 IEEE80211_HT_CAP_SM_PS_SHIFT;
364 /*
365 * Assume that hostapd advertises our caps in the beacon and
366 * this is the known_smps_mode for a station that just assciated
367 */
368 switch (smps) {
369 case WLAN_HT_SMPS_CONTROL_DISABLED:
370 sta->known_smps_mode = IEEE80211_SMPS_OFF;
371 break;
372 case WLAN_HT_SMPS_CONTROL_STATIC:
373 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
374 break;
375 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
376 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
377 break;
378 default:
379 WARN_ON(1);
380 }
381 }
Johannes Bergaf0ed692013-02-12 14:21:00 +0100382
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200383 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
Johannes Bergef04a292014-01-06 15:56:59 +0100384
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100385 return sta;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100386
387free_txq:
388 if (sta->sta.txq[0])
389 kfree(to_txq_info(sta->sta.txq[0]));
390free:
391 kfree(sta);
392 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +0100393}
394
Guy Eilam8c71df72011-08-17 15:18:14 +0300395static int sta_info_insert_check(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100396{
Johannes Berg34e89502010-02-03 13:59:58 +0100397 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg34e89502010-02-03 13:59:58 +0100398
Johannes Berg03e44972008-02-27 09:56:40 +0100399 /*
400 * Can't be a WARN_ON because it can be triggered through a race:
401 * something inserts a STA (on one CPU) without holding the RTNL
402 * and another CPU turns off the net device.
403 */
Guy Eilam8c71df72011-08-17 15:18:14 +0300404 if (unlikely(!ieee80211_sdata_running(sdata)))
405 return -ENETDOWN;
Johannes Berg03e44972008-02-27 09:56:40 +0100406
Joe Perchesb203ca32012-05-08 18:56:52 +0000407 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
Guy Eilam8c71df72011-08-17 15:18:14 +0300408 is_multicast_ether_addr(sta->sta.addr)))
409 return -EINVAL;
410
411 return 0;
412}
413
Johannes Bergf09603a2012-01-20 13:55:21 +0100414static int sta_info_insert_drv_state(struct ieee80211_local *local,
415 struct ieee80211_sub_if_data *sdata,
416 struct sta_info *sta)
417{
418 enum ieee80211_sta_state state;
419 int err = 0;
420
421 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
422 err = drv_sta_state(local, sdata, sta, state, state + 1);
423 if (err)
424 break;
425 }
426
427 if (!err) {
Johannes Berga4ec45a2012-01-20 13:55:22 +0100428 /*
429 * Drivers using legacy sta_add/sta_remove callbacks only
430 * get uploaded set to true after sta_add is called.
431 */
432 if (!local->ops->sta_add)
433 sta->uploaded = true;
Johannes Bergf09603a2012-01-20 13:55:21 +0100434 return 0;
435 }
436
437 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200438 sdata_info(sdata,
439 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
440 sta->sta.addr, state + 1, err);
Johannes Bergf09603a2012-01-20 13:55:21 +0100441 err = 0;
442 }
443
444 /* unwind on error */
445 for (; state > IEEE80211_STA_NOTEXIST; state--)
446 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
447
448 return err;
449}
450
Guy Eilam8c71df72011-08-17 15:18:14 +0300451/*
452 * should be called with sta_mtx locked
453 * this function replaces the mutex lock
454 * with a RCU lock
455 */
Johannes Berg4d339602011-12-15 11:24:20 +0100456static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
Guy Eilam8c71df72011-08-17 15:18:14 +0300457{
458 struct ieee80211_local *local = sta->local;
459 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg7852e362012-01-20 13:55:24 +0100460 struct station_info sinfo;
Guy Eilam8c71df72011-08-17 15:18:14 +0300461 int err = 0;
462
463 lockdep_assert_held(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +0100464
Johannes Berg7852e362012-01-20 13:55:24 +0100465 /* check if STA exists already */
466 if (sta_info_get_bss(sdata, sta->sta.addr)) {
467 err = -EEXIST;
468 goto out_err;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100469 }
Johannes Berg32bfd352007-12-19 01:31:26 +0100470
Johannes Berg7852e362012-01-20 13:55:24 +0100471 local->num_sta++;
472 local->sta_generation++;
473 smp_mb();
Johannes Berg4d339602011-12-15 11:24:20 +0100474
Johannes Berg5108ca82014-02-17 20:49:03 +0100475 /* simplify things and don't accept BA sessions yet */
476 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
477
Johannes Berg7852e362012-01-20 13:55:24 +0100478 /* make the station visible */
479 sta_info_hash_add(local, sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100480
Arik Nemtsov2bad77482014-10-22 12:32:16 +0300481 list_add_tail_rcu(&sta->list, &local->sta_list);
Johannes Berg83d5cc02012-01-12 09:31:10 +0100482
Johannes Berg5108ca82014-02-17 20:49:03 +0100483 /* notify driver */
484 err = sta_info_insert_drv_state(local, sdata, sta);
485 if (err)
486 goto out_remove;
487
Johannes Berg7852e362012-01-20 13:55:24 +0100488 set_sta_flag(sta, WLAN_STA_INSERTED);
Johannes Berg5108ca82014-02-17 20:49:03 +0100489 /* accept BA sessions now */
490 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg4d339602011-12-15 11:24:20 +0100491
Eliad Peller21f659b2013-11-11 20:14:01 +0200492 ieee80211_recalc_min_chandef(sdata);
Johannes Berg7852e362012-01-20 13:55:24 +0100493 ieee80211_sta_debugfs_add(sta);
494 rate_control_add_sta_debugfs(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100495
Johannes Berg7852e362012-01-20 13:55:24 +0100496 memset(&sinfo, 0, sizeof(sinfo));
497 sinfo.filled = 0;
498 sinfo.generation = local->sta_generation;
499 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
Johannes Bergd0709a62008-02-25 16:27:46 +0100500
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200501 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700502
Johannes Berg34e89502010-02-03 13:59:58 +0100503 /* move reference to rcu-protected */
504 rcu_read_lock();
505 mutex_unlock(&local->sta_mtx);
Jiri Bence9f207f2007-05-05 11:46:38 -0700506
Johannes Berg73651ee2008-02-25 16:27:47 +0100507 if (ieee80211_vif_is_mesh(&sdata->vif))
508 mesh_accept_plinks_update(sdata);
509
510 return 0;
Johannes Berg5108ca82014-02-17 20:49:03 +0100511 out_remove:
512 sta_info_hash_del(local, sta);
513 list_del_rcu(&sta->list);
514 local->num_sta--;
515 synchronize_net();
516 __cleanup_single_sta(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100517 out_err:
518 mutex_unlock(&local->sta_mtx);
519 rcu_read_lock();
520 return err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300521}
522
523int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
524{
525 struct ieee80211_local *local = sta->local;
Zhao, Gang308f7fc2014-04-21 12:53:00 +0800526 int err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300527
Johannes Berg4d339602011-12-15 11:24:20 +0100528 might_sleep();
529
Guy Eilam8c71df72011-08-17 15:18:14 +0300530 err = sta_info_insert_check(sta);
531 if (err) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700532 rcu_read_lock();
533 goto out_free;
534 }
535
Johannes Berg004c8722008-02-20 11:21:35 +0100536 mutex_lock(&local->sta_mtx);
537
Johannes Berg4d339602011-12-15 11:24:20 +0100538 err = sta_info_insert_finish(sta);
Guy Eilam8c71df72011-08-17 15:18:14 +0300539 if (err)
Johannes Berg004c8722008-02-20 11:21:35 +0100540 goto out_free;
Johannes Bergd0709a62008-02-25 16:27:46 +0100541
Johannes Berg004c8722008-02-20 11:21:35 +0100542 return 0;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200543 out_free:
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100544 sta_info_free(local, sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200545 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -0700546}
547
Johannes Berg34e89502010-02-03 13:59:58 +0100548int sta_info_insert(struct sta_info *sta)
549{
550 int err = sta_info_insert_rcu(sta);
551
552 rcu_read_unlock();
553
554 return err;
555}
556
Marco Porschd012a602012-10-10 12:39:50 -0700557static inline void __bss_tim_set(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100558{
559 /*
560 * This format has been mandated by the IEEE specifications,
561 * so this line may not be changed to use the __set_bit() format.
562 */
Marco Porschd012a602012-10-10 12:39:50 -0700563 tim[id / 8] |= (1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100564}
565
Marco Porschd012a602012-10-10 12:39:50 -0700566static inline void __bss_tim_clear(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100567{
568 /*
569 * This format has been mandated by the IEEE specifications,
570 * so this line may not be changed to use the __clear_bit() format.
571 */
Marco Porschd012a602012-10-10 12:39:50 -0700572 tim[id / 8] &= ~(1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100573}
574
Ilan Peer3d5839b2013-03-05 15:27:20 +0200575static inline bool __bss_tim_get(u8 *tim, u16 id)
576{
577 /*
578 * This format has been mandated by the IEEE specifications,
579 * so this line may not be changed to use the test_bit() format.
580 */
581 return tim[id / 8] & (1 << (id % 8));
582}
583
Johannes Berg948d8872011-09-29 16:04:29 +0200584static unsigned long ieee80211_tids_for_ac(int ac)
Johannes Berg004c8722008-02-20 11:21:35 +0100585{
Johannes Berg948d8872011-09-29 16:04:29 +0200586 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
587 switch (ac) {
588 case IEEE80211_AC_VO:
589 return BIT(6) | BIT(7);
590 case IEEE80211_AC_VI:
591 return BIT(4) | BIT(5);
592 case IEEE80211_AC_BE:
593 return BIT(0) | BIT(3);
594 case IEEE80211_AC_BK:
595 return BIT(1) | BIT(2);
596 default:
597 WARN_ON(1);
598 return 0;
Johannes Bergd0709a62008-02-25 16:27:46 +0100599 }
Johannes Berg004c8722008-02-20 11:21:35 +0100600}
601
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100602static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
Johannes Berg004c8722008-02-20 11:21:35 +0100603{
Johannes Bergc868cb352011-09-29 16:04:27 +0200604 struct ieee80211_local *local = sta->local;
Marco Porschd012a602012-10-10 12:39:50 -0700605 struct ps_data *ps;
Johannes Berg948d8872011-09-29 16:04:29 +0200606 bool indicate_tim = false;
607 u8 ignore_for_tim = sta->sta.uapsd_queues;
608 int ac;
Marco Porschd012a602012-10-10 12:39:50 -0700609 u16 id;
Johannes Berg004c8722008-02-20 11:21:35 +0100610
Marco Porschd012a602012-10-10 12:39:50 -0700611 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
612 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
613 if (WARN_ON_ONCE(!sta->sdata->bss))
614 return;
615
616 ps = &sta->sdata->bss->ps;
617 id = sta->sta.aid;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100618#ifdef CONFIG_MAC80211_MESH
619 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
620 ps = &sta->sdata->u.mesh.ps;
Thomas Pedersen204d1302013-11-05 11:17:05 -0800621 /* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800622 id = sta->plid % (IEEE80211_MAX_AID + 1);
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100623#endif
Marco Porschd012a602012-10-10 12:39:50 -0700624 } else {
Johannes Bergc868cb352011-09-29 16:04:27 +0200625 return;
Marco Porschd012a602012-10-10 12:39:50 -0700626 }
Johannes Berg3e122be2008-07-09 14:40:34 +0200627
Johannes Bergc868cb352011-09-29 16:04:27 +0200628 /* No need to do anything if the driver does all */
629 if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
630 return;
Johannes Berg004c8722008-02-20 11:21:35 +0100631
Johannes Bergc868cb352011-09-29 16:04:27 +0200632 if (sta->dead)
633 goto done;
Johannes Berg3e122be2008-07-09 14:40:34 +0200634
Johannes Berg948d8872011-09-29 16:04:29 +0200635 /*
636 * If all ACs are delivery-enabled then we should build
637 * the TIM bit for all ACs anyway; if only some are then
638 * we ignore those and build the TIM bit using only the
639 * non-enabled ones.
640 */
641 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
642 ignore_for_tim = 0;
Johannes Berg3e122be2008-07-09 14:40:34 +0200643
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100644 if (ignore_pending)
645 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
646
Johannes Berg948d8872011-09-29 16:04:29 +0200647 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
648 unsigned long tids;
649
650 if (ignore_for_tim & BIT(ac))
651 continue;
652
653 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
654 !skb_queue_empty(&sta->ps_tx_buf[ac]);
655 if (indicate_tim)
656 break;
657
658 tids = ieee80211_tids_for_ac(ac);
659
660 indicate_tim |=
661 sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100662 indicate_tim |=
663 sta->txq_buffered_tids & tids;
Johannes Bergd0709a62008-02-25 16:27:46 +0100664 }
Johannes Berg004c8722008-02-20 11:21:35 +0100665
Johannes Bergc868cb352011-09-29 16:04:27 +0200666 done:
Johannes Berg65f704a2013-02-13 17:39:53 +0100667 spin_lock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100668
Ilan Peer3d5839b2013-03-05 15:27:20 +0200669 if (indicate_tim == __bss_tim_get(ps->tim, id))
670 goto out_unlock;
671
Johannes Berg948d8872011-09-29 16:04:29 +0200672 if (indicate_tim)
Marco Porschd012a602012-10-10 12:39:50 -0700673 __bss_tim_set(ps->tim, id);
Johannes Bergc868cb352011-09-29 16:04:27 +0200674 else
Marco Porschd012a602012-10-10 12:39:50 -0700675 __bss_tim_clear(ps->tim, id);
Johannes Berg3e122be2008-07-09 14:40:34 +0200676
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100677 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
Johannes Bergc868cb352011-09-29 16:04:27 +0200678 local->tim_in_locked_section = true;
Johannes Berg948d8872011-09-29 16:04:29 +0200679 drv_set_tim(local, &sta->sta, indicate_tim);
Johannes Bergc868cb352011-09-29 16:04:27 +0200680 local->tim_in_locked_section = false;
Johannes Berg004c8722008-02-20 11:21:35 +0100681 }
Johannes Berg004c8722008-02-20 11:21:35 +0100682
Ilan Peer3d5839b2013-03-05 15:27:20 +0200683out_unlock:
Johannes Berg65f704a2013-02-13 17:39:53 +0100684 spin_unlock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100685}
686
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100687void sta_info_recalc_tim(struct sta_info *sta)
688{
689 __sta_info_recalc_tim(sta, false);
690}
691
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200692static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700693{
Johannes Berge039fa42008-05-15 12:55:29 +0200694 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700695 int timeout;
696
697 if (!skb)
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200698 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700699
Johannes Berge039fa42008-05-15 12:55:29 +0200700 info = IEEE80211_SKB_CB(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700701
702 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200703 timeout = (sta->listen_interval *
704 sta->sdata->vif.bss_conf.beacon_int *
705 32 / 15625) * HZ;
Jiri Bencf0706e82007-05-05 11:45:53 -0700706 if (timeout < STA_TX_BUFFER_EXPIRE)
707 timeout = STA_TX_BUFFER_EXPIRE;
Johannes Berge039fa42008-05-15 12:55:29 +0200708 return time_after(jiffies, info->control.jiffies + timeout);
Jiri Bencf0706e82007-05-05 11:45:53 -0700709}
710
711
Johannes Berg948d8872011-09-29 16:04:29 +0200712static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
713 struct sta_info *sta, int ac)
Jiri Bencf0706e82007-05-05 11:45:53 -0700714{
715 unsigned long flags;
716 struct sk_buff *skb;
717
Johannes Berg60750392011-09-29 16:04:28 +0200718 /*
719 * First check for frames that should expire on the filtered
720 * queue. Frames here were rejected by the driver and are on
721 * a separate queue to avoid reordering with normal PS-buffered
722 * frames. They also aren't accounted for right now in the
723 * total_ps_buffered counter.
724 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700725 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200726 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
727 skb = skb_peek(&sta->tx_filtered[ac]);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200728 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200729 skb = __skb_dequeue(&sta->tx_filtered[ac]);
Johannes Berg836341a2008-02-20 02:07:21 +0100730 else
Jiri Bencf0706e82007-05-05 11:45:53 -0700731 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200732 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700733
Johannes Berg60750392011-09-29 16:04:28 +0200734 /*
735 * Frames are queued in order, so if this one
736 * hasn't expired yet we can stop testing. If
737 * we actually reached the end of the queue we
738 * also need to stop, of course.
739 */
740 if (!skb)
741 break;
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200742 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg60750392011-09-29 16:04:28 +0200743 }
744
745 /*
746 * Now also check the normal PS-buffered queue, this will
747 * only find something if the filtered queue was emptied
748 * since the filtered frames are all before the normal PS
749 * buffered frames.
750 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700751 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200752 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
753 skb = skb_peek(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700754 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200755 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700756 else
757 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200758 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700759
Johannes Berg60750392011-09-29 16:04:28 +0200760 /*
761 * frames are queued in order, so if this one
762 * hasn't expired yet (or we reached the end of
763 * the queue) we can stop testing
764 */
Johannes Berg836341a2008-02-20 02:07:21 +0100765 if (!skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700766 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100767
Johannes Berg836341a2008-02-20 02:07:21 +0100768 local->total_ps_buffered--;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200769 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
770 sta->sta.addr);
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200771 ieee80211_free_txskb(&local->hw, skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700772 }
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300773
Johannes Berg60750392011-09-29 16:04:28 +0200774 /*
775 * Finally, recalculate the TIM bit for this station -- it might
776 * now be clear because the station was too slow to retrieve its
777 * frames.
778 */
779 sta_info_recalc_tim(sta);
780
781 /*
782 * Return whether there are any frames still buffered, this is
783 * used to check whether the cleanup timer still needs to run,
784 * if there are no frames we don't need to rearm the timer.
785 */
Johannes Berg948d8872011-09-29 16:04:29 +0200786 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
787 skb_queue_empty(&sta->tx_filtered[ac]));
788}
789
790static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
791 struct sta_info *sta)
792{
793 bool have_buffered = false;
794 int ac;
795
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100796 /* This is only necessary for stations on BSS/MBSS interfaces */
797 if (!sta->sdata->bss &&
798 !ieee80211_vif_is_mesh(&sta->sdata->vif))
Johannes Berg948d8872011-09-29 16:04:29 +0200799 return false;
800
801 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
802 have_buffered |=
803 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
804
805 return have_buffered;
Jiri Bencf0706e82007-05-05 11:45:53 -0700806}
807
Johannes Bergd7782072013-12-04 23:12:31 +0100808static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100809{
810 struct ieee80211_local *local;
811 struct ieee80211_sub_if_data *sdata;
Johannes Berg6d10e462013-03-06 23:09:11 +0100812 int ret;
Johannes Berg34e89502010-02-03 13:59:58 +0100813
814 might_sleep();
815
816 if (!sta)
817 return -ENOENT;
818
819 local = sta->local;
820 sdata = sta->sdata;
821
Johannes Berg83d5cc02012-01-12 09:31:10 +0100822 lockdep_assert_held(&local->sta_mtx);
823
Johannes Berg098a6072010-04-06 11:18:47 +0200824 /*
825 * Before removing the station from the driver and
826 * rate control, it might still start new aggregation
827 * sessions -- block that to make sure the tear-down
828 * will be sufficient.
829 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200830 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Bergc82c4a82012-07-18 13:31:31 +0200831 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
Johannes Berg098a6072010-04-06 11:18:47 +0200832
Johannes Berg34e89502010-02-03 13:59:58 +0100833 ret = sta_info_hash_del(local, sta);
Johannes Bergb01711b2013-12-04 20:25:27 +0100834 if (WARN_ON(ret))
Johannes Berg34e89502010-02-03 13:59:58 +0100835 return ret;
836
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200837 /*
838 * for TDLS peers, make sure to return to the base channel before
839 * removal.
840 */
841 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
842 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
843 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
844 }
845
Arik Nemtsov794454c2012-06-03 23:32:32 +0300846 list_del_rcu(&sta->list);
Johannes Berg4d339602011-12-15 11:24:20 +0100847
Johannes Berg6a9d1b92013-12-04 22:39:17 +0100848 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
849
Johannes Berga710c812013-12-04 20:11:06 +0100850 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
851 rcu_access_pointer(sdata->u.vlan.sta) == sta)
852 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
853
Johannes Bergd7782072013-12-04 23:12:31 +0100854 return 0;
855}
856
857static void __sta_info_destroy_part2(struct sta_info *sta)
858{
859 struct ieee80211_local *local = sta->local;
860 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg6f7a8d22014-11-14 20:32:57 +0100861 struct station_info sinfo = {};
Johannes Bergd7782072013-12-04 23:12:31 +0100862 int ret;
863
864 /*
865 * NOTE: This assumes at least synchronize_net() was done
866 * after _part1 and before _part2!
867 */
868
869 might_sleep();
870 lockdep_assert_held(&local->sta_mtx);
871
Johannes Bergc8782072013-12-04 23:05:45 +0100872 /* now keys can no longer be reached */
Johannes Berg6d10e462013-03-06 23:09:11 +0100873 ieee80211_free_sta_keys(local, sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100874
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100875 /* disable TIM bit - last chance to tell driver */
876 __sta_info_recalc_tim(sta, true);
877
Johannes Berg34e89502010-02-03 13:59:58 +0100878 sta->dead = true;
879
Johannes Berg34e89502010-02-03 13:59:58 +0100880 local->num_sta--;
881 local->sta_generation++;
882
Johannes Berg83d5cc02012-01-12 09:31:10 +0100883 while (sta->sta_state > IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100884 ret = sta_info_move_state(sta, sta->sta_state - 1);
885 if (ret) {
Johannes Berg83d5cc02012-01-12 09:31:10 +0100886 WARN_ON_ONCE(1);
887 break;
888 }
889 }
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100890
Johannes Bergf09603a2012-01-20 13:55:21 +0100891 if (sta->uploaded) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100892 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
893 IEEE80211_STA_NOTEXIST);
894 WARN_ON_ONCE(ret != 0);
895 }
Johannes Berg34e89502010-02-03 13:59:58 +0100896
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200897 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
898
Johannes Berg6f7a8d22014-11-14 20:32:57 +0100899 sta_set_sinfo(sta, &sinfo);
900 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
Jouni Malinenec15e682011-03-23 15:29:52 +0200901
Johannes Berg34e89502010-02-03 13:59:58 +0100902 rate_control_remove_sta_debugfs(sta);
903 ieee80211_sta_debugfs_remove(sta);
Eliad Peller21f659b2013-11-11 20:14:01 +0200904 ieee80211_recalc_min_chandef(sdata);
Johannes Berg34e89502010-02-03 13:59:58 +0100905
Johannes Bergd34ba212013-12-04 22:46:11 +0100906 cleanup_single_sta(sta);
Johannes Bergd7782072013-12-04 23:12:31 +0100907}
908
909int __must_check __sta_info_destroy(struct sta_info *sta)
910{
911 int err = __sta_info_destroy_part1(sta);
912
913 if (err)
914 return err;
915
916 synchronize_net();
917
918 __sta_info_destroy_part2(sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100919
920 return 0;
921}
922
923int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
924{
925 struct sta_info *sta;
926 int ret;
927
928 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100929 sta = sta_info_get(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100930 ret = __sta_info_destroy(sta);
931 mutex_unlock(&sdata->local->sta_mtx);
932
933 return ret;
934}
935
936int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
937 const u8 *addr)
938{
939 struct sta_info *sta;
940 int ret;
941
942 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100943 sta = sta_info_get_bss(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100944 ret = __sta_info_destroy(sta);
945 mutex_unlock(&sdata->local->sta_mtx);
946
947 return ret;
948}
Jiri Bencf0706e82007-05-05 11:45:53 -0700949
950static void sta_info_cleanup(unsigned long data)
951{
952 struct ieee80211_local *local = (struct ieee80211_local *) data;
953 struct sta_info *sta;
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300954 bool timer_needed = false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700955
Johannes Bergd0709a62008-02-25 16:27:46 +0100956 rcu_read_lock();
957 list_for_each_entry_rcu(sta, &local->sta_list, list)
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300958 if (sta_info_cleanup_expire_buffered(local, sta))
959 timer_needed = true;
Johannes Bergd0709a62008-02-25 16:27:46 +0100960 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -0700961
Johannes Berg5bb644a2009-05-17 11:40:42 +0200962 if (local->quiescing)
963 return;
964
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300965 if (!timer_needed)
966 return;
967
Johannes Berg26d59532011-04-01 13:52:48 +0200968 mod_timer(&local->sta_cleanup,
969 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
Jiri Bencf0706e82007-05-05 11:45:53 -0700970}
971
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100972u32 sta_addr_hash(const void *key, u32 length, u32 seed)
Jiri Bencf0706e82007-05-05 11:45:53 -0700973{
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100974 return jhash(key, ETH_ALEN, seed);
975}
976
977int sta_info_init(struct ieee80211_local *local)
978{
979 int err;
980
981 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
982 if (err)
983 return err;
984
Johannes Berg4d339602011-12-15 11:24:20 +0100985 spin_lock_init(&local->tim_lock);
Johannes Berg34e89502010-02-03 13:59:58 +0100986 mutex_init(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700987 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -0700988
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800989 setup_timer(&local->sta_cleanup, sta_info_cleanup,
990 (unsigned long)local);
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100991 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700992}
993
994void sta_info_stop(struct ieee80211_local *local)
995{
Johannes Berga56f9922012-12-13 23:08:52 +0100996 del_timer_sync(&local->sta_cleanup);
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100997 rhashtable_destroy(&local->sta_hash);
Jiri Bencf0706e82007-05-05 11:45:53 -0700998}
999
Johannes Berg051007d2012-12-13 23:49:02 +01001000
Johannes Berge7162512013-12-04 23:18:37 +01001001int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
Jiri Bencf0706e82007-05-05 11:45:53 -07001002{
Johannes Bergb998e8b2012-12-13 23:07:46 +01001003 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001004 struct sta_info *sta, *tmp;
Johannes Bergd7782072013-12-04 23:12:31 +01001005 LIST_HEAD(free_list);
Johannes Berg44213b52008-02-25 16:27:49 +01001006 int ret = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001007
Johannes Bergd0709a62008-02-25 16:27:46 +01001008 might_sleep();
1009
Johannes Berge7162512013-12-04 23:18:37 +01001010 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1011 WARN_ON(vlans && !sdata->bss);
1012
Johannes Berg34e89502010-02-03 13:59:58 +01001013 mutex_lock(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +01001014 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Johannes Berge7162512013-12-04 23:18:37 +01001015 if (sdata == sta->sdata ||
1016 (vlans && sdata->bss == sta->sdata->bss)) {
Johannes Bergd7782072013-12-04 23:12:31 +01001017 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1018 list_add(&sta->free_list, &free_list);
Johannes Berg34316832012-02-25 21:40:46 +01001019 ret++;
1020 }
Johannes Berg34e89502010-02-03 13:59:58 +01001021 }
Johannes Bergd7782072013-12-04 23:12:31 +01001022
1023 if (!list_empty(&free_list)) {
1024 synchronize_net();
1025 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1026 __sta_info_destroy_part2(sta);
1027 }
Johannes Berg34e89502010-02-03 13:59:58 +01001028 mutex_unlock(&local->sta_mtx);
Johannes Berg44213b52008-02-25 16:27:49 +01001029
Johannes Berg051007d2012-12-13 23:49:02 +01001030 return ret;
1031}
1032
Johannes Berg24723d12008-09-11 00:01:46 +02001033void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1034 unsigned long exp_time)
1035{
1036 struct ieee80211_local *local = sdata->local;
1037 struct sta_info *sta, *tmp;
Johannes Berg24723d12008-09-11 00:01:46 +02001038
Johannes Berg34e89502010-02-03 13:59:58 +01001039 mutex_lock(&local->sta_mtx);
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301040
1041 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Marek Lindnerec2b7742011-12-20 23:16:52 +08001042 if (sdata != sta->sdata)
1043 continue;
1044
Johannes Berg24723d12008-09-11 00:01:46 +02001045 if (time_after(jiffies, sta->last_rx + exp_time)) {
Mohammed Shafi Shajakhaneea57d42012-10-08 21:33:47 +05301046 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1047 sta->sta.addr);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001048
1049 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1050 test_sta_flag(sta, WLAN_STA_PS_STA))
1051 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1052
Johannes Berg34e89502010-02-03 13:59:58 +01001053 WARN_ON(__sta_info_destroy(sta));
Johannes Berg24723d12008-09-11 00:01:46 +02001054 }
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301055 }
1056
Johannes Berg34e89502010-02-03 13:59:58 +01001057 mutex_unlock(&local->sta_mtx);
Johannes Berg24723d12008-09-11 00:01:46 +02001058}
Johannes Berg17741cd2008-09-11 00:02:02 +02001059
Ben Greear686b9cb2010-09-23 09:44:36 -07001060struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001061 const u8 *addr,
1062 const u8 *localaddr)
Johannes Berg17741cd2008-09-11 00:02:02 +02001063{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001064 struct ieee80211_local *local = hw_to_local(hw);
1065 struct sta_info *sta;
1066 struct rhash_head *tmp;
1067 const struct bucket_table *tbl;
1068
1069 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
Johannes Berg17741cd2008-09-11 00:02:02 +02001070
Ben Greear686b9cb2010-09-23 09:44:36 -07001071 /*
1072 * Just return a random station if localaddr is NULL
1073 * ... first in list.
1074 */
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001075 for_each_sta_info(local, tbl, addr, sta, tmp) {
Ben Greear686b9cb2010-09-23 09:44:36 -07001076 if (localaddr &&
Joe Perchesb203ca32012-05-08 18:56:52 +00001077 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
Ben Greear686b9cb2010-09-23 09:44:36 -07001078 continue;
Johannes Bergf7c65592010-04-30 13:48:36 +02001079 if (!sta->uploaded)
1080 return NULL;
Johannes Bergabe60632009-11-25 17:46:18 +01001081 return &sta->sta;
Johannes Bergf7c65592010-04-30 13:48:36 +02001082 }
1083
Johannes Bergabe60632009-11-25 17:46:18 +01001084 return NULL;
Johannes Berg17741cd2008-09-11 00:02:02 +02001085}
Ben Greear686b9cb2010-09-23 09:44:36 -07001086EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
Johannes Berg5ed176e2009-11-04 14:42:28 +01001087
1088struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1089 const u8 *addr)
1090{
Johannes Bergf7c65592010-04-30 13:48:36 +02001091 struct sta_info *sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001092
1093 if (!vif)
1094 return NULL;
1095
Johannes Bergf7c65592010-04-30 13:48:36 +02001096 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1097 if (!sta)
1098 return NULL;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001099
Johannes Bergf7c65592010-04-30 13:48:36 +02001100 if (!sta->uploaded)
1101 return NULL;
1102
1103 return &sta->sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001104}
Johannes Berg17741cd2008-09-11 00:02:02 +02001105EXPORT_SYMBOL(ieee80211_find_sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001106
Johannes Berge3685e02014-02-20 11:19:58 +01001107/* powersave support code */
1108void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
Johannes Berg50a94322010-11-16 11:50:28 -08001109{
Helmut Schaa608383b2012-01-30 15:18:00 +01001110 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berge3685e02014-02-20 11:19:58 +01001111 struct ieee80211_local *local = sdata->local;
1112 struct sk_buff_head pending;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001113 int filtered = 0, buffered = 0, ac, i;
Johannes Berge3685e02014-02-20 11:19:58 +01001114 unsigned long flags;
Marco Porschd012a602012-10-10 12:39:50 -07001115 struct ps_data *ps;
1116
Felix Fietkau3918edb2014-07-25 16:20:23 +02001117 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1118 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1119 u.ap);
1120
1121 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porschd012a602012-10-10 12:39:50 -07001122 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001123 else if (ieee80211_vif_is_mesh(&sdata->vif))
1124 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07001125 else
1126 return;
Johannes Berg50a94322010-11-16 11:50:28 -08001127
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001128 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg47086fc2011-09-29 16:04:33 +02001129
Johannes Berg5a306f52012-11-14 23:22:21 +01001130 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
Johannes Berg948d8872011-09-29 16:04:29 +02001131 sta->driver_buffered_tids = 0;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001132 sta->txq_buffered_tids = 0;
Johannes Berg948d8872011-09-29 16:04:29 +02001133
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001134 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1135 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001136
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001137 if (sta->sta.txq[0]) {
1138 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1139 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1140
1141 if (!skb_queue_len(&txqi->queue))
1142 continue;
1143
1144 drv_wake_tx_queue(local, txqi);
1145 }
1146 }
1147
Johannes Berg948d8872011-09-29 16:04:29 +02001148 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001149
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001150 /* sync with ieee80211_tx_h_unicast_ps_buf */
1151 spin_lock(&sta->ps_lock);
Johannes Bergaf818582009-11-06 11:35:50 +01001152 /* Send all buffered frames to the station */
Johannes Berg948d8872011-09-29 16:04:29 +02001153 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1154 int count = skb_queue_len(&pending), tmp;
1155
Arik Nemtsov987c2852012-11-05 10:27:52 +02001156 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001157 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001158 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001159 tmp = skb_queue_len(&pending);
1160 filtered += tmp - count;
1161 count = tmp;
1162
Arik Nemtsov987c2852012-11-05 10:27:52 +02001163 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001164 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001165 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001166 tmp = skb_queue_len(&pending);
1167 buffered += tmp - count;
1168 }
1169
Johannes Berge3685e02014-02-20 11:19:58 +01001170 ieee80211_add_pending_skbs(local, &pending);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001171
1172 /* now we're no longer in the deliver code */
1173 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1174
1175 /* The station might have polled and then woken up before we responded,
1176 * so clear these flags now to avoid them sticking around.
1177 */
1178 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1179 clear_sta_flag(sta, WLAN_STA_UAPSD);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001180 spin_unlock(&sta->ps_lock);
Johannes Berg948d8872011-09-29 16:04:29 +02001181
Johannes Berge3685e02014-02-20 11:19:58 +01001182 atomic_dec(&ps->num_sta_ps);
1183
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001184 /* This station just woke up and isn't aware of our SMPS state */
Chun-Yeow Yeoh062f1d62014-04-22 18:19:25 +08001185 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1186 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001187 sdata->smps_mode) &&
1188 sta->known_smps_mode != sdata->bss->req_smps &&
1189 sta_info_tx_streams(sta) != 1) {
1190 ht_dbg(sdata,
1191 "%pM just woke up and MIMO capable - update SMPS\n",
1192 sta->sta.addr);
1193 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1194 sta->sta.addr,
1195 sdata->vif.bss_conf.bssid);
1196 }
1197
Johannes Bergaf818582009-11-06 11:35:50 +01001198 local->total_ps_buffered -= buffered;
1199
Johannes Bergc868cb352011-09-29 16:04:27 +02001200 sta_info_recalc_tim(sta);
1201
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001202 ps_dbg(sdata,
1203 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1204 sta->sta.addr, sta->sta.aid, filtered, buffered);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001205
1206 ieee80211_check_fast_xmit(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001207}
1208
Johannes Bergce662b442011-09-29 16:04:34 +02001209static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1210 struct sta_info *sta, int tid,
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001211 enum ieee80211_frame_release_type reason,
1212 bool call_driver)
Johannes Bergce662b442011-09-29 16:04:34 +02001213{
1214 struct ieee80211_local *local = sdata->local;
1215 struct ieee80211_qos_hdr *nullfunc;
1216 struct sk_buff *skb;
1217 int size = sizeof(*nullfunc);
1218 __le16 fc;
Johannes Berga74a8c82014-07-22 14:50:47 +02001219 bool qos = sta->sta.wme;
Johannes Bergce662b442011-09-29 16:04:34 +02001220 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02001221 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergce662b442011-09-29 16:04:34 +02001222
1223 if (qos) {
1224 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1225 IEEE80211_STYPE_QOS_NULLFUNC |
1226 IEEE80211_FCTL_FROMDS);
1227 } else {
1228 size -= 2;
1229 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1230 IEEE80211_STYPE_NULLFUNC |
1231 IEEE80211_FCTL_FROMDS);
1232 }
1233
1234 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1235 if (!skb)
1236 return;
1237
1238 skb_reserve(skb, local->hw.extra_tx_headroom);
1239
1240 nullfunc = (void *) skb_put(skb, size);
1241 nullfunc->frame_control = fc;
1242 nullfunc->duration_id = 0;
1243 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1244 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1245 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
Johannes Berg864a6042014-03-04 13:46:53 +01001246 nullfunc->seq_ctrl = 0;
Johannes Bergce662b442011-09-29 16:04:34 +02001247
Johannes Berg59b66252011-10-13 13:19:19 +02001248 skb->priority = tid;
1249 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
Johannes Bergce662b442011-09-29 16:04:34 +02001250 if (qos) {
Johannes Bergce662b442011-09-29 16:04:34 +02001251 nullfunc->qos_ctrl = cpu_to_le16(tid);
1252
Johannes Berg40b96402011-09-29 16:04:38 +02001253 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
Johannes Bergce662b442011-09-29 16:04:34 +02001254 nullfunc->qos_ctrl |=
1255 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1256 }
1257
1258 info = IEEE80211_SKB_CB(skb);
1259
1260 /*
1261 * Tell TX path to send this frame even though the
1262 * STA may still remain is PS mode after this frame
Johannes Bergdeeaee12011-09-29 16:04:35 +02001263 * exchange. Also set EOSP to indicate this packet
1264 * ends the poll/service period.
Johannes Bergce662b442011-09-29 16:04:34 +02001265 */
Johannes Berg02f2f1a2012-02-27 12:18:30 +01001266 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
Johannes Bergdeeaee12011-09-29 16:04:35 +02001267 IEEE80211_TX_STATUS_EOSP |
1268 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergce662b442011-09-29 16:04:34 +02001269
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301270 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1271
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001272 if (call_driver)
1273 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1274 reason, false);
Johannes Berg40b96402011-09-29 16:04:38 +02001275
Johannes Berg89afe612013-02-13 15:39:57 +01001276 skb->dev = sdata->dev;
1277
Johannes Berg55de9082012-07-26 17:24:39 +02001278 rcu_read_lock();
1279 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1280 if (WARN_ON(!chanctx_conf)) {
1281 rcu_read_unlock();
1282 kfree_skb(skb);
1283 return;
1284 }
1285
Johannes Berg73c4e192014-11-09 18:50:09 +02001286 info->band = chanctx_conf->def.chan->band;
Johannes Berg7c107702015-03-20 14:18:27 +01001287 ieee80211_xmit(sdata, sta, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001288 rcu_read_unlock();
Johannes Bergce662b442011-09-29 16:04:34 +02001289}
1290
Johannes Berg0a1cb802014-01-09 11:05:31 +01001291static int find_highest_prio_tid(unsigned long tids)
1292{
1293 /* lower 3 TIDs aren't ordered perfectly */
1294 if (tids & 0xF8)
1295 return fls(tids) - 1;
1296 /* TID 0 is BE just like TID 3 */
1297 if (tids & BIT(0))
1298 return 0;
1299 return fls(tids) - 1;
1300}
1301
Johannes Berg47086fc2011-09-29 16:04:33 +02001302static void
1303ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1304 int n_frames, u8 ignored_acs,
1305 enum ieee80211_frame_release_type reason)
Johannes Bergaf818582009-11-06 11:35:50 +01001306{
1307 struct ieee80211_sub_if_data *sdata = sta->sdata;
1308 struct ieee80211_local *local = sdata->local;
Johannes Berg948d8872011-09-29 16:04:29 +02001309 bool more_data = false;
1310 int ac;
Johannes Berg4049e092011-09-29 16:04:32 +02001311 unsigned long driver_release_tids = 0;
Johannes Berg47086fc2011-09-29 16:04:33 +02001312 struct sk_buff_head frames;
1313
Johannes Bergdeeaee12011-09-29 16:04:35 +02001314 /* Service or PS-Poll period starts */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001315 set_sta_flag(sta, WLAN_STA_SP);
Johannes Bergdeeaee12011-09-29 16:04:35 +02001316
Johannes Berg47086fc2011-09-29 16:04:33 +02001317 __skb_queue_head_init(&frames);
Johannes Bergaf818582009-11-06 11:35:50 +01001318
Johannes Bergf9f760b2014-01-08 17:45:07 +01001319 /* Get response frame(s) and more data bit for the last one. */
Johannes Berg948d8872011-09-29 16:04:29 +02001320 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg4049e092011-09-29 16:04:32 +02001321 unsigned long tids;
1322
Johannes Berg47086fc2011-09-29 16:04:33 +02001323 if (ignored_acs & BIT(ac))
Johannes Berg948d8872011-09-29 16:04:29 +02001324 continue;
1325
Johannes Berg4049e092011-09-29 16:04:32 +02001326 tids = ieee80211_tids_for_ac(ac);
1327
Johannes Bergf9f760b2014-01-08 17:45:07 +01001328 /* if we already have frames from software, then we can't also
1329 * release from hardware queues
1330 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001331 if (skb_queue_empty(&frames)) {
Johannes Bergf9f760b2014-01-08 17:45:07 +01001332 driver_release_tids |= sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001333 driver_release_tids |= sta->txq_buffered_tids & tids;
1334 }
Johannes Berg47086fc2011-09-29 16:04:33 +02001335
Johannes Bergf9f760b2014-01-08 17:45:07 +01001336 if (driver_release_tids) {
1337 /* If the driver has data on more than one TID then
Johannes Berg4049e092011-09-29 16:04:32 +02001338 * certainly there's more data if we release just a
Johannes Bergf9f760b2014-01-08 17:45:07 +01001339 * single frame now (from a single TID). This will
1340 * only happen for PS-Poll.
Johannes Berg4049e092011-09-29 16:04:32 +02001341 */
Johannes Berg47086fc2011-09-29 16:04:33 +02001342 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1343 hweight16(driver_release_tids) > 1) {
Johannes Berg4049e092011-09-29 16:04:32 +02001344 more_data = true;
1345 driver_release_tids =
Johannes Berg0a1cb802014-01-09 11:05:31 +01001346 BIT(find_highest_prio_tid(
1347 driver_release_tids));
Johannes Berg4049e092011-09-29 16:04:32 +02001348 break;
Johannes Berg948d8872011-09-29 16:04:29 +02001349 }
Johannes Bergf9f760b2014-01-08 17:45:07 +01001350 } else {
1351 struct sk_buff *skb;
1352
1353 while (n_frames > 0) {
1354 skb = skb_dequeue(&sta->tx_filtered[ac]);
1355 if (!skb) {
1356 skb = skb_dequeue(
1357 &sta->ps_tx_buf[ac]);
1358 if (skb)
1359 local->total_ps_buffered--;
1360 }
1361 if (!skb)
1362 break;
1363 n_frames--;
1364 __skb_queue_tail(&frames, skb);
1365 }
Johannes Berg948d8872011-09-29 16:04:29 +02001366 }
1367
Johannes Bergf9f760b2014-01-08 17:45:07 +01001368 /* If we have more frames buffered on this AC, then set the
1369 * more-data bit and abort the loop since we can't send more
1370 * data from other ACs before the buffered frames from this.
1371 */
Johannes Berg948d8872011-09-29 16:04:29 +02001372 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1373 !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1374 more_data = true;
1375 break;
1376 }
Johannes Bergaf818582009-11-06 11:35:50 +01001377 }
Johannes Bergaf818582009-11-06 11:35:50 +01001378
Johannes Bergf9f760b2014-01-08 17:45:07 +01001379 if (skb_queue_empty(&frames) && !driver_release_tids) {
Johannes Bergce662b442011-09-29 16:04:34 +02001380 int tid;
Johannes Berg4049e092011-09-29 16:04:32 +02001381
Johannes Bergce662b442011-09-29 16:04:34 +02001382 /*
1383 * For PS-Poll, this can only happen due to a race condition
1384 * when we set the TIM bit and the station notices it, but
1385 * before it can poll for the frame we expire it.
1386 *
1387 * For uAPSD, this is said in the standard (11.2.1.5 h):
1388 * At each unscheduled SP for a non-AP STA, the AP shall
1389 * attempt to transmit at least one MSDU or MMPDU, but no
1390 * more than the value specified in the Max SP Length field
1391 * in the QoS Capability element from delivery-enabled ACs,
1392 * that are destined for the non-AP STA.
1393 *
1394 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1395 */
1396
1397 /* This will evaluate to 1, 3, 5 or 7. */
1398 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1399
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001400 ieee80211_send_null_response(sdata, sta, tid, reason, true);
Johannes Bergf9f760b2014-01-08 17:45:07 +01001401 } else if (!driver_release_tids) {
Johannes Berg47086fc2011-09-29 16:04:33 +02001402 struct sk_buff_head pending;
1403 struct sk_buff *skb;
Johannes Berg40b96402011-09-29 16:04:38 +02001404 int num = 0;
1405 u16 tids = 0;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001406 bool need_null = false;
Johannes Bergaf818582009-11-06 11:35:50 +01001407
Johannes Berg47086fc2011-09-29 16:04:33 +02001408 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001409
Johannes Berg47086fc2011-09-29 16:04:33 +02001410 while ((skb = __skb_dequeue(&frames))) {
1411 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1412 struct ieee80211_hdr *hdr = (void *) skb->data;
Johannes Berg40b96402011-09-29 16:04:38 +02001413 u8 *qoshdr = NULL;
1414
1415 num++;
Johannes Bergaf818582009-11-06 11:35:50 +01001416
Johannes Berg47086fc2011-09-29 16:04:33 +02001417 /*
1418 * Tell TX path to send this frame even though the
1419 * STA may still remain is PS mode after this frame
1420 * exchange.
1421 */
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301422 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1423 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
Johannes Bergaf818582009-11-06 11:35:50 +01001424
Johannes Berg47086fc2011-09-29 16:04:33 +02001425 /*
1426 * Use MoreData flag to indicate whether there are
1427 * more buffered frames for this STA
1428 */
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001429 if (more_data || !skb_queue_empty(&frames))
Johannes Berg47086fc2011-09-29 16:04:33 +02001430 hdr->frame_control |=
1431 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001432 else
1433 hdr->frame_control &=
1434 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
Johannes Berg47086fc2011-09-29 16:04:33 +02001435
Johannes Berg40b96402011-09-29 16:04:38 +02001436 if (ieee80211_is_data_qos(hdr->frame_control) ||
1437 ieee80211_is_qos_nullfunc(hdr->frame_control))
1438 qoshdr = ieee80211_get_qos_ctl(hdr);
1439
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001440 tids |= BIT(skb->priority);
1441
1442 __skb_queue_tail(&pending, skb);
1443
1444 /* end service period after last frame or add one */
1445 if (!skb_queue_empty(&frames))
1446 continue;
1447
1448 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1449 /* for PS-Poll, there's only one frame */
1450 info->flags |= IEEE80211_TX_STATUS_EOSP |
1451 IEEE80211_TX_CTL_REQ_TX_STATUS;
1452 break;
1453 }
1454
1455 /* For uAPSD, things are a bit more complicated. If the
1456 * last frame has a QoS header (i.e. is a QoS-data or
1457 * QoS-nulldata frame) then just set the EOSP bit there
1458 * and be done.
1459 * If the frame doesn't have a QoS header (which means
1460 * it should be a bufferable MMPDU) then we can't set
1461 * the EOSP bit in the QoS header; add a QoS-nulldata
1462 * frame to the list to send it after the MMPDU.
1463 *
1464 * Note that this code is only in the mac80211-release
1465 * code path, we assume that the driver will not buffer
1466 * anything but QoS-data frames, or if it does, will
1467 * create the QoS-nulldata frame by itself if needed.
1468 *
1469 * Cf. 802.11-2012 10.2.1.10 (c).
1470 */
1471 if (qoshdr) {
1472 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
Johannes Berg47086fc2011-09-29 16:04:33 +02001473
Marco Porsch52a3f202012-03-16 15:30:26 +01001474 info->flags |= IEEE80211_TX_STATUS_EOSP |
1475 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001476 } else {
1477 /* The standard isn't completely clear on this
1478 * as it says the more-data bit should be set
1479 * if there are more BUs. The QoS-Null frame
1480 * we're about to send isn't buffered yet, we
1481 * only create it below, but let's pretend it
1482 * was buffered just in case some clients only
1483 * expect more-data=0 when eosp=1.
1484 */
1485 hdr->frame_control |=
1486 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1487 need_null = true;
1488 num++;
Marco Porsch52a3f202012-03-16 15:30:26 +01001489 }
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001490 break;
Johannes Berg47086fc2011-09-29 16:04:33 +02001491 }
1492
Johannes Berg40b96402011-09-29 16:04:38 +02001493 drv_allow_buffered_frames(local, sta, tids, num,
1494 reason, more_data);
1495
Johannes Berg47086fc2011-09-29 16:04:33 +02001496 ieee80211_add_pending_skbs(local, &pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001497
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001498 if (need_null)
1499 ieee80211_send_null_response(
1500 sdata, sta, find_highest_prio_tid(tids),
1501 reason, false);
1502
Johannes Bergc868cb352011-09-29 16:04:27 +02001503 sta_info_recalc_tim(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001504 } else {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001505 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1506 int tid;
1507
Johannes Bergaf818582009-11-06 11:35:50 +01001508 /*
Johannes Berg4049e092011-09-29 16:04:32 +02001509 * We need to release a frame that is buffered somewhere in the
1510 * driver ... it'll have to handle that.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001511 * Note that the driver also has to check the number of frames
1512 * on the TIDs we're releasing from - if there are more than
1513 * n_frames it has to set the more-data bit (if we didn't ask
1514 * it to set it anyway due to other buffered frames); if there
1515 * are fewer than n_frames it has to make sure to adjust that
1516 * to allow the service period to end properly.
Johannes Bergaf818582009-11-06 11:35:50 +01001517 */
Johannes Berg4049e092011-09-29 16:04:32 +02001518 drv_release_buffered_frames(local, sta, driver_release_tids,
Johannes Berg47086fc2011-09-29 16:04:33 +02001519 n_frames, reason, more_data);
Johannes Berg4049e092011-09-29 16:04:32 +02001520
1521 /*
1522 * Note that we don't recalculate the TIM bit here as it would
1523 * most likely have no effect at all unless the driver told us
Johannes Bergf9f760b2014-01-08 17:45:07 +01001524 * that the TID(s) became empty before returning here from the
Johannes Berg4049e092011-09-29 16:04:32 +02001525 * release function.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001526 * Either way, however, when the driver tells us that the TID(s)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001527 * became empty or we find that a txq became empty, we'll do the
1528 * TIM recalculation.
Johannes Berg4049e092011-09-29 16:04:32 +02001529 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001530
1531 if (!sta->sta.txq[0])
1532 return;
1533
1534 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1535 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1536
1537 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1538 continue;
1539
1540 sta_info_recalc_tim(sta);
1541 break;
1542 }
Johannes Bergaf818582009-11-06 11:35:50 +01001543 }
1544}
1545
Johannes Bergaf818582009-11-06 11:35:50 +01001546void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1547{
Johannes Berg47086fc2011-09-29 16:04:33 +02001548 u8 ignore_for_response = sta->sta.uapsd_queues;
Johannes Bergaf818582009-11-06 11:35:50 +01001549
Johannes Berg47086fc2011-09-29 16:04:33 +02001550 /*
1551 * If all ACs are delivery-enabled then we should reply
1552 * from any of them, if only some are enabled we reply
1553 * only from the non-enabled ones.
1554 */
1555 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1556 ignore_for_response = 0;
1557
1558 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1559 IEEE80211_FRAME_RELEASE_PSPOLL);
1560}
1561
1562void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1563{
1564 int n_frames = sta->sta.max_sp;
1565 u8 delivery_enabled = sta->sta.uapsd_queues;
1566
1567 /*
1568 * If we ever grow support for TSPEC this might happen if
1569 * the TSPEC update from hostapd comes in between a trigger
1570 * frame setting WLAN_STA_UAPSD in the RX path and this
1571 * actually getting called.
1572 */
1573 if (!delivery_enabled)
1574 return;
1575
Johannes Berg47086fc2011-09-29 16:04:33 +02001576 switch (sta->sta.max_sp) {
1577 case 1:
1578 n_frames = 2;
1579 break;
1580 case 2:
1581 n_frames = 4;
1582 break;
1583 case 3:
1584 n_frames = 6;
1585 break;
1586 case 0:
1587 /* XXX: what is a good value? */
Andrei Otcheretianski13a80982014-11-04 11:33:04 +02001588 n_frames = 128;
Johannes Berg47086fc2011-09-29 16:04:33 +02001589 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001590 }
Johannes Bergaf818582009-11-06 11:35:50 +01001591
Johannes Berg47086fc2011-09-29 16:04:33 +02001592 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1593 IEEE80211_FRAME_RELEASE_UAPSD);
Johannes Bergaf818582009-11-06 11:35:50 +01001594}
1595
1596void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1597 struct ieee80211_sta *pubsta, bool block)
1598{
1599 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1600
Johannes Bergb5878a22010-04-07 16:48:40 +02001601 trace_api_sta_block_awake(sta->local, pubsta, block);
1602
Johannes Berg5ac2e352014-05-27 16:32:27 +02001603 if (block) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001604 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001605 ieee80211_clear_fast_xmit(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001606 return;
1607 }
1608
1609 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1610 return;
1611
1612 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1613 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1614 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1615 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1616 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1617 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1618 /* must be asleep in this case */
1619 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1620 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1621 } else {
1622 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001623 ieee80211_check_fast_xmit(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001624 }
Johannes Bergaf818582009-11-06 11:35:50 +01001625}
1626EXPORT_SYMBOL(ieee80211_sta_block_awake);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001627
Johannes Berge9437892013-02-15 21:38:08 +01001628void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
Johannes Berg37fbd902011-09-29 16:04:39 +02001629{
1630 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1631 struct ieee80211_local *local = sta->local;
Johannes Berg37fbd902011-09-29 16:04:39 +02001632
1633 trace_api_eosp(local, pubsta);
1634
Johannes Berge9437892013-02-15 21:38:08 +01001635 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg37fbd902011-09-29 16:04:39 +02001636}
Johannes Berge9437892013-02-15 21:38:08 +01001637EXPORT_SYMBOL(ieee80211_sta_eosp);
Johannes Berg37fbd902011-09-29 16:04:39 +02001638
Johannes Berg042ec452011-09-29 16:04:26 +02001639void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1640 u8 tid, bool buffered)
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001641{
1642 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1643
Johannes Berg5a306f52012-11-14 23:22:21 +01001644 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
Johannes Berg042ec452011-09-29 16:04:26 +02001645 return;
1646
Johannes Berg1b000782013-12-19 10:47:48 +01001647 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1648
Johannes Berg948d8872011-09-29 16:04:29 +02001649 if (buffered)
1650 set_bit(tid, &sta->driver_buffered_tids);
1651 else
1652 clear_bit(tid, &sta->driver_buffered_tids);
1653
Johannes Bergc868cb352011-09-29 16:04:27 +02001654 sta_info_recalc_tim(sta);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001655}
Johannes Berg042ec452011-09-29 16:04:26 +02001656EXPORT_SYMBOL(ieee80211_sta_set_buffered);
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001657
Johannes Berg83d5cc02012-01-12 09:31:10 +01001658int sta_info_move_state(struct sta_info *sta,
1659 enum ieee80211_sta_state new_state)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001660{
Johannes Berg8bf11d82011-12-15 11:17:37 +01001661 might_sleep();
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001662
1663 if (sta->sta_state == new_state)
1664 return 0;
1665
Johannes Bergf09603a2012-01-20 13:55:21 +01001666 /* check allowed transitions first */
1667
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001668 switch (new_state) {
1669 case IEEE80211_STA_NONE:
Johannes Bergf09603a2012-01-20 13:55:21 +01001670 if (sta->sta_state != IEEE80211_STA_AUTH)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001671 return -EINVAL;
1672 break;
1673 case IEEE80211_STA_AUTH:
Johannes Bergf09603a2012-01-20 13:55:21 +01001674 if (sta->sta_state != IEEE80211_STA_NONE &&
1675 sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001676 return -EINVAL;
1677 break;
1678 case IEEE80211_STA_ASSOC:
Johannes Bergf09603a2012-01-20 13:55:21 +01001679 if (sta->sta_state != IEEE80211_STA_AUTH &&
1680 sta->sta_state != IEEE80211_STA_AUTHORIZED)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001681 return -EINVAL;
1682 break;
1683 case IEEE80211_STA_AUTHORIZED:
Johannes Bergf09603a2012-01-20 13:55:21 +01001684 if (sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001685 return -EINVAL;
1686 break;
1687 default:
1688 WARN(1, "invalid state %d", new_state);
1689 return -EINVAL;
1690 }
1691
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001692 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1693 sta->sta.addr, new_state);
Johannes Bergf09603a2012-01-20 13:55:21 +01001694
1695 /*
1696 * notify the driver before the actual changes so it can
1697 * fail the transition
1698 */
1699 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1700 int err = drv_sta_state(sta->local, sta->sdata, sta,
1701 sta->sta_state, new_state);
1702 if (err)
1703 return err;
1704 }
1705
1706 /* reflect the change in all state variables */
1707
1708 switch (new_state) {
1709 case IEEE80211_STA_NONE:
1710 if (sta->sta_state == IEEE80211_STA_AUTH)
1711 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1712 break;
1713 case IEEE80211_STA_AUTH:
1714 if (sta->sta_state == IEEE80211_STA_NONE)
1715 set_bit(WLAN_STA_AUTH, &sta->_flags);
1716 else if (sta->sta_state == IEEE80211_STA_ASSOC)
1717 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1718 break;
1719 case IEEE80211_STA_ASSOC:
1720 if (sta->sta_state == IEEE80211_STA_AUTH) {
1721 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1722 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001723 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1724 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1725 !sta->sdata->u.vlan.sta))
1726 atomic_dec(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001727 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001728 ieee80211_clear_fast_xmit(sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001729 }
1730 break;
1731 case IEEE80211_STA_AUTHORIZED:
1732 if (sta->sta_state == IEEE80211_STA_ASSOC) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001733 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1734 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1735 !sta->sdata->u.vlan.sta))
1736 atomic_inc(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001737 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001738 ieee80211_check_fast_xmit(sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001739 }
1740 break;
1741 default:
1742 break;
1743 }
1744
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001745 sta->sta_state = new_state;
1746
1747 return 0;
1748}
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001749
1750u8 sta_info_tx_streams(struct sta_info *sta)
1751{
1752 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1753 u8 rx_streams;
1754
1755 if (!sta->sta.ht_cap.ht_supported)
1756 return 1;
1757
1758 if (sta->sta.vht_cap.vht_supported) {
1759 int i;
1760 u16 tx_mcs_map =
1761 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1762
1763 for (i = 7; i >= 0; i--)
1764 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1765 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1766 return i + 1;
1767 }
1768
1769 if (ht_cap->mcs.rx_mask[3])
1770 rx_streams = 4;
1771 else if (ht_cap->mcs.rx_mask[2])
1772 rx_streams = 3;
1773 else if (ht_cap->mcs.rx_mask[1])
1774 rx_streams = 2;
1775 else
1776 rx_streams = 1;
1777
1778 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1779 return rx_streams;
1780
1781 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1782 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1783}
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001784
1785void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1786{
1787 struct ieee80211_sub_if_data *sdata = sta->sdata;
1788 struct ieee80211_local *local = sdata->local;
John W. Linville9a244402014-07-25 10:22:36 -04001789 struct rate_control_ref *ref = NULL;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001790 struct timespec uptime;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001791 u32 thr = 0;
1792 int i, ac;
1793
John W. Linville9a244402014-07-25 10:22:36 -04001794 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1795 ref = local->rate_ctrl;
1796
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001797 sinfo->generation = sdata->local->sta_generation;
1798
Johannes Berg225b8182015-01-21 21:09:02 +01001799 /* do before driver, so beacon filtering drivers have a
1800 * chance to e.g. just add the number of filtered beacons
1801 * (or just modify the value entirely, of course)
1802 */
1803 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1804 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1805
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001806 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1807
Johannes Berg319090b2014-11-17 14:08:11 +01001808 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1809 BIT(NL80211_STA_INFO_STA_FLAGS) |
1810 BIT(NL80211_STA_INFO_BSS_PARAM) |
1811 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1812 BIT(NL80211_STA_INFO_RX_DROP_MISC) |
1813 BIT(NL80211_STA_INFO_BEACON_LOSS);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001814
Thomas Gleixner18171522014-06-11 23:59:14 +00001815 ktime_get_ts(&uptime);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001816 sinfo->connected_time = uptime.tv_sec - sta->last_connected;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001817 sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001818
Johannes Berg319090b2014-11-17 14:08:11 +01001819 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1820 BIT(NL80211_STA_INFO_TX_BYTES)))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001821 sinfo->tx_bytes = 0;
1822 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1823 sinfo->tx_bytes += sta->tx_bytes[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01001824 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001825 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001826
Johannes Berg319090b2014-11-17 14:08:11 +01001827 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001828 sinfo->tx_packets = 0;
1829 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1830 sinfo->tx_packets += sta->tx_packets[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01001831 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001832 }
1833
Johannes Berg319090b2014-11-17 14:08:11 +01001834 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1835 BIT(NL80211_STA_INFO_RX_BYTES)))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001836 sinfo->rx_bytes = sta->rx_bytes;
Johannes Berg319090b2014-11-17 14:08:11 +01001837 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001838 }
1839
Johannes Berg319090b2014-11-17 14:08:11 +01001840 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001841 sinfo->rx_packets = sta->rx_packets;
Johannes Berg319090b2014-11-17 14:08:11 +01001842 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001843 }
1844
Johannes Berg319090b2014-11-17 14:08:11 +01001845 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001846 sinfo->tx_retries = sta->tx_retry_count;
Johannes Berg319090b2014-11-17 14:08:11 +01001847 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001848 }
1849
Johannes Berg319090b2014-11-17 14:08:11 +01001850 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001851 sinfo->tx_failed = sta->tx_retry_failed;
Johannes Berg319090b2014-11-17 14:08:11 +01001852 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001853 }
1854
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001855 sinfo->rx_dropped_misc = sta->rx_dropped;
1856 sinfo->beacon_loss_count = sta->beacon_loss_count;
1857
Johannes Berg225b8182015-01-21 21:09:02 +01001858 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1859 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1860 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1861 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1862 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1863 }
1864
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001865 if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
1866 (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
Johannes Berg319090b2014-11-17 14:08:11 +01001867 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001868 sinfo->signal = (s8)sta->last_signal;
Johannes Berg319090b2014-11-17 14:08:11 +01001869 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001870 }
1871
Johannes Berg319090b2014-11-17 14:08:11 +01001872 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001873 sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
Johannes Berg319090b2014-11-17 14:08:11 +01001874 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001875 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001876 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001877
1878 if (sta->chains &&
Johannes Berg319090b2014-11-17 14:08:11 +01001879 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1880 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1881 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1882 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001883
1884 sinfo->chains = sta->chains;
1885 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1886 sinfo->chain_signal[i] = sta->chain_signal_last[i];
1887 sinfo->chain_signal_avg[i] =
1888 (s8) -ewma_read(&sta->chain_signal_avg[i]);
1889 }
1890 }
1891
Johannes Berg319090b2014-11-17 14:08:11 +01001892 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001893 sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
Johannes Berg319090b2014-11-17 14:08:11 +01001894 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001895 }
1896
Johannes Berg319090b2014-11-17 14:08:11 +01001897 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001898 sta_set_rate_info_rx(sta, &sinfo->rxrate);
Johannes Berg319090b2014-11-17 14:08:11 +01001899 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001900 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001901
Johannes Berg79c892b2014-11-21 14:26:31 +01001902 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
1903 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
1904 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
1905
1906 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
1907 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
1908 tidstats->rx_msdu = sta->rx_msdu[i];
1909 }
1910
1911 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
1912 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
1913 tidstats->tx_msdu = sta->tx_msdu[i];
1914 }
1915
1916 if (!(tidstats->filled &
1917 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
1918 local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1919 tidstats->filled |=
1920 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
1921 tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
1922 }
1923
1924 if (!(tidstats->filled &
1925 BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
1926 local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1927 tidstats->filled |=
1928 BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
1929 tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
1930 }
1931 }
1932
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001933 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1934#ifdef CONFIG_MAC80211_MESH
Johannes Berg319090b2014-11-17 14:08:11 +01001935 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
1936 BIT(NL80211_STA_INFO_PLID) |
1937 BIT(NL80211_STA_INFO_PLINK_STATE) |
1938 BIT(NL80211_STA_INFO_LOCAL_PM) |
1939 BIT(NL80211_STA_INFO_PEER_PM) |
1940 BIT(NL80211_STA_INFO_NONPEER_PM);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001941
1942 sinfo->llid = sta->llid;
1943 sinfo->plid = sta->plid;
1944 sinfo->plink_state = sta->plink_state;
1945 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
Johannes Berg319090b2014-11-17 14:08:11 +01001946 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001947 sinfo->t_offset = sta->t_offset;
1948 }
1949 sinfo->local_pm = sta->local_pm;
1950 sinfo->peer_pm = sta->peer_pm;
1951 sinfo->nonpeer_pm = sta->nonpeer_pm;
1952#endif
1953 }
1954
1955 sinfo->bss_param.flags = 0;
1956 if (sdata->vif.bss_conf.use_cts_prot)
1957 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1958 if (sdata->vif.bss_conf.use_short_preamble)
1959 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1960 if (sdata->vif.bss_conf.use_short_slot)
1961 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
Emmanuel Grumbach785e21a2014-09-03 15:25:04 +03001962 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001963 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1964
1965 sinfo->sta_flags.set = 0;
1966 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1967 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1968 BIT(NL80211_STA_FLAG_WME) |
1969 BIT(NL80211_STA_FLAG_MFP) |
1970 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1971 BIT(NL80211_STA_FLAG_ASSOCIATED) |
1972 BIT(NL80211_STA_FLAG_TDLS_PEER);
1973 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1974 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1975 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1976 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
Johannes Berga74a8c82014-07-22 14:50:47 +02001977 if (sta->sta.wme)
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001978 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1979 if (test_sta_flag(sta, WLAN_STA_MFP))
1980 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1981 if (test_sta_flag(sta, WLAN_STA_AUTH))
1982 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
1983 if (test_sta_flag(sta, WLAN_STA_ASSOC))
1984 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1985 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1986 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
1987
1988 /* check if the driver has a SW RC implementation */
1989 if (ref && ref->ops->get_expected_throughput)
1990 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
1991 else
1992 thr = drv_get_expected_throughput(local, &sta->sta);
1993
1994 if (thr != 0) {
Johannes Berg319090b2014-11-17 14:08:11 +01001995 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001996 sinfo->expected_throughput = thr;
1997 }
1998}