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