blob: 960e13d8ed304570efd7bdeb2299be620498bff3 [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
Sara Sharonf59374e2016-03-02 23:46:14 +02005 * Copyright (C) 2015 - 2016 Intel Deutschland GmbH
Jiri Bencf0706e82007-05-05 11:45:53 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
Felix Fietkau888d04d2012-03-01 15:22:09 +010014#include <linux/etherdevice.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070015#include <linux/netdevice.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/if_arp.h>
Johannes Berg0d174402007-12-17 15:07:43 +010020#include <linux/timer.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010021#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070022
23#include <net/mac80211.h>
24#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020025#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040026#include "rate.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070027#include "sta_info.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070028#include "debugfs_sta.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010029#include "mesh.h"
Johannes Bergce662b442011-09-29 16:04:34 +020030#include "wme.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070031
Johannes Bergd0709a62008-02-25 16:27:46 +010032/**
33 * DOC: STA information lifetime rules
34 *
35 * STA info structures (&struct sta_info) are managed in a hash table
36 * for faster lookup and a list for iteration. They are managed using
37 * RCU, i.e. access to the list and hash table is protected by RCU.
38 *
Johannes Berg34e89502010-02-03 13:59:58 +010039 * Upon allocating a STA info structure with sta_info_alloc(), the caller
40 * owns that structure. It must then insert it into the hash table using
41 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
42 * case (which acquires an rcu read section but must not be called from
43 * within one) will the pointer still be valid after the call. Note that
44 * the caller may not do much with the STA info before inserting it, in
45 * particular, it may not start any mesh peer link management or add
46 * encryption keys.
Johannes Berg93e5deb2008-04-01 15:21:00 +020047 *
48 * When the insertion fails (sta_info_insert()) returns non-zero), the
49 * structure will have been freed by sta_info_insert()!
Johannes Bergd0709a62008-02-25 16:27:46 +010050 *
Johannes Berg34e89502010-02-03 13:59:58 +010051 * Station entries are added by mac80211 when you establish a link with a
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040052 * peer. This means different things for the different type of interfaces
53 * we support. For a regular station this mean we add the AP sta when we
Lucas De Marchi25985ed2011-03-30 22:57:33 -030054 * receive an association response from the AP. For IBSS this occurs when
Johannes Berg34e89502010-02-03 13:59:58 +010055 * 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 -030056 * the peer immediately upon device open. When using AP mode we add stations
Johannes Berg34e89502010-02-03 13:59:58 +010057 * for each respective station upon request from userspace through nl80211.
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040058 *
Johannes Berg34e89502010-02-03 13:59:58 +010059 * In order to remove a STA info structure, various sta_info_destroy_*()
60 * calls are available.
Johannes Bergd0709a62008-02-25 16:27:46 +010061 *
Johannes Berg34e89502010-02-03 13:59:58 +010062 * There is no concept of ownership on a STA entry, each structure is
63 * owned by the global hash table/list until it is removed. All users of
64 * the structure need to be RCU protected so that the structure won't be
65 * freed before they are done using it.
Johannes Bergd0709a62008-02-25 16:27:46 +010066 */
Jiri Bencf0706e82007-05-05 11:45:53 -070067
Johannes Berg7bedd0c2015-02-13 21:55:15 +010068static const struct rhashtable_params sta_rht_params = {
69 .nelem_hint = 3, /* start small */
Johannes Bergcaf22d32015-04-24 11:10:10 +020070 .automatic_shrinking = true,
Johannes Berg7bedd0c2015-02-13 21:55:15 +010071 .head_offset = offsetof(struct sta_info, hash_node),
Johannes Bergac100ce2015-06-16 16:22:12 +020072 .key_offset = offsetof(struct sta_info, addr),
Johannes Berg7bedd0c2015-02-13 21:55:15 +010073 .key_len = ETH_ALEN,
74 .hashfn = sta_addr_hash,
Johannes Bergebd82b32015-04-23 17:26:06 +020075 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
Johannes Berg7bedd0c2015-02-13 21:55:15 +010076};
77
Johannes Berg4d339602011-12-15 11:24:20 +010078/* Caller must hold local->sta_mtx */
Michael Wube8755e2007-07-27 15:43:23 +020079static int sta_info_hash_del(struct ieee80211_local *local,
80 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070081{
Johannes Berg7bedd0c2015-02-13 21:55:15 +010082 return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
83 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -070084}
85
Johannes Berg5108ca82014-02-17 20:49:03 +010086static void __cleanup_single_sta(struct sta_info *sta)
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030087{
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030088 int ac, i;
89 struct tid_ampdu_tx *tid_tx;
90 struct ieee80211_sub_if_data *sdata = sta->sdata;
91 struct ieee80211_local *local = sdata->local;
Marco Porschd012a602012-10-10 12:39:50 -070092 struct ps_data *ps;
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030093
Johannes Berge3685e02014-02-20 11:19:58 +010094 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
Johannes Berg5ac2e352014-05-27 16:32:27 +020095 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
96 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
Marco Porschd012a602012-10-10 12:39:50 -070097 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
98 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
99 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100100 else if (ieee80211_vif_is_mesh(&sdata->vif))
101 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700102 else
103 return;
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300104
105 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Berge3685e02014-02-20 11:19:58 +0100106 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200107 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300108
Marco Porschd012a602012-10-10 12:39:50 -0700109 atomic_dec(&ps->num_sta_ps);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300110 }
111
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100112 if (sta->sta.txq[0]) {
113 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
114 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
115 int n = skb_queue_len(&txqi->queue);
116
117 ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
118 atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
Michal Kaziorf2ac7e32016-01-27 15:26:12 +0100119 txqi->byte_cnt = 0;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100120 }
121 }
122
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300123 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
124 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
Felix Fietkau1f98ab72012-11-10 03:44:14 +0100125 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
126 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300127 }
128
Thomas Pedersen45b50282013-02-06 10:17:21 -0800129 if (ieee80211_vif_is_mesh(&sdata->vif))
130 mesh_sta_cleanup(sta);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300131
Johannes Berg5ac2e352014-05-27 16:32:27 +0200132 cancel_work_sync(&sta->drv_deliver_wk);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300133
134 /*
135 * Destroy aggregation state here. It would be nice to wait for the
136 * driver to finish aggregation stop and then clean up, but for now
137 * drivers have to handle aggregation stop being requested, followed
138 * directly by station destruction.
139 */
Johannes Berg5a306f52012-11-14 23:22:21 +0100140 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berg661eb382013-06-12 22:47:56 +0200141 kfree(sta->ampdu_mlme.tid_start_tx[i]);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300142 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
143 if (!tid_tx)
144 continue;
Felix Fietkau1f98ab72012-11-10 03:44:14 +0100145 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300146 kfree(tid_tx);
147 }
Johannes Berg5108ca82014-02-17 20:49:03 +0100148}
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300149
Johannes Berg5108ca82014-02-17 20:49:03 +0100150static void cleanup_single_sta(struct sta_info *sta)
151{
152 struct ieee80211_sub_if_data *sdata = sta->sdata;
153 struct ieee80211_local *local = sdata->local;
154
155 __cleanup_single_sta(sta);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300156 sta_info_free(local, sta);
157}
158
Johannes Bergd0709a62008-02-25 16:27:46 +0100159/* protected by RCU */
Johannes Bergabe60632009-11-25 17:46:18 +0100160struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
161 const u8 *addr)
Johannes Berg43ba7e92008-02-21 14:09:30 +0100162{
Johannes Bergabe60632009-11-25 17:46:18 +0100163 struct ieee80211_local *local = sdata->local;
Johannes Berg60f4b622015-04-23 14:02:30 +0200164 struct sta_info *sta;
165 struct rhash_head *tmp;
166 const struct bucket_table *tbl;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100167
Johannes Berg60f4b622015-04-23 14:02:30 +0200168 rcu_read_lock();
169 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
170
171 for_each_sta_info(local, tbl, addr, sta, tmp) {
172 if (sta->sdata == sdata) {
173 rcu_read_unlock();
174 /* this is safe as the caller must already hold
175 * another rcu read section or the mutex
176 */
177 return sta;
178 }
179 }
180 rcu_read_unlock();
181 return NULL;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100182}
183
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100184/*
185 * Get sta info either from the specified interface
186 * or from one of its vlans
187 */
188struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
189 const u8 *addr)
190{
191 struct ieee80211_local *local = sdata->local;
192 struct sta_info *sta;
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100193 struct rhash_head *tmp;
194 const struct bucket_table *tbl;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100195
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100196 rcu_read_lock();
197 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
198
199 for_each_sta_info(local, tbl, addr, sta, tmp) {
200 if (sta->sdata == sdata ||
201 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
202 rcu_read_unlock();
203 /* this is safe as the caller must already hold
204 * another rcu read section or the mutex
205 */
206 return sta;
207 }
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100208 }
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100209 rcu_read_unlock();
210 return NULL;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100211}
212
Johannes Berg3b53fde82009-11-16 12:00:37 +0100213struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
214 int idx)
Luis Carlos Coboee385852008-02-23 15:17:11 +0100215{
Johannes Berg3b53fde82009-11-16 12:00:37 +0100216 struct ieee80211_local *local = sdata->local;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100217 struct sta_info *sta;
218 int i = 0;
219
Johannes Bergd0709a62008-02-25 16:27:46 +0100220 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg3b53fde82009-11-16 12:00:37 +0100221 if (sdata != sta->sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800222 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100223 if (i < idx) {
224 ++i;
225 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100226 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800227 return sta;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100228 }
Luis Carlos Coboee385852008-02-23 15:17:11 +0100229
230 return NULL;
231}
Jiri Bencf0706e82007-05-05 11:45:53 -0700232
Johannes Berg93e5deb2008-04-01 15:21:00 +0200233/**
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100234 * sta_info_free - free STA
Johannes Berg93e5deb2008-04-01 15:21:00 +0200235 *
Randy Dunlap6ef307b2008-07-03 13:52:18 -0700236 * @local: pointer to the global information
Johannes Berg93e5deb2008-04-01 15:21:00 +0200237 * @sta: STA info to free
238 *
239 * This function must undo everything done by sta_info_alloc()
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100240 * that may happen before sta_info_insert(). It may only be
241 * called when sta_info_insert() has not been attempted (and
242 * if that fails, the station is freed anyway.)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200243 */
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100244void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200245{
Johannes Berg889cbb92012-01-17 10:33:29 +0100246 if (sta->rate_ctrl)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100247 rate_control_free_sta(sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200248
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200249 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200250
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100251 if (sta->sta.txq[0])
252 kfree(to_txq_info(sta->sta.txq[0]));
Felix Fietkau53d04522014-05-27 22:33:57 +0200253 kfree(rcu_dereference_raw(sta->sta.rates));
Johannes Berg433f5bc2015-06-17 10:31:00 +0200254#ifdef CONFIG_MAC80211_MESH
255 kfree(sta->mesh);
256#endif
Johannes Bergc9c59622016-03-31 20:02:11 +0300257 free_percpu(sta->pcpu_rx_stats);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200258 kfree(sta);
259}
260
Johannes Berg4d339602011-12-15 11:24:20 +0100261/* Caller must hold local->sta_mtx */
Johannes Bergd0709a62008-02-25 16:27:46 +0100262static void sta_info_hash_add(struct ieee80211_local *local,
263 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700264{
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100265 rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
266 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -0700267}
Jiri Bencf0706e82007-05-05 11:45:53 -0700268
Johannes Berg5ac2e352014-05-27 16:32:27 +0200269static void sta_deliver_ps_frames(struct work_struct *wk)
Johannes Bergaf818582009-11-06 11:35:50 +0100270{
271 struct sta_info *sta;
272
Johannes Berg5ac2e352014-05-27 16:32:27 +0200273 sta = container_of(wk, struct sta_info, drv_deliver_wk);
Johannes Bergaf818582009-11-06 11:35:50 +0100274
275 if (sta->dead)
276 return;
277
Johannes Berg5ac2e352014-05-27 16:32:27 +0200278 local_bh_disable();
279 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
Johannes Bergaf818582009-11-06 11:35:50 +0100280 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200281 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
Johannes Bergaf818582009-11-06 11:35:50 +0100282 ieee80211_sta_ps_deliver_poll_response(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200283 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
Johannes Berg47086fc2011-09-29 16:04:33 +0200284 ieee80211_sta_ps_deliver_uapsd(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200285 local_bh_enable();
Johannes Bergaf818582009-11-06 11:35:50 +0100286}
287
Johannes Bergaf65cd962009-11-17 18:18:36 +0100288static int sta_prepare_rate_control(struct ieee80211_local *local,
289 struct sta_info *sta, gfp_t gfp)
290{
Johannes Berg30686bf2015-06-02 21:39:54 +0200291 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
Johannes Bergaf65cd962009-11-17 18:18:36 +0100292 return 0;
293
Johannes Berg889cbb92012-01-17 10:33:29 +0100294 sta->rate_ctrl = local->rate_ctrl;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100295 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
Johannes Berg35c347a2015-03-05 16:10:08 +0100296 sta, gfp);
Johannes Berg889cbb92012-01-17 10:33:29 +0100297 if (!sta->rate_ctrl_priv)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100298 return -ENOMEM;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100299
300 return 0;
301}
302
Johannes Berg73651ee2008-02-25 16:27:47 +0100303struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
Johannes Berg56544162011-12-14 13:28:46 +0100304 const u8 *addr, gfp_t gfp)
Jiri Bencf0706e82007-05-05 11:45:53 -0700305{
Johannes Bergd0709a62008-02-25 16:27:46 +0100306 struct ieee80211_local *local = sdata->local;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100307 struct ieee80211_hw *hw = &local->hw;
Jiri Bencf0706e82007-05-05 11:45:53 -0700308 struct sta_info *sta;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200309 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700310
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100311 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
Jiri Bencf0706e82007-05-05 11:45:53 -0700312 if (!sta)
Johannes Berg73651ee2008-02-25 16:27:47 +0100313 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700314
Johannes Bergc9c59622016-03-31 20:02:11 +0300315 if (ieee80211_hw_check(hw, USES_RSS)) {
316 sta->pcpu_rx_stats =
317 alloc_percpu(struct ieee80211_sta_rx_stats);
318 if (!sta->pcpu_rx_stats)
319 goto free;
320 }
321
Johannes Berg07346f812008-05-03 01:02:02 +0200322 spin_lock_init(&sta->lock);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200323 spin_lock_init(&sta->ps_lock);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200324 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
Johannes Berg67c282c2010-06-10 10:21:43 +0200325 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
Johannes Berga93e3642010-06-10 10:21:46 +0200326 mutex_init(&sta->ampdu_mlme.mtx);
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800327#ifdef CONFIG_MAC80211_MESH
Johannes Berg433f5bc2015-06-17 10:31:00 +0200328 if (ieee80211_vif_is_mesh(&sdata->vif)) {
329 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
330 if (!sta->mesh)
331 goto free;
332 spin_lock_init(&sta->mesh->plink_lock);
333 if (ieee80211_vif_is_mesh(&sdata->vif) &&
334 !sdata->u.mesh.user_mpm)
335 init_timer(&sta->mesh->plink_timer);
336 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
337 }
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800338#endif
Johannes Berg07346f812008-05-03 01:02:02 +0200339
Johannes Bergac100ce2015-06-16 16:22:12 +0200340 memcpy(sta->addr, addr, ETH_ALEN);
Johannes Berg17741cd2008-09-11 00:02:02 +0200341 memcpy(sta->sta.addr, addr, ETH_ALEN);
Johannes Bergd0709a62008-02-25 16:27:46 +0100342 sta->local = local;
343 sta->sdata = sdata;
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200344 sta->rx_stats.last_rx = jiffies;
Jiri Bencf0706e82007-05-05 11:45:53 -0700345
Johannes Berg0f9c5a62016-03-31 20:02:09 +0300346 u64_stats_init(&sta->rx_stats.syncp);
347
Johannes Berg71ec3752012-01-20 13:55:20 +0100348 sta->sta_state = IEEE80211_STA_NONE;
349
Liad Kaufmanb6da9112014-11-19 13:47:38 +0200350 /* Mark TID as unreserved */
351 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
352
Arnd Bergmann84b00602015-09-30 13:26:36 +0200353 sta->last_connected = ktime_get_seconds();
Johannes Berg0be6ed12016-03-31 20:02:05 +0300354 ewma_signal_init(&sta->rx_stats_avg.signal);
355 for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
356 ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
Bruno Randolf541a45a2010-12-02 19:12:43 +0900357
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100358 if (local->ops->wake_tx_queue) {
359 void *txq_data;
360 int size = sizeof(struct txq_info) +
361 ALIGN(hw->txq_data_size, sizeof(void *));
362
363 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
364 if (!txq_data)
365 goto free;
366
367 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
368 struct txq_info *txq = txq_data + i * size;
369
370 ieee80211_init_tx_queue(sdata, sta, txq, i);
371 }
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100372 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700373
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100374 if (sta_prepare_rate_control(local, sta, gfp))
375 goto free_txq;
376
Johannes Berg5a306f52012-11-14 23:22:21 +0100377 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berga622ab72010-06-10 10:21:39 +0200378 /*
379 * timer_to_tid must be initialized with identity mapping
380 * to enable session_timer's data differentiation. See
381 * sta_rx_agg_session_timer_expired for usage.
382 */
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200383 sta->timer_to_tid[i] = i;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200384 }
Johannes Berg948d8872011-09-29 16:04:29 +0200385 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
386 skb_queue_head_init(&sta->ps_tx_buf[i]);
387 skb_queue_head_init(&sta->tx_filtered[i]);
388 }
Johannes Berg73651ee2008-02-25 16:27:47 +0100389
Johannes Berg5a306f52012-11-14 23:22:21 +0100390 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700391 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
Senthil Balasubramaniancccaec92009-05-14 18:42:08 +0530392
Johannes Bergaf0ed692013-02-12 14:21:00 +0100393 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300394 if (sdata->vif.type == NL80211_IFTYPE_AP ||
395 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
396 struct ieee80211_supported_band *sband =
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100397 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300398 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
399 IEEE80211_HT_CAP_SM_PS_SHIFT;
400 /*
401 * Assume that hostapd advertises our caps in the beacon and
402 * this is the known_smps_mode for a station that just assciated
403 */
404 switch (smps) {
405 case WLAN_HT_SMPS_CONTROL_DISABLED:
406 sta->known_smps_mode = IEEE80211_SMPS_OFF;
407 break;
408 case WLAN_HT_SMPS_CONTROL_STATIC:
409 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
410 break;
411 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
412 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
413 break;
414 default:
415 WARN_ON(1);
416 }
417 }
Johannes Bergaf0ed692013-02-12 14:21:00 +0100418
Felix Fietkau6e0456b2016-03-03 22:59:00 +0100419 sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
420
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200421 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
Johannes Bergef04a292014-01-06 15:56:59 +0100422
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100423 return sta;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100424
425free_txq:
426 if (sta->sta.txq[0])
427 kfree(to_txq_info(sta->sta.txq[0]));
428free:
Johannes Berg433f5bc2015-06-17 10:31:00 +0200429#ifdef CONFIG_MAC80211_MESH
430 kfree(sta->mesh);
431#endif
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100432 kfree(sta);
433 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +0100434}
435
Guy Eilam8c71df72011-08-17 15:18:14 +0300436static int sta_info_insert_check(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100437{
Johannes Berg34e89502010-02-03 13:59:58 +0100438 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg34e89502010-02-03 13:59:58 +0100439
Johannes Berg03e44972008-02-27 09:56:40 +0100440 /*
441 * Can't be a WARN_ON because it can be triggered through a race:
442 * something inserts a STA (on one CPU) without holding the RTNL
443 * and another CPU turns off the net device.
444 */
Guy Eilam8c71df72011-08-17 15:18:14 +0300445 if (unlikely(!ieee80211_sdata_running(sdata)))
446 return -ENETDOWN;
Johannes Berg03e44972008-02-27 09:56:40 +0100447
Joe Perchesb203ca32012-05-08 18:56:52 +0000448 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
Guy Eilam8c71df72011-08-17 15:18:14 +0300449 is_multicast_ether_addr(sta->sta.addr)))
450 return -EINVAL;
451
Johannes Berg31104892015-10-22 17:35:19 +0200452 /* Strictly speaking this isn't necessary as we hold the mutex, but
453 * the rhashtable code can't really deal with that distinction. We
454 * do require the mutex for correctness though.
455 */
456 rcu_read_lock();
457 lockdep_assert_held(&sdata->local->sta_mtx);
458 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
459 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
460 rcu_read_unlock();
461 return -ENOTUNIQ;
462 }
463 rcu_read_unlock();
464
Guy Eilam8c71df72011-08-17 15:18:14 +0300465 return 0;
466}
467
Johannes Bergf09603a2012-01-20 13:55:21 +0100468static int sta_info_insert_drv_state(struct ieee80211_local *local,
469 struct ieee80211_sub_if_data *sdata,
470 struct sta_info *sta)
471{
472 enum ieee80211_sta_state state;
473 int err = 0;
474
475 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
476 err = drv_sta_state(local, sdata, sta, state, state + 1);
477 if (err)
478 break;
479 }
480
481 if (!err) {
Johannes Berga4ec45a2012-01-20 13:55:22 +0100482 /*
483 * Drivers using legacy sta_add/sta_remove callbacks only
484 * get uploaded set to true after sta_add is called.
485 */
486 if (!local->ops->sta_add)
487 sta->uploaded = true;
Johannes Bergf09603a2012-01-20 13:55:21 +0100488 return 0;
489 }
490
491 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200492 sdata_info(sdata,
493 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
494 sta->sta.addr, state + 1, err);
Johannes Bergf09603a2012-01-20 13:55:21 +0100495 err = 0;
496 }
497
498 /* unwind on error */
499 for (; state > IEEE80211_STA_NOTEXIST; state--)
500 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
501
502 return err;
503}
504
Guy Eilam8c71df72011-08-17 15:18:14 +0300505/*
506 * should be called with sta_mtx locked
507 * this function replaces the mutex lock
508 * with a RCU lock
509 */
Johannes Berg4d339602011-12-15 11:24:20 +0100510static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
Guy Eilam8c71df72011-08-17 15:18:14 +0300511{
512 struct ieee80211_local *local = sta->local;
513 struct ieee80211_sub_if_data *sdata = sta->sdata;
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100514 struct station_info *sinfo;
Guy Eilam8c71df72011-08-17 15:18:14 +0300515 int err = 0;
516
517 lockdep_assert_held(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +0100518
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100519 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
520 if (!sinfo) {
521 err = -ENOMEM;
522 goto out_err;
523 }
524
Johannes Berg7852e362012-01-20 13:55:24 +0100525 /* check if STA exists already */
526 if (sta_info_get_bss(sdata, sta->sta.addr)) {
527 err = -EEXIST;
528 goto out_err;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100529 }
Johannes Berg32bfd352007-12-19 01:31:26 +0100530
Johannes Berg7852e362012-01-20 13:55:24 +0100531 local->num_sta++;
532 local->sta_generation++;
533 smp_mb();
Johannes Berg4d339602011-12-15 11:24:20 +0100534
Johannes Berg5108ca82014-02-17 20:49:03 +0100535 /* simplify things and don't accept BA sessions yet */
536 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
537
Johannes Berg7852e362012-01-20 13:55:24 +0100538 /* make the station visible */
539 sta_info_hash_add(local, sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100540
Arik Nemtsov2bad77482014-10-22 12:32:16 +0300541 list_add_tail_rcu(&sta->list, &local->sta_list);
Johannes Berg83d5cc02012-01-12 09:31:10 +0100542
Johannes Berg5108ca82014-02-17 20:49:03 +0100543 /* notify driver */
544 err = sta_info_insert_drv_state(local, sdata, sta);
545 if (err)
546 goto out_remove;
547
Johannes Berg7852e362012-01-20 13:55:24 +0100548 set_sta_flag(sta, WLAN_STA_INSERTED);
Johannes Berg5108ca82014-02-17 20:49:03 +0100549 /* accept BA sessions now */
550 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg4d339602011-12-15 11:24:20 +0100551
Johannes Berg7852e362012-01-20 13:55:24 +0100552 ieee80211_sta_debugfs_add(sta);
553 rate_control_add_sta_debugfs(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100554
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100555 sinfo->generation = local->sta_generation;
556 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
557 kfree(sinfo);
Johannes Bergd0709a62008-02-25 16:27:46 +0100558
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200559 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700560
Johannes Berg34e89502010-02-03 13:59:58 +0100561 /* move reference to rcu-protected */
562 rcu_read_lock();
563 mutex_unlock(&local->sta_mtx);
Jiri Bence9f207f2007-05-05 11:46:38 -0700564
Johannes Berg73651ee2008-02-25 16:27:47 +0100565 if (ieee80211_vif_is_mesh(&sdata->vif))
566 mesh_accept_plinks_update(sdata);
567
568 return 0;
Johannes Berg5108ca82014-02-17 20:49:03 +0100569 out_remove:
570 sta_info_hash_del(local, sta);
571 list_del_rcu(&sta->list);
572 local->num_sta--;
573 synchronize_net();
574 __cleanup_single_sta(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100575 out_err:
576 mutex_unlock(&local->sta_mtx);
Sudip Mukherjeeea32f062016-02-02 13:21:14 +0530577 kfree(sinfo);
Johannes Berg4d339602011-12-15 11:24:20 +0100578 rcu_read_lock();
579 return err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300580}
581
582int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
583{
584 struct ieee80211_local *local = sta->local;
Zhao, Gang308f7fc2014-04-21 12:53:00 +0800585 int err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300586
Johannes Berg4d339602011-12-15 11:24:20 +0100587 might_sleep();
588
Johannes Berg31104892015-10-22 17:35:19 +0200589 mutex_lock(&local->sta_mtx);
590
Guy Eilam8c71df72011-08-17 15:18:14 +0300591 err = sta_info_insert_check(sta);
592 if (err) {
Johannes Berg31104892015-10-22 17:35:19 +0200593 mutex_unlock(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700594 rcu_read_lock();
595 goto out_free;
596 }
597
Johannes Berg4d339602011-12-15 11:24:20 +0100598 err = sta_info_insert_finish(sta);
Guy Eilam8c71df72011-08-17 15:18:14 +0300599 if (err)
Johannes Berg004c8722008-02-20 11:21:35 +0100600 goto out_free;
Johannes Bergd0709a62008-02-25 16:27:46 +0100601
Johannes Berg004c8722008-02-20 11:21:35 +0100602 return 0;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200603 out_free:
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100604 sta_info_free(local, sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200605 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -0700606}
607
Johannes Berg34e89502010-02-03 13:59:58 +0100608int sta_info_insert(struct sta_info *sta)
609{
610 int err = sta_info_insert_rcu(sta);
611
612 rcu_read_unlock();
613
614 return err;
615}
616
Marco Porschd012a602012-10-10 12:39:50 -0700617static inline void __bss_tim_set(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100618{
619 /*
620 * This format has been mandated by the IEEE specifications,
621 * so this line may not be changed to use the __set_bit() format.
622 */
Marco Porschd012a602012-10-10 12:39:50 -0700623 tim[id / 8] |= (1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100624}
625
Marco Porschd012a602012-10-10 12:39:50 -0700626static inline void __bss_tim_clear(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100627{
628 /*
629 * This format has been mandated by the IEEE specifications,
630 * so this line may not be changed to use the __clear_bit() format.
631 */
Marco Porschd012a602012-10-10 12:39:50 -0700632 tim[id / 8] &= ~(1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100633}
634
Ilan Peer3d5839b2013-03-05 15:27:20 +0200635static inline bool __bss_tim_get(u8 *tim, u16 id)
636{
637 /*
638 * This format has been mandated by the IEEE specifications,
639 * so this line may not be changed to use the test_bit() format.
640 */
641 return tim[id / 8] & (1 << (id % 8));
642}
643
Johannes Berg948d8872011-09-29 16:04:29 +0200644static unsigned long ieee80211_tids_for_ac(int ac)
Johannes Berg004c8722008-02-20 11:21:35 +0100645{
Johannes Berg948d8872011-09-29 16:04:29 +0200646 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
647 switch (ac) {
648 case IEEE80211_AC_VO:
649 return BIT(6) | BIT(7);
650 case IEEE80211_AC_VI:
651 return BIT(4) | BIT(5);
652 case IEEE80211_AC_BE:
653 return BIT(0) | BIT(3);
654 case IEEE80211_AC_BK:
655 return BIT(1) | BIT(2);
656 default:
657 WARN_ON(1);
658 return 0;
Johannes Bergd0709a62008-02-25 16:27:46 +0100659 }
Johannes Berg004c8722008-02-20 11:21:35 +0100660}
661
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100662static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
Johannes Berg004c8722008-02-20 11:21:35 +0100663{
Johannes Bergc868cb352011-09-29 16:04:27 +0200664 struct ieee80211_local *local = sta->local;
Marco Porschd012a602012-10-10 12:39:50 -0700665 struct ps_data *ps;
Johannes Berg948d8872011-09-29 16:04:29 +0200666 bool indicate_tim = false;
667 u8 ignore_for_tim = sta->sta.uapsd_queues;
668 int ac;
Bob Copelanda69bd8e2015-07-14 08:31:58 -0400669 u16 id = sta->sta.aid;
Johannes Berg004c8722008-02-20 11:21:35 +0100670
Marco Porschd012a602012-10-10 12:39:50 -0700671 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
672 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
673 if (WARN_ON_ONCE(!sta->sdata->bss))
674 return;
675
676 ps = &sta->sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100677#ifdef CONFIG_MAC80211_MESH
678 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
679 ps = &sta->sdata->u.mesh.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100680#endif
Marco Porschd012a602012-10-10 12:39:50 -0700681 } else {
Johannes Bergc868cb352011-09-29 16:04:27 +0200682 return;
Marco Porschd012a602012-10-10 12:39:50 -0700683 }
Johannes Berg3e122be2008-07-09 14:40:34 +0200684
Johannes Bergc868cb352011-09-29 16:04:27 +0200685 /* No need to do anything if the driver does all */
Johannes Berg30686bf2015-06-02 21:39:54 +0200686 if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
Johannes Bergc868cb352011-09-29 16:04:27 +0200687 return;
Johannes Berg004c8722008-02-20 11:21:35 +0100688
Johannes Bergc868cb352011-09-29 16:04:27 +0200689 if (sta->dead)
690 goto done;
Johannes Berg3e122be2008-07-09 14:40:34 +0200691
Johannes Berg948d8872011-09-29 16:04:29 +0200692 /*
693 * If all ACs are delivery-enabled then we should build
694 * the TIM bit for all ACs anyway; if only some are then
695 * we ignore those and build the TIM bit using only the
696 * non-enabled ones.
697 */
698 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
699 ignore_for_tim = 0;
Johannes Berg3e122be2008-07-09 14:40:34 +0200700
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100701 if (ignore_pending)
702 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
703
Johannes Berg948d8872011-09-29 16:04:29 +0200704 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
705 unsigned long tids;
706
707 if (ignore_for_tim & BIT(ac))
708 continue;
709
710 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
711 !skb_queue_empty(&sta->ps_tx_buf[ac]);
712 if (indicate_tim)
713 break;
714
715 tids = ieee80211_tids_for_ac(ac);
716
717 indicate_tim |=
718 sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100719 indicate_tim |=
720 sta->txq_buffered_tids & tids;
Johannes Bergd0709a62008-02-25 16:27:46 +0100721 }
Johannes Berg004c8722008-02-20 11:21:35 +0100722
Johannes Bergc868cb352011-09-29 16:04:27 +0200723 done:
Johannes Berg65f704a2013-02-13 17:39:53 +0100724 spin_lock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100725
Ilan Peer3d5839b2013-03-05 15:27:20 +0200726 if (indicate_tim == __bss_tim_get(ps->tim, id))
727 goto out_unlock;
728
Johannes Berg948d8872011-09-29 16:04:29 +0200729 if (indicate_tim)
Marco Porschd012a602012-10-10 12:39:50 -0700730 __bss_tim_set(ps->tim, id);
Johannes Bergc868cb352011-09-29 16:04:27 +0200731 else
Marco Porschd012a602012-10-10 12:39:50 -0700732 __bss_tim_clear(ps->tim, id);
Johannes Berg3e122be2008-07-09 14:40:34 +0200733
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100734 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
Johannes Bergc868cb352011-09-29 16:04:27 +0200735 local->tim_in_locked_section = true;
Johannes Berg948d8872011-09-29 16:04:29 +0200736 drv_set_tim(local, &sta->sta, indicate_tim);
Johannes Bergc868cb352011-09-29 16:04:27 +0200737 local->tim_in_locked_section = false;
Johannes Berg004c8722008-02-20 11:21:35 +0100738 }
Johannes Berg004c8722008-02-20 11:21:35 +0100739
Ilan Peer3d5839b2013-03-05 15:27:20 +0200740out_unlock:
Johannes Berg65f704a2013-02-13 17:39:53 +0100741 spin_unlock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100742}
743
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100744void sta_info_recalc_tim(struct sta_info *sta)
745{
746 __sta_info_recalc_tim(sta, false);
747}
748
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200749static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700750{
Johannes Berge039fa42008-05-15 12:55:29 +0200751 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700752 int timeout;
753
754 if (!skb)
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200755 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700756
Johannes Berge039fa42008-05-15 12:55:29 +0200757 info = IEEE80211_SKB_CB(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700758
759 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200760 timeout = (sta->listen_interval *
761 sta->sdata->vif.bss_conf.beacon_int *
762 32 / 15625) * HZ;
Jiri Bencf0706e82007-05-05 11:45:53 -0700763 if (timeout < STA_TX_BUFFER_EXPIRE)
764 timeout = STA_TX_BUFFER_EXPIRE;
Johannes Berge039fa42008-05-15 12:55:29 +0200765 return time_after(jiffies, info->control.jiffies + timeout);
Jiri Bencf0706e82007-05-05 11:45:53 -0700766}
767
768
Johannes Berg948d8872011-09-29 16:04:29 +0200769static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
770 struct sta_info *sta, int ac)
Jiri Bencf0706e82007-05-05 11:45:53 -0700771{
772 unsigned long flags;
773 struct sk_buff *skb;
774
Johannes Berg60750392011-09-29 16:04:28 +0200775 /*
776 * First check for frames that should expire on the filtered
777 * queue. Frames here were rejected by the driver and are on
778 * a separate queue to avoid reordering with normal PS-buffered
779 * frames. They also aren't accounted for right now in the
780 * total_ps_buffered counter.
781 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700782 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200783 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
784 skb = skb_peek(&sta->tx_filtered[ac]);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200785 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200786 skb = __skb_dequeue(&sta->tx_filtered[ac]);
Johannes Berg836341a2008-02-20 02:07:21 +0100787 else
Jiri Bencf0706e82007-05-05 11:45:53 -0700788 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200789 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700790
Johannes Berg60750392011-09-29 16:04:28 +0200791 /*
792 * Frames are queued in order, so if this one
793 * hasn't expired yet we can stop testing. If
794 * we actually reached the end of the queue we
795 * also need to stop, of course.
796 */
797 if (!skb)
798 break;
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200799 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg60750392011-09-29 16:04:28 +0200800 }
801
802 /*
803 * Now also check the normal PS-buffered queue, this will
804 * only find something if the filtered queue was emptied
805 * since the filtered frames are all before the normal PS
806 * buffered frames.
807 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700808 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200809 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
810 skb = skb_peek(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700811 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200812 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700813 else
814 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200815 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700816
Johannes Berg60750392011-09-29 16:04:28 +0200817 /*
818 * frames are queued in order, so if this one
819 * hasn't expired yet (or we reached the end of
820 * the queue) we can stop testing
821 */
Johannes Berg836341a2008-02-20 02:07:21 +0100822 if (!skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700823 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100824
Johannes Berg836341a2008-02-20 02:07:21 +0100825 local->total_ps_buffered--;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200826 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
827 sta->sta.addr);
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200828 ieee80211_free_txskb(&local->hw, skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700829 }
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300830
Johannes Berg60750392011-09-29 16:04:28 +0200831 /*
832 * Finally, recalculate the TIM bit for this station -- it might
833 * now be clear because the station was too slow to retrieve its
834 * frames.
835 */
836 sta_info_recalc_tim(sta);
837
838 /*
839 * Return whether there are any frames still buffered, this is
840 * used to check whether the cleanup timer still needs to run,
841 * if there are no frames we don't need to rearm the timer.
842 */
Johannes Berg948d8872011-09-29 16:04:29 +0200843 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
844 skb_queue_empty(&sta->tx_filtered[ac]));
845}
846
847static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
848 struct sta_info *sta)
849{
850 bool have_buffered = false;
851 int ac;
852
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100853 /* This is only necessary for stations on BSS/MBSS interfaces */
854 if (!sta->sdata->bss &&
855 !ieee80211_vif_is_mesh(&sta->sdata->vif))
Johannes Berg948d8872011-09-29 16:04:29 +0200856 return false;
857
858 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
859 have_buffered |=
860 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
861
862 return have_buffered;
Jiri Bencf0706e82007-05-05 11:45:53 -0700863}
864
Johannes Bergd7782072013-12-04 23:12:31 +0100865static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100866{
867 struct ieee80211_local *local;
868 struct ieee80211_sub_if_data *sdata;
Johannes Berg6d10e462013-03-06 23:09:11 +0100869 int ret;
Johannes Berg34e89502010-02-03 13:59:58 +0100870
871 might_sleep();
872
873 if (!sta)
874 return -ENOENT;
875
876 local = sta->local;
877 sdata = sta->sdata;
878
Johannes Berg83d5cc02012-01-12 09:31:10 +0100879 lockdep_assert_held(&local->sta_mtx);
880
Johannes Berg098a6072010-04-06 11:18:47 +0200881 /*
882 * Before removing the station from the driver and
883 * rate control, it might still start new aggregation
884 * sessions -- block that to make sure the tear-down
885 * will be sufficient.
886 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200887 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Bergc82c4a82012-07-18 13:31:31 +0200888 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
Johannes Berg098a6072010-04-06 11:18:47 +0200889
Sara Sharonf59374e2016-03-02 23:46:14 +0200890 /*
891 * Before removing the station from the driver there might be pending
892 * rx frames on RSS queues sent prior to the disassociation - wait for
893 * all such frames to be processed.
894 */
895 drv_sync_rx_queues(local, sta);
896
Johannes Berg34e89502010-02-03 13:59:58 +0100897 ret = sta_info_hash_del(local, sta);
Johannes Bergb01711b2013-12-04 20:25:27 +0100898 if (WARN_ON(ret))
Johannes Berg34e89502010-02-03 13:59:58 +0100899 return ret;
900
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200901 /*
902 * for TDLS peers, make sure to return to the base channel before
903 * removal.
904 */
905 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
906 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
907 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
908 }
909
Arik Nemtsov794454c2012-06-03 23:32:32 +0300910 list_del_rcu(&sta->list);
Eliad Pelleref044762015-11-17 10:24:37 +0200911 sta->removed = true;
Johannes Berg4d339602011-12-15 11:24:20 +0100912
Johannes Berg6a9d1b92013-12-04 22:39:17 +0100913 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
914
Johannes Berga710c812013-12-04 20:11:06 +0100915 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
916 rcu_access_pointer(sdata->u.vlan.sta) == sta)
917 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
918
Johannes Bergd7782072013-12-04 23:12:31 +0100919 return 0;
920}
921
922static void __sta_info_destroy_part2(struct sta_info *sta)
923{
924 struct ieee80211_local *local = sta->local;
925 struct ieee80211_sub_if_data *sdata = sta->sdata;
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100926 struct station_info *sinfo;
Johannes Bergd7782072013-12-04 23:12:31 +0100927 int ret;
928
929 /*
930 * NOTE: This assumes at least synchronize_net() was done
931 * after _part1 and before _part2!
932 */
933
934 might_sleep();
935 lockdep_assert_held(&local->sta_mtx);
936
Johannes Bergc8782072013-12-04 23:05:45 +0100937 /* now keys can no longer be reached */
Johannes Berg6d10e462013-03-06 23:09:11 +0100938 ieee80211_free_sta_keys(local, sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100939
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100940 /* disable TIM bit - last chance to tell driver */
941 __sta_info_recalc_tim(sta, true);
942
Johannes Berg34e89502010-02-03 13:59:58 +0100943 sta->dead = true;
944
Johannes Berg34e89502010-02-03 13:59:58 +0100945 local->num_sta--;
946 local->sta_generation++;
947
Johannes Berg83d5cc02012-01-12 09:31:10 +0100948 while (sta->sta_state > IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100949 ret = sta_info_move_state(sta, sta->sta_state - 1);
950 if (ret) {
Johannes Berg83d5cc02012-01-12 09:31:10 +0100951 WARN_ON_ONCE(1);
952 break;
953 }
954 }
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100955
Johannes Bergf09603a2012-01-20 13:55:21 +0100956 if (sta->uploaded) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100957 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
958 IEEE80211_STA_NOTEXIST);
959 WARN_ON_ONCE(ret != 0);
960 }
Johannes Berg34e89502010-02-03 13:59:58 +0100961
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200962 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
963
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100964 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
965 if (sinfo)
966 sta_set_sinfo(sta, sinfo);
967 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
968 kfree(sinfo);
Jouni Malinenec15e682011-03-23 15:29:52 +0200969
Johannes Berg34e89502010-02-03 13:59:58 +0100970 rate_control_remove_sta_debugfs(sta);
971 ieee80211_sta_debugfs_remove(sta);
972
Johannes Bergd34ba212013-12-04 22:46:11 +0100973 cleanup_single_sta(sta);
Johannes Bergd7782072013-12-04 23:12:31 +0100974}
975
976int __must_check __sta_info_destroy(struct sta_info *sta)
977{
978 int err = __sta_info_destroy_part1(sta);
979
980 if (err)
981 return err;
982
983 synchronize_net();
984
985 __sta_info_destroy_part2(sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100986
987 return 0;
988}
989
990int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
991{
992 struct sta_info *sta;
993 int ret;
994
995 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100996 sta = sta_info_get(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100997 ret = __sta_info_destroy(sta);
998 mutex_unlock(&sdata->local->sta_mtx);
999
1000 return ret;
1001}
1002
1003int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1004 const u8 *addr)
1005{
1006 struct sta_info *sta;
1007 int ret;
1008
1009 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +01001010 sta = sta_info_get_bss(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +01001011 ret = __sta_info_destroy(sta);
1012 mutex_unlock(&sdata->local->sta_mtx);
1013
1014 return ret;
1015}
Jiri Bencf0706e82007-05-05 11:45:53 -07001016
1017static void sta_info_cleanup(unsigned long data)
1018{
1019 struct ieee80211_local *local = (struct ieee80211_local *) data;
1020 struct sta_info *sta;
Juuso Oikarinen3393a602010-04-19 10:12:52 +03001021 bool timer_needed = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001022
Johannes Bergd0709a62008-02-25 16:27:46 +01001023 rcu_read_lock();
1024 list_for_each_entry_rcu(sta, &local->sta_list, list)
Juuso Oikarinen3393a602010-04-19 10:12:52 +03001025 if (sta_info_cleanup_expire_buffered(local, sta))
1026 timer_needed = true;
Johannes Bergd0709a62008-02-25 16:27:46 +01001027 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001028
Johannes Berg5bb644a2009-05-17 11:40:42 +02001029 if (local->quiescing)
1030 return;
1031
Juuso Oikarinen3393a602010-04-19 10:12:52 +03001032 if (!timer_needed)
1033 return;
1034
Johannes Berg26d59532011-04-01 13:52:48 +02001035 mod_timer(&local->sta_cleanup,
1036 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
Jiri Bencf0706e82007-05-05 11:45:53 -07001037}
1038
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001039u32 sta_addr_hash(const void *key, u32 length, u32 seed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001040{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001041 return jhash(key, ETH_ALEN, seed);
1042}
1043
1044int sta_info_init(struct ieee80211_local *local)
1045{
1046 int err;
1047
1048 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
1049 if (err)
1050 return err;
1051
Johannes Berg4d339602011-12-15 11:24:20 +01001052 spin_lock_init(&local->tim_lock);
Johannes Berg34e89502010-02-03 13:59:58 +01001053 mutex_init(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07001054 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001055
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001056 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1057 (unsigned long)local);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001058 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001059}
1060
1061void sta_info_stop(struct ieee80211_local *local)
1062{
Johannes Berga56f9922012-12-13 23:08:52 +01001063 del_timer_sync(&local->sta_cleanup);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001064 rhashtable_destroy(&local->sta_hash);
Jiri Bencf0706e82007-05-05 11:45:53 -07001065}
1066
Johannes Berg051007d2012-12-13 23:49:02 +01001067
Johannes Berge7162512013-12-04 23:18:37 +01001068int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
Jiri Bencf0706e82007-05-05 11:45:53 -07001069{
Johannes Bergb998e8b2012-12-13 23:07:46 +01001070 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001071 struct sta_info *sta, *tmp;
Johannes Bergd7782072013-12-04 23:12:31 +01001072 LIST_HEAD(free_list);
Johannes Berg44213b52008-02-25 16:27:49 +01001073 int ret = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001074
Johannes Bergd0709a62008-02-25 16:27:46 +01001075 might_sleep();
1076
Johannes Berge7162512013-12-04 23:18:37 +01001077 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1078 WARN_ON(vlans && !sdata->bss);
1079
Johannes Berg34e89502010-02-03 13:59:58 +01001080 mutex_lock(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +01001081 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Johannes Berge7162512013-12-04 23:18:37 +01001082 if (sdata == sta->sdata ||
1083 (vlans && sdata->bss == sta->sdata->bss)) {
Johannes Bergd7782072013-12-04 23:12:31 +01001084 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1085 list_add(&sta->free_list, &free_list);
Johannes Berg34316832012-02-25 21:40:46 +01001086 ret++;
1087 }
Johannes Berg34e89502010-02-03 13:59:58 +01001088 }
Johannes Bergd7782072013-12-04 23:12:31 +01001089
1090 if (!list_empty(&free_list)) {
1091 synchronize_net();
1092 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1093 __sta_info_destroy_part2(sta);
1094 }
Johannes Berg34e89502010-02-03 13:59:58 +01001095 mutex_unlock(&local->sta_mtx);
Johannes Berg44213b52008-02-25 16:27:49 +01001096
Johannes Berg051007d2012-12-13 23:49:02 +01001097 return ret;
1098}
1099
Johannes Berg24723d12008-09-11 00:01:46 +02001100void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1101 unsigned long exp_time)
1102{
1103 struct ieee80211_local *local = sdata->local;
1104 struct sta_info *sta, *tmp;
Johannes Berg24723d12008-09-11 00:01:46 +02001105
Johannes Berg34e89502010-02-03 13:59:58 +01001106 mutex_lock(&local->sta_mtx);
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301107
1108 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Johannes Bergb8da6b62016-03-31 20:02:07 +03001109 unsigned long last_active = ieee80211_sta_last_active(sta);
1110
Marek Lindnerec2b7742011-12-20 23:16:52 +08001111 if (sdata != sta->sdata)
1112 continue;
1113
Johannes Bergb8da6b62016-03-31 20:02:07 +03001114 if (time_is_before_jiffies(last_active + exp_time)) {
Mohammed Shafi Shajakhaneea57d42012-10-08 21:33:47 +05301115 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1116 sta->sta.addr);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001117
1118 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1119 test_sta_flag(sta, WLAN_STA_PS_STA))
1120 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1121
Johannes Berg34e89502010-02-03 13:59:58 +01001122 WARN_ON(__sta_info_destroy(sta));
Johannes Berg24723d12008-09-11 00:01:46 +02001123 }
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301124 }
1125
Johannes Berg34e89502010-02-03 13:59:58 +01001126 mutex_unlock(&local->sta_mtx);
Johannes Berg24723d12008-09-11 00:01:46 +02001127}
Johannes Berg17741cd2008-09-11 00:02:02 +02001128
Ben Greear686b9cb2010-09-23 09:44:36 -07001129struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001130 const u8 *addr,
1131 const u8 *localaddr)
Johannes Berg17741cd2008-09-11 00:02:02 +02001132{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001133 struct ieee80211_local *local = hw_to_local(hw);
1134 struct sta_info *sta;
1135 struct rhash_head *tmp;
1136 const struct bucket_table *tbl;
1137
1138 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
Johannes Berg17741cd2008-09-11 00:02:02 +02001139
Ben Greear686b9cb2010-09-23 09:44:36 -07001140 /*
1141 * Just return a random station if localaddr is NULL
1142 * ... first in list.
1143 */
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001144 for_each_sta_info(local, tbl, addr, sta, tmp) {
Ben Greear686b9cb2010-09-23 09:44:36 -07001145 if (localaddr &&
Joe Perchesb203ca32012-05-08 18:56:52 +00001146 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
Ben Greear686b9cb2010-09-23 09:44:36 -07001147 continue;
Johannes Bergf7c65592010-04-30 13:48:36 +02001148 if (!sta->uploaded)
1149 return NULL;
Johannes Bergabe60632009-11-25 17:46:18 +01001150 return &sta->sta;
Johannes Bergf7c65592010-04-30 13:48:36 +02001151 }
1152
Johannes Bergabe60632009-11-25 17:46:18 +01001153 return NULL;
Johannes Berg17741cd2008-09-11 00:02:02 +02001154}
Ben Greear686b9cb2010-09-23 09:44:36 -07001155EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
Johannes Berg5ed176e2009-11-04 14:42:28 +01001156
1157struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1158 const u8 *addr)
1159{
Johannes Bergf7c65592010-04-30 13:48:36 +02001160 struct sta_info *sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001161
1162 if (!vif)
1163 return NULL;
1164
Johannes Bergf7c65592010-04-30 13:48:36 +02001165 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1166 if (!sta)
1167 return NULL;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001168
Johannes Bergf7c65592010-04-30 13:48:36 +02001169 if (!sta->uploaded)
1170 return NULL;
1171
1172 return &sta->sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001173}
Johannes Berg17741cd2008-09-11 00:02:02 +02001174EXPORT_SYMBOL(ieee80211_find_sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001175
Johannes Berge3685e02014-02-20 11:19:58 +01001176/* powersave support code */
1177void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
Johannes Berg50a94322010-11-16 11:50:28 -08001178{
Helmut Schaa608383b2012-01-30 15:18:00 +01001179 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berge3685e02014-02-20 11:19:58 +01001180 struct ieee80211_local *local = sdata->local;
1181 struct sk_buff_head pending;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001182 int filtered = 0, buffered = 0, ac, i;
Johannes Berge3685e02014-02-20 11:19:58 +01001183 unsigned long flags;
Marco Porschd012a602012-10-10 12:39:50 -07001184 struct ps_data *ps;
1185
Felix Fietkau3918edb2014-07-25 16:20:23 +02001186 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1187 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1188 u.ap);
1189
1190 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porschd012a602012-10-10 12:39:50 -07001191 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001192 else if (ieee80211_vif_is_mesh(&sdata->vif))
1193 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07001194 else
1195 return;
Johannes Berg50a94322010-11-16 11:50:28 -08001196
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001197 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg47086fc2011-09-29 16:04:33 +02001198
Johannes Berg5a306f52012-11-14 23:22:21 +01001199 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
Johannes Berg948d8872011-09-29 16:04:29 +02001200 sta->driver_buffered_tids = 0;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001201 sta->txq_buffered_tids = 0;
Johannes Berg948d8872011-09-29 16:04:29 +02001202
Johannes Berg30686bf2015-06-02 21:39:54 +02001203 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001204 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001205
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001206 if (sta->sta.txq[0]) {
1207 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1208 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1209
1210 if (!skb_queue_len(&txqi->queue))
1211 continue;
1212
1213 drv_wake_tx_queue(local, txqi);
1214 }
1215 }
1216
Johannes Berg948d8872011-09-29 16:04:29 +02001217 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001218
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001219 /* sync with ieee80211_tx_h_unicast_ps_buf */
1220 spin_lock(&sta->ps_lock);
Johannes Bergaf818582009-11-06 11:35:50 +01001221 /* Send all buffered frames to the station */
Johannes Berg948d8872011-09-29 16:04:29 +02001222 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1223 int count = skb_queue_len(&pending), tmp;
1224
Arik Nemtsov987c2852012-11-05 10:27:52 +02001225 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001226 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001227 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001228 tmp = skb_queue_len(&pending);
1229 filtered += tmp - count;
1230 count = tmp;
1231
Arik Nemtsov987c2852012-11-05 10:27:52 +02001232 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001233 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001234 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001235 tmp = skb_queue_len(&pending);
1236 buffered += tmp - count;
1237 }
1238
Johannes Berge3685e02014-02-20 11:19:58 +01001239 ieee80211_add_pending_skbs(local, &pending);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001240
1241 /* now we're no longer in the deliver code */
1242 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1243
1244 /* The station might have polled and then woken up before we responded,
1245 * so clear these flags now to avoid them sticking around.
1246 */
1247 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1248 clear_sta_flag(sta, WLAN_STA_UAPSD);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001249 spin_unlock(&sta->ps_lock);
Johannes Berg948d8872011-09-29 16:04:29 +02001250
Johannes Berge3685e02014-02-20 11:19:58 +01001251 atomic_dec(&ps->num_sta_ps);
1252
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001253 /* This station just woke up and isn't aware of our SMPS state */
Chun-Yeow Yeoh062f1d62014-04-22 18:19:25 +08001254 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1255 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001256 sdata->smps_mode) &&
1257 sta->known_smps_mode != sdata->bss->req_smps &&
1258 sta_info_tx_streams(sta) != 1) {
1259 ht_dbg(sdata,
1260 "%pM just woke up and MIMO capable - update SMPS\n",
1261 sta->sta.addr);
1262 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1263 sta->sta.addr,
1264 sdata->vif.bss_conf.bssid);
1265 }
1266
Johannes Bergaf818582009-11-06 11:35:50 +01001267 local->total_ps_buffered -= buffered;
1268
Johannes Bergc868cb352011-09-29 16:04:27 +02001269 sta_info_recalc_tim(sta);
1270
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001271 ps_dbg(sdata,
1272 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1273 sta->sta.addr, sta->sta.aid, filtered, buffered);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001274
1275 ieee80211_check_fast_xmit(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001276}
1277
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001278static void ieee80211_send_null_response(struct sta_info *sta, int tid,
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001279 enum ieee80211_frame_release_type reason,
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001280 bool call_driver, bool more_data)
Johannes Bergce662b442011-09-29 16:04:34 +02001281{
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001282 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Bergce662b442011-09-29 16:04:34 +02001283 struct ieee80211_local *local = sdata->local;
1284 struct ieee80211_qos_hdr *nullfunc;
1285 struct sk_buff *skb;
1286 int size = sizeof(*nullfunc);
1287 __le16 fc;
Johannes Berga74a8c82014-07-22 14:50:47 +02001288 bool qos = sta->sta.wme;
Johannes Bergce662b442011-09-29 16:04:34 +02001289 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02001290 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergce662b442011-09-29 16:04:34 +02001291
1292 if (qos) {
1293 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1294 IEEE80211_STYPE_QOS_NULLFUNC |
1295 IEEE80211_FCTL_FROMDS);
1296 } else {
1297 size -= 2;
1298 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1299 IEEE80211_STYPE_NULLFUNC |
1300 IEEE80211_FCTL_FROMDS);
1301 }
1302
1303 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1304 if (!skb)
1305 return;
1306
1307 skb_reserve(skb, local->hw.extra_tx_headroom);
1308
1309 nullfunc = (void *) skb_put(skb, size);
1310 nullfunc->frame_control = fc;
1311 nullfunc->duration_id = 0;
1312 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1313 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1314 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
Johannes Berg864a6042014-03-04 13:46:53 +01001315 nullfunc->seq_ctrl = 0;
Johannes Bergce662b442011-09-29 16:04:34 +02001316
Johannes Berg59b66252011-10-13 13:19:19 +02001317 skb->priority = tid;
1318 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
Johannes Bergce662b442011-09-29 16:04:34 +02001319 if (qos) {
Johannes Bergce662b442011-09-29 16:04:34 +02001320 nullfunc->qos_ctrl = cpu_to_le16(tid);
1321
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001322 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
Johannes Bergce662b442011-09-29 16:04:34 +02001323 nullfunc->qos_ctrl |=
1324 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001325 if (more_data)
1326 nullfunc->frame_control |=
1327 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1328 }
Johannes Bergce662b442011-09-29 16:04:34 +02001329 }
1330
1331 info = IEEE80211_SKB_CB(skb);
1332
1333 /*
1334 * Tell TX path to send this frame even though the
1335 * STA may still remain is PS mode after this frame
Johannes Bergdeeaee12011-09-29 16:04:35 +02001336 * exchange. Also set EOSP to indicate this packet
1337 * ends the poll/service period.
Johannes Bergce662b442011-09-29 16:04:34 +02001338 */
Johannes Berg02f2f1a2012-02-27 12:18:30 +01001339 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
Johannes Bergdeeaee12011-09-29 16:04:35 +02001340 IEEE80211_TX_STATUS_EOSP |
1341 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergce662b442011-09-29 16:04:34 +02001342
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301343 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1344
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001345 if (call_driver)
1346 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1347 reason, false);
Johannes Berg40b96402011-09-29 16:04:38 +02001348
Johannes Berg89afe612013-02-13 15:39:57 +01001349 skb->dev = sdata->dev;
1350
Johannes Berg55de9082012-07-26 17:24:39 +02001351 rcu_read_lock();
1352 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1353 if (WARN_ON(!chanctx_conf)) {
1354 rcu_read_unlock();
1355 kfree_skb(skb);
1356 return;
1357 }
1358
Johannes Berg73c4e192014-11-09 18:50:09 +02001359 info->band = chanctx_conf->def.chan->band;
Johannes Berg7c107702015-03-20 14:18:27 +01001360 ieee80211_xmit(sdata, sta, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001361 rcu_read_unlock();
Johannes Bergce662b442011-09-29 16:04:34 +02001362}
1363
Johannes Berg0a1cb802014-01-09 11:05:31 +01001364static int find_highest_prio_tid(unsigned long tids)
1365{
1366 /* lower 3 TIDs aren't ordered perfectly */
1367 if (tids & 0xF8)
1368 return fls(tids) - 1;
1369 /* TID 0 is BE just like TID 3 */
1370 if (tids & BIT(0))
1371 return 0;
1372 return fls(tids) - 1;
1373}
1374
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001375/* Indicates if the MORE_DATA bit should be set in the last
1376 * frame obtained by ieee80211_sta_ps_get_frames.
1377 * Note that driver_release_tids is relevant only if
1378 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1379 */
1380static bool
1381ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1382 enum ieee80211_frame_release_type reason,
1383 unsigned long driver_release_tids)
1384{
1385 int ac;
1386
1387 /* If the driver has data on more than one TID then
1388 * certainly there's more data if we release just a
1389 * single frame now (from a single TID). This will
1390 * only happen for PS-Poll.
1391 */
1392 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1393 hweight16(driver_release_tids) > 1)
1394 return true;
1395
1396 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1397 if (ignored_acs & BIT(ac))
1398 continue;
1399
1400 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1401 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1402 return true;
1403 }
1404
1405 return false;
1406}
1407
Johannes Berg47086fc2011-09-29 16:04:33 +02001408static void
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001409ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1410 enum ieee80211_frame_release_type reason,
1411 struct sk_buff_head *frames,
1412 unsigned long *driver_release_tids)
Johannes Bergaf818582009-11-06 11:35:50 +01001413{
1414 struct ieee80211_sub_if_data *sdata = sta->sdata;
1415 struct ieee80211_local *local = sdata->local;
Johannes Berg948d8872011-09-29 16:04:29 +02001416 int ac;
Johannes Bergaf818582009-11-06 11:35:50 +01001417
Johannes Bergf9f760b2014-01-08 17:45:07 +01001418 /* Get response frame(s) and more data bit for the last one. */
Johannes Berg948d8872011-09-29 16:04:29 +02001419 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg4049e092011-09-29 16:04:32 +02001420 unsigned long tids;
1421
Johannes Berg47086fc2011-09-29 16:04:33 +02001422 if (ignored_acs & BIT(ac))
Johannes Berg948d8872011-09-29 16:04:29 +02001423 continue;
1424
Johannes Berg4049e092011-09-29 16:04:32 +02001425 tids = ieee80211_tids_for_ac(ac);
1426
Johannes Bergf9f760b2014-01-08 17:45:07 +01001427 /* if we already have frames from software, then we can't also
1428 * release from hardware queues
1429 */
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001430 if (skb_queue_empty(frames)) {
1431 *driver_release_tids |=
1432 sta->driver_buffered_tids & tids;
1433 *driver_release_tids |= sta->txq_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001434 }
Johannes Berg47086fc2011-09-29 16:04:33 +02001435
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001436 if (!*driver_release_tids) {
Johannes Bergf9f760b2014-01-08 17:45:07 +01001437 struct sk_buff *skb;
1438
1439 while (n_frames > 0) {
1440 skb = skb_dequeue(&sta->tx_filtered[ac]);
1441 if (!skb) {
1442 skb = skb_dequeue(
1443 &sta->ps_tx_buf[ac]);
1444 if (skb)
1445 local->total_ps_buffered--;
1446 }
1447 if (!skb)
1448 break;
1449 n_frames--;
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001450 __skb_queue_tail(frames, skb);
Johannes Bergf9f760b2014-01-08 17:45:07 +01001451 }
Johannes Berg948d8872011-09-29 16:04:29 +02001452 }
1453
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001454 /* If we have more frames buffered on this AC, then abort the
1455 * loop since we can't send more data from other ACs before
1456 * the buffered frames from this.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001457 */
Johannes Berg948d8872011-09-29 16:04:29 +02001458 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001459 !skb_queue_empty(&sta->ps_tx_buf[ac]))
Johannes Berg948d8872011-09-29 16:04:29 +02001460 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001461 }
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001462}
1463
1464static void
1465ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1466 int n_frames, u8 ignored_acs,
1467 enum ieee80211_frame_release_type reason)
1468{
1469 struct ieee80211_sub_if_data *sdata = sta->sdata;
1470 struct ieee80211_local *local = sdata->local;
1471 unsigned long driver_release_tids = 0;
1472 struct sk_buff_head frames;
1473 bool more_data;
1474
1475 /* Service or PS-Poll period starts */
1476 set_sta_flag(sta, WLAN_STA_SP);
1477
1478 __skb_queue_head_init(&frames);
1479
1480 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1481 &frames, &driver_release_tids);
1482
1483 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1484
Emmanuel Grumbach1a570812015-12-20 13:50:00 +02001485 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001486 driver_release_tids =
1487 BIT(find_highest_prio_tid(driver_release_tids));
Johannes Bergaf818582009-11-06 11:35:50 +01001488
Johannes Bergf9f760b2014-01-08 17:45:07 +01001489 if (skb_queue_empty(&frames) && !driver_release_tids) {
Johannes Bergce662b442011-09-29 16:04:34 +02001490 int tid;
Johannes Berg4049e092011-09-29 16:04:32 +02001491
Johannes Bergce662b442011-09-29 16:04:34 +02001492 /*
1493 * For PS-Poll, this can only happen due to a race condition
1494 * when we set the TIM bit and the station notices it, but
1495 * before it can poll for the frame we expire it.
1496 *
1497 * For uAPSD, this is said in the standard (11.2.1.5 h):
1498 * At each unscheduled SP for a non-AP STA, the AP shall
1499 * attempt to transmit at least one MSDU or MMPDU, but no
1500 * more than the value specified in the Max SP Length field
1501 * in the QoS Capability element from delivery-enabled ACs,
1502 * that are destined for the non-AP STA.
1503 *
1504 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1505 */
1506
1507 /* This will evaluate to 1, 3, 5 or 7. */
1508 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1509
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001510 ieee80211_send_null_response(sta, tid, reason, true, false);
Johannes Bergf9f760b2014-01-08 17:45:07 +01001511 } else if (!driver_release_tids) {
Johannes Berg47086fc2011-09-29 16:04:33 +02001512 struct sk_buff_head pending;
1513 struct sk_buff *skb;
Johannes Berg40b96402011-09-29 16:04:38 +02001514 int num = 0;
1515 u16 tids = 0;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001516 bool need_null = false;
Johannes Bergaf818582009-11-06 11:35:50 +01001517
Johannes Berg47086fc2011-09-29 16:04:33 +02001518 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001519
Johannes Berg47086fc2011-09-29 16:04:33 +02001520 while ((skb = __skb_dequeue(&frames))) {
1521 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1522 struct ieee80211_hdr *hdr = (void *) skb->data;
Johannes Berg40b96402011-09-29 16:04:38 +02001523 u8 *qoshdr = NULL;
1524
1525 num++;
Johannes Bergaf818582009-11-06 11:35:50 +01001526
Johannes Berg47086fc2011-09-29 16:04:33 +02001527 /*
1528 * Tell TX path to send this frame even though the
1529 * STA may still remain is PS mode after this frame
1530 * exchange.
1531 */
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301532 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1533 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
Johannes Bergaf818582009-11-06 11:35:50 +01001534
Johannes Berg47086fc2011-09-29 16:04:33 +02001535 /*
1536 * Use MoreData flag to indicate whether there are
1537 * more buffered frames for this STA
1538 */
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001539 if (more_data || !skb_queue_empty(&frames))
Johannes Berg47086fc2011-09-29 16:04:33 +02001540 hdr->frame_control |=
1541 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001542 else
1543 hdr->frame_control &=
1544 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
Johannes Berg47086fc2011-09-29 16:04:33 +02001545
Johannes Berg40b96402011-09-29 16:04:38 +02001546 if (ieee80211_is_data_qos(hdr->frame_control) ||
1547 ieee80211_is_qos_nullfunc(hdr->frame_control))
1548 qoshdr = ieee80211_get_qos_ctl(hdr);
1549
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001550 tids |= BIT(skb->priority);
1551
1552 __skb_queue_tail(&pending, skb);
1553
1554 /* end service period after last frame or add one */
1555 if (!skb_queue_empty(&frames))
1556 continue;
1557
1558 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1559 /* for PS-Poll, there's only one frame */
1560 info->flags |= IEEE80211_TX_STATUS_EOSP |
1561 IEEE80211_TX_CTL_REQ_TX_STATUS;
1562 break;
1563 }
1564
1565 /* For uAPSD, things are a bit more complicated. If the
1566 * last frame has a QoS header (i.e. is a QoS-data or
1567 * QoS-nulldata frame) then just set the EOSP bit there
1568 * and be done.
1569 * If the frame doesn't have a QoS header (which means
1570 * it should be a bufferable MMPDU) then we can't set
1571 * the EOSP bit in the QoS header; add a QoS-nulldata
1572 * frame to the list to send it after the MMPDU.
1573 *
1574 * Note that this code is only in the mac80211-release
1575 * code path, we assume that the driver will not buffer
1576 * anything but QoS-data frames, or if it does, will
1577 * create the QoS-nulldata frame by itself if needed.
1578 *
1579 * Cf. 802.11-2012 10.2.1.10 (c).
1580 */
1581 if (qoshdr) {
1582 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
Johannes Berg47086fc2011-09-29 16:04:33 +02001583
Marco Porsch52a3f202012-03-16 15:30:26 +01001584 info->flags |= IEEE80211_TX_STATUS_EOSP |
1585 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001586 } else {
1587 /* The standard isn't completely clear on this
1588 * as it says the more-data bit should be set
1589 * if there are more BUs. The QoS-Null frame
1590 * we're about to send isn't buffered yet, we
1591 * only create it below, but let's pretend it
1592 * was buffered just in case some clients only
1593 * expect more-data=0 when eosp=1.
1594 */
1595 hdr->frame_control |=
1596 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1597 need_null = true;
1598 num++;
Marco Porsch52a3f202012-03-16 15:30:26 +01001599 }
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001600 break;
Johannes Berg47086fc2011-09-29 16:04:33 +02001601 }
1602
Johannes Berg40b96402011-09-29 16:04:38 +02001603 drv_allow_buffered_frames(local, sta, tids, num,
1604 reason, more_data);
1605
Johannes Berg47086fc2011-09-29 16:04:33 +02001606 ieee80211_add_pending_skbs(local, &pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001607
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001608 if (need_null)
1609 ieee80211_send_null_response(
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001610 sta, find_highest_prio_tid(tids),
1611 reason, false, false);
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001612
Johannes Bergc868cb352011-09-29 16:04:27 +02001613 sta_info_recalc_tim(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001614 } else {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001615 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1616 int tid;
1617
Johannes Bergaf818582009-11-06 11:35:50 +01001618 /*
Johannes Berg4049e092011-09-29 16:04:32 +02001619 * We need to release a frame that is buffered somewhere in the
1620 * driver ... it'll have to handle that.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001621 * Note that the driver also has to check the number of frames
1622 * on the TIDs we're releasing from - if there are more than
1623 * n_frames it has to set the more-data bit (if we didn't ask
1624 * it to set it anyway due to other buffered frames); if there
1625 * are fewer than n_frames it has to make sure to adjust that
1626 * to allow the service period to end properly.
Johannes Bergaf818582009-11-06 11:35:50 +01001627 */
Johannes Berg4049e092011-09-29 16:04:32 +02001628 drv_release_buffered_frames(local, sta, driver_release_tids,
Johannes Berg47086fc2011-09-29 16:04:33 +02001629 n_frames, reason, more_data);
Johannes Berg4049e092011-09-29 16:04:32 +02001630
1631 /*
1632 * Note that we don't recalculate the TIM bit here as it would
1633 * most likely have no effect at all unless the driver told us
Johannes Bergf9f760b2014-01-08 17:45:07 +01001634 * that the TID(s) became empty before returning here from the
Johannes Berg4049e092011-09-29 16:04:32 +02001635 * release function.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001636 * Either way, however, when the driver tells us that the TID(s)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001637 * became empty or we find that a txq became empty, we'll do the
1638 * TIM recalculation.
Johannes Berg4049e092011-09-29 16:04:32 +02001639 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001640
1641 if (!sta->sta.txq[0])
1642 return;
1643
1644 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1645 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1646
1647 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1648 continue;
1649
1650 sta_info_recalc_tim(sta);
1651 break;
1652 }
Johannes Bergaf818582009-11-06 11:35:50 +01001653 }
1654}
1655
Johannes Bergaf818582009-11-06 11:35:50 +01001656void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1657{
Johannes Berg47086fc2011-09-29 16:04:33 +02001658 u8 ignore_for_response = sta->sta.uapsd_queues;
Johannes Bergaf818582009-11-06 11:35:50 +01001659
Johannes Berg47086fc2011-09-29 16:04:33 +02001660 /*
1661 * If all ACs are delivery-enabled then we should reply
1662 * from any of them, if only some are enabled we reply
1663 * only from the non-enabled ones.
1664 */
1665 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1666 ignore_for_response = 0;
1667
1668 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1669 IEEE80211_FRAME_RELEASE_PSPOLL);
1670}
1671
1672void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1673{
1674 int n_frames = sta->sta.max_sp;
1675 u8 delivery_enabled = sta->sta.uapsd_queues;
1676
1677 /*
1678 * If we ever grow support for TSPEC this might happen if
1679 * the TSPEC update from hostapd comes in between a trigger
1680 * frame setting WLAN_STA_UAPSD in the RX path and this
1681 * actually getting called.
1682 */
1683 if (!delivery_enabled)
1684 return;
1685
Johannes Berg47086fc2011-09-29 16:04:33 +02001686 switch (sta->sta.max_sp) {
1687 case 1:
1688 n_frames = 2;
1689 break;
1690 case 2:
1691 n_frames = 4;
1692 break;
1693 case 3:
1694 n_frames = 6;
1695 break;
1696 case 0:
1697 /* XXX: what is a good value? */
Andrei Otcheretianski13a80982014-11-04 11:33:04 +02001698 n_frames = 128;
Johannes Berg47086fc2011-09-29 16:04:33 +02001699 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001700 }
Johannes Bergaf818582009-11-06 11:35:50 +01001701
Johannes Berg47086fc2011-09-29 16:04:33 +02001702 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1703 IEEE80211_FRAME_RELEASE_UAPSD);
Johannes Bergaf818582009-11-06 11:35:50 +01001704}
1705
1706void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1707 struct ieee80211_sta *pubsta, bool block)
1708{
1709 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1710
Johannes Bergb5878a22010-04-07 16:48:40 +02001711 trace_api_sta_block_awake(sta->local, pubsta, block);
1712
Johannes Berg5ac2e352014-05-27 16:32:27 +02001713 if (block) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001714 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001715 ieee80211_clear_fast_xmit(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001716 return;
1717 }
1718
1719 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1720 return;
1721
1722 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1723 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1724 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1725 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1726 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1727 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1728 /* must be asleep in this case */
1729 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1730 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1731 } else {
1732 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001733 ieee80211_check_fast_xmit(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001734 }
Johannes Bergaf818582009-11-06 11:35:50 +01001735}
1736EXPORT_SYMBOL(ieee80211_sta_block_awake);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001737
Johannes Berge9437892013-02-15 21:38:08 +01001738void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
Johannes Berg37fbd902011-09-29 16:04:39 +02001739{
1740 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1741 struct ieee80211_local *local = sta->local;
Johannes Berg37fbd902011-09-29 16:04:39 +02001742
1743 trace_api_eosp(local, pubsta);
1744
Johannes Berge9437892013-02-15 21:38:08 +01001745 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg37fbd902011-09-29 16:04:39 +02001746}
Johannes Berge9437892013-02-15 21:38:08 +01001747EXPORT_SYMBOL(ieee80211_sta_eosp);
Johannes Berg37fbd902011-09-29 16:04:39 +02001748
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001749void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1750{
1751 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1752 enum ieee80211_frame_release_type reason;
1753 bool more_data;
1754
1755 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1756
1757 reason = IEEE80211_FRAME_RELEASE_UAPSD;
1758 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1759 reason, 0);
1760
1761 ieee80211_send_null_response(sta, tid, reason, false, more_data);
1762}
1763EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1764
Johannes Berg042ec452011-09-29 16:04:26 +02001765void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1766 u8 tid, bool buffered)
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001767{
1768 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1769
Johannes Berg5a306f52012-11-14 23:22:21 +01001770 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
Johannes Berg042ec452011-09-29 16:04:26 +02001771 return;
1772
Johannes Berg1b000782013-12-19 10:47:48 +01001773 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1774
Johannes Berg948d8872011-09-29 16:04:29 +02001775 if (buffered)
1776 set_bit(tid, &sta->driver_buffered_tids);
1777 else
1778 clear_bit(tid, &sta->driver_buffered_tids);
1779
Johannes Bergc868cb352011-09-29 16:04:27 +02001780 sta_info_recalc_tim(sta);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001781}
Johannes Berg042ec452011-09-29 16:04:26 +02001782EXPORT_SYMBOL(ieee80211_sta_set_buffered);
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001783
Ayala Beker52cfa1d2016-03-17 15:41:39 +02001784static void
1785ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
1786{
1787 struct ieee80211_local *local = sdata->local;
1788 bool allow_p2p_go_ps = sdata->vif.p2p;
1789 struct sta_info *sta;
1790
1791 rcu_read_lock();
1792 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1793 if (sdata != sta->sdata ||
1794 !test_sta_flag(sta, WLAN_STA_ASSOC))
1795 continue;
1796 if (!sta->sta.support_p2p_ps) {
1797 allow_p2p_go_ps = false;
1798 break;
1799 }
1800 }
1801 rcu_read_unlock();
1802
1803 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
1804 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
1805 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
1806 }
1807}
1808
Johannes Berg83d5cc02012-01-12 09:31:10 +01001809int sta_info_move_state(struct sta_info *sta,
1810 enum ieee80211_sta_state new_state)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001811{
Johannes Berg8bf11d82011-12-15 11:17:37 +01001812 might_sleep();
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001813
1814 if (sta->sta_state == new_state)
1815 return 0;
1816
Johannes Bergf09603a2012-01-20 13:55:21 +01001817 /* check allowed transitions first */
1818
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001819 switch (new_state) {
1820 case IEEE80211_STA_NONE:
Johannes Bergf09603a2012-01-20 13:55:21 +01001821 if (sta->sta_state != IEEE80211_STA_AUTH)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001822 return -EINVAL;
1823 break;
1824 case IEEE80211_STA_AUTH:
Johannes Bergf09603a2012-01-20 13:55:21 +01001825 if (sta->sta_state != IEEE80211_STA_NONE &&
1826 sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001827 return -EINVAL;
1828 break;
1829 case IEEE80211_STA_ASSOC:
Johannes Bergf09603a2012-01-20 13:55:21 +01001830 if (sta->sta_state != IEEE80211_STA_AUTH &&
1831 sta->sta_state != IEEE80211_STA_AUTHORIZED)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001832 return -EINVAL;
1833 break;
1834 case IEEE80211_STA_AUTHORIZED:
Johannes Bergf09603a2012-01-20 13:55:21 +01001835 if (sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001836 return -EINVAL;
1837 break;
1838 default:
1839 WARN(1, "invalid state %d", new_state);
1840 return -EINVAL;
1841 }
1842
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001843 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1844 sta->sta.addr, new_state);
Johannes Bergf09603a2012-01-20 13:55:21 +01001845
1846 /*
1847 * notify the driver before the actual changes so it can
1848 * fail the transition
1849 */
1850 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1851 int err = drv_sta_state(sta->local, sta->sdata, sta,
1852 sta->sta_state, new_state);
1853 if (err)
1854 return err;
1855 }
1856
1857 /* reflect the change in all state variables */
1858
1859 switch (new_state) {
1860 case IEEE80211_STA_NONE:
1861 if (sta->sta_state == IEEE80211_STA_AUTH)
1862 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1863 break;
1864 case IEEE80211_STA_AUTH:
Ilan Peera7201a62015-12-13 13:41:43 +02001865 if (sta->sta_state == IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +01001866 set_bit(WLAN_STA_AUTH, &sta->_flags);
Ilan Peera7201a62015-12-13 13:41:43 +02001867 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
Johannes Bergf09603a2012-01-20 13:55:21 +01001868 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
Ilan Peera7201a62015-12-13 13:41:43 +02001869 ieee80211_recalc_min_chandef(sta->sdata);
Ayala Beker52cfa1d2016-03-17 15:41:39 +02001870 if (!sta->sta.support_p2p_ps)
1871 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
Ilan Peera7201a62015-12-13 13:41:43 +02001872 }
Johannes Bergf09603a2012-01-20 13:55:21 +01001873 break;
1874 case IEEE80211_STA_ASSOC:
1875 if (sta->sta_state == IEEE80211_STA_AUTH) {
1876 set_bit(WLAN_STA_ASSOC, &sta->_flags);
Ilan Peera7201a62015-12-13 13:41:43 +02001877 ieee80211_recalc_min_chandef(sta->sdata);
Ayala Beker52cfa1d2016-03-17 15:41:39 +02001878 if (!sta->sta.support_p2p_ps)
1879 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
Johannes Bergf09603a2012-01-20 13:55:21 +01001880 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001881 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1882 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1883 !sta->sdata->u.vlan.sta))
1884 atomic_dec(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001885 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001886 ieee80211_clear_fast_xmit(sta);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03001887 ieee80211_clear_fast_rx(sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001888 }
1889 break;
1890 case IEEE80211_STA_AUTHORIZED:
1891 if (sta->sta_state == IEEE80211_STA_ASSOC) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001892 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1893 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1894 !sta->sdata->u.vlan.sta))
1895 atomic_inc(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001896 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001897 ieee80211_check_fast_xmit(sta);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03001898 ieee80211_check_fast_rx(sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001899 }
1900 break;
1901 default:
1902 break;
1903 }
1904
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001905 sta->sta_state = new_state;
1906
1907 return 0;
1908}
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001909
1910u8 sta_info_tx_streams(struct sta_info *sta)
1911{
1912 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1913 u8 rx_streams;
1914
1915 if (!sta->sta.ht_cap.ht_supported)
1916 return 1;
1917
1918 if (sta->sta.vht_cap.vht_supported) {
1919 int i;
1920 u16 tx_mcs_map =
1921 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1922
1923 for (i = 7; i >= 0; i--)
1924 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1925 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1926 return i + 1;
1927 }
1928
1929 if (ht_cap->mcs.rx_mask[3])
1930 rx_streams = 4;
1931 else if (ht_cap->mcs.rx_mask[2])
1932 rx_streams = 3;
1933 else if (ht_cap->mcs.rx_mask[1])
1934 rx_streams = 2;
1935 else
1936 rx_streams = 1;
1937
1938 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1939 return rx_streams;
1940
1941 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1942 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1943}
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001944
Johannes Bergc9c59622016-03-31 20:02:11 +03001945static struct ieee80211_sta_rx_stats *
1946sta_get_last_rx_stats(struct sta_info *sta)
1947{
1948 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
1949 struct ieee80211_local *local = sta->local;
1950 int cpu;
1951
1952 if (!ieee80211_hw_check(&local->hw, USES_RSS))
1953 return stats;
1954
1955 for_each_possible_cpu(cpu) {
1956 struct ieee80211_sta_rx_stats *cpustats;
1957
1958 cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
1959
1960 if (time_after(cpustats->last_rx, stats->last_rx))
1961 stats = cpustats;
1962 }
1963
1964 return stats;
1965}
1966
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001967static void sta_stats_decode_rate(struct ieee80211_local *local, u16 rate,
1968 struct rate_info *rinfo)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001969{
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001970 rinfo->bw = (rate & STA_STATS_RATE_BW_MASK) >>
1971 STA_STATS_RATE_BW_SHIFT;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001972
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001973 if (rate & STA_STATS_RATE_VHT) {
1974 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
1975 rinfo->mcs = rate & 0xf;
1976 rinfo->nss = (rate & 0xf0) >> 4;
1977 } else if (rate & STA_STATS_RATE_HT) {
1978 rinfo->flags = RATE_INFO_FLAGS_MCS;
1979 rinfo->mcs = rate & 0xff;
1980 } else if (rate & STA_STATS_RATE_LEGACY) {
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001981 struct ieee80211_supported_band *sband;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001982 u16 brate;
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001983 unsigned int shift;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001984
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001985 sband = local->hw.wiphy->bands[(rate >> 4) & 0xf];
1986 brate = sband->bitrates[rate & 0xf].bitrate;
1987 if (rinfo->bw == RATE_INFO_BW_5)
1988 shift = 2;
1989 else if (rinfo->bw == RATE_INFO_BW_10)
1990 shift = 1;
1991 else
1992 shift = 0;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001993 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
1994 }
1995
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001996 if (rate & STA_STATS_RATE_SGI)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001997 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001998}
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001999
Johannes Berg4f6b1b32016-03-31 20:02:08 +03002000static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2001{
Johannes Bergc9c59622016-03-31 20:02:11 +03002002 u16 rate = ACCESS_ONCE(sta_get_last_rx_stats(sta)->last_rate);
Johannes Berg4f6b1b32016-03-31 20:02:08 +03002003
2004 if (rate == STA_STATS_RATE_INVALID)
2005 rinfo->flags = 0;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02002006 else
Johannes Berg4f6b1b32016-03-31 20:02:08 +03002007 sta_stats_decode_rate(sta->local, rate, rinfo);
Johannes Bergfbd6ff52015-10-14 18:31:30 +02002008}
2009
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002010static void sta_set_tidstats(struct sta_info *sta,
2011 struct cfg80211_tid_stats *tidstats,
2012 int tid)
2013{
2014 struct ieee80211_local *local = sta->local;
2015
2016 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2017 unsigned int start;
2018
2019 do {
2020 start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
2021 tidstats->rx_msdu = sta->rx_stats.msdu[tid];
2022 } while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
2023
2024 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2025 }
2026
2027 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2028 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2029 tidstats->tx_msdu = sta->tx_stats.msdu[tid];
2030 }
2031
2032 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2033 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2034 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2035 tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
2036 }
2037
2038 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2039 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2040 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2041 tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
2042 }
2043}
2044
Johannes Bergc9c59622016-03-31 20:02:11 +03002045static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2046{
2047 unsigned int start;
2048 u64 value;
2049
2050 do {
2051 start = u64_stats_fetch_begin(&rxstats->syncp);
2052 value = rxstats->bytes;
2053 } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2054
2055 return value;
2056}
2057
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002058void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
2059{
2060 struct ieee80211_sub_if_data *sdata = sta->sdata;
2061 struct ieee80211_local *local = sdata->local;
John W. Linville9a244402014-07-25 10:22:36 -04002062 struct rate_control_ref *ref = NULL;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002063 u32 thr = 0;
Johannes Bergc9c59622016-03-31 20:02:11 +03002064 int i, ac, cpu;
2065 struct ieee80211_sta_rx_stats *last_rxstats;
2066
2067 last_rxstats = sta_get_last_rx_stats(sta);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002068
John W. Linville9a244402014-07-25 10:22:36 -04002069 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2070 ref = local->rate_ctrl;
2071
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002072 sinfo->generation = sdata->local->sta_generation;
2073
Johannes Berg225b8182015-01-21 21:09:02 +01002074 /* do before driver, so beacon filtering drivers have a
2075 * chance to e.g. just add the number of filtered beacons
2076 * (or just modify the value entirely, of course)
2077 */
2078 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2079 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2080
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002081 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2082
Johannes Berg319090b2014-11-17 14:08:11 +01002083 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
2084 BIT(NL80211_STA_INFO_STA_FLAGS) |
2085 BIT(NL80211_STA_INFO_BSS_PARAM) |
2086 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
Johannes Berg976bd9e2015-10-16 17:18:11 +02002087 BIT(NL80211_STA_INFO_RX_DROP_MISC);
2088
2089 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2090 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2091 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
2092 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002093
Arnd Bergmann84b00602015-09-30 13:26:36 +02002094 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002095 sinfo->inactive_time =
Johannes Bergb8da6b62016-03-31 20:02:07 +03002096 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002097
Johannes Berg319090b2014-11-17 14:08:11 +01002098 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
2099 BIT(NL80211_STA_INFO_TX_BYTES)))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002100 sinfo->tx_bytes = 0;
2101 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002102 sinfo->tx_bytes += sta->tx_stats.bytes[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01002103 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002104 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002105
Johannes Berg319090b2014-11-17 14:08:11 +01002106 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002107 sinfo->tx_packets = 0;
2108 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002109 sinfo->tx_packets += sta->tx_stats.packets[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01002110 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002111 }
2112
Johannes Berg319090b2014-11-17 14:08:11 +01002113 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
2114 BIT(NL80211_STA_INFO_RX_BYTES)))) {
Johannes Bergc9c59622016-03-31 20:02:11 +03002115 sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002116
Johannes Bergc9c59622016-03-31 20:02:11 +03002117 if (sta->pcpu_rx_stats) {
2118 for_each_possible_cpu(cpu) {
2119 struct ieee80211_sta_rx_stats *cpurxs;
2120
2121 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2122 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2123 }
2124 }
2125
Johannes Berg319090b2014-11-17 14:08:11 +01002126 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002127 }
2128
Johannes Berg319090b2014-11-17 14:08:11 +01002129 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002130 sinfo->rx_packets = sta->rx_stats.packets;
Johannes Bergc9c59622016-03-31 20:02:11 +03002131 if (sta->pcpu_rx_stats) {
2132 for_each_possible_cpu(cpu) {
2133 struct ieee80211_sta_rx_stats *cpurxs;
2134
2135 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2136 sinfo->rx_packets += cpurxs->packets;
2137 }
2138 }
Johannes Berg319090b2014-11-17 14:08:11 +01002139 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002140 }
2141
Johannes Berg319090b2014-11-17 14:08:11 +01002142 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002143 sinfo->tx_retries = sta->status_stats.retry_count;
Johannes Berg319090b2014-11-17 14:08:11 +01002144 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002145 }
2146
Johannes Berg319090b2014-11-17 14:08:11 +01002147 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002148 sinfo->tx_failed = sta->status_stats.retry_failed;
Johannes Berg319090b2014-11-17 14:08:11 +01002149 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002150 }
2151
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002152 sinfo->rx_dropped_misc = sta->rx_stats.dropped;
Johannes Bergc9c59622016-03-31 20:02:11 +03002153 if (sta->pcpu_rx_stats) {
2154 for_each_possible_cpu(cpu) {
2155 struct ieee80211_sta_rx_stats *cpurxs;
2156
2157 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2158 sinfo->rx_packets += cpurxs->dropped;
2159 }
2160 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002161
Johannes Berg225b8182015-01-21 21:09:02 +01002162 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2163 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2164 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
2165 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2166 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2167 }
2168
Johannes Berg30686bf2015-06-02 21:39:54 +02002169 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2170 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
Johannes Berg319090b2014-11-17 14:08:11 +01002171 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
Johannes Bergc9c59622016-03-31 20:02:11 +03002172 sinfo->signal = (s8)last_rxstats->last_signal;
Johannes Berg319090b2014-11-17 14:08:11 +01002173 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002174 }
2175
Johannes Bergc9c59622016-03-31 20:02:11 +03002176 if (!sta->pcpu_rx_stats &&
2177 !(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
Johannes Berg40d9a382015-07-13 12:26:46 +02002178 sinfo->signal_avg =
Johannes Berg0be6ed12016-03-31 20:02:05 +03002179 -ewma_signal_read(&sta->rx_stats_avg.signal);
Johannes Berg319090b2014-11-17 14:08:11 +01002180 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002181 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002182 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002183
Johannes Bergc9c59622016-03-31 20:02:11 +03002184 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2185 * the sta->rx_stats struct, so the check here is fine with and without
2186 * pcpu statistics
2187 */
2188 if (last_rxstats->chains &&
Johannes Berg319090b2014-11-17 14:08:11 +01002189 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2190 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
Johannes Bergc9c59622016-03-31 20:02:11 +03002191 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL);
2192 if (!sta->pcpu_rx_stats)
2193 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002194
Johannes Bergc9c59622016-03-31 20:02:11 +03002195 sinfo->chains = last_rxstats->chains;
2196
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002197 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002198 sinfo->chain_signal[i] =
Johannes Bergc9c59622016-03-31 20:02:11 +03002199 last_rxstats->chain_signal_last[i];
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002200 sinfo->chain_signal_avg[i] =
Johannes Berg0be6ed12016-03-31 20:02:05 +03002201 -ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002202 }
2203 }
2204
Johannes Berg319090b2014-11-17 14:08:11 +01002205 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002206 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2207 &sinfo->txrate);
Johannes Berg319090b2014-11-17 14:08:11 +01002208 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002209 }
2210
Johannes Berg319090b2014-11-17 14:08:11 +01002211 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002212 sta_set_rate_info_rx(sta, &sinfo->rxrate);
Johannes Berg319090b2014-11-17 14:08:11 +01002213 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002214 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002215
Johannes Berg79c892b2014-11-21 14:26:31 +01002216 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
2217 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
2218 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
2219
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002220 sta_set_tidstats(sta, tidstats, i);
Johannes Berg79c892b2014-11-21 14:26:31 +01002221 }
2222
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002223 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2224#ifdef CONFIG_MAC80211_MESH
Johannes Berg319090b2014-11-17 14:08:11 +01002225 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2226 BIT(NL80211_STA_INFO_PLID) |
2227 BIT(NL80211_STA_INFO_PLINK_STATE) |
2228 BIT(NL80211_STA_INFO_LOCAL_PM) |
2229 BIT(NL80211_STA_INFO_PEER_PM) |
2230 BIT(NL80211_STA_INFO_NONPEER_PM);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002231
Johannes Berg433f5bc2015-06-17 10:31:00 +02002232 sinfo->llid = sta->mesh->llid;
2233 sinfo->plid = sta->mesh->plid;
2234 sinfo->plink_state = sta->mesh->plink_state;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002235 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
Johannes Berg319090b2014-11-17 14:08:11 +01002236 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
Johannes Berg433f5bc2015-06-17 10:31:00 +02002237 sinfo->t_offset = sta->mesh->t_offset;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002238 }
Johannes Berg433f5bc2015-06-17 10:31:00 +02002239 sinfo->local_pm = sta->mesh->local_pm;
2240 sinfo->peer_pm = sta->mesh->peer_pm;
2241 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002242#endif
2243 }
2244
2245 sinfo->bss_param.flags = 0;
2246 if (sdata->vif.bss_conf.use_cts_prot)
2247 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2248 if (sdata->vif.bss_conf.use_short_preamble)
2249 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2250 if (sdata->vif.bss_conf.use_short_slot)
2251 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
Emmanuel Grumbach785e21a2014-09-03 15:25:04 +03002252 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002253 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2254
2255 sinfo->sta_flags.set = 0;
2256 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2257 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2258 BIT(NL80211_STA_FLAG_WME) |
2259 BIT(NL80211_STA_FLAG_MFP) |
2260 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2261 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2262 BIT(NL80211_STA_FLAG_TDLS_PEER);
2263 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2264 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2265 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2266 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
Johannes Berga74a8c82014-07-22 14:50:47 +02002267 if (sta->sta.wme)
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002268 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2269 if (test_sta_flag(sta, WLAN_STA_MFP))
2270 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2271 if (test_sta_flag(sta, WLAN_STA_AUTH))
2272 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2273 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2274 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2275 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2276 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2277
2278 /* check if the driver has a SW RC implementation */
2279 if (ref && ref->ops->get_expected_throughput)
2280 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2281 else
2282 thr = drv_get_expected_throughput(local, &sta->sta);
2283
2284 if (thr != 0) {
Johannes Berg319090b2014-11-17 14:08:11 +01002285 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002286 sinfo->expected_throughput = thr;
2287 }
2288}
Johannes Bergb8da6b62016-03-31 20:02:07 +03002289
2290unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2291{
Johannes Bergc9c59622016-03-31 20:02:11 +03002292 struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2293
2294 if (time_after(stats->last_rx, sta->status_stats.last_ack))
2295 return stats->last_rx;
Johannes Bergb8da6b62016-03-31 20:02:07 +03002296 return sta->status_stats.last_ack;
2297}