blob: a0ce7e40f420850066809c33b811134cc62d8436 [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 Kaziorf2ac7e302016-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 Berg93e5deb2008-04-01 15:21:00 +0200257 kfree(sta);
258}
259
Johannes Berg4d339602011-12-15 11:24:20 +0100260/* Caller must hold local->sta_mtx */
Johannes Bergd0709a62008-02-25 16:27:46 +0100261static void sta_info_hash_add(struct ieee80211_local *local,
262 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700263{
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100264 rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
265 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -0700266}
Jiri Bencf0706e82007-05-05 11:45:53 -0700267
Johannes Berg5ac2e352014-05-27 16:32:27 +0200268static void sta_deliver_ps_frames(struct work_struct *wk)
Johannes Bergaf818582009-11-06 11:35:50 +0100269{
270 struct sta_info *sta;
271
Johannes Berg5ac2e352014-05-27 16:32:27 +0200272 sta = container_of(wk, struct sta_info, drv_deliver_wk);
Johannes Bergaf818582009-11-06 11:35:50 +0100273
274 if (sta->dead)
275 return;
276
Johannes Berg5ac2e352014-05-27 16:32:27 +0200277 local_bh_disable();
278 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
Johannes Bergaf818582009-11-06 11:35:50 +0100279 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200280 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
Johannes Bergaf818582009-11-06 11:35:50 +0100281 ieee80211_sta_ps_deliver_poll_response(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200282 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
Johannes Berg47086fc2011-09-29 16:04:33 +0200283 ieee80211_sta_ps_deliver_uapsd(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200284 local_bh_enable();
Johannes Bergaf818582009-11-06 11:35:50 +0100285}
286
Johannes Bergaf65cd962009-11-17 18:18:36 +0100287static int sta_prepare_rate_control(struct ieee80211_local *local,
288 struct sta_info *sta, gfp_t gfp)
289{
Johannes Berg30686bf2015-06-02 21:39:54 +0200290 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
Johannes Bergaf65cd962009-11-17 18:18:36 +0100291 return 0;
292
Johannes Berg889cbb92012-01-17 10:33:29 +0100293 sta->rate_ctrl = local->rate_ctrl;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100294 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
Johannes Berg35c347a2015-03-05 16:10:08 +0100295 sta, gfp);
Johannes Berg889cbb92012-01-17 10:33:29 +0100296 if (!sta->rate_ctrl_priv)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100297 return -ENOMEM;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100298
299 return 0;
300}
301
Johannes Berg73651ee2008-02-25 16:27:47 +0100302struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
Johannes Berg56544162011-12-14 13:28:46 +0100303 const u8 *addr, gfp_t gfp)
Jiri Bencf0706e82007-05-05 11:45:53 -0700304{
Johannes Bergd0709a62008-02-25 16:27:46 +0100305 struct ieee80211_local *local = sdata->local;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100306 struct ieee80211_hw *hw = &local->hw;
Jiri Bencf0706e82007-05-05 11:45:53 -0700307 struct sta_info *sta;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200308 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700309
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100310 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
Jiri Bencf0706e82007-05-05 11:45:53 -0700311 if (!sta)
Johannes Berg73651ee2008-02-25 16:27:47 +0100312 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700313
Johannes Berg07346f812008-05-03 01:02:02 +0200314 spin_lock_init(&sta->lock);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200315 spin_lock_init(&sta->ps_lock);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200316 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
Johannes Berg67c282c2010-06-10 10:21:43 +0200317 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
Johannes Berga93e3642010-06-10 10:21:46 +0200318 mutex_init(&sta->ampdu_mlme.mtx);
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800319#ifdef CONFIG_MAC80211_MESH
Johannes Berg433f5bc2015-06-17 10:31:00 +0200320 if (ieee80211_vif_is_mesh(&sdata->vif)) {
321 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
322 if (!sta->mesh)
323 goto free;
324 spin_lock_init(&sta->mesh->plink_lock);
325 if (ieee80211_vif_is_mesh(&sdata->vif) &&
326 !sdata->u.mesh.user_mpm)
327 init_timer(&sta->mesh->plink_timer);
328 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
329 }
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800330#endif
Johannes Berg07346f812008-05-03 01:02:02 +0200331
Johannes Bergac100ce2015-06-16 16:22:12 +0200332 memcpy(sta->addr, addr, ETH_ALEN);
Johannes Berg17741cd2008-09-11 00:02:02 +0200333 memcpy(sta->sta.addr, addr, ETH_ALEN);
Johannes Bergd0709a62008-02-25 16:27:46 +0100334 sta->local = local;
335 sta->sdata = sdata;
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200336 sta->rx_stats.last_rx = jiffies;
Jiri Bencf0706e82007-05-05 11:45:53 -0700337
Johannes Berg0f9c5a62016-03-31 20:02:09 +0300338 u64_stats_init(&sta->rx_stats.syncp);
339
Johannes Berg71ec3752012-01-20 13:55:20 +0100340 sta->sta_state = IEEE80211_STA_NONE;
341
Liad Kaufmanb6da9112014-11-19 13:47:38 +0200342 /* Mark TID as unreserved */
343 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
344
Arnd Bergmann84b00602015-09-30 13:26:36 +0200345 sta->last_connected = ktime_get_seconds();
Johannes Berg0be6ed12016-03-31 20:02:05 +0300346 ewma_signal_init(&sta->rx_stats_avg.signal);
347 for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
348 ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
Bruno Randolf541a45a2010-12-02 19:12:43 +0900349
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100350 if (local->ops->wake_tx_queue) {
351 void *txq_data;
352 int size = sizeof(struct txq_info) +
353 ALIGN(hw->txq_data_size, sizeof(void *));
354
355 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
356 if (!txq_data)
357 goto free;
358
359 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
360 struct txq_info *txq = txq_data + i * size;
361
362 ieee80211_init_tx_queue(sdata, sta, txq, i);
363 }
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100364 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700365
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100366 if (sta_prepare_rate_control(local, sta, gfp))
367 goto free_txq;
368
Johannes Berg5a306f52012-11-14 23:22:21 +0100369 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berga622ab72010-06-10 10:21:39 +0200370 /*
371 * timer_to_tid must be initialized with identity mapping
372 * to enable session_timer's data differentiation. See
373 * sta_rx_agg_session_timer_expired for usage.
374 */
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200375 sta->timer_to_tid[i] = i;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200376 }
Johannes Berg948d8872011-09-29 16:04:29 +0200377 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
378 skb_queue_head_init(&sta->ps_tx_buf[i]);
379 skb_queue_head_init(&sta->tx_filtered[i]);
380 }
Johannes Berg73651ee2008-02-25 16:27:47 +0100381
Johannes Berg5a306f52012-11-14 23:22:21 +0100382 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700383 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
Senthil Balasubramaniancccaec92009-05-14 18:42:08 +0530384
Johannes Bergaf0ed692013-02-12 14:21:00 +0100385 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300386 if (sdata->vif.type == NL80211_IFTYPE_AP ||
387 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
388 struct ieee80211_supported_band *sband =
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100389 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300390 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
391 IEEE80211_HT_CAP_SM_PS_SHIFT;
392 /*
393 * Assume that hostapd advertises our caps in the beacon and
394 * this is the known_smps_mode for a station that just assciated
395 */
396 switch (smps) {
397 case WLAN_HT_SMPS_CONTROL_DISABLED:
398 sta->known_smps_mode = IEEE80211_SMPS_OFF;
399 break;
400 case WLAN_HT_SMPS_CONTROL_STATIC:
401 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
402 break;
403 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
404 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
405 break;
406 default:
407 WARN_ON(1);
408 }
409 }
Johannes Bergaf0ed692013-02-12 14:21:00 +0100410
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200411 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
Johannes Bergef04a292014-01-06 15:56:59 +0100412
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100413 return sta;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100414
415free_txq:
416 if (sta->sta.txq[0])
417 kfree(to_txq_info(sta->sta.txq[0]));
418free:
Johannes Berg433f5bc2015-06-17 10:31:00 +0200419#ifdef CONFIG_MAC80211_MESH
420 kfree(sta->mesh);
421#endif
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100422 kfree(sta);
423 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +0100424}
425
Guy Eilam8c71df72011-08-17 15:18:14 +0300426static int sta_info_insert_check(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100427{
Johannes Berg34e89502010-02-03 13:59:58 +0100428 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg34e89502010-02-03 13:59:58 +0100429
Johannes Berg03e44972008-02-27 09:56:40 +0100430 /*
431 * Can't be a WARN_ON because it can be triggered through a race:
432 * something inserts a STA (on one CPU) without holding the RTNL
433 * and another CPU turns off the net device.
434 */
Guy Eilam8c71df72011-08-17 15:18:14 +0300435 if (unlikely(!ieee80211_sdata_running(sdata)))
436 return -ENETDOWN;
Johannes Berg03e44972008-02-27 09:56:40 +0100437
Joe Perchesb203ca32012-05-08 18:56:52 +0000438 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
Guy Eilam8c71df72011-08-17 15:18:14 +0300439 is_multicast_ether_addr(sta->sta.addr)))
440 return -EINVAL;
441
Johannes Berg31104892015-10-22 17:35:19 +0200442 /* Strictly speaking this isn't necessary as we hold the mutex, but
443 * the rhashtable code can't really deal with that distinction. We
444 * do require the mutex for correctness though.
445 */
446 rcu_read_lock();
447 lockdep_assert_held(&sdata->local->sta_mtx);
448 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
449 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
450 rcu_read_unlock();
451 return -ENOTUNIQ;
452 }
453 rcu_read_unlock();
454
Guy Eilam8c71df72011-08-17 15:18:14 +0300455 return 0;
456}
457
Johannes Bergf09603a2012-01-20 13:55:21 +0100458static int sta_info_insert_drv_state(struct ieee80211_local *local,
459 struct ieee80211_sub_if_data *sdata,
460 struct sta_info *sta)
461{
462 enum ieee80211_sta_state state;
463 int err = 0;
464
465 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
466 err = drv_sta_state(local, sdata, sta, state, state + 1);
467 if (err)
468 break;
469 }
470
471 if (!err) {
Johannes Berga4ec45a2012-01-20 13:55:22 +0100472 /*
473 * Drivers using legacy sta_add/sta_remove callbacks only
474 * get uploaded set to true after sta_add is called.
475 */
476 if (!local->ops->sta_add)
477 sta->uploaded = true;
Johannes Bergf09603a2012-01-20 13:55:21 +0100478 return 0;
479 }
480
481 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200482 sdata_info(sdata,
483 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
484 sta->sta.addr, state + 1, err);
Johannes Bergf09603a2012-01-20 13:55:21 +0100485 err = 0;
486 }
487
488 /* unwind on error */
489 for (; state > IEEE80211_STA_NOTEXIST; state--)
490 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
491
492 return err;
493}
494
Guy Eilam8c71df72011-08-17 15:18:14 +0300495/*
496 * should be called with sta_mtx locked
497 * this function replaces the mutex lock
498 * with a RCU lock
499 */
Johannes Berg4d339602011-12-15 11:24:20 +0100500static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
Guy Eilam8c71df72011-08-17 15:18:14 +0300501{
502 struct ieee80211_local *local = sta->local;
503 struct ieee80211_sub_if_data *sdata = sta->sdata;
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100504 struct station_info *sinfo;
Guy Eilam8c71df72011-08-17 15:18:14 +0300505 int err = 0;
506
507 lockdep_assert_held(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +0100508
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100509 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
510 if (!sinfo) {
511 err = -ENOMEM;
512 goto out_err;
513 }
514
Johannes Berg7852e362012-01-20 13:55:24 +0100515 /* check if STA exists already */
516 if (sta_info_get_bss(sdata, sta->sta.addr)) {
517 err = -EEXIST;
518 goto out_err;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100519 }
Johannes Berg32bfd352007-12-19 01:31:26 +0100520
Johannes Berg7852e362012-01-20 13:55:24 +0100521 local->num_sta++;
522 local->sta_generation++;
523 smp_mb();
Johannes Berg4d339602011-12-15 11:24:20 +0100524
Johannes Berg5108ca82014-02-17 20:49:03 +0100525 /* simplify things and don't accept BA sessions yet */
526 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
527
Johannes Berg7852e362012-01-20 13:55:24 +0100528 /* make the station visible */
529 sta_info_hash_add(local, sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100530
Arik Nemtsov2bad77482014-10-22 12:32:16 +0300531 list_add_tail_rcu(&sta->list, &local->sta_list);
Johannes Berg83d5cc02012-01-12 09:31:10 +0100532
Johannes Berg5108ca82014-02-17 20:49:03 +0100533 /* notify driver */
534 err = sta_info_insert_drv_state(local, sdata, sta);
535 if (err)
536 goto out_remove;
537
Johannes Berg7852e362012-01-20 13:55:24 +0100538 set_sta_flag(sta, WLAN_STA_INSERTED);
Johannes Berg5108ca82014-02-17 20:49:03 +0100539 /* accept BA sessions now */
540 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg4d339602011-12-15 11:24:20 +0100541
Johannes Berg7852e362012-01-20 13:55:24 +0100542 ieee80211_sta_debugfs_add(sta);
543 rate_control_add_sta_debugfs(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100544
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100545 sinfo->generation = local->sta_generation;
546 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
547 kfree(sinfo);
Johannes Bergd0709a62008-02-25 16:27:46 +0100548
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200549 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700550
Johannes Berg34e89502010-02-03 13:59:58 +0100551 /* move reference to rcu-protected */
552 rcu_read_lock();
553 mutex_unlock(&local->sta_mtx);
Jiri Bence9f207f2007-05-05 11:46:38 -0700554
Johannes Berg73651ee2008-02-25 16:27:47 +0100555 if (ieee80211_vif_is_mesh(&sdata->vif))
556 mesh_accept_plinks_update(sdata);
557
558 return 0;
Johannes Berg5108ca82014-02-17 20:49:03 +0100559 out_remove:
560 sta_info_hash_del(local, sta);
561 list_del_rcu(&sta->list);
562 local->num_sta--;
563 synchronize_net();
564 __cleanup_single_sta(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100565 out_err:
566 mutex_unlock(&local->sta_mtx);
Sudip Mukherjeeea32f062016-02-02 13:21:14 +0530567 kfree(sinfo);
Johannes Berg4d339602011-12-15 11:24:20 +0100568 rcu_read_lock();
569 return err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300570}
571
572int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
573{
574 struct ieee80211_local *local = sta->local;
Zhao, Gang308f7fc2014-04-21 12:53:00 +0800575 int err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300576
Johannes Berg4d339602011-12-15 11:24:20 +0100577 might_sleep();
578
Johannes Berg31104892015-10-22 17:35:19 +0200579 mutex_lock(&local->sta_mtx);
580
Guy Eilam8c71df72011-08-17 15:18:14 +0300581 err = sta_info_insert_check(sta);
582 if (err) {
Johannes Berg31104892015-10-22 17:35:19 +0200583 mutex_unlock(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700584 rcu_read_lock();
585 goto out_free;
586 }
587
Johannes Berg4d339602011-12-15 11:24:20 +0100588 err = sta_info_insert_finish(sta);
Guy Eilam8c71df72011-08-17 15:18:14 +0300589 if (err)
Johannes Berg004c8722008-02-20 11:21:35 +0100590 goto out_free;
Johannes Bergd0709a62008-02-25 16:27:46 +0100591
Johannes Berg004c8722008-02-20 11:21:35 +0100592 return 0;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200593 out_free:
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100594 sta_info_free(local, sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200595 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -0700596}
597
Johannes Berg34e89502010-02-03 13:59:58 +0100598int sta_info_insert(struct sta_info *sta)
599{
600 int err = sta_info_insert_rcu(sta);
601
602 rcu_read_unlock();
603
604 return err;
605}
606
Marco Porschd012a602012-10-10 12:39:50 -0700607static inline void __bss_tim_set(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100608{
609 /*
610 * This format has been mandated by the IEEE specifications,
611 * so this line may not be changed to use the __set_bit() format.
612 */
Marco Porschd012a602012-10-10 12:39:50 -0700613 tim[id / 8] |= (1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100614}
615
Marco Porschd012a602012-10-10 12:39:50 -0700616static inline void __bss_tim_clear(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100617{
618 /*
619 * This format has been mandated by the IEEE specifications,
620 * so this line may not be changed to use the __clear_bit() format.
621 */
Marco Porschd012a602012-10-10 12:39:50 -0700622 tim[id / 8] &= ~(1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100623}
624
Ilan Peer3d5839b2013-03-05 15:27:20 +0200625static inline bool __bss_tim_get(u8 *tim, u16 id)
626{
627 /*
628 * This format has been mandated by the IEEE specifications,
629 * so this line may not be changed to use the test_bit() format.
630 */
631 return tim[id / 8] & (1 << (id % 8));
632}
633
Johannes Berg948d8872011-09-29 16:04:29 +0200634static unsigned long ieee80211_tids_for_ac(int ac)
Johannes Berg004c8722008-02-20 11:21:35 +0100635{
Johannes Berg948d8872011-09-29 16:04:29 +0200636 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
637 switch (ac) {
638 case IEEE80211_AC_VO:
639 return BIT(6) | BIT(7);
640 case IEEE80211_AC_VI:
641 return BIT(4) | BIT(5);
642 case IEEE80211_AC_BE:
643 return BIT(0) | BIT(3);
644 case IEEE80211_AC_BK:
645 return BIT(1) | BIT(2);
646 default:
647 WARN_ON(1);
648 return 0;
Johannes Bergd0709a62008-02-25 16:27:46 +0100649 }
Johannes Berg004c8722008-02-20 11:21:35 +0100650}
651
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100652static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
Johannes Berg004c8722008-02-20 11:21:35 +0100653{
Johannes Bergc868cb352011-09-29 16:04:27 +0200654 struct ieee80211_local *local = sta->local;
Marco Porschd012a602012-10-10 12:39:50 -0700655 struct ps_data *ps;
Johannes Berg948d8872011-09-29 16:04:29 +0200656 bool indicate_tim = false;
657 u8 ignore_for_tim = sta->sta.uapsd_queues;
658 int ac;
Bob Copelanda69bd8e2015-07-14 08:31:58 -0400659 u16 id = sta->sta.aid;
Johannes Berg004c8722008-02-20 11:21:35 +0100660
Marco Porschd012a602012-10-10 12:39:50 -0700661 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
662 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
663 if (WARN_ON_ONCE(!sta->sdata->bss))
664 return;
665
666 ps = &sta->sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100667#ifdef CONFIG_MAC80211_MESH
668 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
669 ps = &sta->sdata->u.mesh.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100670#endif
Marco Porschd012a602012-10-10 12:39:50 -0700671 } else {
Johannes Bergc868cb352011-09-29 16:04:27 +0200672 return;
Marco Porschd012a602012-10-10 12:39:50 -0700673 }
Johannes Berg3e122be2008-07-09 14:40:34 +0200674
Johannes Bergc868cb352011-09-29 16:04:27 +0200675 /* No need to do anything if the driver does all */
Johannes Berg30686bf2015-06-02 21:39:54 +0200676 if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
Johannes Bergc868cb352011-09-29 16:04:27 +0200677 return;
Johannes Berg004c8722008-02-20 11:21:35 +0100678
Johannes Bergc868cb352011-09-29 16:04:27 +0200679 if (sta->dead)
680 goto done;
Johannes Berg3e122be2008-07-09 14:40:34 +0200681
Johannes Berg948d8872011-09-29 16:04:29 +0200682 /*
683 * If all ACs are delivery-enabled then we should build
684 * the TIM bit for all ACs anyway; if only some are then
685 * we ignore those and build the TIM bit using only the
686 * non-enabled ones.
687 */
688 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
689 ignore_for_tim = 0;
Johannes Berg3e122be2008-07-09 14:40:34 +0200690
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100691 if (ignore_pending)
692 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
693
Johannes Berg948d8872011-09-29 16:04:29 +0200694 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
695 unsigned long tids;
696
697 if (ignore_for_tim & BIT(ac))
698 continue;
699
700 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
701 !skb_queue_empty(&sta->ps_tx_buf[ac]);
702 if (indicate_tim)
703 break;
704
705 tids = ieee80211_tids_for_ac(ac);
706
707 indicate_tim |=
708 sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100709 indicate_tim |=
710 sta->txq_buffered_tids & tids;
Johannes Bergd0709a62008-02-25 16:27:46 +0100711 }
Johannes Berg004c8722008-02-20 11:21:35 +0100712
Johannes Bergc868cb352011-09-29 16:04:27 +0200713 done:
Johannes Berg65f704a2013-02-13 17:39:53 +0100714 spin_lock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100715
Ilan Peer3d5839b2013-03-05 15:27:20 +0200716 if (indicate_tim == __bss_tim_get(ps->tim, id))
717 goto out_unlock;
718
Johannes Berg948d8872011-09-29 16:04:29 +0200719 if (indicate_tim)
Marco Porschd012a602012-10-10 12:39:50 -0700720 __bss_tim_set(ps->tim, id);
Johannes Bergc868cb352011-09-29 16:04:27 +0200721 else
Marco Porschd012a602012-10-10 12:39:50 -0700722 __bss_tim_clear(ps->tim, id);
Johannes Berg3e122be2008-07-09 14:40:34 +0200723
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100724 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
Johannes Bergc868cb352011-09-29 16:04:27 +0200725 local->tim_in_locked_section = true;
Johannes Berg948d8872011-09-29 16:04:29 +0200726 drv_set_tim(local, &sta->sta, indicate_tim);
Johannes Bergc868cb352011-09-29 16:04:27 +0200727 local->tim_in_locked_section = false;
Johannes Berg004c8722008-02-20 11:21:35 +0100728 }
Johannes Berg004c8722008-02-20 11:21:35 +0100729
Ilan Peer3d5839b2013-03-05 15:27:20 +0200730out_unlock:
Johannes Berg65f704a2013-02-13 17:39:53 +0100731 spin_unlock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100732}
733
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100734void sta_info_recalc_tim(struct sta_info *sta)
735{
736 __sta_info_recalc_tim(sta, false);
737}
738
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200739static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700740{
Johannes Berge039fa42008-05-15 12:55:29 +0200741 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700742 int timeout;
743
744 if (!skb)
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200745 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700746
Johannes Berge039fa42008-05-15 12:55:29 +0200747 info = IEEE80211_SKB_CB(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700748
749 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200750 timeout = (sta->listen_interval *
751 sta->sdata->vif.bss_conf.beacon_int *
752 32 / 15625) * HZ;
Jiri Bencf0706e82007-05-05 11:45:53 -0700753 if (timeout < STA_TX_BUFFER_EXPIRE)
754 timeout = STA_TX_BUFFER_EXPIRE;
Johannes Berge039fa42008-05-15 12:55:29 +0200755 return time_after(jiffies, info->control.jiffies + timeout);
Jiri Bencf0706e82007-05-05 11:45:53 -0700756}
757
758
Johannes Berg948d8872011-09-29 16:04:29 +0200759static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
760 struct sta_info *sta, int ac)
Jiri Bencf0706e82007-05-05 11:45:53 -0700761{
762 unsigned long flags;
763 struct sk_buff *skb;
764
Johannes Berg60750392011-09-29 16:04:28 +0200765 /*
766 * First check for frames that should expire on the filtered
767 * queue. Frames here were rejected by the driver and are on
768 * a separate queue to avoid reordering with normal PS-buffered
769 * frames. They also aren't accounted for right now in the
770 * total_ps_buffered counter.
771 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700772 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200773 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
774 skb = skb_peek(&sta->tx_filtered[ac]);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200775 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200776 skb = __skb_dequeue(&sta->tx_filtered[ac]);
Johannes Berg836341a2008-02-20 02:07:21 +0100777 else
Jiri Bencf0706e82007-05-05 11:45:53 -0700778 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200779 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700780
Johannes Berg60750392011-09-29 16:04:28 +0200781 /*
782 * Frames are queued in order, so if this one
783 * hasn't expired yet we can stop testing. If
784 * we actually reached the end of the queue we
785 * also need to stop, of course.
786 */
787 if (!skb)
788 break;
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200789 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg60750392011-09-29 16:04:28 +0200790 }
791
792 /*
793 * Now also check the normal PS-buffered queue, this will
794 * only find something if the filtered queue was emptied
795 * since the filtered frames are all before the normal PS
796 * buffered frames.
797 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700798 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200799 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
800 skb = skb_peek(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700801 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200802 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700803 else
804 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200805 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700806
Johannes Berg60750392011-09-29 16:04:28 +0200807 /*
808 * frames are queued in order, so if this one
809 * hasn't expired yet (or we reached the end of
810 * the queue) we can stop testing
811 */
Johannes Berg836341a2008-02-20 02:07:21 +0100812 if (!skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700813 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100814
Johannes Berg836341a2008-02-20 02:07:21 +0100815 local->total_ps_buffered--;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200816 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
817 sta->sta.addr);
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200818 ieee80211_free_txskb(&local->hw, skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700819 }
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300820
Johannes Berg60750392011-09-29 16:04:28 +0200821 /*
822 * Finally, recalculate the TIM bit for this station -- it might
823 * now be clear because the station was too slow to retrieve its
824 * frames.
825 */
826 sta_info_recalc_tim(sta);
827
828 /*
829 * Return whether there are any frames still buffered, this is
830 * used to check whether the cleanup timer still needs to run,
831 * if there are no frames we don't need to rearm the timer.
832 */
Johannes Berg948d8872011-09-29 16:04:29 +0200833 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
834 skb_queue_empty(&sta->tx_filtered[ac]));
835}
836
837static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
838 struct sta_info *sta)
839{
840 bool have_buffered = false;
841 int ac;
842
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100843 /* This is only necessary for stations on BSS/MBSS interfaces */
844 if (!sta->sdata->bss &&
845 !ieee80211_vif_is_mesh(&sta->sdata->vif))
Johannes Berg948d8872011-09-29 16:04:29 +0200846 return false;
847
848 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
849 have_buffered |=
850 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
851
852 return have_buffered;
Jiri Bencf0706e82007-05-05 11:45:53 -0700853}
854
Johannes Bergd7782072013-12-04 23:12:31 +0100855static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100856{
857 struct ieee80211_local *local;
858 struct ieee80211_sub_if_data *sdata;
Johannes Berg6d10e462013-03-06 23:09:11 +0100859 int ret;
Johannes Berg34e89502010-02-03 13:59:58 +0100860
861 might_sleep();
862
863 if (!sta)
864 return -ENOENT;
865
866 local = sta->local;
867 sdata = sta->sdata;
868
Johannes Berg83d5cc02012-01-12 09:31:10 +0100869 lockdep_assert_held(&local->sta_mtx);
870
Johannes Berg098a6072010-04-06 11:18:47 +0200871 /*
872 * Before removing the station from the driver and
873 * rate control, it might still start new aggregation
874 * sessions -- block that to make sure the tear-down
875 * will be sufficient.
876 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200877 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Bergc82c4a82012-07-18 13:31:31 +0200878 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
Johannes Berg098a6072010-04-06 11:18:47 +0200879
Sara Sharonf59374e2016-03-02 23:46:14 +0200880 /*
881 * Before removing the station from the driver there might be pending
882 * rx frames on RSS queues sent prior to the disassociation - wait for
883 * all such frames to be processed.
884 */
885 drv_sync_rx_queues(local, sta);
886
Johannes Berg34e89502010-02-03 13:59:58 +0100887 ret = sta_info_hash_del(local, sta);
Johannes Bergb01711b2013-12-04 20:25:27 +0100888 if (WARN_ON(ret))
Johannes Berg34e89502010-02-03 13:59:58 +0100889 return ret;
890
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200891 /*
892 * for TDLS peers, make sure to return to the base channel before
893 * removal.
894 */
895 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
896 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
897 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
898 }
899
Arik Nemtsov794454c2012-06-03 23:32:32 +0300900 list_del_rcu(&sta->list);
Eliad Pelleref044762015-11-17 10:24:37 +0200901 sta->removed = true;
Johannes Berg4d339602011-12-15 11:24:20 +0100902
Johannes Berg6a9d1b92013-12-04 22:39:17 +0100903 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
904
Johannes Berga710c812013-12-04 20:11:06 +0100905 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
906 rcu_access_pointer(sdata->u.vlan.sta) == sta)
907 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
908
Johannes Bergd7782072013-12-04 23:12:31 +0100909 return 0;
910}
911
912static void __sta_info_destroy_part2(struct sta_info *sta)
913{
914 struct ieee80211_local *local = sta->local;
915 struct ieee80211_sub_if_data *sdata = sta->sdata;
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100916 struct station_info *sinfo;
Johannes Bergd7782072013-12-04 23:12:31 +0100917 int ret;
918
919 /*
920 * NOTE: This assumes at least synchronize_net() was done
921 * after _part1 and before _part2!
922 */
923
924 might_sleep();
925 lockdep_assert_held(&local->sta_mtx);
926
Johannes Bergc8782072013-12-04 23:05:45 +0100927 /* now keys can no longer be reached */
Johannes Berg6d10e462013-03-06 23:09:11 +0100928 ieee80211_free_sta_keys(local, sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100929
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100930 /* disable TIM bit - last chance to tell driver */
931 __sta_info_recalc_tim(sta, true);
932
Johannes Berg34e89502010-02-03 13:59:58 +0100933 sta->dead = true;
934
Johannes Berg34e89502010-02-03 13:59:58 +0100935 local->num_sta--;
936 local->sta_generation++;
937
Johannes Berg83d5cc02012-01-12 09:31:10 +0100938 while (sta->sta_state > IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100939 ret = sta_info_move_state(sta, sta->sta_state - 1);
940 if (ret) {
Johannes Berg83d5cc02012-01-12 09:31:10 +0100941 WARN_ON_ONCE(1);
942 break;
943 }
944 }
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100945
Johannes Bergf09603a2012-01-20 13:55:21 +0100946 if (sta->uploaded) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100947 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
948 IEEE80211_STA_NOTEXIST);
949 WARN_ON_ONCE(ret != 0);
950 }
Johannes Berg34e89502010-02-03 13:59:58 +0100951
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200952 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
953
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100954 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
955 if (sinfo)
956 sta_set_sinfo(sta, sinfo);
957 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
958 kfree(sinfo);
Jouni Malinenec15e682011-03-23 15:29:52 +0200959
Johannes Berg34e89502010-02-03 13:59:58 +0100960 rate_control_remove_sta_debugfs(sta);
961 ieee80211_sta_debugfs_remove(sta);
962
Johannes Bergd34ba212013-12-04 22:46:11 +0100963 cleanup_single_sta(sta);
Johannes Bergd7782072013-12-04 23:12:31 +0100964}
965
966int __must_check __sta_info_destroy(struct sta_info *sta)
967{
968 int err = __sta_info_destroy_part1(sta);
969
970 if (err)
971 return err;
972
973 synchronize_net();
974
975 __sta_info_destroy_part2(sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100976
977 return 0;
978}
979
980int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
981{
982 struct sta_info *sta;
983 int ret;
984
985 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100986 sta = sta_info_get(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100987 ret = __sta_info_destroy(sta);
988 mutex_unlock(&sdata->local->sta_mtx);
989
990 return ret;
991}
992
993int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
994 const u8 *addr)
995{
996 struct sta_info *sta;
997 int ret;
998
999 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +01001000 sta = sta_info_get_bss(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +01001001 ret = __sta_info_destroy(sta);
1002 mutex_unlock(&sdata->local->sta_mtx);
1003
1004 return ret;
1005}
Jiri Bencf0706e82007-05-05 11:45:53 -07001006
1007static void sta_info_cleanup(unsigned long data)
1008{
1009 struct ieee80211_local *local = (struct ieee80211_local *) data;
1010 struct sta_info *sta;
Juuso Oikarinen3393a602010-04-19 10:12:52 +03001011 bool timer_needed = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001012
Johannes Bergd0709a62008-02-25 16:27:46 +01001013 rcu_read_lock();
1014 list_for_each_entry_rcu(sta, &local->sta_list, list)
Juuso Oikarinen3393a602010-04-19 10:12:52 +03001015 if (sta_info_cleanup_expire_buffered(local, sta))
1016 timer_needed = true;
Johannes Bergd0709a62008-02-25 16:27:46 +01001017 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001018
Johannes Berg5bb644a2009-05-17 11:40:42 +02001019 if (local->quiescing)
1020 return;
1021
Juuso Oikarinen3393a602010-04-19 10:12:52 +03001022 if (!timer_needed)
1023 return;
1024
Johannes Berg26d59532011-04-01 13:52:48 +02001025 mod_timer(&local->sta_cleanup,
1026 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
Jiri Bencf0706e82007-05-05 11:45:53 -07001027}
1028
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001029u32 sta_addr_hash(const void *key, u32 length, u32 seed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001030{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001031 return jhash(key, ETH_ALEN, seed);
1032}
1033
1034int sta_info_init(struct ieee80211_local *local)
1035{
1036 int err;
1037
1038 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
1039 if (err)
1040 return err;
1041
Johannes Berg4d339602011-12-15 11:24:20 +01001042 spin_lock_init(&local->tim_lock);
Johannes Berg34e89502010-02-03 13:59:58 +01001043 mutex_init(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07001044 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001045
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001046 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1047 (unsigned long)local);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001048 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001049}
1050
1051void sta_info_stop(struct ieee80211_local *local)
1052{
Johannes Berga56f9922012-12-13 23:08:52 +01001053 del_timer_sync(&local->sta_cleanup);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001054 rhashtable_destroy(&local->sta_hash);
Jiri Bencf0706e82007-05-05 11:45:53 -07001055}
1056
Johannes Berg051007d2012-12-13 23:49:02 +01001057
Johannes Berge7162512013-12-04 23:18:37 +01001058int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
Jiri Bencf0706e82007-05-05 11:45:53 -07001059{
Johannes Bergb998e8b2012-12-13 23:07:46 +01001060 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001061 struct sta_info *sta, *tmp;
Johannes Bergd7782072013-12-04 23:12:31 +01001062 LIST_HEAD(free_list);
Johannes Berg44213b52008-02-25 16:27:49 +01001063 int ret = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001064
Johannes Bergd0709a62008-02-25 16:27:46 +01001065 might_sleep();
1066
Johannes Berge7162512013-12-04 23:18:37 +01001067 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1068 WARN_ON(vlans && !sdata->bss);
1069
Johannes Berg34e89502010-02-03 13:59:58 +01001070 mutex_lock(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +01001071 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Johannes Berge7162512013-12-04 23:18:37 +01001072 if (sdata == sta->sdata ||
1073 (vlans && sdata->bss == sta->sdata->bss)) {
Johannes Bergd7782072013-12-04 23:12:31 +01001074 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1075 list_add(&sta->free_list, &free_list);
Johannes Berg34316832012-02-25 21:40:46 +01001076 ret++;
1077 }
Johannes Berg34e89502010-02-03 13:59:58 +01001078 }
Johannes Bergd7782072013-12-04 23:12:31 +01001079
1080 if (!list_empty(&free_list)) {
1081 synchronize_net();
1082 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1083 __sta_info_destroy_part2(sta);
1084 }
Johannes Berg34e89502010-02-03 13:59:58 +01001085 mutex_unlock(&local->sta_mtx);
Johannes Berg44213b52008-02-25 16:27:49 +01001086
Johannes Berg051007d2012-12-13 23:49:02 +01001087 return ret;
1088}
1089
Johannes Berg24723d12008-09-11 00:01:46 +02001090void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1091 unsigned long exp_time)
1092{
1093 struct ieee80211_local *local = sdata->local;
1094 struct sta_info *sta, *tmp;
Johannes Berg24723d12008-09-11 00:01:46 +02001095
Johannes Berg34e89502010-02-03 13:59:58 +01001096 mutex_lock(&local->sta_mtx);
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301097
1098 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Johannes Bergb8da6b62016-03-31 20:02:07 +03001099 unsigned long last_active = ieee80211_sta_last_active(sta);
1100
Marek Lindnerec2b7742011-12-20 23:16:52 +08001101 if (sdata != sta->sdata)
1102 continue;
1103
Johannes Bergb8da6b62016-03-31 20:02:07 +03001104 if (time_is_before_jiffies(last_active + exp_time)) {
Mohammed Shafi Shajakhaneea57d42012-10-08 21:33:47 +05301105 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1106 sta->sta.addr);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001107
1108 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1109 test_sta_flag(sta, WLAN_STA_PS_STA))
1110 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1111
Johannes Berg34e89502010-02-03 13:59:58 +01001112 WARN_ON(__sta_info_destroy(sta));
Johannes Berg24723d12008-09-11 00:01:46 +02001113 }
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301114 }
1115
Johannes Berg34e89502010-02-03 13:59:58 +01001116 mutex_unlock(&local->sta_mtx);
Johannes Berg24723d12008-09-11 00:01:46 +02001117}
Johannes Berg17741cd2008-09-11 00:02:02 +02001118
Ben Greear686b9cb2010-09-23 09:44:36 -07001119struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001120 const u8 *addr,
1121 const u8 *localaddr)
Johannes Berg17741cd2008-09-11 00:02:02 +02001122{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001123 struct ieee80211_local *local = hw_to_local(hw);
1124 struct sta_info *sta;
1125 struct rhash_head *tmp;
1126 const struct bucket_table *tbl;
1127
1128 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
Johannes Berg17741cd2008-09-11 00:02:02 +02001129
Ben Greear686b9cb2010-09-23 09:44:36 -07001130 /*
1131 * Just return a random station if localaddr is NULL
1132 * ... first in list.
1133 */
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001134 for_each_sta_info(local, tbl, addr, sta, tmp) {
Ben Greear686b9cb2010-09-23 09:44:36 -07001135 if (localaddr &&
Joe Perchesb203ca32012-05-08 18:56:52 +00001136 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
Ben Greear686b9cb2010-09-23 09:44:36 -07001137 continue;
Johannes Bergf7c65592010-04-30 13:48:36 +02001138 if (!sta->uploaded)
1139 return NULL;
Johannes Bergabe60632009-11-25 17:46:18 +01001140 return &sta->sta;
Johannes Bergf7c65592010-04-30 13:48:36 +02001141 }
1142
Johannes Bergabe60632009-11-25 17:46:18 +01001143 return NULL;
Johannes Berg17741cd2008-09-11 00:02:02 +02001144}
Ben Greear686b9cb2010-09-23 09:44:36 -07001145EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
Johannes Berg5ed176e2009-11-04 14:42:28 +01001146
1147struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1148 const u8 *addr)
1149{
Johannes Bergf7c65592010-04-30 13:48:36 +02001150 struct sta_info *sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001151
1152 if (!vif)
1153 return NULL;
1154
Johannes Bergf7c65592010-04-30 13:48:36 +02001155 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1156 if (!sta)
1157 return NULL;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001158
Johannes Bergf7c65592010-04-30 13:48:36 +02001159 if (!sta->uploaded)
1160 return NULL;
1161
1162 return &sta->sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001163}
Johannes Berg17741cd2008-09-11 00:02:02 +02001164EXPORT_SYMBOL(ieee80211_find_sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001165
Johannes Berge3685e02014-02-20 11:19:58 +01001166/* powersave support code */
1167void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
Johannes Berg50a94322010-11-16 11:50:28 -08001168{
Helmut Schaa608383b2012-01-30 15:18:00 +01001169 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berge3685e02014-02-20 11:19:58 +01001170 struct ieee80211_local *local = sdata->local;
1171 struct sk_buff_head pending;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001172 int filtered = 0, buffered = 0, ac, i;
Johannes Berge3685e02014-02-20 11:19:58 +01001173 unsigned long flags;
Marco Porschd012a602012-10-10 12:39:50 -07001174 struct ps_data *ps;
1175
Felix Fietkau3918edb2014-07-25 16:20:23 +02001176 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1177 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1178 u.ap);
1179
1180 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porschd012a602012-10-10 12:39:50 -07001181 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001182 else if (ieee80211_vif_is_mesh(&sdata->vif))
1183 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07001184 else
1185 return;
Johannes Berg50a94322010-11-16 11:50:28 -08001186
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001187 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg47086fc2011-09-29 16:04:33 +02001188
Johannes Berg5a306f52012-11-14 23:22:21 +01001189 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
Johannes Berg948d8872011-09-29 16:04:29 +02001190 sta->driver_buffered_tids = 0;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001191 sta->txq_buffered_tids = 0;
Johannes Berg948d8872011-09-29 16:04:29 +02001192
Johannes Berg30686bf2015-06-02 21:39:54 +02001193 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001194 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001195
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001196 if (sta->sta.txq[0]) {
1197 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1198 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1199
1200 if (!skb_queue_len(&txqi->queue))
1201 continue;
1202
1203 drv_wake_tx_queue(local, txqi);
1204 }
1205 }
1206
Johannes Berg948d8872011-09-29 16:04:29 +02001207 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001208
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001209 /* sync with ieee80211_tx_h_unicast_ps_buf */
1210 spin_lock(&sta->ps_lock);
Johannes Bergaf818582009-11-06 11:35:50 +01001211 /* Send all buffered frames to the station */
Johannes Berg948d8872011-09-29 16:04:29 +02001212 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1213 int count = skb_queue_len(&pending), tmp;
1214
Arik Nemtsov987c2852012-11-05 10:27:52 +02001215 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001216 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001217 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001218 tmp = skb_queue_len(&pending);
1219 filtered += tmp - count;
1220 count = tmp;
1221
Arik Nemtsov987c2852012-11-05 10:27:52 +02001222 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001223 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001224 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001225 tmp = skb_queue_len(&pending);
1226 buffered += tmp - count;
1227 }
1228
Johannes Berge3685e02014-02-20 11:19:58 +01001229 ieee80211_add_pending_skbs(local, &pending);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001230
1231 /* now we're no longer in the deliver code */
1232 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1233
1234 /* The station might have polled and then woken up before we responded,
1235 * so clear these flags now to avoid them sticking around.
1236 */
1237 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1238 clear_sta_flag(sta, WLAN_STA_UAPSD);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001239 spin_unlock(&sta->ps_lock);
Johannes Berg948d8872011-09-29 16:04:29 +02001240
Johannes Berge3685e02014-02-20 11:19:58 +01001241 atomic_dec(&ps->num_sta_ps);
1242
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001243 /* This station just woke up and isn't aware of our SMPS state */
Chun-Yeow Yeoh062f1d62014-04-22 18:19:25 +08001244 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1245 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001246 sdata->smps_mode) &&
1247 sta->known_smps_mode != sdata->bss->req_smps &&
1248 sta_info_tx_streams(sta) != 1) {
1249 ht_dbg(sdata,
1250 "%pM just woke up and MIMO capable - update SMPS\n",
1251 sta->sta.addr);
1252 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1253 sta->sta.addr,
1254 sdata->vif.bss_conf.bssid);
1255 }
1256
Johannes Bergaf818582009-11-06 11:35:50 +01001257 local->total_ps_buffered -= buffered;
1258
Johannes Bergc868cb352011-09-29 16:04:27 +02001259 sta_info_recalc_tim(sta);
1260
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001261 ps_dbg(sdata,
1262 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1263 sta->sta.addr, sta->sta.aid, filtered, buffered);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001264
1265 ieee80211_check_fast_xmit(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001266}
1267
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001268static void ieee80211_send_null_response(struct sta_info *sta, int tid,
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001269 enum ieee80211_frame_release_type reason,
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001270 bool call_driver, bool more_data)
Johannes Bergce662b442011-09-29 16:04:34 +02001271{
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001272 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Bergce662b442011-09-29 16:04:34 +02001273 struct ieee80211_local *local = sdata->local;
1274 struct ieee80211_qos_hdr *nullfunc;
1275 struct sk_buff *skb;
1276 int size = sizeof(*nullfunc);
1277 __le16 fc;
Johannes Berga74a8c82014-07-22 14:50:47 +02001278 bool qos = sta->sta.wme;
Johannes Bergce662b442011-09-29 16:04:34 +02001279 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02001280 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergce662b442011-09-29 16:04:34 +02001281
1282 if (qos) {
1283 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1284 IEEE80211_STYPE_QOS_NULLFUNC |
1285 IEEE80211_FCTL_FROMDS);
1286 } else {
1287 size -= 2;
1288 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1289 IEEE80211_STYPE_NULLFUNC |
1290 IEEE80211_FCTL_FROMDS);
1291 }
1292
1293 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1294 if (!skb)
1295 return;
1296
1297 skb_reserve(skb, local->hw.extra_tx_headroom);
1298
1299 nullfunc = (void *) skb_put(skb, size);
1300 nullfunc->frame_control = fc;
1301 nullfunc->duration_id = 0;
1302 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1303 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1304 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
Johannes Berg864a6042014-03-04 13:46:53 +01001305 nullfunc->seq_ctrl = 0;
Johannes Bergce662b442011-09-29 16:04:34 +02001306
Johannes Berg59b66252011-10-13 13:19:19 +02001307 skb->priority = tid;
1308 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
Johannes Bergce662b442011-09-29 16:04:34 +02001309 if (qos) {
Johannes Bergce662b442011-09-29 16:04:34 +02001310 nullfunc->qos_ctrl = cpu_to_le16(tid);
1311
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001312 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
Johannes Bergce662b442011-09-29 16:04:34 +02001313 nullfunc->qos_ctrl |=
1314 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001315 if (more_data)
1316 nullfunc->frame_control |=
1317 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1318 }
Johannes Bergce662b442011-09-29 16:04:34 +02001319 }
1320
1321 info = IEEE80211_SKB_CB(skb);
1322
1323 /*
1324 * Tell TX path to send this frame even though the
1325 * STA may still remain is PS mode after this frame
Johannes Bergdeeaee192011-09-29 16:04:35 +02001326 * exchange. Also set EOSP to indicate this packet
1327 * ends the poll/service period.
Johannes Bergce662b442011-09-29 16:04:34 +02001328 */
Johannes Berg02f2f1a2012-02-27 12:18:30 +01001329 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
Johannes Bergdeeaee192011-09-29 16:04:35 +02001330 IEEE80211_TX_STATUS_EOSP |
1331 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergce662b442011-09-29 16:04:34 +02001332
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301333 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1334
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001335 if (call_driver)
1336 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1337 reason, false);
Johannes Berg40b96402011-09-29 16:04:38 +02001338
Johannes Berg89afe612013-02-13 15:39:57 +01001339 skb->dev = sdata->dev;
1340
Johannes Berg55de9082012-07-26 17:24:39 +02001341 rcu_read_lock();
1342 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1343 if (WARN_ON(!chanctx_conf)) {
1344 rcu_read_unlock();
1345 kfree_skb(skb);
1346 return;
1347 }
1348
Johannes Berg73c4e192014-11-09 18:50:09 +02001349 info->band = chanctx_conf->def.chan->band;
Johannes Berg7c107702015-03-20 14:18:27 +01001350 ieee80211_xmit(sdata, sta, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001351 rcu_read_unlock();
Johannes Bergce662b442011-09-29 16:04:34 +02001352}
1353
Johannes Berg0a1cb802014-01-09 11:05:31 +01001354static int find_highest_prio_tid(unsigned long tids)
1355{
1356 /* lower 3 TIDs aren't ordered perfectly */
1357 if (tids & 0xF8)
1358 return fls(tids) - 1;
1359 /* TID 0 is BE just like TID 3 */
1360 if (tids & BIT(0))
1361 return 0;
1362 return fls(tids) - 1;
1363}
1364
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001365/* Indicates if the MORE_DATA bit should be set in the last
1366 * frame obtained by ieee80211_sta_ps_get_frames.
1367 * Note that driver_release_tids is relevant only if
1368 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1369 */
1370static bool
1371ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1372 enum ieee80211_frame_release_type reason,
1373 unsigned long driver_release_tids)
1374{
1375 int ac;
1376
1377 /* If the driver has data on more than one TID then
1378 * certainly there's more data if we release just a
1379 * single frame now (from a single TID). This will
1380 * only happen for PS-Poll.
1381 */
1382 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1383 hweight16(driver_release_tids) > 1)
1384 return true;
1385
1386 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1387 if (ignored_acs & BIT(ac))
1388 continue;
1389
1390 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1391 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1392 return true;
1393 }
1394
1395 return false;
1396}
1397
Johannes Berg47086fc2011-09-29 16:04:33 +02001398static void
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001399ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1400 enum ieee80211_frame_release_type reason,
1401 struct sk_buff_head *frames,
1402 unsigned long *driver_release_tids)
Johannes Bergaf818582009-11-06 11:35:50 +01001403{
1404 struct ieee80211_sub_if_data *sdata = sta->sdata;
1405 struct ieee80211_local *local = sdata->local;
Johannes Berg948d8872011-09-29 16:04:29 +02001406 int ac;
Johannes Bergaf818582009-11-06 11:35:50 +01001407
Johannes Bergf9f760b2014-01-08 17:45:07 +01001408 /* Get response frame(s) and more data bit for the last one. */
Johannes Berg948d8872011-09-29 16:04:29 +02001409 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg4049e092011-09-29 16:04:32 +02001410 unsigned long tids;
1411
Johannes Berg47086fc2011-09-29 16:04:33 +02001412 if (ignored_acs & BIT(ac))
Johannes Berg948d8872011-09-29 16:04:29 +02001413 continue;
1414
Johannes Berg4049e092011-09-29 16:04:32 +02001415 tids = ieee80211_tids_for_ac(ac);
1416
Johannes Bergf9f760b2014-01-08 17:45:07 +01001417 /* if we already have frames from software, then we can't also
1418 * release from hardware queues
1419 */
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001420 if (skb_queue_empty(frames)) {
1421 *driver_release_tids |=
1422 sta->driver_buffered_tids & tids;
1423 *driver_release_tids |= sta->txq_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001424 }
Johannes Berg47086fc2011-09-29 16:04:33 +02001425
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001426 if (!*driver_release_tids) {
Johannes Bergf9f760b2014-01-08 17:45:07 +01001427 struct sk_buff *skb;
1428
1429 while (n_frames > 0) {
1430 skb = skb_dequeue(&sta->tx_filtered[ac]);
1431 if (!skb) {
1432 skb = skb_dequeue(
1433 &sta->ps_tx_buf[ac]);
1434 if (skb)
1435 local->total_ps_buffered--;
1436 }
1437 if (!skb)
1438 break;
1439 n_frames--;
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001440 __skb_queue_tail(frames, skb);
Johannes Bergf9f760b2014-01-08 17:45:07 +01001441 }
Johannes Berg948d8872011-09-29 16:04:29 +02001442 }
1443
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001444 /* If we have more frames buffered on this AC, then abort the
1445 * loop since we can't send more data from other ACs before
1446 * the buffered frames from this.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001447 */
Johannes Berg948d8872011-09-29 16:04:29 +02001448 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001449 !skb_queue_empty(&sta->ps_tx_buf[ac]))
Johannes Berg948d8872011-09-29 16:04:29 +02001450 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001451 }
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001452}
1453
1454static void
1455ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1456 int n_frames, u8 ignored_acs,
1457 enum ieee80211_frame_release_type reason)
1458{
1459 struct ieee80211_sub_if_data *sdata = sta->sdata;
1460 struct ieee80211_local *local = sdata->local;
1461 unsigned long driver_release_tids = 0;
1462 struct sk_buff_head frames;
1463 bool more_data;
1464
1465 /* Service or PS-Poll period starts */
1466 set_sta_flag(sta, WLAN_STA_SP);
1467
1468 __skb_queue_head_init(&frames);
1469
1470 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1471 &frames, &driver_release_tids);
1472
1473 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1474
Emmanuel Grumbach1a570812015-12-20 13:50:00 +02001475 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001476 driver_release_tids =
1477 BIT(find_highest_prio_tid(driver_release_tids));
Johannes Bergaf818582009-11-06 11:35:50 +01001478
Johannes Bergf9f760b2014-01-08 17:45:07 +01001479 if (skb_queue_empty(&frames) && !driver_release_tids) {
Johannes Bergce662b442011-09-29 16:04:34 +02001480 int tid;
Johannes Berg4049e092011-09-29 16:04:32 +02001481
Johannes Bergce662b442011-09-29 16:04:34 +02001482 /*
1483 * For PS-Poll, this can only happen due to a race condition
1484 * when we set the TIM bit and the station notices it, but
1485 * before it can poll for the frame we expire it.
1486 *
1487 * For uAPSD, this is said in the standard (11.2.1.5 h):
1488 * At each unscheduled SP for a non-AP STA, the AP shall
1489 * attempt to transmit at least one MSDU or MMPDU, but no
1490 * more than the value specified in the Max SP Length field
1491 * in the QoS Capability element from delivery-enabled ACs,
1492 * that are destined for the non-AP STA.
1493 *
1494 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1495 */
1496
1497 /* This will evaluate to 1, 3, 5 or 7. */
1498 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1499
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001500 ieee80211_send_null_response(sta, tid, reason, true, false);
Johannes Bergf9f760b2014-01-08 17:45:07 +01001501 } else if (!driver_release_tids) {
Johannes Berg47086fc2011-09-29 16:04:33 +02001502 struct sk_buff_head pending;
1503 struct sk_buff *skb;
Johannes Berg40b96402011-09-29 16:04:38 +02001504 int num = 0;
1505 u16 tids = 0;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001506 bool need_null = false;
Johannes Bergaf818582009-11-06 11:35:50 +01001507
Johannes Berg47086fc2011-09-29 16:04:33 +02001508 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001509
Johannes Berg47086fc2011-09-29 16:04:33 +02001510 while ((skb = __skb_dequeue(&frames))) {
1511 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1512 struct ieee80211_hdr *hdr = (void *) skb->data;
Johannes Berg40b96402011-09-29 16:04:38 +02001513 u8 *qoshdr = NULL;
1514
1515 num++;
Johannes Bergaf818582009-11-06 11:35:50 +01001516
Johannes Berg47086fc2011-09-29 16:04:33 +02001517 /*
1518 * Tell TX path to send this frame even though the
1519 * STA may still remain is PS mode after this frame
1520 * exchange.
1521 */
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301522 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1523 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
Johannes Bergaf818582009-11-06 11:35:50 +01001524
Johannes Berg47086fc2011-09-29 16:04:33 +02001525 /*
1526 * Use MoreData flag to indicate whether there are
1527 * more buffered frames for this STA
1528 */
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001529 if (more_data || !skb_queue_empty(&frames))
Johannes Berg47086fc2011-09-29 16:04:33 +02001530 hdr->frame_control |=
1531 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001532 else
1533 hdr->frame_control &=
1534 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
Johannes Berg47086fc2011-09-29 16:04:33 +02001535
Johannes Berg40b96402011-09-29 16:04:38 +02001536 if (ieee80211_is_data_qos(hdr->frame_control) ||
1537 ieee80211_is_qos_nullfunc(hdr->frame_control))
1538 qoshdr = ieee80211_get_qos_ctl(hdr);
1539
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001540 tids |= BIT(skb->priority);
1541
1542 __skb_queue_tail(&pending, skb);
1543
1544 /* end service period after last frame or add one */
1545 if (!skb_queue_empty(&frames))
1546 continue;
1547
1548 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1549 /* for PS-Poll, there's only one frame */
1550 info->flags |= IEEE80211_TX_STATUS_EOSP |
1551 IEEE80211_TX_CTL_REQ_TX_STATUS;
1552 break;
1553 }
1554
1555 /* For uAPSD, things are a bit more complicated. If the
1556 * last frame has a QoS header (i.e. is a QoS-data or
1557 * QoS-nulldata frame) then just set the EOSP bit there
1558 * and be done.
1559 * If the frame doesn't have a QoS header (which means
1560 * it should be a bufferable MMPDU) then we can't set
1561 * the EOSP bit in the QoS header; add a QoS-nulldata
1562 * frame to the list to send it after the MMPDU.
1563 *
1564 * Note that this code is only in the mac80211-release
1565 * code path, we assume that the driver will not buffer
1566 * anything but QoS-data frames, or if it does, will
1567 * create the QoS-nulldata frame by itself if needed.
1568 *
1569 * Cf. 802.11-2012 10.2.1.10 (c).
1570 */
1571 if (qoshdr) {
1572 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
Johannes Berg47086fc2011-09-29 16:04:33 +02001573
Marco Porsch52a3f202012-03-16 15:30:26 +01001574 info->flags |= IEEE80211_TX_STATUS_EOSP |
1575 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001576 } else {
1577 /* The standard isn't completely clear on this
1578 * as it says the more-data bit should be set
1579 * if there are more BUs. The QoS-Null frame
1580 * we're about to send isn't buffered yet, we
1581 * only create it below, but let's pretend it
1582 * was buffered just in case some clients only
1583 * expect more-data=0 when eosp=1.
1584 */
1585 hdr->frame_control |=
1586 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1587 need_null = true;
1588 num++;
Marco Porsch52a3f202012-03-16 15:30:26 +01001589 }
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001590 break;
Johannes Berg47086fc2011-09-29 16:04:33 +02001591 }
1592
Johannes Berg40b96402011-09-29 16:04:38 +02001593 drv_allow_buffered_frames(local, sta, tids, num,
1594 reason, more_data);
1595
Johannes Berg47086fc2011-09-29 16:04:33 +02001596 ieee80211_add_pending_skbs(local, &pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001597
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001598 if (need_null)
1599 ieee80211_send_null_response(
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001600 sta, find_highest_prio_tid(tids),
1601 reason, false, false);
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001602
Johannes Bergc868cb352011-09-29 16:04:27 +02001603 sta_info_recalc_tim(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001604 } else {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001605 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1606 int tid;
1607
Johannes Bergaf818582009-11-06 11:35:50 +01001608 /*
Johannes Berg4049e092011-09-29 16:04:32 +02001609 * We need to release a frame that is buffered somewhere in the
1610 * driver ... it'll have to handle that.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001611 * Note that the driver also has to check the number of frames
1612 * on the TIDs we're releasing from - if there are more than
1613 * n_frames it has to set the more-data bit (if we didn't ask
1614 * it to set it anyway due to other buffered frames); if there
1615 * are fewer than n_frames it has to make sure to adjust that
1616 * to allow the service period to end properly.
Johannes Bergaf818582009-11-06 11:35:50 +01001617 */
Johannes Berg4049e092011-09-29 16:04:32 +02001618 drv_release_buffered_frames(local, sta, driver_release_tids,
Johannes Berg47086fc2011-09-29 16:04:33 +02001619 n_frames, reason, more_data);
Johannes Berg4049e092011-09-29 16:04:32 +02001620
1621 /*
1622 * Note that we don't recalculate the TIM bit here as it would
1623 * most likely have no effect at all unless the driver told us
Johannes Bergf9f760b2014-01-08 17:45:07 +01001624 * that the TID(s) became empty before returning here from the
Johannes Berg4049e092011-09-29 16:04:32 +02001625 * release function.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001626 * Either way, however, when the driver tells us that the TID(s)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001627 * became empty or we find that a txq became empty, we'll do the
1628 * TIM recalculation.
Johannes Berg4049e092011-09-29 16:04:32 +02001629 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001630
1631 if (!sta->sta.txq[0])
1632 return;
1633
1634 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1635 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1636
1637 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1638 continue;
1639
1640 sta_info_recalc_tim(sta);
1641 break;
1642 }
Johannes Bergaf818582009-11-06 11:35:50 +01001643 }
1644}
1645
Johannes Bergaf818582009-11-06 11:35:50 +01001646void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1647{
Johannes Berg47086fc2011-09-29 16:04:33 +02001648 u8 ignore_for_response = sta->sta.uapsd_queues;
Johannes Bergaf818582009-11-06 11:35:50 +01001649
Johannes Berg47086fc2011-09-29 16:04:33 +02001650 /*
1651 * If all ACs are delivery-enabled then we should reply
1652 * from any of them, if only some are enabled we reply
1653 * only from the non-enabled ones.
1654 */
1655 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1656 ignore_for_response = 0;
1657
1658 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1659 IEEE80211_FRAME_RELEASE_PSPOLL);
1660}
1661
1662void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1663{
1664 int n_frames = sta->sta.max_sp;
1665 u8 delivery_enabled = sta->sta.uapsd_queues;
1666
1667 /*
1668 * If we ever grow support for TSPEC this might happen if
1669 * the TSPEC update from hostapd comes in between a trigger
1670 * frame setting WLAN_STA_UAPSD in the RX path and this
1671 * actually getting called.
1672 */
1673 if (!delivery_enabled)
1674 return;
1675
Johannes Berg47086fc2011-09-29 16:04:33 +02001676 switch (sta->sta.max_sp) {
1677 case 1:
1678 n_frames = 2;
1679 break;
1680 case 2:
1681 n_frames = 4;
1682 break;
1683 case 3:
1684 n_frames = 6;
1685 break;
1686 case 0:
1687 /* XXX: what is a good value? */
Andrei Otcheretianski13a80982014-11-04 11:33:04 +02001688 n_frames = 128;
Johannes Berg47086fc2011-09-29 16:04:33 +02001689 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001690 }
Johannes Bergaf818582009-11-06 11:35:50 +01001691
Johannes Berg47086fc2011-09-29 16:04:33 +02001692 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1693 IEEE80211_FRAME_RELEASE_UAPSD);
Johannes Bergaf818582009-11-06 11:35:50 +01001694}
1695
1696void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1697 struct ieee80211_sta *pubsta, bool block)
1698{
1699 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1700
Johannes Bergb5878a22010-04-07 16:48:40 +02001701 trace_api_sta_block_awake(sta->local, pubsta, block);
1702
Johannes Berg5ac2e352014-05-27 16:32:27 +02001703 if (block) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001704 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001705 ieee80211_clear_fast_xmit(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001706 return;
1707 }
1708
1709 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1710 return;
1711
1712 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1713 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1714 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1715 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1716 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1717 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1718 /* must be asleep in this case */
1719 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1720 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1721 } else {
1722 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001723 ieee80211_check_fast_xmit(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001724 }
Johannes Bergaf818582009-11-06 11:35:50 +01001725}
1726EXPORT_SYMBOL(ieee80211_sta_block_awake);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001727
Johannes Berge9437892013-02-15 21:38:08 +01001728void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
Johannes Berg37fbd902011-09-29 16:04:39 +02001729{
1730 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1731 struct ieee80211_local *local = sta->local;
Johannes Berg37fbd902011-09-29 16:04:39 +02001732
1733 trace_api_eosp(local, pubsta);
1734
Johannes Berge9437892013-02-15 21:38:08 +01001735 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg37fbd902011-09-29 16:04:39 +02001736}
Johannes Berge9437892013-02-15 21:38:08 +01001737EXPORT_SYMBOL(ieee80211_sta_eosp);
Johannes Berg37fbd902011-09-29 16:04:39 +02001738
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001739void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1740{
1741 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1742 enum ieee80211_frame_release_type reason;
1743 bool more_data;
1744
1745 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1746
1747 reason = IEEE80211_FRAME_RELEASE_UAPSD;
1748 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1749 reason, 0);
1750
1751 ieee80211_send_null_response(sta, tid, reason, false, more_data);
1752}
1753EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1754
Johannes Berg042ec452011-09-29 16:04:26 +02001755void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1756 u8 tid, bool buffered)
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001757{
1758 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1759
Johannes Berg5a306f52012-11-14 23:22:21 +01001760 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
Johannes Berg042ec452011-09-29 16:04:26 +02001761 return;
1762
Johannes Berg1b000782013-12-19 10:47:48 +01001763 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1764
Johannes Berg948d8872011-09-29 16:04:29 +02001765 if (buffered)
1766 set_bit(tid, &sta->driver_buffered_tids);
1767 else
1768 clear_bit(tid, &sta->driver_buffered_tids);
1769
Johannes Bergc868cb352011-09-29 16:04:27 +02001770 sta_info_recalc_tim(sta);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001771}
Johannes Berg042ec452011-09-29 16:04:26 +02001772EXPORT_SYMBOL(ieee80211_sta_set_buffered);
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001773
Ayala Beker52cfa1d2016-03-17 15:41:39 +02001774static void
1775ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
1776{
1777 struct ieee80211_local *local = sdata->local;
1778 bool allow_p2p_go_ps = sdata->vif.p2p;
1779 struct sta_info *sta;
1780
1781 rcu_read_lock();
1782 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1783 if (sdata != sta->sdata ||
1784 !test_sta_flag(sta, WLAN_STA_ASSOC))
1785 continue;
1786 if (!sta->sta.support_p2p_ps) {
1787 allow_p2p_go_ps = false;
1788 break;
1789 }
1790 }
1791 rcu_read_unlock();
1792
1793 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
1794 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
1795 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
1796 }
1797}
1798
Johannes Berg83d5cc02012-01-12 09:31:10 +01001799int sta_info_move_state(struct sta_info *sta,
1800 enum ieee80211_sta_state new_state)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001801{
Johannes Berg8bf11d82011-12-15 11:17:37 +01001802 might_sleep();
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001803
1804 if (sta->sta_state == new_state)
1805 return 0;
1806
Johannes Bergf09603a2012-01-20 13:55:21 +01001807 /* check allowed transitions first */
1808
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001809 switch (new_state) {
1810 case IEEE80211_STA_NONE:
Johannes Bergf09603a2012-01-20 13:55:21 +01001811 if (sta->sta_state != IEEE80211_STA_AUTH)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001812 return -EINVAL;
1813 break;
1814 case IEEE80211_STA_AUTH:
Johannes Bergf09603a2012-01-20 13:55:21 +01001815 if (sta->sta_state != IEEE80211_STA_NONE &&
1816 sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001817 return -EINVAL;
1818 break;
1819 case IEEE80211_STA_ASSOC:
Johannes Bergf09603a2012-01-20 13:55:21 +01001820 if (sta->sta_state != IEEE80211_STA_AUTH &&
1821 sta->sta_state != IEEE80211_STA_AUTHORIZED)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001822 return -EINVAL;
1823 break;
1824 case IEEE80211_STA_AUTHORIZED:
Johannes Bergf09603a2012-01-20 13:55:21 +01001825 if (sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001826 return -EINVAL;
1827 break;
1828 default:
1829 WARN(1, "invalid state %d", new_state);
1830 return -EINVAL;
1831 }
1832
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001833 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1834 sta->sta.addr, new_state);
Johannes Bergf09603a2012-01-20 13:55:21 +01001835
1836 /*
1837 * notify the driver before the actual changes so it can
1838 * fail the transition
1839 */
1840 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1841 int err = drv_sta_state(sta->local, sta->sdata, sta,
1842 sta->sta_state, new_state);
1843 if (err)
1844 return err;
1845 }
1846
1847 /* reflect the change in all state variables */
1848
1849 switch (new_state) {
1850 case IEEE80211_STA_NONE:
1851 if (sta->sta_state == IEEE80211_STA_AUTH)
1852 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1853 break;
1854 case IEEE80211_STA_AUTH:
Ilan Peera7201a62015-12-13 13:41:43 +02001855 if (sta->sta_state == IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +01001856 set_bit(WLAN_STA_AUTH, &sta->_flags);
Ilan Peera7201a62015-12-13 13:41:43 +02001857 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
Johannes Bergf09603a2012-01-20 13:55:21 +01001858 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
Ilan Peera7201a62015-12-13 13:41:43 +02001859 ieee80211_recalc_min_chandef(sta->sdata);
Ayala Beker52cfa1d2016-03-17 15:41:39 +02001860 if (!sta->sta.support_p2p_ps)
1861 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
Ilan Peera7201a62015-12-13 13:41:43 +02001862 }
Johannes Bergf09603a2012-01-20 13:55:21 +01001863 break;
1864 case IEEE80211_STA_ASSOC:
1865 if (sta->sta_state == IEEE80211_STA_AUTH) {
1866 set_bit(WLAN_STA_ASSOC, &sta->_flags);
Ilan Peera7201a62015-12-13 13:41:43 +02001867 ieee80211_recalc_min_chandef(sta->sdata);
Ayala Beker52cfa1d2016-03-17 15:41:39 +02001868 if (!sta->sta.support_p2p_ps)
1869 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
Johannes Bergf09603a2012-01-20 13:55:21 +01001870 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001871 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1872 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1873 !sta->sdata->u.vlan.sta))
1874 atomic_dec(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001875 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001876 ieee80211_clear_fast_xmit(sta);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03001877 ieee80211_clear_fast_rx(sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001878 }
1879 break;
1880 case IEEE80211_STA_AUTHORIZED:
1881 if (sta->sta_state == IEEE80211_STA_ASSOC) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001882 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1883 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1884 !sta->sdata->u.vlan.sta))
1885 atomic_inc(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001886 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001887 ieee80211_check_fast_xmit(sta);
Johannes Berg49ddf8e2016-03-31 20:02:10 +03001888 ieee80211_check_fast_rx(sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001889 }
1890 break;
1891 default:
1892 break;
1893 }
1894
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001895 sta->sta_state = new_state;
1896
1897 return 0;
1898}
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001899
1900u8 sta_info_tx_streams(struct sta_info *sta)
1901{
1902 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1903 u8 rx_streams;
1904
1905 if (!sta->sta.ht_cap.ht_supported)
1906 return 1;
1907
1908 if (sta->sta.vht_cap.vht_supported) {
1909 int i;
1910 u16 tx_mcs_map =
1911 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1912
1913 for (i = 7; i >= 0; i--)
1914 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1915 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1916 return i + 1;
1917 }
1918
1919 if (ht_cap->mcs.rx_mask[3])
1920 rx_streams = 4;
1921 else if (ht_cap->mcs.rx_mask[2])
1922 rx_streams = 3;
1923 else if (ht_cap->mcs.rx_mask[1])
1924 rx_streams = 2;
1925 else
1926 rx_streams = 1;
1927
1928 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1929 return rx_streams;
1930
1931 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1932 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1933}
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001934
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001935static void sta_stats_decode_rate(struct ieee80211_local *local, u16 rate,
1936 struct rate_info *rinfo)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001937{
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001938 rinfo->bw = (rate & STA_STATS_RATE_BW_MASK) >>
1939 STA_STATS_RATE_BW_SHIFT;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001940
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001941 if (rate & STA_STATS_RATE_VHT) {
1942 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
1943 rinfo->mcs = rate & 0xf;
1944 rinfo->nss = (rate & 0xf0) >> 4;
1945 } else if (rate & STA_STATS_RATE_HT) {
1946 rinfo->flags = RATE_INFO_FLAGS_MCS;
1947 rinfo->mcs = rate & 0xff;
1948 } else if (rate & STA_STATS_RATE_LEGACY) {
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001949 struct ieee80211_supported_band *sband;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001950 u16 brate;
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001951 unsigned int shift;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001952
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001953 sband = local->hw.wiphy->bands[(rate >> 4) & 0xf];
1954 brate = sband->bitrates[rate & 0xf].bitrate;
1955 if (rinfo->bw == RATE_INFO_BW_5)
1956 shift = 2;
1957 else if (rinfo->bw == RATE_INFO_BW_10)
1958 shift = 1;
1959 else
1960 shift = 0;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001961 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
1962 }
1963
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001964 if (rate & STA_STATS_RATE_SGI)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001965 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001966}
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001967
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001968static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
1969{
1970 u16 rate = ACCESS_ONCE(sta->rx_stats.last_rate);
1971
1972 if (rate == STA_STATS_RATE_INVALID)
1973 rinfo->flags = 0;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001974 else
Johannes Berg4f6b1b32016-03-31 20:02:08 +03001975 sta_stats_decode_rate(sta->local, rate, rinfo);
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001976}
1977
Johannes Berg0f9c5a62016-03-31 20:02:09 +03001978static void sta_set_tidstats(struct sta_info *sta,
1979 struct cfg80211_tid_stats *tidstats,
1980 int tid)
1981{
1982 struct ieee80211_local *local = sta->local;
1983
1984 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
1985 unsigned int start;
1986
1987 do {
1988 start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
1989 tidstats->rx_msdu = sta->rx_stats.msdu[tid];
1990 } while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
1991
1992 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
1993 }
1994
1995 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
1996 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
1997 tidstats->tx_msdu = sta->tx_stats.msdu[tid];
1998 }
1999
2000 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2001 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2002 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2003 tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
2004 }
2005
2006 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2007 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2008 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2009 tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
2010 }
2011}
2012
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002013void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
2014{
2015 struct ieee80211_sub_if_data *sdata = sta->sdata;
2016 struct ieee80211_local *local = sdata->local;
John W. Linville9a244402014-07-25 10:22:36 -04002017 struct rate_control_ref *ref = NULL;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002018 u32 thr = 0;
2019 int i, ac;
2020
John W. Linville9a244402014-07-25 10:22:36 -04002021 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2022 ref = local->rate_ctrl;
2023
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002024 sinfo->generation = sdata->local->sta_generation;
2025
Johannes Berg225b8182015-01-21 21:09:02 +01002026 /* do before driver, so beacon filtering drivers have a
2027 * chance to e.g. just add the number of filtered beacons
2028 * (or just modify the value entirely, of course)
2029 */
2030 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2031 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2032
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002033 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2034
Johannes Berg319090b2014-11-17 14:08:11 +01002035 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
2036 BIT(NL80211_STA_INFO_STA_FLAGS) |
2037 BIT(NL80211_STA_INFO_BSS_PARAM) |
2038 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
Johannes Berg976bd9e2015-10-16 17:18:11 +02002039 BIT(NL80211_STA_INFO_RX_DROP_MISC);
2040
2041 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2042 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2043 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
2044 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002045
Arnd Bergmann84b00602015-09-30 13:26:36 +02002046 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002047 sinfo->inactive_time =
Johannes Bergb8da6b62016-03-31 20:02:07 +03002048 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002049
Johannes Berg319090b2014-11-17 14:08:11 +01002050 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
2051 BIT(NL80211_STA_INFO_TX_BYTES)))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002052 sinfo->tx_bytes = 0;
2053 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002054 sinfo->tx_bytes += sta->tx_stats.bytes[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01002055 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002056 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002057
Johannes Berg319090b2014-11-17 14:08:11 +01002058 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002059 sinfo->tx_packets = 0;
2060 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002061 sinfo->tx_packets += sta->tx_stats.packets[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01002062 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002063 }
2064
Johannes Berg319090b2014-11-17 14:08:11 +01002065 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
2066 BIT(NL80211_STA_INFO_RX_BYTES)))) {
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002067 unsigned int start;
2068
2069 do {
2070 start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
2071 sinfo->rx_bytes = sta->rx_stats.bytes;
2072 } while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
Johannes Berg319090b2014-11-17 14:08:11 +01002073 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002074 }
2075
Johannes Berg319090b2014-11-17 14:08:11 +01002076 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002077 sinfo->rx_packets = sta->rx_stats.packets;
Johannes Berg319090b2014-11-17 14:08:11 +01002078 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002079 }
2080
Johannes Berg319090b2014-11-17 14:08:11 +01002081 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002082 sinfo->tx_retries = sta->status_stats.retry_count;
Johannes Berg319090b2014-11-17 14:08:11 +01002083 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002084 }
2085
Johannes Berg319090b2014-11-17 14:08:11 +01002086 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002087 sinfo->tx_failed = sta->status_stats.retry_failed;
Johannes Berg319090b2014-11-17 14:08:11 +01002088 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002089 }
2090
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002091 sinfo->rx_dropped_misc = sta->rx_stats.dropped;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002092
Johannes Berg225b8182015-01-21 21:09:02 +01002093 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2094 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2095 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
2096 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2097 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2098 }
2099
Johannes Berg30686bf2015-06-02 21:39:54 +02002100 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2101 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
Johannes Berg319090b2014-11-17 14:08:11 +01002102 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002103 sinfo->signal = (s8)sta->rx_stats.last_signal;
Johannes Berg319090b2014-11-17 14:08:11 +01002104 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002105 }
2106
Johannes Berg319090b2014-11-17 14:08:11 +01002107 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
Johannes Berg40d9a382015-07-13 12:26:46 +02002108 sinfo->signal_avg =
Johannes Berg0be6ed12016-03-31 20:02:05 +03002109 -ewma_signal_read(&sta->rx_stats_avg.signal);
Johannes Berg319090b2014-11-17 14:08:11 +01002110 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002111 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002112 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002113
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002114 if (sta->rx_stats.chains &&
Johannes Berg319090b2014-11-17 14:08:11 +01002115 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2116 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2117 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2118 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002119
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002120 sinfo->chains = sta->rx_stats.chains;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002121 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002122 sinfo->chain_signal[i] =
2123 sta->rx_stats.chain_signal_last[i];
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002124 sinfo->chain_signal_avg[i] =
Johannes Berg0be6ed12016-03-31 20:02:05 +03002125 -ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002126 }
2127 }
2128
Johannes Berg319090b2014-11-17 14:08:11 +01002129 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002130 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2131 &sinfo->txrate);
Johannes Berg319090b2014-11-17 14:08:11 +01002132 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002133 }
2134
Johannes Berg319090b2014-11-17 14:08:11 +01002135 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002136 sta_set_rate_info_rx(sta, &sinfo->rxrate);
Johannes Berg319090b2014-11-17 14:08:11 +01002137 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002138 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002139
Johannes Berg79c892b2014-11-21 14:26:31 +01002140 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
2141 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
2142 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
2143
Johannes Berg0f9c5a62016-03-31 20:02:09 +03002144 sta_set_tidstats(sta, tidstats, i);
Johannes Berg79c892b2014-11-21 14:26:31 +01002145 }
2146
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002147 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2148#ifdef CONFIG_MAC80211_MESH
Johannes Berg319090b2014-11-17 14:08:11 +01002149 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2150 BIT(NL80211_STA_INFO_PLID) |
2151 BIT(NL80211_STA_INFO_PLINK_STATE) |
2152 BIT(NL80211_STA_INFO_LOCAL_PM) |
2153 BIT(NL80211_STA_INFO_PEER_PM) |
2154 BIT(NL80211_STA_INFO_NONPEER_PM);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002155
Johannes Berg433f5bc2015-06-17 10:31:00 +02002156 sinfo->llid = sta->mesh->llid;
2157 sinfo->plid = sta->mesh->plid;
2158 sinfo->plink_state = sta->mesh->plink_state;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002159 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
Johannes Berg319090b2014-11-17 14:08:11 +01002160 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
Johannes Berg433f5bc2015-06-17 10:31:00 +02002161 sinfo->t_offset = sta->mesh->t_offset;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002162 }
Johannes Berg433f5bc2015-06-17 10:31:00 +02002163 sinfo->local_pm = sta->mesh->local_pm;
2164 sinfo->peer_pm = sta->mesh->peer_pm;
2165 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002166#endif
2167 }
2168
2169 sinfo->bss_param.flags = 0;
2170 if (sdata->vif.bss_conf.use_cts_prot)
2171 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2172 if (sdata->vif.bss_conf.use_short_preamble)
2173 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2174 if (sdata->vif.bss_conf.use_short_slot)
2175 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
Emmanuel Grumbach785e21a2014-09-03 15:25:04 +03002176 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002177 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2178
2179 sinfo->sta_flags.set = 0;
2180 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2181 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2182 BIT(NL80211_STA_FLAG_WME) |
2183 BIT(NL80211_STA_FLAG_MFP) |
2184 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2185 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2186 BIT(NL80211_STA_FLAG_TDLS_PEER);
2187 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2188 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2189 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2190 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
Johannes Berga74a8c82014-07-22 14:50:47 +02002191 if (sta->sta.wme)
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002192 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2193 if (test_sta_flag(sta, WLAN_STA_MFP))
2194 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2195 if (test_sta_flag(sta, WLAN_STA_AUTH))
2196 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2197 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2198 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2199 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2200 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2201
2202 /* check if the driver has a SW RC implementation */
2203 if (ref && ref->ops->get_expected_throughput)
2204 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2205 else
2206 thr = drv_get_expected_throughput(local, &sta->sta);
2207
2208 if (thr != 0) {
Johannes Berg319090b2014-11-17 14:08:11 +01002209 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002210 sinfo->expected_throughput = thr;
2211 }
2212}
Johannes Bergb8da6b62016-03-31 20:02:07 +03002213
2214unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2215{
2216 if (time_after(sta->rx_stats.last_rx, sta->status_stats.last_ack))
2217 return sta->rx_stats.last_rx;
2218 return sta->status_stats.last_ack;
2219}