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