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) 2018-2024 Intel Corporation 9 * 10 * Transmit and frame generation functions. 11 */ 12 13 #include <linux/kernel.h> 14 #include <linux/slab.h> 15 #include <linux/skbuff.h> 16 #include <linux/if_vlan.h> 17 #include <linux/etherdevice.h> 18 #include <linux/bitmap.h> 19 #include <linux/rcupdate.h> 20 #include <linux/export.h> 21 #include <net/net_namespace.h> 22 #include <net/ieee80211_radiotap.h> 23 #include <net/cfg80211.h> 24 #include <net/mac80211.h> 25 #include <net/codel.h> 26 #include <net/codel_impl.h> 27 #include <linux/unaligned.h> 28 #include <net/fq_impl.h> 29 #include <net/sock.h> 30 #include <net/gso.h> 31 32 #include "ieee80211_i.h" 33 #include "driver-ops.h" 34 #include "led.h" 35 #include "mesh.h" 36 #include "wep.h" 37 #include "wpa.h" 38 #include "wme.h" 39 #include "rate.h" 40 41 /* misc utils */ 42 43 static __le16 ieee80211_duration(struct ieee80211_tx_data *tx, 44 struct sk_buff *skb, int group_addr, 45 int next_frag_len) 46 { 47 int rate, mrate, erp, dur, i; 48 struct ieee80211_rate *txrate; 49 struct ieee80211_local *local = tx->local; 50 struct ieee80211_supported_band *sband; 51 struct ieee80211_hdr *hdr; 52 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 53 54 /* assume HW handles this */ 55 if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS)) 56 return 0; 57 58 /* uh huh? */ 59 if (WARN_ON_ONCE(tx->rate.idx < 0)) 60 return 0; 61 62 sband = local->hw.wiphy->bands[info->band]; 63 txrate = &sband->bitrates[tx->rate.idx]; 64 65 erp = txrate->flags & IEEE80211_RATE_ERP_G; 66 67 /* device is expected to do this */ 68 if (sband->band == NL80211_BAND_S1GHZ) 69 return 0; 70 71 /* 72 * data and mgmt (except PS Poll): 73 * - during CFP: 32768 74 * - during contention period: 75 * if addr1 is group address: 0 76 * if more fragments = 0 and addr1 is individual address: time to 77 * transmit one ACK plus SIFS 78 * if more fragments = 1 and addr1 is individual address: time to 79 * transmit next fragment plus 2 x ACK plus 3 x SIFS 80 * 81 * IEEE 802.11, 9.6: 82 * - control response frame (CTS or ACK) shall be transmitted using the 83 * same rate as the immediately previous frame in the frame exchange 84 * sequence, if this rate belongs to the PHY mandatory rates, or else 85 * at the highest possible rate belonging to the PHY rates in the 86 * BSSBasicRateSet 87 */ 88 hdr = (struct ieee80211_hdr *)skb->data; 89 if (ieee80211_is_ctl(hdr->frame_control)) { 90 /* TODO: These control frames are not currently sent by 91 * mac80211, but should they be implemented, this function 92 * needs to be updated to support duration field calculation. 93 * 94 * RTS: time needed to transmit pending data/mgmt frame plus 95 * one CTS frame plus one ACK frame plus 3 x SIFS 96 * CTS: duration of immediately previous RTS minus time 97 * required to transmit CTS and its SIFS 98 * ACK: 0 if immediately previous directed data/mgmt had 99 * more=0, with more=1 duration in ACK frame is duration 100 * from previous frame minus time needed to transmit ACK 101 * and its SIFS 102 * PS Poll: BIT(15) | BIT(14) | aid 103 */ 104 return 0; 105 } 106 107 /* data/mgmt */ 108 if (0 /* FIX: data/mgmt during CFP */) 109 return cpu_to_le16(32768); 110 111 if (group_addr) /* Group address as the destination - no ACK */ 112 return 0; 113 114 /* Individual destination address: 115 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes) 116 * CTS and ACK frames shall be transmitted using the highest rate in 117 * basic rate set that is less than or equal to the rate of the 118 * immediately previous frame and that is using the same modulation 119 * (CCK or OFDM). If no basic rate set matches with these requirements, 120 * the highest mandatory rate of the PHY that is less than or equal to 121 * the rate of the previous frame is used. 122 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps 123 */ 124 rate = -1; 125 /* use lowest available if everything fails */ 126 mrate = sband->bitrates[0].bitrate; 127 for (i = 0; i < sband->n_bitrates; i++) { 128 struct ieee80211_rate *r = &sband->bitrates[i]; 129 u32 flag; 130 131 if (r->bitrate > txrate->bitrate) 132 break; 133 134 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i)) 135 rate = r->bitrate; 136 137 switch (sband->band) { 138 case NL80211_BAND_2GHZ: 139 case NL80211_BAND_LC: 140 if (tx->sdata->deflink.operating_11g_mode) 141 flag = IEEE80211_RATE_MANDATORY_G; 142 else 143 flag = IEEE80211_RATE_MANDATORY_B; 144 break; 145 case NL80211_BAND_5GHZ: 146 case NL80211_BAND_6GHZ: 147 flag = IEEE80211_RATE_MANDATORY_A; 148 break; 149 default: 150 flag = 0; 151 WARN_ON(1); 152 break; 153 } 154 155 if (r->flags & flag) 156 mrate = r->bitrate; 157 } 158 if (rate == -1) { 159 /* No matching basic rate found; use highest suitable mandatory 160 * PHY rate */ 161 rate = mrate; 162 } 163 164 /* Don't calculate ACKs for QoS Frames with NoAck Policy set */ 165 if (ieee80211_is_data_qos(hdr->frame_control) && 166 *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK) 167 dur = 0; 168 else 169 /* Time needed to transmit ACK 170 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up 171 * to closest integer */ 172 dur = ieee80211_frame_duration(sband->band, 10, rate, erp, 173 tx->sdata->vif.bss_conf.use_short_preamble); 174 175 if (next_frag_len) { 176 /* Frame is fragmented: duration increases with time needed to 177 * transmit next fragment plus ACK and 2 x SIFS. */ 178 dur *= 2; /* ACK + SIFS */ 179 /* next fragment */ 180 dur += ieee80211_frame_duration(sband->band, next_frag_len, 181 txrate->bitrate, erp, 182 tx->sdata->vif.bss_conf.use_short_preamble); 183 } 184 185 return cpu_to_le16(dur); 186 } 187 188 /* tx handlers */ 189 static ieee80211_tx_result debug_noinline 190 ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx) 191 { 192 struct ieee80211_local *local = tx->local; 193 struct ieee80211_if_managed *ifmgd; 194 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb); 195 196 /* driver doesn't support power save */ 197 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS)) 198 return TX_CONTINUE; 199 200 /* hardware does dynamic power save */ 201 if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) 202 return TX_CONTINUE; 203 204 /* dynamic power save disabled */ 205 if (local->hw.conf.dynamic_ps_timeout <= 0) 206 return TX_CONTINUE; 207 208 /* we are scanning, don't enable power save */ 209 if (local->scanning) 210 return TX_CONTINUE; 211 212 if (!local->ps_sdata) 213 return TX_CONTINUE; 214 215 /* No point if we're going to suspend */ 216 if (local->quiescing) 217 return TX_CONTINUE; 218 219 /* dynamic ps is supported only in managed mode */ 220 if (tx->sdata->vif.type != NL80211_IFTYPE_STATION) 221 return TX_CONTINUE; 222 223 if (unlikely(info->flags & IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) 224 return TX_CONTINUE; 225 226 ifmgd = &tx->sdata->u.mgd; 227 228 /* 229 * Don't wakeup from power save if u-apsd is enabled, voip ac has 230 * u-apsd enabled and the frame is in voip class. This effectively 231 * means that even if all access categories have u-apsd enabled, in 232 * practise u-apsd is only used with the voip ac. This is a 233 * workaround for the case when received voip class packets do not 234 * have correct qos tag for some reason, due the network or the 235 * peer application. 236 * 237 * Note: ifmgd->uapsd_queues access is racy here. If the value is 238 * changed via debugfs, user needs to reassociate manually to have 239 * everything in sync. 240 */ 241 if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) && 242 (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) && 243 skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO) 244 return TX_CONTINUE; 245 246 if (local->hw.conf.flags & IEEE80211_CONF_PS) { 247 ieee80211_stop_queues_by_reason(&local->hw, 248 IEEE80211_MAX_QUEUE_MAP, 249 IEEE80211_QUEUE_STOP_REASON_PS, 250 false); 251 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED; 252 wiphy_work_queue(local->hw.wiphy, 253 &local->dynamic_ps_disable_work); 254 } 255 256 /* Don't restart the timer if we're not disassociated */ 257 if (!ifmgd->associated) 258 return TX_CONTINUE; 259 260 mod_timer(&local->dynamic_ps_timer, jiffies + 261 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout)); 262 263 return TX_CONTINUE; 264 } 265 266 static ieee80211_tx_result debug_noinline 267 ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx) 268 { 269 270 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data; 271 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb); 272 bool assoc = false; 273 274 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED)) 275 return TX_CONTINUE; 276 277 if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) && 278 test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) && 279 !ieee80211_is_probe_req(hdr->frame_control) && 280 !ieee80211_is_any_nullfunc(hdr->frame_control)) 281 /* 282 * When software scanning only nullfunc frames (to notify 283 * the sleep state to the AP) and probe requests (for the 284 * active scan) are allowed, all other frames should not be 285 * sent and we should not get here, but if we do 286 * nonetheless, drop them to avoid sending them 287 * off-channel. See the link below and 288 * ieee80211_start_scan() for more. 289 * 290 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089 291 */ 292 return TX_DROP; 293 294 if (tx->sdata->vif.type == NL80211_IFTYPE_OCB) 295 return TX_CONTINUE; 296 297 if (tx->flags & IEEE80211_TX_PS_BUFFERED) 298 return TX_CONTINUE; 299 300 if (tx->sta) 301 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC); 302 303 if (likely(tx->flags & IEEE80211_TX_UNICAST)) { 304 if (unlikely(!assoc && 305 ieee80211_is_data(hdr->frame_control))) { 306 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 307 sdata_info(tx->sdata, 308 "dropped data frame to not associated station %pM\n", 309 hdr->addr1); 310 #endif 311 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc); 312 return TX_DROP; 313 } 314 } else if (unlikely(ieee80211_is_data(hdr->frame_control) && 315 ieee80211_vif_get_num_mcast_if(tx->sdata) == 0)) { 316 /* 317 * No associated STAs - no need to send multicast 318 * frames. 319 */ 320 return TX_DROP; 321 } 322 323 return TX_CONTINUE; 324 } 325 326 /* This function is called whenever the AP is about to exceed the maximum limit 327 * of buffered frames for power saving STAs. This situation should not really 328 * happen often during normal operation, so dropping the oldest buffered packet 329 * from each queue should be OK to make some room for new frames. */ 330 static void purge_old_ps_buffers(struct ieee80211_local *local) 331 { 332 int total = 0, purged = 0; 333 struct sk_buff *skb; 334 struct ieee80211_sub_if_data *sdata; 335 struct sta_info *sta; 336 337 list_for_each_entry_rcu(sdata, &local->interfaces, list) { 338 struct ps_data *ps; 339 340 if (sdata->vif.type == NL80211_IFTYPE_AP) 341 ps = &sdata->u.ap.ps; 342 else if (ieee80211_vif_is_mesh(&sdata->vif)) 343 ps = &sdata->u.mesh.ps; 344 else 345 continue; 346 347 skb = skb_dequeue(&ps->bc_buf); 348 if (skb) { 349 purged++; 350 ieee80211_free_txskb(&local->hw, skb); 351 } 352 total += skb_queue_len(&ps->bc_buf); 353 } 354 355 /* 356 * Drop one frame from each station from the lowest-priority 357 * AC that has frames at all. 358 */ 359 list_for_each_entry_rcu(sta, &local->sta_list, list) { 360 int ac; 361 362 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) { 363 skb = skb_dequeue(&sta->ps_tx_buf[ac]); 364 total += skb_queue_len(&sta->ps_tx_buf[ac]); 365 if (skb) { 366 purged++; 367 ieee80211_free_txskb(&local->hw, skb); 368 break; 369 } 370 } 371 } 372 373 local->total_ps_buffered = total; 374 ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged); 375 } 376 377 static ieee80211_tx_result 378 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx) 379 { 380 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb); 381 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data; 382 struct ps_data *ps; 383 384 /* 385 * broadcast/multicast frame 386 * 387 * If any of the associated/peer stations is in power save mode, 388 * the frame is buffered to be sent after DTIM beacon frame. 389 * This is done either by the hardware or us. 390 */ 391 392 /* powersaving STAs currently only in AP/VLAN/mesh mode */ 393 if (tx->sdata->vif.type == NL80211_IFTYPE_AP || 394 tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 395 if (!tx->sdata->bss) 396 return TX_CONTINUE; 397 398 ps = &tx->sdata->bss->ps; 399 } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) { 400 ps = &tx->sdata->u.mesh.ps; 401 } else { 402 return TX_CONTINUE; 403 } 404 405 406 /* no buffering for ordered frames */ 407 if (ieee80211_has_order(hdr->frame_control)) 408 return TX_CONTINUE; 409 410 if (ieee80211_is_probe_req(hdr->frame_control)) 411 return TX_CONTINUE; 412 413 if (ieee80211_hw_check(&tx->local->hw, QUEUE_CONTROL)) 414 info->hw_queue = tx->sdata->vif.cab_queue; 415 416 /* no stations in PS mode and no buffered packets */ 417 if (!atomic_read(&ps->num_sta_ps) && skb_queue_empty(&ps->bc_buf)) 418 return TX_CONTINUE; 419 420 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM; 421 422 /* device releases frame after DTIM beacon */ 423 if (!ieee80211_hw_check(&tx->local->hw, HOST_BROADCAST_PS_BUFFERING)) 424 return TX_CONTINUE; 425 426 /* buffered in mac80211 */ 427 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER) 428 purge_old_ps_buffers(tx->local); 429 430 if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) { 431 ps_dbg(tx->sdata, 432 "BC TX buffer full - dropping the oldest frame\n"); 433 ieee80211_free_txskb(&tx->local->hw, skb_dequeue(&ps->bc_buf)); 434 } else 435 tx->local->total_ps_buffered++; 436 437 skb_queue_tail(&ps->bc_buf, tx->skb); 438 439 return TX_QUEUED; 440 } 441 442 static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta, 443 struct sk_buff *skb) 444 { 445 if (!ieee80211_is_mgmt(fc)) 446 return 0; 447 448 if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP)) 449 return 0; 450 451 if (!ieee80211_is_robust_mgmt_frame(skb)) 452 return 0; 453 454 return 1; 455 } 456 457 static ieee80211_tx_result 458 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx) 459 { 460 struct sta_info *sta = tx->sta; 461 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb); 462 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data; 463 struct ieee80211_local *local = tx->local; 464 465 if (unlikely(!sta)) 466 return TX_CONTINUE; 467 468 if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) || 469 test_sta_flag(sta, WLAN_STA_PS_DRIVER) || 470 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) && 471 !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) { 472 int ac = skb_get_queue_mapping(tx->skb); 473 474 if (ieee80211_is_mgmt(hdr->frame_control) && 475 !ieee80211_is_bufferable_mmpdu(tx->skb)) { 476 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER; 477 return TX_CONTINUE; 478 } 479 480 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n", 481 sta->sta.addr, sta->sta.aid, ac); 482 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER) 483 purge_old_ps_buffers(tx->local); 484 485 /* sync with ieee80211_sta_ps_deliver_wakeup */ 486 spin_lock(&sta->ps_lock); 487 /* 488 * STA woke up the meantime and all the frames on ps_tx_buf have 489 * been queued to pending queue. No reordering can happen, go 490 * ahead and Tx the packet. 491 */ 492 if (!test_sta_flag(sta, WLAN_STA_PS_STA) && 493 !test_sta_flag(sta, WLAN_STA_PS_DRIVER) && 494 !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) { 495 spin_unlock(&sta->ps_lock); 496 return TX_CONTINUE; 497 } 498 499 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) { 500 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]); 501 ps_dbg(tx->sdata, 502 "STA %pM TX buffer for AC %d full - dropping oldest frame\n", 503 sta->sta.addr, ac); 504 ieee80211_free_txskb(&local->hw, old); 505 } else 506 tx->local->total_ps_buffered++; 507 508 info->control.jiffies = jiffies; 509 info->control.vif = &tx->sdata->vif; 510 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING; 511 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS; 512 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb); 513 spin_unlock(&sta->ps_lock); 514 515 if (!timer_pending(&local->sta_cleanup)) 516 mod_timer(&local->sta_cleanup, 517 round_jiffies(jiffies + 518 STA_INFO_CLEANUP_INTERVAL)); 519 520 /* 521 * We queued up some frames, so the TIM bit might 522 * need to be set, recalculate it. 523 */ 524 sta_info_recalc_tim(sta); 525 526 return TX_QUEUED; 527 } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) { 528 ps_dbg(tx->sdata, 529 "STA %pM in PS mode, but polling/in SP -> send frame\n", 530 sta->sta.addr); 531 } 532 533 return TX_CONTINUE; 534 } 535 536 static ieee80211_tx_result debug_noinline 537 ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx) 538 { 539 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED)) 540 return TX_CONTINUE; 541 542 if (tx->flags & IEEE80211_TX_UNICAST) 543 return ieee80211_tx_h_unicast_ps_buf(tx); 544 else 545 return ieee80211_tx_h_multicast_ps_buf(tx); 546 } 547 548 static ieee80211_tx_result debug_noinline 549 ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx) 550 { 551 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb); 552 553 if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) { 554 if (tx->sdata->control_port_no_encrypt) 555 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT; 556 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO; 557 info->flags |= IEEE80211_TX_CTL_USE_MINRATE; 558 } 559 560 return TX_CONTINUE; 561 } 562 563 static struct ieee80211_key * 564 ieee80211_select_link_key(struct ieee80211_tx_data *tx) 565 { 566 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data; 567 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb); 568 struct ieee80211_link_data *link; 569 unsigned int link_id; 570 571 link_id = u32_get_bits(info->control.flags, IEEE80211_TX_CTRL_MLO_LINK); 572 if (link_id == IEEE80211_LINK_UNSPECIFIED) { 573 link = &tx->sdata->deflink; 574 } else { 575 link = rcu_dereference(tx->sdata->link[link_id]); 576 if (!link) 577 return NULL; 578 } 579 580 if (ieee80211_is_group_privacy_action(tx->skb)) 581 return rcu_dereference(link->default_multicast_key); 582 else if (ieee80211_is_mgmt(hdr->frame_control) && 583 is_multicast_ether_addr(hdr->addr1) && 584 ieee80211_is_robust_mgmt_frame(tx->skb)) 585 return rcu_dereference(link->default_mgmt_key); 586 else if (is_multicast_ether_addr(hdr->addr1)) 587 return rcu_dereference(link->default_multicast_key); 588 589 return NULL; 590 } 591 592 static ieee80211_tx_result debug_noinline 593 ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx) 594 { 595 struct ieee80211_key *key; 596 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb); 597 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data; 598 599 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)) { 600 tx->key = NULL; 601 return TX_CONTINUE; 602 } 603 604 if (tx->sta && 605 (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx]))) 606 tx->key = key; 607 else if ((key = ieee80211_select_link_key(tx))) 608 tx->key = key; 609 else if (!is_multicast_ether_addr(hdr->addr1) && 610 (key = rcu_dereference(tx->sdata->default_unicast_key))) 611 tx->key = key; 612 else 613 tx->key = NULL; 614 615 if (tx->key) { 616 bool skip_hw = false; 617 618 /* TODO: add threshold stuff again */ 619 620 switch (tx->key->conf.cipher) { 621 case WLAN_CIPHER_SUITE_WEP40: 622 case WLAN_CIPHER_SUITE_WEP104: 623 case WLAN_CIPHER_SUITE_TKIP: 624 if (!ieee80211_is_data_present(hdr->frame_control)) 625 tx->key = NULL; 626 break; 627 case WLAN_CIPHER_SUITE_CCMP: 628 case WLAN_CIPHER_SUITE_CCMP_256: 629 case WLAN_CIPHER_SUITE_GCMP: 630 case WLAN_CIPHER_SUITE_GCMP_256: 631 if (!ieee80211_is_data_present(hdr->frame_control) && 632 !ieee80211_use_mfp(hdr->frame_control, tx->sta, 633 tx->skb) && 634 !ieee80211_is_group_privacy_action(tx->skb)) 635 tx->key = NULL; 636 else 637 skip_hw = (tx->key->conf.flags & 638 IEEE80211_KEY_FLAG_SW_MGMT_TX) && 639 ieee80211_is_mgmt(hdr->frame_control); 640 break; 641 case WLAN_CIPHER_SUITE_AES_CMAC: 642 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 643 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 644 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 645 if (!ieee80211_is_mgmt(hdr->frame_control)) 646 tx->key = NULL; 647 break; 648 } 649 650 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED && 651 !ieee80211_is_deauth(hdr->frame_control)) && 652 tx->skb->protocol != tx->sdata->control_port_protocol) 653 return TX_DROP; 654 655 if (!skip_hw && tx->key && 656 tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) 657 info->control.hw_key = &tx->key->conf; 658 } else if (ieee80211_is_data_present(hdr->frame_control) && tx->sta && 659 test_sta_flag(tx->sta, WLAN_STA_USES_ENCRYPTION)) { 660 return TX_DROP; 661 } 662 663 return TX_CONTINUE; 664 } 665 666 static ieee80211_tx_result debug_noinline 667 ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx) 668 { 669 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb); 670 struct ieee80211_hdr *hdr = (void *)tx->skb->data; 671 struct ieee80211_supported_band *sband; 672 u32 len; 673 struct ieee80211_tx_rate_control txrc; 674 struct ieee80211_sta_rates *ratetbl = NULL; 675 bool encap = info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP; 676 bool assoc = false; 677 678 memset(&txrc, 0, sizeof(txrc)); 679 680 sband = tx->local->hw.wiphy->bands[info->band]; 681 682 len = min_t(u32, tx->skb->len + FCS_LEN, 683 tx->local->hw.wiphy->frag_threshold); 684 685 /* set up the tx rate control struct we give the RC algo */ 686 txrc.hw = &tx->local->hw; 687 txrc.sband = sband; 688 txrc.bss_conf = &tx->sdata->vif.bss_conf; 689 txrc.skb = tx->skb; 690 txrc.reported_rate.idx = -1; 691 692 if (unlikely(info->control.flags & IEEE80211_TX_CTRL_DONT_USE_RATE_MASK)) { 693 txrc.rate_idx_mask = ~0; 694 } else { 695 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band]; 696 697 if (tx->sdata->rc_has_mcs_mask[info->band]) 698 txrc.rate_idx_mcs_mask = 699 tx->sdata->rc_rateidx_mcs_mask[info->band]; 700 } 701 702 txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP || 703 tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT || 704 tx->sdata->vif.type == NL80211_IFTYPE_ADHOC || 705 tx->sdata->vif.type == NL80211_IFTYPE_OCB); 706 707 /* set up RTS protection if desired */ 708 if (len > tx->local->hw.wiphy->rts_threshold) { 709 txrc.rts = true; 710 } 711 712 info->control.use_rts = txrc.rts; 713 info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot; 714 715 /* 716 * Use short preamble if the BSS can handle it, but not for 717 * management frames unless we know the receiver can handle 718 * that -- the management frame might be to a station that 719 * just wants a probe response. 720 */ 721 if (tx->sdata->vif.bss_conf.use_short_preamble && 722 (ieee80211_is_tx_data(tx->skb) || 723 (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE)))) 724 txrc.short_preamble = true; 725 726 info->control.short_preamble = txrc.short_preamble; 727 728 /* don't ask rate control when rate already injected via radiotap */ 729 if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT) 730 return TX_CONTINUE; 731 732 if (tx->sta) 733 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC); 734 735 /* 736 * Lets not bother rate control if we're associated and cannot 737 * talk to the sta. This should not happen. 738 */ 739 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc && 740 !rate_usable_index_exists(sband, &tx->sta->sta), 741 "%s: Dropped data frame as no usable bitrate found while " 742 "scanning and associated. Target station: " 743 "%pM on %d GHz band\n", 744 tx->sdata->name, 745 encap ? ((struct ethhdr *)hdr)->h_dest : hdr->addr1, 746 info->band ? 5 : 2)) 747 return TX_DROP; 748 749 /* 750 * If we're associated with the sta at this point we know we can at 751 * least send the frame at the lowest bit rate. 752 */ 753 rate_control_get_rate(tx->sdata, tx->sta, &txrc); 754 755 if (tx->sta && !info->control.skip_table) 756 ratetbl = rcu_dereference(tx->sta->sta.rates); 757 758 if (unlikely(info->control.rates[0].idx < 0)) { 759 if (ratetbl) { 760 struct ieee80211_tx_rate rate = { 761 .idx = ratetbl->rate[0].idx, 762 .flags = ratetbl->rate[0].flags, 763 .count = ratetbl->rate[0].count 764 }; 765 766 if (ratetbl->rate[0].idx < 0) 767 return TX_DROP; 768 769 tx->rate = rate; 770 } else { 771 return TX_DROP; 772 } 773 } else { 774 tx->rate = info->control.rates[0]; 775 } 776 777 if (txrc.reported_rate.idx < 0) { 778 txrc.reported_rate = tx->rate; 779 if (tx->sta && ieee80211_is_tx_data(tx->skb)) 780 tx->sta->deflink.tx_stats.last_rate = txrc.reported_rate; 781 } else if (tx->sta) 782 tx->sta->deflink.tx_stats.last_rate = txrc.reported_rate; 783 784 if (ratetbl) 785 return TX_CONTINUE; 786 787 if (unlikely(!info->control.rates[0].count)) 788 info->control.rates[0].count = 1; 789 790 if (WARN_ON_ONCE((info->control.rates[0].count > 1) && 791 (info->flags & IEEE80211_TX_CTL_NO_ACK))) 792 info->control.rates[0].count = 1; 793 794 return TX_CONTINUE; 795 } 796 797 static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid) 798 { 799 u16 *seq = &sta->tid_seq[tid]; 800 __le16 ret = cpu_to_le16(*seq); 801 802 /* Increase the sequence number. */ 803 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ; 804 805 return ret; 806 } 807 808 static ieee80211_tx_result debug_noinline 809 ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx) 810 { 811 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb); 812 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data; 813 int tid; 814 815 /* 816 * Packet injection may want to control the sequence 817 * number, if we have no matching interface then we 818 * neither assign one ourselves nor ask the driver to. 819 */ 820 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR)) 821 return TX_CONTINUE; 822 823 if (unlikely(ieee80211_is_ctl(hdr->frame_control))) 824 return TX_CONTINUE; 825 826 if (ieee80211_hdrlen(hdr->frame_control) < 24) 827 return TX_CONTINUE; 828 829 if (ieee80211_is_qos_nullfunc(hdr->frame_control)) 830 return TX_CONTINUE; 831 832 if (info->control.flags & IEEE80211_TX_CTRL_NO_SEQNO) 833 return TX_CONTINUE; 834 835 /* SNS11 from 802.11be 10.3.2.14 */ 836 if (unlikely(is_multicast_ether_addr(hdr->addr1) && 837 ieee80211_vif_is_mld(info->control.vif) && 838 info->control.vif->type == NL80211_IFTYPE_AP)) { 839 if (info->control.flags & IEEE80211_TX_CTRL_MCAST_MLO_FIRST_TX) 840 tx->sdata->mld_mcast_seq += 0x10; 841 hdr->seq_ctrl = cpu_to_le16(tx->sdata->mld_mcast_seq); 842 return TX_CONTINUE; 843 } 844 845 /* 846 * Anything but QoS data that has a sequence number field 847 * (is long enough) gets a sequence number from the global 848 * counter. QoS data frames with a multicast destination 849 * also use the global counter (802.11-2012 9.3.2.10). 850 */ 851 if (!ieee80211_is_data_qos(hdr->frame_control) || 852 is_multicast_ether_addr(hdr->addr1)) { 853 /* driver should assign sequence number */ 854 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ; 855 /* for pure STA mode without beacons, we can do it */ 856 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number); 857 tx->sdata->sequence_number += 0x10; 858 if (tx->sta) 859 tx->sta->deflink.tx_stats.msdu[IEEE80211_NUM_TIDS]++; 860 return TX_CONTINUE; 861 } 862 863 /* 864 * This should be true for injected/management frames only, for 865 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ 866 * above since they are not QoS-data frames. 867 */ 868 if (!tx->sta) 869 return TX_CONTINUE; 870 871 /* include per-STA, per-TID sequence counter */ 872 tid = ieee80211_get_tid(hdr); 873 tx->sta->deflink.tx_stats.msdu[tid]++; 874 875 hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid); 876 877 return TX_CONTINUE; 878 } 879 880 static int ieee80211_fragment(struct ieee80211_tx_data *tx, 881 struct sk_buff *skb, int hdrlen, 882 int frag_threshold) 883 { 884 struct ieee80211_local *local = tx->local; 885 struct ieee80211_tx_info *info; 886 struct sk_buff *tmp; 887 int per_fragm = frag_threshold - hdrlen - FCS_LEN; 888 int pos = hdrlen + per_fragm; 889 int rem = skb->len - hdrlen - per_fragm; 890 891 if (WARN_ON(rem < 0)) 892 return -EINVAL; 893 894 /* first fragment was already added to queue by caller */ 895 896 while (rem) { 897 int fraglen = per_fragm; 898 899 if (fraglen > rem) 900 fraglen = rem; 901 rem -= fraglen; 902 tmp = dev_alloc_skb(local->tx_headroom + 903 frag_threshold + 904 IEEE80211_ENCRYPT_HEADROOM + 905 IEEE80211_ENCRYPT_TAILROOM); 906 if (!tmp) 907 return -ENOMEM; 908 909 __skb_queue_tail(&tx->skbs, tmp); 910 911 skb_reserve(tmp, 912 local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM); 913 914 /* copy control information */ 915 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb)); 916 917 info = IEEE80211_SKB_CB(tmp); 918 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT | 919 IEEE80211_TX_CTL_FIRST_FRAGMENT); 920 921 if (rem) 922 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES; 923 924 skb_copy_queue_mapping(tmp, skb); 925 tmp->priority = skb->priority; 926 tmp->dev = skb->dev; 927 928 /* copy header and data */ 929 skb_put_data(tmp, skb->data, hdrlen); 930 skb_put_data(tmp, skb->data + pos, fraglen); 931 932 pos += fraglen; 933 } 934 935 /* adjust first fragment's length */ 936 skb_trim(skb, hdrlen + per_fragm); 937 return 0; 938 } 939 940 static ieee80211_tx_result debug_noinline 941 ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx) 942 { 943 struct sk_buff *skb = tx->skb; 944 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 945 struct ieee80211_hdr *hdr = (void *)skb->data; 946 int frag_threshold = tx->local->hw.wiphy->frag_threshold; 947 int hdrlen; 948 int fragnum; 949 950 /* no matter what happens, tx->skb moves to tx->skbs */ 951 __skb_queue_tail(&tx->skbs, skb); 952 tx->skb = NULL; 953 954 if (info->flags & IEEE80211_TX_CTL_DONTFRAG) 955 return TX_CONTINUE; 956 957 if (ieee80211_hw_check(&tx->local->hw, SUPPORTS_TX_FRAG)) 958 return TX_CONTINUE; 959 960 /* 961 * Warn when submitting a fragmented A-MPDU frame and drop it. 962 * This scenario is handled in ieee80211_tx_prepare but extra 963 * caution taken here as fragmented ampdu may cause Tx stop. 964 */ 965 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU)) 966 return TX_DROP; 967 968 hdrlen = ieee80211_hdrlen(hdr->frame_control); 969 970 /* internal error, why isn't DONTFRAG set? */ 971 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold)) 972 return TX_DROP; 973 974 /* 975 * Now fragment the frame. This will allocate all the fragments and 976 * chain them (using skb as the first fragment) to skb->next. 977 * During transmission, we will remove the successfully transmitted 978 * fragments from this list. When the low-level driver rejects one 979 * of the fragments then we will simply pretend to accept the skb 980 * but store it away as pending. 981 */ 982 if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold)) 983 return TX_DROP; 984 985 /* update duration/seq/flags of fragments */ 986 fragnum = 0; 987 988 skb_queue_walk(&tx->skbs, skb) { 989 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS); 990 991 hdr = (void *)skb->data; 992 info = IEEE80211_SKB_CB(skb); 993 994 if (!skb_queue_is_last(&tx->skbs, skb)) { 995 hdr->frame_control |= morefrags; 996 /* 997 * No multi-rate retries for fragmented frames, that 998 * would completely throw off the NAV at other STAs. 999 */ 1000 info->control.rates[1].idx = -1; 1001 info->control.rates[2].idx = -1; 1002 info->control.rates[3].idx = -1; 1003 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4); 1004 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE; 1005 } else { 1006 hdr->frame_control &= ~morefrags; 1007 } 1008 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG); 1009 fragnum++; 1010 } 1011 1012 return TX_CONTINUE; 1013 } 1014 1015 static ieee80211_tx_result debug_noinline 1016 ieee80211_tx_h_stats(struct ieee80211_tx_data *tx) 1017 { 1018 struct sk_buff *skb; 1019 int ac = -1; 1020 1021 if (!tx->sta) 1022 return TX_CONTINUE; 1023 1024 skb_queue_walk(&tx->skbs, skb) { 1025 ac = skb_get_queue_mapping(skb); 1026 tx->sta->deflink.tx_stats.bytes[ac] += skb->len; 1027 } 1028 if (ac >= 0) 1029 tx->sta->deflink.tx_stats.packets[ac]++; 1030 1031 return TX_CONTINUE; 1032 } 1033 1034 static ieee80211_tx_result debug_noinline 1035 ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx) 1036 { 1037 if (!tx->key) 1038 return TX_CONTINUE; 1039 1040 switch (tx->key->conf.cipher) { 1041 case WLAN_CIPHER_SUITE_WEP40: 1042 case WLAN_CIPHER_SUITE_WEP104: 1043 return ieee80211_crypto_wep_encrypt(tx); 1044 case WLAN_CIPHER_SUITE_TKIP: 1045 return ieee80211_crypto_tkip_encrypt(tx); 1046 case WLAN_CIPHER_SUITE_CCMP: 1047 return ieee80211_crypto_ccmp_encrypt( 1048 tx, IEEE80211_CCMP_MIC_LEN); 1049 case WLAN_CIPHER_SUITE_CCMP_256: 1050 return ieee80211_crypto_ccmp_encrypt( 1051 tx, IEEE80211_CCMP_256_MIC_LEN); 1052 case WLAN_CIPHER_SUITE_AES_CMAC: 1053 return ieee80211_crypto_aes_cmac_encrypt(tx); 1054 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 1055 return ieee80211_crypto_aes_cmac_256_encrypt(tx); 1056 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 1057 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 1058 return ieee80211_crypto_aes_gmac_encrypt(tx); 1059 case WLAN_CIPHER_SUITE_GCMP: 1060 case WLAN_CIPHER_SUITE_GCMP_256: 1061 return ieee80211_crypto_gcmp_encrypt(tx); 1062 } 1063 1064 return TX_DROP; 1065 } 1066 1067 static ieee80211_tx_result debug_noinline 1068 ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx) 1069 { 1070 struct sk_buff *skb; 1071 struct ieee80211_hdr *hdr; 1072 int next_len; 1073 bool group_addr; 1074 1075 skb_queue_walk(&tx->skbs, skb) { 1076 hdr = (void *) skb->data; 1077 if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) 1078 break; /* must not overwrite AID */ 1079 if (!skb_queue_is_last(&tx->skbs, skb)) { 1080 struct sk_buff *next = skb_queue_next(&tx->skbs, skb); 1081 next_len = next->len; 1082 } else 1083 next_len = 0; 1084 group_addr = is_multicast_ether_addr(hdr->addr1); 1085 1086 hdr->duration_id = 1087 ieee80211_duration(tx, skb, group_addr, next_len); 1088 } 1089 1090 return TX_CONTINUE; 1091 } 1092 1093 /* actual transmit path */ 1094 1095 static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx, 1096 struct sk_buff *skb, 1097 struct ieee80211_tx_info *info, 1098 struct tid_ampdu_tx *tid_tx, 1099 int tid) 1100 { 1101 bool queued = false; 1102 bool reset_agg_timer = false; 1103 struct sk_buff *purge_skb = NULL; 1104 1105 if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) { 1106 reset_agg_timer = true; 1107 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) { 1108 /* 1109 * nothing -- this aggregation session is being started 1110 * but that might still fail with the driver 1111 */ 1112 } else if (!tx->sta->sta.txq[tid]) { 1113 spin_lock(&tx->sta->lock); 1114 /* 1115 * Need to re-check now, because we may get here 1116 * 1117 * 1) in the window during which the setup is actually 1118 * already done, but not marked yet because not all 1119 * packets are spliced over to the driver pending 1120 * queue yet -- if this happened we acquire the lock 1121 * either before or after the splice happens, but 1122 * need to recheck which of these cases happened. 1123 * 1124 * 2) during session teardown, if the OPERATIONAL bit 1125 * was cleared due to the teardown but the pointer 1126 * hasn't been assigned NULL yet (or we loaded it 1127 * before it was assigned) -- in this case it may 1128 * now be NULL which means we should just let the 1129 * packet pass through because splicing the frames 1130 * back is already done. 1131 */ 1132 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid); 1133 1134 if (!tid_tx) { 1135 /* do nothing, let packet pass through */ 1136 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) { 1137 reset_agg_timer = true; 1138 } else { 1139 queued = true; 1140 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) { 1141 clear_sta_flag(tx->sta, WLAN_STA_SP); 1142 ps_dbg(tx->sta->sdata, 1143 "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n", 1144 tx->sta->sta.addr, tx->sta->sta.aid); 1145 } 1146 info->control.vif = &tx->sdata->vif; 1147 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING; 1148 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS; 1149 __skb_queue_tail(&tid_tx->pending, skb); 1150 if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER) 1151 purge_skb = __skb_dequeue(&tid_tx->pending); 1152 } 1153 spin_unlock(&tx->sta->lock); 1154 1155 if (purge_skb) 1156 ieee80211_free_txskb(&tx->local->hw, purge_skb); 1157 } 1158 1159 /* reset session timer */ 1160 if (reset_agg_timer) 1161 tid_tx->last_tx = jiffies; 1162 1163 return queued; 1164 } 1165 1166 void ieee80211_aggr_check(struct ieee80211_sub_if_data *sdata, 1167 struct sta_info *sta, struct sk_buff *skb) 1168 { 1169 struct rate_control_ref *ref = sdata->local->rate_ctrl; 1170 u16 tid; 1171 1172 if (!ref || !(ref->ops->capa & RATE_CTRL_CAPA_AMPDU_TRIGGER)) 1173 return; 1174 1175 if (!sta || 1176 (!sta->sta.valid_links && !sta->sta.deflink.ht_cap.ht_supported) || 1177 !sta->sta.wme || skb_get_queue_mapping(skb) == IEEE80211_AC_VO || 1178 skb->protocol == sdata->control_port_protocol) 1179 return; 1180 1181 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK; 1182 if (likely(sta->ampdu_mlme.tid_tx[tid])) 1183 return; 1184 1185 ieee80211_start_tx_ba_session(&sta->sta, tid, 0); 1186 } 1187 1188 /* 1189 * initialises @tx 1190 * pass %NULL for the station if unknown, a valid pointer if known 1191 * or an ERR_PTR() if the station is known not to exist 1192 */ 1193 static ieee80211_tx_result 1194 ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata, 1195 struct ieee80211_tx_data *tx, 1196 struct sta_info *sta, struct sk_buff *skb) 1197 { 1198 struct ieee80211_local *local = sdata->local; 1199 struct ieee80211_hdr *hdr; 1200 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 1201 bool aggr_check = false; 1202 int tid; 1203 1204 memset(tx, 0, sizeof(*tx)); 1205 tx->skb = skb; 1206 tx->local = local; 1207 tx->sdata = sdata; 1208 __skb_queue_head_init(&tx->skbs); 1209 1210 /* 1211 * If this flag is set to true anywhere, and we get here, 1212 * we are doing the needed processing, so remove the flag 1213 * now. 1214 */ 1215 info->control.flags &= ~IEEE80211_TX_INTCFL_NEED_TXPROCESSING; 1216 1217 hdr = (struct ieee80211_hdr *) skb->data; 1218 1219 if (likely(sta)) { 1220 if (!IS_ERR(sta)) 1221 tx->sta = sta; 1222 } else { 1223 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 1224 tx->sta = rcu_dereference(sdata->u.vlan.sta); 1225 if (!tx->sta && sdata->wdev.use_4addr) 1226 return TX_DROP; 1227 } else if (tx->sdata->control_port_protocol == tx->skb->protocol) { 1228 tx->sta = sta_info_get_bss(sdata, hdr->addr1); 1229 } 1230 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1)) { 1231 tx->sta = sta_info_get(sdata, hdr->addr1); 1232 aggr_check = true; 1233 } 1234 } 1235 1236 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) && 1237 !ieee80211_is_qos_nullfunc(hdr->frame_control) && 1238 ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) && 1239 !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) { 1240 struct tid_ampdu_tx *tid_tx; 1241 1242 tid = ieee80211_get_tid(hdr); 1243 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]); 1244 if (!tid_tx && aggr_check) { 1245 ieee80211_aggr_check(sdata, tx->sta, skb); 1246 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]); 1247 } 1248 1249 if (tid_tx) { 1250 bool queued; 1251 1252 queued = ieee80211_tx_prep_agg(tx, skb, info, 1253 tid_tx, tid); 1254 1255 if (unlikely(queued)) 1256 return TX_QUEUED; 1257 } 1258 } 1259 1260 if (is_multicast_ether_addr(hdr->addr1)) { 1261 tx->flags &= ~IEEE80211_TX_UNICAST; 1262 info->flags |= IEEE80211_TX_CTL_NO_ACK; 1263 } else 1264 tx->flags |= IEEE80211_TX_UNICAST; 1265 1266 if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) { 1267 if (!(tx->flags & IEEE80211_TX_UNICAST) || 1268 skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold || 1269 info->flags & IEEE80211_TX_CTL_AMPDU) 1270 info->flags |= IEEE80211_TX_CTL_DONTFRAG; 1271 } 1272 1273 if (!tx->sta) 1274 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT; 1275 else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) { 1276 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT; 1277 ieee80211_check_fast_xmit(tx->sta); 1278 } 1279 1280 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT; 1281 1282 return TX_CONTINUE; 1283 } 1284 1285 static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local, 1286 struct ieee80211_vif *vif, 1287 struct sta_info *sta, 1288 struct sk_buff *skb) 1289 { 1290 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 1291 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 1292 struct ieee80211_txq *txq = NULL; 1293 1294 if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) || 1295 (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE)) 1296 return NULL; 1297 1298 if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) && 1299 unlikely(!ieee80211_is_data_present(hdr->frame_control))) { 1300 if ((!ieee80211_is_mgmt(hdr->frame_control) || 1301 ieee80211_is_bufferable_mmpdu(skb) || 1302 vif->type == NL80211_IFTYPE_STATION) && 1303 sta && sta->uploaded) { 1304 /* 1305 * This will be NULL if the driver didn't set the 1306 * opt-in hardware flag. 1307 */ 1308 txq = sta->sta.txq[IEEE80211_NUM_TIDS]; 1309 } 1310 } else if (sta) { 1311 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK; 1312 1313 if (!sta->uploaded) 1314 return NULL; 1315 1316 txq = sta->sta.txq[tid]; 1317 } else { 1318 txq = vif->txq; 1319 } 1320 1321 if (!txq) 1322 return NULL; 1323 1324 return to_txq_info(txq); 1325 } 1326 1327 static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb) 1328 { 1329 struct sk_buff *next; 1330 codel_time_t now = codel_get_time(); 1331 1332 skb_list_walk_safe(skb, skb, next) 1333 IEEE80211_SKB_CB(skb)->control.enqueue_time = now; 1334 } 1335 1336 static u32 codel_skb_len_func(const struct sk_buff *skb) 1337 { 1338 return skb->len; 1339 } 1340 1341 static codel_time_t codel_skb_time_func(const struct sk_buff *skb) 1342 { 1343 const struct ieee80211_tx_info *info; 1344 1345 info = (const struct ieee80211_tx_info *)skb->cb; 1346 return info->control.enqueue_time; 1347 } 1348 1349 static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars, 1350 void *ctx) 1351 { 1352 struct ieee80211_local *local; 1353 struct txq_info *txqi; 1354 struct fq *fq; 1355 struct fq_flow *flow; 1356 1357 txqi = ctx; 1358 local = vif_to_sdata(txqi->txq.vif)->local; 1359 fq = &local->fq; 1360 1361 if (cvars == &txqi->def_cvars) 1362 flow = &txqi->tin.default_flow; 1363 else 1364 flow = &fq->flows[cvars - local->cvars]; 1365 1366 return fq_flow_dequeue(fq, flow); 1367 } 1368 1369 static void codel_drop_func(struct sk_buff *skb, 1370 void *ctx) 1371 { 1372 struct ieee80211_local *local; 1373 struct ieee80211_hw *hw; 1374 struct txq_info *txqi; 1375 1376 txqi = ctx; 1377 local = vif_to_sdata(txqi->txq.vif)->local; 1378 hw = &local->hw; 1379 1380 ieee80211_free_txskb(hw, skb); 1381 } 1382 1383 static struct sk_buff *fq_tin_dequeue_func(struct fq *fq, 1384 struct fq_tin *tin, 1385 struct fq_flow *flow) 1386 { 1387 struct ieee80211_local *local; 1388 struct txq_info *txqi; 1389 struct codel_vars *cvars; 1390 struct codel_params *cparams; 1391 struct codel_stats *cstats; 1392 1393 local = container_of(fq, struct ieee80211_local, fq); 1394 txqi = container_of(tin, struct txq_info, tin); 1395 cparams = &local->cparams; 1396 cstats = &txqi->cstats; 1397 1398 if (flow == &tin->default_flow) 1399 cvars = &txqi->def_cvars; 1400 else 1401 cvars = &local->cvars[flow - fq->flows]; 1402 1403 return codel_dequeue(txqi, 1404 &flow->backlog, 1405 cparams, 1406 cvars, 1407 cstats, 1408 codel_skb_len_func, 1409 codel_skb_time_func, 1410 codel_drop_func, 1411 codel_dequeue_func); 1412 } 1413 1414 static void fq_skb_free_func(struct fq *fq, 1415 struct fq_tin *tin, 1416 struct fq_flow *flow, 1417 struct sk_buff *skb) 1418 { 1419 struct ieee80211_local *local; 1420 1421 local = container_of(fq, struct ieee80211_local, fq); 1422 ieee80211_free_txskb(&local->hw, skb); 1423 } 1424 1425 static void ieee80211_txq_enqueue(struct ieee80211_local *local, 1426 struct txq_info *txqi, 1427 struct sk_buff *skb) 1428 { 1429 struct fq *fq = &local->fq; 1430 struct fq_tin *tin = &txqi->tin; 1431 u32 flow_idx = fq_flow_idx(fq, skb); 1432 1433 ieee80211_set_skb_enqueue_time(skb); 1434 1435 spin_lock_bh(&fq->lock); 1436 /* 1437 * For management frames, don't really apply codel etc., 1438 * we don't want to apply any shaping or anything we just 1439 * want to simplify the driver API by having them on the 1440 * txqi. 1441 */ 1442 if (unlikely(txqi->txq.tid == IEEE80211_NUM_TIDS)) { 1443 IEEE80211_SKB_CB(skb)->control.flags |= 1444 IEEE80211_TX_INTCFL_NEED_TXPROCESSING; 1445 __skb_queue_tail(&txqi->frags, skb); 1446 } else { 1447 fq_tin_enqueue(fq, tin, flow_idx, skb, 1448 fq_skb_free_func); 1449 } 1450 spin_unlock_bh(&fq->lock); 1451 } 1452 1453 static bool fq_vlan_filter_func(struct fq *fq, struct fq_tin *tin, 1454 struct fq_flow *flow, struct sk_buff *skb, 1455 void *data) 1456 { 1457 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 1458 1459 return info->control.vif == data; 1460 } 1461 1462 void ieee80211_txq_remove_vlan(struct ieee80211_local *local, 1463 struct ieee80211_sub_if_data *sdata) 1464 { 1465 struct fq *fq = &local->fq; 1466 struct txq_info *txqi; 1467 struct fq_tin *tin; 1468 struct ieee80211_sub_if_data *ap; 1469 1470 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN)) 1471 return; 1472 1473 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap); 1474 1475 if (!ap->vif.txq) 1476 return; 1477 1478 txqi = to_txq_info(ap->vif.txq); 1479 tin = &txqi->tin; 1480 1481 spin_lock_bh(&fq->lock); 1482 fq_tin_filter(fq, tin, fq_vlan_filter_func, &sdata->vif, 1483 fq_skb_free_func); 1484 spin_unlock_bh(&fq->lock); 1485 } 1486 1487 void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata, 1488 struct sta_info *sta, 1489 struct txq_info *txqi, int tid) 1490 { 1491 fq_tin_init(&txqi->tin); 1492 codel_vars_init(&txqi->def_cvars); 1493 codel_stats_init(&txqi->cstats); 1494 __skb_queue_head_init(&txqi->frags); 1495 INIT_LIST_HEAD(&txqi->schedule_order); 1496 1497 txqi->txq.vif = &sdata->vif; 1498 1499 if (!sta) { 1500 sdata->vif.txq = &txqi->txq; 1501 txqi->txq.tid = 0; 1502 txqi->txq.ac = IEEE80211_AC_BE; 1503 1504 return; 1505 } 1506 1507 if (tid == IEEE80211_NUM_TIDS) { 1508 if (sdata->vif.type == NL80211_IFTYPE_STATION) { 1509 /* Drivers need to opt in to the management MPDU TXQ */ 1510 if (!ieee80211_hw_check(&sdata->local->hw, 1511 STA_MMPDU_TXQ)) 1512 return; 1513 } else if (!ieee80211_hw_check(&sdata->local->hw, 1514 BUFF_MMPDU_TXQ)) { 1515 /* Drivers need to opt in to the bufferable MMPDU TXQ */ 1516 return; 1517 } 1518 txqi->txq.ac = IEEE80211_AC_VO; 1519 } else { 1520 txqi->txq.ac = ieee80211_ac_from_tid(tid); 1521 } 1522 1523 txqi->txq.sta = &sta->sta; 1524 txqi->txq.tid = tid; 1525 sta->sta.txq[tid] = &txqi->txq; 1526 } 1527 1528 void ieee80211_txq_purge(struct ieee80211_local *local, 1529 struct txq_info *txqi) 1530 { 1531 struct fq *fq = &local->fq; 1532 struct fq_tin *tin = &txqi->tin; 1533 1534 spin_lock_bh(&fq->lock); 1535 fq_tin_reset(fq, tin, fq_skb_free_func); 1536 ieee80211_purge_tx_queue(&local->hw, &txqi->frags); 1537 spin_unlock_bh(&fq->lock); 1538 1539 spin_lock_bh(&local->active_txq_lock[txqi->txq.ac]); 1540 list_del_init(&txqi->schedule_order); 1541 spin_unlock_bh(&local->active_txq_lock[txqi->txq.ac]); 1542 } 1543 1544 void ieee80211_txq_set_params(struct ieee80211_local *local) 1545 { 1546 if (local->hw.wiphy->txq_limit) 1547 local->fq.limit = local->hw.wiphy->txq_limit; 1548 else 1549 local->hw.wiphy->txq_limit = local->fq.limit; 1550 1551 if (local->hw.wiphy->txq_memory_limit) 1552 local->fq.memory_limit = local->hw.wiphy->txq_memory_limit; 1553 else 1554 local->hw.wiphy->txq_memory_limit = local->fq.memory_limit; 1555 1556 if (local->hw.wiphy->txq_quantum) 1557 local->fq.quantum = local->hw.wiphy->txq_quantum; 1558 else 1559 local->hw.wiphy->txq_quantum = local->fq.quantum; 1560 } 1561 1562 int ieee80211_txq_setup_flows(struct ieee80211_local *local) 1563 { 1564 struct fq *fq = &local->fq; 1565 int ret; 1566 int i; 1567 bool supp_vht = false; 1568 enum nl80211_band band; 1569 1570 ret = fq_init(fq, 4096); 1571 if (ret) 1572 return ret; 1573 1574 /* 1575 * If the hardware doesn't support VHT, it is safe to limit the maximum 1576 * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n. 1577 */ 1578 for (band = 0; band < NUM_NL80211_BANDS; band++) { 1579 struct ieee80211_supported_band *sband; 1580 1581 sband = local->hw.wiphy->bands[band]; 1582 if (!sband) 1583 continue; 1584 1585 supp_vht = supp_vht || sband->vht_cap.vht_supported; 1586 } 1587 1588 if (!supp_vht) 1589 fq->memory_limit = 4 << 20; /* 4 Mbytes */ 1590 1591 codel_params_init(&local->cparams); 1592 local->cparams.interval = MS2TIME(100); 1593 local->cparams.target = MS2TIME(20); 1594 local->cparams.ecn = true; 1595 1596 local->cvars = kvcalloc(fq->flows_cnt, sizeof(local->cvars[0]), 1597 GFP_KERNEL); 1598 if (!local->cvars) { 1599 spin_lock_bh(&fq->lock); 1600 fq_reset(fq, fq_skb_free_func); 1601 spin_unlock_bh(&fq->lock); 1602 return -ENOMEM; 1603 } 1604 1605 for (i = 0; i < fq->flows_cnt; i++) 1606 codel_vars_init(&local->cvars[i]); 1607 1608 ieee80211_txq_set_params(local); 1609 1610 return 0; 1611 } 1612 1613 void ieee80211_txq_teardown_flows(struct ieee80211_local *local) 1614 { 1615 struct fq *fq = &local->fq; 1616 1617 kvfree(local->cvars); 1618 local->cvars = NULL; 1619 1620 spin_lock_bh(&fq->lock); 1621 fq_reset(fq, fq_skb_free_func); 1622 spin_unlock_bh(&fq->lock); 1623 } 1624 1625 static bool ieee80211_queue_skb(struct ieee80211_local *local, 1626 struct ieee80211_sub_if_data *sdata, 1627 struct sta_info *sta, 1628 struct sk_buff *skb) 1629 { 1630 struct ieee80211_vif *vif; 1631 struct txq_info *txqi; 1632 1633 if (sdata->vif.type == NL80211_IFTYPE_MONITOR) 1634 return false; 1635 1636 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 1637 sdata = container_of(sdata->bss, 1638 struct ieee80211_sub_if_data, u.ap); 1639 1640 vif = &sdata->vif; 1641 txqi = ieee80211_get_txq(local, vif, sta, skb); 1642 1643 if (!txqi) 1644 return false; 1645 1646 ieee80211_txq_enqueue(local, txqi, skb); 1647 1648 schedule_and_wake_txq(local, txqi); 1649 1650 return true; 1651 } 1652 1653 static bool ieee80211_tx_frags(struct ieee80211_local *local, 1654 struct ieee80211_vif *vif, 1655 struct sta_info *sta, 1656 struct sk_buff_head *skbs, 1657 bool txpending) 1658 { 1659 struct ieee80211_tx_control control = {}; 1660 struct sk_buff *skb, *tmp; 1661 unsigned long flags; 1662 1663 skb_queue_walk_safe(skbs, skb, tmp) { 1664 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 1665 int q = info->hw_queue; 1666 1667 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 1668 if (WARN_ON_ONCE(q >= local->hw.queues)) { 1669 __skb_unlink(skb, skbs); 1670 ieee80211_free_txskb(&local->hw, skb); 1671 continue; 1672 } 1673 #endif 1674 1675 spin_lock_irqsave(&local->queue_stop_reason_lock, flags); 1676 if (local->queue_stop_reasons[q] || 1677 (!txpending && !skb_queue_empty(&local->pending[q]))) { 1678 if (unlikely(info->flags & 1679 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) { 1680 if (local->queue_stop_reasons[q] & 1681 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) { 1682 /* 1683 * Drop off-channel frames if queues 1684 * are stopped for any reason other 1685 * than off-channel operation. Never 1686 * queue them. 1687 */ 1688 spin_unlock_irqrestore( 1689 &local->queue_stop_reason_lock, 1690 flags); 1691 ieee80211_purge_tx_queue(&local->hw, 1692 skbs); 1693 return true; 1694 } 1695 } else { 1696 1697 /* 1698 * Since queue is stopped, queue up frames for 1699 * later transmission from the tx-pending 1700 * tasklet when the queue is woken again. 1701 */ 1702 if (txpending) 1703 skb_queue_splice_init(skbs, 1704 &local->pending[q]); 1705 else 1706 skb_queue_splice_tail_init(skbs, 1707 &local->pending[q]); 1708 1709 spin_unlock_irqrestore(&local->queue_stop_reason_lock, 1710 flags); 1711 return false; 1712 } 1713 } 1714 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 1715 1716 info->control.vif = vif; 1717 control.sta = sta ? &sta->sta : NULL; 1718 1719 __skb_unlink(skb, skbs); 1720 drv_tx(local, &control, skb); 1721 } 1722 1723 return true; 1724 } 1725 1726 /* 1727 * Returns false if the frame couldn't be transmitted but was queued instead. 1728 */ 1729 static bool __ieee80211_tx(struct ieee80211_local *local, 1730 struct sk_buff_head *skbs, struct sta_info *sta, 1731 bool txpending) 1732 { 1733 struct ieee80211_tx_info *info; 1734 struct ieee80211_sub_if_data *sdata; 1735 struct ieee80211_vif *vif; 1736 struct sk_buff *skb; 1737 bool result; 1738 1739 if (WARN_ON(skb_queue_empty(skbs))) 1740 return true; 1741 1742 skb = skb_peek(skbs); 1743 info = IEEE80211_SKB_CB(skb); 1744 sdata = vif_to_sdata(info->control.vif); 1745 if (sta && !sta->uploaded) 1746 sta = NULL; 1747 1748 switch (sdata->vif.type) { 1749 case NL80211_IFTYPE_MONITOR: 1750 if ((sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) || 1751 ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR)) { 1752 vif = &sdata->vif; 1753 break; 1754 } 1755 sdata = rcu_dereference(local->monitor_sdata); 1756 if (sdata && ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF)) { 1757 vif = &sdata->vif; 1758 info->hw_queue = 1759 vif->hw_queue[skb_get_queue_mapping(skb)]; 1760 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) { 1761 ieee80211_purge_tx_queue(&local->hw, skbs); 1762 return true; 1763 } else 1764 vif = NULL; 1765 break; 1766 case NL80211_IFTYPE_AP_VLAN: 1767 sdata = container_of(sdata->bss, 1768 struct ieee80211_sub_if_data, u.ap); 1769 fallthrough; 1770 default: 1771 vif = &sdata->vif; 1772 break; 1773 } 1774 1775 result = ieee80211_tx_frags(local, vif, sta, skbs, txpending); 1776 1777 WARN_ON_ONCE(!skb_queue_empty(skbs)); 1778 1779 return result; 1780 } 1781 1782 /* 1783 * Invoke TX handlers, return 0 on success and non-zero if the 1784 * frame was dropped or queued. 1785 * 1786 * The handlers are split into an early and late part. The latter is everything 1787 * that can be sensitive to reordering, and will be deferred to after packets 1788 * are dequeued from the intermediate queues (when they are enabled). 1789 */ 1790 static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx) 1791 { 1792 ieee80211_tx_result res = TX_DROP; 1793 1794 #define CALL_TXH(txh) \ 1795 do { \ 1796 res = txh(tx); \ 1797 if (res != TX_CONTINUE) \ 1798 goto txh_done; \ 1799 } while (0) 1800 1801 CALL_TXH(ieee80211_tx_h_dynamic_ps); 1802 CALL_TXH(ieee80211_tx_h_check_assoc); 1803 CALL_TXH(ieee80211_tx_h_ps_buf); 1804 CALL_TXH(ieee80211_tx_h_check_control_port_protocol); 1805 CALL_TXH(ieee80211_tx_h_select_key); 1806 1807 txh_done: 1808 if (unlikely(res == TX_DROP)) { 1809 I802_DEBUG_INC(tx->local->tx_handlers_drop); 1810 if (tx->skb) 1811 ieee80211_free_txskb(&tx->local->hw, tx->skb); 1812 else 1813 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs); 1814 return -1; 1815 } else if (unlikely(res == TX_QUEUED)) { 1816 I802_DEBUG_INC(tx->local->tx_handlers_queued); 1817 return -1; 1818 } 1819 1820 return 0; 1821 } 1822 1823 /* 1824 * Late handlers can be called while the sta lock is held. Handlers that can 1825 * cause packets to be generated will cause deadlock! 1826 */ 1827 static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx) 1828 { 1829 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb); 1830 ieee80211_tx_result res = TX_CONTINUE; 1831 1832 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL)) 1833 CALL_TXH(ieee80211_tx_h_rate_ctrl); 1834 1835 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) { 1836 __skb_queue_tail(&tx->skbs, tx->skb); 1837 tx->skb = NULL; 1838 goto txh_done; 1839 } 1840 1841 CALL_TXH(ieee80211_tx_h_michael_mic_add); 1842 CALL_TXH(ieee80211_tx_h_sequence); 1843 CALL_TXH(ieee80211_tx_h_fragment); 1844 /* handlers after fragment must be aware of tx info fragmentation! */ 1845 CALL_TXH(ieee80211_tx_h_stats); 1846 CALL_TXH(ieee80211_tx_h_encrypt); 1847 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL)) 1848 CALL_TXH(ieee80211_tx_h_calculate_duration); 1849 #undef CALL_TXH 1850 1851 txh_done: 1852 if (unlikely(res == TX_DROP)) { 1853 I802_DEBUG_INC(tx->local->tx_handlers_drop); 1854 if (tx->skb) 1855 ieee80211_free_txskb(&tx->local->hw, tx->skb); 1856 else 1857 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs); 1858 return -1; 1859 } else if (unlikely(res == TX_QUEUED)) { 1860 I802_DEBUG_INC(tx->local->tx_handlers_queued); 1861 return -1; 1862 } 1863 1864 return 0; 1865 } 1866 1867 static int invoke_tx_handlers(struct ieee80211_tx_data *tx) 1868 { 1869 int r = invoke_tx_handlers_early(tx); 1870 1871 if (r) 1872 return r; 1873 return invoke_tx_handlers_late(tx); 1874 } 1875 1876 bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw, 1877 struct ieee80211_vif *vif, struct sk_buff *skb, 1878 int band, struct ieee80211_sta **sta) 1879 { 1880 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 1881 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 1882 struct ieee80211_tx_data tx; 1883 struct sk_buff *skb2; 1884 1885 if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP) 1886 return false; 1887 1888 info->band = band; 1889 info->control.vif = vif; 1890 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)]; 1891 1892 if (invoke_tx_handlers(&tx)) 1893 return false; 1894 1895 if (sta) { 1896 if (tx.sta) 1897 *sta = &tx.sta->sta; 1898 else 1899 *sta = NULL; 1900 } 1901 1902 /* this function isn't suitable for fragmented data frames */ 1903 skb2 = __skb_dequeue(&tx.skbs); 1904 if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) { 1905 ieee80211_free_txskb(hw, skb2); 1906 ieee80211_purge_tx_queue(hw, &tx.skbs); 1907 return false; 1908 } 1909 1910 return true; 1911 } 1912 EXPORT_SYMBOL(ieee80211_tx_prepare_skb); 1913 1914 /* 1915 * Returns false if the frame couldn't be transmitted but was queued instead. 1916 */ 1917 static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata, 1918 struct sta_info *sta, struct sk_buff *skb, 1919 bool txpending) 1920 { 1921 struct ieee80211_local *local = sdata->local; 1922 struct ieee80211_tx_data tx; 1923 ieee80211_tx_result res_prepare; 1924 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 1925 bool result = true; 1926 1927 if (unlikely(skb->len < 10)) { 1928 dev_kfree_skb(skb); 1929 return true; 1930 } 1931 1932 /* initialises tx */ 1933 res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb); 1934 1935 if (unlikely(res_prepare == TX_DROP)) { 1936 ieee80211_free_txskb(&local->hw, skb); 1937 return true; 1938 } else if (unlikely(res_prepare == TX_QUEUED)) { 1939 return true; 1940 } 1941 1942 /* set up hw_queue value early */ 1943 if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) || 1944 !ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) 1945 info->hw_queue = 1946 sdata->vif.hw_queue[skb_get_queue_mapping(skb)]; 1947 1948 if (invoke_tx_handlers_early(&tx)) 1949 return true; 1950 1951 if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb)) 1952 return true; 1953 1954 if (!invoke_tx_handlers_late(&tx)) 1955 result = __ieee80211_tx(local, &tx.skbs, tx.sta, txpending); 1956 1957 return result; 1958 } 1959 1960 /* device xmit handlers */ 1961 1962 enum ieee80211_encrypt { 1963 ENCRYPT_NO, 1964 ENCRYPT_MGMT, 1965 ENCRYPT_DATA, 1966 }; 1967 1968 static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata, 1969 struct sk_buff *skb, 1970 int head_need, 1971 enum ieee80211_encrypt encrypt) 1972 { 1973 struct ieee80211_local *local = sdata->local; 1974 bool enc_tailroom; 1975 int tail_need = 0; 1976 1977 enc_tailroom = encrypt == ENCRYPT_MGMT || 1978 (encrypt == ENCRYPT_DATA && 1979 sdata->crypto_tx_tailroom_needed_cnt); 1980 1981 if (enc_tailroom) { 1982 tail_need = IEEE80211_ENCRYPT_TAILROOM; 1983 tail_need -= skb_tailroom(skb); 1984 tail_need = max_t(int, tail_need, 0); 1985 } 1986 1987 if (skb_cloned(skb) && 1988 (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) || 1989 !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom)) 1990 I802_DEBUG_INC(local->tx_expand_skb_head_cloned); 1991 else if (head_need || tail_need) 1992 I802_DEBUG_INC(local->tx_expand_skb_head); 1993 else 1994 return 0; 1995 1996 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) { 1997 wiphy_debug(local->hw.wiphy, 1998 "failed to reallocate TX buffer\n"); 1999 return -ENOMEM; 2000 } 2001 2002 return 0; 2003 } 2004 2005 void ieee80211_xmit(struct ieee80211_sub_if_data *sdata, 2006 struct sta_info *sta, struct sk_buff *skb) 2007 { 2008 struct ieee80211_local *local = sdata->local; 2009 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 2010 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 2011 int headroom; 2012 enum ieee80211_encrypt encrypt; 2013 2014 if (info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT) 2015 encrypt = ENCRYPT_NO; 2016 else if (ieee80211_is_mgmt(hdr->frame_control)) 2017 encrypt = ENCRYPT_MGMT; 2018 else 2019 encrypt = ENCRYPT_DATA; 2020 2021 headroom = local->tx_headroom; 2022 if (encrypt != ENCRYPT_NO) 2023 headroom += IEEE80211_ENCRYPT_HEADROOM; 2024 headroom -= skb_headroom(skb); 2025 headroom = max_t(int, 0, headroom); 2026 2027 if (ieee80211_skb_resize(sdata, skb, headroom, encrypt)) { 2028 ieee80211_free_txskb(&local->hw, skb); 2029 return; 2030 } 2031 2032 /* reload after potential resize */ 2033 hdr = (struct ieee80211_hdr *) skb->data; 2034 info->control.vif = &sdata->vif; 2035 2036 if (ieee80211_vif_is_mesh(&sdata->vif)) { 2037 if (ieee80211_is_data(hdr->frame_control) && 2038 is_unicast_ether_addr(hdr->addr1)) { 2039 if (mesh_nexthop_resolve(sdata, skb)) 2040 return; /* skb queued: don't free */ 2041 } else { 2042 ieee80211_mps_set_frame_flags(sdata, NULL, hdr); 2043 } 2044 } 2045 2046 ieee80211_set_qos_hdr(sdata, skb); 2047 ieee80211_tx(sdata, sta, skb, false); 2048 } 2049 2050 static bool ieee80211_validate_radiotap_len(struct sk_buff *skb) 2051 { 2052 struct ieee80211_radiotap_header *rthdr = 2053 (struct ieee80211_radiotap_header *)skb->data; 2054 2055 /* check for not even having the fixed radiotap header part */ 2056 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header))) 2057 return false; /* too short to be possibly valid */ 2058 2059 /* is it a header version we can trust to find length from? */ 2060 if (unlikely(rthdr->it_version)) 2061 return false; /* only version 0 is supported */ 2062 2063 /* does the skb contain enough to deliver on the alleged length? */ 2064 if (unlikely(skb->len < ieee80211_get_radiotap_len(skb->data))) 2065 return false; /* skb too short for claimed rt header extent */ 2066 2067 return true; 2068 } 2069 2070 bool ieee80211_parse_tx_radiotap(struct sk_buff *skb, 2071 struct net_device *dev) 2072 { 2073 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); 2074 struct ieee80211_radiotap_iterator iterator; 2075 struct ieee80211_radiotap_header *rthdr = 2076 (struct ieee80211_radiotap_header *) skb->data; 2077 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 2078 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len, 2079 NULL); 2080 u16 txflags; 2081 u16 rate = 0; 2082 bool rate_found = false; 2083 u8 rate_retries = 0; 2084 u16 rate_flags = 0; 2085 u8 mcs_known, mcs_flags, mcs_bw; 2086 u16 vht_known; 2087 u8 vht_mcs = 0, vht_nss = 0; 2088 int i; 2089 2090 if (!ieee80211_validate_radiotap_len(skb)) 2091 return false; 2092 2093 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT | 2094 IEEE80211_TX_CTL_DONTFRAG; 2095 2096 /* 2097 * for every radiotap entry that is present 2098 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more 2099 * entries present, or -EINVAL on error) 2100 */ 2101 2102 while (!ret) { 2103 ret = ieee80211_radiotap_iterator_next(&iterator); 2104 2105 if (ret) 2106 continue; 2107 2108 /* see if this argument is something we can use */ 2109 switch (iterator.this_arg_index) { 2110 /* 2111 * You must take care when dereferencing iterator.this_arg 2112 * for multibyte types... the pointer is not aligned. Use 2113 * get_unaligned((type *)iterator.this_arg) to dereference 2114 * iterator.this_arg for type "type" safely on all arches. 2115 */ 2116 case IEEE80211_RADIOTAP_FLAGS: 2117 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) { 2118 /* 2119 * this indicates that the skb we have been 2120 * handed has the 32-bit FCS CRC at the end... 2121 * we should react to that by snipping it off 2122 * because it will be recomputed and added 2123 * on transmission 2124 */ 2125 if (skb->len < (iterator._max_length + FCS_LEN)) 2126 return false; 2127 2128 skb_trim(skb, skb->len - FCS_LEN); 2129 } 2130 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP) 2131 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT; 2132 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG) 2133 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG; 2134 break; 2135 2136 case IEEE80211_RADIOTAP_TX_FLAGS: 2137 txflags = get_unaligned_le16(iterator.this_arg); 2138 if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK) 2139 info->flags |= IEEE80211_TX_CTL_NO_ACK; 2140 if (txflags & IEEE80211_RADIOTAP_F_TX_NOSEQNO) 2141 info->control.flags |= IEEE80211_TX_CTRL_NO_SEQNO; 2142 if (txflags & IEEE80211_RADIOTAP_F_TX_ORDER) 2143 info->control.flags |= 2144 IEEE80211_TX_CTRL_DONT_REORDER; 2145 break; 2146 2147 case IEEE80211_RADIOTAP_RATE: 2148 rate = *iterator.this_arg; 2149 rate_flags = 0; 2150 rate_found = true; 2151 break; 2152 2153 case IEEE80211_RADIOTAP_ANTENNA: 2154 /* this can appear multiple times, keep a bitmap */ 2155 info->control.antennas |= BIT(*iterator.this_arg); 2156 break; 2157 2158 case IEEE80211_RADIOTAP_DATA_RETRIES: 2159 rate_retries = *iterator.this_arg; 2160 break; 2161 2162 case IEEE80211_RADIOTAP_MCS: 2163 mcs_known = iterator.this_arg[0]; 2164 mcs_flags = iterator.this_arg[1]; 2165 if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS)) 2166 break; 2167 2168 rate_found = true; 2169 rate = iterator.this_arg[2]; 2170 rate_flags = IEEE80211_TX_RC_MCS; 2171 2172 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI && 2173 mcs_flags & IEEE80211_RADIOTAP_MCS_SGI) 2174 rate_flags |= IEEE80211_TX_RC_SHORT_GI; 2175 2176 mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK; 2177 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW && 2178 mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40) 2179 rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH; 2180 2181 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_FEC && 2182 mcs_flags & IEEE80211_RADIOTAP_MCS_FEC_LDPC) 2183 info->flags |= IEEE80211_TX_CTL_LDPC; 2184 2185 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_STBC) { 2186 u8 stbc = u8_get_bits(mcs_flags, 2187 IEEE80211_RADIOTAP_MCS_STBC_MASK); 2188 2189 info->flags |= 2190 u32_encode_bits(stbc, 2191 IEEE80211_TX_CTL_STBC); 2192 } 2193 break; 2194 2195 case IEEE80211_RADIOTAP_VHT: 2196 vht_known = get_unaligned_le16(iterator.this_arg); 2197 rate_found = true; 2198 2199 rate_flags = IEEE80211_TX_RC_VHT_MCS; 2200 if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) && 2201 (iterator.this_arg[2] & 2202 IEEE80211_RADIOTAP_VHT_FLAG_SGI)) 2203 rate_flags |= IEEE80211_TX_RC_SHORT_GI; 2204 if (vht_known & 2205 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) { 2206 if (iterator.this_arg[3] == 1) 2207 rate_flags |= 2208 IEEE80211_TX_RC_40_MHZ_WIDTH; 2209 else if (iterator.this_arg[3] == 4) 2210 rate_flags |= 2211 IEEE80211_TX_RC_80_MHZ_WIDTH; 2212 else if (iterator.this_arg[3] == 11) 2213 rate_flags |= 2214 IEEE80211_TX_RC_160_MHZ_WIDTH; 2215 } 2216 2217 vht_mcs = iterator.this_arg[4] >> 4; 2218 if (vht_mcs > 11) 2219 vht_mcs = 0; 2220 vht_nss = iterator.this_arg[4] & 0xF; 2221 if (!vht_nss || vht_nss > 8) 2222 vht_nss = 1; 2223 break; 2224 2225 /* 2226 * Please update the file 2227 * Documentation/networking/mac80211-injection.rst 2228 * when parsing new fields here. 2229 */ 2230 2231 default: 2232 break; 2233 } 2234 } 2235 2236 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */ 2237 return false; 2238 2239 if (rate_found) { 2240 struct ieee80211_supported_band *sband = 2241 local->hw.wiphy->bands[info->band]; 2242 2243 info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT; 2244 2245 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) { 2246 info->control.rates[i].idx = -1; 2247 info->control.rates[i].flags = 0; 2248 info->control.rates[i].count = 0; 2249 } 2250 2251 if (rate_flags & IEEE80211_TX_RC_MCS) { 2252 /* reset antennas if not enough */ 2253 if (IEEE80211_HT_MCS_CHAINS(rate) > 2254 hweight8(info->control.antennas)) 2255 info->control.antennas = 0; 2256 2257 info->control.rates[0].idx = rate; 2258 } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) { 2259 /* reset antennas if not enough */ 2260 if (vht_nss > hweight8(info->control.antennas)) 2261 info->control.antennas = 0; 2262 2263 ieee80211_rate_set_vht(info->control.rates, vht_mcs, 2264 vht_nss); 2265 } else if (sband) { 2266 for (i = 0; i < sband->n_bitrates; i++) { 2267 if (rate * 5 != sband->bitrates[i].bitrate) 2268 continue; 2269 2270 info->control.rates[0].idx = i; 2271 break; 2272 } 2273 } 2274 2275 if (info->control.rates[0].idx < 0) 2276 info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT; 2277 2278 info->control.rates[0].flags = rate_flags; 2279 info->control.rates[0].count = min_t(u8, rate_retries + 1, 2280 local->hw.max_rate_tries); 2281 } 2282 2283 return true; 2284 } 2285 2286 netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb, 2287 struct net_device *dev) 2288 { 2289 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); 2290 struct ieee80211_chanctx_conf *chanctx_conf; 2291 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 2292 struct ieee80211_hdr *hdr; 2293 struct ieee80211_sub_if_data *tmp_sdata, *sdata; 2294 struct cfg80211_chan_def *chandef; 2295 u16 len_rthdr; 2296 int hdrlen; 2297 2298 sdata = IEEE80211_DEV_TO_SUB_IF(dev); 2299 if (unlikely(!ieee80211_sdata_running(sdata))) 2300 goto fail; 2301 2302 memset(info, 0, sizeof(*info)); 2303 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS | 2304 IEEE80211_TX_CTL_INJECTED; 2305 2306 /* Sanity-check the length of the radiotap header */ 2307 if (!ieee80211_validate_radiotap_len(skb)) 2308 goto fail; 2309 2310 /* we now know there is a radiotap header with a length we can use */ 2311 len_rthdr = ieee80211_get_radiotap_len(skb->data); 2312 2313 /* 2314 * fix up the pointers accounting for the radiotap 2315 * header still being in there. We are being given 2316 * a precooked IEEE80211 header so no need for 2317 * normal processing 2318 */ 2319 skb_set_mac_header(skb, len_rthdr); 2320 /* 2321 * these are just fixed to the end of the rt area since we 2322 * don't have any better information and at this point, nobody cares 2323 */ 2324 skb_set_network_header(skb, len_rthdr); 2325 skb_set_transport_header(skb, len_rthdr); 2326 2327 if (skb->len < len_rthdr + 2) 2328 goto fail; 2329 2330 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr); 2331 hdrlen = ieee80211_hdrlen(hdr->frame_control); 2332 2333 if (skb->len < len_rthdr + hdrlen) 2334 goto fail; 2335 2336 /* 2337 * Initialize skb->protocol if the injected frame is a data frame 2338 * carrying a rfc1042 header 2339 */ 2340 if (ieee80211_is_data(hdr->frame_control) && 2341 skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) { 2342 u8 *payload = (u8 *)hdr + hdrlen; 2343 2344 if (ether_addr_equal(payload, rfc1042_header)) 2345 skb->protocol = cpu_to_be16((payload[6] << 8) | 2346 payload[7]); 2347 } 2348 2349 rcu_read_lock(); 2350 2351 /* 2352 * We process outgoing injected frames that have a local address 2353 * we handle as though they are non-injected frames. 2354 * This code here isn't entirely correct, the local MAC address 2355 * isn't always enough to find the interface to use; for proper 2356 * VLAN support we have an nl80211-based mechanism. 2357 * 2358 * This is necessary, for example, for old hostapd versions that 2359 * don't use nl80211-based management TX/RX. 2360 */ 2361 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) { 2362 if (!ieee80211_sdata_running(tmp_sdata)) 2363 continue; 2364 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR || 2365 tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 2366 continue; 2367 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) { 2368 sdata = tmp_sdata; 2369 break; 2370 } 2371 } 2372 2373 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf); 2374 if (!chanctx_conf) { 2375 tmp_sdata = rcu_dereference(local->monitor_sdata); 2376 if (tmp_sdata) 2377 chanctx_conf = 2378 rcu_dereference(tmp_sdata->vif.bss_conf.chanctx_conf); 2379 } 2380 2381 if (chanctx_conf) 2382 chandef = &chanctx_conf->def; 2383 else 2384 goto fail_rcu; 2385 2386 /* 2387 * If driver/HW supports IEEE80211_CHAN_CAN_MONITOR we still 2388 * shouldn't transmit on disabled channels. 2389 */ 2390 if (!cfg80211_chandef_usable(local->hw.wiphy, chandef, 2391 IEEE80211_CHAN_DISABLED)) 2392 goto fail_rcu; 2393 2394 /* 2395 * Frame injection is not allowed if beaconing is not allowed 2396 * or if we need radar detection. Beaconing is usually not allowed when 2397 * the mode or operation (Adhoc, AP, Mesh) does not support DFS. 2398 * Passive scan is also used in world regulatory domains where 2399 * your country is not known and as such it should be treated as 2400 * NO TX unless the channel is explicitly allowed in which case 2401 * your current regulatory domain would not have the passive scan 2402 * flag. 2403 * 2404 * Since AP mode uses monitor interfaces to inject/TX management 2405 * frames we can make AP mode the exception to this rule once it 2406 * supports radar detection as its implementation can deal with 2407 * radar detection by itself. We can do that later by adding a 2408 * monitor flag interfaces used for AP support. 2409 */ 2410 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef, 2411 sdata->vif.type)) 2412 goto fail_rcu; 2413 2414 info->band = chandef->chan->band; 2415 2416 /* Initialize skb->priority according to frame type and TID class, 2417 * with respect to the sub interface that the frame will actually 2418 * be transmitted on. If the DONT_REORDER flag is set, the original 2419 * skb-priority is preserved to assure frames injected with this 2420 * flag are not reordered relative to each other. 2421 */ 2422 ieee80211_select_queue_80211(sdata, skb, hdr); 2423 skb_set_queue_mapping(skb, ieee80211_ac_from_tid(skb->priority)); 2424 2425 /* 2426 * Process the radiotap header. This will now take into account the 2427 * selected chandef above to accurately set injection rates and 2428 * retransmissions. 2429 */ 2430 if (!ieee80211_parse_tx_radiotap(skb, dev)) 2431 goto fail_rcu; 2432 2433 /* remove the injection radiotap header */ 2434 skb_pull(skb, len_rthdr); 2435 2436 ieee80211_xmit(sdata, NULL, skb); 2437 rcu_read_unlock(); 2438 2439 return NETDEV_TX_OK; 2440 2441 fail_rcu: 2442 rcu_read_unlock(); 2443 fail: 2444 dev_kfree_skb(skb); 2445 return NETDEV_TX_OK; /* meaning, we dealt with the skb */ 2446 } 2447 2448 static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb) 2449 { 2450 u16 ethertype = (skb->data[12] << 8) | skb->data[13]; 2451 2452 return ethertype == ETH_P_TDLS && 2453 skb->len > 14 && 2454 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE; 2455 } 2456 2457 int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata, 2458 struct sk_buff *skb, 2459 struct sta_info **sta_out) 2460 { 2461 struct sta_info *sta; 2462 2463 switch (sdata->vif.type) { 2464 case NL80211_IFTYPE_AP_VLAN: 2465 sta = rcu_dereference(sdata->u.vlan.sta); 2466 if (sta) { 2467 *sta_out = sta; 2468 return 0; 2469 } else if (sdata->wdev.use_4addr) { 2470 return -ENOLINK; 2471 } 2472 fallthrough; 2473 case NL80211_IFTYPE_AP: 2474 case NL80211_IFTYPE_OCB: 2475 case NL80211_IFTYPE_ADHOC: 2476 if (is_multicast_ether_addr(skb->data)) { 2477 *sta_out = ERR_PTR(-ENOENT); 2478 return 0; 2479 } 2480 sta = sta_info_get_bss(sdata, skb->data); 2481 break; 2482 #ifdef CONFIG_MAC80211_MESH 2483 case NL80211_IFTYPE_MESH_POINT: 2484 /* determined much later */ 2485 *sta_out = NULL; 2486 return 0; 2487 #endif 2488 case NL80211_IFTYPE_STATION: 2489 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) { 2490 sta = sta_info_get(sdata, skb->data); 2491 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) { 2492 if (test_sta_flag(sta, 2493 WLAN_STA_TDLS_PEER_AUTH)) { 2494 *sta_out = sta; 2495 return 0; 2496 } 2497 2498 /* 2499 * TDLS link during setup - throw out frames to 2500 * peer. Allow TDLS-setup frames to unauthorized 2501 * peers for the special case of a link teardown 2502 * after a TDLS sta is removed due to being 2503 * unreachable. 2504 */ 2505 if (!ieee80211_is_tdls_setup(skb)) 2506 return -EINVAL; 2507 } 2508 2509 } 2510 2511 sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr); 2512 if (!sta) 2513 return -ENOLINK; 2514 break; 2515 default: 2516 return -EINVAL; 2517 } 2518 2519 *sta_out = sta ?: ERR_PTR(-ENOENT); 2520 return 0; 2521 } 2522 2523 static u16 ieee80211_store_ack_skb(struct ieee80211_local *local, 2524 struct sk_buff *skb, 2525 u32 *info_flags, 2526 u64 *cookie) 2527 { 2528 struct sk_buff *ack_skb; 2529 u16 info_id = 0; 2530 2531 if (skb->sk) 2532 ack_skb = skb_clone_sk(skb); 2533 else 2534 ack_skb = skb_clone(skb, GFP_ATOMIC); 2535 2536 if (ack_skb) { 2537 unsigned long flags; 2538 int id; 2539 2540 spin_lock_irqsave(&local->ack_status_lock, flags); 2541 id = idr_alloc(&local->ack_status_frames, ack_skb, 2542 1, 0x2000, GFP_ATOMIC); 2543 spin_unlock_irqrestore(&local->ack_status_lock, flags); 2544 2545 if (id >= 0) { 2546 info_id = id; 2547 *info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS; 2548 if (cookie) { 2549 *cookie = ieee80211_mgmt_tx_cookie(local); 2550 IEEE80211_SKB_CB(ack_skb)->ack.cookie = *cookie; 2551 } 2552 } else { 2553 kfree_skb(ack_skb); 2554 } 2555 } 2556 2557 return info_id; 2558 } 2559 2560 /** 2561 * ieee80211_build_hdr - build 802.11 header in the given frame 2562 * @sdata: virtual interface to build the header for 2563 * @skb: the skb to build the header in 2564 * @info_flags: skb flags to set 2565 * @sta: the station pointer 2566 * @ctrl_flags: info control flags to set 2567 * @cookie: cookie pointer to fill (if not %NULL) 2568 * 2569 * This function takes the skb with 802.3 header and reformats the header to 2570 * the appropriate IEEE 802.11 header based on which interface the packet is 2571 * being transmitted on. 2572 * 2573 * Note that this function also takes care of the TX status request and 2574 * potential unsharing of the SKB - this needs to be interleaved with the 2575 * header building. 2576 * 2577 * The function requires the read-side RCU lock held 2578 * 2579 * Returns: the (possibly reallocated) skb or an ERR_PTR() code 2580 */ 2581 static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata, 2582 struct sk_buff *skb, u32 info_flags, 2583 struct sta_info *sta, u32 ctrl_flags, 2584 u64 *cookie) 2585 { 2586 struct ieee80211_local *local = sdata->local; 2587 struct ieee80211_tx_info *info; 2588 int head_need; 2589 u16 ethertype, hdrlen, meshhdrlen = 0; 2590 __le16 fc; 2591 struct ieee80211_hdr hdr; 2592 struct ieee80211s_hdr mesh_hdr __maybe_unused; 2593 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL; 2594 const u8 *encaps_data; 2595 int encaps_len, skip_header_bytes; 2596 bool wme_sta = false, authorized = false; 2597 bool tdls_peer; 2598 bool multicast; 2599 u16 info_id = 0; 2600 struct ieee80211_chanctx_conf *chanctx_conf = NULL; 2601 enum nl80211_band band; 2602 int ret; 2603 u8 link_id = u32_get_bits(ctrl_flags, IEEE80211_TX_CTRL_MLO_LINK); 2604 2605 if (IS_ERR(sta)) 2606 sta = NULL; 2607 2608 #ifdef CONFIG_MAC80211_DEBUGFS 2609 if (local->force_tx_status) 2610 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS; 2611 #endif 2612 2613 /* convert Ethernet header to proper 802.11 header (based on 2614 * operation mode) */ 2615 ethertype = (skb->data[12] << 8) | skb->data[13]; 2616 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA); 2617 2618 if (!ieee80211_vif_is_mld(&sdata->vif)) 2619 chanctx_conf = 2620 rcu_dereference(sdata->vif.bss_conf.chanctx_conf); 2621 2622 switch (sdata->vif.type) { 2623 case NL80211_IFTYPE_AP_VLAN: 2624 if (sdata->wdev.use_4addr) { 2625 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS); 2626 /* RA TA DA SA */ 2627 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN); 2628 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN); 2629 memcpy(hdr.addr3, skb->data, ETH_ALEN); 2630 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN); 2631 hdrlen = 30; 2632 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED); 2633 wme_sta = sta->sta.wme; 2634 } 2635 if (!ieee80211_vif_is_mld(&sdata->vif)) { 2636 struct ieee80211_sub_if_data *ap_sdata; 2637 2638 /* override chanctx_conf from AP (we don't have one) */ 2639 ap_sdata = container_of(sdata->bss, 2640 struct ieee80211_sub_if_data, 2641 u.ap); 2642 chanctx_conf = 2643 rcu_dereference(ap_sdata->vif.bss_conf.chanctx_conf); 2644 } 2645 if (sdata->wdev.use_4addr) 2646 break; 2647 fallthrough; 2648 case NL80211_IFTYPE_AP: 2649 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS); 2650 /* DA BSSID SA */ 2651 memcpy(hdr.addr1, skb->data, ETH_ALEN); 2652 2653 if (ieee80211_vif_is_mld(&sdata->vif) && sta && !sta->sta.mlo) { 2654 struct ieee80211_link_data *link; 2655 2656 link_id = sta->deflink.link_id; 2657 link = rcu_dereference(sdata->link[link_id]); 2658 if (WARN_ON(!link)) { 2659 ret = -ENOLINK; 2660 goto free; 2661 } 2662 memcpy(hdr.addr2, link->conf->addr, ETH_ALEN); 2663 } else if (link_id == IEEE80211_LINK_UNSPECIFIED || 2664 (sta && sta->sta.mlo)) { 2665 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN); 2666 } else { 2667 struct ieee80211_bss_conf *conf; 2668 2669 conf = rcu_dereference(sdata->vif.link_conf[link_id]); 2670 if (unlikely(!conf)) { 2671 ret = -ENOLINK; 2672 goto free; 2673 } 2674 2675 memcpy(hdr.addr2, conf->addr, ETH_ALEN); 2676 } 2677 2678 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN); 2679 hdrlen = 24; 2680 break; 2681 #ifdef CONFIG_MAC80211_MESH 2682 case NL80211_IFTYPE_MESH_POINT: 2683 if (!is_multicast_ether_addr(skb->data)) { 2684 struct sta_info *next_hop; 2685 bool mpp_lookup = true; 2686 2687 mpath = mesh_path_lookup(sdata, skb->data); 2688 if (mpath) { 2689 mpp_lookup = false; 2690 next_hop = rcu_dereference(mpath->next_hop); 2691 if (!next_hop || 2692 !(mpath->flags & (MESH_PATH_ACTIVE | 2693 MESH_PATH_RESOLVING))) 2694 mpp_lookup = true; 2695 } 2696 2697 if (mpp_lookup) { 2698 mppath = mpp_path_lookup(sdata, skb->data); 2699 if (mppath) 2700 mppath->exp_time = jiffies; 2701 } 2702 2703 if (mppath && mpath) 2704 mesh_path_del(sdata, mpath->dst); 2705 } 2706 2707 /* 2708 * Use address extension if it is a packet from 2709 * another interface or if we know the destination 2710 * is being proxied by a portal (i.e. portal address 2711 * differs from proxied address) 2712 */ 2713 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) && 2714 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) { 2715 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc, 2716 skb->data, skb->data + ETH_ALEN); 2717 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr, 2718 NULL, NULL); 2719 } else { 2720 /* DS -> MBSS (802.11-2012 13.11.3.3). 2721 * For unicast with unknown forwarding information, 2722 * destination might be in the MBSS or if that fails 2723 * forwarded to another mesh gate. In either case 2724 * resolution will be handled in ieee80211_xmit(), so 2725 * leave the original DA. This also works for mcast */ 2726 const u8 *mesh_da = skb->data; 2727 2728 if (mppath) 2729 mesh_da = mppath->mpp; 2730 else if (mpath) 2731 mesh_da = mpath->dst; 2732 2733 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc, 2734 mesh_da, sdata->vif.addr); 2735 if (is_multicast_ether_addr(mesh_da)) 2736 /* DA TA mSA AE:SA */ 2737 meshhdrlen = ieee80211_new_mesh_header( 2738 sdata, &mesh_hdr, 2739 skb->data + ETH_ALEN, NULL); 2740 else 2741 /* RA TA mDA mSA AE:DA SA */ 2742 meshhdrlen = ieee80211_new_mesh_header( 2743 sdata, &mesh_hdr, skb->data, 2744 skb->data + ETH_ALEN); 2745 2746 } 2747 2748 /* For injected frames, fill RA right away as nexthop lookup 2749 * will be skipped. 2750 */ 2751 if ((ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP) && 2752 is_zero_ether_addr(hdr.addr1)) 2753 memcpy(hdr.addr1, skb->data, ETH_ALEN); 2754 break; 2755 #endif 2756 case NL80211_IFTYPE_STATION: 2757 /* we already did checks when looking up the RA STA */ 2758 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER); 2759 2760 if (tdls_peer) { 2761 /* For TDLS only one link can be valid with peer STA */ 2762 int tdls_link_id = ieee80211_tdls_sta_link_id(sta); 2763 struct ieee80211_link_data *link; 2764 2765 /* DA SA BSSID */ 2766 memcpy(hdr.addr1, skb->data, ETH_ALEN); 2767 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN); 2768 link = rcu_dereference(sdata->link[tdls_link_id]); 2769 if (WARN_ON_ONCE(!link)) { 2770 ret = -EINVAL; 2771 goto free; 2772 } 2773 memcpy(hdr.addr3, link->u.mgd.bssid, ETH_ALEN); 2774 hdrlen = 24; 2775 } else if (sdata->u.mgd.use_4addr && 2776 cpu_to_be16(ethertype) != sdata->control_port_protocol) { 2777 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | 2778 IEEE80211_FCTL_TODS); 2779 /* RA TA DA SA */ 2780 memcpy(hdr.addr1, sdata->deflink.u.mgd.bssid, ETH_ALEN); 2781 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN); 2782 memcpy(hdr.addr3, skb->data, ETH_ALEN); 2783 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN); 2784 hdrlen = 30; 2785 } else { 2786 fc |= cpu_to_le16(IEEE80211_FCTL_TODS); 2787 /* BSSID SA DA */ 2788 memcpy(hdr.addr1, sdata->vif.cfg.ap_addr, ETH_ALEN); 2789 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN); 2790 memcpy(hdr.addr3, skb->data, ETH_ALEN); 2791 hdrlen = 24; 2792 } 2793 break; 2794 case NL80211_IFTYPE_OCB: 2795 /* DA SA BSSID */ 2796 memcpy(hdr.addr1, skb->data, ETH_ALEN); 2797 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN); 2798 eth_broadcast_addr(hdr.addr3); 2799 hdrlen = 24; 2800 break; 2801 case NL80211_IFTYPE_ADHOC: 2802 /* DA SA BSSID */ 2803 memcpy(hdr.addr1, skb->data, ETH_ALEN); 2804 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN); 2805 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN); 2806 hdrlen = 24; 2807 break; 2808 default: 2809 ret = -EINVAL; 2810 goto free; 2811 } 2812 2813 if (!chanctx_conf) { 2814 if (!ieee80211_vif_is_mld(&sdata->vif)) { 2815 ret = -ENOTCONN; 2816 goto free; 2817 } 2818 /* MLD transmissions must not rely on the band */ 2819 band = 0; 2820 } else { 2821 band = chanctx_conf->def.chan->band; 2822 } 2823 2824 multicast = is_multicast_ether_addr(hdr.addr1); 2825 2826 /* sta is always NULL for mesh */ 2827 if (sta) { 2828 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED); 2829 wme_sta = sta->sta.wme; 2830 } else if (ieee80211_vif_is_mesh(&sdata->vif)) { 2831 /* For mesh, the use of the QoS header is mandatory */ 2832 wme_sta = true; 2833 } 2834 2835 /* receiver does QoS (which also means we do) use it */ 2836 if (wme_sta) { 2837 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA); 2838 hdrlen += 2; 2839 } 2840 2841 /* 2842 * Drop unicast frames to unauthorised stations unless they are 2843 * EAPOL frames from the local station. 2844 */ 2845 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) && 2846 (sdata->vif.type != NL80211_IFTYPE_OCB) && 2847 !multicast && !authorized && 2848 (cpu_to_be16(ethertype) != sdata->control_port_protocol || 2849 !ieee80211_is_our_addr(sdata, skb->data + ETH_ALEN, NULL)))) { 2850 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 2851 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n", 2852 sdata->name, hdr.addr1); 2853 #endif 2854 2855 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port); 2856 2857 ret = -EPERM; 2858 goto free; 2859 } 2860 2861 if (unlikely(!multicast && 2862 ((skb->sk && sock_flag(skb->sk, SOCK_WIFI_STATUS)) || 2863 ctrl_flags & IEEE80211_TX_CTL_REQ_TX_STATUS))) 2864 info_id = ieee80211_store_ack_skb(local, skb, &info_flags, 2865 cookie); 2866 2867 /* 2868 * If the skb is shared we need to obtain our own copy. 2869 */ 2870 skb = skb_share_check(skb, GFP_ATOMIC); 2871 if (unlikely(!skb)) { 2872 ret = -ENOMEM; 2873 goto free; 2874 } 2875 2876 hdr.frame_control = fc; 2877 hdr.duration_id = 0; 2878 hdr.seq_ctrl = 0; 2879 2880 skip_header_bytes = ETH_HLEN; 2881 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) { 2882 encaps_data = bridge_tunnel_header; 2883 encaps_len = sizeof(bridge_tunnel_header); 2884 skip_header_bytes -= 2; 2885 } else if (ethertype >= ETH_P_802_3_MIN) { 2886 encaps_data = rfc1042_header; 2887 encaps_len = sizeof(rfc1042_header); 2888 skip_header_bytes -= 2; 2889 } else { 2890 encaps_data = NULL; 2891 encaps_len = 0; 2892 } 2893 2894 skb_pull(skb, skip_header_bytes); 2895 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb); 2896 2897 /* 2898 * So we need to modify the skb header and hence need a copy of 2899 * that. The head_need variable above doesn't, so far, include 2900 * the needed header space that we don't need right away. If we 2901 * can, then we don't reallocate right now but only after the 2902 * frame arrives at the master device (if it does...) 2903 * 2904 * If we cannot, however, then we will reallocate to include all 2905 * the ever needed space. Also, if we need to reallocate it anyway, 2906 * make it big enough for everything we may ever need. 2907 */ 2908 2909 if (head_need > 0 || skb_cloned(skb)) { 2910 head_need += IEEE80211_ENCRYPT_HEADROOM; 2911 head_need += local->tx_headroom; 2912 head_need = max_t(int, 0, head_need); 2913 if (ieee80211_skb_resize(sdata, skb, head_need, ENCRYPT_DATA)) { 2914 ieee80211_free_txskb(&local->hw, skb); 2915 skb = NULL; 2916 return ERR_PTR(-ENOMEM); 2917 } 2918 } 2919 2920 if (encaps_data) 2921 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len); 2922 2923 #ifdef CONFIG_MAC80211_MESH 2924 if (meshhdrlen > 0) 2925 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen); 2926 #endif 2927 2928 if (ieee80211_is_data_qos(fc)) { 2929 __le16 *qos_control; 2930 2931 qos_control = skb_push(skb, 2); 2932 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2); 2933 /* 2934 * Maybe we could actually set some fields here, for now just 2935 * initialise to zero to indicate no special operation. 2936 */ 2937 *qos_control = 0; 2938 } else 2939 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen); 2940 2941 skb_reset_mac_header(skb); 2942 2943 info = IEEE80211_SKB_CB(skb); 2944 memset(info, 0, sizeof(*info)); 2945 2946 info->flags = info_flags; 2947 if (info_id) { 2948 info->status_data = info_id; 2949 info->status_data_idr = 1; 2950 } 2951 info->band = band; 2952 2953 if (likely(!cookie)) { 2954 ctrl_flags |= u32_encode_bits(link_id, 2955 IEEE80211_TX_CTRL_MLO_LINK); 2956 } else { 2957 unsigned int pre_conf_link_id; 2958 2959 /* 2960 * ctrl_flags already have been set by 2961 * ieee80211_tx_control_port(), here 2962 * we just sanity check that 2963 */ 2964 2965 pre_conf_link_id = u32_get_bits(ctrl_flags, 2966 IEEE80211_TX_CTRL_MLO_LINK); 2967 2968 if (pre_conf_link_id != link_id && 2969 link_id != IEEE80211_LINK_UNSPECIFIED) { 2970 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG 2971 net_info_ratelimited("%s: dropped frame to %pM with bad link ID request (%d vs. %d)\n", 2972 sdata->name, hdr.addr1, 2973 pre_conf_link_id, link_id); 2974 #endif 2975 ret = -EINVAL; 2976 goto free; 2977 } 2978 } 2979 2980 info->control.flags = ctrl_flags; 2981 2982 return skb; 2983 free: 2984 kfree_skb(skb); 2985 return ERR_PTR(ret); 2986 } 2987 2988 /* 2989 * fast-xmit overview 2990 * 2991 * The core idea of this fast-xmit is to remove per-packet checks by checking 2992 * them out of band. ieee80211_check_fast_xmit() implements the out-of-band 2993 * checks that are needed to get the sta->fast_tx pointer assigned, after which 2994 * much less work can be done per packet. For example, fragmentation must be 2995 * disabled or the fast_tx pointer will not be set. All the conditions are seen 2996 * in the code here. 2997 * 2998 * Once assigned, the fast_tx data structure also caches the per-packet 802.11 2999 * header and other data to aid packet processing in ieee80211_xmit_fast(). 3000 * 3001 * The most difficult part of this is that when any of these assumptions 3002 * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(), 3003 * ieee80211_check_fast_xmit() or friends) is required to reset the data, 3004 * since the per-packet code no longer checks the conditions. This is reflected 3005 * by the calls to these functions throughout the rest of the code, and must be 3006 * maintained if any of the TX path checks change. 3007 */ 3008 3009 void ieee80211_check_fast_xmit(struct sta_info *sta) 3010 { 3011 struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old; 3012 struct ieee80211_local *local = sta->local; 3013 struct ieee80211_sub_if_data *sdata = sta->sdata; 3014 struct ieee80211_hdr *hdr = (void *)build.hdr; 3015 struct ieee80211_chanctx_conf *chanctx_conf; 3016 __le16 fc; 3017 3018 if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT)) 3019 return; 3020 3021 if (ieee80211_vif_is_mesh(&sdata->vif)) 3022 mesh_fast_tx_flush_sta(sdata, sta); 3023 3024 /* Locking here protects both the pointer itself, and against concurrent 3025 * invocations winning data access races to, e.g., the key pointer that 3026 * is used. 3027 * Without it, the invocation of this function right after the key 3028 * pointer changes wouldn't be sufficient, as another CPU could access 3029 * the pointer, then stall, and then do the cache update after the CPU 3030 * that invalidated the key. 3031 * With the locking, such scenarios cannot happen as the check for the 3032 * key and the fast-tx assignment are done atomically, so the CPU that 3033 * modifies the key will either wait or other one will see the key 3034 * cleared/changed already. 3035 */ 3036 spin_lock_bh(&sta->lock); 3037 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) && 3038 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) && 3039 sdata->vif.type == NL80211_IFTYPE_STATION) 3040 goto out; 3041 3042 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED) || !sta->uploaded) 3043 goto out; 3044 3045 if (test_sta_flag(sta, WLAN_STA_PS_STA) || 3046 test_sta_flag(sta, WLAN_STA_PS_DRIVER) || 3047 test_sta_flag(sta, WLAN_STA_PS_DELIVER) || 3048 test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT)) 3049 goto out; 3050 3051 if (sdata->noack_map) 3052 goto out; 3053 3054 /* fast-xmit doesn't handle fragmentation at all */ 3055 if (local->hw.wiphy->frag_threshold != (u32)-1 && 3056 !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG)) 3057 goto out; 3058 3059 if (!ieee80211_vif_is_mld(&sdata->vif)) { 3060 rcu_read_lock(); 3061 chanctx_conf = 3062 rcu_dereference(sdata->vif.bss_conf.chanctx_conf); 3063 if (!chanctx_conf) { 3064 rcu_read_unlock(); 3065 goto out; 3066 } 3067 build.band = chanctx_conf->def.chan->band; 3068 rcu_read_unlock(); 3069 } else { 3070 /* MLD transmissions must not rely on the band */ 3071 build.band = 0; 3072 } 3073 3074 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA); 3075 3076 switch (sdata->vif.type) { 3077 case NL80211_IFTYPE_ADHOC: 3078 /* DA SA BSSID */ 3079 build.da_offs = offsetof(struct ieee80211_hdr, addr1); 3080 build.sa_offs = offsetof(struct ieee80211_hdr, addr2); 3081 memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN); 3082 build.hdr_len = 24; 3083 break; 3084 case NL80211_IFTYPE_STATION: 3085 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) { 3086 /* For TDLS only one link can be valid with peer STA */ 3087 int tdls_link_id = ieee80211_tdls_sta_link_id(sta); 3088 struct ieee80211_link_data *link; 3089 3090 /* DA SA BSSID */ 3091 build.da_offs = offsetof(struct ieee80211_hdr, addr1); 3092 build.sa_offs = offsetof(struct ieee80211_hdr, addr2); 3093 rcu_read_lock(); 3094 link = rcu_dereference(sdata->link[tdls_link_id]); 3095 if (!WARN_ON_ONCE(!link)) 3096 memcpy(hdr->addr3, link->u.mgd.bssid, ETH_ALEN); 3097 rcu_read_unlock(); 3098 build.hdr_len = 24; 3099 break; 3100 } 3101 3102 if (sdata->u.mgd.use_4addr) { 3103 /* non-regular ethertype cannot use the fastpath */ 3104 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | 3105 IEEE80211_FCTL_TODS); 3106 /* RA TA DA SA */ 3107 memcpy(hdr->addr1, sdata->deflink.u.mgd.bssid, ETH_ALEN); 3108 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN); 3109 build.da_offs = offsetof(struct ieee80211_hdr, addr3); 3110 build.sa_offs = offsetof(struct ieee80211_hdr, addr4); 3111 build.hdr_len = 30; 3112 break; 3113 } 3114 fc |= cpu_to_le16(IEEE80211_FCTL_TODS); 3115 /* BSSID SA DA */ 3116 memcpy(hdr->addr1, sdata->vif.cfg.ap_addr, ETH_ALEN); 3117 build.da_offs = offsetof(struct ieee80211_hdr, addr3); 3118 build.sa_offs = offsetof(struct ieee80211_hdr, addr2); 3119 build.hdr_len = 24; 3120 break; 3121 case NL80211_IFTYPE_AP_VLAN: 3122 if (sdata->wdev.use_4addr) { 3123 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | 3124 IEEE80211_FCTL_TODS); 3125 /* RA TA DA SA */ 3126 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN); 3127 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN); 3128 build.da_offs = offsetof(struct ieee80211_hdr, addr3); 3129 build.sa_offs = offsetof(struct ieee80211_hdr, addr4); 3130 build.hdr_len = 30; 3131 break; 3132 } 3133 fallthrough; 3134 case NL80211_IFTYPE_AP: 3135 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS); 3136 /* DA BSSID SA */ 3137 build.da_offs = offsetof(struct ieee80211_hdr, addr1); 3138 if (sta->sta.mlo || !ieee80211_vif_is_mld(&sdata->vif)) { 3139 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN); 3140 } else { 3141 unsigned int link_id = sta->deflink.link_id; 3142 struct ieee80211_link_data *link; 3143 3144 rcu_read_lock(); 3145 link = rcu_dereference(sdata->link[link_id]); 3146 if (WARN_ON(!link)) { 3147 rcu_read_unlock(); 3148 goto out; 3149 } 3150 memcpy(hdr->addr2, link->conf->addr, ETH_ALEN); 3151 rcu_read_unlock(); 3152 } 3153 build.sa_offs = offsetof(struct ieee80211_hdr, addr3); 3154 build.hdr_len = 24; 3155 break; 3156 default: 3157 /* not handled on fast-xmit */ 3158 goto out; 3159 } 3160 3161 if (sta->sta.wme) { 3162 build.hdr_len += 2; 3163 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA); 3164 } 3165 3166 /* We store the key here so there's no point in using rcu_dereference() 3167 * but that's fine because the code that changes the pointers will call 3168 * this function after doing so. For a single CPU that would be enough, 3169 * for multiple see the comment above. 3170 */ 3171 build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]); 3172 if (!build.key) 3173 build.key = rcu_access_pointer(sdata->default_unicast_key); 3174 if (build.key) { 3175 bool gen_iv, iv_spc, mmic; 3176 3177 gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV; 3178 iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE; 3179 mmic = build.key->conf.flags & 3180 (IEEE80211_KEY_FLAG_GENERATE_MMIC | 3181 IEEE80211_KEY_FLAG_PUT_MIC_SPACE); 3182 3183 /* don't handle software crypto */ 3184 if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)) 3185 goto out; 3186 3187 /* Key is being removed */ 3188 if (build.key->flags & KEY_FLAG_TAINTED) 3189 goto out; 3190 3191 switch (build.key->conf.cipher) { 3192 case WLAN_CIPHER_SUITE_CCMP: 3193 case WLAN_CIPHER_SUITE_CCMP_256: 3194 if (gen_iv) 3195 build.pn_offs = build.hdr_len; 3196 if (gen_iv || iv_spc) 3197 build.hdr_len += IEEE80211_CCMP_HDR_LEN; 3198 break; 3199 case WLAN_CIPHER_SUITE_GCMP: 3200 case WLAN_CIPHER_SUITE_GCMP_256: 3201 if (gen_iv) 3202 build.pn_offs = build.hdr_len; 3203 if (gen_iv || iv_spc) 3204 build.hdr_len += IEEE80211_GCMP_HDR_LEN; 3205 break; 3206 case WLAN_CIPHER_SUITE_TKIP: 3207 /* cannot handle MMIC or IV generation in xmit-fast */ 3208 if (mmic || gen_iv) 3209 goto out; 3210 if (iv_spc) 3211 build.hdr_len += IEEE80211_TKIP_IV_LEN; 3212 break; 3213 case WLAN_CIPHER_SUITE_WEP40: 3214 case WLAN_CIPHER_SUITE_WEP104: 3215 /* cannot handle IV generation in fast-xmit */ 3216 if (gen_iv) 3217 goto out; 3218 if (iv_spc) 3219 build.hdr_len += IEEE80211_WEP_IV_LEN; 3220 break; 3221 case WLAN_CIPHER_SUITE_AES_CMAC: 3222 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 3223 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 3224 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 3225 WARN(1, 3226 "management cipher suite 0x%x enabled for data\n", 3227 build.key->conf.cipher); 3228 goto out; 3229 default: 3230 /* we don't know how to generate IVs for this at all */ 3231 if (WARN_ON(gen_iv)) 3232 goto out; 3233 } 3234 3235 fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED); 3236 } 3237 3238 hdr->frame_control = fc; 3239 3240 memcpy(build.hdr + build.hdr_len, 3241 rfc1042_header, sizeof(rfc1042_header)); 3242 build.hdr_len += sizeof(rfc1042_header); 3243 3244 fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC); 3245 /* if the kmemdup fails, continue w/o fast_tx */ 3246 3247 out: 3248 /* we might have raced against another call to this function */ 3249 old = rcu_dereference_protected(sta->fast_tx, 3250 lockdep_is_held(&sta->lock)); 3251 rcu_assign_pointer(sta->fast_tx, fast_tx); 3252 if (old) 3253 kfree_rcu(old, rcu_head); 3254 spin_unlock_bh(&sta->lock); 3255 } 3256 3257 void ieee80211_check_fast_xmit_all(struct ieee80211_local *local) 3258 { 3259 struct sta_info *sta; 3260 3261 rcu_read_lock(); 3262 list_for_each_entry_rcu(sta, &local->sta_list, list) 3263 ieee80211_check_fast_xmit(sta); 3264 rcu_read_unlock(); 3265 } 3266 3267 void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata) 3268 { 3269 struct ieee80211_local *local = sdata->local; 3270 struct sta_info *sta; 3271 3272 rcu_read_lock(); 3273 3274 list_for_each_entry_rcu(sta, &local->sta_list, list) { 3275 if (sdata != sta->sdata && 3276 (!sta->sdata->bss || sta->sdata->bss != sdata->bss)) 3277 continue; 3278 ieee80211_check_fast_xmit(sta); 3279 } 3280 3281 rcu_read_unlock(); 3282 } 3283 3284 void ieee80211_clear_fast_xmit(struct sta_info *sta) 3285 { 3286 struct ieee80211_fast_tx *fast_tx; 3287 3288 spin_lock_bh(&sta->lock); 3289 fast_tx = rcu_dereference_protected(sta->fast_tx, 3290 lockdep_is_held(&sta->lock)); 3291 RCU_INIT_POINTER(sta->fast_tx, NULL); 3292 spin_unlock_bh(&sta->lock); 3293 3294 if (fast_tx) 3295 kfree_rcu(fast_tx, rcu_head); 3296 } 3297 3298 static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local, 3299 struct sk_buff *skb, int headroom) 3300 { 3301 if (skb_headroom(skb) < headroom) { 3302 I802_DEBUG_INC(local->tx_expand_skb_head); 3303 3304 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) { 3305 wiphy_debug(local->hw.wiphy, 3306 "failed to reallocate TX buffer\n"); 3307 return false; 3308 } 3309 } 3310 3311 return true; 3312 } 3313 3314 static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata, 3315 struct ieee80211_fast_tx *fast_tx, 3316 struct sk_buff *skb) 3317 { 3318 struct ieee80211_local *local = sdata->local; 3319 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 3320 struct ieee80211_hdr *hdr; 3321 struct ethhdr *amsdu_hdr; 3322 int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header); 3323 int subframe_len = skb->len - hdr_len; 3324 void *data; 3325 u8 *qc, *h_80211_src, *h_80211_dst; 3326 const u8 *bssid; 3327 3328 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) 3329 return false; 3330 3331 if (info->control.flags & IEEE80211_TX_CTRL_AMSDU) 3332 return true; 3333 3334 if (!ieee80211_amsdu_realloc_pad(local, skb, 3335 sizeof(*amsdu_hdr) + 3336 local->hw.extra_tx_headroom)) 3337 return false; 3338 3339 data = skb_push(skb, sizeof(*amsdu_hdr)); 3340 memmove(data, data + sizeof(*amsdu_hdr), hdr_len); 3341 hdr = data; 3342 amsdu_hdr = data + hdr_len; 3343 /* h_80211_src/dst is addr* field within hdr */ 3344 h_80211_src = data + fast_tx->sa_offs; 3345 h_80211_dst = data + fast_tx->da_offs; 3346 3347 amsdu_hdr->h_proto = cpu_to_be16(subframe_len); 3348 ether_addr_copy(amsdu_hdr->h_source, h_80211_src); 3349 ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst); 3350 3351 /* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA 3352 * fields needs to be changed to BSSID for A-MSDU frames depending 3353 * on FromDS/ToDS values. 3354 */ 3355 switch (sdata->vif.type) { 3356 case NL80211_IFTYPE_STATION: 3357 bssid = sdata->vif.cfg.ap_addr; 3358 break; 3359 case NL80211_IFTYPE_AP: 3360 case NL80211_IFTYPE_AP_VLAN: 3361 bssid = sdata->vif.addr; 3362 break; 3363 default: 3364 bssid = NULL; 3365 } 3366 3367 if (bssid && ieee80211_has_fromds(hdr->frame_control)) 3368 ether_addr_copy(h_80211_src, bssid); 3369 3370 if (bssid && ieee80211_has_tods(hdr->frame_control)) 3371 ether_addr_copy(h_80211_dst, bssid); 3372 3373 qc = ieee80211_get_qos_ctl(hdr); 3374 *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT; 3375 3376 info->control.flags |= IEEE80211_TX_CTRL_AMSDU; 3377 3378 return true; 3379 } 3380 3381 static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata, 3382 struct sta_info *sta, 3383 struct ieee80211_fast_tx *fast_tx, 3384 struct sk_buff *skb, 3385 const u8 *da, const u8 *sa) 3386 { 3387 struct ieee80211_local *local = sdata->local; 3388 struct fq *fq = &local->fq; 3389 struct fq_tin *tin; 3390 struct fq_flow *flow; 3391 u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK; 3392 struct ieee80211_txq *txq = sta->sta.txq[tid]; 3393 struct txq_info *txqi; 3394 struct sk_buff **frag_tail, *head; 3395 int subframe_len = skb->len - ETH_ALEN; 3396 u8 max_subframes = sta->sta.max_amsdu_subframes; 3397 int max_frags = local->hw.max_tx_fragments; 3398 int max_amsdu_len = sta->sta.cur->max_amsdu_len; 3399 int orig_truesize; 3400 u32 flow_idx; 3401 __be16 len; 3402 void *data; 3403 bool ret = false; 3404 unsigned int orig_len; 3405 int n = 2, nfrags, pad = 0; 3406 u16 hdrlen; 3407 3408 if (!ieee80211_hw_check(&local->hw, TX_AMSDU)) 3409 return false; 3410 3411 if (sdata->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED) 3412 return false; 3413 3414 if (ieee80211_vif_is_mesh(&sdata->vif)) 3415 return false; 3416 3417 if (skb_is_gso(skb)) 3418 return false; 3419 3420 if (!txq) 3421 return false; 3422 3423 txqi = to_txq_info(txq); 3424 if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags)) 3425 return false; 3426 3427 if (sta->sta.cur->max_rc_amsdu_len) 3428 max_amsdu_len = min_t(int, max_amsdu_len, 3429 sta->sta.cur->max_rc_amsdu_len); 3430 3431 if (sta->sta.cur->max_tid_amsdu_len[tid]) 3432 max_amsdu_len = min_t(int, max_amsdu_len, 3433 sta->sta.cur->max_tid_amsdu_len[tid]); 3434 3435 flow_idx = fq_flow_idx(fq, skb); 3436 3437 spin_lock_bh(&fq->lock); 3438 3439 /* TODO: Ideally aggregation should be done on dequeue to remain 3440 * responsive to environment changes. 3441 */ 3442 3443 tin = &txqi->tin; 3444 flow = fq_flow_classify(fq, tin, flow_idx, skb); 3445 head = skb_peek_tail(&flow->queue); 3446 if (!head || skb_is_gso(head)) 3447 goto out; 3448 3449 orig_truesize = head->truesize; 3450 orig_len = head->len; 3451 3452 if (skb->len + head->len > max_amsdu_len) 3453 goto out; 3454 3455 nfrags = 1 + skb_shinfo(skb)->nr_frags; 3456 nfrags += 1 + skb_shinfo(head)->nr_frags; 3457 frag_tail = &skb_shinfo(head)->frag_list; 3458 while (*frag_tail) { 3459 nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags; 3460 frag_tail = &(*frag_tail)->next; 3461 n++; 3462 } 3463 3464 if (max_subframes && n > max_subframes) 3465 goto out; 3466 3467 if (max_frags && nfrags > max_frags) 3468 goto out; 3469 3470 if (!drv_can_aggregate_in_amsdu(local, head, skb)) 3471 goto out; 3472 3473 if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head)) 3474 goto out; 3475 3476 /* If n == 2, the "while (*frag_tail)" loop above didn't execute 3477 * and frag_tail should be &skb_shinfo(head)->frag_list. 3478 * However, ieee80211_amsdu_prepare_head() can reallocate it. 3479 * Reload frag_tail to have it pointing to the correct place. 3480 */ 3481 if (n == 2) 3482 frag_tail = &skb_shinfo(head)->frag_list; 3483 3484 /* 3485 * Pad out the previous subframe to a multiple of 4 by adding the 3486 * padding to the next one, that's being added. Note that head->len 3487 * is the length of the full A-MSDU, but that works since each time 3488 * we add a new subframe we pad out the previous one to a multiple 3489 * of 4 and thus it no longer matters in the next round. 3490 */ 3491 hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header); 3492 if ((head->len - hdrlen) & 3) 3493 pad = 4 - ((head->len - hdrlen) & 3); 3494 3495 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) + 3496 2 + pad)) 3497 goto out_recalc; 3498 3499 ret = true; 3500 data = skb_push(skb, ETH_ALEN + 2); 3501 ether_addr_copy(data, da); 3502 ether_addr_copy(data + ETH_ALEN, sa); 3503 3504 data += 2 * ETH_ALEN; 3505 len = cpu_to_be16(subframe_len); 3506 memcpy(data, &len, 2); 3507 memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header)); 3508 3509 memset(skb_push(skb, pad), 0, pad); 3510 3511 head->len += skb->len; 3512 head->data_len += skb->len; 3513 *frag_tail = skb; 3514 3515 out_recalc: 3516 fq->memory_usage += head->truesize - orig_truesize; 3517 if (head->len != orig_len) { 3518 flow->backlog += head->len - orig_len; 3519 tin->backlog_bytes += head->len - orig_len; 3520 } 3521 out: 3522 spin_unlock_bh(&fq->lock); 3523 3524 return ret; 3525 } 3526 3527 /* 3528 * Can be called while the sta lock is held. Anything that can cause packets to 3529 * be generated will cause deadlock! 3530 */ 3531 static ieee80211_tx_result 3532 ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata, 3533 struct sta_info *sta, u8 pn_offs, 3534 struct ieee80211_key *key, 3535 struct ieee80211_tx_data *tx) 3536 { 3537 struct sk_buff *skb = tx->skb; 3538 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 3539 struct ieee80211_hdr *hdr = (void *)skb->data; 3540 u8 tid = IEEE80211_NUM_TIDS; 3541 3542 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL) && 3543 ieee80211_tx_h_rate_ctrl(tx) != TX_CONTINUE) 3544 return TX_DROP; 3545 3546 if (key) 3547 info->control.hw_key = &key->conf; 3548 3549 dev_sw_netstats_tx_add(skb->dev, 1, skb->len); 3550 3551 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) { 3552 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK; 3553 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid); 3554 } else { 3555 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ; 3556 hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number); 3557 sdata->sequence_number += 0x10; 3558 } 3559 3560 if (skb_shinfo(skb)->gso_size) 3561 sta->deflink.tx_stats.msdu[tid] += 3562 DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size); 3563 else 3564 sta->deflink.tx_stats.msdu[tid]++; 3565 3566 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)]; 3567 3568 /* statistics normally done by ieee80211_tx_h_stats (but that 3569 * has to consider fragmentation, so is more complex) 3570 */ 3571 sta->deflink.tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len; 3572 sta->deflink.tx_stats.packets[skb_get_queue_mapping(skb)]++; 3573 3574 if (pn_offs) { 3575 u64 pn; 3576 u8 *crypto_hdr = skb->data + pn_offs; 3577 3578 switch (key->conf.cipher) { 3579 case WLAN_CIPHER_SUITE_CCMP: 3580 case WLAN_CIPHER_SUITE_CCMP_256: 3581 case WLAN_CIPHER_SUITE_GCMP: 3582 case WLAN_CIPHER_SUITE_GCMP_256: 3583 pn = atomic64_inc_return(&key->conf.tx_pn); 3584 crypto_hdr[0] = pn; 3585 crypto_hdr[1] = pn >> 8; 3586 crypto_hdr[3] = 0x20 | (key->conf.keyidx << 6); 3587 crypto_hdr[4] = pn >> 16; 3588 crypto_hdr[5] = pn >> 24; 3589 crypto_hdr[6] = pn >> 32; 3590 crypto_hdr[7] = pn >> 40; 3591 break; 3592 } 3593 } 3594 3595 return TX_CONTINUE; 3596 } 3597 3598 static netdev_features_t 3599 ieee80211_sdata_netdev_features(struct ieee80211_sub_if_data *sdata) 3600 { 3601 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN) 3602 return sdata->vif.netdev_features; 3603 3604 if (!sdata->bss) 3605 return 0; 3606 3607 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap); 3608 return sdata->vif.netdev_features; 3609 } 3610 3611 static struct sk_buff * 3612 ieee80211_tx_skb_fixup(struct sk_buff *skb, netdev_features_t features) 3613 { 3614 if (skb_is_gso(skb)) { 3615 struct sk_buff *segs; 3616 3617 segs = skb_gso_segment(skb, features); 3618 if (!segs) 3619 return skb; 3620 if (IS_ERR(segs)) 3621 goto free; 3622 3623 consume_skb(skb); 3624 return segs; 3625 } 3626 3627 if (skb_needs_linearize(skb, features) && __skb_linearize(skb)) 3628 goto free; 3629 3630 if (skb->ip_summed == CHECKSUM_PARTIAL) { 3631 int ofs = skb_checksum_start_offset(skb); 3632 3633 if (skb->encapsulation) 3634 skb_set_inner_transport_header(skb, ofs); 3635 else 3636 skb_set_transport_header(skb, ofs); 3637 3638 if (skb_csum_hwoffload_help(skb, features)) 3639 goto free; 3640 } 3641 3642 skb_mark_not_on_list(skb); 3643 return skb; 3644 3645 free: 3646 kfree_skb(skb); 3647 return NULL; 3648 } 3649 3650 void __ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata, 3651 struct sta_info *sta, 3652 struct ieee80211_fast_tx *fast_tx, 3653 struct sk_buff *skb, bool ampdu, 3654 const u8 *da, const u8 *sa) 3655 { 3656 struct ieee80211_local *local = sdata->local; 3657 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr; 3658 struct ieee80211_tx_info *info; 3659 struct ieee80211_tx_data tx; 3660 ieee80211_tx_result r; 3661 int hw_headroom = sdata->local->hw.extra_tx_headroom; 3662 int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2); 3663 3664 skb = skb_share_check(skb, GFP_ATOMIC); 3665 if (unlikely(!skb)) 3666 return; 3667 3668 if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) && 3669 ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb, da, sa)) 3670 return; 3671 3672 /* will not be crypto-handled beyond what we do here, so use false 3673 * as the may-encrypt argument for the resize to not account for 3674 * more room than we already have in 'extra_head' 3675 */ 3676 if (unlikely(ieee80211_skb_resize(sdata, skb, 3677 max_t(int, extra_head + hw_headroom - 3678 skb_headroom(skb), 0), 3679 ENCRYPT_NO))) 3680 goto free; 3681 3682 hdr = skb_push(skb, extra_head); 3683 memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len); 3684 memcpy(skb->data + fast_tx->da_offs, da, ETH_ALEN); 3685 memcpy(skb->data + fast_tx->sa_offs, sa, ETH_ALEN); 3686 3687 info = IEEE80211_SKB_CB(skb); 3688 memset(info, 0, sizeof(*info)); 3689 info->band = fast_tx->band; 3690 info->control.vif = &sdata->vif; 3691 info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT | 3692 IEEE80211_TX_CTL_DONTFRAG; 3693 info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT | 3694 u32_encode_bits(IEEE80211_LINK_UNSPECIFIED, 3695 IEEE80211_TX_CTRL_MLO_LINK); 3696 3697 #ifdef CONFIG_MAC80211_DEBUGFS 3698 if (local->force_tx_status) 3699 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS; 3700 #endif 3701 3702 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) { 3703 u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK; 3704 3705 *ieee80211_get_qos_ctl(hdr) = tid; 3706 } 3707 3708 __skb_queue_head_init(&tx.skbs); 3709 3710 tx.flags = IEEE80211_TX_UNICAST; 3711 tx.local = local; 3712 tx.sdata = sdata; 3713 tx.sta = sta; 3714 tx.key = fast_tx->key; 3715 3716 if (ieee80211_queue_skb(local, sdata, sta, skb)) 3717 return; 3718 3719 tx.skb = skb; 3720 r = ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs, 3721 fast_tx->key, &tx); 3722 tx.skb = NULL; 3723 if (r == TX_DROP) 3724 goto free; 3725 3726 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 3727 sdata = container_of(sdata->bss, 3728 struct ieee80211_sub_if_data, u.ap); 3729 3730 __skb_queue_tail(&tx.skbs, skb); 3731 ieee80211_tx_frags(local, &sdata->vif, sta, &tx.skbs, false); 3732 return; 3733 3734 free: 3735 kfree_skb(skb); 3736 } 3737 3738 static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata, 3739 struct sta_info *sta, 3740 struct ieee80211_fast_tx *fast_tx, 3741 struct sk_buff *skb) 3742 { 3743 u16 ethertype = (skb->data[12] << 8) | skb->data[13]; 3744 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr; 3745 struct tid_ampdu_tx *tid_tx = NULL; 3746 struct sk_buff *next; 3747 struct ethhdr eth; 3748 u8 tid = IEEE80211_NUM_TIDS; 3749 3750 /* control port protocol needs a lot of special handling */ 3751 if (cpu_to_be16(ethertype) == sdata->control_port_protocol) 3752 return false; 3753 3754 /* only RFC 1042 SNAP */ 3755 if (ethertype < ETH_P_802_3_MIN) 3756 return false; 3757 3758 /* don't handle TX status request here either */ 3759 if (skb->sk && sock_flag(skb->sk, SOCK_WIFI_STATUS)) 3760 return false; 3761 3762 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) { 3763 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK; 3764 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]); 3765 if (tid_tx) { 3766 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) 3767 return false; 3768 if (tid_tx->timeout) 3769 tid_tx->last_tx = jiffies; 3770 } 3771 } 3772 3773 memcpy(ð, skb->data, ETH_HLEN - 2); 3774 3775 /* after this point (skb is modified) we cannot return false */ 3776 skb = ieee80211_tx_skb_fixup(skb, ieee80211_sdata_netdev_features(sdata)); 3777 if (!skb) 3778 return true; 3779 3780 skb_list_walk_safe(skb, skb, next) { 3781 skb_mark_not_on_list(skb); 3782 __ieee80211_xmit_fast(sdata, sta, fast_tx, skb, tid_tx, 3783 eth.h_dest, eth.h_source); 3784 } 3785 3786 return true; 3787 } 3788 3789 struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw, 3790 struct ieee80211_txq *txq) 3791 { 3792 struct ieee80211_local *local = hw_to_local(hw); 3793 struct txq_info *txqi = container_of(txq, struct txq_info, txq); 3794 struct ieee80211_hdr *hdr; 3795 struct sk_buff *skb = NULL; 3796 struct fq *fq = &local->fq; 3797 struct fq_tin *tin = &txqi->tin; 3798 struct ieee80211_tx_info *info; 3799 struct ieee80211_tx_data tx; 3800 ieee80211_tx_result r; 3801 struct ieee80211_vif *vif = txq->vif; 3802 int q = vif->hw_queue[txq->ac]; 3803 unsigned long flags; 3804 bool q_stopped; 3805 3806 WARN_ON_ONCE(softirq_count() == 0); 3807 3808 if (!ieee80211_txq_airtime_check(hw, txq)) 3809 return NULL; 3810 3811 begin: 3812 spin_lock_irqsave(&local->queue_stop_reason_lock, flags); 3813 q_stopped = local->queue_stop_reasons[q]; 3814 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 3815 3816 if (unlikely(q_stopped)) { 3817 /* mark for waking later */ 3818 set_bit(IEEE80211_TXQ_DIRTY, &txqi->flags); 3819 return NULL; 3820 } 3821 3822 spin_lock_bh(&fq->lock); 3823 3824 /* Make sure fragments stay together. */ 3825 skb = __skb_dequeue(&txqi->frags); 3826 if (unlikely(skb)) { 3827 if (!(IEEE80211_SKB_CB(skb)->control.flags & 3828 IEEE80211_TX_INTCFL_NEED_TXPROCESSING)) 3829 goto out; 3830 IEEE80211_SKB_CB(skb)->control.flags &= 3831 ~IEEE80211_TX_INTCFL_NEED_TXPROCESSING; 3832 } else { 3833 if (unlikely(test_bit(IEEE80211_TXQ_STOP, &txqi->flags))) 3834 goto out; 3835 3836 skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func); 3837 } 3838 3839 if (!skb) 3840 goto out; 3841 3842 spin_unlock_bh(&fq->lock); 3843 3844 hdr = (struct ieee80211_hdr *)skb->data; 3845 info = IEEE80211_SKB_CB(skb); 3846 3847 memset(&tx, 0, sizeof(tx)); 3848 __skb_queue_head_init(&tx.skbs); 3849 tx.local = local; 3850 tx.skb = skb; 3851 tx.sdata = vif_to_sdata(info->control.vif); 3852 3853 if (txq->sta) { 3854 tx.sta = container_of(txq->sta, struct sta_info, sta); 3855 /* 3856 * Drop unicast frames to unauthorised stations unless they are 3857 * injected frames or EAPOL frames from the local station. 3858 */ 3859 if (unlikely(!(info->flags & IEEE80211_TX_CTL_INJECTED) && 3860 ieee80211_is_data(hdr->frame_control) && 3861 !ieee80211_vif_is_mesh(&tx.sdata->vif) && 3862 tx.sdata->vif.type != NL80211_IFTYPE_OCB && 3863 !is_multicast_ether_addr(hdr->addr1) && 3864 !test_sta_flag(tx.sta, WLAN_STA_AUTHORIZED) && 3865 (!(info->control.flags & 3866 IEEE80211_TX_CTRL_PORT_CTRL_PROTO) || 3867 !ieee80211_is_our_addr(tx.sdata, hdr->addr2, 3868 NULL)))) { 3869 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port); 3870 ieee80211_free_txskb(&local->hw, skb); 3871 goto begin; 3872 } 3873 } 3874 3875 /* 3876 * The key can be removed while the packet was queued, so need to call 3877 * this here to get the current key. 3878 */ 3879 r = ieee80211_tx_h_select_key(&tx); 3880 if (r != TX_CONTINUE) { 3881 ieee80211_free_txskb(&local->hw, skb); 3882 goto begin; 3883 } 3884 3885 if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags)) 3886 info->flags |= (IEEE80211_TX_CTL_AMPDU | 3887 IEEE80211_TX_CTL_DONTFRAG); 3888 3889 if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) { 3890 if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) { 3891 r = ieee80211_tx_h_rate_ctrl(&tx); 3892 if (r != TX_CONTINUE) { 3893 ieee80211_free_txskb(&local->hw, skb); 3894 goto begin; 3895 } 3896 } 3897 goto encap_out; 3898 } 3899 3900 if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) { 3901 struct sta_info *sta = container_of(txq->sta, struct sta_info, 3902 sta); 3903 u8 pn_offs = 0; 3904 3905 if (tx.key && 3906 (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) 3907 pn_offs = ieee80211_hdrlen(hdr->frame_control); 3908 3909 r = ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs, 3910 tx.key, &tx); 3911 if (r != TX_CONTINUE) { 3912 ieee80211_free_txskb(&local->hw, skb); 3913 goto begin; 3914 } 3915 } else { 3916 if (invoke_tx_handlers_late(&tx)) 3917 goto begin; 3918 3919 skb = __skb_dequeue(&tx.skbs); 3920 info = IEEE80211_SKB_CB(skb); 3921 3922 if (!skb_queue_empty(&tx.skbs)) { 3923 spin_lock_bh(&fq->lock); 3924 skb_queue_splice_tail(&tx.skbs, &txqi->frags); 3925 spin_unlock_bh(&fq->lock); 3926 } 3927 } 3928 3929 if (skb_has_frag_list(skb) && 3930 !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) { 3931 if (skb_linearize(skb)) { 3932 ieee80211_free_txskb(&local->hw, skb); 3933 goto begin; 3934 } 3935 } 3936 3937 switch (tx.sdata->vif.type) { 3938 case NL80211_IFTYPE_MONITOR: 3939 if ((tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) || 3940 ieee80211_hw_check(&local->hw, NO_VIRTUAL_MONITOR)) { 3941 vif = &tx.sdata->vif; 3942 break; 3943 } 3944 tx.sdata = rcu_dereference(local->monitor_sdata); 3945 if (tx.sdata && 3946 ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF)) { 3947 vif = &tx.sdata->vif; 3948 info->hw_queue = 3949 vif->hw_queue[skb_get_queue_mapping(skb)]; 3950 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) { 3951 ieee80211_free_txskb(&local->hw, skb); 3952 goto begin; 3953 } else { 3954 info->control.vif = NULL; 3955 return skb; 3956 } 3957 break; 3958 case NL80211_IFTYPE_AP_VLAN: 3959 tx.sdata = container_of(tx.sdata->bss, 3960 struct ieee80211_sub_if_data, u.ap); 3961 fallthrough; 3962 default: 3963 vif = &tx.sdata->vif; 3964 break; 3965 } 3966 3967 encap_out: 3968 info->control.vif = vif; 3969 3970 if (tx.sta && 3971 wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) { 3972 bool ampdu = txq->ac != IEEE80211_AC_VO; 3973 u32 airtime; 3974 3975 airtime = ieee80211_calc_expected_tx_airtime(hw, vif, txq->sta, 3976 skb->len, ampdu); 3977 if (airtime) { 3978 airtime = ieee80211_info_set_tx_time_est(info, airtime); 3979 ieee80211_sta_update_pending_airtime(local, tx.sta, 3980 txq->ac, 3981 airtime, 3982 false); 3983 } 3984 } 3985 3986 return skb; 3987 3988 out: 3989 spin_unlock_bh(&fq->lock); 3990 3991 return skb; 3992 } 3993 EXPORT_SYMBOL(ieee80211_tx_dequeue); 3994 3995 static inline s32 ieee80211_sta_deficit(struct sta_info *sta, u8 ac) 3996 { 3997 struct airtime_info *air_info = &sta->airtime[ac]; 3998 3999 return air_info->deficit - atomic_read(&air_info->aql_tx_pending); 4000 } 4001 4002 static void 4003 ieee80211_txq_set_active(struct txq_info *txqi) 4004 { 4005 struct sta_info *sta; 4006 4007 if (!txqi->txq.sta) 4008 return; 4009 4010 sta = container_of(txqi->txq.sta, struct sta_info, sta); 4011 sta->airtime[txqi->txq.ac].last_active = jiffies; 4012 } 4013 4014 static bool 4015 ieee80211_txq_keep_active(struct txq_info *txqi) 4016 { 4017 struct sta_info *sta; 4018 4019 if (!txqi->txq.sta) 4020 return false; 4021 4022 sta = container_of(txqi->txq.sta, struct sta_info, sta); 4023 if (ieee80211_sta_deficit(sta, txqi->txq.ac) >= 0) 4024 return false; 4025 4026 return ieee80211_sta_keep_active(sta, txqi->txq.ac); 4027 } 4028 4029 struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac) 4030 { 4031 struct ieee80211_local *local = hw_to_local(hw); 4032 struct ieee80211_txq *ret = NULL; 4033 struct txq_info *txqi = NULL, *head = NULL; 4034 bool found_eligible_txq = false; 4035 4036 spin_lock_bh(&local->active_txq_lock[ac]); 4037 4038 if (!local->schedule_round[ac]) 4039 goto out; 4040 4041 begin: 4042 txqi = list_first_entry_or_null(&local->active_txqs[ac], 4043 struct txq_info, 4044 schedule_order); 4045 if (!txqi) 4046 goto out; 4047 4048 if (txqi == head) { 4049 if (!found_eligible_txq) 4050 goto out; 4051 else 4052 found_eligible_txq = false; 4053 } 4054 4055 if (!head) 4056 head = txqi; 4057 4058 if (txqi->txq.sta) { 4059 struct sta_info *sta = container_of(txqi->txq.sta, 4060 struct sta_info, sta); 4061 bool aql_check = ieee80211_txq_airtime_check(hw, &txqi->txq); 4062 s32 deficit = ieee80211_sta_deficit(sta, txqi->txq.ac); 4063 4064 if (aql_check) 4065 found_eligible_txq = true; 4066 4067 if (deficit < 0) 4068 sta->airtime[txqi->txq.ac].deficit += 4069 sta->airtime_weight; 4070 4071 if (deficit < 0 || !aql_check) { 4072 list_move_tail(&txqi->schedule_order, 4073 &local->active_txqs[txqi->txq.ac]); 4074 goto begin; 4075 } 4076 } 4077 4078 if (txqi->schedule_round == local->schedule_round[ac]) 4079 goto out; 4080 4081 list_del_init(&txqi->schedule_order); 4082 txqi->schedule_round = local->schedule_round[ac]; 4083 ret = &txqi->txq; 4084 4085 out: 4086 spin_unlock_bh(&local->active_txq_lock[ac]); 4087 return ret; 4088 } 4089 EXPORT_SYMBOL(ieee80211_next_txq); 4090 4091 void __ieee80211_schedule_txq(struct ieee80211_hw *hw, 4092 struct ieee80211_txq *txq, 4093 bool force) 4094 { 4095 struct ieee80211_local *local = hw_to_local(hw); 4096 struct txq_info *txqi = to_txq_info(txq); 4097 bool has_queue; 4098 4099 spin_lock_bh(&local->active_txq_lock[txq->ac]); 4100 4101 has_queue = force || txq_has_queue(txq); 4102 if (list_empty(&txqi->schedule_order) && 4103 (has_queue || ieee80211_txq_keep_active(txqi))) { 4104 /* If airtime accounting is active, always enqueue STAs at the 4105 * head of the list to ensure that they only get moved to the 4106 * back by the airtime DRR scheduler once they have a negative 4107 * deficit. A station that already has a negative deficit will 4108 * get immediately moved to the back of the list on the next 4109 * call to ieee80211_next_txq(). 4110 */ 4111 if (txqi->txq.sta && local->airtime_flags && has_queue && 4112 wiphy_ext_feature_isset(local->hw.wiphy, 4113 NL80211_EXT_FEATURE_AIRTIME_FAIRNESS)) 4114 list_add(&txqi->schedule_order, 4115 &local->active_txqs[txq->ac]); 4116 else 4117 list_add_tail(&txqi->schedule_order, 4118 &local->active_txqs[txq->ac]); 4119 if (has_queue) 4120 ieee80211_txq_set_active(txqi); 4121 } 4122 4123 spin_unlock_bh(&local->active_txq_lock[txq->ac]); 4124 } 4125 EXPORT_SYMBOL(__ieee80211_schedule_txq); 4126 4127 DEFINE_STATIC_KEY_FALSE(aql_disable); 4128 4129 bool ieee80211_txq_airtime_check(struct ieee80211_hw *hw, 4130 struct ieee80211_txq *txq) 4131 { 4132 struct sta_info *sta; 4133 struct ieee80211_local *local = hw_to_local(hw); 4134 4135 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) 4136 return true; 4137 4138 if (static_branch_unlikely(&aql_disable)) 4139 return true; 4140 4141 if (!txq->sta) 4142 return true; 4143 4144 if (unlikely(txq->tid == IEEE80211_NUM_TIDS)) 4145 return true; 4146 4147 sta = container_of(txq->sta, struct sta_info, sta); 4148 if (atomic_read(&sta->airtime[txq->ac].aql_tx_pending) < 4149 sta->airtime[txq->ac].aql_limit_low) 4150 return true; 4151 4152 if (atomic_read(&local->aql_total_pending_airtime) < 4153 local->aql_threshold && 4154 atomic_read(&sta->airtime[txq->ac].aql_tx_pending) < 4155 sta->airtime[txq->ac].aql_limit_high) 4156 return true; 4157 4158 return false; 4159 } 4160 EXPORT_SYMBOL(ieee80211_txq_airtime_check); 4161 4162 static bool 4163 ieee80211_txq_schedule_airtime_check(struct ieee80211_local *local, u8 ac) 4164 { 4165 unsigned int num_txq = 0; 4166 struct txq_info *txq; 4167 u32 aql_limit; 4168 4169 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) 4170 return true; 4171 4172 list_for_each_entry(txq, &local->active_txqs[ac], schedule_order) 4173 num_txq++; 4174 4175 aql_limit = (num_txq - 1) * local->aql_txq_limit_low[ac] / 2 + 4176 local->aql_txq_limit_high[ac]; 4177 4178 return atomic_read(&local->aql_ac_pending_airtime[ac]) < aql_limit; 4179 } 4180 4181 bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw, 4182 struct ieee80211_txq *txq) 4183 { 4184 struct ieee80211_local *local = hw_to_local(hw); 4185 struct txq_info *iter, *tmp, *txqi = to_txq_info(txq); 4186 struct sta_info *sta; 4187 u8 ac = txq->ac; 4188 4189 spin_lock_bh(&local->active_txq_lock[ac]); 4190 4191 if (!txqi->txq.sta) 4192 goto out; 4193 4194 if (list_empty(&txqi->schedule_order)) 4195 goto out; 4196 4197 if (!ieee80211_txq_schedule_airtime_check(local, ac)) 4198 goto out; 4199 4200 list_for_each_entry_safe(iter, tmp, &local->active_txqs[ac], 4201 schedule_order) { 4202 if (iter == txqi) 4203 break; 4204 4205 if (!iter->txq.sta) { 4206 list_move_tail(&iter->schedule_order, 4207 &local->active_txqs[ac]); 4208 continue; 4209 } 4210 sta = container_of(iter->txq.sta, struct sta_info, sta); 4211 if (ieee80211_sta_deficit(sta, ac) < 0) 4212 sta->airtime[ac].deficit += sta->airtime_weight; 4213 list_move_tail(&iter->schedule_order, &local->active_txqs[ac]); 4214 } 4215 4216 sta = container_of(txqi->txq.sta, struct sta_info, sta); 4217 if (sta->airtime[ac].deficit >= 0) 4218 goto out; 4219 4220 sta->airtime[ac].deficit += sta->airtime_weight; 4221 list_move_tail(&txqi->schedule_order, &local->active_txqs[ac]); 4222 spin_unlock_bh(&local->active_txq_lock[ac]); 4223 4224 return false; 4225 out: 4226 if (!list_empty(&txqi->schedule_order)) 4227 list_del_init(&txqi->schedule_order); 4228 spin_unlock_bh(&local->active_txq_lock[ac]); 4229 4230 return true; 4231 } 4232 EXPORT_SYMBOL(ieee80211_txq_may_transmit); 4233 4234 void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac) 4235 { 4236 struct ieee80211_local *local = hw_to_local(hw); 4237 4238 spin_lock_bh(&local->active_txq_lock[ac]); 4239 4240 if (ieee80211_txq_schedule_airtime_check(local, ac)) { 4241 local->schedule_round[ac]++; 4242 if (!local->schedule_round[ac]) 4243 local->schedule_round[ac]++; 4244 } else { 4245 local->schedule_round[ac] = 0; 4246 } 4247 4248 spin_unlock_bh(&local->active_txq_lock[ac]); 4249 } 4250 EXPORT_SYMBOL(ieee80211_txq_schedule_start); 4251 4252 void __ieee80211_subif_start_xmit(struct sk_buff *skb, 4253 struct net_device *dev, 4254 u32 info_flags, 4255 u32 ctrl_flags, 4256 u64 *cookie) 4257 { 4258 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 4259 struct ieee80211_local *local = sdata->local; 4260 struct sta_info *sta; 4261 struct sk_buff *next; 4262 int len = skb->len; 4263 4264 if (unlikely(!ieee80211_sdata_running(sdata) || skb->len < ETH_HLEN)) { 4265 kfree_skb(skb); 4266 return; 4267 } 4268 4269 sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift); 4270 4271 rcu_read_lock(); 4272 4273 if (ieee80211_vif_is_mesh(&sdata->vif) && 4274 ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT) && 4275 ieee80211_mesh_xmit_fast(sdata, skb, ctrl_flags)) 4276 goto out; 4277 4278 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) 4279 goto out_free; 4280 4281 if (IS_ERR(sta)) 4282 sta = NULL; 4283 4284 skb_set_queue_mapping(skb, ieee80211_select_queue(sdata, sta, skb)); 4285 ieee80211_aggr_check(sdata, sta, skb); 4286 4287 if (sta) { 4288 struct ieee80211_fast_tx *fast_tx; 4289 4290 fast_tx = rcu_dereference(sta->fast_tx); 4291 4292 if (fast_tx && 4293 ieee80211_xmit_fast(sdata, sta, fast_tx, skb)) 4294 goto out; 4295 } 4296 4297 /* the frame could be fragmented, software-encrypted, and other 4298 * things so we cannot really handle checksum or GSO offload. 4299 * fix it up in software before we handle anything else. 4300 */ 4301 skb = ieee80211_tx_skb_fixup(skb, 0); 4302 if (!skb) { 4303 len = 0; 4304 goto out; 4305 } 4306 4307 skb_list_walk_safe(skb, skb, next) { 4308 skb_mark_not_on_list(skb); 4309 4310 if (skb->protocol == sdata->control_port_protocol) 4311 ctrl_flags |= IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP; 4312 4313 skb = ieee80211_build_hdr(sdata, skb, info_flags, 4314 sta, ctrl_flags, cookie); 4315 if (IS_ERR(skb)) { 4316 kfree_skb_list(next); 4317 goto out; 4318 } 4319 4320 dev_sw_netstats_tx_add(dev, 1, skb->len); 4321 4322 ieee80211_xmit(sdata, sta, skb); 4323 } 4324 goto out; 4325 out_free: 4326 kfree_skb(skb); 4327 len = 0; 4328 out: 4329 if (len) 4330 ieee80211_tpt_led_trig_tx(local, len); 4331 rcu_read_unlock(); 4332 } 4333 4334 static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta) 4335 { 4336 struct ethhdr *eth; 4337 int err; 4338 4339 err = skb_ensure_writable(skb, ETH_HLEN); 4340 if (unlikely(err)) 4341 return err; 4342 4343 eth = (void *)skb->data; 4344 ether_addr_copy(eth->h_dest, sta->sta.addr); 4345 4346 return 0; 4347 } 4348 4349 static bool ieee80211_multicast_to_unicast(struct sk_buff *skb, 4350 struct net_device *dev) 4351 { 4352 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 4353 const struct ethhdr *eth = (void *)skb->data; 4354 const struct vlan_ethhdr *ethvlan = (void *)skb->data; 4355 __be16 ethertype; 4356 4357 switch (sdata->vif.type) { 4358 case NL80211_IFTYPE_AP_VLAN: 4359 if (sdata->u.vlan.sta) 4360 return false; 4361 if (sdata->wdev.use_4addr) 4362 return false; 4363 fallthrough; 4364 case NL80211_IFTYPE_AP: 4365 /* check runtime toggle for this bss */ 4366 if (!sdata->bss->multicast_to_unicast) 4367 return false; 4368 break; 4369 default: 4370 return false; 4371 } 4372 4373 /* multicast to unicast conversion only for some payload */ 4374 ethertype = eth->h_proto; 4375 if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN) 4376 ethertype = ethvlan->h_vlan_encapsulated_proto; 4377 switch (ethertype) { 4378 case htons(ETH_P_ARP): 4379 case htons(ETH_P_IP): 4380 case htons(ETH_P_IPV6): 4381 break; 4382 default: 4383 return false; 4384 } 4385 4386 return true; 4387 } 4388 4389 static void 4390 ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev, 4391 struct sk_buff_head *queue) 4392 { 4393 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 4394 struct ieee80211_local *local = sdata->local; 4395 const struct ethhdr *eth = (struct ethhdr *)skb->data; 4396 struct sta_info *sta, *first = NULL; 4397 struct sk_buff *cloned_skb; 4398 4399 rcu_read_lock(); 4400 4401 list_for_each_entry_rcu(sta, &local->sta_list, list) { 4402 if (sdata != sta->sdata) 4403 /* AP-VLAN mismatch */ 4404 continue; 4405 if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr))) 4406 /* do not send back to source */ 4407 continue; 4408 if (!first) { 4409 first = sta; 4410 continue; 4411 } 4412 cloned_skb = skb_clone(skb, GFP_ATOMIC); 4413 if (!cloned_skb) 4414 goto multicast; 4415 if (unlikely(ieee80211_change_da(cloned_skb, sta))) { 4416 dev_kfree_skb(cloned_skb); 4417 goto multicast; 4418 } 4419 __skb_queue_tail(queue, cloned_skb); 4420 } 4421 4422 if (likely(first)) { 4423 if (unlikely(ieee80211_change_da(skb, first))) 4424 goto multicast; 4425 __skb_queue_tail(queue, skb); 4426 } else { 4427 /* no STA connected, drop */ 4428 kfree_skb(skb); 4429 skb = NULL; 4430 } 4431 4432 goto out; 4433 multicast: 4434 __skb_queue_purge(queue); 4435 __skb_queue_tail(queue, skb); 4436 out: 4437 rcu_read_unlock(); 4438 } 4439 4440 static void ieee80211_mlo_multicast_tx_one(struct ieee80211_sub_if_data *sdata, 4441 struct sk_buff *skb, u32 ctrl_flags, 4442 unsigned int link_id) 4443 { 4444 struct sk_buff *out; 4445 4446 out = skb_copy(skb, GFP_ATOMIC); 4447 if (!out) 4448 return; 4449 4450 ctrl_flags |= u32_encode_bits(link_id, IEEE80211_TX_CTRL_MLO_LINK); 4451 __ieee80211_subif_start_xmit(out, sdata->dev, 0, ctrl_flags, NULL); 4452 } 4453 4454 static void ieee80211_mlo_multicast_tx(struct net_device *dev, 4455 struct sk_buff *skb) 4456 { 4457 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 4458 unsigned long links = sdata->vif.active_links; 4459 unsigned int link; 4460 u32 ctrl_flags = IEEE80211_TX_CTRL_MCAST_MLO_FIRST_TX; 4461 4462 if (hweight16(links) == 1) { 4463 ctrl_flags |= u32_encode_bits(__ffs(links), 4464 IEEE80211_TX_CTRL_MLO_LINK); 4465 4466 __ieee80211_subif_start_xmit(skb, sdata->dev, 0, ctrl_flags, 4467 NULL); 4468 return; 4469 } 4470 4471 for_each_set_bit(link, &links, IEEE80211_MLD_MAX_NUM_LINKS) { 4472 ieee80211_mlo_multicast_tx_one(sdata, skb, ctrl_flags, link); 4473 ctrl_flags = 0; 4474 } 4475 kfree_skb(skb); 4476 } 4477 4478 /** 4479 * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs 4480 * @skb: packet to be sent 4481 * @dev: incoming interface 4482 * 4483 * On failure skb will be freed. 4484 * 4485 * Returns: the netdev TX status (but really only %NETDEV_TX_OK) 4486 */ 4487 netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb, 4488 struct net_device *dev) 4489 { 4490 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 4491 const struct ethhdr *eth = (void *)skb->data; 4492 4493 if (likely(!is_multicast_ether_addr(eth->h_dest))) 4494 goto normal; 4495 4496 if (unlikely(!ieee80211_sdata_running(sdata))) { 4497 kfree_skb(skb); 4498 return NETDEV_TX_OK; 4499 } 4500 4501 if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) { 4502 struct sk_buff_head queue; 4503 4504 __skb_queue_head_init(&queue); 4505 ieee80211_convert_to_unicast(skb, dev, &queue); 4506 while ((skb = __skb_dequeue(&queue))) 4507 __ieee80211_subif_start_xmit(skb, dev, 0, 4508 IEEE80211_TX_CTRL_MLO_LINK_UNSPEC, 4509 NULL); 4510 } else if (ieee80211_vif_is_mld(&sdata->vif) && 4511 ((sdata->vif.type == NL80211_IFTYPE_AP && 4512 !ieee80211_hw_check(&sdata->local->hw, MLO_MCAST_MULTI_LINK_TX)) || 4513 (sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 4514 !sdata->wdev.use_4addr))) { 4515 ieee80211_mlo_multicast_tx(dev, skb); 4516 } else { 4517 normal: 4518 __ieee80211_subif_start_xmit(skb, dev, 0, 4519 IEEE80211_TX_CTRL_MLO_LINK_UNSPEC, 4520 NULL); 4521 } 4522 4523 return NETDEV_TX_OK; 4524 } 4525 4526 4527 4528 static bool __ieee80211_tx_8023(struct ieee80211_sub_if_data *sdata, 4529 struct sk_buff *skb, struct sta_info *sta, 4530 bool txpending) 4531 { 4532 struct ieee80211_local *local = sdata->local; 4533 struct ieee80211_tx_control control = {}; 4534 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 4535 struct ieee80211_sta *pubsta = NULL; 4536 unsigned long flags; 4537 int q = info->hw_queue; 4538 4539 spin_lock_irqsave(&local->queue_stop_reason_lock, flags); 4540 4541 if (local->queue_stop_reasons[q] || 4542 (!txpending && !skb_queue_empty(&local->pending[q]))) { 4543 if (txpending) 4544 skb_queue_head(&local->pending[q], skb); 4545 else 4546 skb_queue_tail(&local->pending[q], skb); 4547 4548 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 4549 4550 return false; 4551 } 4552 4553 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 4554 4555 if (sta && sta->uploaded) 4556 pubsta = &sta->sta; 4557 4558 control.sta = pubsta; 4559 4560 drv_tx(local, &control, skb); 4561 4562 return true; 4563 } 4564 4565 static bool ieee80211_tx_8023(struct ieee80211_sub_if_data *sdata, 4566 struct sk_buff *skb, struct sta_info *sta, 4567 bool txpending) 4568 { 4569 struct ieee80211_local *local = sdata->local; 4570 struct sk_buff *next; 4571 bool ret = true; 4572 4573 if (ieee80211_queue_skb(local, sdata, sta, skb)) 4574 return true; 4575 4576 skb_list_walk_safe(skb, skb, next) { 4577 skb_mark_not_on_list(skb); 4578 if (!__ieee80211_tx_8023(sdata, skb, sta, txpending)) 4579 ret = false; 4580 } 4581 4582 return ret; 4583 } 4584 4585 static void ieee80211_8023_xmit(struct ieee80211_sub_if_data *sdata, 4586 struct net_device *dev, struct sta_info *sta, 4587 struct ieee80211_key *key, struct sk_buff *skb) 4588 { 4589 struct ieee80211_tx_info *info; 4590 struct ieee80211_local *local = sdata->local; 4591 struct tid_ampdu_tx *tid_tx; 4592 struct sk_buff *seg, *next; 4593 unsigned int skbs = 0, len = 0; 4594 u16 queue; 4595 u8 tid; 4596 4597 queue = ieee80211_select_queue(sdata, sta, skb); 4598 skb_set_queue_mapping(skb, queue); 4599 4600 if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning)) && 4601 test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)) 4602 goto out_free; 4603 4604 skb = skb_share_check(skb, GFP_ATOMIC); 4605 if (unlikely(!skb)) 4606 return; 4607 4608 ieee80211_aggr_check(sdata, sta, skb); 4609 4610 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK; 4611 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]); 4612 if (tid_tx) { 4613 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) { 4614 /* fall back to non-offload slow path */ 4615 __ieee80211_subif_start_xmit(skb, dev, 0, 4616 IEEE80211_TX_CTRL_MLO_LINK_UNSPEC, 4617 NULL); 4618 return; 4619 } 4620 4621 if (tid_tx->timeout) 4622 tid_tx->last_tx = jiffies; 4623 } 4624 4625 skb = ieee80211_tx_skb_fixup(skb, ieee80211_sdata_netdev_features(sdata)); 4626 if (!skb) 4627 return; 4628 4629 info = IEEE80211_SKB_CB(skb); 4630 memset(info, 0, sizeof(*info)); 4631 4632 info->hw_queue = sdata->vif.hw_queue[queue]; 4633 4634 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 4635 sdata = container_of(sdata->bss, 4636 struct ieee80211_sub_if_data, u.ap); 4637 4638 info->flags |= IEEE80211_TX_CTL_HW_80211_ENCAP; 4639 info->control.vif = &sdata->vif; 4640 4641 if (key) 4642 info->control.hw_key = &key->conf; 4643 4644 skb_list_walk_safe(skb, seg, next) { 4645 skbs++; 4646 len += seg->len; 4647 if (seg != skb) 4648 memcpy(IEEE80211_SKB_CB(seg), info, sizeof(*info)); 4649 } 4650 4651 if (unlikely(skb->sk && sock_flag(skb->sk, SOCK_WIFI_STATUS))) { 4652 info->status_data = ieee80211_store_ack_skb(local, skb, 4653 &info->flags, NULL); 4654 if (info->status_data) 4655 info->status_data_idr = 1; 4656 } 4657 4658 dev_sw_netstats_tx_add(dev, skbs, len); 4659 sta->deflink.tx_stats.packets[queue] += skbs; 4660 sta->deflink.tx_stats.bytes[queue] += len; 4661 4662 ieee80211_tpt_led_trig_tx(local, len); 4663 4664 ieee80211_tx_8023(sdata, skb, sta, false); 4665 4666 return; 4667 4668 out_free: 4669 kfree_skb(skb); 4670 } 4671 4672 netdev_tx_t ieee80211_subif_start_xmit_8023(struct sk_buff *skb, 4673 struct net_device *dev) 4674 { 4675 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 4676 struct ethhdr *ehdr = (struct ethhdr *)skb->data; 4677 struct ieee80211_key *key; 4678 struct sta_info *sta; 4679 4680 if (unlikely(!ieee80211_sdata_running(sdata) || skb->len < ETH_HLEN)) { 4681 kfree_skb(skb); 4682 return NETDEV_TX_OK; 4683 } 4684 4685 rcu_read_lock(); 4686 4687 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) { 4688 kfree_skb(skb); 4689 goto out; 4690 } 4691 4692 if (unlikely(IS_ERR_OR_NULL(sta) || !sta->uploaded || 4693 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) || 4694 sdata->control_port_protocol == ehdr->h_proto)) 4695 goto skip_offload; 4696 4697 key = rcu_dereference(sta->ptk[sta->ptk_idx]); 4698 if (!key) 4699 key = rcu_dereference(sdata->default_unicast_key); 4700 4701 if (key && (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) || 4702 key->conf.cipher == WLAN_CIPHER_SUITE_TKIP)) 4703 goto skip_offload; 4704 4705 sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift); 4706 ieee80211_8023_xmit(sdata, dev, sta, key, skb); 4707 goto out; 4708 4709 skip_offload: 4710 ieee80211_subif_start_xmit(skb, dev); 4711 out: 4712 rcu_read_unlock(); 4713 4714 return NETDEV_TX_OK; 4715 } 4716 4717 struct sk_buff * 4718 ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata, 4719 struct sk_buff *skb, u32 info_flags) 4720 { 4721 struct ieee80211_hdr *hdr; 4722 struct ieee80211_tx_data tx = { 4723 .local = sdata->local, 4724 .sdata = sdata, 4725 }; 4726 struct sta_info *sta; 4727 4728 rcu_read_lock(); 4729 4730 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) { 4731 kfree_skb(skb); 4732 skb = ERR_PTR(-EINVAL); 4733 goto out; 4734 } 4735 4736 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta, 4737 IEEE80211_TX_CTRL_MLO_LINK_UNSPEC, NULL); 4738 if (IS_ERR(skb)) 4739 goto out; 4740 4741 hdr = (void *)skb->data; 4742 tx.sta = sta_info_get(sdata, hdr->addr1); 4743 tx.skb = skb; 4744 4745 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) { 4746 rcu_read_unlock(); 4747 kfree_skb(skb); 4748 return ERR_PTR(-EINVAL); 4749 } 4750 4751 out: 4752 rcu_read_unlock(); 4753 return skb; 4754 } 4755 4756 /* 4757 * ieee80211_clear_tx_pending may not be called in a context where 4758 * it is possible that it packets could come in again. 4759 */ 4760 void ieee80211_clear_tx_pending(struct ieee80211_local *local) 4761 { 4762 struct sk_buff *skb; 4763 int i; 4764 4765 for (i = 0; i < local->hw.queues; i++) { 4766 while ((skb = skb_dequeue(&local->pending[i])) != NULL) 4767 ieee80211_free_txskb(&local->hw, skb); 4768 } 4769 } 4770 4771 /* 4772 * Returns false if the frame couldn't be transmitted but was queued instead, 4773 * which in this case means re-queued -- take as an indication to stop sending 4774 * more pending frames. 4775 */ 4776 static bool ieee80211_tx_pending_skb(struct ieee80211_local *local, 4777 struct sk_buff *skb) 4778 { 4779 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 4780 struct ieee80211_sub_if_data *sdata; 4781 struct sta_info *sta; 4782 struct ieee80211_hdr *hdr; 4783 bool result; 4784 struct ieee80211_chanctx_conf *chanctx_conf; 4785 4786 sdata = vif_to_sdata(info->control.vif); 4787 4788 if (info->control.flags & IEEE80211_TX_INTCFL_NEED_TXPROCESSING) { 4789 /* update band only for non-MLD */ 4790 if (!ieee80211_vif_is_mld(&sdata->vif)) { 4791 chanctx_conf = 4792 rcu_dereference(sdata->vif.bss_conf.chanctx_conf); 4793 if (unlikely(!chanctx_conf)) { 4794 dev_kfree_skb(skb); 4795 return true; 4796 } 4797 info->band = chanctx_conf->def.chan->band; 4798 } 4799 result = ieee80211_tx(sdata, NULL, skb, true); 4800 } else if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) { 4801 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) { 4802 dev_kfree_skb(skb); 4803 return true; 4804 } 4805 4806 if (IS_ERR(sta) || (sta && !sta->uploaded)) 4807 sta = NULL; 4808 4809 result = ieee80211_tx_8023(sdata, skb, sta, true); 4810 } else { 4811 struct sk_buff_head skbs; 4812 4813 __skb_queue_head_init(&skbs); 4814 __skb_queue_tail(&skbs, skb); 4815 4816 hdr = (struct ieee80211_hdr *)skb->data; 4817 sta = sta_info_get(sdata, hdr->addr1); 4818 4819 result = __ieee80211_tx(local, &skbs, sta, true); 4820 } 4821 4822 return result; 4823 } 4824 4825 /* 4826 * Transmit all pending packets. Called from tasklet. 4827 */ 4828 void ieee80211_tx_pending(struct tasklet_struct *t) 4829 { 4830 struct ieee80211_local *local = from_tasklet(local, t, 4831 tx_pending_tasklet); 4832 unsigned long flags; 4833 int i; 4834 bool txok; 4835 4836 rcu_read_lock(); 4837 4838 spin_lock_irqsave(&local->queue_stop_reason_lock, flags); 4839 for (i = 0; i < local->hw.queues; i++) { 4840 /* 4841 * If queue is stopped by something other than due to pending 4842 * frames, or we have no pending frames, proceed to next queue. 4843 */ 4844 if (local->queue_stop_reasons[i] || 4845 skb_queue_empty(&local->pending[i])) 4846 continue; 4847 4848 while (!skb_queue_empty(&local->pending[i])) { 4849 struct sk_buff *skb = __skb_dequeue(&local->pending[i]); 4850 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 4851 4852 if (WARN_ON(!info->control.vif)) { 4853 ieee80211_free_txskb(&local->hw, skb); 4854 continue; 4855 } 4856 4857 spin_unlock_irqrestore(&local->queue_stop_reason_lock, 4858 flags); 4859 4860 txok = ieee80211_tx_pending_skb(local, skb); 4861 spin_lock_irqsave(&local->queue_stop_reason_lock, 4862 flags); 4863 if (!txok) 4864 break; 4865 } 4866 } 4867 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 4868 4869 rcu_read_unlock(); 4870 } 4871 4872 /* functions for drivers to get certain frames */ 4873 4874 static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata, 4875 struct ieee80211_link_data *link, 4876 struct ps_data *ps, struct sk_buff *skb, 4877 bool is_template) 4878 { 4879 u8 *pos, *tim; 4880 int aid0 = 0; 4881 int i, have_bits = 0, n1, n2; 4882 struct ieee80211_bss_conf *link_conf = link->conf; 4883 4884 /* Generate bitmap for TIM only if there are any STAs in power save 4885 * mode. */ 4886 if (atomic_read(&ps->num_sta_ps) > 0) 4887 /* in the hope that this is faster than 4888 * checking byte-for-byte */ 4889 have_bits = !bitmap_empty((unsigned long *)ps->tim, 4890 IEEE80211_MAX_AID+1); 4891 if (!is_template) { 4892 if (ps->dtim_count == 0) 4893 ps->dtim_count = link_conf->dtim_period - 1; 4894 else 4895 ps->dtim_count--; 4896 } 4897 4898 tim = pos = skb_put(skb, 5); 4899 *pos++ = WLAN_EID_TIM; 4900 *pos++ = 3; 4901 *pos++ = ps->dtim_count; 4902 *pos++ = link_conf->dtim_period; 4903 4904 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf)) 4905 aid0 = 1; 4906 4907 ps->dtim_bc_mc = aid0 == 1; 4908 4909 if (have_bits) { 4910 /* Find largest even number N1 so that bits numbered 1 through 4911 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits 4912 * (N2 + 1) x 8 through 2007 are 0. */ 4913 n1 = 0; 4914 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) { 4915 if (ps->tim[i]) { 4916 n1 = i & 0xfe; 4917 break; 4918 } 4919 } 4920 n2 = n1; 4921 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) { 4922 if (ps->tim[i]) { 4923 n2 = i; 4924 break; 4925 } 4926 } 4927 4928 /* Bitmap control */ 4929 *pos++ = n1 | aid0; 4930 /* Part Virt Bitmap */ 4931 skb_put_data(skb, ps->tim + n1, n2 - n1 + 1); 4932 4933 tim[1] = n2 - n1 + 4; 4934 } else { 4935 *pos++ = aid0; /* Bitmap control */ 4936 4937 if (ieee80211_get_link_sband(link)->band != NL80211_BAND_S1GHZ) { 4938 tim[1] = 4; 4939 /* Part Virt Bitmap */ 4940 skb_put_u8(skb, 0); 4941 } 4942 } 4943 } 4944 4945 static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata, 4946 struct ieee80211_link_data *link, 4947 struct ps_data *ps, struct sk_buff *skb, 4948 bool is_template) 4949 { 4950 struct ieee80211_local *local = sdata->local; 4951 4952 /* 4953 * Not very nice, but we want to allow the driver to call 4954 * ieee80211_beacon_get() as a response to the set_tim() 4955 * callback. That, however, is already invoked under the 4956 * sta_lock to guarantee consistent and race-free update 4957 * of the tim bitmap in mac80211 and the driver. 4958 */ 4959 if (local->tim_in_locked_section) { 4960 __ieee80211_beacon_add_tim(sdata, link, ps, skb, is_template); 4961 } else { 4962 spin_lock_bh(&local->tim_lock); 4963 __ieee80211_beacon_add_tim(sdata, link, ps, skb, is_template); 4964 spin_unlock_bh(&local->tim_lock); 4965 } 4966 4967 return 0; 4968 } 4969 4970 static void ieee80211_set_beacon_cntdwn(struct ieee80211_sub_if_data *sdata, 4971 struct beacon_data *beacon, 4972 struct ieee80211_link_data *link) 4973 { 4974 u8 *beacon_data, count, max_count = 1; 4975 struct probe_resp *resp; 4976 size_t beacon_data_len; 4977 u16 *bcn_offsets; 4978 int i; 4979 4980 switch (sdata->vif.type) { 4981 case NL80211_IFTYPE_AP: 4982 beacon_data = beacon->tail; 4983 beacon_data_len = beacon->tail_len; 4984 break; 4985 case NL80211_IFTYPE_ADHOC: 4986 beacon_data = beacon->head; 4987 beacon_data_len = beacon->head_len; 4988 break; 4989 case NL80211_IFTYPE_MESH_POINT: 4990 beacon_data = beacon->head; 4991 beacon_data_len = beacon->head_len; 4992 break; 4993 default: 4994 return; 4995 } 4996 4997 resp = rcu_dereference(link->u.ap.probe_resp); 4998 4999 bcn_offsets = beacon->cntdwn_counter_offsets; 5000 count = beacon->cntdwn_current_counter; 5001 if (link->conf->csa_active) 5002 max_count = IEEE80211_MAX_CNTDWN_COUNTERS_NUM; 5003 5004 for (i = 0; i < max_count; ++i) { 5005 if (bcn_offsets[i]) { 5006 if (WARN_ON_ONCE(bcn_offsets[i] >= beacon_data_len)) 5007 return; 5008 beacon_data[bcn_offsets[i]] = count; 5009 } 5010 5011 if (sdata->vif.type == NL80211_IFTYPE_AP && resp) { 5012 u16 *resp_offsets = resp->cntdwn_counter_offsets; 5013 5014 resp->data[resp_offsets[i]] = count; 5015 } 5016 } 5017 } 5018 5019 static u8 __ieee80211_beacon_update_cntdwn(struct beacon_data *beacon) 5020 { 5021 beacon->cntdwn_current_counter--; 5022 5023 /* the counter should never reach 0 */ 5024 WARN_ON_ONCE(!beacon->cntdwn_current_counter); 5025 5026 return beacon->cntdwn_current_counter; 5027 } 5028 5029 u8 ieee80211_beacon_update_cntdwn(struct ieee80211_vif *vif, unsigned int link_id) 5030 { 5031 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 5032 struct ieee80211_link_data *link; 5033 struct beacon_data *beacon = NULL; 5034 u8 count = 0; 5035 5036 if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS)) 5037 return 0; 5038 5039 rcu_read_lock(); 5040 5041 link = rcu_dereference(sdata->link[link_id]); 5042 if (!link) 5043 goto unlock; 5044 5045 if (sdata->vif.type == NL80211_IFTYPE_AP) 5046 beacon = rcu_dereference(link->u.ap.beacon); 5047 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) 5048 beacon = rcu_dereference(sdata->u.ibss.presp); 5049 else if (ieee80211_vif_is_mesh(&sdata->vif)) 5050 beacon = rcu_dereference(sdata->u.mesh.beacon); 5051 5052 if (!beacon) 5053 goto unlock; 5054 5055 count = __ieee80211_beacon_update_cntdwn(beacon); 5056 5057 unlock: 5058 rcu_read_unlock(); 5059 return count; 5060 } 5061 EXPORT_SYMBOL(ieee80211_beacon_update_cntdwn); 5062 5063 void ieee80211_beacon_set_cntdwn(struct ieee80211_vif *vif, u8 counter) 5064 { 5065 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 5066 struct beacon_data *beacon = NULL; 5067 5068 rcu_read_lock(); 5069 5070 if (sdata->vif.type == NL80211_IFTYPE_AP) 5071 beacon = rcu_dereference(sdata->deflink.u.ap.beacon); 5072 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) 5073 beacon = rcu_dereference(sdata->u.ibss.presp); 5074 else if (ieee80211_vif_is_mesh(&sdata->vif)) 5075 beacon = rcu_dereference(sdata->u.mesh.beacon); 5076 5077 if (!beacon) 5078 goto unlock; 5079 5080 if (counter < beacon->cntdwn_current_counter) 5081 beacon->cntdwn_current_counter = counter; 5082 5083 unlock: 5084 rcu_read_unlock(); 5085 } 5086 EXPORT_SYMBOL(ieee80211_beacon_set_cntdwn); 5087 5088 bool ieee80211_beacon_cntdwn_is_complete(struct ieee80211_vif *vif, 5089 unsigned int link_id) 5090 { 5091 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 5092 struct ieee80211_link_data *link; 5093 struct beacon_data *beacon = NULL; 5094 u8 *beacon_data; 5095 size_t beacon_data_len; 5096 int ret = false; 5097 5098 if (!ieee80211_sdata_running(sdata)) 5099 return false; 5100 5101 if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS)) 5102 return 0; 5103 5104 rcu_read_lock(); 5105 5106 link = rcu_dereference(sdata->link[link_id]); 5107 if (!link) 5108 goto out; 5109 5110 if (vif->type == NL80211_IFTYPE_AP) { 5111 beacon = rcu_dereference(link->u.ap.beacon); 5112 if (WARN_ON(!beacon || !beacon->tail)) 5113 goto out; 5114 beacon_data = beacon->tail; 5115 beacon_data_len = beacon->tail_len; 5116 } else if (vif->type == NL80211_IFTYPE_ADHOC) { 5117 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; 5118 5119 beacon = rcu_dereference(ifibss->presp); 5120 if (!beacon) 5121 goto out; 5122 5123 beacon_data = beacon->head; 5124 beacon_data_len = beacon->head_len; 5125 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) { 5126 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; 5127 5128 beacon = rcu_dereference(ifmsh->beacon); 5129 if (!beacon) 5130 goto out; 5131 5132 beacon_data = beacon->head; 5133 beacon_data_len = beacon->head_len; 5134 } else { 5135 WARN_ON(1); 5136 goto out; 5137 } 5138 5139 if (!beacon->cntdwn_counter_offsets[0]) 5140 goto out; 5141 5142 if (WARN_ON_ONCE(beacon->cntdwn_counter_offsets[0] > beacon_data_len)) 5143 goto out; 5144 5145 if (beacon_data[beacon->cntdwn_counter_offsets[0]] == 1) 5146 ret = true; 5147 5148 out: 5149 rcu_read_unlock(); 5150 5151 return ret; 5152 } 5153 EXPORT_SYMBOL(ieee80211_beacon_cntdwn_is_complete); 5154 5155 static int ieee80211_beacon_protect(struct sk_buff *skb, 5156 struct ieee80211_local *local, 5157 struct ieee80211_sub_if_data *sdata, 5158 struct ieee80211_link_data *link) 5159 { 5160 ieee80211_tx_result res; 5161 struct ieee80211_tx_data tx; 5162 struct sk_buff *check_skb; 5163 5164 memset(&tx, 0, sizeof(tx)); 5165 tx.key = rcu_dereference(link->default_beacon_key); 5166 if (!tx.key) 5167 return 0; 5168 5169 if (unlikely(tx.key->flags & KEY_FLAG_TAINTED)) { 5170 tx.key = NULL; 5171 return -EINVAL; 5172 } 5173 5174 if (!(tx.key->conf.flags & IEEE80211_KEY_FLAG_SW_MGMT_TX) && 5175 tx.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) 5176 IEEE80211_SKB_CB(skb)->control.hw_key = &tx.key->conf; 5177 5178 tx.local = local; 5179 tx.sdata = sdata; 5180 __skb_queue_head_init(&tx.skbs); 5181 __skb_queue_tail(&tx.skbs, skb); 5182 res = ieee80211_tx_h_encrypt(&tx); 5183 check_skb = __skb_dequeue(&tx.skbs); 5184 /* we may crash after this, but it'd be a bug in crypto */ 5185 WARN_ON(check_skb != skb); 5186 if (WARN_ON_ONCE(res != TX_CONTINUE)) 5187 return -EINVAL; 5188 5189 return 0; 5190 } 5191 5192 static void 5193 ieee80211_beacon_get_finish(struct ieee80211_hw *hw, 5194 struct ieee80211_vif *vif, 5195 struct ieee80211_link_data *link, 5196 struct ieee80211_mutable_offsets *offs, 5197 struct beacon_data *beacon, 5198 struct sk_buff *skb, 5199 struct ieee80211_chanctx_conf *chanctx_conf, 5200 u16 csa_off_base) 5201 { 5202 struct ieee80211_local *local = hw_to_local(hw); 5203 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 5204 struct ieee80211_tx_info *info; 5205 enum nl80211_band band; 5206 struct ieee80211_tx_rate_control txrc; 5207 5208 /* CSA offsets */ 5209 if (offs && beacon) { 5210 u16 i; 5211 5212 for (i = 0; i < IEEE80211_MAX_CNTDWN_COUNTERS_NUM; i++) { 5213 u16 csa_off = beacon->cntdwn_counter_offsets[i]; 5214 5215 if (!csa_off) 5216 continue; 5217 5218 offs->cntdwn_counter_offs[i] = csa_off_base + csa_off; 5219 } 5220 } 5221 5222 band = chanctx_conf->def.chan->band; 5223 info = IEEE80211_SKB_CB(skb); 5224 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT; 5225 info->flags |= IEEE80211_TX_CTL_NO_ACK; 5226 info->band = band; 5227 5228 memset(&txrc, 0, sizeof(txrc)); 5229 txrc.hw = hw; 5230 txrc.sband = local->hw.wiphy->bands[band]; 5231 txrc.bss_conf = link->conf; 5232 txrc.skb = skb; 5233 txrc.reported_rate.idx = -1; 5234 if (sdata->beacon_rate_set && sdata->beacon_rateidx_mask[band]) 5235 txrc.rate_idx_mask = sdata->beacon_rateidx_mask[band]; 5236 else 5237 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band]; 5238 txrc.bss = true; 5239 rate_control_get_rate(sdata, NULL, &txrc); 5240 5241 info->control.vif = vif; 5242 info->control.flags |= u32_encode_bits(link->link_id, 5243 IEEE80211_TX_CTRL_MLO_LINK); 5244 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT | 5245 IEEE80211_TX_CTL_ASSIGN_SEQ | 5246 IEEE80211_TX_CTL_FIRST_FRAGMENT; 5247 } 5248 5249 static void 5250 ieee80211_beacon_add_mbssid(struct sk_buff *skb, struct beacon_data *beacon, 5251 u8 i) 5252 { 5253 if (!beacon->mbssid_ies || !beacon->mbssid_ies->cnt || 5254 i > beacon->mbssid_ies->cnt) 5255 return; 5256 5257 if (i < beacon->mbssid_ies->cnt) { 5258 skb_put_data(skb, beacon->mbssid_ies->elem[i].data, 5259 beacon->mbssid_ies->elem[i].len); 5260 5261 if (beacon->rnr_ies && beacon->rnr_ies->cnt) { 5262 skb_put_data(skb, beacon->rnr_ies->elem[i].data, 5263 beacon->rnr_ies->elem[i].len); 5264 5265 for (i = beacon->mbssid_ies->cnt; i < beacon->rnr_ies->cnt; i++) 5266 skb_put_data(skb, beacon->rnr_ies->elem[i].data, 5267 beacon->rnr_ies->elem[i].len); 5268 } 5269 return; 5270 } 5271 5272 /* i == beacon->mbssid_ies->cnt, include all MBSSID elements */ 5273 for (i = 0; i < beacon->mbssid_ies->cnt; i++) 5274 skb_put_data(skb, beacon->mbssid_ies->elem[i].data, 5275 beacon->mbssid_ies->elem[i].len); 5276 } 5277 5278 static struct sk_buff * 5279 ieee80211_beacon_get_ap(struct ieee80211_hw *hw, 5280 struct ieee80211_vif *vif, 5281 struct ieee80211_link_data *link, 5282 struct ieee80211_mutable_offsets *offs, 5283 bool is_template, 5284 struct beacon_data *beacon, 5285 struct ieee80211_chanctx_conf *chanctx_conf, 5286 u8 ema_index) 5287 { 5288 struct ieee80211_local *local = hw_to_local(hw); 5289 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 5290 struct ieee80211_if_ap *ap = &sdata->u.ap; 5291 struct sk_buff *skb = NULL; 5292 u16 csa_off_base = 0; 5293 int mbssid_len; 5294 5295 if (beacon->cntdwn_counter_offsets[0]) { 5296 if (!is_template) 5297 ieee80211_beacon_update_cntdwn(vif, link->link_id); 5298 5299 ieee80211_set_beacon_cntdwn(sdata, beacon, link); 5300 } 5301 5302 /* headroom, head length, 5303 * tail length, maximum TIM length and multiple BSSID length 5304 */ 5305 mbssid_len = ieee80211_get_mbssid_beacon_len(beacon->mbssid_ies, 5306 beacon->rnr_ies, 5307 ema_index); 5308 5309 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len + 5310 beacon->tail_len + 256 + 5311 local->hw.extra_beacon_tailroom + mbssid_len); 5312 if (!skb) 5313 return NULL; 5314 5315 skb_reserve(skb, local->tx_headroom); 5316 skb_put_data(skb, beacon->head, beacon->head_len); 5317 5318 ieee80211_beacon_add_tim(sdata, link, &ap->ps, skb, is_template); 5319 5320 if (offs) { 5321 offs->tim_offset = beacon->head_len; 5322 offs->tim_length = skb->len - beacon->head_len; 5323 offs->cntdwn_counter_offs[0] = beacon->cntdwn_counter_offsets[0]; 5324 5325 if (mbssid_len) { 5326 ieee80211_beacon_add_mbssid(skb, beacon, ema_index); 5327 offs->mbssid_off = skb->len - mbssid_len; 5328 } 5329 5330 /* for AP the csa offsets are from tail */ 5331 csa_off_base = skb->len; 5332 } 5333 5334 if (beacon->tail) 5335 skb_put_data(skb, beacon->tail, beacon->tail_len); 5336 5337 if (ieee80211_beacon_protect(skb, local, sdata, link) < 0) { 5338 dev_kfree_skb(skb); 5339 return NULL; 5340 } 5341 5342 ieee80211_beacon_get_finish(hw, vif, link, offs, beacon, skb, 5343 chanctx_conf, csa_off_base); 5344 return skb; 5345 } 5346 5347 static struct ieee80211_ema_beacons * 5348 ieee80211_beacon_get_ap_ema_list(struct ieee80211_hw *hw, 5349 struct ieee80211_vif *vif, 5350 struct ieee80211_link_data *link, 5351 struct ieee80211_mutable_offsets *offs, 5352 bool is_template, struct beacon_data *beacon, 5353 struct ieee80211_chanctx_conf *chanctx_conf) 5354 { 5355 struct ieee80211_ema_beacons *ema = NULL; 5356 5357 if (!beacon->mbssid_ies || !beacon->mbssid_ies->cnt) 5358 return NULL; 5359 5360 ema = kzalloc(struct_size(ema, bcn, beacon->mbssid_ies->cnt), 5361 GFP_ATOMIC); 5362 if (!ema) 5363 return NULL; 5364 5365 for (ema->cnt = 0; ema->cnt < beacon->mbssid_ies->cnt; ema->cnt++) { 5366 ema->bcn[ema->cnt].skb = 5367 ieee80211_beacon_get_ap(hw, vif, link, 5368 &ema->bcn[ema->cnt].offs, 5369 is_template, beacon, 5370 chanctx_conf, ema->cnt); 5371 if (!ema->bcn[ema->cnt].skb) 5372 break; 5373 } 5374 5375 if (ema->cnt == beacon->mbssid_ies->cnt) 5376 return ema; 5377 5378 ieee80211_beacon_free_ema_list(ema); 5379 return NULL; 5380 } 5381 5382 #define IEEE80211_INCLUDE_ALL_MBSSID_ELEMS -1 5383 5384 static struct sk_buff * 5385 __ieee80211_beacon_get(struct ieee80211_hw *hw, 5386 struct ieee80211_vif *vif, 5387 struct ieee80211_mutable_offsets *offs, 5388 bool is_template, 5389 unsigned int link_id, 5390 int ema_index, 5391 struct ieee80211_ema_beacons **ema_beacons) 5392 { 5393 struct ieee80211_local *local = hw_to_local(hw); 5394 struct beacon_data *beacon = NULL; 5395 struct sk_buff *skb = NULL; 5396 struct ieee80211_sub_if_data *sdata = NULL; 5397 struct ieee80211_chanctx_conf *chanctx_conf; 5398 struct ieee80211_link_data *link; 5399 5400 rcu_read_lock(); 5401 5402 sdata = vif_to_sdata(vif); 5403 link = rcu_dereference(sdata->link[link_id]); 5404 if (!link) 5405 goto out; 5406 chanctx_conf = 5407 rcu_dereference(link->conf->chanctx_conf); 5408 5409 if (!ieee80211_sdata_running(sdata) || !chanctx_conf) 5410 goto out; 5411 5412 if (offs) 5413 memset(offs, 0, sizeof(*offs)); 5414 5415 if (sdata->vif.type == NL80211_IFTYPE_AP) { 5416 beacon = rcu_dereference(link->u.ap.beacon); 5417 if (!beacon) 5418 goto out; 5419 5420 if (ema_beacons) { 5421 *ema_beacons = 5422 ieee80211_beacon_get_ap_ema_list(hw, vif, link, 5423 offs, 5424 is_template, 5425 beacon, 5426 chanctx_conf); 5427 } else { 5428 if (beacon->mbssid_ies && beacon->mbssid_ies->cnt) { 5429 if (ema_index >= beacon->mbssid_ies->cnt) 5430 goto out; /* End of MBSSID elements */ 5431 5432 if (ema_index <= IEEE80211_INCLUDE_ALL_MBSSID_ELEMS) 5433 ema_index = beacon->mbssid_ies->cnt; 5434 } else { 5435 ema_index = 0; 5436 } 5437 5438 skb = ieee80211_beacon_get_ap(hw, vif, link, offs, 5439 is_template, beacon, 5440 chanctx_conf, 5441 ema_index); 5442 } 5443 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { 5444 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; 5445 struct ieee80211_hdr *hdr; 5446 5447 beacon = rcu_dereference(ifibss->presp); 5448 if (!beacon) 5449 goto out; 5450 5451 if (beacon->cntdwn_counter_offsets[0]) { 5452 if (!is_template) 5453 __ieee80211_beacon_update_cntdwn(beacon); 5454 5455 ieee80211_set_beacon_cntdwn(sdata, beacon, link); 5456 } 5457 5458 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len + 5459 local->hw.extra_beacon_tailroom); 5460 if (!skb) 5461 goto out; 5462 skb_reserve(skb, local->tx_headroom); 5463 skb_put_data(skb, beacon->head, beacon->head_len); 5464 5465 hdr = (struct ieee80211_hdr *) skb->data; 5466 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | 5467 IEEE80211_STYPE_BEACON); 5468 5469 ieee80211_beacon_get_finish(hw, vif, link, offs, beacon, skb, 5470 chanctx_conf, 0); 5471 } else if (ieee80211_vif_is_mesh(&sdata->vif)) { 5472 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; 5473 5474 beacon = rcu_dereference(ifmsh->beacon); 5475 if (!beacon) 5476 goto out; 5477 5478 if (beacon->cntdwn_counter_offsets[0]) { 5479 if (!is_template) 5480 /* TODO: For mesh csa_counter is in TU, so 5481 * decrementing it by one isn't correct, but 5482 * for now we leave it consistent with overall 5483 * mac80211's behavior. 5484 */ 5485 __ieee80211_beacon_update_cntdwn(beacon); 5486 5487 ieee80211_set_beacon_cntdwn(sdata, beacon, link); 5488 } 5489 5490 if (ifmsh->sync_ops) 5491 ifmsh->sync_ops->adjust_tsf(sdata, beacon); 5492 5493 skb = dev_alloc_skb(local->tx_headroom + 5494 beacon->head_len + 5495 256 + /* TIM IE */ 5496 beacon->tail_len + 5497 local->hw.extra_beacon_tailroom); 5498 if (!skb) 5499 goto out; 5500 skb_reserve(skb, local->tx_headroom); 5501 skb_put_data(skb, beacon->head, beacon->head_len); 5502 ieee80211_beacon_add_tim(sdata, link, &ifmsh->ps, skb, 5503 is_template); 5504 5505 if (offs) { 5506 offs->tim_offset = beacon->head_len; 5507 offs->tim_length = skb->len - beacon->head_len; 5508 } 5509 5510 skb_put_data(skb, beacon->tail, beacon->tail_len); 5511 ieee80211_beacon_get_finish(hw, vif, link, offs, beacon, skb, 5512 chanctx_conf, 0); 5513 } else { 5514 WARN_ON(1); 5515 goto out; 5516 } 5517 5518 out: 5519 rcu_read_unlock(); 5520 return skb; 5521 5522 } 5523 5524 struct sk_buff * 5525 ieee80211_beacon_get_template(struct ieee80211_hw *hw, 5526 struct ieee80211_vif *vif, 5527 struct ieee80211_mutable_offsets *offs, 5528 unsigned int link_id) 5529 { 5530 return __ieee80211_beacon_get(hw, vif, offs, true, link_id, 5531 IEEE80211_INCLUDE_ALL_MBSSID_ELEMS, NULL); 5532 } 5533 EXPORT_SYMBOL(ieee80211_beacon_get_template); 5534 5535 struct sk_buff * 5536 ieee80211_beacon_get_template_ema_index(struct ieee80211_hw *hw, 5537 struct ieee80211_vif *vif, 5538 struct ieee80211_mutable_offsets *offs, 5539 unsigned int link_id, u8 ema_index) 5540 { 5541 return __ieee80211_beacon_get(hw, vif, offs, true, link_id, ema_index, 5542 NULL); 5543 } 5544 EXPORT_SYMBOL(ieee80211_beacon_get_template_ema_index); 5545 5546 void ieee80211_beacon_free_ema_list(struct ieee80211_ema_beacons *ema_beacons) 5547 { 5548 u8 i; 5549 5550 if (!ema_beacons) 5551 return; 5552 5553 for (i = 0; i < ema_beacons->cnt; i++) 5554 kfree_skb(ema_beacons->bcn[i].skb); 5555 5556 kfree(ema_beacons); 5557 } 5558 EXPORT_SYMBOL(ieee80211_beacon_free_ema_list); 5559 5560 struct ieee80211_ema_beacons * 5561 ieee80211_beacon_get_template_ema_list(struct ieee80211_hw *hw, 5562 struct ieee80211_vif *vif, 5563 unsigned int link_id) 5564 { 5565 struct ieee80211_ema_beacons *ema_beacons = NULL; 5566 5567 WARN_ON(__ieee80211_beacon_get(hw, vif, NULL, true, link_id, 0, 5568 &ema_beacons)); 5569 5570 return ema_beacons; 5571 } 5572 EXPORT_SYMBOL(ieee80211_beacon_get_template_ema_list); 5573 5574 struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw, 5575 struct ieee80211_vif *vif, 5576 u16 *tim_offset, u16 *tim_length, 5577 unsigned int link_id) 5578 { 5579 struct ieee80211_mutable_offsets offs = {}; 5580 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false, 5581 link_id, 5582 IEEE80211_INCLUDE_ALL_MBSSID_ELEMS, 5583 NULL); 5584 struct sk_buff *copy; 5585 5586 if (!bcn) 5587 return bcn; 5588 5589 if (tim_offset) 5590 *tim_offset = offs.tim_offset; 5591 5592 if (tim_length) 5593 *tim_length = offs.tim_length; 5594 5595 if (ieee80211_hw_check(hw, BEACON_TX_STATUS) || 5596 !hw_to_local(hw)->monitors) 5597 return bcn; 5598 5599 /* send a copy to monitor interfaces */ 5600 copy = skb_copy(bcn, GFP_ATOMIC); 5601 if (!copy) 5602 return bcn; 5603 5604 ieee80211_tx_monitor(hw_to_local(hw), copy, 1, NULL); 5605 5606 return bcn; 5607 } 5608 EXPORT_SYMBOL(ieee80211_beacon_get_tim); 5609 5610 struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw, 5611 struct ieee80211_vif *vif) 5612 { 5613 struct sk_buff *skb = NULL; 5614 struct probe_resp *presp = NULL; 5615 struct ieee80211_hdr *hdr; 5616 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 5617 5618 if (sdata->vif.type != NL80211_IFTYPE_AP) 5619 return NULL; 5620 5621 rcu_read_lock(); 5622 presp = rcu_dereference(sdata->deflink.u.ap.probe_resp); 5623 if (!presp) 5624 goto out; 5625 5626 skb = dev_alloc_skb(presp->len); 5627 if (!skb) 5628 goto out; 5629 5630 skb_put_data(skb, presp->data, presp->len); 5631 5632 hdr = (struct ieee80211_hdr *) skb->data; 5633 memset(hdr->addr1, 0, sizeof(hdr->addr1)); 5634 5635 out: 5636 rcu_read_unlock(); 5637 return skb; 5638 } 5639 EXPORT_SYMBOL(ieee80211_proberesp_get); 5640 5641 struct sk_buff *ieee80211_get_fils_discovery_tmpl(struct ieee80211_hw *hw, 5642 struct ieee80211_vif *vif) 5643 { 5644 struct sk_buff *skb = NULL; 5645 struct fils_discovery_data *tmpl = NULL; 5646 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 5647 5648 if (sdata->vif.type != NL80211_IFTYPE_AP) 5649 return NULL; 5650 5651 rcu_read_lock(); 5652 tmpl = rcu_dereference(sdata->deflink.u.ap.fils_discovery); 5653 if (!tmpl) { 5654 rcu_read_unlock(); 5655 return NULL; 5656 } 5657 5658 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len); 5659 if (skb) { 5660 skb_reserve(skb, sdata->local->hw.extra_tx_headroom); 5661 skb_put_data(skb, tmpl->data, tmpl->len); 5662 } 5663 5664 rcu_read_unlock(); 5665 return skb; 5666 } 5667 EXPORT_SYMBOL(ieee80211_get_fils_discovery_tmpl); 5668 5669 struct sk_buff * 5670 ieee80211_get_unsol_bcast_probe_resp_tmpl(struct ieee80211_hw *hw, 5671 struct ieee80211_vif *vif) 5672 { 5673 struct sk_buff *skb = NULL; 5674 struct unsol_bcast_probe_resp_data *tmpl = NULL; 5675 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 5676 5677 if (sdata->vif.type != NL80211_IFTYPE_AP) 5678 return NULL; 5679 5680 rcu_read_lock(); 5681 tmpl = rcu_dereference(sdata->deflink.u.ap.unsol_bcast_probe_resp); 5682 if (!tmpl) { 5683 rcu_read_unlock(); 5684 return NULL; 5685 } 5686 5687 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len); 5688 if (skb) { 5689 skb_reserve(skb, sdata->local->hw.extra_tx_headroom); 5690 skb_put_data(skb, tmpl->data, tmpl->len); 5691 } 5692 5693 rcu_read_unlock(); 5694 return skb; 5695 } 5696 EXPORT_SYMBOL(ieee80211_get_unsol_bcast_probe_resp_tmpl); 5697 5698 struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw, 5699 struct ieee80211_vif *vif) 5700 { 5701 struct ieee80211_sub_if_data *sdata; 5702 struct ieee80211_pspoll *pspoll; 5703 struct ieee80211_local *local; 5704 struct sk_buff *skb; 5705 5706 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION)) 5707 return NULL; 5708 5709 sdata = vif_to_sdata(vif); 5710 local = sdata->local; 5711 5712 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll)); 5713 if (!skb) 5714 return NULL; 5715 5716 skb_reserve(skb, local->hw.extra_tx_headroom); 5717 5718 pspoll = skb_put_zero(skb, sizeof(*pspoll)); 5719 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL | 5720 IEEE80211_STYPE_PSPOLL); 5721 pspoll->aid = cpu_to_le16(sdata->vif.cfg.aid); 5722 5723 /* aid in PS-Poll has its two MSBs each set to 1 */ 5724 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14); 5725 5726 memcpy(pspoll->bssid, sdata->deflink.u.mgd.bssid, ETH_ALEN); 5727 memcpy(pspoll->ta, vif->addr, ETH_ALEN); 5728 5729 return skb; 5730 } 5731 EXPORT_SYMBOL(ieee80211_pspoll_get); 5732 5733 struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw, 5734 struct ieee80211_vif *vif, 5735 int link_id, bool qos_ok) 5736 { 5737 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 5738 struct ieee80211_local *local = sdata->local; 5739 struct ieee80211_link_data *link = NULL; 5740 struct ieee80211_hdr_3addr *nullfunc; 5741 struct sk_buff *skb; 5742 bool qos = false; 5743 5744 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION)) 5745 return NULL; 5746 5747 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 5748 sizeof(*nullfunc) + 2); 5749 if (!skb) 5750 return NULL; 5751 5752 rcu_read_lock(); 5753 if (qos_ok) { 5754 struct sta_info *sta; 5755 5756 sta = sta_info_get(sdata, vif->cfg.ap_addr); 5757 qos = sta && sta->sta.wme; 5758 } 5759 5760 if (link_id >= 0) { 5761 link = rcu_dereference(sdata->link[link_id]); 5762 if (WARN_ON_ONCE(!link)) { 5763 rcu_read_unlock(); 5764 kfree_skb(skb); 5765 return NULL; 5766 } 5767 } 5768 5769 skb_reserve(skb, local->hw.extra_tx_headroom); 5770 5771 nullfunc = skb_put_zero(skb, sizeof(*nullfunc)); 5772 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA | 5773 IEEE80211_STYPE_NULLFUNC | 5774 IEEE80211_FCTL_TODS); 5775 if (qos) { 5776 __le16 qoshdr = cpu_to_le16(7); 5777 5778 BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC | 5779 IEEE80211_STYPE_NULLFUNC) != 5780 IEEE80211_STYPE_QOS_NULLFUNC); 5781 nullfunc->frame_control |= 5782 cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC); 5783 skb->priority = 7; 5784 skb_set_queue_mapping(skb, IEEE80211_AC_VO); 5785 skb_put_data(skb, &qoshdr, sizeof(qoshdr)); 5786 } 5787 5788 if (link) { 5789 memcpy(nullfunc->addr1, link->conf->bssid, ETH_ALEN); 5790 memcpy(nullfunc->addr2, link->conf->addr, ETH_ALEN); 5791 memcpy(nullfunc->addr3, link->conf->bssid, ETH_ALEN); 5792 } else { 5793 memcpy(nullfunc->addr1, vif->cfg.ap_addr, ETH_ALEN); 5794 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN); 5795 memcpy(nullfunc->addr3, vif->cfg.ap_addr, ETH_ALEN); 5796 } 5797 rcu_read_unlock(); 5798 5799 return skb; 5800 } 5801 EXPORT_SYMBOL(ieee80211_nullfunc_get); 5802 5803 struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw, 5804 const u8 *src_addr, 5805 const u8 *ssid, size_t ssid_len, 5806 size_t tailroom) 5807 { 5808 struct ieee80211_local *local = hw_to_local(hw); 5809 struct ieee80211_hdr_3addr *hdr; 5810 struct sk_buff *skb; 5811 size_t ie_ssid_len; 5812 u8 *pos; 5813 5814 ie_ssid_len = 2 + ssid_len; 5815 5816 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) + 5817 ie_ssid_len + tailroom); 5818 if (!skb) 5819 return NULL; 5820 5821 skb_reserve(skb, local->hw.extra_tx_headroom); 5822 5823 hdr = skb_put_zero(skb, sizeof(*hdr)); 5824 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | 5825 IEEE80211_STYPE_PROBE_REQ); 5826 eth_broadcast_addr(hdr->addr1); 5827 memcpy(hdr->addr2, src_addr, ETH_ALEN); 5828 eth_broadcast_addr(hdr->addr3); 5829 5830 pos = skb_put(skb, ie_ssid_len); 5831 *pos++ = WLAN_EID_SSID; 5832 *pos++ = ssid_len; 5833 if (ssid_len) 5834 memcpy(pos, ssid, ssid_len); 5835 pos += ssid_len; 5836 5837 return skb; 5838 } 5839 EXPORT_SYMBOL(ieee80211_probereq_get); 5840 5841 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 5842 const void *frame, size_t frame_len, 5843 const struct ieee80211_tx_info *frame_txctl, 5844 struct ieee80211_rts *rts) 5845 { 5846 const struct ieee80211_hdr *hdr = frame; 5847 5848 rts->frame_control = 5849 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS); 5850 rts->duration = ieee80211_rts_duration(hw, vif, frame_len, 5851 frame_txctl); 5852 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra)); 5853 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta)); 5854 } 5855 EXPORT_SYMBOL(ieee80211_rts_get); 5856 5857 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 5858 const void *frame, size_t frame_len, 5859 const struct ieee80211_tx_info *frame_txctl, 5860 struct ieee80211_cts *cts) 5861 { 5862 const struct ieee80211_hdr *hdr = frame; 5863 5864 cts->frame_control = 5865 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS); 5866 cts->duration = ieee80211_ctstoself_duration(hw, vif, 5867 frame_len, frame_txctl); 5868 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra)); 5869 } 5870 EXPORT_SYMBOL(ieee80211_ctstoself_get); 5871 5872 struct sk_buff * 5873 ieee80211_get_buffered_bc(struct ieee80211_hw *hw, 5874 struct ieee80211_vif *vif) 5875 { 5876 struct ieee80211_local *local = hw_to_local(hw); 5877 struct sk_buff *skb = NULL; 5878 struct ieee80211_tx_data tx; 5879 struct ieee80211_sub_if_data *sdata; 5880 struct ps_data *ps; 5881 struct ieee80211_tx_info *info; 5882 struct ieee80211_chanctx_conf *chanctx_conf; 5883 5884 sdata = vif_to_sdata(vif); 5885 5886 rcu_read_lock(); 5887 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf); 5888 5889 if (!chanctx_conf) 5890 goto out; 5891 5892 if (sdata->vif.type == NL80211_IFTYPE_AP) { 5893 struct beacon_data *beacon = 5894 rcu_dereference(sdata->deflink.u.ap.beacon); 5895 5896 if (!beacon || !beacon->head) 5897 goto out; 5898 5899 ps = &sdata->u.ap.ps; 5900 } else if (ieee80211_vif_is_mesh(&sdata->vif)) { 5901 ps = &sdata->u.mesh.ps; 5902 } else { 5903 goto out; 5904 } 5905 5906 if (ps->dtim_count != 0 || !ps->dtim_bc_mc) 5907 goto out; /* send buffered bc/mc only after DTIM beacon */ 5908 5909 while (1) { 5910 skb = skb_dequeue(&ps->bc_buf); 5911 if (!skb) 5912 goto out; 5913 local->total_ps_buffered--; 5914 5915 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) { 5916 struct ieee80211_hdr *hdr = 5917 (struct ieee80211_hdr *) skb->data; 5918 /* more buffered multicast/broadcast frames ==> set 5919 * MoreData flag in IEEE 802.11 header to inform PS 5920 * STAs */ 5921 hdr->frame_control |= 5922 cpu_to_le16(IEEE80211_FCTL_MOREDATA); 5923 } 5924 5925 if (sdata->vif.type == NL80211_IFTYPE_AP) 5926 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev); 5927 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb)) 5928 break; 5929 ieee80211_free_txskb(hw, skb); 5930 } 5931 5932 info = IEEE80211_SKB_CB(skb); 5933 5934 tx.flags |= IEEE80211_TX_PS_BUFFERED; 5935 info->band = chanctx_conf->def.chan->band; 5936 5937 if (invoke_tx_handlers(&tx)) 5938 skb = NULL; 5939 out: 5940 rcu_read_unlock(); 5941 5942 return skb; 5943 } 5944 EXPORT_SYMBOL(ieee80211_get_buffered_bc); 5945 5946 int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid) 5947 { 5948 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 5949 struct ieee80211_sub_if_data *sdata = sta->sdata; 5950 struct ieee80211_local *local = sdata->local; 5951 int ret; 5952 u32 queues; 5953 5954 lockdep_assert_wiphy(local->hw.wiphy); 5955 5956 /* only some cases are supported right now */ 5957 switch (sdata->vif.type) { 5958 case NL80211_IFTYPE_STATION: 5959 case NL80211_IFTYPE_AP: 5960 case NL80211_IFTYPE_AP_VLAN: 5961 break; 5962 default: 5963 WARN_ON(1); 5964 return -EINVAL; 5965 } 5966 5967 if (WARN_ON(tid >= IEEE80211_NUM_UPS)) 5968 return -EINVAL; 5969 5970 if (sta->reserved_tid == tid) { 5971 ret = 0; 5972 goto out; 5973 } 5974 5975 if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) { 5976 sdata_err(sdata, "TID reservation already active\n"); 5977 ret = -EALREADY; 5978 goto out; 5979 } 5980 5981 ieee80211_stop_vif_queues(sdata->local, sdata, 5982 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID); 5983 5984 synchronize_net(); 5985 5986 /* Tear down BA sessions so we stop aggregating on this TID */ 5987 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) { 5988 set_sta_flag(sta, WLAN_STA_BLOCK_BA); 5989 __ieee80211_stop_tx_ba_session(sta, tid, 5990 AGG_STOP_LOCAL_REQUEST); 5991 } 5992 5993 queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]); 5994 __ieee80211_flush_queues(local, sdata, queues, false); 5995 5996 sta->reserved_tid = tid; 5997 5998 ieee80211_wake_vif_queues(local, sdata, 5999 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID); 6000 6001 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) 6002 clear_sta_flag(sta, WLAN_STA_BLOCK_BA); 6003 6004 ret = 0; 6005 out: 6006 return ret; 6007 } 6008 EXPORT_SYMBOL(ieee80211_reserve_tid); 6009 6010 void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid) 6011 { 6012 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 6013 struct ieee80211_sub_if_data *sdata = sta->sdata; 6014 6015 lockdep_assert_wiphy(sdata->local->hw.wiphy); 6016 6017 /* only some cases are supported right now */ 6018 switch (sdata->vif.type) { 6019 case NL80211_IFTYPE_STATION: 6020 case NL80211_IFTYPE_AP: 6021 case NL80211_IFTYPE_AP_VLAN: 6022 break; 6023 default: 6024 WARN_ON(1); 6025 return; 6026 } 6027 6028 if (tid != sta->reserved_tid) { 6029 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid); 6030 return; 6031 } 6032 6033 sta->reserved_tid = IEEE80211_TID_UNRESERVED; 6034 } 6035 EXPORT_SYMBOL(ieee80211_unreserve_tid); 6036 6037 void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata, 6038 struct sk_buff *skb, int tid, int link_id, 6039 enum nl80211_band band) 6040 { 6041 const struct ieee80211_hdr *hdr = (void *)skb->data; 6042 int ac = ieee80211_ac_from_tid(tid); 6043 unsigned int link; 6044 6045 skb_reset_mac_header(skb); 6046 skb_set_queue_mapping(skb, ac); 6047 skb->priority = tid; 6048 6049 skb->dev = sdata->dev; 6050 6051 BUILD_BUG_ON(IEEE80211_LINK_UNSPECIFIED < IEEE80211_MLD_MAX_NUM_LINKS); 6052 BUILD_BUG_ON(!FIELD_FIT(IEEE80211_TX_CTRL_MLO_LINK, 6053 IEEE80211_LINK_UNSPECIFIED)); 6054 6055 if (!ieee80211_vif_is_mld(&sdata->vif)) { 6056 link = 0; 6057 } else if (link_id >= 0) { 6058 link = link_id; 6059 } else if (memcmp(sdata->vif.addr, hdr->addr2, ETH_ALEN) == 0) { 6060 /* address from the MLD */ 6061 link = IEEE80211_LINK_UNSPECIFIED; 6062 } else { 6063 /* otherwise must be addressed from a link */ 6064 rcu_read_lock(); 6065 for (link = 0; link < ARRAY_SIZE(sdata->vif.link_conf); link++) { 6066 struct ieee80211_bss_conf *link_conf; 6067 6068 link_conf = rcu_dereference(sdata->vif.link_conf[link]); 6069 if (!link_conf) 6070 continue; 6071 if (memcmp(link_conf->addr, hdr->addr2, ETH_ALEN) == 0) 6072 break; 6073 } 6074 rcu_read_unlock(); 6075 6076 if (WARN_ON_ONCE(link == ARRAY_SIZE(sdata->vif.link_conf))) 6077 link = ffs(sdata->vif.active_links) - 1; 6078 } 6079 6080 IEEE80211_SKB_CB(skb)->control.flags |= 6081 u32_encode_bits(link, IEEE80211_TX_CTRL_MLO_LINK); 6082 6083 /* 6084 * The other path calling ieee80211_xmit is from the tasklet, 6085 * and while we can handle concurrent transmissions locking 6086 * requirements are that we do not come into tx with bhs on. 6087 */ 6088 local_bh_disable(); 6089 IEEE80211_SKB_CB(skb)->band = band; 6090 ieee80211_xmit(sdata, NULL, skb); 6091 local_bh_enable(); 6092 } 6093 6094 void ieee80211_tx_skb_tid(struct ieee80211_sub_if_data *sdata, 6095 struct sk_buff *skb, int tid, int link_id) 6096 { 6097 struct ieee80211_chanctx_conf *chanctx_conf; 6098 enum nl80211_band band; 6099 6100 rcu_read_lock(); 6101 if (!ieee80211_vif_is_mld(&sdata->vif)) { 6102 WARN_ON(link_id >= 0); 6103 chanctx_conf = 6104 rcu_dereference(sdata->vif.bss_conf.chanctx_conf); 6105 if (WARN_ON(!chanctx_conf)) { 6106 rcu_read_unlock(); 6107 kfree_skb(skb); 6108 return; 6109 } 6110 band = chanctx_conf->def.chan->band; 6111 } else { 6112 WARN_ON(link_id >= 0 && 6113 !(sdata->vif.active_links & BIT(link_id))); 6114 /* MLD transmissions must not rely on the band */ 6115 band = 0; 6116 } 6117 6118 __ieee80211_tx_skb_tid_band(sdata, skb, tid, link_id, band); 6119 rcu_read_unlock(); 6120 } 6121 6122 int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev, 6123 const u8 *buf, size_t len, 6124 const u8 *dest, __be16 proto, bool unencrypted, 6125 int link_id, u64 *cookie) 6126 { 6127 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 6128 struct ieee80211_local *local = sdata->local; 6129 struct sta_info *sta; 6130 struct sk_buff *skb; 6131 struct ethhdr *ehdr; 6132 u32 ctrl_flags = 0; 6133 u32 flags = 0; 6134 int err; 6135 6136 /* mutex lock is only needed for incrementing the cookie counter */ 6137 lockdep_assert_wiphy(local->hw.wiphy); 6138 6139 /* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE 6140 * or Pre-Authentication 6141 */ 6142 if (proto != sdata->control_port_protocol && 6143 proto != cpu_to_be16(ETH_P_PREAUTH)) 6144 return -EINVAL; 6145 6146 if (proto == sdata->control_port_protocol) 6147 ctrl_flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO | 6148 IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP; 6149 6150 if (unencrypted) 6151 flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT; 6152 6153 if (cookie) 6154 ctrl_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS; 6155 6156 flags |= IEEE80211_TX_INTFL_NL80211_FRAME_TX; 6157 6158 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 6159 sizeof(struct ethhdr) + len); 6160 if (!skb) 6161 return -ENOMEM; 6162 6163 skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr)); 6164 6165 skb_put_data(skb, buf, len); 6166 6167 ehdr = skb_push(skb, sizeof(struct ethhdr)); 6168 memcpy(ehdr->h_dest, dest, ETH_ALEN); 6169 6170 /* we may override the SA for MLO STA later */ 6171 if (link_id < 0) { 6172 ctrl_flags |= u32_encode_bits(IEEE80211_LINK_UNSPECIFIED, 6173 IEEE80211_TX_CTRL_MLO_LINK); 6174 memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN); 6175 } else { 6176 struct ieee80211_bss_conf *link_conf; 6177 6178 ctrl_flags |= u32_encode_bits(link_id, 6179 IEEE80211_TX_CTRL_MLO_LINK); 6180 6181 rcu_read_lock(); 6182 link_conf = rcu_dereference(sdata->vif.link_conf[link_id]); 6183 if (!link_conf) { 6184 dev_kfree_skb(skb); 6185 rcu_read_unlock(); 6186 return -ENOLINK; 6187 } 6188 memcpy(ehdr->h_source, link_conf->addr, ETH_ALEN); 6189 rcu_read_unlock(); 6190 } 6191 6192 ehdr->h_proto = proto; 6193 6194 skb->dev = dev; 6195 skb->protocol = proto; 6196 skb_reset_network_header(skb); 6197 skb_reset_mac_header(skb); 6198 6199 if (local->hw.queues < IEEE80211_NUM_ACS) 6200 goto start_xmit; 6201 6202 /* update QoS header to prioritize control port frames if possible, 6203 * prioritization also happens for control port frames send over 6204 * AF_PACKET 6205 */ 6206 rcu_read_lock(); 6207 err = ieee80211_lookup_ra_sta(sdata, skb, &sta); 6208 if (err) { 6209 dev_kfree_skb(skb); 6210 rcu_read_unlock(); 6211 return err; 6212 } 6213 6214 if (!IS_ERR(sta)) { 6215 u16 queue = ieee80211_select_queue(sdata, sta, skb); 6216 6217 skb_set_queue_mapping(skb, queue); 6218 6219 /* 6220 * for MLO STA, the SA should be the AP MLD address, but 6221 * the link ID has been selected already 6222 */ 6223 if (sta && sta->sta.mlo) 6224 memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN); 6225 } 6226 rcu_read_unlock(); 6227 6228 start_xmit: 6229 local_bh_disable(); 6230 __ieee80211_subif_start_xmit(skb, skb->dev, flags, ctrl_flags, cookie); 6231 local_bh_enable(); 6232 6233 return 0; 6234 } 6235 6236 int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev, 6237 const u8 *buf, size_t len) 6238 { 6239 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 6240 struct ieee80211_local *local = sdata->local; 6241 struct sk_buff *skb; 6242 6243 skb = dev_alloc_skb(local->hw.extra_tx_headroom + len + 6244 30 + /* header size */ 6245 18); /* 11s header size */ 6246 if (!skb) 6247 return -ENOMEM; 6248 6249 skb_reserve(skb, local->hw.extra_tx_headroom); 6250 skb_put_data(skb, buf, len); 6251 6252 skb->dev = dev; 6253 skb->protocol = htons(ETH_P_802_3); 6254 skb_reset_network_header(skb); 6255 skb_reset_mac_header(skb); 6256 6257 local_bh_disable(); 6258 __ieee80211_subif_start_xmit(skb, skb->dev, 0, 6259 IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP, 6260 NULL); 6261 local_bh_enable(); 6262 6263 return 0; 6264 } 6265