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