Linux 4.1.16
[linux/fpc-iii.git] / net / mac80211 / sta_info.c
blob2880f2ae99abe3a05b6421f53340a42e11a9ca30
1 /*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
4 * Copyright 2013-2014 Intel Mobile Communications GmbH
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/etherdevice.h>
14 #include <linux/netdevice.h>
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/skbuff.h>
18 #include <linux/if_arp.h>
19 #include <linux/timer.h>
20 #include <linux/rtnetlink.h>
22 #include <net/mac80211.h>
23 #include "ieee80211_i.h"
24 #include "driver-ops.h"
25 #include "rate.h"
26 #include "sta_info.h"
27 #include "debugfs_sta.h"
28 #include "mesh.h"
29 #include "wme.h"
31 /**
32 * DOC: STA information lifetime rules
34 * STA info structures (&struct sta_info) are managed in a hash table
35 * for faster lookup and a list for iteration. They are managed using
36 * RCU, i.e. access to the list and hash table is protected by RCU.
38 * Upon allocating a STA info structure with sta_info_alloc(), the caller
39 * owns that structure. It must then insert it into the hash table using
40 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
41 * case (which acquires an rcu read section but must not be called from
42 * within one) will the pointer still be valid after the call. Note that
43 * the caller may not do much with the STA info before inserting it, in
44 * particular, it may not start any mesh peer link management or add
45 * encryption keys.
47 * When the insertion fails (sta_info_insert()) returns non-zero), the
48 * structure will have been freed by sta_info_insert()!
50 * Station entries are added by mac80211 when you establish a link with a
51 * peer. This means different things for the different type of interfaces
52 * we support. For a regular station this mean we add the AP sta when we
53 * receive an association response from the AP. For IBSS this occurs when
54 * get to know about a peer on the same IBSS. For WDS we add the sta for
55 * the peer immediately upon device open. When using AP mode we add stations
56 * for each respective station upon request from userspace through nl80211.
58 * In order to remove a STA info structure, various sta_info_destroy_*()
59 * calls are available.
61 * There is no concept of ownership on a STA entry, each structure is
62 * owned by the global hash table/list until it is removed. All users of
63 * the structure need to be RCU protected so that the structure won't be
64 * freed before they are done using it.
67 static const struct rhashtable_params sta_rht_params = {
68 .nelem_hint = 3, /* start small */
69 .automatic_shrinking = true,
70 .head_offset = offsetof(struct sta_info, hash_node),
71 .key_offset = offsetof(struct sta_info, sta.addr),
72 .key_len = ETH_ALEN,
73 .hashfn = sta_addr_hash,
76 /* Caller must hold local->sta_mtx */
77 static int sta_info_hash_del(struct ieee80211_local *local,
78 struct sta_info *sta)
80 return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
81 sta_rht_params);
84 static void __cleanup_single_sta(struct sta_info *sta)
86 int ac, i;
87 struct tid_ampdu_tx *tid_tx;
88 struct ieee80211_sub_if_data *sdata = sta->sdata;
89 struct ieee80211_local *local = sdata->local;
90 struct ps_data *ps;
92 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
93 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
94 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
95 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
96 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
97 ps = &sdata->bss->ps;
98 else if (ieee80211_vif_is_mesh(&sdata->vif))
99 ps = &sdata->u.mesh.ps;
100 else
101 return;
103 clear_sta_flag(sta, WLAN_STA_PS_STA);
104 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
105 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
107 atomic_dec(&ps->num_sta_ps);
110 if (sta->sta.txq[0]) {
111 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
112 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
113 int n = skb_queue_len(&txqi->queue);
115 ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
116 atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
120 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
121 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
122 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
123 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
126 if (ieee80211_vif_is_mesh(&sdata->vif))
127 mesh_sta_cleanup(sta);
129 cancel_work_sync(&sta->drv_deliver_wk);
132 * Destroy aggregation state here. It would be nice to wait for the
133 * driver to finish aggregation stop and then clean up, but for now
134 * drivers have to handle aggregation stop being requested, followed
135 * directly by station destruction.
137 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
138 kfree(sta->ampdu_mlme.tid_start_tx[i]);
139 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
140 if (!tid_tx)
141 continue;
142 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
143 kfree(tid_tx);
147 static void cleanup_single_sta(struct sta_info *sta)
149 struct ieee80211_sub_if_data *sdata = sta->sdata;
150 struct ieee80211_local *local = sdata->local;
152 __cleanup_single_sta(sta);
153 sta_info_free(local, sta);
156 /* protected by RCU */
157 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
158 const u8 *addr)
160 struct ieee80211_local *local = sdata->local;
161 struct sta_info *sta;
162 struct rhash_head *tmp;
163 const struct bucket_table *tbl;
165 rcu_read_lock();
166 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
168 for_each_sta_info(local, tbl, addr, sta, tmp) {
169 if (sta->sdata == sdata) {
170 rcu_read_unlock();
171 /* this is safe as the caller must already hold
172 * another rcu read section or the mutex
174 return sta;
177 rcu_read_unlock();
178 return NULL;
182 * Get sta info either from the specified interface
183 * or from one of its vlans
185 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
186 const u8 *addr)
188 struct ieee80211_local *local = sdata->local;
189 struct sta_info *sta;
190 struct rhash_head *tmp;
191 const struct bucket_table *tbl;
193 rcu_read_lock();
194 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
196 for_each_sta_info(local, tbl, addr, sta, tmp) {
197 if (sta->sdata == sdata ||
198 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
199 rcu_read_unlock();
200 /* this is safe as the caller must already hold
201 * another rcu read section or the mutex
203 return sta;
206 rcu_read_unlock();
207 return NULL;
210 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
211 int idx)
213 struct ieee80211_local *local = sdata->local;
214 struct sta_info *sta;
215 int i = 0;
217 list_for_each_entry_rcu(sta, &local->sta_list, list) {
218 if (sdata != sta->sdata)
219 continue;
220 if (i < idx) {
221 ++i;
222 continue;
224 return sta;
227 return NULL;
231 * sta_info_free - free STA
233 * @local: pointer to the global information
234 * @sta: STA info to free
236 * This function must undo everything done by sta_info_alloc()
237 * that may happen before sta_info_insert(). It may only be
238 * called when sta_info_insert() has not been attempted (and
239 * if that fails, the station is freed anyway.)
241 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
243 if (sta->rate_ctrl)
244 rate_control_free_sta(sta);
246 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
248 if (sta->sta.txq[0])
249 kfree(to_txq_info(sta->sta.txq[0]));
250 kfree(rcu_dereference_raw(sta->sta.rates));
251 kfree(sta);
254 /* Caller must hold local->sta_mtx */
255 static void sta_info_hash_add(struct ieee80211_local *local,
256 struct sta_info *sta)
258 rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
259 sta_rht_params);
262 static void sta_deliver_ps_frames(struct work_struct *wk)
264 struct sta_info *sta;
266 sta = container_of(wk, struct sta_info, drv_deliver_wk);
268 if (sta->dead)
269 return;
271 local_bh_disable();
272 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
273 ieee80211_sta_ps_deliver_wakeup(sta);
274 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
275 ieee80211_sta_ps_deliver_poll_response(sta);
276 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
277 ieee80211_sta_ps_deliver_uapsd(sta);
278 local_bh_enable();
281 static int sta_prepare_rate_control(struct ieee80211_local *local,
282 struct sta_info *sta, gfp_t gfp)
284 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
285 return 0;
287 sta->rate_ctrl = local->rate_ctrl;
288 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
289 &sta->sta, gfp);
290 if (!sta->rate_ctrl_priv)
291 return -ENOMEM;
293 return 0;
296 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
297 const u8 *addr, gfp_t gfp)
299 struct ieee80211_local *local = sdata->local;
300 struct ieee80211_hw *hw = &local->hw;
301 struct sta_info *sta;
302 struct timespec uptime;
303 int i;
305 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
306 if (!sta)
307 return NULL;
309 spin_lock_init(&sta->lock);
310 spin_lock_init(&sta->ps_lock);
311 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
312 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
313 mutex_init(&sta->ampdu_mlme.mtx);
314 #ifdef CONFIG_MAC80211_MESH
315 if (ieee80211_vif_is_mesh(&sdata->vif) &&
316 !sdata->u.mesh.user_mpm)
317 init_timer(&sta->plink_timer);
318 sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
319 #endif
321 memcpy(sta->sta.addr, addr, ETH_ALEN);
322 sta->local = local;
323 sta->sdata = sdata;
324 sta->last_rx = jiffies;
326 sta->sta_state = IEEE80211_STA_NONE;
328 /* Mark TID as unreserved */
329 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
331 ktime_get_ts(&uptime);
332 sta->last_connected = uptime.tv_sec;
333 ewma_init(&sta->avg_signal, 1024, 8);
334 for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
335 ewma_init(&sta->chain_signal_avg[i], 1024, 8);
337 if (local->ops->wake_tx_queue) {
338 void *txq_data;
339 int size = sizeof(struct txq_info) +
340 ALIGN(hw->txq_data_size, sizeof(void *));
342 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
343 if (!txq_data)
344 goto free;
346 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
347 struct txq_info *txq = txq_data + i * size;
349 ieee80211_init_tx_queue(sdata, sta, txq, i);
353 if (sta_prepare_rate_control(local, sta, gfp))
354 goto free_txq;
356 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
358 * timer_to_tid must be initialized with identity mapping
359 * to enable session_timer's data differentiation. See
360 * sta_rx_agg_session_timer_expired for usage.
362 sta->timer_to_tid[i] = i;
364 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
365 skb_queue_head_init(&sta->ps_tx_buf[i]);
366 skb_queue_head_init(&sta->tx_filtered[i]);
369 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
370 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
372 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
373 if (sdata->vif.type == NL80211_IFTYPE_AP ||
374 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
375 struct ieee80211_supported_band *sband =
376 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
377 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
378 IEEE80211_HT_CAP_SM_PS_SHIFT;
380 * Assume that hostapd advertises our caps in the beacon and
381 * this is the known_smps_mode for a station that just assciated
383 switch (smps) {
384 case WLAN_HT_SMPS_CONTROL_DISABLED:
385 sta->known_smps_mode = IEEE80211_SMPS_OFF;
386 break;
387 case WLAN_HT_SMPS_CONTROL_STATIC:
388 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
389 break;
390 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
391 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
392 break;
393 default:
394 WARN_ON(1);
398 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
400 return sta;
402 free_txq:
403 if (sta->sta.txq[0])
404 kfree(to_txq_info(sta->sta.txq[0]));
405 free:
406 kfree(sta);
407 return NULL;
410 static int sta_info_insert_check(struct sta_info *sta)
412 struct ieee80211_sub_if_data *sdata = sta->sdata;
415 * Can't be a WARN_ON because it can be triggered through a race:
416 * something inserts a STA (on one CPU) without holding the RTNL
417 * and another CPU turns off the net device.
419 if (unlikely(!ieee80211_sdata_running(sdata)))
420 return -ENETDOWN;
422 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
423 is_multicast_ether_addr(sta->sta.addr)))
424 return -EINVAL;
426 return 0;
429 static int sta_info_insert_drv_state(struct ieee80211_local *local,
430 struct ieee80211_sub_if_data *sdata,
431 struct sta_info *sta)
433 enum ieee80211_sta_state state;
434 int err = 0;
436 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
437 err = drv_sta_state(local, sdata, sta, state, state + 1);
438 if (err)
439 break;
442 if (!err) {
444 * Drivers using legacy sta_add/sta_remove callbacks only
445 * get uploaded set to true after sta_add is called.
447 if (!local->ops->sta_add)
448 sta->uploaded = true;
449 return 0;
452 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
453 sdata_info(sdata,
454 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
455 sta->sta.addr, state + 1, err);
456 err = 0;
459 /* unwind on error */
460 for (; state > IEEE80211_STA_NOTEXIST; state--)
461 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
463 return err;
467 * should be called with sta_mtx locked
468 * this function replaces the mutex lock
469 * with a RCU lock
471 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
473 struct ieee80211_local *local = sta->local;
474 struct ieee80211_sub_if_data *sdata = sta->sdata;
475 struct station_info sinfo;
476 int err = 0;
478 lockdep_assert_held(&local->sta_mtx);
480 /* check if STA exists already */
481 if (sta_info_get_bss(sdata, sta->sta.addr)) {
482 err = -EEXIST;
483 goto out_err;
486 local->num_sta++;
487 local->sta_generation++;
488 smp_mb();
490 /* simplify things and don't accept BA sessions yet */
491 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
493 /* make the station visible */
494 sta_info_hash_add(local, sta);
496 list_add_tail_rcu(&sta->list, &local->sta_list);
498 /* notify driver */
499 err = sta_info_insert_drv_state(local, sdata, sta);
500 if (err)
501 goto out_remove;
503 set_sta_flag(sta, WLAN_STA_INSERTED);
504 /* accept BA sessions now */
505 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
507 ieee80211_recalc_min_chandef(sdata);
508 ieee80211_sta_debugfs_add(sta);
509 rate_control_add_sta_debugfs(sta);
511 memset(&sinfo, 0, sizeof(sinfo));
512 sinfo.filled = 0;
513 sinfo.generation = local->sta_generation;
514 cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
516 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
518 /* move reference to rcu-protected */
519 rcu_read_lock();
520 mutex_unlock(&local->sta_mtx);
522 if (ieee80211_vif_is_mesh(&sdata->vif))
523 mesh_accept_plinks_update(sdata);
525 return 0;
526 out_remove:
527 sta_info_hash_del(local, sta);
528 list_del_rcu(&sta->list);
529 local->num_sta--;
530 synchronize_net();
531 __cleanup_single_sta(sta);
532 out_err:
533 mutex_unlock(&local->sta_mtx);
534 rcu_read_lock();
535 return err;
538 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
540 struct ieee80211_local *local = sta->local;
541 int err;
543 might_sleep();
545 err = sta_info_insert_check(sta);
546 if (err) {
547 rcu_read_lock();
548 goto out_free;
551 mutex_lock(&local->sta_mtx);
553 err = sta_info_insert_finish(sta);
554 if (err)
555 goto out_free;
557 return 0;
558 out_free:
559 sta_info_free(local, sta);
560 return err;
563 int sta_info_insert(struct sta_info *sta)
565 int err = sta_info_insert_rcu(sta);
567 rcu_read_unlock();
569 return err;
572 static inline void __bss_tim_set(u8 *tim, u16 id)
575 * This format has been mandated by the IEEE specifications,
576 * so this line may not be changed to use the __set_bit() format.
578 tim[id / 8] |= (1 << (id % 8));
581 static inline void __bss_tim_clear(u8 *tim, u16 id)
584 * This format has been mandated by the IEEE specifications,
585 * so this line may not be changed to use the __clear_bit() format.
587 tim[id / 8] &= ~(1 << (id % 8));
590 static inline bool __bss_tim_get(u8 *tim, u16 id)
593 * This format has been mandated by the IEEE specifications,
594 * so this line may not be changed to use the test_bit() format.
596 return tim[id / 8] & (1 << (id % 8));
599 static unsigned long ieee80211_tids_for_ac(int ac)
601 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
602 switch (ac) {
603 case IEEE80211_AC_VO:
604 return BIT(6) | BIT(7);
605 case IEEE80211_AC_VI:
606 return BIT(4) | BIT(5);
607 case IEEE80211_AC_BE:
608 return BIT(0) | BIT(3);
609 case IEEE80211_AC_BK:
610 return BIT(1) | BIT(2);
611 default:
612 WARN_ON(1);
613 return 0;
617 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
619 struct ieee80211_local *local = sta->local;
620 struct ps_data *ps;
621 bool indicate_tim = false;
622 u8 ignore_for_tim = sta->sta.uapsd_queues;
623 int ac;
624 u16 id;
626 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
627 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
628 if (WARN_ON_ONCE(!sta->sdata->bss))
629 return;
631 ps = &sta->sdata->bss->ps;
632 id = sta->sta.aid;
633 #ifdef CONFIG_MAC80211_MESH
634 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
635 ps = &sta->sdata->u.mesh.ps;
636 /* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
637 id = sta->plid % (IEEE80211_MAX_AID + 1);
638 #endif
639 } else {
640 return;
643 /* No need to do anything if the driver does all */
644 if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
645 return;
647 if (sta->dead)
648 goto done;
651 * If all ACs are delivery-enabled then we should build
652 * the TIM bit for all ACs anyway; if only some are then
653 * we ignore those and build the TIM bit using only the
654 * non-enabled ones.
656 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
657 ignore_for_tim = 0;
659 if (ignore_pending)
660 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
662 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
663 unsigned long tids;
665 if (ignore_for_tim & BIT(ac))
666 continue;
668 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
669 !skb_queue_empty(&sta->ps_tx_buf[ac]);
670 if (indicate_tim)
671 break;
673 tids = ieee80211_tids_for_ac(ac);
675 indicate_tim |=
676 sta->driver_buffered_tids & tids;
677 indicate_tim |=
678 sta->txq_buffered_tids & tids;
681 done:
682 spin_lock_bh(&local->tim_lock);
684 if (indicate_tim == __bss_tim_get(ps->tim, id))
685 goto out_unlock;
687 if (indicate_tim)
688 __bss_tim_set(ps->tim, id);
689 else
690 __bss_tim_clear(ps->tim, id);
692 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
693 local->tim_in_locked_section = true;
694 drv_set_tim(local, &sta->sta, indicate_tim);
695 local->tim_in_locked_section = false;
698 out_unlock:
699 spin_unlock_bh(&local->tim_lock);
702 void sta_info_recalc_tim(struct sta_info *sta)
704 __sta_info_recalc_tim(sta, false);
707 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
709 struct ieee80211_tx_info *info;
710 int timeout;
712 if (!skb)
713 return false;
715 info = IEEE80211_SKB_CB(skb);
717 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
718 timeout = (sta->listen_interval *
719 sta->sdata->vif.bss_conf.beacon_int *
720 32 / 15625) * HZ;
721 if (timeout < STA_TX_BUFFER_EXPIRE)
722 timeout = STA_TX_BUFFER_EXPIRE;
723 return time_after(jiffies, info->control.jiffies + timeout);
727 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
728 struct sta_info *sta, int ac)
730 unsigned long flags;
731 struct sk_buff *skb;
734 * First check for frames that should expire on the filtered
735 * queue. Frames here were rejected by the driver and are on
736 * a separate queue to avoid reordering with normal PS-buffered
737 * frames. They also aren't accounted for right now in the
738 * total_ps_buffered counter.
740 for (;;) {
741 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
742 skb = skb_peek(&sta->tx_filtered[ac]);
743 if (sta_info_buffer_expired(sta, skb))
744 skb = __skb_dequeue(&sta->tx_filtered[ac]);
745 else
746 skb = NULL;
747 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
750 * Frames are queued in order, so if this one
751 * hasn't expired yet we can stop testing. If
752 * we actually reached the end of the queue we
753 * also need to stop, of course.
755 if (!skb)
756 break;
757 ieee80211_free_txskb(&local->hw, skb);
761 * Now also check the normal PS-buffered queue, this will
762 * only find something if the filtered queue was emptied
763 * since the filtered frames are all before the normal PS
764 * buffered frames.
766 for (;;) {
767 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
768 skb = skb_peek(&sta->ps_tx_buf[ac]);
769 if (sta_info_buffer_expired(sta, skb))
770 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
771 else
772 skb = NULL;
773 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
776 * frames are queued in order, so if this one
777 * hasn't expired yet (or we reached the end of
778 * the queue) we can stop testing
780 if (!skb)
781 break;
783 local->total_ps_buffered--;
784 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
785 sta->sta.addr);
786 ieee80211_free_txskb(&local->hw, skb);
790 * Finally, recalculate the TIM bit for this station -- it might
791 * now be clear because the station was too slow to retrieve its
792 * frames.
794 sta_info_recalc_tim(sta);
797 * Return whether there are any frames still buffered, this is
798 * used to check whether the cleanup timer still needs to run,
799 * if there are no frames we don't need to rearm the timer.
801 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
802 skb_queue_empty(&sta->tx_filtered[ac]));
805 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
806 struct sta_info *sta)
808 bool have_buffered = false;
809 int ac;
811 /* This is only necessary for stations on BSS/MBSS interfaces */
812 if (!sta->sdata->bss &&
813 !ieee80211_vif_is_mesh(&sta->sdata->vif))
814 return false;
816 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
817 have_buffered |=
818 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
820 return have_buffered;
823 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
825 struct ieee80211_local *local;
826 struct ieee80211_sub_if_data *sdata;
827 int ret;
829 might_sleep();
831 if (!sta)
832 return -ENOENT;
834 local = sta->local;
835 sdata = sta->sdata;
837 lockdep_assert_held(&local->sta_mtx);
840 * Before removing the station from the driver and
841 * rate control, it might still start new aggregation
842 * sessions -- block that to make sure the tear-down
843 * will be sufficient.
845 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
846 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
848 ret = sta_info_hash_del(local, sta);
849 if (WARN_ON(ret))
850 return ret;
853 * for TDLS peers, make sure to return to the base channel before
854 * removal.
856 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
857 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
858 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
861 list_del_rcu(&sta->list);
863 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
865 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
866 rcu_access_pointer(sdata->u.vlan.sta) == sta)
867 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
869 return 0;
872 static void __sta_info_destroy_part2(struct sta_info *sta)
874 struct ieee80211_local *local = sta->local;
875 struct ieee80211_sub_if_data *sdata = sta->sdata;
876 struct station_info sinfo = {};
877 int ret;
880 * NOTE: This assumes at least synchronize_net() was done
881 * after _part1 and before _part2!
884 might_sleep();
885 lockdep_assert_held(&local->sta_mtx);
887 /* now keys can no longer be reached */
888 ieee80211_free_sta_keys(local, sta);
890 /* disable TIM bit - last chance to tell driver */
891 __sta_info_recalc_tim(sta, true);
893 sta->dead = true;
895 local->num_sta--;
896 local->sta_generation++;
898 while (sta->sta_state > IEEE80211_STA_NONE) {
899 ret = sta_info_move_state(sta, sta->sta_state - 1);
900 if (ret) {
901 WARN_ON_ONCE(1);
902 break;
906 if (sta->uploaded) {
907 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
908 IEEE80211_STA_NOTEXIST);
909 WARN_ON_ONCE(ret != 0);
912 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
914 sta_set_sinfo(sta, &sinfo);
915 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
917 rate_control_remove_sta_debugfs(sta);
918 ieee80211_sta_debugfs_remove(sta);
919 ieee80211_recalc_min_chandef(sdata);
921 cleanup_single_sta(sta);
924 int __must_check __sta_info_destroy(struct sta_info *sta)
926 int err = __sta_info_destroy_part1(sta);
928 if (err)
929 return err;
931 synchronize_net();
933 __sta_info_destroy_part2(sta);
935 return 0;
938 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
940 struct sta_info *sta;
941 int ret;
943 mutex_lock(&sdata->local->sta_mtx);
944 sta = sta_info_get(sdata, addr);
945 ret = __sta_info_destroy(sta);
946 mutex_unlock(&sdata->local->sta_mtx);
948 return ret;
951 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
952 const u8 *addr)
954 struct sta_info *sta;
955 int ret;
957 mutex_lock(&sdata->local->sta_mtx);
958 sta = sta_info_get_bss(sdata, addr);
959 ret = __sta_info_destroy(sta);
960 mutex_unlock(&sdata->local->sta_mtx);
962 return ret;
965 static void sta_info_cleanup(unsigned long data)
967 struct ieee80211_local *local = (struct ieee80211_local *) data;
968 struct sta_info *sta;
969 bool timer_needed = false;
971 rcu_read_lock();
972 list_for_each_entry_rcu(sta, &local->sta_list, list)
973 if (sta_info_cleanup_expire_buffered(local, sta))
974 timer_needed = true;
975 rcu_read_unlock();
977 if (local->quiescing)
978 return;
980 if (!timer_needed)
981 return;
983 mod_timer(&local->sta_cleanup,
984 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
987 u32 sta_addr_hash(const void *key, u32 length, u32 seed)
989 return jhash(key, ETH_ALEN, seed);
992 int sta_info_init(struct ieee80211_local *local)
994 int err;
996 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
997 if (err)
998 return err;
1000 spin_lock_init(&local->tim_lock);
1001 mutex_init(&local->sta_mtx);
1002 INIT_LIST_HEAD(&local->sta_list);
1004 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1005 (unsigned long)local);
1006 return 0;
1009 void sta_info_stop(struct ieee80211_local *local)
1011 del_timer_sync(&local->sta_cleanup);
1012 rhashtable_destroy(&local->sta_hash);
1016 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1018 struct ieee80211_local *local = sdata->local;
1019 struct sta_info *sta, *tmp;
1020 LIST_HEAD(free_list);
1021 int ret = 0;
1023 might_sleep();
1025 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1026 WARN_ON(vlans && !sdata->bss);
1028 mutex_lock(&local->sta_mtx);
1029 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1030 if (sdata == sta->sdata ||
1031 (vlans && sdata->bss == sta->sdata->bss)) {
1032 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1033 list_add(&sta->free_list, &free_list);
1034 ret++;
1038 if (!list_empty(&free_list)) {
1039 synchronize_net();
1040 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1041 __sta_info_destroy_part2(sta);
1043 mutex_unlock(&local->sta_mtx);
1045 return ret;
1048 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1049 unsigned long exp_time)
1051 struct ieee80211_local *local = sdata->local;
1052 struct sta_info *sta, *tmp;
1054 mutex_lock(&local->sta_mtx);
1056 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1057 if (sdata != sta->sdata)
1058 continue;
1060 if (time_after(jiffies, sta->last_rx + exp_time)) {
1061 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1062 sta->sta.addr);
1064 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1065 test_sta_flag(sta, WLAN_STA_PS_STA))
1066 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1068 WARN_ON(__sta_info_destroy(sta));
1072 mutex_unlock(&local->sta_mtx);
1075 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1076 const u8 *addr,
1077 const u8 *localaddr)
1079 struct ieee80211_local *local = hw_to_local(hw);
1080 struct sta_info *sta;
1081 struct rhash_head *tmp;
1082 const struct bucket_table *tbl;
1084 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
1087 * Just return a random station if localaddr is NULL
1088 * ... first in list.
1090 for_each_sta_info(local, tbl, addr, sta, tmp) {
1091 if (localaddr &&
1092 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1093 continue;
1094 if (!sta->uploaded)
1095 return NULL;
1096 return &sta->sta;
1099 return NULL;
1101 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1103 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1104 const u8 *addr)
1106 struct sta_info *sta;
1108 if (!vif)
1109 return NULL;
1111 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1112 if (!sta)
1113 return NULL;
1115 if (!sta->uploaded)
1116 return NULL;
1118 return &sta->sta;
1120 EXPORT_SYMBOL(ieee80211_find_sta);
1122 /* powersave support code */
1123 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1125 struct ieee80211_sub_if_data *sdata = sta->sdata;
1126 struct ieee80211_local *local = sdata->local;
1127 struct sk_buff_head pending;
1128 int filtered = 0, buffered = 0, ac, i;
1129 unsigned long flags;
1130 struct ps_data *ps;
1132 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1133 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1134 u.ap);
1136 if (sdata->vif.type == NL80211_IFTYPE_AP)
1137 ps = &sdata->bss->ps;
1138 else if (ieee80211_vif_is_mesh(&sdata->vif))
1139 ps = &sdata->u.mesh.ps;
1140 else
1141 return;
1143 clear_sta_flag(sta, WLAN_STA_SP);
1145 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1146 sta->driver_buffered_tids = 0;
1147 sta->txq_buffered_tids = 0;
1149 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1150 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1152 if (sta->sta.txq[0]) {
1153 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1154 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1156 if (!skb_queue_len(&txqi->queue))
1157 continue;
1159 drv_wake_tx_queue(local, txqi);
1163 skb_queue_head_init(&pending);
1165 /* sync with ieee80211_tx_h_unicast_ps_buf */
1166 spin_lock(&sta->ps_lock);
1167 /* Send all buffered frames to the station */
1168 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1169 int count = skb_queue_len(&pending), tmp;
1171 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1172 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1173 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1174 tmp = skb_queue_len(&pending);
1175 filtered += tmp - count;
1176 count = tmp;
1178 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1179 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1180 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1181 tmp = skb_queue_len(&pending);
1182 buffered += tmp - count;
1185 ieee80211_add_pending_skbs(local, &pending);
1187 /* now we're no longer in the deliver code */
1188 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1190 /* The station might have polled and then woken up before we responded,
1191 * so clear these flags now to avoid them sticking around.
1193 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1194 clear_sta_flag(sta, WLAN_STA_UAPSD);
1195 spin_unlock(&sta->ps_lock);
1197 atomic_dec(&ps->num_sta_ps);
1199 /* This station just woke up and isn't aware of our SMPS state */
1200 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1201 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1202 sdata->smps_mode) &&
1203 sta->known_smps_mode != sdata->bss->req_smps &&
1204 sta_info_tx_streams(sta) != 1) {
1205 ht_dbg(sdata,
1206 "%pM just woke up and MIMO capable - update SMPS\n",
1207 sta->sta.addr);
1208 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1209 sta->sta.addr,
1210 sdata->vif.bss_conf.bssid);
1213 local->total_ps_buffered -= buffered;
1215 sta_info_recalc_tim(sta);
1217 ps_dbg(sdata,
1218 "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1219 sta->sta.addr, sta->sta.aid, filtered, buffered);
1222 static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1223 struct sta_info *sta, int tid,
1224 enum ieee80211_frame_release_type reason,
1225 bool call_driver)
1227 struct ieee80211_local *local = sdata->local;
1228 struct ieee80211_qos_hdr *nullfunc;
1229 struct sk_buff *skb;
1230 int size = sizeof(*nullfunc);
1231 __le16 fc;
1232 bool qos = sta->sta.wme;
1233 struct ieee80211_tx_info *info;
1234 struct ieee80211_chanctx_conf *chanctx_conf;
1236 if (qos) {
1237 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1238 IEEE80211_STYPE_QOS_NULLFUNC |
1239 IEEE80211_FCTL_FROMDS);
1240 } else {
1241 size -= 2;
1242 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1243 IEEE80211_STYPE_NULLFUNC |
1244 IEEE80211_FCTL_FROMDS);
1247 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1248 if (!skb)
1249 return;
1251 skb_reserve(skb, local->hw.extra_tx_headroom);
1253 nullfunc = (void *) skb_put(skb, size);
1254 nullfunc->frame_control = fc;
1255 nullfunc->duration_id = 0;
1256 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1257 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1258 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1259 nullfunc->seq_ctrl = 0;
1261 skb->priority = tid;
1262 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1263 if (qos) {
1264 nullfunc->qos_ctrl = cpu_to_le16(tid);
1266 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1267 nullfunc->qos_ctrl |=
1268 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1271 info = IEEE80211_SKB_CB(skb);
1274 * Tell TX path to send this frame even though the
1275 * STA may still remain is PS mode after this frame
1276 * exchange. Also set EOSP to indicate this packet
1277 * ends the poll/service period.
1279 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1280 IEEE80211_TX_STATUS_EOSP |
1281 IEEE80211_TX_CTL_REQ_TX_STATUS;
1283 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1285 if (call_driver)
1286 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1287 reason, false);
1289 skb->dev = sdata->dev;
1291 rcu_read_lock();
1292 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1293 if (WARN_ON(!chanctx_conf)) {
1294 rcu_read_unlock();
1295 kfree_skb(skb);
1296 return;
1299 info->band = chanctx_conf->def.chan->band;
1300 ieee80211_xmit(sdata, sta, skb);
1301 rcu_read_unlock();
1304 static int find_highest_prio_tid(unsigned long tids)
1306 /* lower 3 TIDs aren't ordered perfectly */
1307 if (tids & 0xF8)
1308 return fls(tids) - 1;
1309 /* TID 0 is BE just like TID 3 */
1310 if (tids & BIT(0))
1311 return 0;
1312 return fls(tids) - 1;
1315 static void
1316 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1317 int n_frames, u8 ignored_acs,
1318 enum ieee80211_frame_release_type reason)
1320 struct ieee80211_sub_if_data *sdata = sta->sdata;
1321 struct ieee80211_local *local = sdata->local;
1322 bool more_data = false;
1323 int ac;
1324 unsigned long driver_release_tids = 0;
1325 struct sk_buff_head frames;
1327 /* Service or PS-Poll period starts */
1328 set_sta_flag(sta, WLAN_STA_SP);
1330 __skb_queue_head_init(&frames);
1332 /* Get response frame(s) and more data bit for the last one. */
1333 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1334 unsigned long tids;
1336 if (ignored_acs & BIT(ac))
1337 continue;
1339 tids = ieee80211_tids_for_ac(ac);
1341 /* if we already have frames from software, then we can't also
1342 * release from hardware queues
1344 if (skb_queue_empty(&frames)) {
1345 driver_release_tids |= sta->driver_buffered_tids & tids;
1346 driver_release_tids |= sta->txq_buffered_tids & tids;
1349 if (driver_release_tids) {
1350 /* If the driver has data on more than one TID then
1351 * certainly there's more data if we release just a
1352 * single frame now (from a single TID). This will
1353 * only happen for PS-Poll.
1355 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1356 hweight16(driver_release_tids) > 1) {
1357 more_data = true;
1358 driver_release_tids =
1359 BIT(find_highest_prio_tid(
1360 driver_release_tids));
1361 break;
1363 } else {
1364 struct sk_buff *skb;
1366 while (n_frames > 0) {
1367 skb = skb_dequeue(&sta->tx_filtered[ac]);
1368 if (!skb) {
1369 skb = skb_dequeue(
1370 &sta->ps_tx_buf[ac]);
1371 if (skb)
1372 local->total_ps_buffered--;
1374 if (!skb)
1375 break;
1376 n_frames--;
1377 __skb_queue_tail(&frames, skb);
1381 /* If we have more frames buffered on this AC, then set the
1382 * more-data bit and abort the loop since we can't send more
1383 * data from other ACs before the buffered frames from this.
1385 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1386 !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1387 more_data = true;
1388 break;
1392 if (skb_queue_empty(&frames) && !driver_release_tids) {
1393 int tid;
1396 * For PS-Poll, this can only happen due to a race condition
1397 * when we set the TIM bit and the station notices it, but
1398 * before it can poll for the frame we expire it.
1400 * For uAPSD, this is said in the standard (11.2.1.5 h):
1401 * At each unscheduled SP for a non-AP STA, the AP shall
1402 * attempt to transmit at least one MSDU or MMPDU, but no
1403 * more than the value specified in the Max SP Length field
1404 * in the QoS Capability element from delivery-enabled ACs,
1405 * that are destined for the non-AP STA.
1407 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1410 /* This will evaluate to 1, 3, 5 or 7. */
1411 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1413 ieee80211_send_null_response(sdata, sta, tid, reason, true);
1414 } else if (!driver_release_tids) {
1415 struct sk_buff_head pending;
1416 struct sk_buff *skb;
1417 int num = 0;
1418 u16 tids = 0;
1419 bool need_null = false;
1421 skb_queue_head_init(&pending);
1423 while ((skb = __skb_dequeue(&frames))) {
1424 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1425 struct ieee80211_hdr *hdr = (void *) skb->data;
1426 u8 *qoshdr = NULL;
1428 num++;
1431 * Tell TX path to send this frame even though the
1432 * STA may still remain is PS mode after this frame
1433 * exchange.
1435 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1436 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1439 * Use MoreData flag to indicate whether there are
1440 * more buffered frames for this STA
1442 if (more_data || !skb_queue_empty(&frames))
1443 hdr->frame_control |=
1444 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1445 else
1446 hdr->frame_control &=
1447 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1449 if (ieee80211_is_data_qos(hdr->frame_control) ||
1450 ieee80211_is_qos_nullfunc(hdr->frame_control))
1451 qoshdr = ieee80211_get_qos_ctl(hdr);
1453 tids |= BIT(skb->priority);
1455 __skb_queue_tail(&pending, skb);
1457 /* end service period after last frame or add one */
1458 if (!skb_queue_empty(&frames))
1459 continue;
1461 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1462 /* for PS-Poll, there's only one frame */
1463 info->flags |= IEEE80211_TX_STATUS_EOSP |
1464 IEEE80211_TX_CTL_REQ_TX_STATUS;
1465 break;
1468 /* For uAPSD, things are a bit more complicated. If the
1469 * last frame has a QoS header (i.e. is a QoS-data or
1470 * QoS-nulldata frame) then just set the EOSP bit there
1471 * and be done.
1472 * If the frame doesn't have a QoS header (which means
1473 * it should be a bufferable MMPDU) then we can't set
1474 * the EOSP bit in the QoS header; add a QoS-nulldata
1475 * frame to the list to send it after the MMPDU.
1477 * Note that this code is only in the mac80211-release
1478 * code path, we assume that the driver will not buffer
1479 * anything but QoS-data frames, or if it does, will
1480 * create the QoS-nulldata frame by itself if needed.
1482 * Cf. 802.11-2012 10.2.1.10 (c).
1484 if (qoshdr) {
1485 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1487 info->flags |= IEEE80211_TX_STATUS_EOSP |
1488 IEEE80211_TX_CTL_REQ_TX_STATUS;
1489 } else {
1490 /* The standard isn't completely clear on this
1491 * as it says the more-data bit should be set
1492 * if there are more BUs. The QoS-Null frame
1493 * we're about to send isn't buffered yet, we
1494 * only create it below, but let's pretend it
1495 * was buffered just in case some clients only
1496 * expect more-data=0 when eosp=1.
1498 hdr->frame_control |=
1499 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1500 need_null = true;
1501 num++;
1503 break;
1506 drv_allow_buffered_frames(local, sta, tids, num,
1507 reason, more_data);
1509 ieee80211_add_pending_skbs(local, &pending);
1511 if (need_null)
1512 ieee80211_send_null_response(
1513 sdata, sta, find_highest_prio_tid(tids),
1514 reason, false);
1516 sta_info_recalc_tim(sta);
1517 } else {
1518 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1519 int tid;
1522 * We need to release a frame that is buffered somewhere in the
1523 * driver ... it'll have to handle that.
1524 * Note that the driver also has to check the number of frames
1525 * on the TIDs we're releasing from - if there are more than
1526 * n_frames it has to set the more-data bit (if we didn't ask
1527 * it to set it anyway due to other buffered frames); if there
1528 * are fewer than n_frames it has to make sure to adjust that
1529 * to allow the service period to end properly.
1531 drv_release_buffered_frames(local, sta, driver_release_tids,
1532 n_frames, reason, more_data);
1535 * Note that we don't recalculate the TIM bit here as it would
1536 * most likely have no effect at all unless the driver told us
1537 * that the TID(s) became empty before returning here from the
1538 * release function.
1539 * Either way, however, when the driver tells us that the TID(s)
1540 * became empty or we find that a txq became empty, we'll do the
1541 * TIM recalculation.
1544 if (!sta->sta.txq[0])
1545 return;
1547 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1548 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1550 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1551 continue;
1553 sta_info_recalc_tim(sta);
1554 break;
1559 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1561 u8 ignore_for_response = sta->sta.uapsd_queues;
1564 * If all ACs are delivery-enabled then we should reply
1565 * from any of them, if only some are enabled we reply
1566 * only from the non-enabled ones.
1568 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1569 ignore_for_response = 0;
1571 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1572 IEEE80211_FRAME_RELEASE_PSPOLL);
1575 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1577 int n_frames = sta->sta.max_sp;
1578 u8 delivery_enabled = sta->sta.uapsd_queues;
1581 * If we ever grow support for TSPEC this might happen if
1582 * the TSPEC update from hostapd comes in between a trigger
1583 * frame setting WLAN_STA_UAPSD in the RX path and this
1584 * actually getting called.
1586 if (!delivery_enabled)
1587 return;
1589 switch (sta->sta.max_sp) {
1590 case 1:
1591 n_frames = 2;
1592 break;
1593 case 2:
1594 n_frames = 4;
1595 break;
1596 case 3:
1597 n_frames = 6;
1598 break;
1599 case 0:
1600 /* XXX: what is a good value? */
1601 n_frames = 128;
1602 break;
1605 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1606 IEEE80211_FRAME_RELEASE_UAPSD);
1609 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1610 struct ieee80211_sta *pubsta, bool block)
1612 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1614 trace_api_sta_block_awake(sta->local, pubsta, block);
1616 if (block) {
1617 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1618 return;
1621 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1622 return;
1624 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1625 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1626 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1627 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1628 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1629 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1630 /* must be asleep in this case */
1631 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1632 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1633 } else {
1634 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1637 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1639 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1641 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1642 struct ieee80211_local *local = sta->local;
1644 trace_api_eosp(local, pubsta);
1646 clear_sta_flag(sta, WLAN_STA_SP);
1648 EXPORT_SYMBOL(ieee80211_sta_eosp);
1650 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1651 u8 tid, bool buffered)
1653 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1655 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1656 return;
1658 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1660 if (buffered)
1661 set_bit(tid, &sta->driver_buffered_tids);
1662 else
1663 clear_bit(tid, &sta->driver_buffered_tids);
1665 sta_info_recalc_tim(sta);
1667 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1669 int sta_info_move_state(struct sta_info *sta,
1670 enum ieee80211_sta_state new_state)
1672 might_sleep();
1674 if (sta->sta_state == new_state)
1675 return 0;
1677 /* check allowed transitions first */
1679 switch (new_state) {
1680 case IEEE80211_STA_NONE:
1681 if (sta->sta_state != IEEE80211_STA_AUTH)
1682 return -EINVAL;
1683 break;
1684 case IEEE80211_STA_AUTH:
1685 if (sta->sta_state != IEEE80211_STA_NONE &&
1686 sta->sta_state != IEEE80211_STA_ASSOC)
1687 return -EINVAL;
1688 break;
1689 case IEEE80211_STA_ASSOC:
1690 if (sta->sta_state != IEEE80211_STA_AUTH &&
1691 sta->sta_state != IEEE80211_STA_AUTHORIZED)
1692 return -EINVAL;
1693 break;
1694 case IEEE80211_STA_AUTHORIZED:
1695 if (sta->sta_state != IEEE80211_STA_ASSOC)
1696 return -EINVAL;
1697 break;
1698 default:
1699 WARN(1, "invalid state %d", new_state);
1700 return -EINVAL;
1703 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1704 sta->sta.addr, new_state);
1707 * notify the driver before the actual changes so it can
1708 * fail the transition
1710 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1711 int err = drv_sta_state(sta->local, sta->sdata, sta,
1712 sta->sta_state, new_state);
1713 if (err)
1714 return err;
1717 /* reflect the change in all state variables */
1719 switch (new_state) {
1720 case IEEE80211_STA_NONE:
1721 if (sta->sta_state == IEEE80211_STA_AUTH)
1722 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1723 break;
1724 case IEEE80211_STA_AUTH:
1725 if (sta->sta_state == IEEE80211_STA_NONE)
1726 set_bit(WLAN_STA_AUTH, &sta->_flags);
1727 else if (sta->sta_state == IEEE80211_STA_ASSOC)
1728 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1729 break;
1730 case IEEE80211_STA_ASSOC:
1731 if (sta->sta_state == IEEE80211_STA_AUTH) {
1732 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1733 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1734 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1735 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1736 !sta->sdata->u.vlan.sta))
1737 atomic_dec(&sta->sdata->bss->num_mcast_sta);
1738 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1740 break;
1741 case IEEE80211_STA_AUTHORIZED:
1742 if (sta->sta_state == IEEE80211_STA_ASSOC) {
1743 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1744 (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1745 !sta->sdata->u.vlan.sta))
1746 atomic_inc(&sta->sdata->bss->num_mcast_sta);
1747 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1749 break;
1750 default:
1751 break;
1754 sta->sta_state = new_state;
1756 return 0;
1759 u8 sta_info_tx_streams(struct sta_info *sta)
1761 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1762 u8 rx_streams;
1764 if (!sta->sta.ht_cap.ht_supported)
1765 return 1;
1767 if (sta->sta.vht_cap.vht_supported) {
1768 int i;
1769 u16 tx_mcs_map =
1770 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1772 for (i = 7; i >= 0; i--)
1773 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1774 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1775 return i + 1;
1778 if (ht_cap->mcs.rx_mask[3])
1779 rx_streams = 4;
1780 else if (ht_cap->mcs.rx_mask[2])
1781 rx_streams = 3;
1782 else if (ht_cap->mcs.rx_mask[1])
1783 rx_streams = 2;
1784 else
1785 rx_streams = 1;
1787 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1788 return rx_streams;
1790 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1791 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1794 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1796 struct ieee80211_sub_if_data *sdata = sta->sdata;
1797 struct ieee80211_local *local = sdata->local;
1798 struct rate_control_ref *ref = NULL;
1799 struct timespec uptime;
1800 u32 thr = 0;
1801 int i, ac;
1803 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1804 ref = local->rate_ctrl;
1806 sinfo->generation = sdata->local->sta_generation;
1808 /* do before driver, so beacon filtering drivers have a
1809 * chance to e.g. just add the number of filtered beacons
1810 * (or just modify the value entirely, of course)
1812 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1813 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1815 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1817 sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1818 BIT(NL80211_STA_INFO_STA_FLAGS) |
1819 BIT(NL80211_STA_INFO_BSS_PARAM) |
1820 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1821 BIT(NL80211_STA_INFO_RX_DROP_MISC) |
1822 BIT(NL80211_STA_INFO_BEACON_LOSS);
1824 ktime_get_ts(&uptime);
1825 sinfo->connected_time = uptime.tv_sec - sta->last_connected;
1826 sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
1828 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1829 BIT(NL80211_STA_INFO_TX_BYTES)))) {
1830 sinfo->tx_bytes = 0;
1831 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1832 sinfo->tx_bytes += sta->tx_bytes[ac];
1833 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1836 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
1837 sinfo->tx_packets = 0;
1838 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1839 sinfo->tx_packets += sta->tx_packets[ac];
1840 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
1843 if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1844 BIT(NL80211_STA_INFO_RX_BYTES)))) {
1845 sinfo->rx_bytes = sta->rx_bytes;
1846 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
1849 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1850 sinfo->rx_packets = sta->rx_packets;
1851 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
1854 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
1855 sinfo->tx_retries = sta->tx_retry_count;
1856 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
1859 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
1860 sinfo->tx_failed = sta->tx_retry_failed;
1861 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
1864 sinfo->rx_dropped_misc = sta->rx_dropped;
1865 sinfo->beacon_loss_count = sta->beacon_loss_count;
1867 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1868 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1869 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1870 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1871 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1874 if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
1875 (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
1876 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
1877 sinfo->signal = (s8)sta->last_signal;
1878 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
1881 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
1882 sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
1883 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
1887 if (sta->chains &&
1888 !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1889 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1890 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1891 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
1893 sinfo->chains = sta->chains;
1894 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1895 sinfo->chain_signal[i] = sta->chain_signal_last[i];
1896 sinfo->chain_signal_avg[i] =
1897 (s8) -ewma_read(&sta->chain_signal_avg[i]);
1901 if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
1902 sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
1903 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
1906 if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
1907 sta_set_rate_info_rx(sta, &sinfo->rxrate);
1908 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
1911 sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
1912 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
1913 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
1915 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
1916 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
1917 tidstats->rx_msdu = sta->rx_msdu[i];
1920 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
1921 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
1922 tidstats->tx_msdu = sta->tx_msdu[i];
1925 if (!(tidstats->filled &
1926 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
1927 local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1928 tidstats->filled |=
1929 BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
1930 tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
1933 if (!(tidstats->filled &
1934 BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
1935 local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1936 tidstats->filled |=
1937 BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
1938 tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
1942 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1943 #ifdef CONFIG_MAC80211_MESH
1944 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
1945 BIT(NL80211_STA_INFO_PLID) |
1946 BIT(NL80211_STA_INFO_PLINK_STATE) |
1947 BIT(NL80211_STA_INFO_LOCAL_PM) |
1948 BIT(NL80211_STA_INFO_PEER_PM) |
1949 BIT(NL80211_STA_INFO_NONPEER_PM);
1951 sinfo->llid = sta->llid;
1952 sinfo->plid = sta->plid;
1953 sinfo->plink_state = sta->plink_state;
1954 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
1955 sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
1956 sinfo->t_offset = sta->t_offset;
1958 sinfo->local_pm = sta->local_pm;
1959 sinfo->peer_pm = sta->peer_pm;
1960 sinfo->nonpeer_pm = sta->nonpeer_pm;
1961 #endif
1964 sinfo->bss_param.flags = 0;
1965 if (sdata->vif.bss_conf.use_cts_prot)
1966 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1967 if (sdata->vif.bss_conf.use_short_preamble)
1968 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1969 if (sdata->vif.bss_conf.use_short_slot)
1970 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1971 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
1972 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1974 sinfo->sta_flags.set = 0;
1975 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1976 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1977 BIT(NL80211_STA_FLAG_WME) |
1978 BIT(NL80211_STA_FLAG_MFP) |
1979 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1980 BIT(NL80211_STA_FLAG_ASSOCIATED) |
1981 BIT(NL80211_STA_FLAG_TDLS_PEER);
1982 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1983 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1984 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1985 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
1986 if (sta->sta.wme)
1987 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1988 if (test_sta_flag(sta, WLAN_STA_MFP))
1989 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1990 if (test_sta_flag(sta, WLAN_STA_AUTH))
1991 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
1992 if (test_sta_flag(sta, WLAN_STA_ASSOC))
1993 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1994 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1995 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
1997 /* check if the driver has a SW RC implementation */
1998 if (ref && ref->ops->get_expected_throughput)
1999 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2000 else
2001 thr = drv_get_expected_throughput(local, &sta->sta);
2003 if (thr != 0) {
2004 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2005 sinfo->expected_throughput = thr;