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