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