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