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