1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
6 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2013-2014 Intel Mobile Communications GmbH
8 * Copyright (C) 2018 Intel Corporation
10 * Transmit and frame generation functions.
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/skbuff.h>
16 #include <linux/if_vlan.h>
17 #include <linux/etherdevice.h>
18 #include <linux/bitmap.h>
19 #include <linux/rcupdate.h>
20 #include <linux/export.h>
21 #include <net/net_namespace.h>
22 #include <net/ieee80211_radiotap.h>
23 #include <net/cfg80211.h>
24 #include <net/mac80211.h>
25 #include <net/codel.h>
26 #include <net/codel_impl.h>
27 #include <asm/unaligned.h>
28 #include <net/fq_impl.h>
30 #include "ieee80211_i.h"
31 #include "driver-ops.h"
41 static inline void ieee80211_tx_stats(struct net_device *dev, u32 len)
43 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
45 u64_stats_update_begin(&tstats->syncp);
47 tstats->tx_bytes += len;
48 u64_stats_update_end(&tstats->syncp);
51 static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
52 struct sk_buff *skb, int group_addr,
55 int rate, mrate, erp, dur, i, shift = 0;
56 struct ieee80211_rate *txrate;
57 struct ieee80211_local *local = tx->local;
58 struct ieee80211_supported_band *sband;
59 struct ieee80211_hdr *hdr;
60 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
61 struct ieee80211_chanctx_conf *chanctx_conf;
64 /* assume HW handles this */
65 if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))
69 chanctx_conf = rcu_dereference(tx->sdata->vif.chanctx_conf);
71 shift = ieee80211_chandef_get_shift(&chanctx_conf->def);
72 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
77 if (WARN_ON_ONCE(tx->rate.idx < 0))
80 sband = local->hw.wiphy->bands[info->band];
81 txrate = &sband->bitrates[tx->rate.idx];
83 erp = txrate->flags & IEEE80211_RATE_ERP_G;
86 * data and mgmt (except PS Poll):
88 * - during contention period:
89 * if addr1 is group address: 0
90 * if more fragments = 0 and addr1 is individual address: time to
91 * transmit one ACK plus SIFS
92 * if more fragments = 1 and addr1 is individual address: time to
93 * transmit next fragment plus 2 x ACK plus 3 x SIFS
96 * - control response frame (CTS or ACK) shall be transmitted using the
97 * same rate as the immediately previous frame in the frame exchange
98 * sequence, if this rate belongs to the PHY mandatory rates, or else
99 * at the highest possible rate belonging to the PHY rates in the
102 hdr = (struct ieee80211_hdr *)skb->data;
103 if (ieee80211_is_ctl(hdr->frame_control)) {
104 /* TODO: These control frames are not currently sent by
105 * mac80211, but should they be implemented, this function
106 * needs to be updated to support duration field calculation.
108 * RTS: time needed to transmit pending data/mgmt frame plus
109 * one CTS frame plus one ACK frame plus 3 x SIFS
110 * CTS: duration of immediately previous RTS minus time
111 * required to transmit CTS and its SIFS
112 * ACK: 0 if immediately previous directed data/mgmt had
113 * more=0, with more=1 duration in ACK frame is duration
114 * from previous frame minus time needed to transmit ACK
116 * PS Poll: BIT(15) | BIT(14) | aid
122 if (0 /* FIX: data/mgmt during CFP */)
123 return cpu_to_le16(32768);
125 if (group_addr) /* Group address as the destination - no ACK */
128 /* Individual destination address:
129 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
130 * CTS and ACK frames shall be transmitted using the highest rate in
131 * basic rate set that is less than or equal to the rate of the
132 * immediately previous frame and that is using the same modulation
133 * (CCK or OFDM). If no basic rate set matches with these requirements,
134 * the highest mandatory rate of the PHY that is less than or equal to
135 * the rate of the previous frame is used.
136 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
139 /* use lowest available if everything fails */
140 mrate = sband->bitrates[0].bitrate;
141 for (i = 0; i < sband->n_bitrates; i++) {
142 struct ieee80211_rate *r = &sband->bitrates[i];
144 if (r->bitrate > txrate->bitrate)
147 if ((rate_flags & r->flags) != rate_flags)
150 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
151 rate = DIV_ROUND_UP(r->bitrate, 1 << shift);
153 switch (sband->band) {
154 case NL80211_BAND_2GHZ: {
156 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
157 flag = IEEE80211_RATE_MANDATORY_G;
159 flag = IEEE80211_RATE_MANDATORY_B;
164 case NL80211_BAND_5GHZ:
165 case NL80211_BAND_6GHZ:
166 if (r->flags & IEEE80211_RATE_MANDATORY_A)
169 case NL80211_BAND_60GHZ:
170 /* TODO, for now fall through */
171 case NUM_NL80211_BANDS:
177 /* No matching basic rate found; use highest suitable mandatory
179 rate = DIV_ROUND_UP(mrate, 1 << shift);
182 /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
183 if (ieee80211_is_data_qos(hdr->frame_control) &&
184 *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
187 /* Time needed to transmit ACK
188 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
189 * to closest integer */
190 dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
191 tx->sdata->vif.bss_conf.use_short_preamble,
195 /* Frame is fragmented: duration increases with time needed to
196 * transmit next fragment plus ACK and 2 x SIFS. */
197 dur *= 2; /* ACK + SIFS */
199 dur += ieee80211_frame_duration(sband->band, next_frag_len,
200 txrate->bitrate, erp,
201 tx->sdata->vif.bss_conf.use_short_preamble,
205 return cpu_to_le16(dur);
209 static ieee80211_tx_result debug_noinline
210 ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
212 struct ieee80211_local *local = tx->local;
213 struct ieee80211_if_managed *ifmgd;
214 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
216 /* driver doesn't support power save */
217 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
220 /* hardware does dynamic power save */
221 if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
224 /* dynamic power save disabled */
225 if (local->hw.conf.dynamic_ps_timeout <= 0)
228 /* we are scanning, don't enable power save */
232 if (!local->ps_sdata)
235 /* No point if we're going to suspend */
236 if (local->quiescing)
239 /* dynamic ps is supported only in managed mode */
240 if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
243 if (unlikely(info->flags & IEEE80211_TX_INTFL_OFFCHAN_TX_OK))
246 ifmgd = &tx->sdata->u.mgd;
249 * Don't wakeup from power save if u-apsd is enabled, voip ac has
250 * u-apsd enabled and the frame is in voip class. This effectively
251 * means that even if all access categories have u-apsd enabled, in
252 * practise u-apsd is only used with the voip ac. This is a
253 * workaround for the case when received voip class packets do not
254 * have correct qos tag for some reason, due the network or the
257 * Note: ifmgd->uapsd_queues access is racy here. If the value is
258 * changed via debugfs, user needs to reassociate manually to have
259 * everything in sync.
261 if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
262 (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
263 skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
266 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
267 ieee80211_stop_queues_by_reason(&local->hw,
268 IEEE80211_MAX_QUEUE_MAP,
269 IEEE80211_QUEUE_STOP_REASON_PS,
271 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
272 ieee80211_queue_work(&local->hw,
273 &local->dynamic_ps_disable_work);
276 /* Don't restart the timer if we're not disassociated */
277 if (!ifmgd->associated)
280 mod_timer(&local->dynamic_ps_timer, jiffies +
281 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
286 static ieee80211_tx_result debug_noinline
287 ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
290 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
291 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
294 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
297 if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
298 test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
299 !ieee80211_is_probe_req(hdr->frame_control) &&
300 !ieee80211_is_nullfunc(hdr->frame_control))
302 * When software scanning only nullfunc frames (to notify
303 * the sleep state to the AP) and probe requests (for the
304 * active scan) are allowed, all other frames should not be
305 * sent and we should not get here, but if we do
306 * nonetheless, drop them to avoid sending them
307 * off-channel. See the link below and
308 * ieee80211_start_scan() for more.
310 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
314 if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
317 if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
320 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
324 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
326 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
327 if (unlikely(!assoc &&
328 ieee80211_is_data(hdr->frame_control))) {
329 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
330 sdata_info(tx->sdata,
331 "dropped data frame to not associated station %pM\n",
334 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
337 } else if (unlikely(ieee80211_is_data(hdr->frame_control) &&
338 ieee80211_vif_get_num_mcast_if(tx->sdata) == 0)) {
340 * No associated STAs - no need to send multicast
349 /* This function is called whenever the AP is about to exceed the maximum limit
350 * of buffered frames for power saving STAs. This situation should not really
351 * happen often during normal operation, so dropping the oldest buffered packet
352 * from each queue should be OK to make some room for new frames. */
353 static void purge_old_ps_buffers(struct ieee80211_local *local)
355 int total = 0, purged = 0;
357 struct ieee80211_sub_if_data *sdata;
358 struct sta_info *sta;
360 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
363 if (sdata->vif.type == NL80211_IFTYPE_AP)
364 ps = &sdata->u.ap.ps;
365 else if (ieee80211_vif_is_mesh(&sdata->vif))
366 ps = &sdata->u.mesh.ps;
370 skb = skb_dequeue(&ps->bc_buf);
373 ieee80211_free_txskb(&local->hw, skb);
375 total += skb_queue_len(&ps->bc_buf);
379 * Drop one frame from each station from the lowest-priority
380 * AC that has frames at all.
382 list_for_each_entry_rcu(sta, &local->sta_list, list) {
385 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
386 skb = skb_dequeue(&sta->ps_tx_buf[ac]);
387 total += skb_queue_len(&sta->ps_tx_buf[ac]);
390 ieee80211_free_txskb(&local->hw, skb);
396 local->total_ps_buffered = total;
397 ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
400 static ieee80211_tx_result
401 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
403 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
404 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
408 * broadcast/multicast frame
410 * If any of the associated/peer stations is in power save mode,
411 * the frame is buffered to be sent after DTIM beacon frame.
412 * This is done either by the hardware or us.
415 /* powersaving STAs currently only in AP/VLAN/mesh mode */
416 if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
417 tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
421 ps = &tx->sdata->bss->ps;
422 } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
423 ps = &tx->sdata->u.mesh.ps;
429 /* no buffering for ordered frames */
430 if (ieee80211_has_order(hdr->frame_control))
433 if (ieee80211_is_probe_req(hdr->frame_control))
436 if (ieee80211_hw_check(&tx->local->hw, QUEUE_CONTROL))
437 info->hw_queue = tx->sdata->vif.cab_queue;
439 /* no stations in PS mode and no buffered packets */
440 if (!atomic_read(&ps->num_sta_ps) && skb_queue_empty(&ps->bc_buf))
443 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
445 /* device releases frame after DTIM beacon */
446 if (!ieee80211_hw_check(&tx->local->hw, HOST_BROADCAST_PS_BUFFERING))
449 /* buffered in mac80211 */
450 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
451 purge_old_ps_buffers(tx->local);
453 if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
455 "BC TX buffer full - dropping the oldest frame\n");
456 ieee80211_free_txskb(&tx->local->hw, skb_dequeue(&ps->bc_buf));
458 tx->local->total_ps_buffered++;
460 skb_queue_tail(&ps->bc_buf, tx->skb);
465 static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
468 if (!ieee80211_is_mgmt(fc))
471 if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
474 if (!ieee80211_is_robust_mgmt_frame(skb))
480 static ieee80211_tx_result
481 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
483 struct sta_info *sta = tx->sta;
484 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
485 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
486 struct ieee80211_local *local = tx->local;
491 if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
492 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
493 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
494 !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
495 int ac = skb_get_queue_mapping(tx->skb);
497 if (ieee80211_is_mgmt(hdr->frame_control) &&
498 !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
499 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
503 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
504 sta->sta.addr, sta->sta.aid, ac);
505 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
506 purge_old_ps_buffers(tx->local);
508 /* sync with ieee80211_sta_ps_deliver_wakeup */
509 spin_lock(&sta->ps_lock);
511 * STA woke up the meantime and all the frames on ps_tx_buf have
512 * been queued to pending queue. No reordering can happen, go
513 * ahead and Tx the packet.
515 if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
516 !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
517 !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
518 spin_unlock(&sta->ps_lock);
522 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
523 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
525 "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
527 ieee80211_free_txskb(&local->hw, old);
529 tx->local->total_ps_buffered++;
531 info->control.jiffies = jiffies;
532 info->control.vif = &tx->sdata->vif;
533 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
534 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
535 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
536 spin_unlock(&sta->ps_lock);
538 if (!timer_pending(&local->sta_cleanup))
539 mod_timer(&local->sta_cleanup,
540 round_jiffies(jiffies +
541 STA_INFO_CLEANUP_INTERVAL));
544 * We queued up some frames, so the TIM bit might
545 * need to be set, recalculate it.
547 sta_info_recalc_tim(sta);
550 } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
552 "STA %pM in PS mode, but polling/in SP -> send frame\n",
559 static ieee80211_tx_result debug_noinline
560 ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
562 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
565 if (tx->flags & IEEE80211_TX_UNICAST)
566 return ieee80211_tx_h_unicast_ps_buf(tx);
568 return ieee80211_tx_h_multicast_ps_buf(tx);
571 static ieee80211_tx_result debug_noinline
572 ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
574 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
576 if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
577 if (tx->sdata->control_port_no_encrypt)
578 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
579 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
580 info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
586 static ieee80211_tx_result debug_noinline
587 ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
589 struct ieee80211_key *key;
590 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
591 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
593 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
596 (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
598 else if (ieee80211_is_group_privacy_action(tx->skb) &&
599 (key = rcu_dereference(tx->sdata->default_multicast_key)))
601 else if (ieee80211_is_mgmt(hdr->frame_control) &&
602 is_multicast_ether_addr(hdr->addr1) &&
603 ieee80211_is_robust_mgmt_frame(tx->skb) &&
604 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
606 else if (is_multicast_ether_addr(hdr->addr1) &&
607 (key = rcu_dereference(tx->sdata->default_multicast_key)))
609 else if (!is_multicast_ether_addr(hdr->addr1) &&
610 (key = rcu_dereference(tx->sdata->default_unicast_key)))
616 bool skip_hw = false;
618 /* TODO: add threshold stuff again */
620 switch (tx->key->conf.cipher) {
621 case WLAN_CIPHER_SUITE_WEP40:
622 case WLAN_CIPHER_SUITE_WEP104:
623 case WLAN_CIPHER_SUITE_TKIP:
624 if (!ieee80211_is_data_present(hdr->frame_control))
627 case WLAN_CIPHER_SUITE_CCMP:
628 case WLAN_CIPHER_SUITE_CCMP_256:
629 case WLAN_CIPHER_SUITE_GCMP:
630 case WLAN_CIPHER_SUITE_GCMP_256:
631 if (!ieee80211_is_data_present(hdr->frame_control) &&
632 !ieee80211_use_mfp(hdr->frame_control, tx->sta,
634 !ieee80211_is_group_privacy_action(tx->skb))
637 skip_hw = (tx->key->conf.flags &
638 IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
639 ieee80211_is_mgmt(hdr->frame_control);
641 case WLAN_CIPHER_SUITE_AES_CMAC:
642 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
643 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
644 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
645 if (!ieee80211_is_mgmt(hdr->frame_control))
650 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
651 !ieee80211_is_deauth(hdr->frame_control)))
654 if (!skip_hw && tx->key &&
655 tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
656 info->control.hw_key = &tx->key->conf;
662 static ieee80211_tx_result debug_noinline
663 ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
665 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
666 struct ieee80211_hdr *hdr = (void *)tx->skb->data;
667 struct ieee80211_supported_band *sband;
669 struct ieee80211_tx_rate_control txrc;
670 struct ieee80211_sta_rates *ratetbl = NULL;
673 memset(&txrc, 0, sizeof(txrc));
675 sband = tx->local->hw.wiphy->bands[info->band];
677 len = min_t(u32, tx->skb->len + FCS_LEN,
678 tx->local->hw.wiphy->frag_threshold);
680 /* set up the tx rate control struct we give the RC algo */
681 txrc.hw = &tx->local->hw;
683 txrc.bss_conf = &tx->sdata->vif.bss_conf;
685 txrc.reported_rate.idx = -1;
686 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
688 if (tx->sdata->rc_has_mcs_mask[info->band])
689 txrc.rate_idx_mcs_mask =
690 tx->sdata->rc_rateidx_mcs_mask[info->band];
692 txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
693 tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
694 tx->sdata->vif.type == NL80211_IFTYPE_ADHOC ||
695 tx->sdata->vif.type == NL80211_IFTYPE_OCB);
697 /* set up RTS protection if desired */
698 if (len > tx->local->hw.wiphy->rts_threshold) {
702 info->control.use_rts = txrc.rts;
703 info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
706 * Use short preamble if the BSS can handle it, but not for
707 * management frames unless we know the receiver can handle
708 * that -- the management frame might be to a station that
709 * just wants a probe response.
711 if (tx->sdata->vif.bss_conf.use_short_preamble &&
712 (ieee80211_is_data(hdr->frame_control) ||
713 (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
714 txrc.short_preamble = true;
716 info->control.short_preamble = txrc.short_preamble;
718 /* don't ask rate control when rate already injected via radiotap */
719 if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)
723 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
726 * Lets not bother rate control if we're associated and cannot
727 * talk to the sta. This should not happen.
729 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
730 !rate_usable_index_exists(sband, &tx->sta->sta),
731 "%s: Dropped data frame as no usable bitrate found while "
732 "scanning and associated. Target station: "
733 "%pM on %d GHz band\n",
734 tx->sdata->name, hdr->addr1,
739 * If we're associated with the sta at this point we know we can at
740 * least send the frame at the lowest bit rate.
742 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
744 if (tx->sta && !info->control.skip_table)
745 ratetbl = rcu_dereference(tx->sta->sta.rates);
747 if (unlikely(info->control.rates[0].idx < 0)) {
749 struct ieee80211_tx_rate rate = {
750 .idx = ratetbl->rate[0].idx,
751 .flags = ratetbl->rate[0].flags,
752 .count = ratetbl->rate[0].count
755 if (ratetbl->rate[0].idx < 0)
763 tx->rate = info->control.rates[0];
766 if (txrc.reported_rate.idx < 0) {
767 txrc.reported_rate = tx->rate;
768 if (tx->sta && ieee80211_is_data(hdr->frame_control))
769 tx->sta->tx_stats.last_rate = txrc.reported_rate;
771 tx->sta->tx_stats.last_rate = txrc.reported_rate;
776 if (unlikely(!info->control.rates[0].count))
777 info->control.rates[0].count = 1;
779 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
780 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
781 info->control.rates[0].count = 1;
786 static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
788 u16 *seq = &sta->tid_seq[tid];
789 __le16 ret = cpu_to_le16(*seq);
791 /* Increase the sequence number. */
792 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
797 static ieee80211_tx_result debug_noinline
798 ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
800 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
801 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
805 * Packet injection may want to control the sequence
806 * number, if we have no matching interface then we
807 * neither assign one ourselves nor ask the driver to.
809 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
812 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
815 if (ieee80211_hdrlen(hdr->frame_control) < 24)
818 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
822 * Anything but QoS data that has a sequence number field
823 * (is long enough) gets a sequence number from the global
824 * counter. QoS data frames with a multicast destination
825 * also use the global counter (802.11-2012 9.3.2.10).
827 if (!ieee80211_is_data_qos(hdr->frame_control) ||
828 is_multicast_ether_addr(hdr->addr1)) {
829 if (tx->flags & IEEE80211_TX_NO_SEQNO)
831 /* driver should assign sequence number */
832 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
833 /* for pure STA mode without beacons, we can do it */
834 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
835 tx->sdata->sequence_number += 0x10;
837 tx->sta->tx_stats.msdu[IEEE80211_NUM_TIDS]++;
842 * This should be true for injected/management frames only, for
843 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
844 * above since they are not QoS-data frames.
849 /* include per-STA, per-TID sequence counter */
850 tid = ieee80211_get_tid(hdr);
851 tx->sta->tx_stats.msdu[tid]++;
853 hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
858 static int ieee80211_fragment(struct ieee80211_tx_data *tx,
859 struct sk_buff *skb, int hdrlen,
862 struct ieee80211_local *local = tx->local;
863 struct ieee80211_tx_info *info;
865 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
866 int pos = hdrlen + per_fragm;
867 int rem = skb->len - hdrlen - per_fragm;
869 if (WARN_ON(rem < 0))
872 /* first fragment was already added to queue by caller */
875 int fraglen = per_fragm;
880 tmp = dev_alloc_skb(local->tx_headroom +
882 tx->sdata->encrypt_headroom +
883 IEEE80211_ENCRYPT_TAILROOM);
887 __skb_queue_tail(&tx->skbs, tmp);
890 local->tx_headroom + tx->sdata->encrypt_headroom);
892 /* copy control information */
893 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
895 info = IEEE80211_SKB_CB(tmp);
896 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
897 IEEE80211_TX_CTL_FIRST_FRAGMENT);
900 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
902 skb_copy_queue_mapping(tmp, skb);
903 tmp->priority = skb->priority;
906 /* copy header and data */
907 skb_put_data(tmp, skb->data, hdrlen);
908 skb_put_data(tmp, skb->data + pos, fraglen);
913 /* adjust first fragment's length */
914 skb_trim(skb, hdrlen + per_fragm);
918 static ieee80211_tx_result debug_noinline
919 ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
921 struct sk_buff *skb = tx->skb;
922 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
923 struct ieee80211_hdr *hdr = (void *)skb->data;
924 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
928 /* no matter what happens, tx->skb moves to tx->skbs */
929 __skb_queue_tail(&tx->skbs, skb);
932 if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
935 if (ieee80211_hw_check(&tx->local->hw, SUPPORTS_TX_FRAG))
939 * Warn when submitting a fragmented A-MPDU frame and drop it.
940 * This scenario is handled in ieee80211_tx_prepare but extra
941 * caution taken here as fragmented ampdu may cause Tx stop.
943 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
946 hdrlen = ieee80211_hdrlen(hdr->frame_control);
948 /* internal error, why isn't DONTFRAG set? */
949 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
953 * Now fragment the frame. This will allocate all the fragments and
954 * chain them (using skb as the first fragment) to skb->next.
955 * During transmission, we will remove the successfully transmitted
956 * fragments from this list. When the low-level driver rejects one
957 * of the fragments then we will simply pretend to accept the skb
958 * but store it away as pending.
960 if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
963 /* update duration/seq/flags of fragments */
966 skb_queue_walk(&tx->skbs, skb) {
967 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
969 hdr = (void *)skb->data;
970 info = IEEE80211_SKB_CB(skb);
972 if (!skb_queue_is_last(&tx->skbs, skb)) {
973 hdr->frame_control |= morefrags;
975 * No multi-rate retries for fragmented frames, that
976 * would completely throw off the NAV at other STAs.
978 info->control.rates[1].idx = -1;
979 info->control.rates[2].idx = -1;
980 info->control.rates[3].idx = -1;
981 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
982 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
984 hdr->frame_control &= ~morefrags;
986 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
993 static ieee80211_tx_result debug_noinline
994 ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
1002 skb_queue_walk(&tx->skbs, skb) {
1003 ac = skb_get_queue_mapping(skb);
1004 tx->sta->tx_stats.bytes[ac] += skb->len;
1007 tx->sta->tx_stats.packets[ac]++;
1012 static ieee80211_tx_result debug_noinline
1013 ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
1018 switch (tx->key->conf.cipher) {
1019 case WLAN_CIPHER_SUITE_WEP40:
1020 case WLAN_CIPHER_SUITE_WEP104:
1021 return ieee80211_crypto_wep_encrypt(tx);
1022 case WLAN_CIPHER_SUITE_TKIP:
1023 return ieee80211_crypto_tkip_encrypt(tx);
1024 case WLAN_CIPHER_SUITE_CCMP:
1025 return ieee80211_crypto_ccmp_encrypt(
1026 tx, IEEE80211_CCMP_MIC_LEN);
1027 case WLAN_CIPHER_SUITE_CCMP_256:
1028 return ieee80211_crypto_ccmp_encrypt(
1029 tx, IEEE80211_CCMP_256_MIC_LEN);
1030 case WLAN_CIPHER_SUITE_AES_CMAC:
1031 return ieee80211_crypto_aes_cmac_encrypt(tx);
1032 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1033 return ieee80211_crypto_aes_cmac_256_encrypt(tx);
1034 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1035 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1036 return ieee80211_crypto_aes_gmac_encrypt(tx);
1037 case WLAN_CIPHER_SUITE_GCMP:
1038 case WLAN_CIPHER_SUITE_GCMP_256:
1039 return ieee80211_crypto_gcmp_encrypt(tx);
1041 return ieee80211_crypto_hw_encrypt(tx);
1047 static ieee80211_tx_result debug_noinline
1048 ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1050 struct sk_buff *skb;
1051 struct ieee80211_hdr *hdr;
1055 skb_queue_walk(&tx->skbs, skb) {
1056 hdr = (void *) skb->data;
1057 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1058 break; /* must not overwrite AID */
1059 if (!skb_queue_is_last(&tx->skbs, skb)) {
1060 struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1061 next_len = next->len;
1064 group_addr = is_multicast_ether_addr(hdr->addr1);
1067 ieee80211_duration(tx, skb, group_addr, next_len);
1073 /* actual transmit path */
1075 static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1076 struct sk_buff *skb,
1077 struct ieee80211_tx_info *info,
1078 struct tid_ampdu_tx *tid_tx,
1081 bool queued = false;
1082 bool reset_agg_timer = false;
1083 struct sk_buff *purge_skb = NULL;
1085 if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1086 info->flags |= IEEE80211_TX_CTL_AMPDU;
1087 reset_agg_timer = true;
1088 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1090 * nothing -- this aggregation session is being started
1091 * but that might still fail with the driver
1093 } else if (!tx->sta->sta.txq[tid]) {
1094 spin_lock(&tx->sta->lock);
1096 * Need to re-check now, because we may get here
1098 * 1) in the window during which the setup is actually
1099 * already done, but not marked yet because not all
1100 * packets are spliced over to the driver pending
1101 * queue yet -- if this happened we acquire the lock
1102 * either before or after the splice happens, but
1103 * need to recheck which of these cases happened.
1105 * 2) during session teardown, if the OPERATIONAL bit
1106 * was cleared due to the teardown but the pointer
1107 * hasn't been assigned NULL yet (or we loaded it
1108 * before it was assigned) -- in this case it may
1109 * now be NULL which means we should just let the
1110 * packet pass through because splicing the frames
1111 * back is already done.
1113 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
1116 /* do nothing, let packet pass through */
1117 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1118 info->flags |= IEEE80211_TX_CTL_AMPDU;
1119 reset_agg_timer = true;
1122 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
1123 clear_sta_flag(tx->sta, WLAN_STA_SP);
1124 ps_dbg(tx->sta->sdata,
1125 "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
1126 tx->sta->sta.addr, tx->sta->sta.aid);
1128 info->control.vif = &tx->sdata->vif;
1129 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1130 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
1131 __skb_queue_tail(&tid_tx->pending, skb);
1132 if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1133 purge_skb = __skb_dequeue(&tid_tx->pending);
1135 spin_unlock(&tx->sta->lock);
1138 ieee80211_free_txskb(&tx->local->hw, purge_skb);
1141 /* reset session timer */
1142 if (reset_agg_timer)
1143 tid_tx->last_tx = jiffies;
1150 * pass %NULL for the station if unknown, a valid pointer if known
1151 * or an ERR_PTR() if the station is known not to exist
1153 static ieee80211_tx_result
1154 ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1155 struct ieee80211_tx_data *tx,
1156 struct sta_info *sta, struct sk_buff *skb)
1158 struct ieee80211_local *local = sdata->local;
1159 struct ieee80211_hdr *hdr;
1160 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1163 memset(tx, 0, sizeof(*tx));
1167 __skb_queue_head_init(&tx->skbs);
1170 * If this flag is set to true anywhere, and we get here,
1171 * we are doing the needed processing, so remove the flag
1174 info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1176 hdr = (struct ieee80211_hdr *) skb->data;
1182 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1183 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1184 if (!tx->sta && sdata->wdev.use_4addr)
1186 } else if (info->flags & (IEEE80211_TX_INTFL_NL80211_FRAME_TX |
1187 IEEE80211_TX_CTL_INJECTED) ||
1188 tx->sdata->control_port_protocol == tx->skb->protocol) {
1189 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1191 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
1192 tx->sta = sta_info_get(sdata, hdr->addr1);
1195 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
1196 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
1197 ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
1198 !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
1199 struct tid_ampdu_tx *tid_tx;
1201 tid = ieee80211_get_tid(hdr);
1203 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1207 queued = ieee80211_tx_prep_agg(tx, skb, info,
1210 if (unlikely(queued))
1215 if (is_multicast_ether_addr(hdr->addr1)) {
1216 tx->flags &= ~IEEE80211_TX_UNICAST;
1217 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1219 tx->flags |= IEEE80211_TX_UNICAST;
1221 if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1222 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1223 skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1224 info->flags & IEEE80211_TX_CTL_AMPDU)
1225 info->flags |= IEEE80211_TX_CTL_DONTFRAG;
1229 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1230 else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
1231 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1232 ieee80211_check_fast_xmit(tx->sta);
1235 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
1240 static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
1241 struct ieee80211_vif *vif,
1242 struct sta_info *sta,
1243 struct sk_buff *skb)
1245 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1246 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1247 struct ieee80211_txq *txq = NULL;
1249 if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
1250 (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
1253 if (unlikely(!ieee80211_is_data_present(hdr->frame_control))) {
1254 if ((!ieee80211_is_mgmt(hdr->frame_control) ||
1255 ieee80211_is_bufferable_mmpdu(hdr->frame_control) ||
1256 vif->type == NL80211_IFTYPE_STATION) &&
1257 sta && sta->uploaded) {
1259 * This will be NULL if the driver didn't set the
1260 * opt-in hardware flag.
1262 txq = sta->sta.txq[IEEE80211_NUM_TIDS];
1265 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1270 txq = sta->sta.txq[tid];
1278 return to_txq_info(txq);
1281 static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
1283 IEEE80211_SKB_CB(skb)->control.enqueue_time = codel_get_time();
1286 static u32 codel_skb_len_func(const struct sk_buff *skb)
1291 static codel_time_t codel_skb_time_func(const struct sk_buff *skb)
1293 const struct ieee80211_tx_info *info;
1295 info = (const struct ieee80211_tx_info *)skb->cb;
1296 return info->control.enqueue_time;
1299 static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars,
1302 struct ieee80211_local *local;
1303 struct txq_info *txqi;
1305 struct fq_flow *flow;
1308 local = vif_to_sdata(txqi->txq.vif)->local;
1311 if (cvars == &txqi->def_cvars)
1312 flow = &txqi->def_flow;
1314 flow = &fq->flows[cvars - local->cvars];
1316 return fq_flow_dequeue(fq, flow);
1319 static void codel_drop_func(struct sk_buff *skb,
1322 struct ieee80211_local *local;
1323 struct ieee80211_hw *hw;
1324 struct txq_info *txqi;
1327 local = vif_to_sdata(txqi->txq.vif)->local;
1330 ieee80211_free_txskb(hw, skb);
1333 static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
1335 struct fq_flow *flow)
1337 struct ieee80211_local *local;
1338 struct txq_info *txqi;
1339 struct codel_vars *cvars;
1340 struct codel_params *cparams;
1341 struct codel_stats *cstats;
1343 local = container_of(fq, struct ieee80211_local, fq);
1344 txqi = container_of(tin, struct txq_info, tin);
1345 cstats = &txqi->cstats;
1347 if (txqi->txq.sta) {
1348 struct sta_info *sta = container_of(txqi->txq.sta,
1349 struct sta_info, sta);
1350 cparams = &sta->cparams;
1352 cparams = &local->cparams;
1355 if (flow == &txqi->def_flow)
1356 cvars = &txqi->def_cvars;
1358 cvars = &local->cvars[flow - fq->flows];
1360 return codel_dequeue(txqi,
1366 codel_skb_time_func,
1368 codel_dequeue_func);
1371 static void fq_skb_free_func(struct fq *fq,
1373 struct fq_flow *flow,
1374 struct sk_buff *skb)
1376 struct ieee80211_local *local;
1378 local = container_of(fq, struct ieee80211_local, fq);
1379 ieee80211_free_txskb(&local->hw, skb);
1382 static struct fq_flow *fq_flow_get_default_func(struct fq *fq,
1385 struct sk_buff *skb)
1387 struct txq_info *txqi;
1389 txqi = container_of(tin, struct txq_info, tin);
1390 return &txqi->def_flow;
1393 static void ieee80211_txq_enqueue(struct ieee80211_local *local,
1394 struct txq_info *txqi,
1395 struct sk_buff *skb)
1397 struct fq *fq = &local->fq;
1398 struct fq_tin *tin = &txqi->tin;
1399 u32 flow_idx = fq_flow_idx(fq, skb);
1401 ieee80211_set_skb_enqueue_time(skb);
1403 spin_lock_bh(&fq->lock);
1404 fq_tin_enqueue(fq, tin, flow_idx, skb,
1406 fq_flow_get_default_func);
1407 spin_unlock_bh(&fq->lock);
1410 static bool fq_vlan_filter_func(struct fq *fq, struct fq_tin *tin,
1411 struct fq_flow *flow, struct sk_buff *skb,
1414 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1416 return info->control.vif == data;
1419 void ieee80211_txq_remove_vlan(struct ieee80211_local *local,
1420 struct ieee80211_sub_if_data *sdata)
1422 struct fq *fq = &local->fq;
1423 struct txq_info *txqi;
1425 struct ieee80211_sub_if_data *ap;
1427 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1430 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1435 txqi = to_txq_info(ap->vif.txq);
1438 spin_lock_bh(&fq->lock);
1439 fq_tin_filter(fq, tin, fq_vlan_filter_func, &sdata->vif,
1441 spin_unlock_bh(&fq->lock);
1444 void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
1445 struct sta_info *sta,
1446 struct txq_info *txqi, int tid)
1448 fq_tin_init(&txqi->tin);
1449 fq_flow_init(&txqi->def_flow);
1450 codel_vars_init(&txqi->def_cvars);
1451 codel_stats_init(&txqi->cstats);
1452 __skb_queue_head_init(&txqi->frags);
1453 INIT_LIST_HEAD(&txqi->schedule_order);
1455 txqi->txq.vif = &sdata->vif;
1458 sdata->vif.txq = &txqi->txq;
1460 txqi->txq.ac = IEEE80211_AC_BE;
1465 if (tid == IEEE80211_NUM_TIDS) {
1466 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1467 /* Drivers need to opt in to the management MPDU TXQ */
1468 if (!ieee80211_hw_check(&sdata->local->hw,
1471 } else if (!ieee80211_hw_check(&sdata->local->hw,
1473 /* Drivers need to opt in to the bufferable MMPDU TXQ */
1476 txqi->txq.ac = IEEE80211_AC_VO;
1478 txqi->txq.ac = ieee80211_ac_from_tid(tid);
1481 txqi->txq.sta = &sta->sta;
1482 txqi->txq.tid = tid;
1483 sta->sta.txq[tid] = &txqi->txq;
1486 void ieee80211_txq_purge(struct ieee80211_local *local,
1487 struct txq_info *txqi)
1489 struct fq *fq = &local->fq;
1490 struct fq_tin *tin = &txqi->tin;
1492 spin_lock_bh(&fq->lock);
1493 fq_tin_reset(fq, tin, fq_skb_free_func);
1494 ieee80211_purge_tx_queue(&local->hw, &txqi->frags);
1495 spin_unlock_bh(&fq->lock);
1497 spin_lock_bh(&local->active_txq_lock[txqi->txq.ac]);
1498 list_del_init(&txqi->schedule_order);
1499 spin_unlock_bh(&local->active_txq_lock[txqi->txq.ac]);
1502 void ieee80211_txq_set_params(struct ieee80211_local *local)
1504 if (local->hw.wiphy->txq_limit)
1505 local->fq.limit = local->hw.wiphy->txq_limit;
1507 local->hw.wiphy->txq_limit = local->fq.limit;
1509 if (local->hw.wiphy->txq_memory_limit)
1510 local->fq.memory_limit = local->hw.wiphy->txq_memory_limit;
1512 local->hw.wiphy->txq_memory_limit = local->fq.memory_limit;
1514 if (local->hw.wiphy->txq_quantum)
1515 local->fq.quantum = local->hw.wiphy->txq_quantum;
1517 local->hw.wiphy->txq_quantum = local->fq.quantum;
1520 int ieee80211_txq_setup_flows(struct ieee80211_local *local)
1522 struct fq *fq = &local->fq;
1525 bool supp_vht = false;
1526 enum nl80211_band band;
1528 if (!local->ops->wake_tx_queue)
1531 ret = fq_init(fq, 4096);
1536 * If the hardware doesn't support VHT, it is safe to limit the maximum
1537 * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
1539 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1540 struct ieee80211_supported_band *sband;
1542 sband = local->hw.wiphy->bands[band];
1546 supp_vht = supp_vht || sband->vht_cap.vht_supported;
1550 fq->memory_limit = 4 << 20; /* 4 Mbytes */
1552 codel_params_init(&local->cparams);
1553 local->cparams.interval = MS2TIME(100);
1554 local->cparams.target = MS2TIME(20);
1555 local->cparams.ecn = true;
1557 local->cvars = kcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
1559 if (!local->cvars) {
1560 spin_lock_bh(&fq->lock);
1561 fq_reset(fq, fq_skb_free_func);
1562 spin_unlock_bh(&fq->lock);
1566 for (i = 0; i < fq->flows_cnt; i++)
1567 codel_vars_init(&local->cvars[i]);
1569 ieee80211_txq_set_params(local);
1574 void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1576 struct fq *fq = &local->fq;
1578 if (!local->ops->wake_tx_queue)
1581 kfree(local->cvars);
1582 local->cvars = NULL;
1584 spin_lock_bh(&fq->lock);
1585 fq_reset(fq, fq_skb_free_func);
1586 spin_unlock_bh(&fq->lock);
1589 static bool ieee80211_queue_skb(struct ieee80211_local *local,
1590 struct ieee80211_sub_if_data *sdata,
1591 struct sta_info *sta,
1592 struct sk_buff *skb)
1594 struct ieee80211_vif *vif;
1595 struct txq_info *txqi;
1597 if (!local->ops->wake_tx_queue ||
1598 sdata->vif.type == NL80211_IFTYPE_MONITOR)
1601 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1602 sdata = container_of(sdata->bss,
1603 struct ieee80211_sub_if_data, u.ap);
1606 txqi = ieee80211_get_txq(local, vif, sta, skb);
1611 ieee80211_txq_enqueue(local, txqi, skb);
1613 schedule_and_wake_txq(local, txqi);
1618 static bool ieee80211_tx_frags(struct ieee80211_local *local,
1619 struct ieee80211_vif *vif,
1620 struct sta_info *sta,
1621 struct sk_buff_head *skbs,
1624 struct ieee80211_tx_control control = {};
1625 struct sk_buff *skb, *tmp;
1626 unsigned long flags;
1628 skb_queue_walk_safe(skbs, skb, tmp) {
1629 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1630 int q = info->hw_queue;
1632 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1633 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1634 __skb_unlink(skb, skbs);
1635 ieee80211_free_txskb(&local->hw, skb);
1640 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1641 if (local->queue_stop_reasons[q] ||
1642 (!txpending && !skb_queue_empty(&local->pending[q]))) {
1643 if (unlikely(info->flags &
1644 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1645 if (local->queue_stop_reasons[q] &
1646 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1648 * Drop off-channel frames if queues
1649 * are stopped for any reason other
1650 * than off-channel operation. Never
1653 spin_unlock_irqrestore(
1654 &local->queue_stop_reason_lock,
1656 ieee80211_purge_tx_queue(&local->hw,
1663 * Since queue is stopped, queue up frames for
1664 * later transmission from the tx-pending
1665 * tasklet when the queue is woken again.
1668 skb_queue_splice_init(skbs,
1669 &local->pending[q]);
1671 skb_queue_splice_tail_init(skbs,
1672 &local->pending[q]);
1674 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1679 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1681 info->control.vif = vif;
1682 control.sta = sta ? &sta->sta : NULL;
1684 __skb_unlink(skb, skbs);
1685 drv_tx(local, &control, skb);
1692 * Returns false if the frame couldn't be transmitted but was queued instead.
1694 static bool __ieee80211_tx(struct ieee80211_local *local,
1695 struct sk_buff_head *skbs, int led_len,
1696 struct sta_info *sta, bool txpending)
1698 struct ieee80211_tx_info *info;
1699 struct ieee80211_sub_if_data *sdata;
1700 struct ieee80211_vif *vif;
1701 struct sk_buff *skb;
1705 if (WARN_ON(skb_queue_empty(skbs)))
1708 skb = skb_peek(skbs);
1709 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1710 info = IEEE80211_SKB_CB(skb);
1711 sdata = vif_to_sdata(info->control.vif);
1712 if (sta && !sta->uploaded)
1715 switch (sdata->vif.type) {
1716 case NL80211_IFTYPE_MONITOR:
1717 if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
1721 sdata = rcu_dereference(local->monitor_sdata);
1725 vif->hw_queue[skb_get_queue_mapping(skb)];
1726 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
1727 ieee80211_purge_tx_queue(&local->hw, skbs);
1732 case NL80211_IFTYPE_AP_VLAN:
1733 sdata = container_of(sdata->bss,
1734 struct ieee80211_sub_if_data, u.ap);
1741 result = ieee80211_tx_frags(local, vif, sta, skbs, txpending);
1743 ieee80211_tpt_led_trig_tx(local, fc, led_len);
1745 WARN_ON_ONCE(!skb_queue_empty(skbs));
1751 * Invoke TX handlers, return 0 on success and non-zero if the
1752 * frame was dropped or queued.
1754 * The handlers are split into an early and late part. The latter is everything
1755 * that can be sensitive to reordering, and will be deferred to after packets
1756 * are dequeued from the intermediate queues (when they are enabled).
1758 static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx)
1760 ieee80211_tx_result res = TX_DROP;
1762 #define CALL_TXH(txh) \
1765 if (res != TX_CONTINUE) \
1769 CALL_TXH(ieee80211_tx_h_dynamic_ps);
1770 CALL_TXH(ieee80211_tx_h_check_assoc);
1771 CALL_TXH(ieee80211_tx_h_ps_buf);
1772 CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
1773 CALL_TXH(ieee80211_tx_h_select_key);
1774 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1775 CALL_TXH(ieee80211_tx_h_rate_ctrl);
1778 if (unlikely(res == TX_DROP)) {
1779 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1781 ieee80211_free_txskb(&tx->local->hw, tx->skb);
1783 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1785 } else if (unlikely(res == TX_QUEUED)) {
1786 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1794 * Late handlers can be called while the sta lock is held. Handlers that can
1795 * cause packets to be generated will cause deadlock!
1797 static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx)
1799 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1800 ieee80211_tx_result res = TX_CONTINUE;
1802 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1803 __skb_queue_tail(&tx->skbs, tx->skb);
1808 CALL_TXH(ieee80211_tx_h_michael_mic_add);
1809 CALL_TXH(ieee80211_tx_h_sequence);
1810 CALL_TXH(ieee80211_tx_h_fragment);
1811 /* handlers after fragment must be aware of tx info fragmentation! */
1812 CALL_TXH(ieee80211_tx_h_stats);
1813 CALL_TXH(ieee80211_tx_h_encrypt);
1814 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1815 CALL_TXH(ieee80211_tx_h_calculate_duration);
1819 if (unlikely(res == TX_DROP)) {
1820 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1822 ieee80211_free_txskb(&tx->local->hw, tx->skb);
1824 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1826 } else if (unlikely(res == TX_QUEUED)) {
1827 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1834 static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1836 int r = invoke_tx_handlers_early(tx);
1840 return invoke_tx_handlers_late(tx);
1843 bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1844 struct ieee80211_vif *vif, struct sk_buff *skb,
1845 int band, struct ieee80211_sta **sta)
1847 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1848 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1849 struct ieee80211_tx_data tx;
1850 struct sk_buff *skb2;
1852 if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
1856 info->control.vif = vif;
1857 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1859 if (invoke_tx_handlers(&tx))
1864 *sta = &tx.sta->sta;
1869 /* this function isn't suitable for fragmented data frames */
1870 skb2 = __skb_dequeue(&tx.skbs);
1871 if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1872 ieee80211_free_txskb(hw, skb2);
1873 ieee80211_purge_tx_queue(hw, &tx.skbs);
1879 EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1882 * Returns false if the frame couldn't be transmitted but was queued instead.
1884 static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
1885 struct sta_info *sta, struct sk_buff *skb,
1886 bool txpending, u32 txdata_flags)
1888 struct ieee80211_local *local = sdata->local;
1889 struct ieee80211_tx_data tx;
1890 ieee80211_tx_result res_prepare;
1891 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1895 if (unlikely(skb->len < 10)) {
1900 /* initialises tx */
1902 res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
1904 tx.flags |= txdata_flags;
1906 if (unlikely(res_prepare == TX_DROP)) {
1907 ieee80211_free_txskb(&local->hw, skb);
1909 } else if (unlikely(res_prepare == TX_QUEUED)) {
1913 /* set up hw_queue value early */
1914 if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
1915 !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
1917 sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1919 if (invoke_tx_handlers_early(&tx))
1922 if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb))
1925 if (!invoke_tx_handlers_late(&tx))
1926 result = __ieee80211_tx(local, &tx.skbs, led_len,
1932 /* device xmit handlers */
1934 static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
1935 struct sk_buff *skb,
1936 int head_need, bool may_encrypt)
1938 struct ieee80211_local *local = sdata->local;
1939 struct ieee80211_hdr *hdr;
1943 hdr = (struct ieee80211_hdr *) skb->data;
1944 enc_tailroom = may_encrypt &&
1945 (sdata->crypto_tx_tailroom_needed_cnt ||
1946 ieee80211_is_mgmt(hdr->frame_control));
1949 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1950 tail_need -= skb_tailroom(skb);
1951 tail_need = max_t(int, tail_need, 0);
1954 if (skb_cloned(skb) &&
1955 (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
1956 !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
1957 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1958 else if (head_need || tail_need)
1959 I802_DEBUG_INC(local->tx_expand_skb_head);
1963 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1964 wiphy_debug(local->hw.wiphy,
1965 "failed to reallocate TX buffer\n");
1972 void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
1973 struct sta_info *sta, struct sk_buff *skb,
1976 struct ieee80211_local *local = sdata->local;
1977 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1978 struct ieee80211_hdr *hdr;
1982 may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
1984 headroom = local->tx_headroom;
1986 headroom += sdata->encrypt_headroom;
1987 headroom -= skb_headroom(skb);
1988 headroom = max_t(int, 0, headroom);
1990 if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
1991 ieee80211_free_txskb(&local->hw, skb);
1995 hdr = (struct ieee80211_hdr *) skb->data;
1996 info->control.vif = &sdata->vif;
1998 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1999 if (ieee80211_is_data(hdr->frame_control) &&
2000 is_unicast_ether_addr(hdr->addr1)) {
2001 if (mesh_nexthop_resolve(sdata, skb))
2002 return; /* skb queued: don't free */
2004 ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
2008 ieee80211_set_qos_hdr(sdata, skb);
2009 ieee80211_tx(sdata, sta, skb, false, txdata_flags);
2012 static bool ieee80211_parse_tx_radiotap(struct ieee80211_local *local,
2013 struct sk_buff *skb)
2015 struct ieee80211_radiotap_iterator iterator;
2016 struct ieee80211_radiotap_header *rthdr =
2017 (struct ieee80211_radiotap_header *) skb->data;
2018 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2019 struct ieee80211_supported_band *sband =
2020 local->hw.wiphy->bands[info->band];
2021 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
2025 bool rate_found = false;
2026 u8 rate_retries = 0;
2028 u8 mcs_known, mcs_flags, mcs_bw;
2030 u8 vht_mcs = 0, vht_nss = 0;
2033 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
2034 IEEE80211_TX_CTL_DONTFRAG;
2037 * for every radiotap entry that is present
2038 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
2039 * entries present, or -EINVAL on error)
2043 ret = ieee80211_radiotap_iterator_next(&iterator);
2048 /* see if this argument is something we can use */
2049 switch (iterator.this_arg_index) {
2051 * You must take care when dereferencing iterator.this_arg
2052 * for multibyte types... the pointer is not aligned. Use
2053 * get_unaligned((type *)iterator.this_arg) to dereference
2054 * iterator.this_arg for type "type" safely on all arches.
2056 case IEEE80211_RADIOTAP_FLAGS:
2057 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
2059 * this indicates that the skb we have been
2060 * handed has the 32-bit FCS CRC at the end...
2061 * we should react to that by snipping it off
2062 * because it will be recomputed and added
2065 if (skb->len < (iterator._max_length + FCS_LEN))
2068 skb_trim(skb, skb->len - FCS_LEN);
2070 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
2071 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
2072 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
2073 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
2076 case IEEE80211_RADIOTAP_TX_FLAGS:
2077 txflags = get_unaligned_le16(iterator.this_arg);
2078 if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
2079 info->flags |= IEEE80211_TX_CTL_NO_ACK;
2082 case IEEE80211_RADIOTAP_RATE:
2083 rate = *iterator.this_arg;
2088 case IEEE80211_RADIOTAP_DATA_RETRIES:
2089 rate_retries = *iterator.this_arg;
2092 case IEEE80211_RADIOTAP_MCS:
2093 mcs_known = iterator.this_arg[0];
2094 mcs_flags = iterator.this_arg[1];
2095 if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
2099 rate = iterator.this_arg[2];
2100 rate_flags = IEEE80211_TX_RC_MCS;
2102 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
2103 mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
2104 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2106 mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
2107 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
2108 mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
2109 rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2112 case IEEE80211_RADIOTAP_VHT:
2113 vht_known = get_unaligned_le16(iterator.this_arg);
2116 rate_flags = IEEE80211_TX_RC_VHT_MCS;
2117 if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
2118 (iterator.this_arg[2] &
2119 IEEE80211_RADIOTAP_VHT_FLAG_SGI))
2120 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2122 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
2123 if (iterator.this_arg[3] == 1)
2125 IEEE80211_TX_RC_40_MHZ_WIDTH;
2126 else if (iterator.this_arg[3] == 4)
2128 IEEE80211_TX_RC_80_MHZ_WIDTH;
2129 else if (iterator.this_arg[3] == 11)
2131 IEEE80211_TX_RC_160_MHZ_WIDTH;
2134 vht_mcs = iterator.this_arg[4] >> 4;
2135 vht_nss = iterator.this_arg[4] & 0xF;
2139 * Please update the file
2140 * Documentation/networking/mac80211-injection.txt
2141 * when parsing new fields here.
2149 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
2153 info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
2155 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2156 info->control.rates[i].idx = -1;
2157 info->control.rates[i].flags = 0;
2158 info->control.rates[i].count = 0;
2161 if (rate_flags & IEEE80211_TX_RC_MCS) {
2162 info->control.rates[0].idx = rate;
2163 } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
2164 ieee80211_rate_set_vht(info->control.rates, vht_mcs,
2167 for (i = 0; i < sband->n_bitrates; i++) {
2168 if (rate * 5 != sband->bitrates[i].bitrate)
2171 info->control.rates[0].idx = i;
2176 if (info->control.rates[0].idx < 0)
2177 info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
2179 info->control.rates[0].flags = rate_flags;
2180 info->control.rates[0].count = min_t(u8, rate_retries + 1,
2181 local->hw.max_rate_tries);
2185 * remove the radiotap header
2186 * iterator->_max_length was sanity-checked against
2187 * skb->len by iterator init
2189 skb_pull(skb, iterator._max_length);
2194 netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
2195 struct net_device *dev)
2197 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2198 struct ieee80211_chanctx_conf *chanctx_conf;
2199 struct ieee80211_radiotap_header *prthdr =
2200 (struct ieee80211_radiotap_header *)skb->data;
2201 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2202 struct ieee80211_hdr *hdr;
2203 struct ieee80211_sub_if_data *tmp_sdata, *sdata;
2204 struct cfg80211_chan_def *chandef;
2208 /* check for not even having the fixed radiotap header part */
2209 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2210 goto fail; /* too short to be possibly valid */
2212 /* is it a header version we can trust to find length from? */
2213 if (unlikely(prthdr->it_version))
2214 goto fail; /* only version 0 is supported */
2216 /* then there must be a radiotap header with a length we can use */
2217 len_rthdr = ieee80211_get_radiotap_len(skb->data);
2219 /* does the skb contain enough to deliver on the alleged length? */
2220 if (unlikely(skb->len < len_rthdr))
2221 goto fail; /* skb too short for claimed rt header extent */
2224 * fix up the pointers accounting for the radiotap
2225 * header still being in there. We are being given
2226 * a precooked IEEE80211 header so no need for
2229 skb_set_mac_header(skb, len_rthdr);
2231 * these are just fixed to the end of the rt area since we
2232 * don't have any better information and at this point, nobody cares
2234 skb_set_network_header(skb, len_rthdr);
2235 skb_set_transport_header(skb, len_rthdr);
2237 if (skb->len < len_rthdr + 2)
2240 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2241 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2243 if (skb->len < len_rthdr + hdrlen)
2247 * Initialize skb->protocol if the injected frame is a data frame
2248 * carrying a rfc1042 header
2250 if (ieee80211_is_data(hdr->frame_control) &&
2251 skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2252 u8 *payload = (u8 *)hdr + hdrlen;
2254 if (ether_addr_equal(payload, rfc1042_header))
2255 skb->protocol = cpu_to_be16((payload[6] << 8) |
2259 memset(info, 0, sizeof(*info));
2261 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2262 IEEE80211_TX_CTL_INJECTED;
2267 * We process outgoing injected frames that have a local address
2268 * we handle as though they are non-injected frames.
2269 * This code here isn't entirely correct, the local MAC address
2270 * isn't always enough to find the interface to use; for proper
2271 * VLAN/WDS support we will need a different mechanism (which
2272 * likely isn't going to be monitor interfaces).
2274 * This is necessary, for example, for old hostapd versions that
2275 * don't use nl80211-based management TX/RX.
2277 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2279 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2280 if (!ieee80211_sdata_running(tmp_sdata))
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)
2286 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
2292 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2293 if (!chanctx_conf) {
2294 tmp_sdata = rcu_dereference(local->monitor_sdata);
2297 rcu_dereference(tmp_sdata->vif.chanctx_conf);
2301 chandef = &chanctx_conf->def;
2302 else if (!local->use_chanctx)
2303 chandef = &local->_oper_chandef;
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
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.
2323 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2327 info->band = chandef->chan->band;
2329 /* process and remove the injection radiotap header */
2330 if (!ieee80211_parse_tx_radiotap(local, skb))
2333 ieee80211_xmit(sdata, NULL, skb, 0);
2336 return NETDEV_TX_OK;
2342 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
2345 static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
2347 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
2349 return ethertype == ETH_P_TDLS &&
2351 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2354 static int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2355 struct sk_buff *skb,
2356 struct sta_info **sta_out)
2358 struct sta_info *sta;
2360 switch (sdata->vif.type) {
2361 case NL80211_IFTYPE_AP_VLAN:
2362 sta = rcu_dereference(sdata->u.vlan.sta);
2366 } else if (sdata->wdev.use_4addr) {
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);
2377 sta = sta_info_get_bss(sdata, skb->data);
2379 case NL80211_IFTYPE_WDS:
2380 sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
2382 #ifdef CONFIG_MAC80211_MESH
2383 case NL80211_IFTYPE_MESH_POINT:
2384 /* determined much later */
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)) {
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
2405 if (!ieee80211_is_tdls_setup(skb))
2411 sta = sta_info_get(sdata, sdata->u.mgd.bssid);
2419 *sta_out = sta ?: ERR_PTR(-ENOENT);
2423 static int ieee80211_store_ack_skb(struct ieee80211_local *local,
2424 struct sk_buff *skb,
2427 struct sk_buff *ack_skb = skb_clone_sk(skb);
2431 unsigned long flags;
2434 spin_lock_irqsave(&local->ack_status_lock, flags);
2435 id = idr_alloc(&local->ack_status_frames, ack_skb,
2436 1, 0x40, GFP_ATOMIC);
2437 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2441 *info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2451 * ieee80211_build_hdr - build 802.11 header in the given frame
2452 * @sdata: virtual interface to build the header for
2453 * @skb: the skb to build the header in
2454 * @info_flags: skb flags to set
2455 * @ctrl_flags: info control flags to set
2457 * This function takes the skb with 802.3 header and reformats the header to
2458 * the appropriate IEEE 802.11 header based on which interface the packet is
2459 * being transmitted on.
2461 * Note that this function also takes care of the TX status request and
2462 * potential unsharing of the SKB - this needs to be interleaved with the
2465 * The function requires the read-side RCU lock held
2467 * Returns: the (possibly reallocated) skb or an ERR_PTR() code
2469 static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
2470 struct sk_buff *skb, u32 info_flags,
2471 struct sta_info *sta, u32 ctrl_flags)
2473 struct ieee80211_local *local = sdata->local;
2474 struct ieee80211_tx_info *info;
2476 u16 ethertype, hdrlen, meshhdrlen = 0;
2478 struct ieee80211_hdr hdr;
2479 struct ieee80211s_hdr mesh_hdr __maybe_unused;
2480 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
2481 const u8 *encaps_data;
2482 int encaps_len, skip_header_bytes;
2483 bool wme_sta = false, authorized = false;
2487 struct ieee80211_chanctx_conf *chanctx_conf;
2488 struct ieee80211_sub_if_data *ap_sdata;
2489 enum nl80211_band band;
2495 #ifdef CONFIG_MAC80211_DEBUGFS
2496 if (local->force_tx_status)
2497 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2500 /* convert Ethernet header to proper 802.11 header (based on
2501 * operation mode) */
2502 ethertype = (skb->data[12] << 8) | skb->data[13];
2503 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2505 switch (sdata->vif.type) {
2506 case NL80211_IFTYPE_AP_VLAN:
2507 if (sdata->wdev.use_4addr) {
2508 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2510 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
2511 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2512 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2513 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2515 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2516 wme_sta = sta->sta.wme;
2518 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2520 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
2521 if (!chanctx_conf) {
2525 band = chanctx_conf->def.chan->band;
2526 if (sdata->wdev.use_4addr)
2529 case NL80211_IFTYPE_AP:
2530 if (sdata->vif.type == NL80211_IFTYPE_AP)
2531 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2532 if (!chanctx_conf) {
2536 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2538 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2539 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2540 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2542 band = chanctx_conf->def.chan->band;
2544 case NL80211_IFTYPE_WDS:
2545 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2547 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
2548 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2549 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2550 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2553 * This is the exception! WDS style interfaces are prohibited
2554 * when channel contexts are in used so this must be valid
2556 band = local->hw.conf.chandef.chan->band;
2558 #ifdef CONFIG_MAC80211_MESH
2559 case NL80211_IFTYPE_MESH_POINT:
2560 if (!is_multicast_ether_addr(skb->data)) {
2561 struct sta_info *next_hop;
2562 bool mpp_lookup = true;
2564 mpath = mesh_path_lookup(sdata, skb->data);
2567 next_hop = rcu_dereference(mpath->next_hop);
2569 !(mpath->flags & (MESH_PATH_ACTIVE |
2570 MESH_PATH_RESOLVING)))
2575 mppath = mpp_path_lookup(sdata, skb->data);
2577 mppath->exp_time = jiffies;
2580 if (mppath && mpath)
2581 mesh_path_del(sdata, mpath->dst);
2585 * Use address extension if it is a packet from
2586 * another interface or if we know the destination
2587 * is being proxied by a portal (i.e. portal address
2588 * differs from proxied address)
2590 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2591 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
2592 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2593 skb->data, skb->data + ETH_ALEN);
2594 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2597 /* DS -> MBSS (802.11-2012 13.11.3.3).
2598 * For unicast with unknown forwarding information,
2599 * destination might be in the MBSS or if that fails
2600 * forwarded to another mesh gate. In either case
2601 * resolution will be handled in ieee80211_xmit(), so
2602 * leave the original DA. This also works for mcast */
2603 const u8 *mesh_da = skb->data;
2606 mesh_da = mppath->mpp;
2608 mesh_da = mpath->dst;
2610 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2611 mesh_da, sdata->vif.addr);
2612 if (is_multicast_ether_addr(mesh_da))
2613 /* DA TA mSA AE:SA */
2614 meshhdrlen = ieee80211_new_mesh_header(
2616 skb->data + ETH_ALEN, NULL);
2618 /* RA TA mDA mSA AE:DA SA */
2619 meshhdrlen = ieee80211_new_mesh_header(
2620 sdata, &mesh_hdr, skb->data,
2621 skb->data + ETH_ALEN);
2624 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2625 if (!chanctx_conf) {
2629 band = chanctx_conf->def.chan->band;
2631 /* For injected frames, fill RA right away as nexthop lookup
2634 if ((ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP) &&
2635 is_zero_ether_addr(hdr.addr1))
2636 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2639 case NL80211_IFTYPE_STATION:
2640 /* we already did checks when looking up the RA STA */
2641 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
2645 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2646 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2647 memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2649 } else if (sdata->u.mgd.use_4addr &&
2650 cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2651 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2652 IEEE80211_FCTL_TODS);
2654 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2655 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2656 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2657 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2660 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2662 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2663 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2664 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2667 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2668 if (!chanctx_conf) {
2672 band = chanctx_conf->def.chan->band;
2674 case NL80211_IFTYPE_OCB:
2676 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2677 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2678 eth_broadcast_addr(hdr.addr3);
2680 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2681 if (!chanctx_conf) {
2685 band = chanctx_conf->def.chan->band;
2687 case NL80211_IFTYPE_ADHOC:
2689 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2690 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2691 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
2693 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2694 if (!chanctx_conf) {
2698 band = chanctx_conf->def.chan->band;
2705 multicast = is_multicast_ether_addr(hdr.addr1);
2707 /* sta is always NULL for mesh */
2709 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2710 wme_sta = sta->sta.wme;
2711 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2712 /* For mesh, the use of the QoS header is mandatory */
2716 /* receiver does QoS (which also means we do) use it */
2718 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2723 * Drop unicast frames to unauthorised stations unless they are
2724 * EAPOL frames from the local station.
2726 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
2727 (sdata->vif.type != NL80211_IFTYPE_OCB) &&
2728 !multicast && !authorized &&
2729 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
2730 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
2731 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2732 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
2733 sdata->name, hdr.addr1);
2736 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2742 if (unlikely(!multicast && skb->sk &&
2743 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS))
2744 info_id = ieee80211_store_ack_skb(local, skb, &info_flags);
2747 * If the skb is shared we need to obtain our own copy.
2749 if (skb_shared(skb)) {
2750 struct sk_buff *tmp_skb = skb;
2752 /* can't happen -- skb is a clone if info_id != 0 */
2755 skb = skb_clone(skb, GFP_ATOMIC);
2764 hdr.frame_control = fc;
2765 hdr.duration_id = 0;
2768 skip_header_bytes = ETH_HLEN;
2769 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2770 encaps_data = bridge_tunnel_header;
2771 encaps_len = sizeof(bridge_tunnel_header);
2772 skip_header_bytes -= 2;
2773 } else if (ethertype >= ETH_P_802_3_MIN) {
2774 encaps_data = rfc1042_header;
2775 encaps_len = sizeof(rfc1042_header);
2776 skip_header_bytes -= 2;
2782 skb_pull(skb, skip_header_bytes);
2783 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
2786 * So we need to modify the skb header and hence need a copy of
2787 * that. The head_need variable above doesn't, so far, include
2788 * the needed header space that we don't need right away. If we
2789 * can, then we don't reallocate right now but only after the
2790 * frame arrives at the master device (if it does...)
2792 * If we cannot, however, then we will reallocate to include all
2793 * the ever needed space. Also, if we need to reallocate it anyway,
2794 * make it big enough for everything we may ever need.
2797 if (head_need > 0 || skb_cloned(skb)) {
2798 head_need += sdata->encrypt_headroom;
2799 head_need += local->tx_headroom;
2800 head_need = max_t(int, 0, head_need);
2801 if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
2802 ieee80211_free_txskb(&local->hw, skb);
2804 return ERR_PTR(-ENOMEM);
2809 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
2811 #ifdef CONFIG_MAC80211_MESH
2813 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
2816 if (ieee80211_is_data_qos(fc)) {
2817 __le16 *qos_control;
2819 qos_control = skb_push(skb, 2);
2820 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2822 * Maybe we could actually set some fields here, for now just
2823 * initialise to zero to indicate no special operation.
2827 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2829 skb_reset_mac_header(skb);
2831 info = IEEE80211_SKB_CB(skb);
2832 memset(info, 0, sizeof(*info));
2834 info->flags = info_flags;
2835 info->ack_frame_id = info_id;
2837 info->control.flags = ctrl_flags;
2842 return ERR_PTR(ret);
2846 * fast-xmit overview
2848 * The core idea of this fast-xmit is to remove per-packet checks by checking
2849 * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2850 * checks that are needed to get the sta->fast_tx pointer assigned, after which
2851 * much less work can be done per packet. For example, fragmentation must be
2852 * disabled or the fast_tx pointer will not be set. All the conditions are seen
2855 * Once assigned, the fast_tx data structure also caches the per-packet 802.11
2856 * header and other data to aid packet processing in ieee80211_xmit_fast().
2858 * The most difficult part of this is that when any of these assumptions
2859 * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
2860 * ieee80211_check_fast_xmit() or friends) is required to reset the data,
2861 * since the per-packet code no longer checks the conditions. This is reflected
2862 * by the calls to these functions throughout the rest of the code, and must be
2863 * maintained if any of the TX path checks change.
2866 void ieee80211_check_fast_xmit(struct sta_info *sta)
2868 struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
2869 struct ieee80211_local *local = sta->local;
2870 struct ieee80211_sub_if_data *sdata = sta->sdata;
2871 struct ieee80211_hdr *hdr = (void *)build.hdr;
2872 struct ieee80211_chanctx_conf *chanctx_conf;
2875 if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
2878 /* Locking here protects both the pointer itself, and against concurrent
2879 * invocations winning data access races to, e.g., the key pointer that
2881 * Without it, the invocation of this function right after the key
2882 * pointer changes wouldn't be sufficient, as another CPU could access
2883 * the pointer, then stall, and then do the cache update after the CPU
2884 * that invalidated the key.
2885 * With the locking, such scenarios cannot happen as the check for the
2886 * key and the fast-tx assignment are done atomically, so the CPU that
2887 * modifies the key will either wait or other one will see the key
2888 * cleared/changed already.
2890 spin_lock_bh(&sta->lock);
2891 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
2892 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2893 sdata->vif.type == NL80211_IFTYPE_STATION)
2896 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2899 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
2900 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
2901 test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
2902 test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
2905 if (sdata->noack_map)
2908 /* fast-xmit doesn't handle fragmentation at all */
2909 if (local->hw.wiphy->frag_threshold != (u32)-1 &&
2910 !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
2914 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2915 if (!chanctx_conf) {
2919 build.band = chanctx_conf->def.chan->band;
2922 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2924 switch (sdata->vif.type) {
2925 case NL80211_IFTYPE_ADHOC:
2927 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2928 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2929 memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
2932 case NL80211_IFTYPE_STATION:
2933 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2935 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2936 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2937 memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
2942 if (sdata->u.mgd.use_4addr) {
2943 /* non-regular ethertype cannot use the fastpath */
2944 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2945 IEEE80211_FCTL_TODS);
2947 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2948 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2949 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2950 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2954 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2956 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2957 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2958 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2961 case NL80211_IFTYPE_AP_VLAN:
2962 if (sdata->wdev.use_4addr) {
2963 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2964 IEEE80211_FCTL_TODS);
2966 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
2967 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2968 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2969 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2974 case NL80211_IFTYPE_AP:
2975 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2977 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2978 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2979 build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
2983 /* not handled on fast-xmit */
2989 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2992 /* We store the key here so there's no point in using rcu_dereference()
2993 * but that's fine because the code that changes the pointers will call
2994 * this function after doing so. For a single CPU that would be enough,
2995 * for multiple see the comment above.
2997 build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
2999 build.key = rcu_access_pointer(sdata->default_unicast_key);
3001 bool gen_iv, iv_spc, mmic;
3003 gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
3004 iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3005 mmic = build.key->conf.flags &
3006 (IEEE80211_KEY_FLAG_GENERATE_MMIC |
3007 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
3009 /* don't handle software crypto */
3010 if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
3013 /* Key is being removed */
3014 if (build.key->flags & KEY_FLAG_TAINTED)
3017 switch (build.key->conf.cipher) {
3018 case WLAN_CIPHER_SUITE_CCMP:
3019 case WLAN_CIPHER_SUITE_CCMP_256:
3021 build.pn_offs = build.hdr_len;
3022 if (gen_iv || iv_spc)
3023 build.hdr_len += IEEE80211_CCMP_HDR_LEN;
3025 case WLAN_CIPHER_SUITE_GCMP:
3026 case WLAN_CIPHER_SUITE_GCMP_256:
3028 build.pn_offs = build.hdr_len;
3029 if (gen_iv || iv_spc)
3030 build.hdr_len += IEEE80211_GCMP_HDR_LEN;
3032 case WLAN_CIPHER_SUITE_TKIP:
3033 /* cannot handle MMIC or IV generation in xmit-fast */
3037 build.hdr_len += IEEE80211_TKIP_IV_LEN;
3039 case WLAN_CIPHER_SUITE_WEP40:
3040 case WLAN_CIPHER_SUITE_WEP104:
3041 /* cannot handle IV generation in fast-xmit */
3045 build.hdr_len += IEEE80211_WEP_IV_LEN;
3047 case WLAN_CIPHER_SUITE_AES_CMAC:
3048 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3049 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3050 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3052 "management cipher suite 0x%x enabled for data\n",
3053 build.key->conf.cipher);
3056 /* we don't know how to generate IVs for this at all */
3057 if (WARN_ON(gen_iv))
3059 /* pure hardware keys are OK, of course */
3060 if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
3062 /* cipher scheme might require space allocation */
3064 build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
3067 build.hdr_len += build.key->conf.iv_len;
3070 fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3073 hdr->frame_control = fc;
3075 memcpy(build.hdr + build.hdr_len,
3076 rfc1042_header, sizeof(rfc1042_header));
3077 build.hdr_len += sizeof(rfc1042_header);
3079 fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3080 /* if the kmemdup fails, continue w/o fast_tx */
3085 /* we might have raced against another call to this function */
3086 old = rcu_dereference_protected(sta->fast_tx,
3087 lockdep_is_held(&sta->lock));
3088 rcu_assign_pointer(sta->fast_tx, fast_tx);
3090 kfree_rcu(old, rcu_head);
3091 spin_unlock_bh(&sta->lock);
3094 void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3096 struct sta_info *sta;
3099 list_for_each_entry_rcu(sta, &local->sta_list, list)
3100 ieee80211_check_fast_xmit(sta);
3104 void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3106 struct ieee80211_local *local = sdata->local;
3107 struct sta_info *sta;
3111 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3112 if (sdata != sta->sdata &&
3113 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3115 ieee80211_check_fast_xmit(sta);
3121 void ieee80211_clear_fast_xmit(struct sta_info *sta)
3123 struct ieee80211_fast_tx *fast_tx;
3125 spin_lock_bh(&sta->lock);
3126 fast_tx = rcu_dereference_protected(sta->fast_tx,
3127 lockdep_is_held(&sta->lock));
3128 RCU_INIT_POINTER(sta->fast_tx, NULL);
3129 spin_unlock_bh(&sta->lock);
3132 kfree_rcu(fast_tx, rcu_head);
3135 static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
3136 struct sk_buff *skb, int headroom)
3138 if (skb_headroom(skb) < headroom) {
3139 I802_DEBUG_INC(local->tx_expand_skb_head);
3141 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
3142 wiphy_debug(local->hw.wiphy,
3143 "failed to reallocate TX buffer\n");
3151 static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3152 struct ieee80211_fast_tx *fast_tx,
3153 struct sk_buff *skb)
3155 struct ieee80211_local *local = sdata->local;
3156 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3157 struct ieee80211_hdr *hdr;
3158 struct ethhdr *amsdu_hdr;
3159 int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3160 int subframe_len = skb->len - hdr_len;
3162 u8 *qc, *h_80211_src, *h_80211_dst;
3165 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3168 if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3171 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(*amsdu_hdr)))
3174 data = skb_push(skb, sizeof(*amsdu_hdr));
3175 memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
3177 amsdu_hdr = data + hdr_len;
3178 /* h_80211_src/dst is addr* field within hdr */
3179 h_80211_src = data + fast_tx->sa_offs;
3180 h_80211_dst = data + fast_tx->da_offs;
3182 amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3183 ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3184 ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
3186 /* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3187 * fields needs to be changed to BSSID for A-MSDU frames depending
3188 * on FromDS/ToDS values.
3190 switch (sdata->vif.type) {
3191 case NL80211_IFTYPE_STATION:
3192 bssid = sdata->u.mgd.bssid;
3194 case NL80211_IFTYPE_AP:
3195 case NL80211_IFTYPE_AP_VLAN:
3196 bssid = sdata->vif.addr;
3202 if (bssid && ieee80211_has_fromds(hdr->frame_control))
3203 ether_addr_copy(h_80211_src, bssid);
3205 if (bssid && ieee80211_has_tods(hdr->frame_control))
3206 ether_addr_copy(h_80211_dst, bssid);
3208 qc = ieee80211_get_qos_ctl(hdr);
3209 *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3211 info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3216 static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3217 struct sta_info *sta,
3218 struct ieee80211_fast_tx *fast_tx,
3219 struct sk_buff *skb)
3221 struct ieee80211_local *local = sdata->local;
3222 struct fq *fq = &local->fq;
3224 struct fq_flow *flow;
3225 u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3226 struct ieee80211_txq *txq = sta->sta.txq[tid];
3227 struct txq_info *txqi;
3228 struct sk_buff **frag_tail, *head;
3229 int subframe_len = skb->len - ETH_ALEN;
3230 u8 max_subframes = sta->sta.max_amsdu_subframes;
3231 int max_frags = local->hw.max_tx_fragments;
3232 int max_amsdu_len = sta->sta.max_amsdu_len;
3238 unsigned int orig_len;
3239 int n = 2, nfrags, pad = 0;
3242 if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
3245 if (skb_is_gso(skb))
3251 txqi = to_txq_info(txq);
3252 if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3255 if (sta->sta.max_rc_amsdu_len)
3256 max_amsdu_len = min_t(int, max_amsdu_len,
3257 sta->sta.max_rc_amsdu_len);
3259 if (sta->sta.max_tid_amsdu_len[tid])
3260 max_amsdu_len = min_t(int, max_amsdu_len,
3261 sta->sta.max_tid_amsdu_len[tid]);
3263 flow_idx = fq_flow_idx(fq, skb);
3265 spin_lock_bh(&fq->lock);
3267 /* TODO: Ideally aggregation should be done on dequeue to remain
3268 * responsive to environment changes.
3272 flow = fq_flow_classify(fq, tin, flow_idx, skb,
3273 fq_flow_get_default_func);
3274 head = skb_peek_tail(&flow->queue);
3275 if (!head || skb_is_gso(head))
3278 orig_truesize = head->truesize;
3279 orig_len = head->len;
3281 if (skb->len + head->len > max_amsdu_len)
3284 nfrags = 1 + skb_shinfo(skb)->nr_frags;
3285 nfrags += 1 + skb_shinfo(head)->nr_frags;
3286 frag_tail = &skb_shinfo(head)->frag_list;
3287 while (*frag_tail) {
3288 nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3289 frag_tail = &(*frag_tail)->next;
3293 if (max_subframes && n > max_subframes)
3296 if (max_frags && nfrags > max_frags)
3299 if (!drv_can_aggregate_in_amsdu(local, head, skb))
3302 if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
3306 * Pad out the previous subframe to a multiple of 4 by adding the
3307 * padding to the next one, that's being added. Note that head->len
3308 * is the length of the full A-MSDU, but that works since each time
3309 * we add a new subframe we pad out the previous one to a multiple
3310 * of 4 and thus it no longer matters in the next round.
3312 hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3313 if ((head->len - hdrlen) & 3)
3314 pad = 4 - ((head->len - hdrlen) & 3);
3316 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3321 data = skb_push(skb, ETH_ALEN + 2);
3322 memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3324 data += 2 * ETH_ALEN;
3325 len = cpu_to_be16(subframe_len);
3326 memcpy(data, &len, 2);
3327 memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3329 memset(skb_push(skb, pad), 0, pad);
3331 head->len += skb->len;
3332 head->data_len += skb->len;
3336 fq->memory_usage += head->truesize - orig_truesize;
3337 if (head->len != orig_len) {
3338 flow->backlog += head->len - orig_len;
3339 tin->backlog_bytes += head->len - orig_len;
3341 fq_recalc_backlog(fq, tin, flow);
3344 spin_unlock_bh(&fq->lock);
3350 * Can be called while the sta lock is held. Anything that can cause packets to
3351 * be generated will cause deadlock!
3353 static void ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3354 struct sta_info *sta, u8 pn_offs,
3355 struct ieee80211_key *key,
3356 struct sk_buff *skb)
3358 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3359 struct ieee80211_hdr *hdr = (void *)skb->data;
3360 u8 tid = IEEE80211_NUM_TIDS;
3363 info->control.hw_key = &key->conf;
3365 ieee80211_tx_stats(skb->dev, skb->len);
3367 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3368 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3369 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3371 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3372 hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3373 sdata->sequence_number += 0x10;
3376 if (skb_shinfo(skb)->gso_size)
3377 sta->tx_stats.msdu[tid] +=
3378 DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3380 sta->tx_stats.msdu[tid]++;
3382 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3384 /* statistics normally done by ieee80211_tx_h_stats (but that
3385 * has to consider fragmentation, so is more complex)
3387 sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3388 sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
3392 u8 *crypto_hdr = skb->data + pn_offs;
3394 switch (key->conf.cipher) {
3395 case WLAN_CIPHER_SUITE_CCMP:
3396 case WLAN_CIPHER_SUITE_CCMP_256:
3397 case WLAN_CIPHER_SUITE_GCMP:
3398 case WLAN_CIPHER_SUITE_GCMP_256:
3399 pn = atomic64_inc_return(&key->conf.tx_pn);
3401 crypto_hdr[1] = pn >> 8;
3402 crypto_hdr[3] = 0x20 | (key->conf.keyidx << 6);
3403 crypto_hdr[4] = pn >> 16;
3404 crypto_hdr[5] = pn >> 24;
3405 crypto_hdr[6] = pn >> 32;
3406 crypto_hdr[7] = pn >> 40;
3412 static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
3413 struct sta_info *sta,
3414 struct ieee80211_fast_tx *fast_tx,
3415 struct sk_buff *skb)
3417 struct ieee80211_local *local = sdata->local;
3418 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3419 int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3420 int hw_headroom = sdata->local->hw.extra_tx_headroom;
3422 struct ieee80211_tx_info *info;
3423 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3424 struct ieee80211_tx_data tx;
3425 ieee80211_tx_result r;
3426 struct tid_ampdu_tx *tid_tx = NULL;
3427 u8 tid = IEEE80211_NUM_TIDS;
3429 /* control port protocol needs a lot of special handling */
3430 if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3433 /* only RFC 1042 SNAP */
3434 if (ethertype < ETH_P_802_3_MIN)
3437 /* don't handle TX status request here either */
3438 if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3441 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3442 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3443 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
3445 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3447 if (tid_tx->timeout)
3448 tid_tx->last_tx = jiffies;
3452 /* after this point (skb is modified) we cannot return false */
3454 if (skb_shared(skb)) {
3455 struct sk_buff *tmp_skb = skb;
3457 skb = skb_clone(skb, GFP_ATOMIC);
3464 if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3465 ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
3468 /* will not be crypto-handled beyond what we do here, so use false
3469 * as the may-encrypt argument for the resize to not account for
3470 * more room than we already have in 'extra_head'
3472 if (unlikely(ieee80211_skb_resize(sdata, skb,
3473 max_t(int, extra_head + hw_headroom -
3474 skb_headroom(skb), 0),
3480 memcpy(ð, skb->data, ETH_HLEN - 2);
3481 hdr = skb_push(skb, extra_head);
3482 memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3483 memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
3484 memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
3486 info = IEEE80211_SKB_CB(skb);
3487 memset(info, 0, sizeof(*info));
3488 info->band = fast_tx->band;
3489 info->control.vif = &sdata->vif;
3490 info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3491 IEEE80211_TX_CTL_DONTFRAG |
3492 (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
3493 info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT;
3495 #ifdef CONFIG_MAC80211_DEBUGFS
3496 if (local->force_tx_status)
3497 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3500 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3501 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3502 *ieee80211_get_qos_ctl(hdr) = tid;
3505 __skb_queue_head_init(&tx.skbs);
3507 tx.flags = IEEE80211_TX_UNICAST;
3511 tx.key = fast_tx->key;
3513 if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
3515 r = ieee80211_tx_h_rate_ctrl(&tx);
3519 if (r != TX_CONTINUE) {
3526 if (ieee80211_queue_skb(local, sdata, sta, skb))
3529 ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3532 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3533 sdata = container_of(sdata->bss,
3534 struct ieee80211_sub_if_data, u.ap);
3536 __skb_queue_tail(&tx.skbs, skb);
3537 ieee80211_tx_frags(local, &sdata->vif, sta, &tx.skbs, false);
3541 struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3542 struct ieee80211_txq *txq)
3544 struct ieee80211_local *local = hw_to_local(hw);
3545 struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3546 struct ieee80211_hdr *hdr;
3547 struct sk_buff *skb = NULL;
3548 struct fq *fq = &local->fq;
3549 struct fq_tin *tin = &txqi->tin;
3550 struct ieee80211_tx_info *info;
3551 struct ieee80211_tx_data tx;
3552 ieee80211_tx_result r;
3553 struct ieee80211_vif *vif = txq->vif;
3555 WARN_ON_ONCE(softirq_count() == 0);
3557 if (!ieee80211_txq_airtime_check(hw, txq))
3561 spin_lock_bh(&fq->lock);
3563 if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags) ||
3564 test_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags))
3567 if (vif->txqs_stopped[ieee80211_ac_from_tid(txq->tid)]) {
3568 set_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags);
3572 /* Make sure fragments stay together. */
3573 skb = __skb_dequeue(&txqi->frags);
3577 skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
3581 spin_unlock_bh(&fq->lock);
3583 hdr = (struct ieee80211_hdr *)skb->data;
3584 info = IEEE80211_SKB_CB(skb);
3586 memset(&tx, 0, sizeof(tx));
3587 __skb_queue_head_init(&tx.skbs);
3590 tx.sdata = vif_to_sdata(info->control.vif);
3593 tx.sta = container_of(txq->sta, struct sta_info, sta);
3596 * The key can be removed while the packet was queued, so need to call
3597 * this here to get the current key.
3599 r = ieee80211_tx_h_select_key(&tx);
3600 if (r != TX_CONTINUE) {
3601 ieee80211_free_txskb(&local->hw, skb);
3605 if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
3606 info->flags |= IEEE80211_TX_CTL_AMPDU;
3608 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
3610 if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
3611 struct sta_info *sta = container_of(txq->sta, struct sta_info,
3616 (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
3617 pn_offs = ieee80211_hdrlen(hdr->frame_control);
3619 ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
3622 if (invoke_tx_handlers_late(&tx))
3625 skb = __skb_dequeue(&tx.skbs);
3627 if (!skb_queue_empty(&tx.skbs)) {
3628 spin_lock_bh(&fq->lock);
3629 skb_queue_splice_tail(&tx.skbs, &txqi->frags);
3630 spin_unlock_bh(&fq->lock);
3634 if (skb_has_frag_list(skb) &&
3635 !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
3636 if (skb_linearize(skb)) {
3637 ieee80211_free_txskb(&local->hw, skb);
3642 switch (tx.sdata->vif.type) {
3643 case NL80211_IFTYPE_MONITOR:
3644 if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
3645 vif = &tx.sdata->vif;
3648 tx.sdata = rcu_dereference(local->monitor_sdata);
3650 vif = &tx.sdata->vif;
3652 vif->hw_queue[skb_get_queue_mapping(skb)];
3653 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
3654 ieee80211_free_txskb(&local->hw, skb);
3660 case NL80211_IFTYPE_AP_VLAN:
3661 tx.sdata = container_of(tx.sdata->bss,
3662 struct ieee80211_sub_if_data, u.ap);
3665 vif = &tx.sdata->vif;
3669 IEEE80211_SKB_CB(skb)->control.vif = vif;
3671 if (local->airtime_flags & AIRTIME_USE_AQL) {
3674 airtime = ieee80211_calc_expected_tx_airtime(hw, vif, txq->sta,
3677 airtime = ieee80211_info_set_tx_time_est(info, airtime);
3678 ieee80211_sta_update_pending_airtime(local, tx.sta,
3688 spin_unlock_bh(&fq->lock);
3692 EXPORT_SYMBOL(ieee80211_tx_dequeue);
3694 struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac)
3696 struct ieee80211_local *local = hw_to_local(hw);
3697 struct ieee80211_txq *ret = NULL;
3698 struct txq_info *txqi = NULL, *head = NULL;
3699 bool found_eligible_txq = false;
3701 spin_lock_bh(&local->active_txq_lock[ac]);
3704 txqi = list_first_entry_or_null(&local->active_txqs[ac],
3711 if (!found_eligible_txq)
3714 found_eligible_txq = false;
3720 if (txqi->txq.sta) {
3721 struct sta_info *sta = container_of(txqi->txq.sta,
3722 struct sta_info, sta);
3723 bool aql_check = ieee80211_txq_airtime_check(hw, &txqi->txq);
3724 s64 deficit = sta->airtime[txqi->txq.ac].deficit;
3727 found_eligible_txq = true;
3730 sta->airtime[txqi->txq.ac].deficit +=
3731 sta->airtime_weight;
3733 if (deficit < 0 || !aql_check) {
3734 list_move_tail(&txqi->schedule_order,
3735 &local->active_txqs[txqi->txq.ac]);
3741 if (txqi->schedule_round == local->schedule_round[ac])
3744 list_del_init(&txqi->schedule_order);
3745 txqi->schedule_round = local->schedule_round[ac];
3749 spin_unlock_bh(&local->active_txq_lock[ac]);
3752 EXPORT_SYMBOL(ieee80211_next_txq);
3754 void __ieee80211_schedule_txq(struct ieee80211_hw *hw,
3755 struct ieee80211_txq *txq,
3758 struct ieee80211_local *local = hw_to_local(hw);
3759 struct txq_info *txqi = to_txq_info(txq);
3761 spin_lock_bh(&local->active_txq_lock[txq->ac]);
3763 if (list_empty(&txqi->schedule_order) &&
3764 (force || !skb_queue_empty(&txqi->frags) ||
3765 txqi->tin.backlog_packets)) {
3766 /* If airtime accounting is active, always enqueue STAs at the
3767 * head of the list to ensure that they only get moved to the
3768 * back by the airtime DRR scheduler once they have a negative
3769 * deficit. A station that already has a negative deficit will
3770 * get immediately moved to the back of the list on the next
3771 * call to ieee80211_next_txq().
3773 if (txqi->txq.sta &&
3774 wiphy_ext_feature_isset(local->hw.wiphy,
3775 NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
3776 list_add(&txqi->schedule_order,
3777 &local->active_txqs[txq->ac]);
3779 list_add_tail(&txqi->schedule_order,
3780 &local->active_txqs[txq->ac]);
3783 spin_unlock_bh(&local->active_txq_lock[txq->ac]);
3785 EXPORT_SYMBOL(__ieee80211_schedule_txq);
3787 bool ieee80211_txq_airtime_check(struct ieee80211_hw *hw,
3788 struct ieee80211_txq *txq)
3790 struct sta_info *sta;
3791 struct ieee80211_local *local = hw_to_local(hw);
3793 if (!(local->airtime_flags & AIRTIME_USE_AQL))
3799 sta = container_of(txq->sta, struct sta_info, sta);
3800 if (atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
3801 sta->airtime[txq->ac].aql_limit_low)
3804 if (atomic_read(&local->aql_total_pending_airtime) <
3805 local->aql_threshold &&
3806 atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
3807 sta->airtime[txq->ac].aql_limit_high)
3812 EXPORT_SYMBOL(ieee80211_txq_airtime_check);
3814 bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
3815 struct ieee80211_txq *txq)
3817 struct ieee80211_local *local = hw_to_local(hw);
3818 struct txq_info *iter, *tmp, *txqi = to_txq_info(txq);
3819 struct sta_info *sta;
3822 spin_lock_bh(&local->active_txq_lock[ac]);
3827 if (list_empty(&txqi->schedule_order))
3830 list_for_each_entry_safe(iter, tmp, &local->active_txqs[ac],
3835 if (!iter->txq.sta) {
3836 list_move_tail(&iter->schedule_order,
3837 &local->active_txqs[ac]);
3840 sta = container_of(iter->txq.sta, struct sta_info, sta);
3841 if (sta->airtime[ac].deficit < 0)
3842 sta->airtime[ac].deficit += sta->airtime_weight;
3843 list_move_tail(&iter->schedule_order, &local->active_txqs[ac]);
3846 sta = container_of(txqi->txq.sta, struct sta_info, sta);
3847 if (sta->airtime[ac].deficit >= 0)
3850 sta->airtime[ac].deficit += sta->airtime_weight;
3851 list_move_tail(&txqi->schedule_order, &local->active_txqs[ac]);
3852 spin_unlock_bh(&local->active_txq_lock[ac]);
3856 if (!list_empty(&txqi->schedule_order))
3857 list_del_init(&txqi->schedule_order);
3858 spin_unlock_bh(&local->active_txq_lock[ac]);
3862 EXPORT_SYMBOL(ieee80211_txq_may_transmit);
3864 void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
3866 struct ieee80211_local *local = hw_to_local(hw);
3868 spin_lock_bh(&local->active_txq_lock[ac]);
3869 local->schedule_round[ac]++;
3870 spin_unlock_bh(&local->active_txq_lock[ac]);
3872 EXPORT_SYMBOL(ieee80211_txq_schedule_start);
3874 void __ieee80211_subif_start_xmit(struct sk_buff *skb,
3875 struct net_device *dev,
3879 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3880 struct ieee80211_local *local = sdata->local;
3881 struct sta_info *sta;
3882 struct sk_buff *next;
3884 if (unlikely(skb->len < ETH_HLEN)) {
3891 if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
3897 if (local->ops->wake_tx_queue) {
3898 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
3899 skb_set_queue_mapping(skb, queue);
3903 struct ieee80211_fast_tx *fast_tx;
3905 sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
3907 fast_tx = rcu_dereference(sta->fast_tx);
3910 ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
3914 if (skb_is_gso(skb)) {
3915 struct sk_buff *segs;
3917 segs = skb_gso_segment(skb, 0);
3925 /* we cannot process non-linear frames on this path */
3926 if (skb_linearize(skb)) {
3931 /* the frame could be fragmented, software-encrypted, and other
3932 * things so we cannot really handle checksum offload with it -
3933 * fix it up in software before we handle anything else.
3935 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3936 skb_set_transport_header(skb,
3937 skb_checksum_start_offset(skb));
3938 if (skb_checksum_help(skb))
3951 skb = ieee80211_build_hdr(sdata, skb, info_flags,
3956 ieee80211_tx_stats(dev, skb->len);
3958 ieee80211_xmit(sdata, sta, skb, 0);
3967 static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
3972 err = skb_ensure_writable(skb, ETH_HLEN);
3976 eth = (void *)skb->data;
3977 ether_addr_copy(eth->h_dest, sta->sta.addr);
3982 static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
3983 struct net_device *dev)
3985 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3986 const struct ethhdr *eth = (void *)skb->data;
3987 const struct vlan_ethhdr *ethvlan = (void *)skb->data;
3990 if (likely(!is_multicast_ether_addr(eth->h_dest)))
3993 switch (sdata->vif.type) {
3994 case NL80211_IFTYPE_AP_VLAN:
3995 if (sdata->u.vlan.sta)
3997 if (sdata->wdev.use_4addr)
4000 case NL80211_IFTYPE_AP:
4001 /* check runtime toggle for this bss */
4002 if (!sdata->bss->multicast_to_unicast)
4009 /* multicast to unicast conversion only for some payload */
4010 ethertype = eth->h_proto;
4011 if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
4012 ethertype = ethvlan->h_vlan_encapsulated_proto;
4013 switch (ethertype) {
4014 case htons(ETH_P_ARP):
4015 case htons(ETH_P_IP):
4016 case htons(ETH_P_IPV6):
4026 ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
4027 struct sk_buff_head *queue)
4029 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4030 struct ieee80211_local *local = sdata->local;
4031 const struct ethhdr *eth = (struct ethhdr *)skb->data;
4032 struct sta_info *sta, *first = NULL;
4033 struct sk_buff *cloned_skb;
4037 list_for_each_entry_rcu(sta, &local->sta_list, list) {
4038 if (sdata != sta->sdata)
4039 /* AP-VLAN mismatch */
4041 if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
4042 /* do not send back to source */
4048 cloned_skb = skb_clone(skb, GFP_ATOMIC);
4051 if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
4052 dev_kfree_skb(cloned_skb);
4055 __skb_queue_tail(queue, cloned_skb);
4058 if (likely(first)) {
4059 if (unlikely(ieee80211_change_da(skb, first)))
4061 __skb_queue_tail(queue, skb);
4063 /* no STA connected, drop */
4070 __skb_queue_purge(queue);
4071 __skb_queue_tail(queue, skb);
4077 * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
4078 * @skb: packet to be sent
4079 * @dev: incoming interface
4081 * On failure skb will be freed.
4083 netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
4084 struct net_device *dev)
4086 if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
4087 struct sk_buff_head queue;
4089 __skb_queue_head_init(&queue);
4090 ieee80211_convert_to_unicast(skb, dev, &queue);
4091 while ((skb = __skb_dequeue(&queue)))
4092 __ieee80211_subif_start_xmit(skb, dev, 0, 0);
4094 __ieee80211_subif_start_xmit(skb, dev, 0, 0);
4097 return NETDEV_TX_OK;
4101 ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
4102 struct sk_buff *skb, u32 info_flags)
4104 struct ieee80211_hdr *hdr;
4105 struct ieee80211_tx_data tx = {
4106 .local = sdata->local,
4109 struct sta_info *sta;
4113 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4115 skb = ERR_PTR(-EINVAL);
4119 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta, 0);
4123 hdr = (void *)skb->data;
4124 tx.sta = sta_info_get(sdata, hdr->addr1);
4127 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
4130 return ERR_PTR(-EINVAL);
4139 * ieee80211_clear_tx_pending may not be called in a context where
4140 * it is possible that it packets could come in again.
4142 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
4144 struct sk_buff *skb;
4147 for (i = 0; i < local->hw.queues; i++) {
4148 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
4149 ieee80211_free_txskb(&local->hw, skb);
4154 * Returns false if the frame couldn't be transmitted but was queued instead,
4155 * which in this case means re-queued -- take as an indication to stop sending
4156 * more pending frames.
4158 static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
4159 struct sk_buff *skb)
4161 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4162 struct ieee80211_sub_if_data *sdata;
4163 struct sta_info *sta;
4164 struct ieee80211_hdr *hdr;
4166 struct ieee80211_chanctx_conf *chanctx_conf;
4168 sdata = vif_to_sdata(info->control.vif);
4170 if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
4171 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4172 if (unlikely(!chanctx_conf)) {
4176 info->band = chanctx_conf->def.chan->band;
4177 result = ieee80211_tx(sdata, NULL, skb, true, 0);
4179 struct sk_buff_head skbs;
4181 __skb_queue_head_init(&skbs);
4182 __skb_queue_tail(&skbs, skb);
4184 hdr = (struct ieee80211_hdr *)skb->data;
4185 sta = sta_info_get(sdata, hdr->addr1);
4187 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
4194 * Transmit all pending packets. Called from tasklet.
4196 void ieee80211_tx_pending(unsigned long data)
4198 struct ieee80211_local *local = (struct ieee80211_local *)data;
4199 unsigned long flags;
4205 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4206 for (i = 0; i < local->hw.queues; i++) {
4208 * If queue is stopped by something other than due to pending
4209 * frames, or we have no pending frames, proceed to next queue.
4211 if (local->queue_stop_reasons[i] ||
4212 skb_queue_empty(&local->pending[i]))
4215 while (!skb_queue_empty(&local->pending[i])) {
4216 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
4217 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4219 if (WARN_ON(!info->control.vif)) {
4220 ieee80211_free_txskb(&local->hw, skb);
4224 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
4227 txok = ieee80211_tx_pending_skb(local, skb);
4228 spin_lock_irqsave(&local->queue_stop_reason_lock,
4234 if (skb_queue_empty(&local->pending[i]))
4235 ieee80211_propagate_queue_wake(local, i);
4237 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4242 /* functions for drivers to get certain frames */
4244 static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
4245 struct ps_data *ps, struct sk_buff *skb,
4250 int i, have_bits = 0, n1, n2;
4252 /* Generate bitmap for TIM only if there are any STAs in power save
4254 if (atomic_read(&ps->num_sta_ps) > 0)
4255 /* in the hope that this is faster than
4256 * checking byte-for-byte */
4257 have_bits = !bitmap_empty((unsigned long *)ps->tim,
4258 IEEE80211_MAX_AID+1);
4260 if (ps->dtim_count == 0)
4261 ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
4266 tim = pos = skb_put(skb, 6);
4267 *pos++ = WLAN_EID_TIM;
4269 *pos++ = ps->dtim_count;
4270 *pos++ = sdata->vif.bss_conf.dtim_period;
4272 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
4275 ps->dtim_bc_mc = aid0 == 1;
4278 /* Find largest even number N1 so that bits numbered 1 through
4279 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
4280 * (N2 + 1) x 8 through 2007 are 0. */
4282 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
4289 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
4296 /* Bitmap control */
4298 /* Part Virt Bitmap */
4299 skb_put(skb, n2 - n1);
4300 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
4302 tim[1] = n2 - n1 + 4;
4304 *pos++ = aid0; /* Bitmap control */
4305 *pos++ = 0; /* Part Virt Bitmap */
4309 static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
4310 struct ps_data *ps, struct sk_buff *skb,
4313 struct ieee80211_local *local = sdata->local;
4316 * Not very nice, but we want to allow the driver to call
4317 * ieee80211_beacon_get() as a response to the set_tim()
4318 * callback. That, however, is already invoked under the
4319 * sta_lock to guarantee consistent and race-free update
4320 * of the tim bitmap in mac80211 and the driver.
4322 if (local->tim_in_locked_section) {
4323 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
4325 spin_lock_bh(&local->tim_lock);
4326 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
4327 spin_unlock_bh(&local->tim_lock);
4333 static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
4334 struct beacon_data *beacon)
4336 struct probe_resp *resp;
4338 size_t beacon_data_len;
4340 u8 count = beacon->csa_current_counter;
4342 switch (sdata->vif.type) {
4343 case NL80211_IFTYPE_AP:
4344 beacon_data = beacon->tail;
4345 beacon_data_len = beacon->tail_len;
4347 case NL80211_IFTYPE_ADHOC:
4348 beacon_data = beacon->head;
4349 beacon_data_len = beacon->head_len;
4351 case NL80211_IFTYPE_MESH_POINT:
4352 beacon_data = beacon->head;
4353 beacon_data_len = beacon->head_len;
4360 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
4361 resp = rcu_dereference(sdata->u.ap.probe_resp);
4363 if (beacon->csa_counter_offsets[i]) {
4364 if (WARN_ON_ONCE(beacon->csa_counter_offsets[i] >=
4370 beacon_data[beacon->csa_counter_offsets[i]] = count;
4373 if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
4374 resp->data[resp->csa_counter_offsets[i]] = count;
4379 static u8 __ieee80211_csa_update_counter(struct beacon_data *beacon)
4381 beacon->csa_current_counter--;
4383 /* the counter should never reach 0 */
4384 WARN_ON_ONCE(!beacon->csa_current_counter);
4386 return beacon->csa_current_counter;
4389 u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
4391 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4392 struct beacon_data *beacon = NULL;
4397 if (sdata->vif.type == NL80211_IFTYPE_AP)
4398 beacon = rcu_dereference(sdata->u.ap.beacon);
4399 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4400 beacon = rcu_dereference(sdata->u.ibss.presp);
4401 else if (ieee80211_vif_is_mesh(&sdata->vif))
4402 beacon = rcu_dereference(sdata->u.mesh.beacon);
4407 count = __ieee80211_csa_update_counter(beacon);
4413 EXPORT_SYMBOL(ieee80211_csa_update_counter);
4415 void ieee80211_csa_set_counter(struct ieee80211_vif *vif, u8 counter)
4417 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4418 struct beacon_data *beacon = NULL;
4422 if (sdata->vif.type == NL80211_IFTYPE_AP)
4423 beacon = rcu_dereference(sdata->u.ap.beacon);
4424 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4425 beacon = rcu_dereference(sdata->u.ibss.presp);
4426 else if (ieee80211_vif_is_mesh(&sdata->vif))
4427 beacon = rcu_dereference(sdata->u.mesh.beacon);
4432 if (counter < beacon->csa_current_counter)
4433 beacon->csa_current_counter = counter;
4438 EXPORT_SYMBOL(ieee80211_csa_set_counter);
4440 bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
4442 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4443 struct beacon_data *beacon = NULL;
4445 size_t beacon_data_len;
4448 if (!ieee80211_sdata_running(sdata))
4452 if (vif->type == NL80211_IFTYPE_AP) {
4453 struct ieee80211_if_ap *ap = &sdata->u.ap;
4455 beacon = rcu_dereference(ap->beacon);
4456 if (WARN_ON(!beacon || !beacon->tail))
4458 beacon_data = beacon->tail;
4459 beacon_data_len = beacon->tail_len;
4460 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
4461 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4463 beacon = rcu_dereference(ifibss->presp);
4467 beacon_data = beacon->head;
4468 beacon_data_len = beacon->head_len;
4469 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
4470 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4472 beacon = rcu_dereference(ifmsh->beacon);
4476 beacon_data = beacon->head;
4477 beacon_data_len = beacon->head_len;
4483 if (!beacon->csa_counter_offsets[0])
4486 if (WARN_ON_ONCE(beacon->csa_counter_offsets[0] > beacon_data_len))
4489 if (beacon_data[beacon->csa_counter_offsets[0]] == 1)
4496 EXPORT_SYMBOL(ieee80211_csa_is_complete);
4498 static struct sk_buff *
4499 __ieee80211_beacon_get(struct ieee80211_hw *hw,
4500 struct ieee80211_vif *vif,
4501 struct ieee80211_mutable_offsets *offs,
4504 struct ieee80211_local *local = hw_to_local(hw);
4505 struct beacon_data *beacon = NULL;
4506 struct sk_buff *skb = NULL;
4507 struct ieee80211_tx_info *info;
4508 struct ieee80211_sub_if_data *sdata = NULL;
4509 enum nl80211_band band;
4510 struct ieee80211_tx_rate_control txrc;
4511 struct ieee80211_chanctx_conf *chanctx_conf;
4512 int csa_off_base = 0;
4516 sdata = vif_to_sdata(vif);
4517 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4519 if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
4523 memset(offs, 0, sizeof(*offs));
4525 if (sdata->vif.type == NL80211_IFTYPE_AP) {
4526 struct ieee80211_if_ap *ap = &sdata->u.ap;
4528 beacon = rcu_dereference(ap->beacon);
4530 if (beacon->csa_counter_offsets[0]) {
4532 __ieee80211_csa_update_counter(beacon);
4534 ieee80211_set_csa(sdata, beacon);
4538 * headroom, head length,
4539 * tail length and maximum TIM length
4541 skb = dev_alloc_skb(local->tx_headroom +
4543 beacon->tail_len + 256 +
4544 local->hw.extra_beacon_tailroom);
4548 skb_reserve(skb, local->tx_headroom);
4549 skb_put_data(skb, beacon->head, beacon->head_len);
4551 ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
4555 offs->tim_offset = beacon->head_len;
4556 offs->tim_length = skb->len - beacon->head_len;
4558 /* for AP the csa offsets are from tail */
4559 csa_off_base = skb->len;
4563 skb_put_data(skb, beacon->tail,
4567 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
4568 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4569 struct ieee80211_hdr *hdr;
4571 beacon = rcu_dereference(ifibss->presp);
4575 if (beacon->csa_counter_offsets[0]) {
4577 __ieee80211_csa_update_counter(beacon);
4579 ieee80211_set_csa(sdata, beacon);
4582 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
4583 local->hw.extra_beacon_tailroom);
4586 skb_reserve(skb, local->tx_headroom);
4587 skb_put_data(skb, beacon->head, beacon->head_len);
4589 hdr = (struct ieee80211_hdr *) skb->data;
4590 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4591 IEEE80211_STYPE_BEACON);
4592 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4593 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4595 beacon = rcu_dereference(ifmsh->beacon);
4599 if (beacon->csa_counter_offsets[0]) {
4601 /* TODO: For mesh csa_counter is in TU, so
4602 * decrementing it by one isn't correct, but
4603 * for now we leave it consistent with overall
4604 * mac80211's behavior.
4606 __ieee80211_csa_update_counter(beacon);
4608 ieee80211_set_csa(sdata, beacon);
4611 if (ifmsh->sync_ops)
4612 ifmsh->sync_ops->adjust_tsf(sdata, beacon);
4614 skb = dev_alloc_skb(local->tx_headroom +
4618 local->hw.extra_beacon_tailroom);
4621 skb_reserve(skb, local->tx_headroom);
4622 skb_put_data(skb, beacon->head, beacon->head_len);
4623 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
4626 offs->tim_offset = beacon->head_len;
4627 offs->tim_length = skb->len - beacon->head_len;
4630 skb_put_data(skb, beacon->tail, beacon->tail_len);
4637 if (offs && beacon) {
4640 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
4641 u16 csa_off = beacon->csa_counter_offsets[i];
4646 offs->csa_counter_offs[i] = csa_off_base + csa_off;
4650 band = chanctx_conf->def.chan->band;
4652 info = IEEE80211_SKB_CB(skb);
4654 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
4655 info->flags |= IEEE80211_TX_CTL_NO_ACK;
4658 memset(&txrc, 0, sizeof(txrc));
4660 txrc.sband = local->hw.wiphy->bands[band];
4661 txrc.bss_conf = &sdata->vif.bss_conf;
4663 txrc.reported_rate.idx = -1;
4664 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
4666 rate_control_get_rate(sdata, NULL, &txrc);
4668 info->control.vif = vif;
4670 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
4671 IEEE80211_TX_CTL_ASSIGN_SEQ |
4672 IEEE80211_TX_CTL_FIRST_FRAGMENT;
4680 ieee80211_beacon_get_template(struct ieee80211_hw *hw,
4681 struct ieee80211_vif *vif,
4682 struct ieee80211_mutable_offsets *offs)
4684 return __ieee80211_beacon_get(hw, vif, offs, true);
4686 EXPORT_SYMBOL(ieee80211_beacon_get_template);
4688 struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
4689 struct ieee80211_vif *vif,
4690 u16 *tim_offset, u16 *tim_length)
4692 struct ieee80211_mutable_offsets offs = {};
4693 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
4694 struct sk_buff *copy;
4695 struct ieee80211_supported_band *sband;
4702 *tim_offset = offs.tim_offset;
4705 *tim_length = offs.tim_length;
4707 if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
4708 !hw_to_local(hw)->monitors)
4711 /* send a copy to monitor interfaces */
4712 copy = skb_copy(bcn, GFP_ATOMIC);
4716 shift = ieee80211_vif_get_shift(vif);
4717 sband = ieee80211_get_sband(vif_to_sdata(vif));
4721 ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false,
4726 EXPORT_SYMBOL(ieee80211_beacon_get_tim);
4728 struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
4729 struct ieee80211_vif *vif)
4731 struct ieee80211_if_ap *ap = NULL;
4732 struct sk_buff *skb = NULL;
4733 struct probe_resp *presp = NULL;
4734 struct ieee80211_hdr *hdr;
4735 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4737 if (sdata->vif.type != NL80211_IFTYPE_AP)
4743 presp = rcu_dereference(ap->probe_resp);
4747 skb = dev_alloc_skb(presp->len);
4751 skb_put_data(skb, presp->data, presp->len);
4753 hdr = (struct ieee80211_hdr *) skb->data;
4754 memset(hdr->addr1, 0, sizeof(hdr->addr1));
4760 EXPORT_SYMBOL(ieee80211_proberesp_get);
4762 struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
4763 struct ieee80211_vif *vif)
4765 struct ieee80211_sub_if_data *sdata;
4766 struct ieee80211_if_managed *ifmgd;
4767 struct ieee80211_pspoll *pspoll;
4768 struct ieee80211_local *local;
4769 struct sk_buff *skb;
4771 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4774 sdata = vif_to_sdata(vif);
4775 ifmgd = &sdata->u.mgd;
4776 local = sdata->local;
4778 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
4782 skb_reserve(skb, local->hw.extra_tx_headroom);
4784 pspoll = skb_put_zero(skb, sizeof(*pspoll));
4785 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
4786 IEEE80211_STYPE_PSPOLL);
4787 pspoll->aid = cpu_to_le16(ifmgd->aid);
4789 /* aid in PS-Poll has its two MSBs each set to 1 */
4790 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
4792 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
4793 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
4797 EXPORT_SYMBOL(ieee80211_pspoll_get);
4799 struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
4800 struct ieee80211_vif *vif,
4803 struct ieee80211_hdr_3addr *nullfunc;
4804 struct ieee80211_sub_if_data *sdata;
4805 struct ieee80211_if_managed *ifmgd;
4806 struct ieee80211_local *local;
4807 struct sk_buff *skb;
4810 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4813 sdata = vif_to_sdata(vif);
4814 ifmgd = &sdata->u.mgd;
4815 local = sdata->local;
4818 struct sta_info *sta;
4821 sta = sta_info_get(sdata, ifmgd->bssid);
4822 qos = sta && sta->sta.wme;
4826 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
4827 sizeof(*nullfunc) + 2);
4831 skb_reserve(skb, local->hw.extra_tx_headroom);
4833 nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
4834 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
4835 IEEE80211_STYPE_NULLFUNC |
4836 IEEE80211_FCTL_TODS);
4838 __le16 qoshdr = cpu_to_le16(7);
4840 BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
4841 IEEE80211_STYPE_NULLFUNC) !=
4842 IEEE80211_STYPE_QOS_NULLFUNC);
4843 nullfunc->frame_control |=
4844 cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
4846 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
4847 skb_put_data(skb, &qoshdr, sizeof(qoshdr));
4850 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
4851 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
4852 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
4856 EXPORT_SYMBOL(ieee80211_nullfunc_get);
4858 struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
4860 const u8 *ssid, size_t ssid_len,
4863 struct ieee80211_local *local = hw_to_local(hw);
4864 struct ieee80211_hdr_3addr *hdr;
4865 struct sk_buff *skb;
4869 ie_ssid_len = 2 + ssid_len;
4871 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
4872 ie_ssid_len + tailroom);
4876 skb_reserve(skb, local->hw.extra_tx_headroom);
4878 hdr = skb_put_zero(skb, sizeof(*hdr));
4879 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4880 IEEE80211_STYPE_PROBE_REQ);
4881 eth_broadcast_addr(hdr->addr1);
4882 memcpy(hdr->addr2, src_addr, ETH_ALEN);
4883 eth_broadcast_addr(hdr->addr3);
4885 pos = skb_put(skb, ie_ssid_len);
4886 *pos++ = WLAN_EID_SSID;
4889 memcpy(pos, ssid, ssid_len);
4894 EXPORT_SYMBOL(ieee80211_probereq_get);
4896 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4897 const void *frame, size_t frame_len,
4898 const struct ieee80211_tx_info *frame_txctl,
4899 struct ieee80211_rts *rts)
4901 const struct ieee80211_hdr *hdr = frame;
4903 rts->frame_control =
4904 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
4905 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
4907 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
4908 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
4910 EXPORT_SYMBOL(ieee80211_rts_get);
4912 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4913 const void *frame, size_t frame_len,
4914 const struct ieee80211_tx_info *frame_txctl,
4915 struct ieee80211_cts *cts)
4917 const struct ieee80211_hdr *hdr = frame;
4919 cts->frame_control =
4920 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
4921 cts->duration = ieee80211_ctstoself_duration(hw, vif,
4922 frame_len, frame_txctl);
4923 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
4925 EXPORT_SYMBOL(ieee80211_ctstoself_get);
4928 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
4929 struct ieee80211_vif *vif)
4931 struct ieee80211_local *local = hw_to_local(hw);
4932 struct sk_buff *skb = NULL;
4933 struct ieee80211_tx_data tx;
4934 struct ieee80211_sub_if_data *sdata;
4936 struct ieee80211_tx_info *info;
4937 struct ieee80211_chanctx_conf *chanctx_conf;
4939 sdata = vif_to_sdata(vif);
4942 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4947 if (sdata->vif.type == NL80211_IFTYPE_AP) {
4948 struct beacon_data *beacon =
4949 rcu_dereference(sdata->u.ap.beacon);
4951 if (!beacon || !beacon->head)
4954 ps = &sdata->u.ap.ps;
4955 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4956 ps = &sdata->u.mesh.ps;
4961 if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
4962 goto out; /* send buffered bc/mc only after DTIM beacon */
4965 skb = skb_dequeue(&ps->bc_buf);
4968 local->total_ps_buffered--;
4970 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
4971 struct ieee80211_hdr *hdr =
4972 (struct ieee80211_hdr *) skb->data;
4973 /* more buffered multicast/broadcast frames ==> set
4974 * MoreData flag in IEEE 802.11 header to inform PS
4976 hdr->frame_control |=
4977 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
4980 if (sdata->vif.type == NL80211_IFTYPE_AP)
4981 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
4982 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
4984 ieee80211_free_txskb(hw, skb);
4987 info = IEEE80211_SKB_CB(skb);
4989 tx.flags |= IEEE80211_TX_PS_BUFFERED;
4990 info->band = chanctx_conf->def.chan->band;
4992 if (invoke_tx_handlers(&tx))
4999 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
5001 int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5003 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5004 struct ieee80211_sub_if_data *sdata = sta->sdata;
5005 struct ieee80211_local *local = sdata->local;
5009 lockdep_assert_held(&local->sta_mtx);
5011 /* only some cases are supported right now */
5012 switch (sdata->vif.type) {
5013 case NL80211_IFTYPE_STATION:
5014 case NL80211_IFTYPE_AP:
5015 case NL80211_IFTYPE_AP_VLAN:
5022 if (WARN_ON(tid >= IEEE80211_NUM_UPS))
5025 if (sta->reserved_tid == tid) {
5030 if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
5031 sdata_err(sdata, "TID reservation already active\n");
5036 ieee80211_stop_vif_queues(sdata->local, sdata,
5037 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5041 /* Tear down BA sessions so we stop aggregating on this TID */
5042 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
5043 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5044 __ieee80211_stop_tx_ba_session(sta, tid,
5045 AGG_STOP_LOCAL_REQUEST);
5048 queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
5049 __ieee80211_flush_queues(local, sdata, queues, false);
5051 sta->reserved_tid = tid;
5053 ieee80211_wake_vif_queues(local, sdata,
5054 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5056 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
5057 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5063 EXPORT_SYMBOL(ieee80211_reserve_tid);
5065 void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5067 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5068 struct ieee80211_sub_if_data *sdata = sta->sdata;
5070 lockdep_assert_held(&sdata->local->sta_mtx);
5072 /* only some cases are supported right now */
5073 switch (sdata->vif.type) {
5074 case NL80211_IFTYPE_STATION:
5075 case NL80211_IFTYPE_AP:
5076 case NL80211_IFTYPE_AP_VLAN:
5083 if (tid != sta->reserved_tid) {
5084 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
5088 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
5090 EXPORT_SYMBOL(ieee80211_unreserve_tid);
5092 void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
5093 struct sk_buff *skb, int tid,
5094 enum nl80211_band band, u32 txdata_flags)
5096 int ac = ieee80211_ac_from_tid(tid);
5098 skb_reset_mac_header(skb);
5099 skb_set_queue_mapping(skb, ac);
5100 skb->priority = tid;
5102 skb->dev = sdata->dev;
5105 * The other path calling ieee80211_xmit is from the tasklet,
5106 * and while we can handle concurrent transmissions locking
5107 * requirements are that we do not come into tx with bhs on.
5110 IEEE80211_SKB_CB(skb)->band = band;
5111 ieee80211_xmit(sdata, NULL, skb, txdata_flags);
5115 int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
5116 const u8 *buf, size_t len,
5117 const u8 *dest, __be16 proto, bool unencrypted)
5119 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5120 struct ieee80211_local *local = sdata->local;
5121 struct sk_buff *skb;
5122 struct ethhdr *ehdr;
5125 /* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
5126 * or Pre-Authentication
5128 if (proto != sdata->control_port_protocol &&
5129 proto != cpu_to_be16(ETH_P_PREAUTH))
5133 flags = IEEE80211_TX_INTFL_DONT_ENCRYPT;
5137 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5138 sizeof(struct ethhdr) + len);
5142 skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
5144 skb_put_data(skb, buf, len);
5146 ehdr = skb_push(skb, sizeof(struct ethhdr));
5147 memcpy(ehdr->h_dest, dest, ETH_ALEN);
5148 memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
5149 ehdr->h_proto = proto;
5152 skb->protocol = htons(ETH_P_802_3);
5153 skb_reset_network_header(skb);
5154 skb_reset_mac_header(skb);
5157 __ieee80211_subif_start_xmit(skb, skb->dev, flags, 0);
5163 int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
5164 const u8 *buf, size_t len)
5166 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5167 struct ieee80211_local *local = sdata->local;
5168 struct sk_buff *skb;
5170 skb = dev_alloc_skb(local->hw.extra_tx_headroom + len +
5171 30 + /* header size */
5172 18); /* 11s header size */
5176 skb_reserve(skb, local->hw.extra_tx_headroom);
5177 skb_put_data(skb, buf, len);
5180 skb->protocol = htons(ETH_P_802_3);
5181 skb_reset_network_header(skb);
5182 skb_reset_mac_header(skb);
5185 __ieee80211_subif_start_xmit(skb, skb->dev, 0,
5186 IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP);