]> git.karo-electronics.de Git - linux-beck.git/blob - net/mac80211/sta_info.c
mac80211: let sta_info_get_by_idx get sta by sdata
[linux-beck.git] / net / mac80211 / sta_info.c
1 /*
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>
17 #include <linux/timer.h>
18 #include <linux/rtnetlink.h>
19
20 #include <net/mac80211.h>
21 #include "ieee80211_i.h"
22 #include "driver-ops.h"
23 #include "rate.h"
24 #include "sta_info.h"
25 #include "debugfs_sta.h"
26 #include "mesh.h"
27
28 /**
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  *
35  * Upon allocating a STA info structure with sta_info_alloc(), the caller owns
36  * that structure. It must then either destroy it using sta_info_destroy()
37  * (which is pretty useless) or insert it into the hash table using
38  * sta_info_insert() which demotes the reference from ownership to a regular
39  * RCU-protected reference; if the function is called without protection by an
40  * RCU critical section the reference is instantly invalidated. Note that the
41  * caller may not do much with the STA info before inserting it, in particular,
42  * it may not start any mesh peer link management or add encryption keys.
43  *
44  * When the insertion fails (sta_info_insert()) returns non-zero), the
45  * structure will have been freed by sta_info_insert()!
46  *
47  * sta entries are added by mac80211 when you establish a link with a
48  * 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
51  * we receive a probe response or a beacon from target IBSS network. For
52  * WDS we add the sta for the peer imediately upon device open. When using
53  * AP mode we add stations for each respective station upon request from
54  * userspace through nl80211.
55  *
56  * Because there are debugfs entries for each station, and adding those
57  * must be able to sleep, it is also possible to "pin" a station entry,
58  * that means it can be removed from the hash table but not be freed.
59  * See the comment in __sta_info_unlink() for more information, this is
60  * an internal capability only.
61  *
62  * In order to remove a STA info structure, the caller needs to first
63  * unlink it (sta_info_unlink()) from the list and hash tables and
64  * then destroy it; sta_info_destroy() will wait for an RCU grace period
65  * to elapse before actually freeing it. Due to the pinning and the
66  * possibility of multiple callers trying to remove the same STA info at
67  * the same time, sta_info_unlink() can clear the STA info pointer it is
68  * passed to indicate that the STA info is owned by somebody else now.
69  *
70  * If sta_info_unlink() did not clear the pointer then the caller owns
71  * the STA info structure now and is responsible of destroying it with
72  * a call to sta_info_destroy().
73  *
74  * In all other cases, there is no concept of ownership on a STA entry,
75  * each structure is owned by the global hash table/list until it is
76  * removed. All users of the structure need to be RCU protected so that
77  * the structure won't be freed before they are done using it.
78  */
79
80 /* Caller must hold local->sta_lock */
81 static int sta_info_hash_del(struct ieee80211_local *local,
82                              struct sta_info *sta)
83 {
84         struct sta_info *s;
85
86         s = local->sta_hash[STA_HASH(sta->sta.addr)];
87         if (!s)
88                 return -ENOENT;
89         if (s == sta) {
90                 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
91                                    s->hnext);
92                 return 0;
93         }
94
95         while (s->hnext && s->hnext != sta)
96                 s = s->hnext;
97         if (s->hnext) {
98                 rcu_assign_pointer(s->hnext, sta->hnext);
99                 return 0;
100         }
101
102         return -ENOENT;
103 }
104
105 /* protected by RCU */
106 struct sta_info *sta_info_get(struct ieee80211_local *local, const u8 *addr)
107 {
108         struct sta_info *sta;
109
110         sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
111         while (sta) {
112                 if (memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
113                         break;
114                 sta = rcu_dereference(sta->hnext);
115         }
116         return sta;
117 }
118
119 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
120                                      int idx)
121 {
122         struct ieee80211_local *local = sdata->local;
123         struct sta_info *sta;
124         int i = 0;
125
126         list_for_each_entry_rcu(sta, &local->sta_list, list) {
127                 if (sdata != sta->sdata)
128                         continue;
129                 if (i < idx) {
130                         ++i;
131                         continue;
132                 }
133                 return sta;
134         }
135
136         return NULL;
137 }
138
139 /**
140  * __sta_info_free - internal STA free helper
141  *
142  * @local: pointer to the global information
143  * @sta: STA info to free
144  *
145  * This function must undo everything done by sta_info_alloc()
146  * that may happen before sta_info_insert().
147  */
148 static void __sta_info_free(struct ieee80211_local *local,
149                             struct sta_info *sta)
150 {
151         rate_control_free_sta(sta);
152         rate_control_put(sta->rate_ctrl);
153
154 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
155         printk(KERN_DEBUG "%s: Destroyed STA %pM\n",
156                wiphy_name(local->hw.wiphy), sta->sta.addr);
157 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
158
159         kfree(sta);
160 }
161
162 void sta_info_destroy(struct sta_info *sta)
163 {
164         struct ieee80211_local *local;
165         struct sk_buff *skb;
166         int i;
167
168         might_sleep();
169
170         if (!sta)
171                 return;
172
173         local = sta->local;
174
175         cancel_work_sync(&sta->drv_unblock_wk);
176
177         rate_control_remove_sta_debugfs(sta);
178         ieee80211_sta_debugfs_remove(sta);
179
180 #ifdef CONFIG_MAC80211_MESH
181         if (ieee80211_vif_is_mesh(&sta->sdata->vif))
182                 mesh_plink_deactivate(sta);
183 #endif
184
185         /*
186          * We have only unlinked the key, and actually destroying it
187          * may mean it is removed from hardware which requires that
188          * the key->sta pointer is still valid, so flush the key todo
189          * list here.
190          *
191          * ieee80211_key_todo() will synchronize_rcu() so after this
192          * nothing can reference this sta struct any more.
193          */
194         ieee80211_key_todo();
195
196 #ifdef CONFIG_MAC80211_MESH
197         if (ieee80211_vif_is_mesh(&sta->sdata->vif))
198                 del_timer_sync(&sta->plink_timer);
199 #endif
200
201         while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
202                 local->total_ps_buffered--;
203                 dev_kfree_skb_any(skb);
204         }
205
206         while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
207                 dev_kfree_skb_any(skb);
208
209         for (i = 0; i <  STA_TID_NUM; i++) {
210                 struct tid_ampdu_rx *tid_rx;
211                 struct tid_ampdu_tx *tid_tx;
212
213                 spin_lock_bh(&sta->lock);
214                 tid_rx = sta->ampdu_mlme.tid_rx[i];
215                 /* Make sure timer won't free the tid_rx struct, see below */
216                 if (tid_rx)
217                         tid_rx->shutdown = true;
218
219                 spin_unlock_bh(&sta->lock);
220
221                 /*
222                  * Outside spinlock - shutdown is true now so that the timer
223                  * won't free tid_rx, we have to do that now. Can't let the
224                  * timer do it because we have to sync the timer outside the
225                  * lock that it takes itself.
226                  */
227                 if (tid_rx) {
228                         del_timer_sync(&tid_rx->session_timer);
229                         kfree(tid_rx);
230                 }
231
232                 /*
233                  * No need to do such complications for TX agg sessions, the
234                  * path leading to freeing the tid_tx struct goes via a call
235                  * from the driver, and thus needs to look up the sta struct
236                  * again, which cannot be found when we get here. Hence, we
237                  * just need to delete the timer and free the aggregation
238                  * info; we won't be telling the peer about it then but that
239                  * doesn't matter if we're not talking to it again anyway.
240                  */
241                 tid_tx = sta->ampdu_mlme.tid_tx[i];
242                 if (tid_tx) {
243                         del_timer_sync(&tid_tx->addba_resp_timer);
244                         /*
245                          * STA removed while aggregation session being
246                          * started? Bit odd, but purge frames anyway.
247                          */
248                         skb_queue_purge(&tid_tx->pending);
249                         kfree(tid_tx);
250                 }
251         }
252
253         __sta_info_free(local, sta);
254 }
255
256
257 /* Caller must hold local->sta_lock */
258 static void sta_info_hash_add(struct ieee80211_local *local,
259                               struct sta_info *sta)
260 {
261         sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
262         rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
263 }
264
265 static void sta_unblock(struct work_struct *wk)
266 {
267         struct sta_info *sta;
268
269         sta = container_of(wk, struct sta_info, drv_unblock_wk);
270
271         if (sta->dead)
272                 return;
273
274         if (!test_sta_flags(sta, WLAN_STA_PS_STA))
275                 ieee80211_sta_ps_deliver_wakeup(sta);
276         else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL))
277                 ieee80211_sta_ps_deliver_poll_response(sta);
278 }
279
280 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
281                                 u8 *addr, gfp_t gfp)
282 {
283         struct ieee80211_local *local = sdata->local;
284         struct sta_info *sta;
285         int i;
286
287         sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
288         if (!sta)
289                 return NULL;
290
291         spin_lock_init(&sta->lock);
292         spin_lock_init(&sta->flaglock);
293         INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
294
295         memcpy(sta->sta.addr, addr, ETH_ALEN);
296         sta->local = local;
297         sta->sdata = sdata;
298
299         sta->rate_ctrl = rate_control_get(local->rate_ctrl);
300         sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
301                                                      &sta->sta, gfp);
302         if (!sta->rate_ctrl_priv) {
303                 rate_control_put(sta->rate_ctrl);
304                 kfree(sta);
305                 return NULL;
306         }
307
308         for (i = 0; i < STA_TID_NUM; i++) {
309                 /* timer_to_tid must be initialized with identity mapping to
310                  * enable session_timer's data differentiation. refer to
311                  * sta_rx_agg_session_timer_expired for useage */
312                 sta->timer_to_tid[i] = i;
313                 /* rx */
314                 sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
315                 sta->ampdu_mlme.tid_rx[i] = NULL;
316                 /* tx */
317                 sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
318                 sta->ampdu_mlme.tid_tx[i] = NULL;
319                 sta->ampdu_mlme.addba_req_num[i] = 0;
320         }
321         skb_queue_head_init(&sta->ps_tx_buf);
322         skb_queue_head_init(&sta->tx_filtered);
323
324         for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
325                 sta->last_seq_ctrl[i] = cpu_to_le16(USHORT_MAX);
326
327 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
328         printk(KERN_DEBUG "%s: Allocated STA %pM\n",
329                wiphy_name(local->hw.wiphy), sta->sta.addr);
330 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
331
332 #ifdef CONFIG_MAC80211_MESH
333         sta->plink_state = PLINK_LISTEN;
334         init_timer(&sta->plink_timer);
335 #endif
336
337         return sta;
338 }
339
340 int sta_info_insert(struct sta_info *sta)
341 {
342         struct ieee80211_local *local = sta->local;
343         struct ieee80211_sub_if_data *sdata = sta->sdata;
344         unsigned long flags;
345         int err = 0;
346
347         /*
348          * Can't be a WARN_ON because it can be triggered through a race:
349          * something inserts a STA (on one CPU) without holding the RTNL
350          * and another CPU turns off the net device.
351          */
352         if (unlikely(!netif_running(sdata->dev))) {
353                 err = -ENETDOWN;
354                 goto out_free;
355         }
356
357         if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->dev->dev_addr) == 0 ||
358                     is_multicast_ether_addr(sta->sta.addr))) {
359                 err = -EINVAL;
360                 goto out_free;
361         }
362
363         spin_lock_irqsave(&local->sta_lock, flags);
364         /* check if STA exists already */
365         if (sta_info_get(local, sta->sta.addr)) {
366                 spin_unlock_irqrestore(&local->sta_lock, flags);
367                 err = -EEXIST;
368                 goto out_free;
369         }
370         list_add(&sta->list, &local->sta_list);
371         local->sta_generation++;
372         local->num_sta++;
373         sta_info_hash_add(local, sta);
374
375         /* notify driver */
376         if (local->ops->sta_notify) {
377                 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
378                         sdata = container_of(sdata->bss,
379                                              struct ieee80211_sub_if_data,
380                                              u.ap);
381
382                 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_ADD, &sta->sta);
383                 sdata = sta->sdata;
384         }
385
386 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
387         printk(KERN_DEBUG "%s: Inserted STA %pM\n",
388                wiphy_name(local->hw.wiphy), sta->sta.addr);
389 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
390
391         spin_unlock_irqrestore(&local->sta_lock, flags);
392
393 #ifdef CONFIG_MAC80211_DEBUGFS
394         /*
395          * Debugfs entry adding might sleep, so schedule process
396          * context task for adding entry for STAs that do not yet
397          * have one.
398          * NOTE: due to auto-freeing semantics this may only be done
399          *       if the insertion is successful!
400          */
401         schedule_work(&local->sta_debugfs_add);
402 #endif
403
404         if (ieee80211_vif_is_mesh(&sdata->vif))
405                 mesh_accept_plinks_update(sdata);
406
407         return 0;
408  out_free:
409         BUG_ON(!err);
410         __sta_info_free(local, sta);
411         return err;
412 }
413
414 static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
415 {
416         /*
417          * This format has been mandated by the IEEE specifications,
418          * so this line may not be changed to use the __set_bit() format.
419          */
420         bss->tim[aid / 8] |= (1 << (aid % 8));
421 }
422
423 static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
424 {
425         /*
426          * This format has been mandated by the IEEE specifications,
427          * so this line may not be changed to use the __clear_bit() format.
428          */
429         bss->tim[aid / 8] &= ~(1 << (aid % 8));
430 }
431
432 static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
433                                    struct sta_info *sta)
434 {
435         BUG_ON(!bss);
436
437         __bss_tim_set(bss, sta->sta.aid);
438
439         if (sta->local->ops->set_tim) {
440                 sta->local->tim_in_locked_section = true;
441                 drv_set_tim(sta->local, &sta->sta, true);
442                 sta->local->tim_in_locked_section = false;
443         }
444 }
445
446 void sta_info_set_tim_bit(struct sta_info *sta)
447 {
448         unsigned long flags;
449
450         BUG_ON(!sta->sdata->bss);
451
452         spin_lock_irqsave(&sta->local->sta_lock, flags);
453         __sta_info_set_tim_bit(sta->sdata->bss, sta);
454         spin_unlock_irqrestore(&sta->local->sta_lock, flags);
455 }
456
457 static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
458                                      struct sta_info *sta)
459 {
460         BUG_ON(!bss);
461
462         __bss_tim_clear(bss, sta->sta.aid);
463
464         if (sta->local->ops->set_tim) {
465                 sta->local->tim_in_locked_section = true;
466                 drv_set_tim(sta->local, &sta->sta, false);
467                 sta->local->tim_in_locked_section = false;
468         }
469 }
470
471 void sta_info_clear_tim_bit(struct sta_info *sta)
472 {
473         unsigned long flags;
474
475         BUG_ON(!sta->sdata->bss);
476
477         spin_lock_irqsave(&sta->local->sta_lock, flags);
478         __sta_info_clear_tim_bit(sta->sdata->bss, sta);
479         spin_unlock_irqrestore(&sta->local->sta_lock, flags);
480 }
481
482 static void __sta_info_unlink(struct sta_info **sta)
483 {
484         struct ieee80211_local *local = (*sta)->local;
485         struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
486         /*
487          * pull caller's reference if we're already gone.
488          */
489         if (sta_info_hash_del(local, *sta)) {
490                 *sta = NULL;
491                 return;
492         }
493
494         if ((*sta)->key) {
495                 ieee80211_key_free((*sta)->key);
496                 WARN_ON((*sta)->key);
497         }
498
499         list_del(&(*sta)->list);
500         (*sta)->dead = true;
501
502         if (test_and_clear_sta_flags(*sta,
503                                 WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
504                 BUG_ON(!sdata->bss);
505
506                 atomic_dec(&sdata->bss->num_sta_ps);
507                 __sta_info_clear_tim_bit(sdata->bss, *sta);
508         }
509
510         local->num_sta--;
511         local->sta_generation++;
512
513         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
514                 rcu_assign_pointer(sdata->u.vlan.sta, NULL);
515
516         if (local->ops->sta_notify) {
517                 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
518                         sdata = container_of(sdata->bss,
519                                              struct ieee80211_sub_if_data,
520                                              u.ap);
521
522                 drv_sta_notify(local, &sdata->vif, STA_NOTIFY_REMOVE,
523                                &(*sta)->sta);
524                 sdata = (*sta)->sdata;
525         }
526
527         if (ieee80211_vif_is_mesh(&sdata->vif)) {
528                 mesh_accept_plinks_update(sdata);
529 #ifdef CONFIG_MAC80211_MESH
530                 del_timer(&(*sta)->plink_timer);
531 #endif
532         }
533
534 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
535         printk(KERN_DEBUG "%s: Removed STA %pM\n",
536                wiphy_name(local->hw.wiphy), (*sta)->sta.addr);
537 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
538
539         /*
540          * Finally, pull caller's reference if the STA is pinned by the
541          * task that is adding the debugfs entries. In that case, we
542          * leave the STA "to be freed".
543          *
544          * The rules are not trivial, but not too complex either:
545          *  (1) pin_status is only modified under the sta_lock
546          *  (2) STAs may only be pinned under the RTNL so that
547          *      sta_info_flush() is guaranteed to actually destroy
548          *      all STAs that are active for a given interface, this
549          *      is required for correctness because otherwise we
550          *      could notify a driver that an interface is going
551          *      away and only after that (!) notify it about a STA
552          *      on that interface going away.
553          *  (3) sta_info_debugfs_add_work() will set the status
554          *      to PINNED when it found an item that needs a new
555          *      debugfs directory created. In that case, that item
556          *      must not be freed although all *RCU* users are done
557          *      with it. Hence, we tell the caller of _unlink()
558          *      that the item is already gone (as can happen when
559          *      two tasks try to unlink/destroy at the same time)
560          *  (4) We set the pin_status to DESTROY here when we
561          *      find such an item.
562          *  (5) sta_info_debugfs_add_work() will reset the pin_status
563          *      from PINNED to NORMAL when it is done with the item,
564          *      but will check for DESTROY before resetting it in
565          *      which case it will free the item.
566          */
567         if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
568                 (*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
569                 *sta = NULL;
570                 return;
571         }
572 }
573
574 void sta_info_unlink(struct sta_info **sta)
575 {
576         struct ieee80211_local *local = (*sta)->local;
577         unsigned long flags;
578
579         spin_lock_irqsave(&local->sta_lock, flags);
580         __sta_info_unlink(sta);
581         spin_unlock_irqrestore(&local->sta_lock, flags);
582 }
583
584 static int sta_info_buffer_expired(struct sta_info *sta,
585                                    struct sk_buff *skb)
586 {
587         struct ieee80211_tx_info *info;
588         int timeout;
589
590         if (!skb)
591                 return 0;
592
593         info = IEEE80211_SKB_CB(skb);
594
595         /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
596         timeout = (sta->listen_interval *
597                    sta->sdata->vif.bss_conf.beacon_int *
598                    32 / 15625) * HZ;
599         if (timeout < STA_TX_BUFFER_EXPIRE)
600                 timeout = STA_TX_BUFFER_EXPIRE;
601         return time_after(jiffies, info->control.jiffies + timeout);
602 }
603
604
605 static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
606                                              struct sta_info *sta)
607 {
608         unsigned long flags;
609         struct sk_buff *skb;
610         struct ieee80211_sub_if_data *sdata;
611
612         if (skb_queue_empty(&sta->ps_tx_buf))
613                 return;
614
615         for (;;) {
616                 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
617                 skb = skb_peek(&sta->ps_tx_buf);
618                 if (sta_info_buffer_expired(sta, skb))
619                         skb = __skb_dequeue(&sta->ps_tx_buf);
620                 else
621                         skb = NULL;
622                 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
623
624                 if (!skb)
625                         break;
626
627                 sdata = sta->sdata;
628                 local->total_ps_buffered--;
629 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
630                 printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
631                        sta->sta.addr);
632 #endif
633                 dev_kfree_skb(skb);
634
635                 if (skb_queue_empty(&sta->ps_tx_buf))
636                         sta_info_clear_tim_bit(sta);
637         }
638 }
639
640
641 static void sta_info_cleanup(unsigned long data)
642 {
643         struct ieee80211_local *local = (struct ieee80211_local *) data;
644         struct sta_info *sta;
645
646         rcu_read_lock();
647         list_for_each_entry_rcu(sta, &local->sta_list, list)
648                 sta_info_cleanup_expire_buffered(local, sta);
649         rcu_read_unlock();
650
651         if (local->quiescing)
652                 return;
653
654         local->sta_cleanup.expires =
655                 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
656         add_timer(&local->sta_cleanup);
657 }
658
659 #ifdef CONFIG_MAC80211_DEBUGFS
660 /*
661  * See comment in __sta_info_unlink,
662  * caller must hold local->sta_lock.
663  */
664 static void __sta_info_pin(struct sta_info *sta)
665 {
666         WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
667         sta->pin_status = STA_INFO_PIN_STAT_PINNED;
668 }
669
670 /*
671  * See comment in __sta_info_unlink, returns sta if it
672  * needs to be destroyed.
673  */
674 static struct sta_info *__sta_info_unpin(struct sta_info *sta)
675 {
676         struct sta_info *ret = NULL;
677         unsigned long flags;
678
679         spin_lock_irqsave(&sta->local->sta_lock, flags);
680         WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
681                 sta->pin_status != STA_INFO_PIN_STAT_PINNED);
682         if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
683                 ret = sta;
684         sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
685         spin_unlock_irqrestore(&sta->local->sta_lock, flags);
686
687         return ret;
688 }
689
690 static void sta_info_debugfs_add_work(struct work_struct *work)
691 {
692         struct ieee80211_local *local =
693                 container_of(work, struct ieee80211_local, sta_debugfs_add);
694         struct sta_info *sta, *tmp;
695         unsigned long flags;
696
697         /* We need to keep the RTNL across the whole pinned status. */
698         rtnl_lock();
699         while (1) {
700                 sta = NULL;
701
702                 spin_lock_irqsave(&local->sta_lock, flags);
703                 list_for_each_entry(tmp, &local->sta_list, list) {
704                         /*
705                          * debugfs.add_has_run will be set by
706                          * ieee80211_sta_debugfs_add regardless
707                          * of what else it does.
708                          */
709                         if (!tmp->debugfs.add_has_run) {
710                                 sta = tmp;
711                                 __sta_info_pin(sta);
712                                 break;
713                         }
714                 }
715                 spin_unlock_irqrestore(&local->sta_lock, flags);
716
717                 if (!sta)
718                         break;
719
720                 ieee80211_sta_debugfs_add(sta);
721                 rate_control_add_sta_debugfs(sta);
722
723                 sta = __sta_info_unpin(sta);
724                 sta_info_destroy(sta);
725         }
726         rtnl_unlock();
727 }
728 #endif
729
730 void sta_info_init(struct ieee80211_local *local)
731 {
732         spin_lock_init(&local->sta_lock);
733         INIT_LIST_HEAD(&local->sta_list);
734
735         setup_timer(&local->sta_cleanup, sta_info_cleanup,
736                     (unsigned long)local);
737         local->sta_cleanup.expires =
738                 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
739
740 #ifdef CONFIG_MAC80211_DEBUGFS
741         INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
742 #endif
743 }
744
745 int sta_info_start(struct ieee80211_local *local)
746 {
747         add_timer(&local->sta_cleanup);
748         return 0;
749 }
750
751 void sta_info_stop(struct ieee80211_local *local)
752 {
753         del_timer(&local->sta_cleanup);
754 #ifdef CONFIG_MAC80211_DEBUGFS
755         /*
756          * Make sure the debugfs adding work isn't pending after this
757          * because we're about to be destroyed. It doesn't matter
758          * whether it ran or not since we're going to flush all STAs
759          * anyway.
760          */
761         cancel_work_sync(&local->sta_debugfs_add);
762 #endif
763
764         sta_info_flush(local, NULL);
765 }
766
767 /**
768  * sta_info_flush - flush matching STA entries from the STA table
769  *
770  * Returns the number of removed STA entries.
771  *
772  * @local: local interface data
773  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
774  */
775 int sta_info_flush(struct ieee80211_local *local,
776                    struct ieee80211_sub_if_data *sdata)
777 {
778         struct sta_info *sta, *tmp;
779         LIST_HEAD(tmp_list);
780         int ret = 0;
781         unsigned long flags;
782
783         might_sleep();
784
785         spin_lock_irqsave(&local->sta_lock, flags);
786         list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
787                 if (!sdata || sdata == sta->sdata) {
788                         __sta_info_unlink(&sta);
789                         if (sta) {
790                                 list_add_tail(&sta->list, &tmp_list);
791                                 ret++;
792                         }
793                 }
794         }
795         spin_unlock_irqrestore(&local->sta_lock, flags);
796
797         list_for_each_entry_safe(sta, tmp, &tmp_list, list)
798                 sta_info_destroy(sta);
799
800         return ret;
801 }
802
803 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
804                           unsigned long exp_time)
805 {
806         struct ieee80211_local *local = sdata->local;
807         struct sta_info *sta, *tmp;
808         LIST_HEAD(tmp_list);
809         unsigned long flags;
810
811         spin_lock_irqsave(&local->sta_lock, flags);
812         list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
813                 if (time_after(jiffies, sta->last_rx + exp_time)) {
814 #ifdef CONFIG_MAC80211_IBSS_DEBUG
815                         printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
816                                sdata->dev->name, sta->sta.addr);
817 #endif
818                         __sta_info_unlink(&sta);
819                         if (sta)
820                                 list_add(&sta->list, &tmp_list);
821                 }
822         spin_unlock_irqrestore(&local->sta_lock, flags);
823
824         list_for_each_entry_safe(sta, tmp, &tmp_list, list)
825                 sta_info_destroy(sta);
826 }
827
828 struct ieee80211_sta *ieee80211_find_sta_by_hw(struct ieee80211_hw *hw,
829                                                const u8 *addr)
830 {
831         struct sta_info *sta = sta_info_get(hw_to_local(hw), addr);
832
833         if (!sta)
834                 return NULL;
835         return &sta->sta;
836 }
837 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_hw);
838
839 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
840                                          const u8 *addr)
841 {
842         struct ieee80211_sub_if_data *sdata;
843
844         if (!vif)
845                 return NULL;
846
847         sdata = vif_to_sdata(vif);
848
849         return ieee80211_find_sta_by_hw(&sdata->local->hw, addr);
850 }
851 EXPORT_SYMBOL(ieee80211_find_sta);
852
853 /* powersave support code */
854 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
855 {
856         struct ieee80211_sub_if_data *sdata = sta->sdata;
857         struct ieee80211_local *local = sdata->local;
858         int sent, buffered;
859
860         drv_sta_notify(local, &sdata->vif, STA_NOTIFY_AWAKE, &sta->sta);
861
862         if (!skb_queue_empty(&sta->ps_tx_buf))
863                 sta_info_clear_tim_bit(sta);
864
865         /* Send all buffered frames to the station */
866         sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
867         buffered = ieee80211_add_pending_skbs(local, &sta->ps_tx_buf);
868         sent += buffered;
869         local->total_ps_buffered -= buffered;
870
871 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
872         printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
873                "since STA not sleeping anymore\n", sdata->dev->name,
874                sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
875 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
876 }
877
878 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
879 {
880         struct ieee80211_sub_if_data *sdata = sta->sdata;
881         struct ieee80211_local *local = sdata->local;
882         struct sk_buff *skb;
883         int no_pending_pkts;
884
885         skb = skb_dequeue(&sta->tx_filtered);
886         if (!skb) {
887                 skb = skb_dequeue(&sta->ps_tx_buf);
888                 if (skb)
889                         local->total_ps_buffered--;
890         }
891         no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
892                 skb_queue_empty(&sta->ps_tx_buf);
893
894         if (skb) {
895                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
896                 struct ieee80211_hdr *hdr =
897                         (struct ieee80211_hdr *) skb->data;
898
899                 /*
900                  * Tell TX path to send this frame even though the STA may
901                  * still remain is PS mode after this frame exchange.
902                  */
903                 info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
904
905 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
906                 printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
907                        sta->sta.addr, sta->sta.aid,
908                        skb_queue_len(&sta->ps_tx_buf));
909 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
910
911                 /* Use MoreData flag to indicate whether there are more
912                  * buffered frames for this STA */
913                 if (no_pending_pkts)
914                         hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
915                 else
916                         hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
917
918                 ieee80211_add_pending_skb(local, skb);
919
920                 if (no_pending_pkts)
921                         sta_info_clear_tim_bit(sta);
922 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
923         } else {
924                 /*
925                  * FIXME: This can be the result of a race condition between
926                  *        us expiring a frame and the station polling for it.
927                  *        Should we send it a null-func frame indicating we
928                  *        have nothing buffered for it?
929                  */
930                 printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
931                        "though there are no buffered frames for it\n",
932                        sdata->dev->name, sta->sta.addr);
933 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
934         }
935 }
936
937 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
938                                struct ieee80211_sta *pubsta, bool block)
939 {
940         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
941
942         if (block)
943                 set_sta_flags(sta, WLAN_STA_PS_DRIVER);
944         else
945                 ieee80211_queue_work(hw, &sta->drv_unblock_wk);
946 }
947 EXPORT_SYMBOL(ieee80211_sta_block_awake);