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