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