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