Linux 4.19.133
[linux/fpc-iii.git] / net / mac80211 / tx.c
blob3160ffd93a153abfc265efa4fd67c00c57e9e5f8
1 /*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright 2013-2014 Intel Mobile Communications GmbH
7 * Copyright (C) 2018, 2020 Intel Corporation
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 * Transmit and frame generation functions.
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/skbuff.h>
20 #include <linux/if_vlan.h>
21 #include <linux/etherdevice.h>
22 #include <linux/bitmap.h>
23 #include <linux/rcupdate.h>
24 #include <linux/export.h>
25 #include <net/net_namespace.h>
26 #include <net/ieee80211_radiotap.h>
27 #include <net/cfg80211.h>
28 #include <net/mac80211.h>
29 #include <net/codel.h>
30 #include <net/codel_impl.h>
31 #include <asm/unaligned.h>
32 #include <net/fq_impl.h>
34 #include "ieee80211_i.h"
35 #include "driver-ops.h"
36 #include "led.h"
37 #include "mesh.h"
38 #include "wep.h"
39 #include "wpa.h"
40 #include "wme.h"
41 #include "rate.h"
43 /* misc utils */
45 static inline void ieee80211_tx_stats(struct net_device *dev, u32 len)
47 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
49 u64_stats_update_begin(&tstats->syncp);
50 tstats->tx_packets++;
51 tstats->tx_bytes += len;
52 u64_stats_update_end(&tstats->syncp);
55 static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
56 struct sk_buff *skb, int group_addr,
57 int next_frag_len)
59 int rate, mrate, erp, dur, i, shift = 0;
60 struct ieee80211_rate *txrate;
61 struct ieee80211_local *local = tx->local;
62 struct ieee80211_supported_band *sband;
63 struct ieee80211_hdr *hdr;
64 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
65 struct ieee80211_chanctx_conf *chanctx_conf;
66 u32 rate_flags = 0;
68 /* assume HW handles this */
69 if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))
70 return 0;
72 rcu_read_lock();
73 chanctx_conf = rcu_dereference(tx->sdata->vif.chanctx_conf);
74 if (chanctx_conf) {
75 shift = ieee80211_chandef_get_shift(&chanctx_conf->def);
76 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
78 rcu_read_unlock();
80 /* uh huh? */
81 if (WARN_ON_ONCE(tx->rate.idx < 0))
82 return 0;
84 sband = local->hw.wiphy->bands[info->band];
85 txrate = &sband->bitrates[tx->rate.idx];
87 erp = txrate->flags & IEEE80211_RATE_ERP_G;
90 * data and mgmt (except PS Poll):
91 * - during CFP: 32768
92 * - during contention period:
93 * if addr1 is group address: 0
94 * if more fragments = 0 and addr1 is individual address: time to
95 * transmit one ACK plus SIFS
96 * if more fragments = 1 and addr1 is individual address: time to
97 * transmit next fragment plus 2 x ACK plus 3 x SIFS
99 * IEEE 802.11, 9.6:
100 * - control response frame (CTS or ACK) shall be transmitted using the
101 * same rate as the immediately previous frame in the frame exchange
102 * sequence, if this rate belongs to the PHY mandatory rates, or else
103 * at the highest possible rate belonging to the PHY rates in the
104 * BSSBasicRateSet
106 hdr = (struct ieee80211_hdr *)skb->data;
107 if (ieee80211_is_ctl(hdr->frame_control)) {
108 /* TODO: These control frames are not currently sent by
109 * mac80211, but should they be implemented, this function
110 * needs to be updated to support duration field calculation.
112 * RTS: time needed to transmit pending data/mgmt frame plus
113 * one CTS frame plus one ACK frame plus 3 x SIFS
114 * CTS: duration of immediately previous RTS minus time
115 * required to transmit CTS and its SIFS
116 * ACK: 0 if immediately previous directed data/mgmt had
117 * more=0, with more=1 duration in ACK frame is duration
118 * from previous frame minus time needed to transmit ACK
119 * and its SIFS
120 * PS Poll: BIT(15) | BIT(14) | aid
122 return 0;
125 /* data/mgmt */
126 if (0 /* FIX: data/mgmt during CFP */)
127 return cpu_to_le16(32768);
129 if (group_addr) /* Group address as the destination - no ACK */
130 return 0;
132 /* Individual destination address:
133 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
134 * CTS and ACK frames shall be transmitted using the highest rate in
135 * basic rate set that is less than or equal to the rate of the
136 * immediately previous frame and that is using the same modulation
137 * (CCK or OFDM). If no basic rate set matches with these requirements,
138 * the highest mandatory rate of the PHY that is less than or equal to
139 * the rate of the previous frame is used.
140 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
142 rate = -1;
143 /* use lowest available if everything fails */
144 mrate = sband->bitrates[0].bitrate;
145 for (i = 0; i < sband->n_bitrates; i++) {
146 struct ieee80211_rate *r = &sband->bitrates[i];
148 if (r->bitrate > txrate->bitrate)
149 break;
151 if ((rate_flags & r->flags) != rate_flags)
152 continue;
154 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
155 rate = DIV_ROUND_UP(r->bitrate, 1 << shift);
157 switch (sband->band) {
158 case NL80211_BAND_2GHZ: {
159 u32 flag;
160 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
161 flag = IEEE80211_RATE_MANDATORY_G;
162 else
163 flag = IEEE80211_RATE_MANDATORY_B;
164 if (r->flags & flag)
165 mrate = r->bitrate;
166 break;
168 case NL80211_BAND_5GHZ:
169 if (r->flags & IEEE80211_RATE_MANDATORY_A)
170 mrate = r->bitrate;
171 break;
172 case NL80211_BAND_60GHZ:
173 /* TODO, for now fall through */
174 case NUM_NL80211_BANDS:
175 WARN_ON(1);
176 break;
179 if (rate == -1) {
180 /* No matching basic rate found; use highest suitable mandatory
181 * PHY rate */
182 rate = DIV_ROUND_UP(mrate, 1 << shift);
185 /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
186 if (ieee80211_is_data_qos(hdr->frame_control) &&
187 *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
188 dur = 0;
189 else
190 /* Time needed to transmit ACK
191 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
192 * to closest integer */
193 dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
194 tx->sdata->vif.bss_conf.use_short_preamble,
195 shift);
197 if (next_frag_len) {
198 /* Frame is fragmented: duration increases with time needed to
199 * transmit next fragment plus ACK and 2 x SIFS. */
200 dur *= 2; /* ACK + SIFS */
201 /* next fragment */
202 dur += ieee80211_frame_duration(sband->band, next_frag_len,
203 txrate->bitrate, erp,
204 tx->sdata->vif.bss_conf.use_short_preamble,
205 shift);
208 return cpu_to_le16(dur);
211 /* tx handlers */
212 static ieee80211_tx_result debug_noinline
213 ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
215 struct ieee80211_local *local = tx->local;
216 struct ieee80211_if_managed *ifmgd;
217 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
219 /* driver doesn't support power save */
220 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
221 return TX_CONTINUE;
223 /* hardware does dynamic power save */
224 if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
225 return TX_CONTINUE;
227 /* dynamic power save disabled */
228 if (local->hw.conf.dynamic_ps_timeout <= 0)
229 return TX_CONTINUE;
231 /* we are scanning, don't enable power save */
232 if (local->scanning)
233 return TX_CONTINUE;
235 if (!local->ps_sdata)
236 return TX_CONTINUE;
238 /* No point if we're going to suspend */
239 if (local->quiescing)
240 return TX_CONTINUE;
242 /* dynamic ps is supported only in managed mode */
243 if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
244 return TX_CONTINUE;
246 if (unlikely(info->flags & IEEE80211_TX_INTFL_OFFCHAN_TX_OK))
247 return TX_CONTINUE;
249 ifmgd = &tx->sdata->u.mgd;
252 * Don't wakeup from power save if u-apsd is enabled, voip ac has
253 * u-apsd enabled and the frame is in voip class. This effectively
254 * means that even if all access categories have u-apsd enabled, in
255 * practise u-apsd is only used with the voip ac. This is a
256 * workaround for the case when received voip class packets do not
257 * have correct qos tag for some reason, due the network or the
258 * peer application.
260 * Note: ifmgd->uapsd_queues access is racy here. If the value is
261 * changed via debugfs, user needs to reassociate manually to have
262 * everything in sync.
264 if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
265 (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
266 skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
267 return TX_CONTINUE;
269 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
270 ieee80211_stop_queues_by_reason(&local->hw,
271 IEEE80211_MAX_QUEUE_MAP,
272 IEEE80211_QUEUE_STOP_REASON_PS,
273 false);
274 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
275 ieee80211_queue_work(&local->hw,
276 &local->dynamic_ps_disable_work);
279 /* Don't restart the timer if we're not disassociated */
280 if (!ifmgd->associated)
281 return TX_CONTINUE;
283 mod_timer(&local->dynamic_ps_timer, jiffies +
284 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
286 return TX_CONTINUE;
289 static ieee80211_tx_result debug_noinline
290 ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
293 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
294 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
295 bool assoc = false;
297 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
298 return TX_CONTINUE;
300 if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
301 test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
302 !ieee80211_is_probe_req(hdr->frame_control) &&
303 !ieee80211_is_any_nullfunc(hdr->frame_control))
305 * When software scanning only nullfunc frames (to notify
306 * the sleep state to the AP) and probe requests (for the
307 * active scan) are allowed, all other frames should not be
308 * sent and we should not get here, but if we do
309 * nonetheless, drop them to avoid sending them
310 * off-channel. See the link below and
311 * ieee80211_start_scan() for more.
313 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
315 return TX_DROP;
317 if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
318 return TX_CONTINUE;
320 if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
321 return TX_CONTINUE;
323 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
324 return TX_CONTINUE;
326 if (tx->sta)
327 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
329 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
330 if (unlikely(!assoc &&
331 ieee80211_is_data(hdr->frame_control))) {
332 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
333 sdata_info(tx->sdata,
334 "dropped data frame to not associated station %pM\n",
335 hdr->addr1);
336 #endif
337 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
338 return TX_DROP;
340 } else if (unlikely(ieee80211_is_data(hdr->frame_control) &&
341 ieee80211_vif_get_num_mcast_if(tx->sdata) == 0)) {
343 * No associated STAs - no need to send multicast
344 * frames.
346 return TX_DROP;
349 return TX_CONTINUE;
352 /* This function is called whenever the AP is about to exceed the maximum limit
353 * of buffered frames for power saving STAs. This situation should not really
354 * happen often during normal operation, so dropping the oldest buffered packet
355 * from each queue should be OK to make some room for new frames. */
356 static void purge_old_ps_buffers(struct ieee80211_local *local)
358 int total = 0, purged = 0;
359 struct sk_buff *skb;
360 struct ieee80211_sub_if_data *sdata;
361 struct sta_info *sta;
363 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
364 struct ps_data *ps;
366 if (sdata->vif.type == NL80211_IFTYPE_AP)
367 ps = &sdata->u.ap.ps;
368 else if (ieee80211_vif_is_mesh(&sdata->vif))
369 ps = &sdata->u.mesh.ps;
370 else
371 continue;
373 skb = skb_dequeue(&ps->bc_buf);
374 if (skb) {
375 purged++;
376 ieee80211_free_txskb(&local->hw, skb);
378 total += skb_queue_len(&ps->bc_buf);
382 * Drop one frame from each station from the lowest-priority
383 * AC that has frames at all.
385 list_for_each_entry_rcu(sta, &local->sta_list, list) {
386 int ac;
388 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
389 skb = skb_dequeue(&sta->ps_tx_buf[ac]);
390 total += skb_queue_len(&sta->ps_tx_buf[ac]);
391 if (skb) {
392 purged++;
393 ieee80211_free_txskb(&local->hw, skb);
394 break;
399 local->total_ps_buffered = total;
400 ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
403 static ieee80211_tx_result
404 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
406 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
407 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
408 struct ps_data *ps;
411 * broadcast/multicast frame
413 * If any of the associated/peer stations is in power save mode,
414 * the frame is buffered to be sent after DTIM beacon frame.
415 * This is done either by the hardware or us.
418 /* powersaving STAs currently only in AP/VLAN/mesh mode */
419 if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
420 tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
421 if (!tx->sdata->bss)
422 return TX_CONTINUE;
424 ps = &tx->sdata->bss->ps;
425 } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
426 ps = &tx->sdata->u.mesh.ps;
427 } else {
428 return TX_CONTINUE;
432 /* no buffering for ordered frames */
433 if (ieee80211_has_order(hdr->frame_control))
434 return TX_CONTINUE;
436 if (ieee80211_is_probe_req(hdr->frame_control))
437 return TX_CONTINUE;
439 if (ieee80211_hw_check(&tx->local->hw, QUEUE_CONTROL))
440 info->hw_queue = tx->sdata->vif.cab_queue;
442 /* no stations in PS mode and no buffered packets */
443 if (!atomic_read(&ps->num_sta_ps) && skb_queue_empty(&ps->bc_buf))
444 return TX_CONTINUE;
446 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
448 /* device releases frame after DTIM beacon */
449 if (!ieee80211_hw_check(&tx->local->hw, HOST_BROADCAST_PS_BUFFERING))
450 return TX_CONTINUE;
452 /* buffered in mac80211 */
453 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
454 purge_old_ps_buffers(tx->local);
456 if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
457 ps_dbg(tx->sdata,
458 "BC TX buffer full - dropping the oldest frame\n");
459 ieee80211_free_txskb(&tx->local->hw, skb_dequeue(&ps->bc_buf));
460 } else
461 tx->local->total_ps_buffered++;
463 skb_queue_tail(&ps->bc_buf, tx->skb);
465 return TX_QUEUED;
468 static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
469 struct sk_buff *skb)
471 if (!ieee80211_is_mgmt(fc))
472 return 0;
474 if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
475 return 0;
477 if (!ieee80211_is_robust_mgmt_frame(skb))
478 return 0;
480 return 1;
483 static ieee80211_tx_result
484 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
486 struct sta_info *sta = tx->sta;
487 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
488 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
489 struct ieee80211_local *local = tx->local;
491 if (unlikely(!sta))
492 return TX_CONTINUE;
494 if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
495 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
496 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
497 !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
498 int ac = skb_get_queue_mapping(tx->skb);
500 if (ieee80211_is_mgmt(hdr->frame_control) &&
501 !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
502 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
503 return TX_CONTINUE;
506 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
507 sta->sta.addr, sta->sta.aid, ac);
508 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
509 purge_old_ps_buffers(tx->local);
511 /* sync with ieee80211_sta_ps_deliver_wakeup */
512 spin_lock(&sta->ps_lock);
514 * STA woke up the meantime and all the frames on ps_tx_buf have
515 * been queued to pending queue. No reordering can happen, go
516 * ahead and Tx the packet.
518 if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
519 !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
520 !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
521 spin_unlock(&sta->ps_lock);
522 return TX_CONTINUE;
525 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
526 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
527 ps_dbg(tx->sdata,
528 "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
529 sta->sta.addr, ac);
530 ieee80211_free_txskb(&local->hw, old);
531 } else
532 tx->local->total_ps_buffered++;
534 info->control.jiffies = jiffies;
535 info->control.vif = &tx->sdata->vif;
536 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
537 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
538 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
539 spin_unlock(&sta->ps_lock);
541 if (!timer_pending(&local->sta_cleanup))
542 mod_timer(&local->sta_cleanup,
543 round_jiffies(jiffies +
544 STA_INFO_CLEANUP_INTERVAL));
547 * We queued up some frames, so the TIM bit might
548 * need to be set, recalculate it.
550 sta_info_recalc_tim(sta);
552 return TX_QUEUED;
553 } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
554 ps_dbg(tx->sdata,
555 "STA %pM in PS mode, but polling/in SP -> send frame\n",
556 sta->sta.addr);
559 return TX_CONTINUE;
562 static ieee80211_tx_result debug_noinline
563 ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
565 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
566 return TX_CONTINUE;
568 if (tx->flags & IEEE80211_TX_UNICAST)
569 return ieee80211_tx_h_unicast_ps_buf(tx);
570 else
571 return ieee80211_tx_h_multicast_ps_buf(tx);
574 static ieee80211_tx_result debug_noinline
575 ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
577 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
579 if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
580 if (tx->sdata->control_port_no_encrypt)
581 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
582 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
583 info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
586 return TX_CONTINUE;
589 static ieee80211_tx_result debug_noinline
590 ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
592 struct ieee80211_key *key;
593 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
594 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
596 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
597 tx->key = NULL;
598 else if (tx->sta &&
599 (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
600 tx->key = key;
601 else if (ieee80211_is_group_privacy_action(tx->skb) &&
602 (key = rcu_dereference(tx->sdata->default_multicast_key)))
603 tx->key = key;
604 else if (ieee80211_is_mgmt(hdr->frame_control) &&
605 is_multicast_ether_addr(hdr->addr1) &&
606 ieee80211_is_robust_mgmt_frame(tx->skb) &&
607 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
608 tx->key = key;
609 else if (is_multicast_ether_addr(hdr->addr1) &&
610 (key = rcu_dereference(tx->sdata->default_multicast_key)))
611 tx->key = key;
612 else if (!is_multicast_ether_addr(hdr->addr1) &&
613 (key = rcu_dereference(tx->sdata->default_unicast_key)))
614 tx->key = key;
615 else
616 tx->key = NULL;
618 if (tx->key) {
619 bool skip_hw = false;
621 /* TODO: add threshold stuff again */
623 switch (tx->key->conf.cipher) {
624 case WLAN_CIPHER_SUITE_WEP40:
625 case WLAN_CIPHER_SUITE_WEP104:
626 case WLAN_CIPHER_SUITE_TKIP:
627 if (!ieee80211_is_data_present(hdr->frame_control))
628 tx->key = NULL;
629 break;
630 case WLAN_CIPHER_SUITE_CCMP:
631 case WLAN_CIPHER_SUITE_CCMP_256:
632 case WLAN_CIPHER_SUITE_GCMP:
633 case WLAN_CIPHER_SUITE_GCMP_256:
634 if (!ieee80211_is_data_present(hdr->frame_control) &&
635 !ieee80211_use_mfp(hdr->frame_control, tx->sta,
636 tx->skb) &&
637 !ieee80211_is_group_privacy_action(tx->skb))
638 tx->key = NULL;
639 else
640 skip_hw = (tx->key->conf.flags &
641 IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
642 ieee80211_is_mgmt(hdr->frame_control);
643 break;
644 case WLAN_CIPHER_SUITE_AES_CMAC:
645 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
646 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
647 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
648 if (!ieee80211_is_mgmt(hdr->frame_control))
649 tx->key = NULL;
650 break;
653 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
654 !ieee80211_is_deauth(hdr->frame_control)))
655 return TX_DROP;
657 if (!skip_hw && tx->key &&
658 tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
659 info->control.hw_key = &tx->key->conf;
662 return TX_CONTINUE;
665 static ieee80211_tx_result debug_noinline
666 ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
668 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
669 struct ieee80211_hdr *hdr = (void *)tx->skb->data;
670 struct ieee80211_supported_band *sband;
671 u32 len;
672 struct ieee80211_tx_rate_control txrc;
673 struct ieee80211_sta_rates *ratetbl = NULL;
674 bool assoc = false;
676 memset(&txrc, 0, sizeof(txrc));
678 sband = tx->local->hw.wiphy->bands[info->band];
680 len = min_t(u32, tx->skb->len + FCS_LEN,
681 tx->local->hw.wiphy->frag_threshold);
683 /* set up the tx rate control struct we give the RC algo */
684 txrc.hw = &tx->local->hw;
685 txrc.sband = sband;
686 txrc.bss_conf = &tx->sdata->vif.bss_conf;
687 txrc.skb = tx->skb;
688 txrc.reported_rate.idx = -1;
689 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
691 if (tx->sdata->rc_has_mcs_mask[info->band])
692 txrc.rate_idx_mcs_mask =
693 tx->sdata->rc_rateidx_mcs_mask[info->band];
695 txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
696 tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
697 tx->sdata->vif.type == NL80211_IFTYPE_ADHOC ||
698 tx->sdata->vif.type == NL80211_IFTYPE_OCB);
700 /* set up RTS protection if desired */
701 if (len > tx->local->hw.wiphy->rts_threshold) {
702 txrc.rts = true;
705 info->control.use_rts = txrc.rts;
706 info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
709 * Use short preamble if the BSS can handle it, but not for
710 * management frames unless we know the receiver can handle
711 * that -- the management frame might be to a station that
712 * just wants a probe response.
714 if (tx->sdata->vif.bss_conf.use_short_preamble &&
715 (ieee80211_is_data(hdr->frame_control) ||
716 (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
717 txrc.short_preamble = true;
719 info->control.short_preamble = txrc.short_preamble;
721 /* don't ask rate control when rate already injected via radiotap */
722 if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)
723 return TX_CONTINUE;
725 if (tx->sta)
726 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
729 * Lets not bother rate control if we're associated and cannot
730 * talk to the sta. This should not happen.
732 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
733 !rate_usable_index_exists(sband, &tx->sta->sta),
734 "%s: Dropped data frame as no usable bitrate found while "
735 "scanning and associated. Target station: "
736 "%pM on %d GHz band\n",
737 tx->sdata->name, hdr->addr1,
738 info->band ? 5 : 2))
739 return TX_DROP;
742 * If we're associated with the sta at this point we know we can at
743 * least send the frame at the lowest bit rate.
745 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
747 if (tx->sta && !info->control.skip_table)
748 ratetbl = rcu_dereference(tx->sta->sta.rates);
750 if (unlikely(info->control.rates[0].idx < 0)) {
751 if (ratetbl) {
752 struct ieee80211_tx_rate rate = {
753 .idx = ratetbl->rate[0].idx,
754 .flags = ratetbl->rate[0].flags,
755 .count = ratetbl->rate[0].count
758 if (ratetbl->rate[0].idx < 0)
759 return TX_DROP;
761 tx->rate = rate;
762 } else {
763 return TX_DROP;
765 } else {
766 tx->rate = info->control.rates[0];
769 if (txrc.reported_rate.idx < 0) {
770 txrc.reported_rate = tx->rate;
771 if (tx->sta && ieee80211_is_data(hdr->frame_control))
772 tx->sta->tx_stats.last_rate = txrc.reported_rate;
773 } else if (tx->sta)
774 tx->sta->tx_stats.last_rate = txrc.reported_rate;
776 if (ratetbl)
777 return TX_CONTINUE;
779 if (unlikely(!info->control.rates[0].count))
780 info->control.rates[0].count = 1;
782 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
783 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
784 info->control.rates[0].count = 1;
786 return TX_CONTINUE;
789 static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
791 u16 *seq = &sta->tid_seq[tid];
792 __le16 ret = cpu_to_le16(*seq);
794 /* Increase the sequence number. */
795 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
797 return ret;
800 static ieee80211_tx_result debug_noinline
801 ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
803 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
804 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
805 int tid;
808 * Packet injection may want to control the sequence
809 * number, if we have no matching interface then we
810 * neither assign one ourselves nor ask the driver to.
812 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
813 return TX_CONTINUE;
815 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
816 return TX_CONTINUE;
818 if (ieee80211_hdrlen(hdr->frame_control) < 24)
819 return TX_CONTINUE;
821 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
822 return TX_CONTINUE;
825 * Anything but QoS data that has a sequence number field
826 * (is long enough) gets a sequence number from the global
827 * counter. QoS data frames with a multicast destination
828 * also use the global counter (802.11-2012 9.3.2.10).
830 if (!ieee80211_is_data_qos(hdr->frame_control) ||
831 is_multicast_ether_addr(hdr->addr1)) {
832 if (tx->flags & IEEE80211_TX_NO_SEQNO)
833 return TX_CONTINUE;
834 /* driver should assign sequence number */
835 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
836 /* for pure STA mode without beacons, we can do it */
837 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
838 tx->sdata->sequence_number += 0x10;
839 if (tx->sta)
840 tx->sta->tx_stats.msdu[IEEE80211_NUM_TIDS]++;
841 return TX_CONTINUE;
845 * This should be true for injected/management frames only, for
846 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
847 * above since they are not QoS-data frames.
849 if (!tx->sta)
850 return TX_CONTINUE;
852 /* include per-STA, per-TID sequence counter */
853 tid = ieee80211_get_tid(hdr);
854 tx->sta->tx_stats.msdu[tid]++;
856 hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
858 return TX_CONTINUE;
861 static int ieee80211_fragment(struct ieee80211_tx_data *tx,
862 struct sk_buff *skb, int hdrlen,
863 int frag_threshold)
865 struct ieee80211_local *local = tx->local;
866 struct ieee80211_tx_info *info;
867 struct sk_buff *tmp;
868 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
869 int pos = hdrlen + per_fragm;
870 int rem = skb->len - hdrlen - per_fragm;
872 if (WARN_ON(rem < 0))
873 return -EINVAL;
875 /* first fragment was already added to queue by caller */
877 while (rem) {
878 int fraglen = per_fragm;
880 if (fraglen > rem)
881 fraglen = rem;
882 rem -= fraglen;
883 tmp = dev_alloc_skb(local->tx_headroom +
884 frag_threshold +
885 tx->sdata->encrypt_headroom +
886 IEEE80211_ENCRYPT_TAILROOM);
887 if (!tmp)
888 return -ENOMEM;
890 __skb_queue_tail(&tx->skbs, tmp);
892 skb_reserve(tmp,
893 local->tx_headroom + tx->sdata->encrypt_headroom);
895 /* copy control information */
896 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
898 info = IEEE80211_SKB_CB(tmp);
899 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
900 IEEE80211_TX_CTL_FIRST_FRAGMENT);
902 if (rem)
903 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
905 skb_copy_queue_mapping(tmp, skb);
906 tmp->priority = skb->priority;
907 tmp->dev = skb->dev;
909 /* copy header and data */
910 skb_put_data(tmp, skb->data, hdrlen);
911 skb_put_data(tmp, skb->data + pos, fraglen);
913 pos += fraglen;
916 /* adjust first fragment's length */
917 skb_trim(skb, hdrlen + per_fragm);
918 return 0;
921 static ieee80211_tx_result debug_noinline
922 ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
924 struct sk_buff *skb = tx->skb;
925 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
926 struct ieee80211_hdr *hdr = (void *)skb->data;
927 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
928 int hdrlen;
929 int fragnum;
931 /* no matter what happens, tx->skb moves to tx->skbs */
932 __skb_queue_tail(&tx->skbs, skb);
933 tx->skb = NULL;
935 if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
936 return TX_CONTINUE;
938 if (ieee80211_hw_check(&tx->local->hw, SUPPORTS_TX_FRAG))
939 return TX_CONTINUE;
942 * Warn when submitting a fragmented A-MPDU frame and drop it.
943 * This scenario is handled in ieee80211_tx_prepare but extra
944 * caution taken here as fragmented ampdu may cause Tx stop.
946 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
947 return TX_DROP;
949 hdrlen = ieee80211_hdrlen(hdr->frame_control);
951 /* internal error, why isn't DONTFRAG set? */
952 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
953 return TX_DROP;
956 * Now fragment the frame. This will allocate all the fragments and
957 * chain them (using skb as the first fragment) to skb->next.
958 * During transmission, we will remove the successfully transmitted
959 * fragments from this list. When the low-level driver rejects one
960 * of the fragments then we will simply pretend to accept the skb
961 * but store it away as pending.
963 if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
964 return TX_DROP;
966 /* update duration/seq/flags of fragments */
967 fragnum = 0;
969 skb_queue_walk(&tx->skbs, skb) {
970 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
972 hdr = (void *)skb->data;
973 info = IEEE80211_SKB_CB(skb);
975 if (!skb_queue_is_last(&tx->skbs, skb)) {
976 hdr->frame_control |= morefrags;
978 * No multi-rate retries for fragmented frames, that
979 * would completely throw off the NAV at other STAs.
981 info->control.rates[1].idx = -1;
982 info->control.rates[2].idx = -1;
983 info->control.rates[3].idx = -1;
984 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
985 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
986 } else {
987 hdr->frame_control &= ~morefrags;
989 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
990 fragnum++;
993 return TX_CONTINUE;
996 static ieee80211_tx_result debug_noinline
997 ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
999 struct sk_buff *skb;
1000 int ac = -1;
1002 if (!tx->sta)
1003 return TX_CONTINUE;
1005 skb_queue_walk(&tx->skbs, skb) {
1006 ac = skb_get_queue_mapping(skb);
1007 tx->sta->tx_stats.bytes[ac] += skb->len;
1009 if (ac >= 0)
1010 tx->sta->tx_stats.packets[ac]++;
1012 return TX_CONTINUE;
1015 static ieee80211_tx_result debug_noinline
1016 ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
1018 if (!tx->key)
1019 return TX_CONTINUE;
1021 switch (tx->key->conf.cipher) {
1022 case WLAN_CIPHER_SUITE_WEP40:
1023 case WLAN_CIPHER_SUITE_WEP104:
1024 return ieee80211_crypto_wep_encrypt(tx);
1025 case WLAN_CIPHER_SUITE_TKIP:
1026 return ieee80211_crypto_tkip_encrypt(tx);
1027 case WLAN_CIPHER_SUITE_CCMP:
1028 return ieee80211_crypto_ccmp_encrypt(
1029 tx, IEEE80211_CCMP_MIC_LEN);
1030 case WLAN_CIPHER_SUITE_CCMP_256:
1031 return ieee80211_crypto_ccmp_encrypt(
1032 tx, IEEE80211_CCMP_256_MIC_LEN);
1033 case WLAN_CIPHER_SUITE_AES_CMAC:
1034 return ieee80211_crypto_aes_cmac_encrypt(tx);
1035 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1036 return ieee80211_crypto_aes_cmac_256_encrypt(tx);
1037 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1038 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1039 return ieee80211_crypto_aes_gmac_encrypt(tx);
1040 case WLAN_CIPHER_SUITE_GCMP:
1041 case WLAN_CIPHER_SUITE_GCMP_256:
1042 return ieee80211_crypto_gcmp_encrypt(tx);
1043 default:
1044 return ieee80211_crypto_hw_encrypt(tx);
1047 return TX_DROP;
1050 static ieee80211_tx_result debug_noinline
1051 ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1053 struct sk_buff *skb;
1054 struct ieee80211_hdr *hdr;
1055 int next_len;
1056 bool group_addr;
1058 skb_queue_walk(&tx->skbs, skb) {
1059 hdr = (void *) skb->data;
1060 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1061 break; /* must not overwrite AID */
1062 if (!skb_queue_is_last(&tx->skbs, skb)) {
1063 struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1064 next_len = next->len;
1065 } else
1066 next_len = 0;
1067 group_addr = is_multicast_ether_addr(hdr->addr1);
1069 hdr->duration_id =
1070 ieee80211_duration(tx, skb, group_addr, next_len);
1073 return TX_CONTINUE;
1076 /* actual transmit path */
1078 static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1079 struct sk_buff *skb,
1080 struct ieee80211_tx_info *info,
1081 struct tid_ampdu_tx *tid_tx,
1082 int tid)
1084 bool queued = false;
1085 bool reset_agg_timer = false;
1086 struct sk_buff *purge_skb = NULL;
1088 if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1089 info->flags |= IEEE80211_TX_CTL_AMPDU;
1090 reset_agg_timer = true;
1091 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1093 * nothing -- this aggregation session is being started
1094 * but that might still fail with the driver
1096 } else if (!tx->sta->sta.txq[tid]) {
1097 spin_lock(&tx->sta->lock);
1099 * Need to re-check now, because we may get here
1101 * 1) in the window during which the setup is actually
1102 * already done, but not marked yet because not all
1103 * packets are spliced over to the driver pending
1104 * queue yet -- if this happened we acquire the lock
1105 * either before or after the splice happens, but
1106 * need to recheck which of these cases happened.
1108 * 2) during session teardown, if the OPERATIONAL bit
1109 * was cleared due to the teardown but the pointer
1110 * hasn't been assigned NULL yet (or we loaded it
1111 * before it was assigned) -- in this case it may
1112 * now be NULL which means we should just let the
1113 * packet pass through because splicing the frames
1114 * back is already done.
1116 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
1118 if (!tid_tx) {
1119 /* do nothing, let packet pass through */
1120 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1121 info->flags |= IEEE80211_TX_CTL_AMPDU;
1122 reset_agg_timer = true;
1123 } else {
1124 queued = true;
1125 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
1126 clear_sta_flag(tx->sta, WLAN_STA_SP);
1127 ps_dbg(tx->sta->sdata,
1128 "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
1129 tx->sta->sta.addr, tx->sta->sta.aid);
1131 info->control.vif = &tx->sdata->vif;
1132 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1133 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
1134 __skb_queue_tail(&tid_tx->pending, skb);
1135 if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1136 purge_skb = __skb_dequeue(&tid_tx->pending);
1138 spin_unlock(&tx->sta->lock);
1140 if (purge_skb)
1141 ieee80211_free_txskb(&tx->local->hw, purge_skb);
1144 /* reset session timer */
1145 if (reset_agg_timer)
1146 tid_tx->last_tx = jiffies;
1148 return queued;
1152 * initialises @tx
1153 * pass %NULL for the station if unknown, a valid pointer if known
1154 * or an ERR_PTR() if the station is known not to exist
1156 static ieee80211_tx_result
1157 ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1158 struct ieee80211_tx_data *tx,
1159 struct sta_info *sta, struct sk_buff *skb)
1161 struct ieee80211_local *local = sdata->local;
1162 struct ieee80211_hdr *hdr;
1163 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1164 int tid;
1166 memset(tx, 0, sizeof(*tx));
1167 tx->skb = skb;
1168 tx->local = local;
1169 tx->sdata = sdata;
1170 __skb_queue_head_init(&tx->skbs);
1173 * If this flag is set to true anywhere, and we get here,
1174 * we are doing the needed processing, so remove the flag
1175 * now.
1177 info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1179 hdr = (struct ieee80211_hdr *) skb->data;
1181 if (likely(sta)) {
1182 if (!IS_ERR(sta))
1183 tx->sta = sta;
1184 } else {
1185 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1186 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1187 if (!tx->sta && sdata->wdev.use_4addr)
1188 return TX_DROP;
1189 } else if (info->flags & (IEEE80211_TX_INTFL_NL80211_FRAME_TX |
1190 IEEE80211_TX_CTL_INJECTED) ||
1191 tx->sdata->control_port_protocol == tx->skb->protocol) {
1192 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1194 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
1195 tx->sta = sta_info_get(sdata, hdr->addr1);
1198 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
1199 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
1200 ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
1201 !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
1202 struct tid_ampdu_tx *tid_tx;
1204 tid = ieee80211_get_tid(hdr);
1206 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1207 if (tid_tx) {
1208 bool queued;
1210 queued = ieee80211_tx_prep_agg(tx, skb, info,
1211 tid_tx, tid);
1213 if (unlikely(queued))
1214 return TX_QUEUED;
1218 if (is_multicast_ether_addr(hdr->addr1)) {
1219 tx->flags &= ~IEEE80211_TX_UNICAST;
1220 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1221 } else
1222 tx->flags |= IEEE80211_TX_UNICAST;
1224 if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1225 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1226 skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1227 info->flags & IEEE80211_TX_CTL_AMPDU)
1228 info->flags |= IEEE80211_TX_CTL_DONTFRAG;
1231 if (!tx->sta)
1232 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1233 else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
1234 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1235 ieee80211_check_fast_xmit(tx->sta);
1238 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
1240 return TX_CONTINUE;
1243 static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
1244 struct ieee80211_vif *vif,
1245 struct sta_info *sta,
1246 struct sk_buff *skb)
1248 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1249 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1250 struct ieee80211_txq *txq = NULL;
1252 if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
1253 (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
1254 return NULL;
1256 if (!ieee80211_is_data_present(hdr->frame_control))
1257 return NULL;
1259 if (sta) {
1260 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1262 if (!sta->uploaded)
1263 return NULL;
1265 txq = sta->sta.txq[tid];
1266 } else if (vif) {
1267 txq = vif->txq;
1270 if (!txq)
1271 return NULL;
1273 return to_txq_info(txq);
1276 static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
1278 IEEE80211_SKB_CB(skb)->control.enqueue_time = codel_get_time();
1281 static u32 codel_skb_len_func(const struct sk_buff *skb)
1283 return skb->len;
1286 static codel_time_t codel_skb_time_func(const struct sk_buff *skb)
1288 const struct ieee80211_tx_info *info;
1290 info = (const struct ieee80211_tx_info *)skb->cb;
1291 return info->control.enqueue_time;
1294 static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars,
1295 void *ctx)
1297 struct ieee80211_local *local;
1298 struct txq_info *txqi;
1299 struct fq *fq;
1300 struct fq_flow *flow;
1302 txqi = ctx;
1303 local = vif_to_sdata(txqi->txq.vif)->local;
1304 fq = &local->fq;
1306 if (cvars == &txqi->def_cvars)
1307 flow = &txqi->def_flow;
1308 else
1309 flow = &fq->flows[cvars - local->cvars];
1311 return fq_flow_dequeue(fq, flow);
1314 static void codel_drop_func(struct sk_buff *skb,
1315 void *ctx)
1317 struct ieee80211_local *local;
1318 struct ieee80211_hw *hw;
1319 struct txq_info *txqi;
1321 txqi = ctx;
1322 local = vif_to_sdata(txqi->txq.vif)->local;
1323 hw = &local->hw;
1325 ieee80211_free_txskb(hw, skb);
1328 static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
1329 struct fq_tin *tin,
1330 struct fq_flow *flow)
1332 struct ieee80211_local *local;
1333 struct txq_info *txqi;
1334 struct codel_vars *cvars;
1335 struct codel_params *cparams;
1336 struct codel_stats *cstats;
1338 local = container_of(fq, struct ieee80211_local, fq);
1339 txqi = container_of(tin, struct txq_info, tin);
1340 cstats = &txqi->cstats;
1342 if (txqi->txq.sta) {
1343 struct sta_info *sta = container_of(txqi->txq.sta,
1344 struct sta_info, sta);
1345 cparams = &sta->cparams;
1346 } else {
1347 cparams = &local->cparams;
1350 if (flow == &txqi->def_flow)
1351 cvars = &txqi->def_cvars;
1352 else
1353 cvars = &local->cvars[flow - fq->flows];
1355 return codel_dequeue(txqi,
1356 &flow->backlog,
1357 cparams,
1358 cvars,
1359 cstats,
1360 codel_skb_len_func,
1361 codel_skb_time_func,
1362 codel_drop_func,
1363 codel_dequeue_func);
1366 static void fq_skb_free_func(struct fq *fq,
1367 struct fq_tin *tin,
1368 struct fq_flow *flow,
1369 struct sk_buff *skb)
1371 struct ieee80211_local *local;
1373 local = container_of(fq, struct ieee80211_local, fq);
1374 ieee80211_free_txskb(&local->hw, skb);
1377 static struct fq_flow *fq_flow_get_default_func(struct fq *fq,
1378 struct fq_tin *tin,
1379 int idx,
1380 struct sk_buff *skb)
1382 struct txq_info *txqi;
1384 txqi = container_of(tin, struct txq_info, tin);
1385 return &txqi->def_flow;
1388 static void ieee80211_txq_enqueue(struct ieee80211_local *local,
1389 struct txq_info *txqi,
1390 struct sk_buff *skb)
1392 struct fq *fq = &local->fq;
1393 struct fq_tin *tin = &txqi->tin;
1395 ieee80211_set_skb_enqueue_time(skb);
1396 fq_tin_enqueue(fq, tin, skb,
1397 fq_skb_free_func,
1398 fq_flow_get_default_func);
1401 static bool fq_vlan_filter_func(struct fq *fq, struct fq_tin *tin,
1402 struct fq_flow *flow, struct sk_buff *skb,
1403 void *data)
1405 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1407 return info->control.vif == data;
1410 void ieee80211_txq_remove_vlan(struct ieee80211_local *local,
1411 struct ieee80211_sub_if_data *sdata)
1413 struct fq *fq = &local->fq;
1414 struct txq_info *txqi;
1415 struct fq_tin *tin;
1416 struct ieee80211_sub_if_data *ap;
1418 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1419 return;
1421 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1423 if (!ap->vif.txq)
1424 return;
1426 txqi = to_txq_info(ap->vif.txq);
1427 tin = &txqi->tin;
1429 spin_lock_bh(&fq->lock);
1430 fq_tin_filter(fq, tin, fq_vlan_filter_func, &sdata->vif,
1431 fq_skb_free_func);
1432 spin_unlock_bh(&fq->lock);
1435 void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
1436 struct sta_info *sta,
1437 struct txq_info *txqi, int tid)
1439 fq_tin_init(&txqi->tin);
1440 fq_flow_init(&txqi->def_flow);
1441 codel_vars_init(&txqi->def_cvars);
1442 codel_stats_init(&txqi->cstats);
1443 __skb_queue_head_init(&txqi->frags);
1445 txqi->txq.vif = &sdata->vif;
1447 if (sta) {
1448 txqi->txq.sta = &sta->sta;
1449 sta->sta.txq[tid] = &txqi->txq;
1450 txqi->txq.tid = tid;
1451 txqi->txq.ac = ieee80211_ac_from_tid(tid);
1452 } else {
1453 sdata->vif.txq = &txqi->txq;
1454 txqi->txq.tid = 0;
1455 txqi->txq.ac = IEEE80211_AC_BE;
1459 void ieee80211_txq_purge(struct ieee80211_local *local,
1460 struct txq_info *txqi)
1462 struct fq *fq = &local->fq;
1463 struct fq_tin *tin = &txqi->tin;
1465 fq_tin_reset(fq, tin, fq_skb_free_func);
1466 ieee80211_purge_tx_queue(&local->hw, &txqi->frags);
1469 void ieee80211_txq_set_params(struct ieee80211_local *local)
1471 if (local->hw.wiphy->txq_limit)
1472 local->fq.limit = local->hw.wiphy->txq_limit;
1473 else
1474 local->hw.wiphy->txq_limit = local->fq.limit;
1476 if (local->hw.wiphy->txq_memory_limit)
1477 local->fq.memory_limit = local->hw.wiphy->txq_memory_limit;
1478 else
1479 local->hw.wiphy->txq_memory_limit = local->fq.memory_limit;
1481 if (local->hw.wiphy->txq_quantum)
1482 local->fq.quantum = local->hw.wiphy->txq_quantum;
1483 else
1484 local->hw.wiphy->txq_quantum = local->fq.quantum;
1487 int ieee80211_txq_setup_flows(struct ieee80211_local *local)
1489 struct fq *fq = &local->fq;
1490 int ret;
1491 int i;
1492 bool supp_vht = false;
1493 enum nl80211_band band;
1495 if (!local->ops->wake_tx_queue)
1496 return 0;
1498 ret = fq_init(fq, 4096);
1499 if (ret)
1500 return ret;
1503 * If the hardware doesn't support VHT, it is safe to limit the maximum
1504 * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
1506 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1507 struct ieee80211_supported_band *sband;
1509 sband = local->hw.wiphy->bands[band];
1510 if (!sband)
1511 continue;
1513 supp_vht = supp_vht || sband->vht_cap.vht_supported;
1516 if (!supp_vht)
1517 fq->memory_limit = 4 << 20; /* 4 Mbytes */
1519 codel_params_init(&local->cparams);
1520 local->cparams.interval = MS2TIME(100);
1521 local->cparams.target = MS2TIME(20);
1522 local->cparams.ecn = true;
1524 local->cvars = kcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
1525 GFP_KERNEL);
1526 if (!local->cvars) {
1527 spin_lock_bh(&fq->lock);
1528 fq_reset(fq, fq_skb_free_func);
1529 spin_unlock_bh(&fq->lock);
1530 return -ENOMEM;
1533 for (i = 0; i < fq->flows_cnt; i++)
1534 codel_vars_init(&local->cvars[i]);
1536 ieee80211_txq_set_params(local);
1538 return 0;
1541 void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1543 struct fq *fq = &local->fq;
1545 if (!local->ops->wake_tx_queue)
1546 return;
1548 kfree(local->cvars);
1549 local->cvars = NULL;
1551 spin_lock_bh(&fq->lock);
1552 fq_reset(fq, fq_skb_free_func);
1553 spin_unlock_bh(&fq->lock);
1556 static bool ieee80211_queue_skb(struct ieee80211_local *local,
1557 struct ieee80211_sub_if_data *sdata,
1558 struct sta_info *sta,
1559 struct sk_buff *skb)
1561 struct fq *fq = &local->fq;
1562 struct ieee80211_vif *vif;
1563 struct txq_info *txqi;
1565 if (!local->ops->wake_tx_queue ||
1566 sdata->vif.type == NL80211_IFTYPE_MONITOR)
1567 return false;
1569 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1570 sdata = container_of(sdata->bss,
1571 struct ieee80211_sub_if_data, u.ap);
1573 vif = &sdata->vif;
1574 txqi = ieee80211_get_txq(local, vif, sta, skb);
1576 if (!txqi)
1577 return false;
1579 spin_lock_bh(&fq->lock);
1580 ieee80211_txq_enqueue(local, txqi, skb);
1581 spin_unlock_bh(&fq->lock);
1583 drv_wake_tx_queue(local, txqi);
1585 return true;
1588 static bool ieee80211_tx_frags(struct ieee80211_local *local,
1589 struct ieee80211_vif *vif,
1590 struct ieee80211_sta *sta,
1591 struct sk_buff_head *skbs,
1592 bool txpending)
1594 struct ieee80211_tx_control control = {};
1595 struct sk_buff *skb, *tmp;
1596 unsigned long flags;
1598 skb_queue_walk_safe(skbs, skb, tmp) {
1599 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1600 int q = info->hw_queue;
1602 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1603 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1604 __skb_unlink(skb, skbs);
1605 ieee80211_free_txskb(&local->hw, skb);
1606 continue;
1608 #endif
1610 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1611 if (local->queue_stop_reasons[q] ||
1612 (!txpending && !skb_queue_empty(&local->pending[q]))) {
1613 if (unlikely(info->flags &
1614 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1615 if (local->queue_stop_reasons[q] &
1616 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1618 * Drop off-channel frames if queues
1619 * are stopped for any reason other
1620 * than off-channel operation. Never
1621 * queue them.
1623 spin_unlock_irqrestore(
1624 &local->queue_stop_reason_lock,
1625 flags);
1626 ieee80211_purge_tx_queue(&local->hw,
1627 skbs);
1628 return true;
1630 } else {
1633 * Since queue is stopped, queue up frames for
1634 * later transmission from the tx-pending
1635 * tasklet when the queue is woken again.
1637 if (txpending)
1638 skb_queue_splice_init(skbs,
1639 &local->pending[q]);
1640 else
1641 skb_queue_splice_tail_init(skbs,
1642 &local->pending[q]);
1644 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1645 flags);
1646 return false;
1649 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1651 info->control.vif = vif;
1652 control.sta = sta;
1654 __skb_unlink(skb, skbs);
1655 drv_tx(local, &control, skb);
1658 return true;
1662 * Returns false if the frame couldn't be transmitted but was queued instead.
1664 static bool __ieee80211_tx(struct ieee80211_local *local,
1665 struct sk_buff_head *skbs, int led_len,
1666 struct sta_info *sta, bool txpending)
1668 struct ieee80211_tx_info *info;
1669 struct ieee80211_sub_if_data *sdata;
1670 struct ieee80211_vif *vif;
1671 struct ieee80211_sta *pubsta;
1672 struct sk_buff *skb;
1673 bool result = true;
1674 __le16 fc;
1676 if (WARN_ON(skb_queue_empty(skbs)))
1677 return true;
1679 skb = skb_peek(skbs);
1680 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1681 info = IEEE80211_SKB_CB(skb);
1682 sdata = vif_to_sdata(info->control.vif);
1683 if (sta && !sta->uploaded)
1684 sta = NULL;
1686 if (sta)
1687 pubsta = &sta->sta;
1688 else
1689 pubsta = NULL;
1691 switch (sdata->vif.type) {
1692 case NL80211_IFTYPE_MONITOR:
1693 if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
1694 vif = &sdata->vif;
1695 break;
1697 sdata = rcu_dereference(local->monitor_sdata);
1698 if (sdata) {
1699 vif = &sdata->vif;
1700 info->hw_queue =
1701 vif->hw_queue[skb_get_queue_mapping(skb)];
1702 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
1703 ieee80211_purge_tx_queue(&local->hw, skbs);
1704 return true;
1705 } else
1706 vif = NULL;
1707 break;
1708 case NL80211_IFTYPE_AP_VLAN:
1709 sdata = container_of(sdata->bss,
1710 struct ieee80211_sub_if_data, u.ap);
1711 /* fall through */
1712 default:
1713 vif = &sdata->vif;
1714 break;
1717 result = ieee80211_tx_frags(local, vif, pubsta, skbs,
1718 txpending);
1720 ieee80211_tpt_led_trig_tx(local, fc, led_len);
1722 WARN_ON_ONCE(!skb_queue_empty(skbs));
1724 return result;
1728 * Invoke TX handlers, return 0 on success and non-zero if the
1729 * frame was dropped or queued.
1731 * The handlers are split into an early and late part. The latter is everything
1732 * that can be sensitive to reordering, and will be deferred to after packets
1733 * are dequeued from the intermediate queues (when they are enabled).
1735 static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx)
1737 ieee80211_tx_result res = TX_DROP;
1739 #define CALL_TXH(txh) \
1740 do { \
1741 res = txh(tx); \
1742 if (res != TX_CONTINUE) \
1743 goto txh_done; \
1744 } while (0)
1746 CALL_TXH(ieee80211_tx_h_dynamic_ps);
1747 CALL_TXH(ieee80211_tx_h_check_assoc);
1748 CALL_TXH(ieee80211_tx_h_ps_buf);
1749 CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
1750 CALL_TXH(ieee80211_tx_h_select_key);
1751 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1752 CALL_TXH(ieee80211_tx_h_rate_ctrl);
1754 txh_done:
1755 if (unlikely(res == TX_DROP)) {
1756 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1757 if (tx->skb)
1758 ieee80211_free_txskb(&tx->local->hw, tx->skb);
1759 else
1760 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1761 return -1;
1762 } else if (unlikely(res == TX_QUEUED)) {
1763 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1764 return -1;
1767 return 0;
1771 * Late handlers can be called while the sta lock is held. Handlers that can
1772 * cause packets to be generated will cause deadlock!
1774 static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx)
1776 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1777 ieee80211_tx_result res = TX_CONTINUE;
1779 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1780 __skb_queue_tail(&tx->skbs, tx->skb);
1781 tx->skb = NULL;
1782 goto txh_done;
1785 CALL_TXH(ieee80211_tx_h_michael_mic_add);
1786 CALL_TXH(ieee80211_tx_h_sequence);
1787 CALL_TXH(ieee80211_tx_h_fragment);
1788 /* handlers after fragment must be aware of tx info fragmentation! */
1789 CALL_TXH(ieee80211_tx_h_stats);
1790 CALL_TXH(ieee80211_tx_h_encrypt);
1791 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1792 CALL_TXH(ieee80211_tx_h_calculate_duration);
1793 #undef CALL_TXH
1795 txh_done:
1796 if (unlikely(res == TX_DROP)) {
1797 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1798 if (tx->skb)
1799 ieee80211_free_txskb(&tx->local->hw, tx->skb);
1800 else
1801 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1802 return -1;
1803 } else if (unlikely(res == TX_QUEUED)) {
1804 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1805 return -1;
1808 return 0;
1811 static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1813 int r = invoke_tx_handlers_early(tx);
1815 if (r)
1816 return r;
1817 return invoke_tx_handlers_late(tx);
1820 bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1821 struct ieee80211_vif *vif, struct sk_buff *skb,
1822 int band, struct ieee80211_sta **sta)
1824 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1825 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1826 struct ieee80211_tx_data tx;
1827 struct sk_buff *skb2;
1829 if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
1830 return false;
1832 info->band = band;
1833 info->control.vif = vif;
1834 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1836 if (invoke_tx_handlers(&tx))
1837 return false;
1839 if (sta) {
1840 if (tx.sta)
1841 *sta = &tx.sta->sta;
1842 else
1843 *sta = NULL;
1846 /* this function isn't suitable for fragmented data frames */
1847 skb2 = __skb_dequeue(&tx.skbs);
1848 if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1849 ieee80211_free_txskb(hw, skb2);
1850 ieee80211_purge_tx_queue(hw, &tx.skbs);
1851 return false;
1854 return true;
1856 EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1859 * Returns false if the frame couldn't be transmitted but was queued instead.
1861 static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
1862 struct sta_info *sta, struct sk_buff *skb,
1863 bool txpending, u32 txdata_flags)
1865 struct ieee80211_local *local = sdata->local;
1866 struct ieee80211_tx_data tx;
1867 ieee80211_tx_result res_prepare;
1868 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1869 bool result = true;
1870 int led_len;
1872 if (unlikely(skb->len < 10)) {
1873 dev_kfree_skb(skb);
1874 return true;
1877 /* initialises tx */
1878 led_len = skb->len;
1879 res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
1881 tx.flags |= txdata_flags;
1883 if (unlikely(res_prepare == TX_DROP)) {
1884 ieee80211_free_txskb(&local->hw, skb);
1885 return true;
1886 } else if (unlikely(res_prepare == TX_QUEUED)) {
1887 return true;
1890 /* set up hw_queue value early */
1891 if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
1892 !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
1893 info->hw_queue =
1894 sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1896 if (invoke_tx_handlers_early(&tx))
1897 return true;
1899 if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb))
1900 return true;
1902 if (!invoke_tx_handlers_late(&tx))
1903 result = __ieee80211_tx(local, &tx.skbs, led_len,
1904 tx.sta, txpending);
1906 return result;
1909 /* device xmit handlers */
1911 static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
1912 struct sk_buff *skb,
1913 int head_need, bool may_encrypt)
1915 struct ieee80211_local *local = sdata->local;
1916 struct ieee80211_hdr *hdr;
1917 bool enc_tailroom;
1918 int tail_need = 0;
1920 hdr = (struct ieee80211_hdr *) skb->data;
1921 enc_tailroom = may_encrypt &&
1922 (sdata->crypto_tx_tailroom_needed_cnt ||
1923 ieee80211_is_mgmt(hdr->frame_control));
1925 if (enc_tailroom) {
1926 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1927 tail_need -= skb_tailroom(skb);
1928 tail_need = max_t(int, tail_need, 0);
1931 if (skb_cloned(skb) &&
1932 (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
1933 !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
1934 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1935 else if (head_need || tail_need)
1936 I802_DEBUG_INC(local->tx_expand_skb_head);
1937 else
1938 return 0;
1940 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1941 wiphy_debug(local->hw.wiphy,
1942 "failed to reallocate TX buffer\n");
1943 return -ENOMEM;
1946 return 0;
1949 void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
1950 struct sta_info *sta, struct sk_buff *skb,
1951 u32 txdata_flags)
1953 struct ieee80211_local *local = sdata->local;
1954 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1955 struct ieee80211_hdr *hdr;
1956 int headroom;
1957 bool may_encrypt;
1959 may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
1961 headroom = local->tx_headroom;
1962 if (may_encrypt)
1963 headroom += sdata->encrypt_headroom;
1964 headroom -= skb_headroom(skb);
1965 headroom = max_t(int, 0, headroom);
1967 if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
1968 ieee80211_free_txskb(&local->hw, skb);
1969 return;
1972 hdr = (struct ieee80211_hdr *) skb->data;
1973 info->control.vif = &sdata->vif;
1975 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1976 if (ieee80211_is_data(hdr->frame_control) &&
1977 is_unicast_ether_addr(hdr->addr1)) {
1978 if (mesh_nexthop_resolve(sdata, skb))
1979 return; /* skb queued: don't free */
1980 } else {
1981 ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
1985 ieee80211_set_qos_hdr(sdata, skb);
1986 ieee80211_tx(sdata, sta, skb, false, txdata_flags);
1989 static bool ieee80211_parse_tx_radiotap(struct ieee80211_local *local,
1990 struct sk_buff *skb)
1992 struct ieee80211_radiotap_iterator iterator;
1993 struct ieee80211_radiotap_header *rthdr =
1994 (struct ieee80211_radiotap_header *) skb->data;
1995 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1996 struct ieee80211_supported_band *sband =
1997 local->hw.wiphy->bands[info->band];
1998 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
1999 NULL);
2000 u16 txflags;
2001 u16 rate = 0;
2002 bool rate_found = false;
2003 u8 rate_retries = 0;
2004 u16 rate_flags = 0;
2005 u8 mcs_known, mcs_flags, mcs_bw;
2006 u16 vht_known;
2007 u8 vht_mcs = 0, vht_nss = 0;
2008 int i;
2010 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
2011 IEEE80211_TX_CTL_DONTFRAG;
2014 * for every radiotap entry that is present
2015 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
2016 * entries present, or -EINVAL on error)
2019 while (!ret) {
2020 ret = ieee80211_radiotap_iterator_next(&iterator);
2022 if (ret)
2023 continue;
2025 /* see if this argument is something we can use */
2026 switch (iterator.this_arg_index) {
2028 * You must take care when dereferencing iterator.this_arg
2029 * for multibyte types... the pointer is not aligned. Use
2030 * get_unaligned((type *)iterator.this_arg) to dereference
2031 * iterator.this_arg for type "type" safely on all arches.
2033 case IEEE80211_RADIOTAP_FLAGS:
2034 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
2036 * this indicates that the skb we have been
2037 * handed has the 32-bit FCS CRC at the end...
2038 * we should react to that by snipping it off
2039 * because it will be recomputed and added
2040 * on transmission
2042 if (skb->len < (iterator._max_length + FCS_LEN))
2043 return false;
2045 skb_trim(skb, skb->len - FCS_LEN);
2047 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
2048 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
2049 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
2050 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
2051 break;
2053 case IEEE80211_RADIOTAP_TX_FLAGS:
2054 txflags = get_unaligned_le16(iterator.this_arg);
2055 if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
2056 info->flags |= IEEE80211_TX_CTL_NO_ACK;
2057 break;
2059 case IEEE80211_RADIOTAP_RATE:
2060 rate = *iterator.this_arg;
2061 rate_flags = 0;
2062 rate_found = true;
2063 break;
2065 case IEEE80211_RADIOTAP_DATA_RETRIES:
2066 rate_retries = *iterator.this_arg;
2067 break;
2069 case IEEE80211_RADIOTAP_MCS:
2070 mcs_known = iterator.this_arg[0];
2071 mcs_flags = iterator.this_arg[1];
2072 if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
2073 break;
2075 rate_found = true;
2076 rate = iterator.this_arg[2];
2077 rate_flags = IEEE80211_TX_RC_MCS;
2079 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
2080 mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
2081 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2083 mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
2084 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
2085 mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
2086 rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2087 break;
2089 case IEEE80211_RADIOTAP_VHT:
2090 vht_known = get_unaligned_le16(iterator.this_arg);
2091 rate_found = true;
2093 rate_flags = IEEE80211_TX_RC_VHT_MCS;
2094 if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
2095 (iterator.this_arg[2] &
2096 IEEE80211_RADIOTAP_VHT_FLAG_SGI))
2097 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2098 if (vht_known &
2099 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
2100 if (iterator.this_arg[3] == 1)
2101 rate_flags |=
2102 IEEE80211_TX_RC_40_MHZ_WIDTH;
2103 else if (iterator.this_arg[3] == 4)
2104 rate_flags |=
2105 IEEE80211_TX_RC_80_MHZ_WIDTH;
2106 else if (iterator.this_arg[3] == 11)
2107 rate_flags |=
2108 IEEE80211_TX_RC_160_MHZ_WIDTH;
2111 vht_mcs = iterator.this_arg[4] >> 4;
2112 vht_nss = iterator.this_arg[4] & 0xF;
2113 break;
2116 * Please update the file
2117 * Documentation/networking/mac80211-injection.txt
2118 * when parsing new fields here.
2121 default:
2122 break;
2126 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
2127 return false;
2129 if (rate_found) {
2130 info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
2132 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2133 info->control.rates[i].idx = -1;
2134 info->control.rates[i].flags = 0;
2135 info->control.rates[i].count = 0;
2138 if (rate_flags & IEEE80211_TX_RC_MCS) {
2139 info->control.rates[0].idx = rate;
2140 } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
2141 ieee80211_rate_set_vht(info->control.rates, vht_mcs,
2142 vht_nss);
2143 } else {
2144 for (i = 0; i < sband->n_bitrates; i++) {
2145 if (rate * 5 != sband->bitrates[i].bitrate)
2146 continue;
2148 info->control.rates[0].idx = i;
2149 break;
2153 if (info->control.rates[0].idx < 0)
2154 info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
2156 info->control.rates[0].flags = rate_flags;
2157 info->control.rates[0].count = min_t(u8, rate_retries + 1,
2158 local->hw.max_rate_tries);
2162 * remove the radiotap header
2163 * iterator->_max_length was sanity-checked against
2164 * skb->len by iterator init
2166 skb_pull(skb, iterator._max_length);
2168 return true;
2171 netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
2172 struct net_device *dev)
2174 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2175 struct ieee80211_chanctx_conf *chanctx_conf;
2176 struct ieee80211_radiotap_header *prthdr =
2177 (struct ieee80211_radiotap_header *)skb->data;
2178 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2179 struct ieee80211_hdr *hdr;
2180 struct ieee80211_sub_if_data *tmp_sdata, *sdata;
2181 struct cfg80211_chan_def *chandef;
2182 u16 len_rthdr;
2183 int hdrlen;
2185 /* check for not even having the fixed radiotap header part */
2186 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2187 goto fail; /* too short to be possibly valid */
2189 /* is it a header version we can trust to find length from? */
2190 if (unlikely(prthdr->it_version))
2191 goto fail; /* only version 0 is supported */
2193 /* then there must be a radiotap header with a length we can use */
2194 len_rthdr = ieee80211_get_radiotap_len(skb->data);
2196 /* does the skb contain enough to deliver on the alleged length? */
2197 if (unlikely(skb->len < len_rthdr))
2198 goto fail; /* skb too short for claimed rt header extent */
2201 * fix up the pointers accounting for the radiotap
2202 * header still being in there. We are being given
2203 * a precooked IEEE80211 header so no need for
2204 * normal processing
2206 skb_set_mac_header(skb, len_rthdr);
2208 * these are just fixed to the end of the rt area since we
2209 * don't have any better information and at this point, nobody cares
2211 skb_set_network_header(skb, len_rthdr);
2212 skb_set_transport_header(skb, len_rthdr);
2214 if (skb->len < len_rthdr + 2)
2215 goto fail;
2217 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2218 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2220 if (skb->len < len_rthdr + hdrlen)
2221 goto fail;
2224 * Initialize skb->protocol if the injected frame is a data frame
2225 * carrying a rfc1042 header
2227 if (ieee80211_is_data(hdr->frame_control) &&
2228 skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2229 u8 *payload = (u8 *)hdr + hdrlen;
2231 if (ether_addr_equal(payload, rfc1042_header))
2232 skb->protocol = cpu_to_be16((payload[6] << 8) |
2233 payload[7]);
2236 memset(info, 0, sizeof(*info));
2238 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2239 IEEE80211_TX_CTL_INJECTED;
2241 rcu_read_lock();
2244 * We process outgoing injected frames that have a local address
2245 * we handle as though they are non-injected frames.
2246 * This code here isn't entirely correct, the local MAC address
2247 * isn't always enough to find the interface to use; for proper
2248 * VLAN/WDS support we will need a different mechanism (which
2249 * likely isn't going to be monitor interfaces).
2251 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2253 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2254 if (!ieee80211_sdata_running(tmp_sdata))
2255 continue;
2256 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2257 tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2258 tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
2259 continue;
2260 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
2261 sdata = tmp_sdata;
2262 break;
2266 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2267 if (!chanctx_conf) {
2268 tmp_sdata = rcu_dereference(local->monitor_sdata);
2269 if (tmp_sdata)
2270 chanctx_conf =
2271 rcu_dereference(tmp_sdata->vif.chanctx_conf);
2274 if (chanctx_conf)
2275 chandef = &chanctx_conf->def;
2276 else if (!local->use_chanctx)
2277 chandef = &local->_oper_chandef;
2278 else
2279 goto fail_rcu;
2282 * Frame injection is not allowed if beaconing is not allowed
2283 * or if we need radar detection. Beaconing is usually not allowed when
2284 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2285 * Passive scan is also used in world regulatory domains where
2286 * your country is not known and as such it should be treated as
2287 * NO TX unless the channel is explicitly allowed in which case
2288 * your current regulatory domain would not have the passive scan
2289 * flag.
2291 * Since AP mode uses monitor interfaces to inject/TX management
2292 * frames we can make AP mode the exception to this rule once it
2293 * supports radar detection as its implementation can deal with
2294 * radar detection by itself. We can do that later by adding a
2295 * monitor flag interfaces used for AP support.
2297 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2298 sdata->vif.type))
2299 goto fail_rcu;
2301 info->band = chandef->chan->band;
2303 /* process and remove the injection radiotap header */
2304 if (!ieee80211_parse_tx_radiotap(local, skb))
2305 goto fail_rcu;
2307 ieee80211_xmit(sdata, NULL, skb, 0);
2308 rcu_read_unlock();
2310 return NETDEV_TX_OK;
2312 fail_rcu:
2313 rcu_read_unlock();
2314 fail:
2315 dev_kfree_skb(skb);
2316 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
2319 static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
2321 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
2323 return ethertype == ETH_P_TDLS &&
2324 skb->len > 14 &&
2325 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2328 static int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2329 struct sk_buff *skb,
2330 struct sta_info **sta_out)
2332 struct sta_info *sta;
2334 switch (sdata->vif.type) {
2335 case NL80211_IFTYPE_AP_VLAN:
2336 sta = rcu_dereference(sdata->u.vlan.sta);
2337 if (sta) {
2338 *sta_out = sta;
2339 return 0;
2340 } else if (sdata->wdev.use_4addr) {
2341 return -ENOLINK;
2343 /* fall through */
2344 case NL80211_IFTYPE_AP:
2345 case NL80211_IFTYPE_OCB:
2346 case NL80211_IFTYPE_ADHOC:
2347 if (is_multicast_ether_addr(skb->data)) {
2348 *sta_out = ERR_PTR(-ENOENT);
2349 return 0;
2351 sta = sta_info_get_bss(sdata, skb->data);
2352 break;
2353 case NL80211_IFTYPE_WDS:
2354 sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
2355 break;
2356 #ifdef CONFIG_MAC80211_MESH
2357 case NL80211_IFTYPE_MESH_POINT:
2358 /* determined much later */
2359 *sta_out = NULL;
2360 return 0;
2361 #endif
2362 case NL80211_IFTYPE_STATION:
2363 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2364 sta = sta_info_get(sdata, skb->data);
2365 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2366 if (test_sta_flag(sta,
2367 WLAN_STA_TDLS_PEER_AUTH)) {
2368 *sta_out = sta;
2369 return 0;
2373 * TDLS link during setup - throw out frames to
2374 * peer. Allow TDLS-setup frames to unauthorized
2375 * peers for the special case of a link teardown
2376 * after a TDLS sta is removed due to being
2377 * unreachable.
2379 if (!ieee80211_is_tdls_setup(skb))
2380 return -EINVAL;
2385 sta = sta_info_get(sdata, sdata->u.mgd.bssid);
2386 if (!sta)
2387 return -ENOLINK;
2388 break;
2389 default:
2390 return -EINVAL;
2393 *sta_out = sta ?: ERR_PTR(-ENOENT);
2394 return 0;
2398 * ieee80211_build_hdr - build 802.11 header in the given frame
2399 * @sdata: virtual interface to build the header for
2400 * @skb: the skb to build the header in
2401 * @info_flags: skb flags to set
2402 * @ctrl_flags: info control flags to set
2404 * This function takes the skb with 802.3 header and reformats the header to
2405 * the appropriate IEEE 802.11 header based on which interface the packet is
2406 * being transmitted on.
2408 * Note that this function also takes care of the TX status request and
2409 * potential unsharing of the SKB - this needs to be interleaved with the
2410 * header building.
2412 * The function requires the read-side RCU lock held
2414 * Returns: the (possibly reallocated) skb or an ERR_PTR() code
2416 static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
2417 struct sk_buff *skb, u32 info_flags,
2418 struct sta_info *sta, u32 ctrl_flags)
2420 struct ieee80211_local *local = sdata->local;
2421 struct ieee80211_tx_info *info;
2422 int head_need;
2423 u16 ethertype, hdrlen, meshhdrlen = 0;
2424 __le16 fc;
2425 struct ieee80211_hdr hdr;
2426 struct ieee80211s_hdr mesh_hdr __maybe_unused;
2427 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
2428 const u8 *encaps_data;
2429 int encaps_len, skip_header_bytes;
2430 bool wme_sta = false, authorized = false;
2431 bool tdls_peer;
2432 bool multicast;
2433 u16 info_id = 0;
2434 struct ieee80211_chanctx_conf *chanctx_conf;
2435 struct ieee80211_sub_if_data *ap_sdata;
2436 enum nl80211_band band;
2437 int ret;
2439 if (IS_ERR(sta))
2440 sta = NULL;
2442 /* convert Ethernet header to proper 802.11 header (based on
2443 * operation mode) */
2444 ethertype = (skb->data[12] << 8) | skb->data[13];
2445 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2447 switch (sdata->vif.type) {
2448 case NL80211_IFTYPE_AP_VLAN:
2449 if (sdata->wdev.use_4addr) {
2450 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2451 /* RA TA DA SA */
2452 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
2453 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2454 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2455 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2456 hdrlen = 30;
2457 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2458 wme_sta = sta->sta.wme;
2460 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2461 u.ap);
2462 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
2463 if (!chanctx_conf) {
2464 ret = -ENOTCONN;
2465 goto free;
2467 band = chanctx_conf->def.chan->band;
2468 if (sdata->wdev.use_4addr)
2469 break;
2470 /* fall through */
2471 case NL80211_IFTYPE_AP:
2472 if (sdata->vif.type == NL80211_IFTYPE_AP)
2473 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2474 if (!chanctx_conf) {
2475 ret = -ENOTCONN;
2476 goto free;
2478 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2479 /* DA BSSID SA */
2480 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2481 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2482 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2483 hdrlen = 24;
2484 band = chanctx_conf->def.chan->band;
2485 break;
2486 case NL80211_IFTYPE_WDS:
2487 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2488 /* RA TA DA SA */
2489 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
2490 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2491 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2492 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2493 hdrlen = 30;
2495 * This is the exception! WDS style interfaces are prohibited
2496 * when channel contexts are in used so this must be valid
2498 band = local->hw.conf.chandef.chan->band;
2499 break;
2500 #ifdef CONFIG_MAC80211_MESH
2501 case NL80211_IFTYPE_MESH_POINT:
2502 if (!is_multicast_ether_addr(skb->data)) {
2503 struct sta_info *next_hop;
2504 bool mpp_lookup = true;
2506 mpath = mesh_path_lookup(sdata, skb->data);
2507 if (mpath) {
2508 mpp_lookup = false;
2509 next_hop = rcu_dereference(mpath->next_hop);
2510 if (!next_hop ||
2511 !(mpath->flags & (MESH_PATH_ACTIVE |
2512 MESH_PATH_RESOLVING)))
2513 mpp_lookup = true;
2516 if (mpp_lookup) {
2517 mppath = mpp_path_lookup(sdata, skb->data);
2518 if (mppath)
2519 mppath->exp_time = jiffies;
2522 if (mppath && mpath)
2523 mesh_path_del(sdata, mpath->dst);
2527 * Use address extension if it is a packet from
2528 * another interface or if we know the destination
2529 * is being proxied by a portal (i.e. portal address
2530 * differs from proxied address)
2532 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2533 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
2534 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2535 skb->data, skb->data + ETH_ALEN);
2536 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2537 NULL, NULL);
2538 } else {
2539 /* DS -> MBSS (802.11-2012 13.11.3.3).
2540 * For unicast with unknown forwarding information,
2541 * destination might be in the MBSS or if that fails
2542 * forwarded to another mesh gate. In either case
2543 * resolution will be handled in ieee80211_xmit(), so
2544 * leave the original DA. This also works for mcast */
2545 const u8 *mesh_da = skb->data;
2547 if (mppath)
2548 mesh_da = mppath->mpp;
2549 else if (mpath)
2550 mesh_da = mpath->dst;
2552 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2553 mesh_da, sdata->vif.addr);
2554 if (is_multicast_ether_addr(mesh_da))
2555 /* DA TA mSA AE:SA */
2556 meshhdrlen = ieee80211_new_mesh_header(
2557 sdata, &mesh_hdr,
2558 skb->data + ETH_ALEN, NULL);
2559 else
2560 /* RA TA mDA mSA AE:DA SA */
2561 meshhdrlen = ieee80211_new_mesh_header(
2562 sdata, &mesh_hdr, skb->data,
2563 skb->data + ETH_ALEN);
2566 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2567 if (!chanctx_conf) {
2568 ret = -ENOTCONN;
2569 goto free;
2571 band = chanctx_conf->def.chan->band;
2572 break;
2573 #endif
2574 case NL80211_IFTYPE_STATION:
2575 /* we already did checks when looking up the RA STA */
2576 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
2578 if (tdls_peer) {
2579 /* DA SA BSSID */
2580 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2581 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2582 memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2583 hdrlen = 24;
2584 } else if (sdata->u.mgd.use_4addr &&
2585 cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2586 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2587 IEEE80211_FCTL_TODS);
2588 /* RA TA DA SA */
2589 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2590 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2591 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2592 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2593 hdrlen = 30;
2594 } else {
2595 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2596 /* BSSID SA DA */
2597 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2598 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2599 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2600 hdrlen = 24;
2602 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2603 if (!chanctx_conf) {
2604 ret = -ENOTCONN;
2605 goto free;
2607 band = chanctx_conf->def.chan->band;
2608 break;
2609 case NL80211_IFTYPE_OCB:
2610 /* DA SA BSSID */
2611 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2612 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2613 eth_broadcast_addr(hdr.addr3);
2614 hdrlen = 24;
2615 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2616 if (!chanctx_conf) {
2617 ret = -ENOTCONN;
2618 goto free;
2620 band = chanctx_conf->def.chan->band;
2621 break;
2622 case NL80211_IFTYPE_ADHOC:
2623 /* DA SA BSSID */
2624 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2625 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2626 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
2627 hdrlen = 24;
2628 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2629 if (!chanctx_conf) {
2630 ret = -ENOTCONN;
2631 goto free;
2633 band = chanctx_conf->def.chan->band;
2634 break;
2635 default:
2636 ret = -EINVAL;
2637 goto free;
2640 multicast = is_multicast_ether_addr(hdr.addr1);
2642 /* sta is always NULL for mesh */
2643 if (sta) {
2644 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2645 wme_sta = sta->sta.wme;
2646 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2647 /* For mesh, the use of the QoS header is mandatory */
2648 wme_sta = true;
2651 /* receiver does QoS (which also means we do) use it */
2652 if (wme_sta) {
2653 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2654 hdrlen += 2;
2658 * Drop unicast frames to unauthorised stations unless they are
2659 * EAPOL frames from the local station.
2661 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
2662 (sdata->vif.type != NL80211_IFTYPE_OCB) &&
2663 !multicast && !authorized &&
2664 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
2665 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
2666 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2667 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
2668 sdata->name, hdr.addr1);
2669 #endif
2671 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2673 ret = -EPERM;
2674 goto free;
2677 if (unlikely(!multicast && skb->sk &&
2678 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)) {
2679 struct sk_buff *ack_skb = skb_clone_sk(skb);
2681 if (ack_skb) {
2682 unsigned long flags;
2683 int id;
2685 spin_lock_irqsave(&local->ack_status_lock, flags);
2686 id = idr_alloc(&local->ack_status_frames, ack_skb,
2687 1, 0x10000, GFP_ATOMIC);
2688 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2690 if (id >= 0) {
2691 info_id = id;
2692 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2693 } else {
2694 kfree_skb(ack_skb);
2700 * If the skb is shared we need to obtain our own copy.
2702 if (skb_shared(skb)) {
2703 struct sk_buff *tmp_skb = skb;
2705 /* can't happen -- skb is a clone if info_id != 0 */
2706 WARN_ON(info_id);
2708 skb = skb_clone(skb, GFP_ATOMIC);
2709 kfree_skb(tmp_skb);
2711 if (!skb) {
2712 ret = -ENOMEM;
2713 goto free;
2717 hdr.frame_control = fc;
2718 hdr.duration_id = 0;
2719 hdr.seq_ctrl = 0;
2721 skip_header_bytes = ETH_HLEN;
2722 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2723 encaps_data = bridge_tunnel_header;
2724 encaps_len = sizeof(bridge_tunnel_header);
2725 skip_header_bytes -= 2;
2726 } else if (ethertype >= ETH_P_802_3_MIN) {
2727 encaps_data = rfc1042_header;
2728 encaps_len = sizeof(rfc1042_header);
2729 skip_header_bytes -= 2;
2730 } else {
2731 encaps_data = NULL;
2732 encaps_len = 0;
2735 skb_pull(skb, skip_header_bytes);
2736 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
2739 * So we need to modify the skb header and hence need a copy of
2740 * that. The head_need variable above doesn't, so far, include
2741 * the needed header space that we don't need right away. If we
2742 * can, then we don't reallocate right now but only after the
2743 * frame arrives at the master device (if it does...)
2745 * If we cannot, however, then we will reallocate to include all
2746 * the ever needed space. Also, if we need to reallocate it anyway,
2747 * make it big enough for everything we may ever need.
2750 if (head_need > 0 || skb_cloned(skb)) {
2751 head_need += sdata->encrypt_headroom;
2752 head_need += local->tx_headroom;
2753 head_need = max_t(int, 0, head_need);
2754 if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
2755 ieee80211_free_txskb(&local->hw, skb);
2756 skb = NULL;
2757 return ERR_PTR(-ENOMEM);
2761 if (encaps_data)
2762 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
2764 #ifdef CONFIG_MAC80211_MESH
2765 if (meshhdrlen > 0)
2766 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
2767 #endif
2769 if (ieee80211_is_data_qos(fc)) {
2770 __le16 *qos_control;
2772 qos_control = skb_push(skb, 2);
2773 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2775 * Maybe we could actually set some fields here, for now just
2776 * initialise to zero to indicate no special operation.
2778 *qos_control = 0;
2779 } else
2780 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2782 skb_reset_mac_header(skb);
2784 info = IEEE80211_SKB_CB(skb);
2785 memset(info, 0, sizeof(*info));
2787 info->flags = info_flags;
2788 info->ack_frame_id = info_id;
2789 info->band = band;
2790 info->control.flags = ctrl_flags;
2792 return skb;
2793 free:
2794 kfree_skb(skb);
2795 return ERR_PTR(ret);
2799 * fast-xmit overview
2801 * The core idea of this fast-xmit is to remove per-packet checks by checking
2802 * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2803 * checks that are needed to get the sta->fast_tx pointer assigned, after which
2804 * much less work can be done per packet. For example, fragmentation must be
2805 * disabled or the fast_tx pointer will not be set. All the conditions are seen
2806 * in the code here.
2808 * Once assigned, the fast_tx data structure also caches the per-packet 802.11
2809 * header and other data to aid packet processing in ieee80211_xmit_fast().
2811 * The most difficult part of this is that when any of these assumptions
2812 * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
2813 * ieee80211_check_fast_xmit() or friends) is required to reset the data,
2814 * since the per-packet code no longer checks the conditions. This is reflected
2815 * by the calls to these functions throughout the rest of the code, and must be
2816 * maintained if any of the TX path checks change.
2819 void ieee80211_check_fast_xmit(struct sta_info *sta)
2821 struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
2822 struct ieee80211_local *local = sta->local;
2823 struct ieee80211_sub_if_data *sdata = sta->sdata;
2824 struct ieee80211_hdr *hdr = (void *)build.hdr;
2825 struct ieee80211_chanctx_conf *chanctx_conf;
2826 __le16 fc;
2828 if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
2829 return;
2831 /* Locking here protects both the pointer itself, and against concurrent
2832 * invocations winning data access races to, e.g., the key pointer that
2833 * is used.
2834 * Without it, the invocation of this function right after the key
2835 * pointer changes wouldn't be sufficient, as another CPU could access
2836 * the pointer, then stall, and then do the cache update after the CPU
2837 * that invalidated the key.
2838 * With the locking, such scenarios cannot happen as the check for the
2839 * key and the fast-tx assignment are done atomically, so the CPU that
2840 * modifies the key will either wait or other one will see the key
2841 * cleared/changed already.
2843 spin_lock_bh(&sta->lock);
2844 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
2845 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2846 sdata->vif.type == NL80211_IFTYPE_STATION)
2847 goto out;
2849 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2850 goto out;
2852 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
2853 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
2854 test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
2855 test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
2856 goto out;
2858 if (sdata->noack_map)
2859 goto out;
2861 /* fast-xmit doesn't handle fragmentation at all */
2862 if (local->hw.wiphy->frag_threshold != (u32)-1 &&
2863 !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
2864 goto out;
2866 rcu_read_lock();
2867 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2868 if (!chanctx_conf) {
2869 rcu_read_unlock();
2870 goto out;
2872 build.band = chanctx_conf->def.chan->band;
2873 rcu_read_unlock();
2875 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2877 switch (sdata->vif.type) {
2878 case NL80211_IFTYPE_ADHOC:
2879 /* DA SA BSSID */
2880 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2881 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2882 memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
2883 build.hdr_len = 24;
2884 break;
2885 case NL80211_IFTYPE_STATION:
2886 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2887 /* DA SA BSSID */
2888 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2889 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2890 memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
2891 build.hdr_len = 24;
2892 break;
2895 if (sdata->u.mgd.use_4addr) {
2896 /* non-regular ethertype cannot use the fastpath */
2897 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2898 IEEE80211_FCTL_TODS);
2899 /* RA TA DA SA */
2900 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2901 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2902 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2903 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2904 build.hdr_len = 30;
2905 break;
2907 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2908 /* BSSID SA DA */
2909 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2910 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2911 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2912 build.hdr_len = 24;
2913 break;
2914 case NL80211_IFTYPE_AP_VLAN:
2915 if (sdata->wdev.use_4addr) {
2916 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2917 IEEE80211_FCTL_TODS);
2918 /* RA TA DA SA */
2919 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
2920 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2921 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2922 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2923 build.hdr_len = 30;
2924 break;
2926 /* fall through */
2927 case NL80211_IFTYPE_AP:
2928 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2929 /* DA BSSID SA */
2930 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2931 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2932 build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
2933 build.hdr_len = 24;
2934 break;
2935 default:
2936 /* not handled on fast-xmit */
2937 goto out;
2940 if (sta->sta.wme) {
2941 build.hdr_len += 2;
2942 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2945 /* We store the key here so there's no point in using rcu_dereference()
2946 * but that's fine because the code that changes the pointers will call
2947 * this function after doing so. For a single CPU that would be enough,
2948 * for multiple see the comment above.
2950 build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
2951 if (!build.key)
2952 build.key = rcu_access_pointer(sdata->default_unicast_key);
2953 if (build.key) {
2954 bool gen_iv, iv_spc, mmic;
2956 gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
2957 iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2958 mmic = build.key->conf.flags &
2959 (IEEE80211_KEY_FLAG_GENERATE_MMIC |
2960 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
2962 /* don't handle software crypto */
2963 if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
2964 goto out;
2966 switch (build.key->conf.cipher) {
2967 case WLAN_CIPHER_SUITE_CCMP:
2968 case WLAN_CIPHER_SUITE_CCMP_256:
2969 /* add fixed key ID */
2970 if (gen_iv) {
2971 (build.hdr + build.hdr_len)[3] =
2972 0x20 | (build.key->conf.keyidx << 6);
2973 build.pn_offs = build.hdr_len;
2975 if (gen_iv || iv_spc)
2976 build.hdr_len += IEEE80211_CCMP_HDR_LEN;
2977 break;
2978 case WLAN_CIPHER_SUITE_GCMP:
2979 case WLAN_CIPHER_SUITE_GCMP_256:
2980 /* add fixed key ID */
2981 if (gen_iv) {
2982 (build.hdr + build.hdr_len)[3] =
2983 0x20 | (build.key->conf.keyidx << 6);
2984 build.pn_offs = build.hdr_len;
2986 if (gen_iv || iv_spc)
2987 build.hdr_len += IEEE80211_GCMP_HDR_LEN;
2988 break;
2989 case WLAN_CIPHER_SUITE_TKIP:
2990 /* cannot handle MMIC or IV generation in xmit-fast */
2991 if (mmic || gen_iv)
2992 goto out;
2993 if (iv_spc)
2994 build.hdr_len += IEEE80211_TKIP_IV_LEN;
2995 break;
2996 case WLAN_CIPHER_SUITE_WEP40:
2997 case WLAN_CIPHER_SUITE_WEP104:
2998 /* cannot handle IV generation in fast-xmit */
2999 if (gen_iv)
3000 goto out;
3001 if (iv_spc)
3002 build.hdr_len += IEEE80211_WEP_IV_LEN;
3003 break;
3004 case WLAN_CIPHER_SUITE_AES_CMAC:
3005 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3006 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3007 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3008 WARN(1,
3009 "management cipher suite 0x%x enabled for data\n",
3010 build.key->conf.cipher);
3011 goto out;
3012 default:
3013 /* we don't know how to generate IVs for this at all */
3014 if (WARN_ON(gen_iv))
3015 goto out;
3016 /* pure hardware keys are OK, of course */
3017 if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
3018 break;
3019 /* cipher scheme might require space allocation */
3020 if (iv_spc &&
3021 build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
3022 goto out;
3023 if (iv_spc)
3024 build.hdr_len += build.key->conf.iv_len;
3027 fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3030 hdr->frame_control = fc;
3032 memcpy(build.hdr + build.hdr_len,
3033 rfc1042_header, sizeof(rfc1042_header));
3034 build.hdr_len += sizeof(rfc1042_header);
3036 fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3037 /* if the kmemdup fails, continue w/o fast_tx */
3038 if (!fast_tx)
3039 goto out;
3041 out:
3042 /* we might have raced against another call to this function */
3043 old = rcu_dereference_protected(sta->fast_tx,
3044 lockdep_is_held(&sta->lock));
3045 rcu_assign_pointer(sta->fast_tx, fast_tx);
3046 if (old)
3047 kfree_rcu(old, rcu_head);
3048 spin_unlock_bh(&sta->lock);
3051 void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3053 struct sta_info *sta;
3055 rcu_read_lock();
3056 list_for_each_entry_rcu(sta, &local->sta_list, list)
3057 ieee80211_check_fast_xmit(sta);
3058 rcu_read_unlock();
3061 void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3063 struct ieee80211_local *local = sdata->local;
3064 struct sta_info *sta;
3066 rcu_read_lock();
3068 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3069 if (sdata != sta->sdata &&
3070 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3071 continue;
3072 ieee80211_check_fast_xmit(sta);
3075 rcu_read_unlock();
3078 void ieee80211_clear_fast_xmit(struct sta_info *sta)
3080 struct ieee80211_fast_tx *fast_tx;
3082 spin_lock_bh(&sta->lock);
3083 fast_tx = rcu_dereference_protected(sta->fast_tx,
3084 lockdep_is_held(&sta->lock));
3085 RCU_INIT_POINTER(sta->fast_tx, NULL);
3086 spin_unlock_bh(&sta->lock);
3088 if (fast_tx)
3089 kfree_rcu(fast_tx, rcu_head);
3092 static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
3093 struct sk_buff *skb, int headroom)
3095 if (skb_headroom(skb) < headroom) {
3096 I802_DEBUG_INC(local->tx_expand_skb_head);
3098 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
3099 wiphy_debug(local->hw.wiphy,
3100 "failed to reallocate TX buffer\n");
3101 return false;
3105 return true;
3108 static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3109 struct ieee80211_fast_tx *fast_tx,
3110 struct sk_buff *skb)
3112 struct ieee80211_local *local = sdata->local;
3113 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3114 struct ieee80211_hdr *hdr;
3115 struct ethhdr *amsdu_hdr;
3116 int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3117 int subframe_len = skb->len - hdr_len;
3118 void *data;
3119 u8 *qc, *h_80211_src, *h_80211_dst;
3120 const u8 *bssid;
3122 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3123 return false;
3125 if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3126 return true;
3128 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(*amsdu_hdr)))
3129 return false;
3131 data = skb_push(skb, sizeof(*amsdu_hdr));
3132 memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
3133 hdr = data;
3134 amsdu_hdr = data + hdr_len;
3135 /* h_80211_src/dst is addr* field within hdr */
3136 h_80211_src = data + fast_tx->sa_offs;
3137 h_80211_dst = data + fast_tx->da_offs;
3139 amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3140 ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3141 ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
3143 /* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3144 * fields needs to be changed to BSSID for A-MSDU frames depending
3145 * on FromDS/ToDS values.
3147 switch (sdata->vif.type) {
3148 case NL80211_IFTYPE_STATION:
3149 bssid = sdata->u.mgd.bssid;
3150 break;
3151 case NL80211_IFTYPE_AP:
3152 case NL80211_IFTYPE_AP_VLAN:
3153 bssid = sdata->vif.addr;
3154 break;
3155 default:
3156 bssid = NULL;
3159 if (bssid && ieee80211_has_fromds(hdr->frame_control))
3160 ether_addr_copy(h_80211_src, bssid);
3162 if (bssid && ieee80211_has_tods(hdr->frame_control))
3163 ether_addr_copy(h_80211_dst, bssid);
3165 qc = ieee80211_get_qos_ctl(hdr);
3166 *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3168 info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3170 return true;
3173 static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3174 struct sta_info *sta,
3175 struct ieee80211_fast_tx *fast_tx,
3176 struct sk_buff *skb)
3178 struct ieee80211_local *local = sdata->local;
3179 struct fq *fq = &local->fq;
3180 struct fq_tin *tin;
3181 struct fq_flow *flow;
3182 u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3183 struct ieee80211_txq *txq = sta->sta.txq[tid];
3184 struct txq_info *txqi;
3185 struct sk_buff **frag_tail, *head;
3186 int subframe_len = skb->len - ETH_ALEN;
3187 u8 max_subframes = sta->sta.max_amsdu_subframes;
3188 int max_frags = local->hw.max_tx_fragments;
3189 int max_amsdu_len = sta->sta.max_amsdu_len;
3190 int orig_truesize;
3191 __be16 len;
3192 void *data;
3193 bool ret = false;
3194 unsigned int orig_len;
3195 int n = 2, nfrags, pad = 0;
3196 u16 hdrlen;
3198 if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
3199 return false;
3201 if (!txq)
3202 return false;
3204 txqi = to_txq_info(txq);
3205 if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3206 return false;
3208 if (sta->sta.max_rc_amsdu_len)
3209 max_amsdu_len = min_t(int, max_amsdu_len,
3210 sta->sta.max_rc_amsdu_len);
3212 spin_lock_bh(&fq->lock);
3214 /* TODO: Ideally aggregation should be done on dequeue to remain
3215 * responsive to environment changes.
3218 tin = &txqi->tin;
3219 flow = fq_flow_classify(fq, tin, skb, fq_flow_get_default_func);
3220 head = skb_peek_tail(&flow->queue);
3221 if (!head)
3222 goto out;
3224 orig_truesize = head->truesize;
3225 orig_len = head->len;
3227 if (skb->len + head->len > max_amsdu_len)
3228 goto out;
3230 nfrags = 1 + skb_shinfo(skb)->nr_frags;
3231 nfrags += 1 + skb_shinfo(head)->nr_frags;
3232 frag_tail = &skb_shinfo(head)->frag_list;
3233 while (*frag_tail) {
3234 nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3235 frag_tail = &(*frag_tail)->next;
3236 n++;
3239 if (max_subframes && n > max_subframes)
3240 goto out;
3242 if (max_frags && nfrags > max_frags)
3243 goto out;
3245 if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
3246 goto out;
3249 * Pad out the previous subframe to a multiple of 4 by adding the
3250 * padding to the next one, that's being added. Note that head->len
3251 * is the length of the full A-MSDU, but that works since each time
3252 * we add a new subframe we pad out the previous one to a multiple
3253 * of 4 and thus it no longer matters in the next round.
3255 hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3256 if ((head->len - hdrlen) & 3)
3257 pad = 4 - ((head->len - hdrlen) & 3);
3259 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3260 2 + pad))
3261 goto out_recalc;
3263 ret = true;
3264 data = skb_push(skb, ETH_ALEN + 2);
3265 memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3267 data += 2 * ETH_ALEN;
3268 len = cpu_to_be16(subframe_len);
3269 memcpy(data, &len, 2);
3270 memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3272 memset(skb_push(skb, pad), 0, pad);
3274 head->len += skb->len;
3275 head->data_len += skb->len;
3276 *frag_tail = skb;
3278 out_recalc:
3279 fq->memory_usage += head->truesize - orig_truesize;
3280 if (head->len != orig_len) {
3281 flow->backlog += head->len - orig_len;
3282 tin->backlog_bytes += head->len - orig_len;
3284 fq_recalc_backlog(fq, tin, flow);
3286 out:
3287 spin_unlock_bh(&fq->lock);
3289 return ret;
3293 * Can be called while the sta lock is held. Anything that can cause packets to
3294 * be generated will cause deadlock!
3296 static void ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3297 struct sta_info *sta, u8 pn_offs,
3298 struct ieee80211_key *key,
3299 struct sk_buff *skb)
3301 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3302 struct ieee80211_hdr *hdr = (void *)skb->data;
3303 u8 tid = IEEE80211_NUM_TIDS;
3305 if (key)
3306 info->control.hw_key = &key->conf;
3308 ieee80211_tx_stats(skb->dev, skb->len);
3310 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3311 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3312 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3313 } else {
3314 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3315 hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3316 sdata->sequence_number += 0x10;
3319 if (skb_shinfo(skb)->gso_size)
3320 sta->tx_stats.msdu[tid] +=
3321 DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3322 else
3323 sta->tx_stats.msdu[tid]++;
3325 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3327 /* statistics normally done by ieee80211_tx_h_stats (but that
3328 * has to consider fragmentation, so is more complex)
3330 sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3331 sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
3333 if (pn_offs) {
3334 u64 pn;
3335 u8 *crypto_hdr = skb->data + pn_offs;
3337 switch (key->conf.cipher) {
3338 case WLAN_CIPHER_SUITE_CCMP:
3339 case WLAN_CIPHER_SUITE_CCMP_256:
3340 case WLAN_CIPHER_SUITE_GCMP:
3341 case WLAN_CIPHER_SUITE_GCMP_256:
3342 pn = atomic64_inc_return(&key->conf.tx_pn);
3343 crypto_hdr[0] = pn;
3344 crypto_hdr[1] = pn >> 8;
3345 crypto_hdr[4] = pn >> 16;
3346 crypto_hdr[5] = pn >> 24;
3347 crypto_hdr[6] = pn >> 32;
3348 crypto_hdr[7] = pn >> 40;
3349 break;
3354 static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
3355 struct sta_info *sta,
3356 struct ieee80211_fast_tx *fast_tx,
3357 struct sk_buff *skb)
3359 struct ieee80211_local *local = sdata->local;
3360 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3361 int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3362 int hw_headroom = sdata->local->hw.extra_tx_headroom;
3363 struct ethhdr eth;
3364 struct ieee80211_tx_info *info;
3365 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3366 struct ieee80211_tx_data tx;
3367 ieee80211_tx_result r;
3368 struct tid_ampdu_tx *tid_tx = NULL;
3369 u8 tid = IEEE80211_NUM_TIDS;
3371 /* control port protocol needs a lot of special handling */
3372 if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3373 return false;
3375 /* only RFC 1042 SNAP */
3376 if (ethertype < ETH_P_802_3_MIN)
3377 return false;
3379 /* don't handle TX status request here either */
3380 if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3381 return false;
3383 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3384 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3385 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
3386 if (tid_tx) {
3387 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3388 return false;
3389 if (tid_tx->timeout)
3390 tid_tx->last_tx = jiffies;
3394 /* after this point (skb is modified) we cannot return false */
3396 if (skb_shared(skb)) {
3397 struct sk_buff *tmp_skb = skb;
3399 skb = skb_clone(skb, GFP_ATOMIC);
3400 kfree_skb(tmp_skb);
3402 if (!skb)
3403 return true;
3406 if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3407 ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
3408 return true;
3410 /* will not be crypto-handled beyond what we do here, so use false
3411 * as the may-encrypt argument for the resize to not account for
3412 * more room than we already have in 'extra_head'
3414 if (unlikely(ieee80211_skb_resize(sdata, skb,
3415 max_t(int, extra_head + hw_headroom -
3416 skb_headroom(skb), 0),
3417 false))) {
3418 kfree_skb(skb);
3419 return true;
3422 memcpy(&eth, skb->data, ETH_HLEN - 2);
3423 hdr = skb_push(skb, extra_head);
3424 memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3425 memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
3426 memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
3428 info = IEEE80211_SKB_CB(skb);
3429 memset(info, 0, sizeof(*info));
3430 info->band = fast_tx->band;
3431 info->control.vif = &sdata->vif;
3432 info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3433 IEEE80211_TX_CTL_DONTFRAG |
3434 (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
3435 info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT;
3437 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3438 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3439 *ieee80211_get_qos_ctl(hdr) = tid;
3442 __skb_queue_head_init(&tx.skbs);
3444 tx.flags = IEEE80211_TX_UNICAST;
3445 tx.local = local;
3446 tx.sdata = sdata;
3447 tx.sta = sta;
3448 tx.key = fast_tx->key;
3450 if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
3451 tx.skb = skb;
3452 r = ieee80211_tx_h_rate_ctrl(&tx);
3453 skb = tx.skb;
3454 tx.skb = NULL;
3456 if (r != TX_CONTINUE) {
3457 if (r != TX_QUEUED)
3458 kfree_skb(skb);
3459 return true;
3463 if (ieee80211_queue_skb(local, sdata, sta, skb))
3464 return true;
3466 ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3467 fast_tx->key, skb);
3469 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3470 sdata = container_of(sdata->bss,
3471 struct ieee80211_sub_if_data, u.ap);
3473 __skb_queue_tail(&tx.skbs, skb);
3474 ieee80211_tx_frags(local, &sdata->vif, &sta->sta, &tx.skbs, false);
3475 return true;
3478 struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3479 struct ieee80211_txq *txq)
3481 struct ieee80211_local *local = hw_to_local(hw);
3482 struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3483 struct ieee80211_hdr *hdr;
3484 struct sk_buff *skb = NULL;
3485 struct fq *fq = &local->fq;
3486 struct fq_tin *tin = &txqi->tin;
3487 struct ieee80211_tx_info *info;
3488 struct ieee80211_tx_data tx;
3489 ieee80211_tx_result r;
3490 struct ieee80211_vif *vif;
3492 spin_lock_bh(&fq->lock);
3494 if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags))
3495 goto out;
3497 /* Make sure fragments stay together. */
3498 skb = __skb_dequeue(&txqi->frags);
3499 if (skb)
3500 goto out;
3502 begin:
3503 skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
3504 if (!skb)
3505 goto out;
3507 hdr = (struct ieee80211_hdr *)skb->data;
3508 info = IEEE80211_SKB_CB(skb);
3510 memset(&tx, 0, sizeof(tx));
3511 __skb_queue_head_init(&tx.skbs);
3512 tx.local = local;
3513 tx.skb = skb;
3514 tx.sdata = vif_to_sdata(info->control.vif);
3516 if (txq->sta) {
3517 tx.sta = container_of(txq->sta, struct sta_info, sta);
3519 * Drop unicast frames to unauthorised stations unless they are
3520 * EAPOL frames from the local station.
3522 if (unlikely(ieee80211_is_data(hdr->frame_control) &&
3523 !ieee80211_vif_is_mesh(&tx.sdata->vif) &&
3524 tx.sdata->vif.type != NL80211_IFTYPE_OCB &&
3525 !is_multicast_ether_addr(hdr->addr1) &&
3526 !test_sta_flag(tx.sta, WLAN_STA_AUTHORIZED) &&
3527 (!(info->control.flags &
3528 IEEE80211_TX_CTRL_PORT_CTRL_PROTO) ||
3529 !ether_addr_equal(tx.sdata->vif.addr,
3530 hdr->addr2)))) {
3531 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
3532 ieee80211_free_txskb(&local->hw, skb);
3533 goto begin;
3538 * The key can be removed while the packet was queued, so need to call
3539 * this here to get the current key.
3541 r = ieee80211_tx_h_select_key(&tx);
3542 if (r != TX_CONTINUE) {
3543 ieee80211_free_txskb(&local->hw, skb);
3544 goto begin;
3547 if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
3548 info->flags |= IEEE80211_TX_CTL_AMPDU;
3549 else
3550 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
3552 if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
3553 struct sta_info *sta = container_of(txq->sta, struct sta_info,
3554 sta);
3555 u8 pn_offs = 0;
3557 if (tx.key &&
3558 (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
3559 pn_offs = ieee80211_hdrlen(hdr->frame_control);
3561 ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
3562 tx.key, skb);
3563 } else {
3564 if (invoke_tx_handlers_late(&tx))
3565 goto begin;
3567 skb = __skb_dequeue(&tx.skbs);
3569 if (!skb_queue_empty(&tx.skbs))
3570 skb_queue_splice_tail(&tx.skbs, &txqi->frags);
3573 if (skb && skb_has_frag_list(skb) &&
3574 !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
3575 if (skb_linearize(skb)) {
3576 ieee80211_free_txskb(&local->hw, skb);
3577 goto begin;
3581 switch (tx.sdata->vif.type) {
3582 case NL80211_IFTYPE_MONITOR:
3583 if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
3584 vif = &tx.sdata->vif;
3585 break;
3587 tx.sdata = rcu_dereference(local->monitor_sdata);
3588 if (tx.sdata) {
3589 vif = &tx.sdata->vif;
3590 info->hw_queue =
3591 vif->hw_queue[skb_get_queue_mapping(skb)];
3592 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
3593 ieee80211_free_txskb(&local->hw, skb);
3594 goto begin;
3595 } else {
3596 vif = NULL;
3598 break;
3599 case NL80211_IFTYPE_AP_VLAN:
3600 tx.sdata = container_of(tx.sdata->bss,
3601 struct ieee80211_sub_if_data, u.ap);
3602 /* fall through */
3603 default:
3604 vif = &tx.sdata->vif;
3605 break;
3608 IEEE80211_SKB_CB(skb)->control.vif = vif;
3609 out:
3610 spin_unlock_bh(&fq->lock);
3612 return skb;
3614 EXPORT_SYMBOL(ieee80211_tx_dequeue);
3616 void __ieee80211_subif_start_xmit(struct sk_buff *skb,
3617 struct net_device *dev,
3618 u32 info_flags,
3619 u32 ctrl_flags)
3621 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3622 struct sta_info *sta;
3623 struct sk_buff *next;
3625 if (unlikely(skb->len < ETH_HLEN)) {
3626 kfree_skb(skb);
3627 return;
3630 rcu_read_lock();
3632 if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
3633 goto out_free;
3635 if (!IS_ERR_OR_NULL(sta)) {
3636 struct ieee80211_fast_tx *fast_tx;
3638 /* We need a bit of data queued to build aggregates properly, so
3639 * instruct the TCP stack to allow more than a single ms of data
3640 * to be queued in the stack. The value is a bit-shift of 1
3641 * second, so 7 is ~8ms of queued data. Only affects local TCP
3642 * sockets.
3644 sk_pacing_shift_update(skb->sk, 7);
3646 fast_tx = rcu_dereference(sta->fast_tx);
3648 if (fast_tx &&
3649 ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
3650 goto out;
3653 if (skb_is_gso(skb)) {
3654 struct sk_buff *segs;
3656 segs = skb_gso_segment(skb, 0);
3657 if (IS_ERR(segs)) {
3658 goto out_free;
3659 } else if (segs) {
3660 consume_skb(skb);
3661 skb = segs;
3663 } else {
3664 /* we cannot process non-linear frames on this path */
3665 if (skb_linearize(skb)) {
3666 kfree_skb(skb);
3667 goto out;
3670 /* the frame could be fragmented, software-encrypted, and other
3671 * things so we cannot really handle checksum offload with it -
3672 * fix it up in software before we handle anything else.
3674 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3675 skb_set_transport_header(skb,
3676 skb_checksum_start_offset(skb));
3677 if (skb_checksum_help(skb))
3678 goto out_free;
3682 next = skb;
3683 while (next) {
3684 skb = next;
3685 next = skb->next;
3687 skb->prev = NULL;
3688 skb->next = NULL;
3690 skb = ieee80211_build_hdr(sdata, skb, info_flags,
3691 sta, ctrl_flags);
3692 if (IS_ERR(skb))
3693 goto out;
3695 ieee80211_tx_stats(dev, skb->len);
3697 ieee80211_xmit(sdata, sta, skb, 0);
3699 goto out;
3700 out_free:
3701 kfree_skb(skb);
3702 out:
3703 rcu_read_unlock();
3706 static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
3708 struct ethhdr *eth;
3709 int err;
3711 err = skb_ensure_writable(skb, ETH_HLEN);
3712 if (unlikely(err))
3713 return err;
3715 eth = (void *)skb->data;
3716 ether_addr_copy(eth->h_dest, sta->sta.addr);
3718 return 0;
3721 static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
3722 struct net_device *dev)
3724 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3725 const struct ethhdr *eth = (void *)skb->data;
3726 const struct vlan_ethhdr *ethvlan = (void *)skb->data;
3727 __be16 ethertype;
3729 if (likely(!is_multicast_ether_addr(eth->h_dest)))
3730 return false;
3732 switch (sdata->vif.type) {
3733 case NL80211_IFTYPE_AP_VLAN:
3734 if (sdata->u.vlan.sta)
3735 return false;
3736 if (sdata->wdev.use_4addr)
3737 return false;
3738 /* fall through */
3739 case NL80211_IFTYPE_AP:
3740 /* check runtime toggle for this bss */
3741 if (!sdata->bss->multicast_to_unicast)
3742 return false;
3743 break;
3744 default:
3745 return false;
3748 /* multicast to unicast conversion only for some payload */
3749 ethertype = eth->h_proto;
3750 if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
3751 ethertype = ethvlan->h_vlan_encapsulated_proto;
3752 switch (ethertype) {
3753 case htons(ETH_P_ARP):
3754 case htons(ETH_P_IP):
3755 case htons(ETH_P_IPV6):
3756 break;
3757 default:
3758 return false;
3761 return true;
3764 static void
3765 ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
3766 struct sk_buff_head *queue)
3768 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3769 struct ieee80211_local *local = sdata->local;
3770 const struct ethhdr *eth = (struct ethhdr *)skb->data;
3771 struct sta_info *sta, *first = NULL;
3772 struct sk_buff *cloned_skb;
3774 rcu_read_lock();
3776 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3777 if (sdata != sta->sdata)
3778 /* AP-VLAN mismatch */
3779 continue;
3780 if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
3781 /* do not send back to source */
3782 continue;
3783 if (!first) {
3784 first = sta;
3785 continue;
3787 cloned_skb = skb_clone(skb, GFP_ATOMIC);
3788 if (!cloned_skb)
3789 goto multicast;
3790 if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
3791 dev_kfree_skb(cloned_skb);
3792 goto multicast;
3794 __skb_queue_tail(queue, cloned_skb);
3797 if (likely(first)) {
3798 if (unlikely(ieee80211_change_da(skb, first)))
3799 goto multicast;
3800 __skb_queue_tail(queue, skb);
3801 } else {
3802 /* no STA connected, drop */
3803 kfree_skb(skb);
3804 skb = NULL;
3807 goto out;
3808 multicast:
3809 __skb_queue_purge(queue);
3810 __skb_queue_tail(queue, skb);
3811 out:
3812 rcu_read_unlock();
3816 * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
3817 * @skb: packet to be sent
3818 * @dev: incoming interface
3820 * On failure skb will be freed.
3822 netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
3823 struct net_device *dev)
3825 if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
3826 struct sk_buff_head queue;
3828 __skb_queue_head_init(&queue);
3829 ieee80211_convert_to_unicast(skb, dev, &queue);
3830 while ((skb = __skb_dequeue(&queue)))
3831 __ieee80211_subif_start_xmit(skb, dev, 0, 0);
3832 } else {
3833 __ieee80211_subif_start_xmit(skb, dev, 0, 0);
3836 return NETDEV_TX_OK;
3839 struct sk_buff *
3840 ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
3841 struct sk_buff *skb, u32 info_flags)
3843 struct ieee80211_hdr *hdr;
3844 struct ieee80211_tx_data tx = {
3845 .local = sdata->local,
3846 .sdata = sdata,
3848 struct sta_info *sta;
3850 rcu_read_lock();
3852 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
3853 kfree_skb(skb);
3854 skb = ERR_PTR(-EINVAL);
3855 goto out;
3858 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta, 0);
3859 if (IS_ERR(skb))
3860 goto out;
3862 hdr = (void *)skb->data;
3863 tx.sta = sta_info_get(sdata, hdr->addr1);
3864 tx.skb = skb;
3866 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
3867 rcu_read_unlock();
3868 kfree_skb(skb);
3869 return ERR_PTR(-EINVAL);
3872 out:
3873 rcu_read_unlock();
3874 return skb;
3878 * ieee80211_clear_tx_pending may not be called in a context where
3879 * it is possible that it packets could come in again.
3881 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
3883 struct sk_buff *skb;
3884 int i;
3886 for (i = 0; i < local->hw.queues; i++) {
3887 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
3888 ieee80211_free_txskb(&local->hw, skb);
3893 * Returns false if the frame couldn't be transmitted but was queued instead,
3894 * which in this case means re-queued -- take as an indication to stop sending
3895 * more pending frames.
3897 static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
3898 struct sk_buff *skb)
3900 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3901 struct ieee80211_sub_if_data *sdata;
3902 struct sta_info *sta;
3903 struct ieee80211_hdr *hdr;
3904 bool result;
3905 struct ieee80211_chanctx_conf *chanctx_conf;
3907 sdata = vif_to_sdata(info->control.vif);
3909 if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
3910 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3911 if (unlikely(!chanctx_conf)) {
3912 dev_kfree_skb(skb);
3913 return true;
3915 info->band = chanctx_conf->def.chan->band;
3916 result = ieee80211_tx(sdata, NULL, skb, true, 0);
3917 } else {
3918 struct sk_buff_head skbs;
3920 __skb_queue_head_init(&skbs);
3921 __skb_queue_tail(&skbs, skb);
3923 hdr = (struct ieee80211_hdr *)skb->data;
3924 sta = sta_info_get(sdata, hdr->addr1);
3926 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
3929 return result;
3933 * Transmit all pending packets. Called from tasklet.
3935 void ieee80211_tx_pending(unsigned long data)
3937 struct ieee80211_local *local = (struct ieee80211_local *)data;
3938 unsigned long flags;
3939 int i;
3940 bool txok;
3942 rcu_read_lock();
3944 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
3945 for (i = 0; i < local->hw.queues; i++) {
3947 * If queue is stopped by something other than due to pending
3948 * frames, or we have no pending frames, proceed to next queue.
3950 if (local->queue_stop_reasons[i] ||
3951 skb_queue_empty(&local->pending[i]))
3952 continue;
3954 while (!skb_queue_empty(&local->pending[i])) {
3955 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
3956 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3958 if (WARN_ON(!info->control.vif)) {
3959 ieee80211_free_txskb(&local->hw, skb);
3960 continue;
3963 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
3964 flags);
3966 txok = ieee80211_tx_pending_skb(local, skb);
3967 spin_lock_irqsave(&local->queue_stop_reason_lock,
3968 flags);
3969 if (!txok)
3970 break;
3973 if (skb_queue_empty(&local->pending[i]))
3974 ieee80211_propagate_queue_wake(local, i);
3976 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
3978 rcu_read_unlock();
3981 /* functions for drivers to get certain frames */
3983 static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
3984 struct ps_data *ps, struct sk_buff *skb,
3985 bool is_template)
3987 u8 *pos, *tim;
3988 int aid0 = 0;
3989 int i, have_bits = 0, n1, n2;
3991 /* Generate bitmap for TIM only if there are any STAs in power save
3992 * mode. */
3993 if (atomic_read(&ps->num_sta_ps) > 0)
3994 /* in the hope that this is faster than
3995 * checking byte-for-byte */
3996 have_bits = !bitmap_empty((unsigned long *)ps->tim,
3997 IEEE80211_MAX_AID+1);
3998 if (!is_template) {
3999 if (ps->dtim_count == 0)
4000 ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
4001 else
4002 ps->dtim_count--;
4005 tim = pos = skb_put(skb, 6);
4006 *pos++ = WLAN_EID_TIM;
4007 *pos++ = 4;
4008 *pos++ = ps->dtim_count;
4009 *pos++ = sdata->vif.bss_conf.dtim_period;
4011 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
4012 aid0 = 1;
4014 ps->dtim_bc_mc = aid0 == 1;
4016 if (have_bits) {
4017 /* Find largest even number N1 so that bits numbered 1 through
4018 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
4019 * (N2 + 1) x 8 through 2007 are 0. */
4020 n1 = 0;
4021 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
4022 if (ps->tim[i]) {
4023 n1 = i & 0xfe;
4024 break;
4027 n2 = n1;
4028 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
4029 if (ps->tim[i]) {
4030 n2 = i;
4031 break;
4035 /* Bitmap control */
4036 *pos++ = n1 | aid0;
4037 /* Part Virt Bitmap */
4038 skb_put(skb, n2 - n1);
4039 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
4041 tim[1] = n2 - n1 + 4;
4042 } else {
4043 *pos++ = aid0; /* Bitmap control */
4044 *pos++ = 0; /* Part Virt Bitmap */
4048 static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
4049 struct ps_data *ps, struct sk_buff *skb,
4050 bool is_template)
4052 struct ieee80211_local *local = sdata->local;
4055 * Not very nice, but we want to allow the driver to call
4056 * ieee80211_beacon_get() as a response to the set_tim()
4057 * callback. That, however, is already invoked under the
4058 * sta_lock to guarantee consistent and race-free update
4059 * of the tim bitmap in mac80211 and the driver.
4061 if (local->tim_in_locked_section) {
4062 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
4063 } else {
4064 spin_lock_bh(&local->tim_lock);
4065 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
4066 spin_unlock_bh(&local->tim_lock);
4069 return 0;
4072 static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
4073 struct beacon_data *beacon)
4075 struct probe_resp *resp;
4076 u8 *beacon_data;
4077 size_t beacon_data_len;
4078 int i;
4079 u8 count = beacon->csa_current_counter;
4081 switch (sdata->vif.type) {
4082 case NL80211_IFTYPE_AP:
4083 beacon_data = beacon->tail;
4084 beacon_data_len = beacon->tail_len;
4085 break;
4086 case NL80211_IFTYPE_ADHOC:
4087 beacon_data = beacon->head;
4088 beacon_data_len = beacon->head_len;
4089 break;
4090 case NL80211_IFTYPE_MESH_POINT:
4091 beacon_data = beacon->head;
4092 beacon_data_len = beacon->head_len;
4093 break;
4094 default:
4095 return;
4098 rcu_read_lock();
4099 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
4100 resp = rcu_dereference(sdata->u.ap.probe_resp);
4102 if (beacon->csa_counter_offsets[i]) {
4103 if (WARN_ON_ONCE(beacon->csa_counter_offsets[i] >=
4104 beacon_data_len)) {
4105 rcu_read_unlock();
4106 return;
4109 beacon_data[beacon->csa_counter_offsets[i]] = count;
4112 if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
4113 resp->data[resp->csa_counter_offsets[i]] = count;
4115 rcu_read_unlock();
4118 static u8 __ieee80211_csa_update_counter(struct beacon_data *beacon)
4120 beacon->csa_current_counter--;
4122 /* the counter should never reach 0 */
4123 WARN_ON_ONCE(!beacon->csa_current_counter);
4125 return beacon->csa_current_counter;
4128 u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
4130 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4131 struct beacon_data *beacon = NULL;
4132 u8 count = 0;
4134 rcu_read_lock();
4136 if (sdata->vif.type == NL80211_IFTYPE_AP)
4137 beacon = rcu_dereference(sdata->u.ap.beacon);
4138 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4139 beacon = rcu_dereference(sdata->u.ibss.presp);
4140 else if (ieee80211_vif_is_mesh(&sdata->vif))
4141 beacon = rcu_dereference(sdata->u.mesh.beacon);
4143 if (!beacon)
4144 goto unlock;
4146 count = __ieee80211_csa_update_counter(beacon);
4148 unlock:
4149 rcu_read_unlock();
4150 return count;
4152 EXPORT_SYMBOL(ieee80211_csa_update_counter);
4154 void ieee80211_csa_set_counter(struct ieee80211_vif *vif, u8 counter)
4156 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4157 struct beacon_data *beacon = NULL;
4159 rcu_read_lock();
4161 if (sdata->vif.type == NL80211_IFTYPE_AP)
4162 beacon = rcu_dereference(sdata->u.ap.beacon);
4163 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4164 beacon = rcu_dereference(sdata->u.ibss.presp);
4165 else if (ieee80211_vif_is_mesh(&sdata->vif))
4166 beacon = rcu_dereference(sdata->u.mesh.beacon);
4168 if (!beacon)
4169 goto unlock;
4171 if (counter < beacon->csa_current_counter)
4172 beacon->csa_current_counter = counter;
4174 unlock:
4175 rcu_read_unlock();
4177 EXPORT_SYMBOL(ieee80211_csa_set_counter);
4179 bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
4181 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4182 struct beacon_data *beacon = NULL;
4183 u8 *beacon_data;
4184 size_t beacon_data_len;
4185 int ret = false;
4187 if (!ieee80211_sdata_running(sdata))
4188 return false;
4190 rcu_read_lock();
4191 if (vif->type == NL80211_IFTYPE_AP) {
4192 struct ieee80211_if_ap *ap = &sdata->u.ap;
4194 beacon = rcu_dereference(ap->beacon);
4195 if (WARN_ON(!beacon || !beacon->tail))
4196 goto out;
4197 beacon_data = beacon->tail;
4198 beacon_data_len = beacon->tail_len;
4199 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
4200 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4202 beacon = rcu_dereference(ifibss->presp);
4203 if (!beacon)
4204 goto out;
4206 beacon_data = beacon->head;
4207 beacon_data_len = beacon->head_len;
4208 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
4209 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4211 beacon = rcu_dereference(ifmsh->beacon);
4212 if (!beacon)
4213 goto out;
4215 beacon_data = beacon->head;
4216 beacon_data_len = beacon->head_len;
4217 } else {
4218 WARN_ON(1);
4219 goto out;
4222 if (!beacon->csa_counter_offsets[0])
4223 goto out;
4225 if (WARN_ON_ONCE(beacon->csa_counter_offsets[0] > beacon_data_len))
4226 goto out;
4228 if (beacon_data[beacon->csa_counter_offsets[0]] == 1)
4229 ret = true;
4230 out:
4231 rcu_read_unlock();
4233 return ret;
4235 EXPORT_SYMBOL(ieee80211_csa_is_complete);
4237 static struct sk_buff *
4238 __ieee80211_beacon_get(struct ieee80211_hw *hw,
4239 struct ieee80211_vif *vif,
4240 struct ieee80211_mutable_offsets *offs,
4241 bool is_template)
4243 struct ieee80211_local *local = hw_to_local(hw);
4244 struct beacon_data *beacon = NULL;
4245 struct sk_buff *skb = NULL;
4246 struct ieee80211_tx_info *info;
4247 struct ieee80211_sub_if_data *sdata = NULL;
4248 enum nl80211_band band;
4249 struct ieee80211_tx_rate_control txrc;
4250 struct ieee80211_chanctx_conf *chanctx_conf;
4251 int csa_off_base = 0;
4253 rcu_read_lock();
4255 sdata = vif_to_sdata(vif);
4256 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4258 if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
4259 goto out;
4261 if (offs)
4262 memset(offs, 0, sizeof(*offs));
4264 if (sdata->vif.type == NL80211_IFTYPE_AP) {
4265 struct ieee80211_if_ap *ap = &sdata->u.ap;
4267 beacon = rcu_dereference(ap->beacon);
4268 if (beacon) {
4269 if (beacon->csa_counter_offsets[0]) {
4270 if (!is_template)
4271 __ieee80211_csa_update_counter(beacon);
4273 ieee80211_set_csa(sdata, beacon);
4277 * headroom, head length,
4278 * tail length and maximum TIM length
4280 skb = dev_alloc_skb(local->tx_headroom +
4281 beacon->head_len +
4282 beacon->tail_len + 256 +
4283 local->hw.extra_beacon_tailroom);
4284 if (!skb)
4285 goto out;
4287 skb_reserve(skb, local->tx_headroom);
4288 skb_put_data(skb, beacon->head, beacon->head_len);
4290 ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
4291 is_template);
4293 if (offs) {
4294 offs->tim_offset = beacon->head_len;
4295 offs->tim_length = skb->len - beacon->head_len;
4297 /* for AP the csa offsets are from tail */
4298 csa_off_base = skb->len;
4301 if (beacon->tail)
4302 skb_put_data(skb, beacon->tail,
4303 beacon->tail_len);
4304 } else
4305 goto out;
4306 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
4307 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4308 struct ieee80211_hdr *hdr;
4310 beacon = rcu_dereference(ifibss->presp);
4311 if (!beacon)
4312 goto out;
4314 if (beacon->csa_counter_offsets[0]) {
4315 if (!is_template)
4316 __ieee80211_csa_update_counter(beacon);
4318 ieee80211_set_csa(sdata, beacon);
4321 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
4322 local->hw.extra_beacon_tailroom);
4323 if (!skb)
4324 goto out;
4325 skb_reserve(skb, local->tx_headroom);
4326 skb_put_data(skb, beacon->head, beacon->head_len);
4328 hdr = (struct ieee80211_hdr *) skb->data;
4329 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4330 IEEE80211_STYPE_BEACON);
4331 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4332 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4334 beacon = rcu_dereference(ifmsh->beacon);
4335 if (!beacon)
4336 goto out;
4338 if (beacon->csa_counter_offsets[0]) {
4339 if (!is_template)
4340 /* TODO: For mesh csa_counter is in TU, so
4341 * decrementing it by one isn't correct, but
4342 * for now we leave it consistent with overall
4343 * mac80211's behavior.
4345 __ieee80211_csa_update_counter(beacon);
4347 ieee80211_set_csa(sdata, beacon);
4350 if (ifmsh->sync_ops)
4351 ifmsh->sync_ops->adjust_tsf(sdata, beacon);
4353 skb = dev_alloc_skb(local->tx_headroom +
4354 beacon->head_len +
4355 256 + /* TIM IE */
4356 beacon->tail_len +
4357 local->hw.extra_beacon_tailroom);
4358 if (!skb)
4359 goto out;
4360 skb_reserve(skb, local->tx_headroom);
4361 skb_put_data(skb, beacon->head, beacon->head_len);
4362 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
4364 if (offs) {
4365 offs->tim_offset = beacon->head_len;
4366 offs->tim_length = skb->len - beacon->head_len;
4369 skb_put_data(skb, beacon->tail, beacon->tail_len);
4370 } else {
4371 WARN_ON(1);
4372 goto out;
4375 /* CSA offsets */
4376 if (offs && beacon) {
4377 int i;
4379 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
4380 u16 csa_off = beacon->csa_counter_offsets[i];
4382 if (!csa_off)
4383 continue;
4385 offs->csa_counter_offs[i] = csa_off_base + csa_off;
4389 band = chanctx_conf->def.chan->band;
4391 info = IEEE80211_SKB_CB(skb);
4393 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
4394 info->flags |= IEEE80211_TX_CTL_NO_ACK;
4395 info->band = band;
4397 memset(&txrc, 0, sizeof(txrc));
4398 txrc.hw = hw;
4399 txrc.sband = local->hw.wiphy->bands[band];
4400 txrc.bss_conf = &sdata->vif.bss_conf;
4401 txrc.skb = skb;
4402 txrc.reported_rate.idx = -1;
4403 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
4404 txrc.bss = true;
4405 rate_control_get_rate(sdata, NULL, &txrc);
4407 info->control.vif = vif;
4409 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
4410 IEEE80211_TX_CTL_ASSIGN_SEQ |
4411 IEEE80211_TX_CTL_FIRST_FRAGMENT;
4412 out:
4413 rcu_read_unlock();
4414 return skb;
4418 struct sk_buff *
4419 ieee80211_beacon_get_template(struct ieee80211_hw *hw,
4420 struct ieee80211_vif *vif,
4421 struct ieee80211_mutable_offsets *offs)
4423 return __ieee80211_beacon_get(hw, vif, offs, true);
4425 EXPORT_SYMBOL(ieee80211_beacon_get_template);
4427 struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
4428 struct ieee80211_vif *vif,
4429 u16 *tim_offset, u16 *tim_length)
4431 struct ieee80211_mutable_offsets offs = {};
4432 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
4433 struct sk_buff *copy;
4434 struct ieee80211_supported_band *sband;
4435 int shift;
4437 if (!bcn)
4438 return bcn;
4440 if (tim_offset)
4441 *tim_offset = offs.tim_offset;
4443 if (tim_length)
4444 *tim_length = offs.tim_length;
4446 if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
4447 !hw_to_local(hw)->monitors)
4448 return bcn;
4450 /* send a copy to monitor interfaces */
4451 copy = skb_copy(bcn, GFP_ATOMIC);
4452 if (!copy)
4453 return bcn;
4455 shift = ieee80211_vif_get_shift(vif);
4456 sband = ieee80211_get_sband(vif_to_sdata(vif));
4457 if (!sband)
4458 return bcn;
4460 ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false);
4462 return bcn;
4464 EXPORT_SYMBOL(ieee80211_beacon_get_tim);
4466 struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
4467 struct ieee80211_vif *vif)
4469 struct ieee80211_if_ap *ap = NULL;
4470 struct sk_buff *skb = NULL;
4471 struct probe_resp *presp = NULL;
4472 struct ieee80211_hdr *hdr;
4473 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4475 if (sdata->vif.type != NL80211_IFTYPE_AP)
4476 return NULL;
4478 rcu_read_lock();
4480 ap = &sdata->u.ap;
4481 presp = rcu_dereference(ap->probe_resp);
4482 if (!presp)
4483 goto out;
4485 skb = dev_alloc_skb(presp->len);
4486 if (!skb)
4487 goto out;
4489 skb_put_data(skb, presp->data, presp->len);
4491 hdr = (struct ieee80211_hdr *) skb->data;
4492 memset(hdr->addr1, 0, sizeof(hdr->addr1));
4494 out:
4495 rcu_read_unlock();
4496 return skb;
4498 EXPORT_SYMBOL(ieee80211_proberesp_get);
4500 struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
4501 struct ieee80211_vif *vif)
4503 struct ieee80211_sub_if_data *sdata;
4504 struct ieee80211_if_managed *ifmgd;
4505 struct ieee80211_pspoll *pspoll;
4506 struct ieee80211_local *local;
4507 struct sk_buff *skb;
4509 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4510 return NULL;
4512 sdata = vif_to_sdata(vif);
4513 ifmgd = &sdata->u.mgd;
4514 local = sdata->local;
4516 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
4517 if (!skb)
4518 return NULL;
4520 skb_reserve(skb, local->hw.extra_tx_headroom);
4522 pspoll = skb_put_zero(skb, sizeof(*pspoll));
4523 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
4524 IEEE80211_STYPE_PSPOLL);
4525 pspoll->aid = cpu_to_le16(ifmgd->aid);
4527 /* aid in PS-Poll has its two MSBs each set to 1 */
4528 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
4530 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
4531 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
4533 return skb;
4535 EXPORT_SYMBOL(ieee80211_pspoll_get);
4537 struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
4538 struct ieee80211_vif *vif,
4539 bool qos_ok)
4541 struct ieee80211_hdr_3addr *nullfunc;
4542 struct ieee80211_sub_if_data *sdata;
4543 struct ieee80211_if_managed *ifmgd;
4544 struct ieee80211_local *local;
4545 struct sk_buff *skb;
4546 bool qos = false;
4548 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4549 return NULL;
4551 sdata = vif_to_sdata(vif);
4552 ifmgd = &sdata->u.mgd;
4553 local = sdata->local;
4555 if (qos_ok) {
4556 struct sta_info *sta;
4558 rcu_read_lock();
4559 sta = sta_info_get(sdata, ifmgd->bssid);
4560 qos = sta && sta->sta.wme;
4561 rcu_read_unlock();
4564 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
4565 sizeof(*nullfunc) + 2);
4566 if (!skb)
4567 return NULL;
4569 skb_reserve(skb, local->hw.extra_tx_headroom);
4571 nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
4572 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
4573 IEEE80211_STYPE_NULLFUNC |
4574 IEEE80211_FCTL_TODS);
4575 if (qos) {
4576 __le16 qos = cpu_to_le16(7);
4578 BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
4579 IEEE80211_STYPE_NULLFUNC) !=
4580 IEEE80211_STYPE_QOS_NULLFUNC);
4581 nullfunc->frame_control |=
4582 cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
4583 skb->priority = 7;
4584 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
4585 skb_put_data(skb, &qos, sizeof(qos));
4588 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
4589 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
4590 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
4592 return skb;
4594 EXPORT_SYMBOL(ieee80211_nullfunc_get);
4596 struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
4597 const u8 *src_addr,
4598 const u8 *ssid, size_t ssid_len,
4599 size_t tailroom)
4601 struct ieee80211_local *local = hw_to_local(hw);
4602 struct ieee80211_hdr_3addr *hdr;
4603 struct sk_buff *skb;
4604 size_t ie_ssid_len;
4605 u8 *pos;
4607 ie_ssid_len = 2 + ssid_len;
4609 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
4610 ie_ssid_len + tailroom);
4611 if (!skb)
4612 return NULL;
4614 skb_reserve(skb, local->hw.extra_tx_headroom);
4616 hdr = skb_put_zero(skb, sizeof(*hdr));
4617 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4618 IEEE80211_STYPE_PROBE_REQ);
4619 eth_broadcast_addr(hdr->addr1);
4620 memcpy(hdr->addr2, src_addr, ETH_ALEN);
4621 eth_broadcast_addr(hdr->addr3);
4623 pos = skb_put(skb, ie_ssid_len);
4624 *pos++ = WLAN_EID_SSID;
4625 *pos++ = ssid_len;
4626 if (ssid_len)
4627 memcpy(pos, ssid, ssid_len);
4628 pos += ssid_len;
4630 return skb;
4632 EXPORT_SYMBOL(ieee80211_probereq_get);
4634 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4635 const void *frame, size_t frame_len,
4636 const struct ieee80211_tx_info *frame_txctl,
4637 struct ieee80211_rts *rts)
4639 const struct ieee80211_hdr *hdr = frame;
4641 rts->frame_control =
4642 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
4643 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
4644 frame_txctl);
4645 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
4646 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
4648 EXPORT_SYMBOL(ieee80211_rts_get);
4650 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4651 const void *frame, size_t frame_len,
4652 const struct ieee80211_tx_info *frame_txctl,
4653 struct ieee80211_cts *cts)
4655 const struct ieee80211_hdr *hdr = frame;
4657 cts->frame_control =
4658 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
4659 cts->duration = ieee80211_ctstoself_duration(hw, vif,
4660 frame_len, frame_txctl);
4661 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
4663 EXPORT_SYMBOL(ieee80211_ctstoself_get);
4665 struct sk_buff *
4666 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
4667 struct ieee80211_vif *vif)
4669 struct ieee80211_local *local = hw_to_local(hw);
4670 struct sk_buff *skb = NULL;
4671 struct ieee80211_tx_data tx;
4672 struct ieee80211_sub_if_data *sdata;
4673 struct ps_data *ps;
4674 struct ieee80211_tx_info *info;
4675 struct ieee80211_chanctx_conf *chanctx_conf;
4677 sdata = vif_to_sdata(vif);
4679 rcu_read_lock();
4680 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4682 if (!chanctx_conf)
4683 goto out;
4685 if (sdata->vif.type == NL80211_IFTYPE_AP) {
4686 struct beacon_data *beacon =
4687 rcu_dereference(sdata->u.ap.beacon);
4689 if (!beacon || !beacon->head)
4690 goto out;
4692 ps = &sdata->u.ap.ps;
4693 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4694 ps = &sdata->u.mesh.ps;
4695 } else {
4696 goto out;
4699 if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
4700 goto out; /* send buffered bc/mc only after DTIM beacon */
4702 while (1) {
4703 skb = skb_dequeue(&ps->bc_buf);
4704 if (!skb)
4705 goto out;
4706 local->total_ps_buffered--;
4708 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
4709 struct ieee80211_hdr *hdr =
4710 (struct ieee80211_hdr *) skb->data;
4711 /* more buffered multicast/broadcast frames ==> set
4712 * MoreData flag in IEEE 802.11 header to inform PS
4713 * STAs */
4714 hdr->frame_control |=
4715 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
4718 if (sdata->vif.type == NL80211_IFTYPE_AP)
4719 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
4720 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
4721 break;
4722 ieee80211_free_txskb(hw, skb);
4725 info = IEEE80211_SKB_CB(skb);
4727 tx.flags |= IEEE80211_TX_PS_BUFFERED;
4728 info->band = chanctx_conf->def.chan->band;
4730 if (invoke_tx_handlers(&tx))
4731 skb = NULL;
4732 out:
4733 rcu_read_unlock();
4735 return skb;
4737 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
4739 int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
4741 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
4742 struct ieee80211_sub_if_data *sdata = sta->sdata;
4743 struct ieee80211_local *local = sdata->local;
4744 int ret;
4745 u32 queues;
4747 lockdep_assert_held(&local->sta_mtx);
4749 /* only some cases are supported right now */
4750 switch (sdata->vif.type) {
4751 case NL80211_IFTYPE_STATION:
4752 case NL80211_IFTYPE_AP:
4753 case NL80211_IFTYPE_AP_VLAN:
4754 break;
4755 default:
4756 WARN_ON(1);
4757 return -EINVAL;
4760 if (WARN_ON(tid >= IEEE80211_NUM_UPS))
4761 return -EINVAL;
4763 if (sta->reserved_tid == tid) {
4764 ret = 0;
4765 goto out;
4768 if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
4769 sdata_err(sdata, "TID reservation already active\n");
4770 ret = -EALREADY;
4771 goto out;
4774 ieee80211_stop_vif_queues(sdata->local, sdata,
4775 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
4777 synchronize_net();
4779 /* Tear down BA sessions so we stop aggregating on this TID */
4780 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
4781 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
4782 __ieee80211_stop_tx_ba_session(sta, tid,
4783 AGG_STOP_LOCAL_REQUEST);
4786 queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
4787 __ieee80211_flush_queues(local, sdata, queues, false);
4789 sta->reserved_tid = tid;
4791 ieee80211_wake_vif_queues(local, sdata,
4792 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
4794 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
4795 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
4797 ret = 0;
4798 out:
4799 return ret;
4801 EXPORT_SYMBOL(ieee80211_reserve_tid);
4803 void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
4805 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
4806 struct ieee80211_sub_if_data *sdata = sta->sdata;
4808 lockdep_assert_held(&sdata->local->sta_mtx);
4810 /* only some cases are supported right now */
4811 switch (sdata->vif.type) {
4812 case NL80211_IFTYPE_STATION:
4813 case NL80211_IFTYPE_AP:
4814 case NL80211_IFTYPE_AP_VLAN:
4815 break;
4816 default:
4817 WARN_ON(1);
4818 return;
4821 if (tid != sta->reserved_tid) {
4822 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
4823 return;
4826 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
4828 EXPORT_SYMBOL(ieee80211_unreserve_tid);
4830 void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
4831 struct sk_buff *skb, int tid,
4832 enum nl80211_band band, u32 txdata_flags)
4834 int ac = ieee80211_ac_from_tid(tid);
4836 skb_reset_mac_header(skb);
4837 skb_set_queue_mapping(skb, ac);
4838 skb->priority = tid;
4840 skb->dev = sdata->dev;
4843 * The other path calling ieee80211_xmit is from the tasklet,
4844 * and while we can handle concurrent transmissions locking
4845 * requirements are that we do not come into tx with bhs on.
4847 local_bh_disable();
4848 IEEE80211_SKB_CB(skb)->band = band;
4849 ieee80211_xmit(sdata, NULL, skb, txdata_flags);
4850 local_bh_enable();
4853 int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
4854 const u8 *buf, size_t len,
4855 const u8 *dest, __be16 proto, bool unencrypted)
4857 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4858 struct ieee80211_local *local = sdata->local;
4859 struct sk_buff *skb;
4860 struct ethhdr *ehdr;
4861 u32 ctrl_flags = 0;
4862 u32 flags;
4864 /* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
4865 * or Pre-Authentication
4867 if (proto != sdata->control_port_protocol &&
4868 proto != cpu_to_be16(ETH_P_PREAUTH))
4869 return -EINVAL;
4871 if (proto == sdata->control_port_protocol)
4872 ctrl_flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
4874 if (unencrypted)
4875 flags = IEEE80211_TX_INTFL_DONT_ENCRYPT;
4876 else
4877 flags = 0;
4879 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
4880 sizeof(struct ethhdr) + len);
4881 if (!skb)
4882 return -ENOMEM;
4884 skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
4886 skb_put_data(skb, buf, len);
4888 ehdr = skb_push(skb, sizeof(struct ethhdr));
4889 memcpy(ehdr->h_dest, dest, ETH_ALEN);
4890 memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
4891 ehdr->h_proto = proto;
4893 skb->dev = dev;
4894 skb->protocol = htons(ETH_P_802_3);
4895 skb_reset_network_header(skb);
4896 skb_reset_mac_header(skb);
4898 local_bh_disable();
4899 __ieee80211_subif_start_xmit(skb, skb->dev, flags, ctrl_flags);
4900 local_bh_enable();
4902 return 0;