blob: caa7caa89ab92a339b09437c6c0ac12c229bcb6b [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;
Ryder Lee3187ba02021-06-17 18:31:13 +0200669 bool encap = info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200670 bool assoc = false;
Johannes Berge6a98542008-10-21 12:40:02 +0200671
672 memset(&txrc, 0, sizeof(txrc));
Johannes Berg8318d782008-01-24 19:38:38 +0100673
Johannes Berg2d565772012-07-23 15:12:51 +0200674 sband = tx->local->hw.wiphy->bands[info->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200675
Shanyu Zhaoa2c40242010-04-27 11:15:12 -0700676 len = min_t(u32, tx->skb->len + FCS_LEN,
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200677 tx->local->hw.wiphy->frag_threshold);
Johannes Berg58d41852007-09-26 17:53:18 +0200678
Johannes Berge6a98542008-10-21 12:40:02 +0200679 /* set up the tx rate control struct we give the RC algo */
Johannes Berg005e4722012-02-26 11:24:35 +0100680 txrc.hw = &tx->local->hw;
Johannes Berge6a98542008-10-21 12:40:02 +0200681 txrc.sband = sband;
682 txrc.bss_conf = &tx->sdata->vif.bss_conf;
683 txrc.skb = tx->skb;
684 txrc.reported_rate.idx = -1;
Johannes Berg2d565772012-07-23 15:12:51 +0200685 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
Felix Fietkau2ffbe6d2013-04-16 13:38:42 +0200686
687 if (tx->sdata->rc_has_mcs_mask[info->band])
688 txrc.rate_idx_mcs_mask =
689 tx->sdata->rc_rateidx_mcs_mask[info->band];
690
Felix Fietkau8f0729b2010-11-11 15:07:23 +0100691 txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -0800692 tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
Bertold Van den Bergh5765f9f2015-08-05 16:02:28 +0200693 tx->sdata->vif.type == NL80211_IFTYPE_ADHOC ||
694 tx->sdata->vif.type == NL80211_IFTYPE_OCB);
Johannes Berg58d41852007-09-26 17:53:18 +0200695
Johannes Berge6a98542008-10-21 12:40:02 +0200696 /* set up RTS protection if desired */
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200697 if (len > tx->local->hw.wiphy->rts_threshold) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200698 txrc.rts = true;
Johannes Berge2ebc742007-07-27 15:43:22 +0200699 }
Johannes Berge6a98542008-10-21 12:40:02 +0200700
Felix Fietkau0d528d82013-04-22 16:14:41 +0200701 info->control.use_rts = txrc.rts;
Felix Fietkau991fec02013-04-16 13:38:43 +0200702 info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
703
Johannes Berge6a98542008-10-21 12:40:02 +0200704 /*
705 * Use short preamble if the BSS can handle it, but not for
706 * management frames unless we know the receiver can handle
707 * that -- the management frame might be to a station that
708 * just wants a probe response.
709 */
710 if (tx->sdata->vif.bss_conf.use_short_preamble &&
Ryder Lee3187ba02021-06-17 18:31:13 +0200711 (ieee80211_is_tx_data(tx->skb) ||
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200712 (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
Felix Fietkau0d528d82013-04-22 16:14:41 +0200713 txrc.short_preamble = true;
714
715 info->control.short_preamble = txrc.short_preamble;
Johannes Berge6a98542008-10-21 12:40:02 +0200716
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +0100717 /* don't ask rate control when rate already injected via radiotap */
718 if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)
719 return TX_CONTINUE;
720
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200721 if (tx->sta)
722 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge6a98542008-10-21 12:40:02 +0200723
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700724 /*
725 * Lets not bother rate control if we're associated and cannot
726 * talk to the sta. This should not happen.
727 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200728 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700729 !rate_usable_index_exists(sband, &tx->sta->sta),
730 "%s: Dropped data frame as no usable bitrate found while "
731 "scanning and associated. Target station: "
732 "%pM on %d GHz band\n",
Ryder Lee3187ba02021-06-17 18:31:13 +0200733 tx->sdata->name,
734 encap ? ((struct ethhdr *)hdr)->h_dest : hdr->addr1,
Johannes Berg2d565772012-07-23 15:12:51 +0200735 info->band ? 5 : 2))
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700736 return TX_DROP;
737
738 /*
739 * If we're associated with the sta at this point we know we can at
740 * least send the frame at the lowest bit rate.
741 */
Johannes Berge6a98542008-10-21 12:40:02 +0200742 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
743
Felix Fietkau0d528d82013-04-22 16:14:41 +0200744 if (tx->sta && !info->control.skip_table)
745 ratetbl = rcu_dereference(tx->sta->sta.rates);
746
747 if (unlikely(info->control.rates[0].idx < 0)) {
748 if (ratetbl) {
749 struct ieee80211_tx_rate rate = {
750 .idx = ratetbl->rate[0].idx,
751 .flags = ratetbl->rate[0].flags,
752 .count = ratetbl->rate[0].count
753 };
754
755 if (ratetbl->rate[0].idx < 0)
756 return TX_DROP;
757
758 tx->rate = rate;
759 } else {
760 return TX_DROP;
761 }
762 } else {
763 tx->rate = info->control.rates[0];
764 }
Johannes Berge6a98542008-10-21 12:40:02 +0200765
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100766 if (txrc.reported_rate.idx < 0) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200767 txrc.reported_rate = tx->rate;
Ryder Lee3187ba02021-06-17 18:31:13 +0200768 if (tx->sta && ieee80211_is_tx_data(tx->skb))
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200769 tx->sta->tx_stats.last_rate = txrc.reported_rate;
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100770 } else if (tx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200771 tx->sta->tx_stats.last_rate = txrc.reported_rate;
Johannes Berge6a98542008-10-21 12:40:02 +0200772
Felix Fietkau0d528d82013-04-22 16:14:41 +0200773 if (ratetbl)
774 return TX_CONTINUE;
775
Johannes Berge6a98542008-10-21 12:40:02 +0200776 if (unlikely(!info->control.rates[0].count))
777 info->control.rates[0].count = 1;
778
Gábor Stefanik99551512009-04-23 19:36:14 +0200779 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
780 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
781 info->control.rates[0].count = 1;
782
Johannes Berg9ae54c82008-01-31 19:48:20 +0100783 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200784}
785
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100786static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
787{
788 u16 *seq = &sta->tid_seq[tid];
789 __le16 ret = cpu_to_le16(*seq);
790
791 /* Increase the sequence number. */
792 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
793
794 return ret;
795}
796
Johannes Bergd9e8a702008-06-30 15:10:44 +0200797static ieee80211_tx_result debug_noinline
Johannes Bergf591fa52008-07-10 11:21:26 +0200798ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
799{
800 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
801 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergf591fa52008-07-10 11:21:26 +0200802 int tid;
803
Johannes Berg25d834e2008-09-12 22:52:47 +0200804 /*
805 * Packet injection may want to control the sequence
806 * number, if we have no matching interface then we
807 * neither assign one ourselves nor ask the driver to.
808 */
Johannes Berg5061b0c2009-07-14 00:33:34 +0200809 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
Johannes Berg25d834e2008-09-12 22:52:47 +0200810 return TX_CONTINUE;
811
Johannes Bergf591fa52008-07-10 11:21:26 +0200812 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
813 return TX_CONTINUE;
814
815 if (ieee80211_hdrlen(hdr->frame_control) < 24)
816 return TX_CONTINUE;
817
Johannes Berg49a59542011-09-29 16:04:41 +0200818 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
819 return TX_CONTINUE;
820
Mathy Vanhoef29c3e952020-07-23 14:01:50 +0400821 if (info->control.flags & IEEE80211_TX_CTRL_NO_SEQNO)
822 return TX_CONTINUE;
823
Johannes Berg94778282008-10-10 13:21:59 +0200824 /*
825 * Anything but QoS data that has a sequence number field
826 * (is long enough) gets a sequence number from the global
Bob Copelandc4c205f2013-08-23 09:35:38 -0400827 * counter. QoS data frames with a multicast destination
828 * also use the global counter (802.11-2012 9.3.2.10).
Johannes Berg94778282008-10-10 13:21:59 +0200829 */
Bob Copelandc4c205f2013-08-23 09:35:38 -0400830 if (!ieee80211_is_data_qos(hdr->frame_control) ||
831 is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg94778282008-10-10 13:21:59 +0200832 /* driver should assign sequence number */
Johannes Bergf591fa52008-07-10 11:21:26 +0200833 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
Johannes Berg94778282008-10-10 13:21:59 +0200834 /* for pure STA mode without beacons, we can do it */
835 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
836 tx->sdata->sequence_number += 0x10;
Johannes Berg79c892b2014-11-21 14:26:31 +0100837 if (tx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200838 tx->sta->tx_stats.msdu[IEEE80211_NUM_TIDS]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200839 return TX_CONTINUE;
840 }
841
842 /*
843 * This should be true for injected/management frames only, for
844 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
845 * above since they are not QoS-data frames.
846 */
847 if (!tx->sta)
848 return TX_CONTINUE;
849
850 /* include per-STA, per-TID sequence counter */
Sara Sharona1f2ba04c2018-02-19 14:48:40 +0200851 tid = ieee80211_get_tid(hdr);
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200852 tx->sta->tx_stats.msdu[tid]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200853
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +0200854 hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
Johannes Bergf591fa52008-07-10 11:21:26 +0200855
856 return TX_CONTINUE;
857}
858
Johannes Berg252b86c2011-11-16 15:28:55 +0100859static int ieee80211_fragment(struct ieee80211_tx_data *tx,
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100860 struct sk_buff *skb, int hdrlen,
861 int frag_threshold)
862{
Johannes Berg252b86c2011-11-16 15:28:55 +0100863 struct ieee80211_local *local = tx->local;
Johannes Berga1a3fce2011-11-16 15:28:56 +0100864 struct ieee80211_tx_info *info;
Johannes Berg252b86c2011-11-16 15:28:55 +0100865 struct sk_buff *tmp;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100866 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
867 int pos = hdrlen + per_fragm;
868 int rem = skb->len - hdrlen - per_fragm;
869
870 if (WARN_ON(rem < 0))
871 return -EINVAL;
872
Johannes Berg252b86c2011-11-16 15:28:55 +0100873 /* first fragment was already added to queue by caller */
874
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100875 while (rem) {
876 int fraglen = per_fragm;
877
878 if (fraglen > rem)
879 fraglen = rem;
880 rem -= fraglen;
881 tmp = dev_alloc_skb(local->tx_headroom +
882 frag_threshold +
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200883 tx->sdata->encrypt_headroom +
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100884 IEEE80211_ENCRYPT_TAILROOM);
885 if (!tmp)
886 return -ENOMEM;
Johannes Berg252b86c2011-11-16 15:28:55 +0100887
888 __skb_queue_tail(&tx->skbs, tmp);
889
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200890 skb_reserve(tmp,
891 local->tx_headroom + tx->sdata->encrypt_headroom);
892
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100893 /* copy control information */
894 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
Johannes Berga1a3fce2011-11-16 15:28:56 +0100895
896 info = IEEE80211_SKB_CB(tmp);
897 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
898 IEEE80211_TX_CTL_FIRST_FRAGMENT);
899
900 if (rem)
901 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
902
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100903 skb_copy_queue_mapping(tmp, skb);
904 tmp->priority = skb->priority;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100905 tmp->dev = skb->dev;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100906
907 /* copy header and data */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200908 skb_put_data(tmp, skb->data, hdrlen);
909 skb_put_data(tmp, skb->data + pos, fraglen);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100910
911 pos += fraglen;
912 }
913
Johannes Berg252b86c2011-11-16 15:28:55 +0100914 /* adjust first fragment's length */
Johannes Berg338f9772014-02-01 00:16:23 +0100915 skb_trim(skb, hdrlen + per_fragm);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100916 return 0;
917}
918
Johannes Bergf591fa52008-07-10 11:21:26 +0200919static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +0200920ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
921{
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100922 struct sk_buff *skb = tx->skb;
923 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
924 struct ieee80211_hdr *hdr = (void *)skb->data;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200925 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100926 int hdrlen;
927 int fragnum;
Johannes Berge2454942008-05-15 12:55:28 +0200928
Johannes Berg252b86c2011-11-16 15:28:55 +0100929 /* no matter what happens, tx->skb moves to tx->skbs */
930 __skb_queue_tail(&tx->skbs, skb);
931 tx->skb = NULL;
932
Johannes Berga26eb272011-10-07 14:01:25 +0200933 if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
934 return TX_CONTINUE;
935
Sara Sharonf3fe4e92016-10-18 23:12:11 +0300936 if (ieee80211_hw_check(&tx->local->hw, SUPPORTS_TX_FRAG))
Johannes Berge2454942008-05-15 12:55:28 +0200937 return TX_CONTINUE;
938
Johannes Bergeefce912008-05-17 00:57:13 +0200939 /*
940 * Warn when submitting a fragmented A-MPDU frame and drop it.
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200941 * This scenario is handled in ieee80211_tx_prepare but extra
Ron Rindjunsky8d5e0d52008-06-12 15:42:29 +0300942 * caution taken here as fragmented ampdu may cause Tx stop.
Johannes Bergeefce912008-05-17 00:57:13 +0200943 */
Sujith8b30b1f2008-10-24 09:55:27 +0530944 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
Johannes Bergeefce912008-05-17 00:57:13 +0200945 return TX_DROP;
946
Harvey Harrison065e96052008-06-22 16:45:27 -0700947 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berge2454942008-05-15 12:55:28 +0200948
Johannes Berga26eb272011-10-07 14:01:25 +0200949 /* internal error, why isn't DONTFRAG set? */
Johannes Berg8ccd8f22009-04-29 23:35:56 +0200950 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100951 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200952
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100953 /*
954 * Now fragment the frame. This will allocate all the fragments and
955 * chain them (using skb as the first fragment) to skb->next.
956 * During transmission, we will remove the successfully transmitted
957 * fragments from this list. When the low-level driver rejects one
958 * of the fragments then we will simply pretend to accept the skb
959 * but store it away as pending.
960 */
Johannes Berg252b86c2011-11-16 15:28:55 +0100961 if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100962 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200963
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100964 /* update duration/seq/flags of fragments */
965 fragnum = 0;
Johannes Berg252b86c2011-11-16 15:28:55 +0100966
967 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100968 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
Johannes Berge2454942008-05-15 12:55:28 +0200969
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100970 hdr = (void *)skb->data;
971 info = IEEE80211_SKB_CB(skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200972
Johannes Berg252b86c2011-11-16 15:28:55 +0100973 if (!skb_queue_is_last(&tx->skbs, skb)) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100974 hdr->frame_control |= morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200975 /*
976 * No multi-rate retries for fragmented frames, that
977 * would completely throw off the NAV at other STAs.
978 */
979 info->control.rates[1].idx = -1;
980 info->control.rates[2].idx = -1;
981 info->control.rates[3].idx = -1;
Thomas Huehne3e1a0b2012-07-02 19:46:16 +0200982 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
Johannes Berge6a98542008-10-21 12:40:02 +0200983 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100984 } else {
985 hdr->frame_control &= ~morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200986 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100987 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
988 fragnum++;
Johannes Berg252b86c2011-11-16 15:28:55 +0100989 }
Johannes Berge2454942008-05-15 12:55:28 +0200990
991 return TX_CONTINUE;
Johannes Berge2454942008-05-15 12:55:28 +0200992}
993
Johannes Bergd9e8a702008-06-30 15:10:44 +0200994static ieee80211_tx_result debug_noinline
Johannes Bergfeff1f22009-08-10 16:01:54 +0200995ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
996{
Johannes Berg252b86c2011-11-16 15:28:55 +0100997 struct sk_buff *skb;
Johannes Berg560d2682013-02-05 10:55:21 +0100998 int ac = -1;
Johannes Bergfeff1f22009-08-10 16:01:54 +0200999
1000 if (!tx->sta)
1001 return TX_CONTINUE;
1002
Johannes Berg252b86c2011-11-16 15:28:55 +01001003 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg560d2682013-02-05 10:55:21 +01001004 ac = skb_get_queue_mapping(skb);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001005 tx->sta->tx_stats.bytes[ac] += skb->len;
Johannes Berg252b86c2011-11-16 15:28:55 +01001006 }
Johannes Berg560d2682013-02-05 10:55:21 +01001007 if (ac >= 0)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001008 tx->sta->tx_stats.packets[ac]++;
Johannes Bergfeff1f22009-08-10 16:01:54 +02001009
1010 return TX_CONTINUE;
1011}
1012
1013static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +02001014ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
1015{
1016 if (!tx->key)
1017 return TX_CONTINUE;
1018
Johannes Berg97359d12010-08-10 09:46:38 +02001019 switch (tx->key->conf.cipher) {
1020 case WLAN_CIPHER_SUITE_WEP40:
1021 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge2454942008-05-15 12:55:28 +02001022 return ieee80211_crypto_wep_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001023 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berge2454942008-05-15 12:55:28 +02001024 return ieee80211_crypto_tkip_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001025 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001026 return ieee80211_crypto_ccmp_encrypt(
1027 tx, IEEE80211_CCMP_MIC_LEN);
1028 case WLAN_CIPHER_SUITE_CCMP_256:
1029 return ieee80211_crypto_ccmp_encrypt(
1030 tx, IEEE80211_CCMP_256_MIC_LEN);
Johannes Berg97359d12010-08-10 09:46:38 +02001031 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001032 return ieee80211_crypto_aes_cmac_encrypt(tx);
Jouni Malinen56c52da2015-01-24 19:52:08 +02001033 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1034 return ieee80211_crypto_aes_cmac_256_encrypt(tx);
Jouni Malinen8ade5382015-01-24 19:52:09 +02001035 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1036 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1037 return ieee80211_crypto_aes_gmac_encrypt(tx);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001038 case WLAN_CIPHER_SUITE_GCMP:
1039 case WLAN_CIPHER_SUITE_GCMP_256:
1040 return ieee80211_crypto_gcmp_encrypt(tx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +03001041 default:
Yoni Divinskyd32a1022012-01-16 15:18:59 +02001042 return ieee80211_crypto_hw_encrypt(tx);
Johannes Berge2454942008-05-15 12:55:28 +02001043 }
1044
Johannes Berge2454942008-05-15 12:55:28 +02001045 return TX_DROP;
1046}
1047
Johannes Bergd9e8a702008-06-30 15:10:44 +02001048static ieee80211_tx_result debug_noinline
Johannes Berg03f93c32008-06-25 14:36:27 +03001049ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1050{
Johannes Berg252b86c2011-11-16 15:28:55 +01001051 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001052 struct ieee80211_hdr *hdr;
1053 int next_len;
1054 bool group_addr;
Johannes Berg03f93c32008-06-25 14:36:27 +03001055
Johannes Berg252b86c2011-11-16 15:28:55 +01001056 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001057 hdr = (void *) skb->data;
Jouni Malinen7e0aae42009-05-19 19:25:58 +03001058 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1059 break; /* must not overwrite AID */
Johannes Berg252b86c2011-11-16 15:28:55 +01001060 if (!skb_queue_is_last(&tx->skbs, skb)) {
1061 struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1062 next_len = next->len;
1063 } else
1064 next_len = 0;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001065 group_addr = is_multicast_ether_addr(hdr->addr1);
Johannes Berg03f93c32008-06-25 14:36:27 +03001066
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001067 hdr->duration_id =
Johannes Berg252b86c2011-11-16 15:28:55 +01001068 ieee80211_duration(tx, skb, group_addr, next_len);
1069 }
Johannes Berg03f93c32008-06-25 14:36:27 +03001070
1071 return TX_CONTINUE;
1072}
1073
Johannes Berge2ebc742007-07-27 15:43:22 +02001074/* actual transmit path */
1075
Johannes Berga622ab72010-06-10 10:21:39 +02001076static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1077 struct sk_buff *skb,
1078 struct ieee80211_tx_info *info,
1079 struct tid_ampdu_tx *tid_tx,
1080 int tid)
1081{
1082 bool queued = false;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001083 bool reset_agg_timer = false;
Helmut Schaaaa454582012-03-07 17:20:30 +01001084 struct sk_buff *purge_skb = NULL;
Johannes Berga622ab72010-06-10 10:21:39 +02001085
1086 if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1087 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001088 reset_agg_timer = true;
Johannes Berg0ab33702010-06-10 10:21:42 +02001089 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1090 /*
1091 * nothing -- this aggregation session is being started
1092 * but that might still fail with the driver
1093 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001094 } else if (!tx->sta->sta.txq[tid]) {
Johannes Berga622ab72010-06-10 10:21:39 +02001095 spin_lock(&tx->sta->lock);
1096 /*
1097 * Need to re-check now, because we may get here
1098 *
1099 * 1) in the window during which the setup is actually
1100 * already done, but not marked yet because not all
1101 * packets are spliced over to the driver pending
1102 * queue yet -- if this happened we acquire the lock
1103 * either before or after the splice happens, but
1104 * need to recheck which of these cases happened.
1105 *
1106 * 2) during session teardown, if the OPERATIONAL bit
1107 * was cleared due to the teardown but the pointer
1108 * hasn't been assigned NULL yet (or we loaded it
1109 * before it was assigned) -- in this case it may
1110 * now be NULL which means we should just let the
1111 * packet pass through because splicing the frames
1112 * back is already done.
1113 */
Johannes Berg40b275b2011-05-13 14:15:49 +02001114 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
Johannes Berga622ab72010-06-10 10:21:39 +02001115
1116 if (!tid_tx) {
1117 /* do nothing, let packet pass through */
1118 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1119 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001120 reset_agg_timer = true;
Johannes Berga622ab72010-06-10 10:21:39 +02001121 } else {
1122 queued = true;
Emmanuel Grumbachf6d46712016-03-17 16:51:42 +02001123 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
1124 clear_sta_flag(tx->sta, WLAN_STA_SP);
1125 ps_dbg(tx->sta->sdata,
1126 "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
1127 tx->sta->sta.addr, tx->sta->sta.aid);
1128 }
Johannes Berga622ab72010-06-10 10:21:39 +02001129 info->control.vif = &tx->sdata->vif;
Felix Fietkaucc20ff22020-09-08 14:36:57 +02001130 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
Emmanuel Grumbachfacde7f2016-03-17 16:51:41 +02001131 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berga622ab72010-06-10 10:21:39 +02001132 __skb_queue_tail(&tid_tx->pending, skb);
Helmut Schaaaa454582012-03-07 17:20:30 +01001133 if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1134 purge_skb = __skb_dequeue(&tid_tx->pending);
Johannes Berga622ab72010-06-10 10:21:39 +02001135 }
1136 spin_unlock(&tx->sta->lock);
Helmut Schaaaa454582012-03-07 17:20:30 +01001137
1138 if (purge_skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001139 ieee80211_free_txskb(&tx->local->hw, purge_skb);
Johannes Berga622ab72010-06-10 10:21:39 +02001140 }
1141
Nikolay Martynov285fa692011-11-22 21:50:28 -05001142 /* reset session timer */
Sara Sharon914eac22018-04-20 13:49:19 +03001143 if (reset_agg_timer)
Felix Fietkau12d3952f2012-03-18 22:58:06 +01001144 tid_tx->last_tx = jiffies;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001145
Johannes Berga622ab72010-06-10 10:21:39 +02001146 return queued;
1147}
1148
Johannes Berg58d41852007-09-26 17:53:18 +02001149/*
1150 * initialises @tx
Johannes Berg7c107702015-03-20 14:18:27 +01001151 * pass %NULL for the station if unknown, a valid pointer if known
1152 * or an ERR_PTR() if the station is known not to exist
Johannes Berg58d41852007-09-26 17:53:18 +02001153 */
Johannes Berg9ae54c82008-01-31 19:48:20 +01001154static ieee80211_tx_result
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001155ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1156 struct ieee80211_tx_data *tx,
Johannes Berg7c107702015-03-20 14:18:27 +01001157 struct sta_info *sta, struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02001158{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001159 struct ieee80211_local *local = sdata->local;
Johannes Berg58d41852007-09-26 17:53:18 +02001160 struct ieee80211_hdr *hdr;
Johannes Berge039fa42008-05-15 12:55:29 +02001161 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg68f2b512011-10-07 14:01:24 +02001162 int tid;
Johannes Berge2ebc742007-07-27 15:43:22 +02001163
1164 memset(tx, 0, sizeof(*tx));
1165 tx->skb = skb;
Johannes Berge2ebc742007-07-27 15:43:22 +02001166 tx->local = local;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001167 tx->sdata = sdata;
Johannes Berg252b86c2011-11-16 15:28:55 +01001168 __skb_queue_head_init(&tx->skbs);
Johannes Berge2ebc742007-07-27 15:43:22 +02001169
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001170 /*
1171 * If this flag is set to true anywhere, and we get here,
1172 * we are doing the needed processing, so remove the flag
1173 * now.
1174 */
Felix Fietkaucc20ff22020-09-08 14:36:57 +02001175 info->control.flags &= ~IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001176
Johannes Berg58d41852007-09-26 17:53:18 +02001177 hdr = (struct ieee80211_hdr *) skb->data;
1178
Johannes Berg7c107702015-03-20 14:18:27 +01001179 if (likely(sta)) {
1180 if (!IS_ERR(sta))
1181 tx->sta = sta;
1182 } else {
1183 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1184 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1185 if (!tx->sta && sdata->wdev.use_4addr)
1186 return TX_DROP;
Markus Theil10cb8e62021-02-06 12:51:12 +01001187 } else if (tx->sdata->control_port_protocol == tx->skb->protocol) {
Johannes Berg7c107702015-03-20 14:18:27 +01001188 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1189 }
1190 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
1191 tx->sta = sta_info_get(sdata, hdr->addr1);
Felix Fietkau3f0e0b22010-01-08 18:15:13 +01001192 }
Johannes Berg58d41852007-09-26 17:53:18 +02001193
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001194 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
Johannes Berg49a59542011-09-29 16:04:41 +02001195 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001196 ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
1197 !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001198 struct tid_ampdu_tx *tid_tx;
1199
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001200 tid = ieee80211_get_tid(hdr);
Sujith8b30b1f2008-10-24 09:55:27 +05301201
Johannes Berga622ab72010-06-10 10:21:39 +02001202 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1203 if (tid_tx) {
1204 bool queued;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001205
Johannes Berga622ab72010-06-10 10:21:39 +02001206 queued = ieee80211_tx_prep_agg(tx, skb, info,
1207 tid_tx, tid);
1208
1209 if (unlikely(queued))
1210 return TX_QUEUED;
1211 }
Sujith8b30b1f2008-10-24 09:55:27 +05301212 }
1213
Jiri Slabybadffb72007-08-28 17:01:54 -04001214 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001215 tx->flags &= ~IEEE80211_TX_UNICAST;
Johannes Berge039fa42008-05-15 12:55:29 +02001216 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Simon Wunderlich6fd67e92011-11-18 14:20:42 +01001217 } else
Johannes Berg5cf121c2008-02-25 16:27:43 +01001218 tx->flags |= IEEE80211_TX_UNICAST;
Johannes Berg58d41852007-09-26 17:53:18 +02001219
Johannes Berga26eb272011-10-07 14:01:25 +02001220 if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1221 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1222 skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1223 info->flags & IEEE80211_TX_CTL_AMPDU)
1224 info->flags |= IEEE80211_TX_CTL_DONTFRAG;
Johannes Berg58d41852007-09-26 17:53:18 +02001225 }
1226
Johannes Berge2ebc742007-07-27 15:43:22 +02001227 if (!tx->sta)
Johannes Berge039fa42008-05-15 12:55:29 +02001228 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Felix Fietkauf7418bc2015-09-24 14:59:49 +02001229 else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
Johannes Berge039fa42008-05-15 12:55:29 +02001230 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Felix Fietkauf7418bc2015-09-24 14:59:49 +02001231 ieee80211_check_fast_xmit(tx->sta);
1232 }
Johannes Berg58d41852007-09-26 17:53:18 +02001233
Johannes Berge039fa42008-05-15 12:55:29 +02001234 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001235
Johannes Berg9ae54c82008-01-31 19:48:20 +01001236 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001237}
1238
Michal Kazior80a83cf2016-05-19 10:37:48 +02001239static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
1240 struct ieee80211_vif *vif,
Michal Kaziordbef5362017-01-13 13:32:51 +01001241 struct sta_info *sta,
Michal Kazior80a83cf2016-05-19 10:37:48 +02001242 struct sk_buff *skb)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001243{
1244 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001245 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001246 struct ieee80211_txq *txq = NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001247
Felix Fietkauc3732a72016-02-28 15:19:53 +01001248 if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
1249 (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
Michal Kazior80a83cf2016-05-19 10:37:48 +02001250 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001251
Felix Fietkaucc20ff22020-09-08 14:36:57 +02001252 if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) &&
John Crispin50ff4772019-11-25 11:04:37 +01001253 unlikely(!ieee80211_is_data_present(hdr->frame_control))) {
Johannes Bergadf8ed02018-08-31 11:31:08 +03001254 if ((!ieee80211_is_mgmt(hdr->frame_control) ||
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001255 ieee80211_is_bufferable_mmpdu(hdr->frame_control) ||
1256 vif->type == NL80211_IFTYPE_STATION) &&
Johannes Bergadf8ed02018-08-31 11:31:08 +03001257 sta && sta->uploaded) {
1258 /*
1259 * This will be NULL if the driver didn't set the
1260 * opt-in hardware flag.
1261 */
1262 txq = sta->sta.txq[IEEE80211_NUM_TIDS];
1263 }
1264 } else if (sta) {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001265 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1266
Michal Kaziordbef5362017-01-13 13:32:51 +01001267 if (!sta->uploaded)
1268 return NULL;
1269
1270 txq = sta->sta.txq[tid];
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001271 } else if (vif) {
1272 txq = vif->txq;
1273 }
1274
1275 if (!txq)
Michal Kazior80a83cf2016-05-19 10:37:48 +02001276 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001277
Michal Kazior80a83cf2016-05-19 10:37:48 +02001278 return to_txq_info(txq);
1279}
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001280
Michal Kazior5caa3282016-05-19 10:37:51 +02001281static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
1282{
1283 IEEE80211_SKB_CB(skb)->control.enqueue_time = codel_get_time();
1284}
1285
Michal Kazior5caa3282016-05-19 10:37:51 +02001286static u32 codel_skb_len_func(const struct sk_buff *skb)
1287{
1288 return skb->len;
1289}
1290
1291static codel_time_t codel_skb_time_func(const struct sk_buff *skb)
1292{
1293 const struct ieee80211_tx_info *info;
1294
1295 info = (const struct ieee80211_tx_info *)skb->cb;
1296 return info->control.enqueue_time;
1297}
1298
1299static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars,
1300 void *ctx)
1301{
1302 struct ieee80211_local *local;
1303 struct txq_info *txqi;
1304 struct fq *fq;
1305 struct fq_flow *flow;
1306
1307 txqi = ctx;
1308 local = vif_to_sdata(txqi->txq.vif)->local;
1309 fq = &local->fq;
1310
1311 if (cvars == &txqi->def_cvars)
Felix Fietkaubf9009b2020-12-18 19:47:14 +01001312 flow = &txqi->tin.default_flow;
Michal Kazior5caa3282016-05-19 10:37:51 +02001313 else
1314 flow = &fq->flows[cvars - local->cvars];
1315
1316 return fq_flow_dequeue(fq, flow);
1317}
1318
1319static void codel_drop_func(struct sk_buff *skb,
1320 void *ctx)
1321{
1322 struct ieee80211_local *local;
1323 struct ieee80211_hw *hw;
1324 struct txq_info *txqi;
1325
1326 txqi = ctx;
1327 local = vif_to_sdata(txqi->txq.vif)->local;
1328 hw = &local->hw;
1329
1330 ieee80211_free_txskb(hw, skb);
1331}
1332
Michal Kaziorfa962b92016-05-19 10:37:49 +02001333static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
1334 struct fq_tin *tin,
1335 struct fq_flow *flow)
1336{
Michal Kazior5caa3282016-05-19 10:37:51 +02001337 struct ieee80211_local *local;
1338 struct txq_info *txqi;
1339 struct codel_vars *cvars;
1340 struct codel_params *cparams;
1341 struct codel_stats *cstats;
1342
1343 local = container_of(fq, struct ieee80211_local, fq);
1344 txqi = container_of(tin, struct txq_info, tin);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001345 cstats = &txqi->cstats;
Michal Kazior5caa3282016-05-19 10:37:51 +02001346
Toke Høiland-Jørgensen484a54c2017-04-06 11:38:26 +02001347 if (txqi->txq.sta) {
1348 struct sta_info *sta = container_of(txqi->txq.sta,
1349 struct sta_info, sta);
1350 cparams = &sta->cparams;
1351 } else {
1352 cparams = &local->cparams;
1353 }
1354
Felix Fietkaubf9009b2020-12-18 19:47:14 +01001355 if (flow == &tin->default_flow)
Michal Kazior5caa3282016-05-19 10:37:51 +02001356 cvars = &txqi->def_cvars;
1357 else
1358 cvars = &local->cvars[flow - fq->flows];
1359
1360 return codel_dequeue(txqi,
1361 &flow->backlog,
1362 cparams,
1363 cvars,
1364 cstats,
1365 codel_skb_len_func,
1366 codel_skb_time_func,
1367 codel_drop_func,
1368 codel_dequeue_func);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001369}
1370
1371static void fq_skb_free_func(struct fq *fq,
1372 struct fq_tin *tin,
1373 struct fq_flow *flow,
1374 struct sk_buff *skb)
1375{
1376 struct ieee80211_local *local;
1377
1378 local = container_of(fq, struct ieee80211_local, fq);
1379 ieee80211_free_txskb(&local->hw, skb);
1380}
1381
Michal Kazior80a83cf2016-05-19 10:37:48 +02001382static void ieee80211_txq_enqueue(struct ieee80211_local *local,
1383 struct txq_info *txqi,
1384 struct sk_buff *skb)
1385{
Michal Kaziorfa962b92016-05-19 10:37:49 +02001386 struct fq *fq = &local->fq;
1387 struct fq_tin *tin = &txqi->tin;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001388 u32 flow_idx = fq_flow_idx(fq, skb);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001389
Michal Kazior5caa3282016-05-19 10:37:51 +02001390 ieee80211_set_skb_enqueue_time(skb);
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001391
1392 spin_lock_bh(&fq->lock);
Johannes Berg73bc9e02021-03-19 23:28:01 +01001393 /*
1394 * For management frames, don't really apply codel etc.,
1395 * we don't want to apply any shaping or anything we just
1396 * want to simplify the driver API by having them on the
1397 * txqi.
1398 */
Johannes Bergca47b462021-04-16 13:47:04 +02001399 if (unlikely(txqi->txq.tid == IEEE80211_NUM_TIDS)) {
1400 IEEE80211_SKB_CB(skb)->control.flags |=
1401 IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
Johannes Berg73bc9e02021-03-19 23:28:01 +01001402 __skb_queue_tail(&txqi->frags, skb);
Johannes Bergca47b462021-04-16 13:47:04 +02001403 } else {
Johannes Berg73bc9e02021-03-19 23:28:01 +01001404 fq_tin_enqueue(fq, tin, flow_idx, skb,
1405 fq_skb_free_func);
Johannes Bergca47b462021-04-16 13:47:04 +02001406 }
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001407 spin_unlock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001408}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001409
Johannes Berg2a9e2572017-10-06 11:53:33 +02001410static bool fq_vlan_filter_func(struct fq *fq, struct fq_tin *tin,
1411 struct fq_flow *flow, struct sk_buff *skb,
1412 void *data)
1413{
1414 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1415
1416 return info->control.vif == data;
1417}
1418
1419void ieee80211_txq_remove_vlan(struct ieee80211_local *local,
1420 struct ieee80211_sub_if_data *sdata)
1421{
1422 struct fq *fq = &local->fq;
1423 struct txq_info *txqi;
1424 struct fq_tin *tin;
1425 struct ieee80211_sub_if_data *ap;
1426
1427 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1428 return;
1429
1430 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1431
1432 if (!ap->vif.txq)
1433 return;
1434
1435 txqi = to_txq_info(ap->vif.txq);
1436 tin = &txqi->tin;
1437
1438 spin_lock_bh(&fq->lock);
1439 fq_tin_filter(fq, tin, fq_vlan_filter_func, &sdata->vif,
1440 fq_skb_free_func);
1441 spin_unlock_bh(&fq->lock);
1442}
1443
Michal Kaziorfa962b92016-05-19 10:37:49 +02001444void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
1445 struct sta_info *sta,
1446 struct txq_info *txqi, int tid)
1447{
1448 fq_tin_init(&txqi->tin);
Michal Kazior5caa3282016-05-19 10:37:51 +02001449 codel_vars_init(&txqi->def_cvars);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001450 codel_stats_init(&txqi->cstats);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001451 __skb_queue_head_init(&txqi->frags);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001452 INIT_LIST_HEAD(&txqi->schedule_order);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001453
1454 txqi->txq.vif = &sdata->vif;
1455
Johannes Bergadf8ed02018-08-31 11:31:08 +03001456 if (!sta) {
Michal Kaziorfa962b92016-05-19 10:37:49 +02001457 sdata->vif.txq = &txqi->txq;
1458 txqi->txq.tid = 0;
1459 txqi->txq.ac = IEEE80211_AC_BE;
Johannes Bergadf8ed02018-08-31 11:31:08 +03001460
1461 return;
Michal Kazior80a83cf2016-05-19 10:37:48 +02001462 }
Johannes Bergadf8ed02018-08-31 11:31:08 +03001463
1464 if (tid == IEEE80211_NUM_TIDS) {
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001465 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1466 /* Drivers need to opt in to the management MPDU TXQ */
1467 if (!ieee80211_hw_check(&sdata->local->hw,
1468 STA_MMPDU_TXQ))
1469 return;
1470 } else if (!ieee80211_hw_check(&sdata->local->hw,
1471 BUFF_MMPDU_TXQ)) {
1472 /* Drivers need to opt in to the bufferable MMPDU TXQ */
Johannes Bergadf8ed02018-08-31 11:31:08 +03001473 return;
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001474 }
Johannes Bergadf8ed02018-08-31 11:31:08 +03001475 txqi->txq.ac = IEEE80211_AC_VO;
1476 } else {
1477 txqi->txq.ac = ieee80211_ac_from_tid(tid);
1478 }
1479
1480 txqi->txq.sta = &sta->sta;
1481 txqi->txq.tid = tid;
1482 sta->sta.txq[tid] = &txqi->txq;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001483}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001484
Michal Kaziorfa962b92016-05-19 10:37:49 +02001485void ieee80211_txq_purge(struct ieee80211_local *local,
1486 struct txq_info *txqi)
1487{
1488 struct fq *fq = &local->fq;
1489 struct fq_tin *tin = &txqi->tin;
1490
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08001491 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001492 fq_tin_reset(fq, tin, fq_skb_free_func);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001493 ieee80211_purge_tx_queue(&local->hw, &txqi->frags);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08001494 spin_unlock_bh(&fq->lock);
1495
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001496 spin_lock_bh(&local->active_txq_lock[txqi->txq.ac]);
1497 list_del_init(&txqi->schedule_order);
1498 spin_unlock_bh(&local->active_txq_lock[txqi->txq.ac]);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001499}
1500
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001501void ieee80211_txq_set_params(struct ieee80211_local *local)
1502{
1503 if (local->hw.wiphy->txq_limit)
1504 local->fq.limit = local->hw.wiphy->txq_limit;
1505 else
1506 local->hw.wiphy->txq_limit = local->fq.limit;
1507
1508 if (local->hw.wiphy->txq_memory_limit)
1509 local->fq.memory_limit = local->hw.wiphy->txq_memory_limit;
1510 else
1511 local->hw.wiphy->txq_memory_limit = local->fq.memory_limit;
1512
1513 if (local->hw.wiphy->txq_quantum)
1514 local->fq.quantum = local->hw.wiphy->txq_quantum;
1515 else
1516 local->hw.wiphy->txq_quantum = local->fq.quantum;
1517}
1518
Michal Kaziorfa962b92016-05-19 10:37:49 +02001519int ieee80211_txq_setup_flows(struct ieee80211_local *local)
1520{
1521 struct fq *fq = &local->fq;
1522 int ret;
Michal Kazior5caa3282016-05-19 10:37:51 +02001523 int i;
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001524 bool supp_vht = false;
1525 enum nl80211_band band;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001526
1527 if (!local->ops->wake_tx_queue)
1528 return 0;
1529
1530 ret = fq_init(fq, 4096);
1531 if (ret)
1532 return ret;
1533
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001534 /*
1535 * If the hardware doesn't support VHT, it is safe to limit the maximum
1536 * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
1537 */
1538 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1539 struct ieee80211_supported_band *sband;
1540
1541 sband = local->hw.wiphy->bands[band];
1542 if (!sband)
1543 continue;
1544
1545 supp_vht = supp_vht || sband->vht_cap.vht_supported;
1546 }
1547
1548 if (!supp_vht)
1549 fq->memory_limit = 4 << 20; /* 4 Mbytes */
1550
Michal Kazior5caa3282016-05-19 10:37:51 +02001551 codel_params_init(&local->cparams);
Michal Kazior5caa3282016-05-19 10:37:51 +02001552 local->cparams.interval = MS2TIME(100);
1553 local->cparams.target = MS2TIME(20);
1554 local->cparams.ecn = true;
1555
1556 local->cvars = kcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
1557 GFP_KERNEL);
1558 if (!local->cvars) {
Michal Kazior59a7c822016-06-29 14:00:34 +02001559 spin_lock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001560 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001561 spin_unlock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001562 return -ENOMEM;
1563 }
1564
1565 for (i = 0; i < fq->flows_cnt; i++)
1566 codel_vars_init(&local->cvars[i]);
1567
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001568 ieee80211_txq_set_params(local);
1569
Michal Kaziorfa962b92016-05-19 10:37:49 +02001570 return 0;
1571}
1572
1573void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1574{
1575 struct fq *fq = &local->fq;
1576
1577 if (!local->ops->wake_tx_queue)
1578 return;
1579
Michal Kazior5caa3282016-05-19 10:37:51 +02001580 kfree(local->cvars);
1581 local->cvars = NULL;
1582
Michal Kazior59a7c822016-06-29 14:00:34 +02001583 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001584 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001585 spin_unlock_bh(&fq->lock);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001586}
1587
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001588static bool ieee80211_queue_skb(struct ieee80211_local *local,
1589 struct ieee80211_sub_if_data *sdata,
1590 struct sta_info *sta,
1591 struct sk_buff *skb)
1592{
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001593 struct ieee80211_vif *vif;
1594 struct txq_info *txqi;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001595
1596 if (!local->ops->wake_tx_queue ||
1597 sdata->vif.type == NL80211_IFTYPE_MONITOR)
1598 return false;
1599
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001600 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1601 sdata = container_of(sdata->bss,
1602 struct ieee80211_sub_if_data, u.ap);
1603
1604 vif = &sdata->vif;
Michal Kaziordbef5362017-01-13 13:32:51 +01001605 txqi = ieee80211_get_txq(local, vif, sta, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001606
1607 if (!txqi)
1608 return false;
1609
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001610 ieee80211_txq_enqueue(local, txqi, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001611
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001612 schedule_and_wake_txq(local, txqi);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001613
1614 return true;
1615}
1616
Johannes Berg11127e92011-11-16 16:02:47 +01001617static bool ieee80211_tx_frags(struct ieee80211_local *local,
1618 struct ieee80211_vif *vif,
Johannes Berg4fd03282019-10-01 23:26:35 +02001619 struct sta_info *sta,
Johannes Berg11127e92011-11-16 16:02:47 +01001620 struct sk_buff_head *skbs,
1621 bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001622{
Michal Kazior80a83cf2016-05-19 10:37:48 +02001623 struct ieee80211_tx_control control = {};
Johannes Berg252b86c2011-11-16 15:28:55 +01001624 struct sk_buff *skb, *tmp;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001625 unsigned long flags;
Johannes Berge2ebc742007-07-27 15:43:22 +02001626
Johannes Berg252b86c2011-11-16 15:28:55 +01001627 skb_queue_walk_safe(skbs, skb, tmp) {
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001628 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1629 int q = info->hw_queue;
1630
1631#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1632 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1633 __skb_unlink(skb, skbs);
Felix Fietkauc3e77242012-10-08 14:39:33 +02001634 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001635 continue;
1636 }
1637#endif
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001638
1639 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001640 if (local->queue_stop_reasons[q] ||
Johannes Berg7bb45682011-02-24 14:42:06 +01001641 (!txpending && !skb_queue_empty(&local->pending[q]))) {
Seth Forshee6c17b772013-02-11 11:21:07 -06001642 if (unlikely(info->flags &
Seth Forsheea7679ed2013-02-25 14:58:05 -06001643 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1644 if (local->queue_stop_reasons[q] &
1645 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1646 /*
1647 * Drop off-channel frames if queues
1648 * are stopped for any reason other
1649 * than off-channel operation. Never
1650 * queue them.
1651 */
1652 spin_unlock_irqrestore(
1653 &local->queue_stop_reason_lock,
1654 flags);
1655 ieee80211_purge_tx_queue(&local->hw,
1656 skbs);
1657 return true;
1658 }
1659 } else {
1660
Seth Forshee6c17b772013-02-11 11:21:07 -06001661 /*
Seth Forsheea7679ed2013-02-25 14:58:05 -06001662 * Since queue is stopped, queue up frames for
1663 * later transmission from the tx-pending
1664 * tasklet when the queue is woken again.
Seth Forshee6c17b772013-02-11 11:21:07 -06001665 */
Seth Forsheea7679ed2013-02-25 14:58:05 -06001666 if (txpending)
1667 skb_queue_splice_init(skbs,
1668 &local->pending[q]);
1669 else
1670 skb_queue_splice_tail_init(skbs,
1671 &local->pending[q]);
1672
1673 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1674 flags);
1675 return false;
Seth Forshee6c17b772013-02-11 11:21:07 -06001676 }
Johannes Berg7bb45682011-02-24 14:42:06 +01001677 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001678 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Tomas Winklerf8e79dd2008-07-24 18:46:44 +03001679
Johannes Berg11127e92011-11-16 16:02:47 +01001680 info->control.vif = vif;
Johannes Berg4fd03282019-10-01 23:26:35 +02001681 control.sta = sta ? &sta->sta : NULL;
Johannes Bergec25acc2010-07-22 17:11:28 +02001682
Johannes Berg252b86c2011-11-16 15:28:55 +01001683 __skb_unlink(skb, skbs);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001684 drv_tx(local, &control, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001685 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001686
Johannes Berg11127e92011-11-16 16:02:47 +01001687 return true;
1688}
1689
1690/*
1691 * Returns false if the frame couldn't be transmitted but was queued instead.
1692 */
1693static bool __ieee80211_tx(struct ieee80211_local *local,
1694 struct sk_buff_head *skbs, int led_len,
1695 struct sta_info *sta, bool txpending)
1696{
1697 struct ieee80211_tx_info *info;
1698 struct ieee80211_sub_if_data *sdata;
1699 struct ieee80211_vif *vif;
Johannes Berg11127e92011-11-16 16:02:47 +01001700 struct sk_buff *skb;
Colin Ian King958574c2021-03-28 22:37:29 +01001701 bool result;
Johannes Berg11127e92011-11-16 16:02:47 +01001702 __le16 fc;
1703
1704 if (WARN_ON(skb_queue_empty(skbs)))
1705 return true;
1706
1707 skb = skb_peek(skbs);
1708 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1709 info = IEEE80211_SKB_CB(skb);
1710 sdata = vif_to_sdata(info->control.vif);
1711 if (sta && !sta->uploaded)
1712 sta = NULL;
1713
Johannes Berg11127e92011-11-16 16:02:47 +01001714 switch (sdata->vif.type) {
1715 case NL80211_IFTYPE_MONITOR:
Aviya Erenfeldd8212182016-08-29 23:25:15 +03001716 if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
Johannes Bergc82b5a72013-07-14 23:39:20 +03001717 vif = &sdata->vif;
1718 break;
1719 }
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001720 sdata = rcu_dereference(local->monitor_sdata);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001721 if (sdata) {
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001722 vif = &sdata->vif;
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001723 info->hw_queue =
1724 vif->hw_queue[skb_get_queue_mapping(skb)];
Johannes Berg30686bf2015-06-02 21:39:54 +02001725 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
Johannes Berg63b4d8b2015-11-24 15:41:50 +01001726 ieee80211_purge_tx_queue(&local->hw, skbs);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001727 return true;
1728 } else
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001729 vif = NULL;
Johannes Berg11127e92011-11-16 16:02:47 +01001730 break;
1731 case NL80211_IFTYPE_AP_VLAN:
1732 sdata = container_of(sdata->bss,
1733 struct ieee80211_sub_if_data, u.ap);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05001734 fallthrough;
Johannes Berg11127e92011-11-16 16:02:47 +01001735 default:
1736 vif = &sdata->vif;
1737 break;
1738 }
1739
Johannes Berg4fd03282019-10-01 23:26:35 +02001740 result = ieee80211_tx_frags(local, vif, sta, skbs, txpending);
Johannes Berg11127e92011-11-16 16:02:47 +01001741
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001742 ieee80211_tpt_led_trig_tx(local, fc, led_len);
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001743
Johannes Berg4db4e0a2011-11-24 14:47:36 +01001744 WARN_ON_ONCE(!skb_queue_empty(skbs));
Johannes Berg252b86c2011-11-16 15:28:55 +01001745
Johannes Berg11127e92011-11-16 16:02:47 +01001746 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001747}
1748
Johannes Berg97b045d2008-06-20 01:22:30 +02001749/*
1750 * Invoke TX handlers, return 0 on success and non-zero if the
1751 * frame was dropped or queued.
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001752 *
1753 * The handlers are split into an early and late part. The latter is everything
1754 * that can be sensitive to reordering, and will be deferred to after packets
1755 * are dequeued from the intermediate queues (when they are enabled).
Johannes Berg97b045d2008-06-20 01:22:30 +02001756 */
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001757static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx)
Johannes Berg97b045d2008-06-20 01:22:30 +02001758{
Johannes Berg97b045d2008-06-20 01:22:30 +02001759 ieee80211_tx_result res = TX_DROP;
Johannes Berg97b045d2008-06-20 01:22:30 +02001760
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001761#define CALL_TXH(txh) \
1762 do { \
1763 res = txh(tx); \
1764 if (res != TX_CONTINUE) \
1765 goto txh_done; \
1766 } while (0)
Johannes Berg97b045d2008-06-20 01:22:30 +02001767
Kalle Valo5c1b98a2010-01-12 10:42:46 +02001768 CALL_TXH(ieee80211_tx_h_dynamic_ps);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001769 CALL_TXH(ieee80211_tx_h_check_assoc);
1770 CALL_TXH(ieee80211_tx_h_ps_buf);
Johannes Berga621fa42010-08-27 14:26:54 +03001771 CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001772 CALL_TXH(ieee80211_tx_h_select_key);
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001773
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001774 txh_done:
1775 if (unlikely(res == TX_DROP)) {
1776 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1777 if (tx->skb)
1778 ieee80211_free_txskb(&tx->local->hw, tx->skb);
1779 else
1780 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1781 return -1;
1782 } else if (unlikely(res == TX_QUEUED)) {
1783 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1784 return -1;
1785 }
1786
1787 return 0;
1788}
1789
1790/*
1791 * Late handlers can be called while the sta lock is held. Handlers that can
1792 * cause packets to be generated will cause deadlock!
1793 */
1794static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx)
1795{
1796 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1797 ieee80211_tx_result res = TX_CONTINUE;
1798
Johannes Bergaa5b5492011-12-02 22:08:52 +01001799 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1800 __skb_queue_tail(&tx->skbs, tx->skb);
1801 tx->skb = NULL;
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001802 goto txh_done;
Johannes Bergaa5b5492011-12-02 22:08:52 +01001803 }
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001804
Ryder Lee03c39112021-06-17 18:31:12 +02001805 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1806 CALL_TXH(ieee80211_tx_h_rate_ctrl);
1807
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001808 CALL_TXH(ieee80211_tx_h_michael_mic_add);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001809 CALL_TXH(ieee80211_tx_h_sequence);
1810 CALL_TXH(ieee80211_tx_h_fragment);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001811 /* handlers after fragment must be aware of tx info fragmentation! */
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001812 CALL_TXH(ieee80211_tx_h_stats);
1813 CALL_TXH(ieee80211_tx_h_encrypt);
Johannes Berg30686bf2015-06-02 21:39:54 +02001814 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
Arik Nemtsov8fd369e2011-01-31 22:29:12 +02001815 CALL_TXH(ieee80211_tx_h_calculate_duration);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001816#undef CALL_TXH
1817
1818 txh_done:
Johannes Berg97b045d2008-06-20 01:22:30 +02001819 if (unlikely(res == TX_DROP)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001820 I802_DEBUG_INC(tx->local->tx_handlers_drop);
Johannes Berg252b86c2011-11-16 15:28:55 +01001821 if (tx->skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001822 ieee80211_free_txskb(&tx->local->hw, tx->skb);
Johannes Berg252b86c2011-11-16 15:28:55 +01001823 else
Felix Fietkau1f98ab72012-11-10 03:44:14 +01001824 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
Johannes Berg97b045d2008-06-20 01:22:30 +02001825 return -1;
1826 } else if (unlikely(res == TX_QUEUED)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001827 I802_DEBUG_INC(tx->local->tx_handlers_queued);
Johannes Berg97b045d2008-06-20 01:22:30 +02001828 return -1;
1829 }
1830
1831 return 0;
1832}
1833
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001834static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1835{
1836 int r = invoke_tx_handlers_early(tx);
1837
1838 if (r)
1839 return r;
1840 return invoke_tx_handlers_late(tx);
1841}
1842
Felix Fietkau06be6b12013-10-14 18:01:00 +02001843bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1844 struct ieee80211_vif *vif, struct sk_buff *skb,
1845 int band, struct ieee80211_sta **sta)
1846{
1847 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1848 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1849 struct ieee80211_tx_data tx;
Johannes Berg88724a82015-03-01 09:10:12 +02001850 struct sk_buff *skb2;
Felix Fietkau06be6b12013-10-14 18:01:00 +02001851
Johannes Berg7c107702015-03-20 14:18:27 +01001852 if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
Felix Fietkau06be6b12013-10-14 18:01:00 +02001853 return false;
1854
1855 info->band = band;
1856 info->control.vif = vif;
1857 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1858
1859 if (invoke_tx_handlers(&tx))
1860 return false;
1861
1862 if (sta) {
1863 if (tx.sta)
1864 *sta = &tx.sta->sta;
1865 else
1866 *sta = NULL;
1867 }
1868
Johannes Berg88724a82015-03-01 09:10:12 +02001869 /* this function isn't suitable for fragmented data frames */
1870 skb2 = __skb_dequeue(&tx.skbs);
1871 if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1872 ieee80211_free_txskb(hw, skb2);
1873 ieee80211_purge_tx_queue(hw, &tx.skbs);
1874 return false;
1875 }
1876
Felix Fietkau06be6b12013-10-14 18:01:00 +02001877 return true;
1878}
1879EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1880
Johannes Berg7bb45682011-02-24 14:42:06 +01001881/*
1882 * Returns false if the frame couldn't be transmitted but was queued instead.
1883 */
1884static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01001885 struct sta_info *sta, struct sk_buff *skb,
Mathy Vanhoef08aca292020-07-23 14:01:52 +04001886 bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001887{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001888 struct ieee80211_local *local = sdata->local;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001889 struct ieee80211_tx_data tx;
Johannes Berg97b045d2008-06-20 01:22:30 +02001890 ieee80211_tx_result res_prepare;
Johannes Berge039fa42008-05-15 12:55:29 +02001891 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001892 bool result = true;
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001893 int led_len;
Johannes Berge2ebc742007-07-27 15:43:22 +02001894
Johannes Berge2ebc742007-07-27 15:43:22 +02001895 if (unlikely(skb->len < 10)) {
1896 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001897 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001898 }
1899
Johannes Berg58d41852007-09-26 17:53:18 +02001900 /* initialises tx */
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001901 led_len = skb->len;
Johannes Berg7c107702015-03-20 14:18:27 +01001902 res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001903
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001904 if (unlikely(res_prepare == TX_DROP)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02001905 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001906 return true;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001907 } else if (unlikely(res_prepare == TX_QUEUED)) {
Johannes Berg55de9082012-07-26 17:24:39 +02001908 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001909 }
1910
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001911 /* set up hw_queue value early */
1912 if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
Johannes Berg30686bf2015-06-02 21:39:54 +02001913 !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001914 info->hw_queue =
1915 sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1916
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001917 if (invoke_tx_handlers_early(&tx))
Bob Copeland6eae4a62018-09-05 06:22:59 -04001918 return true;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001919
1920 if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb))
1921 return true;
1922
1923 if (!invoke_tx_handlers_late(&tx))
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001924 result = __ieee80211_tx(local, &tx.skbs, led_len,
1925 tx.sta, txpending);
Johannes Berg55de9082012-07-26 17:24:39 +02001926
Johannes Berg7bb45682011-02-24 14:42:06 +01001927 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001928}
1929
1930/* device xmit handlers */
1931
Johannes Berg14f46c12020-10-09 13:25:41 +02001932enum ieee80211_encrypt {
1933 ENCRYPT_NO,
1934 ENCRYPT_MGMT,
1935 ENCRYPT_DATA,
1936};
1937
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301938static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
Johannes Berg23c07522008-05-29 10:38:53 +02001939 struct sk_buff *skb,
Johannes Berg14f46c12020-10-09 13:25:41 +02001940 int head_need,
1941 enum ieee80211_encrypt encrypt)
Johannes Berg23c07522008-05-29 10:38:53 +02001942{
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301943 struct ieee80211_local *local = sdata->local;
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001944 bool enc_tailroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001945 int tail_need = 0;
1946
Johannes Berg14f46c12020-10-09 13:25:41 +02001947 enc_tailroom = encrypt == ENCRYPT_MGMT ||
1948 (encrypt == ENCRYPT_DATA &&
1949 sdata->crypto_tx_tailroom_needed_cnt);
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001950
1951 if (enc_tailroom) {
Johannes Berg23c07522008-05-29 10:38:53 +02001952 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1953 tail_need -= skb_tailroom(skb);
1954 tail_need = max_t(int, tail_need, 0);
1955 }
1956
Ido Yarivc70f59a2014-07-29 15:39:14 +03001957 if (skb_cloned(skb) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001958 (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001959 !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
Johannes Berg23c07522008-05-29 10:38:53 +02001960 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001961 else if (head_need || tail_need)
Johannes Berg23c07522008-05-29 10:38:53 +02001962 I802_DEBUG_INC(local->tx_expand_skb_head);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001963 else
1964 return 0;
Johannes Berg23c07522008-05-29 10:38:53 +02001965
1966 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
Joe Perches0fb9a9e2010-08-20 16:25:38 -07001967 wiphy_debug(local->hw.wiphy,
1968 "failed to reallocate TX buffer\n");
Johannes Berg23c07522008-05-29 10:38:53 +02001969 return -ENOMEM;
1970 }
1971
Johannes Berg23c07522008-05-29 10:38:53 +02001972 return 0;
1973}
1974
Johannes Berg7c107702015-03-20 14:18:27 +01001975void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
Mathy Vanhoef08aca292020-07-23 14:01:52 +04001976 struct sta_info *sta, struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02001977{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001978 struct ieee80211_local *local = sdata->local;
Johannes Berge039fa42008-05-15 12:55:29 +02001979 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg14f46c12020-10-09 13:25:41 +02001980 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berge2ebc742007-07-27 15:43:22 +02001981 int headroom;
Johannes Berg14f46c12020-10-09 13:25:41 +02001982 enum ieee80211_encrypt encrypt;
Johannes Berge2ebc742007-07-27 15:43:22 +02001983
Johannes Berg14f46c12020-10-09 13:25:41 +02001984 if (info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)
1985 encrypt = ENCRYPT_NO;
1986 else if (ieee80211_is_mgmt(hdr->frame_control))
1987 encrypt = ENCRYPT_MGMT;
1988 else
1989 encrypt = ENCRYPT_DATA;
Johannes Berg23c07522008-05-29 10:38:53 +02001990
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001991 headroom = local->tx_headroom;
Johannes Berg14f46c12020-10-09 13:25:41 +02001992 if (encrypt != ENCRYPT_NO)
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001993 headroom += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001994 headroom -= skb_headroom(skb);
1995 headroom = max_t(int, 0, headroom);
1996
Johannes Berg14f46c12020-10-09 13:25:41 +02001997 if (ieee80211_skb_resize(sdata, skb, headroom, encrypt)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02001998 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001999 return;
Johannes Berge2ebc742007-07-27 15:43:22 +02002000 }
2001
Johannes Berg14f46c12020-10-09 13:25:41 +02002002 /* reload after potential resize */
Steve deRosier740c1aa2010-09-11 20:01:31 -07002003 hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berg5061b0c2009-07-14 00:33:34 +02002004 info->control.vif = &sdata->vif;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01002005
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002006 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2007 if (ieee80211_is_data(hdr->frame_control) &&
2008 is_unicast_ether_addr(hdr->addr1)) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002009 if (mesh_nexthop_resolve(sdata, skb))
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002010 return; /* skb queued: don't free */
2011 } else {
2012 ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
2013 }
Johannes Berg98aed9f2012-03-27 14:18:36 +02002014 }
Javier Cardonacca89492009-08-10 17:29:29 -07002015
Javier Cardona2154c81c2011-09-07 17:49:53 -07002016 ieee80211_set_qos_hdr(sdata, skb);
Mathy Vanhoef08aca292020-07-23 14:01:52 +04002017 ieee80211_tx(sdata, sta, skb, false);
Johannes Berge2ebc742007-07-27 15:43:22 +02002018}
2019
Mathy Vanhoefbddc0c42021-05-30 15:32:26 +02002020static bool ieee80211_validate_radiotap_len(struct sk_buff *skb)
Johannes Berg73b9f032011-10-07 14:01:26 +02002021{
Johannes Berg73b9f032011-10-07 14:01:26 +02002022 struct ieee80211_radiotap_header *rthdr =
Mathy Vanhoefbddc0c42021-05-30 15:32:26 +02002023 (struct ieee80211_radiotap_header *)skb->data;
Johannes Berg73b9f032011-10-07 14:01:26 +02002024
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002025 /* check for not even having the fixed radiotap header part */
2026 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2027 return false; /* too short to be possibly valid */
2028
2029 /* is it a header version we can trust to find length from? */
2030 if (unlikely(rthdr->it_version))
2031 return false; /* only version 0 is supported */
2032
2033 /* does the skb contain enough to deliver on the alleged length? */
2034 if (unlikely(skb->len < ieee80211_get_radiotap_len(skb->data)))
2035 return false; /* skb too short for claimed rt header extent */
2036
Mathy Vanhoefbddc0c42021-05-30 15:32:26 +02002037 return true;
2038}
2039
2040bool ieee80211_parse_tx_radiotap(struct sk_buff *skb,
2041 struct net_device *dev)
2042{
2043 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2044 struct ieee80211_radiotap_iterator iterator;
2045 struct ieee80211_radiotap_header *rthdr =
2046 (struct ieee80211_radiotap_header *) skb->data;
2047 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2048 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
2049 NULL);
2050 u16 txflags;
2051 u16 rate = 0;
2052 bool rate_found = false;
2053 u8 rate_retries = 0;
2054 u16 rate_flags = 0;
2055 u8 mcs_known, mcs_flags, mcs_bw;
2056 u16 vht_known;
2057 u8 vht_mcs = 0, vht_nss = 0;
2058 int i;
2059
2060 if (!ieee80211_validate_radiotap_len(skb))
2061 return false;
2062
Johannes Berg73b9f032011-10-07 14:01:26 +02002063 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
2064 IEEE80211_TX_CTL_DONTFRAG;
2065
2066 /*
2067 * for every radiotap entry that is present
2068 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
2069 * entries present, or -EINVAL on error)
2070 */
2071
2072 while (!ret) {
2073 ret = ieee80211_radiotap_iterator_next(&iterator);
2074
2075 if (ret)
2076 continue;
2077
2078 /* see if this argument is something we can use */
2079 switch (iterator.this_arg_index) {
2080 /*
2081 * You must take care when dereferencing iterator.this_arg
2082 * for multibyte types... the pointer is not aligned. Use
2083 * get_unaligned((type *)iterator.this_arg) to dereference
2084 * iterator.this_arg for type "type" safely on all arches.
2085 */
2086 case IEEE80211_RADIOTAP_FLAGS:
2087 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
2088 /*
2089 * this indicates that the skb we have been
2090 * handed has the 32-bit FCS CRC at the end...
2091 * we should react to that by snipping it off
2092 * because it will be recomputed and added
2093 * on transmission
2094 */
2095 if (skb->len < (iterator._max_length + FCS_LEN))
2096 return false;
2097
2098 skb_trim(skb, skb->len - FCS_LEN);
2099 }
2100 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
2101 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
2102 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
2103 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
2104 break;
2105
2106 case IEEE80211_RADIOTAP_TX_FLAGS:
2107 txflags = get_unaligned_le16(iterator.this_arg);
2108 if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
2109 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Mathy Vanhoefe02281e2020-07-23 14:01:49 +04002110 if (txflags & IEEE80211_RADIOTAP_F_TX_NOSEQNO)
2111 info->control.flags |= IEEE80211_TX_CTRL_NO_SEQNO;
Mathy Vanhoef30df8132020-11-04 10:18:19 +04002112 if (txflags & IEEE80211_RADIOTAP_F_TX_ORDER)
2113 info->control.flags |=
2114 IEEE80211_TX_CTRL_DONT_REORDER;
Johannes Berg73b9f032011-10-07 14:01:26 +02002115 break;
2116
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002117 case IEEE80211_RADIOTAP_RATE:
2118 rate = *iterator.this_arg;
2119 rate_flags = 0;
2120 rate_found = true;
2121 break;
2122
2123 case IEEE80211_RADIOTAP_DATA_RETRIES:
2124 rate_retries = *iterator.this_arg;
2125 break;
2126
2127 case IEEE80211_RADIOTAP_MCS:
2128 mcs_known = iterator.this_arg[0];
2129 mcs_flags = iterator.this_arg[1];
2130 if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
2131 break;
2132
2133 rate_found = true;
2134 rate = iterator.this_arg[2];
2135 rate_flags = IEEE80211_TX_RC_MCS;
2136
2137 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
2138 mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
2139 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2140
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002141 mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002142 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002143 mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002144 rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
Philipp Borgersf1864e12020-12-19 18:07:10 +01002145
2146 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_FEC &&
2147 mcs_flags & IEEE80211_RADIOTAP_MCS_FEC_LDPC)
2148 info->flags |= IEEE80211_TX_CTL_LDPC;
Philipp Borgers549fdd32021-01-25 16:07:44 +01002149
2150 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_STBC) {
2151 u8 stbc = u8_get_bits(mcs_flags,
2152 IEEE80211_RADIOTAP_MCS_STBC_MASK);
2153
2154 info->flags |=
2155 u32_encode_bits(stbc,
2156 IEEE80211_TX_CTL_STBC);
2157 }
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002158 break;
2159
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002160 case IEEE80211_RADIOTAP_VHT:
2161 vht_known = get_unaligned_le16(iterator.this_arg);
2162 rate_found = true;
2163
2164 rate_flags = IEEE80211_TX_RC_VHT_MCS;
2165 if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
2166 (iterator.this_arg[2] &
2167 IEEE80211_RADIOTAP_VHT_FLAG_SGI))
2168 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2169 if (vht_known &
2170 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
2171 if (iterator.this_arg[3] == 1)
2172 rate_flags |=
2173 IEEE80211_TX_RC_40_MHZ_WIDTH;
2174 else if (iterator.this_arg[3] == 4)
2175 rate_flags |=
2176 IEEE80211_TX_RC_80_MHZ_WIDTH;
2177 else if (iterator.this_arg[3] == 11)
2178 rate_flags |=
2179 IEEE80211_TX_RC_160_MHZ_WIDTH;
2180 }
2181
2182 vht_mcs = iterator.this_arg[4] >> 4;
2183 vht_nss = iterator.this_arg[4] & 0xF;
2184 break;
2185
Johannes Berg73b9f032011-10-07 14:01:26 +02002186 /*
2187 * Please update the file
Mauro Carvalho Chehab429ff872020-04-30 18:03:59 +02002188 * Documentation/networking/mac80211-injection.rst
Johannes Berg73b9f032011-10-07 14:01:26 +02002189 * when parsing new fields here.
2190 */
2191
2192 default:
2193 break;
2194 }
2195 }
2196
2197 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
2198 return false;
2199
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002200 if (rate_found) {
Mathy Vanhoefbddc0c42021-05-30 15:32:26 +02002201 struct ieee80211_supported_band *sband =
2202 local->hw.wiphy->bands[info->band];
2203
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002204 info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
2205
2206 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2207 info->control.rates[i].idx = -1;
2208 info->control.rates[i].flags = 0;
2209 info->control.rates[i].count = 0;
2210 }
2211
2212 if (rate_flags & IEEE80211_TX_RC_MCS) {
2213 info->control.rates[0].idx = rate;
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002214 } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
2215 ieee80211_rate_set_vht(info->control.rates, vht_mcs,
2216 vht_nss);
Mathy Vanhoefbddc0c42021-05-30 15:32:26 +02002217 } else if (sband) {
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002218 for (i = 0; i < sband->n_bitrates; i++) {
2219 if (rate * 5 != sband->bitrates[i].bitrate)
2220 continue;
2221
2222 info->control.rates[0].idx = i;
2223 break;
2224 }
2225 }
2226
Felix Fietkau07310a62016-03-02 15:54:51 +01002227 if (info->control.rates[0].idx < 0)
2228 info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
2229
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002230 info->control.rates[0].flags = rate_flags;
2231 info->control.rates[0].count = min_t(u8, rate_retries + 1,
2232 local->hw.max_rate_tries);
2233 }
2234
Johannes Berg73b9f032011-10-07 14:01:26 +02002235 return true;
2236}
2237
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +00002238netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
2239 struct net_device *dev)
Johannes Berge2ebc742007-07-27 15:43:22 +02002240{
2241 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg55de9082012-07-26 17:24:39 +02002242 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002243 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002244 struct ieee80211_hdr *hdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002245 struct ieee80211_sub_if_data *tmp_sdata, *sdata;
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002246 struct cfg80211_chan_def *chandef;
Andy Green9b8a74e2007-07-27 15:43:24 +02002247 u16 len_rthdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002248 int hdrlen;
Johannes Berge2ebc742007-07-27 15:43:22 +02002249
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002250 memset(info, 0, sizeof(*info));
2251 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2252 IEEE80211_TX_CTL_INJECTED;
Andy Green9b8a74e2007-07-27 15:43:24 +02002253
Mathy Vanhoefbddc0c42021-05-30 15:32:26 +02002254 /* Sanity-check the length of the radiotap header */
2255 if (!ieee80211_validate_radiotap_len(skb))
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002256 goto fail;
Andy Green9b8a74e2007-07-27 15:43:24 +02002257
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002258 /* we now know there is a radiotap header with a length we can use */
Andy Green9b8a74e2007-07-27 15:43:24 +02002259 len_rthdr = ieee80211_get_radiotap_len(skb->data);
2260
Johannes Berge2ebc742007-07-27 15:43:22 +02002261 /*
2262 * fix up the pointers accounting for the radiotap
2263 * header still being in there. We are being given
2264 * a precooked IEEE80211 header so no need for
2265 * normal processing
2266 */
Andy Green9b8a74e2007-07-27 15:43:24 +02002267 skb_set_mac_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02002268 /*
Andy Green9b8a74e2007-07-27 15:43:24 +02002269 * these are just fixed to the end of the rt area since we
2270 * don't have any better information and at this point, nobody cares
Johannes Berge2ebc742007-07-27 15:43:22 +02002271 */
Andy Green9b8a74e2007-07-27 15:43:24 +02002272 skb_set_network_header(skb, len_rthdr);
2273 skb_set_transport_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02002274
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002275 if (skb->len < len_rthdr + 2)
2276 goto fail;
2277
2278 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2279 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2280
2281 if (skb->len < len_rthdr + hdrlen)
2282 goto fail;
2283
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002284 /*
2285 * Initialize skb->protocol if the injected frame is a data frame
2286 * carrying a rfc1042 header
2287 */
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002288 if (ieee80211_is_data(hdr->frame_control) &&
2289 skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2290 u8 *payload = (u8 *)hdr + hdrlen;
2291
Joe Perchesb203ca32012-05-08 18:56:52 +00002292 if (ether_addr_equal(payload, rfc1042_header))
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002293 skb->protocol = cpu_to_be16((payload[6] << 8) |
2294 payload[7]);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002295 }
2296
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002297 rcu_read_lock();
2298
2299 /*
2300 * We process outgoing injected frames that have a local address
2301 * we handle as though they are non-injected frames.
2302 * This code here isn't entirely correct, the local MAC address
2303 * isn't always enough to find the interface to use; for proper
Johannes Berg70d9c592020-11-09 10:57:46 +01002304 * VLAN support we have an nl80211-based mechanism.
Johannes Bergb226a822019-11-22 12:42:42 +01002305 *
2306 * This is necessary, for example, for old hostapd versions that
2307 * don't use nl80211-based management TX/RX.
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002308 */
2309 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2310
2311 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2312 if (!ieee80211_sdata_running(tmp_sdata))
2313 continue;
2314 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
Johannes Berg70d9c592020-11-09 10:57:46 +01002315 tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002316 continue;
Joe Perchesb203ca32012-05-08 18:56:52 +00002317 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002318 sdata = tmp_sdata;
2319 break;
2320 }
2321 }
Johannes Berg7351c6b2009-11-19 01:08:30 +01002322
Johannes Berg55de9082012-07-26 17:24:39 +02002323 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2324 if (!chanctx_conf) {
2325 tmp_sdata = rcu_dereference(local->monitor_sdata);
2326 if (tmp_sdata)
2327 chanctx_conf =
2328 rcu_dereference(tmp_sdata->vif.chanctx_conf);
2329 }
Johannes Berg55de9082012-07-26 17:24:39 +02002330
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002331 if (chanctx_conf)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002332 chandef = &chanctx_conf->def;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002333 else if (!local->use_chanctx)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002334 chandef = &local->_oper_chandef;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002335 else
2336 goto fail_rcu;
Johannes Berg55de9082012-07-26 17:24:39 +02002337
2338 /*
2339 * Frame injection is not allowed if beaconing is not allowed
2340 * or if we need radar detection. Beaconing is usually not allowed when
2341 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2342 * Passive scan is also used in world regulatory domains where
2343 * your country is not known and as such it should be treated as
2344 * NO TX unless the channel is explicitly allowed in which case
2345 * your current regulatory domain would not have the passive scan
2346 * flag.
2347 *
2348 * Since AP mode uses monitor interfaces to inject/TX management
2349 * frames we can make AP mode the exception to this rule once it
2350 * supports radar detection as its implementation can deal with
2351 * radar detection by itself. We can do that later by adding a
2352 * monitor flag interfaces used for AP support.
2353 */
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002354 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2355 sdata->vif.type))
Johannes Berg55de9082012-07-26 17:24:39 +02002356 goto fail_rcu;
2357
Johannes Berg73c4e192014-11-09 18:50:09 +02002358 info->band = chandef->chan->band;
Lorenzo Bianconi109843b02016-02-19 12:18:01 +01002359
Johan Almbladh96a71092021-04-01 18:44:55 +02002360 /* Initialize skb->priority according to frame type and TID class,
2361 * with respect to the sub interface that the frame will actually
2362 * be transmitted on. If the DONT_REORDER flag is set, the original
2363 * skb-priority is preserved to assure frames injected with this
2364 * flag are not reordered relative to each other.
2365 */
2366 ieee80211_select_queue_80211(sdata, skb, hdr);
2367 skb_set_queue_mapping(skb, ieee80211_ac_from_tid(skb->priority));
2368
Mathy Vanhoefbddc0c42021-05-30 15:32:26 +02002369 /*
2370 * Process the radiotap header. This will now take into account the
2371 * selected chandef above to accurately set injection rates and
2372 * retransmissions.
2373 */
2374 if (!ieee80211_parse_tx_radiotap(skb, dev))
2375 goto fail_rcu;
2376
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002377 /* remove the injection radiotap header */
2378 skb_pull(skb, len_rthdr);
Lorenzo Bianconi109843b02016-02-19 12:18:01 +01002379
Mathy Vanhoef08aca292020-07-23 14:01:52 +04002380 ieee80211_xmit(sdata, NULL, skb);
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002381 rcu_read_unlock();
2382
Johannes Berge2ebc742007-07-27 15:43:22 +02002383 return NETDEV_TX_OK;
Andy Green9b8a74e2007-07-27 15:43:24 +02002384
Johannes Berg55de9082012-07-26 17:24:39 +02002385fail_rcu:
2386 rcu_read_unlock();
Andy Green9b8a74e2007-07-27 15:43:24 +02002387fail:
2388 dev_kfree_skb(skb);
2389 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
Johannes Berge2ebc742007-07-27 15:43:22 +02002390}
2391
Johannes Berg97ffe752015-03-21 09:13:45 +01002392static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002393{
Johannes Berg97ffe752015-03-21 09:13:45 +01002394 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002395
Johannes Berg97ffe752015-03-21 09:13:45 +01002396 return ethertype == ETH_P_TDLS &&
2397 skb->len > 14 &&
2398 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2399}
2400
John Crispin50ff4772019-11-25 11:04:37 +01002401int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2402 struct sk_buff *skb,
2403 struct sta_info **sta_out)
Johannes Berg97ffe752015-03-21 09:13:45 +01002404{
2405 struct sta_info *sta;
2406
2407 switch (sdata->vif.type) {
2408 case NL80211_IFTYPE_AP_VLAN:
2409 sta = rcu_dereference(sdata->u.vlan.sta);
2410 if (sta) {
2411 *sta_out = sta;
2412 return 0;
2413 } else if (sdata->wdev.use_4addr) {
2414 return -ENOLINK;
2415 }
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05002416 fallthrough;
Johannes Berg97ffe752015-03-21 09:13:45 +01002417 case NL80211_IFTYPE_AP:
2418 case NL80211_IFTYPE_OCB:
2419 case NL80211_IFTYPE_ADHOC:
2420 if (is_multicast_ether_addr(skb->data)) {
2421 *sta_out = ERR_PTR(-ENOENT);
2422 return 0;
2423 }
2424 sta = sta_info_get_bss(sdata, skb->data);
2425 break;
Johannes Berg97ffe752015-03-21 09:13:45 +01002426#ifdef CONFIG_MAC80211_MESH
2427 case NL80211_IFTYPE_MESH_POINT:
2428 /* determined much later */
2429 *sta_out = NULL;
2430 return 0;
2431#endif
2432 case NL80211_IFTYPE_STATION:
2433 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2434 sta = sta_info_get(sdata, skb->data);
Johannes Berg11d62ca2016-09-13 08:28:22 +02002435 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2436 if (test_sta_flag(sta,
2437 WLAN_STA_TDLS_PEER_AUTH)) {
Johannes Berg97ffe752015-03-21 09:13:45 +01002438 *sta_out = sta;
2439 return 0;
2440 }
2441
2442 /*
2443 * TDLS link during setup - throw out frames to
2444 * peer. Allow TDLS-setup frames to unauthorized
2445 * peers for the special case of a link teardown
2446 * after a TDLS sta is removed due to being
2447 * unreachable.
2448 */
Johannes Berg11d62ca2016-09-13 08:28:22 +02002449 if (!ieee80211_is_tdls_setup(skb))
Johannes Berg97ffe752015-03-21 09:13:45 +01002450 return -EINVAL;
2451 }
2452
2453 }
2454
2455 sta = sta_info_get(sdata, sdata->u.mgd.bssid);
2456 if (!sta)
2457 return -ENOLINK;
2458 break;
2459 default:
2460 return -EINVAL;
2461 }
2462
2463 *sta_out = sta ?: ERR_PTR(-ENOENT);
2464 return 0;
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002465}
2466
Markus Theila7528192020-05-27 18:03:34 +02002467static u16 ieee80211_store_ack_skb(struct ieee80211_local *local,
John Crispin5d8983c2019-10-29 10:13:02 +01002468 struct sk_buff *skb,
Markus Theila7528192020-05-27 18:03:34 +02002469 u32 *info_flags,
2470 u64 *cookie)
John Crispin5d8983c2019-10-29 10:13:02 +01002471{
Markus Theila7528192020-05-27 18:03:34 +02002472 struct sk_buff *ack_skb;
John Crispin5d8983c2019-10-29 10:13:02 +01002473 u16 info_id = 0;
2474
Markus Theila7528192020-05-27 18:03:34 +02002475 if (skb->sk)
2476 ack_skb = skb_clone_sk(skb);
2477 else
2478 ack_skb = skb_clone(skb, GFP_ATOMIC);
2479
John Crispin5d8983c2019-10-29 10:13:02 +01002480 if (ack_skb) {
2481 unsigned long flags;
2482 int id;
2483
2484 spin_lock_irqsave(&local->ack_status_lock, flags);
2485 id = idr_alloc(&local->ack_status_frames, ack_skb,
Johannes Bergf2b18ba2020-01-15 12:25:50 +01002486 1, 0x2000, GFP_ATOMIC);
John Crispin5d8983c2019-10-29 10:13:02 +01002487 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2488
2489 if (id >= 0) {
2490 info_id = id;
2491 *info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
Markus Theila7528192020-05-27 18:03:34 +02002492 if (cookie) {
2493 *cookie = ieee80211_mgmt_tx_cookie(local);
2494 IEEE80211_SKB_CB(ack_skb)->ack.cookie = *cookie;
2495 }
John Crispin5d8983c2019-10-29 10:13:02 +01002496 } else {
2497 kfree_skb(ack_skb);
2498 }
2499 }
2500
2501 return info_id;
2502}
2503
Johannes Berge2ebc742007-07-27 15:43:22 +02002504/**
Johannes Berg4c9451e2014-11-09 18:50:10 +02002505 * ieee80211_build_hdr - build 802.11 header in the given frame
2506 * @sdata: virtual interface to build the header for
2507 * @skb: the skb to build the header in
Liad Kaufman24d342c2014-11-09 18:50:07 +02002508 * @info_flags: skb flags to set
Johannes Bergf0daf542020-09-24 19:25:11 +02002509 * @sta: the station pointer
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002510 * @ctrl_flags: info control flags to set
Johannes Bergf0daf542020-09-24 19:25:11 +02002511 * @cookie: cookie pointer to fill (if not %NULL)
Johannes Berge2ebc742007-07-27 15:43:22 +02002512 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002513 * This function takes the skb with 802.3 header and reformats the header to
2514 * the appropriate IEEE 802.11 header based on which interface the packet is
2515 * being transmitted on.
Johannes Berge2ebc742007-07-27 15:43:22 +02002516 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002517 * Note that this function also takes care of the TX status request and
2518 * potential unsharing of the SKB - this needs to be interleaved with the
2519 * header building.
2520 *
2521 * The function requires the read-side RCU lock held
2522 *
2523 * Returns: the (possibly reallocated) skb or an ERR_PTR() code
Johannes Berge2ebc742007-07-27 15:43:22 +02002524 */
Johannes Berg4c9451e2014-11-09 18:50:10 +02002525static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01002526 struct sk_buff *skb, u32 info_flags,
Markus Theila7528192020-05-27 18:03:34 +02002527 struct sta_info *sta, u32 ctrl_flags,
2528 u64 *cookie)
Johannes Berge2ebc742007-07-27 15:43:22 +02002529{
Johannes Berg133b8222008-09-16 14:18:59 +02002530 struct ieee80211_local *local = sdata->local;
Felix Fietkau489ee912010-12-18 19:30:48 +01002531 struct ieee80211_tx_info *info;
Johannes Bergdcf33962012-07-30 15:11:56 +02002532 int head_need;
Harvey Harrison065e96052008-06-22 16:45:27 -07002533 u16 ethertype, hdrlen, meshhdrlen = 0;
2534 __le16 fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002535 struct ieee80211_hdr hdr;
Wey-Yi Guyff67bb82010-08-21 07:23:29 -07002536 struct ieee80211s_hdr mesh_hdr __maybe_unused;
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002537 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02002538 const u8 *encaps_data;
2539 int encaps_len, skip_header_bytes;
Johannes Berg97ffe752015-03-21 09:13:45 +01002540 bool wme_sta = false, authorized = false;
2541 bool tdls_peer;
Johannes Berga729cff2011-11-06 14:13:34 +01002542 bool multicast;
Johannes Berga729cff2011-11-06 14:13:34 +01002543 u16 info_id = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002544 struct ieee80211_chanctx_conf *chanctx_conf;
2545 struct ieee80211_sub_if_data *ap_sdata;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002546 enum nl80211_band band;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002547 int ret;
Johannes Berge2ebc742007-07-27 15:43:22 +02002548
Johannes Berg97ffe752015-03-21 09:13:45 +01002549 if (IS_ERR(sta))
2550 sta = NULL;
2551
Julius Niedworok276d9e82019-03-28 21:01:06 +01002552#ifdef CONFIG_MAC80211_DEBUGFS
2553 if (local->force_tx_status)
2554 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2555#endif
2556
Johannes Berge2ebc742007-07-27 15:43:22 +02002557 /* convert Ethernet header to proper 802.11 header (based on
2558 * operation mode) */
2559 ethertype = (skb->data[12] << 8) | skb->data[13];
Harvey Harrison065e96052008-06-22 16:45:27 -07002560 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
Johannes Berge2ebc742007-07-27 15:43:22 +02002561
Johannes Berg51fb61e2007-12-19 01:31:27 +01002562 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002563 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg97ffe752015-03-21 09:13:45 +01002564 if (sdata->wdev.use_4addr) {
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002565 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2566 /* RA TA DA SA */
2567 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002568 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002569 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2570 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2571 hdrlen = 30;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002572 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Johannes Berga74a8c82014-07-22 14:50:47 +02002573 wme_sta = sta->sta.wme;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002574 }
Johannes Berg55de9082012-07-26 17:24:39 +02002575 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2576 u.ap);
2577 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002578 if (!chanctx_conf) {
2579 ret = -ENOTCONN;
2580 goto free;
2581 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002582 band = chanctx_conf->def.chan->band;
Johannes Berg97ffe752015-03-21 09:13:45 +01002583 if (sdata->wdev.use_4addr)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002584 break;
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05002585 fallthrough;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002586 case NL80211_IFTYPE_AP:
Arnd Bergmannfe801232013-01-25 14:14:33 +00002587 if (sdata->vif.type == NL80211_IFTYPE_AP)
2588 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002589 if (!chanctx_conf) {
2590 ret = -ENOTCONN;
2591 goto free;
2592 }
Harvey Harrison065e96052008-06-22 16:45:27 -07002593 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
Johannes Berge2ebc742007-07-27 15:43:22 +02002594 /* DA BSSID SA */
2595 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002596 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002597 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2598 hdrlen = 24;
Johannes Berg4bf88532012-11-09 11:39:59 +01002599 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002600 break;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002601#ifdef CONFIG_MAC80211_MESH
Johannes Berg05c914f2008-09-11 00:01:58 +02002602 case NL80211_IFTYPE_MESH_POINT:
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002603 if (!is_multicast_ether_addr(skb->data)) {
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002604 struct sta_info *next_hop;
2605 bool mpp_lookup = true;
2606
Johannes Bergbf7cd942013-02-15 14:40:31 +01002607 mpath = mesh_path_lookup(sdata, skb->data);
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002608 if (mpath) {
2609 mpp_lookup = false;
2610 next_hop = rcu_dereference(mpath->next_hop);
2611 if (!next_hop ||
2612 !(mpath->flags & (MESH_PATH_ACTIVE |
2613 MESH_PATH_RESOLVING)))
2614 mpp_lookup = true;
2615 }
2616
Henning Roggeab1c7902016-02-03 13:58:37 +01002617 if (mpp_lookup) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002618 mppath = mpp_path_lookup(sdata, skb->data);
Henning Roggeab1c7902016-02-03 13:58:37 +01002619 if (mppath)
2620 mppath->exp_time = jiffies;
2621 }
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002622
2623 if (mppath && mpath)
Bob Copeland2bdaf382016-02-28 20:03:56 -05002624 mesh_path_del(sdata, mpath->dst);
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002625 }
YanBo79617de2008-09-22 13:30:32 +08002626
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002627 /*
Joel A Fernandes9d525012011-01-10 00:44:23 -06002628 * Use address extension if it is a packet from
2629 * another interface or if we know the destination
2630 * is being proxied by a portal (i.e. portal address
2631 * differs from proxied address)
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002632 */
Joe Perchesb203ca32012-05-08 18:56:52 +00002633 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2634 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
Javier Cardona3c5772a2009-08-10 12:15:48 -07002635 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2636 skb->data, skb->data + ETH_ALEN);
Johannes Bergbf7cd942013-02-15 14:40:31 +01002637 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2638 NULL, NULL);
YanBo79617de2008-09-22 13:30:32 +08002639 } else {
Thomas Pedersen27f01122012-08-20 11:28:25 -07002640 /* DS -> MBSS (802.11-2012 13.11.3.3).
2641 * For unicast with unknown forwarding information,
2642 * destination might be in the MBSS or if that fails
2643 * forwarded to another mesh gate. In either case
2644 * resolution will be handled in ieee80211_xmit(), so
2645 * leave the original DA. This also works for mcast */
2646 const u8 *mesh_da = skb->data;
YanBo79617de2008-09-22 13:30:32 +08002647
Thomas Pedersen27f01122012-08-20 11:28:25 -07002648 if (mppath)
2649 mesh_da = mppath->mpp;
2650 else if (mpath)
2651 mesh_da = mpath->dst;
Thomas Pedersen27f01122012-08-20 11:28:25 -07002652
Javier Cardona3c5772a2009-08-10 12:15:48 -07002653 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
Johannes Berg47846c92009-11-25 17:46:19 +01002654 mesh_da, sdata->vif.addr);
Thomas Pedersen27f01122012-08-20 11:28:25 -07002655 if (is_multicast_ether_addr(mesh_da))
2656 /* DA TA mSA AE:SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002657 meshhdrlen = ieee80211_new_mesh_header(
2658 sdata, &mesh_hdr,
2659 skb->data + ETH_ALEN, NULL);
Javier Cardona3c5772a2009-08-10 12:15:48 -07002660 else
Thomas Pedersen27f01122012-08-20 11:28:25 -07002661 /* RA TA mDA mSA AE:DA SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002662 meshhdrlen = ieee80211_new_mesh_header(
2663 sdata, &mesh_hdr, skb->data,
2664 skb->data + ETH_ALEN);
YanBo79617de2008-09-22 13:30:32 +08002665
YanBo79617de2008-09-22 13:30:32 +08002666 }
Johannes Berg55de9082012-07-26 17:24:39 +02002667 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002668 if (!chanctx_conf) {
2669 ret = -ENOTCONN;
2670 goto free;
2671 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002672 band = chanctx_conf->def.chan->band;
Rajkumar Manoharan8828f812019-04-11 13:47:26 -07002673
2674 /* For injected frames, fill RA right away as nexthop lookup
2675 * will be skipped.
2676 */
2677 if ((ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP) &&
2678 is_zero_ether_addr(hdr.addr1))
2679 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002680 break;
2681#endif
Johannes Berg05c914f2008-09-11 00:01:58 +02002682 case NL80211_IFTYPE_STATION:
Johannes Berg97ffe752015-03-21 09:13:45 +01002683 /* we already did checks when looking up the RA STA */
2684 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002685
Johannes Berg97ffe752015-03-21 09:13:45 +01002686 if (tdls_peer) {
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002687 /* DA SA BSSID */
2688 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2689 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2690 memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2691 hdrlen = 24;
2692 } else if (sdata->u.mgd.use_4addr &&
2693 cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2694 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2695 IEEE80211_FCTL_TODS);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002696 /* RA TA DA SA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002697 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002698 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002699 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2700 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2701 hdrlen = 30;
2702 } else {
2703 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2704 /* BSSID SA DA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002705 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002706 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2707 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2708 hdrlen = 24;
2709 }
Johannes Berg55de9082012-07-26 17:24:39 +02002710 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002711 if (!chanctx_conf) {
2712 ret = -ENOTCONN;
2713 goto free;
2714 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002715 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002716 break;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002717 case NL80211_IFTYPE_OCB:
2718 /* DA SA BSSID */
2719 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2720 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2721 eth_broadcast_addr(hdr.addr3);
2722 hdrlen = 24;
2723 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002724 if (!chanctx_conf) {
2725 ret = -ENOTCONN;
2726 goto free;
2727 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002728 band = chanctx_conf->def.chan->band;
2729 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002730 case NL80211_IFTYPE_ADHOC:
Johannes Berge2ebc742007-07-27 15:43:22 +02002731 /* DA SA BSSID */
2732 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2733 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01002734 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002735 hdrlen = 24;
Johannes Berg55de9082012-07-26 17:24:39 +02002736 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002737 if (!chanctx_conf) {
2738 ret = -ENOTCONN;
2739 goto free;
2740 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002741 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002742 break;
2743 default:
Johannes Berg4c9451e2014-11-09 18:50:10 +02002744 ret = -EINVAL;
2745 goto free;
Johannes Berge2ebc742007-07-27 15:43:22 +02002746 }
2747
Johannes Berga729cff2011-11-06 14:13:34 +01002748 multicast = is_multicast_ether_addr(hdr.addr1);
Johannes Berge2ebc742007-07-27 15:43:22 +02002749
Johannes Berg97ffe752015-03-21 09:13:45 +01002750 /* sta is always NULL for mesh */
2751 if (sta) {
2752 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2753 wme_sta = sta->sta.wme;
2754 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2755 /* For mesh, the use of the QoS header is mandatory */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002756 wme_sta = true;
Johannes Berge2ebc742007-07-27 15:43:22 +02002757 }
2758
Johannes Berg527871d2015-03-21 08:09:55 +01002759 /* receiver does QoS (which also means we do) use it */
2760 if (wme_sta) {
Harvey Harrison065e96052008-06-22 16:45:27 -07002761 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002762 hdrlen += 2;
2763 }
2764
2765 /*
Johannes Berg238814f2008-01-28 17:19:37 +01002766 * Drop unicast frames to unauthorised stations unless they are
2767 * EAPOL frames from the local station.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002768 */
Johannes Berg55182e42011-10-12 17:28:21 +02002769 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002770 (sdata->vif.type != NL80211_IFTYPE_OCB) &&
Sergey Ryazanova6ececf2013-08-30 01:35:09 +04002771 !multicast && !authorized &&
Johannes Berg55182e42011-10-12 17:28:21 +02002772 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002773 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002774#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002775 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
Johannes Berg4c9451e2014-11-09 18:50:10 +02002776 sdata->name, hdr.addr1);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002777#endif
2778
2779 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2780
Johannes Berg4c9451e2014-11-09 18:50:10 +02002781 ret = -EPERM;
2782 goto free;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002783 }
2784
Markus Theila7528192020-05-27 18:03:34 +02002785 if (unlikely(!multicast && ((skb->sk &&
2786 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS) ||
2787 ctrl_flags & IEEE80211_TX_CTL_REQ_TX_STATUS)))
2788 info_id = ieee80211_store_ack_skb(local, skb, &info_flags,
2789 cookie);
Johannes Berga729cff2011-11-06 14:13:34 +01002790
Helmut Schaa7e244702010-12-02 18:44:09 +01002791 /*
2792 * If the skb is shared we need to obtain our own copy.
2793 */
2794 if (skb_shared(skb)) {
Johannes Berga729cff2011-11-06 14:13:34 +01002795 struct sk_buff *tmp_skb = skb;
2796
2797 /* can't happen -- skb is a clone if info_id != 0 */
2798 WARN_ON(info_id);
2799
Felix Fietkauf8a0a782010-12-18 19:30:50 +01002800 skb = skb_clone(skb, GFP_ATOMIC);
Helmut Schaa7e244702010-12-02 18:44:09 +01002801 kfree_skb(tmp_skb);
2802
Johannes Berg4c9451e2014-11-09 18:50:10 +02002803 if (!skb) {
2804 ret = -ENOMEM;
2805 goto free;
2806 }
Helmut Schaa7e244702010-12-02 18:44:09 +01002807 }
2808
Harvey Harrison065e96052008-06-22 16:45:27 -07002809 hdr.frame_control = fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002810 hdr.duration_id = 0;
2811 hdr.seq_ctrl = 0;
2812
2813 skip_header_bytes = ETH_HLEN;
2814 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2815 encaps_data = bridge_tunnel_header;
2816 encaps_len = sizeof(bridge_tunnel_header);
2817 skip_header_bytes -= 2;
Simon Hormane5c5d222013-03-28 13:38:25 +09002818 } else if (ethertype >= ETH_P_802_3_MIN) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002819 encaps_data = rfc1042_header;
2820 encaps_len = sizeof(rfc1042_header);
2821 skip_header_bytes -= 2;
2822 } else {
2823 encaps_data = NULL;
2824 encaps_len = 0;
2825 }
2826
2827 skb_pull(skb, skip_header_bytes);
Johannes Berg23c07522008-05-29 10:38:53 +02002828 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002829
Johannes Berg23c07522008-05-29 10:38:53 +02002830 /*
2831 * So we need to modify the skb header and hence need a copy of
2832 * that. The head_need variable above doesn't, so far, include
2833 * the needed header space that we don't need right away. If we
2834 * can, then we don't reallocate right now but only after the
2835 * frame arrives at the master device (if it does...)
2836 *
2837 * If we cannot, however, then we will reallocate to include all
2838 * the ever needed space. Also, if we need to reallocate it anyway,
2839 * make it big enough for everything we may ever need.
2840 */
Johannes Berge2ebc742007-07-27 15:43:22 +02002841
David S. Miller3a5be7d2008-06-18 01:19:51 -07002842 if (head_need > 0 || skb_cloned(skb)) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002843 head_need += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02002844 head_need += local->tx_headroom;
2845 head_need = max_t(int, 0, head_need);
Johannes Berg14f46c12020-10-09 13:25:41 +02002846 if (ieee80211_skb_resize(sdata, skb, head_need, ENCRYPT_DATA)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02002847 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg1c963be2012-11-07 14:02:30 +01002848 skb = NULL;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002849 return ERR_PTR(-ENOMEM);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002850 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002851 }
2852
Felix Fietkaueae44302016-07-13 11:00:02 +02002853 if (encaps_data)
Johannes Berge2ebc742007-07-27 15:43:22 +02002854 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002855
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002856#ifdef CONFIG_MAC80211_MESH
Felix Fietkaueae44302016-07-13 11:00:02 +02002857 if (meshhdrlen > 0)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002858 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002859#endif
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002860
Harvey Harrison065e96052008-06-22 16:45:27 -07002861 if (ieee80211_is_data_qos(fc)) {
Johannes Bergc29b9b92007-09-14 11:10:24 -04002862 __le16 *qos_control;
2863
Johannes Bergd58ff352017-06-16 14:29:23 +02002864 qos_control = skb_push(skb, 2);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002865 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2866 /*
2867 * Maybe we could actually set some fields here, for now just
2868 * initialise to zero to indicate no special operation.
2869 */
2870 *qos_control = 0;
2871 } else
2872 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2873
Zhang Shengjud57a5442016-03-03 01:16:56 +00002874 skb_reset_mac_header(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002875
Felix Fietkau489ee912010-12-18 19:30:48 +01002876 info = IEEE80211_SKB_CB(skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002877 memset(info, 0, sizeof(*info));
2878
Johannes Berga729cff2011-11-06 14:13:34 +01002879 info->flags = info_flags;
2880 info->ack_frame_id = info_id;
Johannes Berg73c4e192014-11-09 18:50:09 +02002881 info->band = band;
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002882 info->control.flags = ctrl_flags;
Johannes Berga729cff2011-11-06 14:13:34 +01002883
Johannes Berg4c9451e2014-11-09 18:50:10 +02002884 return skb;
2885 free:
2886 kfree_skb(skb);
2887 return ERR_PTR(ret);
Johannes Berge2ebc742007-07-27 15:43:22 +02002888}
2889
Johannes Berg17c18bf2015-03-21 15:25:43 +01002890/*
2891 * fast-xmit overview
2892 *
2893 * The core idea of this fast-xmit is to remove per-packet checks by checking
2894 * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2895 * checks that are needed to get the sta->fast_tx pointer assigned, after which
2896 * much less work can be done per packet. For example, fragmentation must be
2897 * disabled or the fast_tx pointer will not be set. All the conditions are seen
2898 * in the code here.
2899 *
2900 * Once assigned, the fast_tx data structure also caches the per-packet 802.11
2901 * header and other data to aid packet processing in ieee80211_xmit_fast().
2902 *
2903 * The most difficult part of this is that when any of these assumptions
2904 * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
2905 * ieee80211_check_fast_xmit() or friends) is required to reset the data,
2906 * since the per-packet code no longer checks the conditions. This is reflected
2907 * by the calls to these functions throughout the rest of the code, and must be
2908 * maintained if any of the TX path checks change.
2909 */
2910
2911void ieee80211_check_fast_xmit(struct sta_info *sta)
2912{
2913 struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
2914 struct ieee80211_local *local = sta->local;
2915 struct ieee80211_sub_if_data *sdata = sta->sdata;
2916 struct ieee80211_hdr *hdr = (void *)build.hdr;
2917 struct ieee80211_chanctx_conf *chanctx_conf;
2918 __le16 fc;
2919
Johannes Berg30686bf2015-06-02 21:39:54 +02002920 if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002921 return;
2922
2923 /* Locking here protects both the pointer itself, and against concurrent
2924 * invocations winning data access races to, e.g., the key pointer that
2925 * is used.
2926 * Without it, the invocation of this function right after the key
2927 * pointer changes wouldn't be sufficient, as another CPU could access
2928 * the pointer, then stall, and then do the cache update after the CPU
2929 * that invalidated the key.
2930 * With the locking, such scenarios cannot happen as the check for the
2931 * key and the fast-tx assignment are done atomically, so the CPU that
2932 * modifies the key will either wait or other one will see the key
2933 * cleared/changed already.
2934 */
2935 spin_lock_bh(&sta->lock);
Johannes Berg30686bf2015-06-02 21:39:54 +02002936 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
2937 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
Johannes Berg17c18bf2015-03-21 15:25:43 +01002938 sdata->vif.type == NL80211_IFTYPE_STATION)
2939 goto out;
2940
2941 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2942 goto out;
2943
2944 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
2945 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
Felix Fietkauf7418bc2015-09-24 14:59:49 +02002946 test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
2947 test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002948 goto out;
2949
2950 if (sdata->noack_map)
2951 goto out;
2952
2953 /* fast-xmit doesn't handle fragmentation at all */
Johannes Berg725b8122015-04-10 14:02:08 +02002954 if (local->hw.wiphy->frag_threshold != (u32)-1 &&
Sara Sharonf3fe4e92016-10-18 23:12:11 +03002955 !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002956 goto out;
2957
2958 rcu_read_lock();
2959 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2960 if (!chanctx_conf) {
2961 rcu_read_unlock();
2962 goto out;
2963 }
2964 build.band = chanctx_conf->def.chan->band;
2965 rcu_read_unlock();
2966
2967 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2968
2969 switch (sdata->vif.type) {
Johannes Berg3ffd8842015-04-14 10:28:37 +02002970 case NL80211_IFTYPE_ADHOC:
2971 /* DA SA BSSID */
2972 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2973 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2974 memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
2975 build.hdr_len = 24;
2976 break;
Johannes Berg17c18bf2015-03-21 15:25:43 +01002977 case NL80211_IFTYPE_STATION:
2978 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2979 /* DA SA BSSID */
2980 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2981 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2982 memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
2983 build.hdr_len = 24;
2984 break;
2985 }
2986
2987 if (sdata->u.mgd.use_4addr) {
2988 /* non-regular ethertype cannot use the fastpath */
2989 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2990 IEEE80211_FCTL_TODS);
2991 /* RA TA DA SA */
2992 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2993 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2994 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2995 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2996 build.hdr_len = 30;
2997 break;
2998 }
2999 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
3000 /* BSSID SA DA */
3001 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
3002 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
3003 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3004 build.hdr_len = 24;
3005 break;
3006 case NL80211_IFTYPE_AP_VLAN:
3007 if (sdata->wdev.use_4addr) {
3008 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
3009 IEEE80211_FCTL_TODS);
3010 /* RA TA DA SA */
3011 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
3012 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
3013 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
3014 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
3015 build.hdr_len = 30;
3016 break;
3017 }
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05003018 fallthrough;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003019 case NL80211_IFTYPE_AP:
3020 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
3021 /* DA BSSID SA */
3022 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
3023 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
3024 build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
3025 build.hdr_len = 24;
3026 break;
3027 default:
3028 /* not handled on fast-xmit */
3029 goto out;
3030 }
3031
3032 if (sta->sta.wme) {
3033 build.hdr_len += 2;
3034 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3035 }
3036
3037 /* We store the key here so there's no point in using rcu_dereference()
3038 * but that's fine because the code that changes the pointers will call
3039 * this function after doing so. For a single CPU that would be enough,
3040 * for multiple see the comment above.
3041 */
3042 build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
3043 if (!build.key)
3044 build.key = rcu_access_pointer(sdata->default_unicast_key);
3045 if (build.key) {
Johannes Berge495c242015-04-10 14:03:17 +02003046 bool gen_iv, iv_spc, mmic;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003047
3048 gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
3049 iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
David Spinadel9de18d82017-12-01 13:50:52 +02003050 mmic = build.key->conf.flags &
3051 (IEEE80211_KEY_FLAG_GENERATE_MMIC |
3052 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003053
3054 /* don't handle software crypto */
3055 if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
3056 goto out;
3057
Alexander Wetzel62872a92018-08-31 15:00:38 +02003058 /* Key is being removed */
3059 if (build.key->flags & KEY_FLAG_TAINTED)
3060 goto out;
3061
Johannes Berg17c18bf2015-03-21 15:25:43 +01003062 switch (build.key->conf.cipher) {
3063 case WLAN_CIPHER_SUITE_CCMP:
3064 case WLAN_CIPHER_SUITE_CCMP_256:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003065 if (gen_iv)
Johannes Berg17c18bf2015-03-21 15:25:43 +01003066 build.pn_offs = build.hdr_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003067 if (gen_iv || iv_spc)
3068 build.hdr_len += IEEE80211_CCMP_HDR_LEN;
3069 break;
3070 case WLAN_CIPHER_SUITE_GCMP:
3071 case WLAN_CIPHER_SUITE_GCMP_256:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003072 if (gen_iv)
Johannes Berg17c18bf2015-03-21 15:25:43 +01003073 build.pn_offs = build.hdr_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003074 if (gen_iv || iv_spc)
3075 build.hdr_len += IEEE80211_GCMP_HDR_LEN;
3076 break;
Johannes Berge495c242015-04-10 14:03:17 +02003077 case WLAN_CIPHER_SUITE_TKIP:
3078 /* cannot handle MMIC or IV generation in xmit-fast */
3079 if (mmic || gen_iv)
3080 goto out;
3081 if (iv_spc)
3082 build.hdr_len += IEEE80211_TKIP_IV_LEN;
3083 break;
3084 case WLAN_CIPHER_SUITE_WEP40:
3085 case WLAN_CIPHER_SUITE_WEP104:
3086 /* cannot handle IV generation in fast-xmit */
3087 if (gen_iv)
3088 goto out;
3089 if (iv_spc)
3090 build.hdr_len += IEEE80211_WEP_IV_LEN;
3091 break;
3092 case WLAN_CIPHER_SUITE_AES_CMAC:
3093 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3094 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3095 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3096 WARN(1,
3097 "management cipher suite 0x%x enabled for data\n",
3098 build.key->conf.cipher);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003099 goto out;
Johannes Berge495c242015-04-10 14:03:17 +02003100 default:
3101 /* we don't know how to generate IVs for this at all */
3102 if (WARN_ON(gen_iv))
3103 goto out;
3104 /* pure hardware keys are OK, of course */
3105 if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
3106 break;
3107 /* cipher scheme might require space allocation */
3108 if (iv_spc &&
3109 build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
3110 goto out;
3111 if (iv_spc)
3112 build.hdr_len += build.key->conf.iv_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003113 }
3114
3115 fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3116 }
3117
3118 hdr->frame_control = fc;
3119
3120 memcpy(build.hdr + build.hdr_len,
3121 rfc1042_header, sizeof(rfc1042_header));
3122 build.hdr_len += sizeof(rfc1042_header);
3123
3124 fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3125 /* if the kmemdup fails, continue w/o fast_tx */
3126 if (!fast_tx)
3127 goto out;
3128
3129 out:
3130 /* we might have raced against another call to this function */
3131 old = rcu_dereference_protected(sta->fast_tx,
3132 lockdep_is_held(&sta->lock));
3133 rcu_assign_pointer(sta->fast_tx, fast_tx);
3134 if (old)
3135 kfree_rcu(old, rcu_head);
3136 spin_unlock_bh(&sta->lock);
3137}
3138
3139void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3140{
3141 struct sta_info *sta;
3142
3143 rcu_read_lock();
3144 list_for_each_entry_rcu(sta, &local->sta_list, list)
3145 ieee80211_check_fast_xmit(sta);
3146 rcu_read_unlock();
3147}
3148
3149void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3150{
3151 struct ieee80211_local *local = sdata->local;
3152 struct sta_info *sta;
3153
3154 rcu_read_lock();
3155
3156 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3157 if (sdata != sta->sdata &&
3158 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3159 continue;
3160 ieee80211_check_fast_xmit(sta);
3161 }
3162
3163 rcu_read_unlock();
3164}
3165
3166void ieee80211_clear_fast_xmit(struct sta_info *sta)
3167{
3168 struct ieee80211_fast_tx *fast_tx;
3169
3170 spin_lock_bh(&sta->lock);
3171 fast_tx = rcu_dereference_protected(sta->fast_tx,
3172 lockdep_is_held(&sta->lock));
3173 RCU_INIT_POINTER(sta->fast_tx, NULL);
3174 spin_unlock_bh(&sta->lock);
3175
3176 if (fast_tx)
3177 kfree_rcu(fast_tx, rcu_head);
3178}
3179
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003180static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
Sara Sharon166ac9d2018-08-29 08:57:02 +02003181 struct sk_buff *skb, int headroom)
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003182{
Sara Sharon166ac9d2018-08-29 08:57:02 +02003183 if (skb_headroom(skb) < headroom) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003184 I802_DEBUG_INC(local->tx_expand_skb_head);
3185
Sara Sharon166ac9d2018-08-29 08:57:02 +02003186 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003187 wiphy_debug(local->hw.wiphy,
3188 "failed to reallocate TX buffer\n");
3189 return false;
3190 }
3191 }
3192
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003193 return true;
3194}
3195
3196static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3197 struct ieee80211_fast_tx *fast_tx,
3198 struct sk_buff *skb)
3199{
3200 struct ieee80211_local *local = sdata->local;
3201 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3202 struct ieee80211_hdr *hdr;
Michael Braun06f2bb12016-10-15 13:28:18 +02003203 struct ethhdr *amsdu_hdr;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003204 int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3205 int subframe_len = skb->len - hdr_len;
3206 void *data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003207 u8 *qc, *h_80211_src, *h_80211_dst;
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003208 const u8 *bssid;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003209
3210 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3211 return false;
3212
3213 if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3214 return true;
3215
Sara Sharon166ac9d2018-08-29 08:57:02 +02003216 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(*amsdu_hdr)))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003217 return false;
3218
Michael Braun06f2bb12016-10-15 13:28:18 +02003219 data = skb_push(skb, sizeof(*amsdu_hdr));
3220 memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003221 hdr = data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003222 amsdu_hdr = data + hdr_len;
3223 /* h_80211_src/dst is addr* field within hdr */
3224 h_80211_src = data + fast_tx->sa_offs;
3225 h_80211_dst = data + fast_tx->da_offs;
3226
3227 amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3228 ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3229 ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
3230
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003231 /* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3232 * fields needs to be changed to BSSID for A-MSDU frames depending
3233 * on FromDS/ToDS values.
3234 */
3235 switch (sdata->vif.type) {
3236 case NL80211_IFTYPE_STATION:
3237 bssid = sdata->u.mgd.bssid;
3238 break;
3239 case NL80211_IFTYPE_AP:
3240 case NL80211_IFTYPE_AP_VLAN:
3241 bssid = sdata->vif.addr;
3242 break;
3243 default:
3244 bssid = NULL;
3245 }
3246
3247 if (bssid && ieee80211_has_fromds(hdr->frame_control))
3248 ether_addr_copy(h_80211_src, bssid);
3249
3250 if (bssid && ieee80211_has_tods(hdr->frame_control))
3251 ether_addr_copy(h_80211_dst, bssid);
3252
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003253 qc = ieee80211_get_qos_ctl(hdr);
3254 *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3255
3256 info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3257
3258 return true;
3259}
3260
3261static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3262 struct sta_info *sta,
3263 struct ieee80211_fast_tx *fast_tx,
3264 struct sk_buff *skb)
3265{
3266 struct ieee80211_local *local = sdata->local;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003267 struct fq *fq = &local->fq;
3268 struct fq_tin *tin;
3269 struct fq_flow *flow;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003270 u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3271 struct ieee80211_txq *txq = sta->sta.txq[tid];
3272 struct txq_info *txqi;
3273 struct sk_buff **frag_tail, *head;
3274 int subframe_len = skb->len - ETH_ALEN;
3275 u8 max_subframes = sta->sta.max_amsdu_subframes;
3276 int max_frags = local->hw.max_tx_fragments;
3277 int max_amsdu_len = sta->sta.max_amsdu_len;
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003278 int orig_truesize;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003279 u32 flow_idx;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003280 __be16 len;
3281 void *data;
3282 bool ret = false;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003283 unsigned int orig_len;
Lorenzo Bianconi66eb02d2018-08-31 01:04:13 +02003284 int n = 2, nfrags, pad = 0;
Sara Sharon166ac9d2018-08-29 08:57:02 +02003285 u16 hdrlen;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003286
3287 if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
3288 return false;
3289
Ryder Lee4f2e3eb2021-06-18 04:38:59 +08003290 if (sdata->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
3291 return false;
3292
Sara Sharon344f8e02018-12-15 11:03:07 +02003293 if (skb_is_gso(skb))
3294 return false;
3295
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003296 if (!txq)
3297 return false;
3298
3299 txqi = to_txq_info(txq);
3300 if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3301 return false;
3302
3303 if (sta->sta.max_rc_amsdu_len)
3304 max_amsdu_len = min_t(int, max_amsdu_len,
3305 sta->sta.max_rc_amsdu_len);
3306
Sara Sharonedba6bd2018-09-05 08:06:10 +03003307 if (sta->sta.max_tid_amsdu_len[tid])
3308 max_amsdu_len = min_t(int, max_amsdu_len,
3309 sta->sta.max_tid_amsdu_len[tid]);
3310
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003311 flow_idx = fq_flow_idx(fq, skb);
3312
Michal Kaziorfa962b92016-05-19 10:37:49 +02003313 spin_lock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003314
Michal Kaziorfa962b92016-05-19 10:37:49 +02003315 /* TODO: Ideally aggregation should be done on dequeue to remain
3316 * responsive to environment changes.
3317 */
3318
3319 tin = &txqi->tin;
Felix Fietkaubf9009b2020-12-18 19:47:14 +01003320 flow = fq_flow_classify(fq, tin, flow_idx, skb);
Michal Kaziorfa962b92016-05-19 10:37:49 +02003321 head = skb_peek_tail(&flow->queue);
Sara Sharon344f8e02018-12-15 11:03:07 +02003322 if (!head || skb_is_gso(head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003323 goto out;
3324
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003325 orig_truesize = head->truesize;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003326 orig_len = head->len;
3327
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003328 if (skb->len + head->len > max_amsdu_len)
3329 goto out;
3330
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003331 nfrags = 1 + skb_shinfo(skb)->nr_frags;
3332 nfrags += 1 + skb_shinfo(head)->nr_frags;
3333 frag_tail = &skb_shinfo(head)->frag_list;
3334 while (*frag_tail) {
3335 nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3336 frag_tail = &(*frag_tail)->next;
3337 n++;
3338 }
3339
3340 if (max_subframes && n > max_subframes)
3341 goto out;
3342
3343 if (max_frags && nfrags > max_frags)
3344 goto out;
3345
Sara Sharon9739fe22018-09-05 08:06:11 +03003346 if (!drv_can_aggregate_in_amsdu(local, head, skb))
3347 goto out;
3348
Lorenzo Bianconi1eb50792018-08-29 21:03:25 +02003349 if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003350 goto out;
3351
Sara Sharon166ac9d2018-08-29 08:57:02 +02003352 /*
3353 * Pad out the previous subframe to a multiple of 4 by adding the
3354 * padding to the next one, that's being added. Note that head->len
3355 * is the length of the full A-MSDU, but that works since each time
3356 * we add a new subframe we pad out the previous one to a multiple
3357 * of 4 and thus it no longer matters in the next round.
3358 */
3359 hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3360 if ((head->len - hdrlen) & 3)
3361 pad = 4 - ((head->len - hdrlen) & 3);
3362
3363 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3364 2 + pad))
Johannes Bergaa58acf2018-08-30 10:55:49 +02003365 goto out_recalc;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003366
3367 ret = true;
3368 data = skb_push(skb, ETH_ALEN + 2);
3369 memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3370
3371 data += 2 * ETH_ALEN;
3372 len = cpu_to_be16(subframe_len);
3373 memcpy(data, &len, 2);
3374 memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3375
Sara Sharon166ac9d2018-08-29 08:57:02 +02003376 memset(skb_push(skb, pad), 0, pad);
3377
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003378 head->len += skb->len;
3379 head->data_len += skb->len;
3380 *frag_tail = skb;
3381
Johannes Bergaa58acf2018-08-30 10:55:49 +02003382out_recalc:
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003383 fq->memory_usage += head->truesize - orig_truesize;
Johannes Bergaa58acf2018-08-30 10:55:49 +02003384 if (head->len != orig_len) {
3385 flow->backlog += head->len - orig_len;
3386 tin->backlog_bytes += head->len - orig_len;
Johannes Bergaa58acf2018-08-30 10:55:49 +02003387 }
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003388out:
Michal Kaziorfa962b92016-05-19 10:37:49 +02003389 spin_unlock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003390
3391 return ret;
3392}
3393
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003394/*
3395 * Can be called while the sta lock is held. Anything that can cause packets to
3396 * be generated will cause deadlock!
3397 */
Ryder Lee03c39112021-06-17 18:31:12 +02003398static ieee80211_tx_result
3399ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3400 struct sta_info *sta, u8 pn_offs,
3401 struct ieee80211_key *key,
3402 struct ieee80211_tx_data *tx)
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003403{
Ryder Lee03c39112021-06-17 18:31:12 +02003404 struct sk_buff *skb = tx->skb;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003405 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3406 struct ieee80211_hdr *hdr = (void *)skb->data;
3407 u8 tid = IEEE80211_NUM_TIDS;
3408
Ryder Lee03c39112021-06-17 18:31:12 +02003409 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL) &&
3410 ieee80211_tx_h_rate_ctrl(tx) != TX_CONTINUE)
3411 return TX_DROP;
3412
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003413 if (key)
3414 info->control.hw_key = &key->conf;
3415
Lev Stipakov36ec1442020-11-13 23:46:24 +02003416 dev_sw_netstats_tx_add(skb->dev, 1, skb->len);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003417
3418 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3419 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003420 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3421 } else {
3422 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3423 hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3424 sdata->sequence_number += 0x10;
3425 }
3426
3427 if (skb_shinfo(skb)->gso_size)
3428 sta->tx_stats.msdu[tid] +=
3429 DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3430 else
3431 sta->tx_stats.msdu[tid]++;
3432
3433 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3434
3435 /* statistics normally done by ieee80211_tx_h_stats (but that
3436 * has to consider fragmentation, so is more complex)
3437 */
3438 sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3439 sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
3440
3441 if (pn_offs) {
3442 u64 pn;
3443 u8 *crypto_hdr = skb->data + pn_offs;
3444
3445 switch (key->conf.cipher) {
3446 case WLAN_CIPHER_SUITE_CCMP:
3447 case WLAN_CIPHER_SUITE_CCMP_256:
3448 case WLAN_CIPHER_SUITE_GCMP:
3449 case WLAN_CIPHER_SUITE_GCMP_256:
3450 pn = atomic64_inc_return(&key->conf.tx_pn);
3451 crypto_hdr[0] = pn;
3452 crypto_hdr[1] = pn >> 8;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003453 crypto_hdr[3] = 0x20 | (key->conf.keyidx << 6);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003454 crypto_hdr[4] = pn >> 16;
3455 crypto_hdr[5] = pn >> 24;
3456 crypto_hdr[6] = pn >> 32;
3457 crypto_hdr[7] = pn >> 40;
3458 break;
3459 }
3460 }
Ryder Lee03c39112021-06-17 18:31:12 +02003461
3462 return TX_CONTINUE;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003463}
3464
Johannes Berg17c18bf2015-03-21 15:25:43 +01003465static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003466 struct sta_info *sta,
Johannes Berg17c18bf2015-03-21 15:25:43 +01003467 struct ieee80211_fast_tx *fast_tx,
3468 struct sk_buff *skb)
3469{
3470 struct ieee80211_local *local = sdata->local;
3471 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3472 int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3473 int hw_headroom = sdata->local->hw.extra_tx_headroom;
3474 struct ethhdr eth;
Johannes Berg35f432a2017-01-02 11:19:29 +01003475 struct ieee80211_tx_info *info;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003476 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3477 struct ieee80211_tx_data tx;
3478 ieee80211_tx_result r;
3479 struct tid_ampdu_tx *tid_tx = NULL;
3480 u8 tid = IEEE80211_NUM_TIDS;
3481
3482 /* control port protocol needs a lot of special handling */
3483 if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3484 return false;
3485
3486 /* only RFC 1042 SNAP */
3487 if (ethertype < ETH_P_802_3_MIN)
3488 return false;
3489
3490 /* don't handle TX status request here either */
3491 if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3492 return false;
3493
3494 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3495 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3496 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
Johannes Berg472be002015-06-09 16:45:08 +02003497 if (tid_tx) {
3498 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3499 return false;
3500 if (tid_tx->timeout)
3501 tid_tx->last_tx = jiffies;
3502 }
Johannes Berg17c18bf2015-03-21 15:25:43 +01003503 }
3504
3505 /* after this point (skb is modified) we cannot return false */
3506
3507 if (skb_shared(skb)) {
3508 struct sk_buff *tmp_skb = skb;
3509
3510 skb = skb_clone(skb, GFP_ATOMIC);
3511 kfree_skb(tmp_skb);
3512
3513 if (!skb)
3514 return true;
3515 }
3516
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003517 if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3518 ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
3519 return true;
3520
Johannes Berg17c18bf2015-03-21 15:25:43 +01003521 /* will not be crypto-handled beyond what we do here, so use false
3522 * as the may-encrypt argument for the resize to not account for
3523 * more room than we already have in 'extra_head'
3524 */
3525 if (unlikely(ieee80211_skb_resize(sdata, skb,
3526 max_t(int, extra_head + hw_headroom -
3527 skb_headroom(skb), 0),
Johannes Berg14f46c12020-10-09 13:25:41 +02003528 ENCRYPT_NO))) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01003529 kfree_skb(skb);
3530 return true;
3531 }
3532
3533 memcpy(&eth, skb->data, ETH_HLEN - 2);
Johannes Bergd58ff352017-06-16 14:29:23 +02003534 hdr = skb_push(skb, extra_head);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003535 memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3536 memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
3537 memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
3538
Johannes Berg35f432a2017-01-02 11:19:29 +01003539 info = IEEE80211_SKB_CB(skb);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003540 memset(info, 0, sizeof(*info));
3541 info->band = fast_tx->band;
3542 info->control.vif = &sdata->vif;
3543 info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3544 IEEE80211_TX_CTL_DONTFRAG |
3545 (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003546 info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003547
Julius Niedworok276d9e82019-03-28 21:01:06 +01003548#ifdef CONFIG_MAC80211_DEBUGFS
3549 if (local->force_tx_status)
3550 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3551#endif
3552
Felix Fietkaua786f962016-11-04 10:27:54 +01003553 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3554 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3555 *ieee80211_get_qos_ctl(hdr) = tid;
3556 }
3557
Johannes Berg17c18bf2015-03-21 15:25:43 +01003558 __skb_queue_head_init(&tx.skbs);
3559
3560 tx.flags = IEEE80211_TX_UNICAST;
3561 tx.local = local;
3562 tx.sdata = sdata;
3563 tx.sta = sta;
3564 tx.key = fast_tx->key;
3565
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003566 if (ieee80211_queue_skb(local, sdata, sta, skb))
3567 return true;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003568
Ryder Lee03c39112021-06-17 18:31:12 +02003569 tx.skb = skb;
3570 r = ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3571 fast_tx->key, &tx);
3572 tx.skb = NULL;
3573 if (r == TX_DROP) {
3574 kfree_skb(skb);
3575 return true;
3576 }
Johannes Berg17c18bf2015-03-21 15:25:43 +01003577
3578 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3579 sdata = container_of(sdata->bss,
3580 struct ieee80211_sub_if_data, u.ap);
3581
3582 __skb_queue_tail(&tx.skbs, skb);
Johannes Berg4fd03282019-10-01 23:26:35 +02003583 ieee80211_tx_frags(local, &sdata->vif, sta, &tx.skbs, false);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003584 return true;
3585}
3586
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003587struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3588 struct ieee80211_txq *txq)
3589{
3590 struct ieee80211_local *local = hw_to_local(hw);
3591 struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3592 struct ieee80211_hdr *hdr;
3593 struct sk_buff *skb = NULL;
3594 struct fq *fq = &local->fq;
3595 struct fq_tin *tin = &txqi->tin;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003596 struct ieee80211_tx_info *info;
3597 struct ieee80211_tx_data tx;
3598 ieee80211_tx_result r;
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303599 struct ieee80211_vif *vif = txq->vif;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003600
Erik Stromdahlfb0e76a2019-06-17 22:01:39 +02003601 WARN_ON_ONCE(softirq_count() == 0);
3602
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003603 if (!ieee80211_txq_airtime_check(hw, txq))
3604 return NULL;
3605
Felix Fietkauded46982019-03-16 18:06:33 +01003606begin:
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003607 spin_lock_bh(&fq->lock);
3608
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303609 if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags) ||
3610 test_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags))
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003611 goto out;
3612
Johannes Berg1153a742021-03-23 21:05:01 +01003613 if (vif->txqs_stopped[txq->ac]) {
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303614 set_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags);
3615 goto out;
3616 }
3617
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003618 /* Make sure fragments stay together. */
3619 skb = __skb_dequeue(&txqi->frags);
Johannes Bergca47b462021-04-16 13:47:04 +02003620 if (unlikely(skb)) {
3621 if (!(IEEE80211_SKB_CB(skb)->control.flags &
3622 IEEE80211_TX_INTCFL_NEED_TXPROCESSING))
3623 goto out;
3624 IEEE80211_SKB_CB(skb)->control.flags &=
3625 ~IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
3626 } else {
3627 skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
3628 }
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003629
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003630 if (!skb)
3631 goto out;
3632
Felix Fietkauded46982019-03-16 18:06:33 +01003633 spin_unlock_bh(&fq->lock);
3634
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003635 hdr = (struct ieee80211_hdr *)skb->data;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003636 info = IEEE80211_SKB_CB(skb);
3637
3638 memset(&tx, 0, sizeof(tx));
3639 __skb_queue_head_init(&tx.skbs);
3640 tx.local = local;
3641 tx.skb = skb;
3642 tx.sdata = vif_to_sdata(info->control.vif);
3643
Mathy Vanhoef804fc6a2020-10-19 20:01:13 +04003644 if (txq->sta) {
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003645 tx.sta = container_of(txq->sta, struct sta_info, sta);
Jouni Malinence2e1ca2020-03-26 15:51:34 +01003646 /*
3647 * Drop unicast frames to unauthorised stations unless they are
Mathy Vanhoef804fc6a2020-10-19 20:01:13 +04003648 * injected frames or EAPOL frames from the local station.
Jouni Malinence2e1ca2020-03-26 15:51:34 +01003649 */
Mathy Vanhoef804fc6a2020-10-19 20:01:13 +04003650 if (unlikely(!(info->flags & IEEE80211_TX_CTL_INJECTED) &&
3651 ieee80211_is_data(hdr->frame_control) &&
Johannes Bergbe8c8272020-03-29 22:50:06 +02003652 !ieee80211_vif_is_mesh(&tx.sdata->vif) &&
Jouni Malinence2e1ca2020-03-26 15:51:34 +01003653 tx.sdata->vif.type != NL80211_IFTYPE_OCB &&
3654 !is_multicast_ether_addr(hdr->addr1) &&
3655 !test_sta_flag(tx.sta, WLAN_STA_AUTHORIZED) &&
3656 (!(info->control.flags &
3657 IEEE80211_TX_CTRL_PORT_CTRL_PROTO) ||
3658 !ether_addr_equal(tx.sdata->vif.addr,
3659 hdr->addr2)))) {
3660 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
3661 ieee80211_free_txskb(&local->hw, skb);
3662 goto begin;
3663 }
3664 }
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003665
3666 /*
3667 * The key can be removed while the packet was queued, so need to call
3668 * this here to get the current key.
3669 */
3670 r = ieee80211_tx_h_select_key(&tx);
3671 if (r != TX_CONTINUE) {
3672 ieee80211_free_txskb(&local->hw, skb);
3673 goto begin;
3674 }
3675
Felix Fietkauc1f4c9e2016-11-04 10:27:52 +01003676 if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
3677 info->flags |= IEEE80211_TX_CTL_AMPDU;
3678 else
3679 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
3680
Ryder Lee3187ba02021-06-17 18:31:13 +02003681 if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
3682 if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
3683 r = ieee80211_tx_h_rate_ctrl(&tx);
3684 if (r != TX_CONTINUE) {
3685 ieee80211_free_txskb(&local->hw, skb);
3686 goto begin;
3687 }
3688 }
John Crispin50ff4772019-11-25 11:04:37 +01003689 goto encap_out;
Ryder Lee3187ba02021-06-17 18:31:13 +02003690 }
John Crispin50ff4772019-11-25 11:04:37 +01003691
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003692 if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003693 struct sta_info *sta = container_of(txq->sta, struct sta_info,
3694 sta);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003695 u8 pn_offs = 0;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003696
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003697 if (tx.key &&
3698 (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
3699 pn_offs = ieee80211_hdrlen(hdr->frame_control);
3700
Ryder Lee03c39112021-06-17 18:31:12 +02003701 r = ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
3702 tx.key, &tx);
3703 if (r != TX_CONTINUE) {
3704 ieee80211_free_txskb(&local->hw, skb);
3705 goto begin;
3706 }
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003707 } else {
3708 if (invoke_tx_handlers_late(&tx))
3709 goto begin;
3710
3711 skb = __skb_dequeue(&tx.skbs);
3712
Felix Fietkauded46982019-03-16 18:06:33 +01003713 if (!skb_queue_empty(&tx.skbs)) {
3714 spin_lock_bh(&fq->lock);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003715 skb_queue_splice_tail(&tx.skbs, &txqi->frags);
Felix Fietkauded46982019-03-16 18:06:33 +01003716 spin_unlock_bh(&fq->lock);
3717 }
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003718 }
3719
Johannes Berg233e98d2018-12-15 11:03:09 +02003720 if (skb_has_frag_list(skb) &&
Johannes Berg1e1430d2016-10-04 09:22:19 +02003721 !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
3722 if (skb_linearize(skb)) {
3723 ieee80211_free_txskb(&local->hw, skb);
3724 goto begin;
3725 }
3726 }
3727
Johannes Berg53168212017-06-22 12:20:30 +02003728 switch (tx.sdata->vif.type) {
3729 case NL80211_IFTYPE_MONITOR:
3730 if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
3731 vif = &tx.sdata->vif;
3732 break;
3733 }
3734 tx.sdata = rcu_dereference(local->monitor_sdata);
3735 if (tx.sdata) {
3736 vif = &tx.sdata->vif;
3737 info->hw_queue =
3738 vif->hw_queue[skb_get_queue_mapping(skb)];
3739 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
3740 ieee80211_free_txskb(&local->hw, skb);
3741 goto begin;
3742 } else {
3743 vif = NULL;
3744 }
3745 break;
3746 case NL80211_IFTYPE_AP_VLAN:
3747 tx.sdata = container_of(tx.sdata->bss,
3748 struct ieee80211_sub_if_data, u.ap);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05003749 fallthrough;
Johannes Berg53168212017-06-22 12:20:30 +02003750 default:
3751 vif = &tx.sdata->vif;
3752 break;
3753 }
3754
John Crispin50ff4772019-11-25 11:04:37 +01003755encap_out:
Johannes Berg53168212017-06-22 12:20:30 +02003756 IEEE80211_SKB_CB(skb)->control.vif = vif;
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003757
Johannes Bergca98c472020-02-21 10:45:45 +01003758 if (vif &&
3759 wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) {
Felix Fietkau3ff901c2020-07-24 20:28:16 +02003760 bool ampdu = txq->ac != IEEE80211_AC_VO;
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003761 u32 airtime;
3762
3763 airtime = ieee80211_calc_expected_tx_airtime(hw, vif, txq->sta,
Felix Fietkau3ff901c2020-07-24 20:28:16 +02003764 skb->len, ampdu);
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003765 if (airtime) {
3766 airtime = ieee80211_info_set_tx_time_est(info, airtime);
3767 ieee80211_sta_update_pending_airtime(local, tx.sta,
3768 txq->ac,
3769 airtime,
3770 false);
3771 }
3772 }
3773
Felix Fietkauded46982019-03-16 18:06:33 +01003774 return skb;
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303775
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003776out:
3777 spin_unlock_bh(&fq->lock);
3778
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003779 return skb;
3780}
3781EXPORT_SYMBOL(ieee80211_tx_dequeue);
3782
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003783struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac)
3784{
3785 struct ieee80211_local *local = hw_to_local(hw);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003786 struct ieee80211_txq *ret = NULL;
Kan Yan3ace10f2019-11-18 22:06:09 -08003787 struct txq_info *txqi = NULL, *head = NULL;
3788 bool found_eligible_txq = false;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003789
Felix Fietkau5b989c12019-03-15 11:03:35 +01003790 spin_lock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003791
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003792 begin:
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003793 txqi = list_first_entry_or_null(&local->active_txqs[ac],
3794 struct txq_info,
3795 schedule_order);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003796 if (!txqi)
Felix Fietkau5b989c12019-03-15 11:03:35 +01003797 goto out;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003798
Kan Yan3ace10f2019-11-18 22:06:09 -08003799 if (txqi == head) {
3800 if (!found_eligible_txq)
3801 goto out;
3802 else
3803 found_eligible_txq = false;
3804 }
3805
3806 if (!head)
3807 head = txqi;
3808
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003809 if (txqi->txq.sta) {
3810 struct sta_info *sta = container_of(txqi->txq.sta,
Kan Yan3ace10f2019-11-18 22:06:09 -08003811 struct sta_info, sta);
3812 bool aql_check = ieee80211_txq_airtime_check(hw, &txqi->txq);
3813 s64 deficit = sta->airtime[txqi->txq.ac].deficit;
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003814
Kan Yan3ace10f2019-11-18 22:06:09 -08003815 if (aql_check)
3816 found_eligible_txq = true;
3817
3818 if (deficit < 0)
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003819 sta->airtime[txqi->txq.ac].deficit +=
3820 sta->airtime_weight;
Kan Yan3ace10f2019-11-18 22:06:09 -08003821
3822 if (deficit < 0 || !aql_check) {
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003823 list_move_tail(&txqi->schedule_order,
3824 &local->active_txqs[txqi->txq.ac]);
3825 goto begin;
3826 }
3827 }
3828
3829
3830 if (txqi->schedule_round == local->schedule_round[ac])
Felix Fietkau5b989c12019-03-15 11:03:35 +01003831 goto out;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003832
3833 list_del_init(&txqi->schedule_order);
3834 txqi->schedule_round = local->schedule_round[ac];
Felix Fietkau5b989c12019-03-15 11:03:35 +01003835 ret = &txqi->txq;
3836
3837out:
3838 spin_unlock_bh(&local->active_txq_lock[ac]);
3839 return ret;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003840}
3841EXPORT_SYMBOL(ieee80211_next_txq);
3842
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003843void __ieee80211_schedule_txq(struct ieee80211_hw *hw,
3844 struct ieee80211_txq *txq,
3845 bool force)
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003846{
3847 struct ieee80211_local *local = hw_to_local(hw);
3848 struct txq_info *txqi = to_txq_info(txq);
3849
Felix Fietkau5b989c12019-03-15 11:03:35 +01003850 spin_lock_bh(&local->active_txq_lock[txq->ac]);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003851
3852 if (list_empty(&txqi->schedule_order) &&
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003853 (force || !skb_queue_empty(&txqi->frags) ||
3854 txqi->tin.backlog_packets)) {
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003855 /* If airtime accounting is active, always enqueue STAs at the
3856 * head of the list to ensure that they only get moved to the
3857 * back by the airtime DRR scheduler once they have a negative
3858 * deficit. A station that already has a negative deficit will
3859 * get immediately moved to the back of the list on the next
3860 * call to ieee80211_next_txq().
3861 */
Lorenzo Bianconic13cf5c2020-12-26 10:39:08 +01003862 if (txqi->txq.sta && local->airtime_flags &&
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003863 wiphy_ext_feature_isset(local->hw.wiphy,
3864 NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
3865 list_add(&txqi->schedule_order,
3866 &local->active_txqs[txq->ac]);
3867 else
3868 list_add_tail(&txqi->schedule_order,
3869 &local->active_txqs[txq->ac]);
3870 }
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003871
Toke Høiland-Jørgensen390298e2019-01-22 15:20:16 +01003872 spin_unlock_bh(&local->active_txq_lock[txq->ac]);
3873}
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003874EXPORT_SYMBOL(__ieee80211_schedule_txq);
Toke Høiland-Jørgensen390298e2019-01-22 15:20:16 +01003875
Lorenzo Bianconie9084352021-01-09 18:57:51 +01003876DEFINE_STATIC_KEY_FALSE(aql_disable);
3877
Kan Yan3ace10f2019-11-18 22:06:09 -08003878bool ieee80211_txq_airtime_check(struct ieee80211_hw *hw,
3879 struct ieee80211_txq *txq)
3880{
3881 struct sta_info *sta;
3882 struct ieee80211_local *local = hw_to_local(hw);
3883
Toke Høiland-Jørgensen911bde02019-12-12 12:14:37 +01003884 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
Kan Yan3ace10f2019-11-18 22:06:09 -08003885 return true;
3886
Lorenzo Bianconie9084352021-01-09 18:57:51 +01003887 if (static_branch_unlikely(&aql_disable))
3888 return true;
3889
Kan Yan3ace10f2019-11-18 22:06:09 -08003890 if (!txq->sta)
3891 return true;
3892
Johannes Berg73bc9e02021-03-19 23:28:01 +01003893 if (unlikely(txq->tid == IEEE80211_NUM_TIDS))
3894 return true;
3895
Kan Yan3ace10f2019-11-18 22:06:09 -08003896 sta = container_of(txq->sta, struct sta_info, sta);
3897 if (atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
3898 sta->airtime[txq->ac].aql_limit_low)
3899 return true;
3900
3901 if (atomic_read(&local->aql_total_pending_airtime) <
3902 local->aql_threshold &&
3903 atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
3904 sta->airtime[txq->ac].aql_limit_high)
3905 return true;
3906
3907 return false;
3908}
3909EXPORT_SYMBOL(ieee80211_txq_airtime_check);
3910
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003911bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
3912 struct ieee80211_txq *txq)
3913{
3914 struct ieee80211_local *local = hw_to_local(hw);
3915 struct txq_info *iter, *tmp, *txqi = to_txq_info(txq);
3916 struct sta_info *sta;
3917 u8 ac = txq->ac;
3918
Felix Fietkau5b989c12019-03-15 11:03:35 +01003919 spin_lock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003920
3921 if (!txqi->txq.sta)
3922 goto out;
3923
3924 if (list_empty(&txqi->schedule_order))
3925 goto out;
3926
3927 list_for_each_entry_safe(iter, tmp, &local->active_txqs[ac],
3928 schedule_order) {
3929 if (iter == txqi)
3930 break;
3931
3932 if (!iter->txq.sta) {
3933 list_move_tail(&iter->schedule_order,
3934 &local->active_txqs[ac]);
3935 continue;
3936 }
3937 sta = container_of(iter->txq.sta, struct sta_info, sta);
3938 if (sta->airtime[ac].deficit < 0)
3939 sta->airtime[ac].deficit += sta->airtime_weight;
3940 list_move_tail(&iter->schedule_order, &local->active_txqs[ac]);
3941 }
3942
3943 sta = container_of(txqi->txq.sta, struct sta_info, sta);
3944 if (sta->airtime[ac].deficit >= 0)
3945 goto out;
3946
3947 sta->airtime[ac].deficit += sta->airtime_weight;
3948 list_move_tail(&txqi->schedule_order, &local->active_txqs[ac]);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003949 spin_unlock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003950
3951 return false;
3952out:
3953 if (!list_empty(&txqi->schedule_order))
3954 list_del_init(&txqi->schedule_order);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003955 spin_unlock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003956
3957 return true;
3958}
3959EXPORT_SYMBOL(ieee80211_txq_may_transmit);
3960
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003961void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003962{
3963 struct ieee80211_local *local = hw_to_local(hw);
3964
3965 spin_lock_bh(&local->active_txq_lock[ac]);
3966 local->schedule_round[ac]++;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003967 spin_unlock_bh(&local->active_txq_lock[ac]);
3968}
Felix Fietkau5b989c12019-03-15 11:03:35 +01003969EXPORT_SYMBOL(ieee80211_txq_schedule_start);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003970
Felix Fietkau08a46c642021-06-17 18:31:11 +02003971static void
3972ieee80211_aggr_check(struct ieee80211_sub_if_data *sdata,
3973 struct sta_info *sta,
3974 struct sk_buff *skb)
3975{
3976 struct rate_control_ref *ref = sdata->local->rate_ctrl;
3977 u16 tid;
3978
3979 if (!ref || !(ref->ops->capa & RATE_CTRL_CAPA_AMPDU_TRIGGER))
3980 return;
3981
3982 if (!sta || !sta->sta.ht_cap.ht_supported ||
3983 !sta->sta.wme || skb_get_queue_mapping(skb) == IEEE80211_AC_VO ||
3984 skb->protocol == sdata->control_port_protocol)
3985 return;
3986
3987 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
3988 if (likely(sta->ampdu_mlme.tid_tx[tid]))
3989 return;
3990
3991 ieee80211_start_tx_ba_session(&sta->sta, tid, 0);
3992}
3993
Johannes Berg4c9451e2014-11-09 18:50:10 +02003994void __ieee80211_subif_start_xmit(struct sk_buff *skb,
3995 struct net_device *dev,
Rajkumar Manoharan06016772019-04-11 13:47:25 -07003996 u32 info_flags,
Markus Theila7528192020-05-27 18:03:34 +02003997 u32 ctrl_flags,
3998 u64 *cookie)
Johannes Berg4c9451e2014-11-09 18:50:10 +02003999{
4000 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Felix Fietkau1974da82019-03-25 08:59:23 +01004001 struct ieee80211_local *local = sdata->local;
Johannes Berg97ffe752015-03-21 09:13:45 +01004002 struct sta_info *sta;
Johannes Berg80616c02015-04-14 14:50:41 +02004003 struct sk_buff *next;
Johannes Berg4c9451e2014-11-09 18:50:10 +02004004
4005 if (unlikely(skb->len < ETH_HLEN)) {
4006 kfree_skb(skb);
4007 return;
4008 }
4009
4010 rcu_read_lock();
4011
Johannes Berg2d981fd2015-04-10 14:10:10 +02004012 if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
4013 goto out_free;
Johannes Berg4c9451e2014-11-09 18:50:10 +02004014
Felix Fietkau1974da82019-03-25 08:59:23 +01004015 if (IS_ERR(sta))
4016 sta = NULL;
4017
4018 if (local->ops->wake_tx_queue) {
4019 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
4020 skb_set_queue_mapping(skb, queue);
Felix Fietkau180ac482020-07-26 15:09:47 +02004021 skb_get_hash(skb);
Felix Fietkau1974da82019-03-25 08:59:23 +01004022 }
4023
Felix Fietkau08a46c642021-06-17 18:31:11 +02004024 ieee80211_aggr_check(sdata, sta, skb);
4025
Felix Fietkau1974da82019-03-25 08:59:23 +01004026 if (sta) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01004027 struct ieee80211_fast_tx *fast_tx;
4028
Wen Gong70e53662018-08-08 18:40:01 +08004029 sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
Toke Høiland-Jørgensen36148c22018-02-02 16:11:05 +01004030
Johannes Berg17c18bf2015-03-21 15:25:43 +01004031 fast_tx = rcu_dereference(sta->fast_tx);
4032
4033 if (fast_tx &&
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02004034 ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
Johannes Berg17c18bf2015-03-21 15:25:43 +01004035 goto out;
4036 }
4037
Johannes Berg80616c02015-04-14 14:50:41 +02004038 if (skb_is_gso(skb)) {
4039 struct sk_buff *segs;
Johannes Berg680a0da2015-04-13 16:58:25 +02004040
Johannes Berg80616c02015-04-14 14:50:41 +02004041 segs = skb_gso_segment(skb, 0);
4042 if (IS_ERR(segs)) {
Johannes Berg2d981fd2015-04-10 14:10:10 +02004043 goto out_free;
Johannes Berg80616c02015-04-14 14:50:41 +02004044 } else if (segs) {
4045 consume_skb(skb);
4046 skb = segs;
4047 }
4048 } else {
4049 /* we cannot process non-linear frames on this path */
4050 if (skb_linearize(skb)) {
4051 kfree_skb(skb);
4052 goto out;
4053 }
4054
4055 /* the frame could be fragmented, software-encrypted, and other
4056 * things so we cannot really handle checksum offload with it -
4057 * fix it up in software before we handle anything else.
4058 */
4059 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Johannes Bergf603f1f2015-05-05 15:25:33 +02004060 skb_set_transport_header(skb,
4061 skb_checksum_start_offset(skb));
Johannes Berg80616c02015-04-14 14:50:41 +02004062 if (skb_checksum_help(skb))
4063 goto out_free;
4064 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02004065 }
4066
Jason A. Donenfeld9f3ef3d2020-01-13 18:42:33 -05004067 skb_list_walk_safe(skb, skb, next) {
4068 skb_mark_not_on_list(skb);
Johannes Berg4c9451e2014-11-09 18:50:10 +02004069
Markus Theil5af7fef2020-06-17 10:26:36 +02004070 if (skb->protocol == sdata->control_port_protocol)
4071 ctrl_flags |= IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
4072
Rajkumar Manoharan06016772019-04-11 13:47:25 -07004073 skb = ieee80211_build_hdr(sdata, skb, info_flags,
Markus Theila7528192020-05-27 18:03:34 +02004074 sta, ctrl_flags, cookie);
Jason A. Donenfeld9f3ef3d2020-01-13 18:42:33 -05004075 if (IS_ERR(skb)) {
4076 kfree_skb_list(next);
Johannes Berg80616c02015-04-14 14:50:41 +02004077 goto out;
Jason A. Donenfeld9f3ef3d2020-01-13 18:42:33 -05004078 }
Johannes Berg80616c02015-04-14 14:50:41 +02004079
Lev Stipakov36ec1442020-11-13 23:46:24 +02004080 dev_sw_netstats_tx_add(dev, 1, skb->len);
Johannes Berg80616c02015-04-14 14:50:41 +02004081
Mathy Vanhoef08aca292020-07-23 14:01:52 +04004082 ieee80211_xmit(sdata, sta, skb);
Johannes Berg80616c02015-04-14 14:50:41 +02004083 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02004084 goto out;
4085 out_free:
4086 kfree_skb(skb);
Johannes Berg4c9451e2014-11-09 18:50:10 +02004087 out:
4088 rcu_read_unlock();
4089}
4090
Michael Braunebceec82016-11-22 11:52:18 +01004091static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
4092{
4093 struct ethhdr *eth;
4094 int err;
4095
4096 err = skb_ensure_writable(skb, ETH_HLEN);
4097 if (unlikely(err))
4098 return err;
4099
4100 eth = (void *)skb->data;
4101 ether_addr_copy(eth->h_dest, sta->sta.addr);
4102
4103 return 0;
4104}
4105
4106static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
4107 struct net_device *dev)
4108{
4109 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4110 const struct ethhdr *eth = (void *)skb->data;
4111 const struct vlan_ethhdr *ethvlan = (void *)skb->data;
4112 __be16 ethertype;
4113
4114 if (likely(!is_multicast_ether_addr(eth->h_dest)))
4115 return false;
4116
4117 switch (sdata->vif.type) {
4118 case NL80211_IFTYPE_AP_VLAN:
4119 if (sdata->u.vlan.sta)
4120 return false;
4121 if (sdata->wdev.use_4addr)
4122 return false;
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05004123 fallthrough;
Michael Braunebceec82016-11-22 11:52:18 +01004124 case NL80211_IFTYPE_AP:
4125 /* check runtime toggle for this bss */
4126 if (!sdata->bss->multicast_to_unicast)
4127 return false;
4128 break;
4129 default:
4130 return false;
4131 }
4132
4133 /* multicast to unicast conversion only for some payload */
4134 ethertype = eth->h_proto;
4135 if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
4136 ethertype = ethvlan->h_vlan_encapsulated_proto;
4137 switch (ethertype) {
4138 case htons(ETH_P_ARP):
4139 case htons(ETH_P_IP):
4140 case htons(ETH_P_IPV6):
4141 break;
4142 default:
4143 return false;
4144 }
4145
4146 return true;
4147}
4148
4149static void
4150ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
4151 struct sk_buff_head *queue)
4152{
4153 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4154 struct ieee80211_local *local = sdata->local;
4155 const struct ethhdr *eth = (struct ethhdr *)skb->data;
4156 struct sta_info *sta, *first = NULL;
4157 struct sk_buff *cloned_skb;
4158
4159 rcu_read_lock();
4160
4161 list_for_each_entry_rcu(sta, &local->sta_list, list) {
4162 if (sdata != sta->sdata)
4163 /* AP-VLAN mismatch */
4164 continue;
4165 if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
4166 /* do not send back to source */
4167 continue;
4168 if (!first) {
4169 first = sta;
4170 continue;
4171 }
4172 cloned_skb = skb_clone(skb, GFP_ATOMIC);
4173 if (!cloned_skb)
4174 goto multicast;
4175 if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
4176 dev_kfree_skb(cloned_skb);
4177 goto multicast;
4178 }
4179 __skb_queue_tail(queue, cloned_skb);
4180 }
4181
4182 if (likely(first)) {
4183 if (unlikely(ieee80211_change_da(skb, first)))
4184 goto multicast;
4185 __skb_queue_tail(queue, skb);
4186 } else {
4187 /* no STA connected, drop */
4188 kfree_skb(skb);
4189 skb = NULL;
4190 }
4191
4192 goto out;
4193multicast:
4194 __skb_queue_purge(queue);
4195 __skb_queue_tail(queue, skb);
4196out:
4197 rcu_read_unlock();
4198}
4199
Johannes Berg4c9451e2014-11-09 18:50:10 +02004200/**
4201 * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
4202 * @skb: packet to be sent
4203 * @dev: incoming interface
4204 *
4205 * On failure skb will be freed.
4206 */
Liad Kaufman24d342c2014-11-09 18:50:07 +02004207netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
4208 struct net_device *dev)
4209{
Michael Braunebceec82016-11-22 11:52:18 +01004210 if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
4211 struct sk_buff_head queue;
4212
4213 __skb_queue_head_init(&queue);
4214 ieee80211_convert_to_unicast(skb, dev, &queue);
4215 while ((skb = __skb_dequeue(&queue)))
Markus Theila7528192020-05-27 18:03:34 +02004216 __ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
Michael Braunebceec82016-11-22 11:52:18 +01004217 } else {
Markus Theila7528192020-05-27 18:03:34 +02004218 __ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
Michael Braunebceec82016-11-22 11:52:18 +01004219 }
4220
Liad Kaufman24d342c2014-11-09 18:50:07 +02004221 return NETDEV_TX_OK;
4222}
Johannes Berge2ebc742007-07-27 15:43:22 +02004223
John Crispin50ff4772019-11-25 11:04:37 +01004224static bool ieee80211_tx_8023(struct ieee80211_sub_if_data *sdata,
4225 struct sk_buff *skb, int led_len,
4226 struct sta_info *sta,
4227 bool txpending)
4228{
4229 struct ieee80211_local *local = sdata->local;
4230 struct ieee80211_tx_control control = {};
4231 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4232 struct ieee80211_sta *pubsta = NULL;
4233 unsigned long flags;
4234 int q = info->hw_queue;
4235
Lorenzo Bianconi196900f2021-03-08 23:01:49 +01004236 if (sta)
4237 sk_pacing_shift_update(skb->sk, local->hw.tx_sk_pacing_shift);
4238
John Crispin50ff4772019-11-25 11:04:37 +01004239 if (ieee80211_queue_skb(local, sdata, sta, skb))
4240 return true;
4241
4242 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4243
4244 if (local->queue_stop_reasons[q] ||
4245 (!txpending && !skb_queue_empty(&local->pending[q]))) {
4246 if (txpending)
4247 skb_queue_head(&local->pending[q], skb);
4248 else
4249 skb_queue_tail(&local->pending[q], skb);
4250
4251 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4252
4253 return false;
4254 }
4255
4256 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4257
4258 if (sta && sta->uploaded)
4259 pubsta = &sta->sta;
4260
4261 control.sta = pubsta;
4262
4263 drv_tx(local, &control, skb);
4264
4265 return true;
4266}
4267
4268static void ieee80211_8023_xmit(struct ieee80211_sub_if_data *sdata,
4269 struct net_device *dev, struct sta_info *sta,
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004270 struct ieee80211_key *key, struct sk_buff *skb)
John Crispin50ff4772019-11-25 11:04:37 +01004271{
4272 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
John Crispin50ff4772019-11-25 11:04:37 +01004273 struct ieee80211_local *local = sdata->local;
Felix Fietkau96ae9cd2020-09-08 14:36:50 +02004274 struct tid_ampdu_tx *tid_tx;
4275 u8 tid;
John Crispin50ff4772019-11-25 11:04:37 +01004276
Felix Fietkau5f8d69e2020-09-08 14:36:49 +02004277 if (local->ops->wake_tx_queue) {
4278 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
4279 skb_set_queue_mapping(skb, queue);
4280 skb_get_hash(skb);
4281 }
4282
John Crispin50ff4772019-11-25 11:04:37 +01004283 if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning)) &&
4284 test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
4285 goto out_free;
4286
Vasanthakumar Thiagarajane61fbfc2020-07-22 19:50:17 +05304287 memset(info, 0, sizeof(*info));
4288
Felix Fietkau08a46c642021-06-17 18:31:11 +02004289 ieee80211_aggr_check(sdata, sta, skb);
4290
Felix Fietkauaea6a3f2020-09-08 14:36:51 +02004291 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
4292 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
4293 if (tid_tx) {
4294 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
4295 /* fall back to non-offload slow path */
4296 __ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
4297 return;
Felix Fietkau96ae9cd2020-09-08 14:36:50 +02004298 }
Felix Fietkauaea6a3f2020-09-08 14:36:51 +02004299
4300 info->flags |= IEEE80211_TX_CTL_AMPDU;
4301 if (tid_tx->timeout)
4302 tid_tx->last_tx = jiffies;
Felix Fietkau96ae9cd2020-09-08 14:36:50 +02004303 }
4304
Felix Fietkauaea6a3f2020-09-08 14:36:51 +02004305 if (unlikely(skb->sk &&
John Crispin50ff4772019-11-25 11:04:37 +01004306 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS))
Vasanthakumar Thiagarajane61fbfc2020-07-22 19:50:17 +05304307 info->ack_frame_id = ieee80211_store_ack_skb(local, skb,
4308 &info->flags, NULL);
John Crispin50ff4772019-11-25 11:04:37 +01004309
John Crispin50ff4772019-11-25 11:04:37 +01004310 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
4311
Lev Stipakov36ec1442020-11-13 23:46:24 +02004312 dev_sw_netstats_tx_add(dev, 1, skb->len);
John Crispin50ff4772019-11-25 11:04:37 +01004313
Felix Fietkauaea6a3f2020-09-08 14:36:51 +02004314 sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
4315 sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
John Crispin50ff4772019-11-25 11:04:37 +01004316
4317 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4318 sdata = container_of(sdata->bss,
4319 struct ieee80211_sub_if_data, u.ap);
4320
Felix Fietkaucc20ff22020-09-08 14:36:57 +02004321 info->flags |= IEEE80211_TX_CTL_HW_80211_ENCAP;
John Crispin50ff4772019-11-25 11:04:37 +01004322 info->control.vif = &sdata->vif;
4323
Felix Fietkauae045152020-09-08 14:36:52 +02004324 if (key)
4325 info->control.hw_key = &key->conf;
4326
John Crispin50ff4772019-11-25 11:04:37 +01004327 ieee80211_tx_8023(sdata, skb, skb->len, sta, false);
4328
4329 return;
4330
4331out_free:
4332 kfree_skb(skb);
4333}
4334
4335netdev_tx_t ieee80211_subif_start_xmit_8023(struct sk_buff *skb,
4336 struct net_device *dev)
4337{
4338 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Felix Fietkauaea6a3f2020-09-08 14:36:51 +02004339 struct ethhdr *ehdr = (struct ethhdr *)skb->data;
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004340 struct ieee80211_key *key;
John Crispin50ff4772019-11-25 11:04:37 +01004341 struct sta_info *sta;
John Crispin50ff4772019-11-25 11:04:37 +01004342
4343 if (unlikely(skb->len < ETH_HLEN)) {
4344 kfree_skb(skb);
4345 return NETDEV_TX_OK;
4346 }
4347
4348 rcu_read_lock();
4349
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004350 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
John Crispin50ff4772019-11-25 11:04:37 +01004351 kfree_skb(skb);
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004352 goto out;
4353 }
John Crispin50ff4772019-11-25 11:04:37 +01004354
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004355 if (unlikely(IS_ERR_OR_NULL(sta) || !sta->uploaded ||
4356 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
Felix Fietkaub101dd22020-12-18 19:47:16 +01004357 sdata->control_port_protocol == ehdr->h_proto))
4358 goto skip_offload;
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004359
Felix Fietkaub101dd22020-12-18 19:47:16 +01004360 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4361 if (!key)
4362 key = rcu_dereference(sdata->default_unicast_key);
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004363
Felix Fietkaub101dd22020-12-18 19:47:16 +01004364 if (key && (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) ||
4365 key->conf.cipher == WLAN_CIPHER_SUITE_TKIP))
4366 goto skip_offload;
4367
4368 ieee80211_8023_xmit(sdata, dev, sta, key, skb);
4369 goto out;
4370
4371skip_offload:
4372 ieee80211_subif_start_xmit(skb, dev);
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004373out:
John Crispin50ff4772019-11-25 11:04:37 +01004374 rcu_read_unlock();
4375
4376 return NETDEV_TX_OK;
4377}
4378
Johannes Berg7528ec52014-11-09 18:50:11 +02004379struct sk_buff *
4380ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
4381 struct sk_buff *skb, u32 info_flags)
4382{
4383 struct ieee80211_hdr *hdr;
4384 struct ieee80211_tx_data tx = {
4385 .local = sdata->local,
4386 .sdata = sdata,
4387 };
Johannes Berg97ffe752015-03-21 09:13:45 +01004388 struct sta_info *sta;
Johannes Berg7528ec52014-11-09 18:50:11 +02004389
4390 rcu_read_lock();
4391
Johannes Berg97ffe752015-03-21 09:13:45 +01004392 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4393 kfree_skb(skb);
4394 skb = ERR_PTR(-EINVAL);
4395 goto out;
4396 }
4397
Markus Theila7528192020-05-27 18:03:34 +02004398 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta, 0, NULL);
Johannes Berg7528ec52014-11-09 18:50:11 +02004399 if (IS_ERR(skb))
4400 goto out;
4401
4402 hdr = (void *)skb->data;
4403 tx.sta = sta_info_get(sdata, hdr->addr1);
4404 tx.skb = skb;
4405
4406 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
4407 rcu_read_unlock();
4408 kfree_skb(skb);
4409 return ERR_PTR(-EINVAL);
4410 }
4411
4412out:
4413 rcu_read_unlock();
4414 return skb;
4415}
4416
Johannes Berge2530082008-05-17 00:57:14 +02004417/*
4418 * ieee80211_clear_tx_pending may not be called in a context where
4419 * it is possible that it packets could come in again.
4420 */
Johannes Berge2ebc742007-07-27 15:43:22 +02004421void ieee80211_clear_tx_pending(struct ieee80211_local *local)
4422{
Felix Fietkau1f98ab72012-11-10 03:44:14 +01004423 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01004424 int i;
Johannes Berge2ebc742007-07-27 15:43:22 +02004425
Felix Fietkau1f98ab72012-11-10 03:44:14 +01004426 for (i = 0; i < local->hw.queues; i++) {
4427 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
4428 ieee80211_free_txskb(&local->hw, skb);
4429 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004430}
4431
Johannes Berg7bb45682011-02-24 14:42:06 +01004432/*
4433 * Returns false if the frame couldn't be transmitted but was queued instead,
4434 * which in this case means re-queued -- take as an indication to stop sending
4435 * more pending frames.
4436 */
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004437static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
4438 struct sk_buff *skb)
4439{
4440 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4441 struct ieee80211_sub_if_data *sdata;
4442 struct sta_info *sta;
4443 struct ieee80211_hdr *hdr;
Johannes Berg7bb45682011-02-24 14:42:06 +01004444 bool result;
Johannes Berg55de9082012-07-26 17:24:39 +02004445 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004446
Johannes Berg5061b0c2009-07-14 00:33:34 +02004447 sdata = vif_to_sdata(info->control.vif);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004448
Felix Fietkaucc20ff22020-09-08 14:36:57 +02004449 if (info->control.flags & IEEE80211_TX_INTCFL_NEED_TXPROCESSING) {
Johannes Berg55de9082012-07-26 17:24:39 +02004450 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4451 if (unlikely(!chanctx_conf)) {
4452 dev_kfree_skb(skb);
4453 return true;
4454 }
Johannes Berg73c4e192014-11-09 18:50:09 +02004455 info->band = chanctx_conf->def.chan->band;
Mathy Vanhoef08aca292020-07-23 14:01:52 +04004456 result = ieee80211_tx(sdata, NULL, skb, true);
Felix Fietkaucc20ff22020-09-08 14:36:57 +02004457 } else if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
John Crispin50ff4772019-11-25 11:04:37 +01004458 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4459 dev_kfree_skb(skb);
4460 return true;
4461 }
4462
4463 if (IS_ERR(sta) || (sta && !sta->uploaded))
4464 sta = NULL;
4465
4466 result = ieee80211_tx_8023(sdata, skb, skb->len, sta, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004467 } else {
Johannes Berg252b86c2011-11-16 15:28:55 +01004468 struct sk_buff_head skbs;
4469
4470 __skb_queue_head_init(&skbs);
4471 __skb_queue_tail(&skbs, skb);
4472
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004473 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Bergabe60632009-11-25 17:46:18 +01004474 sta = sta_info_get(sdata, hdr->addr1);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004475
Johannes Berg74e4dbf2011-11-16 15:28:57 +01004476 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004477 }
4478
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004479 return result;
4480}
4481
Johannes Berge2530082008-05-17 00:57:14 +02004482/*
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004483 * Transmit all pending packets. Called from tasklet.
Johannes Berge2530082008-05-17 00:57:14 +02004484 */
Allen Paisda1cad72020-11-03 14:48:18 +05304485void ieee80211_tx_pending(struct tasklet_struct *t)
Johannes Berge2ebc742007-07-27 15:43:22 +02004486{
Allen Paisda1cad72020-11-03 14:48:18 +05304487 struct ieee80211_local *local = from_tasklet(local, t,
4488 tx_pending_tasklet);
Johannes Berg2a577d92009-03-23 17:28:37 +01004489 unsigned long flags;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004490 int i;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004491 bool txok;
Johannes Berge2ebc742007-07-27 15:43:22 +02004492
Johannes Bergf0e72852009-03-23 17:28:36 +01004493 rcu_read_lock();
Johannes Berg2a577d92009-03-23 17:28:37 +01004494
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004495 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg176be722009-03-12 23:49:28 +01004496 for (i = 0; i < local->hw.queues; i++) {
Johannes Berg2a577d92009-03-23 17:28:37 +01004497 /*
4498 * If queue is stopped by something other than due to pending
4499 * frames, or we have no pending frames, proceed to next queue.
4500 */
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004501 if (local->queue_stop_reasons[i] ||
Johannes Berg2a577d92009-03-23 17:28:37 +01004502 skb_queue_empty(&local->pending[i]))
Johannes Berge2ebc742007-07-27 15:43:22 +02004503 continue;
Johannes Berge2530082008-05-17 00:57:14 +02004504
Johannes Berg2a577d92009-03-23 17:28:37 +01004505 while (!skb_queue_empty(&local->pending[i])) {
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004506 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
Johannes Berg5061b0c2009-07-14 00:33:34 +02004507 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg5061b0c2009-07-14 00:33:34 +02004508
Johannes Berga7bc3762009-07-27 10:33:31 +02004509 if (WARN_ON(!info->control.vif)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02004510 ieee80211_free_txskb(&local->hw, skb);
Johannes Berga7bc3762009-07-27 10:33:31 +02004511 continue;
4512 }
4513
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004514 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
4515 flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01004516
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004517 txok = ieee80211_tx_pending_skb(local, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004518 spin_lock_irqsave(&local->queue_stop_reason_lock,
4519 flags);
4520 if (!txok)
Johannes Berg2a577d92009-03-23 17:28:37 +01004521 break;
Johannes Berge2ebc742007-07-27 15:43:22 +02004522 }
Johannes Berg7236fe22010-03-22 13:42:43 -07004523
4524 if (skb_queue_empty(&local->pending[i]))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02004525 ieee80211_propagate_queue_wake(local, i);
Johannes Berge2ebc742007-07-27 15:43:22 +02004526 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004527 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01004528
Johannes Bergf0e72852009-03-23 17:28:36 +01004529 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004530}
4531
4532/* functions for drivers to get certain frames */
4533
Marco Porscheac70c12013-01-07 16:04:50 +01004534static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004535 struct ps_data *ps, struct sk_buff *skb,
4536 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02004537{
4538 u8 *pos, *tim;
4539 int aid0 = 0;
4540 int i, have_bits = 0, n1, n2;
4541
4542 /* Generate bitmap for TIM only if there are any STAs in power save
4543 * mode. */
Marco Porschd012a602012-10-10 12:39:50 -07004544 if (atomic_read(&ps->num_sta_ps) > 0)
Johannes Berge2ebc742007-07-27 15:43:22 +02004545 /* in the hope that this is faster than
4546 * checking byte-for-byte */
Weilong Chenf359d3f2013-12-18 15:44:16 +08004547 have_bits = !bitmap_empty((unsigned long *)ps->tim,
Johannes Berge2ebc742007-07-27 15:43:22 +02004548 IEEE80211_MAX_AID+1);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004549 if (!is_template) {
4550 if (ps->dtim_count == 0)
4551 ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
4552 else
4553 ps->dtim_count--;
4554 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004555
Johannes Berg4df864c2017-06-16 14:29:21 +02004556 tim = pos = skb_put(skb, 6);
Johannes Berge2ebc742007-07-27 15:43:22 +02004557 *pos++ = WLAN_EID_TIM;
4558 *pos++ = 4;
Marco Porschd012a602012-10-10 12:39:50 -07004559 *pos++ = ps->dtim_count;
Johannes Berg88600202012-02-13 15:17:18 +01004560 *pos++ = sdata->vif.bss_conf.dtim_period;
Johannes Berge2ebc742007-07-27 15:43:22 +02004561
Marco Porschd012a602012-10-10 12:39:50 -07004562 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
Johannes Berge2ebc742007-07-27 15:43:22 +02004563 aid0 = 1;
4564
Marco Porschd012a602012-10-10 12:39:50 -07004565 ps->dtim_bc_mc = aid0 == 1;
Christian Lamparter512119b2011-01-31 20:48:44 +02004566
Johannes Berge2ebc742007-07-27 15:43:22 +02004567 if (have_bits) {
4568 /* Find largest even number N1 so that bits numbered 1 through
4569 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
4570 * (N2 + 1) x 8 through 2007 are 0. */
4571 n1 = 0;
4572 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
Marco Porschd012a602012-10-10 12:39:50 -07004573 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004574 n1 = i & 0xfe;
4575 break;
4576 }
4577 }
4578 n2 = n1;
4579 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
Marco Porschd012a602012-10-10 12:39:50 -07004580 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004581 n2 = i;
4582 break;
4583 }
4584 }
4585
4586 /* Bitmap control */
4587 *pos++ = n1 | aid0;
4588 /* Part Virt Bitmap */
Eliad Peller5220da32011-11-24 16:50:00 +02004589 skb_put(skb, n2 - n1);
Marco Porschd012a602012-10-10 12:39:50 -07004590 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
Johannes Berge2ebc742007-07-27 15:43:22 +02004591
4592 tim[1] = n2 - n1 + 4;
Johannes Berge2ebc742007-07-27 15:43:22 +02004593 } else {
4594 *pos++ = aid0; /* Bitmap control */
4595 *pos++ = 0; /* Part Virt Bitmap */
4596 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004597}
4598
Marco Porscheac70c12013-01-07 16:04:50 +01004599static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004600 struct ps_data *ps, struct sk_buff *skb,
4601 bool is_template)
Marco Porscheac70c12013-01-07 16:04:50 +01004602{
4603 struct ieee80211_local *local = sdata->local;
4604
4605 /*
4606 * Not very nice, but we want to allow the driver to call
4607 * ieee80211_beacon_get() as a response to the set_tim()
4608 * callback. That, however, is already invoked under the
4609 * sta_lock to guarantee consistent and race-free update
4610 * of the tim bitmap in mac80211 and the driver.
4611 */
4612 if (local->tim_in_locked_section) {
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004613 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Marco Porscheac70c12013-01-07 16:04:50 +01004614 } else {
Johannes Berg1b917312013-02-22 12:55:01 +01004615 spin_lock_bh(&local->tim_lock);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004616 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Johannes Berg1b917312013-02-22 12:55:01 +01004617 spin_unlock_bh(&local->tim_lock);
Marco Porscheac70c12013-01-07 16:04:50 +01004618 }
4619
4620 return 0;
4621}
4622
John Crispin8552a432020-08-11 10:01:04 +02004623static void ieee80211_set_beacon_cntdwn(struct ieee80211_sub_if_data *sdata,
4624 struct beacon_data *beacon)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004625{
4626 struct probe_resp *resp;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004627 u8 *beacon_data;
4628 size_t beacon_data_len;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004629 int i;
John Crispin8552a432020-08-11 10:01:04 +02004630 u8 count = beacon->cntdwn_current_counter;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004631
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004632 switch (sdata->vif.type) {
4633 case NL80211_IFTYPE_AP:
4634 beacon_data = beacon->tail;
4635 beacon_data_len = beacon->tail_len;
4636 break;
4637 case NL80211_IFTYPE_ADHOC:
4638 beacon_data = beacon->head;
4639 beacon_data_len = beacon->head_len;
4640 break;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004641 case NL80211_IFTYPE_MESH_POINT:
4642 beacon_data = beacon->head;
4643 beacon_data_len = beacon->head_len;
4644 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004645 default:
4646 return;
4647 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004648
Michal Kazioraf296bd2014-06-05 14:21:36 +02004649 rcu_read_lock();
John Crispin8552a432020-08-11 10:01:04 +02004650 for (i = 0; i < IEEE80211_MAX_CNTDWN_COUNTERS_NUM; ++i) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004651 resp = rcu_dereference(sdata->u.ap.probe_resp);
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004652
John Crispin8552a432020-08-11 10:01:04 +02004653 if (beacon->cntdwn_counter_offsets[i]) {
4654 if (WARN_ON_ONCE(beacon->cntdwn_counter_offsets[i] >=
Michal Kazioraf296bd2014-06-05 14:21:36 +02004655 beacon_data_len)) {
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004656 rcu_read_unlock();
4657 return;
4658 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004659
John Crispin8552a432020-08-11 10:01:04 +02004660 beacon_data[beacon->cntdwn_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004661 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004662
4663 if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
John Crispin8552a432020-08-11 10:01:04 +02004664 resp->data[resp->cntdwn_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004665 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004666 rcu_read_unlock();
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004667}
4668
John Crispin8552a432020-08-11 10:01:04 +02004669static u8 __ieee80211_beacon_update_cntdwn(struct beacon_data *beacon)
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004670{
John Crispin8552a432020-08-11 10:01:04 +02004671 beacon->cntdwn_current_counter--;
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004672
4673 /* the counter should never reach 0 */
John Crispin8552a432020-08-11 10:01:04 +02004674 WARN_ON_ONCE(!beacon->cntdwn_current_counter);
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004675
John Crispin8552a432020-08-11 10:01:04 +02004676 return beacon->cntdwn_current_counter;
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004677}
4678
John Crispin8552a432020-08-11 10:01:04 +02004679u8 ieee80211_beacon_update_cntdwn(struct ieee80211_vif *vif)
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004680{
4681 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004682 struct beacon_data *beacon = NULL;
4683 u8 count = 0;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004684
Michal Kazioraf296bd2014-06-05 14:21:36 +02004685 rcu_read_lock();
4686
4687 if (sdata->vif.type == NL80211_IFTYPE_AP)
4688 beacon = rcu_dereference(sdata->u.ap.beacon);
4689 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4690 beacon = rcu_dereference(sdata->u.ibss.presp);
4691 else if (ieee80211_vif_is_mesh(&sdata->vif))
4692 beacon = rcu_dereference(sdata->u.mesh.beacon);
4693
4694 if (!beacon)
4695 goto unlock;
4696
John Crispin8552a432020-08-11 10:01:04 +02004697 count = __ieee80211_beacon_update_cntdwn(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004698
Michal Kazioraf296bd2014-06-05 14:21:36 +02004699unlock:
4700 rcu_read_unlock();
4701 return count;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004702}
John Crispin8552a432020-08-11 10:01:04 +02004703EXPORT_SYMBOL(ieee80211_beacon_update_cntdwn);
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004704
John Crispin8552a432020-08-11 10:01:04 +02004705void ieee80211_beacon_set_cntdwn(struct ieee80211_vif *vif, u8 counter)
Gregory Greenman03737002018-04-20 13:49:24 +03004706{
4707 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4708 struct beacon_data *beacon = NULL;
4709
4710 rcu_read_lock();
4711
4712 if (sdata->vif.type == NL80211_IFTYPE_AP)
4713 beacon = rcu_dereference(sdata->u.ap.beacon);
4714 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4715 beacon = rcu_dereference(sdata->u.ibss.presp);
4716 else if (ieee80211_vif_is_mesh(&sdata->vif))
4717 beacon = rcu_dereference(sdata->u.mesh.beacon);
4718
4719 if (!beacon)
4720 goto unlock;
4721
John Crispin8552a432020-08-11 10:01:04 +02004722 if (counter < beacon->cntdwn_current_counter)
4723 beacon->cntdwn_current_counter = counter;
Gregory Greenman03737002018-04-20 13:49:24 +03004724
4725unlock:
4726 rcu_read_unlock();
4727}
John Crispin8552a432020-08-11 10:01:04 +02004728EXPORT_SYMBOL(ieee80211_beacon_set_cntdwn);
Gregory Greenman03737002018-04-20 13:49:24 +03004729
John Crispin8552a432020-08-11 10:01:04 +02004730bool ieee80211_beacon_cntdwn_is_complete(struct ieee80211_vif *vif)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004731{
4732 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4733 struct beacon_data *beacon = NULL;
4734 u8 *beacon_data;
4735 size_t beacon_data_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004736 int ret = false;
4737
4738 if (!ieee80211_sdata_running(sdata))
4739 return false;
4740
4741 rcu_read_lock();
4742 if (vif->type == NL80211_IFTYPE_AP) {
4743 struct ieee80211_if_ap *ap = &sdata->u.ap;
4744
4745 beacon = rcu_dereference(ap->beacon);
4746 if (WARN_ON(!beacon || !beacon->tail))
4747 goto out;
4748 beacon_data = beacon->tail;
4749 beacon_data_len = beacon->tail_len;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004750 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
4751 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4752
4753 beacon = rcu_dereference(ifibss->presp);
4754 if (!beacon)
4755 goto out;
4756
4757 beacon_data = beacon->head;
4758 beacon_data_len = beacon->head_len;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004759 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
4760 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4761
4762 beacon = rcu_dereference(ifmsh->beacon);
4763 if (!beacon)
4764 goto out;
4765
4766 beacon_data = beacon->head;
4767 beacon_data_len = beacon->head_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004768 } else {
4769 WARN_ON(1);
4770 goto out;
4771 }
4772
John Crispin8552a432020-08-11 10:01:04 +02004773 if (!beacon->cntdwn_counter_offsets[0])
Michal Kazior10d78f22014-06-05 14:21:37 +02004774 goto out;
4775
John Crispin8552a432020-08-11 10:01:04 +02004776 if (WARN_ON_ONCE(beacon->cntdwn_counter_offsets[0] > beacon_data_len))
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004777 goto out;
4778
John Crispin8552a432020-08-11 10:01:04 +02004779 if (beacon_data[beacon->cntdwn_counter_offsets[0]] == 1)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004780 ret = true;
John Crispin8552a432020-08-11 10:01:04 +02004781
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004782 out:
4783 rcu_read_unlock();
4784
4785 return ret;
4786}
John Crispin8552a432020-08-11 10:01:04 +02004787EXPORT_SYMBOL(ieee80211_beacon_cntdwn_is_complete);
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004788
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004789static int ieee80211_beacon_protect(struct sk_buff *skb,
4790 struct ieee80211_local *local,
4791 struct ieee80211_sub_if_data *sdata)
4792{
4793 ieee80211_tx_result res;
4794 struct ieee80211_tx_data tx;
Johannes Berg95247702020-03-20 10:20:23 +01004795 struct sk_buff *check_skb;
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004796
4797 memset(&tx, 0, sizeof(tx));
4798 tx.key = rcu_dereference(sdata->default_beacon_key);
4799 if (!tx.key)
4800 return 0;
4801 tx.local = local;
4802 tx.sdata = sdata;
4803 __skb_queue_head_init(&tx.skbs);
4804 __skb_queue_tail(&tx.skbs, skb);
4805 res = ieee80211_tx_h_encrypt(&tx);
Johannes Berg95247702020-03-20 10:20:23 +01004806 check_skb = __skb_dequeue(&tx.skbs);
4807 /* we may crash after this, but it'd be a bug in crypto */
4808 WARN_ON(check_skb != skb);
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004809 if (WARN_ON_ONCE(res != TX_CONTINUE))
Johannes Berg95247702020-03-20 10:20:23 +01004810 return -EINVAL;
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004811
4812 return 0;
4813}
4814
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004815static struct sk_buff *
4816__ieee80211_beacon_get(struct ieee80211_hw *hw,
4817 struct ieee80211_vif *vif,
4818 struct ieee80211_mutable_offsets *offs,
4819 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02004820{
4821 struct ieee80211_local *local = hw_to_local(hw);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004822 struct beacon_data *beacon = NULL;
Johannes Berg9d139c82008-07-09 14:40:37 +02004823 struct sk_buff *skb = NULL;
Johannes Berge039fa42008-05-15 12:55:29 +02004824 struct ieee80211_tx_info *info;
Johannes Berge2ebc742007-07-27 15:43:22 +02004825 struct ieee80211_sub_if_data *sdata = NULL;
Johannes Berg57fbcce2016-04-12 15:56:15 +02004826 enum nl80211_band band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004827 struct ieee80211_tx_rate_control txrc;
Johannes Berg55de9082012-07-26 17:24:39 +02004828 struct ieee80211_chanctx_conf *chanctx_conf;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004829 int csa_off_base = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004830
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004831 rcu_read_lock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004832
Johannes Berg32bfd352007-12-19 01:31:26 +01004833 sdata = vif_to_sdata(vif);
Johannes Berg55de9082012-07-26 17:24:39 +02004834 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berge2ebc742007-07-27 15:43:22 +02004835
Johannes Berg55de9082012-07-26 17:24:39 +02004836 if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
Felix Fietkaueb3e5542011-01-24 19:28:49 +01004837 goto out;
4838
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004839 if (offs)
4840 memset(offs, 0, sizeof(*offs));
Johannes Bergeddcbb942009-10-29 08:30:35 +01004841
Johannes Berg05c914f2008-09-11 00:01:58 +02004842 if (sdata->vif.type == NL80211_IFTYPE_AP) {
Marco Porschd012a602012-10-10 12:39:50 -07004843 struct ieee80211_if_ap *ap = &sdata->u.ap;
Marco Porschd012a602012-10-10 12:39:50 -07004844
Michal Kazioraf296bd2014-06-05 14:21:36 +02004845 beacon = rcu_dereference(ap->beacon);
Dan Carpenter3ad97fb2011-02-07 22:03:35 +03004846 if (beacon) {
John Crispin8552a432020-08-11 10:01:04 +02004847 if (beacon->cntdwn_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004848 if (!is_template)
John Crispin8552a432020-08-11 10:01:04 +02004849 ieee80211_beacon_update_cntdwn(vif);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004850
John Crispin8552a432020-08-11 10:01:04 +02004851 ieee80211_set_beacon_cntdwn(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004852 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004853
Johannes Berg902acc72008-02-23 15:17:19 +01004854 /*
4855 * headroom, head length,
4856 * tail length and maximum TIM length
4857 */
4858 skb = dev_alloc_skb(local->tx_headroom +
4859 beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004860 beacon->tail_len + 256 +
4861 local->hw.extra_beacon_tailroom);
Johannes Berg902acc72008-02-23 15:17:19 +01004862 if (!skb)
4863 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004864
Johannes Berg902acc72008-02-23 15:17:19 +01004865 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004866 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg902acc72008-02-23 15:17:19 +01004867
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004868 ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
4869 is_template);
Johannes Berg902acc72008-02-23 15:17:19 +01004870
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004871 if (offs) {
4872 offs->tim_offset = beacon->head_len;
4873 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004874
4875 /* for AP the csa offsets are from tail */
4876 csa_off_base = skb->len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004877 }
Johannes Bergeddcbb942009-10-29 08:30:35 +01004878
Johannes Berg902acc72008-02-23 15:17:19 +01004879 if (beacon->tail)
Johannes Berg59ae1d12017-06-16 14:29:20 +02004880 skb_put_data(skb, beacon->tail,
4881 beacon->tail_len);
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004882
4883 if (ieee80211_beacon_protect(skb, local, sdata) < 0)
4884 goto out;
Johannes Berg9d139c82008-07-09 14:40:37 +02004885 } else
4886 goto out;
Johannes Berg05c914f2008-09-11 00:01:58 +02004887 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Berg46900292009-02-15 12:44:28 +01004888 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg9d139c82008-07-09 14:40:37 +02004889 struct ieee80211_hdr *hdr;
Johannes Berg902acc72008-02-23 15:17:19 +01004890
Michal Kazioraf296bd2014-06-05 14:21:36 +02004891 beacon = rcu_dereference(ifibss->presp);
4892 if (!beacon)
Johannes Berg9d139c82008-07-09 14:40:37 +02004893 goto out;
4894
John Crispin8552a432020-08-11 10:01:04 +02004895 if (beacon->cntdwn_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004896 if (!is_template)
John Crispin8552a432020-08-11 10:01:04 +02004897 __ieee80211_beacon_update_cntdwn(beacon);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004898
John Crispin8552a432020-08-11 10:01:04 +02004899 ieee80211_set_beacon_cntdwn(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004900 }
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004901
Michal Kazioraf296bd2014-06-05 14:21:36 +02004902 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004903 local->hw.extra_beacon_tailroom);
Johannes Berg9d139c82008-07-09 14:40:37 +02004904 if (!skb)
4905 goto out;
Johannes Bergc3ffeab2013-03-07 20:54:29 +01004906 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004907 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004908
4909 hdr = (struct ieee80211_hdr *) skb->data;
Harvey Harrisone7827a72008-07-15 18:44:13 -07004910 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4911 IEEE80211_STYPE_BEACON);
Johannes Berg902acc72008-02-23 15:17:19 +01004912 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
Javier Cardonadbf498f2012-03-31 11:31:32 -07004913 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg472dbc42008-09-11 00:01:49 +02004914
Michal Kazioraf296bd2014-06-05 14:21:36 +02004915 beacon = rcu_dereference(ifmsh->beacon);
4916 if (!beacon)
Johannes Bergac1bd842011-01-18 13:45:32 +01004917 goto out;
Johannes Bergac1bd842011-01-18 13:45:32 +01004918
John Crispin8552a432020-08-11 10:01:04 +02004919 if (beacon->cntdwn_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004920 if (!is_template)
4921 /* TODO: For mesh csa_counter is in TU, so
4922 * decrementing it by one isn't correct, but
4923 * for now we leave it consistent with overall
4924 * mac80211's behavior.
4925 */
John Crispin8552a432020-08-11 10:01:04 +02004926 __ieee80211_beacon_update_cntdwn(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004927
John Crispin8552a432020-08-11 10:01:04 +02004928 ieee80211_set_beacon_cntdwn(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004929 }
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004930
Javier Cardonadbf498f2012-03-31 11:31:32 -07004931 if (ifmsh->sync_ops)
Masashi Honma445cd452016-12-08 10:15:51 +09004932 ifmsh->sync_ops->adjust_tsf(sdata, beacon);
Javier Cardonadbf498f2012-03-31 11:31:32 -07004933
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -07004934 skb = dev_alloc_skb(local->tx_headroom +
Michal Kazioraf296bd2014-06-05 14:21:36 +02004935 beacon->head_len +
Marco Porsch3f52b7e2013-01-30 18:14:08 +01004936 256 + /* TIM IE */
Michal Kazioraf296bd2014-06-05 14:21:36 +02004937 beacon->tail_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004938 local->hw.extra_beacon_tailroom);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01004939 if (!skb)
4940 goto out;
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08004941 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004942 skb_put_data(skb, beacon->head, beacon->head_len);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004943 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
4944
4945 if (offs) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004946 offs->tim_offset = beacon->head_len;
4947 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004948 }
4949
Johannes Berg59ae1d12017-06-16 14:29:20 +02004950 skb_put_data(skb, beacon->tail, beacon->tail_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004951 } else {
4952 WARN_ON(1);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004953 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02004954 }
4955
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004956 /* CSA offsets */
Michal Kazioraf296bd2014-06-05 14:21:36 +02004957 if (offs && beacon) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004958 int i;
4959
John Crispin8552a432020-08-11 10:01:04 +02004960 for (i = 0; i < IEEE80211_MAX_CNTDWN_COUNTERS_NUM; i++) {
4961 u16 csa_off = beacon->cntdwn_counter_offsets[i];
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004962
4963 if (!csa_off)
4964 continue;
4965
John Crispin8552a432020-08-11 10:01:04 +02004966 offs->cntdwn_counter_offs[i] = csa_off_base + csa_off;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004967 }
4968 }
4969
Johannes Berg4bf88532012-11-09 11:39:59 +01004970 band = chanctx_conf->def.chan->band;
Johannes Berg55de9082012-07-26 17:24:39 +02004971
Johannes Berge039fa42008-05-15 12:55:29 +02004972 info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02004973
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004974 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004975 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Johannes Berge039fa42008-05-15 12:55:29 +02004976 info->band = band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004977
4978 memset(&txrc, 0, sizeof(txrc));
4979 txrc.hw = hw;
Johannes Berge31583c2012-07-26 14:07:46 +02004980 txrc.sband = local->hw.wiphy->bands[band];
Jouni Malinene00cfce2009-12-29 12:59:19 +02004981 txrc.bss_conf = &sdata->vif.bss_conf;
4982 txrc.skb = skb;
4983 txrc.reported_rate.idx = -1;
Jouni Malinen08fad432020-04-25 18:57:12 +03004984 if (sdata->beacon_rate_set && sdata->beacon_rateidx_mask[band])
4985 txrc.rate_idx_mask = sdata->beacon_rateidx_mask[band];
4986 else
4987 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
Felix Fietkau8f0729b2010-11-11 15:07:23 +01004988 txrc.bss = true;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004989 rate_control_get_rate(sdata, NULL, &txrc);
Johannes Berge039fa42008-05-15 12:55:29 +02004990
4991 info->control.vif = vif;
Johannes Bergf591fa52008-07-10 11:21:26 +02004992
John W. Linvillea472e712010-05-06 14:45:17 -04004993 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
4994 IEEE80211_TX_CTL_ASSIGN_SEQ |
4995 IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge6a98542008-10-21 12:40:02 +02004996 out:
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004997 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004998 return skb;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004999
5000}
5001
5002struct sk_buff *
5003ieee80211_beacon_get_template(struct ieee80211_hw *hw,
5004 struct ieee80211_vif *vif,
5005 struct ieee80211_mutable_offsets *offs)
5006{
5007 return __ieee80211_beacon_get(hw, vif, offs, true);
5008}
5009EXPORT_SYMBOL(ieee80211_beacon_get_template);
5010
5011struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
5012 struct ieee80211_vif *vif,
5013 u16 *tim_offset, u16 *tim_length)
5014{
5015 struct ieee80211_mutable_offsets offs = {};
5016 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
Helmut Schaa35afa582015-09-09 09:46:32 +02005017 struct sk_buff *copy;
5018 struct ieee80211_supported_band *sband;
5019 int shift;
5020
5021 if (!bcn)
5022 return bcn;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03005023
5024 if (tim_offset)
5025 *tim_offset = offs.tim_offset;
5026
5027 if (tim_length)
5028 *tim_length = offs.tim_length;
5029
Helmut Schaa35afa582015-09-09 09:46:32 +02005030 if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
5031 !hw_to_local(hw)->monitors)
5032 return bcn;
5033
5034 /* send a copy to monitor interfaces */
5035 copy = skb_copy(bcn, GFP_ATOMIC);
5036 if (!copy)
5037 return bcn;
5038
5039 shift = ieee80211_vif_get_shift(vif);
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05305040 sband = ieee80211_get_sband(vif_to_sdata(vif));
5041 if (!sband)
5042 return bcn;
5043
John Crispinb7b2e8c2019-07-14 17:44:15 +02005044 ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false,
5045 NULL);
Helmut Schaa35afa582015-09-09 09:46:32 +02005046
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03005047 return bcn;
Johannes Berge2ebc742007-07-27 15:43:22 +02005048}
Johannes Bergeddcbb942009-10-29 08:30:35 +01005049EXPORT_SYMBOL(ieee80211_beacon_get_tim);
Johannes Berge2ebc742007-07-27 15:43:22 +02005050
Arik Nemtsov02945822011-11-10 11:28:57 +02005051struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
5052 struct ieee80211_vif *vif)
5053{
5054 struct ieee80211_if_ap *ap = NULL;
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03005055 struct sk_buff *skb = NULL;
5056 struct probe_resp *presp = NULL;
Arik Nemtsov02945822011-11-10 11:28:57 +02005057 struct ieee80211_hdr *hdr;
5058 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5059
5060 if (sdata->vif.type != NL80211_IFTYPE_AP)
5061 return NULL;
5062
5063 rcu_read_lock();
5064
5065 ap = &sdata->u.ap;
5066 presp = rcu_dereference(ap->probe_resp);
5067 if (!presp)
5068 goto out;
5069
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03005070 skb = dev_alloc_skb(presp->len);
Arik Nemtsov02945822011-11-10 11:28:57 +02005071 if (!skb)
5072 goto out;
5073
Johannes Berg59ae1d12017-06-16 14:29:20 +02005074 skb_put_data(skb, presp->data, presp->len);
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03005075
Arik Nemtsov02945822011-11-10 11:28:57 +02005076 hdr = (struct ieee80211_hdr *) skb->data;
5077 memset(hdr->addr1, 0, sizeof(hdr->addr1));
5078
5079out:
5080 rcu_read_unlock();
5081 return skb;
5082}
5083EXPORT_SYMBOL(ieee80211_proberesp_get);
5084
Aloka Dixit295b02c2020-09-11 00:05:31 +00005085struct sk_buff *ieee80211_get_fils_discovery_tmpl(struct ieee80211_hw *hw,
5086 struct ieee80211_vif *vif)
5087{
5088 struct sk_buff *skb = NULL;
5089 struct fils_discovery_data *tmpl = NULL;
5090 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5091
5092 if (sdata->vif.type != NL80211_IFTYPE_AP)
5093 return NULL;
5094
5095 rcu_read_lock();
5096 tmpl = rcu_dereference(sdata->u.ap.fils_discovery);
5097 if (!tmpl) {
5098 rcu_read_unlock();
5099 return NULL;
5100 }
5101
5102 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
5103 if (skb) {
5104 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
5105 skb_put_data(skb, tmpl->data, tmpl->len);
5106 }
5107
5108 rcu_read_unlock();
5109 return skb;
5110}
5111EXPORT_SYMBOL(ieee80211_get_fils_discovery_tmpl);
5112
Aloka Dixit632189a2020-09-11 00:33:01 +00005113struct sk_buff *
5114ieee80211_get_unsol_bcast_probe_resp_tmpl(struct ieee80211_hw *hw,
5115 struct ieee80211_vif *vif)
5116{
5117 struct sk_buff *skb = NULL;
5118 struct unsol_bcast_probe_resp_data *tmpl = NULL;
5119 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5120
5121 if (sdata->vif.type != NL80211_IFTYPE_AP)
5122 return NULL;
5123
5124 rcu_read_lock();
5125 tmpl = rcu_dereference(sdata->u.ap.unsol_bcast_probe_resp);
5126 if (!tmpl) {
5127 rcu_read_unlock();
5128 return NULL;
5129 }
5130
5131 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
5132 if (skb) {
5133 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
5134 skb_put_data(skb, tmpl->data, tmpl->len);
5135 }
5136
5137 rcu_read_unlock();
5138 return skb;
5139}
5140EXPORT_SYMBOL(ieee80211_get_unsol_bcast_probe_resp_tmpl);
5141
Kalle Valo7044cc52010-01-05 20:16:19 +02005142struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
5143 struct ieee80211_vif *vif)
5144{
5145 struct ieee80211_sub_if_data *sdata;
5146 struct ieee80211_if_managed *ifmgd;
5147 struct ieee80211_pspoll *pspoll;
5148 struct ieee80211_local *local;
5149 struct sk_buff *skb;
5150
5151 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5152 return NULL;
5153
5154 sdata = vif_to_sdata(vif);
5155 ifmgd = &sdata->u.mgd;
5156 local = sdata->local;
5157
5158 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
Joe Perchesd15b8452011-08-29 14:17:31 -07005159 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02005160 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07005161
Kalle Valo7044cc52010-01-05 20:16:19 +02005162 skb_reserve(skb, local->hw.extra_tx_headroom);
5163
Johannes Bergb080db52017-06-16 14:29:19 +02005164 pspoll = skb_put_zero(skb, sizeof(*pspoll));
Kalle Valo7044cc52010-01-05 20:16:19 +02005165 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
5166 IEEE80211_STYPE_PSPOLL);
Johannes Berg1db364c2020-04-17 12:38:04 +02005167 pspoll->aid = cpu_to_le16(sdata->vif.bss_conf.aid);
Kalle Valo7044cc52010-01-05 20:16:19 +02005168
5169 /* aid in PS-Poll has its two MSBs each set to 1 */
5170 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
5171
5172 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
5173 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
5174
5175 return skb;
5176}
5177EXPORT_SYMBOL(ieee80211_pspoll_get);
5178
5179struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005180 struct ieee80211_vif *vif,
5181 bool qos_ok)
Kalle Valo7044cc52010-01-05 20:16:19 +02005182{
5183 struct ieee80211_hdr_3addr *nullfunc;
5184 struct ieee80211_sub_if_data *sdata;
5185 struct ieee80211_if_managed *ifmgd;
5186 struct ieee80211_local *local;
5187 struct sk_buff *skb;
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005188 bool qos = false;
Kalle Valo7044cc52010-01-05 20:16:19 +02005189
5190 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5191 return NULL;
5192
5193 sdata = vif_to_sdata(vif);
5194 ifmgd = &sdata->u.mgd;
5195 local = sdata->local;
5196
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005197 if (qos_ok) {
5198 struct sta_info *sta;
5199
5200 rcu_read_lock();
5201 sta = sta_info_get(sdata, ifmgd->bssid);
5202 qos = sta && sta->sta.wme;
5203 rcu_read_unlock();
5204 }
5205
5206 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5207 sizeof(*nullfunc) + 2);
Joe Perchesd15b8452011-08-29 14:17:31 -07005208 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02005209 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07005210
Kalle Valo7044cc52010-01-05 20:16:19 +02005211 skb_reserve(skb, local->hw.extra_tx_headroom);
5212
Johannes Bergb080db52017-06-16 14:29:19 +02005213 nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
Kalle Valo7044cc52010-01-05 20:16:19 +02005214 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
5215 IEEE80211_STYPE_NULLFUNC |
5216 IEEE80211_FCTL_TODS);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005217 if (qos) {
Johannes Berge0ba7092018-11-09 11:16:46 +01005218 __le16 qoshdr = cpu_to_le16(7);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005219
5220 BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
5221 IEEE80211_STYPE_NULLFUNC) !=
5222 IEEE80211_STYPE_QOS_NULLFUNC);
5223 nullfunc->frame_control |=
5224 cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
5225 skb->priority = 7;
5226 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
Johannes Berge0ba7092018-11-09 11:16:46 +01005227 skb_put_data(skb, &qoshdr, sizeof(qoshdr));
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005228 }
5229
Kalle Valo7044cc52010-01-05 20:16:19 +02005230 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
5231 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
5232 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
5233
5234 return skb;
5235}
5236EXPORT_SYMBOL(ieee80211_nullfunc_get);
5237
Kalle Valo05e54ea2010-01-05 20:16:38 +02005238struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
Johannes Berga344d672014-06-12 22:24:31 +02005239 const u8 *src_addr,
Kalle Valo05e54ea2010-01-05 20:16:38 +02005240 const u8 *ssid, size_t ssid_len,
Johannes Bergb9a9ada2012-11-29 13:00:10 +01005241 size_t tailroom)
Kalle Valo05e54ea2010-01-05 20:16:38 +02005242{
Johannes Berga344d672014-06-12 22:24:31 +02005243 struct ieee80211_local *local = hw_to_local(hw);
Kalle Valo05e54ea2010-01-05 20:16:38 +02005244 struct ieee80211_hdr_3addr *hdr;
5245 struct sk_buff *skb;
5246 size_t ie_ssid_len;
5247 u8 *pos;
5248
Kalle Valo05e54ea2010-01-05 20:16:38 +02005249 ie_ssid_len = 2 + ssid_len;
5250
5251 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
Johannes Bergb9a9ada2012-11-29 13:00:10 +01005252 ie_ssid_len + tailroom);
Joe Perchesd15b8452011-08-29 14:17:31 -07005253 if (!skb)
Kalle Valo05e54ea2010-01-05 20:16:38 +02005254 return NULL;
Kalle Valo05e54ea2010-01-05 20:16:38 +02005255
5256 skb_reserve(skb, local->hw.extra_tx_headroom);
5257
Johannes Bergb080db52017-06-16 14:29:19 +02005258 hdr = skb_put_zero(skb, sizeof(*hdr));
Kalle Valo05e54ea2010-01-05 20:16:38 +02005259 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
5260 IEEE80211_STYPE_PROBE_REQ);
Johannes Berge83e6542012-07-13 16:23:07 +02005261 eth_broadcast_addr(hdr->addr1);
Johannes Berga344d672014-06-12 22:24:31 +02005262 memcpy(hdr->addr2, src_addr, ETH_ALEN);
Johannes Berge83e6542012-07-13 16:23:07 +02005263 eth_broadcast_addr(hdr->addr3);
Kalle Valo05e54ea2010-01-05 20:16:38 +02005264
5265 pos = skb_put(skb, ie_ssid_len);
5266 *pos++ = WLAN_EID_SSID;
5267 *pos++ = ssid_len;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02005268 if (ssid_len)
Kalle Valo05e54ea2010-01-05 20:16:38 +02005269 memcpy(pos, ssid, ssid_len);
5270 pos += ssid_len;
5271
Kalle Valo05e54ea2010-01-05 20:16:38 +02005272 return skb;
5273}
5274EXPORT_SYMBOL(ieee80211_probereq_get);
5275
Johannes Berg32bfd352007-12-19 01:31:26 +01005276void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02005277 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02005278 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02005279 struct ieee80211_rts *rts)
5280{
5281 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02005282
Harvey Harrison065e96052008-06-22 16:45:27 -07005283 rts->frame_control =
5284 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01005285 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
5286 frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02005287 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
5288 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
5289}
5290EXPORT_SYMBOL(ieee80211_rts_get);
5291
Johannes Berg32bfd352007-12-19 01:31:26 +01005292void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02005293 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02005294 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02005295 struct ieee80211_cts *cts)
5296{
5297 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02005298
Harvey Harrison065e96052008-06-22 16:45:27 -07005299 cts->frame_control =
5300 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01005301 cts->duration = ieee80211_ctstoself_duration(hw, vif,
5302 frame_len, frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02005303 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
5304}
5305EXPORT_SYMBOL(ieee80211_ctstoself_get);
5306
5307struct sk_buff *
Johannes Berg32bfd352007-12-19 01:31:26 +01005308ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
Johannes Berge039fa42008-05-15 12:55:29 +02005309 struct ieee80211_vif *vif)
Johannes Berge2ebc742007-07-27 15:43:22 +02005310{
5311 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winkler747cf5e2008-05-27 17:50:51 +03005312 struct sk_buff *skb = NULL;
Johannes Berg5cf121c2008-02-25 16:27:43 +01005313 struct ieee80211_tx_data tx;
Johannes Berge2ebc742007-07-27 15:43:22 +02005314 struct ieee80211_sub_if_data *sdata;
Marco Porschd012a602012-10-10 12:39:50 -07005315 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +02005316 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02005317 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berge2ebc742007-07-27 15:43:22 +02005318
Johannes Berg32bfd352007-12-19 01:31:26 +01005319 sdata = vif_to_sdata(vif);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005320
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005321 rcu_read_lock();
Johannes Berg55de9082012-07-26 17:24:39 +02005322 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005323
Marco Porschd012a602012-10-10 12:39:50 -07005324 if (!chanctx_conf)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03005325 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005326
Marco Porschd012a602012-10-10 12:39:50 -07005327 if (sdata->vif.type == NL80211_IFTYPE_AP) {
5328 struct beacon_data *beacon =
5329 rcu_dereference(sdata->u.ap.beacon);
5330
5331 if (!beacon || !beacon->head)
5332 goto out;
5333
5334 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01005335 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
5336 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07005337 } else {
5338 goto out;
5339 }
5340
5341 if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03005342 goto out; /* send buffered bc/mc only after DTIM beacon */
Johannes Berge039fa42008-05-15 12:55:29 +02005343
Johannes Berge2ebc742007-07-27 15:43:22 +02005344 while (1) {
Marco Porschd012a602012-10-10 12:39:50 -07005345 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +02005346 if (!skb)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03005347 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02005348 local->total_ps_buffered--;
5349
Marco Porschd012a602012-10-10 12:39:50 -07005350 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
Johannes Berge2ebc742007-07-27 15:43:22 +02005351 struct ieee80211_hdr *hdr =
5352 (struct ieee80211_hdr *) skb->data;
5353 /* more buffered multicast/broadcast frames ==> set
5354 * MoreData flag in IEEE 802.11 header to inform PS
5355 * STAs */
5356 hdr->frame_control |=
5357 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
5358 }
5359
Michael Braun112c44b2014-03-06 15:08:43 +01005360 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porsch7cbf9d02013-03-01 16:01:18 +01005361 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
Johannes Berg7c107702015-03-20 14:18:27 +01005362 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
Johannes Berge2ebc742007-07-27 15:43:22 +02005363 break;
Felix Fietkau6b07d9c2016-08-02 11:13:41 +02005364 ieee80211_free_txskb(hw, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02005365 }
Johannes Berge039fa42008-05-15 12:55:29 +02005366
5367 info = IEEE80211_SKB_CB(skb);
5368
Johannes Berg5cf121c2008-02-25 16:27:43 +01005369 tx.flags |= IEEE80211_TX_PS_BUFFERED;
Johannes Berg4bf88532012-11-09 11:39:59 +01005370 info->band = chanctx_conf->def.chan->band;
Johannes Berge2ebc742007-07-27 15:43:22 +02005371
Johannes Berg97b045d2008-06-20 01:22:30 +02005372 if (invoke_tx_handlers(&tx))
Johannes Berge2ebc742007-07-27 15:43:22 +02005373 skb = NULL;
Johannes Berg97b045d2008-06-20 01:22:30 +02005374 out:
Johannes Bergd0709a62008-02-25 16:27:46 +01005375 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02005376
5377 return skb;
5378}
5379EXPORT_SYMBOL(ieee80211_get_buffered_bc);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005380
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005381int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5382{
5383 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5384 struct ieee80211_sub_if_data *sdata = sta->sdata;
5385 struct ieee80211_local *local = sdata->local;
5386 int ret;
5387 u32 queues;
5388
5389 lockdep_assert_held(&local->sta_mtx);
5390
5391 /* only some cases are supported right now */
5392 switch (sdata->vif.type) {
5393 case NL80211_IFTYPE_STATION:
5394 case NL80211_IFTYPE_AP:
5395 case NL80211_IFTYPE_AP_VLAN:
5396 break;
5397 default:
5398 WARN_ON(1);
5399 return -EINVAL;
5400 }
5401
5402 if (WARN_ON(tid >= IEEE80211_NUM_UPS))
5403 return -EINVAL;
5404
5405 if (sta->reserved_tid == tid) {
5406 ret = 0;
5407 goto out;
5408 }
5409
5410 if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
5411 sdata_err(sdata, "TID reservation already active\n");
5412 ret = -EALREADY;
5413 goto out;
5414 }
5415
5416 ieee80211_stop_vif_queues(sdata->local, sdata,
5417 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5418
5419 synchronize_net();
5420
5421 /* Tear down BA sessions so we stop aggregating on this TID */
Johannes Berg30686bf2015-06-02 21:39:54 +02005422 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005423 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5424 __ieee80211_stop_tx_ba_session(sta, tid,
5425 AGG_STOP_LOCAL_REQUEST);
5426 }
5427
5428 queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02005429 __ieee80211_flush_queues(local, sdata, queues, false);
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005430
5431 sta->reserved_tid = tid;
5432
5433 ieee80211_wake_vif_queues(local, sdata,
5434 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5435
Johannes Berg30686bf2015-06-02 21:39:54 +02005436 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005437 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5438
5439 ret = 0;
5440 out:
5441 return ret;
5442}
5443EXPORT_SYMBOL(ieee80211_reserve_tid);
5444
5445void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5446{
5447 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5448 struct ieee80211_sub_if_data *sdata = sta->sdata;
5449
5450 lockdep_assert_held(&sdata->local->sta_mtx);
5451
5452 /* only some cases are supported right now */
5453 switch (sdata->vif.type) {
5454 case NL80211_IFTYPE_STATION:
5455 case NL80211_IFTYPE_AP:
5456 case NL80211_IFTYPE_AP_VLAN:
5457 break;
5458 default:
5459 WARN_ON(1);
5460 return;
5461 }
5462
5463 if (tid != sta->reserved_tid) {
5464 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
5465 return;
5466 }
5467
5468 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
5469}
5470EXPORT_SYMBOL(ieee80211_unreserve_tid);
5471
Johannes Berg55de9082012-07-26 17:24:39 +02005472void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
5473 struct sk_buff *skb, int tid,
Mathy Vanhoef08aca292020-07-23 14:01:52 +04005474 enum nl80211_band band)
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005475{
Amadeusz Sławiński731977e2017-01-24 16:42:10 +01005476 int ac = ieee80211_ac_from_tid(tid);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02005477
Zhang Shengjud57a5442016-03-03 01:16:56 +00005478 skb_reset_mac_header(skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02005479 skb_set_queue_mapping(skb, ac);
Helmut Schaacf6bb792011-12-15 10:18:34 +01005480 skb->priority = tid;
Johannes Bergcf0277e2010-01-05 18:00:58 +01005481
Johannes Berg89afe612013-02-13 15:39:57 +01005482 skb->dev = sdata->dev;
5483
Johannes Berg3d34deb2009-06-18 17:25:11 +02005484 /*
5485 * The other path calling ieee80211_xmit is from the tasklet,
5486 * and while we can handle concurrent transmissions locking
5487 * requirements are that we do not come into tx with bhs on.
5488 */
5489 local_bh_disable();
Johannes Berg73c4e192014-11-09 18:50:09 +02005490 IEEE80211_SKB_CB(skb)->band = band;
Mathy Vanhoef08aca292020-07-23 14:01:52 +04005491 ieee80211_xmit(sdata, NULL, skb);
Johannes Berg3d34deb2009-06-18 17:25:11 +02005492 local_bh_enable();
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005493}
Denis Kenzior91180642018-03-26 12:52:50 -05005494
5495int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
5496 const u8 *buf, size_t len,
Markus Theildca9ca22020-05-08 16:42:00 +02005497 const u8 *dest, __be16 proto, bool unencrypted,
5498 u64 *cookie)
Denis Kenzior91180642018-03-26 12:52:50 -05005499{
5500 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5501 struct ieee80211_local *local = sdata->local;
Markus Theil10cb8e62021-02-06 12:51:12 +01005502 struct sta_info *sta;
Denis Kenzior91180642018-03-26 12:52:50 -05005503 struct sk_buff *skb;
5504 struct ethhdr *ehdr;
Johannes Bergb95d2cc2020-03-26 15:53:34 +01005505 u32 ctrl_flags = 0;
Markus Theila7528192020-05-27 18:03:34 +02005506 u32 flags = 0;
Denis Kenzior91180642018-03-26 12:52:50 -05005507
5508 /* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
5509 * or Pre-Authentication
5510 */
5511 if (proto != sdata->control_port_protocol &&
5512 proto != cpu_to_be16(ETH_P_PREAUTH))
5513 return -EINVAL;
5514
Johannes Bergb95d2cc2020-03-26 15:53:34 +01005515 if (proto == sdata->control_port_protocol)
Markus Theil5af7fef2020-06-17 10:26:36 +02005516 ctrl_flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO |
5517 IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
Johannes Bergb95d2cc2020-03-26 15:53:34 +01005518
Denis Kenzior91180642018-03-26 12:52:50 -05005519 if (unencrypted)
Markus Theila7528192020-05-27 18:03:34 +02005520 flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
5521
5522 if (cookie)
5523 ctrl_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
5524
Markus Theil10cb8e62021-02-06 12:51:12 +01005525 flags |= IEEE80211_TX_INTFL_NL80211_FRAME_TX;
Denis Kenzior91180642018-03-26 12:52:50 -05005526
5527 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5528 sizeof(struct ethhdr) + len);
5529 if (!skb)
5530 return -ENOMEM;
5531
5532 skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
5533
5534 skb_put_data(skb, buf, len);
5535
5536 ehdr = skb_push(skb, sizeof(struct ethhdr));
5537 memcpy(ehdr->h_dest, dest, ETH_ALEN);
Johannes Berg8079e4f2020-02-24 10:19:11 +01005538 memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
Denis Kenzior91180642018-03-26 12:52:50 -05005539 ehdr->h_proto = proto;
5540
5541 skb->dev = dev;
Markus Theil10cb8e62021-02-06 12:51:12 +01005542 skb->protocol = proto;
Denis Kenzior91180642018-03-26 12:52:50 -05005543 skb_reset_network_header(skb);
5544 skb_reset_mac_header(skb);
5545
Markus Theil10cb8e62021-02-06 12:51:12 +01005546 /* update QoS header to prioritize control port frames if possible,
5547 * priorization also happens for control port frames send over
5548 * AF_PACKET
5549 */
5550 rcu_read_lock();
5551
5552 if (ieee80211_lookup_ra_sta(sdata, skb, &sta) == 0 && !IS_ERR(sta)) {
5553 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
5554
5555 skb_set_queue_mapping(skb, queue);
5556 skb_get_hash(skb);
5557 }
5558
5559 rcu_read_unlock();
5560
Markus Theila7528192020-05-27 18:03:34 +02005561 /* mutex lock is only needed for incrementing the cookie counter */
5562 mutex_lock(&local->mtx);
5563
Denis Kenziore7441c92018-06-19 10:39:50 -05005564 local_bh_disable();
Markus Theila7528192020-05-27 18:03:34 +02005565 __ieee80211_subif_start_xmit(skb, skb->dev, flags, ctrl_flags, cookie);
Denis Kenziore7441c92018-06-19 10:39:50 -05005566 local_bh_enable();
Denis Kenzior91180642018-03-26 12:52:50 -05005567
Markus Theila7528192020-05-27 18:03:34 +02005568 mutex_unlock(&local->mtx);
5569
Denis Kenzior91180642018-03-26 12:52:50 -05005570 return 0;
5571}
Rajkumar Manoharan8828f812019-04-11 13:47:26 -07005572
5573int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
5574 const u8 *buf, size_t len)
5575{
5576 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5577 struct ieee80211_local *local = sdata->local;
5578 struct sk_buff *skb;
5579
5580 skb = dev_alloc_skb(local->hw.extra_tx_headroom + len +
5581 30 + /* header size */
5582 18); /* 11s header size */
5583 if (!skb)
5584 return -ENOMEM;
5585
5586 skb_reserve(skb, local->hw.extra_tx_headroom);
5587 skb_put_data(skb, buf, len);
5588
5589 skb->dev = dev;
5590 skb->protocol = htons(ETH_P_802_3);
5591 skb_reset_network_header(skb);
5592 skb_reset_mac_header(skb);
5593
5594 local_bh_disable();
5595 __ieee80211_subif_start_xmit(skb, skb->dev, 0,
Markus Theila7528192020-05-27 18:03:34 +02005596 IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP,
5597 NULL);
Johannes Berge2ebc742007-07-27 15:43:22 +02005598 local_bh_enable();
5599
5600 return 0;
5601}