blob: b28e7a220d563429fb45518e7a8729fb97f619c3 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
Johannes Bergd98ad832014-09-03 15:24:57 +03004 * Copyright 2013-2014 Intel Mobile Communications GmbH
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02005 * Copyright (C) 2015 Intel Deutschland GmbH
Jiri Bencf0706e82007-05-05 11:45:53 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
Felix Fietkau888d04d2012-03-01 15:22:09 +010014#include <linux/etherdevice.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070015#include <linux/netdevice.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/if_arp.h>
Johannes Berg0d174402007-12-17 15:07:43 +010020#include <linux/timer.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010021#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070022
23#include <net/mac80211.h>
24#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020025#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040026#include "rate.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070027#include "sta_info.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070028#include "debugfs_sta.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010029#include "mesh.h"
Johannes Bergce662b442011-09-29 16:04:34 +020030#include "wme.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070031
Johannes Bergd0709a62008-02-25 16:27:46 +010032/**
33 * DOC: STA information lifetime rules
34 *
35 * STA info structures (&struct sta_info) are managed in a hash table
36 * for faster lookup and a list for iteration. They are managed using
37 * RCU, i.e. access to the list and hash table is protected by RCU.
38 *
Johannes Berg34e89502010-02-03 13:59:58 +010039 * Upon allocating a STA info structure with sta_info_alloc(), the caller
40 * owns that structure. It must then insert it into the hash table using
41 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
42 * case (which acquires an rcu read section but must not be called from
43 * within one) will the pointer still be valid after the call. Note that
44 * the caller may not do much with the STA info before inserting it, in
45 * particular, it may not start any mesh peer link management or add
46 * encryption keys.
Johannes Berg93e5deb2008-04-01 15:21:00 +020047 *
48 * When the insertion fails (sta_info_insert()) returns non-zero), the
49 * structure will have been freed by sta_info_insert()!
Johannes Bergd0709a62008-02-25 16:27:46 +010050 *
Johannes Berg34e89502010-02-03 13:59:58 +010051 * Station entries are added by mac80211 when you establish a link with a
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040052 * peer. This means different things for the different type of interfaces
53 * we support. For a regular station this mean we add the AP sta when we
Lucas De Marchi25985ed2011-03-30 22:57:33 -030054 * receive an association response from the AP. For IBSS this occurs when
Johannes Berg34e89502010-02-03 13:59:58 +010055 * get to know about a peer on the same IBSS. For WDS we add the sta for
Lucas De Marchi25985ed2011-03-30 22:57:33 -030056 * the peer immediately upon device open. When using AP mode we add stations
Johannes Berg34e89502010-02-03 13:59:58 +010057 * for each respective station upon request from userspace through nl80211.
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040058 *
Johannes Berg34e89502010-02-03 13:59:58 +010059 * In order to remove a STA info structure, various sta_info_destroy_*()
60 * calls are available.
Johannes Bergd0709a62008-02-25 16:27:46 +010061 *
Johannes Berg34e89502010-02-03 13:59:58 +010062 * There is no concept of ownership on a STA entry, each structure is
63 * owned by the global hash table/list until it is removed. All users of
64 * the structure need to be RCU protected so that the structure won't be
65 * freed before they are done using it.
Johannes Bergd0709a62008-02-25 16:27:46 +010066 */
Jiri Bencf0706e82007-05-05 11:45:53 -070067
Johannes Berg7bedd0c2015-02-13 21:55:15 +010068static const struct rhashtable_params sta_rht_params = {
69 .nelem_hint = 3, /* start small */
Johannes Bergcaf22d32015-04-24 11:10:10 +020070 .automatic_shrinking = true,
Johannes Berg7bedd0c2015-02-13 21:55:15 +010071 .head_offset = offsetof(struct sta_info, hash_node),
Johannes Bergac100ce2015-06-16 16:22:12 +020072 .key_offset = offsetof(struct sta_info, addr),
Johannes Berg7bedd0c2015-02-13 21:55:15 +010073 .key_len = ETH_ALEN,
74 .hashfn = sta_addr_hash,
Johannes Bergebd82b32015-04-23 17:26:06 +020075 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
Johannes Berg7bedd0c2015-02-13 21:55:15 +010076};
77
Johannes Berg4d339602011-12-15 11:24:20 +010078/* Caller must hold local->sta_mtx */
Michael Wube8755e2007-07-27 15:43:23 +020079static int sta_info_hash_del(struct ieee80211_local *local,
80 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070081{
Johannes Berg7bedd0c2015-02-13 21:55:15 +010082 return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
83 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -070084}
85
Johannes Berg5108ca82014-02-17 20:49:03 +010086static void __cleanup_single_sta(struct sta_info *sta)
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030087{
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030088 int ac, i;
89 struct tid_ampdu_tx *tid_tx;
90 struct ieee80211_sub_if_data *sdata = sta->sdata;
91 struct ieee80211_local *local = sdata->local;
Marco Porschd012a602012-10-10 12:39:50 -070092 struct ps_data *ps;
Eliad Pellerb22cfcf2012-09-09 14:43:51 +030093
Johannes Berge3685e02014-02-20 11:19:58 +010094 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
Johannes Berg5ac2e352014-05-27 16:32:27 +020095 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
96 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
Marco Porschd012a602012-10-10 12:39:50 -070097 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
98 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
99 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100100 else if (ieee80211_vif_is_mesh(&sdata->vif))
101 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700102 else
103 return;
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300104
105 clear_sta_flag(sta, WLAN_STA_PS_STA);
Johannes Berge3685e02014-02-20 11:19:58 +0100106 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200107 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300108
Marco Porschd012a602012-10-10 12:39:50 -0700109 atomic_dec(&ps->num_sta_ps);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300110 }
111
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100112 if (sta->sta.txq[0]) {
113 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
114 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
115 int n = skb_queue_len(&txqi->queue);
116
117 ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
118 atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
119 }
120 }
121
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300122 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
123 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
Felix Fietkau1f98ab72012-11-10 03:44:14 +0100124 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
125 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300126 }
127
Thomas Pedersen45b50282013-02-06 10:17:21 -0800128 if (ieee80211_vif_is_mesh(&sdata->vif))
129 mesh_sta_cleanup(sta);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300130
Johannes Berg5ac2e352014-05-27 16:32:27 +0200131 cancel_work_sync(&sta->drv_deliver_wk);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300132
133 /*
134 * Destroy aggregation state here. It would be nice to wait for the
135 * driver to finish aggregation stop and then clean up, but for now
136 * drivers have to handle aggregation stop being requested, followed
137 * directly by station destruction.
138 */
Johannes Berg5a306f52012-11-14 23:22:21 +0100139 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berg661eb382013-06-12 22:47:56 +0200140 kfree(sta->ampdu_mlme.tid_start_tx[i]);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300141 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
142 if (!tid_tx)
143 continue;
Felix Fietkau1f98ab72012-11-10 03:44:14 +0100144 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300145 kfree(tid_tx);
146 }
Johannes Berg5108ca82014-02-17 20:49:03 +0100147}
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300148
Johannes Berg5108ca82014-02-17 20:49:03 +0100149static void cleanup_single_sta(struct sta_info *sta)
150{
151 struct ieee80211_sub_if_data *sdata = sta->sdata;
152 struct ieee80211_local *local = sdata->local;
153
154 __cleanup_single_sta(sta);
Eliad Pellerb22cfcf2012-09-09 14:43:51 +0300155 sta_info_free(local, sta);
156}
157
Johannes Bergd0709a62008-02-25 16:27:46 +0100158/* protected by RCU */
Johannes Bergabe60632009-11-25 17:46:18 +0100159struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
160 const u8 *addr)
Johannes Berg43ba7e92008-02-21 14:09:30 +0100161{
Johannes Bergabe60632009-11-25 17:46:18 +0100162 struct ieee80211_local *local = sdata->local;
Johannes Berg60f4b622015-04-23 14:02:30 +0200163 struct sta_info *sta;
164 struct rhash_head *tmp;
165 const struct bucket_table *tbl;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100166
Johannes Berg60f4b622015-04-23 14:02:30 +0200167 rcu_read_lock();
168 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
169
170 for_each_sta_info(local, tbl, addr, sta, tmp) {
171 if (sta->sdata == sdata) {
172 rcu_read_unlock();
173 /* this is safe as the caller must already hold
174 * another rcu read section or the mutex
175 */
176 return sta;
177 }
178 }
179 rcu_read_unlock();
180 return NULL;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100181}
182
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100183/*
184 * Get sta info either from the specified interface
185 * or from one of its vlans
186 */
187struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
188 const u8 *addr)
189{
190 struct ieee80211_local *local = sdata->local;
191 struct sta_info *sta;
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100192 struct rhash_head *tmp;
193 const struct bucket_table *tbl;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100194
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100195 rcu_read_lock();
196 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
197
198 for_each_sta_info(local, tbl, addr, sta, tmp) {
199 if (sta->sdata == sdata ||
200 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
201 rcu_read_unlock();
202 /* this is safe as the caller must already hold
203 * another rcu read section or the mutex
204 */
205 return sta;
206 }
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100207 }
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100208 rcu_read_unlock();
209 return NULL;
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100210}
211
Johannes Berg3b53fde82009-11-16 12:00:37 +0100212struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
213 int idx)
Luis Carlos Coboee385852008-02-23 15:17:11 +0100214{
Johannes Berg3b53fde82009-11-16 12:00:37 +0100215 struct ieee80211_local *local = sdata->local;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100216 struct sta_info *sta;
217 int i = 0;
218
Johannes Bergd0709a62008-02-25 16:27:46 +0100219 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg3b53fde82009-11-16 12:00:37 +0100220 if (sdata != sta->sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800221 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100222 if (i < idx) {
223 ++i;
224 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100225 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800226 return sta;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100227 }
Luis Carlos Coboee385852008-02-23 15:17:11 +0100228
229 return NULL;
230}
Jiri Bencf0706e82007-05-05 11:45:53 -0700231
Johannes Berg93e5deb2008-04-01 15:21:00 +0200232/**
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100233 * sta_info_free - free STA
Johannes Berg93e5deb2008-04-01 15:21:00 +0200234 *
Randy Dunlap6ef307b2008-07-03 13:52:18 -0700235 * @local: pointer to the global information
Johannes Berg93e5deb2008-04-01 15:21:00 +0200236 * @sta: STA info to free
237 *
238 * This function must undo everything done by sta_info_alloc()
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100239 * that may happen before sta_info_insert(). It may only be
240 * called when sta_info_insert() has not been attempted (and
241 * if that fails, the station is freed anyway.)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200242 */
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100243void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200244{
Johannes Berg889cbb92012-01-17 10:33:29 +0100245 if (sta->rate_ctrl)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100246 rate_control_free_sta(sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200247
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200248 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200249
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100250 if (sta->sta.txq[0])
251 kfree(to_txq_info(sta->sta.txq[0]));
Felix Fietkau53d04522014-05-27 22:33:57 +0200252 kfree(rcu_dereference_raw(sta->sta.rates));
Johannes Berg433f5bc2015-06-17 10:31:00 +0200253#ifdef CONFIG_MAC80211_MESH
254 kfree(sta->mesh);
255#endif
Johannes Berg93e5deb2008-04-01 15:21:00 +0200256 kfree(sta);
257}
258
Johannes Berg4d339602011-12-15 11:24:20 +0100259/* Caller must hold local->sta_mtx */
Johannes Bergd0709a62008-02-25 16:27:46 +0100260static void sta_info_hash_add(struct ieee80211_local *local,
261 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700262{
Johannes Berg7bedd0c2015-02-13 21:55:15 +0100263 rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
264 sta_rht_params);
Jiri Bencf0706e82007-05-05 11:45:53 -0700265}
Jiri Bencf0706e82007-05-05 11:45:53 -0700266
Johannes Berg5ac2e352014-05-27 16:32:27 +0200267static void sta_deliver_ps_frames(struct work_struct *wk)
Johannes Bergaf818582009-11-06 11:35:50 +0100268{
269 struct sta_info *sta;
270
Johannes Berg5ac2e352014-05-27 16:32:27 +0200271 sta = container_of(wk, struct sta_info, drv_deliver_wk);
Johannes Bergaf818582009-11-06 11:35:50 +0100272
273 if (sta->dead)
274 return;
275
Johannes Berg5ac2e352014-05-27 16:32:27 +0200276 local_bh_disable();
277 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
Johannes Bergaf818582009-11-06 11:35:50 +0100278 ieee80211_sta_ps_deliver_wakeup(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200279 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
Johannes Bergaf818582009-11-06 11:35:50 +0100280 ieee80211_sta_ps_deliver_poll_response(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200281 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
Johannes Berg47086fc2011-09-29 16:04:33 +0200282 ieee80211_sta_ps_deliver_uapsd(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200283 local_bh_enable();
Johannes Bergaf818582009-11-06 11:35:50 +0100284}
285
Johannes Bergaf65cd962009-11-17 18:18:36 +0100286static int sta_prepare_rate_control(struct ieee80211_local *local,
287 struct sta_info *sta, gfp_t gfp)
288{
Johannes Berg30686bf2015-06-02 21:39:54 +0200289 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
Johannes Bergaf65cd962009-11-17 18:18:36 +0100290 return 0;
291
Johannes Berg889cbb92012-01-17 10:33:29 +0100292 sta->rate_ctrl = local->rate_ctrl;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100293 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
Johannes Berg35c347a2015-03-05 16:10:08 +0100294 sta, gfp);
Johannes Berg889cbb92012-01-17 10:33:29 +0100295 if (!sta->rate_ctrl_priv)
Johannes Bergaf65cd962009-11-17 18:18:36 +0100296 return -ENOMEM;
Johannes Bergaf65cd962009-11-17 18:18:36 +0100297
298 return 0;
299}
300
Johannes Berg73651ee2008-02-25 16:27:47 +0100301struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
Johannes Berg56544162011-12-14 13:28:46 +0100302 const u8 *addr, gfp_t gfp)
Jiri Bencf0706e82007-05-05 11:45:53 -0700303{
Johannes Bergd0709a62008-02-25 16:27:46 +0100304 struct ieee80211_local *local = sdata->local;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100305 struct ieee80211_hw *hw = &local->hw;
Jiri Bencf0706e82007-05-05 11:45:53 -0700306 struct sta_info *sta;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200307 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700308
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100309 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
Jiri Bencf0706e82007-05-05 11:45:53 -0700310 if (!sta)
Johannes Berg73651ee2008-02-25 16:27:47 +0100311 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700312
Johannes Berg07346f812008-05-03 01:02:02 +0200313 spin_lock_init(&sta->lock);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200314 spin_lock_init(&sta->ps_lock);
Johannes Berg5ac2e352014-05-27 16:32:27 +0200315 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
Johannes Berg67c282c2010-06-10 10:21:43 +0200316 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
Johannes Berga93e3642010-06-10 10:21:46 +0200317 mutex_init(&sta->ampdu_mlme.mtx);
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800318#ifdef CONFIG_MAC80211_MESH
Johannes Berg433f5bc2015-06-17 10:31:00 +0200319 if (ieee80211_vif_is_mesh(&sdata->vif)) {
320 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
321 if (!sta->mesh)
322 goto free;
323 spin_lock_init(&sta->mesh->plink_lock);
324 if (ieee80211_vif_is_mesh(&sdata->vif) &&
325 !sdata->u.mesh.user_mpm)
326 init_timer(&sta->mesh->plink_timer);
327 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
328 }
Thomas Pedersen87f59c72013-03-01 22:02:52 -0800329#endif
Johannes Berg07346f812008-05-03 01:02:02 +0200330
Johannes Bergac100ce2015-06-16 16:22:12 +0200331 memcpy(sta->addr, addr, ETH_ALEN);
Johannes Berg17741cd2008-09-11 00:02:02 +0200332 memcpy(sta->sta.addr, addr, ETH_ALEN);
Johannes Bergd0709a62008-02-25 16:27:46 +0100333 sta->local = local;
334 sta->sdata = sdata;
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200335 sta->rx_stats.last_rx = jiffies;
Jiri Bencf0706e82007-05-05 11:45:53 -0700336
Johannes Berg71ec3752012-01-20 13:55:20 +0100337 sta->sta_state = IEEE80211_STA_NONE;
338
Liad Kaufmanb6da9112014-11-19 13:47:38 +0200339 /* Mark TID as unreserved */
340 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
341
Arnd Bergmann84b00602015-09-30 13:26:36 +0200342 sta->last_connected = ktime_get_seconds();
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200343 ewma_signal_init(&sta->rx_stats.avg_signal);
344 for (i = 0; i < ARRAY_SIZE(sta->rx_stats.chain_signal_avg); i++)
345 ewma_signal_init(&sta->rx_stats.chain_signal_avg[i]);
Bruno Randolf541a45a2010-12-02 19:12:43 +0900346
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100347 if (local->ops->wake_tx_queue) {
348 void *txq_data;
349 int size = sizeof(struct txq_info) +
350 ALIGN(hw->txq_data_size, sizeof(void *));
351
352 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
353 if (!txq_data)
354 goto free;
355
356 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
357 struct txq_info *txq = txq_data + i * size;
358
359 ieee80211_init_tx_queue(sdata, sta, txq, i);
360 }
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100361 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700362
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100363 if (sta_prepare_rate_control(local, sta, gfp))
364 goto free_txq;
365
Johannes Berg5a306f52012-11-14 23:22:21 +0100366 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
Johannes Berga622ab72010-06-10 10:21:39 +0200367 /*
368 * timer_to_tid must be initialized with identity mapping
369 * to enable session_timer's data differentiation. See
370 * sta_rx_agg_session_timer_expired for usage.
371 */
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200372 sta->timer_to_tid[i] = i;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200373 }
Johannes Berg948d8872011-09-29 16:04:29 +0200374 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
375 skb_queue_head_init(&sta->ps_tx_buf[i]);
376 skb_queue_head_init(&sta->tx_filtered[i]);
377 }
Johannes Berg73651ee2008-02-25 16:27:47 +0100378
Johannes Berg5a306f52012-11-14 23:22:21 +0100379 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700380 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
Senthil Balasubramaniancccaec92009-05-14 18:42:08 +0530381
Johannes Bergaf0ed692013-02-12 14:21:00 +0100382 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300383 if (sdata->vif.type == NL80211_IFTYPE_AP ||
384 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
385 struct ieee80211_supported_band *sband =
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100386 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
Emmanuel Grumbach687da132013-10-01 16:45:43 +0300387 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
388 IEEE80211_HT_CAP_SM_PS_SHIFT;
389 /*
390 * Assume that hostapd advertises our caps in the beacon and
391 * this is the known_smps_mode for a station that just assciated
392 */
393 switch (smps) {
394 case WLAN_HT_SMPS_CONTROL_DISABLED:
395 sta->known_smps_mode = IEEE80211_SMPS_OFF;
396 break;
397 case WLAN_HT_SMPS_CONTROL_STATIC:
398 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
399 break;
400 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
401 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
402 break;
403 default:
404 WARN_ON(1);
405 }
406 }
Johannes Bergaf0ed692013-02-12 14:21:00 +0100407
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200408 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
Johannes Bergef04a292014-01-06 15:56:59 +0100409
Johannes Bergabfbc3a2015-02-25 10:03:25 +0100410 return sta;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100411
412free_txq:
413 if (sta->sta.txq[0])
414 kfree(to_txq_info(sta->sta.txq[0]));
415free:
Johannes Berg433f5bc2015-06-17 10:31:00 +0200416#ifdef CONFIG_MAC80211_MESH
417 kfree(sta->mesh);
418#endif
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100419 kfree(sta);
420 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +0100421}
422
Guy Eilam8c71df72011-08-17 15:18:14 +0300423static int sta_info_insert_check(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100424{
Johannes Berg34e89502010-02-03 13:59:58 +0100425 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg34e89502010-02-03 13:59:58 +0100426
Johannes Berg03e44972008-02-27 09:56:40 +0100427 /*
428 * Can't be a WARN_ON because it can be triggered through a race:
429 * something inserts a STA (on one CPU) without holding the RTNL
430 * and another CPU turns off the net device.
431 */
Guy Eilam8c71df72011-08-17 15:18:14 +0300432 if (unlikely(!ieee80211_sdata_running(sdata)))
433 return -ENETDOWN;
Johannes Berg03e44972008-02-27 09:56:40 +0100434
Joe Perchesb203ca32012-05-08 18:56:52 +0000435 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
Guy Eilam8c71df72011-08-17 15:18:14 +0300436 is_multicast_ether_addr(sta->sta.addr)))
437 return -EINVAL;
438
Johannes Berg31104892015-10-22 17:35:19 +0200439 /* Strictly speaking this isn't necessary as we hold the mutex, but
440 * the rhashtable code can't really deal with that distinction. We
441 * do require the mutex for correctness though.
442 */
443 rcu_read_lock();
444 lockdep_assert_held(&sdata->local->sta_mtx);
445 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
446 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
447 rcu_read_unlock();
448 return -ENOTUNIQ;
449 }
450 rcu_read_unlock();
451
Guy Eilam8c71df72011-08-17 15:18:14 +0300452 return 0;
453}
454
Johannes Bergf09603a2012-01-20 13:55:21 +0100455static int sta_info_insert_drv_state(struct ieee80211_local *local,
456 struct ieee80211_sub_if_data *sdata,
457 struct sta_info *sta)
458{
459 enum ieee80211_sta_state state;
460 int err = 0;
461
462 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
463 err = drv_sta_state(local, sdata, sta, state, state + 1);
464 if (err)
465 break;
466 }
467
468 if (!err) {
Johannes Berga4ec45a2012-01-20 13:55:22 +0100469 /*
470 * Drivers using legacy sta_add/sta_remove callbacks only
471 * get uploaded set to true after sta_add is called.
472 */
473 if (!local->ops->sta_add)
474 sta->uploaded = true;
Johannes Bergf09603a2012-01-20 13:55:21 +0100475 return 0;
476 }
477
478 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200479 sdata_info(sdata,
480 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
481 sta->sta.addr, state + 1, err);
Johannes Bergf09603a2012-01-20 13:55:21 +0100482 err = 0;
483 }
484
485 /* unwind on error */
486 for (; state > IEEE80211_STA_NOTEXIST; state--)
487 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
488
489 return err;
490}
491
Guy Eilam8c71df72011-08-17 15:18:14 +0300492/*
493 * should be called with sta_mtx locked
494 * this function replaces the mutex lock
495 * with a RCU lock
496 */
Johannes Berg4d339602011-12-15 11:24:20 +0100497static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
Guy Eilam8c71df72011-08-17 15:18:14 +0300498{
499 struct ieee80211_local *local = sta->local;
500 struct ieee80211_sub_if_data *sdata = sta->sdata;
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100501 struct station_info *sinfo;
Guy Eilam8c71df72011-08-17 15:18:14 +0300502 int err = 0;
503
504 lockdep_assert_held(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +0100505
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100506 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
507 if (!sinfo) {
508 err = -ENOMEM;
509 goto out_err;
510 }
511
Johannes Berg7852e362012-01-20 13:55:24 +0100512 /* check if STA exists already */
513 if (sta_info_get_bss(sdata, sta->sta.addr)) {
514 err = -EEXIST;
515 goto out_err;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100516 }
Johannes Berg32bfd352007-12-19 01:31:26 +0100517
Johannes Berg7852e362012-01-20 13:55:24 +0100518 local->num_sta++;
519 local->sta_generation++;
520 smp_mb();
Johannes Berg4d339602011-12-15 11:24:20 +0100521
Johannes Berg5108ca82014-02-17 20:49:03 +0100522 /* simplify things and don't accept BA sessions yet */
523 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
524
Johannes Berg7852e362012-01-20 13:55:24 +0100525 /* make the station visible */
526 sta_info_hash_add(local, sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100527
Arik Nemtsov2bad77482014-10-22 12:32:16 +0300528 list_add_tail_rcu(&sta->list, &local->sta_list);
Johannes Berg83d5cc02012-01-12 09:31:10 +0100529
Johannes Berg5108ca82014-02-17 20:49:03 +0100530 /* notify driver */
531 err = sta_info_insert_drv_state(local, sdata, sta);
532 if (err)
533 goto out_remove;
534
Johannes Berg7852e362012-01-20 13:55:24 +0100535 set_sta_flag(sta, WLAN_STA_INSERTED);
Johannes Berg5108ca82014-02-17 20:49:03 +0100536 /* accept BA sessions now */
537 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg4d339602011-12-15 11:24:20 +0100538
Johannes Berg7852e362012-01-20 13:55:24 +0100539 ieee80211_sta_debugfs_add(sta);
540 rate_control_add_sta_debugfs(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100541
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100542 sinfo->generation = local->sta_generation;
543 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
544 kfree(sinfo);
Johannes Bergd0709a62008-02-25 16:27:46 +0100545
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200546 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700547
Johannes Berg34e89502010-02-03 13:59:58 +0100548 /* move reference to rcu-protected */
549 rcu_read_lock();
550 mutex_unlock(&local->sta_mtx);
Jiri Bence9f207f2007-05-05 11:46:38 -0700551
Johannes Berg73651ee2008-02-25 16:27:47 +0100552 if (ieee80211_vif_is_mesh(&sdata->vif))
553 mesh_accept_plinks_update(sdata);
554
555 return 0;
Johannes Berg5108ca82014-02-17 20:49:03 +0100556 out_remove:
557 sta_info_hash_del(local, sta);
558 list_del_rcu(&sta->list);
559 local->num_sta--;
560 synchronize_net();
561 __cleanup_single_sta(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100562 out_err:
563 mutex_unlock(&local->sta_mtx);
564 rcu_read_lock();
565 return err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300566}
567
568int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
569{
570 struct ieee80211_local *local = sta->local;
Zhao, Gang308f7fc2014-04-21 12:53:00 +0800571 int err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300572
Johannes Berg4d339602011-12-15 11:24:20 +0100573 might_sleep();
574
Johannes Berg31104892015-10-22 17:35:19 +0200575 mutex_lock(&local->sta_mtx);
576
Guy Eilam8c71df72011-08-17 15:18:14 +0300577 err = sta_info_insert_check(sta);
578 if (err) {
Johannes Berg31104892015-10-22 17:35:19 +0200579 mutex_unlock(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700580 rcu_read_lock();
581 goto out_free;
582 }
583
Johannes Berg4d339602011-12-15 11:24:20 +0100584 err = sta_info_insert_finish(sta);
Guy Eilam8c71df72011-08-17 15:18:14 +0300585 if (err)
Johannes Berg004c8722008-02-20 11:21:35 +0100586 goto out_free;
Johannes Bergd0709a62008-02-25 16:27:46 +0100587
Johannes Berg004c8722008-02-20 11:21:35 +0100588 return 0;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200589 out_free:
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100590 sta_info_free(local, sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200591 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -0700592}
593
Johannes Berg34e89502010-02-03 13:59:58 +0100594int sta_info_insert(struct sta_info *sta)
595{
596 int err = sta_info_insert_rcu(sta);
597
598 rcu_read_unlock();
599
600 return err;
601}
602
Marco Porschd012a602012-10-10 12:39:50 -0700603static inline void __bss_tim_set(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100604{
605 /*
606 * This format has been mandated by the IEEE specifications,
607 * so this line may not be changed to use the __set_bit() format.
608 */
Marco Porschd012a602012-10-10 12:39:50 -0700609 tim[id / 8] |= (1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100610}
611
Marco Porschd012a602012-10-10 12:39:50 -0700612static inline void __bss_tim_clear(u8 *tim, u16 id)
Johannes Berg004c8722008-02-20 11:21:35 +0100613{
614 /*
615 * This format has been mandated by the IEEE specifications,
616 * so this line may not be changed to use the __clear_bit() format.
617 */
Marco Porschd012a602012-10-10 12:39:50 -0700618 tim[id / 8] &= ~(1 << (id % 8));
Johannes Berg004c8722008-02-20 11:21:35 +0100619}
620
Ilan Peer3d5839b2013-03-05 15:27:20 +0200621static inline bool __bss_tim_get(u8 *tim, u16 id)
622{
623 /*
624 * This format has been mandated by the IEEE specifications,
625 * so this line may not be changed to use the test_bit() format.
626 */
627 return tim[id / 8] & (1 << (id % 8));
628}
629
Johannes Berg948d8872011-09-29 16:04:29 +0200630static unsigned long ieee80211_tids_for_ac(int ac)
Johannes Berg004c8722008-02-20 11:21:35 +0100631{
Johannes Berg948d8872011-09-29 16:04:29 +0200632 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
633 switch (ac) {
634 case IEEE80211_AC_VO:
635 return BIT(6) | BIT(7);
636 case IEEE80211_AC_VI:
637 return BIT(4) | BIT(5);
638 case IEEE80211_AC_BE:
639 return BIT(0) | BIT(3);
640 case IEEE80211_AC_BK:
641 return BIT(1) | BIT(2);
642 default:
643 WARN_ON(1);
644 return 0;
Johannes Bergd0709a62008-02-25 16:27:46 +0100645 }
Johannes Berg004c8722008-02-20 11:21:35 +0100646}
647
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100648static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
Johannes Berg004c8722008-02-20 11:21:35 +0100649{
Johannes Bergc868cb352011-09-29 16:04:27 +0200650 struct ieee80211_local *local = sta->local;
Marco Porschd012a602012-10-10 12:39:50 -0700651 struct ps_data *ps;
Johannes Berg948d8872011-09-29 16:04:29 +0200652 bool indicate_tim = false;
653 u8 ignore_for_tim = sta->sta.uapsd_queues;
654 int ac;
Bob Copelanda69bd8e2015-07-14 08:31:58 -0400655 u16 id = sta->sta.aid;
Johannes Berg004c8722008-02-20 11:21:35 +0100656
Marco Porschd012a602012-10-10 12:39:50 -0700657 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
658 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
659 if (WARN_ON_ONCE(!sta->sdata->bss))
660 return;
661
662 ps = &sta->sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100663#ifdef CONFIG_MAC80211_MESH
664 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
665 ps = &sta->sdata->u.mesh.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100666#endif
Marco Porschd012a602012-10-10 12:39:50 -0700667 } else {
Johannes Bergc868cb352011-09-29 16:04:27 +0200668 return;
Marco Porschd012a602012-10-10 12:39:50 -0700669 }
Johannes Berg3e122be2008-07-09 14:40:34 +0200670
Johannes Bergc868cb352011-09-29 16:04:27 +0200671 /* No need to do anything if the driver does all */
Johannes Berg30686bf2015-06-02 21:39:54 +0200672 if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
Johannes Bergc868cb352011-09-29 16:04:27 +0200673 return;
Johannes Berg004c8722008-02-20 11:21:35 +0100674
Johannes Bergc868cb352011-09-29 16:04:27 +0200675 if (sta->dead)
676 goto done;
Johannes Berg3e122be2008-07-09 14:40:34 +0200677
Johannes Berg948d8872011-09-29 16:04:29 +0200678 /*
679 * If all ACs are delivery-enabled then we should build
680 * the TIM bit for all ACs anyway; if only some are then
681 * we ignore those and build the TIM bit using only the
682 * non-enabled ones.
683 */
684 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
685 ignore_for_tim = 0;
Johannes Berg3e122be2008-07-09 14:40:34 +0200686
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100687 if (ignore_pending)
688 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
689
Johannes Berg948d8872011-09-29 16:04:29 +0200690 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
691 unsigned long tids;
692
693 if (ignore_for_tim & BIT(ac))
694 continue;
695
696 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
697 !skb_queue_empty(&sta->ps_tx_buf[ac]);
698 if (indicate_tim)
699 break;
700
701 tids = ieee80211_tids_for_ac(ac);
702
703 indicate_tim |=
704 sta->driver_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100705 indicate_tim |=
706 sta->txq_buffered_tids & tids;
Johannes Bergd0709a62008-02-25 16:27:46 +0100707 }
Johannes Berg004c8722008-02-20 11:21:35 +0100708
Johannes Bergc868cb352011-09-29 16:04:27 +0200709 done:
Johannes Berg65f704a2013-02-13 17:39:53 +0100710 spin_lock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100711
Ilan Peer3d5839b2013-03-05 15:27:20 +0200712 if (indicate_tim == __bss_tim_get(ps->tim, id))
713 goto out_unlock;
714
Johannes Berg948d8872011-09-29 16:04:29 +0200715 if (indicate_tim)
Marco Porschd012a602012-10-10 12:39:50 -0700716 __bss_tim_set(ps->tim, id);
Johannes Bergc868cb352011-09-29 16:04:27 +0200717 else
Marco Porschd012a602012-10-10 12:39:50 -0700718 __bss_tim_clear(ps->tim, id);
Johannes Berg3e122be2008-07-09 14:40:34 +0200719
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100720 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
Johannes Bergc868cb352011-09-29 16:04:27 +0200721 local->tim_in_locked_section = true;
Johannes Berg948d8872011-09-29 16:04:29 +0200722 drv_set_tim(local, &sta->sta, indicate_tim);
Johannes Bergc868cb352011-09-29 16:04:27 +0200723 local->tim_in_locked_section = false;
Johannes Berg004c8722008-02-20 11:21:35 +0100724 }
Johannes Berg004c8722008-02-20 11:21:35 +0100725
Ilan Peer3d5839b2013-03-05 15:27:20 +0200726out_unlock:
Johannes Berg65f704a2013-02-13 17:39:53 +0100727 spin_unlock_bh(&local->tim_lock);
Johannes Berg004c8722008-02-20 11:21:35 +0100728}
729
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100730void sta_info_recalc_tim(struct sta_info *sta)
731{
732 __sta_info_recalc_tim(sta, false);
733}
734
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200735static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700736{
Johannes Berge039fa42008-05-15 12:55:29 +0200737 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700738 int timeout;
739
740 if (!skb)
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200741 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -0700742
Johannes Berge039fa42008-05-15 12:55:29 +0200743 info = IEEE80211_SKB_CB(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700744
745 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200746 timeout = (sta->listen_interval *
747 sta->sdata->vif.bss_conf.beacon_int *
748 32 / 15625) * HZ;
Jiri Bencf0706e82007-05-05 11:45:53 -0700749 if (timeout < STA_TX_BUFFER_EXPIRE)
750 timeout = STA_TX_BUFFER_EXPIRE;
Johannes Berge039fa42008-05-15 12:55:29 +0200751 return time_after(jiffies, info->control.jiffies + timeout);
Jiri Bencf0706e82007-05-05 11:45:53 -0700752}
753
754
Johannes Berg948d8872011-09-29 16:04:29 +0200755static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
756 struct sta_info *sta, int ac)
Jiri Bencf0706e82007-05-05 11:45:53 -0700757{
758 unsigned long flags;
759 struct sk_buff *skb;
760
Johannes Berg60750392011-09-29 16:04:28 +0200761 /*
762 * First check for frames that should expire on the filtered
763 * queue. Frames here were rejected by the driver and are on
764 * a separate queue to avoid reordering with normal PS-buffered
765 * frames. They also aren't accounted for right now in the
766 * total_ps_buffered counter.
767 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700768 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200769 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
770 skb = skb_peek(&sta->tx_filtered[ac]);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200771 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200772 skb = __skb_dequeue(&sta->tx_filtered[ac]);
Johannes Berg836341a2008-02-20 02:07:21 +0100773 else
Jiri Bencf0706e82007-05-05 11:45:53 -0700774 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200775 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700776
Johannes Berg60750392011-09-29 16:04:28 +0200777 /*
778 * Frames are queued in order, so if this one
779 * hasn't expired yet we can stop testing. If
780 * we actually reached the end of the queue we
781 * also need to stop, of course.
782 */
783 if (!skb)
784 break;
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200785 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg60750392011-09-29 16:04:28 +0200786 }
787
788 /*
789 * Now also check the normal PS-buffered queue, this will
790 * only find something if the filtered queue was emptied
791 * since the filtered frames are all before the normal PS
792 * buffered frames.
793 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700794 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200795 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
796 skb = skb_peek(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700797 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200798 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e82007-05-05 11:45:53 -0700799 else
800 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200801 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -0700802
Johannes Berg60750392011-09-29 16:04:28 +0200803 /*
804 * frames are queued in order, so if this one
805 * hasn't expired yet (or we reached the end of
806 * the queue) we can stop testing
807 */
Johannes Berg836341a2008-02-20 02:07:21 +0100808 if (!skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700809 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100810
Johannes Berg836341a2008-02-20 02:07:21 +0100811 local->total_ps_buffered--;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200812 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
813 sta->sta.addr);
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200814 ieee80211_free_txskb(&local->hw, skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700815 }
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300816
Johannes Berg60750392011-09-29 16:04:28 +0200817 /*
818 * Finally, recalculate the TIM bit for this station -- it might
819 * now be clear because the station was too slow to retrieve its
820 * frames.
821 */
822 sta_info_recalc_tim(sta);
823
824 /*
825 * Return whether there are any frames still buffered, this is
826 * used to check whether the cleanup timer still needs to run,
827 * if there are no frames we don't need to rearm the timer.
828 */
Johannes Berg948d8872011-09-29 16:04:29 +0200829 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
830 skb_queue_empty(&sta->tx_filtered[ac]));
831}
832
833static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
834 struct sta_info *sta)
835{
836 bool have_buffered = false;
837 int ac;
838
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100839 /* This is only necessary for stations on BSS/MBSS interfaces */
840 if (!sta->sdata->bss &&
841 !ieee80211_vif_is_mesh(&sta->sdata->vif))
Johannes Berg948d8872011-09-29 16:04:29 +0200842 return false;
843
844 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
845 have_buffered |=
846 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
847
848 return have_buffered;
Jiri Bencf0706e82007-05-05 11:45:53 -0700849}
850
Johannes Bergd7782072013-12-04 23:12:31 +0100851static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100852{
853 struct ieee80211_local *local;
854 struct ieee80211_sub_if_data *sdata;
Johannes Berg6d10e462013-03-06 23:09:11 +0100855 int ret;
Johannes Berg34e89502010-02-03 13:59:58 +0100856
857 might_sleep();
858
859 if (!sta)
860 return -ENOENT;
861
862 local = sta->local;
863 sdata = sta->sdata;
864
Johannes Berg83d5cc02012-01-12 09:31:10 +0100865 lockdep_assert_held(&local->sta_mtx);
866
Johannes Berg098a6072010-04-06 11:18:47 +0200867 /*
868 * Before removing the station from the driver and
869 * rate control, it might still start new aggregation
870 * sessions -- block that to make sure the tear-down
871 * will be sufficient.
872 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200873 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Bergc82c4a82012-07-18 13:31:31 +0200874 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
Johannes Berg098a6072010-04-06 11:18:47 +0200875
Johannes Berg34e89502010-02-03 13:59:58 +0100876 ret = sta_info_hash_del(local, sta);
Johannes Bergb01711b2013-12-04 20:25:27 +0100877 if (WARN_ON(ret))
Johannes Berg34e89502010-02-03 13:59:58 +0100878 return ret;
879
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200880 /*
881 * for TDLS peers, make sure to return to the base channel before
882 * removal.
883 */
884 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
885 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
886 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
887 }
888
Arik Nemtsov794454c2012-06-03 23:32:32 +0300889 list_del_rcu(&sta->list);
Eliad Pelleref044762015-11-17 10:24:37 +0200890 sta->removed = true;
Johannes Berg4d339602011-12-15 11:24:20 +0100891
Johannes Berg6a9d1b92013-12-04 22:39:17 +0100892 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
893
Johannes Berga710c812013-12-04 20:11:06 +0100894 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
895 rcu_access_pointer(sdata->u.vlan.sta) == sta)
896 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
897
Johannes Bergd7782072013-12-04 23:12:31 +0100898 return 0;
899}
900
901static void __sta_info_destroy_part2(struct sta_info *sta)
902{
903 struct ieee80211_local *local = sta->local;
904 struct ieee80211_sub_if_data *sdata = sta->sdata;
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100905 struct station_info *sinfo;
Johannes Bergd7782072013-12-04 23:12:31 +0100906 int ret;
907
908 /*
909 * NOTE: This assumes at least synchronize_net() was done
910 * after _part1 and before _part2!
911 */
912
913 might_sleep();
914 lockdep_assert_held(&local->sta_mtx);
915
Johannes Bergc8782072013-12-04 23:05:45 +0100916 /* now keys can no longer be reached */
Johannes Berg6d10e462013-03-06 23:09:11 +0100917 ieee80211_free_sta_keys(local, sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100918
Johannes Berg9b7a86f2015-01-09 11:40:39 +0100919 /* disable TIM bit - last chance to tell driver */
920 __sta_info_recalc_tim(sta, true);
921
Johannes Berg34e89502010-02-03 13:59:58 +0100922 sta->dead = true;
923
Johannes Berg34e89502010-02-03 13:59:58 +0100924 local->num_sta--;
925 local->sta_generation++;
926
Johannes Berg83d5cc02012-01-12 09:31:10 +0100927 while (sta->sta_state > IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100928 ret = sta_info_move_state(sta, sta->sta_state - 1);
929 if (ret) {
Johannes Berg83d5cc02012-01-12 09:31:10 +0100930 WARN_ON_ONCE(1);
931 break;
932 }
933 }
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100934
Johannes Bergf09603a2012-01-20 13:55:21 +0100935 if (sta->uploaded) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100936 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
937 IEEE80211_STA_NOTEXIST);
938 WARN_ON_ONCE(ret != 0);
939 }
Johannes Berg34e89502010-02-03 13:59:58 +0100940
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200941 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
942
Arnd Bergmann0ef049d2016-01-26 23:05:31 +0100943 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
944 if (sinfo)
945 sta_set_sinfo(sta, sinfo);
946 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
947 kfree(sinfo);
Jouni Malinenec15e682011-03-23 15:29:52 +0200948
Johannes Berg34e89502010-02-03 13:59:58 +0100949 rate_control_remove_sta_debugfs(sta);
950 ieee80211_sta_debugfs_remove(sta);
951
Johannes Bergd34ba212013-12-04 22:46:11 +0100952 cleanup_single_sta(sta);
Johannes Bergd7782072013-12-04 23:12:31 +0100953}
954
955int __must_check __sta_info_destroy(struct sta_info *sta)
956{
957 int err = __sta_info_destroy_part1(sta);
958
959 if (err)
960 return err;
961
962 synchronize_net();
963
964 __sta_info_destroy_part2(sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100965
966 return 0;
967}
968
969int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
970{
971 struct sta_info *sta;
972 int ret;
973
974 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100975 sta = sta_info_get(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100976 ret = __sta_info_destroy(sta);
977 mutex_unlock(&sdata->local->sta_mtx);
978
979 return ret;
980}
981
982int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
983 const u8 *addr)
984{
985 struct sta_info *sta;
986 int ret;
987
988 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100989 sta = sta_info_get_bss(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100990 ret = __sta_info_destroy(sta);
991 mutex_unlock(&sdata->local->sta_mtx);
992
993 return ret;
994}
Jiri Bencf0706e82007-05-05 11:45:53 -0700995
996static void sta_info_cleanup(unsigned long data)
997{
998 struct ieee80211_local *local = (struct ieee80211_local *) data;
999 struct sta_info *sta;
Juuso Oikarinen3393a602010-04-19 10:12:52 +03001000 bool timer_needed = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001001
Johannes Bergd0709a62008-02-25 16:27:46 +01001002 rcu_read_lock();
1003 list_for_each_entry_rcu(sta, &local->sta_list, list)
Juuso Oikarinen3393a602010-04-19 10:12:52 +03001004 if (sta_info_cleanup_expire_buffered(local, sta))
1005 timer_needed = true;
Johannes Bergd0709a62008-02-25 16:27:46 +01001006 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001007
Johannes Berg5bb644a2009-05-17 11:40:42 +02001008 if (local->quiescing)
1009 return;
1010
Juuso Oikarinen3393a602010-04-19 10:12:52 +03001011 if (!timer_needed)
1012 return;
1013
Johannes Berg26d59532011-04-01 13:52:48 +02001014 mod_timer(&local->sta_cleanup,
1015 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
Jiri Bencf0706e82007-05-05 11:45:53 -07001016}
1017
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001018u32 sta_addr_hash(const void *key, u32 length, u32 seed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001019{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001020 return jhash(key, ETH_ALEN, seed);
1021}
1022
1023int sta_info_init(struct ieee80211_local *local)
1024{
1025 int err;
1026
1027 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
1028 if (err)
1029 return err;
1030
Johannes Berg4d339602011-12-15 11:24:20 +01001031 spin_lock_init(&local->tim_lock);
Johannes Berg34e89502010-02-03 13:59:58 +01001032 mutex_init(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07001033 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001034
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001035 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1036 (unsigned long)local);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001037 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001038}
1039
1040void sta_info_stop(struct ieee80211_local *local)
1041{
Johannes Berga56f9922012-12-13 23:08:52 +01001042 del_timer_sync(&local->sta_cleanup);
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001043 rhashtable_destroy(&local->sta_hash);
Jiri Bencf0706e82007-05-05 11:45:53 -07001044}
1045
Johannes Berg051007d2012-12-13 23:49:02 +01001046
Johannes Berge7162512013-12-04 23:18:37 +01001047int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
Jiri Bencf0706e82007-05-05 11:45:53 -07001048{
Johannes Bergb998e8b2012-12-13 23:07:46 +01001049 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001050 struct sta_info *sta, *tmp;
Johannes Bergd7782072013-12-04 23:12:31 +01001051 LIST_HEAD(free_list);
Johannes Berg44213b52008-02-25 16:27:49 +01001052 int ret = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001053
Johannes Bergd0709a62008-02-25 16:27:46 +01001054 might_sleep();
1055
Johannes Berge7162512013-12-04 23:18:37 +01001056 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1057 WARN_ON(vlans && !sdata->bss);
1058
Johannes Berg34e89502010-02-03 13:59:58 +01001059 mutex_lock(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +01001060 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Johannes Berge7162512013-12-04 23:18:37 +01001061 if (sdata == sta->sdata ||
1062 (vlans && sdata->bss == sta->sdata->bss)) {
Johannes Bergd7782072013-12-04 23:12:31 +01001063 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1064 list_add(&sta->free_list, &free_list);
Johannes Berg34316832012-02-25 21:40:46 +01001065 ret++;
1066 }
Johannes Berg34e89502010-02-03 13:59:58 +01001067 }
Johannes Bergd7782072013-12-04 23:12:31 +01001068
1069 if (!list_empty(&free_list)) {
1070 synchronize_net();
1071 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1072 __sta_info_destroy_part2(sta);
1073 }
Johannes Berg34e89502010-02-03 13:59:58 +01001074 mutex_unlock(&local->sta_mtx);
Johannes Berg44213b52008-02-25 16:27:49 +01001075
Johannes Berg051007d2012-12-13 23:49:02 +01001076 return ret;
1077}
1078
Johannes Berg24723d12008-09-11 00:01:46 +02001079void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1080 unsigned long exp_time)
1081{
1082 struct ieee80211_local *local = sdata->local;
1083 struct sta_info *sta, *tmp;
Johannes Berg24723d12008-09-11 00:01:46 +02001084
Johannes Berg34e89502010-02-03 13:59:58 +01001085 mutex_lock(&local->sta_mtx);
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301086
1087 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Marek Lindnerec2b7742011-12-20 23:16:52 +08001088 if (sdata != sta->sdata)
1089 continue;
1090
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001091 if (time_after(jiffies, sta->rx_stats.last_rx + exp_time)) {
Mohammed Shafi Shajakhaneea57d42012-10-08 21:33:47 +05301092 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1093 sta->sta.addr);
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001094
1095 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1096 test_sta_flag(sta, WLAN_STA_PS_STA))
1097 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1098
Johannes Berg34e89502010-02-03 13:59:58 +01001099 WARN_ON(__sta_info_destroy(sta));
Johannes Berg24723d12008-09-11 00:01:46 +02001100 }
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +05301101 }
1102
Johannes Berg34e89502010-02-03 13:59:58 +01001103 mutex_unlock(&local->sta_mtx);
Johannes Berg24723d12008-09-11 00:01:46 +02001104}
Johannes Berg17741cd2008-09-11 00:02:02 +02001105
Ben Greear686b9cb2010-09-23 09:44:36 -07001106struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001107 const u8 *addr,
1108 const u8 *localaddr)
Johannes Berg17741cd2008-09-11 00:02:02 +02001109{
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001110 struct ieee80211_local *local = hw_to_local(hw);
1111 struct sta_info *sta;
1112 struct rhash_head *tmp;
1113 const struct bucket_table *tbl;
1114
1115 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
Johannes Berg17741cd2008-09-11 00:02:02 +02001116
Ben Greear686b9cb2010-09-23 09:44:36 -07001117 /*
1118 * Just return a random station if localaddr is NULL
1119 * ... first in list.
1120 */
Johannes Berg7bedd0c2015-02-13 21:55:15 +01001121 for_each_sta_info(local, tbl, addr, sta, tmp) {
Ben Greear686b9cb2010-09-23 09:44:36 -07001122 if (localaddr &&
Joe Perchesb203ca32012-05-08 18:56:52 +00001123 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
Ben Greear686b9cb2010-09-23 09:44:36 -07001124 continue;
Johannes Bergf7c65592010-04-30 13:48:36 +02001125 if (!sta->uploaded)
1126 return NULL;
Johannes Bergabe60632009-11-25 17:46:18 +01001127 return &sta->sta;
Johannes Bergf7c65592010-04-30 13:48:36 +02001128 }
1129
Johannes Bergabe60632009-11-25 17:46:18 +01001130 return NULL;
Johannes Berg17741cd2008-09-11 00:02:02 +02001131}
Ben Greear686b9cb2010-09-23 09:44:36 -07001132EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
Johannes Berg5ed176e2009-11-04 14:42:28 +01001133
1134struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1135 const u8 *addr)
1136{
Johannes Bergf7c65592010-04-30 13:48:36 +02001137 struct sta_info *sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001138
1139 if (!vif)
1140 return NULL;
1141
Johannes Bergf7c65592010-04-30 13:48:36 +02001142 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1143 if (!sta)
1144 return NULL;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001145
Johannes Bergf7c65592010-04-30 13:48:36 +02001146 if (!sta->uploaded)
1147 return NULL;
1148
1149 return &sta->sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +01001150}
Johannes Berg17741cd2008-09-11 00:02:02 +02001151EXPORT_SYMBOL(ieee80211_find_sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001152
Johannes Berge3685e02014-02-20 11:19:58 +01001153/* powersave support code */
1154void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
Johannes Berg50a94322010-11-16 11:50:28 -08001155{
Helmut Schaa608383b2012-01-30 15:18:00 +01001156 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berge3685e02014-02-20 11:19:58 +01001157 struct ieee80211_local *local = sdata->local;
1158 struct sk_buff_head pending;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001159 int filtered = 0, buffered = 0, ac, i;
Johannes Berge3685e02014-02-20 11:19:58 +01001160 unsigned long flags;
Marco Porschd012a602012-10-10 12:39:50 -07001161 struct ps_data *ps;
1162
Felix Fietkau3918edb2014-07-25 16:20:23 +02001163 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1164 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1165 u.ap);
1166
1167 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porschd012a602012-10-10 12:39:50 -07001168 ps = &sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001169 else if (ieee80211_vif_is_mesh(&sdata->vif))
1170 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07001171 else
1172 return;
Johannes Berg50a94322010-11-16 11:50:28 -08001173
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001174 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg47086fc2011-09-29 16:04:33 +02001175
Johannes Berg5a306f52012-11-14 23:22:21 +01001176 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
Johannes Berg948d8872011-09-29 16:04:29 +02001177 sta->driver_buffered_tids = 0;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001178 sta->txq_buffered_tids = 0;
Johannes Berg948d8872011-09-29 16:04:29 +02001179
Johannes Berg30686bf2015-06-02 21:39:54 +02001180 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
Arik Nemtsovd057e5a2011-01-31 22:29:13 +02001181 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001182
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001183 if (sta->sta.txq[0]) {
1184 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1185 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1186
1187 if (!skb_queue_len(&txqi->queue))
1188 continue;
1189
1190 drv_wake_tx_queue(local, txqi);
1191 }
1192 }
1193
Johannes Berg948d8872011-09-29 16:04:29 +02001194 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001195
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001196 /* sync with ieee80211_tx_h_unicast_ps_buf */
1197 spin_lock(&sta->ps_lock);
Johannes Bergaf818582009-11-06 11:35:50 +01001198 /* Send all buffered frames to the station */
Johannes Berg948d8872011-09-29 16:04:29 +02001199 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1200 int count = skb_queue_len(&pending), tmp;
1201
Arik Nemtsov987c2852012-11-05 10:27:52 +02001202 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001203 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001204 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001205 tmp = skb_queue_len(&pending);
1206 filtered += tmp - count;
1207 count = tmp;
1208
Arik Nemtsov987c2852012-11-05 10:27:52 +02001209 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001210 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
Arik Nemtsov987c2852012-11-05 10:27:52 +02001211 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +02001212 tmp = skb_queue_len(&pending);
1213 buffered += tmp - count;
1214 }
1215
Johannes Berge3685e02014-02-20 11:19:58 +01001216 ieee80211_add_pending_skbs(local, &pending);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001217
1218 /* now we're no longer in the deliver code */
1219 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1220
1221 /* The station might have polled and then woken up before we responded,
1222 * so clear these flags now to avoid them sticking around.
1223 */
1224 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1225 clear_sta_flag(sta, WLAN_STA_UAPSD);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +02001226 spin_unlock(&sta->ps_lock);
Johannes Berg948d8872011-09-29 16:04:29 +02001227
Johannes Berge3685e02014-02-20 11:19:58 +01001228 atomic_dec(&ps->num_sta_ps);
1229
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001230 /* This station just woke up and isn't aware of our SMPS state */
Chun-Yeow Yeoh062f1d62014-04-22 18:19:25 +08001231 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1232 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001233 sdata->smps_mode) &&
1234 sta->known_smps_mode != sdata->bss->req_smps &&
1235 sta_info_tx_streams(sta) != 1) {
1236 ht_dbg(sdata,
1237 "%pM just woke up and MIMO capable - update SMPS\n",
1238 sta->sta.addr);
1239 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1240 sta->sta.addr,
1241 sdata->vif.bss_conf.bssid);
1242 }
1243
Johannes Bergaf818582009-11-06 11:35:50 +01001244 local->total_ps_buffered -= buffered;
1245
Johannes Bergc868cb352011-09-29 16:04:27 +02001246 sta_info_recalc_tim(sta);
1247
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001248 ps_dbg(sdata,
1249 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1250 sta->sta.addr, sta->sta.aid, filtered, buffered);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001251
1252 ieee80211_check_fast_xmit(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001253}
1254
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001255static void ieee80211_send_null_response(struct sta_info *sta, int tid,
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001256 enum ieee80211_frame_release_type reason,
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001257 bool call_driver, bool more_data)
Johannes Bergce662b442011-09-29 16:04:34 +02001258{
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001259 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Bergce662b442011-09-29 16:04:34 +02001260 struct ieee80211_local *local = sdata->local;
1261 struct ieee80211_qos_hdr *nullfunc;
1262 struct sk_buff *skb;
1263 int size = sizeof(*nullfunc);
1264 __le16 fc;
Johannes Berga74a8c82014-07-22 14:50:47 +02001265 bool qos = sta->sta.wme;
Johannes Bergce662b442011-09-29 16:04:34 +02001266 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02001267 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergce662b442011-09-29 16:04:34 +02001268
1269 if (qos) {
1270 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1271 IEEE80211_STYPE_QOS_NULLFUNC |
1272 IEEE80211_FCTL_FROMDS);
1273 } else {
1274 size -= 2;
1275 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1276 IEEE80211_STYPE_NULLFUNC |
1277 IEEE80211_FCTL_FROMDS);
1278 }
1279
1280 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1281 if (!skb)
1282 return;
1283
1284 skb_reserve(skb, local->hw.extra_tx_headroom);
1285
1286 nullfunc = (void *) skb_put(skb, size);
1287 nullfunc->frame_control = fc;
1288 nullfunc->duration_id = 0;
1289 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1290 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1291 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
Johannes Berg864a6042014-03-04 13:46:53 +01001292 nullfunc->seq_ctrl = 0;
Johannes Bergce662b442011-09-29 16:04:34 +02001293
Johannes Berg59b66252011-10-13 13:19:19 +02001294 skb->priority = tid;
1295 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
Johannes Bergce662b442011-09-29 16:04:34 +02001296 if (qos) {
Johannes Bergce662b442011-09-29 16:04:34 +02001297 nullfunc->qos_ctrl = cpu_to_le16(tid);
1298
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001299 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
Johannes Bergce662b442011-09-29 16:04:34 +02001300 nullfunc->qos_ctrl |=
1301 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001302 if (more_data)
1303 nullfunc->frame_control |=
1304 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1305 }
Johannes Bergce662b442011-09-29 16:04:34 +02001306 }
1307
1308 info = IEEE80211_SKB_CB(skb);
1309
1310 /*
1311 * Tell TX path to send this frame even though the
1312 * STA may still remain is PS mode after this frame
Johannes Bergdeeaee192011-09-29 16:04:35 +02001313 * exchange. Also set EOSP to indicate this packet
1314 * ends the poll/service period.
Johannes Bergce662b442011-09-29 16:04:34 +02001315 */
Johannes Berg02f2f1a2012-02-27 12:18:30 +01001316 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
Johannes Bergdeeaee192011-09-29 16:04:35 +02001317 IEEE80211_TX_STATUS_EOSP |
1318 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergce662b442011-09-29 16:04:34 +02001319
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301320 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1321
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001322 if (call_driver)
1323 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1324 reason, false);
Johannes Berg40b96402011-09-29 16:04:38 +02001325
Johannes Berg89afe612013-02-13 15:39:57 +01001326 skb->dev = sdata->dev;
1327
Johannes Berg55de9082012-07-26 17:24:39 +02001328 rcu_read_lock();
1329 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1330 if (WARN_ON(!chanctx_conf)) {
1331 rcu_read_unlock();
1332 kfree_skb(skb);
1333 return;
1334 }
1335
Johannes Berg73c4e192014-11-09 18:50:09 +02001336 info->band = chanctx_conf->def.chan->band;
Johannes Berg7c107702015-03-20 14:18:27 +01001337 ieee80211_xmit(sdata, sta, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001338 rcu_read_unlock();
Johannes Bergce662b442011-09-29 16:04:34 +02001339}
1340
Johannes Berg0a1cb802014-01-09 11:05:31 +01001341static int find_highest_prio_tid(unsigned long tids)
1342{
1343 /* lower 3 TIDs aren't ordered perfectly */
1344 if (tids & 0xF8)
1345 return fls(tids) - 1;
1346 /* TID 0 is BE just like TID 3 */
1347 if (tids & BIT(0))
1348 return 0;
1349 return fls(tids) - 1;
1350}
1351
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001352/* Indicates if the MORE_DATA bit should be set in the last
1353 * frame obtained by ieee80211_sta_ps_get_frames.
1354 * Note that driver_release_tids is relevant only if
1355 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1356 */
1357static bool
1358ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1359 enum ieee80211_frame_release_type reason,
1360 unsigned long driver_release_tids)
1361{
1362 int ac;
1363
1364 /* If the driver has data on more than one TID then
1365 * certainly there's more data if we release just a
1366 * single frame now (from a single TID). This will
1367 * only happen for PS-Poll.
1368 */
1369 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1370 hweight16(driver_release_tids) > 1)
1371 return true;
1372
1373 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1374 if (ignored_acs & BIT(ac))
1375 continue;
1376
1377 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1378 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1379 return true;
1380 }
1381
1382 return false;
1383}
1384
Johannes Berg47086fc2011-09-29 16:04:33 +02001385static void
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001386ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1387 enum ieee80211_frame_release_type reason,
1388 struct sk_buff_head *frames,
1389 unsigned long *driver_release_tids)
Johannes Bergaf818582009-11-06 11:35:50 +01001390{
1391 struct ieee80211_sub_if_data *sdata = sta->sdata;
1392 struct ieee80211_local *local = sdata->local;
Johannes Berg948d8872011-09-29 16:04:29 +02001393 int ac;
Johannes Bergaf818582009-11-06 11:35:50 +01001394
Johannes Bergf9f760b2014-01-08 17:45:07 +01001395 /* Get response frame(s) and more data bit for the last one. */
Johannes Berg948d8872011-09-29 16:04:29 +02001396 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg4049e092011-09-29 16:04:32 +02001397 unsigned long tids;
1398
Johannes Berg47086fc2011-09-29 16:04:33 +02001399 if (ignored_acs & BIT(ac))
Johannes Berg948d8872011-09-29 16:04:29 +02001400 continue;
1401
Johannes Berg4049e092011-09-29 16:04:32 +02001402 tids = ieee80211_tids_for_ac(ac);
1403
Johannes Bergf9f760b2014-01-08 17:45:07 +01001404 /* if we already have frames from software, then we can't also
1405 * release from hardware queues
1406 */
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001407 if (skb_queue_empty(frames)) {
1408 *driver_release_tids |=
1409 sta->driver_buffered_tids & tids;
1410 *driver_release_tids |= sta->txq_buffered_tids & tids;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001411 }
Johannes Berg47086fc2011-09-29 16:04:33 +02001412
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001413 if (!*driver_release_tids) {
Johannes Bergf9f760b2014-01-08 17:45:07 +01001414 struct sk_buff *skb;
1415
1416 while (n_frames > 0) {
1417 skb = skb_dequeue(&sta->tx_filtered[ac]);
1418 if (!skb) {
1419 skb = skb_dequeue(
1420 &sta->ps_tx_buf[ac]);
1421 if (skb)
1422 local->total_ps_buffered--;
1423 }
1424 if (!skb)
1425 break;
1426 n_frames--;
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001427 __skb_queue_tail(frames, skb);
Johannes Bergf9f760b2014-01-08 17:45:07 +01001428 }
Johannes Berg948d8872011-09-29 16:04:29 +02001429 }
1430
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001431 /* If we have more frames buffered on this AC, then abort the
1432 * loop since we can't send more data from other ACs before
1433 * the buffered frames from this.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001434 */
Johannes Berg948d8872011-09-29 16:04:29 +02001435 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001436 !skb_queue_empty(&sta->ps_tx_buf[ac]))
Johannes Berg948d8872011-09-29 16:04:29 +02001437 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001438 }
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001439}
1440
1441static void
1442ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1443 int n_frames, u8 ignored_acs,
1444 enum ieee80211_frame_release_type reason)
1445{
1446 struct ieee80211_sub_if_data *sdata = sta->sdata;
1447 struct ieee80211_local *local = sdata->local;
1448 unsigned long driver_release_tids = 0;
1449 struct sk_buff_head frames;
1450 bool more_data;
1451
1452 /* Service or PS-Poll period starts */
1453 set_sta_flag(sta, WLAN_STA_SP);
1454
1455 __skb_queue_head_init(&frames);
1456
1457 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1458 &frames, &driver_release_tids);
1459
1460 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1461
1462 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1463 driver_release_tids =
1464 BIT(find_highest_prio_tid(driver_release_tids));
Johannes Bergaf818582009-11-06 11:35:50 +01001465
Johannes Bergf9f760b2014-01-08 17:45:07 +01001466 if (skb_queue_empty(&frames) && !driver_release_tids) {
Johannes Bergce662b442011-09-29 16:04:34 +02001467 int tid;
Johannes Berg4049e092011-09-29 16:04:32 +02001468
Johannes Bergce662b442011-09-29 16:04:34 +02001469 /*
1470 * For PS-Poll, this can only happen due to a race condition
1471 * when we set the TIM bit and the station notices it, but
1472 * before it can poll for the frame we expire it.
1473 *
1474 * For uAPSD, this is said in the standard (11.2.1.5 h):
1475 * At each unscheduled SP for a non-AP STA, the AP shall
1476 * attempt to transmit at least one MSDU or MMPDU, but no
1477 * more than the value specified in the Max SP Length field
1478 * in the QoS Capability element from delivery-enabled ACs,
1479 * that are destined for the non-AP STA.
1480 *
1481 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1482 */
1483
1484 /* This will evaluate to 1, 3, 5 or 7. */
1485 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1486
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001487 ieee80211_send_null_response(sta, tid, reason, true, false);
Johannes Bergf9f760b2014-01-08 17:45:07 +01001488 } else if (!driver_release_tids) {
Johannes Berg47086fc2011-09-29 16:04:33 +02001489 struct sk_buff_head pending;
1490 struct sk_buff *skb;
Johannes Berg40b96402011-09-29 16:04:38 +02001491 int num = 0;
1492 u16 tids = 0;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001493 bool need_null = false;
Johannes Bergaf818582009-11-06 11:35:50 +01001494
Johannes Berg47086fc2011-09-29 16:04:33 +02001495 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001496
Johannes Berg47086fc2011-09-29 16:04:33 +02001497 while ((skb = __skb_dequeue(&frames))) {
1498 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1499 struct ieee80211_hdr *hdr = (void *) skb->data;
Johannes Berg40b96402011-09-29 16:04:38 +02001500 u8 *qoshdr = NULL;
1501
1502 num++;
Johannes Bergaf818582009-11-06 11:35:50 +01001503
Johannes Berg47086fc2011-09-29 16:04:33 +02001504 /*
1505 * Tell TX path to send this frame even though the
1506 * STA may still remain is PS mode after this frame
1507 * exchange.
1508 */
Sujith Manoharan6b127c72014-12-10 21:26:10 +05301509 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1510 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
Johannes Bergaf818582009-11-06 11:35:50 +01001511
Johannes Berg47086fc2011-09-29 16:04:33 +02001512 /*
1513 * Use MoreData flag to indicate whether there are
1514 * more buffered frames for this STA
1515 */
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001516 if (more_data || !skb_queue_empty(&frames))
Johannes Berg47086fc2011-09-29 16:04:33 +02001517 hdr->frame_control |=
1518 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001519 else
1520 hdr->frame_control &=
1521 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
Johannes Berg47086fc2011-09-29 16:04:33 +02001522
Johannes Berg40b96402011-09-29 16:04:38 +02001523 if (ieee80211_is_data_qos(hdr->frame_control) ||
1524 ieee80211_is_qos_nullfunc(hdr->frame_control))
1525 qoshdr = ieee80211_get_qos_ctl(hdr);
1526
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001527 tids |= BIT(skb->priority);
1528
1529 __skb_queue_tail(&pending, skb);
1530
1531 /* end service period after last frame or add one */
1532 if (!skb_queue_empty(&frames))
1533 continue;
1534
1535 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1536 /* for PS-Poll, there's only one frame */
1537 info->flags |= IEEE80211_TX_STATUS_EOSP |
1538 IEEE80211_TX_CTL_REQ_TX_STATUS;
1539 break;
1540 }
1541
1542 /* For uAPSD, things are a bit more complicated. If the
1543 * last frame has a QoS header (i.e. is a QoS-data or
1544 * QoS-nulldata frame) then just set the EOSP bit there
1545 * and be done.
1546 * If the frame doesn't have a QoS header (which means
1547 * it should be a bufferable MMPDU) then we can't set
1548 * the EOSP bit in the QoS header; add a QoS-nulldata
1549 * frame to the list to send it after the MMPDU.
1550 *
1551 * Note that this code is only in the mac80211-release
1552 * code path, we assume that the driver will not buffer
1553 * anything but QoS-data frames, or if it does, will
1554 * create the QoS-nulldata frame by itself if needed.
1555 *
1556 * Cf. 802.11-2012 10.2.1.10 (c).
1557 */
1558 if (qoshdr) {
1559 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
Johannes Berg47086fc2011-09-29 16:04:33 +02001560
Marco Porsch52a3f202012-03-16 15:30:26 +01001561 info->flags |= IEEE80211_TX_STATUS_EOSP |
1562 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001563 } else {
1564 /* The standard isn't completely clear on this
1565 * as it says the more-data bit should be set
1566 * if there are more BUs. The QoS-Null frame
1567 * we're about to send isn't buffered yet, we
1568 * only create it below, but let's pretend it
1569 * was buffered just in case some clients only
1570 * expect more-data=0 when eosp=1.
1571 */
1572 hdr->frame_control |=
1573 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1574 need_null = true;
1575 num++;
Marco Porsch52a3f202012-03-16 15:30:26 +01001576 }
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001577 break;
Johannes Berg47086fc2011-09-29 16:04:33 +02001578 }
1579
Johannes Berg40b96402011-09-29 16:04:38 +02001580 drv_allow_buffered_frames(local, sta, tids, num,
1581 reason, more_data);
1582
Johannes Berg47086fc2011-09-29 16:04:33 +02001583 ieee80211_add_pending_skbs(local, &pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001584
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001585 if (need_null)
1586 ieee80211_send_null_response(
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001587 sta, find_highest_prio_tid(tids),
1588 reason, false, false);
Johannes Bergb77cf4f2014-01-09 00:00:38 +01001589
Johannes Bergc868cb352011-09-29 16:04:27 +02001590 sta_info_recalc_tim(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001591 } else {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001592 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1593 int tid;
1594
Johannes Bergaf818582009-11-06 11:35:50 +01001595 /*
Johannes Berg4049e092011-09-29 16:04:32 +02001596 * We need to release a frame that is buffered somewhere in the
1597 * driver ... it'll have to handle that.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001598 * Note that the driver also has to check the number of frames
1599 * on the TIDs we're releasing from - if there are more than
1600 * n_frames it has to set the more-data bit (if we didn't ask
1601 * it to set it anyway due to other buffered frames); if there
1602 * are fewer than n_frames it has to make sure to adjust that
1603 * to allow the service period to end properly.
Johannes Bergaf818582009-11-06 11:35:50 +01001604 */
Johannes Berg4049e092011-09-29 16:04:32 +02001605 drv_release_buffered_frames(local, sta, driver_release_tids,
Johannes Berg47086fc2011-09-29 16:04:33 +02001606 n_frames, reason, more_data);
Johannes Berg4049e092011-09-29 16:04:32 +02001607
1608 /*
1609 * Note that we don't recalculate the TIM bit here as it would
1610 * most likely have no effect at all unless the driver told us
Johannes Bergf9f760b2014-01-08 17:45:07 +01001611 * that the TID(s) became empty before returning here from the
Johannes Berg4049e092011-09-29 16:04:32 +02001612 * release function.
Johannes Bergf9f760b2014-01-08 17:45:07 +01001613 * Either way, however, when the driver tells us that the TID(s)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001614 * became empty or we find that a txq became empty, we'll do the
1615 * TIM recalculation.
Johannes Berg4049e092011-09-29 16:04:32 +02001616 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001617
1618 if (!sta->sta.txq[0])
1619 return;
1620
1621 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1622 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1623
1624 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1625 continue;
1626
1627 sta_info_recalc_tim(sta);
1628 break;
1629 }
Johannes Bergaf818582009-11-06 11:35:50 +01001630 }
1631}
1632
Johannes Bergaf818582009-11-06 11:35:50 +01001633void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1634{
Johannes Berg47086fc2011-09-29 16:04:33 +02001635 u8 ignore_for_response = sta->sta.uapsd_queues;
Johannes Bergaf818582009-11-06 11:35:50 +01001636
Johannes Berg47086fc2011-09-29 16:04:33 +02001637 /*
1638 * If all ACs are delivery-enabled then we should reply
1639 * from any of them, if only some are enabled we reply
1640 * only from the non-enabled ones.
1641 */
1642 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1643 ignore_for_response = 0;
1644
1645 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1646 IEEE80211_FRAME_RELEASE_PSPOLL);
1647}
1648
1649void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1650{
1651 int n_frames = sta->sta.max_sp;
1652 u8 delivery_enabled = sta->sta.uapsd_queues;
1653
1654 /*
1655 * If we ever grow support for TSPEC this might happen if
1656 * the TSPEC update from hostapd comes in between a trigger
1657 * frame setting WLAN_STA_UAPSD in the RX path and this
1658 * actually getting called.
1659 */
1660 if (!delivery_enabled)
1661 return;
1662
Johannes Berg47086fc2011-09-29 16:04:33 +02001663 switch (sta->sta.max_sp) {
1664 case 1:
1665 n_frames = 2;
1666 break;
1667 case 2:
1668 n_frames = 4;
1669 break;
1670 case 3:
1671 n_frames = 6;
1672 break;
1673 case 0:
1674 /* XXX: what is a good value? */
Andrei Otcheretianski13a80982014-11-04 11:33:04 +02001675 n_frames = 128;
Johannes Berg47086fc2011-09-29 16:04:33 +02001676 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001677 }
Johannes Bergaf818582009-11-06 11:35:50 +01001678
Johannes Berg47086fc2011-09-29 16:04:33 +02001679 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1680 IEEE80211_FRAME_RELEASE_UAPSD);
Johannes Bergaf818582009-11-06 11:35:50 +01001681}
1682
1683void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1684 struct ieee80211_sta *pubsta, bool block)
1685{
1686 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1687
Johannes Bergb5878a22010-04-07 16:48:40 +02001688 trace_api_sta_block_awake(sta->local, pubsta, block);
1689
Johannes Berg5ac2e352014-05-27 16:32:27 +02001690 if (block) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001691 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001692 ieee80211_clear_fast_xmit(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001693 return;
1694 }
1695
1696 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1697 return;
1698
1699 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1700 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1701 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1702 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1703 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1704 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1705 /* must be asleep in this case */
1706 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1707 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1708 } else {
1709 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001710 ieee80211_check_fast_xmit(sta);
Johannes Berg5ac2e352014-05-27 16:32:27 +02001711 }
Johannes Bergaf818582009-11-06 11:35:50 +01001712}
1713EXPORT_SYMBOL(ieee80211_sta_block_awake);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001714
Johannes Berge9437892013-02-15 21:38:08 +01001715void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
Johannes Berg37fbd902011-09-29 16:04:39 +02001716{
1717 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1718 struct ieee80211_local *local = sta->local;
Johannes Berg37fbd902011-09-29 16:04:39 +02001719
1720 trace_api_eosp(local, pubsta);
1721
Johannes Berge9437892013-02-15 21:38:08 +01001722 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg37fbd902011-09-29 16:04:39 +02001723}
Johannes Berge9437892013-02-15 21:38:08 +01001724EXPORT_SYMBOL(ieee80211_sta_eosp);
Johannes Berg37fbd902011-09-29 16:04:39 +02001725
Emmanuel Grumbach0ead2512015-11-17 10:24:36 +02001726void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1727{
1728 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1729 enum ieee80211_frame_release_type reason;
1730 bool more_data;
1731
1732 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1733
1734 reason = IEEE80211_FRAME_RELEASE_UAPSD;
1735 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1736 reason, 0);
1737
1738 ieee80211_send_null_response(sta, tid, reason, false, more_data);
1739}
1740EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1741
Johannes Berg042ec452011-09-29 16:04:26 +02001742void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1743 u8 tid, bool buffered)
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001744{
1745 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1746
Johannes Berg5a306f52012-11-14 23:22:21 +01001747 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
Johannes Berg042ec452011-09-29 16:04:26 +02001748 return;
1749
Johannes Berg1b000782013-12-19 10:47:48 +01001750 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1751
Johannes Berg948d8872011-09-29 16:04:29 +02001752 if (buffered)
1753 set_bit(tid, &sta->driver_buffered_tids);
1754 else
1755 clear_bit(tid, &sta->driver_buffered_tids);
1756
Johannes Bergc868cb352011-09-29 16:04:27 +02001757 sta_info_recalc_tim(sta);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001758}
Johannes Berg042ec452011-09-29 16:04:26 +02001759EXPORT_SYMBOL(ieee80211_sta_set_buffered);
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001760
Johannes Berg83d5cc02012-01-12 09:31:10 +01001761int sta_info_move_state(struct sta_info *sta,
1762 enum ieee80211_sta_state new_state)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001763{
Johannes Berg8bf11d82011-12-15 11:17:37 +01001764 might_sleep();
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001765
1766 if (sta->sta_state == new_state)
1767 return 0;
1768
Johannes Bergf09603a2012-01-20 13:55:21 +01001769 /* check allowed transitions first */
1770
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001771 switch (new_state) {
1772 case IEEE80211_STA_NONE:
Johannes Bergf09603a2012-01-20 13:55:21 +01001773 if (sta->sta_state != IEEE80211_STA_AUTH)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001774 return -EINVAL;
1775 break;
1776 case IEEE80211_STA_AUTH:
Johannes Bergf09603a2012-01-20 13:55:21 +01001777 if (sta->sta_state != IEEE80211_STA_NONE &&
1778 sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001779 return -EINVAL;
1780 break;
1781 case IEEE80211_STA_ASSOC:
Johannes Bergf09603a2012-01-20 13:55:21 +01001782 if (sta->sta_state != IEEE80211_STA_AUTH &&
1783 sta->sta_state != IEEE80211_STA_AUTHORIZED)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001784 return -EINVAL;
1785 break;
1786 case IEEE80211_STA_AUTHORIZED:
Johannes Bergf09603a2012-01-20 13:55:21 +01001787 if (sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001788 return -EINVAL;
1789 break;
1790 default:
1791 WARN(1, "invalid state %d", new_state);
1792 return -EINVAL;
1793 }
1794
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001795 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1796 sta->sta.addr, new_state);
Johannes Bergf09603a2012-01-20 13:55:21 +01001797
1798 /*
1799 * notify the driver before the actual changes so it can
1800 * fail the transition
1801 */
1802 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1803 int err = drv_sta_state(sta->local, sta->sdata, sta,
1804 sta->sta_state, new_state);
1805 if (err)
1806 return err;
1807 }
1808
1809 /* reflect the change in all state variables */
1810
1811 switch (new_state) {
1812 case IEEE80211_STA_NONE:
1813 if (sta->sta_state == IEEE80211_STA_AUTH)
1814 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1815 break;
1816 case IEEE80211_STA_AUTH:
Ilan Peera7201a62015-12-13 13:41:43 +02001817 if (sta->sta_state == IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +01001818 set_bit(WLAN_STA_AUTH, &sta->_flags);
Ilan Peera7201a62015-12-13 13:41:43 +02001819 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
Johannes Bergf09603a2012-01-20 13:55:21 +01001820 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
Ilan Peera7201a62015-12-13 13:41:43 +02001821 ieee80211_recalc_min_chandef(sta->sdata);
1822 }
Johannes Bergf09603a2012-01-20 13:55:21 +01001823 break;
1824 case IEEE80211_STA_ASSOC:
1825 if (sta->sta_state == IEEE80211_STA_AUTH) {
1826 set_bit(WLAN_STA_ASSOC, &sta->_flags);
Ilan Peera7201a62015-12-13 13:41:43 +02001827 ieee80211_recalc_min_chandef(sta->sdata);
Johannes Bergf09603a2012-01-20 13:55:21 +01001828 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001829 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1830 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1831 !sta->sdata->u.vlan.sta))
1832 atomic_dec(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001833 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001834 ieee80211_clear_fast_xmit(sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001835 }
1836 break;
1837 case IEEE80211_STA_AUTHORIZED:
1838 if (sta->sta_state == IEEE80211_STA_ASSOC) {
Felix Fietkau7e3ed022012-04-23 19:49:03 +02001839 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1840 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1841 !sta->sdata->u.vlan.sta))
1842 atomic_inc(&sta->sdata->bss->num_mcast_sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001843 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
Johannes Berg17c18bf2015-03-21 15:25:43 +01001844 ieee80211_check_fast_xmit(sta);
Johannes Bergf09603a2012-01-20 13:55:21 +01001845 }
1846 break;
1847 default:
1848 break;
1849 }
1850
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001851 sta->sta_state = new_state;
1852
1853 return 0;
1854}
Emmanuel Grumbach687da132013-10-01 16:45:43 +03001855
1856u8 sta_info_tx_streams(struct sta_info *sta)
1857{
1858 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1859 u8 rx_streams;
1860
1861 if (!sta->sta.ht_cap.ht_supported)
1862 return 1;
1863
1864 if (sta->sta.vht_cap.vht_supported) {
1865 int i;
1866 u16 tx_mcs_map =
1867 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1868
1869 for (i = 7; i >= 0; i--)
1870 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1871 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1872 return i + 1;
1873 }
1874
1875 if (ht_cap->mcs.rx_mask[3])
1876 rx_streams = 4;
1877 else if (ht_cap->mcs.rx_mask[2])
1878 rx_streams = 3;
1879 else if (ht_cap->mcs.rx_mask[1])
1880 rx_streams = 2;
1881 else
1882 rx_streams = 1;
1883
1884 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1885 return rx_streams;
1886
1887 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1888 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1889}
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001890
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001891static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
1892{
1893 rinfo->flags = 0;
1894
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001895 if (sta->rx_stats.last_rate_flag & RX_FLAG_HT) {
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001896 rinfo->flags |= RATE_INFO_FLAGS_MCS;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001897 rinfo->mcs = sta->rx_stats.last_rate_idx;
1898 } else if (sta->rx_stats.last_rate_flag & RX_FLAG_VHT) {
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001899 rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001900 rinfo->nss = sta->rx_stats.last_rate_vht_nss;
1901 rinfo->mcs = sta->rx_stats.last_rate_idx;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001902 } else {
1903 struct ieee80211_supported_band *sband;
1904 int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
1905 u16 brate;
1906
1907 sband = sta->local->hw.wiphy->bands[
1908 ieee80211_get_sdata_band(sta->sdata)];
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001909 brate = sband->bitrates[sta->rx_stats.last_rate_idx].bitrate;
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001910 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
1911 }
1912
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001913 if (sta->rx_stats.last_rate_flag & RX_FLAG_SHORT_GI)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001914 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
1915
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001916 if (sta->rx_stats.last_rate_flag & RX_FLAG_5MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001917 rinfo->bw = RATE_INFO_BW_5;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001918 else if (sta->rx_stats.last_rate_flag & RX_FLAG_10MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001919 rinfo->bw = RATE_INFO_BW_10;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001920 else if (sta->rx_stats.last_rate_flag & RX_FLAG_40MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001921 rinfo->bw = RATE_INFO_BW_40;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001922 else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_80MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001923 rinfo->bw = RATE_INFO_BW_80;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001924 else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_160MHZ)
Johannes Bergfbd6ff52015-10-14 18:31:30 +02001925 rinfo->bw = RATE_INFO_BW_160;
1926 else
1927 rinfo->bw = RATE_INFO_BW_20;
1928}
1929
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001930void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1931{
1932 struct ieee80211_sub_if_data *sdata = sta->sdata;
1933 struct ieee80211_local *local = sdata->local;
John W. Linville9a244402014-07-25 10:22:36 -04001934 struct rate_control_ref *ref = NULL;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001935 u32 thr = 0;
1936 int i, ac;
1937
John W. Linville9a244402014-07-25 10:22:36 -04001938 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1939 ref = local->rate_ctrl;
1940
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001941 sinfo->generation = sdata->local->sta_generation;
1942
Johannes Berg225b8182015-01-21 21:09:02 +01001943 /* do before driver, so beacon filtering drivers have a
1944 * chance to e.g. just add the number of filtered beacons
1945 * (or just modify the value entirely, of course)
1946 */
1947 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1948 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1949
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001950 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1951
Johannes Berg319090b2014-11-17 14:08:11 +01001952 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1953 BIT(NL80211_STA_INFO_STA_FLAGS) |
1954 BIT(NL80211_STA_INFO_BSS_PARAM) |
1955 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
Johannes Berg976bd9e2015-10-16 17:18:11 +02001956 BIT(NL80211_STA_INFO_RX_DROP_MISC);
1957
1958 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1959 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
1960 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
1961 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001962
Arnd Bergmann84b00602015-09-30 13:26:36 +02001963 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001964 sinfo->inactive_time =
1965 jiffies_to_msecs(jiffies - sta->rx_stats.last_rx);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001966
Johannes Berg319090b2014-11-17 14:08:11 +01001967 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1968 BIT(NL80211_STA_INFO_TX_BYTES)))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001969 sinfo->tx_bytes = 0;
1970 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001971 sinfo->tx_bytes += sta->tx_stats.bytes[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01001972 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02001973 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001974
Johannes Berg319090b2014-11-17 14:08:11 +01001975 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001976 sinfo->tx_packets = 0;
1977 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001978 sinfo->tx_packets += sta->tx_stats.packets[ac];
Johannes Berg319090b2014-11-17 14:08:11 +01001979 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001980 }
1981
Johannes Berg319090b2014-11-17 14:08:11 +01001982 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1983 BIT(NL80211_STA_INFO_RX_BYTES)))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001984 sinfo->rx_bytes = sta->rx_stats.bytes;
Johannes Berg319090b2014-11-17 14:08:11 +01001985 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001986 }
1987
Johannes Berg319090b2014-11-17 14:08:11 +01001988 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001989 sinfo->rx_packets = sta->rx_stats.packets;
Johannes Berg319090b2014-11-17 14:08:11 +01001990 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001991 }
1992
Johannes Berg319090b2014-11-17 14:08:11 +01001993 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001994 sinfo->tx_retries = sta->status_stats.retry_count;
Johannes Berg319090b2014-11-17 14:08:11 +01001995 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01001996 }
1997
Johannes Berg319090b2014-11-17 14:08:11 +01001998 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001999 sinfo->tx_failed = sta->status_stats.retry_failed;
Johannes Berg319090b2014-11-17 14:08:11 +01002000 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002001 }
2002
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002003 sinfo->rx_dropped_misc = sta->rx_stats.dropped;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002004
Johannes Berg225b8182015-01-21 21:09:02 +01002005 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2006 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2007 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
2008 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2009 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2010 }
2011
Johannes Berg30686bf2015-06-02 21:39:54 +02002012 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2013 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
Johannes Berg319090b2014-11-17 14:08:11 +01002014 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002015 sinfo->signal = (s8)sta->rx_stats.last_signal;
Johannes Berg319090b2014-11-17 14:08:11 +01002016 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002017 }
2018
Johannes Berg319090b2014-11-17 14:08:11 +01002019 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
Johannes Berg40d9a382015-07-13 12:26:46 +02002020 sinfo->signal_avg =
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002021 -ewma_signal_read(&sta->rx_stats.avg_signal);
Johannes Berg319090b2014-11-17 14:08:11 +01002022 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002023 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002024 }
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002025
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002026 if (sta->rx_stats.chains &&
Johannes Berg319090b2014-11-17 14:08:11 +01002027 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2028 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2029 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2030 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002031
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002032 sinfo->chains = sta->rx_stats.chains;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002033 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002034 sinfo->chain_signal[i] =
2035 sta->rx_stats.chain_signal_last[i];
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002036 sinfo->chain_signal_avg[i] =
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002037 -ewma_signal_read(&sta->rx_stats.chain_signal_avg[i]);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002038 }
2039 }
2040
Johannes Berg319090b2014-11-17 14:08:11 +01002041 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002042 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2043 &sinfo->txrate);
Johannes Berg319090b2014-11-17 14:08:11 +01002044 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002045 }
2046
Johannes Berg319090b2014-11-17 14:08:11 +01002047 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002048 sta_set_rate_info_rx(sta, &sinfo->rxrate);
Johannes Berg319090b2014-11-17 14:08:11 +01002049 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
Johannes Berg2b9a7e12014-11-17 11:35:23 +01002050 }
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002051
Johannes Berg79c892b2014-11-21 14:26:31 +01002052 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
2053 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
2054 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
2055
2056 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2057 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002058 tidstats->rx_msdu = sta->rx_stats.msdu[i];
Johannes Berg79c892b2014-11-21 14:26:31 +01002059 }
2060
2061 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2062 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002063 tidstats->tx_msdu = sta->tx_stats.msdu[i];
Johannes Berg79c892b2014-11-21 14:26:31 +01002064 }
2065
2066 if (!(tidstats->filled &
2067 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02002068 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Berg79c892b2014-11-21 14:26:31 +01002069 tidstats->filled |=
2070 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002071 tidstats->tx_msdu_retries =
2072 sta->status_stats.msdu_retries[i];
Johannes Berg79c892b2014-11-21 14:26:31 +01002073 }
2074
2075 if (!(tidstats->filled &
2076 BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02002077 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
Johannes Berg79c892b2014-11-21 14:26:31 +01002078 tidstats->filled |=
2079 BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02002080 tidstats->tx_msdu_failed =
2081 sta->status_stats.msdu_failed[i];
Johannes Berg79c892b2014-11-21 14:26:31 +01002082 }
2083 }
2084
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002085 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2086#ifdef CONFIG_MAC80211_MESH
Johannes Berg319090b2014-11-17 14:08:11 +01002087 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2088 BIT(NL80211_STA_INFO_PLID) |
2089 BIT(NL80211_STA_INFO_PLINK_STATE) |
2090 BIT(NL80211_STA_INFO_LOCAL_PM) |
2091 BIT(NL80211_STA_INFO_PEER_PM) |
2092 BIT(NL80211_STA_INFO_NONPEER_PM);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002093
Johannes Berg433f5bc2015-06-17 10:31:00 +02002094 sinfo->llid = sta->mesh->llid;
2095 sinfo->plid = sta->mesh->plid;
2096 sinfo->plink_state = sta->mesh->plink_state;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002097 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
Johannes Berg319090b2014-11-17 14:08:11 +01002098 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
Johannes Berg433f5bc2015-06-17 10:31:00 +02002099 sinfo->t_offset = sta->mesh->t_offset;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002100 }
Johannes Berg433f5bc2015-06-17 10:31:00 +02002101 sinfo->local_pm = sta->mesh->local_pm;
2102 sinfo->peer_pm = sta->mesh->peer_pm;
2103 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002104#endif
2105 }
2106
2107 sinfo->bss_param.flags = 0;
2108 if (sdata->vif.bss_conf.use_cts_prot)
2109 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2110 if (sdata->vif.bss_conf.use_short_preamble)
2111 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2112 if (sdata->vif.bss_conf.use_short_slot)
2113 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
Emmanuel Grumbach785e21a2014-09-03 15:25:04 +03002114 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002115 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2116
2117 sinfo->sta_flags.set = 0;
2118 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2119 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2120 BIT(NL80211_STA_FLAG_WME) |
2121 BIT(NL80211_STA_FLAG_MFP) |
2122 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2123 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2124 BIT(NL80211_STA_FLAG_TDLS_PEER);
2125 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2126 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2127 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2128 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
Johannes Berga74a8c82014-07-22 14:50:47 +02002129 if (sta->sta.wme)
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002130 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2131 if (test_sta_flag(sta, WLAN_STA_MFP))
2132 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2133 if (test_sta_flag(sta, WLAN_STA_AUTH))
2134 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2135 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2136 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2137 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2138 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2139
2140 /* check if the driver has a SW RC implementation */
2141 if (ref && ref->ops->get_expected_throughput)
2142 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2143 else
2144 thr = drv_get_expected_throughput(local, &sta->sta);
2145
2146 if (thr != 0) {
Johannes Berg319090b2014-11-17 14:08:11 +01002147 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
Johannes Bergb7ffbd72014-06-04 17:31:56 +02002148 sinfo->expected_throughput = thr;
2149 }
2150}