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