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