blob: 5894c0a1c01ff64afe28d2fbaaf793344603a2b7 [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
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02005 * Copyright (C) 2015 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 Berg71ec3752012-01-20 13:55:20 +0100338 sta->sta_state = IEEE80211_STA_NONE;
339
Liad Kaufmanb6da9112014-11-19 13:47:38 +0200340 /* Mark TID as unreserved */
341 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
342
Arnd Bergmann84b00602015-09-30 13:26:36 +0200343 sta->last_connected = ktime_get_seconds();
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200344 ewma_signal_init(&sta->rx_stats.avg_signal);
345 for (i = 0; i < ARRAY_SIZE(sta->rx_stats.chain_signal_avg); i++)
346 ewma_signal_init(&sta->rx_stats.chain_signal_avg[i]);
Bruno Randolf541a45a2010-12-02 19:12:43 +0900347
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100348 if (local->ops->wake_tx_queue) {
349 void *txq_data;
350 int size = sizeof(struct txq_info) +
351 ALIGN(hw->txq_data_size, sizeof(void *));
352
353 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
354 if (!txq_data)
355 goto free;
356
357 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
358 struct txq_info *txq = txq_data + i * size;
359
360 ieee80211_init_tx_queue(sdata, sta, txq, i);
361 }
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100362 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700363
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100364 if (sta_prepare_rate_control(local, sta, gfp))
365 goto free_txq;
366
Johannes Berg5a306f52012-11-14 23:22:21 +0100367 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berga622ab72010-06-10 10:21:39 +0200368 /*
369 * timer_to_tid must be initialized with identity mapping
370 * to enable session_timer's data differentiation. See
371 * sta_rx_agg_session_timer_expired for usage.
372 */
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200373 sta->timer_to_tid[i] = i;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200374 }
Johannes Berg948d8872011-09-29 16:04:29 +0200375 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
376 skb_queue_head_init(&sta->ps_tx_buf[i]);
377 skb_queue_head_init(&sta->tx_filtered[i]);
378 }
Johannes Berg73651ee2008-02-25 16:27:47 +0100379
Johannes Berg5a306f52012-11-14 23:22:21 +0100380 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700381 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
Senthil Balasubramaniancccaec92009-05-14 18:42:08 +0530382
Johannes Bergaf0ed692013-02-12 14:21:00 +0100383 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300384 if (sdata->vif.type == NL80211_IFTYPE_AP ||
385 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
386 struct ieee80211_supported_band *sband =
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100387 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300388 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
389 IEEE80211_HT_CAP_SM_PS_SHIFT;
390 /*
391 * Assume that hostapd advertises our caps in the beacon and
392 * this is the known_smps_mode for a station that just assciated
393 */
394 switch (smps) {
395 case WLAN_HT_SMPS_CONTROL_DISABLED:
396 sta->known_smps_mode = IEEE80211_SMPS_OFF;
397 break;
398 case WLAN_HT_SMPS_CONTROL_STATIC:
399 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
400 break;
401 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
402 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
403 break;
404 default:
405 WARN_ON(1);
406 }
407 }
Johannes Bergaf0ed692013-02-12 14:21:00 +0100408
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200409 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
Johannes Bergef04a292014-01-06 15:56:59 +0100410
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100411 return sta;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100412
413free_txq:
414 if (sta->sta.txq[0])
415 kfree(to_txq_info(sta->sta.txq[0]));
416free:
Johannes Berg433f5bc2015-06-17 10:31:00 +0200417#ifdef CONFIG_MAC80211_MESH
418 kfree(sta->mesh);
419#endif
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100420 kfree(sta);
421 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +0100422}
423
Guy Eilam8c71df72011-08-17 15:18:14 +0300424static int sta_info_insert_check(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100425{
Johannes Berg34e89502010-02-03 13:59:58 +0100426 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg34e89502010-02-03 13:59:58 +0100427
Johannes Berg03e44972008-02-27 09:56:40 +0100428 /*
429 * Can't be a WARN_ON because it can be triggered through a race:
430 * something inserts a STA (on one CPU) without holding the RTNL
431 * and another CPU turns off the net device.
432 */
Guy Eilam8c71df72011-08-17 15:18:14 +0300433 if (unlikely(!ieee80211_sdata_running(sdata)))
434 return -ENETDOWN;
Johannes Berg03e44972008-02-27 09:56:40 +0100435
Joe Perchesb203ca32012-05-08 18:56:52 +0000436 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
Guy Eilam8c71df72011-08-17 15:18:14 +0300437 is_multicast_ether_addr(sta->sta.addr)))
438 return -EINVAL;
439
Johannes Berg31104892015-10-22 17:35:19 +0200440 /* Strictly speaking this isn't necessary as we hold the mutex, but
441 * the rhashtable code can't really deal with that distinction. We
442 * do require the mutex for correctness though.
443 */
444 rcu_read_lock();
445 lockdep_assert_held(&sdata->local->sta_mtx);
446 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
447 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
448 rcu_read_unlock();
449 return -ENOTUNIQ;
450 }
451 rcu_read_unlock();
452
Guy Eilam8c71df72011-08-17 15:18:14 +0300453 return 0;
454}
455
Johannes Bergf09603a2012-01-20 13:55:21 +0100456static int sta_info_insert_drv_state(struct ieee80211_local *local,
457 struct ieee80211_sub_if_data *sdata,
458 struct sta_info *sta)
459{
460 enum ieee80211_sta_state state;
461 int err = 0;
462
463 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
464 err = drv_sta_state(local, sdata, sta, state, state + 1);
465 if (err)
466 break;
467 }
468
469 if (!err) {
Johannes Berga4ec45a2012-01-20 13:55:22 +0100470 /*
471 * Drivers using legacy sta_add/sta_remove callbacks only
472 * get uploaded set to true after sta_add is called.
473 */
474 if (!local->ops->sta_add)
475 sta->uploaded = true;
Johannes Bergf09603a2012-01-20 13:55:21 +0100476 return 0;
477 }
478
479 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200480 sdata_info(sdata,
481 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
482 sta->sta.addr, state + 1, err);
Johannes Bergf09603a2012-01-20 13:55:21 +0100483 err = 0;
484 }
485
486 /* unwind on error */
487 for (; state > IEEE80211_STA_NOTEXIST; state--)
488 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
489
490 return err;
491}
492
Guy Eilam8c71df72011-08-17 15:18:14 +0300493/*
494 * should be called with sta_mtx locked
495 * this function replaces the mutex lock
496 * with a RCU lock
497 */
Johannes Berg4d339602011-12-15 11:24:20 +0100498static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
Guy Eilam8c71df72011-08-17 15:18:14 +0300499{
500 struct ieee80211_local *local = sta->local;
501 struct ieee80211_sub_if_data *sdata = sta->sdata;
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100502 struct station_info *sinfo;
Guy Eilam8c71df72011-08-17 15:18:14 +0300503 int err = 0;
504
505 lockdep_assert_held(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +0100506
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100507 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
508 if (!sinfo) {
509 err = -ENOMEM;
510 goto out_err;
511 }
512
Johannes Berg7852e362012-01-20 13:55:24 +0100513 /* check if STA exists already */
514 if (sta_info_get_bss(sdata, sta->sta.addr)) {
515 err = -EEXIST;
516 goto out_err;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100517 }
Johannes Berg32bfd352007-12-19 01:31:26 +0100518
Johannes Berg7852e362012-01-20 13:55:24 +0100519 local->num_sta++;
520 local->sta_generation++;
521 smp_mb();
Johannes Berg4d339602011-12-15 11:24:20 +0100522
Johannes Berg5108ca82014-02-17 20:49:03 +0100523 /* simplify things and don't accept BA sessions yet */
524 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
525
Johannes Berg7852e362012-01-20 13:55:24 +0100526 /* make the station visible */
527 sta_info_hash_add(local, sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100528
Arik Nemtsov2bad77482014-10-22 12:32:16 +0300529 list_add_tail_rcu(&sta->list, &local->sta_list);
Johannes Berg83d5cc02012-01-12 09:31:10 +0100530
Johannes Berg5108ca82014-02-17 20:49:03 +0100531 /* notify driver */
532 err = sta_info_insert_drv_state(local, sdata, sta);
533 if (err)
534 goto out_remove;
535
Johannes Berg7852e362012-01-20 13:55:24 +0100536 set_sta_flag(sta, WLAN_STA_INSERTED);
Johannes Berg5108ca82014-02-17 20:49:03 +0100537 /* accept BA sessions now */
538 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg4d339602011-12-15 11:24:20 +0100539
Johannes Berg7852e362012-01-20 13:55:24 +0100540 ieee80211_sta_debugfs_add(sta);
541 rate_control_add_sta_debugfs(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100542
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100543 sinfo->generation = local->sta_generation;
544 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
545 kfree(sinfo);
Johannes Bergd0709a62008-02-25 16:27:46 +0100546
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200547 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700548
Johannes Berg34e89502010-02-03 13:59:58 +0100549 /* move reference to rcu-protected */
550 rcu_read_lock();
551 mutex_unlock(&local->sta_mtx);
Jiri Bence9f207f2007-05-05 11:46:38 -0700552
Johannes Berg73651ee2008-02-25 16:27:47 +0100553 if (ieee80211_vif_is_mesh(&sdata->vif))
554 mesh_accept_plinks_update(sdata);
555
556 return 0;
Johannes Berg5108ca82014-02-17 20:49:03 +0100557 out_remove:
558 sta_info_hash_del(local, sta);
559 list_del_rcu(&sta->list);
560 local->num_sta--;
561 synchronize_net();
562 __cleanup_single_sta(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100563 out_err:
564 mutex_unlock(&local->sta_mtx);
565 rcu_read_lock();
566 return err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300567}
568
569int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
570{
571 struct ieee80211_local *local = sta->local;
Zhao, Gang308f7fc2014-04-21 12:53:00 +0800572 int err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300573
Johannes Berg4d339602011-12-15 11:24:20 +0100574 might_sleep();
575
Johannes Berg31104892015-10-22 17:35:19 +0200576 mutex_lock(&local->sta_mtx);
577
Guy Eilam8c71df72011-08-17 15:18:14 +0300578 err = sta_info_insert_check(sta);
579 if (err) {
Johannes Berg31104892015-10-22 17:35:19 +0200580 mutex_unlock(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700581 rcu_read_lock();
582 goto out_free;
583 }
584
Johannes Berg4d339602011-12-15 11:24:20 +0100585 err = sta_info_insert_finish(sta);
Guy Eilam8c71df72011-08-17 15:18:14 +0300586 if (err)
Johannes Berg004c8722008-02-20 11:21:35 +0100587 goto out_free;
Johannes Bergd0709a62008-02-25 16:27:46 +0100588
Johannes Berg004c8722008-02-20 11:21:35 +0100589 return 0;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200590 out_free:
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100591 sta_info_free(local, sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200592 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -0700593}
594
Johannes Berg34e89502010-02-03 13:59:58 +0100595int sta_info_insert(struct sta_info *sta)
596{
597 int err = sta_info_insert_rcu(sta);
598
599 rcu_read_unlock();
600
601 return err;
602}
603
Marco Porschd012a602012-10-10 12:39:50 -0700604static inline void __bss_tim_set(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100605{
606 /*
607 * This format has been mandated by the IEEE specifications,
608 * so this line may not be changed to use the __set_bit() format.
609 */
Marco Porschd012a602012-10-10 12:39:50 -0700610 tim[id / 8] |= (1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100611}
612
Marco Porschd012a602012-10-10 12:39:50 -0700613static inline void __bss_tim_clear(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100614{
615 /*
616 * This format has been mandated by the IEEE specifications,
617 * so this line may not be changed to use the __clear_bit() format.
618 */
Marco Porschd012a602012-10-10 12:39:50 -0700619 tim[id / 8] &= ~(1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100620}
621
Ilan Peer3d5839b2013-03-05 15:27:20 +0200622static inline bool __bss_tim_get(u8 *tim, u16 id)
623{
624 /*
625 * This format has been mandated by the IEEE specifications,
626 * so this line may not be changed to use the test_bit() format.
627 */
628 return tim[id / 8] & (1 << (id % 8));
629}
630
Johannes Berg948d8872011-09-29 16:04:29 +0200631static unsigned long ieee80211_tids_for_ac(int ac)
Johannes Berg004c8722008-02-20 11:21:35 +0100632{
Johannes Berg948d8872011-09-29 16:04:29 +0200633 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
634 switch (ac) {
635 case IEEE80211_AC_VO:
636 return BIT(6) | BIT(7);
637 case IEEE80211_AC_VI:
638 return BIT(4) | BIT(5);
639 case IEEE80211_AC_BE:
640 return BIT(0) | BIT(3);
641 case IEEE80211_AC_BK:
642 return BIT(1) | BIT(2);
643 default:
644 WARN_ON(1);
645 return 0;
Johannes Bergd0709a62008-02-25 16:27:46 +0100646 }
Johannes Berg004c8722008-02-20 11:21:35 +0100647}
648
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100649static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
Johannes Berg004c8722008-02-20 11:21:35 +0100650{
Johannes Bergc868cb352011-09-29 16:04:27 +0200651 struct ieee80211_local *local = sta->local;
Marco Porschd012a602012-10-10 12:39:50 -0700652 struct ps_data *ps;
Johannes Berg948d8872011-09-29 16:04:29 +0200653 bool indicate_tim = false;
654 u8 ignore_for_tim = sta->sta.uapsd_queues;
655 int ac;
Bob Copelanda69bd8e2015-07-14 08:31:58 -0400656 u16 id = sta->sta.aid;
Johannes Berg004c8722008-02-20 11:21:35 +0100657
Marco Porschd012a602012-10-10 12:39:50 -0700658 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
659 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
660 if (WARN_ON_ONCE(!sta->sdata->bss))
661 return;
662
663 ps = &sta->sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100664#ifdef CONFIG_MAC80211_MESH
665 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
666 ps = &sta->sdata->u.mesh.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100667#endif
Marco Porschd012a602012-10-10 12:39:50 -0700668 } else {
Johannes Bergc868cb352011-09-29 16:04:27 +0200669 return;
Marco Porschd012a602012-10-10 12:39:50 -0700670 }
Johannes Berg3e122be2008-07-09 14:40:34 +0200671
Johannes Bergc868cb352011-09-29 16:04:27 +0200672 /* No need to do anything if the driver does all */
Johannes Berg30686bf2015-06-02 21:39:54 +0200673 if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
Johannes Bergc868cb352011-09-29 16:04:27 +0200674 return;
Johannes Berg004c8722008-02-20 11:21:35 +0100675
Johannes Bergc868cb352011-09-29 16:04:27 +0200676 if (sta->dead)
677 goto done;
Johannes Berg3e122be2008-07-09 14:40:34 +0200678
Johannes Berg948d8872011-09-29 16:04:29 +0200679 /*
680 * If all ACs are delivery-enabled then we should build
681 * the TIM bit for all ACs anyway; if only some are then
682 * we ignore those and build the TIM bit using only the
683 * non-enabled ones.
684 */
685 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
686 ignore_for_tim = 0;
Johannes Berg3e122be2008-07-09 14:40:34 +0200687
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100688 if (ignore_pending)
689 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
690
Johannes Berg948d8872011-09-29 16:04:29 +0200691 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
692 unsigned long tids;
693
694 if (ignore_for_tim & BIT(ac))
695 continue;
696
697 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
698 !skb_queue_empty(&sta->ps_tx_buf[ac]);
699 if (indicate_tim)
700 break;
701
702 tids = ieee80211_tids_for_ac(ac);
703
704 indicate_tim |=
705 sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100706 indicate_tim |=
707 sta->txq_buffered_tids & tids;
Johannes Bergd0709a62008-02-25 16:27:46 +0100708 }
Johannes Berg004c8722008-02-20 11:21:35 +0100709
Johannes Bergc868cb352011-09-29 16:04:27 +0200710 done:
Johannes Berg65f704a2013-02-13 17:39:53 +0100711 spin_lock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100712
Ilan Peer3d5839b2013-03-05 15:27:20 +0200713 if (indicate_tim == __bss_tim_get(ps->tim, id))
714 goto out_unlock;
715
Johannes Berg948d8872011-09-29 16:04:29 +0200716 if (indicate_tim)
Marco Porschd012a602012-10-10 12:39:50 -0700717 __bss_tim_set(ps->tim, id);
Johannes Bergc868cb352011-09-29 16:04:27 +0200718 else
Marco Porschd012a602012-10-10 12:39:50 -0700719 __bss_tim_clear(ps->tim, id);
Johannes Berg3e122be2008-07-09 14:40:34 +0200720
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100721 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
Johannes Bergc868cb352011-09-29 16:04:27 +0200722 local->tim_in_locked_section = true;
Johannes Berg948d8872011-09-29 16:04:29 +0200723 drv_set_tim(local, &sta->sta, indicate_tim);
Johannes Bergc868cb352011-09-29 16:04:27 +0200724 local->tim_in_locked_section = false;
Johannes Berg004c8722008-02-20 11:21:35 +0100725 }
Johannes Berg004c8722008-02-20 11:21:35 +0100726
Ilan Peer3d5839b2013-03-05 15:27:20 +0200727out_unlock:
Johannes Berg65f704a2013-02-13 17:39:53 +0100728 spin_unlock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100729}
730
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100731void sta_info_recalc_tim(struct sta_info *sta)
732{
733 __sta_info_recalc_tim(sta, false);
734}
735
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200736static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700737{
Johannes Berge039fa42008-05-15 12:55:29 +0200738 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700739 int timeout;
740
741 if (!skb)
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200742 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700743
Johannes Berge039fa42008-05-15 12:55:29 +0200744 info = IEEE80211_SKB_CB(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700745
746 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200747 timeout = (sta->listen_interval *
748 sta->sdata->vif.bss_conf.beacon_int *
749 32 / 15625) * HZ;
Jiri Bencf0706e82007-05-05 11:45:53 -0700750 if (timeout < STA_TX_BUFFER_EXPIRE)
751 timeout = STA_TX_BUFFER_EXPIRE;
Johannes Berge039fa42008-05-15 12:55:29 +0200752 return time_after(jiffies, info->control.jiffies + timeout);
Jiri Bencf0706e82007-05-05 11:45:53 -0700753}
754
755
Johannes Berg948d8872011-09-29 16:04:29 +0200756static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
757 struct sta_info *sta, int ac)
Jiri Bencf0706e82007-05-05 11:45:53 -0700758{
759 unsigned long flags;
760 struct sk_buff *skb;
761
Johannes Berg60750392011-09-29 16:04:28 +0200762 /*
763 * First check for frames that should expire on the filtered
764 * queue. Frames here were rejected by the driver and are on
765 * a separate queue to avoid reordering with normal PS-buffered
766 * frames. They also aren't accounted for right now in the
767 * total_ps_buffered counter.
768 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700769 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200770 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
771 skb = skb_peek(&sta->tx_filtered[ac]);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200772 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200773 skb = __skb_dequeue(&sta->tx_filtered[ac]);
Johannes Berg836341a2008-02-20 02:07:21 +0100774 else
Jiri Bencf0706e82007-05-05 11:45:53 -0700775 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200776 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700777
Johannes Berg60750392011-09-29 16:04:28 +0200778 /*
779 * Frames are queued in order, so if this one
780 * hasn't expired yet we can stop testing. If
781 * we actually reached the end of the queue we
782 * also need to stop, of course.
783 */
784 if (!skb)
785 break;
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200786 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg60750392011-09-29 16:04:28 +0200787 }
788
789 /*
790 * Now also check the normal PS-buffered queue, this will
791 * only find something if the filtered queue was emptied
792 * since the filtered frames are all before the normal PS
793 * buffered frames.
794 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700795 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200796 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
797 skb = skb_peek(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700798 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200799 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700800 else
801 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200802 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700803
Johannes Berg60750392011-09-29 16:04:28 +0200804 /*
805 * frames are queued in order, so if this one
806 * hasn't expired yet (or we reached the end of
807 * the queue) we can stop testing
808 */
Johannes Berg836341a2008-02-20 02:07:21 +0100809 if (!skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700810 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100811
Johannes Berg836341a2008-02-20 02:07:21 +0100812 local->total_ps_buffered--;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200813 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
814 sta->sta.addr);
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200815 ieee80211_free_txskb(&local->hw, skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700816 }
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300817
Johannes Berg60750392011-09-29 16:04:28 +0200818 /*
819 * Finally, recalculate the TIM bit for this station -- it might
820 * now be clear because the station was too slow to retrieve its
821 * frames.
822 */
823 sta_info_recalc_tim(sta);
824
825 /*
826 * Return whether there are any frames still buffered, this is
827 * used to check whether the cleanup timer still needs to run,
828 * if there are no frames we don't need to rearm the timer.
829 */
Johannes Berg948d8872011-09-29 16:04:29 +0200830 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
831 skb_queue_empty(&sta->tx_filtered[ac]));
832}
833
834static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
835 struct sta_info *sta)
836{
837 bool have_buffered = false;
838 int ac;
839
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100840 /* This is only necessary for stations on BSS/MBSS interfaces */
841 if (!sta->sdata->bss &&
842 !ieee80211_vif_is_mesh(&sta->sdata->vif))
Johannes Berg948d8872011-09-29 16:04:29 +0200843 return false;
844
845 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
846 have_buffered |=
847 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
848
849 return have_buffered;
Jiri Bencf0706e82007-05-05 11:45:53 -0700850}
851
Johannes Bergd7782072013-12-04 23:12:31 +0100852static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100853{
854 struct ieee80211_local *local;
855 struct ieee80211_sub_if_data *sdata;
Johannes Berg6d10e462013-03-06 23:09:11 +0100856 int ret;
Johannes Berg34e89502010-02-03 13:59:58 +0100857
858 might_sleep();
859
860 if (!sta)
861 return -ENOENT;
862
863 local = sta->local;
864 sdata = sta->sdata;
865
Johannes Berg83d5cc02012-01-12 09:31:10 +0100866 lockdep_assert_held(&local->sta_mtx);
867
Johannes Berg098a6072010-04-06 11:18:47 +0200868 /*
869 * Before removing the station from the driver and
870 * rate control, it might still start new aggregation
871 * sessions -- block that to make sure the tear-down
872 * will be sufficient.
873 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200874 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Bergc82c4a82012-07-18 13:31:31 +0200875 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
Johannes Berg098a6072010-04-06 11:18:47 +0200876
Johannes Berg34e89502010-02-03 13:59:58 +0100877 ret = sta_info_hash_del(local, sta);
Johannes Bergb01711b2013-12-04 20:25:27 +0100878 if (WARN_ON(ret))
Johannes Berg34e89502010-02-03 13:59:58 +0100879 return ret;
880
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200881 /*
882 * for TDLS peers, make sure to return to the base channel before
883 * removal.
884 */
885 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
886 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
887 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
888 }
889
Arik Nemtsov794454c2012-06-03 23:32:32 +0300890 list_del_rcu(&sta->list);
Eliad Pelleref044762015-11-17 10:24:37 +0200891 sta->removed = true;
Johannes Berg4d339602011-12-15 11:24:20 +0100892
Johannes Berg6a9d1b92013-12-04 22:39:17 +0100893 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
894
Johannes Berga710c812013-12-04 20:11:06 +0100895 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
896 rcu_access_pointer(sdata->u.vlan.sta) == sta)
897 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
898
Johannes Bergd7782072013-12-04 23:12:31 +0100899 return 0;
900}
901
902static void __sta_info_destroy_part2(struct sta_info *sta)
903{
904 struct ieee80211_local *local = sta->local;
905 struct ieee80211_sub_if_data *sdata = sta->sdata;
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100906 struct station_info *sinfo;
Johannes Bergd7782072013-12-04 23:12:31 +0100907 int ret;
908
909 /*
910 * NOTE: This assumes at least synchronize_net() was done
911 * after _part1 and before _part2!
912 */
913
914 might_sleep();
915 lockdep_assert_held(&local->sta_mtx);
916
Johannes Bergc8782072013-12-04 23:05:45 +0100917 /* now keys can no longer be reached */
Johannes Berg6d10e462013-03-06 23:09:11 +0100918 ieee80211_free_sta_keys(local, sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100919
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100920 /* disable TIM bit - last chance to tell driver */
921 __sta_info_recalc_tim(sta, true);
922
Johannes Berg34e89502010-02-03 13:59:58 +0100923 sta->dead = true;
924
Johannes Berg34e89502010-02-03 13:59:58 +0100925 local->num_sta--;
926 local->sta_generation++;
927
Johannes Berg83d5cc02012-01-12 09:31:10 +0100928 while (sta->sta_state > IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100929 ret = sta_info_move_state(sta, sta->sta_state - 1);
930 if (ret) {
Johannes Berg83d5cc02012-01-12 09:31:10 +0100931 WARN_ON_ONCE(1);
932 break;
933 }
934 }
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100935
Johannes Bergf09603a2012-01-20 13:55:21 +0100936 if (sta->uploaded) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100937 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
938 IEEE80211_STA_NOTEXIST);
939 WARN_ON_ONCE(ret != 0);
940 }
Johannes Berg34e89502010-02-03 13:59:58 +0100941
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200942 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
943
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100944 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
945 if (sinfo)
946 sta_set_sinfo(sta, sinfo);
947 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
948 kfree(sinfo);
Jouni Malinenec15e682011-03-23 15:29:52 +0200949
Johannes Berg34e89502010-02-03 13:59:58 +0100950 rate_control_remove_sta_debugfs(sta);
951 ieee80211_sta_debugfs_remove(sta);
952
Johannes Bergd34ba212013-12-04 22:46:11 +0100953 cleanup_single_sta(sta);
Johannes Bergd7782072013-12-04 23:12:31 +0100954}
955
956int __must_check __sta_info_destroy(struct sta_info *sta)
957{
958 int err = __sta_info_destroy_part1(sta);
959
960 if (err)
961 return err;
962
963 synchronize_net();
964
965 __sta_info_destroy_part2(sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100966
967 return 0;
968}
969
970int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
971{
972 struct sta_info *sta;
973 int ret;
974
975 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100976 sta = sta_info_get(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100977 ret = __sta_info_destroy(sta);
978 mutex_unlock(&sdata->local->sta_mtx);
979
980 return ret;
981}
982
983int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
984 const u8 *addr)
985{
986 struct sta_info *sta;
987 int ret;
988
989 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100990 sta = sta_info_get_bss(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100991 ret = __sta_info_destroy(sta);
992 mutex_unlock(&sdata->local->sta_mtx);
993
994 return ret;
995}
Jiri Bencf0706e82007-05-05 11:45:53 -0700996
997static void sta_info_cleanup(unsigned long data)
998{
999 struct ieee80211_local *local = (struct ieee80211_local *) data;
1000 struct sta_info *sta;
Juuso Oikarinen3393a602010-04-19 10:12:52 +03001001 bool timer_needed = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001002
Johannes Bergd0709a62008-02-25 16:27:46 +01001003 rcu_read_lock();
1004 list_for_each_entry_rcu(sta, &local->sta_list, list)
Juuso Oikarinen3393a602010-04-19 10:12:52 +03001005 if (sta_info_cleanup_expire_buffered(local, sta))
1006 timer_needed = true;
Johannes Bergd0709a62008-02-25 16:27:46 +01001007 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001008
Johannes Berg5bb644a2009-05-17 11:40:42 +02001009 if (local->quiescing)
1010 return;
1011
Juuso Oikarinen3393a602010-04-19 10:12:52 +03001012 if (!timer_needed)
1013 return;
1014
Johannes Berg26d59532011-04-01 13:52:48 +02001015 mod_timer(&local->sta_cleanup,
1016 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
Jiri Bencf0706e82007-05-05 11:45:53 -07001017}
1018
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001019u32 sta_addr_hash(const void *key, u32 length, u32 seed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001020{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001021 return jhash(key, ETH_ALEN, seed);
1022}
1023
1024int sta_info_init(struct ieee80211_local *local)
1025{
1026 int err;
1027
1028 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
1029 if (err)
1030 return err;
1031
Johannes Berg4d339602011-12-15 11:24:20 +01001032 spin_lock_init(&local->tim_lock);
Johannes Berg34e89502010-02-03 13:59:58 +01001033 mutex_init(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07001034 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001035
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001036 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1037 (unsigned long)local);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001038 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001039}
1040
1041void sta_info_stop(struct ieee80211_local *local)
1042{
Johannes Berga56f9922012-12-13 23:08:52 +01001043 del_timer_sync(&local->sta_cleanup);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001044 rhashtable_destroy(&local->sta_hash);
Jiri Bencf0706e82007-05-05 11:45:53 -07001045}
1046
Johannes Berg051007d2012-12-13 23:49:02 +01001047
Johannes Berge7162512013-12-04 23:18:37 +01001048int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
Jiri Bencf0706e82007-05-05 11:45:53 -07001049{
Johannes Bergb998e8b2012-12-13 23:07:46 +01001050 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001051 struct sta_info *sta, *tmp;
Johannes Bergd7782072013-12-04 23:12:31 +01001052 LIST_HEAD(free_list);
Johannes Berg44213b52008-02-25 16:27:49 +01001053 int ret = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001054
Johannes Bergd0709a62008-02-25 16:27:46 +01001055 might_sleep();
1056
Johannes Berge7162512013-12-04 23:18:37 +01001057 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1058 WARN_ON(vlans && !sdata->bss);
1059
Johannes Berg34e89502010-02-03 13:59:58 +01001060 mutex_lock(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +01001061 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Johannes Berge7162512013-12-04 23:18:37 +01001062 if (sdata == sta->sdata ||
1063 (vlans && sdata->bss == sta->sdata->bss)) {
Johannes Bergd7782072013-12-04 23:12:31 +01001064 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1065 list_add(&sta->free_list, &free_list);
Johannes Berg34316832012-02-25 21:40:46 +01001066 ret++;
1067 }
Johannes Berg34e89502010-02-03 13:59:58 +01001068 }
Johannes Bergd7782072013-12-04 23:12:31 +01001069
1070 if (!list_empty(&free_list)) {
1071 synchronize_net();
1072 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1073 __sta_info_destroy_part2(sta);
1074 }
Johannes Berg34e89502010-02-03 13:59:58 +01001075 mutex_unlock(&local->sta_mtx);
Johannes Berg44213b52008-02-25 16:27:49 +01001076
Johannes Berg051007d2012-12-13 23:49:02 +01001077 return ret;
1078}
1079
Johannes Berg24723d12008-09-11 00:01:46 +02001080void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1081 unsigned long exp_time)
1082{
1083 struct ieee80211_local *local = sdata->local;
1084 struct sta_info *sta, *tmp;
Johannes Berg24723d12008-09-11 00:01:46 +02001085
Johannes Berg34e89502010-02-03 13:59:58 +01001086 mutex_lock(&local->sta_mtx);
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301087
1088 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Marek Lindnerec2b7742011-12-20 23:16:52 +08001089 if (sdata != sta->sdata)
1090 continue;
1091
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001092 if (time_after(jiffies, sta->rx_stats.last_rx + exp_time)) {
Mohammed Shafi Shajakhaneea57d42012-10-08 21:33:47 +05301093 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1094 sta->sta.addr);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001095
1096 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1097 test_sta_flag(sta, WLAN_STA_PS_STA))
1098 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1099
Johannes Berg34e89502010-02-03 13:59:58 +01001100 WARN_ON(__sta_info_destroy(sta));
Johannes Berg24723d12008-09-11 00:01:46 +02001101 }
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301102 }
1103
Johannes Berg34e89502010-02-03 13:59:58 +01001104 mutex_unlock(&local->sta_mtx);
Johannes Berg24723d12008-09-11 00:01:46 +02001105}
Johannes Berg17741cd2008-09-11 00:02:02 +02001106
Ben Greear686b9cb2010-09-23 09:44:36 -07001107struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001108 const u8 *addr,
1109 const u8 *localaddr)
Johannes Berg17741cd2008-09-11 00:02:02 +02001110{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001111 struct ieee80211_local *local = hw_to_local(hw);
1112 struct sta_info *sta;
1113 struct rhash_head *tmp;
1114 const struct bucket_table *tbl;
1115
1116 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
Johannes Berg17741cd2008-09-11 00:02:02 +02001117
Ben Greear686b9cb2010-09-23 09:44:36 -07001118 /*
1119 * Just return a random station if localaddr is NULL
1120 * ... first in list.
1121 */
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001122 for_each_sta_info(local, tbl, addr, sta, tmp) {
Ben Greear686b9cb2010-09-23 09:44:36 -07001123 if (localaddr &&
Joe Perchesb203ca32012-05-08 18:56:52 +00001124 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
Ben Greear686b9cb2010-09-23 09:44:36 -07001125 continue;
Johannes Bergf7c65592010-04-30 13:48:36 +02001126 if (!sta->uploaded)
1127 return NULL;
Johannes Bergabe60632009-11-25 17:46:18 +01001128 return &sta->sta;
Johannes Bergf7c65592010-04-30 13:48:36 +02001129 }
1130
Johannes Bergabe60632009-11-25 17:46:18 +01001131 return NULL;
Johannes Berg17741cd2008-09-11 00:02:02 +02001132}
Ben Greear686b9cb2010-09-23 09:44:36 -07001133EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
Johannes Berg5ed176e2009-11-04 14:42:28 +01001134
1135struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1136 const u8 *addr)
1137{
Johannes Bergf7c65592010-04-30 13:48:36 +02001138 struct sta_info *sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001139
1140 if (!vif)
1141 return NULL;
1142
Johannes Bergf7c65592010-04-30 13:48:36 +02001143 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1144 if (!sta)
1145 return NULL;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001146
Johannes Bergf7c65592010-04-30 13:48:36 +02001147 if (!sta->uploaded)
1148 return NULL;
1149
1150 return &sta->sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001151}
Johannes Berg17741cd2008-09-11 00:02:02 +02001152EXPORT_SYMBOL(ieee80211_find_sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001153
Johannes Berge3685e02014-02-20 11:19:58 +01001154/* powersave support code */
1155void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
Johannes Berg50a94322010-11-16 11:50:28 -08001156{
Helmut Schaa608383b2012-01-30 15:18:00 +01001157 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berge3685e02014-02-20 11:19:58 +01001158 struct ieee80211_local *local = sdata->local;
1159 struct sk_buff_head pending;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001160 int filtered = 0, buffered = 0, ac, i;
Johannes Berge3685e02014-02-20 11:19:58 +01001161 unsigned long flags;
Marco Porschd012a602012-10-10 12:39:50 -07001162 struct ps_data *ps;
1163
Felix Fietkau3918edb2014-07-25 16:20:23 +02001164 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1165 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1166 u.ap);
1167
1168 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porschd012a602012-10-10 12:39:50 -07001169 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001170 else if (ieee80211_vif_is_mesh(&sdata->vif))
1171 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07001172 else
1173 return;
Johannes Berg50a94322010-11-16 11:50:28 -08001174
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001175 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg47086fc2011-09-29 16:04:33 +02001176
Johannes Berg5a306f52012-11-14 23:22:21 +01001177 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
Johannes Berg948d8872011-09-29 16:04:29 +02001178 sta->driver_buffered_tids = 0;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001179 sta->txq_buffered_tids = 0;
Johannes Berg948d8872011-09-29 16:04:29 +02001180
Johannes Berg30686bf2015-06-02 21:39:54 +02001181 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001182 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001183
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001184 if (sta->sta.txq[0]) {
1185 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1186 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1187
1188 if (!skb_queue_len(&txqi->queue))
1189 continue;
1190
1191 drv_wake_tx_queue(local, txqi);
1192 }
1193 }
1194
Johannes Berg948d8872011-09-29 16:04:29 +02001195 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001196
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001197 /* sync with ieee80211_tx_h_unicast_ps_buf */
1198 spin_lock(&sta->ps_lock);
Johannes Bergaf818582009-11-06 11:35:50 +01001199 /* Send all buffered frames to the station */
Johannes Berg948d8872011-09-29 16:04:29 +02001200 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1201 int count = skb_queue_len(&pending), tmp;
1202
Arik Nemtsov987c2852012-11-05 10:27:52 +02001203 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001204 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001205 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001206 tmp = skb_queue_len(&pending);
1207 filtered += tmp - count;
1208 count = tmp;
1209
Arik Nemtsov987c2852012-11-05 10:27:52 +02001210 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001211 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001212 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001213 tmp = skb_queue_len(&pending);
1214 buffered += tmp - count;
1215 }
1216
Johannes Berge3685e02014-02-20 11:19:58 +01001217 ieee80211_add_pending_skbs(local, &pending);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001218
1219 /* now we're no longer in the deliver code */
1220 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1221
1222 /* The station might have polled and then woken up before we responded,
1223 * so clear these flags now to avoid them sticking around.
1224 */
1225 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1226 clear_sta_flag(sta, WLAN_STA_UAPSD);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001227 spin_unlock(&sta->ps_lock);
Johannes Berg948d8872011-09-29 16:04:29 +02001228
Johannes Berge3685e02014-02-20 11:19:58 +01001229 atomic_dec(&ps->num_sta_ps);
1230
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001231 /* This station just woke up and isn't aware of our SMPS state */
Chun-Yeow Yeoh062f1d62014-04-22 18:19:25 +08001232 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1233 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001234 sdata->smps_mode) &&
1235 sta->known_smps_mode != sdata->bss->req_smps &&
1236 sta_info_tx_streams(sta) != 1) {
1237 ht_dbg(sdata,
1238 "%pM just woke up and MIMO capable - update SMPS\n",
1239 sta->sta.addr);
1240 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1241 sta->sta.addr,
1242 sdata->vif.bss_conf.bssid);
1243 }
1244
Johannes Bergaf818582009-11-06 11:35:50 +01001245 local->total_ps_buffered -= buffered;
1246
Johannes Bergc868cb352011-09-29 16:04:27 +02001247 sta_info_recalc_tim(sta);
1248
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001249 ps_dbg(sdata,
1250 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1251 sta->sta.addr, sta->sta.aid, filtered, buffered);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001252
1253 ieee80211_check_fast_xmit(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001254}
1255
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001256static void ieee80211_send_null_response(struct sta_info *sta, int tid,
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001257 enum ieee80211_frame_release_type reason,
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001258 bool call_driver, bool more_data)
Johannes Bergce662b442011-09-29 16:04:34 +02001259{
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001260 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Bergce662b442011-09-29 16:04:34 +02001261 struct ieee80211_local *local = sdata->local;
1262 struct ieee80211_qos_hdr *nullfunc;
1263 struct sk_buff *skb;
1264 int size = sizeof(*nullfunc);
1265 __le16 fc;
Johannes Berga74a8c82014-07-22 14:50:47 +02001266 bool qos = sta->sta.wme;
Johannes Bergce662b442011-09-29 16:04:34 +02001267 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02001268 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergce662b442011-09-29 16:04:34 +02001269
1270 if (qos) {
1271 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1272 IEEE80211_STYPE_QOS_NULLFUNC |
1273 IEEE80211_FCTL_FROMDS);
1274 } else {
1275 size -= 2;
1276 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1277 IEEE80211_STYPE_NULLFUNC |
1278 IEEE80211_FCTL_FROMDS);
1279 }
1280
1281 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1282 if (!skb)
1283 return;
1284
1285 skb_reserve(skb, local->hw.extra_tx_headroom);
1286
1287 nullfunc = (void *) skb_put(skb, size);
1288 nullfunc->frame_control = fc;
1289 nullfunc->duration_id = 0;
1290 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1291 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1292 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
Johannes Berg864a6042014-03-04 13:46:53 +01001293 nullfunc->seq_ctrl = 0;
Johannes Bergce662b442011-09-29 16:04:34 +02001294
Johannes Berg59b66252011-10-13 13:19:19 +02001295 skb->priority = tid;
1296 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
Johannes Bergce662b442011-09-29 16:04:34 +02001297 if (qos) {
Johannes Bergce662b442011-09-29 16:04:34 +02001298 nullfunc->qos_ctrl = cpu_to_le16(tid);
1299
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001300 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
Johannes Bergce662b442011-09-29 16:04:34 +02001301 nullfunc->qos_ctrl |=
1302 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001303 if (more_data)
1304 nullfunc->frame_control |=
1305 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1306 }
Johannes Bergce662b442011-09-29 16:04:34 +02001307 }
1308
1309 info = IEEE80211_SKB_CB(skb);
1310
1311 /*
1312 * Tell TX path to send this frame even though the
1313 * STA may still remain is PS mode after this frame
Johannes Bergdeeaee192011-09-29 16:04:35 +02001314 * exchange. Also set EOSP to indicate this packet
1315 * ends the poll/service period.
Johannes Bergce662b442011-09-29 16:04:34 +02001316 */
Johannes Berg02f2f1a2012-02-27 12:18:30 +01001317 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
Johannes Bergdeeaee192011-09-29 16:04:35 +02001318 IEEE80211_TX_STATUS_EOSP |
1319 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergce662b442011-09-29 16:04:34 +02001320
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301321 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1322
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001323 if (call_driver)
1324 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1325 reason, false);
Johannes Berg40b96402011-09-29 16:04:38 +02001326
Johannes Berg89afe612013-02-13 15:39:57 +01001327 skb->dev = sdata->dev;
1328
Johannes Berg55de9082012-07-26 17:24:39 +02001329 rcu_read_lock();
1330 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1331 if (WARN_ON(!chanctx_conf)) {
1332 rcu_read_unlock();
1333 kfree_skb(skb);
1334 return;
1335 }
1336
Johannes Berg73c4e192014-11-09 18:50:09 +02001337 info->band = chanctx_conf->def.chan->band;
Johannes Berg7c107702015-03-20 14:18:27 +01001338 ieee80211_xmit(sdata, sta, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001339 rcu_read_unlock();
Johannes Bergce662b442011-09-29 16:04:34 +02001340}
1341
Johannes Berg0a1cb802014-01-09 11:05:31 +01001342static int find_highest_prio_tid(unsigned long tids)
1343{
1344 /* lower 3 TIDs aren't ordered perfectly */
1345 if (tids & 0xF8)
1346 return fls(tids) - 1;
1347 /* TID 0 is BE just like TID 3 */
1348 if (tids & BIT(0))
1349 return 0;
1350 return fls(tids) - 1;
1351}
1352
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001353/* Indicates if the MORE_DATA bit should be set in the last
1354 * frame obtained by ieee80211_sta_ps_get_frames.
1355 * Note that driver_release_tids is relevant only if
1356 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1357 */
1358static bool
1359ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1360 enum ieee80211_frame_release_type reason,
1361 unsigned long driver_release_tids)
1362{
1363 int ac;
1364
1365 /* If the driver has data on more than one TID then
1366 * certainly there's more data if we release just a
1367 * single frame now (from a single TID). This will
1368 * only happen for PS-Poll.
1369 */
1370 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1371 hweight16(driver_release_tids) > 1)
1372 return true;
1373
1374 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1375 if (ignored_acs & BIT(ac))
1376 continue;
1377
1378 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1379 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1380 return true;
1381 }
1382
1383 return false;
1384}
1385
Johannes Berg47086fc2011-09-29 16:04:33 +02001386static void
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001387ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1388 enum ieee80211_frame_release_type reason,
1389 struct sk_buff_head *frames,
1390 unsigned long *driver_release_tids)
Johannes Bergaf818582009-11-06 11:35:50 +01001391{
1392 struct ieee80211_sub_if_data *sdata = sta->sdata;
1393 struct ieee80211_local *local = sdata->local;
Johannes Berg948d8872011-09-29 16:04:29 +02001394 int ac;
Johannes Bergaf818582009-11-06 11:35:50 +01001395
Johannes Bergf9f760b2014-01-08 17:45:07 +01001396 /* Get response frame(s) and more data bit for the last one. */
Johannes Berg948d8872011-09-29 16:04:29 +02001397 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg4049e092011-09-29 16:04:32 +02001398 unsigned long tids;
1399
Johannes Berg47086fc2011-09-29 16:04:33 +02001400 if (ignored_acs & BIT(ac))
Johannes Berg948d8872011-09-29 16:04:29 +02001401 continue;
1402
Johannes Berg4049e092011-09-29 16:04:32 +02001403 tids = ieee80211_tids_for_ac(ac);
1404
Johannes Bergf9f760b2014-01-08 17:45:07 +01001405 /* if we already have frames from software, then we can't also
1406 * release from hardware queues
1407 */
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001408 if (skb_queue_empty(frames)) {
1409 *driver_release_tids |=
1410 sta->driver_buffered_tids & tids;
1411 *driver_release_tids |= sta->txq_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001412 }
Johannes Berg47086fc2011-09-29 16:04:33 +02001413
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001414 if (!*driver_release_tids) {
Johannes Bergf9f760b2014-01-08 17:45:07 +01001415 struct sk_buff *skb;
1416
1417 while (n_frames > 0) {
1418 skb = skb_dequeue(&sta->tx_filtered[ac]);
1419 if (!skb) {
1420 skb = skb_dequeue(
1421 &sta->ps_tx_buf[ac]);
1422 if (skb)
1423 local->total_ps_buffered--;
1424 }
1425 if (!skb)
1426 break;
1427 n_frames--;
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001428 __skb_queue_tail(frames, skb);
Johannes Bergf9f760b2014-01-08 17:45:07 +01001429 }
Johannes Berg948d8872011-09-29 16:04:29 +02001430 }
1431
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001432 /* If we have more frames buffered on this AC, then abort the
1433 * loop since we can't send more data from other ACs before
1434 * the buffered frames from this.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001435 */
Johannes Berg948d8872011-09-29 16:04:29 +02001436 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001437 !skb_queue_empty(&sta->ps_tx_buf[ac]))
Johannes Berg948d8872011-09-29 16:04:29 +02001438 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001439 }
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001440}
1441
1442static void
1443ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1444 int n_frames, u8 ignored_acs,
1445 enum ieee80211_frame_release_type reason)
1446{
1447 struct ieee80211_sub_if_data *sdata = sta->sdata;
1448 struct ieee80211_local *local = sdata->local;
1449 unsigned long driver_release_tids = 0;
1450 struct sk_buff_head frames;
1451 bool more_data;
1452
1453 /* Service or PS-Poll period starts */
1454 set_sta_flag(sta, WLAN_STA_SP);
1455
1456 __skb_queue_head_init(&frames);
1457
1458 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1459 &frames, &driver_release_tids);
1460
1461 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1462
1463 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1464 driver_release_tids =
1465 BIT(find_highest_prio_tid(driver_release_tids));
Johannes Bergaf818582009-11-06 11:35:50 +01001466
Johannes Bergf9f760b2014-01-08 17:45:07 +01001467 if (skb_queue_empty(&frames) && !driver_release_tids) {
Johannes Bergce662b442011-09-29 16:04:34 +02001468 int tid;
Johannes Berg4049e092011-09-29 16:04:32 +02001469
Johannes Bergce662b442011-09-29 16:04:34 +02001470 /*
1471 * For PS-Poll, this can only happen due to a race condition
1472 * when we set the TIM bit and the station notices it, but
1473 * before it can poll for the frame we expire it.
1474 *
1475 * For uAPSD, this is said in the standard (11.2.1.5 h):
1476 * At each unscheduled SP for a non-AP STA, the AP shall
1477 * attempt to transmit at least one MSDU or MMPDU, but no
1478 * more than the value specified in the Max SP Length field
1479 * in the QoS Capability element from delivery-enabled ACs,
1480 * that are destined for the non-AP STA.
1481 *
1482 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1483 */
1484
1485 /* This will evaluate to 1, 3, 5 or 7. */
1486 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1487
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001488 ieee80211_send_null_response(sta, tid, reason, true, false);
Johannes Bergf9f760b2014-01-08 17:45:07 +01001489 } else if (!driver_release_tids) {
Johannes Berg47086fc2011-09-29 16:04:33 +02001490 struct sk_buff_head pending;
1491 struct sk_buff *skb;
Johannes Berg40b96402011-09-29 16:04:38 +02001492 int num = 0;
1493 u16 tids = 0;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001494 bool need_null = false;
Johannes Bergaf818582009-11-06 11:35:50 +01001495
Johannes Berg47086fc2011-09-29 16:04:33 +02001496 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001497
Johannes Berg47086fc2011-09-29 16:04:33 +02001498 while ((skb = __skb_dequeue(&frames))) {
1499 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1500 struct ieee80211_hdr *hdr = (void *) skb->data;
Johannes Berg40b96402011-09-29 16:04:38 +02001501 u8 *qoshdr = NULL;
1502
1503 num++;
Johannes Bergaf818582009-11-06 11:35:50 +01001504
Johannes Berg47086fc2011-09-29 16:04:33 +02001505 /*
1506 * Tell TX path to send this frame even though the
1507 * STA may still remain is PS mode after this frame
1508 * exchange.
1509 */
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301510 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1511 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
Johannes Bergaf818582009-11-06 11:35:50 +01001512
Johannes Berg47086fc2011-09-29 16:04:33 +02001513 /*
1514 * Use MoreData flag to indicate whether there are
1515 * more buffered frames for this STA
1516 */
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001517 if (more_data || !skb_queue_empty(&frames))
Johannes Berg47086fc2011-09-29 16:04:33 +02001518 hdr->frame_control |=
1519 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001520 else
1521 hdr->frame_control &=
1522 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
Johannes Berg47086fc2011-09-29 16:04:33 +02001523
Johannes Berg40b96402011-09-29 16:04:38 +02001524 if (ieee80211_is_data_qos(hdr->frame_control) ||
1525 ieee80211_is_qos_nullfunc(hdr->frame_control))
1526 qoshdr = ieee80211_get_qos_ctl(hdr);
1527
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001528 tids |= BIT(skb->priority);
1529
1530 __skb_queue_tail(&pending, skb);
1531
1532 /* end service period after last frame or add one */
1533 if (!skb_queue_empty(&frames))
1534 continue;
1535
1536 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1537 /* for PS-Poll, there's only one frame */
1538 info->flags |= IEEE80211_TX_STATUS_EOSP |
1539 IEEE80211_TX_CTL_REQ_TX_STATUS;
1540 break;
1541 }
1542
1543 /* For uAPSD, things are a bit more complicated. If the
1544 * last frame has a QoS header (i.e. is a QoS-data or
1545 * QoS-nulldata frame) then just set the EOSP bit there
1546 * and be done.
1547 * If the frame doesn't have a QoS header (which means
1548 * it should be a bufferable MMPDU) then we can't set
1549 * the EOSP bit in the QoS header; add a QoS-nulldata
1550 * frame to the list to send it after the MMPDU.
1551 *
1552 * Note that this code is only in the mac80211-release
1553 * code path, we assume that the driver will not buffer
1554 * anything but QoS-data frames, or if it does, will
1555 * create the QoS-nulldata frame by itself if needed.
1556 *
1557 * Cf. 802.11-2012 10.2.1.10 (c).
1558 */
1559 if (qoshdr) {
1560 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
Johannes Berg47086fc2011-09-29 16:04:33 +02001561
Marco Porsch52a3f202012-03-16 15:30:26 +01001562 info->flags |= IEEE80211_TX_STATUS_EOSP |
1563 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001564 } else {
1565 /* The standard isn't completely clear on this
1566 * as it says the more-data bit should be set
1567 * if there are more BUs. The QoS-Null frame
1568 * we're about to send isn't buffered yet, we
1569 * only create it below, but let's pretend it
1570 * was buffered just in case some clients only
1571 * expect more-data=0 when eosp=1.
1572 */
1573 hdr->frame_control |=
1574 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1575 need_null = true;
1576 num++;
Marco Porsch52a3f202012-03-16 15:30:26 +01001577 }
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001578 break;
Johannes Berg47086fc2011-09-29 16:04:33 +02001579 }
1580
Johannes Berg40b96402011-09-29 16:04:38 +02001581 drv_allow_buffered_frames(local, sta, tids, num,
1582 reason, more_data);
1583
Johannes Berg47086fc2011-09-29 16:04:33 +02001584 ieee80211_add_pending_skbs(local, &pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001585
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001586 if (need_null)
1587 ieee80211_send_null_response(
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001588 sta, find_highest_prio_tid(tids),
1589 reason, false, false);
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001590
Johannes Bergc868cb352011-09-29 16:04:27 +02001591 sta_info_recalc_tim(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001592 } else {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001593 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1594 int tid;
1595
Johannes Bergaf818582009-11-06 11:35:50 +01001596 /*
Johannes Berg4049e092011-09-29 16:04:32 +02001597 * We need to release a frame that is buffered somewhere in the
1598 * driver ... it'll have to handle that.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001599 * Note that the driver also has to check the number of frames
1600 * on the TIDs we're releasing from - if there are more than
1601 * n_frames it has to set the more-data bit (if we didn't ask
1602 * it to set it anyway due to other buffered frames); if there
1603 * are fewer than n_frames it has to make sure to adjust that
1604 * to allow the service period to end properly.
Johannes Bergaf818582009-11-06 11:35:50 +01001605 */
Johannes Berg4049e092011-09-29 16:04:32 +02001606 drv_release_buffered_frames(local, sta, driver_release_tids,
Johannes Berg47086fc2011-09-29 16:04:33 +02001607 n_frames, reason, more_data);
Johannes Berg4049e092011-09-29 16:04:32 +02001608
1609 /*
1610 * Note that we don't recalculate the TIM bit here as it would
1611 * most likely have no effect at all unless the driver told us
Johannes Bergf9f760b2014-01-08 17:45:07 +01001612 * that the TID(s) became empty before returning here from the
Johannes Berg4049e092011-09-29 16:04:32 +02001613 * release function.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001614 * Either way, however, when the driver tells us that the TID(s)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001615 * became empty or we find that a txq became empty, we'll do the
1616 * TIM recalculation.
Johannes Berg4049e092011-09-29 16:04:32 +02001617 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001618
1619 if (!sta->sta.txq[0])
1620 return;
1621
1622 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1623 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1624
1625 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1626 continue;
1627
1628 sta_info_recalc_tim(sta);
1629 break;
1630 }
Johannes Bergaf818582009-11-06 11:35:50 +01001631 }
1632}
1633
Johannes Bergaf818582009-11-06 11:35:50 +01001634void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1635{
Johannes Berg47086fc2011-09-29 16:04:33 +02001636 u8 ignore_for_response = sta->sta.uapsd_queues;
Johannes Bergaf818582009-11-06 11:35:50 +01001637
Johannes Berg47086fc2011-09-29 16:04:33 +02001638 /*
1639 * If all ACs are delivery-enabled then we should reply
1640 * from any of them, if only some are enabled we reply
1641 * only from the non-enabled ones.
1642 */
1643 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1644 ignore_for_response = 0;
1645
1646 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1647 IEEE80211_FRAME_RELEASE_PSPOLL);
1648}
1649
1650void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1651{
1652 int n_frames = sta->sta.max_sp;
1653 u8 delivery_enabled = sta->sta.uapsd_queues;
1654
1655 /*
1656 * If we ever grow support for TSPEC this might happen if
1657 * the TSPEC update from hostapd comes in between a trigger
1658 * frame setting WLAN_STA_UAPSD in the RX path and this
1659 * actually getting called.
1660 */
1661 if (!delivery_enabled)
1662 return;
1663
Johannes Berg47086fc2011-09-29 16:04:33 +02001664 switch (sta->sta.max_sp) {
1665 case 1:
1666 n_frames = 2;
1667 break;
1668 case 2:
1669 n_frames = 4;
1670 break;
1671 case 3:
1672 n_frames = 6;
1673 break;
1674 case 0:
1675 /* XXX: what is a good value? */
Andrei Otcheretianski13a80982014-11-04 11:33:04 +02001676 n_frames = 128;
Johannes Berg47086fc2011-09-29 16:04:33 +02001677 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001678 }
Johannes Bergaf818582009-11-06 11:35:50 +01001679
Johannes Berg47086fc2011-09-29 16:04:33 +02001680 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1681 IEEE80211_FRAME_RELEASE_UAPSD);
Johannes Bergaf818582009-11-06 11:35:50 +01001682}
1683
1684void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1685 struct ieee80211_sta *pubsta, bool block)
1686{
1687 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1688
Johannes Bergb5878a22010-04-07 16:48:40 +02001689 trace_api_sta_block_awake(sta->local, pubsta, block);
1690
Johannes Berg5ac2e352014-05-27 16:32:27 +02001691 if (block) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001692 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001693 ieee80211_clear_fast_xmit(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001694 return;
1695 }
1696
1697 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1698 return;
1699
1700 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1701 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1702 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1703 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1704 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1705 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1706 /* must be asleep in this case */
1707 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1708 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1709 } else {
1710 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001711 ieee80211_check_fast_xmit(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001712 }
Johannes Bergaf818582009-11-06 11:35:50 +01001713}
1714EXPORT_SYMBOL(ieee80211_sta_block_awake);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001715
Johannes Berge9437892013-02-15 21:38:08 +01001716void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
Johannes Berg37fbd902011-09-29 16:04:39 +02001717{
1718 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1719 struct ieee80211_local *local = sta->local;
Johannes Berg37fbd902011-09-29 16:04:39 +02001720
1721 trace_api_eosp(local, pubsta);
1722
Johannes Berge9437892013-02-15 21:38:08 +01001723 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg37fbd902011-09-29 16:04:39 +02001724}
Johannes Berge9437892013-02-15 21:38:08 +01001725EXPORT_SYMBOL(ieee80211_sta_eosp);
Johannes Berg37fbd902011-09-29 16:04:39 +02001726
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001727void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1728{
1729 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1730 enum ieee80211_frame_release_type reason;
1731 bool more_data;
1732
1733 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1734
1735 reason = IEEE80211_FRAME_RELEASE_UAPSD;
1736 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1737 reason, 0);
1738
1739 ieee80211_send_null_response(sta, tid, reason, false, more_data);
1740}
1741EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1742
Johannes Berg042ec452011-09-29 16:04:26 +02001743void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1744 u8 tid, bool buffered)
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001745{
1746 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1747
Johannes Berg5a306f52012-11-14 23:22:21 +01001748 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
Johannes Berg042ec452011-09-29 16:04:26 +02001749 return;
1750
Johannes Berg1b000782013-12-19 10:47:48 +01001751 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1752
Johannes Berg948d8872011-09-29 16:04:29 +02001753 if (buffered)
1754 set_bit(tid, &sta->driver_buffered_tids);
1755 else
1756 clear_bit(tid, &sta->driver_buffered_tids);
1757
Johannes Bergc868cb352011-09-29 16:04:27 +02001758 sta_info_recalc_tim(sta);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001759}
Johannes Berg042ec452011-09-29 16:04:26 +02001760EXPORT_SYMBOL(ieee80211_sta_set_buffered);
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001761
Johannes Berg83d5cc02012-01-12 09:31:10 +01001762int sta_info_move_state(struct sta_info *sta,
1763 enum ieee80211_sta_state new_state)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001764{
Johannes Berg8bf11d82011-12-15 11:17:37 +01001765 might_sleep();
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001766
1767 if (sta->sta_state == new_state)
1768 return 0;
1769
Johannes Bergf09603a2012-01-20 13:55:21 +01001770 /* check allowed transitions first */
1771
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001772 switch (new_state) {
1773 case IEEE80211_STA_NONE:
Johannes Bergf09603a2012-01-20 13:55:21 +01001774 if (sta->sta_state != IEEE80211_STA_AUTH)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001775 return -EINVAL;
1776 break;
1777 case IEEE80211_STA_AUTH:
Johannes Bergf09603a2012-01-20 13:55:21 +01001778 if (sta->sta_state != IEEE80211_STA_NONE &&
1779 sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001780 return -EINVAL;
1781 break;
1782 case IEEE80211_STA_ASSOC:
Johannes Bergf09603a2012-01-20 13:55:21 +01001783 if (sta->sta_state != IEEE80211_STA_AUTH &&
1784 sta->sta_state != IEEE80211_STA_AUTHORIZED)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001785 return -EINVAL;
1786 break;
1787 case IEEE80211_STA_AUTHORIZED:
Johannes Bergf09603a2012-01-20 13:55:21 +01001788 if (sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001789 return -EINVAL;
1790 break;
1791 default:
1792 WARN(1, "invalid state %d", new_state);
1793 return -EINVAL;
1794 }
1795
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001796 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1797 sta->sta.addr, new_state);
Johannes Bergf09603a2012-01-20 13:55:21 +01001798
1799 /*
1800 * notify the driver before the actual changes so it can
1801 * fail the transition
1802 */
1803 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1804 int err = drv_sta_state(sta->local, sta->sdata, sta,
1805 sta->sta_state, new_state);
1806 if (err)
1807 return err;
1808 }
1809
1810 /* reflect the change in all state variables */
1811
1812 switch (new_state) {
1813 case IEEE80211_STA_NONE:
1814 if (sta->sta_state == IEEE80211_STA_AUTH)
1815 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1816 break;
1817 case IEEE80211_STA_AUTH:
Ilan Peera7201a62015-12-13 13:41:43 +02001818 if (sta->sta_state == IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +01001819 set_bit(WLAN_STA_AUTH, &sta->_flags);
Ilan Peera7201a62015-12-13 13:41:43 +02001820 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
Johannes Bergf09603a2012-01-20 13:55:21 +01001821 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
Ilan Peera7201a62015-12-13 13:41:43 +02001822 ieee80211_recalc_min_chandef(sta->sdata);
1823 }
Johannes Bergf09603a2012-01-20 13:55:21 +01001824 break;
1825 case IEEE80211_STA_ASSOC:
1826 if (sta->sta_state == IEEE80211_STA_AUTH) {
1827 set_bit(WLAN_STA_ASSOC, &sta->_flags);
Ilan Peera7201a62015-12-13 13:41:43 +02001828 ieee80211_recalc_min_chandef(sta->sdata);
Johannes Bergf09603a2012-01-20 13:55:21 +01001829 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001830 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1831 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1832 !sta->sdata->u.vlan.sta))
1833 atomic_dec(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001834 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001835 ieee80211_clear_fast_xmit(sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001836 }
1837 break;
1838 case IEEE80211_STA_AUTHORIZED:
1839 if (sta->sta_state == IEEE80211_STA_ASSOC) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001840 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1841 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1842 !sta->sdata->u.vlan.sta))
1843 atomic_inc(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001844 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001845 ieee80211_check_fast_xmit(sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001846 }
1847 break;
1848 default:
1849 break;
1850 }
1851
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001852 sta->sta_state = new_state;
1853
1854 return 0;
1855}
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001856
1857u8 sta_info_tx_streams(struct sta_info *sta)
1858{
1859 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1860 u8 rx_streams;
1861
1862 if (!sta->sta.ht_cap.ht_supported)
1863 return 1;
1864
1865 if (sta->sta.vht_cap.vht_supported) {
1866 int i;
1867 u16 tx_mcs_map =
1868 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1869
1870 for (i = 7; i >= 0; i--)
1871 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1872 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1873 return i + 1;
1874 }
1875
1876 if (ht_cap->mcs.rx_mask[3])
1877 rx_streams = 4;
1878 else if (ht_cap->mcs.rx_mask[2])
1879 rx_streams = 3;
1880 else if (ht_cap->mcs.rx_mask[1])
1881 rx_streams = 2;
1882 else
1883 rx_streams = 1;
1884
1885 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1886 return rx_streams;
1887
1888 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1889 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1890}
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001891
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001892static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
1893{
1894 rinfo->flags = 0;
1895
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001896 if (sta->rx_stats.last_rate_flag & RX_FLAG_HT) {
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001897 rinfo->flags |= RATE_INFO_FLAGS_MCS;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001898 rinfo->mcs = sta->rx_stats.last_rate_idx;
1899 } else if (sta->rx_stats.last_rate_flag & RX_FLAG_VHT) {
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001900 rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001901 rinfo->nss = sta->rx_stats.last_rate_vht_nss;
1902 rinfo->mcs = sta->rx_stats.last_rate_idx;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001903 } else {
1904 struct ieee80211_supported_band *sband;
1905 int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
1906 u16 brate;
1907
1908 sband = sta->local->hw.wiphy->bands[
1909 ieee80211_get_sdata_band(sta->sdata)];
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001910 brate = sband->bitrates[sta->rx_stats.last_rate_idx].bitrate;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001911 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
1912 }
1913
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001914 if (sta->rx_stats.last_rate_flag & RX_FLAG_SHORT_GI)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001915 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
1916
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001917 if (sta->rx_stats.last_rate_flag & RX_FLAG_5MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001918 rinfo->bw = RATE_INFO_BW_5;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001919 else if (sta->rx_stats.last_rate_flag & RX_FLAG_10MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001920 rinfo->bw = RATE_INFO_BW_10;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001921 else if (sta->rx_stats.last_rate_flag & RX_FLAG_40MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001922 rinfo->bw = RATE_INFO_BW_40;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001923 else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_80MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001924 rinfo->bw = RATE_INFO_BW_80;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001925 else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_160MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001926 rinfo->bw = RATE_INFO_BW_160;
1927 else
1928 rinfo->bw = RATE_INFO_BW_20;
1929}
1930
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001931void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1932{
1933 struct ieee80211_sub_if_data *sdata = sta->sdata;
1934 struct ieee80211_local *local = sdata->local;
John W. Linville9a244402014-07-25 10:22:36 -04001935 struct rate_control_ref *ref = NULL;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001936 u32 thr = 0;
1937 int i, ac;
1938
John W. Linville9a244402014-07-25 10:22:36 -04001939 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1940 ref = local->rate_ctrl;
1941
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001942 sinfo->generation = sdata->local->sta_generation;
1943
Johannes Berg225b8182015-01-21 21:09:02 +01001944 /* do before driver, so beacon filtering drivers have a
1945 * chance to e.g. just add the number of filtered beacons
1946 * (or just modify the value entirely, of course)
1947 */
1948 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1949 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1950
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001951 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1952
Johannes Berg319090b2014-11-17 14:08:11 +01001953 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1954 BIT(NL80211_STA_INFO_STA_FLAGS) |
1955 BIT(NL80211_STA_INFO_BSS_PARAM) |
1956 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
Johannes Berg976bd9e2015-10-16 17:18:11 +02001957 BIT(NL80211_STA_INFO_RX_DROP_MISC);
1958
1959 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1960 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
1961 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
1962 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001963
Arnd Bergmann84b00602015-09-30 13:26:36 +02001964 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001965 sinfo->inactive_time =
1966 jiffies_to_msecs(jiffies - sta->rx_stats.last_rx);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001967
Johannes Berg319090b2014-11-17 14:08:11 +01001968 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1969 BIT(NL80211_STA_INFO_TX_BYTES)))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001970 sinfo->tx_bytes = 0;
1971 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001972 sinfo->tx_bytes += sta->tx_stats.bytes[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01001973 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001974 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001975
Johannes Berg319090b2014-11-17 14:08:11 +01001976 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001977 sinfo->tx_packets = 0;
1978 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001979 sinfo->tx_packets += sta->tx_stats.packets[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01001980 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001981 }
1982
Johannes Berg319090b2014-11-17 14:08:11 +01001983 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1984 BIT(NL80211_STA_INFO_RX_BYTES)))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001985 sinfo->rx_bytes = sta->rx_stats.bytes;
Johannes Berg319090b2014-11-17 14:08:11 +01001986 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001987 }
1988
Johannes Berg319090b2014-11-17 14:08:11 +01001989 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001990 sinfo->rx_packets = sta->rx_stats.packets;
Johannes Berg319090b2014-11-17 14:08:11 +01001991 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001992 }
1993
Johannes Berg319090b2014-11-17 14:08:11 +01001994 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001995 sinfo->tx_retries = sta->status_stats.retry_count;
Johannes Berg319090b2014-11-17 14:08:11 +01001996 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001997 }
1998
Johannes Berg319090b2014-11-17 14:08:11 +01001999 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002000 sinfo->tx_failed = sta->status_stats.retry_failed;
Johannes Berg319090b2014-11-17 14:08:11 +01002001 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002002 }
2003
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002004 sinfo->rx_dropped_misc = sta->rx_stats.dropped;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002005
Johannes Berg225b8182015-01-21 21:09:02 +01002006 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2007 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2008 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
2009 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2010 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2011 }
2012
Johannes Berg30686bf2015-06-02 21:39:54 +02002013 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2014 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
Johannes Berg319090b2014-11-17 14:08:11 +01002015 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002016 sinfo->signal = (s8)sta->rx_stats.last_signal;
Johannes Berg319090b2014-11-17 14:08:11 +01002017 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002018 }
2019
Johannes Berg319090b2014-11-17 14:08:11 +01002020 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
Johannes Berg40d9a382015-07-13 12:26:46 +02002021 sinfo->signal_avg =
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002022 -ewma_signal_read(&sta->rx_stats.avg_signal);
Johannes Berg319090b2014-11-17 14:08:11 +01002023 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002024 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002025 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002026
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002027 if (sta->rx_stats.chains &&
Johannes Berg319090b2014-11-17 14:08:11 +01002028 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2029 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2030 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2031 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002032
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002033 sinfo->chains = sta->rx_stats.chains;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002034 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002035 sinfo->chain_signal[i] =
2036 sta->rx_stats.chain_signal_last[i];
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002037 sinfo->chain_signal_avg[i] =
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002038 -ewma_signal_read(&sta->rx_stats.chain_signal_avg[i]);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002039 }
2040 }
2041
Johannes Berg319090b2014-11-17 14:08:11 +01002042 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002043 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2044 &sinfo->txrate);
Johannes Berg319090b2014-11-17 14:08:11 +01002045 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002046 }
2047
Johannes Berg319090b2014-11-17 14:08:11 +01002048 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002049 sta_set_rate_info_rx(sta, &sinfo->rxrate);
Johannes Berg319090b2014-11-17 14:08:11 +01002050 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002051 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002052
Johannes Berg79c892b2014-11-21 14:26:31 +01002053 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
2054 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
2055 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
2056
2057 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2058 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002059 tidstats->rx_msdu = sta->rx_stats.msdu[i];
Johannes Berg79c892b2014-11-21 14:26:31 +01002060 }
2061
2062 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2063 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002064 tidstats->tx_msdu = sta->tx_stats.msdu[i];
Johannes Berg79c892b2014-11-21 14:26:31 +01002065 }
2066
2067 if (!(tidstats->filled &
2068 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02002069 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Berg79c892b2014-11-21 14:26:31 +01002070 tidstats->filled |=
2071 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002072 tidstats->tx_msdu_retries =
2073 sta->status_stats.msdu_retries[i];
Johannes Berg79c892b2014-11-21 14:26:31 +01002074 }
2075
2076 if (!(tidstats->filled &
2077 BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02002078 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Berg79c892b2014-11-21 14:26:31 +01002079 tidstats->filled |=
2080 BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002081 tidstats->tx_msdu_failed =
2082 sta->status_stats.msdu_failed[i];
Johannes Berg79c892b2014-11-21 14:26:31 +01002083 }
2084 }
2085
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002086 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2087#ifdef CONFIG_MAC80211_MESH
Johannes Berg319090b2014-11-17 14:08:11 +01002088 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2089 BIT(NL80211_STA_INFO_PLID) |
2090 BIT(NL80211_STA_INFO_PLINK_STATE) |
2091 BIT(NL80211_STA_INFO_LOCAL_PM) |
2092 BIT(NL80211_STA_INFO_PEER_PM) |
2093 BIT(NL80211_STA_INFO_NONPEER_PM);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002094
Johannes Berg433f5bc2015-06-17 10:31:00 +02002095 sinfo->llid = sta->mesh->llid;
2096 sinfo->plid = sta->mesh->plid;
2097 sinfo->plink_state = sta->mesh->plink_state;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002098 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
Johannes Berg319090b2014-11-17 14:08:11 +01002099 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
Johannes Berg433f5bc2015-06-17 10:31:00 +02002100 sinfo->t_offset = sta->mesh->t_offset;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002101 }
Johannes Berg433f5bc2015-06-17 10:31:00 +02002102 sinfo->local_pm = sta->mesh->local_pm;
2103 sinfo->peer_pm = sta->mesh->peer_pm;
2104 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002105#endif
2106 }
2107
2108 sinfo->bss_param.flags = 0;
2109 if (sdata->vif.bss_conf.use_cts_prot)
2110 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2111 if (sdata->vif.bss_conf.use_short_preamble)
2112 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2113 if (sdata->vif.bss_conf.use_short_slot)
2114 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
Emmanuel Grumbach785e21a2014-09-03 15:25:04 +03002115 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002116 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2117
2118 sinfo->sta_flags.set = 0;
2119 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2120 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2121 BIT(NL80211_STA_FLAG_WME) |
2122 BIT(NL80211_STA_FLAG_MFP) |
2123 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2124 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2125 BIT(NL80211_STA_FLAG_TDLS_PEER);
2126 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2127 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2128 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2129 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
Johannes Berga74a8c82014-07-22 14:50:47 +02002130 if (sta->sta.wme)
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002131 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2132 if (test_sta_flag(sta, WLAN_STA_MFP))
2133 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2134 if (test_sta_flag(sta, WLAN_STA_AUTH))
2135 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2136 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2137 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2138 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2139 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2140
2141 /* check if the driver has a SW RC implementation */
2142 if (ref && ref->ops->get_expected_throughput)
2143 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2144 else
2145 thr = drv_get_expected_throughput(local, &sta->sta);
2146
2147 if (thr != 0) {
Johannes Berg319090b2014-11-17 14:08:11 +01002148 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002149 sinfo->expected_throughput = thr;
2150 }
2151}