blob: 569eb2cef3c98a8b90497ca47cc4bced331a47f2 [file] [log] [blame]
Jiri Bencf0706e822007-05-05 11:45:53 -07001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/module.h>
11#include <linux/init.h>
Felix Fietkau888d04d2012-03-01 15:22:09 +010012#include <linux/etherdevice.h>
Jiri Bencf0706e822007-05-05 11:45:53 -070013#include <linux/netdevice.h>
14#include <linux/types.h>
15#include <linux/slab.h>
16#include <linux/skbuff.h>
17#include <linux/if_arp.h>
Johannes Berg0d174402007-12-17 15:07:43 +010018#include <linux/timer.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010019#include <linux/rtnetlink.h>
Jiri Bencf0706e822007-05-05 11:45:53 -070020
21#include <net/mac80211.h>
22#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020023#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040024#include "rate.h"
Jiri Bencf0706e822007-05-05 11:45:53 -070025#include "sta_info.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070026#include "debugfs_sta.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010027#include "mesh.h"
Johannes Bergce662b442011-09-29 16:04:34 +020028#include "wme.h"
Jiri Bencf0706e822007-05-05 11:45:53 -070029
Johannes Bergd0709a62008-02-25 16:27:46 +010030/**
31 * DOC: STA information lifetime rules
32 *
33 * STA info structures (&struct sta_info) are managed in a hash table
34 * for faster lookup and a list for iteration. They are managed using
35 * RCU, i.e. access to the list and hash table is protected by RCU.
36 *
Johannes Berg34e89502010-02-03 13:59:58 +010037 * Upon allocating a STA info structure with sta_info_alloc(), the caller
38 * owns that structure. It must then insert it into the hash table using
39 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
40 * case (which acquires an rcu read section but must not be called from
41 * within one) will the pointer still be valid after the call. Note that
42 * the caller may not do much with the STA info before inserting it, in
43 * particular, it may not start any mesh peer link management or add
44 * encryption keys.
Johannes Berg93e5deb2008-04-01 15:21:00 +020045 *
46 * When the insertion fails (sta_info_insert()) returns non-zero), the
47 * structure will have been freed by sta_info_insert()!
Johannes Bergd0709a62008-02-25 16:27:46 +010048 *
Johannes Berg34e89502010-02-03 13:59:58 +010049 * Station entries are added by mac80211 when you establish a link with a
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040050 * peer. This means different things for the different type of interfaces
51 * we support. For a regular station this mean we add the AP sta when we
Lucas De Marchi25985ed2011-03-30 22:57:33 -030052 * receive an association response from the AP. For IBSS this occurs when
Johannes Berg34e89502010-02-03 13:59:58 +010053 * 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 -030054 * the peer immediately upon device open. When using AP mode we add stations
Johannes Berg34e89502010-02-03 13:59:58 +010055 * for each respective station upon request from userspace through nl80211.
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040056 *
Johannes Berg34e89502010-02-03 13:59:58 +010057 * In order to remove a STA info structure, various sta_info_destroy_*()
58 * calls are available.
Johannes Bergd0709a62008-02-25 16:27:46 +010059 *
Johannes Berg34e89502010-02-03 13:59:58 +010060 * There is no concept of ownership on a STA entry, each structure is
61 * owned by the global hash table/list until it is removed. All users of
62 * the structure need to be RCU protected so that the structure won't be
63 * freed before they are done using it.
Johannes Bergd0709a62008-02-25 16:27:46 +010064 */
Jiri Bencf0706e822007-05-05 11:45:53 -070065
Johannes Berg4d339602011-12-15 11:24:20 +010066/* Caller must hold local->sta_mtx */
Michael Wube8755e2007-07-27 15:43:23 +020067static int sta_info_hash_del(struct ieee80211_local *local,
68 struct sta_info *sta)
Jiri Bencf0706e822007-05-05 11:45:53 -070069{
70 struct sta_info *s;
71
Johannes Berg40b275b2011-05-13 14:15:49 +020072 s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
Johannes Berg4d339602011-12-15 11:24:20 +010073 lockdep_is_held(&local->sta_mtx));
Jiri Bencf0706e822007-05-05 11:45:53 -070074 if (!s)
Michael Wube8755e2007-07-27 15:43:23 +020075 return -ENOENT;
76 if (s == sta) {
Eric Dumazetcf778b02012-01-12 04:41:32 +000077 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
Johannes Bergd0709a62008-02-25 16:27:46 +010078 s->hnext);
Michael Wube8755e2007-07-27 15:43:23 +020079 return 0;
Jiri Bencf0706e822007-05-05 11:45:53 -070080 }
81
Johannes Berg40b275b2011-05-13 14:15:49 +020082 while (rcu_access_pointer(s->hnext) &&
83 rcu_access_pointer(s->hnext) != sta)
84 s = rcu_dereference_protected(s->hnext,
Johannes Berg4d339602011-12-15 11:24:20 +010085 lockdep_is_held(&local->sta_mtx));
Johannes Berg40b275b2011-05-13 14:15:49 +020086 if (rcu_access_pointer(s->hnext)) {
Eric Dumazetcf778b02012-01-12 04:41:32 +000087 rcu_assign_pointer(s->hnext, sta->hnext);
Michael Wube8755e2007-07-27 15:43:23 +020088 return 0;
89 }
Jiri Bencf0706e822007-05-05 11:45:53 -070090
Michael Wube8755e2007-07-27 15:43:23 +020091 return -ENOENT;
Jiri Bencf0706e822007-05-05 11:45:53 -070092}
93
Johannes Bergd0709a62008-02-25 16:27:46 +010094/* protected by RCU */
Johannes Bergabe60632009-11-25 17:46:18 +010095struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
96 const u8 *addr)
Johannes Berg43ba7e92008-02-21 14:09:30 +010097{
Johannes Bergabe60632009-11-25 17:46:18 +010098 struct ieee80211_local *local = sdata->local;
Johannes Berg43ba7e92008-02-21 14:09:30 +010099 struct sta_info *sta;
100
Johannes Berg03791852010-04-06 11:18:42 +0200101 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
Johannes Berg03791852010-04-06 11:18:42 +0200102 lockdep_is_held(&local->sta_mtx));
Johannes Berg43ba7e92008-02-21 14:09:30 +0100103 while (sta) {
Johannes Bergabe60632009-11-25 17:46:18 +0100104 if (sta->sdata == sdata &&
Felix Fietkau888d04d2012-03-01 15:22:09 +0100105 compare_ether_addr(sta->sta.addr, addr) == 0)
Johannes Berg43ba7e92008-02-21 14:09:30 +0100106 break;
Johannes Berg03791852010-04-06 11:18:42 +0200107 sta = rcu_dereference_check(sta->hnext,
Johannes Berg03791852010-04-06 11:18:42 +0200108 lockdep_is_held(&local->sta_mtx));
Johannes Berg43ba7e92008-02-21 14:09:30 +0100109 }
110 return sta;
111}
112
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100113/*
114 * Get sta info either from the specified interface
115 * or from one of its vlans
116 */
117struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
118 const u8 *addr)
119{
120 struct ieee80211_local *local = sdata->local;
121 struct sta_info *sta;
122
Johannes Berg03791852010-04-06 11:18:42 +0200123 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
Johannes Berg03791852010-04-06 11:18:42 +0200124 lockdep_is_held(&local->sta_mtx));
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100125 while (sta) {
126 if ((sta->sdata == sdata ||
Johannes Berga2c1e3d2010-09-14 21:34:14 +0200127 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
Felix Fietkau888d04d2012-03-01 15:22:09 +0100128 compare_ether_addr(sta->sta.addr, addr) == 0)
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100129 break;
Johannes Berg03791852010-04-06 11:18:42 +0200130 sta = rcu_dereference_check(sta->hnext,
Johannes Berg03791852010-04-06 11:18:42 +0200131 lockdep_is_held(&local->sta_mtx));
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100132 }
133 return sta;
134}
135
Johannes Berg3b53fde82009-11-16 12:00:37 +0100136struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
137 int idx)
Luis Carlos Coboee385852008-02-23 15:17:11 +0100138{
Johannes Berg3b53fde82009-11-16 12:00:37 +0100139 struct ieee80211_local *local = sdata->local;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100140 struct sta_info *sta;
141 int i = 0;
142
Johannes Bergd0709a62008-02-25 16:27:46 +0100143 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg3b53fde82009-11-16 12:00:37 +0100144 if (sdata != sta->sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800145 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100146 if (i < idx) {
147 ++i;
148 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100149 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800150 return sta;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100151 }
Luis Carlos Coboee385852008-02-23 15:17:11 +0100152
153 return NULL;
154}
Jiri Bencf0706e822007-05-05 11:45:53 -0700155
Johannes Berg93e5deb2008-04-01 15:21:00 +0200156/**
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100157 * sta_info_free - free STA
Johannes Berg93e5deb2008-04-01 15:21:00 +0200158 *
Randy Dunlap6ef307b2008-07-03 13:52:18 -0700159 * @local: pointer to the global information
Johannes Berg93e5deb2008-04-01 15:21:00 +0200160 * @sta: STA info to free
161 *
162 * This function must undo everything done by sta_info_alloc()
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100163 * that may happen before sta_info_insert(). It may only be
164 * called when sta_info_insert() has not been attempted (and
165 * if that fails, the station is freed anyway.)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200166 */
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100167void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
Johannes Berg93e5deb2008-04-01 15:21:00 +0200168{
Johannes Berg889cbb92012-01-17 10:33:29 +0100169 if (sta->rate_ctrl)
Johannes Bergaf65cd92009-11-17 18:18:36 +0100170 rate_control_free_sta(sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200171
172#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700173 wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200174#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
175
176 kfree(sta);
177}
178
Johannes Berg4d339602011-12-15 11:24:20 +0100179/* Caller must hold local->sta_mtx */
Johannes Bergd0709a62008-02-25 16:27:46 +0100180static void sta_info_hash_add(struct ieee80211_local *local,
181 struct sta_info *sta)
Jiri Bencf0706e822007-05-05 11:45:53 -0700182{
Johannes Berg4d339602011-12-15 11:24:20 +0100183 lockdep_assert_held(&local->sta_mtx);
Johannes Berg17741cd2008-09-11 00:02:02 +0200184 sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
Eric Dumazetcf778b02012-01-12 04:41:32 +0000185 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
Jiri Bencf0706e822007-05-05 11:45:53 -0700186}
Jiri Bencf0706e822007-05-05 11:45:53 -0700187
Johannes Bergaf818582009-11-06 11:35:50 +0100188static void sta_unblock(struct work_struct *wk)
189{
190 struct sta_info *sta;
191
192 sta = container_of(wk, struct sta_info, drv_unblock_wk);
193
194 if (sta->dead)
195 return;
196
Helmut Schaa54420472012-01-17 09:22:49 +0100197 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
198 local_bh_disable();
Johannes Bergaf818582009-11-06 11:35:50 +0100199 ieee80211_sta_ps_deliver_wakeup(sta);
Helmut Schaa54420472012-01-17 09:22:49 +0100200 local_bh_enable();
201 } else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200202 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Bergce662b442011-09-29 16:04:34 +0200203
204 local_bh_disable();
Johannes Bergaf818582009-11-06 11:35:50 +0100205 ieee80211_sta_ps_deliver_poll_response(sta);
Johannes Bergce662b442011-09-29 16:04:34 +0200206 local_bh_enable();
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200207 } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
208 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Bergce662b442011-09-29 16:04:34 +0200209
210 local_bh_disable();
Johannes Berg47086fc2011-09-29 16:04:33 +0200211 ieee80211_sta_ps_deliver_uapsd(sta);
Johannes Bergce662b442011-09-29 16:04:34 +0200212 local_bh_enable();
Johannes Berg50a94322010-11-16 11:50:28 -0800213 } else
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200214 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Johannes Bergaf818582009-11-06 11:35:50 +0100215}
216
Johannes Bergaf65cd92009-11-17 18:18:36 +0100217static int sta_prepare_rate_control(struct ieee80211_local *local,
218 struct sta_info *sta, gfp_t gfp)
219{
220 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
221 return 0;
222
Johannes Berg889cbb92012-01-17 10:33:29 +0100223 sta->rate_ctrl = local->rate_ctrl;
Johannes Bergaf65cd92009-11-17 18:18:36 +0100224 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
225 &sta->sta, gfp);
Johannes Berg889cbb92012-01-17 10:33:29 +0100226 if (!sta->rate_ctrl_priv)
Johannes Bergaf65cd92009-11-17 18:18:36 +0100227 return -ENOMEM;
Johannes Bergaf65cd92009-11-17 18:18:36 +0100228
229 return 0;
230}
231
Johannes Berg73651ee2008-02-25 16:27:47 +0100232struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
Johannes Berg56544162011-12-14 13:28:46 +0100233 const u8 *addr, gfp_t gfp)
Jiri Bencf0706e822007-05-05 11:45:53 -0700234{
Johannes Bergd0709a62008-02-25 16:27:46 +0100235 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e822007-05-05 11:45:53 -0700236 struct sta_info *sta;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +0530237 struct timespec uptime;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200238 int i;
Jiri Bencf0706e822007-05-05 11:45:53 -0700239
Johannes Berg17741cd2008-09-11 00:02:02 +0200240 sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
Jiri Bencf0706e822007-05-05 11:45:53 -0700241 if (!sta)
Johannes Berg73651ee2008-02-25 16:27:47 +0100242 return NULL;
Jiri Bencf0706e822007-05-05 11:45:53 -0700243
Johannes Berg07346f812008-05-03 01:02:02 +0200244 spin_lock_init(&sta->lock);
Emmanuel Grumbach63ef2232014-02-20 09:22:11 +0200245 spin_lock_init(&sta->ps_lock);
Johannes Bergaf818582009-11-06 11:35:50 +0100246 INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
Johannes Berg67c282c2010-06-10 10:21:43 +0200247 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
Johannes Berga93e3642010-06-10 10:21:46 +0200248 mutex_init(&sta->ampdu_mlme.mtx);
Johannes Berg07346f812008-05-03 01:02:02 +0200249
Johannes Berg17741cd2008-09-11 00:02:02 +0200250 memcpy(sta->sta.addr, addr, ETH_ALEN);
Johannes Bergd0709a62008-02-25 16:27:46 +0100251 sta->local = local;
252 sta->sdata = sdata;
Felix Fietkau8bc8aec2011-03-21 20:01:00 +0100253 sta->last_rx = jiffies;
Jiri Bencf0706e822007-05-05 11:45:53 -0700254
Johannes Berg71ec3752012-01-20 13:55:20 +0100255 sta->sta_state = IEEE80211_STA_NONE;
256
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +0530257 do_posix_clock_monotonic_gettime(&uptime);
258 sta->last_connected = uptime.tv_sec;
Bruno Randolf541a45a2010-12-02 19:12:43 +0900259 ewma_init(&sta->avg_signal, 1024, 8);
260
Johannes Bergaf65cd92009-11-17 18:18:36 +0100261 if (sta_prepare_rate_control(local, sta, gfp)) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700262 kfree(sta);
Johannes Berg73651ee2008-02-25 16:27:47 +0100263 return NULL;
Jiri Bencf0706e822007-05-05 11:45:53 -0700264 }
265
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200266 for (i = 0; i < STA_TID_NUM; i++) {
Johannes Berga622ab72010-06-10 10:21:39 +0200267 /*
268 * timer_to_tid must be initialized with identity mapping
269 * to enable session_timer's data differentiation. See
270 * sta_rx_agg_session_timer_expired for usage.
271 */
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200272 sta->timer_to_tid[i] = i;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200273 }
Johannes Berg948d8872011-09-29 16:04:29 +0200274 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
275 skb_queue_head_init(&sta->ps_tx_buf[i]);
276 skb_queue_head_init(&sta->tx_filtered[i]);
277 }
Johannes Berg73651ee2008-02-25 16:27:47 +0100278
Senthil Balasubramaniancccaec92009-05-14 18:42:08 +0530279 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700280 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
Senthil Balasubramaniancccaec92009-05-14 18:42:08 +0530281
Johannes Berg73651ee2008-02-25 16:27:47 +0100282#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700283 wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
Johannes Berg73651ee2008-02-25 16:27:47 +0100284#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
285
Johannes Berg03e44972008-02-27 09:56:40 +0100286#ifdef CONFIG_MAC80211_MESH
Javier Cardona57cf8042011-05-13 10:45:43 -0700287 sta->plink_state = NL80211_PLINK_LISTEN;
Johannes Berg03e44972008-02-27 09:56:40 +0100288 init_timer(&sta->plink_timer);
289#endif
290
Johannes Berg73651ee2008-02-25 16:27:47 +0100291 return sta;
292}
293
Guy Eilam8c71df72011-08-17 15:18:14 +0300294static int sta_info_insert_check(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100295{
Johannes Berg34e89502010-02-03 13:59:58 +0100296 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg34e89502010-02-03 13:59:58 +0100297
Johannes Berg03e44972008-02-27 09:56:40 +0100298 /*
299 * Can't be a WARN_ON because it can be triggered through a race:
300 * something inserts a STA (on one CPU) without holding the RTNL
301 * and another CPU turns off the net device.
302 */
Guy Eilam8c71df72011-08-17 15:18:14 +0300303 if (unlikely(!ieee80211_sdata_running(sdata)))
304 return -ENETDOWN;
Johannes Berg03e44972008-02-27 09:56:40 +0100305
Johannes Berg47846c92009-11-25 17:46:19 +0100306 if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
Guy Eilam8c71df72011-08-17 15:18:14 +0300307 is_multicast_ether_addr(sta->sta.addr)))
308 return -EINVAL;
309
310 return 0;
311}
312
Johannes Bergf09603a2012-01-20 13:55:21 +0100313static int sta_info_insert_drv_state(struct ieee80211_local *local,
314 struct ieee80211_sub_if_data *sdata,
315 struct sta_info *sta)
316{
317 enum ieee80211_sta_state state;
318 int err = 0;
319
320 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
321 err = drv_sta_state(local, sdata, sta, state, state + 1);
322 if (err)
323 break;
324 }
325
326 if (!err) {
Johannes Berga4ec45a2012-01-20 13:55:22 +0100327 /*
328 * Drivers using legacy sta_add/sta_remove callbacks only
329 * get uploaded set to true after sta_add is called.
330 */
331 if (!local->ops->sta_add)
332 sta->uploaded = true;
Johannes Bergf09603a2012-01-20 13:55:21 +0100333 return 0;
334 }
335
336 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
337 printk(KERN_DEBUG
338 "%s: failed to move IBSS STA %pM to state %d (%d) - keeping it anyway.\n",
339 sdata->name, sta->sta.addr, state + 1, err);
340 err = 0;
341 }
342
343 /* unwind on error */
344 for (; state > IEEE80211_STA_NOTEXIST; state--)
345 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
346
347 return err;
348}
349
Guy Eilam8c71df72011-08-17 15:18:14 +0300350/*
351 * should be called with sta_mtx locked
352 * this function replaces the mutex lock
353 * with a RCU lock
354 */
Johannes Berg4d339602011-12-15 11:24:20 +0100355static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
Guy Eilam8c71df72011-08-17 15:18:14 +0300356{
357 struct ieee80211_local *local = sta->local;
358 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg7852e362012-01-20 13:55:24 +0100359 struct station_info sinfo;
Guy Eilam8c71df72011-08-17 15:18:14 +0300360 int err = 0;
361
362 lockdep_assert_held(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +0100363
Johannes Berg7852e362012-01-20 13:55:24 +0100364 /* check if STA exists already */
365 if (sta_info_get_bss(sdata, sta->sta.addr)) {
366 err = -EEXIST;
367 goto out_err;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100368 }
Johannes Berg32bfd352007-12-19 01:31:26 +0100369
Johannes Berg7852e362012-01-20 13:55:24 +0100370 /* notify driver */
371 err = sta_info_insert_drv_state(local, sdata, sta);
372 if (err)
373 goto out_err;
Johannes Berg32bfd352007-12-19 01:31:26 +0100374
Johannes Berg7852e362012-01-20 13:55:24 +0100375 local->num_sta++;
376 local->sta_generation++;
377 smp_mb();
Johannes Berg4d339602011-12-15 11:24:20 +0100378
Johannes Berg7852e362012-01-20 13:55:24 +0100379 /* make the station visible */
380 sta_info_hash_add(local, sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100381
Arik Nemtsov10b34e72012-06-03 23:32:32 +0300382 list_add_rcu(&sta->list, &local->sta_list);
Johannes Berg83d5cc02012-01-12 09:31:10 +0100383
Johannes Berg7852e362012-01-20 13:55:24 +0100384 set_sta_flag(sta, WLAN_STA_INSERTED);
Johannes Berg4d339602011-12-15 11:24:20 +0100385
Johannes Berg7852e362012-01-20 13:55:24 +0100386 ieee80211_sta_debugfs_add(sta);
387 rate_control_add_sta_debugfs(sta);
Johannes Berg4d339602011-12-15 11:24:20 +0100388
Johannes Berg7852e362012-01-20 13:55:24 +0100389 memset(&sinfo, 0, sizeof(sinfo));
390 sinfo.filled = 0;
391 sinfo.generation = local->sta_generation;
392 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
Johannes Bergd0709a62008-02-25 16:27:46 +0100393
Jiri Bencf0706e822007-05-05 11:45:53 -0700394#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg7852e362012-01-20 13:55:24 +0100395 wiphy_debug(local->hw.wiphy, "Inserted STA %pM\n", sta->sta.addr);
Jiri Bencf0706e822007-05-05 11:45:53 -0700396#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
397
Johannes Berg34e89502010-02-03 13:59:58 +0100398 /* move reference to rcu-protected */
399 rcu_read_lock();
400 mutex_unlock(&local->sta_mtx);
Jiri Bence9f207f2007-05-05 11:46:38 -0700401
Johannes Berg73651ee2008-02-25 16:27:47 +0100402 if (ieee80211_vif_is_mesh(&sdata->vif))
403 mesh_accept_plinks_update(sdata);
404
405 return 0;
Johannes Berg4d339602011-12-15 11:24:20 +0100406 out_err:
407 mutex_unlock(&local->sta_mtx);
408 rcu_read_lock();
409 return err;
Guy Eilam8c71df72011-08-17 15:18:14 +0300410}
411
412int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
413{
414 struct ieee80211_local *local = sta->local;
Guy Eilam8c71df72011-08-17 15:18:14 +0300415 int err = 0;
416
Johannes Berg4d339602011-12-15 11:24:20 +0100417 might_sleep();
418
Guy Eilam8c71df72011-08-17 15:18:14 +0300419 err = sta_info_insert_check(sta);
420 if (err) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700421 rcu_read_lock();
422 goto out_free;
423 }
424
Johannes Berg004c8722008-02-20 11:21:35 +0100425 mutex_lock(&local->sta_mtx);
426
Johannes Berg4d339602011-12-15 11:24:20 +0100427 err = sta_info_insert_finish(sta);
Guy Eilam8c71df72011-08-17 15:18:14 +0300428 if (err)
Johannes Berg004c8722008-02-20 11:21:35 +0100429 goto out_free;
Johannes Bergd0709a62008-02-25 16:27:46 +0100430
Johannes Berg004c8722008-02-20 11:21:35 +0100431 return 0;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200432 out_free:
433 BUG_ON(!err);
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100434 sta_info_free(local, sta);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200435 return err;
Jiri Bencf0706e822007-05-05 11:45:53 -0700436}
437
Johannes Berg34e89502010-02-03 13:59:58 +0100438int sta_info_insert(struct sta_info *sta)
439{
440 int err = sta_info_insert_rcu(sta);
441
442 rcu_read_unlock();
443
444 return err;
445}
446
Jiri Bencf0706e822007-05-05 11:45:53 -0700447static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
Johannes Berg004c8722008-02-20 11:21:35 +0100448{
449 /*
450 * This format has been mandated by the IEEE specifications,
451 * so this line may not be changed to use the __set_bit() format.
452 */
453 bss->tim[aid / 8] |= (1 << (aid % 8));
454}
455
456static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
457{
458 /*
459 * This format has been mandated by the IEEE specifications,
460 * so this line may not be changed to use the __clear_bit() format.
461 */
462 bss->tim[aid / 8] &= ~(1 << (aid % 8));
463}
464
Johannes Berg948d8872011-09-29 16:04:29 +0200465static unsigned long ieee80211_tids_for_ac(int ac)
Johannes Berg004c8722008-02-20 11:21:35 +0100466{
Johannes Berg948d8872011-09-29 16:04:29 +0200467 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
468 switch (ac) {
469 case IEEE80211_AC_VO:
470 return BIT(6) | BIT(7);
471 case IEEE80211_AC_VI:
472 return BIT(4) | BIT(5);
473 case IEEE80211_AC_BE:
474 return BIT(0) | BIT(3);
475 case IEEE80211_AC_BK:
476 return BIT(1) | BIT(2);
477 default:
478 WARN_ON(1);
479 return 0;
Johannes Bergd0709a62008-02-25 16:27:46 +0100480 }
Johannes Berg004c8722008-02-20 11:21:35 +0100481}
482
Johannes Bergc868cb32011-09-29 16:04:27 +0200483void sta_info_recalc_tim(struct sta_info *sta)
Johannes Berg004c8722008-02-20 11:21:35 +0100484{
Johannes Bergc868cb32011-09-29 16:04:27 +0200485 struct ieee80211_local *local = sta->local;
486 struct ieee80211_if_ap *bss = sta->sdata->bss;
Johannes Bergd0709a62008-02-25 16:27:46 +0100487 unsigned long flags;
Johannes Berg948d8872011-09-29 16:04:29 +0200488 bool indicate_tim = false;
489 u8 ignore_for_tim = sta->sta.uapsd_queues;
490 int ac;
Johannes Berg004c8722008-02-20 11:21:35 +0100491
Johannes Bergc868cb32011-09-29 16:04:27 +0200492 if (WARN_ON_ONCE(!sta->sdata->bss))
493 return;
Johannes Berg3e122be2008-07-09 14:40:34 +0200494
Johannes Bergc868cb32011-09-29 16:04:27 +0200495 /* No need to do anything if the driver does all */
496 if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
497 return;
Johannes Berg004c8722008-02-20 11:21:35 +0100498
Johannes Bergc868cb32011-09-29 16:04:27 +0200499 if (sta->dead)
500 goto done;
Johannes Berg3e122be2008-07-09 14:40:34 +0200501
Johannes Berg948d8872011-09-29 16:04:29 +0200502 /*
503 * If all ACs are delivery-enabled then we should build
504 * the TIM bit for all ACs anyway; if only some are then
505 * we ignore those and build the TIM bit using only the
506 * non-enabled ones.
507 */
508 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
509 ignore_for_tim = 0;
Johannes Berg3e122be2008-07-09 14:40:34 +0200510
Johannes Berg948d8872011-09-29 16:04:29 +0200511 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
512 unsigned long tids;
513
514 if (ignore_for_tim & BIT(ac))
515 continue;
516
517 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
518 !skb_queue_empty(&sta->ps_tx_buf[ac]);
519 if (indicate_tim)
520 break;
521
522 tids = ieee80211_tids_for_ac(ac);
523
524 indicate_tim |=
525 sta->driver_buffered_tids & tids;
Johannes Bergd0709a62008-02-25 16:27:46 +0100526 }
Johannes Berg004c8722008-02-20 11:21:35 +0100527
Johannes Bergc868cb32011-09-29 16:04:27 +0200528 done:
Johannes Berg4d339602011-12-15 11:24:20 +0100529 spin_lock_irqsave(&local->tim_lock, flags);
Johannes Berg004c8722008-02-20 11:21:35 +0100530
Johannes Berg948d8872011-09-29 16:04:29 +0200531 if (indicate_tim)
Johannes Bergc868cb32011-09-29 16:04:27 +0200532 __bss_tim_set(bss, sta->sta.aid);
533 else
534 __bss_tim_clear(bss, sta->sta.aid);
Johannes Berg3e122be2008-07-09 14:40:34 +0200535
Johannes Bergc868cb32011-09-29 16:04:27 +0200536 if (local->ops->set_tim) {
537 local->tim_in_locked_section = true;
Johannes Berg948d8872011-09-29 16:04:29 +0200538 drv_set_tim(local, &sta->sta, indicate_tim);
Johannes Bergc868cb32011-09-29 16:04:27 +0200539 local->tim_in_locked_section = false;
Johannes Berg004c8722008-02-20 11:21:35 +0100540 }
Johannes Berg004c8722008-02-20 11:21:35 +0100541
Johannes Berg4d339602011-12-15 11:24:20 +0100542 spin_unlock_irqrestore(&local->tim_lock, flags);
Johannes Berg004c8722008-02-20 11:21:35 +0100543}
544
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200545static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
Jiri Bencf0706e822007-05-05 11:45:53 -0700546{
Johannes Berge039fa42008-05-15 12:55:29 +0200547 struct ieee80211_tx_info *info;
Jiri Bencf0706e822007-05-05 11:45:53 -0700548 int timeout;
549
550 if (!skb)
Johannes Bergcd0b8d82011-09-06 14:13:06 +0200551 return false;
Jiri Bencf0706e822007-05-05 11:45:53 -0700552
Johannes Berge039fa42008-05-15 12:55:29 +0200553 info = IEEE80211_SKB_CB(skb);
Jiri Bencf0706e822007-05-05 11:45:53 -0700554
555 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200556 timeout = (sta->listen_interval *
557 sta->sdata->vif.bss_conf.beacon_int *
558 32 / 15625) * HZ;
Jiri Bencf0706e822007-05-05 11:45:53 -0700559 if (timeout < STA_TX_BUFFER_EXPIRE)
560 timeout = STA_TX_BUFFER_EXPIRE;
Johannes Berge039fa42008-05-15 12:55:29 +0200561 return time_after(jiffies, info->control.jiffies + timeout);
Jiri Bencf0706e822007-05-05 11:45:53 -0700562}
563
564
Johannes Berg948d8872011-09-29 16:04:29 +0200565static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
566 struct sta_info *sta, int ac)
Jiri Bencf0706e822007-05-05 11:45:53 -0700567{
568 unsigned long flags;
569 struct sk_buff *skb;
570
Johannes Berg60750392011-09-29 16:04:28 +0200571 /*
572 * First check for frames that should expire on the filtered
573 * queue. Frames here were rejected by the driver and are on
574 * a separate queue to avoid reordering with normal PS-buffered
575 * frames. They also aren't accounted for right now in the
576 * total_ps_buffered counter.
577 */
Jiri Bencf0706e822007-05-05 11:45:53 -0700578 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200579 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
580 skb = skb_peek(&sta->tx_filtered[ac]);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200581 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200582 skb = __skb_dequeue(&sta->tx_filtered[ac]);
Johannes Berg836341a2008-02-20 02:07:21 +0100583 else
Jiri Bencf0706e822007-05-05 11:45:53 -0700584 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200585 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Jiri Bencf0706e822007-05-05 11:45:53 -0700586
Johannes Berg60750392011-09-29 16:04:28 +0200587 /*
588 * Frames are queued in order, so if this one
589 * hasn't expired yet we can stop testing. If
590 * we actually reached the end of the queue we
591 * also need to stop, of course.
592 */
593 if (!skb)
594 break;
595 dev_kfree_skb(skb);
596 }
597
598 /*
599 * Now also check the normal PS-buffered queue, this will
600 * only find something if the filtered queue was emptied
601 * since the filtered frames are all before the normal PS
602 * buffered frames.
603 */
Jiri Bencf0706e822007-05-05 11:45:53 -0700604 for (;;) {
Johannes Berg948d8872011-09-29 16:04:29 +0200605 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
606 skb = skb_peek(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e822007-05-05 11:45:53 -0700607 if (sta_info_buffer_expired(sta, skb))
Johannes Berg948d8872011-09-29 16:04:29 +0200608 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
Jiri Bencf0706e822007-05-05 11:45:53 -0700609 else
610 skb = NULL;
Johannes Berg948d8872011-09-29 16:04:29 +0200611 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Jiri Bencf0706e822007-05-05 11:45:53 -0700612
Johannes Berg60750392011-09-29 16:04:28 +0200613 /*
614 * frames are queued in order, so if this one
615 * hasn't expired yet (or we reached the end of
616 * the queue) we can stop testing
617 */
Johannes Berg836341a2008-02-20 02:07:21 +0100618 if (!skb)
Jiri Bencf0706e822007-05-05 11:45:53 -0700619 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100620
Johannes Berg836341a2008-02-20 02:07:21 +0100621 local->total_ps_buffered--;
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200622#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700623 printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
624 sta->sta.addr);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200625#endif
Johannes Berg836341a2008-02-20 02:07:21 +0100626 dev_kfree_skb(skb);
Jiri Bencf0706e822007-05-05 11:45:53 -0700627 }
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300628
Johannes Berg60750392011-09-29 16:04:28 +0200629 /*
630 * Finally, recalculate the TIM bit for this station -- it might
631 * now be clear because the station was too slow to retrieve its
632 * frames.
633 */
634 sta_info_recalc_tim(sta);
635
636 /*
637 * Return whether there are any frames still buffered, this is
638 * used to check whether the cleanup timer still needs to run,
639 * if there are no frames we don't need to rearm the timer.
640 */
Johannes Berg948d8872011-09-29 16:04:29 +0200641 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
642 skb_queue_empty(&sta->tx_filtered[ac]));
643}
644
645static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
646 struct sta_info *sta)
647{
648 bool have_buffered = false;
649 int ac;
650
651 /* This is only necessary for stations on BSS interfaces */
652 if (!sta->sdata->bss)
653 return false;
654
655 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
656 have_buffered |=
657 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
658
659 return have_buffered;
Jiri Bencf0706e822007-05-05 11:45:53 -0700660}
661
Johannes Berg83d5cc02012-01-12 09:31:10 +0100662int __must_check __sta_info_destroy(struct sta_info *sta)
Johannes Berg34e89502010-02-03 13:59:58 +0100663{
664 struct ieee80211_local *local;
665 struct ieee80211_sub_if_data *sdata;
Johannes Berg948d8872011-09-29 16:04:29 +0200666 int ret, i, ac;
Yogesh Ashok Powar42624d42011-12-08 14:56:15 +0530667 struct tid_ampdu_tx *tid_tx;
Johannes Berg34e89502010-02-03 13:59:58 +0100668
669 might_sleep();
670
671 if (!sta)
672 return -ENOENT;
673
674 local = sta->local;
675 sdata = sta->sdata;
676
Johannes Berg83d5cc02012-01-12 09:31:10 +0100677 lockdep_assert_held(&local->sta_mtx);
678
Johannes Berg098a6072010-04-06 11:18:47 +0200679 /*
680 * Before removing the station from the driver and
681 * rate control, it might still start new aggregation
682 * sessions -- block that to make sure the tear-down
683 * will be sufficient.
684 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200685 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg53f73c02010-10-05 19:37:40 +0200686 ieee80211_sta_tear_down_BA_sessions(sta, true);
Johannes Berg098a6072010-04-06 11:18:47 +0200687
Johannes Berg34e89502010-02-03 13:59:58 +0100688 ret = sta_info_hash_del(local, sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100689 if (ret)
690 return ret;
691
Arik Nemtsov10b34e72012-06-03 23:32:32 +0300692 list_del_rcu(&sta->list);
Johannes Berg4d339602011-12-15 11:24:20 +0100693
Johannes Berg8cb23152011-05-12 15:07:15 +0200694 mutex_lock(&local->key_mtx);
Johannes Berge31b8212010-10-05 19:39:30 +0200695 for (i = 0; i < NUM_DEFAULT_KEYS; i++)
Johannes Berg40b275b2011-05-13 14:15:49 +0200696 __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
Johannes Berge31b8212010-10-05 19:39:30 +0200697 if (sta->ptk)
Johannes Berg40b275b2011-05-13 14:15:49 +0200698 __ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
Johannes Berg8cb23152011-05-12 15:07:15 +0200699 mutex_unlock(&local->key_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +0100700
701 sta->dead = true;
702
Johannes Berg34e89502010-02-03 13:59:58 +0100703 local->num_sta--;
704 local->sta_generation++;
705
706 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000707 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
Johannes Berg34e89502010-02-03 13:59:58 +0100708
Johannes Berg83d5cc02012-01-12 09:31:10 +0100709 while (sta->sta_state > IEEE80211_STA_NONE) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100710 ret = sta_info_move_state(sta, sta->sta_state - 1);
711 if (ret) {
Johannes Berg83d5cc02012-01-12 09:31:10 +0100712 WARN_ON_ONCE(1);
713 break;
714 }
715 }
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100716
Johannes Bergf09603a2012-01-20 13:55:21 +0100717 if (sta->uploaded) {
Johannes Bergf09603a2012-01-20 13:55:21 +0100718 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
719 IEEE80211_STA_NOTEXIST);
720 WARN_ON_ONCE(ret != 0);
721 }
Johannes Berg34e89502010-02-03 13:59:58 +0100722
Johannes Berge64b3792010-04-06 11:18:43 +0200723 /*
724 * At this point, after we wait for an RCU grace period,
725 * neither mac80211 nor the driver can reference this
726 * sta struct any more except by still existing timers
727 * associated with this station that we clean up below.
728 */
729 synchronize_rcu();
730
Helmut Schaa4f3eb0b2012-01-27 11:02:53 +0100731 if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
732 BUG_ON(!sdata->bss);
733
734 clear_sta_flag(sta, WLAN_STA_PS_STA);
735
736 atomic_dec(&sdata->bss->num_sta_ps);
737 sta_info_recalc_tim(sta);
738 }
739
Johannes Berg948d8872011-09-29 16:04:29 +0200740 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
741 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
Felix Fietkaub144f9452012-11-10 03:44:14 +0100742 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
743 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
Johannes Berg948d8872011-09-29 16:04:29 +0200744 }
Johannes Bergc868cb32011-09-29 16:04:27 +0200745
Johannes Berg34e89502010-02-03 13:59:58 +0100746#ifdef CONFIG_MAC80211_MESH
Johannes Berge64b3792010-04-06 11:18:43 +0200747 if (ieee80211_vif_is_mesh(&sdata->vif))
Johannes Berg34e89502010-02-03 13:59:58 +0100748 mesh_accept_plinks_update(sdata);
Johannes Berg34e89502010-02-03 13:59:58 +0100749#endif
750
751#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700752 wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100753#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
754 cancel_work_sync(&sta->drv_unblock_wk);
755
Jouni Malinenec15e682011-03-23 15:29:52 +0200756 cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
757
Johannes Berg34e89502010-02-03 13:59:58 +0100758 rate_control_remove_sta_debugfs(sta);
759 ieee80211_sta_debugfs_remove(sta);
760
761#ifdef CONFIG_MAC80211_MESH
762 if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
763 mesh_plink_deactivate(sta);
764 del_timer_sync(&sta->plink_timer);
765 }
766#endif
767
Johannes Berg151a02f2012-01-17 10:30:01 +0100768 /*
769 * Destroy aggregation state here. It would be nice to wait for the
770 * driver to finish aggregation stop and then clean up, but for now
771 * drivers have to handle aggregation stop being requested, followed
772 * directly by station destruction.
Yogesh Ashok Powar42624d42011-12-08 14:56:15 +0530773 */
774 for (i = 0; i < STA_TID_NUM; i++) {
Johannes Berg151a02f2012-01-17 10:30:01 +0100775 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
Yogesh Ashok Powar27bf8882011-12-16 11:47:15 +0530776 if (!tid_tx)
Yogesh Ashok Powar42624d42011-12-08 14:56:15 +0530777 continue;
Felix Fietkaub144f9452012-11-10 03:44:14 +0100778 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
Johannes Berg151a02f2012-01-17 10:30:01 +0100779 kfree(tid_tx);
Yogesh Ashok Powar42624d42011-12-08 14:56:15 +0530780 }
781
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100782 sta_info_free(local, sta);
Johannes Berg34e89502010-02-03 13:59:58 +0100783
784 return 0;
785}
786
787int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
788{
789 struct sta_info *sta;
790 int ret;
791
792 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100793 sta = sta_info_get(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100794 ret = __sta_info_destroy(sta);
795 mutex_unlock(&sdata->local->sta_mtx);
796
797 return ret;
798}
799
800int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
801 const u8 *addr)
802{
803 struct sta_info *sta;
804 int ret;
805
806 mutex_lock(&sdata->local->sta_mtx);
Johannes Berg7852e362012-01-20 13:55:24 +0100807 sta = sta_info_get_bss(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100808 ret = __sta_info_destroy(sta);
809 mutex_unlock(&sdata->local->sta_mtx);
810
811 return ret;
812}
Jiri Bencf0706e822007-05-05 11:45:53 -0700813
814static void sta_info_cleanup(unsigned long data)
815{
816 struct ieee80211_local *local = (struct ieee80211_local *) data;
817 struct sta_info *sta;
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300818 bool timer_needed = false;
Jiri Bencf0706e822007-05-05 11:45:53 -0700819
Johannes Bergd0709a62008-02-25 16:27:46 +0100820 rcu_read_lock();
821 list_for_each_entry_rcu(sta, &local->sta_list, list)
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300822 if (sta_info_cleanup_expire_buffered(local, sta))
823 timer_needed = true;
Johannes Bergd0709a62008-02-25 16:27:46 +0100824 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -0700825
Johannes Berg5bb644a2009-05-17 11:40:42 +0200826 if (local->quiescing)
827 return;
828
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300829 if (!timer_needed)
830 return;
831
Johannes Berg26d59532011-04-01 13:52:48 +0200832 mod_timer(&local->sta_cleanup,
833 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
Jiri Bencf0706e822007-05-05 11:45:53 -0700834}
835
836void sta_info_init(struct ieee80211_local *local)
837{
Johannes Berg4d339602011-12-15 11:24:20 +0100838 spin_lock_init(&local->tim_lock);
Johannes Berg34e89502010-02-03 13:59:58 +0100839 mutex_init(&local->sta_mtx);
Jiri Bencf0706e822007-05-05 11:45:53 -0700840 INIT_LIST_HEAD(&local->sta_list);
Jiri Bencf0706e822007-05-05 11:45:53 -0700841
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800842 setup_timer(&local->sta_cleanup, sta_info_cleanup,
843 (unsigned long)local);
Jiri Bencf0706e822007-05-05 11:45:53 -0700844}
845
846void sta_info_stop(struct ieee80211_local *local)
847{
Johannes Berg4d2a32d2012-12-13 23:08:52 +0100848 del_timer_sync(&local->sta_cleanup);
Michael Wube8755e2007-07-27 15:43:23 +0200849 sta_info_flush(local, NULL);
Jiri Bencf0706e822007-05-05 11:45:53 -0700850}
851
Jiri Bencf0706e822007-05-05 11:45:53 -0700852/**
853 * sta_info_flush - flush matching STA entries from the STA table
Johannes Berg44213b52008-02-25 16:27:49 +0100854 *
855 * Returns the number of removed STA entries.
856 *
Jiri Bencf0706e822007-05-05 11:45:53 -0700857 * @local: local interface data
Johannes Bergd0709a62008-02-25 16:27:46 +0100858 * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
Jiri Bencf0706e822007-05-05 11:45:53 -0700859 */
Johannes Berg44213b52008-02-25 16:27:49 +0100860int sta_info_flush(struct ieee80211_local *local,
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200861 struct ieee80211_sub_if_data *sdata)
Jiri Bencf0706e822007-05-05 11:45:53 -0700862{
863 struct sta_info *sta, *tmp;
Johannes Berg44213b52008-02-25 16:27:49 +0100864 int ret = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -0700865
Johannes Bergd0709a62008-02-25 16:27:46 +0100866 might_sleep();
867
Johannes Berg34e89502010-02-03 13:59:58 +0100868 mutex_lock(&local->sta_mtx);
Johannes Berg34e89502010-02-03 13:59:58 +0100869 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Johannes Berg34316832012-02-25 21:40:46 +0100870 if (!sdata || sdata == sta->sdata) {
Johannes Berg34e89502010-02-03 13:59:58 +0100871 WARN_ON(__sta_info_destroy(sta));
Johannes Berg34316832012-02-25 21:40:46 +0100872 ret++;
873 }
Johannes Berg34e89502010-02-03 13:59:58 +0100874 }
875 mutex_unlock(&local->sta_mtx);
Johannes Berg44213b52008-02-25 16:27:49 +0100876
877 return ret;
Jiri Bencf0706e822007-05-05 11:45:53 -0700878}
Johannes Bergdc6676b2008-03-31 19:23:03 +0200879
Johannes Berg24723d12008-09-11 00:01:46 +0200880void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
881 unsigned long exp_time)
882{
883 struct ieee80211_local *local = sdata->local;
884 struct sta_info *sta, *tmp;
Johannes Berg24723d12008-09-11 00:01:46 +0200885
Johannes Berg34e89502010-02-03 13:59:58 +0100886 mutex_lock(&local->sta_mtx);
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +0530887
888 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
Marek Lindnerec2b7742011-12-20 23:16:52 +0800889 if (sdata != sta->sdata)
890 continue;
891
Johannes Berg24723d12008-09-11 00:01:46 +0200892 if (time_after(jiffies, sta->last_rx + exp_time)) {
893#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700894 printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100895 sdata->name, sta->sta.addr);
Johannes Berg24723d12008-09-11 00:01:46 +0200896#endif
Johannes Berg34e89502010-02-03 13:59:58 +0100897 WARN_ON(__sta_info_destroy(sta));
Johannes Berg24723d12008-09-11 00:01:46 +0200898 }
Mohammed Shafi Shajakhane46a2cf2011-12-26 10:43:29 +0530899 }
900
Johannes Berg34e89502010-02-03 13:59:58 +0100901 mutex_unlock(&local->sta_mtx);
Johannes Berg24723d12008-09-11 00:01:46 +0200902}
Johannes Berg17741cd2008-09-11 00:02:02 +0200903
Ben Greear686b9cb2010-09-23 09:44:36 -0700904struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
905 const u8 *addr,
906 const u8 *localaddr)
Johannes Berg17741cd2008-09-11 00:02:02 +0200907{
Johannes Bergabe60632009-11-25 17:46:18 +0100908 struct sta_info *sta, *nxt;
Johannes Berg17741cd2008-09-11 00:02:02 +0200909
Ben Greear686b9cb2010-09-23 09:44:36 -0700910 /*
911 * Just return a random station if localaddr is NULL
912 * ... first in list.
913 */
Johannes Bergf7c65592010-04-30 13:48:36 +0200914 for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
Ben Greear686b9cb2010-09-23 09:44:36 -0700915 if (localaddr &&
916 compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
917 continue;
Johannes Bergf7c65592010-04-30 13:48:36 +0200918 if (!sta->uploaded)
919 return NULL;
Johannes Bergabe60632009-11-25 17:46:18 +0100920 return &sta->sta;
Johannes Bergf7c65592010-04-30 13:48:36 +0200921 }
922
Johannes Bergabe60632009-11-25 17:46:18 +0100923 return NULL;
Johannes Berg17741cd2008-09-11 00:02:02 +0200924}
Ben Greear686b9cb2010-09-23 09:44:36 -0700925EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
Johannes Berg5ed176e2009-11-04 14:42:28 +0100926
927struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
928 const u8 *addr)
929{
Johannes Bergf7c65592010-04-30 13:48:36 +0200930 struct sta_info *sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +0100931
932 if (!vif)
933 return NULL;
934
Johannes Bergf7c65592010-04-30 13:48:36 +0200935 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
936 if (!sta)
937 return NULL;
Johannes Berg5ed176e2009-11-04 14:42:28 +0100938
Johannes Bergf7c65592010-04-30 13:48:36 +0200939 if (!sta->uploaded)
940 return NULL;
941
942 return &sta->sta;
Johannes Berg5ed176e2009-11-04 14:42:28 +0100943}
Johannes Berg17741cd2008-09-11 00:02:02 +0200944EXPORT_SYMBOL(ieee80211_find_sta);
Johannes Bergaf818582009-11-06 11:35:50 +0100945
Johannes Berg50a94322010-11-16 11:50:28 -0800946static void clear_sta_ps_flags(void *_sta)
947{
948 struct sta_info *sta = _sta;
Helmut Schaa608383b2012-01-30 15:18:00 +0100949 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg50a94322010-11-16 11:50:28 -0800950
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200951 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
Helmut Schaa608383b2012-01-30 15:18:00 +0100952 if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA))
953 atomic_dec(&sdata->bss->num_sta_ps);
Johannes Berg50a94322010-11-16 11:50:28 -0800954}
955
Johannes Bergaf818582009-11-06 11:35:50 +0100956/* powersave support code */
957void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
958{
959 struct ieee80211_sub_if_data *sdata = sta->sdata;
960 struct ieee80211_local *local = sdata->local;
Johannes Berg948d8872011-09-29 16:04:29 +0200961 struct sk_buff_head pending;
962 int filtered = 0, buffered = 0, ac;
Arik Nemtsov60407002012-11-05 10:27:52 +0200963 unsigned long flags;
Johannes Bergaf818582009-11-06 11:35:50 +0100964
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200965 clear_sta_flag(sta, WLAN_STA_SP);
Johannes Berg47086fc2011-09-29 16:04:33 +0200966
Johannes Berg948d8872011-09-29 16:04:29 +0200967 BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1);
968 sta->driver_buffered_tids = 0;
969
Arik Nemtsovd057e5a2011-01-31 22:29:13 +0200970 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
971 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
Johannes Bergaf818582009-11-06 11:35:50 +0100972
Johannes Berg948d8872011-09-29 16:04:29 +0200973 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +0100974
Emmanuel Grumbach63ef2232014-02-20 09:22:11 +0200975 /* sync with ieee80211_tx_h_unicast_ps_buf */
976 spin_lock(&sta->ps_lock);
Johannes Bergaf818582009-11-06 11:35:50 +0100977 /* Send all buffered frames to the station */
Johannes Berg948d8872011-09-29 16:04:29 +0200978 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
979 int count = skb_queue_len(&pending), tmp;
980
Arik Nemtsov60407002012-11-05 10:27:52 +0200981 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +0200982 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
Arik Nemtsov60407002012-11-05 10:27:52 +0200983 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +0200984 tmp = skb_queue_len(&pending);
985 filtered += tmp - count;
986 count = tmp;
987
Arik Nemtsov60407002012-11-05 10:27:52 +0200988 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +0200989 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
Arik Nemtsov60407002012-11-05 10:27:52 +0200990 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
Johannes Berg948d8872011-09-29 16:04:29 +0200991 tmp = skb_queue_len(&pending);
992 buffered += tmp - count;
993 }
994
995 ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
Emmanuel Grumbach63ef2232014-02-20 09:22:11 +0200996 spin_unlock(&sta->ps_lock);
Johannes Berg948d8872011-09-29 16:04:29 +0200997
Johannes Bergaf818582009-11-06 11:35:50 +0100998 local->total_ps_buffered -= buffered;
999
Johannes Bergc868cb32011-09-29 16:04:27 +02001000 sta_info_recalc_tim(sta);
1001
Johannes Bergaf818582009-11-06 11:35:50 +01001002#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1003 printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
Johannes Berg47846c92009-11-25 17:46:19 +01001004 "since STA not sleeping anymore\n", sdata->name,
Johannes Berg948d8872011-09-29 16:04:29 +02001005 sta->sta.addr, sta->sta.aid, filtered, buffered);
Johannes Bergaf818582009-11-06 11:35:50 +01001006#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1007}
1008
Johannes Bergce662b442011-09-29 16:04:34 +02001009static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1010 struct sta_info *sta, int tid,
Johannes Berg40b96402011-09-29 16:04:38 +02001011 enum ieee80211_frame_release_type reason)
Johannes Bergce662b442011-09-29 16:04:34 +02001012{
1013 struct ieee80211_local *local = sdata->local;
1014 struct ieee80211_qos_hdr *nullfunc;
1015 struct sk_buff *skb;
1016 int size = sizeof(*nullfunc);
1017 __le16 fc;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001018 bool qos = test_sta_flag(sta, WLAN_STA_WME);
Johannes Bergce662b442011-09-29 16:04:34 +02001019 struct ieee80211_tx_info *info;
1020
1021 if (qos) {
1022 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1023 IEEE80211_STYPE_QOS_NULLFUNC |
1024 IEEE80211_FCTL_FROMDS);
1025 } else {
1026 size -= 2;
1027 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1028 IEEE80211_STYPE_NULLFUNC |
1029 IEEE80211_FCTL_FROMDS);
1030 }
1031
1032 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1033 if (!skb)
1034 return;
1035
1036 skb_reserve(skb, local->hw.extra_tx_headroom);
1037
1038 nullfunc = (void *) skb_put(skb, size);
1039 nullfunc->frame_control = fc;
1040 nullfunc->duration_id = 0;
1041 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1042 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1043 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1044
Johannes Berg59b66252011-10-13 13:19:19 +02001045 skb->priority = tid;
1046 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
Johannes Bergce662b442011-09-29 16:04:34 +02001047 if (qos) {
Johannes Bergce662b442011-09-29 16:04:34 +02001048 nullfunc->qos_ctrl = cpu_to_le16(tid);
1049
Johannes Berg40b96402011-09-29 16:04:38 +02001050 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
Johannes Bergce662b442011-09-29 16:04:34 +02001051 nullfunc->qos_ctrl |=
1052 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1053 }
1054
1055 info = IEEE80211_SKB_CB(skb);
1056
1057 /*
1058 * Tell TX path to send this frame even though the
1059 * STA may still remain is PS mode after this frame
Johannes Bergdeeaee12011-09-29 16:04:35 +02001060 * exchange. Also set EOSP to indicate this packet
1061 * ends the poll/service period.
Johannes Bergce662b442011-09-29 16:04:34 +02001062 */
Johannes Berg02f2f1a2012-02-27 12:18:30 +01001063 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
Johannes Bergdeeaee12011-09-29 16:04:35 +02001064 IEEE80211_TX_STATUS_EOSP |
1065 IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Bergce662b442011-09-29 16:04:34 +02001066
Johannes Berg40b96402011-09-29 16:04:38 +02001067 drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
1068
Johannes Bergce662b442011-09-29 16:04:34 +02001069 ieee80211_xmit(sdata, skb);
1070}
1071
Johannes Berg47086fc2011-09-29 16:04:33 +02001072static void
1073ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1074 int n_frames, u8 ignored_acs,
1075 enum ieee80211_frame_release_type reason)
Johannes Bergaf818582009-11-06 11:35:50 +01001076{
1077 struct ieee80211_sub_if_data *sdata = sta->sdata;
1078 struct ieee80211_local *local = sdata->local;
Johannes Berg4049e092011-09-29 16:04:32 +02001079 bool found = false;
Johannes Berg948d8872011-09-29 16:04:29 +02001080 bool more_data = false;
1081 int ac;
Johannes Berg4049e092011-09-29 16:04:32 +02001082 unsigned long driver_release_tids = 0;
Johannes Berg47086fc2011-09-29 16:04:33 +02001083 struct sk_buff_head frames;
1084
Johannes Bergdeeaee12011-09-29 16:04:35 +02001085 /* Service or PS-Poll period starts */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001086 set_sta_flag(sta, WLAN_STA_SP);
Johannes Bergdeeaee12011-09-29 16:04:35 +02001087
Johannes Berg47086fc2011-09-29 16:04:33 +02001088 __skb_queue_head_init(&frames);
Johannes Bergaf818582009-11-06 11:35:50 +01001089
Johannes Berg948d8872011-09-29 16:04:29 +02001090 /*
Johannes Berg47086fc2011-09-29 16:04:33 +02001091 * Get response frame(s) and more data bit for it.
Johannes Berg948d8872011-09-29 16:04:29 +02001092 */
1093 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg4049e092011-09-29 16:04:32 +02001094 unsigned long tids;
1095
Johannes Berg47086fc2011-09-29 16:04:33 +02001096 if (ignored_acs & BIT(ac))
Johannes Berg948d8872011-09-29 16:04:29 +02001097 continue;
1098
Johannes Berg4049e092011-09-29 16:04:32 +02001099 tids = ieee80211_tids_for_ac(ac);
1100
1101 if (!found) {
1102 driver_release_tids = sta->driver_buffered_tids & tids;
1103 if (driver_release_tids) {
1104 found = true;
1105 } else {
Johannes Berg47086fc2011-09-29 16:04:33 +02001106 struct sk_buff *skb;
1107
1108 while (n_frames > 0) {
1109 skb = skb_dequeue(&sta->tx_filtered[ac]);
1110 if (!skb) {
1111 skb = skb_dequeue(
1112 &sta->ps_tx_buf[ac]);
1113 if (skb)
1114 local->total_ps_buffered--;
1115 }
1116 if (!skb)
1117 break;
1118 n_frames--;
Johannes Berg4049e092011-09-29 16:04:32 +02001119 found = true;
Johannes Berg47086fc2011-09-29 16:04:33 +02001120 __skb_queue_tail(&frames, skb);
1121 }
Johannes Berg4049e092011-09-29 16:04:32 +02001122 }
1123
1124 /*
1125 * If the driver has data on more than one TID then
1126 * certainly there's more data if we release just a
1127 * single frame now (from a single TID).
1128 */
Johannes Berg47086fc2011-09-29 16:04:33 +02001129 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1130 hweight16(driver_release_tids) > 1) {
Johannes Berg4049e092011-09-29 16:04:32 +02001131 more_data = true;
1132 driver_release_tids =
1133 BIT(ffs(driver_release_tids) - 1);
1134 break;
Johannes Berg948d8872011-09-29 16:04:29 +02001135 }
1136 }
1137
Johannes Berg948d8872011-09-29 16:04:29 +02001138 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1139 !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1140 more_data = true;
1141 break;
1142 }
Johannes Bergaf818582009-11-06 11:35:50 +01001143 }
Johannes Bergaf818582009-11-06 11:35:50 +01001144
Johannes Berg4049e092011-09-29 16:04:32 +02001145 if (!found) {
Johannes Bergce662b442011-09-29 16:04:34 +02001146 int tid;
Johannes Berg4049e092011-09-29 16:04:32 +02001147
Johannes Bergce662b442011-09-29 16:04:34 +02001148 /*
1149 * For PS-Poll, this can only happen due to a race condition
1150 * when we set the TIM bit and the station notices it, but
1151 * before it can poll for the frame we expire it.
1152 *
1153 * For uAPSD, this is said in the standard (11.2.1.5 h):
1154 * At each unscheduled SP for a non-AP STA, the AP shall
1155 * attempt to transmit at least one MSDU or MMPDU, but no
1156 * more than the value specified in the Max SP Length field
1157 * in the QoS Capability element from delivery-enabled ACs,
1158 * that are destined for the non-AP STA.
1159 *
1160 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1161 */
1162
1163 /* This will evaluate to 1, 3, 5 or 7. */
1164 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1165
Johannes Berg40b96402011-09-29 16:04:38 +02001166 ieee80211_send_null_response(sdata, sta, tid, reason);
Johannes Berg4049e092011-09-29 16:04:32 +02001167 return;
1168 }
1169
Johannes Berg47086fc2011-09-29 16:04:33 +02001170 if (!driver_release_tids) {
1171 struct sk_buff_head pending;
1172 struct sk_buff *skb;
Johannes Berg40b96402011-09-29 16:04:38 +02001173 int num = 0;
1174 u16 tids = 0;
Johannes Bergaf818582009-11-06 11:35:50 +01001175
Johannes Berg47086fc2011-09-29 16:04:33 +02001176 skb_queue_head_init(&pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001177
Johannes Berg47086fc2011-09-29 16:04:33 +02001178 while ((skb = __skb_dequeue(&frames))) {
1179 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1180 struct ieee80211_hdr *hdr = (void *) skb->data;
Johannes Berg40b96402011-09-29 16:04:38 +02001181 u8 *qoshdr = NULL;
1182
1183 num++;
Johannes Bergaf818582009-11-06 11:35:50 +01001184
Johannes Berg47086fc2011-09-29 16:04:33 +02001185 /*
1186 * Tell TX path to send this frame even though the
1187 * STA may still remain is PS mode after this frame
1188 * exchange.
1189 */
Johannes Berg02f2f1a2012-02-27 12:18:30 +01001190 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
Johannes Bergaf818582009-11-06 11:35:50 +01001191
Johannes Berg47086fc2011-09-29 16:04:33 +02001192 /*
1193 * Use MoreData flag to indicate whether there are
1194 * more buffered frames for this STA
1195 */
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001196 if (more_data || !skb_queue_empty(&frames))
Johannes Berg47086fc2011-09-29 16:04:33 +02001197 hdr->frame_control |=
1198 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
Janusz.Dziedzic@tieto.com24b9c372011-11-07 09:47:47 +02001199 else
1200 hdr->frame_control &=
1201 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
Johannes Berg47086fc2011-09-29 16:04:33 +02001202
Johannes Berg40b96402011-09-29 16:04:38 +02001203 if (ieee80211_is_data_qos(hdr->frame_control) ||
1204 ieee80211_is_qos_nullfunc(hdr->frame_control))
1205 qoshdr = ieee80211_get_qos_ctl(hdr);
1206
1207 /* set EOSP for the frame */
Johannes Berg47086fc2011-09-29 16:04:33 +02001208 if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
Johannes Berg40b96402011-09-29 16:04:38 +02001209 qoshdr && skb_queue_empty(&frames))
1210 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
Johannes Berg47086fc2011-09-29 16:04:33 +02001211
Johannes Bergdeeaee12011-09-29 16:04:35 +02001212 info->flags |= IEEE80211_TX_STATUS_EOSP |
1213 IEEE80211_TX_CTL_REQ_TX_STATUS;
1214
Johannes Berg40b96402011-09-29 16:04:38 +02001215 if (qoshdr)
1216 tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
1217 else
1218 tids |= BIT(0);
1219
Johannes Berg47086fc2011-09-29 16:04:33 +02001220 __skb_queue_tail(&pending, skb);
1221 }
1222
Johannes Berg40b96402011-09-29 16:04:38 +02001223 drv_allow_buffered_frames(local, sta, tids, num,
1224 reason, more_data);
1225
Johannes Berg47086fc2011-09-29 16:04:33 +02001226 ieee80211_add_pending_skbs(local, &pending);
Johannes Bergaf818582009-11-06 11:35:50 +01001227
Johannes Bergc868cb32011-09-29 16:04:27 +02001228 sta_info_recalc_tim(sta);
Johannes Bergaf818582009-11-06 11:35:50 +01001229 } else {
1230 /*
Johannes Berg4049e092011-09-29 16:04:32 +02001231 * We need to release a frame that is buffered somewhere in the
1232 * driver ... it'll have to handle that.
1233 * Note that, as per the comment above, it'll also have to see
1234 * if there is more than just one frame on the specific TID that
1235 * we're releasing from, and it needs to set the more-data bit
1236 * accordingly if we tell it that there's no more data. If we do
1237 * tell it there's more data, then of course the more-data bit
1238 * needs to be set anyway.
Johannes Bergaf818582009-11-06 11:35:50 +01001239 */
Johannes Berg4049e092011-09-29 16:04:32 +02001240 drv_release_buffered_frames(local, sta, driver_release_tids,
Johannes Berg47086fc2011-09-29 16:04:33 +02001241 n_frames, reason, more_data);
Johannes Berg4049e092011-09-29 16:04:32 +02001242
1243 /*
1244 * Note that we don't recalculate the TIM bit here as it would
1245 * most likely have no effect at all unless the driver told us
1246 * that the TID became empty before returning here from the
1247 * release function.
1248 * Either way, however, when the driver tells us that the TID
1249 * became empty we'll do the TIM recalculation.
1250 */
Johannes Bergaf818582009-11-06 11:35:50 +01001251 }
1252}
1253
Johannes Bergaf818582009-11-06 11:35:50 +01001254void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1255{
Johannes Berg47086fc2011-09-29 16:04:33 +02001256 u8 ignore_for_response = sta->sta.uapsd_queues;
Johannes Bergaf818582009-11-06 11:35:50 +01001257
Johannes Berg47086fc2011-09-29 16:04:33 +02001258 /*
1259 * If all ACs are delivery-enabled then we should reply
1260 * from any of them, if only some are enabled we reply
1261 * only from the non-enabled ones.
1262 */
1263 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1264 ignore_for_response = 0;
1265
1266 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1267 IEEE80211_FRAME_RELEASE_PSPOLL);
1268}
1269
1270void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1271{
1272 int n_frames = sta->sta.max_sp;
1273 u8 delivery_enabled = sta->sta.uapsd_queues;
1274
1275 /*
1276 * If we ever grow support for TSPEC this might happen if
1277 * the TSPEC update from hostapd comes in between a trigger
1278 * frame setting WLAN_STA_UAPSD in the RX path and this
1279 * actually getting called.
1280 */
1281 if (!delivery_enabled)
1282 return;
1283
Johannes Berg47086fc2011-09-29 16:04:33 +02001284 switch (sta->sta.max_sp) {
1285 case 1:
1286 n_frames = 2;
1287 break;
1288 case 2:
1289 n_frames = 4;
1290 break;
1291 case 3:
1292 n_frames = 6;
1293 break;
1294 case 0:
1295 /* XXX: what is a good value? */
1296 n_frames = 8;
1297 break;
Johannes Bergaf818582009-11-06 11:35:50 +01001298 }
Johannes Bergaf818582009-11-06 11:35:50 +01001299
Johannes Berg47086fc2011-09-29 16:04:33 +02001300 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1301 IEEE80211_FRAME_RELEASE_UAPSD);
Johannes Bergaf818582009-11-06 11:35:50 +01001302}
1303
1304void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1305 struct ieee80211_sta *pubsta, bool block)
1306{
1307 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1308
Johannes Bergb5878a22010-04-07 16:48:40 +02001309 trace_api_sta_block_awake(sta->local, pubsta, block);
1310
Johannes Bergaf818582009-11-06 11:35:50 +01001311 if (block)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001312 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1313 else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
Johannes Bergaf818582009-11-06 11:35:50 +01001314 ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1315}
1316EXPORT_SYMBOL(ieee80211_sta_block_awake);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001317
Johannes Berg37fbd902011-09-29 16:04:39 +02001318void ieee80211_sta_eosp_irqsafe(struct ieee80211_sta *pubsta)
1319{
1320 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1321 struct ieee80211_local *local = sta->local;
1322 struct sk_buff *skb;
1323 struct skb_eosp_msg_data *data;
1324
1325 trace_api_eosp(local, pubsta);
1326
1327 skb = alloc_skb(0, GFP_ATOMIC);
1328 if (!skb) {
1329 /* too bad ... but race is better than loss */
1330 clear_sta_flag(sta, WLAN_STA_SP);
1331 return;
1332 }
1333
1334 data = (void *)skb->cb;
1335 memcpy(data->sta, pubsta->addr, ETH_ALEN);
1336 memcpy(data->iface, sta->sdata->vif.addr, ETH_ALEN);
1337 skb->pkt_type = IEEE80211_EOSP_MSG;
1338 skb_queue_tail(&local->skb_queue, skb);
1339 tasklet_schedule(&local->tasklet);
1340}
1341EXPORT_SYMBOL(ieee80211_sta_eosp_irqsafe);
1342
Johannes Berg042ec452011-09-29 16:04:26 +02001343void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1344 u8 tid, bool buffered)
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001345{
1346 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1347
Johannes Berg948d8872011-09-29 16:04:29 +02001348 if (WARN_ON(tid >= STA_TID_NUM))
Johannes Berg042ec452011-09-29 16:04:26 +02001349 return;
1350
Johannes Berg948d8872011-09-29 16:04:29 +02001351 if (buffered)
1352 set_bit(tid, &sta->driver_buffered_tids);
1353 else
1354 clear_bit(tid, &sta->driver_buffered_tids);
1355
Johannes Bergc868cb32011-09-29 16:04:27 +02001356 sta_info_recalc_tim(sta);
Felix Fietkaudcf55fb2011-04-17 17:45:00 +02001357}
Johannes Berg042ec452011-09-29 16:04:26 +02001358EXPORT_SYMBOL(ieee80211_sta_set_buffered);
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001359
Johannes Berg83d5cc02012-01-12 09:31:10 +01001360int sta_info_move_state(struct sta_info *sta,
1361 enum ieee80211_sta_state new_state)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001362{
Johannes Berg8bf11d82011-12-15 11:17:37 +01001363 might_sleep();
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001364
1365 if (sta->sta_state == new_state)
1366 return 0;
1367
Johannes Bergf09603a2012-01-20 13:55:21 +01001368 /* check allowed transitions first */
1369
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001370 switch (new_state) {
1371 case IEEE80211_STA_NONE:
Johannes Bergf09603a2012-01-20 13:55:21 +01001372 if (sta->sta_state != IEEE80211_STA_AUTH)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001373 return -EINVAL;
1374 break;
1375 case IEEE80211_STA_AUTH:
Johannes Bergf09603a2012-01-20 13:55:21 +01001376 if (sta->sta_state != IEEE80211_STA_NONE &&
1377 sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001378 return -EINVAL;
1379 break;
1380 case IEEE80211_STA_ASSOC:
Johannes Bergf09603a2012-01-20 13:55:21 +01001381 if (sta->sta_state != IEEE80211_STA_AUTH &&
1382 sta->sta_state != IEEE80211_STA_AUTHORIZED)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001383 return -EINVAL;
1384 break;
1385 case IEEE80211_STA_AUTHORIZED:
Johannes Bergf09603a2012-01-20 13:55:21 +01001386 if (sta->sta_state != IEEE80211_STA_ASSOC)
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001387 return -EINVAL;
1388 break;
1389 default:
1390 WARN(1, "invalid state %d", new_state);
1391 return -EINVAL;
1392 }
1393
Felix Fietkau79027592012-02-07 12:45:44 +01001394#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001395 printk(KERN_DEBUG "%s: moving STA %pM to state %d\n",
1396 sta->sdata->name, sta->sta.addr, new_state);
Felix Fietkau79027592012-02-07 12:45:44 +01001397#endif
Johannes Bergf09603a2012-01-20 13:55:21 +01001398
1399 /*
1400 * notify the driver before the actual changes so it can
1401 * fail the transition
1402 */
1403 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1404 int err = drv_sta_state(sta->local, sta->sdata, sta,
1405 sta->sta_state, new_state);
1406 if (err)
1407 return err;
1408 }
1409
1410 /* reflect the change in all state variables */
1411
1412 switch (new_state) {
1413 case IEEE80211_STA_NONE:
1414 if (sta->sta_state == IEEE80211_STA_AUTH)
1415 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1416 break;
1417 case IEEE80211_STA_AUTH:
1418 if (sta->sta_state == IEEE80211_STA_NONE)
1419 set_bit(WLAN_STA_AUTH, &sta->_flags);
1420 else if (sta->sta_state == IEEE80211_STA_ASSOC)
1421 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1422 break;
1423 case IEEE80211_STA_ASSOC:
1424 if (sta->sta_state == IEEE80211_STA_AUTH) {
1425 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1426 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1427 if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
1428 atomic_dec(&sta->sdata->u.ap.num_sta_authorized);
1429 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1430 }
1431 break;
1432 case IEEE80211_STA_AUTHORIZED:
1433 if (sta->sta_state == IEEE80211_STA_ASSOC) {
1434 if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
1435 atomic_inc(&sta->sdata->u.ap.num_sta_authorized);
1436 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1437 }
1438 break;
1439 default:
1440 break;
1441 }
1442
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001443 sta->sta_state = new_state;
1444
1445 return 0;
1446}