blob: dca01d7e6e3e0bc17cbfc58dc5f662fa0befa290 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Johannes Berge2ebc742007-07-27 15:43:22 +02002/*
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
6 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
Johannes Bergd98ad832014-09-03 15:24:57 +03007 * Copyright 2013-2014 Intel Mobile Communications GmbH
Johannes Bergca98c472020-02-21 10:45:45 +01008 * Copyright (C) 2018-2020 Intel Corporation
Johannes Berge2ebc742007-07-27 15:43:22 +02009 *
Johannes Berge2ebc742007-07-27 15:43:22 +020010 * Transmit and frame generation functions.
11 */
12
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/skbuff.h>
Michael Braunebceec82016-11-22 11:52:18 +010016#include <linux/if_vlan.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020017#include <linux/etherdevice.h>
18#include <linux/bitmap.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040019#include <linux/rcupdate.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040020#include <linux/export.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070021#include <net/net_namespace.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020022#include <net/ieee80211_radiotap.h>
23#include <net/cfg80211.h>
24#include <net/mac80211.h>
Michal Kazior5caa3282016-05-19 10:37:51 +020025#include <net/codel.h>
26#include <net/codel_impl.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020027#include <asm/unaligned.h>
Michal Kaziorfa962b92016-05-19 10:37:49 +020028#include <net/fq_impl.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020029
30#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020031#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040032#include "led.h"
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +010033#include "mesh.h"
Johannes Berge2ebc742007-07-27 15:43:22 +020034#include "wep.h"
35#include "wpa.h"
36#include "wme.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040037#include "rate.h"
Johannes Berge2ebc742007-07-27 15:43:22 +020038
Johannes Berge2ebc742007-07-27 15:43:22 +020039/* misc utils */
40
Johannes Berg5a490512015-04-22 17:10:38 +020041static inline void ieee80211_tx_stats(struct net_device *dev, u32 len)
42{
43 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
44
45 u64_stats_update_begin(&tstats->syncp);
46 tstats->tx_packets++;
47 tstats->tx_bytes += len;
48 u64_stats_update_end(&tstats->syncp);
49}
50
Johannes Berg252b86c2011-11-16 15:28:55 +010051static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
52 struct sk_buff *skb, int group_addr,
Johannes Berg03f93c32008-06-25 14:36:27 +030053 int next_frag_len)
Johannes Berge2ebc742007-07-27 15:43:22 +020054{
Simon Wunderlich2103dec2013-07-08 16:55:53 +020055 int rate, mrate, erp, dur, i, shift = 0;
Johannes Berg2e92e6f2008-05-15 12:55:27 +020056 struct ieee80211_rate *txrate;
Johannes Berge2ebc742007-07-27 15:43:22 +020057 struct ieee80211_local *local = tx->local;
Johannes Berg8318d782008-01-24 19:38:38 +010058 struct ieee80211_supported_band *sband;
Harvey Harrison358c8d92008-07-15 18:44:13 -070059 struct ieee80211_hdr *hdr;
Johannes Berg252b86c2011-11-16 15:28:55 +010060 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Simon Wunderlich2103dec2013-07-08 16:55:53 +020061 struct ieee80211_chanctx_conf *chanctx_conf;
62 u32 rate_flags = 0;
63
Felix Fietkau95cd4702016-12-14 20:46:57 +010064 /* assume HW handles this */
65 if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))
66 return 0;
67
Simon Wunderlich2103dec2013-07-08 16:55:53 +020068 rcu_read_lock();
69 chanctx_conf = rcu_dereference(tx->sdata->vif.chanctx_conf);
70 if (chanctx_conf) {
71 shift = ieee80211_chandef_get_shift(&chanctx_conf->def);
72 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
73 }
74 rcu_read_unlock();
Johannes Berge6a98542008-10-21 12:40:02 +020075
Johannes Berge6a98542008-10-21 12:40:02 +020076 /* uh huh? */
Felix Fietkau0d528d82013-04-22 16:14:41 +020077 if (WARN_ON_ONCE(tx->rate.idx < 0))
Johannes Berge6a98542008-10-21 12:40:02 +020078 return 0;
Johannes Berge2ebc742007-07-27 15:43:22 +020079
Johannes Berg2d565772012-07-23 15:12:51 +020080 sband = local->hw.wiphy->bands[info->band];
Felix Fietkau0d528d82013-04-22 16:14:41 +020081 txrate = &sband->bitrates[tx->rate.idx];
Johannes Berg8318d782008-01-24 19:38:38 +010082
Johannes Berge6a98542008-10-21 12:40:02 +020083 erp = txrate->flags & IEEE80211_RATE_ERP_G;
Johannes Berge2ebc742007-07-27 15:43:22 +020084
85 /*
86 * data and mgmt (except PS Poll):
87 * - during CFP: 32768
88 * - during contention period:
89 * if addr1 is group address: 0
90 * if more fragments = 0 and addr1 is individual address: time to
91 * transmit one ACK plus SIFS
92 * if more fragments = 1 and addr1 is individual address: time to
93 * transmit next fragment plus 2 x ACK plus 3 x SIFS
94 *
95 * IEEE 802.11, 9.6:
96 * - control response frame (CTS or ACK) shall be transmitted using the
97 * same rate as the immediately previous frame in the frame exchange
98 * sequence, if this rate belongs to the PHY mandatory rates, or else
99 * at the highest possible rate belonging to the PHY rates in the
100 * BSSBasicRateSet
101 */
Johannes Berg252b86c2011-11-16 15:28:55 +0100102 hdr = (struct ieee80211_hdr *)skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -0700103 if (ieee80211_is_ctl(hdr->frame_control)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200104 /* TODO: These control frames are not currently sent by
Johannes Bergccd7b362008-09-11 00:01:56 +0200105 * mac80211, but should they be implemented, this function
Johannes Berge2ebc742007-07-27 15:43:22 +0200106 * needs to be updated to support duration field calculation.
107 *
108 * RTS: time needed to transmit pending data/mgmt frame plus
109 * one CTS frame plus one ACK frame plus 3 x SIFS
110 * CTS: duration of immediately previous RTS minus time
111 * required to transmit CTS and its SIFS
112 * ACK: 0 if immediately previous directed data/mgmt had
113 * more=0, with more=1 duration in ACK frame is duration
114 * from previous frame minus time needed to transmit ACK
115 * and its SIFS
116 * PS Poll: BIT(15) | BIT(14) | aid
117 */
118 return 0;
119 }
120
121 /* data/mgmt */
122 if (0 /* FIX: data/mgmt during CFP */)
Johannes Berg03f93c32008-06-25 14:36:27 +0300123 return cpu_to_le16(32768);
Johannes Berge2ebc742007-07-27 15:43:22 +0200124
125 if (group_addr) /* Group address as the destination - no ACK */
126 return 0;
127
128 /* Individual destination address:
129 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
130 * CTS and ACK frames shall be transmitted using the highest rate in
131 * basic rate set that is less than or equal to the rate of the
132 * immediately previous frame and that is using the same modulation
133 * (CCK or OFDM). If no basic rate set matches with these requirements,
134 * the highest mandatory rate of the PHY that is less than or equal to
135 * the rate of the previous frame is used.
136 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
137 */
138 rate = -1;
Johannes Berg8318d782008-01-24 19:38:38 +0100139 /* use lowest available if everything fails */
140 mrate = sband->bitrates[0].bitrate;
141 for (i = 0; i < sband->n_bitrates; i++) {
142 struct ieee80211_rate *r = &sband->bitrates[i];
143
144 if (r->bitrate > txrate->bitrate)
Johannes Berge2ebc742007-07-27 15:43:22 +0200145 break;
146
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200147 if ((rate_flags & r->flags) != rate_flags)
148 continue;
149
Johannes Bergbda39332008-10-11 01:51:51 +0200150 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200151 rate = DIV_ROUND_UP(r->bitrate, 1 << shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200152
Johannes Berg8318d782008-01-24 19:38:38 +0100153 switch (sband->band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200154 case NL80211_BAND_2GHZ: {
Johannes Berg8318d782008-01-24 19:38:38 +0100155 u32 flag;
156 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
157 flag = IEEE80211_RATE_MANDATORY_G;
158 else
159 flag = IEEE80211_RATE_MANDATORY_B;
160 if (r->flags & flag)
161 mrate = r->bitrate;
162 break;
163 }
Johannes Berg57fbcce2016-04-12 15:56:15 +0200164 case NL80211_BAND_5GHZ:
Arend van Sprielc5b9a7f2019-08-02 13:30:58 +0200165 case NL80211_BAND_6GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +0100166 if (r->flags & IEEE80211_RATE_MANDATORY_A)
167 mrate = r->bitrate;
168 break;
Thomas Pedersendf78a0c2020-06-01 23:22:47 -0700169 case NL80211_BAND_S1GHZ:
Johannes Berg57fbcce2016-04-12 15:56:15 +0200170 case NL80211_BAND_60GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300171 /* TODO, for now fall through */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200172 case NUM_NL80211_BANDS:
Johannes Berg8318d782008-01-24 19:38:38 +0100173 WARN_ON(1);
174 break;
175 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200176 }
177 if (rate == -1) {
178 /* No matching basic rate found; use highest suitable mandatory
179 * PHY rate */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200180 rate = DIV_ROUND_UP(mrate, 1 << shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200181 }
182
Simon Wunderlich6674f212011-11-21 21:34:30 +0100183 /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
184 if (ieee80211_is_data_qos(hdr->frame_control) &&
Claudio Pisac26a0e12012-05-28 13:06:25 +0100185 *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
Simon Wunderlich6674f212011-11-21 21:34:30 +0100186 dur = 0;
187 else
188 /* Time needed to transmit ACK
189 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
190 * to closest integer */
Michal Kazior4ee73f32012-04-11 08:47:56 +0200191 dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200192 tx->sdata->vif.bss_conf.use_short_preamble,
193 shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200194
195 if (next_frag_len) {
196 /* Frame is fragmented: duration increases with time needed to
197 * transmit next fragment plus ACK and 2 x SIFS. */
198 dur *= 2; /* ACK + SIFS */
199 /* next fragment */
Michal Kazior4ee73f32012-04-11 08:47:56 +0200200 dur += ieee80211_frame_duration(sband->band, next_frag_len,
Johannes Berg8318d782008-01-24 19:38:38 +0100201 txrate->bitrate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200202 tx->sdata->vif.bss_conf.use_short_preamble,
203 shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200204 }
205
Johannes Berg03f93c32008-06-25 14:36:27 +0300206 return cpu_to_le16(dur);
Johannes Berge2ebc742007-07-27 15:43:22 +0200207}
208
Johannes Berge2ebc742007-07-27 15:43:22 +0200209/* tx handlers */
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200210static ieee80211_tx_result debug_noinline
211ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
212{
213 struct ieee80211_local *local = tx->local;
Kalle Valo0c742112010-01-12 10:42:53 +0200214 struct ieee80211_if_managed *ifmgd;
Andrei Otcheretianski94a5b3a2018-09-05 08:06:14 +0300215 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200216
217 /* driver doesn't support power save */
Johannes Berg30686bf2015-06-02 21:39:54 +0200218 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200219 return TX_CONTINUE;
220
221 /* hardware does dynamic power save */
Johannes Berg30686bf2015-06-02 21:39:54 +0200222 if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200223 return TX_CONTINUE;
224
225 /* dynamic power save disabled */
226 if (local->hw.conf.dynamic_ps_timeout <= 0)
227 return TX_CONTINUE;
228
229 /* we are scanning, don't enable power save */
230 if (local->scanning)
231 return TX_CONTINUE;
232
233 if (!local->ps_sdata)
234 return TX_CONTINUE;
235
236 /* No point if we're going to suspend */
237 if (local->quiescing)
238 return TX_CONTINUE;
239
Kalle Valo0c742112010-01-12 10:42:53 +0200240 /* dynamic ps is supported only in managed mode */
241 if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
242 return TX_CONTINUE;
243
Andrei Otcheretianski94a5b3a2018-09-05 08:06:14 +0300244 if (unlikely(info->flags & IEEE80211_TX_INTFL_OFFCHAN_TX_OK))
245 return TX_CONTINUE;
246
Kalle Valo0c742112010-01-12 10:42:53 +0200247 ifmgd = &tx->sdata->u.mgd;
248
249 /*
250 * Don't wakeup from power save if u-apsd is enabled, voip ac has
251 * u-apsd enabled and the frame is in voip class. This effectively
252 * means that even if all access categories have u-apsd enabled, in
253 * practise u-apsd is only used with the voip ac. This is a
254 * workaround for the case when received voip class packets do not
255 * have correct qos tag for some reason, due the network or the
256 * peer application.
257 *
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200258 * Note: ifmgd->uapsd_queues access is racy here. If the value is
Kalle Valo0c742112010-01-12 10:42:53 +0200259 * changed via debugfs, user needs to reassociate manually to have
260 * everything in sync.
261 */
Johannes Berg4875d30d2012-03-27 14:18:37 +0200262 if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
263 (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
264 skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
Kalle Valo0c742112010-01-12 10:42:53 +0200265 return TX_CONTINUE;
266
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200267 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
268 ieee80211_stop_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +0100269 IEEE80211_MAX_QUEUE_MAP,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300270 IEEE80211_QUEUE_STOP_REASON_PS,
271 false);
Vivek Natarajandb285692011-02-18 17:18:03 +0530272 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200273 ieee80211_queue_work(&local->hw,
274 &local->dynamic_ps_disable_work);
275 }
276
Luciano Coelho5db1c072011-05-03 21:40:08 +0300277 /* Don't restart the timer if we're not disassociated */
278 if (!ifmgd->associated)
279 return TX_CONTINUE;
280
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200281 mod_timer(&local->dynamic_ps_timer, jiffies +
282 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
283
284 return TX_CONTINUE;
285}
Johannes Berge2ebc742007-07-27 15:43:22 +0200286
Johannes Bergd9e8a702008-06-30 15:10:44 +0200287static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100288ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200289{
Harvey Harrison358c8d92008-07-15 18:44:13 -0700290
Johannes Berge039fa42008-05-15 12:55:29 +0200291 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200292 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200293 bool assoc = false;
Johannes Berge2ebc742007-07-27 15:43:22 +0200294
Johannes Berge039fa42008-05-15 12:55:29 +0200295 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100296 return TX_CONTINUE;
Johannes Berg58d41852007-09-26 17:53:18 +0200297
Ben Greearb23b0252011-02-04 11:54:17 -0800298 if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
299 test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
Kalle Valoa9a6fff2009-03-18 14:06:44 +0200300 !ieee80211_is_probe_req(hdr->frame_control) &&
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -0800301 !ieee80211_is_any_nullfunc(hdr->frame_control))
Kalle Valoa9a6fff2009-03-18 14:06:44 +0200302 /*
303 * When software scanning only nullfunc frames (to notify
304 * the sleep state to the AP) and probe requests (for the
305 * active scan) are allowed, all other frames should not be
306 * sent and we should not get here, but if we do
307 * nonetheless, drop them to avoid sending them
308 * off-channel. See the link below and
309 * ieee80211_start_scan() for more.
310 *
311 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
312 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100313 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200314
Rostislav Lisovy239281f2014-11-03 10:33:19 +0100315 if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
316 return TX_CONTINUE;
317
Bill Jordan1be7fe82010-10-01 11:20:41 -0400318 if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
319 return TX_CONTINUE;
320
Johannes Berg5cf121c2008-02-25 16:27:43 +0100321 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100322 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200323
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200324 if (tx->sta)
325 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge2ebc742007-07-27 15:43:22 +0200326
Johannes Berg5cf121c2008-02-25 16:27:43 +0100327 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200328 if (unlikely(!assoc &&
Harvey Harrison358c8d92008-07-15 18:44:13 -0700329 ieee80211_is_data(hdr->frame_control))) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200330#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200331 sdata_info(tx->sdata,
332 "dropped data frame to not associated station %pM\n",
333 hdr->addr1);
334#endif
Johannes Berge2ebc742007-07-27 15:43:22 +0200335 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100336 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200337 }
Michael Braun72f15d52016-10-10 19:12:21 +0200338 } else if (unlikely(ieee80211_is_data(hdr->frame_control) &&
339 ieee80211_vif_get_num_mcast_if(tx->sdata) == 0)) {
Johannes Berg29623892011-12-14 12:20:31 +0100340 /*
341 * No associated STAs - no need to send multicast
342 * frames.
343 */
344 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200345 }
346
Johannes Berg9ae54c82008-01-31 19:48:20 +0100347 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200348}
349
Johannes Berge2ebc742007-07-27 15:43:22 +0200350/* This function is called whenever the AP is about to exceed the maximum limit
351 * of buffered frames for power saving STAs. This situation should not really
352 * happen often during normal operation, so dropping the oldest buffered packet
353 * from each queue should be OK to make some room for new frames. */
354static void purge_old_ps_buffers(struct ieee80211_local *local)
355{
356 int total = 0, purged = 0;
357 struct sk_buff *skb;
358 struct ieee80211_sub_if_data *sdata;
359 struct sta_info *sta;
360
Johannes Berg79010422007-09-18 17:29:21 -0400361 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Marco Porschd012a602012-10-10 12:39:50 -0700362 struct ps_data *ps;
363
364 if (sdata->vif.type == NL80211_IFTYPE_AP)
365 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100366 else if (ieee80211_vif_is_mesh(&sdata->vif))
367 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700368 else
Johannes Berge2ebc742007-07-27 15:43:22 +0200369 continue;
Marco Porschd012a602012-10-10 12:39:50 -0700370
371 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +0200372 if (skb) {
373 purged++;
Felix Fietkau6b07d9c2016-08-02 11:13:41 +0200374 ieee80211_free_txskb(&local->hw, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +0200375 }
Marco Porschd012a602012-10-10 12:39:50 -0700376 total += skb_queue_len(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +0200377 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200378
Johannes Berg948d8872011-09-29 16:04:29 +0200379 /*
380 * Drop one frame from each station from the lowest-priority
381 * AC that has frames at all.
382 */
Johannes Bergd0709a62008-02-25 16:27:46 +0100383 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg948d8872011-09-29 16:04:29 +0200384 int ac;
385
386 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
387 skb = skb_dequeue(&sta->ps_tx_buf[ac]);
388 total += skb_queue_len(&sta->ps_tx_buf[ac]);
389 if (skb) {
390 purged++;
Felix Fietkauc3e77242012-10-08 14:39:33 +0200391 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg948d8872011-09-29 16:04:29 +0200392 break;
393 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200394 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200395 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100396
Johannes Berge2ebc742007-07-27 15:43:22 +0200397 local->total_ps_buffered = total;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200398 ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
Johannes Berge2ebc742007-07-27 15:43:22 +0200399}
400
Johannes Berg9ae54c82008-01-31 19:48:20 +0100401static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100402ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200403{
Johannes Berge039fa42008-05-15 12:55:29 +0200404 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700405 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Marco Porschd012a602012-10-10 12:39:50 -0700406 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +0200407
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100408 /*
409 * broadcast/multicast frame
410 *
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100411 * If any of the associated/peer stations is in power save mode,
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100412 * the frame is buffered to be sent after DTIM beacon frame.
413 * This is done either by the hardware or us.
414 */
415
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100416 /* powersaving STAs currently only in AP/VLAN/mesh mode */
Marco Porschd012a602012-10-10 12:39:50 -0700417 if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
418 tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
419 if (!tx->sdata->bss)
420 return TX_CONTINUE;
421
422 ps = &tx->sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100423 } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
424 ps = &tx->sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700425 } else {
Johannes Berg3e122be2008-07-09 14:40:34 +0200426 return TX_CONTINUE;
Marco Porschd012a602012-10-10 12:39:50 -0700427 }
428
Johannes Berg3e122be2008-07-09 14:40:34 +0200429
430 /* no buffering for ordered frames */
Harvey Harrison358c8d92008-07-15 18:44:13 -0700431 if (ieee80211_has_order(hdr->frame_control))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100432 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100433
Johannes Berg08b99392014-07-07 12:01:11 +0200434 if (ieee80211_is_probe_req(hdr->frame_control))
435 return TX_CONTINUE;
436
Johannes Berg30686bf2015-06-02 21:39:54 +0200437 if (ieee80211_hw_check(&tx->local->hw, QUEUE_CONTROL))
Johannes Bergf4d57942013-05-28 17:24:15 +0200438 info->hw_queue = tx->sdata->vif.cab_queue;
439
Felix Fietkau9ec11902018-11-28 22:39:16 +0100440 /* no stations in PS mode and no buffered packets */
441 if (!atomic_read(&ps->num_sta_ps) && skb_queue_empty(&ps->bc_buf))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100442 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100443
Johannes Berg62b517c2009-10-29 12:19:21 +0100444 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
Johannes Berg62b12082009-08-10 16:04:15 +0200445
Johannes Berg62b517c2009-10-29 12:19:21 +0100446 /* device releases frame after DTIM beacon */
Johannes Berg30686bf2015-06-02 21:39:54 +0200447 if (!ieee80211_hw_check(&tx->local->hw, HOST_BROADCAST_PS_BUFFERING))
Johannes Berg62b12082009-08-10 16:04:15 +0200448 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200449
Johannes Berg62b12082009-08-10 16:04:15 +0200450 /* buffered in mac80211 */
451 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
452 purge_old_ps_buffers(tx->local);
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100453
Marco Porschd012a602012-10-10 12:39:50 -0700454 if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200455 ps_dbg(tx->sdata,
456 "BC TX buffer full - dropping the oldest frame\n");
Felix Fietkau6b07d9c2016-08-02 11:13:41 +0200457 ieee80211_free_txskb(&tx->local->hw, skb_dequeue(&ps->bc_buf));
Johannes Berg62b12082009-08-10 16:04:15 +0200458 } else
459 tx->local->total_ps_buffered++;
460
Marco Porschd012a602012-10-10 12:39:50 -0700461 skb_queue_tail(&ps->bc_buf, tx->skb);
Johannes Berg62b12082009-08-10 16:04:15 +0200462
463 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200464}
465
Jouni Malinenfb733332009-01-08 13:32:00 +0200466static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
467 struct sk_buff *skb)
468{
469 if (!ieee80211_is_mgmt(fc))
470 return 0;
471
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200472 if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
Jouni Malinenfb733332009-01-08 13:32:00 +0200473 return 0;
474
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100475 if (!ieee80211_is_robust_mgmt_frame(skb))
Jouni Malinenfb733332009-01-08 13:32:00 +0200476 return 0;
477
478 return 1;
479}
480
Johannes Berg9ae54c82008-01-31 19:48:20 +0100481static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100482ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200483{
484 struct sta_info *sta = tx->sta;
Johannes Berge039fa42008-05-15 12:55:29 +0200485 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berg08b99392014-07-07 12:01:11 +0200486 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300487 struct ieee80211_local *local = tx->local;
Johannes Berge2ebc742007-07-27 15:43:22 +0200488
Johannes Berg02f2f1a2012-02-27 12:18:30 +0100489 if (unlikely(!sta))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100490 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200491
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200492 if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
Johannes Berg5ac2e352014-05-27 16:32:27 +0200493 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
494 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
Johannes Berg02f2f1a2012-02-27 12:18:30 +0100495 !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
Johannes Berg948d8872011-09-29 16:04:29 +0200496 int ac = skb_get_queue_mapping(tx->skb);
497
Johannes Berg08b99392014-07-07 12:01:11 +0200498 if (ieee80211_is_mgmt(hdr->frame_control) &&
499 !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
500 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
501 return TX_CONTINUE;
502 }
503
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200504 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
505 sta->sta.addr, sta->sta.aid, ac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200506 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
507 purge_old_ps_buffers(tx->local);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200508
509 /* sync with ieee80211_sta_ps_deliver_wakeup */
510 spin_lock(&sta->ps_lock);
511 /*
512 * STA woke up the meantime and all the frames on ps_tx_buf have
513 * been queued to pending queue. No reordering can happen, go
514 * ahead and Tx the packet.
515 */
516 if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
Johannes Berg5ac2e352014-05-27 16:32:27 +0200517 !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
518 !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200519 spin_unlock(&sta->ps_lock);
520 return TX_CONTINUE;
521 }
522
Johannes Berg948d8872011-09-29 16:04:29 +0200523 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
524 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200525 ps_dbg(tx->sdata,
526 "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
527 sta->sta.addr, ac);
Felix Fietkauc3e77242012-10-08 14:39:33 +0200528 ieee80211_free_txskb(&local->hw, old);
Johannes Berge2ebc742007-07-27 15:43:22 +0200529 } else
530 tx->local->total_ps_buffered++;
Johannes Berg004c8722008-02-20 11:21:35 +0100531
Johannes Berge039fa42008-05-15 12:55:29 +0200532 info->control.jiffies = jiffies;
Johannes Berg5061b0c2009-07-14 00:33:34 +0200533 info->control.vif = &tx->sdata->vif;
Johannes Berg8f77f382009-06-07 21:58:37 +0200534 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Johannes Berg03c8c062014-01-09 01:45:28 +0100535 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berg948d8872011-09-29 16:04:29 +0200536 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200537 spin_unlock(&sta->ps_lock);
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300538
539 if (!timer_pending(&local->sta_cleanup))
540 mod_timer(&local->sta_cleanup,
541 round_jiffies(jiffies +
542 STA_INFO_CLEANUP_INTERVAL));
543
Johannes Bergc868cb352011-09-29 16:04:27 +0200544 /*
545 * We queued up some frames, so the TIM bit might
546 * need to be set, recalculate it.
547 */
548 sta_info_recalc_tim(sta);
549
Johannes Berg9ae54c82008-01-31 19:48:20 +0100550 return TX_QUEUED;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200551 } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
552 ps_dbg(tx->sdata,
553 "STA %pM in PS mode, but polling/in SP -> send frame\n",
554 sta->sta.addr);
Johannes Berge2ebc742007-07-27 15:43:22 +0200555 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200556
Johannes Berg9ae54c82008-01-31 19:48:20 +0100557 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200558}
559
Johannes Bergd9e8a702008-06-30 15:10:44 +0200560static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100561ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200562{
Johannes Berg5cf121c2008-02-25 16:27:43 +0100563 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100564 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200565
Johannes Berg5cf121c2008-02-25 16:27:43 +0100566 if (tx->flags & IEEE80211_TX_UNICAST)
Johannes Berge2ebc742007-07-27 15:43:22 +0200567 return ieee80211_tx_h_unicast_ps_buf(tx);
568 else
569 return ieee80211_tx_h_multicast_ps_buf(tx);
570}
571
Johannes Bergd9e8a702008-06-30 15:10:44 +0200572static ieee80211_tx_result debug_noinline
Johannes Berga621fa42010-08-27 14:26:54 +0300573ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
574{
575 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
576
Johannes Bergaf61a162013-07-02 18:09:12 +0200577 if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
578 if (tx->sdata->control_port_no_encrypt)
579 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
580 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
Jouni Malinen9c1c98a2015-02-26 15:50:50 +0200581 info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
Johannes Bergaf61a162013-07-02 18:09:12 +0200582 }
Johannes Berga621fa42010-08-27 14:26:54 +0300583
584 return TX_CONTINUE;
585}
586
587static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100588ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200589{
Johannes Berg46e6de12012-07-04 18:10:07 +0200590 struct ieee80211_key *key;
Johannes Berge039fa42008-05-15 12:55:29 +0200591 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700592 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400593
Johannes Berga0761a32020-03-26 15:09:42 +0200594 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200595 tx->key = NULL;
Johannes Berga0761a32020-03-26 15:09:42 +0200596 return TX_CONTINUE;
597 }
598
599 if (tx->sta &&
600 (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
Johannes Bergd4e46a32007-09-14 11:10:24 -0400601 tx->key = key;
Masashi Honma46f6b062016-06-22 19:55:20 +0900602 else if (ieee80211_is_group_privacy_action(tx->skb) &&
603 (key = rcu_dereference(tx->sdata->default_multicast_key)))
604 tx->key = key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200605 else if (ieee80211_is_mgmt(hdr->frame_control) &&
Jouni Malinenecbcd322010-03-29 23:35:23 -0700606 is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100607 ieee80211_is_robust_mgmt_frame(tx->skb) &&
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200608 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
609 tx->key = key;
Johannes Bergf7e01042010-12-09 19:49:02 +0100610 else if (is_multicast_ether_addr(hdr->addr1) &&
611 (key = rcu_dereference(tx->sdata->default_multicast_key)))
612 tx->key = key;
613 else if (!is_multicast_ether_addr(hdr->addr1) &&
614 (key = rcu_dereference(tx->sdata->default_unicast_key)))
Johannes Bergd4e46a32007-09-14 11:10:24 -0400615 tx->key = key;
Johannes Berge8f4fb72015-03-20 11:37:36 +0100616 else
Johannes Berg46e6de12012-07-04 18:10:07 +0200617 tx->key = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +0200618
619 if (tx->key) {
Johannes Berg813d7662010-01-17 01:47:58 +0100620 bool skip_hw = false;
621
Johannes Berg011bfcc2007-09-17 01:29:25 -0400622 /* TODO: add threshold stuff again */
Johannes Berg176e4f82007-12-18 15:27:47 +0100623
Johannes Berg97359d12010-08-10 09:46:38 +0200624 switch (tx->key->conf.cipher) {
625 case WLAN_CIPHER_SUITE_WEP40:
626 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg97359d12010-08-10 09:46:38 +0200627 case WLAN_CIPHER_SUITE_TKIP:
Harvey Harrison358c8d92008-07-15 18:44:13 -0700628 if (!ieee80211_is_data_present(hdr->frame_control))
Johannes Berg176e4f82007-12-18 15:27:47 +0100629 tx->key = NULL;
630 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200631 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200632 case WLAN_CIPHER_SUITE_CCMP_256:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200633 case WLAN_CIPHER_SUITE_GCMP:
634 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinenfb733332009-01-08 13:32:00 +0200635 if (!ieee80211_is_data_present(hdr->frame_control) &&
636 !ieee80211_use_mfp(hdr->frame_control, tx->sta,
Masashi Honma46f6b062016-06-22 19:55:20 +0900637 tx->skb) &&
638 !ieee80211_is_group_privacy_action(tx->skb))
Jouni Malinenfb733332009-01-08 13:32:00 +0200639 tx->key = NULL;
Kalle Valo3b43a182010-01-23 20:27:14 +0200640 else
641 skip_hw = (tx->key->conf.flags &
Johannes Berge548c492012-09-04 17:08:23 +0200642 IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
Kalle Valo3b43a182010-01-23 20:27:14 +0200643 ieee80211_is_mgmt(hdr->frame_control);
Jouni Malinenfb733332009-01-08 13:32:00 +0200644 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200645 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200646 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Jouni Malinen8ade5382015-01-24 19:52:09 +0200647 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
648 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200649 if (!ieee80211_is_mgmt(hdr->frame_control))
650 tx->key = NULL;
651 break;
Johannes Berg176e4f82007-12-18 15:27:47 +0100652 }
Johannes Berg813d7662010-01-17 01:47:58 +0100653
Johannes Berge54faf22013-01-29 11:41:38 +0100654 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
655 !ieee80211_is_deauth(hdr->frame_control)))
Johannes Berg95acac62011-07-12 12:30:59 +0200656 return TX_DROP;
657
Johannes Bergf12553e2010-01-22 22:07:59 +0100658 if (!skip_hw && tx->key &&
Johannes Berg382b1652010-01-25 11:36:16 +0100659 tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
Johannes Berg813d7662010-01-17 01:47:58 +0100660 info->control.hw_key = &tx->key->conf;
Johannes Berga0761a32020-03-26 15:09:42 +0200661 } else if (!ieee80211_is_mgmt(hdr->frame_control) && tx->sta &&
662 test_sta_flag(tx->sta, WLAN_STA_USES_ENCRYPTION)) {
663 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200664 }
665
Johannes Berg9ae54c82008-01-31 19:48:20 +0100666 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200667}
668
Johannes Bergd9e8a702008-06-30 15:10:44 +0200669static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100670ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200671{
Johannes Berge039fa42008-05-15 12:55:29 +0200672 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200673 struct ieee80211_hdr *hdr = (void *)tx->skb->data;
674 struct ieee80211_supported_band *sband;
Shanyu Zhaoa2c40242010-04-27 11:15:12 -0700675 u32 len;
Johannes Berge6a98542008-10-21 12:40:02 +0200676 struct ieee80211_tx_rate_control txrc;
Felix Fietkau0d528d82013-04-22 16:14:41 +0200677 struct ieee80211_sta_rates *ratetbl = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200678 bool assoc = false;
Johannes Berge6a98542008-10-21 12:40:02 +0200679
680 memset(&txrc, 0, sizeof(txrc));
Johannes Berg8318d782008-01-24 19:38:38 +0100681
Johannes Berg2d565772012-07-23 15:12:51 +0200682 sband = tx->local->hw.wiphy->bands[info->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200683
Shanyu Zhaoa2c40242010-04-27 11:15:12 -0700684 len = min_t(u32, tx->skb->len + FCS_LEN,
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200685 tx->local->hw.wiphy->frag_threshold);
Johannes Berg58d41852007-09-26 17:53:18 +0200686
Johannes Berge6a98542008-10-21 12:40:02 +0200687 /* set up the tx rate control struct we give the RC algo */
Johannes Berg005e4722012-02-26 11:24:35 +0100688 txrc.hw = &tx->local->hw;
Johannes Berge6a98542008-10-21 12:40:02 +0200689 txrc.sband = sband;
690 txrc.bss_conf = &tx->sdata->vif.bss_conf;
691 txrc.skb = tx->skb;
692 txrc.reported_rate.idx = -1;
Johannes Berg2d565772012-07-23 15:12:51 +0200693 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
Felix Fietkau2ffbe6d2013-04-16 13:38:42 +0200694
695 if (tx->sdata->rc_has_mcs_mask[info->band])
696 txrc.rate_idx_mcs_mask =
697 tx->sdata->rc_rateidx_mcs_mask[info->band];
698
Felix Fietkau8f0729b2010-11-11 15:07:23 +0100699 txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -0800700 tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
Bertold Van den Bergh5765f9f2015-08-05 16:02:28 +0200701 tx->sdata->vif.type == NL80211_IFTYPE_ADHOC ||
702 tx->sdata->vif.type == NL80211_IFTYPE_OCB);
Johannes Berg58d41852007-09-26 17:53:18 +0200703
Johannes Berge6a98542008-10-21 12:40:02 +0200704 /* set up RTS protection if desired */
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200705 if (len > tx->local->hw.wiphy->rts_threshold) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200706 txrc.rts = true;
Johannes Berge2ebc742007-07-27 15:43:22 +0200707 }
Johannes Berge6a98542008-10-21 12:40:02 +0200708
Felix Fietkau0d528d82013-04-22 16:14:41 +0200709 info->control.use_rts = txrc.rts;
Felix Fietkau991fec02013-04-16 13:38:43 +0200710 info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
711
Johannes Berge6a98542008-10-21 12:40:02 +0200712 /*
713 * Use short preamble if the BSS can handle it, but not for
714 * management frames unless we know the receiver can handle
715 * that -- the management frame might be to a station that
716 * just wants a probe response.
717 */
718 if (tx->sdata->vif.bss_conf.use_short_preamble &&
719 (ieee80211_is_data(hdr->frame_control) ||
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200720 (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
Felix Fietkau0d528d82013-04-22 16:14:41 +0200721 txrc.short_preamble = true;
722
723 info->control.short_preamble = txrc.short_preamble;
Johannes Berge6a98542008-10-21 12:40:02 +0200724
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +0100725 /* don't ask rate control when rate already injected via radiotap */
726 if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)
727 return TX_CONTINUE;
728
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200729 if (tx->sta)
730 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge6a98542008-10-21 12:40:02 +0200731
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700732 /*
733 * Lets not bother rate control if we're associated and cannot
734 * talk to the sta. This should not happen.
735 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200736 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700737 !rate_usable_index_exists(sband, &tx->sta->sta),
738 "%s: Dropped data frame as no usable bitrate found while "
739 "scanning and associated. Target station: "
740 "%pM on %d GHz band\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100741 tx->sdata->name, hdr->addr1,
Johannes Berg2d565772012-07-23 15:12:51 +0200742 info->band ? 5 : 2))
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700743 return TX_DROP;
744
745 /*
746 * If we're associated with the sta at this point we know we can at
747 * least send the frame at the lowest bit rate.
748 */
Johannes Berge6a98542008-10-21 12:40:02 +0200749 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
750
Felix Fietkau0d528d82013-04-22 16:14:41 +0200751 if (tx->sta && !info->control.skip_table)
752 ratetbl = rcu_dereference(tx->sta->sta.rates);
753
754 if (unlikely(info->control.rates[0].idx < 0)) {
755 if (ratetbl) {
756 struct ieee80211_tx_rate rate = {
757 .idx = ratetbl->rate[0].idx,
758 .flags = ratetbl->rate[0].flags,
759 .count = ratetbl->rate[0].count
760 };
761
762 if (ratetbl->rate[0].idx < 0)
763 return TX_DROP;
764
765 tx->rate = rate;
766 } else {
767 return TX_DROP;
768 }
769 } else {
770 tx->rate = info->control.rates[0];
771 }
Johannes Berge6a98542008-10-21 12:40:02 +0200772
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100773 if (txrc.reported_rate.idx < 0) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200774 txrc.reported_rate = tx->rate;
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100775 if (tx->sta && ieee80211_is_data(hdr->frame_control))
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200776 tx->sta->tx_stats.last_rate = txrc.reported_rate;
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100777 } else if (tx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200778 tx->sta->tx_stats.last_rate = txrc.reported_rate;
Johannes Berge6a98542008-10-21 12:40:02 +0200779
Felix Fietkau0d528d82013-04-22 16:14:41 +0200780 if (ratetbl)
781 return TX_CONTINUE;
782
Johannes Berge6a98542008-10-21 12:40:02 +0200783 if (unlikely(!info->control.rates[0].count))
784 info->control.rates[0].count = 1;
785
Gábor Stefanik99551512009-04-23 19:36:14 +0200786 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
787 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
788 info->control.rates[0].count = 1;
789
Johannes Berg9ae54c82008-01-31 19:48:20 +0100790 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200791}
792
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100793static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
794{
795 u16 *seq = &sta->tid_seq[tid];
796 __le16 ret = cpu_to_le16(*seq);
797
798 /* Increase the sequence number. */
799 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
800
801 return ret;
802}
803
Johannes Bergd9e8a702008-06-30 15:10:44 +0200804static ieee80211_tx_result debug_noinline
Johannes Bergf591fa52008-07-10 11:21:26 +0200805ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
806{
807 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
808 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergf591fa52008-07-10 11:21:26 +0200809 int tid;
810
Johannes Berg25d834e2008-09-12 22:52:47 +0200811 /*
812 * Packet injection may want to control the sequence
813 * number, if we have no matching interface then we
814 * neither assign one ourselves nor ask the driver to.
815 */
Johannes Berg5061b0c2009-07-14 00:33:34 +0200816 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
Johannes Berg25d834e2008-09-12 22:52:47 +0200817 return TX_CONTINUE;
818
Johannes Bergf591fa52008-07-10 11:21:26 +0200819 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
820 return TX_CONTINUE;
821
822 if (ieee80211_hdrlen(hdr->frame_control) < 24)
823 return TX_CONTINUE;
824
Johannes Berg49a59542011-09-29 16:04:41 +0200825 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
826 return TX_CONTINUE;
827
Mathy Vanhoef29c3e952020-07-23 14:01:50 +0400828 if (info->control.flags & IEEE80211_TX_CTRL_NO_SEQNO)
829 return TX_CONTINUE;
830
Johannes Berg94778282008-10-10 13:21:59 +0200831 /*
832 * Anything but QoS data that has a sequence number field
833 * (is long enough) gets a sequence number from the global
Bob Copelandc4c205f2013-08-23 09:35:38 -0400834 * counter. QoS data frames with a multicast destination
835 * also use the global counter (802.11-2012 9.3.2.10).
Johannes Berg94778282008-10-10 13:21:59 +0200836 */
Bob Copelandc4c205f2013-08-23 09:35:38 -0400837 if (!ieee80211_is_data_qos(hdr->frame_control) ||
838 is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg94778282008-10-10 13:21:59 +0200839 /* driver should assign sequence number */
Johannes Bergf591fa52008-07-10 11:21:26 +0200840 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
Johannes Berg94778282008-10-10 13:21:59 +0200841 /* for pure STA mode without beacons, we can do it */
842 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
843 tx->sdata->sequence_number += 0x10;
Johannes Berg79c892b2014-11-21 14:26:31 +0100844 if (tx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200845 tx->sta->tx_stats.msdu[IEEE80211_NUM_TIDS]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200846 return TX_CONTINUE;
847 }
848
849 /*
850 * This should be true for injected/management frames only, for
851 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
852 * above since they are not QoS-data frames.
853 */
854 if (!tx->sta)
855 return TX_CONTINUE;
856
857 /* include per-STA, per-TID sequence counter */
Sara Sharona1f2ba04c2018-02-19 14:48:40 +0200858 tid = ieee80211_get_tid(hdr);
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200859 tx->sta->tx_stats.msdu[tid]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200860
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +0200861 hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
Johannes Bergf591fa52008-07-10 11:21:26 +0200862
863 return TX_CONTINUE;
864}
865
Johannes Berg252b86c2011-11-16 15:28:55 +0100866static int ieee80211_fragment(struct ieee80211_tx_data *tx,
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100867 struct sk_buff *skb, int hdrlen,
868 int frag_threshold)
869{
Johannes Berg252b86c2011-11-16 15:28:55 +0100870 struct ieee80211_local *local = tx->local;
Johannes Berga1a3fce2011-11-16 15:28:56 +0100871 struct ieee80211_tx_info *info;
Johannes Berg252b86c2011-11-16 15:28:55 +0100872 struct sk_buff *tmp;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100873 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
874 int pos = hdrlen + per_fragm;
875 int rem = skb->len - hdrlen - per_fragm;
876
877 if (WARN_ON(rem < 0))
878 return -EINVAL;
879
Johannes Berg252b86c2011-11-16 15:28:55 +0100880 /* first fragment was already added to queue by caller */
881
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100882 while (rem) {
883 int fraglen = per_fragm;
884
885 if (fraglen > rem)
886 fraglen = rem;
887 rem -= fraglen;
888 tmp = dev_alloc_skb(local->tx_headroom +
889 frag_threshold +
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200890 tx->sdata->encrypt_headroom +
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100891 IEEE80211_ENCRYPT_TAILROOM);
892 if (!tmp)
893 return -ENOMEM;
Johannes Berg252b86c2011-11-16 15:28:55 +0100894
895 __skb_queue_tail(&tx->skbs, tmp);
896
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200897 skb_reserve(tmp,
898 local->tx_headroom + tx->sdata->encrypt_headroom);
899
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100900 /* copy control information */
901 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
Johannes Berga1a3fce2011-11-16 15:28:56 +0100902
903 info = IEEE80211_SKB_CB(tmp);
904 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
905 IEEE80211_TX_CTL_FIRST_FRAGMENT);
906
907 if (rem)
908 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
909
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100910 skb_copy_queue_mapping(tmp, skb);
911 tmp->priority = skb->priority;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100912 tmp->dev = skb->dev;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100913
914 /* copy header and data */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200915 skb_put_data(tmp, skb->data, hdrlen);
916 skb_put_data(tmp, skb->data + pos, fraglen);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100917
918 pos += fraglen;
919 }
920
Johannes Berg252b86c2011-11-16 15:28:55 +0100921 /* adjust first fragment's length */
Johannes Berg338f9772014-02-01 00:16:23 +0100922 skb_trim(skb, hdrlen + per_fragm);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100923 return 0;
924}
925
Johannes Bergf591fa52008-07-10 11:21:26 +0200926static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +0200927ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
928{
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100929 struct sk_buff *skb = tx->skb;
930 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
931 struct ieee80211_hdr *hdr = (void *)skb->data;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200932 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100933 int hdrlen;
934 int fragnum;
Johannes Berge2454942008-05-15 12:55:28 +0200935
Johannes Berg252b86c2011-11-16 15:28:55 +0100936 /* no matter what happens, tx->skb moves to tx->skbs */
937 __skb_queue_tail(&tx->skbs, skb);
938 tx->skb = NULL;
939
Johannes Berga26eb272011-10-07 14:01:25 +0200940 if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
941 return TX_CONTINUE;
942
Sara Sharonf3fe4e92016-10-18 23:12:11 +0300943 if (ieee80211_hw_check(&tx->local->hw, SUPPORTS_TX_FRAG))
Johannes Berge2454942008-05-15 12:55:28 +0200944 return TX_CONTINUE;
945
Johannes Bergeefce912008-05-17 00:57:13 +0200946 /*
947 * Warn when submitting a fragmented A-MPDU frame and drop it.
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200948 * This scenario is handled in ieee80211_tx_prepare but extra
Ron Rindjunsky8d5e0d52008-06-12 15:42:29 +0300949 * caution taken here as fragmented ampdu may cause Tx stop.
Johannes Bergeefce912008-05-17 00:57:13 +0200950 */
Sujith8b30b1f2008-10-24 09:55:27 +0530951 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
Johannes Bergeefce912008-05-17 00:57:13 +0200952 return TX_DROP;
953
Harvey Harrison065e96052008-06-22 16:45:27 -0700954 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berge2454942008-05-15 12:55:28 +0200955
Johannes Berga26eb272011-10-07 14:01:25 +0200956 /* internal error, why isn't DONTFRAG set? */
Johannes Berg8ccd8f22009-04-29 23:35:56 +0200957 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100958 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200959
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100960 /*
961 * Now fragment the frame. This will allocate all the fragments and
962 * chain them (using skb as the first fragment) to skb->next.
963 * During transmission, we will remove the successfully transmitted
964 * fragments from this list. When the low-level driver rejects one
965 * of the fragments then we will simply pretend to accept the skb
966 * but store it away as pending.
967 */
Johannes Berg252b86c2011-11-16 15:28:55 +0100968 if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100969 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200970
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100971 /* update duration/seq/flags of fragments */
972 fragnum = 0;
Johannes Berg252b86c2011-11-16 15:28:55 +0100973
974 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100975 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
Johannes Berge2454942008-05-15 12:55:28 +0200976
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100977 hdr = (void *)skb->data;
978 info = IEEE80211_SKB_CB(skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200979
Johannes Berg252b86c2011-11-16 15:28:55 +0100980 if (!skb_queue_is_last(&tx->skbs, skb)) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100981 hdr->frame_control |= morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200982 /*
983 * No multi-rate retries for fragmented frames, that
984 * would completely throw off the NAV at other STAs.
985 */
986 info->control.rates[1].idx = -1;
987 info->control.rates[2].idx = -1;
988 info->control.rates[3].idx = -1;
Thomas Huehne3e1a0b2012-07-02 19:46:16 +0200989 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
Johannes Berge6a98542008-10-21 12:40:02 +0200990 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100991 } else {
992 hdr->frame_control &= ~morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200993 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100994 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
995 fragnum++;
Johannes Berg252b86c2011-11-16 15:28:55 +0100996 }
Johannes Berge2454942008-05-15 12:55:28 +0200997
998 return TX_CONTINUE;
Johannes Berge2454942008-05-15 12:55:28 +0200999}
1000
Johannes Bergd9e8a702008-06-30 15:10:44 +02001001static ieee80211_tx_result debug_noinline
Johannes Bergfeff1f22009-08-10 16:01:54 +02001002ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
1003{
Johannes Berg252b86c2011-11-16 15:28:55 +01001004 struct sk_buff *skb;
Johannes Berg560d2682013-02-05 10:55:21 +01001005 int ac = -1;
Johannes Bergfeff1f22009-08-10 16:01:54 +02001006
1007 if (!tx->sta)
1008 return TX_CONTINUE;
1009
Johannes Berg252b86c2011-11-16 15:28:55 +01001010 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg560d2682013-02-05 10:55:21 +01001011 ac = skb_get_queue_mapping(skb);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001012 tx->sta->tx_stats.bytes[ac] += skb->len;
Johannes Berg252b86c2011-11-16 15:28:55 +01001013 }
Johannes Berg560d2682013-02-05 10:55:21 +01001014 if (ac >= 0)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001015 tx->sta->tx_stats.packets[ac]++;
Johannes Bergfeff1f22009-08-10 16:01:54 +02001016
1017 return TX_CONTINUE;
1018}
1019
1020static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +02001021ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
1022{
1023 if (!tx->key)
1024 return TX_CONTINUE;
1025
Johannes Berg97359d12010-08-10 09:46:38 +02001026 switch (tx->key->conf.cipher) {
1027 case WLAN_CIPHER_SUITE_WEP40:
1028 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge2454942008-05-15 12:55:28 +02001029 return ieee80211_crypto_wep_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001030 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berge2454942008-05-15 12:55:28 +02001031 return ieee80211_crypto_tkip_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001032 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001033 return ieee80211_crypto_ccmp_encrypt(
1034 tx, IEEE80211_CCMP_MIC_LEN);
1035 case WLAN_CIPHER_SUITE_CCMP_256:
1036 return ieee80211_crypto_ccmp_encrypt(
1037 tx, IEEE80211_CCMP_256_MIC_LEN);
Johannes Berg97359d12010-08-10 09:46:38 +02001038 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001039 return ieee80211_crypto_aes_cmac_encrypt(tx);
Jouni Malinen56c52da2015-01-24 19:52:08 +02001040 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1041 return ieee80211_crypto_aes_cmac_256_encrypt(tx);
Jouni Malinen8ade5382015-01-24 19:52:09 +02001042 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1043 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1044 return ieee80211_crypto_aes_gmac_encrypt(tx);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001045 case WLAN_CIPHER_SUITE_GCMP:
1046 case WLAN_CIPHER_SUITE_GCMP_256:
1047 return ieee80211_crypto_gcmp_encrypt(tx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +03001048 default:
Yoni Divinskyd32a1022012-01-16 15:18:59 +02001049 return ieee80211_crypto_hw_encrypt(tx);
Johannes Berge2454942008-05-15 12:55:28 +02001050 }
1051
Johannes Berge2454942008-05-15 12:55:28 +02001052 return TX_DROP;
1053}
1054
Johannes Bergd9e8a702008-06-30 15:10:44 +02001055static ieee80211_tx_result debug_noinline
Johannes Berg03f93c32008-06-25 14:36:27 +03001056ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1057{
Johannes Berg252b86c2011-11-16 15:28:55 +01001058 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001059 struct ieee80211_hdr *hdr;
1060 int next_len;
1061 bool group_addr;
Johannes Berg03f93c32008-06-25 14:36:27 +03001062
Johannes Berg252b86c2011-11-16 15:28:55 +01001063 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001064 hdr = (void *) skb->data;
Jouni Malinen7e0aae42009-05-19 19:25:58 +03001065 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1066 break; /* must not overwrite AID */
Johannes Berg252b86c2011-11-16 15:28:55 +01001067 if (!skb_queue_is_last(&tx->skbs, skb)) {
1068 struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1069 next_len = next->len;
1070 } else
1071 next_len = 0;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001072 group_addr = is_multicast_ether_addr(hdr->addr1);
Johannes Berg03f93c32008-06-25 14:36:27 +03001073
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001074 hdr->duration_id =
Johannes Berg252b86c2011-11-16 15:28:55 +01001075 ieee80211_duration(tx, skb, group_addr, next_len);
1076 }
Johannes Berg03f93c32008-06-25 14:36:27 +03001077
1078 return TX_CONTINUE;
1079}
1080
Johannes Berge2ebc742007-07-27 15:43:22 +02001081/* actual transmit path */
1082
Johannes Berga622ab72010-06-10 10:21:39 +02001083static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1084 struct sk_buff *skb,
1085 struct ieee80211_tx_info *info,
1086 struct tid_ampdu_tx *tid_tx,
1087 int tid)
1088{
1089 bool queued = false;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001090 bool reset_agg_timer = false;
Helmut Schaaaa454582012-03-07 17:20:30 +01001091 struct sk_buff *purge_skb = NULL;
Johannes Berga622ab72010-06-10 10:21:39 +02001092
1093 if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1094 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001095 reset_agg_timer = true;
Johannes Berg0ab33702010-06-10 10:21:42 +02001096 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1097 /*
1098 * nothing -- this aggregation session is being started
1099 * but that might still fail with the driver
1100 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001101 } else if (!tx->sta->sta.txq[tid]) {
Johannes Berga622ab72010-06-10 10:21:39 +02001102 spin_lock(&tx->sta->lock);
1103 /*
1104 * Need to re-check now, because we may get here
1105 *
1106 * 1) in the window during which the setup is actually
1107 * already done, but not marked yet because not all
1108 * packets are spliced over to the driver pending
1109 * queue yet -- if this happened we acquire the lock
1110 * either before or after the splice happens, but
1111 * need to recheck which of these cases happened.
1112 *
1113 * 2) during session teardown, if the OPERATIONAL bit
1114 * was cleared due to the teardown but the pointer
1115 * hasn't been assigned NULL yet (or we loaded it
1116 * before it was assigned) -- in this case it may
1117 * now be NULL which means we should just let the
1118 * packet pass through because splicing the frames
1119 * back is already done.
1120 */
Johannes Berg40b275b2011-05-13 14:15:49 +02001121 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
Johannes Berga622ab72010-06-10 10:21:39 +02001122
1123 if (!tid_tx) {
1124 /* do nothing, let packet pass through */
1125 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1126 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001127 reset_agg_timer = true;
Johannes Berga622ab72010-06-10 10:21:39 +02001128 } else {
1129 queued = true;
Emmanuel Grumbachf6d46712016-03-17 16:51:42 +02001130 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
1131 clear_sta_flag(tx->sta, WLAN_STA_SP);
1132 ps_dbg(tx->sta->sdata,
1133 "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
1134 tx->sta->sta.addr, tx->sta->sta.aid);
1135 }
Johannes Berga622ab72010-06-10 10:21:39 +02001136 info->control.vif = &tx->sdata->vif;
1137 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Emmanuel Grumbachfacde7f2016-03-17 16:51:41 +02001138 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berga622ab72010-06-10 10:21:39 +02001139 __skb_queue_tail(&tid_tx->pending, skb);
Helmut Schaaaa454582012-03-07 17:20:30 +01001140 if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1141 purge_skb = __skb_dequeue(&tid_tx->pending);
Johannes Berga622ab72010-06-10 10:21:39 +02001142 }
1143 spin_unlock(&tx->sta->lock);
Helmut Schaaaa454582012-03-07 17:20:30 +01001144
1145 if (purge_skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001146 ieee80211_free_txskb(&tx->local->hw, purge_skb);
Johannes Berga622ab72010-06-10 10:21:39 +02001147 }
1148
Nikolay Martynov285fa692011-11-22 21:50:28 -05001149 /* reset session timer */
Sara Sharon914eac22018-04-20 13:49:19 +03001150 if (reset_agg_timer)
Felix Fietkau12d3952f2012-03-18 22:58:06 +01001151 tid_tx->last_tx = jiffies;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001152
Johannes Berga622ab72010-06-10 10:21:39 +02001153 return queued;
1154}
1155
Johannes Berg58d41852007-09-26 17:53:18 +02001156/*
1157 * initialises @tx
Johannes Berg7c107702015-03-20 14:18:27 +01001158 * pass %NULL for the station if unknown, a valid pointer if known
1159 * or an ERR_PTR() if the station is known not to exist
Johannes Berg58d41852007-09-26 17:53:18 +02001160 */
Johannes Berg9ae54c82008-01-31 19:48:20 +01001161static ieee80211_tx_result
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001162ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1163 struct ieee80211_tx_data *tx,
Johannes Berg7c107702015-03-20 14:18:27 +01001164 struct sta_info *sta, struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02001165{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001166 struct ieee80211_local *local = sdata->local;
Johannes Berg58d41852007-09-26 17:53:18 +02001167 struct ieee80211_hdr *hdr;
Johannes Berge039fa42008-05-15 12:55:29 +02001168 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg68f2b512011-10-07 14:01:24 +02001169 int tid;
Johannes Berge2ebc742007-07-27 15:43:22 +02001170
1171 memset(tx, 0, sizeof(*tx));
1172 tx->skb = skb;
Johannes Berge2ebc742007-07-27 15:43:22 +02001173 tx->local = local;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001174 tx->sdata = sdata;
Johannes Berg252b86c2011-11-16 15:28:55 +01001175 __skb_queue_head_init(&tx->skbs);
Johannes Berge2ebc742007-07-27 15:43:22 +02001176
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001177 /*
1178 * If this flag is set to true anywhere, and we get here,
1179 * we are doing the needed processing, so remove the flag
1180 * now.
1181 */
1182 info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1183
Johannes Berg58d41852007-09-26 17:53:18 +02001184 hdr = (struct ieee80211_hdr *) skb->data;
1185
Johannes Berg7c107702015-03-20 14:18:27 +01001186 if (likely(sta)) {
1187 if (!IS_ERR(sta))
1188 tx->sta = sta;
1189 } else {
1190 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1191 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1192 if (!tx->sta && sdata->wdev.use_4addr)
1193 return TX_DROP;
1194 } else if (info->flags & (IEEE80211_TX_INTFL_NL80211_FRAME_TX |
1195 IEEE80211_TX_CTL_INJECTED) ||
1196 tx->sdata->control_port_protocol == tx->skb->protocol) {
1197 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1198 }
1199 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
1200 tx->sta = sta_info_get(sdata, hdr->addr1);
Felix Fietkau3f0e0b22010-01-08 18:15:13 +01001201 }
Johannes Berg58d41852007-09-26 17:53:18 +02001202
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001203 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
Johannes Berg49a59542011-09-29 16:04:41 +02001204 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001205 ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
1206 !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001207 struct tid_ampdu_tx *tid_tx;
1208
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001209 tid = ieee80211_get_tid(hdr);
Sujith8b30b1f2008-10-24 09:55:27 +05301210
Johannes Berga622ab72010-06-10 10:21:39 +02001211 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1212 if (tid_tx) {
1213 bool queued;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001214
Johannes Berga622ab72010-06-10 10:21:39 +02001215 queued = ieee80211_tx_prep_agg(tx, skb, info,
1216 tid_tx, tid);
1217
1218 if (unlikely(queued))
1219 return TX_QUEUED;
1220 }
Sujith8b30b1f2008-10-24 09:55:27 +05301221 }
1222
Jiri Slabybadffb72007-08-28 17:01:54 -04001223 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001224 tx->flags &= ~IEEE80211_TX_UNICAST;
Johannes Berge039fa42008-05-15 12:55:29 +02001225 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Simon Wunderlich6fd67e92011-11-18 14:20:42 +01001226 } else
Johannes Berg5cf121c2008-02-25 16:27:43 +01001227 tx->flags |= IEEE80211_TX_UNICAST;
Johannes Berg58d41852007-09-26 17:53:18 +02001228
Johannes Berga26eb272011-10-07 14:01:25 +02001229 if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1230 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1231 skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1232 info->flags & IEEE80211_TX_CTL_AMPDU)
1233 info->flags |= IEEE80211_TX_CTL_DONTFRAG;
Johannes Berg58d41852007-09-26 17:53:18 +02001234 }
1235
Johannes Berge2ebc742007-07-27 15:43:22 +02001236 if (!tx->sta)
Johannes Berge039fa42008-05-15 12:55:29 +02001237 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Felix Fietkauf7418bc2015-09-24 14:59:49 +02001238 else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
Johannes Berge039fa42008-05-15 12:55:29 +02001239 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Felix Fietkauf7418bc2015-09-24 14:59:49 +02001240 ieee80211_check_fast_xmit(tx->sta);
1241 }
Johannes Berg58d41852007-09-26 17:53:18 +02001242
Johannes Berge039fa42008-05-15 12:55:29 +02001243 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001244
Johannes Berg9ae54c82008-01-31 19:48:20 +01001245 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001246}
1247
Michal Kazior80a83cf2016-05-19 10:37:48 +02001248static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
1249 struct ieee80211_vif *vif,
Michal Kaziordbef5362017-01-13 13:32:51 +01001250 struct sta_info *sta,
Michal Kazior80a83cf2016-05-19 10:37:48 +02001251 struct sk_buff *skb)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001252{
1253 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001254 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001255 struct ieee80211_txq *txq = NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001256
Felix Fietkauc3732a72016-02-28 15:19:53 +01001257 if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
1258 (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
Michal Kazior80a83cf2016-05-19 10:37:48 +02001259 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001260
John Crispin50ff4772019-11-25 11:04:37 +01001261 if (!(info->control.flags & IEEE80211_TX_CTRL_HW_80211_ENCAP) &&
1262 unlikely(!ieee80211_is_data_present(hdr->frame_control))) {
Johannes Bergadf8ed02018-08-31 11:31:08 +03001263 if ((!ieee80211_is_mgmt(hdr->frame_control) ||
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001264 ieee80211_is_bufferable_mmpdu(hdr->frame_control) ||
1265 vif->type == NL80211_IFTYPE_STATION) &&
Johannes Bergadf8ed02018-08-31 11:31:08 +03001266 sta && sta->uploaded) {
1267 /*
1268 * This will be NULL if the driver didn't set the
1269 * opt-in hardware flag.
1270 */
1271 txq = sta->sta.txq[IEEE80211_NUM_TIDS];
1272 }
1273 } else if (sta) {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001274 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1275
Michal Kaziordbef5362017-01-13 13:32:51 +01001276 if (!sta->uploaded)
1277 return NULL;
1278
1279 txq = sta->sta.txq[tid];
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001280 } else if (vif) {
1281 txq = vif->txq;
1282 }
1283
1284 if (!txq)
Michal Kazior80a83cf2016-05-19 10:37:48 +02001285 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001286
Michal Kazior80a83cf2016-05-19 10:37:48 +02001287 return to_txq_info(txq);
1288}
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001289
Michal Kazior5caa3282016-05-19 10:37:51 +02001290static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
1291{
1292 IEEE80211_SKB_CB(skb)->control.enqueue_time = codel_get_time();
1293}
1294
Michal Kazior5caa3282016-05-19 10:37:51 +02001295static u32 codel_skb_len_func(const struct sk_buff *skb)
1296{
1297 return skb->len;
1298}
1299
1300static codel_time_t codel_skb_time_func(const struct sk_buff *skb)
1301{
1302 const struct ieee80211_tx_info *info;
1303
1304 info = (const struct ieee80211_tx_info *)skb->cb;
1305 return info->control.enqueue_time;
1306}
1307
1308static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars,
1309 void *ctx)
1310{
1311 struct ieee80211_local *local;
1312 struct txq_info *txqi;
1313 struct fq *fq;
1314 struct fq_flow *flow;
1315
1316 txqi = ctx;
1317 local = vif_to_sdata(txqi->txq.vif)->local;
1318 fq = &local->fq;
1319
1320 if (cvars == &txqi->def_cvars)
1321 flow = &txqi->def_flow;
1322 else
1323 flow = &fq->flows[cvars - local->cvars];
1324
1325 return fq_flow_dequeue(fq, flow);
1326}
1327
1328static void codel_drop_func(struct sk_buff *skb,
1329 void *ctx)
1330{
1331 struct ieee80211_local *local;
1332 struct ieee80211_hw *hw;
1333 struct txq_info *txqi;
1334
1335 txqi = ctx;
1336 local = vif_to_sdata(txqi->txq.vif)->local;
1337 hw = &local->hw;
1338
1339 ieee80211_free_txskb(hw, skb);
1340}
1341
Michal Kaziorfa962b92016-05-19 10:37:49 +02001342static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
1343 struct fq_tin *tin,
1344 struct fq_flow *flow)
1345{
Michal Kazior5caa3282016-05-19 10:37:51 +02001346 struct ieee80211_local *local;
1347 struct txq_info *txqi;
1348 struct codel_vars *cvars;
1349 struct codel_params *cparams;
1350 struct codel_stats *cstats;
1351
1352 local = container_of(fq, struct ieee80211_local, fq);
1353 txqi = container_of(tin, struct txq_info, tin);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001354 cstats = &txqi->cstats;
Michal Kazior5caa3282016-05-19 10:37:51 +02001355
Toke Høiland-Jørgensen484a54c2017-04-06 11:38:26 +02001356 if (txqi->txq.sta) {
1357 struct sta_info *sta = container_of(txqi->txq.sta,
1358 struct sta_info, sta);
1359 cparams = &sta->cparams;
1360 } else {
1361 cparams = &local->cparams;
1362 }
1363
Michal Kazior5caa3282016-05-19 10:37:51 +02001364 if (flow == &txqi->def_flow)
1365 cvars = &txqi->def_cvars;
1366 else
1367 cvars = &local->cvars[flow - fq->flows];
1368
1369 return codel_dequeue(txqi,
1370 &flow->backlog,
1371 cparams,
1372 cvars,
1373 cstats,
1374 codel_skb_len_func,
1375 codel_skb_time_func,
1376 codel_drop_func,
1377 codel_dequeue_func);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001378}
1379
1380static void fq_skb_free_func(struct fq *fq,
1381 struct fq_tin *tin,
1382 struct fq_flow *flow,
1383 struct sk_buff *skb)
1384{
1385 struct ieee80211_local *local;
1386
1387 local = container_of(fq, struct ieee80211_local, fq);
1388 ieee80211_free_txskb(&local->hw, skb);
1389}
1390
1391static struct fq_flow *fq_flow_get_default_func(struct fq *fq,
1392 struct fq_tin *tin,
1393 int idx,
1394 struct sk_buff *skb)
1395{
1396 struct txq_info *txqi;
1397
1398 txqi = container_of(tin, struct txq_info, tin);
1399 return &txqi->def_flow;
1400}
1401
Michal Kazior80a83cf2016-05-19 10:37:48 +02001402static void ieee80211_txq_enqueue(struct ieee80211_local *local,
1403 struct txq_info *txqi,
1404 struct sk_buff *skb)
1405{
Michal Kaziorfa962b92016-05-19 10:37:49 +02001406 struct fq *fq = &local->fq;
1407 struct fq_tin *tin = &txqi->tin;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001408 u32 flow_idx = fq_flow_idx(fq, skb);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001409
Michal Kazior5caa3282016-05-19 10:37:51 +02001410 ieee80211_set_skb_enqueue_time(skb);
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001411
1412 spin_lock_bh(&fq->lock);
1413 fq_tin_enqueue(fq, tin, flow_idx, skb,
Michal Kaziorfa962b92016-05-19 10:37:49 +02001414 fq_skb_free_func,
1415 fq_flow_get_default_func);
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001416 spin_unlock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001417}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001418
Johannes Berg2a9e2572017-10-06 11:53:33 +02001419static bool fq_vlan_filter_func(struct fq *fq, struct fq_tin *tin,
1420 struct fq_flow *flow, struct sk_buff *skb,
1421 void *data)
1422{
1423 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1424
1425 return info->control.vif == data;
1426}
1427
1428void ieee80211_txq_remove_vlan(struct ieee80211_local *local,
1429 struct ieee80211_sub_if_data *sdata)
1430{
1431 struct fq *fq = &local->fq;
1432 struct txq_info *txqi;
1433 struct fq_tin *tin;
1434 struct ieee80211_sub_if_data *ap;
1435
1436 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1437 return;
1438
1439 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1440
1441 if (!ap->vif.txq)
1442 return;
1443
1444 txqi = to_txq_info(ap->vif.txq);
1445 tin = &txqi->tin;
1446
1447 spin_lock_bh(&fq->lock);
1448 fq_tin_filter(fq, tin, fq_vlan_filter_func, &sdata->vif,
1449 fq_skb_free_func);
1450 spin_unlock_bh(&fq->lock);
1451}
1452
Michal Kaziorfa962b92016-05-19 10:37:49 +02001453void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
1454 struct sta_info *sta,
1455 struct txq_info *txqi, int tid)
1456{
1457 fq_tin_init(&txqi->tin);
1458 fq_flow_init(&txqi->def_flow);
Michal Kazior5caa3282016-05-19 10:37:51 +02001459 codel_vars_init(&txqi->def_cvars);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001460 codel_stats_init(&txqi->cstats);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001461 __skb_queue_head_init(&txqi->frags);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001462 INIT_LIST_HEAD(&txqi->schedule_order);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001463
1464 txqi->txq.vif = &sdata->vif;
1465
Johannes Bergadf8ed02018-08-31 11:31:08 +03001466 if (!sta) {
Michal Kaziorfa962b92016-05-19 10:37:49 +02001467 sdata->vif.txq = &txqi->txq;
1468 txqi->txq.tid = 0;
1469 txqi->txq.ac = IEEE80211_AC_BE;
Johannes Bergadf8ed02018-08-31 11:31:08 +03001470
1471 return;
Michal Kazior80a83cf2016-05-19 10:37:48 +02001472 }
Johannes Bergadf8ed02018-08-31 11:31:08 +03001473
1474 if (tid == IEEE80211_NUM_TIDS) {
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001475 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1476 /* Drivers need to opt in to the management MPDU TXQ */
1477 if (!ieee80211_hw_check(&sdata->local->hw,
1478 STA_MMPDU_TXQ))
1479 return;
1480 } else if (!ieee80211_hw_check(&sdata->local->hw,
1481 BUFF_MMPDU_TXQ)) {
1482 /* Drivers need to opt in to the bufferable MMPDU TXQ */
Johannes Bergadf8ed02018-08-31 11:31:08 +03001483 return;
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001484 }
Johannes Bergadf8ed02018-08-31 11:31:08 +03001485 txqi->txq.ac = IEEE80211_AC_VO;
1486 } else {
1487 txqi->txq.ac = ieee80211_ac_from_tid(tid);
1488 }
1489
1490 txqi->txq.sta = &sta->sta;
1491 txqi->txq.tid = tid;
1492 sta->sta.txq[tid] = &txqi->txq;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001493}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001494
Michal Kaziorfa962b92016-05-19 10:37:49 +02001495void ieee80211_txq_purge(struct ieee80211_local *local,
1496 struct txq_info *txqi)
1497{
1498 struct fq *fq = &local->fq;
1499 struct fq_tin *tin = &txqi->tin;
1500
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08001501 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001502 fq_tin_reset(fq, tin, fq_skb_free_func);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001503 ieee80211_purge_tx_queue(&local->hw, &txqi->frags);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08001504 spin_unlock_bh(&fq->lock);
1505
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001506 spin_lock_bh(&local->active_txq_lock[txqi->txq.ac]);
1507 list_del_init(&txqi->schedule_order);
1508 spin_unlock_bh(&local->active_txq_lock[txqi->txq.ac]);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001509}
1510
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001511void ieee80211_txq_set_params(struct ieee80211_local *local)
1512{
1513 if (local->hw.wiphy->txq_limit)
1514 local->fq.limit = local->hw.wiphy->txq_limit;
1515 else
1516 local->hw.wiphy->txq_limit = local->fq.limit;
1517
1518 if (local->hw.wiphy->txq_memory_limit)
1519 local->fq.memory_limit = local->hw.wiphy->txq_memory_limit;
1520 else
1521 local->hw.wiphy->txq_memory_limit = local->fq.memory_limit;
1522
1523 if (local->hw.wiphy->txq_quantum)
1524 local->fq.quantum = local->hw.wiphy->txq_quantum;
1525 else
1526 local->hw.wiphy->txq_quantum = local->fq.quantum;
1527}
1528
Michal Kaziorfa962b92016-05-19 10:37:49 +02001529int ieee80211_txq_setup_flows(struct ieee80211_local *local)
1530{
1531 struct fq *fq = &local->fq;
1532 int ret;
Michal Kazior5caa3282016-05-19 10:37:51 +02001533 int i;
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001534 bool supp_vht = false;
1535 enum nl80211_band band;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001536
1537 if (!local->ops->wake_tx_queue)
1538 return 0;
1539
1540 ret = fq_init(fq, 4096);
1541 if (ret)
1542 return ret;
1543
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001544 /*
1545 * If the hardware doesn't support VHT, it is safe to limit the maximum
1546 * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
1547 */
1548 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1549 struct ieee80211_supported_band *sband;
1550
1551 sband = local->hw.wiphy->bands[band];
1552 if (!sband)
1553 continue;
1554
1555 supp_vht = supp_vht || sband->vht_cap.vht_supported;
1556 }
1557
1558 if (!supp_vht)
1559 fq->memory_limit = 4 << 20; /* 4 Mbytes */
1560
Michal Kazior5caa3282016-05-19 10:37:51 +02001561 codel_params_init(&local->cparams);
Michal Kazior5caa3282016-05-19 10:37:51 +02001562 local->cparams.interval = MS2TIME(100);
1563 local->cparams.target = MS2TIME(20);
1564 local->cparams.ecn = true;
1565
1566 local->cvars = kcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
1567 GFP_KERNEL);
1568 if (!local->cvars) {
Michal Kazior59a7c822016-06-29 14:00:34 +02001569 spin_lock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001570 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001571 spin_unlock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001572 return -ENOMEM;
1573 }
1574
1575 for (i = 0; i < fq->flows_cnt; i++)
1576 codel_vars_init(&local->cvars[i]);
1577
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001578 ieee80211_txq_set_params(local);
1579
Michal Kaziorfa962b92016-05-19 10:37:49 +02001580 return 0;
1581}
1582
1583void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1584{
1585 struct fq *fq = &local->fq;
1586
1587 if (!local->ops->wake_tx_queue)
1588 return;
1589
Michal Kazior5caa3282016-05-19 10:37:51 +02001590 kfree(local->cvars);
1591 local->cvars = NULL;
1592
Michal Kazior59a7c822016-06-29 14:00:34 +02001593 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001594 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001595 spin_unlock_bh(&fq->lock);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001596}
1597
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001598static bool ieee80211_queue_skb(struct ieee80211_local *local,
1599 struct ieee80211_sub_if_data *sdata,
1600 struct sta_info *sta,
1601 struct sk_buff *skb)
1602{
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001603 struct ieee80211_vif *vif;
1604 struct txq_info *txqi;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001605
1606 if (!local->ops->wake_tx_queue ||
1607 sdata->vif.type == NL80211_IFTYPE_MONITOR)
1608 return false;
1609
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001610 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1611 sdata = container_of(sdata->bss,
1612 struct ieee80211_sub_if_data, u.ap);
1613
1614 vif = &sdata->vif;
Michal Kaziordbef5362017-01-13 13:32:51 +01001615 txqi = ieee80211_get_txq(local, vif, sta, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001616
1617 if (!txqi)
1618 return false;
1619
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001620 ieee80211_txq_enqueue(local, txqi, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001621
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001622 schedule_and_wake_txq(local, txqi);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001623
1624 return true;
1625}
1626
Johannes Berg11127e92011-11-16 16:02:47 +01001627static bool ieee80211_tx_frags(struct ieee80211_local *local,
1628 struct ieee80211_vif *vif,
Johannes Berg4fd03282019-10-01 23:26:35 +02001629 struct sta_info *sta,
Johannes Berg11127e92011-11-16 16:02:47 +01001630 struct sk_buff_head *skbs,
1631 bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001632{
Michal Kazior80a83cf2016-05-19 10:37:48 +02001633 struct ieee80211_tx_control control = {};
Johannes Berg252b86c2011-11-16 15:28:55 +01001634 struct sk_buff *skb, *tmp;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001635 unsigned long flags;
Johannes Berge2ebc742007-07-27 15:43:22 +02001636
Johannes Berg252b86c2011-11-16 15:28:55 +01001637 skb_queue_walk_safe(skbs, skb, tmp) {
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001638 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1639 int q = info->hw_queue;
1640
1641#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1642 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1643 __skb_unlink(skb, skbs);
Felix Fietkauc3e77242012-10-08 14:39:33 +02001644 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001645 continue;
1646 }
1647#endif
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001648
1649 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001650 if (local->queue_stop_reasons[q] ||
Johannes Berg7bb45682011-02-24 14:42:06 +01001651 (!txpending && !skb_queue_empty(&local->pending[q]))) {
Seth Forshee6c17b772013-02-11 11:21:07 -06001652 if (unlikely(info->flags &
Seth Forsheea7679ed2013-02-25 14:58:05 -06001653 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1654 if (local->queue_stop_reasons[q] &
1655 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1656 /*
1657 * Drop off-channel frames if queues
1658 * are stopped for any reason other
1659 * than off-channel operation. Never
1660 * queue them.
1661 */
1662 spin_unlock_irqrestore(
1663 &local->queue_stop_reason_lock,
1664 flags);
1665 ieee80211_purge_tx_queue(&local->hw,
1666 skbs);
1667 return true;
1668 }
1669 } else {
1670
Seth Forshee6c17b772013-02-11 11:21:07 -06001671 /*
Seth Forsheea7679ed2013-02-25 14:58:05 -06001672 * Since queue is stopped, queue up frames for
1673 * later transmission from the tx-pending
1674 * tasklet when the queue is woken again.
Seth Forshee6c17b772013-02-11 11:21:07 -06001675 */
Seth Forsheea7679ed2013-02-25 14:58:05 -06001676 if (txpending)
1677 skb_queue_splice_init(skbs,
1678 &local->pending[q]);
1679 else
1680 skb_queue_splice_tail_init(skbs,
1681 &local->pending[q]);
1682
1683 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1684 flags);
1685 return false;
Seth Forshee6c17b772013-02-11 11:21:07 -06001686 }
Johannes Berg7bb45682011-02-24 14:42:06 +01001687 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001688 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Tomas Winklerf8e79dd2008-07-24 18:46:44 +03001689
Johannes Berg11127e92011-11-16 16:02:47 +01001690 info->control.vif = vif;
Johannes Berg4fd03282019-10-01 23:26:35 +02001691 control.sta = sta ? &sta->sta : NULL;
Johannes Bergec25acc2010-07-22 17:11:28 +02001692
Johannes Berg252b86c2011-11-16 15:28:55 +01001693 __skb_unlink(skb, skbs);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001694 drv_tx(local, &control, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001695 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001696
Johannes Berg11127e92011-11-16 16:02:47 +01001697 return true;
1698}
1699
1700/*
1701 * Returns false if the frame couldn't be transmitted but was queued instead.
1702 */
1703static bool __ieee80211_tx(struct ieee80211_local *local,
1704 struct sk_buff_head *skbs, int led_len,
1705 struct sta_info *sta, bool txpending)
1706{
1707 struct ieee80211_tx_info *info;
1708 struct ieee80211_sub_if_data *sdata;
1709 struct ieee80211_vif *vif;
Johannes Berg11127e92011-11-16 16:02:47 +01001710 struct sk_buff *skb;
1711 bool result = true;
1712 __le16 fc;
1713
1714 if (WARN_ON(skb_queue_empty(skbs)))
1715 return true;
1716
1717 skb = skb_peek(skbs);
1718 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1719 info = IEEE80211_SKB_CB(skb);
1720 sdata = vif_to_sdata(info->control.vif);
1721 if (sta && !sta->uploaded)
1722 sta = NULL;
1723
Johannes Berg11127e92011-11-16 16:02:47 +01001724 switch (sdata->vif.type) {
1725 case NL80211_IFTYPE_MONITOR:
Aviya Erenfeldd8212182016-08-29 23:25:15 +03001726 if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
Johannes Bergc82b5a72013-07-14 23:39:20 +03001727 vif = &sdata->vif;
1728 break;
1729 }
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001730 sdata = rcu_dereference(local->monitor_sdata);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001731 if (sdata) {
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001732 vif = &sdata->vif;
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001733 info->hw_queue =
1734 vif->hw_queue[skb_get_queue_mapping(skb)];
Johannes Berg30686bf2015-06-02 21:39:54 +02001735 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
Johannes Berg63b4d8b2015-11-24 15:41:50 +01001736 ieee80211_purge_tx_queue(&local->hw, skbs);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001737 return true;
1738 } else
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001739 vif = NULL;
Johannes Berg11127e92011-11-16 16:02:47 +01001740 break;
1741 case NL80211_IFTYPE_AP_VLAN:
1742 sdata = container_of(sdata->bss,
1743 struct ieee80211_sub_if_data, u.ap);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05001744 fallthrough;
Johannes Berg11127e92011-11-16 16:02:47 +01001745 default:
1746 vif = &sdata->vif;
1747 break;
1748 }
1749
Johannes Berg4fd03282019-10-01 23:26:35 +02001750 result = ieee80211_tx_frags(local, vif, sta, skbs, txpending);
Johannes Berg11127e92011-11-16 16:02:47 +01001751
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001752 ieee80211_tpt_led_trig_tx(local, fc, led_len);
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001753
Johannes Berg4db4e0a2011-11-24 14:47:36 +01001754 WARN_ON_ONCE(!skb_queue_empty(skbs));
Johannes Berg252b86c2011-11-16 15:28:55 +01001755
Johannes Berg11127e92011-11-16 16:02:47 +01001756 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001757}
1758
Johannes Berg97b045d2008-06-20 01:22:30 +02001759/*
1760 * Invoke TX handlers, return 0 on success and non-zero if the
1761 * frame was dropped or queued.
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001762 *
1763 * The handlers are split into an early and late part. The latter is everything
1764 * that can be sensitive to reordering, and will be deferred to after packets
1765 * are dequeued from the intermediate queues (when they are enabled).
Johannes Berg97b045d2008-06-20 01:22:30 +02001766 */
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001767static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx)
Johannes Berg97b045d2008-06-20 01:22:30 +02001768{
Johannes Berg97b045d2008-06-20 01:22:30 +02001769 ieee80211_tx_result res = TX_DROP;
Johannes Berg97b045d2008-06-20 01:22:30 +02001770
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001771#define CALL_TXH(txh) \
1772 do { \
1773 res = txh(tx); \
1774 if (res != TX_CONTINUE) \
1775 goto txh_done; \
1776 } while (0)
Johannes Berg97b045d2008-06-20 01:22:30 +02001777
Kalle Valo5c1b98a2010-01-12 10:42:46 +02001778 CALL_TXH(ieee80211_tx_h_dynamic_ps);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001779 CALL_TXH(ieee80211_tx_h_check_assoc);
1780 CALL_TXH(ieee80211_tx_h_ps_buf);
Johannes Berga621fa42010-08-27 14:26:54 +03001781 CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001782 CALL_TXH(ieee80211_tx_h_select_key);
Johannes Berg30686bf2015-06-02 21:39:54 +02001783 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
Johannes Bergaf65cd962009-11-17 18:18:36 +01001784 CALL_TXH(ieee80211_tx_h_rate_ctrl);
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001785
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001786 txh_done:
1787 if (unlikely(res == TX_DROP)) {
1788 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1789 if (tx->skb)
1790 ieee80211_free_txskb(&tx->local->hw, tx->skb);
1791 else
1792 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1793 return -1;
1794 } else if (unlikely(res == TX_QUEUED)) {
1795 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1796 return -1;
1797 }
1798
1799 return 0;
1800}
1801
1802/*
1803 * Late handlers can be called while the sta lock is held. Handlers that can
1804 * cause packets to be generated will cause deadlock!
1805 */
1806static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx)
1807{
1808 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1809 ieee80211_tx_result res = TX_CONTINUE;
1810
Johannes Bergaa5b5492011-12-02 22:08:52 +01001811 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1812 __skb_queue_tail(&tx->skbs, tx->skb);
1813 tx->skb = NULL;
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001814 goto txh_done;
Johannes Bergaa5b5492011-12-02 22:08:52 +01001815 }
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001816
1817 CALL_TXH(ieee80211_tx_h_michael_mic_add);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001818 CALL_TXH(ieee80211_tx_h_sequence);
1819 CALL_TXH(ieee80211_tx_h_fragment);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001820 /* handlers after fragment must be aware of tx info fragmentation! */
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001821 CALL_TXH(ieee80211_tx_h_stats);
1822 CALL_TXH(ieee80211_tx_h_encrypt);
Johannes Berg30686bf2015-06-02 21:39:54 +02001823 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
Arik Nemtsov8fd369e2011-01-31 22:29:12 +02001824 CALL_TXH(ieee80211_tx_h_calculate_duration);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001825#undef CALL_TXH
1826
1827 txh_done:
Johannes Berg97b045d2008-06-20 01:22:30 +02001828 if (unlikely(res == TX_DROP)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001829 I802_DEBUG_INC(tx->local->tx_handlers_drop);
Johannes Berg252b86c2011-11-16 15:28:55 +01001830 if (tx->skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001831 ieee80211_free_txskb(&tx->local->hw, tx->skb);
Johannes Berg252b86c2011-11-16 15:28:55 +01001832 else
Felix Fietkau1f98ab72012-11-10 03:44:14 +01001833 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
Johannes Berg97b045d2008-06-20 01:22:30 +02001834 return -1;
1835 } else if (unlikely(res == TX_QUEUED)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001836 I802_DEBUG_INC(tx->local->tx_handlers_queued);
Johannes Berg97b045d2008-06-20 01:22:30 +02001837 return -1;
1838 }
1839
1840 return 0;
1841}
1842
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001843static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1844{
1845 int r = invoke_tx_handlers_early(tx);
1846
1847 if (r)
1848 return r;
1849 return invoke_tx_handlers_late(tx);
1850}
1851
Felix Fietkau06be6b12013-10-14 18:01:00 +02001852bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1853 struct ieee80211_vif *vif, struct sk_buff *skb,
1854 int band, struct ieee80211_sta **sta)
1855{
1856 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1857 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1858 struct ieee80211_tx_data tx;
Johannes Berg88724a82015-03-01 09:10:12 +02001859 struct sk_buff *skb2;
Felix Fietkau06be6b12013-10-14 18:01:00 +02001860
Johannes Berg7c107702015-03-20 14:18:27 +01001861 if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
Felix Fietkau06be6b12013-10-14 18:01:00 +02001862 return false;
1863
1864 info->band = band;
1865 info->control.vif = vif;
1866 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1867
1868 if (invoke_tx_handlers(&tx))
1869 return false;
1870
1871 if (sta) {
1872 if (tx.sta)
1873 *sta = &tx.sta->sta;
1874 else
1875 *sta = NULL;
1876 }
1877
Johannes Berg88724a82015-03-01 09:10:12 +02001878 /* this function isn't suitable for fragmented data frames */
1879 skb2 = __skb_dequeue(&tx.skbs);
1880 if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1881 ieee80211_free_txskb(hw, skb2);
1882 ieee80211_purge_tx_queue(hw, &tx.skbs);
1883 return false;
1884 }
1885
Felix Fietkau06be6b12013-10-14 18:01:00 +02001886 return true;
1887}
1888EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1889
Johannes Berg7bb45682011-02-24 14:42:06 +01001890/*
1891 * Returns false if the frame couldn't be transmitted but was queued instead.
1892 */
1893static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01001894 struct sta_info *sta, struct sk_buff *skb,
Mathy Vanhoef08aca292020-07-23 14:01:52 +04001895 bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001896{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001897 struct ieee80211_local *local = sdata->local;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001898 struct ieee80211_tx_data tx;
Johannes Berg97b045d2008-06-20 01:22:30 +02001899 ieee80211_tx_result res_prepare;
Johannes Berge039fa42008-05-15 12:55:29 +02001900 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001901 bool result = true;
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001902 int led_len;
Johannes Berge2ebc742007-07-27 15:43:22 +02001903
Johannes Berge2ebc742007-07-27 15:43:22 +02001904 if (unlikely(skb->len < 10)) {
1905 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001906 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001907 }
1908
Johannes Berg58d41852007-09-26 17:53:18 +02001909 /* initialises tx */
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001910 led_len = skb->len;
Johannes Berg7c107702015-03-20 14:18:27 +01001911 res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001912
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001913 if (unlikely(res_prepare == TX_DROP)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02001914 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001915 return true;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001916 } else if (unlikely(res_prepare == TX_QUEUED)) {
Johannes Berg55de9082012-07-26 17:24:39 +02001917 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001918 }
1919
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001920 /* set up hw_queue value early */
1921 if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
Johannes Berg30686bf2015-06-02 21:39:54 +02001922 !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001923 info->hw_queue =
1924 sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1925
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001926 if (invoke_tx_handlers_early(&tx))
Bob Copeland6eae4a62018-09-05 06:22:59 -04001927 return true;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001928
1929 if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb))
1930 return true;
1931
1932 if (!invoke_tx_handlers_late(&tx))
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001933 result = __ieee80211_tx(local, &tx.skbs, led_len,
1934 tx.sta, txpending);
Johannes Berg55de9082012-07-26 17:24:39 +02001935
Johannes Berg7bb45682011-02-24 14:42:06 +01001936 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001937}
1938
1939/* device xmit handlers */
1940
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301941static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
Johannes Berg23c07522008-05-29 10:38:53 +02001942 struct sk_buff *skb,
1943 int head_need, bool may_encrypt)
1944{
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301945 struct ieee80211_local *local = sdata->local;
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001946 struct ieee80211_hdr *hdr;
1947 bool enc_tailroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001948 int tail_need = 0;
1949
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001950 hdr = (struct ieee80211_hdr *) skb->data;
1951 enc_tailroom = may_encrypt &&
1952 (sdata->crypto_tx_tailroom_needed_cnt ||
1953 ieee80211_is_mgmt(hdr->frame_control));
1954
1955 if (enc_tailroom) {
Johannes Berg23c07522008-05-29 10:38:53 +02001956 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1957 tail_need -= skb_tailroom(skb);
1958 tail_need = max_t(int, tail_need, 0);
1959 }
1960
Ido Yarivc70f59a2014-07-29 15:39:14 +03001961 if (skb_cloned(skb) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001962 (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001963 !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
Johannes Berg23c07522008-05-29 10:38:53 +02001964 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001965 else if (head_need || tail_need)
Johannes Berg23c07522008-05-29 10:38:53 +02001966 I802_DEBUG_INC(local->tx_expand_skb_head);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001967 else
1968 return 0;
Johannes Berg23c07522008-05-29 10:38:53 +02001969
1970 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
Joe Perches0fb9a9e2010-08-20 16:25:38 -07001971 wiphy_debug(local->hw.wiphy,
1972 "failed to reallocate TX buffer\n");
Johannes Berg23c07522008-05-29 10:38:53 +02001973 return -ENOMEM;
1974 }
1975
Johannes Berg23c07522008-05-29 10:38:53 +02001976 return 0;
1977}
1978
Johannes Berg7c107702015-03-20 14:18:27 +01001979void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
Mathy Vanhoef08aca292020-07-23 14:01:52 +04001980 struct sta_info *sta, struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02001981{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001982 struct ieee80211_local *local = sdata->local;
Johannes Berge039fa42008-05-15 12:55:29 +02001983 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Colin Ian King11b05ba2018-02-02 16:31:42 +00001984 struct ieee80211_hdr *hdr;
Johannes Berge2ebc742007-07-27 15:43:22 +02001985 int headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001986 bool may_encrypt;
Johannes Berge2ebc742007-07-27 15:43:22 +02001987
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001988 may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
Johannes Berg23c07522008-05-29 10:38:53 +02001989
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001990 headroom = local->tx_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001991 if (may_encrypt)
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001992 headroom += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001993 headroom -= skb_headroom(skb);
1994 headroom = max_t(int, 0, headroom);
1995
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301996 if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02001997 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001998 return;
Johannes Berge2ebc742007-07-27 15:43:22 +02001999 }
2000
Steve deRosier740c1aa2010-09-11 20:01:31 -07002001 hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berg5061b0c2009-07-14 00:33:34 +02002002 info->control.vif = &sdata->vif;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01002003
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002004 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2005 if (ieee80211_is_data(hdr->frame_control) &&
2006 is_unicast_ether_addr(hdr->addr1)) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002007 if (mesh_nexthop_resolve(sdata, skb))
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002008 return; /* skb queued: don't free */
2009 } else {
2010 ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
2011 }
Johannes Berg98aed9f2012-03-27 14:18:36 +02002012 }
Javier Cardonacca89492009-08-10 17:29:29 -07002013
Javier Cardona2154c81c2011-09-07 17:49:53 -07002014 ieee80211_set_qos_hdr(sdata, skb);
Mathy Vanhoef08aca292020-07-23 14:01:52 +04002015 ieee80211_tx(sdata, sta, skb, false);
Johannes Berge2ebc742007-07-27 15:43:22 +02002016}
2017
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002018bool ieee80211_parse_tx_radiotap(struct sk_buff *skb,
2019 struct net_device *dev)
Johannes Berg73b9f032011-10-07 14:01:26 +02002020{
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002021 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg73b9f032011-10-07 14:01:26 +02002022 struct ieee80211_radiotap_iterator iterator;
2023 struct ieee80211_radiotap_header *rthdr =
2024 (struct ieee80211_radiotap_header *) skb->data;
2025 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002026 struct ieee80211_supported_band *sband =
2027 local->hw.wiphy->bands[info->band];
Johannes Berg73b9f032011-10-07 14:01:26 +02002028 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
2029 NULL);
2030 u16 txflags;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002031 u16 rate = 0;
2032 bool rate_found = false;
2033 u8 rate_retries = 0;
2034 u16 rate_flags = 0;
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002035 u8 mcs_known, mcs_flags, mcs_bw;
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002036 u16 vht_known;
2037 u8 vht_mcs = 0, vht_nss = 0;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002038 int i;
Johannes Berg73b9f032011-10-07 14:01:26 +02002039
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002040 /* check for not even having the fixed radiotap header part */
2041 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2042 return false; /* too short to be possibly valid */
2043
2044 /* is it a header version we can trust to find length from? */
2045 if (unlikely(rthdr->it_version))
2046 return false; /* only version 0 is supported */
2047
2048 /* does the skb contain enough to deliver on the alleged length? */
2049 if (unlikely(skb->len < ieee80211_get_radiotap_len(skb->data)))
2050 return false; /* skb too short for claimed rt header extent */
2051
Johannes Berg73b9f032011-10-07 14:01:26 +02002052 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
2053 IEEE80211_TX_CTL_DONTFRAG;
2054
2055 /*
2056 * for every radiotap entry that is present
2057 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
2058 * entries present, or -EINVAL on error)
2059 */
2060
2061 while (!ret) {
2062 ret = ieee80211_radiotap_iterator_next(&iterator);
2063
2064 if (ret)
2065 continue;
2066
2067 /* see if this argument is something we can use */
2068 switch (iterator.this_arg_index) {
2069 /*
2070 * You must take care when dereferencing iterator.this_arg
2071 * for multibyte types... the pointer is not aligned. Use
2072 * get_unaligned((type *)iterator.this_arg) to dereference
2073 * iterator.this_arg for type "type" safely on all arches.
2074 */
2075 case IEEE80211_RADIOTAP_FLAGS:
2076 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
2077 /*
2078 * this indicates that the skb we have been
2079 * handed has the 32-bit FCS CRC at the end...
2080 * we should react to that by snipping it off
2081 * because it will be recomputed and added
2082 * on transmission
2083 */
2084 if (skb->len < (iterator._max_length + FCS_LEN))
2085 return false;
2086
2087 skb_trim(skb, skb->len - FCS_LEN);
2088 }
2089 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
2090 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
2091 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
2092 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
2093 break;
2094
2095 case IEEE80211_RADIOTAP_TX_FLAGS:
2096 txflags = get_unaligned_le16(iterator.this_arg);
2097 if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
2098 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Mathy Vanhoefe02281e2020-07-23 14:01:49 +04002099 if (txflags & IEEE80211_RADIOTAP_F_TX_NOSEQNO)
2100 info->control.flags |= IEEE80211_TX_CTRL_NO_SEQNO;
Johannes Berg73b9f032011-10-07 14:01:26 +02002101 break;
2102
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002103 case IEEE80211_RADIOTAP_RATE:
2104 rate = *iterator.this_arg;
2105 rate_flags = 0;
2106 rate_found = true;
2107 break;
2108
2109 case IEEE80211_RADIOTAP_DATA_RETRIES:
2110 rate_retries = *iterator.this_arg;
2111 break;
2112
2113 case IEEE80211_RADIOTAP_MCS:
2114 mcs_known = iterator.this_arg[0];
2115 mcs_flags = iterator.this_arg[1];
2116 if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
2117 break;
2118
2119 rate_found = true;
2120 rate = iterator.this_arg[2];
2121 rate_flags = IEEE80211_TX_RC_MCS;
2122
2123 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
2124 mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
2125 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2126
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002127 mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002128 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002129 mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002130 rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2131 break;
2132
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002133 case IEEE80211_RADIOTAP_VHT:
2134 vht_known = get_unaligned_le16(iterator.this_arg);
2135 rate_found = true;
2136
2137 rate_flags = IEEE80211_TX_RC_VHT_MCS;
2138 if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
2139 (iterator.this_arg[2] &
2140 IEEE80211_RADIOTAP_VHT_FLAG_SGI))
2141 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2142 if (vht_known &
2143 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
2144 if (iterator.this_arg[3] == 1)
2145 rate_flags |=
2146 IEEE80211_TX_RC_40_MHZ_WIDTH;
2147 else if (iterator.this_arg[3] == 4)
2148 rate_flags |=
2149 IEEE80211_TX_RC_80_MHZ_WIDTH;
2150 else if (iterator.this_arg[3] == 11)
2151 rate_flags |=
2152 IEEE80211_TX_RC_160_MHZ_WIDTH;
2153 }
2154
2155 vht_mcs = iterator.this_arg[4] >> 4;
2156 vht_nss = iterator.this_arg[4] & 0xF;
2157 break;
2158
Johannes Berg73b9f032011-10-07 14:01:26 +02002159 /*
2160 * Please update the file
Mauro Carvalho Chehab429ff872020-04-30 18:03:59 +02002161 * Documentation/networking/mac80211-injection.rst
Johannes Berg73b9f032011-10-07 14:01:26 +02002162 * when parsing new fields here.
2163 */
2164
2165 default:
2166 break;
2167 }
2168 }
2169
2170 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
2171 return false;
2172
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002173 if (rate_found) {
2174 info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
2175
2176 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2177 info->control.rates[i].idx = -1;
2178 info->control.rates[i].flags = 0;
2179 info->control.rates[i].count = 0;
2180 }
2181
2182 if (rate_flags & IEEE80211_TX_RC_MCS) {
2183 info->control.rates[0].idx = rate;
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002184 } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
2185 ieee80211_rate_set_vht(info->control.rates, vht_mcs,
2186 vht_nss);
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002187 } else {
2188 for (i = 0; i < sband->n_bitrates; i++) {
2189 if (rate * 5 != sband->bitrates[i].bitrate)
2190 continue;
2191
2192 info->control.rates[0].idx = i;
2193 break;
2194 }
2195 }
2196
Felix Fietkau07310a62016-03-02 15:54:51 +01002197 if (info->control.rates[0].idx < 0)
2198 info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
2199
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002200 info->control.rates[0].flags = rate_flags;
2201 info->control.rates[0].count = min_t(u8, rate_retries + 1,
2202 local->hw.max_rate_tries);
2203 }
2204
Johannes Berg73b9f032011-10-07 14:01:26 +02002205 return true;
2206}
2207
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +00002208netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
2209 struct net_device *dev)
Johannes Berge2ebc742007-07-27 15:43:22 +02002210{
2211 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg55de9082012-07-26 17:24:39 +02002212 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002213 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002214 struct ieee80211_hdr *hdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002215 struct ieee80211_sub_if_data *tmp_sdata, *sdata;
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002216 struct cfg80211_chan_def *chandef;
Andy Green9b8a74e2007-07-27 15:43:24 +02002217 u16 len_rthdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002218 int hdrlen;
Johannes Berge2ebc742007-07-27 15:43:22 +02002219
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002220 memset(info, 0, sizeof(*info));
2221 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2222 IEEE80211_TX_CTL_INJECTED;
Andy Green9b8a74e2007-07-27 15:43:24 +02002223
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002224 /* Sanity-check and process the injection radiotap header */
2225 if (!ieee80211_parse_tx_radiotap(skb, dev))
2226 goto fail;
Andy Green9b8a74e2007-07-27 15:43:24 +02002227
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002228 /* we now know there is a radiotap header with a length we can use */
Andy Green9b8a74e2007-07-27 15:43:24 +02002229 len_rthdr = ieee80211_get_radiotap_len(skb->data);
2230
Johannes Berge2ebc742007-07-27 15:43:22 +02002231 /*
2232 * fix up the pointers accounting for the radiotap
2233 * header still being in there. We are being given
2234 * a precooked IEEE80211 header so no need for
2235 * normal processing
2236 */
Andy Green9b8a74e2007-07-27 15:43:24 +02002237 skb_set_mac_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02002238 /*
Andy Green9b8a74e2007-07-27 15:43:24 +02002239 * these are just fixed to the end of the rt area since we
2240 * don't have any better information and at this point, nobody cares
Johannes Berge2ebc742007-07-27 15:43:22 +02002241 */
Andy Green9b8a74e2007-07-27 15:43:24 +02002242 skb_set_network_header(skb, len_rthdr);
2243 skb_set_transport_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02002244
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002245 if (skb->len < len_rthdr + 2)
2246 goto fail;
2247
2248 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2249 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2250
2251 if (skb->len < len_rthdr + hdrlen)
2252 goto fail;
2253
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002254 /*
2255 * Initialize skb->protocol if the injected frame is a data frame
2256 * carrying a rfc1042 header
2257 */
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002258 if (ieee80211_is_data(hdr->frame_control) &&
2259 skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2260 u8 *payload = (u8 *)hdr + hdrlen;
2261
Joe Perchesb203ca32012-05-08 18:56:52 +00002262 if (ether_addr_equal(payload, rfc1042_header))
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002263 skb->protocol = cpu_to_be16((payload[6] << 8) |
2264 payload[7]);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002265 }
2266
Fredrik Olofsson753ffad2019-11-19 14:34:51 +01002267 /*
2268 * Initialize skb->priority for QoS frames. This is put in the TID field
2269 * of the frame before passing it to the driver.
2270 */
2271 if (ieee80211_is_data_qos(hdr->frame_control)) {
2272 u8 *p = ieee80211_get_qos_ctl(hdr);
2273 skb->priority = *p & IEEE80211_QOS_CTL_TAG1D_MASK;
2274 }
2275
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002276 rcu_read_lock();
2277
2278 /*
2279 * We process outgoing injected frames that have a local address
2280 * we handle as though they are non-injected frames.
2281 * This code here isn't entirely correct, the local MAC address
2282 * isn't always enough to find the interface to use; for proper
2283 * VLAN/WDS support we will need a different mechanism (which
2284 * likely isn't going to be monitor interfaces).
Johannes Bergb226a822019-11-22 12:42:42 +01002285 *
2286 * This is necessary, for example, for old hostapd versions that
2287 * don't use nl80211-based management TX/RX.
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002288 */
2289 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2290
2291 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2292 if (!ieee80211_sdata_running(tmp_sdata))
2293 continue;
2294 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2295 tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2296 tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
2297 continue;
Joe Perchesb203ca32012-05-08 18:56:52 +00002298 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002299 sdata = tmp_sdata;
2300 break;
2301 }
2302 }
Johannes Berg7351c6b2009-11-19 01:08:30 +01002303
Johannes Berg55de9082012-07-26 17:24:39 +02002304 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2305 if (!chanctx_conf) {
2306 tmp_sdata = rcu_dereference(local->monitor_sdata);
2307 if (tmp_sdata)
2308 chanctx_conf =
2309 rcu_dereference(tmp_sdata->vif.chanctx_conf);
2310 }
Johannes Berg55de9082012-07-26 17:24:39 +02002311
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002312 if (chanctx_conf)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002313 chandef = &chanctx_conf->def;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002314 else if (!local->use_chanctx)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002315 chandef = &local->_oper_chandef;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002316 else
2317 goto fail_rcu;
Johannes Berg55de9082012-07-26 17:24:39 +02002318
2319 /*
2320 * Frame injection is not allowed if beaconing is not allowed
2321 * or if we need radar detection. Beaconing is usually not allowed when
2322 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2323 * Passive scan is also used in world regulatory domains where
2324 * your country is not known and as such it should be treated as
2325 * NO TX unless the channel is explicitly allowed in which case
2326 * your current regulatory domain would not have the passive scan
2327 * flag.
2328 *
2329 * Since AP mode uses monitor interfaces to inject/TX management
2330 * frames we can make AP mode the exception to this rule once it
2331 * supports radar detection as its implementation can deal with
2332 * radar detection by itself. We can do that later by adding a
2333 * monitor flag interfaces used for AP support.
2334 */
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002335 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2336 sdata->vif.type))
Johannes Berg55de9082012-07-26 17:24:39 +02002337 goto fail_rcu;
2338
Johannes Berg73c4e192014-11-09 18:50:09 +02002339 info->band = chandef->chan->band;
Lorenzo Bianconi109843b02016-02-19 12:18:01 +01002340
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002341 /* remove the injection radiotap header */
2342 skb_pull(skb, len_rthdr);
Lorenzo Bianconi109843b02016-02-19 12:18:01 +01002343
Mathy Vanhoef08aca292020-07-23 14:01:52 +04002344 ieee80211_xmit(sdata, NULL, skb);
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002345 rcu_read_unlock();
2346
Johannes Berge2ebc742007-07-27 15:43:22 +02002347 return NETDEV_TX_OK;
Andy Green9b8a74e2007-07-27 15:43:24 +02002348
Johannes Berg55de9082012-07-26 17:24:39 +02002349fail_rcu:
2350 rcu_read_unlock();
Andy Green9b8a74e2007-07-27 15:43:24 +02002351fail:
2352 dev_kfree_skb(skb);
2353 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
Johannes Berge2ebc742007-07-27 15:43:22 +02002354}
2355
Johannes Berg97ffe752015-03-21 09:13:45 +01002356static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002357{
Johannes Berg97ffe752015-03-21 09:13:45 +01002358 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002359
Johannes Berg97ffe752015-03-21 09:13:45 +01002360 return ethertype == ETH_P_TDLS &&
2361 skb->len > 14 &&
2362 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2363}
2364
John Crispin50ff4772019-11-25 11:04:37 +01002365int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2366 struct sk_buff *skb,
2367 struct sta_info **sta_out)
Johannes Berg97ffe752015-03-21 09:13:45 +01002368{
2369 struct sta_info *sta;
2370
2371 switch (sdata->vif.type) {
2372 case NL80211_IFTYPE_AP_VLAN:
2373 sta = rcu_dereference(sdata->u.vlan.sta);
2374 if (sta) {
2375 *sta_out = sta;
2376 return 0;
2377 } else if (sdata->wdev.use_4addr) {
2378 return -ENOLINK;
2379 }
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05002380 fallthrough;
Johannes Berg97ffe752015-03-21 09:13:45 +01002381 case NL80211_IFTYPE_AP:
2382 case NL80211_IFTYPE_OCB:
2383 case NL80211_IFTYPE_ADHOC:
2384 if (is_multicast_ether_addr(skb->data)) {
2385 *sta_out = ERR_PTR(-ENOENT);
2386 return 0;
2387 }
2388 sta = sta_info_get_bss(sdata, skb->data);
2389 break;
2390 case NL80211_IFTYPE_WDS:
2391 sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
2392 break;
2393#ifdef CONFIG_MAC80211_MESH
2394 case NL80211_IFTYPE_MESH_POINT:
2395 /* determined much later */
2396 *sta_out = NULL;
2397 return 0;
2398#endif
2399 case NL80211_IFTYPE_STATION:
2400 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2401 sta = sta_info_get(sdata, skb->data);
Johannes Berg11d62ca2016-09-13 08:28:22 +02002402 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2403 if (test_sta_flag(sta,
2404 WLAN_STA_TDLS_PEER_AUTH)) {
Johannes Berg97ffe752015-03-21 09:13:45 +01002405 *sta_out = sta;
2406 return 0;
2407 }
2408
2409 /*
2410 * TDLS link during setup - throw out frames to
2411 * peer. Allow TDLS-setup frames to unauthorized
2412 * peers for the special case of a link teardown
2413 * after a TDLS sta is removed due to being
2414 * unreachable.
2415 */
Johannes Berg11d62ca2016-09-13 08:28:22 +02002416 if (!ieee80211_is_tdls_setup(skb))
Johannes Berg97ffe752015-03-21 09:13:45 +01002417 return -EINVAL;
2418 }
2419
2420 }
2421
2422 sta = sta_info_get(sdata, sdata->u.mgd.bssid);
2423 if (!sta)
2424 return -ENOLINK;
2425 break;
2426 default:
2427 return -EINVAL;
2428 }
2429
2430 *sta_out = sta ?: ERR_PTR(-ENOENT);
2431 return 0;
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002432}
2433
Markus Theila7528192020-05-27 18:03:34 +02002434static u16 ieee80211_store_ack_skb(struct ieee80211_local *local,
John Crispin5d8983c2019-10-29 10:13:02 +01002435 struct sk_buff *skb,
Markus Theila7528192020-05-27 18:03:34 +02002436 u32 *info_flags,
2437 u64 *cookie)
John Crispin5d8983c2019-10-29 10:13:02 +01002438{
Markus Theila7528192020-05-27 18:03:34 +02002439 struct sk_buff *ack_skb;
John Crispin5d8983c2019-10-29 10:13:02 +01002440 u16 info_id = 0;
2441
Markus Theila7528192020-05-27 18:03:34 +02002442 if (skb->sk)
2443 ack_skb = skb_clone_sk(skb);
2444 else
2445 ack_skb = skb_clone(skb, GFP_ATOMIC);
2446
John Crispin5d8983c2019-10-29 10:13:02 +01002447 if (ack_skb) {
2448 unsigned long flags;
2449 int id;
2450
2451 spin_lock_irqsave(&local->ack_status_lock, flags);
2452 id = idr_alloc(&local->ack_status_frames, ack_skb,
Johannes Bergf2b18ba2020-01-15 12:25:50 +01002453 1, 0x2000, GFP_ATOMIC);
John Crispin5d8983c2019-10-29 10:13:02 +01002454 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2455
2456 if (id >= 0) {
2457 info_id = id;
2458 *info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
Markus Theila7528192020-05-27 18:03:34 +02002459 if (cookie) {
2460 *cookie = ieee80211_mgmt_tx_cookie(local);
2461 IEEE80211_SKB_CB(ack_skb)->ack.cookie = *cookie;
2462 }
John Crispin5d8983c2019-10-29 10:13:02 +01002463 } else {
2464 kfree_skb(ack_skb);
2465 }
2466 }
2467
2468 return info_id;
2469}
2470
Johannes Berge2ebc742007-07-27 15:43:22 +02002471/**
Johannes Berg4c9451e2014-11-09 18:50:10 +02002472 * ieee80211_build_hdr - build 802.11 header in the given frame
2473 * @sdata: virtual interface to build the header for
2474 * @skb: the skb to build the header in
Liad Kaufman24d342c2014-11-09 18:50:07 +02002475 * @info_flags: skb flags to set
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002476 * @ctrl_flags: info control flags to set
Johannes Berge2ebc742007-07-27 15:43:22 +02002477 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002478 * This function takes the skb with 802.3 header and reformats the header to
2479 * the appropriate IEEE 802.11 header based on which interface the packet is
2480 * being transmitted on.
Johannes Berge2ebc742007-07-27 15:43:22 +02002481 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002482 * Note that this function also takes care of the TX status request and
2483 * potential unsharing of the SKB - this needs to be interleaved with the
2484 * header building.
2485 *
2486 * The function requires the read-side RCU lock held
2487 *
2488 * Returns: the (possibly reallocated) skb or an ERR_PTR() code
Johannes Berge2ebc742007-07-27 15:43:22 +02002489 */
Johannes Berg4c9451e2014-11-09 18:50:10 +02002490static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01002491 struct sk_buff *skb, u32 info_flags,
Markus Theila7528192020-05-27 18:03:34 +02002492 struct sta_info *sta, u32 ctrl_flags,
2493 u64 *cookie)
Johannes Berge2ebc742007-07-27 15:43:22 +02002494{
Johannes Berg133b8222008-09-16 14:18:59 +02002495 struct ieee80211_local *local = sdata->local;
Felix Fietkau489ee912010-12-18 19:30:48 +01002496 struct ieee80211_tx_info *info;
Johannes Bergdcf33962012-07-30 15:11:56 +02002497 int head_need;
Harvey Harrison065e96052008-06-22 16:45:27 -07002498 u16 ethertype, hdrlen, meshhdrlen = 0;
2499 __le16 fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002500 struct ieee80211_hdr hdr;
Wey-Yi Guyff67bb82010-08-21 07:23:29 -07002501 struct ieee80211s_hdr mesh_hdr __maybe_unused;
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002502 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02002503 const u8 *encaps_data;
2504 int encaps_len, skip_header_bytes;
Johannes Berg97ffe752015-03-21 09:13:45 +01002505 bool wme_sta = false, authorized = false;
2506 bool tdls_peer;
Johannes Berga729cff2011-11-06 14:13:34 +01002507 bool multicast;
Johannes Berga729cff2011-11-06 14:13:34 +01002508 u16 info_id = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002509 struct ieee80211_chanctx_conf *chanctx_conf;
2510 struct ieee80211_sub_if_data *ap_sdata;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002511 enum nl80211_band band;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002512 int ret;
Johannes Berge2ebc742007-07-27 15:43:22 +02002513
Johannes Berg97ffe752015-03-21 09:13:45 +01002514 if (IS_ERR(sta))
2515 sta = NULL;
2516
Julius Niedworok276d9e82019-03-28 21:01:06 +01002517#ifdef CONFIG_MAC80211_DEBUGFS
2518 if (local->force_tx_status)
2519 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2520#endif
2521
Johannes Berge2ebc742007-07-27 15:43:22 +02002522 /* convert Ethernet header to proper 802.11 header (based on
2523 * operation mode) */
2524 ethertype = (skb->data[12] << 8) | skb->data[13];
Harvey Harrison065e96052008-06-22 16:45:27 -07002525 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
Johannes Berge2ebc742007-07-27 15:43:22 +02002526
Johannes Berg51fb61e2007-12-19 01:31:27 +01002527 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002528 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg97ffe752015-03-21 09:13:45 +01002529 if (sdata->wdev.use_4addr) {
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002530 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2531 /* RA TA DA SA */
2532 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002533 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002534 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2535 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2536 hdrlen = 30;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002537 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Johannes Berga74a8c82014-07-22 14:50:47 +02002538 wme_sta = sta->sta.wme;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002539 }
Johannes Berg55de9082012-07-26 17:24:39 +02002540 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2541 u.ap);
2542 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002543 if (!chanctx_conf) {
2544 ret = -ENOTCONN;
2545 goto free;
2546 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002547 band = chanctx_conf->def.chan->band;
Johannes Berg97ffe752015-03-21 09:13:45 +01002548 if (sdata->wdev.use_4addr)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002549 break;
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05002550 fallthrough;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002551 case NL80211_IFTYPE_AP:
Arnd Bergmannfe801232013-01-25 14:14:33 +00002552 if (sdata->vif.type == NL80211_IFTYPE_AP)
2553 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002554 if (!chanctx_conf) {
2555 ret = -ENOTCONN;
2556 goto free;
2557 }
Harvey Harrison065e96052008-06-22 16:45:27 -07002558 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
Johannes Berge2ebc742007-07-27 15:43:22 +02002559 /* DA BSSID SA */
2560 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002561 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002562 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2563 hdrlen = 24;
Johannes Berg4bf88532012-11-09 11:39:59 +01002564 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002565 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002566 case NL80211_IFTYPE_WDS:
Harvey Harrison065e96052008-06-22 16:45:27 -07002567 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
Johannes Berge2ebc742007-07-27 15:43:22 +02002568 /* RA TA DA SA */
2569 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002570 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002571 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2572 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2573 hdrlen = 30;
Johannes Berg55de9082012-07-26 17:24:39 +02002574 /*
2575 * This is the exception! WDS style interfaces are prohibited
2576 * when channel contexts are in used so this must be valid
2577 */
Karl Beldan675a0b02013-03-25 16:26:57 +01002578 band = local->hw.conf.chandef.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002579 break;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002580#ifdef CONFIG_MAC80211_MESH
Johannes Berg05c914f2008-09-11 00:01:58 +02002581 case NL80211_IFTYPE_MESH_POINT:
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002582 if (!is_multicast_ether_addr(skb->data)) {
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002583 struct sta_info *next_hop;
2584 bool mpp_lookup = true;
2585
Johannes Bergbf7cd942013-02-15 14:40:31 +01002586 mpath = mesh_path_lookup(sdata, skb->data);
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002587 if (mpath) {
2588 mpp_lookup = false;
2589 next_hop = rcu_dereference(mpath->next_hop);
2590 if (!next_hop ||
2591 !(mpath->flags & (MESH_PATH_ACTIVE |
2592 MESH_PATH_RESOLVING)))
2593 mpp_lookup = true;
2594 }
2595
Henning Roggeab1c7902016-02-03 13:58:37 +01002596 if (mpp_lookup) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002597 mppath = mpp_path_lookup(sdata, skb->data);
Henning Roggeab1c7902016-02-03 13:58:37 +01002598 if (mppath)
2599 mppath->exp_time = jiffies;
2600 }
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002601
2602 if (mppath && mpath)
Bob Copeland2bdaf382016-02-28 20:03:56 -05002603 mesh_path_del(sdata, mpath->dst);
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002604 }
YanBo79617de2008-09-22 13:30:32 +08002605
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002606 /*
Joel A Fernandes9d525012011-01-10 00:44:23 -06002607 * Use address extension if it is a packet from
2608 * another interface or if we know the destination
2609 * is being proxied by a portal (i.e. portal address
2610 * differs from proxied address)
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002611 */
Joe Perchesb203ca32012-05-08 18:56:52 +00002612 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2613 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
Javier Cardona3c5772a2009-08-10 12:15:48 -07002614 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2615 skb->data, skb->data + ETH_ALEN);
Johannes Bergbf7cd942013-02-15 14:40:31 +01002616 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2617 NULL, NULL);
YanBo79617de2008-09-22 13:30:32 +08002618 } else {
Thomas Pedersen27f01122012-08-20 11:28:25 -07002619 /* DS -> MBSS (802.11-2012 13.11.3.3).
2620 * For unicast with unknown forwarding information,
2621 * destination might be in the MBSS or if that fails
2622 * forwarded to another mesh gate. In either case
2623 * resolution will be handled in ieee80211_xmit(), so
2624 * leave the original DA. This also works for mcast */
2625 const u8 *mesh_da = skb->data;
YanBo79617de2008-09-22 13:30:32 +08002626
Thomas Pedersen27f01122012-08-20 11:28:25 -07002627 if (mppath)
2628 mesh_da = mppath->mpp;
2629 else if (mpath)
2630 mesh_da = mpath->dst;
Thomas Pedersen27f01122012-08-20 11:28:25 -07002631
Javier Cardona3c5772a2009-08-10 12:15:48 -07002632 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
Johannes Berg47846c92009-11-25 17:46:19 +01002633 mesh_da, sdata->vif.addr);
Thomas Pedersen27f01122012-08-20 11:28:25 -07002634 if (is_multicast_ether_addr(mesh_da))
2635 /* DA TA mSA AE:SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002636 meshhdrlen = ieee80211_new_mesh_header(
2637 sdata, &mesh_hdr,
2638 skb->data + ETH_ALEN, NULL);
Javier Cardona3c5772a2009-08-10 12:15:48 -07002639 else
Thomas Pedersen27f01122012-08-20 11:28:25 -07002640 /* RA TA mDA mSA AE:DA SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002641 meshhdrlen = ieee80211_new_mesh_header(
2642 sdata, &mesh_hdr, skb->data,
2643 skb->data + ETH_ALEN);
YanBo79617de2008-09-22 13:30:32 +08002644
YanBo79617de2008-09-22 13:30:32 +08002645 }
Johannes Berg55de9082012-07-26 17:24:39 +02002646 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002647 if (!chanctx_conf) {
2648 ret = -ENOTCONN;
2649 goto free;
2650 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002651 band = chanctx_conf->def.chan->band;
Rajkumar Manoharan8828f812019-04-11 13:47:26 -07002652
2653 /* For injected frames, fill RA right away as nexthop lookup
2654 * will be skipped.
2655 */
2656 if ((ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP) &&
2657 is_zero_ether_addr(hdr.addr1))
2658 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002659 break;
2660#endif
Johannes Berg05c914f2008-09-11 00:01:58 +02002661 case NL80211_IFTYPE_STATION:
Johannes Berg97ffe752015-03-21 09:13:45 +01002662 /* we already did checks when looking up the RA STA */
2663 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002664
Johannes Berg97ffe752015-03-21 09:13:45 +01002665 if (tdls_peer) {
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002666 /* DA SA BSSID */
2667 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2668 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2669 memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2670 hdrlen = 24;
2671 } else if (sdata->u.mgd.use_4addr &&
2672 cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2673 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2674 IEEE80211_FCTL_TODS);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002675 /* RA TA DA SA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002676 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002677 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002678 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2679 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2680 hdrlen = 30;
2681 } else {
2682 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2683 /* BSSID SA DA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002684 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002685 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2686 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2687 hdrlen = 24;
2688 }
Johannes Berg55de9082012-07-26 17:24:39 +02002689 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002690 if (!chanctx_conf) {
2691 ret = -ENOTCONN;
2692 goto free;
2693 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002694 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002695 break;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002696 case NL80211_IFTYPE_OCB:
2697 /* DA SA BSSID */
2698 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2699 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2700 eth_broadcast_addr(hdr.addr3);
2701 hdrlen = 24;
2702 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002703 if (!chanctx_conf) {
2704 ret = -ENOTCONN;
2705 goto free;
2706 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002707 band = chanctx_conf->def.chan->band;
2708 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002709 case NL80211_IFTYPE_ADHOC:
Johannes Berge2ebc742007-07-27 15:43:22 +02002710 /* DA SA BSSID */
2711 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2712 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01002713 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002714 hdrlen = 24;
Johannes Berg55de9082012-07-26 17:24:39 +02002715 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002716 if (!chanctx_conf) {
2717 ret = -ENOTCONN;
2718 goto free;
2719 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002720 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002721 break;
2722 default:
Johannes Berg4c9451e2014-11-09 18:50:10 +02002723 ret = -EINVAL;
2724 goto free;
Johannes Berge2ebc742007-07-27 15:43:22 +02002725 }
2726
Johannes Berga729cff2011-11-06 14:13:34 +01002727 multicast = is_multicast_ether_addr(hdr.addr1);
Johannes Berge2ebc742007-07-27 15:43:22 +02002728
Johannes Berg97ffe752015-03-21 09:13:45 +01002729 /* sta is always NULL for mesh */
2730 if (sta) {
2731 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2732 wme_sta = sta->sta.wme;
2733 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2734 /* For mesh, the use of the QoS header is mandatory */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002735 wme_sta = true;
Johannes Berge2ebc742007-07-27 15:43:22 +02002736 }
2737
Johannes Berg527871d2015-03-21 08:09:55 +01002738 /* receiver does QoS (which also means we do) use it */
2739 if (wme_sta) {
Harvey Harrison065e96052008-06-22 16:45:27 -07002740 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002741 hdrlen += 2;
2742 }
2743
2744 /*
Johannes Berg238814f2008-01-28 17:19:37 +01002745 * Drop unicast frames to unauthorised stations unless they are
2746 * EAPOL frames from the local station.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002747 */
Johannes Berg55182e42011-10-12 17:28:21 +02002748 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002749 (sdata->vif.type != NL80211_IFTYPE_OCB) &&
Sergey Ryazanova6ececf2013-08-30 01:35:09 +04002750 !multicast && !authorized &&
Johannes Berg55182e42011-10-12 17:28:21 +02002751 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002752 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002753#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002754 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
Johannes Berg4c9451e2014-11-09 18:50:10 +02002755 sdata->name, hdr.addr1);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002756#endif
2757
2758 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2759
Johannes Berg4c9451e2014-11-09 18:50:10 +02002760 ret = -EPERM;
2761 goto free;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002762 }
2763
Markus Theila7528192020-05-27 18:03:34 +02002764 if (unlikely(!multicast && ((skb->sk &&
2765 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS) ||
2766 ctrl_flags & IEEE80211_TX_CTL_REQ_TX_STATUS)))
2767 info_id = ieee80211_store_ack_skb(local, skb, &info_flags,
2768 cookie);
Johannes Berga729cff2011-11-06 14:13:34 +01002769
Helmut Schaa7e244702010-12-02 18:44:09 +01002770 /*
2771 * If the skb is shared we need to obtain our own copy.
2772 */
2773 if (skb_shared(skb)) {
Johannes Berga729cff2011-11-06 14:13:34 +01002774 struct sk_buff *tmp_skb = skb;
2775
2776 /* can't happen -- skb is a clone if info_id != 0 */
2777 WARN_ON(info_id);
2778
Felix Fietkauf8a0a782010-12-18 19:30:50 +01002779 skb = skb_clone(skb, GFP_ATOMIC);
Helmut Schaa7e244702010-12-02 18:44:09 +01002780 kfree_skb(tmp_skb);
2781
Johannes Berg4c9451e2014-11-09 18:50:10 +02002782 if (!skb) {
2783 ret = -ENOMEM;
2784 goto free;
2785 }
Helmut Schaa7e244702010-12-02 18:44:09 +01002786 }
2787
Harvey Harrison065e96052008-06-22 16:45:27 -07002788 hdr.frame_control = fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002789 hdr.duration_id = 0;
2790 hdr.seq_ctrl = 0;
2791
2792 skip_header_bytes = ETH_HLEN;
2793 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2794 encaps_data = bridge_tunnel_header;
2795 encaps_len = sizeof(bridge_tunnel_header);
2796 skip_header_bytes -= 2;
Simon Hormane5c5d222013-03-28 13:38:25 +09002797 } else if (ethertype >= ETH_P_802_3_MIN) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002798 encaps_data = rfc1042_header;
2799 encaps_len = sizeof(rfc1042_header);
2800 skip_header_bytes -= 2;
2801 } else {
2802 encaps_data = NULL;
2803 encaps_len = 0;
2804 }
2805
2806 skb_pull(skb, skip_header_bytes);
Johannes Berg23c07522008-05-29 10:38:53 +02002807 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002808
Johannes Berg23c07522008-05-29 10:38:53 +02002809 /*
2810 * So we need to modify the skb header and hence need a copy of
2811 * that. The head_need variable above doesn't, so far, include
2812 * the needed header space that we don't need right away. If we
2813 * can, then we don't reallocate right now but only after the
2814 * frame arrives at the master device (if it does...)
2815 *
2816 * If we cannot, however, then we will reallocate to include all
2817 * the ever needed space. Also, if we need to reallocate it anyway,
2818 * make it big enough for everything we may ever need.
2819 */
Johannes Berge2ebc742007-07-27 15:43:22 +02002820
David S. Miller3a5be7d2008-06-18 01:19:51 -07002821 if (head_need > 0 || skb_cloned(skb)) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002822 head_need += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02002823 head_need += local->tx_headroom;
2824 head_need = max_t(int, 0, head_need);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002825 if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
2826 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg1c963be2012-11-07 14:02:30 +01002827 skb = NULL;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002828 return ERR_PTR(-ENOMEM);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002829 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002830 }
2831
Felix Fietkaueae44302016-07-13 11:00:02 +02002832 if (encaps_data)
Johannes Berge2ebc742007-07-27 15:43:22 +02002833 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002834
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002835#ifdef CONFIG_MAC80211_MESH
Felix Fietkaueae44302016-07-13 11:00:02 +02002836 if (meshhdrlen > 0)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002837 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002838#endif
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002839
Harvey Harrison065e96052008-06-22 16:45:27 -07002840 if (ieee80211_is_data_qos(fc)) {
Johannes Bergc29b9b92007-09-14 11:10:24 -04002841 __le16 *qos_control;
2842
Johannes Bergd58ff352017-06-16 14:29:23 +02002843 qos_control = skb_push(skb, 2);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002844 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2845 /*
2846 * Maybe we could actually set some fields here, for now just
2847 * initialise to zero to indicate no special operation.
2848 */
2849 *qos_control = 0;
2850 } else
2851 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2852
Zhang Shengjud57a5442016-03-03 01:16:56 +00002853 skb_reset_mac_header(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002854
Felix Fietkau489ee912010-12-18 19:30:48 +01002855 info = IEEE80211_SKB_CB(skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002856 memset(info, 0, sizeof(*info));
2857
Johannes Berga729cff2011-11-06 14:13:34 +01002858 info->flags = info_flags;
2859 info->ack_frame_id = info_id;
Johannes Berg73c4e192014-11-09 18:50:09 +02002860 info->band = band;
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002861 info->control.flags = ctrl_flags;
Johannes Berga729cff2011-11-06 14:13:34 +01002862
Johannes Berg4c9451e2014-11-09 18:50:10 +02002863 return skb;
2864 free:
2865 kfree_skb(skb);
2866 return ERR_PTR(ret);
Johannes Berge2ebc742007-07-27 15:43:22 +02002867}
2868
Johannes Berg17c18bf2015-03-21 15:25:43 +01002869/*
2870 * fast-xmit overview
2871 *
2872 * The core idea of this fast-xmit is to remove per-packet checks by checking
2873 * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2874 * checks that are needed to get the sta->fast_tx pointer assigned, after which
2875 * much less work can be done per packet. For example, fragmentation must be
2876 * disabled or the fast_tx pointer will not be set. All the conditions are seen
2877 * in the code here.
2878 *
2879 * Once assigned, the fast_tx data structure also caches the per-packet 802.11
2880 * header and other data to aid packet processing in ieee80211_xmit_fast().
2881 *
2882 * The most difficult part of this is that when any of these assumptions
2883 * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
2884 * ieee80211_check_fast_xmit() or friends) is required to reset the data,
2885 * since the per-packet code no longer checks the conditions. This is reflected
2886 * by the calls to these functions throughout the rest of the code, and must be
2887 * maintained if any of the TX path checks change.
2888 */
2889
2890void ieee80211_check_fast_xmit(struct sta_info *sta)
2891{
2892 struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
2893 struct ieee80211_local *local = sta->local;
2894 struct ieee80211_sub_if_data *sdata = sta->sdata;
2895 struct ieee80211_hdr *hdr = (void *)build.hdr;
2896 struct ieee80211_chanctx_conf *chanctx_conf;
2897 __le16 fc;
2898
Johannes Berg30686bf2015-06-02 21:39:54 +02002899 if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002900 return;
2901
2902 /* Locking here protects both the pointer itself, and against concurrent
2903 * invocations winning data access races to, e.g., the key pointer that
2904 * is used.
2905 * Without it, the invocation of this function right after the key
2906 * pointer changes wouldn't be sufficient, as another CPU could access
2907 * the pointer, then stall, and then do the cache update after the CPU
2908 * that invalidated the key.
2909 * With the locking, such scenarios cannot happen as the check for the
2910 * key and the fast-tx assignment are done atomically, so the CPU that
2911 * modifies the key will either wait or other one will see the key
2912 * cleared/changed already.
2913 */
2914 spin_lock_bh(&sta->lock);
Johannes Berg30686bf2015-06-02 21:39:54 +02002915 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
2916 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
Johannes Berg17c18bf2015-03-21 15:25:43 +01002917 sdata->vif.type == NL80211_IFTYPE_STATION)
2918 goto out;
2919
2920 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2921 goto out;
2922
2923 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
2924 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
Felix Fietkauf7418bc2015-09-24 14:59:49 +02002925 test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
2926 test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002927 goto out;
2928
2929 if (sdata->noack_map)
2930 goto out;
2931
2932 /* fast-xmit doesn't handle fragmentation at all */
Johannes Berg725b8122015-04-10 14:02:08 +02002933 if (local->hw.wiphy->frag_threshold != (u32)-1 &&
Sara Sharonf3fe4e92016-10-18 23:12:11 +03002934 !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002935 goto out;
2936
2937 rcu_read_lock();
2938 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2939 if (!chanctx_conf) {
2940 rcu_read_unlock();
2941 goto out;
2942 }
2943 build.band = chanctx_conf->def.chan->band;
2944 rcu_read_unlock();
2945
2946 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2947
2948 switch (sdata->vif.type) {
Johannes Berg3ffd8842015-04-14 10:28:37 +02002949 case NL80211_IFTYPE_ADHOC:
2950 /* DA SA BSSID */
2951 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2952 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2953 memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
2954 build.hdr_len = 24;
2955 break;
Johannes Berg17c18bf2015-03-21 15:25:43 +01002956 case NL80211_IFTYPE_STATION:
2957 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2958 /* DA SA BSSID */
2959 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2960 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2961 memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
2962 build.hdr_len = 24;
2963 break;
2964 }
2965
2966 if (sdata->u.mgd.use_4addr) {
2967 /* non-regular ethertype cannot use the fastpath */
2968 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2969 IEEE80211_FCTL_TODS);
2970 /* RA TA DA SA */
2971 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2972 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2973 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2974 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2975 build.hdr_len = 30;
2976 break;
2977 }
2978 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2979 /* BSSID SA DA */
2980 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2981 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2982 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2983 build.hdr_len = 24;
2984 break;
2985 case NL80211_IFTYPE_AP_VLAN:
2986 if (sdata->wdev.use_4addr) {
2987 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2988 IEEE80211_FCTL_TODS);
2989 /* RA TA DA SA */
2990 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
2991 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2992 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2993 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2994 build.hdr_len = 30;
2995 break;
2996 }
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05002997 fallthrough;
Johannes Berg17c18bf2015-03-21 15:25:43 +01002998 case NL80211_IFTYPE_AP:
2999 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
3000 /* DA BSSID SA */
3001 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
3002 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
3003 build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
3004 build.hdr_len = 24;
3005 break;
3006 default:
3007 /* not handled on fast-xmit */
3008 goto out;
3009 }
3010
3011 if (sta->sta.wme) {
3012 build.hdr_len += 2;
3013 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3014 }
3015
3016 /* We store the key here so there's no point in using rcu_dereference()
3017 * but that's fine because the code that changes the pointers will call
3018 * this function after doing so. For a single CPU that would be enough,
3019 * for multiple see the comment above.
3020 */
3021 build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
3022 if (!build.key)
3023 build.key = rcu_access_pointer(sdata->default_unicast_key);
3024 if (build.key) {
Johannes Berge495c242015-04-10 14:03:17 +02003025 bool gen_iv, iv_spc, mmic;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003026
3027 gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
3028 iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
David Spinadel9de18d82017-12-01 13:50:52 +02003029 mmic = build.key->conf.flags &
3030 (IEEE80211_KEY_FLAG_GENERATE_MMIC |
3031 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003032
3033 /* don't handle software crypto */
3034 if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
3035 goto out;
3036
Alexander Wetzel62872a92018-08-31 15:00:38 +02003037 /* Key is being removed */
3038 if (build.key->flags & KEY_FLAG_TAINTED)
3039 goto out;
3040
Johannes Berg17c18bf2015-03-21 15:25:43 +01003041 switch (build.key->conf.cipher) {
3042 case WLAN_CIPHER_SUITE_CCMP:
3043 case WLAN_CIPHER_SUITE_CCMP_256:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003044 if (gen_iv)
Johannes Berg17c18bf2015-03-21 15:25:43 +01003045 build.pn_offs = build.hdr_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003046 if (gen_iv || iv_spc)
3047 build.hdr_len += IEEE80211_CCMP_HDR_LEN;
3048 break;
3049 case WLAN_CIPHER_SUITE_GCMP:
3050 case WLAN_CIPHER_SUITE_GCMP_256:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003051 if (gen_iv)
Johannes Berg17c18bf2015-03-21 15:25:43 +01003052 build.pn_offs = build.hdr_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003053 if (gen_iv || iv_spc)
3054 build.hdr_len += IEEE80211_GCMP_HDR_LEN;
3055 break;
Johannes Berge495c242015-04-10 14:03:17 +02003056 case WLAN_CIPHER_SUITE_TKIP:
3057 /* cannot handle MMIC or IV generation in xmit-fast */
3058 if (mmic || gen_iv)
3059 goto out;
3060 if (iv_spc)
3061 build.hdr_len += IEEE80211_TKIP_IV_LEN;
3062 break;
3063 case WLAN_CIPHER_SUITE_WEP40:
3064 case WLAN_CIPHER_SUITE_WEP104:
3065 /* cannot handle IV generation in fast-xmit */
3066 if (gen_iv)
3067 goto out;
3068 if (iv_spc)
3069 build.hdr_len += IEEE80211_WEP_IV_LEN;
3070 break;
3071 case WLAN_CIPHER_SUITE_AES_CMAC:
3072 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3073 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3074 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3075 WARN(1,
3076 "management cipher suite 0x%x enabled for data\n",
3077 build.key->conf.cipher);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003078 goto out;
Johannes Berge495c242015-04-10 14:03:17 +02003079 default:
3080 /* we don't know how to generate IVs for this at all */
3081 if (WARN_ON(gen_iv))
3082 goto out;
3083 /* pure hardware keys are OK, of course */
3084 if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
3085 break;
3086 /* cipher scheme might require space allocation */
3087 if (iv_spc &&
3088 build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
3089 goto out;
3090 if (iv_spc)
3091 build.hdr_len += build.key->conf.iv_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003092 }
3093
3094 fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3095 }
3096
3097 hdr->frame_control = fc;
3098
3099 memcpy(build.hdr + build.hdr_len,
3100 rfc1042_header, sizeof(rfc1042_header));
3101 build.hdr_len += sizeof(rfc1042_header);
3102
3103 fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3104 /* if the kmemdup fails, continue w/o fast_tx */
3105 if (!fast_tx)
3106 goto out;
3107
3108 out:
3109 /* we might have raced against another call to this function */
3110 old = rcu_dereference_protected(sta->fast_tx,
3111 lockdep_is_held(&sta->lock));
3112 rcu_assign_pointer(sta->fast_tx, fast_tx);
3113 if (old)
3114 kfree_rcu(old, rcu_head);
3115 spin_unlock_bh(&sta->lock);
3116}
3117
3118void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3119{
3120 struct sta_info *sta;
3121
3122 rcu_read_lock();
3123 list_for_each_entry_rcu(sta, &local->sta_list, list)
3124 ieee80211_check_fast_xmit(sta);
3125 rcu_read_unlock();
3126}
3127
3128void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3129{
3130 struct ieee80211_local *local = sdata->local;
3131 struct sta_info *sta;
3132
3133 rcu_read_lock();
3134
3135 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3136 if (sdata != sta->sdata &&
3137 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3138 continue;
3139 ieee80211_check_fast_xmit(sta);
3140 }
3141
3142 rcu_read_unlock();
3143}
3144
3145void ieee80211_clear_fast_xmit(struct sta_info *sta)
3146{
3147 struct ieee80211_fast_tx *fast_tx;
3148
3149 spin_lock_bh(&sta->lock);
3150 fast_tx = rcu_dereference_protected(sta->fast_tx,
3151 lockdep_is_held(&sta->lock));
3152 RCU_INIT_POINTER(sta->fast_tx, NULL);
3153 spin_unlock_bh(&sta->lock);
3154
3155 if (fast_tx)
3156 kfree_rcu(fast_tx, rcu_head);
3157}
3158
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003159static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
Sara Sharon166ac9d2018-08-29 08:57:02 +02003160 struct sk_buff *skb, int headroom)
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003161{
Sara Sharon166ac9d2018-08-29 08:57:02 +02003162 if (skb_headroom(skb) < headroom) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003163 I802_DEBUG_INC(local->tx_expand_skb_head);
3164
Sara Sharon166ac9d2018-08-29 08:57:02 +02003165 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003166 wiphy_debug(local->hw.wiphy,
3167 "failed to reallocate TX buffer\n");
3168 return false;
3169 }
3170 }
3171
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003172 return true;
3173}
3174
3175static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3176 struct ieee80211_fast_tx *fast_tx,
3177 struct sk_buff *skb)
3178{
3179 struct ieee80211_local *local = sdata->local;
3180 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3181 struct ieee80211_hdr *hdr;
Michael Braun06f2bb12016-10-15 13:28:18 +02003182 struct ethhdr *amsdu_hdr;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003183 int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3184 int subframe_len = skb->len - hdr_len;
3185 void *data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003186 u8 *qc, *h_80211_src, *h_80211_dst;
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003187 const u8 *bssid;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003188
3189 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3190 return false;
3191
3192 if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3193 return true;
3194
Sara Sharon166ac9d2018-08-29 08:57:02 +02003195 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(*amsdu_hdr)))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003196 return false;
3197
Michael Braun06f2bb12016-10-15 13:28:18 +02003198 data = skb_push(skb, sizeof(*amsdu_hdr));
3199 memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003200 hdr = data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003201 amsdu_hdr = data + hdr_len;
3202 /* h_80211_src/dst is addr* field within hdr */
3203 h_80211_src = data + fast_tx->sa_offs;
3204 h_80211_dst = data + fast_tx->da_offs;
3205
3206 amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3207 ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3208 ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
3209
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003210 /* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3211 * fields needs to be changed to BSSID for A-MSDU frames depending
3212 * on FromDS/ToDS values.
3213 */
3214 switch (sdata->vif.type) {
3215 case NL80211_IFTYPE_STATION:
3216 bssid = sdata->u.mgd.bssid;
3217 break;
3218 case NL80211_IFTYPE_AP:
3219 case NL80211_IFTYPE_AP_VLAN:
3220 bssid = sdata->vif.addr;
3221 break;
3222 default:
3223 bssid = NULL;
3224 }
3225
3226 if (bssid && ieee80211_has_fromds(hdr->frame_control))
3227 ether_addr_copy(h_80211_src, bssid);
3228
3229 if (bssid && ieee80211_has_tods(hdr->frame_control))
3230 ether_addr_copy(h_80211_dst, bssid);
3231
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003232 qc = ieee80211_get_qos_ctl(hdr);
3233 *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3234
3235 info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3236
3237 return true;
3238}
3239
3240static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3241 struct sta_info *sta,
3242 struct ieee80211_fast_tx *fast_tx,
3243 struct sk_buff *skb)
3244{
3245 struct ieee80211_local *local = sdata->local;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003246 struct fq *fq = &local->fq;
3247 struct fq_tin *tin;
3248 struct fq_flow *flow;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003249 u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3250 struct ieee80211_txq *txq = sta->sta.txq[tid];
3251 struct txq_info *txqi;
3252 struct sk_buff **frag_tail, *head;
3253 int subframe_len = skb->len - ETH_ALEN;
3254 u8 max_subframes = sta->sta.max_amsdu_subframes;
3255 int max_frags = local->hw.max_tx_fragments;
3256 int max_amsdu_len = sta->sta.max_amsdu_len;
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003257 int orig_truesize;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003258 u32 flow_idx;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003259 __be16 len;
3260 void *data;
3261 bool ret = false;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003262 unsigned int orig_len;
Lorenzo Bianconi66eb02d2018-08-31 01:04:13 +02003263 int n = 2, nfrags, pad = 0;
Sara Sharon166ac9d2018-08-29 08:57:02 +02003264 u16 hdrlen;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003265
3266 if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
3267 return false;
3268
Sara Sharon344f8e02018-12-15 11:03:07 +02003269 if (skb_is_gso(skb))
3270 return false;
3271
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003272 if (!txq)
3273 return false;
3274
3275 txqi = to_txq_info(txq);
3276 if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3277 return false;
3278
3279 if (sta->sta.max_rc_amsdu_len)
3280 max_amsdu_len = min_t(int, max_amsdu_len,
3281 sta->sta.max_rc_amsdu_len);
3282
Sara Sharonedba6bd2018-09-05 08:06:10 +03003283 if (sta->sta.max_tid_amsdu_len[tid])
3284 max_amsdu_len = min_t(int, max_amsdu_len,
3285 sta->sta.max_tid_amsdu_len[tid]);
3286
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003287 flow_idx = fq_flow_idx(fq, skb);
3288
Michal Kaziorfa962b92016-05-19 10:37:49 +02003289 spin_lock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003290
Michal Kaziorfa962b92016-05-19 10:37:49 +02003291 /* TODO: Ideally aggregation should be done on dequeue to remain
3292 * responsive to environment changes.
3293 */
3294
3295 tin = &txqi->tin;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003296 flow = fq_flow_classify(fq, tin, flow_idx, skb,
3297 fq_flow_get_default_func);
Michal Kaziorfa962b92016-05-19 10:37:49 +02003298 head = skb_peek_tail(&flow->queue);
Sara Sharon344f8e02018-12-15 11:03:07 +02003299 if (!head || skb_is_gso(head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003300 goto out;
3301
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003302 orig_truesize = head->truesize;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003303 orig_len = head->len;
3304
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003305 if (skb->len + head->len > max_amsdu_len)
3306 goto out;
3307
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003308 nfrags = 1 + skb_shinfo(skb)->nr_frags;
3309 nfrags += 1 + skb_shinfo(head)->nr_frags;
3310 frag_tail = &skb_shinfo(head)->frag_list;
3311 while (*frag_tail) {
3312 nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3313 frag_tail = &(*frag_tail)->next;
3314 n++;
3315 }
3316
3317 if (max_subframes && n > max_subframes)
3318 goto out;
3319
3320 if (max_frags && nfrags > max_frags)
3321 goto out;
3322
Sara Sharon9739fe22018-09-05 08:06:11 +03003323 if (!drv_can_aggregate_in_amsdu(local, head, skb))
3324 goto out;
3325
Lorenzo Bianconi1eb50792018-08-29 21:03:25 +02003326 if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003327 goto out;
3328
Sara Sharon166ac9d2018-08-29 08:57:02 +02003329 /*
3330 * Pad out the previous subframe to a multiple of 4 by adding the
3331 * padding to the next one, that's being added. Note that head->len
3332 * is the length of the full A-MSDU, but that works since each time
3333 * we add a new subframe we pad out the previous one to a multiple
3334 * of 4 and thus it no longer matters in the next round.
3335 */
3336 hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3337 if ((head->len - hdrlen) & 3)
3338 pad = 4 - ((head->len - hdrlen) & 3);
3339
3340 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3341 2 + pad))
Johannes Bergaa58acf2018-08-30 10:55:49 +02003342 goto out_recalc;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003343
3344 ret = true;
3345 data = skb_push(skb, ETH_ALEN + 2);
3346 memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3347
3348 data += 2 * ETH_ALEN;
3349 len = cpu_to_be16(subframe_len);
3350 memcpy(data, &len, 2);
3351 memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3352
Sara Sharon166ac9d2018-08-29 08:57:02 +02003353 memset(skb_push(skb, pad), 0, pad);
3354
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003355 head->len += skb->len;
3356 head->data_len += skb->len;
3357 *frag_tail = skb;
3358
Johannes Bergaa58acf2018-08-30 10:55:49 +02003359out_recalc:
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003360 fq->memory_usage += head->truesize - orig_truesize;
Johannes Bergaa58acf2018-08-30 10:55:49 +02003361 if (head->len != orig_len) {
3362 flow->backlog += head->len - orig_len;
3363 tin->backlog_bytes += head->len - orig_len;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003364
Johannes Bergaa58acf2018-08-30 10:55:49 +02003365 fq_recalc_backlog(fq, tin, flow);
3366 }
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003367out:
Michal Kaziorfa962b92016-05-19 10:37:49 +02003368 spin_unlock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003369
3370 return ret;
3371}
3372
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003373/*
3374 * Can be called while the sta lock is held. Anything that can cause packets to
3375 * be generated will cause deadlock!
3376 */
3377static void ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3378 struct sta_info *sta, u8 pn_offs,
3379 struct ieee80211_key *key,
3380 struct sk_buff *skb)
3381{
3382 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3383 struct ieee80211_hdr *hdr = (void *)skb->data;
3384 u8 tid = IEEE80211_NUM_TIDS;
3385
3386 if (key)
3387 info->control.hw_key = &key->conf;
3388
3389 ieee80211_tx_stats(skb->dev, skb->len);
3390
3391 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3392 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003393 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3394 } else {
3395 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3396 hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3397 sdata->sequence_number += 0x10;
3398 }
3399
3400 if (skb_shinfo(skb)->gso_size)
3401 sta->tx_stats.msdu[tid] +=
3402 DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3403 else
3404 sta->tx_stats.msdu[tid]++;
3405
3406 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3407
3408 /* statistics normally done by ieee80211_tx_h_stats (but that
3409 * has to consider fragmentation, so is more complex)
3410 */
3411 sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3412 sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
3413
3414 if (pn_offs) {
3415 u64 pn;
3416 u8 *crypto_hdr = skb->data + pn_offs;
3417
3418 switch (key->conf.cipher) {
3419 case WLAN_CIPHER_SUITE_CCMP:
3420 case WLAN_CIPHER_SUITE_CCMP_256:
3421 case WLAN_CIPHER_SUITE_GCMP:
3422 case WLAN_CIPHER_SUITE_GCMP_256:
3423 pn = atomic64_inc_return(&key->conf.tx_pn);
3424 crypto_hdr[0] = pn;
3425 crypto_hdr[1] = pn >> 8;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003426 crypto_hdr[3] = 0x20 | (key->conf.keyidx << 6);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003427 crypto_hdr[4] = pn >> 16;
3428 crypto_hdr[5] = pn >> 24;
3429 crypto_hdr[6] = pn >> 32;
3430 crypto_hdr[7] = pn >> 40;
3431 break;
3432 }
3433 }
3434}
3435
Johannes Berg17c18bf2015-03-21 15:25:43 +01003436static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003437 struct sta_info *sta,
Johannes Berg17c18bf2015-03-21 15:25:43 +01003438 struct ieee80211_fast_tx *fast_tx,
3439 struct sk_buff *skb)
3440{
3441 struct ieee80211_local *local = sdata->local;
3442 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3443 int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3444 int hw_headroom = sdata->local->hw.extra_tx_headroom;
3445 struct ethhdr eth;
Johannes Berg35f432a2017-01-02 11:19:29 +01003446 struct ieee80211_tx_info *info;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003447 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3448 struct ieee80211_tx_data tx;
3449 ieee80211_tx_result r;
3450 struct tid_ampdu_tx *tid_tx = NULL;
3451 u8 tid = IEEE80211_NUM_TIDS;
3452
3453 /* control port protocol needs a lot of special handling */
3454 if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3455 return false;
3456
3457 /* only RFC 1042 SNAP */
3458 if (ethertype < ETH_P_802_3_MIN)
3459 return false;
3460
3461 /* don't handle TX status request here either */
3462 if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3463 return false;
3464
3465 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3466 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3467 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
Johannes Berg472be002015-06-09 16:45:08 +02003468 if (tid_tx) {
3469 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3470 return false;
3471 if (tid_tx->timeout)
3472 tid_tx->last_tx = jiffies;
3473 }
Johannes Berg17c18bf2015-03-21 15:25:43 +01003474 }
3475
3476 /* after this point (skb is modified) we cannot return false */
3477
3478 if (skb_shared(skb)) {
3479 struct sk_buff *tmp_skb = skb;
3480
3481 skb = skb_clone(skb, GFP_ATOMIC);
3482 kfree_skb(tmp_skb);
3483
3484 if (!skb)
3485 return true;
3486 }
3487
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003488 if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3489 ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
3490 return true;
3491
Johannes Berg17c18bf2015-03-21 15:25:43 +01003492 /* will not be crypto-handled beyond what we do here, so use false
3493 * as the may-encrypt argument for the resize to not account for
3494 * more room than we already have in 'extra_head'
3495 */
3496 if (unlikely(ieee80211_skb_resize(sdata, skb,
3497 max_t(int, extra_head + hw_headroom -
3498 skb_headroom(skb), 0),
3499 false))) {
3500 kfree_skb(skb);
3501 return true;
3502 }
3503
3504 memcpy(&eth, skb->data, ETH_HLEN - 2);
Johannes Bergd58ff352017-06-16 14:29:23 +02003505 hdr = skb_push(skb, extra_head);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003506 memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3507 memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
3508 memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
3509
Johannes Berg35f432a2017-01-02 11:19:29 +01003510 info = IEEE80211_SKB_CB(skb);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003511 memset(info, 0, sizeof(*info));
3512 info->band = fast_tx->band;
3513 info->control.vif = &sdata->vif;
3514 info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3515 IEEE80211_TX_CTL_DONTFRAG |
3516 (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003517 info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003518
Julius Niedworok276d9e82019-03-28 21:01:06 +01003519#ifdef CONFIG_MAC80211_DEBUGFS
3520 if (local->force_tx_status)
3521 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3522#endif
3523
Felix Fietkaua786f962016-11-04 10:27:54 +01003524 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3525 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3526 *ieee80211_get_qos_ctl(hdr) = tid;
3527 }
3528
Johannes Berg17c18bf2015-03-21 15:25:43 +01003529 __skb_queue_head_init(&tx.skbs);
3530
3531 tx.flags = IEEE80211_TX_UNICAST;
3532 tx.local = local;
3533 tx.sdata = sdata;
3534 tx.sta = sta;
3535 tx.key = fast_tx->key;
3536
Johannes Berg30686bf2015-06-02 21:39:54 +02003537 if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01003538 tx.skb = skb;
3539 r = ieee80211_tx_h_rate_ctrl(&tx);
3540 skb = tx.skb;
3541 tx.skb = NULL;
3542
3543 if (r != TX_CONTINUE) {
3544 if (r != TX_QUEUED)
3545 kfree_skb(skb);
3546 return true;
3547 }
3548 }
3549
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003550 if (ieee80211_queue_skb(local, sdata, sta, skb))
3551 return true;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003552
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003553 ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3554 fast_tx->key, skb);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003555
3556 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3557 sdata = container_of(sdata->bss,
3558 struct ieee80211_sub_if_data, u.ap);
3559
3560 __skb_queue_tail(&tx.skbs, skb);
Johannes Berg4fd03282019-10-01 23:26:35 +02003561 ieee80211_tx_frags(local, &sdata->vif, sta, &tx.skbs, false);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003562 return true;
3563}
3564
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003565struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3566 struct ieee80211_txq *txq)
3567{
3568 struct ieee80211_local *local = hw_to_local(hw);
3569 struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3570 struct ieee80211_hdr *hdr;
3571 struct sk_buff *skb = NULL;
3572 struct fq *fq = &local->fq;
3573 struct fq_tin *tin = &txqi->tin;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003574 struct ieee80211_tx_info *info;
3575 struct ieee80211_tx_data tx;
3576 ieee80211_tx_result r;
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303577 struct ieee80211_vif *vif = txq->vif;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003578
Erik Stromdahlfb0e76a2019-06-17 22:01:39 +02003579 WARN_ON_ONCE(softirq_count() == 0);
3580
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003581 if (!ieee80211_txq_airtime_check(hw, txq))
3582 return NULL;
3583
Felix Fietkauded46982019-03-16 18:06:33 +01003584begin:
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003585 spin_lock_bh(&fq->lock);
3586
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303587 if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags) ||
3588 test_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags))
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003589 goto out;
3590
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303591 if (vif->txqs_stopped[ieee80211_ac_from_tid(txq->tid)]) {
3592 set_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags);
3593 goto out;
3594 }
3595
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003596 /* Make sure fragments stay together. */
3597 skb = __skb_dequeue(&txqi->frags);
3598 if (skb)
3599 goto out;
3600
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003601 skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
3602 if (!skb)
3603 goto out;
3604
Felix Fietkauded46982019-03-16 18:06:33 +01003605 spin_unlock_bh(&fq->lock);
3606
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003607 hdr = (struct ieee80211_hdr *)skb->data;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003608 info = IEEE80211_SKB_CB(skb);
3609
3610 memset(&tx, 0, sizeof(tx));
3611 __skb_queue_head_init(&tx.skbs);
3612 tx.local = local;
3613 tx.skb = skb;
3614 tx.sdata = vif_to_sdata(info->control.vif);
3615
Mathy Vanhoef1df2bdb2020-07-23 14:01:48 +04003616 if (txq->sta && !(info->flags & IEEE80211_TX_CTL_INJECTED)) {
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003617 tx.sta = container_of(txq->sta, struct sta_info, sta);
Jouni Malinence2e1ca2020-03-26 15:51:34 +01003618 /*
3619 * Drop unicast frames to unauthorised stations unless they are
3620 * EAPOL frames from the local station.
3621 */
Johannes Bergbe8c8272020-03-29 22:50:06 +02003622 if (unlikely(ieee80211_is_data(hdr->frame_control) &&
3623 !ieee80211_vif_is_mesh(&tx.sdata->vif) &&
Jouni Malinence2e1ca2020-03-26 15:51:34 +01003624 tx.sdata->vif.type != NL80211_IFTYPE_OCB &&
3625 !is_multicast_ether_addr(hdr->addr1) &&
3626 !test_sta_flag(tx.sta, WLAN_STA_AUTHORIZED) &&
3627 (!(info->control.flags &
3628 IEEE80211_TX_CTRL_PORT_CTRL_PROTO) ||
3629 !ether_addr_equal(tx.sdata->vif.addr,
3630 hdr->addr2)))) {
3631 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
3632 ieee80211_free_txskb(&local->hw, skb);
3633 goto begin;
3634 }
3635 }
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003636
3637 /*
3638 * The key can be removed while the packet was queued, so need to call
3639 * this here to get the current key.
3640 */
3641 r = ieee80211_tx_h_select_key(&tx);
3642 if (r != TX_CONTINUE) {
3643 ieee80211_free_txskb(&local->hw, skb);
3644 goto begin;
3645 }
3646
Felix Fietkauc1f4c9e2016-11-04 10:27:52 +01003647 if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
3648 info->flags |= IEEE80211_TX_CTL_AMPDU;
3649 else
3650 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
3651
John Crispin50ff4772019-11-25 11:04:37 +01003652 if (info->control.flags & IEEE80211_TX_CTRL_HW_80211_ENCAP)
3653 goto encap_out;
3654
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003655 if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003656 struct sta_info *sta = container_of(txq->sta, struct sta_info,
3657 sta);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003658 u8 pn_offs = 0;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003659
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003660 if (tx.key &&
3661 (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
3662 pn_offs = ieee80211_hdrlen(hdr->frame_control);
3663
3664 ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
3665 tx.key, skb);
3666 } else {
3667 if (invoke_tx_handlers_late(&tx))
3668 goto begin;
3669
3670 skb = __skb_dequeue(&tx.skbs);
3671
Felix Fietkauded46982019-03-16 18:06:33 +01003672 if (!skb_queue_empty(&tx.skbs)) {
3673 spin_lock_bh(&fq->lock);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003674 skb_queue_splice_tail(&tx.skbs, &txqi->frags);
Felix Fietkauded46982019-03-16 18:06:33 +01003675 spin_unlock_bh(&fq->lock);
3676 }
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003677 }
3678
Johannes Berg233e98d2018-12-15 11:03:09 +02003679 if (skb_has_frag_list(skb) &&
Johannes Berg1e1430d2016-10-04 09:22:19 +02003680 !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
3681 if (skb_linearize(skb)) {
3682 ieee80211_free_txskb(&local->hw, skb);
3683 goto begin;
3684 }
3685 }
3686
Johannes Berg53168212017-06-22 12:20:30 +02003687 switch (tx.sdata->vif.type) {
3688 case NL80211_IFTYPE_MONITOR:
3689 if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
3690 vif = &tx.sdata->vif;
3691 break;
3692 }
3693 tx.sdata = rcu_dereference(local->monitor_sdata);
3694 if (tx.sdata) {
3695 vif = &tx.sdata->vif;
3696 info->hw_queue =
3697 vif->hw_queue[skb_get_queue_mapping(skb)];
3698 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
3699 ieee80211_free_txskb(&local->hw, skb);
3700 goto begin;
3701 } else {
3702 vif = NULL;
3703 }
3704 break;
3705 case NL80211_IFTYPE_AP_VLAN:
3706 tx.sdata = container_of(tx.sdata->bss,
3707 struct ieee80211_sub_if_data, u.ap);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05003708 fallthrough;
Johannes Berg53168212017-06-22 12:20:30 +02003709 default:
3710 vif = &tx.sdata->vif;
3711 break;
3712 }
3713
John Crispin50ff4772019-11-25 11:04:37 +01003714encap_out:
Johannes Berg53168212017-06-22 12:20:30 +02003715 IEEE80211_SKB_CB(skb)->control.vif = vif;
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003716
Johannes Bergca98c472020-02-21 10:45:45 +01003717 if (vif &&
3718 wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) {
Felix Fietkau3ff901c2020-07-24 20:28:16 +02003719 bool ampdu = txq->ac != IEEE80211_AC_VO;
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003720 u32 airtime;
3721
3722 airtime = ieee80211_calc_expected_tx_airtime(hw, vif, txq->sta,
Felix Fietkau3ff901c2020-07-24 20:28:16 +02003723 skb->len, ampdu);
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003724 if (airtime) {
3725 airtime = ieee80211_info_set_tx_time_est(info, airtime);
3726 ieee80211_sta_update_pending_airtime(local, tx.sta,
3727 txq->ac,
3728 airtime,
3729 false);
3730 }
3731 }
3732
Felix Fietkauded46982019-03-16 18:06:33 +01003733 return skb;
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303734
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003735out:
3736 spin_unlock_bh(&fq->lock);
3737
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003738 return skb;
3739}
3740EXPORT_SYMBOL(ieee80211_tx_dequeue);
3741
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003742struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac)
3743{
3744 struct ieee80211_local *local = hw_to_local(hw);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003745 struct ieee80211_txq *ret = NULL;
Kan Yan3ace10f2019-11-18 22:06:09 -08003746 struct txq_info *txqi = NULL, *head = NULL;
3747 bool found_eligible_txq = false;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003748
Felix Fietkau5b989c12019-03-15 11:03:35 +01003749 spin_lock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003750
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003751 begin:
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003752 txqi = list_first_entry_or_null(&local->active_txqs[ac],
3753 struct txq_info,
3754 schedule_order);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003755 if (!txqi)
Felix Fietkau5b989c12019-03-15 11:03:35 +01003756 goto out;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003757
Kan Yan3ace10f2019-11-18 22:06:09 -08003758 if (txqi == head) {
3759 if (!found_eligible_txq)
3760 goto out;
3761 else
3762 found_eligible_txq = false;
3763 }
3764
3765 if (!head)
3766 head = txqi;
3767
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003768 if (txqi->txq.sta) {
3769 struct sta_info *sta = container_of(txqi->txq.sta,
Kan Yan3ace10f2019-11-18 22:06:09 -08003770 struct sta_info, sta);
3771 bool aql_check = ieee80211_txq_airtime_check(hw, &txqi->txq);
3772 s64 deficit = sta->airtime[txqi->txq.ac].deficit;
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003773
Kan Yan3ace10f2019-11-18 22:06:09 -08003774 if (aql_check)
3775 found_eligible_txq = true;
3776
3777 if (deficit < 0)
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003778 sta->airtime[txqi->txq.ac].deficit +=
3779 sta->airtime_weight;
Kan Yan3ace10f2019-11-18 22:06:09 -08003780
3781 if (deficit < 0 || !aql_check) {
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003782 list_move_tail(&txqi->schedule_order,
3783 &local->active_txqs[txqi->txq.ac]);
3784 goto begin;
3785 }
3786 }
3787
3788
3789 if (txqi->schedule_round == local->schedule_round[ac])
Felix Fietkau5b989c12019-03-15 11:03:35 +01003790 goto out;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003791
3792 list_del_init(&txqi->schedule_order);
3793 txqi->schedule_round = local->schedule_round[ac];
Felix Fietkau5b989c12019-03-15 11:03:35 +01003794 ret = &txqi->txq;
3795
3796out:
3797 spin_unlock_bh(&local->active_txq_lock[ac]);
3798 return ret;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003799}
3800EXPORT_SYMBOL(ieee80211_next_txq);
3801
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003802void __ieee80211_schedule_txq(struct ieee80211_hw *hw,
3803 struct ieee80211_txq *txq,
3804 bool force)
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003805{
3806 struct ieee80211_local *local = hw_to_local(hw);
3807 struct txq_info *txqi = to_txq_info(txq);
3808
Felix Fietkau5b989c12019-03-15 11:03:35 +01003809 spin_lock_bh(&local->active_txq_lock[txq->ac]);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003810
3811 if (list_empty(&txqi->schedule_order) &&
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003812 (force || !skb_queue_empty(&txqi->frags) ||
3813 txqi->tin.backlog_packets)) {
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003814 /* If airtime accounting is active, always enqueue STAs at the
3815 * head of the list to ensure that they only get moved to the
3816 * back by the airtime DRR scheduler once they have a negative
3817 * deficit. A station that already has a negative deficit will
3818 * get immediately moved to the back of the list on the next
3819 * call to ieee80211_next_txq().
3820 */
3821 if (txqi->txq.sta &&
3822 wiphy_ext_feature_isset(local->hw.wiphy,
3823 NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
3824 list_add(&txqi->schedule_order,
3825 &local->active_txqs[txq->ac]);
3826 else
3827 list_add_tail(&txqi->schedule_order,
3828 &local->active_txqs[txq->ac]);
3829 }
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003830
Toke Høiland-Jørgensen390298e2019-01-22 15:20:16 +01003831 spin_unlock_bh(&local->active_txq_lock[txq->ac]);
3832}
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003833EXPORT_SYMBOL(__ieee80211_schedule_txq);
Toke Høiland-Jørgensen390298e2019-01-22 15:20:16 +01003834
Kan Yan3ace10f2019-11-18 22:06:09 -08003835bool ieee80211_txq_airtime_check(struct ieee80211_hw *hw,
3836 struct ieee80211_txq *txq)
3837{
3838 struct sta_info *sta;
3839 struct ieee80211_local *local = hw_to_local(hw);
3840
Toke Høiland-Jørgensen911bde02019-12-12 12:14:37 +01003841 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
Kan Yan3ace10f2019-11-18 22:06:09 -08003842 return true;
3843
3844 if (!txq->sta)
3845 return true;
3846
3847 sta = container_of(txq->sta, struct sta_info, sta);
3848 if (atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
3849 sta->airtime[txq->ac].aql_limit_low)
3850 return true;
3851
3852 if (atomic_read(&local->aql_total_pending_airtime) <
3853 local->aql_threshold &&
3854 atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
3855 sta->airtime[txq->ac].aql_limit_high)
3856 return true;
3857
3858 return false;
3859}
3860EXPORT_SYMBOL(ieee80211_txq_airtime_check);
3861
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003862bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
3863 struct ieee80211_txq *txq)
3864{
3865 struct ieee80211_local *local = hw_to_local(hw);
3866 struct txq_info *iter, *tmp, *txqi = to_txq_info(txq);
3867 struct sta_info *sta;
3868 u8 ac = txq->ac;
3869
Felix Fietkau5b989c12019-03-15 11:03:35 +01003870 spin_lock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003871
3872 if (!txqi->txq.sta)
3873 goto out;
3874
3875 if (list_empty(&txqi->schedule_order))
3876 goto out;
3877
3878 list_for_each_entry_safe(iter, tmp, &local->active_txqs[ac],
3879 schedule_order) {
3880 if (iter == txqi)
3881 break;
3882
3883 if (!iter->txq.sta) {
3884 list_move_tail(&iter->schedule_order,
3885 &local->active_txqs[ac]);
3886 continue;
3887 }
3888 sta = container_of(iter->txq.sta, struct sta_info, sta);
3889 if (sta->airtime[ac].deficit < 0)
3890 sta->airtime[ac].deficit += sta->airtime_weight;
3891 list_move_tail(&iter->schedule_order, &local->active_txqs[ac]);
3892 }
3893
3894 sta = container_of(txqi->txq.sta, struct sta_info, sta);
3895 if (sta->airtime[ac].deficit >= 0)
3896 goto out;
3897
3898 sta->airtime[ac].deficit += sta->airtime_weight;
3899 list_move_tail(&txqi->schedule_order, &local->active_txqs[ac]);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003900 spin_unlock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003901
3902 return false;
3903out:
3904 if (!list_empty(&txqi->schedule_order))
3905 list_del_init(&txqi->schedule_order);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003906 spin_unlock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003907
3908 return true;
3909}
3910EXPORT_SYMBOL(ieee80211_txq_may_transmit);
3911
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003912void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003913{
3914 struct ieee80211_local *local = hw_to_local(hw);
3915
3916 spin_lock_bh(&local->active_txq_lock[ac]);
3917 local->schedule_round[ac]++;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003918 spin_unlock_bh(&local->active_txq_lock[ac]);
3919}
Felix Fietkau5b989c12019-03-15 11:03:35 +01003920EXPORT_SYMBOL(ieee80211_txq_schedule_start);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003921
Johannes Berg4c9451e2014-11-09 18:50:10 +02003922void __ieee80211_subif_start_xmit(struct sk_buff *skb,
3923 struct net_device *dev,
Rajkumar Manoharan06016772019-04-11 13:47:25 -07003924 u32 info_flags,
Markus Theila7528192020-05-27 18:03:34 +02003925 u32 ctrl_flags,
3926 u64 *cookie)
Johannes Berg4c9451e2014-11-09 18:50:10 +02003927{
3928 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Felix Fietkau1974da82019-03-25 08:59:23 +01003929 struct ieee80211_local *local = sdata->local;
Johannes Berg97ffe752015-03-21 09:13:45 +01003930 struct sta_info *sta;
Johannes Berg80616c02015-04-14 14:50:41 +02003931 struct sk_buff *next;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003932
3933 if (unlikely(skb->len < ETH_HLEN)) {
3934 kfree_skb(skb);
3935 return;
3936 }
3937
3938 rcu_read_lock();
3939
Johannes Berg2d981fd2015-04-10 14:10:10 +02003940 if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
3941 goto out_free;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003942
Felix Fietkau1974da82019-03-25 08:59:23 +01003943 if (IS_ERR(sta))
3944 sta = NULL;
3945
3946 if (local->ops->wake_tx_queue) {
3947 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
3948 skb_set_queue_mapping(skb, queue);
Felix Fietkau180ac482020-07-26 15:09:47 +02003949 skb_get_hash(skb);
Felix Fietkau1974da82019-03-25 08:59:23 +01003950 }
3951
3952 if (sta) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01003953 struct ieee80211_fast_tx *fast_tx;
3954
Wen Gong70e53662018-08-08 18:40:01 +08003955 sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
Toke Høiland-Jørgensen36148c22018-02-02 16:11:05 +01003956
Johannes Berg17c18bf2015-03-21 15:25:43 +01003957 fast_tx = rcu_dereference(sta->fast_tx);
3958
3959 if (fast_tx &&
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003960 ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
Johannes Berg17c18bf2015-03-21 15:25:43 +01003961 goto out;
3962 }
3963
Johannes Berg80616c02015-04-14 14:50:41 +02003964 if (skb_is_gso(skb)) {
3965 struct sk_buff *segs;
Johannes Berg680a0da2015-04-13 16:58:25 +02003966
Johannes Berg80616c02015-04-14 14:50:41 +02003967 segs = skb_gso_segment(skb, 0);
3968 if (IS_ERR(segs)) {
Johannes Berg2d981fd2015-04-10 14:10:10 +02003969 goto out_free;
Johannes Berg80616c02015-04-14 14:50:41 +02003970 } else if (segs) {
3971 consume_skb(skb);
3972 skb = segs;
3973 }
3974 } else {
3975 /* we cannot process non-linear frames on this path */
3976 if (skb_linearize(skb)) {
3977 kfree_skb(skb);
3978 goto out;
3979 }
3980
3981 /* the frame could be fragmented, software-encrypted, and other
3982 * things so we cannot really handle checksum offload with it -
3983 * fix it up in software before we handle anything else.
3984 */
3985 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Johannes Bergf603f1f2015-05-05 15:25:33 +02003986 skb_set_transport_header(skb,
3987 skb_checksum_start_offset(skb));
Johannes Berg80616c02015-04-14 14:50:41 +02003988 if (skb_checksum_help(skb))
3989 goto out_free;
3990 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02003991 }
3992
Jason A. Donenfeld9f3ef3d2020-01-13 18:42:33 -05003993 skb_list_walk_safe(skb, skb, next) {
3994 skb_mark_not_on_list(skb);
Johannes Berg4c9451e2014-11-09 18:50:10 +02003995
Markus Theil5af7fef2020-06-17 10:26:36 +02003996 if (skb->protocol == sdata->control_port_protocol)
3997 ctrl_flags |= IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
3998
Rajkumar Manoharan06016772019-04-11 13:47:25 -07003999 skb = ieee80211_build_hdr(sdata, skb, info_flags,
Markus Theila7528192020-05-27 18:03:34 +02004000 sta, ctrl_flags, cookie);
Jason A. Donenfeld9f3ef3d2020-01-13 18:42:33 -05004001 if (IS_ERR(skb)) {
4002 kfree_skb_list(next);
Johannes Berg80616c02015-04-14 14:50:41 +02004003 goto out;
Jason A. Donenfeld9f3ef3d2020-01-13 18:42:33 -05004004 }
Johannes Berg80616c02015-04-14 14:50:41 +02004005
Johannes Berg5a490512015-04-22 17:10:38 +02004006 ieee80211_tx_stats(dev, skb->len);
Johannes Berg80616c02015-04-14 14:50:41 +02004007
Mathy Vanhoef08aca292020-07-23 14:01:52 +04004008 ieee80211_xmit(sdata, sta, skb);
Johannes Berg80616c02015-04-14 14:50:41 +02004009 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02004010 goto out;
4011 out_free:
4012 kfree_skb(skb);
Johannes Berg4c9451e2014-11-09 18:50:10 +02004013 out:
4014 rcu_read_unlock();
4015}
4016
Michael Braunebceec82016-11-22 11:52:18 +01004017static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
4018{
4019 struct ethhdr *eth;
4020 int err;
4021
4022 err = skb_ensure_writable(skb, ETH_HLEN);
4023 if (unlikely(err))
4024 return err;
4025
4026 eth = (void *)skb->data;
4027 ether_addr_copy(eth->h_dest, sta->sta.addr);
4028
4029 return 0;
4030}
4031
4032static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
4033 struct net_device *dev)
4034{
4035 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4036 const struct ethhdr *eth = (void *)skb->data;
4037 const struct vlan_ethhdr *ethvlan = (void *)skb->data;
4038 __be16 ethertype;
4039
4040 if (likely(!is_multicast_ether_addr(eth->h_dest)))
4041 return false;
4042
4043 switch (sdata->vif.type) {
4044 case NL80211_IFTYPE_AP_VLAN:
4045 if (sdata->u.vlan.sta)
4046 return false;
4047 if (sdata->wdev.use_4addr)
4048 return false;
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05004049 fallthrough;
Michael Braunebceec82016-11-22 11:52:18 +01004050 case NL80211_IFTYPE_AP:
4051 /* check runtime toggle for this bss */
4052 if (!sdata->bss->multicast_to_unicast)
4053 return false;
4054 break;
4055 default:
4056 return false;
4057 }
4058
4059 /* multicast to unicast conversion only for some payload */
4060 ethertype = eth->h_proto;
4061 if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
4062 ethertype = ethvlan->h_vlan_encapsulated_proto;
4063 switch (ethertype) {
4064 case htons(ETH_P_ARP):
4065 case htons(ETH_P_IP):
4066 case htons(ETH_P_IPV6):
4067 break;
4068 default:
4069 return false;
4070 }
4071
4072 return true;
4073}
4074
4075static void
4076ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
4077 struct sk_buff_head *queue)
4078{
4079 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4080 struct ieee80211_local *local = sdata->local;
4081 const struct ethhdr *eth = (struct ethhdr *)skb->data;
4082 struct sta_info *sta, *first = NULL;
4083 struct sk_buff *cloned_skb;
4084
4085 rcu_read_lock();
4086
4087 list_for_each_entry_rcu(sta, &local->sta_list, list) {
4088 if (sdata != sta->sdata)
4089 /* AP-VLAN mismatch */
4090 continue;
4091 if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
4092 /* do not send back to source */
4093 continue;
4094 if (!first) {
4095 first = sta;
4096 continue;
4097 }
4098 cloned_skb = skb_clone(skb, GFP_ATOMIC);
4099 if (!cloned_skb)
4100 goto multicast;
4101 if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
4102 dev_kfree_skb(cloned_skb);
4103 goto multicast;
4104 }
4105 __skb_queue_tail(queue, cloned_skb);
4106 }
4107
4108 if (likely(first)) {
4109 if (unlikely(ieee80211_change_da(skb, first)))
4110 goto multicast;
4111 __skb_queue_tail(queue, skb);
4112 } else {
4113 /* no STA connected, drop */
4114 kfree_skb(skb);
4115 skb = NULL;
4116 }
4117
4118 goto out;
4119multicast:
4120 __skb_queue_purge(queue);
4121 __skb_queue_tail(queue, skb);
4122out:
4123 rcu_read_unlock();
4124}
4125
Johannes Berg4c9451e2014-11-09 18:50:10 +02004126/**
4127 * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
4128 * @skb: packet to be sent
4129 * @dev: incoming interface
4130 *
4131 * On failure skb will be freed.
4132 */
Liad Kaufman24d342c2014-11-09 18:50:07 +02004133netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
4134 struct net_device *dev)
4135{
Michael Braunebceec82016-11-22 11:52:18 +01004136 if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
4137 struct sk_buff_head queue;
4138
4139 __skb_queue_head_init(&queue);
4140 ieee80211_convert_to_unicast(skb, dev, &queue);
4141 while ((skb = __skb_dequeue(&queue)))
Markus Theila7528192020-05-27 18:03:34 +02004142 __ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
Michael Braunebceec82016-11-22 11:52:18 +01004143 } else {
Markus Theila7528192020-05-27 18:03:34 +02004144 __ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
Michael Braunebceec82016-11-22 11:52:18 +01004145 }
4146
Liad Kaufman24d342c2014-11-09 18:50:07 +02004147 return NETDEV_TX_OK;
4148}
Johannes Berge2ebc742007-07-27 15:43:22 +02004149
John Crispin50ff4772019-11-25 11:04:37 +01004150static bool ieee80211_tx_8023(struct ieee80211_sub_if_data *sdata,
4151 struct sk_buff *skb, int led_len,
4152 struct sta_info *sta,
4153 bool txpending)
4154{
4155 struct ieee80211_local *local = sdata->local;
4156 struct ieee80211_tx_control control = {};
4157 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4158 struct ieee80211_sta *pubsta = NULL;
4159 unsigned long flags;
4160 int q = info->hw_queue;
4161
4162 if (ieee80211_queue_skb(local, sdata, sta, skb))
4163 return true;
4164
4165 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4166
4167 if (local->queue_stop_reasons[q] ||
4168 (!txpending && !skb_queue_empty(&local->pending[q]))) {
4169 if (txpending)
4170 skb_queue_head(&local->pending[q], skb);
4171 else
4172 skb_queue_tail(&local->pending[q], skb);
4173
4174 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4175
4176 return false;
4177 }
4178
4179 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4180
4181 if (sta && sta->uploaded)
4182 pubsta = &sta->sta;
4183
4184 control.sta = pubsta;
4185
4186 drv_tx(local, &control, skb);
4187
4188 return true;
4189}
4190
4191static void ieee80211_8023_xmit(struct ieee80211_sub_if_data *sdata,
4192 struct net_device *dev, struct sta_info *sta,
4193 struct sk_buff *skb)
4194{
4195 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4196 struct ethhdr *ehdr = (struct ethhdr *)skb->data;
4197 struct ieee80211_local *local = sdata->local;
4198 bool authorized = false;
4199 bool multicast;
4200 unsigned char *ra = ehdr->h_dest;
4201
4202 if (IS_ERR(sta) || (sta && !sta->uploaded))
4203 sta = NULL;
4204
4205 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
4206 (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER)))
4207 ra = sdata->u.mgd.bssid;
4208
Seevalamuthu Mariappan78fb5b52020-06-09 15:45:54 +05304209 if (is_zero_ether_addr(ra))
John Crispin50ff4772019-11-25 11:04:37 +01004210 goto out_free;
4211
4212 multicast = is_multicast_ether_addr(ra);
4213
4214 if (sta)
4215 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
4216
4217 if (!multicast && !authorized &&
4218 (ehdr->h_proto != sdata->control_port_protocol ||
4219 !ether_addr_equal(sdata->vif.addr, ehdr->h_source)))
4220 goto out_free;
4221
4222 if (multicast && sdata->vif.type == NL80211_IFTYPE_AP &&
4223 !atomic_read(&sdata->u.ap.num_mcast_sta))
4224 goto out_free;
4225
4226 if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning)) &&
4227 test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
4228 goto out_free;
4229
Vasanthakumar Thiagarajane61fbfc2020-07-22 19:50:17 +05304230 memset(info, 0, sizeof(*info));
4231
John Crispin50ff4772019-11-25 11:04:37 +01004232 if (unlikely(!multicast && skb->sk &&
4233 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS))
Vasanthakumar Thiagarajane61fbfc2020-07-22 19:50:17 +05304234 info->ack_frame_id = ieee80211_store_ack_skb(local, skb,
4235 &info->flags, NULL);
John Crispin50ff4772019-11-25 11:04:37 +01004236
4237 if (unlikely(sdata->control_port_protocol == ehdr->h_proto)) {
4238 if (sdata->control_port_no_encrypt)
4239 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
4240 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
4241 }
4242
4243 if (multicast)
4244 info->flags |= IEEE80211_TX_CTL_NO_ACK;
4245
4246 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
4247
4248 ieee80211_tx_stats(dev, skb->len);
4249
4250 if (sta) {
4251 sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
4252 sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
4253 }
4254
4255 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4256 sdata = container_of(sdata->bss,
4257 struct ieee80211_sub_if_data, u.ap);
4258
4259 info->control.flags |= IEEE80211_TX_CTRL_HW_80211_ENCAP;
4260 info->control.vif = &sdata->vif;
4261
4262 ieee80211_tx_8023(sdata, skb, skb->len, sta, false);
4263
4264 return;
4265
4266out_free:
4267 kfree_skb(skb);
4268}
4269
4270netdev_tx_t ieee80211_subif_start_xmit_8023(struct sk_buff *skb,
4271 struct net_device *dev)
4272{
4273 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4274 struct sta_info *sta;
4275
4276 if (WARN_ON(!sdata->hw_80211_encap)) {
4277 kfree_skb(skb);
4278 return NETDEV_TX_OK;
4279 }
4280
4281 if (unlikely(skb->len < ETH_HLEN)) {
4282 kfree_skb(skb);
4283 return NETDEV_TX_OK;
4284 }
4285
4286 rcu_read_lock();
4287
4288 if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
4289 kfree_skb(skb);
4290 else
4291 ieee80211_8023_xmit(sdata, dev, sta, skb);
4292
4293 rcu_read_unlock();
4294
4295 return NETDEV_TX_OK;
4296}
4297
Johannes Berg7528ec52014-11-09 18:50:11 +02004298struct sk_buff *
4299ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
4300 struct sk_buff *skb, u32 info_flags)
4301{
4302 struct ieee80211_hdr *hdr;
4303 struct ieee80211_tx_data tx = {
4304 .local = sdata->local,
4305 .sdata = sdata,
4306 };
Johannes Berg97ffe752015-03-21 09:13:45 +01004307 struct sta_info *sta;
Johannes Berg7528ec52014-11-09 18:50:11 +02004308
4309 rcu_read_lock();
4310
Johannes Berg97ffe752015-03-21 09:13:45 +01004311 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4312 kfree_skb(skb);
4313 skb = ERR_PTR(-EINVAL);
4314 goto out;
4315 }
4316
Markus Theila7528192020-05-27 18:03:34 +02004317 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta, 0, NULL);
Johannes Berg7528ec52014-11-09 18:50:11 +02004318 if (IS_ERR(skb))
4319 goto out;
4320
4321 hdr = (void *)skb->data;
4322 tx.sta = sta_info_get(sdata, hdr->addr1);
4323 tx.skb = skb;
4324
4325 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
4326 rcu_read_unlock();
4327 kfree_skb(skb);
4328 return ERR_PTR(-EINVAL);
4329 }
4330
4331out:
4332 rcu_read_unlock();
4333 return skb;
4334}
4335
Johannes Berge2530082008-05-17 00:57:14 +02004336/*
4337 * ieee80211_clear_tx_pending may not be called in a context where
4338 * it is possible that it packets could come in again.
4339 */
Johannes Berge2ebc742007-07-27 15:43:22 +02004340void ieee80211_clear_tx_pending(struct ieee80211_local *local)
4341{
Felix Fietkau1f98ab72012-11-10 03:44:14 +01004342 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01004343 int i;
Johannes Berge2ebc742007-07-27 15:43:22 +02004344
Felix Fietkau1f98ab72012-11-10 03:44:14 +01004345 for (i = 0; i < local->hw.queues; i++) {
4346 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
4347 ieee80211_free_txskb(&local->hw, skb);
4348 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004349}
4350
Johannes Berg7bb45682011-02-24 14:42:06 +01004351/*
4352 * Returns false if the frame couldn't be transmitted but was queued instead,
4353 * which in this case means re-queued -- take as an indication to stop sending
4354 * more pending frames.
4355 */
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004356static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
4357 struct sk_buff *skb)
4358{
4359 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4360 struct ieee80211_sub_if_data *sdata;
4361 struct sta_info *sta;
4362 struct ieee80211_hdr *hdr;
Johannes Berg7bb45682011-02-24 14:42:06 +01004363 bool result;
Johannes Berg55de9082012-07-26 17:24:39 +02004364 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004365
Johannes Berg5061b0c2009-07-14 00:33:34 +02004366 sdata = vif_to_sdata(info->control.vif);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004367
4368 if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
Johannes Berg55de9082012-07-26 17:24:39 +02004369 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4370 if (unlikely(!chanctx_conf)) {
4371 dev_kfree_skb(skb);
4372 return true;
4373 }
Johannes Berg73c4e192014-11-09 18:50:09 +02004374 info->band = chanctx_conf->def.chan->band;
Mathy Vanhoef08aca292020-07-23 14:01:52 +04004375 result = ieee80211_tx(sdata, NULL, skb, true);
John Crispin50ff4772019-11-25 11:04:37 +01004376 } else if (info->control.flags & IEEE80211_TX_CTRL_HW_80211_ENCAP) {
4377 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4378 dev_kfree_skb(skb);
4379 return true;
4380 }
4381
4382 if (IS_ERR(sta) || (sta && !sta->uploaded))
4383 sta = NULL;
4384
4385 result = ieee80211_tx_8023(sdata, skb, skb->len, sta, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004386 } else {
Johannes Berg252b86c2011-11-16 15:28:55 +01004387 struct sk_buff_head skbs;
4388
4389 __skb_queue_head_init(&skbs);
4390 __skb_queue_tail(&skbs, skb);
4391
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004392 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Bergabe60632009-11-25 17:46:18 +01004393 sta = sta_info_get(sdata, hdr->addr1);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004394
Johannes Berg74e4dbf2011-11-16 15:28:57 +01004395 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004396 }
4397
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004398 return result;
4399}
4400
Johannes Berge2530082008-05-17 00:57:14 +02004401/*
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004402 * Transmit all pending packets. Called from tasklet.
Johannes Berge2530082008-05-17 00:57:14 +02004403 */
Johannes Berge2ebc742007-07-27 15:43:22 +02004404void ieee80211_tx_pending(unsigned long data)
4405{
4406 struct ieee80211_local *local = (struct ieee80211_local *)data;
Johannes Berg2a577d92009-03-23 17:28:37 +01004407 unsigned long flags;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004408 int i;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004409 bool txok;
Johannes Berge2ebc742007-07-27 15:43:22 +02004410
Johannes Bergf0e72852009-03-23 17:28:36 +01004411 rcu_read_lock();
Johannes Berg2a577d92009-03-23 17:28:37 +01004412
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004413 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg176be722009-03-12 23:49:28 +01004414 for (i = 0; i < local->hw.queues; i++) {
Johannes Berg2a577d92009-03-23 17:28:37 +01004415 /*
4416 * If queue is stopped by something other than due to pending
4417 * frames, or we have no pending frames, proceed to next queue.
4418 */
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004419 if (local->queue_stop_reasons[i] ||
Johannes Berg2a577d92009-03-23 17:28:37 +01004420 skb_queue_empty(&local->pending[i]))
Johannes Berge2ebc742007-07-27 15:43:22 +02004421 continue;
Johannes Berge2530082008-05-17 00:57:14 +02004422
Johannes Berg2a577d92009-03-23 17:28:37 +01004423 while (!skb_queue_empty(&local->pending[i])) {
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004424 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
Johannes Berg5061b0c2009-07-14 00:33:34 +02004425 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg5061b0c2009-07-14 00:33:34 +02004426
Johannes Berga7bc3762009-07-27 10:33:31 +02004427 if (WARN_ON(!info->control.vif)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02004428 ieee80211_free_txskb(&local->hw, skb);
Johannes Berga7bc3762009-07-27 10:33:31 +02004429 continue;
4430 }
4431
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004432 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
4433 flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01004434
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004435 txok = ieee80211_tx_pending_skb(local, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004436 spin_lock_irqsave(&local->queue_stop_reason_lock,
4437 flags);
4438 if (!txok)
Johannes Berg2a577d92009-03-23 17:28:37 +01004439 break;
Johannes Berge2ebc742007-07-27 15:43:22 +02004440 }
Johannes Berg7236fe22010-03-22 13:42:43 -07004441
4442 if (skb_queue_empty(&local->pending[i]))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02004443 ieee80211_propagate_queue_wake(local, i);
Johannes Berge2ebc742007-07-27 15:43:22 +02004444 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004445 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01004446
Johannes Bergf0e72852009-03-23 17:28:36 +01004447 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004448}
4449
4450/* functions for drivers to get certain frames */
4451
Marco Porscheac70c12013-01-07 16:04:50 +01004452static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004453 struct ps_data *ps, struct sk_buff *skb,
4454 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02004455{
4456 u8 *pos, *tim;
4457 int aid0 = 0;
4458 int i, have_bits = 0, n1, n2;
4459
4460 /* Generate bitmap for TIM only if there are any STAs in power save
4461 * mode. */
Marco Porschd012a602012-10-10 12:39:50 -07004462 if (atomic_read(&ps->num_sta_ps) > 0)
Johannes Berge2ebc742007-07-27 15:43:22 +02004463 /* in the hope that this is faster than
4464 * checking byte-for-byte */
Weilong Chenf359d3f2013-12-18 15:44:16 +08004465 have_bits = !bitmap_empty((unsigned long *)ps->tim,
Johannes Berge2ebc742007-07-27 15:43:22 +02004466 IEEE80211_MAX_AID+1);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004467 if (!is_template) {
4468 if (ps->dtim_count == 0)
4469 ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
4470 else
4471 ps->dtim_count--;
4472 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004473
Johannes Berg4df864c2017-06-16 14:29:21 +02004474 tim = pos = skb_put(skb, 6);
Johannes Berge2ebc742007-07-27 15:43:22 +02004475 *pos++ = WLAN_EID_TIM;
4476 *pos++ = 4;
Marco Porschd012a602012-10-10 12:39:50 -07004477 *pos++ = ps->dtim_count;
Johannes Berg88600202012-02-13 15:17:18 +01004478 *pos++ = sdata->vif.bss_conf.dtim_period;
Johannes Berge2ebc742007-07-27 15:43:22 +02004479
Marco Porschd012a602012-10-10 12:39:50 -07004480 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
Johannes Berge2ebc742007-07-27 15:43:22 +02004481 aid0 = 1;
4482
Marco Porschd012a602012-10-10 12:39:50 -07004483 ps->dtim_bc_mc = aid0 == 1;
Christian Lamparter512119b2011-01-31 20:48:44 +02004484
Johannes Berge2ebc742007-07-27 15:43:22 +02004485 if (have_bits) {
4486 /* Find largest even number N1 so that bits numbered 1 through
4487 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
4488 * (N2 + 1) x 8 through 2007 are 0. */
4489 n1 = 0;
4490 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
Marco Porschd012a602012-10-10 12:39:50 -07004491 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004492 n1 = i & 0xfe;
4493 break;
4494 }
4495 }
4496 n2 = n1;
4497 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
Marco Porschd012a602012-10-10 12:39:50 -07004498 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004499 n2 = i;
4500 break;
4501 }
4502 }
4503
4504 /* Bitmap control */
4505 *pos++ = n1 | aid0;
4506 /* Part Virt Bitmap */
Eliad Peller5220da32011-11-24 16:50:00 +02004507 skb_put(skb, n2 - n1);
Marco Porschd012a602012-10-10 12:39:50 -07004508 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
Johannes Berge2ebc742007-07-27 15:43:22 +02004509
4510 tim[1] = n2 - n1 + 4;
Johannes Berge2ebc742007-07-27 15:43:22 +02004511 } else {
4512 *pos++ = aid0; /* Bitmap control */
4513 *pos++ = 0; /* Part Virt Bitmap */
4514 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004515}
4516
Marco Porscheac70c12013-01-07 16:04:50 +01004517static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004518 struct ps_data *ps, struct sk_buff *skb,
4519 bool is_template)
Marco Porscheac70c12013-01-07 16:04:50 +01004520{
4521 struct ieee80211_local *local = sdata->local;
4522
4523 /*
4524 * Not very nice, but we want to allow the driver to call
4525 * ieee80211_beacon_get() as a response to the set_tim()
4526 * callback. That, however, is already invoked under the
4527 * sta_lock to guarantee consistent and race-free update
4528 * of the tim bitmap in mac80211 and the driver.
4529 */
4530 if (local->tim_in_locked_section) {
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004531 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Marco Porscheac70c12013-01-07 16:04:50 +01004532 } else {
Johannes Berg1b917312013-02-22 12:55:01 +01004533 spin_lock_bh(&local->tim_lock);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004534 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Johannes Berg1b917312013-02-22 12:55:01 +01004535 spin_unlock_bh(&local->tim_lock);
Marco Porscheac70c12013-01-07 16:04:50 +01004536 }
4537
4538 return 0;
4539}
4540
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004541static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
4542 struct beacon_data *beacon)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004543{
4544 struct probe_resp *resp;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004545 u8 *beacon_data;
4546 size_t beacon_data_len;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004547 int i;
Michal Kazioraf296bd2014-06-05 14:21:36 +02004548 u8 count = beacon->csa_current_counter;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004549
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004550 switch (sdata->vif.type) {
4551 case NL80211_IFTYPE_AP:
4552 beacon_data = beacon->tail;
4553 beacon_data_len = beacon->tail_len;
4554 break;
4555 case NL80211_IFTYPE_ADHOC:
4556 beacon_data = beacon->head;
4557 beacon_data_len = beacon->head_len;
4558 break;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004559 case NL80211_IFTYPE_MESH_POINT:
4560 beacon_data = beacon->head;
4561 beacon_data_len = beacon->head_len;
4562 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004563 default:
4564 return;
4565 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004566
Michal Kazioraf296bd2014-06-05 14:21:36 +02004567 rcu_read_lock();
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004568 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004569 resp = rcu_dereference(sdata->u.ap.probe_resp);
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004570
Michal Kazioraf296bd2014-06-05 14:21:36 +02004571 if (beacon->csa_counter_offsets[i]) {
4572 if (WARN_ON_ONCE(beacon->csa_counter_offsets[i] >=
4573 beacon_data_len)) {
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004574 rcu_read_unlock();
4575 return;
4576 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004577
4578 beacon_data[beacon->csa_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004579 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004580
4581 if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
4582 resp->data[resp->csa_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004583 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004584 rcu_read_unlock();
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004585}
4586
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004587static u8 __ieee80211_csa_update_counter(struct beacon_data *beacon)
4588{
4589 beacon->csa_current_counter--;
4590
4591 /* the counter should never reach 0 */
4592 WARN_ON_ONCE(!beacon->csa_current_counter);
4593
4594 return beacon->csa_current_counter;
4595}
4596
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004597u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
4598{
4599 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004600 struct beacon_data *beacon = NULL;
4601 u8 count = 0;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004602
Michal Kazioraf296bd2014-06-05 14:21:36 +02004603 rcu_read_lock();
4604
4605 if (sdata->vif.type == NL80211_IFTYPE_AP)
4606 beacon = rcu_dereference(sdata->u.ap.beacon);
4607 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4608 beacon = rcu_dereference(sdata->u.ibss.presp);
4609 else if (ieee80211_vif_is_mesh(&sdata->vif))
4610 beacon = rcu_dereference(sdata->u.mesh.beacon);
4611
4612 if (!beacon)
4613 goto unlock;
4614
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004615 count = __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004616
Michal Kazioraf296bd2014-06-05 14:21:36 +02004617unlock:
4618 rcu_read_unlock();
4619 return count;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004620}
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004621EXPORT_SYMBOL(ieee80211_csa_update_counter);
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004622
Gregory Greenman03737002018-04-20 13:49:24 +03004623void ieee80211_csa_set_counter(struct ieee80211_vif *vif, u8 counter)
4624{
4625 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4626 struct beacon_data *beacon = NULL;
4627
4628 rcu_read_lock();
4629
4630 if (sdata->vif.type == NL80211_IFTYPE_AP)
4631 beacon = rcu_dereference(sdata->u.ap.beacon);
4632 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4633 beacon = rcu_dereference(sdata->u.ibss.presp);
4634 else if (ieee80211_vif_is_mesh(&sdata->vif))
4635 beacon = rcu_dereference(sdata->u.mesh.beacon);
4636
4637 if (!beacon)
4638 goto unlock;
4639
4640 if (counter < beacon->csa_current_counter)
4641 beacon->csa_current_counter = counter;
4642
4643unlock:
4644 rcu_read_unlock();
4645}
4646EXPORT_SYMBOL(ieee80211_csa_set_counter);
4647
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004648bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
4649{
4650 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4651 struct beacon_data *beacon = NULL;
4652 u8 *beacon_data;
4653 size_t beacon_data_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004654 int ret = false;
4655
4656 if (!ieee80211_sdata_running(sdata))
4657 return false;
4658
4659 rcu_read_lock();
4660 if (vif->type == NL80211_IFTYPE_AP) {
4661 struct ieee80211_if_ap *ap = &sdata->u.ap;
4662
4663 beacon = rcu_dereference(ap->beacon);
4664 if (WARN_ON(!beacon || !beacon->tail))
4665 goto out;
4666 beacon_data = beacon->tail;
4667 beacon_data_len = beacon->tail_len;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004668 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
4669 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4670
4671 beacon = rcu_dereference(ifibss->presp);
4672 if (!beacon)
4673 goto out;
4674
4675 beacon_data = beacon->head;
4676 beacon_data_len = beacon->head_len;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004677 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
4678 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4679
4680 beacon = rcu_dereference(ifmsh->beacon);
4681 if (!beacon)
4682 goto out;
4683
4684 beacon_data = beacon->head;
4685 beacon_data_len = beacon->head_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004686 } else {
4687 WARN_ON(1);
4688 goto out;
4689 }
4690
Michal Kazior10d78f22014-06-05 14:21:37 +02004691 if (!beacon->csa_counter_offsets[0])
4692 goto out;
4693
Michal Kazioraf296bd2014-06-05 14:21:36 +02004694 if (WARN_ON_ONCE(beacon->csa_counter_offsets[0] > beacon_data_len))
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004695 goto out;
4696
Michal Kazioraf296bd2014-06-05 14:21:36 +02004697 if (beacon_data[beacon->csa_counter_offsets[0]] == 1)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004698 ret = true;
4699 out:
4700 rcu_read_unlock();
4701
4702 return ret;
4703}
4704EXPORT_SYMBOL(ieee80211_csa_is_complete);
4705
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004706static int ieee80211_beacon_protect(struct sk_buff *skb,
4707 struct ieee80211_local *local,
4708 struct ieee80211_sub_if_data *sdata)
4709{
4710 ieee80211_tx_result res;
4711 struct ieee80211_tx_data tx;
Johannes Berg95247702020-03-20 10:20:23 +01004712 struct sk_buff *check_skb;
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004713
4714 memset(&tx, 0, sizeof(tx));
4715 tx.key = rcu_dereference(sdata->default_beacon_key);
4716 if (!tx.key)
4717 return 0;
4718 tx.local = local;
4719 tx.sdata = sdata;
4720 __skb_queue_head_init(&tx.skbs);
4721 __skb_queue_tail(&tx.skbs, skb);
4722 res = ieee80211_tx_h_encrypt(&tx);
Johannes Berg95247702020-03-20 10:20:23 +01004723 check_skb = __skb_dequeue(&tx.skbs);
4724 /* we may crash after this, but it'd be a bug in crypto */
4725 WARN_ON(check_skb != skb);
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004726 if (WARN_ON_ONCE(res != TX_CONTINUE))
Johannes Berg95247702020-03-20 10:20:23 +01004727 return -EINVAL;
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004728
4729 return 0;
4730}
4731
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004732static struct sk_buff *
4733__ieee80211_beacon_get(struct ieee80211_hw *hw,
4734 struct ieee80211_vif *vif,
4735 struct ieee80211_mutable_offsets *offs,
4736 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02004737{
4738 struct ieee80211_local *local = hw_to_local(hw);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004739 struct beacon_data *beacon = NULL;
Johannes Berg9d139c82008-07-09 14:40:37 +02004740 struct sk_buff *skb = NULL;
Johannes Berge039fa42008-05-15 12:55:29 +02004741 struct ieee80211_tx_info *info;
Johannes Berge2ebc742007-07-27 15:43:22 +02004742 struct ieee80211_sub_if_data *sdata = NULL;
Johannes Berg57fbcce2016-04-12 15:56:15 +02004743 enum nl80211_band band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004744 struct ieee80211_tx_rate_control txrc;
Johannes Berg55de9082012-07-26 17:24:39 +02004745 struct ieee80211_chanctx_conf *chanctx_conf;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004746 int csa_off_base = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004747
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004748 rcu_read_lock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004749
Johannes Berg32bfd352007-12-19 01:31:26 +01004750 sdata = vif_to_sdata(vif);
Johannes Berg55de9082012-07-26 17:24:39 +02004751 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berge2ebc742007-07-27 15:43:22 +02004752
Johannes Berg55de9082012-07-26 17:24:39 +02004753 if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
Felix Fietkaueb3e5542011-01-24 19:28:49 +01004754 goto out;
4755
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004756 if (offs)
4757 memset(offs, 0, sizeof(*offs));
Johannes Bergeddcbb942009-10-29 08:30:35 +01004758
Johannes Berg05c914f2008-09-11 00:01:58 +02004759 if (sdata->vif.type == NL80211_IFTYPE_AP) {
Marco Porschd012a602012-10-10 12:39:50 -07004760 struct ieee80211_if_ap *ap = &sdata->u.ap;
Marco Porschd012a602012-10-10 12:39:50 -07004761
Michal Kazioraf296bd2014-06-05 14:21:36 +02004762 beacon = rcu_dereference(ap->beacon);
Dan Carpenter3ad97fb2011-02-07 22:03:35 +03004763 if (beacon) {
Michal Kazior10d78f22014-06-05 14:21:37 +02004764 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004765 if (!is_template)
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004766 __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004767
4768 ieee80211_set_csa(sdata, beacon);
4769 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004770
Johannes Berg902acc72008-02-23 15:17:19 +01004771 /*
4772 * headroom, head length,
4773 * tail length and maximum TIM length
4774 */
4775 skb = dev_alloc_skb(local->tx_headroom +
4776 beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004777 beacon->tail_len + 256 +
4778 local->hw.extra_beacon_tailroom);
Johannes Berg902acc72008-02-23 15:17:19 +01004779 if (!skb)
4780 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004781
Johannes Berg902acc72008-02-23 15:17:19 +01004782 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004783 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg902acc72008-02-23 15:17:19 +01004784
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004785 ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
4786 is_template);
Johannes Berg902acc72008-02-23 15:17:19 +01004787
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004788 if (offs) {
4789 offs->tim_offset = beacon->head_len;
4790 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004791
4792 /* for AP the csa offsets are from tail */
4793 csa_off_base = skb->len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004794 }
Johannes Bergeddcbb942009-10-29 08:30:35 +01004795
Johannes Berg902acc72008-02-23 15:17:19 +01004796 if (beacon->tail)
Johannes Berg59ae1d12017-06-16 14:29:20 +02004797 skb_put_data(skb, beacon->tail,
4798 beacon->tail_len);
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004799
4800 if (ieee80211_beacon_protect(skb, local, sdata) < 0)
4801 goto out;
Johannes Berg9d139c82008-07-09 14:40:37 +02004802 } else
4803 goto out;
Johannes Berg05c914f2008-09-11 00:01:58 +02004804 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Berg46900292009-02-15 12:44:28 +01004805 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg9d139c82008-07-09 14:40:37 +02004806 struct ieee80211_hdr *hdr;
Johannes Berg902acc72008-02-23 15:17:19 +01004807
Michal Kazioraf296bd2014-06-05 14:21:36 +02004808 beacon = rcu_dereference(ifibss->presp);
4809 if (!beacon)
Johannes Berg9d139c82008-07-09 14:40:37 +02004810 goto out;
4811
Michal Kazior10d78f22014-06-05 14:21:37 +02004812 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004813 if (!is_template)
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004814 __ieee80211_csa_update_counter(beacon);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004815
Michal Kazioraf296bd2014-06-05 14:21:36 +02004816 ieee80211_set_csa(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004817 }
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004818
Michal Kazioraf296bd2014-06-05 14:21:36 +02004819 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004820 local->hw.extra_beacon_tailroom);
Johannes Berg9d139c82008-07-09 14:40:37 +02004821 if (!skb)
4822 goto out;
Johannes Bergc3ffeab2013-03-07 20:54:29 +01004823 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004824 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004825
4826 hdr = (struct ieee80211_hdr *) skb->data;
Harvey Harrisone7827a72008-07-15 18:44:13 -07004827 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4828 IEEE80211_STYPE_BEACON);
Johannes Berg902acc72008-02-23 15:17:19 +01004829 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
Javier Cardonadbf498f2012-03-31 11:31:32 -07004830 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg472dbc42008-09-11 00:01:49 +02004831
Michal Kazioraf296bd2014-06-05 14:21:36 +02004832 beacon = rcu_dereference(ifmsh->beacon);
4833 if (!beacon)
Johannes Bergac1bd842011-01-18 13:45:32 +01004834 goto out;
Johannes Bergac1bd842011-01-18 13:45:32 +01004835
Michal Kazior10d78f22014-06-05 14:21:37 +02004836 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004837 if (!is_template)
4838 /* TODO: For mesh csa_counter is in TU, so
4839 * decrementing it by one isn't correct, but
4840 * for now we leave it consistent with overall
4841 * mac80211's behavior.
4842 */
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004843 __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004844
Michal Kazioraf296bd2014-06-05 14:21:36 +02004845 ieee80211_set_csa(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004846 }
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004847
Javier Cardonadbf498f2012-03-31 11:31:32 -07004848 if (ifmsh->sync_ops)
Masashi Honma445cd452016-12-08 10:15:51 +09004849 ifmsh->sync_ops->adjust_tsf(sdata, beacon);
Javier Cardonadbf498f2012-03-31 11:31:32 -07004850
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -07004851 skb = dev_alloc_skb(local->tx_headroom +
Michal Kazioraf296bd2014-06-05 14:21:36 +02004852 beacon->head_len +
Marco Porsch3f52b7e2013-01-30 18:14:08 +01004853 256 + /* TIM IE */
Michal Kazioraf296bd2014-06-05 14:21:36 +02004854 beacon->tail_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004855 local->hw.extra_beacon_tailroom);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01004856 if (!skb)
4857 goto out;
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08004858 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004859 skb_put_data(skb, beacon->head, beacon->head_len);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004860 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
4861
4862 if (offs) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004863 offs->tim_offset = beacon->head_len;
4864 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004865 }
4866
Johannes Berg59ae1d12017-06-16 14:29:20 +02004867 skb_put_data(skb, beacon->tail, beacon->tail_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004868 } else {
4869 WARN_ON(1);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004870 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02004871 }
4872
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004873 /* CSA offsets */
Michal Kazioraf296bd2014-06-05 14:21:36 +02004874 if (offs && beacon) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004875 int i;
4876
4877 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004878 u16 csa_off = beacon->csa_counter_offsets[i];
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004879
4880 if (!csa_off)
4881 continue;
4882
4883 offs->csa_counter_offs[i] = csa_off_base + csa_off;
4884 }
4885 }
4886
Johannes Berg4bf88532012-11-09 11:39:59 +01004887 band = chanctx_conf->def.chan->band;
Johannes Berg55de9082012-07-26 17:24:39 +02004888
Johannes Berge039fa42008-05-15 12:55:29 +02004889 info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02004890
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004891 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004892 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Johannes Berge039fa42008-05-15 12:55:29 +02004893 info->band = band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004894
4895 memset(&txrc, 0, sizeof(txrc));
4896 txrc.hw = hw;
Johannes Berge31583c2012-07-26 14:07:46 +02004897 txrc.sband = local->hw.wiphy->bands[band];
Jouni Malinene00cfce2009-12-29 12:59:19 +02004898 txrc.bss_conf = &sdata->vif.bss_conf;
4899 txrc.skb = skb;
4900 txrc.reported_rate.idx = -1;
Jouni Malinen08fad432020-04-25 18:57:12 +03004901 if (sdata->beacon_rate_set && sdata->beacon_rateidx_mask[band])
4902 txrc.rate_idx_mask = sdata->beacon_rateidx_mask[band];
4903 else
4904 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
Felix Fietkau8f0729b2010-11-11 15:07:23 +01004905 txrc.bss = true;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004906 rate_control_get_rate(sdata, NULL, &txrc);
Johannes Berge039fa42008-05-15 12:55:29 +02004907
4908 info->control.vif = vif;
Johannes Bergf591fa52008-07-10 11:21:26 +02004909
John W. Linvillea472e712010-05-06 14:45:17 -04004910 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
4911 IEEE80211_TX_CTL_ASSIGN_SEQ |
4912 IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge6a98542008-10-21 12:40:02 +02004913 out:
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004914 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004915 return skb;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004916
4917}
4918
4919struct sk_buff *
4920ieee80211_beacon_get_template(struct ieee80211_hw *hw,
4921 struct ieee80211_vif *vif,
4922 struct ieee80211_mutable_offsets *offs)
4923{
4924 return __ieee80211_beacon_get(hw, vif, offs, true);
4925}
4926EXPORT_SYMBOL(ieee80211_beacon_get_template);
4927
4928struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
4929 struct ieee80211_vif *vif,
4930 u16 *tim_offset, u16 *tim_length)
4931{
4932 struct ieee80211_mutable_offsets offs = {};
4933 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
Helmut Schaa35afa582015-09-09 09:46:32 +02004934 struct sk_buff *copy;
4935 struct ieee80211_supported_band *sband;
4936 int shift;
4937
4938 if (!bcn)
4939 return bcn;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004940
4941 if (tim_offset)
4942 *tim_offset = offs.tim_offset;
4943
4944 if (tim_length)
4945 *tim_length = offs.tim_length;
4946
Helmut Schaa35afa582015-09-09 09:46:32 +02004947 if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
4948 !hw_to_local(hw)->monitors)
4949 return bcn;
4950
4951 /* send a copy to monitor interfaces */
4952 copy = skb_copy(bcn, GFP_ATOMIC);
4953 if (!copy)
4954 return bcn;
4955
4956 shift = ieee80211_vif_get_shift(vif);
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05304957 sband = ieee80211_get_sband(vif_to_sdata(vif));
4958 if (!sband)
4959 return bcn;
4960
John Crispinb7b2e8c2019-07-14 17:44:15 +02004961 ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false,
4962 NULL);
Helmut Schaa35afa582015-09-09 09:46:32 +02004963
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004964 return bcn;
Johannes Berge2ebc742007-07-27 15:43:22 +02004965}
Johannes Bergeddcbb942009-10-29 08:30:35 +01004966EXPORT_SYMBOL(ieee80211_beacon_get_tim);
Johannes Berge2ebc742007-07-27 15:43:22 +02004967
Arik Nemtsov02945822011-11-10 11:28:57 +02004968struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
4969 struct ieee80211_vif *vif)
4970{
4971 struct ieee80211_if_ap *ap = NULL;
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004972 struct sk_buff *skb = NULL;
4973 struct probe_resp *presp = NULL;
Arik Nemtsov02945822011-11-10 11:28:57 +02004974 struct ieee80211_hdr *hdr;
4975 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4976
4977 if (sdata->vif.type != NL80211_IFTYPE_AP)
4978 return NULL;
4979
4980 rcu_read_lock();
4981
4982 ap = &sdata->u.ap;
4983 presp = rcu_dereference(ap->probe_resp);
4984 if (!presp)
4985 goto out;
4986
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004987 skb = dev_alloc_skb(presp->len);
Arik Nemtsov02945822011-11-10 11:28:57 +02004988 if (!skb)
4989 goto out;
4990
Johannes Berg59ae1d12017-06-16 14:29:20 +02004991 skb_put_data(skb, presp->data, presp->len);
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004992
Arik Nemtsov02945822011-11-10 11:28:57 +02004993 hdr = (struct ieee80211_hdr *) skb->data;
4994 memset(hdr->addr1, 0, sizeof(hdr->addr1));
4995
4996out:
4997 rcu_read_unlock();
4998 return skb;
4999}
5000EXPORT_SYMBOL(ieee80211_proberesp_get);
5001
Kalle Valo7044cc52010-01-05 20:16:19 +02005002struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
5003 struct ieee80211_vif *vif)
5004{
5005 struct ieee80211_sub_if_data *sdata;
5006 struct ieee80211_if_managed *ifmgd;
5007 struct ieee80211_pspoll *pspoll;
5008 struct ieee80211_local *local;
5009 struct sk_buff *skb;
5010
5011 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5012 return NULL;
5013
5014 sdata = vif_to_sdata(vif);
5015 ifmgd = &sdata->u.mgd;
5016 local = sdata->local;
5017
5018 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
Joe Perchesd15b8452011-08-29 14:17:31 -07005019 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02005020 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07005021
Kalle Valo7044cc52010-01-05 20:16:19 +02005022 skb_reserve(skb, local->hw.extra_tx_headroom);
5023
Johannes Bergb080db52017-06-16 14:29:19 +02005024 pspoll = skb_put_zero(skb, sizeof(*pspoll));
Kalle Valo7044cc52010-01-05 20:16:19 +02005025 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
5026 IEEE80211_STYPE_PSPOLL);
Johannes Berg1db364c2020-04-17 12:38:04 +02005027 pspoll->aid = cpu_to_le16(sdata->vif.bss_conf.aid);
Kalle Valo7044cc52010-01-05 20:16:19 +02005028
5029 /* aid in PS-Poll has its two MSBs each set to 1 */
5030 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
5031
5032 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
5033 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
5034
5035 return skb;
5036}
5037EXPORT_SYMBOL(ieee80211_pspoll_get);
5038
5039struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005040 struct ieee80211_vif *vif,
5041 bool qos_ok)
Kalle Valo7044cc52010-01-05 20:16:19 +02005042{
5043 struct ieee80211_hdr_3addr *nullfunc;
5044 struct ieee80211_sub_if_data *sdata;
5045 struct ieee80211_if_managed *ifmgd;
5046 struct ieee80211_local *local;
5047 struct sk_buff *skb;
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005048 bool qos = false;
Kalle Valo7044cc52010-01-05 20:16:19 +02005049
5050 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5051 return NULL;
5052
5053 sdata = vif_to_sdata(vif);
5054 ifmgd = &sdata->u.mgd;
5055 local = sdata->local;
5056
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005057 if (qos_ok) {
5058 struct sta_info *sta;
5059
5060 rcu_read_lock();
5061 sta = sta_info_get(sdata, ifmgd->bssid);
5062 qos = sta && sta->sta.wme;
5063 rcu_read_unlock();
5064 }
5065
5066 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5067 sizeof(*nullfunc) + 2);
Joe Perchesd15b8452011-08-29 14:17:31 -07005068 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02005069 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07005070
Kalle Valo7044cc52010-01-05 20:16:19 +02005071 skb_reserve(skb, local->hw.extra_tx_headroom);
5072
Johannes Bergb080db52017-06-16 14:29:19 +02005073 nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
Kalle Valo7044cc52010-01-05 20:16:19 +02005074 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
5075 IEEE80211_STYPE_NULLFUNC |
5076 IEEE80211_FCTL_TODS);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005077 if (qos) {
Johannes Berge0ba7092018-11-09 11:16:46 +01005078 __le16 qoshdr = cpu_to_le16(7);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005079
5080 BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
5081 IEEE80211_STYPE_NULLFUNC) !=
5082 IEEE80211_STYPE_QOS_NULLFUNC);
5083 nullfunc->frame_control |=
5084 cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
5085 skb->priority = 7;
5086 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
Johannes Berge0ba7092018-11-09 11:16:46 +01005087 skb_put_data(skb, &qoshdr, sizeof(qoshdr));
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005088 }
5089
Kalle Valo7044cc52010-01-05 20:16:19 +02005090 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
5091 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
5092 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
5093
5094 return skb;
5095}
5096EXPORT_SYMBOL(ieee80211_nullfunc_get);
5097
Kalle Valo05e54ea2010-01-05 20:16:38 +02005098struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
Johannes Berga344d672014-06-12 22:24:31 +02005099 const u8 *src_addr,
Kalle Valo05e54ea2010-01-05 20:16:38 +02005100 const u8 *ssid, size_t ssid_len,
Johannes Bergb9a9ada2012-11-29 13:00:10 +01005101 size_t tailroom)
Kalle Valo05e54ea2010-01-05 20:16:38 +02005102{
Johannes Berga344d672014-06-12 22:24:31 +02005103 struct ieee80211_local *local = hw_to_local(hw);
Kalle Valo05e54ea2010-01-05 20:16:38 +02005104 struct ieee80211_hdr_3addr *hdr;
5105 struct sk_buff *skb;
5106 size_t ie_ssid_len;
5107 u8 *pos;
5108
Kalle Valo05e54ea2010-01-05 20:16:38 +02005109 ie_ssid_len = 2 + ssid_len;
5110
5111 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
Johannes Bergb9a9ada2012-11-29 13:00:10 +01005112 ie_ssid_len + tailroom);
Joe Perchesd15b8452011-08-29 14:17:31 -07005113 if (!skb)
Kalle Valo05e54ea2010-01-05 20:16:38 +02005114 return NULL;
Kalle Valo05e54ea2010-01-05 20:16:38 +02005115
5116 skb_reserve(skb, local->hw.extra_tx_headroom);
5117
Johannes Bergb080db52017-06-16 14:29:19 +02005118 hdr = skb_put_zero(skb, sizeof(*hdr));
Kalle Valo05e54ea2010-01-05 20:16:38 +02005119 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
5120 IEEE80211_STYPE_PROBE_REQ);
Johannes Berge83e6542012-07-13 16:23:07 +02005121 eth_broadcast_addr(hdr->addr1);
Johannes Berga344d672014-06-12 22:24:31 +02005122 memcpy(hdr->addr2, src_addr, ETH_ALEN);
Johannes Berge83e6542012-07-13 16:23:07 +02005123 eth_broadcast_addr(hdr->addr3);
Kalle Valo05e54ea2010-01-05 20:16:38 +02005124
5125 pos = skb_put(skb, ie_ssid_len);
5126 *pos++ = WLAN_EID_SSID;
5127 *pos++ = ssid_len;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02005128 if (ssid_len)
Kalle Valo05e54ea2010-01-05 20:16:38 +02005129 memcpy(pos, ssid, ssid_len);
5130 pos += ssid_len;
5131
Kalle Valo05e54ea2010-01-05 20:16:38 +02005132 return skb;
5133}
5134EXPORT_SYMBOL(ieee80211_probereq_get);
5135
Johannes Berg32bfd352007-12-19 01:31:26 +01005136void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02005137 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02005138 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02005139 struct ieee80211_rts *rts)
5140{
5141 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02005142
Harvey Harrison065e96052008-06-22 16:45:27 -07005143 rts->frame_control =
5144 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01005145 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
5146 frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02005147 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
5148 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
5149}
5150EXPORT_SYMBOL(ieee80211_rts_get);
5151
Johannes Berg32bfd352007-12-19 01:31:26 +01005152void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02005153 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02005154 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02005155 struct ieee80211_cts *cts)
5156{
5157 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02005158
Harvey Harrison065e96052008-06-22 16:45:27 -07005159 cts->frame_control =
5160 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01005161 cts->duration = ieee80211_ctstoself_duration(hw, vif,
5162 frame_len, frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02005163 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
5164}
5165EXPORT_SYMBOL(ieee80211_ctstoself_get);
5166
5167struct sk_buff *
Johannes Berg32bfd352007-12-19 01:31:26 +01005168ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
Johannes Berge039fa42008-05-15 12:55:29 +02005169 struct ieee80211_vif *vif)
Johannes Berge2ebc742007-07-27 15:43:22 +02005170{
5171 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winkler747cf5e2008-05-27 17:50:51 +03005172 struct sk_buff *skb = NULL;
Johannes Berg5cf121c2008-02-25 16:27:43 +01005173 struct ieee80211_tx_data tx;
Johannes Berge2ebc742007-07-27 15:43:22 +02005174 struct ieee80211_sub_if_data *sdata;
Marco Porschd012a602012-10-10 12:39:50 -07005175 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +02005176 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02005177 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berge2ebc742007-07-27 15:43:22 +02005178
Johannes Berg32bfd352007-12-19 01:31:26 +01005179 sdata = vif_to_sdata(vif);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005180
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005181 rcu_read_lock();
Johannes Berg55de9082012-07-26 17:24:39 +02005182 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005183
Marco Porschd012a602012-10-10 12:39:50 -07005184 if (!chanctx_conf)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03005185 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005186
Marco Porschd012a602012-10-10 12:39:50 -07005187 if (sdata->vif.type == NL80211_IFTYPE_AP) {
5188 struct beacon_data *beacon =
5189 rcu_dereference(sdata->u.ap.beacon);
5190
5191 if (!beacon || !beacon->head)
5192 goto out;
5193
5194 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01005195 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
5196 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07005197 } else {
5198 goto out;
5199 }
5200
5201 if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03005202 goto out; /* send buffered bc/mc only after DTIM beacon */
Johannes Berge039fa42008-05-15 12:55:29 +02005203
Johannes Berge2ebc742007-07-27 15:43:22 +02005204 while (1) {
Marco Porschd012a602012-10-10 12:39:50 -07005205 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +02005206 if (!skb)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03005207 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02005208 local->total_ps_buffered--;
5209
Marco Porschd012a602012-10-10 12:39:50 -07005210 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
Johannes Berge2ebc742007-07-27 15:43:22 +02005211 struct ieee80211_hdr *hdr =
5212 (struct ieee80211_hdr *) skb->data;
5213 /* more buffered multicast/broadcast frames ==> set
5214 * MoreData flag in IEEE 802.11 header to inform PS
5215 * STAs */
5216 hdr->frame_control |=
5217 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
5218 }
5219
Michael Braun112c44b2014-03-06 15:08:43 +01005220 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porsch7cbf9d02013-03-01 16:01:18 +01005221 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
Johannes Berg7c107702015-03-20 14:18:27 +01005222 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
Johannes Berge2ebc742007-07-27 15:43:22 +02005223 break;
Felix Fietkau6b07d9c2016-08-02 11:13:41 +02005224 ieee80211_free_txskb(hw, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02005225 }
Johannes Berge039fa42008-05-15 12:55:29 +02005226
5227 info = IEEE80211_SKB_CB(skb);
5228
Johannes Berg5cf121c2008-02-25 16:27:43 +01005229 tx.flags |= IEEE80211_TX_PS_BUFFERED;
Johannes Berg4bf88532012-11-09 11:39:59 +01005230 info->band = chanctx_conf->def.chan->band;
Johannes Berge2ebc742007-07-27 15:43:22 +02005231
Johannes Berg97b045d2008-06-20 01:22:30 +02005232 if (invoke_tx_handlers(&tx))
Johannes Berge2ebc742007-07-27 15:43:22 +02005233 skb = NULL;
Johannes Berg97b045d2008-06-20 01:22:30 +02005234 out:
Johannes Bergd0709a62008-02-25 16:27:46 +01005235 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02005236
5237 return skb;
5238}
5239EXPORT_SYMBOL(ieee80211_get_buffered_bc);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005240
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005241int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5242{
5243 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5244 struct ieee80211_sub_if_data *sdata = sta->sdata;
5245 struct ieee80211_local *local = sdata->local;
5246 int ret;
5247 u32 queues;
5248
5249 lockdep_assert_held(&local->sta_mtx);
5250
5251 /* only some cases are supported right now */
5252 switch (sdata->vif.type) {
5253 case NL80211_IFTYPE_STATION:
5254 case NL80211_IFTYPE_AP:
5255 case NL80211_IFTYPE_AP_VLAN:
5256 break;
5257 default:
5258 WARN_ON(1);
5259 return -EINVAL;
5260 }
5261
5262 if (WARN_ON(tid >= IEEE80211_NUM_UPS))
5263 return -EINVAL;
5264
5265 if (sta->reserved_tid == tid) {
5266 ret = 0;
5267 goto out;
5268 }
5269
5270 if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
5271 sdata_err(sdata, "TID reservation already active\n");
5272 ret = -EALREADY;
5273 goto out;
5274 }
5275
5276 ieee80211_stop_vif_queues(sdata->local, sdata,
5277 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5278
5279 synchronize_net();
5280
5281 /* Tear down BA sessions so we stop aggregating on this TID */
Johannes Berg30686bf2015-06-02 21:39:54 +02005282 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005283 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5284 __ieee80211_stop_tx_ba_session(sta, tid,
5285 AGG_STOP_LOCAL_REQUEST);
5286 }
5287
5288 queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02005289 __ieee80211_flush_queues(local, sdata, queues, false);
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005290
5291 sta->reserved_tid = tid;
5292
5293 ieee80211_wake_vif_queues(local, sdata,
5294 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5295
Johannes Berg30686bf2015-06-02 21:39:54 +02005296 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005297 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5298
5299 ret = 0;
5300 out:
5301 return ret;
5302}
5303EXPORT_SYMBOL(ieee80211_reserve_tid);
5304
5305void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5306{
5307 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5308 struct ieee80211_sub_if_data *sdata = sta->sdata;
5309
5310 lockdep_assert_held(&sdata->local->sta_mtx);
5311
5312 /* only some cases are supported right now */
5313 switch (sdata->vif.type) {
5314 case NL80211_IFTYPE_STATION:
5315 case NL80211_IFTYPE_AP:
5316 case NL80211_IFTYPE_AP_VLAN:
5317 break;
5318 default:
5319 WARN_ON(1);
5320 return;
5321 }
5322
5323 if (tid != sta->reserved_tid) {
5324 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
5325 return;
5326 }
5327
5328 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
5329}
5330EXPORT_SYMBOL(ieee80211_unreserve_tid);
5331
Johannes Berg55de9082012-07-26 17:24:39 +02005332void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
5333 struct sk_buff *skb, int tid,
Mathy Vanhoef08aca292020-07-23 14:01:52 +04005334 enum nl80211_band band)
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005335{
Amadeusz Sławiński731977e2017-01-24 16:42:10 +01005336 int ac = ieee80211_ac_from_tid(tid);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02005337
Zhang Shengjud57a5442016-03-03 01:16:56 +00005338 skb_reset_mac_header(skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02005339 skb_set_queue_mapping(skb, ac);
Helmut Schaacf6bb792011-12-15 10:18:34 +01005340 skb->priority = tid;
Johannes Bergcf0277e2010-01-05 18:00:58 +01005341
Johannes Berg89afe612013-02-13 15:39:57 +01005342 skb->dev = sdata->dev;
5343
Johannes Berg3d34deb2009-06-18 17:25:11 +02005344 /*
5345 * The other path calling ieee80211_xmit is from the tasklet,
5346 * and while we can handle concurrent transmissions locking
5347 * requirements are that we do not come into tx with bhs on.
5348 */
5349 local_bh_disable();
Johannes Berg73c4e192014-11-09 18:50:09 +02005350 IEEE80211_SKB_CB(skb)->band = band;
Mathy Vanhoef08aca292020-07-23 14:01:52 +04005351 ieee80211_xmit(sdata, NULL, skb);
Johannes Berg3d34deb2009-06-18 17:25:11 +02005352 local_bh_enable();
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005353}
Denis Kenzior91180642018-03-26 12:52:50 -05005354
5355int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
5356 const u8 *buf, size_t len,
Markus Theildca9ca22020-05-08 16:42:00 +02005357 const u8 *dest, __be16 proto, bool unencrypted,
5358 u64 *cookie)
Denis Kenzior91180642018-03-26 12:52:50 -05005359{
5360 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5361 struct ieee80211_local *local = sdata->local;
5362 struct sk_buff *skb;
5363 struct ethhdr *ehdr;
Johannes Bergb95d2cc2020-03-26 15:53:34 +01005364 u32 ctrl_flags = 0;
Markus Theila7528192020-05-27 18:03:34 +02005365 u32 flags = 0;
Denis Kenzior91180642018-03-26 12:52:50 -05005366
5367 /* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
5368 * or Pre-Authentication
5369 */
5370 if (proto != sdata->control_port_protocol &&
5371 proto != cpu_to_be16(ETH_P_PREAUTH))
5372 return -EINVAL;
5373
Johannes Bergb95d2cc2020-03-26 15:53:34 +01005374 if (proto == sdata->control_port_protocol)
Markus Theil5af7fef2020-06-17 10:26:36 +02005375 ctrl_flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO |
5376 IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
Johannes Bergb95d2cc2020-03-26 15:53:34 +01005377
Denis Kenzior91180642018-03-26 12:52:50 -05005378 if (unencrypted)
Markus Theila7528192020-05-27 18:03:34 +02005379 flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
5380
5381 if (cookie)
5382 ctrl_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
5383
5384 flags |= IEEE80211_TX_INTFL_NL80211_FRAME_TX |
5385 IEEE80211_TX_CTL_INJECTED;
Denis Kenzior91180642018-03-26 12:52:50 -05005386
5387 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5388 sizeof(struct ethhdr) + len);
5389 if (!skb)
5390 return -ENOMEM;
5391
5392 skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
5393
5394 skb_put_data(skb, buf, len);
5395
5396 ehdr = skb_push(skb, sizeof(struct ethhdr));
5397 memcpy(ehdr->h_dest, dest, ETH_ALEN);
Johannes Berg8079e4f2020-02-24 10:19:11 +01005398 memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
Denis Kenzior91180642018-03-26 12:52:50 -05005399 ehdr->h_proto = proto;
5400
5401 skb->dev = dev;
5402 skb->protocol = htons(ETH_P_802_3);
5403 skb_reset_network_header(skb);
5404 skb_reset_mac_header(skb);
5405
Markus Theila7528192020-05-27 18:03:34 +02005406 /* mutex lock is only needed for incrementing the cookie counter */
5407 mutex_lock(&local->mtx);
5408
Denis Kenziore7441c92018-06-19 10:39:50 -05005409 local_bh_disable();
Markus Theila7528192020-05-27 18:03:34 +02005410 __ieee80211_subif_start_xmit(skb, skb->dev, flags, ctrl_flags, cookie);
Denis Kenziore7441c92018-06-19 10:39:50 -05005411 local_bh_enable();
Denis Kenzior91180642018-03-26 12:52:50 -05005412
Markus Theila7528192020-05-27 18:03:34 +02005413 mutex_unlock(&local->mtx);
5414
Denis Kenzior91180642018-03-26 12:52:50 -05005415 return 0;
5416}
Rajkumar Manoharan8828f812019-04-11 13:47:26 -07005417
5418int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
5419 const u8 *buf, size_t len)
5420{
5421 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5422 struct ieee80211_local *local = sdata->local;
5423 struct sk_buff *skb;
5424
5425 skb = dev_alloc_skb(local->hw.extra_tx_headroom + len +
5426 30 + /* header size */
5427 18); /* 11s header size */
5428 if (!skb)
5429 return -ENOMEM;
5430
5431 skb_reserve(skb, local->hw.extra_tx_headroom);
5432 skb_put_data(skb, buf, len);
5433
5434 skb->dev = dev;
5435 skb->protocol = htons(ETH_P_802_3);
5436 skb_reset_network_header(skb);
5437 skb_reset_mac_header(skb);
5438
5439 local_bh_disable();
5440 __ieee80211_subif_start_xmit(skb, skb->dev, 0,
Markus Theila7528192020-05-27 18:03:34 +02005441 IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP,
5442 NULL);
Johannes Berge2ebc742007-07-27 15:43:22 +02005443 local_bh_enable();
5444
5445 return 0;
5446}