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