blob: f13eb2f61ccfc7d53b85f8dc2ec4140e55b2afde [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
Sara Sharon914eac22018-04-20 13:49:19 +03008 * Copyright (C) 2018 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:
Johannes Berg8318d782008-01-24 19:38:38 +0100165 if (r->flags & IEEE80211_RATE_MANDATORY_A)
166 mrate = r->bitrate;
167 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200168 case NL80211_BAND_60GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300169 /* TODO, for now fall through */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200170 case NUM_NL80211_BANDS:
Johannes Berg8318d782008-01-24 19:38:38 +0100171 WARN_ON(1);
172 break;
173 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200174 }
175 if (rate == -1) {
176 /* No matching basic rate found; use highest suitable mandatory
177 * PHY rate */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200178 rate = DIV_ROUND_UP(mrate, 1 << shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200179 }
180
Simon Wunderlich6674f212011-11-21 21:34:30 +0100181 /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
182 if (ieee80211_is_data_qos(hdr->frame_control) &&
Claudio Pisac26a0e12012-05-28 13:06:25 +0100183 *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
Simon Wunderlich6674f212011-11-21 21:34:30 +0100184 dur = 0;
185 else
186 /* Time needed to transmit ACK
187 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
188 * to closest integer */
Michal Kazior4ee73f32012-04-11 08:47:56 +0200189 dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200190 tx->sdata->vif.bss_conf.use_short_preamble,
191 shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200192
193 if (next_frag_len) {
194 /* Frame is fragmented: duration increases with time needed to
195 * transmit next fragment plus ACK and 2 x SIFS. */
196 dur *= 2; /* ACK + SIFS */
197 /* next fragment */
Michal Kazior4ee73f32012-04-11 08:47:56 +0200198 dur += ieee80211_frame_duration(sband->band, next_frag_len,
Johannes Berg8318d782008-01-24 19:38:38 +0100199 txrate->bitrate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200200 tx->sdata->vif.bss_conf.use_short_preamble,
201 shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200202 }
203
Johannes Berg03f93c32008-06-25 14:36:27 +0300204 return cpu_to_le16(dur);
Johannes Berge2ebc742007-07-27 15:43:22 +0200205}
206
Johannes Berge2ebc742007-07-27 15:43:22 +0200207/* tx handlers */
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200208static ieee80211_tx_result debug_noinline
209ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
210{
211 struct ieee80211_local *local = tx->local;
Kalle Valo0c742112010-01-12 10:42:53 +0200212 struct ieee80211_if_managed *ifmgd;
Andrei Otcheretianski94a5b3a2018-09-05 08:06:14 +0300213 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200214
215 /* driver doesn't support power save */
Johannes Berg30686bf2015-06-02 21:39:54 +0200216 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200217 return TX_CONTINUE;
218
219 /* hardware does dynamic power save */
Johannes Berg30686bf2015-06-02 21:39:54 +0200220 if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200221 return TX_CONTINUE;
222
223 /* dynamic power save disabled */
224 if (local->hw.conf.dynamic_ps_timeout <= 0)
225 return TX_CONTINUE;
226
227 /* we are scanning, don't enable power save */
228 if (local->scanning)
229 return TX_CONTINUE;
230
231 if (!local->ps_sdata)
232 return TX_CONTINUE;
233
234 /* No point if we're going to suspend */
235 if (local->quiescing)
236 return TX_CONTINUE;
237
Kalle Valo0c742112010-01-12 10:42:53 +0200238 /* dynamic ps is supported only in managed mode */
239 if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
240 return TX_CONTINUE;
241
Andrei Otcheretianski94a5b3a2018-09-05 08:06:14 +0300242 if (unlikely(info->flags & IEEE80211_TX_INTFL_OFFCHAN_TX_OK))
243 return TX_CONTINUE;
244
Kalle Valo0c742112010-01-12 10:42:53 +0200245 ifmgd = &tx->sdata->u.mgd;
246
247 /*
248 * Don't wakeup from power save if u-apsd is enabled, voip ac has
249 * u-apsd enabled and the frame is in voip class. This effectively
250 * means that even if all access categories have u-apsd enabled, in
251 * practise u-apsd is only used with the voip ac. This is a
252 * workaround for the case when received voip class packets do not
253 * have correct qos tag for some reason, due the network or the
254 * peer application.
255 *
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200256 * Note: ifmgd->uapsd_queues access is racy here. If the value is
Kalle Valo0c742112010-01-12 10:42:53 +0200257 * changed via debugfs, user needs to reassociate manually to have
258 * everything in sync.
259 */
Johannes Berg4875d30d2012-03-27 14:18:37 +0200260 if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
261 (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
262 skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
Kalle Valo0c742112010-01-12 10:42:53 +0200263 return TX_CONTINUE;
264
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200265 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
266 ieee80211_stop_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +0100267 IEEE80211_MAX_QUEUE_MAP,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300268 IEEE80211_QUEUE_STOP_REASON_PS,
269 false);
Vivek Natarajandb285692011-02-18 17:18:03 +0530270 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200271 ieee80211_queue_work(&local->hw,
272 &local->dynamic_ps_disable_work);
273 }
274
Luciano Coelho5db1c072011-05-03 21:40:08 +0300275 /* Don't restart the timer if we're not disassociated */
276 if (!ifmgd->associated)
277 return TX_CONTINUE;
278
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200279 mod_timer(&local->dynamic_ps_timer, jiffies +
280 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
281
282 return TX_CONTINUE;
283}
Johannes Berge2ebc742007-07-27 15:43:22 +0200284
Johannes Bergd9e8a702008-06-30 15:10:44 +0200285static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100286ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200287{
Harvey Harrison358c8d92008-07-15 18:44:13 -0700288
Johannes Berge039fa42008-05-15 12:55:29 +0200289 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200290 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200291 bool assoc = false;
Johannes Berge2ebc742007-07-27 15:43:22 +0200292
Johannes Berge039fa42008-05-15 12:55:29 +0200293 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100294 return TX_CONTINUE;
Johannes Berg58d41852007-09-26 17:53:18 +0200295
Ben Greearb23b0252011-02-04 11:54:17 -0800296 if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
297 test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
Kalle Valoa9a6fff2009-03-18 14:06:44 +0200298 !ieee80211_is_probe_req(hdr->frame_control) &&
299 !ieee80211_is_nullfunc(hdr->frame_control))
300 /*
301 * When software scanning only nullfunc frames (to notify
302 * the sleep state to the AP) and probe requests (for the
303 * active scan) are allowed, all other frames should not be
304 * sent and we should not get here, but if we do
305 * nonetheless, drop them to avoid sending them
306 * off-channel. See the link below and
307 * ieee80211_start_scan() for more.
308 *
309 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
310 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100311 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200312
Rostislav Lisovy239281f2014-11-03 10:33:19 +0100313 if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
314 return TX_CONTINUE;
315
Bill Jordan1be7fe82010-10-01 11:20:41 -0400316 if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
317 return TX_CONTINUE;
318
Johannes Berg5cf121c2008-02-25 16:27:43 +0100319 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100320 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200321
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200322 if (tx->sta)
323 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge2ebc742007-07-27 15:43:22 +0200324
Johannes Berg5cf121c2008-02-25 16:27:43 +0100325 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200326 if (unlikely(!assoc &&
Harvey Harrison358c8d92008-07-15 18:44:13 -0700327 ieee80211_is_data(hdr->frame_control))) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200328#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200329 sdata_info(tx->sdata,
330 "dropped data frame to not associated station %pM\n",
331 hdr->addr1);
332#endif
Johannes Berge2ebc742007-07-27 15:43:22 +0200333 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100334 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200335 }
Michael Braun72f15d52016-10-10 19:12:21 +0200336 } else if (unlikely(ieee80211_is_data(hdr->frame_control) &&
337 ieee80211_vif_get_num_mcast_if(tx->sdata) == 0)) {
Johannes Berg29623892011-12-14 12:20:31 +0100338 /*
339 * No associated STAs - no need to send multicast
340 * frames.
341 */
342 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200343 }
344
Johannes Berg9ae54c82008-01-31 19:48:20 +0100345 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200346}
347
Johannes Berge2ebc742007-07-27 15:43:22 +0200348/* This function is called whenever the AP is about to exceed the maximum limit
349 * of buffered frames for power saving STAs. This situation should not really
350 * happen often during normal operation, so dropping the oldest buffered packet
351 * from each queue should be OK to make some room for new frames. */
352static void purge_old_ps_buffers(struct ieee80211_local *local)
353{
354 int total = 0, purged = 0;
355 struct sk_buff *skb;
356 struct ieee80211_sub_if_data *sdata;
357 struct sta_info *sta;
358
Johannes Berg79010422007-09-18 17:29:21 -0400359 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Marco Porschd012a602012-10-10 12:39:50 -0700360 struct ps_data *ps;
361
362 if (sdata->vif.type == NL80211_IFTYPE_AP)
363 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100364 else if (ieee80211_vif_is_mesh(&sdata->vif))
365 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700366 else
Johannes Berge2ebc742007-07-27 15:43:22 +0200367 continue;
Marco Porschd012a602012-10-10 12:39:50 -0700368
369 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +0200370 if (skb) {
371 purged++;
Felix Fietkau6b07d9c2016-08-02 11:13:41 +0200372 ieee80211_free_txskb(&local->hw, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +0200373 }
Marco Porschd012a602012-10-10 12:39:50 -0700374 total += skb_queue_len(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +0200375 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200376
Johannes Berg948d8872011-09-29 16:04:29 +0200377 /*
378 * Drop one frame from each station from the lowest-priority
379 * AC that has frames at all.
380 */
Johannes Bergd0709a62008-02-25 16:27:46 +0100381 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg948d8872011-09-29 16:04:29 +0200382 int ac;
383
384 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
385 skb = skb_dequeue(&sta->ps_tx_buf[ac]);
386 total += skb_queue_len(&sta->ps_tx_buf[ac]);
387 if (skb) {
388 purged++;
Felix Fietkauc3e77242012-10-08 14:39:33 +0200389 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg948d8872011-09-29 16:04:29 +0200390 break;
391 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200392 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200393 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100394
Johannes Berge2ebc742007-07-27 15:43:22 +0200395 local->total_ps_buffered = total;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200396 ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
Johannes Berge2ebc742007-07-27 15:43:22 +0200397}
398
Johannes Berg9ae54c82008-01-31 19:48:20 +0100399static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100400ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200401{
Johannes Berge039fa42008-05-15 12:55:29 +0200402 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700403 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Marco Porschd012a602012-10-10 12:39:50 -0700404 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +0200405
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100406 /*
407 * broadcast/multicast frame
408 *
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100409 * If any of the associated/peer stations is in power save mode,
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100410 * the frame is buffered to be sent after DTIM beacon frame.
411 * This is done either by the hardware or us.
412 */
413
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100414 /* powersaving STAs currently only in AP/VLAN/mesh mode */
Marco Porschd012a602012-10-10 12:39:50 -0700415 if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
416 tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
417 if (!tx->sdata->bss)
418 return TX_CONTINUE;
419
420 ps = &tx->sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100421 } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
422 ps = &tx->sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700423 } else {
Johannes Berg3e122be2008-07-09 14:40:34 +0200424 return TX_CONTINUE;
Marco Porschd012a602012-10-10 12:39:50 -0700425 }
426
Johannes Berg3e122be2008-07-09 14:40:34 +0200427
428 /* no buffering for ordered frames */
Harvey Harrison358c8d92008-07-15 18:44:13 -0700429 if (ieee80211_has_order(hdr->frame_control))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100430 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100431
Johannes Berg08b99392014-07-07 12:01:11 +0200432 if (ieee80211_is_probe_req(hdr->frame_control))
433 return TX_CONTINUE;
434
Johannes Berg30686bf2015-06-02 21:39:54 +0200435 if (ieee80211_hw_check(&tx->local->hw, QUEUE_CONTROL))
Johannes Bergf4d57942013-05-28 17:24:15 +0200436 info->hw_queue = tx->sdata->vif.cab_queue;
437
Felix Fietkau9ec11902018-11-28 22:39:16 +0100438 /* no stations in PS mode and no buffered packets */
439 if (!atomic_read(&ps->num_sta_ps) && skb_queue_empty(&ps->bc_buf))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100440 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100441
Johannes Berg62b517c2009-10-29 12:19:21 +0100442 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
Johannes Berg62b12082009-08-10 16:04:15 +0200443
Johannes Berg62b517c2009-10-29 12:19:21 +0100444 /* device releases frame after DTIM beacon */
Johannes Berg30686bf2015-06-02 21:39:54 +0200445 if (!ieee80211_hw_check(&tx->local->hw, HOST_BROADCAST_PS_BUFFERING))
Johannes Berg62b12082009-08-10 16:04:15 +0200446 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200447
Johannes Berg62b12082009-08-10 16:04:15 +0200448 /* buffered in mac80211 */
449 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
450 purge_old_ps_buffers(tx->local);
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100451
Marco Porschd012a602012-10-10 12:39:50 -0700452 if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200453 ps_dbg(tx->sdata,
454 "BC TX buffer full - dropping the oldest frame\n");
Felix Fietkau6b07d9c2016-08-02 11:13:41 +0200455 ieee80211_free_txskb(&tx->local->hw, skb_dequeue(&ps->bc_buf));
Johannes Berg62b12082009-08-10 16:04:15 +0200456 } else
457 tx->local->total_ps_buffered++;
458
Marco Porschd012a602012-10-10 12:39:50 -0700459 skb_queue_tail(&ps->bc_buf, tx->skb);
Johannes Berg62b12082009-08-10 16:04:15 +0200460
461 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200462}
463
Jouni Malinenfb733332009-01-08 13:32:00 +0200464static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
465 struct sk_buff *skb)
466{
467 if (!ieee80211_is_mgmt(fc))
468 return 0;
469
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200470 if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
Jouni Malinenfb733332009-01-08 13:32:00 +0200471 return 0;
472
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100473 if (!ieee80211_is_robust_mgmt_frame(skb))
Jouni Malinenfb733332009-01-08 13:32:00 +0200474 return 0;
475
476 return 1;
477}
478
Johannes Berg9ae54c82008-01-31 19:48:20 +0100479static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100480ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200481{
482 struct sta_info *sta = tx->sta;
Johannes Berge039fa42008-05-15 12:55:29 +0200483 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berg08b99392014-07-07 12:01:11 +0200484 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300485 struct ieee80211_local *local = tx->local;
Johannes Berge2ebc742007-07-27 15:43:22 +0200486
Johannes Berg02f2f1a2012-02-27 12:18:30 +0100487 if (unlikely(!sta))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100488 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200489
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200490 if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
Johannes Berg5ac2e352014-05-27 16:32:27 +0200491 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
492 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
Johannes Berg02f2f1a2012-02-27 12:18:30 +0100493 !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
Johannes Berg948d8872011-09-29 16:04:29 +0200494 int ac = skb_get_queue_mapping(tx->skb);
495
Johannes Berg08b99392014-07-07 12:01:11 +0200496 if (ieee80211_is_mgmt(hdr->frame_control) &&
497 !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
498 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
499 return TX_CONTINUE;
500 }
501
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200502 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
503 sta->sta.addr, sta->sta.aid, ac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200504 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
505 purge_old_ps_buffers(tx->local);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200506
507 /* sync with ieee80211_sta_ps_deliver_wakeup */
508 spin_lock(&sta->ps_lock);
509 /*
510 * STA woke up the meantime and all the frames on ps_tx_buf have
511 * been queued to pending queue. No reordering can happen, go
512 * ahead and Tx the packet.
513 */
514 if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
Johannes Berg5ac2e352014-05-27 16:32:27 +0200515 !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
516 !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200517 spin_unlock(&sta->ps_lock);
518 return TX_CONTINUE;
519 }
520
Johannes Berg948d8872011-09-29 16:04:29 +0200521 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
522 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200523 ps_dbg(tx->sdata,
524 "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
525 sta->sta.addr, ac);
Felix Fietkauc3e77242012-10-08 14:39:33 +0200526 ieee80211_free_txskb(&local->hw, old);
Johannes Berge2ebc742007-07-27 15:43:22 +0200527 } else
528 tx->local->total_ps_buffered++;
Johannes Berg004c8722008-02-20 11:21:35 +0100529
Johannes Berge039fa42008-05-15 12:55:29 +0200530 info->control.jiffies = jiffies;
Johannes Berg5061b0c2009-07-14 00:33:34 +0200531 info->control.vif = &tx->sdata->vif;
Johannes Berg8f77f382009-06-07 21:58:37 +0200532 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Johannes Berg03c8c062014-01-09 01:45:28 +0100533 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berg948d8872011-09-29 16:04:29 +0200534 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200535 spin_unlock(&sta->ps_lock);
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300536
537 if (!timer_pending(&local->sta_cleanup))
538 mod_timer(&local->sta_cleanup,
539 round_jiffies(jiffies +
540 STA_INFO_CLEANUP_INTERVAL));
541
Johannes Bergc868cb352011-09-29 16:04:27 +0200542 /*
543 * We queued up some frames, so the TIM bit might
544 * need to be set, recalculate it.
545 */
546 sta_info_recalc_tim(sta);
547
Johannes Berg9ae54c82008-01-31 19:48:20 +0100548 return TX_QUEUED;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200549 } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
550 ps_dbg(tx->sdata,
551 "STA %pM in PS mode, but polling/in SP -> send frame\n",
552 sta->sta.addr);
Johannes Berge2ebc742007-07-27 15:43:22 +0200553 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200554
Johannes Berg9ae54c82008-01-31 19:48:20 +0100555 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200556}
557
Johannes Bergd9e8a702008-06-30 15:10:44 +0200558static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100559ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200560{
Johannes Berg5cf121c2008-02-25 16:27:43 +0100561 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100562 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200563
Johannes Berg5cf121c2008-02-25 16:27:43 +0100564 if (tx->flags & IEEE80211_TX_UNICAST)
Johannes Berge2ebc742007-07-27 15:43:22 +0200565 return ieee80211_tx_h_unicast_ps_buf(tx);
566 else
567 return ieee80211_tx_h_multicast_ps_buf(tx);
568}
569
Johannes Bergd9e8a702008-06-30 15:10:44 +0200570static ieee80211_tx_result debug_noinline
Johannes Berga621fa42010-08-27 14:26:54 +0300571ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
572{
573 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
574
Johannes Bergaf61a162013-07-02 18:09:12 +0200575 if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
576 if (tx->sdata->control_port_no_encrypt)
577 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
578 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
Jouni Malinen9c1c98a2015-02-26 15:50:50 +0200579 info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
Johannes Bergaf61a162013-07-02 18:09:12 +0200580 }
Johannes Berga621fa42010-08-27 14:26:54 +0300581
582 return TX_CONTINUE;
583}
584
585static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100586ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200587{
Johannes Berg46e6de12012-07-04 18:10:07 +0200588 struct ieee80211_key *key;
Johannes Berge039fa42008-05-15 12:55:29 +0200589 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700590 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400591
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200592 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
Johannes Berge2ebc742007-07-27 15:43:22 +0200593 tx->key = NULL;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200594 else if (tx->sta &&
595 (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
Johannes Bergd4e46a32007-09-14 11:10:24 -0400596 tx->key = key;
Masashi Honma46f6b062016-06-22 19:55:20 +0900597 else if (ieee80211_is_group_privacy_action(tx->skb) &&
598 (key = rcu_dereference(tx->sdata->default_multicast_key)))
599 tx->key = key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200600 else if (ieee80211_is_mgmt(hdr->frame_control) &&
Jouni Malinenecbcd322010-03-29 23:35:23 -0700601 is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100602 ieee80211_is_robust_mgmt_frame(tx->skb) &&
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200603 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
604 tx->key = key;
Johannes Bergf7e01042010-12-09 19:49:02 +0100605 else if (is_multicast_ether_addr(hdr->addr1) &&
606 (key = rcu_dereference(tx->sdata->default_multicast_key)))
607 tx->key = key;
608 else if (!is_multicast_ether_addr(hdr->addr1) &&
609 (key = rcu_dereference(tx->sdata->default_unicast_key)))
Johannes Bergd4e46a32007-09-14 11:10:24 -0400610 tx->key = key;
Johannes Berge8f4fb72015-03-20 11:37:36 +0100611 else
Johannes Berg46e6de12012-07-04 18:10:07 +0200612 tx->key = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +0200613
614 if (tx->key) {
Johannes Berg813d7662010-01-17 01:47:58 +0100615 bool skip_hw = false;
616
Johannes Berg011bfcc2007-09-17 01:29:25 -0400617 /* TODO: add threshold stuff again */
Johannes Berg176e4f82007-12-18 15:27:47 +0100618
Johannes Berg97359d12010-08-10 09:46:38 +0200619 switch (tx->key->conf.cipher) {
620 case WLAN_CIPHER_SUITE_WEP40:
621 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg97359d12010-08-10 09:46:38 +0200622 case WLAN_CIPHER_SUITE_TKIP:
Harvey Harrison358c8d92008-07-15 18:44:13 -0700623 if (!ieee80211_is_data_present(hdr->frame_control))
Johannes Berg176e4f82007-12-18 15:27:47 +0100624 tx->key = NULL;
625 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200626 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200627 case WLAN_CIPHER_SUITE_CCMP_256:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200628 case WLAN_CIPHER_SUITE_GCMP:
629 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinenfb733332009-01-08 13:32:00 +0200630 if (!ieee80211_is_data_present(hdr->frame_control) &&
631 !ieee80211_use_mfp(hdr->frame_control, tx->sta,
Masashi Honma46f6b062016-06-22 19:55:20 +0900632 tx->skb) &&
633 !ieee80211_is_group_privacy_action(tx->skb))
Jouni Malinenfb733332009-01-08 13:32:00 +0200634 tx->key = NULL;
Kalle Valo3b43a182010-01-23 20:27:14 +0200635 else
636 skip_hw = (tx->key->conf.flags &
Johannes Berge548c492012-09-04 17:08:23 +0200637 IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
Kalle Valo3b43a182010-01-23 20:27:14 +0200638 ieee80211_is_mgmt(hdr->frame_control);
Jouni Malinenfb733332009-01-08 13:32:00 +0200639 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200640 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200641 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Jouni Malinen8ade5382015-01-24 19:52:09 +0200642 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
643 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200644 if (!ieee80211_is_mgmt(hdr->frame_control))
645 tx->key = NULL;
646 break;
Johannes Berg176e4f82007-12-18 15:27:47 +0100647 }
Johannes Berg813d7662010-01-17 01:47:58 +0100648
Johannes Berge54faf22013-01-29 11:41:38 +0100649 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
650 !ieee80211_is_deauth(hdr->frame_control)))
Johannes Berg95acac62011-07-12 12:30:59 +0200651 return TX_DROP;
652
Johannes Bergf12553e2010-01-22 22:07:59 +0100653 if (!skip_hw && tx->key &&
Johannes Berg382b1652010-01-25 11:36:16 +0100654 tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
Johannes Berg813d7662010-01-17 01:47:58 +0100655 info->control.hw_key = &tx->key->conf;
Johannes Berge2ebc742007-07-27 15:43:22 +0200656 }
657
Johannes Berg9ae54c82008-01-31 19:48:20 +0100658 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200659}
660
Johannes Bergd9e8a702008-06-30 15:10:44 +0200661static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100662ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200663{
Johannes Berge039fa42008-05-15 12:55:29 +0200664 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200665 struct ieee80211_hdr *hdr = (void *)tx->skb->data;
666 struct ieee80211_supported_band *sband;
Shanyu Zhaoa2c40242010-04-27 11:15:12 -0700667 u32 len;
Johannes Berge6a98542008-10-21 12:40:02 +0200668 struct ieee80211_tx_rate_control txrc;
Felix Fietkau0d528d82013-04-22 16:14:41 +0200669 struct ieee80211_sta_rates *ratetbl = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200670 bool assoc = false;
Johannes Berge6a98542008-10-21 12:40:02 +0200671
672 memset(&txrc, 0, sizeof(txrc));
Johannes Berg8318d782008-01-24 19:38:38 +0100673
Johannes Berg2d565772012-07-23 15:12:51 +0200674 sband = tx->local->hw.wiphy->bands[info->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200675
Shanyu Zhaoa2c40242010-04-27 11:15:12 -0700676 len = min_t(u32, tx->skb->len + FCS_LEN,
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200677 tx->local->hw.wiphy->frag_threshold);
Johannes Berg58d41852007-09-26 17:53:18 +0200678
Johannes Berge6a98542008-10-21 12:40:02 +0200679 /* set up the tx rate control struct we give the RC algo */
Johannes Berg005e4722012-02-26 11:24:35 +0100680 txrc.hw = &tx->local->hw;
Johannes Berge6a98542008-10-21 12:40:02 +0200681 txrc.sband = sband;
682 txrc.bss_conf = &tx->sdata->vif.bss_conf;
683 txrc.skb = tx->skb;
684 txrc.reported_rate.idx = -1;
Johannes Berg2d565772012-07-23 15:12:51 +0200685 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
Felix Fietkau2ffbe6d2013-04-16 13:38:42 +0200686
687 if (tx->sdata->rc_has_mcs_mask[info->band])
688 txrc.rate_idx_mcs_mask =
689 tx->sdata->rc_rateidx_mcs_mask[info->band];
690
Felix Fietkau8f0729b2010-11-11 15:07:23 +0100691 txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -0800692 tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
Bertold Van den Bergh5765f9f2015-08-05 16:02:28 +0200693 tx->sdata->vif.type == NL80211_IFTYPE_ADHOC ||
694 tx->sdata->vif.type == NL80211_IFTYPE_OCB);
Johannes Berg58d41852007-09-26 17:53:18 +0200695
Johannes Berge6a98542008-10-21 12:40:02 +0200696 /* set up RTS protection if desired */
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200697 if (len > tx->local->hw.wiphy->rts_threshold) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200698 txrc.rts = true;
Johannes Berge2ebc742007-07-27 15:43:22 +0200699 }
Johannes Berge6a98542008-10-21 12:40:02 +0200700
Felix Fietkau0d528d82013-04-22 16:14:41 +0200701 info->control.use_rts = txrc.rts;
Felix Fietkau991fec02013-04-16 13:38:43 +0200702 info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
703
Johannes Berge6a98542008-10-21 12:40:02 +0200704 /*
705 * Use short preamble if the BSS can handle it, but not for
706 * management frames unless we know the receiver can handle
707 * that -- the management frame might be to a station that
708 * just wants a probe response.
709 */
710 if (tx->sdata->vif.bss_conf.use_short_preamble &&
711 (ieee80211_is_data(hdr->frame_control) ||
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200712 (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
Felix Fietkau0d528d82013-04-22 16:14:41 +0200713 txrc.short_preamble = true;
714
715 info->control.short_preamble = txrc.short_preamble;
Johannes Berge6a98542008-10-21 12:40:02 +0200716
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +0100717 /* don't ask rate control when rate already injected via radiotap */
718 if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)
719 return TX_CONTINUE;
720
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200721 if (tx->sta)
722 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge6a98542008-10-21 12:40:02 +0200723
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700724 /*
725 * Lets not bother rate control if we're associated and cannot
726 * talk to the sta. This should not happen.
727 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200728 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700729 !rate_usable_index_exists(sband, &tx->sta->sta),
730 "%s: Dropped data frame as no usable bitrate found while "
731 "scanning and associated. Target station: "
732 "%pM on %d GHz band\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100733 tx->sdata->name, hdr->addr1,
Johannes Berg2d565772012-07-23 15:12:51 +0200734 info->band ? 5 : 2))
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700735 return TX_DROP;
736
737 /*
738 * If we're associated with the sta at this point we know we can at
739 * least send the frame at the lowest bit rate.
740 */
Johannes Berge6a98542008-10-21 12:40:02 +0200741 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
742
Felix Fietkau0d528d82013-04-22 16:14:41 +0200743 if (tx->sta && !info->control.skip_table)
744 ratetbl = rcu_dereference(tx->sta->sta.rates);
745
746 if (unlikely(info->control.rates[0].idx < 0)) {
747 if (ratetbl) {
748 struct ieee80211_tx_rate rate = {
749 .idx = ratetbl->rate[0].idx,
750 .flags = ratetbl->rate[0].flags,
751 .count = ratetbl->rate[0].count
752 };
753
754 if (ratetbl->rate[0].idx < 0)
755 return TX_DROP;
756
757 tx->rate = rate;
758 } else {
759 return TX_DROP;
760 }
761 } else {
762 tx->rate = info->control.rates[0];
763 }
Johannes Berge6a98542008-10-21 12:40:02 +0200764
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100765 if (txrc.reported_rate.idx < 0) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200766 txrc.reported_rate = tx->rate;
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100767 if (tx->sta && ieee80211_is_data(hdr->frame_control))
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200768 tx->sta->tx_stats.last_rate = txrc.reported_rate;
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100769 } else if (tx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200770 tx->sta->tx_stats.last_rate = txrc.reported_rate;
Johannes Berge6a98542008-10-21 12:40:02 +0200771
Felix Fietkau0d528d82013-04-22 16:14:41 +0200772 if (ratetbl)
773 return TX_CONTINUE;
774
Johannes Berge6a98542008-10-21 12:40:02 +0200775 if (unlikely(!info->control.rates[0].count))
776 info->control.rates[0].count = 1;
777
Gábor Stefanik99551512009-04-23 19:36:14 +0200778 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
779 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
780 info->control.rates[0].count = 1;
781
Johannes Berg9ae54c82008-01-31 19:48:20 +0100782 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200783}
784
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100785static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
786{
787 u16 *seq = &sta->tid_seq[tid];
788 __le16 ret = cpu_to_le16(*seq);
789
790 /* Increase the sequence number. */
791 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
792
793 return ret;
794}
795
Johannes Bergd9e8a702008-06-30 15:10:44 +0200796static ieee80211_tx_result debug_noinline
Johannes Bergf591fa52008-07-10 11:21:26 +0200797ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
798{
799 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
800 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergf591fa52008-07-10 11:21:26 +0200801 int tid;
802
Johannes Berg25d834e2008-09-12 22:52:47 +0200803 /*
804 * Packet injection may want to control the sequence
805 * number, if we have no matching interface then we
806 * neither assign one ourselves nor ask the driver to.
807 */
Johannes Berg5061b0c2009-07-14 00:33:34 +0200808 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
Johannes Berg25d834e2008-09-12 22:52:47 +0200809 return TX_CONTINUE;
810
Johannes Bergf591fa52008-07-10 11:21:26 +0200811 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
812 return TX_CONTINUE;
813
814 if (ieee80211_hdrlen(hdr->frame_control) < 24)
815 return TX_CONTINUE;
816
Johannes Berg49a59542011-09-29 16:04:41 +0200817 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
818 return TX_CONTINUE;
819
Johannes Berg94778282008-10-10 13:21:59 +0200820 /*
821 * Anything but QoS data that has a sequence number field
822 * (is long enough) gets a sequence number from the global
Bob Copelandc4c205f2013-08-23 09:35:38 -0400823 * counter. QoS data frames with a multicast destination
824 * also use the global counter (802.11-2012 9.3.2.10).
Johannes Berg94778282008-10-10 13:21:59 +0200825 */
Bob Copelandc4c205f2013-08-23 09:35:38 -0400826 if (!ieee80211_is_data_qos(hdr->frame_control) ||
827 is_multicast_ether_addr(hdr->addr1)) {
Johannes Bergb9771d42018-05-28 15:47:41 +0200828 if (tx->flags & IEEE80211_TX_NO_SEQNO)
829 return TX_CONTINUE;
Johannes Berg94778282008-10-10 13:21:59 +0200830 /* driver should assign sequence number */
Johannes Bergf591fa52008-07-10 11:21:26 +0200831 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
Johannes Berg94778282008-10-10 13:21:59 +0200832 /* for pure STA mode without beacons, we can do it */
833 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
834 tx->sdata->sequence_number += 0x10;
Johannes Berg79c892b2014-11-21 14:26:31 +0100835 if (tx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200836 tx->sta->tx_stats.msdu[IEEE80211_NUM_TIDS]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200837 return TX_CONTINUE;
838 }
839
840 /*
841 * This should be true for injected/management frames only, for
842 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
843 * above since they are not QoS-data frames.
844 */
845 if (!tx->sta)
846 return TX_CONTINUE;
847
848 /* include per-STA, per-TID sequence counter */
Sara Sharona1f2ba04c2018-02-19 14:48:40 +0200849 tid = ieee80211_get_tid(hdr);
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200850 tx->sta->tx_stats.msdu[tid]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200851
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +0200852 hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
Johannes Bergf591fa52008-07-10 11:21:26 +0200853
854 return TX_CONTINUE;
855}
856
Johannes Berg252b86c2011-11-16 15:28:55 +0100857static int ieee80211_fragment(struct ieee80211_tx_data *tx,
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100858 struct sk_buff *skb, int hdrlen,
859 int frag_threshold)
860{
Johannes Berg252b86c2011-11-16 15:28:55 +0100861 struct ieee80211_local *local = tx->local;
Johannes Berga1a3fce2011-11-16 15:28:56 +0100862 struct ieee80211_tx_info *info;
Johannes Berg252b86c2011-11-16 15:28:55 +0100863 struct sk_buff *tmp;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100864 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
865 int pos = hdrlen + per_fragm;
866 int rem = skb->len - hdrlen - per_fragm;
867
868 if (WARN_ON(rem < 0))
869 return -EINVAL;
870
Johannes Berg252b86c2011-11-16 15:28:55 +0100871 /* first fragment was already added to queue by caller */
872
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100873 while (rem) {
874 int fraglen = per_fragm;
875
876 if (fraglen > rem)
877 fraglen = rem;
878 rem -= fraglen;
879 tmp = dev_alloc_skb(local->tx_headroom +
880 frag_threshold +
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200881 tx->sdata->encrypt_headroom +
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100882 IEEE80211_ENCRYPT_TAILROOM);
883 if (!tmp)
884 return -ENOMEM;
Johannes Berg252b86c2011-11-16 15:28:55 +0100885
886 __skb_queue_tail(&tx->skbs, tmp);
887
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200888 skb_reserve(tmp,
889 local->tx_headroom + tx->sdata->encrypt_headroom);
890
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100891 /* copy control information */
892 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
Johannes Berga1a3fce2011-11-16 15:28:56 +0100893
894 info = IEEE80211_SKB_CB(tmp);
895 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
896 IEEE80211_TX_CTL_FIRST_FRAGMENT);
897
898 if (rem)
899 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
900
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100901 skb_copy_queue_mapping(tmp, skb);
902 tmp->priority = skb->priority;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100903 tmp->dev = skb->dev;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100904
905 /* copy header and data */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200906 skb_put_data(tmp, skb->data, hdrlen);
907 skb_put_data(tmp, skb->data + pos, fraglen);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100908
909 pos += fraglen;
910 }
911
Johannes Berg252b86c2011-11-16 15:28:55 +0100912 /* adjust first fragment's length */
Johannes Berg338f9772014-02-01 00:16:23 +0100913 skb_trim(skb, hdrlen + per_fragm);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100914 return 0;
915}
916
Johannes Bergf591fa52008-07-10 11:21:26 +0200917static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +0200918ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
919{
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100920 struct sk_buff *skb = tx->skb;
921 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
922 struct ieee80211_hdr *hdr = (void *)skb->data;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200923 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100924 int hdrlen;
925 int fragnum;
Johannes Berge2454942008-05-15 12:55:28 +0200926
Johannes Berg252b86c2011-11-16 15:28:55 +0100927 /* no matter what happens, tx->skb moves to tx->skbs */
928 __skb_queue_tail(&tx->skbs, skb);
929 tx->skb = NULL;
930
Johannes Berga26eb272011-10-07 14:01:25 +0200931 if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
932 return TX_CONTINUE;
933
Sara Sharonf3fe4e92016-10-18 23:12:11 +0300934 if (ieee80211_hw_check(&tx->local->hw, SUPPORTS_TX_FRAG))
Johannes Berge2454942008-05-15 12:55:28 +0200935 return TX_CONTINUE;
936
Johannes Bergeefce912008-05-17 00:57:13 +0200937 /*
938 * Warn when submitting a fragmented A-MPDU frame and drop it.
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200939 * This scenario is handled in ieee80211_tx_prepare but extra
Ron Rindjunsky8d5e0d52008-06-12 15:42:29 +0300940 * caution taken here as fragmented ampdu may cause Tx stop.
Johannes Bergeefce912008-05-17 00:57:13 +0200941 */
Sujith8b30b1f2008-10-24 09:55:27 +0530942 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
Johannes Bergeefce912008-05-17 00:57:13 +0200943 return TX_DROP;
944
Harvey Harrison065e96052008-06-22 16:45:27 -0700945 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berge2454942008-05-15 12:55:28 +0200946
Johannes Berga26eb272011-10-07 14:01:25 +0200947 /* internal error, why isn't DONTFRAG set? */
Johannes Berg8ccd8f22009-04-29 23:35:56 +0200948 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100949 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200950
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100951 /*
952 * Now fragment the frame. This will allocate all the fragments and
953 * chain them (using skb as the first fragment) to skb->next.
954 * During transmission, we will remove the successfully transmitted
955 * fragments from this list. When the low-level driver rejects one
956 * of the fragments then we will simply pretend to accept the skb
957 * but store it away as pending.
958 */
Johannes Berg252b86c2011-11-16 15:28:55 +0100959 if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100960 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200961
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100962 /* update duration/seq/flags of fragments */
963 fragnum = 0;
Johannes Berg252b86c2011-11-16 15:28:55 +0100964
965 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100966 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
Johannes Berge2454942008-05-15 12:55:28 +0200967
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100968 hdr = (void *)skb->data;
969 info = IEEE80211_SKB_CB(skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200970
Johannes Berg252b86c2011-11-16 15:28:55 +0100971 if (!skb_queue_is_last(&tx->skbs, skb)) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100972 hdr->frame_control |= morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200973 /*
974 * No multi-rate retries for fragmented frames, that
975 * would completely throw off the NAV at other STAs.
976 */
977 info->control.rates[1].idx = -1;
978 info->control.rates[2].idx = -1;
979 info->control.rates[3].idx = -1;
Thomas Huehne3e1a0b2012-07-02 19:46:16 +0200980 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
Johannes Berge6a98542008-10-21 12:40:02 +0200981 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100982 } else {
983 hdr->frame_control &= ~morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200984 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100985 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
986 fragnum++;
Johannes Berg252b86c2011-11-16 15:28:55 +0100987 }
Johannes Berge2454942008-05-15 12:55:28 +0200988
989 return TX_CONTINUE;
Johannes Berge2454942008-05-15 12:55:28 +0200990}
991
Johannes Bergd9e8a702008-06-30 15:10:44 +0200992static ieee80211_tx_result debug_noinline
Johannes Bergfeff1f22009-08-10 16:01:54 +0200993ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
994{
Johannes Berg252b86c2011-11-16 15:28:55 +0100995 struct sk_buff *skb;
Johannes Berg560d2682013-02-05 10:55:21 +0100996 int ac = -1;
Johannes Bergfeff1f22009-08-10 16:01:54 +0200997
998 if (!tx->sta)
999 return TX_CONTINUE;
1000
Johannes Berg252b86c2011-11-16 15:28:55 +01001001 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg560d2682013-02-05 10:55:21 +01001002 ac = skb_get_queue_mapping(skb);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001003 tx->sta->tx_stats.bytes[ac] += skb->len;
Johannes Berg252b86c2011-11-16 15:28:55 +01001004 }
Johannes Berg560d2682013-02-05 10:55:21 +01001005 if (ac >= 0)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001006 tx->sta->tx_stats.packets[ac]++;
Johannes Bergfeff1f22009-08-10 16:01:54 +02001007
1008 return TX_CONTINUE;
1009}
1010
1011static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +02001012ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
1013{
1014 if (!tx->key)
1015 return TX_CONTINUE;
1016
Johannes Berg97359d12010-08-10 09:46:38 +02001017 switch (tx->key->conf.cipher) {
1018 case WLAN_CIPHER_SUITE_WEP40:
1019 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge2454942008-05-15 12:55:28 +02001020 return ieee80211_crypto_wep_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001021 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berge2454942008-05-15 12:55:28 +02001022 return ieee80211_crypto_tkip_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001023 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001024 return ieee80211_crypto_ccmp_encrypt(
1025 tx, IEEE80211_CCMP_MIC_LEN);
1026 case WLAN_CIPHER_SUITE_CCMP_256:
1027 return ieee80211_crypto_ccmp_encrypt(
1028 tx, IEEE80211_CCMP_256_MIC_LEN);
Johannes Berg97359d12010-08-10 09:46:38 +02001029 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001030 return ieee80211_crypto_aes_cmac_encrypt(tx);
Jouni Malinen56c52da2015-01-24 19:52:08 +02001031 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1032 return ieee80211_crypto_aes_cmac_256_encrypt(tx);
Jouni Malinen8ade5382015-01-24 19:52:09 +02001033 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1034 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1035 return ieee80211_crypto_aes_gmac_encrypt(tx);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001036 case WLAN_CIPHER_SUITE_GCMP:
1037 case WLAN_CIPHER_SUITE_GCMP_256:
1038 return ieee80211_crypto_gcmp_encrypt(tx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +03001039 default:
Yoni Divinskyd32a1022012-01-16 15:18:59 +02001040 return ieee80211_crypto_hw_encrypt(tx);
Johannes Berge2454942008-05-15 12:55:28 +02001041 }
1042
Johannes Berge2454942008-05-15 12:55:28 +02001043 return TX_DROP;
1044}
1045
Johannes Bergd9e8a702008-06-30 15:10:44 +02001046static ieee80211_tx_result debug_noinline
Johannes Berg03f93c32008-06-25 14:36:27 +03001047ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1048{
Johannes Berg252b86c2011-11-16 15:28:55 +01001049 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001050 struct ieee80211_hdr *hdr;
1051 int next_len;
1052 bool group_addr;
Johannes Berg03f93c32008-06-25 14:36:27 +03001053
Johannes Berg252b86c2011-11-16 15:28:55 +01001054 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001055 hdr = (void *) skb->data;
Jouni Malinen7e0aae42009-05-19 19:25:58 +03001056 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1057 break; /* must not overwrite AID */
Johannes Berg252b86c2011-11-16 15:28:55 +01001058 if (!skb_queue_is_last(&tx->skbs, skb)) {
1059 struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1060 next_len = next->len;
1061 } else
1062 next_len = 0;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001063 group_addr = is_multicast_ether_addr(hdr->addr1);
Johannes Berg03f93c32008-06-25 14:36:27 +03001064
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001065 hdr->duration_id =
Johannes Berg252b86c2011-11-16 15:28:55 +01001066 ieee80211_duration(tx, skb, group_addr, next_len);
1067 }
Johannes Berg03f93c32008-06-25 14:36:27 +03001068
1069 return TX_CONTINUE;
1070}
1071
Johannes Berge2ebc742007-07-27 15:43:22 +02001072/* actual transmit path */
1073
Johannes Berga622ab72010-06-10 10:21:39 +02001074static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1075 struct sk_buff *skb,
1076 struct ieee80211_tx_info *info,
1077 struct tid_ampdu_tx *tid_tx,
1078 int tid)
1079{
1080 bool queued = false;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001081 bool reset_agg_timer = false;
Helmut Schaaaa454582012-03-07 17:20:30 +01001082 struct sk_buff *purge_skb = NULL;
Johannes Berga622ab72010-06-10 10:21:39 +02001083
1084 if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1085 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001086 reset_agg_timer = true;
Johannes Berg0ab33702010-06-10 10:21:42 +02001087 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1088 /*
1089 * nothing -- this aggregation session is being started
1090 * but that might still fail with the driver
1091 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001092 } else if (!tx->sta->sta.txq[tid]) {
Johannes Berga622ab72010-06-10 10:21:39 +02001093 spin_lock(&tx->sta->lock);
1094 /*
1095 * Need to re-check now, because we may get here
1096 *
1097 * 1) in the window during which the setup is actually
1098 * already done, but not marked yet because not all
1099 * packets are spliced over to the driver pending
1100 * queue yet -- if this happened we acquire the lock
1101 * either before or after the splice happens, but
1102 * need to recheck which of these cases happened.
1103 *
1104 * 2) during session teardown, if the OPERATIONAL bit
1105 * was cleared due to the teardown but the pointer
1106 * hasn't been assigned NULL yet (or we loaded it
1107 * before it was assigned) -- in this case it may
1108 * now be NULL which means we should just let the
1109 * packet pass through because splicing the frames
1110 * back is already done.
1111 */
Johannes Berg40b275b2011-05-13 14:15:49 +02001112 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
Johannes Berga622ab72010-06-10 10:21:39 +02001113
1114 if (!tid_tx) {
1115 /* do nothing, let packet pass through */
1116 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1117 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001118 reset_agg_timer = true;
Johannes Berga622ab72010-06-10 10:21:39 +02001119 } else {
1120 queued = true;
Emmanuel Grumbachf6d46712016-03-17 16:51:42 +02001121 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
1122 clear_sta_flag(tx->sta, WLAN_STA_SP);
1123 ps_dbg(tx->sta->sdata,
1124 "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
1125 tx->sta->sta.addr, tx->sta->sta.aid);
1126 }
Johannes Berga622ab72010-06-10 10:21:39 +02001127 info->control.vif = &tx->sdata->vif;
1128 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Emmanuel Grumbachfacde7f2016-03-17 16:51:41 +02001129 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berga622ab72010-06-10 10:21:39 +02001130 __skb_queue_tail(&tid_tx->pending, skb);
Helmut Schaaaa454582012-03-07 17:20:30 +01001131 if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1132 purge_skb = __skb_dequeue(&tid_tx->pending);
Johannes Berga622ab72010-06-10 10:21:39 +02001133 }
1134 spin_unlock(&tx->sta->lock);
Helmut Schaaaa454582012-03-07 17:20:30 +01001135
1136 if (purge_skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001137 ieee80211_free_txskb(&tx->local->hw, purge_skb);
Johannes Berga622ab72010-06-10 10:21:39 +02001138 }
1139
Nikolay Martynov285fa692011-11-22 21:50:28 -05001140 /* reset session timer */
Sara Sharon914eac22018-04-20 13:49:19 +03001141 if (reset_agg_timer)
Felix Fietkau12d3952f2012-03-18 22:58:06 +01001142 tid_tx->last_tx = jiffies;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001143
Johannes Berga622ab72010-06-10 10:21:39 +02001144 return queued;
1145}
1146
Johannes Berg58d41852007-09-26 17:53:18 +02001147/*
1148 * initialises @tx
Johannes Berg7c107702015-03-20 14:18:27 +01001149 * pass %NULL for the station if unknown, a valid pointer if known
1150 * or an ERR_PTR() if the station is known not to exist
Johannes Berg58d41852007-09-26 17:53:18 +02001151 */
Johannes Berg9ae54c82008-01-31 19:48:20 +01001152static ieee80211_tx_result
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001153ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1154 struct ieee80211_tx_data *tx,
Johannes Berg7c107702015-03-20 14:18:27 +01001155 struct sta_info *sta, struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02001156{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001157 struct ieee80211_local *local = sdata->local;
Johannes Berg58d41852007-09-26 17:53:18 +02001158 struct ieee80211_hdr *hdr;
Johannes Berge039fa42008-05-15 12:55:29 +02001159 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg68f2b512011-10-07 14:01:24 +02001160 int tid;
Johannes Berge2ebc742007-07-27 15:43:22 +02001161
1162 memset(tx, 0, sizeof(*tx));
1163 tx->skb = skb;
Johannes Berge2ebc742007-07-27 15:43:22 +02001164 tx->local = local;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001165 tx->sdata = sdata;
Johannes Berg252b86c2011-11-16 15:28:55 +01001166 __skb_queue_head_init(&tx->skbs);
Johannes Berge2ebc742007-07-27 15:43:22 +02001167
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001168 /*
1169 * If this flag is set to true anywhere, and we get here,
1170 * we are doing the needed processing, so remove the flag
1171 * now.
1172 */
1173 info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1174
Johannes Berg58d41852007-09-26 17:53:18 +02001175 hdr = (struct ieee80211_hdr *) skb->data;
1176
Johannes Berg7c107702015-03-20 14:18:27 +01001177 if (likely(sta)) {
1178 if (!IS_ERR(sta))
1179 tx->sta = sta;
1180 } else {
1181 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1182 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1183 if (!tx->sta && sdata->wdev.use_4addr)
1184 return TX_DROP;
1185 } else if (info->flags & (IEEE80211_TX_INTFL_NL80211_FRAME_TX |
1186 IEEE80211_TX_CTL_INJECTED) ||
1187 tx->sdata->control_port_protocol == tx->skb->protocol) {
1188 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1189 }
1190 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
1191 tx->sta = sta_info_get(sdata, hdr->addr1);
Felix Fietkau3f0e0b22010-01-08 18:15:13 +01001192 }
Johannes Berg58d41852007-09-26 17:53:18 +02001193
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001194 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
Johannes Berg49a59542011-09-29 16:04:41 +02001195 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001196 ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
1197 !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001198 struct tid_ampdu_tx *tid_tx;
1199
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001200 tid = ieee80211_get_tid(hdr);
Sujith8b30b1f2008-10-24 09:55:27 +05301201
Johannes Berga622ab72010-06-10 10:21:39 +02001202 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1203 if (tid_tx) {
1204 bool queued;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001205
Johannes Berga622ab72010-06-10 10:21:39 +02001206 queued = ieee80211_tx_prep_agg(tx, skb, info,
1207 tid_tx, tid);
1208
1209 if (unlikely(queued))
1210 return TX_QUEUED;
1211 }
Sujith8b30b1f2008-10-24 09:55:27 +05301212 }
1213
Jiri Slabybadffb72007-08-28 17:01:54 -04001214 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001215 tx->flags &= ~IEEE80211_TX_UNICAST;
Johannes Berge039fa42008-05-15 12:55:29 +02001216 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Simon Wunderlich6fd67e92011-11-18 14:20:42 +01001217 } else
Johannes Berg5cf121c2008-02-25 16:27:43 +01001218 tx->flags |= IEEE80211_TX_UNICAST;
Johannes Berg58d41852007-09-26 17:53:18 +02001219
Johannes Berga26eb272011-10-07 14:01:25 +02001220 if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1221 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1222 skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1223 info->flags & IEEE80211_TX_CTL_AMPDU)
1224 info->flags |= IEEE80211_TX_CTL_DONTFRAG;
Johannes Berg58d41852007-09-26 17:53:18 +02001225 }
1226
Johannes Berge2ebc742007-07-27 15:43:22 +02001227 if (!tx->sta)
Johannes Berge039fa42008-05-15 12:55:29 +02001228 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Felix Fietkauf7418bc2015-09-24 14:59:49 +02001229 else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
Johannes Berge039fa42008-05-15 12:55:29 +02001230 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Felix Fietkauf7418bc2015-09-24 14:59:49 +02001231 ieee80211_check_fast_xmit(tx->sta);
1232 }
Johannes Berg58d41852007-09-26 17:53:18 +02001233
Johannes Berge039fa42008-05-15 12:55:29 +02001234 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001235
Johannes Berg9ae54c82008-01-31 19:48:20 +01001236 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001237}
1238
Michal Kazior80a83cf2016-05-19 10:37:48 +02001239static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
1240 struct ieee80211_vif *vif,
Michal Kaziordbef5362017-01-13 13:32:51 +01001241 struct sta_info *sta,
Michal Kazior80a83cf2016-05-19 10:37:48 +02001242 struct sk_buff *skb)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001243{
1244 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001245 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001246 struct ieee80211_txq *txq = NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001247
Felix Fietkauc3732a72016-02-28 15:19:53 +01001248 if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
1249 (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
Michal Kazior80a83cf2016-05-19 10:37:48 +02001250 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001251
Johannes Bergadf8ed02018-08-31 11:31:08 +03001252 if (unlikely(!ieee80211_is_data_present(hdr->frame_control))) {
1253 if ((!ieee80211_is_mgmt(hdr->frame_control) ||
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001254 ieee80211_is_bufferable_mmpdu(hdr->frame_control) ||
1255 vif->type == NL80211_IFTYPE_STATION) &&
Johannes Bergadf8ed02018-08-31 11:31:08 +03001256 sta && sta->uploaded) {
1257 /*
1258 * This will be NULL if the driver didn't set the
1259 * opt-in hardware flag.
1260 */
1261 txq = sta->sta.txq[IEEE80211_NUM_TIDS];
1262 }
1263 } else if (sta) {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001264 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1265
Michal Kaziordbef5362017-01-13 13:32:51 +01001266 if (!sta->uploaded)
1267 return NULL;
1268
1269 txq = sta->sta.txq[tid];
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001270 } else if (vif) {
1271 txq = vif->txq;
1272 }
1273
1274 if (!txq)
Michal Kazior80a83cf2016-05-19 10:37:48 +02001275 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001276
Michal Kazior80a83cf2016-05-19 10:37:48 +02001277 return to_txq_info(txq);
1278}
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001279
Michal Kazior5caa3282016-05-19 10:37:51 +02001280static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
1281{
1282 IEEE80211_SKB_CB(skb)->control.enqueue_time = codel_get_time();
1283}
1284
Michal Kazior5caa3282016-05-19 10:37:51 +02001285static u32 codel_skb_len_func(const struct sk_buff *skb)
1286{
1287 return skb->len;
1288}
1289
1290static codel_time_t codel_skb_time_func(const struct sk_buff *skb)
1291{
1292 const struct ieee80211_tx_info *info;
1293
1294 info = (const struct ieee80211_tx_info *)skb->cb;
1295 return info->control.enqueue_time;
1296}
1297
1298static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars,
1299 void *ctx)
1300{
1301 struct ieee80211_local *local;
1302 struct txq_info *txqi;
1303 struct fq *fq;
1304 struct fq_flow *flow;
1305
1306 txqi = ctx;
1307 local = vif_to_sdata(txqi->txq.vif)->local;
1308 fq = &local->fq;
1309
1310 if (cvars == &txqi->def_cvars)
1311 flow = &txqi->def_flow;
1312 else
1313 flow = &fq->flows[cvars - local->cvars];
1314
1315 return fq_flow_dequeue(fq, flow);
1316}
1317
1318static void codel_drop_func(struct sk_buff *skb,
1319 void *ctx)
1320{
1321 struct ieee80211_local *local;
1322 struct ieee80211_hw *hw;
1323 struct txq_info *txqi;
1324
1325 txqi = ctx;
1326 local = vif_to_sdata(txqi->txq.vif)->local;
1327 hw = &local->hw;
1328
1329 ieee80211_free_txskb(hw, skb);
1330}
1331
Michal Kaziorfa962b92016-05-19 10:37:49 +02001332static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
1333 struct fq_tin *tin,
1334 struct fq_flow *flow)
1335{
Michal Kazior5caa3282016-05-19 10:37:51 +02001336 struct ieee80211_local *local;
1337 struct txq_info *txqi;
1338 struct codel_vars *cvars;
1339 struct codel_params *cparams;
1340 struct codel_stats *cstats;
1341
1342 local = container_of(fq, struct ieee80211_local, fq);
1343 txqi = container_of(tin, struct txq_info, tin);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001344 cstats = &txqi->cstats;
Michal Kazior5caa3282016-05-19 10:37:51 +02001345
Toke Høiland-Jørgensen484a54c2017-04-06 11:38:26 +02001346 if (txqi->txq.sta) {
1347 struct sta_info *sta = container_of(txqi->txq.sta,
1348 struct sta_info, sta);
1349 cparams = &sta->cparams;
1350 } else {
1351 cparams = &local->cparams;
1352 }
1353
Michal Kazior5caa3282016-05-19 10:37:51 +02001354 if (flow == &txqi->def_flow)
1355 cvars = &txqi->def_cvars;
1356 else
1357 cvars = &local->cvars[flow - fq->flows];
1358
1359 return codel_dequeue(txqi,
1360 &flow->backlog,
1361 cparams,
1362 cvars,
1363 cstats,
1364 codel_skb_len_func,
1365 codel_skb_time_func,
1366 codel_drop_func,
1367 codel_dequeue_func);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001368}
1369
1370static void fq_skb_free_func(struct fq *fq,
1371 struct fq_tin *tin,
1372 struct fq_flow *flow,
1373 struct sk_buff *skb)
1374{
1375 struct ieee80211_local *local;
1376
1377 local = container_of(fq, struct ieee80211_local, fq);
1378 ieee80211_free_txskb(&local->hw, skb);
1379}
1380
1381static struct fq_flow *fq_flow_get_default_func(struct fq *fq,
1382 struct fq_tin *tin,
1383 int idx,
1384 struct sk_buff *skb)
1385{
1386 struct txq_info *txqi;
1387
1388 txqi = container_of(tin, struct txq_info, tin);
1389 return &txqi->def_flow;
1390}
1391
Michal Kazior80a83cf2016-05-19 10:37:48 +02001392static void ieee80211_txq_enqueue(struct ieee80211_local *local,
1393 struct txq_info *txqi,
1394 struct sk_buff *skb)
1395{
Michal Kaziorfa962b92016-05-19 10:37:49 +02001396 struct fq *fq = &local->fq;
1397 struct fq_tin *tin = &txqi->tin;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001398 u32 flow_idx = fq_flow_idx(fq, skb);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001399
Michal Kazior5caa3282016-05-19 10:37:51 +02001400 ieee80211_set_skb_enqueue_time(skb);
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001401
1402 spin_lock_bh(&fq->lock);
1403 fq_tin_enqueue(fq, tin, flow_idx, skb,
Michal Kaziorfa962b92016-05-19 10:37:49 +02001404 fq_skb_free_func,
1405 fq_flow_get_default_func);
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001406 spin_unlock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001407}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001408
Johannes Berg2a9e2572017-10-06 11:53:33 +02001409static bool fq_vlan_filter_func(struct fq *fq, struct fq_tin *tin,
1410 struct fq_flow *flow, struct sk_buff *skb,
1411 void *data)
1412{
1413 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1414
1415 return info->control.vif == data;
1416}
1417
1418void ieee80211_txq_remove_vlan(struct ieee80211_local *local,
1419 struct ieee80211_sub_if_data *sdata)
1420{
1421 struct fq *fq = &local->fq;
1422 struct txq_info *txqi;
1423 struct fq_tin *tin;
1424 struct ieee80211_sub_if_data *ap;
1425
1426 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1427 return;
1428
1429 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1430
1431 if (!ap->vif.txq)
1432 return;
1433
1434 txqi = to_txq_info(ap->vif.txq);
1435 tin = &txqi->tin;
1436
1437 spin_lock_bh(&fq->lock);
1438 fq_tin_filter(fq, tin, fq_vlan_filter_func, &sdata->vif,
1439 fq_skb_free_func);
1440 spin_unlock_bh(&fq->lock);
1441}
1442
Michal Kaziorfa962b92016-05-19 10:37:49 +02001443void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
1444 struct sta_info *sta,
1445 struct txq_info *txqi, int tid)
1446{
1447 fq_tin_init(&txqi->tin);
1448 fq_flow_init(&txqi->def_flow);
Michal Kazior5caa3282016-05-19 10:37:51 +02001449 codel_vars_init(&txqi->def_cvars);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001450 codel_stats_init(&txqi->cstats);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001451 __skb_queue_head_init(&txqi->frags);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001452 INIT_LIST_HEAD(&txqi->schedule_order);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001453
1454 txqi->txq.vif = &sdata->vif;
1455
Johannes Bergadf8ed02018-08-31 11:31:08 +03001456 if (!sta) {
Michal Kaziorfa962b92016-05-19 10:37:49 +02001457 sdata->vif.txq = &txqi->txq;
1458 txqi->txq.tid = 0;
1459 txqi->txq.ac = IEEE80211_AC_BE;
Johannes Bergadf8ed02018-08-31 11:31:08 +03001460
1461 return;
Michal Kazior80a83cf2016-05-19 10:37:48 +02001462 }
Johannes Bergadf8ed02018-08-31 11:31:08 +03001463
1464 if (tid == IEEE80211_NUM_TIDS) {
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001465 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1466 /* Drivers need to opt in to the management MPDU TXQ */
1467 if (!ieee80211_hw_check(&sdata->local->hw,
1468 STA_MMPDU_TXQ))
1469 return;
1470 } else if (!ieee80211_hw_check(&sdata->local->hw,
1471 BUFF_MMPDU_TXQ)) {
1472 /* Drivers need to opt in to the bufferable MMPDU TXQ */
Johannes Bergadf8ed02018-08-31 11:31:08 +03001473 return;
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001474 }
Johannes Bergadf8ed02018-08-31 11:31:08 +03001475 txqi->txq.ac = IEEE80211_AC_VO;
1476 } else {
1477 txqi->txq.ac = ieee80211_ac_from_tid(tid);
1478 }
1479
1480 txqi->txq.sta = &sta->sta;
1481 txqi->txq.tid = tid;
1482 sta->sta.txq[tid] = &txqi->txq;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001483}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001484
Michal Kaziorfa962b92016-05-19 10:37:49 +02001485void ieee80211_txq_purge(struct ieee80211_local *local,
1486 struct txq_info *txqi)
1487{
1488 struct fq *fq = &local->fq;
1489 struct fq_tin *tin = &txqi->tin;
1490
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08001491 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001492 fq_tin_reset(fq, tin, fq_skb_free_func);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001493 ieee80211_purge_tx_queue(&local->hw, &txqi->frags);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08001494 spin_unlock_bh(&fq->lock);
1495
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001496 spin_lock_bh(&local->active_txq_lock[txqi->txq.ac]);
1497 list_del_init(&txqi->schedule_order);
1498 spin_unlock_bh(&local->active_txq_lock[txqi->txq.ac]);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001499}
1500
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001501void ieee80211_txq_set_params(struct ieee80211_local *local)
1502{
1503 if (local->hw.wiphy->txq_limit)
1504 local->fq.limit = local->hw.wiphy->txq_limit;
1505 else
1506 local->hw.wiphy->txq_limit = local->fq.limit;
1507
1508 if (local->hw.wiphy->txq_memory_limit)
1509 local->fq.memory_limit = local->hw.wiphy->txq_memory_limit;
1510 else
1511 local->hw.wiphy->txq_memory_limit = local->fq.memory_limit;
1512
1513 if (local->hw.wiphy->txq_quantum)
1514 local->fq.quantum = local->hw.wiphy->txq_quantum;
1515 else
1516 local->hw.wiphy->txq_quantum = local->fq.quantum;
1517}
1518
Michal Kaziorfa962b92016-05-19 10:37:49 +02001519int ieee80211_txq_setup_flows(struct ieee80211_local *local)
1520{
1521 struct fq *fq = &local->fq;
1522 int ret;
Michal Kazior5caa3282016-05-19 10:37:51 +02001523 int i;
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001524 bool supp_vht = false;
1525 enum nl80211_band band;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001526
1527 if (!local->ops->wake_tx_queue)
1528 return 0;
1529
1530 ret = fq_init(fq, 4096);
1531 if (ret)
1532 return ret;
1533
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001534 /*
1535 * If the hardware doesn't support VHT, it is safe to limit the maximum
1536 * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
1537 */
1538 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1539 struct ieee80211_supported_band *sband;
1540
1541 sband = local->hw.wiphy->bands[band];
1542 if (!sband)
1543 continue;
1544
1545 supp_vht = supp_vht || sband->vht_cap.vht_supported;
1546 }
1547
1548 if (!supp_vht)
1549 fq->memory_limit = 4 << 20; /* 4 Mbytes */
1550
Michal Kazior5caa3282016-05-19 10:37:51 +02001551 codel_params_init(&local->cparams);
Michal Kazior5caa3282016-05-19 10:37:51 +02001552 local->cparams.interval = MS2TIME(100);
1553 local->cparams.target = MS2TIME(20);
1554 local->cparams.ecn = true;
1555
1556 local->cvars = kcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
1557 GFP_KERNEL);
1558 if (!local->cvars) {
Michal Kazior59a7c822016-06-29 14:00:34 +02001559 spin_lock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001560 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001561 spin_unlock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001562 return -ENOMEM;
1563 }
1564
1565 for (i = 0; i < fq->flows_cnt; i++)
1566 codel_vars_init(&local->cvars[i]);
1567
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001568 ieee80211_txq_set_params(local);
1569
Michal Kaziorfa962b92016-05-19 10:37:49 +02001570 return 0;
1571}
1572
1573void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1574{
1575 struct fq *fq = &local->fq;
1576
1577 if (!local->ops->wake_tx_queue)
1578 return;
1579
Michal Kazior5caa3282016-05-19 10:37:51 +02001580 kfree(local->cvars);
1581 local->cvars = NULL;
1582
Michal Kazior59a7c822016-06-29 14:00:34 +02001583 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001584 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001585 spin_unlock_bh(&fq->lock);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001586}
1587
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001588static bool ieee80211_queue_skb(struct ieee80211_local *local,
1589 struct ieee80211_sub_if_data *sdata,
1590 struct sta_info *sta,
1591 struct sk_buff *skb)
1592{
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001593 struct ieee80211_vif *vif;
1594 struct txq_info *txqi;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001595
1596 if (!local->ops->wake_tx_queue ||
1597 sdata->vif.type == NL80211_IFTYPE_MONITOR)
1598 return false;
1599
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001600 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1601 sdata = container_of(sdata->bss,
1602 struct ieee80211_sub_if_data, u.ap);
1603
1604 vif = &sdata->vif;
Michal Kaziordbef5362017-01-13 13:32:51 +01001605 txqi = ieee80211_get_txq(local, vif, sta, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001606
1607 if (!txqi)
1608 return false;
1609
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001610 ieee80211_txq_enqueue(local, txqi, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001611
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001612 schedule_and_wake_txq(local, txqi);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001613
1614 return true;
1615}
1616
Johannes Berg11127e92011-11-16 16:02:47 +01001617static bool ieee80211_tx_frags(struct ieee80211_local *local,
1618 struct ieee80211_vif *vif,
1619 struct ieee80211_sta *sta,
1620 struct sk_buff_head *skbs,
1621 bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001622{
Michal Kazior80a83cf2016-05-19 10:37:48 +02001623 struct ieee80211_tx_control control = {};
Johannes Berg252b86c2011-11-16 15:28:55 +01001624 struct sk_buff *skb, *tmp;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001625 unsigned long flags;
Johannes Berge2ebc742007-07-27 15:43:22 +02001626
Johannes Berg252b86c2011-11-16 15:28:55 +01001627 skb_queue_walk_safe(skbs, skb, tmp) {
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001628 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1629 int q = info->hw_queue;
1630
1631#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1632 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1633 __skb_unlink(skb, skbs);
Felix Fietkauc3e77242012-10-08 14:39:33 +02001634 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001635 continue;
1636 }
1637#endif
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001638
1639 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001640 if (local->queue_stop_reasons[q] ||
Johannes Berg7bb45682011-02-24 14:42:06 +01001641 (!txpending && !skb_queue_empty(&local->pending[q]))) {
Seth Forshee6c17b772013-02-11 11:21:07 -06001642 if (unlikely(info->flags &
Seth Forsheea7679ed2013-02-25 14:58:05 -06001643 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1644 if (local->queue_stop_reasons[q] &
1645 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1646 /*
1647 * Drop off-channel frames if queues
1648 * are stopped for any reason other
1649 * than off-channel operation. Never
1650 * queue them.
1651 */
1652 spin_unlock_irqrestore(
1653 &local->queue_stop_reason_lock,
1654 flags);
1655 ieee80211_purge_tx_queue(&local->hw,
1656 skbs);
1657 return true;
1658 }
1659 } else {
1660
Seth Forshee6c17b772013-02-11 11:21:07 -06001661 /*
Seth Forsheea7679ed2013-02-25 14:58:05 -06001662 * Since queue is stopped, queue up frames for
1663 * later transmission from the tx-pending
1664 * tasklet when the queue is woken again.
Seth Forshee6c17b772013-02-11 11:21:07 -06001665 */
Seth Forsheea7679ed2013-02-25 14:58:05 -06001666 if (txpending)
1667 skb_queue_splice_init(skbs,
1668 &local->pending[q]);
1669 else
1670 skb_queue_splice_tail_init(skbs,
1671 &local->pending[q]);
1672
1673 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1674 flags);
1675 return false;
Seth Forshee6c17b772013-02-11 11:21:07 -06001676 }
Johannes Berg7bb45682011-02-24 14:42:06 +01001677 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001678 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Tomas Winklerf8e79dd2008-07-24 18:46:44 +03001679
Johannes Berg11127e92011-11-16 16:02:47 +01001680 info->control.vif = vif;
Michal Kazior80a83cf2016-05-19 10:37:48 +02001681 control.sta = sta;
Johannes Bergec25acc2010-07-22 17:11:28 +02001682
Johannes Berg252b86c2011-11-16 15:28:55 +01001683 __skb_unlink(skb, skbs);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001684 drv_tx(local, &control, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001685 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001686
Johannes Berg11127e92011-11-16 16:02:47 +01001687 return true;
1688}
1689
1690/*
1691 * Returns false if the frame couldn't be transmitted but was queued instead.
1692 */
1693static bool __ieee80211_tx(struct ieee80211_local *local,
1694 struct sk_buff_head *skbs, int led_len,
1695 struct sta_info *sta, bool txpending)
1696{
1697 struct ieee80211_tx_info *info;
1698 struct ieee80211_sub_if_data *sdata;
1699 struct ieee80211_vif *vif;
1700 struct ieee80211_sta *pubsta;
1701 struct sk_buff *skb;
1702 bool result = true;
1703 __le16 fc;
1704
1705 if (WARN_ON(skb_queue_empty(skbs)))
1706 return true;
1707
1708 skb = skb_peek(skbs);
1709 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1710 info = IEEE80211_SKB_CB(skb);
1711 sdata = vif_to_sdata(info->control.vif);
1712 if (sta && !sta->uploaded)
1713 sta = NULL;
1714
1715 if (sta)
1716 pubsta = &sta->sta;
1717 else
1718 pubsta = NULL;
1719
1720 switch (sdata->vif.type) {
1721 case NL80211_IFTYPE_MONITOR:
Aviya Erenfeldd8212182016-08-29 23:25:15 +03001722 if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
Johannes Bergc82b5a72013-07-14 23:39:20 +03001723 vif = &sdata->vif;
1724 break;
1725 }
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001726 sdata = rcu_dereference(local->monitor_sdata);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001727 if (sdata) {
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001728 vif = &sdata->vif;
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001729 info->hw_queue =
1730 vif->hw_queue[skb_get_queue_mapping(skb)];
Johannes Berg30686bf2015-06-02 21:39:54 +02001731 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
Johannes Berg63b4d8b2015-11-24 15:41:50 +01001732 ieee80211_purge_tx_queue(&local->hw, skbs);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001733 return true;
1734 } else
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001735 vif = NULL;
Johannes Berg11127e92011-11-16 16:02:47 +01001736 break;
1737 case NL80211_IFTYPE_AP_VLAN:
1738 sdata = container_of(sdata->bss,
1739 struct ieee80211_sub_if_data, u.ap);
1740 /* fall through */
1741 default:
1742 vif = &sdata->vif;
1743 break;
1744 }
1745
Johannes Bergcb831b52012-07-02 15:40:18 +02001746 result = ieee80211_tx_frags(local, vif, pubsta, skbs,
1747 txpending);
Johannes Berg11127e92011-11-16 16:02:47 +01001748
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001749 ieee80211_tpt_led_trig_tx(local, fc, led_len);
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001750
Johannes Berg4db4e0a2011-11-24 14:47:36 +01001751 WARN_ON_ONCE(!skb_queue_empty(skbs));
Johannes Berg252b86c2011-11-16 15:28:55 +01001752
Johannes Berg11127e92011-11-16 16:02:47 +01001753 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001754}
1755
Johannes Berg97b045d2008-06-20 01:22:30 +02001756/*
1757 * Invoke TX handlers, return 0 on success and non-zero if the
1758 * frame was dropped or queued.
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001759 *
1760 * The handlers are split into an early and late part. The latter is everything
1761 * that can be sensitive to reordering, and will be deferred to after packets
1762 * are dequeued from the intermediate queues (when they are enabled).
Johannes Berg97b045d2008-06-20 01:22:30 +02001763 */
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001764static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx)
Johannes Berg97b045d2008-06-20 01:22:30 +02001765{
Johannes Berg97b045d2008-06-20 01:22:30 +02001766 ieee80211_tx_result res = TX_DROP;
Johannes Berg97b045d2008-06-20 01:22:30 +02001767
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001768#define CALL_TXH(txh) \
1769 do { \
1770 res = txh(tx); \
1771 if (res != TX_CONTINUE) \
1772 goto txh_done; \
1773 } while (0)
Johannes Berg97b045d2008-06-20 01:22:30 +02001774
Kalle Valo5c1b98a2010-01-12 10:42:46 +02001775 CALL_TXH(ieee80211_tx_h_dynamic_ps);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001776 CALL_TXH(ieee80211_tx_h_check_assoc);
1777 CALL_TXH(ieee80211_tx_h_ps_buf);
Johannes Berga621fa42010-08-27 14:26:54 +03001778 CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001779 CALL_TXH(ieee80211_tx_h_select_key);
Johannes Berg30686bf2015-06-02 21:39:54 +02001780 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
Johannes Bergaf65cd962009-11-17 18:18:36 +01001781 CALL_TXH(ieee80211_tx_h_rate_ctrl);
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001782
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001783 txh_done:
1784 if (unlikely(res == TX_DROP)) {
1785 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1786 if (tx->skb)
1787 ieee80211_free_txskb(&tx->local->hw, tx->skb);
1788 else
1789 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1790 return -1;
1791 } else if (unlikely(res == TX_QUEUED)) {
1792 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1793 return -1;
1794 }
1795
1796 return 0;
1797}
1798
1799/*
1800 * Late handlers can be called while the sta lock is held. Handlers that can
1801 * cause packets to be generated will cause deadlock!
1802 */
1803static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx)
1804{
1805 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1806 ieee80211_tx_result res = TX_CONTINUE;
1807
Johannes Bergaa5b5492011-12-02 22:08:52 +01001808 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1809 __skb_queue_tail(&tx->skbs, tx->skb);
1810 tx->skb = NULL;
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001811 goto txh_done;
Johannes Bergaa5b5492011-12-02 22:08:52 +01001812 }
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001813
1814 CALL_TXH(ieee80211_tx_h_michael_mic_add);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001815 CALL_TXH(ieee80211_tx_h_sequence);
1816 CALL_TXH(ieee80211_tx_h_fragment);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001817 /* handlers after fragment must be aware of tx info fragmentation! */
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001818 CALL_TXH(ieee80211_tx_h_stats);
1819 CALL_TXH(ieee80211_tx_h_encrypt);
Johannes Berg30686bf2015-06-02 21:39:54 +02001820 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
Arik Nemtsov8fd369e2011-01-31 22:29:12 +02001821 CALL_TXH(ieee80211_tx_h_calculate_duration);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001822#undef CALL_TXH
1823
1824 txh_done:
Johannes Berg97b045d2008-06-20 01:22:30 +02001825 if (unlikely(res == TX_DROP)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001826 I802_DEBUG_INC(tx->local->tx_handlers_drop);
Johannes Berg252b86c2011-11-16 15:28:55 +01001827 if (tx->skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001828 ieee80211_free_txskb(&tx->local->hw, tx->skb);
Johannes Berg252b86c2011-11-16 15:28:55 +01001829 else
Felix Fietkau1f98ab72012-11-10 03:44:14 +01001830 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
Johannes Berg97b045d2008-06-20 01:22:30 +02001831 return -1;
1832 } else if (unlikely(res == TX_QUEUED)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001833 I802_DEBUG_INC(tx->local->tx_handlers_queued);
Johannes Berg97b045d2008-06-20 01:22:30 +02001834 return -1;
1835 }
1836
1837 return 0;
1838}
1839
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001840static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1841{
1842 int r = invoke_tx_handlers_early(tx);
1843
1844 if (r)
1845 return r;
1846 return invoke_tx_handlers_late(tx);
1847}
1848
Felix Fietkau06be6b12013-10-14 18:01:00 +02001849bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1850 struct ieee80211_vif *vif, struct sk_buff *skb,
1851 int band, struct ieee80211_sta **sta)
1852{
1853 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1854 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1855 struct ieee80211_tx_data tx;
Johannes Berg88724a82015-03-01 09:10:12 +02001856 struct sk_buff *skb2;
Felix Fietkau06be6b12013-10-14 18:01:00 +02001857
Johannes Berg7c107702015-03-20 14:18:27 +01001858 if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
Felix Fietkau06be6b12013-10-14 18:01:00 +02001859 return false;
1860
1861 info->band = band;
1862 info->control.vif = vif;
1863 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1864
1865 if (invoke_tx_handlers(&tx))
1866 return false;
1867
1868 if (sta) {
1869 if (tx.sta)
1870 *sta = &tx.sta->sta;
1871 else
1872 *sta = NULL;
1873 }
1874
Johannes Berg88724a82015-03-01 09:10:12 +02001875 /* this function isn't suitable for fragmented data frames */
1876 skb2 = __skb_dequeue(&tx.skbs);
1877 if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1878 ieee80211_free_txskb(hw, skb2);
1879 ieee80211_purge_tx_queue(hw, &tx.skbs);
1880 return false;
1881 }
1882
Felix Fietkau06be6b12013-10-14 18:01:00 +02001883 return true;
1884}
1885EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1886
Johannes Berg7bb45682011-02-24 14:42:06 +01001887/*
1888 * Returns false if the frame couldn't be transmitted but was queued instead.
1889 */
1890static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01001891 struct sta_info *sta, struct sk_buff *skb,
Johannes Bergb9771d42018-05-28 15:47:41 +02001892 bool txpending, u32 txdata_flags)
Johannes Berge2ebc742007-07-27 15:43:22 +02001893{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001894 struct ieee80211_local *local = sdata->local;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001895 struct ieee80211_tx_data tx;
Johannes Berg97b045d2008-06-20 01:22:30 +02001896 ieee80211_tx_result res_prepare;
Johannes Berge039fa42008-05-15 12:55:29 +02001897 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001898 bool result = true;
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001899 int led_len;
Johannes Berge2ebc742007-07-27 15:43:22 +02001900
Johannes Berge2ebc742007-07-27 15:43:22 +02001901 if (unlikely(skb->len < 10)) {
1902 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001903 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001904 }
1905
Johannes Berg58d41852007-09-26 17:53:18 +02001906 /* initialises tx */
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001907 led_len = skb->len;
Johannes Berg7c107702015-03-20 14:18:27 +01001908 res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001909
Johannes Bergb9771d42018-05-28 15:47:41 +02001910 tx.flags |= txdata_flags;
1911
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001912 if (unlikely(res_prepare == TX_DROP)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02001913 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001914 return true;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001915 } else if (unlikely(res_prepare == TX_QUEUED)) {
Johannes Berg55de9082012-07-26 17:24:39 +02001916 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001917 }
1918
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001919 /* set up hw_queue value early */
1920 if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
Johannes Berg30686bf2015-06-02 21:39:54 +02001921 !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001922 info->hw_queue =
1923 sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1924
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001925 if (invoke_tx_handlers_early(&tx))
Bob Copeland6eae4a62018-09-05 06:22:59 -04001926 return true;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001927
1928 if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb))
1929 return true;
1930
1931 if (!invoke_tx_handlers_late(&tx))
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001932 result = __ieee80211_tx(local, &tx.skbs, led_len,
1933 tx.sta, txpending);
Johannes Berg55de9082012-07-26 17:24:39 +02001934
Johannes Berg7bb45682011-02-24 14:42:06 +01001935 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001936}
1937
1938/* device xmit handlers */
1939
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301940static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
Johannes Berg23c07522008-05-29 10:38:53 +02001941 struct sk_buff *skb,
1942 int head_need, bool may_encrypt)
1943{
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301944 struct ieee80211_local *local = sdata->local;
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001945 struct ieee80211_hdr *hdr;
1946 bool enc_tailroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001947 int tail_need = 0;
1948
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001949 hdr = (struct ieee80211_hdr *) skb->data;
1950 enc_tailroom = may_encrypt &&
1951 (sdata->crypto_tx_tailroom_needed_cnt ||
1952 ieee80211_is_mgmt(hdr->frame_control));
1953
1954 if (enc_tailroom) {
Johannes Berg23c07522008-05-29 10:38:53 +02001955 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1956 tail_need -= skb_tailroom(skb);
1957 tail_need = max_t(int, tail_need, 0);
1958 }
1959
Ido Yarivc70f59a2014-07-29 15:39:14 +03001960 if (skb_cloned(skb) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001961 (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001962 !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
Johannes Berg23c07522008-05-29 10:38:53 +02001963 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001964 else if (head_need || tail_need)
Johannes Berg23c07522008-05-29 10:38:53 +02001965 I802_DEBUG_INC(local->tx_expand_skb_head);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001966 else
1967 return 0;
Johannes Berg23c07522008-05-29 10:38:53 +02001968
1969 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
Joe Perches0fb9a9e2010-08-20 16:25:38 -07001970 wiphy_debug(local->hw.wiphy,
1971 "failed to reallocate TX buffer\n");
Johannes Berg23c07522008-05-29 10:38:53 +02001972 return -ENOMEM;
1973 }
1974
Johannes Berg23c07522008-05-29 10:38:53 +02001975 return 0;
1976}
1977
Johannes Berg7c107702015-03-20 14:18:27 +01001978void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
Johannes Bergb9771d42018-05-28 15:47:41 +02001979 struct sta_info *sta, struct sk_buff *skb,
1980 u32 txdata_flags)
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);
Johannes Bergb9771d42018-05-28 15:47:41 +02002015 ieee80211_tx(sdata, sta, skb, false, txdata_flags);
Johannes Berge2ebc742007-07-27 15:43:22 +02002016}
2017
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002018static bool ieee80211_parse_tx_radiotap(struct ieee80211_local *local,
2019 struct sk_buff *skb)
Johannes Berg73b9f032011-10-07 14:01:26 +02002020{
2021 struct ieee80211_radiotap_iterator iterator;
2022 struct ieee80211_radiotap_header *rthdr =
2023 (struct ieee80211_radiotap_header *) skb->data;
2024 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002025 struct ieee80211_supported_band *sband =
2026 local->hw.wiphy->bands[info->band];
Johannes Berg73b9f032011-10-07 14:01:26 +02002027 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
2028 NULL);
2029 u16 txflags;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002030 u16 rate = 0;
2031 bool rate_found = false;
2032 u8 rate_retries = 0;
2033 u16 rate_flags = 0;
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002034 u8 mcs_known, mcs_flags, mcs_bw;
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002035 u16 vht_known;
2036 u8 vht_mcs = 0, vht_nss = 0;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002037 int i;
Johannes Berg73b9f032011-10-07 14:01:26 +02002038
2039 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
2040 IEEE80211_TX_CTL_DONTFRAG;
2041
2042 /*
2043 * for every radiotap entry that is present
2044 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
2045 * entries present, or -EINVAL on error)
2046 */
2047
2048 while (!ret) {
2049 ret = ieee80211_radiotap_iterator_next(&iterator);
2050
2051 if (ret)
2052 continue;
2053
2054 /* see if this argument is something we can use */
2055 switch (iterator.this_arg_index) {
2056 /*
2057 * You must take care when dereferencing iterator.this_arg
2058 * for multibyte types... the pointer is not aligned. Use
2059 * get_unaligned((type *)iterator.this_arg) to dereference
2060 * iterator.this_arg for type "type" safely on all arches.
2061 */
2062 case IEEE80211_RADIOTAP_FLAGS:
2063 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
2064 /*
2065 * this indicates that the skb we have been
2066 * handed has the 32-bit FCS CRC at the end...
2067 * we should react to that by snipping it off
2068 * because it will be recomputed and added
2069 * on transmission
2070 */
2071 if (skb->len < (iterator._max_length + FCS_LEN))
2072 return false;
2073
2074 skb_trim(skb, skb->len - FCS_LEN);
2075 }
2076 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
2077 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
2078 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
2079 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
2080 break;
2081
2082 case IEEE80211_RADIOTAP_TX_FLAGS:
2083 txflags = get_unaligned_le16(iterator.this_arg);
2084 if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
2085 info->flags |= IEEE80211_TX_CTL_NO_ACK;
2086 break;
2087
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002088 case IEEE80211_RADIOTAP_RATE:
2089 rate = *iterator.this_arg;
2090 rate_flags = 0;
2091 rate_found = true;
2092 break;
2093
2094 case IEEE80211_RADIOTAP_DATA_RETRIES:
2095 rate_retries = *iterator.this_arg;
2096 break;
2097
2098 case IEEE80211_RADIOTAP_MCS:
2099 mcs_known = iterator.this_arg[0];
2100 mcs_flags = iterator.this_arg[1];
2101 if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
2102 break;
2103
2104 rate_found = true;
2105 rate = iterator.this_arg[2];
2106 rate_flags = IEEE80211_TX_RC_MCS;
2107
2108 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
2109 mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
2110 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2111
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002112 mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002113 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002114 mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002115 rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2116 break;
2117
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002118 case IEEE80211_RADIOTAP_VHT:
2119 vht_known = get_unaligned_le16(iterator.this_arg);
2120 rate_found = true;
2121
2122 rate_flags = IEEE80211_TX_RC_VHT_MCS;
2123 if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
2124 (iterator.this_arg[2] &
2125 IEEE80211_RADIOTAP_VHT_FLAG_SGI))
2126 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2127 if (vht_known &
2128 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
2129 if (iterator.this_arg[3] == 1)
2130 rate_flags |=
2131 IEEE80211_TX_RC_40_MHZ_WIDTH;
2132 else if (iterator.this_arg[3] == 4)
2133 rate_flags |=
2134 IEEE80211_TX_RC_80_MHZ_WIDTH;
2135 else if (iterator.this_arg[3] == 11)
2136 rate_flags |=
2137 IEEE80211_TX_RC_160_MHZ_WIDTH;
2138 }
2139
2140 vht_mcs = iterator.this_arg[4] >> 4;
2141 vht_nss = iterator.this_arg[4] & 0xF;
2142 break;
2143
Johannes Berg73b9f032011-10-07 14:01:26 +02002144 /*
2145 * Please update the file
2146 * Documentation/networking/mac80211-injection.txt
2147 * when parsing new fields here.
2148 */
2149
2150 default:
2151 break;
2152 }
2153 }
2154
2155 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
2156 return false;
2157
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002158 if (rate_found) {
2159 info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
2160
2161 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2162 info->control.rates[i].idx = -1;
2163 info->control.rates[i].flags = 0;
2164 info->control.rates[i].count = 0;
2165 }
2166
2167 if (rate_flags & IEEE80211_TX_RC_MCS) {
2168 info->control.rates[0].idx = rate;
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002169 } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
2170 ieee80211_rate_set_vht(info->control.rates, vht_mcs,
2171 vht_nss);
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002172 } else {
2173 for (i = 0; i < sband->n_bitrates; i++) {
2174 if (rate * 5 != sband->bitrates[i].bitrate)
2175 continue;
2176
2177 info->control.rates[0].idx = i;
2178 break;
2179 }
2180 }
2181
Felix Fietkau07310a62016-03-02 15:54:51 +01002182 if (info->control.rates[0].idx < 0)
2183 info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
2184
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002185 info->control.rates[0].flags = rate_flags;
2186 info->control.rates[0].count = min_t(u8, rate_retries + 1,
2187 local->hw.max_rate_tries);
2188 }
2189
Johannes Berg73b9f032011-10-07 14:01:26 +02002190 /*
2191 * remove the radiotap header
2192 * iterator->_max_length was sanity-checked against
2193 * skb->len by iterator init
2194 */
2195 skb_pull(skb, iterator._max_length);
2196
2197 return true;
2198}
2199
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +00002200netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
2201 struct net_device *dev)
Johannes Berge2ebc742007-07-27 15:43:22 +02002202{
2203 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg55de9082012-07-26 17:24:39 +02002204 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berge2ebc742007-07-27 15:43:22 +02002205 struct ieee80211_radiotap_header *prthdr =
2206 (struct ieee80211_radiotap_header *)skb->data;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002207 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002208 struct ieee80211_hdr *hdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002209 struct ieee80211_sub_if_data *tmp_sdata, *sdata;
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002210 struct cfg80211_chan_def *chandef;
Andy Green9b8a74e2007-07-27 15:43:24 +02002211 u16 len_rthdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002212 int hdrlen;
Johannes Berge2ebc742007-07-27 15:43:22 +02002213
Andy Green9b8a74e2007-07-27 15:43:24 +02002214 /* check for not even having the fixed radiotap header part */
2215 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2216 goto fail; /* too short to be possibly valid */
2217
2218 /* is it a header version we can trust to find length from? */
2219 if (unlikely(prthdr->it_version))
2220 goto fail; /* only version 0 is supported */
2221
2222 /* then there must be a radiotap header with a length we can use */
2223 len_rthdr = ieee80211_get_radiotap_len(skb->data);
2224
2225 /* does the skb contain enough to deliver on the alleged length? */
2226 if (unlikely(skb->len < len_rthdr))
2227 goto fail; /* skb too short for claimed rt header extent */
Johannes Berge2ebc742007-07-27 15:43:22 +02002228
Johannes Berge2ebc742007-07-27 15:43:22 +02002229 /*
2230 * fix up the pointers accounting for the radiotap
2231 * header still being in there. We are being given
2232 * a precooked IEEE80211 header so no need for
2233 * normal processing
2234 */
Andy Green9b8a74e2007-07-27 15:43:24 +02002235 skb_set_mac_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02002236 /*
Andy Green9b8a74e2007-07-27 15:43:24 +02002237 * these are just fixed to the end of the rt area since we
2238 * don't have any better information and at this point, nobody cares
Johannes Berge2ebc742007-07-27 15:43:22 +02002239 */
Andy Green9b8a74e2007-07-27 15:43:24 +02002240 skb_set_network_header(skb, len_rthdr);
2241 skb_set_transport_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02002242
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002243 if (skb->len < len_rthdr + 2)
2244 goto fail;
2245
2246 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2247 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2248
2249 if (skb->len < len_rthdr + hdrlen)
2250 goto fail;
2251
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002252 /*
2253 * Initialize skb->protocol if the injected frame is a data frame
2254 * carrying a rfc1042 header
2255 */
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002256 if (ieee80211_is_data(hdr->frame_control) &&
2257 skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2258 u8 *payload = (u8 *)hdr + hdrlen;
2259
Joe Perchesb203ca32012-05-08 18:56:52 +00002260 if (ether_addr_equal(payload, rfc1042_header))
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002261 skb->protocol = cpu_to_be16((payload[6] << 8) |
2262 payload[7]);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002263 }
2264
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002265 memset(info, 0, sizeof(*info));
2266
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002267 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
Johannes Berg73b9f032011-10-07 14:01:26 +02002268 IEEE80211_TX_CTL_INJECTED;
2269
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002270 rcu_read_lock();
2271
2272 /*
2273 * We process outgoing injected frames that have a local address
2274 * we handle as though they are non-injected frames.
2275 * This code here isn't entirely correct, the local MAC address
2276 * isn't always enough to find the interface to use; for proper
2277 * VLAN/WDS support we will need a different mechanism (which
2278 * likely isn't going to be monitor interfaces).
2279 */
2280 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2281
2282 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2283 if (!ieee80211_sdata_running(tmp_sdata))
2284 continue;
2285 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2286 tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2287 tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
2288 continue;
Joe Perchesb203ca32012-05-08 18:56:52 +00002289 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002290 sdata = tmp_sdata;
2291 break;
2292 }
2293 }
Johannes Berg7351c6b2009-11-19 01:08:30 +01002294
Johannes Berg55de9082012-07-26 17:24:39 +02002295 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2296 if (!chanctx_conf) {
2297 tmp_sdata = rcu_dereference(local->monitor_sdata);
2298 if (tmp_sdata)
2299 chanctx_conf =
2300 rcu_dereference(tmp_sdata->vif.chanctx_conf);
2301 }
Johannes Berg55de9082012-07-26 17:24:39 +02002302
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002303 if (chanctx_conf)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002304 chandef = &chanctx_conf->def;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002305 else if (!local->use_chanctx)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002306 chandef = &local->_oper_chandef;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002307 else
2308 goto fail_rcu;
Johannes Berg55de9082012-07-26 17:24:39 +02002309
2310 /*
2311 * Frame injection is not allowed if beaconing is not allowed
2312 * or if we need radar detection. Beaconing is usually not allowed when
2313 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2314 * Passive scan is also used in world regulatory domains where
2315 * your country is not known and as such it should be treated as
2316 * NO TX unless the channel is explicitly allowed in which case
2317 * your current regulatory domain would not have the passive scan
2318 * flag.
2319 *
2320 * Since AP mode uses monitor interfaces to inject/TX management
2321 * frames we can make AP mode the exception to this rule once it
2322 * supports radar detection as its implementation can deal with
2323 * radar detection by itself. We can do that later by adding a
2324 * monitor flag interfaces used for AP support.
2325 */
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002326 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2327 sdata->vif.type))
Johannes Berg55de9082012-07-26 17:24:39 +02002328 goto fail_rcu;
2329
Johannes Berg73c4e192014-11-09 18:50:09 +02002330 info->band = chandef->chan->band;
Lorenzo Bianconi109843b02016-02-19 12:18:01 +01002331
2332 /* process and remove the injection radiotap header */
2333 if (!ieee80211_parse_tx_radiotap(local, skb))
2334 goto fail_rcu;
2335
Johannes Bergb9771d42018-05-28 15:47:41 +02002336 ieee80211_xmit(sdata, NULL, skb, 0);
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002337 rcu_read_unlock();
2338
Johannes Berge2ebc742007-07-27 15:43:22 +02002339 return NETDEV_TX_OK;
Andy Green9b8a74e2007-07-27 15:43:24 +02002340
Johannes Berg55de9082012-07-26 17:24:39 +02002341fail_rcu:
2342 rcu_read_unlock();
Andy Green9b8a74e2007-07-27 15:43:24 +02002343fail:
2344 dev_kfree_skb(skb);
2345 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
Johannes Berge2ebc742007-07-27 15:43:22 +02002346}
2347
Johannes Berg97ffe752015-03-21 09:13:45 +01002348static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002349{
Johannes Berg97ffe752015-03-21 09:13:45 +01002350 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002351
Johannes Berg97ffe752015-03-21 09:13:45 +01002352 return ethertype == ETH_P_TDLS &&
2353 skb->len > 14 &&
2354 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2355}
2356
2357static int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2358 struct sk_buff *skb,
2359 struct sta_info **sta_out)
2360{
2361 struct sta_info *sta;
2362
2363 switch (sdata->vif.type) {
2364 case NL80211_IFTYPE_AP_VLAN:
2365 sta = rcu_dereference(sdata->u.vlan.sta);
2366 if (sta) {
2367 *sta_out = sta;
2368 return 0;
2369 } else if (sdata->wdev.use_4addr) {
2370 return -ENOLINK;
2371 }
2372 /* fall through */
2373 case NL80211_IFTYPE_AP:
2374 case NL80211_IFTYPE_OCB:
2375 case NL80211_IFTYPE_ADHOC:
2376 if (is_multicast_ether_addr(skb->data)) {
2377 *sta_out = ERR_PTR(-ENOENT);
2378 return 0;
2379 }
2380 sta = sta_info_get_bss(sdata, skb->data);
2381 break;
2382 case NL80211_IFTYPE_WDS:
2383 sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
2384 break;
2385#ifdef CONFIG_MAC80211_MESH
2386 case NL80211_IFTYPE_MESH_POINT:
2387 /* determined much later */
2388 *sta_out = NULL;
2389 return 0;
2390#endif
2391 case NL80211_IFTYPE_STATION:
2392 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2393 sta = sta_info_get(sdata, skb->data);
Johannes Berg11d62ca2016-09-13 08:28:22 +02002394 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2395 if (test_sta_flag(sta,
2396 WLAN_STA_TDLS_PEER_AUTH)) {
Johannes Berg97ffe752015-03-21 09:13:45 +01002397 *sta_out = sta;
2398 return 0;
2399 }
2400
2401 /*
2402 * TDLS link during setup - throw out frames to
2403 * peer. Allow TDLS-setup frames to unauthorized
2404 * peers for the special case of a link teardown
2405 * after a TDLS sta is removed due to being
2406 * unreachable.
2407 */
Johannes Berg11d62ca2016-09-13 08:28:22 +02002408 if (!ieee80211_is_tdls_setup(skb))
Johannes Berg97ffe752015-03-21 09:13:45 +01002409 return -EINVAL;
2410 }
2411
2412 }
2413
2414 sta = sta_info_get(sdata, sdata->u.mgd.bssid);
2415 if (!sta)
2416 return -ENOLINK;
2417 break;
2418 default:
2419 return -EINVAL;
2420 }
2421
2422 *sta_out = sta ?: ERR_PTR(-ENOENT);
2423 return 0;
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002424}
2425
Johannes Berge2ebc742007-07-27 15:43:22 +02002426/**
Johannes Berg4c9451e2014-11-09 18:50:10 +02002427 * ieee80211_build_hdr - build 802.11 header in the given frame
2428 * @sdata: virtual interface to build the header for
2429 * @skb: the skb to build the header in
Liad Kaufman24d342c2014-11-09 18:50:07 +02002430 * @info_flags: skb flags to set
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002431 * @ctrl_flags: info control flags to set
Johannes Berge2ebc742007-07-27 15:43:22 +02002432 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002433 * This function takes the skb with 802.3 header and reformats the header to
2434 * the appropriate IEEE 802.11 header based on which interface the packet is
2435 * being transmitted on.
Johannes Berge2ebc742007-07-27 15:43:22 +02002436 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002437 * Note that this function also takes care of the TX status request and
2438 * potential unsharing of the SKB - this needs to be interleaved with the
2439 * header building.
2440 *
2441 * The function requires the read-side RCU lock held
2442 *
2443 * Returns: the (possibly reallocated) skb or an ERR_PTR() code
Johannes Berge2ebc742007-07-27 15:43:22 +02002444 */
Johannes Berg4c9451e2014-11-09 18:50:10 +02002445static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01002446 struct sk_buff *skb, u32 info_flags,
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002447 struct sta_info *sta, u32 ctrl_flags)
Johannes Berge2ebc742007-07-27 15:43:22 +02002448{
Johannes Berg133b8222008-09-16 14:18:59 +02002449 struct ieee80211_local *local = sdata->local;
Felix Fietkau489ee912010-12-18 19:30:48 +01002450 struct ieee80211_tx_info *info;
Johannes Bergdcf33962012-07-30 15:11:56 +02002451 int head_need;
Harvey Harrison065e96052008-06-22 16:45:27 -07002452 u16 ethertype, hdrlen, meshhdrlen = 0;
2453 __le16 fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002454 struct ieee80211_hdr hdr;
Wey-Yi Guyff67bb82010-08-21 07:23:29 -07002455 struct ieee80211s_hdr mesh_hdr __maybe_unused;
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002456 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02002457 const u8 *encaps_data;
2458 int encaps_len, skip_header_bytes;
Johannes Berg97ffe752015-03-21 09:13:45 +01002459 bool wme_sta = false, authorized = false;
2460 bool tdls_peer;
Johannes Berga729cff2011-11-06 14:13:34 +01002461 bool multicast;
Johannes Berga729cff2011-11-06 14:13:34 +01002462 u16 info_id = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002463 struct ieee80211_chanctx_conf *chanctx_conf;
2464 struct ieee80211_sub_if_data *ap_sdata;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002465 enum nl80211_band band;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002466 int ret;
Johannes Berge2ebc742007-07-27 15:43:22 +02002467
Johannes Berg97ffe752015-03-21 09:13:45 +01002468 if (IS_ERR(sta))
2469 sta = NULL;
2470
Julius Niedworok276d9e82019-03-28 21:01:06 +01002471#ifdef CONFIG_MAC80211_DEBUGFS
2472 if (local->force_tx_status)
2473 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2474#endif
2475
Johannes Berge2ebc742007-07-27 15:43:22 +02002476 /* convert Ethernet header to proper 802.11 header (based on
2477 * operation mode) */
2478 ethertype = (skb->data[12] << 8) | skb->data[13];
Harvey Harrison065e96052008-06-22 16:45:27 -07002479 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
Johannes Berge2ebc742007-07-27 15:43:22 +02002480
Johannes Berg51fb61e2007-12-19 01:31:27 +01002481 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002482 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg97ffe752015-03-21 09:13:45 +01002483 if (sdata->wdev.use_4addr) {
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002484 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2485 /* RA TA DA SA */
2486 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002487 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002488 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2489 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2490 hdrlen = 30;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002491 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Johannes Berga74a8c82014-07-22 14:50:47 +02002492 wme_sta = sta->sta.wme;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002493 }
Johannes Berg55de9082012-07-26 17:24:39 +02002494 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2495 u.ap);
2496 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002497 if (!chanctx_conf) {
2498 ret = -ENOTCONN;
2499 goto free;
2500 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002501 band = chanctx_conf->def.chan->band;
Johannes Berg97ffe752015-03-21 09:13:45 +01002502 if (sdata->wdev.use_4addr)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002503 break;
2504 /* fall through */
2505 case NL80211_IFTYPE_AP:
Arnd Bergmannfe801232013-01-25 14:14:33 +00002506 if (sdata->vif.type == NL80211_IFTYPE_AP)
2507 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002508 if (!chanctx_conf) {
2509 ret = -ENOTCONN;
2510 goto free;
2511 }
Harvey Harrison065e96052008-06-22 16:45:27 -07002512 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
Johannes Berge2ebc742007-07-27 15:43:22 +02002513 /* DA BSSID SA */
2514 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002515 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002516 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2517 hdrlen = 24;
Johannes Berg4bf88532012-11-09 11:39:59 +01002518 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002519 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002520 case NL80211_IFTYPE_WDS:
Harvey Harrison065e96052008-06-22 16:45:27 -07002521 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
Johannes Berge2ebc742007-07-27 15:43:22 +02002522 /* RA TA DA SA */
2523 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002524 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002525 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2526 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2527 hdrlen = 30;
Johannes Berg55de9082012-07-26 17:24:39 +02002528 /*
2529 * This is the exception! WDS style interfaces are prohibited
2530 * when channel contexts are in used so this must be valid
2531 */
Karl Beldan675a0b02013-03-25 16:26:57 +01002532 band = local->hw.conf.chandef.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002533 break;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002534#ifdef CONFIG_MAC80211_MESH
Johannes Berg05c914f2008-09-11 00:01:58 +02002535 case NL80211_IFTYPE_MESH_POINT:
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002536 if (!is_multicast_ether_addr(skb->data)) {
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002537 struct sta_info *next_hop;
2538 bool mpp_lookup = true;
2539
Johannes Bergbf7cd942013-02-15 14:40:31 +01002540 mpath = mesh_path_lookup(sdata, skb->data);
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002541 if (mpath) {
2542 mpp_lookup = false;
2543 next_hop = rcu_dereference(mpath->next_hop);
2544 if (!next_hop ||
2545 !(mpath->flags & (MESH_PATH_ACTIVE |
2546 MESH_PATH_RESOLVING)))
2547 mpp_lookup = true;
2548 }
2549
Henning Roggeab1c7902016-02-03 13:58:37 +01002550 if (mpp_lookup) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002551 mppath = mpp_path_lookup(sdata, skb->data);
Henning Roggeab1c7902016-02-03 13:58:37 +01002552 if (mppath)
2553 mppath->exp_time = jiffies;
2554 }
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002555
2556 if (mppath && mpath)
Bob Copeland2bdaf382016-02-28 20:03:56 -05002557 mesh_path_del(sdata, mpath->dst);
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002558 }
YanBo79617de2008-09-22 13:30:32 +08002559
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002560 /*
Joel A Fernandes9d525012011-01-10 00:44:23 -06002561 * Use address extension if it is a packet from
2562 * another interface or if we know the destination
2563 * is being proxied by a portal (i.e. portal address
2564 * differs from proxied address)
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002565 */
Joe Perchesb203ca32012-05-08 18:56:52 +00002566 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2567 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
Javier Cardona3c5772a2009-08-10 12:15:48 -07002568 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2569 skb->data, skb->data + ETH_ALEN);
Johannes Bergbf7cd942013-02-15 14:40:31 +01002570 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2571 NULL, NULL);
YanBo79617de2008-09-22 13:30:32 +08002572 } else {
Thomas Pedersen27f01122012-08-20 11:28:25 -07002573 /* DS -> MBSS (802.11-2012 13.11.3.3).
2574 * For unicast with unknown forwarding information,
2575 * destination might be in the MBSS or if that fails
2576 * forwarded to another mesh gate. In either case
2577 * resolution will be handled in ieee80211_xmit(), so
2578 * leave the original DA. This also works for mcast */
2579 const u8 *mesh_da = skb->data;
YanBo79617de2008-09-22 13:30:32 +08002580
Thomas Pedersen27f01122012-08-20 11:28:25 -07002581 if (mppath)
2582 mesh_da = mppath->mpp;
2583 else if (mpath)
2584 mesh_da = mpath->dst;
Thomas Pedersen27f01122012-08-20 11:28:25 -07002585
Javier Cardona3c5772a2009-08-10 12:15:48 -07002586 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
Johannes Berg47846c92009-11-25 17:46:19 +01002587 mesh_da, sdata->vif.addr);
Thomas Pedersen27f01122012-08-20 11:28:25 -07002588 if (is_multicast_ether_addr(mesh_da))
2589 /* DA TA mSA AE:SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002590 meshhdrlen = ieee80211_new_mesh_header(
2591 sdata, &mesh_hdr,
2592 skb->data + ETH_ALEN, NULL);
Javier Cardona3c5772a2009-08-10 12:15:48 -07002593 else
Thomas Pedersen27f01122012-08-20 11:28:25 -07002594 /* RA TA mDA mSA AE:DA SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002595 meshhdrlen = ieee80211_new_mesh_header(
2596 sdata, &mesh_hdr, skb->data,
2597 skb->data + ETH_ALEN);
YanBo79617de2008-09-22 13:30:32 +08002598
YanBo79617de2008-09-22 13:30:32 +08002599 }
Johannes Berg55de9082012-07-26 17:24:39 +02002600 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002601 if (!chanctx_conf) {
2602 ret = -ENOTCONN;
2603 goto free;
2604 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002605 band = chanctx_conf->def.chan->band;
Rajkumar Manoharan8828f812019-04-11 13:47:26 -07002606
2607 /* For injected frames, fill RA right away as nexthop lookup
2608 * will be skipped.
2609 */
2610 if ((ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP) &&
2611 is_zero_ether_addr(hdr.addr1))
2612 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002613 break;
2614#endif
Johannes Berg05c914f2008-09-11 00:01:58 +02002615 case NL80211_IFTYPE_STATION:
Johannes Berg97ffe752015-03-21 09:13:45 +01002616 /* we already did checks when looking up the RA STA */
2617 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002618
Johannes Berg97ffe752015-03-21 09:13:45 +01002619 if (tdls_peer) {
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002620 /* DA SA BSSID */
2621 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2622 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2623 memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2624 hdrlen = 24;
2625 } else if (sdata->u.mgd.use_4addr &&
2626 cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2627 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2628 IEEE80211_FCTL_TODS);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002629 /* RA TA DA SA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002630 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002631 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002632 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2633 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2634 hdrlen = 30;
2635 } else {
2636 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2637 /* BSSID SA DA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002638 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002639 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2640 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2641 hdrlen = 24;
2642 }
Johannes Berg55de9082012-07-26 17:24:39 +02002643 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002644 if (!chanctx_conf) {
2645 ret = -ENOTCONN;
2646 goto free;
2647 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002648 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002649 break;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002650 case NL80211_IFTYPE_OCB:
2651 /* DA SA BSSID */
2652 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2653 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2654 eth_broadcast_addr(hdr.addr3);
2655 hdrlen = 24;
2656 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002657 if (!chanctx_conf) {
2658 ret = -ENOTCONN;
2659 goto free;
2660 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002661 band = chanctx_conf->def.chan->band;
2662 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002663 case NL80211_IFTYPE_ADHOC:
Johannes Berge2ebc742007-07-27 15:43:22 +02002664 /* DA SA BSSID */
2665 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2666 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01002667 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002668 hdrlen = 24;
Johannes Berg55de9082012-07-26 17:24:39 +02002669 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002670 if (!chanctx_conf) {
2671 ret = -ENOTCONN;
2672 goto free;
2673 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002674 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002675 break;
2676 default:
Johannes Berg4c9451e2014-11-09 18:50:10 +02002677 ret = -EINVAL;
2678 goto free;
Johannes Berge2ebc742007-07-27 15:43:22 +02002679 }
2680
Johannes Berga729cff2011-11-06 14:13:34 +01002681 multicast = is_multicast_ether_addr(hdr.addr1);
Johannes Berge2ebc742007-07-27 15:43:22 +02002682
Johannes Berg97ffe752015-03-21 09:13:45 +01002683 /* sta is always NULL for mesh */
2684 if (sta) {
2685 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2686 wme_sta = sta->sta.wme;
2687 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2688 /* For mesh, the use of the QoS header is mandatory */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002689 wme_sta = true;
Johannes Berge2ebc742007-07-27 15:43:22 +02002690 }
2691
Johannes Berg527871d2015-03-21 08:09:55 +01002692 /* receiver does QoS (which also means we do) use it */
2693 if (wme_sta) {
Harvey Harrison065e96052008-06-22 16:45:27 -07002694 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002695 hdrlen += 2;
2696 }
2697
2698 /*
Johannes Berg238814f2008-01-28 17:19:37 +01002699 * Drop unicast frames to unauthorised stations unless they are
2700 * EAPOL frames from the local station.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002701 */
Johannes Berg55182e42011-10-12 17:28:21 +02002702 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002703 (sdata->vif.type != NL80211_IFTYPE_OCB) &&
Sergey Ryazanova6ececf2013-08-30 01:35:09 +04002704 !multicast && !authorized &&
Johannes Berg55182e42011-10-12 17:28:21 +02002705 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002706 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002707#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002708 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
Johannes Berg4c9451e2014-11-09 18:50:10 +02002709 sdata->name, hdr.addr1);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002710#endif
2711
2712 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2713
Johannes Berg4c9451e2014-11-09 18:50:10 +02002714 ret = -EPERM;
2715 goto free;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002716 }
2717
Johannes Berga729cff2011-11-06 14:13:34 +01002718 if (unlikely(!multicast && skb->sk &&
2719 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)) {
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002720 struct sk_buff *ack_skb = skb_clone_sk(skb);
Johannes Berga729cff2011-11-06 14:13:34 +01002721
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002722 if (ack_skb) {
Johannes Berga729cff2011-11-06 14:13:34 +01002723 unsigned long flags;
Tejun Heo9475af62013-02-27 17:04:59 -08002724 int id;
Johannes Berga729cff2011-11-06 14:13:34 +01002725
2726 spin_lock_irqsave(&local->ack_status_lock, flags);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002727 id = idr_alloc(&local->ack_status_frames, ack_skb,
Tejun Heo9475af62013-02-27 17:04:59 -08002728 1, 0x10000, GFP_ATOMIC);
Johannes Berga729cff2011-11-06 14:13:34 +01002729 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2730
Tejun Heo9475af62013-02-27 17:04:59 -08002731 if (id >= 0) {
Johannes Berga729cff2011-11-06 14:13:34 +01002732 info_id = id;
2733 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Berga729cff2011-11-06 14:13:34 +01002734 } else {
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002735 kfree_skb(ack_skb);
Johannes Berga729cff2011-11-06 14:13:34 +01002736 }
Johannes Berga729cff2011-11-06 14:13:34 +01002737 }
2738 }
2739
Helmut Schaa7e244702010-12-02 18:44:09 +01002740 /*
2741 * If the skb is shared we need to obtain our own copy.
2742 */
2743 if (skb_shared(skb)) {
Johannes Berga729cff2011-11-06 14:13:34 +01002744 struct sk_buff *tmp_skb = skb;
2745
2746 /* can't happen -- skb is a clone if info_id != 0 */
2747 WARN_ON(info_id);
2748
Felix Fietkauf8a0a782010-12-18 19:30:50 +01002749 skb = skb_clone(skb, GFP_ATOMIC);
Helmut Schaa7e244702010-12-02 18:44:09 +01002750 kfree_skb(tmp_skb);
2751
Johannes Berg4c9451e2014-11-09 18:50:10 +02002752 if (!skb) {
2753 ret = -ENOMEM;
2754 goto free;
2755 }
Helmut Schaa7e244702010-12-02 18:44:09 +01002756 }
2757
Harvey Harrison065e96052008-06-22 16:45:27 -07002758 hdr.frame_control = fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002759 hdr.duration_id = 0;
2760 hdr.seq_ctrl = 0;
2761
2762 skip_header_bytes = ETH_HLEN;
2763 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2764 encaps_data = bridge_tunnel_header;
2765 encaps_len = sizeof(bridge_tunnel_header);
2766 skip_header_bytes -= 2;
Simon Hormane5c5d222013-03-28 13:38:25 +09002767 } else if (ethertype >= ETH_P_802_3_MIN) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002768 encaps_data = rfc1042_header;
2769 encaps_len = sizeof(rfc1042_header);
2770 skip_header_bytes -= 2;
2771 } else {
2772 encaps_data = NULL;
2773 encaps_len = 0;
2774 }
2775
2776 skb_pull(skb, skip_header_bytes);
Johannes Berg23c07522008-05-29 10:38:53 +02002777 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002778
Johannes Berg23c07522008-05-29 10:38:53 +02002779 /*
2780 * So we need to modify the skb header and hence need a copy of
2781 * that. The head_need variable above doesn't, so far, include
2782 * the needed header space that we don't need right away. If we
2783 * can, then we don't reallocate right now but only after the
2784 * frame arrives at the master device (if it does...)
2785 *
2786 * If we cannot, however, then we will reallocate to include all
2787 * the ever needed space. Also, if we need to reallocate it anyway,
2788 * make it big enough for everything we may ever need.
2789 */
Johannes Berge2ebc742007-07-27 15:43:22 +02002790
David S. Miller3a5be7d2008-06-18 01:19:51 -07002791 if (head_need > 0 || skb_cloned(skb)) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002792 head_need += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02002793 head_need += local->tx_headroom;
2794 head_need = max_t(int, 0, head_need);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002795 if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
2796 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg1c963be2012-11-07 14:02:30 +01002797 skb = NULL;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002798 return ERR_PTR(-ENOMEM);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002799 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002800 }
2801
Felix Fietkaueae44302016-07-13 11:00:02 +02002802 if (encaps_data)
Johannes Berge2ebc742007-07-27 15:43:22 +02002803 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002804
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002805#ifdef CONFIG_MAC80211_MESH
Felix Fietkaueae44302016-07-13 11:00:02 +02002806 if (meshhdrlen > 0)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002807 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002808#endif
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002809
Harvey Harrison065e96052008-06-22 16:45:27 -07002810 if (ieee80211_is_data_qos(fc)) {
Johannes Bergc29b9b92007-09-14 11:10:24 -04002811 __le16 *qos_control;
2812
Johannes Bergd58ff352017-06-16 14:29:23 +02002813 qos_control = skb_push(skb, 2);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002814 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2815 /*
2816 * Maybe we could actually set some fields here, for now just
2817 * initialise to zero to indicate no special operation.
2818 */
2819 *qos_control = 0;
2820 } else
2821 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2822
Zhang Shengjud57a5442016-03-03 01:16:56 +00002823 skb_reset_mac_header(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002824
Felix Fietkau489ee912010-12-18 19:30:48 +01002825 info = IEEE80211_SKB_CB(skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002826 memset(info, 0, sizeof(*info));
2827
Johannes Berga729cff2011-11-06 14:13:34 +01002828 info->flags = info_flags;
2829 info->ack_frame_id = info_id;
Johannes Berg73c4e192014-11-09 18:50:09 +02002830 info->band = band;
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002831 info->control.flags = ctrl_flags;
Johannes Berga729cff2011-11-06 14:13:34 +01002832
Johannes Berg4c9451e2014-11-09 18:50:10 +02002833 return skb;
2834 free:
2835 kfree_skb(skb);
2836 return ERR_PTR(ret);
Johannes Berge2ebc742007-07-27 15:43:22 +02002837}
2838
Johannes Berg17c18bf2015-03-21 15:25:43 +01002839/*
2840 * fast-xmit overview
2841 *
2842 * The core idea of this fast-xmit is to remove per-packet checks by checking
2843 * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2844 * checks that are needed to get the sta->fast_tx pointer assigned, after which
2845 * much less work can be done per packet. For example, fragmentation must be
2846 * disabled or the fast_tx pointer will not be set. All the conditions are seen
2847 * in the code here.
2848 *
2849 * Once assigned, the fast_tx data structure also caches the per-packet 802.11
2850 * header and other data to aid packet processing in ieee80211_xmit_fast().
2851 *
2852 * The most difficult part of this is that when any of these assumptions
2853 * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
2854 * ieee80211_check_fast_xmit() or friends) is required to reset the data,
2855 * since the per-packet code no longer checks the conditions. This is reflected
2856 * by the calls to these functions throughout the rest of the code, and must be
2857 * maintained if any of the TX path checks change.
2858 */
2859
2860void ieee80211_check_fast_xmit(struct sta_info *sta)
2861{
2862 struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
2863 struct ieee80211_local *local = sta->local;
2864 struct ieee80211_sub_if_data *sdata = sta->sdata;
2865 struct ieee80211_hdr *hdr = (void *)build.hdr;
2866 struct ieee80211_chanctx_conf *chanctx_conf;
2867 __le16 fc;
2868
Johannes Berg30686bf2015-06-02 21:39:54 +02002869 if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002870 return;
2871
2872 /* Locking here protects both the pointer itself, and against concurrent
2873 * invocations winning data access races to, e.g., the key pointer that
2874 * is used.
2875 * Without it, the invocation of this function right after the key
2876 * pointer changes wouldn't be sufficient, as another CPU could access
2877 * the pointer, then stall, and then do the cache update after the CPU
2878 * that invalidated the key.
2879 * With the locking, such scenarios cannot happen as the check for the
2880 * key and the fast-tx assignment are done atomically, so the CPU that
2881 * modifies the key will either wait or other one will see the key
2882 * cleared/changed already.
2883 */
2884 spin_lock_bh(&sta->lock);
Johannes Berg30686bf2015-06-02 21:39:54 +02002885 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
2886 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
Johannes Berg17c18bf2015-03-21 15:25:43 +01002887 sdata->vif.type == NL80211_IFTYPE_STATION)
2888 goto out;
2889
2890 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2891 goto out;
2892
2893 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
2894 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
Felix Fietkauf7418bc2015-09-24 14:59:49 +02002895 test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
2896 test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002897 goto out;
2898
2899 if (sdata->noack_map)
2900 goto out;
2901
2902 /* fast-xmit doesn't handle fragmentation at all */
Johannes Berg725b8122015-04-10 14:02:08 +02002903 if (local->hw.wiphy->frag_threshold != (u32)-1 &&
Sara Sharonf3fe4e92016-10-18 23:12:11 +03002904 !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002905 goto out;
2906
2907 rcu_read_lock();
2908 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2909 if (!chanctx_conf) {
2910 rcu_read_unlock();
2911 goto out;
2912 }
2913 build.band = chanctx_conf->def.chan->band;
2914 rcu_read_unlock();
2915
2916 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2917
2918 switch (sdata->vif.type) {
Johannes Berg3ffd8842015-04-14 10:28:37 +02002919 case NL80211_IFTYPE_ADHOC:
2920 /* DA SA BSSID */
2921 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2922 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2923 memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
2924 build.hdr_len = 24;
2925 break;
Johannes Berg17c18bf2015-03-21 15:25:43 +01002926 case NL80211_IFTYPE_STATION:
2927 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2928 /* DA SA BSSID */
2929 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2930 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2931 memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
2932 build.hdr_len = 24;
2933 break;
2934 }
2935
2936 if (sdata->u.mgd.use_4addr) {
2937 /* non-regular ethertype cannot use the fastpath */
2938 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2939 IEEE80211_FCTL_TODS);
2940 /* RA TA DA SA */
2941 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2942 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2943 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2944 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2945 build.hdr_len = 30;
2946 break;
2947 }
2948 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2949 /* BSSID SA DA */
2950 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2951 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2952 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2953 build.hdr_len = 24;
2954 break;
2955 case NL80211_IFTYPE_AP_VLAN:
2956 if (sdata->wdev.use_4addr) {
2957 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2958 IEEE80211_FCTL_TODS);
2959 /* RA TA DA SA */
2960 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
2961 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2962 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2963 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2964 build.hdr_len = 30;
2965 break;
2966 }
2967 /* fall through */
2968 case NL80211_IFTYPE_AP:
2969 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2970 /* DA BSSID SA */
2971 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2972 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2973 build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
2974 build.hdr_len = 24;
2975 break;
2976 default:
2977 /* not handled on fast-xmit */
2978 goto out;
2979 }
2980
2981 if (sta->sta.wme) {
2982 build.hdr_len += 2;
2983 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2984 }
2985
2986 /* We store the key here so there's no point in using rcu_dereference()
2987 * but that's fine because the code that changes the pointers will call
2988 * this function after doing so. For a single CPU that would be enough,
2989 * for multiple see the comment above.
2990 */
2991 build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
2992 if (!build.key)
2993 build.key = rcu_access_pointer(sdata->default_unicast_key);
2994 if (build.key) {
Johannes Berge495c242015-04-10 14:03:17 +02002995 bool gen_iv, iv_spc, mmic;
Johannes Berg17c18bf2015-03-21 15:25:43 +01002996
2997 gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
2998 iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
David Spinadel9de18d82017-12-01 13:50:52 +02002999 mmic = build.key->conf.flags &
3000 (IEEE80211_KEY_FLAG_GENERATE_MMIC |
3001 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003002
3003 /* don't handle software crypto */
3004 if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
3005 goto out;
3006
Alexander Wetzel62872a92018-08-31 15:00:38 +02003007 /* Key is being removed */
3008 if (build.key->flags & KEY_FLAG_TAINTED)
3009 goto out;
3010
Johannes Berg17c18bf2015-03-21 15:25:43 +01003011 switch (build.key->conf.cipher) {
3012 case WLAN_CIPHER_SUITE_CCMP:
3013 case WLAN_CIPHER_SUITE_CCMP_256:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003014 if (gen_iv)
Johannes Berg17c18bf2015-03-21 15:25:43 +01003015 build.pn_offs = build.hdr_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003016 if (gen_iv || iv_spc)
3017 build.hdr_len += IEEE80211_CCMP_HDR_LEN;
3018 break;
3019 case WLAN_CIPHER_SUITE_GCMP:
3020 case WLAN_CIPHER_SUITE_GCMP_256:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003021 if (gen_iv)
Johannes Berg17c18bf2015-03-21 15:25:43 +01003022 build.pn_offs = build.hdr_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003023 if (gen_iv || iv_spc)
3024 build.hdr_len += IEEE80211_GCMP_HDR_LEN;
3025 break;
Johannes Berge495c242015-04-10 14:03:17 +02003026 case WLAN_CIPHER_SUITE_TKIP:
3027 /* cannot handle MMIC or IV generation in xmit-fast */
3028 if (mmic || gen_iv)
3029 goto out;
3030 if (iv_spc)
3031 build.hdr_len += IEEE80211_TKIP_IV_LEN;
3032 break;
3033 case WLAN_CIPHER_SUITE_WEP40:
3034 case WLAN_CIPHER_SUITE_WEP104:
3035 /* cannot handle IV generation in fast-xmit */
3036 if (gen_iv)
3037 goto out;
3038 if (iv_spc)
3039 build.hdr_len += IEEE80211_WEP_IV_LEN;
3040 break;
3041 case WLAN_CIPHER_SUITE_AES_CMAC:
3042 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3043 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3044 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3045 WARN(1,
3046 "management cipher suite 0x%x enabled for data\n",
3047 build.key->conf.cipher);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003048 goto out;
Johannes Berge495c242015-04-10 14:03:17 +02003049 default:
3050 /* we don't know how to generate IVs for this at all */
3051 if (WARN_ON(gen_iv))
3052 goto out;
3053 /* pure hardware keys are OK, of course */
3054 if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
3055 break;
3056 /* cipher scheme might require space allocation */
3057 if (iv_spc &&
3058 build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
3059 goto out;
3060 if (iv_spc)
3061 build.hdr_len += build.key->conf.iv_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003062 }
3063
3064 fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3065 }
3066
3067 hdr->frame_control = fc;
3068
3069 memcpy(build.hdr + build.hdr_len,
3070 rfc1042_header, sizeof(rfc1042_header));
3071 build.hdr_len += sizeof(rfc1042_header);
3072
3073 fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3074 /* if the kmemdup fails, continue w/o fast_tx */
3075 if (!fast_tx)
3076 goto out;
3077
3078 out:
3079 /* we might have raced against another call to this function */
3080 old = rcu_dereference_protected(sta->fast_tx,
3081 lockdep_is_held(&sta->lock));
3082 rcu_assign_pointer(sta->fast_tx, fast_tx);
3083 if (old)
3084 kfree_rcu(old, rcu_head);
3085 spin_unlock_bh(&sta->lock);
3086}
3087
3088void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3089{
3090 struct sta_info *sta;
3091
3092 rcu_read_lock();
3093 list_for_each_entry_rcu(sta, &local->sta_list, list)
3094 ieee80211_check_fast_xmit(sta);
3095 rcu_read_unlock();
3096}
3097
3098void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3099{
3100 struct ieee80211_local *local = sdata->local;
3101 struct sta_info *sta;
3102
3103 rcu_read_lock();
3104
3105 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3106 if (sdata != sta->sdata &&
3107 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3108 continue;
3109 ieee80211_check_fast_xmit(sta);
3110 }
3111
3112 rcu_read_unlock();
3113}
3114
3115void ieee80211_clear_fast_xmit(struct sta_info *sta)
3116{
3117 struct ieee80211_fast_tx *fast_tx;
3118
3119 spin_lock_bh(&sta->lock);
3120 fast_tx = rcu_dereference_protected(sta->fast_tx,
3121 lockdep_is_held(&sta->lock));
3122 RCU_INIT_POINTER(sta->fast_tx, NULL);
3123 spin_unlock_bh(&sta->lock);
3124
3125 if (fast_tx)
3126 kfree_rcu(fast_tx, rcu_head);
3127}
3128
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003129static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
Sara Sharon166ac9d2018-08-29 08:57:02 +02003130 struct sk_buff *skb, int headroom)
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003131{
Sara Sharon166ac9d2018-08-29 08:57:02 +02003132 if (skb_headroom(skb) < headroom) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003133 I802_DEBUG_INC(local->tx_expand_skb_head);
3134
Sara Sharon166ac9d2018-08-29 08:57:02 +02003135 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003136 wiphy_debug(local->hw.wiphy,
3137 "failed to reallocate TX buffer\n");
3138 return false;
3139 }
3140 }
3141
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003142 return true;
3143}
3144
3145static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3146 struct ieee80211_fast_tx *fast_tx,
3147 struct sk_buff *skb)
3148{
3149 struct ieee80211_local *local = sdata->local;
3150 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3151 struct ieee80211_hdr *hdr;
Michael Braun06f2bb12016-10-15 13:28:18 +02003152 struct ethhdr *amsdu_hdr;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003153 int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3154 int subframe_len = skb->len - hdr_len;
3155 void *data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003156 u8 *qc, *h_80211_src, *h_80211_dst;
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003157 const u8 *bssid;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003158
3159 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3160 return false;
3161
3162 if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3163 return true;
3164
Sara Sharon166ac9d2018-08-29 08:57:02 +02003165 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(*amsdu_hdr)))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003166 return false;
3167
Michael Braun06f2bb12016-10-15 13:28:18 +02003168 data = skb_push(skb, sizeof(*amsdu_hdr));
3169 memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003170 hdr = data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003171 amsdu_hdr = data + hdr_len;
3172 /* h_80211_src/dst is addr* field within hdr */
3173 h_80211_src = data + fast_tx->sa_offs;
3174 h_80211_dst = data + fast_tx->da_offs;
3175
3176 amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3177 ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3178 ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
3179
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003180 /* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3181 * fields needs to be changed to BSSID for A-MSDU frames depending
3182 * on FromDS/ToDS values.
3183 */
3184 switch (sdata->vif.type) {
3185 case NL80211_IFTYPE_STATION:
3186 bssid = sdata->u.mgd.bssid;
3187 break;
3188 case NL80211_IFTYPE_AP:
3189 case NL80211_IFTYPE_AP_VLAN:
3190 bssid = sdata->vif.addr;
3191 break;
3192 default:
3193 bssid = NULL;
3194 }
3195
3196 if (bssid && ieee80211_has_fromds(hdr->frame_control))
3197 ether_addr_copy(h_80211_src, bssid);
3198
3199 if (bssid && ieee80211_has_tods(hdr->frame_control))
3200 ether_addr_copy(h_80211_dst, bssid);
3201
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003202 qc = ieee80211_get_qos_ctl(hdr);
3203 *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3204
3205 info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3206
3207 return true;
3208}
3209
3210static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3211 struct sta_info *sta,
3212 struct ieee80211_fast_tx *fast_tx,
3213 struct sk_buff *skb)
3214{
3215 struct ieee80211_local *local = sdata->local;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003216 struct fq *fq = &local->fq;
3217 struct fq_tin *tin;
3218 struct fq_flow *flow;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003219 u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3220 struct ieee80211_txq *txq = sta->sta.txq[tid];
3221 struct txq_info *txqi;
3222 struct sk_buff **frag_tail, *head;
3223 int subframe_len = skb->len - ETH_ALEN;
3224 u8 max_subframes = sta->sta.max_amsdu_subframes;
3225 int max_frags = local->hw.max_tx_fragments;
3226 int max_amsdu_len = sta->sta.max_amsdu_len;
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003227 int orig_truesize;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003228 u32 flow_idx;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003229 __be16 len;
3230 void *data;
3231 bool ret = false;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003232 unsigned int orig_len;
Lorenzo Bianconi66eb02d2018-08-31 01:04:13 +02003233 int n = 2, nfrags, pad = 0;
Sara Sharon166ac9d2018-08-29 08:57:02 +02003234 u16 hdrlen;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003235
3236 if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
3237 return false;
3238
Sara Sharon344f8e02018-12-15 11:03:07 +02003239 if (skb_is_gso(skb))
3240 return false;
3241
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003242 if (!txq)
3243 return false;
3244
3245 txqi = to_txq_info(txq);
3246 if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3247 return false;
3248
3249 if (sta->sta.max_rc_amsdu_len)
3250 max_amsdu_len = min_t(int, max_amsdu_len,
3251 sta->sta.max_rc_amsdu_len);
3252
Sara Sharonedba6bd2018-09-05 08:06:10 +03003253 if (sta->sta.max_tid_amsdu_len[tid])
3254 max_amsdu_len = min_t(int, max_amsdu_len,
3255 sta->sta.max_tid_amsdu_len[tid]);
3256
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003257 flow_idx = fq_flow_idx(fq, skb);
3258
Michal Kaziorfa962b92016-05-19 10:37:49 +02003259 spin_lock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003260
Michal Kaziorfa962b92016-05-19 10:37:49 +02003261 /* TODO: Ideally aggregation should be done on dequeue to remain
3262 * responsive to environment changes.
3263 */
3264
3265 tin = &txqi->tin;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003266 flow = fq_flow_classify(fq, tin, flow_idx, skb,
3267 fq_flow_get_default_func);
Michal Kaziorfa962b92016-05-19 10:37:49 +02003268 head = skb_peek_tail(&flow->queue);
Sara Sharon344f8e02018-12-15 11:03:07 +02003269 if (!head || skb_is_gso(head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003270 goto out;
3271
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003272 orig_truesize = head->truesize;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003273 orig_len = head->len;
3274
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003275 if (skb->len + head->len > max_amsdu_len)
3276 goto out;
3277
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003278 nfrags = 1 + skb_shinfo(skb)->nr_frags;
3279 nfrags += 1 + skb_shinfo(head)->nr_frags;
3280 frag_tail = &skb_shinfo(head)->frag_list;
3281 while (*frag_tail) {
3282 nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3283 frag_tail = &(*frag_tail)->next;
3284 n++;
3285 }
3286
3287 if (max_subframes && n > max_subframes)
3288 goto out;
3289
3290 if (max_frags && nfrags > max_frags)
3291 goto out;
3292
Sara Sharon9739fe22018-09-05 08:06:11 +03003293 if (!drv_can_aggregate_in_amsdu(local, head, skb))
3294 goto out;
3295
Lorenzo Bianconi1eb50792018-08-29 21:03:25 +02003296 if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003297 goto out;
3298
Sara Sharon166ac9d2018-08-29 08:57:02 +02003299 /*
3300 * Pad out the previous subframe to a multiple of 4 by adding the
3301 * padding to the next one, that's being added. Note that head->len
3302 * is the length of the full A-MSDU, but that works since each time
3303 * we add a new subframe we pad out the previous one to a multiple
3304 * of 4 and thus it no longer matters in the next round.
3305 */
3306 hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3307 if ((head->len - hdrlen) & 3)
3308 pad = 4 - ((head->len - hdrlen) & 3);
3309
3310 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3311 2 + pad))
Johannes Bergaa58acf2018-08-30 10:55:49 +02003312 goto out_recalc;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003313
3314 ret = true;
3315 data = skb_push(skb, ETH_ALEN + 2);
3316 memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3317
3318 data += 2 * ETH_ALEN;
3319 len = cpu_to_be16(subframe_len);
3320 memcpy(data, &len, 2);
3321 memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3322
Sara Sharon166ac9d2018-08-29 08:57:02 +02003323 memset(skb_push(skb, pad), 0, pad);
3324
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003325 head->len += skb->len;
3326 head->data_len += skb->len;
3327 *frag_tail = skb;
3328
Johannes Bergaa58acf2018-08-30 10:55:49 +02003329out_recalc:
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003330 fq->memory_usage += head->truesize - orig_truesize;
Johannes Bergaa58acf2018-08-30 10:55:49 +02003331 if (head->len != orig_len) {
3332 flow->backlog += head->len - orig_len;
3333 tin->backlog_bytes += head->len - orig_len;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003334
Johannes Bergaa58acf2018-08-30 10:55:49 +02003335 fq_recalc_backlog(fq, tin, flow);
3336 }
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003337out:
Michal Kaziorfa962b92016-05-19 10:37:49 +02003338 spin_unlock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003339
3340 return ret;
3341}
3342
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003343/*
3344 * Can be called while the sta lock is held. Anything that can cause packets to
3345 * be generated will cause deadlock!
3346 */
3347static void ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3348 struct sta_info *sta, u8 pn_offs,
3349 struct ieee80211_key *key,
3350 struct sk_buff *skb)
3351{
3352 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3353 struct ieee80211_hdr *hdr = (void *)skb->data;
3354 u8 tid = IEEE80211_NUM_TIDS;
3355
3356 if (key)
3357 info->control.hw_key = &key->conf;
3358
3359 ieee80211_tx_stats(skb->dev, skb->len);
3360
3361 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3362 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003363 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3364 } else {
3365 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3366 hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3367 sdata->sequence_number += 0x10;
3368 }
3369
3370 if (skb_shinfo(skb)->gso_size)
3371 sta->tx_stats.msdu[tid] +=
3372 DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3373 else
3374 sta->tx_stats.msdu[tid]++;
3375
3376 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3377
3378 /* statistics normally done by ieee80211_tx_h_stats (but that
3379 * has to consider fragmentation, so is more complex)
3380 */
3381 sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3382 sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
3383
3384 if (pn_offs) {
3385 u64 pn;
3386 u8 *crypto_hdr = skb->data + pn_offs;
3387
3388 switch (key->conf.cipher) {
3389 case WLAN_CIPHER_SUITE_CCMP:
3390 case WLAN_CIPHER_SUITE_CCMP_256:
3391 case WLAN_CIPHER_SUITE_GCMP:
3392 case WLAN_CIPHER_SUITE_GCMP_256:
3393 pn = atomic64_inc_return(&key->conf.tx_pn);
3394 crypto_hdr[0] = pn;
3395 crypto_hdr[1] = pn >> 8;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003396 crypto_hdr[3] = 0x20 | (key->conf.keyidx << 6);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003397 crypto_hdr[4] = pn >> 16;
3398 crypto_hdr[5] = pn >> 24;
3399 crypto_hdr[6] = pn >> 32;
3400 crypto_hdr[7] = pn >> 40;
3401 break;
3402 }
3403 }
3404}
3405
Johannes Berg17c18bf2015-03-21 15:25:43 +01003406static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003407 struct sta_info *sta,
Johannes Berg17c18bf2015-03-21 15:25:43 +01003408 struct ieee80211_fast_tx *fast_tx,
3409 struct sk_buff *skb)
3410{
3411 struct ieee80211_local *local = sdata->local;
3412 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3413 int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3414 int hw_headroom = sdata->local->hw.extra_tx_headroom;
3415 struct ethhdr eth;
Johannes Berg35f432a2017-01-02 11:19:29 +01003416 struct ieee80211_tx_info *info;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003417 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3418 struct ieee80211_tx_data tx;
3419 ieee80211_tx_result r;
3420 struct tid_ampdu_tx *tid_tx = NULL;
3421 u8 tid = IEEE80211_NUM_TIDS;
3422
3423 /* control port protocol needs a lot of special handling */
3424 if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3425 return false;
3426
3427 /* only RFC 1042 SNAP */
3428 if (ethertype < ETH_P_802_3_MIN)
3429 return false;
3430
3431 /* don't handle TX status request here either */
3432 if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3433 return false;
3434
3435 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3436 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3437 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
Johannes Berg472be002015-06-09 16:45:08 +02003438 if (tid_tx) {
3439 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3440 return false;
3441 if (tid_tx->timeout)
3442 tid_tx->last_tx = jiffies;
3443 }
Johannes Berg17c18bf2015-03-21 15:25:43 +01003444 }
3445
3446 /* after this point (skb is modified) we cannot return false */
3447
3448 if (skb_shared(skb)) {
3449 struct sk_buff *tmp_skb = skb;
3450
3451 skb = skb_clone(skb, GFP_ATOMIC);
3452 kfree_skb(tmp_skb);
3453
3454 if (!skb)
3455 return true;
3456 }
3457
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003458 if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3459 ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
3460 return true;
3461
Johannes Berg17c18bf2015-03-21 15:25:43 +01003462 /* will not be crypto-handled beyond what we do here, so use false
3463 * as the may-encrypt argument for the resize to not account for
3464 * more room than we already have in 'extra_head'
3465 */
3466 if (unlikely(ieee80211_skb_resize(sdata, skb,
3467 max_t(int, extra_head + hw_headroom -
3468 skb_headroom(skb), 0),
3469 false))) {
3470 kfree_skb(skb);
3471 return true;
3472 }
3473
3474 memcpy(&eth, skb->data, ETH_HLEN - 2);
Johannes Bergd58ff352017-06-16 14:29:23 +02003475 hdr = skb_push(skb, extra_head);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003476 memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3477 memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
3478 memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
3479
Johannes Berg35f432a2017-01-02 11:19:29 +01003480 info = IEEE80211_SKB_CB(skb);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003481 memset(info, 0, sizeof(*info));
3482 info->band = fast_tx->band;
3483 info->control.vif = &sdata->vif;
3484 info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3485 IEEE80211_TX_CTL_DONTFRAG |
3486 (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003487 info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003488
Julius Niedworok276d9e82019-03-28 21:01:06 +01003489#ifdef CONFIG_MAC80211_DEBUGFS
3490 if (local->force_tx_status)
3491 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3492#endif
3493
Felix Fietkaua786f962016-11-04 10:27:54 +01003494 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3495 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3496 *ieee80211_get_qos_ctl(hdr) = tid;
3497 }
3498
Johannes Berg17c18bf2015-03-21 15:25:43 +01003499 __skb_queue_head_init(&tx.skbs);
3500
3501 tx.flags = IEEE80211_TX_UNICAST;
3502 tx.local = local;
3503 tx.sdata = sdata;
3504 tx.sta = sta;
3505 tx.key = fast_tx->key;
3506
Johannes Berg30686bf2015-06-02 21:39:54 +02003507 if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01003508 tx.skb = skb;
3509 r = ieee80211_tx_h_rate_ctrl(&tx);
3510 skb = tx.skb;
3511 tx.skb = NULL;
3512
3513 if (r != TX_CONTINUE) {
3514 if (r != TX_QUEUED)
3515 kfree_skb(skb);
3516 return true;
3517 }
3518 }
3519
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003520 if (ieee80211_queue_skb(local, sdata, sta, skb))
3521 return true;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003522
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003523 ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3524 fast_tx->key, skb);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003525
3526 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3527 sdata = container_of(sdata->bss,
3528 struct ieee80211_sub_if_data, u.ap);
3529
3530 __skb_queue_tail(&tx.skbs, skb);
3531 ieee80211_tx_frags(local, &sdata->vif, &sta->sta, &tx.skbs, false);
3532 return true;
3533}
3534
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003535struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3536 struct ieee80211_txq *txq)
3537{
3538 struct ieee80211_local *local = hw_to_local(hw);
3539 struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3540 struct ieee80211_hdr *hdr;
3541 struct sk_buff *skb = NULL;
3542 struct fq *fq = &local->fq;
3543 struct fq_tin *tin = &txqi->tin;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003544 struct ieee80211_tx_info *info;
3545 struct ieee80211_tx_data tx;
3546 ieee80211_tx_result r;
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303547 struct ieee80211_vif *vif = txq->vif;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003548
Felix Fietkauded46982019-03-16 18:06:33 +01003549begin:
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003550 spin_lock_bh(&fq->lock);
3551
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303552 if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags) ||
3553 test_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags))
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003554 goto out;
3555
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303556 if (vif->txqs_stopped[ieee80211_ac_from_tid(txq->tid)]) {
3557 set_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags);
3558 goto out;
3559 }
3560
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003561 /* Make sure fragments stay together. */
3562 skb = __skb_dequeue(&txqi->frags);
3563 if (skb)
3564 goto out;
3565
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003566 skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
3567 if (!skb)
3568 goto out;
3569
Felix Fietkauded46982019-03-16 18:06:33 +01003570 spin_unlock_bh(&fq->lock);
3571
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003572 hdr = (struct ieee80211_hdr *)skb->data;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003573 info = IEEE80211_SKB_CB(skb);
3574
3575 memset(&tx, 0, sizeof(tx));
3576 __skb_queue_head_init(&tx.skbs);
3577 tx.local = local;
3578 tx.skb = skb;
3579 tx.sdata = vif_to_sdata(info->control.vif);
3580
3581 if (txq->sta)
3582 tx.sta = container_of(txq->sta, struct sta_info, sta);
3583
3584 /*
3585 * The key can be removed while the packet was queued, so need to call
3586 * this here to get the current key.
3587 */
3588 r = ieee80211_tx_h_select_key(&tx);
3589 if (r != TX_CONTINUE) {
3590 ieee80211_free_txskb(&local->hw, skb);
3591 goto begin;
3592 }
3593
Felix Fietkauc1f4c9e2016-11-04 10:27:52 +01003594 if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
3595 info->flags |= IEEE80211_TX_CTL_AMPDU;
3596 else
3597 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
3598
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003599 if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003600 struct sta_info *sta = container_of(txq->sta, struct sta_info,
3601 sta);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003602 u8 pn_offs = 0;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003603
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003604 if (tx.key &&
3605 (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
3606 pn_offs = ieee80211_hdrlen(hdr->frame_control);
3607
3608 ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
3609 tx.key, skb);
3610 } else {
3611 if (invoke_tx_handlers_late(&tx))
3612 goto begin;
3613
3614 skb = __skb_dequeue(&tx.skbs);
3615
Felix Fietkauded46982019-03-16 18:06:33 +01003616 if (!skb_queue_empty(&tx.skbs)) {
3617 spin_lock_bh(&fq->lock);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003618 skb_queue_splice_tail(&tx.skbs, &txqi->frags);
Felix Fietkauded46982019-03-16 18:06:33 +01003619 spin_unlock_bh(&fq->lock);
3620 }
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003621 }
3622
Johannes Berg233e98d2018-12-15 11:03:09 +02003623 if (skb_has_frag_list(skb) &&
Johannes Berg1e1430d2016-10-04 09:22:19 +02003624 !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
3625 if (skb_linearize(skb)) {
3626 ieee80211_free_txskb(&local->hw, skb);
3627 goto begin;
3628 }
3629 }
3630
Johannes Berg53168212017-06-22 12:20:30 +02003631 switch (tx.sdata->vif.type) {
3632 case NL80211_IFTYPE_MONITOR:
3633 if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
3634 vif = &tx.sdata->vif;
3635 break;
3636 }
3637 tx.sdata = rcu_dereference(local->monitor_sdata);
3638 if (tx.sdata) {
3639 vif = &tx.sdata->vif;
3640 info->hw_queue =
3641 vif->hw_queue[skb_get_queue_mapping(skb)];
3642 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
3643 ieee80211_free_txskb(&local->hw, skb);
3644 goto begin;
3645 } else {
3646 vif = NULL;
3647 }
3648 break;
3649 case NL80211_IFTYPE_AP_VLAN:
3650 tx.sdata = container_of(tx.sdata->bss,
3651 struct ieee80211_sub_if_data, u.ap);
3652 /* fall through */
3653 default:
3654 vif = &tx.sdata->vif;
3655 break;
3656 }
3657
3658 IEEE80211_SKB_CB(skb)->control.vif = vif;
Felix Fietkauded46982019-03-16 18:06:33 +01003659 return skb;
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303660
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003661out:
3662 spin_unlock_bh(&fq->lock);
3663
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003664 return skb;
3665}
3666EXPORT_SYMBOL(ieee80211_tx_dequeue);
3667
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003668struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac)
3669{
3670 struct ieee80211_local *local = hw_to_local(hw);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003671 struct ieee80211_txq *ret = NULL;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003672 struct txq_info *txqi = NULL;
3673
Felix Fietkau5b989c12019-03-15 11:03:35 +01003674 spin_lock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003675
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003676 begin:
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003677 txqi = list_first_entry_or_null(&local->active_txqs[ac],
3678 struct txq_info,
3679 schedule_order);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003680 if (!txqi)
Felix Fietkau5b989c12019-03-15 11:03:35 +01003681 goto out;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003682
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003683 if (txqi->txq.sta) {
3684 struct sta_info *sta = container_of(txqi->txq.sta,
3685 struct sta_info, sta);
3686
3687 if (sta->airtime[txqi->txq.ac].deficit < 0) {
3688 sta->airtime[txqi->txq.ac].deficit +=
3689 sta->airtime_weight;
3690 list_move_tail(&txqi->schedule_order,
3691 &local->active_txqs[txqi->txq.ac]);
3692 goto begin;
3693 }
3694 }
3695
3696
3697 if (txqi->schedule_round == local->schedule_round[ac])
Felix Fietkau5b989c12019-03-15 11:03:35 +01003698 goto out;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003699
3700 list_del_init(&txqi->schedule_order);
3701 txqi->schedule_round = local->schedule_round[ac];
Felix Fietkau5b989c12019-03-15 11:03:35 +01003702 ret = &txqi->txq;
3703
3704out:
3705 spin_unlock_bh(&local->active_txq_lock[ac]);
3706 return ret;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003707}
3708EXPORT_SYMBOL(ieee80211_next_txq);
3709
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003710void __ieee80211_schedule_txq(struct ieee80211_hw *hw,
3711 struct ieee80211_txq *txq,
3712 bool force)
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003713{
3714 struct ieee80211_local *local = hw_to_local(hw);
3715 struct txq_info *txqi = to_txq_info(txq);
3716
Felix Fietkau5b989c12019-03-15 11:03:35 +01003717 spin_lock_bh(&local->active_txq_lock[txq->ac]);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003718
3719 if (list_empty(&txqi->schedule_order) &&
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003720 (force || !skb_queue_empty(&txqi->frags) ||
3721 txqi->tin.backlog_packets)) {
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003722 /* If airtime accounting is active, always enqueue STAs at the
3723 * head of the list to ensure that they only get moved to the
3724 * back by the airtime DRR scheduler once they have a negative
3725 * deficit. A station that already has a negative deficit will
3726 * get immediately moved to the back of the list on the next
3727 * call to ieee80211_next_txq().
3728 */
3729 if (txqi->txq.sta &&
3730 wiphy_ext_feature_isset(local->hw.wiphy,
3731 NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
3732 list_add(&txqi->schedule_order,
3733 &local->active_txqs[txq->ac]);
3734 else
3735 list_add_tail(&txqi->schedule_order,
3736 &local->active_txqs[txq->ac]);
3737 }
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003738
Toke Høiland-Jørgensen390298e2019-01-22 15:20:16 +01003739 spin_unlock_bh(&local->active_txq_lock[txq->ac]);
3740}
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003741EXPORT_SYMBOL(__ieee80211_schedule_txq);
Toke Høiland-Jørgensen390298e2019-01-22 15:20:16 +01003742
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003743bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
3744 struct ieee80211_txq *txq)
3745{
3746 struct ieee80211_local *local = hw_to_local(hw);
3747 struct txq_info *iter, *tmp, *txqi = to_txq_info(txq);
3748 struct sta_info *sta;
3749 u8 ac = txq->ac;
3750
Felix Fietkau5b989c12019-03-15 11:03:35 +01003751 spin_lock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003752
3753 if (!txqi->txq.sta)
3754 goto out;
3755
3756 if (list_empty(&txqi->schedule_order))
3757 goto out;
3758
3759 list_for_each_entry_safe(iter, tmp, &local->active_txqs[ac],
3760 schedule_order) {
3761 if (iter == txqi)
3762 break;
3763
3764 if (!iter->txq.sta) {
3765 list_move_tail(&iter->schedule_order,
3766 &local->active_txqs[ac]);
3767 continue;
3768 }
3769 sta = container_of(iter->txq.sta, struct sta_info, sta);
3770 if (sta->airtime[ac].deficit < 0)
3771 sta->airtime[ac].deficit += sta->airtime_weight;
3772 list_move_tail(&iter->schedule_order, &local->active_txqs[ac]);
3773 }
3774
3775 sta = container_of(txqi->txq.sta, struct sta_info, sta);
3776 if (sta->airtime[ac].deficit >= 0)
3777 goto out;
3778
3779 sta->airtime[ac].deficit += sta->airtime_weight;
3780 list_move_tail(&txqi->schedule_order, &local->active_txqs[ac]);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003781 spin_unlock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003782
3783 return false;
3784out:
3785 if (!list_empty(&txqi->schedule_order))
3786 list_del_init(&txqi->schedule_order);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003787 spin_unlock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003788
3789 return true;
3790}
3791EXPORT_SYMBOL(ieee80211_txq_may_transmit);
3792
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003793void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003794{
3795 struct ieee80211_local *local = hw_to_local(hw);
3796
3797 spin_lock_bh(&local->active_txq_lock[ac]);
3798 local->schedule_round[ac]++;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003799 spin_unlock_bh(&local->active_txq_lock[ac]);
3800}
Felix Fietkau5b989c12019-03-15 11:03:35 +01003801EXPORT_SYMBOL(ieee80211_txq_schedule_start);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003802
Johannes Berg4c9451e2014-11-09 18:50:10 +02003803void __ieee80211_subif_start_xmit(struct sk_buff *skb,
3804 struct net_device *dev,
Rajkumar Manoharan06016772019-04-11 13:47:25 -07003805 u32 info_flags,
3806 u32 ctrl_flags)
Johannes Berg4c9451e2014-11-09 18:50:10 +02003807{
3808 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Felix Fietkau1974da82019-03-25 08:59:23 +01003809 struct ieee80211_local *local = sdata->local;
Johannes Berg97ffe752015-03-21 09:13:45 +01003810 struct sta_info *sta;
Johannes Berg80616c02015-04-14 14:50:41 +02003811 struct sk_buff *next;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003812
3813 if (unlikely(skb->len < ETH_HLEN)) {
3814 kfree_skb(skb);
3815 return;
3816 }
3817
3818 rcu_read_lock();
3819
Johannes Berg2d981fd2015-04-10 14:10:10 +02003820 if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
3821 goto out_free;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003822
Felix Fietkau1974da82019-03-25 08:59:23 +01003823 if (IS_ERR(sta))
3824 sta = NULL;
3825
3826 if (local->ops->wake_tx_queue) {
3827 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
3828 skb_set_queue_mapping(skb, queue);
3829 }
3830
3831 if (sta) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01003832 struct ieee80211_fast_tx *fast_tx;
3833
Wen Gong70e53662018-08-08 18:40:01 +08003834 sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
Toke Høiland-Jørgensen36148c22018-02-02 16:11:05 +01003835
Johannes Berg17c18bf2015-03-21 15:25:43 +01003836 fast_tx = rcu_dereference(sta->fast_tx);
3837
3838 if (fast_tx &&
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003839 ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
Johannes Berg17c18bf2015-03-21 15:25:43 +01003840 goto out;
3841 }
3842
Johannes Berg80616c02015-04-14 14:50:41 +02003843 if (skb_is_gso(skb)) {
3844 struct sk_buff *segs;
Johannes Berg680a0da2015-04-13 16:58:25 +02003845
Johannes Berg80616c02015-04-14 14:50:41 +02003846 segs = skb_gso_segment(skb, 0);
3847 if (IS_ERR(segs)) {
Johannes Berg2d981fd2015-04-10 14:10:10 +02003848 goto out_free;
Johannes Berg80616c02015-04-14 14:50:41 +02003849 } else if (segs) {
3850 consume_skb(skb);
3851 skb = segs;
3852 }
3853 } else {
3854 /* we cannot process non-linear frames on this path */
3855 if (skb_linearize(skb)) {
3856 kfree_skb(skb);
3857 goto out;
3858 }
3859
3860 /* the frame could be fragmented, software-encrypted, and other
3861 * things so we cannot really handle checksum offload with it -
3862 * fix it up in software before we handle anything else.
3863 */
3864 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Johannes Bergf603f1f2015-05-05 15:25:33 +02003865 skb_set_transport_header(skb,
3866 skb_checksum_start_offset(skb));
Johannes Berg80616c02015-04-14 14:50:41 +02003867 if (skb_checksum_help(skb))
3868 goto out_free;
3869 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02003870 }
3871
Johannes Berg80616c02015-04-14 14:50:41 +02003872 next = skb;
3873 while (next) {
3874 skb = next;
3875 next = skb->next;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003876
Johannes Berg80616c02015-04-14 14:50:41 +02003877 skb->prev = NULL;
3878 skb->next = NULL;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003879
Rajkumar Manoharan06016772019-04-11 13:47:25 -07003880 skb = ieee80211_build_hdr(sdata, skb, info_flags,
3881 sta, ctrl_flags);
Johannes Berg80616c02015-04-14 14:50:41 +02003882 if (IS_ERR(skb))
3883 goto out;
3884
Johannes Berg5a490512015-04-22 17:10:38 +02003885 ieee80211_tx_stats(dev, skb->len);
Johannes Berg80616c02015-04-14 14:50:41 +02003886
Johannes Bergb9771d42018-05-28 15:47:41 +02003887 ieee80211_xmit(sdata, sta, skb, 0);
Johannes Berg80616c02015-04-14 14:50:41 +02003888 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02003889 goto out;
3890 out_free:
3891 kfree_skb(skb);
Johannes Berg4c9451e2014-11-09 18:50:10 +02003892 out:
3893 rcu_read_unlock();
3894}
3895
Michael Braunebceec82016-11-22 11:52:18 +01003896static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
3897{
3898 struct ethhdr *eth;
3899 int err;
3900
3901 err = skb_ensure_writable(skb, ETH_HLEN);
3902 if (unlikely(err))
3903 return err;
3904
3905 eth = (void *)skb->data;
3906 ether_addr_copy(eth->h_dest, sta->sta.addr);
3907
3908 return 0;
3909}
3910
3911static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
3912 struct net_device *dev)
3913{
3914 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3915 const struct ethhdr *eth = (void *)skb->data;
3916 const struct vlan_ethhdr *ethvlan = (void *)skb->data;
3917 __be16 ethertype;
3918
3919 if (likely(!is_multicast_ether_addr(eth->h_dest)))
3920 return false;
3921
3922 switch (sdata->vif.type) {
3923 case NL80211_IFTYPE_AP_VLAN:
3924 if (sdata->u.vlan.sta)
3925 return false;
3926 if (sdata->wdev.use_4addr)
3927 return false;
3928 /* fall through */
3929 case NL80211_IFTYPE_AP:
3930 /* check runtime toggle for this bss */
3931 if (!sdata->bss->multicast_to_unicast)
3932 return false;
3933 break;
3934 default:
3935 return false;
3936 }
3937
3938 /* multicast to unicast conversion only for some payload */
3939 ethertype = eth->h_proto;
3940 if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
3941 ethertype = ethvlan->h_vlan_encapsulated_proto;
3942 switch (ethertype) {
3943 case htons(ETH_P_ARP):
3944 case htons(ETH_P_IP):
3945 case htons(ETH_P_IPV6):
3946 break;
3947 default:
3948 return false;
3949 }
3950
3951 return true;
3952}
3953
3954static void
3955ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
3956 struct sk_buff_head *queue)
3957{
3958 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3959 struct ieee80211_local *local = sdata->local;
3960 const struct ethhdr *eth = (struct ethhdr *)skb->data;
3961 struct sta_info *sta, *first = NULL;
3962 struct sk_buff *cloned_skb;
3963
3964 rcu_read_lock();
3965
3966 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3967 if (sdata != sta->sdata)
3968 /* AP-VLAN mismatch */
3969 continue;
3970 if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
3971 /* do not send back to source */
3972 continue;
3973 if (!first) {
3974 first = sta;
3975 continue;
3976 }
3977 cloned_skb = skb_clone(skb, GFP_ATOMIC);
3978 if (!cloned_skb)
3979 goto multicast;
3980 if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
3981 dev_kfree_skb(cloned_skb);
3982 goto multicast;
3983 }
3984 __skb_queue_tail(queue, cloned_skb);
3985 }
3986
3987 if (likely(first)) {
3988 if (unlikely(ieee80211_change_da(skb, first)))
3989 goto multicast;
3990 __skb_queue_tail(queue, skb);
3991 } else {
3992 /* no STA connected, drop */
3993 kfree_skb(skb);
3994 skb = NULL;
3995 }
3996
3997 goto out;
3998multicast:
3999 __skb_queue_purge(queue);
4000 __skb_queue_tail(queue, skb);
4001out:
4002 rcu_read_unlock();
4003}
4004
Johannes Berg4c9451e2014-11-09 18:50:10 +02004005/**
4006 * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
4007 * @skb: packet to be sent
4008 * @dev: incoming interface
4009 *
4010 * On failure skb will be freed.
4011 */
Liad Kaufman24d342c2014-11-09 18:50:07 +02004012netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
4013 struct net_device *dev)
4014{
Michael Braunebceec82016-11-22 11:52:18 +01004015 if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
4016 struct sk_buff_head queue;
4017
4018 __skb_queue_head_init(&queue);
4019 ieee80211_convert_to_unicast(skb, dev, &queue);
4020 while ((skb = __skb_dequeue(&queue)))
Rajkumar Manoharan06016772019-04-11 13:47:25 -07004021 __ieee80211_subif_start_xmit(skb, dev, 0, 0);
Michael Braunebceec82016-11-22 11:52:18 +01004022 } else {
Rajkumar Manoharan06016772019-04-11 13:47:25 -07004023 __ieee80211_subif_start_xmit(skb, dev, 0, 0);
Michael Braunebceec82016-11-22 11:52:18 +01004024 }
4025
Liad Kaufman24d342c2014-11-09 18:50:07 +02004026 return NETDEV_TX_OK;
4027}
Johannes Berge2ebc742007-07-27 15:43:22 +02004028
Johannes Berg7528ec52014-11-09 18:50:11 +02004029struct sk_buff *
4030ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
4031 struct sk_buff *skb, u32 info_flags)
4032{
4033 struct ieee80211_hdr *hdr;
4034 struct ieee80211_tx_data tx = {
4035 .local = sdata->local,
4036 .sdata = sdata,
4037 };
Johannes Berg97ffe752015-03-21 09:13:45 +01004038 struct sta_info *sta;
Johannes Berg7528ec52014-11-09 18:50:11 +02004039
4040 rcu_read_lock();
4041
Johannes Berg97ffe752015-03-21 09:13:45 +01004042 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4043 kfree_skb(skb);
4044 skb = ERR_PTR(-EINVAL);
4045 goto out;
4046 }
4047
Rajkumar Manoharan06016772019-04-11 13:47:25 -07004048 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta, 0);
Johannes Berg7528ec52014-11-09 18:50:11 +02004049 if (IS_ERR(skb))
4050 goto out;
4051
4052 hdr = (void *)skb->data;
4053 tx.sta = sta_info_get(sdata, hdr->addr1);
4054 tx.skb = skb;
4055
4056 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
4057 rcu_read_unlock();
4058 kfree_skb(skb);
4059 return ERR_PTR(-EINVAL);
4060 }
4061
4062out:
4063 rcu_read_unlock();
4064 return skb;
4065}
4066
Johannes Berge2530082008-05-17 00:57:14 +02004067/*
4068 * ieee80211_clear_tx_pending may not be called in a context where
4069 * it is possible that it packets could come in again.
4070 */
Johannes Berge2ebc742007-07-27 15:43:22 +02004071void ieee80211_clear_tx_pending(struct ieee80211_local *local)
4072{
Felix Fietkau1f98ab72012-11-10 03:44:14 +01004073 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01004074 int i;
Johannes Berge2ebc742007-07-27 15:43:22 +02004075
Felix Fietkau1f98ab72012-11-10 03:44:14 +01004076 for (i = 0; i < local->hw.queues; i++) {
4077 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
4078 ieee80211_free_txskb(&local->hw, skb);
4079 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004080}
4081
Johannes Berg7bb45682011-02-24 14:42:06 +01004082/*
4083 * Returns false if the frame couldn't be transmitted but was queued instead,
4084 * which in this case means re-queued -- take as an indication to stop sending
4085 * more pending frames.
4086 */
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004087static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
4088 struct sk_buff *skb)
4089{
4090 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4091 struct ieee80211_sub_if_data *sdata;
4092 struct sta_info *sta;
4093 struct ieee80211_hdr *hdr;
Johannes Berg7bb45682011-02-24 14:42:06 +01004094 bool result;
Johannes Berg55de9082012-07-26 17:24:39 +02004095 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004096
Johannes Berg5061b0c2009-07-14 00:33:34 +02004097 sdata = vif_to_sdata(info->control.vif);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004098
4099 if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
Johannes Berg55de9082012-07-26 17:24:39 +02004100 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4101 if (unlikely(!chanctx_conf)) {
4102 dev_kfree_skb(skb);
4103 return true;
4104 }
Johannes Berg73c4e192014-11-09 18:50:09 +02004105 info->band = chanctx_conf->def.chan->band;
Johannes Bergb9771d42018-05-28 15:47:41 +02004106 result = ieee80211_tx(sdata, NULL, skb, true, 0);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004107 } else {
Johannes Berg252b86c2011-11-16 15:28:55 +01004108 struct sk_buff_head skbs;
4109
4110 __skb_queue_head_init(&skbs);
4111 __skb_queue_tail(&skbs, skb);
4112
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004113 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Bergabe60632009-11-25 17:46:18 +01004114 sta = sta_info_get(sdata, hdr->addr1);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004115
Johannes Berg74e4dbf2011-11-16 15:28:57 +01004116 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004117 }
4118
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004119 return result;
4120}
4121
Johannes Berge2530082008-05-17 00:57:14 +02004122/*
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004123 * Transmit all pending packets. Called from tasklet.
Johannes Berge2530082008-05-17 00:57:14 +02004124 */
Johannes Berge2ebc742007-07-27 15:43:22 +02004125void ieee80211_tx_pending(unsigned long data)
4126{
4127 struct ieee80211_local *local = (struct ieee80211_local *)data;
Johannes Berg2a577d92009-03-23 17:28:37 +01004128 unsigned long flags;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004129 int i;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004130 bool txok;
Johannes Berge2ebc742007-07-27 15:43:22 +02004131
Johannes Bergf0e72852009-03-23 17:28:36 +01004132 rcu_read_lock();
Johannes Berg2a577d92009-03-23 17:28:37 +01004133
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004134 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg176be722009-03-12 23:49:28 +01004135 for (i = 0; i < local->hw.queues; i++) {
Johannes Berg2a577d92009-03-23 17:28:37 +01004136 /*
4137 * If queue is stopped by something other than due to pending
4138 * frames, or we have no pending frames, proceed to next queue.
4139 */
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004140 if (local->queue_stop_reasons[i] ||
Johannes Berg2a577d92009-03-23 17:28:37 +01004141 skb_queue_empty(&local->pending[i]))
Johannes Berge2ebc742007-07-27 15:43:22 +02004142 continue;
Johannes Berge2530082008-05-17 00:57:14 +02004143
Johannes Berg2a577d92009-03-23 17:28:37 +01004144 while (!skb_queue_empty(&local->pending[i])) {
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004145 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
Johannes Berg5061b0c2009-07-14 00:33:34 +02004146 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg5061b0c2009-07-14 00:33:34 +02004147
Johannes Berga7bc3762009-07-27 10:33:31 +02004148 if (WARN_ON(!info->control.vif)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02004149 ieee80211_free_txskb(&local->hw, skb);
Johannes Berga7bc3762009-07-27 10:33:31 +02004150 continue;
4151 }
4152
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004153 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
4154 flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01004155
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004156 txok = ieee80211_tx_pending_skb(local, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004157 spin_lock_irqsave(&local->queue_stop_reason_lock,
4158 flags);
4159 if (!txok)
Johannes Berg2a577d92009-03-23 17:28:37 +01004160 break;
Johannes Berge2ebc742007-07-27 15:43:22 +02004161 }
Johannes Berg7236fe22010-03-22 13:42:43 -07004162
4163 if (skb_queue_empty(&local->pending[i]))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02004164 ieee80211_propagate_queue_wake(local, i);
Johannes Berge2ebc742007-07-27 15:43:22 +02004165 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004166 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01004167
Johannes Bergf0e72852009-03-23 17:28:36 +01004168 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004169}
4170
4171/* functions for drivers to get certain frames */
4172
Marco Porscheac70c12013-01-07 16:04:50 +01004173static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004174 struct ps_data *ps, struct sk_buff *skb,
4175 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02004176{
4177 u8 *pos, *tim;
4178 int aid0 = 0;
4179 int i, have_bits = 0, n1, n2;
4180
4181 /* Generate bitmap for TIM only if there are any STAs in power save
4182 * mode. */
Marco Porschd012a602012-10-10 12:39:50 -07004183 if (atomic_read(&ps->num_sta_ps) > 0)
Johannes Berge2ebc742007-07-27 15:43:22 +02004184 /* in the hope that this is faster than
4185 * checking byte-for-byte */
Weilong Chenf359d3f2013-12-18 15:44:16 +08004186 have_bits = !bitmap_empty((unsigned long *)ps->tim,
Johannes Berge2ebc742007-07-27 15:43:22 +02004187 IEEE80211_MAX_AID+1);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004188 if (!is_template) {
4189 if (ps->dtim_count == 0)
4190 ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
4191 else
4192 ps->dtim_count--;
4193 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004194
Johannes Berg4df864c2017-06-16 14:29:21 +02004195 tim = pos = skb_put(skb, 6);
Johannes Berge2ebc742007-07-27 15:43:22 +02004196 *pos++ = WLAN_EID_TIM;
4197 *pos++ = 4;
Marco Porschd012a602012-10-10 12:39:50 -07004198 *pos++ = ps->dtim_count;
Johannes Berg88600202012-02-13 15:17:18 +01004199 *pos++ = sdata->vif.bss_conf.dtim_period;
Johannes Berge2ebc742007-07-27 15:43:22 +02004200
Marco Porschd012a602012-10-10 12:39:50 -07004201 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
Johannes Berge2ebc742007-07-27 15:43:22 +02004202 aid0 = 1;
4203
Marco Porschd012a602012-10-10 12:39:50 -07004204 ps->dtim_bc_mc = aid0 == 1;
Christian Lamparter512119b2011-01-31 20:48:44 +02004205
Johannes Berge2ebc742007-07-27 15:43:22 +02004206 if (have_bits) {
4207 /* Find largest even number N1 so that bits numbered 1 through
4208 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
4209 * (N2 + 1) x 8 through 2007 are 0. */
4210 n1 = 0;
4211 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
Marco Porschd012a602012-10-10 12:39:50 -07004212 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004213 n1 = i & 0xfe;
4214 break;
4215 }
4216 }
4217 n2 = n1;
4218 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
Marco Porschd012a602012-10-10 12:39:50 -07004219 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004220 n2 = i;
4221 break;
4222 }
4223 }
4224
4225 /* Bitmap control */
4226 *pos++ = n1 | aid0;
4227 /* Part Virt Bitmap */
Eliad Peller5220da32011-11-24 16:50:00 +02004228 skb_put(skb, n2 - n1);
Marco Porschd012a602012-10-10 12:39:50 -07004229 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
Johannes Berge2ebc742007-07-27 15:43:22 +02004230
4231 tim[1] = n2 - n1 + 4;
Johannes Berge2ebc742007-07-27 15:43:22 +02004232 } else {
4233 *pos++ = aid0; /* Bitmap control */
4234 *pos++ = 0; /* Part Virt Bitmap */
4235 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004236}
4237
Marco Porscheac70c12013-01-07 16:04:50 +01004238static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004239 struct ps_data *ps, struct sk_buff *skb,
4240 bool is_template)
Marco Porscheac70c12013-01-07 16:04:50 +01004241{
4242 struct ieee80211_local *local = sdata->local;
4243
4244 /*
4245 * Not very nice, but we want to allow the driver to call
4246 * ieee80211_beacon_get() as a response to the set_tim()
4247 * callback. That, however, is already invoked under the
4248 * sta_lock to guarantee consistent and race-free update
4249 * of the tim bitmap in mac80211 and the driver.
4250 */
4251 if (local->tim_in_locked_section) {
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004252 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Marco Porscheac70c12013-01-07 16:04:50 +01004253 } else {
Johannes Berg1b917312013-02-22 12:55:01 +01004254 spin_lock_bh(&local->tim_lock);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004255 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Johannes Berg1b917312013-02-22 12:55:01 +01004256 spin_unlock_bh(&local->tim_lock);
Marco Porscheac70c12013-01-07 16:04:50 +01004257 }
4258
4259 return 0;
4260}
4261
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004262static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
4263 struct beacon_data *beacon)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004264{
4265 struct probe_resp *resp;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004266 u8 *beacon_data;
4267 size_t beacon_data_len;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004268 int i;
Michal Kazioraf296bd2014-06-05 14:21:36 +02004269 u8 count = beacon->csa_current_counter;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004270
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004271 switch (sdata->vif.type) {
4272 case NL80211_IFTYPE_AP:
4273 beacon_data = beacon->tail;
4274 beacon_data_len = beacon->tail_len;
4275 break;
4276 case NL80211_IFTYPE_ADHOC:
4277 beacon_data = beacon->head;
4278 beacon_data_len = beacon->head_len;
4279 break;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004280 case NL80211_IFTYPE_MESH_POINT:
4281 beacon_data = beacon->head;
4282 beacon_data_len = beacon->head_len;
4283 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004284 default:
4285 return;
4286 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004287
Michal Kazioraf296bd2014-06-05 14:21:36 +02004288 rcu_read_lock();
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004289 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004290 resp = rcu_dereference(sdata->u.ap.probe_resp);
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004291
Michal Kazioraf296bd2014-06-05 14:21:36 +02004292 if (beacon->csa_counter_offsets[i]) {
4293 if (WARN_ON_ONCE(beacon->csa_counter_offsets[i] >=
4294 beacon_data_len)) {
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004295 rcu_read_unlock();
4296 return;
4297 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004298
4299 beacon_data[beacon->csa_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004300 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004301
4302 if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
4303 resp->data[resp->csa_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004304 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004305 rcu_read_unlock();
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004306}
4307
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004308static u8 __ieee80211_csa_update_counter(struct beacon_data *beacon)
4309{
4310 beacon->csa_current_counter--;
4311
4312 /* the counter should never reach 0 */
4313 WARN_ON_ONCE(!beacon->csa_current_counter);
4314
4315 return beacon->csa_current_counter;
4316}
4317
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004318u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
4319{
4320 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004321 struct beacon_data *beacon = NULL;
4322 u8 count = 0;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004323
Michal Kazioraf296bd2014-06-05 14:21:36 +02004324 rcu_read_lock();
4325
4326 if (sdata->vif.type == NL80211_IFTYPE_AP)
4327 beacon = rcu_dereference(sdata->u.ap.beacon);
4328 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4329 beacon = rcu_dereference(sdata->u.ibss.presp);
4330 else if (ieee80211_vif_is_mesh(&sdata->vif))
4331 beacon = rcu_dereference(sdata->u.mesh.beacon);
4332
4333 if (!beacon)
4334 goto unlock;
4335
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004336 count = __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004337
Michal Kazioraf296bd2014-06-05 14:21:36 +02004338unlock:
4339 rcu_read_unlock();
4340 return count;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004341}
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004342EXPORT_SYMBOL(ieee80211_csa_update_counter);
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004343
Gregory Greenman03737002018-04-20 13:49:24 +03004344void ieee80211_csa_set_counter(struct ieee80211_vif *vif, u8 counter)
4345{
4346 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4347 struct beacon_data *beacon = NULL;
4348
4349 rcu_read_lock();
4350
4351 if (sdata->vif.type == NL80211_IFTYPE_AP)
4352 beacon = rcu_dereference(sdata->u.ap.beacon);
4353 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4354 beacon = rcu_dereference(sdata->u.ibss.presp);
4355 else if (ieee80211_vif_is_mesh(&sdata->vif))
4356 beacon = rcu_dereference(sdata->u.mesh.beacon);
4357
4358 if (!beacon)
4359 goto unlock;
4360
4361 if (counter < beacon->csa_current_counter)
4362 beacon->csa_current_counter = counter;
4363
4364unlock:
4365 rcu_read_unlock();
4366}
4367EXPORT_SYMBOL(ieee80211_csa_set_counter);
4368
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004369bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
4370{
4371 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4372 struct beacon_data *beacon = NULL;
4373 u8 *beacon_data;
4374 size_t beacon_data_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004375 int ret = false;
4376
4377 if (!ieee80211_sdata_running(sdata))
4378 return false;
4379
4380 rcu_read_lock();
4381 if (vif->type == NL80211_IFTYPE_AP) {
4382 struct ieee80211_if_ap *ap = &sdata->u.ap;
4383
4384 beacon = rcu_dereference(ap->beacon);
4385 if (WARN_ON(!beacon || !beacon->tail))
4386 goto out;
4387 beacon_data = beacon->tail;
4388 beacon_data_len = beacon->tail_len;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004389 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
4390 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4391
4392 beacon = rcu_dereference(ifibss->presp);
4393 if (!beacon)
4394 goto out;
4395
4396 beacon_data = beacon->head;
4397 beacon_data_len = beacon->head_len;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004398 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
4399 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4400
4401 beacon = rcu_dereference(ifmsh->beacon);
4402 if (!beacon)
4403 goto out;
4404
4405 beacon_data = beacon->head;
4406 beacon_data_len = beacon->head_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004407 } else {
4408 WARN_ON(1);
4409 goto out;
4410 }
4411
Michal Kazior10d78f22014-06-05 14:21:37 +02004412 if (!beacon->csa_counter_offsets[0])
4413 goto out;
4414
Michal Kazioraf296bd2014-06-05 14:21:36 +02004415 if (WARN_ON_ONCE(beacon->csa_counter_offsets[0] > beacon_data_len))
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004416 goto out;
4417
Michal Kazioraf296bd2014-06-05 14:21:36 +02004418 if (beacon_data[beacon->csa_counter_offsets[0]] == 1)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004419 ret = true;
4420 out:
4421 rcu_read_unlock();
4422
4423 return ret;
4424}
4425EXPORT_SYMBOL(ieee80211_csa_is_complete);
4426
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004427static struct sk_buff *
4428__ieee80211_beacon_get(struct ieee80211_hw *hw,
4429 struct ieee80211_vif *vif,
4430 struct ieee80211_mutable_offsets *offs,
4431 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02004432{
4433 struct ieee80211_local *local = hw_to_local(hw);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004434 struct beacon_data *beacon = NULL;
Johannes Berg9d139c82008-07-09 14:40:37 +02004435 struct sk_buff *skb = NULL;
Johannes Berge039fa42008-05-15 12:55:29 +02004436 struct ieee80211_tx_info *info;
Johannes Berge2ebc742007-07-27 15:43:22 +02004437 struct ieee80211_sub_if_data *sdata = NULL;
Johannes Berg57fbcce2016-04-12 15:56:15 +02004438 enum nl80211_band band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004439 struct ieee80211_tx_rate_control txrc;
Johannes Berg55de9082012-07-26 17:24:39 +02004440 struct ieee80211_chanctx_conf *chanctx_conf;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004441 int csa_off_base = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004442
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004443 rcu_read_lock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004444
Johannes Berg32bfd352007-12-19 01:31:26 +01004445 sdata = vif_to_sdata(vif);
Johannes Berg55de9082012-07-26 17:24:39 +02004446 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berge2ebc742007-07-27 15:43:22 +02004447
Johannes Berg55de9082012-07-26 17:24:39 +02004448 if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
Felix Fietkaueb3e5542011-01-24 19:28:49 +01004449 goto out;
4450
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004451 if (offs)
4452 memset(offs, 0, sizeof(*offs));
Johannes Bergeddcbb942009-10-29 08:30:35 +01004453
Johannes Berg05c914f2008-09-11 00:01:58 +02004454 if (sdata->vif.type == NL80211_IFTYPE_AP) {
Marco Porschd012a602012-10-10 12:39:50 -07004455 struct ieee80211_if_ap *ap = &sdata->u.ap;
Marco Porschd012a602012-10-10 12:39:50 -07004456
Michal Kazioraf296bd2014-06-05 14:21:36 +02004457 beacon = rcu_dereference(ap->beacon);
Dan Carpenter3ad97fb2011-02-07 22:03:35 +03004458 if (beacon) {
Michal Kazior10d78f22014-06-05 14:21:37 +02004459 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004460 if (!is_template)
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004461 __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004462
4463 ieee80211_set_csa(sdata, beacon);
4464 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004465
Johannes Berg902acc72008-02-23 15:17:19 +01004466 /*
4467 * headroom, head length,
4468 * tail length and maximum TIM length
4469 */
4470 skb = dev_alloc_skb(local->tx_headroom +
4471 beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004472 beacon->tail_len + 256 +
4473 local->hw.extra_beacon_tailroom);
Johannes Berg902acc72008-02-23 15:17:19 +01004474 if (!skb)
4475 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004476
Johannes Berg902acc72008-02-23 15:17:19 +01004477 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004478 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg902acc72008-02-23 15:17:19 +01004479
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004480 ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
4481 is_template);
Johannes Berg902acc72008-02-23 15:17:19 +01004482
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004483 if (offs) {
4484 offs->tim_offset = beacon->head_len;
4485 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004486
4487 /* for AP the csa offsets are from tail */
4488 csa_off_base = skb->len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004489 }
Johannes Bergeddcbb942009-10-29 08:30:35 +01004490
Johannes Berg902acc72008-02-23 15:17:19 +01004491 if (beacon->tail)
Johannes Berg59ae1d12017-06-16 14:29:20 +02004492 skb_put_data(skb, beacon->tail,
4493 beacon->tail_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004494 } else
4495 goto out;
Johannes Berg05c914f2008-09-11 00:01:58 +02004496 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Berg46900292009-02-15 12:44:28 +01004497 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg9d139c82008-07-09 14:40:37 +02004498 struct ieee80211_hdr *hdr;
Johannes Berg902acc72008-02-23 15:17:19 +01004499
Michal Kazioraf296bd2014-06-05 14:21:36 +02004500 beacon = rcu_dereference(ifibss->presp);
4501 if (!beacon)
Johannes Berg9d139c82008-07-09 14:40:37 +02004502 goto out;
4503
Michal Kazior10d78f22014-06-05 14:21:37 +02004504 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004505 if (!is_template)
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004506 __ieee80211_csa_update_counter(beacon);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004507
Michal Kazioraf296bd2014-06-05 14:21:36 +02004508 ieee80211_set_csa(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004509 }
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004510
Michal Kazioraf296bd2014-06-05 14:21:36 +02004511 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004512 local->hw.extra_beacon_tailroom);
Johannes Berg9d139c82008-07-09 14:40:37 +02004513 if (!skb)
4514 goto out;
Johannes Bergc3ffeab2013-03-07 20:54:29 +01004515 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004516 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004517
4518 hdr = (struct ieee80211_hdr *) skb->data;
Harvey Harrisone7827a72008-07-15 18:44:13 -07004519 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4520 IEEE80211_STYPE_BEACON);
Johannes Berg902acc72008-02-23 15:17:19 +01004521 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
Javier Cardonadbf498f2012-03-31 11:31:32 -07004522 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg472dbc42008-09-11 00:01:49 +02004523
Michal Kazioraf296bd2014-06-05 14:21:36 +02004524 beacon = rcu_dereference(ifmsh->beacon);
4525 if (!beacon)
Johannes Bergac1bd842011-01-18 13:45:32 +01004526 goto out;
Johannes Bergac1bd842011-01-18 13:45:32 +01004527
Michal Kazior10d78f22014-06-05 14:21:37 +02004528 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004529 if (!is_template)
4530 /* TODO: For mesh csa_counter is in TU, so
4531 * decrementing it by one isn't correct, but
4532 * for now we leave it consistent with overall
4533 * mac80211's behavior.
4534 */
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004535 __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004536
Michal Kazioraf296bd2014-06-05 14:21:36 +02004537 ieee80211_set_csa(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004538 }
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004539
Javier Cardonadbf498f2012-03-31 11:31:32 -07004540 if (ifmsh->sync_ops)
Masashi Honma445cd452016-12-08 10:15:51 +09004541 ifmsh->sync_ops->adjust_tsf(sdata, beacon);
Javier Cardonadbf498f2012-03-31 11:31:32 -07004542
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -07004543 skb = dev_alloc_skb(local->tx_headroom +
Michal Kazioraf296bd2014-06-05 14:21:36 +02004544 beacon->head_len +
Marco Porsch3f52b7e2013-01-30 18:14:08 +01004545 256 + /* TIM IE */
Michal Kazioraf296bd2014-06-05 14:21:36 +02004546 beacon->tail_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004547 local->hw.extra_beacon_tailroom);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01004548 if (!skb)
4549 goto out;
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08004550 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004551 skb_put_data(skb, beacon->head, beacon->head_len);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004552 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
4553
4554 if (offs) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004555 offs->tim_offset = beacon->head_len;
4556 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004557 }
4558
Johannes Berg59ae1d12017-06-16 14:29:20 +02004559 skb_put_data(skb, beacon->tail, beacon->tail_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004560 } else {
4561 WARN_ON(1);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004562 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02004563 }
4564
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004565 /* CSA offsets */
Michal Kazioraf296bd2014-06-05 14:21:36 +02004566 if (offs && beacon) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004567 int i;
4568
4569 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004570 u16 csa_off = beacon->csa_counter_offsets[i];
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004571
4572 if (!csa_off)
4573 continue;
4574
4575 offs->csa_counter_offs[i] = csa_off_base + csa_off;
4576 }
4577 }
4578
Johannes Berg4bf88532012-11-09 11:39:59 +01004579 band = chanctx_conf->def.chan->band;
Johannes Berg55de9082012-07-26 17:24:39 +02004580
Johannes Berge039fa42008-05-15 12:55:29 +02004581 info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02004582
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004583 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004584 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Johannes Berge039fa42008-05-15 12:55:29 +02004585 info->band = band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004586
4587 memset(&txrc, 0, sizeof(txrc));
4588 txrc.hw = hw;
Johannes Berge31583c2012-07-26 14:07:46 +02004589 txrc.sband = local->hw.wiphy->bands[band];
Jouni Malinene00cfce2009-12-29 12:59:19 +02004590 txrc.bss_conf = &sdata->vif.bss_conf;
4591 txrc.skb = skb;
4592 txrc.reported_rate.idx = -1;
Jouni Malinen37eb0b12010-01-06 13:09:08 +02004593 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
Felix Fietkau8f0729b2010-11-11 15:07:23 +01004594 txrc.bss = true;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004595 rate_control_get_rate(sdata, NULL, &txrc);
Johannes Berge039fa42008-05-15 12:55:29 +02004596
4597 info->control.vif = vif;
Johannes Bergf591fa52008-07-10 11:21:26 +02004598
John W. Linvillea472e712010-05-06 14:45:17 -04004599 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
4600 IEEE80211_TX_CTL_ASSIGN_SEQ |
4601 IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge6a98542008-10-21 12:40:02 +02004602 out:
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004603 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004604 return skb;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004605
4606}
4607
4608struct sk_buff *
4609ieee80211_beacon_get_template(struct ieee80211_hw *hw,
4610 struct ieee80211_vif *vif,
4611 struct ieee80211_mutable_offsets *offs)
4612{
4613 return __ieee80211_beacon_get(hw, vif, offs, true);
4614}
4615EXPORT_SYMBOL(ieee80211_beacon_get_template);
4616
4617struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
4618 struct ieee80211_vif *vif,
4619 u16 *tim_offset, u16 *tim_length)
4620{
4621 struct ieee80211_mutable_offsets offs = {};
4622 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
Helmut Schaa35afa582015-09-09 09:46:32 +02004623 struct sk_buff *copy;
4624 struct ieee80211_supported_band *sband;
4625 int shift;
4626
4627 if (!bcn)
4628 return bcn;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004629
4630 if (tim_offset)
4631 *tim_offset = offs.tim_offset;
4632
4633 if (tim_length)
4634 *tim_length = offs.tim_length;
4635
Helmut Schaa35afa582015-09-09 09:46:32 +02004636 if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
4637 !hw_to_local(hw)->monitors)
4638 return bcn;
4639
4640 /* send a copy to monitor interfaces */
4641 copy = skb_copy(bcn, GFP_ATOMIC);
4642 if (!copy)
4643 return bcn;
4644
4645 shift = ieee80211_vif_get_shift(vif);
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05304646 sband = ieee80211_get_sband(vif_to_sdata(vif));
4647 if (!sband)
4648 return bcn;
4649
Helmut Schaa35afa582015-09-09 09:46:32 +02004650 ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false);
4651
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004652 return bcn;
Johannes Berge2ebc742007-07-27 15:43:22 +02004653}
Johannes Bergeddcbb942009-10-29 08:30:35 +01004654EXPORT_SYMBOL(ieee80211_beacon_get_tim);
Johannes Berge2ebc742007-07-27 15:43:22 +02004655
Arik Nemtsov02945822011-11-10 11:28:57 +02004656struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
4657 struct ieee80211_vif *vif)
4658{
4659 struct ieee80211_if_ap *ap = NULL;
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004660 struct sk_buff *skb = NULL;
4661 struct probe_resp *presp = NULL;
Arik Nemtsov02945822011-11-10 11:28:57 +02004662 struct ieee80211_hdr *hdr;
4663 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4664
4665 if (sdata->vif.type != NL80211_IFTYPE_AP)
4666 return NULL;
4667
4668 rcu_read_lock();
4669
4670 ap = &sdata->u.ap;
4671 presp = rcu_dereference(ap->probe_resp);
4672 if (!presp)
4673 goto out;
4674
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004675 skb = dev_alloc_skb(presp->len);
Arik Nemtsov02945822011-11-10 11:28:57 +02004676 if (!skb)
4677 goto out;
4678
Johannes Berg59ae1d12017-06-16 14:29:20 +02004679 skb_put_data(skb, presp->data, presp->len);
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004680
Arik Nemtsov02945822011-11-10 11:28:57 +02004681 hdr = (struct ieee80211_hdr *) skb->data;
4682 memset(hdr->addr1, 0, sizeof(hdr->addr1));
4683
4684out:
4685 rcu_read_unlock();
4686 return skb;
4687}
4688EXPORT_SYMBOL(ieee80211_proberesp_get);
4689
Kalle Valo7044cc52010-01-05 20:16:19 +02004690struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
4691 struct ieee80211_vif *vif)
4692{
4693 struct ieee80211_sub_if_data *sdata;
4694 struct ieee80211_if_managed *ifmgd;
4695 struct ieee80211_pspoll *pspoll;
4696 struct ieee80211_local *local;
4697 struct sk_buff *skb;
4698
4699 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4700 return NULL;
4701
4702 sdata = vif_to_sdata(vif);
4703 ifmgd = &sdata->u.mgd;
4704 local = sdata->local;
4705
4706 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
Joe Perchesd15b8452011-08-29 14:17:31 -07004707 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02004708 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07004709
Kalle Valo7044cc52010-01-05 20:16:19 +02004710 skb_reserve(skb, local->hw.extra_tx_headroom);
4711
Johannes Bergb080db52017-06-16 14:29:19 +02004712 pspoll = skb_put_zero(skb, sizeof(*pspoll));
Kalle Valo7044cc52010-01-05 20:16:19 +02004713 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
4714 IEEE80211_STYPE_PSPOLL);
4715 pspoll->aid = cpu_to_le16(ifmgd->aid);
4716
4717 /* aid in PS-Poll has its two MSBs each set to 1 */
4718 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
4719
4720 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
4721 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
4722
4723 return skb;
4724}
4725EXPORT_SYMBOL(ieee80211_pspoll_get);
4726
4727struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004728 struct ieee80211_vif *vif,
4729 bool qos_ok)
Kalle Valo7044cc52010-01-05 20:16:19 +02004730{
4731 struct ieee80211_hdr_3addr *nullfunc;
4732 struct ieee80211_sub_if_data *sdata;
4733 struct ieee80211_if_managed *ifmgd;
4734 struct ieee80211_local *local;
4735 struct sk_buff *skb;
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004736 bool qos = false;
Kalle Valo7044cc52010-01-05 20:16:19 +02004737
4738 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4739 return NULL;
4740
4741 sdata = vif_to_sdata(vif);
4742 ifmgd = &sdata->u.mgd;
4743 local = sdata->local;
4744
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004745 if (qos_ok) {
4746 struct sta_info *sta;
4747
4748 rcu_read_lock();
4749 sta = sta_info_get(sdata, ifmgd->bssid);
4750 qos = sta && sta->sta.wme;
4751 rcu_read_unlock();
4752 }
4753
4754 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
4755 sizeof(*nullfunc) + 2);
Joe Perchesd15b8452011-08-29 14:17:31 -07004756 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02004757 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07004758
Kalle Valo7044cc52010-01-05 20:16:19 +02004759 skb_reserve(skb, local->hw.extra_tx_headroom);
4760
Johannes Bergb080db52017-06-16 14:29:19 +02004761 nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
Kalle Valo7044cc52010-01-05 20:16:19 +02004762 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
4763 IEEE80211_STYPE_NULLFUNC |
4764 IEEE80211_FCTL_TODS);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004765 if (qos) {
Johannes Berge0ba7092018-11-09 11:16:46 +01004766 __le16 qoshdr = cpu_to_le16(7);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004767
4768 BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
4769 IEEE80211_STYPE_NULLFUNC) !=
4770 IEEE80211_STYPE_QOS_NULLFUNC);
4771 nullfunc->frame_control |=
4772 cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
4773 skb->priority = 7;
4774 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
Johannes Berge0ba7092018-11-09 11:16:46 +01004775 skb_put_data(skb, &qoshdr, sizeof(qoshdr));
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004776 }
4777
Kalle Valo7044cc52010-01-05 20:16:19 +02004778 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
4779 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
4780 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
4781
4782 return skb;
4783}
4784EXPORT_SYMBOL(ieee80211_nullfunc_get);
4785
Kalle Valo05e54ea2010-01-05 20:16:38 +02004786struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
Johannes Berga344d672014-06-12 22:24:31 +02004787 const u8 *src_addr,
Kalle Valo05e54ea2010-01-05 20:16:38 +02004788 const u8 *ssid, size_t ssid_len,
Johannes Bergb9a9ada2012-11-29 13:00:10 +01004789 size_t tailroom)
Kalle Valo05e54ea2010-01-05 20:16:38 +02004790{
Johannes Berga344d672014-06-12 22:24:31 +02004791 struct ieee80211_local *local = hw_to_local(hw);
Kalle Valo05e54ea2010-01-05 20:16:38 +02004792 struct ieee80211_hdr_3addr *hdr;
4793 struct sk_buff *skb;
4794 size_t ie_ssid_len;
4795 u8 *pos;
4796
Kalle Valo05e54ea2010-01-05 20:16:38 +02004797 ie_ssid_len = 2 + ssid_len;
4798
4799 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
Johannes Bergb9a9ada2012-11-29 13:00:10 +01004800 ie_ssid_len + tailroom);
Joe Perchesd15b8452011-08-29 14:17:31 -07004801 if (!skb)
Kalle Valo05e54ea2010-01-05 20:16:38 +02004802 return NULL;
Kalle Valo05e54ea2010-01-05 20:16:38 +02004803
4804 skb_reserve(skb, local->hw.extra_tx_headroom);
4805
Johannes Bergb080db52017-06-16 14:29:19 +02004806 hdr = skb_put_zero(skb, sizeof(*hdr));
Kalle Valo05e54ea2010-01-05 20:16:38 +02004807 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4808 IEEE80211_STYPE_PROBE_REQ);
Johannes Berge83e6542012-07-13 16:23:07 +02004809 eth_broadcast_addr(hdr->addr1);
Johannes Berga344d672014-06-12 22:24:31 +02004810 memcpy(hdr->addr2, src_addr, ETH_ALEN);
Johannes Berge83e6542012-07-13 16:23:07 +02004811 eth_broadcast_addr(hdr->addr3);
Kalle Valo05e54ea2010-01-05 20:16:38 +02004812
4813 pos = skb_put(skb, ie_ssid_len);
4814 *pos++ = WLAN_EID_SSID;
4815 *pos++ = ssid_len;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02004816 if (ssid_len)
Kalle Valo05e54ea2010-01-05 20:16:38 +02004817 memcpy(pos, ssid, ssid_len);
4818 pos += ssid_len;
4819
Kalle Valo05e54ea2010-01-05 20:16:38 +02004820 return skb;
4821}
4822EXPORT_SYMBOL(ieee80211_probereq_get);
4823
Johannes Berg32bfd352007-12-19 01:31:26 +01004824void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02004825 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02004826 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02004827 struct ieee80211_rts *rts)
4828{
4829 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02004830
Harvey Harrison065e96052008-06-22 16:45:27 -07004831 rts->frame_control =
4832 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01004833 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
4834 frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02004835 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
4836 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
4837}
4838EXPORT_SYMBOL(ieee80211_rts_get);
4839
Johannes Berg32bfd352007-12-19 01:31:26 +01004840void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02004841 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02004842 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02004843 struct ieee80211_cts *cts)
4844{
4845 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02004846
Harvey Harrison065e96052008-06-22 16:45:27 -07004847 cts->frame_control =
4848 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01004849 cts->duration = ieee80211_ctstoself_duration(hw, vif,
4850 frame_len, frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02004851 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
4852}
4853EXPORT_SYMBOL(ieee80211_ctstoself_get);
4854
4855struct sk_buff *
Johannes Berg32bfd352007-12-19 01:31:26 +01004856ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
Johannes Berge039fa42008-05-15 12:55:29 +02004857 struct ieee80211_vif *vif)
Johannes Berge2ebc742007-07-27 15:43:22 +02004858{
4859 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004860 struct sk_buff *skb = NULL;
Johannes Berg5cf121c2008-02-25 16:27:43 +01004861 struct ieee80211_tx_data tx;
Johannes Berge2ebc742007-07-27 15:43:22 +02004862 struct ieee80211_sub_if_data *sdata;
Marco Porschd012a602012-10-10 12:39:50 -07004863 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +02004864 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02004865 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berge2ebc742007-07-27 15:43:22 +02004866
Johannes Berg32bfd352007-12-19 01:31:26 +01004867 sdata = vif_to_sdata(vif);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004868
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004869 rcu_read_lock();
Johannes Berg55de9082012-07-26 17:24:39 +02004870 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004871
Marco Porschd012a602012-10-10 12:39:50 -07004872 if (!chanctx_conf)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004873 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004874
Marco Porschd012a602012-10-10 12:39:50 -07004875 if (sdata->vif.type == NL80211_IFTYPE_AP) {
4876 struct beacon_data *beacon =
4877 rcu_dereference(sdata->u.ap.beacon);
4878
4879 if (!beacon || !beacon->head)
4880 goto out;
4881
4882 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01004883 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4884 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07004885 } else {
4886 goto out;
4887 }
4888
4889 if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004890 goto out; /* send buffered bc/mc only after DTIM beacon */
Johannes Berge039fa42008-05-15 12:55:29 +02004891
Johannes Berge2ebc742007-07-27 15:43:22 +02004892 while (1) {
Marco Porschd012a602012-10-10 12:39:50 -07004893 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +02004894 if (!skb)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004895 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02004896 local->total_ps_buffered--;
4897
Marco Porschd012a602012-10-10 12:39:50 -07004898 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004899 struct ieee80211_hdr *hdr =
4900 (struct ieee80211_hdr *) skb->data;
4901 /* more buffered multicast/broadcast frames ==> set
4902 * MoreData flag in IEEE 802.11 header to inform PS
4903 * STAs */
4904 hdr->frame_control |=
4905 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
4906 }
4907
Michael Braun112c44b2014-03-06 15:08:43 +01004908 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porsch7cbf9d02013-03-01 16:01:18 +01004909 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
Johannes Berg7c107702015-03-20 14:18:27 +01004910 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
Johannes Berge2ebc742007-07-27 15:43:22 +02004911 break;
Felix Fietkau6b07d9c2016-08-02 11:13:41 +02004912 ieee80211_free_txskb(hw, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02004913 }
Johannes Berge039fa42008-05-15 12:55:29 +02004914
4915 info = IEEE80211_SKB_CB(skb);
4916
Johannes Berg5cf121c2008-02-25 16:27:43 +01004917 tx.flags |= IEEE80211_TX_PS_BUFFERED;
Johannes Berg4bf88532012-11-09 11:39:59 +01004918 info->band = chanctx_conf->def.chan->band;
Johannes Berge2ebc742007-07-27 15:43:22 +02004919
Johannes Berg97b045d2008-06-20 01:22:30 +02004920 if (invoke_tx_handlers(&tx))
Johannes Berge2ebc742007-07-27 15:43:22 +02004921 skb = NULL;
Johannes Berg97b045d2008-06-20 01:22:30 +02004922 out:
Johannes Bergd0709a62008-02-25 16:27:46 +01004923 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004924
4925 return skb;
4926}
4927EXPORT_SYMBOL(ieee80211_get_buffered_bc);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004928
Liad Kaufmanb6da9112014-11-19 13:47:38 +02004929int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
4930{
4931 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
4932 struct ieee80211_sub_if_data *sdata = sta->sdata;
4933 struct ieee80211_local *local = sdata->local;
4934 int ret;
4935 u32 queues;
4936
4937 lockdep_assert_held(&local->sta_mtx);
4938
4939 /* only some cases are supported right now */
4940 switch (sdata->vif.type) {
4941 case NL80211_IFTYPE_STATION:
4942 case NL80211_IFTYPE_AP:
4943 case NL80211_IFTYPE_AP_VLAN:
4944 break;
4945 default:
4946 WARN_ON(1);
4947 return -EINVAL;
4948 }
4949
4950 if (WARN_ON(tid >= IEEE80211_NUM_UPS))
4951 return -EINVAL;
4952
4953 if (sta->reserved_tid == tid) {
4954 ret = 0;
4955 goto out;
4956 }
4957
4958 if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
4959 sdata_err(sdata, "TID reservation already active\n");
4960 ret = -EALREADY;
4961 goto out;
4962 }
4963
4964 ieee80211_stop_vif_queues(sdata->local, sdata,
4965 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
4966
4967 synchronize_net();
4968
4969 /* Tear down BA sessions so we stop aggregating on this TID */
Johannes Berg30686bf2015-06-02 21:39:54 +02004970 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
Liad Kaufmanb6da9112014-11-19 13:47:38 +02004971 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
4972 __ieee80211_stop_tx_ba_session(sta, tid,
4973 AGG_STOP_LOCAL_REQUEST);
4974 }
4975
4976 queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02004977 __ieee80211_flush_queues(local, sdata, queues, false);
Liad Kaufmanb6da9112014-11-19 13:47:38 +02004978
4979 sta->reserved_tid = tid;
4980
4981 ieee80211_wake_vif_queues(local, sdata,
4982 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
4983
Johannes Berg30686bf2015-06-02 21:39:54 +02004984 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
Liad Kaufmanb6da9112014-11-19 13:47:38 +02004985 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
4986
4987 ret = 0;
4988 out:
4989 return ret;
4990}
4991EXPORT_SYMBOL(ieee80211_reserve_tid);
4992
4993void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
4994{
4995 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
4996 struct ieee80211_sub_if_data *sdata = sta->sdata;
4997
4998 lockdep_assert_held(&sdata->local->sta_mtx);
4999
5000 /* only some cases are supported right now */
5001 switch (sdata->vif.type) {
5002 case NL80211_IFTYPE_STATION:
5003 case NL80211_IFTYPE_AP:
5004 case NL80211_IFTYPE_AP_VLAN:
5005 break;
5006 default:
5007 WARN_ON(1);
5008 return;
5009 }
5010
5011 if (tid != sta->reserved_tid) {
5012 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
5013 return;
5014 }
5015
5016 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
5017}
5018EXPORT_SYMBOL(ieee80211_unreserve_tid);
5019
Johannes Berg55de9082012-07-26 17:24:39 +02005020void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
5021 struct sk_buff *skb, int tid,
Johannes Bergb9771d42018-05-28 15:47:41 +02005022 enum nl80211_band band, u32 txdata_flags)
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005023{
Amadeusz Sławiński731977e2017-01-24 16:42:10 +01005024 int ac = ieee80211_ac_from_tid(tid);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02005025
Zhang Shengjud57a5442016-03-03 01:16:56 +00005026 skb_reset_mac_header(skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02005027 skb_set_queue_mapping(skb, ac);
Helmut Schaacf6bb792011-12-15 10:18:34 +01005028 skb->priority = tid;
Johannes Bergcf0277e2010-01-05 18:00:58 +01005029
Johannes Berg89afe612013-02-13 15:39:57 +01005030 skb->dev = sdata->dev;
5031
Johannes Berg3d34deb2009-06-18 17:25:11 +02005032 /*
5033 * The other path calling ieee80211_xmit is from the tasklet,
5034 * and while we can handle concurrent transmissions locking
5035 * requirements are that we do not come into tx with bhs on.
5036 */
5037 local_bh_disable();
Johannes Berg73c4e192014-11-09 18:50:09 +02005038 IEEE80211_SKB_CB(skb)->band = band;
Johannes Bergb9771d42018-05-28 15:47:41 +02005039 ieee80211_xmit(sdata, NULL, skb, txdata_flags);
Johannes Berg3d34deb2009-06-18 17:25:11 +02005040 local_bh_enable();
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005041}
Denis Kenzior91180642018-03-26 12:52:50 -05005042
5043int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
5044 const u8 *buf, size_t len,
5045 const u8 *dest, __be16 proto, bool unencrypted)
5046{
5047 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5048 struct ieee80211_local *local = sdata->local;
5049 struct sk_buff *skb;
5050 struct ethhdr *ehdr;
5051 u32 flags;
5052
5053 /* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
5054 * or Pre-Authentication
5055 */
5056 if (proto != sdata->control_port_protocol &&
5057 proto != cpu_to_be16(ETH_P_PREAUTH))
5058 return -EINVAL;
5059
5060 if (unencrypted)
5061 flags = IEEE80211_TX_INTFL_DONT_ENCRYPT;
5062 else
5063 flags = 0;
5064
5065 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5066 sizeof(struct ethhdr) + len);
5067 if (!skb)
5068 return -ENOMEM;
5069
5070 skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
5071
5072 skb_put_data(skb, buf, len);
5073
5074 ehdr = skb_push(skb, sizeof(struct ethhdr));
5075 memcpy(ehdr->h_dest, dest, ETH_ALEN);
5076 memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
5077 ehdr->h_proto = proto;
5078
5079 skb->dev = dev;
5080 skb->protocol = htons(ETH_P_802_3);
5081 skb_reset_network_header(skb);
5082 skb_reset_mac_header(skb);
5083
Denis Kenziore7441c92018-06-19 10:39:50 -05005084 local_bh_disable();
Rajkumar Manoharan06016772019-04-11 13:47:25 -07005085 __ieee80211_subif_start_xmit(skb, skb->dev, flags, 0);
Denis Kenziore7441c92018-06-19 10:39:50 -05005086 local_bh_enable();
Denis Kenzior91180642018-03-26 12:52:50 -05005087
5088 return 0;
5089}
Rajkumar Manoharan8828f812019-04-11 13:47:26 -07005090
5091int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
5092 const u8 *buf, size_t len)
5093{
5094 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5095 struct ieee80211_local *local = sdata->local;
5096 struct sk_buff *skb;
5097
5098 skb = dev_alloc_skb(local->hw.extra_tx_headroom + len +
5099 30 + /* header size */
5100 18); /* 11s header size */
5101 if (!skb)
5102 return -ENOMEM;
5103
5104 skb_reserve(skb, local->hw.extra_tx_headroom);
5105 skb_put_data(skb, buf, len);
5106
5107 skb->dev = dev;
5108 skb->protocol = htons(ETH_P_802_3);
5109 skb_reset_network_header(skb);
5110 skb_reset_mac_header(skb);
5111
5112 local_bh_disable();
5113 __ieee80211_subif_start_xmit(skb, skb->dev, 0,
5114 IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP);
Johannes Berge2ebc742007-07-27 15:43:22 +02005115 local_bh_enable();
5116
5117 return 0;
5118}