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