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