1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2002-2005, Instant802 Networks, Inc. 4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> 5 * Copyright 2013-2014 Intel Mobile Communications GmbH 6 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH 7 * Copyright (C) 2018-2026 Intel Corporation 8 */ 9 10 #include <linux/module.h> 11 #include <linux/init.h> 12 #include <linux/etherdevice.h> 13 #include <linux/netdevice.h> 14 #include <linux/types.h> 15 #include <linux/slab.h> 16 #include <linux/skbuff.h> 17 #include <linux/if_arp.h> 18 #include <linux/timer.h> 19 #include <linux/rtnetlink.h> 20 21 #include <net/mac80211.h> 22 #include "ieee80211_i.h" 23 #include "driver-ops.h" 24 #include "rate.h" 25 #include "sta_info.h" 26 #include "debugfs_sta.h" 27 #include "mesh.h" 28 #include "wme.h" 29 30 /** 31 * DOC: STA information lifetime rules 32 * 33 * STA info structures (&struct sta_info) are managed in a hash table 34 * for faster lookup and a list for iteration. They are managed using 35 * RCU, i.e. access to the list and hash table is protected by RCU. 36 * 37 * Upon allocating a STA info structure with sta_info_alloc(), the caller 38 * owns that structure. It must then insert it into the hash table using 39 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter 40 * case (which acquires an rcu read section but must not be called from 41 * within one) will the pointer still be valid after the call. Note that 42 * the caller may not do much with the STA info before inserting it; in 43 * particular, it may not start any mesh peer link management or add 44 * encryption keys. 45 * 46 * When the insertion fails (sta_info_insert()) returns non-zero), the 47 * structure will have been freed by sta_info_insert()! 48 * 49 * Station entries are added by mac80211 when you establish a link with a 50 * peer. This means different things for the different type of interfaces 51 * we support. For a regular station this mean we add the AP sta when we 52 * receive an association response from the AP. For IBSS this occurs when 53 * get to know about a peer on the same IBSS. For WDS we add the sta for 54 * the peer immediately upon device open. When using AP mode we add stations 55 * for each respective station upon request from userspace through nl80211. 56 * 57 * In order to remove a STA info structure, various sta_info_destroy_*() 58 * calls are available. 59 * 60 * There is no concept of ownership on a STA entry; each structure is 61 * owned by the global hash table/list until it is removed. All users of 62 * the structure need to be RCU protected so that the structure won't be 63 * freed before they are done using it. 64 */ 65 66 struct sta_link_alloc { 67 struct link_sta_info info; 68 struct ieee80211_link_sta sta; 69 struct rcu_head rcu_head; 70 }; 71 72 static const struct rhashtable_params sta_rht_params = { 73 .nelem_hint = 3, /* start small */ 74 .automatic_shrinking = true, 75 .head_offset = offsetof(struct sta_info, hash_node), 76 .key_offset = offsetof(struct sta_info, addr), 77 .key_len = ETH_ALEN, 78 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE, 79 }; 80 81 static const struct rhashtable_params link_sta_rht_params = { 82 .nelem_hint = 3, /* start small */ 83 .automatic_shrinking = true, 84 .head_offset = offsetof(struct link_sta_info, link_hash_node), 85 .key_offset = offsetof(struct link_sta_info, addr), 86 .key_len = ETH_ALEN, 87 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE, 88 }; 89 90 static int sta_info_hash_del(struct ieee80211_local *local, 91 struct sta_info *sta) 92 { 93 return rhltable_remove(&local->sta_hash, &sta->hash_node, 94 sta_rht_params); 95 } 96 97 static int link_sta_info_hash_add(struct ieee80211_local *local, 98 struct link_sta_info *link_sta) 99 { 100 lockdep_assert_wiphy(local->hw.wiphy); 101 102 return rhltable_insert(&local->link_sta_hash, 103 &link_sta->link_hash_node, link_sta_rht_params); 104 } 105 106 static int link_sta_info_hash_del(struct ieee80211_local *local, 107 struct link_sta_info *link_sta) 108 { 109 lockdep_assert_wiphy(local->hw.wiphy); 110 111 return rhltable_remove(&local->link_sta_hash, 112 &link_sta->link_hash_node, link_sta_rht_params); 113 } 114 115 void ieee80211_purge_sta_txqs(struct sta_info *sta) 116 { 117 struct ieee80211_local *local = sta->sdata->local; 118 int i; 119 120 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) { 121 struct txq_info *txqi; 122 123 if (!sta->sta.txq[i]) 124 continue; 125 126 txqi = to_txq_info(sta->sta.txq[i]); 127 128 ieee80211_txq_purge(local, txqi); 129 } 130 } 131 132 static void __cleanup_single_sta(struct sta_info *sta) 133 { 134 int ac, i; 135 struct tid_ampdu_tx *tid_tx; 136 struct ieee80211_sub_if_data *sdata = sta->sdata; 137 struct ieee80211_local *local = sdata->local; 138 struct ps_data *ps; 139 140 cancel_work_sync(&sta->drv_deliver_wk); 141 142 if (test_sta_flag(sta, WLAN_STA_PS_STA) || 143 test_sta_flag(sta, WLAN_STA_PS_DRIVER) || 144 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) { 145 if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 146 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 147 ps = &sdata->bss->ps; 148 else if (ieee80211_vif_is_mesh(&sdata->vif)) 149 ps = &sdata->u.mesh.ps; 150 else 151 return; 152 153 clear_sta_flag(sta, WLAN_STA_PS_STA); 154 clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 155 clear_sta_flag(sta, WLAN_STA_PS_DELIVER); 156 157 atomic_dec(&ps->num_sta_ps); 158 } 159 160 ieee80211_purge_sta_txqs(sta); 161 162 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 163 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]); 164 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]); 165 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]); 166 } 167 168 if (ieee80211_vif_is_mesh(&sdata->vif)) 169 mesh_sta_cleanup(sta); 170 171 /* 172 * Destroy aggregation state here. It would be nice to wait for the 173 * driver to finish aggregation stop and then clean up, but for now 174 * drivers have to handle aggregation stop being requested, followed 175 * directly by station destruction. 176 */ 177 for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 178 kfree(sta->ampdu_mlme.tid_start_tx[i]); 179 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]); 180 if (!tid_tx) 181 continue; 182 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending); 183 kfree(tid_tx); 184 } 185 } 186 187 static void cleanup_single_sta(struct sta_info *sta) 188 { 189 struct ieee80211_sub_if_data *sdata = sta->sdata; 190 struct ieee80211_local *local = sdata->local; 191 192 __cleanup_single_sta(sta); 193 sta_info_free(local, sta); 194 } 195 196 struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local, 197 const u8 *addr) 198 { 199 return rhltable_lookup(&local->sta_hash, addr, sta_rht_params); 200 } 201 202 /* protected by RCU */ 203 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata, 204 const u8 *addr) 205 { 206 struct ieee80211_local *local = sdata->local; 207 struct rhlist_head *tmp; 208 struct sta_info *sta; 209 210 rcu_read_lock(); 211 for_each_sta_info(local, addr, sta, tmp) { 212 if (sta->sdata == sdata) { 213 rcu_read_unlock(); 214 /* this is safe as the caller must already hold 215 * another rcu read section or the mutex 216 */ 217 return sta; 218 } 219 } 220 rcu_read_unlock(); 221 return NULL; 222 } 223 224 /* 225 * Get sta info either from the specified interface 226 * or from one of its vlans 227 */ 228 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata, 229 const u8 *addr) 230 { 231 struct ieee80211_local *local = sdata->local; 232 struct rhlist_head *tmp; 233 struct sta_info *sta; 234 235 rcu_read_lock(); 236 for_each_sta_info(local, addr, sta, tmp) { 237 if (sta->sdata == sdata || 238 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) { 239 rcu_read_unlock(); 240 /* this is safe as the caller must already hold 241 * another rcu read section or the mutex 242 */ 243 return sta; 244 } 245 } 246 rcu_read_unlock(); 247 return NULL; 248 } 249 250 struct rhlist_head *link_sta_info_hash_lookup(struct ieee80211_local *local, 251 const u8 *addr) 252 { 253 return rhltable_lookup(&local->link_sta_hash, addr, 254 link_sta_rht_params); 255 } 256 257 struct link_sta_info * 258 link_sta_info_get_bss(struct ieee80211_sub_if_data *sdata, const u8 *addr) 259 { 260 struct ieee80211_local *local = sdata->local; 261 struct rhlist_head *tmp; 262 struct link_sta_info *link_sta; 263 264 rcu_read_lock(); 265 for_each_link_sta_info(local, addr, link_sta, tmp) { 266 struct sta_info *sta = link_sta->sta; 267 268 if (sta->sdata == sdata || 269 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) { 270 rcu_read_unlock(); 271 /* this is safe as the caller must already hold 272 * another rcu read section or the mutex 273 */ 274 return link_sta; 275 } 276 } 277 rcu_read_unlock(); 278 return NULL; 279 } 280 281 struct ieee80211_sta * 282 ieee80211_find_sta_by_link_addrs(struct ieee80211_hw *hw, 283 const u8 *addr, 284 const u8 *localaddr, 285 unsigned int *link_id) 286 { 287 struct ieee80211_local *local = hw_to_local(hw); 288 struct link_sta_info *link_sta; 289 struct rhlist_head *tmp; 290 291 for_each_link_sta_info(local, addr, link_sta, tmp) { 292 struct sta_info *sta = link_sta->sta; 293 struct ieee80211_link_data *link; 294 u8 _link_id = link_sta->link_id; 295 296 if (!localaddr) { 297 if (link_id) 298 *link_id = _link_id; 299 return &sta->sta; 300 } 301 302 link = rcu_dereference(sta->sdata->link[_link_id]); 303 if (!link) 304 continue; 305 306 if (memcmp(link->conf->addr, localaddr, ETH_ALEN)) 307 continue; 308 309 if (link_id) 310 *link_id = _link_id; 311 return &sta->sta; 312 } 313 314 return NULL; 315 } 316 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_link_addrs); 317 318 struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local, 319 const u8 *sta_addr, const u8 *vif_addr) 320 { 321 struct rhlist_head *tmp; 322 struct sta_info *sta; 323 324 for_each_sta_info(local, sta_addr, sta, tmp) { 325 if (ether_addr_equal(vif_addr, sta->sdata->vif.addr)) 326 return sta; 327 } 328 329 return NULL; 330 } 331 332 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, 333 int idx) 334 { 335 struct ieee80211_local *local = sdata->local; 336 struct sta_info *sta; 337 int i = 0; 338 339 list_for_each_entry_rcu(sta, &local->sta_list, list, 340 lockdep_is_held(&local->hw.wiphy->mtx)) { 341 if (sdata != sta->sdata) 342 continue; 343 if (i < idx) { 344 ++i; 345 continue; 346 } 347 return sta; 348 } 349 350 return NULL; 351 } 352 353 static void sta_info_free_link(struct link_sta_info *link_sta) 354 { 355 free_percpu(link_sta->pcpu_rx_stats); 356 } 357 358 static void sta_link_free_rcu(struct rcu_head *head) 359 { 360 struct sta_link_alloc *alloc = 361 container_of(head, struct sta_link_alloc, rcu_head); 362 363 sta_info_free_link(&alloc->info); 364 kfree(alloc); 365 } 366 367 static void sta_accumulate_removed_link_stats(struct sta_info *sta, int link_id) 368 { 369 struct link_sta_info *link_sta = wiphy_dereference(sta->local->hw.wiphy, 370 sta->link[link_id]); 371 struct ieee80211_link_data *link; 372 unsigned int start; 373 int ac, tid; 374 u64 value; 375 u32 thr; 376 377 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 378 sta->rem_link_stats.tx_packets += 379 link_sta->tx_stats.packets[ac]; 380 sta->rem_link_stats.tx_bytes += link_sta->tx_stats.bytes[ac]; 381 } 382 383 do { 384 start = u64_stats_fetch_begin(&link_sta->rx_stats.syncp); 385 value = u64_stats_read(&link_sta->rx_stats.bytes); 386 } while (u64_stats_fetch_retry(&link_sta->rx_stats.syncp, start)); 387 388 sta->rem_link_stats.rx_packets += link_sta->rx_stats.packets; 389 sta->rem_link_stats.rx_bytes += value; 390 sta->rem_link_stats.tx_retries += link_sta->status_stats.retry_count; 391 sta->rem_link_stats.tx_failed += link_sta->status_stats.retry_failed; 392 sta->rem_link_stats.rx_dropped_misc += link_sta->rx_stats.dropped; 393 394 thr = sta_get_expected_throughput(sta); 395 if (thr != 0) 396 sta->rem_link_stats.expected_throughput += thr; 397 398 for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) { 399 do { 400 start = u64_stats_fetch_begin(&link_sta->rx_stats.syncp); 401 value = u64_stats_read(&link_sta->rx_stats.msdu[tid]); 402 } while (u64_stats_fetch_retry(&link_sta->rx_stats.syncp, 403 start)); 404 405 sta->rem_link_stats.pertid_stats.rx_msdu += value; 406 sta->rem_link_stats.pertid_stats.tx_msdu += 407 link_sta->tx_stats.msdu[tid]; 408 sta->rem_link_stats.pertid_stats.tx_msdu_retries += 409 link_sta->status_stats.msdu_retries[tid]; 410 sta->rem_link_stats.pertid_stats.tx_msdu_failed += 411 link_sta->status_stats.msdu_failed[tid]; 412 } 413 414 if (sta->sdata->vif.type == NL80211_IFTYPE_STATION) { 415 link = wiphy_dereference(sta->sdata->local->hw.wiphy, 416 sta->sdata->link[link_id]); 417 if (link) 418 sta->rem_link_stats.beacon_loss_count += 419 link->u.mgd.beacon_loss_count; 420 } 421 } 422 423 static void sta_remove_link(struct sta_info *sta, unsigned int link_id, 424 bool unhash) 425 { 426 struct sta_link_alloc *alloc = NULL; 427 struct link_sta_info *link_sta; 428 429 lockdep_assert_wiphy(sta->local->hw.wiphy); 430 431 link_sta = rcu_access_pointer(sta->link[link_id]); 432 if (WARN_ON(!link_sta)) 433 return; 434 435 if (unhash) 436 link_sta_info_hash_del(sta->local, link_sta); 437 438 if (test_sta_flag(sta, WLAN_STA_INSERTED)) 439 ieee80211_link_sta_debugfs_remove(link_sta); 440 441 if (link_sta != &sta->deflink) 442 alloc = container_of(link_sta, typeof(*alloc), info); 443 444 sta->sta.valid_links &= ~BIT(link_id); 445 446 /* store removed link info for accumulated stats consistency */ 447 sta_accumulate_removed_link_stats(sta, link_id); 448 449 RCU_INIT_POINTER(sta->link[link_id], NULL); 450 RCU_INIT_POINTER(sta->sta.link[link_id], NULL); 451 if (alloc) 452 call_rcu(&alloc->rcu_head, sta_link_free_rcu); 453 454 ieee80211_sta_recalc_aggregates(&sta->sta); 455 } 456 457 /** 458 * sta_info_free - free STA 459 * 460 * @local: pointer to the global information 461 * @sta: STA info to free 462 * 463 * This function must undo everything done by sta_info_alloc() 464 * that may happen before sta_info_insert(). It may only be 465 * called when sta_info_insert() has not been attempted (and 466 * if that fails, the station is freed anyway.) 467 */ 468 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta) 469 { 470 int i; 471 472 for (i = 0; i < ARRAY_SIZE(sta->link); i++) { 473 struct link_sta_info *link_sta; 474 475 link_sta = rcu_access_pointer(sta->link[i]); 476 if (!link_sta) 477 continue; 478 479 sta_remove_link(sta, i, false); 480 } 481 482 /* 483 * If we had used sta_info_pre_move_state() then we might not 484 * have gone through the state transitions down again, so do 485 * it here now (and warn if it's inserted). 486 * 487 * This will clear state such as fast TX/RX that may have been 488 * allocated during state transitions. 489 */ 490 while (sta->sta_state > IEEE80211_STA_NONE) { 491 int ret; 492 493 WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED)); 494 495 ret = sta_info_move_state(sta, sta->sta_state - 1); 496 if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret)) 497 break; 498 } 499 500 if (sta->rate_ctrl) 501 rate_control_free_sta(sta); 502 503 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr); 504 505 kfree(to_txq_info(sta->sta.txq[0])); 506 kfree(rcu_dereference_raw(sta->sta.rates)); 507 #ifdef CONFIG_MAC80211_MESH 508 kfree(sta->mesh); 509 #endif 510 511 sta_info_free_link(&sta->deflink); 512 kfree(sta); 513 } 514 515 static int sta_info_hash_add(struct ieee80211_local *local, 516 struct sta_info *sta) 517 { 518 return rhltable_insert(&local->sta_hash, &sta->hash_node, 519 sta_rht_params); 520 } 521 522 static void sta_deliver_ps_frames(struct work_struct *wk) 523 { 524 struct sta_info *sta; 525 526 sta = container_of(wk, struct sta_info, drv_deliver_wk); 527 528 if (sta->dead) 529 return; 530 531 local_bh_disable(); 532 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) 533 ieee80211_sta_ps_deliver_wakeup(sta); 534 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) 535 ieee80211_sta_ps_deliver_poll_response(sta); 536 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) 537 ieee80211_sta_ps_deliver_uapsd(sta); 538 local_bh_enable(); 539 } 540 541 static int sta_prepare_rate_control(struct ieee80211_local *local, 542 struct sta_info *sta, gfp_t gfp) 543 { 544 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) 545 return 0; 546 547 sta->rate_ctrl = local->rate_ctrl; 548 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, 549 sta, gfp); 550 if (!sta->rate_ctrl_priv) 551 return -ENOMEM; 552 553 return 0; 554 } 555 556 static int sta_info_alloc_link(struct ieee80211_local *local, 557 struct link_sta_info *link_info, 558 gfp_t gfp) 559 { 560 struct ieee80211_hw *hw = &local->hw; 561 int i; 562 563 if (ieee80211_hw_check(hw, USES_RSS)) { 564 link_info->pcpu_rx_stats = 565 alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp); 566 if (!link_info->pcpu_rx_stats) 567 return -ENOMEM; 568 } 569 570 link_info->rx_stats.last_rx = jiffies; 571 u64_stats_init(&link_info->rx_stats.syncp); 572 573 ewma_signal_init(&link_info->rx_stats_avg.signal); 574 ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal); 575 for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++) 576 ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]); 577 578 link_info->rx_omi_bw_rx = IEEE80211_STA_RX_BW_MAX; 579 link_info->rx_omi_bw_tx = IEEE80211_STA_RX_BW_MAX; 580 link_info->rx_omi_bw_staging = IEEE80211_STA_RX_BW_MAX; 581 582 /* 583 * This will always be taken into account, so set to MAX. 584 * When mac80211 is the client on a UHR AP, it'll be used 585 * for the TX side, to limit the bandwidth to TX to the AP 586 * with, to limit to the BSS width during DBE enablement. 587 * 588 * This is needed since the chanreq, which normally has 589 * maximum bandwidth to use with the AP, will already be 590 * set to the DBE width during enablement to prepare for 591 * RX (and not be racy), but the TX can only use higher 592 * bandwidth after enablement finishes. 593 */ 594 link_info->uhr_usable_tx_width = IEEE80211_STA_RX_BW_MAX; 595 596 link_info->op_mode_bw = IEEE80211_STA_RX_BW_MAX; 597 598 /* 599 * Cause (a) warning(s) if IEEE80211_STA_RX_BW_MAX != 320 600 * or if new values are added to the enum. 601 */ 602 switch (link_info->op_mode_bw) { 603 case IEEE80211_STA_RX_BW_20: 604 case IEEE80211_STA_RX_BW_40: 605 case IEEE80211_STA_RX_BW_80: 606 case IEEE80211_STA_RX_BW_160: 607 case IEEE80211_STA_RX_BW_MAX: 608 /* intentionally nothing */ 609 break; 610 } 611 612 return 0; 613 } 614 615 static void sta_info_add_link(struct sta_info *sta, 616 unsigned int link_id, 617 struct link_sta_info *link_info, 618 struct ieee80211_link_sta *link_sta) 619 { 620 link_info->sta = sta; 621 link_info->link_id = link_id; 622 link_info->pub = link_sta; 623 link_info->pub->sta = &sta->sta; 624 link_sta->link_id = link_id; 625 rcu_assign_pointer(sta->link[link_id], link_info); 626 rcu_assign_pointer(sta->sta.link[link_id], link_sta); 627 628 link_sta->smps_mode = IEEE80211_SMPS_OFF; 629 link_sta->agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA; 630 } 631 632 static struct sta_info * 633 __sta_info_alloc(struct ieee80211_sub_if_data *sdata, 634 const u8 *addr, int link_id, const u8 *link_addr, 635 gfp_t gfp) 636 { 637 struct ieee80211_local *local = sdata->local; 638 struct ieee80211_hw *hw = &local->hw; 639 struct sta_info *sta; 640 void *txq_data; 641 int size; 642 int i; 643 644 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp); 645 if (!sta) 646 return NULL; 647 648 sta->local = local; 649 sta->sdata = sdata; 650 651 if (sta_info_alloc_link(local, &sta->deflink, gfp)) 652 goto free; 653 654 if (link_id >= 0) { 655 sta_info_add_link(sta, link_id, &sta->deflink, 656 &sta->sta.deflink); 657 sta->sta.valid_links = BIT(link_id); 658 } else { 659 sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink); 660 } 661 662 sta->sta.cur = &sta->sta.deflink.agg; 663 664 spin_lock_init(&sta->lock); 665 spin_lock_init(&sta->ps_lock); 666 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames); 667 wiphy_work_init(&sta->ampdu_mlme.work, ieee80211_ba_session_work); 668 #ifdef CONFIG_MAC80211_MESH 669 if (ieee80211_vif_is_mesh(&sdata->vif)) { 670 sta->mesh = kzalloc_obj(*sta->mesh, gfp); 671 if (!sta->mesh) 672 goto free; 673 sta->mesh->plink_sta = sta; 674 spin_lock_init(&sta->mesh->plink_lock); 675 if (!sdata->u.mesh.user_mpm) 676 timer_setup(&sta->mesh->plink_timer, mesh_plink_timer, 677 0); 678 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE; 679 } 680 #endif 681 682 memcpy(sta->addr, addr, ETH_ALEN); 683 memcpy(sta->sta.addr, addr, ETH_ALEN); 684 memcpy(sta->deflink.addr, link_addr, ETH_ALEN); 685 memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN); 686 sta->sta.max_rx_aggregation_subframes = 687 local->hw.max_rx_aggregation_subframes; 688 689 /* TODO link specific alloc and assignments for MLO Link STA */ 690 691 /* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only. 692 * The Tx path starts to use a key as soon as the key slot ptk_idx 693 * references to is not NULL. To not use the initial Rx-only key 694 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid 695 * which always will refer to a NULL key. 696 */ 697 BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX); 698 sta->ptk_idx = INVALID_PTK_KEYIDX; 699 700 701 ieee80211_init_frag_cache(&sta->frags); 702 703 sta->sta_state = IEEE80211_STA_NONE; 704 705 if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT) 706 sta->amsdu_mesh_control = -1; 707 708 /* Mark TID as unreserved */ 709 sta->reserved_tid = IEEE80211_TID_UNRESERVED; 710 711 sta->last_connected = ktime_get_seconds(); 712 713 size = sizeof(struct txq_info) + 714 ALIGN(hw->txq_data_size, sizeof(void *)); 715 716 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp); 717 if (!txq_data) 718 goto free; 719 720 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) { 721 struct txq_info *txq = txq_data + i * size; 722 723 /* might not do anything for the (bufferable) MMPDU TXQ */ 724 ieee80211_txq_init(sdata, sta, txq, i); 725 } 726 727 if (sta_prepare_rate_control(local, sta, gfp)) 728 goto free_txq; 729 730 sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT; 731 732 for (i = 0; i < IEEE80211_NUM_ACS; i++) { 733 skb_queue_head_init(&sta->ps_tx_buf[i]); 734 skb_queue_head_init(&sta->tx_filtered[i]); 735 sta->airtime[i].deficit = sta->airtime_weight; 736 atomic_set(&sta->airtime[i].aql_tx_pending, 0); 737 sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i]; 738 sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i]; 739 } 740 741 for (i = 0; i < IEEE80211_NUM_TIDS; i++) 742 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); 743 744 for (i = 0; i < NUM_NL80211_BANDS; i++) { 745 u32 mandatory = 0; 746 int r; 747 748 if (!hw->wiphy->bands[i]) 749 continue; 750 751 switch (i) { 752 case NL80211_BAND_2GHZ: 753 case NL80211_BAND_LC: 754 /* 755 * We use both here, even if we cannot really know for 756 * sure the station will support both, but the only use 757 * for this is when we don't know anything yet and send 758 * management frames, and then we'll pick the lowest 759 * possible rate anyway. 760 * If we don't include _G here, we cannot find a rate 761 * in P2P, and thus trigger the WARN_ONCE() in rate.c 762 */ 763 mandatory = IEEE80211_RATE_MANDATORY_B | 764 IEEE80211_RATE_MANDATORY_G; 765 break; 766 case NL80211_BAND_5GHZ: 767 case NL80211_BAND_6GHZ: 768 mandatory = IEEE80211_RATE_MANDATORY_A; 769 break; 770 case NL80211_BAND_60GHZ: 771 WARN_ON(1); 772 mandatory = 0; 773 break; 774 } 775 776 for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) { 777 struct ieee80211_rate *rate; 778 779 rate = &hw->wiphy->bands[i]->bitrates[r]; 780 781 if (!(rate->flags & mandatory)) 782 continue; 783 sta->sta.deflink.supp_rates[i] |= BIT(r); 784 } 785 } 786 787 788 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr); 789 790 return sta; 791 792 free_txq: 793 kfree(to_txq_info(sta->sta.txq[0])); 794 free: 795 sta_info_free_link(&sta->deflink); 796 #ifdef CONFIG_MAC80211_MESH 797 kfree(sta->mesh); 798 #endif 799 kfree(sta); 800 return NULL; 801 } 802 803 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, 804 const u8 *addr, gfp_t gfp) 805 { 806 return __sta_info_alloc(sdata, addr, -1, addr, gfp); 807 } 808 809 struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata, 810 const u8 *mld_addr, 811 unsigned int link_id, 812 const u8 *link_addr, 813 gfp_t gfp) 814 { 815 return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp); 816 } 817 818 static int sta_info_insert_check(struct sta_info *sta) 819 { 820 struct ieee80211_sub_if_data *sdata = sta->sdata; 821 struct ieee80211_sta *same_addr_sta; 822 823 lockdep_assert_wiphy(sdata->local->hw.wiphy); 824 825 /* 826 * Can't be a WARN_ON because it can be triggered through a race: 827 * something inserts a STA (on one CPU) without holding the RTNL 828 * and another CPU turns off the net device. 829 */ 830 if (unlikely(!ieee80211_sdata_running(sdata))) 831 return -ENETDOWN; 832 833 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) || 834 !is_valid_ether_addr(sta->sta.addr))) 835 return -EINVAL; 836 837 if (!ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR)) 838 return 0; 839 840 /* The RCU read lock is required by rhashtable due to 841 * asynchronous resize/rehash. We also require the mutex 842 * for correctness. 843 */ 844 rcu_read_lock(); 845 same_addr_sta = ieee80211_find_sta_by_ifaddr(&sdata->local->hw, 846 sta->addr, NULL); 847 /* For NAN, a peer can re-use */ 848 if (same_addr_sta && same_addr_sta != rcu_access_pointer(sta->sta.nmi)) { 849 rcu_read_unlock(); 850 return -ENOTUNIQ; 851 } 852 rcu_read_unlock(); 853 854 return 0; 855 } 856 857 static int sta_info_insert_drv_state(struct ieee80211_local *local, 858 struct ieee80211_sub_if_data *sdata, 859 struct sta_info *sta) 860 { 861 enum ieee80211_sta_state state; 862 int err = 0; 863 864 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) { 865 err = drv_sta_state(local, sdata, sta, state, state + 1); 866 if (err) 867 break; 868 } 869 870 if (!err) { 871 /* 872 * Drivers using legacy sta_add/sta_remove callbacks only 873 * get uploaded set to true after sta_add is called. 874 */ 875 if (!local->ops->sta_add) 876 sta->uploaded = true; 877 return 0; 878 } 879 880 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { 881 sdata_info(sdata, 882 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n", 883 sta->sta.addr, state + 1, err); 884 err = 0; 885 } 886 887 /* unwind on error */ 888 for (; state > IEEE80211_STA_NOTEXIST; state--) 889 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1)); 890 891 return err; 892 } 893 894 static void 895 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata) 896 { 897 struct ieee80211_local *local = sdata->local; 898 bool allow_p2p_go_ps = sdata->vif.p2p; 899 struct sta_info *sta; 900 901 rcu_read_lock(); 902 list_for_each_entry_rcu(sta, &local->sta_list, list) { 903 if (sdata != sta->sdata || 904 !test_sta_flag(sta, WLAN_STA_ASSOC)) 905 continue; 906 if (!sta->sta.support_p2p_ps) { 907 allow_p2p_go_ps = false; 908 break; 909 } 910 } 911 rcu_read_unlock(); 912 913 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) { 914 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps; 915 ieee80211_link_info_change_notify(sdata, &sdata->deflink, 916 BSS_CHANGED_P2P_PS); 917 } 918 } 919 920 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU) 921 { 922 struct ieee80211_local *local = sta->local; 923 struct ieee80211_sub_if_data *sdata = sta->sdata; 924 struct station_info *sinfo = NULL; 925 int err = 0; 926 927 lockdep_assert_wiphy(local->hw.wiphy); 928 929 /* check if STA exists already */ 930 if (sta_info_get_bss(sdata, sta->sta.addr)) { 931 err = -EEXIST; 932 goto out_cleanup; 933 } 934 935 sinfo = kzalloc_obj(struct station_info); 936 if (!sinfo) { 937 err = -ENOMEM; 938 goto out_cleanup; 939 } 940 941 local->num_sta++; 942 local->sta_generation++; 943 smp_mb(); 944 945 /* simplify things and don't accept BA sessions yet */ 946 set_sta_flag(sta, WLAN_STA_BLOCK_BA); 947 948 /* make the station visible */ 949 err = sta_info_hash_add(local, sta); 950 if (err) 951 goto out_drop_sta; 952 953 if (sta->sta.valid_links) { 954 err = link_sta_info_hash_add(local, &sta->deflink); 955 if (err) { 956 sta_info_hash_del(local, sta); 957 goto out_drop_sta; 958 } 959 } 960 961 list_add_tail_rcu(&sta->list, &local->sta_list); 962 963 /* update channel context before notifying the driver about state 964 * change, this enables driver using the updated channel context right away. 965 */ 966 if (sta->sta_state >= IEEE80211_STA_ASSOC) { 967 ieee80211_recalc_min_chandef(sta->sdata, -1); 968 if (!sta->sta.support_p2p_ps) 969 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata); 970 } 971 972 /* notify driver */ 973 err = sta_info_insert_drv_state(local, sdata, sta); 974 if (err) 975 goto out_remove; 976 977 set_sta_flag(sta, WLAN_STA_INSERTED); 978 979 /* accept BA sessions now */ 980 clear_sta_flag(sta, WLAN_STA_BLOCK_BA); 981 982 ieee80211_sta_debugfs_add(sta); 983 rate_control_add_sta_debugfs(sta); 984 if (sta->sta.valid_links) { 985 int i; 986 987 for (i = 0; i < ARRAY_SIZE(sta->link); i++) { 988 struct link_sta_info *link_sta; 989 990 link_sta = rcu_dereference_protected(sta->link[i], 991 lockdep_is_held(&local->hw.wiphy->mtx)); 992 993 if (!link_sta) 994 continue; 995 996 ieee80211_link_sta_debugfs_add(link_sta); 997 if (sdata->vif.active_links & BIT(i)) 998 ieee80211_link_sta_debugfs_drv_add(link_sta); 999 } 1000 } else { 1001 ieee80211_link_sta_debugfs_add(&sta->deflink); 1002 ieee80211_link_sta_debugfs_drv_add(&sta->deflink); 1003 } 1004 1005 sinfo->generation = local->sta_generation; 1006 cfg80211_new_sta(&sdata->wdev, sta->sta.addr, sinfo, GFP_KERNEL); 1007 kfree(sinfo); 1008 1009 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr); 1010 1011 /* move reference to rcu-protected */ 1012 rcu_read_lock(); 1013 1014 if (ieee80211_vif_is_mesh(&sdata->vif)) 1015 mesh_accept_plinks_update(sdata); 1016 1017 ieee80211_check_fast_xmit(sta); 1018 1019 return 0; 1020 out_remove: 1021 if (sta->sta.valid_links) 1022 link_sta_info_hash_del(local, &sta->deflink); 1023 sta_info_hash_del(local, sta); 1024 list_del_rcu(&sta->list); 1025 out_drop_sta: 1026 local->num_sta--; 1027 synchronize_net(); 1028 out_cleanup: 1029 cleanup_single_sta(sta); 1030 kfree(sinfo); 1031 rcu_read_lock(); 1032 return err; 1033 } 1034 1035 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) 1036 { 1037 struct ieee80211_local *local = sta->local; 1038 int err; 1039 1040 might_sleep(); 1041 lockdep_assert_wiphy(local->hw.wiphy); 1042 1043 err = sta_info_insert_check(sta); 1044 if (err) { 1045 sta_info_free(local, sta); 1046 rcu_read_lock(); 1047 return err; 1048 } 1049 1050 return sta_info_insert_finish(sta); 1051 } 1052 1053 int sta_info_insert(struct sta_info *sta) 1054 { 1055 int err = sta_info_insert_rcu(sta); 1056 1057 rcu_read_unlock(); 1058 1059 return err; 1060 } 1061 1062 static inline void __bss_tim_set(u8 *tim, u16 id) 1063 { 1064 /* 1065 * This format has been mandated by the IEEE specifications, 1066 * so this line may not be changed to use the __set_bit() format. 1067 */ 1068 tim[id / 8] |= (1 << (id % 8)); 1069 } 1070 1071 static inline void __bss_tim_clear(u8 *tim, u16 id) 1072 { 1073 /* 1074 * This format has been mandated by the IEEE specifications, 1075 * so this line may not be changed to use the __clear_bit() format. 1076 */ 1077 tim[id / 8] &= ~(1 << (id % 8)); 1078 } 1079 1080 static inline bool __bss_tim_get(u8 *tim, u16 id) 1081 { 1082 /* 1083 * This format has been mandated by the IEEE specifications, 1084 * so this line may not be changed to use the test_bit() format. 1085 */ 1086 return tim[id / 8] & (1 << (id % 8)); 1087 } 1088 1089 static unsigned long ieee80211_tids_for_ac(int ac) 1090 { 1091 /* If we ever support TIDs > 7, this obviously needs to be adjusted */ 1092 switch (ac) { 1093 case IEEE80211_AC_VO: 1094 return BIT(6) | BIT(7); 1095 case IEEE80211_AC_VI: 1096 return BIT(4) | BIT(5); 1097 case IEEE80211_AC_BE: 1098 return BIT(0) | BIT(3); 1099 case IEEE80211_AC_BK: 1100 return BIT(1) | BIT(2); 1101 default: 1102 WARN_ON(1); 1103 return 0; 1104 } 1105 } 1106 1107 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending) 1108 { 1109 struct ieee80211_local *local = sta->local; 1110 struct ps_data *ps; 1111 bool indicate_tim = false; 1112 u8 ignore_for_tim = sta->sta.uapsd_queues; 1113 int ac; 1114 u16 id = sta->sta.aid; 1115 1116 if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 1117 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 1118 if (WARN_ON_ONCE(!sta->sdata->bss)) 1119 return; 1120 1121 ps = &sta->sdata->bss->ps; 1122 #ifdef CONFIG_MAC80211_MESH 1123 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) { 1124 ps = &sta->sdata->u.mesh.ps; 1125 #endif 1126 } else { 1127 return; 1128 } 1129 1130 /* No need to do anything if the driver does all */ 1131 if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim) 1132 return; 1133 1134 if (sta->dead) 1135 goto done; 1136 1137 /* 1138 * If all ACs are delivery-enabled then we should build 1139 * the TIM bit for all ACs anyway; if only some are then 1140 * we ignore those and build the TIM bit using only the 1141 * non-enabled ones. 1142 */ 1143 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1) 1144 ignore_for_tim = 0; 1145 1146 if (ignore_pending) 1147 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1; 1148 1149 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1150 unsigned long tids; 1151 1152 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac]) 1153 continue; 1154 1155 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) || 1156 !skb_queue_empty(&sta->ps_tx_buf[ac]); 1157 if (indicate_tim) 1158 break; 1159 1160 tids = ieee80211_tids_for_ac(ac); 1161 1162 indicate_tim |= 1163 sta->driver_buffered_tids & tids; 1164 indicate_tim |= 1165 sta->txq_buffered_tids & tids; 1166 } 1167 1168 done: 1169 spin_lock_bh(&local->tim_lock); 1170 1171 if (indicate_tim == __bss_tim_get(ps->tim, id)) 1172 goto out_unlock; 1173 1174 if (indicate_tim) 1175 __bss_tim_set(ps->tim, id); 1176 else 1177 __bss_tim_clear(ps->tim, id); 1178 1179 if (local->ops->set_tim && !WARN_ON(sta->dead)) { 1180 local->tim_in_locked_section = true; 1181 drv_set_tim(local, &sta->sta, indicate_tim); 1182 local->tim_in_locked_section = false; 1183 } 1184 1185 out_unlock: 1186 spin_unlock_bh(&local->tim_lock); 1187 } 1188 1189 void sta_info_recalc_tim(struct sta_info *sta) 1190 { 1191 __sta_info_recalc_tim(sta, false); 1192 } 1193 1194 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb) 1195 { 1196 struct ieee80211_tx_info *info; 1197 int timeout; 1198 1199 if (!skb) 1200 return false; 1201 1202 info = IEEE80211_SKB_CB(skb); 1203 1204 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 1205 timeout = (sta->listen_interval * 1206 sta->sdata->vif.bss_conf.beacon_int * 1207 32 / 15625) * HZ; 1208 if (timeout < STA_TX_BUFFER_EXPIRE) 1209 timeout = STA_TX_BUFFER_EXPIRE; 1210 return time_after(jiffies, info->control.jiffies + timeout); 1211 } 1212 1213 1214 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local, 1215 struct sta_info *sta, int ac) 1216 { 1217 unsigned long flags; 1218 struct sk_buff *skb; 1219 1220 /* 1221 * First check for frames that should expire on the filtered 1222 * queue. Frames here were rejected by the driver and are on 1223 * a separate queue to avoid reordering with normal PS-buffered 1224 * frames. They also aren't accounted for right now in the 1225 * total_ps_buffered counter. 1226 */ 1227 for (;;) { 1228 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 1229 skb = skb_peek(&sta->tx_filtered[ac]); 1230 if (sta_info_buffer_expired(sta, skb)) 1231 skb = __skb_dequeue(&sta->tx_filtered[ac]); 1232 else 1233 skb = NULL; 1234 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 1235 1236 /* 1237 * Frames are queued in order, so if this one 1238 * hasn't expired yet we can stop testing. If 1239 * we actually reached the end of the queue we 1240 * also need to stop, of course. 1241 */ 1242 if (!skb) 1243 break; 1244 ieee80211_free_txskb(&local->hw, skb); 1245 } 1246 1247 /* 1248 * Now also check the normal PS-buffered queue, this will 1249 * only find something if the filtered queue was emptied 1250 * since the filtered frames are all before the normal PS 1251 * buffered frames. 1252 */ 1253 for (;;) { 1254 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 1255 skb = skb_peek(&sta->ps_tx_buf[ac]); 1256 if (sta_info_buffer_expired(sta, skb)) 1257 skb = __skb_dequeue(&sta->ps_tx_buf[ac]); 1258 else 1259 skb = NULL; 1260 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 1261 1262 /* 1263 * frames are queued in order, so if this one 1264 * hasn't expired yet (or we reached the end of 1265 * the queue) we can stop testing 1266 */ 1267 if (!skb) 1268 break; 1269 1270 local->total_ps_buffered--; 1271 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n", 1272 sta->sta.addr); 1273 ieee80211_free_txskb(&local->hw, skb); 1274 } 1275 1276 /* 1277 * Finally, recalculate the TIM bit for this station -- it might 1278 * now be clear because the station was too slow to retrieve its 1279 * frames. 1280 */ 1281 sta_info_recalc_tim(sta); 1282 1283 /* 1284 * Return whether there are any frames still buffered, this is 1285 * used to check whether the cleanup timer still needs to run, 1286 * if there are no frames we don't need to rearm the timer. 1287 */ 1288 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) && 1289 skb_queue_empty(&sta->tx_filtered[ac])); 1290 } 1291 1292 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 1293 struct sta_info *sta) 1294 { 1295 bool have_buffered = false; 1296 int ac; 1297 1298 /* This is only necessary for stations on BSS/MBSS interfaces */ 1299 if (!sta->sdata->bss && 1300 !ieee80211_vif_is_mesh(&sta->sdata->vif)) 1301 return false; 1302 1303 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 1304 have_buffered |= 1305 sta_info_cleanup_expire_buffered_ac(local, sta, ac); 1306 1307 return have_buffered; 1308 } 1309 1310 static int __must_check __sta_info_destroy_part1(struct sta_info *sta) 1311 { 1312 struct ieee80211_local *local; 1313 struct ieee80211_sub_if_data *sdata; 1314 int ret, i; 1315 1316 might_sleep(); 1317 1318 if (!sta) 1319 return -ENOENT; 1320 1321 local = sta->local; 1322 sdata = sta->sdata; 1323 1324 lockdep_assert_wiphy(local->hw.wiphy); 1325 1326 if (sdata->vif.type == NL80211_IFTYPE_NAN) { 1327 struct sta_info *sta_iter, *tmp; 1328 1329 /* Remove all NDI stations associated with this NMI STA */ 1330 list_for_each_entry_safe(sta_iter, tmp, &local->sta_list, list) { 1331 if (rcu_access_pointer(sta_iter->sta.nmi) != &sta->sta) 1332 continue; 1333 sta_info_destroy_addr(sta_iter->sdata, sta_iter->addr); 1334 } 1335 1336 /* Free and clear the local peer schedule */ 1337 ieee80211_nan_free_peer_sched(sta->sta.nan_sched); 1338 sta->sta.nan_sched = NULL; 1339 } 1340 1341 /* 1342 * Before removing the station from the driver and 1343 * rate control, it might still start new aggregation 1344 * sessions -- block that to make sure the tear-down 1345 * will be sufficient. 1346 */ 1347 set_sta_flag(sta, WLAN_STA_BLOCK_BA); 1348 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA); 1349 1350 /* 1351 * Before removing the station from the driver there might be pending 1352 * rx frames on RSS queues sent prior to the disassociation - wait for 1353 * all such frames to be processed. 1354 */ 1355 drv_sync_rx_queues(local, sta); 1356 1357 for (i = 0; i < ARRAY_SIZE(sta->link); i++) { 1358 struct link_sta_info *link_sta; 1359 1360 if (!(sta->sta.valid_links & BIT(i))) 1361 continue; 1362 1363 link_sta = rcu_dereference_protected(sta->link[i], 1364 lockdep_is_held(&local->hw.wiphy->mtx)); 1365 1366 link_sta_info_hash_del(local, link_sta); 1367 } 1368 1369 ret = sta_info_hash_del(local, sta); 1370 if (WARN_ON(ret)) 1371 return ret; 1372 1373 /* 1374 * for TDLS peers, make sure to return to the base channel before 1375 * removal. 1376 */ 1377 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) { 1378 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta); 1379 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL); 1380 } 1381 1382 list_del_rcu(&sta->list); 1383 sta->removed = true; 1384 1385 if (sta->uploaded) 1386 drv_sta_pre_rcu_remove(local, sta->sdata, sta); 1387 1388 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 1389 rcu_access_pointer(sdata->u.vlan.sta) == sta) 1390 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL); 1391 1392 return 0; 1393 } 1394 1395 static int _sta_info_move_state(struct sta_info *sta, 1396 enum ieee80211_sta_state new_state, 1397 bool recalc) 1398 { 1399 struct ieee80211_local *local = sta->local; 1400 1401 might_sleep(); 1402 1403 if (sta->sta_state == new_state) 1404 return 0; 1405 1406 /* check allowed transitions first */ 1407 1408 switch (new_state) { 1409 case IEEE80211_STA_NONE: 1410 if (sta->sta_state != IEEE80211_STA_AUTH) 1411 return -EINVAL; 1412 break; 1413 case IEEE80211_STA_AUTH: 1414 if (sta->sta_state != IEEE80211_STA_NONE && 1415 sta->sta_state != IEEE80211_STA_ASSOC) 1416 return -EINVAL; 1417 break; 1418 case IEEE80211_STA_ASSOC: 1419 if (sta->sta_state != IEEE80211_STA_AUTH && 1420 sta->sta_state != IEEE80211_STA_AUTHORIZED) 1421 return -EINVAL; 1422 break; 1423 case IEEE80211_STA_AUTHORIZED: 1424 if (sta->sta_state != IEEE80211_STA_ASSOC) 1425 return -EINVAL; 1426 break; 1427 default: 1428 WARN(1, "invalid state %d", new_state); 1429 return -EINVAL; 1430 } 1431 1432 sta_dbg(sta->sdata, "moving STA %pM to state %d\n", 1433 sta->sta.addr, new_state); 1434 1435 /* notify the driver before the actual changes so it can 1436 * fail the transition if the state is increasing. 1437 * The driver is required not to fail when the transition 1438 * is decreasing the state, so first, do all the preparation 1439 * work and only then, notify the driver. 1440 */ 1441 if (new_state > sta->sta_state && 1442 test_sta_flag(sta, WLAN_STA_INSERTED)) { 1443 int err = drv_sta_state(sta->local, sta->sdata, sta, 1444 sta->sta_state, new_state); 1445 if (err) 1446 return err; 1447 } 1448 1449 /* reflect the change in all state variables */ 1450 1451 switch (new_state) { 1452 case IEEE80211_STA_NONE: 1453 if (sta->sta_state == IEEE80211_STA_AUTH) 1454 clear_bit(WLAN_STA_AUTH, &sta->_flags); 1455 break; 1456 case IEEE80211_STA_AUTH: 1457 if (sta->sta_state == IEEE80211_STA_NONE) { 1458 set_bit(WLAN_STA_AUTH, &sta->_flags); 1459 } else if (sta->sta_state == IEEE80211_STA_ASSOC) { 1460 clear_bit(WLAN_STA_ASSOC, &sta->_flags); 1461 if (recalc) { 1462 ieee80211_recalc_min_chandef(sta->sdata, -1); 1463 if (!sta->sta.support_p2p_ps) 1464 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata); 1465 } 1466 } 1467 break; 1468 case IEEE80211_STA_ASSOC: 1469 if (sta->sta_state == IEEE80211_STA_AUTH) { 1470 set_bit(WLAN_STA_ASSOC, &sta->_flags); 1471 sta->assoc_at = ktime_get_boottime_ns(); 1472 if (recalc) { 1473 ieee80211_recalc_min_chandef(sta->sdata, -1); 1474 if (!sta->sta.support_p2p_ps) 1475 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata); 1476 } 1477 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) { 1478 ieee80211_vif_dec_num_mcast(sta->sdata); 1479 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 1480 if (sta->sdata->vif.type == NL80211_IFTYPE_NAN_DATA) 1481 ieee80211_nan_update_ndi_carrier(sta->sdata); 1482 1483 /* 1484 * If we have encryption offload, flush (station) queues 1485 * (after ensuring concurrent TX completed) so we won't 1486 * transmit anything later unencrypted if/when keys are 1487 * also removed, which might otherwise happen depending 1488 * on how the hardware offload works. 1489 */ 1490 if (local->ops->set_key) { 1491 synchronize_net(); 1492 if (local->ops->flush_sta) 1493 drv_flush_sta(local, sta->sdata, sta); 1494 else 1495 ieee80211_flush_queues(local, 1496 sta->sdata, 1497 false); 1498 } 1499 1500 ieee80211_clear_fast_xmit(sta); 1501 ieee80211_clear_fast_rx(sta); 1502 } 1503 break; 1504 case IEEE80211_STA_AUTHORIZED: 1505 if (sta->sta_state == IEEE80211_STA_ASSOC) { 1506 ieee80211_vif_inc_num_mcast(sta->sdata); 1507 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 1508 ieee80211_check_fast_xmit(sta); 1509 ieee80211_check_fast_rx(sta); 1510 if (sta->sdata->vif.type == NL80211_IFTYPE_NAN_DATA) 1511 ieee80211_nan_update_ndi_carrier(sta->sdata); 1512 } 1513 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN || 1514 sta->sdata->vif.type == NL80211_IFTYPE_AP) 1515 cfg80211_send_layer2_update(sta->sdata->dev, 1516 sta->sta.addr); 1517 break; 1518 default: 1519 break; 1520 } 1521 1522 if (new_state < sta->sta_state && 1523 test_sta_flag(sta, WLAN_STA_INSERTED)) { 1524 int err = drv_sta_state(sta->local, sta->sdata, sta, 1525 sta->sta_state, new_state); 1526 1527 WARN_ONCE(err, 1528 "Driver is not allowed to fail if the sta_state is transitioning down the list: %d\n", 1529 err); 1530 } 1531 1532 sta->sta_state = new_state; 1533 1534 return 0; 1535 } 1536 1537 int sta_info_move_state(struct sta_info *sta, 1538 enum ieee80211_sta_state new_state) 1539 { 1540 return _sta_info_move_state(sta, new_state, true); 1541 } 1542 1543 static void __sta_info_destroy_part2(struct sta_info *sta, bool recalc) 1544 { 1545 struct ieee80211_local *local = sta->local; 1546 struct ieee80211_sub_if_data *sdata = sta->sdata; 1547 struct station_info *sinfo; 1548 int ret; 1549 1550 /* 1551 * NOTE: This assumes at least synchronize_net() was done 1552 * after _part1 and before _part2! 1553 */ 1554 1555 /* 1556 * There's a potential race in _part1 where we set WLAN_STA_BLOCK_BA 1557 * but someone might have just gotten past a check, and not yet into 1558 * queuing the work/creating the data/etc. 1559 * 1560 * Do another round of destruction so that the worker is certainly 1561 * canceled before we later free the station. 1562 * 1563 * Since this is after synchronize_rcu()/synchronize_net() we're now 1564 * certain that nobody can actually hold a reference to the STA and 1565 * be calling e.g. ieee80211_start_tx_ba_session(). 1566 */ 1567 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA); 1568 1569 might_sleep(); 1570 lockdep_assert_wiphy(local->hw.wiphy); 1571 1572 if (sta->sta_state == IEEE80211_STA_AUTHORIZED) { 1573 ret = _sta_info_move_state(sta, IEEE80211_STA_ASSOC, recalc); 1574 WARN_ON_ONCE(ret); 1575 } 1576 1577 /* now keys can no longer be reached */ 1578 ieee80211_free_sta_keys(local, sta); 1579 1580 /* disable TIM bit - last chance to tell driver */ 1581 __sta_info_recalc_tim(sta, true); 1582 1583 sta->dead = true; 1584 1585 cancel_work_sync(&sta->drv_deliver_wk); 1586 1587 local->num_sta--; 1588 local->sta_generation++; 1589 1590 while (sta->sta_state > IEEE80211_STA_NONE) { 1591 ret = _sta_info_move_state(sta, sta->sta_state - 1, recalc); 1592 if (ret) { 1593 WARN_ON_ONCE(1); 1594 break; 1595 } 1596 } 1597 1598 sinfo = kzalloc_obj(*sinfo); 1599 if (sinfo) 1600 sta_set_sinfo(sta, sinfo, true); 1601 1602 if (sta->uploaded) { 1603 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE, 1604 IEEE80211_STA_NOTEXIST); 1605 WARN_ON_ONCE(ret != 0); 1606 } 1607 1608 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr); 1609 1610 cfg80211_del_sta_sinfo(&sdata->wdev, sta->sta.addr, sinfo, GFP_KERNEL); 1611 kfree(sinfo); 1612 1613 ieee80211_sta_debugfs_remove(sta); 1614 1615 ieee80211_destroy_frag_cache(&sta->frags); 1616 1617 cleanup_single_sta(sta); 1618 } 1619 1620 int __must_check __sta_info_destroy(struct sta_info *sta) 1621 { 1622 int err = __sta_info_destroy_part1(sta); 1623 1624 if (err) 1625 return err; 1626 1627 synchronize_net(); 1628 1629 __sta_info_destroy_part2(sta, true); 1630 1631 return 0; 1632 } 1633 1634 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) 1635 { 1636 struct sta_info *sta; 1637 1638 lockdep_assert_wiphy(sdata->local->hw.wiphy); 1639 1640 sta = sta_info_get(sdata, addr); 1641 return __sta_info_destroy(sta); 1642 } 1643 1644 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, 1645 const u8 *addr) 1646 { 1647 struct sta_info *sta; 1648 1649 lockdep_assert_wiphy(sdata->local->hw.wiphy); 1650 1651 sta = sta_info_get_bss(sdata, addr); 1652 return __sta_info_destroy(sta); 1653 } 1654 1655 static void sta_info_cleanup(struct timer_list *t) 1656 { 1657 struct ieee80211_local *local = timer_container_of(local, t, 1658 sta_cleanup); 1659 struct sta_info *sta; 1660 bool timer_needed = false; 1661 1662 rcu_read_lock(); 1663 list_for_each_entry_rcu(sta, &local->sta_list, list) 1664 if (sta_info_cleanup_expire_buffered(local, sta)) 1665 timer_needed = true; 1666 rcu_read_unlock(); 1667 1668 if (local->quiescing) 1669 return; 1670 1671 if (!timer_needed) 1672 return; 1673 1674 mod_timer(&local->sta_cleanup, 1675 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 1676 } 1677 1678 int sta_info_init(struct ieee80211_local *local) 1679 { 1680 int err; 1681 1682 err = rhltable_init(&local->sta_hash, &sta_rht_params); 1683 if (err) 1684 return err; 1685 1686 err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params); 1687 if (err) { 1688 rhltable_destroy(&local->sta_hash); 1689 return err; 1690 } 1691 1692 spin_lock_init(&local->tim_lock); 1693 INIT_LIST_HEAD(&local->sta_list); 1694 1695 timer_setup(&local->sta_cleanup, sta_info_cleanup, 0); 1696 return 0; 1697 } 1698 1699 void sta_info_stop(struct ieee80211_local *local) 1700 { 1701 timer_delete_sync(&local->sta_cleanup); 1702 rhltable_destroy(&local->sta_hash); 1703 rhltable_destroy(&local->link_sta_hash); 1704 } 1705 1706 1707 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans, 1708 int link_id, struct sta_info *do_not_flush_sta) 1709 { 1710 struct ieee80211_local *local = sdata->local; 1711 struct sta_info *sta, *tmp; 1712 LIST_HEAD(free_list); 1713 int ret = 0; 1714 1715 might_sleep(); 1716 lockdep_assert_wiphy(local->hw.wiphy); 1717 1718 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP); 1719 WARN_ON(vlans && !sdata->bss); 1720 1721 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 1722 if (sdata != sta->sdata && 1723 (!vlans || sdata->bss != sta->sdata->bss)) 1724 continue; 1725 1726 if (sta == do_not_flush_sta) 1727 continue; 1728 1729 if (link_id >= 0 && sta->sta.valid_links && 1730 !(sta->sta.valid_links & BIT(link_id))) 1731 continue; 1732 1733 if (!WARN_ON(__sta_info_destroy_part1(sta))) 1734 list_add(&sta->free_list, &free_list); 1735 1736 ret++; 1737 } 1738 1739 if (!list_empty(&free_list)) { 1740 bool support_p2p_ps = true; 1741 1742 synchronize_net(); 1743 list_for_each_entry_safe(sta, tmp, &free_list, free_list) { 1744 if (!sta->sta.support_p2p_ps) 1745 support_p2p_ps = false; 1746 __sta_info_destroy_part2(sta, false); 1747 } 1748 1749 ieee80211_recalc_min_chandef(sdata, -1); 1750 if (!support_p2p_ps) 1751 ieee80211_recalc_p2p_go_ps_allowed(sdata); 1752 } 1753 1754 return ret; 1755 } 1756 1757 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 1758 unsigned long exp_time) 1759 { 1760 struct ieee80211_local *local = sdata->local; 1761 struct sta_info *sta, *tmp; 1762 1763 lockdep_assert_wiphy(local->hw.wiphy); 1764 1765 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 1766 unsigned long last_active = ieee80211_sta_last_active(sta, -1); 1767 1768 if (sdata != sta->sdata) 1769 continue; 1770 1771 if (time_is_before_jiffies(last_active + exp_time)) { 1772 sta_dbg(sta->sdata, "expiring inactive STA %pM\n", 1773 sta->sta.addr); 1774 1775 if (ieee80211_vif_is_mesh(&sdata->vif) && 1776 test_sta_flag(sta, WLAN_STA_PS_STA)) 1777 atomic_dec(&sdata->u.mesh.ps.num_sta_ps); 1778 1779 WARN_ON(__sta_info_destroy(sta)); 1780 } 1781 } 1782 } 1783 1784 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 1785 const u8 *addr, 1786 const u8 *localaddr) 1787 { 1788 struct ieee80211_local *local = hw_to_local(hw); 1789 struct rhlist_head *tmp; 1790 struct sta_info *sta; 1791 1792 /* 1793 * Just return a random station if localaddr is NULL 1794 * ... first in list. 1795 */ 1796 for_each_sta_info(local, addr, sta, tmp) { 1797 if (localaddr && 1798 !ether_addr_equal(sta->sdata->vif.addr, localaddr)) 1799 continue; 1800 if (!sta->uploaded) 1801 return NULL; 1802 return &sta->sta; 1803 } 1804 1805 return NULL; 1806 } 1807 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); 1808 1809 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, 1810 const u8 *addr) 1811 { 1812 struct sta_info *sta; 1813 1814 if (!vif) 1815 return NULL; 1816 1817 sta = sta_info_get_bss(vif_to_sdata(vif), addr); 1818 if (!sta) 1819 return NULL; 1820 1821 if (!sta->uploaded) 1822 return NULL; 1823 1824 return &sta->sta; 1825 } 1826 EXPORT_SYMBOL(ieee80211_find_sta); 1827 1828 /* powersave support code */ 1829 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) 1830 { 1831 struct ieee80211_sub_if_data *sdata = sta->sdata; 1832 struct ieee80211_local *local = sdata->local; 1833 struct sk_buff_head pending; 1834 int filtered = 0, buffered = 0, ac, i; 1835 unsigned long flags; 1836 struct ps_data *ps; 1837 1838 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 1839 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data, 1840 u.ap); 1841 1842 if (sdata->vif.type == NL80211_IFTYPE_AP) 1843 ps = &sdata->bss->ps; 1844 else if (ieee80211_vif_is_mesh(&sdata->vif)) 1845 ps = &sdata->u.mesh.ps; 1846 else 1847 return; 1848 1849 clear_sta_flag(sta, WLAN_STA_SP); 1850 1851 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1); 1852 sta->driver_buffered_tids = 0; 1853 sta->txq_buffered_tids = 0; 1854 1855 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS)) 1856 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 1857 1858 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) { 1859 if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i])) 1860 continue; 1861 1862 schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i])); 1863 } 1864 1865 skb_queue_head_init(&pending); 1866 1867 /* sync with ieee80211_tx_h_unicast_ps_buf */ 1868 spin_lock_bh(&sta->ps_lock); 1869 /* Send all buffered frames to the station */ 1870 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1871 int count = skb_queue_len(&pending), tmp; 1872 1873 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 1874 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending); 1875 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 1876 tmp = skb_queue_len(&pending); 1877 filtered += tmp - count; 1878 count = tmp; 1879 1880 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 1881 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending); 1882 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 1883 tmp = skb_queue_len(&pending); 1884 buffered += tmp - count; 1885 } 1886 1887 ieee80211_add_pending_skbs(local, &pending); 1888 1889 /* now we're no longer in the deliver code */ 1890 clear_sta_flag(sta, WLAN_STA_PS_DELIVER); 1891 1892 /* The station might have polled and then woken up before we responded, 1893 * so clear these flags now to avoid them sticking around. 1894 */ 1895 clear_sta_flag(sta, WLAN_STA_PSPOLL); 1896 clear_sta_flag(sta, WLAN_STA_UAPSD); 1897 spin_unlock_bh(&sta->ps_lock); 1898 1899 atomic_dec(&ps->num_sta_ps); 1900 1901 local->total_ps_buffered -= buffered; 1902 1903 sta_info_recalc_tim(sta); 1904 1905 ps_dbg(sdata, 1906 "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n", 1907 sta->sta.addr, sta->sta.aid, filtered, buffered); 1908 1909 ieee80211_check_fast_xmit(sta); 1910 } 1911 1912 static void ieee80211_send_null_response(struct sta_info *sta, int tid, 1913 enum ieee80211_frame_release_type reason, 1914 bool call_driver, bool more_data) 1915 { 1916 struct ieee80211_sub_if_data *sdata = sta->sdata; 1917 struct ieee80211_local *local = sdata->local; 1918 struct ieee80211_qos_hdr *nullfunc; 1919 struct sk_buff *skb; 1920 int size = sizeof(*nullfunc); 1921 __le16 fc; 1922 bool qos = sta->sta.wme; 1923 struct ieee80211_tx_info *info; 1924 struct ieee80211_chanctx_conf *chanctx_conf; 1925 1926 if (qos) { 1927 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1928 IEEE80211_STYPE_QOS_NULLFUNC | 1929 IEEE80211_FCTL_FROMDS); 1930 } else { 1931 size -= 2; 1932 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1933 IEEE80211_STYPE_NULLFUNC | 1934 IEEE80211_FCTL_FROMDS); 1935 } 1936 1937 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size); 1938 if (!skb) 1939 return; 1940 1941 skb_reserve(skb, local->hw.extra_tx_headroom); 1942 1943 nullfunc = skb_put(skb, size); 1944 nullfunc->frame_control = fc; 1945 nullfunc->duration_id = 0; 1946 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN); 1947 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN); 1948 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN); 1949 nullfunc->seq_ctrl = 0; 1950 1951 skb->priority = tid; 1952 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]); 1953 if (qos) { 1954 nullfunc->qos_ctrl = cpu_to_le16(tid); 1955 1956 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) { 1957 nullfunc->qos_ctrl |= 1958 cpu_to_le16(IEEE80211_QOS_CTL_EOSP); 1959 if (more_data) 1960 nullfunc->frame_control |= 1961 cpu_to_le16(IEEE80211_FCTL_MOREDATA); 1962 } 1963 } 1964 1965 info = IEEE80211_SKB_CB(skb); 1966 1967 /* 1968 * Tell TX path to send this frame even though the 1969 * STA may still remain is PS mode after this frame 1970 * exchange. Also set EOSP to indicate this packet 1971 * ends the poll/service period. 1972 */ 1973 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER | 1974 IEEE80211_TX_STATUS_EOSP | 1975 IEEE80211_TX_CTL_REQ_TX_STATUS; 1976 1977 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE; 1978 1979 if (call_driver) 1980 drv_allow_buffered_frames(local, sta, BIT(tid), 1, 1981 reason, false); 1982 1983 skb->dev = sdata->dev; 1984 1985 rcu_read_lock(); 1986 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf); 1987 if (WARN_ON(!chanctx_conf)) { 1988 rcu_read_unlock(); 1989 kfree_skb(skb); 1990 return; 1991 } 1992 1993 info->band = chanctx_conf->def.chan->band; 1994 ieee80211_xmit(sdata, sta, skb); 1995 rcu_read_unlock(); 1996 } 1997 1998 static int find_highest_prio_tid(unsigned long tids) 1999 { 2000 /* lower 3 TIDs aren't ordered perfectly */ 2001 if (tids & 0xF8) 2002 return fls(tids) - 1; 2003 /* TID 0 is BE just like TID 3 */ 2004 if (tids & BIT(0)) 2005 return 0; 2006 return fls(tids) - 1; 2007 } 2008 2009 /* Indicates if the MORE_DATA bit should be set in the last 2010 * frame obtained by ieee80211_sta_ps_get_frames. 2011 * Note that driver_release_tids is relevant only if 2012 * reason = IEEE80211_FRAME_RELEASE_PSPOLL 2013 */ 2014 static bool 2015 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs, 2016 enum ieee80211_frame_release_type reason, 2017 unsigned long driver_release_tids) 2018 { 2019 int ac; 2020 2021 /* If the driver has data on more than one TID then 2022 * certainly there's more data if we release just a 2023 * single frame now (from a single TID). This will 2024 * only happen for PS-Poll. 2025 */ 2026 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL && 2027 hweight16(driver_release_tids) > 1) 2028 return true; 2029 2030 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 2031 if (ignored_acs & ieee80211_ac_to_qos_mask[ac]) 2032 continue; 2033 2034 if (!skb_queue_empty(&sta->tx_filtered[ac]) || 2035 !skb_queue_empty(&sta->ps_tx_buf[ac])) 2036 return true; 2037 } 2038 2039 return false; 2040 } 2041 2042 static void 2043 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs, 2044 enum ieee80211_frame_release_type reason, 2045 struct sk_buff_head *frames, 2046 unsigned long *driver_release_tids) 2047 { 2048 struct ieee80211_sub_if_data *sdata = sta->sdata; 2049 struct ieee80211_local *local = sdata->local; 2050 int ac; 2051 2052 /* Get response frame(s) and more data bit for the last one. */ 2053 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 2054 unsigned long tids; 2055 2056 if (ignored_acs & ieee80211_ac_to_qos_mask[ac]) 2057 continue; 2058 2059 tids = ieee80211_tids_for_ac(ac); 2060 2061 /* if we already have frames from software, then we can't also 2062 * release from hardware queues 2063 */ 2064 if (skb_queue_empty(frames)) { 2065 *driver_release_tids |= 2066 sta->driver_buffered_tids & tids; 2067 *driver_release_tids |= sta->txq_buffered_tids & tids; 2068 } 2069 2070 if (!*driver_release_tids) { 2071 struct sk_buff *skb; 2072 2073 while (n_frames > 0) { 2074 skb = skb_dequeue(&sta->tx_filtered[ac]); 2075 if (!skb) { 2076 skb = skb_dequeue( 2077 &sta->ps_tx_buf[ac]); 2078 if (skb) 2079 local->total_ps_buffered--; 2080 } 2081 if (!skb) 2082 break; 2083 n_frames--; 2084 __skb_queue_tail(frames, skb); 2085 } 2086 } 2087 2088 /* If we have more frames buffered on this AC, then abort the 2089 * loop since we can't send more data from other ACs before 2090 * the buffered frames from this. 2091 */ 2092 if (!skb_queue_empty(&sta->tx_filtered[ac]) || 2093 !skb_queue_empty(&sta->ps_tx_buf[ac])) 2094 break; 2095 } 2096 } 2097 2098 static void 2099 ieee80211_sta_ps_deliver_response(struct sta_info *sta, 2100 int n_frames, u8 ignored_acs, 2101 enum ieee80211_frame_release_type reason) 2102 { 2103 struct ieee80211_sub_if_data *sdata = sta->sdata; 2104 struct ieee80211_local *local = sdata->local; 2105 unsigned long driver_release_tids = 0; 2106 struct sk_buff_head frames; 2107 bool more_data; 2108 2109 /* Service or PS-Poll period starts */ 2110 set_sta_flag(sta, WLAN_STA_SP); 2111 2112 __skb_queue_head_init(&frames); 2113 2114 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason, 2115 &frames, &driver_release_tids); 2116 2117 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids); 2118 2119 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL) 2120 driver_release_tids = 2121 BIT(find_highest_prio_tid(driver_release_tids)); 2122 2123 if (skb_queue_empty(&frames) && !driver_release_tids) { 2124 int tid, ac; 2125 2126 /* 2127 * For PS-Poll, this can only happen due to a race condition 2128 * when we set the TIM bit and the station notices it, but 2129 * before it can poll for the frame we expire it. 2130 * 2131 * For uAPSD, this is said in the standard (11.2.1.5 h): 2132 * At each unscheduled SP for a non-AP STA, the AP shall 2133 * attempt to transmit at least one MSDU or MMPDU, but no 2134 * more than the value specified in the Max SP Length field 2135 * in the QoS Capability element from delivery-enabled ACs, 2136 * that are destined for the non-AP STA. 2137 * 2138 * Since we have no other MSDU/MMPDU, transmit a QoS null frame. 2139 */ 2140 2141 /* This will evaluate to 1, 3, 5 or 7. */ 2142 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++) 2143 if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac])) 2144 break; 2145 tid = 7 - 2 * ac; 2146 2147 ieee80211_send_null_response(sta, tid, reason, true, false); 2148 } else if (!driver_release_tids) { 2149 struct sk_buff_head pending; 2150 struct sk_buff *skb; 2151 int num = 0; 2152 u16 tids = 0; 2153 bool need_null = false; 2154 2155 skb_queue_head_init(&pending); 2156 2157 while ((skb = __skb_dequeue(&frames))) { 2158 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 2159 struct ieee80211_hdr *hdr = (void *) skb->data; 2160 u8 *qoshdr = NULL; 2161 2162 num++; 2163 2164 /* 2165 * Tell TX path to send this frame even though the 2166 * STA may still remain is PS mode after this frame 2167 * exchange. 2168 */ 2169 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER; 2170 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE; 2171 2172 /* 2173 * Use MoreData flag to indicate whether there are 2174 * more buffered frames for this STA 2175 */ 2176 if (more_data || !skb_queue_empty(&frames)) 2177 hdr->frame_control |= 2178 cpu_to_le16(IEEE80211_FCTL_MOREDATA); 2179 else 2180 hdr->frame_control &= 2181 cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 2182 2183 if (ieee80211_is_data_qos(hdr->frame_control) || 2184 ieee80211_is_qos_nullfunc(hdr->frame_control)) 2185 qoshdr = ieee80211_get_qos_ctl(hdr); 2186 2187 tids |= BIT(skb->priority); 2188 2189 __skb_queue_tail(&pending, skb); 2190 2191 /* end service period after last frame or add one */ 2192 if (!skb_queue_empty(&frames)) 2193 continue; 2194 2195 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) { 2196 /* for PS-Poll, there's only one frame */ 2197 info->flags |= IEEE80211_TX_STATUS_EOSP | 2198 IEEE80211_TX_CTL_REQ_TX_STATUS; 2199 break; 2200 } 2201 2202 /* For uAPSD, things are a bit more complicated. If the 2203 * last frame has a QoS header (i.e. is a QoS-data or 2204 * QoS-nulldata frame) then just set the EOSP bit there 2205 * and be done. 2206 * If the frame doesn't have a QoS header (which means 2207 * it should be a bufferable MMPDU) then we can't set 2208 * the EOSP bit in the QoS header; add a QoS-nulldata 2209 * frame to the list to send it after the MMPDU. 2210 * 2211 * Note that this code is only in the mac80211-release 2212 * code path, we assume that the driver will not buffer 2213 * anything but QoS-data frames, or if it does, will 2214 * create the QoS-nulldata frame by itself if needed. 2215 * 2216 * Cf. 802.11-2012 10.2.1.10 (c). 2217 */ 2218 if (qoshdr) { 2219 *qoshdr |= IEEE80211_QOS_CTL_EOSP; 2220 2221 info->flags |= IEEE80211_TX_STATUS_EOSP | 2222 IEEE80211_TX_CTL_REQ_TX_STATUS; 2223 } else { 2224 /* The standard isn't completely clear on this 2225 * as it says the more-data bit should be set 2226 * if there are more BUs. The QoS-Null frame 2227 * we're about to send isn't buffered yet, we 2228 * only create it below, but let's pretend it 2229 * was buffered just in case some clients only 2230 * expect more-data=0 when eosp=1. 2231 */ 2232 hdr->frame_control |= 2233 cpu_to_le16(IEEE80211_FCTL_MOREDATA); 2234 need_null = true; 2235 num++; 2236 } 2237 break; 2238 } 2239 2240 drv_allow_buffered_frames(local, sta, tids, num, 2241 reason, more_data); 2242 2243 ieee80211_add_pending_skbs(local, &pending); 2244 2245 if (need_null) 2246 ieee80211_send_null_response( 2247 sta, find_highest_prio_tid(tids), 2248 reason, false, false); 2249 2250 sta_info_recalc_tim(sta); 2251 } else { 2252 int tid; 2253 2254 /* 2255 * We need to release a frame that is buffered somewhere in the 2256 * driver ... it'll have to handle that. 2257 * Note that the driver also has to check the number of frames 2258 * on the TIDs we're releasing from - if there are more than 2259 * n_frames it has to set the more-data bit (if we didn't ask 2260 * it to set it anyway due to other buffered frames); if there 2261 * are fewer than n_frames it has to make sure to adjust that 2262 * to allow the service period to end properly. 2263 */ 2264 drv_release_buffered_frames(local, sta, driver_release_tids, 2265 n_frames, reason, more_data); 2266 2267 /* 2268 * Note that we don't recalculate the TIM bit here as it would 2269 * most likely have no effect at all unless the driver told us 2270 * that the TID(s) became empty before returning here from the 2271 * release function. 2272 * Either way, however, when the driver tells us that the TID(s) 2273 * became empty or we find that a txq became empty, we'll do the 2274 * TIM recalculation. 2275 */ 2276 2277 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) { 2278 if (!sta->sta.txq[tid] || 2279 !(driver_release_tids & BIT(tid)) || 2280 txq_has_queue(sta->sta.txq[tid])) 2281 continue; 2282 2283 sta_info_recalc_tim(sta); 2284 break; 2285 } 2286 } 2287 } 2288 2289 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 2290 { 2291 u8 ignore_for_response = sta->sta.uapsd_queues; 2292 2293 /* 2294 * If all ACs are delivery-enabled then we should reply 2295 * from any of them, if only some are enabled we reply 2296 * only from the non-enabled ones. 2297 */ 2298 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1) 2299 ignore_for_response = 0; 2300 2301 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response, 2302 IEEE80211_FRAME_RELEASE_PSPOLL); 2303 } 2304 2305 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta) 2306 { 2307 int n_frames = sta->sta.max_sp; 2308 u8 delivery_enabled = sta->sta.uapsd_queues; 2309 2310 /* 2311 * If we ever grow support for TSPEC this might happen if 2312 * the TSPEC update from hostapd comes in between a trigger 2313 * frame setting WLAN_STA_UAPSD in the RX path and this 2314 * actually getting called. 2315 */ 2316 if (!delivery_enabled) 2317 return; 2318 2319 switch (sta->sta.max_sp) { 2320 case 1: 2321 n_frames = 2; 2322 break; 2323 case 2: 2324 n_frames = 4; 2325 break; 2326 case 3: 2327 n_frames = 6; 2328 break; 2329 case 0: 2330 /* XXX: what is a good value? */ 2331 n_frames = 128; 2332 break; 2333 } 2334 2335 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled, 2336 IEEE80211_FRAME_RELEASE_UAPSD); 2337 } 2338 2339 void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 2340 struct ieee80211_sta *pubsta, bool block) 2341 { 2342 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 2343 2344 trace_api_sta_block_awake(sta->local, pubsta, block); 2345 2346 if (block) { 2347 set_sta_flag(sta, WLAN_STA_PS_DRIVER); 2348 ieee80211_clear_fast_xmit(sta); 2349 return; 2350 } 2351 2352 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER)) 2353 return; 2354 2355 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) { 2356 set_sta_flag(sta, WLAN_STA_PS_DELIVER); 2357 clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 2358 ieee80211_queue_work(hw, &sta->drv_deliver_wk); 2359 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) || 2360 test_sta_flag(sta, WLAN_STA_UAPSD)) { 2361 /* must be asleep in this case */ 2362 clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 2363 ieee80211_queue_work(hw, &sta->drv_deliver_wk); 2364 } else { 2365 clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 2366 ieee80211_check_fast_xmit(sta); 2367 } 2368 } 2369 EXPORT_SYMBOL(ieee80211_sta_block_awake); 2370 2371 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta) 2372 { 2373 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 2374 struct ieee80211_local *local = sta->local; 2375 2376 trace_api_eosp(local, pubsta); 2377 2378 clear_sta_flag(sta, WLAN_STA_SP); 2379 } 2380 EXPORT_SYMBOL(ieee80211_sta_eosp); 2381 2382 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid) 2383 { 2384 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 2385 enum ieee80211_frame_release_type reason; 2386 bool more_data; 2387 2388 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid); 2389 2390 reason = IEEE80211_FRAME_RELEASE_UAPSD; 2391 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues, 2392 reason, 0); 2393 2394 ieee80211_send_null_response(sta, tid, reason, false, more_data); 2395 } 2396 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc); 2397 2398 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, 2399 u8 tid, bool buffered) 2400 { 2401 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 2402 2403 if (WARN_ON(tid >= IEEE80211_NUM_TIDS)) 2404 return; 2405 2406 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered); 2407 2408 if (buffered) 2409 set_bit(tid, &sta->driver_buffered_tids); 2410 else 2411 clear_bit(tid, &sta->driver_buffered_tids); 2412 2413 sta_info_recalc_tim(sta); 2414 } 2415 EXPORT_SYMBOL(ieee80211_sta_set_buffered); 2416 2417 void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid, 2418 u32 tx_airtime, u32 rx_airtime) 2419 { 2420 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 2421 struct ieee80211_local *local = sta->sdata->local; 2422 u8 ac = ieee80211_ac_from_tid(tid); 2423 u32 airtime = 0; 2424 2425 if (sta->local->airtime_flags & AIRTIME_USE_TX) 2426 airtime += tx_airtime; 2427 if (sta->local->airtime_flags & AIRTIME_USE_RX) 2428 airtime += rx_airtime; 2429 2430 spin_lock_bh(&local->active_txq_lock[ac]); 2431 sta->airtime[ac].tx_airtime += tx_airtime; 2432 sta->airtime[ac].rx_airtime += rx_airtime; 2433 2434 if (ieee80211_sta_keep_active(sta, ac)) 2435 sta->airtime[ac].deficit -= airtime; 2436 2437 spin_unlock_bh(&local->active_txq_lock[ac]); 2438 } 2439 EXPORT_SYMBOL(ieee80211_sta_register_airtime); 2440 2441 void __ieee80211_sta_recalc_aggregates(struct sta_info *sta, u16 active_links) 2442 { 2443 bool first = true; 2444 int link_id; 2445 2446 if (!sta->sta.valid_links || !sta->sta.mlo) { 2447 sta->sta.cur = &sta->sta.deflink.agg; 2448 return; 2449 } 2450 2451 rcu_read_lock(); 2452 for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) { 2453 struct ieee80211_link_sta *link_sta; 2454 int i; 2455 2456 if (!(active_links & BIT(link_id))) 2457 continue; 2458 2459 link_sta = rcu_dereference(sta->sta.link[link_id]); 2460 if (!link_sta) 2461 continue; 2462 2463 if (first) { 2464 sta->cur = sta->sta.deflink.agg; 2465 first = false; 2466 continue; 2467 } 2468 2469 sta->cur.max_amsdu_len = 2470 min(sta->cur.max_amsdu_len, 2471 link_sta->agg.max_amsdu_len); 2472 sta->cur.max_rc_amsdu_len = 2473 min(sta->cur.max_rc_amsdu_len, 2474 link_sta->agg.max_rc_amsdu_len); 2475 2476 for (i = 0; i < ARRAY_SIZE(sta->cur.max_tid_amsdu_len); i++) 2477 sta->cur.max_tid_amsdu_len[i] = 2478 min(sta->cur.max_tid_amsdu_len[i], 2479 link_sta->agg.max_tid_amsdu_len[i]); 2480 } 2481 rcu_read_unlock(); 2482 2483 sta->sta.cur = &sta->cur; 2484 } 2485 2486 void ieee80211_sta_recalc_aggregates(struct ieee80211_sta *pubsta) 2487 { 2488 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 2489 2490 __ieee80211_sta_recalc_aggregates(sta, sta->sdata->vif.active_links); 2491 } 2492 EXPORT_SYMBOL(ieee80211_sta_recalc_aggregates); 2493 2494 void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local, 2495 struct sta_info *sta, u8 ac, 2496 u16 tx_airtime, bool tx_completed, 2497 bool mcast) 2498 { 2499 int tx_pending; 2500 2501 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) 2502 return; 2503 2504 if (mcast) { 2505 if (!tx_completed) { 2506 atomic_add(tx_airtime, &local->aql_mc_pending_airtime); 2507 return; 2508 } 2509 2510 tx_pending = atomic_sub_return(tx_airtime, 2511 &local->aql_mc_pending_airtime); 2512 if (tx_pending < 0) 2513 atomic_cmpxchg(&local->aql_mc_pending_airtime, 2514 tx_pending, 0); 2515 return; 2516 } 2517 2518 if (!tx_completed) { 2519 if (sta) 2520 atomic_add(tx_airtime, 2521 &sta->airtime[ac].aql_tx_pending); 2522 2523 atomic_add(tx_airtime, &local->aql_total_pending_airtime); 2524 atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]); 2525 return; 2526 } 2527 2528 if (sta) { 2529 tx_pending = atomic_sub_return(tx_airtime, 2530 &sta->airtime[ac].aql_tx_pending); 2531 if (tx_pending < 0) 2532 atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending, 2533 tx_pending, 0); 2534 } 2535 2536 atomic_sub(tx_airtime, &local->aql_total_pending_airtime); 2537 tx_pending = atomic_sub_return(tx_airtime, 2538 &local->aql_ac_pending_airtime[ac]); 2539 if (WARN_ONCE(tx_pending < 0, 2540 "Device %s AC %d pending airtime underflow: %u, %u", 2541 wiphy_name(local->hw.wiphy), ac, tx_pending, 2542 tx_airtime)) { 2543 atomic_cmpxchg(&local->aql_ac_pending_airtime[ac], 2544 tx_pending, 0); 2545 atomic_sub(tx_pending, &local->aql_total_pending_airtime); 2546 } 2547 } 2548 2549 static struct ieee80211_sta_rx_stats * 2550 sta_get_last_rx_stats(struct link_sta_info *link_sta_info) 2551 { 2552 struct ieee80211_sta_rx_stats *stats = &link_sta_info->rx_stats; 2553 int cpu; 2554 2555 if (!link_sta_info->pcpu_rx_stats) 2556 return stats; 2557 2558 for_each_possible_cpu(cpu) { 2559 struct ieee80211_sta_rx_stats *cpustats; 2560 2561 cpustats = per_cpu_ptr(link_sta_info->pcpu_rx_stats, cpu); 2562 2563 if (time_after(cpustats->last_rx, stats->last_rx)) 2564 stats = cpustats; 2565 } 2566 2567 return stats; 2568 } 2569 2570 static unsigned long sta_link_last_active(struct link_sta_info *link_sta_info) 2571 { 2572 struct ieee80211_sta_rx_stats *stats; 2573 2574 stats = sta_get_last_rx_stats(link_sta_info); 2575 2576 if (!link_sta_info->status_stats.last_ack || 2577 time_after(stats->last_rx, link_sta_info->status_stats.last_ack)) 2578 return stats->last_rx; 2579 2580 return link_sta_info->status_stats.last_ack; 2581 } 2582 2583 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate, 2584 struct rate_info *rinfo) 2585 { 2586 rinfo->bw = STA_STATS_GET(BW, rate); 2587 2588 switch (STA_STATS_GET(TYPE, rate)) { 2589 case STA_STATS_RATE_TYPE_VHT: 2590 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS; 2591 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate); 2592 rinfo->nss = STA_STATS_GET(VHT_NSS, rate); 2593 if (STA_STATS_GET(SGI, rate)) 2594 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI; 2595 break; 2596 case STA_STATS_RATE_TYPE_HT: 2597 rinfo->flags = RATE_INFO_FLAGS_MCS; 2598 rinfo->mcs = STA_STATS_GET(HT_MCS, rate); 2599 if (STA_STATS_GET(SGI, rate)) 2600 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI; 2601 break; 2602 case STA_STATS_RATE_TYPE_LEGACY: { 2603 struct ieee80211_supported_band *sband; 2604 u16 brate; 2605 unsigned int shift; 2606 int band = STA_STATS_GET(LEGACY_BAND, rate); 2607 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate); 2608 2609 sband = local->hw.wiphy->bands[band]; 2610 2611 if (WARN_ON_ONCE(!sband->bitrates)) 2612 break; 2613 2614 brate = sband->bitrates[rate_idx].bitrate; 2615 if (rinfo->bw == RATE_INFO_BW_5) 2616 shift = 2; 2617 else if (rinfo->bw == RATE_INFO_BW_10) 2618 shift = 1; 2619 else 2620 shift = 0; 2621 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift); 2622 break; 2623 } 2624 case STA_STATS_RATE_TYPE_HE: 2625 rinfo->flags = RATE_INFO_FLAGS_HE_MCS; 2626 rinfo->mcs = STA_STATS_GET(HE_MCS, rate); 2627 rinfo->nss = STA_STATS_GET(HE_NSS, rate); 2628 rinfo->he_gi = STA_STATS_GET(HE_GI, rate); 2629 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate); 2630 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate); 2631 break; 2632 case STA_STATS_RATE_TYPE_EHT: 2633 rinfo->flags = RATE_INFO_FLAGS_EHT_MCS; 2634 rinfo->mcs = STA_STATS_GET(EHT_MCS, rate); 2635 rinfo->nss = STA_STATS_GET(EHT_NSS, rate); 2636 rinfo->eht_gi = STA_STATS_GET(EHT_GI, rate); 2637 rinfo->eht_ru_alloc = STA_STATS_GET(EHT_RU, rate); 2638 break; 2639 case STA_STATS_RATE_TYPE_UHR: 2640 rinfo->flags = RATE_INFO_FLAGS_UHR_MCS; 2641 rinfo->mcs = STA_STATS_GET(UHR_MCS, rate); 2642 rinfo->nss = STA_STATS_GET(UHR_NSS, rate); 2643 rinfo->eht_gi = STA_STATS_GET(UHR_GI, rate); 2644 rinfo->eht_ru_alloc = STA_STATS_GET(UHR_RU, rate); 2645 if (STA_STATS_GET(UHR_ELR, rate)) 2646 rinfo->flags |= RATE_INFO_FLAGS_UHR_ELR_MCS; 2647 if (STA_STATS_GET(UHR_IM, rate)) 2648 rinfo->flags |= RATE_INFO_FLAGS_UHR_IM; 2649 break; 2650 case STA_STATS_RATE_TYPE_S1G: 2651 rinfo->flags = RATE_INFO_FLAGS_S1G_MCS; 2652 rinfo->mcs = STA_STATS_GET(S1G_MCS, rate); 2653 rinfo->nss = STA_STATS_GET(S1G_NSS, rate); 2654 if (STA_STATS_GET(SGI, rate)) 2655 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI; 2656 break; 2657 } 2658 } 2659 2660 static int sta_set_rate_info_rx(struct sta_info *sta, 2661 struct link_sta_info *link_sta_info, 2662 struct rate_info *rinfo) 2663 { 2664 u32 rate = READ_ONCE(sta_get_last_rx_stats(link_sta_info)->last_rate); 2665 2666 if (rate == STA_STATS_RATE_INVALID) 2667 return -EINVAL; 2668 2669 sta_stats_decode_rate(sta->local, rate, rinfo); 2670 return 0; 2671 } 2672 2673 static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats, 2674 int tid) 2675 { 2676 unsigned int start; 2677 u64 value; 2678 2679 do { 2680 start = u64_stats_fetch_begin(&rxstats->syncp); 2681 value = u64_stats_read(&rxstats->msdu[tid]); 2682 } while (u64_stats_fetch_retry(&rxstats->syncp, start)); 2683 2684 return value; 2685 } 2686 2687 static void sta_set_tidstats(struct sta_info *sta, 2688 struct link_sta_info *link_sta_info, 2689 struct cfg80211_tid_stats *tidstats, 2690 int tid, bool txq_stats) 2691 { 2692 struct ieee80211_local *local = sta->local; 2693 int cpu; 2694 2695 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) { 2696 tidstats->rx_msdu += 2697 sta_get_tidstats_msdu(&link_sta_info->rx_stats, 2698 tid); 2699 2700 if (link_sta_info->pcpu_rx_stats) { 2701 for_each_possible_cpu(cpu) { 2702 struct ieee80211_sta_rx_stats *cpurxs; 2703 2704 cpurxs = per_cpu_ptr(link_sta_info->pcpu_rx_stats, 2705 cpu); 2706 tidstats->rx_msdu += 2707 sta_get_tidstats_msdu(cpurxs, tid); 2708 } 2709 } 2710 2711 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU); 2712 } 2713 2714 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) { 2715 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU); 2716 tidstats->tx_msdu = link_sta_info->tx_stats.msdu[tid]; 2717 } 2718 2719 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) && 2720 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) { 2721 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES); 2722 tidstats->tx_msdu_retries = 2723 link_sta_info->status_stats.msdu_retries[tid]; 2724 } 2725 2726 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) && 2727 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) { 2728 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED); 2729 tidstats->tx_msdu_failed = 2730 link_sta_info->status_stats.msdu_failed[tid]; 2731 } 2732 2733 if (txq_stats && tid < IEEE80211_NUM_TIDS) { 2734 spin_lock_bh(&local->fq.lock); 2735 2736 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS); 2737 ieee80211_fill_txq_stats(&tidstats->txq_stats, 2738 to_txq_info(sta->sta.txq[tid])); 2739 2740 spin_unlock_bh(&local->fq.lock); 2741 } 2742 } 2743 2744 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats) 2745 { 2746 unsigned int start; 2747 u64 value; 2748 2749 do { 2750 start = u64_stats_fetch_begin(&rxstats->syncp); 2751 value = u64_stats_read(&rxstats->bytes); 2752 } while (u64_stats_fetch_retry(&rxstats->syncp, start)); 2753 2754 return value; 2755 } 2756 2757 #ifdef CONFIG_MAC80211_MESH 2758 static void sta_set_mesh_sinfo(struct sta_info *sta, 2759 struct station_info *sinfo) 2760 { 2761 struct ieee80211_local *local = sta->sdata->local; 2762 2763 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) | 2764 BIT_ULL(NL80211_STA_INFO_PLID) | 2765 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) | 2766 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) | 2767 BIT_ULL(NL80211_STA_INFO_PEER_PM) | 2768 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) | 2769 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) | 2770 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS); 2771 2772 sinfo->llid = sta->mesh->llid; 2773 sinfo->plid = sta->mesh->plid; 2774 sinfo->plink_state = sta->mesh->plink_state; 2775 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) { 2776 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET); 2777 sinfo->t_offset = sta->mesh->t_offset; 2778 } 2779 sinfo->local_pm = sta->mesh->local_pm; 2780 sinfo->peer_pm = sta->mesh->peer_pm; 2781 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm; 2782 sinfo->connected_to_gate = sta->mesh->connected_to_gate; 2783 sinfo->connected_to_as = sta->mesh->connected_to_as; 2784 2785 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC); 2786 sinfo->airtime_link_metric = airtime_link_metric_get(local, sta); 2787 } 2788 #endif 2789 2790 void sta_set_accumulated_removed_links_sinfo(struct sta_info *sta, 2791 struct station_info *sinfo) 2792 { 2793 /* Accumulating the removed link statistics. */ 2794 sinfo->tx_packets = sta->rem_link_stats.tx_packets; 2795 sinfo->rx_packets = sta->rem_link_stats.rx_packets; 2796 sinfo->tx_bytes = sta->rem_link_stats.tx_bytes; 2797 sinfo->rx_bytes = sta->rem_link_stats.rx_bytes; 2798 sinfo->tx_retries = sta->rem_link_stats.tx_retries; 2799 sinfo->tx_failed = sta->rem_link_stats.tx_failed; 2800 sinfo->rx_dropped_misc = sta->rem_link_stats.rx_dropped_misc; 2801 sinfo->beacon_loss_count = sta->rem_link_stats.beacon_loss_count; 2802 sinfo->expected_throughput = sta->rem_link_stats.expected_throughput; 2803 2804 if (sinfo->pertid) { 2805 sinfo->pertid->rx_msdu = 2806 sta->rem_link_stats.pertid_stats.rx_msdu; 2807 sinfo->pertid->tx_msdu = 2808 sta->rem_link_stats.pertid_stats.tx_msdu; 2809 sinfo->pertid->tx_msdu_retries = 2810 sta->rem_link_stats.pertid_stats.tx_msdu_retries; 2811 sinfo->pertid->tx_msdu_failed = 2812 sta->rem_link_stats.pertid_stats.tx_msdu_failed; 2813 } 2814 } 2815 2816 static u32 sta_estimate_expected_throughput(struct sta_info *sta, 2817 struct rate_info *ri, 2818 struct ieee80211_bss_conf *bss_conf) 2819 { 2820 struct ieee80211_hw *hw = &sta->sdata->local->hw; 2821 struct ieee80211_chanctx_conf *conf; 2822 u32 duration; 2823 u8 band; 2824 2825 conf = rcu_dereference_wiphy(hw->wiphy, bss_conf->chanctx_conf); 2826 if (!conf) 2827 return 0; 2828 band = conf->def.chan->band; 2829 2830 duration = ieee80211_rate_expected_tx_airtime(hw, NULL, ri, band, true, 1024); 2831 duration += duration >> 4; /* add assumed packet error rate of ~6% */ 2832 if (!duration) 2833 return 0; 2834 2835 return ((1024 * USEC_PER_SEC) / duration) * 8; 2836 } 2837 2838 static int sta_set_link_sinfo(struct sta_info *sta, 2839 struct link_sta_info *link_sta_info, 2840 struct link_station_info *link_sinfo, 2841 struct ieee80211_link_data *link, 2842 bool tidstats, bool locked) 2843 { 2844 struct ieee80211_sub_if_data *sdata = sta->sdata; 2845 struct ieee80211_sta_rx_stats *last_rxstats; 2846 int i, ac, cpu, ret; 2847 u32 thr = 0; 2848 2849 last_rxstats = sta_get_last_rx_stats(link_sta_info); 2850 2851 /* do before driver, so beacon filtering drivers have a 2852 * chance to e.g. just add the number of filtered beacons 2853 * (or just modify the value entirely, of course) 2854 */ 2855 if (sdata->vif.type == NL80211_IFTYPE_STATION) 2856 link_sinfo->rx_beacon = link->u.mgd.count_beacon_signal; 2857 2858 ether_addr_copy(link_sinfo->addr, link_sta_info->addr); 2859 2860 ret = drv_link_sta_statistics(sta->local, sdata, link_sta_info->pub, 2861 link_sinfo, locked); 2862 if (ret) 2863 return ret; 2864 2865 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) | 2866 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC); 2867 2868 if (sdata->vif.type == NL80211_IFTYPE_STATION) { 2869 link_sinfo->beacon_loss_count = 2870 link->u.mgd.beacon_loss_count; 2871 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS); 2872 } 2873 2874 link_sinfo->inactive_time = 2875 jiffies_delta_to_msecs(jiffies - 2876 sta_link_last_active(link_sta_info)); 2877 2878 if (!(link_sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) | 2879 BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) { 2880 link_sinfo->tx_bytes = 0; 2881 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 2882 link_sinfo->tx_bytes += 2883 link_sta_info->tx_stats.bytes[ac]; 2884 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64); 2885 } 2886 2887 if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) { 2888 link_sinfo->tx_packets = 0; 2889 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 2890 link_sinfo->tx_packets += 2891 link_sta_info->tx_stats.packets[ac]; 2892 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS); 2893 } 2894 2895 if (!(link_sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) | 2896 BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) { 2897 link_sinfo->rx_bytes += 2898 sta_get_stats_bytes(&link_sta_info->rx_stats); 2899 2900 if (link_sta_info->pcpu_rx_stats) { 2901 for_each_possible_cpu(cpu) { 2902 struct ieee80211_sta_rx_stats *cpurxs; 2903 2904 cpurxs = per_cpu_ptr(link_sta_info->pcpu_rx_stats, 2905 cpu); 2906 link_sinfo->rx_bytes += 2907 sta_get_stats_bytes(cpurxs); 2908 } 2909 } 2910 2911 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64); 2912 } 2913 2914 if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) { 2915 link_sinfo->rx_packets = link_sta_info->rx_stats.packets; 2916 if (link_sta_info->pcpu_rx_stats) { 2917 for_each_possible_cpu(cpu) { 2918 struct ieee80211_sta_rx_stats *cpurxs; 2919 2920 cpurxs = per_cpu_ptr(link_sta_info->pcpu_rx_stats, 2921 cpu); 2922 link_sinfo->rx_packets += cpurxs->packets; 2923 } 2924 } 2925 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS); 2926 } 2927 2928 if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) { 2929 link_sinfo->tx_retries = 2930 link_sta_info->status_stats.retry_count; 2931 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES); 2932 } 2933 2934 if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) { 2935 link_sinfo->tx_failed = 2936 link_sta_info->status_stats.retry_failed; 2937 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED); 2938 } 2939 2940 if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) { 2941 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 2942 link_sinfo->rx_duration += sta->airtime[ac].rx_airtime; 2943 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION); 2944 } 2945 2946 if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) { 2947 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 2948 link_sinfo->tx_duration += sta->airtime[ac].tx_airtime; 2949 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION); 2950 } 2951 2952 if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) { 2953 link_sinfo->airtime_weight = sta->airtime_weight; 2954 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT); 2955 } 2956 2957 link_sinfo->rx_dropped_misc = link_sta_info->rx_stats.dropped; 2958 if (link_sta_info->pcpu_rx_stats) { 2959 for_each_possible_cpu(cpu) { 2960 struct ieee80211_sta_rx_stats *cpurxs; 2961 2962 cpurxs = per_cpu_ptr(link_sta_info->pcpu_rx_stats, 2963 cpu); 2964 link_sinfo->rx_dropped_misc += cpurxs->dropped; 2965 } 2966 } 2967 2968 if (sdata->vif.type == NL80211_IFTYPE_STATION && 2969 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) { 2970 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) | 2971 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG); 2972 link_sinfo->rx_beacon_signal_avg = 2973 ieee80211_ave_rssi(&sdata->vif, -1); 2974 } 2975 2976 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) || 2977 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) { 2978 if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) { 2979 link_sinfo->signal = (s8)last_rxstats->last_signal; 2980 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL); 2981 } 2982 2983 if (!link_sta_info->pcpu_rx_stats && 2984 !(link_sinfo->filled & 2985 BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) { 2986 link_sinfo->signal_avg = 2987 -ewma_signal_read(&link_sta_info->rx_stats_avg.signal); 2988 link_sinfo->filled |= 2989 BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG); 2990 } 2991 } 2992 2993 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to 2994 * the sta->rx_stats struct, so the check here is fine with and without 2995 * pcpu statistics 2996 */ 2997 if (last_rxstats->chains && 2998 !(link_sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) | 2999 BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) { 3000 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL); 3001 if (!link_sta_info->pcpu_rx_stats) 3002 link_sinfo->filled |= 3003 BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG); 3004 3005 link_sinfo->chains = last_rxstats->chains; 3006 3007 for (i = 0; i < ARRAY_SIZE(link_sinfo->chain_signal); i++) { 3008 link_sinfo->chain_signal[i] = 3009 last_rxstats->chain_signal_last[i]; 3010 link_sinfo->chain_signal_avg[i] = 3011 -ewma_signal_read( 3012 &link_sta_info->rx_stats_avg.chain_signal[i]); 3013 } 3014 } 3015 3016 if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) && 3017 ieee80211_rate_valid(&link_sta_info->tx_stats.last_rate)) { 3018 sta_set_rate_info_tx(sta, &link_sta_info->tx_stats.last_rate, 3019 &link_sinfo->txrate); 3020 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); 3021 } 3022 3023 if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) { 3024 if (sta_set_rate_info_rx(sta, link_sta_info, 3025 &link_sinfo->rxrate) == 0) 3026 link_sinfo->filled |= 3027 BIT_ULL(NL80211_STA_INFO_RX_BITRATE); 3028 } 3029 3030 if (tidstats && !cfg80211_link_sinfo_alloc_tid_stats(link_sinfo, 3031 GFP_ATOMIC)) { 3032 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) 3033 sta_set_tidstats(sta, link_sta_info, 3034 &link_sinfo->pertid[i], i, false); 3035 } 3036 3037 if (sdata->vif.type == NL80211_IFTYPE_STATION) { 3038 link_sinfo->bss_param.flags = 0; 3039 if (link->conf->use_cts_prot) 3040 link_sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT; 3041 if (link->conf->use_short_preamble) 3042 link_sinfo->bss_param.flags |= 3043 BSS_PARAM_FLAGS_SHORT_PREAMBLE; 3044 if (link->conf->use_short_slot) 3045 link_sinfo->bss_param.flags |= 3046 BSS_PARAM_FLAGS_SHORT_SLOT_TIME; 3047 link_sinfo->bss_param.dtim_period = link->conf->dtim_period; 3048 link_sinfo->bss_param.beacon_interval = link->conf->beacon_int; 3049 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BSS_PARAM); 3050 } 3051 3052 thr = sta_get_expected_throughput(sta); 3053 if (!thr && (link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) 3054 thr = sta_estimate_expected_throughput(sta, 3055 &link_sinfo->txrate, 3056 link->conf); 3057 3058 if (thr != 0) { 3059 link_sinfo->filled |= 3060 BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT); 3061 link_sinfo->expected_throughput = thr; 3062 } 3063 3064 if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) && 3065 link_sta_info->status_stats.ack_signal_filled) { 3066 link_sinfo->ack_signal = 3067 link_sta_info->status_stats.last_ack_signal; 3068 link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL); 3069 } 3070 3071 if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) && 3072 link_sta_info->status_stats.ack_signal_filled) { 3073 link_sinfo->avg_ack_signal = 3074 -(s8)ewma_avg_signal_read( 3075 &link_sta_info->status_stats.avg_ack_signal); 3076 link_sinfo->filled |= 3077 BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG); 3078 } 3079 3080 return 0; 3081 } 3082 3083 int __sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo, 3084 bool tidstats, bool locked) 3085 { 3086 struct ieee80211_sub_if_data *sdata = sta->sdata; 3087 struct ieee80211_local *local = sdata->local; 3088 u32 thr = 0; 3089 int i, ac, cpu, ret; 3090 struct ieee80211_sta_rx_stats *last_rxstats; 3091 3092 last_rxstats = sta_get_last_rx_stats(&sta->deflink); 3093 3094 sinfo->generation = sdata->local->sta_generation; 3095 3096 /* do before driver, so beacon filtering drivers have a 3097 * chance to e.g. just add the number of filtered beacons 3098 * (or just modify the value entirely, of course) 3099 */ 3100 if (sdata->vif.type == NL80211_IFTYPE_STATION) 3101 sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal; 3102 3103 ret = drv_sta_statistics(local, sdata, &sta->sta, sinfo, locked); 3104 if (ret) 3105 return ret; 3106 3107 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) | 3108 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) | 3109 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) | 3110 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) | 3111 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC); 3112 3113 if (sdata->vif.type == NL80211_IFTYPE_STATION) { 3114 sinfo->beacon_loss_count = 3115 sdata->deflink.u.mgd.beacon_loss_count; 3116 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS); 3117 } 3118 3119 sinfo->connected_time = ktime_get_seconds() - sta->last_connected; 3120 sinfo->assoc_at = sta->assoc_at; 3121 sinfo->inactive_time = 3122 jiffies_delta_to_msecs(jiffies - 3123 sta_link_last_active(&sta->deflink)); 3124 3125 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) | 3126 BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) { 3127 sinfo->tx_bytes = 0; 3128 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 3129 sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac]; 3130 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64); 3131 } 3132 3133 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) { 3134 sinfo->tx_packets = 0; 3135 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 3136 sinfo->tx_packets += sta->deflink.tx_stats.packets[ac]; 3137 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS); 3138 } 3139 3140 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) | 3141 BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) { 3142 sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats); 3143 3144 if (sta->deflink.pcpu_rx_stats) { 3145 for_each_possible_cpu(cpu) { 3146 struct ieee80211_sta_rx_stats *cpurxs; 3147 3148 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, 3149 cpu); 3150 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs); 3151 } 3152 } 3153 3154 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64); 3155 } 3156 3157 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) { 3158 sinfo->rx_packets = sta->deflink.rx_stats.packets; 3159 if (sta->deflink.pcpu_rx_stats) { 3160 for_each_possible_cpu(cpu) { 3161 struct ieee80211_sta_rx_stats *cpurxs; 3162 3163 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, 3164 cpu); 3165 sinfo->rx_packets += cpurxs->packets; 3166 } 3167 } 3168 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS); 3169 } 3170 3171 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) { 3172 sinfo->tx_retries = sta->deflink.status_stats.retry_count; 3173 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES); 3174 } 3175 3176 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) { 3177 sinfo->tx_failed = sta->deflink.status_stats.retry_failed; 3178 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED); 3179 } 3180 3181 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) { 3182 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 3183 sinfo->rx_duration += sta->airtime[ac].rx_airtime; 3184 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION); 3185 } 3186 3187 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) { 3188 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 3189 sinfo->tx_duration += sta->airtime[ac].tx_airtime; 3190 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION); 3191 } 3192 3193 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) { 3194 sinfo->airtime_weight = sta->airtime_weight; 3195 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT); 3196 } 3197 3198 sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped; 3199 if (sta->deflink.pcpu_rx_stats) { 3200 for_each_possible_cpu(cpu) { 3201 struct ieee80211_sta_rx_stats *cpurxs; 3202 3203 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu); 3204 sinfo->rx_dropped_misc += cpurxs->dropped; 3205 } 3206 } 3207 3208 if (sdata->vif.type == NL80211_IFTYPE_STATION && 3209 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) { 3210 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) | 3211 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG); 3212 sinfo->rx_beacon_signal_avg = 3213 ieee80211_ave_rssi(&sdata->vif, -1); 3214 } 3215 3216 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) || 3217 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) { 3218 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) { 3219 sinfo->signal = (s8)last_rxstats->last_signal; 3220 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL); 3221 } 3222 3223 if (!sta->deflink.pcpu_rx_stats && 3224 !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) { 3225 sinfo->signal_avg = 3226 -ewma_signal_read(&sta->deflink.rx_stats_avg.signal); 3227 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG); 3228 } 3229 } 3230 3231 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to 3232 * the sta->rx_stats struct, so the check here is fine with and without 3233 * pcpu statistics 3234 */ 3235 if (last_rxstats->chains && 3236 !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) | 3237 BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) { 3238 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL); 3239 if (!sta->deflink.pcpu_rx_stats) 3240 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG); 3241 3242 sinfo->chains = last_rxstats->chains; 3243 3244 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) { 3245 sinfo->chain_signal[i] = 3246 last_rxstats->chain_signal_last[i]; 3247 sinfo->chain_signal_avg[i] = 3248 -ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]); 3249 } 3250 } 3251 3252 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) && 3253 !sta->sta.valid_links && 3254 ieee80211_rate_valid(&sta->deflink.tx_stats.last_rate)) { 3255 sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate, 3256 &sinfo->txrate); 3257 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); 3258 } 3259 3260 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) && 3261 !sta->sta.valid_links) { 3262 if (sta_set_rate_info_rx(sta, &sta->deflink, 3263 &sinfo->rxrate) == 0) 3264 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE); 3265 } 3266 3267 if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_ATOMIC)) { 3268 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) 3269 sta_set_tidstats(sta, &sta->deflink, 3270 &sinfo->pertid[i], i, true); 3271 } 3272 3273 #ifdef CONFIG_MAC80211_MESH 3274 if (ieee80211_vif_is_mesh(&sdata->vif)) 3275 sta_set_mesh_sinfo(sta, sinfo); 3276 #endif 3277 3278 if (sdata->vif.type == NL80211_IFTYPE_STATION) { 3279 sinfo->bss_param.flags = 0; 3280 if (sdata->vif.bss_conf.use_cts_prot) 3281 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT; 3282 if (sdata->vif.bss_conf.use_short_preamble) 3283 sinfo->bss_param.flags |= 3284 BSS_PARAM_FLAGS_SHORT_PREAMBLE; 3285 if (sdata->vif.bss_conf.use_short_slot) 3286 sinfo->bss_param.flags |= 3287 BSS_PARAM_FLAGS_SHORT_SLOT_TIME; 3288 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period; 3289 sinfo->bss_param.beacon_interval = 3290 sdata->vif.bss_conf.beacon_int; 3291 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BSS_PARAM); 3292 } 3293 3294 sinfo->sta_flags.set = 0; 3295 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) | 3296 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) | 3297 BIT(NL80211_STA_FLAG_WME) | 3298 BIT(NL80211_STA_FLAG_MFP) | 3299 BIT(NL80211_STA_FLAG_AUTHENTICATED) | 3300 BIT(NL80211_STA_FLAG_ASSOCIATED) | 3301 BIT(NL80211_STA_FLAG_TDLS_PEER); 3302 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED)) 3303 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED); 3304 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE)) 3305 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE); 3306 if (sta->sta.wme) 3307 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME); 3308 if (test_sta_flag(sta, WLAN_STA_MFP)) 3309 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP); 3310 if (test_sta_flag(sta, WLAN_STA_AUTH)) 3311 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED); 3312 if (test_sta_flag(sta, WLAN_STA_ASSOC)) 3313 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED); 3314 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) 3315 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER); 3316 3317 thr = sta_get_expected_throughput(sta); 3318 3319 if (thr != 0) { 3320 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT); 3321 sinfo->expected_throughput = thr; 3322 } else if (!sta->sta.valid_links && 3323 (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) { 3324 thr = sta_estimate_expected_throughput(sta, &sinfo->txrate, 3325 &sdata->vif.bss_conf); 3326 if (thr) { 3327 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT); 3328 sinfo->expected_throughput = thr; 3329 } 3330 } 3331 3332 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) && 3333 sta->deflink.status_stats.ack_signal_filled) { 3334 sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal; 3335 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL); 3336 } 3337 3338 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) && 3339 sta->deflink.status_stats.ack_signal_filled) { 3340 sinfo->avg_ack_signal = 3341 -(s8)ewma_avg_signal_read( 3342 &sta->deflink.status_stats.avg_ack_signal); 3343 sinfo->filled |= 3344 BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG); 3345 } 3346 3347 if (sta->sta.valid_links) { 3348 struct ieee80211_link_data *link; 3349 struct link_sta_info *link_sta; 3350 u32 est_thr = 0; 3351 int link_id; 3352 3353 sinfo->mlo_params_valid = true; 3354 sinfo->assoc_link_id = sta->deflink.link_id; 3355 if (sta->sta.mlo) 3356 ether_addr_copy(sinfo->mld_addr, sta->addr); 3357 3358 /* assign valid links first for iteration */ 3359 sinfo->valid_links = sta->sta.valid_links; 3360 3361 for_each_valid_link(sinfo, link_id) { 3362 struct link_station_info *link_sinfo = sinfo->links[link_id]; 3363 3364 link_sta = rcu_dereference_wiphy(sta->local->hw.wiphy, 3365 sta->link[link_id]); 3366 link = rcu_dereference_wiphy(sdata->local->hw.wiphy, 3367 sdata->link[link_id]); 3368 3369 if (!link_sta || !link_sinfo || !link) { 3370 sinfo->valid_links &= ~BIT(link_id); 3371 continue; 3372 } 3373 ret = sta_set_link_sinfo(sta, link_sta, link_sinfo, link, 3374 tidstats, locked); 3375 if (ret) 3376 return ret; 3377 if (!thr && 3378 (link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT))) 3379 est_thr += link_sinfo->expected_throughput; 3380 } 3381 if (est_thr) { 3382 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT); 3383 sinfo->expected_throughput = est_thr; 3384 } 3385 } 3386 3387 return 0; 3388 } 3389 3390 u32 sta_get_expected_throughput(struct sta_info *sta) 3391 { 3392 struct ieee80211_sub_if_data *sdata = sta->sdata; 3393 struct ieee80211_local *local = sdata->local; 3394 struct rate_control_ref *ref = NULL; 3395 u32 thr = 0; 3396 3397 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) 3398 ref = local->rate_ctrl; 3399 3400 /* check if the driver has a SW RC implementation */ 3401 if (ref && ref->ops->get_expected_throughput) 3402 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv); 3403 else 3404 thr = drv_get_expected_throughput(local, sta); 3405 3406 return thr; 3407 } 3408 3409 unsigned long ieee80211_sta_last_active(struct sta_info *sta, int link_id) 3410 { 3411 struct link_sta_info *link_sta_info; 3412 3413 if (link_id < 0) 3414 link_sta_info = &sta->deflink; 3415 else 3416 link_sta_info = wiphy_dereference(sta->local->hw.wiphy, 3417 sta->link[link_id]); 3418 3419 return sta_link_last_active(link_sta_info); 3420 } 3421 3422 int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id) 3423 { 3424 struct ieee80211_sub_if_data *sdata = sta->sdata; 3425 struct sta_link_alloc *alloc; 3426 int ret; 3427 3428 lockdep_assert_wiphy(sdata->local->hw.wiphy); 3429 3430 WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED)); 3431 3432 /* must represent an MLD from the start */ 3433 if (WARN_ON(!sta->sta.valid_links)) 3434 return -EINVAL; 3435 3436 if (WARN_ON(sta->sta.valid_links & BIT(link_id) || 3437 sta->link[link_id])) 3438 return -EBUSY; 3439 3440 alloc = kzalloc_obj(*alloc); 3441 if (!alloc) 3442 return -ENOMEM; 3443 3444 ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL); 3445 if (ret) { 3446 kfree(alloc); 3447 return ret; 3448 } 3449 3450 sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta); 3451 3452 ieee80211_link_sta_debugfs_add(&alloc->info); 3453 3454 return 0; 3455 } 3456 3457 void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id) 3458 { 3459 lockdep_assert_wiphy(sta->sdata->local->hw.wiphy); 3460 3461 WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED)); 3462 3463 sta_remove_link(sta, link_id, false); 3464 } 3465 3466 int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id) 3467 { 3468 struct ieee80211_sub_if_data *sdata = sta->sdata; 3469 struct link_sta_info *link_sta; 3470 u16 old_links = sta->sta.valid_links; 3471 u16 new_links = old_links | BIT(link_id); 3472 int ret; 3473 3474 link_sta = rcu_dereference_protected(sta->link[link_id], 3475 lockdep_is_held(&sdata->local->hw.wiphy->mtx)); 3476 3477 if (WARN_ON(old_links == new_links || !link_sta)) 3478 return -EINVAL; 3479 3480 rcu_read_lock(); 3481 if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) { 3482 rcu_read_unlock(); 3483 return -EALREADY; 3484 } 3485 /* we only modify under the mutex so this is fine */ 3486 rcu_read_unlock(); 3487 3488 sta->sta.valid_links = new_links; 3489 3490 if (WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED))) 3491 goto hash; 3492 3493 ieee80211_recalc_min_chandef(sdata, link_id); 3494 3495 /* Ensure the values are updated for the driver, 3496 * redone by sta_remove_link on failure. 3497 */ 3498 ieee80211_sta_recalc_aggregates(&sta->sta); 3499 3500 ret = drv_change_sta_links(sdata->local, sdata, &sta->sta, 3501 old_links, new_links); 3502 if (ret) { 3503 sta->sta.valid_links = old_links; 3504 sta_remove_link(sta, link_id, false); 3505 return ret; 3506 } 3507 3508 hash: 3509 ret = link_sta_info_hash_add(sdata->local, link_sta); 3510 WARN_ON(ret); 3511 return 0; 3512 } 3513 3514 void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id) 3515 { 3516 struct ieee80211_sub_if_data *sdata = sta->sdata; 3517 u16 old_links = sta->sta.valid_links; 3518 3519 lockdep_assert_wiphy(sdata->local->hw.wiphy); 3520 3521 sta->sta.valid_links &= ~BIT(link_id); 3522 3523 if (!WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED))) 3524 drv_change_sta_links(sdata->local, sdata, &sta->sta, 3525 old_links, sta->sta.valid_links); 3526 3527 sta_remove_link(sta, link_id, true); 3528 } 3529 3530 static u8 ieee80211_sta_nss_capability(struct link_sta_info *link_sta) 3531 { 3532 u8 ht_rx_nss = 0, vht_rx_nss = 0, he_rx_nss = 0, eht_rx_nss = 0, rx_nss; 3533 bool support_160; 3534 3535 if (link_sta->pub->eht_cap.has_eht) { 3536 int i; 3537 const u8 *rx_nss_mcs = (void *)&link_sta->pub->eht_cap.eht_mcs_nss_supp; 3538 3539 /* get the max nss for EHT over all possible bandwidths and mcs */ 3540 for (i = 0; i < sizeof(struct ieee80211_eht_mcs_nss_supp); i++) 3541 eht_rx_nss = max_t(u8, eht_rx_nss, 3542 u8_get_bits(rx_nss_mcs[i], 3543 IEEE80211_EHT_MCS_NSS_RX)); 3544 } 3545 3546 if (link_sta->pub->he_cap.has_he) { 3547 int i; 3548 u8 rx_mcs_80 = 0, rx_mcs_160 = 0; 3549 const struct ieee80211_sta_he_cap *he_cap = &link_sta->pub->he_cap; 3550 u16 mcs_160_map = 3551 le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160); 3552 u16 mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80); 3553 3554 for (i = 7; i >= 0; i--) { 3555 u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3; 3556 3557 if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) { 3558 rx_mcs_160 = i + 1; 3559 break; 3560 } 3561 } 3562 for (i = 7; i >= 0; i--) { 3563 u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3; 3564 3565 if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) { 3566 rx_mcs_80 = i + 1; 3567 break; 3568 } 3569 } 3570 3571 support_160 = he_cap->he_cap_elem.phy_cap_info[0] & 3572 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G; 3573 3574 if (support_160) 3575 he_rx_nss = min(rx_mcs_80, rx_mcs_160); 3576 else 3577 he_rx_nss = rx_mcs_80; 3578 } 3579 3580 if (link_sta->pub->ht_cap.ht_supported) { 3581 if (link_sta->pub->ht_cap.mcs.rx_mask[0]) 3582 ht_rx_nss++; 3583 if (link_sta->pub->ht_cap.mcs.rx_mask[1]) 3584 ht_rx_nss++; 3585 if (link_sta->pub->ht_cap.mcs.rx_mask[2]) 3586 ht_rx_nss++; 3587 if (link_sta->pub->ht_cap.mcs.rx_mask[3]) 3588 ht_rx_nss++; 3589 /* FIXME: consider rx_highest? */ 3590 } 3591 3592 if (link_sta->pub->vht_cap.vht_supported) { 3593 int i; 3594 u16 rx_mcs_map; 3595 3596 rx_mcs_map = le16_to_cpu(link_sta->pub->vht_cap.vht_mcs.rx_mcs_map); 3597 3598 for (i = 7; i >= 0; i--) { 3599 u8 mcs = (rx_mcs_map >> (2 * i)) & 3; 3600 3601 if (mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) { 3602 vht_rx_nss = i + 1; 3603 break; 3604 } 3605 } 3606 /* FIXME: consider rx_highest? */ 3607 } 3608 3609 rx_nss = max(vht_rx_nss, ht_rx_nss); 3610 rx_nss = max(he_rx_nss, rx_nss); 3611 rx_nss = max(eht_rx_nss, rx_nss); 3612 rx_nss = max_t(u8, 1, rx_nss); 3613 3614 return rx_nss; 3615 } 3616 3617 void ieee80211_sta_init_nss_bw_capa(struct link_sta_info *link_sta, 3618 struct cfg80211_chan_def *chandef) 3619 { 3620 /* 3621 * TODO: The entirety of the STA Tx/Rx bandwidth handling 3622 * assumes 20MHz based widths, so for now don't initialise 3623 * pubsta->bandwidth for S1G bands. Since enum 3624 * ieee80211_sta_rx_bandwidth is ordered, we will probably 3625 * need to introduce ieee80211_s1g_sta_rx_bandwidth with 3626 * S1G widths and associated S1G specific code. Additionally, 3627 * existing S1G hardware is all 1SS, in the future if hardware 3628 * starts supporting >1SS this should be implemented in 3629 * ieee80211_sta_nss_capability(). 3630 */ 3631 if (cfg80211_chandef_is_s1g(chandef)) { 3632 link_sta->capa_nss = 1; 3633 link_sta->pub->rx_nss = 1; 3634 return; 3635 } 3636 3637 link_sta->capa_nss = ieee80211_sta_nss_capability(link_sta); 3638 link_sta->pub->rx_nss = link_sta->capa_nss; 3639 3640 link_sta->pub->bandwidth = 3641 ieee80211_sta_current_bw(link_sta, chandef, 3642 IEEE80211_STA_BW_TX_TO_STA); 3643 } 3644 3645 void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta, 3646 const u8 *ext_capab, 3647 unsigned int ext_capab_len) 3648 { 3649 u8 val; 3650 3651 sta->sta.max_amsdu_subframes = 0; 3652 3653 if (ext_capab_len < 8) 3654 return; 3655 3656 /* The sender might not have sent the last bit, consider it to be 0 */ 3657 val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB); 3658 3659 /* we did get all the bits, take the MSB as well */ 3660 if (ext_capab_len >= 9) 3661 val |= u8_get_bits(ext_capab[8], 3662 WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1; 3663 3664 if (val) 3665 sta->sta.max_amsdu_subframes = 4 << (4 - val); 3666 } 3667 3668 #ifdef CONFIG_LOCKDEP 3669 bool lockdep_sta_mutex_held(struct ieee80211_sta *pubsta) 3670 { 3671 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 3672 3673 return lockdep_is_held(&sta->local->hw.wiphy->mtx); 3674 } 3675 EXPORT_SYMBOL(lockdep_sta_mutex_held); 3676 #endif 3677 3678 /** 3679 * ieee80211_sta_bw_capability - get STA's bandwidth capability 3680 * @link_sta: the (link) STA to get the capability for 3681 * @band: the band to get the capability on 3682 * 3683 * Return: the maximum bandwidth supported by the STA 3684 */ 3685 static enum ieee80211_sta_rx_bandwidth 3686 ieee80211_sta_bw_capability(struct link_sta_info *link_sta, 3687 enum nl80211_band band) 3688 { 3689 struct ieee80211_sta_vht_cap *vht_cap = &link_sta->pub->vht_cap; 3690 struct ieee80211_sta_he_cap *he_cap = &link_sta->pub->he_cap; 3691 struct ieee80211_sta_eht_cap *eht_cap = &link_sta->pub->eht_cap; 3692 u32 cap_width; 3693 3694 if (he_cap->has_he) { 3695 u8 info; 3696 3697 if (eht_cap->has_eht && band == NL80211_BAND_6GHZ) { 3698 info = eht_cap->eht_cap_elem.phy_cap_info[0]; 3699 3700 if (info & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ) 3701 return IEEE80211_STA_RX_BW_320; 3702 } 3703 3704 info = he_cap->he_cap_elem.phy_cap_info[0]; 3705 3706 if (band == NL80211_BAND_2GHZ) { 3707 if (info & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G) 3708 return IEEE80211_STA_RX_BW_40; 3709 return IEEE80211_STA_RX_BW_20; 3710 } 3711 3712 if (info & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G || 3713 info & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) 3714 return IEEE80211_STA_RX_BW_160; 3715 3716 if (info & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G) 3717 return IEEE80211_STA_RX_BW_80; 3718 3719 return IEEE80211_STA_RX_BW_20; 3720 } 3721 3722 if (!vht_cap->vht_supported) 3723 return link_sta->pub->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 ? 3724 IEEE80211_STA_RX_BW_40 : 3725 IEEE80211_STA_RX_BW_20; 3726 3727 cap_width = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK; 3728 3729 if (cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ || 3730 cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) 3731 return IEEE80211_STA_RX_BW_160; 3732 3733 /* 3734 * If this is non-zero, then it does support 160 MHz after all, 3735 * in one form or the other. We don't distinguish here (or even 3736 * above) between 160 and 80+80 yet. 3737 */ 3738 if (vht_cap->cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) 3739 return IEEE80211_STA_RX_BW_160; 3740 3741 return IEEE80211_STA_RX_BW_80; 3742 } 3743 3744 /** 3745 * ieee80211_sta_usable_bw - get STA's usable bandwidth capability 3746 * @link_sta: the (link) STA to get the capability for 3747 * @band: the band to get the capability on 3748 * 3749 * If the STA is on an AP interface, take into account the AP's 3750 * bandwidth corresponding to this station's PHY capability 3751 * 3752 * Return: the maximum bandwidth supported by the STA on the 3753 * connection to the interface it's connected to 3754 */ 3755 static enum ieee80211_sta_rx_bandwidth 3756 ieee80211_sta_usable_bw(struct link_sta_info *link_sta, 3757 enum nl80211_band band) 3758 { 3759 struct ieee80211_sub_if_data *sdata = link_sta->sta->sdata; 3760 enum ieee80211_sta_rx_bandwidth bw; 3761 struct ieee80211_link_data *link; 3762 3763 bw = ieee80211_sta_bw_capability(link_sta, band); 3764 3765 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 3766 sdata = get_bss_sdata(sdata); 3767 3768 /* for a STA to exist on VLAN, it must have AP */ 3769 if (WARN_ON(!sdata)) 3770 return IEEE80211_STA_RX_BW_20; 3771 } 3772 3773 if (sdata->vif.type != NL80211_IFTYPE_AP) 3774 return bw; 3775 3776 /* for a link STA to exist, vif must have the link */ 3777 link = sdata_dereference(sdata->link[link_sta->link_id], sdata); 3778 if (WARN_ON(!link)) 3779 return IEEE80211_STA_RX_BW_20; 3780 3781 if (!link_sta->pub->eht_cap.has_eht) 3782 return min(bw, link->bss_bw.he_and_lower); 3783 3784 if (!link_sta->pub->uhr_cap.has_uhr || 3785 !link_sta->uhr_dbe_enabled) 3786 return min(bw, link->bss_bw.eht); 3787 3788 return bw; 3789 } 3790 3791 static enum ieee80211_sta_rx_bandwidth 3792 ieee80211_sta_current_bw_rx_from_sta(struct link_sta_info *link_sta, 3793 struct cfg80211_chan_def *chandef) 3794 { 3795 /* 3796 * Take RX OMI into account. The value "rx_omi_bw_rx" is what 3797 * we've indicated to the STA we can currently receive. 3798 * 3799 * This is needed since the RX OMI is done by us to save power, 3800 * requiring changing both our TX (rate control) and RX (chanctx), 3801 * which in turn needs to be done in the right order (stop TX 3802 * at a higher bandwidth first while reducing bandwidth, and 3803 * change the chanctx only after the peer accepts, etc.) 3804 */ 3805 return min(ieee80211_sta_usable_bw(link_sta, chandef->chan->band), 3806 link_sta->rx_omi_bw_rx); 3807 } 3808 3809 static enum ieee80211_sta_rx_bandwidth 3810 ieee80211_sta_current_bw_tx_to_sta(struct link_sta_info *link_sta, 3811 struct cfg80211_chan_def *chandef) 3812 { 3813 struct sta_info *sta = link_sta->sta; 3814 enum nl80211_chan_width bss_width; 3815 enum ieee80211_sta_rx_bandwidth bw; 3816 enum nl80211_band band; 3817 3818 bss_width = chandef->width; 3819 band = chandef->chan->band; 3820 3821 bw = ieee80211_sta_usable_bw(link_sta, band); 3822 bw = min(bw, link_sta->op_mode_bw); 3823 /* also limit to RX OMI bandwidth we TX to the STA */ 3824 bw = min(bw, link_sta->rx_omi_bw_tx); 3825 /* and UHR DBE transition limits */ 3826 bw = min(bw, link_sta->uhr_usable_tx_width); 3827 3828 /* Don't consider AP's bandwidth for TDLS peers, section 11.23.1 of 3829 * IEEE80211-2016 specification makes higher bandwidth operation 3830 * possible on the TDLS link if the peers have wider bandwidth 3831 * capability. 3832 * 3833 * However, in this case, and only if the TDLS peer is authorized, 3834 * limit to the tdls_chandef so that the configuration here isn't 3835 * wider than what's actually requested on the channel context. 3836 */ 3837 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) && 3838 test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW) && 3839 test_sta_flag(sta, WLAN_STA_AUTHORIZED) && 3840 sta->tdls_chandef.chan) 3841 bw = min(bw, ieee80211_chan_width_to_rx_bw(sta->tdls_chandef.width)); 3842 else 3843 bw = min(bw, ieee80211_chan_width_to_rx_bw(bss_width)); 3844 3845 return bw; 3846 } 3847 3848 /** 3849 * ieee80211_sta_current_bw - get STA's current usable bandwidth 3850 * @link_sta: the (link) STA to get the bandwidth for 3851 * @chandef: the chandef for the channel the STA is on 3852 * @direction: the direction (to or from STA) 3853 * 3854 * Return: the maximum bandwidth that the station can/may 3855 * (currently) use in the given direction 3856 */ 3857 enum ieee80211_sta_rx_bandwidth 3858 ieee80211_sta_current_bw(struct link_sta_info *link_sta, 3859 struct cfg80211_chan_def *chandef, 3860 enum ieee80211_sta_bw_direction direction) 3861 { 3862 if (WARN_ON(!chandef)) 3863 return IEEE80211_STA_RX_BW_20; 3864 3865 switch (direction) { 3866 case IEEE80211_STA_BW_RX_FROM_STA: 3867 return ieee80211_sta_current_bw_rx_from_sta(link_sta, chandef); 3868 case IEEE80211_STA_BW_TX_TO_STA: 3869 return ieee80211_sta_current_bw_tx_to_sta(link_sta, chandef); 3870 } 3871 3872 /* unreachable */ 3873 return IEEE80211_STA_RX_BW_20; 3874 } 3875 3876 bool ieee80211_link_sta_update_rc_bw(struct ieee80211_link_data *link, 3877 struct link_sta_info *link_sta) 3878 { 3879 struct ieee80211_sub_if_data *sdata = link->sdata; 3880 struct ieee80211_supported_band *sband; 3881 enum ieee80211_sta_rx_bandwidth new_bw; 3882 enum nl80211_band band; 3883 3884 band = link->conf->chanreq.oper.chan->band; 3885 sband = sdata->local->hw.wiphy->bands[band]; 3886 3887 new_bw = ieee80211_sta_current_bw(link_sta, &link->conf->chanreq.oper, 3888 IEEE80211_STA_BW_TX_TO_STA); 3889 if (link_sta->pub->bandwidth == new_bw) 3890 return false; 3891 3892 link_sta->pub->bandwidth = new_bw; 3893 rate_control_rate_update(sdata->local, sband, link_sta, 3894 IEEE80211_RC_BW_CHANGED); 3895 3896 return true; 3897 } 3898