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