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