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