blob: ac73b9c7e8d845204daadc8b4c2d8b3d134a9911 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
Johannes Bergd98ad832014-09-03 15:24:57 +03004 * Copyright 2013-2014 Intel Mobile Communications GmbH
Sara Sharonf59374e2016-03-02 23:46:14 +02005 * Copyright (C) 2015 - 2016 Intel Deutschland GmbH
Jiri Bencf0706e82007-05-05 11:45:53 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
Felix Fietkau888d04d2012-03-01 15:22:09 +010014#include <linux/etherdevice.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070015#include <linux/netdevice.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/if_arp.h>
Johannes Berg0d174402007-12-17 15:07:43 +010020#include <linux/timer.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010021#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070022
23#include <net/mac80211.h>
24#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020025#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040026#include "rate.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070027#include "sta_info.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070028#include "debugfs_sta.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010029#include "mesh.h"
Johannes Bergce662b442011-09-29 16:04:34 +020030#include "wme.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070031
Johannes Bergd0709a62008-02-25 16:27:46 +010032/**
33 * DOC: STA information lifetime rules
34 *
35 * STA info structures (&struct sta_info) are managed in a hash table
36 * for faster lookup and a list for iteration. They are managed using
37 * RCU, i.e. access to the list and hash table is protected by RCU.
38 *
Johannes Berg34e89502010-02-03 13:59:58 +010039 * Upon allocating a STA info structure with sta_info_alloc(), the caller
40 * owns that structure. It must then insert it into the hash table using
41 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
42 * case (which acquires an rcu read section but must not be called from
43 * within one) will the pointer still be valid after the call. Note that
44 * the caller may not do much with the STA info before inserting it, in
45 * particular, it may not start any mesh peer link management or add
46 * encryption keys.
Johannes Berg93e5deb2008-04-01 15:21:00 +020047 *
48 * When the insertion fails (sta_info_insert()) returns non-zero), the
49 * structure will have been freed by sta_info_insert()!
Johannes Bergd0709a62008-02-25 16:27:46 +010050 *
Johannes Berg34e89502010-02-03 13:59:58 +010051 * Station entries are added by mac80211 when you establish a link with a
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040052 * peer. This means different things for the different type of interfaces
53 * we support. For a regular station this mean we add the AP sta when we
Lucas De Marchi25985ed2011-03-30 22:57:33 -030054 * receive an association response from the AP. For IBSS this occurs when
Johannes Berg34e89502010-02-03 13:59:58 +010055 * get to know about a peer on the same IBSS. For WDS we add the sta for
Lucas De Marchi25985ed2011-03-30 22:57:33 -030056 * the peer immediately upon device open. When using AP mode we add stations
Johannes Berg34e89502010-02-03 13:59:58 +010057 * for each respective station upon request from userspace through nl80211.
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040058 *
Johannes Berg34e89502010-02-03 13:59:58 +010059 * In order to remove a STA info structure, various sta_info_destroy_*()
60 * calls are available.
Johannes Bergd0709a62008-02-25 16:27:46 +010061 *
Johannes Berg34e89502010-02-03 13:59:58 +010062 * There is no concept of ownership on a STA entry, each structure is
63 * owned by the global hash table/list until it is removed. All users of
64 * the structure need to be RCU protected so that the structure won't be
65 * freed before they are done using it.
Johannes Bergd0709a62008-02-25 16:27:46 +010066 */
Jiri Bencf0706e82007-05-05 11:45:53 -070067
Johannes Berg7bedd0c2015-02-13 21:55:15 +010068static const struct rhashtable_params sta_rht_params = {
69 .nelem_hint = 3, /* start small */
Johannes Bergcaf22d32015-04-24 11:10:10 +020070 .automatic_shrinking = true,
Johannes Berg7bedd0c2015-02-13 21:55:15 +010071 .head_offset = offsetof(struct sta_info, hash_node),
Johannes Bergac100ce2015-06-16 16:22:12 +020072 .key_offset = offsetof(struct sta_info, addr),
Johannes Berg7bedd0c2015-02-13 21:55:15 +010073 .key_len = ETH_ALEN,
74 .hashfn = sta_addr_hash,
Johannes Bergebd82b32015-04-23 17:26:06 +020075 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
Johannes Berg7bedd0c2015-02-13 21:55:15 +010076};
77
Johannes Berg4d339602011-12-15 11:24:20 +010078/* Caller must hold local->sta_mtx */
Michael Wube8755e2007-07-27 15:43:23 +020079static int sta_info_hash_del(struct ieee80211_local *local,
80 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070081{
Johannes Berg7bedd0c2015-02-13 21:55:15 +010082 return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
83 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -070084}
85
Johannes Berg5108ca82014-02-17 20:49:03 +010086static void __cleanup_single_sta(struct sta_info *sta)
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030087{
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030088 int ac, i;
89 struct tid_ampdu_tx *tid_tx;
90 struct ieee80211_sub_if_data *sdata = sta->sdata;
91 struct ieee80211_local *local = sdata->local;
Marco Porschd012a602012-10-10 12:39:50 -070092 struct ps_data *ps;
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030093
Johannes Berge3685e02014-02-20 11:19:58 +010094 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
Johannes Berg5ac2e352014-05-27 16:32:27 +020095 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
96 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
Marco Porschd012a602012-10-10 12:39:50 -070097 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
98 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
99 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100100 else if (ieee80211_vif_is_mesh(&sdata->vif))
101 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700102 else
103 return;
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300104
105 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Berge3685e02014-02-20 11:19:58 +0100106 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200107 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300108
Marco Porschd012a602012-10-10 12:39:50 -0700109 atomic_dec(&ps->num_sta_ps);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300110 }
111
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100112 if (sta->sta.txq[0]) {
113 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
114 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
115 int n = skb_queue_len(&txqi->queue);
116
117 ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
118 atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
Michal Kaziorf2ac7e32016-01-27 15:26:12 +0100119 txqi->byte_cnt = 0;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100120 }
121 }
122
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300123 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
124 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
Felix Fietkau1f98ab72012-11-10 03:44:14 +0100125 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
126 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300127 }
128
Thomas Pedersen45b50282013-02-06 10:17:21 -0800129 if (ieee80211_vif_is_mesh(&sdata->vif))
130 mesh_sta_cleanup(sta);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300131
Johannes Berg5ac2e352014-05-27 16:32:27 +0200132 cancel_work_sync(&sta->drv_deliver_wk);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300133
134 /*
135 * Destroy aggregation state here. It would be nice to wait for the
136 * driver to finish aggregation stop and then clean up, but for now
137 * drivers have to handle aggregation stop being requested, followed
138 * directly by station destruction.
139 */
Johannes Berg5a306f52012-11-14 23:22:21 +0100140 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berg661eb382013-06-12 22:47:56 +0200141 kfree(sta->ampdu_mlme.tid_start_tx[i]);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300142 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
143 if (!tid_tx)
144 continue;
Felix Fietkau1f98ab72012-11-10 03:44:14 +0100145 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300146 kfree(tid_tx);
147 }
Johannes Berg5108ca82014-02-17 20:49:03 +0100148}
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300149
Johannes Berg5108ca82014-02-17 20:49:03 +0100150static void cleanup_single_sta(struct sta_info *sta)
151{
152 struct ieee80211_sub_if_data *sdata = sta->sdata;
153 struct ieee80211_local *local = sdata->local;
154
155 __cleanup_single_sta(sta);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300156 sta_info_free(local, sta);
157}
158
Johannes Bergd0709a62008-02-25 16:27:46 +0100159/* protected by RCU */
Johannes Bergabe60632009-11-25 17:46:18 +0100160struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
161 const u8 *addr)
Johannes Berg43ba7e92008-02-21 14:09:30 +0100162{
Johannes Bergabe60632009-11-25 17:46:18 +0100163 struct ieee80211_local *local = sdata->local;
Johannes Berg60f4b622015-04-23 14:02:30 +0200164 struct sta_info *sta;
165 struct rhash_head *tmp;
166 const struct bucket_table *tbl;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100167
Johannes Berg60f4b622015-04-23 14:02:30 +0200168 rcu_read_lock();
169 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
170
171 for_each_sta_info(local, tbl, addr, sta, tmp) {
172 if (sta->sdata == sdata) {
173 rcu_read_unlock();
174 /* this is safe as the caller must already hold
175 * another rcu read section or the mutex
176 */
177 return sta;
178 }
179 }
180 rcu_read_unlock();
181 return NULL;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100182}
183
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100184/*
185 * Get sta info either from the specified interface
186 * or from one of its vlans
187 */
188struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
189 const u8 *addr)
190{
191 struct ieee80211_local *local = sdata->local;
192 struct sta_info *sta;
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100193 struct rhash_head *tmp;
194 const struct bucket_table *tbl;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100195
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100196 rcu_read_lock();
197 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
198
199 for_each_sta_info(local, tbl, addr, sta, tmp) {
200 if (sta->sdata == sdata ||
201 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
202 rcu_read_unlock();
203 /* this is safe as the caller must already hold
204 * another rcu read section or the mutex
205 */
206 return sta;
207 }
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100208 }
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100209 rcu_read_unlock();
210 return NULL;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100211}
212
Johannes Berg3b53fde82009-11-16 12:00:37 +0100213struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
214 int idx)
Luis Carlos Coboee385852008-02-23 15:17:11 +0100215{
Johannes Berg3b53fde82009-11-16 12:00:37 +0100216 struct ieee80211_local *local = sdata->local;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100217 struct sta_info *sta;
218 int i = 0;
219
Johannes Bergd0709a62008-02-25 16:27:46 +0100220 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg3b53fde82009-11-16 12:00:37 +0100221 if (sdata != sta->sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800222 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100223 if (i < idx) {
224 ++i;
225 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100226 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800227 return sta;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100228 }
Luis Carlos Coboee385852008-02-23 15:17:11 +0100229
230 return NULL;
231}
Jiri Bencf0706e82007-05-05 11:45:53 -0700232
Johannes Berg93e5deb2008-04-01 15:21:00 +0200233/**
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100234 * sta_info_free - free STA
Johannes Berg93e5deb2008-04-01 15:21:00 +0200235 *
Randy Dunlap6ef307b2008-07-03 13:52:18 -0700236 * @local: pointer to the global information
Johannes Berg93e5deb2008-04-01 15:21:00 +0200237 * @sta: STA info to free
238 *
239 * This function must undo everything done by sta_info_alloc()
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100240 * that may happen before sta_info_insert(). It may only be
241 * called when sta_info_insert() has not been attempted (and
242 * if that fails, the station is freed anyway.)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200243 */
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100244void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200245{
Johannes Berg889cbb92012-01-17 10:33:29 +0100246 if (sta->rate_ctrl)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100247 rate_control_free_sta(sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200248
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200249 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200250
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100251 if (sta->sta.txq[0])
252 kfree(to_txq_info(sta->sta.txq[0]));
Felix Fietkau53d04522014-05-27 22:33:57 +0200253 kfree(rcu_dereference_raw(sta->sta.rates));
Johannes Berg433f5bc2015-06-17 10:31:00 +0200254#ifdef CONFIG_MAC80211_MESH
255 kfree(sta->mesh);
256#endif
Johannes 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 Berg0be6ed12016-03-31 20:02:05 +0300344 ewma_signal_init(&sta->rx_stats_avg.signal);
345 for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
346 ewma_signal_init(&sta->rx_stats_avg.chain_signal[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);
Sudip Mukherjeeea32f062016-02-02 13:21:14 +0530565 kfree(sinfo);
Johannes Berg4d339602011-12-15 11:24:20 +0100566 rcu_read_lock();
567 return err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300568}
569
570int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
571{
572 struct ieee80211_local *local = sta->local;
Zhao, Gang308f7fc2014-04-21 12:53:00 +0800573 int err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300574
Johannes Berg4d339602011-12-15 11:24:20 +0100575 might_sleep();
576
Johannes Berg31104892015-10-22 17:35:19 +0200577 mutex_lock(&local->sta_mtx);
578
Guy Eilam8c71df72011-08-17 15:18:14 +0300579 err = sta_info_insert_check(sta);
580 if (err) {
Johannes Berg31104892015-10-22 17:35:19 +0200581 mutex_unlock(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700582 rcu_read_lock();
583 goto out_free;
584 }
585
Johannes Berg4d339602011-12-15 11:24:20 +0100586 err = sta_info_insert_finish(sta);
Guy Eilam8c71df72011-08-17 15:18:14 +0300587 if (err)
Johannes Berg004c8722008-02-20 11:21:35 +0100588 goto out_free;
Johannes Bergd0709a62008-02-25 16:27:46 +0100589
Johannes Berg004c8722008-02-20 11:21:35 +0100590 return 0;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200591 out_free:
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100592 sta_info_free(local, sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200593 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -0700594}
595
Johannes Berg34e89502010-02-03 13:59:58 +0100596int sta_info_insert(struct sta_info *sta)
597{
598 int err = sta_info_insert_rcu(sta);
599
600 rcu_read_unlock();
601
602 return err;
603}
604
Marco Porschd012a602012-10-10 12:39:50 -0700605static inline void __bss_tim_set(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100606{
607 /*
608 * This format has been mandated by the IEEE specifications,
609 * so this line may not be changed to use the __set_bit() format.
610 */
Marco Porschd012a602012-10-10 12:39:50 -0700611 tim[id / 8] |= (1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100612}
613
Marco Porschd012a602012-10-10 12:39:50 -0700614static inline void __bss_tim_clear(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100615{
616 /*
617 * This format has been mandated by the IEEE specifications,
618 * so this line may not be changed to use the __clear_bit() format.
619 */
Marco Porschd012a602012-10-10 12:39:50 -0700620 tim[id / 8] &= ~(1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100621}
622
Ilan Peer3d5839b2013-03-05 15:27:20 +0200623static inline bool __bss_tim_get(u8 *tim, u16 id)
624{
625 /*
626 * This format has been mandated by the IEEE specifications,
627 * so this line may not be changed to use the test_bit() format.
628 */
629 return tim[id / 8] & (1 << (id % 8));
630}
631
Johannes Berg948d8872011-09-29 16:04:29 +0200632static unsigned long ieee80211_tids_for_ac(int ac)
Johannes Berg004c8722008-02-20 11:21:35 +0100633{
Johannes Berg948d8872011-09-29 16:04:29 +0200634 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
635 switch (ac) {
636 case IEEE80211_AC_VO:
637 return BIT(6) | BIT(7);
638 case IEEE80211_AC_VI:
639 return BIT(4) | BIT(5);
640 case IEEE80211_AC_BE:
641 return BIT(0) | BIT(3);
642 case IEEE80211_AC_BK:
643 return BIT(1) | BIT(2);
644 default:
645 WARN_ON(1);
646 return 0;
Johannes Bergd0709a62008-02-25 16:27:46 +0100647 }
Johannes Berg004c8722008-02-20 11:21:35 +0100648}
649
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100650static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
Johannes Berg004c8722008-02-20 11:21:35 +0100651{
Johannes Bergc868cb352011-09-29 16:04:27 +0200652 struct ieee80211_local *local = sta->local;
Marco Porschd012a602012-10-10 12:39:50 -0700653 struct ps_data *ps;
Johannes Berg948d8872011-09-29 16:04:29 +0200654 bool indicate_tim = false;
655 u8 ignore_for_tim = sta->sta.uapsd_queues;
656 int ac;
Bob Copelanda69bd8e2015-07-14 08:31:58 -0400657 u16 id = sta->sta.aid;
Johannes Berg004c8722008-02-20 11:21:35 +0100658
Marco Porschd012a602012-10-10 12:39:50 -0700659 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
660 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
661 if (WARN_ON_ONCE(!sta->sdata->bss))
662 return;
663
664 ps = &sta->sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100665#ifdef CONFIG_MAC80211_MESH
666 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
667 ps = &sta->sdata->u.mesh.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100668#endif
Marco Porschd012a602012-10-10 12:39:50 -0700669 } else {
Johannes Bergc868cb352011-09-29 16:04:27 +0200670 return;
Marco Porschd012a602012-10-10 12:39:50 -0700671 }
Johannes Berg3e122be2008-07-09 14:40:34 +0200672
Johannes Bergc868cb352011-09-29 16:04:27 +0200673 /* No need to do anything if the driver does all */
Johannes Berg30686bf2015-06-02 21:39:54 +0200674 if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
Johannes Bergc868cb352011-09-29 16:04:27 +0200675 return;
Johannes Berg004c8722008-02-20 11:21:35 +0100676
Johannes Bergc868cb352011-09-29 16:04:27 +0200677 if (sta->dead)
678 goto done;
Johannes Berg3e122be2008-07-09 14:40:34 +0200679
Johannes Berg948d8872011-09-29 16:04:29 +0200680 /*
681 * If all ACs are delivery-enabled then we should build
682 * the TIM bit for all ACs anyway; if only some are then
683 * we ignore those and build the TIM bit using only the
684 * non-enabled ones.
685 */
686 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
687 ignore_for_tim = 0;
Johannes Berg3e122be2008-07-09 14:40:34 +0200688
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100689 if (ignore_pending)
690 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
691
Johannes Berg948d8872011-09-29 16:04:29 +0200692 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
693 unsigned long tids;
694
695 if (ignore_for_tim & BIT(ac))
696 continue;
697
698 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
699 !skb_queue_empty(&sta->ps_tx_buf[ac]);
700 if (indicate_tim)
701 break;
702
703 tids = ieee80211_tids_for_ac(ac);
704
705 indicate_tim |=
706 sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100707 indicate_tim |=
708 sta->txq_buffered_tids & tids;
Johannes Bergd0709a62008-02-25 16:27:46 +0100709 }
Johannes Berg004c8722008-02-20 11:21:35 +0100710
Johannes Bergc868cb352011-09-29 16:04:27 +0200711 done:
Johannes Berg65f704a2013-02-13 17:39:53 +0100712 spin_lock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100713
Ilan Peer3d5839b2013-03-05 15:27:20 +0200714 if (indicate_tim == __bss_tim_get(ps->tim, id))
715 goto out_unlock;
716
Johannes Berg948d8872011-09-29 16:04:29 +0200717 if (indicate_tim)
Marco Porschd012a602012-10-10 12:39:50 -0700718 __bss_tim_set(ps->tim, id);
Johannes Bergc868cb352011-09-29 16:04:27 +0200719 else
Marco Porschd012a602012-10-10 12:39:50 -0700720 __bss_tim_clear(ps->tim, id);
Johannes Berg3e122be2008-07-09 14:40:34 +0200721
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100722 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
Johannes Bergc868cb352011-09-29 16:04:27 +0200723 local->tim_in_locked_section = true;
Johannes Berg948d8872011-09-29 16:04:29 +0200724 drv_set_tim(local, &sta->sta, indicate_tim);
Johannes Bergc868cb352011-09-29 16:04:27 +0200725 local->tim_in_locked_section = false;
Johannes Berg004c8722008-02-20 11:21:35 +0100726 }
Johannes Berg004c8722008-02-20 11:21:35 +0100727
Ilan Peer3d5839b2013-03-05 15:27:20 +0200728out_unlock:
Johannes Berg65f704a2013-02-13 17:39:53 +0100729 spin_unlock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100730}
731
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100732void sta_info_recalc_tim(struct sta_info *sta)
733{
734 __sta_info_recalc_tim(sta, false);
735}
736
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200737static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700738{
Johannes Berge039fa42008-05-15 12:55:29 +0200739 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700740 int timeout;
741
742 if (!skb)
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200743 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700744
Johannes Berge039fa42008-05-15 12:55:29 +0200745 info = IEEE80211_SKB_CB(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700746
747 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200748 timeout = (sta->listen_interval *
749 sta->sdata->vif.bss_conf.beacon_int *
750 32 / 15625) * HZ;
Jiri Bencf0706e82007-05-05 11:45:53 -0700751 if (timeout < STA_TX_BUFFER_EXPIRE)
752 timeout = STA_TX_BUFFER_EXPIRE;
Johannes Berge039fa42008-05-15 12:55:29 +0200753 return time_after(jiffies, info->control.jiffies + timeout);
Jiri Bencf0706e82007-05-05 11:45:53 -0700754}
755
756
Johannes Berg948d8872011-09-29 16:04:29 +0200757static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
758 struct sta_info *sta, int ac)
Jiri Bencf0706e82007-05-05 11:45:53 -0700759{
760 unsigned long flags;
761 struct sk_buff *skb;
762
Johannes Berg60750392011-09-29 16:04:28 +0200763 /*
764 * First check for frames that should expire on the filtered
765 * queue. Frames here were rejected by the driver and are on
766 * a separate queue to avoid reordering with normal PS-buffered
767 * frames. They also aren't accounted for right now in the
768 * total_ps_buffered counter.
769 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700770 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200771 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
772 skb = skb_peek(&sta->tx_filtered[ac]);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200773 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200774 skb = __skb_dequeue(&sta->tx_filtered[ac]);
Johannes Berg836341a2008-02-20 02:07:21 +0100775 else
Jiri Bencf0706e82007-05-05 11:45:53 -0700776 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200777 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700778
Johannes Berg60750392011-09-29 16:04:28 +0200779 /*
780 * Frames are queued in order, so if this one
781 * hasn't expired yet we can stop testing. If
782 * we actually reached the end of the queue we
783 * also need to stop, of course.
784 */
785 if (!skb)
786 break;
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200787 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg60750392011-09-29 16:04:28 +0200788 }
789
790 /*
791 * Now also check the normal PS-buffered queue, this will
792 * only find something if the filtered queue was emptied
793 * since the filtered frames are all before the normal PS
794 * buffered frames.
795 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700796 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200797 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
798 skb = skb_peek(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700799 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200800 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700801 else
802 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200803 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700804
Johannes Berg60750392011-09-29 16:04:28 +0200805 /*
806 * frames are queued in order, so if this one
807 * hasn't expired yet (or we reached the end of
808 * the queue) we can stop testing
809 */
Johannes Berg836341a2008-02-20 02:07:21 +0100810 if (!skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700811 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100812
Johannes Berg836341a2008-02-20 02:07:21 +0100813 local->total_ps_buffered--;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200814 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
815 sta->sta.addr);
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200816 ieee80211_free_txskb(&local->hw, skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700817 }
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300818
Johannes Berg60750392011-09-29 16:04:28 +0200819 /*
820 * Finally, recalculate the TIM bit for this station -- it might
821 * now be clear because the station was too slow to retrieve its
822 * frames.
823 */
824 sta_info_recalc_tim(sta);
825
826 /*
827 * Return whether there are any frames still buffered, this is
828 * used to check whether the cleanup timer still needs to run,
829 * if there are no frames we don't need to rearm the timer.
830 */
Johannes Berg948d8872011-09-29 16:04:29 +0200831 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
832 skb_queue_empty(&sta->tx_filtered[ac]));
833}
834
835static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
836 struct sta_info *sta)
837{
838 bool have_buffered = false;
839 int ac;
840
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100841 /* This is only necessary for stations on BSS/MBSS interfaces */
842 if (!sta->sdata->bss &&
843 !ieee80211_vif_is_mesh(&sta->sdata->vif))
Johannes Berg948d8872011-09-29 16:04:29 +0200844 return false;
845
846 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
847 have_buffered |=
848 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
849
850 return have_buffered;
Jiri Bencf0706e82007-05-05 11:45:53 -0700851}
852
Johannes Bergd7782072013-12-04 23:12:31 +0100853static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100854{
855 struct ieee80211_local *local;
856 struct ieee80211_sub_if_data *sdata;
Johannes Berg6d10e462013-03-06 23:09:11 +0100857 int ret;
Johannes Berg34e89502010-02-03 13:59:58 +0100858
859 might_sleep();
860
861 if (!sta)
862 return -ENOENT;
863
864 local = sta->local;
865 sdata = sta->sdata;
866
Johannes Berg83d5cc02012-01-12 09:31:10 +0100867 lockdep_assert_held(&local->sta_mtx);
868
Johannes Berg098a6072010-04-06 11:18:47 +0200869 /*
870 * Before removing the station from the driver and
871 * rate control, it might still start new aggregation
872 * sessions -- block that to make sure the tear-down
873 * will be sufficient.
874 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200875 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Bergc82c4a82012-07-18 13:31:31 +0200876 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
Johannes Berg098a6072010-04-06 11:18:47 +0200877
Sara Sharonf59374e2016-03-02 23:46:14 +0200878 /*
879 * Before removing the station from the driver there might be pending
880 * rx frames on RSS queues sent prior to the disassociation - wait for
881 * all such frames to be processed.
882 */
883 drv_sync_rx_queues(local, sta);
884
Johannes Berg34e89502010-02-03 13:59:58 +0100885 ret = sta_info_hash_del(local, sta);
Johannes Bergb01711b2013-12-04 20:25:27 +0100886 if (WARN_ON(ret))
Johannes Berg34e89502010-02-03 13:59:58 +0100887 return ret;
888
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200889 /*
890 * for TDLS peers, make sure to return to the base channel before
891 * removal.
892 */
893 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
894 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
895 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
896 }
897
Arik Nemtsov794454c2012-06-03 23:32:32 +0300898 list_del_rcu(&sta->list);
Eliad Pelleref044762015-11-17 10:24:37 +0200899 sta->removed = true;
Johannes Berg4d339602011-12-15 11:24:20 +0100900
Johannes Berg6a9d1b92013-12-04 22:39:17 +0100901 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
902
Johannes Berga710c812013-12-04 20:11:06 +0100903 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
904 rcu_access_pointer(sdata->u.vlan.sta) == sta)
905 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
906
Johannes Bergd7782072013-12-04 23:12:31 +0100907 return 0;
908}
909
910static void __sta_info_destroy_part2(struct sta_info *sta)
911{
912 struct ieee80211_local *local = sta->local;
913 struct ieee80211_sub_if_data *sdata = sta->sdata;
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100914 struct station_info *sinfo;
Johannes Bergd7782072013-12-04 23:12:31 +0100915 int ret;
916
917 /*
918 * NOTE: This assumes at least synchronize_net() was done
919 * after _part1 and before _part2!
920 */
921
922 might_sleep();
923 lockdep_assert_held(&local->sta_mtx);
924
Johannes Bergc8782072013-12-04 23:05:45 +0100925 /* now keys can no longer be reached */
Johannes Berg6d10e462013-03-06 23:09:11 +0100926 ieee80211_free_sta_keys(local, sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100927
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100928 /* disable TIM bit - last chance to tell driver */
929 __sta_info_recalc_tim(sta, true);
930
Johannes Berg34e89502010-02-03 13:59:58 +0100931 sta->dead = true;
932
Johannes Berg34e89502010-02-03 13:59:58 +0100933 local->num_sta--;
934 local->sta_generation++;
935
Johannes Berg83d5cc02012-01-12 09:31:10 +0100936 while (sta->sta_state > IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100937 ret = sta_info_move_state(sta, sta->sta_state - 1);
938 if (ret) {
Johannes Berg83d5cc02012-01-12 09:31:10 +0100939 WARN_ON_ONCE(1);
940 break;
941 }
942 }
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100943
Johannes Bergf09603a2012-01-20 13:55:21 +0100944 if (sta->uploaded) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100945 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
946 IEEE80211_STA_NOTEXIST);
947 WARN_ON_ONCE(ret != 0);
948 }
Johannes Berg34e89502010-02-03 13:59:58 +0100949
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200950 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
951
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100952 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
953 if (sinfo)
954 sta_set_sinfo(sta, sinfo);
955 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
956 kfree(sinfo);
Jouni Malinenec15e682011-03-23 15:29:52 +0200957
Johannes Berg34e89502010-02-03 13:59:58 +0100958 rate_control_remove_sta_debugfs(sta);
959 ieee80211_sta_debugfs_remove(sta);
960
Johannes Bergd34ba212013-12-04 22:46:11 +0100961 cleanup_single_sta(sta);
Johannes Bergd7782072013-12-04 23:12:31 +0100962}
963
964int __must_check __sta_info_destroy(struct sta_info *sta)
965{
966 int err = __sta_info_destroy_part1(sta);
967
968 if (err)
969 return err;
970
971 synchronize_net();
972
973 __sta_info_destroy_part2(sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100974
975 return 0;
976}
977
978int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
979{
980 struct sta_info *sta;
981 int ret;
982
983 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100984 sta = sta_info_get(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100985 ret = __sta_info_destroy(sta);
986 mutex_unlock(&sdata->local->sta_mtx);
987
988 return ret;
989}
990
991int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
992 const u8 *addr)
993{
994 struct sta_info *sta;
995 int ret;
996
997 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100998 sta = sta_info_get_bss(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100999 ret = __sta_info_destroy(sta);
1000 mutex_unlock(&sdata->local->sta_mtx);
1001
1002 return ret;
1003}
Jiri Bencf0706e82007-05-05 11:45:53 -07001004
1005static void sta_info_cleanup(unsigned long data)
1006{
1007 struct ieee80211_local *local = (struct ieee80211_local *) data;
1008 struct sta_info *sta;
Juuso Oikarinen3393a602010-04-19 10:12:52 +03001009 bool timer_needed = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001010
Johannes Bergd0709a62008-02-25 16:27:46 +01001011 rcu_read_lock();
1012 list_for_each_entry_rcu(sta, &local->sta_list, list)
Juuso Oikarinen3393a602010-04-19 10:12:52 +03001013 if (sta_info_cleanup_expire_buffered(local, sta))
1014 timer_needed = true;
Johannes Bergd0709a62008-02-25 16:27:46 +01001015 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001016
Johannes Berg5bb644a2009-05-17 11:40:42 +02001017 if (local->quiescing)
1018 return;
1019
Juuso Oikarinen3393a602010-04-19 10:12:52 +03001020 if (!timer_needed)
1021 return;
1022
Johannes Berg26d59532011-04-01 13:52:48 +02001023 mod_timer(&local->sta_cleanup,
1024 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
Jiri Bencf0706e82007-05-05 11:45:53 -07001025}
1026
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001027u32 sta_addr_hash(const void *key, u32 length, u32 seed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001028{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001029 return jhash(key, ETH_ALEN, seed);
1030}
1031
1032int sta_info_init(struct ieee80211_local *local)
1033{
1034 int err;
1035
1036 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
1037 if (err)
1038 return err;
1039
Johannes Berg4d339602011-12-15 11:24:20 +01001040 spin_lock_init(&local->tim_lock);
Johannes Berg34e89502010-02-03 13:59:58 +01001041 mutex_init(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07001042 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001043
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001044 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1045 (unsigned long)local);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001046 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001047}
1048
1049void sta_info_stop(struct ieee80211_local *local)
1050{
Johannes Berga56f9922012-12-13 23:08:52 +01001051 del_timer_sync(&local->sta_cleanup);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001052 rhashtable_destroy(&local->sta_hash);
Jiri Bencf0706e82007-05-05 11:45:53 -07001053}
1054
Johannes Berg051007d2012-12-13 23:49:02 +01001055
Johannes Berge7162512013-12-04 23:18:37 +01001056int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
Jiri Bencf0706e82007-05-05 11:45:53 -07001057{
Johannes Bergb998e8b2012-12-13 23:07:46 +01001058 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001059 struct sta_info *sta, *tmp;
Johannes Bergd7782072013-12-04 23:12:31 +01001060 LIST_HEAD(free_list);
Johannes Berg44213b52008-02-25 16:27:49 +01001061 int ret = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001062
Johannes Bergd0709a62008-02-25 16:27:46 +01001063 might_sleep();
1064
Johannes Berge7162512013-12-04 23:18:37 +01001065 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1066 WARN_ON(vlans && !sdata->bss);
1067
Johannes Berg34e89502010-02-03 13:59:58 +01001068 mutex_lock(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +01001069 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Johannes Berge7162512013-12-04 23:18:37 +01001070 if (sdata == sta->sdata ||
1071 (vlans && sdata->bss == sta->sdata->bss)) {
Johannes Bergd7782072013-12-04 23:12:31 +01001072 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1073 list_add(&sta->free_list, &free_list);
Johannes Berg34316832012-02-25 21:40:46 +01001074 ret++;
1075 }
Johannes Berg34e89502010-02-03 13:59:58 +01001076 }
Johannes Bergd7782072013-12-04 23:12:31 +01001077
1078 if (!list_empty(&free_list)) {
1079 synchronize_net();
1080 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1081 __sta_info_destroy_part2(sta);
1082 }
Johannes Berg34e89502010-02-03 13:59:58 +01001083 mutex_unlock(&local->sta_mtx);
Johannes Berg44213b52008-02-25 16:27:49 +01001084
Johannes Berg051007d2012-12-13 23:49:02 +01001085 return ret;
1086}
1087
Johannes Berg24723d12008-09-11 00:01:46 +02001088void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1089 unsigned long exp_time)
1090{
1091 struct ieee80211_local *local = sdata->local;
1092 struct sta_info *sta, *tmp;
Johannes Berg24723d12008-09-11 00:01:46 +02001093
Johannes Berg34e89502010-02-03 13:59:58 +01001094 mutex_lock(&local->sta_mtx);
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301095
1096 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Johannes Bergb8da6b62016-03-31 20:02:07 +03001097 unsigned long last_active = ieee80211_sta_last_active(sta);
1098
Marek Lindnerec2b7742011-12-20 23:16:52 +08001099 if (sdata != sta->sdata)
1100 continue;
1101
Johannes Bergb8da6b62016-03-31 20:02:07 +03001102 if (time_is_before_jiffies(last_active + exp_time)) {
Mohammed Shafi Shajakhaneea57d42012-10-08 21:33:47 +05301103 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1104 sta->sta.addr);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001105
1106 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1107 test_sta_flag(sta, WLAN_STA_PS_STA))
1108 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1109
Johannes Berg34e89502010-02-03 13:59:58 +01001110 WARN_ON(__sta_info_destroy(sta));
Johannes Berg24723d12008-09-11 00:01:46 +02001111 }
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301112 }
1113
Johannes Berg34e89502010-02-03 13:59:58 +01001114 mutex_unlock(&local->sta_mtx);
Johannes Berg24723d12008-09-11 00:01:46 +02001115}
Johannes Berg17741cd2008-09-11 00:02:02 +02001116
Ben Greear686b9cb2010-09-23 09:44:36 -07001117struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001118 const u8 *addr,
1119 const u8 *localaddr)
Johannes Berg17741cd2008-09-11 00:02:02 +02001120{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001121 struct ieee80211_local *local = hw_to_local(hw);
1122 struct sta_info *sta;
1123 struct rhash_head *tmp;
1124 const struct bucket_table *tbl;
1125
1126 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
Johannes Berg17741cd2008-09-11 00:02:02 +02001127
Ben Greear686b9cb2010-09-23 09:44:36 -07001128 /*
1129 * Just return a random station if localaddr is NULL
1130 * ... first in list.
1131 */
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001132 for_each_sta_info(local, tbl, addr, sta, tmp) {
Ben Greear686b9cb2010-09-23 09:44:36 -07001133 if (localaddr &&
Joe Perchesb203ca32012-05-08 18:56:52 +00001134 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
Ben Greear686b9cb2010-09-23 09:44:36 -07001135 continue;
Johannes Bergf7c65592010-04-30 13:48:36 +02001136 if (!sta->uploaded)
1137 return NULL;
Johannes Bergabe60632009-11-25 17:46:18 +01001138 return &sta->sta;
Johannes Bergf7c65592010-04-30 13:48:36 +02001139 }
1140
Johannes Bergabe60632009-11-25 17:46:18 +01001141 return NULL;
Johannes Berg17741cd2008-09-11 00:02:02 +02001142}
Ben Greear686b9cb2010-09-23 09:44:36 -07001143EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
Johannes Berg5ed176e2009-11-04 14:42:28 +01001144
1145struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1146 const u8 *addr)
1147{
Johannes Bergf7c65592010-04-30 13:48:36 +02001148 struct sta_info *sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001149
1150 if (!vif)
1151 return NULL;
1152
Johannes Bergf7c65592010-04-30 13:48:36 +02001153 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1154 if (!sta)
1155 return NULL;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001156
Johannes Bergf7c65592010-04-30 13:48:36 +02001157 if (!sta->uploaded)
1158 return NULL;
1159
1160 return &sta->sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001161}
Johannes Berg17741cd2008-09-11 00:02:02 +02001162EXPORT_SYMBOL(ieee80211_find_sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001163
Johannes Berge3685e02014-02-20 11:19:58 +01001164/* powersave support code */
1165void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
Johannes Berg50a94322010-11-16 11:50:28 -08001166{
Helmut Schaa608383b2012-01-30 15:18:00 +01001167 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berge3685e02014-02-20 11:19:58 +01001168 struct ieee80211_local *local = sdata->local;
1169 struct sk_buff_head pending;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001170 int filtered = 0, buffered = 0, ac, i;
Johannes Berge3685e02014-02-20 11:19:58 +01001171 unsigned long flags;
Marco Porschd012a602012-10-10 12:39:50 -07001172 struct ps_data *ps;
1173
Felix Fietkau3918edb2014-07-25 16:20:23 +02001174 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1175 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1176 u.ap);
1177
1178 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porschd012a602012-10-10 12:39:50 -07001179 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001180 else if (ieee80211_vif_is_mesh(&sdata->vif))
1181 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07001182 else
1183 return;
Johannes Berg50a94322010-11-16 11:50:28 -08001184
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001185 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg47086fc2011-09-29 16:04:33 +02001186
Johannes Berg5a306f52012-11-14 23:22:21 +01001187 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
Johannes Berg948d8872011-09-29 16:04:29 +02001188 sta->driver_buffered_tids = 0;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001189 sta->txq_buffered_tids = 0;
Johannes Berg948d8872011-09-29 16:04:29 +02001190
Johannes Berg30686bf2015-06-02 21:39:54 +02001191 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001192 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001193
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001194 if (sta->sta.txq[0]) {
1195 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1196 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1197
1198 if (!skb_queue_len(&txqi->queue))
1199 continue;
1200
1201 drv_wake_tx_queue(local, txqi);
1202 }
1203 }
1204
Johannes Berg948d8872011-09-29 16:04:29 +02001205 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001206
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001207 /* sync with ieee80211_tx_h_unicast_ps_buf */
1208 spin_lock(&sta->ps_lock);
Johannes Bergaf818582009-11-06 11:35:50 +01001209 /* Send all buffered frames to the station */
Johannes Berg948d8872011-09-29 16:04:29 +02001210 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1211 int count = skb_queue_len(&pending), tmp;
1212
Arik Nemtsov987c2852012-11-05 10:27:52 +02001213 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001214 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001215 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001216 tmp = skb_queue_len(&pending);
1217 filtered += tmp - count;
1218 count = tmp;
1219
Arik Nemtsov987c2852012-11-05 10:27:52 +02001220 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001221 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001222 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001223 tmp = skb_queue_len(&pending);
1224 buffered += tmp - count;
1225 }
1226
Johannes Berge3685e02014-02-20 11:19:58 +01001227 ieee80211_add_pending_skbs(local, &pending);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001228
1229 /* now we're no longer in the deliver code */
1230 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1231
1232 /* The station might have polled and then woken up before we responded,
1233 * so clear these flags now to avoid them sticking around.
1234 */
1235 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1236 clear_sta_flag(sta, WLAN_STA_UAPSD);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001237 spin_unlock(&sta->ps_lock);
Johannes Berg948d8872011-09-29 16:04:29 +02001238
Johannes Berge3685e02014-02-20 11:19:58 +01001239 atomic_dec(&ps->num_sta_ps);
1240
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001241 /* This station just woke up and isn't aware of our SMPS state */
Chun-Yeow Yeoh062f1d62014-04-22 18:19:25 +08001242 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1243 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001244 sdata->smps_mode) &&
1245 sta->known_smps_mode != sdata->bss->req_smps &&
1246 sta_info_tx_streams(sta) != 1) {
1247 ht_dbg(sdata,
1248 "%pM just woke up and MIMO capable - update SMPS\n",
1249 sta->sta.addr);
1250 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1251 sta->sta.addr,
1252 sdata->vif.bss_conf.bssid);
1253 }
1254
Johannes Bergaf818582009-11-06 11:35:50 +01001255 local->total_ps_buffered -= buffered;
1256
Johannes Bergc868cb352011-09-29 16:04:27 +02001257 sta_info_recalc_tim(sta);
1258
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001259 ps_dbg(sdata,
1260 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1261 sta->sta.addr, sta->sta.aid, filtered, buffered);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001262
1263 ieee80211_check_fast_xmit(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001264}
1265
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001266static void ieee80211_send_null_response(struct sta_info *sta, int tid,
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001267 enum ieee80211_frame_release_type reason,
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001268 bool call_driver, bool more_data)
Johannes Bergce662b442011-09-29 16:04:34 +02001269{
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001270 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Bergce662b442011-09-29 16:04:34 +02001271 struct ieee80211_local *local = sdata->local;
1272 struct ieee80211_qos_hdr *nullfunc;
1273 struct sk_buff *skb;
1274 int size = sizeof(*nullfunc);
1275 __le16 fc;
Johannes Berga74a8c82014-07-22 14:50:47 +02001276 bool qos = sta->sta.wme;
Johannes Bergce662b442011-09-29 16:04:34 +02001277 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02001278 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergce662b442011-09-29 16:04:34 +02001279
1280 if (qos) {
1281 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1282 IEEE80211_STYPE_QOS_NULLFUNC |
1283 IEEE80211_FCTL_FROMDS);
1284 } else {
1285 size -= 2;
1286 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1287 IEEE80211_STYPE_NULLFUNC |
1288 IEEE80211_FCTL_FROMDS);
1289 }
1290
1291 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1292 if (!skb)
1293 return;
1294
1295 skb_reserve(skb, local->hw.extra_tx_headroom);
1296
1297 nullfunc = (void *) skb_put(skb, size);
1298 nullfunc->frame_control = fc;
1299 nullfunc->duration_id = 0;
1300 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1301 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1302 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
Johannes Berg864a6042014-03-04 13:46:53 +01001303 nullfunc->seq_ctrl = 0;
Johannes Bergce662b442011-09-29 16:04:34 +02001304
Johannes Berg59b66252011-10-13 13:19:19 +02001305 skb->priority = tid;
1306 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
Johannes Bergce662b442011-09-29 16:04:34 +02001307 if (qos) {
Johannes Bergce662b442011-09-29 16:04:34 +02001308 nullfunc->qos_ctrl = cpu_to_le16(tid);
1309
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001310 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
Johannes Bergce662b442011-09-29 16:04:34 +02001311 nullfunc->qos_ctrl |=
1312 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001313 if (more_data)
1314 nullfunc->frame_control |=
1315 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1316 }
Johannes Bergce662b442011-09-29 16:04:34 +02001317 }
1318
1319 info = IEEE80211_SKB_CB(skb);
1320
1321 /*
1322 * Tell TX path to send this frame even though the
1323 * STA may still remain is PS mode after this frame
Johannes Bergdeeaee12011-09-29 16:04:35 +02001324 * exchange. Also set EOSP to indicate this packet
1325 * ends the poll/service period.
Johannes Bergce662b442011-09-29 16:04:34 +02001326 */
Johannes Berg02f2f1a2012-02-27 12:18:30 +01001327 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
Johannes Bergdeeaee12011-09-29 16:04:35 +02001328 IEEE80211_TX_STATUS_EOSP |
1329 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergce662b442011-09-29 16:04:34 +02001330
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301331 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1332
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001333 if (call_driver)
1334 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1335 reason, false);
Johannes Berg40b96402011-09-29 16:04:38 +02001336
Johannes Berg89afe612013-02-13 15:39:57 +01001337 skb->dev = sdata->dev;
1338
Johannes Berg55de9082012-07-26 17:24:39 +02001339 rcu_read_lock();
1340 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1341 if (WARN_ON(!chanctx_conf)) {
1342 rcu_read_unlock();
1343 kfree_skb(skb);
1344 return;
1345 }
1346
Johannes Berg73c4e192014-11-09 18:50:09 +02001347 info->band = chanctx_conf->def.chan->band;
Johannes Berg7c107702015-03-20 14:18:27 +01001348 ieee80211_xmit(sdata, sta, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001349 rcu_read_unlock();
Johannes Bergce662b442011-09-29 16:04:34 +02001350}
1351
Johannes Berg0a1cb802014-01-09 11:05:31 +01001352static int find_highest_prio_tid(unsigned long tids)
1353{
1354 /* lower 3 TIDs aren't ordered perfectly */
1355 if (tids & 0xF8)
1356 return fls(tids) - 1;
1357 /* TID 0 is BE just like TID 3 */
1358 if (tids & BIT(0))
1359 return 0;
1360 return fls(tids) - 1;
1361}
1362
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001363/* Indicates if the MORE_DATA bit should be set in the last
1364 * frame obtained by ieee80211_sta_ps_get_frames.
1365 * Note that driver_release_tids is relevant only if
1366 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1367 */
1368static bool
1369ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1370 enum ieee80211_frame_release_type reason,
1371 unsigned long driver_release_tids)
1372{
1373 int ac;
1374
1375 /* If the driver has data on more than one TID then
1376 * certainly there's more data if we release just a
1377 * single frame now (from a single TID). This will
1378 * only happen for PS-Poll.
1379 */
1380 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1381 hweight16(driver_release_tids) > 1)
1382 return true;
1383
1384 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1385 if (ignored_acs & BIT(ac))
1386 continue;
1387
1388 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1389 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1390 return true;
1391 }
1392
1393 return false;
1394}
1395
Johannes Berg47086fc2011-09-29 16:04:33 +02001396static void
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001397ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1398 enum ieee80211_frame_release_type reason,
1399 struct sk_buff_head *frames,
1400 unsigned long *driver_release_tids)
Johannes Bergaf818582009-11-06 11:35:50 +01001401{
1402 struct ieee80211_sub_if_data *sdata = sta->sdata;
1403 struct ieee80211_local *local = sdata->local;
Johannes Berg948d8872011-09-29 16:04:29 +02001404 int ac;
Johannes Bergaf818582009-11-06 11:35:50 +01001405
Johannes Bergf9f760b2014-01-08 17:45:07 +01001406 /* Get response frame(s) and more data bit for the last one. */
Johannes Berg948d8872011-09-29 16:04:29 +02001407 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg4049e092011-09-29 16:04:32 +02001408 unsigned long tids;
1409
Johannes Berg47086fc2011-09-29 16:04:33 +02001410 if (ignored_acs & BIT(ac))
Johannes Berg948d8872011-09-29 16:04:29 +02001411 continue;
1412
Johannes Berg4049e092011-09-29 16:04:32 +02001413 tids = ieee80211_tids_for_ac(ac);
1414
Johannes Bergf9f760b2014-01-08 17:45:07 +01001415 /* if we already have frames from software, then we can't also
1416 * release from hardware queues
1417 */
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001418 if (skb_queue_empty(frames)) {
1419 *driver_release_tids |=
1420 sta->driver_buffered_tids & tids;
1421 *driver_release_tids |= sta->txq_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001422 }
Johannes Berg47086fc2011-09-29 16:04:33 +02001423
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001424 if (!*driver_release_tids) {
Johannes Bergf9f760b2014-01-08 17:45:07 +01001425 struct sk_buff *skb;
1426
1427 while (n_frames > 0) {
1428 skb = skb_dequeue(&sta->tx_filtered[ac]);
1429 if (!skb) {
1430 skb = skb_dequeue(
1431 &sta->ps_tx_buf[ac]);
1432 if (skb)
1433 local->total_ps_buffered--;
1434 }
1435 if (!skb)
1436 break;
1437 n_frames--;
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001438 __skb_queue_tail(frames, skb);
Johannes Bergf9f760b2014-01-08 17:45:07 +01001439 }
Johannes Berg948d8872011-09-29 16:04:29 +02001440 }
1441
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001442 /* If we have more frames buffered on this AC, then abort the
1443 * loop since we can't send more data from other ACs before
1444 * the buffered frames from this.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001445 */
Johannes Berg948d8872011-09-29 16:04:29 +02001446 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001447 !skb_queue_empty(&sta->ps_tx_buf[ac]))
Johannes Berg948d8872011-09-29 16:04:29 +02001448 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001449 }
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001450}
1451
1452static void
1453ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1454 int n_frames, u8 ignored_acs,
1455 enum ieee80211_frame_release_type reason)
1456{
1457 struct ieee80211_sub_if_data *sdata = sta->sdata;
1458 struct ieee80211_local *local = sdata->local;
1459 unsigned long driver_release_tids = 0;
1460 struct sk_buff_head frames;
1461 bool more_data;
1462
1463 /* Service or PS-Poll period starts */
1464 set_sta_flag(sta, WLAN_STA_SP);
1465
1466 __skb_queue_head_init(&frames);
1467
1468 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1469 &frames, &driver_release_tids);
1470
1471 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1472
Emmanuel Grumbach1a570812015-12-20 13:50:00 +02001473 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001474 driver_release_tids =
1475 BIT(find_highest_prio_tid(driver_release_tids));
Johannes Bergaf818582009-11-06 11:35:50 +01001476
Johannes Bergf9f760b2014-01-08 17:45:07 +01001477 if (skb_queue_empty(&frames) && !driver_release_tids) {
Johannes Bergce662b442011-09-29 16:04:34 +02001478 int tid;
Johannes Berg4049e092011-09-29 16:04:32 +02001479
Johannes Bergce662b442011-09-29 16:04:34 +02001480 /*
1481 * For PS-Poll, this can only happen due to a race condition
1482 * when we set the TIM bit and the station notices it, but
1483 * before it can poll for the frame we expire it.
1484 *
1485 * For uAPSD, this is said in the standard (11.2.1.5 h):
1486 * At each unscheduled SP for a non-AP STA, the AP shall
1487 * attempt to transmit at least one MSDU or MMPDU, but no
1488 * more than the value specified in the Max SP Length field
1489 * in the QoS Capability element from delivery-enabled ACs,
1490 * that are destined for the non-AP STA.
1491 *
1492 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1493 */
1494
1495 /* This will evaluate to 1, 3, 5 or 7. */
1496 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1497
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001498 ieee80211_send_null_response(sta, tid, reason, true, false);
Johannes Bergf9f760b2014-01-08 17:45:07 +01001499 } else if (!driver_release_tids) {
Johannes Berg47086fc2011-09-29 16:04:33 +02001500 struct sk_buff_head pending;
1501 struct sk_buff *skb;
Johannes Berg40b96402011-09-29 16:04:38 +02001502 int num = 0;
1503 u16 tids = 0;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001504 bool need_null = false;
Johannes Bergaf818582009-11-06 11:35:50 +01001505
Johannes Berg47086fc2011-09-29 16:04:33 +02001506 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001507
Johannes Berg47086fc2011-09-29 16:04:33 +02001508 while ((skb = __skb_dequeue(&frames))) {
1509 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1510 struct ieee80211_hdr *hdr = (void *) skb->data;
Johannes Berg40b96402011-09-29 16:04:38 +02001511 u8 *qoshdr = NULL;
1512
1513 num++;
Johannes Bergaf818582009-11-06 11:35:50 +01001514
Johannes Berg47086fc2011-09-29 16:04:33 +02001515 /*
1516 * Tell TX path to send this frame even though the
1517 * STA may still remain is PS mode after this frame
1518 * exchange.
1519 */
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301520 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1521 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
Johannes Bergaf818582009-11-06 11:35:50 +01001522
Johannes Berg47086fc2011-09-29 16:04:33 +02001523 /*
1524 * Use MoreData flag to indicate whether there are
1525 * more buffered frames for this STA
1526 */
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001527 if (more_data || !skb_queue_empty(&frames))
Johannes Berg47086fc2011-09-29 16:04:33 +02001528 hdr->frame_control |=
1529 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001530 else
1531 hdr->frame_control &=
1532 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
Johannes Berg47086fc2011-09-29 16:04:33 +02001533
Johannes Berg40b96402011-09-29 16:04:38 +02001534 if (ieee80211_is_data_qos(hdr->frame_control) ||
1535 ieee80211_is_qos_nullfunc(hdr->frame_control))
1536 qoshdr = ieee80211_get_qos_ctl(hdr);
1537
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001538 tids |= BIT(skb->priority);
1539
1540 __skb_queue_tail(&pending, skb);
1541
1542 /* end service period after last frame or add one */
1543 if (!skb_queue_empty(&frames))
1544 continue;
1545
1546 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1547 /* for PS-Poll, there's only one frame */
1548 info->flags |= IEEE80211_TX_STATUS_EOSP |
1549 IEEE80211_TX_CTL_REQ_TX_STATUS;
1550 break;
1551 }
1552
1553 /* For uAPSD, things are a bit more complicated. If the
1554 * last frame has a QoS header (i.e. is a QoS-data or
1555 * QoS-nulldata frame) then just set the EOSP bit there
1556 * and be done.
1557 * If the frame doesn't have a QoS header (which means
1558 * it should be a bufferable MMPDU) then we can't set
1559 * the EOSP bit in the QoS header; add a QoS-nulldata
1560 * frame to the list to send it after the MMPDU.
1561 *
1562 * Note that this code is only in the mac80211-release
1563 * code path, we assume that the driver will not buffer
1564 * anything but QoS-data frames, or if it does, will
1565 * create the QoS-nulldata frame by itself if needed.
1566 *
1567 * Cf. 802.11-2012 10.2.1.10 (c).
1568 */
1569 if (qoshdr) {
1570 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
Johannes Berg47086fc2011-09-29 16:04:33 +02001571
Marco Porsch52a3f202012-03-16 15:30:26 +01001572 info->flags |= IEEE80211_TX_STATUS_EOSP |
1573 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001574 } else {
1575 /* The standard isn't completely clear on this
1576 * as it says the more-data bit should be set
1577 * if there are more BUs. The QoS-Null frame
1578 * we're about to send isn't buffered yet, we
1579 * only create it below, but let's pretend it
1580 * was buffered just in case some clients only
1581 * expect more-data=0 when eosp=1.
1582 */
1583 hdr->frame_control |=
1584 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1585 need_null = true;
1586 num++;
Marco Porsch52a3f202012-03-16 15:30:26 +01001587 }
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001588 break;
Johannes Berg47086fc2011-09-29 16:04:33 +02001589 }
1590
Johannes Berg40b96402011-09-29 16:04:38 +02001591 drv_allow_buffered_frames(local, sta, tids, num,
1592 reason, more_data);
1593
Johannes Berg47086fc2011-09-29 16:04:33 +02001594 ieee80211_add_pending_skbs(local, &pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001595
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001596 if (need_null)
1597 ieee80211_send_null_response(
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001598 sta, find_highest_prio_tid(tids),
1599 reason, false, false);
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001600
Johannes Bergc868cb352011-09-29 16:04:27 +02001601 sta_info_recalc_tim(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001602 } else {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001603 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1604 int tid;
1605
Johannes Bergaf818582009-11-06 11:35:50 +01001606 /*
Johannes Berg4049e092011-09-29 16:04:32 +02001607 * We need to release a frame that is buffered somewhere in the
1608 * driver ... it'll have to handle that.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001609 * Note that the driver also has to check the number of frames
1610 * on the TIDs we're releasing from - if there are more than
1611 * n_frames it has to set the more-data bit (if we didn't ask
1612 * it to set it anyway due to other buffered frames); if there
1613 * are fewer than n_frames it has to make sure to adjust that
1614 * to allow the service period to end properly.
Johannes Bergaf818582009-11-06 11:35:50 +01001615 */
Johannes Berg4049e092011-09-29 16:04:32 +02001616 drv_release_buffered_frames(local, sta, driver_release_tids,
Johannes Berg47086fc2011-09-29 16:04:33 +02001617 n_frames, reason, more_data);
Johannes Berg4049e092011-09-29 16:04:32 +02001618
1619 /*
1620 * Note that we don't recalculate the TIM bit here as it would
1621 * most likely have no effect at all unless the driver told us
Johannes Bergf9f760b2014-01-08 17:45:07 +01001622 * that the TID(s) became empty before returning here from the
Johannes Berg4049e092011-09-29 16:04:32 +02001623 * release function.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001624 * Either way, however, when the driver tells us that the TID(s)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001625 * became empty or we find that a txq became empty, we'll do the
1626 * TIM recalculation.
Johannes Berg4049e092011-09-29 16:04:32 +02001627 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001628
1629 if (!sta->sta.txq[0])
1630 return;
1631
1632 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1633 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1634
1635 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1636 continue;
1637
1638 sta_info_recalc_tim(sta);
1639 break;
1640 }
Johannes Bergaf818582009-11-06 11:35:50 +01001641 }
1642}
1643
Johannes Bergaf818582009-11-06 11:35:50 +01001644void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1645{
Johannes Berg47086fc2011-09-29 16:04:33 +02001646 u8 ignore_for_response = sta->sta.uapsd_queues;
Johannes Bergaf818582009-11-06 11:35:50 +01001647
Johannes Berg47086fc2011-09-29 16:04:33 +02001648 /*
1649 * If all ACs are delivery-enabled then we should reply
1650 * from any of them, if only some are enabled we reply
1651 * only from the non-enabled ones.
1652 */
1653 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1654 ignore_for_response = 0;
1655
1656 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1657 IEEE80211_FRAME_RELEASE_PSPOLL);
1658}
1659
1660void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1661{
1662 int n_frames = sta->sta.max_sp;
1663 u8 delivery_enabled = sta->sta.uapsd_queues;
1664
1665 /*
1666 * If we ever grow support for TSPEC this might happen if
1667 * the TSPEC update from hostapd comes in between a trigger
1668 * frame setting WLAN_STA_UAPSD in the RX path and this
1669 * actually getting called.
1670 */
1671 if (!delivery_enabled)
1672 return;
1673
Johannes Berg47086fc2011-09-29 16:04:33 +02001674 switch (sta->sta.max_sp) {
1675 case 1:
1676 n_frames = 2;
1677 break;
1678 case 2:
1679 n_frames = 4;
1680 break;
1681 case 3:
1682 n_frames = 6;
1683 break;
1684 case 0:
1685 /* XXX: what is a good value? */
Andrei Otcheretianski13a80982014-11-04 11:33:04 +02001686 n_frames = 128;
Johannes Berg47086fc2011-09-29 16:04:33 +02001687 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001688 }
Johannes Bergaf818582009-11-06 11:35:50 +01001689
Johannes Berg47086fc2011-09-29 16:04:33 +02001690 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1691 IEEE80211_FRAME_RELEASE_UAPSD);
Johannes Bergaf818582009-11-06 11:35:50 +01001692}
1693
1694void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1695 struct ieee80211_sta *pubsta, bool block)
1696{
1697 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1698
Johannes Bergb5878a22010-04-07 16:48:40 +02001699 trace_api_sta_block_awake(sta->local, pubsta, block);
1700
Johannes Berg5ac2e352014-05-27 16:32:27 +02001701 if (block) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001702 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001703 ieee80211_clear_fast_xmit(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001704 return;
1705 }
1706
1707 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1708 return;
1709
1710 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1711 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1712 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1713 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1714 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1715 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1716 /* must be asleep in this case */
1717 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1718 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1719 } else {
1720 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001721 ieee80211_check_fast_xmit(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001722 }
Johannes Bergaf818582009-11-06 11:35:50 +01001723}
1724EXPORT_SYMBOL(ieee80211_sta_block_awake);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001725
Johannes Berge9437892013-02-15 21:38:08 +01001726void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
Johannes Berg37fbd902011-09-29 16:04:39 +02001727{
1728 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1729 struct ieee80211_local *local = sta->local;
Johannes Berg37fbd902011-09-29 16:04:39 +02001730
1731 trace_api_eosp(local, pubsta);
1732
Johannes Berge9437892013-02-15 21:38:08 +01001733 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg37fbd902011-09-29 16:04:39 +02001734}
Johannes Berge9437892013-02-15 21:38:08 +01001735EXPORT_SYMBOL(ieee80211_sta_eosp);
Johannes Berg37fbd902011-09-29 16:04:39 +02001736
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001737void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1738{
1739 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1740 enum ieee80211_frame_release_type reason;
1741 bool more_data;
1742
1743 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1744
1745 reason = IEEE80211_FRAME_RELEASE_UAPSD;
1746 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1747 reason, 0);
1748
1749 ieee80211_send_null_response(sta, tid, reason, false, more_data);
1750}
1751EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1752
Johannes Berg042ec452011-09-29 16:04:26 +02001753void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1754 u8 tid, bool buffered)
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001755{
1756 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1757
Johannes Berg5a306f52012-11-14 23:22:21 +01001758 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
Johannes Berg042ec452011-09-29 16:04:26 +02001759 return;
1760
Johannes Berg1b000782013-12-19 10:47:48 +01001761 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1762
Johannes Berg948d8872011-09-29 16:04:29 +02001763 if (buffered)
1764 set_bit(tid, &sta->driver_buffered_tids);
1765 else
1766 clear_bit(tid, &sta->driver_buffered_tids);
1767
Johannes Bergc868cb352011-09-29 16:04:27 +02001768 sta_info_recalc_tim(sta);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001769}
Johannes Berg042ec452011-09-29 16:04:26 +02001770EXPORT_SYMBOL(ieee80211_sta_set_buffered);
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001771
Ayala Beker52cfa1d2016-03-17 15:41:39 +02001772static void
1773ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
1774{
1775 struct ieee80211_local *local = sdata->local;
1776 bool allow_p2p_go_ps = sdata->vif.p2p;
1777 struct sta_info *sta;
1778
1779 rcu_read_lock();
1780 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1781 if (sdata != sta->sdata ||
1782 !test_sta_flag(sta, WLAN_STA_ASSOC))
1783 continue;
1784 if (!sta->sta.support_p2p_ps) {
1785 allow_p2p_go_ps = false;
1786 break;
1787 }
1788 }
1789 rcu_read_unlock();
1790
1791 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
1792 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
1793 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
1794 }
1795}
1796
Johannes Berg83d5cc02012-01-12 09:31:10 +01001797int sta_info_move_state(struct sta_info *sta,
1798 enum ieee80211_sta_state new_state)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001799{
Johannes Berg8bf11d82011-12-15 11:17:37 +01001800 might_sleep();
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001801
1802 if (sta->sta_state == new_state)
1803 return 0;
1804
Johannes Bergf09603a2012-01-20 13:55:21 +01001805 /* check allowed transitions first */
1806
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001807 switch (new_state) {
1808 case IEEE80211_STA_NONE:
Johannes Bergf09603a2012-01-20 13:55:21 +01001809 if (sta->sta_state != IEEE80211_STA_AUTH)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001810 return -EINVAL;
1811 break;
1812 case IEEE80211_STA_AUTH:
Johannes Bergf09603a2012-01-20 13:55:21 +01001813 if (sta->sta_state != IEEE80211_STA_NONE &&
1814 sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001815 return -EINVAL;
1816 break;
1817 case IEEE80211_STA_ASSOC:
Johannes Bergf09603a2012-01-20 13:55:21 +01001818 if (sta->sta_state != IEEE80211_STA_AUTH &&
1819 sta->sta_state != IEEE80211_STA_AUTHORIZED)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001820 return -EINVAL;
1821 break;
1822 case IEEE80211_STA_AUTHORIZED:
Johannes Bergf09603a2012-01-20 13:55:21 +01001823 if (sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001824 return -EINVAL;
1825 break;
1826 default:
1827 WARN(1, "invalid state %d", new_state);
1828 return -EINVAL;
1829 }
1830
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001831 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1832 sta->sta.addr, new_state);
Johannes Bergf09603a2012-01-20 13:55:21 +01001833
1834 /*
1835 * notify the driver before the actual changes so it can
1836 * fail the transition
1837 */
1838 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1839 int err = drv_sta_state(sta->local, sta->sdata, sta,
1840 sta->sta_state, new_state);
1841 if (err)
1842 return err;
1843 }
1844
1845 /* reflect the change in all state variables */
1846
1847 switch (new_state) {
1848 case IEEE80211_STA_NONE:
1849 if (sta->sta_state == IEEE80211_STA_AUTH)
1850 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1851 break;
1852 case IEEE80211_STA_AUTH:
Ilan Peera7201a62015-12-13 13:41:43 +02001853 if (sta->sta_state == IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +01001854 set_bit(WLAN_STA_AUTH, &sta->_flags);
Ilan Peera7201a62015-12-13 13:41:43 +02001855 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
Johannes Bergf09603a2012-01-20 13:55:21 +01001856 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
Ilan Peera7201a62015-12-13 13:41:43 +02001857 ieee80211_recalc_min_chandef(sta->sdata);
Ayala Beker52cfa1d2016-03-17 15:41:39 +02001858 if (!sta->sta.support_p2p_ps)
1859 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
Ilan Peera7201a62015-12-13 13:41:43 +02001860 }
Johannes Bergf09603a2012-01-20 13:55:21 +01001861 break;
1862 case IEEE80211_STA_ASSOC:
1863 if (sta->sta_state == IEEE80211_STA_AUTH) {
1864 set_bit(WLAN_STA_ASSOC, &sta->_flags);
Ilan Peera7201a62015-12-13 13:41:43 +02001865 ieee80211_recalc_min_chandef(sta->sdata);
Ayala Beker52cfa1d2016-03-17 15:41:39 +02001866 if (!sta->sta.support_p2p_ps)
1867 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
Johannes Bergf09603a2012-01-20 13:55:21 +01001868 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001869 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1870 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1871 !sta->sdata->u.vlan.sta))
1872 atomic_dec(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001873 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001874 ieee80211_clear_fast_xmit(sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001875 }
1876 break;
1877 case IEEE80211_STA_AUTHORIZED:
1878 if (sta->sta_state == IEEE80211_STA_ASSOC) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001879 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1880 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1881 !sta->sdata->u.vlan.sta))
1882 atomic_inc(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001883 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001884 ieee80211_check_fast_xmit(sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001885 }
1886 break;
1887 default:
1888 break;
1889 }
1890
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001891 sta->sta_state = new_state;
1892
1893 return 0;
1894}
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001895
1896u8 sta_info_tx_streams(struct sta_info *sta)
1897{
1898 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1899 u8 rx_streams;
1900
1901 if (!sta->sta.ht_cap.ht_supported)
1902 return 1;
1903
1904 if (sta->sta.vht_cap.vht_supported) {
1905 int i;
1906 u16 tx_mcs_map =
1907 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1908
1909 for (i = 7; i >= 0; i--)
1910 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1911 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1912 return i + 1;
1913 }
1914
1915 if (ht_cap->mcs.rx_mask[3])
1916 rx_streams = 4;
1917 else if (ht_cap->mcs.rx_mask[2])
1918 rx_streams = 3;
1919 else if (ht_cap->mcs.rx_mask[1])
1920 rx_streams = 2;
1921 else
1922 rx_streams = 1;
1923
1924 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1925 return rx_streams;
1926
1927 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1928 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1929}
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001930
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001931static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
1932{
1933 rinfo->flags = 0;
1934
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001935 if (sta->rx_stats.last_rate_flag & RX_FLAG_HT) {
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001936 rinfo->flags |= RATE_INFO_FLAGS_MCS;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001937 rinfo->mcs = sta->rx_stats.last_rate_idx;
1938 } else if (sta->rx_stats.last_rate_flag & RX_FLAG_VHT) {
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001939 rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001940 rinfo->nss = sta->rx_stats.last_rate_vht_nss;
1941 rinfo->mcs = sta->rx_stats.last_rate_idx;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001942 } else {
1943 struct ieee80211_supported_band *sband;
1944 int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
1945 u16 brate;
1946
1947 sband = sta->local->hw.wiphy->bands[
1948 ieee80211_get_sdata_band(sta->sdata)];
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001949 brate = sband->bitrates[sta->rx_stats.last_rate_idx].bitrate;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001950 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
1951 }
1952
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001953 if (sta->rx_stats.last_rate_flag & RX_FLAG_SHORT_GI)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001954 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
1955
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001956 if (sta->rx_stats.last_rate_flag & RX_FLAG_5MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001957 rinfo->bw = RATE_INFO_BW_5;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001958 else if (sta->rx_stats.last_rate_flag & RX_FLAG_10MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001959 rinfo->bw = RATE_INFO_BW_10;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001960 else if (sta->rx_stats.last_rate_flag & RX_FLAG_40MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001961 rinfo->bw = RATE_INFO_BW_40;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001962 else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_80MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001963 rinfo->bw = RATE_INFO_BW_80;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001964 else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_160MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001965 rinfo->bw = RATE_INFO_BW_160;
1966 else
1967 rinfo->bw = RATE_INFO_BW_20;
1968}
1969
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001970void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1971{
1972 struct ieee80211_sub_if_data *sdata = sta->sdata;
1973 struct ieee80211_local *local = sdata->local;
John W. Linville9a244402014-07-25 10:22:36 -04001974 struct rate_control_ref *ref = NULL;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001975 u32 thr = 0;
1976 int i, ac;
1977
John W. Linville9a244402014-07-25 10:22:36 -04001978 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1979 ref = local->rate_ctrl;
1980
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001981 sinfo->generation = sdata->local->sta_generation;
1982
Johannes Berg225b8182015-01-21 21:09:02 +01001983 /* do before driver, so beacon filtering drivers have a
1984 * chance to e.g. just add the number of filtered beacons
1985 * (or just modify the value entirely, of course)
1986 */
1987 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1988 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1989
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001990 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1991
Johannes Berg319090b2014-11-17 14:08:11 +01001992 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1993 BIT(NL80211_STA_INFO_STA_FLAGS) |
1994 BIT(NL80211_STA_INFO_BSS_PARAM) |
1995 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
Johannes Berg976bd9e2015-10-16 17:18:11 +02001996 BIT(NL80211_STA_INFO_RX_DROP_MISC);
1997
1998 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1999 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2000 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
2001 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002002
Arnd Bergmann84b00602015-09-30 13:26:36 +02002003 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002004 sinfo->inactive_time =
Johannes Bergb8da6b62016-03-31 20:02:07 +03002005 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002006
Johannes Berg319090b2014-11-17 14:08:11 +01002007 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
2008 BIT(NL80211_STA_INFO_TX_BYTES)))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002009 sinfo->tx_bytes = 0;
2010 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002011 sinfo->tx_bytes += sta->tx_stats.bytes[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01002012 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002013 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002014
Johannes Berg319090b2014-11-17 14:08:11 +01002015 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002016 sinfo->tx_packets = 0;
2017 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002018 sinfo->tx_packets += sta->tx_stats.packets[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01002019 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002020 }
2021
Johannes Berg319090b2014-11-17 14:08:11 +01002022 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
2023 BIT(NL80211_STA_INFO_RX_BYTES)))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002024 sinfo->rx_bytes = sta->rx_stats.bytes;
Johannes Berg319090b2014-11-17 14:08:11 +01002025 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002026 }
2027
Johannes Berg319090b2014-11-17 14:08:11 +01002028 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002029 sinfo->rx_packets = sta->rx_stats.packets;
Johannes Berg319090b2014-11-17 14:08:11 +01002030 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002031 }
2032
Johannes Berg319090b2014-11-17 14:08:11 +01002033 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002034 sinfo->tx_retries = sta->status_stats.retry_count;
Johannes Berg319090b2014-11-17 14:08:11 +01002035 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002036 }
2037
Johannes Berg319090b2014-11-17 14:08:11 +01002038 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002039 sinfo->tx_failed = sta->status_stats.retry_failed;
Johannes Berg319090b2014-11-17 14:08:11 +01002040 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002041 }
2042
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002043 sinfo->rx_dropped_misc = sta->rx_stats.dropped;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002044
Johannes Berg225b8182015-01-21 21:09:02 +01002045 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2046 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2047 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
2048 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2049 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2050 }
2051
Johannes Berg30686bf2015-06-02 21:39:54 +02002052 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2053 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
Johannes Berg319090b2014-11-17 14:08:11 +01002054 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002055 sinfo->signal = (s8)sta->rx_stats.last_signal;
Johannes Berg319090b2014-11-17 14:08:11 +01002056 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002057 }
2058
Johannes Berg319090b2014-11-17 14:08:11 +01002059 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
Johannes Berg40d9a382015-07-13 12:26:46 +02002060 sinfo->signal_avg =
Johannes Berg0be6ed12016-03-31 20:02:05 +03002061 -ewma_signal_read(&sta->rx_stats_avg.signal);
Johannes Berg319090b2014-11-17 14:08:11 +01002062 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002063 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002064 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002065
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002066 if (sta->rx_stats.chains &&
Johannes Berg319090b2014-11-17 14:08:11 +01002067 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2068 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2069 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2070 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002071
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002072 sinfo->chains = sta->rx_stats.chains;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002073 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002074 sinfo->chain_signal[i] =
2075 sta->rx_stats.chain_signal_last[i];
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002076 sinfo->chain_signal_avg[i] =
Johannes Berg0be6ed12016-03-31 20:02:05 +03002077 -ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002078 }
2079 }
2080
Johannes Berg319090b2014-11-17 14:08:11 +01002081 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002082 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2083 &sinfo->txrate);
Johannes Berg319090b2014-11-17 14:08:11 +01002084 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002085 }
2086
Johannes Berg319090b2014-11-17 14:08:11 +01002087 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002088 sta_set_rate_info_rx(sta, &sinfo->rxrate);
Johannes Berg319090b2014-11-17 14:08:11 +01002089 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002090 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002091
Johannes Berg79c892b2014-11-21 14:26:31 +01002092 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
2093 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
2094 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
2095
2096 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2097 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002098 tidstats->rx_msdu = sta->rx_stats.msdu[i];
Johannes Berg79c892b2014-11-21 14:26:31 +01002099 }
2100
2101 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2102 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002103 tidstats->tx_msdu = sta->tx_stats.msdu[i];
Johannes Berg79c892b2014-11-21 14:26:31 +01002104 }
2105
2106 if (!(tidstats->filled &
2107 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02002108 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Berg79c892b2014-11-21 14:26:31 +01002109 tidstats->filled |=
2110 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002111 tidstats->tx_msdu_retries =
2112 sta->status_stats.msdu_retries[i];
Johannes Berg79c892b2014-11-21 14:26:31 +01002113 }
2114
2115 if (!(tidstats->filled &
2116 BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02002117 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Berg79c892b2014-11-21 14:26:31 +01002118 tidstats->filled |=
2119 BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002120 tidstats->tx_msdu_failed =
2121 sta->status_stats.msdu_failed[i];
Johannes Berg79c892b2014-11-21 14:26:31 +01002122 }
2123 }
2124
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002125 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2126#ifdef CONFIG_MAC80211_MESH
Johannes Berg319090b2014-11-17 14:08:11 +01002127 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2128 BIT(NL80211_STA_INFO_PLID) |
2129 BIT(NL80211_STA_INFO_PLINK_STATE) |
2130 BIT(NL80211_STA_INFO_LOCAL_PM) |
2131 BIT(NL80211_STA_INFO_PEER_PM) |
2132 BIT(NL80211_STA_INFO_NONPEER_PM);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002133
Johannes Berg433f5bc2015-06-17 10:31:00 +02002134 sinfo->llid = sta->mesh->llid;
2135 sinfo->plid = sta->mesh->plid;
2136 sinfo->plink_state = sta->mesh->plink_state;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002137 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
Johannes Berg319090b2014-11-17 14:08:11 +01002138 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
Johannes Berg433f5bc2015-06-17 10:31:00 +02002139 sinfo->t_offset = sta->mesh->t_offset;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002140 }
Johannes Berg433f5bc2015-06-17 10:31:00 +02002141 sinfo->local_pm = sta->mesh->local_pm;
2142 sinfo->peer_pm = sta->mesh->peer_pm;
2143 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002144#endif
2145 }
2146
2147 sinfo->bss_param.flags = 0;
2148 if (sdata->vif.bss_conf.use_cts_prot)
2149 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2150 if (sdata->vif.bss_conf.use_short_preamble)
2151 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2152 if (sdata->vif.bss_conf.use_short_slot)
2153 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
Emmanuel Grumbach785e21a2014-09-03 15:25:04 +03002154 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002155 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2156
2157 sinfo->sta_flags.set = 0;
2158 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2159 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2160 BIT(NL80211_STA_FLAG_WME) |
2161 BIT(NL80211_STA_FLAG_MFP) |
2162 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2163 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2164 BIT(NL80211_STA_FLAG_TDLS_PEER);
2165 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2166 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2167 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2168 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
Johannes Berga74a8c82014-07-22 14:50:47 +02002169 if (sta->sta.wme)
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002170 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2171 if (test_sta_flag(sta, WLAN_STA_MFP))
2172 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2173 if (test_sta_flag(sta, WLAN_STA_AUTH))
2174 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2175 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2176 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2177 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2178 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2179
2180 /* check if the driver has a SW RC implementation */
2181 if (ref && ref->ops->get_expected_throughput)
2182 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2183 else
2184 thr = drv_get_expected_throughput(local, &sta->sta);
2185
2186 if (thr != 0) {
Johannes Berg319090b2014-11-17 14:08:11 +01002187 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002188 sinfo->expected_throughput = thr;
2189 }
2190}
Johannes Bergb8da6b62016-03-31 20:02:07 +03002191
2192unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2193{
2194 if (time_after(sta->rx_stats.last_rx, sta->status_stats.last_ack))
2195 return sta->rx_stats.last_rx;
2196 return sta->status_stats.last_ack;
2197}