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