GNU Linux-libre 4.9.309-gnu1
[releases.git] / net / mac80211 / sta_info.c
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
5  * Copyright (C) 2015 - 2016 Intel Deutschland GmbH
6  * Copyright (C) 2018-2020 Intel Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/etherdevice.h>
16 #include <linux/netdevice.h>
17 #include <linux/types.h>
18 #include <linux/slab.h>
19 #include <linux/skbuff.h>
20 #include <linux/if_arp.h>
21 #include <linux/timer.h>
22 #include <linux/rtnetlink.h>
23
24 #include <net/mac80211.h>
25 #include "ieee80211_i.h"
26 #include "driver-ops.h"
27 #include "rate.h"
28 #include "sta_info.h"
29 #include "debugfs_sta.h"
30 #include "mesh.h"
31 #include "wme.h"
32
33 /**
34  * DOC: STA information lifetime rules
35  *
36  * STA info structures (&struct sta_info) are managed in a hash table
37  * for faster lookup and a list for iteration. They are managed using
38  * RCU, i.e. access to the list and hash table is protected by RCU.
39  *
40  * Upon allocating a STA info structure with sta_info_alloc(), the caller
41  * owns that structure. It must then insert it into the hash table using
42  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
43  * case (which acquires an rcu read section but must not be called from
44  * within one) will the pointer still be valid after the call. Note that
45  * the caller may not do much with the STA info before inserting it, in
46  * particular, it may not start any mesh peer link management or add
47  * encryption keys.
48  *
49  * When the insertion fails (sta_info_insert()) returns non-zero), the
50  * structure will have been freed by sta_info_insert()!
51  *
52  * Station entries are added by mac80211 when you establish a link with a
53  * peer. This means different things for the different type of interfaces
54  * we support. For a regular station this mean we add the AP sta when we
55  * receive an association response from the AP. For IBSS this occurs when
56  * get to know about a peer on the same IBSS. For WDS we add the sta for
57  * the peer immediately upon device open. When using AP mode we add stations
58  * for each respective station upon request from userspace through nl80211.
59  *
60  * In order to remove a STA info structure, various sta_info_destroy_*()
61  * calls are available.
62  *
63  * There is no concept of ownership on a STA entry, each structure is
64  * owned by the global hash table/list until it is removed. All users of
65  * the structure need to be RCU protected so that the structure won't be
66  * freed before they are done using it.
67  */
68
69 static const struct rhashtable_params sta_rht_params = {
70         .nelem_hint = 3, /* start small */
71         .automatic_shrinking = true,
72         .head_offset = offsetof(struct sta_info, hash_node),
73         .key_offset = offsetof(struct sta_info, addr),
74         .key_len = ETH_ALEN,
75         .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
76 };
77
78 /* Caller must hold local->sta_mtx */
79 static int sta_info_hash_del(struct ieee80211_local *local,
80                              struct sta_info *sta)
81 {
82         return rhltable_remove(&local->sta_hash, &sta->hash_node,
83                                sta_rht_params);
84 }
85
86 static void __cleanup_single_sta(struct sta_info *sta)
87 {
88         int ac, i;
89         struct tid_ampdu_tx *tid_tx;
90         struct ieee80211_sub_if_data *sdata = sta->sdata;
91         struct ieee80211_local *local = sdata->local;
92         struct fq *fq = &local->fq;
93         struct ps_data *ps;
94
95         if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
96             test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
97             test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
98                 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
99                     sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
100                         ps = &sdata->bss->ps;
101                 else if (ieee80211_vif_is_mesh(&sdata->vif))
102                         ps = &sdata->u.mesh.ps;
103                 else
104                         return;
105
106                 clear_sta_flag(sta, WLAN_STA_PS_STA);
107                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
108                 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
109
110                 atomic_dec(&ps->num_sta_ps);
111         }
112
113         if (sta->sta.txq[0]) {
114                 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
115                         struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
116
117                         spin_lock_bh(&fq->lock);
118                         ieee80211_txq_purge(local, txqi);
119                         spin_unlock_bh(&fq->lock);
120                 }
121         }
122
123         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
124                 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
125                 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
126                 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
127         }
128
129         if (ieee80211_vif_is_mesh(&sdata->vif))
130                 mesh_sta_cleanup(sta);
131
132         cancel_work_sync(&sta->drv_deliver_wk);
133
134         /*
135          * Destroy aggregation state here. It would be nice to wait for the
136          * driver to finish aggregation stop and then clean up, but for now
137          * drivers have to handle aggregation stop being requested, followed
138          * directly by station destruction.
139          */
140         for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
141                 kfree(sta->ampdu_mlme.tid_start_tx[i]);
142                 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
143                 if (!tid_tx)
144                         continue;
145                 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
146                 kfree(tid_tx);
147         }
148 }
149
150 static void cleanup_single_sta(struct sta_info *sta)
151 {
152         struct ieee80211_sub_if_data *sdata = sta->sdata;
153         struct ieee80211_local *local = sdata->local;
154
155         __cleanup_single_sta(sta);
156         sta_info_free(local, sta);
157 }
158
159 struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
160                                          const u8 *addr)
161 {
162         return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
163 }
164
165 /* protected by RCU */
166 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
167                               const u8 *addr)
168 {
169         struct ieee80211_local *local = sdata->local;
170         struct rhlist_head *tmp;
171         struct sta_info *sta;
172
173         rcu_read_lock();
174         for_each_sta_info(local, addr, sta, tmp) {
175                 if (sta->sdata == sdata) {
176                         rcu_read_unlock();
177                         /* this is safe as the caller must already hold
178                          * another rcu read section or the mutex
179                          */
180                         return sta;
181                 }
182         }
183         rcu_read_unlock();
184         return NULL;
185 }
186
187 /*
188  * Get sta info either from the specified interface
189  * or from one of its vlans
190  */
191 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
192                                   const u8 *addr)
193 {
194         struct ieee80211_local *local = sdata->local;
195         struct rhlist_head *tmp;
196         struct sta_info *sta;
197
198         rcu_read_lock();
199         for_each_sta_info(local, addr, sta, tmp) {
200                 if (sta->sdata == sdata ||
201                     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
202                         rcu_read_unlock();
203                         /* this is safe as the caller must already hold
204                          * another rcu read section or the mutex
205                          */
206                         return sta;
207                 }
208         }
209         rcu_read_unlock();
210         return NULL;
211 }
212
213 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
214                                      int idx)
215 {
216         struct ieee80211_local *local = sdata->local;
217         struct sta_info *sta;
218         int i = 0;
219
220         list_for_each_entry_rcu(sta, &local->sta_list, list) {
221                 if (sdata != sta->sdata)
222                         continue;
223                 if (i < idx) {
224                         ++i;
225                         continue;
226                 }
227                 return sta;
228         }
229
230         return NULL;
231 }
232
233 /**
234  * sta_info_free - free STA
235  *
236  * @local: pointer to the global information
237  * @sta: STA info to free
238  *
239  * This function must undo everything done by sta_info_alloc()
240  * that may happen before sta_info_insert(). It may only be
241  * called when sta_info_insert() has not been attempted (and
242  * if that fails, the station is freed anyway.)
243  */
244 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
245 {
246         /*
247          * If we had used sta_info_pre_move_state() then we might not
248          * have gone through the state transitions down again, so do
249          * it here now (and warn if it's inserted).
250          *
251          * This will clear state such as fast TX/RX that may have been
252          * allocated during state transitions.
253          */
254         while (sta->sta_state > IEEE80211_STA_NONE) {
255                 int ret;
256
257                 WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
258
259                 ret = sta_info_move_state(sta, sta->sta_state - 1);
260                 if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
261                         break;
262         }
263
264         if (sta->rate_ctrl)
265                 rate_control_free_sta(sta);
266
267         sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
268
269         if (sta->sta.txq[0])
270                 kfree(to_txq_info(sta->sta.txq[0]));
271         kfree(rcu_dereference_raw(sta->sta.rates));
272 #ifdef CONFIG_MAC80211_MESH
273         kfree(sta->mesh);
274 #endif
275         free_percpu(sta->pcpu_rx_stats);
276         kfree(sta);
277 }
278
279 /* Caller must hold local->sta_mtx */
280 static int sta_info_hash_add(struct ieee80211_local *local,
281                              struct sta_info *sta)
282 {
283         return rhltable_insert(&local->sta_hash, &sta->hash_node,
284                                sta_rht_params);
285 }
286
287 static void sta_deliver_ps_frames(struct work_struct *wk)
288 {
289         struct sta_info *sta;
290
291         sta = container_of(wk, struct sta_info, drv_deliver_wk);
292
293         if (sta->dead)
294                 return;
295
296         local_bh_disable();
297         if (!test_sta_flag(sta, WLAN_STA_PS_STA))
298                 ieee80211_sta_ps_deliver_wakeup(sta);
299         else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
300                 ieee80211_sta_ps_deliver_poll_response(sta);
301         else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
302                 ieee80211_sta_ps_deliver_uapsd(sta);
303         local_bh_enable();
304 }
305
306 static int sta_prepare_rate_control(struct ieee80211_local *local,
307                                     struct sta_info *sta, gfp_t gfp)
308 {
309         if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
310                 return 0;
311
312         sta->rate_ctrl = local->rate_ctrl;
313         sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
314                                                      sta, gfp);
315         if (!sta->rate_ctrl_priv)
316                 return -ENOMEM;
317
318         return 0;
319 }
320
321 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
322                                 const u8 *addr, gfp_t gfp)
323 {
324         struct ieee80211_local *local = sdata->local;
325         struct ieee80211_hw *hw = &local->hw;
326         struct sta_info *sta;
327         int i;
328
329         sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
330         if (!sta)
331                 return NULL;
332
333         if (ieee80211_hw_check(hw, USES_RSS)) {
334                 sta->pcpu_rx_stats =
335                         alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
336                 if (!sta->pcpu_rx_stats)
337                         goto free;
338         }
339
340         spin_lock_init(&sta->lock);
341         spin_lock_init(&sta->ps_lock);
342         INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
343         INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
344         mutex_init(&sta->ampdu_mlme.mtx);
345 #ifdef CONFIG_MAC80211_MESH
346         if (ieee80211_vif_is_mesh(&sdata->vif)) {
347                 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
348                 if (!sta->mesh)
349                         goto free;
350                 spin_lock_init(&sta->mesh->plink_lock);
351                 if (ieee80211_vif_is_mesh(&sdata->vif) &&
352                     !sdata->u.mesh.user_mpm)
353                         init_timer(&sta->mesh->plink_timer);
354                 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
355         }
356 #endif
357
358         memcpy(sta->addr, addr, ETH_ALEN);
359         memcpy(sta->sta.addr, addr, ETH_ALEN);
360         sta->sta.max_rx_aggregation_subframes =
361                 local->hw.max_rx_aggregation_subframes;
362
363         sta->local = local;
364         sta->sdata = sdata;
365         sta->rx_stats.last_rx = jiffies;
366
367         u64_stats_init(&sta->rx_stats.syncp);
368
369         ieee80211_init_frag_cache(&sta->frags);
370
371         sta->sta_state = IEEE80211_STA_NONE;
372
373         /* Mark TID as unreserved */
374         sta->reserved_tid = IEEE80211_TID_UNRESERVED;
375
376         sta->last_connected = ktime_get_seconds();
377         ewma_signal_init(&sta->rx_stats_avg.signal);
378         for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
379                 ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
380
381         if (local->ops->wake_tx_queue) {
382                 void *txq_data;
383                 int size = sizeof(struct txq_info) +
384                            ALIGN(hw->txq_data_size, sizeof(void *));
385
386                 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
387                 if (!txq_data)
388                         goto free;
389
390                 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
391                         struct txq_info *txq = txq_data + i * size;
392
393                         ieee80211_txq_init(sdata, sta, txq, i);
394                 }
395         }
396
397         if (sta_prepare_rate_control(local, sta, gfp))
398                 goto free_txq;
399
400         for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
401                 /*
402                  * timer_to_tid must be initialized with identity mapping
403                  * to enable session_timer's data differentiation. See
404                  * sta_rx_agg_session_timer_expired for usage.
405                  */
406                 sta->timer_to_tid[i] = i;
407         }
408         for (i = 0; i < IEEE80211_NUM_ACS; i++) {
409                 skb_queue_head_init(&sta->ps_tx_buf[i]);
410                 skb_queue_head_init(&sta->tx_filtered[i]);
411         }
412
413         for (i = 0; i < IEEE80211_NUM_TIDS; i++)
414                 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
415
416         sta->sta.smps_mode = IEEE80211_SMPS_OFF;
417         if (sdata->vif.type == NL80211_IFTYPE_AP ||
418             sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
419                 struct ieee80211_supported_band *sband;
420                 u8 smps;
421
422                 sband = ieee80211_get_sband(sdata);
423                 if (!sband)
424                         goto free_txq;
425
426                 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
427                         IEEE80211_HT_CAP_SM_PS_SHIFT;
428                 /*
429                  * Assume that hostapd advertises our caps in the beacon and
430                  * this is the known_smps_mode for a station that just assciated
431                  */
432                 switch (smps) {
433                 case WLAN_HT_SMPS_CONTROL_DISABLED:
434                         sta->known_smps_mode = IEEE80211_SMPS_OFF;
435                         break;
436                 case WLAN_HT_SMPS_CONTROL_STATIC:
437                         sta->known_smps_mode = IEEE80211_SMPS_STATIC;
438                         break;
439                 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
440                         sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
441                         break;
442                 default:
443                         WARN_ON(1);
444                 }
445         }
446
447         sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
448
449         sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
450
451         return sta;
452
453 free_txq:
454         if (sta->sta.txq[0])
455                 kfree(to_txq_info(sta->sta.txq[0]));
456 free:
457         free_percpu(sta->pcpu_rx_stats);
458 #ifdef CONFIG_MAC80211_MESH
459         kfree(sta->mesh);
460 #endif
461         kfree(sta);
462         return NULL;
463 }
464
465 static int sta_info_insert_check(struct sta_info *sta)
466 {
467         struct ieee80211_sub_if_data *sdata = sta->sdata;
468
469         /*
470          * Can't be a WARN_ON because it can be triggered through a race:
471          * something inserts a STA (on one CPU) without holding the RTNL
472          * and another CPU turns off the net device.
473          */
474         if (unlikely(!ieee80211_sdata_running(sdata)))
475                 return -ENETDOWN;
476
477         if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
478                     is_multicast_ether_addr(sta->sta.addr)))
479                 return -EINVAL;
480
481         /* The RCU read lock is required by rhashtable due to
482          * asynchronous resize/rehash.  We also require the mutex
483          * for correctness.
484          */
485         rcu_read_lock();
486         lockdep_assert_held(&sdata->local->sta_mtx);
487         if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
488             ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
489                 rcu_read_unlock();
490                 return -ENOTUNIQ;
491         }
492         rcu_read_unlock();
493
494         return 0;
495 }
496
497 static int sta_info_insert_drv_state(struct ieee80211_local *local,
498                                      struct ieee80211_sub_if_data *sdata,
499                                      struct sta_info *sta)
500 {
501         enum ieee80211_sta_state state;
502         int err = 0;
503
504         for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
505                 err = drv_sta_state(local, sdata, sta, state, state + 1);
506                 if (err)
507                         break;
508         }
509
510         if (!err) {
511                 /*
512                  * Drivers using legacy sta_add/sta_remove callbacks only
513                  * get uploaded set to true after sta_add is called.
514                  */
515                 if (!local->ops->sta_add)
516                         sta->uploaded = true;
517                 return 0;
518         }
519
520         if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
521                 sdata_info(sdata,
522                            "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
523                            sta->sta.addr, state + 1, err);
524                 err = 0;
525         }
526
527         /* unwind on error */
528         for (; state > IEEE80211_STA_NOTEXIST; state--)
529                 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
530
531         return err;
532 }
533
534 /*
535  * should be called with sta_mtx locked
536  * this function replaces the mutex lock
537  * with a RCU lock
538  */
539 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
540 {
541         struct ieee80211_local *local = sta->local;
542         struct ieee80211_sub_if_data *sdata = sta->sdata;
543         struct station_info *sinfo;
544         int err = 0;
545
546         lockdep_assert_held(&local->sta_mtx);
547
548         sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
549         if (!sinfo) {
550                 err = -ENOMEM;
551                 goto out_err;
552         }
553
554         /* check if STA exists already */
555         if (sta_info_get_bss(sdata, sta->sta.addr)) {
556                 err = -EEXIST;
557                 goto out_err;
558         }
559
560         local->num_sta++;
561         local->sta_generation++;
562         smp_mb();
563
564         /* simplify things and don't accept BA sessions yet */
565         set_sta_flag(sta, WLAN_STA_BLOCK_BA);
566
567         /* make the station visible */
568         err = sta_info_hash_add(local, sta);
569         if (err)
570                 goto out_drop_sta;
571
572         list_add_tail_rcu(&sta->list, &local->sta_list);
573
574         /* notify driver */
575         err = sta_info_insert_drv_state(local, sdata, sta);
576         if (err)
577                 goto out_remove;
578
579         set_sta_flag(sta, WLAN_STA_INSERTED);
580         /* accept BA sessions now */
581         clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
582
583         ieee80211_sta_debugfs_add(sta);
584         rate_control_add_sta_debugfs(sta);
585
586         sinfo->generation = local->sta_generation;
587         cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
588         kfree(sinfo);
589
590         sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
591
592         /* move reference to rcu-protected */
593         rcu_read_lock();
594         mutex_unlock(&local->sta_mtx);
595
596         if (ieee80211_vif_is_mesh(&sdata->vif))
597                 mesh_accept_plinks_update(sdata);
598
599         return 0;
600  out_remove:
601         sta_info_hash_del(local, sta);
602         list_del_rcu(&sta->list);
603  out_drop_sta:
604         local->num_sta--;
605         synchronize_net();
606         cleanup_single_sta(sta);
607  out_err:
608         mutex_unlock(&local->sta_mtx);
609         kfree(sinfo);
610         rcu_read_lock();
611         return err;
612 }
613
614 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
615 {
616         struct ieee80211_local *local = sta->local;
617         int err;
618
619         might_sleep();
620
621         mutex_lock(&local->sta_mtx);
622
623         err = sta_info_insert_check(sta);
624         if (err) {
625                 sta_info_free(local, sta);
626                 mutex_unlock(&local->sta_mtx);
627                 rcu_read_lock();
628                 return err;
629         }
630
631         return sta_info_insert_finish(sta);
632 }
633
634 int sta_info_insert(struct sta_info *sta)
635 {
636         int err = sta_info_insert_rcu(sta);
637
638         rcu_read_unlock();
639
640         return err;
641 }
642
643 static inline void __bss_tim_set(u8 *tim, u16 id)
644 {
645         /*
646          * This format has been mandated by the IEEE specifications,
647          * so this line may not be changed to use the __set_bit() format.
648          */
649         tim[id / 8] |= (1 << (id % 8));
650 }
651
652 static inline void __bss_tim_clear(u8 *tim, u16 id)
653 {
654         /*
655          * This format has been mandated by the IEEE specifications,
656          * so this line may not be changed to use the __clear_bit() format.
657          */
658         tim[id / 8] &= ~(1 << (id % 8));
659 }
660
661 static inline bool __bss_tim_get(u8 *tim, u16 id)
662 {
663         /*
664          * This format has been mandated by the IEEE specifications,
665          * so this line may not be changed to use the test_bit() format.
666          */
667         return tim[id / 8] & (1 << (id % 8));
668 }
669
670 static unsigned long ieee80211_tids_for_ac(int ac)
671 {
672         /* If we ever support TIDs > 7, this obviously needs to be adjusted */
673         switch (ac) {
674         case IEEE80211_AC_VO:
675                 return BIT(6) | BIT(7);
676         case IEEE80211_AC_VI:
677                 return BIT(4) | BIT(5);
678         case IEEE80211_AC_BE:
679                 return BIT(0) | BIT(3);
680         case IEEE80211_AC_BK:
681                 return BIT(1) | BIT(2);
682         default:
683                 WARN_ON(1);
684                 return 0;
685         }
686 }
687
688 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
689 {
690         struct ieee80211_local *local = sta->local;
691         struct ps_data *ps;
692         bool indicate_tim = false;
693         u8 ignore_for_tim = sta->sta.uapsd_queues;
694         int ac;
695         u16 id = sta->sta.aid;
696
697         if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
698             sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
699                 if (WARN_ON_ONCE(!sta->sdata->bss))
700                         return;
701
702                 ps = &sta->sdata->bss->ps;
703 #ifdef CONFIG_MAC80211_MESH
704         } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
705                 ps = &sta->sdata->u.mesh.ps;
706 #endif
707         } else {
708                 return;
709         }
710
711         /* No need to do anything if the driver does all */
712         if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
713                 return;
714
715         if (sta->dead)
716                 goto done;
717
718         /*
719          * If all ACs are delivery-enabled then we should build
720          * the TIM bit for all ACs anyway; if only some are then
721          * we ignore those and build the TIM bit using only the
722          * non-enabled ones.
723          */
724         if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
725                 ignore_for_tim = 0;
726
727         if (ignore_pending)
728                 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
729
730         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
731                 unsigned long tids;
732
733                 if (ignore_for_tim & BIT(ac))
734                         continue;
735
736                 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
737                                 !skb_queue_empty(&sta->ps_tx_buf[ac]);
738                 if (indicate_tim)
739                         break;
740
741                 tids = ieee80211_tids_for_ac(ac);
742
743                 indicate_tim |=
744                         sta->driver_buffered_tids & tids;
745                 indicate_tim |=
746                         sta->txq_buffered_tids & tids;
747         }
748
749  done:
750         spin_lock_bh(&local->tim_lock);
751
752         if (indicate_tim == __bss_tim_get(ps->tim, id))
753                 goto out_unlock;
754
755         if (indicate_tim)
756                 __bss_tim_set(ps->tim, id);
757         else
758                 __bss_tim_clear(ps->tim, id);
759
760         if (local->ops->set_tim && !WARN_ON(sta->dead)) {
761                 local->tim_in_locked_section = true;
762                 drv_set_tim(local, &sta->sta, indicate_tim);
763                 local->tim_in_locked_section = false;
764         }
765
766 out_unlock:
767         spin_unlock_bh(&local->tim_lock);
768 }
769
770 void sta_info_recalc_tim(struct sta_info *sta)
771 {
772         __sta_info_recalc_tim(sta, false);
773 }
774
775 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
776 {
777         struct ieee80211_tx_info *info;
778         int timeout;
779
780         if (!skb)
781                 return false;
782
783         info = IEEE80211_SKB_CB(skb);
784
785         /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
786         timeout = (sta->listen_interval *
787                    sta->sdata->vif.bss_conf.beacon_int *
788                    32 / 15625) * HZ;
789         if (timeout < STA_TX_BUFFER_EXPIRE)
790                 timeout = STA_TX_BUFFER_EXPIRE;
791         return time_after(jiffies, info->control.jiffies + timeout);
792 }
793
794
795 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
796                                                 struct sta_info *sta, int ac)
797 {
798         unsigned long flags;
799         struct sk_buff *skb;
800
801         /*
802          * First check for frames that should expire on the filtered
803          * queue. Frames here were rejected by the driver and are on
804          * a separate queue to avoid reordering with normal PS-buffered
805          * frames. They also aren't accounted for right now in the
806          * total_ps_buffered counter.
807          */
808         for (;;) {
809                 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
810                 skb = skb_peek(&sta->tx_filtered[ac]);
811                 if (sta_info_buffer_expired(sta, skb))
812                         skb = __skb_dequeue(&sta->tx_filtered[ac]);
813                 else
814                         skb = NULL;
815                 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
816
817                 /*
818                  * Frames are queued in order, so if this one
819                  * hasn't expired yet we can stop testing. If
820                  * we actually reached the end of the queue we
821                  * also need to stop, of course.
822                  */
823                 if (!skb)
824                         break;
825                 ieee80211_free_txskb(&local->hw, skb);
826         }
827
828         /*
829          * Now also check the normal PS-buffered queue, this will
830          * only find something if the filtered queue was emptied
831          * since the filtered frames are all before the normal PS
832          * buffered frames.
833          */
834         for (;;) {
835                 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
836                 skb = skb_peek(&sta->ps_tx_buf[ac]);
837                 if (sta_info_buffer_expired(sta, skb))
838                         skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
839                 else
840                         skb = NULL;
841                 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
842
843                 /*
844                  * frames are queued in order, so if this one
845                  * hasn't expired yet (or we reached the end of
846                  * the queue) we can stop testing
847                  */
848                 if (!skb)
849                         break;
850
851                 local->total_ps_buffered--;
852                 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
853                        sta->sta.addr);
854                 ieee80211_free_txskb(&local->hw, skb);
855         }
856
857         /*
858          * Finally, recalculate the TIM bit for this station -- it might
859          * now be clear because the station was too slow to retrieve its
860          * frames.
861          */
862         sta_info_recalc_tim(sta);
863
864         /*
865          * Return whether there are any frames still buffered, this is
866          * used to check whether the cleanup timer still needs to run,
867          * if there are no frames we don't need to rearm the timer.
868          */
869         return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
870                  skb_queue_empty(&sta->tx_filtered[ac]));
871 }
872
873 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
874                                              struct sta_info *sta)
875 {
876         bool have_buffered = false;
877         int ac;
878
879         /* This is only necessary for stations on BSS/MBSS interfaces */
880         if (!sta->sdata->bss &&
881             !ieee80211_vif_is_mesh(&sta->sdata->vif))
882                 return false;
883
884         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
885                 have_buffered |=
886                         sta_info_cleanup_expire_buffered_ac(local, sta, ac);
887
888         return have_buffered;
889 }
890
891 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
892 {
893         struct ieee80211_local *local;
894         struct ieee80211_sub_if_data *sdata;
895         int ret;
896
897         might_sleep();
898
899         if (!sta)
900                 return -ENOENT;
901
902         local = sta->local;
903         sdata = sta->sdata;
904
905         lockdep_assert_held(&local->sta_mtx);
906
907         /*
908          * Before removing the station from the driver and
909          * rate control, it might still start new aggregation
910          * sessions -- block that to make sure the tear-down
911          * will be sufficient.
912          */
913         set_sta_flag(sta, WLAN_STA_BLOCK_BA);
914         ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
915
916         /*
917          * Before removing the station from the driver there might be pending
918          * rx frames on RSS queues sent prior to the disassociation - wait for
919          * all such frames to be processed.
920          */
921         drv_sync_rx_queues(local, sta);
922
923         ret = sta_info_hash_del(local, sta);
924         if (WARN_ON(ret))
925                 return ret;
926
927         /*
928          * for TDLS peers, make sure to return to the base channel before
929          * removal.
930          */
931         if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
932                 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
933                 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
934         }
935
936         list_del_rcu(&sta->list);
937         sta->removed = true;
938
939         drv_sta_pre_rcu_remove(local, sta->sdata, sta);
940
941         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
942             rcu_access_pointer(sdata->u.vlan.sta) == sta)
943                 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
944
945         return 0;
946 }
947
948 static void __sta_info_destroy_part2(struct sta_info *sta)
949 {
950         struct ieee80211_local *local = sta->local;
951         struct ieee80211_sub_if_data *sdata = sta->sdata;
952         struct station_info *sinfo;
953         int ret;
954
955         /*
956          * NOTE: This assumes at least synchronize_net() was done
957          *       after _part1 and before _part2!
958          */
959
960         might_sleep();
961         lockdep_assert_held(&local->sta_mtx);
962
963         if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
964                 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
965                 WARN_ON_ONCE(ret);
966         }
967
968         /* now keys can no longer be reached */
969         ieee80211_free_sta_keys(local, sta);
970
971         /* disable TIM bit - last chance to tell driver */
972         __sta_info_recalc_tim(sta, true);
973
974         sta->dead = true;
975
976         local->num_sta--;
977         local->sta_generation++;
978
979         while (sta->sta_state > IEEE80211_STA_NONE) {
980                 ret = sta_info_move_state(sta, sta->sta_state - 1);
981                 if (ret) {
982                         WARN_ON_ONCE(1);
983                         break;
984                 }
985         }
986
987         if (sta->uploaded) {
988                 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
989                                     IEEE80211_STA_NOTEXIST);
990                 WARN_ON_ONCE(ret != 0);
991         }
992
993         sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
994
995         sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
996         if (sinfo)
997                 sta_set_sinfo(sta, sinfo);
998         cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
999         kfree(sinfo);
1000
1001         rate_control_remove_sta_debugfs(sta);
1002         ieee80211_sta_debugfs_remove(sta);
1003
1004         ieee80211_destroy_frag_cache(&sta->frags);
1005
1006         cleanup_single_sta(sta);
1007 }
1008
1009 int __must_check __sta_info_destroy(struct sta_info *sta)
1010 {
1011         int err = __sta_info_destroy_part1(sta);
1012
1013         if (err)
1014                 return err;
1015
1016         synchronize_net();
1017
1018         __sta_info_destroy_part2(sta);
1019
1020         return 0;
1021 }
1022
1023 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1024 {
1025         struct sta_info *sta;
1026         int ret;
1027
1028         mutex_lock(&sdata->local->sta_mtx);
1029         sta = sta_info_get(sdata, addr);
1030         ret = __sta_info_destroy(sta);
1031         mutex_unlock(&sdata->local->sta_mtx);
1032
1033         return ret;
1034 }
1035
1036 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1037                               const u8 *addr)
1038 {
1039         struct sta_info *sta;
1040         int ret;
1041
1042         mutex_lock(&sdata->local->sta_mtx);
1043         sta = sta_info_get_bss(sdata, addr);
1044         ret = __sta_info_destroy(sta);
1045         mutex_unlock(&sdata->local->sta_mtx);
1046
1047         return ret;
1048 }
1049
1050 static void sta_info_cleanup(unsigned long data)
1051 {
1052         struct ieee80211_local *local = (struct ieee80211_local *) data;
1053         struct sta_info *sta;
1054         bool timer_needed = false;
1055
1056         rcu_read_lock();
1057         list_for_each_entry_rcu(sta, &local->sta_list, list)
1058                 if (sta_info_cleanup_expire_buffered(local, sta))
1059                         timer_needed = true;
1060         rcu_read_unlock();
1061
1062         if (local->quiescing)
1063                 return;
1064
1065         if (!timer_needed)
1066                 return;
1067
1068         mod_timer(&local->sta_cleanup,
1069                   round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1070 }
1071
1072 int sta_info_init(struct ieee80211_local *local)
1073 {
1074         int err;
1075
1076         err = rhltable_init(&local->sta_hash, &sta_rht_params);
1077         if (err)
1078                 return err;
1079
1080         spin_lock_init(&local->tim_lock);
1081         mutex_init(&local->sta_mtx);
1082         INIT_LIST_HEAD(&local->sta_list);
1083
1084         setup_timer(&local->sta_cleanup, sta_info_cleanup,
1085                     (unsigned long)local);
1086         return 0;
1087 }
1088
1089 void sta_info_stop(struct ieee80211_local *local)
1090 {
1091         del_timer_sync(&local->sta_cleanup);
1092         rhltable_destroy(&local->sta_hash);
1093 }
1094
1095
1096 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1097 {
1098         struct ieee80211_local *local = sdata->local;
1099         struct sta_info *sta, *tmp;
1100         LIST_HEAD(free_list);
1101         int ret = 0;
1102
1103         might_sleep();
1104
1105         WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1106         WARN_ON(vlans && !sdata->bss);
1107
1108         mutex_lock(&local->sta_mtx);
1109         list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1110                 if (sdata == sta->sdata ||
1111                     (vlans && sdata->bss == sta->sdata->bss)) {
1112                         if (!WARN_ON(__sta_info_destroy_part1(sta)))
1113                                 list_add(&sta->free_list, &free_list);
1114                         ret++;
1115                 }
1116         }
1117
1118         if (!list_empty(&free_list)) {
1119                 synchronize_net();
1120                 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1121                         __sta_info_destroy_part2(sta);
1122         }
1123         mutex_unlock(&local->sta_mtx);
1124
1125         return ret;
1126 }
1127
1128 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1129                           unsigned long exp_time)
1130 {
1131         struct ieee80211_local *local = sdata->local;
1132         struct sta_info *sta, *tmp;
1133
1134         mutex_lock(&local->sta_mtx);
1135
1136         list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1137                 unsigned long last_active = ieee80211_sta_last_active(sta);
1138
1139                 if (sdata != sta->sdata)
1140                         continue;
1141
1142                 if (time_is_before_jiffies(last_active + exp_time)) {
1143                         sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1144                                 sta->sta.addr);
1145
1146                         if (ieee80211_vif_is_mesh(&sdata->vif) &&
1147                             test_sta_flag(sta, WLAN_STA_PS_STA))
1148                                 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1149
1150                         WARN_ON(__sta_info_destroy(sta));
1151                 }
1152         }
1153
1154         mutex_unlock(&local->sta_mtx);
1155 }
1156
1157 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1158                                                    const u8 *addr,
1159                                                    const u8 *localaddr)
1160 {
1161         struct ieee80211_local *local = hw_to_local(hw);
1162         struct rhlist_head *tmp;
1163         struct sta_info *sta;
1164
1165         /*
1166          * Just return a random station if localaddr is NULL
1167          * ... first in list.
1168          */
1169         for_each_sta_info(local, addr, sta, tmp) {
1170                 if (localaddr &&
1171                     !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1172                         continue;
1173                 if (!sta->uploaded)
1174                         return NULL;
1175                 return &sta->sta;
1176         }
1177
1178         return NULL;
1179 }
1180 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1181
1182 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1183                                          const u8 *addr)
1184 {
1185         struct sta_info *sta;
1186
1187         if (!vif)
1188                 return NULL;
1189
1190         sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1191         if (!sta)
1192                 return NULL;
1193
1194         if (!sta->uploaded)
1195                 return NULL;
1196
1197         return &sta->sta;
1198 }
1199 EXPORT_SYMBOL(ieee80211_find_sta);
1200
1201 /* powersave support code */
1202 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1203 {
1204         struct ieee80211_sub_if_data *sdata = sta->sdata;
1205         struct ieee80211_local *local = sdata->local;
1206         struct sk_buff_head pending;
1207         int filtered = 0, buffered = 0, ac, i;
1208         unsigned long flags;
1209         struct ps_data *ps;
1210
1211         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1212                 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1213                                      u.ap);
1214
1215         if (sdata->vif.type == NL80211_IFTYPE_AP)
1216                 ps = &sdata->bss->ps;
1217         else if (ieee80211_vif_is_mesh(&sdata->vif))
1218                 ps = &sdata->u.mesh.ps;
1219         else
1220                 return;
1221
1222         clear_sta_flag(sta, WLAN_STA_SP);
1223
1224         BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1225         sta->driver_buffered_tids = 0;
1226         sta->txq_buffered_tids = 0;
1227
1228         if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1229                 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1230
1231         if (sta->sta.txq[0]) {
1232                 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1233                         if (!txq_has_queue(sta->sta.txq[i]))
1234                                 continue;
1235
1236                         drv_wake_tx_queue(local, to_txq_info(sta->sta.txq[i]));
1237                 }
1238         }
1239
1240         skb_queue_head_init(&pending);
1241
1242         /* sync with ieee80211_tx_h_unicast_ps_buf */
1243         spin_lock(&sta->ps_lock);
1244         /* Send all buffered frames to the station */
1245         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1246                 int count = skb_queue_len(&pending), tmp;
1247
1248                 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1249                 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1250                 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1251                 tmp = skb_queue_len(&pending);
1252                 filtered += tmp - count;
1253                 count = tmp;
1254
1255                 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1256                 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1257                 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1258                 tmp = skb_queue_len(&pending);
1259                 buffered += tmp - count;
1260         }
1261
1262         ieee80211_add_pending_skbs(local, &pending);
1263
1264         /* now we're no longer in the deliver code */
1265         clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1266
1267         /* The station might have polled and then woken up before we responded,
1268          * so clear these flags now to avoid them sticking around.
1269          */
1270         clear_sta_flag(sta, WLAN_STA_PSPOLL);
1271         clear_sta_flag(sta, WLAN_STA_UAPSD);
1272         spin_unlock(&sta->ps_lock);
1273
1274         atomic_dec(&ps->num_sta_ps);
1275
1276         /* This station just woke up and isn't aware of our SMPS state */
1277         if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1278             !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1279                                            sdata->smps_mode) &&
1280             sta->known_smps_mode != sdata->bss->req_smps &&
1281             sta_info_tx_streams(sta) != 1) {
1282                 ht_dbg(sdata,
1283                        "%pM just woke up and MIMO capable - update SMPS\n",
1284                        sta->sta.addr);
1285                 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1286                                            sta->sta.addr,
1287                                            sdata->vif.bss_conf.bssid);
1288         }
1289
1290         local->total_ps_buffered -= buffered;
1291
1292         sta_info_recalc_tim(sta);
1293
1294         ps_dbg(sdata,
1295                "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1296                sta->sta.addr, sta->sta.aid, filtered, buffered);
1297
1298         ieee80211_check_fast_xmit(sta);
1299 }
1300
1301 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1302                                          enum ieee80211_frame_release_type reason,
1303                                          bool call_driver, bool more_data)
1304 {
1305         struct ieee80211_sub_if_data *sdata = sta->sdata;
1306         struct ieee80211_local *local = sdata->local;
1307         struct ieee80211_qos_hdr *nullfunc;
1308         struct sk_buff *skb;
1309         int size = sizeof(*nullfunc);
1310         __le16 fc;
1311         bool qos = sta->sta.wme;
1312         struct ieee80211_tx_info *info;
1313         struct ieee80211_chanctx_conf *chanctx_conf;
1314
1315         if (qos) {
1316                 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1317                                  IEEE80211_STYPE_QOS_NULLFUNC |
1318                                  IEEE80211_FCTL_FROMDS);
1319         } else {
1320                 size -= 2;
1321                 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1322                                  IEEE80211_STYPE_NULLFUNC |
1323                                  IEEE80211_FCTL_FROMDS);
1324         }
1325
1326         skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1327         if (!skb)
1328                 return;
1329
1330         skb_reserve(skb, local->hw.extra_tx_headroom);
1331
1332         nullfunc = (void *) skb_put(skb, size);
1333         nullfunc->frame_control = fc;
1334         nullfunc->duration_id = 0;
1335         memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1336         memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1337         memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1338         nullfunc->seq_ctrl = 0;
1339
1340         skb->priority = tid;
1341         skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1342         if (qos) {
1343                 nullfunc->qos_ctrl = cpu_to_le16(tid);
1344
1345                 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1346                         nullfunc->qos_ctrl |=
1347                                 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1348                         if (more_data)
1349                                 nullfunc->frame_control |=
1350                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1351                 }
1352         }
1353
1354         info = IEEE80211_SKB_CB(skb);
1355
1356         /*
1357          * Tell TX path to send this frame even though the
1358          * STA may still remain is PS mode after this frame
1359          * exchange. Also set EOSP to indicate this packet
1360          * ends the poll/service period.
1361          */
1362         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1363                        IEEE80211_TX_STATUS_EOSP |
1364                        IEEE80211_TX_CTL_REQ_TX_STATUS;
1365
1366         info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1367
1368         if (call_driver)
1369                 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1370                                           reason, false);
1371
1372         skb->dev = sdata->dev;
1373
1374         rcu_read_lock();
1375         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1376         if (WARN_ON(!chanctx_conf)) {
1377                 rcu_read_unlock();
1378                 kfree_skb(skb);
1379                 return;
1380         }
1381
1382         info->band = chanctx_conf->def.chan->band;
1383         ieee80211_xmit(sdata, sta, skb);
1384         rcu_read_unlock();
1385 }
1386
1387 static int find_highest_prio_tid(unsigned long tids)
1388 {
1389         /* lower 3 TIDs aren't ordered perfectly */
1390         if (tids & 0xF8)
1391                 return fls(tids) - 1;
1392         /* TID 0 is BE just like TID 3 */
1393         if (tids & BIT(0))
1394                 return 0;
1395         return fls(tids) - 1;
1396 }
1397
1398 /* Indicates if the MORE_DATA bit should be set in the last
1399  * frame obtained by ieee80211_sta_ps_get_frames.
1400  * Note that driver_release_tids is relevant only if
1401  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1402  */
1403 static bool
1404 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1405                            enum ieee80211_frame_release_type reason,
1406                            unsigned long driver_release_tids)
1407 {
1408         int ac;
1409
1410         /* If the driver has data on more than one TID then
1411          * certainly there's more data if we release just a
1412          * single frame now (from a single TID). This will
1413          * only happen for PS-Poll.
1414          */
1415         if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1416             hweight16(driver_release_tids) > 1)
1417                 return true;
1418
1419         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1420                 if (ignored_acs & BIT(ac))
1421                         continue;
1422
1423                 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1424                     !skb_queue_empty(&sta->ps_tx_buf[ac]))
1425                         return true;
1426         }
1427
1428         return false;
1429 }
1430
1431 static void
1432 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1433                             enum ieee80211_frame_release_type reason,
1434                             struct sk_buff_head *frames,
1435                             unsigned long *driver_release_tids)
1436 {
1437         struct ieee80211_sub_if_data *sdata = sta->sdata;
1438         struct ieee80211_local *local = sdata->local;
1439         int ac;
1440
1441         /* Get response frame(s) and more data bit for the last one. */
1442         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1443                 unsigned long tids;
1444
1445                 if (ignored_acs & BIT(ac))
1446                         continue;
1447
1448                 tids = ieee80211_tids_for_ac(ac);
1449
1450                 /* if we already have frames from software, then we can't also
1451                  * release from hardware queues
1452                  */
1453                 if (skb_queue_empty(frames)) {
1454                         *driver_release_tids |=
1455                                 sta->driver_buffered_tids & tids;
1456                         *driver_release_tids |= sta->txq_buffered_tids & tids;
1457                 }
1458
1459                 if (!*driver_release_tids) {
1460                         struct sk_buff *skb;
1461
1462                         while (n_frames > 0) {
1463                                 skb = skb_dequeue(&sta->tx_filtered[ac]);
1464                                 if (!skb) {
1465                                         skb = skb_dequeue(
1466                                                 &sta->ps_tx_buf[ac]);
1467                                         if (skb)
1468                                                 local->total_ps_buffered--;
1469                                 }
1470                                 if (!skb)
1471                                         break;
1472                                 n_frames--;
1473                                 __skb_queue_tail(frames, skb);
1474                         }
1475                 }
1476
1477                 /* If we have more frames buffered on this AC, then abort the
1478                  * loop since we can't send more data from other ACs before
1479                  * the buffered frames from this.
1480                  */
1481                 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1482                     !skb_queue_empty(&sta->ps_tx_buf[ac]))
1483                         break;
1484         }
1485 }
1486
1487 static void
1488 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1489                                   int n_frames, u8 ignored_acs,
1490                                   enum ieee80211_frame_release_type reason)
1491 {
1492         struct ieee80211_sub_if_data *sdata = sta->sdata;
1493         struct ieee80211_local *local = sdata->local;
1494         unsigned long driver_release_tids = 0;
1495         struct sk_buff_head frames;
1496         bool more_data;
1497
1498         /* Service or PS-Poll period starts */
1499         set_sta_flag(sta, WLAN_STA_SP);
1500
1501         __skb_queue_head_init(&frames);
1502
1503         ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1504                                     &frames, &driver_release_tids);
1505
1506         more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1507
1508         if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1509                 driver_release_tids =
1510                         BIT(find_highest_prio_tid(driver_release_tids));
1511
1512         if (skb_queue_empty(&frames) && !driver_release_tids) {
1513                 int tid;
1514
1515                 /*
1516                  * For PS-Poll, this can only happen due to a race condition
1517                  * when we set the TIM bit and the station notices it, but
1518                  * before it can poll for the frame we expire it.
1519                  *
1520                  * For uAPSD, this is said in the standard (11.2.1.5 h):
1521                  *      At each unscheduled SP for a non-AP STA, the AP shall
1522                  *      attempt to transmit at least one MSDU or MMPDU, but no
1523                  *      more than the value specified in the Max SP Length field
1524                  *      in the QoS Capability element from delivery-enabled ACs,
1525                  *      that are destined for the non-AP STA.
1526                  *
1527                  * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1528                  */
1529
1530                 /* This will evaluate to 1, 3, 5 or 7. */
1531                 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1532
1533                 ieee80211_send_null_response(sta, tid, reason, true, false);
1534         } else if (!driver_release_tids) {
1535                 struct sk_buff_head pending;
1536                 struct sk_buff *skb;
1537                 int num = 0;
1538                 u16 tids = 0;
1539                 bool need_null = false;
1540
1541                 skb_queue_head_init(&pending);
1542
1543                 while ((skb = __skb_dequeue(&frames))) {
1544                         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1545                         struct ieee80211_hdr *hdr = (void *) skb->data;
1546                         u8 *qoshdr = NULL;
1547
1548                         num++;
1549
1550                         /*
1551                          * Tell TX path to send this frame even though the
1552                          * STA may still remain is PS mode after this frame
1553                          * exchange.
1554                          */
1555                         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1556                         info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1557
1558                         /*
1559                          * Use MoreData flag to indicate whether there are
1560                          * more buffered frames for this STA
1561                          */
1562                         if (more_data || !skb_queue_empty(&frames))
1563                                 hdr->frame_control |=
1564                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1565                         else
1566                                 hdr->frame_control &=
1567                                         cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1568
1569                         if (ieee80211_is_data_qos(hdr->frame_control) ||
1570                             ieee80211_is_qos_nullfunc(hdr->frame_control))
1571                                 qoshdr = ieee80211_get_qos_ctl(hdr);
1572
1573                         tids |= BIT(skb->priority);
1574
1575                         __skb_queue_tail(&pending, skb);
1576
1577                         /* end service period after last frame or add one */
1578                         if (!skb_queue_empty(&frames))
1579                                 continue;
1580
1581                         if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1582                                 /* for PS-Poll, there's only one frame */
1583                                 info->flags |= IEEE80211_TX_STATUS_EOSP |
1584                                                IEEE80211_TX_CTL_REQ_TX_STATUS;
1585                                 break;
1586                         }
1587
1588                         /* For uAPSD, things are a bit more complicated. If the
1589                          * last frame has a QoS header (i.e. is a QoS-data or
1590                          * QoS-nulldata frame) then just set the EOSP bit there
1591                          * and be done.
1592                          * If the frame doesn't have a QoS header (which means
1593                          * it should be a bufferable MMPDU) then we can't set
1594                          * the EOSP bit in the QoS header; add a QoS-nulldata
1595                          * frame to the list to send it after the MMPDU.
1596                          *
1597                          * Note that this code is only in the mac80211-release
1598                          * code path, we assume that the driver will not buffer
1599                          * anything but QoS-data frames, or if it does, will
1600                          * create the QoS-nulldata frame by itself if needed.
1601                          *
1602                          * Cf. 802.11-2012 10.2.1.10 (c).
1603                          */
1604                         if (qoshdr) {
1605                                 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1606
1607                                 info->flags |= IEEE80211_TX_STATUS_EOSP |
1608                                                IEEE80211_TX_CTL_REQ_TX_STATUS;
1609                         } else {
1610                                 /* The standard isn't completely clear on this
1611                                  * as it says the more-data bit should be set
1612                                  * if there are more BUs. The QoS-Null frame
1613                                  * we're about to send isn't buffered yet, we
1614                                  * only create it below, but let's pretend it
1615                                  * was buffered just in case some clients only
1616                                  * expect more-data=0 when eosp=1.
1617                                  */
1618                                 hdr->frame_control |=
1619                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1620                                 need_null = true;
1621                                 num++;
1622                         }
1623                         break;
1624                 }
1625
1626                 drv_allow_buffered_frames(local, sta, tids, num,
1627                                           reason, more_data);
1628
1629                 ieee80211_add_pending_skbs(local, &pending);
1630
1631                 if (need_null)
1632                         ieee80211_send_null_response(
1633                                 sta, find_highest_prio_tid(tids),
1634                                 reason, false, false);
1635
1636                 sta_info_recalc_tim(sta);
1637         } else {
1638                 int tid;
1639
1640                 /*
1641                  * We need to release a frame that is buffered somewhere in the
1642                  * driver ... it'll have to handle that.
1643                  * Note that the driver also has to check the number of frames
1644                  * on the TIDs we're releasing from - if there are more than
1645                  * n_frames it has to set the more-data bit (if we didn't ask
1646                  * it to set it anyway due to other buffered frames); if there
1647                  * are fewer than n_frames it has to make sure to adjust that
1648                  * to allow the service period to end properly.
1649                  */
1650                 drv_release_buffered_frames(local, sta, driver_release_tids,
1651                                             n_frames, reason, more_data);
1652
1653                 /*
1654                  * Note that we don't recalculate the TIM bit here as it would
1655                  * most likely have no effect at all unless the driver told us
1656                  * that the TID(s) became empty before returning here from the
1657                  * release function.
1658                  * Either way, however, when the driver tells us that the TID(s)
1659                  * became empty or we find that a txq became empty, we'll do the
1660                  * TIM recalculation.
1661                  */
1662
1663                 if (!sta->sta.txq[0])
1664                         return;
1665
1666                 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1667                         if (!(driver_release_tids & BIT(tid)) ||
1668                             txq_has_queue(sta->sta.txq[tid]))
1669                                 continue;
1670
1671                         sta_info_recalc_tim(sta);
1672                         break;
1673                 }
1674         }
1675 }
1676
1677 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1678 {
1679         u8 ignore_for_response = sta->sta.uapsd_queues;
1680
1681         /*
1682          * If all ACs are delivery-enabled then we should reply
1683          * from any of them, if only some are enabled we reply
1684          * only from the non-enabled ones.
1685          */
1686         if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1687                 ignore_for_response = 0;
1688
1689         ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1690                                           IEEE80211_FRAME_RELEASE_PSPOLL);
1691 }
1692
1693 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1694 {
1695         int n_frames = sta->sta.max_sp;
1696         u8 delivery_enabled = sta->sta.uapsd_queues;
1697
1698         /*
1699          * If we ever grow support for TSPEC this might happen if
1700          * the TSPEC update from hostapd comes in between a trigger
1701          * frame setting WLAN_STA_UAPSD in the RX path and this
1702          * actually getting called.
1703          */
1704         if (!delivery_enabled)
1705                 return;
1706
1707         switch (sta->sta.max_sp) {
1708         case 1:
1709                 n_frames = 2;
1710                 break;
1711         case 2:
1712                 n_frames = 4;
1713                 break;
1714         case 3:
1715                 n_frames = 6;
1716                 break;
1717         case 0:
1718                 /* XXX: what is a good value? */
1719                 n_frames = 128;
1720                 break;
1721         }
1722
1723         ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1724                                           IEEE80211_FRAME_RELEASE_UAPSD);
1725 }
1726
1727 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1728                                struct ieee80211_sta *pubsta, bool block)
1729 {
1730         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1731
1732         trace_api_sta_block_awake(sta->local, pubsta, block);
1733
1734         if (block) {
1735                 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1736                 ieee80211_clear_fast_xmit(sta);
1737                 return;
1738         }
1739
1740         if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1741                 return;
1742
1743         if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1744                 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1745                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1746                 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1747         } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1748                    test_sta_flag(sta, WLAN_STA_UAPSD)) {
1749                 /* must be asleep in this case */
1750                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1751                 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1752         } else {
1753                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1754                 ieee80211_check_fast_xmit(sta);
1755         }
1756 }
1757 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1758
1759 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1760 {
1761         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1762         struct ieee80211_local *local = sta->local;
1763
1764         trace_api_eosp(local, pubsta);
1765
1766         clear_sta_flag(sta, WLAN_STA_SP);
1767 }
1768 EXPORT_SYMBOL(ieee80211_sta_eosp);
1769
1770 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1771 {
1772         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1773         enum ieee80211_frame_release_type reason;
1774         bool more_data;
1775
1776         trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1777
1778         reason = IEEE80211_FRAME_RELEASE_UAPSD;
1779         more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1780                                                reason, 0);
1781
1782         ieee80211_send_null_response(sta, tid, reason, false, more_data);
1783 }
1784 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1785
1786 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1787                                 u8 tid, bool buffered)
1788 {
1789         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1790
1791         if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1792                 return;
1793
1794         trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1795
1796         if (buffered)
1797                 set_bit(tid, &sta->driver_buffered_tids);
1798         else
1799                 clear_bit(tid, &sta->driver_buffered_tids);
1800
1801         sta_info_recalc_tim(sta);
1802 }
1803 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1804
1805 static void
1806 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
1807 {
1808         struct ieee80211_local *local = sdata->local;
1809         bool allow_p2p_go_ps = sdata->vif.p2p;
1810         struct sta_info *sta;
1811
1812         rcu_read_lock();
1813         list_for_each_entry_rcu(sta, &local->sta_list, list) {
1814                 if (sdata != sta->sdata ||
1815                     !test_sta_flag(sta, WLAN_STA_ASSOC))
1816                         continue;
1817                 if (!sta->sta.support_p2p_ps) {
1818                         allow_p2p_go_ps = false;
1819                         break;
1820                 }
1821         }
1822         rcu_read_unlock();
1823
1824         if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
1825                 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
1826                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
1827         }
1828 }
1829
1830 int sta_info_move_state(struct sta_info *sta,
1831                         enum ieee80211_sta_state new_state)
1832 {
1833         might_sleep();
1834
1835         if (sta->sta_state == new_state)
1836                 return 0;
1837
1838         /* check allowed transitions first */
1839
1840         switch (new_state) {
1841         case IEEE80211_STA_NONE:
1842                 if (sta->sta_state != IEEE80211_STA_AUTH)
1843                         return -EINVAL;
1844                 break;
1845         case IEEE80211_STA_AUTH:
1846                 if (sta->sta_state != IEEE80211_STA_NONE &&
1847                     sta->sta_state != IEEE80211_STA_ASSOC)
1848                         return -EINVAL;
1849                 break;
1850         case IEEE80211_STA_ASSOC:
1851                 if (sta->sta_state != IEEE80211_STA_AUTH &&
1852                     sta->sta_state != IEEE80211_STA_AUTHORIZED)
1853                         return -EINVAL;
1854                 break;
1855         case IEEE80211_STA_AUTHORIZED:
1856                 if (sta->sta_state != IEEE80211_STA_ASSOC)
1857                         return -EINVAL;
1858                 break;
1859         default:
1860                 WARN(1, "invalid state %d", new_state);
1861                 return -EINVAL;
1862         }
1863
1864         sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1865                 sta->sta.addr, new_state);
1866
1867         /*
1868          * notify the driver before the actual changes so it can
1869          * fail the transition
1870          */
1871         if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1872                 int err = drv_sta_state(sta->local, sta->sdata, sta,
1873                                         sta->sta_state, new_state);
1874                 if (err)
1875                         return err;
1876         }
1877
1878         /* reflect the change in all state variables */
1879
1880         switch (new_state) {
1881         case IEEE80211_STA_NONE:
1882                 if (sta->sta_state == IEEE80211_STA_AUTH)
1883                         clear_bit(WLAN_STA_AUTH, &sta->_flags);
1884                 break;
1885         case IEEE80211_STA_AUTH:
1886                 if (sta->sta_state == IEEE80211_STA_NONE) {
1887                         set_bit(WLAN_STA_AUTH, &sta->_flags);
1888                 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1889                         clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1890                         ieee80211_recalc_min_chandef(sta->sdata);
1891                         if (!sta->sta.support_p2p_ps)
1892                                 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1893                 }
1894                 break;
1895         case IEEE80211_STA_ASSOC:
1896                 if (sta->sta_state == IEEE80211_STA_AUTH) {
1897                         set_bit(WLAN_STA_ASSOC, &sta->_flags);
1898                         ieee80211_recalc_min_chandef(sta->sdata);
1899                         if (!sta->sta.support_p2p_ps)
1900                                 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1901                 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1902                         if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1903                             (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1904                              !sta->sdata->u.vlan.sta))
1905                                 atomic_dec(&sta->sdata->bss->num_mcast_sta);
1906                         clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1907                         ieee80211_clear_fast_xmit(sta);
1908                         ieee80211_clear_fast_rx(sta);
1909                 }
1910                 break;
1911         case IEEE80211_STA_AUTHORIZED:
1912                 if (sta->sta_state == IEEE80211_STA_ASSOC) {
1913                         if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1914                             (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1915                              !sta->sdata->u.vlan.sta))
1916                                 atomic_inc(&sta->sdata->bss->num_mcast_sta);
1917                         set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1918                         ieee80211_check_fast_xmit(sta);
1919                         ieee80211_check_fast_rx(sta);
1920                 }
1921                 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1922                     sta->sdata->vif.type == NL80211_IFTYPE_AP)
1923                         cfg80211_send_layer2_update(sta->sdata->dev,
1924                                                     sta->sta.addr);
1925                 break;
1926         default:
1927                 break;
1928         }
1929
1930         sta->sta_state = new_state;
1931
1932         return 0;
1933 }
1934
1935 u8 sta_info_tx_streams(struct sta_info *sta)
1936 {
1937         struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1938         u8 rx_streams;
1939
1940         if (!sta->sta.ht_cap.ht_supported)
1941                 return 1;
1942
1943         if (sta->sta.vht_cap.vht_supported) {
1944                 int i;
1945                 u16 tx_mcs_map =
1946                         le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1947
1948                 for (i = 7; i >= 0; i--)
1949                         if ((tx_mcs_map & (0x3 << (i * 2))) !=
1950                             IEEE80211_VHT_MCS_NOT_SUPPORTED)
1951                                 return i + 1;
1952         }
1953
1954         if (ht_cap->mcs.rx_mask[3])
1955                 rx_streams = 4;
1956         else if (ht_cap->mcs.rx_mask[2])
1957                 rx_streams = 3;
1958         else if (ht_cap->mcs.rx_mask[1])
1959                 rx_streams = 2;
1960         else
1961                 rx_streams = 1;
1962
1963         if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1964                 return rx_streams;
1965
1966         return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1967                         >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1968 }
1969
1970 static struct ieee80211_sta_rx_stats *
1971 sta_get_last_rx_stats(struct sta_info *sta)
1972 {
1973         struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
1974         struct ieee80211_local *local = sta->local;
1975         int cpu;
1976
1977         if (!ieee80211_hw_check(&local->hw, USES_RSS))
1978                 return stats;
1979
1980         for_each_possible_cpu(cpu) {
1981                 struct ieee80211_sta_rx_stats *cpustats;
1982
1983                 cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
1984
1985                 if (time_after(cpustats->last_rx, stats->last_rx))
1986                         stats = cpustats;
1987         }
1988
1989         return stats;
1990 }
1991
1992 static void sta_stats_decode_rate(struct ieee80211_local *local, u16 rate,
1993                                   struct rate_info *rinfo)
1994 {
1995         rinfo->bw = (rate & STA_STATS_RATE_BW_MASK) >>
1996                 STA_STATS_RATE_BW_SHIFT;
1997
1998         if (rate & STA_STATS_RATE_VHT) {
1999                 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2000                 rinfo->mcs = rate & 0xf;
2001                 rinfo->nss = (rate & 0xf0) >> 4;
2002         } else if (rate & STA_STATS_RATE_HT) {
2003                 rinfo->flags = RATE_INFO_FLAGS_MCS;
2004                 rinfo->mcs = rate & 0xff;
2005         } else if (rate & STA_STATS_RATE_LEGACY) {
2006                 struct ieee80211_supported_band *sband;
2007                 u16 brate;
2008                 unsigned int shift;
2009
2010                 sband = local->hw.wiphy->bands[(rate >> 4) & 0xf];
2011                 brate = sband->bitrates[rate & 0xf].bitrate;
2012                 if (rinfo->bw == RATE_INFO_BW_5)
2013                         shift = 2;
2014                 else if (rinfo->bw == RATE_INFO_BW_10)
2015                         shift = 1;
2016                 else
2017                         shift = 0;
2018                 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2019         }
2020
2021         if (rate & STA_STATS_RATE_SGI)
2022                 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2023 }
2024
2025 static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2026 {
2027         u16 rate = ACCESS_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2028
2029         if (rate == STA_STATS_RATE_INVALID)
2030                 rinfo->flags = 0;
2031         else
2032                 sta_stats_decode_rate(sta->local, rate, rinfo);
2033 }
2034
2035 static void sta_set_tidstats(struct sta_info *sta,
2036                              struct cfg80211_tid_stats *tidstats,
2037                              int tid)
2038 {
2039         struct ieee80211_local *local = sta->local;
2040
2041         if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2042                 unsigned int start;
2043
2044                 do {
2045                         start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
2046                         tidstats->rx_msdu = sta->rx_stats.msdu[tid];
2047                 } while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
2048
2049                 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2050         }
2051
2052         if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2053                 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2054                 tidstats->tx_msdu = sta->tx_stats.msdu[tid];
2055         }
2056
2057         if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2058             ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2059                 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2060                 tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
2061         }
2062
2063         if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2064             ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2065                 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2066                 tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
2067         }
2068 }
2069
2070 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2071 {
2072         unsigned int start;
2073         u64 value;
2074
2075         do {
2076                 start = u64_stats_fetch_begin(&rxstats->syncp);
2077                 value = rxstats->bytes;
2078         } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2079
2080         return value;
2081 }
2082
2083 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
2084 {
2085         struct ieee80211_sub_if_data *sdata = sta->sdata;
2086         struct ieee80211_local *local = sdata->local;
2087         struct rate_control_ref *ref = NULL;
2088         u32 thr = 0;
2089         int i, ac, cpu;
2090         struct ieee80211_sta_rx_stats *last_rxstats;
2091
2092         last_rxstats = sta_get_last_rx_stats(sta);
2093
2094         if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2095                 ref = local->rate_ctrl;
2096
2097         sinfo->generation = sdata->local->sta_generation;
2098
2099         /* do before driver, so beacon filtering drivers have a
2100          * chance to e.g. just add the number of filtered beacons
2101          * (or just modify the value entirely, of course)
2102          */
2103         if (sdata->vif.type == NL80211_IFTYPE_STATION)
2104                 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2105
2106         drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2107
2108         sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
2109                          BIT(NL80211_STA_INFO_STA_FLAGS) |
2110                          BIT(NL80211_STA_INFO_BSS_PARAM) |
2111                          BIT(NL80211_STA_INFO_CONNECTED_TIME) |
2112                          BIT(NL80211_STA_INFO_RX_DROP_MISC);
2113
2114         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2115                 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2116                 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
2117         }
2118
2119         sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2120         sinfo->inactive_time =
2121                 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2122
2123         if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
2124                                BIT(NL80211_STA_INFO_TX_BYTES)))) {
2125                 sinfo->tx_bytes = 0;
2126                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2127                         sinfo->tx_bytes += sta->tx_stats.bytes[ac];
2128                 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
2129         }
2130
2131         if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
2132                 sinfo->tx_packets = 0;
2133                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2134                         sinfo->tx_packets += sta->tx_stats.packets[ac];
2135                 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
2136         }
2137
2138         if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
2139                                BIT(NL80211_STA_INFO_RX_BYTES)))) {
2140                 sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
2141
2142                 if (sta->pcpu_rx_stats) {
2143                         for_each_possible_cpu(cpu) {
2144                                 struct ieee80211_sta_rx_stats *cpurxs;
2145
2146                                 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2147                                 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2148                         }
2149                 }
2150
2151                 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
2152         }
2153
2154         if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
2155                 sinfo->rx_packets = sta->rx_stats.packets;
2156                 if (sta->pcpu_rx_stats) {
2157                         for_each_possible_cpu(cpu) {
2158                                 struct ieee80211_sta_rx_stats *cpurxs;
2159
2160                                 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2161                                 sinfo->rx_packets += cpurxs->packets;
2162                         }
2163                 }
2164                 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
2165         }
2166
2167         if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
2168                 sinfo->tx_retries = sta->status_stats.retry_count;
2169                 sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
2170         }
2171
2172         if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
2173                 sinfo->tx_failed = sta->status_stats.retry_failed;
2174                 sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
2175         }
2176
2177         sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2178         if (sta->pcpu_rx_stats) {
2179                 for_each_possible_cpu(cpu) {
2180                         struct ieee80211_sta_rx_stats *cpurxs;
2181
2182                         cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2183                         sinfo->rx_dropped_misc += cpurxs->dropped;
2184                 }
2185         }
2186
2187         if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2188             !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2189                 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
2190                                  BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2191                 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2192         }
2193
2194         if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2195             ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2196                 if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
2197                         sinfo->signal = (s8)last_rxstats->last_signal;
2198                         sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
2199                 }
2200
2201                 if (!sta->pcpu_rx_stats &&
2202                     !(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
2203                         sinfo->signal_avg =
2204                                 -ewma_signal_read(&sta->rx_stats_avg.signal);
2205                         sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
2206                 }
2207         }
2208
2209         /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2210          * the sta->rx_stats struct, so the check here is fine with and without
2211          * pcpu statistics
2212          */
2213         if (last_rxstats->chains &&
2214             !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2215                                BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2216                 sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL);
2217                 if (!sta->pcpu_rx_stats)
2218                         sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2219
2220                 sinfo->chains = last_rxstats->chains;
2221
2222                 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2223                         sinfo->chain_signal[i] =
2224                                 last_rxstats->chain_signal_last[i];
2225                         sinfo->chain_signal_avg[i] =
2226                                 -ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
2227                 }
2228         }
2229
2230         if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
2231                 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2232                                      &sinfo->txrate);
2233                 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
2234         }
2235
2236         if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
2237                 sta_set_rate_info_rx(sta, &sinfo->rxrate);
2238                 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
2239         }
2240
2241         sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
2242         for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
2243                 struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
2244
2245                 sta_set_tidstats(sta, tidstats, i);
2246         }
2247
2248         if (ieee80211_vif_is_mesh(&sdata->vif)) {
2249 #ifdef CONFIG_MAC80211_MESH
2250                 sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2251                                  BIT(NL80211_STA_INFO_PLID) |
2252                                  BIT(NL80211_STA_INFO_PLINK_STATE) |
2253                                  BIT(NL80211_STA_INFO_LOCAL_PM) |
2254                                  BIT(NL80211_STA_INFO_PEER_PM) |
2255                                  BIT(NL80211_STA_INFO_NONPEER_PM);
2256
2257                 sinfo->llid = sta->mesh->llid;
2258                 sinfo->plid = sta->mesh->plid;
2259                 sinfo->plink_state = sta->mesh->plink_state;
2260                 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2261                         sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
2262                         sinfo->t_offset = sta->mesh->t_offset;
2263                 }
2264                 sinfo->local_pm = sta->mesh->local_pm;
2265                 sinfo->peer_pm = sta->mesh->peer_pm;
2266                 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2267 #endif
2268         }
2269
2270         sinfo->bss_param.flags = 0;
2271         if (sdata->vif.bss_conf.use_cts_prot)
2272                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2273         if (sdata->vif.bss_conf.use_short_preamble)
2274                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2275         if (sdata->vif.bss_conf.use_short_slot)
2276                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2277         sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2278         sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2279
2280         sinfo->sta_flags.set = 0;
2281         sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2282                                 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2283                                 BIT(NL80211_STA_FLAG_WME) |
2284                                 BIT(NL80211_STA_FLAG_MFP) |
2285                                 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2286                                 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2287                                 BIT(NL80211_STA_FLAG_TDLS_PEER);
2288         if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2289                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2290         if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2291                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2292         if (sta->sta.wme)
2293                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2294         if (test_sta_flag(sta, WLAN_STA_MFP))
2295                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2296         if (test_sta_flag(sta, WLAN_STA_AUTH))
2297                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2298         if (test_sta_flag(sta, WLAN_STA_ASSOC))
2299                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2300         if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2301                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2302
2303         thr = sta_get_expected_throughput(sta);
2304
2305         if (thr != 0) {
2306                 sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2307                 sinfo->expected_throughput = thr;
2308         }
2309 }
2310
2311 u32 sta_get_expected_throughput(struct sta_info *sta)
2312 {
2313         struct ieee80211_sub_if_data *sdata = sta->sdata;
2314         struct ieee80211_local *local = sdata->local;
2315         struct rate_control_ref *ref = NULL;
2316         u32 thr = 0;
2317
2318         if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2319                 ref = local->rate_ctrl;
2320
2321         /* check if the driver has a SW RC implementation */
2322         if (ref && ref->ops->get_expected_throughput)
2323                 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2324         else
2325                 thr = drv_get_expected_throughput(local, sta);
2326
2327         return thr;
2328 }
2329
2330 unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2331 {
2332         struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2333
2334         if (!sta->status_stats.last_ack ||
2335             time_after(stats->last_rx, sta->status_stats.last_ack))
2336                 return stats->last_rx;
2337         return sta->status_stats.last_ack;
2338 }