2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright 2013-2014 Intel Mobile Communications GmbH
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 * Transmit and frame generation functions.
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/etherdevice.h>
20 #include <linux/bitmap.h>
21 #include <linux/rcupdate.h>
22 #include <linux/export.h>
23 #include <net/net_namespace.h>
24 #include <net/ieee80211_radiotap.h>
25 #include <net/cfg80211.h>
26 #include <net/mac80211.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;
65 chanctx_conf = rcu_dereference(tx->sdata->vif.chanctx_conf);
67 shift = ieee80211_chandef_get_shift(&chanctx_conf->def);
68 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
72 /* assume HW handles this */
73 if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))
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 if (r->flags & IEEE80211_RATE_MANDATORY_A)
168 case NL80211_BAND_60GHZ:
169 /* TODO, for now fall through */
170 case NUM_NL80211_BANDS:
176 /* No matching basic rate found; use highest suitable mandatory
178 rate = DIV_ROUND_UP(mrate, 1 << shift);
181 /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
182 if (ieee80211_is_data_qos(hdr->frame_control) &&
183 *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
186 /* Time needed to transmit ACK
187 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
188 * to closest integer */
189 dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
190 tx->sdata->vif.bss_conf.use_short_preamble,
194 /* Frame is fragmented: duration increases with time needed to
195 * transmit next fragment plus ACK and 2 x SIFS. */
196 dur *= 2; /* ACK + SIFS */
198 dur += ieee80211_frame_duration(sband->band, next_frag_len,
199 txrate->bitrate, erp,
200 tx->sdata->vif.bss_conf.use_short_preamble,
204 return cpu_to_le16(dur);
208 static ieee80211_tx_result debug_noinline
209 ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
211 struct ieee80211_local *local = tx->local;
212 struct ieee80211_if_managed *ifmgd;
214 /* driver doesn't support power save */
215 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
218 /* hardware does dynamic power save */
219 if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
222 /* dynamic power save disabled */
223 if (local->hw.conf.dynamic_ps_timeout <= 0)
226 /* we are scanning, don't enable power save */
230 if (!local->ps_sdata)
233 /* No point if we're going to suspend */
234 if (local->quiescing)
237 /* dynamic ps is supported only in managed mode */
238 if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
241 ifmgd = &tx->sdata->u.mgd;
244 * Don't wakeup from power save if u-apsd is enabled, voip ac has
245 * u-apsd enabled and the frame is in voip class. This effectively
246 * means that even if all access categories have u-apsd enabled, in
247 * practise u-apsd is only used with the voip ac. This is a
248 * workaround for the case when received voip class packets do not
249 * have correct qos tag for some reason, due the network or the
252 * Note: ifmgd->uapsd_queues access is racy here. If the value is
253 * changed via debugfs, user needs to reassociate manually to have
254 * everything in sync.
256 if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
257 (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
258 skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
261 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
262 ieee80211_stop_queues_by_reason(&local->hw,
263 IEEE80211_MAX_QUEUE_MAP,
264 IEEE80211_QUEUE_STOP_REASON_PS,
266 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
267 ieee80211_queue_work(&local->hw,
268 &local->dynamic_ps_disable_work);
271 /* Don't restart the timer if we're not disassociated */
272 if (!ifmgd->associated)
275 mod_timer(&local->dynamic_ps_timer, jiffies +
276 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
281 static ieee80211_tx_result debug_noinline
282 ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
285 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
286 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
289 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
292 if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
293 test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
294 !ieee80211_is_probe_req(hdr->frame_control) &&
295 !ieee80211_is_nullfunc(hdr->frame_control))
297 * When software scanning only nullfunc frames (to notify
298 * the sleep state to the AP) and probe requests (for the
299 * active scan) are allowed, all other frames should not be
300 * sent and we should not get here, but if we do
301 * nonetheless, drop them to avoid sending them
302 * off-channel. See the link below and
303 * ieee80211_start_scan() for more.
305 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
309 if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
312 if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
315 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
319 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
321 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
322 if (unlikely(!assoc &&
323 ieee80211_is_data(hdr->frame_control))) {
324 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
325 sdata_info(tx->sdata,
326 "dropped data frame to not associated station %pM\n",
329 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
332 } else if (unlikely(tx->sdata->vif.type == NL80211_IFTYPE_AP &&
333 ieee80211_is_data(hdr->frame_control) &&
334 !atomic_read(&tx->sdata->u.ap.num_mcast_sta))) {
336 * No associated STAs - no need to send multicast
345 /* This function is called whenever the AP is about to exceed the maximum limit
346 * of buffered frames for power saving STAs. This situation should not really
347 * happen often during normal operation, so dropping the oldest buffered packet
348 * from each queue should be OK to make some room for new frames. */
349 static void purge_old_ps_buffers(struct ieee80211_local *local)
351 int total = 0, purged = 0;
353 struct ieee80211_sub_if_data *sdata;
354 struct sta_info *sta;
356 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
359 if (sdata->vif.type == NL80211_IFTYPE_AP)
360 ps = &sdata->u.ap.ps;
361 else if (ieee80211_vif_is_mesh(&sdata->vif))
362 ps = &sdata->u.mesh.ps;
366 skb = skb_dequeue(&ps->bc_buf);
371 total += skb_queue_len(&ps->bc_buf);
375 * Drop one frame from each station from the lowest-priority
376 * AC that has frames at all.
378 list_for_each_entry_rcu(sta, &local->sta_list, list) {
381 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
382 skb = skb_dequeue(&sta->ps_tx_buf[ac]);
383 total += skb_queue_len(&sta->ps_tx_buf[ac]);
386 ieee80211_free_txskb(&local->hw, skb);
392 local->total_ps_buffered = total;
393 ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
396 static ieee80211_tx_result
397 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
399 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
400 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
404 * broadcast/multicast frame
406 * If any of the associated/peer stations is in power save mode,
407 * the frame is buffered to be sent after DTIM beacon frame.
408 * This is done either by the hardware or us.
411 /* powersaving STAs currently only in AP/VLAN/mesh mode */
412 if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
413 tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
417 ps = &tx->sdata->bss->ps;
418 } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
419 ps = &tx->sdata->u.mesh.ps;
425 /* no buffering for ordered frames */
426 if (ieee80211_has_order(hdr->frame_control))
429 if (ieee80211_is_probe_req(hdr->frame_control))
432 if (ieee80211_hw_check(&tx->local->hw, QUEUE_CONTROL))
433 info->hw_queue = tx->sdata->vif.cab_queue;
435 /* no stations in PS mode */
436 if (!atomic_read(&ps->num_sta_ps))
439 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
441 /* device releases frame after DTIM beacon */
442 if (!ieee80211_hw_check(&tx->local->hw, HOST_BROADCAST_PS_BUFFERING))
445 /* buffered in mac80211 */
446 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
447 purge_old_ps_buffers(tx->local);
449 if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
451 "BC TX buffer full - dropping the oldest frame\n");
452 dev_kfree_skb(skb_dequeue(&ps->bc_buf));
454 tx->local->total_ps_buffered++;
456 skb_queue_tail(&ps->bc_buf, tx->skb);
461 static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
464 if (!ieee80211_is_mgmt(fc))
467 if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
470 if (!ieee80211_is_robust_mgmt_frame(skb))
476 static ieee80211_tx_result
477 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
479 struct sta_info *sta = tx->sta;
480 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
481 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
482 struct ieee80211_local *local = tx->local;
487 if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
488 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
489 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
490 !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
491 int ac = skb_get_queue_mapping(tx->skb);
493 if (ieee80211_is_mgmt(hdr->frame_control) &&
494 !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
495 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
499 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
500 sta->sta.addr, sta->sta.aid, ac);
501 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
502 purge_old_ps_buffers(tx->local);
504 /* sync with ieee80211_sta_ps_deliver_wakeup */
505 spin_lock(&sta->ps_lock);
507 * STA woke up the meantime and all the frames on ps_tx_buf have
508 * been queued to pending queue. No reordering can happen, go
509 * ahead and Tx the packet.
511 if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
512 !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
513 !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
514 spin_unlock(&sta->ps_lock);
518 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
519 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
521 "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
523 ieee80211_free_txskb(&local->hw, old);
525 tx->local->total_ps_buffered++;
527 info->control.jiffies = jiffies;
528 info->control.vif = &tx->sdata->vif;
529 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
530 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
531 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
532 spin_unlock(&sta->ps_lock);
534 if (!timer_pending(&local->sta_cleanup))
535 mod_timer(&local->sta_cleanup,
536 round_jiffies(jiffies +
537 STA_INFO_CLEANUP_INTERVAL));
540 * We queued up some frames, so the TIM bit might
541 * need to be set, recalculate it.
543 sta_info_recalc_tim(sta);
546 } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
548 "STA %pM in PS mode, but polling/in SP -> send frame\n",
555 static ieee80211_tx_result debug_noinline
556 ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
558 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
561 if (tx->flags & IEEE80211_TX_UNICAST)
562 return ieee80211_tx_h_unicast_ps_buf(tx);
564 return ieee80211_tx_h_multicast_ps_buf(tx);
567 static ieee80211_tx_result debug_noinline
568 ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
570 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
572 if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
573 if (tx->sdata->control_port_no_encrypt)
574 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
575 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
576 info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
582 static ieee80211_tx_result debug_noinline
583 ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
585 struct ieee80211_key *key;
586 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
587 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
589 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
592 (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
594 else if (ieee80211_is_mgmt(hdr->frame_control) &&
595 is_multicast_ether_addr(hdr->addr1) &&
596 ieee80211_is_robust_mgmt_frame(tx->skb) &&
597 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
599 else if (is_multicast_ether_addr(hdr->addr1) &&
600 (key = rcu_dereference(tx->sdata->default_multicast_key)))
602 else if (!is_multicast_ether_addr(hdr->addr1) &&
603 (key = rcu_dereference(tx->sdata->default_unicast_key)))
609 bool skip_hw = false;
611 /* TODO: add threshold stuff again */
613 switch (tx->key->conf.cipher) {
614 case WLAN_CIPHER_SUITE_WEP40:
615 case WLAN_CIPHER_SUITE_WEP104:
616 case WLAN_CIPHER_SUITE_TKIP:
617 if (!ieee80211_is_data_present(hdr->frame_control))
620 case WLAN_CIPHER_SUITE_CCMP:
621 case WLAN_CIPHER_SUITE_CCMP_256:
622 case WLAN_CIPHER_SUITE_GCMP:
623 case WLAN_CIPHER_SUITE_GCMP_256:
624 if (!ieee80211_is_data_present(hdr->frame_control) &&
625 !ieee80211_use_mfp(hdr->frame_control, tx->sta,
629 skip_hw = (tx->key->conf.flags &
630 IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
631 ieee80211_is_mgmt(hdr->frame_control);
633 case WLAN_CIPHER_SUITE_AES_CMAC:
634 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
635 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
636 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
637 if (!ieee80211_is_mgmt(hdr->frame_control))
642 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
643 !ieee80211_is_deauth(hdr->frame_control)))
646 if (!skip_hw && tx->key &&
647 tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
648 info->control.hw_key = &tx->key->conf;
654 static ieee80211_tx_result debug_noinline
655 ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
657 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
658 struct ieee80211_hdr *hdr = (void *)tx->skb->data;
659 struct ieee80211_supported_band *sband;
661 struct ieee80211_tx_rate_control txrc;
662 struct ieee80211_sta_rates *ratetbl = NULL;
665 memset(&txrc, 0, sizeof(txrc));
667 sband = tx->local->hw.wiphy->bands[info->band];
669 len = min_t(u32, tx->skb->len + FCS_LEN,
670 tx->local->hw.wiphy->frag_threshold);
672 /* set up the tx rate control struct we give the RC algo */
673 txrc.hw = &tx->local->hw;
675 txrc.bss_conf = &tx->sdata->vif.bss_conf;
677 txrc.reported_rate.idx = -1;
678 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
679 if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
680 txrc.max_rate_idx = -1;
682 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
684 if (tx->sdata->rc_has_mcs_mask[info->band])
685 txrc.rate_idx_mcs_mask =
686 tx->sdata->rc_rateidx_mcs_mask[info->band];
688 txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
689 tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
690 tx->sdata->vif.type == NL80211_IFTYPE_ADHOC ||
691 tx->sdata->vif.type == NL80211_IFTYPE_OCB);
693 /* set up RTS protection if desired */
694 if (len > tx->local->hw.wiphy->rts_threshold) {
698 info->control.use_rts = txrc.rts;
699 info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
702 * Use short preamble if the BSS can handle it, but not for
703 * management frames unless we know the receiver can handle
704 * that -- the management frame might be to a station that
705 * just wants a probe response.
707 if (tx->sdata->vif.bss_conf.use_short_preamble &&
708 (ieee80211_is_data(hdr->frame_control) ||
709 (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
710 txrc.short_preamble = true;
712 info->control.short_preamble = txrc.short_preamble;
714 /* don't ask rate control when rate already injected via radiotap */
715 if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)
719 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
722 * Lets not bother rate control if we're associated and cannot
723 * talk to the sta. This should not happen.
725 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
726 !rate_usable_index_exists(sband, &tx->sta->sta),
727 "%s: Dropped data frame as no usable bitrate found while "
728 "scanning and associated. Target station: "
729 "%pM on %d GHz band\n",
730 tx->sdata->name, hdr->addr1,
735 * If we're associated with the sta at this point we know we can at
736 * least send the frame at the lowest bit rate.
738 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
740 if (tx->sta && !info->control.skip_table)
741 ratetbl = rcu_dereference(tx->sta->sta.rates);
743 if (unlikely(info->control.rates[0].idx < 0)) {
745 struct ieee80211_tx_rate rate = {
746 .idx = ratetbl->rate[0].idx,
747 .flags = ratetbl->rate[0].flags,
748 .count = ratetbl->rate[0].count
751 if (ratetbl->rate[0].idx < 0)
759 tx->rate = info->control.rates[0];
762 if (txrc.reported_rate.idx < 0) {
763 txrc.reported_rate = tx->rate;
764 if (tx->sta && ieee80211_is_data(hdr->frame_control))
765 tx->sta->tx_stats.last_rate = txrc.reported_rate;
767 tx->sta->tx_stats.last_rate = txrc.reported_rate;
772 if (unlikely(!info->control.rates[0].count))
773 info->control.rates[0].count = 1;
775 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
776 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
777 info->control.rates[0].count = 1;
782 static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
784 u16 *seq = &sta->tid_seq[tid];
785 __le16 ret = cpu_to_le16(*seq);
787 /* Increase the sequence number. */
788 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
793 static ieee80211_tx_result debug_noinline
794 ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
796 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
797 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
802 * Packet injection may want to control the sequence
803 * number, if we have no matching interface then we
804 * neither assign one ourselves nor ask the driver to.
806 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
809 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
812 if (ieee80211_hdrlen(hdr->frame_control) < 24)
815 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
819 * Anything but QoS data that has a sequence number field
820 * (is long enough) gets a sequence number from the global
821 * counter. QoS data frames with a multicast destination
822 * also use the global counter (802.11-2012 9.3.2.10).
824 if (!ieee80211_is_data_qos(hdr->frame_control) ||
825 is_multicast_ether_addr(hdr->addr1)) {
826 /* driver should assign sequence number */
827 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
828 /* for pure STA mode without beacons, we can do it */
829 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
830 tx->sdata->sequence_number += 0x10;
832 tx->sta->tx_stats.msdu[IEEE80211_NUM_TIDS]++;
837 * This should be true for injected/management frames only, for
838 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
839 * above since they are not QoS-data frames.
844 /* include per-STA, per-TID sequence counter */
846 qc = ieee80211_get_qos_ctl(hdr);
847 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
848 tx->sta->tx_stats.msdu[tid]++;
850 if (!tx->sta->sta.txq[0])
851 hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
856 static int ieee80211_fragment(struct ieee80211_tx_data *tx,
857 struct sk_buff *skb, int hdrlen,
860 struct ieee80211_local *local = tx->local;
861 struct ieee80211_tx_info *info;
863 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
864 int pos = hdrlen + per_fragm;
865 int rem = skb->len - hdrlen - per_fragm;
867 if (WARN_ON(rem < 0))
870 /* first fragment was already added to queue by caller */
873 int fraglen = per_fragm;
878 tmp = dev_alloc_skb(local->tx_headroom +
880 tx->sdata->encrypt_headroom +
881 IEEE80211_ENCRYPT_TAILROOM);
885 __skb_queue_tail(&tx->skbs, tmp);
888 local->tx_headroom + tx->sdata->encrypt_headroom);
890 /* copy control information */
891 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
893 info = IEEE80211_SKB_CB(tmp);
894 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
895 IEEE80211_TX_CTL_FIRST_FRAGMENT);
898 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
900 skb_copy_queue_mapping(tmp, skb);
901 tmp->priority = skb->priority;
904 /* copy header and data */
905 memcpy(skb_put(tmp, hdrlen), skb->data, hdrlen);
906 memcpy(skb_put(tmp, fraglen), skb->data + pos, fraglen);
911 /* adjust first fragment's length */
912 skb_trim(skb, hdrlen + per_fragm);
916 static ieee80211_tx_result debug_noinline
917 ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
919 struct sk_buff *skb = tx->skb;
920 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
921 struct ieee80211_hdr *hdr = (void *)skb->data;
922 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
926 /* no matter what happens, tx->skb moves to tx->skbs */
927 __skb_queue_tail(&tx->skbs, skb);
930 if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
933 if (tx->local->ops->set_frag_threshold)
937 * Warn when submitting a fragmented A-MPDU frame and drop it.
938 * This scenario is handled in ieee80211_tx_prepare but extra
939 * caution taken here as fragmented ampdu may cause Tx stop.
941 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
944 hdrlen = ieee80211_hdrlen(hdr->frame_control);
946 /* internal error, why isn't DONTFRAG set? */
947 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
951 * Now fragment the frame. This will allocate all the fragments and
952 * chain them (using skb as the first fragment) to skb->next.
953 * During transmission, we will remove the successfully transmitted
954 * fragments from this list. When the low-level driver rejects one
955 * of the fragments then we will simply pretend to accept the skb
956 * but store it away as pending.
958 if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
961 /* update duration/seq/flags of fragments */
964 skb_queue_walk(&tx->skbs, skb) {
965 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
967 hdr = (void *)skb->data;
968 info = IEEE80211_SKB_CB(skb);
970 if (!skb_queue_is_last(&tx->skbs, skb)) {
971 hdr->frame_control |= morefrags;
973 * No multi-rate retries for fragmented frames, that
974 * would completely throw off the NAV at other STAs.
976 info->control.rates[1].idx = -1;
977 info->control.rates[2].idx = -1;
978 info->control.rates[3].idx = -1;
979 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
980 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
982 hdr->frame_control &= ~morefrags;
984 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
991 static ieee80211_tx_result debug_noinline
992 ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
1000 skb_queue_walk(&tx->skbs, skb) {
1001 ac = skb_get_queue_mapping(skb);
1002 tx->sta->tx_stats.bytes[ac] += skb->len;
1005 tx->sta->tx_stats.packets[ac]++;
1010 static ieee80211_tx_result debug_noinline
1011 ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
1016 switch (tx->key->conf.cipher) {
1017 case WLAN_CIPHER_SUITE_WEP40:
1018 case WLAN_CIPHER_SUITE_WEP104:
1019 return ieee80211_crypto_wep_encrypt(tx);
1020 case WLAN_CIPHER_SUITE_TKIP:
1021 return ieee80211_crypto_tkip_encrypt(tx);
1022 case WLAN_CIPHER_SUITE_CCMP:
1023 return ieee80211_crypto_ccmp_encrypt(
1024 tx, IEEE80211_CCMP_MIC_LEN);
1025 case WLAN_CIPHER_SUITE_CCMP_256:
1026 return ieee80211_crypto_ccmp_encrypt(
1027 tx, IEEE80211_CCMP_256_MIC_LEN);
1028 case WLAN_CIPHER_SUITE_AES_CMAC:
1029 return ieee80211_crypto_aes_cmac_encrypt(tx);
1030 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1031 return ieee80211_crypto_aes_cmac_256_encrypt(tx);
1032 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1033 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1034 return ieee80211_crypto_aes_gmac_encrypt(tx);
1035 case WLAN_CIPHER_SUITE_GCMP:
1036 case WLAN_CIPHER_SUITE_GCMP_256:
1037 return ieee80211_crypto_gcmp_encrypt(tx);
1039 return ieee80211_crypto_hw_encrypt(tx);
1045 static ieee80211_tx_result debug_noinline
1046 ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1048 struct sk_buff *skb;
1049 struct ieee80211_hdr *hdr;
1053 skb_queue_walk(&tx->skbs, skb) {
1054 hdr = (void *) skb->data;
1055 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1056 break; /* must not overwrite AID */
1057 if (!skb_queue_is_last(&tx->skbs, skb)) {
1058 struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1059 next_len = next->len;
1062 group_addr = is_multicast_ether_addr(hdr->addr1);
1065 ieee80211_duration(tx, skb, group_addr, next_len);
1071 /* actual transmit path */
1073 static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1074 struct sk_buff *skb,
1075 struct ieee80211_tx_info *info,
1076 struct tid_ampdu_tx *tid_tx,
1079 bool queued = false;
1080 bool reset_agg_timer = false;
1081 struct sk_buff *purge_skb = NULL;
1083 if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1084 info->flags |= IEEE80211_TX_CTL_AMPDU;
1085 reset_agg_timer = true;
1086 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1088 * nothing -- this aggregation session is being started
1089 * but that might still fail with the driver
1091 } else if (!tx->sta->sta.txq[tid]) {
1092 spin_lock(&tx->sta->lock);
1094 * Need to re-check now, because we may get here
1096 * 1) in the window during which the setup is actually
1097 * already done, but not marked yet because not all
1098 * packets are spliced over to the driver pending
1099 * queue yet -- if this happened we acquire the lock
1100 * either before or after the splice happens, but
1101 * need to recheck which of these cases happened.
1103 * 2) during session teardown, if the OPERATIONAL bit
1104 * was cleared due to the teardown but the pointer
1105 * hasn't been assigned NULL yet (or we loaded it
1106 * before it was assigned) -- in this case it may
1107 * now be NULL which means we should just let the
1108 * packet pass through because splicing the frames
1109 * back is already done.
1111 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
1114 /* do nothing, let packet pass through */
1115 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1116 info->flags |= IEEE80211_TX_CTL_AMPDU;
1117 reset_agg_timer = true;
1120 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
1121 clear_sta_flag(tx->sta, WLAN_STA_SP);
1122 ps_dbg(tx->sta->sdata,
1123 "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
1124 tx->sta->sta.addr, tx->sta->sta.aid);
1126 info->control.vif = &tx->sdata->vif;
1127 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1128 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
1129 __skb_queue_tail(&tid_tx->pending, skb);
1130 if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1131 purge_skb = __skb_dequeue(&tid_tx->pending);
1133 spin_unlock(&tx->sta->lock);
1136 ieee80211_free_txskb(&tx->local->hw, purge_skb);
1139 /* reset session timer */
1140 if (reset_agg_timer && tid_tx->timeout)
1141 tid_tx->last_tx = jiffies;
1148 * pass %NULL for the station if unknown, a valid pointer if known
1149 * or an ERR_PTR() if the station is known not to exist
1151 static ieee80211_tx_result
1152 ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1153 struct ieee80211_tx_data *tx,
1154 struct sta_info *sta, struct sk_buff *skb)
1156 struct ieee80211_local *local = sdata->local;
1157 struct ieee80211_hdr *hdr;
1158 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1162 memset(tx, 0, sizeof(*tx));
1166 __skb_queue_head_init(&tx->skbs);
1169 * If this flag is set to true anywhere, and we get here,
1170 * we are doing the needed processing, so remove the flag
1173 info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1175 hdr = (struct ieee80211_hdr *) skb->data;
1181 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1182 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1183 if (!tx->sta && sdata->wdev.use_4addr)
1185 } else if (info->flags & (IEEE80211_TX_INTFL_NL80211_FRAME_TX |
1186 IEEE80211_TX_CTL_INJECTED) ||
1187 tx->sdata->control_port_protocol == tx->skb->protocol) {
1188 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1190 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
1191 tx->sta = sta_info_get(sdata, hdr->addr1);
1194 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
1195 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
1196 ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
1197 !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
1198 struct tid_ampdu_tx *tid_tx;
1200 qc = ieee80211_get_qos_ctl(hdr);
1201 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
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 ieee80211_sta *pubsta,
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 (!ieee80211_is_data(hdr->frame_control))
1257 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1259 txq = pubsta->txq[tid];
1267 return to_txq_info(txq);
1270 static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
1272 struct fq_flow *flow)
1274 return fq_flow_dequeue(fq, flow);
1277 static void fq_skb_free_func(struct fq *fq,
1279 struct fq_flow *flow,
1280 struct sk_buff *skb)
1282 struct ieee80211_local *local;
1284 local = container_of(fq, struct ieee80211_local, fq);
1285 ieee80211_free_txskb(&local->hw, skb);
1288 static struct fq_flow *fq_flow_get_default_func(struct fq *fq,
1291 struct sk_buff *skb)
1293 struct txq_info *txqi;
1295 txqi = container_of(tin, struct txq_info, tin);
1296 return &txqi->def_flow;
1299 static void ieee80211_txq_enqueue(struct ieee80211_local *local,
1300 struct txq_info *txqi,
1301 struct sk_buff *skb)
1303 struct fq *fq = &local->fq;
1304 struct fq_tin *tin = &txqi->tin;
1306 fq_tin_enqueue(fq, tin, skb,
1308 fq_flow_get_default_func);
1311 void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
1312 struct sta_info *sta,
1313 struct txq_info *txqi, int tid)
1315 fq_tin_init(&txqi->tin);
1316 fq_flow_init(&txqi->def_flow);
1318 txqi->txq.vif = &sdata->vif;
1321 txqi->txq.sta = &sta->sta;
1322 sta->sta.txq[tid] = &txqi->txq;
1323 txqi->txq.tid = tid;
1324 txqi->txq.ac = ieee802_1d_to_ac[tid & 7];
1326 sdata->vif.txq = &txqi->txq;
1328 txqi->txq.ac = IEEE80211_AC_BE;
1332 void ieee80211_txq_purge(struct ieee80211_local *local,
1333 struct txq_info *txqi)
1335 struct fq *fq = &local->fq;
1336 struct fq_tin *tin = &txqi->tin;
1338 fq_tin_reset(fq, tin, fq_skb_free_func);
1341 int ieee80211_txq_setup_flows(struct ieee80211_local *local)
1343 struct fq *fq = &local->fq;
1346 if (!local->ops->wake_tx_queue)
1349 ret = fq_init(fq, 4096);
1356 void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1358 struct fq *fq = &local->fq;
1360 if (!local->ops->wake_tx_queue)
1363 fq_reset(fq, fq_skb_free_func);
1366 struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
1367 struct ieee80211_txq *txq)
1369 struct ieee80211_local *local = hw_to_local(hw);
1370 struct txq_info *txqi = container_of(txq, struct txq_info, txq);
1371 struct ieee80211_hdr *hdr;
1372 struct sk_buff *skb = NULL;
1373 struct fq *fq = &local->fq;
1374 struct fq_tin *tin = &txqi->tin;
1376 spin_lock_bh(&fq->lock);
1378 if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags))
1381 skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
1385 hdr = (struct ieee80211_hdr *)skb->data;
1386 if (txq->sta && ieee80211_is_data_qos(hdr->frame_control)) {
1387 struct sta_info *sta = container_of(txq->sta, struct sta_info,
1389 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1391 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, txq->tid);
1392 if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
1393 info->flags |= IEEE80211_TX_CTL_AMPDU;
1395 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
1399 spin_unlock_bh(&fq->lock);
1401 if (skb && skb_has_frag_list(skb) &&
1402 !ieee80211_hw_check(&local->hw, TX_FRAG_LIST))
1407 EXPORT_SYMBOL(ieee80211_tx_dequeue);
1409 static bool ieee80211_tx_frags(struct ieee80211_local *local,
1410 struct ieee80211_vif *vif,
1411 struct ieee80211_sta *sta,
1412 struct sk_buff_head *skbs,
1415 struct ieee80211_tx_control control = {};
1416 struct fq *fq = &local->fq;
1417 struct sk_buff *skb, *tmp;
1418 struct txq_info *txqi;
1419 unsigned long flags;
1421 skb_queue_walk_safe(skbs, skb, tmp) {
1422 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1423 int q = info->hw_queue;
1425 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1426 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1427 __skb_unlink(skb, skbs);
1428 ieee80211_free_txskb(&local->hw, skb);
1433 txqi = ieee80211_get_txq(local, vif, sta, skb);
1435 info->control.vif = vif;
1437 __skb_unlink(skb, skbs);
1439 spin_lock_bh(&fq->lock);
1440 ieee80211_txq_enqueue(local, txqi, skb);
1441 spin_unlock_bh(&fq->lock);
1443 drv_wake_tx_queue(local, txqi);
1448 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1449 if (local->queue_stop_reasons[q] ||
1450 (!txpending && !skb_queue_empty(&local->pending[q]))) {
1451 if (unlikely(info->flags &
1452 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1453 if (local->queue_stop_reasons[q] &
1454 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1456 * Drop off-channel frames if queues
1457 * are stopped for any reason other
1458 * than off-channel operation. Never
1461 spin_unlock_irqrestore(
1462 &local->queue_stop_reason_lock,
1464 ieee80211_purge_tx_queue(&local->hw,
1471 * Since queue is stopped, queue up frames for
1472 * later transmission from the tx-pending
1473 * tasklet when the queue is woken again.
1476 skb_queue_splice_init(skbs,
1477 &local->pending[q]);
1479 skb_queue_splice_tail_init(skbs,
1480 &local->pending[q]);
1482 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1487 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1489 info->control.vif = vif;
1492 __skb_unlink(skb, skbs);
1493 drv_tx(local, &control, skb);
1500 * Returns false if the frame couldn't be transmitted but was queued instead.
1502 static bool __ieee80211_tx(struct ieee80211_local *local,
1503 struct sk_buff_head *skbs, int led_len,
1504 struct sta_info *sta, bool txpending)
1506 struct ieee80211_tx_info *info;
1507 struct ieee80211_sub_if_data *sdata;
1508 struct ieee80211_vif *vif;
1509 struct ieee80211_sta *pubsta;
1510 struct sk_buff *skb;
1514 if (WARN_ON(skb_queue_empty(skbs)))
1517 skb = skb_peek(skbs);
1518 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1519 info = IEEE80211_SKB_CB(skb);
1520 sdata = vif_to_sdata(info->control.vif);
1521 if (sta && !sta->uploaded)
1529 switch (sdata->vif.type) {
1530 case NL80211_IFTYPE_MONITOR:
1531 if (sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE) {
1535 sdata = rcu_dereference(local->monitor_sdata);
1539 vif->hw_queue[skb_get_queue_mapping(skb)];
1540 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
1541 ieee80211_purge_tx_queue(&local->hw, skbs);
1546 case NL80211_IFTYPE_AP_VLAN:
1547 sdata = container_of(sdata->bss,
1548 struct ieee80211_sub_if_data, u.ap);
1555 result = ieee80211_tx_frags(local, vif, pubsta, skbs,
1558 ieee80211_tpt_led_trig_tx(local, fc, led_len);
1560 WARN_ON_ONCE(!skb_queue_empty(skbs));
1566 * Invoke TX handlers, return 0 on success and non-zero if the
1567 * frame was dropped or queued.
1569 static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1571 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1572 ieee80211_tx_result res = TX_DROP;
1574 #define CALL_TXH(txh) \
1577 if (res != TX_CONTINUE) \
1581 CALL_TXH(ieee80211_tx_h_dynamic_ps);
1582 CALL_TXH(ieee80211_tx_h_check_assoc);
1583 CALL_TXH(ieee80211_tx_h_ps_buf);
1584 CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
1585 CALL_TXH(ieee80211_tx_h_select_key);
1586 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1587 CALL_TXH(ieee80211_tx_h_rate_ctrl);
1589 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1590 __skb_queue_tail(&tx->skbs, tx->skb);
1595 CALL_TXH(ieee80211_tx_h_michael_mic_add);
1596 CALL_TXH(ieee80211_tx_h_sequence);
1597 CALL_TXH(ieee80211_tx_h_fragment);
1598 /* handlers after fragment must be aware of tx info fragmentation! */
1599 CALL_TXH(ieee80211_tx_h_stats);
1600 CALL_TXH(ieee80211_tx_h_encrypt);
1601 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1602 CALL_TXH(ieee80211_tx_h_calculate_duration);
1606 if (unlikely(res == TX_DROP)) {
1607 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1609 ieee80211_free_txskb(&tx->local->hw, tx->skb);
1611 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1613 } else if (unlikely(res == TX_QUEUED)) {
1614 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1621 bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1622 struct ieee80211_vif *vif, struct sk_buff *skb,
1623 int band, struct ieee80211_sta **sta)
1625 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1626 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1627 struct ieee80211_tx_data tx;
1628 struct sk_buff *skb2;
1630 if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
1634 info->control.vif = vif;
1635 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1637 if (invoke_tx_handlers(&tx))
1642 *sta = &tx.sta->sta;
1647 /* this function isn't suitable for fragmented data frames */
1648 skb2 = __skb_dequeue(&tx.skbs);
1649 if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1650 ieee80211_free_txskb(hw, skb2);
1651 ieee80211_purge_tx_queue(hw, &tx.skbs);
1657 EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1660 * Returns false if the frame couldn't be transmitted but was queued instead.
1662 static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
1663 struct sta_info *sta, struct sk_buff *skb,
1666 struct ieee80211_local *local = sdata->local;
1667 struct ieee80211_tx_data tx;
1668 ieee80211_tx_result res_prepare;
1669 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1673 if (unlikely(skb->len < 10)) {
1678 /* initialises tx */
1680 res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
1682 if (unlikely(res_prepare == TX_DROP)) {
1683 ieee80211_free_txskb(&local->hw, skb);
1685 } else if (unlikely(res_prepare == TX_QUEUED)) {
1689 /* set up hw_queue value early */
1690 if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
1691 !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
1693 sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1695 if (!invoke_tx_handlers(&tx))
1696 result = __ieee80211_tx(local, &tx.skbs, led_len,
1702 /* device xmit handlers */
1704 static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
1705 struct sk_buff *skb,
1706 int head_need, bool may_encrypt)
1708 struct ieee80211_local *local = sdata->local;
1711 if (may_encrypt && sdata->crypto_tx_tailroom_needed_cnt) {
1712 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1713 tail_need -= skb_tailroom(skb);
1714 tail_need = max_t(int, tail_need, 0);
1717 if (skb_cloned(skb) &&
1718 (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
1719 !skb_clone_writable(skb, ETH_HLEN) ||
1720 (may_encrypt && sdata->crypto_tx_tailroom_needed_cnt)))
1721 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1722 else if (head_need || tail_need)
1723 I802_DEBUG_INC(local->tx_expand_skb_head);
1727 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1728 wiphy_debug(local->hw.wiphy,
1729 "failed to reallocate TX buffer\n");
1736 void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
1737 struct sta_info *sta, struct sk_buff *skb)
1739 struct ieee80211_local *local = sdata->local;
1740 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1741 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1745 may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
1747 headroom = local->tx_headroom;
1749 headroom += sdata->encrypt_headroom;
1750 headroom -= skb_headroom(skb);
1751 headroom = max_t(int, 0, headroom);
1753 if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
1754 ieee80211_free_txskb(&local->hw, skb);
1758 hdr = (struct ieee80211_hdr *) skb->data;
1759 info->control.vif = &sdata->vif;
1761 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1762 if (ieee80211_is_data(hdr->frame_control) &&
1763 is_unicast_ether_addr(hdr->addr1)) {
1764 if (mesh_nexthop_resolve(sdata, skb))
1765 return; /* skb queued: don't free */
1767 ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
1771 ieee80211_set_qos_hdr(sdata, skb);
1772 ieee80211_tx(sdata, sta, skb, false);
1775 static bool ieee80211_parse_tx_radiotap(struct ieee80211_local *local,
1776 struct sk_buff *skb)
1778 struct ieee80211_radiotap_iterator iterator;
1779 struct ieee80211_radiotap_header *rthdr =
1780 (struct ieee80211_radiotap_header *) skb->data;
1781 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1782 struct ieee80211_supported_band *sband =
1783 local->hw.wiphy->bands[info->band];
1784 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
1788 bool rate_found = false;
1789 u8 rate_retries = 0;
1791 u8 mcs_known, mcs_flags, mcs_bw;
1793 u8 vht_mcs = 0, vht_nss = 0;
1796 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1797 IEEE80211_TX_CTL_DONTFRAG;
1800 * for every radiotap entry that is present
1801 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
1802 * entries present, or -EINVAL on error)
1806 ret = ieee80211_radiotap_iterator_next(&iterator);
1811 /* see if this argument is something we can use */
1812 switch (iterator.this_arg_index) {
1814 * You must take care when dereferencing iterator.this_arg
1815 * for multibyte types... the pointer is not aligned. Use
1816 * get_unaligned((type *)iterator.this_arg) to dereference
1817 * iterator.this_arg for type "type" safely on all arches.
1819 case IEEE80211_RADIOTAP_FLAGS:
1820 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
1822 * this indicates that the skb we have been
1823 * handed has the 32-bit FCS CRC at the end...
1824 * we should react to that by snipping it off
1825 * because it will be recomputed and added
1828 if (skb->len < (iterator._max_length + FCS_LEN))
1831 skb_trim(skb, skb->len - FCS_LEN);
1833 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
1834 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
1835 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
1836 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
1839 case IEEE80211_RADIOTAP_TX_FLAGS:
1840 txflags = get_unaligned_le16(iterator.this_arg);
1841 if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
1842 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1845 case IEEE80211_RADIOTAP_RATE:
1846 rate = *iterator.this_arg;
1851 case IEEE80211_RADIOTAP_DATA_RETRIES:
1852 rate_retries = *iterator.this_arg;
1855 case IEEE80211_RADIOTAP_MCS:
1856 mcs_known = iterator.this_arg[0];
1857 mcs_flags = iterator.this_arg[1];
1858 if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
1862 rate = iterator.this_arg[2];
1863 rate_flags = IEEE80211_TX_RC_MCS;
1865 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
1866 mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
1867 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
1869 mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
1870 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
1871 mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
1872 rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
1875 case IEEE80211_RADIOTAP_VHT:
1876 vht_known = get_unaligned_le16(iterator.this_arg);
1879 rate_flags = IEEE80211_TX_RC_VHT_MCS;
1880 if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
1881 (iterator.this_arg[2] &
1882 IEEE80211_RADIOTAP_VHT_FLAG_SGI))
1883 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
1885 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
1886 if (iterator.this_arg[3] == 1)
1888 IEEE80211_TX_RC_40_MHZ_WIDTH;
1889 else if (iterator.this_arg[3] == 4)
1891 IEEE80211_TX_RC_80_MHZ_WIDTH;
1892 else if (iterator.this_arg[3] == 11)
1894 IEEE80211_TX_RC_160_MHZ_WIDTH;
1897 vht_mcs = iterator.this_arg[4] >> 4;
1898 vht_nss = iterator.this_arg[4] & 0xF;
1902 * Please update the file
1903 * Documentation/networking/mac80211-injection.txt
1904 * when parsing new fields here.
1912 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
1916 info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
1918 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1919 info->control.rates[i].idx = -1;
1920 info->control.rates[i].flags = 0;
1921 info->control.rates[i].count = 0;
1924 if (rate_flags & IEEE80211_TX_RC_MCS) {
1925 info->control.rates[0].idx = rate;
1926 } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
1927 ieee80211_rate_set_vht(info->control.rates, vht_mcs,
1930 for (i = 0; i < sband->n_bitrates; i++) {
1931 if (rate * 5 != sband->bitrates[i].bitrate)
1934 info->control.rates[0].idx = i;
1939 if (info->control.rates[0].idx < 0)
1940 info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
1942 info->control.rates[0].flags = rate_flags;
1943 info->control.rates[0].count = min_t(u8, rate_retries + 1,
1944 local->hw.max_rate_tries);
1948 * remove the radiotap header
1949 * iterator->_max_length was sanity-checked against
1950 * skb->len by iterator init
1952 skb_pull(skb, iterator._max_length);
1957 netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
1958 struct net_device *dev)
1960 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1961 struct ieee80211_chanctx_conf *chanctx_conf;
1962 struct ieee80211_radiotap_header *prthdr =
1963 (struct ieee80211_radiotap_header *)skb->data;
1964 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1965 struct ieee80211_hdr *hdr;
1966 struct ieee80211_sub_if_data *tmp_sdata, *sdata;
1967 struct cfg80211_chan_def *chandef;
1971 /* check for not even having the fixed radiotap header part */
1972 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1973 goto fail; /* too short to be possibly valid */
1975 /* is it a header version we can trust to find length from? */
1976 if (unlikely(prthdr->it_version))
1977 goto fail; /* only version 0 is supported */
1979 /* then there must be a radiotap header with a length we can use */
1980 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1982 /* does the skb contain enough to deliver on the alleged length? */
1983 if (unlikely(skb->len < len_rthdr))
1984 goto fail; /* skb too short for claimed rt header extent */
1987 * fix up the pointers accounting for the radiotap
1988 * header still being in there. We are being given
1989 * a precooked IEEE80211 header so no need for
1992 skb_set_mac_header(skb, len_rthdr);
1994 * these are just fixed to the end of the rt area since we
1995 * don't have any better information and at this point, nobody cares
1997 skb_set_network_header(skb, len_rthdr);
1998 skb_set_transport_header(skb, len_rthdr);
2000 if (skb->len < len_rthdr + 2)
2003 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2004 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2006 if (skb->len < len_rthdr + hdrlen)
2010 * Initialize skb->protocol if the injected frame is a data frame
2011 * carrying a rfc1042 header
2013 if (ieee80211_is_data(hdr->frame_control) &&
2014 skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2015 u8 *payload = (u8 *)hdr + hdrlen;
2017 if (ether_addr_equal(payload, rfc1042_header))
2018 skb->protocol = cpu_to_be16((payload[6] << 8) |
2022 memset(info, 0, sizeof(*info));
2024 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2025 IEEE80211_TX_CTL_INJECTED;
2030 * We process outgoing injected frames that have a local address
2031 * we handle as though they are non-injected frames.
2032 * This code here isn't entirely correct, the local MAC address
2033 * isn't always enough to find the interface to use; for proper
2034 * VLAN/WDS support we will need a different mechanism (which
2035 * likely isn't going to be monitor interfaces).
2037 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2039 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2040 if (!ieee80211_sdata_running(tmp_sdata))
2042 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2043 tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2044 tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
2046 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
2052 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2053 if (!chanctx_conf) {
2054 tmp_sdata = rcu_dereference(local->monitor_sdata);
2057 rcu_dereference(tmp_sdata->vif.chanctx_conf);
2061 chandef = &chanctx_conf->def;
2062 else if (!local->use_chanctx)
2063 chandef = &local->_oper_chandef;
2068 * Frame injection is not allowed if beaconing is not allowed
2069 * or if we need radar detection. Beaconing is usually not allowed when
2070 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2071 * Passive scan is also used in world regulatory domains where
2072 * your country is not known and as such it should be treated as
2073 * NO TX unless the channel is explicitly allowed in which case
2074 * your current regulatory domain would not have the passive scan
2077 * Since AP mode uses monitor interfaces to inject/TX management
2078 * frames we can make AP mode the exception to this rule once it
2079 * supports radar detection as its implementation can deal with
2080 * radar detection by itself. We can do that later by adding a
2081 * monitor flag interfaces used for AP support.
2083 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2087 info->band = chandef->chan->band;
2089 /* process and remove the injection radiotap header */
2090 if (!ieee80211_parse_tx_radiotap(local, skb))
2093 ieee80211_xmit(sdata, NULL, skb);
2096 return NETDEV_TX_OK;
2102 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
2105 static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
2107 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
2109 return ethertype == ETH_P_TDLS &&
2111 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2114 static int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2115 struct sk_buff *skb,
2116 struct sta_info **sta_out)
2118 struct sta_info *sta;
2120 switch (sdata->vif.type) {
2121 case NL80211_IFTYPE_AP_VLAN:
2122 sta = rcu_dereference(sdata->u.vlan.sta);
2126 } else if (sdata->wdev.use_4addr) {
2130 case NL80211_IFTYPE_AP:
2131 case NL80211_IFTYPE_OCB:
2132 case NL80211_IFTYPE_ADHOC:
2133 if (is_multicast_ether_addr(skb->data)) {
2134 *sta_out = ERR_PTR(-ENOENT);
2137 sta = sta_info_get_bss(sdata, skb->data);
2139 case NL80211_IFTYPE_WDS:
2140 sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
2142 #ifdef CONFIG_MAC80211_MESH
2143 case NL80211_IFTYPE_MESH_POINT:
2144 /* determined much later */
2148 case NL80211_IFTYPE_STATION:
2149 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2150 sta = sta_info_get(sdata, skb->data);
2152 bool tdls_peer, tdls_auth;
2154 tdls_peer = test_sta_flag(sta,
2155 WLAN_STA_TDLS_PEER);
2156 tdls_auth = test_sta_flag(sta,
2157 WLAN_STA_TDLS_PEER_AUTH);
2159 if (tdls_peer && tdls_auth) {
2165 * TDLS link during setup - throw out frames to
2166 * peer. Allow TDLS-setup frames to unauthorized
2167 * peers for the special case of a link teardown
2168 * after a TDLS sta is removed due to being
2171 if (tdls_peer && !tdls_auth &&
2172 !ieee80211_is_tdls_setup(skb))
2178 sta = sta_info_get(sdata, sdata->u.mgd.bssid);
2186 *sta_out = sta ?: ERR_PTR(-ENOENT);
2191 * ieee80211_build_hdr - build 802.11 header in the given frame
2192 * @sdata: virtual interface to build the header for
2193 * @skb: the skb to build the header in
2194 * @info_flags: skb flags to set
2196 * This function takes the skb with 802.3 header and reformats the header to
2197 * the appropriate IEEE 802.11 header based on which interface the packet is
2198 * being transmitted on.
2200 * Note that this function also takes care of the TX status request and
2201 * potential unsharing of the SKB - this needs to be interleaved with the
2204 * The function requires the read-side RCU lock held
2206 * Returns: the (possibly reallocated) skb or an ERR_PTR() code
2208 static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
2209 struct sk_buff *skb, u32 info_flags,
2210 struct sta_info *sta)
2212 struct ieee80211_local *local = sdata->local;
2213 struct ieee80211_tx_info *info;
2215 u16 ethertype, hdrlen, meshhdrlen = 0;
2217 struct ieee80211_hdr hdr;
2218 struct ieee80211s_hdr mesh_hdr __maybe_unused;
2219 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
2220 const u8 *encaps_data;
2221 int encaps_len, skip_header_bytes;
2223 bool wme_sta = false, authorized = false;
2227 struct ieee80211_chanctx_conf *chanctx_conf;
2228 struct ieee80211_sub_if_data *ap_sdata;
2229 enum nl80211_band band;
2235 /* convert Ethernet header to proper 802.11 header (based on
2236 * operation mode) */
2237 ethertype = (skb->data[12] << 8) | skb->data[13];
2238 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2240 switch (sdata->vif.type) {
2241 case NL80211_IFTYPE_AP_VLAN:
2242 if (sdata->wdev.use_4addr) {
2243 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2245 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
2246 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2247 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2248 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2250 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2251 wme_sta = sta->sta.wme;
2253 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2255 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
2256 if (!chanctx_conf) {
2260 band = chanctx_conf->def.chan->band;
2261 if (sdata->wdev.use_4addr)
2264 case NL80211_IFTYPE_AP:
2265 if (sdata->vif.type == NL80211_IFTYPE_AP)
2266 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2267 if (!chanctx_conf) {
2271 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2273 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2274 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2275 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2277 band = chanctx_conf->def.chan->band;
2279 case NL80211_IFTYPE_WDS:
2280 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2282 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
2283 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2284 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2285 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2288 * This is the exception! WDS style interfaces are prohibited
2289 * when channel contexts are in used so this must be valid
2291 band = local->hw.conf.chandef.chan->band;
2293 #ifdef CONFIG_MAC80211_MESH
2294 case NL80211_IFTYPE_MESH_POINT:
2295 if (!is_multicast_ether_addr(skb->data)) {
2296 struct sta_info *next_hop;
2297 bool mpp_lookup = true;
2299 mpath = mesh_path_lookup(sdata, skb->data);
2302 next_hop = rcu_dereference(mpath->next_hop);
2304 !(mpath->flags & (MESH_PATH_ACTIVE |
2305 MESH_PATH_RESOLVING)))
2310 mppath = mpp_path_lookup(sdata, skb->data);
2312 mppath->exp_time = jiffies;
2315 if (mppath && mpath)
2316 mesh_path_del(sdata, mpath->dst);
2320 * Use address extension if it is a packet from
2321 * another interface or if we know the destination
2322 * is being proxied by a portal (i.e. portal address
2323 * differs from proxied address)
2325 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2326 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
2327 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2328 skb->data, skb->data + ETH_ALEN);
2329 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2332 /* DS -> MBSS (802.11-2012 13.11.3.3).
2333 * For unicast with unknown forwarding information,
2334 * destination might be in the MBSS or if that fails
2335 * forwarded to another mesh gate. In either case
2336 * resolution will be handled in ieee80211_xmit(), so
2337 * leave the original DA. This also works for mcast */
2338 const u8 *mesh_da = skb->data;
2341 mesh_da = mppath->mpp;
2343 mesh_da = mpath->dst;
2345 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2346 mesh_da, sdata->vif.addr);
2347 if (is_multicast_ether_addr(mesh_da))
2348 /* DA TA mSA AE:SA */
2349 meshhdrlen = ieee80211_new_mesh_header(
2351 skb->data + ETH_ALEN, NULL);
2353 /* RA TA mDA mSA AE:DA SA */
2354 meshhdrlen = ieee80211_new_mesh_header(
2355 sdata, &mesh_hdr, skb->data,
2356 skb->data + ETH_ALEN);
2359 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2360 if (!chanctx_conf) {
2364 band = chanctx_conf->def.chan->band;
2367 case NL80211_IFTYPE_STATION:
2368 /* we already did checks when looking up the RA STA */
2369 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
2373 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2374 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2375 memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2377 } else if (sdata->u.mgd.use_4addr &&
2378 cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2379 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2380 IEEE80211_FCTL_TODS);
2382 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2383 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2384 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2385 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2388 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2390 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2391 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2392 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2395 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2396 if (!chanctx_conf) {
2400 band = chanctx_conf->def.chan->band;
2402 case NL80211_IFTYPE_OCB:
2404 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2405 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2406 eth_broadcast_addr(hdr.addr3);
2408 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2409 if (!chanctx_conf) {
2413 band = chanctx_conf->def.chan->band;
2415 case NL80211_IFTYPE_ADHOC:
2417 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2418 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2419 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
2421 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2422 if (!chanctx_conf) {
2426 band = chanctx_conf->def.chan->band;
2433 multicast = is_multicast_ether_addr(hdr.addr1);
2435 /* sta is always NULL for mesh */
2437 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2438 wme_sta = sta->sta.wme;
2439 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2440 /* For mesh, the use of the QoS header is mandatory */
2444 /* receiver does QoS (which also means we do) use it */
2446 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2451 * Drop unicast frames to unauthorised stations unless they are
2452 * EAPOL frames from the local station.
2454 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
2455 (sdata->vif.type != NL80211_IFTYPE_OCB) &&
2456 !multicast && !authorized &&
2457 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
2458 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
2459 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2460 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
2461 sdata->name, hdr.addr1);
2464 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2470 if (unlikely(!multicast && skb->sk &&
2471 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)) {
2472 struct sk_buff *ack_skb = skb_clone_sk(skb);
2475 unsigned long flags;
2478 spin_lock_irqsave(&local->ack_status_lock, flags);
2479 id = idr_alloc(&local->ack_status_frames, ack_skb,
2480 1, 0x10000, GFP_ATOMIC);
2481 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2485 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2493 * If the skb is shared we need to obtain our own copy.
2495 if (skb_shared(skb)) {
2496 struct sk_buff *tmp_skb = skb;
2498 /* can't happen -- skb is a clone if info_id != 0 */
2501 skb = skb_clone(skb, GFP_ATOMIC);
2510 hdr.frame_control = fc;
2511 hdr.duration_id = 0;
2514 skip_header_bytes = ETH_HLEN;
2515 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2516 encaps_data = bridge_tunnel_header;
2517 encaps_len = sizeof(bridge_tunnel_header);
2518 skip_header_bytes -= 2;
2519 } else if (ethertype >= ETH_P_802_3_MIN) {
2520 encaps_data = rfc1042_header;
2521 encaps_len = sizeof(rfc1042_header);
2522 skip_header_bytes -= 2;
2528 nh_pos = skb_network_header(skb) - skb->data;
2529 h_pos = skb_transport_header(skb) - skb->data;
2531 skb_pull(skb, skip_header_bytes);
2532 nh_pos -= skip_header_bytes;
2533 h_pos -= skip_header_bytes;
2535 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
2538 * So we need to modify the skb header and hence need a copy of
2539 * that. The head_need variable above doesn't, so far, include
2540 * the needed header space that we don't need right away. If we
2541 * can, then we don't reallocate right now but only after the
2542 * frame arrives at the master device (if it does...)
2544 * If we cannot, however, then we will reallocate to include all
2545 * the ever needed space. Also, if we need to reallocate it anyway,
2546 * make it big enough for everything we may ever need.
2549 if (head_need > 0 || skb_cloned(skb)) {
2550 head_need += sdata->encrypt_headroom;
2551 head_need += local->tx_headroom;
2552 head_need = max_t(int, 0, head_need);
2553 if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
2554 ieee80211_free_txskb(&local->hw, skb);
2556 return ERR_PTR(-ENOMEM);
2561 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
2562 nh_pos += encaps_len;
2563 h_pos += encaps_len;
2566 #ifdef CONFIG_MAC80211_MESH
2567 if (meshhdrlen > 0) {
2568 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
2569 nh_pos += meshhdrlen;
2570 h_pos += meshhdrlen;
2574 if (ieee80211_is_data_qos(fc)) {
2575 __le16 *qos_control;
2577 qos_control = (__le16 *) skb_push(skb, 2);
2578 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2580 * Maybe we could actually set some fields here, for now just
2581 * initialise to zero to indicate no special operation.
2585 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2590 /* Update skb pointers to various headers since this modified frame
2591 * is going to go through Linux networking code that may potentially
2592 * need things like pointer to IP header. */
2593 skb_reset_mac_header(skb);
2594 skb_set_network_header(skb, nh_pos);
2595 skb_set_transport_header(skb, h_pos);
2597 info = IEEE80211_SKB_CB(skb);
2598 memset(info, 0, sizeof(*info));
2600 info->flags = info_flags;
2601 info->ack_frame_id = info_id;
2607 return ERR_PTR(ret);
2611 * fast-xmit overview
2613 * The core idea of this fast-xmit is to remove per-packet checks by checking
2614 * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2615 * checks that are needed to get the sta->fast_tx pointer assigned, after which
2616 * much less work can be done per packet. For example, fragmentation must be
2617 * disabled or the fast_tx pointer will not be set. All the conditions are seen
2620 * Once assigned, the fast_tx data structure also caches the per-packet 802.11
2621 * header and other data to aid packet processing in ieee80211_xmit_fast().
2623 * The most difficult part of this is that when any of these assumptions
2624 * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
2625 * ieee80211_check_fast_xmit() or friends) is required to reset the data,
2626 * since the per-packet code no longer checks the conditions. This is reflected
2627 * by the calls to these functions throughout the rest of the code, and must be
2628 * maintained if any of the TX path checks change.
2631 void ieee80211_check_fast_xmit(struct sta_info *sta)
2633 struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
2634 struct ieee80211_local *local = sta->local;
2635 struct ieee80211_sub_if_data *sdata = sta->sdata;
2636 struct ieee80211_hdr *hdr = (void *)build.hdr;
2637 struct ieee80211_chanctx_conf *chanctx_conf;
2640 if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
2643 /* Locking here protects both the pointer itself, and against concurrent
2644 * invocations winning data access races to, e.g., the key pointer that
2646 * Without it, the invocation of this function right after the key
2647 * pointer changes wouldn't be sufficient, as another CPU could access
2648 * the pointer, then stall, and then do the cache update after the CPU
2649 * that invalidated the key.
2650 * With the locking, such scenarios cannot happen as the check for the
2651 * key and the fast-tx assignment are done atomically, so the CPU that
2652 * modifies the key will either wait or other one will see the key
2653 * cleared/changed already.
2655 spin_lock_bh(&sta->lock);
2656 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
2657 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2658 sdata->vif.type == NL80211_IFTYPE_STATION)
2661 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2664 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
2665 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
2666 test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
2667 test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
2670 if (sdata->noack_map)
2673 /* fast-xmit doesn't handle fragmentation at all */
2674 if (local->hw.wiphy->frag_threshold != (u32)-1 &&
2675 !local->ops->set_frag_threshold)
2679 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2680 if (!chanctx_conf) {
2684 build.band = chanctx_conf->def.chan->band;
2687 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2689 switch (sdata->vif.type) {
2690 case NL80211_IFTYPE_ADHOC:
2692 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2693 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2694 memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
2697 case NL80211_IFTYPE_STATION:
2698 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2700 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2701 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2702 memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
2707 if (sdata->u.mgd.use_4addr) {
2708 /* non-regular ethertype cannot use the fastpath */
2709 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2710 IEEE80211_FCTL_TODS);
2712 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2713 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2714 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2715 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2719 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2721 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2722 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2723 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2726 case NL80211_IFTYPE_AP_VLAN:
2727 if (sdata->wdev.use_4addr) {
2728 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2729 IEEE80211_FCTL_TODS);
2731 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
2732 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2733 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2734 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2739 case NL80211_IFTYPE_AP:
2740 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2742 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2743 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2744 build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
2748 /* not handled on fast-xmit */
2754 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2757 /* We store the key here so there's no point in using rcu_dereference()
2758 * but that's fine because the code that changes the pointers will call
2759 * this function after doing so. For a single CPU that would be enough,
2760 * for multiple see the comment above.
2762 build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
2764 build.key = rcu_access_pointer(sdata->default_unicast_key);
2766 bool gen_iv, iv_spc, mmic;
2768 gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
2769 iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2770 mmic = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC;
2772 /* don't handle software crypto */
2773 if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
2776 switch (build.key->conf.cipher) {
2777 case WLAN_CIPHER_SUITE_CCMP:
2778 case WLAN_CIPHER_SUITE_CCMP_256:
2779 /* add fixed key ID */
2781 (build.hdr + build.hdr_len)[3] =
2782 0x20 | (build.key->conf.keyidx << 6);
2783 build.pn_offs = build.hdr_len;
2785 if (gen_iv || iv_spc)
2786 build.hdr_len += IEEE80211_CCMP_HDR_LEN;
2788 case WLAN_CIPHER_SUITE_GCMP:
2789 case WLAN_CIPHER_SUITE_GCMP_256:
2790 /* add fixed key ID */
2792 (build.hdr + build.hdr_len)[3] =
2793 0x20 | (build.key->conf.keyidx << 6);
2794 build.pn_offs = build.hdr_len;
2796 if (gen_iv || iv_spc)
2797 build.hdr_len += IEEE80211_GCMP_HDR_LEN;
2799 case WLAN_CIPHER_SUITE_TKIP:
2800 /* cannot handle MMIC or IV generation in xmit-fast */
2804 build.hdr_len += IEEE80211_TKIP_IV_LEN;
2806 case WLAN_CIPHER_SUITE_WEP40:
2807 case WLAN_CIPHER_SUITE_WEP104:
2808 /* cannot handle IV generation in fast-xmit */
2812 build.hdr_len += IEEE80211_WEP_IV_LEN;
2814 case WLAN_CIPHER_SUITE_AES_CMAC:
2815 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2816 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2817 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2819 "management cipher suite 0x%x enabled for data\n",
2820 build.key->conf.cipher);
2823 /* we don't know how to generate IVs for this at all */
2824 if (WARN_ON(gen_iv))
2826 /* pure hardware keys are OK, of course */
2827 if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
2829 /* cipher scheme might require space allocation */
2831 build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
2834 build.hdr_len += build.key->conf.iv_len;
2837 fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
2840 hdr->frame_control = fc;
2842 memcpy(build.hdr + build.hdr_len,
2843 rfc1042_header, sizeof(rfc1042_header));
2844 build.hdr_len += sizeof(rfc1042_header);
2846 fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
2847 /* if the kmemdup fails, continue w/o fast_tx */
2852 /* we might have raced against another call to this function */
2853 old = rcu_dereference_protected(sta->fast_tx,
2854 lockdep_is_held(&sta->lock));
2855 rcu_assign_pointer(sta->fast_tx, fast_tx);
2857 kfree_rcu(old, rcu_head);
2858 spin_unlock_bh(&sta->lock);
2861 void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
2863 struct sta_info *sta;
2866 list_for_each_entry_rcu(sta, &local->sta_list, list)
2867 ieee80211_check_fast_xmit(sta);
2871 void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
2873 struct ieee80211_local *local = sdata->local;
2874 struct sta_info *sta;
2878 list_for_each_entry_rcu(sta, &local->sta_list, list) {
2879 if (sdata != sta->sdata &&
2880 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
2882 ieee80211_check_fast_xmit(sta);
2888 void ieee80211_clear_fast_xmit(struct sta_info *sta)
2890 struct ieee80211_fast_tx *fast_tx;
2892 spin_lock_bh(&sta->lock);
2893 fast_tx = rcu_dereference_protected(sta->fast_tx,
2894 lockdep_is_held(&sta->lock));
2895 RCU_INIT_POINTER(sta->fast_tx, NULL);
2896 spin_unlock_bh(&sta->lock);
2899 kfree_rcu(fast_tx, rcu_head);
2902 static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
2903 struct sk_buff *skb, int headroom,
2906 int amsdu_len = *subframe_len + sizeof(struct ethhdr);
2907 int padding = (4 - amsdu_len) & 3;
2909 if (skb_headroom(skb) < headroom || skb_tailroom(skb) < padding) {
2910 I802_DEBUG_INC(local->tx_expand_skb_head);
2912 if (pskb_expand_head(skb, headroom, padding, GFP_ATOMIC)) {
2913 wiphy_debug(local->hw.wiphy,
2914 "failed to reallocate TX buffer\n");
2920 *subframe_len += padding;
2921 memset(skb_put(skb, padding), 0, padding);
2927 static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
2928 struct ieee80211_fast_tx *fast_tx,
2929 struct sk_buff *skb)
2931 struct ieee80211_local *local = sdata->local;
2932 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2933 struct ieee80211_hdr *hdr;
2934 struct ethhdr amsdu_hdr;
2935 int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
2936 int subframe_len = skb->len - hdr_len;
2940 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
2943 if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
2946 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(amsdu_hdr),
2950 amsdu_hdr.h_proto = cpu_to_be16(subframe_len);
2951 memcpy(amsdu_hdr.h_source, skb->data + fast_tx->sa_offs, ETH_ALEN);
2952 memcpy(amsdu_hdr.h_dest, skb->data + fast_tx->da_offs, ETH_ALEN);
2954 data = skb_push(skb, sizeof(amsdu_hdr));
2955 memmove(data, data + sizeof(amsdu_hdr), hdr_len);
2956 memcpy(data + hdr_len, &amsdu_hdr, sizeof(amsdu_hdr));
2959 qc = ieee80211_get_qos_ctl(hdr);
2960 *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
2962 info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
2967 static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
2968 struct sta_info *sta,
2969 struct ieee80211_fast_tx *fast_tx,
2970 struct sk_buff *skb)
2972 struct ieee80211_local *local = sdata->local;
2973 struct fq *fq = &local->fq;
2975 struct fq_flow *flow;
2976 u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
2977 struct ieee80211_txq *txq = sta->sta.txq[tid];
2978 struct txq_info *txqi;
2979 struct sk_buff **frag_tail, *head;
2980 int subframe_len = skb->len - ETH_ALEN;
2981 u8 max_subframes = sta->sta.max_amsdu_subframes;
2982 int max_frags = local->hw.max_tx_fragments;
2983 int max_amsdu_len = sta->sta.max_amsdu_len;
2987 unsigned int orig_len;
2990 if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
2996 txqi = to_txq_info(txq);
2997 if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3000 if (sta->sta.max_rc_amsdu_len)
3001 max_amsdu_len = min_t(int, max_amsdu_len,
3002 sta->sta.max_rc_amsdu_len);
3004 spin_lock_bh(&fq->lock);
3006 /* TODO: Ideally aggregation should be done on dequeue to remain
3007 * responsive to environment changes.
3011 flow = fq_flow_classify(fq, tin, skb, fq_flow_get_default_func);
3012 head = skb_peek_tail(&flow->queue);
3016 orig_len = head->len;
3018 if (skb->len + head->len > max_amsdu_len)
3021 if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
3024 nfrags = 1 + skb_shinfo(skb)->nr_frags;
3025 nfrags += 1 + skb_shinfo(head)->nr_frags;
3026 frag_tail = &skb_shinfo(head)->frag_list;
3027 while (*frag_tail) {
3028 nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3029 frag_tail = &(*frag_tail)->next;
3033 if (max_subframes && n > max_subframes)
3036 if (max_frags && nfrags > max_frags)
3039 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) + 2,
3044 data = skb_push(skb, ETH_ALEN + 2);
3045 memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3047 data += 2 * ETH_ALEN;
3048 len = cpu_to_be16(subframe_len);
3049 memcpy(data, &len, 2);
3050 memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3052 head->len += skb->len;
3053 head->data_len += skb->len;
3056 flow->backlog += head->len - orig_len;
3057 tin->backlog_bytes += head->len - orig_len;
3059 fq_recalc_backlog(fq, tin, flow);
3062 spin_unlock_bh(&fq->lock);
3067 static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
3068 struct net_device *dev, struct sta_info *sta,
3069 struct ieee80211_fast_tx *fast_tx,
3070 struct sk_buff *skb)
3072 struct ieee80211_local *local = sdata->local;
3073 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3074 int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3075 int hw_headroom = sdata->local->hw.extra_tx_headroom;
3077 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3078 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3079 struct ieee80211_tx_data tx;
3080 ieee80211_tx_result r;
3081 struct tid_ampdu_tx *tid_tx = NULL;
3082 u8 tid = IEEE80211_NUM_TIDS;
3084 /* control port protocol needs a lot of special handling */
3085 if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3088 /* only RFC 1042 SNAP */
3089 if (ethertype < ETH_P_802_3_MIN)
3092 /* don't handle TX status request here either */
3093 if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3096 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3097 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3098 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
3100 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3102 if (tid_tx->timeout)
3103 tid_tx->last_tx = jiffies;
3107 /* after this point (skb is modified) we cannot return false */
3109 if (skb_shared(skb)) {
3110 struct sk_buff *tmp_skb = skb;
3112 skb = skb_clone(skb, GFP_ATOMIC);
3119 ieee80211_tx_stats(dev, skb->len + extra_head);
3121 if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3122 ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
3125 /* will not be crypto-handled beyond what we do here, so use false
3126 * as the may-encrypt argument for the resize to not account for
3127 * more room than we already have in 'extra_head'
3129 if (unlikely(ieee80211_skb_resize(sdata, skb,
3130 max_t(int, extra_head + hw_headroom -
3131 skb_headroom(skb), 0),
3137 memcpy(ð, skb->data, ETH_HLEN - 2);
3138 hdr = (void *)skb_push(skb, extra_head);
3139 memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3140 memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
3141 memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
3143 memset(info, 0, sizeof(*info));
3144 info->band = fast_tx->band;
3145 info->control.vif = &sdata->vif;
3146 info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3147 IEEE80211_TX_CTL_DONTFRAG |
3148 (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
3150 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3151 *ieee80211_get_qos_ctl(hdr) = tid;
3152 if (!sta->sta.txq[0])
3153 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3155 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3156 hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3157 sdata->sequence_number += 0x10;
3160 if (skb_shinfo(skb)->gso_size)
3161 sta->tx_stats.msdu[tid] +=
3162 DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3164 sta->tx_stats.msdu[tid]++;
3166 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3168 __skb_queue_head_init(&tx.skbs);
3170 tx.flags = IEEE80211_TX_UNICAST;
3174 tx.key = fast_tx->key;
3177 info->control.hw_key = &fast_tx->key->conf;
3179 if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
3181 r = ieee80211_tx_h_rate_ctrl(&tx);
3185 if (r != TX_CONTINUE) {
3192 /* statistics normally done by ieee80211_tx_h_stats (but that
3193 * has to consider fragmentation, so is more complex)
3195 sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3196 sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
3198 if (fast_tx->pn_offs) {
3200 u8 *crypto_hdr = skb->data + fast_tx->pn_offs;
3202 switch (fast_tx->key->conf.cipher) {
3203 case WLAN_CIPHER_SUITE_CCMP:
3204 case WLAN_CIPHER_SUITE_CCMP_256:
3205 case WLAN_CIPHER_SUITE_GCMP:
3206 case WLAN_CIPHER_SUITE_GCMP_256:
3207 pn = atomic64_inc_return(&fast_tx->key->conf.tx_pn);
3209 crypto_hdr[1] = pn >> 8;
3210 crypto_hdr[4] = pn >> 16;
3211 crypto_hdr[5] = pn >> 24;
3212 crypto_hdr[6] = pn >> 32;
3213 crypto_hdr[7] = pn >> 40;
3218 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3219 sdata = container_of(sdata->bss,
3220 struct ieee80211_sub_if_data, u.ap);
3222 __skb_queue_tail(&tx.skbs, skb);
3223 ieee80211_tx_frags(local, &sdata->vif, &sta->sta, &tx.skbs, false);
3227 void __ieee80211_subif_start_xmit(struct sk_buff *skb,
3228 struct net_device *dev,
3231 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3232 struct sta_info *sta;
3233 struct sk_buff *next;
3235 if (unlikely(skb->len < ETH_HLEN)) {
3242 if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
3245 if (!IS_ERR_OR_NULL(sta)) {
3246 struct ieee80211_fast_tx *fast_tx;
3248 fast_tx = rcu_dereference(sta->fast_tx);
3251 ieee80211_xmit_fast(sdata, dev, sta, fast_tx, skb))
3255 if (skb_is_gso(skb)) {
3256 struct sk_buff *segs;
3258 segs = skb_gso_segment(skb, 0);
3266 /* we cannot process non-linear frames on this path */
3267 if (skb_linearize(skb)) {
3272 /* the frame could be fragmented, software-encrypted, and other
3273 * things so we cannot really handle checksum offload with it -
3274 * fix it up in software before we handle anything else.
3276 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3277 skb_set_transport_header(skb,
3278 skb_checksum_start_offset(skb));
3279 if (skb_checksum_help(skb))
3292 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta);
3296 ieee80211_tx_stats(dev, skb->len);
3298 ieee80211_xmit(sdata, sta, skb);
3308 * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
3309 * @skb: packet to be sent
3310 * @dev: incoming interface
3312 * On failure skb will be freed.
3314 netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
3315 struct net_device *dev)
3317 __ieee80211_subif_start_xmit(skb, dev, 0);
3318 return NETDEV_TX_OK;
3322 ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
3323 struct sk_buff *skb, u32 info_flags)
3325 struct ieee80211_hdr *hdr;
3326 struct ieee80211_tx_data tx = {
3327 .local = sdata->local,
3330 struct sta_info *sta;
3334 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
3336 skb = ERR_PTR(-EINVAL);
3340 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta);
3344 hdr = (void *)skb->data;
3345 tx.sta = sta_info_get(sdata, hdr->addr1);
3348 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
3351 return ERR_PTR(-EINVAL);
3360 * ieee80211_clear_tx_pending may not be called in a context where
3361 * it is possible that it packets could come in again.
3363 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
3365 struct sk_buff *skb;
3368 for (i = 0; i < local->hw.queues; i++) {
3369 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
3370 ieee80211_free_txskb(&local->hw, skb);
3375 * Returns false if the frame couldn't be transmitted but was queued instead,
3376 * which in this case means re-queued -- take as an indication to stop sending
3377 * more pending frames.
3379 static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
3380 struct sk_buff *skb)
3382 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3383 struct ieee80211_sub_if_data *sdata;
3384 struct sta_info *sta;
3385 struct ieee80211_hdr *hdr;
3387 struct ieee80211_chanctx_conf *chanctx_conf;
3389 sdata = vif_to_sdata(info->control.vif);
3391 if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
3392 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3393 if (unlikely(!chanctx_conf)) {
3397 info->band = chanctx_conf->def.chan->band;
3398 result = ieee80211_tx(sdata, NULL, skb, true);
3400 struct sk_buff_head skbs;
3402 __skb_queue_head_init(&skbs);
3403 __skb_queue_tail(&skbs, skb);
3405 hdr = (struct ieee80211_hdr *)skb->data;
3406 sta = sta_info_get(sdata, hdr->addr1);
3408 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
3415 * Transmit all pending packets. Called from tasklet.
3417 void ieee80211_tx_pending(unsigned long data)
3419 struct ieee80211_local *local = (struct ieee80211_local *)data;
3420 unsigned long flags;
3426 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
3427 for (i = 0; i < local->hw.queues; i++) {
3429 * If queue is stopped by something other than due to pending
3430 * frames, or we have no pending frames, proceed to next queue.
3432 if (local->queue_stop_reasons[i] ||
3433 skb_queue_empty(&local->pending[i]))
3436 while (!skb_queue_empty(&local->pending[i])) {
3437 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
3438 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3440 if (WARN_ON(!info->control.vif)) {
3441 ieee80211_free_txskb(&local->hw, skb);
3445 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
3448 txok = ieee80211_tx_pending_skb(local, skb);
3449 spin_lock_irqsave(&local->queue_stop_reason_lock,
3455 if (skb_queue_empty(&local->pending[i]))
3456 ieee80211_propagate_queue_wake(local, i);
3458 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
3463 /* functions for drivers to get certain frames */
3465 static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
3466 struct ps_data *ps, struct sk_buff *skb,
3471 int i, have_bits = 0, n1, n2;
3473 /* Generate bitmap for TIM only if there are any STAs in power save
3475 if (atomic_read(&ps->num_sta_ps) > 0)
3476 /* in the hope that this is faster than
3477 * checking byte-for-byte */
3478 have_bits = !bitmap_empty((unsigned long *)ps->tim,
3479 IEEE80211_MAX_AID+1);
3481 if (ps->dtim_count == 0)
3482 ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
3487 tim = pos = (u8 *) skb_put(skb, 6);
3488 *pos++ = WLAN_EID_TIM;
3490 *pos++ = ps->dtim_count;
3491 *pos++ = sdata->vif.bss_conf.dtim_period;
3493 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
3496 ps->dtim_bc_mc = aid0 == 1;
3499 /* Find largest even number N1 so that bits numbered 1 through
3500 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
3501 * (N2 + 1) x 8 through 2007 are 0. */
3503 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
3510 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
3517 /* Bitmap control */
3519 /* Part Virt Bitmap */
3520 skb_put(skb, n2 - n1);
3521 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
3523 tim[1] = n2 - n1 + 4;
3525 *pos++ = aid0; /* Bitmap control */
3526 *pos++ = 0; /* Part Virt Bitmap */
3530 static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
3531 struct ps_data *ps, struct sk_buff *skb,
3534 struct ieee80211_local *local = sdata->local;
3537 * Not very nice, but we want to allow the driver to call
3538 * ieee80211_beacon_get() as a response to the set_tim()
3539 * callback. That, however, is already invoked under the
3540 * sta_lock to guarantee consistent and race-free update
3541 * of the tim bitmap in mac80211 and the driver.
3543 if (local->tim_in_locked_section) {
3544 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
3546 spin_lock_bh(&local->tim_lock);
3547 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
3548 spin_unlock_bh(&local->tim_lock);
3554 static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
3555 struct beacon_data *beacon)
3557 struct probe_resp *resp;
3559 size_t beacon_data_len;
3561 u8 count = beacon->csa_current_counter;
3563 switch (sdata->vif.type) {
3564 case NL80211_IFTYPE_AP:
3565 beacon_data = beacon->tail;
3566 beacon_data_len = beacon->tail_len;
3568 case NL80211_IFTYPE_ADHOC:
3569 beacon_data = beacon->head;
3570 beacon_data_len = beacon->head_len;
3572 case NL80211_IFTYPE_MESH_POINT:
3573 beacon_data = beacon->head;
3574 beacon_data_len = beacon->head_len;
3581 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
3582 resp = rcu_dereference(sdata->u.ap.probe_resp);
3584 if (beacon->csa_counter_offsets[i]) {
3585 if (WARN_ON_ONCE(beacon->csa_counter_offsets[i] >=
3591 beacon_data[beacon->csa_counter_offsets[i]] = count;
3594 if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
3595 resp->data[resp->csa_counter_offsets[i]] = count;
3600 static u8 __ieee80211_csa_update_counter(struct beacon_data *beacon)
3602 beacon->csa_current_counter--;
3604 /* the counter should never reach 0 */
3605 WARN_ON_ONCE(!beacon->csa_current_counter);
3607 return beacon->csa_current_counter;
3610 u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
3612 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3613 struct beacon_data *beacon = NULL;
3618 if (sdata->vif.type == NL80211_IFTYPE_AP)
3619 beacon = rcu_dereference(sdata->u.ap.beacon);
3620 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3621 beacon = rcu_dereference(sdata->u.ibss.presp);
3622 else if (ieee80211_vif_is_mesh(&sdata->vif))
3623 beacon = rcu_dereference(sdata->u.mesh.beacon);
3628 count = __ieee80211_csa_update_counter(beacon);
3634 EXPORT_SYMBOL(ieee80211_csa_update_counter);
3636 bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
3638 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3639 struct beacon_data *beacon = NULL;
3641 size_t beacon_data_len;
3644 if (!ieee80211_sdata_running(sdata))
3648 if (vif->type == NL80211_IFTYPE_AP) {
3649 struct ieee80211_if_ap *ap = &sdata->u.ap;
3651 beacon = rcu_dereference(ap->beacon);
3652 if (WARN_ON(!beacon || !beacon->tail))
3654 beacon_data = beacon->tail;
3655 beacon_data_len = beacon->tail_len;
3656 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
3657 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
3659 beacon = rcu_dereference(ifibss->presp);
3663 beacon_data = beacon->head;
3664 beacon_data_len = beacon->head_len;
3665 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
3666 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
3668 beacon = rcu_dereference(ifmsh->beacon);
3672 beacon_data = beacon->head;
3673 beacon_data_len = beacon->head_len;
3679 if (!beacon->csa_counter_offsets[0])
3682 if (WARN_ON_ONCE(beacon->csa_counter_offsets[0] > beacon_data_len))
3685 if (beacon_data[beacon->csa_counter_offsets[0]] == 1)
3692 EXPORT_SYMBOL(ieee80211_csa_is_complete);
3694 static struct sk_buff *
3695 __ieee80211_beacon_get(struct ieee80211_hw *hw,
3696 struct ieee80211_vif *vif,
3697 struct ieee80211_mutable_offsets *offs,
3700 struct ieee80211_local *local = hw_to_local(hw);
3701 struct beacon_data *beacon = NULL;
3702 struct sk_buff *skb = NULL;
3703 struct ieee80211_tx_info *info;
3704 struct ieee80211_sub_if_data *sdata = NULL;
3705 enum nl80211_band band;
3706 struct ieee80211_tx_rate_control txrc;
3707 struct ieee80211_chanctx_conf *chanctx_conf;
3708 int csa_off_base = 0;
3712 sdata = vif_to_sdata(vif);
3713 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3715 if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
3719 memset(offs, 0, sizeof(*offs));
3721 if (sdata->vif.type == NL80211_IFTYPE_AP) {
3722 struct ieee80211_if_ap *ap = &sdata->u.ap;
3724 beacon = rcu_dereference(ap->beacon);
3726 if (beacon->csa_counter_offsets[0]) {
3728 __ieee80211_csa_update_counter(beacon);
3730 ieee80211_set_csa(sdata, beacon);
3734 * headroom, head length,
3735 * tail length and maximum TIM length
3737 skb = dev_alloc_skb(local->tx_headroom +
3739 beacon->tail_len + 256 +
3740 local->hw.extra_beacon_tailroom);
3744 skb_reserve(skb, local->tx_headroom);
3745 memcpy(skb_put(skb, beacon->head_len), beacon->head,
3748 ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
3752 offs->tim_offset = beacon->head_len;
3753 offs->tim_length = skb->len - beacon->head_len;
3755 /* for AP the csa offsets are from tail */
3756 csa_off_base = skb->len;
3760 memcpy(skb_put(skb, beacon->tail_len),
3761 beacon->tail, beacon->tail_len);
3764 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
3765 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
3766 struct ieee80211_hdr *hdr;
3768 beacon = rcu_dereference(ifibss->presp);
3772 if (beacon->csa_counter_offsets[0]) {
3774 __ieee80211_csa_update_counter(beacon);
3776 ieee80211_set_csa(sdata, beacon);
3779 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
3780 local->hw.extra_beacon_tailroom);
3783 skb_reserve(skb, local->tx_headroom);
3784 memcpy(skb_put(skb, beacon->head_len), beacon->head,
3787 hdr = (struct ieee80211_hdr *) skb->data;
3788 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3789 IEEE80211_STYPE_BEACON);
3790 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
3791 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
3793 beacon = rcu_dereference(ifmsh->beacon);
3797 if (beacon->csa_counter_offsets[0]) {
3799 /* TODO: For mesh csa_counter is in TU, so
3800 * decrementing it by one isn't correct, but
3801 * for now we leave it consistent with overall
3802 * mac80211's behavior.
3804 __ieee80211_csa_update_counter(beacon);
3806 ieee80211_set_csa(sdata, beacon);
3809 if (ifmsh->sync_ops)
3810 ifmsh->sync_ops->adjust_tbtt(sdata, beacon);
3812 skb = dev_alloc_skb(local->tx_headroom +
3816 local->hw.extra_beacon_tailroom);
3819 skb_reserve(skb, local->tx_headroom);
3820 memcpy(skb_put(skb, beacon->head_len), beacon->head,
3822 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
3825 offs->tim_offset = beacon->head_len;
3826 offs->tim_length = skb->len - beacon->head_len;
3829 memcpy(skb_put(skb, beacon->tail_len), beacon->tail,
3837 if (offs && beacon) {
3840 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
3841 u16 csa_off = beacon->csa_counter_offsets[i];
3846 offs->csa_counter_offs[i] = csa_off_base + csa_off;
3850 band = chanctx_conf->def.chan->band;
3852 info = IEEE80211_SKB_CB(skb);
3854 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
3855 info->flags |= IEEE80211_TX_CTL_NO_ACK;
3858 memset(&txrc, 0, sizeof(txrc));
3860 txrc.sband = local->hw.wiphy->bands[band];
3861 txrc.bss_conf = &sdata->vif.bss_conf;
3863 txrc.reported_rate.idx = -1;
3864 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
3865 if (txrc.rate_idx_mask == (1 << txrc.sband->n_bitrates) - 1)
3866 txrc.max_rate_idx = -1;
3868 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
3870 rate_control_get_rate(sdata, NULL, &txrc);
3872 info->control.vif = vif;
3874 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
3875 IEEE80211_TX_CTL_ASSIGN_SEQ |
3876 IEEE80211_TX_CTL_FIRST_FRAGMENT;
3884 ieee80211_beacon_get_template(struct ieee80211_hw *hw,
3885 struct ieee80211_vif *vif,
3886 struct ieee80211_mutable_offsets *offs)
3888 return __ieee80211_beacon_get(hw, vif, offs, true);
3890 EXPORT_SYMBOL(ieee80211_beacon_get_template);
3892 struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
3893 struct ieee80211_vif *vif,
3894 u16 *tim_offset, u16 *tim_length)
3896 struct ieee80211_mutable_offsets offs = {};
3897 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
3898 struct sk_buff *copy;
3899 struct ieee80211_supported_band *sband;
3906 *tim_offset = offs.tim_offset;
3909 *tim_length = offs.tim_length;
3911 if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
3912 !hw_to_local(hw)->monitors)
3915 /* send a copy to monitor interfaces */
3916 copy = skb_copy(bcn, GFP_ATOMIC);
3920 shift = ieee80211_vif_get_shift(vif);
3921 sband = hw->wiphy->bands[ieee80211_get_sdata_band(vif_to_sdata(vif))];
3922 ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false);
3926 EXPORT_SYMBOL(ieee80211_beacon_get_tim);
3928 struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
3929 struct ieee80211_vif *vif)
3931 struct ieee80211_if_ap *ap = NULL;
3932 struct sk_buff *skb = NULL;
3933 struct probe_resp *presp = NULL;
3934 struct ieee80211_hdr *hdr;
3935 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3937 if (sdata->vif.type != NL80211_IFTYPE_AP)
3943 presp = rcu_dereference(ap->probe_resp);
3947 skb = dev_alloc_skb(presp->len);
3951 memcpy(skb_put(skb, presp->len), presp->data, presp->len);
3953 hdr = (struct ieee80211_hdr *) skb->data;
3954 memset(hdr->addr1, 0, sizeof(hdr->addr1));
3960 EXPORT_SYMBOL(ieee80211_proberesp_get);
3962 struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
3963 struct ieee80211_vif *vif)
3965 struct ieee80211_sub_if_data *sdata;
3966 struct ieee80211_if_managed *ifmgd;
3967 struct ieee80211_pspoll *pspoll;
3968 struct ieee80211_local *local;
3969 struct sk_buff *skb;
3971 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
3974 sdata = vif_to_sdata(vif);
3975 ifmgd = &sdata->u.mgd;
3976 local = sdata->local;
3978 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
3982 skb_reserve(skb, local->hw.extra_tx_headroom);
3984 pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
3985 memset(pspoll, 0, sizeof(*pspoll));
3986 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
3987 IEEE80211_STYPE_PSPOLL);
3988 pspoll->aid = cpu_to_le16(ifmgd->aid);
3990 /* aid in PS-Poll has its two MSBs each set to 1 */
3991 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
3993 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
3994 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
3998 EXPORT_SYMBOL(ieee80211_pspoll_get);
4000 struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
4001 struct ieee80211_vif *vif)
4003 struct ieee80211_hdr_3addr *nullfunc;
4004 struct ieee80211_sub_if_data *sdata;
4005 struct ieee80211_if_managed *ifmgd;
4006 struct ieee80211_local *local;
4007 struct sk_buff *skb;
4009 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4012 sdata = vif_to_sdata(vif);
4013 ifmgd = &sdata->u.mgd;
4014 local = sdata->local;
4016 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*nullfunc));
4020 skb_reserve(skb, local->hw.extra_tx_headroom);
4022 nullfunc = (struct ieee80211_hdr_3addr *) skb_put(skb,
4024 memset(nullfunc, 0, sizeof(*nullfunc));
4025 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
4026 IEEE80211_STYPE_NULLFUNC |
4027 IEEE80211_FCTL_TODS);
4028 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
4029 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
4030 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
4034 EXPORT_SYMBOL(ieee80211_nullfunc_get);
4036 struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
4038 const u8 *ssid, size_t ssid_len,
4041 struct ieee80211_local *local = hw_to_local(hw);
4042 struct ieee80211_hdr_3addr *hdr;
4043 struct sk_buff *skb;
4047 ie_ssid_len = 2 + ssid_len;
4049 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
4050 ie_ssid_len + tailroom);
4054 skb_reserve(skb, local->hw.extra_tx_headroom);
4056 hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
4057 memset(hdr, 0, sizeof(*hdr));
4058 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4059 IEEE80211_STYPE_PROBE_REQ);
4060 eth_broadcast_addr(hdr->addr1);
4061 memcpy(hdr->addr2, src_addr, ETH_ALEN);
4062 eth_broadcast_addr(hdr->addr3);
4064 pos = skb_put(skb, ie_ssid_len);
4065 *pos++ = WLAN_EID_SSID;
4068 memcpy(pos, ssid, ssid_len);
4073 EXPORT_SYMBOL(ieee80211_probereq_get);
4075 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4076 const void *frame, size_t frame_len,
4077 const struct ieee80211_tx_info *frame_txctl,
4078 struct ieee80211_rts *rts)
4080 const struct ieee80211_hdr *hdr = frame;
4082 rts->frame_control =
4083 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
4084 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
4086 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
4087 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
4089 EXPORT_SYMBOL(ieee80211_rts_get);
4091 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4092 const void *frame, size_t frame_len,
4093 const struct ieee80211_tx_info *frame_txctl,
4094 struct ieee80211_cts *cts)
4096 const struct ieee80211_hdr *hdr = frame;
4098 cts->frame_control =
4099 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
4100 cts->duration = ieee80211_ctstoself_duration(hw, vif,
4101 frame_len, frame_txctl);
4102 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
4104 EXPORT_SYMBOL(ieee80211_ctstoself_get);
4107 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
4108 struct ieee80211_vif *vif)
4110 struct ieee80211_local *local = hw_to_local(hw);
4111 struct sk_buff *skb = NULL;
4112 struct ieee80211_tx_data tx;
4113 struct ieee80211_sub_if_data *sdata;
4115 struct ieee80211_tx_info *info;
4116 struct ieee80211_chanctx_conf *chanctx_conf;
4118 sdata = vif_to_sdata(vif);
4121 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4126 if (sdata->vif.type == NL80211_IFTYPE_AP) {
4127 struct beacon_data *beacon =
4128 rcu_dereference(sdata->u.ap.beacon);
4130 if (!beacon || !beacon->head)
4133 ps = &sdata->u.ap.ps;
4134 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4135 ps = &sdata->u.mesh.ps;
4140 if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
4141 goto out; /* send buffered bc/mc only after DTIM beacon */
4144 skb = skb_dequeue(&ps->bc_buf);
4147 local->total_ps_buffered--;
4149 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
4150 struct ieee80211_hdr *hdr =
4151 (struct ieee80211_hdr *) skb->data;
4152 /* more buffered multicast/broadcast frames ==> set
4153 * MoreData flag in IEEE 802.11 header to inform PS
4155 hdr->frame_control |=
4156 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
4159 if (sdata->vif.type == NL80211_IFTYPE_AP)
4160 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
4161 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
4163 dev_kfree_skb_any(skb);
4166 info = IEEE80211_SKB_CB(skb);
4168 tx.flags |= IEEE80211_TX_PS_BUFFERED;
4169 info->band = chanctx_conf->def.chan->band;
4171 if (invoke_tx_handlers(&tx))
4178 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
4180 int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
4182 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
4183 struct ieee80211_sub_if_data *sdata = sta->sdata;
4184 struct ieee80211_local *local = sdata->local;
4188 lockdep_assert_held(&local->sta_mtx);
4190 /* only some cases are supported right now */
4191 switch (sdata->vif.type) {
4192 case NL80211_IFTYPE_STATION:
4193 case NL80211_IFTYPE_AP:
4194 case NL80211_IFTYPE_AP_VLAN:
4201 if (WARN_ON(tid >= IEEE80211_NUM_UPS))
4204 if (sta->reserved_tid == tid) {
4209 if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
4210 sdata_err(sdata, "TID reservation already active\n");
4215 ieee80211_stop_vif_queues(sdata->local, sdata,
4216 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
4220 /* Tear down BA sessions so we stop aggregating on this TID */
4221 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
4222 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
4223 __ieee80211_stop_tx_ba_session(sta, tid,
4224 AGG_STOP_LOCAL_REQUEST);
4227 queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
4228 __ieee80211_flush_queues(local, sdata, queues, false);
4230 sta->reserved_tid = tid;
4232 ieee80211_wake_vif_queues(local, sdata,
4233 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
4235 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
4236 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
4242 EXPORT_SYMBOL(ieee80211_reserve_tid);
4244 void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
4246 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
4247 struct ieee80211_sub_if_data *sdata = sta->sdata;
4249 lockdep_assert_held(&sdata->local->sta_mtx);
4251 /* only some cases are supported right now */
4252 switch (sdata->vif.type) {
4253 case NL80211_IFTYPE_STATION:
4254 case NL80211_IFTYPE_AP:
4255 case NL80211_IFTYPE_AP_VLAN:
4262 if (tid != sta->reserved_tid) {
4263 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
4267 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
4269 EXPORT_SYMBOL(ieee80211_unreserve_tid);
4271 void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
4272 struct sk_buff *skb, int tid,
4273 enum nl80211_band band)
4275 int ac = ieee802_1d_to_ac[tid & 7];
4277 skb_reset_mac_header(skb);
4278 skb_reset_network_header(skb);
4279 skb_reset_transport_header(skb);
4281 skb_set_queue_mapping(skb, ac);
4282 skb->priority = tid;
4284 skb->dev = sdata->dev;
4287 * The other path calling ieee80211_xmit is from the tasklet,
4288 * and while we can handle concurrent transmissions locking
4289 * requirements are that we do not come into tx with bhs on.
4292 IEEE80211_SKB_CB(skb)->band = band;
4293 ieee80211_xmit(sdata, NULL, skb);