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