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-2022 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 void __sta_info_destroy_part2(struct sta_info *sta) 1278 { 1279 struct ieee80211_local *local = sta->local; 1280 struct ieee80211_sub_if_data *sdata = sta->sdata; 1281 struct station_info *sinfo; 1282 int ret; 1283 1284 /* 1285 * NOTE: This assumes at least synchronize_net() was done 1286 * after _part1 and before _part2! 1287 */ 1288 1289 might_sleep(); 1290 lockdep_assert_held(&local->sta_mtx); 1291 1292 if (sta->sta_state == IEEE80211_STA_AUTHORIZED) { 1293 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC); 1294 WARN_ON_ONCE(ret); 1295 } 1296 1297 /* now keys can no longer be reached */ 1298 ieee80211_free_sta_keys(local, sta); 1299 1300 /* disable TIM bit - last chance to tell driver */ 1301 __sta_info_recalc_tim(sta, true); 1302 1303 sta->dead = true; 1304 1305 local->num_sta--; 1306 local->sta_generation++; 1307 1308 while (sta->sta_state > IEEE80211_STA_NONE) { 1309 ret = sta_info_move_state(sta, sta->sta_state - 1); 1310 if (ret) { 1311 WARN_ON_ONCE(1); 1312 break; 1313 } 1314 } 1315 1316 if (sta->uploaded) { 1317 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE, 1318 IEEE80211_STA_NOTEXIST); 1319 WARN_ON_ONCE(ret != 0); 1320 } 1321 1322 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr); 1323 1324 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL); 1325 if (sinfo) 1326 sta_set_sinfo(sta, sinfo, true); 1327 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL); 1328 kfree(sinfo); 1329 1330 ieee80211_sta_debugfs_remove(sta); 1331 1332 ieee80211_destroy_frag_cache(&sta->frags); 1333 1334 cleanup_single_sta(sta); 1335 } 1336 1337 int __must_check __sta_info_destroy(struct sta_info *sta) 1338 { 1339 int err = __sta_info_destroy_part1(sta); 1340 1341 if (err) 1342 return err; 1343 1344 synchronize_net(); 1345 1346 __sta_info_destroy_part2(sta); 1347 1348 return 0; 1349 } 1350 1351 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) 1352 { 1353 struct sta_info *sta; 1354 int ret; 1355 1356 mutex_lock(&sdata->local->sta_mtx); 1357 sta = sta_info_get(sdata, addr); 1358 ret = __sta_info_destroy(sta); 1359 mutex_unlock(&sdata->local->sta_mtx); 1360 1361 return ret; 1362 } 1363 1364 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, 1365 const u8 *addr) 1366 { 1367 struct sta_info *sta; 1368 int ret; 1369 1370 mutex_lock(&sdata->local->sta_mtx); 1371 sta = sta_info_get_bss(sdata, addr); 1372 ret = __sta_info_destroy(sta); 1373 mutex_unlock(&sdata->local->sta_mtx); 1374 1375 return ret; 1376 } 1377 1378 static void sta_info_cleanup(struct timer_list *t) 1379 { 1380 struct ieee80211_local *local = from_timer(local, t, sta_cleanup); 1381 struct sta_info *sta; 1382 bool timer_needed = false; 1383 1384 rcu_read_lock(); 1385 list_for_each_entry_rcu(sta, &local->sta_list, list) 1386 if (sta_info_cleanup_expire_buffered(local, sta)) 1387 timer_needed = true; 1388 rcu_read_unlock(); 1389 1390 if (local->quiescing) 1391 return; 1392 1393 if (!timer_needed) 1394 return; 1395 1396 mod_timer(&local->sta_cleanup, 1397 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 1398 } 1399 1400 int sta_info_init(struct ieee80211_local *local) 1401 { 1402 int err; 1403 1404 err = rhltable_init(&local->sta_hash, &sta_rht_params); 1405 if (err) 1406 return err; 1407 1408 err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params); 1409 if (err) { 1410 rhltable_destroy(&local->sta_hash); 1411 return err; 1412 } 1413 1414 spin_lock_init(&local->tim_lock); 1415 mutex_init(&local->sta_mtx); 1416 INIT_LIST_HEAD(&local->sta_list); 1417 1418 timer_setup(&local->sta_cleanup, sta_info_cleanup, 0); 1419 return 0; 1420 } 1421 1422 void sta_info_stop(struct ieee80211_local *local) 1423 { 1424 del_timer_sync(&local->sta_cleanup); 1425 rhltable_destroy(&local->sta_hash); 1426 rhltable_destroy(&local->link_sta_hash); 1427 } 1428 1429 1430 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans) 1431 { 1432 struct ieee80211_local *local = sdata->local; 1433 struct sta_info *sta, *tmp; 1434 LIST_HEAD(free_list); 1435 int ret = 0; 1436 1437 might_sleep(); 1438 1439 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP); 1440 WARN_ON(vlans && !sdata->bss); 1441 1442 mutex_lock(&local->sta_mtx); 1443 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 1444 if (sdata == sta->sdata || 1445 (vlans && sdata->bss == sta->sdata->bss)) { 1446 if (!WARN_ON(__sta_info_destroy_part1(sta))) 1447 list_add(&sta->free_list, &free_list); 1448 ret++; 1449 } 1450 } 1451 1452 if (!list_empty(&free_list)) { 1453 synchronize_net(); 1454 list_for_each_entry_safe(sta, tmp, &free_list, free_list) 1455 __sta_info_destroy_part2(sta); 1456 } 1457 mutex_unlock(&local->sta_mtx); 1458 1459 return ret; 1460 } 1461 1462 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 1463 unsigned long exp_time) 1464 { 1465 struct ieee80211_local *local = sdata->local; 1466 struct sta_info *sta, *tmp; 1467 1468 mutex_lock(&local->sta_mtx); 1469 1470 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 1471 unsigned long last_active = ieee80211_sta_last_active(sta); 1472 1473 if (sdata != sta->sdata) 1474 continue; 1475 1476 if (time_is_before_jiffies(last_active + exp_time)) { 1477 sta_dbg(sta->sdata, "expiring inactive STA %pM\n", 1478 sta->sta.addr); 1479 1480 if (ieee80211_vif_is_mesh(&sdata->vif) && 1481 test_sta_flag(sta, WLAN_STA_PS_STA)) 1482 atomic_dec(&sdata->u.mesh.ps.num_sta_ps); 1483 1484 WARN_ON(__sta_info_destroy(sta)); 1485 } 1486 } 1487 1488 mutex_unlock(&local->sta_mtx); 1489 } 1490 1491 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 1492 const u8 *addr, 1493 const u8 *localaddr) 1494 { 1495 struct ieee80211_local *local = hw_to_local(hw); 1496 struct rhlist_head *tmp; 1497 struct sta_info *sta; 1498 1499 /* 1500 * Just return a random station if localaddr is NULL 1501 * ... first in list. 1502 */ 1503 for_each_sta_info(local, addr, sta, tmp) { 1504 if (localaddr && 1505 !ether_addr_equal(sta->sdata->vif.addr, localaddr)) 1506 continue; 1507 if (!sta->uploaded) 1508 return NULL; 1509 return &sta->sta; 1510 } 1511 1512 return NULL; 1513 } 1514 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); 1515 1516 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, 1517 const u8 *addr) 1518 { 1519 struct sta_info *sta; 1520 1521 if (!vif) 1522 return NULL; 1523 1524 sta = sta_info_get_bss(vif_to_sdata(vif), addr); 1525 if (!sta) 1526 return NULL; 1527 1528 if (!sta->uploaded) 1529 return NULL; 1530 1531 return &sta->sta; 1532 } 1533 EXPORT_SYMBOL(ieee80211_find_sta); 1534 1535 /* powersave support code */ 1536 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) 1537 { 1538 struct ieee80211_sub_if_data *sdata = sta->sdata; 1539 struct ieee80211_local *local = sdata->local; 1540 struct sk_buff_head pending; 1541 int filtered = 0, buffered = 0, ac, i; 1542 unsigned long flags; 1543 struct ps_data *ps; 1544 1545 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 1546 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data, 1547 u.ap); 1548 1549 if (sdata->vif.type == NL80211_IFTYPE_AP) 1550 ps = &sdata->bss->ps; 1551 else if (ieee80211_vif_is_mesh(&sdata->vif)) 1552 ps = &sdata->u.mesh.ps; 1553 else 1554 return; 1555 1556 clear_sta_flag(sta, WLAN_STA_SP); 1557 1558 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1); 1559 sta->driver_buffered_tids = 0; 1560 sta->txq_buffered_tids = 0; 1561 1562 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS)) 1563 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 1564 1565 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) { 1566 if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i])) 1567 continue; 1568 1569 schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i])); 1570 } 1571 1572 skb_queue_head_init(&pending); 1573 1574 /* sync with ieee80211_tx_h_unicast_ps_buf */ 1575 spin_lock(&sta->ps_lock); 1576 /* Send all buffered frames to the station */ 1577 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1578 int count = skb_queue_len(&pending), tmp; 1579 1580 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 1581 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending); 1582 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 1583 tmp = skb_queue_len(&pending); 1584 filtered += tmp - count; 1585 count = tmp; 1586 1587 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 1588 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending); 1589 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 1590 tmp = skb_queue_len(&pending); 1591 buffered += tmp - count; 1592 } 1593 1594 ieee80211_add_pending_skbs(local, &pending); 1595 1596 /* now we're no longer in the deliver code */ 1597 clear_sta_flag(sta, WLAN_STA_PS_DELIVER); 1598 1599 /* The station might have polled and then woken up before we responded, 1600 * so clear these flags now to avoid them sticking around. 1601 */ 1602 clear_sta_flag(sta, WLAN_STA_PSPOLL); 1603 clear_sta_flag(sta, WLAN_STA_UAPSD); 1604 spin_unlock(&sta->ps_lock); 1605 1606 atomic_dec(&ps->num_sta_ps); 1607 1608 local->total_ps_buffered -= buffered; 1609 1610 sta_info_recalc_tim(sta); 1611 1612 ps_dbg(sdata, 1613 "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n", 1614 sta->sta.addr, sta->sta.aid, filtered, buffered); 1615 1616 ieee80211_check_fast_xmit(sta); 1617 } 1618 1619 static void ieee80211_send_null_response(struct sta_info *sta, int tid, 1620 enum ieee80211_frame_release_type reason, 1621 bool call_driver, bool more_data) 1622 { 1623 struct ieee80211_sub_if_data *sdata = sta->sdata; 1624 struct ieee80211_local *local = sdata->local; 1625 struct ieee80211_qos_hdr *nullfunc; 1626 struct sk_buff *skb; 1627 int size = sizeof(*nullfunc); 1628 __le16 fc; 1629 bool qos = sta->sta.wme; 1630 struct ieee80211_tx_info *info; 1631 struct ieee80211_chanctx_conf *chanctx_conf; 1632 1633 if (qos) { 1634 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1635 IEEE80211_STYPE_QOS_NULLFUNC | 1636 IEEE80211_FCTL_FROMDS); 1637 } else { 1638 size -= 2; 1639 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1640 IEEE80211_STYPE_NULLFUNC | 1641 IEEE80211_FCTL_FROMDS); 1642 } 1643 1644 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size); 1645 if (!skb) 1646 return; 1647 1648 skb_reserve(skb, local->hw.extra_tx_headroom); 1649 1650 nullfunc = skb_put(skb, size); 1651 nullfunc->frame_control = fc; 1652 nullfunc->duration_id = 0; 1653 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN); 1654 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN); 1655 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN); 1656 nullfunc->seq_ctrl = 0; 1657 1658 skb->priority = tid; 1659 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]); 1660 if (qos) { 1661 nullfunc->qos_ctrl = cpu_to_le16(tid); 1662 1663 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) { 1664 nullfunc->qos_ctrl |= 1665 cpu_to_le16(IEEE80211_QOS_CTL_EOSP); 1666 if (more_data) 1667 nullfunc->frame_control |= 1668 cpu_to_le16(IEEE80211_FCTL_MOREDATA); 1669 } 1670 } 1671 1672 info = IEEE80211_SKB_CB(skb); 1673 1674 /* 1675 * Tell TX path to send this frame even though the 1676 * STA may still remain is PS mode after this frame 1677 * exchange. Also set EOSP to indicate this packet 1678 * ends the poll/service period. 1679 */ 1680 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER | 1681 IEEE80211_TX_STATUS_EOSP | 1682 IEEE80211_TX_CTL_REQ_TX_STATUS; 1683 1684 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE; 1685 1686 if (call_driver) 1687 drv_allow_buffered_frames(local, sta, BIT(tid), 1, 1688 reason, false); 1689 1690 skb->dev = sdata->dev; 1691 1692 rcu_read_lock(); 1693 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf); 1694 if (WARN_ON(!chanctx_conf)) { 1695 rcu_read_unlock(); 1696 kfree_skb(skb); 1697 return; 1698 } 1699 1700 info->band = chanctx_conf->def.chan->band; 1701 ieee80211_xmit(sdata, sta, skb); 1702 rcu_read_unlock(); 1703 } 1704 1705 static int find_highest_prio_tid(unsigned long tids) 1706 { 1707 /* lower 3 TIDs aren't ordered perfectly */ 1708 if (tids & 0xF8) 1709 return fls(tids) - 1; 1710 /* TID 0 is BE just like TID 3 */ 1711 if (tids & BIT(0)) 1712 return 0; 1713 return fls(tids) - 1; 1714 } 1715 1716 /* Indicates if the MORE_DATA bit should be set in the last 1717 * frame obtained by ieee80211_sta_ps_get_frames. 1718 * Note that driver_release_tids is relevant only if 1719 * reason = IEEE80211_FRAME_RELEASE_PSPOLL 1720 */ 1721 static bool 1722 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs, 1723 enum ieee80211_frame_release_type reason, 1724 unsigned long driver_release_tids) 1725 { 1726 int ac; 1727 1728 /* If the driver has data on more than one TID then 1729 * certainly there's more data if we release just a 1730 * single frame now (from a single TID). This will 1731 * only happen for PS-Poll. 1732 */ 1733 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL && 1734 hweight16(driver_release_tids) > 1) 1735 return true; 1736 1737 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1738 if (ignored_acs & ieee80211_ac_to_qos_mask[ac]) 1739 continue; 1740 1741 if (!skb_queue_empty(&sta->tx_filtered[ac]) || 1742 !skb_queue_empty(&sta->ps_tx_buf[ac])) 1743 return true; 1744 } 1745 1746 return false; 1747 } 1748 1749 static void 1750 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs, 1751 enum ieee80211_frame_release_type reason, 1752 struct sk_buff_head *frames, 1753 unsigned long *driver_release_tids) 1754 { 1755 struct ieee80211_sub_if_data *sdata = sta->sdata; 1756 struct ieee80211_local *local = sdata->local; 1757 int ac; 1758 1759 /* Get response frame(s) and more data bit for the last one. */ 1760 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1761 unsigned long tids; 1762 1763 if (ignored_acs & ieee80211_ac_to_qos_mask[ac]) 1764 continue; 1765 1766 tids = ieee80211_tids_for_ac(ac); 1767 1768 /* if we already have frames from software, then we can't also 1769 * release from hardware queues 1770 */ 1771 if (skb_queue_empty(frames)) { 1772 *driver_release_tids |= 1773 sta->driver_buffered_tids & tids; 1774 *driver_release_tids |= sta->txq_buffered_tids & tids; 1775 } 1776 1777 if (!*driver_release_tids) { 1778 struct sk_buff *skb; 1779 1780 while (n_frames > 0) { 1781 skb = skb_dequeue(&sta->tx_filtered[ac]); 1782 if (!skb) { 1783 skb = skb_dequeue( 1784 &sta->ps_tx_buf[ac]); 1785 if (skb) 1786 local->total_ps_buffered--; 1787 } 1788 if (!skb) 1789 break; 1790 n_frames--; 1791 __skb_queue_tail(frames, skb); 1792 } 1793 } 1794 1795 /* If we have more frames buffered on this AC, then abort the 1796 * loop since we can't send more data from other ACs before 1797 * the buffered frames from this. 1798 */ 1799 if (!skb_queue_empty(&sta->tx_filtered[ac]) || 1800 !skb_queue_empty(&sta->ps_tx_buf[ac])) 1801 break; 1802 } 1803 } 1804 1805 static void 1806 ieee80211_sta_ps_deliver_response(struct sta_info *sta, 1807 int n_frames, u8 ignored_acs, 1808 enum ieee80211_frame_release_type reason) 1809 { 1810 struct ieee80211_sub_if_data *sdata = sta->sdata; 1811 struct ieee80211_local *local = sdata->local; 1812 unsigned long driver_release_tids = 0; 1813 struct sk_buff_head frames; 1814 bool more_data; 1815 1816 /* Service or PS-Poll period starts */ 1817 set_sta_flag(sta, WLAN_STA_SP); 1818 1819 __skb_queue_head_init(&frames); 1820 1821 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason, 1822 &frames, &driver_release_tids); 1823 1824 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids); 1825 1826 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL) 1827 driver_release_tids = 1828 BIT(find_highest_prio_tid(driver_release_tids)); 1829 1830 if (skb_queue_empty(&frames) && !driver_release_tids) { 1831 int tid, ac; 1832 1833 /* 1834 * For PS-Poll, this can only happen due to a race condition 1835 * when we set the TIM bit and the station notices it, but 1836 * before it can poll for the frame we expire it. 1837 * 1838 * For uAPSD, this is said in the standard (11.2.1.5 h): 1839 * At each unscheduled SP for a non-AP STA, the AP shall 1840 * attempt to transmit at least one MSDU or MMPDU, but no 1841 * more than the value specified in the Max SP Length field 1842 * in the QoS Capability element from delivery-enabled ACs, 1843 * that are destined for the non-AP STA. 1844 * 1845 * Since we have no other MSDU/MMPDU, transmit a QoS null frame. 1846 */ 1847 1848 /* This will evaluate to 1, 3, 5 or 7. */ 1849 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++) 1850 if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac])) 1851 break; 1852 tid = 7 - 2 * ac; 1853 1854 ieee80211_send_null_response(sta, tid, reason, true, false); 1855 } else if (!driver_release_tids) { 1856 struct sk_buff_head pending; 1857 struct sk_buff *skb; 1858 int num = 0; 1859 u16 tids = 0; 1860 bool need_null = false; 1861 1862 skb_queue_head_init(&pending); 1863 1864 while ((skb = __skb_dequeue(&frames))) { 1865 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 1866 struct ieee80211_hdr *hdr = (void *) skb->data; 1867 u8 *qoshdr = NULL; 1868 1869 num++; 1870 1871 /* 1872 * Tell TX path to send this frame even though the 1873 * STA may still remain is PS mode after this frame 1874 * exchange. 1875 */ 1876 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER; 1877 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE; 1878 1879 /* 1880 * Use MoreData flag to indicate whether there are 1881 * more buffered frames for this STA 1882 */ 1883 if (more_data || !skb_queue_empty(&frames)) 1884 hdr->frame_control |= 1885 cpu_to_le16(IEEE80211_FCTL_MOREDATA); 1886 else 1887 hdr->frame_control &= 1888 cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 1889 1890 if (ieee80211_is_data_qos(hdr->frame_control) || 1891 ieee80211_is_qos_nullfunc(hdr->frame_control)) 1892 qoshdr = ieee80211_get_qos_ctl(hdr); 1893 1894 tids |= BIT(skb->priority); 1895 1896 __skb_queue_tail(&pending, skb); 1897 1898 /* end service period after last frame or add one */ 1899 if (!skb_queue_empty(&frames)) 1900 continue; 1901 1902 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) { 1903 /* for PS-Poll, there's only one frame */ 1904 info->flags |= IEEE80211_TX_STATUS_EOSP | 1905 IEEE80211_TX_CTL_REQ_TX_STATUS; 1906 break; 1907 } 1908 1909 /* For uAPSD, things are a bit more complicated. If the 1910 * last frame has a QoS header (i.e. is a QoS-data or 1911 * QoS-nulldata frame) then just set the EOSP bit there 1912 * and be done. 1913 * If the frame doesn't have a QoS header (which means 1914 * it should be a bufferable MMPDU) then we can't set 1915 * the EOSP bit in the QoS header; add a QoS-nulldata 1916 * frame to the list to send it after the MMPDU. 1917 * 1918 * Note that this code is only in the mac80211-release 1919 * code path, we assume that the driver will not buffer 1920 * anything but QoS-data frames, or if it does, will 1921 * create the QoS-nulldata frame by itself if needed. 1922 * 1923 * Cf. 802.11-2012 10.2.1.10 (c). 1924 */ 1925 if (qoshdr) { 1926 *qoshdr |= IEEE80211_QOS_CTL_EOSP; 1927 1928 info->flags |= IEEE80211_TX_STATUS_EOSP | 1929 IEEE80211_TX_CTL_REQ_TX_STATUS; 1930 } else { 1931 /* The standard isn't completely clear on this 1932 * as it says the more-data bit should be set 1933 * if there are more BUs. The QoS-Null frame 1934 * we're about to send isn't buffered yet, we 1935 * only create it below, but let's pretend it 1936 * was buffered just in case some clients only 1937 * expect more-data=0 when eosp=1. 1938 */ 1939 hdr->frame_control |= 1940 cpu_to_le16(IEEE80211_FCTL_MOREDATA); 1941 need_null = true; 1942 num++; 1943 } 1944 break; 1945 } 1946 1947 drv_allow_buffered_frames(local, sta, tids, num, 1948 reason, more_data); 1949 1950 ieee80211_add_pending_skbs(local, &pending); 1951 1952 if (need_null) 1953 ieee80211_send_null_response( 1954 sta, find_highest_prio_tid(tids), 1955 reason, false, false); 1956 1957 sta_info_recalc_tim(sta); 1958 } else { 1959 int tid; 1960 1961 /* 1962 * We need to release a frame that is buffered somewhere in the 1963 * driver ... it'll have to handle that. 1964 * Note that the driver also has to check the number of frames 1965 * on the TIDs we're releasing from - if there are more than 1966 * n_frames it has to set the more-data bit (if we didn't ask 1967 * it to set it anyway due to other buffered frames); if there 1968 * are fewer than n_frames it has to make sure to adjust that 1969 * to allow the service period to end properly. 1970 */ 1971 drv_release_buffered_frames(local, sta, driver_release_tids, 1972 n_frames, reason, more_data); 1973 1974 /* 1975 * Note that we don't recalculate the TIM bit here as it would 1976 * most likely have no effect at all unless the driver told us 1977 * that the TID(s) became empty before returning here from the 1978 * release function. 1979 * Either way, however, when the driver tells us that the TID(s) 1980 * became empty or we find that a txq became empty, we'll do the 1981 * TIM recalculation. 1982 */ 1983 1984 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) { 1985 if (!sta->sta.txq[tid] || 1986 !(driver_release_tids & BIT(tid)) || 1987 txq_has_queue(sta->sta.txq[tid])) 1988 continue; 1989 1990 sta_info_recalc_tim(sta); 1991 break; 1992 } 1993 } 1994 } 1995 1996 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 1997 { 1998 u8 ignore_for_response = sta->sta.uapsd_queues; 1999 2000 /* 2001 * If all ACs are delivery-enabled then we should reply 2002 * from any of them, if only some are enabled we reply 2003 * only from the non-enabled ones. 2004 */ 2005 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1) 2006 ignore_for_response = 0; 2007 2008 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response, 2009 IEEE80211_FRAME_RELEASE_PSPOLL); 2010 } 2011 2012 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta) 2013 { 2014 int n_frames = sta->sta.max_sp; 2015 u8 delivery_enabled = sta->sta.uapsd_queues; 2016 2017 /* 2018 * If we ever grow support for TSPEC this might happen if 2019 * the TSPEC update from hostapd comes in between a trigger 2020 * frame setting WLAN_STA_UAPSD in the RX path and this 2021 * actually getting called. 2022 */ 2023 if (!delivery_enabled) 2024 return; 2025 2026 switch (sta->sta.max_sp) { 2027 case 1: 2028 n_frames = 2; 2029 break; 2030 case 2: 2031 n_frames = 4; 2032 break; 2033 case 3: 2034 n_frames = 6; 2035 break; 2036 case 0: 2037 /* XXX: what is a good value? */ 2038 n_frames = 128; 2039 break; 2040 } 2041 2042 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled, 2043 IEEE80211_FRAME_RELEASE_UAPSD); 2044 } 2045 2046 void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 2047 struct ieee80211_sta *pubsta, bool block) 2048 { 2049 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 2050 2051 trace_api_sta_block_awake(sta->local, pubsta, block); 2052 2053 if (block) { 2054 set_sta_flag(sta, WLAN_STA_PS_DRIVER); 2055 ieee80211_clear_fast_xmit(sta); 2056 return; 2057 } 2058 2059 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER)) 2060 return; 2061 2062 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) { 2063 set_sta_flag(sta, WLAN_STA_PS_DELIVER); 2064 clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 2065 ieee80211_queue_work(hw, &sta->drv_deliver_wk); 2066 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) || 2067 test_sta_flag(sta, WLAN_STA_UAPSD)) { 2068 /* must be asleep in this case */ 2069 clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 2070 ieee80211_queue_work(hw, &sta->drv_deliver_wk); 2071 } else { 2072 clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 2073 ieee80211_check_fast_xmit(sta); 2074 } 2075 } 2076 EXPORT_SYMBOL(ieee80211_sta_block_awake); 2077 2078 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta) 2079 { 2080 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 2081 struct ieee80211_local *local = sta->local; 2082 2083 trace_api_eosp(local, pubsta); 2084 2085 clear_sta_flag(sta, WLAN_STA_SP); 2086 } 2087 EXPORT_SYMBOL(ieee80211_sta_eosp); 2088 2089 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid) 2090 { 2091 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 2092 enum ieee80211_frame_release_type reason; 2093 bool more_data; 2094 2095 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid); 2096 2097 reason = IEEE80211_FRAME_RELEASE_UAPSD; 2098 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues, 2099 reason, 0); 2100 2101 ieee80211_send_null_response(sta, tid, reason, false, more_data); 2102 } 2103 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc); 2104 2105 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, 2106 u8 tid, bool buffered) 2107 { 2108 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 2109 2110 if (WARN_ON(tid >= IEEE80211_NUM_TIDS)) 2111 return; 2112 2113 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered); 2114 2115 if (buffered) 2116 set_bit(tid, &sta->driver_buffered_tids); 2117 else 2118 clear_bit(tid, &sta->driver_buffered_tids); 2119 2120 sta_info_recalc_tim(sta); 2121 } 2122 EXPORT_SYMBOL(ieee80211_sta_set_buffered); 2123 2124 void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid, 2125 u32 tx_airtime, u32 rx_airtime) 2126 { 2127 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 2128 struct ieee80211_local *local = sta->sdata->local; 2129 u8 ac = ieee80211_ac_from_tid(tid); 2130 u32 airtime = 0; 2131 u32 diff; 2132 2133 if (sta->local->airtime_flags & AIRTIME_USE_TX) 2134 airtime += tx_airtime; 2135 if (sta->local->airtime_flags & AIRTIME_USE_RX) 2136 airtime += rx_airtime; 2137 2138 spin_lock_bh(&local->active_txq_lock[ac]); 2139 sta->airtime[ac].tx_airtime += tx_airtime; 2140 sta->airtime[ac].rx_airtime += rx_airtime; 2141 2142 diff = (u32)jiffies - sta->airtime[ac].last_active; 2143 if (diff <= AIRTIME_ACTIVE_DURATION) 2144 sta->airtime[ac].deficit -= airtime; 2145 2146 spin_unlock_bh(&local->active_txq_lock[ac]); 2147 } 2148 EXPORT_SYMBOL(ieee80211_sta_register_airtime); 2149 2150 void __ieee80211_sta_recalc_aggregates(struct sta_info *sta, u16 active_links) 2151 { 2152 bool first = true; 2153 int link_id; 2154 2155 if (!sta->sta.valid_links || !sta->sta.mlo) { 2156 sta->sta.cur = &sta->sta.deflink.agg; 2157 return; 2158 } 2159 2160 rcu_read_lock(); 2161 for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) { 2162 struct ieee80211_link_sta *link_sta; 2163 int i; 2164 2165 if (!(active_links & BIT(link_id))) 2166 continue; 2167 2168 link_sta = rcu_dereference(sta->sta.link[link_id]); 2169 if (!link_sta) 2170 continue; 2171 2172 if (first) { 2173 sta->cur = sta->sta.deflink.agg; 2174 first = false; 2175 continue; 2176 } 2177 2178 sta->cur.max_amsdu_len = 2179 min(sta->cur.max_amsdu_len, 2180 link_sta->agg.max_amsdu_len); 2181 sta->cur.max_rc_amsdu_len = 2182 min(sta->cur.max_rc_amsdu_len, 2183 link_sta->agg.max_rc_amsdu_len); 2184 2185 for (i = 0; i < ARRAY_SIZE(sta->cur.max_tid_amsdu_len); i++) 2186 sta->cur.max_tid_amsdu_len[i] = 2187 min(sta->cur.max_tid_amsdu_len[i], 2188 link_sta->agg.max_tid_amsdu_len[i]); 2189 } 2190 rcu_read_unlock(); 2191 2192 sta->sta.cur = &sta->cur; 2193 } 2194 2195 void ieee80211_sta_recalc_aggregates(struct ieee80211_sta *pubsta) 2196 { 2197 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 2198 2199 __ieee80211_sta_recalc_aggregates(sta, sta->sdata->vif.active_links); 2200 } 2201 EXPORT_SYMBOL(ieee80211_sta_recalc_aggregates); 2202 2203 void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local, 2204 struct sta_info *sta, u8 ac, 2205 u16 tx_airtime, bool tx_completed) 2206 { 2207 int tx_pending; 2208 2209 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) 2210 return; 2211 2212 if (!tx_completed) { 2213 if (sta) 2214 atomic_add(tx_airtime, 2215 &sta->airtime[ac].aql_tx_pending); 2216 2217 atomic_add(tx_airtime, &local->aql_total_pending_airtime); 2218 atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]); 2219 return; 2220 } 2221 2222 if (sta) { 2223 tx_pending = atomic_sub_return(tx_airtime, 2224 &sta->airtime[ac].aql_tx_pending); 2225 if (tx_pending < 0) 2226 atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending, 2227 tx_pending, 0); 2228 } 2229 2230 atomic_sub(tx_airtime, &local->aql_total_pending_airtime); 2231 tx_pending = atomic_sub_return(tx_airtime, 2232 &local->aql_ac_pending_airtime[ac]); 2233 if (WARN_ONCE(tx_pending < 0, 2234 "Device %s AC %d pending airtime underflow: %u, %u", 2235 wiphy_name(local->hw.wiphy), ac, tx_pending, 2236 tx_airtime)) { 2237 atomic_cmpxchg(&local->aql_ac_pending_airtime[ac], 2238 tx_pending, 0); 2239 atomic_sub(tx_pending, &local->aql_total_pending_airtime); 2240 } 2241 } 2242 2243 int sta_info_move_state(struct sta_info *sta, 2244 enum ieee80211_sta_state new_state) 2245 { 2246 might_sleep(); 2247 2248 if (sta->sta_state == new_state) 2249 return 0; 2250 2251 /* check allowed transitions first */ 2252 2253 switch (new_state) { 2254 case IEEE80211_STA_NONE: 2255 if (sta->sta_state != IEEE80211_STA_AUTH) 2256 return -EINVAL; 2257 break; 2258 case IEEE80211_STA_AUTH: 2259 if (sta->sta_state != IEEE80211_STA_NONE && 2260 sta->sta_state != IEEE80211_STA_ASSOC) 2261 return -EINVAL; 2262 break; 2263 case IEEE80211_STA_ASSOC: 2264 if (sta->sta_state != IEEE80211_STA_AUTH && 2265 sta->sta_state != IEEE80211_STA_AUTHORIZED) 2266 return -EINVAL; 2267 break; 2268 case IEEE80211_STA_AUTHORIZED: 2269 if (sta->sta_state != IEEE80211_STA_ASSOC) 2270 return -EINVAL; 2271 break; 2272 default: 2273 WARN(1, "invalid state %d", new_state); 2274 return -EINVAL; 2275 } 2276 2277 sta_dbg(sta->sdata, "moving STA %pM to state %d\n", 2278 sta->sta.addr, new_state); 2279 2280 /* 2281 * notify the driver before the actual changes so it can 2282 * fail the transition 2283 */ 2284 if (test_sta_flag(sta, WLAN_STA_INSERTED)) { 2285 int err = drv_sta_state(sta->local, sta->sdata, sta, 2286 sta->sta_state, new_state); 2287 if (err) 2288 return err; 2289 } 2290 2291 /* reflect the change in all state variables */ 2292 2293 switch (new_state) { 2294 case IEEE80211_STA_NONE: 2295 if (sta->sta_state == IEEE80211_STA_AUTH) 2296 clear_bit(WLAN_STA_AUTH, &sta->_flags); 2297 break; 2298 case IEEE80211_STA_AUTH: 2299 if (sta->sta_state == IEEE80211_STA_NONE) { 2300 set_bit(WLAN_STA_AUTH, &sta->_flags); 2301 } else if (sta->sta_state == IEEE80211_STA_ASSOC) { 2302 clear_bit(WLAN_STA_ASSOC, &sta->_flags); 2303 ieee80211_recalc_min_chandef(sta->sdata, -1); 2304 if (!sta->sta.support_p2p_ps) 2305 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata); 2306 } 2307 break; 2308 case IEEE80211_STA_ASSOC: 2309 if (sta->sta_state == IEEE80211_STA_AUTH) { 2310 set_bit(WLAN_STA_ASSOC, &sta->_flags); 2311 sta->assoc_at = ktime_get_boottime_ns(); 2312 ieee80211_recalc_min_chandef(sta->sdata, -1); 2313 if (!sta->sta.support_p2p_ps) 2314 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata); 2315 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) { 2316 ieee80211_vif_dec_num_mcast(sta->sdata); 2317 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 2318 ieee80211_clear_fast_xmit(sta); 2319 ieee80211_clear_fast_rx(sta); 2320 } 2321 break; 2322 case IEEE80211_STA_AUTHORIZED: 2323 if (sta->sta_state == IEEE80211_STA_ASSOC) { 2324 ieee80211_vif_inc_num_mcast(sta->sdata); 2325 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 2326 ieee80211_check_fast_xmit(sta); 2327 ieee80211_check_fast_rx(sta); 2328 } 2329 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN || 2330 sta->sdata->vif.type == NL80211_IFTYPE_AP) 2331 cfg80211_send_layer2_update(sta->sdata->dev, 2332 sta->sta.addr); 2333 break; 2334 default: 2335 break; 2336 } 2337 2338 sta->sta_state = new_state; 2339 2340 return 0; 2341 } 2342 2343 static struct ieee80211_sta_rx_stats * 2344 sta_get_last_rx_stats(struct sta_info *sta) 2345 { 2346 struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats; 2347 int cpu; 2348 2349 if (!sta->deflink.pcpu_rx_stats) 2350 return stats; 2351 2352 for_each_possible_cpu(cpu) { 2353 struct ieee80211_sta_rx_stats *cpustats; 2354 2355 cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu); 2356 2357 if (time_after(cpustats->last_rx, stats->last_rx)) 2358 stats = cpustats; 2359 } 2360 2361 return stats; 2362 } 2363 2364 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate, 2365 struct rate_info *rinfo) 2366 { 2367 rinfo->bw = STA_STATS_GET(BW, rate); 2368 2369 switch (STA_STATS_GET(TYPE, rate)) { 2370 case STA_STATS_RATE_TYPE_VHT: 2371 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS; 2372 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate); 2373 rinfo->nss = STA_STATS_GET(VHT_NSS, rate); 2374 if (STA_STATS_GET(SGI, rate)) 2375 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI; 2376 break; 2377 case STA_STATS_RATE_TYPE_HT: 2378 rinfo->flags = RATE_INFO_FLAGS_MCS; 2379 rinfo->mcs = STA_STATS_GET(HT_MCS, rate); 2380 if (STA_STATS_GET(SGI, rate)) 2381 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI; 2382 break; 2383 case STA_STATS_RATE_TYPE_LEGACY: { 2384 struct ieee80211_supported_band *sband; 2385 u16 brate; 2386 unsigned int shift; 2387 int band = STA_STATS_GET(LEGACY_BAND, rate); 2388 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate); 2389 2390 sband = local->hw.wiphy->bands[band]; 2391 2392 if (WARN_ON_ONCE(!sband->bitrates)) 2393 break; 2394 2395 brate = sband->bitrates[rate_idx].bitrate; 2396 if (rinfo->bw == RATE_INFO_BW_5) 2397 shift = 2; 2398 else if (rinfo->bw == RATE_INFO_BW_10) 2399 shift = 1; 2400 else 2401 shift = 0; 2402 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift); 2403 break; 2404 } 2405 case STA_STATS_RATE_TYPE_HE: 2406 rinfo->flags = RATE_INFO_FLAGS_HE_MCS; 2407 rinfo->mcs = STA_STATS_GET(HE_MCS, rate); 2408 rinfo->nss = STA_STATS_GET(HE_NSS, rate); 2409 rinfo->he_gi = STA_STATS_GET(HE_GI, rate); 2410 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate); 2411 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate); 2412 break; 2413 case STA_STATS_RATE_TYPE_EHT: 2414 rinfo->flags = RATE_INFO_FLAGS_EHT_MCS; 2415 rinfo->mcs = STA_STATS_GET(EHT_MCS, rate); 2416 rinfo->nss = STA_STATS_GET(EHT_NSS, rate); 2417 rinfo->eht_gi = STA_STATS_GET(EHT_GI, rate); 2418 rinfo->eht_ru_alloc = STA_STATS_GET(EHT_RU, rate); 2419 break; 2420 } 2421 } 2422 2423 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo) 2424 { 2425 u32 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate); 2426 2427 if (rate == STA_STATS_RATE_INVALID) 2428 return -EINVAL; 2429 2430 sta_stats_decode_rate(sta->local, rate, rinfo); 2431 return 0; 2432 } 2433 2434 static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats, 2435 int tid) 2436 { 2437 unsigned int start; 2438 u64 value; 2439 2440 do { 2441 start = u64_stats_fetch_begin(&rxstats->syncp); 2442 value = rxstats->msdu[tid]; 2443 } while (u64_stats_fetch_retry(&rxstats->syncp, start)); 2444 2445 return value; 2446 } 2447 2448 static void sta_set_tidstats(struct sta_info *sta, 2449 struct cfg80211_tid_stats *tidstats, 2450 int tid) 2451 { 2452 struct ieee80211_local *local = sta->local; 2453 int cpu; 2454 2455 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) { 2456 tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats, 2457 tid); 2458 2459 if (sta->deflink.pcpu_rx_stats) { 2460 for_each_possible_cpu(cpu) { 2461 struct ieee80211_sta_rx_stats *cpurxs; 2462 2463 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, 2464 cpu); 2465 tidstats->rx_msdu += 2466 sta_get_tidstats_msdu(cpurxs, tid); 2467 } 2468 } 2469 2470 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU); 2471 } 2472 2473 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) { 2474 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU); 2475 tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid]; 2476 } 2477 2478 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) && 2479 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) { 2480 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES); 2481 tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid]; 2482 } 2483 2484 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) && 2485 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) { 2486 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED); 2487 tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid]; 2488 } 2489 2490 if (tid < IEEE80211_NUM_TIDS) { 2491 spin_lock_bh(&local->fq.lock); 2492 rcu_read_lock(); 2493 2494 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS); 2495 ieee80211_fill_txq_stats(&tidstats->txq_stats, 2496 to_txq_info(sta->sta.txq[tid])); 2497 2498 rcu_read_unlock(); 2499 spin_unlock_bh(&local->fq.lock); 2500 } 2501 } 2502 2503 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats) 2504 { 2505 unsigned int start; 2506 u64 value; 2507 2508 do { 2509 start = u64_stats_fetch_begin(&rxstats->syncp); 2510 value = rxstats->bytes; 2511 } while (u64_stats_fetch_retry(&rxstats->syncp, start)); 2512 2513 return value; 2514 } 2515 2516 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo, 2517 bool tidstats) 2518 { 2519 struct ieee80211_sub_if_data *sdata = sta->sdata; 2520 struct ieee80211_local *local = sdata->local; 2521 u32 thr = 0; 2522 int i, ac, cpu; 2523 struct ieee80211_sta_rx_stats *last_rxstats; 2524 2525 last_rxstats = sta_get_last_rx_stats(sta); 2526 2527 sinfo->generation = sdata->local->sta_generation; 2528 2529 /* do before driver, so beacon filtering drivers have a 2530 * chance to e.g. just add the number of filtered beacons 2531 * (or just modify the value entirely, of course) 2532 */ 2533 if (sdata->vif.type == NL80211_IFTYPE_STATION) 2534 sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal; 2535 2536 drv_sta_statistics(local, sdata, &sta->sta, sinfo); 2537 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) | 2538 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) | 2539 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) | 2540 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) | 2541 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) | 2542 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC); 2543 2544 if (sdata->vif.type == NL80211_IFTYPE_STATION) { 2545 sinfo->beacon_loss_count = 2546 sdata->deflink.u.mgd.beacon_loss_count; 2547 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS); 2548 } 2549 2550 sinfo->connected_time = ktime_get_seconds() - sta->last_connected; 2551 sinfo->assoc_at = sta->assoc_at; 2552 sinfo->inactive_time = 2553 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta)); 2554 2555 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) | 2556 BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) { 2557 sinfo->tx_bytes = 0; 2558 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 2559 sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac]; 2560 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64); 2561 } 2562 2563 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) { 2564 sinfo->tx_packets = 0; 2565 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 2566 sinfo->tx_packets += sta->deflink.tx_stats.packets[ac]; 2567 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS); 2568 } 2569 2570 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) | 2571 BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) { 2572 sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats); 2573 2574 if (sta->deflink.pcpu_rx_stats) { 2575 for_each_possible_cpu(cpu) { 2576 struct ieee80211_sta_rx_stats *cpurxs; 2577 2578 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, 2579 cpu); 2580 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs); 2581 } 2582 } 2583 2584 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64); 2585 } 2586 2587 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) { 2588 sinfo->rx_packets = sta->deflink.rx_stats.packets; 2589 if (sta->deflink.pcpu_rx_stats) { 2590 for_each_possible_cpu(cpu) { 2591 struct ieee80211_sta_rx_stats *cpurxs; 2592 2593 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, 2594 cpu); 2595 sinfo->rx_packets += cpurxs->packets; 2596 } 2597 } 2598 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS); 2599 } 2600 2601 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) { 2602 sinfo->tx_retries = sta->deflink.status_stats.retry_count; 2603 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES); 2604 } 2605 2606 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) { 2607 sinfo->tx_failed = sta->deflink.status_stats.retry_failed; 2608 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED); 2609 } 2610 2611 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) { 2612 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 2613 sinfo->rx_duration += sta->airtime[ac].rx_airtime; 2614 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION); 2615 } 2616 2617 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) { 2618 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 2619 sinfo->tx_duration += sta->airtime[ac].tx_airtime; 2620 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION); 2621 } 2622 2623 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) { 2624 sinfo->airtime_weight = sta->airtime_weight; 2625 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT); 2626 } 2627 2628 sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped; 2629 if (sta->deflink.pcpu_rx_stats) { 2630 for_each_possible_cpu(cpu) { 2631 struct ieee80211_sta_rx_stats *cpurxs; 2632 2633 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu); 2634 sinfo->rx_dropped_misc += cpurxs->dropped; 2635 } 2636 } 2637 2638 if (sdata->vif.type == NL80211_IFTYPE_STATION && 2639 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) { 2640 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) | 2641 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG); 2642 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif); 2643 } 2644 2645 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) || 2646 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) { 2647 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) { 2648 sinfo->signal = (s8)last_rxstats->last_signal; 2649 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL); 2650 } 2651 2652 if (!sta->deflink.pcpu_rx_stats && 2653 !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) { 2654 sinfo->signal_avg = 2655 -ewma_signal_read(&sta->deflink.rx_stats_avg.signal); 2656 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG); 2657 } 2658 } 2659 2660 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to 2661 * the sta->rx_stats struct, so the check here is fine with and without 2662 * pcpu statistics 2663 */ 2664 if (last_rxstats->chains && 2665 !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) | 2666 BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) { 2667 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL); 2668 if (!sta->deflink.pcpu_rx_stats) 2669 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG); 2670 2671 sinfo->chains = last_rxstats->chains; 2672 2673 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) { 2674 sinfo->chain_signal[i] = 2675 last_rxstats->chain_signal_last[i]; 2676 sinfo->chain_signal_avg[i] = 2677 -ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]); 2678 } 2679 } 2680 2681 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) && 2682 !sta->sta.valid_links) { 2683 sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate, 2684 &sinfo->txrate); 2685 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); 2686 } 2687 2688 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) && 2689 !sta->sta.valid_links) { 2690 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0) 2691 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE); 2692 } 2693 2694 if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) { 2695 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) 2696 sta_set_tidstats(sta, &sinfo->pertid[i], i); 2697 } 2698 2699 if (ieee80211_vif_is_mesh(&sdata->vif)) { 2700 #ifdef CONFIG_MAC80211_MESH 2701 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) | 2702 BIT_ULL(NL80211_STA_INFO_PLID) | 2703 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) | 2704 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) | 2705 BIT_ULL(NL80211_STA_INFO_PEER_PM) | 2706 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) | 2707 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) | 2708 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS); 2709 2710 sinfo->llid = sta->mesh->llid; 2711 sinfo->plid = sta->mesh->plid; 2712 sinfo->plink_state = sta->mesh->plink_state; 2713 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) { 2714 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET); 2715 sinfo->t_offset = sta->mesh->t_offset; 2716 } 2717 sinfo->local_pm = sta->mesh->local_pm; 2718 sinfo->peer_pm = sta->mesh->peer_pm; 2719 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm; 2720 sinfo->connected_to_gate = sta->mesh->connected_to_gate; 2721 sinfo->connected_to_as = sta->mesh->connected_to_as; 2722 #endif 2723 } 2724 2725 sinfo->bss_param.flags = 0; 2726 if (sdata->vif.bss_conf.use_cts_prot) 2727 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT; 2728 if (sdata->vif.bss_conf.use_short_preamble) 2729 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE; 2730 if (sdata->vif.bss_conf.use_short_slot) 2731 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME; 2732 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period; 2733 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int; 2734 2735 sinfo->sta_flags.set = 0; 2736 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) | 2737 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) | 2738 BIT(NL80211_STA_FLAG_WME) | 2739 BIT(NL80211_STA_FLAG_MFP) | 2740 BIT(NL80211_STA_FLAG_AUTHENTICATED) | 2741 BIT(NL80211_STA_FLAG_ASSOCIATED) | 2742 BIT(NL80211_STA_FLAG_TDLS_PEER); 2743 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED)) 2744 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED); 2745 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE)) 2746 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE); 2747 if (sta->sta.wme) 2748 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME); 2749 if (test_sta_flag(sta, WLAN_STA_MFP)) 2750 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP); 2751 if (test_sta_flag(sta, WLAN_STA_AUTH)) 2752 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED); 2753 if (test_sta_flag(sta, WLAN_STA_ASSOC)) 2754 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED); 2755 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) 2756 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER); 2757 2758 thr = sta_get_expected_throughput(sta); 2759 2760 if (thr != 0) { 2761 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT); 2762 sinfo->expected_throughput = thr; 2763 } 2764 2765 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) && 2766 sta->deflink.status_stats.ack_signal_filled) { 2767 sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal; 2768 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL); 2769 } 2770 2771 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) && 2772 sta->deflink.status_stats.ack_signal_filled) { 2773 sinfo->avg_ack_signal = 2774 -(s8)ewma_avg_signal_read( 2775 &sta->deflink.status_stats.avg_ack_signal); 2776 sinfo->filled |= 2777 BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG); 2778 } 2779 2780 if (ieee80211_vif_is_mesh(&sdata->vif)) { 2781 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC); 2782 sinfo->airtime_link_metric = 2783 airtime_link_metric_get(local, sta); 2784 } 2785 } 2786 2787 u32 sta_get_expected_throughput(struct sta_info *sta) 2788 { 2789 struct ieee80211_sub_if_data *sdata = sta->sdata; 2790 struct ieee80211_local *local = sdata->local; 2791 struct rate_control_ref *ref = NULL; 2792 u32 thr = 0; 2793 2794 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) 2795 ref = local->rate_ctrl; 2796 2797 /* check if the driver has a SW RC implementation */ 2798 if (ref && ref->ops->get_expected_throughput) 2799 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv); 2800 else 2801 thr = drv_get_expected_throughput(local, sta); 2802 2803 return thr; 2804 } 2805 2806 unsigned long ieee80211_sta_last_active(struct sta_info *sta) 2807 { 2808 struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta); 2809 2810 if (!sta->deflink.status_stats.last_ack || 2811 time_after(stats->last_rx, sta->deflink.status_stats.last_ack)) 2812 return stats->last_rx; 2813 return sta->deflink.status_stats.last_ack; 2814 } 2815 2816 static void sta_update_codel_params(struct sta_info *sta, u32 thr) 2817 { 2818 if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) { 2819 sta->cparams.target = MS2TIME(50); 2820 sta->cparams.interval = MS2TIME(300); 2821 sta->cparams.ecn = false; 2822 } else { 2823 sta->cparams.target = MS2TIME(20); 2824 sta->cparams.interval = MS2TIME(100); 2825 sta->cparams.ecn = true; 2826 } 2827 } 2828 2829 void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta, 2830 u32 thr) 2831 { 2832 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 2833 2834 sta_update_codel_params(sta, thr); 2835 } 2836 2837 int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id) 2838 { 2839 struct ieee80211_sub_if_data *sdata = sta->sdata; 2840 struct sta_link_alloc *alloc; 2841 int ret; 2842 2843 lockdep_assert_held(&sdata->local->sta_mtx); 2844 2845 /* must represent an MLD from the start */ 2846 if (WARN_ON(!sta->sta.valid_links)) 2847 return -EINVAL; 2848 2849 if (WARN_ON(sta->sta.valid_links & BIT(link_id) || 2850 sta->link[link_id])) 2851 return -EBUSY; 2852 2853 alloc = kzalloc(sizeof(*alloc), GFP_KERNEL); 2854 if (!alloc) 2855 return -ENOMEM; 2856 2857 ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL); 2858 if (ret) { 2859 kfree(alloc); 2860 return ret; 2861 } 2862 2863 sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta); 2864 2865 ieee80211_link_sta_debugfs_add(&alloc->info); 2866 2867 return 0; 2868 } 2869 2870 void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id) 2871 { 2872 lockdep_assert_held(&sta->sdata->local->sta_mtx); 2873 2874 sta_remove_link(sta, link_id, false); 2875 } 2876 2877 int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id) 2878 { 2879 struct ieee80211_sub_if_data *sdata = sta->sdata; 2880 struct link_sta_info *link_sta; 2881 u16 old_links = sta->sta.valid_links; 2882 u16 new_links = old_links | BIT(link_id); 2883 int ret; 2884 2885 link_sta = rcu_dereference_protected(sta->link[link_id], 2886 lockdep_is_held(&sdata->local->sta_mtx)); 2887 2888 if (WARN_ON(old_links == new_links || !link_sta)) 2889 return -EINVAL; 2890 2891 rcu_read_lock(); 2892 if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) { 2893 rcu_read_unlock(); 2894 return -EALREADY; 2895 } 2896 /* we only modify under the mutex so this is fine */ 2897 rcu_read_unlock(); 2898 2899 sta->sta.valid_links = new_links; 2900 2901 if (!test_sta_flag(sta, WLAN_STA_INSERTED)) 2902 goto hash; 2903 2904 /* Ensure the values are updated for the driver, 2905 * redone by sta_remove_link on failure. 2906 */ 2907 ieee80211_sta_recalc_aggregates(&sta->sta); 2908 2909 ret = drv_change_sta_links(sdata->local, sdata, &sta->sta, 2910 old_links, new_links); 2911 if (ret) { 2912 sta->sta.valid_links = old_links; 2913 sta_remove_link(sta, link_id, false); 2914 return ret; 2915 } 2916 2917 hash: 2918 ret = link_sta_info_hash_add(sdata->local, link_sta); 2919 WARN_ON(ret); 2920 return 0; 2921 } 2922 2923 void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id) 2924 { 2925 struct ieee80211_sub_if_data *sdata = sta->sdata; 2926 u16 old_links = sta->sta.valid_links; 2927 2928 lockdep_assert_held(&sdata->local->sta_mtx); 2929 2930 sta->sta.valid_links &= ~BIT(link_id); 2931 2932 if (test_sta_flag(sta, WLAN_STA_INSERTED)) 2933 drv_change_sta_links(sdata->local, sdata, &sta->sta, 2934 old_links, sta->sta.valid_links); 2935 2936 sta_remove_link(sta, link_id, true); 2937 } 2938 2939 void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta, 2940 const u8 *ext_capab, 2941 unsigned int ext_capab_len) 2942 { 2943 u8 val; 2944 2945 sta->sta.max_amsdu_subframes = 0; 2946 2947 if (ext_capab_len < 8) 2948 return; 2949 2950 /* The sender might not have sent the last bit, consider it to be 0 */ 2951 val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB); 2952 2953 /* we did get all the bits, take the MSB as well */ 2954 if (ext_capab_len >= 9) 2955 val |= u8_get_bits(ext_capab[8], 2956 WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1; 2957 2958 if (val) 2959 sta->sta.max_amsdu_subframes = 4 << val; 2960 } 2961 2962 #ifdef CONFIG_LOCKDEP 2963 bool lockdep_sta_mutex_held(struct ieee80211_sta *pubsta) 2964 { 2965 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 2966 2967 return lockdep_is_held(&sta->local->sta_mtx); 2968 } 2969 EXPORT_SYMBOL(lockdep_sta_mutex_held); 2970 #endif 2971