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