blob: 7dbfb9e3cd84279f5ffb83235791e1cdfc7d6d56 [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:
Arend van Sprielc5b9a7f2019-08-02 13:30:58 +0200165 case NL80211_BAND_6GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +0100166 if (r->flags & IEEE80211_RATE_MANDATORY_A)
167 mrate = r->bitrate;
168 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200169 case NL80211_BAND_60GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300170 /* TODO, for now fall through */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200171 case NUM_NL80211_BANDS:
Johannes Berg8318d782008-01-24 19:38:38 +0100172 WARN_ON(1);
173 break;
174 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200175 }
176 if (rate == -1) {
177 /* No matching basic rate found; use highest suitable mandatory
178 * PHY rate */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200179 rate = DIV_ROUND_UP(mrate, 1 << shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200180 }
181
Simon Wunderlich6674f212011-11-21 21:34:30 +0100182 /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
183 if (ieee80211_is_data_qos(hdr->frame_control) &&
Claudio Pisac26a0e12012-05-28 13:06:25 +0100184 *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
Simon Wunderlich6674f212011-11-21 21:34:30 +0100185 dur = 0;
186 else
187 /* Time needed to transmit ACK
188 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
189 * to closest integer */
Michal Kazior4ee73f32012-04-11 08:47:56 +0200190 dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200191 tx->sdata->vif.bss_conf.use_short_preamble,
192 shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200193
194 if (next_frag_len) {
195 /* Frame is fragmented: duration increases with time needed to
196 * transmit next fragment plus ACK and 2 x SIFS. */
197 dur *= 2; /* ACK + SIFS */
198 /* next fragment */
Michal Kazior4ee73f32012-04-11 08:47:56 +0200199 dur += ieee80211_frame_duration(sband->band, next_frag_len,
Johannes Berg8318d782008-01-24 19:38:38 +0100200 txrate->bitrate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200201 tx->sdata->vif.bss_conf.use_short_preamble,
202 shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200203 }
204
Johannes Berg03f93c32008-06-25 14:36:27 +0300205 return cpu_to_le16(dur);
Johannes Berge2ebc742007-07-27 15:43:22 +0200206}
207
Johannes Berge2ebc742007-07-27 15:43:22 +0200208/* tx handlers */
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200209static ieee80211_tx_result debug_noinline
210ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
211{
212 struct ieee80211_local *local = tx->local;
Kalle Valo0c742112010-01-12 10:42:53 +0200213 struct ieee80211_if_managed *ifmgd;
Andrei Otcheretianski94a5b3a2018-09-05 08:06:14 +0300214 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200215
216 /* driver doesn't support power save */
Johannes Berg30686bf2015-06-02 21:39:54 +0200217 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200218 return TX_CONTINUE;
219
220 /* hardware does dynamic power save */
Johannes Berg30686bf2015-06-02 21:39:54 +0200221 if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200222 return TX_CONTINUE;
223
224 /* dynamic power save disabled */
225 if (local->hw.conf.dynamic_ps_timeout <= 0)
226 return TX_CONTINUE;
227
228 /* we are scanning, don't enable power save */
229 if (local->scanning)
230 return TX_CONTINUE;
231
232 if (!local->ps_sdata)
233 return TX_CONTINUE;
234
235 /* No point if we're going to suspend */
236 if (local->quiescing)
237 return TX_CONTINUE;
238
Kalle Valo0c742112010-01-12 10:42:53 +0200239 /* dynamic ps is supported only in managed mode */
240 if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
241 return TX_CONTINUE;
242
Andrei Otcheretianski94a5b3a2018-09-05 08:06:14 +0300243 if (unlikely(info->flags & IEEE80211_TX_INTFL_OFFCHAN_TX_OK))
244 return TX_CONTINUE;
245
Kalle Valo0c742112010-01-12 10:42:53 +0200246 ifmgd = &tx->sdata->u.mgd;
247
248 /*
249 * Don't wakeup from power save if u-apsd is enabled, voip ac has
250 * u-apsd enabled and the frame is in voip class. This effectively
251 * means that even if all access categories have u-apsd enabled, in
252 * practise u-apsd is only used with the voip ac. This is a
253 * workaround for the case when received voip class packets do not
254 * have correct qos tag for some reason, due the network or the
255 * peer application.
256 *
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200257 * Note: ifmgd->uapsd_queues access is racy here. If the value is
Kalle Valo0c742112010-01-12 10:42:53 +0200258 * changed via debugfs, user needs to reassociate manually to have
259 * everything in sync.
260 */
Johannes Berg4875d30d2012-03-27 14:18:37 +0200261 if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
262 (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
263 skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
Kalle Valo0c742112010-01-12 10:42:53 +0200264 return TX_CONTINUE;
265
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200266 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
267 ieee80211_stop_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +0100268 IEEE80211_MAX_QUEUE_MAP,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300269 IEEE80211_QUEUE_STOP_REASON_PS,
270 false);
Vivek Natarajandb285692011-02-18 17:18:03 +0530271 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200272 ieee80211_queue_work(&local->hw,
273 &local->dynamic_ps_disable_work);
274 }
275
Luciano Coelho5db1c072011-05-03 21:40:08 +0300276 /* Don't restart the timer if we're not disassociated */
277 if (!ifmgd->associated)
278 return TX_CONTINUE;
279
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200280 mod_timer(&local->dynamic_ps_timer, jiffies +
281 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
282
283 return TX_CONTINUE;
284}
Johannes Berge2ebc742007-07-27 15:43:22 +0200285
Johannes Bergd9e8a702008-06-30 15:10:44 +0200286static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100287ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200288{
Harvey Harrison358c8d92008-07-15 18:44:13 -0700289
Johannes Berge039fa42008-05-15 12:55:29 +0200290 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200291 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200292 bool assoc = false;
Johannes Berge2ebc742007-07-27 15:43:22 +0200293
Johannes Berge039fa42008-05-15 12:55:29 +0200294 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100295 return TX_CONTINUE;
Johannes Berg58d41852007-09-26 17:53:18 +0200296
Ben Greearb23b0252011-02-04 11:54:17 -0800297 if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
298 test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
Kalle Valoa9a6fff2009-03-18 14:06:44 +0200299 !ieee80211_is_probe_req(hdr->frame_control) &&
300 !ieee80211_is_nullfunc(hdr->frame_control))
301 /*
302 * When software scanning only nullfunc frames (to notify
303 * the sleep state to the AP) and probe requests (for the
304 * active scan) are allowed, all other frames should not be
305 * sent and we should not get here, but if we do
306 * nonetheless, drop them to avoid sending them
307 * off-channel. See the link below and
308 * ieee80211_start_scan() for more.
309 *
310 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
311 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100312 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200313
Rostislav Lisovy239281f2014-11-03 10:33:19 +0100314 if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
315 return TX_CONTINUE;
316
Bill Jordan1be7fe82010-10-01 11:20:41 -0400317 if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
318 return TX_CONTINUE;
319
Johannes Berg5cf121c2008-02-25 16:27:43 +0100320 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100321 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200322
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200323 if (tx->sta)
324 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge2ebc742007-07-27 15:43:22 +0200325
Johannes Berg5cf121c2008-02-25 16:27:43 +0100326 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200327 if (unlikely(!assoc &&
Harvey Harrison358c8d92008-07-15 18:44:13 -0700328 ieee80211_is_data(hdr->frame_control))) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200329#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200330 sdata_info(tx->sdata,
331 "dropped data frame to not associated station %pM\n",
332 hdr->addr1);
333#endif
Johannes Berge2ebc742007-07-27 15:43:22 +0200334 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100335 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200336 }
Michael Braun72f15d52016-10-10 19:12:21 +0200337 } else if (unlikely(ieee80211_is_data(hdr->frame_control) &&
338 ieee80211_vif_get_num_mcast_if(tx->sdata) == 0)) {
Johannes Berg29623892011-12-14 12:20:31 +0100339 /*
340 * No associated STAs - no need to send multicast
341 * frames.
342 */
343 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200344 }
345
Johannes Berg9ae54c82008-01-31 19:48:20 +0100346 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200347}
348
Johannes Berge2ebc742007-07-27 15:43:22 +0200349/* This function is called whenever the AP is about to exceed the maximum limit
350 * of buffered frames for power saving STAs. This situation should not really
351 * happen often during normal operation, so dropping the oldest buffered packet
352 * from each queue should be OK to make some room for new frames. */
353static void purge_old_ps_buffers(struct ieee80211_local *local)
354{
355 int total = 0, purged = 0;
356 struct sk_buff *skb;
357 struct ieee80211_sub_if_data *sdata;
358 struct sta_info *sta;
359
Johannes Berg79010422007-09-18 17:29:21 -0400360 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Marco Porschd012a602012-10-10 12:39:50 -0700361 struct ps_data *ps;
362
363 if (sdata->vif.type == NL80211_IFTYPE_AP)
364 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100365 else if (ieee80211_vif_is_mesh(&sdata->vif))
366 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700367 else
Johannes Berge2ebc742007-07-27 15:43:22 +0200368 continue;
Marco Porschd012a602012-10-10 12:39:50 -0700369
370 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +0200371 if (skb) {
372 purged++;
Felix Fietkau6b07d9c2016-08-02 11:13:41 +0200373 ieee80211_free_txskb(&local->hw, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +0200374 }
Marco Porschd012a602012-10-10 12:39:50 -0700375 total += skb_queue_len(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +0200376 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200377
Johannes Berg948d8872011-09-29 16:04:29 +0200378 /*
379 * Drop one frame from each station from the lowest-priority
380 * AC that has frames at all.
381 */
Johannes Bergd0709a62008-02-25 16:27:46 +0100382 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg948d8872011-09-29 16:04:29 +0200383 int ac;
384
385 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
386 skb = skb_dequeue(&sta->ps_tx_buf[ac]);
387 total += skb_queue_len(&sta->ps_tx_buf[ac]);
388 if (skb) {
389 purged++;
Felix Fietkauc3e77242012-10-08 14:39:33 +0200390 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg948d8872011-09-29 16:04:29 +0200391 break;
392 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200393 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200394 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100395
Johannes Berge2ebc742007-07-27 15:43:22 +0200396 local->total_ps_buffered = total;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200397 ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
Johannes Berge2ebc742007-07-27 15:43:22 +0200398}
399
Johannes Berg9ae54c82008-01-31 19:48:20 +0100400static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100401ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200402{
Johannes Berge039fa42008-05-15 12:55:29 +0200403 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700404 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Marco Porschd012a602012-10-10 12:39:50 -0700405 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +0200406
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100407 /*
408 * broadcast/multicast frame
409 *
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100410 * If any of the associated/peer stations is in power save mode,
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100411 * the frame is buffered to be sent after DTIM beacon frame.
412 * This is done either by the hardware or us.
413 */
414
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100415 /* powersaving STAs currently only in AP/VLAN/mesh mode */
Marco Porschd012a602012-10-10 12:39:50 -0700416 if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
417 tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
418 if (!tx->sdata->bss)
419 return TX_CONTINUE;
420
421 ps = &tx->sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100422 } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
423 ps = &tx->sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700424 } else {
Johannes Berg3e122be2008-07-09 14:40:34 +0200425 return TX_CONTINUE;
Marco Porschd012a602012-10-10 12:39:50 -0700426 }
427
Johannes Berg3e122be2008-07-09 14:40:34 +0200428
429 /* no buffering for ordered frames */
Harvey Harrison358c8d92008-07-15 18:44:13 -0700430 if (ieee80211_has_order(hdr->frame_control))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100431 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100432
Johannes Berg08b99392014-07-07 12:01:11 +0200433 if (ieee80211_is_probe_req(hdr->frame_control))
434 return TX_CONTINUE;
435
Johannes Berg30686bf2015-06-02 21:39:54 +0200436 if (ieee80211_hw_check(&tx->local->hw, QUEUE_CONTROL))
Johannes Bergf4d57942013-05-28 17:24:15 +0200437 info->hw_queue = tx->sdata->vif.cab_queue;
438
Felix Fietkau9ec11902018-11-28 22:39:16 +0100439 /* no stations in PS mode and no buffered packets */
440 if (!atomic_read(&ps->num_sta_ps) && skb_queue_empty(&ps->bc_buf))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100441 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100442
Johannes Berg62b517c2009-10-29 12:19:21 +0100443 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
Johannes Berg62b12082009-08-10 16:04:15 +0200444
Johannes Berg62b517c2009-10-29 12:19:21 +0100445 /* device releases frame after DTIM beacon */
Johannes Berg30686bf2015-06-02 21:39:54 +0200446 if (!ieee80211_hw_check(&tx->local->hw, HOST_BROADCAST_PS_BUFFERING))
Johannes Berg62b12082009-08-10 16:04:15 +0200447 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200448
Johannes Berg62b12082009-08-10 16:04:15 +0200449 /* buffered in mac80211 */
450 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
451 purge_old_ps_buffers(tx->local);
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100452
Marco Porschd012a602012-10-10 12:39:50 -0700453 if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200454 ps_dbg(tx->sdata,
455 "BC TX buffer full - dropping the oldest frame\n");
Felix Fietkau6b07d9c2016-08-02 11:13:41 +0200456 ieee80211_free_txskb(&tx->local->hw, skb_dequeue(&ps->bc_buf));
Johannes Berg62b12082009-08-10 16:04:15 +0200457 } else
458 tx->local->total_ps_buffered++;
459
Marco Porschd012a602012-10-10 12:39:50 -0700460 skb_queue_tail(&ps->bc_buf, tx->skb);
Johannes Berg62b12082009-08-10 16:04:15 +0200461
462 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200463}
464
Jouni Malinenfb733332009-01-08 13:32:00 +0200465static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
466 struct sk_buff *skb)
467{
468 if (!ieee80211_is_mgmt(fc))
469 return 0;
470
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200471 if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
Jouni Malinenfb733332009-01-08 13:32:00 +0200472 return 0;
473
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100474 if (!ieee80211_is_robust_mgmt_frame(skb))
Jouni Malinenfb733332009-01-08 13:32:00 +0200475 return 0;
476
477 return 1;
478}
479
Johannes Berg9ae54c82008-01-31 19:48:20 +0100480static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100481ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200482{
483 struct sta_info *sta = tx->sta;
Johannes Berge039fa42008-05-15 12:55:29 +0200484 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berg08b99392014-07-07 12:01:11 +0200485 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300486 struct ieee80211_local *local = tx->local;
Johannes Berge2ebc742007-07-27 15:43:22 +0200487
Johannes Berg02f2f1a2012-02-27 12:18:30 +0100488 if (unlikely(!sta))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100489 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200490
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200491 if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
Johannes Berg5ac2e352014-05-27 16:32:27 +0200492 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
493 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
Johannes Berg02f2f1a2012-02-27 12:18:30 +0100494 !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
Johannes Berg948d8872011-09-29 16:04:29 +0200495 int ac = skb_get_queue_mapping(tx->skb);
496
Johannes Berg08b99392014-07-07 12:01:11 +0200497 if (ieee80211_is_mgmt(hdr->frame_control) &&
498 !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
499 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
500 return TX_CONTINUE;
501 }
502
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200503 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
504 sta->sta.addr, sta->sta.aid, ac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200505 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
506 purge_old_ps_buffers(tx->local);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200507
508 /* sync with ieee80211_sta_ps_deliver_wakeup */
509 spin_lock(&sta->ps_lock);
510 /*
511 * STA woke up the meantime and all the frames on ps_tx_buf have
512 * been queued to pending queue. No reordering can happen, go
513 * ahead and Tx the packet.
514 */
515 if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
Johannes Berg5ac2e352014-05-27 16:32:27 +0200516 !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
517 !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200518 spin_unlock(&sta->ps_lock);
519 return TX_CONTINUE;
520 }
521
Johannes Berg948d8872011-09-29 16:04:29 +0200522 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
523 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200524 ps_dbg(tx->sdata,
525 "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
526 sta->sta.addr, ac);
Felix Fietkauc3e77242012-10-08 14:39:33 +0200527 ieee80211_free_txskb(&local->hw, old);
Johannes Berge2ebc742007-07-27 15:43:22 +0200528 } else
529 tx->local->total_ps_buffered++;
Johannes Berg004c8722008-02-20 11:21:35 +0100530
Johannes Berge039fa42008-05-15 12:55:29 +0200531 info->control.jiffies = jiffies;
Johannes Berg5061b0c2009-07-14 00:33:34 +0200532 info->control.vif = &tx->sdata->vif;
Johannes Berg8f77f382009-06-07 21:58:37 +0200533 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Johannes Berg03c8c062014-01-09 01:45:28 +0100534 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berg948d8872011-09-29 16:04:29 +0200535 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200536 spin_unlock(&sta->ps_lock);
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300537
538 if (!timer_pending(&local->sta_cleanup))
539 mod_timer(&local->sta_cleanup,
540 round_jiffies(jiffies +
541 STA_INFO_CLEANUP_INTERVAL));
542
Johannes Bergc868cb352011-09-29 16:04:27 +0200543 /*
544 * We queued up some frames, so the TIM bit might
545 * need to be set, recalculate it.
546 */
547 sta_info_recalc_tim(sta);
548
Johannes Berg9ae54c82008-01-31 19:48:20 +0100549 return TX_QUEUED;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200550 } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
551 ps_dbg(tx->sdata,
552 "STA %pM in PS mode, but polling/in SP -> send frame\n",
553 sta->sta.addr);
Johannes Berge2ebc742007-07-27 15:43:22 +0200554 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200555
Johannes Berg9ae54c82008-01-31 19:48:20 +0100556 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200557}
558
Johannes Bergd9e8a702008-06-30 15:10:44 +0200559static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100560ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200561{
Johannes Berg5cf121c2008-02-25 16:27:43 +0100562 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100563 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200564
Johannes Berg5cf121c2008-02-25 16:27:43 +0100565 if (tx->flags & IEEE80211_TX_UNICAST)
Johannes Berge2ebc742007-07-27 15:43:22 +0200566 return ieee80211_tx_h_unicast_ps_buf(tx);
567 else
568 return ieee80211_tx_h_multicast_ps_buf(tx);
569}
570
Johannes Bergd9e8a702008-06-30 15:10:44 +0200571static ieee80211_tx_result debug_noinline
Johannes Berga621fa42010-08-27 14:26:54 +0300572ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
573{
574 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
575
Johannes Bergaf61a162013-07-02 18:09:12 +0200576 if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
577 if (tx->sdata->control_port_no_encrypt)
578 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
579 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
Jouni Malinen9c1c98a2015-02-26 15:50:50 +0200580 info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
Johannes Bergaf61a162013-07-02 18:09:12 +0200581 }
Johannes Berga621fa42010-08-27 14:26:54 +0300582
583 return TX_CONTINUE;
584}
585
586static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100587ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200588{
Johannes Berg46e6de12012-07-04 18:10:07 +0200589 struct ieee80211_key *key;
Johannes Berge039fa42008-05-15 12:55:29 +0200590 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700591 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400592
Johannes Berga0761a32020-03-26 15:09:42 +0200593 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200594 tx->key = NULL;
Johannes Berga0761a32020-03-26 15:09:42 +0200595 return TX_CONTINUE;
596 }
597
598 if (tx->sta &&
599 (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
Johannes Bergd4e46a32007-09-14 11:10:24 -0400600 tx->key = key;
Masashi Honma46f6b062016-06-22 19:55:20 +0900601 else if (ieee80211_is_group_privacy_action(tx->skb) &&
602 (key = rcu_dereference(tx->sdata->default_multicast_key)))
603 tx->key = key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200604 else if (ieee80211_is_mgmt(hdr->frame_control) &&
Jouni Malinenecbcd322010-03-29 23:35:23 -0700605 is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100606 ieee80211_is_robust_mgmt_frame(tx->skb) &&
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200607 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
608 tx->key = key;
Johannes Bergf7e01042010-12-09 19:49:02 +0100609 else if (is_multicast_ether_addr(hdr->addr1) &&
610 (key = rcu_dereference(tx->sdata->default_multicast_key)))
611 tx->key = key;
612 else if (!is_multicast_ether_addr(hdr->addr1) &&
613 (key = rcu_dereference(tx->sdata->default_unicast_key)))
Johannes Bergd4e46a32007-09-14 11:10:24 -0400614 tx->key = key;
Johannes Berge8f4fb72015-03-20 11:37:36 +0100615 else
Johannes Berg46e6de12012-07-04 18:10:07 +0200616 tx->key = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +0200617
618 if (tx->key) {
Johannes Berg813d7662010-01-17 01:47:58 +0100619 bool skip_hw = false;
620
Johannes Berg011bfcc2007-09-17 01:29:25 -0400621 /* TODO: add threshold stuff again */
Johannes Berg176e4f82007-12-18 15:27:47 +0100622
Johannes Berg97359d12010-08-10 09:46:38 +0200623 switch (tx->key->conf.cipher) {
624 case WLAN_CIPHER_SUITE_WEP40:
625 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg97359d12010-08-10 09:46:38 +0200626 case WLAN_CIPHER_SUITE_TKIP:
Harvey Harrison358c8d92008-07-15 18:44:13 -0700627 if (!ieee80211_is_data_present(hdr->frame_control))
Johannes Berg176e4f82007-12-18 15:27:47 +0100628 tx->key = NULL;
629 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200630 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200631 case WLAN_CIPHER_SUITE_CCMP_256:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200632 case WLAN_CIPHER_SUITE_GCMP:
633 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinenfb733332009-01-08 13:32:00 +0200634 if (!ieee80211_is_data_present(hdr->frame_control) &&
635 !ieee80211_use_mfp(hdr->frame_control, tx->sta,
Masashi Honma46f6b062016-06-22 19:55:20 +0900636 tx->skb) &&
637 !ieee80211_is_group_privacy_action(tx->skb))
Jouni Malinenfb733332009-01-08 13:32:00 +0200638 tx->key = NULL;
Kalle Valo3b43a182010-01-23 20:27:14 +0200639 else
640 skip_hw = (tx->key->conf.flags &
Johannes Berge548c492012-09-04 17:08:23 +0200641 IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
Kalle Valo3b43a182010-01-23 20:27:14 +0200642 ieee80211_is_mgmt(hdr->frame_control);
Jouni Malinenfb733332009-01-08 13:32:00 +0200643 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200644 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200645 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Jouni Malinen8ade5382015-01-24 19:52:09 +0200646 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
647 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200648 if (!ieee80211_is_mgmt(hdr->frame_control))
649 tx->key = NULL;
650 break;
Johannes Berg176e4f82007-12-18 15:27:47 +0100651 }
Johannes Berg813d7662010-01-17 01:47:58 +0100652
Johannes Berge54faf22013-01-29 11:41:38 +0100653 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
654 !ieee80211_is_deauth(hdr->frame_control)))
Johannes Berg95acac62011-07-12 12:30:59 +0200655 return TX_DROP;
656
Johannes Bergf12553e2010-01-22 22:07:59 +0100657 if (!skip_hw && tx->key &&
Johannes Berg382b1652010-01-25 11:36:16 +0100658 tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
Johannes Berg813d7662010-01-17 01:47:58 +0100659 info->control.hw_key = &tx->key->conf;
Johannes Berga0761a32020-03-26 15:09:42 +0200660 } else if (!ieee80211_is_mgmt(hdr->frame_control) && tx->sta &&
661 test_sta_flag(tx->sta, WLAN_STA_USES_ENCRYPTION)) {
662 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200663 }
664
Johannes Berg9ae54c82008-01-31 19:48:20 +0100665 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200666}
667
Johannes Bergd9e8a702008-06-30 15:10:44 +0200668static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100669ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200670{
Johannes Berge039fa42008-05-15 12:55:29 +0200671 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200672 struct ieee80211_hdr *hdr = (void *)tx->skb->data;
673 struct ieee80211_supported_band *sband;
Shanyu Zhaoa2c40242010-04-27 11:15:12 -0700674 u32 len;
Johannes Berge6a98542008-10-21 12:40:02 +0200675 struct ieee80211_tx_rate_control txrc;
Felix Fietkau0d528d82013-04-22 16:14:41 +0200676 struct ieee80211_sta_rates *ratetbl = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200677 bool assoc = false;
Johannes Berge6a98542008-10-21 12:40:02 +0200678
679 memset(&txrc, 0, sizeof(txrc));
Johannes Berg8318d782008-01-24 19:38:38 +0100680
Johannes Berg2d565772012-07-23 15:12:51 +0200681 sband = tx->local->hw.wiphy->bands[info->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200682
Shanyu Zhaoa2c40242010-04-27 11:15:12 -0700683 len = min_t(u32, tx->skb->len + FCS_LEN,
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200684 tx->local->hw.wiphy->frag_threshold);
Johannes Berg58d41852007-09-26 17:53:18 +0200685
Johannes Berge6a98542008-10-21 12:40:02 +0200686 /* set up the tx rate control struct we give the RC algo */
Johannes Berg005e4722012-02-26 11:24:35 +0100687 txrc.hw = &tx->local->hw;
Johannes Berge6a98542008-10-21 12:40:02 +0200688 txrc.sband = sband;
689 txrc.bss_conf = &tx->sdata->vif.bss_conf;
690 txrc.skb = tx->skb;
691 txrc.reported_rate.idx = -1;
Johannes Berg2d565772012-07-23 15:12:51 +0200692 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
Felix Fietkau2ffbe6d2013-04-16 13:38:42 +0200693
694 if (tx->sdata->rc_has_mcs_mask[info->band])
695 txrc.rate_idx_mcs_mask =
696 tx->sdata->rc_rateidx_mcs_mask[info->band];
697
Felix Fietkau8f0729b2010-11-11 15:07:23 +0100698 txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -0800699 tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
Bertold Van den Bergh5765f9f2015-08-05 16:02:28 +0200700 tx->sdata->vif.type == NL80211_IFTYPE_ADHOC ||
701 tx->sdata->vif.type == NL80211_IFTYPE_OCB);
Johannes Berg58d41852007-09-26 17:53:18 +0200702
Johannes Berge6a98542008-10-21 12:40:02 +0200703 /* set up RTS protection if desired */
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200704 if (len > tx->local->hw.wiphy->rts_threshold) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200705 txrc.rts = true;
Johannes Berge2ebc742007-07-27 15:43:22 +0200706 }
Johannes Berge6a98542008-10-21 12:40:02 +0200707
Felix Fietkau0d528d82013-04-22 16:14:41 +0200708 info->control.use_rts = txrc.rts;
Felix Fietkau991fec02013-04-16 13:38:43 +0200709 info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
710
Johannes Berge6a98542008-10-21 12:40:02 +0200711 /*
712 * Use short preamble if the BSS can handle it, but not for
713 * management frames unless we know the receiver can handle
714 * that -- the management frame might be to a station that
715 * just wants a probe response.
716 */
717 if (tx->sdata->vif.bss_conf.use_short_preamble &&
718 (ieee80211_is_data(hdr->frame_control) ||
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200719 (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
Felix Fietkau0d528d82013-04-22 16:14:41 +0200720 txrc.short_preamble = true;
721
722 info->control.short_preamble = txrc.short_preamble;
Johannes Berge6a98542008-10-21 12:40:02 +0200723
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +0100724 /* don't ask rate control when rate already injected via radiotap */
725 if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)
726 return TX_CONTINUE;
727
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200728 if (tx->sta)
729 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge6a98542008-10-21 12:40:02 +0200730
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700731 /*
732 * Lets not bother rate control if we're associated and cannot
733 * talk to the sta. This should not happen.
734 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200735 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700736 !rate_usable_index_exists(sband, &tx->sta->sta),
737 "%s: Dropped data frame as no usable bitrate found while "
738 "scanning and associated. Target station: "
739 "%pM on %d GHz band\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100740 tx->sdata->name, hdr->addr1,
Johannes Berg2d565772012-07-23 15:12:51 +0200741 info->band ? 5 : 2))
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700742 return TX_DROP;
743
744 /*
745 * If we're associated with the sta at this point we know we can at
746 * least send the frame at the lowest bit rate.
747 */
Johannes Berge6a98542008-10-21 12:40:02 +0200748 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
749
Felix Fietkau0d528d82013-04-22 16:14:41 +0200750 if (tx->sta && !info->control.skip_table)
751 ratetbl = rcu_dereference(tx->sta->sta.rates);
752
753 if (unlikely(info->control.rates[0].idx < 0)) {
754 if (ratetbl) {
755 struct ieee80211_tx_rate rate = {
756 .idx = ratetbl->rate[0].idx,
757 .flags = ratetbl->rate[0].flags,
758 .count = ratetbl->rate[0].count
759 };
760
761 if (ratetbl->rate[0].idx < 0)
762 return TX_DROP;
763
764 tx->rate = rate;
765 } else {
766 return TX_DROP;
767 }
768 } else {
769 tx->rate = info->control.rates[0];
770 }
Johannes Berge6a98542008-10-21 12:40:02 +0200771
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100772 if (txrc.reported_rate.idx < 0) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200773 txrc.reported_rate = tx->rate;
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100774 if (tx->sta && ieee80211_is_data(hdr->frame_control))
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200775 tx->sta->tx_stats.last_rate = txrc.reported_rate;
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100776 } else if (tx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200777 tx->sta->tx_stats.last_rate = txrc.reported_rate;
Johannes Berge6a98542008-10-21 12:40:02 +0200778
Felix Fietkau0d528d82013-04-22 16:14:41 +0200779 if (ratetbl)
780 return TX_CONTINUE;
781
Johannes Berge6a98542008-10-21 12:40:02 +0200782 if (unlikely(!info->control.rates[0].count))
783 info->control.rates[0].count = 1;
784
Gábor Stefanik99551512009-04-23 19:36:14 +0200785 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
786 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
787 info->control.rates[0].count = 1;
788
Johannes Berg9ae54c82008-01-31 19:48:20 +0100789 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200790}
791
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100792static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
793{
794 u16 *seq = &sta->tid_seq[tid];
795 __le16 ret = cpu_to_le16(*seq);
796
797 /* Increase the sequence number. */
798 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
799
800 return ret;
801}
802
Johannes Bergd9e8a702008-06-30 15:10:44 +0200803static ieee80211_tx_result debug_noinline
Johannes Bergf591fa52008-07-10 11:21:26 +0200804ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
805{
806 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
807 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergf591fa52008-07-10 11:21:26 +0200808 int tid;
809
Johannes Berg25d834e2008-09-12 22:52:47 +0200810 /*
811 * Packet injection may want to control the sequence
812 * number, if we have no matching interface then we
813 * neither assign one ourselves nor ask the driver to.
814 */
Johannes Berg5061b0c2009-07-14 00:33:34 +0200815 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
Johannes Berg25d834e2008-09-12 22:52:47 +0200816 return TX_CONTINUE;
817
Johannes Bergf591fa52008-07-10 11:21:26 +0200818 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
819 return TX_CONTINUE;
820
821 if (ieee80211_hdrlen(hdr->frame_control) < 24)
822 return TX_CONTINUE;
823
Johannes Berg49a59542011-09-29 16:04:41 +0200824 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
825 return TX_CONTINUE;
826
Johannes Berg94778282008-10-10 13:21:59 +0200827 /*
828 * Anything but QoS data that has a sequence number field
829 * (is long enough) gets a sequence number from the global
Bob Copelandc4c205f2013-08-23 09:35:38 -0400830 * counter. QoS data frames with a multicast destination
831 * also use the global counter (802.11-2012 9.3.2.10).
Johannes Berg94778282008-10-10 13:21:59 +0200832 */
Bob Copelandc4c205f2013-08-23 09:35:38 -0400833 if (!ieee80211_is_data_qos(hdr->frame_control) ||
834 is_multicast_ether_addr(hdr->addr1)) {
Johannes Bergb9771d42018-05-28 15:47:41 +0200835 if (tx->flags & IEEE80211_TX_NO_SEQNO)
836 return TX_CONTINUE;
Johannes Berg94778282008-10-10 13:21:59 +0200837 /* driver should assign sequence number */
Johannes Bergf591fa52008-07-10 11:21:26 +0200838 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
Johannes Berg94778282008-10-10 13:21:59 +0200839 /* for pure STA mode without beacons, we can do it */
840 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
841 tx->sdata->sequence_number += 0x10;
Johannes Berg79c892b2014-11-21 14:26:31 +0100842 if (tx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200843 tx->sta->tx_stats.msdu[IEEE80211_NUM_TIDS]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200844 return TX_CONTINUE;
845 }
846
847 /*
848 * This should be true for injected/management frames only, for
849 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
850 * above since they are not QoS-data frames.
851 */
852 if (!tx->sta)
853 return TX_CONTINUE;
854
855 /* include per-STA, per-TID sequence counter */
Sara Sharona1f2ba04c2018-02-19 14:48:40 +0200856 tid = ieee80211_get_tid(hdr);
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200857 tx->sta->tx_stats.msdu[tid]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200858
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +0200859 hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
Johannes Bergf591fa52008-07-10 11:21:26 +0200860
861 return TX_CONTINUE;
862}
863
Johannes Berg252b86c2011-11-16 15:28:55 +0100864static int ieee80211_fragment(struct ieee80211_tx_data *tx,
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100865 struct sk_buff *skb, int hdrlen,
866 int frag_threshold)
867{
Johannes Berg252b86c2011-11-16 15:28:55 +0100868 struct ieee80211_local *local = tx->local;
Johannes Berga1a3fce2011-11-16 15:28:56 +0100869 struct ieee80211_tx_info *info;
Johannes Berg252b86c2011-11-16 15:28:55 +0100870 struct sk_buff *tmp;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100871 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
872 int pos = hdrlen + per_fragm;
873 int rem = skb->len - hdrlen - per_fragm;
874
875 if (WARN_ON(rem < 0))
876 return -EINVAL;
877
Johannes Berg252b86c2011-11-16 15:28:55 +0100878 /* first fragment was already added to queue by caller */
879
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100880 while (rem) {
881 int fraglen = per_fragm;
882
883 if (fraglen > rem)
884 fraglen = rem;
885 rem -= fraglen;
886 tmp = dev_alloc_skb(local->tx_headroom +
887 frag_threshold +
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200888 tx->sdata->encrypt_headroom +
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100889 IEEE80211_ENCRYPT_TAILROOM);
890 if (!tmp)
891 return -ENOMEM;
Johannes Berg252b86c2011-11-16 15:28:55 +0100892
893 __skb_queue_tail(&tx->skbs, tmp);
894
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200895 skb_reserve(tmp,
896 local->tx_headroom + tx->sdata->encrypt_headroom);
897
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100898 /* copy control information */
899 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
Johannes Berga1a3fce2011-11-16 15:28:56 +0100900
901 info = IEEE80211_SKB_CB(tmp);
902 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
903 IEEE80211_TX_CTL_FIRST_FRAGMENT);
904
905 if (rem)
906 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
907
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100908 skb_copy_queue_mapping(tmp, skb);
909 tmp->priority = skb->priority;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100910 tmp->dev = skb->dev;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100911
912 /* copy header and data */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200913 skb_put_data(tmp, skb->data, hdrlen);
914 skb_put_data(tmp, skb->data + pos, fraglen);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100915
916 pos += fraglen;
917 }
918
Johannes Berg252b86c2011-11-16 15:28:55 +0100919 /* adjust first fragment's length */
Johannes Berg338f9772014-02-01 00:16:23 +0100920 skb_trim(skb, hdrlen + per_fragm);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100921 return 0;
922}
923
Johannes Bergf591fa52008-07-10 11:21:26 +0200924static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +0200925ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
926{
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100927 struct sk_buff *skb = tx->skb;
928 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
929 struct ieee80211_hdr *hdr = (void *)skb->data;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200930 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100931 int hdrlen;
932 int fragnum;
Johannes Berge2454942008-05-15 12:55:28 +0200933
Johannes Berg252b86c2011-11-16 15:28:55 +0100934 /* no matter what happens, tx->skb moves to tx->skbs */
935 __skb_queue_tail(&tx->skbs, skb);
936 tx->skb = NULL;
937
Johannes Berga26eb272011-10-07 14:01:25 +0200938 if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
939 return TX_CONTINUE;
940
Sara Sharonf3fe4e92016-10-18 23:12:11 +0300941 if (ieee80211_hw_check(&tx->local->hw, SUPPORTS_TX_FRAG))
Johannes Berge2454942008-05-15 12:55:28 +0200942 return TX_CONTINUE;
943
Johannes Bergeefce912008-05-17 00:57:13 +0200944 /*
945 * Warn when submitting a fragmented A-MPDU frame and drop it.
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200946 * This scenario is handled in ieee80211_tx_prepare but extra
Ron Rindjunsky8d5e0d52008-06-12 15:42:29 +0300947 * caution taken here as fragmented ampdu may cause Tx stop.
Johannes Bergeefce912008-05-17 00:57:13 +0200948 */
Sujith8b30b1f2008-10-24 09:55:27 +0530949 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
Johannes Bergeefce912008-05-17 00:57:13 +0200950 return TX_DROP;
951
Harvey Harrison065e96052008-06-22 16:45:27 -0700952 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berge2454942008-05-15 12:55:28 +0200953
Johannes Berga26eb272011-10-07 14:01:25 +0200954 /* internal error, why isn't DONTFRAG set? */
Johannes Berg8ccd8f22009-04-29 23:35:56 +0200955 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100956 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200957
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100958 /*
959 * Now fragment the frame. This will allocate all the fragments and
960 * chain them (using skb as the first fragment) to skb->next.
961 * During transmission, we will remove the successfully transmitted
962 * fragments from this list. When the low-level driver rejects one
963 * of the fragments then we will simply pretend to accept the skb
964 * but store it away as pending.
965 */
Johannes Berg252b86c2011-11-16 15:28:55 +0100966 if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100967 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200968
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100969 /* update duration/seq/flags of fragments */
970 fragnum = 0;
Johannes Berg252b86c2011-11-16 15:28:55 +0100971
972 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100973 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
Johannes Berge2454942008-05-15 12:55:28 +0200974
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100975 hdr = (void *)skb->data;
976 info = IEEE80211_SKB_CB(skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200977
Johannes Berg252b86c2011-11-16 15:28:55 +0100978 if (!skb_queue_is_last(&tx->skbs, skb)) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100979 hdr->frame_control |= morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200980 /*
981 * No multi-rate retries for fragmented frames, that
982 * would completely throw off the NAV at other STAs.
983 */
984 info->control.rates[1].idx = -1;
985 info->control.rates[2].idx = -1;
986 info->control.rates[3].idx = -1;
Thomas Huehne3e1a0b2012-07-02 19:46:16 +0200987 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
Johannes Berge6a98542008-10-21 12:40:02 +0200988 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100989 } else {
990 hdr->frame_control &= ~morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200991 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100992 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
993 fragnum++;
Johannes Berg252b86c2011-11-16 15:28:55 +0100994 }
Johannes Berge2454942008-05-15 12:55:28 +0200995
996 return TX_CONTINUE;
Johannes Berge2454942008-05-15 12:55:28 +0200997}
998
Johannes Bergd9e8a702008-06-30 15:10:44 +0200999static ieee80211_tx_result debug_noinline
Johannes Bergfeff1f22009-08-10 16:01:54 +02001000ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
1001{
Johannes Berg252b86c2011-11-16 15:28:55 +01001002 struct sk_buff *skb;
Johannes Berg560d2682013-02-05 10:55:21 +01001003 int ac = -1;
Johannes Bergfeff1f22009-08-10 16:01:54 +02001004
1005 if (!tx->sta)
1006 return TX_CONTINUE;
1007
Johannes Berg252b86c2011-11-16 15:28:55 +01001008 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg560d2682013-02-05 10:55:21 +01001009 ac = skb_get_queue_mapping(skb);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001010 tx->sta->tx_stats.bytes[ac] += skb->len;
Johannes Berg252b86c2011-11-16 15:28:55 +01001011 }
Johannes Berg560d2682013-02-05 10:55:21 +01001012 if (ac >= 0)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001013 tx->sta->tx_stats.packets[ac]++;
Johannes Bergfeff1f22009-08-10 16:01:54 +02001014
1015 return TX_CONTINUE;
1016}
1017
1018static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +02001019ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
1020{
1021 if (!tx->key)
1022 return TX_CONTINUE;
1023
Johannes Berg97359d12010-08-10 09:46:38 +02001024 switch (tx->key->conf.cipher) {
1025 case WLAN_CIPHER_SUITE_WEP40:
1026 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge2454942008-05-15 12:55:28 +02001027 return ieee80211_crypto_wep_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001028 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berge2454942008-05-15 12:55:28 +02001029 return ieee80211_crypto_tkip_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001030 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001031 return ieee80211_crypto_ccmp_encrypt(
1032 tx, IEEE80211_CCMP_MIC_LEN);
1033 case WLAN_CIPHER_SUITE_CCMP_256:
1034 return ieee80211_crypto_ccmp_encrypt(
1035 tx, IEEE80211_CCMP_256_MIC_LEN);
Johannes Berg97359d12010-08-10 09:46:38 +02001036 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001037 return ieee80211_crypto_aes_cmac_encrypt(tx);
Jouni Malinen56c52da2015-01-24 19:52:08 +02001038 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1039 return ieee80211_crypto_aes_cmac_256_encrypt(tx);
Jouni Malinen8ade5382015-01-24 19:52:09 +02001040 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1041 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1042 return ieee80211_crypto_aes_gmac_encrypt(tx);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001043 case WLAN_CIPHER_SUITE_GCMP:
1044 case WLAN_CIPHER_SUITE_GCMP_256:
1045 return ieee80211_crypto_gcmp_encrypt(tx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +03001046 default:
Yoni Divinskyd32a1022012-01-16 15:18:59 +02001047 return ieee80211_crypto_hw_encrypt(tx);
Johannes Berge2454942008-05-15 12:55:28 +02001048 }
1049
Johannes Berge2454942008-05-15 12:55:28 +02001050 return TX_DROP;
1051}
1052
Johannes Bergd9e8a702008-06-30 15:10:44 +02001053static ieee80211_tx_result debug_noinline
Johannes Berg03f93c32008-06-25 14:36:27 +03001054ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1055{
Johannes Berg252b86c2011-11-16 15:28:55 +01001056 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001057 struct ieee80211_hdr *hdr;
1058 int next_len;
1059 bool group_addr;
Johannes Berg03f93c32008-06-25 14:36:27 +03001060
Johannes Berg252b86c2011-11-16 15:28:55 +01001061 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001062 hdr = (void *) skb->data;
Jouni Malinen7e0aae42009-05-19 19:25:58 +03001063 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1064 break; /* must not overwrite AID */
Johannes Berg252b86c2011-11-16 15:28:55 +01001065 if (!skb_queue_is_last(&tx->skbs, skb)) {
1066 struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1067 next_len = next->len;
1068 } else
1069 next_len = 0;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001070 group_addr = is_multicast_ether_addr(hdr->addr1);
Johannes Berg03f93c32008-06-25 14:36:27 +03001071
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001072 hdr->duration_id =
Johannes Berg252b86c2011-11-16 15:28:55 +01001073 ieee80211_duration(tx, skb, group_addr, next_len);
1074 }
Johannes Berg03f93c32008-06-25 14:36:27 +03001075
1076 return TX_CONTINUE;
1077}
1078
Johannes Berge2ebc742007-07-27 15:43:22 +02001079/* actual transmit path */
1080
Johannes Berga622ab72010-06-10 10:21:39 +02001081static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1082 struct sk_buff *skb,
1083 struct ieee80211_tx_info *info,
1084 struct tid_ampdu_tx *tid_tx,
1085 int tid)
1086{
1087 bool queued = false;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001088 bool reset_agg_timer = false;
Helmut Schaaaa454582012-03-07 17:20:30 +01001089 struct sk_buff *purge_skb = NULL;
Johannes Berga622ab72010-06-10 10:21:39 +02001090
1091 if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1092 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001093 reset_agg_timer = true;
Johannes Berg0ab33702010-06-10 10:21:42 +02001094 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1095 /*
1096 * nothing -- this aggregation session is being started
1097 * but that might still fail with the driver
1098 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001099 } else if (!tx->sta->sta.txq[tid]) {
Johannes Berga622ab72010-06-10 10:21:39 +02001100 spin_lock(&tx->sta->lock);
1101 /*
1102 * Need to re-check now, because we may get here
1103 *
1104 * 1) in the window during which the setup is actually
1105 * already done, but not marked yet because not all
1106 * packets are spliced over to the driver pending
1107 * queue yet -- if this happened we acquire the lock
1108 * either before or after the splice happens, but
1109 * need to recheck which of these cases happened.
1110 *
1111 * 2) during session teardown, if the OPERATIONAL bit
1112 * was cleared due to the teardown but the pointer
1113 * hasn't been assigned NULL yet (or we loaded it
1114 * before it was assigned) -- in this case it may
1115 * now be NULL which means we should just let the
1116 * packet pass through because splicing the frames
1117 * back is already done.
1118 */
Johannes Berg40b275b2011-05-13 14:15:49 +02001119 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
Johannes Berga622ab72010-06-10 10:21:39 +02001120
1121 if (!tid_tx) {
1122 /* do nothing, let packet pass through */
1123 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1124 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001125 reset_agg_timer = true;
Johannes Berga622ab72010-06-10 10:21:39 +02001126 } else {
1127 queued = true;
Emmanuel Grumbachf6d46712016-03-17 16:51:42 +02001128 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
1129 clear_sta_flag(tx->sta, WLAN_STA_SP);
1130 ps_dbg(tx->sta->sdata,
1131 "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
1132 tx->sta->sta.addr, tx->sta->sta.aid);
1133 }
Johannes Berga622ab72010-06-10 10:21:39 +02001134 info->control.vif = &tx->sdata->vif;
1135 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Emmanuel Grumbachfacde7f2016-03-17 16:51:41 +02001136 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berga622ab72010-06-10 10:21:39 +02001137 __skb_queue_tail(&tid_tx->pending, skb);
Helmut Schaaaa454582012-03-07 17:20:30 +01001138 if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1139 purge_skb = __skb_dequeue(&tid_tx->pending);
Johannes Berga622ab72010-06-10 10:21:39 +02001140 }
1141 spin_unlock(&tx->sta->lock);
Helmut Schaaaa454582012-03-07 17:20:30 +01001142
1143 if (purge_skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001144 ieee80211_free_txskb(&tx->local->hw, purge_skb);
Johannes Berga622ab72010-06-10 10:21:39 +02001145 }
1146
Nikolay Martynov285fa692011-11-22 21:50:28 -05001147 /* reset session timer */
Sara Sharon914eac22018-04-20 13:49:19 +03001148 if (reset_agg_timer)
Felix Fietkau12d3952f2012-03-18 22:58:06 +01001149 tid_tx->last_tx = jiffies;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001150
Johannes Berga622ab72010-06-10 10:21:39 +02001151 return queued;
1152}
1153
Johannes Berg58d41852007-09-26 17:53:18 +02001154/*
1155 * initialises @tx
Johannes Berg7c107702015-03-20 14:18:27 +01001156 * pass %NULL for the station if unknown, a valid pointer if known
1157 * or an ERR_PTR() if the station is known not to exist
Johannes Berg58d41852007-09-26 17:53:18 +02001158 */
Johannes Berg9ae54c82008-01-31 19:48:20 +01001159static ieee80211_tx_result
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001160ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1161 struct ieee80211_tx_data *tx,
Johannes Berg7c107702015-03-20 14:18:27 +01001162 struct sta_info *sta, struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02001163{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001164 struct ieee80211_local *local = sdata->local;
Johannes Berg58d41852007-09-26 17:53:18 +02001165 struct ieee80211_hdr *hdr;
Johannes Berge039fa42008-05-15 12:55:29 +02001166 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg68f2b512011-10-07 14:01:24 +02001167 int tid;
Johannes Berge2ebc742007-07-27 15:43:22 +02001168
1169 memset(tx, 0, sizeof(*tx));
1170 tx->skb = skb;
Johannes Berge2ebc742007-07-27 15:43:22 +02001171 tx->local = local;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001172 tx->sdata = sdata;
Johannes Berg252b86c2011-11-16 15:28:55 +01001173 __skb_queue_head_init(&tx->skbs);
Johannes Berge2ebc742007-07-27 15:43:22 +02001174
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001175 /*
1176 * If this flag is set to true anywhere, and we get here,
1177 * we are doing the needed processing, so remove the flag
1178 * now.
1179 */
1180 info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1181
Johannes Berg58d41852007-09-26 17:53:18 +02001182 hdr = (struct ieee80211_hdr *) skb->data;
1183
Johannes Berg7c107702015-03-20 14:18:27 +01001184 if (likely(sta)) {
1185 if (!IS_ERR(sta))
1186 tx->sta = sta;
1187 } else {
1188 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1189 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1190 if (!tx->sta && sdata->wdev.use_4addr)
1191 return TX_DROP;
1192 } else if (info->flags & (IEEE80211_TX_INTFL_NL80211_FRAME_TX |
1193 IEEE80211_TX_CTL_INJECTED) ||
1194 tx->sdata->control_port_protocol == tx->skb->protocol) {
1195 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1196 }
1197 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
1198 tx->sta = sta_info_get(sdata, hdr->addr1);
Felix Fietkau3f0e0b22010-01-08 18:15:13 +01001199 }
Johannes Berg58d41852007-09-26 17:53:18 +02001200
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001201 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
Johannes Berg49a59542011-09-29 16:04:41 +02001202 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001203 ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
1204 !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001205 struct tid_ampdu_tx *tid_tx;
1206
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001207 tid = ieee80211_get_tid(hdr);
Sujith8b30b1f2008-10-24 09:55:27 +05301208
Johannes Berga622ab72010-06-10 10:21:39 +02001209 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1210 if (tid_tx) {
1211 bool queued;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001212
Johannes Berga622ab72010-06-10 10:21:39 +02001213 queued = ieee80211_tx_prep_agg(tx, skb, info,
1214 tid_tx, tid);
1215
1216 if (unlikely(queued))
1217 return TX_QUEUED;
1218 }
Sujith8b30b1f2008-10-24 09:55:27 +05301219 }
1220
Jiri Slabybadffb72007-08-28 17:01:54 -04001221 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001222 tx->flags &= ~IEEE80211_TX_UNICAST;
Johannes Berge039fa42008-05-15 12:55:29 +02001223 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Simon Wunderlich6fd67e92011-11-18 14:20:42 +01001224 } else
Johannes Berg5cf121c2008-02-25 16:27:43 +01001225 tx->flags |= IEEE80211_TX_UNICAST;
Johannes Berg58d41852007-09-26 17:53:18 +02001226
Johannes Berga26eb272011-10-07 14:01:25 +02001227 if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1228 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1229 skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1230 info->flags & IEEE80211_TX_CTL_AMPDU)
1231 info->flags |= IEEE80211_TX_CTL_DONTFRAG;
Johannes Berg58d41852007-09-26 17:53:18 +02001232 }
1233
Johannes Berge2ebc742007-07-27 15:43:22 +02001234 if (!tx->sta)
Johannes Berge039fa42008-05-15 12:55:29 +02001235 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Felix Fietkauf7418bc2015-09-24 14:59:49 +02001236 else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
Johannes Berge039fa42008-05-15 12:55:29 +02001237 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Felix Fietkauf7418bc2015-09-24 14:59:49 +02001238 ieee80211_check_fast_xmit(tx->sta);
1239 }
Johannes Berg58d41852007-09-26 17:53:18 +02001240
Johannes Berge039fa42008-05-15 12:55:29 +02001241 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001242
Johannes Berg9ae54c82008-01-31 19:48:20 +01001243 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001244}
1245
Michal Kazior80a83cf2016-05-19 10:37:48 +02001246static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
1247 struct ieee80211_vif *vif,
Michal Kaziordbef5362017-01-13 13:32:51 +01001248 struct sta_info *sta,
Michal Kazior80a83cf2016-05-19 10:37:48 +02001249 struct sk_buff *skb)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001250{
1251 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001252 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001253 struct ieee80211_txq *txq = NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001254
Felix Fietkauc3732a72016-02-28 15:19:53 +01001255 if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
1256 (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
Michal Kazior80a83cf2016-05-19 10:37:48 +02001257 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001258
Johannes Bergadf8ed02018-08-31 11:31:08 +03001259 if (unlikely(!ieee80211_is_data_present(hdr->frame_control))) {
1260 if ((!ieee80211_is_mgmt(hdr->frame_control) ||
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001261 ieee80211_is_bufferable_mmpdu(hdr->frame_control) ||
1262 vif->type == NL80211_IFTYPE_STATION) &&
Johannes Bergadf8ed02018-08-31 11:31:08 +03001263 sta && sta->uploaded) {
1264 /*
1265 * This will be NULL if the driver didn't set the
1266 * opt-in hardware flag.
1267 */
1268 txq = sta->sta.txq[IEEE80211_NUM_TIDS];
1269 }
1270 } else if (sta) {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001271 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1272
Michal Kaziordbef5362017-01-13 13:32:51 +01001273 if (!sta->uploaded)
1274 return NULL;
1275
1276 txq = sta->sta.txq[tid];
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001277 } else if (vif) {
1278 txq = vif->txq;
1279 }
1280
1281 if (!txq)
Michal Kazior80a83cf2016-05-19 10:37:48 +02001282 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001283
Michal Kazior80a83cf2016-05-19 10:37:48 +02001284 return to_txq_info(txq);
1285}
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001286
Michal Kazior5caa3282016-05-19 10:37:51 +02001287static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
1288{
1289 IEEE80211_SKB_CB(skb)->control.enqueue_time = codel_get_time();
1290}
1291
Michal Kazior5caa3282016-05-19 10:37:51 +02001292static u32 codel_skb_len_func(const struct sk_buff *skb)
1293{
1294 return skb->len;
1295}
1296
1297static codel_time_t codel_skb_time_func(const struct sk_buff *skb)
1298{
1299 const struct ieee80211_tx_info *info;
1300
1301 info = (const struct ieee80211_tx_info *)skb->cb;
1302 return info->control.enqueue_time;
1303}
1304
1305static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars,
1306 void *ctx)
1307{
1308 struct ieee80211_local *local;
1309 struct txq_info *txqi;
1310 struct fq *fq;
1311 struct fq_flow *flow;
1312
1313 txqi = ctx;
1314 local = vif_to_sdata(txqi->txq.vif)->local;
1315 fq = &local->fq;
1316
1317 if (cvars == &txqi->def_cvars)
1318 flow = &txqi->def_flow;
1319 else
1320 flow = &fq->flows[cvars - local->cvars];
1321
1322 return fq_flow_dequeue(fq, flow);
1323}
1324
1325static void codel_drop_func(struct sk_buff *skb,
1326 void *ctx)
1327{
1328 struct ieee80211_local *local;
1329 struct ieee80211_hw *hw;
1330 struct txq_info *txqi;
1331
1332 txqi = ctx;
1333 local = vif_to_sdata(txqi->txq.vif)->local;
1334 hw = &local->hw;
1335
1336 ieee80211_free_txskb(hw, skb);
1337}
1338
Michal Kaziorfa962b92016-05-19 10:37:49 +02001339static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
1340 struct fq_tin *tin,
1341 struct fq_flow *flow)
1342{
Michal Kazior5caa3282016-05-19 10:37:51 +02001343 struct ieee80211_local *local;
1344 struct txq_info *txqi;
1345 struct codel_vars *cvars;
1346 struct codel_params *cparams;
1347 struct codel_stats *cstats;
1348
1349 local = container_of(fq, struct ieee80211_local, fq);
1350 txqi = container_of(tin, struct txq_info, tin);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001351 cstats = &txqi->cstats;
Michal Kazior5caa3282016-05-19 10:37:51 +02001352
Toke Høiland-Jørgensen484a54c2017-04-06 11:38:26 +02001353 if (txqi->txq.sta) {
1354 struct sta_info *sta = container_of(txqi->txq.sta,
1355 struct sta_info, sta);
1356 cparams = &sta->cparams;
1357 } else {
1358 cparams = &local->cparams;
1359 }
1360
Michal Kazior5caa3282016-05-19 10:37:51 +02001361 if (flow == &txqi->def_flow)
1362 cvars = &txqi->def_cvars;
1363 else
1364 cvars = &local->cvars[flow - fq->flows];
1365
1366 return codel_dequeue(txqi,
1367 &flow->backlog,
1368 cparams,
1369 cvars,
1370 cstats,
1371 codel_skb_len_func,
1372 codel_skb_time_func,
1373 codel_drop_func,
1374 codel_dequeue_func);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001375}
1376
1377static void fq_skb_free_func(struct fq *fq,
1378 struct fq_tin *tin,
1379 struct fq_flow *flow,
1380 struct sk_buff *skb)
1381{
1382 struct ieee80211_local *local;
1383
1384 local = container_of(fq, struct ieee80211_local, fq);
1385 ieee80211_free_txskb(&local->hw, skb);
1386}
1387
1388static struct fq_flow *fq_flow_get_default_func(struct fq *fq,
1389 struct fq_tin *tin,
1390 int idx,
1391 struct sk_buff *skb)
1392{
1393 struct txq_info *txqi;
1394
1395 txqi = container_of(tin, struct txq_info, tin);
1396 return &txqi->def_flow;
1397}
1398
Michal Kazior80a83cf2016-05-19 10:37:48 +02001399static void ieee80211_txq_enqueue(struct ieee80211_local *local,
1400 struct txq_info *txqi,
1401 struct sk_buff *skb)
1402{
Michal Kaziorfa962b92016-05-19 10:37:49 +02001403 struct fq *fq = &local->fq;
1404 struct fq_tin *tin = &txqi->tin;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001405 u32 flow_idx = fq_flow_idx(fq, skb);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001406
Michal Kazior5caa3282016-05-19 10:37:51 +02001407 ieee80211_set_skb_enqueue_time(skb);
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001408
1409 spin_lock_bh(&fq->lock);
1410 fq_tin_enqueue(fq, tin, flow_idx, skb,
Michal Kaziorfa962b92016-05-19 10:37:49 +02001411 fq_skb_free_func,
1412 fq_flow_get_default_func);
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001413 spin_unlock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001414}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001415
Johannes Berg2a9e2572017-10-06 11:53:33 +02001416static bool fq_vlan_filter_func(struct fq *fq, struct fq_tin *tin,
1417 struct fq_flow *flow, struct sk_buff *skb,
1418 void *data)
1419{
1420 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1421
1422 return info->control.vif == data;
1423}
1424
1425void ieee80211_txq_remove_vlan(struct ieee80211_local *local,
1426 struct ieee80211_sub_if_data *sdata)
1427{
1428 struct fq *fq = &local->fq;
1429 struct txq_info *txqi;
1430 struct fq_tin *tin;
1431 struct ieee80211_sub_if_data *ap;
1432
1433 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1434 return;
1435
1436 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1437
1438 if (!ap->vif.txq)
1439 return;
1440
1441 txqi = to_txq_info(ap->vif.txq);
1442 tin = &txqi->tin;
1443
1444 spin_lock_bh(&fq->lock);
1445 fq_tin_filter(fq, tin, fq_vlan_filter_func, &sdata->vif,
1446 fq_skb_free_func);
1447 spin_unlock_bh(&fq->lock);
1448}
1449
Michal Kaziorfa962b92016-05-19 10:37:49 +02001450void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
1451 struct sta_info *sta,
1452 struct txq_info *txqi, int tid)
1453{
1454 fq_tin_init(&txqi->tin);
1455 fq_flow_init(&txqi->def_flow);
Michal Kazior5caa3282016-05-19 10:37:51 +02001456 codel_vars_init(&txqi->def_cvars);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001457 codel_stats_init(&txqi->cstats);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001458 __skb_queue_head_init(&txqi->frags);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001459 INIT_LIST_HEAD(&txqi->schedule_order);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001460
1461 txqi->txq.vif = &sdata->vif;
1462
Johannes Bergadf8ed02018-08-31 11:31:08 +03001463 if (!sta) {
Michal Kaziorfa962b92016-05-19 10:37:49 +02001464 sdata->vif.txq = &txqi->txq;
1465 txqi->txq.tid = 0;
1466 txqi->txq.ac = IEEE80211_AC_BE;
Johannes Bergadf8ed02018-08-31 11:31:08 +03001467
1468 return;
Michal Kazior80a83cf2016-05-19 10:37:48 +02001469 }
Johannes Bergadf8ed02018-08-31 11:31:08 +03001470
1471 if (tid == IEEE80211_NUM_TIDS) {
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001472 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1473 /* Drivers need to opt in to the management MPDU TXQ */
1474 if (!ieee80211_hw_check(&sdata->local->hw,
1475 STA_MMPDU_TXQ))
1476 return;
1477 } else if (!ieee80211_hw_check(&sdata->local->hw,
1478 BUFF_MMPDU_TXQ)) {
1479 /* Drivers need to opt in to the bufferable MMPDU TXQ */
Johannes Bergadf8ed02018-08-31 11:31:08 +03001480 return;
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001481 }
Johannes Bergadf8ed02018-08-31 11:31:08 +03001482 txqi->txq.ac = IEEE80211_AC_VO;
1483 } else {
1484 txqi->txq.ac = ieee80211_ac_from_tid(tid);
1485 }
1486
1487 txqi->txq.sta = &sta->sta;
1488 txqi->txq.tid = tid;
1489 sta->sta.txq[tid] = &txqi->txq;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001490}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001491
Michal Kaziorfa962b92016-05-19 10:37:49 +02001492void ieee80211_txq_purge(struct ieee80211_local *local,
1493 struct txq_info *txqi)
1494{
1495 struct fq *fq = &local->fq;
1496 struct fq_tin *tin = &txqi->tin;
1497
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08001498 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001499 fq_tin_reset(fq, tin, fq_skb_free_func);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001500 ieee80211_purge_tx_queue(&local->hw, &txqi->frags);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08001501 spin_unlock_bh(&fq->lock);
1502
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001503 spin_lock_bh(&local->active_txq_lock[txqi->txq.ac]);
1504 list_del_init(&txqi->schedule_order);
1505 spin_unlock_bh(&local->active_txq_lock[txqi->txq.ac]);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001506}
1507
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001508void ieee80211_txq_set_params(struct ieee80211_local *local)
1509{
1510 if (local->hw.wiphy->txq_limit)
1511 local->fq.limit = local->hw.wiphy->txq_limit;
1512 else
1513 local->hw.wiphy->txq_limit = local->fq.limit;
1514
1515 if (local->hw.wiphy->txq_memory_limit)
1516 local->fq.memory_limit = local->hw.wiphy->txq_memory_limit;
1517 else
1518 local->hw.wiphy->txq_memory_limit = local->fq.memory_limit;
1519
1520 if (local->hw.wiphy->txq_quantum)
1521 local->fq.quantum = local->hw.wiphy->txq_quantum;
1522 else
1523 local->hw.wiphy->txq_quantum = local->fq.quantum;
1524}
1525
Michal Kaziorfa962b92016-05-19 10:37:49 +02001526int ieee80211_txq_setup_flows(struct ieee80211_local *local)
1527{
1528 struct fq *fq = &local->fq;
1529 int ret;
Michal Kazior5caa3282016-05-19 10:37:51 +02001530 int i;
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001531 bool supp_vht = false;
1532 enum nl80211_band band;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001533
1534 if (!local->ops->wake_tx_queue)
1535 return 0;
1536
1537 ret = fq_init(fq, 4096);
1538 if (ret)
1539 return ret;
1540
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001541 /*
1542 * If the hardware doesn't support VHT, it is safe to limit the maximum
1543 * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
1544 */
1545 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1546 struct ieee80211_supported_band *sband;
1547
1548 sband = local->hw.wiphy->bands[band];
1549 if (!sband)
1550 continue;
1551
1552 supp_vht = supp_vht || sband->vht_cap.vht_supported;
1553 }
1554
1555 if (!supp_vht)
1556 fq->memory_limit = 4 << 20; /* 4 Mbytes */
1557
Michal Kazior5caa3282016-05-19 10:37:51 +02001558 codel_params_init(&local->cparams);
Michal Kazior5caa3282016-05-19 10:37:51 +02001559 local->cparams.interval = MS2TIME(100);
1560 local->cparams.target = MS2TIME(20);
1561 local->cparams.ecn = true;
1562
1563 local->cvars = kcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
1564 GFP_KERNEL);
1565 if (!local->cvars) {
Michal Kazior59a7c822016-06-29 14:00:34 +02001566 spin_lock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001567 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001568 spin_unlock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001569 return -ENOMEM;
1570 }
1571
1572 for (i = 0; i < fq->flows_cnt; i++)
1573 codel_vars_init(&local->cvars[i]);
1574
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001575 ieee80211_txq_set_params(local);
1576
Michal Kaziorfa962b92016-05-19 10:37:49 +02001577 return 0;
1578}
1579
1580void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1581{
1582 struct fq *fq = &local->fq;
1583
1584 if (!local->ops->wake_tx_queue)
1585 return;
1586
Michal Kazior5caa3282016-05-19 10:37:51 +02001587 kfree(local->cvars);
1588 local->cvars = NULL;
1589
Michal Kazior59a7c822016-06-29 14:00:34 +02001590 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001591 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001592 spin_unlock_bh(&fq->lock);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001593}
1594
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001595static bool ieee80211_queue_skb(struct ieee80211_local *local,
1596 struct ieee80211_sub_if_data *sdata,
1597 struct sta_info *sta,
1598 struct sk_buff *skb)
1599{
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001600 struct ieee80211_vif *vif;
1601 struct txq_info *txqi;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001602
1603 if (!local->ops->wake_tx_queue ||
1604 sdata->vif.type == NL80211_IFTYPE_MONITOR)
1605 return false;
1606
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001607 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1608 sdata = container_of(sdata->bss,
1609 struct ieee80211_sub_if_data, u.ap);
1610
1611 vif = &sdata->vif;
Michal Kaziordbef5362017-01-13 13:32:51 +01001612 txqi = ieee80211_get_txq(local, vif, sta, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001613
1614 if (!txqi)
1615 return false;
1616
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001617 ieee80211_txq_enqueue(local, txqi, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001618
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001619 schedule_and_wake_txq(local, txqi);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001620
1621 return true;
1622}
1623
Johannes Berg11127e92011-11-16 16:02:47 +01001624static bool ieee80211_tx_frags(struct ieee80211_local *local,
1625 struct ieee80211_vif *vif,
Johannes Berg4fd03282019-10-01 23:26:35 +02001626 struct sta_info *sta,
Johannes Berg11127e92011-11-16 16:02:47 +01001627 struct sk_buff_head *skbs,
1628 bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001629{
Michal Kazior80a83cf2016-05-19 10:37:48 +02001630 struct ieee80211_tx_control control = {};
Johannes Berg252b86c2011-11-16 15:28:55 +01001631 struct sk_buff *skb, *tmp;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001632 unsigned long flags;
Johannes Berge2ebc742007-07-27 15:43:22 +02001633
Johannes Berg252b86c2011-11-16 15:28:55 +01001634 skb_queue_walk_safe(skbs, skb, tmp) {
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001635 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1636 int q = info->hw_queue;
1637
1638#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1639 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1640 __skb_unlink(skb, skbs);
Felix Fietkauc3e77242012-10-08 14:39:33 +02001641 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001642 continue;
1643 }
1644#endif
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001645
1646 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001647 if (local->queue_stop_reasons[q] ||
Johannes Berg7bb45682011-02-24 14:42:06 +01001648 (!txpending && !skb_queue_empty(&local->pending[q]))) {
Seth Forshee6c17b772013-02-11 11:21:07 -06001649 if (unlikely(info->flags &
Seth Forsheea7679ed2013-02-25 14:58:05 -06001650 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1651 if (local->queue_stop_reasons[q] &
1652 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1653 /*
1654 * Drop off-channel frames if queues
1655 * are stopped for any reason other
1656 * than off-channel operation. Never
1657 * queue them.
1658 */
1659 spin_unlock_irqrestore(
1660 &local->queue_stop_reason_lock,
1661 flags);
1662 ieee80211_purge_tx_queue(&local->hw,
1663 skbs);
1664 return true;
1665 }
1666 } else {
1667
Seth Forshee6c17b772013-02-11 11:21:07 -06001668 /*
Seth Forsheea7679ed2013-02-25 14:58:05 -06001669 * Since queue is stopped, queue up frames for
1670 * later transmission from the tx-pending
1671 * tasklet when the queue is woken again.
Seth Forshee6c17b772013-02-11 11:21:07 -06001672 */
Seth Forsheea7679ed2013-02-25 14:58:05 -06001673 if (txpending)
1674 skb_queue_splice_init(skbs,
1675 &local->pending[q]);
1676 else
1677 skb_queue_splice_tail_init(skbs,
1678 &local->pending[q]);
1679
1680 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1681 flags);
1682 return false;
Seth Forshee6c17b772013-02-11 11:21:07 -06001683 }
Johannes Berg7bb45682011-02-24 14:42:06 +01001684 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001685 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Tomas Winklerf8e79dd2008-07-24 18:46:44 +03001686
Johannes Berg11127e92011-11-16 16:02:47 +01001687 info->control.vif = vif;
Johannes Berg4fd03282019-10-01 23:26:35 +02001688 control.sta = sta ? &sta->sta : NULL;
Johannes Bergec25acc2010-07-22 17:11:28 +02001689
Johannes Berg252b86c2011-11-16 15:28:55 +01001690 __skb_unlink(skb, skbs);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001691 drv_tx(local, &control, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001692 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001693
Johannes Berg11127e92011-11-16 16:02:47 +01001694 return true;
1695}
1696
1697/*
1698 * Returns false if the frame couldn't be transmitted but was queued instead.
1699 */
1700static bool __ieee80211_tx(struct ieee80211_local *local,
1701 struct sk_buff_head *skbs, int led_len,
1702 struct sta_info *sta, bool txpending)
1703{
1704 struct ieee80211_tx_info *info;
1705 struct ieee80211_sub_if_data *sdata;
1706 struct ieee80211_vif *vif;
Johannes Berg11127e92011-11-16 16:02:47 +01001707 struct sk_buff *skb;
1708 bool result = true;
1709 __le16 fc;
1710
1711 if (WARN_ON(skb_queue_empty(skbs)))
1712 return true;
1713
1714 skb = skb_peek(skbs);
1715 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1716 info = IEEE80211_SKB_CB(skb);
1717 sdata = vif_to_sdata(info->control.vif);
1718 if (sta && !sta->uploaded)
1719 sta = NULL;
1720
Johannes Berg11127e92011-11-16 16:02:47 +01001721 switch (sdata->vif.type) {
1722 case NL80211_IFTYPE_MONITOR:
Aviya Erenfeldd8212182016-08-29 23:25:15 +03001723 if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
Johannes Bergc82b5a72013-07-14 23:39:20 +03001724 vif = &sdata->vif;
1725 break;
1726 }
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001727 sdata = rcu_dereference(local->monitor_sdata);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001728 if (sdata) {
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001729 vif = &sdata->vif;
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001730 info->hw_queue =
1731 vif->hw_queue[skb_get_queue_mapping(skb)];
Johannes Berg30686bf2015-06-02 21:39:54 +02001732 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
Johannes Berg63b4d8b2015-11-24 15:41:50 +01001733 ieee80211_purge_tx_queue(&local->hw, skbs);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001734 return true;
1735 } else
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001736 vif = NULL;
Johannes Berg11127e92011-11-16 16:02:47 +01001737 break;
1738 case NL80211_IFTYPE_AP_VLAN:
1739 sdata = container_of(sdata->bss,
1740 struct ieee80211_sub_if_data, u.ap);
1741 /* fall through */
1742 default:
1743 vif = &sdata->vif;
1744 break;
1745 }
1746
Johannes Berg4fd03282019-10-01 23:26:35 +02001747 result = ieee80211_tx_frags(local, vif, sta, skbs, 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
Fredrik Olofsson753ffad2019-11-19 14:34:51 +01002265 /*
2266 * Initialize skb->priority for QoS frames. This is put in the TID field
2267 * of the frame before passing it to the driver.
2268 */
2269 if (ieee80211_is_data_qos(hdr->frame_control)) {
2270 u8 *p = ieee80211_get_qos_ctl(hdr);
2271 skb->priority = *p & IEEE80211_QOS_CTL_TAG1D_MASK;
2272 }
2273
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002274 memset(info, 0, sizeof(*info));
2275
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002276 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
Johannes Berg73b9f032011-10-07 14:01:26 +02002277 IEEE80211_TX_CTL_INJECTED;
2278
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002279 rcu_read_lock();
2280
2281 /*
2282 * We process outgoing injected frames that have a local address
2283 * we handle as though they are non-injected frames.
2284 * This code here isn't entirely correct, the local MAC address
2285 * isn't always enough to find the interface to use; for proper
2286 * VLAN/WDS support we will need a different mechanism (which
2287 * likely isn't going to be monitor interfaces).
Johannes Bergb226a822019-11-22 12:42:42 +01002288 *
2289 * This is necessary, for example, for old hostapd versions that
2290 * don't use nl80211-based management TX/RX.
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002291 */
2292 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2293
2294 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2295 if (!ieee80211_sdata_running(tmp_sdata))
2296 continue;
2297 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2298 tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2299 tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
2300 continue;
Joe Perchesb203ca32012-05-08 18:56:52 +00002301 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002302 sdata = tmp_sdata;
2303 break;
2304 }
2305 }
Johannes Berg7351c6b2009-11-19 01:08:30 +01002306
Johannes Berg55de9082012-07-26 17:24:39 +02002307 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2308 if (!chanctx_conf) {
2309 tmp_sdata = rcu_dereference(local->monitor_sdata);
2310 if (tmp_sdata)
2311 chanctx_conf =
2312 rcu_dereference(tmp_sdata->vif.chanctx_conf);
2313 }
Johannes Berg55de9082012-07-26 17:24:39 +02002314
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002315 if (chanctx_conf)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002316 chandef = &chanctx_conf->def;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002317 else if (!local->use_chanctx)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002318 chandef = &local->_oper_chandef;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002319 else
2320 goto fail_rcu;
Johannes Berg55de9082012-07-26 17:24:39 +02002321
2322 /*
2323 * Frame injection is not allowed if beaconing is not allowed
2324 * or if we need radar detection. Beaconing is usually not allowed when
2325 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2326 * Passive scan is also used in world regulatory domains where
2327 * your country is not known and as such it should be treated as
2328 * NO TX unless the channel is explicitly allowed in which case
2329 * your current regulatory domain would not have the passive scan
2330 * flag.
2331 *
2332 * Since AP mode uses monitor interfaces to inject/TX management
2333 * frames we can make AP mode the exception to this rule once it
2334 * supports radar detection as its implementation can deal with
2335 * radar detection by itself. We can do that later by adding a
2336 * monitor flag interfaces used for AP support.
2337 */
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002338 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2339 sdata->vif.type))
Johannes Berg55de9082012-07-26 17:24:39 +02002340 goto fail_rcu;
2341
Johannes Berg73c4e192014-11-09 18:50:09 +02002342 info->band = chandef->chan->band;
Lorenzo Bianconi109843b02016-02-19 12:18:01 +01002343
2344 /* process and remove the injection radiotap header */
2345 if (!ieee80211_parse_tx_radiotap(local, skb))
2346 goto fail_rcu;
2347
Johannes Bergb9771d42018-05-28 15:47:41 +02002348 ieee80211_xmit(sdata, NULL, skb, 0);
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002349 rcu_read_unlock();
2350
Johannes Berge2ebc742007-07-27 15:43:22 +02002351 return NETDEV_TX_OK;
Andy Green9b8a74e2007-07-27 15:43:24 +02002352
Johannes Berg55de9082012-07-26 17:24:39 +02002353fail_rcu:
2354 rcu_read_unlock();
Andy Green9b8a74e2007-07-27 15:43:24 +02002355fail:
2356 dev_kfree_skb(skb);
2357 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
Johannes Berge2ebc742007-07-27 15:43:22 +02002358}
2359
Johannes Berg97ffe752015-03-21 09:13:45 +01002360static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002361{
Johannes Berg97ffe752015-03-21 09:13:45 +01002362 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002363
Johannes Berg97ffe752015-03-21 09:13:45 +01002364 return ethertype == ETH_P_TDLS &&
2365 skb->len > 14 &&
2366 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2367}
2368
2369static int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2370 struct sk_buff *skb,
2371 struct sta_info **sta_out)
2372{
2373 struct sta_info *sta;
2374
2375 switch (sdata->vif.type) {
2376 case NL80211_IFTYPE_AP_VLAN:
2377 sta = rcu_dereference(sdata->u.vlan.sta);
2378 if (sta) {
2379 *sta_out = sta;
2380 return 0;
2381 } else if (sdata->wdev.use_4addr) {
2382 return -ENOLINK;
2383 }
2384 /* fall through */
2385 case NL80211_IFTYPE_AP:
2386 case NL80211_IFTYPE_OCB:
2387 case NL80211_IFTYPE_ADHOC:
2388 if (is_multicast_ether_addr(skb->data)) {
2389 *sta_out = ERR_PTR(-ENOENT);
2390 return 0;
2391 }
2392 sta = sta_info_get_bss(sdata, skb->data);
2393 break;
2394 case NL80211_IFTYPE_WDS:
2395 sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
2396 break;
2397#ifdef CONFIG_MAC80211_MESH
2398 case NL80211_IFTYPE_MESH_POINT:
2399 /* determined much later */
2400 *sta_out = NULL;
2401 return 0;
2402#endif
2403 case NL80211_IFTYPE_STATION:
2404 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2405 sta = sta_info_get(sdata, skb->data);
Johannes Berg11d62ca2016-09-13 08:28:22 +02002406 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2407 if (test_sta_flag(sta,
2408 WLAN_STA_TDLS_PEER_AUTH)) {
Johannes Berg97ffe752015-03-21 09:13:45 +01002409 *sta_out = sta;
2410 return 0;
2411 }
2412
2413 /*
2414 * TDLS link during setup - throw out frames to
2415 * peer. Allow TDLS-setup frames to unauthorized
2416 * peers for the special case of a link teardown
2417 * after a TDLS sta is removed due to being
2418 * unreachable.
2419 */
Johannes Berg11d62ca2016-09-13 08:28:22 +02002420 if (!ieee80211_is_tdls_setup(skb))
Johannes Berg97ffe752015-03-21 09:13:45 +01002421 return -EINVAL;
2422 }
2423
2424 }
2425
2426 sta = sta_info_get(sdata, sdata->u.mgd.bssid);
2427 if (!sta)
2428 return -ENOLINK;
2429 break;
2430 default:
2431 return -EINVAL;
2432 }
2433
2434 *sta_out = sta ?: ERR_PTR(-ENOENT);
2435 return 0;
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002436}
2437
John Crispin5d8983c2019-10-29 10:13:02 +01002438static int ieee80211_store_ack_skb(struct ieee80211_local *local,
2439 struct sk_buff *skb,
2440 u32 *info_flags)
2441{
2442 struct sk_buff *ack_skb = skb_clone_sk(skb);
2443 u16 info_id = 0;
2444
2445 if (ack_skb) {
2446 unsigned long flags;
2447 int id;
2448
2449 spin_lock_irqsave(&local->ack_status_lock, flags);
2450 id = idr_alloc(&local->ack_status_frames, ack_skb,
Johannes Bergf2b18ba2020-01-15 12:25:50 +01002451 1, 0x2000, GFP_ATOMIC);
John Crispin5d8983c2019-10-29 10:13:02 +01002452 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2453
2454 if (id >= 0) {
2455 info_id = id;
2456 *info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2457 } else {
2458 kfree_skb(ack_skb);
2459 }
2460 }
2461
2462 return info_id;
2463}
2464
Johannes Berge2ebc742007-07-27 15:43:22 +02002465/**
Johannes Berg4c9451e2014-11-09 18:50:10 +02002466 * ieee80211_build_hdr - build 802.11 header in the given frame
2467 * @sdata: virtual interface to build the header for
2468 * @skb: the skb to build the header in
Liad Kaufman24d342c2014-11-09 18:50:07 +02002469 * @info_flags: skb flags to set
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002470 * @ctrl_flags: info control flags to set
Johannes Berge2ebc742007-07-27 15:43:22 +02002471 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002472 * This function takes the skb with 802.3 header and reformats the header to
2473 * the appropriate IEEE 802.11 header based on which interface the packet is
2474 * being transmitted on.
Johannes Berge2ebc742007-07-27 15:43:22 +02002475 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002476 * Note that this function also takes care of the TX status request and
2477 * potential unsharing of the SKB - this needs to be interleaved with the
2478 * header building.
2479 *
2480 * The function requires the read-side RCU lock held
2481 *
2482 * Returns: the (possibly reallocated) skb or an ERR_PTR() code
Johannes Berge2ebc742007-07-27 15:43:22 +02002483 */
Johannes Berg4c9451e2014-11-09 18:50:10 +02002484static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01002485 struct sk_buff *skb, u32 info_flags,
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002486 struct sta_info *sta, u32 ctrl_flags)
Johannes Berge2ebc742007-07-27 15:43:22 +02002487{
Johannes Berg133b8222008-09-16 14:18:59 +02002488 struct ieee80211_local *local = sdata->local;
Felix Fietkau489ee912010-12-18 19:30:48 +01002489 struct ieee80211_tx_info *info;
Johannes Bergdcf33962012-07-30 15:11:56 +02002490 int head_need;
Harvey Harrison065e96052008-06-22 16:45:27 -07002491 u16 ethertype, hdrlen, meshhdrlen = 0;
2492 __le16 fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002493 struct ieee80211_hdr hdr;
Wey-Yi Guyff67bb82010-08-21 07:23:29 -07002494 struct ieee80211s_hdr mesh_hdr __maybe_unused;
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002495 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02002496 const u8 *encaps_data;
2497 int encaps_len, skip_header_bytes;
Johannes Berg97ffe752015-03-21 09:13:45 +01002498 bool wme_sta = false, authorized = false;
2499 bool tdls_peer;
Johannes Berga729cff2011-11-06 14:13:34 +01002500 bool multicast;
Johannes Berga729cff2011-11-06 14:13:34 +01002501 u16 info_id = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002502 struct ieee80211_chanctx_conf *chanctx_conf;
2503 struct ieee80211_sub_if_data *ap_sdata;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002504 enum nl80211_band band;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002505 int ret;
Johannes Berge2ebc742007-07-27 15:43:22 +02002506
Johannes Berg97ffe752015-03-21 09:13:45 +01002507 if (IS_ERR(sta))
2508 sta = NULL;
2509
Julius Niedworok276d9e82019-03-28 21:01:06 +01002510#ifdef CONFIG_MAC80211_DEBUGFS
2511 if (local->force_tx_status)
2512 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2513#endif
2514
Johannes Berge2ebc742007-07-27 15:43:22 +02002515 /* convert Ethernet header to proper 802.11 header (based on
2516 * operation mode) */
2517 ethertype = (skb->data[12] << 8) | skb->data[13];
Harvey Harrison065e96052008-06-22 16:45:27 -07002518 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
Johannes Berge2ebc742007-07-27 15:43:22 +02002519
Johannes Berg51fb61e2007-12-19 01:31:27 +01002520 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002521 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg97ffe752015-03-21 09:13:45 +01002522 if (sdata->wdev.use_4addr) {
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002523 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2524 /* RA TA DA SA */
2525 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002526 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002527 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2528 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2529 hdrlen = 30;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002530 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Johannes Berga74a8c82014-07-22 14:50:47 +02002531 wme_sta = sta->sta.wme;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002532 }
Johannes Berg55de9082012-07-26 17:24:39 +02002533 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2534 u.ap);
2535 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002536 if (!chanctx_conf) {
2537 ret = -ENOTCONN;
2538 goto free;
2539 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002540 band = chanctx_conf->def.chan->band;
Johannes Berg97ffe752015-03-21 09:13:45 +01002541 if (sdata->wdev.use_4addr)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002542 break;
2543 /* fall through */
2544 case NL80211_IFTYPE_AP:
Arnd Bergmannfe801232013-01-25 14:14:33 +00002545 if (sdata->vif.type == NL80211_IFTYPE_AP)
2546 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002547 if (!chanctx_conf) {
2548 ret = -ENOTCONN;
2549 goto free;
2550 }
Harvey Harrison065e96052008-06-22 16:45:27 -07002551 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
Johannes Berge2ebc742007-07-27 15:43:22 +02002552 /* DA BSSID SA */
2553 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002554 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002555 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2556 hdrlen = 24;
Johannes Berg4bf88532012-11-09 11:39:59 +01002557 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002558 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002559 case NL80211_IFTYPE_WDS:
Harvey Harrison065e96052008-06-22 16:45:27 -07002560 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
Johannes Berge2ebc742007-07-27 15:43:22 +02002561 /* RA TA DA SA */
2562 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002563 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002564 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2565 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2566 hdrlen = 30;
Johannes Berg55de9082012-07-26 17:24:39 +02002567 /*
2568 * This is the exception! WDS style interfaces are prohibited
2569 * when channel contexts are in used so this must be valid
2570 */
Karl Beldan675a0b02013-03-25 16:26:57 +01002571 band = local->hw.conf.chandef.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002572 break;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002573#ifdef CONFIG_MAC80211_MESH
Johannes Berg05c914f2008-09-11 00:01:58 +02002574 case NL80211_IFTYPE_MESH_POINT:
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002575 if (!is_multicast_ether_addr(skb->data)) {
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002576 struct sta_info *next_hop;
2577 bool mpp_lookup = true;
2578
Johannes Bergbf7cd942013-02-15 14:40:31 +01002579 mpath = mesh_path_lookup(sdata, skb->data);
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002580 if (mpath) {
2581 mpp_lookup = false;
2582 next_hop = rcu_dereference(mpath->next_hop);
2583 if (!next_hop ||
2584 !(mpath->flags & (MESH_PATH_ACTIVE |
2585 MESH_PATH_RESOLVING)))
2586 mpp_lookup = true;
2587 }
2588
Henning Roggeab1c7902016-02-03 13:58:37 +01002589 if (mpp_lookup) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002590 mppath = mpp_path_lookup(sdata, skb->data);
Henning Roggeab1c7902016-02-03 13:58:37 +01002591 if (mppath)
2592 mppath->exp_time = jiffies;
2593 }
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002594
2595 if (mppath && mpath)
Bob Copeland2bdaf382016-02-28 20:03:56 -05002596 mesh_path_del(sdata, mpath->dst);
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002597 }
YanBo79617de2008-09-22 13:30:32 +08002598
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002599 /*
Joel A Fernandes9d525012011-01-10 00:44:23 -06002600 * Use address extension if it is a packet from
2601 * another interface or if we know the destination
2602 * is being proxied by a portal (i.e. portal address
2603 * differs from proxied address)
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002604 */
Joe Perchesb203ca32012-05-08 18:56:52 +00002605 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2606 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
Javier Cardona3c5772a2009-08-10 12:15:48 -07002607 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2608 skb->data, skb->data + ETH_ALEN);
Johannes Bergbf7cd942013-02-15 14:40:31 +01002609 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2610 NULL, NULL);
YanBo79617de2008-09-22 13:30:32 +08002611 } else {
Thomas Pedersen27f01122012-08-20 11:28:25 -07002612 /* DS -> MBSS (802.11-2012 13.11.3.3).
2613 * For unicast with unknown forwarding information,
2614 * destination might be in the MBSS or if that fails
2615 * forwarded to another mesh gate. In either case
2616 * resolution will be handled in ieee80211_xmit(), so
2617 * leave the original DA. This also works for mcast */
2618 const u8 *mesh_da = skb->data;
YanBo79617de2008-09-22 13:30:32 +08002619
Thomas Pedersen27f01122012-08-20 11:28:25 -07002620 if (mppath)
2621 mesh_da = mppath->mpp;
2622 else if (mpath)
2623 mesh_da = mpath->dst;
Thomas Pedersen27f01122012-08-20 11:28:25 -07002624
Javier Cardona3c5772a2009-08-10 12:15:48 -07002625 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
Johannes Berg47846c92009-11-25 17:46:19 +01002626 mesh_da, sdata->vif.addr);
Thomas Pedersen27f01122012-08-20 11:28:25 -07002627 if (is_multicast_ether_addr(mesh_da))
2628 /* DA TA mSA AE:SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002629 meshhdrlen = ieee80211_new_mesh_header(
2630 sdata, &mesh_hdr,
2631 skb->data + ETH_ALEN, NULL);
Javier Cardona3c5772a2009-08-10 12:15:48 -07002632 else
Thomas Pedersen27f01122012-08-20 11:28:25 -07002633 /* RA TA mDA mSA AE:DA SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002634 meshhdrlen = ieee80211_new_mesh_header(
2635 sdata, &mesh_hdr, skb->data,
2636 skb->data + ETH_ALEN);
YanBo79617de2008-09-22 13:30:32 +08002637
YanBo79617de2008-09-22 13:30:32 +08002638 }
Johannes Berg55de9082012-07-26 17:24:39 +02002639 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002640 if (!chanctx_conf) {
2641 ret = -ENOTCONN;
2642 goto free;
2643 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002644 band = chanctx_conf->def.chan->band;
Rajkumar Manoharan8828f812019-04-11 13:47:26 -07002645
2646 /* For injected frames, fill RA right away as nexthop lookup
2647 * will be skipped.
2648 */
2649 if ((ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP) &&
2650 is_zero_ether_addr(hdr.addr1))
2651 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002652 break;
2653#endif
Johannes Berg05c914f2008-09-11 00:01:58 +02002654 case NL80211_IFTYPE_STATION:
Johannes Berg97ffe752015-03-21 09:13:45 +01002655 /* we already did checks when looking up the RA STA */
2656 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002657
Johannes Berg97ffe752015-03-21 09:13:45 +01002658 if (tdls_peer) {
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002659 /* DA SA BSSID */
2660 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2661 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2662 memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2663 hdrlen = 24;
2664 } else if (sdata->u.mgd.use_4addr &&
2665 cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2666 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2667 IEEE80211_FCTL_TODS);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002668 /* RA TA DA SA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002669 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002670 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002671 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2672 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2673 hdrlen = 30;
2674 } else {
2675 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2676 /* BSSID SA DA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002677 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002678 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2679 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2680 hdrlen = 24;
2681 }
Johannes Berg55de9082012-07-26 17:24:39 +02002682 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002683 if (!chanctx_conf) {
2684 ret = -ENOTCONN;
2685 goto free;
2686 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002687 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002688 break;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002689 case NL80211_IFTYPE_OCB:
2690 /* DA SA BSSID */
2691 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2692 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2693 eth_broadcast_addr(hdr.addr3);
2694 hdrlen = 24;
2695 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002696 if (!chanctx_conf) {
2697 ret = -ENOTCONN;
2698 goto free;
2699 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002700 band = chanctx_conf->def.chan->band;
2701 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002702 case NL80211_IFTYPE_ADHOC:
Johannes Berge2ebc742007-07-27 15:43:22 +02002703 /* DA SA BSSID */
2704 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2705 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01002706 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002707 hdrlen = 24;
Johannes Berg55de9082012-07-26 17:24:39 +02002708 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002709 if (!chanctx_conf) {
2710 ret = -ENOTCONN;
2711 goto free;
2712 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002713 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002714 break;
2715 default:
Johannes Berg4c9451e2014-11-09 18:50:10 +02002716 ret = -EINVAL;
2717 goto free;
Johannes Berge2ebc742007-07-27 15:43:22 +02002718 }
2719
Johannes Berga729cff2011-11-06 14:13:34 +01002720 multicast = is_multicast_ether_addr(hdr.addr1);
Johannes Berge2ebc742007-07-27 15:43:22 +02002721
Johannes Berg97ffe752015-03-21 09:13:45 +01002722 /* sta is always NULL for mesh */
2723 if (sta) {
2724 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2725 wme_sta = sta->sta.wme;
2726 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2727 /* For mesh, the use of the QoS header is mandatory */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002728 wme_sta = true;
Johannes Berge2ebc742007-07-27 15:43:22 +02002729 }
2730
Johannes Berg527871d2015-03-21 08:09:55 +01002731 /* receiver does QoS (which also means we do) use it */
2732 if (wme_sta) {
Harvey Harrison065e96052008-06-22 16:45:27 -07002733 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002734 hdrlen += 2;
2735 }
2736
2737 /*
Johannes Berg238814f2008-01-28 17:19:37 +01002738 * Drop unicast frames to unauthorised stations unless they are
2739 * EAPOL frames from the local station.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002740 */
Johannes Berg55182e42011-10-12 17:28:21 +02002741 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002742 (sdata->vif.type != NL80211_IFTYPE_OCB) &&
Sergey Ryazanova6ececf2013-08-30 01:35:09 +04002743 !multicast && !authorized &&
Johannes Berg55182e42011-10-12 17:28:21 +02002744 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002745 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002746#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002747 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
Johannes Berg4c9451e2014-11-09 18:50:10 +02002748 sdata->name, hdr.addr1);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002749#endif
2750
2751 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2752
Johannes Berg4c9451e2014-11-09 18:50:10 +02002753 ret = -EPERM;
2754 goto free;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002755 }
2756
Johannes Berga729cff2011-11-06 14:13:34 +01002757 if (unlikely(!multicast && skb->sk &&
John Crispin5d8983c2019-10-29 10:13:02 +01002758 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS))
2759 info_id = ieee80211_store_ack_skb(local, skb, &info_flags);
Johannes Berga729cff2011-11-06 14:13:34 +01002760
Helmut Schaa7e244702010-12-02 18:44:09 +01002761 /*
2762 * If the skb is shared we need to obtain our own copy.
2763 */
2764 if (skb_shared(skb)) {
Johannes Berga729cff2011-11-06 14:13:34 +01002765 struct sk_buff *tmp_skb = skb;
2766
2767 /* can't happen -- skb is a clone if info_id != 0 */
2768 WARN_ON(info_id);
2769
Felix Fietkauf8a0a782010-12-18 19:30:50 +01002770 skb = skb_clone(skb, GFP_ATOMIC);
Helmut Schaa7e244702010-12-02 18:44:09 +01002771 kfree_skb(tmp_skb);
2772
Johannes Berg4c9451e2014-11-09 18:50:10 +02002773 if (!skb) {
2774 ret = -ENOMEM;
2775 goto free;
2776 }
Helmut Schaa7e244702010-12-02 18:44:09 +01002777 }
2778
Harvey Harrison065e96052008-06-22 16:45:27 -07002779 hdr.frame_control = fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002780 hdr.duration_id = 0;
2781 hdr.seq_ctrl = 0;
2782
2783 skip_header_bytes = ETH_HLEN;
2784 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2785 encaps_data = bridge_tunnel_header;
2786 encaps_len = sizeof(bridge_tunnel_header);
2787 skip_header_bytes -= 2;
Simon Hormane5c5d222013-03-28 13:38:25 +09002788 } else if (ethertype >= ETH_P_802_3_MIN) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002789 encaps_data = rfc1042_header;
2790 encaps_len = sizeof(rfc1042_header);
2791 skip_header_bytes -= 2;
2792 } else {
2793 encaps_data = NULL;
2794 encaps_len = 0;
2795 }
2796
2797 skb_pull(skb, skip_header_bytes);
Johannes Berg23c07522008-05-29 10:38:53 +02002798 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002799
Johannes Berg23c07522008-05-29 10:38:53 +02002800 /*
2801 * So we need to modify the skb header and hence need a copy of
2802 * that. The head_need variable above doesn't, so far, include
2803 * the needed header space that we don't need right away. If we
2804 * can, then we don't reallocate right now but only after the
2805 * frame arrives at the master device (if it does...)
2806 *
2807 * If we cannot, however, then we will reallocate to include all
2808 * the ever needed space. Also, if we need to reallocate it anyway,
2809 * make it big enough for everything we may ever need.
2810 */
Johannes Berge2ebc742007-07-27 15:43:22 +02002811
David S. Miller3a5be7d2008-06-18 01:19:51 -07002812 if (head_need > 0 || skb_cloned(skb)) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002813 head_need += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02002814 head_need += local->tx_headroom;
2815 head_need = max_t(int, 0, head_need);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002816 if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
2817 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg1c963be2012-11-07 14:02:30 +01002818 skb = NULL;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002819 return ERR_PTR(-ENOMEM);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002820 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002821 }
2822
Felix Fietkaueae44302016-07-13 11:00:02 +02002823 if (encaps_data)
Johannes Berge2ebc742007-07-27 15:43:22 +02002824 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002825
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002826#ifdef CONFIG_MAC80211_MESH
Felix Fietkaueae44302016-07-13 11:00:02 +02002827 if (meshhdrlen > 0)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002828 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002829#endif
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002830
Harvey Harrison065e96052008-06-22 16:45:27 -07002831 if (ieee80211_is_data_qos(fc)) {
Johannes Bergc29b9b92007-09-14 11:10:24 -04002832 __le16 *qos_control;
2833
Johannes Bergd58ff352017-06-16 14:29:23 +02002834 qos_control = skb_push(skb, 2);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002835 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2836 /*
2837 * Maybe we could actually set some fields here, for now just
2838 * initialise to zero to indicate no special operation.
2839 */
2840 *qos_control = 0;
2841 } else
2842 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2843
Zhang Shengjud57a5442016-03-03 01:16:56 +00002844 skb_reset_mac_header(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002845
Felix Fietkau489ee912010-12-18 19:30:48 +01002846 info = IEEE80211_SKB_CB(skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002847 memset(info, 0, sizeof(*info));
2848
Johannes Berga729cff2011-11-06 14:13:34 +01002849 info->flags = info_flags;
2850 info->ack_frame_id = info_id;
Johannes Berg73c4e192014-11-09 18:50:09 +02002851 info->band = band;
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002852 info->control.flags = ctrl_flags;
Johannes Berga729cff2011-11-06 14:13:34 +01002853
Johannes Berg4c9451e2014-11-09 18:50:10 +02002854 return skb;
2855 free:
2856 kfree_skb(skb);
2857 return ERR_PTR(ret);
Johannes Berge2ebc742007-07-27 15:43:22 +02002858}
2859
Johannes Berg17c18bf2015-03-21 15:25:43 +01002860/*
2861 * fast-xmit overview
2862 *
2863 * The core idea of this fast-xmit is to remove per-packet checks by checking
2864 * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2865 * checks that are needed to get the sta->fast_tx pointer assigned, after which
2866 * much less work can be done per packet. For example, fragmentation must be
2867 * disabled or the fast_tx pointer will not be set. All the conditions are seen
2868 * in the code here.
2869 *
2870 * Once assigned, the fast_tx data structure also caches the per-packet 802.11
2871 * header and other data to aid packet processing in ieee80211_xmit_fast().
2872 *
2873 * The most difficult part of this is that when any of these assumptions
2874 * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
2875 * ieee80211_check_fast_xmit() or friends) is required to reset the data,
2876 * since the per-packet code no longer checks the conditions. This is reflected
2877 * by the calls to these functions throughout the rest of the code, and must be
2878 * maintained if any of the TX path checks change.
2879 */
2880
2881void ieee80211_check_fast_xmit(struct sta_info *sta)
2882{
2883 struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
2884 struct ieee80211_local *local = sta->local;
2885 struct ieee80211_sub_if_data *sdata = sta->sdata;
2886 struct ieee80211_hdr *hdr = (void *)build.hdr;
2887 struct ieee80211_chanctx_conf *chanctx_conf;
2888 __le16 fc;
2889
Johannes Berg30686bf2015-06-02 21:39:54 +02002890 if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002891 return;
2892
2893 /* Locking here protects both the pointer itself, and against concurrent
2894 * invocations winning data access races to, e.g., the key pointer that
2895 * is used.
2896 * Without it, the invocation of this function right after the key
2897 * pointer changes wouldn't be sufficient, as another CPU could access
2898 * the pointer, then stall, and then do the cache update after the CPU
2899 * that invalidated the key.
2900 * With the locking, such scenarios cannot happen as the check for the
2901 * key and the fast-tx assignment are done atomically, so the CPU that
2902 * modifies the key will either wait or other one will see the key
2903 * cleared/changed already.
2904 */
2905 spin_lock_bh(&sta->lock);
Johannes Berg30686bf2015-06-02 21:39:54 +02002906 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
2907 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
Johannes Berg17c18bf2015-03-21 15:25:43 +01002908 sdata->vif.type == NL80211_IFTYPE_STATION)
2909 goto out;
2910
2911 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2912 goto out;
2913
2914 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
2915 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
Felix Fietkauf7418bc2015-09-24 14:59:49 +02002916 test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
2917 test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002918 goto out;
2919
2920 if (sdata->noack_map)
2921 goto out;
2922
2923 /* fast-xmit doesn't handle fragmentation at all */
Johannes Berg725b8122015-04-10 14:02:08 +02002924 if (local->hw.wiphy->frag_threshold != (u32)-1 &&
Sara Sharonf3fe4e92016-10-18 23:12:11 +03002925 !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002926 goto out;
2927
2928 rcu_read_lock();
2929 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2930 if (!chanctx_conf) {
2931 rcu_read_unlock();
2932 goto out;
2933 }
2934 build.band = chanctx_conf->def.chan->band;
2935 rcu_read_unlock();
2936
2937 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2938
2939 switch (sdata->vif.type) {
Johannes Berg3ffd8842015-04-14 10:28:37 +02002940 case NL80211_IFTYPE_ADHOC:
2941 /* DA SA BSSID */
2942 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2943 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2944 memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
2945 build.hdr_len = 24;
2946 break;
Johannes Berg17c18bf2015-03-21 15:25:43 +01002947 case NL80211_IFTYPE_STATION:
2948 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2949 /* DA SA BSSID */
2950 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2951 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2952 memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
2953 build.hdr_len = 24;
2954 break;
2955 }
2956
2957 if (sdata->u.mgd.use_4addr) {
2958 /* non-regular ethertype cannot use the fastpath */
2959 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2960 IEEE80211_FCTL_TODS);
2961 /* RA TA DA SA */
2962 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2963 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2964 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2965 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2966 build.hdr_len = 30;
2967 break;
2968 }
2969 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2970 /* BSSID SA DA */
2971 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2972 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2973 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2974 build.hdr_len = 24;
2975 break;
2976 case NL80211_IFTYPE_AP_VLAN:
2977 if (sdata->wdev.use_4addr) {
2978 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2979 IEEE80211_FCTL_TODS);
2980 /* RA TA DA SA */
2981 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
2982 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2983 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2984 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2985 build.hdr_len = 30;
2986 break;
2987 }
2988 /* fall through */
2989 case NL80211_IFTYPE_AP:
2990 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2991 /* DA BSSID SA */
2992 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2993 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2994 build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
2995 build.hdr_len = 24;
2996 break;
2997 default:
2998 /* not handled on fast-xmit */
2999 goto out;
3000 }
3001
3002 if (sta->sta.wme) {
3003 build.hdr_len += 2;
3004 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3005 }
3006
3007 /* We store the key here so there's no point in using rcu_dereference()
3008 * but that's fine because the code that changes the pointers will call
3009 * this function after doing so. For a single CPU that would be enough,
3010 * for multiple see the comment above.
3011 */
3012 build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
3013 if (!build.key)
3014 build.key = rcu_access_pointer(sdata->default_unicast_key);
3015 if (build.key) {
Johannes Berge495c242015-04-10 14:03:17 +02003016 bool gen_iv, iv_spc, mmic;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003017
3018 gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
3019 iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
David Spinadel9de18d82017-12-01 13:50:52 +02003020 mmic = build.key->conf.flags &
3021 (IEEE80211_KEY_FLAG_GENERATE_MMIC |
3022 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003023
3024 /* don't handle software crypto */
3025 if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
3026 goto out;
3027
Alexander Wetzel62872a92018-08-31 15:00:38 +02003028 /* Key is being removed */
3029 if (build.key->flags & KEY_FLAG_TAINTED)
3030 goto out;
3031
Johannes Berg17c18bf2015-03-21 15:25:43 +01003032 switch (build.key->conf.cipher) {
3033 case WLAN_CIPHER_SUITE_CCMP:
3034 case WLAN_CIPHER_SUITE_CCMP_256:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003035 if (gen_iv)
Johannes Berg17c18bf2015-03-21 15:25:43 +01003036 build.pn_offs = build.hdr_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003037 if (gen_iv || iv_spc)
3038 build.hdr_len += IEEE80211_CCMP_HDR_LEN;
3039 break;
3040 case WLAN_CIPHER_SUITE_GCMP:
3041 case WLAN_CIPHER_SUITE_GCMP_256:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003042 if (gen_iv)
Johannes Berg17c18bf2015-03-21 15:25:43 +01003043 build.pn_offs = build.hdr_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003044 if (gen_iv || iv_spc)
3045 build.hdr_len += IEEE80211_GCMP_HDR_LEN;
3046 break;
Johannes Berge495c242015-04-10 14:03:17 +02003047 case WLAN_CIPHER_SUITE_TKIP:
3048 /* cannot handle MMIC or IV generation in xmit-fast */
3049 if (mmic || gen_iv)
3050 goto out;
3051 if (iv_spc)
3052 build.hdr_len += IEEE80211_TKIP_IV_LEN;
3053 break;
3054 case WLAN_CIPHER_SUITE_WEP40:
3055 case WLAN_CIPHER_SUITE_WEP104:
3056 /* cannot handle IV generation in fast-xmit */
3057 if (gen_iv)
3058 goto out;
3059 if (iv_spc)
3060 build.hdr_len += IEEE80211_WEP_IV_LEN;
3061 break;
3062 case WLAN_CIPHER_SUITE_AES_CMAC:
3063 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3064 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3065 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3066 WARN(1,
3067 "management cipher suite 0x%x enabled for data\n",
3068 build.key->conf.cipher);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003069 goto out;
Johannes Berge495c242015-04-10 14:03:17 +02003070 default:
3071 /* we don't know how to generate IVs for this at all */
3072 if (WARN_ON(gen_iv))
3073 goto out;
3074 /* pure hardware keys are OK, of course */
3075 if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
3076 break;
3077 /* cipher scheme might require space allocation */
3078 if (iv_spc &&
3079 build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
3080 goto out;
3081 if (iv_spc)
3082 build.hdr_len += build.key->conf.iv_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003083 }
3084
3085 fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3086 }
3087
3088 hdr->frame_control = fc;
3089
3090 memcpy(build.hdr + build.hdr_len,
3091 rfc1042_header, sizeof(rfc1042_header));
3092 build.hdr_len += sizeof(rfc1042_header);
3093
3094 fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3095 /* if the kmemdup fails, continue w/o fast_tx */
3096 if (!fast_tx)
3097 goto out;
3098
3099 out:
3100 /* we might have raced against another call to this function */
3101 old = rcu_dereference_protected(sta->fast_tx,
3102 lockdep_is_held(&sta->lock));
3103 rcu_assign_pointer(sta->fast_tx, fast_tx);
3104 if (old)
3105 kfree_rcu(old, rcu_head);
3106 spin_unlock_bh(&sta->lock);
3107}
3108
3109void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3110{
3111 struct sta_info *sta;
3112
3113 rcu_read_lock();
3114 list_for_each_entry_rcu(sta, &local->sta_list, list)
3115 ieee80211_check_fast_xmit(sta);
3116 rcu_read_unlock();
3117}
3118
3119void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3120{
3121 struct ieee80211_local *local = sdata->local;
3122 struct sta_info *sta;
3123
3124 rcu_read_lock();
3125
3126 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3127 if (sdata != sta->sdata &&
3128 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3129 continue;
3130 ieee80211_check_fast_xmit(sta);
3131 }
3132
3133 rcu_read_unlock();
3134}
3135
3136void ieee80211_clear_fast_xmit(struct sta_info *sta)
3137{
3138 struct ieee80211_fast_tx *fast_tx;
3139
3140 spin_lock_bh(&sta->lock);
3141 fast_tx = rcu_dereference_protected(sta->fast_tx,
3142 lockdep_is_held(&sta->lock));
3143 RCU_INIT_POINTER(sta->fast_tx, NULL);
3144 spin_unlock_bh(&sta->lock);
3145
3146 if (fast_tx)
3147 kfree_rcu(fast_tx, rcu_head);
3148}
3149
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003150static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
Sara Sharon166ac9d2018-08-29 08:57:02 +02003151 struct sk_buff *skb, int headroom)
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003152{
Sara Sharon166ac9d2018-08-29 08:57:02 +02003153 if (skb_headroom(skb) < headroom) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003154 I802_DEBUG_INC(local->tx_expand_skb_head);
3155
Sara Sharon166ac9d2018-08-29 08:57:02 +02003156 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003157 wiphy_debug(local->hw.wiphy,
3158 "failed to reallocate TX buffer\n");
3159 return false;
3160 }
3161 }
3162
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003163 return true;
3164}
3165
3166static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3167 struct ieee80211_fast_tx *fast_tx,
3168 struct sk_buff *skb)
3169{
3170 struct ieee80211_local *local = sdata->local;
3171 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3172 struct ieee80211_hdr *hdr;
Michael Braun06f2bb12016-10-15 13:28:18 +02003173 struct ethhdr *amsdu_hdr;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003174 int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3175 int subframe_len = skb->len - hdr_len;
3176 void *data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003177 u8 *qc, *h_80211_src, *h_80211_dst;
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003178 const u8 *bssid;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003179
3180 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3181 return false;
3182
3183 if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3184 return true;
3185
Sara Sharon166ac9d2018-08-29 08:57:02 +02003186 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(*amsdu_hdr)))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003187 return false;
3188
Michael Braun06f2bb12016-10-15 13:28:18 +02003189 data = skb_push(skb, sizeof(*amsdu_hdr));
3190 memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003191 hdr = data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003192 amsdu_hdr = data + hdr_len;
3193 /* h_80211_src/dst is addr* field within hdr */
3194 h_80211_src = data + fast_tx->sa_offs;
3195 h_80211_dst = data + fast_tx->da_offs;
3196
3197 amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3198 ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3199 ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
3200
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003201 /* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3202 * fields needs to be changed to BSSID for A-MSDU frames depending
3203 * on FromDS/ToDS values.
3204 */
3205 switch (sdata->vif.type) {
3206 case NL80211_IFTYPE_STATION:
3207 bssid = sdata->u.mgd.bssid;
3208 break;
3209 case NL80211_IFTYPE_AP:
3210 case NL80211_IFTYPE_AP_VLAN:
3211 bssid = sdata->vif.addr;
3212 break;
3213 default:
3214 bssid = NULL;
3215 }
3216
3217 if (bssid && ieee80211_has_fromds(hdr->frame_control))
3218 ether_addr_copy(h_80211_src, bssid);
3219
3220 if (bssid && ieee80211_has_tods(hdr->frame_control))
3221 ether_addr_copy(h_80211_dst, bssid);
3222
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003223 qc = ieee80211_get_qos_ctl(hdr);
3224 *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3225
3226 info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3227
3228 return true;
3229}
3230
3231static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3232 struct sta_info *sta,
3233 struct ieee80211_fast_tx *fast_tx,
3234 struct sk_buff *skb)
3235{
3236 struct ieee80211_local *local = sdata->local;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003237 struct fq *fq = &local->fq;
3238 struct fq_tin *tin;
3239 struct fq_flow *flow;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003240 u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3241 struct ieee80211_txq *txq = sta->sta.txq[tid];
3242 struct txq_info *txqi;
3243 struct sk_buff **frag_tail, *head;
3244 int subframe_len = skb->len - ETH_ALEN;
3245 u8 max_subframes = sta->sta.max_amsdu_subframes;
3246 int max_frags = local->hw.max_tx_fragments;
3247 int max_amsdu_len = sta->sta.max_amsdu_len;
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003248 int orig_truesize;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003249 u32 flow_idx;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003250 __be16 len;
3251 void *data;
3252 bool ret = false;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003253 unsigned int orig_len;
Lorenzo Bianconi66eb02d2018-08-31 01:04:13 +02003254 int n = 2, nfrags, pad = 0;
Sara Sharon166ac9d2018-08-29 08:57:02 +02003255 u16 hdrlen;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003256
3257 if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
3258 return false;
3259
Sara Sharon344f8e02018-12-15 11:03:07 +02003260 if (skb_is_gso(skb))
3261 return false;
3262
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003263 if (!txq)
3264 return false;
3265
3266 txqi = to_txq_info(txq);
3267 if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3268 return false;
3269
3270 if (sta->sta.max_rc_amsdu_len)
3271 max_amsdu_len = min_t(int, max_amsdu_len,
3272 sta->sta.max_rc_amsdu_len);
3273
Sara Sharonedba6bd2018-09-05 08:06:10 +03003274 if (sta->sta.max_tid_amsdu_len[tid])
3275 max_amsdu_len = min_t(int, max_amsdu_len,
3276 sta->sta.max_tid_amsdu_len[tid]);
3277
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003278 flow_idx = fq_flow_idx(fq, skb);
3279
Michal Kaziorfa962b92016-05-19 10:37:49 +02003280 spin_lock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003281
Michal Kaziorfa962b92016-05-19 10:37:49 +02003282 /* TODO: Ideally aggregation should be done on dequeue to remain
3283 * responsive to environment changes.
3284 */
3285
3286 tin = &txqi->tin;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003287 flow = fq_flow_classify(fq, tin, flow_idx, skb,
3288 fq_flow_get_default_func);
Michal Kaziorfa962b92016-05-19 10:37:49 +02003289 head = skb_peek_tail(&flow->queue);
Sara Sharon344f8e02018-12-15 11:03:07 +02003290 if (!head || skb_is_gso(head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003291 goto out;
3292
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003293 orig_truesize = head->truesize;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003294 orig_len = head->len;
3295
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003296 if (skb->len + head->len > max_amsdu_len)
3297 goto out;
3298
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003299 nfrags = 1 + skb_shinfo(skb)->nr_frags;
3300 nfrags += 1 + skb_shinfo(head)->nr_frags;
3301 frag_tail = &skb_shinfo(head)->frag_list;
3302 while (*frag_tail) {
3303 nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3304 frag_tail = &(*frag_tail)->next;
3305 n++;
3306 }
3307
3308 if (max_subframes && n > max_subframes)
3309 goto out;
3310
3311 if (max_frags && nfrags > max_frags)
3312 goto out;
3313
Sara Sharon9739fe22018-09-05 08:06:11 +03003314 if (!drv_can_aggregate_in_amsdu(local, head, skb))
3315 goto out;
3316
Lorenzo Bianconi1eb50792018-08-29 21:03:25 +02003317 if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003318 goto out;
3319
Sara Sharon166ac9d2018-08-29 08:57:02 +02003320 /*
3321 * Pad out the previous subframe to a multiple of 4 by adding the
3322 * padding to the next one, that's being added. Note that head->len
3323 * is the length of the full A-MSDU, but that works since each time
3324 * we add a new subframe we pad out the previous one to a multiple
3325 * of 4 and thus it no longer matters in the next round.
3326 */
3327 hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3328 if ((head->len - hdrlen) & 3)
3329 pad = 4 - ((head->len - hdrlen) & 3);
3330
3331 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3332 2 + pad))
Johannes Bergaa58acf2018-08-30 10:55:49 +02003333 goto out_recalc;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003334
3335 ret = true;
3336 data = skb_push(skb, ETH_ALEN + 2);
3337 memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3338
3339 data += 2 * ETH_ALEN;
3340 len = cpu_to_be16(subframe_len);
3341 memcpy(data, &len, 2);
3342 memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3343
Sara Sharon166ac9d2018-08-29 08:57:02 +02003344 memset(skb_push(skb, pad), 0, pad);
3345
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003346 head->len += skb->len;
3347 head->data_len += skb->len;
3348 *frag_tail = skb;
3349
Johannes Bergaa58acf2018-08-30 10:55:49 +02003350out_recalc:
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003351 fq->memory_usage += head->truesize - orig_truesize;
Johannes Bergaa58acf2018-08-30 10:55:49 +02003352 if (head->len != orig_len) {
3353 flow->backlog += head->len - orig_len;
3354 tin->backlog_bytes += head->len - orig_len;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003355
Johannes Bergaa58acf2018-08-30 10:55:49 +02003356 fq_recalc_backlog(fq, tin, flow);
3357 }
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003358out:
Michal Kaziorfa962b92016-05-19 10:37:49 +02003359 spin_unlock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003360
3361 return ret;
3362}
3363
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003364/*
3365 * Can be called while the sta lock is held. Anything that can cause packets to
3366 * be generated will cause deadlock!
3367 */
3368static void ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3369 struct sta_info *sta, u8 pn_offs,
3370 struct ieee80211_key *key,
3371 struct sk_buff *skb)
3372{
3373 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3374 struct ieee80211_hdr *hdr = (void *)skb->data;
3375 u8 tid = IEEE80211_NUM_TIDS;
3376
3377 if (key)
3378 info->control.hw_key = &key->conf;
3379
3380 ieee80211_tx_stats(skb->dev, skb->len);
3381
3382 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3383 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003384 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3385 } else {
3386 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3387 hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3388 sdata->sequence_number += 0x10;
3389 }
3390
3391 if (skb_shinfo(skb)->gso_size)
3392 sta->tx_stats.msdu[tid] +=
3393 DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3394 else
3395 sta->tx_stats.msdu[tid]++;
3396
3397 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3398
3399 /* statistics normally done by ieee80211_tx_h_stats (but that
3400 * has to consider fragmentation, so is more complex)
3401 */
3402 sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3403 sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
3404
3405 if (pn_offs) {
3406 u64 pn;
3407 u8 *crypto_hdr = skb->data + pn_offs;
3408
3409 switch (key->conf.cipher) {
3410 case WLAN_CIPHER_SUITE_CCMP:
3411 case WLAN_CIPHER_SUITE_CCMP_256:
3412 case WLAN_CIPHER_SUITE_GCMP:
3413 case WLAN_CIPHER_SUITE_GCMP_256:
3414 pn = atomic64_inc_return(&key->conf.tx_pn);
3415 crypto_hdr[0] = pn;
3416 crypto_hdr[1] = pn >> 8;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003417 crypto_hdr[3] = 0x20 | (key->conf.keyidx << 6);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003418 crypto_hdr[4] = pn >> 16;
3419 crypto_hdr[5] = pn >> 24;
3420 crypto_hdr[6] = pn >> 32;
3421 crypto_hdr[7] = pn >> 40;
3422 break;
3423 }
3424 }
3425}
3426
Johannes Berg17c18bf2015-03-21 15:25:43 +01003427static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003428 struct sta_info *sta,
Johannes Berg17c18bf2015-03-21 15:25:43 +01003429 struct ieee80211_fast_tx *fast_tx,
3430 struct sk_buff *skb)
3431{
3432 struct ieee80211_local *local = sdata->local;
3433 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3434 int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3435 int hw_headroom = sdata->local->hw.extra_tx_headroom;
3436 struct ethhdr eth;
Johannes Berg35f432a2017-01-02 11:19:29 +01003437 struct ieee80211_tx_info *info;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003438 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3439 struct ieee80211_tx_data tx;
3440 ieee80211_tx_result r;
3441 struct tid_ampdu_tx *tid_tx = NULL;
3442 u8 tid = IEEE80211_NUM_TIDS;
3443
3444 /* control port protocol needs a lot of special handling */
3445 if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3446 return false;
3447
3448 /* only RFC 1042 SNAP */
3449 if (ethertype < ETH_P_802_3_MIN)
3450 return false;
3451
3452 /* don't handle TX status request here either */
3453 if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3454 return false;
3455
3456 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3457 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3458 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
Johannes Berg472be002015-06-09 16:45:08 +02003459 if (tid_tx) {
3460 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3461 return false;
3462 if (tid_tx->timeout)
3463 tid_tx->last_tx = jiffies;
3464 }
Johannes Berg17c18bf2015-03-21 15:25:43 +01003465 }
3466
3467 /* after this point (skb is modified) we cannot return false */
3468
3469 if (skb_shared(skb)) {
3470 struct sk_buff *tmp_skb = skb;
3471
3472 skb = skb_clone(skb, GFP_ATOMIC);
3473 kfree_skb(tmp_skb);
3474
3475 if (!skb)
3476 return true;
3477 }
3478
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003479 if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3480 ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
3481 return true;
3482
Johannes Berg17c18bf2015-03-21 15:25:43 +01003483 /* will not be crypto-handled beyond what we do here, so use false
3484 * as the may-encrypt argument for the resize to not account for
3485 * more room than we already have in 'extra_head'
3486 */
3487 if (unlikely(ieee80211_skb_resize(sdata, skb,
3488 max_t(int, extra_head + hw_headroom -
3489 skb_headroom(skb), 0),
3490 false))) {
3491 kfree_skb(skb);
3492 return true;
3493 }
3494
3495 memcpy(&eth, skb->data, ETH_HLEN - 2);
Johannes Bergd58ff352017-06-16 14:29:23 +02003496 hdr = skb_push(skb, extra_head);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003497 memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3498 memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
3499 memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
3500
Johannes Berg35f432a2017-01-02 11:19:29 +01003501 info = IEEE80211_SKB_CB(skb);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003502 memset(info, 0, sizeof(*info));
3503 info->band = fast_tx->band;
3504 info->control.vif = &sdata->vif;
3505 info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3506 IEEE80211_TX_CTL_DONTFRAG |
3507 (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003508 info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003509
Julius Niedworok276d9e82019-03-28 21:01:06 +01003510#ifdef CONFIG_MAC80211_DEBUGFS
3511 if (local->force_tx_status)
3512 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3513#endif
3514
Felix Fietkaua786f962016-11-04 10:27:54 +01003515 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3516 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3517 *ieee80211_get_qos_ctl(hdr) = tid;
3518 }
3519
Johannes Berg17c18bf2015-03-21 15:25:43 +01003520 __skb_queue_head_init(&tx.skbs);
3521
3522 tx.flags = IEEE80211_TX_UNICAST;
3523 tx.local = local;
3524 tx.sdata = sdata;
3525 tx.sta = sta;
3526 tx.key = fast_tx->key;
3527
Johannes Berg30686bf2015-06-02 21:39:54 +02003528 if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01003529 tx.skb = skb;
3530 r = ieee80211_tx_h_rate_ctrl(&tx);
3531 skb = tx.skb;
3532 tx.skb = NULL;
3533
3534 if (r != TX_CONTINUE) {
3535 if (r != TX_QUEUED)
3536 kfree_skb(skb);
3537 return true;
3538 }
3539 }
3540
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003541 if (ieee80211_queue_skb(local, sdata, sta, skb))
3542 return true;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003543
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003544 ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3545 fast_tx->key, skb);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003546
3547 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3548 sdata = container_of(sdata->bss,
3549 struct ieee80211_sub_if_data, u.ap);
3550
3551 __skb_queue_tail(&tx.skbs, skb);
Johannes Berg4fd03282019-10-01 23:26:35 +02003552 ieee80211_tx_frags(local, &sdata->vif, sta, &tx.skbs, false);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003553 return true;
3554}
3555
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003556struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3557 struct ieee80211_txq *txq)
3558{
3559 struct ieee80211_local *local = hw_to_local(hw);
3560 struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3561 struct ieee80211_hdr *hdr;
3562 struct sk_buff *skb = NULL;
3563 struct fq *fq = &local->fq;
3564 struct fq_tin *tin = &txqi->tin;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003565 struct ieee80211_tx_info *info;
3566 struct ieee80211_tx_data tx;
3567 ieee80211_tx_result r;
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303568 struct ieee80211_vif *vif = txq->vif;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003569
Erik Stromdahlfb0e76a2019-06-17 22:01:39 +02003570 WARN_ON_ONCE(softirq_count() == 0);
3571
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003572 if (!ieee80211_txq_airtime_check(hw, txq))
3573 return NULL;
3574
Felix Fietkauded46982019-03-16 18:06:33 +01003575begin:
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003576 spin_lock_bh(&fq->lock);
3577
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303578 if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags) ||
3579 test_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags))
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003580 goto out;
3581
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303582 if (vif->txqs_stopped[ieee80211_ac_from_tid(txq->tid)]) {
3583 set_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags);
3584 goto out;
3585 }
3586
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003587 /* Make sure fragments stay together. */
3588 skb = __skb_dequeue(&txqi->frags);
3589 if (skb)
3590 goto out;
3591
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003592 skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
3593 if (!skb)
3594 goto out;
3595
Felix Fietkauded46982019-03-16 18:06:33 +01003596 spin_unlock_bh(&fq->lock);
3597
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003598 hdr = (struct ieee80211_hdr *)skb->data;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003599 info = IEEE80211_SKB_CB(skb);
3600
3601 memset(&tx, 0, sizeof(tx));
3602 __skb_queue_head_init(&tx.skbs);
3603 tx.local = local;
3604 tx.skb = skb;
3605 tx.sdata = vif_to_sdata(info->control.vif);
3606
3607 if (txq->sta)
3608 tx.sta = container_of(txq->sta, struct sta_info, sta);
3609
3610 /*
3611 * The key can be removed while the packet was queued, so need to call
3612 * this here to get the current key.
3613 */
3614 r = ieee80211_tx_h_select_key(&tx);
3615 if (r != TX_CONTINUE) {
3616 ieee80211_free_txskb(&local->hw, skb);
3617 goto begin;
3618 }
3619
Felix Fietkauc1f4c9e2016-11-04 10:27:52 +01003620 if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
3621 info->flags |= IEEE80211_TX_CTL_AMPDU;
3622 else
3623 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
3624
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003625 if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003626 struct sta_info *sta = container_of(txq->sta, struct sta_info,
3627 sta);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003628 u8 pn_offs = 0;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003629
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003630 if (tx.key &&
3631 (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
3632 pn_offs = ieee80211_hdrlen(hdr->frame_control);
3633
3634 ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
3635 tx.key, skb);
3636 } else {
3637 if (invoke_tx_handlers_late(&tx))
3638 goto begin;
3639
3640 skb = __skb_dequeue(&tx.skbs);
3641
Felix Fietkauded46982019-03-16 18:06:33 +01003642 if (!skb_queue_empty(&tx.skbs)) {
3643 spin_lock_bh(&fq->lock);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003644 skb_queue_splice_tail(&tx.skbs, &txqi->frags);
Felix Fietkauded46982019-03-16 18:06:33 +01003645 spin_unlock_bh(&fq->lock);
3646 }
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003647 }
3648
Johannes Berg233e98d2018-12-15 11:03:09 +02003649 if (skb_has_frag_list(skb) &&
Johannes Berg1e1430d2016-10-04 09:22:19 +02003650 !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
3651 if (skb_linearize(skb)) {
3652 ieee80211_free_txskb(&local->hw, skb);
3653 goto begin;
3654 }
3655 }
3656
Johannes Berg53168212017-06-22 12:20:30 +02003657 switch (tx.sdata->vif.type) {
3658 case NL80211_IFTYPE_MONITOR:
3659 if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
3660 vif = &tx.sdata->vif;
3661 break;
3662 }
3663 tx.sdata = rcu_dereference(local->monitor_sdata);
3664 if (tx.sdata) {
3665 vif = &tx.sdata->vif;
3666 info->hw_queue =
3667 vif->hw_queue[skb_get_queue_mapping(skb)];
3668 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
3669 ieee80211_free_txskb(&local->hw, skb);
3670 goto begin;
3671 } else {
3672 vif = NULL;
3673 }
3674 break;
3675 case NL80211_IFTYPE_AP_VLAN:
3676 tx.sdata = container_of(tx.sdata->bss,
3677 struct ieee80211_sub_if_data, u.ap);
3678 /* fall through */
3679 default:
3680 vif = &tx.sdata->vif;
3681 break;
3682 }
3683
3684 IEEE80211_SKB_CB(skb)->control.vif = vif;
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003685
Toke Høiland-Jørgensen911bde02019-12-12 12:14:37 +01003686 if (wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) {
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003687 u32 airtime;
3688
3689 airtime = ieee80211_calc_expected_tx_airtime(hw, vif, txq->sta,
3690 skb->len);
3691 if (airtime) {
3692 airtime = ieee80211_info_set_tx_time_est(info, airtime);
3693 ieee80211_sta_update_pending_airtime(local, tx.sta,
3694 txq->ac,
3695 airtime,
3696 false);
3697 }
3698 }
3699
Felix Fietkauded46982019-03-16 18:06:33 +01003700 return skb;
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303701
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003702out:
3703 spin_unlock_bh(&fq->lock);
3704
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003705 return skb;
3706}
3707EXPORT_SYMBOL(ieee80211_tx_dequeue);
3708
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003709struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac)
3710{
3711 struct ieee80211_local *local = hw_to_local(hw);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003712 struct ieee80211_txq *ret = NULL;
Kan Yan3ace10f2019-11-18 22:06:09 -08003713 struct txq_info *txqi = NULL, *head = NULL;
3714 bool found_eligible_txq = false;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003715
Felix Fietkau5b989c12019-03-15 11:03:35 +01003716 spin_lock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003717
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003718 begin:
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003719 txqi = list_first_entry_or_null(&local->active_txqs[ac],
3720 struct txq_info,
3721 schedule_order);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003722 if (!txqi)
Felix Fietkau5b989c12019-03-15 11:03:35 +01003723 goto out;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003724
Kan Yan3ace10f2019-11-18 22:06:09 -08003725 if (txqi == head) {
3726 if (!found_eligible_txq)
3727 goto out;
3728 else
3729 found_eligible_txq = false;
3730 }
3731
3732 if (!head)
3733 head = txqi;
3734
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003735 if (txqi->txq.sta) {
3736 struct sta_info *sta = container_of(txqi->txq.sta,
Kan Yan3ace10f2019-11-18 22:06:09 -08003737 struct sta_info, sta);
3738 bool aql_check = ieee80211_txq_airtime_check(hw, &txqi->txq);
3739 s64 deficit = sta->airtime[txqi->txq.ac].deficit;
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003740
Kan Yan3ace10f2019-11-18 22:06:09 -08003741 if (aql_check)
3742 found_eligible_txq = true;
3743
3744 if (deficit < 0)
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003745 sta->airtime[txqi->txq.ac].deficit +=
3746 sta->airtime_weight;
Kan Yan3ace10f2019-11-18 22:06:09 -08003747
3748 if (deficit < 0 || !aql_check) {
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003749 list_move_tail(&txqi->schedule_order,
3750 &local->active_txqs[txqi->txq.ac]);
3751 goto begin;
3752 }
3753 }
3754
3755
3756 if (txqi->schedule_round == local->schedule_round[ac])
Felix Fietkau5b989c12019-03-15 11:03:35 +01003757 goto out;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003758
3759 list_del_init(&txqi->schedule_order);
3760 txqi->schedule_round = local->schedule_round[ac];
Felix Fietkau5b989c12019-03-15 11:03:35 +01003761 ret = &txqi->txq;
3762
3763out:
3764 spin_unlock_bh(&local->active_txq_lock[ac]);
3765 return ret;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003766}
3767EXPORT_SYMBOL(ieee80211_next_txq);
3768
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003769void __ieee80211_schedule_txq(struct ieee80211_hw *hw,
3770 struct ieee80211_txq *txq,
3771 bool force)
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003772{
3773 struct ieee80211_local *local = hw_to_local(hw);
3774 struct txq_info *txqi = to_txq_info(txq);
3775
Felix Fietkau5b989c12019-03-15 11:03:35 +01003776 spin_lock_bh(&local->active_txq_lock[txq->ac]);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003777
3778 if (list_empty(&txqi->schedule_order) &&
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003779 (force || !skb_queue_empty(&txqi->frags) ||
3780 txqi->tin.backlog_packets)) {
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003781 /* If airtime accounting is active, always enqueue STAs at the
3782 * head of the list to ensure that they only get moved to the
3783 * back by the airtime DRR scheduler once they have a negative
3784 * deficit. A station that already has a negative deficit will
3785 * get immediately moved to the back of the list on the next
3786 * call to ieee80211_next_txq().
3787 */
3788 if (txqi->txq.sta &&
3789 wiphy_ext_feature_isset(local->hw.wiphy,
3790 NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
3791 list_add(&txqi->schedule_order,
3792 &local->active_txqs[txq->ac]);
3793 else
3794 list_add_tail(&txqi->schedule_order,
3795 &local->active_txqs[txq->ac]);
3796 }
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003797
Toke Høiland-Jørgensen390298e2019-01-22 15:20:16 +01003798 spin_unlock_bh(&local->active_txq_lock[txq->ac]);
3799}
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003800EXPORT_SYMBOL(__ieee80211_schedule_txq);
Toke Høiland-Jørgensen390298e2019-01-22 15:20:16 +01003801
Kan Yan3ace10f2019-11-18 22:06:09 -08003802bool ieee80211_txq_airtime_check(struct ieee80211_hw *hw,
3803 struct ieee80211_txq *txq)
3804{
3805 struct sta_info *sta;
3806 struct ieee80211_local *local = hw_to_local(hw);
3807
Toke Høiland-Jørgensen911bde02019-12-12 12:14:37 +01003808 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
Kan Yan3ace10f2019-11-18 22:06:09 -08003809 return true;
3810
3811 if (!txq->sta)
3812 return true;
3813
3814 sta = container_of(txq->sta, struct sta_info, sta);
3815 if (atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
3816 sta->airtime[txq->ac].aql_limit_low)
3817 return true;
3818
3819 if (atomic_read(&local->aql_total_pending_airtime) <
3820 local->aql_threshold &&
3821 atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
3822 sta->airtime[txq->ac].aql_limit_high)
3823 return true;
3824
3825 return false;
3826}
3827EXPORT_SYMBOL(ieee80211_txq_airtime_check);
3828
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003829bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
3830 struct ieee80211_txq *txq)
3831{
3832 struct ieee80211_local *local = hw_to_local(hw);
3833 struct txq_info *iter, *tmp, *txqi = to_txq_info(txq);
3834 struct sta_info *sta;
3835 u8 ac = txq->ac;
3836
Felix Fietkau5b989c12019-03-15 11:03:35 +01003837 spin_lock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003838
3839 if (!txqi->txq.sta)
3840 goto out;
3841
3842 if (list_empty(&txqi->schedule_order))
3843 goto out;
3844
3845 list_for_each_entry_safe(iter, tmp, &local->active_txqs[ac],
3846 schedule_order) {
3847 if (iter == txqi)
3848 break;
3849
3850 if (!iter->txq.sta) {
3851 list_move_tail(&iter->schedule_order,
3852 &local->active_txqs[ac]);
3853 continue;
3854 }
3855 sta = container_of(iter->txq.sta, struct sta_info, sta);
3856 if (sta->airtime[ac].deficit < 0)
3857 sta->airtime[ac].deficit += sta->airtime_weight;
3858 list_move_tail(&iter->schedule_order, &local->active_txqs[ac]);
3859 }
3860
3861 sta = container_of(txqi->txq.sta, struct sta_info, sta);
3862 if (sta->airtime[ac].deficit >= 0)
3863 goto out;
3864
3865 sta->airtime[ac].deficit += sta->airtime_weight;
3866 list_move_tail(&txqi->schedule_order, &local->active_txqs[ac]);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003867 spin_unlock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003868
3869 return false;
3870out:
3871 if (!list_empty(&txqi->schedule_order))
3872 list_del_init(&txqi->schedule_order);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003873 spin_unlock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003874
3875 return true;
3876}
3877EXPORT_SYMBOL(ieee80211_txq_may_transmit);
3878
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003879void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003880{
3881 struct ieee80211_local *local = hw_to_local(hw);
3882
3883 spin_lock_bh(&local->active_txq_lock[ac]);
3884 local->schedule_round[ac]++;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003885 spin_unlock_bh(&local->active_txq_lock[ac]);
3886}
Felix Fietkau5b989c12019-03-15 11:03:35 +01003887EXPORT_SYMBOL(ieee80211_txq_schedule_start);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003888
Johannes Berg4c9451e2014-11-09 18:50:10 +02003889void __ieee80211_subif_start_xmit(struct sk_buff *skb,
3890 struct net_device *dev,
Rajkumar Manoharan06016772019-04-11 13:47:25 -07003891 u32 info_flags,
3892 u32 ctrl_flags)
Johannes Berg4c9451e2014-11-09 18:50:10 +02003893{
3894 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Felix Fietkau1974da82019-03-25 08:59:23 +01003895 struct ieee80211_local *local = sdata->local;
Johannes Berg97ffe752015-03-21 09:13:45 +01003896 struct sta_info *sta;
Johannes Berg80616c02015-04-14 14:50:41 +02003897 struct sk_buff *next;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003898
3899 if (unlikely(skb->len < ETH_HLEN)) {
3900 kfree_skb(skb);
3901 return;
3902 }
3903
3904 rcu_read_lock();
3905
Johannes Berg2d981fd2015-04-10 14:10:10 +02003906 if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
3907 goto out_free;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003908
Felix Fietkau1974da82019-03-25 08:59:23 +01003909 if (IS_ERR(sta))
3910 sta = NULL;
3911
3912 if (local->ops->wake_tx_queue) {
3913 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
3914 skb_set_queue_mapping(skb, queue);
3915 }
3916
3917 if (sta) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01003918 struct ieee80211_fast_tx *fast_tx;
3919
Wen Gong70e53662018-08-08 18:40:01 +08003920 sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
Toke Høiland-Jørgensen36148c22018-02-02 16:11:05 +01003921
Johannes Berg17c18bf2015-03-21 15:25:43 +01003922 fast_tx = rcu_dereference(sta->fast_tx);
3923
3924 if (fast_tx &&
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003925 ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
Johannes Berg17c18bf2015-03-21 15:25:43 +01003926 goto out;
3927 }
3928
Johannes Berg80616c02015-04-14 14:50:41 +02003929 if (skb_is_gso(skb)) {
3930 struct sk_buff *segs;
Johannes Berg680a0da2015-04-13 16:58:25 +02003931
Johannes Berg80616c02015-04-14 14:50:41 +02003932 segs = skb_gso_segment(skb, 0);
3933 if (IS_ERR(segs)) {
Johannes Berg2d981fd2015-04-10 14:10:10 +02003934 goto out_free;
Johannes Berg80616c02015-04-14 14:50:41 +02003935 } else if (segs) {
3936 consume_skb(skb);
3937 skb = segs;
3938 }
3939 } else {
3940 /* we cannot process non-linear frames on this path */
3941 if (skb_linearize(skb)) {
3942 kfree_skb(skb);
3943 goto out;
3944 }
3945
3946 /* the frame could be fragmented, software-encrypted, and other
3947 * things so we cannot really handle checksum offload with it -
3948 * fix it up in software before we handle anything else.
3949 */
3950 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Johannes Bergf603f1f2015-05-05 15:25:33 +02003951 skb_set_transport_header(skb,
3952 skb_checksum_start_offset(skb));
Johannes Berg80616c02015-04-14 14:50:41 +02003953 if (skb_checksum_help(skb))
3954 goto out_free;
3955 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02003956 }
3957
Jason A. Donenfeld9f3ef3d2020-01-13 18:42:33 -05003958 skb_list_walk_safe(skb, skb, next) {
3959 skb_mark_not_on_list(skb);
Johannes Berg4c9451e2014-11-09 18:50:10 +02003960
Rajkumar Manoharan06016772019-04-11 13:47:25 -07003961 skb = ieee80211_build_hdr(sdata, skb, info_flags,
3962 sta, ctrl_flags);
Jason A. Donenfeld9f3ef3d2020-01-13 18:42:33 -05003963 if (IS_ERR(skb)) {
3964 kfree_skb_list(next);
Johannes Berg80616c02015-04-14 14:50:41 +02003965 goto out;
Jason A. Donenfeld9f3ef3d2020-01-13 18:42:33 -05003966 }
Johannes Berg80616c02015-04-14 14:50:41 +02003967
Johannes Berg5a490512015-04-22 17:10:38 +02003968 ieee80211_tx_stats(dev, skb->len);
Johannes Berg80616c02015-04-14 14:50:41 +02003969
Johannes Bergb9771d42018-05-28 15:47:41 +02003970 ieee80211_xmit(sdata, sta, skb, 0);
Johannes Berg80616c02015-04-14 14:50:41 +02003971 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02003972 goto out;
3973 out_free:
3974 kfree_skb(skb);
Johannes Berg4c9451e2014-11-09 18:50:10 +02003975 out:
3976 rcu_read_unlock();
3977}
3978
Michael Braunebceec82016-11-22 11:52:18 +01003979static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
3980{
3981 struct ethhdr *eth;
3982 int err;
3983
3984 err = skb_ensure_writable(skb, ETH_HLEN);
3985 if (unlikely(err))
3986 return err;
3987
3988 eth = (void *)skb->data;
3989 ether_addr_copy(eth->h_dest, sta->sta.addr);
3990
3991 return 0;
3992}
3993
3994static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
3995 struct net_device *dev)
3996{
3997 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3998 const struct ethhdr *eth = (void *)skb->data;
3999 const struct vlan_ethhdr *ethvlan = (void *)skb->data;
4000 __be16 ethertype;
4001
4002 if (likely(!is_multicast_ether_addr(eth->h_dest)))
4003 return false;
4004
4005 switch (sdata->vif.type) {
4006 case NL80211_IFTYPE_AP_VLAN:
4007 if (sdata->u.vlan.sta)
4008 return false;
4009 if (sdata->wdev.use_4addr)
4010 return false;
4011 /* fall through */
4012 case NL80211_IFTYPE_AP:
4013 /* check runtime toggle for this bss */
4014 if (!sdata->bss->multicast_to_unicast)
4015 return false;
4016 break;
4017 default:
4018 return false;
4019 }
4020
4021 /* multicast to unicast conversion only for some payload */
4022 ethertype = eth->h_proto;
4023 if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
4024 ethertype = ethvlan->h_vlan_encapsulated_proto;
4025 switch (ethertype) {
4026 case htons(ETH_P_ARP):
4027 case htons(ETH_P_IP):
4028 case htons(ETH_P_IPV6):
4029 break;
4030 default:
4031 return false;
4032 }
4033
4034 return true;
4035}
4036
4037static void
4038ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
4039 struct sk_buff_head *queue)
4040{
4041 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4042 struct ieee80211_local *local = sdata->local;
4043 const struct ethhdr *eth = (struct ethhdr *)skb->data;
4044 struct sta_info *sta, *first = NULL;
4045 struct sk_buff *cloned_skb;
4046
4047 rcu_read_lock();
4048
4049 list_for_each_entry_rcu(sta, &local->sta_list, list) {
4050 if (sdata != sta->sdata)
4051 /* AP-VLAN mismatch */
4052 continue;
4053 if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
4054 /* do not send back to source */
4055 continue;
4056 if (!first) {
4057 first = sta;
4058 continue;
4059 }
4060 cloned_skb = skb_clone(skb, GFP_ATOMIC);
4061 if (!cloned_skb)
4062 goto multicast;
4063 if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
4064 dev_kfree_skb(cloned_skb);
4065 goto multicast;
4066 }
4067 __skb_queue_tail(queue, cloned_skb);
4068 }
4069
4070 if (likely(first)) {
4071 if (unlikely(ieee80211_change_da(skb, first)))
4072 goto multicast;
4073 __skb_queue_tail(queue, skb);
4074 } else {
4075 /* no STA connected, drop */
4076 kfree_skb(skb);
4077 skb = NULL;
4078 }
4079
4080 goto out;
4081multicast:
4082 __skb_queue_purge(queue);
4083 __skb_queue_tail(queue, skb);
4084out:
4085 rcu_read_unlock();
4086}
4087
Johannes Berg4c9451e2014-11-09 18:50:10 +02004088/**
4089 * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
4090 * @skb: packet to be sent
4091 * @dev: incoming interface
4092 *
4093 * On failure skb will be freed.
4094 */
Liad Kaufman24d342c2014-11-09 18:50:07 +02004095netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
4096 struct net_device *dev)
4097{
Michael Braunebceec82016-11-22 11:52:18 +01004098 if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
4099 struct sk_buff_head queue;
4100
4101 __skb_queue_head_init(&queue);
4102 ieee80211_convert_to_unicast(skb, dev, &queue);
4103 while ((skb = __skb_dequeue(&queue)))
Rajkumar Manoharan06016772019-04-11 13:47:25 -07004104 __ieee80211_subif_start_xmit(skb, dev, 0, 0);
Michael Braunebceec82016-11-22 11:52:18 +01004105 } else {
Rajkumar Manoharan06016772019-04-11 13:47:25 -07004106 __ieee80211_subif_start_xmit(skb, dev, 0, 0);
Michael Braunebceec82016-11-22 11:52:18 +01004107 }
4108
Liad Kaufman24d342c2014-11-09 18:50:07 +02004109 return NETDEV_TX_OK;
4110}
Johannes Berge2ebc742007-07-27 15:43:22 +02004111
Johannes Berg7528ec52014-11-09 18:50:11 +02004112struct sk_buff *
4113ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
4114 struct sk_buff *skb, u32 info_flags)
4115{
4116 struct ieee80211_hdr *hdr;
4117 struct ieee80211_tx_data tx = {
4118 .local = sdata->local,
4119 .sdata = sdata,
4120 };
Johannes Berg97ffe752015-03-21 09:13:45 +01004121 struct sta_info *sta;
Johannes Berg7528ec52014-11-09 18:50:11 +02004122
4123 rcu_read_lock();
4124
Johannes Berg97ffe752015-03-21 09:13:45 +01004125 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4126 kfree_skb(skb);
4127 skb = ERR_PTR(-EINVAL);
4128 goto out;
4129 }
4130
Rajkumar Manoharan06016772019-04-11 13:47:25 -07004131 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta, 0);
Johannes Berg7528ec52014-11-09 18:50:11 +02004132 if (IS_ERR(skb))
4133 goto out;
4134
4135 hdr = (void *)skb->data;
4136 tx.sta = sta_info_get(sdata, hdr->addr1);
4137 tx.skb = skb;
4138
4139 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
4140 rcu_read_unlock();
4141 kfree_skb(skb);
4142 return ERR_PTR(-EINVAL);
4143 }
4144
4145out:
4146 rcu_read_unlock();
4147 return skb;
4148}
4149
Johannes Berge2530082008-05-17 00:57:14 +02004150/*
4151 * ieee80211_clear_tx_pending may not be called in a context where
4152 * it is possible that it packets could come in again.
4153 */
Johannes Berge2ebc742007-07-27 15:43:22 +02004154void ieee80211_clear_tx_pending(struct ieee80211_local *local)
4155{
Felix Fietkau1f98ab72012-11-10 03:44:14 +01004156 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01004157 int i;
Johannes Berge2ebc742007-07-27 15:43:22 +02004158
Felix Fietkau1f98ab72012-11-10 03:44:14 +01004159 for (i = 0; i < local->hw.queues; i++) {
4160 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
4161 ieee80211_free_txskb(&local->hw, skb);
4162 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004163}
4164
Johannes Berg7bb45682011-02-24 14:42:06 +01004165/*
4166 * Returns false if the frame couldn't be transmitted but was queued instead,
4167 * which in this case means re-queued -- take as an indication to stop sending
4168 * more pending frames.
4169 */
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004170static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
4171 struct sk_buff *skb)
4172{
4173 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4174 struct ieee80211_sub_if_data *sdata;
4175 struct sta_info *sta;
4176 struct ieee80211_hdr *hdr;
Johannes Berg7bb45682011-02-24 14:42:06 +01004177 bool result;
Johannes Berg55de9082012-07-26 17:24:39 +02004178 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004179
Johannes Berg5061b0c2009-07-14 00:33:34 +02004180 sdata = vif_to_sdata(info->control.vif);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004181
4182 if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
Johannes Berg55de9082012-07-26 17:24:39 +02004183 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4184 if (unlikely(!chanctx_conf)) {
4185 dev_kfree_skb(skb);
4186 return true;
4187 }
Johannes Berg73c4e192014-11-09 18:50:09 +02004188 info->band = chanctx_conf->def.chan->band;
Johannes Bergb9771d42018-05-28 15:47:41 +02004189 result = ieee80211_tx(sdata, NULL, skb, true, 0);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004190 } else {
Johannes Berg252b86c2011-11-16 15:28:55 +01004191 struct sk_buff_head skbs;
4192
4193 __skb_queue_head_init(&skbs);
4194 __skb_queue_tail(&skbs, skb);
4195
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004196 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Bergabe60632009-11-25 17:46:18 +01004197 sta = sta_info_get(sdata, hdr->addr1);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004198
Johannes Berg74e4dbf2011-11-16 15:28:57 +01004199 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004200 }
4201
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004202 return result;
4203}
4204
Johannes Berge2530082008-05-17 00:57:14 +02004205/*
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004206 * Transmit all pending packets. Called from tasklet.
Johannes Berge2530082008-05-17 00:57:14 +02004207 */
Johannes Berge2ebc742007-07-27 15:43:22 +02004208void ieee80211_tx_pending(unsigned long data)
4209{
4210 struct ieee80211_local *local = (struct ieee80211_local *)data;
Johannes Berg2a577d92009-03-23 17:28:37 +01004211 unsigned long flags;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004212 int i;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004213 bool txok;
Johannes Berge2ebc742007-07-27 15:43:22 +02004214
Johannes Bergf0e72852009-03-23 17:28:36 +01004215 rcu_read_lock();
Johannes Berg2a577d92009-03-23 17:28:37 +01004216
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004217 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg176be722009-03-12 23:49:28 +01004218 for (i = 0; i < local->hw.queues; i++) {
Johannes Berg2a577d92009-03-23 17:28:37 +01004219 /*
4220 * If queue is stopped by something other than due to pending
4221 * frames, or we have no pending frames, proceed to next queue.
4222 */
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004223 if (local->queue_stop_reasons[i] ||
Johannes Berg2a577d92009-03-23 17:28:37 +01004224 skb_queue_empty(&local->pending[i]))
Johannes Berge2ebc742007-07-27 15:43:22 +02004225 continue;
Johannes Berge2530082008-05-17 00:57:14 +02004226
Johannes Berg2a577d92009-03-23 17:28:37 +01004227 while (!skb_queue_empty(&local->pending[i])) {
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004228 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
Johannes Berg5061b0c2009-07-14 00:33:34 +02004229 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg5061b0c2009-07-14 00:33:34 +02004230
Johannes Berga7bc3762009-07-27 10:33:31 +02004231 if (WARN_ON(!info->control.vif)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02004232 ieee80211_free_txskb(&local->hw, skb);
Johannes Berga7bc3762009-07-27 10:33:31 +02004233 continue;
4234 }
4235
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004236 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
4237 flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01004238
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004239 txok = ieee80211_tx_pending_skb(local, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004240 spin_lock_irqsave(&local->queue_stop_reason_lock,
4241 flags);
4242 if (!txok)
Johannes Berg2a577d92009-03-23 17:28:37 +01004243 break;
Johannes Berge2ebc742007-07-27 15:43:22 +02004244 }
Johannes Berg7236fe22010-03-22 13:42:43 -07004245
4246 if (skb_queue_empty(&local->pending[i]))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02004247 ieee80211_propagate_queue_wake(local, i);
Johannes Berge2ebc742007-07-27 15:43:22 +02004248 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004249 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01004250
Johannes Bergf0e72852009-03-23 17:28:36 +01004251 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004252}
4253
4254/* functions for drivers to get certain frames */
4255
Marco Porscheac70c12013-01-07 16:04:50 +01004256static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004257 struct ps_data *ps, struct sk_buff *skb,
4258 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02004259{
4260 u8 *pos, *tim;
4261 int aid0 = 0;
4262 int i, have_bits = 0, n1, n2;
4263
4264 /* Generate bitmap for TIM only if there are any STAs in power save
4265 * mode. */
Marco Porschd012a602012-10-10 12:39:50 -07004266 if (atomic_read(&ps->num_sta_ps) > 0)
Johannes Berge2ebc742007-07-27 15:43:22 +02004267 /* in the hope that this is faster than
4268 * checking byte-for-byte */
Weilong Chenf359d3f2013-12-18 15:44:16 +08004269 have_bits = !bitmap_empty((unsigned long *)ps->tim,
Johannes Berge2ebc742007-07-27 15:43:22 +02004270 IEEE80211_MAX_AID+1);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004271 if (!is_template) {
4272 if (ps->dtim_count == 0)
4273 ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
4274 else
4275 ps->dtim_count--;
4276 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004277
Johannes Berg4df864c2017-06-16 14:29:21 +02004278 tim = pos = skb_put(skb, 6);
Johannes Berge2ebc742007-07-27 15:43:22 +02004279 *pos++ = WLAN_EID_TIM;
4280 *pos++ = 4;
Marco Porschd012a602012-10-10 12:39:50 -07004281 *pos++ = ps->dtim_count;
Johannes Berg88600202012-02-13 15:17:18 +01004282 *pos++ = sdata->vif.bss_conf.dtim_period;
Johannes Berge2ebc742007-07-27 15:43:22 +02004283
Marco Porschd012a602012-10-10 12:39:50 -07004284 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
Johannes Berge2ebc742007-07-27 15:43:22 +02004285 aid0 = 1;
4286
Marco Porschd012a602012-10-10 12:39:50 -07004287 ps->dtim_bc_mc = aid0 == 1;
Christian Lamparter512119b2011-01-31 20:48:44 +02004288
Johannes Berge2ebc742007-07-27 15:43:22 +02004289 if (have_bits) {
4290 /* Find largest even number N1 so that bits numbered 1 through
4291 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
4292 * (N2 + 1) x 8 through 2007 are 0. */
4293 n1 = 0;
4294 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
Marco Porschd012a602012-10-10 12:39:50 -07004295 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004296 n1 = i & 0xfe;
4297 break;
4298 }
4299 }
4300 n2 = n1;
4301 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
Marco Porschd012a602012-10-10 12:39:50 -07004302 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004303 n2 = i;
4304 break;
4305 }
4306 }
4307
4308 /* Bitmap control */
4309 *pos++ = n1 | aid0;
4310 /* Part Virt Bitmap */
Eliad Peller5220da32011-11-24 16:50:00 +02004311 skb_put(skb, n2 - n1);
Marco Porschd012a602012-10-10 12:39:50 -07004312 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
Johannes Berge2ebc742007-07-27 15:43:22 +02004313
4314 tim[1] = n2 - n1 + 4;
Johannes Berge2ebc742007-07-27 15:43:22 +02004315 } else {
4316 *pos++ = aid0; /* Bitmap control */
4317 *pos++ = 0; /* Part Virt Bitmap */
4318 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004319}
4320
Marco Porscheac70c12013-01-07 16:04:50 +01004321static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004322 struct ps_data *ps, struct sk_buff *skb,
4323 bool is_template)
Marco Porscheac70c12013-01-07 16:04:50 +01004324{
4325 struct ieee80211_local *local = sdata->local;
4326
4327 /*
4328 * Not very nice, but we want to allow the driver to call
4329 * ieee80211_beacon_get() as a response to the set_tim()
4330 * callback. That, however, is already invoked under the
4331 * sta_lock to guarantee consistent and race-free update
4332 * of the tim bitmap in mac80211 and the driver.
4333 */
4334 if (local->tim_in_locked_section) {
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004335 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Marco Porscheac70c12013-01-07 16:04:50 +01004336 } else {
Johannes Berg1b917312013-02-22 12:55:01 +01004337 spin_lock_bh(&local->tim_lock);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004338 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Johannes Berg1b917312013-02-22 12:55:01 +01004339 spin_unlock_bh(&local->tim_lock);
Marco Porscheac70c12013-01-07 16:04:50 +01004340 }
4341
4342 return 0;
4343}
4344
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004345static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
4346 struct beacon_data *beacon)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004347{
4348 struct probe_resp *resp;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004349 u8 *beacon_data;
4350 size_t beacon_data_len;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004351 int i;
Michal Kazioraf296bd2014-06-05 14:21:36 +02004352 u8 count = beacon->csa_current_counter;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004353
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004354 switch (sdata->vif.type) {
4355 case NL80211_IFTYPE_AP:
4356 beacon_data = beacon->tail;
4357 beacon_data_len = beacon->tail_len;
4358 break;
4359 case NL80211_IFTYPE_ADHOC:
4360 beacon_data = beacon->head;
4361 beacon_data_len = beacon->head_len;
4362 break;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004363 case NL80211_IFTYPE_MESH_POINT:
4364 beacon_data = beacon->head;
4365 beacon_data_len = beacon->head_len;
4366 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004367 default:
4368 return;
4369 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004370
Michal Kazioraf296bd2014-06-05 14:21:36 +02004371 rcu_read_lock();
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004372 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004373 resp = rcu_dereference(sdata->u.ap.probe_resp);
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004374
Michal Kazioraf296bd2014-06-05 14:21:36 +02004375 if (beacon->csa_counter_offsets[i]) {
4376 if (WARN_ON_ONCE(beacon->csa_counter_offsets[i] >=
4377 beacon_data_len)) {
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004378 rcu_read_unlock();
4379 return;
4380 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004381
4382 beacon_data[beacon->csa_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004383 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004384
4385 if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
4386 resp->data[resp->csa_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004387 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004388 rcu_read_unlock();
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004389}
4390
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004391static u8 __ieee80211_csa_update_counter(struct beacon_data *beacon)
4392{
4393 beacon->csa_current_counter--;
4394
4395 /* the counter should never reach 0 */
4396 WARN_ON_ONCE(!beacon->csa_current_counter);
4397
4398 return beacon->csa_current_counter;
4399}
4400
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004401u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
4402{
4403 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004404 struct beacon_data *beacon = NULL;
4405 u8 count = 0;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004406
Michal Kazioraf296bd2014-06-05 14:21:36 +02004407 rcu_read_lock();
4408
4409 if (sdata->vif.type == NL80211_IFTYPE_AP)
4410 beacon = rcu_dereference(sdata->u.ap.beacon);
4411 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4412 beacon = rcu_dereference(sdata->u.ibss.presp);
4413 else if (ieee80211_vif_is_mesh(&sdata->vif))
4414 beacon = rcu_dereference(sdata->u.mesh.beacon);
4415
4416 if (!beacon)
4417 goto unlock;
4418
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004419 count = __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004420
Michal Kazioraf296bd2014-06-05 14:21:36 +02004421unlock:
4422 rcu_read_unlock();
4423 return count;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004424}
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004425EXPORT_SYMBOL(ieee80211_csa_update_counter);
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004426
Gregory Greenman03737002018-04-20 13:49:24 +03004427void ieee80211_csa_set_counter(struct ieee80211_vif *vif, u8 counter)
4428{
4429 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4430 struct beacon_data *beacon = NULL;
4431
4432 rcu_read_lock();
4433
4434 if (sdata->vif.type == NL80211_IFTYPE_AP)
4435 beacon = rcu_dereference(sdata->u.ap.beacon);
4436 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4437 beacon = rcu_dereference(sdata->u.ibss.presp);
4438 else if (ieee80211_vif_is_mesh(&sdata->vif))
4439 beacon = rcu_dereference(sdata->u.mesh.beacon);
4440
4441 if (!beacon)
4442 goto unlock;
4443
4444 if (counter < beacon->csa_current_counter)
4445 beacon->csa_current_counter = counter;
4446
4447unlock:
4448 rcu_read_unlock();
4449}
4450EXPORT_SYMBOL(ieee80211_csa_set_counter);
4451
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004452bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
4453{
4454 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4455 struct beacon_data *beacon = NULL;
4456 u8 *beacon_data;
4457 size_t beacon_data_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004458 int ret = false;
4459
4460 if (!ieee80211_sdata_running(sdata))
4461 return false;
4462
4463 rcu_read_lock();
4464 if (vif->type == NL80211_IFTYPE_AP) {
4465 struct ieee80211_if_ap *ap = &sdata->u.ap;
4466
4467 beacon = rcu_dereference(ap->beacon);
4468 if (WARN_ON(!beacon || !beacon->tail))
4469 goto out;
4470 beacon_data = beacon->tail;
4471 beacon_data_len = beacon->tail_len;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004472 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
4473 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4474
4475 beacon = rcu_dereference(ifibss->presp);
4476 if (!beacon)
4477 goto out;
4478
4479 beacon_data = beacon->head;
4480 beacon_data_len = beacon->head_len;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004481 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
4482 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4483
4484 beacon = rcu_dereference(ifmsh->beacon);
4485 if (!beacon)
4486 goto out;
4487
4488 beacon_data = beacon->head;
4489 beacon_data_len = beacon->head_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004490 } else {
4491 WARN_ON(1);
4492 goto out;
4493 }
4494
Michal Kazior10d78f22014-06-05 14:21:37 +02004495 if (!beacon->csa_counter_offsets[0])
4496 goto out;
4497
Michal Kazioraf296bd2014-06-05 14:21:36 +02004498 if (WARN_ON_ONCE(beacon->csa_counter_offsets[0] > beacon_data_len))
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004499 goto out;
4500
Michal Kazioraf296bd2014-06-05 14:21:36 +02004501 if (beacon_data[beacon->csa_counter_offsets[0]] == 1)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004502 ret = true;
4503 out:
4504 rcu_read_unlock();
4505
4506 return ret;
4507}
4508EXPORT_SYMBOL(ieee80211_csa_is_complete);
4509
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004510static struct sk_buff *
4511__ieee80211_beacon_get(struct ieee80211_hw *hw,
4512 struct ieee80211_vif *vif,
4513 struct ieee80211_mutable_offsets *offs,
4514 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02004515{
4516 struct ieee80211_local *local = hw_to_local(hw);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004517 struct beacon_data *beacon = NULL;
Johannes Berg9d139c82008-07-09 14:40:37 +02004518 struct sk_buff *skb = NULL;
Johannes Berge039fa42008-05-15 12:55:29 +02004519 struct ieee80211_tx_info *info;
Johannes Berge2ebc742007-07-27 15:43:22 +02004520 struct ieee80211_sub_if_data *sdata = NULL;
Johannes Berg57fbcce2016-04-12 15:56:15 +02004521 enum nl80211_band band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004522 struct ieee80211_tx_rate_control txrc;
Johannes Berg55de9082012-07-26 17:24:39 +02004523 struct ieee80211_chanctx_conf *chanctx_conf;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004524 int csa_off_base = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004525
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004526 rcu_read_lock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004527
Johannes Berg32bfd352007-12-19 01:31:26 +01004528 sdata = vif_to_sdata(vif);
Johannes Berg55de9082012-07-26 17:24:39 +02004529 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berge2ebc742007-07-27 15:43:22 +02004530
Johannes Berg55de9082012-07-26 17:24:39 +02004531 if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
Felix Fietkaueb3e5542011-01-24 19:28:49 +01004532 goto out;
4533
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004534 if (offs)
4535 memset(offs, 0, sizeof(*offs));
Johannes Bergeddcbb942009-10-29 08:30:35 +01004536
Johannes Berg05c914f2008-09-11 00:01:58 +02004537 if (sdata->vif.type == NL80211_IFTYPE_AP) {
Marco Porschd012a602012-10-10 12:39:50 -07004538 struct ieee80211_if_ap *ap = &sdata->u.ap;
Marco Porschd012a602012-10-10 12:39:50 -07004539
Michal Kazioraf296bd2014-06-05 14:21:36 +02004540 beacon = rcu_dereference(ap->beacon);
Dan Carpenter3ad97fb2011-02-07 22:03:35 +03004541 if (beacon) {
Michal Kazior10d78f22014-06-05 14:21:37 +02004542 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004543 if (!is_template)
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004544 __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004545
4546 ieee80211_set_csa(sdata, beacon);
4547 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004548
Johannes Berg902acc72008-02-23 15:17:19 +01004549 /*
4550 * headroom, head length,
4551 * tail length and maximum TIM length
4552 */
4553 skb = dev_alloc_skb(local->tx_headroom +
4554 beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004555 beacon->tail_len + 256 +
4556 local->hw.extra_beacon_tailroom);
Johannes Berg902acc72008-02-23 15:17:19 +01004557 if (!skb)
4558 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004559
Johannes Berg902acc72008-02-23 15:17:19 +01004560 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004561 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg902acc72008-02-23 15:17:19 +01004562
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004563 ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
4564 is_template);
Johannes Berg902acc72008-02-23 15:17:19 +01004565
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004566 if (offs) {
4567 offs->tim_offset = beacon->head_len;
4568 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004569
4570 /* for AP the csa offsets are from tail */
4571 csa_off_base = skb->len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004572 }
Johannes Bergeddcbb942009-10-29 08:30:35 +01004573
Johannes Berg902acc72008-02-23 15:17:19 +01004574 if (beacon->tail)
Johannes Berg59ae1d12017-06-16 14:29:20 +02004575 skb_put_data(skb, beacon->tail,
4576 beacon->tail_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004577 } else
4578 goto out;
Johannes Berg05c914f2008-09-11 00:01:58 +02004579 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Berg46900292009-02-15 12:44:28 +01004580 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg9d139c82008-07-09 14:40:37 +02004581 struct ieee80211_hdr *hdr;
Johannes Berg902acc72008-02-23 15:17:19 +01004582
Michal Kazioraf296bd2014-06-05 14:21:36 +02004583 beacon = rcu_dereference(ifibss->presp);
4584 if (!beacon)
Johannes Berg9d139c82008-07-09 14:40:37 +02004585 goto out;
4586
Michal Kazior10d78f22014-06-05 14:21:37 +02004587 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004588 if (!is_template)
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004589 __ieee80211_csa_update_counter(beacon);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004590
Michal Kazioraf296bd2014-06-05 14:21:36 +02004591 ieee80211_set_csa(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004592 }
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004593
Michal Kazioraf296bd2014-06-05 14:21:36 +02004594 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004595 local->hw.extra_beacon_tailroom);
Johannes Berg9d139c82008-07-09 14:40:37 +02004596 if (!skb)
4597 goto out;
Johannes Bergc3ffeab2013-03-07 20:54:29 +01004598 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004599 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004600
4601 hdr = (struct ieee80211_hdr *) skb->data;
Harvey Harrisone7827a72008-07-15 18:44:13 -07004602 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4603 IEEE80211_STYPE_BEACON);
Johannes Berg902acc72008-02-23 15:17:19 +01004604 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
Javier Cardonadbf498f2012-03-31 11:31:32 -07004605 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg472dbc42008-09-11 00:01:49 +02004606
Michal Kazioraf296bd2014-06-05 14:21:36 +02004607 beacon = rcu_dereference(ifmsh->beacon);
4608 if (!beacon)
Johannes Bergac1bd842011-01-18 13:45:32 +01004609 goto out;
Johannes Bergac1bd842011-01-18 13:45:32 +01004610
Michal Kazior10d78f22014-06-05 14:21:37 +02004611 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004612 if (!is_template)
4613 /* TODO: For mesh csa_counter is in TU, so
4614 * decrementing it by one isn't correct, but
4615 * for now we leave it consistent with overall
4616 * mac80211's behavior.
4617 */
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004618 __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004619
Michal Kazioraf296bd2014-06-05 14:21:36 +02004620 ieee80211_set_csa(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004621 }
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004622
Javier Cardonadbf498f2012-03-31 11:31:32 -07004623 if (ifmsh->sync_ops)
Masashi Honma445cd452016-12-08 10:15:51 +09004624 ifmsh->sync_ops->adjust_tsf(sdata, beacon);
Javier Cardonadbf498f2012-03-31 11:31:32 -07004625
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -07004626 skb = dev_alloc_skb(local->tx_headroom +
Michal Kazioraf296bd2014-06-05 14:21:36 +02004627 beacon->head_len +
Marco Porsch3f52b7e2013-01-30 18:14:08 +01004628 256 + /* TIM IE */
Michal Kazioraf296bd2014-06-05 14:21:36 +02004629 beacon->tail_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004630 local->hw.extra_beacon_tailroom);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01004631 if (!skb)
4632 goto out;
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08004633 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004634 skb_put_data(skb, beacon->head, beacon->head_len);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004635 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
4636
4637 if (offs) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004638 offs->tim_offset = beacon->head_len;
4639 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004640 }
4641
Johannes Berg59ae1d12017-06-16 14:29:20 +02004642 skb_put_data(skb, beacon->tail, beacon->tail_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004643 } else {
4644 WARN_ON(1);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004645 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02004646 }
4647
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004648 /* CSA offsets */
Michal Kazioraf296bd2014-06-05 14:21:36 +02004649 if (offs && beacon) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004650 int i;
4651
4652 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004653 u16 csa_off = beacon->csa_counter_offsets[i];
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004654
4655 if (!csa_off)
4656 continue;
4657
4658 offs->csa_counter_offs[i] = csa_off_base + csa_off;
4659 }
4660 }
4661
Johannes Berg4bf88532012-11-09 11:39:59 +01004662 band = chanctx_conf->def.chan->band;
Johannes Berg55de9082012-07-26 17:24:39 +02004663
Johannes Berge039fa42008-05-15 12:55:29 +02004664 info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02004665
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004666 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004667 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Johannes Berge039fa42008-05-15 12:55:29 +02004668 info->band = band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004669
4670 memset(&txrc, 0, sizeof(txrc));
4671 txrc.hw = hw;
Johannes Berge31583c2012-07-26 14:07:46 +02004672 txrc.sband = local->hw.wiphy->bands[band];
Jouni Malinene00cfce2009-12-29 12:59:19 +02004673 txrc.bss_conf = &sdata->vif.bss_conf;
4674 txrc.skb = skb;
4675 txrc.reported_rate.idx = -1;
Jouni Malinen37eb0b12010-01-06 13:09:08 +02004676 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
Felix Fietkau8f0729b2010-11-11 15:07:23 +01004677 txrc.bss = true;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004678 rate_control_get_rate(sdata, NULL, &txrc);
Johannes Berge039fa42008-05-15 12:55:29 +02004679
4680 info->control.vif = vif;
Johannes Bergf591fa52008-07-10 11:21:26 +02004681
John W. Linvillea472e712010-05-06 14:45:17 -04004682 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
4683 IEEE80211_TX_CTL_ASSIGN_SEQ |
4684 IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge6a98542008-10-21 12:40:02 +02004685 out:
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004686 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004687 return skb;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004688
4689}
4690
4691struct sk_buff *
4692ieee80211_beacon_get_template(struct ieee80211_hw *hw,
4693 struct ieee80211_vif *vif,
4694 struct ieee80211_mutable_offsets *offs)
4695{
4696 return __ieee80211_beacon_get(hw, vif, offs, true);
4697}
4698EXPORT_SYMBOL(ieee80211_beacon_get_template);
4699
4700struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
4701 struct ieee80211_vif *vif,
4702 u16 *tim_offset, u16 *tim_length)
4703{
4704 struct ieee80211_mutable_offsets offs = {};
4705 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
Helmut Schaa35afa582015-09-09 09:46:32 +02004706 struct sk_buff *copy;
4707 struct ieee80211_supported_band *sband;
4708 int shift;
4709
4710 if (!bcn)
4711 return bcn;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004712
4713 if (tim_offset)
4714 *tim_offset = offs.tim_offset;
4715
4716 if (tim_length)
4717 *tim_length = offs.tim_length;
4718
Helmut Schaa35afa582015-09-09 09:46:32 +02004719 if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
4720 !hw_to_local(hw)->monitors)
4721 return bcn;
4722
4723 /* send a copy to monitor interfaces */
4724 copy = skb_copy(bcn, GFP_ATOMIC);
4725 if (!copy)
4726 return bcn;
4727
4728 shift = ieee80211_vif_get_shift(vif);
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05304729 sband = ieee80211_get_sband(vif_to_sdata(vif));
4730 if (!sband)
4731 return bcn;
4732
John Crispinb7b2e8c2019-07-14 17:44:15 +02004733 ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false,
4734 NULL);
Helmut Schaa35afa582015-09-09 09:46:32 +02004735
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004736 return bcn;
Johannes Berge2ebc742007-07-27 15:43:22 +02004737}
Johannes Bergeddcbb942009-10-29 08:30:35 +01004738EXPORT_SYMBOL(ieee80211_beacon_get_tim);
Johannes Berge2ebc742007-07-27 15:43:22 +02004739
Arik Nemtsov02945822011-11-10 11:28:57 +02004740struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
4741 struct ieee80211_vif *vif)
4742{
4743 struct ieee80211_if_ap *ap = NULL;
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004744 struct sk_buff *skb = NULL;
4745 struct probe_resp *presp = NULL;
Arik Nemtsov02945822011-11-10 11:28:57 +02004746 struct ieee80211_hdr *hdr;
4747 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4748
4749 if (sdata->vif.type != NL80211_IFTYPE_AP)
4750 return NULL;
4751
4752 rcu_read_lock();
4753
4754 ap = &sdata->u.ap;
4755 presp = rcu_dereference(ap->probe_resp);
4756 if (!presp)
4757 goto out;
4758
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004759 skb = dev_alloc_skb(presp->len);
Arik Nemtsov02945822011-11-10 11:28:57 +02004760 if (!skb)
4761 goto out;
4762
Johannes Berg59ae1d12017-06-16 14:29:20 +02004763 skb_put_data(skb, presp->data, presp->len);
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004764
Arik Nemtsov02945822011-11-10 11:28:57 +02004765 hdr = (struct ieee80211_hdr *) skb->data;
4766 memset(hdr->addr1, 0, sizeof(hdr->addr1));
4767
4768out:
4769 rcu_read_unlock();
4770 return skb;
4771}
4772EXPORT_SYMBOL(ieee80211_proberesp_get);
4773
Kalle Valo7044cc52010-01-05 20:16:19 +02004774struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
4775 struct ieee80211_vif *vif)
4776{
4777 struct ieee80211_sub_if_data *sdata;
4778 struct ieee80211_if_managed *ifmgd;
4779 struct ieee80211_pspoll *pspoll;
4780 struct ieee80211_local *local;
4781 struct sk_buff *skb;
4782
4783 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4784 return NULL;
4785
4786 sdata = vif_to_sdata(vif);
4787 ifmgd = &sdata->u.mgd;
4788 local = sdata->local;
4789
4790 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
Joe Perchesd15b8452011-08-29 14:17:31 -07004791 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02004792 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07004793
Kalle Valo7044cc52010-01-05 20:16:19 +02004794 skb_reserve(skb, local->hw.extra_tx_headroom);
4795
Johannes Bergb080db52017-06-16 14:29:19 +02004796 pspoll = skb_put_zero(skb, sizeof(*pspoll));
Kalle Valo7044cc52010-01-05 20:16:19 +02004797 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
4798 IEEE80211_STYPE_PSPOLL);
4799 pspoll->aid = cpu_to_le16(ifmgd->aid);
4800
4801 /* aid in PS-Poll has its two MSBs each set to 1 */
4802 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
4803
4804 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
4805 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
4806
4807 return skb;
4808}
4809EXPORT_SYMBOL(ieee80211_pspoll_get);
4810
4811struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004812 struct ieee80211_vif *vif,
4813 bool qos_ok)
Kalle Valo7044cc52010-01-05 20:16:19 +02004814{
4815 struct ieee80211_hdr_3addr *nullfunc;
4816 struct ieee80211_sub_if_data *sdata;
4817 struct ieee80211_if_managed *ifmgd;
4818 struct ieee80211_local *local;
4819 struct sk_buff *skb;
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004820 bool qos = false;
Kalle Valo7044cc52010-01-05 20:16:19 +02004821
4822 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4823 return NULL;
4824
4825 sdata = vif_to_sdata(vif);
4826 ifmgd = &sdata->u.mgd;
4827 local = sdata->local;
4828
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004829 if (qos_ok) {
4830 struct sta_info *sta;
4831
4832 rcu_read_lock();
4833 sta = sta_info_get(sdata, ifmgd->bssid);
4834 qos = sta && sta->sta.wme;
4835 rcu_read_unlock();
4836 }
4837
4838 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
4839 sizeof(*nullfunc) + 2);
Joe Perchesd15b8452011-08-29 14:17:31 -07004840 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02004841 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07004842
Kalle Valo7044cc52010-01-05 20:16:19 +02004843 skb_reserve(skb, local->hw.extra_tx_headroom);
4844
Johannes Bergb080db52017-06-16 14:29:19 +02004845 nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
Kalle Valo7044cc52010-01-05 20:16:19 +02004846 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
4847 IEEE80211_STYPE_NULLFUNC |
4848 IEEE80211_FCTL_TODS);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004849 if (qos) {
Johannes Berge0ba7092018-11-09 11:16:46 +01004850 __le16 qoshdr = cpu_to_le16(7);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004851
4852 BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
4853 IEEE80211_STYPE_NULLFUNC) !=
4854 IEEE80211_STYPE_QOS_NULLFUNC);
4855 nullfunc->frame_control |=
4856 cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
4857 skb->priority = 7;
4858 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
Johannes Berge0ba7092018-11-09 11:16:46 +01004859 skb_put_data(skb, &qoshdr, sizeof(qoshdr));
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004860 }
4861
Kalle Valo7044cc52010-01-05 20:16:19 +02004862 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
4863 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
4864 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
4865
4866 return skb;
4867}
4868EXPORT_SYMBOL(ieee80211_nullfunc_get);
4869
Kalle Valo05e54ea2010-01-05 20:16:38 +02004870struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
Johannes Berga344d672014-06-12 22:24:31 +02004871 const u8 *src_addr,
Kalle Valo05e54ea2010-01-05 20:16:38 +02004872 const u8 *ssid, size_t ssid_len,
Johannes Bergb9a9ada2012-11-29 13:00:10 +01004873 size_t tailroom)
Kalle Valo05e54ea2010-01-05 20:16:38 +02004874{
Johannes Berga344d672014-06-12 22:24:31 +02004875 struct ieee80211_local *local = hw_to_local(hw);
Kalle Valo05e54ea2010-01-05 20:16:38 +02004876 struct ieee80211_hdr_3addr *hdr;
4877 struct sk_buff *skb;
4878 size_t ie_ssid_len;
4879 u8 *pos;
4880
Kalle Valo05e54ea2010-01-05 20:16:38 +02004881 ie_ssid_len = 2 + ssid_len;
4882
4883 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
Johannes Bergb9a9ada2012-11-29 13:00:10 +01004884 ie_ssid_len + tailroom);
Joe Perchesd15b8452011-08-29 14:17:31 -07004885 if (!skb)
Kalle Valo05e54ea2010-01-05 20:16:38 +02004886 return NULL;
Kalle Valo05e54ea2010-01-05 20:16:38 +02004887
4888 skb_reserve(skb, local->hw.extra_tx_headroom);
4889
Johannes Bergb080db52017-06-16 14:29:19 +02004890 hdr = skb_put_zero(skb, sizeof(*hdr));
Kalle Valo05e54ea2010-01-05 20:16:38 +02004891 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4892 IEEE80211_STYPE_PROBE_REQ);
Johannes Berge83e6542012-07-13 16:23:07 +02004893 eth_broadcast_addr(hdr->addr1);
Johannes Berga344d672014-06-12 22:24:31 +02004894 memcpy(hdr->addr2, src_addr, ETH_ALEN);
Johannes Berge83e6542012-07-13 16:23:07 +02004895 eth_broadcast_addr(hdr->addr3);
Kalle Valo05e54ea2010-01-05 20:16:38 +02004896
4897 pos = skb_put(skb, ie_ssid_len);
4898 *pos++ = WLAN_EID_SSID;
4899 *pos++ = ssid_len;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02004900 if (ssid_len)
Kalle Valo05e54ea2010-01-05 20:16:38 +02004901 memcpy(pos, ssid, ssid_len);
4902 pos += ssid_len;
4903
Kalle Valo05e54ea2010-01-05 20:16:38 +02004904 return skb;
4905}
4906EXPORT_SYMBOL(ieee80211_probereq_get);
4907
Johannes Berg32bfd352007-12-19 01:31:26 +01004908void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02004909 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02004910 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02004911 struct ieee80211_rts *rts)
4912{
4913 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02004914
Harvey Harrison065e96052008-06-22 16:45:27 -07004915 rts->frame_control =
4916 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01004917 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
4918 frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02004919 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
4920 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
4921}
4922EXPORT_SYMBOL(ieee80211_rts_get);
4923
Johannes Berg32bfd352007-12-19 01:31:26 +01004924void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02004925 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02004926 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02004927 struct ieee80211_cts *cts)
4928{
4929 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02004930
Harvey Harrison065e96052008-06-22 16:45:27 -07004931 cts->frame_control =
4932 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01004933 cts->duration = ieee80211_ctstoself_duration(hw, vif,
4934 frame_len, frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02004935 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
4936}
4937EXPORT_SYMBOL(ieee80211_ctstoself_get);
4938
4939struct sk_buff *
Johannes Berg32bfd352007-12-19 01:31:26 +01004940ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
Johannes Berge039fa42008-05-15 12:55:29 +02004941 struct ieee80211_vif *vif)
Johannes Berge2ebc742007-07-27 15:43:22 +02004942{
4943 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004944 struct sk_buff *skb = NULL;
Johannes Berg5cf121c2008-02-25 16:27:43 +01004945 struct ieee80211_tx_data tx;
Johannes Berge2ebc742007-07-27 15:43:22 +02004946 struct ieee80211_sub_if_data *sdata;
Marco Porschd012a602012-10-10 12:39:50 -07004947 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +02004948 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02004949 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berge2ebc742007-07-27 15:43:22 +02004950
Johannes Berg32bfd352007-12-19 01:31:26 +01004951 sdata = vif_to_sdata(vif);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004952
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004953 rcu_read_lock();
Johannes Berg55de9082012-07-26 17:24:39 +02004954 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004955
Marco Porschd012a602012-10-10 12:39:50 -07004956 if (!chanctx_conf)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004957 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004958
Marco Porschd012a602012-10-10 12:39:50 -07004959 if (sdata->vif.type == NL80211_IFTYPE_AP) {
4960 struct beacon_data *beacon =
4961 rcu_dereference(sdata->u.ap.beacon);
4962
4963 if (!beacon || !beacon->head)
4964 goto out;
4965
4966 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01004967 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4968 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07004969 } else {
4970 goto out;
4971 }
4972
4973 if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004974 goto out; /* send buffered bc/mc only after DTIM beacon */
Johannes Berge039fa42008-05-15 12:55:29 +02004975
Johannes Berge2ebc742007-07-27 15:43:22 +02004976 while (1) {
Marco Porschd012a602012-10-10 12:39:50 -07004977 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +02004978 if (!skb)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004979 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02004980 local->total_ps_buffered--;
4981
Marco Porschd012a602012-10-10 12:39:50 -07004982 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004983 struct ieee80211_hdr *hdr =
4984 (struct ieee80211_hdr *) skb->data;
4985 /* more buffered multicast/broadcast frames ==> set
4986 * MoreData flag in IEEE 802.11 header to inform PS
4987 * STAs */
4988 hdr->frame_control |=
4989 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
4990 }
4991
Michael Braun112c44b2014-03-06 15:08:43 +01004992 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porsch7cbf9d02013-03-01 16:01:18 +01004993 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
Johannes Berg7c107702015-03-20 14:18:27 +01004994 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
Johannes Berge2ebc742007-07-27 15:43:22 +02004995 break;
Felix Fietkau6b07d9c2016-08-02 11:13:41 +02004996 ieee80211_free_txskb(hw, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02004997 }
Johannes Berge039fa42008-05-15 12:55:29 +02004998
4999 info = IEEE80211_SKB_CB(skb);
5000
Johannes Berg5cf121c2008-02-25 16:27:43 +01005001 tx.flags |= IEEE80211_TX_PS_BUFFERED;
Johannes Berg4bf88532012-11-09 11:39:59 +01005002 info->band = chanctx_conf->def.chan->band;
Johannes Berge2ebc742007-07-27 15:43:22 +02005003
Johannes Berg97b045d2008-06-20 01:22:30 +02005004 if (invoke_tx_handlers(&tx))
Johannes Berge2ebc742007-07-27 15:43:22 +02005005 skb = NULL;
Johannes Berg97b045d2008-06-20 01:22:30 +02005006 out:
Johannes Bergd0709a62008-02-25 16:27:46 +01005007 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02005008
5009 return skb;
5010}
5011EXPORT_SYMBOL(ieee80211_get_buffered_bc);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005012
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005013int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5014{
5015 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5016 struct ieee80211_sub_if_data *sdata = sta->sdata;
5017 struct ieee80211_local *local = sdata->local;
5018 int ret;
5019 u32 queues;
5020
5021 lockdep_assert_held(&local->sta_mtx);
5022
5023 /* only some cases are supported right now */
5024 switch (sdata->vif.type) {
5025 case NL80211_IFTYPE_STATION:
5026 case NL80211_IFTYPE_AP:
5027 case NL80211_IFTYPE_AP_VLAN:
5028 break;
5029 default:
5030 WARN_ON(1);
5031 return -EINVAL;
5032 }
5033
5034 if (WARN_ON(tid >= IEEE80211_NUM_UPS))
5035 return -EINVAL;
5036
5037 if (sta->reserved_tid == tid) {
5038 ret = 0;
5039 goto out;
5040 }
5041
5042 if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
5043 sdata_err(sdata, "TID reservation already active\n");
5044 ret = -EALREADY;
5045 goto out;
5046 }
5047
5048 ieee80211_stop_vif_queues(sdata->local, sdata,
5049 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5050
5051 synchronize_net();
5052
5053 /* Tear down BA sessions so we stop aggregating on this TID */
Johannes Berg30686bf2015-06-02 21:39:54 +02005054 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005055 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5056 __ieee80211_stop_tx_ba_session(sta, tid,
5057 AGG_STOP_LOCAL_REQUEST);
5058 }
5059
5060 queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02005061 __ieee80211_flush_queues(local, sdata, queues, false);
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005062
5063 sta->reserved_tid = tid;
5064
5065 ieee80211_wake_vif_queues(local, sdata,
5066 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5067
Johannes Berg30686bf2015-06-02 21:39:54 +02005068 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005069 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5070
5071 ret = 0;
5072 out:
5073 return ret;
5074}
5075EXPORT_SYMBOL(ieee80211_reserve_tid);
5076
5077void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5078{
5079 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5080 struct ieee80211_sub_if_data *sdata = sta->sdata;
5081
5082 lockdep_assert_held(&sdata->local->sta_mtx);
5083
5084 /* only some cases are supported right now */
5085 switch (sdata->vif.type) {
5086 case NL80211_IFTYPE_STATION:
5087 case NL80211_IFTYPE_AP:
5088 case NL80211_IFTYPE_AP_VLAN:
5089 break;
5090 default:
5091 WARN_ON(1);
5092 return;
5093 }
5094
5095 if (tid != sta->reserved_tid) {
5096 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
5097 return;
5098 }
5099
5100 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
5101}
5102EXPORT_SYMBOL(ieee80211_unreserve_tid);
5103
Johannes Berg55de9082012-07-26 17:24:39 +02005104void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
5105 struct sk_buff *skb, int tid,
Johannes Bergb9771d42018-05-28 15:47:41 +02005106 enum nl80211_band band, u32 txdata_flags)
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005107{
Amadeusz Sławiński731977e2017-01-24 16:42:10 +01005108 int ac = ieee80211_ac_from_tid(tid);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02005109
Zhang Shengjud57a5442016-03-03 01:16:56 +00005110 skb_reset_mac_header(skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02005111 skb_set_queue_mapping(skb, ac);
Helmut Schaacf6bb792011-12-15 10:18:34 +01005112 skb->priority = tid;
Johannes Bergcf0277e2010-01-05 18:00:58 +01005113
Johannes Berg89afe612013-02-13 15:39:57 +01005114 skb->dev = sdata->dev;
5115
Johannes Berg3d34deb2009-06-18 17:25:11 +02005116 /*
5117 * The other path calling ieee80211_xmit is from the tasklet,
5118 * and while we can handle concurrent transmissions locking
5119 * requirements are that we do not come into tx with bhs on.
5120 */
5121 local_bh_disable();
Johannes Berg73c4e192014-11-09 18:50:09 +02005122 IEEE80211_SKB_CB(skb)->band = band;
Johannes Bergb9771d42018-05-28 15:47:41 +02005123 ieee80211_xmit(sdata, NULL, skb, txdata_flags);
Johannes Berg3d34deb2009-06-18 17:25:11 +02005124 local_bh_enable();
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005125}
Denis Kenzior91180642018-03-26 12:52:50 -05005126
5127int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
5128 const u8 *buf, size_t len,
5129 const u8 *dest, __be16 proto, bool unencrypted)
5130{
5131 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5132 struct ieee80211_local *local = sdata->local;
5133 struct sk_buff *skb;
5134 struct ethhdr *ehdr;
5135 u32 flags;
5136
5137 /* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
5138 * or Pre-Authentication
5139 */
5140 if (proto != sdata->control_port_protocol &&
5141 proto != cpu_to_be16(ETH_P_PREAUTH))
5142 return -EINVAL;
5143
5144 if (unencrypted)
5145 flags = IEEE80211_TX_INTFL_DONT_ENCRYPT;
5146 else
5147 flags = 0;
5148
5149 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5150 sizeof(struct ethhdr) + len);
5151 if (!skb)
5152 return -ENOMEM;
5153
5154 skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
5155
5156 skb_put_data(skb, buf, len);
5157
5158 ehdr = skb_push(skb, sizeof(struct ethhdr));
5159 memcpy(ehdr->h_dest, dest, ETH_ALEN);
5160 memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
5161 ehdr->h_proto = proto;
5162
5163 skb->dev = dev;
5164 skb->protocol = htons(ETH_P_802_3);
5165 skb_reset_network_header(skb);
5166 skb_reset_mac_header(skb);
5167
Denis Kenziore7441c92018-06-19 10:39:50 -05005168 local_bh_disable();
Rajkumar Manoharan06016772019-04-11 13:47:25 -07005169 __ieee80211_subif_start_xmit(skb, skb->dev, flags, 0);
Denis Kenziore7441c92018-06-19 10:39:50 -05005170 local_bh_enable();
Denis Kenzior91180642018-03-26 12:52:50 -05005171
5172 return 0;
5173}
Rajkumar Manoharan8828f812019-04-11 13:47:26 -07005174
5175int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
5176 const u8 *buf, size_t len)
5177{
5178 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5179 struct ieee80211_local *local = sdata->local;
5180 struct sk_buff *skb;
5181
5182 skb = dev_alloc_skb(local->hw.extra_tx_headroom + len +
5183 30 + /* header size */
5184 18); /* 11s header size */
5185 if (!skb)
5186 return -ENOMEM;
5187
5188 skb_reserve(skb, local->hw.extra_tx_headroom);
5189 skb_put_data(skb, buf, len);
5190
5191 skb->dev = dev;
5192 skb->protocol = htons(ETH_P_802_3);
5193 skb_reset_network_header(skb);
5194 skb_reset_mac_header(skb);
5195
5196 local_bh_disable();
5197 __ieee80211_subif_start_xmit(skb, skb->dev, 0,
5198 IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP);
Johannes Berge2ebc742007-07-27 15:43:22 +02005199 local_bh_enable();
5200
5201 return 0;
5202}