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