blob: bd11753c15254edaf734539b9b3a551d0ed0123c [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>
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>
12#include <linux/netdevice.h>
13#include <linux/types.h>
14#include <linux/slab.h>
15#include <linux/skbuff.h>
16#include <linux/if_arp.h>
Johannes Berg0d174402007-12-17 15:07:43 +010017#include <linux/timer.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010018#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070019
20#include <net/mac80211.h>
21#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020022#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040023#include "rate.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070024#include "sta_info.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070025#include "debugfs_sta.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010026#include "mesh.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070027
Johannes Bergd0709a62008-02-25 16:27:46 +010028/**
29 * DOC: STA information lifetime rules
30 *
31 * STA info structures (&struct sta_info) are managed in a hash table
32 * for faster lookup and a list for iteration. They are managed using
33 * RCU, i.e. access to the list and hash table is protected by RCU.
34 *
Johannes Berg34e89502010-02-03 13:59:58 +010035 * Upon allocating a STA info structure with sta_info_alloc(), the caller
36 * owns that structure. It must then insert it into the hash table using
37 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
38 * case (which acquires an rcu read section but must not be called from
39 * within one) will the pointer still be valid after the call. Note that
40 * the caller may not do much with the STA info before inserting it, in
41 * particular, it may not start any mesh peer link management or add
42 * encryption keys.
Johannes Berg93e5deb2008-04-01 15:21:00 +020043 *
44 * When the insertion fails (sta_info_insert()) returns non-zero), the
45 * structure will have been freed by sta_info_insert()!
Johannes Bergd0709a62008-02-25 16:27:46 +010046 *
Johannes Berg34e89502010-02-03 13:59:58 +010047 * Station entries are added by mac80211 when you establish a link with a
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040048 * peer. This means different things for the different type of interfaces
49 * we support. For a regular station this mean we add the AP sta when we
50 * receive an assocation response from the AP. For IBSS this occurs when
Johannes Berg34e89502010-02-03 13:59:58 +010051 * get to know about a peer on the same IBSS. For WDS we add the sta for
52 * the peer imediately upon device open. When using AP mode we add stations
53 * for each respective station upon request from userspace through nl80211.
Luis R. Rodriguez7e189a12009-06-02 18:38:14 -040054 *
Johannes Berg34e89502010-02-03 13:59:58 +010055 * In order to remove a STA info structure, various sta_info_destroy_*()
56 * calls are available.
Johannes Bergd0709a62008-02-25 16:27:46 +010057 *
Johannes Berg34e89502010-02-03 13:59:58 +010058 * There is no concept of ownership on a STA entry, each structure is
59 * owned by the global hash table/list until it is removed. All users of
60 * the structure need to be RCU protected so that the structure won't be
61 * freed before they are done using it.
Johannes Bergd0709a62008-02-25 16:27:46 +010062 */
Jiri Bencf0706e82007-05-05 11:45:53 -070063
64/* Caller must hold local->sta_lock */
Michael Wube8755e2007-07-27 15:43:23 +020065static int sta_info_hash_del(struct ieee80211_local *local,
66 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070067{
68 struct sta_info *s;
69
Johannes Berg17741cd2008-09-11 00:02:02 +020070 s = local->sta_hash[STA_HASH(sta->sta.addr)];
Jiri Bencf0706e82007-05-05 11:45:53 -070071 if (!s)
Michael Wube8755e2007-07-27 15:43:23 +020072 return -ENOENT;
73 if (s == sta) {
Johannes Berg17741cd2008-09-11 00:02:02 +020074 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
Johannes Bergd0709a62008-02-25 16:27:46 +010075 s->hnext);
Michael Wube8755e2007-07-27 15:43:23 +020076 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -070077 }
78
Michael Wube8755e2007-07-27 15:43:23 +020079 while (s->hnext && s->hnext != sta)
Jiri Bencf0706e82007-05-05 11:45:53 -070080 s = s->hnext;
Michael Wube8755e2007-07-27 15:43:23 +020081 if (s->hnext) {
Johannes Bergd0709a62008-02-25 16:27:46 +010082 rcu_assign_pointer(s->hnext, sta->hnext);
Michael Wube8755e2007-07-27 15:43:23 +020083 return 0;
84 }
Jiri Bencf0706e82007-05-05 11:45:53 -070085
Michael Wube8755e2007-07-27 15:43:23 +020086 return -ENOENT;
Jiri Bencf0706e82007-05-05 11:45:53 -070087}
88
Johannes Bergd0709a62008-02-25 16:27:46 +010089/* protected by RCU */
Johannes Bergabe60632009-11-25 17:46:18 +010090struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
91 const u8 *addr)
Johannes Berg43ba7e92008-02-21 14:09:30 +010092{
Johannes Bergabe60632009-11-25 17:46:18 +010093 struct ieee80211_local *local = sdata->local;
Johannes Berg43ba7e92008-02-21 14:09:30 +010094 struct sta_info *sta;
95
Johannes Bergd0709a62008-02-25 16:27:46 +010096 sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
Johannes Berg43ba7e92008-02-21 14:09:30 +010097 while (sta) {
Johannes Bergabe60632009-11-25 17:46:18 +010098 if (sta->sdata == sdata &&
99 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
Johannes Berg43ba7e92008-02-21 14:09:30 +0100100 break;
Johannes Bergd0709a62008-02-25 16:27:46 +0100101 sta = rcu_dereference(sta->hnext);
Johannes Berg43ba7e92008-02-21 14:09:30 +0100102 }
103 return sta;
104}
105
Felix Fietkau0e5ded52010-01-08 18:10:58 +0100106/*
107 * Get sta info either from the specified interface
108 * or from one of its vlans
109 */
110struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
111 const u8 *addr)
112{
113 struct ieee80211_local *local = sdata->local;
114 struct sta_info *sta;
115
116 sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
117 while (sta) {
118 if ((sta->sdata == sdata ||
119 sta->sdata->bss == sdata->bss) &&
120 memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
121 break;
122 sta = rcu_dereference(sta->hnext);
123 }
124 return sta;
125}
126
Johannes Berg3b53fde82009-11-16 12:00:37 +0100127struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
128 int idx)
Luis Carlos Coboee385852008-02-23 15:17:11 +0100129{
Johannes Berg3b53fde82009-11-16 12:00:37 +0100130 struct ieee80211_local *local = sdata->local;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100131 struct sta_info *sta;
132 int i = 0;
133
Johannes Bergd0709a62008-02-25 16:27:46 +0100134 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg3b53fde82009-11-16 12:00:37 +0100135 if (sdata != sta->sdata)
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800136 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100137 if (i < idx) {
138 ++i;
139 continue;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100140 }
Luis Carlos Cobo2a8ca292008-02-29 17:51:25 -0800141 return sta;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100142 }
Luis Carlos Coboee385852008-02-23 15:17:11 +0100143
144 return NULL;
145}
Jiri Bencf0706e82007-05-05 11:45:53 -0700146
Johannes Berg93e5deb2008-04-01 15:21:00 +0200147/**
148 * __sta_info_free - internal STA free helper
149 *
Randy Dunlap6ef307b2008-07-03 13:52:18 -0700150 * @local: pointer to the global information
Johannes Berg93e5deb2008-04-01 15:21:00 +0200151 * @sta: STA info to free
152 *
153 * This function must undo everything done by sta_info_alloc()
154 * that may happen before sta_info_insert().
155 */
156static void __sta_info_free(struct ieee80211_local *local,
157 struct sta_info *sta)
158{
Johannes Bergaf65cd962009-11-17 18:18:36 +0100159 if (sta->rate_ctrl) {
160 rate_control_free_sta(sta);
161 rate_control_put(sta->rate_ctrl);
162 }
Johannes Berg93e5deb2008-04-01 15:21:00 +0200163
164#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700165 printk(KERN_DEBUG "%s: Destroyed STA %pM\n",
166 wiphy_name(local->hw.wiphy), sta->sta.addr);
Johannes Berg93e5deb2008-04-01 15:21:00 +0200167#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
168
169 kfree(sta);
170}
171
Johannes Bergd0709a62008-02-25 16:27:46 +0100172/* Caller must hold local->sta_lock */
173static void sta_info_hash_add(struct ieee80211_local *local,
174 struct sta_info *sta)
Jiri Bencf0706e82007-05-05 11:45:53 -0700175{
Johannes Berg17741cd2008-09-11 00:02:02 +0200176 sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
177 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700178}
Jiri Bencf0706e82007-05-05 11:45:53 -0700179
Johannes Bergaf818582009-11-06 11:35:50 +0100180static void sta_unblock(struct work_struct *wk)
181{
182 struct sta_info *sta;
183
184 sta = container_of(wk, struct sta_info, drv_unblock_wk);
185
186 if (sta->dead)
187 return;
188
189 if (!test_sta_flags(sta, WLAN_STA_PS_STA))
190 ieee80211_sta_ps_deliver_wakeup(sta);
191 else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL))
192 ieee80211_sta_ps_deliver_poll_response(sta);
193}
194
Johannes Bergaf65cd962009-11-17 18:18:36 +0100195static int sta_prepare_rate_control(struct ieee80211_local *local,
196 struct sta_info *sta, gfp_t gfp)
197{
198 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
199 return 0;
200
201 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
202 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
203 &sta->sta, gfp);
204 if (!sta->rate_ctrl_priv) {
205 rate_control_put(sta->rate_ctrl);
206 return -ENOMEM;
207 }
208
209 return 0;
210}
211
Johannes Berg73651ee2008-02-25 16:27:47 +0100212struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
213 u8 *addr, gfp_t gfp)
Jiri Bencf0706e82007-05-05 11:45:53 -0700214{
Johannes Bergd0709a62008-02-25 16:27:46 +0100215 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700216 struct sta_info *sta;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200217 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700218
Johannes Berg17741cd2008-09-11 00:02:02 +0200219 sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
Jiri Bencf0706e82007-05-05 11:45:53 -0700220 if (!sta)
Johannes Berg73651ee2008-02-25 16:27:47 +0100221 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700222
Johannes Berg07346f812008-05-03 01:02:02 +0200223 spin_lock_init(&sta->lock);
Johannes Berg5a9f7b02008-06-18 14:58:09 +0200224 spin_lock_init(&sta->flaglock);
Johannes Bergaf818582009-11-06 11:35:50 +0100225 INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
Johannes Berg07346f812008-05-03 01:02:02 +0200226
Johannes Berg17741cd2008-09-11 00:02:02 +0200227 memcpy(sta->sta.addr, addr, ETH_ALEN);
Johannes Bergd0709a62008-02-25 16:27:46 +0100228 sta->local = local;
229 sta->sdata = sdata;
Jiri Bencf0706e82007-05-05 11:45:53 -0700230
Johannes Bergaf65cd962009-11-17 18:18:36 +0100231 if (sta_prepare_rate_control(local, sta, gfp)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700232 kfree(sta);
Johannes Berg73651ee2008-02-25 16:27:47 +0100233 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700234 }
235
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200236 for (i = 0; i < STA_TID_NUM; i++) {
237 /* timer_to_tid must be initialized with identity mapping to
238 * enable session_timer's data differentiation. refer to
239 * sta_rx_agg_session_timer_expired for useage */
240 sta->timer_to_tid[i] = i;
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200241 /* rx */
242 sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
243 sta->ampdu_mlme.tid_rx[i] = NULL;
244 /* tx */
245 sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
246 sta->ampdu_mlme.tid_tx[i] = NULL;
247 sta->ampdu_mlme.addba_req_num[i] = 0;
Ron Rindjunsky16c5f152007-12-25 17:00:34 +0200248 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700249 skb_queue_head_init(&sta->ps_tx_buf);
250 skb_queue_head_init(&sta->tx_filtered);
Johannes Berg73651ee2008-02-25 16:27:47 +0100251
Senthil Balasubramaniancccaec92009-05-14 18:42:08 +0530252 for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
253 sta->last_seq_ctrl[i] = cpu_to_le16(USHORT_MAX);
254
Johannes Berg73651ee2008-02-25 16:27:47 +0100255#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700256 printk(KERN_DEBUG "%s: Allocated STA %pM\n",
257 wiphy_name(local->hw.wiphy), sta->sta.addr);
Johannes Berg73651ee2008-02-25 16:27:47 +0100258#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
259
Johannes Berg03e44972008-02-27 09:56:40 +0100260#ifdef CONFIG_MAC80211_MESH
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800261 sta->plink_state = PLINK_LISTEN;
Johannes Berg03e44972008-02-27 09:56:40 +0100262 init_timer(&sta->plink_timer);
263#endif
264
Johannes Berg73651ee2008-02-25 16:27:47 +0100265 return sta;
266}
267
Johannes Berg34e89502010-02-03 13:59:58 +0100268static int sta_info_finish_insert(struct sta_info *sta, bool async)
Johannes Berg73651ee2008-02-25 16:27:47 +0100269{
270 struct ieee80211_local *local = sta->local;
271 struct ieee80211_sub_if_data *sdata = sta->sdata;
Johannes Berg98b62182009-12-23 13:15:44 +0100272 struct station_info sinfo;
Johannes Berg73651ee2008-02-25 16:27:47 +0100273 unsigned long flags;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200274 int err = 0;
Johannes Berg73651ee2008-02-25 16:27:47 +0100275
Johannes Berg34e89502010-02-03 13:59:58 +0100276 WARN_ON(!mutex_is_locked(&local->sta_mtx));
277
278 /* notify driver */
279 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
280 sdata = container_of(sdata->bss,
281 struct ieee80211_sub_if_data,
282 u.ap);
283 err = drv_sta_add(local, sdata, &sta->sta);
284 if (err) {
285 if (!async)
286 return err;
287 printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to driver (%d)"
288 " - keeping it anyway.\n",
289 sdata->name, sta->sta.addr, err);
290 } else {
291 sta->uploaded = true;
292#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
293 if (async)
294 printk(KERN_DEBUG "%s: Finished adding IBSS STA %pM\n",
295 wiphy_name(local->hw.wiphy), sta->sta.addr);
296#endif
297 }
298
299 sdata = sta->sdata;
300
301 if (!async) {
302 local->num_sta++;
303 local->sta_generation++;
304 smp_mb();
305
306 /* make the station visible */
307 spin_lock_irqsave(&local->sta_lock, flags);
308 sta_info_hash_add(local, sta);
309 spin_unlock_irqrestore(&local->sta_lock, flags);
310 }
311
312 list_add(&sta->list, &local->sta_list);
313
314 ieee80211_sta_debugfs_add(sta);
315 rate_control_add_sta_debugfs(sta);
316
317 sinfo.filled = 0;
318 sinfo.generation = local->sta_generation;
319 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
320
321
322 return 0;
323}
324
325static void sta_info_finish_pending(struct ieee80211_local *local)
326{
327 struct sta_info *sta;
328 unsigned long flags;
329
330 spin_lock_irqsave(&local->sta_lock, flags);
331 while (!list_empty(&local->sta_pending_list)) {
332 sta = list_first_entry(&local->sta_pending_list,
333 struct sta_info, list);
334 list_del(&sta->list);
335 spin_unlock_irqrestore(&local->sta_lock, flags);
336
337 sta_info_finish_insert(sta, true);
338
339 spin_lock_irqsave(&local->sta_lock, flags);
340 }
341 spin_unlock_irqrestore(&local->sta_lock, flags);
342}
343
344static void sta_info_finish_work(struct work_struct *work)
345{
346 struct ieee80211_local *local =
347 container_of(work, struct ieee80211_local, sta_finish_work);
348
349 mutex_lock(&local->sta_mtx);
350 sta_info_finish_pending(local);
351 mutex_unlock(&local->sta_mtx);
352}
353
354int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
355{
356 struct ieee80211_local *local = sta->local;
357 struct ieee80211_sub_if_data *sdata = sta->sdata;
358 unsigned long flags;
359 int err = 0;
360
Johannes Berg03e44972008-02-27 09:56:40 +0100361 /*
362 * Can't be a WARN_ON because it can be triggered through a race:
363 * something inserts a STA (on one CPU) without holding the RTNL
364 * and another CPU turns off the net device.
365 */
Johannes Berg9607e6b2009-12-23 13:15:31 +0100366 if (unlikely(!ieee80211_sdata_running(sdata))) {
Johannes Berg93e5deb2008-04-01 15:21:00 +0200367 err = -ENETDOWN;
Johannes Berg34e89502010-02-03 13:59:58 +0100368 rcu_read_lock();
Johannes Berg93e5deb2008-04-01 15:21:00 +0200369 goto out_free;
370 }
Johannes Berg03e44972008-02-27 09:56:40 +0100371
Johannes Berg47846c92009-11-25 17:46:19 +0100372 if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
Johannes Bergc6a1fa12008-10-07 12:04:32 +0200373 is_multicast_ether_addr(sta->sta.addr))) {
Johannes Berg93e5deb2008-04-01 15:21:00 +0200374 err = -EINVAL;
Johannes Berg34e89502010-02-03 13:59:58 +0100375 rcu_read_lock();
Johannes Berg93e5deb2008-04-01 15:21:00 +0200376 goto out_free;
377 }
Johannes Berg44213b52008-02-25 16:27:49 +0100378
Johannes Berg34e89502010-02-03 13:59:58 +0100379 /*
380 * In ad-hoc mode, we sometimes need to insert stations
381 * from tasklet context from the RX path. To avoid races,
382 * always do so in that case -- see the comment below.
383 */
384 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
385 spin_lock_irqsave(&local->sta_lock, flags);
386 /* check if STA exists already */
387 if (sta_info_get_bss(sdata, sta->sta.addr)) {
388 spin_unlock_irqrestore(&local->sta_lock, flags);
389 rcu_read_lock();
390 err = -EEXIST;
391 goto out_free;
392 }
393
394 local->num_sta++;
395 local->sta_generation++;
396 smp_mb();
397 sta_info_hash_add(local, sta);
398
399 list_add_tail(&sta->list, &local->sta_pending_list);
400
401 rcu_read_lock();
402 spin_unlock_irqrestore(&local->sta_lock, flags);
403
404#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
405 printk(KERN_DEBUG "%s: Added IBSS STA %pM\n",
406 wiphy_name(local->hw.wiphy), sta->sta.addr);
407#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
408
409 ieee80211_queue_work(&local->hw, &local->sta_finish_work);
410
411 return 0;
412 }
413
414 /*
415 * On first glance, this will look racy, because the code
416 * below this point, which inserts a station with sleeping,
417 * unlocks the sta_lock between checking existence in the
418 * hash table and inserting into it.
419 *
420 * However, it is not racy against itself because it keeps
421 * the mutex locked. It still seems to race against the
422 * above code that atomically inserts the station... That,
423 * however, is not true because the above code can only
424 * be invoked for IBSS interfaces, and the below code will
425 * not be -- and the two do not race against each other as
426 * the hash table also keys off the interface.
427 */
428
429 might_sleep();
430
431 mutex_lock(&local->sta_mtx);
432
Johannes Bergd0709a62008-02-25 16:27:46 +0100433 spin_lock_irqsave(&local->sta_lock, flags);
Johannes Berg43ba7e92008-02-21 14:09:30 +0100434 /* check if STA exists already */
Johannes Berg34e89502010-02-03 13:59:58 +0100435 if (sta_info_get_bss(sdata, sta->sta.addr)) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100436 spin_unlock_irqrestore(&local->sta_lock, flags);
Johannes Berg34e89502010-02-03 13:59:58 +0100437 rcu_read_lock();
Johannes Berg93e5deb2008-04-01 15:21:00 +0200438 err = -EEXIST;
439 goto out_free;
Johannes Berg43ba7e92008-02-21 14:09:30 +0100440 }
Johannes Berg32bfd352007-12-19 01:31:26 +0100441
Johannes Berg34e89502010-02-03 13:59:58 +0100442 spin_unlock_irqrestore(&local->sta_lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +0100443
Johannes Berg34e89502010-02-03 13:59:58 +0100444 err = sta_info_finish_insert(sta, false);
445 if (err) {
446 mutex_unlock(&local->sta_mtx);
447 rcu_read_lock();
448 goto out_free;
Johannes Berg32bfd352007-12-19 01:31:26 +0100449 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100450
Jiri Bencf0706e82007-05-05 11:45:53 -0700451#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700452 printk(KERN_DEBUG "%s: Inserted STA %pM\n",
453 wiphy_name(local->hw.wiphy), sta->sta.addr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700454#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
455
Johannes Berg34e89502010-02-03 13:59:58 +0100456 /* move reference to rcu-protected */
457 rcu_read_lock();
458 mutex_unlock(&local->sta_mtx);
Jiri Bence9f207f2007-05-05 11:46:38 -0700459
Johannes Berg73651ee2008-02-25 16:27:47 +0100460 if (ieee80211_vif_is_mesh(&sdata->vif))
461 mesh_accept_plinks_update(sdata);
462
463 return 0;
Johannes Berg93e5deb2008-04-01 15:21:00 +0200464 out_free:
465 BUG_ON(!err);
466 __sta_info_free(local, sta);
467 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -0700468}
469
Johannes Berg34e89502010-02-03 13:59:58 +0100470int sta_info_insert(struct sta_info *sta)
471{
472 int err = sta_info_insert_rcu(sta);
473
474 rcu_read_unlock();
475
476 return err;
477}
478
Johannes Berg004c8722008-02-20 11:21:35 +0100479static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
480{
481 /*
482 * This format has been mandated by the IEEE specifications,
483 * so this line may not be changed to use the __set_bit() format.
484 */
485 bss->tim[aid / 8] |= (1 << (aid % 8));
486}
487
488static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
489{
490 /*
491 * This format has been mandated by the IEEE specifications,
492 * so this line may not be changed to use the __clear_bit() format.
493 */
494 bss->tim[aid / 8] &= ~(1 << (aid % 8));
495}
496
497static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
498 struct sta_info *sta)
499{
Johannes Berg3e122be2008-07-09 14:40:34 +0200500 BUG_ON(!bss);
501
Johannes Berg17741cd2008-09-11 00:02:02 +0200502 __bss_tim_set(bss, sta->sta.aid);
Johannes Berg3e122be2008-07-09 14:40:34 +0200503
Johannes Bergd0709a62008-02-25 16:27:46 +0100504 if (sta->local->ops->set_tim) {
505 sta->local->tim_in_locked_section = true;
Johannes Berg24487982009-04-23 18:52:52 +0200506 drv_set_tim(sta->local, &sta->sta, true);
Johannes Bergd0709a62008-02-25 16:27:46 +0100507 sta->local->tim_in_locked_section = false;
508 }
Johannes Berg004c8722008-02-20 11:21:35 +0100509}
510
511void sta_info_set_tim_bit(struct sta_info *sta)
512{
Johannes Bergd0709a62008-02-25 16:27:46 +0100513 unsigned long flags;
Johannes Berg004c8722008-02-20 11:21:35 +0100514
Johannes Berg3e122be2008-07-09 14:40:34 +0200515 BUG_ON(!sta->sdata->bss);
516
Johannes Bergd0709a62008-02-25 16:27:46 +0100517 spin_lock_irqsave(&sta->local->sta_lock, flags);
518 __sta_info_set_tim_bit(sta->sdata->bss, sta);
519 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
Johannes Berg004c8722008-02-20 11:21:35 +0100520}
521
522static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
523 struct sta_info *sta)
524{
Johannes Berg3e122be2008-07-09 14:40:34 +0200525 BUG_ON(!bss);
526
Johannes Berg17741cd2008-09-11 00:02:02 +0200527 __bss_tim_clear(bss, sta->sta.aid);
Johannes Berg3e122be2008-07-09 14:40:34 +0200528
Johannes Bergd0709a62008-02-25 16:27:46 +0100529 if (sta->local->ops->set_tim) {
530 sta->local->tim_in_locked_section = true;
Johannes Berg24487982009-04-23 18:52:52 +0200531 drv_set_tim(sta->local, &sta->sta, false);
Johannes Bergd0709a62008-02-25 16:27:46 +0100532 sta->local->tim_in_locked_section = false;
533 }
Johannes Berg004c8722008-02-20 11:21:35 +0100534}
535
536void sta_info_clear_tim_bit(struct sta_info *sta)
537{
Johannes Bergd0709a62008-02-25 16:27:46 +0100538 unsigned long flags;
Johannes Berg004c8722008-02-20 11:21:35 +0100539
Johannes Berg3e122be2008-07-09 14:40:34 +0200540 BUG_ON(!sta->sdata->bss);
541
Johannes Bergd0709a62008-02-25 16:27:46 +0100542 spin_lock_irqsave(&sta->local->sta_lock, flags);
543 __sta_info_clear_tim_bit(sta->sdata->bss, sta);
544 spin_unlock_irqrestore(&sta->local->sta_lock, flags);
Johannes Berg004c8722008-02-20 11:21:35 +0100545}
546
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200547static int sta_info_buffer_expired(struct sta_info *sta,
548 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700549{
Johannes Berge039fa42008-05-15 12:55:29 +0200550 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700551 int timeout;
552
553 if (!skb)
554 return 0;
555
Johannes Berge039fa42008-05-15 12:55:29 +0200556 info = IEEE80211_SKB_CB(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700557
558 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200559 timeout = (sta->listen_interval *
560 sta->sdata->vif.bss_conf.beacon_int *
561 32 / 15625) * HZ;
Jiri Bencf0706e82007-05-05 11:45:53 -0700562 if (timeout < STA_TX_BUFFER_EXPIRE)
563 timeout = STA_TX_BUFFER_EXPIRE;
Johannes Berge039fa42008-05-15 12:55:29 +0200564 return time_after(jiffies, info->control.jiffies + timeout);
Jiri Bencf0706e82007-05-05 11:45:53 -0700565}
566
567
568static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
569 struct sta_info *sta)
570{
571 unsigned long flags;
572 struct sk_buff *skb;
Johannes Berg836341a2008-02-20 02:07:21 +0100573 struct ieee80211_sub_if_data *sdata;
Jiri Bencf0706e82007-05-05 11:45:53 -0700574
575 if (skb_queue_empty(&sta->ps_tx_buf))
576 return;
577
578 for (;;) {
579 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
580 skb = skb_peek(&sta->ps_tx_buf);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200581 if (sta_info_buffer_expired(sta, skb))
Jiri Bencf0706e82007-05-05 11:45:53 -0700582 skb = __skb_dequeue(&sta->ps_tx_buf);
Johannes Berg836341a2008-02-20 02:07:21 +0100583 else
Jiri Bencf0706e82007-05-05 11:45:53 -0700584 skb = NULL;
585 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
586
Johannes Berg836341a2008-02-20 02:07:21 +0100587 if (!skb)
Jiri Bencf0706e82007-05-05 11:45:53 -0700588 break;
Johannes Berg836341a2008-02-20 02:07:21 +0100589
Johannes Bergd0709a62008-02-25 16:27:46 +0100590 sdata = sta->sdata;
Johannes Berg836341a2008-02-20 02:07:21 +0100591 local->total_ps_buffered--;
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200592#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700593 printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
594 sta->sta.addr);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200595#endif
Johannes Berg836341a2008-02-20 02:07:21 +0100596 dev_kfree_skb(skb);
597
Johannes Berg004c8722008-02-20 11:21:35 +0100598 if (skb_queue_empty(&sta->ps_tx_buf))
599 sta_info_clear_tim_bit(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700600 }
601}
602
Johannes Berg34e89502010-02-03 13:59:58 +0100603static int __must_check __sta_info_destroy(struct sta_info *sta)
604{
605 struct ieee80211_local *local;
606 struct ieee80211_sub_if_data *sdata;
607 struct sk_buff *skb;
608 unsigned long flags;
609 int ret, i;
610
611 might_sleep();
612
613 if (!sta)
614 return -ENOENT;
615
616 local = sta->local;
617 sdata = sta->sdata;
618
619 spin_lock_irqsave(&local->sta_lock, flags);
620 ret = sta_info_hash_del(local, sta);
621 /* this might still be the pending list ... which is fine */
622 if (!ret)
623 list_del(&sta->list);
624 spin_unlock_irqrestore(&local->sta_lock, flags);
625 if (ret)
626 return ret;
627
628 if (sta->key) {
629 ieee80211_key_free(sta->key);
630 /*
631 * We have only unlinked the key, and actually destroying it
632 * may mean it is removed from hardware which requires that
633 * the key->sta pointer is still valid, so flush the key todo
634 * list here.
Johannes Berg34e89502010-02-03 13:59:58 +0100635 */
636 ieee80211_key_todo();
637
638 WARN_ON(sta->key);
639 }
640
641 sta->dead = true;
642
643 if (test_and_clear_sta_flags(sta,
644 WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
645 BUG_ON(!sdata->bss);
646
647 atomic_dec(&sdata->bss->num_sta_ps);
648 __sta_info_clear_tim_bit(sdata->bss, sta);
649 }
650
651 local->num_sta--;
652 local->sta_generation++;
653
654 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
655 rcu_assign_pointer(sdata->u.vlan.sta, NULL);
656
657 if (sta->uploaded) {
658 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
659 sdata = container_of(sdata->bss,
660 struct ieee80211_sub_if_data,
661 u.ap);
662 drv_sta_remove(local, sdata, &sta->sta);
663 sdata = sta->sdata;
664 }
665
Johannes Berge64b3792010-04-06 11:18:43 +0200666 /*
667 * At this point, after we wait for an RCU grace period,
668 * neither mac80211 nor the driver can reference this
669 * sta struct any more except by still existing timers
670 * associated with this station that we clean up below.
671 */
672 synchronize_rcu();
673
Johannes Berg34e89502010-02-03 13:59:58 +0100674#ifdef CONFIG_MAC80211_MESH
Johannes Berge64b3792010-04-06 11:18:43 +0200675 if (ieee80211_vif_is_mesh(&sdata->vif))
Johannes Berg34e89502010-02-03 13:59:58 +0100676 mesh_accept_plinks_update(sdata);
Johannes Berg34e89502010-02-03 13:59:58 +0100677#endif
678
679#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
680 printk(KERN_DEBUG "%s: Removed STA %pM\n",
681 wiphy_name(local->hw.wiphy), sta->sta.addr);
682#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
683 cancel_work_sync(&sta->drv_unblock_wk);
684
685 rate_control_remove_sta_debugfs(sta);
686 ieee80211_sta_debugfs_remove(sta);
687
688#ifdef CONFIG_MAC80211_MESH
689 if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
690 mesh_plink_deactivate(sta);
691 del_timer_sync(&sta->plink_timer);
692 }
693#endif
694
695 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
696 local->total_ps_buffered--;
697 dev_kfree_skb_any(skb);
698 }
699
700 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
701 dev_kfree_skb_any(skb);
702
703 for (i = 0; i < STA_TID_NUM; i++) {
704 struct tid_ampdu_rx *tid_rx;
705 struct tid_ampdu_tx *tid_tx;
706
707 spin_lock_bh(&sta->lock);
708 tid_rx = sta->ampdu_mlme.tid_rx[i];
709 /* Make sure timer won't free the tid_rx struct, see below */
710 if (tid_rx)
711 tid_rx->shutdown = true;
712
713 spin_unlock_bh(&sta->lock);
714
715 /*
716 * Outside spinlock - shutdown is true now so that the timer
717 * won't free tid_rx, we have to do that now. Can't let the
718 * timer do it because we have to sync the timer outside the
719 * lock that it takes itself.
720 */
721 if (tid_rx) {
722 del_timer_sync(&tid_rx->session_timer);
723 kfree(tid_rx);
724 }
725
726 /*
727 * No need to do such complications for TX agg sessions, the
728 * path leading to freeing the tid_tx struct goes via a call
729 * from the driver, and thus needs to look up the sta struct
730 * again, which cannot be found when we get here. Hence, we
731 * just need to delete the timer and free the aggregation
732 * info; we won't be telling the peer about it then but that
733 * doesn't matter if we're not talking to it again anyway.
734 */
735 tid_tx = sta->ampdu_mlme.tid_tx[i];
736 if (tid_tx) {
737 del_timer_sync(&tid_tx->addba_resp_timer);
738 /*
739 * STA removed while aggregation session being
740 * started? Bit odd, but purge frames anyway.
741 */
742 skb_queue_purge(&tid_tx->pending);
743 kfree(tid_tx);
744 }
745 }
746
747 __sta_info_free(local, sta);
748
749 return 0;
750}
751
752int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
753{
754 struct sta_info *sta;
755 int ret;
756
757 mutex_lock(&sdata->local->sta_mtx);
758 sta = sta_info_get(sdata, addr);
759 ret = __sta_info_destroy(sta);
760 mutex_unlock(&sdata->local->sta_mtx);
761
762 return ret;
763}
764
765int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
766 const u8 *addr)
767{
768 struct sta_info *sta;
769 int ret;
770
771 mutex_lock(&sdata->local->sta_mtx);
772 sta = sta_info_get_bss(sdata, addr);
773 ret = __sta_info_destroy(sta);
774 mutex_unlock(&sdata->local->sta_mtx);
775
776 return ret;
777}
Jiri Bencf0706e82007-05-05 11:45:53 -0700778
779static void sta_info_cleanup(unsigned long data)
780{
781 struct ieee80211_local *local = (struct ieee80211_local *) data;
782 struct sta_info *sta;
783
Johannes Bergd0709a62008-02-25 16:27:46 +0100784 rcu_read_lock();
785 list_for_each_entry_rcu(sta, &local->sta_list, list)
Jiri Bencf0706e82007-05-05 11:45:53 -0700786 sta_info_cleanup_expire_buffered(local, sta);
Johannes Bergd0709a62008-02-25 16:27:46 +0100787 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -0700788
Johannes Berg5bb644a2009-05-17 11:40:42 +0200789 if (local->quiescing)
790 return;
791
Johannes Berg0d174402007-12-17 15:07:43 +0100792 local->sta_cleanup.expires =
793 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700794 add_timer(&local->sta_cleanup);
795}
796
797void sta_info_init(struct ieee80211_local *local)
798{
Johannes Bergd0709a62008-02-25 16:27:46 +0100799 spin_lock_init(&local->sta_lock);
Johannes Berg34e89502010-02-03 13:59:58 +0100800 mutex_init(&local->sta_mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700801 INIT_LIST_HEAD(&local->sta_list);
Johannes Berg34e89502010-02-03 13:59:58 +0100802 INIT_LIST_HEAD(&local->sta_pending_list);
803 INIT_WORK(&local->sta_finish_work, sta_info_finish_work);
Jiri Bencf0706e82007-05-05 11:45:53 -0700804
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800805 setup_timer(&local->sta_cleanup, sta_info_cleanup,
806 (unsigned long)local);
Johannes Berg0d174402007-12-17 15:07:43 +0100807 local->sta_cleanup.expires =
808 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700809}
810
811int sta_info_start(struct ieee80211_local *local)
812{
813 add_timer(&local->sta_cleanup);
814 return 0;
815}
816
817void sta_info_stop(struct ieee80211_local *local)
818{
Jiri Bencf0706e82007-05-05 11:45:53 -0700819 del_timer(&local->sta_cleanup);
Michael Wube8755e2007-07-27 15:43:23 +0200820 sta_info_flush(local, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700821}
822
Jiri Bencf0706e82007-05-05 11:45:53 -0700823/**
824 * sta_info_flush - flush matching STA entries from the STA table
Johannes Berg44213b52008-02-25 16:27:49 +0100825 *
826 * Returns the number of removed STA entries.
827 *
Jiri Bencf0706e82007-05-05 11:45:53 -0700828 * @local: local interface data
Johannes Bergd0709a62008-02-25 16:27:46 +0100829 * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
Jiri Bencf0706e82007-05-05 11:45:53 -0700830 */
Johannes Berg44213b52008-02-25 16:27:49 +0100831int sta_info_flush(struct ieee80211_local *local,
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200832 struct ieee80211_sub_if_data *sdata)
Jiri Bencf0706e82007-05-05 11:45:53 -0700833{
834 struct sta_info *sta, *tmp;
Johannes Berg44213b52008-02-25 16:27:49 +0100835 int ret = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700836
Johannes Bergd0709a62008-02-25 16:27:46 +0100837 might_sleep();
838
Johannes Berg34e89502010-02-03 13:59:58 +0100839 mutex_lock(&local->sta_mtx);
Johannes Bergd0709a62008-02-25 16:27:46 +0100840
Johannes Berg34e89502010-02-03 13:59:58 +0100841 sta_info_finish_pending(local);
842
843 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
844 if (!sdata || sdata == sta->sdata)
845 WARN_ON(__sta_info_destroy(sta));
846 }
847 mutex_unlock(&local->sta_mtx);
Johannes Berg44213b52008-02-25 16:27:49 +0100848
849 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -0700850}
Johannes Bergdc6676b2008-03-31 19:23:03 +0200851
Johannes Berg24723d12008-09-11 00:01:46 +0200852void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
853 unsigned long exp_time)
854{
855 struct ieee80211_local *local = sdata->local;
856 struct sta_info *sta, *tmp;
Johannes Berg24723d12008-09-11 00:01:46 +0200857
Johannes Berg34e89502010-02-03 13:59:58 +0100858 mutex_lock(&local->sta_mtx);
Johannes Berg24723d12008-09-11 00:01:46 +0200859 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
860 if (time_after(jiffies, sta->last_rx + exp_time)) {
861#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700862 printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100863 sdata->name, sta->sta.addr);
Johannes Berg24723d12008-09-11 00:01:46 +0200864#endif
Johannes Berg34e89502010-02-03 13:59:58 +0100865 WARN_ON(__sta_info_destroy(sta));
Johannes Berg24723d12008-09-11 00:01:46 +0200866 }
Johannes Berg34e89502010-02-03 13:59:58 +0100867 mutex_unlock(&local->sta_mtx);
Johannes Berg24723d12008-09-11 00:01:46 +0200868}
Johannes Berg17741cd2008-09-11 00:02:02 +0200869
Johannes Berg5ed176e2009-11-04 14:42:28 +0100870struct ieee80211_sta *ieee80211_find_sta_by_hw(struct ieee80211_hw *hw,
871 const u8 *addr)
Johannes Berg17741cd2008-09-11 00:02:02 +0200872{
Johannes Bergabe60632009-11-25 17:46:18 +0100873 struct sta_info *sta, *nxt;
Johannes Berg17741cd2008-09-11 00:02:02 +0200874
Johannes Bergabe60632009-11-25 17:46:18 +0100875 /* Just return a random station ... first in list ... */
876 for_each_sta_info(hw_to_local(hw), addr, sta, nxt)
877 return &sta->sta;
878 return NULL;
Johannes Berg17741cd2008-09-11 00:02:02 +0200879}
Johannes Berg5ed176e2009-11-04 14:42:28 +0100880EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_hw);
881
882struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
883 const u8 *addr)
884{
885 struct ieee80211_sub_if_data *sdata;
886
887 if (!vif)
888 return NULL;
889
890 sdata = vif_to_sdata(vif);
891
892 return ieee80211_find_sta_by_hw(&sdata->local->hw, addr);
893}
Johannes Berg17741cd2008-09-11 00:02:02 +0200894EXPORT_SYMBOL(ieee80211_find_sta);
Johannes Bergaf818582009-11-06 11:35:50 +0100895
896/* powersave support code */
897void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
898{
899 struct ieee80211_sub_if_data *sdata = sta->sdata;
900 struct ieee80211_local *local = sdata->local;
901 int sent, buffered;
902
Johannes Berg12375ef2009-11-25 20:30:31 +0100903 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
Johannes Bergaf818582009-11-06 11:35:50 +0100904
905 if (!skb_queue_empty(&sta->ps_tx_buf))
906 sta_info_clear_tim_bit(sta);
907
908 /* Send all buffered frames to the station */
909 sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
910 buffered = ieee80211_add_pending_skbs(local, &sta->ps_tx_buf);
911 sent += buffered;
912 local->total_ps_buffered -= buffered;
913
914#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
915 printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
Johannes Berg47846c92009-11-25 17:46:19 +0100916 "since STA not sleeping anymore\n", sdata->name,
Johannes Bergaf818582009-11-06 11:35:50 +0100917 sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
918#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
919}
920
921void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
922{
923 struct ieee80211_sub_if_data *sdata = sta->sdata;
924 struct ieee80211_local *local = sdata->local;
925 struct sk_buff *skb;
926 int no_pending_pkts;
927
928 skb = skb_dequeue(&sta->tx_filtered);
929 if (!skb) {
930 skb = skb_dequeue(&sta->ps_tx_buf);
931 if (skb)
932 local->total_ps_buffered--;
933 }
934 no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
935 skb_queue_empty(&sta->ps_tx_buf);
936
937 if (skb) {
938 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
939 struct ieee80211_hdr *hdr =
940 (struct ieee80211_hdr *) skb->data;
941
942 /*
943 * Tell TX path to send this frame even though the STA may
944 * still remain is PS mode after this frame exchange.
945 */
946 info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
947
948#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
949 printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
950 sta->sta.addr, sta->sta.aid,
951 skb_queue_len(&sta->ps_tx_buf));
952#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
953
954 /* Use MoreData flag to indicate whether there are more
955 * buffered frames for this STA */
956 if (no_pending_pkts)
957 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
958 else
959 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
960
961 ieee80211_add_pending_skb(local, skb);
962
963 if (no_pending_pkts)
964 sta_info_clear_tim_bit(sta);
965#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
966 } else {
967 /*
968 * FIXME: This can be the result of a race condition between
969 * us expiring a frame and the station polling for it.
970 * Should we send it a null-func frame indicating we
971 * have nothing buffered for it?
972 */
973 printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
974 "though there are no buffered frames for it\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100975 sdata->name, sta->sta.addr);
Johannes Bergaf818582009-11-06 11:35:50 +0100976#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
977 }
978}
979
980void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
981 struct ieee80211_sta *pubsta, bool block)
982{
983 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
984
985 if (block)
986 set_sta_flags(sta, WLAN_STA_PS_DRIVER);
987 else
988 ieee80211_queue_work(hw, &sta->drv_unblock_wk);
989}
990EXPORT_SYMBOL(ieee80211_sta_block_awake);