wifi: mac80211: properly implement MLO key handling
[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
JB
490 if (ieee80211_is_mgmt(hdr->frame_control) &&
491 !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
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)) {
1132 info->flags |= IEEE80211_TX_CTL_AMPDU;
285fa695 1133 reset_agg_timer = true;
0ab33703
JB
1134 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1135 /*
1136 * nothing -- this aggregation session is being started
1137 * but that might still fail with the driver
1138 */
ba8c3d6f 1139 } else if (!tx->sta->sta.txq[tid]) {
a622ab72
JB
1140 spin_lock(&tx->sta->lock);
1141 /*
1142 * Need to re-check now, because we may get here
1143 *
1144 * 1) in the window during which the setup is actually
1145 * already done, but not marked yet because not all
1146 * packets are spliced over to the driver pending
1147 * queue yet -- if this happened we acquire the lock
1148 * either before or after the splice happens, but
1149 * need to recheck which of these cases happened.
1150 *
1151 * 2) during session teardown, if the OPERATIONAL bit
1152 * was cleared due to the teardown but the pointer
1153 * hasn't been assigned NULL yet (or we loaded it
1154 * before it was assigned) -- in this case it may
1155 * now be NULL which means we should just let the
1156 * packet pass through because splicing the frames
1157 * back is already done.
1158 */
40b275b6 1159 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
a622ab72
JB
1160
1161 if (!tid_tx) {
1162 /* do nothing, let packet pass through */
1163 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1164 info->flags |= IEEE80211_TX_CTL_AMPDU;
285fa695 1165 reset_agg_timer = true;
a622ab72
JB
1166 } else {
1167 queued = true;
f6d4671a
EG
1168 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
1169 clear_sta_flag(tx->sta, WLAN_STA_SP);
1170 ps_dbg(tx->sta->sdata,
1171 "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
1172 tx->sta->sta.addr, tx->sta->sta.aid);
1173 }
a622ab72 1174 info->control.vif = &tx->sdata->vif;
cc20ff2c 1175 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
facde7f3 1176 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
a622ab72 1177 __skb_queue_tail(&tid_tx->pending, skb);
aa454580
HS
1178 if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1179 purge_skb = __skb_dequeue(&tid_tx->pending);
a622ab72
JB
1180 }
1181 spin_unlock(&tx->sta->lock);
aa454580
HS
1182
1183 if (purge_skb)
c3e7724b 1184 ieee80211_free_txskb(&tx->local->hw, purge_skb);
a622ab72
JB
1185 }
1186
285fa695 1187 /* reset session timer */
914eac24 1188 if (reset_agg_timer)
12d3952f 1189 tid_tx->last_tx = jiffies;
285fa695 1190
a622ab72
JB
1191 return queued;
1192}
1193
1a791550
FF
1194static void
1195ieee80211_aggr_check(struct ieee80211_sub_if_data *sdata,
1196 struct sta_info *sta,
1197 struct sk_buff *skb)
1198{
1199 struct rate_control_ref *ref = sdata->local->rate_ctrl;
1200 u16 tid;
1201
1202 if (!ref || !(ref->ops->capa & RATE_CTRL_CAPA_AMPDU_TRIGGER))
1203 return;
1204
046d2e7c 1205 if (!sta || !sta->sta.deflink.ht_cap.ht_supported ||
1a791550
FF
1206 !sta->sta.wme || skb_get_queue_mapping(skb) == IEEE80211_AC_VO ||
1207 skb->protocol == sdata->control_port_protocol)
1208 return;
1209
1210 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1211 if (likely(sta->ampdu_mlme.tid_tx[tid]))
1212 return;
1213
1214 ieee80211_start_tx_ba_session(&sta->sta, tid, 0);
1215}
1216
58d4185e
JB
1217/*
1218 * initialises @tx
7c10770f
JB
1219 * pass %NULL for the station if unknown, a valid pointer if known
1220 * or an ERR_PTR() if the station is known not to exist
58d4185e 1221 */
9ae54c84 1222static ieee80211_tx_result
3b8d81e0
JB
1223ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1224 struct ieee80211_tx_data *tx,
7c10770f 1225 struct sta_info *sta, struct sk_buff *skb)
e2ebc74d 1226{
3b8d81e0 1227 struct ieee80211_local *local = sdata->local;
58d4185e 1228 struct ieee80211_hdr *hdr;
e039fa4a 1229 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1a791550 1230 bool aggr_check = false;
68f2b517 1231 int tid;
e2ebc74d
JB
1232
1233 memset(tx, 0, sizeof(*tx));
1234 tx->skb = skb;
e2ebc74d 1235 tx->local = local;
3b8d81e0 1236 tx->sdata = sdata;
252b86c4 1237 __skb_queue_head_init(&tx->skbs);
e2ebc74d 1238
cd8ffc80
JB
1239 /*
1240 * If this flag is set to true anywhere, and we get here,
1241 * we are doing the needed processing, so remove the flag
1242 * now.
1243 */
cc20ff2c 1244 info->control.flags &= ~IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
cd8ffc80 1245
58d4185e
JB
1246 hdr = (struct ieee80211_hdr *) skb->data;
1247
7c10770f
JB
1248 if (likely(sta)) {
1249 if (!IS_ERR(sta))
1250 tx->sta = sta;
1251 } else {
1252 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1253 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1254 if (!tx->sta && sdata->wdev.use_4addr)
1255 return TX_DROP;
10cb8e61 1256 } else if (tx->sdata->control_port_protocol == tx->skb->protocol) {
7c10770f
JB
1257 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1258 }
1a791550 1259 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1)) {
7c10770f 1260 tx->sta = sta_info_get(sdata, hdr->addr1);
1a791550
FF
1261 aggr_check = true;
1262 }
3f0e0b22 1263 }
58d4185e 1264
cd8ffc80 1265 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
49a59543 1266 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
30686bf7
JB
1267 ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
1268 !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
cd8ffc80
JB
1269 struct tid_ampdu_tx *tid_tx;
1270
a1f2ba04 1271 tid = ieee80211_get_tid(hdr);
a622ab72 1272 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1a791550
FF
1273 if (!tid_tx && aggr_check) {
1274 ieee80211_aggr_check(sdata, tx->sta, skb);
1275 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1276 }
1277
a622ab72
JB
1278 if (tid_tx) {
1279 bool queued;
cd8ffc80 1280
a622ab72
JB
1281 queued = ieee80211_tx_prep_agg(tx, skb, info,
1282 tid_tx, tid);
1283
1284 if (unlikely(queued))
1285 return TX_QUEUED;
1286 }
8b30b1fe
S
1287 }
1288
badffb72 1289 if (is_multicast_ether_addr(hdr->addr1)) {
5cf121c3 1290 tx->flags &= ~IEEE80211_TX_UNICAST;
e039fa4a 1291 info->flags |= IEEE80211_TX_CTL_NO_ACK;
6fd67e93 1292 } else
5cf121c3 1293 tx->flags |= IEEE80211_TX_UNICAST;
58d4185e 1294
a26eb27a
JB
1295 if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1296 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1297 skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1298 info->flags & IEEE80211_TX_CTL_AMPDU)
1299 info->flags |= IEEE80211_TX_CTL_DONTFRAG;
58d4185e
JB
1300 }
1301
e2ebc74d 1302 if (!tx->sta)
e039fa4a 1303 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
f7418bc1 1304 else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
e039fa4a 1305 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
f7418bc1
FF
1306 ieee80211_check_fast_xmit(tx->sta);
1307 }
58d4185e 1308
e039fa4a 1309 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
e2ebc74d 1310
9ae54c84 1311 return TX_CONTINUE;
e2ebc74d
JB
1312}
1313
80a83cfc
MK
1314static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
1315 struct ieee80211_vif *vif,
dbef5362 1316 struct sta_info *sta,
80a83cfc 1317 struct sk_buff *skb)
ba8c3d6f
FF
1318{
1319 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
ba8c3d6f 1320 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
ba8c3d6f 1321 struct ieee80211_txq *txq = NULL;
ba8c3d6f 1322
c3732a7b
FF
1323 if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
1324 (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
80a83cfc 1325 return NULL;
ba8c3d6f 1326
cc20ff2c 1327 if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) &&
50ff477a 1328 unlikely(!ieee80211_is_data_present(hdr->frame_control))) {
adf8ed01 1329 if ((!ieee80211_is_mgmt(hdr->frame_control) ||
0eeb2b67
SS
1330 ieee80211_is_bufferable_mmpdu(hdr->frame_control) ||
1331 vif->type == NL80211_IFTYPE_STATION) &&
adf8ed01
JB
1332 sta && sta->uploaded) {
1333 /*
1334 * This will be NULL if the driver didn't set the
1335 * opt-in hardware flag.
1336 */
1337 txq = sta->sta.txq[IEEE80211_NUM_TIDS];
1338 }
1339 } else if (sta) {
ba8c3d6f
FF
1340 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1341
dbef5362
MK
1342 if (!sta->uploaded)
1343 return NULL;
1344
1345 txq = sta->sta.txq[tid];
ba8c3d6f
FF
1346 } else if (vif) {
1347 txq = vif->txq;
1348 }
1349
1350 if (!txq)
80a83cfc 1351 return NULL;
ba8c3d6f 1352
80a83cfc
MK
1353 return to_txq_info(txq);
1354}
ba8c3d6f 1355
5caa328e
MK
1356static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
1357{
1358 IEEE80211_SKB_CB(skb)->control.enqueue_time = codel_get_time();
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
MK
1601
1602 if (!local->ops->wake_tx_queue)
1603 return 0;
1604
1605 ret = fq_init(fq, 4096);
1606 if (ret)
1607 return ret;
1608
3ff23cd5
THJ
1609 /*
1610 * If the hardware doesn't support VHT, it is safe to limit the maximum
1611 * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
1612 */
1613 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1614 struct ieee80211_supported_band *sband;
1615
1616 sband = local->hw.wiphy->bands[band];
1617 if (!sband)
1618 continue;
1619
1620 supp_vht = supp_vht || sband->vht_cap.vht_supported;
1621 }
1622
1623 if (!supp_vht)
1624 fq->memory_limit = 4 << 20; /* 4 Mbytes */
1625
5caa328e 1626 codel_params_init(&local->cparams);
5caa328e
MK
1627 local->cparams.interval = MS2TIME(100);
1628 local->cparams.target = MS2TIME(20);
1629 local->cparams.ecn = true;
1630
1631 local->cvars = kcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
1632 GFP_KERNEL);
1633 if (!local->cvars) {
59a7c828 1634 spin_lock_bh(&fq->lock);
5caa328e 1635 fq_reset(fq, fq_skb_free_func);
59a7c828 1636 spin_unlock_bh(&fq->lock);
5caa328e
MK
1637 return -ENOMEM;
1638 }
1639
1640 for (i = 0; i < fq->flows_cnt; i++)
1641 codel_vars_init(&local->cvars[i]);
1642
2fe4a29a
THJ
1643 ieee80211_txq_set_params(local);
1644
fa962b92
MK
1645 return 0;
1646}
1647
1648void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1649{
1650 struct fq *fq = &local->fq;
1651
1652 if (!local->ops->wake_tx_queue)
1653 return;
1654
5caa328e
MK
1655 kfree(local->cvars);
1656 local->cvars = NULL;
1657
59a7c828 1658 spin_lock_bh(&fq->lock);
fa962b92 1659 fq_reset(fq, fq_skb_free_func);
59a7c828 1660 spin_unlock_bh(&fq->lock);
ba8c3d6f
FF
1661}
1662
bb42f2d1
THJ
1663static bool ieee80211_queue_skb(struct ieee80211_local *local,
1664 struct ieee80211_sub_if_data *sdata,
1665 struct sta_info *sta,
1666 struct sk_buff *skb)
1667{
bb42f2d1
THJ
1668 struct ieee80211_vif *vif;
1669 struct txq_info *txqi;
bb42f2d1
THJ
1670
1671 if (!local->ops->wake_tx_queue ||
1672 sdata->vif.type == NL80211_IFTYPE_MONITOR)
1673 return false;
1674
bb42f2d1
THJ
1675 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1676 sdata = container_of(sdata->bss,
1677 struct ieee80211_sub_if_data, u.ap);
1678
1679 vif = &sdata->vif;
dbef5362 1680 txqi = ieee80211_get_txq(local, vif, sta, skb);
bb42f2d1
THJ
1681
1682 if (!txqi)
1683 return false;
1684
bb42f2d1 1685 ieee80211_txq_enqueue(local, txqi, skb);
bb42f2d1 1686
18667600 1687 schedule_and_wake_txq(local, txqi);
bb42f2d1
THJ
1688
1689 return true;
1690}
1691
11127e91
JB
1692static bool ieee80211_tx_frags(struct ieee80211_local *local,
1693 struct ieee80211_vif *vif,
4fd0328d 1694 struct sta_info *sta,
11127e91
JB
1695 struct sk_buff_head *skbs,
1696 bool txpending)
e2ebc74d 1697{
80a83cfc 1698 struct ieee80211_tx_control control = {};
252b86c4 1699 struct sk_buff *skb, *tmp;
3b8d81e0 1700 unsigned long flags;
e2ebc74d 1701
252b86c4 1702 skb_queue_walk_safe(skbs, skb, tmp) {
3a25a8c8
JB
1703 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1704 int q = info->hw_queue;
1705
1706#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1707 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1708 __skb_unlink(skb, skbs);
c3e7724b 1709 ieee80211_free_txskb(&local->hw, skb);
3a25a8c8
JB
1710 continue;
1711 }
1712#endif
3b8d81e0
JB
1713
1714 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
3b8d81e0 1715 if (local->queue_stop_reasons[q] ||
7bb45683 1716 (!txpending && !skb_queue_empty(&local->pending[q]))) {
6c17b77b 1717 if (unlikely(info->flags &
a7679ed5
SF
1718 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1719 if (local->queue_stop_reasons[q] &
1720 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1721 /*
1722 * Drop off-channel frames if queues
1723 * are stopped for any reason other
1724 * than off-channel operation. Never
1725 * queue them.
1726 */
1727 spin_unlock_irqrestore(
1728 &local->queue_stop_reason_lock,
1729 flags);
1730 ieee80211_purge_tx_queue(&local->hw,
1731 skbs);
1732 return true;
1733 }
1734 } else {
1735
6c17b77b 1736 /*
a7679ed5
SF
1737 * Since queue is stopped, queue up frames for
1738 * later transmission from the tx-pending
1739 * tasklet when the queue is woken again.
6c17b77b 1740 */
a7679ed5
SF
1741 if (txpending)
1742 skb_queue_splice_init(skbs,
1743 &local->pending[q]);
1744 else
1745 skb_queue_splice_tail_init(skbs,
1746 &local->pending[q]);
1747
1748 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1749 flags);
1750 return false;
6c17b77b 1751 }
7bb45683 1752 }
3b8d81e0 1753 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
f8e79ddd 1754
11127e91 1755 info->control.vif = vif;
4fd0328d 1756 control.sta = sta ? &sta->sta : NULL;
f0e72851 1757
11127e91 1758 __skb_unlink(skb, skbs);
80a83cfc 1759 drv_tx(local, &control, skb);
11127e91 1760 }
5061b0c2 1761
11127e91
JB
1762 return true;
1763}
1764
1765/*
1766 * Returns false if the frame couldn't be transmitted but was queued instead.
1767 */
1768static bool __ieee80211_tx(struct ieee80211_local *local,
30f6cf96
FF
1769 struct sk_buff_head *skbs, struct sta_info *sta,
1770 bool txpending)
11127e91
JB
1771{
1772 struct ieee80211_tx_info *info;
1773 struct ieee80211_sub_if_data *sdata;
1774 struct ieee80211_vif *vif;
11127e91 1775 struct sk_buff *skb;
958574cb 1776 bool result;
5061b0c2 1777
11127e91
JB
1778 if (WARN_ON(skb_queue_empty(skbs)))
1779 return true;
ec25acc4 1780
11127e91 1781 skb = skb_peek(skbs);
11127e91
JB
1782 info = IEEE80211_SKB_CB(skb);
1783 sdata = vif_to_sdata(info->control.vif);
1784 if (sta && !sta->uploaded)
1785 sta = NULL;
1786
11127e91
JB
1787 switch (sdata->vif.type) {
1788 case NL80211_IFTYPE_MONITOR:
d8212184 1789 if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
c82b5a74
JB
1790 vif = &sdata->vif;
1791 break;
1792 }
4b6f1dd6 1793 sdata = rcu_dereference(local->monitor_sdata);
3a25a8c8 1794 if (sdata) {
4b6f1dd6 1795 vif = &sdata->vif;
3a25a8c8
JB
1796 info->hw_queue =
1797 vif->hw_queue[skb_get_queue_mapping(skb)];
30686bf7 1798 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
63b4d8b3 1799 ieee80211_purge_tx_queue(&local->hw, skbs);
3a25a8c8
JB
1800 return true;
1801 } else
4b6f1dd6 1802 vif = NULL;
11127e91
JB
1803 break;
1804 case NL80211_IFTYPE_AP_VLAN:
1805 sdata = container_of(sdata->bss,
1806 struct ieee80211_sub_if_data, u.ap);
fc0561dc 1807 fallthrough;
11127e91
JB
1808 default:
1809 vif = &sdata->vif;
1810 break;
e2ebc74d 1811 }
2de8e0d9 1812
4fd0328d 1813 result = ieee80211_tx_frags(local, vif, sta, skbs, txpending);
11127e91 1814
4db4e0a1 1815 WARN_ON_ONCE(!skb_queue_empty(skbs));
252b86c4 1816
11127e91 1817 return result;
e2ebc74d
JB
1818}
1819
97b045d6
JB
1820/*
1821 * Invoke TX handlers, return 0 on success and non-zero if the
1822 * frame was dropped or queued.
bb42f2d1
THJ
1823 *
1824 * The handlers are split into an early and late part. The latter is everything
1825 * that can be sensitive to reordering, and will be deferred to after packets
1826 * are dequeued from the intermediate queues (when they are enabled).
97b045d6 1827 */
bb42f2d1 1828static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx)
97b045d6 1829{
97b045d6 1830 ieee80211_tx_result res = TX_DROP;
97b045d6 1831
9aa4aee3
JB
1832#define CALL_TXH(txh) \
1833 do { \
1834 res = txh(tx); \
1835 if (res != TX_CONTINUE) \
1836 goto txh_done; \
1837 } while (0)
1838
5c1b98a5 1839 CALL_TXH(ieee80211_tx_h_dynamic_ps);
9aa4aee3
JB
1840 CALL_TXH(ieee80211_tx_h_check_assoc);
1841 CALL_TXH(ieee80211_tx_h_ps_buf);
a621fa4d 1842 CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
9aa4aee3 1843 CALL_TXH(ieee80211_tx_h_select_key);
c6fcf6bc 1844
bb42f2d1
THJ
1845 txh_done:
1846 if (unlikely(res == TX_DROP)) {
1847 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1848 if (tx->skb)
1849 ieee80211_free_txskb(&tx->local->hw, tx->skb);
1850 else
1851 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1852 return -1;
1853 } else if (unlikely(res == TX_QUEUED)) {
1854 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1855 return -1;
1856 }
1857
1858 return 0;
1859}
1860
1861/*
1862 * Late handlers can be called while the sta lock is held. Handlers that can
1863 * cause packets to be generated will cause deadlock!
1864 */
1865static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx)
1866{
1867 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1868 ieee80211_tx_result res = TX_CONTINUE;
1869
18688c80
FF
1870 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1871 CALL_TXH(ieee80211_tx_h_rate_ctrl);
1872
aa5b5492
JB
1873 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1874 __skb_queue_tail(&tx->skbs, tx->skb);
1875 tx->skb = NULL;
c6fcf6bc 1876 goto txh_done;
aa5b5492 1877 }
c6fcf6bc
JB
1878
1879 CALL_TXH(ieee80211_tx_h_michael_mic_add);
9aa4aee3
JB
1880 CALL_TXH(ieee80211_tx_h_sequence);
1881 CALL_TXH(ieee80211_tx_h_fragment);
d9e8a70f 1882 /* handlers after fragment must be aware of tx info fragmentation! */
9aa4aee3
JB
1883 CALL_TXH(ieee80211_tx_h_stats);
1884 CALL_TXH(ieee80211_tx_h_encrypt);
30686bf7 1885 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
8fd369ee 1886 CALL_TXH(ieee80211_tx_h_calculate_duration);
d9e8a70f 1887#undef CALL_TXH
97b045d6 1888
d9e8a70f 1889 txh_done:
97b045d6 1890 if (unlikely(res == TX_DROP)) {
5479d0e7 1891 I802_DEBUG_INC(tx->local->tx_handlers_drop);
252b86c4 1892 if (tx->skb)
c3e7724b 1893 ieee80211_free_txskb(&tx->local->hw, tx->skb);
252b86c4 1894 else
1f98ab7f 1895 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
97b045d6
JB
1896 return -1;
1897 } else if (unlikely(res == TX_QUEUED)) {
5479d0e7 1898 I802_DEBUG_INC(tx->local->tx_handlers_queued);
97b045d6
JB
1899 return -1;
1900 }
1901
1902 return 0;
1903}
1904
bb42f2d1
THJ
1905static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1906{
1907 int r = invoke_tx_handlers_early(tx);
1908
1909 if (r)
1910 return r;
1911 return invoke_tx_handlers_late(tx);
1912}
1913
06be6b14
FF
1914bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1915 struct ieee80211_vif *vif, struct sk_buff *skb,
1916 int band, struct ieee80211_sta **sta)
1917{
1918 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1919 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1920 struct ieee80211_tx_data tx;
88724a81 1921 struct sk_buff *skb2;
06be6b14 1922
7c10770f 1923 if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
06be6b14
FF
1924 return false;
1925
1926 info->band = band;
1927 info->control.vif = vif;
1928 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1929
1930 if (invoke_tx_handlers(&tx))
1931 return false;
1932
1933 if (sta) {
1934 if (tx.sta)
1935 *sta = &tx.sta->sta;
1936 else
1937 *sta = NULL;
1938 }
1939
88724a81
JB
1940 /* this function isn't suitable for fragmented data frames */
1941 skb2 = __skb_dequeue(&tx.skbs);
1942 if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1943 ieee80211_free_txskb(hw, skb2);
1944 ieee80211_purge_tx_queue(hw, &tx.skbs);
1945 return false;
1946 }
1947
06be6b14
FF
1948 return true;
1949}
1950EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1951
7bb45683
JB
1952/*
1953 * Returns false if the frame couldn't be transmitted but was queued instead.
1954 */
1955static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
7c10770f 1956 struct sta_info *sta, struct sk_buff *skb,
08aca29a 1957 bool txpending)
e2ebc74d 1958{
3b8d81e0 1959 struct ieee80211_local *local = sdata->local;
5cf121c3 1960 struct ieee80211_tx_data tx;
97b045d6 1961 ieee80211_tx_result res_prepare;
e039fa4a 1962 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
7bb45683 1963 bool result = true;
e2ebc74d 1964
e2ebc74d
JB
1965 if (unlikely(skb->len < 10)) {
1966 dev_kfree_skb(skb);
7bb45683 1967 return true;
e2ebc74d
JB
1968 }
1969
58d4185e 1970 /* initialises tx */
7c10770f 1971 res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
e2ebc74d 1972
cd8ffc80 1973 if (unlikely(res_prepare == TX_DROP)) {
c3e7724b 1974 ieee80211_free_txskb(&local->hw, skb);
55de908a 1975 return true;
cd8ffc80 1976 } else if (unlikely(res_prepare == TX_QUEUED)) {
55de908a 1977 return true;
e2ebc74d
JB
1978 }
1979
3a25a8c8
JB
1980 /* set up hw_queue value early */
1981 if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
30686bf7 1982 !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
3a25a8c8
JB
1983 info->hw_queue =
1984 sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1985
bb42f2d1 1986 if (invoke_tx_handlers_early(&tx))
6eae4a6c 1987 return true;
bb42f2d1
THJ
1988
1989 if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb))
1990 return true;
1991
1992 if (!invoke_tx_handlers_late(&tx))
30f6cf96 1993 result = __ieee80211_tx(local, &tx.skbs, tx.sta, txpending);
55de908a 1994
7bb45683 1995 return result;
e2ebc74d
JB
1996}
1997
1998/* device xmit handlers */
1999
14f46c1e
JB
2000enum ieee80211_encrypt {
2001 ENCRYPT_NO,
2002 ENCRYPT_MGMT,
2003 ENCRYPT_DATA,
2004};
2005
3bff1865 2006static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
23c0752a 2007 struct sk_buff *skb,
14f46c1e
JB
2008 int head_need,
2009 enum ieee80211_encrypt encrypt)
23c0752a 2010{
3bff1865 2011 struct ieee80211_local *local = sdata->local;
9d0f50b8 2012 bool enc_tailroom;
23c0752a
JB
2013 int tail_need = 0;
2014
14f46c1e
JB
2015 enc_tailroom = encrypt == ENCRYPT_MGMT ||
2016 (encrypt == ENCRYPT_DATA &&
2017 sdata->crypto_tx_tailroom_needed_cnt);
9d0f50b8
FF
2018
2019 if (enc_tailroom) {
23c0752a
JB
2020 tail_need = IEEE80211_ENCRYPT_TAILROOM;
2021 tail_need -= skb_tailroom(skb);
2022 tail_need = max_t(int, tail_need, 0);
2023 }
2024
c70f59a2 2025 if (skb_cloned(skb) &&
30686bf7 2026 (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
9d0f50b8 2027 !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
23c0752a 2028 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
4cd06a34 2029 else if (head_need || tail_need)
23c0752a 2030 I802_DEBUG_INC(local->tx_expand_skb_head);
4cd06a34
FF
2031 else
2032 return 0;
23c0752a
JB
2033
2034 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
0fb9a9ec
JP
2035 wiphy_debug(local->hw.wiphy,
2036 "failed to reallocate TX buffer\n");
23c0752a
JB
2037 return -ENOMEM;
2038 }
2039
23c0752a
JB
2040 return 0;
2041}
2042
7c10770f 2043void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
08aca29a 2044 struct sta_info *sta, struct sk_buff *skb)
e2ebc74d 2045{
3b8d81e0 2046 struct ieee80211_local *local = sdata->local;
e039fa4a 2047 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
14f46c1e 2048 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
e2ebc74d 2049 int headroom;
14f46c1e 2050 enum ieee80211_encrypt encrypt;
e2ebc74d 2051
14f46c1e
JB
2052 if (info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)
2053 encrypt = ENCRYPT_NO;
2054 else if (ieee80211_is_mgmt(hdr->frame_control))
2055 encrypt = ENCRYPT_MGMT;
2056 else
2057 encrypt = ENCRYPT_DATA;
23c0752a 2058
3b8d81e0 2059 headroom = local->tx_headroom;
14f46c1e 2060 if (encrypt != ENCRYPT_NO)
23a5f0af 2061 headroom += IEEE80211_ENCRYPT_HEADROOM;
23c0752a
JB
2062 headroom -= skb_headroom(skb);
2063 headroom = max_t(int, 0, headroom);
2064
14f46c1e 2065 if (ieee80211_skb_resize(sdata, skb, headroom, encrypt)) {
c3e7724b 2066 ieee80211_free_txskb(&local->hw, skb);
3b8d81e0 2067 return;
e2ebc74d
JB
2068 }
2069
14f46c1e 2070 /* reload after potential resize */
740c1aa3 2071 hdr = (struct ieee80211_hdr *) skb->data;
5061b0c2 2072 info->control.vif = &sdata->vif;
e2ebc74d 2073
3f52b7e3
MP
2074 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2075 if (ieee80211_is_data(hdr->frame_control) &&
2076 is_unicast_ether_addr(hdr->addr1)) {
bf7cd94d 2077 if (mesh_nexthop_resolve(sdata, skb))
3f52b7e3
MP
2078 return; /* skb queued: don't free */
2079 } else {
2080 ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
2081 }
98aed9fd 2082 }
cca89496 2083
2154c81c 2084 ieee80211_set_qos_hdr(sdata, skb);
08aca29a 2085 ieee80211_tx(sdata, sta, skb, false);
e2ebc74d
JB
2086}
2087
bddc0c41
MV
2088static bool ieee80211_validate_radiotap_len(struct sk_buff *skb)
2089{
2090 struct ieee80211_radiotap_header *rthdr =
2091 (struct ieee80211_radiotap_header *)skb->data;
2092
2093 /* check for not even having the fixed radiotap header part */
2094 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2095 return false; /* too short to be possibly valid */
2096
2097 /* is it a header version we can trust to find length from? */
2098 if (unlikely(rthdr->it_version))
2099 return false; /* only version 0 is supported */
2100
2101 /* does the skb contain enough to deliver on the alleged length? */
2102 if (unlikely(skb->len < ieee80211_get_radiotap_len(skb->data)))
2103 return false; /* skb too short for claimed rt header extent */
2104
2105 return true;
2106}
2107
cb17ed29
MV
2108bool ieee80211_parse_tx_radiotap(struct sk_buff *skb,
2109 struct net_device *dev)
73b9f03a 2110{
cb17ed29 2111 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
73b9f03a
JB
2112 struct ieee80211_radiotap_iterator iterator;
2113 struct ieee80211_radiotap_header *rthdr =
2114 (struct ieee80211_radiotap_header *) skb->data;
2115 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2116 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
2117 NULL);
2118 u16 txflags;
dfdfc2be
SE
2119 u16 rate = 0;
2120 bool rate_found = false;
2121 u8 rate_retries = 0;
2122 u16 rate_flags = 0;
f66b60f6 2123 u8 mcs_known, mcs_flags, mcs_bw;
646e76bb
LB
2124 u16 vht_known;
2125 u8 vht_mcs = 0, vht_nss = 0;
dfdfc2be 2126 int i;
73b9f03a 2127
bddc0c41
MV
2128 if (!ieee80211_validate_radiotap_len(skb))
2129 return false;
cb17ed29 2130
73b9f03a
JB
2131 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
2132 IEEE80211_TX_CTL_DONTFRAG;
2133
2134 /*
2135 * for every radiotap entry that is present
2136 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
2137 * entries present, or -EINVAL on error)
2138 */
2139
2140 while (!ret) {
2141 ret = ieee80211_radiotap_iterator_next(&iterator);
2142
2143 if (ret)
2144 continue;
2145
2146 /* see if this argument is something we can use */
2147 switch (iterator.this_arg_index) {
2148 /*
2149 * You must take care when dereferencing iterator.this_arg
2150 * for multibyte types... the pointer is not aligned. Use
2151 * get_unaligned((type *)iterator.this_arg) to dereference
2152 * iterator.this_arg for type "type" safely on all arches.
2153 */
2154 case IEEE80211_RADIOTAP_FLAGS:
2155 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
2156 /*
2157 * this indicates that the skb we have been
2158 * handed has the 32-bit FCS CRC at the end...
2159 * we should react to that by snipping it off
2160 * because it will be recomputed and added
2161 * on transmission
2162 */
2163 if (skb->len < (iterator._max_length + FCS_LEN))
2164 return false;
2165
2166 skb_trim(skb, skb->len - FCS_LEN);
2167 }
2168 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
2169 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
2170 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
2171 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
2172 break;
2173
2174 case IEEE80211_RADIOTAP_TX_FLAGS:
2175 txflags = get_unaligned_le16(iterator.this_arg);
2176 if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
2177 info->flags |= IEEE80211_TX_CTL_NO_ACK;
e02281e7
MV
2178 if (txflags & IEEE80211_RADIOTAP_F_TX_NOSEQNO)
2179 info->control.flags |= IEEE80211_TX_CTRL_NO_SEQNO;
30df8130
MV
2180 if (txflags & IEEE80211_RADIOTAP_F_TX_ORDER)
2181 info->control.flags |=
2182 IEEE80211_TX_CTRL_DONT_REORDER;
73b9f03a
JB
2183 break;
2184
dfdfc2be
SE
2185 case IEEE80211_RADIOTAP_RATE:
2186 rate = *iterator.this_arg;
2187 rate_flags = 0;
2188 rate_found = true;
2189 break;
2190
2191 case IEEE80211_RADIOTAP_DATA_RETRIES:
2192 rate_retries = *iterator.this_arg;
2193 break;
2194
2195 case IEEE80211_RADIOTAP_MCS:
2196 mcs_known = iterator.this_arg[0];
2197 mcs_flags = iterator.this_arg[1];
2198 if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
2199 break;
2200
2201 rate_found = true;
2202 rate = iterator.this_arg[2];
2203 rate_flags = IEEE80211_TX_RC_MCS;
2204
2205 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
2206 mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
2207 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2208
f66b60f6 2209 mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
dfdfc2be 2210 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
f66b60f6 2211 mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
dfdfc2be 2212 rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
f1864e19
PB
2213
2214 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_FEC &&
2215 mcs_flags & IEEE80211_RADIOTAP_MCS_FEC_LDPC)
2216 info->flags |= IEEE80211_TX_CTL_LDPC;
549fdd34
PB
2217
2218 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_STBC) {
2219 u8 stbc = u8_get_bits(mcs_flags,
2220 IEEE80211_RADIOTAP_MCS_STBC_MASK);
2221
2222 info->flags |=
2223 u32_encode_bits(stbc,
2224 IEEE80211_TX_CTL_STBC);
2225 }
dfdfc2be
SE
2226 break;
2227
646e76bb
LB
2228 case IEEE80211_RADIOTAP_VHT:
2229 vht_known = get_unaligned_le16(iterator.this_arg);
2230 rate_found = true;
2231
2232 rate_flags = IEEE80211_TX_RC_VHT_MCS;
2233 if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
2234 (iterator.this_arg[2] &
2235 IEEE80211_RADIOTAP_VHT_FLAG_SGI))
2236 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2237 if (vht_known &
2238 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
2239 if (iterator.this_arg[3] == 1)
2240 rate_flags |=
2241 IEEE80211_TX_RC_40_MHZ_WIDTH;
2242 else if (iterator.this_arg[3] == 4)
2243 rate_flags |=
2244 IEEE80211_TX_RC_80_MHZ_WIDTH;
2245 else if (iterator.this_arg[3] == 11)
2246 rate_flags |=
2247 IEEE80211_TX_RC_160_MHZ_WIDTH;
2248 }
2249
2250 vht_mcs = iterator.this_arg[4] >> 4;
13cb6d82
LB
2251 if (vht_mcs > 11)
2252 vht_mcs = 0;
646e76bb 2253 vht_nss = iterator.this_arg[4] & 0xF;
13cb6d82
LB
2254 if (!vht_nss || vht_nss > 8)
2255 vht_nss = 1;
646e76bb
LB
2256 break;
2257
73b9f03a
JB
2258 /*
2259 * Please update the file
429ff87b 2260 * Documentation/networking/mac80211-injection.rst
73b9f03a
JB
2261 * when parsing new fields here.
2262 */
2263
2264 default:
2265 break;
2266 }
2267 }
2268
2269 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
2270 return false;
2271
dfdfc2be 2272 if (rate_found) {
bddc0c41
MV
2273 struct ieee80211_supported_band *sband =
2274 local->hw.wiphy->bands[info->band];
2275
dfdfc2be
SE
2276 info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
2277
2278 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2279 info->control.rates[i].idx = -1;
2280 info->control.rates[i].flags = 0;
2281 info->control.rates[i].count = 0;
2282 }
2283
2284 if (rate_flags & IEEE80211_TX_RC_MCS) {
2285 info->control.rates[0].idx = rate;
646e76bb
LB
2286 } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
2287 ieee80211_rate_set_vht(info->control.rates, vht_mcs,
2288 vht_nss);
bddc0c41 2289 } else if (sband) {
dfdfc2be
SE
2290 for (i = 0; i < sband->n_bitrates; i++) {
2291 if (rate * 5 != sband->bitrates[i].bitrate)
2292 continue;
2293
2294 info->control.rates[0].idx = i;
2295 break;
2296 }
2297 }
2298
07310a63
FF
2299 if (info->control.rates[0].idx < 0)
2300 info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
2301
dfdfc2be
SE
2302 info->control.rates[0].flags = rate_flags;
2303 info->control.rates[0].count = min_t(u8, rate_retries + 1,
2304 local->hw.max_rate_tries);
2305 }
2306
73b9f03a
JB
2307 return true;
2308}
2309
d0cf9c0d
SH
2310netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
2311 struct net_device *dev)
e2ebc74d
JB
2312{
2313 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
55de908a 2314 struct ieee80211_chanctx_conf *chanctx_conf;
3b8d81e0 2315 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
f75f5c6f 2316 struct ieee80211_hdr *hdr;
5d9cf4a5 2317 struct ieee80211_sub_if_data *tmp_sdata, *sdata;
b4932836 2318 struct cfg80211_chan_def *chandef;
9b8a74e3 2319 u16 len_rthdr;
5d9cf4a5 2320 int hdrlen;
e2ebc74d 2321
cb17ed29
MV
2322 memset(info, 0, sizeof(*info));
2323 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2324 IEEE80211_TX_CTL_INJECTED;
9b8a74e3 2325
bddc0c41
MV
2326 /* Sanity-check the length of the radiotap header */
2327 if (!ieee80211_validate_radiotap_len(skb))
cb17ed29 2328 goto fail;
9b8a74e3 2329
cb17ed29 2330 /* we now know there is a radiotap header with a length we can use */
9b8a74e3
AG
2331 len_rthdr = ieee80211_get_radiotap_len(skb->data);
2332
e2ebc74d
JB
2333 /*
2334 * fix up the pointers accounting for the radiotap
2335 * header still being in there. We are being given
2336 * a precooked IEEE80211 header so no need for
2337 * normal processing
2338 */
9b8a74e3 2339 skb_set_mac_header(skb, len_rthdr);
e2ebc74d 2340 /*
9b8a74e3
AG
2341 * these are just fixed to the end of the rt area since we
2342 * don't have any better information and at this point, nobody cares
e2ebc74d 2343 */
9b8a74e3
AG
2344 skb_set_network_header(skb, len_rthdr);
2345 skb_set_transport_header(skb, len_rthdr);
e2ebc74d 2346
5d9cf4a5
JB
2347 if (skb->len < len_rthdr + 2)
2348 goto fail;
2349
2350 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2351 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2352
2353 if (skb->len < len_rthdr + hdrlen)
2354 goto fail;
2355
f75f5c6f
HS
2356 /*
2357 * Initialize skb->protocol if the injected frame is a data frame
2358 * carrying a rfc1042 header
2359 */
5d9cf4a5
JB
2360 if (ieee80211_is_data(hdr->frame_control) &&
2361 skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2362 u8 *payload = (u8 *)hdr + hdrlen;
2363
b203ca39 2364 if (ether_addr_equal(payload, rfc1042_header))
5d9cf4a5
JB
2365 skb->protocol = cpu_to_be16((payload[6] << 8) |
2366 payload[7]);
f75f5c6f
HS
2367 }
2368
5d9cf4a5
JB
2369 rcu_read_lock();
2370
2371 /*
2372 * We process outgoing injected frames that have a local address
2373 * we handle as though they are non-injected frames.
2374 * This code here isn't entirely correct, the local MAC address
2375 * isn't always enough to find the interface to use; for proper
70d9c599 2376 * VLAN support we have an nl80211-based mechanism.
b226a826
JB
2377 *
2378 * This is necessary, for example, for old hostapd versions that
2379 * don't use nl80211-based management TX/RX.
5d9cf4a5
JB
2380 */
2381 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2382
2383 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2384 if (!ieee80211_sdata_running(tmp_sdata))
2385 continue;
2386 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
70d9c599 2387 tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
5d9cf4a5 2388 continue;
b203ca39 2389 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
5d9cf4a5
JB
2390 sdata = tmp_sdata;
2391 break;
2392 }
2393 }
7351c6bd 2394
d0a9123e 2395 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
55de908a
JB
2396 if (!chanctx_conf) {
2397 tmp_sdata = rcu_dereference(local->monitor_sdata);
2398 if (tmp_sdata)
2399 chanctx_conf =
d0a9123e 2400 rcu_dereference(tmp_sdata->vif.bss_conf.chanctx_conf);
55de908a 2401 }
55de908a 2402
b4a7ff75 2403 if (chanctx_conf)
b4932836 2404 chandef = &chanctx_conf->def;
b4a7ff75 2405 else if (!local->use_chanctx)
b4932836 2406 chandef = &local->_oper_chandef;
b4a7ff75
FF
2407 else
2408 goto fail_rcu;
55de908a
JB
2409
2410 /*
2411 * Frame injection is not allowed if beaconing is not allowed
2412 * or if we need radar detection. Beaconing is usually not allowed when
2413 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2414 * Passive scan is also used in world regulatory domains where
2415 * your country is not known and as such it should be treated as
2416 * NO TX unless the channel is explicitly allowed in which case
2417 * your current regulatory domain would not have the passive scan
2418 * flag.
2419 *
2420 * Since AP mode uses monitor interfaces to inject/TX management
2421 * frames we can make AP mode the exception to this rule once it
2422 * supports radar detection as its implementation can deal with
2423 * radar detection by itself. We can do that later by adding a
2424 * monitor flag interfaces used for AP support.
2425 */
b4932836
JD
2426 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2427 sdata->vif.type))
55de908a
JB
2428 goto fail_rcu;
2429
73c4e195 2430 info->band = chandef->chan->band;
109843b0 2431
96a7109a
JA
2432 /* Initialize skb->priority according to frame type and TID class,
2433 * with respect to the sub interface that the frame will actually
2434 * be transmitted on. If the DONT_REORDER flag is set, the original
2435 * skb-priority is preserved to assure frames injected with this
2436 * flag are not reordered relative to each other.
2437 */
2438 ieee80211_select_queue_80211(sdata, skb, hdr);
2439 skb_set_queue_mapping(skb, ieee80211_ac_from_tid(skb->priority));
2440
bddc0c41
MV
2441 /*
2442 * Process the radiotap header. This will now take into account the
2443 * selected chandef above to accurately set injection rates and
2444 * retransmissions.
2445 */
2446 if (!ieee80211_parse_tx_radiotap(skb, dev))
2447 goto fail_rcu;
2448
cb17ed29
MV
2449 /* remove the injection radiotap header */
2450 skb_pull(skb, len_rthdr);
109843b0 2451
08aca29a 2452 ieee80211_xmit(sdata, NULL, skb);
5d9cf4a5
JB
2453 rcu_read_unlock();
2454
e2ebc74d 2455 return NETDEV_TX_OK;
9b8a74e3 2456
55de908a
JB
2457fail_rcu:
2458 rcu_read_unlock();
9b8a74e3
AG
2459fail:
2460 dev_kfree_skb(skb);
2461 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
e2ebc74d
JB
2462}
2463
97ffe757 2464static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
ad38bfc9 2465{
97ffe757 2466 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
ad38bfc9 2467
97ffe757
JB
2468 return ethertype == ETH_P_TDLS &&
2469 skb->len > 14 &&
2470 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2471}
2472
50ff477a
JC
2473int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2474 struct sk_buff *skb,
2475 struct sta_info **sta_out)
97ffe757
JB
2476{
2477 struct sta_info *sta;
2478
2479 switch (sdata->vif.type) {
2480 case NL80211_IFTYPE_AP_VLAN:
2481 sta = rcu_dereference(sdata->u.vlan.sta);
2482 if (sta) {
2483 *sta_out = sta;
2484 return 0;
2485 } else if (sdata->wdev.use_4addr) {
2486 return -ENOLINK;
2487 }
fc0561dc 2488 fallthrough;
97ffe757
JB
2489 case NL80211_IFTYPE_AP:
2490 case NL80211_IFTYPE_OCB:
2491 case NL80211_IFTYPE_ADHOC:
2492 if (is_multicast_ether_addr(skb->data)) {
2493 *sta_out = ERR_PTR(-ENOENT);
2494 return 0;
2495 }
2496 sta = sta_info_get_bss(sdata, skb->data);
2497 break;
97ffe757
JB
2498#ifdef CONFIG_MAC80211_MESH
2499 case NL80211_IFTYPE_MESH_POINT:
2500 /* determined much later */
2501 *sta_out = NULL;
2502 return 0;
2503#endif
2504 case NL80211_IFTYPE_STATION:
2505 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2506 sta = sta_info_get(sdata, skb->data);
11d62caf
JB
2507 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2508 if (test_sta_flag(sta,
2509 WLAN_STA_TDLS_PEER_AUTH)) {
97ffe757
JB
2510 *sta_out = sta;
2511 return 0;
2512 }
2513
2514 /*
2515 * TDLS link during setup - throw out frames to
2516 * peer. Allow TDLS-setup frames to unauthorized
2517 * peers for the special case of a link teardown
2518 * after a TDLS sta is removed due to being
2519 * unreachable.
2520 */
11d62caf 2521 if (!ieee80211_is_tdls_setup(skb))
97ffe757
JB
2522 return -EINVAL;
2523 }
2524
2525 }
2526
81151ce4 2527 sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
97ffe757
JB
2528 if (!sta)
2529 return -ENOLINK;
2530 break;
2531 default:
2532 return -EINVAL;
2533 }
2534
2535 *sta_out = sta ?: ERR_PTR(-ENOENT);
2536 return 0;
ad38bfc9
MG
2537}
2538
a7528198 2539static u16 ieee80211_store_ack_skb(struct ieee80211_local *local,
5d8983c8 2540 struct sk_buff *skb,
a7528198
MT
2541 u32 *info_flags,
2542 u64 *cookie)
5d8983c8 2543{
a7528198 2544 struct sk_buff *ack_skb;
5d8983c8
JC
2545 u16 info_id = 0;
2546
a7528198
MT
2547 if (skb->sk)
2548 ack_skb = skb_clone_sk(skb);
2549 else
2550 ack_skb = skb_clone(skb, GFP_ATOMIC);
2551
5d8983c8
JC
2552 if (ack_skb) {
2553 unsigned long flags;
2554 int id;
2555
2556 spin_lock_irqsave(&local->ack_status_lock, flags);
2557 id = idr_alloc(&local->ack_status_frames, ack_skb,
f2b18bac 2558 1, 0x2000, GFP_ATOMIC);
5d8983c8
JC
2559 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2560
2561 if (id >= 0) {
2562 info_id = id;
2563 *info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
a7528198
MT
2564 if (cookie) {
2565 *cookie = ieee80211_mgmt_tx_cookie(local);
2566 IEEE80211_SKB_CB(ack_skb)->ack.cookie = *cookie;
2567 }
5d8983c8
JC
2568 } else {
2569 kfree_skb(ack_skb);
2570 }
2571 }
2572
2573 return info_id;
2574}
2575
e2ebc74d 2576/**
4c9451ed
JB
2577 * ieee80211_build_hdr - build 802.11 header in the given frame
2578 * @sdata: virtual interface to build the header for
2579 * @skb: the skb to build the header in
24d342c5 2580 * @info_flags: skb flags to set
f0daf54f 2581 * @sta: the station pointer
06016772 2582 * @ctrl_flags: info control flags to set
f0daf54f 2583 * @cookie: cookie pointer to fill (if not %NULL)
e2ebc74d 2584 *
4c9451ed
JB
2585 * This function takes the skb with 802.3 header and reformats the header to
2586 * the appropriate IEEE 802.11 header based on which interface the packet is
2587 * being transmitted on.
2588 *
2589 * Note that this function also takes care of the TX status request and
2590 * potential unsharing of the SKB - this needs to be interleaved with the
2591 * header building.
e2ebc74d 2592 *
4c9451ed
JB
2593 * The function requires the read-side RCU lock held
2594 *
2595 * Returns: the (possibly reallocated) skb or an ERR_PTR() code
e2ebc74d 2596 */
4c9451ed 2597static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
7c10770f 2598 struct sk_buff *skb, u32 info_flags,
a7528198
MT
2599 struct sta_info *sta, u32 ctrl_flags,
2600 u64 *cookie)
e2ebc74d 2601{
133b8226 2602 struct ieee80211_local *local = sdata->local;
489ee919 2603 struct ieee80211_tx_info *info;
dcf33963 2604 int head_need;
065e9605
HH
2605 u16 ethertype, hdrlen, meshhdrlen = 0;
2606 __le16 fc;
e2ebc74d 2607 struct ieee80211_hdr hdr;
ff67bb86 2608 struct ieee80211s_hdr mesh_hdr __maybe_unused;
b8bacc18 2609 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
e2ebc74d
JB
2610 const u8 *encaps_data;
2611 int encaps_len, skip_header_bytes;
97ffe757
JB
2612 bool wme_sta = false, authorized = false;
2613 bool tdls_peer;
a729cff8 2614 bool multicast;
a729cff8 2615 u16 info_id = 0;
eef25a66 2616 struct ieee80211_chanctx_conf *chanctx_conf = NULL;
57fbcce3 2617 enum nl80211_band band;
4c9451ed 2618 int ret;
963d0e8d 2619 u8 link_id = u32_get_bits(ctrl_flags, IEEE80211_TX_CTRL_MLO_LINK);
e2ebc74d 2620
97ffe757
JB
2621 if (IS_ERR(sta))
2622 sta = NULL;
2623
276d9e82
JN
2624#ifdef CONFIG_MAC80211_DEBUGFS
2625 if (local->force_tx_status)
2626 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2627#endif
2628
e2ebc74d
JB
2629 /* convert Ethernet header to proper 802.11 header (based on
2630 * operation mode) */
2631 ethertype = (skb->data[12] << 8) | skb->data[13];
065e9605 2632 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
e2ebc74d 2633
eef25a66
JB
2634 if (!sdata->vif.valid_links)
2635 chanctx_conf =
2636 rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
27f852de 2637
51fb61e7 2638 switch (sdata->vif.type) {
05c914fe 2639 case NL80211_IFTYPE_AP_VLAN:
97ffe757 2640 if (sdata->wdev.use_4addr) {
f14543ee
FF
2641 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2642 /* RA TA DA SA */
2643 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
47846c9b 2644 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
f14543ee
FF
2645 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2646 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2647 hdrlen = 30;
c2c98fde 2648 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
a74a8c84 2649 wme_sta = sta->sta.wme;
f14543ee 2650 }
eef25a66
JB
2651 if (!sdata->vif.valid_links) {
2652 struct ieee80211_sub_if_data *ap_sdata;
2653
2654 /* override chanctx_conf from AP (we don't have one) */
2655 ap_sdata = container_of(sdata->bss,
2656 struct ieee80211_sub_if_data,
2657 u.ap);
2658 chanctx_conf =
2659 rcu_dereference(ap_sdata->vif.bss_conf.chanctx_conf);
2660 }
97ffe757 2661 if (sdata->wdev.use_4addr)
f14543ee 2662 break;
fc0561dc 2663 fallthrough;
f14543ee 2664 case NL80211_IFTYPE_AP:
065e9605 2665 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
e2ebc74d
JB
2666 /* DA BSSID SA */
2667 memcpy(hdr.addr1, skb->data, ETH_ALEN);
3e0278b7
AO
2668
2669 if (sdata->vif.valid_links && sta && !sta->sta.mlo) {
2670 struct ieee80211_link_data *link;
2671
2672 link_id = sta->deflink.link_id;
2673 link = rcu_dereference(sdata->link[link_id]);
2674 if (WARN_ON(!link)) {
2675 ret = -ENOLINK;
2676 goto free;
2677 }
2678 memcpy(hdr.addr2, link->conf->addr, ETH_ALEN);
963d0e8d 2679 } else if (link_id == IEEE80211_LINK_UNSPECIFIED) {
3e0278b7 2680 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
963d0e8d
JB
2681 } else {
2682 struct ieee80211_bss_conf *conf;
2683
2684 conf = rcu_dereference(sdata->vif.link_conf[link_id]);
2685 if (unlikely(!conf)) {
2686 ret = -ENOLINK;
2687 goto free;
2688 }
2689
2690 memcpy(hdr.addr2, conf->addr, ETH_ALEN);
3e0278b7
AO
2691 }
2692
e2ebc74d
JB
2693 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2694 hdrlen = 24;
cf966838 2695 break;
33b64eb2 2696#ifdef CONFIG_MAC80211_MESH
05c914fe 2697 case NL80211_IFTYPE_MESH_POINT:
b8bacc18 2698 if (!is_multicast_ether_addr(skb->data)) {
163df6cf
CYY
2699 struct sta_info *next_hop;
2700 bool mpp_lookup = true;
2701
bf7cd94d 2702 mpath = mesh_path_lookup(sdata, skb->data);
163df6cf
CYY
2703 if (mpath) {
2704 mpp_lookup = false;
2705 next_hop = rcu_dereference(mpath->next_hop);
2706 if (!next_hop ||
2707 !(mpath->flags & (MESH_PATH_ACTIVE |
2708 MESH_PATH_RESOLVING)))
2709 mpp_lookup = true;
2710 }
2711
ab1c7906 2712 if (mpp_lookup) {
bf7cd94d 2713 mppath = mpp_path_lookup(sdata, skb->data);
ab1c7906
HR
2714 if (mppath)
2715 mppath->exp_time = jiffies;
2716 }
163df6cf
CYY
2717
2718 if (mppath && mpath)
2bdaf386 2719 mesh_path_del(sdata, mpath->dst);
b8bacc18 2720 }
79617dee 2721
f76b57b4 2722 /*
9d52501b
JF
2723 * Use address extension if it is a packet from
2724 * another interface or if we know the destination
2725 * is being proxied by a portal (i.e. portal address
2726 * differs from proxied address)
f76b57b4 2727 */
b203ca39
JP
2728 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2729 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
3c5772a5
JC
2730 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2731 skb->data, skb->data + ETH_ALEN);
bf7cd94d
JB
2732 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2733 NULL, NULL);
79617dee 2734 } else {
27f01124
TP
2735 /* DS -> MBSS (802.11-2012 13.11.3.3).
2736 * For unicast with unknown forwarding information,
2737 * destination might be in the MBSS or if that fails
2738 * forwarded to another mesh gate. In either case
2739 * resolution will be handled in ieee80211_xmit(), so
2740 * leave the original DA. This also works for mcast */
2741 const u8 *mesh_da = skb->data;
2742
2743 if (mppath)
2744 mesh_da = mppath->mpp;
2745 else if (mpath)
2746 mesh_da = mpath->dst;
79617dee 2747
3c5772a5 2748 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
47846c9b 2749 mesh_da, sdata->vif.addr);
27f01124
TP
2750 if (is_multicast_ether_addr(mesh_da))
2751 /* DA TA mSA AE:SA */
bf7cd94d
JB
2752 meshhdrlen = ieee80211_new_mesh_header(
2753 sdata, &mesh_hdr,
2754 skb->data + ETH_ALEN, NULL);
3c5772a5 2755 else
27f01124 2756 /* RA TA mDA mSA AE:DA SA */
bf7cd94d
JB
2757 meshhdrlen = ieee80211_new_mesh_header(
2758 sdata, &mesh_hdr, skb->data,
2759 skb->data + ETH_ALEN);
79617dee 2760
79617dee 2761 }
8828f81a
RM
2762
2763 /* For injected frames, fill RA right away as nexthop lookup
2764 * will be skipped.
2765 */
2766 if ((ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP) &&
2767 is_zero_ether_addr(hdr.addr1))
2768 memcpy(hdr.addr1, skb->data, ETH_ALEN);
33b64eb2
LCC
2769 break;
2770#endif
05c914fe 2771 case NL80211_IFTYPE_STATION:
97ffe757
JB
2772 /* we already did checks when looking up the RA STA */
2773 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
941c93cd 2774
97ffe757 2775 if (tdls_peer) {
941c93cd
AN
2776 /* DA SA BSSID */
2777 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2778 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
bfd8403a 2779 memcpy(hdr.addr3, sdata->deflink.u.mgd.bssid, ETH_ALEN);
941c93cd
AN
2780 hdrlen = 24;
2781 } else if (sdata->u.mgd.use_4addr &&
2782 cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2783 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2784 IEEE80211_FCTL_TODS);
f14543ee 2785 /* RA TA DA SA */
bfd8403a 2786 memcpy(hdr.addr1, sdata->deflink.u.mgd.bssid, ETH_ALEN);
47846c9b 2787 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
f14543ee
FF
2788 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2789 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2790 hdrlen = 30;
2791 } else {
2792 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2793 /* BSSID SA DA */
8a9be422 2794 memcpy(hdr.addr1, sdata->vif.cfg.ap_addr, ETH_ALEN);
f14543ee
FF
2795 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2796 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2797 hdrlen = 24;
2798 }
cf966838 2799 break;
239281f8
RL
2800 case NL80211_IFTYPE_OCB:
2801 /* DA SA BSSID */
2802 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2803 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2804 eth_broadcast_addr(hdr.addr3);
2805 hdrlen = 24;
239281f8 2806 break;
05c914fe 2807 case NL80211_IFTYPE_ADHOC:
e2ebc74d
JB
2808 /* DA SA BSSID */
2809 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2810 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
46900298 2811 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
e2ebc74d 2812 hdrlen = 24;
cf966838
JB
2813 break;
2814 default:
4c9451ed
JB
2815 ret = -EINVAL;
2816 goto free;
e2ebc74d
JB
2817 }
2818
27f852de 2819 if (!chanctx_conf) {
eef25a66
JB
2820 if (!sdata->vif.valid_links) {
2821 ret = -ENOTCONN;
2822 goto free;
2823 }
2824 /* MLD transmissions must not rely on the band */
2825 band = 0;
2826 } else {
2827 band = chanctx_conf->def.chan->band;
27f852de 2828 }
27f852de 2829
a729cff8 2830 multicast = is_multicast_ether_addr(hdr.addr1);
e2ebc74d 2831
97ffe757
JB
2832 /* sta is always NULL for mesh */
2833 if (sta) {
2834 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2835 wme_sta = sta->sta.wme;
2836 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2837 /* For mesh, the use of the QoS header is mandatory */
c2c98fde 2838 wme_sta = true;
97ffe757 2839 }
4777be41 2840
527871d7
JB
2841 /* receiver does QoS (which also means we do) use it */
2842 if (wme_sta) {
065e9605 2843 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
ce3edf6d
JB
2844 hdrlen += 2;
2845 }
2846
2847 /*
238814fd
JB
2848 * Drop unicast frames to unauthorised stations unless they are
2849 * EAPOL frames from the local station.
ce3edf6d 2850 */
55182e4a 2851 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
239281f8 2852 (sdata->vif.type != NL80211_IFTYPE_OCB) &&
a6ececf4 2853 !multicast && !authorized &&
55182e4a 2854 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
0d5891e3 2855 !ieee80211_is_our_addr(sdata, skb->data + ETH_ALEN, NULL)))) {
ce3edf6d 2856#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
bdcbd8e0 2857 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
4c9451ed 2858 sdata->name, hdr.addr1);
ce3edf6d
JB
2859#endif
2860
2861 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2862
4c9451ed
JB
2863 ret = -EPERM;
2864 goto free;
ce3edf6d
JB
2865 }
2866
a7528198
MT
2867 if (unlikely(!multicast && ((skb->sk &&
2868 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS) ||
2869 ctrl_flags & IEEE80211_TX_CTL_REQ_TX_STATUS)))
2870 info_id = ieee80211_store_ack_skb(local, skb, &info_flags,
2871 cookie);
a729cff8 2872
7e244707
HS
2873 /*
2874 * If the skb is shared we need to obtain our own copy.
2875 */
a4926abb
RL
2876 skb = skb_share_check(skb, GFP_ATOMIC);
2877 if (unlikely(!skb)) {
2878 ret = -ENOMEM;
2879 goto free;
7e244707
HS
2880 }
2881
065e9605 2882 hdr.frame_control = fc;
e2ebc74d
JB
2883 hdr.duration_id = 0;
2884 hdr.seq_ctrl = 0;
2885
2886 skip_header_bytes = ETH_HLEN;
2887 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2888 encaps_data = bridge_tunnel_header;
2889 encaps_len = sizeof(bridge_tunnel_header);
2890 skip_header_bytes -= 2;
e5c5d22e 2891 } else if (ethertype >= ETH_P_802_3_MIN) {
e2ebc74d
JB
2892 encaps_data = rfc1042_header;
2893 encaps_len = sizeof(rfc1042_header);
2894 skip_header_bytes -= 2;
2895 } else {
2896 encaps_data = NULL;
2897 encaps_len = 0;
2898 }
2899
2900 skb_pull(skb, skip_header_bytes);
23c0752a 2901 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
e2ebc74d 2902
23c0752a
JB
2903 /*
2904 * So we need to modify the skb header and hence need a copy of
2905 * that. The head_need variable above doesn't, so far, include
2906 * the needed header space that we don't need right away. If we
2907 * can, then we don't reallocate right now but only after the
2908 * frame arrives at the master device (if it does...)
2909 *
2910 * If we cannot, however, then we will reallocate to include all
2911 * the ever needed space. Also, if we need to reallocate it anyway,
2912 * make it big enough for everything we may ever need.
2913 */
e2ebc74d 2914
3a5be7d4 2915 if (head_need > 0 || skb_cloned(skb)) {
23a5f0af 2916 head_need += IEEE80211_ENCRYPT_HEADROOM;
23c0752a
JB
2917 head_need += local->tx_headroom;
2918 head_need = max_t(int, 0, head_need);
14f46c1e 2919 if (ieee80211_skb_resize(sdata, skb, head_need, ENCRYPT_DATA)) {
c3e7724b 2920 ieee80211_free_txskb(&local->hw, skb);
1c963bec 2921 skb = NULL;
4c9451ed 2922 return ERR_PTR(-ENOMEM);
c3e7724b 2923 }
e2ebc74d
JB
2924 }
2925
eae4430e 2926 if (encaps_data)
e2ebc74d 2927 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
c29b9b9b 2928
e4ab7eb0 2929#ifdef CONFIG_MAC80211_MESH
eae4430e 2930 if (meshhdrlen > 0)
33b64eb2 2931 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
e4ab7eb0 2932#endif
33b64eb2 2933
065e9605 2934 if (ieee80211_is_data_qos(fc)) {
c29b9b9b
JB
2935 __le16 *qos_control;
2936
d58ff351 2937 qos_control = skb_push(skb, 2);
c29b9b9b
JB
2938 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2939 /*
2940 * Maybe we could actually set some fields here, for now just
2941 * initialise to zero to indicate no special operation.
2942 */
2943 *qos_control = 0;
2944 } else
2945 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2946
d57a544d 2947 skb_reset_mac_header(skb);
e2ebc74d 2948
489ee919 2949 info = IEEE80211_SKB_CB(skb);
3b8d81e0
JB
2950 memset(info, 0, sizeof(*info));
2951
a729cff8
JB
2952 info->flags = info_flags;
2953 info->ack_frame_id = info_id;
73c4e195 2954 info->band = band;
9dd19538
JB
2955
2956 if (likely(!cookie)) {
2957 ctrl_flags |= u32_encode_bits(link_id,
69d41b5a 2958 IEEE80211_TX_CTRL_MLO_LINK);
9dd19538
JB
2959 } else {
2960 unsigned int pre_conf_link_id;
2961
2962 /*
2963 * ctrl_flags already have been set by
2964 * ieee80211_tx_control_port(), here
2965 * we just sanity check that
2966 */
2967
2968 pre_conf_link_id = u32_get_bits(ctrl_flags,
2969 IEEE80211_TX_CTRL_MLO_LINK);
2970
2971 if (pre_conf_link_id != link_id &&
2972 link_id != IEEE80211_LINK_UNSPECIFIED) {
2973#ifdef CPTCFG_MAC80211_VERBOSE_DEBUG
2974 net_info_ratelimited("%s: dropped frame to %pM with bad link ID request (%d vs. %d)\n",
2975 sdata->name, hdr.addr1,
2976 pre_conf_link_id, link_id);
2977#endif
2978 ret = -EINVAL;
2979 goto free;
2980 }
2981 }
2982
2983 info->control.flags = ctrl_flags;
a729cff8 2984
4c9451ed
JB
2985 return skb;
2986 free:
2987 kfree_skb(skb);
2988 return ERR_PTR(ret);
2989}
2990
17c18bf8
JB
2991/*
2992 * fast-xmit overview
2993 *
2994 * The core idea of this fast-xmit is to remove per-packet checks by checking
2995 * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2996 * checks that are needed to get the sta->fast_tx pointer assigned, after which
2997 * much less work can be done per packet. For example, fragmentation must be
2998 * disabled or the fast_tx pointer will not be set. All the conditions are seen
2999 * in the code here.
3000 *
3001 * Once assigned, the fast_tx data structure also caches the per-packet 802.11
3002 * header and other data to aid packet processing in ieee80211_xmit_fast().
3003 *
3004 * The most difficult part of this is that when any of these assumptions
3005 * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
3006 * ieee80211_check_fast_xmit() or friends) is required to reset the data,
3007 * since the per-packet code no longer checks the conditions. This is reflected
3008 * by the calls to these functions throughout the rest of the code, and must be
3009 * maintained if any of the TX path checks change.
3010 */
3011
3012void ieee80211_check_fast_xmit(struct sta_info *sta)
3013{
3014 struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
3015 struct ieee80211_local *local = sta->local;
3016 struct ieee80211_sub_if_data *sdata = sta->sdata;
3017 struct ieee80211_hdr *hdr = (void *)build.hdr;
3018 struct ieee80211_chanctx_conf *chanctx_conf;
3019 __le16 fc;
3020
30686bf7 3021 if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
17c18bf8
JB
3022 return;
3023
3024 /* Locking here protects both the pointer itself, and against concurrent
3025 * invocations winning data access races to, e.g., the key pointer that
3026 * is used.
3027 * Without it, the invocation of this function right after the key
3028 * pointer changes wouldn't be sufficient, as another CPU could access
3029 * the pointer, then stall, and then do the cache update after the CPU
3030 * that invalidated the key.
3031 * With the locking, such scenarios cannot happen as the check for the
3032 * key and the fast-tx assignment are done atomically, so the CPU that
3033 * modifies the key will either wait or other one will see the key
3034 * cleared/changed already.
3035 */
3036 spin_lock_bh(&sta->lock);
30686bf7
JB
3037 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
3038 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
17c18bf8
JB
3039 sdata->vif.type == NL80211_IFTYPE_STATION)
3040 goto out;
3041
3042 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
3043 goto out;
3044
3045 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
3046 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
f7418bc1
FF
3047 test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
3048 test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
17c18bf8
JB
3049 goto out;
3050
3051 if (sdata->noack_map)
3052 goto out;
3053
3054 /* fast-xmit doesn't handle fragmentation at all */
725b812c 3055 if (local->hw.wiphy->frag_threshold != (u32)-1 &&
f3fe4e93 3056 !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
17c18bf8
JB
3057 goto out;
3058
eef25a66
JB
3059 if (!sdata->vif.valid_links) {
3060 rcu_read_lock();
3061 chanctx_conf =
3062 rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
3063 if (!chanctx_conf) {
3064 rcu_read_unlock();
3065 goto out;
3066 }
3067 build.band = chanctx_conf->def.chan->band;
17c18bf8 3068 rcu_read_unlock();
eef25a66
JB
3069 } else {
3070 /* MLD transmissions must not rely on the band */
3071 build.band = 0;
17c18bf8 3072 }
17c18bf8
JB
3073
3074 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
3075
3076 switch (sdata->vif.type) {
3ffd8840
JB
3077 case NL80211_IFTYPE_ADHOC:
3078 /* DA SA BSSID */
3079 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
3080 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3081 memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
3082 build.hdr_len = 24;
3083 break;
17c18bf8
JB
3084 case NL80211_IFTYPE_STATION:
3085 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
3086 /* DA SA BSSID */
3087 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
3088 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
bfd8403a 3089 memcpy(hdr->addr3, sdata->deflink.u.mgd.bssid, ETH_ALEN);
17c18bf8
JB
3090 build.hdr_len = 24;
3091 break;
3092 }
3093
3094 if (sdata->u.mgd.use_4addr) {
3095 /* non-regular ethertype cannot use the fastpath */
3096 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
3097 IEEE80211_FCTL_TODS);
3098 /* RA TA DA SA */
bfd8403a 3099 memcpy(hdr->addr1, sdata->deflink.u.mgd.bssid, ETH_ALEN);
17c18bf8
JB
3100 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
3101 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
3102 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
3103 build.hdr_len = 30;
3104 break;
3105 }
3106 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
3107 /* BSSID SA DA */
8a9be422 3108 memcpy(hdr->addr1, sdata->vif.cfg.ap_addr, ETH_ALEN);
17c18bf8
JB
3109 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
3110 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3111 build.hdr_len = 24;
3112 break;
3113 case NL80211_IFTYPE_AP_VLAN:
3114 if (sdata->wdev.use_4addr) {
3115 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
3116 IEEE80211_FCTL_TODS);
3117 /* RA TA DA SA */
3118 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
3119 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
3120 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
3121 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
3122 build.hdr_len = 30;
3123 break;
3124 }
fc0561dc 3125 fallthrough;
17c18bf8
JB
3126 case NL80211_IFTYPE_AP:
3127 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
3128 /* DA BSSID SA */
3129 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
0f13f3c3
JB
3130 if (sta->sta.mlo || !sdata->vif.valid_links) {
3131 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
3132 } else {
3133 unsigned int link_id = sta->deflink.link_id;
3134 struct ieee80211_link_data *link;
3135
3136 rcu_read_lock();
3137 link = rcu_dereference(sdata->link[link_id]);
3138 if (WARN_ON(!link)) {
3139 rcu_read_unlock();
3140 goto out;
3141 }
3142 memcpy(hdr->addr2, link->conf->addr, ETH_ALEN);
3143 rcu_read_unlock();
3144 }
17c18bf8
JB
3145 build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
3146 build.hdr_len = 24;
3147 break;
3148 default:
3149 /* not handled on fast-xmit */
3150 goto out;
3151 }
3152
3153 if (sta->sta.wme) {
3154 build.hdr_len += 2;
3155 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3156 }
3157
3158 /* We store the key here so there's no point in using rcu_dereference()
3159 * but that's fine because the code that changes the pointers will call
3160 * this function after doing so. For a single CPU that would be enough,
3161 * for multiple see the comment above.
3162 */
3163 build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
3164 if (!build.key)
3165 build.key = rcu_access_pointer(sdata->default_unicast_key);
3166 if (build.key) {
e495c247 3167 bool gen_iv, iv_spc, mmic;
17c18bf8
JB
3168
3169 gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
3170 iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
9de18d81
DS
3171 mmic = build.key->conf.flags &
3172 (IEEE80211_KEY_FLAG_GENERATE_MMIC |
3173 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
17c18bf8
JB
3174
3175 /* don't handle software crypto */
3176 if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
3177 goto out;
3178
62872a9b
AW
3179 /* Key is being removed */
3180 if (build.key->flags & KEY_FLAG_TAINTED)
3181 goto out;
3182
17c18bf8
JB
3183 switch (build.key->conf.cipher) {
3184 case WLAN_CIPHER_SUITE_CCMP:
3185 case WLAN_CIPHER_SUITE_CCMP_256:
96fc6efb 3186 if (gen_iv)
17c18bf8 3187 build.pn_offs = build.hdr_len;
17c18bf8
JB
3188 if (gen_iv || iv_spc)
3189 build.hdr_len += IEEE80211_CCMP_HDR_LEN;
3190 break;
3191 case WLAN_CIPHER_SUITE_GCMP:
3192 case WLAN_CIPHER_SUITE_GCMP_256:
96fc6efb 3193 if (gen_iv)
17c18bf8 3194 build.pn_offs = build.hdr_len;
17c18bf8
JB
3195 if (gen_iv || iv_spc)
3196 build.hdr_len += IEEE80211_GCMP_HDR_LEN;
3197 break;
e495c247
JB
3198 case WLAN_CIPHER_SUITE_TKIP:
3199 /* cannot handle MMIC or IV generation in xmit-fast */
3200 if (mmic || gen_iv)
3201 goto out;
3202 if (iv_spc)
3203 build.hdr_len += IEEE80211_TKIP_IV_LEN;
3204 break;
3205 case WLAN_CIPHER_SUITE_WEP40:
3206 case WLAN_CIPHER_SUITE_WEP104:
3207 /* cannot handle IV generation in fast-xmit */
3208 if (gen_iv)
3209 goto out;
3210 if (iv_spc)
3211 build.hdr_len += IEEE80211_WEP_IV_LEN;
3212 break;
3213 case WLAN_CIPHER_SUITE_AES_CMAC:
3214 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3215 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3216 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3217 WARN(1,
3218 "management cipher suite 0x%x enabled for data\n",
3219 build.key->conf.cipher);
17c18bf8 3220 goto out;
e495c247
JB
3221 default:
3222 /* we don't know how to generate IVs for this at all */
3223 if (WARN_ON(gen_iv))
3224 goto out;
17c18bf8
JB
3225 }
3226
3227 fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3228 }
3229
3230 hdr->frame_control = fc;
3231
3232 memcpy(build.hdr + build.hdr_len,
3233 rfc1042_header, sizeof(rfc1042_header));
3234 build.hdr_len += sizeof(rfc1042_header);
3235
3236 fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3237 /* if the kmemdup fails, continue w/o fast_tx */
17c18bf8
JB
3238
3239 out:
3240 /* we might have raced against another call to this function */
3241 old = rcu_dereference_protected(sta->fast_tx,
3242 lockdep_is_held(&sta->lock));
3243 rcu_assign_pointer(sta->fast_tx, fast_tx);
3244 if (old)
3245 kfree_rcu(old, rcu_head);
3246 spin_unlock_bh(&sta->lock);
3247}
3248
3249void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3250{
3251 struct sta_info *sta;
3252
3253 rcu_read_lock();
3254 list_for_each_entry_rcu(sta, &local->sta_list, list)
3255 ieee80211_check_fast_xmit(sta);
3256 rcu_read_unlock();
3257}
3258
3259void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3260{
3261 struct ieee80211_local *local = sdata->local;
3262 struct sta_info *sta;
3263
3264 rcu_read_lock();
3265
3266 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3267 if (sdata != sta->sdata &&
3268 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3269 continue;
3270 ieee80211_check_fast_xmit(sta);
3271 }
3272
3273 rcu_read_unlock();
3274}
3275
3276void ieee80211_clear_fast_xmit(struct sta_info *sta)
3277{
3278 struct ieee80211_fast_tx *fast_tx;
3279
3280 spin_lock_bh(&sta->lock);
3281 fast_tx = rcu_dereference_protected(sta->fast_tx,
3282 lockdep_is_held(&sta->lock));
3283 RCU_INIT_POINTER(sta->fast_tx, NULL);
3284 spin_unlock_bh(&sta->lock);
3285
3286 if (fast_tx)
3287 kfree_rcu(fast_tx, rcu_head);
3288}
3289
6e0456b5 3290static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
166ac9d5 3291 struct sk_buff *skb, int headroom)
6e0456b5 3292{
166ac9d5 3293 if (skb_headroom(skb) < headroom) {
6e0456b5
FF
3294 I802_DEBUG_INC(local->tx_expand_skb_head);
3295
166ac9d5 3296 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
6e0456b5
FF
3297 wiphy_debug(local->hw.wiphy,
3298 "failed to reallocate TX buffer\n");
3299 return false;
3300 }
3301 }
3302
6e0456b5
FF
3303 return true;
3304}
3305
3306static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3307 struct ieee80211_fast_tx *fast_tx,
3308 struct sk_buff *skb)
3309{
3310 struct ieee80211_local *local = sdata->local;
3311 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3312 struct ieee80211_hdr *hdr;
06f2bb1e 3313 struct ethhdr *amsdu_hdr;
6e0456b5
FF
3314 int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3315 int subframe_len = skb->len - hdr_len;
3316 void *data;
06f2bb1e 3317 u8 *qc, *h_80211_src, *h_80211_dst;
a3e2f4b6 3318 const u8 *bssid;
6e0456b5
FF
3319
3320 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3321 return false;
3322
3323 if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3324 return true;
3325
f50d2ff8
CKC
3326 if (!ieee80211_amsdu_realloc_pad(local, skb,
3327 sizeof(*amsdu_hdr) +
3328 local->hw.extra_tx_headroom))
6e0456b5
FF
3329 return false;
3330
06f2bb1e
MB
3331 data = skb_push(skb, sizeof(*amsdu_hdr));
3332 memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
3333 hdr = data;
3334 amsdu_hdr = data + hdr_len;
3335 /* h_80211_src/dst is addr* field within hdr */
3336 h_80211_src = data + fast_tx->sa_offs;
3337 h_80211_dst = data + fast_tx->da_offs;
6e0456b5 3338
06f2bb1e
MB
3339 amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3340 ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3341 ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
6e0456b5 3342
a3e2f4b6
MB
3343 /* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3344 * fields needs to be changed to BSSID for A-MSDU frames depending
3345 * on FromDS/ToDS values.
3346 */
3347 switch (sdata->vif.type) {
3348 case NL80211_IFTYPE_STATION:
8a9be422 3349 bssid = sdata->vif.cfg.ap_addr;
a3e2f4b6
MB
3350 break;
3351 case NL80211_IFTYPE_AP:
3352 case NL80211_IFTYPE_AP_VLAN:
3353 bssid = sdata->vif.addr;
3354 break;
3355 default:
3356 bssid = NULL;
3357 }
3358
3359 if (bssid && ieee80211_has_fromds(hdr->frame_control))
3360 ether_addr_copy(h_80211_src, bssid);
3361
3362 if (bssid && ieee80211_has_tods(hdr->frame_control))
3363 ether_addr_copy(h_80211_dst, bssid);
3364
6e0456b5
FF
3365 qc = ieee80211_get_qos_ctl(hdr);
3366 *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3367
3368 info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3369
3370 return true;
3371}
3372
3373static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3374 struct sta_info *sta,
3375 struct ieee80211_fast_tx *fast_tx,
3376 struct sk_buff *skb)
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;
3389 int max_amsdu_len = sta->sta.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
344f8e00
SS
3405 if (skb_is_gso(skb))
3406 return false;
3407
6e0456b5
FF
3408 if (!txq)
3409 return false;
3410
3411 txqi = to_txq_info(txq);
3412 if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3413 return false;
3414
3415 if (sta->sta.max_rc_amsdu_len)
3416 max_amsdu_len = min_t(int, max_amsdu_len,
3417 sta->sta.max_rc_amsdu_len);
3418
edba6bda
SS
3419 if (sta->sta.max_tid_amsdu_len[tid])
3420 max_amsdu_len = min_t(int, max_amsdu_len,
3421 sta->sta.max_tid_amsdu_len[tid]);
3422
f2af2df8
FF
3423 flow_idx = fq_flow_idx(fq, skb);
3424
fa962b92 3425 spin_lock_bh(&fq->lock);
6e0456b5 3426
fa962b92
MK
3427 /* TODO: Ideally aggregation should be done on dequeue to remain
3428 * responsive to environment changes.
3429 */
3430
3431 tin = &txqi->tin;
bf9009bf 3432 flow = fq_flow_classify(fq, tin, flow_idx, skb);
fa962b92 3433 head = skb_peek_tail(&flow->queue);
344f8e00 3434 if (!head || skb_is_gso(head))
6e0456b5
FF
3435 goto out;
3436
eb9b64e3 3437 orig_truesize = head->truesize;
fa962b92
MK
3438 orig_len = head->len;
3439
6e0456b5
FF
3440 if (skb->len + head->len > max_amsdu_len)
3441 goto out;
3442
6e0456b5
FF
3443 nfrags = 1 + skb_shinfo(skb)->nr_frags;
3444 nfrags += 1 + skb_shinfo(head)->nr_frags;
3445 frag_tail = &skb_shinfo(head)->frag_list;
3446 while (*frag_tail) {
3447 nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3448 frag_tail = &(*frag_tail)->next;
3449 n++;
3450 }
3451
3452 if (max_subframes && n > max_subframes)
3453 goto out;
3454
3455 if (max_frags && nfrags > max_frags)
3456 goto out;
9739fe29
SS
3457
3458 if (!drv_can_aggregate_in_amsdu(local, head, skb))
3459 goto out;
6e0456b5 3460
1eb50790 3461 if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
6e0456b5
FF
3462 goto out;
3463
fe94bac6
CKC
3464 /* If n == 2, the "while (*frag_tail)" loop above didn't execute
3465 * and frag_tail should be &skb_shinfo(head)->frag_list.
3466 * However, ieee80211_amsdu_prepare_head() can reallocate it.
3467 * Reload frag_tail to have it pointing to the correct place.
3468 */
3469 if (n == 2)
3470 frag_tail = &skb_shinfo(head)->frag_list;
3471
166ac9d5
SS
3472 /*
3473 * Pad out the previous subframe to a multiple of 4 by adding the
3474 * padding to the next one, that's being added. Note that head->len
3475 * is the length of the full A-MSDU, but that works since each time
3476 * we add a new subframe we pad out the previous one to a multiple
3477 * of 4 and thus it no longer matters in the next round.
3478 */
3479 hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3480 if ((head->len - hdrlen) & 3)
3481 pad = 4 - ((head->len - hdrlen) & 3);
3482
3483 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3484 2 + pad))
aa58acf3 3485 goto out_recalc;
6e0456b5
FF
3486
3487 ret = true;
3488 data = skb_push(skb, ETH_ALEN + 2);
3489 memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3490
3491 data += 2 * ETH_ALEN;
3492 len = cpu_to_be16(subframe_len);
3493 memcpy(data, &len, 2);
3494 memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3495
166ac9d5
SS
3496 memset(skb_push(skb, pad), 0, pad);
3497
6e0456b5
FF
3498 head->len += skb->len;
3499 head->data_len += skb->len;
3500 *frag_tail = skb;
3501
aa58acf3 3502out_recalc:
eb9b64e3 3503 fq->memory_usage += head->truesize - orig_truesize;
aa58acf3
JB
3504 if (head->len != orig_len) {
3505 flow->backlog += head->len - orig_len;
3506 tin->backlog_bytes += head->len - orig_len;
aa58acf3 3507 }
6e0456b5 3508out:
fa962b92 3509 spin_unlock_bh(&fq->lock);
6e0456b5
FF
3510
3511 return ret;
3512}
3513
bb42f2d1
THJ
3514/*
3515 * Can be called while the sta lock is held. Anything that can cause packets to
3516 * be generated will cause deadlock!
3517 */
03c3911d
RL
3518static ieee80211_tx_result
3519ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3520 struct sta_info *sta, u8 pn_offs,
3521 struct ieee80211_key *key,
3522 struct ieee80211_tx_data *tx)
bb42f2d1 3523{
03c3911d 3524 struct sk_buff *skb = tx->skb;
bb42f2d1
THJ
3525 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3526 struct ieee80211_hdr *hdr = (void *)skb->data;
3527 u8 tid = IEEE80211_NUM_TIDS;
3528
03c3911d
RL
3529 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL) &&
3530 ieee80211_tx_h_rate_ctrl(tx) != TX_CONTINUE)
3531 return TX_DROP;
3532
bb42f2d1
THJ
3533 if (key)
3534 info->control.hw_key = &key->conf;
3535
36ec144f 3536 dev_sw_netstats_tx_add(skb->dev, 1, skb->len);
bb42f2d1
THJ
3537
3538 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3539 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
bb42f2d1
THJ
3540 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3541 } else {
3542 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3543 hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3544 sdata->sequence_number += 0x10;
3545 }
3546
3547 if (skb_shinfo(skb)->gso_size)
046d2e7c 3548 sta->deflink.tx_stats.msdu[tid] +=
bb42f2d1
THJ
3549 DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3550 else
046d2e7c 3551 sta->deflink.tx_stats.msdu[tid]++;
bb42f2d1
THJ
3552
3553 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3554
3555 /* statistics normally done by ieee80211_tx_h_stats (but that
3556 * has to consider fragmentation, so is more complex)
3557 */
046d2e7c
S
3558 sta->deflink.tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3559 sta->deflink.tx_stats.packets[skb_get_queue_mapping(skb)]++;
bb42f2d1
THJ
3560
3561 if (pn_offs) {
3562 u64 pn;
3563 u8 *crypto_hdr = skb->data + pn_offs;
3564
3565 switch (key->conf.cipher) {
3566 case WLAN_CIPHER_SUITE_CCMP:
3567 case WLAN_CIPHER_SUITE_CCMP_256:
3568 case WLAN_CIPHER_SUITE_GCMP:
3569 case WLAN_CIPHER_SUITE_GCMP_256:
3570 pn = atomic64_inc_return(&key->conf.tx_pn);
3571 crypto_hdr[0] = pn;
3572 crypto_hdr[1] = pn >> 8;
96fc6efb 3573 crypto_hdr[3] = 0x20 | (key->conf.keyidx << 6);
bb42f2d1
THJ
3574 crypto_hdr[4] = pn >> 16;
3575 crypto_hdr[5] = pn >> 24;
3576 crypto_hdr[6] = pn >> 32;
3577 crypto_hdr[7] = pn >> 40;
3578 break;
3579 }
3580 }
03c3911d
RL
3581
3582 return TX_CONTINUE;
bb42f2d1
THJ
3583}
3584
17c18bf8 3585static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
bb42f2d1 3586 struct sta_info *sta,
17c18bf8
JB
3587 struct ieee80211_fast_tx *fast_tx,
3588 struct sk_buff *skb)
3589{
3590 struct ieee80211_local *local = sdata->local;
3591 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3592 int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3593 int hw_headroom = sdata->local->hw.extra_tx_headroom;
3594 struct ethhdr eth;
35f432a0 3595 struct ieee80211_tx_info *info;
17c18bf8
JB
3596 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3597 struct ieee80211_tx_data tx;
3598 ieee80211_tx_result r;
3599 struct tid_ampdu_tx *tid_tx = NULL;
3600 u8 tid = IEEE80211_NUM_TIDS;
3601
3602 /* control port protocol needs a lot of special handling */
3603 if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3604 return false;
3605
3606 /* only RFC 1042 SNAP */
3607 if (ethertype < ETH_P_802_3_MIN)
3608 return false;
3609
3610 /* don't handle TX status request here either */
3611 if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3612 return false;
3613
3614 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3615 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3616 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
472be00d
JB
3617 if (tid_tx) {
3618 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3619 return false;
3620 if (tid_tx->timeout)
3621 tid_tx->last_tx = jiffies;
3622 }
17c18bf8
JB
3623 }
3624
3625 /* after this point (skb is modified) we cannot return false */
3626
a4926abb
RL
3627 skb = skb_share_check(skb, GFP_ATOMIC);
3628 if (unlikely(!skb))
3629 return true;
17c18bf8 3630
6e0456b5
FF
3631 if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3632 ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
3633 return true;
3634
17c18bf8
JB
3635 /* will not be crypto-handled beyond what we do here, so use false
3636 * as the may-encrypt argument for the resize to not account for
3637 * more room than we already have in 'extra_head'
3638 */
3639 if (unlikely(ieee80211_skb_resize(sdata, skb,
3640 max_t(int, extra_head + hw_headroom -
3641 skb_headroom(skb), 0),
14f46c1e 3642 ENCRYPT_NO))) {
17c18bf8
JB
3643 kfree_skb(skb);
3644 return true;
3645 }
3646
3647 memcpy(&eth, skb->data, ETH_HLEN - 2);
d58ff351 3648 hdr = skb_push(skb, extra_head);
17c18bf8
JB
3649 memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3650 memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
3651 memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
3652
35f432a0 3653 info = IEEE80211_SKB_CB(skb);
17c18bf8
JB
3654 memset(info, 0, sizeof(*info));
3655 info->band = fast_tx->band;
3656 info->control.vif = &sdata->vif;
3657 info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3658 IEEE80211_TX_CTL_DONTFRAG |
3659 (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
69d41b5a
JB
3660 info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT |
3661 u32_encode_bits(IEEE80211_LINK_UNSPECIFIED,
3662 IEEE80211_TX_CTRL_MLO_LINK);
17c18bf8 3663
276d9e82
JN
3664#ifdef CONFIG_MAC80211_DEBUGFS
3665 if (local->force_tx_status)
3666 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3667#endif
3668
a786f96d
FF
3669 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3670 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3671 *ieee80211_get_qos_ctl(hdr) = tid;
3672 }
3673
17c18bf8
JB
3674 __skb_queue_head_init(&tx.skbs);
3675
3676 tx.flags = IEEE80211_TX_UNICAST;
3677 tx.local = local;
3678 tx.sdata = sdata;
3679 tx.sta = sta;
3680 tx.key = fast_tx->key;
3681
bb42f2d1
THJ
3682 if (ieee80211_queue_skb(local, sdata, sta, skb))
3683 return true;
17c18bf8 3684
03c3911d
RL
3685 tx.skb = skb;
3686 r = ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3687 fast_tx->key, &tx);
3688 tx.skb = NULL;
3689 if (r == TX_DROP) {
3690 kfree_skb(skb);
3691 return true;
3692 }
17c18bf8
JB
3693
3694 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3695 sdata = container_of(sdata->bss,
3696 struct ieee80211_sub_if_data, u.ap);
3697
3698 __skb_queue_tail(&tx.skbs, skb);
4fd0328d 3699 ieee80211_tx_frags(local, &sdata->vif, sta, &tx.skbs, false);
17c18bf8
JB
3700 return true;
3701}
3702
e0e2efff
THJ
3703struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3704 struct ieee80211_txq *txq)
3705{
3706 struct ieee80211_local *local = hw_to_local(hw);
3707 struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3708 struct ieee80211_hdr *hdr;
3709 struct sk_buff *skb = NULL;
3710 struct fq *fq = &local->fq;
3711 struct fq_tin *tin = &txqi->tin;
bb42f2d1
THJ
3712 struct ieee80211_tx_info *info;
3713 struct ieee80211_tx_data tx;
3714 ieee80211_tx_result r;
21a5d4c3 3715 struct ieee80211_vif *vif = txq->vif;
e0e2efff 3716
fb0e76ab
ES
3717 WARN_ON_ONCE(softirq_count() == 0);
3718
7a89233a
THJ
3719 if (!ieee80211_txq_airtime_check(hw, txq))
3720 return NULL;
3721
ded4698b 3722begin:
e0e2efff
THJ
3723 spin_lock_bh(&fq->lock);
3724
21a5d4c3
MP
3725 if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags) ||
3726 test_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags))
e0e2efff
THJ
3727 goto out;
3728
1153a747 3729 if (vif->txqs_stopped[txq->ac]) {
21a5d4c3
MP
3730 set_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags);
3731 goto out;
3732 }
3733
bb42f2d1
THJ
3734 /* Make sure fragments stay together. */
3735 skb = __skb_dequeue(&txqi->frags);
ca47b462
JB
3736 if (unlikely(skb)) {
3737 if (!(IEEE80211_SKB_CB(skb)->control.flags &
3738 IEEE80211_TX_INTCFL_NEED_TXPROCESSING))
3739 goto out;
3740 IEEE80211_SKB_CB(skb)->control.flags &=
3741 ~IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
3742 } else {
3743 skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
3744 }
bb42f2d1 3745
e0e2efff
THJ
3746 if (!skb)
3747 goto out;
3748
ded4698b
FF
3749 spin_unlock_bh(&fq->lock);
3750
e0e2efff 3751 hdr = (struct ieee80211_hdr *)skb->data;
bb42f2d1
THJ
3752 info = IEEE80211_SKB_CB(skb);
3753
3754 memset(&tx, 0, sizeof(tx));
3755 __skb_queue_head_init(&tx.skbs);
3756 tx.local = local;
3757 tx.skb = skb;
3758 tx.sdata = vif_to_sdata(info->control.vif);
3759
804fc6a2 3760 if (txq->sta) {
bb42f2d1 3761 tx.sta = container_of(txq->sta, struct sta_info, sta);
ce2e1ca7
JM
3762 /*
3763 * Drop unicast frames to unauthorised stations unless they are
804fc6a2 3764 * injected frames or EAPOL frames from the local station.
ce2e1ca7 3765 */
804fc6a2
MV
3766 if (unlikely(!(info->flags & IEEE80211_TX_CTL_INJECTED) &&
3767 ieee80211_is_data(hdr->frame_control) &&
be8c827f 3768 !ieee80211_vif_is_mesh(&tx.sdata->vif) &&
ce2e1ca7
JM
3769 tx.sdata->vif.type != NL80211_IFTYPE_OCB &&
3770 !is_multicast_ether_addr(hdr->addr1) &&
3771 !test_sta_flag(tx.sta, WLAN_STA_AUTHORIZED) &&
3772 (!(info->control.flags &
3773 IEEE80211_TX_CTRL_PORT_CTRL_PROTO) ||
3774 !ether_addr_equal(tx.sdata->vif.addr,
3775 hdr->addr2)))) {
3776 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
3777 ieee80211_free_txskb(&local->hw, skb);
3778 goto begin;
3779 }
3780 }
bb42f2d1
THJ
3781
3782 /*
3783 * The key can be removed while the packet was queued, so need to call
3784 * this here to get the current key.
3785 */
3786 r = ieee80211_tx_h_select_key(&tx);
3787 if (r != TX_CONTINUE) {
3788 ieee80211_free_txskb(&local->hw, skb);
3789 goto begin;
3790 }
3791
c1f4c9ed
FF
3792 if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
3793 info->flags |= IEEE80211_TX_CTL_AMPDU;
3794 else
3795 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
3796
3187ba0c
RL
3797 if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
3798 if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
3799 r = ieee80211_tx_h_rate_ctrl(&tx);
3800 if (r != TX_CONTINUE) {
3801 ieee80211_free_txskb(&local->hw, skb);
3802 goto begin;
3803 }
3804 }
50ff477a 3805 goto encap_out;
3187ba0c 3806 }
50ff477a 3807
bb42f2d1 3808 if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
e0e2efff
THJ
3809 struct sta_info *sta = container_of(txq->sta, struct sta_info,
3810 sta);
bb42f2d1 3811 u8 pn_offs = 0;
e0e2efff 3812
bb42f2d1
THJ
3813 if (tx.key &&
3814 (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
3815 pn_offs = ieee80211_hdrlen(hdr->frame_control);
3816
03c3911d
RL
3817 r = ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
3818 tx.key, &tx);
3819 if (r != TX_CONTINUE) {
3820 ieee80211_free_txskb(&local->hw, skb);
3821 goto begin;
3822 }
bb42f2d1
THJ
3823 } else {
3824 if (invoke_tx_handlers_late(&tx))
3825 goto begin;
3826
3827 skb = __skb_dequeue(&tx.skbs);
3828
ded4698b
FF
3829 if (!skb_queue_empty(&tx.skbs)) {
3830 spin_lock_bh(&fq->lock);
bb42f2d1 3831 skb_queue_splice_tail(&tx.skbs, &txqi->frags);
ded4698b
FF
3832 spin_unlock_bh(&fq->lock);
3833 }
e0e2efff
THJ
3834 }
3835
233e98dc 3836 if (skb_has_frag_list(skb) &&
1e1430d5
JB
3837 !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
3838 if (skb_linearize(skb)) {
3839 ieee80211_free_txskb(&local->hw, skb);
3840 goto begin;
3841 }
3842 }
3843
53168215
JB
3844 switch (tx.sdata->vif.type) {
3845 case NL80211_IFTYPE_MONITOR:
3846 if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
3847 vif = &tx.sdata->vif;
3848 break;
3849 }
3850 tx.sdata = rcu_dereference(local->monitor_sdata);
3851 if (tx.sdata) {
3852 vif = &tx.sdata->vif;
3853 info->hw_queue =
3854 vif->hw_queue[skb_get_queue_mapping(skb)];
3855 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
3856 ieee80211_free_txskb(&local->hw, skb);
3857 goto begin;
3858 } else {
3859 vif = NULL;
3860 }
3861 break;
3862 case NL80211_IFTYPE_AP_VLAN:
3863 tx.sdata = container_of(tx.sdata->bss,
3864 struct ieee80211_sub_if_data, u.ap);
fc0561dc 3865 fallthrough;
53168215
JB
3866 default:
3867 vif = &tx.sdata->vif;
3868 break;
3869 }
3870
50ff477a 3871encap_out:
53168215 3872 IEEE80211_SKB_CB(skb)->control.vif = vif;
7a89233a 3873
51d3cfaf 3874 if (tx.sta &&
ca98c47d 3875 wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) {
3ff901cb 3876 bool ampdu = txq->ac != IEEE80211_AC_VO;
7a89233a
THJ
3877 u32 airtime;
3878
3879 airtime = ieee80211_calc_expected_tx_airtime(hw, vif, txq->sta,
3ff901cb 3880 skb->len, ampdu);
7a89233a
THJ
3881 if (airtime) {
3882 airtime = ieee80211_info_set_tx_time_est(info, airtime);
3883 ieee80211_sta_update_pending_airtime(local, tx.sta,
3884 txq->ac,
3885 airtime,
3886 false);
3887 }
3888 }
3889
ded4698b 3890 return skb;
21a5d4c3 3891
e0e2efff
THJ
3892out:
3893 spin_unlock_bh(&fq->lock);
3894
e0e2efff
THJ
3895 return skb;
3896}
3897EXPORT_SYMBOL(ieee80211_tx_dequeue);
3898
9c1be3cd
FF
3899static inline s32 ieee80211_sta_deficit(struct sta_info *sta, u8 ac)
3900{
3901 struct airtime_info *air_info = &sta->airtime[ac];
3902
3903 return air_info->deficit - atomic_read(&air_info->aql_tx_pending);
3904}
3905
8ccc0702
FF
3906static void
3907ieee80211_txq_set_active(struct txq_info *txqi)
3908{
3909 struct sta_info *sta;
3910
3911 if (!txqi->txq.sta)
3912 return;
3913
3914 sta = container_of(txqi->txq.sta, struct sta_info, sta);
3915 sta->airtime[txqi->txq.ac].last_active = (u32)jiffies;
3916}
3917
3918static bool
3919ieee80211_txq_keep_active(struct txq_info *txqi)
3920{
3921 struct sta_info *sta;
3922 u32 diff;
3923
3924 if (!txqi->txq.sta)
3925 return false;
3926
3927 sta = container_of(txqi->txq.sta, struct sta_info, sta);
3928 if (ieee80211_sta_deficit(sta, txqi->txq.ac) >= 0)
3929 return false;
3930
3931 diff = (u32)jiffies - sta->airtime[txqi->txq.ac].last_active;
3932
3933 return diff <= AIRTIME_ACTIVE_DURATION;
3934}
3935
18667600
THJ
3936struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac)
3937{
3938 struct ieee80211_local *local = hw_to_local(hw);
5b989c18 3939 struct ieee80211_txq *ret = NULL;
942741da
FF
3940 struct txq_info *txqi = NULL, *head = NULL;
3941 bool found_eligible_txq = false;
18667600 3942
942741da 3943 spin_lock_bh(&local->active_txq_lock[ac]);
18667600 3944
8e4bac06
FF
3945 if (!local->schedule_round[ac])
3946 goto out;
3947
942741da
FF
3948 begin:
3949 txqi = list_first_entry_or_null(&local->active_txqs[ac],
3950 struct txq_info,
3951 schedule_order);
3952 if (!txqi)
2433647b
THJ
3953 goto out;
3954
942741da
FF
3955 if (txqi == head) {
3956 if (!found_eligible_txq)
3957 goto out;
3958 else
3959 found_eligible_txq = false;
2433647b 3960 }
3ace10f5 3961
942741da
FF
3962 if (!head)
3963 head = txqi;
3ace10f5 3964
942741da
FF
3965 if (txqi->txq.sta) {
3966 struct sta_info *sta = container_of(txqi->txq.sta,
3967 struct sta_info, sta);
3968 bool aql_check = ieee80211_txq_airtime_check(hw, &txqi->txq);
9c1be3cd 3969 s32 deficit = ieee80211_sta_deficit(sta, txqi->txq.ac);
2433647b 3970
942741da
FF
3971 if (aql_check)
3972 found_eligible_txq = true;
2433647b 3973
942741da
FF
3974 if (deficit < 0)
3975 sta->airtime[txqi->txq.ac].deficit +=
3976 sta->airtime_weight;
2433647b 3977
942741da
FF
3978 if (deficit < 0 || !aql_check) {
3979 list_move_tail(&txqi->schedule_order,
3980 &local->active_txqs[txqi->txq.ac]);
3981 goto begin;
2433647b 3982 }
b4809e94
THJ
3983 }
3984
942741da 3985 if (txqi->schedule_round == local->schedule_round[ac])
5b989c18 3986 goto out;
18667600 3987
942741da
FF
3988 list_del_init(&txqi->schedule_order);
3989 txqi->schedule_round = local->schedule_round[ac];
3990 ret = &txqi->txq;
5b989c18
FF
3991
3992out:
942741da
FF
3993 spin_unlock_bh(&local->active_txq_lock[ac]);
3994 return ret;
390298e8 3995}
942741da 3996EXPORT_SYMBOL(ieee80211_next_txq);
2433647b 3997
942741da 3998void __ieee80211_schedule_txq(struct ieee80211_hw *hw,
2433647b 3999 struct ieee80211_txq *txq,
942741da 4000 bool force)
2433647b
THJ
4001{
4002 struct ieee80211_local *local = hw_to_local(hw);
4003 struct txq_info *txqi = to_txq_info(txq);
8ccc0702 4004 bool has_queue;
2433647b 4005
942741da
FF
4006 spin_lock_bh(&local->active_txq_lock[txq->ac]);
4007
8ccc0702 4008 has_queue = force || txq_has_queue(txq);
942741da 4009 if (list_empty(&txqi->schedule_order) &&
8ccc0702 4010 (has_queue || ieee80211_txq_keep_active(txqi))) {
942741da
FF
4011 /* If airtime accounting is active, always enqueue STAs at the
4012 * head of the list to ensure that they only get moved to the
4013 * back by the airtime DRR scheduler once they have a negative
4014 * deficit. A station that already has a negative deficit will
4015 * get immediately moved to the back of the list on the next
4016 * call to ieee80211_next_txq().
4017 */
8ccc0702 4018 if (txqi->txq.sta && local->airtime_flags && has_queue &&
942741da
FF
4019 wiphy_ext_feature_isset(local->hw.wiphy,
4020 NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
4021 list_add(&txqi->schedule_order,
4022 &local->active_txqs[txq->ac]);
4023 else
4024 list_add_tail(&txqi->schedule_order,
4025 &local->active_txqs[txq->ac]);
8ccc0702
FF
4026 if (has_queue)
4027 ieee80211_txq_set_active(txqi);
942741da 4028 }
2433647b 4029
942741da 4030 spin_unlock_bh(&local->active_txq_lock[txq->ac]);
2433647b 4031}
942741da 4032EXPORT_SYMBOL(__ieee80211_schedule_txq);
390298e8 4033
e908435e
LB
4034DEFINE_STATIC_KEY_FALSE(aql_disable);
4035
3ace10f5
KY
4036bool ieee80211_txq_airtime_check(struct ieee80211_hw *hw,
4037 struct ieee80211_txq *txq)
4038{
942741da 4039 struct sta_info *sta;
3ace10f5
KY
4040 struct ieee80211_local *local = hw_to_local(hw);
4041
911bde0f 4042 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
3ace10f5
KY
4043 return true;
4044
e908435e
LB
4045 if (static_branch_unlikely(&aql_disable))
4046 return true;
4047
3ace10f5
KY
4048 if (!txq->sta)
4049 return true;
4050
73bc9e0a
JB
4051 if (unlikely(txq->tid == IEEE80211_NUM_TIDS))
4052 return true;
4053
942741da
FF
4054 sta = container_of(txq->sta, struct sta_info, sta);
4055 if (atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
4056 sta->airtime[txq->ac].aql_limit_low)
3ace10f5
KY
4057 return true;
4058
4059 if (atomic_read(&local->aql_total_pending_airtime) <
4060 local->aql_threshold &&
942741da
FF
4061 atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
4062 sta->airtime[txq->ac].aql_limit_high)
3ace10f5
KY
4063 return true;
4064
4065 return false;
4066}
4067EXPORT_SYMBOL(ieee80211_txq_airtime_check);
4068
8e4bac06
FF
4069static bool
4070ieee80211_txq_schedule_airtime_check(struct ieee80211_local *local, u8 ac)
4071{
4072 unsigned int num_txq = 0;
4073 struct txq_info *txq;
4074 u32 aql_limit;
4075
4076 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
4077 return true;
4078
4079 list_for_each_entry(txq, &local->active_txqs[ac], schedule_order)
4080 num_txq++;
4081
4082 aql_limit = (num_txq - 1) * local->aql_txq_limit_low[ac] / 2 +
4083 local->aql_txq_limit_high[ac];
4084
4085 return atomic_read(&local->aql_ac_pending_airtime[ac]) < aql_limit;
4086}
4087
b4809e94
THJ
4088bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
4089 struct ieee80211_txq *txq)
4090{
4091 struct ieee80211_local *local = hw_to_local(hw);
942741da
FF
4092 struct txq_info *iter, *tmp, *txqi = to_txq_info(txq);
4093 struct sta_info *sta;
4094 u8 ac = txq->ac;
b4809e94 4095
942741da 4096 spin_lock_bh(&local->active_txq_lock[ac]);
b4809e94 4097
942741da
FF
4098 if (!txqi->txq.sta)
4099 goto out;
2433647b 4100
942741da 4101 if (list_empty(&txqi->schedule_order))
b4809e94
THJ
4102 goto out;
4103
8e4bac06
FF
4104 if (!ieee80211_txq_schedule_airtime_check(local, ac))
4105 goto out;
4106
942741da
FF
4107 list_for_each_entry_safe(iter, tmp, &local->active_txqs[ac],
4108 schedule_order) {
4109 if (iter == txqi)
4110 break;
b4809e94 4111
942741da
FF
4112 if (!iter->txq.sta) {
4113 list_move_tail(&iter->schedule_order,
4114 &local->active_txqs[ac]);
4115 continue;
4116 }
4117 sta = container_of(iter->txq.sta, struct sta_info, sta);
9c1be3cd 4118 if (ieee80211_sta_deficit(sta, ac) < 0)
942741da
FF
4119 sta->airtime[ac].deficit += sta->airtime_weight;
4120 list_move_tail(&iter->schedule_order, &local->active_txqs[ac]);
b4809e94
THJ
4121 }
4122
942741da
FF
4123 sta = container_of(txqi->txq.sta, struct sta_info, sta);
4124 if (sta->airtime[ac].deficit >= 0)
4125 goto out;
4126
4127 sta->airtime[ac].deficit += sta->airtime_weight;
4128 list_move_tail(&txqi->schedule_order, &local->active_txqs[ac]);
4129 spin_unlock_bh(&local->active_txq_lock[ac]);
b4809e94 4130
942741da 4131 return false;
b4809e94 4132out:
942741da
FF
4133 if (!list_empty(&txqi->schedule_order))
4134 list_del_init(&txqi->schedule_order);
4135 spin_unlock_bh(&local->active_txq_lock[ac]);
4136
4137 return true;
b4809e94
THJ
4138}
4139EXPORT_SYMBOL(ieee80211_txq_may_transmit);
4140
18667600 4141void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
18667600
THJ
4142{
4143 struct ieee80211_local *local = hw_to_local(hw);
4144
942741da 4145 spin_lock_bh(&local->active_txq_lock[ac]);
8e4bac06
FF
4146
4147 if (ieee80211_txq_schedule_airtime_check(local, ac)) {
4148 local->schedule_round[ac]++;
4149 if (!local->schedule_round[ac])
4150 local->schedule_round[ac]++;
4151 } else {
4152 local->schedule_round[ac] = 0;
4153 }
4154
942741da 4155 spin_unlock_bh(&local->active_txq_lock[ac]);
18667600 4156}
5b989c18 4157EXPORT_SYMBOL(ieee80211_txq_schedule_start);
18667600 4158
4c9451ed
JB
4159void __ieee80211_subif_start_xmit(struct sk_buff *skb,
4160 struct net_device *dev,
06016772 4161 u32 info_flags,
a7528198
MT
4162 u32 ctrl_flags,
4163 u64 *cookie)
4c9451ed
JB
4164{
4165 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1974da8b 4166 struct ieee80211_local *local = sdata->local;
97ffe757 4167 struct sta_info *sta;
80616c0d 4168 struct sk_buff *next;
30f6cf96 4169 int len = skb->len;
4c9451ed
JB
4170
4171 if (unlikely(skb->len < ETH_HLEN)) {
4172 kfree_skb(skb);
4173 return;
4174 }
4175
4176 rcu_read_lock();
e2ebc74d 4177
2d981fdd
JB
4178 if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
4179 goto out_free;
4c9451ed 4180
1974da8b
FF
4181 if (IS_ERR(sta))
4182 sta = NULL;
4183
4184 if (local->ops->wake_tx_queue) {
4185 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
4186 skb_set_queue_mapping(skb, queue);
180ac48e 4187 skb_get_hash(skb);
1974da8b
FF
4188 }
4189
08a46c64
FF
4190 ieee80211_aggr_check(sdata, sta, skb);
4191
48c06708
MB
4192 sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
4193
1974da8b 4194 if (sta) {
17c18bf8
JB
4195 struct ieee80211_fast_tx *fast_tx;
4196
4197 fast_tx = rcu_dereference(sta->fast_tx);
4198
4199 if (fast_tx &&
bb42f2d1 4200 ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
17c18bf8
JB
4201 goto out;
4202 }
4203
80616c0d
JB
4204 if (skb_is_gso(skb)) {
4205 struct sk_buff *segs;
680a0dab 4206
80616c0d
JB
4207 segs = skb_gso_segment(skb, 0);
4208 if (IS_ERR(segs)) {
2d981fdd 4209 goto out_free;
80616c0d
JB
4210 } else if (segs) {
4211 consume_skb(skb);
4212 skb = segs;
4213 }
4214 } else {
4215 /* we cannot process non-linear frames on this path */
30f6cf96
FF
4216 if (skb_linearize(skb))
4217 goto out_free;
80616c0d
JB
4218
4219 /* the frame could be fragmented, software-encrypted, and other
4220 * things so we cannot really handle checksum offload with it -
4221 * fix it up in software before we handle anything else.
4222 */
4223 if (skb->ip_summed == CHECKSUM_PARTIAL) {
f603f1f3
JB
4224 skb_set_transport_header(skb,
4225 skb_checksum_start_offset(skb));
80616c0d
JB
4226 if (skb_checksum_help(skb))
4227 goto out_free;
4228 }
2d981fdd
JB
4229 }
4230
9f3ef3d7
JD
4231 skb_list_walk_safe(skb, skb, next) {
4232 skb_mark_not_on_list(skb);
80616c0d 4233
5af7fef3
MT
4234 if (skb->protocol == sdata->control_port_protocol)
4235 ctrl_flags |= IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
4236
06016772 4237 skb = ieee80211_build_hdr(sdata, skb, info_flags,
a7528198 4238 sta, ctrl_flags, cookie);
9f3ef3d7
JD
4239 if (IS_ERR(skb)) {
4240 kfree_skb_list(next);
80616c0d 4241 goto out;
9f3ef3d7 4242 }
e2ebc74d 4243
36ec144f 4244 dev_sw_netstats_tx_add(dev, 1, skb->len);
80616c0d 4245
08aca29a 4246 ieee80211_xmit(sdata, sta, skb);
80616c0d 4247 }
2d981fdd
JB
4248 goto out;
4249 out_free:
4250 kfree_skb(skb);
30f6cf96 4251 len = 0;
4c9451ed 4252 out:
30f6cf96
FF
4253 if (len)
4254 ieee80211_tpt_led_trig_tx(local, len);
55de908a 4255 rcu_read_unlock();
e2ebc74d
JB
4256}
4257
ebceec86
MB
4258static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
4259{
4260 struct ethhdr *eth;
4261 int err;
4262
4263 err = skb_ensure_writable(skb, ETH_HLEN);
4264 if (unlikely(err))
4265 return err;
4266
4267 eth = (void *)skb->data;
4268 ether_addr_copy(eth->h_dest, sta->sta.addr);
4269
4270 return 0;
4271}
4272
4273static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
4274 struct net_device *dev)
4275{
4276 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4277 const struct ethhdr *eth = (void *)skb->data;
4278 const struct vlan_ethhdr *ethvlan = (void *)skb->data;
4279 __be16 ethertype;
4280
ebceec86
MB
4281 switch (sdata->vif.type) {
4282 case NL80211_IFTYPE_AP_VLAN:
4283 if (sdata->u.vlan.sta)
4284 return false;
4285 if (sdata->wdev.use_4addr)
4286 return false;
fc0561dc 4287 fallthrough;
ebceec86
MB
4288 case NL80211_IFTYPE_AP:
4289 /* check runtime toggle for this bss */
4290 if (!sdata->bss->multicast_to_unicast)
4291 return false;
4292 break;
4293 default:
4294 return false;
4295 }
4296
4297 /* multicast to unicast conversion only for some payload */
4298 ethertype = eth->h_proto;
4299 if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
4300 ethertype = ethvlan->h_vlan_encapsulated_proto;
4301 switch (ethertype) {
4302 case htons(ETH_P_ARP):
4303 case htons(ETH_P_IP):
4304 case htons(ETH_P_IPV6):
4305 break;
4306 default:
4307 return false;
4308 }
4309
4310 return true;
4311}
4312
4313static void
4314ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
4315 struct sk_buff_head *queue)
4316{
4317 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4318 struct ieee80211_local *local = sdata->local;
4319 const struct ethhdr *eth = (struct ethhdr *)skb->data;
4320 struct sta_info *sta, *first = NULL;
4321 struct sk_buff *cloned_skb;
4322
4323 rcu_read_lock();
4324
4325 list_for_each_entry_rcu(sta, &local->sta_list, list) {
4326 if (sdata != sta->sdata)
4327 /* AP-VLAN mismatch */
4328 continue;
4329 if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
4330 /* do not send back to source */
4331 continue;
4332 if (!first) {
4333 first = sta;
4334 continue;
4335 }
4336 cloned_skb = skb_clone(skb, GFP_ATOMIC);
4337 if (!cloned_skb)
4338 goto multicast;
4339 if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
4340 dev_kfree_skb(cloned_skb);
4341 goto multicast;
4342 }
4343 __skb_queue_tail(queue, cloned_skb);
4344 }
4345
4346 if (likely(first)) {
4347 if (unlikely(ieee80211_change_da(skb, first)))
4348 goto multicast;
4349 __skb_queue_tail(queue, skb);
4350 } else {
4351 /* no STA connected, drop */
4352 kfree_skb(skb);
4353 skb = NULL;
4354 }
4355
4356 goto out;
4357multicast:
4358 __skb_queue_purge(queue);
4359 __skb_queue_tail(queue, skb);
4360out:
4361 rcu_read_unlock();
4362}
4363
963d0e8d
JB
4364static void ieee80211_mlo_multicast_tx_one(struct ieee80211_sub_if_data *sdata,
4365 struct sk_buff *skb, u32 ctrl_flags,
4366 unsigned int link_id)
4367{
4368 struct sk_buff *out;
4369
4370 out = skb_copy(skb, GFP_ATOMIC);
4371 if (!out)
4372 return;
4373
4374 ctrl_flags |= u32_encode_bits(link_id, IEEE80211_TX_CTRL_MLO_LINK);
4375 __ieee80211_subif_start_xmit(out, sdata->dev, 0, ctrl_flags, NULL);
4376}
4377
4378static void ieee80211_mlo_multicast_tx(struct net_device *dev,
4379 struct sk_buff *skb)
4380{
4381 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4382 unsigned long links = sdata->vif.valid_links;
4383 unsigned int link;
4384 u32 ctrl_flags = IEEE80211_TX_CTRL_MCAST_MLO_FIRST_TX;
4385
4386 if (hweight16(links) == 1) {
4387 ctrl_flags |= u32_encode_bits(ffs(links) - 1,
4388 IEEE80211_TX_CTRL_MLO_LINK);
4389
4390 __ieee80211_subif_start_xmit(skb, sdata->dev, 0, ctrl_flags,
4391 NULL);
4392 return;
4393 }
4394
4395 for_each_set_bit(link, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
4396 ieee80211_mlo_multicast_tx_one(sdata, skb, ctrl_flags, link);
4397 ctrl_flags = 0;
4398 }
4399 kfree_skb(skb);
4400}
4401
4c9451ed
JB
4402/**
4403 * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
4404 * @skb: packet to be sent
4405 * @dev: incoming interface
4406 *
4407 * On failure skb will be freed.
4408 */
24d342c5
LK
4409netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
4410 struct net_device *dev)
4411{
963d0e8d
JB
4412 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4413 const struct ethhdr *eth = (void *)skb->data;
4414
4415 if (likely(!is_multicast_ether_addr(eth->h_dest)))
4416 goto normal;
4417
ebceec86
MB
4418 if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
4419 struct sk_buff_head queue;
4420
4421 __skb_queue_head_init(&queue);
4422 ieee80211_convert_to_unicast(skb, dev, &queue);
4423 while ((skb = __skb_dequeue(&queue)))
963d0e8d
JB
4424 __ieee80211_subif_start_xmit(skb, dev, 0,
4425 IEEE80211_TX_CTRL_MLO_LINK_UNSPEC,
4426 NULL);
4427 } else if (sdata->vif.valid_links &&
4428 sdata->vif.type == NL80211_IFTYPE_AP &&
4429 !ieee80211_hw_check(&sdata->local->hw, MLO_MCAST_MULTI_LINK_TX)) {
4430 ieee80211_mlo_multicast_tx(dev, skb);
ebceec86 4431 } else {
963d0e8d
JB
4432normal:
4433 __ieee80211_subif_start_xmit(skb, dev, 0,
4434 IEEE80211_TX_CTRL_MLO_LINK_UNSPEC,
4435 NULL);
ebceec86
MB
4436 }
4437
24d342c5
LK
4438 return NETDEV_TX_OK;
4439}
e2ebc74d 4440
50ff477a 4441static bool ieee80211_tx_8023(struct ieee80211_sub_if_data *sdata,
30f6cf96 4442 struct sk_buff *skb, struct sta_info *sta,
50ff477a
JC
4443 bool txpending)
4444{
4445 struct ieee80211_local *local = sdata->local;
4446 struct ieee80211_tx_control control = {};
4447 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4448 struct ieee80211_sta *pubsta = NULL;
4449 unsigned long flags;
4450 int q = info->hw_queue;
4451
196900fd
LB
4452 if (sta)
4453 sk_pacing_shift_update(skb->sk, local->hw.tx_sk_pacing_shift);
4454
30f6cf96
FF
4455 ieee80211_tpt_led_trig_tx(local, skb->len);
4456
50ff477a
JC
4457 if (ieee80211_queue_skb(local, sdata, sta, skb))
4458 return true;
4459
4460 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4461
4462 if (local->queue_stop_reasons[q] ||
4463 (!txpending && !skb_queue_empty(&local->pending[q]))) {
4464 if (txpending)
4465 skb_queue_head(&local->pending[q], skb);
4466 else
4467 skb_queue_tail(&local->pending[q], skb);
4468
4469 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4470
4471 return false;
4472 }
4473
4474 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4475
4476 if (sta && sta->uploaded)
4477 pubsta = &sta->sta;
4478
4479 control.sta = pubsta;
4480
4481 drv_tx(local, &control, skb);
4482
4483 return true;
4484}
4485
4486static void ieee80211_8023_xmit(struct ieee80211_sub_if_data *sdata,
4487 struct net_device *dev, struct sta_info *sta,
6aea26ce 4488 struct ieee80211_key *key, struct sk_buff *skb)
50ff477a 4489{
a4926abb 4490 struct ieee80211_tx_info *info;
50ff477a 4491 struct ieee80211_local *local = sdata->local;
96ae9cd0
FF
4492 struct tid_ampdu_tx *tid_tx;
4493 u8 tid;
50ff477a 4494
5f8d69ea
FF
4495 if (local->ops->wake_tx_queue) {
4496 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
4497 skb_set_queue_mapping(skb, queue);
4498 skb_get_hash(skb);
4499 }
4500
50ff477a
JC
4501 if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning)) &&
4502 test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
4503 goto out_free;
4504
a4926abb
RL
4505 skb = skb_share_check(skb, GFP_ATOMIC);
4506 if (unlikely(!skb))
4507 return;
4508
4509 info = IEEE80211_SKB_CB(skb);
e61fbfca
VT
4510 memset(info, 0, sizeof(*info));
4511
08a46c64
FF
4512 ieee80211_aggr_check(sdata, sta, skb);
4513
aea6a3f0
FF
4514 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
4515 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
4516 if (tid_tx) {
4517 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
4518 /* fall back to non-offload slow path */
963d0e8d
JB
4519 __ieee80211_subif_start_xmit(skb, dev, 0,
4520 IEEE80211_TX_CTRL_MLO_LINK_UNSPEC,
4521 NULL);
aea6a3f0 4522 return;
96ae9cd0 4523 }
aea6a3f0
FF
4524
4525 info->flags |= IEEE80211_TX_CTL_AMPDU;
4526 if (tid_tx->timeout)
4527 tid_tx->last_tx = jiffies;
96ae9cd0
FF
4528 }
4529
aea6a3f0 4530 if (unlikely(skb->sk &&
50ff477a 4531 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS))
e61fbfca
VT
4532 info->ack_frame_id = ieee80211_store_ack_skb(local, skb,
4533 &info->flags, NULL);
50ff477a 4534
50ff477a
JC
4535 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
4536
36ec144f 4537 dev_sw_netstats_tx_add(dev, 1, skb->len);
50ff477a 4538
046d2e7c
S
4539 sta->deflink.tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
4540 sta->deflink.tx_stats.packets[skb_get_queue_mapping(skb)]++;
50ff477a
JC
4541
4542 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4543 sdata = container_of(sdata->bss,
4544 struct ieee80211_sub_if_data, u.ap);
4545
cc20ff2c 4546 info->flags |= IEEE80211_TX_CTL_HW_80211_ENCAP;
50ff477a
JC
4547 info->control.vif = &sdata->vif;
4548
ae045152
FF
4549 if (key)
4550 info->control.hw_key = &key->conf;
4551
30f6cf96 4552 ieee80211_tx_8023(sdata, skb, sta, false);
50ff477a
JC
4553
4554 return;
4555
4556out_free:
4557 kfree_skb(skb);
4558}
4559
4560netdev_tx_t ieee80211_subif_start_xmit_8023(struct sk_buff *skb,
4561 struct net_device *dev)
4562{
4563 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
aea6a3f0 4564 struct ethhdr *ehdr = (struct ethhdr *)skb->data;
6aea26ce 4565 struct ieee80211_key *key;
50ff477a 4566 struct sta_info *sta;
50ff477a
JC
4567
4568 if (unlikely(skb->len < ETH_HLEN)) {
4569 kfree_skb(skb);
4570 return NETDEV_TX_OK;
4571 }
4572
4573 rcu_read_lock();
4574
6aea26ce 4575 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
50ff477a 4576 kfree_skb(skb);
6aea26ce
FF
4577 goto out;
4578 }
4579
4580 if (unlikely(IS_ERR_OR_NULL(sta) || !sta->uploaded ||
4581 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
b101dd2d
FF
4582 sdata->control_port_protocol == ehdr->h_proto))
4583 goto skip_offload;
4584
4585 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4586 if (!key)
4587 key = rcu_dereference(sdata->default_unicast_key);
4588
4589 if (key && (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) ||
4590 key->conf.cipher == WLAN_CIPHER_SUITE_TKIP))
4591 goto skip_offload;
4592
4593 ieee80211_8023_xmit(sdata, dev, sta, key, skb);
4594 goto out;
50ff477a 4595
b101dd2d
FF
4596skip_offload:
4597 ieee80211_subif_start_xmit(skb, dev);
6aea26ce 4598out:
50ff477a
JC
4599 rcu_read_unlock();
4600
4601 return NETDEV_TX_OK;
4602}
4603
7528ec57
JB
4604struct sk_buff *
4605ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
4606 struct sk_buff *skb, u32 info_flags)
4607{
4608 struct ieee80211_hdr *hdr;
4609 struct ieee80211_tx_data tx = {
4610 .local = sdata->local,
4611 .sdata = sdata,
4612 };
97ffe757 4613 struct sta_info *sta;
7528ec57
JB
4614
4615 rcu_read_lock();
4616
97ffe757
JB
4617 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4618 kfree_skb(skb);
4619 skb = ERR_PTR(-EINVAL);
4620 goto out;
4621 }
4622
963d0e8d
JB
4623 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta,
4624 IEEE80211_TX_CTRL_MLO_LINK_UNSPEC, NULL);
7528ec57
JB
4625 if (IS_ERR(skb))
4626 goto out;
4627
4628 hdr = (void *)skb->data;
4629 tx.sta = sta_info_get(sdata, hdr->addr1);
4630 tx.skb = skb;
4631
4632 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
4633 rcu_read_unlock();
4634 kfree_skb(skb);
4635 return ERR_PTR(-EINVAL);
4636 }
4637
4638out:
4639 rcu_read_unlock();
4640 return skb;
4641}
4642
e2530083
JB
4643/*
4644 * ieee80211_clear_tx_pending may not be called in a context where
4645 * it is possible that it packets could come in again.
4646 */
e2ebc74d
JB
4647void ieee80211_clear_tx_pending(struct ieee80211_local *local)
4648{
1f98ab7f 4649 struct sk_buff *skb;
2de8e0d9 4650 int i;
e2ebc74d 4651
1f98ab7f
FF
4652 for (i = 0; i < local->hw.queues; i++) {
4653 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
4654 ieee80211_free_txskb(&local->hw, skb);
4655 }
e2ebc74d
JB
4656}
4657
7bb45683
JB
4658/*
4659 * Returns false if the frame couldn't be transmitted but was queued instead,
4660 * which in this case means re-queued -- take as an indication to stop sending
4661 * more pending frames.
4662 */
cd8ffc80
JB
4663static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
4664 struct sk_buff *skb)
4665{
4666 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4667 struct ieee80211_sub_if_data *sdata;
4668 struct sta_info *sta;
4669 struct ieee80211_hdr *hdr;
7bb45683 4670 bool result;
55de908a 4671 struct ieee80211_chanctx_conf *chanctx_conf;
cd8ffc80 4672
5061b0c2 4673 sdata = vif_to_sdata(info->control.vif);
cd8ffc80 4674
cc20ff2c 4675 if (info->control.flags & IEEE80211_TX_INTCFL_NEED_TXPROCESSING) {
eef25a66
JB
4676 /* update band only for non-MLD */
4677 if (!sdata->vif.valid_links) {
4678 chanctx_conf =
4679 rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
4680 if (unlikely(!chanctx_conf)) {
4681 dev_kfree_skb(skb);
4682 return true;
4683 }
4684 info->band = chanctx_conf->def.chan->band;
55de908a 4685 }
08aca29a 4686 result = ieee80211_tx(sdata, NULL, skb, true);
cc20ff2c 4687 } else if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
50ff477a
JC
4688 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4689 dev_kfree_skb(skb);
4690 return true;
4691 }
4692
4693 if (IS_ERR(sta) || (sta && !sta->uploaded))
4694 sta = NULL;
4695
30f6cf96 4696 result = ieee80211_tx_8023(sdata, skb, sta, true);
cd8ffc80 4697 } else {
252b86c4
JB
4698 struct sk_buff_head skbs;
4699
4700 __skb_queue_head_init(&skbs);
4701 __skb_queue_tail(&skbs, skb);
4702
cd8ffc80 4703 hdr = (struct ieee80211_hdr *)skb->data;
abe60632 4704 sta = sta_info_get(sdata, hdr->addr1);
cd8ffc80 4705
30f6cf96 4706 result = __ieee80211_tx(local, &skbs, sta, true);
cd8ffc80
JB
4707 }
4708
cd8ffc80
JB
4709 return result;
4710}
4711
e2530083 4712/*
3b8d81e0 4713 * Transmit all pending packets. Called from tasklet.
e2530083 4714 */
da1cad73 4715void ieee80211_tx_pending(struct tasklet_struct *t)
e2ebc74d 4716{
da1cad73
AP
4717 struct ieee80211_local *local = from_tasklet(local, t,
4718 tx_pending_tasklet);
2a577d98 4719 unsigned long flags;
cd8ffc80 4720 int i;
3b8d81e0 4721 bool txok;
e2ebc74d 4722
f0e72851 4723 rcu_read_lock();
e2530083 4724
3b8d81e0 4725 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
2a577d98
JB
4726 for (i = 0; i < local->hw.queues; i++) {
4727 /*
4728 * If queue is stopped by something other than due to pending
4729 * frames, or we have no pending frames, proceed to next queue.
4730 */
3b8d81e0 4731 if (local->queue_stop_reasons[i] ||
2a577d98 4732 skb_queue_empty(&local->pending[i]))
e2ebc74d 4733 continue;
e2530083 4734
2a577d98 4735 while (!skb_queue_empty(&local->pending[i])) {
3b8d81e0 4736 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
5061b0c2 4737 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
5061b0c2 4738
a7bc376c 4739 if (WARN_ON(!info->control.vif)) {
c3e7724b 4740 ieee80211_free_txskb(&local->hw, skb);
a7bc376c
JB
4741 continue;
4742 }
4743
3b8d81e0
JB
4744 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
4745 flags);
4746
4747 txok = ieee80211_tx_pending_skb(local, skb);
3b8d81e0
JB
4748 spin_lock_irqsave(&local->queue_stop_reason_lock,
4749 flags);
4750 if (!txok)
2a577d98 4751 break;
e2ebc74d 4752 }
7236fe29
JB
4753
4754 if (skb_queue_empty(&local->pending[i]))
3a25a8c8 4755 ieee80211_propagate_queue_wake(local, i);
e2ebc74d 4756 }
3b8d81e0 4757 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
2a577d98 4758
f0e72851 4759 rcu_read_unlock();
e2ebc74d
JB
4760}
4761
4762/* functions for drivers to get certain frames */
4763
eac70c13 4764static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
d8675a63 4765 struct ieee80211_link_data *link,
6ec8c332 4766 struct ps_data *ps, struct sk_buff *skb,
d8675a63 4767 bool is_template)
e2ebc74d
JB
4768{
4769 u8 *pos, *tim;
4770 int aid0 = 0;
4771 int i, have_bits = 0, n1, n2;
d8675a63 4772 struct ieee80211_bss_conf *link_conf = link->conf;
e2ebc74d
JB
4773
4774 /* Generate bitmap for TIM only if there are any STAs in power save
4775 * mode. */
d012a605 4776 if (atomic_read(&ps->num_sta_ps) > 0)
e2ebc74d
JB
4777 /* in the hope that this is faster than
4778 * checking byte-for-byte */
f359d3fe 4779 have_bits = !bitmap_empty((unsigned long *)ps->tim,
e2ebc74d 4780 IEEE80211_MAX_AID+1);
6ec8c332
AO
4781 if (!is_template) {
4782 if (ps->dtim_count == 0)
6e8912a5 4783 ps->dtim_count = link_conf->dtim_period - 1;
6ec8c332
AO
4784 else
4785 ps->dtim_count--;
4786 }
e2ebc74d 4787
4df864c1 4788 tim = pos = skb_put(skb, 6);
e2ebc74d
JB
4789 *pos++ = WLAN_EID_TIM;
4790 *pos++ = 4;
d012a605 4791 *pos++ = ps->dtim_count;
6e8912a5 4792 *pos++ = link_conf->dtim_period;
e2ebc74d 4793
d012a605 4794 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
e2ebc74d
JB
4795 aid0 = 1;
4796
d012a605 4797 ps->dtim_bc_mc = aid0 == 1;
512119b3 4798
e2ebc74d
JB
4799 if (have_bits) {
4800 /* Find largest even number N1 so that bits numbered 1 through
4801 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
4802 * (N2 + 1) x 8 through 2007 are 0. */
4803 n1 = 0;
4804 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
d012a605 4805 if (ps->tim[i]) {
e2ebc74d
JB
4806 n1 = i & 0xfe;
4807 break;
4808 }
4809 }
4810 n2 = n1;
4811 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
d012a605 4812 if (ps->tim[i]) {
e2ebc74d
JB
4813 n2 = i;
4814 break;
4815 }
4816 }
4817
4818 /* Bitmap control */
4819 *pos++ = n1 | aid0;
4820 /* Part Virt Bitmap */
5220da39 4821 skb_put(skb, n2 - n1);
d012a605 4822 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
e2ebc74d
JB
4823
4824 tim[1] = n2 - n1 + 4;
e2ebc74d
JB
4825 } else {
4826 *pos++ = aid0; /* Bitmap control */
4827 *pos++ = 0; /* Part Virt Bitmap */
4828 }
e2ebc74d
JB
4829}
4830
eac70c13 4831static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
d8675a63 4832 struct ieee80211_link_data *link,
6ec8c332 4833 struct ps_data *ps, struct sk_buff *skb,
d8675a63 4834 bool is_template)
eac70c13
MP
4835{
4836 struct ieee80211_local *local = sdata->local;
4837
4838 /*
4839 * Not very nice, but we want to allow the driver to call
4840 * ieee80211_beacon_get() as a response to the set_tim()
4841 * callback. That, however, is already invoked under the
4842 * sta_lock to guarantee consistent and race-free update
4843 * of the tim bitmap in mac80211 and the driver.
4844 */
4845 if (local->tim_in_locked_section) {
d8675a63 4846 __ieee80211_beacon_add_tim(sdata, link, ps, skb, is_template);
eac70c13 4847 } else {
1b91731d 4848 spin_lock_bh(&local->tim_lock);
d8675a63 4849 __ieee80211_beacon_add_tim(sdata, link, ps, skb, is_template);
1b91731d 4850 spin_unlock_bh(&local->tim_lock);
eac70c13
MP
4851 }
4852
4853 return 0;
4854}
4855
8552a434 4856static void ieee80211_set_beacon_cntdwn(struct ieee80211_sub_if_data *sdata,
6e8912a5 4857 struct beacon_data *beacon,
d8675a63 4858 struct ieee80211_link_data *link)
73da7d5b 4859{
5f9404ab 4860 u8 *beacon_data, count, max_count = 1;
73da7d5b 4861 struct probe_resp *resp;
cd7760e6 4862 size_t beacon_data_len;
5f9404ab 4863 u16 *bcn_offsets;
0d06d9ba 4864 int i;
cd7760e6
SW
4865
4866 switch (sdata->vif.type) {
4867 case NL80211_IFTYPE_AP:
4868 beacon_data = beacon->tail;
4869 beacon_data_len = beacon->tail_len;
4870 break;
4871 case NL80211_IFTYPE_ADHOC:
4872 beacon_data = beacon->head;
4873 beacon_data_len = beacon->head_len;
4874 break;
b8456a14
CYY
4875 case NL80211_IFTYPE_MESH_POINT:
4876 beacon_data = beacon->head;
4877 beacon_data_len = beacon->head_len;
4878 break;
cd7760e6
SW
4879 default:
4880 return;
4881 }
73da7d5b 4882
d8675a63 4883 resp = rcu_dereference(link->u.ap.probe_resp);
5f9404ab
JC
4884
4885 bcn_offsets = beacon->cntdwn_counter_offsets;
4886 count = beacon->cntdwn_current_counter;
d8675a63 4887 if (link->conf->csa_active)
5f9404ab 4888 max_count = IEEE80211_MAX_CNTDWN_COUNTERS_NUM;
0d06d9ba 4889
5f9404ab
JC
4890 for (i = 0; i < max_count; ++i) {
4891 if (bcn_offsets[i]) {
d8675a63 4892 if (WARN_ON_ONCE(bcn_offsets[i] >= beacon_data_len))
0d06d9ba 4893 return;
5f9404ab 4894 beacon_data[bcn_offsets[i]] = count;
73da7d5b 4895 }
af296bdb 4896
5f9404ab
JC
4897 if (sdata->vif.type == NL80211_IFTYPE_AP && resp) {
4898 u16 *resp_offsets = resp->cntdwn_counter_offsets;
4899
4900 resp->data[resp_offsets[i]] = count;
4901 }
73da7d5b 4902 }
1af586c9
AO
4903}
4904
8552a434 4905static u8 __ieee80211_beacon_update_cntdwn(struct beacon_data *beacon)
e996ec2a 4906{
8552a434 4907 beacon->cntdwn_current_counter--;
e996ec2a
WD
4908
4909 /* the counter should never reach 0 */
8552a434 4910 WARN_ON_ONCE(!beacon->cntdwn_current_counter);
e996ec2a 4911
8552a434 4912 return beacon->cntdwn_current_counter;
e996ec2a
WD
4913}
4914
8552a434 4915u8 ieee80211_beacon_update_cntdwn(struct ieee80211_vif *vif)
1af586c9
AO
4916{
4917 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
af296bdb
MK
4918 struct beacon_data *beacon = NULL;
4919 u8 count = 0;
0d06d9ba 4920
af296bdb
MK
4921 rcu_read_lock();
4922
4923 if (sdata->vif.type == NL80211_IFTYPE_AP)
bfd8403a 4924 beacon = rcu_dereference(sdata->deflink.u.ap.beacon);
af296bdb
MK
4925 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4926 beacon = rcu_dereference(sdata->u.ibss.presp);
4927 else if (ieee80211_vif_is_mesh(&sdata->vif))
4928 beacon = rcu_dereference(sdata->u.mesh.beacon);
4929
4930 if (!beacon)
4931 goto unlock;
4932
8552a434 4933 count = __ieee80211_beacon_update_cntdwn(beacon);
1af586c9 4934
af296bdb
MK
4935unlock:
4936 rcu_read_unlock();
4937 return count;
73da7d5b 4938}
8552a434 4939EXPORT_SYMBOL(ieee80211_beacon_update_cntdwn);
73da7d5b 4940
8552a434 4941void ieee80211_beacon_set_cntdwn(struct ieee80211_vif *vif, u8 counter)
03737001
GG
4942{
4943 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4944 struct beacon_data *beacon = NULL;
4945
4946 rcu_read_lock();
4947
4948 if (sdata->vif.type == NL80211_IFTYPE_AP)
bfd8403a 4949 beacon = rcu_dereference(sdata->deflink.u.ap.beacon);
03737001
GG
4950 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4951 beacon = rcu_dereference(sdata->u.ibss.presp);
4952 else if (ieee80211_vif_is_mesh(&sdata->vif))
4953 beacon = rcu_dereference(sdata->u.mesh.beacon);
4954
4955 if (!beacon)
4956 goto unlock;
4957
8552a434
JC
4958 if (counter < beacon->cntdwn_current_counter)
4959 beacon->cntdwn_current_counter = counter;
03737001
GG
4960
4961unlock:
4962 rcu_read_unlock();
4963}
8552a434 4964EXPORT_SYMBOL(ieee80211_beacon_set_cntdwn);
03737001 4965
8552a434 4966bool ieee80211_beacon_cntdwn_is_complete(struct ieee80211_vif *vif)
73da7d5b
SW
4967{
4968 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4969 struct beacon_data *beacon = NULL;
4970 u8 *beacon_data;
4971 size_t beacon_data_len;
73da7d5b
SW
4972 int ret = false;
4973
4974 if (!ieee80211_sdata_running(sdata))
4975 return false;
4976
4977 rcu_read_lock();
4978 if (vif->type == NL80211_IFTYPE_AP) {
bfd8403a 4979 beacon = rcu_dereference(sdata->deflink.u.ap.beacon);
73da7d5b
SW
4980 if (WARN_ON(!beacon || !beacon->tail))
4981 goto out;
4982 beacon_data = beacon->tail;
4983 beacon_data_len = beacon->tail_len;
cd7760e6
SW
4984 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
4985 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4986
4987 beacon = rcu_dereference(ifibss->presp);
4988 if (!beacon)
4989 goto out;
4990
b8456a14
CYY
4991 beacon_data = beacon->head;
4992 beacon_data_len = beacon->head_len;
4993 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
4994 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4995
4996 beacon = rcu_dereference(ifmsh->beacon);
4997 if (!beacon)
4998 goto out;
4999
cd7760e6
SW
5000 beacon_data = beacon->head;
5001 beacon_data_len = beacon->head_len;
73da7d5b
SW
5002 } else {
5003 WARN_ON(1);
5004 goto out;
5005 }
5006
8552a434 5007 if (!beacon->cntdwn_counter_offsets[0])
10d78f27
MK
5008 goto out;
5009
8552a434 5010 if (WARN_ON_ONCE(beacon->cntdwn_counter_offsets[0] > beacon_data_len))
73da7d5b
SW
5011 goto out;
5012
8552a434 5013 if (beacon_data[beacon->cntdwn_counter_offsets[0]] == 1)
73da7d5b 5014 ret = true;
8552a434 5015
73da7d5b
SW
5016 out:
5017 rcu_read_unlock();
5018
5019 return ret;
5020}
8552a434 5021EXPORT_SYMBOL(ieee80211_beacon_cntdwn_is_complete);
73da7d5b 5022
0a3a8436
JM
5023static int ieee80211_beacon_protect(struct sk_buff *skb,
5024 struct ieee80211_local *local,
6e8912a5 5025 struct ieee80211_sub_if_data *sdata,
d8675a63 5026 struct ieee80211_link_data *link)
0a3a8436
JM
5027{
5028 ieee80211_tx_result res;
5029 struct ieee80211_tx_data tx;
95247705 5030 struct sk_buff *check_skb;
0a3a8436
JM
5031
5032 memset(&tx, 0, sizeof(tx));
d8675a63 5033 tx.key = rcu_dereference(link->default_beacon_key);
0a3a8436
JM
5034 if (!tx.key)
5035 return 0;
5036 tx.local = local;
5037 tx.sdata = sdata;
5038 __skb_queue_head_init(&tx.skbs);
5039 __skb_queue_tail(&tx.skbs, skb);
5040 res = ieee80211_tx_h_encrypt(&tx);
95247705
JB
5041 check_skb = __skb_dequeue(&tx.skbs);
5042 /* we may crash after this, but it'd be a bug in crypto */
5043 WARN_ON(check_skb != skb);
0a3a8436 5044 if (WARN_ON_ONCE(res != TX_CONTINUE))
95247705 5045 return -EINVAL;
0a3a8436
JM
5046
5047 return 0;
5048}
5049
a6e34fde
AD
5050static void
5051ieee80211_beacon_get_finish(struct ieee80211_hw *hw,
5052 struct ieee80211_vif *vif,
d8675a63 5053 struct ieee80211_link_data *link,
a6e34fde
AD
5054 struct ieee80211_mutable_offsets *offs,
5055 struct beacon_data *beacon,
5056 struct sk_buff *skb,
5057 struct ieee80211_chanctx_conf *chanctx_conf,
d8675a63 5058 u16 csa_off_base)
a6e34fde
AD
5059{
5060 struct ieee80211_local *local = hw_to_local(hw);
5061 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5062 struct ieee80211_tx_info *info;
5063 enum nl80211_band band;
5064 struct ieee80211_tx_rate_control txrc;
5065
5066 /* CSA offsets */
5067 if (offs && beacon) {
5068 u16 i;
5069
5070 for (i = 0; i < IEEE80211_MAX_CNTDWN_COUNTERS_NUM; i++) {
5071 u16 csa_off = beacon->cntdwn_counter_offsets[i];
5072
5073 if (!csa_off)
5074 continue;
5075
5076 offs->cntdwn_counter_offs[i] = csa_off_base + csa_off;
5077 }
5078 }
5079
5080 band = chanctx_conf->def.chan->band;
5081 info = IEEE80211_SKB_CB(skb);
5082 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
5083 info->flags |= IEEE80211_TX_CTL_NO_ACK;
5084 info->band = band;
5085
5086 memset(&txrc, 0, sizeof(txrc));
5087 txrc.hw = hw;
5088 txrc.sband = local->hw.wiphy->bands[band];
d8675a63 5089 txrc.bss_conf = link->conf;
a6e34fde
AD
5090 txrc.skb = skb;
5091 txrc.reported_rate.idx = -1;
5092 if (sdata->beacon_rate_set && sdata->beacon_rateidx_mask[band])
5093 txrc.rate_idx_mask = sdata->beacon_rateidx_mask[band];
5094 else
5095 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
5096 txrc.bss = true;
5097 rate_control_get_rate(sdata, NULL, &txrc);
5098
5099 info->control.vif = vif;
5100 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
5101 IEEE80211_TX_CTL_ASSIGN_SEQ |
5102 IEEE80211_TX_CTL_FIRST_FRAGMENT;
5103}
5104
2b3171c6
LB
5105static void
5106ieee80211_beacon_add_mbssid(struct sk_buff *skb, struct beacon_data *beacon)
5107{
5108 int i;
5109
5110 if (!beacon->mbssid_ies)
5111 return;
5112
5113 for (i = 0; i < beacon->mbssid_ies->cnt; i++)
5114 skb_put_data(skb, beacon->mbssid_ies->elem[i].data,
5115 beacon->mbssid_ies->elem[i].len);
5116}
5117
a6e34fde
AD
5118static struct sk_buff *
5119ieee80211_beacon_get_ap(struct ieee80211_hw *hw,
5120 struct ieee80211_vif *vif,
d8675a63 5121 struct ieee80211_link_data *link,
a6e34fde
AD
5122 struct ieee80211_mutable_offsets *offs,
5123 bool is_template,
5124 struct beacon_data *beacon,
d8675a63 5125 struct ieee80211_chanctx_conf *chanctx_conf)
a6e34fde
AD
5126{
5127 struct ieee80211_local *local = hw_to_local(hw);
5128 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5129 struct ieee80211_if_ap *ap = &sdata->u.ap;
5130 struct sk_buff *skb = NULL;
5131 u16 csa_off_base = 0;
2b3171c6 5132 int mbssid_len;
a6e34fde
AD
5133
5134 if (beacon->cntdwn_counter_offsets[0]) {
5135 if (!is_template)
5136 ieee80211_beacon_update_cntdwn(vif);
5137
d8675a63 5138 ieee80211_set_beacon_cntdwn(sdata, beacon, link);
a6e34fde
AD
5139 }
5140
5141 /* headroom, head length,
2b3171c6 5142 * tail length, maximum TIM length and multiple BSSID length
a6e34fde 5143 */
2b3171c6 5144 mbssid_len = ieee80211_get_mbssid_beacon_len(beacon->mbssid_ies);
a6e34fde
AD
5145 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
5146 beacon->tail_len + 256 +
2b3171c6 5147 local->hw.extra_beacon_tailroom + mbssid_len);
a6e34fde
AD
5148 if (!skb)
5149 return NULL;
5150
5151 skb_reserve(skb, local->tx_headroom);
5152 skb_put_data(skb, beacon->head, beacon->head_len);
5153
d8675a63 5154 ieee80211_beacon_add_tim(sdata, link, &ap->ps, skb, is_template);
a6e34fde
AD
5155
5156 if (offs) {
5157 offs->tim_offset = beacon->head_len;
5158 offs->tim_length = skb->len - beacon->head_len;
5159 offs->cntdwn_counter_offs[0] = beacon->cntdwn_counter_offsets[0];
5160
2b3171c6
LB
5161 if (mbssid_len) {
5162 ieee80211_beacon_add_mbssid(skb, beacon);
5163 offs->mbssid_off = skb->len - mbssid_len;
5164 }
5165
a6e34fde
AD
5166 /* for AP the csa offsets are from tail */
5167 csa_off_base = skb->len;
5168 }
5169
5170 if (beacon->tail)
5171 skb_put_data(skb, beacon->tail, beacon->tail_len);
5172
d8675a63 5173 if (ieee80211_beacon_protect(skb, local, sdata, link) < 0)
a6e34fde
AD
5174 return NULL;
5175
d8675a63
JB
5176 ieee80211_beacon_get_finish(hw, vif, link, offs, beacon, skb,
5177 chanctx_conf, csa_off_base);
a6e34fde
AD
5178 return skb;
5179}
5180
6ec8c332
AO
5181static struct sk_buff *
5182__ieee80211_beacon_get(struct ieee80211_hw *hw,
5183 struct ieee80211_vif *vif,
5184 struct ieee80211_mutable_offsets *offs,
6e8912a5
ST
5185 bool is_template,
5186 unsigned int link_id)
e2ebc74d
JB
5187{
5188 struct ieee80211_local *local = hw_to_local(hw);
af296bdb 5189 struct beacon_data *beacon = NULL;
9d139c81 5190 struct sk_buff *skb = NULL;
e2ebc74d 5191 struct ieee80211_sub_if_data *sdata = NULL;
55de908a 5192 struct ieee80211_chanctx_conf *chanctx_conf;
d8675a63 5193 struct ieee80211_link_data *link;
8318d78a 5194
5dfdaf58 5195 rcu_read_lock();
e2ebc74d 5196
32bfd35d 5197 sdata = vif_to_sdata(vif);
d8675a63
JB
5198 link = rcu_dereference(sdata->link[link_id]);
5199 if (!link)
5200 goto out;
6e8912a5 5201 chanctx_conf =
d8675a63 5202 rcu_dereference(link->conf->chanctx_conf);
e2ebc74d 5203
55de908a 5204 if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
eb3e554b
FF
5205 goto out;
5206
6ec8c332
AO
5207 if (offs)
5208 memset(offs, 0, sizeof(*offs));
eddcbb94 5209
05c914fe 5210 if (sdata->vif.type == NL80211_IFTYPE_AP) {
d8675a63 5211 beacon = rcu_dereference(link->u.ap.beacon);
a6e34fde 5212 if (!beacon)
9d139c81 5213 goto out;
a6e34fde 5214
d8675a63
JB
5215 skb = ieee80211_beacon_get_ap(hw, vif, link, offs, is_template,
5216 beacon, chanctx_conf);
05c914fe 5217 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
46900298 5218 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
9d139c81 5219 struct ieee80211_hdr *hdr;
33b64eb2 5220
af296bdb
MK
5221 beacon = rcu_dereference(ifibss->presp);
5222 if (!beacon)
9d139c81
JB
5223 goto out;
5224
8552a434 5225 if (beacon->cntdwn_counter_offsets[0]) {
1af586c9 5226 if (!is_template)
8552a434 5227 __ieee80211_beacon_update_cntdwn(beacon);
cd7760e6 5228
d8675a63 5229 ieee80211_set_beacon_cntdwn(sdata, beacon, link);
1af586c9 5230 }
cd7760e6 5231
af296bdb 5232 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
70dabeb7 5233 local->hw.extra_beacon_tailroom);
9d139c81
JB
5234 if (!skb)
5235 goto out;
c3ffeab4 5236 skb_reserve(skb, local->tx_headroom);
59ae1d12 5237 skb_put_data(skb, beacon->head, beacon->head_len);
9d139c81
JB
5238
5239 hdr = (struct ieee80211_hdr *) skb->data;
e7827a70
HH
5240 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
5241 IEEE80211_STYPE_BEACON);
a6e34fde 5242
d8675a63
JB
5243 ieee80211_beacon_get_finish(hw, vif, link, offs, beacon, skb,
5244 chanctx_conf, 0);
902acc78 5245 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
dbf498fb 5246 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
472dbc45 5247
af296bdb
MK
5248 beacon = rcu_dereference(ifmsh->beacon);
5249 if (!beacon)
ac1bd846 5250 goto out;
ac1bd846 5251
8552a434 5252 if (beacon->cntdwn_counter_offsets[0]) {
1af586c9
AO
5253 if (!is_template)
5254 /* TODO: For mesh csa_counter is in TU, so
5255 * decrementing it by one isn't correct, but
5256 * for now we leave it consistent with overall
5257 * mac80211's behavior.
5258 */
8552a434 5259 __ieee80211_beacon_update_cntdwn(beacon);
1af586c9 5260
d8675a63 5261 ieee80211_set_beacon_cntdwn(sdata, beacon, link);
1af586c9 5262 }
b8456a14 5263
dbf498fb 5264 if (ifmsh->sync_ops)
445cd452 5265 ifmsh->sync_ops->adjust_tsf(sdata, beacon);
dbf498fb 5266
3b69a9c5 5267 skb = dev_alloc_skb(local->tx_headroom +
af296bdb 5268 beacon->head_len +
3f52b7e3 5269 256 + /* TIM IE */
af296bdb 5270 beacon->tail_len +
70dabeb7 5271 local->hw.extra_beacon_tailroom);
902acc78
JB
5272 if (!skb)
5273 goto out;
2b5e1967 5274 skb_reserve(skb, local->tx_headroom);
59ae1d12 5275 skb_put_data(skb, beacon->head, beacon->head_len);
d8675a63
JB
5276 ieee80211_beacon_add_tim(sdata, link, &ifmsh->ps, skb,
5277 is_template);
6ec8c332
AO
5278
5279 if (offs) {
af296bdb
MK
5280 offs->tim_offset = beacon->head_len;
5281 offs->tim_length = skb->len - beacon->head_len;
6ec8c332
AO
5282 }
5283
59ae1d12 5284 skb_put_data(skb, beacon->tail, beacon->tail_len);
d8675a63
JB
5285 ieee80211_beacon_get_finish(hw, vif, link, offs, beacon, skb,
5286 chanctx_conf, 0);
9d139c81
JB
5287 } else {
5288 WARN_ON(1);
5dfdaf58 5289 goto out;
e2ebc74d
JB
5290 }
5291
e6a9854b 5292 out:
5dfdaf58 5293 rcu_read_unlock();
e2ebc74d 5294 return skb;
6ec8c332
AO
5295
5296}
5297
5298struct sk_buff *
5299ieee80211_beacon_get_template(struct ieee80211_hw *hw,
5300 struct ieee80211_vif *vif,
6e8912a5
ST
5301 struct ieee80211_mutable_offsets *offs,
5302 unsigned int link_id)
6ec8c332 5303{
6e8912a5 5304 return __ieee80211_beacon_get(hw, vif, offs, true, link_id);
6ec8c332
AO
5305}
5306EXPORT_SYMBOL(ieee80211_beacon_get_template);
5307
5308struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
5309 struct ieee80211_vif *vif,
6e8912a5
ST
5310 u16 *tim_offset, u16 *tim_length,
5311 unsigned int link_id)
6ec8c332
AO
5312{
5313 struct ieee80211_mutable_offsets offs = {};
6e8912a5
ST
5314 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false,
5315 link_id);
35afa588 5316 struct sk_buff *copy;
35afa588
HS
5317 int shift;
5318
5319 if (!bcn)
5320 return bcn;
6ec8c332
AO
5321
5322 if (tim_offset)
5323 *tim_offset = offs.tim_offset;
5324
5325 if (tim_length)
5326 *tim_length = offs.tim_length;
5327
35afa588
HS
5328 if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
5329 !hw_to_local(hw)->monitors)
5330 return bcn;
5331
5332 /* send a copy to monitor interfaces */
5333 copy = skb_copy(bcn, GFP_ATOMIC);
5334 if (!copy)
5335 return bcn;
5336
5337 shift = ieee80211_vif_get_shift(vif);
e5c0ee01 5338 ieee80211_tx_monitor(hw_to_local(hw), copy, 1, shift, false, NULL);
35afa588 5339
6ec8c332 5340 return bcn;
e2ebc74d 5341}
eddcbb94 5342EXPORT_SYMBOL(ieee80211_beacon_get_tim);
e2ebc74d 5343
02945821
AN
5344struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
5345 struct ieee80211_vif *vif)
5346{
aa7a0080
ES
5347 struct sk_buff *skb = NULL;
5348 struct probe_resp *presp = NULL;
02945821
AN
5349 struct ieee80211_hdr *hdr;
5350 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5351
5352 if (sdata->vif.type != NL80211_IFTYPE_AP)
5353 return NULL;
5354
5355 rcu_read_lock();
bfd8403a 5356 presp = rcu_dereference(sdata->deflink.u.ap.probe_resp);
02945821
AN
5357 if (!presp)
5358 goto out;
5359
aa7a0080 5360 skb = dev_alloc_skb(presp->len);
02945821
AN
5361 if (!skb)
5362 goto out;
5363
59ae1d12 5364 skb_put_data(skb, presp->data, presp->len);
aa7a0080 5365
02945821
AN
5366 hdr = (struct ieee80211_hdr *) skb->data;
5367 memset(hdr->addr1, 0, sizeof(hdr->addr1));
5368
5369out:
5370 rcu_read_unlock();
5371 return skb;
5372}
5373EXPORT_SYMBOL(ieee80211_proberesp_get);
5374
295b02c4
AD
5375struct sk_buff *ieee80211_get_fils_discovery_tmpl(struct ieee80211_hw *hw,
5376 struct ieee80211_vif *vif)
5377{
5378 struct sk_buff *skb = NULL;
5379 struct fils_discovery_data *tmpl = NULL;
5380 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5381
5382 if (sdata->vif.type != NL80211_IFTYPE_AP)
5383 return NULL;
5384
5385 rcu_read_lock();
bfd8403a 5386 tmpl = rcu_dereference(sdata->deflink.u.ap.fils_discovery);
295b02c4
AD
5387 if (!tmpl) {
5388 rcu_read_unlock();
5389 return NULL;
5390 }
5391
5392 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
5393 if (skb) {
5394 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
5395 skb_put_data(skb, tmpl->data, tmpl->len);
5396 }
5397
5398 rcu_read_unlock();
5399 return skb;
5400}
5401EXPORT_SYMBOL(ieee80211_get_fils_discovery_tmpl);
5402
632189a0
AD
5403struct sk_buff *
5404ieee80211_get_unsol_bcast_probe_resp_tmpl(struct ieee80211_hw *hw,
5405 struct ieee80211_vif *vif)
5406{
5407 struct sk_buff *skb = NULL;
5408 struct unsol_bcast_probe_resp_data *tmpl = NULL;
5409 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5410
5411 if (sdata->vif.type != NL80211_IFTYPE_AP)
5412 return NULL;
5413
5414 rcu_read_lock();
bfd8403a 5415 tmpl = rcu_dereference(sdata->deflink.u.ap.unsol_bcast_probe_resp);
632189a0
AD
5416 if (!tmpl) {
5417 rcu_read_unlock();
5418 return NULL;
5419 }
5420
5421 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
5422 if (skb) {
5423 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
5424 skb_put_data(skb, tmpl->data, tmpl->len);
5425 }
5426
5427 rcu_read_unlock();
5428 return skb;
5429}
5430EXPORT_SYMBOL(ieee80211_get_unsol_bcast_probe_resp_tmpl);
5431
7044cc56
KV
5432struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
5433 struct ieee80211_vif *vif)
5434{
5435 struct ieee80211_sub_if_data *sdata;
7044cc56
KV
5436 struct ieee80211_pspoll *pspoll;
5437 struct ieee80211_local *local;
5438 struct sk_buff *skb;
5439
5440 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5441 return NULL;
5442
5443 sdata = vif_to_sdata(vif);
7044cc56
KV
5444 local = sdata->local;
5445
5446 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
d15b8459 5447 if (!skb)
7044cc56 5448 return NULL;
d15b8459 5449
7044cc56
KV
5450 skb_reserve(skb, local->hw.extra_tx_headroom);
5451
b080db58 5452 pspoll = skb_put_zero(skb, sizeof(*pspoll));
7044cc56
KV
5453 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
5454 IEEE80211_STYPE_PSPOLL);
f276e20b 5455 pspoll->aid = cpu_to_le16(sdata->vif.cfg.aid);
7044cc56
KV
5456
5457 /* aid in PS-Poll has its two MSBs each set to 1 */
5458 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
5459
bfd8403a 5460 memcpy(pspoll->bssid, sdata->deflink.u.mgd.bssid, ETH_ALEN);
7044cc56
KV
5461 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
5462
5463 return skb;
5464}
5465EXPORT_SYMBOL(ieee80211_pspoll_get);
5466
5467struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
7b6ddeaf
JB
5468 struct ieee80211_vif *vif,
5469 bool qos_ok)
7044cc56
KV
5470{
5471 struct ieee80211_hdr_3addr *nullfunc;
5472 struct ieee80211_sub_if_data *sdata;
7044cc56
KV
5473 struct ieee80211_local *local;
5474 struct sk_buff *skb;
7b6ddeaf 5475 bool qos = false;
7044cc56
KV
5476
5477 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5478 return NULL;
5479
5480 sdata = vif_to_sdata(vif);
7044cc56
KV
5481 local = sdata->local;
5482
7b6ddeaf
JB
5483 if (qos_ok) {
5484 struct sta_info *sta;
5485
5486 rcu_read_lock();
bfd8403a 5487 sta = sta_info_get(sdata, sdata->deflink.u.mgd.bssid);
7b6ddeaf
JB
5488 qos = sta && sta->sta.wme;
5489 rcu_read_unlock();
5490 }
5491
5492 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5493 sizeof(*nullfunc) + 2);
d15b8459 5494 if (!skb)
7044cc56 5495 return NULL;
d15b8459 5496
7044cc56
KV
5497 skb_reserve(skb, local->hw.extra_tx_headroom);
5498
b080db58 5499 nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
7044cc56
KV
5500 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
5501 IEEE80211_STYPE_NULLFUNC |
5502 IEEE80211_FCTL_TODS);
7b6ddeaf 5503 if (qos) {
e0ba7095 5504 __le16 qoshdr = cpu_to_le16(7);
7b6ddeaf
JB
5505
5506 BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
5507 IEEE80211_STYPE_NULLFUNC) !=
5508 IEEE80211_STYPE_QOS_NULLFUNC);
5509 nullfunc->frame_control |=
5510 cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
5511 skb->priority = 7;
5512 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
e0ba7095 5513 skb_put_data(skb, &qoshdr, sizeof(qoshdr));
7b6ddeaf
JB
5514 }
5515
bfd8403a 5516 memcpy(nullfunc->addr1, sdata->deflink.u.mgd.bssid, ETH_ALEN);
7044cc56 5517 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
bfd8403a 5518 memcpy(nullfunc->addr3, sdata->deflink.u.mgd.bssid, ETH_ALEN);
7044cc56
KV
5519
5520 return skb;
5521}
5522EXPORT_SYMBOL(ieee80211_nullfunc_get);
5523
05e54ea6 5524struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
a344d677 5525 const u8 *src_addr,
05e54ea6 5526 const u8 *ssid, size_t ssid_len,
b9a9ada1 5527 size_t tailroom)
05e54ea6 5528{
a344d677 5529 struct ieee80211_local *local = hw_to_local(hw);
05e54ea6
KV
5530 struct ieee80211_hdr_3addr *hdr;
5531 struct sk_buff *skb;
5532 size_t ie_ssid_len;
5533 u8 *pos;
5534
05e54ea6
KV
5535 ie_ssid_len = 2 + ssid_len;
5536
5537 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
b9a9ada1 5538 ie_ssid_len + tailroom);
d15b8459 5539 if (!skb)
05e54ea6 5540 return NULL;
05e54ea6
KV
5541
5542 skb_reserve(skb, local->hw.extra_tx_headroom);
5543
b080db58 5544 hdr = skb_put_zero(skb, sizeof(*hdr));
05e54ea6
KV
5545 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
5546 IEEE80211_STYPE_PROBE_REQ);
e83e6541 5547 eth_broadcast_addr(hdr->addr1);
a344d677 5548 memcpy(hdr->addr2, src_addr, ETH_ALEN);
e83e6541 5549 eth_broadcast_addr(hdr->addr3);
05e54ea6
KV
5550
5551 pos = skb_put(skb, ie_ssid_len);
5552 *pos++ = WLAN_EID_SSID;
5553 *pos++ = ssid_len;
88c868c4 5554 if (ssid_len)
05e54ea6
KV
5555 memcpy(pos, ssid, ssid_len);
5556 pos += ssid_len;
5557
05e54ea6
KV
5558 return skb;
5559}
5560EXPORT_SYMBOL(ieee80211_probereq_get);
5561
32bfd35d 5562void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
e2ebc74d 5563 const void *frame, size_t frame_len,
e039fa4a 5564 const struct ieee80211_tx_info *frame_txctl,
e2ebc74d
JB
5565 struct ieee80211_rts *rts)
5566{
5567 const struct ieee80211_hdr *hdr = frame;
e2ebc74d 5568
065e9605
HH
5569 rts->frame_control =
5570 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
32bfd35d
JB
5571 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
5572 frame_txctl);
e2ebc74d
JB
5573 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
5574 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
5575}
5576EXPORT_SYMBOL(ieee80211_rts_get);
5577
32bfd35d 5578void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
e2ebc74d 5579 const void *frame, size_t frame_len,
e039fa4a 5580 const struct ieee80211_tx_info *frame_txctl,
e2ebc74d
JB
5581 struct ieee80211_cts *cts)
5582{
5583 const struct ieee80211_hdr *hdr = frame;
e2ebc74d 5584
065e9605
HH
5585 cts->frame_control =
5586 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
32bfd35d
JB
5587 cts->duration = ieee80211_ctstoself_duration(hw, vif,
5588 frame_len, frame_txctl);
e2ebc74d
JB
5589 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
5590}
5591EXPORT_SYMBOL(ieee80211_ctstoself_get);
5592
5593struct sk_buff *
32bfd35d 5594ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
e039fa4a 5595 struct ieee80211_vif *vif)
e2ebc74d
JB
5596{
5597 struct ieee80211_local *local = hw_to_local(hw);
747cf5e9 5598 struct sk_buff *skb = NULL;
5cf121c3 5599 struct ieee80211_tx_data tx;
e2ebc74d 5600 struct ieee80211_sub_if_data *sdata;
d012a605 5601 struct ps_data *ps;
e039fa4a 5602 struct ieee80211_tx_info *info;
55de908a 5603 struct ieee80211_chanctx_conf *chanctx_conf;
e2ebc74d 5604
32bfd35d 5605 sdata = vif_to_sdata(vif);
5dfdaf58 5606
5dfdaf58 5607 rcu_read_lock();
d0a9123e 5608 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
5dfdaf58 5609
d012a605
MP
5610 if (!chanctx_conf)
5611 goto out;
5612
5613 if (sdata->vif.type == NL80211_IFTYPE_AP) {
5614 struct beacon_data *beacon =
bfd8403a 5615 rcu_dereference(sdata->deflink.u.ap.beacon);
d012a605
MP
5616
5617 if (!beacon || !beacon->head)
5618 goto out;
5619
5620 ps = &sdata->u.ap.ps;
3f52b7e3
MP
5621 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
5622 ps = &sdata->u.mesh.ps;
d012a605 5623 } else {
747cf5e9 5624 goto out;
d012a605 5625 }
5dfdaf58 5626
d012a605 5627 if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
747cf5e9 5628 goto out; /* send buffered bc/mc only after DTIM beacon */
e039fa4a 5629
e2ebc74d 5630 while (1) {
d012a605 5631 skb = skb_dequeue(&ps->bc_buf);
e2ebc74d 5632 if (!skb)
747cf5e9 5633 goto out;
e2ebc74d
JB
5634 local->total_ps_buffered--;
5635
d012a605 5636 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
e2ebc74d
JB
5637 struct ieee80211_hdr *hdr =
5638 (struct ieee80211_hdr *) skb->data;
5639 /* more buffered multicast/broadcast frames ==> set
5640 * MoreData flag in IEEE 802.11 header to inform PS
5641 * STAs */
5642 hdr->frame_control |=
5643 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
5644 }
5645
112c44b2 5646 if (sdata->vif.type == NL80211_IFTYPE_AP)
7cbf9d01 5647 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
7c10770f 5648 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
e2ebc74d 5649 break;
6b07d9ca 5650 ieee80211_free_txskb(hw, skb);
e2ebc74d 5651 }
e039fa4a
JB
5652
5653 info = IEEE80211_SKB_CB(skb);
5654
5cf121c3 5655 tx.flags |= IEEE80211_TX_PS_BUFFERED;
4bf88530 5656 info->band = chanctx_conf->def.chan->band;
e2ebc74d 5657
97b045d6 5658 if (invoke_tx_handlers(&tx))
e2ebc74d 5659 skb = NULL;
97b045d6 5660 out:
d0709a65 5661 rcu_read_unlock();
e2ebc74d
JB
5662
5663 return skb;
5664}
5665EXPORT_SYMBOL(ieee80211_get_buffered_bc);
3b8d81e0 5666
b6da911b
LK
5667int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5668{
5669 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5670 struct ieee80211_sub_if_data *sdata = sta->sdata;
5671 struct ieee80211_local *local = sdata->local;
5672 int ret;
5673 u32 queues;
5674
5675 lockdep_assert_held(&local->sta_mtx);
5676
5677 /* only some cases are supported right now */
5678 switch (sdata->vif.type) {
5679 case NL80211_IFTYPE_STATION:
5680 case NL80211_IFTYPE_AP:
5681 case NL80211_IFTYPE_AP_VLAN:
5682 break;
5683 default:
5684 WARN_ON(1);
5685 return -EINVAL;
5686 }
5687
5688 if (WARN_ON(tid >= IEEE80211_NUM_UPS))
5689 return -EINVAL;
5690
5691 if (sta->reserved_tid == tid) {
5692 ret = 0;
5693 goto out;
5694 }
5695
5696 if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
5697 sdata_err(sdata, "TID reservation already active\n");
5698 ret = -EALREADY;
5699 goto out;
5700 }
5701
5702 ieee80211_stop_vif_queues(sdata->local, sdata,
5703 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5704
5705 synchronize_net();
5706
5707 /* Tear down BA sessions so we stop aggregating on this TID */
30686bf7 5708 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
b6da911b
LK
5709 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5710 __ieee80211_stop_tx_ba_session(sta, tid,
5711 AGG_STOP_LOCAL_REQUEST);
5712 }
5713
5714 queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
3b24f4c6 5715 __ieee80211_flush_queues(local, sdata, queues, false);
b6da911b
LK
5716
5717 sta->reserved_tid = tid;
5718
5719 ieee80211_wake_vif_queues(local, sdata,
5720 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5721
30686bf7 5722 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
b6da911b
LK
5723 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5724
5725 ret = 0;
5726 out:
5727 return ret;
5728}
5729EXPORT_SYMBOL(ieee80211_reserve_tid);
5730
5731void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5732{
5733 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5734 struct ieee80211_sub_if_data *sdata = sta->sdata;
5735
5736 lockdep_assert_held(&sdata->local->sta_mtx);
5737
5738 /* only some cases are supported right now */
5739 switch (sdata->vif.type) {
5740 case NL80211_IFTYPE_STATION:
5741 case NL80211_IFTYPE_AP:
5742 case NL80211_IFTYPE_AP_VLAN:
5743 break;
5744 default:
5745 WARN_ON(1);
5746 return;
5747 }
5748
5749 if (tid != sta->reserved_tid) {
5750 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
5751 return;
5752 }
5753
5754 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
5755}
5756EXPORT_SYMBOL(ieee80211_unreserve_tid);
5757
55de908a 5758void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
e1e68b14 5759 struct sk_buff *skb, int tid, int link_id,
08aca29a 5760 enum nl80211_band band)
3b8d81e0 5761{
69d41b5a 5762 const struct ieee80211_hdr *hdr = (void *)skb->data;
731977e9 5763 int ac = ieee80211_ac_from_tid(tid);
69d41b5a 5764 unsigned int link;
3a25a8c8 5765
d57a544d 5766 skb_reset_mac_header(skb);
3a25a8c8 5767 skb_set_queue_mapping(skb, ac);
cf6bb79a 5768 skb->priority = tid;
cf0277e7 5769
89afe614
JB
5770 skb->dev = sdata->dev;
5771
69d41b5a
JB
5772 BUILD_BUG_ON(IEEE80211_LINK_UNSPECIFIED < IEEE80211_MLD_MAX_NUM_LINKS);
5773 BUILD_BUG_ON(!FIELD_FIT(IEEE80211_TX_CTRL_MLO_LINK,
5774 IEEE80211_LINK_UNSPECIFIED));
5775
5776 if (!sdata->vif.valid_links) {
5777 link = 0;
e1e68b14
JB
5778 } else if (link_id >= 0) {
5779 link = link_id;
69d41b5a
JB
5780 } else if (memcmp(sdata->vif.addr, hdr->addr2, ETH_ALEN) == 0) {
5781 /* address from the MLD */
5782 link = IEEE80211_LINK_UNSPECIFIED;
5783 } else {
5784 /* otherwise must be addressed from a link */
d8675a63 5785 rcu_read_lock();
69d41b5a 5786 for (link = 0; link < ARRAY_SIZE(sdata->vif.link_conf); link++) {
d8675a63
JB
5787 struct ieee80211_bss_conf *link_conf;
5788
5789 link_conf = rcu_dereference(sdata->vif.link_conf[link]);
5790 if (!link_conf)
5791 continue;
5792 if (memcmp(link_conf->addr, hdr->addr2, ETH_ALEN) == 0)
69d41b5a
JB
5793 break;
5794 }
d8675a63 5795 rcu_read_unlock();
69d41b5a
JB
5796
5797 if (WARN_ON_ONCE(link == ARRAY_SIZE(sdata->vif.link_conf)))
5798 link = ffs(sdata->vif.valid_links) - 1;
5799 }
5800
5801 IEEE80211_SKB_CB(skb)->control.flags |=
5802 u32_encode_bits(link, IEEE80211_TX_CTRL_MLO_LINK);
5803
3d34deb6
JB
5804 /*
5805 * The other path calling ieee80211_xmit is from the tasklet,
5806 * and while we can handle concurrent transmissions locking
5807 * requirements are that we do not come into tx with bhs on.
5808 */
5809 local_bh_disable();
73c4e195 5810 IEEE80211_SKB_CB(skb)->band = band;
08aca29a 5811 ieee80211_xmit(sdata, NULL, skb);
3d34deb6 5812 local_bh_enable();
3b8d81e0 5813}
91180649 5814
eef25a66 5815void ieee80211_tx_skb_tid(struct ieee80211_sub_if_data *sdata,
e1e68b14 5816 struct sk_buff *skb, int tid, int link_id)
eef25a66
JB
5817{
5818 struct ieee80211_chanctx_conf *chanctx_conf;
5819 enum nl80211_band band;
5820
5821 rcu_read_lock();
5822 if (!sdata->vif.valid_links) {
e1e68b14 5823 WARN_ON(link_id >= 0);
eef25a66
JB
5824 chanctx_conf =
5825 rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
5826 if (WARN_ON(!chanctx_conf)) {
5827 rcu_read_unlock();
5828 kfree_skb(skb);
5829 return;
5830 }
5831 band = chanctx_conf->def.chan->band;
5832 } else {
e1e68b14
JB
5833 WARN_ON(link_id >= 0 &&
5834 !(sdata->vif.valid_links & BIT(link_id)));
eef25a66
JB
5835 /* MLD transmissions must not rely on the band */
5836 band = 0;
5837 }
5838
e1e68b14 5839 __ieee80211_tx_skb_tid_band(sdata, skb, tid, link_id, band);
eef25a66
JB
5840 rcu_read_unlock();
5841}
5842
91180649
DK
5843int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
5844 const u8 *buf, size_t len,
dca9ca2d 5845 const u8 *dest, __be16 proto, bool unencrypted,
67207bab 5846 int link_id, u64 *cookie)
91180649
DK
5847{
5848 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5849 struct ieee80211_local *local = sdata->local;
10cb8e61 5850 struct sta_info *sta;
91180649
DK
5851 struct sk_buff *skb;
5852 struct ethhdr *ehdr;
b95d2ccd 5853 u32 ctrl_flags = 0;
a7528198 5854 u32 flags = 0;
dd820ed6 5855 int err;
91180649
DK
5856
5857 /* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
5858 * or Pre-Authentication
5859 */
5860 if (proto != sdata->control_port_protocol &&
5861 proto != cpu_to_be16(ETH_P_PREAUTH))
5862 return -EINVAL;
5863
b95d2ccd 5864 if (proto == sdata->control_port_protocol)
5af7fef3
MT
5865 ctrl_flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO |
5866 IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
b95d2ccd 5867
91180649 5868 if (unencrypted)
a7528198
MT
5869 flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
5870
5871 if (cookie)
5872 ctrl_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
5873
10cb8e61 5874 flags |= IEEE80211_TX_INTFL_NL80211_FRAME_TX;
91180649
DK
5875
5876 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5877 sizeof(struct ethhdr) + len);
5878 if (!skb)
5879 return -ENOMEM;
5880
5881 skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
5882
5883 skb_put_data(skb, buf, len);
5884
5885 ehdr = skb_push(skb, sizeof(struct ethhdr));
5886 memcpy(ehdr->h_dest, dest, ETH_ALEN);
67207bab 5887
9dd19538 5888 /* we may override the SA for MLO STA later */
67207bab 5889 if (link_id < 0) {
9dd19538
JB
5890 ctrl_flags |= u32_encode_bits(IEEE80211_LINK_UNSPECIFIED,
5891 IEEE80211_TX_CTRL_MLO_LINK);
67207bab
AO
5892 memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
5893 } else {
5894 struct ieee80211_bss_conf *link_conf;
5895
9dd19538
JB
5896 ctrl_flags |= u32_encode_bits(link_id,
5897 IEEE80211_TX_CTRL_MLO_LINK);
5898
67207bab
AO
5899 rcu_read_lock();
5900 link_conf = rcu_dereference(sdata->vif.link_conf[link_id]);
5901 if (!link_conf) {
5902 dev_kfree_skb(skb);
5903 rcu_read_unlock();
5904 return -ENOLINK;
5905 }
5906 memcpy(ehdr->h_source, link_conf->addr, ETH_ALEN);
5907 rcu_read_unlock();
5908 }
5909
91180649
DK
5910 ehdr->h_proto = proto;
5911
5912 skb->dev = dev;
10cb8e61 5913 skb->protocol = proto;
91180649
DK
5914 skb_reset_network_header(skb);
5915 skb_reset_mac_header(skb);
5916
10cb8e61
MT
5917 /* update QoS header to prioritize control port frames if possible,
5918 * priorization also happens for control port frames send over
5919 * AF_PACKET
5920 */
5921 rcu_read_lock();
dd820ed6
JB
5922 err = ieee80211_lookup_ra_sta(sdata, skb, &sta);
5923 if (err) {
5924 rcu_read_unlock();
5925 return err;
5926 }
10cb8e61 5927
dd820ed6 5928 if (!IS_ERR(sta)) {
10cb8e61
MT
5929 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
5930
5931 skb_set_queue_mapping(skb, queue);
5932 skb_get_hash(skb);
9dd19538
JB
5933
5934 /*
5935 * for MLO STA, the SA should be the AP MLD address, but
5936 * the link ID has been selected already
5937 */
5938 if (sta->sta.mlo)
5939 memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
10cb8e61 5940 }
10cb8e61
MT
5941 rcu_read_unlock();
5942
a7528198
MT
5943 /* mutex lock is only needed for incrementing the cookie counter */
5944 mutex_lock(&local->mtx);
5945
e7441c92 5946 local_bh_disable();
a7528198 5947 __ieee80211_subif_start_xmit(skb, skb->dev, flags, ctrl_flags, cookie);
e7441c92 5948 local_bh_enable();
91180649 5949
a7528198
MT
5950 mutex_unlock(&local->mtx);
5951
91180649
DK
5952 return 0;
5953}
8828f81a
RM
5954
5955int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
5956 const u8 *buf, size_t len)
5957{
5958 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5959 struct ieee80211_local *local = sdata->local;
5960 struct sk_buff *skb;
5961
5962 skb = dev_alloc_skb(local->hw.extra_tx_headroom + len +
5963 30 + /* header size */
5964 18); /* 11s header size */
5965 if (!skb)
5966 return -ENOMEM;
5967
5968 skb_reserve(skb, local->hw.extra_tx_headroom);
5969 skb_put_data(skb, buf, len);
5970
5971 skb->dev = dev;
5972 skb->protocol = htons(ETH_P_802_3);
5973 skb_reset_network_header(skb);
5974 skb_reset_mac_header(skb);
5975
5976 local_bh_disable();
5977 __ieee80211_subif_start_xmit(skb, skb->dev, 0,
a7528198
MT
5978 IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP,
5979 NULL);
e7441c92 5980 local_bh_enable();
91180649
DK
5981
5982 return 0;
5983}