blob: 7aaa71ed49b0ceb602b0bf99f27ac1bb2f31d6f1 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Johannes Berge2ebc742007-07-27 15:43:22 +02002/*
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
6 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
Johannes Bergd98ad832014-09-03 15:24:57 +03007 * Copyright 2013-2014 Intel Mobile Communications GmbH
Johannes Berg73bc9e02021-03-19 23:28:01 +01008 * Copyright (C) 2018-2021 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 Berg252b86c2011-11-16 15:28:55 +010041static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
42 struct sk_buff *skb, int group_addr,
Johannes Berg03f93c32008-06-25 14:36:27 +030043 int next_frag_len)
Johannes Berge2ebc742007-07-27 15:43:22 +020044{
Simon Wunderlich2103dec2013-07-08 16:55:53 +020045 int rate, mrate, erp, dur, i, shift = 0;
Johannes Berg2e92e6f2008-05-15 12:55:27 +020046 struct ieee80211_rate *txrate;
Johannes Berge2ebc742007-07-27 15:43:22 +020047 struct ieee80211_local *local = tx->local;
Johannes Berg8318d782008-01-24 19:38:38 +010048 struct ieee80211_supported_band *sband;
Harvey Harrison358c8d92008-07-15 18:44:13 -070049 struct ieee80211_hdr *hdr;
Johannes Berg252b86c2011-11-16 15:28:55 +010050 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Simon Wunderlich2103dec2013-07-08 16:55:53 +020051 struct ieee80211_chanctx_conf *chanctx_conf;
52 u32 rate_flags = 0;
53
Felix Fietkau95cd4702016-12-14 20:46:57 +010054 /* assume HW handles this */
55 if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))
56 return 0;
57
Simon Wunderlich2103dec2013-07-08 16:55:53 +020058 rcu_read_lock();
59 chanctx_conf = rcu_dereference(tx->sdata->vif.chanctx_conf);
60 if (chanctx_conf) {
61 shift = ieee80211_chandef_get_shift(&chanctx_conf->def);
62 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
63 }
64 rcu_read_unlock();
Johannes Berge6a98542008-10-21 12:40:02 +020065
Johannes Berge6a98542008-10-21 12:40:02 +020066 /* uh huh? */
Felix Fietkau0d528d82013-04-22 16:14:41 +020067 if (WARN_ON_ONCE(tx->rate.idx < 0))
Johannes Berge6a98542008-10-21 12:40:02 +020068 return 0;
Johannes Berge2ebc742007-07-27 15:43:22 +020069
Johannes Berg2d565772012-07-23 15:12:51 +020070 sband = local->hw.wiphy->bands[info->band];
Felix Fietkau0d528d82013-04-22 16:14:41 +020071 txrate = &sband->bitrates[tx->rate.idx];
Johannes Berg8318d782008-01-24 19:38:38 +010072
Johannes Berge6a98542008-10-21 12:40:02 +020073 erp = txrate->flags & IEEE80211_RATE_ERP_G;
Johannes Berge2ebc742007-07-27 15:43:22 +020074
Thomas Pedersen89b8c022020-09-21 19:28:11 -070075 /* device is expected to do this */
76 if (sband->band == NL80211_BAND_S1GHZ)
77 return 0;
78
Johannes Berge2ebc742007-07-27 15:43:22 +020079 /*
80 * data and mgmt (except PS Poll):
81 * - during CFP: 32768
82 * - during contention period:
83 * if addr1 is group address: 0
84 * if more fragments = 0 and addr1 is individual address: time to
85 * transmit one ACK plus SIFS
86 * if more fragments = 1 and addr1 is individual address: time to
87 * transmit next fragment plus 2 x ACK plus 3 x SIFS
88 *
89 * IEEE 802.11, 9.6:
90 * - control response frame (CTS or ACK) shall be transmitted using the
91 * same rate as the immediately previous frame in the frame exchange
92 * sequence, if this rate belongs to the PHY mandatory rates, or else
93 * at the highest possible rate belonging to the PHY rates in the
94 * BSSBasicRateSet
95 */
Johannes Berg252b86c2011-11-16 15:28:55 +010096 hdr = (struct ieee80211_hdr *)skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -070097 if (ieee80211_is_ctl(hdr->frame_control)) {
Johannes Berge2ebc742007-07-27 15:43:22 +020098 /* TODO: These control frames are not currently sent by
Johannes Bergccd7b362008-09-11 00:01:56 +020099 * mac80211, but should they be implemented, this function
Johannes Berge2ebc742007-07-27 15:43:22 +0200100 * needs to be updated to support duration field calculation.
101 *
102 * RTS: time needed to transmit pending data/mgmt frame plus
103 * one CTS frame plus one ACK frame plus 3 x SIFS
104 * CTS: duration of immediately previous RTS minus time
105 * required to transmit CTS and its SIFS
106 * ACK: 0 if immediately previous directed data/mgmt had
107 * more=0, with more=1 duration in ACK frame is duration
108 * from previous frame minus time needed to transmit ACK
109 * and its SIFS
110 * PS Poll: BIT(15) | BIT(14) | aid
111 */
112 return 0;
113 }
114
115 /* data/mgmt */
116 if (0 /* FIX: data/mgmt during CFP */)
Johannes Berg03f93c32008-06-25 14:36:27 +0300117 return cpu_to_le16(32768);
Johannes Berge2ebc742007-07-27 15:43:22 +0200118
119 if (group_addr) /* Group address as the destination - no ACK */
120 return 0;
121
122 /* Individual destination address:
123 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
124 * CTS and ACK frames shall be transmitted using the highest rate in
125 * basic rate set that is less than or equal to the rate of the
126 * immediately previous frame and that is using the same modulation
127 * (CCK or OFDM). If no basic rate set matches with these requirements,
128 * the highest mandatory rate of the PHY that is less than or equal to
129 * the rate of the previous frame is used.
130 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
131 */
132 rate = -1;
Johannes Berg8318d782008-01-24 19:38:38 +0100133 /* use lowest available if everything fails */
134 mrate = sband->bitrates[0].bitrate;
135 for (i = 0; i < sband->n_bitrates; i++) {
136 struct ieee80211_rate *r = &sband->bitrates[i];
137
138 if (r->bitrate > txrate->bitrate)
Johannes Berge2ebc742007-07-27 15:43:22 +0200139 break;
140
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200141 if ((rate_flags & r->flags) != rate_flags)
142 continue;
143
Johannes Bergbda39332008-10-11 01:51:51 +0200144 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200145 rate = DIV_ROUND_UP(r->bitrate, 1 << shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200146
Johannes Berg8318d782008-01-24 19:38:38 +0100147 switch (sband->band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200148 case NL80211_BAND_2GHZ: {
Johannes Berg8318d782008-01-24 19:38:38 +0100149 u32 flag;
150 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
151 flag = IEEE80211_RATE_MANDATORY_G;
152 else
153 flag = IEEE80211_RATE_MANDATORY_B;
154 if (r->flags & flag)
155 mrate = r->bitrate;
156 break;
157 }
Johannes Berg57fbcce2016-04-12 15:56:15 +0200158 case NL80211_BAND_5GHZ:
Arend van Sprielc5b9a7f2019-08-02 13:30:58 +0200159 case NL80211_BAND_6GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +0100160 if (r->flags & IEEE80211_RATE_MANDATORY_A)
161 mrate = r->bitrate;
162 break;
Thomas Pedersendf78a0c2020-06-01 23:22:47 -0700163 case NL80211_BAND_S1GHZ:
Johannes Berg57fbcce2016-04-12 15:56:15 +0200164 case NL80211_BAND_60GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300165 /* TODO, for now fall through */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200166 case NUM_NL80211_BANDS:
Johannes Berg8318d782008-01-24 19:38:38 +0100167 WARN_ON(1);
168 break;
169 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200170 }
171 if (rate == -1) {
172 /* No matching basic rate found; use highest suitable mandatory
173 * PHY rate */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200174 rate = DIV_ROUND_UP(mrate, 1 << shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200175 }
176
Simon Wunderlich6674f212011-11-21 21:34:30 +0100177 /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
178 if (ieee80211_is_data_qos(hdr->frame_control) &&
Claudio Pisac26a0e12012-05-28 13:06:25 +0100179 *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
Simon Wunderlich6674f212011-11-21 21:34:30 +0100180 dur = 0;
181 else
182 /* Time needed to transmit ACK
183 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
184 * to closest integer */
Michal Kazior4ee73f32012-04-11 08:47:56 +0200185 dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200186 tx->sdata->vif.bss_conf.use_short_preamble,
187 shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200188
189 if (next_frag_len) {
190 /* Frame is fragmented: duration increases with time needed to
191 * transmit next fragment plus ACK and 2 x SIFS. */
192 dur *= 2; /* ACK + SIFS */
193 /* next fragment */
Michal Kazior4ee73f32012-04-11 08:47:56 +0200194 dur += ieee80211_frame_duration(sband->band, next_frag_len,
Johannes Berg8318d782008-01-24 19:38:38 +0100195 txrate->bitrate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200196 tx->sdata->vif.bss_conf.use_short_preamble,
197 shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200198 }
199
Johannes Berg03f93c32008-06-25 14:36:27 +0300200 return cpu_to_le16(dur);
Johannes Berge2ebc742007-07-27 15:43:22 +0200201}
202
Johannes Berge2ebc742007-07-27 15:43:22 +0200203/* tx handlers */
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200204static ieee80211_tx_result debug_noinline
205ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
206{
207 struct ieee80211_local *local = tx->local;
Kalle Valo0c742112010-01-12 10:42:53 +0200208 struct ieee80211_if_managed *ifmgd;
Andrei Otcheretianski94a5b3a2018-09-05 08:06:14 +0300209 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200210
211 /* driver doesn't support power save */
Johannes Berg30686bf2015-06-02 21:39:54 +0200212 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200213 return TX_CONTINUE;
214
215 /* hardware does dynamic power save */
Johannes Berg30686bf2015-06-02 21:39:54 +0200216 if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200217 return TX_CONTINUE;
218
219 /* dynamic power save disabled */
220 if (local->hw.conf.dynamic_ps_timeout <= 0)
221 return TX_CONTINUE;
222
223 /* we are scanning, don't enable power save */
224 if (local->scanning)
225 return TX_CONTINUE;
226
227 if (!local->ps_sdata)
228 return TX_CONTINUE;
229
230 /* No point if we're going to suspend */
231 if (local->quiescing)
232 return TX_CONTINUE;
233
Kalle Valo0c742112010-01-12 10:42:53 +0200234 /* dynamic ps is supported only in managed mode */
235 if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
236 return TX_CONTINUE;
237
Andrei Otcheretianski94a5b3a2018-09-05 08:06:14 +0300238 if (unlikely(info->flags & IEEE80211_TX_INTFL_OFFCHAN_TX_OK))
239 return TX_CONTINUE;
240
Kalle Valo0c742112010-01-12 10:42:53 +0200241 ifmgd = &tx->sdata->u.mgd;
242
243 /*
244 * Don't wakeup from power save if u-apsd is enabled, voip ac has
245 * u-apsd enabled and the frame is in voip class. This effectively
246 * means that even if all access categories have u-apsd enabled, in
247 * practise u-apsd is only used with the voip ac. This is a
248 * workaround for the case when received voip class packets do not
249 * have correct qos tag for some reason, due the network or the
250 * peer application.
251 *
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200252 * Note: ifmgd->uapsd_queues access is racy here. If the value is
Kalle Valo0c742112010-01-12 10:42:53 +0200253 * changed via debugfs, user needs to reassociate manually to have
254 * everything in sync.
255 */
Johannes Berg4875d30d2012-03-27 14:18:37 +0200256 if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
257 (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
258 skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
Kalle Valo0c742112010-01-12 10:42:53 +0200259 return TX_CONTINUE;
260
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200261 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
262 ieee80211_stop_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +0100263 IEEE80211_MAX_QUEUE_MAP,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300264 IEEE80211_QUEUE_STOP_REASON_PS,
265 false);
Vivek Natarajandb285692011-02-18 17:18:03 +0530266 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200267 ieee80211_queue_work(&local->hw,
268 &local->dynamic_ps_disable_work);
269 }
270
Luciano Coelho5db1c072011-05-03 21:40:08 +0300271 /* Don't restart the timer if we're not disassociated */
272 if (!ifmgd->associated)
273 return TX_CONTINUE;
274
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200275 mod_timer(&local->dynamic_ps_timer, jiffies +
276 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
277
278 return TX_CONTINUE;
279}
Johannes Berge2ebc742007-07-27 15:43:22 +0200280
Johannes Bergd9e8a702008-06-30 15:10:44 +0200281static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100282ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200283{
Harvey Harrison358c8d92008-07-15 18:44:13 -0700284
Johannes Berge039fa42008-05-15 12:55:29 +0200285 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200286 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200287 bool assoc = false;
Johannes Berge2ebc742007-07-27 15:43:22 +0200288
Johannes Berge039fa42008-05-15 12:55:29 +0200289 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100290 return TX_CONTINUE;
Johannes Berg58d41852007-09-26 17:53:18 +0200291
Ben Greearb23b0252011-02-04 11:54:17 -0800292 if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
293 test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
Kalle Valoa9a6fff2009-03-18 14:06:44 +0200294 !ieee80211_is_probe_req(hdr->frame_control) &&
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -0800295 !ieee80211_is_any_nullfunc(hdr->frame_control))
Kalle Valoa9a6fff2009-03-18 14:06:44 +0200296 /*
297 * When software scanning only nullfunc frames (to notify
298 * the sleep state to the AP) and probe requests (for the
299 * active scan) are allowed, all other frames should not be
300 * sent and we should not get here, but if we do
301 * nonetheless, drop them to avoid sending them
302 * off-channel. See the link below and
303 * ieee80211_start_scan() for more.
304 *
305 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
306 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100307 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200308
Rostislav Lisovy239281f2014-11-03 10:33:19 +0100309 if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
310 return TX_CONTINUE;
311
Johannes Berg5cf121c2008-02-25 16:27:43 +0100312 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100313 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200314
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200315 if (tx->sta)
316 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge2ebc742007-07-27 15:43:22 +0200317
Johannes Berg5cf121c2008-02-25 16:27:43 +0100318 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200319 if (unlikely(!assoc &&
Harvey Harrison358c8d92008-07-15 18:44:13 -0700320 ieee80211_is_data(hdr->frame_control))) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200321#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200322 sdata_info(tx->sdata,
323 "dropped data frame to not associated station %pM\n",
324 hdr->addr1);
325#endif
Johannes Berge2ebc742007-07-27 15:43:22 +0200326 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100327 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200328 }
Michael Braun72f15d52016-10-10 19:12:21 +0200329 } else if (unlikely(ieee80211_is_data(hdr->frame_control) &&
330 ieee80211_vif_get_num_mcast_if(tx->sdata) == 0)) {
Johannes Berg29623892011-12-14 12:20:31 +0100331 /*
332 * No associated STAs - no need to send multicast
333 * frames.
334 */
335 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200336 }
337
Johannes Berg9ae54c82008-01-31 19:48:20 +0100338 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200339}
340
Johannes Berge2ebc742007-07-27 15:43:22 +0200341/* This function is called whenever the AP is about to exceed the maximum limit
342 * of buffered frames for power saving STAs. This situation should not really
343 * happen often during normal operation, so dropping the oldest buffered packet
344 * from each queue should be OK to make some room for new frames. */
345static void purge_old_ps_buffers(struct ieee80211_local *local)
346{
347 int total = 0, purged = 0;
348 struct sk_buff *skb;
349 struct ieee80211_sub_if_data *sdata;
350 struct sta_info *sta;
351
Johannes Berg79010422007-09-18 17:29:21 -0400352 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Marco Porschd012a602012-10-10 12:39:50 -0700353 struct ps_data *ps;
354
355 if (sdata->vif.type == NL80211_IFTYPE_AP)
356 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100357 else if (ieee80211_vif_is_mesh(&sdata->vif))
358 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700359 else
Johannes Berge2ebc742007-07-27 15:43:22 +0200360 continue;
Marco Porschd012a602012-10-10 12:39:50 -0700361
362 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +0200363 if (skb) {
364 purged++;
Felix Fietkau6b07d9c2016-08-02 11:13:41 +0200365 ieee80211_free_txskb(&local->hw, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +0200366 }
Marco Porschd012a602012-10-10 12:39:50 -0700367 total += skb_queue_len(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +0200368 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200369
Johannes Berg948d8872011-09-29 16:04:29 +0200370 /*
371 * Drop one frame from each station from the lowest-priority
372 * AC that has frames at all.
373 */
Johannes Bergd0709a62008-02-25 16:27:46 +0100374 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg948d8872011-09-29 16:04:29 +0200375 int ac;
376
377 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
378 skb = skb_dequeue(&sta->ps_tx_buf[ac]);
379 total += skb_queue_len(&sta->ps_tx_buf[ac]);
380 if (skb) {
381 purged++;
Felix Fietkauc3e77242012-10-08 14:39:33 +0200382 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg948d8872011-09-29 16:04:29 +0200383 break;
384 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200385 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200386 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100387
Johannes Berge2ebc742007-07-27 15:43:22 +0200388 local->total_ps_buffered = total;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200389 ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
Johannes Berge2ebc742007-07-27 15:43:22 +0200390}
391
Johannes Berg9ae54c82008-01-31 19:48:20 +0100392static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100393ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200394{
Johannes Berge039fa42008-05-15 12:55:29 +0200395 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700396 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Marco Porschd012a602012-10-10 12:39:50 -0700397 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +0200398
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100399 /*
400 * broadcast/multicast frame
401 *
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100402 * If any of the associated/peer stations is in power save mode,
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100403 * the frame is buffered to be sent after DTIM beacon frame.
404 * This is done either by the hardware or us.
405 */
406
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100407 /* powersaving STAs currently only in AP/VLAN/mesh mode */
Marco Porschd012a602012-10-10 12:39:50 -0700408 if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
409 tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
410 if (!tx->sdata->bss)
411 return TX_CONTINUE;
412
413 ps = &tx->sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100414 } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
415 ps = &tx->sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700416 } else {
Johannes Berg3e122be2008-07-09 14:40:34 +0200417 return TX_CONTINUE;
Marco Porschd012a602012-10-10 12:39:50 -0700418 }
419
Johannes Berg3e122be2008-07-09 14:40:34 +0200420
421 /* no buffering for ordered frames */
Harvey Harrison358c8d92008-07-15 18:44:13 -0700422 if (ieee80211_has_order(hdr->frame_control))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100423 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100424
Johannes Berg08b99392014-07-07 12:01:11 +0200425 if (ieee80211_is_probe_req(hdr->frame_control))
426 return TX_CONTINUE;
427
Johannes Berg30686bf2015-06-02 21:39:54 +0200428 if (ieee80211_hw_check(&tx->local->hw, QUEUE_CONTROL))
Johannes Bergf4d57942013-05-28 17:24:15 +0200429 info->hw_queue = tx->sdata->vif.cab_queue;
430
Felix Fietkau9ec11902018-11-28 22:39:16 +0100431 /* no stations in PS mode and no buffered packets */
432 if (!atomic_read(&ps->num_sta_ps) && skb_queue_empty(&ps->bc_buf))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100433 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100434
Johannes Berg62b517c2009-10-29 12:19:21 +0100435 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
Johannes Berg62b12082009-08-10 16:04:15 +0200436
Johannes Berg62b517c2009-10-29 12:19:21 +0100437 /* device releases frame after DTIM beacon */
Johannes Berg30686bf2015-06-02 21:39:54 +0200438 if (!ieee80211_hw_check(&tx->local->hw, HOST_BROADCAST_PS_BUFFERING))
Johannes Berg62b12082009-08-10 16:04:15 +0200439 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200440
Johannes Berg62b12082009-08-10 16:04:15 +0200441 /* buffered in mac80211 */
442 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
443 purge_old_ps_buffers(tx->local);
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100444
Marco Porschd012a602012-10-10 12:39:50 -0700445 if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200446 ps_dbg(tx->sdata,
447 "BC TX buffer full - dropping the oldest frame\n");
Felix Fietkau6b07d9c2016-08-02 11:13:41 +0200448 ieee80211_free_txskb(&tx->local->hw, skb_dequeue(&ps->bc_buf));
Johannes Berg62b12082009-08-10 16:04:15 +0200449 } else
450 tx->local->total_ps_buffered++;
451
Marco Porschd012a602012-10-10 12:39:50 -0700452 skb_queue_tail(&ps->bc_buf, tx->skb);
Johannes Berg62b12082009-08-10 16:04:15 +0200453
454 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200455}
456
Jouni Malinenfb733332009-01-08 13:32:00 +0200457static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
458 struct sk_buff *skb)
459{
460 if (!ieee80211_is_mgmt(fc))
461 return 0;
462
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200463 if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
Jouni Malinenfb733332009-01-08 13:32:00 +0200464 return 0;
465
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100466 if (!ieee80211_is_robust_mgmt_frame(skb))
Jouni Malinenfb733332009-01-08 13:32:00 +0200467 return 0;
468
469 return 1;
470}
471
Johannes Berg9ae54c82008-01-31 19:48:20 +0100472static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100473ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200474{
475 struct sta_info *sta = tx->sta;
Johannes Berge039fa42008-05-15 12:55:29 +0200476 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berg08b99392014-07-07 12:01:11 +0200477 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300478 struct ieee80211_local *local = tx->local;
Johannes Berge2ebc742007-07-27 15:43:22 +0200479
Johannes Berg02f2f1a2012-02-27 12:18:30 +0100480 if (unlikely(!sta))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100481 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200482
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200483 if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
Johannes Berg5ac2e352014-05-27 16:32:27 +0200484 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
485 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
Johannes Berg02f2f1a2012-02-27 12:18:30 +0100486 !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
Johannes Berg948d8872011-09-29 16:04:29 +0200487 int ac = skb_get_queue_mapping(tx->skb);
488
Johannes Berg08b99392014-07-07 12:01:11 +0200489 if (ieee80211_is_mgmt(hdr->frame_control) &&
490 !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
491 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
492 return TX_CONTINUE;
493 }
494
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200495 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
496 sta->sta.addr, sta->sta.aid, ac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200497 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
498 purge_old_ps_buffers(tx->local);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200499
500 /* sync with ieee80211_sta_ps_deliver_wakeup */
501 spin_lock(&sta->ps_lock);
502 /*
503 * STA woke up the meantime and all the frames on ps_tx_buf have
504 * been queued to pending queue. No reordering can happen, go
505 * ahead and Tx the packet.
506 */
507 if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
Johannes Berg5ac2e352014-05-27 16:32:27 +0200508 !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
509 !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200510 spin_unlock(&sta->ps_lock);
511 return TX_CONTINUE;
512 }
513
Johannes Berg948d8872011-09-29 16:04:29 +0200514 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
515 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200516 ps_dbg(tx->sdata,
517 "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
518 sta->sta.addr, ac);
Felix Fietkauc3e77242012-10-08 14:39:33 +0200519 ieee80211_free_txskb(&local->hw, old);
Johannes Berge2ebc742007-07-27 15:43:22 +0200520 } else
521 tx->local->total_ps_buffered++;
Johannes Berg004c8722008-02-20 11:21:35 +0100522
Johannes Berge039fa42008-05-15 12:55:29 +0200523 info->control.jiffies = jiffies;
Johannes Berg5061b0c2009-07-14 00:33:34 +0200524 info->control.vif = &tx->sdata->vif;
Felix Fietkaucc20ff22020-09-08 14:36:57 +0200525 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
Johannes Berg03c8c062014-01-09 01:45:28 +0100526 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berg948d8872011-09-29 16:04:29 +0200527 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200528 spin_unlock(&sta->ps_lock);
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300529
530 if (!timer_pending(&local->sta_cleanup))
531 mod_timer(&local->sta_cleanup,
532 round_jiffies(jiffies +
533 STA_INFO_CLEANUP_INTERVAL));
534
Johannes Bergc868cb352011-09-29 16:04:27 +0200535 /*
536 * We queued up some frames, so the TIM bit might
537 * need to be set, recalculate it.
538 */
539 sta_info_recalc_tim(sta);
540
Johannes Berg9ae54c82008-01-31 19:48:20 +0100541 return TX_QUEUED;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200542 } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
543 ps_dbg(tx->sdata,
544 "STA %pM in PS mode, but polling/in SP -> send frame\n",
545 sta->sta.addr);
Johannes Berge2ebc742007-07-27 15:43:22 +0200546 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200547
Johannes Berg9ae54c82008-01-31 19:48:20 +0100548 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200549}
550
Johannes Bergd9e8a702008-06-30 15:10:44 +0200551static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100552ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200553{
Johannes Berg5cf121c2008-02-25 16:27:43 +0100554 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100555 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200556
Johannes Berg5cf121c2008-02-25 16:27:43 +0100557 if (tx->flags & IEEE80211_TX_UNICAST)
Johannes Berge2ebc742007-07-27 15:43:22 +0200558 return ieee80211_tx_h_unicast_ps_buf(tx);
559 else
560 return ieee80211_tx_h_multicast_ps_buf(tx);
561}
562
Johannes Bergd9e8a702008-06-30 15:10:44 +0200563static ieee80211_tx_result debug_noinline
Johannes Berga621fa42010-08-27 14:26:54 +0300564ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
565{
566 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
567
Johannes Bergaf61a162013-07-02 18:09:12 +0200568 if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
569 if (tx->sdata->control_port_no_encrypt)
570 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
571 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
Jouni Malinen9c1c98a2015-02-26 15:50:50 +0200572 info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
Johannes Bergaf61a162013-07-02 18:09:12 +0200573 }
Johannes Berga621fa42010-08-27 14:26:54 +0300574
575 return TX_CONTINUE;
576}
577
578static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100579ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200580{
Johannes Berg46e6de12012-07-04 18:10:07 +0200581 struct ieee80211_key *key;
Johannes Berge039fa42008-05-15 12:55:29 +0200582 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700583 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400584
Johannes Berga0761a32020-03-26 15:09:42 +0200585 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200586 tx->key = NULL;
Johannes Berga0761a32020-03-26 15:09:42 +0200587 return TX_CONTINUE;
588 }
589
590 if (tx->sta &&
591 (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
Johannes Bergd4e46a32007-09-14 11:10:24 -0400592 tx->key = key;
Masashi Honma46f6b062016-06-22 19:55:20 +0900593 else if (ieee80211_is_group_privacy_action(tx->skb) &&
594 (key = rcu_dereference(tx->sdata->default_multicast_key)))
595 tx->key = key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200596 else if (ieee80211_is_mgmt(hdr->frame_control) &&
Jouni Malinenecbcd322010-03-29 23:35:23 -0700597 is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100598 ieee80211_is_robust_mgmt_frame(tx->skb) &&
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200599 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
600 tx->key = key;
Johannes Bergf7e01042010-12-09 19:49:02 +0100601 else if (is_multicast_ether_addr(hdr->addr1) &&
602 (key = rcu_dereference(tx->sdata->default_multicast_key)))
603 tx->key = key;
604 else if (!is_multicast_ether_addr(hdr->addr1) &&
605 (key = rcu_dereference(tx->sdata->default_unicast_key)))
Johannes Bergd4e46a32007-09-14 11:10:24 -0400606 tx->key = key;
Johannes Berge8f4fb72015-03-20 11:37:36 +0100607 else
Johannes Berg46e6de12012-07-04 18:10:07 +0200608 tx->key = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +0200609
610 if (tx->key) {
Johannes Berg813d7662010-01-17 01:47:58 +0100611 bool skip_hw = false;
612
Johannes Berg011bfcc2007-09-17 01:29:25 -0400613 /* TODO: add threshold stuff again */
Johannes Berg176e4f82007-12-18 15:27:47 +0100614
Johannes Berg97359d12010-08-10 09:46:38 +0200615 switch (tx->key->conf.cipher) {
616 case WLAN_CIPHER_SUITE_WEP40:
617 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg97359d12010-08-10 09:46:38 +0200618 case WLAN_CIPHER_SUITE_TKIP:
Harvey Harrison358c8d92008-07-15 18:44:13 -0700619 if (!ieee80211_is_data_present(hdr->frame_control))
Johannes Berg176e4f82007-12-18 15:27:47 +0100620 tx->key = NULL;
621 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200622 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200623 case WLAN_CIPHER_SUITE_CCMP_256:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200624 case WLAN_CIPHER_SUITE_GCMP:
625 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinenfb733332009-01-08 13:32:00 +0200626 if (!ieee80211_is_data_present(hdr->frame_control) &&
627 !ieee80211_use_mfp(hdr->frame_control, tx->sta,
Masashi Honma46f6b062016-06-22 19:55:20 +0900628 tx->skb) &&
629 !ieee80211_is_group_privacy_action(tx->skb))
Jouni Malinenfb733332009-01-08 13:32:00 +0200630 tx->key = NULL;
Kalle Valo3b43a182010-01-23 20:27:14 +0200631 else
632 skip_hw = (tx->key->conf.flags &
Johannes Berge548c492012-09-04 17:08:23 +0200633 IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
Kalle Valo3b43a182010-01-23 20:27:14 +0200634 ieee80211_is_mgmt(hdr->frame_control);
Jouni Malinenfb733332009-01-08 13:32:00 +0200635 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200636 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200637 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Jouni Malinen8ade5382015-01-24 19:52:09 +0200638 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
639 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200640 if (!ieee80211_is_mgmt(hdr->frame_control))
641 tx->key = NULL;
642 break;
Johannes Berg176e4f82007-12-18 15:27:47 +0100643 }
Johannes Berg813d7662010-01-17 01:47:58 +0100644
Johannes Berge54faf22013-01-29 11:41:38 +0100645 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
646 !ieee80211_is_deauth(hdr->frame_control)))
Johannes Berg95acac62011-07-12 12:30:59 +0200647 return TX_DROP;
648
Johannes Bergf12553e2010-01-22 22:07:59 +0100649 if (!skip_hw && tx->key &&
Johannes Berg382b1652010-01-25 11:36:16 +0100650 tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
Johannes Berg813d7662010-01-17 01:47:58 +0100651 info->control.hw_key = &tx->key->conf;
Felix Fietkau2463ec82020-12-18 20:15:25 +0100652 } else if (ieee80211_is_data_present(hdr->frame_control) && tx->sta &&
Johannes Berga0761a32020-03-26 15:09:42 +0200653 test_sta_flag(tx->sta, WLAN_STA_USES_ENCRYPTION)) {
654 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200655 }
656
Johannes Berg9ae54c82008-01-31 19:48:20 +0100657 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200658}
659
Johannes Bergd9e8a702008-06-30 15:10:44 +0200660static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100661ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200662{
Johannes Berge039fa42008-05-15 12:55:29 +0200663 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200664 struct ieee80211_hdr *hdr = (void *)tx->skb->data;
665 struct ieee80211_supported_band *sband;
Shanyu Zhaoa2c40242010-04-27 11:15:12 -0700666 u32 len;
Johannes Berge6a98542008-10-21 12:40:02 +0200667 struct ieee80211_tx_rate_control txrc;
Felix Fietkau0d528d82013-04-22 16:14:41 +0200668 struct ieee80211_sta_rates *ratetbl = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200669 bool assoc = false;
Johannes Berge6a98542008-10-21 12:40:02 +0200670
671 memset(&txrc, 0, sizeof(txrc));
Johannes Berg8318d782008-01-24 19:38:38 +0100672
Johannes Berg2d565772012-07-23 15:12:51 +0200673 sband = tx->local->hw.wiphy->bands[info->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200674
Shanyu Zhaoa2c40242010-04-27 11:15:12 -0700675 len = min_t(u32, tx->skb->len + FCS_LEN,
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200676 tx->local->hw.wiphy->frag_threshold);
Johannes Berg58d41852007-09-26 17:53:18 +0200677
Johannes Berge6a98542008-10-21 12:40:02 +0200678 /* set up the tx rate control struct we give the RC algo */
Johannes Berg005e4722012-02-26 11:24:35 +0100679 txrc.hw = &tx->local->hw;
Johannes Berge6a98542008-10-21 12:40:02 +0200680 txrc.sband = sband;
681 txrc.bss_conf = &tx->sdata->vif.bss_conf;
682 txrc.skb = tx->skb;
683 txrc.reported_rate.idx = -1;
Johannes Berg2d565772012-07-23 15:12:51 +0200684 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
Felix Fietkau2ffbe6d2013-04-16 13:38:42 +0200685
686 if (tx->sdata->rc_has_mcs_mask[info->band])
687 txrc.rate_idx_mcs_mask =
688 tx->sdata->rc_rateidx_mcs_mask[info->band];
689
Felix Fietkau8f0729b2010-11-11 15:07:23 +0100690 txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -0800691 tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
Bertold Van den Bergh5765f9f2015-08-05 16:02:28 +0200692 tx->sdata->vif.type == NL80211_IFTYPE_ADHOC ||
693 tx->sdata->vif.type == NL80211_IFTYPE_OCB);
Johannes Berg58d41852007-09-26 17:53:18 +0200694
Johannes Berge6a98542008-10-21 12:40:02 +0200695 /* set up RTS protection if desired */
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200696 if (len > tx->local->hw.wiphy->rts_threshold) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200697 txrc.rts = true;
Johannes Berge2ebc742007-07-27 15:43:22 +0200698 }
Johannes Berge6a98542008-10-21 12:40:02 +0200699
Felix Fietkau0d528d82013-04-22 16:14:41 +0200700 info->control.use_rts = txrc.rts;
Felix Fietkau991fec02013-04-16 13:38:43 +0200701 info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
702
Johannes Berge6a98542008-10-21 12:40:02 +0200703 /*
704 * Use short preamble if the BSS can handle it, but not for
705 * management frames unless we know the receiver can handle
706 * that -- the management frame might be to a station that
707 * just wants a probe response.
708 */
709 if (tx->sdata->vif.bss_conf.use_short_preamble &&
710 (ieee80211_is_data(hdr->frame_control) ||
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200711 (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
Felix Fietkau0d528d82013-04-22 16:14:41 +0200712 txrc.short_preamble = true;
713
714 info->control.short_preamble = txrc.short_preamble;
Johannes Berge6a98542008-10-21 12:40:02 +0200715
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +0100716 /* don't ask rate control when rate already injected via radiotap */
717 if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)
718 return TX_CONTINUE;
719
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200720 if (tx->sta)
721 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge6a98542008-10-21 12:40:02 +0200722
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700723 /*
724 * Lets not bother rate control if we're associated and cannot
725 * talk to the sta. This should not happen.
726 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200727 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700728 !rate_usable_index_exists(sband, &tx->sta->sta),
729 "%s: Dropped data frame as no usable bitrate found while "
730 "scanning and associated. Target station: "
731 "%pM on %d GHz band\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100732 tx->sdata->name, hdr->addr1,
Johannes Berg2d565772012-07-23 15:12:51 +0200733 info->band ? 5 : 2))
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700734 return TX_DROP;
735
736 /*
737 * If we're associated with the sta at this point we know we can at
738 * least send the frame at the lowest bit rate.
739 */
Johannes Berge6a98542008-10-21 12:40:02 +0200740 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
741
Felix Fietkau0d528d82013-04-22 16:14:41 +0200742 if (tx->sta && !info->control.skip_table)
743 ratetbl = rcu_dereference(tx->sta->sta.rates);
744
745 if (unlikely(info->control.rates[0].idx < 0)) {
746 if (ratetbl) {
747 struct ieee80211_tx_rate rate = {
748 .idx = ratetbl->rate[0].idx,
749 .flags = ratetbl->rate[0].flags,
750 .count = ratetbl->rate[0].count
751 };
752
753 if (ratetbl->rate[0].idx < 0)
754 return TX_DROP;
755
756 tx->rate = rate;
757 } else {
758 return TX_DROP;
759 }
760 } else {
761 tx->rate = info->control.rates[0];
762 }
Johannes Berge6a98542008-10-21 12:40:02 +0200763
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100764 if (txrc.reported_rate.idx < 0) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200765 txrc.reported_rate = tx->rate;
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100766 if (tx->sta && ieee80211_is_data(hdr->frame_control))
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200767 tx->sta->tx_stats.last_rate = txrc.reported_rate;
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100768 } else if (tx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200769 tx->sta->tx_stats.last_rate = txrc.reported_rate;
Johannes Berge6a98542008-10-21 12:40:02 +0200770
Felix Fietkau0d528d82013-04-22 16:14:41 +0200771 if (ratetbl)
772 return TX_CONTINUE;
773
Johannes Berge6a98542008-10-21 12:40:02 +0200774 if (unlikely(!info->control.rates[0].count))
775 info->control.rates[0].count = 1;
776
Gábor Stefanik99551512009-04-23 19:36:14 +0200777 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
778 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
779 info->control.rates[0].count = 1;
780
Johannes Berg9ae54c82008-01-31 19:48:20 +0100781 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200782}
783
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100784static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
785{
786 u16 *seq = &sta->tid_seq[tid];
787 __le16 ret = cpu_to_le16(*seq);
788
789 /* Increase the sequence number. */
790 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
791
792 return ret;
793}
794
Johannes Bergd9e8a702008-06-30 15:10:44 +0200795static ieee80211_tx_result debug_noinline
Johannes Bergf591fa52008-07-10 11:21:26 +0200796ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
797{
798 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
799 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergf591fa52008-07-10 11:21:26 +0200800 int tid;
801
Johannes Berg25d834e2008-09-12 22:52:47 +0200802 /*
803 * Packet injection may want to control the sequence
804 * number, if we have no matching interface then we
805 * neither assign one ourselves nor ask the driver to.
806 */
Johannes Berg5061b0c2009-07-14 00:33:34 +0200807 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
Johannes Berg25d834e2008-09-12 22:52:47 +0200808 return TX_CONTINUE;
809
Johannes Bergf591fa52008-07-10 11:21:26 +0200810 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
811 return TX_CONTINUE;
812
813 if (ieee80211_hdrlen(hdr->frame_control) < 24)
814 return TX_CONTINUE;
815
Johannes Berg49a59542011-09-29 16:04:41 +0200816 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
817 return TX_CONTINUE;
818
Mathy Vanhoef29c3e952020-07-23 14:01:50 +0400819 if (info->control.flags & IEEE80211_TX_CTRL_NO_SEQNO)
820 return TX_CONTINUE;
821
Johannes Berg94778282008-10-10 13:21:59 +0200822 /*
823 * Anything but QoS data that has a sequence number field
824 * (is long enough) gets a sequence number from the global
Bob Copelandc4c205f2013-08-23 09:35:38 -0400825 * counter. QoS data frames with a multicast destination
826 * also use the global counter (802.11-2012 9.3.2.10).
Johannes Berg94778282008-10-10 13:21:59 +0200827 */
Bob Copelandc4c205f2013-08-23 09:35:38 -0400828 if (!ieee80211_is_data_qos(hdr->frame_control) ||
829 is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg94778282008-10-10 13:21:59 +0200830 /* driver should assign sequence number */
Johannes Bergf591fa52008-07-10 11:21:26 +0200831 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
Johannes Berg94778282008-10-10 13:21:59 +0200832 /* for pure STA mode without beacons, we can do it */
833 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
834 tx->sdata->sequence_number += 0x10;
Johannes Berg79c892b2014-11-21 14:26:31 +0100835 if (tx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200836 tx->sta->tx_stats.msdu[IEEE80211_NUM_TIDS]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200837 return TX_CONTINUE;
838 }
839
840 /*
841 * This should be true for injected/management frames only, for
842 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
843 * above since they are not QoS-data frames.
844 */
845 if (!tx->sta)
846 return TX_CONTINUE;
847
848 /* include per-STA, per-TID sequence counter */
Sara Sharona1f2ba04c2018-02-19 14:48:40 +0200849 tid = ieee80211_get_tid(hdr);
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200850 tx->sta->tx_stats.msdu[tid]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200851
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +0200852 hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
Johannes Bergf591fa52008-07-10 11:21:26 +0200853
854 return TX_CONTINUE;
855}
856
Johannes Berg252b86c2011-11-16 15:28:55 +0100857static int ieee80211_fragment(struct ieee80211_tx_data *tx,
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100858 struct sk_buff *skb, int hdrlen,
859 int frag_threshold)
860{
Johannes Berg252b86c2011-11-16 15:28:55 +0100861 struct ieee80211_local *local = tx->local;
Johannes Berga1a3fce2011-11-16 15:28:56 +0100862 struct ieee80211_tx_info *info;
Johannes Berg252b86c2011-11-16 15:28:55 +0100863 struct sk_buff *tmp;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100864 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
865 int pos = hdrlen + per_fragm;
866 int rem = skb->len - hdrlen - per_fragm;
867
868 if (WARN_ON(rem < 0))
869 return -EINVAL;
870
Johannes Berg252b86c2011-11-16 15:28:55 +0100871 /* first fragment was already added to queue by caller */
872
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100873 while (rem) {
874 int fraglen = per_fragm;
875
876 if (fraglen > rem)
877 fraglen = rem;
878 rem -= fraglen;
879 tmp = dev_alloc_skb(local->tx_headroom +
880 frag_threshold +
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200881 tx->sdata->encrypt_headroom +
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100882 IEEE80211_ENCRYPT_TAILROOM);
883 if (!tmp)
884 return -ENOMEM;
Johannes Berg252b86c2011-11-16 15:28:55 +0100885
886 __skb_queue_tail(&tx->skbs, tmp);
887
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200888 skb_reserve(tmp,
889 local->tx_headroom + tx->sdata->encrypt_headroom);
890
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100891 /* copy control information */
892 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
Johannes Berga1a3fce2011-11-16 15:28:56 +0100893
894 info = IEEE80211_SKB_CB(tmp);
895 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
896 IEEE80211_TX_CTL_FIRST_FRAGMENT);
897
898 if (rem)
899 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
900
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100901 skb_copy_queue_mapping(tmp, skb);
902 tmp->priority = skb->priority;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100903 tmp->dev = skb->dev;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100904
905 /* copy header and data */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200906 skb_put_data(tmp, skb->data, hdrlen);
907 skb_put_data(tmp, skb->data + pos, fraglen);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100908
909 pos += fraglen;
910 }
911
Johannes Berg252b86c2011-11-16 15:28:55 +0100912 /* adjust first fragment's length */
Johannes Berg338f9772014-02-01 00:16:23 +0100913 skb_trim(skb, hdrlen + per_fragm);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100914 return 0;
915}
916
Johannes Bergf591fa52008-07-10 11:21:26 +0200917static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +0200918ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
919{
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100920 struct sk_buff *skb = tx->skb;
921 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
922 struct ieee80211_hdr *hdr = (void *)skb->data;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200923 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100924 int hdrlen;
925 int fragnum;
Johannes Berge2454942008-05-15 12:55:28 +0200926
Johannes Berg252b86c2011-11-16 15:28:55 +0100927 /* no matter what happens, tx->skb moves to tx->skbs */
928 __skb_queue_tail(&tx->skbs, skb);
929 tx->skb = NULL;
930
Johannes Berga26eb272011-10-07 14:01:25 +0200931 if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
932 return TX_CONTINUE;
933
Sara Sharonf3fe4e92016-10-18 23:12:11 +0300934 if (ieee80211_hw_check(&tx->local->hw, SUPPORTS_TX_FRAG))
Johannes Berge2454942008-05-15 12:55:28 +0200935 return TX_CONTINUE;
936
Johannes Bergeefce912008-05-17 00:57:13 +0200937 /*
938 * Warn when submitting a fragmented A-MPDU frame and drop it.
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200939 * This scenario is handled in ieee80211_tx_prepare but extra
Ron Rindjunsky8d5e0d52008-06-12 15:42:29 +0300940 * caution taken here as fragmented ampdu may cause Tx stop.
Johannes Bergeefce912008-05-17 00:57:13 +0200941 */
Sujith8b30b1f2008-10-24 09:55:27 +0530942 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
Johannes Bergeefce912008-05-17 00:57:13 +0200943 return TX_DROP;
944
Harvey Harrison065e96052008-06-22 16:45:27 -0700945 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berge2454942008-05-15 12:55:28 +0200946
Johannes Berga26eb272011-10-07 14:01:25 +0200947 /* internal error, why isn't DONTFRAG set? */
Johannes Berg8ccd8f22009-04-29 23:35:56 +0200948 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100949 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200950
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100951 /*
952 * Now fragment the frame. This will allocate all the fragments and
953 * chain them (using skb as the first fragment) to skb->next.
954 * During transmission, we will remove the successfully transmitted
955 * fragments from this list. When the low-level driver rejects one
956 * of the fragments then we will simply pretend to accept the skb
957 * but store it away as pending.
958 */
Johannes Berg252b86c2011-11-16 15:28:55 +0100959 if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100960 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200961
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100962 /* update duration/seq/flags of fragments */
963 fragnum = 0;
Johannes Berg252b86c2011-11-16 15:28:55 +0100964
965 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100966 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
Johannes Berge2454942008-05-15 12:55:28 +0200967
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100968 hdr = (void *)skb->data;
969 info = IEEE80211_SKB_CB(skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200970
Johannes Berg252b86c2011-11-16 15:28:55 +0100971 if (!skb_queue_is_last(&tx->skbs, skb)) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100972 hdr->frame_control |= morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200973 /*
974 * No multi-rate retries for fragmented frames, that
975 * would completely throw off the NAV at other STAs.
976 */
977 info->control.rates[1].idx = -1;
978 info->control.rates[2].idx = -1;
979 info->control.rates[3].idx = -1;
Thomas Huehne3e1a0b2012-07-02 19:46:16 +0200980 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
Johannes Berge6a98542008-10-21 12:40:02 +0200981 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100982 } else {
983 hdr->frame_control &= ~morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200984 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100985 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
986 fragnum++;
Johannes Berg252b86c2011-11-16 15:28:55 +0100987 }
Johannes Berge2454942008-05-15 12:55:28 +0200988
989 return TX_CONTINUE;
Johannes Berge2454942008-05-15 12:55:28 +0200990}
991
Johannes Bergd9e8a702008-06-30 15:10:44 +0200992static ieee80211_tx_result debug_noinline
Johannes Bergfeff1f22009-08-10 16:01:54 +0200993ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
994{
Johannes Berg252b86c2011-11-16 15:28:55 +0100995 struct sk_buff *skb;
Johannes Berg560d2682013-02-05 10:55:21 +0100996 int ac = -1;
Johannes Bergfeff1f22009-08-10 16:01:54 +0200997
998 if (!tx->sta)
999 return TX_CONTINUE;
1000
Johannes Berg252b86c2011-11-16 15:28:55 +01001001 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg560d2682013-02-05 10:55:21 +01001002 ac = skb_get_queue_mapping(skb);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001003 tx->sta->tx_stats.bytes[ac] += skb->len;
Johannes Berg252b86c2011-11-16 15:28:55 +01001004 }
Johannes Berg560d2682013-02-05 10:55:21 +01001005 if (ac >= 0)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001006 tx->sta->tx_stats.packets[ac]++;
Johannes Bergfeff1f22009-08-10 16:01:54 +02001007
1008 return TX_CONTINUE;
1009}
1010
1011static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +02001012ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
1013{
1014 if (!tx->key)
1015 return TX_CONTINUE;
1016
Johannes Berg97359d12010-08-10 09:46:38 +02001017 switch (tx->key->conf.cipher) {
1018 case WLAN_CIPHER_SUITE_WEP40:
1019 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge2454942008-05-15 12:55:28 +02001020 return ieee80211_crypto_wep_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001021 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berge2454942008-05-15 12:55:28 +02001022 return ieee80211_crypto_tkip_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001023 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001024 return ieee80211_crypto_ccmp_encrypt(
1025 tx, IEEE80211_CCMP_MIC_LEN);
1026 case WLAN_CIPHER_SUITE_CCMP_256:
1027 return ieee80211_crypto_ccmp_encrypt(
1028 tx, IEEE80211_CCMP_256_MIC_LEN);
Johannes Berg97359d12010-08-10 09:46:38 +02001029 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001030 return ieee80211_crypto_aes_cmac_encrypt(tx);
Jouni Malinen56c52da2015-01-24 19:52:08 +02001031 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1032 return ieee80211_crypto_aes_cmac_256_encrypt(tx);
Jouni Malinen8ade5382015-01-24 19:52:09 +02001033 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1034 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1035 return ieee80211_crypto_aes_gmac_encrypt(tx);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001036 case WLAN_CIPHER_SUITE_GCMP:
1037 case WLAN_CIPHER_SUITE_GCMP_256:
1038 return ieee80211_crypto_gcmp_encrypt(tx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +03001039 default:
Yoni Divinskyd32a1022012-01-16 15:18:59 +02001040 return ieee80211_crypto_hw_encrypt(tx);
Johannes Berge2454942008-05-15 12:55:28 +02001041 }
1042
Johannes Berge2454942008-05-15 12:55:28 +02001043 return TX_DROP;
1044}
1045
Johannes Bergd9e8a702008-06-30 15:10:44 +02001046static ieee80211_tx_result debug_noinline
Johannes Berg03f93c32008-06-25 14:36:27 +03001047ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1048{
Johannes Berg252b86c2011-11-16 15:28:55 +01001049 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001050 struct ieee80211_hdr *hdr;
1051 int next_len;
1052 bool group_addr;
Johannes Berg03f93c32008-06-25 14:36:27 +03001053
Johannes Berg252b86c2011-11-16 15:28:55 +01001054 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001055 hdr = (void *) skb->data;
Jouni Malinen7e0aae42009-05-19 19:25:58 +03001056 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1057 break; /* must not overwrite AID */
Johannes Berg252b86c2011-11-16 15:28:55 +01001058 if (!skb_queue_is_last(&tx->skbs, skb)) {
1059 struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1060 next_len = next->len;
1061 } else
1062 next_len = 0;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001063 group_addr = is_multicast_ether_addr(hdr->addr1);
Johannes Berg03f93c32008-06-25 14:36:27 +03001064
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001065 hdr->duration_id =
Johannes Berg252b86c2011-11-16 15:28:55 +01001066 ieee80211_duration(tx, skb, group_addr, next_len);
1067 }
Johannes Berg03f93c32008-06-25 14:36:27 +03001068
1069 return TX_CONTINUE;
1070}
1071
Johannes Berge2ebc742007-07-27 15:43:22 +02001072/* actual transmit path */
1073
Johannes Berga622ab72010-06-10 10:21:39 +02001074static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1075 struct sk_buff *skb,
1076 struct ieee80211_tx_info *info,
1077 struct tid_ampdu_tx *tid_tx,
1078 int tid)
1079{
1080 bool queued = false;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001081 bool reset_agg_timer = false;
Helmut Schaaaa454582012-03-07 17:20:30 +01001082 struct sk_buff *purge_skb = NULL;
Johannes Berga622ab72010-06-10 10:21:39 +02001083
1084 if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1085 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001086 reset_agg_timer = true;
Johannes Berg0ab33702010-06-10 10:21:42 +02001087 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1088 /*
1089 * nothing -- this aggregation session is being started
1090 * but that might still fail with the driver
1091 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001092 } else if (!tx->sta->sta.txq[tid]) {
Johannes Berga622ab72010-06-10 10:21:39 +02001093 spin_lock(&tx->sta->lock);
1094 /*
1095 * Need to re-check now, because we may get here
1096 *
1097 * 1) in the window during which the setup is actually
1098 * already done, but not marked yet because not all
1099 * packets are spliced over to the driver pending
1100 * queue yet -- if this happened we acquire the lock
1101 * either before or after the splice happens, but
1102 * need to recheck which of these cases happened.
1103 *
1104 * 2) during session teardown, if the OPERATIONAL bit
1105 * was cleared due to the teardown but the pointer
1106 * hasn't been assigned NULL yet (or we loaded it
1107 * before it was assigned) -- in this case it may
1108 * now be NULL which means we should just let the
1109 * packet pass through because splicing the frames
1110 * back is already done.
1111 */
Johannes Berg40b275b2011-05-13 14:15:49 +02001112 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
Johannes Berga622ab72010-06-10 10:21:39 +02001113
1114 if (!tid_tx) {
1115 /* do nothing, let packet pass through */
1116 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1117 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001118 reset_agg_timer = true;
Johannes Berga622ab72010-06-10 10:21:39 +02001119 } else {
1120 queued = true;
Emmanuel Grumbachf6d46712016-03-17 16:51:42 +02001121 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
1122 clear_sta_flag(tx->sta, WLAN_STA_SP);
1123 ps_dbg(tx->sta->sdata,
1124 "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
1125 tx->sta->sta.addr, tx->sta->sta.aid);
1126 }
Johannes Berga622ab72010-06-10 10:21:39 +02001127 info->control.vif = &tx->sdata->vif;
Felix Fietkaucc20ff22020-09-08 14:36:57 +02001128 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
Emmanuel Grumbachfacde7f2016-03-17 16:51:41 +02001129 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berga622ab72010-06-10 10:21:39 +02001130 __skb_queue_tail(&tid_tx->pending, skb);
Helmut Schaaaa454582012-03-07 17:20:30 +01001131 if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1132 purge_skb = __skb_dequeue(&tid_tx->pending);
Johannes Berga622ab72010-06-10 10:21:39 +02001133 }
1134 spin_unlock(&tx->sta->lock);
Helmut Schaaaa454582012-03-07 17:20:30 +01001135
1136 if (purge_skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001137 ieee80211_free_txskb(&tx->local->hw, purge_skb);
Johannes Berga622ab72010-06-10 10:21:39 +02001138 }
1139
Nikolay Martynov285fa692011-11-22 21:50:28 -05001140 /* reset session timer */
Sara Sharon914eac22018-04-20 13:49:19 +03001141 if (reset_agg_timer)
Felix Fietkau12d3952f2012-03-18 22:58:06 +01001142 tid_tx->last_tx = jiffies;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001143
Johannes Berga622ab72010-06-10 10:21:39 +02001144 return queued;
1145}
1146
Johannes Berg58d41852007-09-26 17:53:18 +02001147/*
1148 * initialises @tx
Johannes Berg7c107702015-03-20 14:18:27 +01001149 * pass %NULL for the station if unknown, a valid pointer if known
1150 * or an ERR_PTR() if the station is known not to exist
Johannes Berg58d41852007-09-26 17:53:18 +02001151 */
Johannes Berg9ae54c82008-01-31 19:48:20 +01001152static ieee80211_tx_result
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001153ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1154 struct ieee80211_tx_data *tx,
Johannes Berg7c107702015-03-20 14:18:27 +01001155 struct sta_info *sta, struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02001156{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001157 struct ieee80211_local *local = sdata->local;
Johannes Berg58d41852007-09-26 17:53:18 +02001158 struct ieee80211_hdr *hdr;
Johannes Berge039fa42008-05-15 12:55:29 +02001159 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg68f2b512011-10-07 14:01:24 +02001160 int tid;
Johannes Berge2ebc742007-07-27 15:43:22 +02001161
1162 memset(tx, 0, sizeof(*tx));
1163 tx->skb = skb;
Johannes Berge2ebc742007-07-27 15:43:22 +02001164 tx->local = local;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001165 tx->sdata = sdata;
Johannes Berg252b86c2011-11-16 15:28:55 +01001166 __skb_queue_head_init(&tx->skbs);
Johannes Berge2ebc742007-07-27 15:43:22 +02001167
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001168 /*
1169 * If this flag is set to true anywhere, and we get here,
1170 * we are doing the needed processing, so remove the flag
1171 * now.
1172 */
Felix Fietkaucc20ff22020-09-08 14:36:57 +02001173 info->control.flags &= ~IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001174
Johannes Berg58d41852007-09-26 17:53:18 +02001175 hdr = (struct ieee80211_hdr *) skb->data;
1176
Johannes Berg7c107702015-03-20 14:18:27 +01001177 if (likely(sta)) {
1178 if (!IS_ERR(sta))
1179 tx->sta = sta;
1180 } else {
1181 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1182 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1183 if (!tx->sta && sdata->wdev.use_4addr)
1184 return TX_DROP;
Markus Theil10cb8e62021-02-06 12:51:12 +01001185 } else if (tx->sdata->control_port_protocol == tx->skb->protocol) {
Johannes Berg7c107702015-03-20 14:18:27 +01001186 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1187 }
1188 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
1189 tx->sta = sta_info_get(sdata, hdr->addr1);
Felix Fietkau3f0e0b22010-01-08 18:15:13 +01001190 }
Johannes Berg58d41852007-09-26 17:53:18 +02001191
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001192 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
Johannes Berg49a59542011-09-29 16:04:41 +02001193 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001194 ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
1195 !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001196 struct tid_ampdu_tx *tid_tx;
1197
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001198 tid = ieee80211_get_tid(hdr);
Sujith8b30b1f2008-10-24 09:55:27 +05301199
Johannes Berga622ab72010-06-10 10:21:39 +02001200 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1201 if (tid_tx) {
1202 bool queued;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001203
Johannes Berga622ab72010-06-10 10:21:39 +02001204 queued = ieee80211_tx_prep_agg(tx, skb, info,
1205 tid_tx, tid);
1206
1207 if (unlikely(queued))
1208 return TX_QUEUED;
1209 }
Sujith8b30b1f2008-10-24 09:55:27 +05301210 }
1211
Jiri Slabybadffb72007-08-28 17:01:54 -04001212 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001213 tx->flags &= ~IEEE80211_TX_UNICAST;
Johannes Berge039fa42008-05-15 12:55:29 +02001214 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Simon Wunderlich6fd67e92011-11-18 14:20:42 +01001215 } else
Johannes Berg5cf121c2008-02-25 16:27:43 +01001216 tx->flags |= IEEE80211_TX_UNICAST;
Johannes Berg58d41852007-09-26 17:53:18 +02001217
Johannes Berga26eb272011-10-07 14:01:25 +02001218 if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1219 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1220 skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1221 info->flags & IEEE80211_TX_CTL_AMPDU)
1222 info->flags |= IEEE80211_TX_CTL_DONTFRAG;
Johannes Berg58d41852007-09-26 17:53:18 +02001223 }
1224
Johannes Berge2ebc742007-07-27 15:43:22 +02001225 if (!tx->sta)
Johannes Berge039fa42008-05-15 12:55:29 +02001226 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Felix Fietkauf7418bc2015-09-24 14:59:49 +02001227 else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
Johannes Berge039fa42008-05-15 12:55:29 +02001228 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Felix Fietkauf7418bc2015-09-24 14:59:49 +02001229 ieee80211_check_fast_xmit(tx->sta);
1230 }
Johannes Berg58d41852007-09-26 17:53:18 +02001231
Johannes Berge039fa42008-05-15 12:55:29 +02001232 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001233
Johannes Berg9ae54c82008-01-31 19:48:20 +01001234 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001235}
1236
Michal Kazior80a83cf2016-05-19 10:37:48 +02001237static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
1238 struct ieee80211_vif *vif,
Michal Kaziordbef5362017-01-13 13:32:51 +01001239 struct sta_info *sta,
Michal Kazior80a83cf2016-05-19 10:37:48 +02001240 struct sk_buff *skb)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001241{
1242 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001243 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001244 struct ieee80211_txq *txq = NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001245
Felix Fietkauc3732a72016-02-28 15:19:53 +01001246 if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
1247 (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
Michal Kazior80a83cf2016-05-19 10:37:48 +02001248 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001249
Felix Fietkaucc20ff22020-09-08 14:36:57 +02001250 if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) &&
John Crispin50ff4772019-11-25 11:04:37 +01001251 unlikely(!ieee80211_is_data_present(hdr->frame_control))) {
Johannes Bergadf8ed02018-08-31 11:31:08 +03001252 if ((!ieee80211_is_mgmt(hdr->frame_control) ||
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001253 ieee80211_is_bufferable_mmpdu(hdr->frame_control) ||
1254 vif->type == NL80211_IFTYPE_STATION) &&
Johannes Bergadf8ed02018-08-31 11:31:08 +03001255 sta && sta->uploaded) {
1256 /*
1257 * This will be NULL if the driver didn't set the
1258 * opt-in hardware flag.
1259 */
1260 txq = sta->sta.txq[IEEE80211_NUM_TIDS];
1261 }
1262 } else if (sta) {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001263 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1264
Michal Kaziordbef5362017-01-13 13:32:51 +01001265 if (!sta->uploaded)
1266 return NULL;
1267
1268 txq = sta->sta.txq[tid];
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001269 } else if (vif) {
1270 txq = vif->txq;
1271 }
1272
1273 if (!txq)
Michal Kazior80a83cf2016-05-19 10:37:48 +02001274 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001275
Michal Kazior80a83cf2016-05-19 10:37:48 +02001276 return to_txq_info(txq);
1277}
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001278
Michal Kazior5caa3282016-05-19 10:37:51 +02001279static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
1280{
1281 IEEE80211_SKB_CB(skb)->control.enqueue_time = codel_get_time();
1282}
1283
Michal Kazior5caa3282016-05-19 10:37:51 +02001284static u32 codel_skb_len_func(const struct sk_buff *skb)
1285{
1286 return skb->len;
1287}
1288
1289static codel_time_t codel_skb_time_func(const struct sk_buff *skb)
1290{
1291 const struct ieee80211_tx_info *info;
1292
1293 info = (const struct ieee80211_tx_info *)skb->cb;
1294 return info->control.enqueue_time;
1295}
1296
1297static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars,
1298 void *ctx)
1299{
1300 struct ieee80211_local *local;
1301 struct txq_info *txqi;
1302 struct fq *fq;
1303 struct fq_flow *flow;
1304
1305 txqi = ctx;
1306 local = vif_to_sdata(txqi->txq.vif)->local;
1307 fq = &local->fq;
1308
1309 if (cvars == &txqi->def_cvars)
Felix Fietkaubf9009b2020-12-18 19:47:14 +01001310 flow = &txqi->tin.default_flow;
Michal Kazior5caa3282016-05-19 10:37:51 +02001311 else
1312 flow = &fq->flows[cvars - local->cvars];
1313
1314 return fq_flow_dequeue(fq, flow);
1315}
1316
1317static void codel_drop_func(struct sk_buff *skb,
1318 void *ctx)
1319{
1320 struct ieee80211_local *local;
1321 struct ieee80211_hw *hw;
1322 struct txq_info *txqi;
1323
1324 txqi = ctx;
1325 local = vif_to_sdata(txqi->txq.vif)->local;
1326 hw = &local->hw;
1327
1328 ieee80211_free_txskb(hw, skb);
1329}
1330
Michal Kaziorfa962b92016-05-19 10:37:49 +02001331static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
1332 struct fq_tin *tin,
1333 struct fq_flow *flow)
1334{
Michal Kazior5caa3282016-05-19 10:37:51 +02001335 struct ieee80211_local *local;
1336 struct txq_info *txqi;
1337 struct codel_vars *cvars;
1338 struct codel_params *cparams;
1339 struct codel_stats *cstats;
1340
1341 local = container_of(fq, struct ieee80211_local, fq);
1342 txqi = container_of(tin, struct txq_info, tin);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001343 cstats = &txqi->cstats;
Michal Kazior5caa3282016-05-19 10:37:51 +02001344
Toke Høiland-Jørgensen484a54c2017-04-06 11:38:26 +02001345 if (txqi->txq.sta) {
1346 struct sta_info *sta = container_of(txqi->txq.sta,
1347 struct sta_info, sta);
1348 cparams = &sta->cparams;
1349 } else {
1350 cparams = &local->cparams;
1351 }
1352
Felix Fietkaubf9009b2020-12-18 19:47:14 +01001353 if (flow == &tin->default_flow)
Michal Kazior5caa3282016-05-19 10:37:51 +02001354 cvars = &txqi->def_cvars;
1355 else
1356 cvars = &local->cvars[flow - fq->flows];
1357
1358 return codel_dequeue(txqi,
1359 &flow->backlog,
1360 cparams,
1361 cvars,
1362 cstats,
1363 codel_skb_len_func,
1364 codel_skb_time_func,
1365 codel_drop_func,
1366 codel_dequeue_func);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001367}
1368
1369static void fq_skb_free_func(struct fq *fq,
1370 struct fq_tin *tin,
1371 struct fq_flow *flow,
1372 struct sk_buff *skb)
1373{
1374 struct ieee80211_local *local;
1375
1376 local = container_of(fq, struct ieee80211_local, fq);
1377 ieee80211_free_txskb(&local->hw, skb);
1378}
1379
Michal Kazior80a83cf2016-05-19 10:37:48 +02001380static void ieee80211_txq_enqueue(struct ieee80211_local *local,
1381 struct txq_info *txqi,
1382 struct sk_buff *skb)
1383{
Michal Kaziorfa962b92016-05-19 10:37:49 +02001384 struct fq *fq = &local->fq;
1385 struct fq_tin *tin = &txqi->tin;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001386 u32 flow_idx = fq_flow_idx(fq, skb);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001387
Michal Kazior5caa3282016-05-19 10:37:51 +02001388 ieee80211_set_skb_enqueue_time(skb);
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001389
1390 spin_lock_bh(&fq->lock);
Johannes Berg73bc9e02021-03-19 23:28:01 +01001391 /*
1392 * For management frames, don't really apply codel etc.,
1393 * we don't want to apply any shaping or anything we just
1394 * want to simplify the driver API by having them on the
1395 * txqi.
1396 */
1397 if (unlikely(txqi->txq.tid == IEEE80211_NUM_TIDS))
1398 __skb_queue_tail(&txqi->frags, skb);
1399 else
1400 fq_tin_enqueue(fq, tin, flow_idx, skb,
1401 fq_skb_free_func);
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001402 spin_unlock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001403}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001404
Johannes Berg2a9e2572017-10-06 11:53:33 +02001405static bool fq_vlan_filter_func(struct fq *fq, struct fq_tin *tin,
1406 struct fq_flow *flow, struct sk_buff *skb,
1407 void *data)
1408{
1409 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1410
1411 return info->control.vif == data;
1412}
1413
1414void ieee80211_txq_remove_vlan(struct ieee80211_local *local,
1415 struct ieee80211_sub_if_data *sdata)
1416{
1417 struct fq *fq = &local->fq;
1418 struct txq_info *txqi;
1419 struct fq_tin *tin;
1420 struct ieee80211_sub_if_data *ap;
1421
1422 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1423 return;
1424
1425 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1426
1427 if (!ap->vif.txq)
1428 return;
1429
1430 txqi = to_txq_info(ap->vif.txq);
1431 tin = &txqi->tin;
1432
1433 spin_lock_bh(&fq->lock);
1434 fq_tin_filter(fq, tin, fq_vlan_filter_func, &sdata->vif,
1435 fq_skb_free_func);
1436 spin_unlock_bh(&fq->lock);
1437}
1438
Michal Kaziorfa962b92016-05-19 10:37:49 +02001439void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
1440 struct sta_info *sta,
1441 struct txq_info *txqi, int tid)
1442{
1443 fq_tin_init(&txqi->tin);
Michal Kazior5caa3282016-05-19 10:37:51 +02001444 codel_vars_init(&txqi->def_cvars);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001445 codel_stats_init(&txqi->cstats);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001446 __skb_queue_head_init(&txqi->frags);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001447 INIT_LIST_HEAD(&txqi->schedule_order);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001448
1449 txqi->txq.vif = &sdata->vif;
1450
Johannes Bergadf8ed02018-08-31 11:31:08 +03001451 if (!sta) {
Michal Kaziorfa962b92016-05-19 10:37:49 +02001452 sdata->vif.txq = &txqi->txq;
1453 txqi->txq.tid = 0;
1454 txqi->txq.ac = IEEE80211_AC_BE;
Johannes Bergadf8ed02018-08-31 11:31:08 +03001455
1456 return;
Michal Kazior80a83cf2016-05-19 10:37:48 +02001457 }
Johannes Bergadf8ed02018-08-31 11:31:08 +03001458
1459 if (tid == IEEE80211_NUM_TIDS) {
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001460 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1461 /* Drivers need to opt in to the management MPDU TXQ */
1462 if (!ieee80211_hw_check(&sdata->local->hw,
1463 STA_MMPDU_TXQ))
1464 return;
1465 } else if (!ieee80211_hw_check(&sdata->local->hw,
1466 BUFF_MMPDU_TXQ)) {
1467 /* Drivers need to opt in to the bufferable MMPDU TXQ */
Johannes Bergadf8ed02018-08-31 11:31:08 +03001468 return;
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001469 }
Johannes Bergadf8ed02018-08-31 11:31:08 +03001470 txqi->txq.ac = IEEE80211_AC_VO;
1471 } else {
1472 txqi->txq.ac = ieee80211_ac_from_tid(tid);
1473 }
1474
1475 txqi->txq.sta = &sta->sta;
1476 txqi->txq.tid = tid;
1477 sta->sta.txq[tid] = &txqi->txq;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001478}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001479
Michal Kaziorfa962b92016-05-19 10:37:49 +02001480void ieee80211_txq_purge(struct ieee80211_local *local,
1481 struct txq_info *txqi)
1482{
1483 struct fq *fq = &local->fq;
1484 struct fq_tin *tin = &txqi->tin;
1485
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08001486 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001487 fq_tin_reset(fq, tin, fq_skb_free_func);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001488 ieee80211_purge_tx_queue(&local->hw, &txqi->frags);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08001489 spin_unlock_bh(&fq->lock);
1490
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001491 spin_lock_bh(&local->active_txq_lock[txqi->txq.ac]);
1492 list_del_init(&txqi->schedule_order);
1493 spin_unlock_bh(&local->active_txq_lock[txqi->txq.ac]);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001494}
1495
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001496void ieee80211_txq_set_params(struct ieee80211_local *local)
1497{
1498 if (local->hw.wiphy->txq_limit)
1499 local->fq.limit = local->hw.wiphy->txq_limit;
1500 else
1501 local->hw.wiphy->txq_limit = local->fq.limit;
1502
1503 if (local->hw.wiphy->txq_memory_limit)
1504 local->fq.memory_limit = local->hw.wiphy->txq_memory_limit;
1505 else
1506 local->hw.wiphy->txq_memory_limit = local->fq.memory_limit;
1507
1508 if (local->hw.wiphy->txq_quantum)
1509 local->fq.quantum = local->hw.wiphy->txq_quantum;
1510 else
1511 local->hw.wiphy->txq_quantum = local->fq.quantum;
1512}
1513
Michal Kaziorfa962b92016-05-19 10:37:49 +02001514int ieee80211_txq_setup_flows(struct ieee80211_local *local)
1515{
1516 struct fq *fq = &local->fq;
1517 int ret;
Michal Kazior5caa3282016-05-19 10:37:51 +02001518 int i;
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001519 bool supp_vht = false;
1520 enum nl80211_band band;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001521
1522 if (!local->ops->wake_tx_queue)
1523 return 0;
1524
1525 ret = fq_init(fq, 4096);
1526 if (ret)
1527 return ret;
1528
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001529 /*
1530 * If the hardware doesn't support VHT, it is safe to limit the maximum
1531 * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
1532 */
1533 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1534 struct ieee80211_supported_band *sband;
1535
1536 sband = local->hw.wiphy->bands[band];
1537 if (!sband)
1538 continue;
1539
1540 supp_vht = supp_vht || sband->vht_cap.vht_supported;
1541 }
1542
1543 if (!supp_vht)
1544 fq->memory_limit = 4 << 20; /* 4 Mbytes */
1545
Michal Kazior5caa3282016-05-19 10:37:51 +02001546 codel_params_init(&local->cparams);
Michal Kazior5caa3282016-05-19 10:37:51 +02001547 local->cparams.interval = MS2TIME(100);
1548 local->cparams.target = MS2TIME(20);
1549 local->cparams.ecn = true;
1550
1551 local->cvars = kcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
1552 GFP_KERNEL);
1553 if (!local->cvars) {
Michal Kazior59a7c822016-06-29 14:00:34 +02001554 spin_lock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001555 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001556 spin_unlock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001557 return -ENOMEM;
1558 }
1559
1560 for (i = 0; i < fq->flows_cnt; i++)
1561 codel_vars_init(&local->cvars[i]);
1562
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001563 ieee80211_txq_set_params(local);
1564
Michal Kaziorfa962b92016-05-19 10:37:49 +02001565 return 0;
1566}
1567
1568void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1569{
1570 struct fq *fq = &local->fq;
1571
1572 if (!local->ops->wake_tx_queue)
1573 return;
1574
Michal Kazior5caa3282016-05-19 10:37:51 +02001575 kfree(local->cvars);
1576 local->cvars = NULL;
1577
Michal Kazior59a7c822016-06-29 14:00:34 +02001578 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001579 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001580 spin_unlock_bh(&fq->lock);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001581}
1582
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001583static bool ieee80211_queue_skb(struct ieee80211_local *local,
1584 struct ieee80211_sub_if_data *sdata,
1585 struct sta_info *sta,
1586 struct sk_buff *skb)
1587{
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001588 struct ieee80211_vif *vif;
1589 struct txq_info *txqi;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001590
1591 if (!local->ops->wake_tx_queue ||
1592 sdata->vif.type == NL80211_IFTYPE_MONITOR)
1593 return false;
1594
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001595 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1596 sdata = container_of(sdata->bss,
1597 struct ieee80211_sub_if_data, u.ap);
1598
1599 vif = &sdata->vif;
Michal Kaziordbef5362017-01-13 13:32:51 +01001600 txqi = ieee80211_get_txq(local, vif, sta, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001601
1602 if (!txqi)
1603 return false;
1604
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001605 ieee80211_txq_enqueue(local, txqi, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001606
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001607 schedule_and_wake_txq(local, txqi);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001608
1609 return true;
1610}
1611
Johannes Berg11127e92011-11-16 16:02:47 +01001612static bool ieee80211_tx_frags(struct ieee80211_local *local,
1613 struct ieee80211_vif *vif,
Johannes Berg4fd03282019-10-01 23:26:35 +02001614 struct sta_info *sta,
Johannes Berg11127e92011-11-16 16:02:47 +01001615 struct sk_buff_head *skbs,
1616 bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001617{
Michal Kazior80a83cf2016-05-19 10:37:48 +02001618 struct ieee80211_tx_control control = {};
Johannes Berg252b86c2011-11-16 15:28:55 +01001619 struct sk_buff *skb, *tmp;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001620 unsigned long flags;
Johannes Berge2ebc742007-07-27 15:43:22 +02001621
Johannes Berg252b86c2011-11-16 15:28:55 +01001622 skb_queue_walk_safe(skbs, skb, tmp) {
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001623 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1624 int q = info->hw_queue;
1625
1626#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1627 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1628 __skb_unlink(skb, skbs);
Felix Fietkauc3e77242012-10-08 14:39:33 +02001629 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001630 continue;
1631 }
1632#endif
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001633
1634 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001635 if (local->queue_stop_reasons[q] ||
Johannes Berg7bb45682011-02-24 14:42:06 +01001636 (!txpending && !skb_queue_empty(&local->pending[q]))) {
Seth Forshee6c17b772013-02-11 11:21:07 -06001637 if (unlikely(info->flags &
Seth Forsheea7679ed2013-02-25 14:58:05 -06001638 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1639 if (local->queue_stop_reasons[q] &
1640 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1641 /*
1642 * Drop off-channel frames if queues
1643 * are stopped for any reason other
1644 * than off-channel operation. Never
1645 * queue them.
1646 */
1647 spin_unlock_irqrestore(
1648 &local->queue_stop_reason_lock,
1649 flags);
1650 ieee80211_purge_tx_queue(&local->hw,
1651 skbs);
1652 return true;
1653 }
1654 } else {
1655
Seth Forshee6c17b772013-02-11 11:21:07 -06001656 /*
Seth Forsheea7679ed2013-02-25 14:58:05 -06001657 * Since queue is stopped, queue up frames for
1658 * later transmission from the tx-pending
1659 * tasklet when the queue is woken again.
Seth Forshee6c17b772013-02-11 11:21:07 -06001660 */
Seth Forsheea7679ed2013-02-25 14:58:05 -06001661 if (txpending)
1662 skb_queue_splice_init(skbs,
1663 &local->pending[q]);
1664 else
1665 skb_queue_splice_tail_init(skbs,
1666 &local->pending[q]);
1667
1668 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1669 flags);
1670 return false;
Seth Forshee6c17b772013-02-11 11:21:07 -06001671 }
Johannes Berg7bb45682011-02-24 14:42:06 +01001672 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001673 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Tomas Winklerf8e79dd2008-07-24 18:46:44 +03001674
Johannes Berg11127e92011-11-16 16:02:47 +01001675 info->control.vif = vif;
Johannes Berg4fd03282019-10-01 23:26:35 +02001676 control.sta = sta ? &sta->sta : NULL;
Johannes Bergec25acc2010-07-22 17:11:28 +02001677
Johannes Berg252b86c2011-11-16 15:28:55 +01001678 __skb_unlink(skb, skbs);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001679 drv_tx(local, &control, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001680 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001681
Johannes Berg11127e92011-11-16 16:02:47 +01001682 return true;
1683}
1684
1685/*
1686 * Returns false if the frame couldn't be transmitted but was queued instead.
1687 */
1688static bool __ieee80211_tx(struct ieee80211_local *local,
1689 struct sk_buff_head *skbs, int led_len,
1690 struct sta_info *sta, bool txpending)
1691{
1692 struct ieee80211_tx_info *info;
1693 struct ieee80211_sub_if_data *sdata;
1694 struct ieee80211_vif *vif;
Johannes Berg11127e92011-11-16 16:02:47 +01001695 struct sk_buff *skb;
Colin Ian King958574c2021-03-28 22:37:29 +01001696 bool result;
Johannes Berg11127e92011-11-16 16:02:47 +01001697 __le16 fc;
1698
1699 if (WARN_ON(skb_queue_empty(skbs)))
1700 return true;
1701
1702 skb = skb_peek(skbs);
1703 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1704 info = IEEE80211_SKB_CB(skb);
1705 sdata = vif_to_sdata(info->control.vif);
1706 if (sta && !sta->uploaded)
1707 sta = NULL;
1708
Johannes Berg11127e92011-11-16 16:02:47 +01001709 switch (sdata->vif.type) {
1710 case NL80211_IFTYPE_MONITOR:
Aviya Erenfeldd8212182016-08-29 23:25:15 +03001711 if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
Johannes Bergc82b5a72013-07-14 23:39:20 +03001712 vif = &sdata->vif;
1713 break;
1714 }
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001715 sdata = rcu_dereference(local->monitor_sdata);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001716 if (sdata) {
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001717 vif = &sdata->vif;
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001718 info->hw_queue =
1719 vif->hw_queue[skb_get_queue_mapping(skb)];
Johannes Berg30686bf2015-06-02 21:39:54 +02001720 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
Johannes Berg63b4d8b2015-11-24 15:41:50 +01001721 ieee80211_purge_tx_queue(&local->hw, skbs);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001722 return true;
1723 } else
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001724 vif = NULL;
Johannes Berg11127e92011-11-16 16:02:47 +01001725 break;
1726 case NL80211_IFTYPE_AP_VLAN:
1727 sdata = container_of(sdata->bss,
1728 struct ieee80211_sub_if_data, u.ap);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05001729 fallthrough;
Johannes Berg11127e92011-11-16 16:02:47 +01001730 default:
1731 vif = &sdata->vif;
1732 break;
1733 }
1734
Johannes Berg4fd03282019-10-01 23:26:35 +02001735 result = ieee80211_tx_frags(local, vif, sta, skbs, txpending);
Johannes Berg11127e92011-11-16 16:02:47 +01001736
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001737 ieee80211_tpt_led_trig_tx(local, fc, led_len);
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001738
Johannes Berg4db4e0a2011-11-24 14:47:36 +01001739 WARN_ON_ONCE(!skb_queue_empty(skbs));
Johannes Berg252b86c2011-11-16 15:28:55 +01001740
Johannes Berg11127e92011-11-16 16:02:47 +01001741 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001742}
1743
Johannes Berg97b045d2008-06-20 01:22:30 +02001744/*
1745 * Invoke TX handlers, return 0 on success and non-zero if the
1746 * frame was dropped or queued.
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001747 *
1748 * The handlers are split into an early and late part. The latter is everything
1749 * that can be sensitive to reordering, and will be deferred to after packets
1750 * are dequeued from the intermediate queues (when they are enabled).
Johannes Berg97b045d2008-06-20 01:22:30 +02001751 */
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001752static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx)
Johannes Berg97b045d2008-06-20 01:22:30 +02001753{
Johannes Berg97b045d2008-06-20 01:22:30 +02001754 ieee80211_tx_result res = TX_DROP;
Johannes Berg97b045d2008-06-20 01:22:30 +02001755
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001756#define CALL_TXH(txh) \
1757 do { \
1758 res = txh(tx); \
1759 if (res != TX_CONTINUE) \
1760 goto txh_done; \
1761 } while (0)
Johannes Berg97b045d2008-06-20 01:22:30 +02001762
Kalle Valo5c1b98a2010-01-12 10:42:46 +02001763 CALL_TXH(ieee80211_tx_h_dynamic_ps);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001764 CALL_TXH(ieee80211_tx_h_check_assoc);
1765 CALL_TXH(ieee80211_tx_h_ps_buf);
Johannes Berga621fa42010-08-27 14:26:54 +03001766 CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001767 CALL_TXH(ieee80211_tx_h_select_key);
Johannes Berg30686bf2015-06-02 21:39:54 +02001768 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
Johannes Bergaf65cd962009-11-17 18:18:36 +01001769 CALL_TXH(ieee80211_tx_h_rate_ctrl);
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001770
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001771 txh_done:
1772 if (unlikely(res == TX_DROP)) {
1773 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1774 if (tx->skb)
1775 ieee80211_free_txskb(&tx->local->hw, tx->skb);
1776 else
1777 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1778 return -1;
1779 } else if (unlikely(res == TX_QUEUED)) {
1780 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1781 return -1;
1782 }
1783
1784 return 0;
1785}
1786
1787/*
1788 * Late handlers can be called while the sta lock is held. Handlers that can
1789 * cause packets to be generated will cause deadlock!
1790 */
1791static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx)
1792{
1793 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1794 ieee80211_tx_result res = TX_CONTINUE;
1795
Johannes Bergaa5b5492011-12-02 22:08:52 +01001796 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1797 __skb_queue_tail(&tx->skbs, tx->skb);
1798 tx->skb = NULL;
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001799 goto txh_done;
Johannes Bergaa5b5492011-12-02 22:08:52 +01001800 }
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001801
1802 CALL_TXH(ieee80211_tx_h_michael_mic_add);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001803 CALL_TXH(ieee80211_tx_h_sequence);
1804 CALL_TXH(ieee80211_tx_h_fragment);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001805 /* handlers after fragment must be aware of tx info fragmentation! */
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001806 CALL_TXH(ieee80211_tx_h_stats);
1807 CALL_TXH(ieee80211_tx_h_encrypt);
Johannes Berg30686bf2015-06-02 21:39:54 +02001808 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
Arik Nemtsov8fd369e2011-01-31 22:29:12 +02001809 CALL_TXH(ieee80211_tx_h_calculate_duration);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001810#undef CALL_TXH
1811
1812 txh_done:
Johannes Berg97b045d2008-06-20 01:22:30 +02001813 if (unlikely(res == TX_DROP)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001814 I802_DEBUG_INC(tx->local->tx_handlers_drop);
Johannes Berg252b86c2011-11-16 15:28:55 +01001815 if (tx->skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001816 ieee80211_free_txskb(&tx->local->hw, tx->skb);
Johannes Berg252b86c2011-11-16 15:28:55 +01001817 else
Felix Fietkau1f98ab72012-11-10 03:44:14 +01001818 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
Johannes Berg97b045d2008-06-20 01:22:30 +02001819 return -1;
1820 } else if (unlikely(res == TX_QUEUED)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001821 I802_DEBUG_INC(tx->local->tx_handlers_queued);
Johannes Berg97b045d2008-06-20 01:22:30 +02001822 return -1;
1823 }
1824
1825 return 0;
1826}
1827
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001828static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1829{
1830 int r = invoke_tx_handlers_early(tx);
1831
1832 if (r)
1833 return r;
1834 return invoke_tx_handlers_late(tx);
1835}
1836
Felix Fietkau06be6b12013-10-14 18:01:00 +02001837bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1838 struct ieee80211_vif *vif, struct sk_buff *skb,
1839 int band, struct ieee80211_sta **sta)
1840{
1841 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1842 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1843 struct ieee80211_tx_data tx;
Johannes Berg88724a82015-03-01 09:10:12 +02001844 struct sk_buff *skb2;
Felix Fietkau06be6b12013-10-14 18:01:00 +02001845
Johannes Berg7c107702015-03-20 14:18:27 +01001846 if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
Felix Fietkau06be6b12013-10-14 18:01:00 +02001847 return false;
1848
1849 info->band = band;
1850 info->control.vif = vif;
1851 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1852
1853 if (invoke_tx_handlers(&tx))
1854 return false;
1855
1856 if (sta) {
1857 if (tx.sta)
1858 *sta = &tx.sta->sta;
1859 else
1860 *sta = NULL;
1861 }
1862
Johannes Berg88724a82015-03-01 09:10:12 +02001863 /* this function isn't suitable for fragmented data frames */
1864 skb2 = __skb_dequeue(&tx.skbs);
1865 if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1866 ieee80211_free_txskb(hw, skb2);
1867 ieee80211_purge_tx_queue(hw, &tx.skbs);
1868 return false;
1869 }
1870
Felix Fietkau06be6b12013-10-14 18:01:00 +02001871 return true;
1872}
1873EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1874
Johannes Berg7bb45682011-02-24 14:42:06 +01001875/*
1876 * Returns false if the frame couldn't be transmitted but was queued instead.
1877 */
1878static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01001879 struct sta_info *sta, struct sk_buff *skb,
Mathy Vanhoef08aca292020-07-23 14:01:52 +04001880 bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001881{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001882 struct ieee80211_local *local = sdata->local;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001883 struct ieee80211_tx_data tx;
Johannes Berg97b045d2008-06-20 01:22:30 +02001884 ieee80211_tx_result res_prepare;
Johannes Berge039fa42008-05-15 12:55:29 +02001885 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001886 bool result = true;
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001887 int led_len;
Johannes Berge2ebc742007-07-27 15:43:22 +02001888
Johannes Berge2ebc742007-07-27 15:43:22 +02001889 if (unlikely(skb->len < 10)) {
1890 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001891 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001892 }
1893
Johannes Berg58d41852007-09-26 17:53:18 +02001894 /* initialises tx */
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001895 led_len = skb->len;
Johannes Berg7c107702015-03-20 14:18:27 +01001896 res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001897
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001898 if (unlikely(res_prepare == TX_DROP)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02001899 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001900 return true;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001901 } else if (unlikely(res_prepare == TX_QUEUED)) {
Johannes Berg55de9082012-07-26 17:24:39 +02001902 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001903 }
1904
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001905 /* set up hw_queue value early */
1906 if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
Johannes Berg30686bf2015-06-02 21:39:54 +02001907 !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001908 info->hw_queue =
1909 sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1910
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001911 if (invoke_tx_handlers_early(&tx))
Bob Copeland6eae4a62018-09-05 06:22:59 -04001912 return true;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001913
1914 if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb))
1915 return true;
1916
1917 if (!invoke_tx_handlers_late(&tx))
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001918 result = __ieee80211_tx(local, &tx.skbs, led_len,
1919 tx.sta, txpending);
Johannes Berg55de9082012-07-26 17:24:39 +02001920
Johannes Berg7bb45682011-02-24 14:42:06 +01001921 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001922}
1923
1924/* device xmit handlers */
1925
Johannes Berg14f46c12020-10-09 13:25:41 +02001926enum ieee80211_encrypt {
1927 ENCRYPT_NO,
1928 ENCRYPT_MGMT,
1929 ENCRYPT_DATA,
1930};
1931
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301932static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
Johannes Berg23c07522008-05-29 10:38:53 +02001933 struct sk_buff *skb,
Johannes Berg14f46c12020-10-09 13:25:41 +02001934 int head_need,
1935 enum ieee80211_encrypt encrypt)
Johannes Berg23c07522008-05-29 10:38:53 +02001936{
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301937 struct ieee80211_local *local = sdata->local;
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001938 bool enc_tailroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001939 int tail_need = 0;
1940
Johannes Berg14f46c12020-10-09 13:25:41 +02001941 enc_tailroom = encrypt == ENCRYPT_MGMT ||
1942 (encrypt == ENCRYPT_DATA &&
1943 sdata->crypto_tx_tailroom_needed_cnt);
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001944
1945 if (enc_tailroom) {
Johannes Berg23c07522008-05-29 10:38:53 +02001946 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1947 tail_need -= skb_tailroom(skb);
1948 tail_need = max_t(int, tail_need, 0);
1949 }
1950
Ido Yarivc70f59a2014-07-29 15:39:14 +03001951 if (skb_cloned(skb) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001952 (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001953 !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
Johannes Berg23c07522008-05-29 10:38:53 +02001954 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001955 else if (head_need || tail_need)
Johannes Berg23c07522008-05-29 10:38:53 +02001956 I802_DEBUG_INC(local->tx_expand_skb_head);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001957 else
1958 return 0;
Johannes Berg23c07522008-05-29 10:38:53 +02001959
1960 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
Joe Perches0fb9a9e2010-08-20 16:25:38 -07001961 wiphy_debug(local->hw.wiphy,
1962 "failed to reallocate TX buffer\n");
Johannes Berg23c07522008-05-29 10:38:53 +02001963 return -ENOMEM;
1964 }
1965
Johannes Berg23c07522008-05-29 10:38:53 +02001966 return 0;
1967}
1968
Johannes Berg7c107702015-03-20 14:18:27 +01001969void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
Mathy Vanhoef08aca292020-07-23 14:01:52 +04001970 struct sta_info *sta, struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02001971{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001972 struct ieee80211_local *local = sdata->local;
Johannes Berge039fa42008-05-15 12:55:29 +02001973 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg14f46c12020-10-09 13:25:41 +02001974 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berge2ebc742007-07-27 15:43:22 +02001975 int headroom;
Johannes Berg14f46c12020-10-09 13:25:41 +02001976 enum ieee80211_encrypt encrypt;
Johannes Berge2ebc742007-07-27 15:43:22 +02001977
Johannes Berg14f46c12020-10-09 13:25:41 +02001978 if (info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)
1979 encrypt = ENCRYPT_NO;
1980 else if (ieee80211_is_mgmt(hdr->frame_control))
1981 encrypt = ENCRYPT_MGMT;
1982 else
1983 encrypt = ENCRYPT_DATA;
Johannes Berg23c07522008-05-29 10:38:53 +02001984
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001985 headroom = local->tx_headroom;
Johannes Berg14f46c12020-10-09 13:25:41 +02001986 if (encrypt != ENCRYPT_NO)
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001987 headroom += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001988 headroom -= skb_headroom(skb);
1989 headroom = max_t(int, 0, headroom);
1990
Johannes Berg14f46c12020-10-09 13:25:41 +02001991 if (ieee80211_skb_resize(sdata, skb, headroom, encrypt)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02001992 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001993 return;
Johannes Berge2ebc742007-07-27 15:43:22 +02001994 }
1995
Johannes Berg14f46c12020-10-09 13:25:41 +02001996 /* reload after potential resize */
Steve deRosier740c1aa2010-09-11 20:01:31 -07001997 hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berg5061b0c2009-07-14 00:33:34 +02001998 info->control.vif = &sdata->vif;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001999
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002000 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2001 if (ieee80211_is_data(hdr->frame_control) &&
2002 is_unicast_ether_addr(hdr->addr1)) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002003 if (mesh_nexthop_resolve(sdata, skb))
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002004 return; /* skb queued: don't free */
2005 } else {
2006 ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
2007 }
Johannes Berg98aed9f2012-03-27 14:18:36 +02002008 }
Javier Cardonacca89492009-08-10 17:29:29 -07002009
Javier Cardona2154c81c2011-09-07 17:49:53 -07002010 ieee80211_set_qos_hdr(sdata, skb);
Mathy Vanhoef08aca292020-07-23 14:01:52 +04002011 ieee80211_tx(sdata, sta, skb, false);
Johannes Berge2ebc742007-07-27 15:43:22 +02002012}
2013
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002014bool ieee80211_parse_tx_radiotap(struct sk_buff *skb,
2015 struct net_device *dev)
Johannes Berg73b9f032011-10-07 14:01:26 +02002016{
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002017 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg73b9f032011-10-07 14:01:26 +02002018 struct ieee80211_radiotap_iterator iterator;
2019 struct ieee80211_radiotap_header *rthdr =
2020 (struct ieee80211_radiotap_header *) skb->data;
2021 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002022 struct ieee80211_supported_band *sband =
2023 local->hw.wiphy->bands[info->band];
Johannes Berg73b9f032011-10-07 14:01:26 +02002024 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
2025 NULL);
2026 u16 txflags;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002027 u16 rate = 0;
2028 bool rate_found = false;
2029 u8 rate_retries = 0;
2030 u16 rate_flags = 0;
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002031 u8 mcs_known, mcs_flags, mcs_bw;
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002032 u16 vht_known;
2033 u8 vht_mcs = 0, vht_nss = 0;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002034 int i;
Johannes Berg73b9f032011-10-07 14:01:26 +02002035
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002036 /* check for not even having the fixed radiotap header part */
2037 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2038 return false; /* too short to be possibly valid */
2039
2040 /* is it a header version we can trust to find length from? */
2041 if (unlikely(rthdr->it_version))
2042 return false; /* only version 0 is supported */
2043
2044 /* does the skb contain enough to deliver on the alleged length? */
2045 if (unlikely(skb->len < ieee80211_get_radiotap_len(skb->data)))
2046 return false; /* skb too short for claimed rt header extent */
2047
Johannes Berg73b9f032011-10-07 14:01:26 +02002048 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
2049 IEEE80211_TX_CTL_DONTFRAG;
2050
2051 /*
2052 * for every radiotap entry that is present
2053 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
2054 * entries present, or -EINVAL on error)
2055 */
2056
2057 while (!ret) {
2058 ret = ieee80211_radiotap_iterator_next(&iterator);
2059
2060 if (ret)
2061 continue;
2062
2063 /* see if this argument is something we can use */
2064 switch (iterator.this_arg_index) {
2065 /*
2066 * You must take care when dereferencing iterator.this_arg
2067 * for multibyte types... the pointer is not aligned. Use
2068 * get_unaligned((type *)iterator.this_arg) to dereference
2069 * iterator.this_arg for type "type" safely on all arches.
2070 */
2071 case IEEE80211_RADIOTAP_FLAGS:
2072 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
2073 /*
2074 * this indicates that the skb we have been
2075 * handed has the 32-bit FCS CRC at the end...
2076 * we should react to that by snipping it off
2077 * because it will be recomputed and added
2078 * on transmission
2079 */
2080 if (skb->len < (iterator._max_length + FCS_LEN))
2081 return false;
2082
2083 skb_trim(skb, skb->len - FCS_LEN);
2084 }
2085 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
2086 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
2087 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
2088 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
2089 break;
2090
2091 case IEEE80211_RADIOTAP_TX_FLAGS:
2092 txflags = get_unaligned_le16(iterator.this_arg);
2093 if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
2094 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Mathy Vanhoefe02281e2020-07-23 14:01:49 +04002095 if (txflags & IEEE80211_RADIOTAP_F_TX_NOSEQNO)
2096 info->control.flags |= IEEE80211_TX_CTRL_NO_SEQNO;
Mathy Vanhoef30df8132020-11-04 10:18:19 +04002097 if (txflags & IEEE80211_RADIOTAP_F_TX_ORDER)
2098 info->control.flags |=
2099 IEEE80211_TX_CTRL_DONT_REORDER;
Johannes Berg73b9f032011-10-07 14:01:26 +02002100 break;
2101
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002102 case IEEE80211_RADIOTAP_RATE:
2103 rate = *iterator.this_arg;
2104 rate_flags = 0;
2105 rate_found = true;
2106 break;
2107
2108 case IEEE80211_RADIOTAP_DATA_RETRIES:
2109 rate_retries = *iterator.this_arg;
2110 break;
2111
2112 case IEEE80211_RADIOTAP_MCS:
2113 mcs_known = iterator.this_arg[0];
2114 mcs_flags = iterator.this_arg[1];
2115 if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
2116 break;
2117
2118 rate_found = true;
2119 rate = iterator.this_arg[2];
2120 rate_flags = IEEE80211_TX_RC_MCS;
2121
2122 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
2123 mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
2124 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2125
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002126 mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002127 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002128 mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002129 rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
Philipp Borgersf1864e12020-12-19 18:07:10 +01002130
2131 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_FEC &&
2132 mcs_flags & IEEE80211_RADIOTAP_MCS_FEC_LDPC)
2133 info->flags |= IEEE80211_TX_CTL_LDPC;
Philipp Borgers549fdd32021-01-25 16:07:44 +01002134
2135 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_STBC) {
2136 u8 stbc = u8_get_bits(mcs_flags,
2137 IEEE80211_RADIOTAP_MCS_STBC_MASK);
2138
2139 info->flags |=
2140 u32_encode_bits(stbc,
2141 IEEE80211_TX_CTL_STBC);
2142 }
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002143 break;
2144
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002145 case IEEE80211_RADIOTAP_VHT:
2146 vht_known = get_unaligned_le16(iterator.this_arg);
2147 rate_found = true;
2148
2149 rate_flags = IEEE80211_TX_RC_VHT_MCS;
2150 if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
2151 (iterator.this_arg[2] &
2152 IEEE80211_RADIOTAP_VHT_FLAG_SGI))
2153 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2154 if (vht_known &
2155 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
2156 if (iterator.this_arg[3] == 1)
2157 rate_flags |=
2158 IEEE80211_TX_RC_40_MHZ_WIDTH;
2159 else if (iterator.this_arg[3] == 4)
2160 rate_flags |=
2161 IEEE80211_TX_RC_80_MHZ_WIDTH;
2162 else if (iterator.this_arg[3] == 11)
2163 rate_flags |=
2164 IEEE80211_TX_RC_160_MHZ_WIDTH;
2165 }
2166
2167 vht_mcs = iterator.this_arg[4] >> 4;
2168 vht_nss = iterator.this_arg[4] & 0xF;
2169 break;
2170
Johannes Berg73b9f032011-10-07 14:01:26 +02002171 /*
2172 * Please update the file
Mauro Carvalho Chehab429ff872020-04-30 18:03:59 +02002173 * Documentation/networking/mac80211-injection.rst
Johannes Berg73b9f032011-10-07 14:01:26 +02002174 * when parsing new fields here.
2175 */
2176
2177 default:
2178 break;
2179 }
2180 }
2181
2182 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
2183 return false;
2184
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002185 if (rate_found) {
2186 info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
2187
2188 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2189 info->control.rates[i].idx = -1;
2190 info->control.rates[i].flags = 0;
2191 info->control.rates[i].count = 0;
2192 }
2193
2194 if (rate_flags & IEEE80211_TX_RC_MCS) {
2195 info->control.rates[0].idx = rate;
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002196 } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
2197 ieee80211_rate_set_vht(info->control.rates, vht_mcs,
2198 vht_nss);
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002199 } else {
2200 for (i = 0; i < sband->n_bitrates; i++) {
2201 if (rate * 5 != sband->bitrates[i].bitrate)
2202 continue;
2203
2204 info->control.rates[0].idx = i;
2205 break;
2206 }
2207 }
2208
Felix Fietkau07310a62016-03-02 15:54:51 +01002209 if (info->control.rates[0].idx < 0)
2210 info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
2211
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002212 info->control.rates[0].flags = rate_flags;
2213 info->control.rates[0].count = min_t(u8, rate_retries + 1,
2214 local->hw.max_rate_tries);
2215 }
2216
Johannes Berg73b9f032011-10-07 14:01:26 +02002217 return true;
2218}
2219
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +00002220netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
2221 struct net_device *dev)
Johannes Berge2ebc742007-07-27 15:43:22 +02002222{
2223 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg55de9082012-07-26 17:24:39 +02002224 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002225 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002226 struct ieee80211_hdr *hdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002227 struct ieee80211_sub_if_data *tmp_sdata, *sdata;
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002228 struct cfg80211_chan_def *chandef;
Andy Green9b8a74e2007-07-27 15:43:24 +02002229 u16 len_rthdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002230 int hdrlen;
Johannes Berge2ebc742007-07-27 15:43:22 +02002231
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002232 memset(info, 0, sizeof(*info));
2233 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2234 IEEE80211_TX_CTL_INJECTED;
Andy Green9b8a74e2007-07-27 15:43:24 +02002235
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002236 /* Sanity-check and process the injection radiotap header */
2237 if (!ieee80211_parse_tx_radiotap(skb, dev))
2238 goto fail;
Andy Green9b8a74e2007-07-27 15:43:24 +02002239
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002240 /* we now know there is a radiotap header with a length we can use */
Andy Green9b8a74e2007-07-27 15:43:24 +02002241 len_rthdr = ieee80211_get_radiotap_len(skb->data);
2242
Johannes Berge2ebc742007-07-27 15:43:22 +02002243 /*
2244 * fix up the pointers accounting for the radiotap
2245 * header still being in there. We are being given
2246 * a precooked IEEE80211 header so no need for
2247 * normal processing
2248 */
Andy Green9b8a74e2007-07-27 15:43:24 +02002249 skb_set_mac_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02002250 /*
Andy Green9b8a74e2007-07-27 15:43:24 +02002251 * these are just fixed to the end of the rt area since we
2252 * don't have any better information and at this point, nobody cares
Johannes Berge2ebc742007-07-27 15:43:22 +02002253 */
Andy Green9b8a74e2007-07-27 15:43:24 +02002254 skb_set_network_header(skb, len_rthdr);
2255 skb_set_transport_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02002256
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002257 if (skb->len < len_rthdr + 2)
2258 goto fail;
2259
2260 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2261 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2262
2263 if (skb->len < len_rthdr + hdrlen)
2264 goto fail;
2265
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002266 /*
2267 * Initialize skb->protocol if the injected frame is a data frame
2268 * carrying a rfc1042 header
2269 */
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002270 if (ieee80211_is_data(hdr->frame_control) &&
2271 skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2272 u8 *payload = (u8 *)hdr + hdrlen;
2273
Joe Perchesb203ca32012-05-08 18:56:52 +00002274 if (ether_addr_equal(payload, rfc1042_header))
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002275 skb->protocol = cpu_to_be16((payload[6] << 8) |
2276 payload[7]);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002277 }
2278
Mathy Vanhoef70c5e402020-11-04 10:18:22 +04002279 /* Initialize skb->priority for QoS frames. If the DONT_REORDER flag
2280 * is set, stick to the default value for skb->priority to assure
2281 * frames injected with this flag are not reordered relative to each
2282 * other.
Fredrik Olofsson753ffad2019-11-19 14:34:51 +01002283 */
Mathy Vanhoef70c5e402020-11-04 10:18:22 +04002284 if (ieee80211_is_data_qos(hdr->frame_control) &&
2285 !(info->control.flags & IEEE80211_TX_CTRL_DONT_REORDER)) {
Fredrik Olofsson753ffad2019-11-19 14:34:51 +01002286 u8 *p = ieee80211_get_qos_ctl(hdr);
2287 skb->priority = *p & IEEE80211_QOS_CTL_TAG1D_MASK;
2288 }
2289
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002290 rcu_read_lock();
2291
2292 /*
2293 * We process outgoing injected frames that have a local address
2294 * we handle as though they are non-injected frames.
2295 * This code here isn't entirely correct, the local MAC address
2296 * isn't always enough to find the interface to use; for proper
Johannes Berg70d9c592020-11-09 10:57:46 +01002297 * VLAN support we have an nl80211-based mechanism.
Johannes Bergb226a822019-11-22 12:42:42 +01002298 *
2299 * This is necessary, for example, for old hostapd versions that
2300 * don't use nl80211-based management TX/RX.
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002301 */
2302 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2303
2304 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2305 if (!ieee80211_sdata_running(tmp_sdata))
2306 continue;
2307 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
Johannes Berg70d9c592020-11-09 10:57:46 +01002308 tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002309 continue;
Joe Perchesb203ca32012-05-08 18:56:52 +00002310 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002311 sdata = tmp_sdata;
2312 break;
2313 }
2314 }
Johannes Berg7351c6b2009-11-19 01:08:30 +01002315
Johannes Berg55de9082012-07-26 17:24:39 +02002316 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2317 if (!chanctx_conf) {
2318 tmp_sdata = rcu_dereference(local->monitor_sdata);
2319 if (tmp_sdata)
2320 chanctx_conf =
2321 rcu_dereference(tmp_sdata->vif.chanctx_conf);
2322 }
Johannes Berg55de9082012-07-26 17:24:39 +02002323
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002324 if (chanctx_conf)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002325 chandef = &chanctx_conf->def;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002326 else if (!local->use_chanctx)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002327 chandef = &local->_oper_chandef;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002328 else
2329 goto fail_rcu;
Johannes Berg55de9082012-07-26 17:24:39 +02002330
2331 /*
2332 * Frame injection is not allowed if beaconing is not allowed
2333 * or if we need radar detection. Beaconing is usually not allowed when
2334 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2335 * Passive scan is also used in world regulatory domains where
2336 * your country is not known and as such it should be treated as
2337 * NO TX unless the channel is explicitly allowed in which case
2338 * your current regulatory domain would not have the passive scan
2339 * flag.
2340 *
2341 * Since AP mode uses monitor interfaces to inject/TX management
2342 * frames we can make AP mode the exception to this rule once it
2343 * supports radar detection as its implementation can deal with
2344 * radar detection by itself. We can do that later by adding a
2345 * monitor flag interfaces used for AP support.
2346 */
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002347 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2348 sdata->vif.type))
Johannes Berg55de9082012-07-26 17:24:39 +02002349 goto fail_rcu;
2350
Johannes Berg73c4e192014-11-09 18:50:09 +02002351 info->band = chandef->chan->band;
Lorenzo Bianconi109843b02016-02-19 12:18:01 +01002352
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002353 /* remove the injection radiotap header */
2354 skb_pull(skb, len_rthdr);
Lorenzo Bianconi109843b02016-02-19 12:18:01 +01002355
Mathy Vanhoef08aca292020-07-23 14:01:52 +04002356 ieee80211_xmit(sdata, NULL, skb);
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002357 rcu_read_unlock();
2358
Johannes Berge2ebc742007-07-27 15:43:22 +02002359 return NETDEV_TX_OK;
Andy Green9b8a74e2007-07-27 15:43:24 +02002360
Johannes Berg55de9082012-07-26 17:24:39 +02002361fail_rcu:
2362 rcu_read_unlock();
Andy Green9b8a74e2007-07-27 15:43:24 +02002363fail:
2364 dev_kfree_skb(skb);
2365 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
Johannes Berge2ebc742007-07-27 15:43:22 +02002366}
2367
Johannes Berg97ffe752015-03-21 09:13:45 +01002368static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002369{
Johannes Berg97ffe752015-03-21 09:13:45 +01002370 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002371
Johannes Berg97ffe752015-03-21 09:13:45 +01002372 return ethertype == ETH_P_TDLS &&
2373 skb->len > 14 &&
2374 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2375}
2376
John Crispin50ff4772019-11-25 11:04:37 +01002377int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2378 struct sk_buff *skb,
2379 struct sta_info **sta_out)
Johannes Berg97ffe752015-03-21 09:13:45 +01002380{
2381 struct sta_info *sta;
2382
2383 switch (sdata->vif.type) {
2384 case NL80211_IFTYPE_AP_VLAN:
2385 sta = rcu_dereference(sdata->u.vlan.sta);
2386 if (sta) {
2387 *sta_out = sta;
2388 return 0;
2389 } else if (sdata->wdev.use_4addr) {
2390 return -ENOLINK;
2391 }
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05002392 fallthrough;
Johannes Berg97ffe752015-03-21 09:13:45 +01002393 case NL80211_IFTYPE_AP:
2394 case NL80211_IFTYPE_OCB:
2395 case NL80211_IFTYPE_ADHOC:
2396 if (is_multicast_ether_addr(skb->data)) {
2397 *sta_out = ERR_PTR(-ENOENT);
2398 return 0;
2399 }
2400 sta = sta_info_get_bss(sdata, skb->data);
2401 break;
Johannes Berg97ffe752015-03-21 09:13:45 +01002402#ifdef CONFIG_MAC80211_MESH
2403 case NL80211_IFTYPE_MESH_POINT:
2404 /* determined much later */
2405 *sta_out = NULL;
2406 return 0;
2407#endif
2408 case NL80211_IFTYPE_STATION:
2409 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2410 sta = sta_info_get(sdata, skb->data);
Johannes Berg11d62ca2016-09-13 08:28:22 +02002411 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2412 if (test_sta_flag(sta,
2413 WLAN_STA_TDLS_PEER_AUTH)) {
Johannes Berg97ffe752015-03-21 09:13:45 +01002414 *sta_out = sta;
2415 return 0;
2416 }
2417
2418 /*
2419 * TDLS link during setup - throw out frames to
2420 * peer. Allow TDLS-setup frames to unauthorized
2421 * peers for the special case of a link teardown
2422 * after a TDLS sta is removed due to being
2423 * unreachable.
2424 */
Johannes Berg11d62ca2016-09-13 08:28:22 +02002425 if (!ieee80211_is_tdls_setup(skb))
Johannes Berg97ffe752015-03-21 09:13:45 +01002426 return -EINVAL;
2427 }
2428
2429 }
2430
2431 sta = sta_info_get(sdata, sdata->u.mgd.bssid);
2432 if (!sta)
2433 return -ENOLINK;
2434 break;
2435 default:
2436 return -EINVAL;
2437 }
2438
2439 *sta_out = sta ?: ERR_PTR(-ENOENT);
2440 return 0;
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002441}
2442
Markus Theila7528192020-05-27 18:03:34 +02002443static u16 ieee80211_store_ack_skb(struct ieee80211_local *local,
John Crispin5d8983c2019-10-29 10:13:02 +01002444 struct sk_buff *skb,
Markus Theila7528192020-05-27 18:03:34 +02002445 u32 *info_flags,
2446 u64 *cookie)
John Crispin5d8983c2019-10-29 10:13:02 +01002447{
Markus Theila7528192020-05-27 18:03:34 +02002448 struct sk_buff *ack_skb;
John Crispin5d8983c2019-10-29 10:13:02 +01002449 u16 info_id = 0;
2450
Markus Theila7528192020-05-27 18:03:34 +02002451 if (skb->sk)
2452 ack_skb = skb_clone_sk(skb);
2453 else
2454 ack_skb = skb_clone(skb, GFP_ATOMIC);
2455
John Crispin5d8983c2019-10-29 10:13:02 +01002456 if (ack_skb) {
2457 unsigned long flags;
2458 int id;
2459
2460 spin_lock_irqsave(&local->ack_status_lock, flags);
2461 id = idr_alloc(&local->ack_status_frames, ack_skb,
Johannes Bergf2b18ba2020-01-15 12:25:50 +01002462 1, 0x2000, GFP_ATOMIC);
John Crispin5d8983c2019-10-29 10:13:02 +01002463 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2464
2465 if (id >= 0) {
2466 info_id = id;
2467 *info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
Markus Theila7528192020-05-27 18:03:34 +02002468 if (cookie) {
2469 *cookie = ieee80211_mgmt_tx_cookie(local);
2470 IEEE80211_SKB_CB(ack_skb)->ack.cookie = *cookie;
2471 }
John Crispin5d8983c2019-10-29 10:13:02 +01002472 } else {
2473 kfree_skb(ack_skb);
2474 }
2475 }
2476
2477 return info_id;
2478}
2479
Johannes Berge2ebc742007-07-27 15:43:22 +02002480/**
Johannes Berg4c9451e2014-11-09 18:50:10 +02002481 * ieee80211_build_hdr - build 802.11 header in the given frame
2482 * @sdata: virtual interface to build the header for
2483 * @skb: the skb to build the header in
Liad Kaufman24d342c2014-11-09 18:50:07 +02002484 * @info_flags: skb flags to set
Johannes Bergf0daf542020-09-24 19:25:11 +02002485 * @sta: the station pointer
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002486 * @ctrl_flags: info control flags to set
Johannes Bergf0daf542020-09-24 19:25:11 +02002487 * @cookie: cookie pointer to fill (if not %NULL)
Johannes Berge2ebc742007-07-27 15:43:22 +02002488 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002489 * This function takes the skb with 802.3 header and reformats the header to
2490 * the appropriate IEEE 802.11 header based on which interface the packet is
2491 * being transmitted on.
Johannes Berge2ebc742007-07-27 15:43:22 +02002492 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002493 * Note that this function also takes care of the TX status request and
2494 * potential unsharing of the SKB - this needs to be interleaved with the
2495 * header building.
2496 *
2497 * The function requires the read-side RCU lock held
2498 *
2499 * Returns: the (possibly reallocated) skb or an ERR_PTR() code
Johannes Berge2ebc742007-07-27 15:43:22 +02002500 */
Johannes Berg4c9451e2014-11-09 18:50:10 +02002501static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01002502 struct sk_buff *skb, u32 info_flags,
Markus Theila7528192020-05-27 18:03:34 +02002503 struct sta_info *sta, u32 ctrl_flags,
2504 u64 *cookie)
Johannes Berge2ebc742007-07-27 15:43:22 +02002505{
Johannes Berg133b8222008-09-16 14:18:59 +02002506 struct ieee80211_local *local = sdata->local;
Felix Fietkau489ee912010-12-18 19:30:48 +01002507 struct ieee80211_tx_info *info;
Johannes Bergdcf33962012-07-30 15:11:56 +02002508 int head_need;
Harvey Harrison065e96052008-06-22 16:45:27 -07002509 u16 ethertype, hdrlen, meshhdrlen = 0;
2510 __le16 fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002511 struct ieee80211_hdr hdr;
Wey-Yi Guyff67bb82010-08-21 07:23:29 -07002512 struct ieee80211s_hdr mesh_hdr __maybe_unused;
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002513 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02002514 const u8 *encaps_data;
2515 int encaps_len, skip_header_bytes;
Johannes Berg97ffe752015-03-21 09:13:45 +01002516 bool wme_sta = false, authorized = false;
2517 bool tdls_peer;
Johannes Berga729cff2011-11-06 14:13:34 +01002518 bool multicast;
Johannes Berga729cff2011-11-06 14:13:34 +01002519 u16 info_id = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002520 struct ieee80211_chanctx_conf *chanctx_conf;
2521 struct ieee80211_sub_if_data *ap_sdata;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002522 enum nl80211_band band;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002523 int ret;
Johannes Berge2ebc742007-07-27 15:43:22 +02002524
Johannes Berg97ffe752015-03-21 09:13:45 +01002525 if (IS_ERR(sta))
2526 sta = NULL;
2527
Julius Niedworok276d9e82019-03-28 21:01:06 +01002528#ifdef CONFIG_MAC80211_DEBUGFS
2529 if (local->force_tx_status)
2530 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2531#endif
2532
Johannes Berge2ebc742007-07-27 15:43:22 +02002533 /* convert Ethernet header to proper 802.11 header (based on
2534 * operation mode) */
2535 ethertype = (skb->data[12] << 8) | skb->data[13];
Harvey Harrison065e96052008-06-22 16:45:27 -07002536 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
Johannes Berge2ebc742007-07-27 15:43:22 +02002537
Johannes Berg51fb61e2007-12-19 01:31:27 +01002538 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002539 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg97ffe752015-03-21 09:13:45 +01002540 if (sdata->wdev.use_4addr) {
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002541 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2542 /* RA TA DA SA */
2543 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002544 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002545 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2546 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2547 hdrlen = 30;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002548 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Johannes Berga74a8c82014-07-22 14:50:47 +02002549 wme_sta = sta->sta.wme;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002550 }
Johannes Berg55de9082012-07-26 17:24:39 +02002551 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2552 u.ap);
2553 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002554 if (!chanctx_conf) {
2555 ret = -ENOTCONN;
2556 goto free;
2557 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002558 band = chanctx_conf->def.chan->band;
Johannes Berg97ffe752015-03-21 09:13:45 +01002559 if (sdata->wdev.use_4addr)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002560 break;
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05002561 fallthrough;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002562 case NL80211_IFTYPE_AP:
Arnd Bergmannfe801232013-01-25 14:14:33 +00002563 if (sdata->vif.type == NL80211_IFTYPE_AP)
2564 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002565 if (!chanctx_conf) {
2566 ret = -ENOTCONN;
2567 goto free;
2568 }
Harvey Harrison065e96052008-06-22 16:45:27 -07002569 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
Johannes Berge2ebc742007-07-27 15:43:22 +02002570 /* DA BSSID SA */
2571 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002572 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002573 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2574 hdrlen = 24;
Johannes Berg4bf88532012-11-09 11:39:59 +01002575 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002576 break;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002577#ifdef CONFIG_MAC80211_MESH
Johannes Berg05c914f2008-09-11 00:01:58 +02002578 case NL80211_IFTYPE_MESH_POINT:
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002579 if (!is_multicast_ether_addr(skb->data)) {
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002580 struct sta_info *next_hop;
2581 bool mpp_lookup = true;
2582
Johannes Bergbf7cd942013-02-15 14:40:31 +01002583 mpath = mesh_path_lookup(sdata, skb->data);
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002584 if (mpath) {
2585 mpp_lookup = false;
2586 next_hop = rcu_dereference(mpath->next_hop);
2587 if (!next_hop ||
2588 !(mpath->flags & (MESH_PATH_ACTIVE |
2589 MESH_PATH_RESOLVING)))
2590 mpp_lookup = true;
2591 }
2592
Henning Roggeab1c7902016-02-03 13:58:37 +01002593 if (mpp_lookup) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002594 mppath = mpp_path_lookup(sdata, skb->data);
Henning Roggeab1c7902016-02-03 13:58:37 +01002595 if (mppath)
2596 mppath->exp_time = jiffies;
2597 }
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002598
2599 if (mppath && mpath)
Bob Copeland2bdaf382016-02-28 20:03:56 -05002600 mesh_path_del(sdata, mpath->dst);
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002601 }
YanBo79617de2008-09-22 13:30:32 +08002602
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002603 /*
Joel A Fernandes9d525012011-01-10 00:44:23 -06002604 * Use address extension if it is a packet from
2605 * another interface or if we know the destination
2606 * is being proxied by a portal (i.e. portal address
2607 * differs from proxied address)
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002608 */
Joe Perchesb203ca32012-05-08 18:56:52 +00002609 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2610 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
Javier Cardona3c5772a2009-08-10 12:15:48 -07002611 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2612 skb->data, skb->data + ETH_ALEN);
Johannes Bergbf7cd942013-02-15 14:40:31 +01002613 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2614 NULL, NULL);
YanBo79617de2008-09-22 13:30:32 +08002615 } else {
Thomas Pedersen27f01122012-08-20 11:28:25 -07002616 /* DS -> MBSS (802.11-2012 13.11.3.3).
2617 * For unicast with unknown forwarding information,
2618 * destination might be in the MBSS or if that fails
2619 * forwarded to another mesh gate. In either case
2620 * resolution will be handled in ieee80211_xmit(), so
2621 * leave the original DA. This also works for mcast */
2622 const u8 *mesh_da = skb->data;
YanBo79617de2008-09-22 13:30:32 +08002623
Thomas Pedersen27f01122012-08-20 11:28:25 -07002624 if (mppath)
2625 mesh_da = mppath->mpp;
2626 else if (mpath)
2627 mesh_da = mpath->dst;
Thomas Pedersen27f01122012-08-20 11:28:25 -07002628
Javier Cardona3c5772a2009-08-10 12:15:48 -07002629 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
Johannes Berg47846c92009-11-25 17:46:19 +01002630 mesh_da, sdata->vif.addr);
Thomas Pedersen27f01122012-08-20 11:28:25 -07002631 if (is_multicast_ether_addr(mesh_da))
2632 /* DA TA mSA AE:SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002633 meshhdrlen = ieee80211_new_mesh_header(
2634 sdata, &mesh_hdr,
2635 skb->data + ETH_ALEN, NULL);
Javier Cardona3c5772a2009-08-10 12:15:48 -07002636 else
Thomas Pedersen27f01122012-08-20 11:28:25 -07002637 /* RA TA mDA mSA AE:DA SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002638 meshhdrlen = ieee80211_new_mesh_header(
2639 sdata, &mesh_hdr, skb->data,
2640 skb->data + ETH_ALEN);
YanBo79617de2008-09-22 13:30:32 +08002641
YanBo79617de2008-09-22 13:30:32 +08002642 }
Johannes Berg55de9082012-07-26 17:24:39 +02002643 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002644 if (!chanctx_conf) {
2645 ret = -ENOTCONN;
2646 goto free;
2647 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002648 band = chanctx_conf->def.chan->band;
Rajkumar Manoharan8828f812019-04-11 13:47:26 -07002649
2650 /* For injected frames, fill RA right away as nexthop lookup
2651 * will be skipped.
2652 */
2653 if ((ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP) &&
2654 is_zero_ether_addr(hdr.addr1))
2655 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002656 break;
2657#endif
Johannes Berg05c914f2008-09-11 00:01:58 +02002658 case NL80211_IFTYPE_STATION:
Johannes Berg97ffe752015-03-21 09:13:45 +01002659 /* we already did checks when looking up the RA STA */
2660 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002661
Johannes Berg97ffe752015-03-21 09:13:45 +01002662 if (tdls_peer) {
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002663 /* DA SA BSSID */
2664 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2665 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2666 memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2667 hdrlen = 24;
2668 } else if (sdata->u.mgd.use_4addr &&
2669 cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2670 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2671 IEEE80211_FCTL_TODS);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002672 /* RA TA DA SA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002673 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002674 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002675 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2676 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2677 hdrlen = 30;
2678 } else {
2679 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2680 /* BSSID SA DA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002681 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002682 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2683 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2684 hdrlen = 24;
2685 }
Johannes Berg55de9082012-07-26 17:24:39 +02002686 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002687 if (!chanctx_conf) {
2688 ret = -ENOTCONN;
2689 goto free;
2690 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002691 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002692 break;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002693 case NL80211_IFTYPE_OCB:
2694 /* DA SA BSSID */
2695 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2696 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2697 eth_broadcast_addr(hdr.addr3);
2698 hdrlen = 24;
2699 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002700 if (!chanctx_conf) {
2701 ret = -ENOTCONN;
2702 goto free;
2703 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002704 band = chanctx_conf->def.chan->band;
2705 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002706 case NL80211_IFTYPE_ADHOC:
Johannes Berge2ebc742007-07-27 15:43:22 +02002707 /* DA SA BSSID */
2708 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2709 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01002710 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002711 hdrlen = 24;
Johannes Berg55de9082012-07-26 17:24:39 +02002712 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002713 if (!chanctx_conf) {
2714 ret = -ENOTCONN;
2715 goto free;
2716 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002717 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002718 break;
2719 default:
Johannes Berg4c9451e2014-11-09 18:50:10 +02002720 ret = -EINVAL;
2721 goto free;
Johannes Berge2ebc742007-07-27 15:43:22 +02002722 }
2723
Johannes Berga729cff2011-11-06 14:13:34 +01002724 multicast = is_multicast_ether_addr(hdr.addr1);
Johannes Berge2ebc742007-07-27 15:43:22 +02002725
Johannes Berg97ffe752015-03-21 09:13:45 +01002726 /* sta is always NULL for mesh */
2727 if (sta) {
2728 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2729 wme_sta = sta->sta.wme;
2730 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2731 /* For mesh, the use of the QoS header is mandatory */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002732 wme_sta = true;
Johannes Berge2ebc742007-07-27 15:43:22 +02002733 }
2734
Johannes Berg527871d2015-03-21 08:09:55 +01002735 /* receiver does QoS (which also means we do) use it */
2736 if (wme_sta) {
Harvey Harrison065e96052008-06-22 16:45:27 -07002737 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002738 hdrlen += 2;
2739 }
2740
2741 /*
Johannes Berg238814f2008-01-28 17:19:37 +01002742 * Drop unicast frames to unauthorised stations unless they are
2743 * EAPOL frames from the local station.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002744 */
Johannes Berg55182e42011-10-12 17:28:21 +02002745 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002746 (sdata->vif.type != NL80211_IFTYPE_OCB) &&
Sergey Ryazanova6ececf2013-08-30 01:35:09 +04002747 !multicast && !authorized &&
Johannes Berg55182e42011-10-12 17:28:21 +02002748 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002749 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002750#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002751 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
Johannes Berg4c9451e2014-11-09 18:50:10 +02002752 sdata->name, hdr.addr1);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002753#endif
2754
2755 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2756
Johannes Berg4c9451e2014-11-09 18:50:10 +02002757 ret = -EPERM;
2758 goto free;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002759 }
2760
Markus Theila7528192020-05-27 18:03:34 +02002761 if (unlikely(!multicast && ((skb->sk &&
2762 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS) ||
2763 ctrl_flags & IEEE80211_TX_CTL_REQ_TX_STATUS)))
2764 info_id = ieee80211_store_ack_skb(local, skb, &info_flags,
2765 cookie);
Johannes Berga729cff2011-11-06 14:13:34 +01002766
Helmut Schaa7e244702010-12-02 18:44:09 +01002767 /*
2768 * If the skb is shared we need to obtain our own copy.
2769 */
2770 if (skb_shared(skb)) {
Johannes Berga729cff2011-11-06 14:13:34 +01002771 struct sk_buff *tmp_skb = skb;
2772
2773 /* can't happen -- skb is a clone if info_id != 0 */
2774 WARN_ON(info_id);
2775
Felix Fietkauf8a0a782010-12-18 19:30:50 +01002776 skb = skb_clone(skb, GFP_ATOMIC);
Helmut Schaa7e244702010-12-02 18:44:09 +01002777 kfree_skb(tmp_skb);
2778
Johannes Berg4c9451e2014-11-09 18:50:10 +02002779 if (!skb) {
2780 ret = -ENOMEM;
2781 goto free;
2782 }
Helmut Schaa7e244702010-12-02 18:44:09 +01002783 }
2784
Harvey Harrison065e96052008-06-22 16:45:27 -07002785 hdr.frame_control = fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002786 hdr.duration_id = 0;
2787 hdr.seq_ctrl = 0;
2788
2789 skip_header_bytes = ETH_HLEN;
2790 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2791 encaps_data = bridge_tunnel_header;
2792 encaps_len = sizeof(bridge_tunnel_header);
2793 skip_header_bytes -= 2;
Simon Hormane5c5d222013-03-28 13:38:25 +09002794 } else if (ethertype >= ETH_P_802_3_MIN) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002795 encaps_data = rfc1042_header;
2796 encaps_len = sizeof(rfc1042_header);
2797 skip_header_bytes -= 2;
2798 } else {
2799 encaps_data = NULL;
2800 encaps_len = 0;
2801 }
2802
2803 skb_pull(skb, skip_header_bytes);
Johannes Berg23c07522008-05-29 10:38:53 +02002804 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002805
Johannes Berg23c07522008-05-29 10:38:53 +02002806 /*
2807 * So we need to modify the skb header and hence need a copy of
2808 * that. The head_need variable above doesn't, so far, include
2809 * the needed header space that we don't need right away. If we
2810 * can, then we don't reallocate right now but only after the
2811 * frame arrives at the master device (if it does...)
2812 *
2813 * If we cannot, however, then we will reallocate to include all
2814 * the ever needed space. Also, if we need to reallocate it anyway,
2815 * make it big enough for everything we may ever need.
2816 */
Johannes Berge2ebc742007-07-27 15:43:22 +02002817
David S. Miller3a5be7d2008-06-18 01:19:51 -07002818 if (head_need > 0 || skb_cloned(skb)) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002819 head_need += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02002820 head_need += local->tx_headroom;
2821 head_need = max_t(int, 0, head_need);
Johannes Berg14f46c12020-10-09 13:25:41 +02002822 if (ieee80211_skb_resize(sdata, skb, head_need, ENCRYPT_DATA)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02002823 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg1c963be2012-11-07 14:02:30 +01002824 skb = NULL;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002825 return ERR_PTR(-ENOMEM);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002826 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002827 }
2828
Felix Fietkaueae44302016-07-13 11:00:02 +02002829 if (encaps_data)
Johannes Berge2ebc742007-07-27 15:43:22 +02002830 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002831
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002832#ifdef CONFIG_MAC80211_MESH
Felix Fietkaueae44302016-07-13 11:00:02 +02002833 if (meshhdrlen > 0)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002834 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002835#endif
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002836
Harvey Harrison065e96052008-06-22 16:45:27 -07002837 if (ieee80211_is_data_qos(fc)) {
Johannes Bergc29b9b92007-09-14 11:10:24 -04002838 __le16 *qos_control;
2839
Johannes Bergd58ff352017-06-16 14:29:23 +02002840 qos_control = skb_push(skb, 2);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002841 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2842 /*
2843 * Maybe we could actually set some fields here, for now just
2844 * initialise to zero to indicate no special operation.
2845 */
2846 *qos_control = 0;
2847 } else
2848 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2849
Zhang Shengjud57a5442016-03-03 01:16:56 +00002850 skb_reset_mac_header(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002851
Felix Fietkau489ee912010-12-18 19:30:48 +01002852 info = IEEE80211_SKB_CB(skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002853 memset(info, 0, sizeof(*info));
2854
Johannes Berga729cff2011-11-06 14:13:34 +01002855 info->flags = info_flags;
2856 info->ack_frame_id = info_id;
Johannes Berg73c4e192014-11-09 18:50:09 +02002857 info->band = band;
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002858 info->control.flags = ctrl_flags;
Johannes Berga729cff2011-11-06 14:13:34 +01002859
Johannes Berg4c9451e2014-11-09 18:50:10 +02002860 return skb;
2861 free:
2862 kfree_skb(skb);
2863 return ERR_PTR(ret);
Johannes Berge2ebc742007-07-27 15:43:22 +02002864}
2865
Johannes Berg17c18bf2015-03-21 15:25:43 +01002866/*
2867 * fast-xmit overview
2868 *
2869 * The core idea of this fast-xmit is to remove per-packet checks by checking
2870 * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2871 * checks that are needed to get the sta->fast_tx pointer assigned, after which
2872 * much less work can be done per packet. For example, fragmentation must be
2873 * disabled or the fast_tx pointer will not be set. All the conditions are seen
2874 * in the code here.
2875 *
2876 * Once assigned, the fast_tx data structure also caches the per-packet 802.11
2877 * header and other data to aid packet processing in ieee80211_xmit_fast().
2878 *
2879 * The most difficult part of this is that when any of these assumptions
2880 * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
2881 * ieee80211_check_fast_xmit() or friends) is required to reset the data,
2882 * since the per-packet code no longer checks the conditions. This is reflected
2883 * by the calls to these functions throughout the rest of the code, and must be
2884 * maintained if any of the TX path checks change.
2885 */
2886
2887void ieee80211_check_fast_xmit(struct sta_info *sta)
2888{
2889 struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
2890 struct ieee80211_local *local = sta->local;
2891 struct ieee80211_sub_if_data *sdata = sta->sdata;
2892 struct ieee80211_hdr *hdr = (void *)build.hdr;
2893 struct ieee80211_chanctx_conf *chanctx_conf;
2894 __le16 fc;
2895
Johannes Berg30686bf2015-06-02 21:39:54 +02002896 if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002897 return;
2898
2899 /* Locking here protects both the pointer itself, and against concurrent
2900 * invocations winning data access races to, e.g., the key pointer that
2901 * is used.
2902 * Without it, the invocation of this function right after the key
2903 * pointer changes wouldn't be sufficient, as another CPU could access
2904 * the pointer, then stall, and then do the cache update after the CPU
2905 * that invalidated the key.
2906 * With the locking, such scenarios cannot happen as the check for the
2907 * key and the fast-tx assignment are done atomically, so the CPU that
2908 * modifies the key will either wait or other one will see the key
2909 * cleared/changed already.
2910 */
2911 spin_lock_bh(&sta->lock);
Johannes Berg30686bf2015-06-02 21:39:54 +02002912 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
2913 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
Johannes Berg17c18bf2015-03-21 15:25:43 +01002914 sdata->vif.type == NL80211_IFTYPE_STATION)
2915 goto out;
2916
2917 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2918 goto out;
2919
2920 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
2921 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
Felix Fietkauf7418bc2015-09-24 14:59:49 +02002922 test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
2923 test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002924 goto out;
2925
2926 if (sdata->noack_map)
2927 goto out;
2928
2929 /* fast-xmit doesn't handle fragmentation at all */
Johannes Berg725b8122015-04-10 14:02:08 +02002930 if (local->hw.wiphy->frag_threshold != (u32)-1 &&
Sara Sharonf3fe4e92016-10-18 23:12:11 +03002931 !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002932 goto out;
2933
2934 rcu_read_lock();
2935 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2936 if (!chanctx_conf) {
2937 rcu_read_unlock();
2938 goto out;
2939 }
2940 build.band = chanctx_conf->def.chan->band;
2941 rcu_read_unlock();
2942
2943 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2944
2945 switch (sdata->vif.type) {
Johannes Berg3ffd8842015-04-14 10:28:37 +02002946 case NL80211_IFTYPE_ADHOC:
2947 /* DA SA BSSID */
2948 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2949 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2950 memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
2951 build.hdr_len = 24;
2952 break;
Johannes Berg17c18bf2015-03-21 15:25:43 +01002953 case NL80211_IFTYPE_STATION:
2954 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2955 /* DA SA BSSID */
2956 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2957 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2958 memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
2959 build.hdr_len = 24;
2960 break;
2961 }
2962
2963 if (sdata->u.mgd.use_4addr) {
2964 /* non-regular ethertype cannot use the fastpath */
2965 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2966 IEEE80211_FCTL_TODS);
2967 /* RA TA DA SA */
2968 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2969 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2970 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2971 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2972 build.hdr_len = 30;
2973 break;
2974 }
2975 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2976 /* BSSID SA DA */
2977 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2978 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2979 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2980 build.hdr_len = 24;
2981 break;
2982 case NL80211_IFTYPE_AP_VLAN:
2983 if (sdata->wdev.use_4addr) {
2984 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2985 IEEE80211_FCTL_TODS);
2986 /* RA TA DA SA */
2987 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
2988 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2989 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2990 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2991 build.hdr_len = 30;
2992 break;
2993 }
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05002994 fallthrough;
Johannes Berg17c18bf2015-03-21 15:25:43 +01002995 case NL80211_IFTYPE_AP:
2996 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2997 /* DA BSSID SA */
2998 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2999 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
3000 build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
3001 build.hdr_len = 24;
3002 break;
3003 default:
3004 /* not handled on fast-xmit */
3005 goto out;
3006 }
3007
3008 if (sta->sta.wme) {
3009 build.hdr_len += 2;
3010 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3011 }
3012
3013 /* We store the key here so there's no point in using rcu_dereference()
3014 * but that's fine because the code that changes the pointers will call
3015 * this function after doing so. For a single CPU that would be enough,
3016 * for multiple see the comment above.
3017 */
3018 build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
3019 if (!build.key)
3020 build.key = rcu_access_pointer(sdata->default_unicast_key);
3021 if (build.key) {
Johannes Berge495c242015-04-10 14:03:17 +02003022 bool gen_iv, iv_spc, mmic;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003023
3024 gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
3025 iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
David Spinadel9de18d82017-12-01 13:50:52 +02003026 mmic = build.key->conf.flags &
3027 (IEEE80211_KEY_FLAG_GENERATE_MMIC |
3028 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003029
3030 /* don't handle software crypto */
3031 if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
3032 goto out;
3033
Alexander Wetzel62872a92018-08-31 15:00:38 +02003034 /* Key is being removed */
3035 if (build.key->flags & KEY_FLAG_TAINTED)
3036 goto out;
3037
Johannes Berg17c18bf2015-03-21 15:25:43 +01003038 switch (build.key->conf.cipher) {
3039 case WLAN_CIPHER_SUITE_CCMP:
3040 case WLAN_CIPHER_SUITE_CCMP_256:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003041 if (gen_iv)
Johannes Berg17c18bf2015-03-21 15:25:43 +01003042 build.pn_offs = build.hdr_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003043 if (gen_iv || iv_spc)
3044 build.hdr_len += IEEE80211_CCMP_HDR_LEN;
3045 break;
3046 case WLAN_CIPHER_SUITE_GCMP:
3047 case WLAN_CIPHER_SUITE_GCMP_256:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003048 if (gen_iv)
Johannes Berg17c18bf2015-03-21 15:25:43 +01003049 build.pn_offs = build.hdr_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003050 if (gen_iv || iv_spc)
3051 build.hdr_len += IEEE80211_GCMP_HDR_LEN;
3052 break;
Johannes Berge495c242015-04-10 14:03:17 +02003053 case WLAN_CIPHER_SUITE_TKIP:
3054 /* cannot handle MMIC or IV generation in xmit-fast */
3055 if (mmic || gen_iv)
3056 goto out;
3057 if (iv_spc)
3058 build.hdr_len += IEEE80211_TKIP_IV_LEN;
3059 break;
3060 case WLAN_CIPHER_SUITE_WEP40:
3061 case WLAN_CIPHER_SUITE_WEP104:
3062 /* cannot handle IV generation in fast-xmit */
3063 if (gen_iv)
3064 goto out;
3065 if (iv_spc)
3066 build.hdr_len += IEEE80211_WEP_IV_LEN;
3067 break;
3068 case WLAN_CIPHER_SUITE_AES_CMAC:
3069 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3070 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3071 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3072 WARN(1,
3073 "management cipher suite 0x%x enabled for data\n",
3074 build.key->conf.cipher);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003075 goto out;
Johannes Berge495c242015-04-10 14:03:17 +02003076 default:
3077 /* we don't know how to generate IVs for this at all */
3078 if (WARN_ON(gen_iv))
3079 goto out;
3080 /* pure hardware keys are OK, of course */
3081 if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
3082 break;
3083 /* cipher scheme might require space allocation */
3084 if (iv_spc &&
3085 build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
3086 goto out;
3087 if (iv_spc)
3088 build.hdr_len += build.key->conf.iv_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003089 }
3090
3091 fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3092 }
3093
3094 hdr->frame_control = fc;
3095
3096 memcpy(build.hdr + build.hdr_len,
3097 rfc1042_header, sizeof(rfc1042_header));
3098 build.hdr_len += sizeof(rfc1042_header);
3099
3100 fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3101 /* if the kmemdup fails, continue w/o fast_tx */
3102 if (!fast_tx)
3103 goto out;
3104
3105 out:
3106 /* we might have raced against another call to this function */
3107 old = rcu_dereference_protected(sta->fast_tx,
3108 lockdep_is_held(&sta->lock));
3109 rcu_assign_pointer(sta->fast_tx, fast_tx);
3110 if (old)
3111 kfree_rcu(old, rcu_head);
3112 spin_unlock_bh(&sta->lock);
3113}
3114
3115void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3116{
3117 struct sta_info *sta;
3118
3119 rcu_read_lock();
3120 list_for_each_entry_rcu(sta, &local->sta_list, list)
3121 ieee80211_check_fast_xmit(sta);
3122 rcu_read_unlock();
3123}
3124
3125void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3126{
3127 struct ieee80211_local *local = sdata->local;
3128 struct sta_info *sta;
3129
3130 rcu_read_lock();
3131
3132 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3133 if (sdata != sta->sdata &&
3134 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3135 continue;
3136 ieee80211_check_fast_xmit(sta);
3137 }
3138
3139 rcu_read_unlock();
3140}
3141
3142void ieee80211_clear_fast_xmit(struct sta_info *sta)
3143{
3144 struct ieee80211_fast_tx *fast_tx;
3145
3146 spin_lock_bh(&sta->lock);
3147 fast_tx = rcu_dereference_protected(sta->fast_tx,
3148 lockdep_is_held(&sta->lock));
3149 RCU_INIT_POINTER(sta->fast_tx, NULL);
3150 spin_unlock_bh(&sta->lock);
3151
3152 if (fast_tx)
3153 kfree_rcu(fast_tx, rcu_head);
3154}
3155
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003156static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
Sara Sharon166ac9d2018-08-29 08:57:02 +02003157 struct sk_buff *skb, int headroom)
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003158{
Sara Sharon166ac9d2018-08-29 08:57:02 +02003159 if (skb_headroom(skb) < headroom) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003160 I802_DEBUG_INC(local->tx_expand_skb_head);
3161
Sara Sharon166ac9d2018-08-29 08:57:02 +02003162 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003163 wiphy_debug(local->hw.wiphy,
3164 "failed to reallocate TX buffer\n");
3165 return false;
3166 }
3167 }
3168
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003169 return true;
3170}
3171
3172static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3173 struct ieee80211_fast_tx *fast_tx,
3174 struct sk_buff *skb)
3175{
3176 struct ieee80211_local *local = sdata->local;
3177 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3178 struct ieee80211_hdr *hdr;
Michael Braun06f2bb12016-10-15 13:28:18 +02003179 struct ethhdr *amsdu_hdr;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003180 int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3181 int subframe_len = skb->len - hdr_len;
3182 void *data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003183 u8 *qc, *h_80211_src, *h_80211_dst;
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003184 const u8 *bssid;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003185
3186 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3187 return false;
3188
3189 if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3190 return true;
3191
Sara Sharon166ac9d2018-08-29 08:57:02 +02003192 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(*amsdu_hdr)))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003193 return false;
3194
Michael Braun06f2bb12016-10-15 13:28:18 +02003195 data = skb_push(skb, sizeof(*amsdu_hdr));
3196 memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003197 hdr = data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003198 amsdu_hdr = data + hdr_len;
3199 /* h_80211_src/dst is addr* field within hdr */
3200 h_80211_src = data + fast_tx->sa_offs;
3201 h_80211_dst = data + fast_tx->da_offs;
3202
3203 amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3204 ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3205 ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
3206
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003207 /* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3208 * fields needs to be changed to BSSID for A-MSDU frames depending
3209 * on FromDS/ToDS values.
3210 */
3211 switch (sdata->vif.type) {
3212 case NL80211_IFTYPE_STATION:
3213 bssid = sdata->u.mgd.bssid;
3214 break;
3215 case NL80211_IFTYPE_AP:
3216 case NL80211_IFTYPE_AP_VLAN:
3217 bssid = sdata->vif.addr;
3218 break;
3219 default:
3220 bssid = NULL;
3221 }
3222
3223 if (bssid && ieee80211_has_fromds(hdr->frame_control))
3224 ether_addr_copy(h_80211_src, bssid);
3225
3226 if (bssid && ieee80211_has_tods(hdr->frame_control))
3227 ether_addr_copy(h_80211_dst, bssid);
3228
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003229 qc = ieee80211_get_qos_ctl(hdr);
3230 *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3231
3232 info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3233
3234 return true;
3235}
3236
3237static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3238 struct sta_info *sta,
3239 struct ieee80211_fast_tx *fast_tx,
3240 struct sk_buff *skb)
3241{
3242 struct ieee80211_local *local = sdata->local;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003243 struct fq *fq = &local->fq;
3244 struct fq_tin *tin;
3245 struct fq_flow *flow;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003246 u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3247 struct ieee80211_txq *txq = sta->sta.txq[tid];
3248 struct txq_info *txqi;
3249 struct sk_buff **frag_tail, *head;
3250 int subframe_len = skb->len - ETH_ALEN;
3251 u8 max_subframes = sta->sta.max_amsdu_subframes;
3252 int max_frags = local->hw.max_tx_fragments;
3253 int max_amsdu_len = sta->sta.max_amsdu_len;
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003254 int orig_truesize;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003255 u32 flow_idx;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003256 __be16 len;
3257 void *data;
3258 bool ret = false;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003259 unsigned int orig_len;
Lorenzo Bianconi66eb02d2018-08-31 01:04:13 +02003260 int n = 2, nfrags, pad = 0;
Sara Sharon166ac9d2018-08-29 08:57:02 +02003261 u16 hdrlen;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003262
3263 if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
3264 return false;
3265
Sara Sharon344f8e02018-12-15 11:03:07 +02003266 if (skb_is_gso(skb))
3267 return false;
3268
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003269 if (!txq)
3270 return false;
3271
3272 txqi = to_txq_info(txq);
3273 if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3274 return false;
3275
3276 if (sta->sta.max_rc_amsdu_len)
3277 max_amsdu_len = min_t(int, max_amsdu_len,
3278 sta->sta.max_rc_amsdu_len);
3279
Sara Sharonedba6bd2018-09-05 08:06:10 +03003280 if (sta->sta.max_tid_amsdu_len[tid])
3281 max_amsdu_len = min_t(int, max_amsdu_len,
3282 sta->sta.max_tid_amsdu_len[tid]);
3283
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003284 flow_idx = fq_flow_idx(fq, skb);
3285
Michal Kaziorfa962b92016-05-19 10:37:49 +02003286 spin_lock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003287
Michal Kaziorfa962b92016-05-19 10:37:49 +02003288 /* TODO: Ideally aggregation should be done on dequeue to remain
3289 * responsive to environment changes.
3290 */
3291
3292 tin = &txqi->tin;
Felix Fietkaubf9009b2020-12-18 19:47:14 +01003293 flow = fq_flow_classify(fq, tin, flow_idx, skb);
Michal Kaziorfa962b92016-05-19 10:37:49 +02003294 head = skb_peek_tail(&flow->queue);
Sara Sharon344f8e02018-12-15 11:03:07 +02003295 if (!head || skb_is_gso(head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003296 goto out;
3297
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003298 orig_truesize = head->truesize;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003299 orig_len = head->len;
3300
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003301 if (skb->len + head->len > max_amsdu_len)
3302 goto out;
3303
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003304 nfrags = 1 + skb_shinfo(skb)->nr_frags;
3305 nfrags += 1 + skb_shinfo(head)->nr_frags;
3306 frag_tail = &skb_shinfo(head)->frag_list;
3307 while (*frag_tail) {
3308 nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3309 frag_tail = &(*frag_tail)->next;
3310 n++;
3311 }
3312
3313 if (max_subframes && n > max_subframes)
3314 goto out;
3315
3316 if (max_frags && nfrags > max_frags)
3317 goto out;
3318
Sara Sharon9739fe22018-09-05 08:06:11 +03003319 if (!drv_can_aggregate_in_amsdu(local, head, skb))
3320 goto out;
3321
Lorenzo Bianconi1eb50792018-08-29 21:03:25 +02003322 if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003323 goto out;
3324
Sara Sharon166ac9d2018-08-29 08:57:02 +02003325 /*
3326 * Pad out the previous subframe to a multiple of 4 by adding the
3327 * padding to the next one, that's being added. Note that head->len
3328 * is the length of the full A-MSDU, but that works since each time
3329 * we add a new subframe we pad out the previous one to a multiple
3330 * of 4 and thus it no longer matters in the next round.
3331 */
3332 hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3333 if ((head->len - hdrlen) & 3)
3334 pad = 4 - ((head->len - hdrlen) & 3);
3335
3336 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3337 2 + pad))
Johannes Bergaa58acf2018-08-30 10:55:49 +02003338 goto out_recalc;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003339
3340 ret = true;
3341 data = skb_push(skb, ETH_ALEN + 2);
3342 memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3343
3344 data += 2 * ETH_ALEN;
3345 len = cpu_to_be16(subframe_len);
3346 memcpy(data, &len, 2);
3347 memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3348
Sara Sharon166ac9d2018-08-29 08:57:02 +02003349 memset(skb_push(skb, pad), 0, pad);
3350
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003351 head->len += skb->len;
3352 head->data_len += skb->len;
3353 *frag_tail = skb;
3354
Johannes Bergaa58acf2018-08-30 10:55:49 +02003355out_recalc:
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003356 fq->memory_usage += head->truesize - orig_truesize;
Johannes Bergaa58acf2018-08-30 10:55:49 +02003357 if (head->len != orig_len) {
3358 flow->backlog += head->len - orig_len;
3359 tin->backlog_bytes += head->len - orig_len;
Johannes Bergaa58acf2018-08-30 10:55:49 +02003360 }
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003361out:
Michal Kaziorfa962b92016-05-19 10:37:49 +02003362 spin_unlock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003363
3364 return ret;
3365}
3366
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003367/*
3368 * Can be called while the sta lock is held. Anything that can cause packets to
3369 * be generated will cause deadlock!
3370 */
3371static void ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3372 struct sta_info *sta, u8 pn_offs,
3373 struct ieee80211_key *key,
3374 struct sk_buff *skb)
3375{
3376 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3377 struct ieee80211_hdr *hdr = (void *)skb->data;
3378 u8 tid = IEEE80211_NUM_TIDS;
3379
3380 if (key)
3381 info->control.hw_key = &key->conf;
3382
Lev Stipakov36ec1442020-11-13 23:46:24 +02003383 dev_sw_netstats_tx_add(skb->dev, 1, skb->len);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003384
3385 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3386 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003387 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3388 } else {
3389 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3390 hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3391 sdata->sequence_number += 0x10;
3392 }
3393
3394 if (skb_shinfo(skb)->gso_size)
3395 sta->tx_stats.msdu[tid] +=
3396 DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3397 else
3398 sta->tx_stats.msdu[tid]++;
3399
3400 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3401
3402 /* statistics normally done by ieee80211_tx_h_stats (but that
3403 * has to consider fragmentation, so is more complex)
3404 */
3405 sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3406 sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
3407
3408 if (pn_offs) {
3409 u64 pn;
3410 u8 *crypto_hdr = skb->data + pn_offs;
3411
3412 switch (key->conf.cipher) {
3413 case WLAN_CIPHER_SUITE_CCMP:
3414 case WLAN_CIPHER_SUITE_CCMP_256:
3415 case WLAN_CIPHER_SUITE_GCMP:
3416 case WLAN_CIPHER_SUITE_GCMP_256:
3417 pn = atomic64_inc_return(&key->conf.tx_pn);
3418 crypto_hdr[0] = pn;
3419 crypto_hdr[1] = pn >> 8;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003420 crypto_hdr[3] = 0x20 | (key->conf.keyidx << 6);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003421 crypto_hdr[4] = pn >> 16;
3422 crypto_hdr[5] = pn >> 24;
3423 crypto_hdr[6] = pn >> 32;
3424 crypto_hdr[7] = pn >> 40;
3425 break;
3426 }
3427 }
3428}
3429
Johannes Berg17c18bf2015-03-21 15:25:43 +01003430static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003431 struct sta_info *sta,
Johannes Berg17c18bf2015-03-21 15:25:43 +01003432 struct ieee80211_fast_tx *fast_tx,
3433 struct sk_buff *skb)
3434{
3435 struct ieee80211_local *local = sdata->local;
3436 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3437 int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3438 int hw_headroom = sdata->local->hw.extra_tx_headroom;
3439 struct ethhdr eth;
Johannes Berg35f432a2017-01-02 11:19:29 +01003440 struct ieee80211_tx_info *info;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003441 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3442 struct ieee80211_tx_data tx;
3443 ieee80211_tx_result r;
3444 struct tid_ampdu_tx *tid_tx = NULL;
3445 u8 tid = IEEE80211_NUM_TIDS;
3446
3447 /* control port protocol needs a lot of special handling */
3448 if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3449 return false;
3450
3451 /* only RFC 1042 SNAP */
3452 if (ethertype < ETH_P_802_3_MIN)
3453 return false;
3454
3455 /* don't handle TX status request here either */
3456 if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3457 return false;
3458
3459 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3460 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3461 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
Johannes Berg472be002015-06-09 16:45:08 +02003462 if (tid_tx) {
3463 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3464 return false;
3465 if (tid_tx->timeout)
3466 tid_tx->last_tx = jiffies;
3467 }
Johannes Berg17c18bf2015-03-21 15:25:43 +01003468 }
3469
3470 /* after this point (skb is modified) we cannot return false */
3471
3472 if (skb_shared(skb)) {
3473 struct sk_buff *tmp_skb = skb;
3474
3475 skb = skb_clone(skb, GFP_ATOMIC);
3476 kfree_skb(tmp_skb);
3477
3478 if (!skb)
3479 return true;
3480 }
3481
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003482 if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3483 ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
3484 return true;
3485
Johannes Berg17c18bf2015-03-21 15:25:43 +01003486 /* will not be crypto-handled beyond what we do here, so use false
3487 * as the may-encrypt argument for the resize to not account for
3488 * more room than we already have in 'extra_head'
3489 */
3490 if (unlikely(ieee80211_skb_resize(sdata, skb,
3491 max_t(int, extra_head + hw_headroom -
3492 skb_headroom(skb), 0),
Johannes Berg14f46c12020-10-09 13:25:41 +02003493 ENCRYPT_NO))) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01003494 kfree_skb(skb);
3495 return true;
3496 }
3497
3498 memcpy(&eth, skb->data, ETH_HLEN - 2);
Johannes Bergd58ff352017-06-16 14:29:23 +02003499 hdr = skb_push(skb, extra_head);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003500 memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3501 memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
3502 memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
3503
Johannes Berg35f432a2017-01-02 11:19:29 +01003504 info = IEEE80211_SKB_CB(skb);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003505 memset(info, 0, sizeof(*info));
3506 info->band = fast_tx->band;
3507 info->control.vif = &sdata->vif;
3508 info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3509 IEEE80211_TX_CTL_DONTFRAG |
3510 (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003511 info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003512
Julius Niedworok276d9e82019-03-28 21:01:06 +01003513#ifdef CONFIG_MAC80211_DEBUGFS
3514 if (local->force_tx_status)
3515 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3516#endif
3517
Felix Fietkaua786f962016-11-04 10:27:54 +01003518 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3519 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3520 *ieee80211_get_qos_ctl(hdr) = tid;
3521 }
3522
Johannes Berg17c18bf2015-03-21 15:25:43 +01003523 __skb_queue_head_init(&tx.skbs);
3524
3525 tx.flags = IEEE80211_TX_UNICAST;
3526 tx.local = local;
3527 tx.sdata = sdata;
3528 tx.sta = sta;
3529 tx.key = fast_tx->key;
3530
Johannes Berg30686bf2015-06-02 21:39:54 +02003531 if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01003532 tx.skb = skb;
3533 r = ieee80211_tx_h_rate_ctrl(&tx);
3534 skb = tx.skb;
3535 tx.skb = NULL;
3536
3537 if (r != TX_CONTINUE) {
3538 if (r != TX_QUEUED)
3539 kfree_skb(skb);
3540 return true;
3541 }
3542 }
3543
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003544 if (ieee80211_queue_skb(local, sdata, sta, skb))
3545 return true;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003546
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003547 ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3548 fast_tx->key, skb);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003549
3550 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3551 sdata = container_of(sdata->bss,
3552 struct ieee80211_sub_if_data, u.ap);
3553
3554 __skb_queue_tail(&tx.skbs, skb);
Johannes Berg4fd03282019-10-01 23:26:35 +02003555 ieee80211_tx_frags(local, &sdata->vif, sta, &tx.skbs, false);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003556 return true;
3557}
3558
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003559struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3560 struct ieee80211_txq *txq)
3561{
3562 struct ieee80211_local *local = hw_to_local(hw);
3563 struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3564 struct ieee80211_hdr *hdr;
3565 struct sk_buff *skb = NULL;
3566 struct fq *fq = &local->fq;
3567 struct fq_tin *tin = &txqi->tin;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003568 struct ieee80211_tx_info *info;
3569 struct ieee80211_tx_data tx;
3570 ieee80211_tx_result r;
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303571 struct ieee80211_vif *vif = txq->vif;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003572
Erik Stromdahlfb0e76a2019-06-17 22:01:39 +02003573 WARN_ON_ONCE(softirq_count() == 0);
3574
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003575 if (!ieee80211_txq_airtime_check(hw, txq))
3576 return NULL;
3577
Felix Fietkauded46982019-03-16 18:06:33 +01003578begin:
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003579 spin_lock_bh(&fq->lock);
3580
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303581 if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags) ||
3582 test_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags))
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003583 goto out;
3584
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303585 if (vif->txqs_stopped[ieee80211_ac_from_tid(txq->tid)]) {
3586 set_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags);
3587 goto out;
3588 }
3589
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003590 /* Make sure fragments stay together. */
3591 skb = __skb_dequeue(&txqi->frags);
3592 if (skb)
3593 goto out;
3594
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003595 skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
3596 if (!skb)
3597 goto out;
3598
Felix Fietkauded46982019-03-16 18:06:33 +01003599 spin_unlock_bh(&fq->lock);
3600
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003601 hdr = (struct ieee80211_hdr *)skb->data;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003602 info = IEEE80211_SKB_CB(skb);
3603
3604 memset(&tx, 0, sizeof(tx));
3605 __skb_queue_head_init(&tx.skbs);
3606 tx.local = local;
3607 tx.skb = skb;
3608 tx.sdata = vif_to_sdata(info->control.vif);
3609
Mathy Vanhoef804fc6a2020-10-19 20:01:13 +04003610 if (txq->sta) {
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003611 tx.sta = container_of(txq->sta, struct sta_info, sta);
Jouni Malinence2e1ca2020-03-26 15:51:34 +01003612 /*
3613 * Drop unicast frames to unauthorised stations unless they are
Mathy Vanhoef804fc6a2020-10-19 20:01:13 +04003614 * injected frames or EAPOL frames from the local station.
Jouni Malinence2e1ca2020-03-26 15:51:34 +01003615 */
Mathy Vanhoef804fc6a2020-10-19 20:01:13 +04003616 if (unlikely(!(info->flags & IEEE80211_TX_CTL_INJECTED) &&
3617 ieee80211_is_data(hdr->frame_control) &&
Johannes Bergbe8c8272020-03-29 22:50:06 +02003618 !ieee80211_vif_is_mesh(&tx.sdata->vif) &&
Jouni Malinence2e1ca2020-03-26 15:51:34 +01003619 tx.sdata->vif.type != NL80211_IFTYPE_OCB &&
3620 !is_multicast_ether_addr(hdr->addr1) &&
3621 !test_sta_flag(tx.sta, WLAN_STA_AUTHORIZED) &&
3622 (!(info->control.flags &
3623 IEEE80211_TX_CTRL_PORT_CTRL_PROTO) ||
3624 !ether_addr_equal(tx.sdata->vif.addr,
3625 hdr->addr2)))) {
3626 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
3627 ieee80211_free_txskb(&local->hw, skb);
3628 goto begin;
3629 }
3630 }
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003631
3632 /*
3633 * The key can be removed while the packet was queued, so need to call
3634 * this here to get the current key.
3635 */
3636 r = ieee80211_tx_h_select_key(&tx);
3637 if (r != TX_CONTINUE) {
3638 ieee80211_free_txskb(&local->hw, skb);
3639 goto begin;
3640 }
3641
Felix Fietkauc1f4c9e2016-11-04 10:27:52 +01003642 if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
3643 info->flags |= IEEE80211_TX_CTL_AMPDU;
3644 else
3645 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
3646
Felix Fietkaucc20ff22020-09-08 14:36:57 +02003647 if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
John Crispin50ff4772019-11-25 11:04:37 +01003648 goto encap_out;
3649
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003650 if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003651 struct sta_info *sta = container_of(txq->sta, struct sta_info,
3652 sta);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003653 u8 pn_offs = 0;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003654
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003655 if (tx.key &&
3656 (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
3657 pn_offs = ieee80211_hdrlen(hdr->frame_control);
3658
3659 ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
3660 tx.key, skb);
3661 } else {
3662 if (invoke_tx_handlers_late(&tx))
3663 goto begin;
3664
3665 skb = __skb_dequeue(&tx.skbs);
3666
Felix Fietkauded46982019-03-16 18:06:33 +01003667 if (!skb_queue_empty(&tx.skbs)) {
3668 spin_lock_bh(&fq->lock);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003669 skb_queue_splice_tail(&tx.skbs, &txqi->frags);
Felix Fietkauded46982019-03-16 18:06:33 +01003670 spin_unlock_bh(&fq->lock);
3671 }
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003672 }
3673
Johannes Berg233e98d2018-12-15 11:03:09 +02003674 if (skb_has_frag_list(skb) &&
Johannes Berg1e1430d2016-10-04 09:22:19 +02003675 !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
3676 if (skb_linearize(skb)) {
3677 ieee80211_free_txskb(&local->hw, skb);
3678 goto begin;
3679 }
3680 }
3681
Johannes Berg53168212017-06-22 12:20:30 +02003682 switch (tx.sdata->vif.type) {
3683 case NL80211_IFTYPE_MONITOR:
3684 if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
3685 vif = &tx.sdata->vif;
3686 break;
3687 }
3688 tx.sdata = rcu_dereference(local->monitor_sdata);
3689 if (tx.sdata) {
3690 vif = &tx.sdata->vif;
3691 info->hw_queue =
3692 vif->hw_queue[skb_get_queue_mapping(skb)];
3693 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
3694 ieee80211_free_txskb(&local->hw, skb);
3695 goto begin;
3696 } else {
3697 vif = NULL;
3698 }
3699 break;
3700 case NL80211_IFTYPE_AP_VLAN:
3701 tx.sdata = container_of(tx.sdata->bss,
3702 struct ieee80211_sub_if_data, u.ap);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05003703 fallthrough;
Johannes Berg53168212017-06-22 12:20:30 +02003704 default:
3705 vif = &tx.sdata->vif;
3706 break;
3707 }
3708
John Crispin50ff4772019-11-25 11:04:37 +01003709encap_out:
Johannes Berg53168212017-06-22 12:20:30 +02003710 IEEE80211_SKB_CB(skb)->control.vif = vif;
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003711
Johannes Bergca98c472020-02-21 10:45:45 +01003712 if (vif &&
3713 wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) {
Felix Fietkau3ff901c2020-07-24 20:28:16 +02003714 bool ampdu = txq->ac != IEEE80211_AC_VO;
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003715 u32 airtime;
3716
3717 airtime = ieee80211_calc_expected_tx_airtime(hw, vif, txq->sta,
Felix Fietkau3ff901c2020-07-24 20:28:16 +02003718 skb->len, ampdu);
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003719 if (airtime) {
3720 airtime = ieee80211_info_set_tx_time_est(info, airtime);
3721 ieee80211_sta_update_pending_airtime(local, tx.sta,
3722 txq->ac,
3723 airtime,
3724 false);
3725 }
3726 }
3727
Felix Fietkauded46982019-03-16 18:06:33 +01003728 return skb;
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303729
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003730out:
3731 spin_unlock_bh(&fq->lock);
3732
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003733 return skb;
3734}
3735EXPORT_SYMBOL(ieee80211_tx_dequeue);
3736
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003737struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac)
3738{
3739 struct ieee80211_local *local = hw_to_local(hw);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003740 struct ieee80211_txq *ret = NULL;
Kan Yan3ace10f2019-11-18 22:06:09 -08003741 struct txq_info *txqi = NULL, *head = NULL;
3742 bool found_eligible_txq = false;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003743
Felix Fietkau5b989c12019-03-15 11:03:35 +01003744 spin_lock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003745
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003746 begin:
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003747 txqi = list_first_entry_or_null(&local->active_txqs[ac],
3748 struct txq_info,
3749 schedule_order);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003750 if (!txqi)
Felix Fietkau5b989c12019-03-15 11:03:35 +01003751 goto out;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003752
Kan Yan3ace10f2019-11-18 22:06:09 -08003753 if (txqi == head) {
3754 if (!found_eligible_txq)
3755 goto out;
3756 else
3757 found_eligible_txq = false;
3758 }
3759
3760 if (!head)
3761 head = txqi;
3762
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003763 if (txqi->txq.sta) {
3764 struct sta_info *sta = container_of(txqi->txq.sta,
Kan Yan3ace10f2019-11-18 22:06:09 -08003765 struct sta_info, sta);
3766 bool aql_check = ieee80211_txq_airtime_check(hw, &txqi->txq);
3767 s64 deficit = sta->airtime[txqi->txq.ac].deficit;
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003768
Kan Yan3ace10f2019-11-18 22:06:09 -08003769 if (aql_check)
3770 found_eligible_txq = true;
3771
3772 if (deficit < 0)
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003773 sta->airtime[txqi->txq.ac].deficit +=
3774 sta->airtime_weight;
Kan Yan3ace10f2019-11-18 22:06:09 -08003775
3776 if (deficit < 0 || !aql_check) {
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003777 list_move_tail(&txqi->schedule_order,
3778 &local->active_txqs[txqi->txq.ac]);
3779 goto begin;
3780 }
3781 }
3782
3783
3784 if (txqi->schedule_round == local->schedule_round[ac])
Felix Fietkau5b989c12019-03-15 11:03:35 +01003785 goto out;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003786
3787 list_del_init(&txqi->schedule_order);
3788 txqi->schedule_round = local->schedule_round[ac];
Felix Fietkau5b989c12019-03-15 11:03:35 +01003789 ret = &txqi->txq;
3790
3791out:
3792 spin_unlock_bh(&local->active_txq_lock[ac]);
3793 return ret;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003794}
3795EXPORT_SYMBOL(ieee80211_next_txq);
3796
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003797void __ieee80211_schedule_txq(struct ieee80211_hw *hw,
3798 struct ieee80211_txq *txq,
3799 bool force)
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003800{
3801 struct ieee80211_local *local = hw_to_local(hw);
3802 struct txq_info *txqi = to_txq_info(txq);
3803
Felix Fietkau5b989c12019-03-15 11:03:35 +01003804 spin_lock_bh(&local->active_txq_lock[txq->ac]);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003805
3806 if (list_empty(&txqi->schedule_order) &&
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003807 (force || !skb_queue_empty(&txqi->frags) ||
3808 txqi->tin.backlog_packets)) {
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003809 /* If airtime accounting is active, always enqueue STAs at the
3810 * head of the list to ensure that they only get moved to the
3811 * back by the airtime DRR scheduler once they have a negative
3812 * deficit. A station that already has a negative deficit will
3813 * get immediately moved to the back of the list on the next
3814 * call to ieee80211_next_txq().
3815 */
Lorenzo Bianconic13cf5c2020-12-26 10:39:08 +01003816 if (txqi->txq.sta && local->airtime_flags &&
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003817 wiphy_ext_feature_isset(local->hw.wiphy,
3818 NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
3819 list_add(&txqi->schedule_order,
3820 &local->active_txqs[txq->ac]);
3821 else
3822 list_add_tail(&txqi->schedule_order,
3823 &local->active_txqs[txq->ac]);
3824 }
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003825
Toke Høiland-Jørgensen390298e2019-01-22 15:20:16 +01003826 spin_unlock_bh(&local->active_txq_lock[txq->ac]);
3827}
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003828EXPORT_SYMBOL(__ieee80211_schedule_txq);
Toke Høiland-Jørgensen390298e2019-01-22 15:20:16 +01003829
Lorenzo Bianconie9084352021-01-09 18:57:51 +01003830DEFINE_STATIC_KEY_FALSE(aql_disable);
3831
Kan Yan3ace10f2019-11-18 22:06:09 -08003832bool ieee80211_txq_airtime_check(struct ieee80211_hw *hw,
3833 struct ieee80211_txq *txq)
3834{
3835 struct sta_info *sta;
3836 struct ieee80211_local *local = hw_to_local(hw);
3837
Toke Høiland-Jørgensen911bde02019-12-12 12:14:37 +01003838 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
Kan Yan3ace10f2019-11-18 22:06:09 -08003839 return true;
3840
Lorenzo Bianconie9084352021-01-09 18:57:51 +01003841 if (static_branch_unlikely(&aql_disable))
3842 return true;
3843
Kan Yan3ace10f2019-11-18 22:06:09 -08003844 if (!txq->sta)
3845 return true;
3846
Johannes Berg73bc9e02021-03-19 23:28:01 +01003847 if (unlikely(txq->tid == IEEE80211_NUM_TIDS))
3848 return true;
3849
Kan Yan3ace10f2019-11-18 22:06:09 -08003850 sta = container_of(txq->sta, struct sta_info, sta);
3851 if (atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
3852 sta->airtime[txq->ac].aql_limit_low)
3853 return true;
3854
3855 if (atomic_read(&local->aql_total_pending_airtime) <
3856 local->aql_threshold &&
3857 atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
3858 sta->airtime[txq->ac].aql_limit_high)
3859 return true;
3860
3861 return false;
3862}
3863EXPORT_SYMBOL(ieee80211_txq_airtime_check);
3864
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003865bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
3866 struct ieee80211_txq *txq)
3867{
3868 struct ieee80211_local *local = hw_to_local(hw);
3869 struct txq_info *iter, *tmp, *txqi = to_txq_info(txq);
3870 struct sta_info *sta;
3871 u8 ac = txq->ac;
3872
Felix Fietkau5b989c12019-03-15 11:03:35 +01003873 spin_lock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003874
3875 if (!txqi->txq.sta)
3876 goto out;
3877
3878 if (list_empty(&txqi->schedule_order))
3879 goto out;
3880
3881 list_for_each_entry_safe(iter, tmp, &local->active_txqs[ac],
3882 schedule_order) {
3883 if (iter == txqi)
3884 break;
3885
3886 if (!iter->txq.sta) {
3887 list_move_tail(&iter->schedule_order,
3888 &local->active_txqs[ac]);
3889 continue;
3890 }
3891 sta = container_of(iter->txq.sta, struct sta_info, sta);
3892 if (sta->airtime[ac].deficit < 0)
3893 sta->airtime[ac].deficit += sta->airtime_weight;
3894 list_move_tail(&iter->schedule_order, &local->active_txqs[ac]);
3895 }
3896
3897 sta = container_of(txqi->txq.sta, struct sta_info, sta);
3898 if (sta->airtime[ac].deficit >= 0)
3899 goto out;
3900
3901 sta->airtime[ac].deficit += sta->airtime_weight;
3902 list_move_tail(&txqi->schedule_order, &local->active_txqs[ac]);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003903 spin_unlock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003904
3905 return false;
3906out:
3907 if (!list_empty(&txqi->schedule_order))
3908 list_del_init(&txqi->schedule_order);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003909 spin_unlock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003910
3911 return true;
3912}
3913EXPORT_SYMBOL(ieee80211_txq_may_transmit);
3914
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003915void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003916{
3917 struct ieee80211_local *local = hw_to_local(hw);
3918
3919 spin_lock_bh(&local->active_txq_lock[ac]);
3920 local->schedule_round[ac]++;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003921 spin_unlock_bh(&local->active_txq_lock[ac]);
3922}
Felix Fietkau5b989c12019-03-15 11:03:35 +01003923EXPORT_SYMBOL(ieee80211_txq_schedule_start);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003924
Johannes Berg4c9451e2014-11-09 18:50:10 +02003925void __ieee80211_subif_start_xmit(struct sk_buff *skb,
3926 struct net_device *dev,
Rajkumar Manoharan06016772019-04-11 13:47:25 -07003927 u32 info_flags,
Markus Theila7528192020-05-27 18:03:34 +02003928 u32 ctrl_flags,
3929 u64 *cookie)
Johannes Berg4c9451e2014-11-09 18:50:10 +02003930{
3931 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Felix Fietkau1974da82019-03-25 08:59:23 +01003932 struct ieee80211_local *local = sdata->local;
Johannes Berg97ffe752015-03-21 09:13:45 +01003933 struct sta_info *sta;
Johannes Berg80616c02015-04-14 14:50:41 +02003934 struct sk_buff *next;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003935
3936 if (unlikely(skb->len < ETH_HLEN)) {
3937 kfree_skb(skb);
3938 return;
3939 }
3940
3941 rcu_read_lock();
3942
Johannes Berg2d981fd2015-04-10 14:10:10 +02003943 if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
3944 goto out_free;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003945
Felix Fietkau1974da82019-03-25 08:59:23 +01003946 if (IS_ERR(sta))
3947 sta = NULL;
3948
3949 if (local->ops->wake_tx_queue) {
3950 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
3951 skb_set_queue_mapping(skb, queue);
Felix Fietkau180ac482020-07-26 15:09:47 +02003952 skb_get_hash(skb);
Felix Fietkau1974da82019-03-25 08:59:23 +01003953 }
3954
3955 if (sta) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01003956 struct ieee80211_fast_tx *fast_tx;
3957
Wen Gong70e53662018-08-08 18:40:01 +08003958 sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
Toke Høiland-Jørgensen36148c22018-02-02 16:11:05 +01003959
Johannes Berg17c18bf2015-03-21 15:25:43 +01003960 fast_tx = rcu_dereference(sta->fast_tx);
3961
3962 if (fast_tx &&
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003963 ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
Johannes Berg17c18bf2015-03-21 15:25:43 +01003964 goto out;
3965 }
3966
Johannes Berg80616c02015-04-14 14:50:41 +02003967 if (skb_is_gso(skb)) {
3968 struct sk_buff *segs;
Johannes Berg680a0da2015-04-13 16:58:25 +02003969
Johannes Berg80616c02015-04-14 14:50:41 +02003970 segs = skb_gso_segment(skb, 0);
3971 if (IS_ERR(segs)) {
Johannes Berg2d981fd2015-04-10 14:10:10 +02003972 goto out_free;
Johannes Berg80616c02015-04-14 14:50:41 +02003973 } else if (segs) {
3974 consume_skb(skb);
3975 skb = segs;
3976 }
3977 } else {
3978 /* we cannot process non-linear frames on this path */
3979 if (skb_linearize(skb)) {
3980 kfree_skb(skb);
3981 goto out;
3982 }
3983
3984 /* the frame could be fragmented, software-encrypted, and other
3985 * things so we cannot really handle checksum offload with it -
3986 * fix it up in software before we handle anything else.
3987 */
3988 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Johannes Bergf603f1f2015-05-05 15:25:33 +02003989 skb_set_transport_header(skb,
3990 skb_checksum_start_offset(skb));
Johannes Berg80616c02015-04-14 14:50:41 +02003991 if (skb_checksum_help(skb))
3992 goto out_free;
3993 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02003994 }
3995
Jason A. Donenfeld9f3ef3d2020-01-13 18:42:33 -05003996 skb_list_walk_safe(skb, skb, next) {
3997 skb_mark_not_on_list(skb);
Johannes Berg4c9451e2014-11-09 18:50:10 +02003998
Markus Theil5af7fef2020-06-17 10:26:36 +02003999 if (skb->protocol == sdata->control_port_protocol)
4000 ctrl_flags |= IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
4001
Rajkumar Manoharan06016772019-04-11 13:47:25 -07004002 skb = ieee80211_build_hdr(sdata, skb, info_flags,
Markus Theila7528192020-05-27 18:03:34 +02004003 sta, ctrl_flags, cookie);
Jason A. Donenfeld9f3ef3d2020-01-13 18:42:33 -05004004 if (IS_ERR(skb)) {
4005 kfree_skb_list(next);
Johannes Berg80616c02015-04-14 14:50:41 +02004006 goto out;
Jason A. Donenfeld9f3ef3d2020-01-13 18:42:33 -05004007 }
Johannes Berg80616c02015-04-14 14:50:41 +02004008
Lev Stipakov36ec1442020-11-13 23:46:24 +02004009 dev_sw_netstats_tx_add(dev, 1, skb->len);
Johannes Berg80616c02015-04-14 14:50:41 +02004010
Mathy Vanhoef08aca292020-07-23 14:01:52 +04004011 ieee80211_xmit(sdata, sta, skb);
Johannes Berg80616c02015-04-14 14:50:41 +02004012 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02004013 goto out;
4014 out_free:
4015 kfree_skb(skb);
Johannes Berg4c9451e2014-11-09 18:50:10 +02004016 out:
4017 rcu_read_unlock();
4018}
4019
Michael Braunebceec82016-11-22 11:52:18 +01004020static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
4021{
4022 struct ethhdr *eth;
4023 int err;
4024
4025 err = skb_ensure_writable(skb, ETH_HLEN);
4026 if (unlikely(err))
4027 return err;
4028
4029 eth = (void *)skb->data;
4030 ether_addr_copy(eth->h_dest, sta->sta.addr);
4031
4032 return 0;
4033}
4034
4035static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
4036 struct net_device *dev)
4037{
4038 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4039 const struct ethhdr *eth = (void *)skb->data;
4040 const struct vlan_ethhdr *ethvlan = (void *)skb->data;
4041 __be16 ethertype;
4042
4043 if (likely(!is_multicast_ether_addr(eth->h_dest)))
4044 return false;
4045
4046 switch (sdata->vif.type) {
4047 case NL80211_IFTYPE_AP_VLAN:
4048 if (sdata->u.vlan.sta)
4049 return false;
4050 if (sdata->wdev.use_4addr)
4051 return false;
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05004052 fallthrough;
Michael Braunebceec82016-11-22 11:52:18 +01004053 case NL80211_IFTYPE_AP:
4054 /* check runtime toggle for this bss */
4055 if (!sdata->bss->multicast_to_unicast)
4056 return false;
4057 break;
4058 default:
4059 return false;
4060 }
4061
4062 /* multicast to unicast conversion only for some payload */
4063 ethertype = eth->h_proto;
4064 if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
4065 ethertype = ethvlan->h_vlan_encapsulated_proto;
4066 switch (ethertype) {
4067 case htons(ETH_P_ARP):
4068 case htons(ETH_P_IP):
4069 case htons(ETH_P_IPV6):
4070 break;
4071 default:
4072 return false;
4073 }
4074
4075 return true;
4076}
4077
4078static void
4079ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
4080 struct sk_buff_head *queue)
4081{
4082 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4083 struct ieee80211_local *local = sdata->local;
4084 const struct ethhdr *eth = (struct ethhdr *)skb->data;
4085 struct sta_info *sta, *first = NULL;
4086 struct sk_buff *cloned_skb;
4087
4088 rcu_read_lock();
4089
4090 list_for_each_entry_rcu(sta, &local->sta_list, list) {
4091 if (sdata != sta->sdata)
4092 /* AP-VLAN mismatch */
4093 continue;
4094 if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
4095 /* do not send back to source */
4096 continue;
4097 if (!first) {
4098 first = sta;
4099 continue;
4100 }
4101 cloned_skb = skb_clone(skb, GFP_ATOMIC);
4102 if (!cloned_skb)
4103 goto multicast;
4104 if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
4105 dev_kfree_skb(cloned_skb);
4106 goto multicast;
4107 }
4108 __skb_queue_tail(queue, cloned_skb);
4109 }
4110
4111 if (likely(first)) {
4112 if (unlikely(ieee80211_change_da(skb, first)))
4113 goto multicast;
4114 __skb_queue_tail(queue, skb);
4115 } else {
4116 /* no STA connected, drop */
4117 kfree_skb(skb);
4118 skb = NULL;
4119 }
4120
4121 goto out;
4122multicast:
4123 __skb_queue_purge(queue);
4124 __skb_queue_tail(queue, skb);
4125out:
4126 rcu_read_unlock();
4127}
4128
Johannes Berg4c9451e2014-11-09 18:50:10 +02004129/**
4130 * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
4131 * @skb: packet to be sent
4132 * @dev: incoming interface
4133 *
4134 * On failure skb will be freed.
4135 */
Liad Kaufman24d342c2014-11-09 18:50:07 +02004136netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
4137 struct net_device *dev)
4138{
Michael Braunebceec82016-11-22 11:52:18 +01004139 if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
4140 struct sk_buff_head queue;
4141
4142 __skb_queue_head_init(&queue);
4143 ieee80211_convert_to_unicast(skb, dev, &queue);
4144 while ((skb = __skb_dequeue(&queue)))
Markus Theila7528192020-05-27 18:03:34 +02004145 __ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
Michael Braunebceec82016-11-22 11:52:18 +01004146 } else {
Markus Theila7528192020-05-27 18:03:34 +02004147 __ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
Michael Braunebceec82016-11-22 11:52:18 +01004148 }
4149
Liad Kaufman24d342c2014-11-09 18:50:07 +02004150 return NETDEV_TX_OK;
4151}
Johannes Berge2ebc742007-07-27 15:43:22 +02004152
John Crispin50ff4772019-11-25 11:04:37 +01004153static bool ieee80211_tx_8023(struct ieee80211_sub_if_data *sdata,
4154 struct sk_buff *skb, int led_len,
4155 struct sta_info *sta,
4156 bool txpending)
4157{
4158 struct ieee80211_local *local = sdata->local;
4159 struct ieee80211_tx_control control = {};
4160 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4161 struct ieee80211_sta *pubsta = NULL;
4162 unsigned long flags;
4163 int q = info->hw_queue;
4164
Lorenzo Bianconi196900f2021-03-08 23:01:49 +01004165 if (sta)
4166 sk_pacing_shift_update(skb->sk, local->hw.tx_sk_pacing_shift);
4167
John Crispin50ff4772019-11-25 11:04:37 +01004168 if (ieee80211_queue_skb(local, sdata, sta, skb))
4169 return true;
4170
4171 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4172
4173 if (local->queue_stop_reasons[q] ||
4174 (!txpending && !skb_queue_empty(&local->pending[q]))) {
4175 if (txpending)
4176 skb_queue_head(&local->pending[q], skb);
4177 else
4178 skb_queue_tail(&local->pending[q], skb);
4179
4180 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4181
4182 return false;
4183 }
4184
4185 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4186
4187 if (sta && sta->uploaded)
4188 pubsta = &sta->sta;
4189
4190 control.sta = pubsta;
4191
4192 drv_tx(local, &control, skb);
4193
4194 return true;
4195}
4196
4197static void ieee80211_8023_xmit(struct ieee80211_sub_if_data *sdata,
4198 struct net_device *dev, struct sta_info *sta,
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004199 struct ieee80211_key *key, struct sk_buff *skb)
John Crispin50ff4772019-11-25 11:04:37 +01004200{
4201 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
John Crispin50ff4772019-11-25 11:04:37 +01004202 struct ieee80211_local *local = sdata->local;
Felix Fietkau96ae9cd2020-09-08 14:36:50 +02004203 struct tid_ampdu_tx *tid_tx;
4204 u8 tid;
John Crispin50ff4772019-11-25 11:04:37 +01004205
Felix Fietkau5f8d69e2020-09-08 14:36:49 +02004206 if (local->ops->wake_tx_queue) {
4207 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
4208 skb_set_queue_mapping(skb, queue);
4209 skb_get_hash(skb);
4210 }
4211
John Crispin50ff4772019-11-25 11:04:37 +01004212 if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning)) &&
4213 test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
4214 goto out_free;
4215
Vasanthakumar Thiagarajane61fbfc2020-07-22 19:50:17 +05304216 memset(info, 0, sizeof(*info));
4217
Felix Fietkauaea6a3f2020-09-08 14:36:51 +02004218 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
4219 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
4220 if (tid_tx) {
4221 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
4222 /* fall back to non-offload slow path */
4223 __ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
4224 return;
Felix Fietkau96ae9cd2020-09-08 14:36:50 +02004225 }
Felix Fietkauaea6a3f2020-09-08 14:36:51 +02004226
4227 info->flags |= IEEE80211_TX_CTL_AMPDU;
4228 if (tid_tx->timeout)
4229 tid_tx->last_tx = jiffies;
Felix Fietkau96ae9cd2020-09-08 14:36:50 +02004230 }
4231
Felix Fietkauaea6a3f2020-09-08 14:36:51 +02004232 if (unlikely(skb->sk &&
John Crispin50ff4772019-11-25 11:04:37 +01004233 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS))
Vasanthakumar Thiagarajane61fbfc2020-07-22 19:50:17 +05304234 info->ack_frame_id = ieee80211_store_ack_skb(local, skb,
4235 &info->flags, NULL);
John Crispin50ff4772019-11-25 11:04:37 +01004236
John Crispin50ff4772019-11-25 11:04:37 +01004237 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
4238
Lev Stipakov36ec1442020-11-13 23:46:24 +02004239 dev_sw_netstats_tx_add(dev, 1, skb->len);
John Crispin50ff4772019-11-25 11:04:37 +01004240
Felix Fietkauaea6a3f2020-09-08 14:36:51 +02004241 sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
4242 sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
John Crispin50ff4772019-11-25 11:04:37 +01004243
4244 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4245 sdata = container_of(sdata->bss,
4246 struct ieee80211_sub_if_data, u.ap);
4247
Felix Fietkaucc20ff22020-09-08 14:36:57 +02004248 info->flags |= IEEE80211_TX_CTL_HW_80211_ENCAP;
John Crispin50ff4772019-11-25 11:04:37 +01004249 info->control.vif = &sdata->vif;
4250
Felix Fietkauae045152020-09-08 14:36:52 +02004251 if (key)
4252 info->control.hw_key = &key->conf;
4253
John Crispin50ff4772019-11-25 11:04:37 +01004254 ieee80211_tx_8023(sdata, skb, skb->len, sta, false);
4255
4256 return;
4257
4258out_free:
4259 kfree_skb(skb);
4260}
4261
4262netdev_tx_t ieee80211_subif_start_xmit_8023(struct sk_buff *skb,
4263 struct net_device *dev)
4264{
4265 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Felix Fietkauaea6a3f2020-09-08 14:36:51 +02004266 struct ethhdr *ehdr = (struct ethhdr *)skb->data;
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004267 struct ieee80211_key *key;
John Crispin50ff4772019-11-25 11:04:37 +01004268 struct sta_info *sta;
John Crispin50ff4772019-11-25 11:04:37 +01004269
4270 if (unlikely(skb->len < ETH_HLEN)) {
4271 kfree_skb(skb);
4272 return NETDEV_TX_OK;
4273 }
4274
4275 rcu_read_lock();
4276
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004277 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
John Crispin50ff4772019-11-25 11:04:37 +01004278 kfree_skb(skb);
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004279 goto out;
4280 }
John Crispin50ff4772019-11-25 11:04:37 +01004281
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004282 if (unlikely(IS_ERR_OR_NULL(sta) || !sta->uploaded ||
4283 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
Felix Fietkaub101dd22020-12-18 19:47:16 +01004284 sdata->control_port_protocol == ehdr->h_proto))
4285 goto skip_offload;
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004286
Felix Fietkaub101dd22020-12-18 19:47:16 +01004287 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4288 if (!key)
4289 key = rcu_dereference(sdata->default_unicast_key);
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004290
Felix Fietkaub101dd22020-12-18 19:47:16 +01004291 if (key && (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) ||
4292 key->conf.cipher == WLAN_CIPHER_SUITE_TKIP))
4293 goto skip_offload;
4294
4295 ieee80211_8023_xmit(sdata, dev, sta, key, skb);
4296 goto out;
4297
4298skip_offload:
4299 ieee80211_subif_start_xmit(skb, dev);
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004300out:
John Crispin50ff4772019-11-25 11:04:37 +01004301 rcu_read_unlock();
4302
4303 return NETDEV_TX_OK;
4304}
4305
Johannes Berg7528ec52014-11-09 18:50:11 +02004306struct sk_buff *
4307ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
4308 struct sk_buff *skb, u32 info_flags)
4309{
4310 struct ieee80211_hdr *hdr;
4311 struct ieee80211_tx_data tx = {
4312 .local = sdata->local,
4313 .sdata = sdata,
4314 };
Johannes Berg97ffe752015-03-21 09:13:45 +01004315 struct sta_info *sta;
Johannes Berg7528ec52014-11-09 18:50:11 +02004316
4317 rcu_read_lock();
4318
Johannes Berg97ffe752015-03-21 09:13:45 +01004319 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4320 kfree_skb(skb);
4321 skb = ERR_PTR(-EINVAL);
4322 goto out;
4323 }
4324
Markus Theila7528192020-05-27 18:03:34 +02004325 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta, 0, NULL);
Johannes Berg7528ec52014-11-09 18:50:11 +02004326 if (IS_ERR(skb))
4327 goto out;
4328
4329 hdr = (void *)skb->data;
4330 tx.sta = sta_info_get(sdata, hdr->addr1);
4331 tx.skb = skb;
4332
4333 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
4334 rcu_read_unlock();
4335 kfree_skb(skb);
4336 return ERR_PTR(-EINVAL);
4337 }
4338
4339out:
4340 rcu_read_unlock();
4341 return skb;
4342}
4343
Johannes Berge2530082008-05-17 00:57:14 +02004344/*
4345 * ieee80211_clear_tx_pending may not be called in a context where
4346 * it is possible that it packets could come in again.
4347 */
Johannes Berge2ebc742007-07-27 15:43:22 +02004348void ieee80211_clear_tx_pending(struct ieee80211_local *local)
4349{
Felix Fietkau1f98ab72012-11-10 03:44:14 +01004350 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01004351 int i;
Johannes Berge2ebc742007-07-27 15:43:22 +02004352
Felix Fietkau1f98ab72012-11-10 03:44:14 +01004353 for (i = 0; i < local->hw.queues; i++) {
4354 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
4355 ieee80211_free_txskb(&local->hw, skb);
4356 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004357}
4358
Johannes Berg7bb45682011-02-24 14:42:06 +01004359/*
4360 * Returns false if the frame couldn't be transmitted but was queued instead,
4361 * which in this case means re-queued -- take as an indication to stop sending
4362 * more pending frames.
4363 */
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004364static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
4365 struct sk_buff *skb)
4366{
4367 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4368 struct ieee80211_sub_if_data *sdata;
4369 struct sta_info *sta;
4370 struct ieee80211_hdr *hdr;
Johannes Berg7bb45682011-02-24 14:42:06 +01004371 bool result;
Johannes Berg55de9082012-07-26 17:24:39 +02004372 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004373
Johannes Berg5061b0c2009-07-14 00:33:34 +02004374 sdata = vif_to_sdata(info->control.vif);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004375
Felix Fietkaucc20ff22020-09-08 14:36:57 +02004376 if (info->control.flags & IEEE80211_TX_INTCFL_NEED_TXPROCESSING) {
Johannes Berg55de9082012-07-26 17:24:39 +02004377 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4378 if (unlikely(!chanctx_conf)) {
4379 dev_kfree_skb(skb);
4380 return true;
4381 }
Johannes Berg73c4e192014-11-09 18:50:09 +02004382 info->band = chanctx_conf->def.chan->band;
Mathy Vanhoef08aca292020-07-23 14:01:52 +04004383 result = ieee80211_tx(sdata, NULL, skb, true);
Felix Fietkaucc20ff22020-09-08 14:36:57 +02004384 } else if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
John Crispin50ff4772019-11-25 11:04:37 +01004385 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4386 dev_kfree_skb(skb);
4387 return true;
4388 }
4389
4390 if (IS_ERR(sta) || (sta && !sta->uploaded))
4391 sta = NULL;
4392
4393 result = ieee80211_tx_8023(sdata, skb, skb->len, sta, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004394 } else {
Johannes Berg252b86c2011-11-16 15:28:55 +01004395 struct sk_buff_head skbs;
4396
4397 __skb_queue_head_init(&skbs);
4398 __skb_queue_tail(&skbs, skb);
4399
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004400 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Bergabe60632009-11-25 17:46:18 +01004401 sta = sta_info_get(sdata, hdr->addr1);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004402
Johannes Berg74e4dbf2011-11-16 15:28:57 +01004403 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004404 }
4405
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004406 return result;
4407}
4408
Johannes Berge2530082008-05-17 00:57:14 +02004409/*
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004410 * Transmit all pending packets. Called from tasklet.
Johannes Berge2530082008-05-17 00:57:14 +02004411 */
Allen Paisda1cad72020-11-03 14:48:18 +05304412void ieee80211_tx_pending(struct tasklet_struct *t)
Johannes Berge2ebc742007-07-27 15:43:22 +02004413{
Allen Paisda1cad72020-11-03 14:48:18 +05304414 struct ieee80211_local *local = from_tasklet(local, t,
4415 tx_pending_tasklet);
Johannes Berg2a577d92009-03-23 17:28:37 +01004416 unsigned long flags;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004417 int i;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004418 bool txok;
Johannes Berge2ebc742007-07-27 15:43:22 +02004419
Johannes Bergf0e72852009-03-23 17:28:36 +01004420 rcu_read_lock();
Johannes Berg2a577d92009-03-23 17:28:37 +01004421
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004422 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg176be722009-03-12 23:49:28 +01004423 for (i = 0; i < local->hw.queues; i++) {
Johannes Berg2a577d92009-03-23 17:28:37 +01004424 /*
4425 * If queue is stopped by something other than due to pending
4426 * frames, or we have no pending frames, proceed to next queue.
4427 */
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004428 if (local->queue_stop_reasons[i] ||
Johannes Berg2a577d92009-03-23 17:28:37 +01004429 skb_queue_empty(&local->pending[i]))
Johannes Berge2ebc742007-07-27 15:43:22 +02004430 continue;
Johannes Berge2530082008-05-17 00:57:14 +02004431
Johannes Berg2a577d92009-03-23 17:28:37 +01004432 while (!skb_queue_empty(&local->pending[i])) {
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004433 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
Johannes Berg5061b0c2009-07-14 00:33:34 +02004434 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg5061b0c2009-07-14 00:33:34 +02004435
Johannes Berga7bc3762009-07-27 10:33:31 +02004436 if (WARN_ON(!info->control.vif)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02004437 ieee80211_free_txskb(&local->hw, skb);
Johannes Berga7bc3762009-07-27 10:33:31 +02004438 continue;
4439 }
4440
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004441 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
4442 flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01004443
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004444 txok = ieee80211_tx_pending_skb(local, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004445 spin_lock_irqsave(&local->queue_stop_reason_lock,
4446 flags);
4447 if (!txok)
Johannes Berg2a577d92009-03-23 17:28:37 +01004448 break;
Johannes Berge2ebc742007-07-27 15:43:22 +02004449 }
Johannes Berg7236fe22010-03-22 13:42:43 -07004450
4451 if (skb_queue_empty(&local->pending[i]))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02004452 ieee80211_propagate_queue_wake(local, i);
Johannes Berge2ebc742007-07-27 15:43:22 +02004453 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004454 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01004455
Johannes Bergf0e72852009-03-23 17:28:36 +01004456 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004457}
4458
4459/* functions for drivers to get certain frames */
4460
Marco Porscheac70c12013-01-07 16:04:50 +01004461static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004462 struct ps_data *ps, struct sk_buff *skb,
4463 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02004464{
4465 u8 *pos, *tim;
4466 int aid0 = 0;
4467 int i, have_bits = 0, n1, n2;
4468
4469 /* Generate bitmap for TIM only if there are any STAs in power save
4470 * mode. */
Marco Porschd012a602012-10-10 12:39:50 -07004471 if (atomic_read(&ps->num_sta_ps) > 0)
Johannes Berge2ebc742007-07-27 15:43:22 +02004472 /* in the hope that this is faster than
4473 * checking byte-for-byte */
Weilong Chenf359d3f2013-12-18 15:44:16 +08004474 have_bits = !bitmap_empty((unsigned long *)ps->tim,
Johannes Berge2ebc742007-07-27 15:43:22 +02004475 IEEE80211_MAX_AID+1);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004476 if (!is_template) {
4477 if (ps->dtim_count == 0)
4478 ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
4479 else
4480 ps->dtim_count--;
4481 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004482
Johannes Berg4df864c2017-06-16 14:29:21 +02004483 tim = pos = skb_put(skb, 6);
Johannes Berge2ebc742007-07-27 15:43:22 +02004484 *pos++ = WLAN_EID_TIM;
4485 *pos++ = 4;
Marco Porschd012a602012-10-10 12:39:50 -07004486 *pos++ = ps->dtim_count;
Johannes Berg88600202012-02-13 15:17:18 +01004487 *pos++ = sdata->vif.bss_conf.dtim_period;
Johannes Berge2ebc742007-07-27 15:43:22 +02004488
Marco Porschd012a602012-10-10 12:39:50 -07004489 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
Johannes Berge2ebc742007-07-27 15:43:22 +02004490 aid0 = 1;
4491
Marco Porschd012a602012-10-10 12:39:50 -07004492 ps->dtim_bc_mc = aid0 == 1;
Christian Lamparter512119b2011-01-31 20:48:44 +02004493
Johannes Berge2ebc742007-07-27 15:43:22 +02004494 if (have_bits) {
4495 /* Find largest even number N1 so that bits numbered 1 through
4496 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
4497 * (N2 + 1) x 8 through 2007 are 0. */
4498 n1 = 0;
4499 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
Marco Porschd012a602012-10-10 12:39:50 -07004500 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004501 n1 = i & 0xfe;
4502 break;
4503 }
4504 }
4505 n2 = n1;
4506 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
Marco Porschd012a602012-10-10 12:39:50 -07004507 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004508 n2 = i;
4509 break;
4510 }
4511 }
4512
4513 /* Bitmap control */
4514 *pos++ = n1 | aid0;
4515 /* Part Virt Bitmap */
Eliad Peller5220da32011-11-24 16:50:00 +02004516 skb_put(skb, n2 - n1);
Marco Porschd012a602012-10-10 12:39:50 -07004517 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
Johannes Berge2ebc742007-07-27 15:43:22 +02004518
4519 tim[1] = n2 - n1 + 4;
Johannes Berge2ebc742007-07-27 15:43:22 +02004520 } else {
4521 *pos++ = aid0; /* Bitmap control */
4522 *pos++ = 0; /* Part Virt Bitmap */
4523 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004524}
4525
Marco Porscheac70c12013-01-07 16:04:50 +01004526static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004527 struct ps_data *ps, struct sk_buff *skb,
4528 bool is_template)
Marco Porscheac70c12013-01-07 16:04:50 +01004529{
4530 struct ieee80211_local *local = sdata->local;
4531
4532 /*
4533 * Not very nice, but we want to allow the driver to call
4534 * ieee80211_beacon_get() as a response to the set_tim()
4535 * callback. That, however, is already invoked under the
4536 * sta_lock to guarantee consistent and race-free update
4537 * of the tim bitmap in mac80211 and the driver.
4538 */
4539 if (local->tim_in_locked_section) {
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004540 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Marco Porscheac70c12013-01-07 16:04:50 +01004541 } else {
Johannes Berg1b917312013-02-22 12:55:01 +01004542 spin_lock_bh(&local->tim_lock);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004543 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Johannes Berg1b917312013-02-22 12:55:01 +01004544 spin_unlock_bh(&local->tim_lock);
Marco Porscheac70c12013-01-07 16:04:50 +01004545 }
4546
4547 return 0;
4548}
4549
John Crispin8552a432020-08-11 10:01:04 +02004550static void ieee80211_set_beacon_cntdwn(struct ieee80211_sub_if_data *sdata,
4551 struct beacon_data *beacon)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004552{
4553 struct probe_resp *resp;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004554 u8 *beacon_data;
4555 size_t beacon_data_len;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004556 int i;
John Crispin8552a432020-08-11 10:01:04 +02004557 u8 count = beacon->cntdwn_current_counter;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004558
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004559 switch (sdata->vif.type) {
4560 case NL80211_IFTYPE_AP:
4561 beacon_data = beacon->tail;
4562 beacon_data_len = beacon->tail_len;
4563 break;
4564 case NL80211_IFTYPE_ADHOC:
4565 beacon_data = beacon->head;
4566 beacon_data_len = beacon->head_len;
4567 break;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004568 case NL80211_IFTYPE_MESH_POINT:
4569 beacon_data = beacon->head;
4570 beacon_data_len = beacon->head_len;
4571 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004572 default:
4573 return;
4574 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004575
Michal Kazioraf296bd2014-06-05 14:21:36 +02004576 rcu_read_lock();
John Crispin8552a432020-08-11 10:01:04 +02004577 for (i = 0; i < IEEE80211_MAX_CNTDWN_COUNTERS_NUM; ++i) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004578 resp = rcu_dereference(sdata->u.ap.probe_resp);
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004579
John Crispin8552a432020-08-11 10:01:04 +02004580 if (beacon->cntdwn_counter_offsets[i]) {
4581 if (WARN_ON_ONCE(beacon->cntdwn_counter_offsets[i] >=
Michal Kazioraf296bd2014-06-05 14:21:36 +02004582 beacon_data_len)) {
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004583 rcu_read_unlock();
4584 return;
4585 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004586
John Crispin8552a432020-08-11 10:01:04 +02004587 beacon_data[beacon->cntdwn_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004588 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004589
4590 if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
John Crispin8552a432020-08-11 10:01:04 +02004591 resp->data[resp->cntdwn_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004592 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004593 rcu_read_unlock();
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004594}
4595
John Crispin8552a432020-08-11 10:01:04 +02004596static u8 __ieee80211_beacon_update_cntdwn(struct beacon_data *beacon)
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004597{
John Crispin8552a432020-08-11 10:01:04 +02004598 beacon->cntdwn_current_counter--;
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004599
4600 /* the counter should never reach 0 */
John Crispin8552a432020-08-11 10:01:04 +02004601 WARN_ON_ONCE(!beacon->cntdwn_current_counter);
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004602
John Crispin8552a432020-08-11 10:01:04 +02004603 return beacon->cntdwn_current_counter;
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004604}
4605
John Crispin8552a432020-08-11 10:01:04 +02004606u8 ieee80211_beacon_update_cntdwn(struct ieee80211_vif *vif)
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004607{
4608 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004609 struct beacon_data *beacon = NULL;
4610 u8 count = 0;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004611
Michal Kazioraf296bd2014-06-05 14:21:36 +02004612 rcu_read_lock();
4613
4614 if (sdata->vif.type == NL80211_IFTYPE_AP)
4615 beacon = rcu_dereference(sdata->u.ap.beacon);
4616 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4617 beacon = rcu_dereference(sdata->u.ibss.presp);
4618 else if (ieee80211_vif_is_mesh(&sdata->vif))
4619 beacon = rcu_dereference(sdata->u.mesh.beacon);
4620
4621 if (!beacon)
4622 goto unlock;
4623
John Crispin8552a432020-08-11 10:01:04 +02004624 count = __ieee80211_beacon_update_cntdwn(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004625
Michal Kazioraf296bd2014-06-05 14:21:36 +02004626unlock:
4627 rcu_read_unlock();
4628 return count;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004629}
John Crispin8552a432020-08-11 10:01:04 +02004630EXPORT_SYMBOL(ieee80211_beacon_update_cntdwn);
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004631
John Crispin8552a432020-08-11 10:01:04 +02004632void ieee80211_beacon_set_cntdwn(struct ieee80211_vif *vif, u8 counter)
Gregory Greenman03737002018-04-20 13:49:24 +03004633{
4634 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4635 struct beacon_data *beacon = NULL;
4636
4637 rcu_read_lock();
4638
4639 if (sdata->vif.type == NL80211_IFTYPE_AP)
4640 beacon = rcu_dereference(sdata->u.ap.beacon);
4641 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4642 beacon = rcu_dereference(sdata->u.ibss.presp);
4643 else if (ieee80211_vif_is_mesh(&sdata->vif))
4644 beacon = rcu_dereference(sdata->u.mesh.beacon);
4645
4646 if (!beacon)
4647 goto unlock;
4648
John Crispin8552a432020-08-11 10:01:04 +02004649 if (counter < beacon->cntdwn_current_counter)
4650 beacon->cntdwn_current_counter = counter;
Gregory Greenman03737002018-04-20 13:49:24 +03004651
4652unlock:
4653 rcu_read_unlock();
4654}
John Crispin8552a432020-08-11 10:01:04 +02004655EXPORT_SYMBOL(ieee80211_beacon_set_cntdwn);
Gregory Greenman03737002018-04-20 13:49:24 +03004656
John Crispin8552a432020-08-11 10:01:04 +02004657bool ieee80211_beacon_cntdwn_is_complete(struct ieee80211_vif *vif)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004658{
4659 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4660 struct beacon_data *beacon = NULL;
4661 u8 *beacon_data;
4662 size_t beacon_data_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004663 int ret = false;
4664
4665 if (!ieee80211_sdata_running(sdata))
4666 return false;
4667
4668 rcu_read_lock();
4669 if (vif->type == NL80211_IFTYPE_AP) {
4670 struct ieee80211_if_ap *ap = &sdata->u.ap;
4671
4672 beacon = rcu_dereference(ap->beacon);
4673 if (WARN_ON(!beacon || !beacon->tail))
4674 goto out;
4675 beacon_data = beacon->tail;
4676 beacon_data_len = beacon->tail_len;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004677 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
4678 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4679
4680 beacon = rcu_dereference(ifibss->presp);
4681 if (!beacon)
4682 goto out;
4683
4684 beacon_data = beacon->head;
4685 beacon_data_len = beacon->head_len;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004686 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
4687 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4688
4689 beacon = rcu_dereference(ifmsh->beacon);
4690 if (!beacon)
4691 goto out;
4692
4693 beacon_data = beacon->head;
4694 beacon_data_len = beacon->head_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004695 } else {
4696 WARN_ON(1);
4697 goto out;
4698 }
4699
John Crispin8552a432020-08-11 10:01:04 +02004700 if (!beacon->cntdwn_counter_offsets[0])
Michal Kazior10d78f22014-06-05 14:21:37 +02004701 goto out;
4702
John Crispin8552a432020-08-11 10:01:04 +02004703 if (WARN_ON_ONCE(beacon->cntdwn_counter_offsets[0] > beacon_data_len))
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004704 goto out;
4705
John Crispin8552a432020-08-11 10:01:04 +02004706 if (beacon_data[beacon->cntdwn_counter_offsets[0]] == 1)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004707 ret = true;
John Crispin8552a432020-08-11 10:01:04 +02004708
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004709 out:
4710 rcu_read_unlock();
4711
4712 return ret;
4713}
John Crispin8552a432020-08-11 10:01:04 +02004714EXPORT_SYMBOL(ieee80211_beacon_cntdwn_is_complete);
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004715
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004716static int ieee80211_beacon_protect(struct sk_buff *skb,
4717 struct ieee80211_local *local,
4718 struct ieee80211_sub_if_data *sdata)
4719{
4720 ieee80211_tx_result res;
4721 struct ieee80211_tx_data tx;
Johannes Berg95247702020-03-20 10:20:23 +01004722 struct sk_buff *check_skb;
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004723
4724 memset(&tx, 0, sizeof(tx));
4725 tx.key = rcu_dereference(sdata->default_beacon_key);
4726 if (!tx.key)
4727 return 0;
4728 tx.local = local;
4729 tx.sdata = sdata;
4730 __skb_queue_head_init(&tx.skbs);
4731 __skb_queue_tail(&tx.skbs, skb);
4732 res = ieee80211_tx_h_encrypt(&tx);
Johannes Berg95247702020-03-20 10:20:23 +01004733 check_skb = __skb_dequeue(&tx.skbs);
4734 /* we may crash after this, but it'd be a bug in crypto */
4735 WARN_ON(check_skb != skb);
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004736 if (WARN_ON_ONCE(res != TX_CONTINUE))
Johannes Berg95247702020-03-20 10:20:23 +01004737 return -EINVAL;
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004738
4739 return 0;
4740}
4741
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004742static struct sk_buff *
4743__ieee80211_beacon_get(struct ieee80211_hw *hw,
4744 struct ieee80211_vif *vif,
4745 struct ieee80211_mutable_offsets *offs,
4746 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02004747{
4748 struct ieee80211_local *local = hw_to_local(hw);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004749 struct beacon_data *beacon = NULL;
Johannes Berg9d139c82008-07-09 14:40:37 +02004750 struct sk_buff *skb = NULL;
Johannes Berge039fa42008-05-15 12:55:29 +02004751 struct ieee80211_tx_info *info;
Johannes Berge2ebc742007-07-27 15:43:22 +02004752 struct ieee80211_sub_if_data *sdata = NULL;
Johannes Berg57fbcce2016-04-12 15:56:15 +02004753 enum nl80211_band band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004754 struct ieee80211_tx_rate_control txrc;
Johannes Berg55de9082012-07-26 17:24:39 +02004755 struct ieee80211_chanctx_conf *chanctx_conf;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004756 int csa_off_base = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004757
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004758 rcu_read_lock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004759
Johannes Berg32bfd352007-12-19 01:31:26 +01004760 sdata = vif_to_sdata(vif);
Johannes Berg55de9082012-07-26 17:24:39 +02004761 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berge2ebc742007-07-27 15:43:22 +02004762
Johannes Berg55de9082012-07-26 17:24:39 +02004763 if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
Felix Fietkaueb3e5542011-01-24 19:28:49 +01004764 goto out;
4765
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004766 if (offs)
4767 memset(offs, 0, sizeof(*offs));
Johannes Bergeddcbb942009-10-29 08:30:35 +01004768
Johannes Berg05c914f2008-09-11 00:01:58 +02004769 if (sdata->vif.type == NL80211_IFTYPE_AP) {
Marco Porschd012a602012-10-10 12:39:50 -07004770 struct ieee80211_if_ap *ap = &sdata->u.ap;
Marco Porschd012a602012-10-10 12:39:50 -07004771
Michal Kazioraf296bd2014-06-05 14:21:36 +02004772 beacon = rcu_dereference(ap->beacon);
Dan Carpenter3ad97fb2011-02-07 22:03:35 +03004773 if (beacon) {
John Crispin8552a432020-08-11 10:01:04 +02004774 if (beacon->cntdwn_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004775 if (!is_template)
John Crispin8552a432020-08-11 10:01:04 +02004776 ieee80211_beacon_update_cntdwn(vif);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004777
John Crispin8552a432020-08-11 10:01:04 +02004778 ieee80211_set_beacon_cntdwn(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004779 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004780
Johannes Berg902acc72008-02-23 15:17:19 +01004781 /*
4782 * headroom, head length,
4783 * tail length and maximum TIM length
4784 */
4785 skb = dev_alloc_skb(local->tx_headroom +
4786 beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004787 beacon->tail_len + 256 +
4788 local->hw.extra_beacon_tailroom);
Johannes Berg902acc72008-02-23 15:17:19 +01004789 if (!skb)
4790 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004791
Johannes Berg902acc72008-02-23 15:17:19 +01004792 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004793 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg902acc72008-02-23 15:17:19 +01004794
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004795 ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
4796 is_template);
Johannes Berg902acc72008-02-23 15:17:19 +01004797
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004798 if (offs) {
4799 offs->tim_offset = beacon->head_len;
4800 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004801
4802 /* for AP the csa offsets are from tail */
4803 csa_off_base = skb->len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004804 }
Johannes Bergeddcbb942009-10-29 08:30:35 +01004805
Johannes Berg902acc72008-02-23 15:17:19 +01004806 if (beacon->tail)
Johannes Berg59ae1d12017-06-16 14:29:20 +02004807 skb_put_data(skb, beacon->tail,
4808 beacon->tail_len);
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004809
4810 if (ieee80211_beacon_protect(skb, local, sdata) < 0)
4811 goto out;
Johannes Berg9d139c82008-07-09 14:40:37 +02004812 } else
4813 goto out;
Johannes Berg05c914f2008-09-11 00:01:58 +02004814 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Berg46900292009-02-15 12:44:28 +01004815 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg9d139c82008-07-09 14:40:37 +02004816 struct ieee80211_hdr *hdr;
Johannes Berg902acc72008-02-23 15:17:19 +01004817
Michal Kazioraf296bd2014-06-05 14:21:36 +02004818 beacon = rcu_dereference(ifibss->presp);
4819 if (!beacon)
Johannes Berg9d139c82008-07-09 14:40:37 +02004820 goto out;
4821
John Crispin8552a432020-08-11 10:01:04 +02004822 if (beacon->cntdwn_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004823 if (!is_template)
John Crispin8552a432020-08-11 10:01:04 +02004824 __ieee80211_beacon_update_cntdwn(beacon);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004825
John Crispin8552a432020-08-11 10:01:04 +02004826 ieee80211_set_beacon_cntdwn(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004827 }
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004828
Michal Kazioraf296bd2014-06-05 14:21:36 +02004829 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004830 local->hw.extra_beacon_tailroom);
Johannes Berg9d139c82008-07-09 14:40:37 +02004831 if (!skb)
4832 goto out;
Johannes Bergc3ffeab2013-03-07 20:54:29 +01004833 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004834 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004835
4836 hdr = (struct ieee80211_hdr *) skb->data;
Harvey Harrisone7827a72008-07-15 18:44:13 -07004837 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4838 IEEE80211_STYPE_BEACON);
Johannes Berg902acc72008-02-23 15:17:19 +01004839 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
Javier Cardonadbf498f2012-03-31 11:31:32 -07004840 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg472dbc42008-09-11 00:01:49 +02004841
Michal Kazioraf296bd2014-06-05 14:21:36 +02004842 beacon = rcu_dereference(ifmsh->beacon);
4843 if (!beacon)
Johannes Bergac1bd842011-01-18 13:45:32 +01004844 goto out;
Johannes Bergac1bd842011-01-18 13:45:32 +01004845
John Crispin8552a432020-08-11 10:01:04 +02004846 if (beacon->cntdwn_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004847 if (!is_template)
4848 /* TODO: For mesh csa_counter is in TU, so
4849 * decrementing it by one isn't correct, but
4850 * for now we leave it consistent with overall
4851 * mac80211's behavior.
4852 */
John Crispin8552a432020-08-11 10:01:04 +02004853 __ieee80211_beacon_update_cntdwn(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004854
John Crispin8552a432020-08-11 10:01:04 +02004855 ieee80211_set_beacon_cntdwn(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004856 }
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004857
Javier Cardonadbf498f2012-03-31 11:31:32 -07004858 if (ifmsh->sync_ops)
Masashi Honma445cd452016-12-08 10:15:51 +09004859 ifmsh->sync_ops->adjust_tsf(sdata, beacon);
Javier Cardonadbf498f2012-03-31 11:31:32 -07004860
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -07004861 skb = dev_alloc_skb(local->tx_headroom +
Michal Kazioraf296bd2014-06-05 14:21:36 +02004862 beacon->head_len +
Marco Porsch3f52b7e2013-01-30 18:14:08 +01004863 256 + /* TIM IE */
Michal Kazioraf296bd2014-06-05 14:21:36 +02004864 beacon->tail_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004865 local->hw.extra_beacon_tailroom);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01004866 if (!skb)
4867 goto out;
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08004868 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004869 skb_put_data(skb, beacon->head, beacon->head_len);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004870 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
4871
4872 if (offs) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004873 offs->tim_offset = beacon->head_len;
4874 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004875 }
4876
Johannes Berg59ae1d12017-06-16 14:29:20 +02004877 skb_put_data(skb, beacon->tail, beacon->tail_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004878 } else {
4879 WARN_ON(1);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004880 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02004881 }
4882
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004883 /* CSA offsets */
Michal Kazioraf296bd2014-06-05 14:21:36 +02004884 if (offs && beacon) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004885 int i;
4886
John Crispin8552a432020-08-11 10:01:04 +02004887 for (i = 0; i < IEEE80211_MAX_CNTDWN_COUNTERS_NUM; i++) {
4888 u16 csa_off = beacon->cntdwn_counter_offsets[i];
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004889
4890 if (!csa_off)
4891 continue;
4892
John Crispin8552a432020-08-11 10:01:04 +02004893 offs->cntdwn_counter_offs[i] = csa_off_base + csa_off;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004894 }
4895 }
4896
Johannes Berg4bf88532012-11-09 11:39:59 +01004897 band = chanctx_conf->def.chan->band;
Johannes Berg55de9082012-07-26 17:24:39 +02004898
Johannes Berge039fa42008-05-15 12:55:29 +02004899 info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02004900
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004901 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004902 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Johannes Berge039fa42008-05-15 12:55:29 +02004903 info->band = band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004904
4905 memset(&txrc, 0, sizeof(txrc));
4906 txrc.hw = hw;
Johannes Berge31583c2012-07-26 14:07:46 +02004907 txrc.sband = local->hw.wiphy->bands[band];
Jouni Malinene00cfce2009-12-29 12:59:19 +02004908 txrc.bss_conf = &sdata->vif.bss_conf;
4909 txrc.skb = skb;
4910 txrc.reported_rate.idx = -1;
Jouni Malinen08fad432020-04-25 18:57:12 +03004911 if (sdata->beacon_rate_set && sdata->beacon_rateidx_mask[band])
4912 txrc.rate_idx_mask = sdata->beacon_rateidx_mask[band];
4913 else
4914 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
Felix Fietkau8f0729b2010-11-11 15:07:23 +01004915 txrc.bss = true;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004916 rate_control_get_rate(sdata, NULL, &txrc);
Johannes Berge039fa42008-05-15 12:55:29 +02004917
4918 info->control.vif = vif;
Johannes Bergf591fa52008-07-10 11:21:26 +02004919
John W. Linvillea472e712010-05-06 14:45:17 -04004920 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
4921 IEEE80211_TX_CTL_ASSIGN_SEQ |
4922 IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge6a98542008-10-21 12:40:02 +02004923 out:
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004924 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004925 return skb;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004926
4927}
4928
4929struct sk_buff *
4930ieee80211_beacon_get_template(struct ieee80211_hw *hw,
4931 struct ieee80211_vif *vif,
4932 struct ieee80211_mutable_offsets *offs)
4933{
4934 return __ieee80211_beacon_get(hw, vif, offs, true);
4935}
4936EXPORT_SYMBOL(ieee80211_beacon_get_template);
4937
4938struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
4939 struct ieee80211_vif *vif,
4940 u16 *tim_offset, u16 *tim_length)
4941{
4942 struct ieee80211_mutable_offsets offs = {};
4943 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
Helmut Schaa35afa582015-09-09 09:46:32 +02004944 struct sk_buff *copy;
4945 struct ieee80211_supported_band *sband;
4946 int shift;
4947
4948 if (!bcn)
4949 return bcn;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004950
4951 if (tim_offset)
4952 *tim_offset = offs.tim_offset;
4953
4954 if (tim_length)
4955 *tim_length = offs.tim_length;
4956
Helmut Schaa35afa582015-09-09 09:46:32 +02004957 if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
4958 !hw_to_local(hw)->monitors)
4959 return bcn;
4960
4961 /* send a copy to monitor interfaces */
4962 copy = skb_copy(bcn, GFP_ATOMIC);
4963 if (!copy)
4964 return bcn;
4965
4966 shift = ieee80211_vif_get_shift(vif);
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05304967 sband = ieee80211_get_sband(vif_to_sdata(vif));
4968 if (!sband)
4969 return bcn;
4970
John Crispinb7b2e8c2019-07-14 17:44:15 +02004971 ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false,
4972 NULL);
Helmut Schaa35afa582015-09-09 09:46:32 +02004973
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004974 return bcn;
Johannes Berge2ebc742007-07-27 15:43:22 +02004975}
Johannes Bergeddcbb942009-10-29 08:30:35 +01004976EXPORT_SYMBOL(ieee80211_beacon_get_tim);
Johannes Berge2ebc742007-07-27 15:43:22 +02004977
Arik Nemtsov02945822011-11-10 11:28:57 +02004978struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
4979 struct ieee80211_vif *vif)
4980{
4981 struct ieee80211_if_ap *ap = NULL;
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004982 struct sk_buff *skb = NULL;
4983 struct probe_resp *presp = NULL;
Arik Nemtsov02945822011-11-10 11:28:57 +02004984 struct ieee80211_hdr *hdr;
4985 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4986
4987 if (sdata->vif.type != NL80211_IFTYPE_AP)
4988 return NULL;
4989
4990 rcu_read_lock();
4991
4992 ap = &sdata->u.ap;
4993 presp = rcu_dereference(ap->probe_resp);
4994 if (!presp)
4995 goto out;
4996
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004997 skb = dev_alloc_skb(presp->len);
Arik Nemtsov02945822011-11-10 11:28:57 +02004998 if (!skb)
4999 goto out;
5000
Johannes Berg59ae1d12017-06-16 14:29:20 +02005001 skb_put_data(skb, presp->data, presp->len);
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03005002
Arik Nemtsov02945822011-11-10 11:28:57 +02005003 hdr = (struct ieee80211_hdr *) skb->data;
5004 memset(hdr->addr1, 0, sizeof(hdr->addr1));
5005
5006out:
5007 rcu_read_unlock();
5008 return skb;
5009}
5010EXPORT_SYMBOL(ieee80211_proberesp_get);
5011
Aloka Dixit295b02c2020-09-11 00:05:31 +00005012struct sk_buff *ieee80211_get_fils_discovery_tmpl(struct ieee80211_hw *hw,
5013 struct ieee80211_vif *vif)
5014{
5015 struct sk_buff *skb = NULL;
5016 struct fils_discovery_data *tmpl = NULL;
5017 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5018
5019 if (sdata->vif.type != NL80211_IFTYPE_AP)
5020 return NULL;
5021
5022 rcu_read_lock();
5023 tmpl = rcu_dereference(sdata->u.ap.fils_discovery);
5024 if (!tmpl) {
5025 rcu_read_unlock();
5026 return NULL;
5027 }
5028
5029 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
5030 if (skb) {
5031 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
5032 skb_put_data(skb, tmpl->data, tmpl->len);
5033 }
5034
5035 rcu_read_unlock();
5036 return skb;
5037}
5038EXPORT_SYMBOL(ieee80211_get_fils_discovery_tmpl);
5039
Aloka Dixit632189a2020-09-11 00:33:01 +00005040struct sk_buff *
5041ieee80211_get_unsol_bcast_probe_resp_tmpl(struct ieee80211_hw *hw,
5042 struct ieee80211_vif *vif)
5043{
5044 struct sk_buff *skb = NULL;
5045 struct unsol_bcast_probe_resp_data *tmpl = NULL;
5046 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5047
5048 if (sdata->vif.type != NL80211_IFTYPE_AP)
5049 return NULL;
5050
5051 rcu_read_lock();
5052 tmpl = rcu_dereference(sdata->u.ap.unsol_bcast_probe_resp);
5053 if (!tmpl) {
5054 rcu_read_unlock();
5055 return NULL;
5056 }
5057
5058 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
5059 if (skb) {
5060 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
5061 skb_put_data(skb, tmpl->data, tmpl->len);
5062 }
5063
5064 rcu_read_unlock();
5065 return skb;
5066}
5067EXPORT_SYMBOL(ieee80211_get_unsol_bcast_probe_resp_tmpl);
5068
Kalle Valo7044cc52010-01-05 20:16:19 +02005069struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
5070 struct ieee80211_vif *vif)
5071{
5072 struct ieee80211_sub_if_data *sdata;
5073 struct ieee80211_if_managed *ifmgd;
5074 struct ieee80211_pspoll *pspoll;
5075 struct ieee80211_local *local;
5076 struct sk_buff *skb;
5077
5078 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5079 return NULL;
5080
5081 sdata = vif_to_sdata(vif);
5082 ifmgd = &sdata->u.mgd;
5083 local = sdata->local;
5084
5085 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
Joe Perchesd15b8452011-08-29 14:17:31 -07005086 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02005087 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07005088
Kalle Valo7044cc52010-01-05 20:16:19 +02005089 skb_reserve(skb, local->hw.extra_tx_headroom);
5090
Johannes Bergb080db52017-06-16 14:29:19 +02005091 pspoll = skb_put_zero(skb, sizeof(*pspoll));
Kalle Valo7044cc52010-01-05 20:16:19 +02005092 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
5093 IEEE80211_STYPE_PSPOLL);
Johannes Berg1db364c2020-04-17 12:38:04 +02005094 pspoll->aid = cpu_to_le16(sdata->vif.bss_conf.aid);
Kalle Valo7044cc52010-01-05 20:16:19 +02005095
5096 /* aid in PS-Poll has its two MSBs each set to 1 */
5097 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
5098
5099 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
5100 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
5101
5102 return skb;
5103}
5104EXPORT_SYMBOL(ieee80211_pspoll_get);
5105
5106struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005107 struct ieee80211_vif *vif,
5108 bool qos_ok)
Kalle Valo7044cc52010-01-05 20:16:19 +02005109{
5110 struct ieee80211_hdr_3addr *nullfunc;
5111 struct ieee80211_sub_if_data *sdata;
5112 struct ieee80211_if_managed *ifmgd;
5113 struct ieee80211_local *local;
5114 struct sk_buff *skb;
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005115 bool qos = false;
Kalle Valo7044cc52010-01-05 20:16:19 +02005116
5117 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5118 return NULL;
5119
5120 sdata = vif_to_sdata(vif);
5121 ifmgd = &sdata->u.mgd;
5122 local = sdata->local;
5123
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005124 if (qos_ok) {
5125 struct sta_info *sta;
5126
5127 rcu_read_lock();
5128 sta = sta_info_get(sdata, ifmgd->bssid);
5129 qos = sta && sta->sta.wme;
5130 rcu_read_unlock();
5131 }
5132
5133 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5134 sizeof(*nullfunc) + 2);
Joe Perchesd15b8452011-08-29 14:17:31 -07005135 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02005136 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07005137
Kalle Valo7044cc52010-01-05 20:16:19 +02005138 skb_reserve(skb, local->hw.extra_tx_headroom);
5139
Johannes Bergb080db52017-06-16 14:29:19 +02005140 nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
Kalle Valo7044cc52010-01-05 20:16:19 +02005141 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
5142 IEEE80211_STYPE_NULLFUNC |
5143 IEEE80211_FCTL_TODS);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005144 if (qos) {
Johannes Berge0ba7092018-11-09 11:16:46 +01005145 __le16 qoshdr = cpu_to_le16(7);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005146
5147 BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
5148 IEEE80211_STYPE_NULLFUNC) !=
5149 IEEE80211_STYPE_QOS_NULLFUNC);
5150 nullfunc->frame_control |=
5151 cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
5152 skb->priority = 7;
5153 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
Johannes Berge0ba7092018-11-09 11:16:46 +01005154 skb_put_data(skb, &qoshdr, sizeof(qoshdr));
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005155 }
5156
Kalle Valo7044cc52010-01-05 20:16:19 +02005157 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
5158 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
5159 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
5160
5161 return skb;
5162}
5163EXPORT_SYMBOL(ieee80211_nullfunc_get);
5164
Kalle Valo05e54ea2010-01-05 20:16:38 +02005165struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
Johannes Berga344d672014-06-12 22:24:31 +02005166 const u8 *src_addr,
Kalle Valo05e54ea2010-01-05 20:16:38 +02005167 const u8 *ssid, size_t ssid_len,
Johannes Bergb9a9ada2012-11-29 13:00:10 +01005168 size_t tailroom)
Kalle Valo05e54ea2010-01-05 20:16:38 +02005169{
Johannes Berga344d672014-06-12 22:24:31 +02005170 struct ieee80211_local *local = hw_to_local(hw);
Kalle Valo05e54ea2010-01-05 20:16:38 +02005171 struct ieee80211_hdr_3addr *hdr;
5172 struct sk_buff *skb;
5173 size_t ie_ssid_len;
5174 u8 *pos;
5175
Kalle Valo05e54ea2010-01-05 20:16:38 +02005176 ie_ssid_len = 2 + ssid_len;
5177
5178 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
Johannes Bergb9a9ada2012-11-29 13:00:10 +01005179 ie_ssid_len + tailroom);
Joe Perchesd15b8452011-08-29 14:17:31 -07005180 if (!skb)
Kalle Valo05e54ea2010-01-05 20:16:38 +02005181 return NULL;
Kalle Valo05e54ea2010-01-05 20:16:38 +02005182
5183 skb_reserve(skb, local->hw.extra_tx_headroom);
5184
Johannes Bergb080db52017-06-16 14:29:19 +02005185 hdr = skb_put_zero(skb, sizeof(*hdr));
Kalle Valo05e54ea2010-01-05 20:16:38 +02005186 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
5187 IEEE80211_STYPE_PROBE_REQ);
Johannes Berge83e6542012-07-13 16:23:07 +02005188 eth_broadcast_addr(hdr->addr1);
Johannes Berga344d672014-06-12 22:24:31 +02005189 memcpy(hdr->addr2, src_addr, ETH_ALEN);
Johannes Berge83e6542012-07-13 16:23:07 +02005190 eth_broadcast_addr(hdr->addr3);
Kalle Valo05e54ea2010-01-05 20:16:38 +02005191
5192 pos = skb_put(skb, ie_ssid_len);
5193 *pos++ = WLAN_EID_SSID;
5194 *pos++ = ssid_len;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02005195 if (ssid_len)
Kalle Valo05e54ea2010-01-05 20:16:38 +02005196 memcpy(pos, ssid, ssid_len);
5197 pos += ssid_len;
5198
Kalle Valo05e54ea2010-01-05 20:16:38 +02005199 return skb;
5200}
5201EXPORT_SYMBOL(ieee80211_probereq_get);
5202
Johannes Berg32bfd352007-12-19 01:31:26 +01005203void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02005204 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02005205 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02005206 struct ieee80211_rts *rts)
5207{
5208 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02005209
Harvey Harrison065e96052008-06-22 16:45:27 -07005210 rts->frame_control =
5211 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01005212 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
5213 frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02005214 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
5215 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
5216}
5217EXPORT_SYMBOL(ieee80211_rts_get);
5218
Johannes Berg32bfd352007-12-19 01:31:26 +01005219void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02005220 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02005221 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02005222 struct ieee80211_cts *cts)
5223{
5224 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02005225
Harvey Harrison065e96052008-06-22 16:45:27 -07005226 cts->frame_control =
5227 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01005228 cts->duration = ieee80211_ctstoself_duration(hw, vif,
5229 frame_len, frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02005230 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
5231}
5232EXPORT_SYMBOL(ieee80211_ctstoself_get);
5233
5234struct sk_buff *
Johannes Berg32bfd352007-12-19 01:31:26 +01005235ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
Johannes Berge039fa42008-05-15 12:55:29 +02005236 struct ieee80211_vif *vif)
Johannes Berge2ebc742007-07-27 15:43:22 +02005237{
5238 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winkler747cf5e2008-05-27 17:50:51 +03005239 struct sk_buff *skb = NULL;
Johannes Berg5cf121c2008-02-25 16:27:43 +01005240 struct ieee80211_tx_data tx;
Johannes Berge2ebc742007-07-27 15:43:22 +02005241 struct ieee80211_sub_if_data *sdata;
Marco Porschd012a602012-10-10 12:39:50 -07005242 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +02005243 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02005244 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berge2ebc742007-07-27 15:43:22 +02005245
Johannes Berg32bfd352007-12-19 01:31:26 +01005246 sdata = vif_to_sdata(vif);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005247
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005248 rcu_read_lock();
Johannes Berg55de9082012-07-26 17:24:39 +02005249 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005250
Marco Porschd012a602012-10-10 12:39:50 -07005251 if (!chanctx_conf)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03005252 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005253
Marco Porschd012a602012-10-10 12:39:50 -07005254 if (sdata->vif.type == NL80211_IFTYPE_AP) {
5255 struct beacon_data *beacon =
5256 rcu_dereference(sdata->u.ap.beacon);
5257
5258 if (!beacon || !beacon->head)
5259 goto out;
5260
5261 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01005262 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
5263 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07005264 } else {
5265 goto out;
5266 }
5267
5268 if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03005269 goto out; /* send buffered bc/mc only after DTIM beacon */
Johannes Berge039fa42008-05-15 12:55:29 +02005270
Johannes Berge2ebc742007-07-27 15:43:22 +02005271 while (1) {
Marco Porschd012a602012-10-10 12:39:50 -07005272 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +02005273 if (!skb)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03005274 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02005275 local->total_ps_buffered--;
5276
Marco Porschd012a602012-10-10 12:39:50 -07005277 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
Johannes Berge2ebc742007-07-27 15:43:22 +02005278 struct ieee80211_hdr *hdr =
5279 (struct ieee80211_hdr *) skb->data;
5280 /* more buffered multicast/broadcast frames ==> set
5281 * MoreData flag in IEEE 802.11 header to inform PS
5282 * STAs */
5283 hdr->frame_control |=
5284 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
5285 }
5286
Michael Braun112c44b2014-03-06 15:08:43 +01005287 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porsch7cbf9d02013-03-01 16:01:18 +01005288 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
Johannes Berg7c107702015-03-20 14:18:27 +01005289 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
Johannes Berge2ebc742007-07-27 15:43:22 +02005290 break;
Felix Fietkau6b07d9c2016-08-02 11:13:41 +02005291 ieee80211_free_txskb(hw, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02005292 }
Johannes Berge039fa42008-05-15 12:55:29 +02005293
5294 info = IEEE80211_SKB_CB(skb);
5295
Johannes Berg5cf121c2008-02-25 16:27:43 +01005296 tx.flags |= IEEE80211_TX_PS_BUFFERED;
Johannes Berg4bf88532012-11-09 11:39:59 +01005297 info->band = chanctx_conf->def.chan->band;
Johannes Berge2ebc742007-07-27 15:43:22 +02005298
Johannes Berg97b045d2008-06-20 01:22:30 +02005299 if (invoke_tx_handlers(&tx))
Johannes Berge2ebc742007-07-27 15:43:22 +02005300 skb = NULL;
Johannes Berg97b045d2008-06-20 01:22:30 +02005301 out:
Johannes Bergd0709a62008-02-25 16:27:46 +01005302 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02005303
5304 return skb;
5305}
5306EXPORT_SYMBOL(ieee80211_get_buffered_bc);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005307
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005308int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5309{
5310 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5311 struct ieee80211_sub_if_data *sdata = sta->sdata;
5312 struct ieee80211_local *local = sdata->local;
5313 int ret;
5314 u32 queues;
5315
5316 lockdep_assert_held(&local->sta_mtx);
5317
5318 /* only some cases are supported right now */
5319 switch (sdata->vif.type) {
5320 case NL80211_IFTYPE_STATION:
5321 case NL80211_IFTYPE_AP:
5322 case NL80211_IFTYPE_AP_VLAN:
5323 break;
5324 default:
5325 WARN_ON(1);
5326 return -EINVAL;
5327 }
5328
5329 if (WARN_ON(tid >= IEEE80211_NUM_UPS))
5330 return -EINVAL;
5331
5332 if (sta->reserved_tid == tid) {
5333 ret = 0;
5334 goto out;
5335 }
5336
5337 if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
5338 sdata_err(sdata, "TID reservation already active\n");
5339 ret = -EALREADY;
5340 goto out;
5341 }
5342
5343 ieee80211_stop_vif_queues(sdata->local, sdata,
5344 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5345
5346 synchronize_net();
5347
5348 /* Tear down BA sessions so we stop aggregating on this TID */
Johannes Berg30686bf2015-06-02 21:39:54 +02005349 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005350 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5351 __ieee80211_stop_tx_ba_session(sta, tid,
5352 AGG_STOP_LOCAL_REQUEST);
5353 }
5354
5355 queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02005356 __ieee80211_flush_queues(local, sdata, queues, false);
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005357
5358 sta->reserved_tid = tid;
5359
5360 ieee80211_wake_vif_queues(local, sdata,
5361 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5362
Johannes Berg30686bf2015-06-02 21:39:54 +02005363 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005364 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5365
5366 ret = 0;
5367 out:
5368 return ret;
5369}
5370EXPORT_SYMBOL(ieee80211_reserve_tid);
5371
5372void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5373{
5374 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5375 struct ieee80211_sub_if_data *sdata = sta->sdata;
5376
5377 lockdep_assert_held(&sdata->local->sta_mtx);
5378
5379 /* only some cases are supported right now */
5380 switch (sdata->vif.type) {
5381 case NL80211_IFTYPE_STATION:
5382 case NL80211_IFTYPE_AP:
5383 case NL80211_IFTYPE_AP_VLAN:
5384 break;
5385 default:
5386 WARN_ON(1);
5387 return;
5388 }
5389
5390 if (tid != sta->reserved_tid) {
5391 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
5392 return;
5393 }
5394
5395 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
5396}
5397EXPORT_SYMBOL(ieee80211_unreserve_tid);
5398
Johannes Berg55de9082012-07-26 17:24:39 +02005399void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
5400 struct sk_buff *skb, int tid,
Mathy Vanhoef08aca292020-07-23 14:01:52 +04005401 enum nl80211_band band)
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005402{
Amadeusz Sławiński731977e2017-01-24 16:42:10 +01005403 int ac = ieee80211_ac_from_tid(tid);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02005404
Zhang Shengjud57a5442016-03-03 01:16:56 +00005405 skb_reset_mac_header(skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02005406 skb_set_queue_mapping(skb, ac);
Helmut Schaacf6bb792011-12-15 10:18:34 +01005407 skb->priority = tid;
Johannes Bergcf0277e2010-01-05 18:00:58 +01005408
Johannes Berg89afe612013-02-13 15:39:57 +01005409 skb->dev = sdata->dev;
5410
Johannes Berg3d34deb2009-06-18 17:25:11 +02005411 /*
5412 * The other path calling ieee80211_xmit is from the tasklet,
5413 * and while we can handle concurrent transmissions locking
5414 * requirements are that we do not come into tx with bhs on.
5415 */
5416 local_bh_disable();
Johannes Berg73c4e192014-11-09 18:50:09 +02005417 IEEE80211_SKB_CB(skb)->band = band;
Mathy Vanhoef08aca292020-07-23 14:01:52 +04005418 ieee80211_xmit(sdata, NULL, skb);
Johannes Berg3d34deb2009-06-18 17:25:11 +02005419 local_bh_enable();
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005420}
Denis Kenzior91180642018-03-26 12:52:50 -05005421
5422int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
5423 const u8 *buf, size_t len,
Markus Theildca9ca22020-05-08 16:42:00 +02005424 const u8 *dest, __be16 proto, bool unencrypted,
5425 u64 *cookie)
Denis Kenzior91180642018-03-26 12:52:50 -05005426{
5427 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5428 struct ieee80211_local *local = sdata->local;
Markus Theil10cb8e62021-02-06 12:51:12 +01005429 struct sta_info *sta;
Denis Kenzior91180642018-03-26 12:52:50 -05005430 struct sk_buff *skb;
5431 struct ethhdr *ehdr;
Johannes Bergb95d2cc2020-03-26 15:53:34 +01005432 u32 ctrl_flags = 0;
Markus Theila7528192020-05-27 18:03:34 +02005433 u32 flags = 0;
Denis Kenzior91180642018-03-26 12:52:50 -05005434
5435 /* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
5436 * or Pre-Authentication
5437 */
5438 if (proto != sdata->control_port_protocol &&
5439 proto != cpu_to_be16(ETH_P_PREAUTH))
5440 return -EINVAL;
5441
Johannes Bergb95d2cc2020-03-26 15:53:34 +01005442 if (proto == sdata->control_port_protocol)
Markus Theil5af7fef2020-06-17 10:26:36 +02005443 ctrl_flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO |
5444 IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
Johannes Bergb95d2cc2020-03-26 15:53:34 +01005445
Denis Kenzior91180642018-03-26 12:52:50 -05005446 if (unencrypted)
Markus Theila7528192020-05-27 18:03:34 +02005447 flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
5448
5449 if (cookie)
5450 ctrl_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
5451
Markus Theil10cb8e62021-02-06 12:51:12 +01005452 flags |= IEEE80211_TX_INTFL_NL80211_FRAME_TX;
Denis Kenzior91180642018-03-26 12:52:50 -05005453
5454 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5455 sizeof(struct ethhdr) + len);
5456 if (!skb)
5457 return -ENOMEM;
5458
5459 skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
5460
5461 skb_put_data(skb, buf, len);
5462
5463 ehdr = skb_push(skb, sizeof(struct ethhdr));
5464 memcpy(ehdr->h_dest, dest, ETH_ALEN);
Johannes Berg8079e4f2020-02-24 10:19:11 +01005465 memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
Denis Kenzior91180642018-03-26 12:52:50 -05005466 ehdr->h_proto = proto;
5467
5468 skb->dev = dev;
Markus Theil10cb8e62021-02-06 12:51:12 +01005469 skb->protocol = proto;
Denis Kenzior91180642018-03-26 12:52:50 -05005470 skb_reset_network_header(skb);
5471 skb_reset_mac_header(skb);
5472
Markus Theil10cb8e62021-02-06 12:51:12 +01005473 /* update QoS header to prioritize control port frames if possible,
5474 * priorization also happens for control port frames send over
5475 * AF_PACKET
5476 */
5477 rcu_read_lock();
5478
5479 if (ieee80211_lookup_ra_sta(sdata, skb, &sta) == 0 && !IS_ERR(sta)) {
5480 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
5481
5482 skb_set_queue_mapping(skb, queue);
5483 skb_get_hash(skb);
5484 }
5485
5486 rcu_read_unlock();
5487
Markus Theila7528192020-05-27 18:03:34 +02005488 /* mutex lock is only needed for incrementing the cookie counter */
5489 mutex_lock(&local->mtx);
5490
Denis Kenziore7441c92018-06-19 10:39:50 -05005491 local_bh_disable();
Markus Theila7528192020-05-27 18:03:34 +02005492 __ieee80211_subif_start_xmit(skb, skb->dev, flags, ctrl_flags, cookie);
Denis Kenziore7441c92018-06-19 10:39:50 -05005493 local_bh_enable();
Denis Kenzior91180642018-03-26 12:52:50 -05005494
Markus Theila7528192020-05-27 18:03:34 +02005495 mutex_unlock(&local->mtx);
5496
Denis Kenzior91180642018-03-26 12:52:50 -05005497 return 0;
5498}
Rajkumar Manoharan8828f812019-04-11 13:47:26 -07005499
5500int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
5501 const u8 *buf, size_t len)
5502{
5503 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5504 struct ieee80211_local *local = sdata->local;
5505 struct sk_buff *skb;
5506
5507 skb = dev_alloc_skb(local->hw.extra_tx_headroom + len +
5508 30 + /* header size */
5509 18); /* 11s header size */
5510 if (!skb)
5511 return -ENOMEM;
5512
5513 skb_reserve(skb, local->hw.extra_tx_headroom);
5514 skb_put_data(skb, buf, len);
5515
5516 skb->dev = dev;
5517 skb->protocol = htons(ETH_P_802_3);
5518 skb_reset_network_header(skb);
5519 skb_reset_mac_header(skb);
5520
5521 local_bh_disable();
5522 __ieee80211_subif_start_xmit(skb, skb->dev, 0,
Markus Theila7528192020-05-27 18:03:34 +02005523 IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP,
5524 NULL);
Johannes Berge2ebc742007-07-27 15:43:22 +02005525 local_bh_enable();
5526
5527 return 0;
5528}