blob: 39893178a1a982dd9e84ea24c801485871258823 [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,
73};
74
Johannes Berg4d339602011-12-15 11:24:20 +010075/* Caller must hold local->sta_mtx */
Michael Wube8755e2007-07-27 15:43:23 +020076static int sta_info_hash_del(struct ieee80211_local *local,
77 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070078{
Johannes Berg7bedd0c2015-02-13 21:55:15 +010079 return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
80 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -070081}
82
Johannes Berg5108ca82014-02-17 20:49:03 +010083static void __cleanup_single_sta(struct sta_info *sta)
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030084{
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030085 int ac, i;
86 struct tid_ampdu_tx *tid_tx;
87 struct ieee80211_sub_if_data *sdata = sta->sdata;
88 struct ieee80211_local *local = sdata->local;
Marco Porschd012a602012-10-10 12:39:50 -070089 struct ps_data *ps;
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030090
Johannes Berge3685e02014-02-20 11:19:58 +010091 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
Johannes Berg5ac2e352014-05-27 16:32:27 +020092 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
93 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
Marco Porschd012a602012-10-10 12:39:50 -070094 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
95 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
96 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +010097 else if (ieee80211_vif_is_mesh(&sdata->vif))
98 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -070099 else
100 return;
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300101
102 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Berge3685e02014-02-20 11:19:58 +0100103 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200104 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300105
Marco Porschd012a602012-10-10 12:39:50 -0700106 atomic_dec(&ps->num_sta_ps);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300107 }
108
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100109 if (sta->sta.txq[0]) {
110 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
111 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
112 int n = skb_queue_len(&txqi->queue);
113
114 ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
115 atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
116 }
117 }
118
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300119 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
120 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
Felix Fietkau1f98ab72012-11-10 03:44:14 +0100121 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
122 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300123 }
124
Thomas Pedersen45b50282013-02-06 10:17:21 -0800125 if (ieee80211_vif_is_mesh(&sdata->vif))
126 mesh_sta_cleanup(sta);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300127
Johannes Berg5ac2e352014-05-27 16:32:27 +0200128 cancel_work_sync(&sta->drv_deliver_wk);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300129
130 /*
131 * Destroy aggregation state here. It would be nice to wait for the
132 * driver to finish aggregation stop and then clean up, but for now
133 * drivers have to handle aggregation stop being requested, followed
134 * directly by station destruction.
135 */
Johannes Berg5a306f52012-11-14 23:22:21 +0100136 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berg661eb382013-06-12 22:47:56 +0200137 kfree(sta->ampdu_mlme.tid_start_tx[i]);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300138 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
139 if (!tid_tx)
140 continue;
Felix Fietkau1f98ab72012-11-10 03:44:14 +0100141 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300142 kfree(tid_tx);
143 }
Johannes Berg5108ca82014-02-17 20:49:03 +0100144}
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300145
Johannes Berg5108ca82014-02-17 20:49:03 +0100146static void cleanup_single_sta(struct sta_info *sta)
147{
148 struct ieee80211_sub_if_data *sdata = sta->sdata;
149 struct ieee80211_local *local = sdata->local;
150
151 __cleanup_single_sta(sta);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300152 sta_info_free(local, sta);
153}
154
Johannes Bergd0709a62008-02-25 16:27:46 +0100155/* protected by RCU */
Johannes Bergabe60632009-11-25 17:46:18 +0100156struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
157 const u8 *addr)
Johannes Berg43ba7e92008-02-21 14:09:30 +0100158{
Johannes Bergabe60632009-11-25 17:46:18 +0100159 struct ieee80211_local *local = sdata->local;
Johannes Berg60f4b622015-04-23 14:02:30 +0200160 struct sta_info *sta;
161 struct rhash_head *tmp;
162 const struct bucket_table *tbl;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100163
Johannes Berg60f4b622015-04-23 14:02:30 +0200164 rcu_read_lock();
165 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
166
167 for_each_sta_info(local, tbl, addr, sta, tmp) {
168 if (sta->sdata == sdata) {
169 rcu_read_unlock();
170 /* this is safe as the caller must already hold
171 * another rcu read section or the mutex
172 */
173 return sta;
174 }
175 }
176 rcu_read_unlock();
177 return NULL;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100178}
179
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100180/*
181 * Get sta info either from the specified interface
182 * or from one of its vlans
183 */
184struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
185 const u8 *addr)
186{
187 struct ieee80211_local *local = sdata->local;
188 struct sta_info *sta;
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100189 struct rhash_head *tmp;
190 const struct bucket_table *tbl;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100191
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100192 rcu_read_lock();
193 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
194
195 for_each_sta_info(local, tbl, addr, sta, tmp) {
196 if (sta->sdata == sdata ||
197 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
198 rcu_read_unlock();
199 /* this is safe as the caller must already hold
200 * another rcu read section or the mutex
201 */
202 return sta;
203 }
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100204 }
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100205 rcu_read_unlock();
206 return NULL;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100207}
208
Johannes Berg3b53fde82009-11-16 12:00:37 +0100209struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
210 int idx)
Luis Carlos Coboee385852008-02-23 15:17:11 +0100211{
Johannes Berg3b53fde82009-11-16 12:00:37 +0100212 struct ieee80211_local *local = sdata->local;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100213 struct sta_info *sta;
214 int i = 0;
215
Johannes Bergd0709a62008-02-25 16:27:46 +0100216 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg3b53fde82009-11-16 12:00:37 +0100217 if (sdata != sta->sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800218 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100219 if (i < idx) {
220 ++i;
221 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100222 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800223 return sta;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100224 }
Luis Carlos Coboee385852008-02-23 15:17:11 +0100225
226 return NULL;
227}
Jiri Bencf0706e82007-05-05 11:45:53 -0700228
Johannes Berg93e5deb2008-04-01 15:21:00 +0200229/**
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100230 * sta_info_free - free STA
Johannes Berg93e5deb2008-04-01 15:21:00 +0200231 *
Randy Dunlap6ef307b2008-07-03 13:52:18 -0700232 * @local: pointer to the global information
Johannes Berg93e5deb2008-04-01 15:21:00 +0200233 * @sta: STA info to free
234 *
235 * This function must undo everything done by sta_info_alloc()
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100236 * that may happen before sta_info_insert(). It may only be
237 * called when sta_info_insert() has not been attempted (and
238 * if that fails, the station is freed anyway.)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200239 */
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100240void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200241{
Johannes Berg889cbb92012-01-17 10:33:29 +0100242 if (sta->rate_ctrl)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100243 rate_control_free_sta(sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200244
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200245 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200246
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100247 if (sta->sta.txq[0])
248 kfree(to_txq_info(sta->sta.txq[0]));
Felix Fietkau53d04522014-05-27 22:33:57 +0200249 kfree(rcu_dereference_raw(sta->sta.rates));
Johannes Berg93e5deb2008-04-01 15:21:00 +0200250 kfree(sta);
251}
252
Johannes Berg4d339602011-12-15 11:24:20 +0100253/* Caller must hold local->sta_mtx */
Johannes Bergd0709a62008-02-25 16:27:46 +0100254static void sta_info_hash_add(struct ieee80211_local *local,
255 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700256{
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100257 rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
258 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -0700259}
Jiri Bencf0706e82007-05-05 11:45:53 -0700260
Johannes Berg5ac2e352014-05-27 16:32:27 +0200261static void sta_deliver_ps_frames(struct work_struct *wk)
Johannes Bergaf818582009-11-06 11:35:50 +0100262{
263 struct sta_info *sta;
264
Johannes Berg5ac2e352014-05-27 16:32:27 +0200265 sta = container_of(wk, struct sta_info, drv_deliver_wk);
Johannes Bergaf818582009-11-06 11:35:50 +0100266
267 if (sta->dead)
268 return;
269
Johannes Berg5ac2e352014-05-27 16:32:27 +0200270 local_bh_disable();
271 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
Johannes Bergaf818582009-11-06 11:35:50 +0100272 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200273 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
Johannes Bergaf818582009-11-06 11:35:50 +0100274 ieee80211_sta_ps_deliver_poll_response(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200275 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
Johannes Berg47086fc2011-09-29 16:04:33 +0200276 ieee80211_sta_ps_deliver_uapsd(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200277 local_bh_enable();
Johannes Bergaf818582009-11-06 11:35:50 +0100278}
279
Johannes Bergaf65cd962009-11-17 18:18:36 +0100280static int sta_prepare_rate_control(struct ieee80211_local *local,
281 struct sta_info *sta, gfp_t gfp)
282{
283 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
284 return 0;
285
Johannes Berg889cbb92012-01-17 10:33:29 +0100286 sta->rate_ctrl = local->rate_ctrl;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100287 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
288 &sta->sta, gfp);
Johannes Berg889cbb92012-01-17 10:33:29 +0100289 if (!sta->rate_ctrl_priv)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100290 return -ENOMEM;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100291
292 return 0;
293}
294
Johannes Berg73651ee2008-02-25 16:27:47 +0100295struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
Johannes Berg56544162011-12-14 13:28:46 +0100296 const u8 *addr, gfp_t gfp)
Jiri Bencf0706e82007-05-05 11:45:53 -0700297{
Johannes Bergd0709a62008-02-25 16:27:46 +0100298 struct ieee80211_local *local = sdata->local;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100299 struct ieee80211_hw *hw = &local->hw;
Jiri Bencf0706e82007-05-05 11:45:53 -0700300 struct sta_info *sta;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +0530301 struct timespec uptime;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200302 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700303
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100304 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
Jiri Bencf0706e82007-05-05 11:45:53 -0700305 if (!sta)
Johannes Berg73651ee2008-02-25 16:27:47 +0100306 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700307
Johannes Berg07346f812008-05-03 01:02:02 +0200308 spin_lock_init(&sta->lock);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200309 spin_lock_init(&sta->ps_lock);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200310 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
Johannes Berg67c282c2010-06-10 10:21:43 +0200311 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
Johannes Berga93e3642010-06-10 10:21:46 +0200312 mutex_init(&sta->ampdu_mlme.mtx);
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800313#ifdef CONFIG_MAC80211_MESH
314 if (ieee80211_vif_is_mesh(&sdata->vif) &&
315 !sdata->u.mesh.user_mpm)
316 init_timer(&sta->plink_timer);
Thomas Pedersen6c7c4cb2013-06-20 23:50:59 -0700317 sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800318#endif
Johannes Berg07346f812008-05-03 01:02:02 +0200319
Johannes Berg17741cd2008-09-11 00:02:02 +0200320 memcpy(sta->sta.addr, addr, ETH_ALEN);
Johannes Bergd0709a62008-02-25 16:27:46 +0100321 sta->local = local;
322 sta->sdata = sdata;
Felix Fietkau8bc8aec2011-03-21 20:01:00 +0100323 sta->last_rx = jiffies;
Jiri Bencf0706e82007-05-05 11:45:53 -0700324
Johannes Berg71ec3752012-01-20 13:55:20 +0100325 sta->sta_state = IEEE80211_STA_NONE;
326
Liad Kaufmanb6da9112014-11-19 13:47:38 +0200327 /* Mark TID as unreserved */
328 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
329
Thomas Gleixner18171522014-06-11 23:59:14 +0000330 ktime_get_ts(&uptime);
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +0530331 sta->last_connected = uptime.tv_sec;
Bruno Randolf541a45a2010-12-02 19:12:43 +0900332 ewma_init(&sta->avg_signal, 1024, 8);
Felix Fietkauef0621e2013-04-22 16:29:31 +0200333 for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
334 ewma_init(&sta->chain_signal_avg[i], 1024, 8);
Bruno Randolf541a45a2010-12-02 19:12:43 +0900335
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100336 if (local->ops->wake_tx_queue) {
337 void *txq_data;
338 int size = sizeof(struct txq_info) +
339 ALIGN(hw->txq_data_size, sizeof(void *));
340
341 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
342 if (!txq_data)
343 goto free;
344
345 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
346 struct txq_info *txq = txq_data + i * size;
347
348 ieee80211_init_tx_queue(sdata, sta, txq, i);
349 }
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100350 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700351
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100352 if (sta_prepare_rate_control(local, sta, gfp))
353 goto free_txq;
354
Johannes Berg5a306f52012-11-14 23:22:21 +0100355 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berga622ab72010-06-10 10:21:39 +0200356 /*
357 * timer_to_tid must be initialized with identity mapping
358 * to enable session_timer's data differentiation. See
359 * sta_rx_agg_session_timer_expired for usage.
360 */
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200361 sta->timer_to_tid[i] = i;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200362 }
Johannes Berg948d8872011-09-29 16:04:29 +0200363 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
364 skb_queue_head_init(&sta->ps_tx_buf[i]);
365 skb_queue_head_init(&sta->tx_filtered[i]);
366 }
Johannes Berg73651ee2008-02-25 16:27:47 +0100367
Johannes Berg5a306f52012-11-14 23:22:21 +0100368 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700369 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
Senthil Balasubramaniancccaec92009-05-14 18:42:08 +0530370
Johannes Bergaf0ed692013-02-12 14:21:00 +0100371 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300372 if (sdata->vif.type == NL80211_IFTYPE_AP ||
373 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
374 struct ieee80211_supported_band *sband =
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100375 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300376 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
377 IEEE80211_HT_CAP_SM_PS_SHIFT;
378 /*
379 * Assume that hostapd advertises our caps in the beacon and
380 * this is the known_smps_mode for a station that just assciated
381 */
382 switch (smps) {
383 case WLAN_HT_SMPS_CONTROL_DISABLED:
384 sta->known_smps_mode = IEEE80211_SMPS_OFF;
385 break;
386 case WLAN_HT_SMPS_CONTROL_STATIC:
387 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
388 break;
389 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
390 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
391 break;
392 default:
393 WARN_ON(1);
394 }
395 }
Johannes Bergaf0ed692013-02-12 14:21:00 +0100396
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200397 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
Johannes Bergef04a292014-01-06 15:56:59 +0100398
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100399 return sta;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100400
401free_txq:
402 if (sta->sta.txq[0])
403 kfree(to_txq_info(sta->sta.txq[0]));
404free:
405 kfree(sta);
406 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +0100407}
408
Guy Eilam8c71df72011-08-17 15:18:14 +0300409static int sta_info_insert_check(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100410{
Johannes Berg34e89502010-02-03 13:59:58 +0100411 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg34e89502010-02-03 13:59:58 +0100412
Johannes Berg03e44972008-02-27 09:56:40 +0100413 /*
414 * Can't be a WARN_ON because it can be triggered through a race:
415 * something inserts a STA (on one CPU) without holding the RTNL
416 * and another CPU turns off the net device.
417 */
Guy Eilam8c71df72011-08-17 15:18:14 +0300418 if (unlikely(!ieee80211_sdata_running(sdata)))
419 return -ENETDOWN;
Johannes Berg03e44972008-02-27 09:56:40 +0100420
Joe Perchesb203ca32012-05-08 18:56:52 +0000421 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
Guy Eilam8c71df72011-08-17 15:18:14 +0300422 is_multicast_ether_addr(sta->sta.addr)))
423 return -EINVAL;
424
425 return 0;
426}
427
Johannes Bergf09603a2012-01-20 13:55:21 +0100428static int sta_info_insert_drv_state(struct ieee80211_local *local,
429 struct ieee80211_sub_if_data *sdata,
430 struct sta_info *sta)
431{
432 enum ieee80211_sta_state state;
433 int err = 0;
434
435 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
436 err = drv_sta_state(local, sdata, sta, state, state + 1);
437 if (err)
438 break;
439 }
440
441 if (!err) {
Johannes Berga4ec45a2012-01-20 13:55:22 +0100442 /*
443 * Drivers using legacy sta_add/sta_remove callbacks only
444 * get uploaded set to true after sta_add is called.
445 */
446 if (!local->ops->sta_add)
447 sta->uploaded = true;
Johannes Bergf09603a2012-01-20 13:55:21 +0100448 return 0;
449 }
450
451 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200452 sdata_info(sdata,
453 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
454 sta->sta.addr, state + 1, err);
Johannes Bergf09603a2012-01-20 13:55:21 +0100455 err = 0;
456 }
457
458 /* unwind on error */
459 for (; state > IEEE80211_STA_NOTEXIST; state--)
460 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
461
462 return err;
463}
464
Guy Eilam8c71df72011-08-17 15:18:14 +0300465/*
466 * should be called with sta_mtx locked
467 * this function replaces the mutex lock
468 * with a RCU lock
469 */
Johannes Berg4d339602011-12-15 11:24:20 +0100470static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
Guy Eilam8c71df72011-08-17 15:18:14 +0300471{
472 struct ieee80211_local *local = sta->local;
473 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg7852e362012-01-20 13:55:24 +0100474 struct station_info sinfo;
Guy Eilam8c71df72011-08-17 15:18:14 +0300475 int err = 0;
476
477 lockdep_assert_held(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +0100478
Johannes Berg7852e362012-01-20 13:55:24 +0100479 /* check if STA exists already */
480 if (sta_info_get_bss(sdata, sta->sta.addr)) {
481 err = -EEXIST;
482 goto out_err;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100483 }
Johannes Berg32bfd352007-12-19 01:31:26 +0100484
Johannes Berg7852e362012-01-20 13:55:24 +0100485 local->num_sta++;
486 local->sta_generation++;
487 smp_mb();
Johannes Berg4d339602011-12-15 11:24:20 +0100488
Johannes Berg5108ca82014-02-17 20:49:03 +0100489 /* simplify things and don't accept BA sessions yet */
490 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
491
Johannes Berg7852e362012-01-20 13:55:24 +0100492 /* make the station visible */
493 sta_info_hash_add(local, sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100494
Arik Nemtsov2bad77482014-10-22 12:32:16 +0300495 list_add_tail_rcu(&sta->list, &local->sta_list);
Johannes Berg83d5cc02012-01-12 09:31:10 +0100496
Johannes Berg5108ca82014-02-17 20:49:03 +0100497 /* notify driver */
498 err = sta_info_insert_drv_state(local, sdata, sta);
499 if (err)
500 goto out_remove;
501
Johannes Berg7852e362012-01-20 13:55:24 +0100502 set_sta_flag(sta, WLAN_STA_INSERTED);
Johannes Berg5108ca82014-02-17 20:49:03 +0100503 /* accept BA sessions now */
504 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg4d339602011-12-15 11:24:20 +0100505
Eliad Peller21f659b2013-11-11 20:14:01 +0200506 ieee80211_recalc_min_chandef(sdata);
Johannes Berg7852e362012-01-20 13:55:24 +0100507 ieee80211_sta_debugfs_add(sta);
508 rate_control_add_sta_debugfs(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100509
Johannes Berg7852e362012-01-20 13:55:24 +0100510 memset(&sinfo, 0, sizeof(sinfo));
511 sinfo.filled = 0;
512 sinfo.generation = local->sta_generation;
513 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
Johannes Bergd0709a62008-02-25 16:27:46 +0100514
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200515 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700516
Johannes Berg34e89502010-02-03 13:59:58 +0100517 /* move reference to rcu-protected */
518 rcu_read_lock();
519 mutex_unlock(&local->sta_mtx);
Jiri Bence9f207f2007-05-05 11:46:38 -0700520
Johannes Berg73651ee2008-02-25 16:27:47 +0100521 if (ieee80211_vif_is_mesh(&sdata->vif))
522 mesh_accept_plinks_update(sdata);
523
524 return 0;
Johannes Berg5108ca82014-02-17 20:49:03 +0100525 out_remove:
526 sta_info_hash_del(local, sta);
527 list_del_rcu(&sta->list);
528 local->num_sta--;
529 synchronize_net();
530 __cleanup_single_sta(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100531 out_err:
532 mutex_unlock(&local->sta_mtx);
533 rcu_read_lock();
534 return err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300535}
536
537int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
538{
539 struct ieee80211_local *local = sta->local;
Zhao, Gang308f7fc2014-04-21 12:53:00 +0800540 int err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300541
Johannes Berg4d339602011-12-15 11:24:20 +0100542 might_sleep();
543
Guy Eilam8c71df72011-08-17 15:18:14 +0300544 err = sta_info_insert_check(sta);
545 if (err) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700546 rcu_read_lock();
547 goto out_free;
548 }
549
Johannes Berg004c8722008-02-20 11:21:35 +0100550 mutex_lock(&local->sta_mtx);
551
Johannes Berg4d339602011-12-15 11:24:20 +0100552 err = sta_info_insert_finish(sta);
Guy Eilam8c71df72011-08-17 15:18:14 +0300553 if (err)
Johannes Berg004c8722008-02-20 11:21:35 +0100554 goto out_free;
Johannes Bergd0709a62008-02-25 16:27:46 +0100555
Johannes Berg004c8722008-02-20 11:21:35 +0100556 return 0;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200557 out_free:
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100558 sta_info_free(local, sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200559 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -0700560}
561
Johannes Berg34e89502010-02-03 13:59:58 +0100562int sta_info_insert(struct sta_info *sta)
563{
564 int err = sta_info_insert_rcu(sta);
565
566 rcu_read_unlock();
567
568 return err;
569}
570
Marco Porschd012a602012-10-10 12:39:50 -0700571static inline void __bss_tim_set(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100572{
573 /*
574 * This format has been mandated by the IEEE specifications,
575 * so this line may not be changed to use the __set_bit() format.
576 */
Marco Porschd012a602012-10-10 12:39:50 -0700577 tim[id / 8] |= (1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100578}
579
Marco Porschd012a602012-10-10 12:39:50 -0700580static inline void __bss_tim_clear(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100581{
582 /*
583 * This format has been mandated by the IEEE specifications,
584 * so this line may not be changed to use the __clear_bit() format.
585 */
Marco Porschd012a602012-10-10 12:39:50 -0700586 tim[id / 8] &= ~(1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100587}
588
Ilan Peer3d5839b2013-03-05 15:27:20 +0200589static inline bool __bss_tim_get(u8 *tim, u16 id)
590{
591 /*
592 * This format has been mandated by the IEEE specifications,
593 * so this line may not be changed to use the test_bit() format.
594 */
595 return tim[id / 8] & (1 << (id % 8));
596}
597
Johannes Berg948d8872011-09-29 16:04:29 +0200598static unsigned long ieee80211_tids_for_ac(int ac)
Johannes Berg004c8722008-02-20 11:21:35 +0100599{
Johannes Berg948d8872011-09-29 16:04:29 +0200600 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
601 switch (ac) {
602 case IEEE80211_AC_VO:
603 return BIT(6) | BIT(7);
604 case IEEE80211_AC_VI:
605 return BIT(4) | BIT(5);
606 case IEEE80211_AC_BE:
607 return BIT(0) | BIT(3);
608 case IEEE80211_AC_BK:
609 return BIT(1) | BIT(2);
610 default:
611 WARN_ON(1);
612 return 0;
Johannes Bergd0709a62008-02-25 16:27:46 +0100613 }
Johannes Berg004c8722008-02-20 11:21:35 +0100614}
615
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100616static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
Johannes Berg004c8722008-02-20 11:21:35 +0100617{
Johannes Bergc868cb352011-09-29 16:04:27 +0200618 struct ieee80211_local *local = sta->local;
Marco Porschd012a602012-10-10 12:39:50 -0700619 struct ps_data *ps;
Johannes Berg948d8872011-09-29 16:04:29 +0200620 bool indicate_tim = false;
621 u8 ignore_for_tim = sta->sta.uapsd_queues;
622 int ac;
Marco Porschd012a602012-10-10 12:39:50 -0700623 u16 id;
Johannes Berg004c8722008-02-20 11:21:35 +0100624
Marco Porschd012a602012-10-10 12:39:50 -0700625 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
626 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
627 if (WARN_ON_ONCE(!sta->sdata->bss))
628 return;
629
630 ps = &sta->sdata->bss->ps;
631 id = sta->sta.aid;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100632#ifdef CONFIG_MAC80211_MESH
633 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
634 ps = &sta->sdata->u.mesh.ps;
Thomas Pedersen204d1302013-11-05 11:17:05 -0800635 /* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800636 id = sta->plid % (IEEE80211_MAX_AID + 1);
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100637#endif
Marco Porschd012a602012-10-10 12:39:50 -0700638 } else {
Johannes Bergc868cb352011-09-29 16:04:27 +0200639 return;
Marco Porschd012a602012-10-10 12:39:50 -0700640 }
Johannes Berg3e122be2008-07-09 14:40:34 +0200641
Johannes Bergc868cb352011-09-29 16:04:27 +0200642 /* No need to do anything if the driver does all */
643 if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
644 return;
Johannes Berg004c8722008-02-20 11:21:35 +0100645
Johannes Bergc868cb352011-09-29 16:04:27 +0200646 if (sta->dead)
647 goto done;
Johannes Berg3e122be2008-07-09 14:40:34 +0200648
Johannes Berg948d8872011-09-29 16:04:29 +0200649 /*
650 * If all ACs are delivery-enabled then we should build
651 * the TIM bit for all ACs anyway; if only some are then
652 * we ignore those and build the TIM bit using only the
653 * non-enabled ones.
654 */
655 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
656 ignore_for_tim = 0;
Johannes Berg3e122be2008-07-09 14:40:34 +0200657
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100658 if (ignore_pending)
659 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
660
Johannes Berg948d8872011-09-29 16:04:29 +0200661 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
662 unsigned long tids;
663
664 if (ignore_for_tim & BIT(ac))
665 continue;
666
667 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
668 !skb_queue_empty(&sta->ps_tx_buf[ac]);
669 if (indicate_tim)
670 break;
671
672 tids = ieee80211_tids_for_ac(ac);
673
674 indicate_tim |=
675 sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100676 indicate_tim |=
677 sta->txq_buffered_tids & tids;
Johannes Bergd0709a62008-02-25 16:27:46 +0100678 }
Johannes Berg004c8722008-02-20 11:21:35 +0100679
Johannes Bergc868cb352011-09-29 16:04:27 +0200680 done:
Johannes Berg65f704a2013-02-13 17:39:53 +0100681 spin_lock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100682
Ilan Peer3d5839b2013-03-05 15:27:20 +0200683 if (indicate_tim == __bss_tim_get(ps->tim, id))
684 goto out_unlock;
685
Johannes Berg948d8872011-09-29 16:04:29 +0200686 if (indicate_tim)
Marco Porschd012a602012-10-10 12:39:50 -0700687 __bss_tim_set(ps->tim, id);
Johannes Bergc868cb352011-09-29 16:04:27 +0200688 else
Marco Porschd012a602012-10-10 12:39:50 -0700689 __bss_tim_clear(ps->tim, id);
Johannes Berg3e122be2008-07-09 14:40:34 +0200690
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100691 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
Johannes Bergc868cb352011-09-29 16:04:27 +0200692 local->tim_in_locked_section = true;
Johannes Berg948d8872011-09-29 16:04:29 +0200693 drv_set_tim(local, &sta->sta, indicate_tim);
Johannes Bergc868cb352011-09-29 16:04:27 +0200694 local->tim_in_locked_section = false;
Johannes Berg004c8722008-02-20 11:21:35 +0100695 }
Johannes Berg004c8722008-02-20 11:21:35 +0100696
Ilan Peer3d5839b2013-03-05 15:27:20 +0200697out_unlock:
Johannes Berg65f704a2013-02-13 17:39:53 +0100698 spin_unlock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100699}
700
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100701void sta_info_recalc_tim(struct sta_info *sta)
702{
703 __sta_info_recalc_tim(sta, false);
704}
705
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200706static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700707{
Johannes Berge039fa42008-05-15 12:55:29 +0200708 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700709 int timeout;
710
711 if (!skb)
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200712 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700713
Johannes Berge039fa42008-05-15 12:55:29 +0200714 info = IEEE80211_SKB_CB(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700715
716 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200717 timeout = (sta->listen_interval *
718 sta->sdata->vif.bss_conf.beacon_int *
719 32 / 15625) * HZ;
Jiri Bencf0706e82007-05-05 11:45:53 -0700720 if (timeout < STA_TX_BUFFER_EXPIRE)
721 timeout = STA_TX_BUFFER_EXPIRE;
Johannes Berge039fa42008-05-15 12:55:29 +0200722 return time_after(jiffies, info->control.jiffies + timeout);
Jiri Bencf0706e82007-05-05 11:45:53 -0700723}
724
725
Johannes Berg948d8872011-09-29 16:04:29 +0200726static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
727 struct sta_info *sta, int ac)
Jiri Bencf0706e82007-05-05 11:45:53 -0700728{
729 unsigned long flags;
730 struct sk_buff *skb;
731
Johannes Berg60750392011-09-29 16:04:28 +0200732 /*
733 * First check for frames that should expire on the filtered
734 * queue. Frames here were rejected by the driver and are on
735 * a separate queue to avoid reordering with normal PS-buffered
736 * frames. They also aren't accounted for right now in the
737 * total_ps_buffered counter.
738 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700739 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200740 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
741 skb = skb_peek(&sta->tx_filtered[ac]);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200742 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200743 skb = __skb_dequeue(&sta->tx_filtered[ac]);
Johannes Berg836341a2008-02-20 02:07:21 +0100744 else
Jiri Bencf0706e82007-05-05 11:45:53 -0700745 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200746 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700747
Johannes Berg60750392011-09-29 16:04:28 +0200748 /*
749 * Frames are queued in order, so if this one
750 * hasn't expired yet we can stop testing. If
751 * we actually reached the end of the queue we
752 * also need to stop, of course.
753 */
754 if (!skb)
755 break;
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200756 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg60750392011-09-29 16:04:28 +0200757 }
758
759 /*
760 * Now also check the normal PS-buffered queue, this will
761 * only find something if the filtered queue was emptied
762 * since the filtered frames are all before the normal PS
763 * buffered frames.
764 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700765 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200766 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
767 skb = skb_peek(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700768 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200769 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700770 else
771 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200772 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700773
Johannes Berg60750392011-09-29 16:04:28 +0200774 /*
775 * frames are queued in order, so if this one
776 * hasn't expired yet (or we reached the end of
777 * the queue) we can stop testing
778 */
Johannes Berg836341a2008-02-20 02:07:21 +0100779 if (!skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700780 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100781
Johannes Berg836341a2008-02-20 02:07:21 +0100782 local->total_ps_buffered--;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200783 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
784 sta->sta.addr);
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200785 ieee80211_free_txskb(&local->hw, skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700786 }
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300787
Johannes Berg60750392011-09-29 16:04:28 +0200788 /*
789 * Finally, recalculate the TIM bit for this station -- it might
790 * now be clear because the station was too slow to retrieve its
791 * frames.
792 */
793 sta_info_recalc_tim(sta);
794
795 /*
796 * Return whether there are any frames still buffered, this is
797 * used to check whether the cleanup timer still needs to run,
798 * if there are no frames we don't need to rearm the timer.
799 */
Johannes Berg948d8872011-09-29 16:04:29 +0200800 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
801 skb_queue_empty(&sta->tx_filtered[ac]));
802}
803
804static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
805 struct sta_info *sta)
806{
807 bool have_buffered = false;
808 int ac;
809
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100810 /* This is only necessary for stations on BSS/MBSS interfaces */
811 if (!sta->sdata->bss &&
812 !ieee80211_vif_is_mesh(&sta->sdata->vif))
Johannes Berg948d8872011-09-29 16:04:29 +0200813 return false;
814
815 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
816 have_buffered |=
817 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
818
819 return have_buffered;
Jiri Bencf0706e82007-05-05 11:45:53 -0700820}
821
Johannes Bergd7782072013-12-04 23:12:31 +0100822static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100823{
824 struct ieee80211_local *local;
825 struct ieee80211_sub_if_data *sdata;
Johannes Berg6d10e462013-03-06 23:09:11 +0100826 int ret;
Johannes Berg34e89502010-02-03 13:59:58 +0100827
828 might_sleep();
829
830 if (!sta)
831 return -ENOENT;
832
833 local = sta->local;
834 sdata = sta->sdata;
835
Johannes Berg83d5cc02012-01-12 09:31:10 +0100836 lockdep_assert_held(&local->sta_mtx);
837
Johannes Berg098a6072010-04-06 11:18:47 +0200838 /*
839 * Before removing the station from the driver and
840 * rate control, it might still start new aggregation
841 * sessions -- block that to make sure the tear-down
842 * will be sufficient.
843 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200844 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Bergc82c4a82012-07-18 13:31:31 +0200845 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
Johannes Berg098a6072010-04-06 11:18:47 +0200846
Johannes Berg34e89502010-02-03 13:59:58 +0100847 ret = sta_info_hash_del(local, sta);
Johannes Bergb01711b2013-12-04 20:25:27 +0100848 if (WARN_ON(ret))
Johannes Berg34e89502010-02-03 13:59:58 +0100849 return ret;
850
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200851 /*
852 * for TDLS peers, make sure to return to the base channel before
853 * removal.
854 */
855 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
856 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
857 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
858 }
859
Arik Nemtsov794454c2012-06-03 23:32:32 +0300860 list_del_rcu(&sta->list);
Johannes Berg4d339602011-12-15 11:24:20 +0100861
Johannes Berg6a9d1b92013-12-04 22:39:17 +0100862 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
863
Johannes Berga710c812013-12-04 20:11:06 +0100864 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
865 rcu_access_pointer(sdata->u.vlan.sta) == sta)
866 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
867
Johannes Bergd7782072013-12-04 23:12:31 +0100868 return 0;
869}
870
871static void __sta_info_destroy_part2(struct sta_info *sta)
872{
873 struct ieee80211_local *local = sta->local;
874 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg6f7a8d22014-11-14 20:32:57 +0100875 struct station_info sinfo = {};
Johannes Bergd7782072013-12-04 23:12:31 +0100876 int ret;
877
878 /*
879 * NOTE: This assumes at least synchronize_net() was done
880 * after _part1 and before _part2!
881 */
882
883 might_sleep();
884 lockdep_assert_held(&local->sta_mtx);
885
Johannes Bergc8782072013-12-04 23:05:45 +0100886 /* now keys can no longer be reached */
Johannes Berg6d10e462013-03-06 23:09:11 +0100887 ieee80211_free_sta_keys(local, sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100888
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100889 /* disable TIM bit - last chance to tell driver */
890 __sta_info_recalc_tim(sta, true);
891
Johannes Berg34e89502010-02-03 13:59:58 +0100892 sta->dead = true;
893
Johannes Berg34e89502010-02-03 13:59:58 +0100894 local->num_sta--;
895 local->sta_generation++;
896
Johannes Berg83d5cc02012-01-12 09:31:10 +0100897 while (sta->sta_state > IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100898 ret = sta_info_move_state(sta, sta->sta_state - 1);
899 if (ret) {
Johannes Berg83d5cc02012-01-12 09:31:10 +0100900 WARN_ON_ONCE(1);
901 break;
902 }
903 }
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100904
Johannes Bergf09603a2012-01-20 13:55:21 +0100905 if (sta->uploaded) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100906 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
907 IEEE80211_STA_NOTEXIST);
908 WARN_ON_ONCE(ret != 0);
909 }
Johannes Berg34e89502010-02-03 13:59:58 +0100910
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200911 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
912
Johannes Berg6f7a8d22014-11-14 20:32:57 +0100913 sta_set_sinfo(sta, &sinfo);
914 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
Jouni Malinenec15e682011-03-23 15:29:52 +0200915
Johannes Berg34e89502010-02-03 13:59:58 +0100916 rate_control_remove_sta_debugfs(sta);
917 ieee80211_sta_debugfs_remove(sta);
Eliad Peller21f659b2013-11-11 20:14:01 +0200918 ieee80211_recalc_min_chandef(sdata);
Johannes Berg34e89502010-02-03 13:59:58 +0100919
Johannes Bergd34ba212013-12-04 22:46:11 +0100920 cleanup_single_sta(sta);
Johannes Bergd7782072013-12-04 23:12:31 +0100921}
922
923int __must_check __sta_info_destroy(struct sta_info *sta)
924{
925 int err = __sta_info_destroy_part1(sta);
926
927 if (err)
928 return err;
929
930 synchronize_net();
931
932 __sta_info_destroy_part2(sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100933
934 return 0;
935}
936
937int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, 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(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}
949
950int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
951 const u8 *addr)
952{
953 struct sta_info *sta;
954 int ret;
955
956 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100957 sta = sta_info_get_bss(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100958 ret = __sta_info_destroy(sta);
959 mutex_unlock(&sdata->local->sta_mtx);
960
961 return ret;
962}
Jiri Bencf0706e82007-05-05 11:45:53 -0700963
964static void sta_info_cleanup(unsigned long data)
965{
966 struct ieee80211_local *local = (struct ieee80211_local *) data;
967 struct sta_info *sta;
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300968 bool timer_needed = false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700969
Johannes Bergd0709a62008-02-25 16:27:46 +0100970 rcu_read_lock();
971 list_for_each_entry_rcu(sta, &local->sta_list, list)
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300972 if (sta_info_cleanup_expire_buffered(local, sta))
973 timer_needed = true;
Johannes Bergd0709a62008-02-25 16:27:46 +0100974 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -0700975
Johannes Berg5bb644a2009-05-17 11:40:42 +0200976 if (local->quiescing)
977 return;
978
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300979 if (!timer_needed)
980 return;
981
Johannes Berg26d59532011-04-01 13:52:48 +0200982 mod_timer(&local->sta_cleanup,
983 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
Jiri Bencf0706e82007-05-05 11:45:53 -0700984}
985
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100986u32 sta_addr_hash(const void *key, u32 length, u32 seed)
Jiri Bencf0706e82007-05-05 11:45:53 -0700987{
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100988 return jhash(key, ETH_ALEN, seed);
989}
990
991int sta_info_init(struct ieee80211_local *local)
992{
993 int err;
994
995 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
996 if (err)
997 return err;
998
Johannes Berg4d339602011-12-15 11:24:20 +0100999 spin_lock_init(&local->tim_lock);
Johannes Berg34e89502010-02-03 13:59:58 +01001000 mutex_init(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07001001 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001002
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001003 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1004 (unsigned long)local);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001005 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001006}
1007
1008void sta_info_stop(struct ieee80211_local *local)
1009{
Johannes Berga56f9922012-12-13 23:08:52 +01001010 del_timer_sync(&local->sta_cleanup);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001011 rhashtable_destroy(&local->sta_hash);
Jiri Bencf0706e82007-05-05 11:45:53 -07001012}
1013
Johannes Berg051007d2012-12-13 23:49:02 +01001014
Johannes Berge7162512013-12-04 23:18:37 +01001015int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
Jiri Bencf0706e82007-05-05 11:45:53 -07001016{
Johannes Bergb998e8b2012-12-13 23:07:46 +01001017 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001018 struct sta_info *sta, *tmp;
Johannes Bergd7782072013-12-04 23:12:31 +01001019 LIST_HEAD(free_list);
Johannes Berg44213b52008-02-25 16:27:49 +01001020 int ret = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001021
Johannes Bergd0709a62008-02-25 16:27:46 +01001022 might_sleep();
1023
Johannes Berge7162512013-12-04 23:18:37 +01001024 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1025 WARN_ON(vlans && !sdata->bss);
1026
Johannes Berg34e89502010-02-03 13:59:58 +01001027 mutex_lock(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +01001028 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Johannes Berge7162512013-12-04 23:18:37 +01001029 if (sdata == sta->sdata ||
1030 (vlans && sdata->bss == sta->sdata->bss)) {
Johannes Bergd7782072013-12-04 23:12:31 +01001031 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1032 list_add(&sta->free_list, &free_list);
Johannes Berg34316832012-02-25 21:40:46 +01001033 ret++;
1034 }
Johannes Berg34e89502010-02-03 13:59:58 +01001035 }
Johannes Bergd7782072013-12-04 23:12:31 +01001036
1037 if (!list_empty(&free_list)) {
1038 synchronize_net();
1039 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1040 __sta_info_destroy_part2(sta);
1041 }
Johannes Berg34e89502010-02-03 13:59:58 +01001042 mutex_unlock(&local->sta_mtx);
Johannes Berg44213b52008-02-25 16:27:49 +01001043
Johannes Berg051007d2012-12-13 23:49:02 +01001044 return ret;
1045}
1046
Johannes Berg24723d12008-09-11 00:01:46 +02001047void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1048 unsigned long exp_time)
1049{
1050 struct ieee80211_local *local = sdata->local;
1051 struct sta_info *sta, *tmp;
Johannes Berg24723d12008-09-11 00:01:46 +02001052
Johannes Berg34e89502010-02-03 13:59:58 +01001053 mutex_lock(&local->sta_mtx);
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301054
1055 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Marek Lindnerec2b7742011-12-20 23:16:52 +08001056 if (sdata != sta->sdata)
1057 continue;
1058
Johannes Berg24723d12008-09-11 00:01:46 +02001059 if (time_after(jiffies, sta->last_rx + exp_time)) {
Mohammed Shafi Shajakhaneea57d42012-10-08 21:33:47 +05301060 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1061 sta->sta.addr);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001062
1063 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1064 test_sta_flag(sta, WLAN_STA_PS_STA))
1065 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1066
Johannes Berg34e89502010-02-03 13:59:58 +01001067 WARN_ON(__sta_info_destroy(sta));
Johannes Berg24723d12008-09-11 00:01:46 +02001068 }
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301069 }
1070
Johannes Berg34e89502010-02-03 13:59:58 +01001071 mutex_unlock(&local->sta_mtx);
Johannes Berg24723d12008-09-11 00:01:46 +02001072}
Johannes Berg17741cd2008-09-11 00:02:02 +02001073
Ben Greear686b9cb2010-09-23 09:44:36 -07001074struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001075 const u8 *addr,
1076 const u8 *localaddr)
Johannes Berg17741cd2008-09-11 00:02:02 +02001077{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001078 struct ieee80211_local *local = hw_to_local(hw);
1079 struct sta_info *sta;
1080 struct rhash_head *tmp;
1081 const struct bucket_table *tbl;
1082
1083 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
Johannes Berg17741cd2008-09-11 00:02:02 +02001084
Ben Greear686b9cb2010-09-23 09:44:36 -07001085 /*
1086 * Just return a random station if localaddr is NULL
1087 * ... first in list.
1088 */
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001089 for_each_sta_info(local, tbl, addr, sta, tmp) {
Ben Greear686b9cb2010-09-23 09:44:36 -07001090 if (localaddr &&
Joe Perchesb203ca32012-05-08 18:56:52 +00001091 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
Ben Greear686b9cb2010-09-23 09:44:36 -07001092 continue;
Johannes Bergf7c65592010-04-30 13:48:36 +02001093 if (!sta->uploaded)
1094 return NULL;
Johannes Bergabe60632009-11-25 17:46:18 +01001095 return &sta->sta;
Johannes Bergf7c65592010-04-30 13:48:36 +02001096 }
1097
Johannes Bergabe60632009-11-25 17:46:18 +01001098 return NULL;
Johannes Berg17741cd2008-09-11 00:02:02 +02001099}
Ben Greear686b9cb2010-09-23 09:44:36 -07001100EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
Johannes Berg5ed176e2009-11-04 14:42:28 +01001101
1102struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1103 const u8 *addr)
1104{
Johannes Bergf7c65592010-04-30 13:48:36 +02001105 struct sta_info *sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001106
1107 if (!vif)
1108 return NULL;
1109
Johannes Bergf7c65592010-04-30 13:48:36 +02001110 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1111 if (!sta)
1112 return NULL;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001113
Johannes Bergf7c65592010-04-30 13:48:36 +02001114 if (!sta->uploaded)
1115 return NULL;
1116
1117 return &sta->sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001118}
Johannes Berg17741cd2008-09-11 00:02:02 +02001119EXPORT_SYMBOL(ieee80211_find_sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001120
Johannes Berge3685e02014-02-20 11:19:58 +01001121/* powersave support code */
1122void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
Johannes Berg50a94322010-11-16 11:50:28 -08001123{
Helmut Schaa608383b2012-01-30 15:18:00 +01001124 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berge3685e02014-02-20 11:19:58 +01001125 struct ieee80211_local *local = sdata->local;
1126 struct sk_buff_head pending;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001127 int filtered = 0, buffered = 0, ac, i;
Johannes Berge3685e02014-02-20 11:19:58 +01001128 unsigned long flags;
Marco Porschd012a602012-10-10 12:39:50 -07001129 struct ps_data *ps;
1130
Felix Fietkau3918edb2014-07-25 16:20:23 +02001131 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1132 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1133 u.ap);
1134
1135 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porschd012a602012-10-10 12:39:50 -07001136 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001137 else if (ieee80211_vif_is_mesh(&sdata->vif))
1138 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07001139 else
1140 return;
Johannes Berg50a94322010-11-16 11:50:28 -08001141
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001142 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg47086fc2011-09-29 16:04:33 +02001143
Johannes Berg5a306f52012-11-14 23:22:21 +01001144 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
Johannes Berg948d8872011-09-29 16:04:29 +02001145 sta->driver_buffered_tids = 0;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001146 sta->txq_buffered_tids = 0;
Johannes Berg948d8872011-09-29 16:04:29 +02001147
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001148 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1149 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001150
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001151 if (sta->sta.txq[0]) {
1152 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1153 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1154
1155 if (!skb_queue_len(&txqi->queue))
1156 continue;
1157
1158 drv_wake_tx_queue(local, txqi);
1159 }
1160 }
1161
Johannes Berg948d8872011-09-29 16:04:29 +02001162 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001163
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001164 /* sync with ieee80211_tx_h_unicast_ps_buf */
1165 spin_lock(&sta->ps_lock);
Johannes Bergaf818582009-11-06 11:35:50 +01001166 /* Send all buffered frames to the station */
Johannes Berg948d8872011-09-29 16:04:29 +02001167 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1168 int count = skb_queue_len(&pending), tmp;
1169
Arik Nemtsov987c2852012-11-05 10:27:52 +02001170 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001171 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001172 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001173 tmp = skb_queue_len(&pending);
1174 filtered += tmp - count;
1175 count = tmp;
1176
Arik Nemtsov987c2852012-11-05 10:27:52 +02001177 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001178 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001179 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001180 tmp = skb_queue_len(&pending);
1181 buffered += tmp - count;
1182 }
1183
Johannes Berge3685e02014-02-20 11:19:58 +01001184 ieee80211_add_pending_skbs(local, &pending);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001185
1186 /* now we're no longer in the deliver code */
1187 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1188
1189 /* The station might have polled and then woken up before we responded,
1190 * so clear these flags now to avoid them sticking around.
1191 */
1192 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1193 clear_sta_flag(sta, WLAN_STA_UAPSD);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001194 spin_unlock(&sta->ps_lock);
Johannes Berg948d8872011-09-29 16:04:29 +02001195
Johannes Berge3685e02014-02-20 11:19:58 +01001196 atomic_dec(&ps->num_sta_ps);
1197
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001198 /* This station just woke up and isn't aware of our SMPS state */
Chun-Yeow Yeoh062f1d62014-04-22 18:19:25 +08001199 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1200 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001201 sdata->smps_mode) &&
1202 sta->known_smps_mode != sdata->bss->req_smps &&
1203 sta_info_tx_streams(sta) != 1) {
1204 ht_dbg(sdata,
1205 "%pM just woke up and MIMO capable - update SMPS\n",
1206 sta->sta.addr);
1207 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1208 sta->sta.addr,
1209 sdata->vif.bss_conf.bssid);
1210 }
1211
Johannes Bergaf818582009-11-06 11:35:50 +01001212 local->total_ps_buffered -= buffered;
1213
Johannes Bergc868cb352011-09-29 16:04:27 +02001214 sta_info_recalc_tim(sta);
1215
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001216 ps_dbg(sdata,
1217 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1218 sta->sta.addr, sta->sta.aid, filtered, buffered);
Johannes Bergaf818582009-11-06 11:35:50 +01001219}
1220
Johannes Bergce662b442011-09-29 16:04:34 +02001221static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1222 struct sta_info *sta, int tid,
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001223 enum ieee80211_frame_release_type reason,
1224 bool call_driver)
Johannes Bergce662b442011-09-29 16:04:34 +02001225{
1226 struct ieee80211_local *local = sdata->local;
1227 struct ieee80211_qos_hdr *nullfunc;
1228 struct sk_buff *skb;
1229 int size = sizeof(*nullfunc);
1230 __le16 fc;
Johannes Berga74a8c82014-07-22 14:50:47 +02001231 bool qos = sta->sta.wme;
Johannes Bergce662b442011-09-29 16:04:34 +02001232 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02001233 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergce662b442011-09-29 16:04:34 +02001234
1235 if (qos) {
1236 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1237 IEEE80211_STYPE_QOS_NULLFUNC |
1238 IEEE80211_FCTL_FROMDS);
1239 } else {
1240 size -= 2;
1241 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1242 IEEE80211_STYPE_NULLFUNC |
1243 IEEE80211_FCTL_FROMDS);
1244 }
1245
1246 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1247 if (!skb)
1248 return;
1249
1250 skb_reserve(skb, local->hw.extra_tx_headroom);
1251
1252 nullfunc = (void *) skb_put(skb, size);
1253 nullfunc->frame_control = fc;
1254 nullfunc->duration_id = 0;
1255 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1256 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1257 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
Johannes Berg864a6042014-03-04 13:46:53 +01001258 nullfunc->seq_ctrl = 0;
Johannes Bergce662b442011-09-29 16:04:34 +02001259
Johannes Berg59b66252011-10-13 13:19:19 +02001260 skb->priority = tid;
1261 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
Johannes Bergce662b442011-09-29 16:04:34 +02001262 if (qos) {
Johannes Bergce662b442011-09-29 16:04:34 +02001263 nullfunc->qos_ctrl = cpu_to_le16(tid);
1264
Johannes Berg40b96402011-09-29 16:04:38 +02001265 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
Johannes Bergce662b442011-09-29 16:04:34 +02001266 nullfunc->qos_ctrl |=
1267 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1268 }
1269
1270 info = IEEE80211_SKB_CB(skb);
1271
1272 /*
1273 * Tell TX path to send this frame even though the
1274 * STA may still remain is PS mode after this frame
Johannes Bergdeeaee192011-09-29 16:04:35 +02001275 * exchange. Also set EOSP to indicate this packet
1276 * ends the poll/service period.
Johannes Bergce662b442011-09-29 16:04:34 +02001277 */
Johannes Berg02f2f1a2012-02-27 12:18:30 +01001278 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
Johannes Bergdeeaee192011-09-29 16:04:35 +02001279 IEEE80211_TX_STATUS_EOSP |
1280 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergce662b442011-09-29 16:04:34 +02001281
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301282 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1283
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001284 if (call_driver)
1285 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1286 reason, false);
Johannes Berg40b96402011-09-29 16:04:38 +02001287
Johannes Berg89afe612013-02-13 15:39:57 +01001288 skb->dev = sdata->dev;
1289
Johannes Berg55de9082012-07-26 17:24:39 +02001290 rcu_read_lock();
1291 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1292 if (WARN_ON(!chanctx_conf)) {
1293 rcu_read_unlock();
1294 kfree_skb(skb);
1295 return;
1296 }
1297
Johannes Berg73c4e192014-11-09 18:50:09 +02001298 info->band = chanctx_conf->def.chan->band;
Johannes Berg7c107702015-03-20 14:18:27 +01001299 ieee80211_xmit(sdata, sta, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001300 rcu_read_unlock();
Johannes Bergce662b442011-09-29 16:04:34 +02001301}
1302
Johannes Berg0a1cb802014-01-09 11:05:31 +01001303static int find_highest_prio_tid(unsigned long tids)
1304{
1305 /* lower 3 TIDs aren't ordered perfectly */
1306 if (tids & 0xF8)
1307 return fls(tids) - 1;
1308 /* TID 0 is BE just like TID 3 */
1309 if (tids & BIT(0))
1310 return 0;
1311 return fls(tids) - 1;
1312}
1313
Johannes Berg47086fc2011-09-29 16:04:33 +02001314static void
1315ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1316 int n_frames, u8 ignored_acs,
1317 enum ieee80211_frame_release_type reason)
Johannes Bergaf818582009-11-06 11:35:50 +01001318{
1319 struct ieee80211_sub_if_data *sdata = sta->sdata;
1320 struct ieee80211_local *local = sdata->local;
Johannes Berg948d8872011-09-29 16:04:29 +02001321 bool more_data = false;
1322 int ac;
Johannes Berg4049e092011-09-29 16:04:32 +02001323 unsigned long driver_release_tids = 0;
Johannes Berg47086fc2011-09-29 16:04:33 +02001324 struct sk_buff_head frames;
1325
Johannes Bergdeeaee192011-09-29 16:04:35 +02001326 /* Service or PS-Poll period starts */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001327 set_sta_flag(sta, WLAN_STA_SP);
Johannes Bergdeeaee192011-09-29 16:04:35 +02001328
Johannes Berg47086fc2011-09-29 16:04:33 +02001329 __skb_queue_head_init(&frames);
Johannes Bergaf818582009-11-06 11:35:50 +01001330
Johannes Bergf9f760b2014-01-08 17:45:07 +01001331 /* Get response frame(s) and more data bit for the last one. */
Johannes Berg948d8872011-09-29 16:04:29 +02001332 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg4049e092011-09-29 16:04:32 +02001333 unsigned long tids;
1334
Johannes Berg47086fc2011-09-29 16:04:33 +02001335 if (ignored_acs & BIT(ac))
Johannes Berg948d8872011-09-29 16:04:29 +02001336 continue;
1337
Johannes Berg4049e092011-09-29 16:04:32 +02001338 tids = ieee80211_tids_for_ac(ac);
1339
Johannes Bergf9f760b2014-01-08 17:45:07 +01001340 /* if we already have frames from software, then we can't also
1341 * release from hardware queues
1342 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001343 if (skb_queue_empty(&frames)) {
Johannes Bergf9f760b2014-01-08 17:45:07 +01001344 driver_release_tids |= sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001345 driver_release_tids |= sta->txq_buffered_tids & tids;
1346 }
Johannes Berg47086fc2011-09-29 16:04:33 +02001347
Johannes Bergf9f760b2014-01-08 17:45:07 +01001348 if (driver_release_tids) {
1349 /* If the driver has data on more than one TID then
Johannes Berg4049e092011-09-29 16:04:32 +02001350 * certainly there's more data if we release just a
Johannes Bergf9f760b2014-01-08 17:45:07 +01001351 * single frame now (from a single TID). This will
1352 * only happen for PS-Poll.
Johannes Berg4049e092011-09-29 16:04:32 +02001353 */
Johannes Berg47086fc2011-09-29 16:04:33 +02001354 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1355 hweight16(driver_release_tids) > 1) {
Johannes Berg4049e092011-09-29 16:04:32 +02001356 more_data = true;
1357 driver_release_tids =
Johannes Berg0a1cb802014-01-09 11:05:31 +01001358 BIT(find_highest_prio_tid(
1359 driver_release_tids));
Johannes Berg4049e092011-09-29 16:04:32 +02001360 break;
Johannes Berg948d8872011-09-29 16:04:29 +02001361 }
Johannes Bergf9f760b2014-01-08 17:45:07 +01001362 } else {
1363 struct sk_buff *skb;
1364
1365 while (n_frames > 0) {
1366 skb = skb_dequeue(&sta->tx_filtered[ac]);
1367 if (!skb) {
1368 skb = skb_dequeue(
1369 &sta->ps_tx_buf[ac]);
1370 if (skb)
1371 local->total_ps_buffered--;
1372 }
1373 if (!skb)
1374 break;
1375 n_frames--;
1376 __skb_queue_tail(&frames, skb);
1377 }
Johannes Berg948d8872011-09-29 16:04:29 +02001378 }
1379
Johannes Bergf9f760b2014-01-08 17:45:07 +01001380 /* If we have more frames buffered on this AC, then set the
1381 * more-data bit and abort the loop since we can't send more
1382 * data from other ACs before the buffered frames from this.
1383 */
Johannes Berg948d8872011-09-29 16:04:29 +02001384 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1385 !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1386 more_data = true;
1387 break;
1388 }
Johannes Bergaf818582009-11-06 11:35:50 +01001389 }
Johannes Bergaf818582009-11-06 11:35:50 +01001390
Johannes Bergf9f760b2014-01-08 17:45:07 +01001391 if (skb_queue_empty(&frames) && !driver_release_tids) {
Johannes Bergce662b442011-09-29 16:04:34 +02001392 int tid;
Johannes Berg4049e092011-09-29 16:04:32 +02001393
Johannes Bergce662b442011-09-29 16:04:34 +02001394 /*
1395 * For PS-Poll, this can only happen due to a race condition
1396 * when we set the TIM bit and the station notices it, but
1397 * before it can poll for the frame we expire it.
1398 *
1399 * For uAPSD, this is said in the standard (11.2.1.5 h):
1400 * At each unscheduled SP for a non-AP STA, the AP shall
1401 * attempt to transmit at least one MSDU or MMPDU, but no
1402 * more than the value specified in the Max SP Length field
1403 * in the QoS Capability element from delivery-enabled ACs,
1404 * that are destined for the non-AP STA.
1405 *
1406 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1407 */
1408
1409 /* This will evaluate to 1, 3, 5 or 7. */
1410 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1411
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001412 ieee80211_send_null_response(sdata, sta, tid, reason, true);
Johannes Bergf9f760b2014-01-08 17:45:07 +01001413 } else if (!driver_release_tids) {
Johannes Berg47086fc2011-09-29 16:04:33 +02001414 struct sk_buff_head pending;
1415 struct sk_buff *skb;
Johannes Berg40b96402011-09-29 16:04:38 +02001416 int num = 0;
1417 u16 tids = 0;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001418 bool need_null = false;
Johannes Bergaf818582009-11-06 11:35:50 +01001419
Johannes Berg47086fc2011-09-29 16:04:33 +02001420 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001421
Johannes Berg47086fc2011-09-29 16:04:33 +02001422 while ((skb = __skb_dequeue(&frames))) {
1423 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1424 struct ieee80211_hdr *hdr = (void *) skb->data;
Johannes Berg40b96402011-09-29 16:04:38 +02001425 u8 *qoshdr = NULL;
1426
1427 num++;
Johannes Bergaf818582009-11-06 11:35:50 +01001428
Johannes Berg47086fc2011-09-29 16:04:33 +02001429 /*
1430 * Tell TX path to send this frame even though the
1431 * STA may still remain is PS mode after this frame
1432 * exchange.
1433 */
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301434 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1435 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
Johannes Bergaf818582009-11-06 11:35:50 +01001436
Johannes Berg47086fc2011-09-29 16:04:33 +02001437 /*
1438 * Use MoreData flag to indicate whether there are
1439 * more buffered frames for this STA
1440 */
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001441 if (more_data || !skb_queue_empty(&frames))
Johannes Berg47086fc2011-09-29 16:04:33 +02001442 hdr->frame_control |=
1443 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001444 else
1445 hdr->frame_control &=
1446 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
Johannes Berg47086fc2011-09-29 16:04:33 +02001447
Johannes Berg40b96402011-09-29 16:04:38 +02001448 if (ieee80211_is_data_qos(hdr->frame_control) ||
1449 ieee80211_is_qos_nullfunc(hdr->frame_control))
1450 qoshdr = ieee80211_get_qos_ctl(hdr);
1451
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001452 tids |= BIT(skb->priority);
1453
1454 __skb_queue_tail(&pending, skb);
1455
1456 /* end service period after last frame or add one */
1457 if (!skb_queue_empty(&frames))
1458 continue;
1459
1460 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1461 /* for PS-Poll, there's only one frame */
1462 info->flags |= IEEE80211_TX_STATUS_EOSP |
1463 IEEE80211_TX_CTL_REQ_TX_STATUS;
1464 break;
1465 }
1466
1467 /* For uAPSD, things are a bit more complicated. If the
1468 * last frame has a QoS header (i.e. is a QoS-data or
1469 * QoS-nulldata frame) then just set the EOSP bit there
1470 * and be done.
1471 * If the frame doesn't have a QoS header (which means
1472 * it should be a bufferable MMPDU) then we can't set
1473 * the EOSP bit in the QoS header; add a QoS-nulldata
1474 * frame to the list to send it after the MMPDU.
1475 *
1476 * Note that this code is only in the mac80211-release
1477 * code path, we assume that the driver will not buffer
1478 * anything but QoS-data frames, or if it does, will
1479 * create the QoS-nulldata frame by itself if needed.
1480 *
1481 * Cf. 802.11-2012 10.2.1.10 (c).
1482 */
1483 if (qoshdr) {
1484 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
Johannes Berg47086fc2011-09-29 16:04:33 +02001485
Marco Porsch52a3f202012-03-16 15:30:26 +01001486 info->flags |= IEEE80211_TX_STATUS_EOSP |
1487 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001488 } else {
1489 /* The standard isn't completely clear on this
1490 * as it says the more-data bit should be set
1491 * if there are more BUs. The QoS-Null frame
1492 * we're about to send isn't buffered yet, we
1493 * only create it below, but let's pretend it
1494 * was buffered just in case some clients only
1495 * expect more-data=0 when eosp=1.
1496 */
1497 hdr->frame_control |=
1498 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1499 need_null = true;
1500 num++;
Marco Porsch52a3f202012-03-16 15:30:26 +01001501 }
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001502 break;
Johannes Berg47086fc2011-09-29 16:04:33 +02001503 }
1504
Johannes Berg40b96402011-09-29 16:04:38 +02001505 drv_allow_buffered_frames(local, sta, tids, num,
1506 reason, more_data);
1507
Johannes Berg47086fc2011-09-29 16:04:33 +02001508 ieee80211_add_pending_skbs(local, &pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001509
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001510 if (need_null)
1511 ieee80211_send_null_response(
1512 sdata, sta, find_highest_prio_tid(tids),
1513 reason, false);
1514
Johannes Bergc868cb352011-09-29 16:04:27 +02001515 sta_info_recalc_tim(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001516 } else {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001517 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1518 int tid;
1519
Johannes Bergaf818582009-11-06 11:35:50 +01001520 /*
Johannes Berg4049e092011-09-29 16:04:32 +02001521 * We need to release a frame that is buffered somewhere in the
1522 * driver ... it'll have to handle that.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001523 * Note that the driver also has to check the number of frames
1524 * on the TIDs we're releasing from - if there are more than
1525 * n_frames it has to set the more-data bit (if we didn't ask
1526 * it to set it anyway due to other buffered frames); if there
1527 * are fewer than n_frames it has to make sure to adjust that
1528 * to allow the service period to end properly.
Johannes Bergaf818582009-11-06 11:35:50 +01001529 */
Johannes Berg4049e092011-09-29 16:04:32 +02001530 drv_release_buffered_frames(local, sta, driver_release_tids,
Johannes Berg47086fc2011-09-29 16:04:33 +02001531 n_frames, reason, more_data);
Johannes Berg4049e092011-09-29 16:04:32 +02001532
1533 /*
1534 * Note that we don't recalculate the TIM bit here as it would
1535 * most likely have no effect at all unless the driver told us
Johannes Bergf9f760b2014-01-08 17:45:07 +01001536 * that the TID(s) became empty before returning here from the
Johannes Berg4049e092011-09-29 16:04:32 +02001537 * release function.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001538 * Either way, however, when the driver tells us that the TID(s)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001539 * became empty or we find that a txq became empty, we'll do the
1540 * TIM recalculation.
Johannes Berg4049e092011-09-29 16:04:32 +02001541 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001542
1543 if (!sta->sta.txq[0])
1544 return;
1545
1546 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1547 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1548
1549 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1550 continue;
1551
1552 sta_info_recalc_tim(sta);
1553 break;
1554 }
Johannes Bergaf818582009-11-06 11:35:50 +01001555 }
1556}
1557
Johannes Bergaf818582009-11-06 11:35:50 +01001558void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1559{
Johannes Berg47086fc2011-09-29 16:04:33 +02001560 u8 ignore_for_response = sta->sta.uapsd_queues;
Johannes Bergaf818582009-11-06 11:35:50 +01001561
Johannes Berg47086fc2011-09-29 16:04:33 +02001562 /*
1563 * If all ACs are delivery-enabled then we should reply
1564 * from any of them, if only some are enabled we reply
1565 * only from the non-enabled ones.
1566 */
1567 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1568 ignore_for_response = 0;
1569
1570 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1571 IEEE80211_FRAME_RELEASE_PSPOLL);
1572}
1573
1574void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1575{
1576 int n_frames = sta->sta.max_sp;
1577 u8 delivery_enabled = sta->sta.uapsd_queues;
1578
1579 /*
1580 * If we ever grow support for TSPEC this might happen if
1581 * the TSPEC update from hostapd comes in between a trigger
1582 * frame setting WLAN_STA_UAPSD in the RX path and this
1583 * actually getting called.
1584 */
1585 if (!delivery_enabled)
1586 return;
1587
Johannes Berg47086fc2011-09-29 16:04:33 +02001588 switch (sta->sta.max_sp) {
1589 case 1:
1590 n_frames = 2;
1591 break;
1592 case 2:
1593 n_frames = 4;
1594 break;
1595 case 3:
1596 n_frames = 6;
1597 break;
1598 case 0:
1599 /* XXX: what is a good value? */
Andrei Otcheretianski13a80982014-11-04 11:33:04 +02001600 n_frames = 128;
Johannes Berg47086fc2011-09-29 16:04:33 +02001601 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001602 }
Johannes Bergaf818582009-11-06 11:35:50 +01001603
Johannes Berg47086fc2011-09-29 16:04:33 +02001604 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1605 IEEE80211_FRAME_RELEASE_UAPSD);
Johannes Bergaf818582009-11-06 11:35:50 +01001606}
1607
1608void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1609 struct ieee80211_sta *pubsta, bool block)
1610{
1611 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1612
Johannes Bergb5878a22010-04-07 16:48:40 +02001613 trace_api_sta_block_awake(sta->local, pubsta, block);
1614
Johannes Berg5ac2e352014-05-27 16:32:27 +02001615 if (block) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001616 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001617 return;
1618 }
1619
1620 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1621 return;
1622
1623 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1624 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1625 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1626 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1627 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1628 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1629 /* must be asleep in this case */
1630 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1631 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1632 } else {
1633 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1634 }
Johannes Bergaf818582009-11-06 11:35:50 +01001635}
1636EXPORT_SYMBOL(ieee80211_sta_block_awake);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001637
Johannes Berge9437892013-02-15 21:38:08 +01001638void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
Johannes Berg37fbd902011-09-29 16:04:39 +02001639{
1640 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1641 struct ieee80211_local *local = sta->local;
Johannes Berg37fbd902011-09-29 16:04:39 +02001642
1643 trace_api_eosp(local, pubsta);
1644
Johannes Berge9437892013-02-15 21:38:08 +01001645 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg37fbd902011-09-29 16:04:39 +02001646}
Johannes Berge9437892013-02-15 21:38:08 +01001647EXPORT_SYMBOL(ieee80211_sta_eosp);
Johannes Berg37fbd902011-09-29 16:04:39 +02001648
Johannes Berg042ec452011-09-29 16:04:26 +02001649void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1650 u8 tid, bool buffered)
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001651{
1652 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1653
Johannes Berg5a306f52012-11-14 23:22:21 +01001654 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
Johannes Berg042ec452011-09-29 16:04:26 +02001655 return;
1656
Johannes Berg1b000782013-12-19 10:47:48 +01001657 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1658
Johannes Berg948d8872011-09-29 16:04:29 +02001659 if (buffered)
1660 set_bit(tid, &sta->driver_buffered_tids);
1661 else
1662 clear_bit(tid, &sta->driver_buffered_tids);
1663
Johannes Bergc868cb352011-09-29 16:04:27 +02001664 sta_info_recalc_tim(sta);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001665}
Johannes Berg042ec452011-09-29 16:04:26 +02001666EXPORT_SYMBOL(ieee80211_sta_set_buffered);
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001667
Johannes Berg83d5cc02012-01-12 09:31:10 +01001668int sta_info_move_state(struct sta_info *sta,
1669 enum ieee80211_sta_state new_state)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001670{
Johannes Berg8bf11d82011-12-15 11:17:37 +01001671 might_sleep();
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001672
1673 if (sta->sta_state == new_state)
1674 return 0;
1675
Johannes Bergf09603a2012-01-20 13:55:21 +01001676 /* check allowed transitions first */
1677
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001678 switch (new_state) {
1679 case IEEE80211_STA_NONE:
Johannes Bergf09603a2012-01-20 13:55:21 +01001680 if (sta->sta_state != IEEE80211_STA_AUTH)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001681 return -EINVAL;
1682 break;
1683 case IEEE80211_STA_AUTH:
Johannes Bergf09603a2012-01-20 13:55:21 +01001684 if (sta->sta_state != IEEE80211_STA_NONE &&
1685 sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001686 return -EINVAL;
1687 break;
1688 case IEEE80211_STA_ASSOC:
Johannes Bergf09603a2012-01-20 13:55:21 +01001689 if (sta->sta_state != IEEE80211_STA_AUTH &&
1690 sta->sta_state != IEEE80211_STA_AUTHORIZED)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001691 return -EINVAL;
1692 break;
1693 case IEEE80211_STA_AUTHORIZED:
Johannes Bergf09603a2012-01-20 13:55:21 +01001694 if (sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001695 return -EINVAL;
1696 break;
1697 default:
1698 WARN(1, "invalid state %d", new_state);
1699 return -EINVAL;
1700 }
1701
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001702 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1703 sta->sta.addr, new_state);
Johannes Bergf09603a2012-01-20 13:55:21 +01001704
1705 /*
1706 * notify the driver before the actual changes so it can
1707 * fail the transition
1708 */
1709 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1710 int err = drv_sta_state(sta->local, sta->sdata, sta,
1711 sta->sta_state, new_state);
1712 if (err)
1713 return err;
1714 }
1715
1716 /* reflect the change in all state variables */
1717
1718 switch (new_state) {
1719 case IEEE80211_STA_NONE:
1720 if (sta->sta_state == IEEE80211_STA_AUTH)
1721 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1722 break;
1723 case IEEE80211_STA_AUTH:
1724 if (sta->sta_state == IEEE80211_STA_NONE)
1725 set_bit(WLAN_STA_AUTH, &sta->_flags);
1726 else if (sta->sta_state == IEEE80211_STA_ASSOC)
1727 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1728 break;
1729 case IEEE80211_STA_ASSOC:
1730 if (sta->sta_state == IEEE80211_STA_AUTH) {
1731 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1732 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
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_dec(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001737 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1738 }
1739 break;
1740 case IEEE80211_STA_AUTHORIZED:
1741 if (sta->sta_state == IEEE80211_STA_ASSOC) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001742 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1743 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1744 !sta->sdata->u.vlan.sta))
1745 atomic_inc(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001746 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1747 }
1748 break;
1749 default:
1750 break;
1751 }
1752
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001753 sta->sta_state = new_state;
1754
1755 return 0;
1756}
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001757
1758u8 sta_info_tx_streams(struct sta_info *sta)
1759{
1760 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1761 u8 rx_streams;
1762
1763 if (!sta->sta.ht_cap.ht_supported)
1764 return 1;
1765
1766 if (sta->sta.vht_cap.vht_supported) {
1767 int i;
1768 u16 tx_mcs_map =
1769 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1770
1771 for (i = 7; i >= 0; i--)
1772 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1773 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1774 return i + 1;
1775 }
1776
1777 if (ht_cap->mcs.rx_mask[3])
1778 rx_streams = 4;
1779 else if (ht_cap->mcs.rx_mask[2])
1780 rx_streams = 3;
1781 else if (ht_cap->mcs.rx_mask[1])
1782 rx_streams = 2;
1783 else
1784 rx_streams = 1;
1785
1786 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1787 return rx_streams;
1788
1789 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1790 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1791}
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001792
1793void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1794{
1795 struct ieee80211_sub_if_data *sdata = sta->sdata;
1796 struct ieee80211_local *local = sdata->local;
John W. Linville9a244402014-07-25 10:22:36 -04001797 struct rate_control_ref *ref = NULL;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001798 struct timespec uptime;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001799 u32 thr = 0;
1800 int i, ac;
1801
John W. Linville9a244402014-07-25 10:22:36 -04001802 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1803 ref = local->rate_ctrl;
1804
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001805 sinfo->generation = sdata->local->sta_generation;
1806
Johannes Berg225b8182015-01-21 21:09:02 +01001807 /* do before driver, so beacon filtering drivers have a
1808 * chance to e.g. just add the number of filtered beacons
1809 * (or just modify the value entirely, of course)
1810 */
1811 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1812 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1813
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001814 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1815
Johannes Berg319090b2014-11-17 14:08:11 +01001816 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1817 BIT(NL80211_STA_INFO_STA_FLAGS) |
1818 BIT(NL80211_STA_INFO_BSS_PARAM) |
1819 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1820 BIT(NL80211_STA_INFO_RX_DROP_MISC) |
1821 BIT(NL80211_STA_INFO_BEACON_LOSS);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001822
Thomas Gleixner18171522014-06-11 23:59:14 +00001823 ktime_get_ts(&uptime);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001824 sinfo->connected_time = uptime.tv_sec - sta->last_connected;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001825 sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001826
Johannes Berg319090b2014-11-17 14:08:11 +01001827 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1828 BIT(NL80211_STA_INFO_TX_BYTES)))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001829 sinfo->tx_bytes = 0;
1830 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1831 sinfo->tx_bytes += sta->tx_bytes[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01001832 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001833 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001834
Johannes Berg319090b2014-11-17 14:08:11 +01001835 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001836 sinfo->tx_packets = 0;
1837 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1838 sinfo->tx_packets += sta->tx_packets[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01001839 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001840 }
1841
Johannes Berg319090b2014-11-17 14:08:11 +01001842 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1843 BIT(NL80211_STA_INFO_RX_BYTES)))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001844 sinfo->rx_bytes = sta->rx_bytes;
Johannes Berg319090b2014-11-17 14:08:11 +01001845 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001846 }
1847
Johannes Berg319090b2014-11-17 14:08:11 +01001848 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001849 sinfo->rx_packets = sta->rx_packets;
Johannes Berg319090b2014-11-17 14:08:11 +01001850 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001851 }
1852
Johannes Berg319090b2014-11-17 14:08:11 +01001853 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001854 sinfo->tx_retries = sta->tx_retry_count;
Johannes Berg319090b2014-11-17 14:08:11 +01001855 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001856 }
1857
Johannes Berg319090b2014-11-17 14:08:11 +01001858 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001859 sinfo->tx_failed = sta->tx_retry_failed;
Johannes Berg319090b2014-11-17 14:08:11 +01001860 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001861 }
1862
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001863 sinfo->rx_dropped_misc = sta->rx_dropped;
1864 sinfo->beacon_loss_count = sta->beacon_loss_count;
1865
Johannes Berg225b8182015-01-21 21:09:02 +01001866 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1867 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1868 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1869 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1870 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1871 }
1872
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001873 if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
1874 (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
Johannes Berg319090b2014-11-17 14:08:11 +01001875 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001876 sinfo->signal = (s8)sta->last_signal;
Johannes Berg319090b2014-11-17 14:08:11 +01001877 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001878 }
1879
Johannes Berg319090b2014-11-17 14:08:11 +01001880 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001881 sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
Johannes Berg319090b2014-11-17 14:08:11 +01001882 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001883 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001884 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001885
1886 if (sta->chains &&
Johannes Berg319090b2014-11-17 14:08:11 +01001887 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1888 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1889 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1890 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001891
1892 sinfo->chains = sta->chains;
1893 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1894 sinfo->chain_signal[i] = sta->chain_signal_last[i];
1895 sinfo->chain_signal_avg[i] =
1896 (s8) -ewma_read(&sta->chain_signal_avg[i]);
1897 }
1898 }
1899
Johannes Berg319090b2014-11-17 14:08:11 +01001900 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001901 sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
Johannes Berg319090b2014-11-17 14:08:11 +01001902 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001903 }
1904
Johannes Berg319090b2014-11-17 14:08:11 +01001905 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001906 sta_set_rate_info_rx(sta, &sinfo->rxrate);
Johannes Berg319090b2014-11-17 14:08:11 +01001907 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001908 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001909
Johannes Berg79c892b2014-11-21 14:26:31 +01001910 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
1911 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
1912 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
1913
1914 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
1915 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
1916 tidstats->rx_msdu = sta->rx_msdu[i];
1917 }
1918
1919 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
1920 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
1921 tidstats->tx_msdu = sta->tx_msdu[i];
1922 }
1923
1924 if (!(tidstats->filled &
1925 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
1926 local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1927 tidstats->filled |=
1928 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
1929 tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
1930 }
1931
1932 if (!(tidstats->filled &
1933 BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
1934 local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1935 tidstats->filled |=
1936 BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
1937 tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
1938 }
1939 }
1940
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001941 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1942#ifdef CONFIG_MAC80211_MESH
Johannes Berg319090b2014-11-17 14:08:11 +01001943 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
1944 BIT(NL80211_STA_INFO_PLID) |
1945 BIT(NL80211_STA_INFO_PLINK_STATE) |
1946 BIT(NL80211_STA_INFO_LOCAL_PM) |
1947 BIT(NL80211_STA_INFO_PEER_PM) |
1948 BIT(NL80211_STA_INFO_NONPEER_PM);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001949
1950 sinfo->llid = sta->llid;
1951 sinfo->plid = sta->plid;
1952 sinfo->plink_state = sta->plink_state;
1953 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
Johannes Berg319090b2014-11-17 14:08:11 +01001954 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001955 sinfo->t_offset = sta->t_offset;
1956 }
1957 sinfo->local_pm = sta->local_pm;
1958 sinfo->peer_pm = sta->peer_pm;
1959 sinfo->nonpeer_pm = sta->nonpeer_pm;
1960#endif
1961 }
1962
1963 sinfo->bss_param.flags = 0;
1964 if (sdata->vif.bss_conf.use_cts_prot)
1965 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1966 if (sdata->vif.bss_conf.use_short_preamble)
1967 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1968 if (sdata->vif.bss_conf.use_short_slot)
1969 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
Emmanuel Grumbach785e21a2014-09-03 15:25:04 +03001970 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001971 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1972
1973 sinfo->sta_flags.set = 0;
1974 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1975 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1976 BIT(NL80211_STA_FLAG_WME) |
1977 BIT(NL80211_STA_FLAG_MFP) |
1978 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1979 BIT(NL80211_STA_FLAG_ASSOCIATED) |
1980 BIT(NL80211_STA_FLAG_TDLS_PEER);
1981 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1982 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1983 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1984 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
Johannes Berga74a8c82014-07-22 14:50:47 +02001985 if (sta->sta.wme)
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001986 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1987 if (test_sta_flag(sta, WLAN_STA_MFP))
1988 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1989 if (test_sta_flag(sta, WLAN_STA_AUTH))
1990 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
1991 if (test_sta_flag(sta, WLAN_STA_ASSOC))
1992 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1993 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1994 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
1995
1996 /* check if the driver has a SW RC implementation */
1997 if (ref && ref->ops->get_expected_throughput)
1998 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
1999 else
2000 thr = drv_get_expected_throughput(local, &sta->sta);
2001
2002 if (thr != 0) {
Johannes Berg319090b2014-11-17 14:08:11 +01002003 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002004 sinfo->expected_throughput = thr;
2005 }
2006}