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