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