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