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