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