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