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