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