blob: 12971b71d0fa1ea8ce69dbdc09314c1c1641d7c8 [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 Berg43ba7e92008-02-21 14:09:30 +0100160
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100161 return rhashtable_lookup_fast(&local->sta_hash, addr, sta_rht_params);
Johannes Berg43ba7e92008-02-21 14:09:30 +0100162}
163
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100164/*
165 * Get sta info either from the specified interface
166 * or from one of its vlans
167 */
168struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
169 const u8 *addr)
170{
171 struct ieee80211_local *local = sdata->local;
172 struct sta_info *sta;
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100173 struct rhash_head *tmp;
174 const struct bucket_table *tbl;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100175
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100176 rcu_read_lock();
177 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
178
179 for_each_sta_info(local, tbl, addr, sta, tmp) {
180 if (sta->sdata == sdata ||
181 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
182 rcu_read_unlock();
183 /* this is safe as the caller must already hold
184 * another rcu read section or the mutex
185 */
186 return sta;
187 }
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100188 }
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100189 rcu_read_unlock();
190 return NULL;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100191}
192
Johannes Berg3b53fde82009-11-16 12:00:37 +0100193struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
194 int idx)
Luis Carlos Coboee385852008-02-23 15:17:11 +0100195{
Johannes Berg3b53fde82009-11-16 12:00:37 +0100196 struct ieee80211_local *local = sdata->local;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100197 struct sta_info *sta;
198 int i = 0;
199
Johannes Bergd0709a62008-02-25 16:27:46 +0100200 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg3b53fde82009-11-16 12:00:37 +0100201 if (sdata != sta->sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800202 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100203 if (i < idx) {
204 ++i;
205 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100206 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800207 return sta;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100208 }
Luis Carlos Coboee385852008-02-23 15:17:11 +0100209
210 return NULL;
211}
Jiri Bencf0706e82007-05-05 11:45:53 -0700212
Johannes Berg93e5deb2008-04-01 15:21:00 +0200213/**
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100214 * sta_info_free - free STA
Johannes Berg93e5deb2008-04-01 15:21:00 +0200215 *
Randy Dunlap6ef307b2008-07-03 13:52:18 -0700216 * @local: pointer to the global information
Johannes Berg93e5deb2008-04-01 15:21:00 +0200217 * @sta: STA info to free
218 *
219 * This function must undo everything done by sta_info_alloc()
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100220 * that may happen before sta_info_insert(). It may only be
221 * called when sta_info_insert() has not been attempted (and
222 * if that fails, the station is freed anyway.)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200223 */
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100224void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200225{
Johannes Berg889cbb92012-01-17 10:33:29 +0100226 if (sta->rate_ctrl)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100227 rate_control_free_sta(sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200228
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200229 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200230
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100231 if (sta->sta.txq[0])
232 kfree(to_txq_info(sta->sta.txq[0]));
Felix Fietkau53d04522014-05-27 22:33:57 +0200233 kfree(rcu_dereference_raw(sta->sta.rates));
Johannes Berg93e5deb2008-04-01 15:21:00 +0200234 kfree(sta);
235}
236
Johannes Berg4d339602011-12-15 11:24:20 +0100237/* Caller must hold local->sta_mtx */
Johannes Bergd0709a62008-02-25 16:27:46 +0100238static void sta_info_hash_add(struct ieee80211_local *local,
239 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700240{
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100241 rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
242 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -0700243}
Jiri Bencf0706e82007-05-05 11:45:53 -0700244
Johannes Berg5ac2e352014-05-27 16:32:27 +0200245static void sta_deliver_ps_frames(struct work_struct *wk)
Johannes Bergaf818582009-11-06 11:35:50 +0100246{
247 struct sta_info *sta;
248
Johannes Berg5ac2e352014-05-27 16:32:27 +0200249 sta = container_of(wk, struct sta_info, drv_deliver_wk);
Johannes Bergaf818582009-11-06 11:35:50 +0100250
251 if (sta->dead)
252 return;
253
Johannes Berg5ac2e352014-05-27 16:32:27 +0200254 local_bh_disable();
255 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
Johannes Bergaf818582009-11-06 11:35:50 +0100256 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200257 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
Johannes Bergaf818582009-11-06 11:35:50 +0100258 ieee80211_sta_ps_deliver_poll_response(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200259 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
Johannes Berg47086fc2011-09-29 16:04:33 +0200260 ieee80211_sta_ps_deliver_uapsd(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200261 local_bh_enable();
Johannes Bergaf818582009-11-06 11:35:50 +0100262}
263
Johannes Bergaf65cd962009-11-17 18:18:36 +0100264static int sta_prepare_rate_control(struct ieee80211_local *local,
265 struct sta_info *sta, gfp_t gfp)
266{
267 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
268 return 0;
269
Johannes Berg889cbb92012-01-17 10:33:29 +0100270 sta->rate_ctrl = local->rate_ctrl;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100271 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
272 &sta->sta, gfp);
Johannes Berg889cbb92012-01-17 10:33:29 +0100273 if (!sta->rate_ctrl_priv)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100274 return -ENOMEM;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100275
276 return 0;
277}
278
Johannes Berg73651ee2008-02-25 16:27:47 +0100279struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
Johannes Berg56544162011-12-14 13:28:46 +0100280 const u8 *addr, gfp_t gfp)
Jiri Bencf0706e82007-05-05 11:45:53 -0700281{
Johannes Bergd0709a62008-02-25 16:27:46 +0100282 struct ieee80211_local *local = sdata->local;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100283 struct ieee80211_hw *hw = &local->hw;
Jiri Bencf0706e82007-05-05 11:45:53 -0700284 struct sta_info *sta;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +0530285 struct timespec uptime;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200286 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700287
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100288 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
Jiri Bencf0706e82007-05-05 11:45:53 -0700289 if (!sta)
Johannes Berg73651ee2008-02-25 16:27:47 +0100290 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700291
Johannes Berg07346f812008-05-03 01:02:02 +0200292 spin_lock_init(&sta->lock);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200293 spin_lock_init(&sta->ps_lock);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200294 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
Johannes Berg67c282c2010-06-10 10:21:43 +0200295 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
Johannes Berga93e3642010-06-10 10:21:46 +0200296 mutex_init(&sta->ampdu_mlme.mtx);
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800297#ifdef CONFIG_MAC80211_MESH
298 if (ieee80211_vif_is_mesh(&sdata->vif) &&
299 !sdata->u.mesh.user_mpm)
300 init_timer(&sta->plink_timer);
Thomas Pedersen6c7c4cb2013-06-20 23:50:59 -0700301 sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800302#endif
Johannes Berg07346f812008-05-03 01:02:02 +0200303
Johannes Berg17741cd2008-09-11 00:02:02 +0200304 memcpy(sta->sta.addr, addr, ETH_ALEN);
Johannes Bergd0709a62008-02-25 16:27:46 +0100305 sta->local = local;
306 sta->sdata = sdata;
Felix Fietkau8bc8aec2011-03-21 20:01:00 +0100307 sta->last_rx = jiffies;
Jiri Bencf0706e82007-05-05 11:45:53 -0700308
Johannes Berg71ec3752012-01-20 13:55:20 +0100309 sta->sta_state = IEEE80211_STA_NONE;
310
Liad Kaufmanb6da9112014-11-19 13:47:38 +0200311 /* Mark TID as unreserved */
312 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
313
Thomas Gleixner18171522014-06-11 23:59:14 +0000314 ktime_get_ts(&uptime);
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +0530315 sta->last_connected = uptime.tv_sec;
Bruno Randolf541a45a2010-12-02 19:12:43 +0900316 ewma_init(&sta->avg_signal, 1024, 8);
Felix Fietkauef0621e2013-04-22 16:29:31 +0200317 for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
318 ewma_init(&sta->chain_signal_avg[i], 1024, 8);
Bruno Randolf541a45a2010-12-02 19:12:43 +0900319
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100320 if (local->ops->wake_tx_queue) {
321 void *txq_data;
322 int size = sizeof(struct txq_info) +
323 ALIGN(hw->txq_data_size, sizeof(void *));
324
325 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
326 if (!txq_data)
327 goto free;
328
329 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
330 struct txq_info *txq = txq_data + i * size;
331
332 ieee80211_init_tx_queue(sdata, sta, txq, i);
333 }
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100334 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700335
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100336 if (sta_prepare_rate_control(local, sta, gfp))
337 goto free_txq;
338
Johannes Berg5a306f52012-11-14 23:22:21 +0100339 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berga622ab72010-06-10 10:21:39 +0200340 /*
341 * timer_to_tid must be initialized with identity mapping
342 * to enable session_timer's data differentiation. See
343 * sta_rx_agg_session_timer_expired for usage.
344 */
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200345 sta->timer_to_tid[i] = i;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200346 }
Johannes Berg948d8872011-09-29 16:04:29 +0200347 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
348 skb_queue_head_init(&sta->ps_tx_buf[i]);
349 skb_queue_head_init(&sta->tx_filtered[i]);
350 }
Johannes Berg73651ee2008-02-25 16:27:47 +0100351
Johannes Berg5a306f52012-11-14 23:22:21 +0100352 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700353 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
Senthil Balasubramaniancccaec92009-05-14 18:42:08 +0530354
Johannes Bergaf0ed692013-02-12 14:21:00 +0100355 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300356 if (sdata->vif.type == NL80211_IFTYPE_AP ||
357 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
358 struct ieee80211_supported_band *sband =
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100359 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300360 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
361 IEEE80211_HT_CAP_SM_PS_SHIFT;
362 /*
363 * Assume that hostapd advertises our caps in the beacon and
364 * this is the known_smps_mode for a station that just assciated
365 */
366 switch (smps) {
367 case WLAN_HT_SMPS_CONTROL_DISABLED:
368 sta->known_smps_mode = IEEE80211_SMPS_OFF;
369 break;
370 case WLAN_HT_SMPS_CONTROL_STATIC:
371 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
372 break;
373 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
374 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
375 break;
376 default:
377 WARN_ON(1);
378 }
379 }
Johannes Bergaf0ed692013-02-12 14:21:00 +0100380
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200381 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
Johannes Bergef04a292014-01-06 15:56:59 +0100382
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100383 return sta;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100384
385free_txq:
386 if (sta->sta.txq[0])
387 kfree(to_txq_info(sta->sta.txq[0]));
388free:
389 kfree(sta);
390 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +0100391}
392
Guy Eilam8c71df72011-08-17 15:18:14 +0300393static int sta_info_insert_check(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100394{
Johannes Berg34e89502010-02-03 13:59:58 +0100395 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg34e89502010-02-03 13:59:58 +0100396
Johannes Berg03e44972008-02-27 09:56:40 +0100397 /*
398 * Can't be a WARN_ON because it can be triggered through a race:
399 * something inserts a STA (on one CPU) without holding the RTNL
400 * and another CPU turns off the net device.
401 */
Guy Eilam8c71df72011-08-17 15:18:14 +0300402 if (unlikely(!ieee80211_sdata_running(sdata)))
403 return -ENETDOWN;
Johannes Berg03e44972008-02-27 09:56:40 +0100404
Joe Perchesb203ca32012-05-08 18:56:52 +0000405 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
Guy Eilam8c71df72011-08-17 15:18:14 +0300406 is_multicast_ether_addr(sta->sta.addr)))
407 return -EINVAL;
408
409 return 0;
410}
411
Johannes Bergf09603a2012-01-20 13:55:21 +0100412static int sta_info_insert_drv_state(struct ieee80211_local *local,
413 struct ieee80211_sub_if_data *sdata,
414 struct sta_info *sta)
415{
416 enum ieee80211_sta_state state;
417 int err = 0;
418
419 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
420 err = drv_sta_state(local, sdata, sta, state, state + 1);
421 if (err)
422 break;
423 }
424
425 if (!err) {
Johannes Berga4ec45a2012-01-20 13:55:22 +0100426 /*
427 * Drivers using legacy sta_add/sta_remove callbacks only
428 * get uploaded set to true after sta_add is called.
429 */
430 if (!local->ops->sta_add)
431 sta->uploaded = true;
Johannes Bergf09603a2012-01-20 13:55:21 +0100432 return 0;
433 }
434
435 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200436 sdata_info(sdata,
437 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
438 sta->sta.addr, state + 1, err);
Johannes Bergf09603a2012-01-20 13:55:21 +0100439 err = 0;
440 }
441
442 /* unwind on error */
443 for (; state > IEEE80211_STA_NOTEXIST; state--)
444 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
445
446 return err;
447}
448
Guy Eilam8c71df72011-08-17 15:18:14 +0300449/*
450 * should be called with sta_mtx locked
451 * this function replaces the mutex lock
452 * with a RCU lock
453 */
Johannes Berg4d339602011-12-15 11:24:20 +0100454static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
Guy Eilam8c71df72011-08-17 15:18:14 +0300455{
456 struct ieee80211_local *local = sta->local;
457 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg7852e362012-01-20 13:55:24 +0100458 struct station_info sinfo;
Guy Eilam8c71df72011-08-17 15:18:14 +0300459 int err = 0;
460
461 lockdep_assert_held(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +0100462
Johannes Berg7852e362012-01-20 13:55:24 +0100463 /* check if STA exists already */
464 if (sta_info_get_bss(sdata, sta->sta.addr)) {
465 err = -EEXIST;
466 goto out_err;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100467 }
Johannes Berg32bfd352007-12-19 01:31:26 +0100468
Johannes Berg7852e362012-01-20 13:55:24 +0100469 local->num_sta++;
470 local->sta_generation++;
471 smp_mb();
Johannes Berg4d339602011-12-15 11:24:20 +0100472
Johannes Berg5108ca82014-02-17 20:49:03 +0100473 /* simplify things and don't accept BA sessions yet */
474 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
475
Johannes Berg7852e362012-01-20 13:55:24 +0100476 /* make the station visible */
477 sta_info_hash_add(local, sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100478
Arik Nemtsov2bad77482014-10-22 12:32:16 +0300479 list_add_tail_rcu(&sta->list, &local->sta_list);
Johannes Berg83d5cc02012-01-12 09:31:10 +0100480
Johannes Berg5108ca82014-02-17 20:49:03 +0100481 /* notify driver */
482 err = sta_info_insert_drv_state(local, sdata, sta);
483 if (err)
484 goto out_remove;
485
Johannes Berg7852e362012-01-20 13:55:24 +0100486 set_sta_flag(sta, WLAN_STA_INSERTED);
Johannes Berg5108ca82014-02-17 20:49:03 +0100487 /* accept BA sessions now */
488 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg4d339602011-12-15 11:24:20 +0100489
Eliad Peller21f659b2013-11-11 20:14:01 +0200490 ieee80211_recalc_min_chandef(sdata);
Johannes Berg7852e362012-01-20 13:55:24 +0100491 ieee80211_sta_debugfs_add(sta);
492 rate_control_add_sta_debugfs(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100493
Johannes Berg7852e362012-01-20 13:55:24 +0100494 memset(&sinfo, 0, sizeof(sinfo));
495 sinfo.filled = 0;
496 sinfo.generation = local->sta_generation;
497 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
Johannes Bergd0709a62008-02-25 16:27:46 +0100498
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200499 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700500
Johannes Berg34e89502010-02-03 13:59:58 +0100501 /* move reference to rcu-protected */
502 rcu_read_lock();
503 mutex_unlock(&local->sta_mtx);
Jiri Bence9f207f2007-05-05 11:46:38 -0700504
Johannes Berg73651ee2008-02-25 16:27:47 +0100505 if (ieee80211_vif_is_mesh(&sdata->vif))
506 mesh_accept_plinks_update(sdata);
507
508 return 0;
Johannes Berg5108ca82014-02-17 20:49:03 +0100509 out_remove:
510 sta_info_hash_del(local, sta);
511 list_del_rcu(&sta->list);
512 local->num_sta--;
513 synchronize_net();
514 __cleanup_single_sta(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100515 out_err:
516 mutex_unlock(&local->sta_mtx);
517 rcu_read_lock();
518 return err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300519}
520
521int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
522{
523 struct ieee80211_local *local = sta->local;
Zhao, Gang308f7fc2014-04-21 12:53:00 +0800524 int err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300525
Johannes Berg4d339602011-12-15 11:24:20 +0100526 might_sleep();
527
Guy Eilam8c71df72011-08-17 15:18:14 +0300528 err = sta_info_insert_check(sta);
529 if (err) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700530 rcu_read_lock();
531 goto out_free;
532 }
533
Johannes Berg004c8722008-02-20 11:21:35 +0100534 mutex_lock(&local->sta_mtx);
535
Johannes Berg4d339602011-12-15 11:24:20 +0100536 err = sta_info_insert_finish(sta);
Guy Eilam8c71df72011-08-17 15:18:14 +0300537 if (err)
Johannes Berg004c8722008-02-20 11:21:35 +0100538 goto out_free;
Johannes Bergd0709a62008-02-25 16:27:46 +0100539
Johannes Berg004c8722008-02-20 11:21:35 +0100540 return 0;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200541 out_free:
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100542 sta_info_free(local, sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200543 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -0700544}
545
Johannes Berg34e89502010-02-03 13:59:58 +0100546int sta_info_insert(struct sta_info *sta)
547{
548 int err = sta_info_insert_rcu(sta);
549
550 rcu_read_unlock();
551
552 return err;
553}
554
Marco Porschd012a602012-10-10 12:39:50 -0700555static inline void __bss_tim_set(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100556{
557 /*
558 * This format has been mandated by the IEEE specifications,
559 * so this line may not be changed to use the __set_bit() format.
560 */
Marco Porschd012a602012-10-10 12:39:50 -0700561 tim[id / 8] |= (1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100562}
563
Marco Porschd012a602012-10-10 12:39:50 -0700564static inline void __bss_tim_clear(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100565{
566 /*
567 * This format has been mandated by the IEEE specifications,
568 * so this line may not be changed to use the __clear_bit() format.
569 */
Marco Porschd012a602012-10-10 12:39:50 -0700570 tim[id / 8] &= ~(1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100571}
572
Ilan Peer3d5839b2013-03-05 15:27:20 +0200573static inline bool __bss_tim_get(u8 *tim, u16 id)
574{
575 /*
576 * This format has been mandated by the IEEE specifications,
577 * so this line may not be changed to use the test_bit() format.
578 */
579 return tim[id / 8] & (1 << (id % 8));
580}
581
Johannes Berg948d8872011-09-29 16:04:29 +0200582static unsigned long ieee80211_tids_for_ac(int ac)
Johannes Berg004c8722008-02-20 11:21:35 +0100583{
Johannes Berg948d8872011-09-29 16:04:29 +0200584 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
585 switch (ac) {
586 case IEEE80211_AC_VO:
587 return BIT(6) | BIT(7);
588 case IEEE80211_AC_VI:
589 return BIT(4) | BIT(5);
590 case IEEE80211_AC_BE:
591 return BIT(0) | BIT(3);
592 case IEEE80211_AC_BK:
593 return BIT(1) | BIT(2);
594 default:
595 WARN_ON(1);
596 return 0;
Johannes Bergd0709a62008-02-25 16:27:46 +0100597 }
Johannes Berg004c8722008-02-20 11:21:35 +0100598}
599
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100600static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
Johannes Berg004c8722008-02-20 11:21:35 +0100601{
Johannes Bergc868cb352011-09-29 16:04:27 +0200602 struct ieee80211_local *local = sta->local;
Marco Porschd012a602012-10-10 12:39:50 -0700603 struct ps_data *ps;
Johannes Berg948d8872011-09-29 16:04:29 +0200604 bool indicate_tim = false;
605 u8 ignore_for_tim = sta->sta.uapsd_queues;
606 int ac;
Marco Porschd012a602012-10-10 12:39:50 -0700607 u16 id;
Johannes Berg004c8722008-02-20 11:21:35 +0100608
Marco Porschd012a602012-10-10 12:39:50 -0700609 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
610 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
611 if (WARN_ON_ONCE(!sta->sdata->bss))
612 return;
613
614 ps = &sta->sdata->bss->ps;
615 id = sta->sta.aid;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100616#ifdef CONFIG_MAC80211_MESH
617 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
618 ps = &sta->sdata->u.mesh.ps;
Thomas Pedersen204d1302013-11-05 11:17:05 -0800619 /* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
Chun-Yeow Yeoh6f101ef2013-11-13 15:43:03 +0800620 id = sta->plid % (IEEE80211_MAX_AID + 1);
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100621#endif
Marco Porschd012a602012-10-10 12:39:50 -0700622 } else {
Johannes Bergc868cb352011-09-29 16:04:27 +0200623 return;
Marco Porschd012a602012-10-10 12:39:50 -0700624 }
Johannes Berg3e122be2008-07-09 14:40:34 +0200625
Johannes Bergc868cb352011-09-29 16:04:27 +0200626 /* No need to do anything if the driver does all */
627 if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
628 return;
Johannes Berg004c8722008-02-20 11:21:35 +0100629
Johannes Bergc868cb352011-09-29 16:04:27 +0200630 if (sta->dead)
631 goto done;
Johannes Berg3e122be2008-07-09 14:40:34 +0200632
Johannes Berg948d8872011-09-29 16:04:29 +0200633 /*
634 * If all ACs are delivery-enabled then we should build
635 * the TIM bit for all ACs anyway; if only some are then
636 * we ignore those and build the TIM bit using only the
637 * non-enabled ones.
638 */
639 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
640 ignore_for_tim = 0;
Johannes Berg3e122be2008-07-09 14:40:34 +0200641
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100642 if (ignore_pending)
643 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
644
Johannes Berg948d8872011-09-29 16:04:29 +0200645 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
646 unsigned long tids;
647
648 if (ignore_for_tim & BIT(ac))
649 continue;
650
651 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
652 !skb_queue_empty(&sta->ps_tx_buf[ac]);
653 if (indicate_tim)
654 break;
655
656 tids = ieee80211_tids_for_ac(ac);
657
658 indicate_tim |=
659 sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100660 indicate_tim |=
661 sta->txq_buffered_tids & tids;
Johannes Bergd0709a62008-02-25 16:27:46 +0100662 }
Johannes Berg004c8722008-02-20 11:21:35 +0100663
Johannes Bergc868cb352011-09-29 16:04:27 +0200664 done:
Johannes Berg65f704a2013-02-13 17:39:53 +0100665 spin_lock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100666
Ilan Peer3d5839b2013-03-05 15:27:20 +0200667 if (indicate_tim == __bss_tim_get(ps->tim, id))
668 goto out_unlock;
669
Johannes Berg948d8872011-09-29 16:04:29 +0200670 if (indicate_tim)
Marco Porschd012a602012-10-10 12:39:50 -0700671 __bss_tim_set(ps->tim, id);
Johannes Bergc868cb352011-09-29 16:04:27 +0200672 else
Marco Porschd012a602012-10-10 12:39:50 -0700673 __bss_tim_clear(ps->tim, id);
Johannes Berg3e122be2008-07-09 14:40:34 +0200674
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100675 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
Johannes Bergc868cb352011-09-29 16:04:27 +0200676 local->tim_in_locked_section = true;
Johannes Berg948d8872011-09-29 16:04:29 +0200677 drv_set_tim(local, &sta->sta, indicate_tim);
Johannes Bergc868cb352011-09-29 16:04:27 +0200678 local->tim_in_locked_section = false;
Johannes Berg004c8722008-02-20 11:21:35 +0100679 }
Johannes Berg004c8722008-02-20 11:21:35 +0100680
Ilan Peer3d5839b2013-03-05 15:27:20 +0200681out_unlock:
Johannes Berg65f704a2013-02-13 17:39:53 +0100682 spin_unlock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100683}
684
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100685void sta_info_recalc_tim(struct sta_info *sta)
686{
687 __sta_info_recalc_tim(sta, false);
688}
689
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200690static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700691{
Johannes Berge039fa42008-05-15 12:55:29 +0200692 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700693 int timeout;
694
695 if (!skb)
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200696 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700697
Johannes Berge039fa42008-05-15 12:55:29 +0200698 info = IEEE80211_SKB_CB(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700699
700 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200701 timeout = (sta->listen_interval *
702 sta->sdata->vif.bss_conf.beacon_int *
703 32 / 15625) * HZ;
Jiri Bencf0706e82007-05-05 11:45:53 -0700704 if (timeout < STA_TX_BUFFER_EXPIRE)
705 timeout = STA_TX_BUFFER_EXPIRE;
Johannes Berge039fa42008-05-15 12:55:29 +0200706 return time_after(jiffies, info->control.jiffies + timeout);
Jiri Bencf0706e82007-05-05 11:45:53 -0700707}
708
709
Johannes Berg948d8872011-09-29 16:04:29 +0200710static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
711 struct sta_info *sta, int ac)
Jiri Bencf0706e82007-05-05 11:45:53 -0700712{
713 unsigned long flags;
714 struct sk_buff *skb;
715
Johannes Berg60750392011-09-29 16:04:28 +0200716 /*
717 * First check for frames that should expire on the filtered
718 * queue. Frames here were rejected by the driver and are on
719 * a separate queue to avoid reordering with normal PS-buffered
720 * frames. They also aren't accounted for right now in the
721 * total_ps_buffered counter.
722 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700723 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200724 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
725 skb = skb_peek(&sta->tx_filtered[ac]);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200726 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200727 skb = __skb_dequeue(&sta->tx_filtered[ac]);
Johannes Berg836341a2008-02-20 02:07:21 +0100728 else
Jiri Bencf0706e82007-05-05 11:45:53 -0700729 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200730 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700731
Johannes Berg60750392011-09-29 16:04:28 +0200732 /*
733 * Frames are queued in order, so if this one
734 * hasn't expired yet we can stop testing. If
735 * we actually reached the end of the queue we
736 * also need to stop, of course.
737 */
738 if (!skb)
739 break;
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200740 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg60750392011-09-29 16:04:28 +0200741 }
742
743 /*
744 * Now also check the normal PS-buffered queue, this will
745 * only find something if the filtered queue was emptied
746 * since the filtered frames are all before the normal PS
747 * buffered frames.
748 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700749 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200750 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
751 skb = skb_peek(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700752 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200753 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700754 else
755 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200756 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700757
Johannes Berg60750392011-09-29 16:04:28 +0200758 /*
759 * frames are queued in order, so if this one
760 * hasn't expired yet (or we reached the end of
761 * the queue) we can stop testing
762 */
Johannes Berg836341a2008-02-20 02:07:21 +0100763 if (!skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700764 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100765
Johannes Berg836341a2008-02-20 02:07:21 +0100766 local->total_ps_buffered--;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200767 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
768 sta->sta.addr);
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200769 ieee80211_free_txskb(&local->hw, skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700770 }
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300771
Johannes Berg60750392011-09-29 16:04:28 +0200772 /*
773 * Finally, recalculate the TIM bit for this station -- it might
774 * now be clear because the station was too slow to retrieve its
775 * frames.
776 */
777 sta_info_recalc_tim(sta);
778
779 /*
780 * Return whether there are any frames still buffered, this is
781 * used to check whether the cleanup timer still needs to run,
782 * if there are no frames we don't need to rearm the timer.
783 */
Johannes Berg948d8872011-09-29 16:04:29 +0200784 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
785 skb_queue_empty(&sta->tx_filtered[ac]));
786}
787
788static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
789 struct sta_info *sta)
790{
791 bool have_buffered = false;
792 int ac;
793
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100794 /* This is only necessary for stations on BSS/MBSS interfaces */
795 if (!sta->sdata->bss &&
796 !ieee80211_vif_is_mesh(&sta->sdata->vif))
Johannes Berg948d8872011-09-29 16:04:29 +0200797 return false;
798
799 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
800 have_buffered |=
801 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
802
803 return have_buffered;
Jiri Bencf0706e82007-05-05 11:45:53 -0700804}
805
Johannes Bergd7782072013-12-04 23:12:31 +0100806static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100807{
808 struct ieee80211_local *local;
809 struct ieee80211_sub_if_data *sdata;
Johannes Berg6d10e462013-03-06 23:09:11 +0100810 int ret;
Johannes Berg34e89502010-02-03 13:59:58 +0100811
812 might_sleep();
813
814 if (!sta)
815 return -ENOENT;
816
817 local = sta->local;
818 sdata = sta->sdata;
819
Johannes Berg83d5cc02012-01-12 09:31:10 +0100820 lockdep_assert_held(&local->sta_mtx);
821
Johannes Berg098a6072010-04-06 11:18:47 +0200822 /*
823 * Before removing the station from the driver and
824 * rate control, it might still start new aggregation
825 * sessions -- block that to make sure the tear-down
826 * will be sufficient.
827 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200828 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Bergc82c4a82012-07-18 13:31:31 +0200829 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
Johannes Berg098a6072010-04-06 11:18:47 +0200830
Johannes Berg34e89502010-02-03 13:59:58 +0100831 ret = sta_info_hash_del(local, sta);
Johannes Bergb01711b2013-12-04 20:25:27 +0100832 if (WARN_ON(ret))
Johannes Berg34e89502010-02-03 13:59:58 +0100833 return ret;
834
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200835 /*
836 * for TDLS peers, make sure to return to the base channel before
837 * removal.
838 */
839 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
840 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
841 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
842 }
843
Arik Nemtsov794454c2012-06-03 23:32:32 +0300844 list_del_rcu(&sta->list);
Johannes Berg4d339602011-12-15 11:24:20 +0100845
Johannes Berg6a9d1b92013-12-04 22:39:17 +0100846 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
847
Johannes Berga710c812013-12-04 20:11:06 +0100848 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
849 rcu_access_pointer(sdata->u.vlan.sta) == sta)
850 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
851
Johannes Bergd7782072013-12-04 23:12:31 +0100852 return 0;
853}
854
855static void __sta_info_destroy_part2(struct sta_info *sta)
856{
857 struct ieee80211_local *local = sta->local;
858 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg6f7a8d22014-11-14 20:32:57 +0100859 struct station_info sinfo = {};
Johannes Bergd7782072013-12-04 23:12:31 +0100860 int ret;
861
862 /*
863 * NOTE: This assumes at least synchronize_net() was done
864 * after _part1 and before _part2!
865 */
866
867 might_sleep();
868 lockdep_assert_held(&local->sta_mtx);
869
Johannes Bergc8782072013-12-04 23:05:45 +0100870 /* now keys can no longer be reached */
Johannes Berg6d10e462013-03-06 23:09:11 +0100871 ieee80211_free_sta_keys(local, sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100872
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100873 /* disable TIM bit - last chance to tell driver */
874 __sta_info_recalc_tim(sta, true);
875
Johannes Berg34e89502010-02-03 13:59:58 +0100876 sta->dead = true;
877
Johannes Berg34e89502010-02-03 13:59:58 +0100878 local->num_sta--;
879 local->sta_generation++;
880
Johannes Berg83d5cc02012-01-12 09:31:10 +0100881 while (sta->sta_state > IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100882 ret = sta_info_move_state(sta, sta->sta_state - 1);
883 if (ret) {
Johannes Berg83d5cc02012-01-12 09:31:10 +0100884 WARN_ON_ONCE(1);
885 break;
886 }
887 }
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100888
Johannes Bergf09603a2012-01-20 13:55:21 +0100889 if (sta->uploaded) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100890 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
891 IEEE80211_STA_NOTEXIST);
892 WARN_ON_ONCE(ret != 0);
893 }
Johannes Berg34e89502010-02-03 13:59:58 +0100894
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200895 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
896
Johannes Berg6f7a8d22014-11-14 20:32:57 +0100897 sta_set_sinfo(sta, &sinfo);
898 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
Jouni Malinenec15e682011-03-23 15:29:52 +0200899
Johannes Berg34e89502010-02-03 13:59:58 +0100900 rate_control_remove_sta_debugfs(sta);
901 ieee80211_sta_debugfs_remove(sta);
Eliad Peller21f659b2013-11-11 20:14:01 +0200902 ieee80211_recalc_min_chandef(sdata);
Johannes Berg34e89502010-02-03 13:59:58 +0100903
Johannes Bergd34ba212013-12-04 22:46:11 +0100904 cleanup_single_sta(sta);
Johannes Bergd7782072013-12-04 23:12:31 +0100905}
906
907int __must_check __sta_info_destroy(struct sta_info *sta)
908{
909 int err = __sta_info_destroy_part1(sta);
910
911 if (err)
912 return err;
913
914 synchronize_net();
915
916 __sta_info_destroy_part2(sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100917
918 return 0;
919}
920
921int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
922{
923 struct sta_info *sta;
924 int ret;
925
926 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100927 sta = sta_info_get(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100928 ret = __sta_info_destroy(sta);
929 mutex_unlock(&sdata->local->sta_mtx);
930
931 return ret;
932}
933
934int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
935 const u8 *addr)
936{
937 struct sta_info *sta;
938 int ret;
939
940 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100941 sta = sta_info_get_bss(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100942 ret = __sta_info_destroy(sta);
943 mutex_unlock(&sdata->local->sta_mtx);
944
945 return ret;
946}
Jiri Bencf0706e82007-05-05 11:45:53 -0700947
948static void sta_info_cleanup(unsigned long data)
949{
950 struct ieee80211_local *local = (struct ieee80211_local *) data;
951 struct sta_info *sta;
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300952 bool timer_needed = false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700953
Johannes Bergd0709a62008-02-25 16:27:46 +0100954 rcu_read_lock();
955 list_for_each_entry_rcu(sta, &local->sta_list, list)
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300956 if (sta_info_cleanup_expire_buffered(local, sta))
957 timer_needed = true;
Johannes Bergd0709a62008-02-25 16:27:46 +0100958 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -0700959
Johannes Berg5bb644a2009-05-17 11:40:42 +0200960 if (local->quiescing)
961 return;
962
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300963 if (!timer_needed)
964 return;
965
Johannes Berg26d59532011-04-01 13:52:48 +0200966 mod_timer(&local->sta_cleanup,
967 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
Jiri Bencf0706e82007-05-05 11:45:53 -0700968}
969
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100970u32 sta_addr_hash(const void *key, u32 length, u32 seed)
Jiri Bencf0706e82007-05-05 11:45:53 -0700971{
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100972 return jhash(key, ETH_ALEN, seed);
973}
974
975int sta_info_init(struct ieee80211_local *local)
976{
977 int err;
978
979 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
980 if (err)
981 return err;
982
Johannes Berg4d339602011-12-15 11:24:20 +0100983 spin_lock_init(&local->tim_lock);
Johannes Berg34e89502010-02-03 13:59:58 +0100984 mutex_init(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700985 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -0700986
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800987 setup_timer(&local->sta_cleanup, sta_info_cleanup,
988 (unsigned long)local);
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100989 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700990}
991
992void sta_info_stop(struct ieee80211_local *local)
993{
Johannes Berga56f9922012-12-13 23:08:52 +0100994 del_timer_sync(&local->sta_cleanup);
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100995 rhashtable_destroy(&local->sta_hash);
Jiri Bencf0706e82007-05-05 11:45:53 -0700996}
997
Johannes Berg051007d2012-12-13 23:49:02 +0100998
Johannes Berge7162512013-12-04 23:18:37 +0100999int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
Jiri Bencf0706e82007-05-05 11:45:53 -07001000{
Johannes Bergb998e8b2012-12-13 23:07:46 +01001001 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001002 struct sta_info *sta, *tmp;
Johannes Bergd7782072013-12-04 23:12:31 +01001003 LIST_HEAD(free_list);
Johannes Berg44213b52008-02-25 16:27:49 +01001004 int ret = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001005
Johannes Bergd0709a62008-02-25 16:27:46 +01001006 might_sleep();
1007
Johannes Berge7162512013-12-04 23:18:37 +01001008 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1009 WARN_ON(vlans && !sdata->bss);
1010
Johannes Berg34e89502010-02-03 13:59:58 +01001011 mutex_lock(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +01001012 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Johannes Berge7162512013-12-04 23:18:37 +01001013 if (sdata == sta->sdata ||
1014 (vlans && sdata->bss == sta->sdata->bss)) {
Johannes Bergd7782072013-12-04 23:12:31 +01001015 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1016 list_add(&sta->free_list, &free_list);
Johannes Berg34316832012-02-25 21:40:46 +01001017 ret++;
1018 }
Johannes Berg34e89502010-02-03 13:59:58 +01001019 }
Johannes Bergd7782072013-12-04 23:12:31 +01001020
1021 if (!list_empty(&free_list)) {
1022 synchronize_net();
1023 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1024 __sta_info_destroy_part2(sta);
1025 }
Johannes Berg34e89502010-02-03 13:59:58 +01001026 mutex_unlock(&local->sta_mtx);
Johannes Berg44213b52008-02-25 16:27:49 +01001027
Johannes Berg051007d2012-12-13 23:49:02 +01001028 return ret;
1029}
1030
Johannes Berg24723d12008-09-11 00:01:46 +02001031void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1032 unsigned long exp_time)
1033{
1034 struct ieee80211_local *local = sdata->local;
1035 struct sta_info *sta, *tmp;
Johannes Berg24723d12008-09-11 00:01:46 +02001036
Johannes Berg34e89502010-02-03 13:59:58 +01001037 mutex_lock(&local->sta_mtx);
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301038
1039 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Marek Lindnerec2b7742011-12-20 23:16:52 +08001040 if (sdata != sta->sdata)
1041 continue;
1042
Johannes Berg24723d12008-09-11 00:01:46 +02001043 if (time_after(jiffies, sta->last_rx + exp_time)) {
Mohammed Shafi Shajakhaneea57d42012-10-08 21:33:47 +05301044 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1045 sta->sta.addr);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001046
1047 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1048 test_sta_flag(sta, WLAN_STA_PS_STA))
1049 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1050
Johannes Berg34e89502010-02-03 13:59:58 +01001051 WARN_ON(__sta_info_destroy(sta));
Johannes Berg24723d12008-09-11 00:01:46 +02001052 }
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301053 }
1054
Johannes Berg34e89502010-02-03 13:59:58 +01001055 mutex_unlock(&local->sta_mtx);
Johannes Berg24723d12008-09-11 00:01:46 +02001056}
Johannes Berg17741cd2008-09-11 00:02:02 +02001057
Ben Greear686b9cb2010-09-23 09:44:36 -07001058struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001059 const u8 *addr,
1060 const u8 *localaddr)
Johannes Berg17741cd2008-09-11 00:02:02 +02001061{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001062 struct ieee80211_local *local = hw_to_local(hw);
1063 struct sta_info *sta;
1064 struct rhash_head *tmp;
1065 const struct bucket_table *tbl;
1066
1067 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
Johannes Berg17741cd2008-09-11 00:02:02 +02001068
Ben Greear686b9cb2010-09-23 09:44:36 -07001069 /*
1070 * Just return a random station if localaddr is NULL
1071 * ... first in list.
1072 */
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001073 for_each_sta_info(local, tbl, addr, sta, tmp) {
Ben Greear686b9cb2010-09-23 09:44:36 -07001074 if (localaddr &&
Joe Perchesb203ca32012-05-08 18:56:52 +00001075 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
Ben Greear686b9cb2010-09-23 09:44:36 -07001076 continue;
Johannes Bergf7c65592010-04-30 13:48:36 +02001077 if (!sta->uploaded)
1078 return NULL;
Johannes Bergabe60632009-11-25 17:46:18 +01001079 return &sta->sta;
Johannes Bergf7c65592010-04-30 13:48:36 +02001080 }
1081
Johannes Bergabe60632009-11-25 17:46:18 +01001082 return NULL;
Johannes Berg17741cd2008-09-11 00:02:02 +02001083}
Ben Greear686b9cb2010-09-23 09:44:36 -07001084EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
Johannes Berg5ed176e2009-11-04 14:42:28 +01001085
1086struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1087 const u8 *addr)
1088{
Johannes Bergf7c65592010-04-30 13:48:36 +02001089 struct sta_info *sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001090
1091 if (!vif)
1092 return NULL;
1093
Johannes Bergf7c65592010-04-30 13:48:36 +02001094 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1095 if (!sta)
1096 return NULL;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001097
Johannes Bergf7c65592010-04-30 13:48:36 +02001098 if (!sta->uploaded)
1099 return NULL;
1100
1101 return &sta->sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001102}
Johannes Berg17741cd2008-09-11 00:02:02 +02001103EXPORT_SYMBOL(ieee80211_find_sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001104
Johannes Berge3685e02014-02-20 11:19:58 +01001105/* powersave support code */
1106void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
Johannes Berg50a94322010-11-16 11:50:28 -08001107{
Helmut Schaa608383b2012-01-30 15:18:00 +01001108 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berge3685e02014-02-20 11:19:58 +01001109 struct ieee80211_local *local = sdata->local;
1110 struct sk_buff_head pending;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001111 int filtered = 0, buffered = 0, ac, i;
Johannes Berge3685e02014-02-20 11:19:58 +01001112 unsigned long flags;
Marco Porschd012a602012-10-10 12:39:50 -07001113 struct ps_data *ps;
1114
Felix Fietkau3918edb2014-07-25 16:20:23 +02001115 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1116 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1117 u.ap);
1118
1119 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porschd012a602012-10-10 12:39:50 -07001120 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001121 else if (ieee80211_vif_is_mesh(&sdata->vif))
1122 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07001123 else
1124 return;
Johannes Berg50a94322010-11-16 11:50:28 -08001125
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001126 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg47086fc2011-09-29 16:04:33 +02001127
Johannes Berg5a306f52012-11-14 23:22:21 +01001128 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
Johannes Berg948d8872011-09-29 16:04:29 +02001129 sta->driver_buffered_tids = 0;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001130 sta->txq_buffered_tids = 0;
Johannes Berg948d8872011-09-29 16:04:29 +02001131
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001132 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1133 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001134
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001135 if (sta->sta.txq[0]) {
1136 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1137 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1138
1139 if (!skb_queue_len(&txqi->queue))
1140 continue;
1141
1142 drv_wake_tx_queue(local, txqi);
1143 }
1144 }
1145
Johannes Berg948d8872011-09-29 16:04:29 +02001146 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001147
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001148 /* sync with ieee80211_tx_h_unicast_ps_buf */
1149 spin_lock(&sta->ps_lock);
Johannes Bergaf818582009-11-06 11:35:50 +01001150 /* Send all buffered frames to the station */
Johannes Berg948d8872011-09-29 16:04:29 +02001151 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1152 int count = skb_queue_len(&pending), tmp;
1153
Arik Nemtsov987c2852012-11-05 10:27:52 +02001154 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001155 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001156 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001157 tmp = skb_queue_len(&pending);
1158 filtered += tmp - count;
1159 count = tmp;
1160
Arik Nemtsov987c2852012-11-05 10:27:52 +02001161 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001162 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001163 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001164 tmp = skb_queue_len(&pending);
1165 buffered += tmp - count;
1166 }
1167
Johannes Berge3685e02014-02-20 11:19:58 +01001168 ieee80211_add_pending_skbs(local, &pending);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001169
1170 /* now we're no longer in the deliver code */
1171 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1172
1173 /* The station might have polled and then woken up before we responded,
1174 * so clear these flags now to avoid them sticking around.
1175 */
1176 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1177 clear_sta_flag(sta, WLAN_STA_UAPSD);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001178 spin_unlock(&sta->ps_lock);
Johannes Berg948d8872011-09-29 16:04:29 +02001179
Johannes Berge3685e02014-02-20 11:19:58 +01001180 atomic_dec(&ps->num_sta_ps);
1181
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001182 /* This station just woke up and isn't aware of our SMPS state */
Chun-Yeow Yeoh062f1d62014-04-22 18:19:25 +08001183 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1184 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001185 sdata->smps_mode) &&
1186 sta->known_smps_mode != sdata->bss->req_smps &&
1187 sta_info_tx_streams(sta) != 1) {
1188 ht_dbg(sdata,
1189 "%pM just woke up and MIMO capable - update SMPS\n",
1190 sta->sta.addr);
1191 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1192 sta->sta.addr,
1193 sdata->vif.bss_conf.bssid);
1194 }
1195
Johannes Bergaf818582009-11-06 11:35:50 +01001196 local->total_ps_buffered -= buffered;
1197
Johannes Bergc868cb352011-09-29 16:04:27 +02001198 sta_info_recalc_tim(sta);
1199
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001200 ps_dbg(sdata,
1201 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1202 sta->sta.addr, sta->sta.aid, filtered, buffered);
Johannes Bergaf818582009-11-06 11:35:50 +01001203}
1204
Johannes Bergce662b442011-09-29 16:04:34 +02001205static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1206 struct sta_info *sta, int tid,
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001207 enum ieee80211_frame_release_type reason,
1208 bool call_driver)
Johannes Bergce662b442011-09-29 16:04:34 +02001209{
1210 struct ieee80211_local *local = sdata->local;
1211 struct ieee80211_qos_hdr *nullfunc;
1212 struct sk_buff *skb;
1213 int size = sizeof(*nullfunc);
1214 __le16 fc;
Johannes Berga74a8c82014-07-22 14:50:47 +02001215 bool qos = sta->sta.wme;
Johannes Bergce662b442011-09-29 16:04:34 +02001216 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02001217 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergce662b442011-09-29 16:04:34 +02001218
1219 if (qos) {
1220 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1221 IEEE80211_STYPE_QOS_NULLFUNC |
1222 IEEE80211_FCTL_FROMDS);
1223 } else {
1224 size -= 2;
1225 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1226 IEEE80211_STYPE_NULLFUNC |
1227 IEEE80211_FCTL_FROMDS);
1228 }
1229
1230 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1231 if (!skb)
1232 return;
1233
1234 skb_reserve(skb, local->hw.extra_tx_headroom);
1235
1236 nullfunc = (void *) skb_put(skb, size);
1237 nullfunc->frame_control = fc;
1238 nullfunc->duration_id = 0;
1239 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1240 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1241 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
Johannes Berg864a6042014-03-04 13:46:53 +01001242 nullfunc->seq_ctrl = 0;
Johannes Bergce662b442011-09-29 16:04:34 +02001243
Johannes Berg59b66252011-10-13 13:19:19 +02001244 skb->priority = tid;
1245 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
Johannes Bergce662b442011-09-29 16:04:34 +02001246 if (qos) {
Johannes Bergce662b442011-09-29 16:04:34 +02001247 nullfunc->qos_ctrl = cpu_to_le16(tid);
1248
Johannes Berg40b96402011-09-29 16:04:38 +02001249 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
Johannes Bergce662b442011-09-29 16:04:34 +02001250 nullfunc->qos_ctrl |=
1251 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1252 }
1253
1254 info = IEEE80211_SKB_CB(skb);
1255
1256 /*
1257 * Tell TX path to send this frame even though the
1258 * STA may still remain is PS mode after this frame
Johannes Bergdeeaee12011-09-29 16:04:35 +02001259 * exchange. Also set EOSP to indicate this packet
1260 * ends the poll/service period.
Johannes Bergce662b442011-09-29 16:04:34 +02001261 */
Johannes Berg02f2f1a2012-02-27 12:18:30 +01001262 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
Johannes Bergdeeaee12011-09-29 16:04:35 +02001263 IEEE80211_TX_STATUS_EOSP |
1264 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergce662b442011-09-29 16:04:34 +02001265
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301266 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1267
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001268 if (call_driver)
1269 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1270 reason, false);
Johannes Berg40b96402011-09-29 16:04:38 +02001271
Johannes Berg89afe612013-02-13 15:39:57 +01001272 skb->dev = sdata->dev;
1273
Johannes Berg55de9082012-07-26 17:24:39 +02001274 rcu_read_lock();
1275 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1276 if (WARN_ON(!chanctx_conf)) {
1277 rcu_read_unlock();
1278 kfree_skb(skb);
1279 return;
1280 }
1281
Johannes Berg73c4e192014-11-09 18:50:09 +02001282 info->band = chanctx_conf->def.chan->band;
Johannes Berg7c107702015-03-20 14:18:27 +01001283 ieee80211_xmit(sdata, sta, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001284 rcu_read_unlock();
Johannes Bergce662b442011-09-29 16:04:34 +02001285}
1286
Johannes Berg0a1cb802014-01-09 11:05:31 +01001287static int find_highest_prio_tid(unsigned long tids)
1288{
1289 /* lower 3 TIDs aren't ordered perfectly */
1290 if (tids & 0xF8)
1291 return fls(tids) - 1;
1292 /* TID 0 is BE just like TID 3 */
1293 if (tids & BIT(0))
1294 return 0;
1295 return fls(tids) - 1;
1296}
1297
Johannes Berg47086fc2011-09-29 16:04:33 +02001298static void
1299ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1300 int n_frames, u8 ignored_acs,
1301 enum ieee80211_frame_release_type reason)
Johannes Bergaf818582009-11-06 11:35:50 +01001302{
1303 struct ieee80211_sub_if_data *sdata = sta->sdata;
1304 struct ieee80211_local *local = sdata->local;
Johannes Berg948d8872011-09-29 16:04:29 +02001305 bool more_data = false;
1306 int ac;
Johannes Berg4049e092011-09-29 16:04:32 +02001307 unsigned long driver_release_tids = 0;
Johannes Berg47086fc2011-09-29 16:04:33 +02001308 struct sk_buff_head frames;
1309
Johannes Bergdeeaee12011-09-29 16:04:35 +02001310 /* Service or PS-Poll period starts */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001311 set_sta_flag(sta, WLAN_STA_SP);
Johannes Bergdeeaee12011-09-29 16:04:35 +02001312
Johannes Berg47086fc2011-09-29 16:04:33 +02001313 __skb_queue_head_init(&frames);
Johannes Bergaf818582009-11-06 11:35:50 +01001314
Johannes Bergf9f760b2014-01-08 17:45:07 +01001315 /* Get response frame(s) and more data bit for the last one. */
Johannes Berg948d8872011-09-29 16:04:29 +02001316 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg4049e092011-09-29 16:04:32 +02001317 unsigned long tids;
1318
Johannes Berg47086fc2011-09-29 16:04:33 +02001319 if (ignored_acs & BIT(ac))
Johannes Berg948d8872011-09-29 16:04:29 +02001320 continue;
1321
Johannes Berg4049e092011-09-29 16:04:32 +02001322 tids = ieee80211_tids_for_ac(ac);
1323
Johannes Bergf9f760b2014-01-08 17:45:07 +01001324 /* if we already have frames from software, then we can't also
1325 * release from hardware queues
1326 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001327 if (skb_queue_empty(&frames)) {
Johannes Bergf9f760b2014-01-08 17:45:07 +01001328 driver_release_tids |= sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001329 driver_release_tids |= sta->txq_buffered_tids & tids;
1330 }
Johannes Berg47086fc2011-09-29 16:04:33 +02001331
Johannes Bergf9f760b2014-01-08 17:45:07 +01001332 if (driver_release_tids) {
1333 /* If the driver has data on more than one TID then
Johannes Berg4049e092011-09-29 16:04:32 +02001334 * certainly there's more data if we release just a
Johannes Bergf9f760b2014-01-08 17:45:07 +01001335 * single frame now (from a single TID). This will
1336 * only happen for PS-Poll.
Johannes Berg4049e092011-09-29 16:04:32 +02001337 */
Johannes Berg47086fc2011-09-29 16:04:33 +02001338 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1339 hweight16(driver_release_tids) > 1) {
Johannes Berg4049e092011-09-29 16:04:32 +02001340 more_data = true;
1341 driver_release_tids =
Johannes Berg0a1cb802014-01-09 11:05:31 +01001342 BIT(find_highest_prio_tid(
1343 driver_release_tids));
Johannes Berg4049e092011-09-29 16:04:32 +02001344 break;
Johannes Berg948d8872011-09-29 16:04:29 +02001345 }
Johannes Bergf9f760b2014-01-08 17:45:07 +01001346 } else {
1347 struct sk_buff *skb;
1348
1349 while (n_frames > 0) {
1350 skb = skb_dequeue(&sta->tx_filtered[ac]);
1351 if (!skb) {
1352 skb = skb_dequeue(
1353 &sta->ps_tx_buf[ac]);
1354 if (skb)
1355 local->total_ps_buffered--;
1356 }
1357 if (!skb)
1358 break;
1359 n_frames--;
1360 __skb_queue_tail(&frames, skb);
1361 }
Johannes Berg948d8872011-09-29 16:04:29 +02001362 }
1363
Johannes Bergf9f760b2014-01-08 17:45:07 +01001364 /* If we have more frames buffered on this AC, then set the
1365 * more-data bit and abort the loop since we can't send more
1366 * data from other ACs before the buffered frames from this.
1367 */
Johannes Berg948d8872011-09-29 16:04:29 +02001368 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1369 !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1370 more_data = true;
1371 break;
1372 }
Johannes Bergaf818582009-11-06 11:35:50 +01001373 }
Johannes Bergaf818582009-11-06 11:35:50 +01001374
Johannes Bergf9f760b2014-01-08 17:45:07 +01001375 if (skb_queue_empty(&frames) && !driver_release_tids) {
Johannes Bergce662b442011-09-29 16:04:34 +02001376 int tid;
Johannes Berg4049e092011-09-29 16:04:32 +02001377
Johannes Bergce662b442011-09-29 16:04:34 +02001378 /*
1379 * For PS-Poll, this can only happen due to a race condition
1380 * when we set the TIM bit and the station notices it, but
1381 * before it can poll for the frame we expire it.
1382 *
1383 * For uAPSD, this is said in the standard (11.2.1.5 h):
1384 * At each unscheduled SP for a non-AP STA, the AP shall
1385 * attempt to transmit at least one MSDU or MMPDU, but no
1386 * more than the value specified in the Max SP Length field
1387 * in the QoS Capability element from delivery-enabled ACs,
1388 * that are destined for the non-AP STA.
1389 *
1390 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1391 */
1392
1393 /* This will evaluate to 1, 3, 5 or 7. */
1394 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1395
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001396 ieee80211_send_null_response(sdata, sta, tid, reason, true);
Johannes Bergf9f760b2014-01-08 17:45:07 +01001397 } else if (!driver_release_tids) {
Johannes Berg47086fc2011-09-29 16:04:33 +02001398 struct sk_buff_head pending;
1399 struct sk_buff *skb;
Johannes Berg40b96402011-09-29 16:04:38 +02001400 int num = 0;
1401 u16 tids = 0;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001402 bool need_null = false;
Johannes Bergaf818582009-11-06 11:35:50 +01001403
Johannes Berg47086fc2011-09-29 16:04:33 +02001404 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001405
Johannes Berg47086fc2011-09-29 16:04:33 +02001406 while ((skb = __skb_dequeue(&frames))) {
1407 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1408 struct ieee80211_hdr *hdr = (void *) skb->data;
Johannes Berg40b96402011-09-29 16:04:38 +02001409 u8 *qoshdr = NULL;
1410
1411 num++;
Johannes Bergaf818582009-11-06 11:35:50 +01001412
Johannes Berg47086fc2011-09-29 16:04:33 +02001413 /*
1414 * Tell TX path to send this frame even though the
1415 * STA may still remain is PS mode after this frame
1416 * exchange.
1417 */
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301418 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1419 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
Johannes Bergaf818582009-11-06 11:35:50 +01001420
Johannes Berg47086fc2011-09-29 16:04:33 +02001421 /*
1422 * Use MoreData flag to indicate whether there are
1423 * more buffered frames for this STA
1424 */
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001425 if (more_data || !skb_queue_empty(&frames))
Johannes Berg47086fc2011-09-29 16:04:33 +02001426 hdr->frame_control |=
1427 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001428 else
1429 hdr->frame_control &=
1430 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
Johannes Berg47086fc2011-09-29 16:04:33 +02001431
Johannes Berg40b96402011-09-29 16:04:38 +02001432 if (ieee80211_is_data_qos(hdr->frame_control) ||
1433 ieee80211_is_qos_nullfunc(hdr->frame_control))
1434 qoshdr = ieee80211_get_qos_ctl(hdr);
1435
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001436 tids |= BIT(skb->priority);
1437
1438 __skb_queue_tail(&pending, skb);
1439
1440 /* end service period after last frame or add one */
1441 if (!skb_queue_empty(&frames))
1442 continue;
1443
1444 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1445 /* for PS-Poll, there's only one frame */
1446 info->flags |= IEEE80211_TX_STATUS_EOSP |
1447 IEEE80211_TX_CTL_REQ_TX_STATUS;
1448 break;
1449 }
1450
1451 /* For uAPSD, things are a bit more complicated. If the
1452 * last frame has a QoS header (i.e. is a QoS-data or
1453 * QoS-nulldata frame) then just set the EOSP bit there
1454 * and be done.
1455 * If the frame doesn't have a QoS header (which means
1456 * it should be a bufferable MMPDU) then we can't set
1457 * the EOSP bit in the QoS header; add a QoS-nulldata
1458 * frame to the list to send it after the MMPDU.
1459 *
1460 * Note that this code is only in the mac80211-release
1461 * code path, we assume that the driver will not buffer
1462 * anything but QoS-data frames, or if it does, will
1463 * create the QoS-nulldata frame by itself if needed.
1464 *
1465 * Cf. 802.11-2012 10.2.1.10 (c).
1466 */
1467 if (qoshdr) {
1468 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
Johannes Berg47086fc2011-09-29 16:04:33 +02001469
Marco Porsch52a3f202012-03-16 15:30:26 +01001470 info->flags |= IEEE80211_TX_STATUS_EOSP |
1471 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001472 } else {
1473 /* The standard isn't completely clear on this
1474 * as it says the more-data bit should be set
1475 * if there are more BUs. The QoS-Null frame
1476 * we're about to send isn't buffered yet, we
1477 * only create it below, but let's pretend it
1478 * was buffered just in case some clients only
1479 * expect more-data=0 when eosp=1.
1480 */
1481 hdr->frame_control |=
1482 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1483 need_null = true;
1484 num++;
Marco Porsch52a3f202012-03-16 15:30:26 +01001485 }
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001486 break;
Johannes Berg47086fc2011-09-29 16:04:33 +02001487 }
1488
Johannes Berg40b96402011-09-29 16:04:38 +02001489 drv_allow_buffered_frames(local, sta, tids, num,
1490 reason, more_data);
1491
Johannes Berg47086fc2011-09-29 16:04:33 +02001492 ieee80211_add_pending_skbs(local, &pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001493
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001494 if (need_null)
1495 ieee80211_send_null_response(
1496 sdata, sta, find_highest_prio_tid(tids),
1497 reason, false);
1498
Johannes Bergc868cb352011-09-29 16:04:27 +02001499 sta_info_recalc_tim(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001500 } else {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001501 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1502 int tid;
1503
Johannes Bergaf818582009-11-06 11:35:50 +01001504 /*
Johannes Berg4049e092011-09-29 16:04:32 +02001505 * We need to release a frame that is buffered somewhere in the
1506 * driver ... it'll have to handle that.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001507 * Note that the driver also has to check the number of frames
1508 * on the TIDs we're releasing from - if there are more than
1509 * n_frames it has to set the more-data bit (if we didn't ask
1510 * it to set it anyway due to other buffered frames); if there
1511 * are fewer than n_frames it has to make sure to adjust that
1512 * to allow the service period to end properly.
Johannes Bergaf818582009-11-06 11:35:50 +01001513 */
Johannes Berg4049e092011-09-29 16:04:32 +02001514 drv_release_buffered_frames(local, sta, driver_release_tids,
Johannes Berg47086fc2011-09-29 16:04:33 +02001515 n_frames, reason, more_data);
Johannes Berg4049e092011-09-29 16:04:32 +02001516
1517 /*
1518 * Note that we don't recalculate the TIM bit here as it would
1519 * most likely have no effect at all unless the driver told us
Johannes Bergf9f760b2014-01-08 17:45:07 +01001520 * that the TID(s) became empty before returning here from the
Johannes Berg4049e092011-09-29 16:04:32 +02001521 * release function.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001522 * Either way, however, when the driver tells us that the TID(s)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001523 * became empty or we find that a txq became empty, we'll do the
1524 * TIM recalculation.
Johannes Berg4049e092011-09-29 16:04:32 +02001525 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001526
1527 if (!sta->sta.txq[0])
1528 return;
1529
1530 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1531 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1532
1533 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1534 continue;
1535
1536 sta_info_recalc_tim(sta);
1537 break;
1538 }
Johannes Bergaf818582009-11-06 11:35:50 +01001539 }
1540}
1541
Johannes Bergaf818582009-11-06 11:35:50 +01001542void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1543{
Johannes Berg47086fc2011-09-29 16:04:33 +02001544 u8 ignore_for_response = sta->sta.uapsd_queues;
Johannes Bergaf818582009-11-06 11:35:50 +01001545
Johannes Berg47086fc2011-09-29 16:04:33 +02001546 /*
1547 * If all ACs are delivery-enabled then we should reply
1548 * from any of them, if only some are enabled we reply
1549 * only from the non-enabled ones.
1550 */
1551 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1552 ignore_for_response = 0;
1553
1554 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1555 IEEE80211_FRAME_RELEASE_PSPOLL);
1556}
1557
1558void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1559{
1560 int n_frames = sta->sta.max_sp;
1561 u8 delivery_enabled = sta->sta.uapsd_queues;
1562
1563 /*
1564 * If we ever grow support for TSPEC this might happen if
1565 * the TSPEC update from hostapd comes in between a trigger
1566 * frame setting WLAN_STA_UAPSD in the RX path and this
1567 * actually getting called.
1568 */
1569 if (!delivery_enabled)
1570 return;
1571
Johannes Berg47086fc2011-09-29 16:04:33 +02001572 switch (sta->sta.max_sp) {
1573 case 1:
1574 n_frames = 2;
1575 break;
1576 case 2:
1577 n_frames = 4;
1578 break;
1579 case 3:
1580 n_frames = 6;
1581 break;
1582 case 0:
1583 /* XXX: what is a good value? */
Andrei Otcheretianski13a80982014-11-04 11:33:04 +02001584 n_frames = 128;
Johannes Berg47086fc2011-09-29 16:04:33 +02001585 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001586 }
Johannes Bergaf818582009-11-06 11:35:50 +01001587
Johannes Berg47086fc2011-09-29 16:04:33 +02001588 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1589 IEEE80211_FRAME_RELEASE_UAPSD);
Johannes Bergaf818582009-11-06 11:35:50 +01001590}
1591
1592void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1593 struct ieee80211_sta *pubsta, bool block)
1594{
1595 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1596
Johannes Bergb5878a22010-04-07 16:48:40 +02001597 trace_api_sta_block_awake(sta->local, pubsta, block);
1598
Johannes Berg5ac2e352014-05-27 16:32:27 +02001599 if (block) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001600 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001601 return;
1602 }
1603
1604 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1605 return;
1606
1607 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1608 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1609 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1610 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1611 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1612 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1613 /* must be asleep in this case */
1614 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1615 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1616 } else {
1617 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1618 }
Johannes Bergaf818582009-11-06 11:35:50 +01001619}
1620EXPORT_SYMBOL(ieee80211_sta_block_awake);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001621
Johannes Berge9437892013-02-15 21:38:08 +01001622void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
Johannes Berg37fbd902011-09-29 16:04:39 +02001623{
1624 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1625 struct ieee80211_local *local = sta->local;
Johannes Berg37fbd902011-09-29 16:04:39 +02001626
1627 trace_api_eosp(local, pubsta);
1628
Johannes Berge9437892013-02-15 21:38:08 +01001629 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg37fbd902011-09-29 16:04:39 +02001630}
Johannes Berge9437892013-02-15 21:38:08 +01001631EXPORT_SYMBOL(ieee80211_sta_eosp);
Johannes Berg37fbd902011-09-29 16:04:39 +02001632
Johannes Berg042ec452011-09-29 16:04:26 +02001633void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1634 u8 tid, bool buffered)
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001635{
1636 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1637
Johannes Berg5a306f52012-11-14 23:22:21 +01001638 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
Johannes Berg042ec452011-09-29 16:04:26 +02001639 return;
1640
Johannes Berg1b000782013-12-19 10:47:48 +01001641 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1642
Johannes Berg948d8872011-09-29 16:04:29 +02001643 if (buffered)
1644 set_bit(tid, &sta->driver_buffered_tids);
1645 else
1646 clear_bit(tid, &sta->driver_buffered_tids);
1647
Johannes Bergc868cb352011-09-29 16:04:27 +02001648 sta_info_recalc_tim(sta);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001649}
Johannes Berg042ec452011-09-29 16:04:26 +02001650EXPORT_SYMBOL(ieee80211_sta_set_buffered);
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001651
Johannes Berg83d5cc02012-01-12 09:31:10 +01001652int sta_info_move_state(struct sta_info *sta,
1653 enum ieee80211_sta_state new_state)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001654{
Johannes Berg8bf11d82011-12-15 11:17:37 +01001655 might_sleep();
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001656
1657 if (sta->sta_state == new_state)
1658 return 0;
1659
Johannes Bergf09603a2012-01-20 13:55:21 +01001660 /* check allowed transitions first */
1661
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001662 switch (new_state) {
1663 case IEEE80211_STA_NONE:
Johannes Bergf09603a2012-01-20 13:55:21 +01001664 if (sta->sta_state != IEEE80211_STA_AUTH)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001665 return -EINVAL;
1666 break;
1667 case IEEE80211_STA_AUTH:
Johannes Bergf09603a2012-01-20 13:55:21 +01001668 if (sta->sta_state != IEEE80211_STA_NONE &&
1669 sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001670 return -EINVAL;
1671 break;
1672 case IEEE80211_STA_ASSOC:
Johannes Bergf09603a2012-01-20 13:55:21 +01001673 if (sta->sta_state != IEEE80211_STA_AUTH &&
1674 sta->sta_state != IEEE80211_STA_AUTHORIZED)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001675 return -EINVAL;
1676 break;
1677 case IEEE80211_STA_AUTHORIZED:
Johannes Bergf09603a2012-01-20 13:55:21 +01001678 if (sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001679 return -EINVAL;
1680 break;
1681 default:
1682 WARN(1, "invalid state %d", new_state);
1683 return -EINVAL;
1684 }
1685
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001686 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1687 sta->sta.addr, new_state);
Johannes Bergf09603a2012-01-20 13:55:21 +01001688
1689 /*
1690 * notify the driver before the actual changes so it can
1691 * fail the transition
1692 */
1693 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1694 int err = drv_sta_state(sta->local, sta->sdata, sta,
1695 sta->sta_state, new_state);
1696 if (err)
1697 return err;
1698 }
1699
1700 /* reflect the change in all state variables */
1701
1702 switch (new_state) {
1703 case IEEE80211_STA_NONE:
1704 if (sta->sta_state == IEEE80211_STA_AUTH)
1705 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1706 break;
1707 case IEEE80211_STA_AUTH:
1708 if (sta->sta_state == IEEE80211_STA_NONE)
1709 set_bit(WLAN_STA_AUTH, &sta->_flags);
1710 else if (sta->sta_state == IEEE80211_STA_ASSOC)
1711 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1712 break;
1713 case IEEE80211_STA_ASSOC:
1714 if (sta->sta_state == IEEE80211_STA_AUTH) {
1715 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1716 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001717 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1718 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1719 !sta->sdata->u.vlan.sta))
1720 atomic_dec(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001721 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1722 }
1723 break;
1724 case IEEE80211_STA_AUTHORIZED:
1725 if (sta->sta_state == IEEE80211_STA_ASSOC) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001726 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1727 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1728 !sta->sdata->u.vlan.sta))
1729 atomic_inc(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001730 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1731 }
1732 break;
1733 default:
1734 break;
1735 }
1736
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001737 sta->sta_state = new_state;
1738
1739 return 0;
1740}
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001741
1742u8 sta_info_tx_streams(struct sta_info *sta)
1743{
1744 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1745 u8 rx_streams;
1746
1747 if (!sta->sta.ht_cap.ht_supported)
1748 return 1;
1749
1750 if (sta->sta.vht_cap.vht_supported) {
1751 int i;
1752 u16 tx_mcs_map =
1753 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1754
1755 for (i = 7; i >= 0; i--)
1756 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1757 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1758 return i + 1;
1759 }
1760
1761 if (ht_cap->mcs.rx_mask[3])
1762 rx_streams = 4;
1763 else if (ht_cap->mcs.rx_mask[2])
1764 rx_streams = 3;
1765 else if (ht_cap->mcs.rx_mask[1])
1766 rx_streams = 2;
1767 else
1768 rx_streams = 1;
1769
1770 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1771 return rx_streams;
1772
1773 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1774 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1775}
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001776
1777void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1778{
1779 struct ieee80211_sub_if_data *sdata = sta->sdata;
1780 struct ieee80211_local *local = sdata->local;
John W. Linville9a244402014-07-25 10:22:36 -04001781 struct rate_control_ref *ref = NULL;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001782 struct timespec uptime;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001783 u32 thr = 0;
1784 int i, ac;
1785
John W. Linville9a244402014-07-25 10:22:36 -04001786 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1787 ref = local->rate_ctrl;
1788
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001789 sinfo->generation = sdata->local->sta_generation;
1790
Johannes Berg225b8182015-01-21 21:09:02 +01001791 /* do before driver, so beacon filtering drivers have a
1792 * chance to e.g. just add the number of filtered beacons
1793 * (or just modify the value entirely, of course)
1794 */
1795 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1796 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1797
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001798 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1799
Johannes Berg319090b2014-11-17 14:08:11 +01001800 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1801 BIT(NL80211_STA_INFO_STA_FLAGS) |
1802 BIT(NL80211_STA_INFO_BSS_PARAM) |
1803 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1804 BIT(NL80211_STA_INFO_RX_DROP_MISC) |
1805 BIT(NL80211_STA_INFO_BEACON_LOSS);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001806
Thomas Gleixner18171522014-06-11 23:59:14 +00001807 ktime_get_ts(&uptime);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001808 sinfo->connected_time = uptime.tv_sec - sta->last_connected;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001809 sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001810
Johannes Berg319090b2014-11-17 14:08:11 +01001811 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1812 BIT(NL80211_STA_INFO_TX_BYTES)))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001813 sinfo->tx_bytes = 0;
1814 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1815 sinfo->tx_bytes += sta->tx_bytes[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01001816 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001817 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001818
Johannes Berg319090b2014-11-17 14:08:11 +01001819 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001820 sinfo->tx_packets = 0;
1821 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1822 sinfo->tx_packets += sta->tx_packets[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01001823 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001824 }
1825
Johannes Berg319090b2014-11-17 14:08:11 +01001826 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1827 BIT(NL80211_STA_INFO_RX_BYTES)))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001828 sinfo->rx_bytes = sta->rx_bytes;
Johannes Berg319090b2014-11-17 14:08:11 +01001829 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001830 }
1831
Johannes Berg319090b2014-11-17 14:08:11 +01001832 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001833 sinfo->rx_packets = sta->rx_packets;
Johannes Berg319090b2014-11-17 14:08:11 +01001834 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001835 }
1836
Johannes Berg319090b2014-11-17 14:08:11 +01001837 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001838 sinfo->tx_retries = sta->tx_retry_count;
Johannes Berg319090b2014-11-17 14:08:11 +01001839 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001840 }
1841
Johannes Berg319090b2014-11-17 14:08:11 +01001842 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001843 sinfo->tx_failed = sta->tx_retry_failed;
Johannes Berg319090b2014-11-17 14:08:11 +01001844 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001845 }
1846
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001847 sinfo->rx_dropped_misc = sta->rx_dropped;
1848 sinfo->beacon_loss_count = sta->beacon_loss_count;
1849
Johannes Berg225b8182015-01-21 21:09:02 +01001850 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1851 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1852 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1853 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1854 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1855 }
1856
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001857 if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
1858 (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
Johannes Berg319090b2014-11-17 14:08:11 +01001859 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001860 sinfo->signal = (s8)sta->last_signal;
Johannes Berg319090b2014-11-17 14:08:11 +01001861 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001862 }
1863
Johannes Berg319090b2014-11-17 14:08:11 +01001864 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001865 sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
Johannes Berg319090b2014-11-17 14:08:11 +01001866 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001867 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001868 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001869
1870 if (sta->chains &&
Johannes Berg319090b2014-11-17 14:08:11 +01001871 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1872 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1873 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1874 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001875
1876 sinfo->chains = sta->chains;
1877 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1878 sinfo->chain_signal[i] = sta->chain_signal_last[i];
1879 sinfo->chain_signal_avg[i] =
1880 (s8) -ewma_read(&sta->chain_signal_avg[i]);
1881 }
1882 }
1883
Johannes Berg319090b2014-11-17 14:08:11 +01001884 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001885 sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
Johannes Berg319090b2014-11-17 14:08:11 +01001886 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001887 }
1888
Johannes Berg319090b2014-11-17 14:08:11 +01001889 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001890 sta_set_rate_info_rx(sta, &sinfo->rxrate);
Johannes Berg319090b2014-11-17 14:08:11 +01001891 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001892 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001893
Johannes Berg79c892b2014-11-21 14:26:31 +01001894 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
1895 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
1896 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
1897
1898 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
1899 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
1900 tidstats->rx_msdu = sta->rx_msdu[i];
1901 }
1902
1903 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
1904 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
1905 tidstats->tx_msdu = sta->tx_msdu[i];
1906 }
1907
1908 if (!(tidstats->filled &
1909 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
1910 local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1911 tidstats->filled |=
1912 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
1913 tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
1914 }
1915
1916 if (!(tidstats->filled &
1917 BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
1918 local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1919 tidstats->filled |=
1920 BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
1921 tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
1922 }
1923 }
1924
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001925 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1926#ifdef CONFIG_MAC80211_MESH
Johannes Berg319090b2014-11-17 14:08:11 +01001927 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
1928 BIT(NL80211_STA_INFO_PLID) |
1929 BIT(NL80211_STA_INFO_PLINK_STATE) |
1930 BIT(NL80211_STA_INFO_LOCAL_PM) |
1931 BIT(NL80211_STA_INFO_PEER_PM) |
1932 BIT(NL80211_STA_INFO_NONPEER_PM);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001933
1934 sinfo->llid = sta->llid;
1935 sinfo->plid = sta->plid;
1936 sinfo->plink_state = sta->plink_state;
1937 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
Johannes Berg319090b2014-11-17 14:08:11 +01001938 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001939 sinfo->t_offset = sta->t_offset;
1940 }
1941 sinfo->local_pm = sta->local_pm;
1942 sinfo->peer_pm = sta->peer_pm;
1943 sinfo->nonpeer_pm = sta->nonpeer_pm;
1944#endif
1945 }
1946
1947 sinfo->bss_param.flags = 0;
1948 if (sdata->vif.bss_conf.use_cts_prot)
1949 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1950 if (sdata->vif.bss_conf.use_short_preamble)
1951 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1952 if (sdata->vif.bss_conf.use_short_slot)
1953 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
Emmanuel Grumbach785e21a2014-09-03 15:25:04 +03001954 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001955 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1956
1957 sinfo->sta_flags.set = 0;
1958 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1959 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1960 BIT(NL80211_STA_FLAG_WME) |
1961 BIT(NL80211_STA_FLAG_MFP) |
1962 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1963 BIT(NL80211_STA_FLAG_ASSOCIATED) |
1964 BIT(NL80211_STA_FLAG_TDLS_PEER);
1965 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1966 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1967 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1968 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
Johannes Berga74a8c82014-07-22 14:50:47 +02001969 if (sta->sta.wme)
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001970 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1971 if (test_sta_flag(sta, WLAN_STA_MFP))
1972 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1973 if (test_sta_flag(sta, WLAN_STA_AUTH))
1974 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
1975 if (test_sta_flag(sta, WLAN_STA_ASSOC))
1976 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1977 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1978 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
1979
1980 /* check if the driver has a SW RC implementation */
1981 if (ref && ref->ops->get_expected_throughput)
1982 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
1983 else
1984 thr = drv_get_expected_throughput(local, &sta->sta);
1985
1986 if (thr != 0) {
Johannes Berg319090b2014-11-17 14:08:11 +01001987 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001988 sinfo->expected_throughput = thr;
1989 }
1990}