Mention branches and keyring.
[releases.git] / mac80211 / sta_info.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2002-2005, Instant802 Networks, Inc.
4  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
5  * Copyright 2013-2014  Intel Mobile Communications GmbH
6  * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
7  * Copyright (C) 2018-2021 Intel Corporation
8  */
9
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
18 #include <linux/timer.h>
19 #include <linux/rtnetlink.h>
20
21 #include <net/codel.h>
22 #include <net/mac80211.h>
23 #include "ieee80211_i.h"
24 #include "driver-ops.h"
25 #include "rate.h"
26 #include "sta_info.h"
27 #include "debugfs_sta.h"
28 #include "mesh.h"
29 #include "wme.h"
30
31 /**
32  * DOC: STA information lifetime rules
33  *
34  * STA info structures (&struct sta_info) are managed in a hash table
35  * for faster lookup and a list for iteration. They are managed using
36  * RCU, i.e. access to the list and hash table is protected by RCU.
37  *
38  * Upon allocating a STA info structure with sta_info_alloc(), the caller
39  * owns that structure. It must then insert it into the hash table using
40  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
41  * case (which acquires an rcu read section but must not be called from
42  * within one) will the pointer still be valid after the call. Note that
43  * the caller may not do much with the STA info before inserting it, in
44  * particular, it may not start any mesh peer link management or add
45  * encryption keys.
46  *
47  * When the insertion fails (sta_info_insert()) returns non-zero), the
48  * structure will have been freed by sta_info_insert()!
49  *
50  * Station entries are added by mac80211 when you establish a link with a
51  * peer. This means different things for the different type of interfaces
52  * we support. For a regular station this mean we add the AP sta when we
53  * receive an association response from the AP. For IBSS this occurs when
54  * get to know about a peer on the same IBSS. For WDS we add the sta for
55  * the peer immediately upon device open. When using AP mode we add stations
56  * for each respective station upon request from userspace through nl80211.
57  *
58  * In order to remove a STA info structure, various sta_info_destroy_*()
59  * calls are available.
60  *
61  * There is no concept of ownership on a STA entry, each structure is
62  * owned by the global hash table/list until it is removed. All users of
63  * the structure need to be RCU protected so that the structure won't be
64  * freed before they are done using it.
65  */
66
67 struct sta_link_alloc {
68         struct link_sta_info info;
69         struct ieee80211_link_sta sta;
70         struct rcu_head rcu_head;
71 };
72
73 static const struct rhashtable_params sta_rht_params = {
74         .nelem_hint = 3, /* start small */
75         .automatic_shrinking = true,
76         .head_offset = offsetof(struct sta_info, hash_node),
77         .key_offset = offsetof(struct sta_info, addr),
78         .key_len = ETH_ALEN,
79         .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
80 };
81
82 static const struct rhashtable_params link_sta_rht_params = {
83         .nelem_hint = 3, /* start small */
84         .automatic_shrinking = true,
85         .head_offset = offsetof(struct link_sta_info, link_hash_node),
86         .key_offset = offsetof(struct link_sta_info, addr),
87         .key_len = ETH_ALEN,
88         .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
89 };
90
91 /* Caller must hold local->sta_mtx */
92 static int sta_info_hash_del(struct ieee80211_local *local,
93                              struct sta_info *sta)
94 {
95         return rhltable_remove(&local->sta_hash, &sta->hash_node,
96                                sta_rht_params);
97 }
98
99 static int link_sta_info_hash_add(struct ieee80211_local *local,
100                                   struct link_sta_info *link_sta)
101 {
102         lockdep_assert_held(&local->sta_mtx);
103         return rhltable_insert(&local->link_sta_hash,
104                                &link_sta->link_hash_node,
105                                link_sta_rht_params);
106 }
107
108 static int link_sta_info_hash_del(struct ieee80211_local *local,
109                                   struct link_sta_info *link_sta)
110 {
111         lockdep_assert_held(&local->sta_mtx);
112         return rhltable_remove(&local->link_sta_hash,
113                                &link_sta->link_hash_node,
114                                link_sta_rht_params);
115 }
116
117 static void __cleanup_single_sta(struct sta_info *sta)
118 {
119         int ac, i;
120         struct tid_ampdu_tx *tid_tx;
121         struct ieee80211_sub_if_data *sdata = sta->sdata;
122         struct ieee80211_local *local = sdata->local;
123         struct ps_data *ps;
124
125         if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
126             test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
127             test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
128                 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
129                     sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
130                         ps = &sdata->bss->ps;
131                 else if (ieee80211_vif_is_mesh(&sdata->vif))
132                         ps = &sdata->u.mesh.ps;
133                 else
134                         return;
135
136                 clear_sta_flag(sta, WLAN_STA_PS_STA);
137                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
138                 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
139
140                 atomic_dec(&ps->num_sta_ps);
141         }
142
143         if (sta->sta.txq[0]) {
144                 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
145                         struct txq_info *txqi;
146
147                         if (!sta->sta.txq[i])
148                                 continue;
149
150                         txqi = to_txq_info(sta->sta.txq[i]);
151
152                         ieee80211_txq_purge(local, txqi);
153                 }
154         }
155
156         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
157                 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
158                 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
159                 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
160         }
161
162         if (ieee80211_vif_is_mesh(&sdata->vif))
163                 mesh_sta_cleanup(sta);
164
165         cancel_work_sync(&sta->drv_deliver_wk);
166
167         /*
168          * Destroy aggregation state here. It would be nice to wait for the
169          * driver to finish aggregation stop and then clean up, but for now
170          * drivers have to handle aggregation stop being requested, followed
171          * directly by station destruction.
172          */
173         for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
174                 kfree(sta->ampdu_mlme.tid_start_tx[i]);
175                 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
176                 if (!tid_tx)
177                         continue;
178                 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
179                 kfree(tid_tx);
180         }
181 }
182
183 static void cleanup_single_sta(struct sta_info *sta)
184 {
185         struct ieee80211_sub_if_data *sdata = sta->sdata;
186         struct ieee80211_local *local = sdata->local;
187
188         __cleanup_single_sta(sta);
189         sta_info_free(local, sta);
190 }
191
192 struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
193                                          const u8 *addr)
194 {
195         return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
196 }
197
198 /* protected by RCU */
199 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
200                               const u8 *addr)
201 {
202         struct ieee80211_local *local = sdata->local;
203         struct rhlist_head *tmp;
204         struct sta_info *sta;
205
206         rcu_read_lock();
207         for_each_sta_info(local, addr, sta, tmp) {
208                 if (sta->sdata == sdata) {
209                         rcu_read_unlock();
210                         /* this is safe as the caller must already hold
211                          * another rcu read section or the mutex
212                          */
213                         return sta;
214                 }
215         }
216         rcu_read_unlock();
217         return NULL;
218 }
219
220 /*
221  * Get sta info either from the specified interface
222  * or from one of its vlans
223  */
224 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
225                                   const u8 *addr)
226 {
227         struct ieee80211_local *local = sdata->local;
228         struct rhlist_head *tmp;
229         struct sta_info *sta;
230
231         rcu_read_lock();
232         for_each_sta_info(local, addr, sta, tmp) {
233                 if (sta->sdata == sdata ||
234                     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
235                         rcu_read_unlock();
236                         /* this is safe as the caller must already hold
237                          * another rcu read section or the mutex
238                          */
239                         return sta;
240                 }
241         }
242         rcu_read_unlock();
243         return NULL;
244 }
245
246 struct rhlist_head *link_sta_info_hash_lookup(struct ieee80211_local *local,
247                                               const u8 *addr)
248 {
249         return rhltable_lookup(&local->link_sta_hash, addr,
250                                link_sta_rht_params);
251 }
252
253 struct link_sta_info *
254 link_sta_info_get_bss(struct ieee80211_sub_if_data *sdata, const u8 *addr)
255 {
256         struct ieee80211_local *local = sdata->local;
257         struct rhlist_head *tmp;
258         struct link_sta_info *link_sta;
259
260         rcu_read_lock();
261         for_each_link_sta_info(local, addr, link_sta, tmp) {
262                 struct sta_info *sta = link_sta->sta;
263
264                 if (sta->sdata == sdata ||
265                     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
266                         rcu_read_unlock();
267                         /* this is safe as the caller must already hold
268                          * another rcu read section or the mutex
269                          */
270                         return link_sta;
271                 }
272         }
273         rcu_read_unlock();
274         return NULL;
275 }
276
277 struct ieee80211_sta *
278 ieee80211_find_sta_by_link_addrs(struct ieee80211_hw *hw,
279                                  const u8 *addr,
280                                  const u8 *localaddr,
281                                  unsigned int *link_id)
282 {
283         struct ieee80211_local *local = hw_to_local(hw);
284         struct link_sta_info *link_sta;
285         struct rhlist_head *tmp;
286
287         for_each_link_sta_info(local, addr, link_sta, tmp) {
288                 struct sta_info *sta = link_sta->sta;
289                 struct ieee80211_link_data *link;
290                 u8 _link_id = link_sta->link_id;
291
292                 if (!localaddr) {
293                         if (link_id)
294                                 *link_id = _link_id;
295                         return &sta->sta;
296                 }
297
298                 link = rcu_dereference(sta->sdata->link[_link_id]);
299                 if (!link)
300                         continue;
301
302                 if (memcmp(link->conf->addr, localaddr, ETH_ALEN))
303                         continue;
304
305                 if (link_id)
306                         *link_id = _link_id;
307                 return &sta->sta;
308         }
309
310         return NULL;
311 }
312 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_link_addrs);
313
314 struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
315                                        const u8 *sta_addr, const u8 *vif_addr)
316 {
317         struct rhlist_head *tmp;
318         struct sta_info *sta;
319
320         for_each_sta_info(local, sta_addr, sta, tmp) {
321                 if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
322                         return sta;
323         }
324
325         return NULL;
326 }
327
328 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
329                                      int idx)
330 {
331         struct ieee80211_local *local = sdata->local;
332         struct sta_info *sta;
333         int i = 0;
334
335         list_for_each_entry_rcu(sta, &local->sta_list, list,
336                                 lockdep_is_held(&local->sta_mtx)) {
337                 if (sdata != sta->sdata)
338                         continue;
339                 if (i < idx) {
340                         ++i;
341                         continue;
342                 }
343                 return sta;
344         }
345
346         return NULL;
347 }
348
349 static void sta_info_free_link(struct link_sta_info *link_sta)
350 {
351         free_percpu(link_sta->pcpu_rx_stats);
352 }
353
354 static void sta_remove_link(struct sta_info *sta, unsigned int link_id,
355                             bool unhash)
356 {
357         struct sta_link_alloc *alloc = NULL;
358         struct link_sta_info *link_sta;
359
360         link_sta = rcu_dereference_protected(sta->link[link_id],
361                                              lockdep_is_held(&sta->local->sta_mtx));
362
363         if (WARN_ON(!link_sta))
364                 return;
365
366         if (unhash)
367                 link_sta_info_hash_del(sta->local, link_sta);
368
369         if (link_sta != &sta->deflink)
370                 alloc = container_of(link_sta, typeof(*alloc), info);
371
372         sta->sta.valid_links &= ~BIT(link_id);
373         RCU_INIT_POINTER(sta->link[link_id], NULL);
374         RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
375         if (alloc) {
376                 sta_info_free_link(&alloc->info);
377                 kfree_rcu(alloc, rcu_head);
378         }
379
380         ieee80211_sta_recalc_aggregates(&sta->sta);
381 }
382
383 /**
384  * sta_info_free - free STA
385  *
386  * @local: pointer to the global information
387  * @sta: STA info to free
388  *
389  * This function must undo everything done by sta_info_alloc()
390  * that may happen before sta_info_insert(). It may only be
391  * called when sta_info_insert() has not been attempted (and
392  * if that fails, the station is freed anyway.)
393  */
394 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
395 {
396         int i;
397
398         for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
399                 struct link_sta_info *link_sta;
400
401                 link_sta = rcu_access_pointer(sta->link[i]);
402                 if (!link_sta)
403                         continue;
404
405                 sta_remove_link(sta, i, false);
406         }
407
408         /*
409          * If we had used sta_info_pre_move_state() then we might not
410          * have gone through the state transitions down again, so do
411          * it here now (and warn if it's inserted).
412          *
413          * This will clear state such as fast TX/RX that may have been
414          * allocated during state transitions.
415          */
416         while (sta->sta_state > IEEE80211_STA_NONE) {
417                 int ret;
418
419                 WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
420
421                 ret = sta_info_move_state(sta, sta->sta_state - 1);
422                 if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
423                         break;
424         }
425
426         if (sta->rate_ctrl)
427                 rate_control_free_sta(sta);
428
429         sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
430
431         if (sta->sta.txq[0])
432                 kfree(to_txq_info(sta->sta.txq[0]));
433         kfree(rcu_dereference_raw(sta->sta.rates));
434 #ifdef CONFIG_MAC80211_MESH
435         kfree(sta->mesh);
436 #endif
437
438         sta_info_free_link(&sta->deflink);
439         kfree(sta);
440 }
441
442 /* Caller must hold local->sta_mtx */
443 static int sta_info_hash_add(struct ieee80211_local *local,
444                              struct sta_info *sta)
445 {
446         return rhltable_insert(&local->sta_hash, &sta->hash_node,
447                                sta_rht_params);
448 }
449
450 static void sta_deliver_ps_frames(struct work_struct *wk)
451 {
452         struct sta_info *sta;
453
454         sta = container_of(wk, struct sta_info, drv_deliver_wk);
455
456         if (sta->dead)
457                 return;
458
459         local_bh_disable();
460         if (!test_sta_flag(sta, WLAN_STA_PS_STA))
461                 ieee80211_sta_ps_deliver_wakeup(sta);
462         else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
463                 ieee80211_sta_ps_deliver_poll_response(sta);
464         else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
465                 ieee80211_sta_ps_deliver_uapsd(sta);
466         local_bh_enable();
467 }
468
469 static int sta_prepare_rate_control(struct ieee80211_local *local,
470                                     struct sta_info *sta, gfp_t gfp)
471 {
472         if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
473                 return 0;
474
475         sta->rate_ctrl = local->rate_ctrl;
476         sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
477                                                      sta, gfp);
478         if (!sta->rate_ctrl_priv)
479                 return -ENOMEM;
480
481         return 0;
482 }
483
484 static int sta_info_alloc_link(struct ieee80211_local *local,
485                                struct link_sta_info *link_info,
486                                gfp_t gfp)
487 {
488         struct ieee80211_hw *hw = &local->hw;
489         int i;
490
491         if (ieee80211_hw_check(hw, USES_RSS)) {
492                 link_info->pcpu_rx_stats =
493                         alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
494                 if (!link_info->pcpu_rx_stats)
495                         return -ENOMEM;
496         }
497
498         link_info->rx_stats.last_rx = jiffies;
499         u64_stats_init(&link_info->rx_stats.syncp);
500
501         ewma_signal_init(&link_info->rx_stats_avg.signal);
502         ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
503         for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
504                 ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
505
506         return 0;
507 }
508
509 static void sta_info_add_link(struct sta_info *sta,
510                               unsigned int link_id,
511                               struct link_sta_info *link_info,
512                               struct ieee80211_link_sta *link_sta)
513 {
514         link_info->sta = sta;
515         link_info->link_id = link_id;
516         link_info->pub = link_sta;
517         link_sta->link_id = link_id;
518         rcu_assign_pointer(sta->link[link_id], link_info);
519         rcu_assign_pointer(sta->sta.link[link_id], link_sta);
520
521         link_sta->smps_mode = IEEE80211_SMPS_OFF;
522         link_sta->agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
523 }
524
525 static struct sta_info *
526 __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
527                  const u8 *addr, int link_id, const u8 *link_addr,
528                  gfp_t gfp)
529 {
530         struct ieee80211_local *local = sdata->local;
531         struct ieee80211_hw *hw = &local->hw;
532         struct sta_info *sta;
533         int i;
534
535         sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
536         if (!sta)
537                 return NULL;
538
539         sta->local = local;
540         sta->sdata = sdata;
541
542         if (sta_info_alloc_link(local, &sta->deflink, gfp))
543                 goto free;
544
545         if (link_id >= 0) {
546                 sta_info_add_link(sta, link_id, &sta->deflink,
547                                   &sta->sta.deflink);
548                 sta->sta.valid_links = BIT(link_id);
549         } else {
550                 sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
551         }
552
553         sta->sta.cur = &sta->sta.deflink.agg;
554
555         spin_lock_init(&sta->lock);
556         spin_lock_init(&sta->ps_lock);
557         INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
558         INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
559         mutex_init(&sta->ampdu_mlme.mtx);
560 #ifdef CONFIG_MAC80211_MESH
561         if (ieee80211_vif_is_mesh(&sdata->vif)) {
562                 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
563                 if (!sta->mesh)
564                         goto free;
565                 sta->mesh->plink_sta = sta;
566                 spin_lock_init(&sta->mesh->plink_lock);
567                 if (!sdata->u.mesh.user_mpm)
568                         timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
569                                     0);
570                 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
571         }
572 #endif
573
574         memcpy(sta->addr, addr, ETH_ALEN);
575         memcpy(sta->sta.addr, addr, ETH_ALEN);
576         memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
577         memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
578         sta->sta.max_rx_aggregation_subframes =
579                 local->hw.max_rx_aggregation_subframes;
580
581         /* TODO link specific alloc and assignments for MLO Link STA */
582
583         /* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
584          * The Tx path starts to use a key as soon as the key slot ptk_idx
585          * references to is not NULL. To not use the initial Rx-only key
586          * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
587          * which always will refer to a NULL key.
588          */
589         BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
590         sta->ptk_idx = INVALID_PTK_KEYIDX;
591
592
593         ieee80211_init_frag_cache(&sta->frags);
594
595         sta->sta_state = IEEE80211_STA_NONE;
596
597         /* Mark TID as unreserved */
598         sta->reserved_tid = IEEE80211_TID_UNRESERVED;
599
600         sta->last_connected = ktime_get_seconds();
601
602         if (local->ops->wake_tx_queue) {
603                 void *txq_data;
604                 int size = sizeof(struct txq_info) +
605                            ALIGN(hw->txq_data_size, sizeof(void *));
606
607                 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
608                 if (!txq_data)
609                         goto free;
610
611                 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
612                         struct txq_info *txq = txq_data + i * size;
613
614                         /* might not do anything for the bufferable MMPDU TXQ */
615                         ieee80211_txq_init(sdata, sta, txq, i);
616                 }
617         }
618
619         if (sta_prepare_rate_control(local, sta, gfp))
620                 goto free_txq;
621
622         sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
623
624         for (i = 0; i < IEEE80211_NUM_ACS; i++) {
625                 skb_queue_head_init(&sta->ps_tx_buf[i]);
626                 skb_queue_head_init(&sta->tx_filtered[i]);
627                 sta->airtime[i].deficit = sta->airtime_weight;
628                 atomic_set(&sta->airtime[i].aql_tx_pending, 0);
629                 sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
630                 sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
631         }
632
633         for (i = 0; i < IEEE80211_NUM_TIDS; i++)
634                 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
635
636         for (i = 0; i < NUM_NL80211_BANDS; i++) {
637                 u32 mandatory = 0;
638                 int r;
639
640                 if (!hw->wiphy->bands[i])
641                         continue;
642
643                 switch (i) {
644                 case NL80211_BAND_2GHZ:
645                 case NL80211_BAND_LC:
646                         /*
647                          * We use both here, even if we cannot really know for
648                          * sure the station will support both, but the only use
649                          * for this is when we don't know anything yet and send
650                          * management frames, and then we'll pick the lowest
651                          * possible rate anyway.
652                          * If we don't include _G here, we cannot find a rate
653                          * in P2P, and thus trigger the WARN_ONCE() in rate.c
654                          */
655                         mandatory = IEEE80211_RATE_MANDATORY_B |
656                                     IEEE80211_RATE_MANDATORY_G;
657                         break;
658                 case NL80211_BAND_5GHZ:
659                         mandatory = IEEE80211_RATE_MANDATORY_A;
660                         break;
661                 case NL80211_BAND_60GHZ:
662                         WARN_ON(1);
663                         mandatory = 0;
664                         break;
665                 }
666
667                 for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
668                         struct ieee80211_rate *rate;
669
670                         rate = &hw->wiphy->bands[i]->bitrates[r];
671
672                         if (!(rate->flags & mandatory))
673                                 continue;
674                         sta->sta.deflink.supp_rates[i] |= BIT(r);
675                 }
676         }
677
678         sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
679         sta->cparams.target = MS2TIME(20);
680         sta->cparams.interval = MS2TIME(100);
681         sta->cparams.ecn = true;
682         sta->cparams.ce_threshold_selector = 0;
683         sta->cparams.ce_threshold_mask = 0;
684
685         sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
686
687         return sta;
688
689 free_txq:
690         if (sta->sta.txq[0])
691                 kfree(to_txq_info(sta->sta.txq[0]));
692 free:
693         sta_info_free_link(&sta->deflink);
694 #ifdef CONFIG_MAC80211_MESH
695         kfree(sta->mesh);
696 #endif
697         kfree(sta);
698         return NULL;
699 }
700
701 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
702                                 const u8 *addr, gfp_t gfp)
703 {
704         return __sta_info_alloc(sdata, addr, -1, addr, gfp);
705 }
706
707 struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
708                                           const u8 *mld_addr,
709                                           unsigned int link_id,
710                                           const u8 *link_addr,
711                                           gfp_t gfp)
712 {
713         return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
714 }
715
716 static int sta_info_insert_check(struct sta_info *sta)
717 {
718         struct ieee80211_sub_if_data *sdata = sta->sdata;
719
720         /*
721          * Can't be a WARN_ON because it can be triggered through a race:
722          * something inserts a STA (on one CPU) without holding the RTNL
723          * and another CPU turns off the net device.
724          */
725         if (unlikely(!ieee80211_sdata_running(sdata)))
726                 return -ENETDOWN;
727
728         if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
729                     !is_valid_ether_addr(sta->sta.addr)))
730                 return -EINVAL;
731
732         /* The RCU read lock is required by rhashtable due to
733          * asynchronous resize/rehash.  We also require the mutex
734          * for correctness.
735          */
736         rcu_read_lock();
737         lockdep_assert_held(&sdata->local->sta_mtx);
738         if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
739             ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
740                 rcu_read_unlock();
741                 return -ENOTUNIQ;
742         }
743         rcu_read_unlock();
744
745         return 0;
746 }
747
748 static int sta_info_insert_drv_state(struct ieee80211_local *local,
749                                      struct ieee80211_sub_if_data *sdata,
750                                      struct sta_info *sta)
751 {
752         enum ieee80211_sta_state state;
753         int err = 0;
754
755         for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
756                 err = drv_sta_state(local, sdata, sta, state, state + 1);
757                 if (err)
758                         break;
759         }
760
761         if (!err) {
762                 /*
763                  * Drivers using legacy sta_add/sta_remove callbacks only
764                  * get uploaded set to true after sta_add is called.
765                  */
766                 if (!local->ops->sta_add)
767                         sta->uploaded = true;
768                 return 0;
769         }
770
771         if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
772                 sdata_info(sdata,
773                            "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
774                            sta->sta.addr, state + 1, err);
775                 err = 0;
776         }
777
778         /* unwind on error */
779         for (; state > IEEE80211_STA_NOTEXIST; state--)
780                 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
781
782         return err;
783 }
784
785 static void
786 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
787 {
788         struct ieee80211_local *local = sdata->local;
789         bool allow_p2p_go_ps = sdata->vif.p2p;
790         struct sta_info *sta;
791
792         rcu_read_lock();
793         list_for_each_entry_rcu(sta, &local->sta_list, list) {
794                 if (sdata != sta->sdata ||
795                     !test_sta_flag(sta, WLAN_STA_ASSOC))
796                         continue;
797                 if (!sta->sta.support_p2p_ps) {
798                         allow_p2p_go_ps = false;
799                         break;
800                 }
801         }
802         rcu_read_unlock();
803
804         if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
805                 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
806                 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
807                                                   BSS_CHANGED_P2P_PS);
808         }
809 }
810
811 /*
812  * should be called with sta_mtx locked
813  * this function replaces the mutex lock
814  * with a RCU lock
815  */
816 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
817 {
818         struct ieee80211_local *local = sta->local;
819         struct ieee80211_sub_if_data *sdata = sta->sdata;
820         struct station_info *sinfo = NULL;
821         int err = 0;
822
823         lockdep_assert_held(&local->sta_mtx);
824
825         /* check if STA exists already */
826         if (sta_info_get_bss(sdata, sta->sta.addr)) {
827                 err = -EEXIST;
828                 goto out_cleanup;
829         }
830
831         sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
832         if (!sinfo) {
833                 err = -ENOMEM;
834                 goto out_cleanup;
835         }
836
837         local->num_sta++;
838         local->sta_generation++;
839         smp_mb();
840
841         /* simplify things and don't accept BA sessions yet */
842         set_sta_flag(sta, WLAN_STA_BLOCK_BA);
843
844         /* make the station visible */
845         err = sta_info_hash_add(local, sta);
846         if (err)
847                 goto out_drop_sta;
848
849         if (sta->sta.valid_links) {
850                 err = link_sta_info_hash_add(local, &sta->deflink);
851                 if (err) {
852                         sta_info_hash_del(local, sta);
853                         goto out_drop_sta;
854                 }
855         }
856
857         list_add_tail_rcu(&sta->list, &local->sta_list);
858
859         /* update channel context before notifying the driver about state
860          * change, this enables driver using the updated channel context right away.
861          */
862         if (sta->sta_state >= IEEE80211_STA_ASSOC) {
863                 ieee80211_recalc_min_chandef(sta->sdata, -1);
864                 if (!sta->sta.support_p2p_ps)
865                         ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
866         }
867
868         /* notify driver */
869         err = sta_info_insert_drv_state(local, sdata, sta);
870         if (err)
871                 goto out_remove;
872
873         set_sta_flag(sta, WLAN_STA_INSERTED);
874
875         /* accept BA sessions now */
876         clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
877
878         ieee80211_sta_debugfs_add(sta);
879         rate_control_add_sta_debugfs(sta);
880
881         sinfo->generation = local->sta_generation;
882         cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
883         kfree(sinfo);
884
885         sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
886
887         /* move reference to rcu-protected */
888         rcu_read_lock();
889         mutex_unlock(&local->sta_mtx);
890
891         if (ieee80211_vif_is_mesh(&sdata->vif))
892                 mesh_accept_plinks_update(sdata);
893
894         ieee80211_check_fast_xmit(sta);
895
896         return 0;
897  out_remove:
898         if (sta->sta.valid_links)
899                 link_sta_info_hash_del(local, &sta->deflink);
900         sta_info_hash_del(local, sta);
901         list_del_rcu(&sta->list);
902  out_drop_sta:
903         local->num_sta--;
904         synchronize_net();
905  out_cleanup:
906         cleanup_single_sta(sta);
907         mutex_unlock(&local->sta_mtx);
908         kfree(sinfo);
909         rcu_read_lock();
910         return err;
911 }
912
913 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
914 {
915         struct ieee80211_local *local = sta->local;
916         int err;
917
918         might_sleep();
919
920         mutex_lock(&local->sta_mtx);
921
922         err = sta_info_insert_check(sta);
923         if (err) {
924                 sta_info_free(local, sta);
925                 mutex_unlock(&local->sta_mtx);
926                 rcu_read_lock();
927                 return err;
928         }
929
930         return sta_info_insert_finish(sta);
931 }
932
933 int sta_info_insert(struct sta_info *sta)
934 {
935         int err = sta_info_insert_rcu(sta);
936
937         rcu_read_unlock();
938
939         return err;
940 }
941
942 static inline void __bss_tim_set(u8 *tim, u16 id)
943 {
944         /*
945          * This format has been mandated by the IEEE specifications,
946          * so this line may not be changed to use the __set_bit() format.
947          */
948         tim[id / 8] |= (1 << (id % 8));
949 }
950
951 static inline void __bss_tim_clear(u8 *tim, u16 id)
952 {
953         /*
954          * This format has been mandated by the IEEE specifications,
955          * so this line may not be changed to use the __clear_bit() format.
956          */
957         tim[id / 8] &= ~(1 << (id % 8));
958 }
959
960 static inline bool __bss_tim_get(u8 *tim, u16 id)
961 {
962         /*
963          * This format has been mandated by the IEEE specifications,
964          * so this line may not be changed to use the test_bit() format.
965          */
966         return tim[id / 8] & (1 << (id % 8));
967 }
968
969 static unsigned long ieee80211_tids_for_ac(int ac)
970 {
971         /* If we ever support TIDs > 7, this obviously needs to be adjusted */
972         switch (ac) {
973         case IEEE80211_AC_VO:
974                 return BIT(6) | BIT(7);
975         case IEEE80211_AC_VI:
976                 return BIT(4) | BIT(5);
977         case IEEE80211_AC_BE:
978                 return BIT(0) | BIT(3);
979         case IEEE80211_AC_BK:
980                 return BIT(1) | BIT(2);
981         default:
982                 WARN_ON(1);
983                 return 0;
984         }
985 }
986
987 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
988 {
989         struct ieee80211_local *local = sta->local;
990         struct ps_data *ps;
991         bool indicate_tim = false;
992         u8 ignore_for_tim = sta->sta.uapsd_queues;
993         int ac;
994         u16 id = sta->sta.aid;
995
996         if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
997             sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
998                 if (WARN_ON_ONCE(!sta->sdata->bss))
999                         return;
1000
1001                 ps = &sta->sdata->bss->ps;
1002 #ifdef CONFIG_MAC80211_MESH
1003         } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
1004                 ps = &sta->sdata->u.mesh.ps;
1005 #endif
1006         } else {
1007                 return;
1008         }
1009
1010         /* No need to do anything if the driver does all */
1011         if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
1012                 return;
1013
1014         if (sta->dead)
1015                 goto done;
1016
1017         /*
1018          * If all ACs are delivery-enabled then we should build
1019          * the TIM bit for all ACs anyway; if only some are then
1020          * we ignore those and build the TIM bit using only the
1021          * non-enabled ones.
1022          */
1023         if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
1024                 ignore_for_tim = 0;
1025
1026         if (ignore_pending)
1027                 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
1028
1029         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1030                 unsigned long tids;
1031
1032                 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
1033                         continue;
1034
1035                 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
1036                                 !skb_queue_empty(&sta->ps_tx_buf[ac]);
1037                 if (indicate_tim)
1038                         break;
1039
1040                 tids = ieee80211_tids_for_ac(ac);
1041
1042                 indicate_tim |=
1043                         sta->driver_buffered_tids & tids;
1044                 indicate_tim |=
1045                         sta->txq_buffered_tids & tids;
1046         }
1047
1048  done:
1049         spin_lock_bh(&local->tim_lock);
1050
1051         if (indicate_tim == __bss_tim_get(ps->tim, id))
1052                 goto out_unlock;
1053
1054         if (indicate_tim)
1055                 __bss_tim_set(ps->tim, id);
1056         else
1057                 __bss_tim_clear(ps->tim, id);
1058
1059         if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1060                 local->tim_in_locked_section = true;
1061                 drv_set_tim(local, &sta->sta, indicate_tim);
1062                 local->tim_in_locked_section = false;
1063         }
1064
1065 out_unlock:
1066         spin_unlock_bh(&local->tim_lock);
1067 }
1068
1069 void sta_info_recalc_tim(struct sta_info *sta)
1070 {
1071         __sta_info_recalc_tim(sta, false);
1072 }
1073
1074 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1075 {
1076         struct ieee80211_tx_info *info;
1077         int timeout;
1078
1079         if (!skb)
1080                 return false;
1081
1082         info = IEEE80211_SKB_CB(skb);
1083
1084         /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
1085         timeout = (sta->listen_interval *
1086                    sta->sdata->vif.bss_conf.beacon_int *
1087                    32 / 15625) * HZ;
1088         if (timeout < STA_TX_BUFFER_EXPIRE)
1089                 timeout = STA_TX_BUFFER_EXPIRE;
1090         return time_after(jiffies, info->control.jiffies + timeout);
1091 }
1092
1093
1094 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1095                                                 struct sta_info *sta, int ac)
1096 {
1097         unsigned long flags;
1098         struct sk_buff *skb;
1099
1100         /*
1101          * First check for frames that should expire on the filtered
1102          * queue. Frames here were rejected by the driver and are on
1103          * a separate queue to avoid reordering with normal PS-buffered
1104          * frames. They also aren't accounted for right now in the
1105          * total_ps_buffered counter.
1106          */
1107         for (;;) {
1108                 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1109                 skb = skb_peek(&sta->tx_filtered[ac]);
1110                 if (sta_info_buffer_expired(sta, skb))
1111                         skb = __skb_dequeue(&sta->tx_filtered[ac]);
1112                 else
1113                         skb = NULL;
1114                 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1115
1116                 /*
1117                  * Frames are queued in order, so if this one
1118                  * hasn't expired yet we can stop testing. If
1119                  * we actually reached the end of the queue we
1120                  * also need to stop, of course.
1121                  */
1122                 if (!skb)
1123                         break;
1124                 ieee80211_free_txskb(&local->hw, skb);
1125         }
1126
1127         /*
1128          * Now also check the normal PS-buffered queue, this will
1129          * only find something if the filtered queue was emptied
1130          * since the filtered frames are all before the normal PS
1131          * buffered frames.
1132          */
1133         for (;;) {
1134                 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1135                 skb = skb_peek(&sta->ps_tx_buf[ac]);
1136                 if (sta_info_buffer_expired(sta, skb))
1137                         skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1138                 else
1139                         skb = NULL;
1140                 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1141
1142                 /*
1143                  * frames are queued in order, so if this one
1144                  * hasn't expired yet (or we reached the end of
1145                  * the queue) we can stop testing
1146                  */
1147                 if (!skb)
1148                         break;
1149
1150                 local->total_ps_buffered--;
1151                 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1152                        sta->sta.addr);
1153                 ieee80211_free_txskb(&local->hw, skb);
1154         }
1155
1156         /*
1157          * Finally, recalculate the TIM bit for this station -- it might
1158          * now be clear because the station was too slow to retrieve its
1159          * frames.
1160          */
1161         sta_info_recalc_tim(sta);
1162
1163         /*
1164          * Return whether there are any frames still buffered, this is
1165          * used to check whether the cleanup timer still needs to run,
1166          * if there are no frames we don't need to rearm the timer.
1167          */
1168         return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1169                  skb_queue_empty(&sta->tx_filtered[ac]));
1170 }
1171
1172 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1173                                              struct sta_info *sta)
1174 {
1175         bool have_buffered = false;
1176         int ac;
1177
1178         /* This is only necessary for stations on BSS/MBSS interfaces */
1179         if (!sta->sdata->bss &&
1180             !ieee80211_vif_is_mesh(&sta->sdata->vif))
1181                 return false;
1182
1183         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1184                 have_buffered |=
1185                         sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1186
1187         return have_buffered;
1188 }
1189
1190 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
1191 {
1192         struct ieee80211_local *local;
1193         struct ieee80211_sub_if_data *sdata;
1194         int ret, i;
1195
1196         might_sleep();
1197
1198         if (!sta)
1199                 return -ENOENT;
1200
1201         local = sta->local;
1202         sdata = sta->sdata;
1203
1204         lockdep_assert_held(&local->sta_mtx);
1205
1206         /*
1207          * Before removing the station from the driver and
1208          * rate control, it might still start new aggregation
1209          * sessions -- block that to make sure the tear-down
1210          * will be sufficient.
1211          */
1212         set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1213         ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1214
1215         /*
1216          * Before removing the station from the driver there might be pending
1217          * rx frames on RSS queues sent prior to the disassociation - wait for
1218          * all such frames to be processed.
1219          */
1220         drv_sync_rx_queues(local, sta);
1221
1222         for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
1223                 struct link_sta_info *link_sta;
1224
1225                 if (!(sta->sta.valid_links & BIT(i)))
1226                         continue;
1227
1228                 link_sta = rcu_dereference_protected(sta->link[i],
1229                                                      lockdep_is_held(&local->sta_mtx));
1230
1231                 link_sta_info_hash_del(local, link_sta);
1232         }
1233
1234         ret = sta_info_hash_del(local, sta);
1235         if (WARN_ON(ret))
1236                 return ret;
1237
1238         /*
1239          * for TDLS peers, make sure to return to the base channel before
1240          * removal.
1241          */
1242         if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1243                 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1244                 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1245         }
1246
1247         list_del_rcu(&sta->list);
1248         sta->removed = true;
1249
1250         if (sta->uploaded)
1251                 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
1252
1253         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1254             rcu_access_pointer(sdata->u.vlan.sta) == sta)
1255                 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1256
1257         return 0;
1258 }
1259
1260 static void __sta_info_destroy_part2(struct sta_info *sta)
1261 {
1262         struct ieee80211_local *local = sta->local;
1263         struct ieee80211_sub_if_data *sdata = sta->sdata;
1264         struct station_info *sinfo;
1265         int ret;
1266
1267         /*
1268          * NOTE: This assumes at least synchronize_net() was done
1269          *       after _part1 and before _part2!
1270          */
1271
1272         might_sleep();
1273         lockdep_assert_held(&local->sta_mtx);
1274
1275         if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1276                 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1277                 WARN_ON_ONCE(ret);
1278         }
1279
1280         /* now keys can no longer be reached */
1281         ieee80211_free_sta_keys(local, sta);
1282
1283         /* disable TIM bit - last chance to tell driver */
1284         __sta_info_recalc_tim(sta, true);
1285
1286         sta->dead = true;
1287
1288         local->num_sta--;
1289         local->sta_generation++;
1290
1291         while (sta->sta_state > IEEE80211_STA_NONE) {
1292                 ret = sta_info_move_state(sta, sta->sta_state - 1);
1293                 if (ret) {
1294                         WARN_ON_ONCE(1);
1295                         break;
1296                 }
1297         }
1298
1299         if (sta->uploaded) {
1300                 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1301                                     IEEE80211_STA_NOTEXIST);
1302                 WARN_ON_ONCE(ret != 0);
1303         }
1304
1305         sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1306
1307         sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1308         if (sinfo)
1309                 sta_set_sinfo(sta, sinfo, true);
1310         cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1311         kfree(sinfo);
1312
1313         ieee80211_sta_debugfs_remove(sta);
1314
1315         ieee80211_destroy_frag_cache(&sta->frags);
1316
1317         cleanup_single_sta(sta);
1318 }
1319
1320 int __must_check __sta_info_destroy(struct sta_info *sta)
1321 {
1322         int err = __sta_info_destroy_part1(sta);
1323
1324         if (err)
1325                 return err;
1326
1327         synchronize_net();
1328
1329         __sta_info_destroy_part2(sta);
1330
1331         return 0;
1332 }
1333
1334 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1335 {
1336         struct sta_info *sta;
1337         int ret;
1338
1339         mutex_lock(&sdata->local->sta_mtx);
1340         sta = sta_info_get(sdata, addr);
1341         ret = __sta_info_destroy(sta);
1342         mutex_unlock(&sdata->local->sta_mtx);
1343
1344         return ret;
1345 }
1346
1347 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1348                               const u8 *addr)
1349 {
1350         struct sta_info *sta;
1351         int ret;
1352
1353         mutex_lock(&sdata->local->sta_mtx);
1354         sta = sta_info_get_bss(sdata, addr);
1355         ret = __sta_info_destroy(sta);
1356         mutex_unlock(&sdata->local->sta_mtx);
1357
1358         return ret;
1359 }
1360
1361 static void sta_info_cleanup(struct timer_list *t)
1362 {
1363         struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1364         struct sta_info *sta;
1365         bool timer_needed = false;
1366
1367         rcu_read_lock();
1368         list_for_each_entry_rcu(sta, &local->sta_list, list)
1369                 if (sta_info_cleanup_expire_buffered(local, sta))
1370                         timer_needed = true;
1371         rcu_read_unlock();
1372
1373         if (local->quiescing)
1374                 return;
1375
1376         if (!timer_needed)
1377                 return;
1378
1379         mod_timer(&local->sta_cleanup,
1380                   round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1381 }
1382
1383 int sta_info_init(struct ieee80211_local *local)
1384 {
1385         int err;
1386
1387         err = rhltable_init(&local->sta_hash, &sta_rht_params);
1388         if (err)
1389                 return err;
1390
1391         err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1392         if (err) {
1393                 rhltable_destroy(&local->sta_hash);
1394                 return err;
1395         }
1396
1397         spin_lock_init(&local->tim_lock);
1398         mutex_init(&local->sta_mtx);
1399         INIT_LIST_HEAD(&local->sta_list);
1400
1401         timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
1402         return 0;
1403 }
1404
1405 void sta_info_stop(struct ieee80211_local *local)
1406 {
1407         del_timer_sync(&local->sta_cleanup);
1408         rhltable_destroy(&local->sta_hash);
1409         rhltable_destroy(&local->link_sta_hash);
1410 }
1411
1412
1413 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1414 {
1415         struct ieee80211_local *local = sdata->local;
1416         struct sta_info *sta, *tmp;
1417         LIST_HEAD(free_list);
1418         int ret = 0;
1419
1420         might_sleep();
1421
1422         WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1423         WARN_ON(vlans && !sdata->bss);
1424
1425         mutex_lock(&local->sta_mtx);
1426         list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1427                 if (sdata == sta->sdata ||
1428                     (vlans && sdata->bss == sta->sdata->bss)) {
1429                         if (!WARN_ON(__sta_info_destroy_part1(sta)))
1430                                 list_add(&sta->free_list, &free_list);
1431                         ret++;
1432                 }
1433         }
1434
1435         if (!list_empty(&free_list)) {
1436                 synchronize_net();
1437                 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1438                         __sta_info_destroy_part2(sta);
1439         }
1440         mutex_unlock(&local->sta_mtx);
1441
1442         return ret;
1443 }
1444
1445 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1446                           unsigned long exp_time)
1447 {
1448         struct ieee80211_local *local = sdata->local;
1449         struct sta_info *sta, *tmp;
1450
1451         mutex_lock(&local->sta_mtx);
1452
1453         list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1454                 unsigned long last_active = ieee80211_sta_last_active(sta);
1455
1456                 if (sdata != sta->sdata)
1457                         continue;
1458
1459                 if (time_is_before_jiffies(last_active + exp_time)) {
1460                         sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1461                                 sta->sta.addr);
1462
1463                         if (ieee80211_vif_is_mesh(&sdata->vif) &&
1464                             test_sta_flag(sta, WLAN_STA_PS_STA))
1465                                 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1466
1467                         WARN_ON(__sta_info_destroy(sta));
1468                 }
1469         }
1470
1471         mutex_unlock(&local->sta_mtx);
1472 }
1473
1474 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1475                                                    const u8 *addr,
1476                                                    const u8 *localaddr)
1477 {
1478         struct ieee80211_local *local = hw_to_local(hw);
1479         struct rhlist_head *tmp;
1480         struct sta_info *sta;
1481
1482         /*
1483          * Just return a random station if localaddr is NULL
1484          * ... first in list.
1485          */
1486         for_each_sta_info(local, addr, sta, tmp) {
1487                 if (localaddr &&
1488                     !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1489                         continue;
1490                 if (!sta->uploaded)
1491                         return NULL;
1492                 return &sta->sta;
1493         }
1494
1495         return NULL;
1496 }
1497 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1498
1499 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1500                                          const u8 *addr)
1501 {
1502         struct sta_info *sta;
1503
1504         if (!vif)
1505                 return NULL;
1506
1507         sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1508         if (!sta)
1509                 return NULL;
1510
1511         if (!sta->uploaded)
1512                 return NULL;
1513
1514         return &sta->sta;
1515 }
1516 EXPORT_SYMBOL(ieee80211_find_sta);
1517
1518 /* powersave support code */
1519 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1520 {
1521         struct ieee80211_sub_if_data *sdata = sta->sdata;
1522         struct ieee80211_local *local = sdata->local;
1523         struct sk_buff_head pending;
1524         int filtered = 0, buffered = 0, ac, i;
1525         unsigned long flags;
1526         struct ps_data *ps;
1527
1528         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1529                 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1530                                      u.ap);
1531
1532         if (sdata->vif.type == NL80211_IFTYPE_AP)
1533                 ps = &sdata->bss->ps;
1534         else if (ieee80211_vif_is_mesh(&sdata->vif))
1535                 ps = &sdata->u.mesh.ps;
1536         else
1537                 return;
1538
1539         clear_sta_flag(sta, WLAN_STA_SP);
1540
1541         BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1542         sta->driver_buffered_tids = 0;
1543         sta->txq_buffered_tids = 0;
1544
1545         if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1546                 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1547
1548         for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1549                 if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1550                         continue;
1551
1552                 schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1553         }
1554
1555         skb_queue_head_init(&pending);
1556
1557         /* sync with ieee80211_tx_h_unicast_ps_buf */
1558         spin_lock(&sta->ps_lock);
1559         /* Send all buffered frames to the station */
1560         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1561                 int count = skb_queue_len(&pending), tmp;
1562
1563                 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1564                 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1565                 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1566                 tmp = skb_queue_len(&pending);
1567                 filtered += tmp - count;
1568                 count = tmp;
1569
1570                 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1571                 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1572                 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1573                 tmp = skb_queue_len(&pending);
1574                 buffered += tmp - count;
1575         }
1576
1577         ieee80211_add_pending_skbs(local, &pending);
1578
1579         /* now we're no longer in the deliver code */
1580         clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1581
1582         /* The station might have polled and then woken up before we responded,
1583          * so clear these flags now to avoid them sticking around.
1584          */
1585         clear_sta_flag(sta, WLAN_STA_PSPOLL);
1586         clear_sta_flag(sta, WLAN_STA_UAPSD);
1587         spin_unlock(&sta->ps_lock);
1588
1589         atomic_dec(&ps->num_sta_ps);
1590
1591         local->total_ps_buffered -= buffered;
1592
1593         sta_info_recalc_tim(sta);
1594
1595         ps_dbg(sdata,
1596                "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1597                sta->sta.addr, sta->sta.aid, filtered, buffered);
1598
1599         ieee80211_check_fast_xmit(sta);
1600 }
1601
1602 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1603                                          enum ieee80211_frame_release_type reason,
1604                                          bool call_driver, bool more_data)
1605 {
1606         struct ieee80211_sub_if_data *sdata = sta->sdata;
1607         struct ieee80211_local *local = sdata->local;
1608         struct ieee80211_qos_hdr *nullfunc;
1609         struct sk_buff *skb;
1610         int size = sizeof(*nullfunc);
1611         __le16 fc;
1612         bool qos = sta->sta.wme;
1613         struct ieee80211_tx_info *info;
1614         struct ieee80211_chanctx_conf *chanctx_conf;
1615
1616         if (qos) {
1617                 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1618                                  IEEE80211_STYPE_QOS_NULLFUNC |
1619                                  IEEE80211_FCTL_FROMDS);
1620         } else {
1621                 size -= 2;
1622                 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1623                                  IEEE80211_STYPE_NULLFUNC |
1624                                  IEEE80211_FCTL_FROMDS);
1625         }
1626
1627         skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1628         if (!skb)
1629                 return;
1630
1631         skb_reserve(skb, local->hw.extra_tx_headroom);
1632
1633         nullfunc = skb_put(skb, size);
1634         nullfunc->frame_control = fc;
1635         nullfunc->duration_id = 0;
1636         memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1637         memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1638         memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1639         nullfunc->seq_ctrl = 0;
1640
1641         skb->priority = tid;
1642         skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1643         if (qos) {
1644                 nullfunc->qos_ctrl = cpu_to_le16(tid);
1645
1646                 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1647                         nullfunc->qos_ctrl |=
1648                                 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1649                         if (more_data)
1650                                 nullfunc->frame_control |=
1651                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1652                 }
1653         }
1654
1655         info = IEEE80211_SKB_CB(skb);
1656
1657         /*
1658          * Tell TX path to send this frame even though the
1659          * STA may still remain is PS mode after this frame
1660          * exchange. Also set EOSP to indicate this packet
1661          * ends the poll/service period.
1662          */
1663         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1664                        IEEE80211_TX_STATUS_EOSP |
1665                        IEEE80211_TX_CTL_REQ_TX_STATUS;
1666
1667         info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1668
1669         if (call_driver)
1670                 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1671                                           reason, false);
1672
1673         skb->dev = sdata->dev;
1674
1675         rcu_read_lock();
1676         chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1677         if (WARN_ON(!chanctx_conf)) {
1678                 rcu_read_unlock();
1679                 kfree_skb(skb);
1680                 return;
1681         }
1682
1683         info->band = chanctx_conf->def.chan->band;
1684         ieee80211_xmit(sdata, sta, skb);
1685         rcu_read_unlock();
1686 }
1687
1688 static int find_highest_prio_tid(unsigned long tids)
1689 {
1690         /* lower 3 TIDs aren't ordered perfectly */
1691         if (tids & 0xF8)
1692                 return fls(tids) - 1;
1693         /* TID 0 is BE just like TID 3 */
1694         if (tids & BIT(0))
1695                 return 0;
1696         return fls(tids) - 1;
1697 }
1698
1699 /* Indicates if the MORE_DATA bit should be set in the last
1700  * frame obtained by ieee80211_sta_ps_get_frames.
1701  * Note that driver_release_tids is relevant only if
1702  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1703  */
1704 static bool
1705 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1706                            enum ieee80211_frame_release_type reason,
1707                            unsigned long driver_release_tids)
1708 {
1709         int ac;
1710
1711         /* If the driver has data on more than one TID then
1712          * certainly there's more data if we release just a
1713          * single frame now (from a single TID). This will
1714          * only happen for PS-Poll.
1715          */
1716         if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1717             hweight16(driver_release_tids) > 1)
1718                 return true;
1719
1720         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1721                 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1722                         continue;
1723
1724                 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1725                     !skb_queue_empty(&sta->ps_tx_buf[ac]))
1726                         return true;
1727         }
1728
1729         return false;
1730 }
1731
1732 static void
1733 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1734                             enum ieee80211_frame_release_type reason,
1735                             struct sk_buff_head *frames,
1736                             unsigned long *driver_release_tids)
1737 {
1738         struct ieee80211_sub_if_data *sdata = sta->sdata;
1739         struct ieee80211_local *local = sdata->local;
1740         int ac;
1741
1742         /* Get response frame(s) and more data bit for the last one. */
1743         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1744                 unsigned long tids;
1745
1746                 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1747                         continue;
1748
1749                 tids = ieee80211_tids_for_ac(ac);
1750
1751                 /* if we already have frames from software, then we can't also
1752                  * release from hardware queues
1753                  */
1754                 if (skb_queue_empty(frames)) {
1755                         *driver_release_tids |=
1756                                 sta->driver_buffered_tids & tids;
1757                         *driver_release_tids |= sta->txq_buffered_tids & tids;
1758                 }
1759
1760                 if (!*driver_release_tids) {
1761                         struct sk_buff *skb;
1762
1763                         while (n_frames > 0) {
1764                                 skb = skb_dequeue(&sta->tx_filtered[ac]);
1765                                 if (!skb) {
1766                                         skb = skb_dequeue(
1767                                                 &sta->ps_tx_buf[ac]);
1768                                         if (skb)
1769                                                 local->total_ps_buffered--;
1770                                 }
1771                                 if (!skb)
1772                                         break;
1773                                 n_frames--;
1774                                 __skb_queue_tail(frames, skb);
1775                         }
1776                 }
1777
1778                 /* If we have more frames buffered on this AC, then abort the
1779                  * loop since we can't send more data from other ACs before
1780                  * the buffered frames from this.
1781                  */
1782                 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1783                     !skb_queue_empty(&sta->ps_tx_buf[ac]))
1784                         break;
1785         }
1786 }
1787
1788 static void
1789 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1790                                   int n_frames, u8 ignored_acs,
1791                                   enum ieee80211_frame_release_type reason)
1792 {
1793         struct ieee80211_sub_if_data *sdata = sta->sdata;
1794         struct ieee80211_local *local = sdata->local;
1795         unsigned long driver_release_tids = 0;
1796         struct sk_buff_head frames;
1797         bool more_data;
1798
1799         /* Service or PS-Poll period starts */
1800         set_sta_flag(sta, WLAN_STA_SP);
1801
1802         __skb_queue_head_init(&frames);
1803
1804         ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1805                                     &frames, &driver_release_tids);
1806
1807         more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1808
1809         if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1810                 driver_release_tids =
1811                         BIT(find_highest_prio_tid(driver_release_tids));
1812
1813         if (skb_queue_empty(&frames) && !driver_release_tids) {
1814                 int tid, ac;
1815
1816                 /*
1817                  * For PS-Poll, this can only happen due to a race condition
1818                  * when we set the TIM bit and the station notices it, but
1819                  * before it can poll for the frame we expire it.
1820                  *
1821                  * For uAPSD, this is said in the standard (11.2.1.5 h):
1822                  *      At each unscheduled SP for a non-AP STA, the AP shall
1823                  *      attempt to transmit at least one MSDU or MMPDU, but no
1824                  *      more than the value specified in the Max SP Length field
1825                  *      in the QoS Capability element from delivery-enabled ACs,
1826                  *      that are destined for the non-AP STA.
1827                  *
1828                  * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1829                  */
1830
1831                 /* This will evaluate to 1, 3, 5 or 7. */
1832                 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1833                         if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1834                                 break;
1835                 tid = 7 - 2 * ac;
1836
1837                 ieee80211_send_null_response(sta, tid, reason, true, false);
1838         } else if (!driver_release_tids) {
1839                 struct sk_buff_head pending;
1840                 struct sk_buff *skb;
1841                 int num = 0;
1842                 u16 tids = 0;
1843                 bool need_null = false;
1844
1845                 skb_queue_head_init(&pending);
1846
1847                 while ((skb = __skb_dequeue(&frames))) {
1848                         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1849                         struct ieee80211_hdr *hdr = (void *) skb->data;
1850                         u8 *qoshdr = NULL;
1851
1852                         num++;
1853
1854                         /*
1855                          * Tell TX path to send this frame even though the
1856                          * STA may still remain is PS mode after this frame
1857                          * exchange.
1858                          */
1859                         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1860                         info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1861
1862                         /*
1863                          * Use MoreData flag to indicate whether there are
1864                          * more buffered frames for this STA
1865                          */
1866                         if (more_data || !skb_queue_empty(&frames))
1867                                 hdr->frame_control |=
1868                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1869                         else
1870                                 hdr->frame_control &=
1871                                         cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1872
1873                         if (ieee80211_is_data_qos(hdr->frame_control) ||
1874                             ieee80211_is_qos_nullfunc(hdr->frame_control))
1875                                 qoshdr = ieee80211_get_qos_ctl(hdr);
1876
1877                         tids |= BIT(skb->priority);
1878
1879                         __skb_queue_tail(&pending, skb);
1880
1881                         /* end service period after last frame or add one */
1882                         if (!skb_queue_empty(&frames))
1883                                 continue;
1884
1885                         if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1886                                 /* for PS-Poll, there's only one frame */
1887                                 info->flags |= IEEE80211_TX_STATUS_EOSP |
1888                                                IEEE80211_TX_CTL_REQ_TX_STATUS;
1889                                 break;
1890                         }
1891
1892                         /* For uAPSD, things are a bit more complicated. If the
1893                          * last frame has a QoS header (i.e. is a QoS-data or
1894                          * QoS-nulldata frame) then just set the EOSP bit there
1895                          * and be done.
1896                          * If the frame doesn't have a QoS header (which means
1897                          * it should be a bufferable MMPDU) then we can't set
1898                          * the EOSP bit in the QoS header; add a QoS-nulldata
1899                          * frame to the list to send it after the MMPDU.
1900                          *
1901                          * Note that this code is only in the mac80211-release
1902                          * code path, we assume that the driver will not buffer
1903                          * anything but QoS-data frames, or if it does, will
1904                          * create the QoS-nulldata frame by itself if needed.
1905                          *
1906                          * Cf. 802.11-2012 10.2.1.10 (c).
1907                          */
1908                         if (qoshdr) {
1909                                 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1910
1911                                 info->flags |= IEEE80211_TX_STATUS_EOSP |
1912                                                IEEE80211_TX_CTL_REQ_TX_STATUS;
1913                         } else {
1914                                 /* The standard isn't completely clear on this
1915                                  * as it says the more-data bit should be set
1916                                  * if there are more BUs. The QoS-Null frame
1917                                  * we're about to send isn't buffered yet, we
1918                                  * only create it below, but let's pretend it
1919                                  * was buffered just in case some clients only
1920                                  * expect more-data=0 when eosp=1.
1921                                  */
1922                                 hdr->frame_control |=
1923                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1924                                 need_null = true;
1925                                 num++;
1926                         }
1927                         break;
1928                 }
1929
1930                 drv_allow_buffered_frames(local, sta, tids, num,
1931                                           reason, more_data);
1932
1933                 ieee80211_add_pending_skbs(local, &pending);
1934
1935                 if (need_null)
1936                         ieee80211_send_null_response(
1937                                 sta, find_highest_prio_tid(tids),
1938                                 reason, false, false);
1939
1940                 sta_info_recalc_tim(sta);
1941         } else {
1942                 int tid;
1943
1944                 /*
1945                  * We need to release a frame that is buffered somewhere in the
1946                  * driver ... it'll have to handle that.
1947                  * Note that the driver also has to check the number of frames
1948                  * on the TIDs we're releasing from - if there are more than
1949                  * n_frames it has to set the more-data bit (if we didn't ask
1950                  * it to set it anyway due to other buffered frames); if there
1951                  * are fewer than n_frames it has to make sure to adjust that
1952                  * to allow the service period to end properly.
1953                  */
1954                 drv_release_buffered_frames(local, sta, driver_release_tids,
1955                                             n_frames, reason, more_data);
1956
1957                 /*
1958                  * Note that we don't recalculate the TIM bit here as it would
1959                  * most likely have no effect at all unless the driver told us
1960                  * that the TID(s) became empty before returning here from the
1961                  * release function.
1962                  * Either way, however, when the driver tells us that the TID(s)
1963                  * became empty or we find that a txq became empty, we'll do the
1964                  * TIM recalculation.
1965                  */
1966
1967                 if (!sta->sta.txq[0])
1968                         return;
1969
1970                 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1971                         if (!sta->sta.txq[tid] ||
1972                             !(driver_release_tids & BIT(tid)) ||
1973                             txq_has_queue(sta->sta.txq[tid]))
1974                                 continue;
1975
1976                         sta_info_recalc_tim(sta);
1977                         break;
1978                 }
1979         }
1980 }
1981
1982 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1983 {
1984         u8 ignore_for_response = sta->sta.uapsd_queues;
1985
1986         /*
1987          * If all ACs are delivery-enabled then we should reply
1988          * from any of them, if only some are enabled we reply
1989          * only from the non-enabled ones.
1990          */
1991         if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1992                 ignore_for_response = 0;
1993
1994         ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1995                                           IEEE80211_FRAME_RELEASE_PSPOLL);
1996 }
1997
1998 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1999 {
2000         int n_frames = sta->sta.max_sp;
2001         u8 delivery_enabled = sta->sta.uapsd_queues;
2002
2003         /*
2004          * If we ever grow support for TSPEC this might happen if
2005          * the TSPEC update from hostapd comes in between a trigger
2006          * frame setting WLAN_STA_UAPSD in the RX path and this
2007          * actually getting called.
2008          */
2009         if (!delivery_enabled)
2010                 return;
2011
2012         switch (sta->sta.max_sp) {
2013         case 1:
2014                 n_frames = 2;
2015                 break;
2016         case 2:
2017                 n_frames = 4;
2018                 break;
2019         case 3:
2020                 n_frames = 6;
2021                 break;
2022         case 0:
2023                 /* XXX: what is a good value? */
2024                 n_frames = 128;
2025                 break;
2026         }
2027
2028         ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
2029                                           IEEE80211_FRAME_RELEASE_UAPSD);
2030 }
2031
2032 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
2033                                struct ieee80211_sta *pubsta, bool block)
2034 {
2035         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2036
2037         trace_api_sta_block_awake(sta->local, pubsta, block);
2038
2039         if (block) {
2040                 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
2041                 ieee80211_clear_fast_xmit(sta);
2042                 return;
2043         }
2044
2045         if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
2046                 return;
2047
2048         if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
2049                 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
2050                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2051                 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2052         } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
2053                    test_sta_flag(sta, WLAN_STA_UAPSD)) {
2054                 /* must be asleep in this case */
2055                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2056                 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2057         } else {
2058                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2059                 ieee80211_check_fast_xmit(sta);
2060         }
2061 }
2062 EXPORT_SYMBOL(ieee80211_sta_block_awake);
2063
2064 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
2065 {
2066         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2067         struct ieee80211_local *local = sta->local;
2068
2069         trace_api_eosp(local, pubsta);
2070
2071         clear_sta_flag(sta, WLAN_STA_SP);
2072 }
2073 EXPORT_SYMBOL(ieee80211_sta_eosp);
2074
2075 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
2076 {
2077         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2078         enum ieee80211_frame_release_type reason;
2079         bool more_data;
2080
2081         trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
2082
2083         reason = IEEE80211_FRAME_RELEASE_UAPSD;
2084         more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
2085                                                reason, 0);
2086
2087         ieee80211_send_null_response(sta, tid, reason, false, more_data);
2088 }
2089 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
2090
2091 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2092                                 u8 tid, bool buffered)
2093 {
2094         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2095
2096         if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2097                 return;
2098
2099         trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
2100
2101         if (buffered)
2102                 set_bit(tid, &sta->driver_buffered_tids);
2103         else
2104                 clear_bit(tid, &sta->driver_buffered_tids);
2105
2106         sta_info_recalc_tim(sta);
2107 }
2108 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2109
2110 void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2111                                     u32 tx_airtime, u32 rx_airtime)
2112 {
2113         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2114         struct ieee80211_local *local = sta->sdata->local;
2115         u8 ac = ieee80211_ac_from_tid(tid);
2116         u32 airtime = 0;
2117         u32 diff;
2118
2119         if (sta->local->airtime_flags & AIRTIME_USE_TX)
2120                 airtime += tx_airtime;
2121         if (sta->local->airtime_flags & AIRTIME_USE_RX)
2122                 airtime += rx_airtime;
2123
2124         spin_lock_bh(&local->active_txq_lock[ac]);
2125         sta->airtime[ac].tx_airtime += tx_airtime;
2126         sta->airtime[ac].rx_airtime += rx_airtime;
2127
2128         diff = (u32)jiffies - sta->airtime[ac].last_active;
2129         if (diff <= AIRTIME_ACTIVE_DURATION)
2130                 sta->airtime[ac].deficit -= airtime;
2131
2132         spin_unlock_bh(&local->active_txq_lock[ac]);
2133 }
2134 EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2135
2136 void ieee80211_sta_recalc_aggregates(struct ieee80211_sta *pubsta)
2137 {
2138         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2139         struct ieee80211_link_sta *link_sta;
2140         int link_id, i;
2141         bool first = true;
2142
2143         if (!pubsta->valid_links || !pubsta->mlo) {
2144                 pubsta->cur = &pubsta->deflink.agg;
2145                 return;
2146         }
2147
2148         rcu_read_lock();
2149         for_each_sta_active_link(&sta->sdata->vif, pubsta, link_sta, link_id) {
2150                 if (first) {
2151                         sta->cur = pubsta->deflink.agg;
2152                         first = false;
2153                         continue;
2154                 }
2155
2156                 sta->cur.max_amsdu_len =
2157                         min(sta->cur.max_amsdu_len,
2158                             link_sta->agg.max_amsdu_len);
2159                 sta->cur.max_rc_amsdu_len =
2160                         min(sta->cur.max_rc_amsdu_len,
2161                             link_sta->agg.max_rc_amsdu_len);
2162
2163                 for (i = 0; i < ARRAY_SIZE(sta->cur.max_tid_amsdu_len); i++)
2164                         sta->cur.max_tid_amsdu_len[i] =
2165                                 min(sta->cur.max_tid_amsdu_len[i],
2166                                     link_sta->agg.max_tid_amsdu_len[i]);
2167         }
2168         rcu_read_unlock();
2169
2170         pubsta->cur = &sta->cur;
2171 }
2172 EXPORT_SYMBOL(ieee80211_sta_recalc_aggregates);
2173
2174 void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
2175                                           struct sta_info *sta, u8 ac,
2176                                           u16 tx_airtime, bool tx_completed)
2177 {
2178         int tx_pending;
2179
2180         if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2181                 return;
2182
2183         if (!tx_completed) {
2184                 if (sta)
2185                         atomic_add(tx_airtime,
2186                                    &sta->airtime[ac].aql_tx_pending);
2187
2188                 atomic_add(tx_airtime, &local->aql_total_pending_airtime);
2189                 atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
2190                 return;
2191         }
2192
2193         if (sta) {
2194                 tx_pending = atomic_sub_return(tx_airtime,
2195                                                &sta->airtime[ac].aql_tx_pending);
2196                 if (tx_pending < 0)
2197                         atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
2198                                        tx_pending, 0);
2199         }
2200
2201         atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
2202         tx_pending = atomic_sub_return(tx_airtime,
2203                                        &local->aql_ac_pending_airtime[ac]);
2204         if (WARN_ONCE(tx_pending < 0,
2205                       "Device %s AC %d pending airtime underflow: %u, %u",
2206                       wiphy_name(local->hw.wiphy), ac, tx_pending,
2207                       tx_airtime)) {
2208                 atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
2209                                tx_pending, 0);
2210                 atomic_sub(tx_pending, &local->aql_total_pending_airtime);
2211         }
2212 }
2213
2214 int sta_info_move_state(struct sta_info *sta,
2215                         enum ieee80211_sta_state new_state)
2216 {
2217         might_sleep();
2218
2219         if (sta->sta_state == new_state)
2220                 return 0;
2221
2222         /* check allowed transitions first */
2223
2224         switch (new_state) {
2225         case IEEE80211_STA_NONE:
2226                 if (sta->sta_state != IEEE80211_STA_AUTH)
2227                         return -EINVAL;
2228                 break;
2229         case IEEE80211_STA_AUTH:
2230                 if (sta->sta_state != IEEE80211_STA_NONE &&
2231                     sta->sta_state != IEEE80211_STA_ASSOC)
2232                         return -EINVAL;
2233                 break;
2234         case IEEE80211_STA_ASSOC:
2235                 if (sta->sta_state != IEEE80211_STA_AUTH &&
2236                     sta->sta_state != IEEE80211_STA_AUTHORIZED)
2237                         return -EINVAL;
2238                 break;
2239         case IEEE80211_STA_AUTHORIZED:
2240                 if (sta->sta_state != IEEE80211_STA_ASSOC)
2241                         return -EINVAL;
2242                 break;
2243         default:
2244                 WARN(1, "invalid state %d", new_state);
2245                 return -EINVAL;
2246         }
2247
2248         sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2249                 sta->sta.addr, new_state);
2250
2251         /*
2252          * notify the driver before the actual changes so it can
2253          * fail the transition
2254          */
2255         if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2256                 int err = drv_sta_state(sta->local, sta->sdata, sta,
2257                                         sta->sta_state, new_state);
2258                 if (err)
2259                         return err;
2260         }
2261
2262         /* reflect the change in all state variables */
2263
2264         switch (new_state) {
2265         case IEEE80211_STA_NONE:
2266                 if (sta->sta_state == IEEE80211_STA_AUTH)
2267                         clear_bit(WLAN_STA_AUTH, &sta->_flags);
2268                 break;
2269         case IEEE80211_STA_AUTH:
2270                 if (sta->sta_state == IEEE80211_STA_NONE) {
2271                         set_bit(WLAN_STA_AUTH, &sta->_flags);
2272                 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2273                         clear_bit(WLAN_STA_ASSOC, &sta->_flags);
2274                         ieee80211_recalc_min_chandef(sta->sdata, -1);
2275                         if (!sta->sta.support_p2p_ps)
2276                                 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2277                 }
2278                 break;
2279         case IEEE80211_STA_ASSOC:
2280                 if (sta->sta_state == IEEE80211_STA_AUTH) {
2281                         set_bit(WLAN_STA_ASSOC, &sta->_flags);
2282                         sta->assoc_at = ktime_get_boottime_ns();
2283                         ieee80211_recalc_min_chandef(sta->sdata, -1);
2284                         if (!sta->sta.support_p2p_ps)
2285                                 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2286                 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
2287                         ieee80211_vif_dec_num_mcast(sta->sdata);
2288                         clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
2289                         ieee80211_clear_fast_xmit(sta);
2290                         ieee80211_clear_fast_rx(sta);
2291                 }
2292                 break;
2293         case IEEE80211_STA_AUTHORIZED:
2294                 if (sta->sta_state == IEEE80211_STA_ASSOC) {
2295                         ieee80211_vif_inc_num_mcast(sta->sdata);
2296                         set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
2297                         ieee80211_check_fast_xmit(sta);
2298                         ieee80211_check_fast_rx(sta);
2299                 }
2300                 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2301                     sta->sdata->vif.type == NL80211_IFTYPE_AP)
2302                         cfg80211_send_layer2_update(sta->sdata->dev,
2303                                                     sta->sta.addr);
2304                 break;
2305         default:
2306                 break;
2307         }
2308
2309         sta->sta_state = new_state;
2310
2311         return 0;
2312 }
2313
2314 static struct ieee80211_sta_rx_stats *
2315 sta_get_last_rx_stats(struct sta_info *sta)
2316 {
2317         struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2318         int cpu;
2319
2320         if (!sta->deflink.pcpu_rx_stats)
2321                 return stats;
2322
2323         for_each_possible_cpu(cpu) {
2324                 struct ieee80211_sta_rx_stats *cpustats;
2325
2326                 cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2327
2328                 if (time_after(cpustats->last_rx, stats->last_rx))
2329                         stats = cpustats;
2330         }
2331
2332         return stats;
2333 }
2334
2335 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
2336                                   struct rate_info *rinfo)
2337 {
2338         rinfo->bw = STA_STATS_GET(BW, rate);
2339
2340         switch (STA_STATS_GET(TYPE, rate)) {
2341         case STA_STATS_RATE_TYPE_VHT:
2342                 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2343                 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2344                 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2345                 if (STA_STATS_GET(SGI, rate))
2346                         rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2347                 break;
2348         case STA_STATS_RATE_TYPE_HT:
2349                 rinfo->flags = RATE_INFO_FLAGS_MCS;
2350                 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2351                 if (STA_STATS_GET(SGI, rate))
2352                         rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2353                 break;
2354         case STA_STATS_RATE_TYPE_LEGACY: {
2355                 struct ieee80211_supported_band *sband;
2356                 u16 brate;
2357                 unsigned int shift;
2358                 int band = STA_STATS_GET(LEGACY_BAND, rate);
2359                 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2360
2361                 sband = local->hw.wiphy->bands[band];
2362
2363                 if (WARN_ON_ONCE(!sband->bitrates))
2364                         break;
2365
2366                 brate = sband->bitrates[rate_idx].bitrate;
2367                 if (rinfo->bw == RATE_INFO_BW_5)
2368                         shift = 2;
2369                 else if (rinfo->bw == RATE_INFO_BW_10)
2370                         shift = 1;
2371                 else
2372                         shift = 0;
2373                 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2374                 break;
2375                 }
2376         case STA_STATS_RATE_TYPE_HE:
2377                 rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2378                 rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2379                 rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2380                 rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2381                 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2382                 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2383                 break;
2384         }
2385 }
2386
2387 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2388 {
2389         u32 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2390
2391         if (rate == STA_STATS_RATE_INVALID)
2392                 return -EINVAL;
2393
2394         sta_stats_decode_rate(sta->local, rate, rinfo);
2395         return 0;
2396 }
2397
2398 static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2399                                         int tid)
2400 {
2401         unsigned int start;
2402         u64 value;
2403
2404         do {
2405                 start = u64_stats_fetch_begin_irq(&rxstats->syncp);
2406                 value = rxstats->msdu[tid];
2407         } while (u64_stats_fetch_retry_irq(&rxstats->syncp, start));
2408
2409         return value;
2410 }
2411
2412 static void sta_set_tidstats(struct sta_info *sta,
2413                              struct cfg80211_tid_stats *tidstats,
2414                              int tid)
2415 {
2416         struct ieee80211_local *local = sta->local;
2417         int cpu;
2418
2419         if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2420                 tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2421                                                            tid);
2422
2423                 if (sta->deflink.pcpu_rx_stats) {
2424                         for_each_possible_cpu(cpu) {
2425                                 struct ieee80211_sta_rx_stats *cpurxs;
2426
2427                                 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2428                                                      cpu);
2429                                 tidstats->rx_msdu +=
2430                                         sta_get_tidstats_msdu(cpurxs, tid);
2431                         }
2432                 }
2433
2434                 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2435         }
2436
2437         if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2438                 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2439                 tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
2440         }
2441
2442         if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2443             ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2444                 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2445                 tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
2446         }
2447
2448         if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2449             ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2450                 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2451                 tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
2452         }
2453
2454         if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
2455                 spin_lock_bh(&local->fq.lock);
2456                 rcu_read_lock();
2457
2458                 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2459                 ieee80211_fill_txq_stats(&tidstats->txq_stats,
2460                                          to_txq_info(sta->sta.txq[tid]));
2461
2462                 rcu_read_unlock();
2463                 spin_unlock_bh(&local->fq.lock);
2464         }
2465 }
2466
2467 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2468 {
2469         unsigned int start;
2470         u64 value;
2471
2472         do {
2473                 start = u64_stats_fetch_begin_irq(&rxstats->syncp);
2474                 value = rxstats->bytes;
2475         } while (u64_stats_fetch_retry_irq(&rxstats->syncp, start));
2476
2477         return value;
2478 }
2479
2480 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
2481                    bool tidstats)
2482 {
2483         struct ieee80211_sub_if_data *sdata = sta->sdata;
2484         struct ieee80211_local *local = sdata->local;
2485         u32 thr = 0;
2486         int i, ac, cpu;
2487         struct ieee80211_sta_rx_stats *last_rxstats;
2488
2489         last_rxstats = sta_get_last_rx_stats(sta);
2490
2491         sinfo->generation = sdata->local->sta_generation;
2492
2493         /* do before driver, so beacon filtering drivers have a
2494          * chance to e.g. just add the number of filtered beacons
2495          * (or just modify the value entirely, of course)
2496          */
2497         if (sdata->vif.type == NL80211_IFTYPE_STATION)
2498                 sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2499
2500         drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2501         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2502                          BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2503                          BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2504                          BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2505                          BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2506                          BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2507
2508         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2509                 sinfo->beacon_loss_count =
2510                         sdata->deflink.u.mgd.beacon_loss_count;
2511                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2512         }
2513
2514         sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2515         sinfo->assoc_at = sta->assoc_at;
2516         sinfo->inactive_time =
2517                 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2518
2519         if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2520                                BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2521                 sinfo->tx_bytes = 0;
2522                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2523                         sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2524                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2525         }
2526
2527         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2528                 sinfo->tx_packets = 0;
2529                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2530                         sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2531                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2532         }
2533
2534         if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2535                                BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2536                 sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
2537
2538                 if (sta->deflink.pcpu_rx_stats) {
2539                         for_each_possible_cpu(cpu) {
2540                                 struct ieee80211_sta_rx_stats *cpurxs;
2541
2542                                 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2543                                                      cpu);
2544                                 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2545                         }
2546                 }
2547
2548                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2549         }
2550
2551         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2552                 sinfo->rx_packets = sta->deflink.rx_stats.packets;
2553                 if (sta->deflink.pcpu_rx_stats) {
2554                         for_each_possible_cpu(cpu) {
2555                                 struct ieee80211_sta_rx_stats *cpurxs;
2556
2557                                 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2558                                                      cpu);
2559                                 sinfo->rx_packets += cpurxs->packets;
2560                         }
2561                 }
2562                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2563         }
2564
2565         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2566                 sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2567                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2568         }
2569
2570         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2571                 sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2572                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2573         }
2574
2575         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2576                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2577                         sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2578                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2579         }
2580
2581         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2582                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2583                         sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2584                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2585         }
2586
2587         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2588                 sinfo->airtime_weight = sta->airtime_weight;
2589                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2590         }
2591
2592         sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2593         if (sta->deflink.pcpu_rx_stats) {
2594                 for_each_possible_cpu(cpu) {
2595                         struct ieee80211_sta_rx_stats *cpurxs;
2596
2597                         cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2598                         sinfo->rx_dropped_misc += cpurxs->dropped;
2599                 }
2600         }
2601
2602         if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2603             !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2604                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2605                                  BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2606                 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2607         }
2608
2609         if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2610             ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2611                 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2612                         sinfo->signal = (s8)last_rxstats->last_signal;
2613                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2614                 }
2615
2616                 if (!sta->deflink.pcpu_rx_stats &&
2617                     !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
2618                         sinfo->signal_avg =
2619                                 -ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2620                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2621                 }
2622         }
2623
2624         /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2625          * the sta->rx_stats struct, so the check here is fine with and without
2626          * pcpu statistics
2627          */
2628         if (last_rxstats->chains &&
2629             !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2630                                BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2631                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2632                 if (!sta->deflink.pcpu_rx_stats)
2633                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2634
2635                 sinfo->chains = last_rxstats->chains;
2636
2637                 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2638                         sinfo->chain_signal[i] =
2639                                 last_rxstats->chain_signal_last[i];
2640                         sinfo->chain_signal_avg[i] =
2641                                 -ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2642                 }
2643         }
2644
2645         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
2646             !sta->sta.valid_links) {
2647                 sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2648                                      &sinfo->txrate);
2649                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2650         }
2651
2652         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
2653             !sta->sta.valid_links) {
2654                 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2655                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2656         }
2657
2658         if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
2659                 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
2660                         sta_set_tidstats(sta, &sinfo->pertid[i], i);
2661         }
2662
2663         if (ieee80211_vif_is_mesh(&sdata->vif)) {
2664 #ifdef CONFIG_MAC80211_MESH
2665                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2666                                  BIT_ULL(NL80211_STA_INFO_PLID) |
2667                                  BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2668                                  BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2669                                  BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2670                                  BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
2671                                  BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
2672                                  BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2673
2674                 sinfo->llid = sta->mesh->llid;
2675                 sinfo->plid = sta->mesh->plid;
2676                 sinfo->plink_state = sta->mesh->plink_state;
2677                 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2678                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2679                         sinfo->t_offset = sta->mesh->t_offset;
2680                 }
2681                 sinfo->local_pm = sta->mesh->local_pm;
2682                 sinfo->peer_pm = sta->mesh->peer_pm;
2683                 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2684                 sinfo->connected_to_gate = sta->mesh->connected_to_gate;
2685                 sinfo->connected_to_as = sta->mesh->connected_to_as;
2686 #endif
2687         }
2688
2689         sinfo->bss_param.flags = 0;
2690         if (sdata->vif.bss_conf.use_cts_prot)
2691                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2692         if (sdata->vif.bss_conf.use_short_preamble)
2693                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2694         if (sdata->vif.bss_conf.use_short_slot)
2695                 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2696         sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2697         sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2698
2699         sinfo->sta_flags.set = 0;
2700         sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2701                                 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2702                                 BIT(NL80211_STA_FLAG_WME) |
2703                                 BIT(NL80211_STA_FLAG_MFP) |
2704                                 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2705                                 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2706                                 BIT(NL80211_STA_FLAG_TDLS_PEER);
2707         if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2708                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2709         if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2710                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2711         if (sta->sta.wme)
2712                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2713         if (test_sta_flag(sta, WLAN_STA_MFP))
2714                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2715         if (test_sta_flag(sta, WLAN_STA_AUTH))
2716                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2717         if (test_sta_flag(sta, WLAN_STA_ASSOC))
2718                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2719         if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2720                 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2721
2722         thr = sta_get_expected_throughput(sta);
2723
2724         if (thr != 0) {
2725                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2726                 sinfo->expected_throughput = thr;
2727         }
2728
2729         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2730             sta->deflink.status_stats.ack_signal_filled) {
2731                 sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2732                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2733         }
2734
2735         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2736             sta->deflink.status_stats.ack_signal_filled) {
2737                 sinfo->avg_ack_signal =
2738                         -(s8)ewma_avg_signal_read(
2739                                 &sta->deflink.status_stats.avg_ack_signal);
2740                 sinfo->filled |=
2741                         BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2742         }
2743
2744         if (ieee80211_vif_is_mesh(&sdata->vif)) {
2745                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2746                 sinfo->airtime_link_metric =
2747                         airtime_link_metric_get(local, sta);
2748         }
2749 }
2750
2751 u32 sta_get_expected_throughput(struct sta_info *sta)
2752 {
2753         struct ieee80211_sub_if_data *sdata = sta->sdata;
2754         struct ieee80211_local *local = sdata->local;
2755         struct rate_control_ref *ref = NULL;
2756         u32 thr = 0;
2757
2758         if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2759                 ref = local->rate_ctrl;
2760
2761         /* check if the driver has a SW RC implementation */
2762         if (ref && ref->ops->get_expected_throughput)
2763                 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2764         else
2765                 thr = drv_get_expected_throughput(local, sta);
2766
2767         return thr;
2768 }
2769
2770 unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2771 {
2772         struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2773
2774         if (!sta->deflink.status_stats.last_ack ||
2775             time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2776                 return stats->last_rx;
2777         return sta->deflink.status_stats.last_ack;
2778 }
2779
2780 static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2781 {
2782         if (!sta->sdata->local->ops->wake_tx_queue)
2783                 return;
2784
2785         if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2786                 sta->cparams.target = MS2TIME(50);
2787                 sta->cparams.interval = MS2TIME(300);
2788                 sta->cparams.ecn = false;
2789         } else {
2790                 sta->cparams.target = MS2TIME(20);
2791                 sta->cparams.interval = MS2TIME(100);
2792                 sta->cparams.ecn = true;
2793         }
2794 }
2795
2796 void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2797                                            u32 thr)
2798 {
2799         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2800
2801         sta_update_codel_params(sta, thr);
2802 }
2803
2804 int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2805 {
2806         struct ieee80211_sub_if_data *sdata = sta->sdata;
2807         struct sta_link_alloc *alloc;
2808         int ret;
2809
2810         lockdep_assert_held(&sdata->local->sta_mtx);
2811
2812         /* must represent an MLD from the start */
2813         if (WARN_ON(!sta->sta.valid_links))
2814                 return -EINVAL;
2815
2816         if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2817                     sta->link[link_id]))
2818                 return -EBUSY;
2819
2820         alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2821         if (!alloc)
2822                 return -ENOMEM;
2823
2824         ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2825         if (ret) {
2826                 kfree(alloc);
2827                 return ret;
2828         }
2829
2830         sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2831
2832         return 0;
2833 }
2834
2835 void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
2836 {
2837         lockdep_assert_held(&sta->sdata->local->sta_mtx);
2838
2839         sta_remove_link(sta, link_id, false);
2840 }
2841
2842 int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2843 {
2844         struct ieee80211_sub_if_data *sdata = sta->sdata;
2845         struct link_sta_info *link_sta;
2846         u16 old_links = sta->sta.valid_links;
2847         u16 new_links = old_links | BIT(link_id);
2848         int ret;
2849
2850         link_sta = rcu_dereference_protected(sta->link[link_id],
2851                                              lockdep_is_held(&sdata->local->sta_mtx));
2852
2853         if (WARN_ON(old_links == new_links || !link_sta))
2854                 return -EINVAL;
2855
2856         rcu_read_lock();
2857         if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2858                 rcu_read_unlock();
2859                 return -EALREADY;
2860         }
2861         /* we only modify under the mutex so this is fine */
2862         rcu_read_unlock();
2863
2864         sta->sta.valid_links = new_links;
2865
2866         if (!test_sta_flag(sta, WLAN_STA_INSERTED))
2867                 goto hash;
2868
2869         ieee80211_recalc_min_chandef(sdata, link_id);
2870
2871         /* Ensure the values are updated for the driver,
2872          * redone by sta_remove_link on failure.
2873          */
2874         ieee80211_sta_recalc_aggregates(&sta->sta);
2875
2876         ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2877                                    old_links, new_links);
2878         if (ret) {
2879                 sta->sta.valid_links = old_links;
2880                 sta_remove_link(sta, link_id, false);
2881                 return ret;
2882         }
2883
2884 hash:
2885         ret = link_sta_info_hash_add(sdata->local, link_sta);
2886         WARN_ON(ret);
2887         return 0;
2888 }
2889
2890 void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2891 {
2892         struct ieee80211_sub_if_data *sdata = sta->sdata;
2893         u16 old_links = sta->sta.valid_links;
2894
2895         lockdep_assert_held(&sdata->local->sta_mtx);
2896
2897         sta->sta.valid_links &= ~BIT(link_id);
2898
2899         if (test_sta_flag(sta, WLAN_STA_INSERTED))
2900                 drv_change_sta_links(sdata->local, sdata, &sta->sta,
2901                                      old_links, sta->sta.valid_links);
2902
2903         sta_remove_link(sta, link_id, true);
2904 }
2905
2906 void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2907                                            const u8 *ext_capab,
2908                                            unsigned int ext_capab_len)
2909 {
2910         u8 val;
2911
2912         sta->sta.max_amsdu_subframes = 0;
2913
2914         if (ext_capab_len < 8)
2915                 return;
2916
2917         /* The sender might not have sent the last bit, consider it to be 0 */
2918         val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
2919
2920         /* we did get all the bits, take the MSB as well */
2921         if (ext_capab_len >= 9)
2922                 val |= u8_get_bits(ext_capab[8],
2923                                    WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
2924
2925         if (val)
2926                 sta->sta.max_amsdu_subframes = 4 << (4 - val);
2927 }
2928
2929 #ifdef CONFIG_LOCKDEP
2930 bool lockdep_sta_mutex_held(struct ieee80211_sta *pubsta)
2931 {
2932         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2933
2934         return lockdep_is_held(&sta->local->sta_mtx);
2935 }
2936 EXPORT_SYMBOL(lockdep_sta_mutex_held);
2937 #endif