1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2002-2005, Instant802 Networks, Inc. 4 * Copyright 2005-2006, Devicescape Software, Inc. 5 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> 6 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net> 7 * Copyright 2013-2014 Intel Mobile Communications GmbH 8 * Copyright (C) 2015-2017 Intel Deutschland GmbH 9 * Copyright (C) 2018-2022 Intel Corporation 10 * 11 * utilities for mac80211 12 */ 13 14 #include <net/mac80211.h> 15 #include <linux/netdevice.h> 16 #include <linux/export.h> 17 #include <linux/types.h> 18 #include <linux/slab.h> 19 #include <linux/skbuff.h> 20 #include <linux/etherdevice.h> 21 #include <linux/if_arp.h> 22 #include <linux/bitmap.h> 23 #include <linux/crc32.h> 24 #include <net/net_namespace.h> 25 #include <net/cfg80211.h> 26 #include <net/rtnetlink.h> 27 28 #include "ieee80211_i.h" 29 #include "driver-ops.h" 30 #include "rate.h" 31 #include "mesh.h" 32 #include "wme.h" 33 #include "led.h" 34 #include "wep.h" 35 36 /* privid for wiphys to determine whether they belong to us or not */ 37 const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid; 38 39 struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy) 40 { 41 struct ieee80211_local *local; 42 43 local = wiphy_priv(wiphy); 44 return &local->hw; 45 } 46 EXPORT_SYMBOL(wiphy_to_ieee80211_hw); 47 48 u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len, 49 enum nl80211_iftype type) 50 { 51 __le16 fc = hdr->frame_control; 52 53 if (ieee80211_is_data(fc)) { 54 if (len < 24) /* drop incorrect hdr len (data) */ 55 return NULL; 56 57 if (ieee80211_has_a4(fc)) 58 return NULL; 59 if (ieee80211_has_tods(fc)) 60 return hdr->addr1; 61 if (ieee80211_has_fromds(fc)) 62 return hdr->addr2; 63 64 return hdr->addr3; 65 } 66 67 if (ieee80211_is_s1g_beacon(fc)) { 68 struct ieee80211_ext *ext = (void *) hdr; 69 70 return ext->u.s1g_beacon.sa; 71 } 72 73 if (ieee80211_is_mgmt(fc)) { 74 if (len < 24) /* drop incorrect hdr len (mgmt) */ 75 return NULL; 76 return hdr->addr3; 77 } 78 79 if (ieee80211_is_ctl(fc)) { 80 if (ieee80211_is_pspoll(fc)) 81 return hdr->addr1; 82 83 if (ieee80211_is_back_req(fc)) { 84 switch (type) { 85 case NL80211_IFTYPE_STATION: 86 return hdr->addr2; 87 case NL80211_IFTYPE_AP: 88 case NL80211_IFTYPE_AP_VLAN: 89 return hdr->addr1; 90 default: 91 break; /* fall through to the return */ 92 } 93 } 94 } 95 96 return NULL; 97 } 98 EXPORT_SYMBOL(ieee80211_get_bssid); 99 100 void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx) 101 { 102 struct sk_buff *skb; 103 struct ieee80211_hdr *hdr; 104 105 skb_queue_walk(&tx->skbs, skb) { 106 hdr = (struct ieee80211_hdr *) skb->data; 107 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED); 108 } 109 } 110 111 int ieee80211_frame_duration(enum nl80211_band band, size_t len, 112 int rate, int erp, int short_preamble, 113 int shift) 114 { 115 int dur; 116 117 /* calculate duration (in microseconds, rounded up to next higher 118 * integer if it includes a fractional microsecond) to send frame of 119 * len bytes (does not include FCS) at the given rate. Duration will 120 * also include SIFS. 121 * 122 * rate is in 100 kbps, so divident is multiplied by 10 in the 123 * DIV_ROUND_UP() operations. 124 * 125 * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and 126 * is assumed to be 0 otherwise. 127 */ 128 129 if (band == NL80211_BAND_5GHZ || erp) { 130 /* 131 * OFDM: 132 * 133 * N_DBPS = DATARATE x 4 134 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS) 135 * (16 = SIGNAL time, 6 = tail bits) 136 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext 137 * 138 * T_SYM = 4 usec 139 * 802.11a - 18.5.2: aSIFSTime = 16 usec 140 * 802.11g - 19.8.4: aSIFSTime = 10 usec + 141 * signal ext = 6 usec 142 */ 143 dur = 16; /* SIFS + signal ext */ 144 dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */ 145 dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */ 146 147 /* IEEE 802.11-2012 18.3.2.4: all values above are: 148 * * times 4 for 5 MHz 149 * * times 2 for 10 MHz 150 */ 151 dur *= 1 << shift; 152 153 /* rates should already consider the channel bandwidth, 154 * don't apply divisor again. 155 */ 156 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10, 157 4 * rate); /* T_SYM x N_SYM */ 158 } else { 159 /* 160 * 802.11b or 802.11g with 802.11b compatibility: 161 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime + 162 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0. 163 * 164 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4 165 * aSIFSTime = 10 usec 166 * aPreambleLength = 144 usec or 72 usec with short preamble 167 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble 168 */ 169 dur = 10; /* aSIFSTime = 10 usec */ 170 dur += short_preamble ? (72 + 24) : (144 + 48); 171 172 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate); 173 } 174 175 return dur; 176 } 177 178 /* Exported duration function for driver use */ 179 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw, 180 struct ieee80211_vif *vif, 181 enum nl80211_band band, 182 size_t frame_len, 183 struct ieee80211_rate *rate) 184 { 185 struct ieee80211_sub_if_data *sdata; 186 u16 dur; 187 int erp, shift = 0; 188 bool short_preamble = false; 189 190 erp = 0; 191 if (vif) { 192 sdata = vif_to_sdata(vif); 193 short_preamble = sdata->vif.bss_conf.use_short_preamble; 194 if (sdata->deflink.operating_11g_mode) 195 erp = rate->flags & IEEE80211_RATE_ERP_G; 196 shift = ieee80211_vif_get_shift(vif); 197 } 198 199 dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp, 200 short_preamble, shift); 201 202 return cpu_to_le16(dur); 203 } 204 EXPORT_SYMBOL(ieee80211_generic_frame_duration); 205 206 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw, 207 struct ieee80211_vif *vif, size_t frame_len, 208 const struct ieee80211_tx_info *frame_txctl) 209 { 210 struct ieee80211_local *local = hw_to_local(hw); 211 struct ieee80211_rate *rate; 212 struct ieee80211_sub_if_data *sdata; 213 bool short_preamble; 214 int erp, shift = 0, bitrate; 215 u16 dur; 216 struct ieee80211_supported_band *sband; 217 218 sband = local->hw.wiphy->bands[frame_txctl->band]; 219 220 short_preamble = false; 221 222 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx]; 223 224 erp = 0; 225 if (vif) { 226 sdata = vif_to_sdata(vif); 227 short_preamble = sdata->vif.bss_conf.use_short_preamble; 228 if (sdata->deflink.operating_11g_mode) 229 erp = rate->flags & IEEE80211_RATE_ERP_G; 230 shift = ieee80211_vif_get_shift(vif); 231 } 232 233 bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift); 234 235 /* CTS duration */ 236 dur = ieee80211_frame_duration(sband->band, 10, bitrate, 237 erp, short_preamble, shift); 238 /* Data frame duration */ 239 dur += ieee80211_frame_duration(sband->band, frame_len, bitrate, 240 erp, short_preamble, shift); 241 /* ACK duration */ 242 dur += ieee80211_frame_duration(sband->band, 10, bitrate, 243 erp, short_preamble, shift); 244 245 return cpu_to_le16(dur); 246 } 247 EXPORT_SYMBOL(ieee80211_rts_duration); 248 249 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw, 250 struct ieee80211_vif *vif, 251 size_t frame_len, 252 const struct ieee80211_tx_info *frame_txctl) 253 { 254 struct ieee80211_local *local = hw_to_local(hw); 255 struct ieee80211_rate *rate; 256 struct ieee80211_sub_if_data *sdata; 257 bool short_preamble; 258 int erp, shift = 0, bitrate; 259 u16 dur; 260 struct ieee80211_supported_band *sband; 261 262 sband = local->hw.wiphy->bands[frame_txctl->band]; 263 264 short_preamble = false; 265 266 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx]; 267 erp = 0; 268 if (vif) { 269 sdata = vif_to_sdata(vif); 270 short_preamble = sdata->vif.bss_conf.use_short_preamble; 271 if (sdata->deflink.operating_11g_mode) 272 erp = rate->flags & IEEE80211_RATE_ERP_G; 273 shift = ieee80211_vif_get_shift(vif); 274 } 275 276 bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift); 277 278 /* Data frame duration */ 279 dur = ieee80211_frame_duration(sband->band, frame_len, bitrate, 280 erp, short_preamble, shift); 281 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) { 282 /* ACK duration */ 283 dur += ieee80211_frame_duration(sband->band, 10, bitrate, 284 erp, short_preamble, shift); 285 } 286 287 return cpu_to_le16(dur); 288 } 289 EXPORT_SYMBOL(ieee80211_ctstoself_duration); 290 291 static void wake_tx_push_queue(struct ieee80211_local *local, 292 struct ieee80211_sub_if_data *sdata, 293 struct ieee80211_txq *queue) 294 { 295 struct ieee80211_tx_control control = { 296 .sta = queue->sta, 297 }; 298 struct sk_buff *skb; 299 300 while (1) { 301 skb = ieee80211_tx_dequeue(&local->hw, queue); 302 if (!skb) 303 break; 304 305 drv_tx(local, &control, skb); 306 } 307 } 308 309 /* wake_tx_queue handler for driver not implementing a custom one*/ 310 void ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw, 311 struct ieee80211_txq *txq) 312 { 313 struct ieee80211_local *local = hw_to_local(hw); 314 struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif); 315 struct ieee80211_txq *queue; 316 317 spin_lock(&local->handle_wake_tx_queue_lock); 318 319 /* Use ieee80211_next_txq() for airtime fairness accounting */ 320 ieee80211_txq_schedule_start(hw, txq->ac); 321 while ((queue = ieee80211_next_txq(hw, txq->ac))) { 322 wake_tx_push_queue(local, sdata, queue); 323 ieee80211_return_txq(hw, queue, false); 324 } 325 ieee80211_txq_schedule_end(hw, txq->ac); 326 spin_unlock(&local->handle_wake_tx_queue_lock); 327 } 328 EXPORT_SYMBOL(ieee80211_handle_wake_tx_queue); 329 330 static void __ieee80211_wake_txqs(struct ieee80211_sub_if_data *sdata, int ac) 331 { 332 struct ieee80211_local *local = sdata->local; 333 struct ieee80211_vif *vif = &sdata->vif; 334 struct fq *fq = &local->fq; 335 struct ps_data *ps = NULL; 336 struct txq_info *txqi; 337 struct sta_info *sta; 338 int i; 339 340 local_bh_disable(); 341 spin_lock(&fq->lock); 342 343 if (!test_bit(SDATA_STATE_RUNNING, &sdata->state)) 344 goto out; 345 346 if (sdata->vif.type == NL80211_IFTYPE_AP) 347 ps = &sdata->bss->ps; 348 349 list_for_each_entry_rcu(sta, &local->sta_list, list) { 350 if (sdata != sta->sdata) 351 continue; 352 353 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) { 354 struct ieee80211_txq *txq = sta->sta.txq[i]; 355 356 if (!txq) 357 continue; 358 359 txqi = to_txq_info(txq); 360 361 if (ac != txq->ac) 362 continue; 363 364 if (!test_and_clear_bit(IEEE80211_TXQ_DIRTY, 365 &txqi->flags)) 366 continue; 367 368 spin_unlock(&fq->lock); 369 drv_wake_tx_queue(local, txqi); 370 spin_lock(&fq->lock); 371 } 372 } 373 374 if (!vif->txq) 375 goto out; 376 377 txqi = to_txq_info(vif->txq); 378 379 if (!test_and_clear_bit(IEEE80211_TXQ_DIRTY, &txqi->flags) || 380 (ps && atomic_read(&ps->num_sta_ps)) || ac != vif->txq->ac) 381 goto out; 382 383 spin_unlock(&fq->lock); 384 385 drv_wake_tx_queue(local, txqi); 386 local_bh_enable(); 387 return; 388 out: 389 spin_unlock(&fq->lock); 390 local_bh_enable(); 391 } 392 393 static void 394 __releases(&local->queue_stop_reason_lock) 395 __acquires(&local->queue_stop_reason_lock) 396 _ieee80211_wake_txqs(struct ieee80211_local *local, unsigned long *flags) 397 { 398 struct ieee80211_sub_if_data *sdata; 399 int n_acs = IEEE80211_NUM_ACS; 400 int i; 401 402 rcu_read_lock(); 403 404 if (local->hw.queues < IEEE80211_NUM_ACS) 405 n_acs = 1; 406 407 for (i = 0; i < local->hw.queues; i++) { 408 if (local->queue_stop_reasons[i]) 409 continue; 410 411 spin_unlock_irqrestore(&local->queue_stop_reason_lock, *flags); 412 list_for_each_entry_rcu(sdata, &local->interfaces, list) { 413 int ac; 414 415 for (ac = 0; ac < n_acs; ac++) { 416 int ac_queue = sdata->vif.hw_queue[ac]; 417 418 if (ac_queue == i || 419 sdata->vif.cab_queue == i) 420 __ieee80211_wake_txqs(sdata, ac); 421 } 422 } 423 spin_lock_irqsave(&local->queue_stop_reason_lock, *flags); 424 } 425 426 rcu_read_unlock(); 427 } 428 429 void ieee80211_wake_txqs(struct tasklet_struct *t) 430 { 431 struct ieee80211_local *local = from_tasklet(local, t, 432 wake_txqs_tasklet); 433 unsigned long flags; 434 435 spin_lock_irqsave(&local->queue_stop_reason_lock, flags); 436 _ieee80211_wake_txqs(local, &flags); 437 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 438 } 439 440 static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue, 441 enum queue_stop_reason reason, 442 bool refcounted, 443 unsigned long *flags) 444 { 445 struct ieee80211_local *local = hw_to_local(hw); 446 447 trace_wake_queue(local, queue, reason); 448 449 if (WARN_ON(queue >= hw->queues)) 450 return; 451 452 if (!test_bit(reason, &local->queue_stop_reasons[queue])) 453 return; 454 455 if (!refcounted) { 456 local->q_stop_reasons[queue][reason] = 0; 457 } else { 458 local->q_stop_reasons[queue][reason]--; 459 if (WARN_ON(local->q_stop_reasons[queue][reason] < 0)) 460 local->q_stop_reasons[queue][reason] = 0; 461 } 462 463 if (local->q_stop_reasons[queue][reason] == 0) 464 __clear_bit(reason, &local->queue_stop_reasons[queue]); 465 466 if (local->queue_stop_reasons[queue] != 0) 467 /* someone still has this queue stopped */ 468 return; 469 470 if (!skb_queue_empty(&local->pending[queue])) 471 tasklet_schedule(&local->tx_pending_tasklet); 472 473 /* 474 * Calling _ieee80211_wake_txqs here can be a problem because it may 475 * release queue_stop_reason_lock which has been taken by 476 * __ieee80211_wake_queue's caller. It is certainly not very nice to 477 * release someone's lock, but it is fine because all the callers of 478 * __ieee80211_wake_queue call it right before releasing the lock. 479 */ 480 if (reason == IEEE80211_QUEUE_STOP_REASON_DRIVER) 481 tasklet_schedule(&local->wake_txqs_tasklet); 482 else 483 _ieee80211_wake_txqs(local, flags); 484 } 485 486 void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue, 487 enum queue_stop_reason reason, 488 bool refcounted) 489 { 490 struct ieee80211_local *local = hw_to_local(hw); 491 unsigned long flags; 492 493 spin_lock_irqsave(&local->queue_stop_reason_lock, flags); 494 __ieee80211_wake_queue(hw, queue, reason, refcounted, &flags); 495 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 496 } 497 498 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue) 499 { 500 ieee80211_wake_queue_by_reason(hw, queue, 501 IEEE80211_QUEUE_STOP_REASON_DRIVER, 502 false); 503 } 504 EXPORT_SYMBOL(ieee80211_wake_queue); 505 506 static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue, 507 enum queue_stop_reason reason, 508 bool refcounted) 509 { 510 struct ieee80211_local *local = hw_to_local(hw); 511 512 trace_stop_queue(local, queue, reason); 513 514 if (WARN_ON(queue >= hw->queues)) 515 return; 516 517 if (!refcounted) 518 local->q_stop_reasons[queue][reason] = 1; 519 else 520 local->q_stop_reasons[queue][reason]++; 521 522 set_bit(reason, &local->queue_stop_reasons[queue]); 523 } 524 525 void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue, 526 enum queue_stop_reason reason, 527 bool refcounted) 528 { 529 struct ieee80211_local *local = hw_to_local(hw); 530 unsigned long flags; 531 532 spin_lock_irqsave(&local->queue_stop_reason_lock, flags); 533 __ieee80211_stop_queue(hw, queue, reason, refcounted); 534 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 535 } 536 537 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue) 538 { 539 ieee80211_stop_queue_by_reason(hw, queue, 540 IEEE80211_QUEUE_STOP_REASON_DRIVER, 541 false); 542 } 543 EXPORT_SYMBOL(ieee80211_stop_queue); 544 545 void ieee80211_add_pending_skb(struct ieee80211_local *local, 546 struct sk_buff *skb) 547 { 548 struct ieee80211_hw *hw = &local->hw; 549 unsigned long flags; 550 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 551 int queue = info->hw_queue; 552 553 if (WARN_ON(!info->control.vif)) { 554 ieee80211_free_txskb(&local->hw, skb); 555 return; 556 } 557 558 spin_lock_irqsave(&local->queue_stop_reason_lock, flags); 559 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD, 560 false); 561 __skb_queue_tail(&local->pending[queue], skb); 562 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD, 563 false, &flags); 564 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 565 } 566 567 void ieee80211_add_pending_skbs(struct ieee80211_local *local, 568 struct sk_buff_head *skbs) 569 { 570 struct ieee80211_hw *hw = &local->hw; 571 struct sk_buff *skb; 572 unsigned long flags; 573 int queue, i; 574 575 spin_lock_irqsave(&local->queue_stop_reason_lock, flags); 576 while ((skb = skb_dequeue(skbs))) { 577 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 578 579 if (WARN_ON(!info->control.vif)) { 580 ieee80211_free_txskb(&local->hw, skb); 581 continue; 582 } 583 584 queue = info->hw_queue; 585 586 __ieee80211_stop_queue(hw, queue, 587 IEEE80211_QUEUE_STOP_REASON_SKB_ADD, 588 false); 589 590 __skb_queue_tail(&local->pending[queue], skb); 591 } 592 593 for (i = 0; i < hw->queues; i++) 594 __ieee80211_wake_queue(hw, i, 595 IEEE80211_QUEUE_STOP_REASON_SKB_ADD, 596 false, &flags); 597 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 598 } 599 600 void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw, 601 unsigned long queues, 602 enum queue_stop_reason reason, 603 bool refcounted) 604 { 605 struct ieee80211_local *local = hw_to_local(hw); 606 unsigned long flags; 607 int i; 608 609 spin_lock_irqsave(&local->queue_stop_reason_lock, flags); 610 611 for_each_set_bit(i, &queues, hw->queues) 612 __ieee80211_stop_queue(hw, i, reason, refcounted); 613 614 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 615 } 616 617 void ieee80211_stop_queues(struct ieee80211_hw *hw) 618 { 619 ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP, 620 IEEE80211_QUEUE_STOP_REASON_DRIVER, 621 false); 622 } 623 EXPORT_SYMBOL(ieee80211_stop_queues); 624 625 int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue) 626 { 627 struct ieee80211_local *local = hw_to_local(hw); 628 unsigned long flags; 629 int ret; 630 631 if (WARN_ON(queue >= hw->queues)) 632 return true; 633 634 spin_lock_irqsave(&local->queue_stop_reason_lock, flags); 635 ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER, 636 &local->queue_stop_reasons[queue]); 637 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 638 return ret; 639 } 640 EXPORT_SYMBOL(ieee80211_queue_stopped); 641 642 void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw, 643 unsigned long queues, 644 enum queue_stop_reason reason, 645 bool refcounted) 646 { 647 struct ieee80211_local *local = hw_to_local(hw); 648 unsigned long flags; 649 int i; 650 651 spin_lock_irqsave(&local->queue_stop_reason_lock, flags); 652 653 for_each_set_bit(i, &queues, hw->queues) 654 __ieee80211_wake_queue(hw, i, reason, refcounted, &flags); 655 656 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 657 } 658 659 void ieee80211_wake_queues(struct ieee80211_hw *hw) 660 { 661 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP, 662 IEEE80211_QUEUE_STOP_REASON_DRIVER, 663 false); 664 } 665 EXPORT_SYMBOL(ieee80211_wake_queues); 666 667 static unsigned int 668 ieee80211_get_vif_queues(struct ieee80211_local *local, 669 struct ieee80211_sub_if_data *sdata) 670 { 671 unsigned int queues; 672 673 if (sdata && ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) { 674 int ac; 675 676 queues = 0; 677 678 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 679 queues |= BIT(sdata->vif.hw_queue[ac]); 680 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE) 681 queues |= BIT(sdata->vif.cab_queue); 682 } else { 683 /* all queues */ 684 queues = BIT(local->hw.queues) - 1; 685 } 686 687 return queues; 688 } 689 690 void __ieee80211_flush_queues(struct ieee80211_local *local, 691 struct ieee80211_sub_if_data *sdata, 692 unsigned int queues, bool drop) 693 { 694 if (!local->ops->flush) 695 return; 696 697 /* 698 * If no queue was set, or if the HW doesn't support 699 * IEEE80211_HW_QUEUE_CONTROL - flush all queues 700 */ 701 if (!queues || !ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) 702 queues = ieee80211_get_vif_queues(local, sdata); 703 704 ieee80211_stop_queues_by_reason(&local->hw, queues, 705 IEEE80211_QUEUE_STOP_REASON_FLUSH, 706 false); 707 708 drv_flush(local, sdata, queues, drop); 709 710 ieee80211_wake_queues_by_reason(&local->hw, queues, 711 IEEE80211_QUEUE_STOP_REASON_FLUSH, 712 false); 713 } 714 715 void ieee80211_flush_queues(struct ieee80211_local *local, 716 struct ieee80211_sub_if_data *sdata, bool drop) 717 { 718 __ieee80211_flush_queues(local, sdata, 0, drop); 719 } 720 721 void ieee80211_stop_vif_queues(struct ieee80211_local *local, 722 struct ieee80211_sub_if_data *sdata, 723 enum queue_stop_reason reason) 724 { 725 ieee80211_stop_queues_by_reason(&local->hw, 726 ieee80211_get_vif_queues(local, sdata), 727 reason, true); 728 } 729 730 void ieee80211_wake_vif_queues(struct ieee80211_local *local, 731 struct ieee80211_sub_if_data *sdata, 732 enum queue_stop_reason reason) 733 { 734 ieee80211_wake_queues_by_reason(&local->hw, 735 ieee80211_get_vif_queues(local, sdata), 736 reason, true); 737 } 738 739 static void __iterate_interfaces(struct ieee80211_local *local, 740 u32 iter_flags, 741 void (*iterator)(void *data, u8 *mac, 742 struct ieee80211_vif *vif), 743 void *data) 744 { 745 struct ieee80211_sub_if_data *sdata; 746 bool active_only = iter_flags & IEEE80211_IFACE_ITER_ACTIVE; 747 748 list_for_each_entry_rcu(sdata, &local->interfaces, list) { 749 switch (sdata->vif.type) { 750 case NL80211_IFTYPE_MONITOR: 751 if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE)) 752 continue; 753 break; 754 case NL80211_IFTYPE_AP_VLAN: 755 continue; 756 default: 757 break; 758 } 759 if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) && 760 active_only && !(sdata->flags & IEEE80211_SDATA_IN_DRIVER)) 761 continue; 762 if ((iter_flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) && 763 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER)) 764 continue; 765 if (ieee80211_sdata_running(sdata) || !active_only) 766 iterator(data, sdata->vif.addr, 767 &sdata->vif); 768 } 769 770 sdata = rcu_dereference_check(local->monitor_sdata, 771 lockdep_is_held(&local->iflist_mtx) || 772 lockdep_is_held(&local->hw.wiphy->mtx)); 773 if (sdata && 774 (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || !active_only || 775 sdata->flags & IEEE80211_SDATA_IN_DRIVER)) 776 iterator(data, sdata->vif.addr, &sdata->vif); 777 } 778 779 void ieee80211_iterate_interfaces( 780 struct ieee80211_hw *hw, u32 iter_flags, 781 void (*iterator)(void *data, u8 *mac, 782 struct ieee80211_vif *vif), 783 void *data) 784 { 785 struct ieee80211_local *local = hw_to_local(hw); 786 787 mutex_lock(&local->iflist_mtx); 788 __iterate_interfaces(local, iter_flags, iterator, data); 789 mutex_unlock(&local->iflist_mtx); 790 } 791 EXPORT_SYMBOL_GPL(ieee80211_iterate_interfaces); 792 793 void ieee80211_iterate_active_interfaces_atomic( 794 struct ieee80211_hw *hw, u32 iter_flags, 795 void (*iterator)(void *data, u8 *mac, 796 struct ieee80211_vif *vif), 797 void *data) 798 { 799 struct ieee80211_local *local = hw_to_local(hw); 800 801 rcu_read_lock(); 802 __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE, 803 iterator, data); 804 rcu_read_unlock(); 805 } 806 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic); 807 808 void ieee80211_iterate_active_interfaces_mtx( 809 struct ieee80211_hw *hw, u32 iter_flags, 810 void (*iterator)(void *data, u8 *mac, 811 struct ieee80211_vif *vif), 812 void *data) 813 { 814 struct ieee80211_local *local = hw_to_local(hw); 815 816 lockdep_assert_wiphy(hw->wiphy); 817 818 __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE, 819 iterator, data); 820 } 821 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_mtx); 822 823 static void __iterate_stations(struct ieee80211_local *local, 824 void (*iterator)(void *data, 825 struct ieee80211_sta *sta), 826 void *data) 827 { 828 struct sta_info *sta; 829 830 list_for_each_entry_rcu(sta, &local->sta_list, list) { 831 if (!sta->uploaded) 832 continue; 833 834 iterator(data, &sta->sta); 835 } 836 } 837 838 void ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw, 839 void (*iterator)(void *data, 840 struct ieee80211_sta *sta), 841 void *data) 842 { 843 struct ieee80211_local *local = hw_to_local(hw); 844 845 rcu_read_lock(); 846 __iterate_stations(local, iterator, data); 847 rcu_read_unlock(); 848 } 849 EXPORT_SYMBOL_GPL(ieee80211_iterate_stations_atomic); 850 851 struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev) 852 { 853 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev); 854 855 if (!ieee80211_sdata_running(sdata) || 856 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER)) 857 return NULL; 858 return &sdata->vif; 859 } 860 EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif); 861 862 struct wireless_dev *ieee80211_vif_to_wdev(struct ieee80211_vif *vif) 863 { 864 if (!vif) 865 return NULL; 866 867 return &vif_to_sdata(vif)->wdev; 868 } 869 EXPORT_SYMBOL_GPL(ieee80211_vif_to_wdev); 870 871 /* 872 * Nothing should have been stuffed into the workqueue during 873 * the suspend->resume cycle. Since we can't check each caller 874 * of this function if we are already quiescing / suspended, 875 * check here and don't WARN since this can actually happen when 876 * the rx path (for example) is racing against __ieee80211_suspend 877 * and suspending / quiescing was set after the rx path checked 878 * them. 879 */ 880 static bool ieee80211_can_queue_work(struct ieee80211_local *local) 881 { 882 if (local->quiescing || (local->suspended && !local->resuming)) { 883 pr_warn("queueing ieee80211 work while going to suspend\n"); 884 return false; 885 } 886 887 return true; 888 } 889 890 void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work) 891 { 892 struct ieee80211_local *local = hw_to_local(hw); 893 894 if (!ieee80211_can_queue_work(local)) 895 return; 896 897 queue_work(local->workqueue, work); 898 } 899 EXPORT_SYMBOL(ieee80211_queue_work); 900 901 void ieee80211_queue_delayed_work(struct ieee80211_hw *hw, 902 struct delayed_work *dwork, 903 unsigned long delay) 904 { 905 struct ieee80211_local *local = hw_to_local(hw); 906 907 if (!ieee80211_can_queue_work(local)) 908 return; 909 910 queue_delayed_work(local->workqueue, dwork, delay); 911 } 912 EXPORT_SYMBOL(ieee80211_queue_delayed_work); 913 914 static void 915 ieee80211_parse_extension_element(u32 *crc, 916 const struct element *elem, 917 struct ieee802_11_elems *elems, 918 struct ieee80211_elems_parse_params *params) 919 { 920 const void *data = elem->data + 1; 921 u8 len; 922 923 if (!elem->datalen) 924 return; 925 926 len = elem->datalen - 1; 927 928 switch (elem->data[0]) { 929 case WLAN_EID_EXT_HE_MU_EDCA: 930 if (len >= sizeof(*elems->mu_edca_param_set)) { 931 elems->mu_edca_param_set = data; 932 if (crc) 933 *crc = crc32_be(*crc, (void *)elem, 934 elem->datalen + 2); 935 } 936 break; 937 case WLAN_EID_EXT_HE_CAPABILITY: 938 if (ieee80211_he_capa_size_ok(data, len)) { 939 elems->he_cap = data; 940 elems->he_cap_len = len; 941 } 942 break; 943 case WLAN_EID_EXT_HE_OPERATION: 944 if (len >= sizeof(*elems->he_operation) && 945 len >= ieee80211_he_oper_size(data) - 1) { 946 if (crc) 947 *crc = crc32_be(*crc, (void *)elem, 948 elem->datalen + 2); 949 elems->he_operation = data; 950 } 951 break; 952 case WLAN_EID_EXT_UORA: 953 if (len >= 1) 954 elems->uora_element = data; 955 break; 956 case WLAN_EID_EXT_MAX_CHANNEL_SWITCH_TIME: 957 if (len == 3) 958 elems->max_channel_switch_time = data; 959 break; 960 case WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION: 961 if (len >= sizeof(*elems->mbssid_config_ie)) 962 elems->mbssid_config_ie = data; 963 break; 964 case WLAN_EID_EXT_HE_SPR: 965 if (len >= sizeof(*elems->he_spr) && 966 len >= ieee80211_he_spr_size(data)) 967 elems->he_spr = data; 968 break; 969 case WLAN_EID_EXT_HE_6GHZ_CAPA: 970 if (len >= sizeof(*elems->he_6ghz_capa)) 971 elems->he_6ghz_capa = data; 972 break; 973 case WLAN_EID_EXT_EHT_CAPABILITY: 974 if (ieee80211_eht_capa_size_ok(elems->he_cap, 975 data, len, 976 params->from_ap)) { 977 elems->eht_cap = data; 978 elems->eht_cap_len = len; 979 } 980 break; 981 case WLAN_EID_EXT_EHT_OPERATION: 982 if (ieee80211_eht_oper_size_ok(data, len)) 983 elems->eht_operation = data; 984 break; 985 case WLAN_EID_EXT_EHT_MULTI_LINK: 986 if (ieee80211_mle_size_ok(data, len)) { 987 elems->multi_link = (void *)data; 988 elems->multi_link_len = len; 989 } 990 break; 991 } 992 } 993 994 static u32 995 _ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params, 996 struct ieee802_11_elems *elems, 997 const struct element *check_inherit) 998 { 999 const struct element *elem; 1000 bool calc_crc = params->filter != 0; 1001 DECLARE_BITMAP(seen_elems, 256); 1002 u32 crc = params->crc; 1003 const u8 *ie; 1004 1005 bitmap_zero(seen_elems, 256); 1006 1007 for_each_element(elem, params->start, params->len) { 1008 bool elem_parse_failed; 1009 u8 id = elem->id; 1010 u8 elen = elem->datalen; 1011 const u8 *pos = elem->data; 1012 1013 if (check_inherit && 1014 !cfg80211_is_element_inherited(elem, 1015 check_inherit)) 1016 continue; 1017 1018 switch (id) { 1019 case WLAN_EID_SSID: 1020 case WLAN_EID_SUPP_RATES: 1021 case WLAN_EID_FH_PARAMS: 1022 case WLAN_EID_DS_PARAMS: 1023 case WLAN_EID_CF_PARAMS: 1024 case WLAN_EID_TIM: 1025 case WLAN_EID_IBSS_PARAMS: 1026 case WLAN_EID_CHALLENGE: 1027 case WLAN_EID_RSN: 1028 case WLAN_EID_ERP_INFO: 1029 case WLAN_EID_EXT_SUPP_RATES: 1030 case WLAN_EID_HT_CAPABILITY: 1031 case WLAN_EID_HT_OPERATION: 1032 case WLAN_EID_VHT_CAPABILITY: 1033 case WLAN_EID_VHT_OPERATION: 1034 case WLAN_EID_MESH_ID: 1035 case WLAN_EID_MESH_CONFIG: 1036 case WLAN_EID_PEER_MGMT: 1037 case WLAN_EID_PREQ: 1038 case WLAN_EID_PREP: 1039 case WLAN_EID_PERR: 1040 case WLAN_EID_RANN: 1041 case WLAN_EID_CHANNEL_SWITCH: 1042 case WLAN_EID_EXT_CHANSWITCH_ANN: 1043 case WLAN_EID_COUNTRY: 1044 case WLAN_EID_PWR_CONSTRAINT: 1045 case WLAN_EID_TIMEOUT_INTERVAL: 1046 case WLAN_EID_SECONDARY_CHANNEL_OFFSET: 1047 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH: 1048 case WLAN_EID_CHAN_SWITCH_PARAM: 1049 case WLAN_EID_EXT_CAPABILITY: 1050 case WLAN_EID_CHAN_SWITCH_TIMING: 1051 case WLAN_EID_LINK_ID: 1052 case WLAN_EID_BSS_MAX_IDLE_PERIOD: 1053 case WLAN_EID_RSNX: 1054 case WLAN_EID_S1G_BCN_COMPAT: 1055 case WLAN_EID_S1G_CAPABILITIES: 1056 case WLAN_EID_S1G_OPERATION: 1057 case WLAN_EID_AID_RESPONSE: 1058 case WLAN_EID_S1G_SHORT_BCN_INTERVAL: 1059 /* 1060 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible 1061 * that if the content gets bigger it might be needed more than once 1062 */ 1063 if (test_bit(id, seen_elems)) { 1064 elems->parse_error = true; 1065 continue; 1066 } 1067 break; 1068 } 1069 1070 if (calc_crc && id < 64 && (params->filter & (1ULL << id))) 1071 crc = crc32_be(crc, pos - 2, elen + 2); 1072 1073 elem_parse_failed = false; 1074 1075 switch (id) { 1076 case WLAN_EID_LINK_ID: 1077 if (elen + 2 < sizeof(struct ieee80211_tdls_lnkie)) { 1078 elem_parse_failed = true; 1079 break; 1080 } 1081 elems->lnk_id = (void *)(pos - 2); 1082 break; 1083 case WLAN_EID_CHAN_SWITCH_TIMING: 1084 if (elen < sizeof(struct ieee80211_ch_switch_timing)) { 1085 elem_parse_failed = true; 1086 break; 1087 } 1088 elems->ch_sw_timing = (void *)pos; 1089 break; 1090 case WLAN_EID_EXT_CAPABILITY: 1091 elems->ext_capab = pos; 1092 elems->ext_capab_len = elen; 1093 break; 1094 case WLAN_EID_SSID: 1095 elems->ssid = pos; 1096 elems->ssid_len = elen; 1097 break; 1098 case WLAN_EID_SUPP_RATES: 1099 elems->supp_rates = pos; 1100 elems->supp_rates_len = elen; 1101 break; 1102 case WLAN_EID_DS_PARAMS: 1103 if (elen >= 1) 1104 elems->ds_params = pos; 1105 else 1106 elem_parse_failed = true; 1107 break; 1108 case WLAN_EID_TIM: 1109 if (elen >= sizeof(struct ieee80211_tim_ie)) { 1110 elems->tim = (void *)pos; 1111 elems->tim_len = elen; 1112 } else 1113 elem_parse_failed = true; 1114 break; 1115 case WLAN_EID_VENDOR_SPECIFIC: 1116 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 && 1117 pos[2] == 0xf2) { 1118 /* Microsoft OUI (00:50:F2) */ 1119 1120 if (calc_crc) 1121 crc = crc32_be(crc, pos - 2, elen + 2); 1122 1123 if (elen >= 5 && pos[3] == 2) { 1124 /* OUI Type 2 - WMM IE */ 1125 if (pos[4] == 0) { 1126 elems->wmm_info = pos; 1127 elems->wmm_info_len = elen; 1128 } else if (pos[4] == 1) { 1129 elems->wmm_param = pos; 1130 elems->wmm_param_len = elen; 1131 } 1132 } 1133 } 1134 break; 1135 case WLAN_EID_RSN: 1136 elems->rsn = pos; 1137 elems->rsn_len = elen; 1138 break; 1139 case WLAN_EID_ERP_INFO: 1140 if (elen >= 1) 1141 elems->erp_info = pos; 1142 else 1143 elem_parse_failed = true; 1144 break; 1145 case WLAN_EID_EXT_SUPP_RATES: 1146 elems->ext_supp_rates = pos; 1147 elems->ext_supp_rates_len = elen; 1148 break; 1149 case WLAN_EID_HT_CAPABILITY: 1150 if (elen >= sizeof(struct ieee80211_ht_cap)) 1151 elems->ht_cap_elem = (void *)pos; 1152 else 1153 elem_parse_failed = true; 1154 break; 1155 case WLAN_EID_HT_OPERATION: 1156 if (elen >= sizeof(struct ieee80211_ht_operation)) 1157 elems->ht_operation = (void *)pos; 1158 else 1159 elem_parse_failed = true; 1160 break; 1161 case WLAN_EID_VHT_CAPABILITY: 1162 if (elen >= sizeof(struct ieee80211_vht_cap)) 1163 elems->vht_cap_elem = (void *)pos; 1164 else 1165 elem_parse_failed = true; 1166 break; 1167 case WLAN_EID_VHT_OPERATION: 1168 if (elen >= sizeof(struct ieee80211_vht_operation)) { 1169 elems->vht_operation = (void *)pos; 1170 if (calc_crc) 1171 crc = crc32_be(crc, pos - 2, elen + 2); 1172 break; 1173 } 1174 elem_parse_failed = true; 1175 break; 1176 case WLAN_EID_OPMODE_NOTIF: 1177 if (elen > 0) { 1178 elems->opmode_notif = pos; 1179 if (calc_crc) 1180 crc = crc32_be(crc, pos - 2, elen + 2); 1181 break; 1182 } 1183 elem_parse_failed = true; 1184 break; 1185 case WLAN_EID_MESH_ID: 1186 elems->mesh_id = pos; 1187 elems->mesh_id_len = elen; 1188 break; 1189 case WLAN_EID_MESH_CONFIG: 1190 if (elen >= sizeof(struct ieee80211_meshconf_ie)) 1191 elems->mesh_config = (void *)pos; 1192 else 1193 elem_parse_failed = true; 1194 break; 1195 case WLAN_EID_PEER_MGMT: 1196 elems->peering = pos; 1197 elems->peering_len = elen; 1198 break; 1199 case WLAN_EID_MESH_AWAKE_WINDOW: 1200 if (elen >= 2) 1201 elems->awake_window = (void *)pos; 1202 break; 1203 case WLAN_EID_PREQ: 1204 elems->preq = pos; 1205 elems->preq_len = elen; 1206 break; 1207 case WLAN_EID_PREP: 1208 elems->prep = pos; 1209 elems->prep_len = elen; 1210 break; 1211 case WLAN_EID_PERR: 1212 elems->perr = pos; 1213 elems->perr_len = elen; 1214 break; 1215 case WLAN_EID_RANN: 1216 if (elen >= sizeof(struct ieee80211_rann_ie)) 1217 elems->rann = (void *)pos; 1218 else 1219 elem_parse_failed = true; 1220 break; 1221 case WLAN_EID_CHANNEL_SWITCH: 1222 if (elen != sizeof(struct ieee80211_channel_sw_ie)) { 1223 elem_parse_failed = true; 1224 break; 1225 } 1226 elems->ch_switch_ie = (void *)pos; 1227 break; 1228 case WLAN_EID_EXT_CHANSWITCH_ANN: 1229 if (elen != sizeof(struct ieee80211_ext_chansw_ie)) { 1230 elem_parse_failed = true; 1231 break; 1232 } 1233 elems->ext_chansw_ie = (void *)pos; 1234 break; 1235 case WLAN_EID_SECONDARY_CHANNEL_OFFSET: 1236 if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) { 1237 elem_parse_failed = true; 1238 break; 1239 } 1240 elems->sec_chan_offs = (void *)pos; 1241 break; 1242 case WLAN_EID_CHAN_SWITCH_PARAM: 1243 if (elen < 1244 sizeof(*elems->mesh_chansw_params_ie)) { 1245 elem_parse_failed = true; 1246 break; 1247 } 1248 elems->mesh_chansw_params_ie = (void *)pos; 1249 break; 1250 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH: 1251 if (!params->action || 1252 elen < sizeof(*elems->wide_bw_chansw_ie)) { 1253 elem_parse_failed = true; 1254 break; 1255 } 1256 elems->wide_bw_chansw_ie = (void *)pos; 1257 break; 1258 case WLAN_EID_CHANNEL_SWITCH_WRAPPER: 1259 if (params->action) { 1260 elem_parse_failed = true; 1261 break; 1262 } 1263 /* 1264 * This is a bit tricky, but as we only care about 1265 * the wide bandwidth channel switch element, so 1266 * just parse it out manually. 1267 */ 1268 ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH, 1269 pos, elen); 1270 if (ie) { 1271 if (ie[1] >= sizeof(*elems->wide_bw_chansw_ie)) 1272 elems->wide_bw_chansw_ie = 1273 (void *)(ie + 2); 1274 else 1275 elem_parse_failed = true; 1276 } 1277 break; 1278 case WLAN_EID_COUNTRY: 1279 elems->country_elem = pos; 1280 elems->country_elem_len = elen; 1281 break; 1282 case WLAN_EID_PWR_CONSTRAINT: 1283 if (elen != 1) { 1284 elem_parse_failed = true; 1285 break; 1286 } 1287 elems->pwr_constr_elem = pos; 1288 break; 1289 case WLAN_EID_CISCO_VENDOR_SPECIFIC: 1290 /* Lots of different options exist, but we only care 1291 * about the Dynamic Transmit Power Control element. 1292 * First check for the Cisco OUI, then for the DTPC 1293 * tag (0x00). 1294 */ 1295 if (elen < 4) { 1296 elem_parse_failed = true; 1297 break; 1298 } 1299 1300 if (pos[0] != 0x00 || pos[1] != 0x40 || 1301 pos[2] != 0x96 || pos[3] != 0x00) 1302 break; 1303 1304 if (elen != 6) { 1305 elem_parse_failed = true; 1306 break; 1307 } 1308 1309 if (calc_crc) 1310 crc = crc32_be(crc, pos - 2, elen + 2); 1311 1312 elems->cisco_dtpc_elem = pos; 1313 break; 1314 case WLAN_EID_ADDBA_EXT: 1315 if (elen < sizeof(struct ieee80211_addba_ext_ie)) { 1316 elem_parse_failed = true; 1317 break; 1318 } 1319 elems->addba_ext_ie = (void *)pos; 1320 break; 1321 case WLAN_EID_TIMEOUT_INTERVAL: 1322 if (elen >= sizeof(struct ieee80211_timeout_interval_ie)) 1323 elems->timeout_int = (void *)pos; 1324 else 1325 elem_parse_failed = true; 1326 break; 1327 case WLAN_EID_BSS_MAX_IDLE_PERIOD: 1328 if (elen >= sizeof(*elems->max_idle_period_ie)) 1329 elems->max_idle_period_ie = (void *)pos; 1330 break; 1331 case WLAN_EID_RSNX: 1332 elems->rsnx = pos; 1333 elems->rsnx_len = elen; 1334 break; 1335 case WLAN_EID_TX_POWER_ENVELOPE: 1336 if (elen < 1 || 1337 elen > sizeof(struct ieee80211_tx_pwr_env)) 1338 break; 1339 1340 if (elems->tx_pwr_env_num >= ARRAY_SIZE(elems->tx_pwr_env)) 1341 break; 1342 1343 elems->tx_pwr_env[elems->tx_pwr_env_num] = (void *)pos; 1344 elems->tx_pwr_env_len[elems->tx_pwr_env_num] = elen; 1345 elems->tx_pwr_env_num++; 1346 break; 1347 case WLAN_EID_EXTENSION: 1348 ieee80211_parse_extension_element(calc_crc ? 1349 &crc : NULL, 1350 elem, elems, params); 1351 break; 1352 case WLAN_EID_S1G_CAPABILITIES: 1353 if (elen >= sizeof(*elems->s1g_capab)) 1354 elems->s1g_capab = (void *)pos; 1355 else 1356 elem_parse_failed = true; 1357 break; 1358 case WLAN_EID_S1G_OPERATION: 1359 if (elen == sizeof(*elems->s1g_oper)) 1360 elems->s1g_oper = (void *)pos; 1361 else 1362 elem_parse_failed = true; 1363 break; 1364 case WLAN_EID_S1G_BCN_COMPAT: 1365 if (elen == sizeof(*elems->s1g_bcn_compat)) 1366 elems->s1g_bcn_compat = (void *)pos; 1367 else 1368 elem_parse_failed = true; 1369 break; 1370 case WLAN_EID_AID_RESPONSE: 1371 if (elen == sizeof(struct ieee80211_aid_response_ie)) 1372 elems->aid_resp = (void *)pos; 1373 else 1374 elem_parse_failed = true; 1375 break; 1376 default: 1377 break; 1378 } 1379 1380 if (elem_parse_failed) 1381 elems->parse_error = true; 1382 else 1383 __set_bit(id, seen_elems); 1384 } 1385 1386 if (!for_each_element_completed(elem, params->start, params->len)) 1387 elems->parse_error = true; 1388 1389 return crc; 1390 } 1391 1392 static size_t ieee802_11_find_bssid_profile(const u8 *start, size_t len, 1393 struct ieee802_11_elems *elems, 1394 struct cfg80211_bss *bss, 1395 u8 *nontransmitted_profile) 1396 { 1397 const struct element *elem, *sub; 1398 size_t profile_len = 0; 1399 bool found = false; 1400 1401 if (!bss || !bss->transmitted_bss) 1402 return profile_len; 1403 1404 for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, len) { 1405 if (elem->datalen < 2) 1406 continue; 1407 if (elem->data[0] < 1 || elem->data[0] > 8) 1408 continue; 1409 1410 for_each_element(sub, elem->data + 1, elem->datalen - 1) { 1411 u8 new_bssid[ETH_ALEN]; 1412 const u8 *index; 1413 1414 if (sub->id != 0 || sub->datalen < 4) { 1415 /* not a valid BSS profile */ 1416 continue; 1417 } 1418 1419 if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP || 1420 sub->data[1] != 2) { 1421 /* The first element of the 1422 * Nontransmitted BSSID Profile is not 1423 * the Nontransmitted BSSID Capability 1424 * element. 1425 */ 1426 continue; 1427 } 1428 1429 memset(nontransmitted_profile, 0, len); 1430 profile_len = cfg80211_merge_profile(start, len, 1431 elem, 1432 sub, 1433 nontransmitted_profile, 1434 len); 1435 1436 /* found a Nontransmitted BSSID Profile */ 1437 index = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, 1438 nontransmitted_profile, 1439 profile_len); 1440 if (!index || index[1] < 1 || index[2] == 0) { 1441 /* Invalid MBSSID Index element */ 1442 continue; 1443 } 1444 1445 cfg80211_gen_new_bssid(bss->transmitted_bss->bssid, 1446 elem->data[0], 1447 index[2], 1448 new_bssid); 1449 if (ether_addr_equal(new_bssid, bss->bssid)) { 1450 found = true; 1451 elems->bssid_index_len = index[1]; 1452 elems->bssid_index = (void *)&index[2]; 1453 break; 1454 } 1455 } 1456 } 1457 1458 return found ? profile_len : 0; 1459 } 1460 1461 static void ieee80211_defragment_element(struct ieee802_11_elems *elems, 1462 void **elem_ptr, size_t *len, 1463 size_t total_len, u8 frag_id) 1464 { 1465 u8 *data = *elem_ptr, *pos, *start; 1466 const struct element *elem; 1467 1468 /* 1469 * Since 'data' points to the data of the element, not the element 1470 * itself, allow 254 in case it was an extended element where the 1471 * extended ID isn't part of the data we see here and thus not part of 1472 * 'len' either. 1473 */ 1474 if (!data || (*len != 254 && *len != 255)) 1475 return; 1476 1477 start = elems->scratch_pos; 1478 1479 if (WARN_ON(*len > (elems->scratch + elems->scratch_len - 1480 elems->scratch_pos))) 1481 return; 1482 1483 memcpy(elems->scratch_pos, data, *len); 1484 elems->scratch_pos += *len; 1485 1486 pos = data + *len; 1487 total_len -= *len; 1488 for_each_element(elem, pos, total_len) { 1489 if (elem->id != frag_id) 1490 break; 1491 1492 if (WARN_ON(elem->datalen > 1493 (elems->scratch + elems->scratch_len - 1494 elems->scratch_pos))) 1495 return; 1496 1497 memcpy(elems->scratch_pos, elem->data, elem->datalen); 1498 elems->scratch_pos += elem->datalen; 1499 1500 *len += elem->datalen; 1501 } 1502 1503 *elem_ptr = start; 1504 } 1505 1506 static void ieee80211_mle_get_sta_prof(struct ieee802_11_elems *elems, 1507 u8 link_id) 1508 { 1509 const struct ieee80211_multi_link_elem *ml = elems->multi_link; 1510 size_t ml_len = elems->multi_link_len; 1511 const struct element *sub; 1512 1513 if (!ml || !ml_len) 1514 return; 1515 1516 if (le16_get_bits(ml->control, IEEE80211_ML_CONTROL_TYPE) != 1517 IEEE80211_ML_CONTROL_TYPE_BASIC) 1518 return; 1519 1520 for_each_mle_subelement(sub, (u8 *)ml, ml_len) { 1521 struct ieee80211_mle_per_sta_profile *prof = (void *)sub->data; 1522 u16 control; 1523 1524 if (sub->id != IEEE80211_MLE_SUBELEM_PER_STA_PROFILE) 1525 continue; 1526 1527 if (!ieee80211_mle_sta_prof_size_ok(sub->data, sub->datalen)) 1528 return; 1529 1530 control = le16_to_cpu(prof->control); 1531 1532 if (link_id != u16_get_bits(control, 1533 IEEE80211_MLE_STA_CONTROL_LINK_ID)) 1534 continue; 1535 1536 if (!(control & IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE)) 1537 return; 1538 1539 elems->prof = prof; 1540 elems->sta_prof_len = sub->datalen; 1541 1542 /* the sub element can be fragmented */ 1543 ieee80211_defragment_element(elems, (void **)&elems->prof, 1544 &elems->sta_prof_len, 1545 ml_len - (sub->data - (u8 *)ml), 1546 IEEE80211_MLE_SUBELEM_FRAGMENT); 1547 return; 1548 } 1549 } 1550 1551 static void ieee80211_mle_parse_link(struct ieee802_11_elems *elems, 1552 struct ieee80211_elems_parse_params *params) 1553 { 1554 struct ieee80211_mle_per_sta_profile *prof; 1555 struct ieee80211_elems_parse_params sub = { 1556 .action = params->action, 1557 .from_ap = params->from_ap, 1558 .link_id = -1, 1559 }; 1560 const struct element *non_inherit = NULL; 1561 const u8 *end; 1562 1563 if (params->link_id == -1) 1564 return; 1565 1566 ieee80211_defragment_element(elems, (void **)&elems->multi_link, 1567 &elems->multi_link_len, 1568 elems->total_len - ((u8 *)elems->multi_link - 1569 elems->ie_start), 1570 WLAN_EID_FRAGMENT); 1571 1572 ieee80211_mle_get_sta_prof(elems, params->link_id); 1573 prof = elems->prof; 1574 1575 if (!prof) 1576 return; 1577 1578 /* check if we have the 4 bytes for the fixed part in assoc response */ 1579 if (elems->sta_prof_len < sizeof(*prof) + prof->sta_info_len - 1 + 4) { 1580 elems->prof = NULL; 1581 elems->sta_prof_len = 0; 1582 return; 1583 } 1584 1585 /* 1586 * Skip the capability information and the status code that are expected 1587 * as part of the station profile in association response frames. Note 1588 * the -1 is because the 'sta_info_len' is accounted to as part of the 1589 * per-STA profile, but not part of the 'u8 variable[]' portion. 1590 */ 1591 sub.start = prof->variable + prof->sta_info_len - 1 + 4; 1592 end = (const u8 *)prof + elems->sta_prof_len; 1593 sub.len = end - sub.start; 1594 1595 non_inherit = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE, 1596 sub.start, sub.len); 1597 _ieee802_11_parse_elems_full(&sub, elems, non_inherit); 1598 } 1599 1600 struct ieee802_11_elems * 1601 ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params) 1602 { 1603 struct ieee802_11_elems *elems; 1604 const struct element *non_inherit = NULL; 1605 u8 *nontransmitted_profile; 1606 int nontransmitted_profile_len = 0; 1607 size_t scratch_len = 3 * params->len; 1608 1609 elems = kzalloc(sizeof(*elems) + scratch_len, GFP_ATOMIC); 1610 if (!elems) 1611 return NULL; 1612 elems->ie_start = params->start; 1613 elems->total_len = params->len; 1614 elems->scratch_len = scratch_len; 1615 elems->scratch_pos = elems->scratch; 1616 1617 nontransmitted_profile = elems->scratch_pos; 1618 nontransmitted_profile_len = 1619 ieee802_11_find_bssid_profile(params->start, params->len, 1620 elems, params->bss, 1621 nontransmitted_profile); 1622 elems->scratch_pos += nontransmitted_profile_len; 1623 elems->scratch_len -= nontransmitted_profile_len; 1624 non_inherit = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE, 1625 nontransmitted_profile, 1626 nontransmitted_profile_len); 1627 1628 elems->crc = _ieee802_11_parse_elems_full(params, elems, non_inherit); 1629 1630 /* Override with nontransmitted profile, if found */ 1631 if (nontransmitted_profile_len) { 1632 struct ieee80211_elems_parse_params sub = { 1633 .start = nontransmitted_profile, 1634 .len = nontransmitted_profile_len, 1635 .action = params->action, 1636 .link_id = params->link_id, 1637 }; 1638 1639 _ieee802_11_parse_elems_full(&sub, elems, NULL); 1640 } 1641 1642 ieee80211_mle_parse_link(elems, params); 1643 1644 if (elems->tim && !elems->parse_error) { 1645 const struct ieee80211_tim_ie *tim_ie = elems->tim; 1646 1647 elems->dtim_period = tim_ie->dtim_period; 1648 elems->dtim_count = tim_ie->dtim_count; 1649 } 1650 1651 /* Override DTIM period and count if needed */ 1652 if (elems->bssid_index && 1653 elems->bssid_index_len >= 1654 offsetofend(struct ieee80211_bssid_index, dtim_period)) 1655 elems->dtim_period = elems->bssid_index->dtim_period; 1656 1657 if (elems->bssid_index && 1658 elems->bssid_index_len >= 1659 offsetofend(struct ieee80211_bssid_index, dtim_count)) 1660 elems->dtim_count = elems->bssid_index->dtim_count; 1661 1662 return elems; 1663 } 1664 1665 void ieee80211_regulatory_limit_wmm_params(struct ieee80211_sub_if_data *sdata, 1666 struct ieee80211_tx_queue_params 1667 *qparam, int ac) 1668 { 1669 struct ieee80211_chanctx_conf *chanctx_conf; 1670 const struct ieee80211_reg_rule *rrule; 1671 const struct ieee80211_wmm_ac *wmm_ac; 1672 u16 center_freq = 0; 1673 1674 if (sdata->vif.type != NL80211_IFTYPE_AP && 1675 sdata->vif.type != NL80211_IFTYPE_STATION) 1676 return; 1677 1678 rcu_read_lock(); 1679 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf); 1680 if (chanctx_conf) 1681 center_freq = chanctx_conf->def.chan->center_freq; 1682 1683 if (!center_freq) { 1684 rcu_read_unlock(); 1685 return; 1686 } 1687 1688 rrule = freq_reg_info(sdata->wdev.wiphy, MHZ_TO_KHZ(center_freq)); 1689 1690 if (IS_ERR_OR_NULL(rrule) || !rrule->has_wmm) { 1691 rcu_read_unlock(); 1692 return; 1693 } 1694 1695 if (sdata->vif.type == NL80211_IFTYPE_AP) 1696 wmm_ac = &rrule->wmm_rule.ap[ac]; 1697 else 1698 wmm_ac = &rrule->wmm_rule.client[ac]; 1699 qparam->cw_min = max_t(u16, qparam->cw_min, wmm_ac->cw_min); 1700 qparam->cw_max = max_t(u16, qparam->cw_max, wmm_ac->cw_max); 1701 qparam->aifs = max_t(u8, qparam->aifs, wmm_ac->aifsn); 1702 qparam->txop = min_t(u16, qparam->txop, wmm_ac->cot / 32); 1703 rcu_read_unlock(); 1704 } 1705 1706 void ieee80211_set_wmm_default(struct ieee80211_link_data *link, 1707 bool bss_notify, bool enable_qos) 1708 { 1709 struct ieee80211_sub_if_data *sdata = link->sdata; 1710 struct ieee80211_local *local = sdata->local; 1711 struct ieee80211_tx_queue_params qparam; 1712 struct ieee80211_chanctx_conf *chanctx_conf; 1713 int ac; 1714 bool use_11b; 1715 bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */ 1716 int aCWmin, aCWmax; 1717 1718 if (!local->ops->conf_tx) 1719 return; 1720 1721 if (local->hw.queues < IEEE80211_NUM_ACS) 1722 return; 1723 1724 memset(&qparam, 0, sizeof(qparam)); 1725 1726 rcu_read_lock(); 1727 chanctx_conf = rcu_dereference(link->conf->chanctx_conf); 1728 use_11b = (chanctx_conf && 1729 chanctx_conf->def.chan->band == NL80211_BAND_2GHZ) && 1730 !link->operating_11g_mode; 1731 rcu_read_unlock(); 1732 1733 is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB); 1734 1735 /* Set defaults according to 802.11-2007 Table 7-37 */ 1736 aCWmax = 1023; 1737 if (use_11b) 1738 aCWmin = 31; 1739 else 1740 aCWmin = 15; 1741 1742 /* Confiure old 802.11b/g medium access rules. */ 1743 qparam.cw_max = aCWmax; 1744 qparam.cw_min = aCWmin; 1745 qparam.txop = 0; 1746 qparam.aifs = 2; 1747 1748 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1749 /* Update if QoS is enabled. */ 1750 if (enable_qos) { 1751 switch (ac) { 1752 case IEEE80211_AC_BK: 1753 qparam.cw_max = aCWmax; 1754 qparam.cw_min = aCWmin; 1755 qparam.txop = 0; 1756 if (is_ocb) 1757 qparam.aifs = 9; 1758 else 1759 qparam.aifs = 7; 1760 break; 1761 /* never happens but let's not leave undefined */ 1762 default: 1763 case IEEE80211_AC_BE: 1764 qparam.cw_max = aCWmax; 1765 qparam.cw_min = aCWmin; 1766 qparam.txop = 0; 1767 if (is_ocb) 1768 qparam.aifs = 6; 1769 else 1770 qparam.aifs = 3; 1771 break; 1772 case IEEE80211_AC_VI: 1773 qparam.cw_max = aCWmin; 1774 qparam.cw_min = (aCWmin + 1) / 2 - 1; 1775 if (is_ocb) 1776 qparam.txop = 0; 1777 else if (use_11b) 1778 qparam.txop = 6016/32; 1779 else 1780 qparam.txop = 3008/32; 1781 1782 if (is_ocb) 1783 qparam.aifs = 3; 1784 else 1785 qparam.aifs = 2; 1786 break; 1787 case IEEE80211_AC_VO: 1788 qparam.cw_max = (aCWmin + 1) / 2 - 1; 1789 qparam.cw_min = (aCWmin + 1) / 4 - 1; 1790 if (is_ocb) 1791 qparam.txop = 0; 1792 else if (use_11b) 1793 qparam.txop = 3264/32; 1794 else 1795 qparam.txop = 1504/32; 1796 qparam.aifs = 2; 1797 break; 1798 } 1799 } 1800 ieee80211_regulatory_limit_wmm_params(sdata, &qparam, ac); 1801 1802 qparam.uapsd = false; 1803 1804 link->tx_conf[ac] = qparam; 1805 drv_conf_tx(local, link, ac, &qparam); 1806 } 1807 1808 if (sdata->vif.type != NL80211_IFTYPE_MONITOR && 1809 sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE && 1810 sdata->vif.type != NL80211_IFTYPE_NAN) { 1811 link->conf->qos = enable_qos; 1812 if (bss_notify) 1813 ieee80211_link_info_change_notify(sdata, link, 1814 BSS_CHANGED_QOS); 1815 } 1816 } 1817 1818 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata, 1819 u16 transaction, u16 auth_alg, u16 status, 1820 const u8 *extra, size_t extra_len, const u8 *da, 1821 const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx, 1822 u32 tx_flags) 1823 { 1824 struct ieee80211_local *local = sdata->local; 1825 struct sk_buff *skb; 1826 struct ieee80211_mgmt *mgmt; 1827 bool multi_link = sdata->vif.valid_links; 1828 struct { 1829 u8 id; 1830 u8 len; 1831 u8 ext_id; 1832 struct ieee80211_multi_link_elem ml; 1833 struct ieee80211_mle_basic_common_info basic; 1834 } __packed mle = { 1835 .id = WLAN_EID_EXTENSION, 1836 .len = sizeof(mle) - 2, 1837 .ext_id = WLAN_EID_EXT_EHT_MULTI_LINK, 1838 .ml.control = cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_BASIC), 1839 .basic.len = sizeof(mle.basic), 1840 }; 1841 int err; 1842 1843 memcpy(mle.basic.mld_mac_addr, sdata->vif.addr, ETH_ALEN); 1844 1845 /* 24 + 6 = header + auth_algo + auth_transaction + status_code */ 1846 skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN + 1847 24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN + 1848 multi_link * sizeof(mle)); 1849 if (!skb) 1850 return; 1851 1852 skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN); 1853 1854 mgmt = skb_put_zero(skb, 24 + 6); 1855 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | 1856 IEEE80211_STYPE_AUTH); 1857 memcpy(mgmt->da, da, ETH_ALEN); 1858 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN); 1859 memcpy(mgmt->bssid, bssid, ETH_ALEN); 1860 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg); 1861 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction); 1862 mgmt->u.auth.status_code = cpu_to_le16(status); 1863 if (extra) 1864 skb_put_data(skb, extra, extra_len); 1865 if (multi_link) 1866 skb_put_data(skb, &mle, sizeof(mle)); 1867 1868 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) { 1869 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED); 1870 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx); 1871 if (WARN_ON(err)) { 1872 kfree_skb(skb); 1873 return; 1874 } 1875 } 1876 1877 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT | 1878 tx_flags; 1879 ieee80211_tx_skb(sdata, skb); 1880 } 1881 1882 void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata, 1883 const u8 *da, const u8 *bssid, 1884 u16 stype, u16 reason, 1885 bool send_frame, u8 *frame_buf) 1886 { 1887 struct ieee80211_local *local = sdata->local; 1888 struct sk_buff *skb; 1889 struct ieee80211_mgmt *mgmt = (void *)frame_buf; 1890 1891 /* build frame */ 1892 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype); 1893 mgmt->duration = 0; /* initialize only */ 1894 mgmt->seq_ctrl = 0; /* initialize only */ 1895 memcpy(mgmt->da, da, ETH_ALEN); 1896 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN); 1897 memcpy(mgmt->bssid, bssid, ETH_ALEN); 1898 /* u.deauth.reason_code == u.disassoc.reason_code */ 1899 mgmt->u.deauth.reason_code = cpu_to_le16(reason); 1900 1901 if (send_frame) { 1902 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 1903 IEEE80211_DEAUTH_FRAME_LEN); 1904 if (!skb) 1905 return; 1906 1907 skb_reserve(skb, local->hw.extra_tx_headroom); 1908 1909 /* copy in frame */ 1910 skb_put_data(skb, mgmt, IEEE80211_DEAUTH_FRAME_LEN); 1911 1912 if (sdata->vif.type != NL80211_IFTYPE_STATION || 1913 !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED)) 1914 IEEE80211_SKB_CB(skb)->flags |= 1915 IEEE80211_TX_INTFL_DONT_ENCRYPT; 1916 1917 ieee80211_tx_skb(sdata, skb); 1918 } 1919 } 1920 1921 static u8 *ieee80211_write_he_6ghz_cap(u8 *pos, __le16 cap, u8 *end) 1922 { 1923 if ((end - pos) < 5) 1924 return pos; 1925 1926 *pos++ = WLAN_EID_EXTENSION; 1927 *pos++ = 1 + sizeof(cap); 1928 *pos++ = WLAN_EID_EXT_HE_6GHZ_CAPA; 1929 memcpy(pos, &cap, sizeof(cap)); 1930 1931 return pos + 2; 1932 } 1933 1934 static int ieee80211_build_preq_ies_band(struct ieee80211_sub_if_data *sdata, 1935 u8 *buffer, size_t buffer_len, 1936 const u8 *ie, size_t ie_len, 1937 enum nl80211_band band, 1938 u32 rate_mask, 1939 struct cfg80211_chan_def *chandef, 1940 size_t *offset, u32 flags) 1941 { 1942 struct ieee80211_local *local = sdata->local; 1943 struct ieee80211_supported_band *sband; 1944 const struct ieee80211_sta_he_cap *he_cap; 1945 const struct ieee80211_sta_eht_cap *eht_cap; 1946 u8 *pos = buffer, *end = buffer + buffer_len; 1947 size_t noffset; 1948 int supp_rates_len, i; 1949 u8 rates[32]; 1950 int num_rates; 1951 int ext_rates_len; 1952 int shift; 1953 u32 rate_flags; 1954 bool have_80mhz = false; 1955 1956 *offset = 0; 1957 1958 sband = local->hw.wiphy->bands[band]; 1959 if (WARN_ON_ONCE(!sband)) 1960 return 0; 1961 1962 rate_flags = ieee80211_chandef_rate_flags(chandef); 1963 shift = ieee80211_chandef_get_shift(chandef); 1964 1965 /* For direct scan add S1G IE and consider its override bits */ 1966 if (band == NL80211_BAND_S1GHZ) { 1967 if (end - pos < 2 + sizeof(struct ieee80211_s1g_cap)) 1968 goto out_err; 1969 pos = ieee80211_ie_build_s1g_cap(pos, &sband->s1g_cap); 1970 goto done; 1971 } 1972 1973 num_rates = 0; 1974 for (i = 0; i < sband->n_bitrates; i++) { 1975 if ((BIT(i) & rate_mask) == 0) 1976 continue; /* skip rate */ 1977 if ((rate_flags & sband->bitrates[i].flags) != rate_flags) 1978 continue; 1979 1980 rates[num_rates++] = 1981 (u8) DIV_ROUND_UP(sband->bitrates[i].bitrate, 1982 (1 << shift) * 5); 1983 } 1984 1985 supp_rates_len = min_t(int, num_rates, 8); 1986 1987 if (end - pos < 2 + supp_rates_len) 1988 goto out_err; 1989 *pos++ = WLAN_EID_SUPP_RATES; 1990 *pos++ = supp_rates_len; 1991 memcpy(pos, rates, supp_rates_len); 1992 pos += supp_rates_len; 1993 1994 /* insert "request information" if in custom IEs */ 1995 if (ie && ie_len) { 1996 static const u8 before_extrates[] = { 1997 WLAN_EID_SSID, 1998 WLAN_EID_SUPP_RATES, 1999 WLAN_EID_REQUEST, 2000 }; 2001 noffset = ieee80211_ie_split(ie, ie_len, 2002 before_extrates, 2003 ARRAY_SIZE(before_extrates), 2004 *offset); 2005 if (end - pos < noffset - *offset) 2006 goto out_err; 2007 memcpy(pos, ie + *offset, noffset - *offset); 2008 pos += noffset - *offset; 2009 *offset = noffset; 2010 } 2011 2012 ext_rates_len = num_rates - supp_rates_len; 2013 if (ext_rates_len > 0) { 2014 if (end - pos < 2 + ext_rates_len) 2015 goto out_err; 2016 *pos++ = WLAN_EID_EXT_SUPP_RATES; 2017 *pos++ = ext_rates_len; 2018 memcpy(pos, rates + supp_rates_len, ext_rates_len); 2019 pos += ext_rates_len; 2020 } 2021 2022 if (chandef->chan && sband->band == NL80211_BAND_2GHZ) { 2023 if (end - pos < 3) 2024 goto out_err; 2025 *pos++ = WLAN_EID_DS_PARAMS; 2026 *pos++ = 1; 2027 *pos++ = ieee80211_frequency_to_channel( 2028 chandef->chan->center_freq); 2029 } 2030 2031 if (flags & IEEE80211_PROBE_FLAG_MIN_CONTENT) 2032 goto done; 2033 2034 /* insert custom IEs that go before HT */ 2035 if (ie && ie_len) { 2036 static const u8 before_ht[] = { 2037 /* 2038 * no need to list the ones split off already 2039 * (or generated here) 2040 */ 2041 WLAN_EID_DS_PARAMS, 2042 WLAN_EID_SUPPORTED_REGULATORY_CLASSES, 2043 }; 2044 noffset = ieee80211_ie_split(ie, ie_len, 2045 before_ht, ARRAY_SIZE(before_ht), 2046 *offset); 2047 if (end - pos < noffset - *offset) 2048 goto out_err; 2049 memcpy(pos, ie + *offset, noffset - *offset); 2050 pos += noffset - *offset; 2051 *offset = noffset; 2052 } 2053 2054 if (sband->ht_cap.ht_supported) { 2055 if (end - pos < 2 + sizeof(struct ieee80211_ht_cap)) 2056 goto out_err; 2057 pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap, 2058 sband->ht_cap.cap); 2059 } 2060 2061 /* insert custom IEs that go before VHT */ 2062 if (ie && ie_len) { 2063 static const u8 before_vht[] = { 2064 /* 2065 * no need to list the ones split off already 2066 * (or generated here) 2067 */ 2068 WLAN_EID_BSS_COEX_2040, 2069 WLAN_EID_EXT_CAPABILITY, 2070 WLAN_EID_SSID_LIST, 2071 WLAN_EID_CHANNEL_USAGE, 2072 WLAN_EID_INTERWORKING, 2073 WLAN_EID_MESH_ID, 2074 /* 60 GHz (Multi-band, DMG, MMS) can't happen */ 2075 }; 2076 noffset = ieee80211_ie_split(ie, ie_len, 2077 before_vht, ARRAY_SIZE(before_vht), 2078 *offset); 2079 if (end - pos < noffset - *offset) 2080 goto out_err; 2081 memcpy(pos, ie + *offset, noffset - *offset); 2082 pos += noffset - *offset; 2083 *offset = noffset; 2084 } 2085 2086 /* Check if any channel in this sband supports at least 80 MHz */ 2087 for (i = 0; i < sband->n_channels; i++) { 2088 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED | 2089 IEEE80211_CHAN_NO_80MHZ)) 2090 continue; 2091 2092 have_80mhz = true; 2093 break; 2094 } 2095 2096 if (sband->vht_cap.vht_supported && have_80mhz) { 2097 if (end - pos < 2 + sizeof(struct ieee80211_vht_cap)) 2098 goto out_err; 2099 pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap, 2100 sband->vht_cap.cap); 2101 } 2102 2103 /* insert custom IEs that go before HE */ 2104 if (ie && ie_len) { 2105 static const u8 before_he[] = { 2106 /* 2107 * no need to list the ones split off before VHT 2108 * or generated here 2109 */ 2110 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_REQ_PARAMS, 2111 WLAN_EID_AP_CSN, 2112 /* TODO: add 11ah/11aj/11ak elements */ 2113 }; 2114 noffset = ieee80211_ie_split(ie, ie_len, 2115 before_he, ARRAY_SIZE(before_he), 2116 *offset); 2117 if (end - pos < noffset - *offset) 2118 goto out_err; 2119 memcpy(pos, ie + *offset, noffset - *offset); 2120 pos += noffset - *offset; 2121 *offset = noffset; 2122 } 2123 2124 he_cap = ieee80211_get_he_iftype_cap(sband, 2125 ieee80211_vif_type_p2p(&sdata->vif)); 2126 if (he_cap && 2127 cfg80211_any_usable_channels(local->hw.wiphy, BIT(sband->band), 2128 IEEE80211_CHAN_NO_HE)) { 2129 pos = ieee80211_ie_build_he_cap(0, pos, he_cap, end); 2130 if (!pos) 2131 goto out_err; 2132 } 2133 2134 eht_cap = ieee80211_get_eht_iftype_cap(sband, 2135 ieee80211_vif_type_p2p(&sdata->vif)); 2136 2137 if (eht_cap && 2138 cfg80211_any_usable_channels(local->hw.wiphy, BIT(sband->band), 2139 IEEE80211_CHAN_NO_HE | 2140 IEEE80211_CHAN_NO_EHT)) { 2141 pos = ieee80211_ie_build_eht_cap(pos, he_cap, eht_cap, end, 2142 sdata->vif.type == NL80211_IFTYPE_AP); 2143 if (!pos) 2144 goto out_err; 2145 } 2146 2147 if (cfg80211_any_usable_channels(local->hw.wiphy, 2148 BIT(NL80211_BAND_6GHZ), 2149 IEEE80211_CHAN_NO_HE)) { 2150 struct ieee80211_supported_band *sband6; 2151 2152 sband6 = local->hw.wiphy->bands[NL80211_BAND_6GHZ]; 2153 he_cap = ieee80211_get_he_iftype_cap(sband6, 2154 ieee80211_vif_type_p2p(&sdata->vif)); 2155 2156 if (he_cap) { 2157 enum nl80211_iftype iftype = 2158 ieee80211_vif_type_p2p(&sdata->vif); 2159 __le16 cap = ieee80211_get_he_6ghz_capa(sband6, iftype); 2160 2161 pos = ieee80211_write_he_6ghz_cap(pos, cap, end); 2162 } 2163 } 2164 2165 /* 2166 * If adding more here, adjust code in main.c 2167 * that calculates local->scan_ies_len. 2168 */ 2169 2170 return pos - buffer; 2171 out_err: 2172 WARN_ONCE(1, "not enough space for preq IEs\n"); 2173 done: 2174 return pos - buffer; 2175 } 2176 2177 int ieee80211_build_preq_ies(struct ieee80211_sub_if_data *sdata, u8 *buffer, 2178 size_t buffer_len, 2179 struct ieee80211_scan_ies *ie_desc, 2180 const u8 *ie, size_t ie_len, 2181 u8 bands_used, u32 *rate_masks, 2182 struct cfg80211_chan_def *chandef, 2183 u32 flags) 2184 { 2185 size_t pos = 0, old_pos = 0, custom_ie_offset = 0; 2186 int i; 2187 2188 memset(ie_desc, 0, sizeof(*ie_desc)); 2189 2190 for (i = 0; i < NUM_NL80211_BANDS; i++) { 2191 if (bands_used & BIT(i)) { 2192 pos += ieee80211_build_preq_ies_band(sdata, 2193 buffer + pos, 2194 buffer_len - pos, 2195 ie, ie_len, i, 2196 rate_masks[i], 2197 chandef, 2198 &custom_ie_offset, 2199 flags); 2200 ie_desc->ies[i] = buffer + old_pos; 2201 ie_desc->len[i] = pos - old_pos; 2202 old_pos = pos; 2203 } 2204 } 2205 2206 /* add any remaining custom IEs */ 2207 if (ie && ie_len) { 2208 if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset, 2209 "not enough space for preq custom IEs\n")) 2210 return pos; 2211 memcpy(buffer + pos, ie + custom_ie_offset, 2212 ie_len - custom_ie_offset); 2213 ie_desc->common_ies = buffer + pos; 2214 ie_desc->common_ie_len = ie_len - custom_ie_offset; 2215 pos += ie_len - custom_ie_offset; 2216 } 2217 2218 return pos; 2219 }; 2220 2221 struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata, 2222 const u8 *src, const u8 *dst, 2223 u32 ratemask, 2224 struct ieee80211_channel *chan, 2225 const u8 *ssid, size_t ssid_len, 2226 const u8 *ie, size_t ie_len, 2227 u32 flags) 2228 { 2229 struct ieee80211_local *local = sdata->local; 2230 struct cfg80211_chan_def chandef; 2231 struct sk_buff *skb; 2232 struct ieee80211_mgmt *mgmt; 2233 int ies_len; 2234 u32 rate_masks[NUM_NL80211_BANDS] = {}; 2235 struct ieee80211_scan_ies dummy_ie_desc; 2236 2237 /* 2238 * Do not send DS Channel parameter for directed probe requests 2239 * in order to maximize the chance that we get a response. Some 2240 * badly-behaved APs don't respond when this parameter is included. 2241 */ 2242 chandef.width = sdata->vif.bss_conf.chandef.width; 2243 if (flags & IEEE80211_PROBE_FLAG_DIRECTED) 2244 chandef.chan = NULL; 2245 else 2246 chandef.chan = chan; 2247 2248 skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len, 2249 local->scan_ies_len + ie_len); 2250 if (!skb) 2251 return NULL; 2252 2253 rate_masks[chan->band] = ratemask; 2254 ies_len = ieee80211_build_preq_ies(sdata, skb_tail_pointer(skb), 2255 skb_tailroom(skb), &dummy_ie_desc, 2256 ie, ie_len, BIT(chan->band), 2257 rate_masks, &chandef, flags); 2258 skb_put(skb, ies_len); 2259 2260 if (dst) { 2261 mgmt = (struct ieee80211_mgmt *) skb->data; 2262 memcpy(mgmt->da, dst, ETH_ALEN); 2263 memcpy(mgmt->bssid, dst, ETH_ALEN); 2264 } 2265 2266 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT; 2267 2268 return skb; 2269 } 2270 2271 u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata, 2272 struct ieee802_11_elems *elems, 2273 enum nl80211_band band, u32 *basic_rates) 2274 { 2275 struct ieee80211_supported_band *sband; 2276 size_t num_rates; 2277 u32 supp_rates, rate_flags; 2278 int i, j, shift; 2279 2280 sband = sdata->local->hw.wiphy->bands[band]; 2281 if (WARN_ON(!sband)) 2282 return 1; 2283 2284 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef); 2285 shift = ieee80211_vif_get_shift(&sdata->vif); 2286 2287 num_rates = sband->n_bitrates; 2288 supp_rates = 0; 2289 for (i = 0; i < elems->supp_rates_len + 2290 elems->ext_supp_rates_len; i++) { 2291 u8 rate = 0; 2292 int own_rate; 2293 bool is_basic; 2294 if (i < elems->supp_rates_len) 2295 rate = elems->supp_rates[i]; 2296 else if (elems->ext_supp_rates) 2297 rate = elems->ext_supp_rates 2298 [i - elems->supp_rates_len]; 2299 own_rate = 5 * (rate & 0x7f); 2300 is_basic = !!(rate & 0x80); 2301 2302 if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY) 2303 continue; 2304 2305 for (j = 0; j < num_rates; j++) { 2306 int brate; 2307 if ((rate_flags & sband->bitrates[j].flags) 2308 != rate_flags) 2309 continue; 2310 2311 brate = DIV_ROUND_UP(sband->bitrates[j].bitrate, 2312 1 << shift); 2313 2314 if (brate == own_rate) { 2315 supp_rates |= BIT(j); 2316 if (basic_rates && is_basic) 2317 *basic_rates |= BIT(j); 2318 } 2319 } 2320 } 2321 return supp_rates; 2322 } 2323 2324 void ieee80211_stop_device(struct ieee80211_local *local) 2325 { 2326 ieee80211_led_radio(local, false); 2327 ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO); 2328 2329 cancel_work_sync(&local->reconfig_filter); 2330 2331 flush_workqueue(local->workqueue); 2332 drv_stop(local); 2333 } 2334 2335 static void ieee80211_flush_completed_scan(struct ieee80211_local *local, 2336 bool aborted) 2337 { 2338 /* It's possible that we don't handle the scan completion in 2339 * time during suspend, so if it's still marked as completed 2340 * here, queue the work and flush it to clean things up. 2341 * Instead of calling the worker function directly here, we 2342 * really queue it to avoid potential races with other flows 2343 * scheduling the same work. 2344 */ 2345 if (test_bit(SCAN_COMPLETED, &local->scanning)) { 2346 /* If coming from reconfiguration failure, abort the scan so 2347 * we don't attempt to continue a partial HW scan - which is 2348 * possible otherwise if (e.g.) the 2.4 GHz portion was the 2349 * completed scan, and a 5 GHz portion is still pending. 2350 */ 2351 if (aborted) 2352 set_bit(SCAN_ABORTED, &local->scanning); 2353 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0); 2354 flush_delayed_work(&local->scan_work); 2355 } 2356 } 2357 2358 static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local) 2359 { 2360 struct ieee80211_sub_if_data *sdata; 2361 struct ieee80211_chanctx *ctx; 2362 2363 /* 2364 * We get here if during resume the device can't be restarted properly. 2365 * We might also get here if this happens during HW reset, which is a 2366 * slightly different situation and we need to drop all connections in 2367 * the latter case. 2368 * 2369 * Ask cfg80211 to turn off all interfaces, this will result in more 2370 * warnings but at least we'll then get into a clean stopped state. 2371 */ 2372 2373 local->resuming = false; 2374 local->suspended = false; 2375 local->in_reconfig = false; 2376 local->reconfig_failure = true; 2377 2378 ieee80211_flush_completed_scan(local, true); 2379 2380 /* scheduled scan clearly can't be running any more, but tell 2381 * cfg80211 and clear local state 2382 */ 2383 ieee80211_sched_scan_end(local); 2384 2385 list_for_each_entry(sdata, &local->interfaces, list) 2386 sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER; 2387 2388 /* Mark channel contexts as not being in the driver any more to avoid 2389 * removing them from the driver during the shutdown process... 2390 */ 2391 mutex_lock(&local->chanctx_mtx); 2392 list_for_each_entry(ctx, &local->chanctx_list, list) 2393 ctx->driver_present = false; 2394 mutex_unlock(&local->chanctx_mtx); 2395 } 2396 2397 static void ieee80211_assign_chanctx(struct ieee80211_local *local, 2398 struct ieee80211_sub_if_data *sdata, 2399 struct ieee80211_link_data *link) 2400 { 2401 struct ieee80211_chanctx_conf *conf; 2402 struct ieee80211_chanctx *ctx; 2403 2404 if (!local->use_chanctx) 2405 return; 2406 2407 mutex_lock(&local->chanctx_mtx); 2408 conf = rcu_dereference_protected(link->conf->chanctx_conf, 2409 lockdep_is_held(&local->chanctx_mtx)); 2410 if (conf) { 2411 ctx = container_of(conf, struct ieee80211_chanctx, conf); 2412 drv_assign_vif_chanctx(local, sdata, link->conf, ctx); 2413 } 2414 mutex_unlock(&local->chanctx_mtx); 2415 } 2416 2417 static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata) 2418 { 2419 struct ieee80211_local *local = sdata->local; 2420 struct sta_info *sta; 2421 2422 /* add STAs back */ 2423 mutex_lock(&local->sta_mtx); 2424 list_for_each_entry(sta, &local->sta_list, list) { 2425 enum ieee80211_sta_state state; 2426 2427 if (!sta->uploaded || sta->sdata != sdata) 2428 continue; 2429 2430 for (state = IEEE80211_STA_NOTEXIST; 2431 state < sta->sta_state; state++) 2432 WARN_ON(drv_sta_state(local, sta->sdata, sta, state, 2433 state + 1)); 2434 } 2435 mutex_unlock(&local->sta_mtx); 2436 } 2437 2438 static int ieee80211_reconfig_nan(struct ieee80211_sub_if_data *sdata) 2439 { 2440 struct cfg80211_nan_func *func, **funcs; 2441 int res, id, i = 0; 2442 2443 res = drv_start_nan(sdata->local, sdata, 2444 &sdata->u.nan.conf); 2445 if (WARN_ON(res)) 2446 return res; 2447 2448 funcs = kcalloc(sdata->local->hw.max_nan_de_entries + 1, 2449 sizeof(*funcs), 2450 GFP_KERNEL); 2451 if (!funcs) 2452 return -ENOMEM; 2453 2454 /* Add all the functions: 2455 * This is a little bit ugly. We need to call a potentially sleeping 2456 * callback for each NAN function, so we can't hold the spinlock. 2457 */ 2458 spin_lock_bh(&sdata->u.nan.func_lock); 2459 2460 idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id) 2461 funcs[i++] = func; 2462 2463 spin_unlock_bh(&sdata->u.nan.func_lock); 2464 2465 for (i = 0; funcs[i]; i++) { 2466 res = drv_add_nan_func(sdata->local, sdata, funcs[i]); 2467 if (WARN_ON(res)) 2468 ieee80211_nan_func_terminated(&sdata->vif, 2469 funcs[i]->instance_id, 2470 NL80211_NAN_FUNC_TERM_REASON_ERROR, 2471 GFP_KERNEL); 2472 } 2473 2474 kfree(funcs); 2475 2476 return 0; 2477 } 2478 2479 static void ieee80211_reconfig_ap_links(struct ieee80211_local *local, 2480 struct ieee80211_sub_if_data *sdata, 2481 u64 changed) 2482 { 2483 int link_id; 2484 2485 for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) { 2486 struct ieee80211_link_data *link; 2487 2488 if (!(sdata->vif.active_links & BIT(link_id))) 2489 continue; 2490 2491 link = sdata_dereference(sdata->link[link_id], sdata); 2492 if (!link) 2493 continue; 2494 2495 if (rcu_access_pointer(link->u.ap.beacon)) 2496 drv_start_ap(local, sdata, link->conf); 2497 2498 if (!link->conf->enable_beacon) 2499 continue; 2500 2501 changed |= BSS_CHANGED_BEACON | 2502 BSS_CHANGED_BEACON_ENABLED; 2503 2504 ieee80211_link_info_change_notify(sdata, link, changed); 2505 } 2506 } 2507 2508 int ieee80211_reconfig(struct ieee80211_local *local) 2509 { 2510 struct ieee80211_hw *hw = &local->hw; 2511 struct ieee80211_sub_if_data *sdata; 2512 struct ieee80211_chanctx *ctx; 2513 struct sta_info *sta; 2514 int res, i; 2515 bool reconfig_due_to_wowlan = false; 2516 struct ieee80211_sub_if_data *sched_scan_sdata; 2517 struct cfg80211_sched_scan_request *sched_scan_req; 2518 bool sched_scan_stopped = false; 2519 bool suspended = local->suspended; 2520 bool in_reconfig = false; 2521 2522 /* nothing to do if HW shouldn't run */ 2523 if (!local->open_count) 2524 goto wake_up; 2525 2526 #ifdef CONFIG_PM 2527 if (suspended) 2528 local->resuming = true; 2529 2530 if (local->wowlan) { 2531 /* 2532 * In the wowlan case, both mac80211 and the device 2533 * are functional when the resume op is called, so 2534 * clear local->suspended so the device could operate 2535 * normally (e.g. pass rx frames). 2536 */ 2537 local->suspended = false; 2538 res = drv_resume(local); 2539 local->wowlan = false; 2540 if (res < 0) { 2541 local->resuming = false; 2542 return res; 2543 } 2544 if (res == 0) 2545 goto wake_up; 2546 WARN_ON(res > 1); 2547 /* 2548 * res is 1, which means the driver requested 2549 * to go through a regular reset on wakeup. 2550 * restore local->suspended in this case. 2551 */ 2552 reconfig_due_to_wowlan = true; 2553 local->suspended = true; 2554 } 2555 #endif 2556 2557 /* 2558 * In case of hw_restart during suspend (without wowlan), 2559 * cancel restart work, as we are reconfiguring the device 2560 * anyway. 2561 * Note that restart_work is scheduled on a frozen workqueue, 2562 * so we can't deadlock in this case. 2563 */ 2564 if (suspended && local->in_reconfig && !reconfig_due_to_wowlan) 2565 cancel_work_sync(&local->restart_work); 2566 2567 local->started = false; 2568 2569 /* 2570 * Upon resume hardware can sometimes be goofy due to 2571 * various platform / driver / bus issues, so restarting 2572 * the device may at times not work immediately. Propagate 2573 * the error. 2574 */ 2575 res = drv_start(local); 2576 if (res) { 2577 if (suspended) 2578 WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n"); 2579 else 2580 WARN(1, "Hardware became unavailable during restart.\n"); 2581 ieee80211_handle_reconfig_failure(local); 2582 return res; 2583 } 2584 2585 /* setup fragmentation threshold */ 2586 drv_set_frag_threshold(local, hw->wiphy->frag_threshold); 2587 2588 /* setup RTS threshold */ 2589 drv_set_rts_threshold(local, hw->wiphy->rts_threshold); 2590 2591 /* reset coverage class */ 2592 drv_set_coverage_class(local, hw->wiphy->coverage_class); 2593 2594 ieee80211_led_radio(local, true); 2595 ieee80211_mod_tpt_led_trig(local, 2596 IEEE80211_TPT_LEDTRIG_FL_RADIO, 0); 2597 2598 /* add interfaces */ 2599 sdata = wiphy_dereference(local->hw.wiphy, local->monitor_sdata); 2600 if (sdata) { 2601 /* in HW restart it exists already */ 2602 WARN_ON(local->resuming); 2603 res = drv_add_interface(local, sdata); 2604 if (WARN_ON(res)) { 2605 RCU_INIT_POINTER(local->monitor_sdata, NULL); 2606 synchronize_net(); 2607 kfree(sdata); 2608 } 2609 } 2610 2611 list_for_each_entry(sdata, &local->interfaces, list) { 2612 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN && 2613 sdata->vif.type != NL80211_IFTYPE_MONITOR && 2614 ieee80211_sdata_running(sdata)) { 2615 res = drv_add_interface(local, sdata); 2616 if (WARN_ON(res)) 2617 break; 2618 } 2619 } 2620 2621 /* If adding any of the interfaces failed above, roll back and 2622 * report failure. 2623 */ 2624 if (res) { 2625 list_for_each_entry_continue_reverse(sdata, &local->interfaces, 2626 list) 2627 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN && 2628 sdata->vif.type != NL80211_IFTYPE_MONITOR && 2629 ieee80211_sdata_running(sdata)) 2630 drv_remove_interface(local, sdata); 2631 ieee80211_handle_reconfig_failure(local); 2632 return res; 2633 } 2634 2635 /* add channel contexts */ 2636 if (local->use_chanctx) { 2637 mutex_lock(&local->chanctx_mtx); 2638 list_for_each_entry(ctx, &local->chanctx_list, list) 2639 if (ctx->replace_state != 2640 IEEE80211_CHANCTX_REPLACES_OTHER) 2641 WARN_ON(drv_add_chanctx(local, ctx)); 2642 mutex_unlock(&local->chanctx_mtx); 2643 2644 sdata = wiphy_dereference(local->hw.wiphy, 2645 local->monitor_sdata); 2646 if (sdata && ieee80211_sdata_running(sdata)) 2647 ieee80211_assign_chanctx(local, sdata, &sdata->deflink); 2648 } 2649 2650 /* reconfigure hardware */ 2651 ieee80211_hw_config(local, ~0); 2652 2653 ieee80211_configure_filter(local); 2654 2655 /* Finally also reconfigure all the BSS information */ 2656 list_for_each_entry(sdata, &local->interfaces, list) { 2657 /* common change flags for all interface types - link only */ 2658 u64 changed = BSS_CHANGED_ERP_CTS_PROT | 2659 BSS_CHANGED_ERP_PREAMBLE | 2660 BSS_CHANGED_ERP_SLOT | 2661 BSS_CHANGED_HT | 2662 BSS_CHANGED_BASIC_RATES | 2663 BSS_CHANGED_BEACON_INT | 2664 BSS_CHANGED_BSSID | 2665 BSS_CHANGED_CQM | 2666 BSS_CHANGED_QOS | 2667 BSS_CHANGED_TXPOWER | 2668 BSS_CHANGED_MCAST_RATE; 2669 struct ieee80211_link_data *link = NULL; 2670 unsigned int link_id; 2671 u32 active_links = 0; 2672 2673 if (!ieee80211_sdata_running(sdata)) 2674 continue; 2675 2676 sdata_lock(sdata); 2677 if (sdata->vif.valid_links) { 2678 struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS] = { 2679 [0] = &sdata->vif.bss_conf, 2680 }; 2681 2682 if (sdata->vif.type == NL80211_IFTYPE_STATION) { 2683 /* start with a single active link */ 2684 active_links = sdata->vif.active_links; 2685 link_id = ffs(active_links) - 1; 2686 sdata->vif.active_links = BIT(link_id); 2687 } 2688 2689 drv_change_vif_links(local, sdata, 0, 2690 sdata->vif.active_links, 2691 old); 2692 } 2693 2694 for (link_id = 0; 2695 link_id < ARRAY_SIZE(sdata->vif.link_conf); 2696 link_id++) { 2697 if (sdata->vif.valid_links && 2698 !(sdata->vif.active_links & BIT(link_id))) 2699 continue; 2700 2701 link = sdata_dereference(sdata->link[link_id], sdata); 2702 if (!link) 2703 continue; 2704 2705 ieee80211_assign_chanctx(local, sdata, link); 2706 } 2707 2708 switch (sdata->vif.type) { 2709 case NL80211_IFTYPE_AP_VLAN: 2710 case NL80211_IFTYPE_MONITOR: 2711 break; 2712 case NL80211_IFTYPE_ADHOC: 2713 if (sdata->vif.cfg.ibss_joined) 2714 WARN_ON(drv_join_ibss(local, sdata)); 2715 fallthrough; 2716 default: 2717 ieee80211_reconfig_stations(sdata); 2718 fallthrough; 2719 case NL80211_IFTYPE_AP: /* AP stations are handled later */ 2720 for (i = 0; i < IEEE80211_NUM_ACS; i++) 2721 drv_conf_tx(local, &sdata->deflink, i, 2722 &sdata->deflink.tx_conf[i]); 2723 break; 2724 } 2725 2726 if (sdata->vif.bss_conf.mu_mimo_owner) 2727 changed |= BSS_CHANGED_MU_GROUPS; 2728 2729 if (!sdata->vif.valid_links) 2730 changed |= BSS_CHANGED_IDLE; 2731 2732 switch (sdata->vif.type) { 2733 case NL80211_IFTYPE_STATION: 2734 if (!sdata->vif.valid_links) { 2735 changed |= BSS_CHANGED_ASSOC | 2736 BSS_CHANGED_ARP_FILTER | 2737 BSS_CHANGED_PS; 2738 2739 /* Re-send beacon info report to the driver */ 2740 if (sdata->deflink.u.mgd.have_beacon) 2741 changed |= BSS_CHANGED_BEACON_INFO; 2742 2743 if (sdata->vif.bss_conf.max_idle_period || 2744 sdata->vif.bss_conf.protected_keep_alive) 2745 changed |= BSS_CHANGED_KEEP_ALIVE; 2746 2747 if (sdata->vif.bss_conf.eht_puncturing) 2748 changed |= BSS_CHANGED_EHT_PUNCTURING; 2749 2750 ieee80211_bss_info_change_notify(sdata, 2751 changed); 2752 } else if (!WARN_ON(!link)) { 2753 ieee80211_link_info_change_notify(sdata, link, 2754 changed); 2755 changed = BSS_CHANGED_ASSOC | 2756 BSS_CHANGED_IDLE | 2757 BSS_CHANGED_PS | 2758 BSS_CHANGED_ARP_FILTER; 2759 ieee80211_vif_cfg_change_notify(sdata, changed); 2760 } 2761 break; 2762 case NL80211_IFTYPE_OCB: 2763 changed |= BSS_CHANGED_OCB; 2764 ieee80211_bss_info_change_notify(sdata, changed); 2765 break; 2766 case NL80211_IFTYPE_ADHOC: 2767 changed |= BSS_CHANGED_IBSS; 2768 fallthrough; 2769 case NL80211_IFTYPE_AP: 2770 changed |= BSS_CHANGED_P2P_PS; 2771 2772 if (sdata->vif.valid_links) 2773 ieee80211_vif_cfg_change_notify(sdata, 2774 BSS_CHANGED_SSID); 2775 else 2776 changed |= BSS_CHANGED_SSID; 2777 2778 if (sdata->vif.bss_conf.ftm_responder == 1 && 2779 wiphy_ext_feature_isset(sdata->local->hw.wiphy, 2780 NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER)) 2781 changed |= BSS_CHANGED_FTM_RESPONDER; 2782 2783 if (sdata->vif.type == NL80211_IFTYPE_AP) { 2784 changed |= BSS_CHANGED_AP_PROBE_RESP; 2785 2786 if (sdata->vif.valid_links) { 2787 ieee80211_reconfig_ap_links(local, 2788 sdata, 2789 changed); 2790 break; 2791 } 2792 2793 if (rcu_access_pointer(sdata->deflink.u.ap.beacon)) 2794 drv_start_ap(local, sdata, 2795 sdata->deflink.conf); 2796 } 2797 fallthrough; 2798 case NL80211_IFTYPE_MESH_POINT: 2799 if (sdata->vif.bss_conf.enable_beacon) { 2800 changed |= BSS_CHANGED_BEACON | 2801 BSS_CHANGED_BEACON_ENABLED; 2802 ieee80211_bss_info_change_notify(sdata, changed); 2803 } 2804 break; 2805 case NL80211_IFTYPE_NAN: 2806 res = ieee80211_reconfig_nan(sdata); 2807 if (res < 0) { 2808 sdata_unlock(sdata); 2809 ieee80211_handle_reconfig_failure(local); 2810 return res; 2811 } 2812 break; 2813 case NL80211_IFTYPE_AP_VLAN: 2814 case NL80211_IFTYPE_MONITOR: 2815 case NL80211_IFTYPE_P2P_DEVICE: 2816 /* nothing to do */ 2817 break; 2818 case NL80211_IFTYPE_UNSPECIFIED: 2819 case NUM_NL80211_IFTYPES: 2820 case NL80211_IFTYPE_P2P_CLIENT: 2821 case NL80211_IFTYPE_P2P_GO: 2822 case NL80211_IFTYPE_WDS: 2823 WARN_ON(1); 2824 break; 2825 } 2826 sdata_unlock(sdata); 2827 2828 if (active_links) 2829 ieee80211_set_active_links(&sdata->vif, active_links); 2830 } 2831 2832 ieee80211_recalc_ps(local); 2833 2834 /* 2835 * The sta might be in psm against the ap (e.g. because 2836 * this was the state before a hw restart), so we 2837 * explicitly send a null packet in order to make sure 2838 * it'll sync against the ap (and get out of psm). 2839 */ 2840 if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) { 2841 list_for_each_entry(sdata, &local->interfaces, list) { 2842 if (sdata->vif.type != NL80211_IFTYPE_STATION) 2843 continue; 2844 if (!sdata->u.mgd.associated) 2845 continue; 2846 2847 ieee80211_send_nullfunc(local, sdata, false); 2848 } 2849 } 2850 2851 /* APs are now beaconing, add back stations */ 2852 list_for_each_entry(sdata, &local->interfaces, list) { 2853 if (!ieee80211_sdata_running(sdata)) 2854 continue; 2855 2856 sdata_lock(sdata); 2857 switch (sdata->vif.type) { 2858 case NL80211_IFTYPE_AP_VLAN: 2859 case NL80211_IFTYPE_AP: 2860 ieee80211_reconfig_stations(sdata); 2861 break; 2862 default: 2863 break; 2864 } 2865 sdata_unlock(sdata); 2866 } 2867 2868 /* add back keys */ 2869 list_for_each_entry(sdata, &local->interfaces, list) 2870 ieee80211_reenable_keys(sdata); 2871 2872 /* Reconfigure sched scan if it was interrupted by FW restart */ 2873 mutex_lock(&local->mtx); 2874 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata, 2875 lockdep_is_held(&local->mtx)); 2876 sched_scan_req = rcu_dereference_protected(local->sched_scan_req, 2877 lockdep_is_held(&local->mtx)); 2878 if (sched_scan_sdata && sched_scan_req) 2879 /* 2880 * Sched scan stopped, but we don't want to report it. Instead, 2881 * we're trying to reschedule. However, if more than one scan 2882 * plan was set, we cannot reschedule since we don't know which 2883 * scan plan was currently running (and some scan plans may have 2884 * already finished). 2885 */ 2886 if (sched_scan_req->n_scan_plans > 1 || 2887 __ieee80211_request_sched_scan_start(sched_scan_sdata, 2888 sched_scan_req)) { 2889 RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 2890 RCU_INIT_POINTER(local->sched_scan_req, NULL); 2891 sched_scan_stopped = true; 2892 } 2893 mutex_unlock(&local->mtx); 2894 2895 if (sched_scan_stopped) 2896 cfg80211_sched_scan_stopped_locked(local->hw.wiphy, 0); 2897 2898 wake_up: 2899 2900 if (local->monitors == local->open_count && local->monitors > 0) 2901 ieee80211_add_virtual_monitor(local); 2902 2903 /* 2904 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation 2905 * sessions can be established after a resume. 2906 * 2907 * Also tear down aggregation sessions since reconfiguring 2908 * them in a hardware restart scenario is not easily done 2909 * right now, and the hardware will have lost information 2910 * about the sessions, but we and the AP still think they 2911 * are active. This is really a workaround though. 2912 */ 2913 if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) { 2914 mutex_lock(&local->sta_mtx); 2915 2916 list_for_each_entry(sta, &local->sta_list, list) { 2917 if (!local->resuming) 2918 ieee80211_sta_tear_down_BA_sessions( 2919 sta, AGG_STOP_LOCAL_REQUEST); 2920 clear_sta_flag(sta, WLAN_STA_BLOCK_BA); 2921 } 2922 2923 mutex_unlock(&local->sta_mtx); 2924 } 2925 2926 /* 2927 * If this is for hw restart things are still running. 2928 * We may want to change that later, however. 2929 */ 2930 if (local->open_count && (!suspended || reconfig_due_to_wowlan)) 2931 drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART); 2932 2933 if (local->in_reconfig) { 2934 in_reconfig = local->in_reconfig; 2935 local->in_reconfig = false; 2936 barrier(); 2937 2938 /* Restart deferred ROCs */ 2939 mutex_lock(&local->mtx); 2940 ieee80211_start_next_roc(local); 2941 mutex_unlock(&local->mtx); 2942 2943 /* Requeue all works */ 2944 list_for_each_entry(sdata, &local->interfaces, list) 2945 wiphy_work_queue(local->hw.wiphy, &sdata->work); 2946 } 2947 2948 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP, 2949 IEEE80211_QUEUE_STOP_REASON_SUSPEND, 2950 false); 2951 2952 if (in_reconfig) { 2953 list_for_each_entry(sdata, &local->interfaces, list) { 2954 if (!ieee80211_sdata_running(sdata)) 2955 continue; 2956 if (sdata->vif.type == NL80211_IFTYPE_STATION) 2957 ieee80211_sta_restart(sdata); 2958 } 2959 } 2960 2961 if (!suspended) 2962 return 0; 2963 2964 #ifdef CONFIG_PM 2965 /* first set suspended false, then resuming */ 2966 local->suspended = false; 2967 mb(); 2968 local->resuming = false; 2969 2970 ieee80211_flush_completed_scan(local, false); 2971 2972 if (local->open_count && !reconfig_due_to_wowlan) 2973 drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND); 2974 2975 list_for_each_entry(sdata, &local->interfaces, list) { 2976 if (!ieee80211_sdata_running(sdata)) 2977 continue; 2978 if (sdata->vif.type == NL80211_IFTYPE_STATION) 2979 ieee80211_sta_restart(sdata); 2980 } 2981 2982 mod_timer(&local->sta_cleanup, jiffies + 1); 2983 #else 2984 WARN_ON(1); 2985 #endif 2986 2987 return 0; 2988 } 2989 2990 static void ieee80211_reconfig_disconnect(struct ieee80211_vif *vif, u8 flag) 2991 { 2992 struct ieee80211_sub_if_data *sdata; 2993 struct ieee80211_local *local; 2994 struct ieee80211_key *key; 2995 2996 if (WARN_ON(!vif)) 2997 return; 2998 2999 sdata = vif_to_sdata(vif); 3000 local = sdata->local; 3001 3002 if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_RESUME && 3003 !local->resuming)) 3004 return; 3005 3006 if (WARN_ON(flag & IEEE80211_SDATA_DISCONNECT_HW_RESTART && 3007 !local->in_reconfig)) 3008 return; 3009 3010 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION)) 3011 return; 3012 3013 sdata->flags |= flag; 3014 3015 mutex_lock(&local->key_mtx); 3016 list_for_each_entry(key, &sdata->key_list, list) 3017 key->flags |= KEY_FLAG_TAINTED; 3018 mutex_unlock(&local->key_mtx); 3019 } 3020 3021 void ieee80211_hw_restart_disconnect(struct ieee80211_vif *vif) 3022 { 3023 ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_HW_RESTART); 3024 } 3025 EXPORT_SYMBOL_GPL(ieee80211_hw_restart_disconnect); 3026 3027 void ieee80211_resume_disconnect(struct ieee80211_vif *vif) 3028 { 3029 ieee80211_reconfig_disconnect(vif, IEEE80211_SDATA_DISCONNECT_RESUME); 3030 } 3031 EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect); 3032 3033 void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata, 3034 struct ieee80211_link_data *link) 3035 { 3036 struct ieee80211_local *local = sdata->local; 3037 struct ieee80211_chanctx_conf *chanctx_conf; 3038 struct ieee80211_chanctx *chanctx; 3039 3040 mutex_lock(&local->chanctx_mtx); 3041 3042 chanctx_conf = rcu_dereference_protected(link->conf->chanctx_conf, 3043 lockdep_is_held(&local->chanctx_mtx)); 3044 3045 /* 3046 * This function can be called from a work, thus it may be possible 3047 * that the chanctx_conf is removed (due to a disconnection, for 3048 * example). 3049 * So nothing should be done in such case. 3050 */ 3051 if (!chanctx_conf) 3052 goto unlock; 3053 3054 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf); 3055 ieee80211_recalc_smps_chanctx(local, chanctx); 3056 unlock: 3057 mutex_unlock(&local->chanctx_mtx); 3058 } 3059 3060 void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata, 3061 int link_id) 3062 { 3063 struct ieee80211_local *local = sdata->local; 3064 struct ieee80211_chanctx_conf *chanctx_conf; 3065 struct ieee80211_chanctx *chanctx; 3066 int i; 3067 3068 mutex_lock(&local->chanctx_mtx); 3069 3070 for (i = 0; i < ARRAY_SIZE(sdata->vif.link_conf); i++) { 3071 struct ieee80211_bss_conf *bss_conf; 3072 3073 if (link_id >= 0 && link_id != i) 3074 continue; 3075 3076 rcu_read_lock(); 3077 bss_conf = rcu_dereference(sdata->vif.link_conf[i]); 3078 if (!bss_conf) { 3079 rcu_read_unlock(); 3080 continue; 3081 } 3082 3083 chanctx_conf = rcu_dereference_protected(bss_conf->chanctx_conf, 3084 lockdep_is_held(&local->chanctx_mtx)); 3085 /* 3086 * Since we hold the chanctx_mtx (checked above) 3087 * we can take the chanctx_conf pointer out of the 3088 * RCU critical section, it cannot go away without 3089 * the mutex. Just the way we reached it could - in 3090 * theory - go away, but we don't really care and 3091 * it really shouldn't happen anyway. 3092 */ 3093 rcu_read_unlock(); 3094 3095 if (!chanctx_conf) 3096 goto unlock; 3097 3098 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, 3099 conf); 3100 ieee80211_recalc_chanctx_min_def(local, chanctx, NULL); 3101 } 3102 unlock: 3103 mutex_unlock(&local->chanctx_mtx); 3104 } 3105 3106 size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset) 3107 { 3108 size_t pos = offset; 3109 3110 while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC) 3111 pos += 2 + ies[pos + 1]; 3112 3113 return pos; 3114 } 3115 3116 u8 *ieee80211_ie_build_s1g_cap(u8 *pos, struct ieee80211_sta_s1g_cap *s1g_cap) 3117 { 3118 *pos++ = WLAN_EID_S1G_CAPABILITIES; 3119 *pos++ = sizeof(struct ieee80211_s1g_cap); 3120 memset(pos, 0, sizeof(struct ieee80211_s1g_cap)); 3121 3122 memcpy(pos, &s1g_cap->cap, sizeof(s1g_cap->cap)); 3123 pos += sizeof(s1g_cap->cap); 3124 3125 memcpy(pos, &s1g_cap->nss_mcs, sizeof(s1g_cap->nss_mcs)); 3126 pos += sizeof(s1g_cap->nss_mcs); 3127 3128 return pos; 3129 } 3130 3131 u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap, 3132 u16 cap) 3133 { 3134 __le16 tmp; 3135 3136 *pos++ = WLAN_EID_HT_CAPABILITY; 3137 *pos++ = sizeof(struct ieee80211_ht_cap); 3138 memset(pos, 0, sizeof(struct ieee80211_ht_cap)); 3139 3140 /* capability flags */ 3141 tmp = cpu_to_le16(cap); 3142 memcpy(pos, &tmp, sizeof(u16)); 3143 pos += sizeof(u16); 3144 3145 /* AMPDU parameters */ 3146 *pos++ = ht_cap->ampdu_factor | 3147 (ht_cap->ampdu_density << 3148 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT); 3149 3150 /* MCS set */ 3151 memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs)); 3152 pos += sizeof(ht_cap->mcs); 3153 3154 /* extended capabilities */ 3155 pos += sizeof(__le16); 3156 3157 /* BF capabilities */ 3158 pos += sizeof(__le32); 3159 3160 /* antenna selection */ 3161 pos += sizeof(u8); 3162 3163 return pos; 3164 } 3165 3166 u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap, 3167 u32 cap) 3168 { 3169 __le32 tmp; 3170 3171 *pos++ = WLAN_EID_VHT_CAPABILITY; 3172 *pos++ = sizeof(struct ieee80211_vht_cap); 3173 memset(pos, 0, sizeof(struct ieee80211_vht_cap)); 3174 3175 /* capability flags */ 3176 tmp = cpu_to_le32(cap); 3177 memcpy(pos, &tmp, sizeof(u32)); 3178 pos += sizeof(u32); 3179 3180 /* VHT MCS set */ 3181 memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs)); 3182 pos += sizeof(vht_cap->vht_mcs); 3183 3184 return pos; 3185 } 3186 3187 u8 ieee80211_ie_len_he_cap(struct ieee80211_sub_if_data *sdata, u8 iftype) 3188 { 3189 const struct ieee80211_sta_he_cap *he_cap; 3190 struct ieee80211_supported_band *sband; 3191 u8 n; 3192 3193 sband = ieee80211_get_sband(sdata); 3194 if (!sband) 3195 return 0; 3196 3197 he_cap = ieee80211_get_he_iftype_cap(sband, iftype); 3198 if (!he_cap) 3199 return 0; 3200 3201 n = ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem); 3202 return 2 + 1 + 3203 sizeof(he_cap->he_cap_elem) + n + 3204 ieee80211_he_ppe_size(he_cap->ppe_thres[0], 3205 he_cap->he_cap_elem.phy_cap_info); 3206 } 3207 3208 u8 *ieee80211_ie_build_he_cap(ieee80211_conn_flags_t disable_flags, u8 *pos, 3209 const struct ieee80211_sta_he_cap *he_cap, 3210 u8 *end) 3211 { 3212 struct ieee80211_he_cap_elem elem; 3213 u8 n; 3214 u8 ie_len; 3215 u8 *orig_pos = pos; 3216 3217 /* Make sure we have place for the IE */ 3218 /* 3219 * TODO: the 1 added is because this temporarily is under the EXTENSION 3220 * IE. Get rid of it when it moves. 3221 */ 3222 if (!he_cap) 3223 return orig_pos; 3224 3225 /* modify on stack first to calculate 'n' and 'ie_len' correctly */ 3226 elem = he_cap->he_cap_elem; 3227 3228 if (disable_flags & IEEE80211_CONN_DISABLE_40MHZ) 3229 elem.phy_cap_info[0] &= 3230 ~(IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G | 3231 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G); 3232 3233 if (disable_flags & IEEE80211_CONN_DISABLE_160MHZ) 3234 elem.phy_cap_info[0] &= 3235 ~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G; 3236 3237 if (disable_flags & IEEE80211_CONN_DISABLE_80P80MHZ) 3238 elem.phy_cap_info[0] &= 3239 ~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G; 3240 3241 n = ieee80211_he_mcs_nss_size(&elem); 3242 ie_len = 2 + 1 + 3243 sizeof(he_cap->he_cap_elem) + n + 3244 ieee80211_he_ppe_size(he_cap->ppe_thres[0], 3245 he_cap->he_cap_elem.phy_cap_info); 3246 3247 if ((end - pos) < ie_len) 3248 return orig_pos; 3249 3250 *pos++ = WLAN_EID_EXTENSION; 3251 pos++; /* We'll set the size later below */ 3252 *pos++ = WLAN_EID_EXT_HE_CAPABILITY; 3253 3254 /* Fixed data */ 3255 memcpy(pos, &elem, sizeof(elem)); 3256 pos += sizeof(elem); 3257 3258 memcpy(pos, &he_cap->he_mcs_nss_supp, n); 3259 pos += n; 3260 3261 /* Check if PPE Threshold should be present */ 3262 if ((he_cap->he_cap_elem.phy_cap_info[6] & 3263 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) == 0) 3264 goto end; 3265 3266 /* 3267 * Calculate how many PPET16/PPET8 pairs are to come. Algorithm: 3268 * (NSS_M1 + 1) x (num of 1 bits in RU_INDEX_BITMASK) 3269 */ 3270 n = hweight8(he_cap->ppe_thres[0] & 3271 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK); 3272 n *= (1 + ((he_cap->ppe_thres[0] & IEEE80211_PPE_THRES_NSS_MASK) >> 3273 IEEE80211_PPE_THRES_NSS_POS)); 3274 3275 /* 3276 * Each pair is 6 bits, and we need to add the 7 "header" bits to the 3277 * total size. 3278 */ 3279 n = (n * IEEE80211_PPE_THRES_INFO_PPET_SIZE * 2) + 7; 3280 n = DIV_ROUND_UP(n, 8); 3281 3282 /* Copy PPE Thresholds */ 3283 memcpy(pos, &he_cap->ppe_thres, n); 3284 pos += n; 3285 3286 end: 3287 orig_pos[1] = (pos - orig_pos) - 2; 3288 return pos; 3289 } 3290 3291 void ieee80211_ie_build_he_6ghz_cap(struct ieee80211_sub_if_data *sdata, 3292 enum ieee80211_smps_mode smps_mode, 3293 struct sk_buff *skb) 3294 { 3295 struct ieee80211_supported_band *sband; 3296 const struct ieee80211_sband_iftype_data *iftd; 3297 enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif); 3298 u8 *pos; 3299 u16 cap; 3300 3301 if (!cfg80211_any_usable_channels(sdata->local->hw.wiphy, 3302 BIT(NL80211_BAND_6GHZ), 3303 IEEE80211_CHAN_NO_HE)) 3304 return; 3305 3306 sband = sdata->local->hw.wiphy->bands[NL80211_BAND_6GHZ]; 3307 3308 iftd = ieee80211_get_sband_iftype_data(sband, iftype); 3309 if (!iftd) 3310 return; 3311 3312 /* Check for device HE 6 GHz capability before adding element */ 3313 if (!iftd->he_6ghz_capa.capa) 3314 return; 3315 3316 cap = le16_to_cpu(iftd->he_6ghz_capa.capa); 3317 cap &= ~IEEE80211_HE_6GHZ_CAP_SM_PS; 3318 3319 switch (smps_mode) { 3320 case IEEE80211_SMPS_AUTOMATIC: 3321 case IEEE80211_SMPS_NUM_MODES: 3322 WARN_ON(1); 3323 fallthrough; 3324 case IEEE80211_SMPS_OFF: 3325 cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED, 3326 IEEE80211_HE_6GHZ_CAP_SM_PS); 3327 break; 3328 case IEEE80211_SMPS_STATIC: 3329 cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_STATIC, 3330 IEEE80211_HE_6GHZ_CAP_SM_PS); 3331 break; 3332 case IEEE80211_SMPS_DYNAMIC: 3333 cap |= u16_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC, 3334 IEEE80211_HE_6GHZ_CAP_SM_PS); 3335 break; 3336 } 3337 3338 pos = skb_put(skb, 2 + 1 + sizeof(cap)); 3339 ieee80211_write_he_6ghz_cap(pos, cpu_to_le16(cap), 3340 pos + 2 + 1 + sizeof(cap)); 3341 } 3342 3343 u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap, 3344 const struct cfg80211_chan_def *chandef, 3345 u16 prot_mode, bool rifs_mode) 3346 { 3347 struct ieee80211_ht_operation *ht_oper; 3348 /* Build HT Information */ 3349 *pos++ = WLAN_EID_HT_OPERATION; 3350 *pos++ = sizeof(struct ieee80211_ht_operation); 3351 ht_oper = (struct ieee80211_ht_operation *)pos; 3352 ht_oper->primary_chan = ieee80211_frequency_to_channel( 3353 chandef->chan->center_freq); 3354 switch (chandef->width) { 3355 case NL80211_CHAN_WIDTH_160: 3356 case NL80211_CHAN_WIDTH_80P80: 3357 case NL80211_CHAN_WIDTH_80: 3358 case NL80211_CHAN_WIDTH_40: 3359 if (chandef->center_freq1 > chandef->chan->center_freq) 3360 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE; 3361 else 3362 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW; 3363 break; 3364 case NL80211_CHAN_WIDTH_320: 3365 /* HT information element should not be included on 6GHz */ 3366 WARN_ON(1); 3367 return pos; 3368 default: 3369 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE; 3370 break; 3371 } 3372 if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 && 3373 chandef->width != NL80211_CHAN_WIDTH_20_NOHT && 3374 chandef->width != NL80211_CHAN_WIDTH_20) 3375 ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY; 3376 3377 if (rifs_mode) 3378 ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE; 3379 3380 ht_oper->operation_mode = cpu_to_le16(prot_mode); 3381 ht_oper->stbc_param = 0x0000; 3382 3383 /* It seems that Basic MCS set and Supported MCS set 3384 are identical for the first 10 bytes */ 3385 memset(&ht_oper->basic_set, 0, 16); 3386 memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10); 3387 3388 return pos + sizeof(struct ieee80211_ht_operation); 3389 } 3390 3391 void ieee80211_ie_build_wide_bw_cs(u8 *pos, 3392 const struct cfg80211_chan_def *chandef) 3393 { 3394 *pos++ = WLAN_EID_WIDE_BW_CHANNEL_SWITCH; /* EID */ 3395 *pos++ = 3; /* IE length */ 3396 /* New channel width */ 3397 switch (chandef->width) { 3398 case NL80211_CHAN_WIDTH_80: 3399 *pos++ = IEEE80211_VHT_CHANWIDTH_80MHZ; 3400 break; 3401 case NL80211_CHAN_WIDTH_160: 3402 *pos++ = IEEE80211_VHT_CHANWIDTH_160MHZ; 3403 break; 3404 case NL80211_CHAN_WIDTH_80P80: 3405 *pos++ = IEEE80211_VHT_CHANWIDTH_80P80MHZ; 3406 break; 3407 case NL80211_CHAN_WIDTH_320: 3408 /* The behavior is not defined for 320 MHz channels */ 3409 WARN_ON(1); 3410 fallthrough; 3411 default: 3412 *pos++ = IEEE80211_VHT_CHANWIDTH_USE_HT; 3413 } 3414 3415 /* new center frequency segment 0 */ 3416 *pos++ = ieee80211_frequency_to_channel(chandef->center_freq1); 3417 /* new center frequency segment 1 */ 3418 if (chandef->center_freq2) 3419 *pos++ = ieee80211_frequency_to_channel(chandef->center_freq2); 3420 else 3421 *pos++ = 0; 3422 } 3423 3424 u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap, 3425 const struct cfg80211_chan_def *chandef) 3426 { 3427 struct ieee80211_vht_operation *vht_oper; 3428 3429 *pos++ = WLAN_EID_VHT_OPERATION; 3430 *pos++ = sizeof(struct ieee80211_vht_operation); 3431 vht_oper = (struct ieee80211_vht_operation *)pos; 3432 vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel( 3433 chandef->center_freq1); 3434 if (chandef->center_freq2) 3435 vht_oper->center_freq_seg1_idx = 3436 ieee80211_frequency_to_channel(chandef->center_freq2); 3437 else 3438 vht_oper->center_freq_seg1_idx = 0x00; 3439 3440 switch (chandef->width) { 3441 case NL80211_CHAN_WIDTH_160: 3442 /* 3443 * Convert 160 MHz channel width to new style as interop 3444 * workaround. 3445 */ 3446 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ; 3447 vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx; 3448 if (chandef->chan->center_freq < chandef->center_freq1) 3449 vht_oper->center_freq_seg0_idx -= 8; 3450 else 3451 vht_oper->center_freq_seg0_idx += 8; 3452 break; 3453 case NL80211_CHAN_WIDTH_80P80: 3454 /* 3455 * Convert 80+80 MHz channel width to new style as interop 3456 * workaround. 3457 */ 3458 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ; 3459 break; 3460 case NL80211_CHAN_WIDTH_80: 3461 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ; 3462 break; 3463 case NL80211_CHAN_WIDTH_320: 3464 /* VHT information element should not be included on 6GHz */ 3465 WARN_ON(1); 3466 return pos; 3467 default: 3468 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT; 3469 break; 3470 } 3471 3472 /* don't require special VHT peer rates */ 3473 vht_oper->basic_mcs_set = cpu_to_le16(0xffff); 3474 3475 return pos + sizeof(struct ieee80211_vht_operation); 3476 } 3477 3478 u8 *ieee80211_ie_build_he_oper(u8 *pos, struct cfg80211_chan_def *chandef) 3479 { 3480 struct ieee80211_he_operation *he_oper; 3481 struct ieee80211_he_6ghz_oper *he_6ghz_op; 3482 u32 he_oper_params; 3483 u8 ie_len = 1 + sizeof(struct ieee80211_he_operation); 3484 3485 if (chandef->chan->band == NL80211_BAND_6GHZ) 3486 ie_len += sizeof(struct ieee80211_he_6ghz_oper); 3487 3488 *pos++ = WLAN_EID_EXTENSION; 3489 *pos++ = ie_len; 3490 *pos++ = WLAN_EID_EXT_HE_OPERATION; 3491 3492 he_oper_params = 0; 3493 he_oper_params |= u32_encode_bits(1023, /* disabled */ 3494 IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK); 3495 he_oper_params |= u32_encode_bits(1, 3496 IEEE80211_HE_OPERATION_ER_SU_DISABLE); 3497 he_oper_params |= u32_encode_bits(1, 3498 IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED); 3499 if (chandef->chan->band == NL80211_BAND_6GHZ) 3500 he_oper_params |= u32_encode_bits(1, 3501 IEEE80211_HE_OPERATION_6GHZ_OP_INFO); 3502 3503 he_oper = (struct ieee80211_he_operation *)pos; 3504 he_oper->he_oper_params = cpu_to_le32(he_oper_params); 3505 3506 /* don't require special HE peer rates */ 3507 he_oper->he_mcs_nss_set = cpu_to_le16(0xffff); 3508 pos += sizeof(struct ieee80211_he_operation); 3509 3510 if (chandef->chan->band != NL80211_BAND_6GHZ) 3511 goto out; 3512 3513 /* TODO add VHT operational */ 3514 he_6ghz_op = (struct ieee80211_he_6ghz_oper *)pos; 3515 he_6ghz_op->minrate = 6; /* 6 Mbps */ 3516 he_6ghz_op->primary = 3517 ieee80211_frequency_to_channel(chandef->chan->center_freq); 3518 he_6ghz_op->ccfs0 = 3519 ieee80211_frequency_to_channel(chandef->center_freq1); 3520 if (chandef->center_freq2) 3521 he_6ghz_op->ccfs1 = 3522 ieee80211_frequency_to_channel(chandef->center_freq2); 3523 else 3524 he_6ghz_op->ccfs1 = 0; 3525 3526 switch (chandef->width) { 3527 case NL80211_CHAN_WIDTH_320: 3528 /* 3529 * TODO: mesh operation is not defined over 6GHz 320 MHz 3530 * channels. 3531 */ 3532 WARN_ON(1); 3533 break; 3534 case NL80211_CHAN_WIDTH_160: 3535 /* Convert 160 MHz channel width to new style as interop 3536 * workaround. 3537 */ 3538 he_6ghz_op->control = 3539 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ; 3540 he_6ghz_op->ccfs1 = he_6ghz_op->ccfs0; 3541 if (chandef->chan->center_freq < chandef->center_freq1) 3542 he_6ghz_op->ccfs0 -= 8; 3543 else 3544 he_6ghz_op->ccfs0 += 8; 3545 fallthrough; 3546 case NL80211_CHAN_WIDTH_80P80: 3547 he_6ghz_op->control = 3548 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ; 3549 break; 3550 case NL80211_CHAN_WIDTH_80: 3551 he_6ghz_op->control = 3552 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ; 3553 break; 3554 case NL80211_CHAN_WIDTH_40: 3555 he_6ghz_op->control = 3556 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ; 3557 break; 3558 default: 3559 he_6ghz_op->control = 3560 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ; 3561 break; 3562 } 3563 3564 pos += sizeof(struct ieee80211_he_6ghz_oper); 3565 3566 out: 3567 return pos; 3568 } 3569 3570 u8 *ieee80211_ie_build_eht_oper(u8 *pos, struct cfg80211_chan_def *chandef, 3571 const struct ieee80211_sta_eht_cap *eht_cap) 3572 3573 { 3574 const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss = 3575 &eht_cap->eht_mcs_nss_supp.only_20mhz; 3576 struct ieee80211_eht_operation *eht_oper; 3577 struct ieee80211_eht_operation_info *eht_oper_info; 3578 u8 eht_oper_len = offsetof(struct ieee80211_eht_operation, optional); 3579 u8 eht_oper_info_len = 3580 offsetof(struct ieee80211_eht_operation_info, optional); 3581 u8 chan_width = 0; 3582 3583 *pos++ = WLAN_EID_EXTENSION; 3584 *pos++ = 1 + eht_oper_len + eht_oper_info_len; 3585 *pos++ = WLAN_EID_EXT_EHT_OPERATION; 3586 3587 eht_oper = (struct ieee80211_eht_operation *)pos; 3588 3589 memcpy(&eht_oper->basic_mcs_nss, eht_mcs_nss, sizeof(*eht_mcs_nss)); 3590 eht_oper->params |= IEEE80211_EHT_OPER_INFO_PRESENT; 3591 pos += eht_oper_len; 3592 3593 eht_oper_info = 3594 (struct ieee80211_eht_operation_info *)eht_oper->optional; 3595 3596 eht_oper_info->ccfs0 = 3597 ieee80211_frequency_to_channel(chandef->center_freq1); 3598 if (chandef->center_freq2) 3599 eht_oper_info->ccfs1 = 3600 ieee80211_frequency_to_channel(chandef->center_freq2); 3601 else 3602 eht_oper_info->ccfs1 = 0; 3603 3604 switch (chandef->width) { 3605 case NL80211_CHAN_WIDTH_320: 3606 chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_320MHZ; 3607 eht_oper_info->ccfs1 = eht_oper_info->ccfs0; 3608 if (chandef->chan->center_freq < chandef->center_freq1) 3609 eht_oper_info->ccfs0 -= 16; 3610 else 3611 eht_oper_info->ccfs0 += 16; 3612 break; 3613 case NL80211_CHAN_WIDTH_160: 3614 eht_oper_info->ccfs1 = eht_oper_info->ccfs0; 3615 if (chandef->chan->center_freq < chandef->center_freq1) 3616 eht_oper_info->ccfs0 -= 8; 3617 else 3618 eht_oper_info->ccfs0 += 8; 3619 fallthrough; 3620 case NL80211_CHAN_WIDTH_80P80: 3621 chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_160MHZ; 3622 break; 3623 case NL80211_CHAN_WIDTH_80: 3624 chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_80MHZ; 3625 break; 3626 case NL80211_CHAN_WIDTH_40: 3627 chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_40MHZ; 3628 break; 3629 default: 3630 chan_width = IEEE80211_EHT_OPER_CHAN_WIDTH_20MHZ; 3631 break; 3632 } 3633 eht_oper_info->control = chan_width; 3634 pos += eht_oper_info_len; 3635 3636 /* TODO: eht_oper_info->optional */ 3637 3638 return pos; 3639 } 3640 3641 bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper, 3642 struct cfg80211_chan_def *chandef) 3643 { 3644 enum nl80211_channel_type channel_type; 3645 3646 if (!ht_oper) 3647 return false; 3648 3649 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) { 3650 case IEEE80211_HT_PARAM_CHA_SEC_NONE: 3651 channel_type = NL80211_CHAN_HT20; 3652 break; 3653 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE: 3654 channel_type = NL80211_CHAN_HT40PLUS; 3655 break; 3656 case IEEE80211_HT_PARAM_CHA_SEC_BELOW: 3657 channel_type = NL80211_CHAN_HT40MINUS; 3658 break; 3659 default: 3660 return false; 3661 } 3662 3663 cfg80211_chandef_create(chandef, chandef->chan, channel_type); 3664 return true; 3665 } 3666 3667 bool ieee80211_chandef_vht_oper(struct ieee80211_hw *hw, u32 vht_cap_info, 3668 const struct ieee80211_vht_operation *oper, 3669 const struct ieee80211_ht_operation *htop, 3670 struct cfg80211_chan_def *chandef) 3671 { 3672 struct cfg80211_chan_def new = *chandef; 3673 int cf0, cf1; 3674 int ccfs0, ccfs1, ccfs2; 3675 int ccf0, ccf1; 3676 u32 vht_cap; 3677 bool support_80_80 = false; 3678 bool support_160 = false; 3679 u8 ext_nss_bw_supp = u32_get_bits(vht_cap_info, 3680 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK); 3681 u8 supp_chwidth = u32_get_bits(vht_cap_info, 3682 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK); 3683 3684 if (!oper || !htop) 3685 return false; 3686 3687 vht_cap = hw->wiphy->bands[chandef->chan->band]->vht_cap.cap; 3688 support_160 = (vht_cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK | 3689 IEEE80211_VHT_CAP_EXT_NSS_BW_MASK)); 3690 support_80_80 = ((vht_cap & 3691 IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) || 3692 (vht_cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ && 3693 vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) || 3694 ((vht_cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) >> 3695 IEEE80211_VHT_CAP_EXT_NSS_BW_SHIFT > 1)); 3696 ccfs0 = oper->center_freq_seg0_idx; 3697 ccfs1 = oper->center_freq_seg1_idx; 3698 ccfs2 = (le16_to_cpu(htop->operation_mode) & 3699 IEEE80211_HT_OP_MODE_CCFS2_MASK) 3700 >> IEEE80211_HT_OP_MODE_CCFS2_SHIFT; 3701 3702 ccf0 = ccfs0; 3703 3704 /* if not supported, parse as though we didn't understand it */ 3705 if (!ieee80211_hw_check(hw, SUPPORTS_VHT_EXT_NSS_BW)) 3706 ext_nss_bw_supp = 0; 3707 3708 /* 3709 * Cf. IEEE 802.11 Table 9-250 3710 * 3711 * We really just consider that because it's inefficient to connect 3712 * at a higher bandwidth than we'll actually be able to use. 3713 */ 3714 switch ((supp_chwidth << 4) | ext_nss_bw_supp) { 3715 default: 3716 case 0x00: 3717 ccf1 = 0; 3718 support_160 = false; 3719 support_80_80 = false; 3720 break; 3721 case 0x01: 3722 support_80_80 = false; 3723 fallthrough; 3724 case 0x02: 3725 case 0x03: 3726 ccf1 = ccfs2; 3727 break; 3728 case 0x10: 3729 ccf1 = ccfs1; 3730 break; 3731 case 0x11: 3732 case 0x12: 3733 if (!ccfs1) 3734 ccf1 = ccfs2; 3735 else 3736 ccf1 = ccfs1; 3737 break; 3738 case 0x13: 3739 case 0x20: 3740 case 0x23: 3741 ccf1 = ccfs1; 3742 break; 3743 } 3744 3745 cf0 = ieee80211_channel_to_frequency(ccf0, chandef->chan->band); 3746 cf1 = ieee80211_channel_to_frequency(ccf1, chandef->chan->band); 3747 3748 switch (oper->chan_width) { 3749 case IEEE80211_VHT_CHANWIDTH_USE_HT: 3750 /* just use HT information directly */ 3751 break; 3752 case IEEE80211_VHT_CHANWIDTH_80MHZ: 3753 new.width = NL80211_CHAN_WIDTH_80; 3754 new.center_freq1 = cf0; 3755 /* If needed, adjust based on the newer interop workaround. */ 3756 if (ccf1) { 3757 unsigned int diff; 3758 3759 diff = abs(ccf1 - ccf0); 3760 if ((diff == 8) && support_160) { 3761 new.width = NL80211_CHAN_WIDTH_160; 3762 new.center_freq1 = cf1; 3763 } else if ((diff > 8) && support_80_80) { 3764 new.width = NL80211_CHAN_WIDTH_80P80; 3765 new.center_freq2 = cf1; 3766 } 3767 } 3768 break; 3769 case IEEE80211_VHT_CHANWIDTH_160MHZ: 3770 /* deprecated encoding */ 3771 new.width = NL80211_CHAN_WIDTH_160; 3772 new.center_freq1 = cf0; 3773 break; 3774 case IEEE80211_VHT_CHANWIDTH_80P80MHZ: 3775 /* deprecated encoding */ 3776 new.width = NL80211_CHAN_WIDTH_80P80; 3777 new.center_freq1 = cf0; 3778 new.center_freq2 = cf1; 3779 break; 3780 default: 3781 return false; 3782 } 3783 3784 if (!cfg80211_chandef_valid(&new)) 3785 return false; 3786 3787 *chandef = new; 3788 return true; 3789 } 3790 3791 void ieee80211_chandef_eht_oper(const struct ieee80211_eht_operation *eht_oper, 3792 bool support_160, bool support_320, 3793 struct cfg80211_chan_def *chandef) 3794 { 3795 struct ieee80211_eht_operation_info *info = (void *)eht_oper->optional; 3796 3797 chandef->center_freq1 = 3798 ieee80211_channel_to_frequency(info->ccfs0, 3799 chandef->chan->band); 3800 3801 switch (u8_get_bits(info->control, 3802 IEEE80211_EHT_OPER_CHAN_WIDTH)) { 3803 case IEEE80211_EHT_OPER_CHAN_WIDTH_20MHZ: 3804 chandef->width = NL80211_CHAN_WIDTH_20; 3805 break; 3806 case IEEE80211_EHT_OPER_CHAN_WIDTH_40MHZ: 3807 chandef->width = NL80211_CHAN_WIDTH_40; 3808 break; 3809 case IEEE80211_EHT_OPER_CHAN_WIDTH_80MHZ: 3810 chandef->width = NL80211_CHAN_WIDTH_80; 3811 break; 3812 case IEEE80211_EHT_OPER_CHAN_WIDTH_160MHZ: 3813 if (support_160) { 3814 chandef->width = NL80211_CHAN_WIDTH_160; 3815 chandef->center_freq1 = 3816 ieee80211_channel_to_frequency(info->ccfs1, 3817 chandef->chan->band); 3818 } else { 3819 chandef->width = NL80211_CHAN_WIDTH_80; 3820 } 3821 break; 3822 case IEEE80211_EHT_OPER_CHAN_WIDTH_320MHZ: 3823 if (support_320) { 3824 chandef->width = NL80211_CHAN_WIDTH_320; 3825 chandef->center_freq1 = 3826 ieee80211_channel_to_frequency(info->ccfs1, 3827 chandef->chan->band); 3828 } else if (support_160) { 3829 chandef->width = NL80211_CHAN_WIDTH_160; 3830 } else { 3831 chandef->width = NL80211_CHAN_WIDTH_80; 3832 3833 if (chandef->center_freq1 > chandef->chan->center_freq) 3834 chandef->center_freq1 -= 40; 3835 else 3836 chandef->center_freq1 += 40; 3837 } 3838 break; 3839 } 3840 } 3841 3842 bool ieee80211_chandef_he_6ghz_oper(struct ieee80211_sub_if_data *sdata, 3843 const struct ieee80211_he_operation *he_oper, 3844 const struct ieee80211_eht_operation *eht_oper, 3845 struct cfg80211_chan_def *chandef) 3846 { 3847 struct ieee80211_local *local = sdata->local; 3848 struct ieee80211_supported_band *sband; 3849 enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif); 3850 const struct ieee80211_sta_he_cap *he_cap; 3851 const struct ieee80211_sta_eht_cap *eht_cap; 3852 struct cfg80211_chan_def he_chandef = *chandef; 3853 const struct ieee80211_he_6ghz_oper *he_6ghz_oper; 3854 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf; 3855 bool support_80_80, support_160, support_320; 3856 u8 he_phy_cap, eht_phy_cap; 3857 u32 freq; 3858 3859 if (chandef->chan->band != NL80211_BAND_6GHZ) 3860 return true; 3861 3862 sband = local->hw.wiphy->bands[NL80211_BAND_6GHZ]; 3863 3864 he_cap = ieee80211_get_he_iftype_cap(sband, iftype); 3865 if (!he_cap) { 3866 sdata_info(sdata, "Missing iftype sband data/HE cap"); 3867 return false; 3868 } 3869 3870 he_phy_cap = he_cap->he_cap_elem.phy_cap_info[0]; 3871 support_160 = 3872 he_phy_cap & 3873 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G; 3874 support_80_80 = 3875 he_phy_cap & 3876 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G; 3877 3878 if (!he_oper) { 3879 sdata_info(sdata, 3880 "HE is not advertised on (on %d MHz), expect issues\n", 3881 chandef->chan->center_freq); 3882 return false; 3883 } 3884 3885 eht_cap = ieee80211_get_eht_iftype_cap(sband, iftype); 3886 if (!eht_cap) { 3887 sdata_info(sdata, "Missing iftype sband data/EHT cap"); 3888 eht_oper = NULL; 3889 } 3890 3891 he_6ghz_oper = ieee80211_he_6ghz_oper(he_oper); 3892 3893 if (!he_6ghz_oper) { 3894 sdata_info(sdata, 3895 "HE 6GHz operation missing (on %d MHz), expect issues\n", 3896 chandef->chan->center_freq); 3897 return false; 3898 } 3899 3900 /* 3901 * The EHT operation IE does not contain the primary channel so the 3902 * primary channel frequency should be taken from the 6 GHz operation 3903 * information. 3904 */ 3905 freq = ieee80211_channel_to_frequency(he_6ghz_oper->primary, 3906 NL80211_BAND_6GHZ); 3907 he_chandef.chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq); 3908 3909 switch (u8_get_bits(he_6ghz_oper->control, 3910 IEEE80211_HE_6GHZ_OPER_CTRL_REG_INFO)) { 3911 case IEEE80211_6GHZ_CTRL_REG_LPI_AP: 3912 bss_conf->power_type = IEEE80211_REG_LPI_AP; 3913 break; 3914 case IEEE80211_6GHZ_CTRL_REG_SP_AP: 3915 bss_conf->power_type = IEEE80211_REG_SP_AP; 3916 break; 3917 default: 3918 bss_conf->power_type = IEEE80211_REG_UNSET_AP; 3919 break; 3920 } 3921 3922 if (!eht_oper || 3923 !(eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT)) { 3924 switch (u8_get_bits(he_6ghz_oper->control, 3925 IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH)) { 3926 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ: 3927 he_chandef.width = NL80211_CHAN_WIDTH_20; 3928 break; 3929 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ: 3930 he_chandef.width = NL80211_CHAN_WIDTH_40; 3931 break; 3932 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ: 3933 he_chandef.width = NL80211_CHAN_WIDTH_80; 3934 break; 3935 case IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ: 3936 he_chandef.width = NL80211_CHAN_WIDTH_80; 3937 if (!he_6ghz_oper->ccfs1) 3938 break; 3939 if (abs(he_6ghz_oper->ccfs1 - he_6ghz_oper->ccfs0) == 8) { 3940 if (support_160) 3941 he_chandef.width = NL80211_CHAN_WIDTH_160; 3942 } else { 3943 if (support_80_80) 3944 he_chandef.width = NL80211_CHAN_WIDTH_80P80; 3945 } 3946 break; 3947 } 3948 3949 if (he_chandef.width == NL80211_CHAN_WIDTH_160) { 3950 he_chandef.center_freq1 = 3951 ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1, 3952 NL80211_BAND_6GHZ); 3953 } else { 3954 he_chandef.center_freq1 = 3955 ieee80211_channel_to_frequency(he_6ghz_oper->ccfs0, 3956 NL80211_BAND_6GHZ); 3957 if (support_80_80 || support_160) 3958 he_chandef.center_freq2 = 3959 ieee80211_channel_to_frequency(he_6ghz_oper->ccfs1, 3960 NL80211_BAND_6GHZ); 3961 } 3962 } else { 3963 eht_phy_cap = eht_cap->eht_cap_elem.phy_cap_info[0]; 3964 support_320 = 3965 eht_phy_cap & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ; 3966 3967 ieee80211_chandef_eht_oper(eht_oper, support_160, 3968 support_320, &he_chandef); 3969 } 3970 3971 if (!cfg80211_chandef_valid(&he_chandef)) { 3972 sdata_info(sdata, 3973 "HE 6GHz operation resulted in invalid chandef: %d MHz/%d/%d MHz/%d MHz\n", 3974 he_chandef.chan ? he_chandef.chan->center_freq : 0, 3975 he_chandef.width, 3976 he_chandef.center_freq1, 3977 he_chandef.center_freq2); 3978 return false; 3979 } 3980 3981 *chandef = he_chandef; 3982 3983 return true; 3984 } 3985 3986 bool ieee80211_chandef_s1g_oper(const struct ieee80211_s1g_oper_ie *oper, 3987 struct cfg80211_chan_def *chandef) 3988 { 3989 u32 oper_freq; 3990 3991 if (!oper) 3992 return false; 3993 3994 switch (FIELD_GET(S1G_OPER_CH_WIDTH_OPER, oper->ch_width)) { 3995 case IEEE80211_S1G_CHANWIDTH_1MHZ: 3996 chandef->width = NL80211_CHAN_WIDTH_1; 3997 break; 3998 case IEEE80211_S1G_CHANWIDTH_2MHZ: 3999 chandef->width = NL80211_CHAN_WIDTH_2; 4000 break; 4001 case IEEE80211_S1G_CHANWIDTH_4MHZ: 4002 chandef->width = NL80211_CHAN_WIDTH_4; 4003 break; 4004 case IEEE80211_S1G_CHANWIDTH_8MHZ: 4005 chandef->width = NL80211_CHAN_WIDTH_8; 4006 break; 4007 case IEEE80211_S1G_CHANWIDTH_16MHZ: 4008 chandef->width = NL80211_CHAN_WIDTH_16; 4009 break; 4010 default: 4011 return false; 4012 } 4013 4014 oper_freq = ieee80211_channel_to_freq_khz(oper->oper_ch, 4015 NL80211_BAND_S1GHZ); 4016 chandef->center_freq1 = KHZ_TO_MHZ(oper_freq); 4017 chandef->freq1_offset = oper_freq % 1000; 4018 4019 return true; 4020 } 4021 4022 int ieee80211_parse_bitrates(enum nl80211_chan_width width, 4023 const struct ieee80211_supported_band *sband, 4024 const u8 *srates, int srates_len, u32 *rates) 4025 { 4026 u32 rate_flags = ieee80211_chanwidth_rate_flags(width); 4027 int shift = ieee80211_chanwidth_get_shift(width); 4028 struct ieee80211_rate *br; 4029 int brate, rate, i, j, count = 0; 4030 4031 *rates = 0; 4032 4033 for (i = 0; i < srates_len; i++) { 4034 rate = srates[i] & 0x7f; 4035 4036 for (j = 0; j < sband->n_bitrates; j++) { 4037 br = &sband->bitrates[j]; 4038 if ((rate_flags & br->flags) != rate_flags) 4039 continue; 4040 4041 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5); 4042 if (brate == rate) { 4043 *rates |= BIT(j); 4044 count++; 4045 break; 4046 } 4047 } 4048 } 4049 return count; 4050 } 4051 4052 int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata, 4053 struct sk_buff *skb, bool need_basic, 4054 enum nl80211_band band) 4055 { 4056 struct ieee80211_local *local = sdata->local; 4057 struct ieee80211_supported_band *sband; 4058 int rate, shift; 4059 u8 i, rates, *pos; 4060 u32 basic_rates = sdata->vif.bss_conf.basic_rates; 4061 u32 rate_flags; 4062 4063 shift = ieee80211_vif_get_shift(&sdata->vif); 4064 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef); 4065 sband = local->hw.wiphy->bands[band]; 4066 rates = 0; 4067 for (i = 0; i < sband->n_bitrates; i++) { 4068 if ((rate_flags & sband->bitrates[i].flags) != rate_flags) 4069 continue; 4070 rates++; 4071 } 4072 if (rates > 8) 4073 rates = 8; 4074 4075 if (skb_tailroom(skb) < rates + 2) 4076 return -ENOMEM; 4077 4078 pos = skb_put(skb, rates + 2); 4079 *pos++ = WLAN_EID_SUPP_RATES; 4080 *pos++ = rates; 4081 for (i = 0; i < rates; i++) { 4082 u8 basic = 0; 4083 if ((rate_flags & sband->bitrates[i].flags) != rate_flags) 4084 continue; 4085 4086 if (need_basic && basic_rates & BIT(i)) 4087 basic = 0x80; 4088 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate, 4089 5 * (1 << shift)); 4090 *pos++ = basic | (u8) rate; 4091 } 4092 4093 return 0; 4094 } 4095 4096 int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata, 4097 struct sk_buff *skb, bool need_basic, 4098 enum nl80211_band band) 4099 { 4100 struct ieee80211_local *local = sdata->local; 4101 struct ieee80211_supported_band *sband; 4102 int rate, shift; 4103 u8 i, exrates, *pos; 4104 u32 basic_rates = sdata->vif.bss_conf.basic_rates; 4105 u32 rate_flags; 4106 4107 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef); 4108 shift = ieee80211_vif_get_shift(&sdata->vif); 4109 4110 sband = local->hw.wiphy->bands[band]; 4111 exrates = 0; 4112 for (i = 0; i < sband->n_bitrates; i++) { 4113 if ((rate_flags & sband->bitrates[i].flags) != rate_flags) 4114 continue; 4115 exrates++; 4116 } 4117 4118 if (exrates > 8) 4119 exrates -= 8; 4120 else 4121 exrates = 0; 4122 4123 if (skb_tailroom(skb) < exrates + 2) 4124 return -ENOMEM; 4125 4126 if (exrates) { 4127 pos = skb_put(skb, exrates + 2); 4128 *pos++ = WLAN_EID_EXT_SUPP_RATES; 4129 *pos++ = exrates; 4130 for (i = 8; i < sband->n_bitrates; i++) { 4131 u8 basic = 0; 4132 if ((rate_flags & sband->bitrates[i].flags) 4133 != rate_flags) 4134 continue; 4135 if (need_basic && basic_rates & BIT(i)) 4136 basic = 0x80; 4137 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate, 4138 5 * (1 << shift)); 4139 *pos++ = basic | (u8) rate; 4140 } 4141 } 4142 return 0; 4143 } 4144 4145 int ieee80211_ave_rssi(struct ieee80211_vif *vif) 4146 { 4147 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 4148 4149 if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) 4150 return 0; 4151 4152 return -ewma_beacon_signal_read(&sdata->deflink.u.mgd.ave_beacon_signal); 4153 } 4154 EXPORT_SYMBOL_GPL(ieee80211_ave_rssi); 4155 4156 u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs) 4157 { 4158 if (!mcs) 4159 return 1; 4160 4161 /* TODO: consider rx_highest */ 4162 4163 if (mcs->rx_mask[3]) 4164 return 4; 4165 if (mcs->rx_mask[2]) 4166 return 3; 4167 if (mcs->rx_mask[1]) 4168 return 2; 4169 return 1; 4170 } 4171 4172 /** 4173 * ieee80211_calculate_rx_timestamp - calculate timestamp in frame 4174 * @local: mac80211 hw info struct 4175 * @status: RX status 4176 * @mpdu_len: total MPDU length (including FCS) 4177 * @mpdu_offset: offset into MPDU to calculate timestamp at 4178 * 4179 * This function calculates the RX timestamp at the given MPDU offset, taking 4180 * into account what the RX timestamp was. An offset of 0 will just normalize 4181 * the timestamp to TSF at beginning of MPDU reception. 4182 */ 4183 u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local, 4184 struct ieee80211_rx_status *status, 4185 unsigned int mpdu_len, 4186 unsigned int mpdu_offset) 4187 { 4188 u64 ts = status->mactime; 4189 struct rate_info ri; 4190 u16 rate; 4191 u8 n_ltf; 4192 4193 if (WARN_ON(!ieee80211_have_rx_timestamp(status))) 4194 return 0; 4195 4196 memset(&ri, 0, sizeof(ri)); 4197 4198 ri.bw = status->bw; 4199 4200 /* Fill cfg80211 rate info */ 4201 switch (status->encoding) { 4202 case RX_ENC_EHT: 4203 ri.flags |= RATE_INFO_FLAGS_EHT_MCS; 4204 ri.mcs = status->rate_idx; 4205 ri.nss = status->nss; 4206 ri.eht_ru_alloc = status->eht.ru; 4207 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI) 4208 ri.flags |= RATE_INFO_FLAGS_SHORT_GI; 4209 /* TODO/FIXME: is this right? handle other PPDUs */ 4210 if (status->flag & RX_FLAG_MACTIME_PLCP_START) { 4211 mpdu_offset += 2; 4212 ts += 36; 4213 } 4214 break; 4215 case RX_ENC_HE: 4216 ri.flags |= RATE_INFO_FLAGS_HE_MCS; 4217 ri.mcs = status->rate_idx; 4218 ri.nss = status->nss; 4219 ri.he_ru_alloc = status->he_ru; 4220 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI) 4221 ri.flags |= RATE_INFO_FLAGS_SHORT_GI; 4222 4223 /* 4224 * See P802.11ax_D6.0, section 27.3.4 for 4225 * VHT PPDU format. 4226 */ 4227 if (status->flag & RX_FLAG_MACTIME_PLCP_START) { 4228 mpdu_offset += 2; 4229 ts += 36; 4230 4231 /* 4232 * TODO: 4233 * For HE MU PPDU, add the HE-SIG-B. 4234 * For HE ER PPDU, add 8us for the HE-SIG-A. 4235 * For HE TB PPDU, add 4us for the HE-STF. 4236 * Add the HE-LTF durations - variable. 4237 */ 4238 } 4239 4240 break; 4241 case RX_ENC_HT: 4242 ri.mcs = status->rate_idx; 4243 ri.flags |= RATE_INFO_FLAGS_MCS; 4244 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI) 4245 ri.flags |= RATE_INFO_FLAGS_SHORT_GI; 4246 4247 /* 4248 * See P802.11REVmd_D3.0, section 19.3.2 for 4249 * HT PPDU format. 4250 */ 4251 if (status->flag & RX_FLAG_MACTIME_PLCP_START) { 4252 mpdu_offset += 2; 4253 if (status->enc_flags & RX_ENC_FLAG_HT_GF) 4254 ts += 24; 4255 else 4256 ts += 32; 4257 4258 /* 4259 * Add Data HT-LTFs per streams 4260 * TODO: add Extension HT-LTFs, 4us per LTF 4261 */ 4262 n_ltf = ((ri.mcs >> 3) & 3) + 1; 4263 n_ltf = n_ltf == 3 ? 4 : n_ltf; 4264 ts += n_ltf * 4; 4265 } 4266 4267 break; 4268 case RX_ENC_VHT: 4269 ri.flags |= RATE_INFO_FLAGS_VHT_MCS; 4270 ri.mcs = status->rate_idx; 4271 ri.nss = status->nss; 4272 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI) 4273 ri.flags |= RATE_INFO_FLAGS_SHORT_GI; 4274 4275 /* 4276 * See P802.11REVmd_D3.0, section 21.3.2 for 4277 * VHT PPDU format. 4278 */ 4279 if (status->flag & RX_FLAG_MACTIME_PLCP_START) { 4280 mpdu_offset += 2; 4281 ts += 36; 4282 4283 /* 4284 * Add VHT-LTFs per streams 4285 */ 4286 n_ltf = (ri.nss != 1) && (ri.nss % 2) ? 4287 ri.nss + 1 : ri.nss; 4288 ts += 4 * n_ltf; 4289 } 4290 4291 break; 4292 default: 4293 WARN_ON(1); 4294 fallthrough; 4295 case RX_ENC_LEGACY: { 4296 struct ieee80211_supported_band *sband; 4297 int shift = 0; 4298 int bitrate; 4299 4300 switch (status->bw) { 4301 case RATE_INFO_BW_10: 4302 shift = 1; 4303 break; 4304 case RATE_INFO_BW_5: 4305 shift = 2; 4306 break; 4307 } 4308 4309 sband = local->hw.wiphy->bands[status->band]; 4310 bitrate = sband->bitrates[status->rate_idx].bitrate; 4311 ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift)); 4312 4313 if (status->flag & RX_FLAG_MACTIME_PLCP_START) { 4314 if (status->band == NL80211_BAND_5GHZ) { 4315 ts += 20 << shift; 4316 mpdu_offset += 2; 4317 } else if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) { 4318 ts += 96; 4319 } else { 4320 ts += 192; 4321 } 4322 } 4323 break; 4324 } 4325 } 4326 4327 rate = cfg80211_calculate_bitrate(&ri); 4328 if (WARN_ONCE(!rate, 4329 "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n", 4330 (unsigned long long)status->flag, status->rate_idx, 4331 status->nss)) 4332 return 0; 4333 4334 /* rewind from end of MPDU */ 4335 if (status->flag & RX_FLAG_MACTIME_END) 4336 ts -= mpdu_len * 8 * 10 / rate; 4337 4338 ts += mpdu_offset * 8 * 10 / rate; 4339 4340 return ts; 4341 } 4342 4343 void ieee80211_dfs_cac_cancel(struct ieee80211_local *local) 4344 { 4345 struct ieee80211_sub_if_data *sdata; 4346 struct cfg80211_chan_def chandef; 4347 4348 /* for interface list, to avoid linking iflist_mtx and chanctx_mtx */ 4349 lockdep_assert_wiphy(local->hw.wiphy); 4350 4351 mutex_lock(&local->mtx); 4352 list_for_each_entry(sdata, &local->interfaces, list) { 4353 /* it might be waiting for the local->mtx, but then 4354 * by the time it gets it, sdata->wdev.cac_started 4355 * will no longer be true 4356 */ 4357 cancel_delayed_work(&sdata->deflink.dfs_cac_timer_work); 4358 4359 if (sdata->wdev.cac_started) { 4360 chandef = sdata->vif.bss_conf.chandef; 4361 ieee80211_link_release_channel(&sdata->deflink); 4362 cfg80211_cac_event(sdata->dev, 4363 &chandef, 4364 NL80211_RADAR_CAC_ABORTED, 4365 GFP_KERNEL); 4366 } 4367 } 4368 mutex_unlock(&local->mtx); 4369 } 4370 4371 void ieee80211_dfs_radar_detected_work(struct work_struct *work) 4372 { 4373 struct ieee80211_local *local = 4374 container_of(work, struct ieee80211_local, radar_detected_work); 4375 struct cfg80211_chan_def chandef = local->hw.conf.chandef; 4376 struct ieee80211_chanctx *ctx; 4377 int num_chanctx = 0; 4378 4379 mutex_lock(&local->chanctx_mtx); 4380 list_for_each_entry(ctx, &local->chanctx_list, list) { 4381 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) 4382 continue; 4383 4384 num_chanctx++; 4385 chandef = ctx->conf.def; 4386 } 4387 mutex_unlock(&local->chanctx_mtx); 4388 4389 wiphy_lock(local->hw.wiphy); 4390 ieee80211_dfs_cac_cancel(local); 4391 wiphy_unlock(local->hw.wiphy); 4392 4393 if (num_chanctx > 1) 4394 /* XXX: multi-channel is not supported yet */ 4395 WARN_ON(1); 4396 else 4397 cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL); 4398 } 4399 4400 void ieee80211_radar_detected(struct ieee80211_hw *hw) 4401 { 4402 struct ieee80211_local *local = hw_to_local(hw); 4403 4404 trace_api_radar_detected(local); 4405 4406 schedule_work(&local->radar_detected_work); 4407 } 4408 EXPORT_SYMBOL(ieee80211_radar_detected); 4409 4410 ieee80211_conn_flags_t ieee80211_chandef_downgrade(struct cfg80211_chan_def *c) 4411 { 4412 ieee80211_conn_flags_t ret; 4413 int tmp; 4414 4415 switch (c->width) { 4416 case NL80211_CHAN_WIDTH_20: 4417 c->width = NL80211_CHAN_WIDTH_20_NOHT; 4418 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT; 4419 break; 4420 case NL80211_CHAN_WIDTH_40: 4421 c->width = NL80211_CHAN_WIDTH_20; 4422 c->center_freq1 = c->chan->center_freq; 4423 ret = IEEE80211_CONN_DISABLE_40MHZ | 4424 IEEE80211_CONN_DISABLE_VHT; 4425 break; 4426 case NL80211_CHAN_WIDTH_80: 4427 tmp = (30 + c->chan->center_freq - c->center_freq1)/20; 4428 /* n_P40 */ 4429 tmp /= 2; 4430 /* freq_P40 */ 4431 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp; 4432 c->width = NL80211_CHAN_WIDTH_40; 4433 ret = IEEE80211_CONN_DISABLE_VHT; 4434 break; 4435 case NL80211_CHAN_WIDTH_80P80: 4436 c->center_freq2 = 0; 4437 c->width = NL80211_CHAN_WIDTH_80; 4438 ret = IEEE80211_CONN_DISABLE_80P80MHZ | 4439 IEEE80211_CONN_DISABLE_160MHZ; 4440 break; 4441 case NL80211_CHAN_WIDTH_160: 4442 /* n_P20 */ 4443 tmp = (70 + c->chan->center_freq - c->center_freq1)/20; 4444 /* n_P80 */ 4445 tmp /= 4; 4446 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp; 4447 c->width = NL80211_CHAN_WIDTH_80; 4448 ret = IEEE80211_CONN_DISABLE_80P80MHZ | 4449 IEEE80211_CONN_DISABLE_160MHZ; 4450 break; 4451 case NL80211_CHAN_WIDTH_320: 4452 /* n_P20 */ 4453 tmp = (150 + c->chan->center_freq - c->center_freq1) / 20; 4454 /* n_P160 */ 4455 tmp /= 8; 4456 c->center_freq1 = c->center_freq1 - 80 + 160 * tmp; 4457 c->width = NL80211_CHAN_WIDTH_160; 4458 ret = IEEE80211_CONN_DISABLE_320MHZ; 4459 break; 4460 default: 4461 case NL80211_CHAN_WIDTH_20_NOHT: 4462 WARN_ON_ONCE(1); 4463 c->width = NL80211_CHAN_WIDTH_20_NOHT; 4464 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT; 4465 break; 4466 case NL80211_CHAN_WIDTH_1: 4467 case NL80211_CHAN_WIDTH_2: 4468 case NL80211_CHAN_WIDTH_4: 4469 case NL80211_CHAN_WIDTH_8: 4470 case NL80211_CHAN_WIDTH_16: 4471 case NL80211_CHAN_WIDTH_5: 4472 case NL80211_CHAN_WIDTH_10: 4473 WARN_ON_ONCE(1); 4474 /* keep c->width */ 4475 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_VHT; 4476 break; 4477 } 4478 4479 WARN_ON_ONCE(!cfg80211_chandef_valid(c)); 4480 4481 return ret; 4482 } 4483 4484 /* 4485 * Returns true if smps_mode_new is strictly more restrictive than 4486 * smps_mode_old. 4487 */ 4488 bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old, 4489 enum ieee80211_smps_mode smps_mode_new) 4490 { 4491 if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC || 4492 smps_mode_new == IEEE80211_SMPS_AUTOMATIC)) 4493 return false; 4494 4495 switch (smps_mode_old) { 4496 case IEEE80211_SMPS_STATIC: 4497 return false; 4498 case IEEE80211_SMPS_DYNAMIC: 4499 return smps_mode_new == IEEE80211_SMPS_STATIC; 4500 case IEEE80211_SMPS_OFF: 4501 return smps_mode_new != IEEE80211_SMPS_OFF; 4502 default: 4503 WARN_ON(1); 4504 } 4505 4506 return false; 4507 } 4508 4509 int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata, 4510 struct cfg80211_csa_settings *csa_settings) 4511 { 4512 struct sk_buff *skb; 4513 struct ieee80211_mgmt *mgmt; 4514 struct ieee80211_local *local = sdata->local; 4515 int freq; 4516 int hdr_len = offsetofend(struct ieee80211_mgmt, 4517 u.action.u.chan_switch); 4518 u8 *pos; 4519 4520 if (sdata->vif.type != NL80211_IFTYPE_ADHOC && 4521 sdata->vif.type != NL80211_IFTYPE_MESH_POINT) 4522 return -EOPNOTSUPP; 4523 4524 skb = dev_alloc_skb(local->tx_headroom + hdr_len + 4525 5 + /* channel switch announcement element */ 4526 3 + /* secondary channel offset element */ 4527 5 + /* wide bandwidth channel switch announcement */ 4528 8); /* mesh channel switch parameters element */ 4529 if (!skb) 4530 return -ENOMEM; 4531 4532 skb_reserve(skb, local->tx_headroom); 4533 mgmt = skb_put_zero(skb, hdr_len); 4534 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | 4535 IEEE80211_STYPE_ACTION); 4536 4537 eth_broadcast_addr(mgmt->da); 4538 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN); 4539 if (ieee80211_vif_is_mesh(&sdata->vif)) { 4540 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN); 4541 } else { 4542 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; 4543 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN); 4544 } 4545 mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT; 4546 mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH; 4547 pos = skb_put(skb, 5); 4548 *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */ 4549 *pos++ = 3; /* IE length */ 4550 *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */ 4551 freq = csa_settings->chandef.chan->center_freq; 4552 *pos++ = ieee80211_frequency_to_channel(freq); /* channel */ 4553 *pos++ = csa_settings->count; /* count */ 4554 4555 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) { 4556 enum nl80211_channel_type ch_type; 4557 4558 skb_put(skb, 3); 4559 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */ 4560 *pos++ = 1; /* IE length */ 4561 ch_type = cfg80211_get_chandef_type(&csa_settings->chandef); 4562 if (ch_type == NL80211_CHAN_HT40PLUS) 4563 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE; 4564 else 4565 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW; 4566 } 4567 4568 if (ieee80211_vif_is_mesh(&sdata->vif)) { 4569 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; 4570 4571 skb_put(skb, 8); 4572 *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */ 4573 *pos++ = 6; /* IE length */ 4574 *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */ 4575 *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */ 4576 *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR; 4577 *pos++ |= csa_settings->block_tx ? 4578 WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00; 4579 put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */ 4580 pos += 2; 4581 put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */ 4582 pos += 2; 4583 } 4584 4585 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_80 || 4586 csa_settings->chandef.width == NL80211_CHAN_WIDTH_80P80 || 4587 csa_settings->chandef.width == NL80211_CHAN_WIDTH_160) { 4588 skb_put(skb, 5); 4589 ieee80211_ie_build_wide_bw_cs(pos, &csa_settings->chandef); 4590 } 4591 4592 ieee80211_tx_skb(sdata, skb); 4593 return 0; 4594 } 4595 4596 static bool 4597 ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i) 4598 { 4599 s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1); 4600 int skip; 4601 4602 if (end > 0) 4603 return false; 4604 4605 /* One shot NOA */ 4606 if (data->count[i] == 1) 4607 return false; 4608 4609 if (data->desc[i].interval == 0) 4610 return false; 4611 4612 /* End time is in the past, check for repetitions */ 4613 skip = DIV_ROUND_UP(-end, data->desc[i].interval); 4614 if (data->count[i] < 255) { 4615 if (data->count[i] <= skip) { 4616 data->count[i] = 0; 4617 return false; 4618 } 4619 4620 data->count[i] -= skip; 4621 } 4622 4623 data->desc[i].start += skip * data->desc[i].interval; 4624 4625 return true; 4626 } 4627 4628 static bool 4629 ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf, 4630 s32 *offset) 4631 { 4632 bool ret = false; 4633 int i; 4634 4635 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) { 4636 s32 cur; 4637 4638 if (!data->count[i]) 4639 continue; 4640 4641 if (ieee80211_extend_noa_desc(data, tsf + *offset, i)) 4642 ret = true; 4643 4644 cur = data->desc[i].start - tsf; 4645 if (cur > *offset) 4646 continue; 4647 4648 cur = data->desc[i].start + data->desc[i].duration - tsf; 4649 if (cur > *offset) 4650 *offset = cur; 4651 } 4652 4653 return ret; 4654 } 4655 4656 static u32 4657 ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf) 4658 { 4659 s32 offset = 0; 4660 int tries = 0; 4661 /* 4662 * arbitrary limit, used to avoid infinite loops when combined NoA 4663 * descriptors cover the full time period. 4664 */ 4665 int max_tries = 5; 4666 4667 ieee80211_extend_absent_time(data, tsf, &offset); 4668 do { 4669 if (!ieee80211_extend_absent_time(data, tsf, &offset)) 4670 break; 4671 4672 tries++; 4673 } while (tries < max_tries); 4674 4675 return offset; 4676 } 4677 4678 void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf) 4679 { 4680 u32 next_offset = BIT(31) - 1; 4681 int i; 4682 4683 data->absent = 0; 4684 data->has_next_tsf = false; 4685 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) { 4686 s32 start; 4687 4688 if (!data->count[i]) 4689 continue; 4690 4691 ieee80211_extend_noa_desc(data, tsf, i); 4692 start = data->desc[i].start - tsf; 4693 if (start <= 0) 4694 data->absent |= BIT(i); 4695 4696 if (next_offset > start) 4697 next_offset = start; 4698 4699 data->has_next_tsf = true; 4700 } 4701 4702 if (data->absent) 4703 next_offset = ieee80211_get_noa_absent_time(data, tsf); 4704 4705 data->next_tsf = tsf + next_offset; 4706 } 4707 EXPORT_SYMBOL(ieee80211_update_p2p_noa); 4708 4709 int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr, 4710 struct ieee80211_noa_data *data, u32 tsf) 4711 { 4712 int ret = 0; 4713 int i; 4714 4715 memset(data, 0, sizeof(*data)); 4716 4717 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) { 4718 const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i]; 4719 4720 if (!desc->count || !desc->duration) 4721 continue; 4722 4723 data->count[i] = desc->count; 4724 data->desc[i].start = le32_to_cpu(desc->start_time); 4725 data->desc[i].duration = le32_to_cpu(desc->duration); 4726 data->desc[i].interval = le32_to_cpu(desc->interval); 4727 4728 if (data->count[i] > 1 && 4729 data->desc[i].interval < data->desc[i].duration) 4730 continue; 4731 4732 ieee80211_extend_noa_desc(data, tsf, i); 4733 ret++; 4734 } 4735 4736 if (ret) 4737 ieee80211_update_p2p_noa(data, tsf); 4738 4739 return ret; 4740 } 4741 EXPORT_SYMBOL(ieee80211_parse_p2p_noa); 4742 4743 void ieee80211_recalc_dtim(struct ieee80211_local *local, 4744 struct ieee80211_sub_if_data *sdata) 4745 { 4746 u64 tsf = drv_get_tsf(local, sdata); 4747 u64 dtim_count = 0; 4748 u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024; 4749 u8 dtim_period = sdata->vif.bss_conf.dtim_period; 4750 struct ps_data *ps; 4751 u8 bcns_from_dtim; 4752 4753 if (tsf == -1ULL || !beacon_int || !dtim_period) 4754 return; 4755 4756 if (sdata->vif.type == NL80211_IFTYPE_AP || 4757 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 4758 if (!sdata->bss) 4759 return; 4760 4761 ps = &sdata->bss->ps; 4762 } else if (ieee80211_vif_is_mesh(&sdata->vif)) { 4763 ps = &sdata->u.mesh.ps; 4764 } else { 4765 return; 4766 } 4767 4768 /* 4769 * actually finds last dtim_count, mac80211 will update in 4770 * __beacon_add_tim(). 4771 * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period 4772 */ 4773 do_div(tsf, beacon_int); 4774 bcns_from_dtim = do_div(tsf, dtim_period); 4775 /* just had a DTIM */ 4776 if (!bcns_from_dtim) 4777 dtim_count = 0; 4778 else 4779 dtim_count = dtim_period - bcns_from_dtim; 4780 4781 ps->dtim_count = dtim_count; 4782 } 4783 4784 static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local, 4785 struct ieee80211_chanctx *ctx) 4786 { 4787 struct ieee80211_link_data *link; 4788 u8 radar_detect = 0; 4789 4790 lockdep_assert_held(&local->chanctx_mtx); 4791 4792 if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)) 4793 return 0; 4794 4795 list_for_each_entry(link, &ctx->reserved_links, reserved_chanctx_list) 4796 if (link->reserved_radar_required) 4797 radar_detect |= BIT(link->reserved_chandef.width); 4798 4799 /* 4800 * An in-place reservation context should not have any assigned vifs 4801 * until it replaces the other context. 4802 */ 4803 WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER && 4804 !list_empty(&ctx->assigned_links)); 4805 4806 list_for_each_entry(link, &ctx->assigned_links, assigned_chanctx_list) { 4807 if (!link->radar_required) 4808 continue; 4809 4810 radar_detect |= 4811 BIT(link->conf->chandef.width); 4812 } 4813 4814 return radar_detect; 4815 } 4816 4817 int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata, 4818 const struct cfg80211_chan_def *chandef, 4819 enum ieee80211_chanctx_mode chanmode, 4820 u8 radar_detect) 4821 { 4822 struct ieee80211_local *local = sdata->local; 4823 struct ieee80211_sub_if_data *sdata_iter; 4824 enum nl80211_iftype iftype = sdata->wdev.iftype; 4825 struct ieee80211_chanctx *ctx; 4826 int total = 1; 4827 struct iface_combination_params params = { 4828 .radar_detect = radar_detect, 4829 }; 4830 4831 lockdep_assert_held(&local->chanctx_mtx); 4832 4833 if (WARN_ON(hweight32(radar_detect) > 1)) 4834 return -EINVAL; 4835 4836 if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED && 4837 !chandef->chan)) 4838 return -EINVAL; 4839 4840 if (WARN_ON(iftype >= NUM_NL80211_IFTYPES)) 4841 return -EINVAL; 4842 4843 if (sdata->vif.type == NL80211_IFTYPE_AP || 4844 sdata->vif.type == NL80211_IFTYPE_MESH_POINT) { 4845 /* 4846 * always passing this is harmless, since it'll be the 4847 * same value that cfg80211 finds if it finds the same 4848 * interface ... and that's always allowed 4849 */ 4850 params.new_beacon_int = sdata->vif.bss_conf.beacon_int; 4851 } 4852 4853 /* Always allow software iftypes */ 4854 if (cfg80211_iftype_allowed(local->hw.wiphy, iftype, 0, 1)) { 4855 if (radar_detect) 4856 return -EINVAL; 4857 return 0; 4858 } 4859 4860 if (chandef) 4861 params.num_different_channels = 1; 4862 4863 if (iftype != NL80211_IFTYPE_UNSPECIFIED) 4864 params.iftype_num[iftype] = 1; 4865 4866 list_for_each_entry(ctx, &local->chanctx_list, list) { 4867 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) 4868 continue; 4869 params.radar_detect |= 4870 ieee80211_chanctx_radar_detect(local, ctx); 4871 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) { 4872 params.num_different_channels++; 4873 continue; 4874 } 4875 if (chandef && chanmode == IEEE80211_CHANCTX_SHARED && 4876 cfg80211_chandef_compatible(chandef, 4877 &ctx->conf.def)) 4878 continue; 4879 params.num_different_channels++; 4880 } 4881 4882 list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) { 4883 struct wireless_dev *wdev_iter; 4884 4885 wdev_iter = &sdata_iter->wdev; 4886 4887 if (sdata_iter == sdata || 4888 !ieee80211_sdata_running(sdata_iter) || 4889 cfg80211_iftype_allowed(local->hw.wiphy, 4890 wdev_iter->iftype, 0, 1)) 4891 continue; 4892 4893 params.iftype_num[wdev_iter->iftype]++; 4894 total++; 4895 } 4896 4897 if (total == 1 && !params.radar_detect) 4898 return 0; 4899 4900 return cfg80211_check_combinations(local->hw.wiphy, ¶ms); 4901 } 4902 4903 static void 4904 ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c, 4905 void *data) 4906 { 4907 u32 *max_num_different_channels = data; 4908 4909 *max_num_different_channels = max(*max_num_different_channels, 4910 c->num_different_channels); 4911 } 4912 4913 int ieee80211_max_num_channels(struct ieee80211_local *local) 4914 { 4915 struct ieee80211_sub_if_data *sdata; 4916 struct ieee80211_chanctx *ctx; 4917 u32 max_num_different_channels = 1; 4918 int err; 4919 struct iface_combination_params params = {0}; 4920 4921 lockdep_assert_held(&local->chanctx_mtx); 4922 4923 list_for_each_entry(ctx, &local->chanctx_list, list) { 4924 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) 4925 continue; 4926 4927 params.num_different_channels++; 4928 4929 params.radar_detect |= 4930 ieee80211_chanctx_radar_detect(local, ctx); 4931 } 4932 4933 list_for_each_entry_rcu(sdata, &local->interfaces, list) 4934 params.iftype_num[sdata->wdev.iftype]++; 4935 4936 err = cfg80211_iter_combinations(local->hw.wiphy, ¶ms, 4937 ieee80211_iter_max_chans, 4938 &max_num_different_channels); 4939 if (err < 0) 4940 return err; 4941 4942 return max_num_different_channels; 4943 } 4944 4945 void ieee80211_add_s1g_capab_ie(struct ieee80211_sub_if_data *sdata, 4946 struct ieee80211_sta_s1g_cap *caps, 4947 struct sk_buff *skb) 4948 { 4949 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 4950 struct ieee80211_s1g_cap s1g_capab; 4951 u8 *pos; 4952 int i; 4953 4954 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION)) 4955 return; 4956 4957 if (!caps->s1g) 4958 return; 4959 4960 memcpy(s1g_capab.capab_info, caps->cap, sizeof(caps->cap)); 4961 memcpy(s1g_capab.supp_mcs_nss, caps->nss_mcs, sizeof(caps->nss_mcs)); 4962 4963 /* override the capability info */ 4964 for (i = 0; i < sizeof(ifmgd->s1g_capa.capab_info); i++) { 4965 u8 mask = ifmgd->s1g_capa_mask.capab_info[i]; 4966 4967 s1g_capab.capab_info[i] &= ~mask; 4968 s1g_capab.capab_info[i] |= ifmgd->s1g_capa.capab_info[i] & mask; 4969 } 4970 4971 /* then MCS and NSS set */ 4972 for (i = 0; i < sizeof(ifmgd->s1g_capa.supp_mcs_nss); i++) { 4973 u8 mask = ifmgd->s1g_capa_mask.supp_mcs_nss[i]; 4974 4975 s1g_capab.supp_mcs_nss[i] &= ~mask; 4976 s1g_capab.supp_mcs_nss[i] |= 4977 ifmgd->s1g_capa.supp_mcs_nss[i] & mask; 4978 } 4979 4980 pos = skb_put(skb, 2 + sizeof(s1g_capab)); 4981 *pos++ = WLAN_EID_S1G_CAPABILITIES; 4982 *pos++ = sizeof(s1g_capab); 4983 4984 memcpy(pos, &s1g_capab, sizeof(s1g_capab)); 4985 } 4986 4987 void ieee80211_add_aid_request_ie(struct ieee80211_sub_if_data *sdata, 4988 struct sk_buff *skb) 4989 { 4990 u8 *pos = skb_put(skb, 3); 4991 4992 *pos++ = WLAN_EID_AID_REQUEST; 4993 *pos++ = 1; 4994 *pos++ = 0; 4995 } 4996 4997 u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo) 4998 { 4999 *buf++ = WLAN_EID_VENDOR_SPECIFIC; 5000 *buf++ = 7; /* len */ 5001 *buf++ = 0x00; /* Microsoft OUI 00:50:F2 */ 5002 *buf++ = 0x50; 5003 *buf++ = 0xf2; 5004 *buf++ = 2; /* WME */ 5005 *buf++ = 0; /* WME info */ 5006 *buf++ = 1; /* WME ver */ 5007 *buf++ = qosinfo; /* U-APSD no in use */ 5008 5009 return buf; 5010 } 5011 5012 void ieee80211_txq_get_depth(struct ieee80211_txq *txq, 5013 unsigned long *frame_cnt, 5014 unsigned long *byte_cnt) 5015 { 5016 struct txq_info *txqi = to_txq_info(txq); 5017 u32 frag_cnt = 0, frag_bytes = 0; 5018 struct sk_buff *skb; 5019 5020 skb_queue_walk(&txqi->frags, skb) { 5021 frag_cnt++; 5022 frag_bytes += skb->len; 5023 } 5024 5025 if (frame_cnt) 5026 *frame_cnt = txqi->tin.backlog_packets + frag_cnt; 5027 5028 if (byte_cnt) 5029 *byte_cnt = txqi->tin.backlog_bytes + frag_bytes; 5030 } 5031 EXPORT_SYMBOL(ieee80211_txq_get_depth); 5032 5033 const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = { 5034 IEEE80211_WMM_IE_STA_QOSINFO_AC_VO, 5035 IEEE80211_WMM_IE_STA_QOSINFO_AC_VI, 5036 IEEE80211_WMM_IE_STA_QOSINFO_AC_BE, 5037 IEEE80211_WMM_IE_STA_QOSINFO_AC_BK 5038 }; 5039 5040 u16 ieee80211_encode_usf(int listen_interval) 5041 { 5042 static const int listen_int_usf[] = { 1, 10, 1000, 10000 }; 5043 u16 ui, usf = 0; 5044 5045 /* find greatest USF */ 5046 while (usf < IEEE80211_MAX_USF) { 5047 if (listen_interval % listen_int_usf[usf + 1]) 5048 break; 5049 usf += 1; 5050 } 5051 ui = listen_interval / listen_int_usf[usf]; 5052 5053 /* error if there is a remainder. Should've been checked by user */ 5054 WARN_ON_ONCE(ui > IEEE80211_MAX_UI); 5055 listen_interval = FIELD_PREP(LISTEN_INT_USF, usf) | 5056 FIELD_PREP(LISTEN_INT_UI, ui); 5057 5058 return (u16) listen_interval; 5059 } 5060 5061 u8 ieee80211_ie_len_eht_cap(struct ieee80211_sub_if_data *sdata, u8 iftype) 5062 { 5063 const struct ieee80211_sta_he_cap *he_cap; 5064 const struct ieee80211_sta_eht_cap *eht_cap; 5065 struct ieee80211_supported_band *sband; 5066 bool is_ap; 5067 u8 n; 5068 5069 sband = ieee80211_get_sband(sdata); 5070 if (!sband) 5071 return 0; 5072 5073 he_cap = ieee80211_get_he_iftype_cap(sband, iftype); 5074 eht_cap = ieee80211_get_eht_iftype_cap(sband, iftype); 5075 if (!he_cap || !eht_cap) 5076 return 0; 5077 5078 is_ap = iftype == NL80211_IFTYPE_AP || 5079 iftype == NL80211_IFTYPE_P2P_GO; 5080 5081 n = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem, 5082 &eht_cap->eht_cap_elem, 5083 is_ap); 5084 return 2 + 1 + 5085 sizeof(eht_cap->eht_cap_elem) + n + 5086 ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0], 5087 eht_cap->eht_cap_elem.phy_cap_info); 5088 return 0; 5089 } 5090 5091 u8 *ieee80211_ie_build_eht_cap(u8 *pos, 5092 const struct ieee80211_sta_he_cap *he_cap, 5093 const struct ieee80211_sta_eht_cap *eht_cap, 5094 u8 *end, 5095 bool for_ap) 5096 { 5097 u8 mcs_nss_len, ppet_len; 5098 u8 ie_len; 5099 u8 *orig_pos = pos; 5100 5101 /* Make sure we have place for the IE */ 5102 if (!he_cap || !eht_cap) 5103 return orig_pos; 5104 5105 mcs_nss_len = ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem, 5106 &eht_cap->eht_cap_elem, 5107 for_ap); 5108 ppet_len = ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0], 5109 eht_cap->eht_cap_elem.phy_cap_info); 5110 5111 ie_len = 2 + 1 + sizeof(eht_cap->eht_cap_elem) + mcs_nss_len + ppet_len; 5112 if ((end - pos) < ie_len) 5113 return orig_pos; 5114 5115 *pos++ = WLAN_EID_EXTENSION; 5116 *pos++ = ie_len - 2; 5117 *pos++ = WLAN_EID_EXT_EHT_CAPABILITY; 5118 5119 /* Fixed data */ 5120 memcpy(pos, &eht_cap->eht_cap_elem, sizeof(eht_cap->eht_cap_elem)); 5121 pos += sizeof(eht_cap->eht_cap_elem); 5122 5123 memcpy(pos, &eht_cap->eht_mcs_nss_supp, mcs_nss_len); 5124 pos += mcs_nss_len; 5125 5126 if (ppet_len) { 5127 memcpy(pos, &eht_cap->eht_ppe_thres, ppet_len); 5128 pos += ppet_len; 5129 } 5130 5131 return pos; 5132 } 5133 5134 void ieee80211_fragment_element(struct sk_buff *skb, u8 *len_pos, u8 frag_id) 5135 { 5136 unsigned int elem_len; 5137 5138 if (!len_pos) 5139 return; 5140 5141 elem_len = skb->data + skb->len - len_pos - 1; 5142 5143 while (elem_len > 255) { 5144 /* this one is 255 */ 5145 *len_pos = 255; 5146 /* remaining data gets smaller */ 5147 elem_len -= 255; 5148 /* make space for the fragment ID/len in SKB */ 5149 skb_put(skb, 2); 5150 /* shift back the remaining data to place fragment ID/len */ 5151 memmove(len_pos + 255 + 3, len_pos + 255 + 1, elem_len); 5152 /* place the fragment ID */ 5153 len_pos += 255 + 1; 5154 *len_pos = frag_id; 5155 /* and point to fragment length to update later */ 5156 len_pos++; 5157 } 5158 5159 *len_pos = elem_len; 5160 } 5161