blob: 6d054fed062f0684819b57fbc45ac8a4e948cd4d [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>
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +020021#include <linux/timekeeping.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070022#include <net/net_namespace.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020023#include <net/ieee80211_radiotap.h>
24#include <net/cfg80211.h>
25#include <net/mac80211.h>
Michal Kazior5caa3282016-05-19 10:37:51 +020026#include <net/codel.h>
27#include <net/codel_impl.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020028#include <asm/unaligned.h>
Michal Kaziorfa962b92016-05-19 10:37:49 +020029#include <net/fq_impl.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020030
31#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020032#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040033#include "led.h"
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +010034#include "mesh.h"
Johannes Berge2ebc742007-07-27 15:43:22 +020035#include "wep.h"
36#include "wpa.h"
37#include "wme.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040038#include "rate.h"
Johannes Berge2ebc742007-07-27 15:43:22 +020039
Johannes Berge2ebc742007-07-27 15:43:22 +020040/* misc utils */
41
Johannes Berg252b86c2011-11-16 15:28:55 +010042static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
43 struct sk_buff *skb, int group_addr,
Johannes Berg03f93c32008-06-25 14:36:27 +030044 int next_frag_len)
Johannes Berge2ebc742007-07-27 15:43:22 +020045{
Simon Wunderlich2103dec2013-07-08 16:55:53 +020046 int rate, mrate, erp, dur, i, shift = 0;
Johannes Berg2e92e6f2008-05-15 12:55:27 +020047 struct ieee80211_rate *txrate;
Johannes Berge2ebc742007-07-27 15:43:22 +020048 struct ieee80211_local *local = tx->local;
Johannes Berg8318d782008-01-24 19:38:38 +010049 struct ieee80211_supported_band *sband;
Harvey Harrison358c8d92008-07-15 18:44:13 -070050 struct ieee80211_hdr *hdr;
Johannes Berg252b86c2011-11-16 15:28:55 +010051 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Simon Wunderlich2103dec2013-07-08 16:55:53 +020052 struct ieee80211_chanctx_conf *chanctx_conf;
53 u32 rate_flags = 0;
54
Felix Fietkau95cd4702016-12-14 20:46:57 +010055 /* assume HW handles this */
56 if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))
57 return 0;
58
Simon Wunderlich2103dec2013-07-08 16:55:53 +020059 rcu_read_lock();
60 chanctx_conf = rcu_dereference(tx->sdata->vif.chanctx_conf);
61 if (chanctx_conf) {
62 shift = ieee80211_chandef_get_shift(&chanctx_conf->def);
63 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
64 }
65 rcu_read_unlock();
Johannes Berge6a98542008-10-21 12:40:02 +020066
Johannes Berge6a98542008-10-21 12:40:02 +020067 /* uh huh? */
Felix Fietkau0d528d82013-04-22 16:14:41 +020068 if (WARN_ON_ONCE(tx->rate.idx < 0))
Johannes Berge6a98542008-10-21 12:40:02 +020069 return 0;
Johannes Berge2ebc742007-07-27 15:43:22 +020070
Johannes Berg2d565772012-07-23 15:12:51 +020071 sband = local->hw.wiphy->bands[info->band];
Felix Fietkau0d528d82013-04-22 16:14:41 +020072 txrate = &sband->bitrates[tx->rate.idx];
Johannes Berg8318d782008-01-24 19:38:38 +010073
Johannes Berge6a98542008-10-21 12:40:02 +020074 erp = txrate->flags & IEEE80211_RATE_ERP_G;
Johannes Berge2ebc742007-07-27 15:43:22 +020075
Thomas Pedersen89b8c022020-09-21 19:28:11 -070076 /* device is expected to do this */
77 if (sband->band == NL80211_BAND_S1GHZ)
78 return 0;
79
Johannes Berge2ebc742007-07-27 15:43:22 +020080 /*
81 * data and mgmt (except PS Poll):
82 * - during CFP: 32768
83 * - during contention period:
84 * if addr1 is group address: 0
85 * if more fragments = 0 and addr1 is individual address: time to
86 * transmit one ACK plus SIFS
87 * if more fragments = 1 and addr1 is individual address: time to
88 * transmit next fragment plus 2 x ACK plus 3 x SIFS
89 *
90 * IEEE 802.11, 9.6:
91 * - control response frame (CTS or ACK) shall be transmitted using the
92 * same rate as the immediately previous frame in the frame exchange
93 * sequence, if this rate belongs to the PHY mandatory rates, or else
94 * at the highest possible rate belonging to the PHY rates in the
95 * BSSBasicRateSet
96 */
Johannes Berg252b86c2011-11-16 15:28:55 +010097 hdr = (struct ieee80211_hdr *)skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -070098 if (ieee80211_is_ctl(hdr->frame_control)) {
Johannes Berge2ebc742007-07-27 15:43:22 +020099 /* TODO: These control frames are not currently sent by
Johannes Bergccd7b362008-09-11 00:01:56 +0200100 * mac80211, but should they be implemented, this function
Johannes Berge2ebc742007-07-27 15:43:22 +0200101 * needs to be updated to support duration field calculation.
102 *
103 * RTS: time needed to transmit pending data/mgmt frame plus
104 * one CTS frame plus one ACK frame plus 3 x SIFS
105 * CTS: duration of immediately previous RTS minus time
106 * required to transmit CTS and its SIFS
107 * ACK: 0 if immediately previous directed data/mgmt had
108 * more=0, with more=1 duration in ACK frame is duration
109 * from previous frame minus time needed to transmit ACK
110 * and its SIFS
111 * PS Poll: BIT(15) | BIT(14) | aid
112 */
113 return 0;
114 }
115
116 /* data/mgmt */
117 if (0 /* FIX: data/mgmt during CFP */)
Johannes Berg03f93c32008-06-25 14:36:27 +0300118 return cpu_to_le16(32768);
Johannes Berge2ebc742007-07-27 15:43:22 +0200119
120 if (group_addr) /* Group address as the destination - no ACK */
121 return 0;
122
123 /* Individual destination address:
124 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
125 * CTS and ACK frames shall be transmitted using the highest rate in
126 * basic rate set that is less than or equal to the rate of the
127 * immediately previous frame and that is using the same modulation
128 * (CCK or OFDM). If no basic rate set matches with these requirements,
129 * the highest mandatory rate of the PHY that is less than or equal to
130 * the rate of the previous frame is used.
131 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
132 */
133 rate = -1;
Johannes Berg8318d782008-01-24 19:38:38 +0100134 /* use lowest available if everything fails */
135 mrate = sband->bitrates[0].bitrate;
136 for (i = 0; i < sband->n_bitrates; i++) {
137 struct ieee80211_rate *r = &sband->bitrates[i];
138
139 if (r->bitrate > txrate->bitrate)
Johannes Berge2ebc742007-07-27 15:43:22 +0200140 break;
141
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200142 if ((rate_flags & r->flags) != rate_flags)
143 continue;
144
Johannes Bergbda39332008-10-11 01:51:51 +0200145 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200146 rate = DIV_ROUND_UP(r->bitrate, 1 << shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200147
Johannes Berg8318d782008-01-24 19:38:38 +0100148 switch (sband->band) {
Srinivasan Raju63fa0422021-10-18 11:00:54 +0100149 case NL80211_BAND_2GHZ:
150 case NL80211_BAND_LC: {
Johannes Berg8318d782008-01-24 19:38:38 +0100151 u32 flag;
152 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
153 flag = IEEE80211_RATE_MANDATORY_G;
154 else
155 flag = IEEE80211_RATE_MANDATORY_B;
156 if (r->flags & flag)
157 mrate = r->bitrate;
158 break;
159 }
Johannes Berg57fbcce2016-04-12 15:56:15 +0200160 case NL80211_BAND_5GHZ:
Arend van Sprielc5b9a7f2019-08-02 13:30:58 +0200161 case NL80211_BAND_6GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +0100162 if (r->flags & IEEE80211_RATE_MANDATORY_A)
163 mrate = r->bitrate;
164 break;
Thomas Pedersendf78a0c2020-06-01 23:22:47 -0700165 case NL80211_BAND_S1GHZ:
Johannes Berg57fbcce2016-04-12 15:56:15 +0200166 case NL80211_BAND_60GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300167 /* TODO, for now fall through */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200168 case NUM_NL80211_BANDS:
Johannes Berg8318d782008-01-24 19:38:38 +0100169 WARN_ON(1);
170 break;
171 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200172 }
173 if (rate == -1) {
174 /* No matching basic rate found; use highest suitable mandatory
175 * PHY rate */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200176 rate = DIV_ROUND_UP(mrate, 1 << shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200177 }
178
Simon Wunderlich6674f212011-11-21 21:34:30 +0100179 /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
180 if (ieee80211_is_data_qos(hdr->frame_control) &&
Claudio Pisac26a0e12012-05-28 13:06:25 +0100181 *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
Simon Wunderlich6674f212011-11-21 21:34:30 +0100182 dur = 0;
183 else
184 /* Time needed to transmit ACK
185 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
186 * to closest integer */
Michal Kazior4ee73f32012-04-11 08:47:56 +0200187 dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200188 tx->sdata->vif.bss_conf.use_short_preamble,
189 shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200190
191 if (next_frag_len) {
192 /* Frame is fragmented: duration increases with time needed to
193 * transmit next fragment plus ACK and 2 x SIFS. */
194 dur *= 2; /* ACK + SIFS */
195 /* next fragment */
Michal Kazior4ee73f32012-04-11 08:47:56 +0200196 dur += ieee80211_frame_duration(sband->band, next_frag_len,
Johannes Berg8318d782008-01-24 19:38:38 +0100197 txrate->bitrate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200198 tx->sdata->vif.bss_conf.use_short_preamble,
199 shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200200 }
201
Johannes Berg03f93c32008-06-25 14:36:27 +0300202 return cpu_to_le16(dur);
Johannes Berge2ebc742007-07-27 15:43:22 +0200203}
204
Johannes Berge2ebc742007-07-27 15:43:22 +0200205/* tx handlers */
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200206static ieee80211_tx_result debug_noinline
207ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
208{
209 struct ieee80211_local *local = tx->local;
Kalle Valo0c742112010-01-12 10:42:53 +0200210 struct ieee80211_if_managed *ifmgd;
Andrei Otcheretianski94a5b3a2018-09-05 08:06:14 +0300211 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200212
213 /* driver doesn't support power save */
Johannes Berg30686bf2015-06-02 21:39:54 +0200214 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200215 return TX_CONTINUE;
216
217 /* hardware does dynamic power save */
Johannes Berg30686bf2015-06-02 21:39:54 +0200218 if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200219 return TX_CONTINUE;
220
221 /* dynamic power save disabled */
222 if (local->hw.conf.dynamic_ps_timeout <= 0)
223 return TX_CONTINUE;
224
225 /* we are scanning, don't enable power save */
226 if (local->scanning)
227 return TX_CONTINUE;
228
229 if (!local->ps_sdata)
230 return TX_CONTINUE;
231
232 /* No point if we're going to suspend */
233 if (local->quiescing)
234 return TX_CONTINUE;
235
Kalle Valo0c742112010-01-12 10:42:53 +0200236 /* dynamic ps is supported only in managed mode */
237 if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
238 return TX_CONTINUE;
239
Andrei Otcheretianski94a5b3a2018-09-05 08:06:14 +0300240 if (unlikely(info->flags & IEEE80211_TX_INTFL_OFFCHAN_TX_OK))
241 return TX_CONTINUE;
242
Kalle Valo0c742112010-01-12 10:42:53 +0200243 ifmgd = &tx->sdata->u.mgd;
244
245 /*
246 * Don't wakeup from power save if u-apsd is enabled, voip ac has
247 * u-apsd enabled and the frame is in voip class. This effectively
248 * means that even if all access categories have u-apsd enabled, in
249 * practise u-apsd is only used with the voip ac. This is a
250 * workaround for the case when received voip class packets do not
251 * have correct qos tag for some reason, due the network or the
252 * peer application.
253 *
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200254 * Note: ifmgd->uapsd_queues access is racy here. If the value is
Kalle Valo0c742112010-01-12 10:42:53 +0200255 * changed via debugfs, user needs to reassociate manually to have
256 * everything in sync.
257 */
Johannes Berg4875d30d2012-03-27 14:18:37 +0200258 if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
259 (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
260 skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
Kalle Valo0c742112010-01-12 10:42:53 +0200261 return TX_CONTINUE;
262
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200263 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
264 ieee80211_stop_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +0100265 IEEE80211_MAX_QUEUE_MAP,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300266 IEEE80211_QUEUE_STOP_REASON_PS,
267 false);
Vivek Natarajandb285692011-02-18 17:18:03 +0530268 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200269 ieee80211_queue_work(&local->hw,
270 &local->dynamic_ps_disable_work);
271 }
272
Luciano Coelho5db1c072011-05-03 21:40:08 +0300273 /* Don't restart the timer if we're not disassociated */
274 if (!ifmgd->associated)
275 return TX_CONTINUE;
276
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200277 mod_timer(&local->dynamic_ps_timer, jiffies +
278 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
279
280 return TX_CONTINUE;
281}
Johannes Berge2ebc742007-07-27 15:43:22 +0200282
Johannes Bergd9e8a702008-06-30 15:10:44 +0200283static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100284ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200285{
Harvey Harrison358c8d92008-07-15 18:44:13 -0700286
Johannes Berge039fa42008-05-15 12:55:29 +0200287 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200288 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200289 bool assoc = false;
Johannes Berge2ebc742007-07-27 15:43:22 +0200290
Johannes Berge039fa42008-05-15 12:55:29 +0200291 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100292 return TX_CONTINUE;
Johannes Berg58d41852007-09-26 17:53:18 +0200293
Ben Greearb23b0252011-02-04 11:54:17 -0800294 if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
295 test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
Kalle Valoa9a6fff2009-03-18 14:06:44 +0200296 !ieee80211_is_probe_req(hdr->frame_control) &&
Thomas Pedersen30b2f0b2020-01-13 21:59:40 -0800297 !ieee80211_is_any_nullfunc(hdr->frame_control))
Kalle Valoa9a6fff2009-03-18 14:06:44 +0200298 /*
299 * When software scanning only nullfunc frames (to notify
300 * the sleep state to the AP) and probe requests (for the
301 * active scan) are allowed, all other frames should not be
302 * sent and we should not get here, but if we do
303 * nonetheless, drop them to avoid sending them
304 * off-channel. See the link below and
305 * ieee80211_start_scan() for more.
306 *
307 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
308 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100309 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200310
Rostislav Lisovy239281f2014-11-03 10:33:19 +0100311 if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
312 return TX_CONTINUE;
313
Johannes Berg5cf121c2008-02-25 16:27:43 +0100314 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100315 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200316
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200317 if (tx->sta)
318 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge2ebc742007-07-27 15:43:22 +0200319
Johannes Berg5cf121c2008-02-25 16:27:43 +0100320 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200321 if (unlikely(!assoc &&
Harvey Harrison358c8d92008-07-15 18:44:13 -0700322 ieee80211_is_data(hdr->frame_control))) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200323#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200324 sdata_info(tx->sdata,
325 "dropped data frame to not associated station %pM\n",
326 hdr->addr1);
327#endif
Johannes Berge2ebc742007-07-27 15:43:22 +0200328 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100329 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200330 }
Michael Braun72f15d52016-10-10 19:12:21 +0200331 } else if (unlikely(ieee80211_is_data(hdr->frame_control) &&
332 ieee80211_vif_get_num_mcast_if(tx->sdata) == 0)) {
Johannes Berg29623892011-12-14 12:20:31 +0100333 /*
334 * No associated STAs - no need to send multicast
335 * frames.
336 */
337 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200338 }
339
Johannes Berg9ae54c82008-01-31 19:48:20 +0100340 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200341}
342
Johannes Berge2ebc742007-07-27 15:43:22 +0200343/* This function is called whenever the AP is about to exceed the maximum limit
344 * of buffered frames for power saving STAs. This situation should not really
345 * happen often during normal operation, so dropping the oldest buffered packet
346 * from each queue should be OK to make some room for new frames. */
347static void purge_old_ps_buffers(struct ieee80211_local *local)
348{
349 int total = 0, purged = 0;
350 struct sk_buff *skb;
351 struct ieee80211_sub_if_data *sdata;
352 struct sta_info *sta;
353
Johannes Berg79010422007-09-18 17:29:21 -0400354 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Marco Porschd012a602012-10-10 12:39:50 -0700355 struct ps_data *ps;
356
357 if (sdata->vif.type == NL80211_IFTYPE_AP)
358 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100359 else if (ieee80211_vif_is_mesh(&sdata->vif))
360 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700361 else
Johannes Berge2ebc742007-07-27 15:43:22 +0200362 continue;
Marco Porschd012a602012-10-10 12:39:50 -0700363
364 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +0200365 if (skb) {
366 purged++;
Felix Fietkau6b07d9c2016-08-02 11:13:41 +0200367 ieee80211_free_txskb(&local->hw, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +0200368 }
Marco Porschd012a602012-10-10 12:39:50 -0700369 total += skb_queue_len(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +0200370 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200371
Johannes Berg948d8872011-09-29 16:04:29 +0200372 /*
373 * Drop one frame from each station from the lowest-priority
374 * AC that has frames at all.
375 */
Johannes Bergd0709a62008-02-25 16:27:46 +0100376 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg948d8872011-09-29 16:04:29 +0200377 int ac;
378
379 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
380 skb = skb_dequeue(&sta->ps_tx_buf[ac]);
381 total += skb_queue_len(&sta->ps_tx_buf[ac]);
382 if (skb) {
383 purged++;
Felix Fietkauc3e77242012-10-08 14:39:33 +0200384 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg948d8872011-09-29 16:04:29 +0200385 break;
386 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200387 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200388 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100389
Johannes Berge2ebc742007-07-27 15:43:22 +0200390 local->total_ps_buffered = total;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200391 ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
Johannes Berge2ebc742007-07-27 15:43:22 +0200392}
393
Johannes Berg9ae54c82008-01-31 19:48:20 +0100394static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100395ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200396{
Johannes Berge039fa42008-05-15 12:55:29 +0200397 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700398 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Marco Porschd012a602012-10-10 12:39:50 -0700399 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +0200400
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100401 /*
402 * broadcast/multicast frame
403 *
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100404 * If any of the associated/peer stations is in power save mode,
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100405 * the frame is buffered to be sent after DTIM beacon frame.
406 * This is done either by the hardware or us.
407 */
408
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100409 /* powersaving STAs currently only in AP/VLAN/mesh mode */
Marco Porschd012a602012-10-10 12:39:50 -0700410 if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
411 tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
412 if (!tx->sdata->bss)
413 return TX_CONTINUE;
414
415 ps = &tx->sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100416 } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
417 ps = &tx->sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700418 } else {
Johannes Berg3e122be2008-07-09 14:40:34 +0200419 return TX_CONTINUE;
Marco Porschd012a602012-10-10 12:39:50 -0700420 }
421
Johannes Berg3e122be2008-07-09 14:40:34 +0200422
423 /* no buffering for ordered frames */
Harvey Harrison358c8d92008-07-15 18:44:13 -0700424 if (ieee80211_has_order(hdr->frame_control))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100425 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100426
Johannes Berg08b99392014-07-07 12:01:11 +0200427 if (ieee80211_is_probe_req(hdr->frame_control))
428 return TX_CONTINUE;
429
Johannes Berg30686bf2015-06-02 21:39:54 +0200430 if (ieee80211_hw_check(&tx->local->hw, QUEUE_CONTROL))
Johannes Bergf4d57942013-05-28 17:24:15 +0200431 info->hw_queue = tx->sdata->vif.cab_queue;
432
Felix Fietkau9ec11902018-11-28 22:39:16 +0100433 /* no stations in PS mode and no buffered packets */
434 if (!atomic_read(&ps->num_sta_ps) && skb_queue_empty(&ps->bc_buf))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100435 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100436
Johannes Berg62b517c2009-10-29 12:19:21 +0100437 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
Johannes Berg62b12082009-08-10 16:04:15 +0200438
Johannes Berg62b517c2009-10-29 12:19:21 +0100439 /* device releases frame after DTIM beacon */
Johannes Berg30686bf2015-06-02 21:39:54 +0200440 if (!ieee80211_hw_check(&tx->local->hw, HOST_BROADCAST_PS_BUFFERING))
Johannes Berg62b12082009-08-10 16:04:15 +0200441 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200442
Johannes Berg62b12082009-08-10 16:04:15 +0200443 /* buffered in mac80211 */
444 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
445 purge_old_ps_buffers(tx->local);
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100446
Marco Porschd012a602012-10-10 12:39:50 -0700447 if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200448 ps_dbg(tx->sdata,
449 "BC TX buffer full - dropping the oldest frame\n");
Felix Fietkau6b07d9c2016-08-02 11:13:41 +0200450 ieee80211_free_txskb(&tx->local->hw, skb_dequeue(&ps->bc_buf));
Johannes Berg62b12082009-08-10 16:04:15 +0200451 } else
452 tx->local->total_ps_buffered++;
453
Marco Porschd012a602012-10-10 12:39:50 -0700454 skb_queue_tail(&ps->bc_buf, tx->skb);
Johannes Berg62b12082009-08-10 16:04:15 +0200455
456 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200457}
458
Jouni Malinenfb733332009-01-08 13:32:00 +0200459static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
460 struct sk_buff *skb)
461{
462 if (!ieee80211_is_mgmt(fc))
463 return 0;
464
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200465 if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
Jouni Malinenfb733332009-01-08 13:32:00 +0200466 return 0;
467
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100468 if (!ieee80211_is_robust_mgmt_frame(skb))
Jouni Malinenfb733332009-01-08 13:32:00 +0200469 return 0;
470
471 return 1;
472}
473
Johannes Berg9ae54c82008-01-31 19:48:20 +0100474static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100475ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200476{
477 struct sta_info *sta = tx->sta;
Johannes Berge039fa42008-05-15 12:55:29 +0200478 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berg08b99392014-07-07 12:01:11 +0200479 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300480 struct ieee80211_local *local = tx->local;
Johannes Berge2ebc742007-07-27 15:43:22 +0200481
Johannes Berg02f2f1a2012-02-27 12:18:30 +0100482 if (unlikely(!sta))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100483 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200484
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200485 if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
Johannes Berg5ac2e352014-05-27 16:32:27 +0200486 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
487 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
Johannes Berg02f2f1a2012-02-27 12:18:30 +0100488 !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
Johannes Berg948d8872011-09-29 16:04:29 +0200489 int ac = skb_get_queue_mapping(tx->skb);
490
Johannes Berg08b99392014-07-07 12:01:11 +0200491 if (ieee80211_is_mgmt(hdr->frame_control) &&
492 !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
493 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
494 return TX_CONTINUE;
495 }
496
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200497 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
498 sta->sta.addr, sta->sta.aid, ac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200499 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
500 purge_old_ps_buffers(tx->local);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200501
502 /* sync with ieee80211_sta_ps_deliver_wakeup */
503 spin_lock(&sta->ps_lock);
504 /*
505 * STA woke up the meantime and all the frames on ps_tx_buf have
506 * been queued to pending queue. No reordering can happen, go
507 * ahead and Tx the packet.
508 */
509 if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
Johannes Berg5ac2e352014-05-27 16:32:27 +0200510 !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
511 !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200512 spin_unlock(&sta->ps_lock);
513 return TX_CONTINUE;
514 }
515
Johannes Berg948d8872011-09-29 16:04:29 +0200516 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
517 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200518 ps_dbg(tx->sdata,
519 "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
520 sta->sta.addr, ac);
Felix Fietkauc3e77242012-10-08 14:39:33 +0200521 ieee80211_free_txskb(&local->hw, old);
Johannes Berge2ebc742007-07-27 15:43:22 +0200522 } else
523 tx->local->total_ps_buffered++;
Johannes Berg004c8722008-02-20 11:21:35 +0100524
Johannes Berge039fa42008-05-15 12:55:29 +0200525 info->control.jiffies = jiffies;
Johannes Berg5061b0c2009-07-14 00:33:34 +0200526 info->control.vif = &tx->sdata->vif;
Felix Fietkaucc20ff22020-09-08 14:36:57 +0200527 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
Johannes Berg03c8c062014-01-09 01:45:28 +0100528 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berg948d8872011-09-29 16:04:29 +0200529 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200530 spin_unlock(&sta->ps_lock);
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300531
532 if (!timer_pending(&local->sta_cleanup))
533 mod_timer(&local->sta_cleanup,
534 round_jiffies(jiffies +
535 STA_INFO_CLEANUP_INTERVAL));
536
Johannes Bergc868cb352011-09-29 16:04:27 +0200537 /*
538 * We queued up some frames, so the TIM bit might
539 * need to be set, recalculate it.
540 */
541 sta_info_recalc_tim(sta);
542
Johannes Berg9ae54c82008-01-31 19:48:20 +0100543 return TX_QUEUED;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200544 } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
545 ps_dbg(tx->sdata,
546 "STA %pM in PS mode, but polling/in SP -> send frame\n",
547 sta->sta.addr);
Johannes Berge2ebc742007-07-27 15:43:22 +0200548 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200549
Johannes Berg9ae54c82008-01-31 19:48:20 +0100550 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200551}
552
Johannes Bergd9e8a702008-06-30 15:10:44 +0200553static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100554ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200555{
Johannes Berg5cf121c2008-02-25 16:27:43 +0100556 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100557 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200558
Johannes Berg5cf121c2008-02-25 16:27:43 +0100559 if (tx->flags & IEEE80211_TX_UNICAST)
Johannes Berge2ebc742007-07-27 15:43:22 +0200560 return ieee80211_tx_h_unicast_ps_buf(tx);
561 else
562 return ieee80211_tx_h_multicast_ps_buf(tx);
563}
564
Johannes Bergd9e8a702008-06-30 15:10:44 +0200565static ieee80211_tx_result debug_noinline
Johannes Berga621fa42010-08-27 14:26:54 +0300566ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
567{
568 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
569
Johannes Bergaf61a162013-07-02 18:09:12 +0200570 if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
571 if (tx->sdata->control_port_no_encrypt)
572 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
573 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
Jouni Malinen9c1c98a2015-02-26 15:50:50 +0200574 info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
Johannes Bergaf61a162013-07-02 18:09:12 +0200575 }
Johannes Berga621fa42010-08-27 14:26:54 +0300576
577 return TX_CONTINUE;
578}
579
580static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100581ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200582{
Johannes Berg46e6de12012-07-04 18:10:07 +0200583 struct ieee80211_key *key;
Johannes Berge039fa42008-05-15 12:55:29 +0200584 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700585 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400586
Johannes Berga0761a32020-03-26 15:09:42 +0200587 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200588 tx->key = NULL;
Johannes Berga0761a32020-03-26 15:09:42 +0200589 return TX_CONTINUE;
590 }
591
592 if (tx->sta &&
593 (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
Johannes Bergd4e46a32007-09-14 11:10:24 -0400594 tx->key = key;
Masashi Honma46f6b062016-06-22 19:55:20 +0900595 else if (ieee80211_is_group_privacy_action(tx->skb) &&
596 (key = rcu_dereference(tx->sdata->default_multicast_key)))
597 tx->key = key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200598 else if (ieee80211_is_mgmt(hdr->frame_control) &&
Jouni Malinenecbcd322010-03-29 23:35:23 -0700599 is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100600 ieee80211_is_robust_mgmt_frame(tx->skb) &&
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200601 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
602 tx->key = key;
Johannes Bergf7e01042010-12-09 19:49:02 +0100603 else if (is_multicast_ether_addr(hdr->addr1) &&
604 (key = rcu_dereference(tx->sdata->default_multicast_key)))
605 tx->key = key;
606 else if (!is_multicast_ether_addr(hdr->addr1) &&
607 (key = rcu_dereference(tx->sdata->default_unicast_key)))
Johannes Bergd4e46a32007-09-14 11:10:24 -0400608 tx->key = key;
Johannes Berge8f4fb72015-03-20 11:37:36 +0100609 else
Johannes Berg46e6de12012-07-04 18:10:07 +0200610 tx->key = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +0200611
612 if (tx->key) {
Johannes Berg813d7662010-01-17 01:47:58 +0100613 bool skip_hw = false;
614
Johannes Berg011bfcc2007-09-17 01:29:25 -0400615 /* TODO: add threshold stuff again */
Johannes Berg176e4f82007-12-18 15:27:47 +0100616
Johannes Berg97359d12010-08-10 09:46:38 +0200617 switch (tx->key->conf.cipher) {
618 case WLAN_CIPHER_SUITE_WEP40:
619 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg97359d12010-08-10 09:46:38 +0200620 case WLAN_CIPHER_SUITE_TKIP:
Harvey Harrison358c8d92008-07-15 18:44:13 -0700621 if (!ieee80211_is_data_present(hdr->frame_control))
Johannes Berg176e4f82007-12-18 15:27:47 +0100622 tx->key = NULL;
623 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200624 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200625 case WLAN_CIPHER_SUITE_CCMP_256:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200626 case WLAN_CIPHER_SUITE_GCMP:
627 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinenfb733332009-01-08 13:32:00 +0200628 if (!ieee80211_is_data_present(hdr->frame_control) &&
629 !ieee80211_use_mfp(hdr->frame_control, tx->sta,
Masashi Honma46f6b062016-06-22 19:55:20 +0900630 tx->skb) &&
631 !ieee80211_is_group_privacy_action(tx->skb))
Jouni Malinenfb733332009-01-08 13:32:00 +0200632 tx->key = NULL;
Kalle Valo3b43a182010-01-23 20:27:14 +0200633 else
634 skip_hw = (tx->key->conf.flags &
Johannes Berge548c492012-09-04 17:08:23 +0200635 IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
Kalle Valo3b43a182010-01-23 20:27:14 +0200636 ieee80211_is_mgmt(hdr->frame_control);
Jouni Malinenfb733332009-01-08 13:32:00 +0200637 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200638 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200639 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Jouni Malinen8ade5382015-01-24 19:52:09 +0200640 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
641 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200642 if (!ieee80211_is_mgmt(hdr->frame_control))
643 tx->key = NULL;
644 break;
Johannes Berg176e4f82007-12-18 15:27:47 +0100645 }
Johannes Berg813d7662010-01-17 01:47:58 +0100646
Johannes Berge54faf22013-01-29 11:41:38 +0100647 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
648 !ieee80211_is_deauth(hdr->frame_control)))
Johannes Berg95acac62011-07-12 12:30:59 +0200649 return TX_DROP;
650
Johannes Bergf12553e2010-01-22 22:07:59 +0100651 if (!skip_hw && tx->key &&
Johannes Berg382b1652010-01-25 11:36:16 +0100652 tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
Johannes Berg813d7662010-01-17 01:47:58 +0100653 info->control.hw_key = &tx->key->conf;
Felix Fietkau2463ec82020-12-18 20:15:25 +0100654 } else if (ieee80211_is_data_present(hdr->frame_control) && tx->sta &&
Johannes Berga0761a32020-03-26 15:09:42 +0200655 test_sta_flag(tx->sta, WLAN_STA_USES_ENCRYPTION)) {
656 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200657 }
658
Johannes Berg9ae54c82008-01-31 19:48:20 +0100659 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200660}
661
Johannes Bergd9e8a702008-06-30 15:10:44 +0200662static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100663ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200664{
Johannes Berge039fa42008-05-15 12:55:29 +0200665 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200666 struct ieee80211_hdr *hdr = (void *)tx->skb->data;
667 struct ieee80211_supported_band *sband;
Shanyu Zhaoa2c40242010-04-27 11:15:12 -0700668 u32 len;
Johannes Berge6a98542008-10-21 12:40:02 +0200669 struct ieee80211_tx_rate_control txrc;
Felix Fietkau0d528d82013-04-22 16:14:41 +0200670 struct ieee80211_sta_rates *ratetbl = NULL;
Ryder Lee3187ba02021-06-17 18:31:13 +0200671 bool encap = info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200672 bool assoc = false;
Johannes Berge6a98542008-10-21 12:40:02 +0200673
674 memset(&txrc, 0, sizeof(txrc));
Johannes Berg8318d782008-01-24 19:38:38 +0100675
Johannes Berg2d565772012-07-23 15:12:51 +0200676 sband = tx->local->hw.wiphy->bands[info->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200677
Shanyu Zhaoa2c40242010-04-27 11:15:12 -0700678 len = min_t(u32, tx->skb->len + FCS_LEN,
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200679 tx->local->hw.wiphy->frag_threshold);
Johannes Berg58d41852007-09-26 17:53:18 +0200680
Johannes Berge6a98542008-10-21 12:40:02 +0200681 /* set up the tx rate control struct we give the RC algo */
Johannes Berg005e4722012-02-26 11:24:35 +0100682 txrc.hw = &tx->local->hw;
Johannes Berge6a98542008-10-21 12:40:02 +0200683 txrc.sband = sband;
684 txrc.bss_conf = &tx->sdata->vif.bss_conf;
685 txrc.skb = tx->skb;
686 txrc.reported_rate.idx = -1;
Johannes Berg2d565772012-07-23 15:12:51 +0200687 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
Felix Fietkau2ffbe6d2013-04-16 13:38:42 +0200688
689 if (tx->sdata->rc_has_mcs_mask[info->band])
690 txrc.rate_idx_mcs_mask =
691 tx->sdata->rc_rateidx_mcs_mask[info->band];
692
Felix Fietkau8f0729b2010-11-11 15:07:23 +0100693 txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -0800694 tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
Bertold Van den Bergh5765f9f2015-08-05 16:02:28 +0200695 tx->sdata->vif.type == NL80211_IFTYPE_ADHOC ||
696 tx->sdata->vif.type == NL80211_IFTYPE_OCB);
Johannes Berg58d41852007-09-26 17:53:18 +0200697
Johannes Berge6a98542008-10-21 12:40:02 +0200698 /* set up RTS protection if desired */
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200699 if (len > tx->local->hw.wiphy->rts_threshold) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200700 txrc.rts = true;
Johannes Berge2ebc742007-07-27 15:43:22 +0200701 }
Johannes Berge6a98542008-10-21 12:40:02 +0200702
Felix Fietkau0d528d82013-04-22 16:14:41 +0200703 info->control.use_rts = txrc.rts;
Felix Fietkau991fec02013-04-16 13:38:43 +0200704 info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
705
Johannes Berge6a98542008-10-21 12:40:02 +0200706 /*
707 * Use short preamble if the BSS can handle it, but not for
708 * management frames unless we know the receiver can handle
709 * that -- the management frame might be to a station that
710 * just wants a probe response.
711 */
712 if (tx->sdata->vif.bss_conf.use_short_preamble &&
Ryder Lee3187ba02021-06-17 18:31:13 +0200713 (ieee80211_is_tx_data(tx->skb) ||
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200714 (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
Felix Fietkau0d528d82013-04-22 16:14:41 +0200715 txrc.short_preamble = true;
716
717 info->control.short_preamble = txrc.short_preamble;
Johannes Berge6a98542008-10-21 12:40:02 +0200718
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +0100719 /* don't ask rate control when rate already injected via radiotap */
720 if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)
721 return TX_CONTINUE;
722
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200723 if (tx->sta)
724 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge6a98542008-10-21 12:40:02 +0200725
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700726 /*
727 * Lets not bother rate control if we're associated and cannot
728 * talk to the sta. This should not happen.
729 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200730 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700731 !rate_usable_index_exists(sband, &tx->sta->sta),
732 "%s: Dropped data frame as no usable bitrate found while "
733 "scanning and associated. Target station: "
734 "%pM on %d GHz band\n",
Ryder Lee3187ba02021-06-17 18:31:13 +0200735 tx->sdata->name,
736 encap ? ((struct ethhdr *)hdr)->h_dest : hdr->addr1,
Johannes Berg2d565772012-07-23 15:12:51 +0200737 info->band ? 5 : 2))
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700738 return TX_DROP;
739
740 /*
741 * If we're associated with the sta at this point we know we can at
742 * least send the frame at the lowest bit rate.
743 */
Johannes Berge6a98542008-10-21 12:40:02 +0200744 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
745
Felix Fietkau0d528d82013-04-22 16:14:41 +0200746 if (tx->sta && !info->control.skip_table)
747 ratetbl = rcu_dereference(tx->sta->sta.rates);
748
749 if (unlikely(info->control.rates[0].idx < 0)) {
750 if (ratetbl) {
751 struct ieee80211_tx_rate rate = {
752 .idx = ratetbl->rate[0].idx,
753 .flags = ratetbl->rate[0].flags,
754 .count = ratetbl->rate[0].count
755 };
756
757 if (ratetbl->rate[0].idx < 0)
758 return TX_DROP;
759
760 tx->rate = rate;
761 } else {
762 return TX_DROP;
763 }
764 } else {
765 tx->rate = info->control.rates[0];
766 }
Johannes Berge6a98542008-10-21 12:40:02 +0200767
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100768 if (txrc.reported_rate.idx < 0) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200769 txrc.reported_rate = tx->rate;
Ryder Lee3187ba02021-06-17 18:31:13 +0200770 if (tx->sta && ieee80211_is_tx_data(tx->skb))
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200771 tx->sta->tx_stats.last_rate = txrc.reported_rate;
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100772 } else if (tx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200773 tx->sta->tx_stats.last_rate = txrc.reported_rate;
Johannes Berge6a98542008-10-21 12:40:02 +0200774
Felix Fietkau0d528d82013-04-22 16:14:41 +0200775 if (ratetbl)
776 return TX_CONTINUE;
777
Johannes Berge6a98542008-10-21 12:40:02 +0200778 if (unlikely(!info->control.rates[0].count))
779 info->control.rates[0].count = 1;
780
Gábor Stefanik99551512009-04-23 19:36:14 +0200781 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
782 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
783 info->control.rates[0].count = 1;
784
Johannes Berg9ae54c82008-01-31 19:48:20 +0100785 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200786}
787
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100788static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
789{
790 u16 *seq = &sta->tid_seq[tid];
791 __le16 ret = cpu_to_le16(*seq);
792
793 /* Increase the sequence number. */
794 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
795
796 return ret;
797}
798
Johannes Bergd9e8a702008-06-30 15:10:44 +0200799static ieee80211_tx_result debug_noinline
Johannes Bergf591fa52008-07-10 11:21:26 +0200800ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
801{
802 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
803 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergf591fa52008-07-10 11:21:26 +0200804 int tid;
805
Johannes Berg25d834e2008-09-12 22:52:47 +0200806 /*
807 * Packet injection may want to control the sequence
808 * number, if we have no matching interface then we
809 * neither assign one ourselves nor ask the driver to.
810 */
Johannes Berg5061b0c2009-07-14 00:33:34 +0200811 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
Johannes Berg25d834e2008-09-12 22:52:47 +0200812 return TX_CONTINUE;
813
Johannes Bergf591fa52008-07-10 11:21:26 +0200814 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
815 return TX_CONTINUE;
816
817 if (ieee80211_hdrlen(hdr->frame_control) < 24)
818 return TX_CONTINUE;
819
Johannes Berg49a59542011-09-29 16:04:41 +0200820 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
821 return TX_CONTINUE;
822
Mathy Vanhoef29c3e952020-07-23 14:01:50 +0400823 if (info->control.flags & IEEE80211_TX_CTRL_NO_SEQNO)
824 return TX_CONTINUE;
825
Johannes Berg94778282008-10-10 13:21:59 +0200826 /*
827 * Anything but QoS data that has a sequence number field
828 * (is long enough) gets a sequence number from the global
Bob Copelandc4c205f2013-08-23 09:35:38 -0400829 * counter. QoS data frames with a multicast destination
830 * also use the global counter (802.11-2012 9.3.2.10).
Johannes Berg94778282008-10-10 13:21:59 +0200831 */
Bob Copelandc4c205f2013-08-23 09:35:38 -0400832 if (!ieee80211_is_data_qos(hdr->frame_control) ||
833 is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg94778282008-10-10 13:21:59 +0200834 /* driver should assign sequence number */
Johannes Bergf591fa52008-07-10 11:21:26 +0200835 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
Johannes Berg94778282008-10-10 13:21:59 +0200836 /* for pure STA mode without beacons, we can do it */
837 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
838 tx->sdata->sequence_number += 0x10;
Johannes Berg79c892b2014-11-21 14:26:31 +0100839 if (tx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200840 tx->sta->tx_stats.msdu[IEEE80211_NUM_TIDS]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200841 return TX_CONTINUE;
842 }
843
844 /*
845 * This should be true for injected/management frames only, for
846 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
847 * above since they are not QoS-data frames.
848 */
849 if (!tx->sta)
850 return TX_CONTINUE;
851
852 /* include per-STA, per-TID sequence counter */
Sara Sharona1f2ba04c2018-02-19 14:48:40 +0200853 tid = ieee80211_get_tid(hdr);
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200854 tx->sta->tx_stats.msdu[tid]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200855
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +0200856 hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
Johannes Bergf591fa52008-07-10 11:21:26 +0200857
858 return TX_CONTINUE;
859}
860
Johannes Berg252b86c2011-11-16 15:28:55 +0100861static int ieee80211_fragment(struct ieee80211_tx_data *tx,
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100862 struct sk_buff *skb, int hdrlen,
863 int frag_threshold)
864{
Johannes Berg252b86c2011-11-16 15:28:55 +0100865 struct ieee80211_local *local = tx->local;
Johannes Berga1a3fce2011-11-16 15:28:56 +0100866 struct ieee80211_tx_info *info;
Johannes Berg252b86c2011-11-16 15:28:55 +0100867 struct sk_buff *tmp;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100868 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
869 int pos = hdrlen + per_fragm;
870 int rem = skb->len - hdrlen - per_fragm;
871
872 if (WARN_ON(rem < 0))
873 return -EINVAL;
874
Johannes Berg252b86c2011-11-16 15:28:55 +0100875 /* first fragment was already added to queue by caller */
876
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100877 while (rem) {
878 int fraglen = per_fragm;
879
880 if (fraglen > rem)
881 fraglen = rem;
882 rem -= fraglen;
883 tmp = dev_alloc_skb(local->tx_headroom +
884 frag_threshold +
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200885 tx->sdata->encrypt_headroom +
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100886 IEEE80211_ENCRYPT_TAILROOM);
887 if (!tmp)
888 return -ENOMEM;
Johannes Berg252b86c2011-11-16 15:28:55 +0100889
890 __skb_queue_tail(&tx->skbs, tmp);
891
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200892 skb_reserve(tmp,
893 local->tx_headroom + tx->sdata->encrypt_headroom);
894
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100895 /* copy control information */
896 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
Johannes Berga1a3fce2011-11-16 15:28:56 +0100897
898 info = IEEE80211_SKB_CB(tmp);
899 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
900 IEEE80211_TX_CTL_FIRST_FRAGMENT);
901
902 if (rem)
903 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
904
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100905 skb_copy_queue_mapping(tmp, skb);
906 tmp->priority = skb->priority;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100907 tmp->dev = skb->dev;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100908
909 /* copy header and data */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200910 skb_put_data(tmp, skb->data, hdrlen);
911 skb_put_data(tmp, skb->data + pos, fraglen);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100912
913 pos += fraglen;
914 }
915
Johannes Berg252b86c2011-11-16 15:28:55 +0100916 /* adjust first fragment's length */
Johannes Berg338f9772014-02-01 00:16:23 +0100917 skb_trim(skb, hdrlen + per_fragm);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100918 return 0;
919}
920
Johannes Bergf591fa52008-07-10 11:21:26 +0200921static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +0200922ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
923{
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100924 struct sk_buff *skb = tx->skb;
925 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
926 struct ieee80211_hdr *hdr = (void *)skb->data;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200927 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100928 int hdrlen;
929 int fragnum;
Johannes Berge2454942008-05-15 12:55:28 +0200930
Johannes Berg252b86c2011-11-16 15:28:55 +0100931 /* no matter what happens, tx->skb moves to tx->skbs */
932 __skb_queue_tail(&tx->skbs, skb);
933 tx->skb = NULL;
934
Johannes Berga26eb272011-10-07 14:01:25 +0200935 if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
936 return TX_CONTINUE;
937
Sara Sharonf3fe4e92016-10-18 23:12:11 +0300938 if (ieee80211_hw_check(&tx->local->hw, SUPPORTS_TX_FRAG))
Johannes Berge2454942008-05-15 12:55:28 +0200939 return TX_CONTINUE;
940
Johannes Bergeefce912008-05-17 00:57:13 +0200941 /*
942 * Warn when submitting a fragmented A-MPDU frame and drop it.
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200943 * This scenario is handled in ieee80211_tx_prepare but extra
Ron Rindjunsky8d5e0d52008-06-12 15:42:29 +0300944 * caution taken here as fragmented ampdu may cause Tx stop.
Johannes Bergeefce912008-05-17 00:57:13 +0200945 */
Sujith8b30b1f2008-10-24 09:55:27 +0530946 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
Johannes Bergeefce912008-05-17 00:57:13 +0200947 return TX_DROP;
948
Harvey Harrison065e96052008-06-22 16:45:27 -0700949 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berge2454942008-05-15 12:55:28 +0200950
Johannes Berga26eb272011-10-07 14:01:25 +0200951 /* internal error, why isn't DONTFRAG set? */
Johannes Berg8ccd8f22009-04-29 23:35:56 +0200952 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100953 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200954
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100955 /*
956 * Now fragment the frame. This will allocate all the fragments and
957 * chain them (using skb as the first fragment) to skb->next.
958 * During transmission, we will remove the successfully transmitted
959 * fragments from this list. When the low-level driver rejects one
960 * of the fragments then we will simply pretend to accept the skb
961 * but store it away as pending.
962 */
Johannes Berg252b86c2011-11-16 15:28:55 +0100963 if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100964 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200965
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100966 /* update duration/seq/flags of fragments */
967 fragnum = 0;
Johannes Berg252b86c2011-11-16 15:28:55 +0100968
969 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100970 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
Johannes Berge2454942008-05-15 12:55:28 +0200971
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100972 hdr = (void *)skb->data;
973 info = IEEE80211_SKB_CB(skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200974
Johannes Berg252b86c2011-11-16 15:28:55 +0100975 if (!skb_queue_is_last(&tx->skbs, skb)) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100976 hdr->frame_control |= morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200977 /*
978 * No multi-rate retries for fragmented frames, that
979 * would completely throw off the NAV at other STAs.
980 */
981 info->control.rates[1].idx = -1;
982 info->control.rates[2].idx = -1;
983 info->control.rates[3].idx = -1;
Thomas Huehne3e1a0b2012-07-02 19:46:16 +0200984 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
Johannes Berge6a98542008-10-21 12:40:02 +0200985 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100986 } else {
987 hdr->frame_control &= ~morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200988 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100989 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
990 fragnum++;
Johannes Berg252b86c2011-11-16 15:28:55 +0100991 }
Johannes Berge2454942008-05-15 12:55:28 +0200992
993 return TX_CONTINUE;
Johannes Berge2454942008-05-15 12:55:28 +0200994}
995
Johannes Bergd9e8a702008-06-30 15:10:44 +0200996static ieee80211_tx_result debug_noinline
Johannes Bergfeff1f22009-08-10 16:01:54 +0200997ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
998{
Johannes Berg252b86c2011-11-16 15:28:55 +0100999 struct sk_buff *skb;
Johannes Berg560d2682013-02-05 10:55:21 +01001000 int ac = -1;
Johannes Bergfeff1f22009-08-10 16:01:54 +02001001
1002 if (!tx->sta)
1003 return TX_CONTINUE;
1004
Johannes Berg252b86c2011-11-16 15:28:55 +01001005 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg560d2682013-02-05 10:55:21 +01001006 ac = skb_get_queue_mapping(skb);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001007 tx->sta->tx_stats.bytes[ac] += skb->len;
Johannes Berg252b86c2011-11-16 15:28:55 +01001008 }
Johannes Berg560d2682013-02-05 10:55:21 +01001009 if (ac >= 0)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001010 tx->sta->tx_stats.packets[ac]++;
Johannes Bergfeff1f22009-08-10 16:01:54 +02001011
1012 return TX_CONTINUE;
1013}
1014
1015static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +02001016ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
1017{
1018 if (!tx->key)
1019 return TX_CONTINUE;
1020
Johannes Berg97359d12010-08-10 09:46:38 +02001021 switch (tx->key->conf.cipher) {
1022 case WLAN_CIPHER_SUITE_WEP40:
1023 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge2454942008-05-15 12:55:28 +02001024 return ieee80211_crypto_wep_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001025 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berge2454942008-05-15 12:55:28 +02001026 return ieee80211_crypto_tkip_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001027 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001028 return ieee80211_crypto_ccmp_encrypt(
1029 tx, IEEE80211_CCMP_MIC_LEN);
1030 case WLAN_CIPHER_SUITE_CCMP_256:
1031 return ieee80211_crypto_ccmp_encrypt(
1032 tx, IEEE80211_CCMP_256_MIC_LEN);
Johannes Berg97359d12010-08-10 09:46:38 +02001033 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001034 return ieee80211_crypto_aes_cmac_encrypt(tx);
Jouni Malinen56c52da2015-01-24 19:52:08 +02001035 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1036 return ieee80211_crypto_aes_cmac_256_encrypt(tx);
Jouni Malinen8ade5382015-01-24 19:52:09 +02001037 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1038 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1039 return ieee80211_crypto_aes_gmac_encrypt(tx);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001040 case WLAN_CIPHER_SUITE_GCMP:
1041 case WLAN_CIPHER_SUITE_GCMP_256:
1042 return ieee80211_crypto_gcmp_encrypt(tx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +03001043 default:
Yoni Divinskyd32a1022012-01-16 15:18:59 +02001044 return ieee80211_crypto_hw_encrypt(tx);
Johannes Berge2454942008-05-15 12:55:28 +02001045 }
1046
Johannes Berge2454942008-05-15 12:55:28 +02001047 return TX_DROP;
1048}
1049
Johannes Bergd9e8a702008-06-30 15:10:44 +02001050static ieee80211_tx_result debug_noinline
Johannes Berg03f93c32008-06-25 14:36:27 +03001051ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1052{
Johannes Berg252b86c2011-11-16 15:28:55 +01001053 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001054 struct ieee80211_hdr *hdr;
1055 int next_len;
1056 bool group_addr;
Johannes Berg03f93c32008-06-25 14:36:27 +03001057
Johannes Berg252b86c2011-11-16 15:28:55 +01001058 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001059 hdr = (void *) skb->data;
Jouni Malinen7e0aae42009-05-19 19:25:58 +03001060 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1061 break; /* must not overwrite AID */
Johannes Berg252b86c2011-11-16 15:28:55 +01001062 if (!skb_queue_is_last(&tx->skbs, skb)) {
1063 struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1064 next_len = next->len;
1065 } else
1066 next_len = 0;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001067 group_addr = is_multicast_ether_addr(hdr->addr1);
Johannes Berg03f93c32008-06-25 14:36:27 +03001068
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001069 hdr->duration_id =
Johannes Berg252b86c2011-11-16 15:28:55 +01001070 ieee80211_duration(tx, skb, group_addr, next_len);
1071 }
Johannes Berg03f93c32008-06-25 14:36:27 +03001072
1073 return TX_CONTINUE;
1074}
1075
Johannes Berge2ebc742007-07-27 15:43:22 +02001076/* actual transmit path */
1077
Johannes Berga622ab72010-06-10 10:21:39 +02001078static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1079 struct sk_buff *skb,
1080 struct ieee80211_tx_info *info,
1081 struct tid_ampdu_tx *tid_tx,
1082 int tid)
1083{
1084 bool queued = false;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001085 bool reset_agg_timer = false;
Helmut Schaaaa454582012-03-07 17:20:30 +01001086 struct sk_buff *purge_skb = NULL;
Johannes Berga622ab72010-06-10 10:21:39 +02001087
1088 if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1089 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001090 reset_agg_timer = true;
Johannes Berg0ab33702010-06-10 10:21:42 +02001091 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1092 /*
1093 * nothing -- this aggregation session is being started
1094 * but that might still fail with the driver
1095 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001096 } else if (!tx->sta->sta.txq[tid]) {
Johannes Berga622ab72010-06-10 10:21:39 +02001097 spin_lock(&tx->sta->lock);
1098 /*
1099 * Need to re-check now, because we may get here
1100 *
1101 * 1) in the window during which the setup is actually
1102 * already done, but not marked yet because not all
1103 * packets are spliced over to the driver pending
1104 * queue yet -- if this happened we acquire the lock
1105 * either before or after the splice happens, but
1106 * need to recheck which of these cases happened.
1107 *
1108 * 2) during session teardown, if the OPERATIONAL bit
1109 * was cleared due to the teardown but the pointer
1110 * hasn't been assigned NULL yet (or we loaded it
1111 * before it was assigned) -- in this case it may
1112 * now be NULL which means we should just let the
1113 * packet pass through because splicing the frames
1114 * back is already done.
1115 */
Johannes Berg40b275b2011-05-13 14:15:49 +02001116 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
Johannes Berga622ab72010-06-10 10:21:39 +02001117
1118 if (!tid_tx) {
1119 /* do nothing, let packet pass through */
1120 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1121 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001122 reset_agg_timer = true;
Johannes Berga622ab72010-06-10 10:21:39 +02001123 } else {
1124 queued = true;
Emmanuel Grumbachf6d46712016-03-17 16:51:42 +02001125 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
1126 clear_sta_flag(tx->sta, WLAN_STA_SP);
1127 ps_dbg(tx->sta->sdata,
1128 "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
1129 tx->sta->sta.addr, tx->sta->sta.aid);
1130 }
Johannes Berga622ab72010-06-10 10:21:39 +02001131 info->control.vif = &tx->sdata->vif;
Felix Fietkaucc20ff22020-09-08 14:36:57 +02001132 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
Emmanuel Grumbachfacde7f2016-03-17 16:51:41 +02001133 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berga622ab72010-06-10 10:21:39 +02001134 __skb_queue_tail(&tid_tx->pending, skb);
Helmut Schaaaa454582012-03-07 17:20:30 +01001135 if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1136 purge_skb = __skb_dequeue(&tid_tx->pending);
Johannes Berga622ab72010-06-10 10:21:39 +02001137 }
1138 spin_unlock(&tx->sta->lock);
Helmut Schaaaa454582012-03-07 17:20:30 +01001139
1140 if (purge_skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001141 ieee80211_free_txskb(&tx->local->hw, purge_skb);
Johannes Berga622ab72010-06-10 10:21:39 +02001142 }
1143
Nikolay Martynov285fa692011-11-22 21:50:28 -05001144 /* reset session timer */
Sara Sharon914eac22018-04-20 13:49:19 +03001145 if (reset_agg_timer)
Felix Fietkau12d3952f2012-03-18 22:58:06 +01001146 tid_tx->last_tx = jiffies;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001147
Johannes Berga622ab72010-06-10 10:21:39 +02001148 return queued;
1149}
1150
Felix Fietkau1a791552021-06-29 13:28:53 +02001151static void
1152ieee80211_aggr_check(struct ieee80211_sub_if_data *sdata,
1153 struct sta_info *sta,
1154 struct sk_buff *skb)
1155{
1156 struct rate_control_ref *ref = sdata->local->rate_ctrl;
1157 u16 tid;
1158
1159 if (!ref || !(ref->ops->capa & RATE_CTRL_CAPA_AMPDU_TRIGGER))
1160 return;
1161
1162 if (!sta || !sta->sta.ht_cap.ht_supported ||
1163 !sta->sta.wme || skb_get_queue_mapping(skb) == IEEE80211_AC_VO ||
1164 skb->protocol == sdata->control_port_protocol)
1165 return;
1166
1167 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1168 if (likely(sta->ampdu_mlme.tid_tx[tid]))
1169 return;
1170
1171 ieee80211_start_tx_ba_session(&sta->sta, tid, 0);
1172}
1173
Johannes Berg58d41852007-09-26 17:53:18 +02001174/*
1175 * initialises @tx
Johannes Berg7c107702015-03-20 14:18:27 +01001176 * pass %NULL for the station if unknown, a valid pointer if known
1177 * or an ERR_PTR() if the station is known not to exist
Johannes Berg58d41852007-09-26 17:53:18 +02001178 */
Johannes Berg9ae54c82008-01-31 19:48:20 +01001179static ieee80211_tx_result
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001180ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1181 struct ieee80211_tx_data *tx,
Johannes Berg7c107702015-03-20 14:18:27 +01001182 struct sta_info *sta, struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02001183{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001184 struct ieee80211_local *local = sdata->local;
Johannes Berg58d41852007-09-26 17:53:18 +02001185 struct ieee80211_hdr *hdr;
Johannes Berge039fa42008-05-15 12:55:29 +02001186 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Felix Fietkau1a791552021-06-29 13:28:53 +02001187 bool aggr_check = false;
Johannes Berg68f2b512011-10-07 14:01:24 +02001188 int tid;
Johannes Berge2ebc742007-07-27 15:43:22 +02001189
1190 memset(tx, 0, sizeof(*tx));
1191 tx->skb = skb;
Johannes Berge2ebc742007-07-27 15:43:22 +02001192 tx->local = local;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001193 tx->sdata = sdata;
Johannes Berg252b86c2011-11-16 15:28:55 +01001194 __skb_queue_head_init(&tx->skbs);
Johannes Berge2ebc742007-07-27 15:43:22 +02001195
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001196 /*
1197 * If this flag is set to true anywhere, and we get here,
1198 * we are doing the needed processing, so remove the flag
1199 * now.
1200 */
Felix Fietkaucc20ff22020-09-08 14:36:57 +02001201 info->control.flags &= ~IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001202
Johannes Berg58d41852007-09-26 17:53:18 +02001203 hdr = (struct ieee80211_hdr *) skb->data;
1204
Johannes Berg7c107702015-03-20 14:18:27 +01001205 if (likely(sta)) {
1206 if (!IS_ERR(sta))
1207 tx->sta = sta;
1208 } else {
1209 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1210 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1211 if (!tx->sta && sdata->wdev.use_4addr)
1212 return TX_DROP;
Markus Theil10cb8e62021-02-06 12:51:12 +01001213 } else if (tx->sdata->control_port_protocol == tx->skb->protocol) {
Johannes Berg7c107702015-03-20 14:18:27 +01001214 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1215 }
Felix Fietkau1a791552021-06-29 13:28:53 +02001216 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg7c107702015-03-20 14:18:27 +01001217 tx->sta = sta_info_get(sdata, hdr->addr1);
Felix Fietkau1a791552021-06-29 13:28:53 +02001218 aggr_check = true;
1219 }
Felix Fietkau3f0e0b22010-01-08 18:15:13 +01001220 }
Johannes Berg58d41852007-09-26 17:53:18 +02001221
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001222 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
Johannes Berg49a59542011-09-29 16:04:41 +02001223 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001224 ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
1225 !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001226 struct tid_ampdu_tx *tid_tx;
1227
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001228 tid = ieee80211_get_tid(hdr);
Johannes Berga622ab72010-06-10 10:21:39 +02001229 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
Felix Fietkau1a791552021-06-29 13:28:53 +02001230 if (!tid_tx && aggr_check) {
1231 ieee80211_aggr_check(sdata, tx->sta, skb);
1232 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1233 }
1234
Johannes Berga622ab72010-06-10 10:21:39 +02001235 if (tid_tx) {
1236 bool queued;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001237
Johannes Berga622ab72010-06-10 10:21:39 +02001238 queued = ieee80211_tx_prep_agg(tx, skb, info,
1239 tid_tx, tid);
1240
1241 if (unlikely(queued))
1242 return TX_QUEUED;
1243 }
Sujith8b30b1f2008-10-24 09:55:27 +05301244 }
1245
Jiri Slabybadffb72007-08-28 17:01:54 -04001246 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001247 tx->flags &= ~IEEE80211_TX_UNICAST;
Johannes Berge039fa42008-05-15 12:55:29 +02001248 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Simon Wunderlich6fd67e92011-11-18 14:20:42 +01001249 } else
Johannes Berg5cf121c2008-02-25 16:27:43 +01001250 tx->flags |= IEEE80211_TX_UNICAST;
Johannes Berg58d41852007-09-26 17:53:18 +02001251
Johannes Berga26eb272011-10-07 14:01:25 +02001252 if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1253 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1254 skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1255 info->flags & IEEE80211_TX_CTL_AMPDU)
1256 info->flags |= IEEE80211_TX_CTL_DONTFRAG;
Johannes Berg58d41852007-09-26 17:53:18 +02001257 }
1258
Johannes Berge2ebc742007-07-27 15:43:22 +02001259 if (!tx->sta)
Johannes Berge039fa42008-05-15 12:55:29 +02001260 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Felix Fietkauf7418bc2015-09-24 14:59:49 +02001261 else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
Johannes Berge039fa42008-05-15 12:55:29 +02001262 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Felix Fietkauf7418bc2015-09-24 14:59:49 +02001263 ieee80211_check_fast_xmit(tx->sta);
1264 }
Johannes Berg58d41852007-09-26 17:53:18 +02001265
Johannes Berge039fa42008-05-15 12:55:29 +02001266 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001267
Johannes Berg9ae54c82008-01-31 19:48:20 +01001268 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001269}
1270
Michal Kazior80a83cf2016-05-19 10:37:48 +02001271static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
1272 struct ieee80211_vif *vif,
Michal Kaziordbef5362017-01-13 13:32:51 +01001273 struct sta_info *sta,
Michal Kazior80a83cf2016-05-19 10:37:48 +02001274 struct sk_buff *skb)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001275{
1276 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001277 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001278 struct ieee80211_txq *txq = NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001279
Felix Fietkauc3732a72016-02-28 15:19:53 +01001280 if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
1281 (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
Michal Kazior80a83cf2016-05-19 10:37:48 +02001282 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001283
Felix Fietkaucc20ff22020-09-08 14:36:57 +02001284 if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) &&
John Crispin50ff4772019-11-25 11:04:37 +01001285 unlikely(!ieee80211_is_data_present(hdr->frame_control))) {
Johannes Bergadf8ed02018-08-31 11:31:08 +03001286 if ((!ieee80211_is_mgmt(hdr->frame_control) ||
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001287 ieee80211_is_bufferable_mmpdu(hdr->frame_control) ||
1288 vif->type == NL80211_IFTYPE_STATION) &&
Johannes Bergadf8ed02018-08-31 11:31:08 +03001289 sta && sta->uploaded) {
1290 /*
1291 * This will be NULL if the driver didn't set the
1292 * opt-in hardware flag.
1293 */
1294 txq = sta->sta.txq[IEEE80211_NUM_TIDS];
1295 }
1296 } else if (sta) {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001297 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1298
Michal Kaziordbef5362017-01-13 13:32:51 +01001299 if (!sta->uploaded)
1300 return NULL;
1301
1302 txq = sta->sta.txq[tid];
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001303 } else if (vif) {
1304 txq = vif->txq;
1305 }
1306
1307 if (!txq)
Michal Kazior80a83cf2016-05-19 10:37:48 +02001308 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001309
Michal Kazior80a83cf2016-05-19 10:37:48 +02001310 return to_txq_info(txq);
1311}
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001312
Michal Kazior5caa3282016-05-19 10:37:51 +02001313static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
1314{
1315 IEEE80211_SKB_CB(skb)->control.enqueue_time = codel_get_time();
1316}
1317
Michal Kazior5caa3282016-05-19 10:37:51 +02001318static u32 codel_skb_len_func(const struct sk_buff *skb)
1319{
1320 return skb->len;
1321}
1322
1323static codel_time_t codel_skb_time_func(const struct sk_buff *skb)
1324{
1325 const struct ieee80211_tx_info *info;
1326
1327 info = (const struct ieee80211_tx_info *)skb->cb;
1328 return info->control.enqueue_time;
1329}
1330
1331static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars,
1332 void *ctx)
1333{
1334 struct ieee80211_local *local;
1335 struct txq_info *txqi;
1336 struct fq *fq;
1337 struct fq_flow *flow;
1338
1339 txqi = ctx;
1340 local = vif_to_sdata(txqi->txq.vif)->local;
1341 fq = &local->fq;
1342
1343 if (cvars == &txqi->def_cvars)
Felix Fietkaubf9009b2020-12-18 19:47:14 +01001344 flow = &txqi->tin.default_flow;
Michal Kazior5caa3282016-05-19 10:37:51 +02001345 else
1346 flow = &fq->flows[cvars - local->cvars];
1347
1348 return fq_flow_dequeue(fq, flow);
1349}
1350
1351static void codel_drop_func(struct sk_buff *skb,
1352 void *ctx)
1353{
1354 struct ieee80211_local *local;
1355 struct ieee80211_hw *hw;
1356 struct txq_info *txqi;
1357
1358 txqi = ctx;
1359 local = vif_to_sdata(txqi->txq.vif)->local;
1360 hw = &local->hw;
1361
1362 ieee80211_free_txskb(hw, skb);
1363}
1364
Michal Kaziorfa962b92016-05-19 10:37:49 +02001365static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
1366 struct fq_tin *tin,
1367 struct fq_flow *flow)
1368{
Michal Kazior5caa3282016-05-19 10:37:51 +02001369 struct ieee80211_local *local;
1370 struct txq_info *txqi;
1371 struct codel_vars *cvars;
1372 struct codel_params *cparams;
1373 struct codel_stats *cstats;
1374
1375 local = container_of(fq, struct ieee80211_local, fq);
1376 txqi = container_of(tin, struct txq_info, tin);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001377 cstats = &txqi->cstats;
Michal Kazior5caa3282016-05-19 10:37:51 +02001378
Toke Høiland-Jørgensen484a54c2017-04-06 11:38:26 +02001379 if (txqi->txq.sta) {
1380 struct sta_info *sta = container_of(txqi->txq.sta,
1381 struct sta_info, sta);
1382 cparams = &sta->cparams;
1383 } else {
1384 cparams = &local->cparams;
1385 }
1386
Felix Fietkaubf9009b2020-12-18 19:47:14 +01001387 if (flow == &tin->default_flow)
Michal Kazior5caa3282016-05-19 10:37:51 +02001388 cvars = &txqi->def_cvars;
1389 else
1390 cvars = &local->cvars[flow - fq->flows];
1391
1392 return codel_dequeue(txqi,
1393 &flow->backlog,
1394 cparams,
1395 cvars,
1396 cstats,
1397 codel_skb_len_func,
1398 codel_skb_time_func,
1399 codel_drop_func,
1400 codel_dequeue_func);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001401}
1402
1403static void fq_skb_free_func(struct fq *fq,
1404 struct fq_tin *tin,
1405 struct fq_flow *flow,
1406 struct sk_buff *skb)
1407{
1408 struct ieee80211_local *local;
1409
1410 local = container_of(fq, struct ieee80211_local, fq);
1411 ieee80211_free_txskb(&local->hw, skb);
1412}
1413
Michal Kazior80a83cf2016-05-19 10:37:48 +02001414static void ieee80211_txq_enqueue(struct ieee80211_local *local,
1415 struct txq_info *txqi,
1416 struct sk_buff *skb)
1417{
Michal Kaziorfa962b92016-05-19 10:37:49 +02001418 struct fq *fq = &local->fq;
1419 struct fq_tin *tin = &txqi->tin;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001420 u32 flow_idx = fq_flow_idx(fq, skb);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001421
Michal Kazior5caa3282016-05-19 10:37:51 +02001422 ieee80211_set_skb_enqueue_time(skb);
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001423
1424 spin_lock_bh(&fq->lock);
Johannes Berg73bc9e02021-03-19 23:28:01 +01001425 /*
1426 * For management frames, don't really apply codel etc.,
1427 * we don't want to apply any shaping or anything we just
1428 * want to simplify the driver API by having them on the
1429 * txqi.
1430 */
Johannes Bergca47b462021-04-16 13:47:04 +02001431 if (unlikely(txqi->txq.tid == IEEE80211_NUM_TIDS)) {
1432 IEEE80211_SKB_CB(skb)->control.flags |=
1433 IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
Johannes Berg73bc9e02021-03-19 23:28:01 +01001434 __skb_queue_tail(&txqi->frags, skb);
Johannes Bergca47b462021-04-16 13:47:04 +02001435 } else {
Johannes Berg73bc9e02021-03-19 23:28:01 +01001436 fq_tin_enqueue(fq, tin, flow_idx, skb,
1437 fq_skb_free_func);
Johannes Bergca47b462021-04-16 13:47:04 +02001438 }
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001439 spin_unlock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001440}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001441
Johannes Berg2a9e2572017-10-06 11:53:33 +02001442static bool fq_vlan_filter_func(struct fq *fq, struct fq_tin *tin,
1443 struct fq_flow *flow, struct sk_buff *skb,
1444 void *data)
1445{
1446 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1447
1448 return info->control.vif == data;
1449}
1450
1451void ieee80211_txq_remove_vlan(struct ieee80211_local *local,
1452 struct ieee80211_sub_if_data *sdata)
1453{
1454 struct fq *fq = &local->fq;
1455 struct txq_info *txqi;
1456 struct fq_tin *tin;
1457 struct ieee80211_sub_if_data *ap;
1458
1459 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1460 return;
1461
1462 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1463
1464 if (!ap->vif.txq)
1465 return;
1466
1467 txqi = to_txq_info(ap->vif.txq);
1468 tin = &txqi->tin;
1469
1470 spin_lock_bh(&fq->lock);
1471 fq_tin_filter(fq, tin, fq_vlan_filter_func, &sdata->vif,
1472 fq_skb_free_func);
1473 spin_unlock_bh(&fq->lock);
1474}
1475
Michal Kaziorfa962b92016-05-19 10:37:49 +02001476void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
1477 struct sta_info *sta,
1478 struct txq_info *txqi, int tid)
1479{
1480 fq_tin_init(&txqi->tin);
Michal Kazior5caa3282016-05-19 10:37:51 +02001481 codel_vars_init(&txqi->def_cvars);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001482 codel_stats_init(&txqi->cstats);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001483 __skb_queue_head_init(&txqi->frags);
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02001484 RB_CLEAR_NODE(&txqi->schedule_order);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001485
1486 txqi->txq.vif = &sdata->vif;
1487
Johannes Bergadf8ed02018-08-31 11:31:08 +03001488 if (!sta) {
Michal Kaziorfa962b92016-05-19 10:37:49 +02001489 sdata->vif.txq = &txqi->txq;
1490 txqi->txq.tid = 0;
1491 txqi->txq.ac = IEEE80211_AC_BE;
Johannes Bergadf8ed02018-08-31 11:31:08 +03001492
1493 return;
Michal Kazior80a83cf2016-05-19 10:37:48 +02001494 }
Johannes Bergadf8ed02018-08-31 11:31:08 +03001495
1496 if (tid == IEEE80211_NUM_TIDS) {
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001497 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1498 /* Drivers need to opt in to the management MPDU TXQ */
1499 if (!ieee80211_hw_check(&sdata->local->hw,
1500 STA_MMPDU_TXQ))
1501 return;
1502 } else if (!ieee80211_hw_check(&sdata->local->hw,
1503 BUFF_MMPDU_TXQ)) {
1504 /* Drivers need to opt in to the bufferable MMPDU TXQ */
Johannes Bergadf8ed02018-08-31 11:31:08 +03001505 return;
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001506 }
Johannes Bergadf8ed02018-08-31 11:31:08 +03001507 txqi->txq.ac = IEEE80211_AC_VO;
1508 } else {
1509 txqi->txq.ac = ieee80211_ac_from_tid(tid);
1510 }
1511
1512 txqi->txq.sta = &sta->sta;
1513 txqi->txq.tid = tid;
1514 sta->sta.txq[tid] = &txqi->txq;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001515}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001516
Michal Kaziorfa962b92016-05-19 10:37:49 +02001517void ieee80211_txq_purge(struct ieee80211_local *local,
1518 struct txq_info *txqi)
1519{
1520 struct fq *fq = &local->fq;
1521 struct fq_tin *tin = &txqi->tin;
1522
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08001523 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001524 fq_tin_reset(fq, tin, fq_skb_free_func);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001525 ieee80211_purge_tx_queue(&local->hw, &txqi->frags);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08001526 spin_unlock_bh(&fq->lock);
1527
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02001528 ieee80211_unschedule_txq(&local->hw, &txqi->txq, true);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001529}
1530
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001531void ieee80211_txq_set_params(struct ieee80211_local *local)
1532{
1533 if (local->hw.wiphy->txq_limit)
1534 local->fq.limit = local->hw.wiphy->txq_limit;
1535 else
1536 local->hw.wiphy->txq_limit = local->fq.limit;
1537
1538 if (local->hw.wiphy->txq_memory_limit)
1539 local->fq.memory_limit = local->hw.wiphy->txq_memory_limit;
1540 else
1541 local->hw.wiphy->txq_memory_limit = local->fq.memory_limit;
1542
1543 if (local->hw.wiphy->txq_quantum)
1544 local->fq.quantum = local->hw.wiphy->txq_quantum;
1545 else
1546 local->hw.wiphy->txq_quantum = local->fq.quantum;
1547}
1548
Michal Kaziorfa962b92016-05-19 10:37:49 +02001549int ieee80211_txq_setup_flows(struct ieee80211_local *local)
1550{
1551 struct fq *fq = &local->fq;
1552 int ret;
Michal Kazior5caa3282016-05-19 10:37:51 +02001553 int i;
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001554 bool supp_vht = false;
1555 enum nl80211_band band;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001556
1557 if (!local->ops->wake_tx_queue)
1558 return 0;
1559
1560 ret = fq_init(fq, 4096);
1561 if (ret)
1562 return ret;
1563
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001564 /*
1565 * If the hardware doesn't support VHT, it is safe to limit the maximum
1566 * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
1567 */
1568 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1569 struct ieee80211_supported_band *sband;
1570
1571 sband = local->hw.wiphy->bands[band];
1572 if (!sband)
1573 continue;
1574
1575 supp_vht = supp_vht || sband->vht_cap.vht_supported;
1576 }
1577
1578 if (!supp_vht)
1579 fq->memory_limit = 4 << 20; /* 4 Mbytes */
1580
Michal Kazior5caa3282016-05-19 10:37:51 +02001581 codel_params_init(&local->cparams);
Michal Kazior5caa3282016-05-19 10:37:51 +02001582 local->cparams.interval = MS2TIME(100);
1583 local->cparams.target = MS2TIME(20);
1584 local->cparams.ecn = true;
1585
1586 local->cvars = kcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
1587 GFP_KERNEL);
1588 if (!local->cvars) {
Michal Kazior59a7c822016-06-29 14:00:34 +02001589 spin_lock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001590 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001591 spin_unlock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001592 return -ENOMEM;
1593 }
1594
1595 for (i = 0; i < fq->flows_cnt; i++)
1596 codel_vars_init(&local->cvars[i]);
1597
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001598 ieee80211_txq_set_params(local);
1599
Michal Kaziorfa962b92016-05-19 10:37:49 +02001600 return 0;
1601}
1602
1603void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1604{
1605 struct fq *fq = &local->fq;
1606
1607 if (!local->ops->wake_tx_queue)
1608 return;
1609
Michal Kazior5caa3282016-05-19 10:37:51 +02001610 kfree(local->cvars);
1611 local->cvars = NULL;
1612
Michal Kazior59a7c822016-06-29 14:00:34 +02001613 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001614 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001615 spin_unlock_bh(&fq->lock);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001616}
1617
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001618static bool ieee80211_queue_skb(struct ieee80211_local *local,
1619 struct ieee80211_sub_if_data *sdata,
1620 struct sta_info *sta,
1621 struct sk_buff *skb)
1622{
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001623 struct ieee80211_vif *vif;
1624 struct txq_info *txqi;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001625
1626 if (!local->ops->wake_tx_queue ||
1627 sdata->vif.type == NL80211_IFTYPE_MONITOR)
1628 return false;
1629
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001630 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1631 sdata = container_of(sdata->bss,
1632 struct ieee80211_sub_if_data, u.ap);
1633
1634 vif = &sdata->vif;
Michal Kaziordbef5362017-01-13 13:32:51 +01001635 txqi = ieee80211_get_txq(local, vif, sta, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001636
1637 if (!txqi)
1638 return false;
1639
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001640 ieee80211_txq_enqueue(local, txqi, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001641
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001642 schedule_and_wake_txq(local, txqi);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001643
1644 return true;
1645}
1646
Johannes Berg11127e92011-11-16 16:02:47 +01001647static bool ieee80211_tx_frags(struct ieee80211_local *local,
1648 struct ieee80211_vif *vif,
Johannes Berg4fd03282019-10-01 23:26:35 +02001649 struct sta_info *sta,
Johannes Berg11127e92011-11-16 16:02:47 +01001650 struct sk_buff_head *skbs,
1651 bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001652{
Michal Kazior80a83cf2016-05-19 10:37:48 +02001653 struct ieee80211_tx_control control = {};
Johannes Berg252b86c2011-11-16 15:28:55 +01001654 struct sk_buff *skb, *tmp;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001655 unsigned long flags;
Johannes Berge2ebc742007-07-27 15:43:22 +02001656
Johannes Berg252b86c2011-11-16 15:28:55 +01001657 skb_queue_walk_safe(skbs, skb, tmp) {
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001658 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1659 int q = info->hw_queue;
1660
1661#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1662 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1663 __skb_unlink(skb, skbs);
Felix Fietkauc3e77242012-10-08 14:39:33 +02001664 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001665 continue;
1666 }
1667#endif
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001668
1669 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001670 if (local->queue_stop_reasons[q] ||
Johannes Berg7bb45682011-02-24 14:42:06 +01001671 (!txpending && !skb_queue_empty(&local->pending[q]))) {
Seth Forshee6c17b772013-02-11 11:21:07 -06001672 if (unlikely(info->flags &
Seth Forsheea7679ed2013-02-25 14:58:05 -06001673 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1674 if (local->queue_stop_reasons[q] &
1675 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1676 /*
1677 * Drop off-channel frames if queues
1678 * are stopped for any reason other
1679 * than off-channel operation. Never
1680 * queue them.
1681 */
1682 spin_unlock_irqrestore(
1683 &local->queue_stop_reason_lock,
1684 flags);
1685 ieee80211_purge_tx_queue(&local->hw,
1686 skbs);
1687 return true;
1688 }
1689 } else {
1690
Seth Forshee6c17b772013-02-11 11:21:07 -06001691 /*
Seth Forsheea7679ed2013-02-25 14:58:05 -06001692 * Since queue is stopped, queue up frames for
1693 * later transmission from the tx-pending
1694 * tasklet when the queue is woken again.
Seth Forshee6c17b772013-02-11 11:21:07 -06001695 */
Seth Forsheea7679ed2013-02-25 14:58:05 -06001696 if (txpending)
1697 skb_queue_splice_init(skbs,
1698 &local->pending[q]);
1699 else
1700 skb_queue_splice_tail_init(skbs,
1701 &local->pending[q]);
1702
1703 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1704 flags);
1705 return false;
Seth Forshee6c17b772013-02-11 11:21:07 -06001706 }
Johannes Berg7bb45682011-02-24 14:42:06 +01001707 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001708 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Tomas Winklerf8e79dd2008-07-24 18:46:44 +03001709
Johannes Berg11127e92011-11-16 16:02:47 +01001710 info->control.vif = vif;
Johannes Berg4fd03282019-10-01 23:26:35 +02001711 control.sta = sta ? &sta->sta : NULL;
Johannes Bergec25acc2010-07-22 17:11:28 +02001712
Johannes Berg252b86c2011-11-16 15:28:55 +01001713 __skb_unlink(skb, skbs);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001714 drv_tx(local, &control, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001715 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001716
Johannes Berg11127e92011-11-16 16:02:47 +01001717 return true;
1718}
1719
1720/*
1721 * Returns false if the frame couldn't be transmitted but was queued instead.
1722 */
1723static bool __ieee80211_tx(struct ieee80211_local *local,
Felix Fietkau30f6cf92021-11-13 07:34:15 +01001724 struct sk_buff_head *skbs, struct sta_info *sta,
1725 bool txpending)
Johannes Berg11127e92011-11-16 16:02:47 +01001726{
1727 struct ieee80211_tx_info *info;
1728 struct ieee80211_sub_if_data *sdata;
1729 struct ieee80211_vif *vif;
Johannes Berg11127e92011-11-16 16:02:47 +01001730 struct sk_buff *skb;
Colin Ian King958574c2021-03-28 22:37:29 +01001731 bool result;
Johannes Berg11127e92011-11-16 16:02:47 +01001732
1733 if (WARN_ON(skb_queue_empty(skbs)))
1734 return true;
1735
1736 skb = skb_peek(skbs);
Johannes Berg11127e92011-11-16 16:02:47 +01001737 info = IEEE80211_SKB_CB(skb);
1738 sdata = vif_to_sdata(info->control.vif);
1739 if (sta && !sta->uploaded)
1740 sta = NULL;
1741
Johannes Berg11127e92011-11-16 16:02:47 +01001742 switch (sdata->vif.type) {
1743 case NL80211_IFTYPE_MONITOR:
Aviya Erenfeldd8212182016-08-29 23:25:15 +03001744 if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
Johannes Bergc82b5a72013-07-14 23:39:20 +03001745 vif = &sdata->vif;
1746 break;
1747 }
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001748 sdata = rcu_dereference(local->monitor_sdata);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001749 if (sdata) {
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001750 vif = &sdata->vif;
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001751 info->hw_queue =
1752 vif->hw_queue[skb_get_queue_mapping(skb)];
Johannes Berg30686bf2015-06-02 21:39:54 +02001753 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
Johannes Berg63b4d8b2015-11-24 15:41:50 +01001754 ieee80211_purge_tx_queue(&local->hw, skbs);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001755 return true;
1756 } else
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001757 vif = NULL;
Johannes Berg11127e92011-11-16 16:02:47 +01001758 break;
1759 case NL80211_IFTYPE_AP_VLAN:
1760 sdata = container_of(sdata->bss,
1761 struct ieee80211_sub_if_data, u.ap);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05001762 fallthrough;
Johannes Berg11127e92011-11-16 16:02:47 +01001763 default:
1764 vif = &sdata->vif;
1765 break;
1766 }
1767
Johannes Berg4fd03282019-10-01 23:26:35 +02001768 result = ieee80211_tx_frags(local, vif, sta, skbs, txpending);
Johannes Berg11127e92011-11-16 16:02:47 +01001769
Johannes Berg4db4e0a2011-11-24 14:47:36 +01001770 WARN_ON_ONCE(!skb_queue_empty(skbs));
Johannes Berg252b86c2011-11-16 15:28:55 +01001771
Johannes Berg11127e92011-11-16 16:02:47 +01001772 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001773}
1774
Johannes Berg97b045d2008-06-20 01:22:30 +02001775/*
1776 * Invoke TX handlers, return 0 on success and non-zero if the
1777 * frame was dropped or queued.
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001778 *
1779 * The handlers are split into an early and late part. The latter is everything
1780 * that can be sensitive to reordering, and will be deferred to after packets
1781 * are dequeued from the intermediate queues (when they are enabled).
Johannes Berg97b045d2008-06-20 01:22:30 +02001782 */
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001783static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx)
Johannes Berg97b045d2008-06-20 01:22:30 +02001784{
Johannes Berg97b045d2008-06-20 01:22:30 +02001785 ieee80211_tx_result res = TX_DROP;
Johannes Berg97b045d2008-06-20 01:22:30 +02001786
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001787#define CALL_TXH(txh) \
1788 do { \
1789 res = txh(tx); \
1790 if (res != TX_CONTINUE) \
1791 goto txh_done; \
1792 } while (0)
Johannes Berg97b045d2008-06-20 01:22:30 +02001793
Kalle Valo5c1b98a2010-01-12 10:42:46 +02001794 CALL_TXH(ieee80211_tx_h_dynamic_ps);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001795 CALL_TXH(ieee80211_tx_h_check_assoc);
1796 CALL_TXH(ieee80211_tx_h_ps_buf);
Johannes Berga621fa42010-08-27 14:26:54 +03001797 CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001798 CALL_TXH(ieee80211_tx_h_select_key);
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001799
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001800 txh_done:
1801 if (unlikely(res == TX_DROP)) {
1802 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1803 if (tx->skb)
1804 ieee80211_free_txskb(&tx->local->hw, tx->skb);
1805 else
1806 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1807 return -1;
1808 } else if (unlikely(res == TX_QUEUED)) {
1809 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1810 return -1;
1811 }
1812
1813 return 0;
1814}
1815
1816/*
1817 * Late handlers can be called while the sta lock is held. Handlers that can
1818 * cause packets to be generated will cause deadlock!
1819 */
1820static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx)
1821{
1822 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1823 ieee80211_tx_result res = TX_CONTINUE;
1824
Felix Fietkau18688c82021-11-22 21:43:23 +01001825 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1826 CALL_TXH(ieee80211_tx_h_rate_ctrl);
1827
Johannes Bergaa5b5492011-12-02 22:08:52 +01001828 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1829 __skb_queue_tail(&tx->skbs, tx->skb);
1830 tx->skb = NULL;
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001831 goto txh_done;
Johannes Bergaa5b5492011-12-02 22:08:52 +01001832 }
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001833
1834 CALL_TXH(ieee80211_tx_h_michael_mic_add);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001835 CALL_TXH(ieee80211_tx_h_sequence);
1836 CALL_TXH(ieee80211_tx_h_fragment);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001837 /* handlers after fragment must be aware of tx info fragmentation! */
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001838 CALL_TXH(ieee80211_tx_h_stats);
1839 CALL_TXH(ieee80211_tx_h_encrypt);
Johannes Berg30686bf2015-06-02 21:39:54 +02001840 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
Arik Nemtsov8fd369e2011-01-31 22:29:12 +02001841 CALL_TXH(ieee80211_tx_h_calculate_duration);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001842#undef CALL_TXH
1843
1844 txh_done:
Johannes Berg97b045d2008-06-20 01:22:30 +02001845 if (unlikely(res == TX_DROP)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001846 I802_DEBUG_INC(tx->local->tx_handlers_drop);
Johannes Berg252b86c2011-11-16 15:28:55 +01001847 if (tx->skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001848 ieee80211_free_txskb(&tx->local->hw, tx->skb);
Johannes Berg252b86c2011-11-16 15:28:55 +01001849 else
Felix Fietkau1f98ab72012-11-10 03:44:14 +01001850 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
Johannes Berg97b045d2008-06-20 01:22:30 +02001851 return -1;
1852 } else if (unlikely(res == TX_QUEUED)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001853 I802_DEBUG_INC(tx->local->tx_handlers_queued);
Johannes Berg97b045d2008-06-20 01:22:30 +02001854 return -1;
1855 }
1856
1857 return 0;
1858}
1859
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001860static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1861{
1862 int r = invoke_tx_handlers_early(tx);
1863
1864 if (r)
1865 return r;
1866 return invoke_tx_handlers_late(tx);
1867}
1868
Felix Fietkau06be6b12013-10-14 18:01:00 +02001869bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1870 struct ieee80211_vif *vif, struct sk_buff *skb,
1871 int band, struct ieee80211_sta **sta)
1872{
1873 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1874 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1875 struct ieee80211_tx_data tx;
Johannes Berg88724a82015-03-01 09:10:12 +02001876 struct sk_buff *skb2;
Felix Fietkau06be6b12013-10-14 18:01:00 +02001877
Johannes Berg7c107702015-03-20 14:18:27 +01001878 if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
Felix Fietkau06be6b12013-10-14 18:01:00 +02001879 return false;
1880
1881 info->band = band;
1882 info->control.vif = vif;
1883 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1884
1885 if (invoke_tx_handlers(&tx))
1886 return false;
1887
1888 if (sta) {
1889 if (tx.sta)
1890 *sta = &tx.sta->sta;
1891 else
1892 *sta = NULL;
1893 }
1894
Johannes Berg88724a82015-03-01 09:10:12 +02001895 /* this function isn't suitable for fragmented data frames */
1896 skb2 = __skb_dequeue(&tx.skbs);
1897 if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1898 ieee80211_free_txskb(hw, skb2);
1899 ieee80211_purge_tx_queue(hw, &tx.skbs);
1900 return false;
1901 }
1902
Felix Fietkau06be6b12013-10-14 18:01:00 +02001903 return true;
1904}
1905EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1906
Johannes Berg7bb45682011-02-24 14:42:06 +01001907/*
1908 * Returns false if the frame couldn't be transmitted but was queued instead.
1909 */
1910static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01001911 struct sta_info *sta, struct sk_buff *skb,
Mathy Vanhoef08aca292020-07-23 14:01:52 +04001912 bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001913{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001914 struct ieee80211_local *local = sdata->local;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001915 struct ieee80211_tx_data tx;
Johannes Berg97b045d2008-06-20 01:22:30 +02001916 ieee80211_tx_result res_prepare;
Johannes Berge039fa42008-05-15 12:55:29 +02001917 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001918 bool result = true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001919
Johannes Berge2ebc742007-07-27 15:43:22 +02001920 if (unlikely(skb->len < 10)) {
1921 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001922 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001923 }
1924
Johannes Berg58d41852007-09-26 17:53:18 +02001925 /* initialises tx */
Johannes Berg7c107702015-03-20 14:18:27 +01001926 res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001927
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001928 if (unlikely(res_prepare == TX_DROP)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02001929 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001930 return true;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001931 } else if (unlikely(res_prepare == TX_QUEUED)) {
Johannes Berg55de9082012-07-26 17:24:39 +02001932 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001933 }
1934
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001935 /* set up hw_queue value early */
1936 if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
Johannes Berg30686bf2015-06-02 21:39:54 +02001937 !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001938 info->hw_queue =
1939 sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1940
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001941 if (invoke_tx_handlers_early(&tx))
Bob Copeland6eae4a62018-09-05 06:22:59 -04001942 return true;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001943
1944 if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb))
1945 return true;
1946
1947 if (!invoke_tx_handlers_late(&tx))
Felix Fietkau30f6cf92021-11-13 07:34:15 +01001948 result = __ieee80211_tx(local, &tx.skbs, tx.sta, txpending);
Johannes Berg55de9082012-07-26 17:24:39 +02001949
Johannes Berg7bb45682011-02-24 14:42:06 +01001950 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001951}
1952
1953/* device xmit handlers */
1954
Johannes Berg14f46c12020-10-09 13:25:41 +02001955enum ieee80211_encrypt {
1956 ENCRYPT_NO,
1957 ENCRYPT_MGMT,
1958 ENCRYPT_DATA,
1959};
1960
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301961static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
Johannes Berg23c07522008-05-29 10:38:53 +02001962 struct sk_buff *skb,
Johannes Berg14f46c12020-10-09 13:25:41 +02001963 int head_need,
1964 enum ieee80211_encrypt encrypt)
Johannes Berg23c07522008-05-29 10:38:53 +02001965{
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301966 struct ieee80211_local *local = sdata->local;
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001967 bool enc_tailroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001968 int tail_need = 0;
1969
Johannes Berg14f46c12020-10-09 13:25:41 +02001970 enc_tailroom = encrypt == ENCRYPT_MGMT ||
1971 (encrypt == ENCRYPT_DATA &&
1972 sdata->crypto_tx_tailroom_needed_cnt);
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001973
1974 if (enc_tailroom) {
Johannes Berg23c07522008-05-29 10:38:53 +02001975 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1976 tail_need -= skb_tailroom(skb);
1977 tail_need = max_t(int, tail_need, 0);
1978 }
1979
Ido Yarivc70f59a2014-07-29 15:39:14 +03001980 if (skb_cloned(skb) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001981 (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001982 !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
Johannes Berg23c07522008-05-29 10:38:53 +02001983 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001984 else if (head_need || tail_need)
Johannes Berg23c07522008-05-29 10:38:53 +02001985 I802_DEBUG_INC(local->tx_expand_skb_head);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001986 else
1987 return 0;
Johannes Berg23c07522008-05-29 10:38:53 +02001988
1989 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
Joe Perches0fb9a9e2010-08-20 16:25:38 -07001990 wiphy_debug(local->hw.wiphy,
1991 "failed to reallocate TX buffer\n");
Johannes Berg23c07522008-05-29 10:38:53 +02001992 return -ENOMEM;
1993 }
1994
Johannes Berg23c07522008-05-29 10:38:53 +02001995 return 0;
1996}
1997
Johannes Berg7c107702015-03-20 14:18:27 +01001998void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
Mathy Vanhoef08aca292020-07-23 14:01:52 +04001999 struct sta_info *sta, struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02002000{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002001 struct ieee80211_local *local = sdata->local;
Johannes Berge039fa42008-05-15 12:55:29 +02002002 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg14f46c12020-10-09 13:25:41 +02002003 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berge2ebc742007-07-27 15:43:22 +02002004 int headroom;
Johannes Berg14f46c12020-10-09 13:25:41 +02002005 enum ieee80211_encrypt encrypt;
Johannes Berge2ebc742007-07-27 15:43:22 +02002006
Johannes Berg14f46c12020-10-09 13:25:41 +02002007 if (info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)
2008 encrypt = ENCRYPT_NO;
2009 else if (ieee80211_is_mgmt(hdr->frame_control))
2010 encrypt = ENCRYPT_MGMT;
2011 else
2012 encrypt = ENCRYPT_DATA;
Johannes Berg23c07522008-05-29 10:38:53 +02002013
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002014 headroom = local->tx_headroom;
Johannes Berg14f46c12020-10-09 13:25:41 +02002015 if (encrypt != ENCRYPT_NO)
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002016 headroom += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02002017 headroom -= skb_headroom(skb);
2018 headroom = max_t(int, 0, headroom);
2019
Johannes Berg14f46c12020-10-09 13:25:41 +02002020 if (ieee80211_skb_resize(sdata, skb, headroom, encrypt)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02002021 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002022 return;
Johannes Berge2ebc742007-07-27 15:43:22 +02002023 }
2024
Johannes Berg14f46c12020-10-09 13:25:41 +02002025 /* reload after potential resize */
Steve deRosier740c1aa2010-09-11 20:01:31 -07002026 hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berg5061b0c2009-07-14 00:33:34 +02002027 info->control.vif = &sdata->vif;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01002028
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002029 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2030 if (ieee80211_is_data(hdr->frame_control) &&
2031 is_unicast_ether_addr(hdr->addr1)) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002032 if (mesh_nexthop_resolve(sdata, skb))
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002033 return; /* skb queued: don't free */
2034 } else {
2035 ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
2036 }
Johannes Berg98aed9f2012-03-27 14:18:36 +02002037 }
Javier Cardonacca89492009-08-10 17:29:29 -07002038
Javier Cardona2154c81c2011-09-07 17:49:53 -07002039 ieee80211_set_qos_hdr(sdata, skb);
Mathy Vanhoef08aca292020-07-23 14:01:52 +04002040 ieee80211_tx(sdata, sta, skb, false);
Johannes Berge2ebc742007-07-27 15:43:22 +02002041}
2042
Mathy Vanhoefbddc0c42021-05-30 15:32:26 +02002043static bool ieee80211_validate_radiotap_len(struct sk_buff *skb)
Johannes Berg73b9f032011-10-07 14:01:26 +02002044{
Johannes Berg73b9f032011-10-07 14:01:26 +02002045 struct ieee80211_radiotap_header *rthdr =
Mathy Vanhoefbddc0c42021-05-30 15:32:26 +02002046 (struct ieee80211_radiotap_header *)skb->data;
Johannes Berg73b9f032011-10-07 14:01:26 +02002047
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002048 /* check for not even having the fixed radiotap header part */
2049 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2050 return false; /* too short to be possibly valid */
2051
2052 /* is it a header version we can trust to find length from? */
2053 if (unlikely(rthdr->it_version))
2054 return false; /* only version 0 is supported */
2055
2056 /* does the skb contain enough to deliver on the alleged length? */
2057 if (unlikely(skb->len < ieee80211_get_radiotap_len(skb->data)))
2058 return false; /* skb too short for claimed rt header extent */
2059
Mathy Vanhoefbddc0c42021-05-30 15:32:26 +02002060 return true;
2061}
2062
2063bool ieee80211_parse_tx_radiotap(struct sk_buff *skb,
2064 struct net_device *dev)
2065{
2066 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2067 struct ieee80211_radiotap_iterator iterator;
2068 struct ieee80211_radiotap_header *rthdr =
2069 (struct ieee80211_radiotap_header *) skb->data;
2070 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2071 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
2072 NULL);
2073 u16 txflags;
2074 u16 rate = 0;
2075 bool rate_found = false;
2076 u8 rate_retries = 0;
2077 u16 rate_flags = 0;
2078 u8 mcs_known, mcs_flags, mcs_bw;
2079 u16 vht_known;
2080 u8 vht_mcs = 0, vht_nss = 0;
2081 int i;
2082
2083 if (!ieee80211_validate_radiotap_len(skb))
2084 return false;
2085
Johannes Berg73b9f032011-10-07 14:01:26 +02002086 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
2087 IEEE80211_TX_CTL_DONTFRAG;
2088
2089 /*
2090 * for every radiotap entry that is present
2091 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
2092 * entries present, or -EINVAL on error)
2093 */
2094
2095 while (!ret) {
2096 ret = ieee80211_radiotap_iterator_next(&iterator);
2097
2098 if (ret)
2099 continue;
2100
2101 /* see if this argument is something we can use */
2102 switch (iterator.this_arg_index) {
2103 /*
2104 * You must take care when dereferencing iterator.this_arg
2105 * for multibyte types... the pointer is not aligned. Use
2106 * get_unaligned((type *)iterator.this_arg) to dereference
2107 * iterator.this_arg for type "type" safely on all arches.
2108 */
2109 case IEEE80211_RADIOTAP_FLAGS:
2110 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
2111 /*
2112 * this indicates that the skb we have been
2113 * handed has the 32-bit FCS CRC at the end...
2114 * we should react to that by snipping it off
2115 * because it will be recomputed and added
2116 * on transmission
2117 */
2118 if (skb->len < (iterator._max_length + FCS_LEN))
2119 return false;
2120
2121 skb_trim(skb, skb->len - FCS_LEN);
2122 }
2123 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
2124 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
2125 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
2126 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
2127 break;
2128
2129 case IEEE80211_RADIOTAP_TX_FLAGS:
2130 txflags = get_unaligned_le16(iterator.this_arg);
2131 if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
2132 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Mathy Vanhoefe02281e2020-07-23 14:01:49 +04002133 if (txflags & IEEE80211_RADIOTAP_F_TX_NOSEQNO)
2134 info->control.flags |= IEEE80211_TX_CTRL_NO_SEQNO;
Mathy Vanhoef30df8132020-11-04 10:18:19 +04002135 if (txflags & IEEE80211_RADIOTAP_F_TX_ORDER)
2136 info->control.flags |=
2137 IEEE80211_TX_CTRL_DONT_REORDER;
Johannes Berg73b9f032011-10-07 14:01:26 +02002138 break;
2139
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002140 case IEEE80211_RADIOTAP_RATE:
2141 rate = *iterator.this_arg;
2142 rate_flags = 0;
2143 rate_found = true;
2144 break;
2145
2146 case IEEE80211_RADIOTAP_DATA_RETRIES:
2147 rate_retries = *iterator.this_arg;
2148 break;
2149
2150 case IEEE80211_RADIOTAP_MCS:
2151 mcs_known = iterator.this_arg[0];
2152 mcs_flags = iterator.this_arg[1];
2153 if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
2154 break;
2155
2156 rate_found = true;
2157 rate = iterator.this_arg[2];
2158 rate_flags = IEEE80211_TX_RC_MCS;
2159
2160 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
2161 mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
2162 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2163
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002164 mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002165 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002166 mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002167 rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
Philipp Borgersf1864e12020-12-19 18:07:10 +01002168
2169 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_FEC &&
2170 mcs_flags & IEEE80211_RADIOTAP_MCS_FEC_LDPC)
2171 info->flags |= IEEE80211_TX_CTL_LDPC;
Philipp Borgers549fdd32021-01-25 16:07:44 +01002172
2173 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_STBC) {
2174 u8 stbc = u8_get_bits(mcs_flags,
2175 IEEE80211_RADIOTAP_MCS_STBC_MASK);
2176
2177 info->flags |=
2178 u32_encode_bits(stbc,
2179 IEEE80211_TX_CTL_STBC);
2180 }
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002181 break;
2182
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002183 case IEEE80211_RADIOTAP_VHT:
2184 vht_known = get_unaligned_le16(iterator.this_arg);
2185 rate_found = true;
2186
2187 rate_flags = IEEE80211_TX_RC_VHT_MCS;
2188 if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
2189 (iterator.this_arg[2] &
2190 IEEE80211_RADIOTAP_VHT_FLAG_SGI))
2191 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2192 if (vht_known &
2193 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
2194 if (iterator.this_arg[3] == 1)
2195 rate_flags |=
2196 IEEE80211_TX_RC_40_MHZ_WIDTH;
2197 else if (iterator.this_arg[3] == 4)
2198 rate_flags |=
2199 IEEE80211_TX_RC_80_MHZ_WIDTH;
2200 else if (iterator.this_arg[3] == 11)
2201 rate_flags |=
2202 IEEE80211_TX_RC_160_MHZ_WIDTH;
2203 }
2204
2205 vht_mcs = iterator.this_arg[4] >> 4;
Lorenzo Bianconi13cb6d82021-09-20 14:45:22 +02002206 if (vht_mcs > 11)
2207 vht_mcs = 0;
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002208 vht_nss = iterator.this_arg[4] & 0xF;
Lorenzo Bianconi13cb6d82021-09-20 14:45:22 +02002209 if (!vht_nss || vht_nss > 8)
2210 vht_nss = 1;
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002211 break;
2212
Johannes Berg73b9f032011-10-07 14:01:26 +02002213 /*
2214 * Please update the file
Mauro Carvalho Chehab429ff872020-04-30 18:03:59 +02002215 * Documentation/networking/mac80211-injection.rst
Johannes Berg73b9f032011-10-07 14:01:26 +02002216 * when parsing new fields here.
2217 */
2218
2219 default:
2220 break;
2221 }
2222 }
2223
2224 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
2225 return false;
2226
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002227 if (rate_found) {
Mathy Vanhoefbddc0c42021-05-30 15:32:26 +02002228 struct ieee80211_supported_band *sband =
2229 local->hw.wiphy->bands[info->band];
2230
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002231 info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
2232
2233 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2234 info->control.rates[i].idx = -1;
2235 info->control.rates[i].flags = 0;
2236 info->control.rates[i].count = 0;
2237 }
2238
2239 if (rate_flags & IEEE80211_TX_RC_MCS) {
2240 info->control.rates[0].idx = rate;
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002241 } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
2242 ieee80211_rate_set_vht(info->control.rates, vht_mcs,
2243 vht_nss);
Mathy Vanhoefbddc0c42021-05-30 15:32:26 +02002244 } else if (sband) {
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002245 for (i = 0; i < sband->n_bitrates; i++) {
2246 if (rate * 5 != sband->bitrates[i].bitrate)
2247 continue;
2248
2249 info->control.rates[0].idx = i;
2250 break;
2251 }
2252 }
2253
Felix Fietkau07310a62016-03-02 15:54:51 +01002254 if (info->control.rates[0].idx < 0)
2255 info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
2256
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002257 info->control.rates[0].flags = rate_flags;
2258 info->control.rates[0].count = min_t(u8, rate_retries + 1,
2259 local->hw.max_rate_tries);
2260 }
2261
Johannes Berg73b9f032011-10-07 14:01:26 +02002262 return true;
2263}
2264
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +00002265netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
2266 struct net_device *dev)
Johannes Berge2ebc742007-07-27 15:43:22 +02002267{
2268 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg55de9082012-07-26 17:24:39 +02002269 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002270 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002271 struct ieee80211_hdr *hdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002272 struct ieee80211_sub_if_data *tmp_sdata, *sdata;
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002273 struct cfg80211_chan_def *chandef;
Andy Green9b8a74e2007-07-27 15:43:24 +02002274 u16 len_rthdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002275 int hdrlen;
Johannes Berge2ebc742007-07-27 15:43:22 +02002276
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002277 memset(info, 0, sizeof(*info));
2278 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2279 IEEE80211_TX_CTL_INJECTED;
Andy Green9b8a74e2007-07-27 15:43:24 +02002280
Mathy Vanhoefbddc0c42021-05-30 15:32:26 +02002281 /* Sanity-check the length of the radiotap header */
2282 if (!ieee80211_validate_radiotap_len(skb))
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002283 goto fail;
Andy Green9b8a74e2007-07-27 15:43:24 +02002284
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002285 /* we now know there is a radiotap header with a length we can use */
Andy Green9b8a74e2007-07-27 15:43:24 +02002286 len_rthdr = ieee80211_get_radiotap_len(skb->data);
2287
Johannes Berge2ebc742007-07-27 15:43:22 +02002288 /*
2289 * fix up the pointers accounting for the radiotap
2290 * header still being in there. We are being given
2291 * a precooked IEEE80211 header so no need for
2292 * normal processing
2293 */
Andy Green9b8a74e2007-07-27 15:43:24 +02002294 skb_set_mac_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02002295 /*
Andy Green9b8a74e2007-07-27 15:43:24 +02002296 * these are just fixed to the end of the rt area since we
2297 * don't have any better information and at this point, nobody cares
Johannes Berge2ebc742007-07-27 15:43:22 +02002298 */
Andy Green9b8a74e2007-07-27 15:43:24 +02002299 skb_set_network_header(skb, len_rthdr);
2300 skb_set_transport_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02002301
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002302 if (skb->len < len_rthdr + 2)
2303 goto fail;
2304
2305 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2306 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2307
2308 if (skb->len < len_rthdr + hdrlen)
2309 goto fail;
2310
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002311 /*
2312 * Initialize skb->protocol if the injected frame is a data frame
2313 * carrying a rfc1042 header
2314 */
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002315 if (ieee80211_is_data(hdr->frame_control) &&
2316 skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2317 u8 *payload = (u8 *)hdr + hdrlen;
2318
Joe Perchesb203ca32012-05-08 18:56:52 +00002319 if (ether_addr_equal(payload, rfc1042_header))
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002320 skb->protocol = cpu_to_be16((payload[6] << 8) |
2321 payload[7]);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002322 }
2323
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002324 rcu_read_lock();
2325
2326 /*
2327 * We process outgoing injected frames that have a local address
2328 * we handle as though they are non-injected frames.
2329 * This code here isn't entirely correct, the local MAC address
2330 * isn't always enough to find the interface to use; for proper
Johannes Berg70d9c592020-11-09 10:57:46 +01002331 * VLAN support we have an nl80211-based mechanism.
Johannes Bergb226a822019-11-22 12:42:42 +01002332 *
2333 * This is necessary, for example, for old hostapd versions that
2334 * don't use nl80211-based management TX/RX.
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002335 */
2336 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2337
2338 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2339 if (!ieee80211_sdata_running(tmp_sdata))
2340 continue;
2341 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
Johannes Berg70d9c592020-11-09 10:57:46 +01002342 tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002343 continue;
Joe Perchesb203ca32012-05-08 18:56:52 +00002344 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002345 sdata = tmp_sdata;
2346 break;
2347 }
2348 }
Johannes Berg7351c6b2009-11-19 01:08:30 +01002349
Johannes Berg55de9082012-07-26 17:24:39 +02002350 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2351 if (!chanctx_conf) {
2352 tmp_sdata = rcu_dereference(local->monitor_sdata);
2353 if (tmp_sdata)
2354 chanctx_conf =
2355 rcu_dereference(tmp_sdata->vif.chanctx_conf);
2356 }
Johannes Berg55de9082012-07-26 17:24:39 +02002357
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002358 if (chanctx_conf)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002359 chandef = &chanctx_conf->def;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002360 else if (!local->use_chanctx)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002361 chandef = &local->_oper_chandef;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002362 else
2363 goto fail_rcu;
Johannes Berg55de9082012-07-26 17:24:39 +02002364
2365 /*
2366 * Frame injection is not allowed if beaconing is not allowed
2367 * or if we need radar detection. Beaconing is usually not allowed when
2368 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2369 * Passive scan is also used in world regulatory domains where
2370 * your country is not known and as such it should be treated as
2371 * NO TX unless the channel is explicitly allowed in which case
2372 * your current regulatory domain would not have the passive scan
2373 * flag.
2374 *
2375 * Since AP mode uses monitor interfaces to inject/TX management
2376 * frames we can make AP mode the exception to this rule once it
2377 * supports radar detection as its implementation can deal with
2378 * radar detection by itself. We can do that later by adding a
2379 * monitor flag interfaces used for AP support.
2380 */
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002381 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2382 sdata->vif.type))
Johannes Berg55de9082012-07-26 17:24:39 +02002383 goto fail_rcu;
2384
Johannes Berg73c4e192014-11-09 18:50:09 +02002385 info->band = chandef->chan->band;
Lorenzo Bianconi109843b02016-02-19 12:18:01 +01002386
Johan Almbladh96a71092021-04-01 18:44:55 +02002387 /* Initialize skb->priority according to frame type and TID class,
2388 * with respect to the sub interface that the frame will actually
2389 * be transmitted on. If the DONT_REORDER flag is set, the original
2390 * skb-priority is preserved to assure frames injected with this
2391 * flag are not reordered relative to each other.
2392 */
2393 ieee80211_select_queue_80211(sdata, skb, hdr);
2394 skb_set_queue_mapping(skb, ieee80211_ac_from_tid(skb->priority));
2395
Mathy Vanhoefbddc0c42021-05-30 15:32:26 +02002396 /*
2397 * Process the radiotap header. This will now take into account the
2398 * selected chandef above to accurately set injection rates and
2399 * retransmissions.
2400 */
2401 if (!ieee80211_parse_tx_radiotap(skb, dev))
2402 goto fail_rcu;
2403
Mathy Vanhoefcb17ed22020-07-23 14:01:53 +04002404 /* remove the injection radiotap header */
2405 skb_pull(skb, len_rthdr);
Lorenzo Bianconi109843b02016-02-19 12:18:01 +01002406
Mathy Vanhoef08aca292020-07-23 14:01:52 +04002407 ieee80211_xmit(sdata, NULL, skb);
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002408 rcu_read_unlock();
2409
Johannes Berge2ebc742007-07-27 15:43:22 +02002410 return NETDEV_TX_OK;
Andy Green9b8a74e2007-07-27 15:43:24 +02002411
Johannes Berg55de9082012-07-26 17:24:39 +02002412fail_rcu:
2413 rcu_read_unlock();
Andy Green9b8a74e2007-07-27 15:43:24 +02002414fail:
2415 dev_kfree_skb(skb);
2416 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
Johannes Berge2ebc742007-07-27 15:43:22 +02002417}
2418
Johannes Berg97ffe752015-03-21 09:13:45 +01002419static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002420{
Johannes Berg97ffe752015-03-21 09:13:45 +01002421 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002422
Johannes Berg97ffe752015-03-21 09:13:45 +01002423 return ethertype == ETH_P_TDLS &&
2424 skb->len > 14 &&
2425 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2426}
2427
John Crispin50ff4772019-11-25 11:04:37 +01002428int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2429 struct sk_buff *skb,
2430 struct sta_info **sta_out)
Johannes Berg97ffe752015-03-21 09:13:45 +01002431{
2432 struct sta_info *sta;
2433
2434 switch (sdata->vif.type) {
2435 case NL80211_IFTYPE_AP_VLAN:
2436 sta = rcu_dereference(sdata->u.vlan.sta);
2437 if (sta) {
2438 *sta_out = sta;
2439 return 0;
2440 } else if (sdata->wdev.use_4addr) {
2441 return -ENOLINK;
2442 }
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05002443 fallthrough;
Johannes Berg97ffe752015-03-21 09:13:45 +01002444 case NL80211_IFTYPE_AP:
2445 case NL80211_IFTYPE_OCB:
2446 case NL80211_IFTYPE_ADHOC:
2447 if (is_multicast_ether_addr(skb->data)) {
2448 *sta_out = ERR_PTR(-ENOENT);
2449 return 0;
2450 }
2451 sta = sta_info_get_bss(sdata, skb->data);
2452 break;
Johannes Berg97ffe752015-03-21 09:13:45 +01002453#ifdef CONFIG_MAC80211_MESH
2454 case NL80211_IFTYPE_MESH_POINT:
2455 /* determined much later */
2456 *sta_out = NULL;
2457 return 0;
2458#endif
2459 case NL80211_IFTYPE_STATION:
2460 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2461 sta = sta_info_get(sdata, skb->data);
Johannes Berg11d62ca2016-09-13 08:28:22 +02002462 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2463 if (test_sta_flag(sta,
2464 WLAN_STA_TDLS_PEER_AUTH)) {
Johannes Berg97ffe752015-03-21 09:13:45 +01002465 *sta_out = sta;
2466 return 0;
2467 }
2468
2469 /*
2470 * TDLS link during setup - throw out frames to
2471 * peer. Allow TDLS-setup frames to unauthorized
2472 * peers for the special case of a link teardown
2473 * after a TDLS sta is removed due to being
2474 * unreachable.
2475 */
Johannes Berg11d62ca2016-09-13 08:28:22 +02002476 if (!ieee80211_is_tdls_setup(skb))
Johannes Berg97ffe752015-03-21 09:13:45 +01002477 return -EINVAL;
2478 }
2479
2480 }
2481
2482 sta = sta_info_get(sdata, sdata->u.mgd.bssid);
2483 if (!sta)
2484 return -ENOLINK;
2485 break;
2486 default:
2487 return -EINVAL;
2488 }
2489
2490 *sta_out = sta ?: ERR_PTR(-ENOENT);
2491 return 0;
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002492}
2493
Markus Theila7528192020-05-27 18:03:34 +02002494static u16 ieee80211_store_ack_skb(struct ieee80211_local *local,
John Crispin5d8983c2019-10-29 10:13:02 +01002495 struct sk_buff *skb,
Markus Theila7528192020-05-27 18:03:34 +02002496 u32 *info_flags,
2497 u64 *cookie)
John Crispin5d8983c2019-10-29 10:13:02 +01002498{
Markus Theila7528192020-05-27 18:03:34 +02002499 struct sk_buff *ack_skb;
John Crispin5d8983c2019-10-29 10:13:02 +01002500 u16 info_id = 0;
2501
Markus Theila7528192020-05-27 18:03:34 +02002502 if (skb->sk)
2503 ack_skb = skb_clone_sk(skb);
2504 else
2505 ack_skb = skb_clone(skb, GFP_ATOMIC);
2506
John Crispin5d8983c2019-10-29 10:13:02 +01002507 if (ack_skb) {
2508 unsigned long flags;
2509 int id;
2510
2511 spin_lock_irqsave(&local->ack_status_lock, flags);
2512 id = idr_alloc(&local->ack_status_frames, ack_skb,
Johannes Bergf2b18ba2020-01-15 12:25:50 +01002513 1, 0x2000, GFP_ATOMIC);
John Crispin5d8983c2019-10-29 10:13:02 +01002514 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2515
2516 if (id >= 0) {
2517 info_id = id;
2518 *info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
Markus Theila7528192020-05-27 18:03:34 +02002519 if (cookie) {
2520 *cookie = ieee80211_mgmt_tx_cookie(local);
2521 IEEE80211_SKB_CB(ack_skb)->ack.cookie = *cookie;
2522 }
John Crispin5d8983c2019-10-29 10:13:02 +01002523 } else {
2524 kfree_skb(ack_skb);
2525 }
2526 }
2527
2528 return info_id;
2529}
2530
Johannes Berge2ebc742007-07-27 15:43:22 +02002531/**
Johannes Berg4c9451e2014-11-09 18:50:10 +02002532 * ieee80211_build_hdr - build 802.11 header in the given frame
2533 * @sdata: virtual interface to build the header for
2534 * @skb: the skb to build the header in
Liad Kaufman24d342c2014-11-09 18:50:07 +02002535 * @info_flags: skb flags to set
Johannes Bergf0daf542020-09-24 19:25:11 +02002536 * @sta: the station pointer
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002537 * @ctrl_flags: info control flags to set
Johannes Bergf0daf542020-09-24 19:25:11 +02002538 * @cookie: cookie pointer to fill (if not %NULL)
Johannes Berge2ebc742007-07-27 15:43:22 +02002539 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002540 * This function takes the skb with 802.3 header and reformats the header to
2541 * the appropriate IEEE 802.11 header based on which interface the packet is
2542 * being transmitted on.
Johannes Berge2ebc742007-07-27 15:43:22 +02002543 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002544 * Note that this function also takes care of the TX status request and
2545 * potential unsharing of the SKB - this needs to be interleaved with the
2546 * header building.
2547 *
2548 * The function requires the read-side RCU lock held
2549 *
2550 * Returns: the (possibly reallocated) skb or an ERR_PTR() code
Johannes Berge2ebc742007-07-27 15:43:22 +02002551 */
Johannes Berg4c9451e2014-11-09 18:50:10 +02002552static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01002553 struct sk_buff *skb, u32 info_flags,
Markus Theila7528192020-05-27 18:03:34 +02002554 struct sta_info *sta, u32 ctrl_flags,
2555 u64 *cookie)
Johannes Berge2ebc742007-07-27 15:43:22 +02002556{
Johannes Berg133b8222008-09-16 14:18:59 +02002557 struct ieee80211_local *local = sdata->local;
Felix Fietkau489ee912010-12-18 19:30:48 +01002558 struct ieee80211_tx_info *info;
Johannes Bergdcf33962012-07-30 15:11:56 +02002559 int head_need;
Harvey Harrison065e96052008-06-22 16:45:27 -07002560 u16 ethertype, hdrlen, meshhdrlen = 0;
2561 __le16 fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002562 struct ieee80211_hdr hdr;
Wey-Yi Guyff67bb82010-08-21 07:23:29 -07002563 struct ieee80211s_hdr mesh_hdr __maybe_unused;
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002564 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02002565 const u8 *encaps_data;
2566 int encaps_len, skip_header_bytes;
Johannes Berg97ffe752015-03-21 09:13:45 +01002567 bool wme_sta = false, authorized = false;
2568 bool tdls_peer;
Johannes Berga729cff2011-11-06 14:13:34 +01002569 bool multicast;
Johannes Berga729cff2011-11-06 14:13:34 +01002570 u16 info_id = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002571 struct ieee80211_chanctx_conf *chanctx_conf;
2572 struct ieee80211_sub_if_data *ap_sdata;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002573 enum nl80211_band band;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002574 int ret;
Johannes Berge2ebc742007-07-27 15:43:22 +02002575
Johannes Berg97ffe752015-03-21 09:13:45 +01002576 if (IS_ERR(sta))
2577 sta = NULL;
2578
Julius Niedworok276d9e82019-03-28 21:01:06 +01002579#ifdef CONFIG_MAC80211_DEBUGFS
2580 if (local->force_tx_status)
2581 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2582#endif
2583
Johannes Berge2ebc742007-07-27 15:43:22 +02002584 /* convert Ethernet header to proper 802.11 header (based on
2585 * operation mode) */
2586 ethertype = (skb->data[12] << 8) | skb->data[13];
Harvey Harrison065e96052008-06-22 16:45:27 -07002587 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
Johannes Berge2ebc742007-07-27 15:43:22 +02002588
Johannes Berg51fb61e2007-12-19 01:31:27 +01002589 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002590 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg97ffe752015-03-21 09:13:45 +01002591 if (sdata->wdev.use_4addr) {
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002592 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2593 /* RA TA DA SA */
2594 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002595 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002596 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2597 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2598 hdrlen = 30;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002599 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Johannes Berga74a8c82014-07-22 14:50:47 +02002600 wme_sta = sta->sta.wme;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002601 }
Johannes Berg55de9082012-07-26 17:24:39 +02002602 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2603 u.ap);
2604 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002605 if (!chanctx_conf) {
2606 ret = -ENOTCONN;
2607 goto free;
2608 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002609 band = chanctx_conf->def.chan->band;
Johannes Berg97ffe752015-03-21 09:13:45 +01002610 if (sdata->wdev.use_4addr)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002611 break;
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05002612 fallthrough;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002613 case NL80211_IFTYPE_AP:
Arnd Bergmannfe801232013-01-25 14:14:33 +00002614 if (sdata->vif.type == NL80211_IFTYPE_AP)
2615 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002616 if (!chanctx_conf) {
2617 ret = -ENOTCONN;
2618 goto free;
2619 }
Harvey Harrison065e96052008-06-22 16:45:27 -07002620 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
Johannes Berge2ebc742007-07-27 15:43:22 +02002621 /* DA BSSID SA */
2622 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002623 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002624 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2625 hdrlen = 24;
Johannes Berg4bf88532012-11-09 11:39:59 +01002626 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002627 break;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002628#ifdef CONFIG_MAC80211_MESH
Johannes Berg05c914f2008-09-11 00:01:58 +02002629 case NL80211_IFTYPE_MESH_POINT:
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002630 if (!is_multicast_ether_addr(skb->data)) {
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002631 struct sta_info *next_hop;
2632 bool mpp_lookup = true;
2633
Johannes Bergbf7cd942013-02-15 14:40:31 +01002634 mpath = mesh_path_lookup(sdata, skb->data);
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002635 if (mpath) {
2636 mpp_lookup = false;
2637 next_hop = rcu_dereference(mpath->next_hop);
2638 if (!next_hop ||
2639 !(mpath->flags & (MESH_PATH_ACTIVE |
2640 MESH_PATH_RESOLVING)))
2641 mpp_lookup = true;
2642 }
2643
Henning Roggeab1c7902016-02-03 13:58:37 +01002644 if (mpp_lookup) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002645 mppath = mpp_path_lookup(sdata, skb->data);
Henning Roggeab1c7902016-02-03 13:58:37 +01002646 if (mppath)
2647 mppath->exp_time = jiffies;
2648 }
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002649
2650 if (mppath && mpath)
Bob Copeland2bdaf382016-02-28 20:03:56 -05002651 mesh_path_del(sdata, mpath->dst);
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002652 }
YanBo79617de2008-09-22 13:30:32 +08002653
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002654 /*
Joel A Fernandes9d525012011-01-10 00:44:23 -06002655 * Use address extension if it is a packet from
2656 * another interface or if we know the destination
2657 * is being proxied by a portal (i.e. portal address
2658 * differs from proxied address)
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002659 */
Joe Perchesb203ca32012-05-08 18:56:52 +00002660 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2661 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
Javier Cardona3c5772a2009-08-10 12:15:48 -07002662 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2663 skb->data, skb->data + ETH_ALEN);
Johannes Bergbf7cd942013-02-15 14:40:31 +01002664 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2665 NULL, NULL);
YanBo79617de2008-09-22 13:30:32 +08002666 } else {
Thomas Pedersen27f01122012-08-20 11:28:25 -07002667 /* DS -> MBSS (802.11-2012 13.11.3.3).
2668 * For unicast with unknown forwarding information,
2669 * destination might be in the MBSS or if that fails
2670 * forwarded to another mesh gate. In either case
2671 * resolution will be handled in ieee80211_xmit(), so
2672 * leave the original DA. This also works for mcast */
2673 const u8 *mesh_da = skb->data;
YanBo79617de2008-09-22 13:30:32 +08002674
Thomas Pedersen27f01122012-08-20 11:28:25 -07002675 if (mppath)
2676 mesh_da = mppath->mpp;
2677 else if (mpath)
2678 mesh_da = mpath->dst;
Thomas Pedersen27f01122012-08-20 11:28:25 -07002679
Javier Cardona3c5772a2009-08-10 12:15:48 -07002680 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
Johannes Berg47846c92009-11-25 17:46:19 +01002681 mesh_da, sdata->vif.addr);
Thomas Pedersen27f01122012-08-20 11:28:25 -07002682 if (is_multicast_ether_addr(mesh_da))
2683 /* DA TA mSA AE:SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002684 meshhdrlen = ieee80211_new_mesh_header(
2685 sdata, &mesh_hdr,
2686 skb->data + ETH_ALEN, NULL);
Javier Cardona3c5772a2009-08-10 12:15:48 -07002687 else
Thomas Pedersen27f01122012-08-20 11:28:25 -07002688 /* RA TA mDA mSA AE:DA SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002689 meshhdrlen = ieee80211_new_mesh_header(
2690 sdata, &mesh_hdr, skb->data,
2691 skb->data + ETH_ALEN);
YanBo79617de2008-09-22 13:30:32 +08002692
YanBo79617de2008-09-22 13:30:32 +08002693 }
Johannes Berg55de9082012-07-26 17:24:39 +02002694 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002695 if (!chanctx_conf) {
2696 ret = -ENOTCONN;
2697 goto free;
2698 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002699 band = chanctx_conf->def.chan->band;
Rajkumar Manoharan8828f812019-04-11 13:47:26 -07002700
2701 /* For injected frames, fill RA right away as nexthop lookup
2702 * will be skipped.
2703 */
2704 if ((ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP) &&
2705 is_zero_ether_addr(hdr.addr1))
2706 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002707 break;
2708#endif
Johannes Berg05c914f2008-09-11 00:01:58 +02002709 case NL80211_IFTYPE_STATION:
Johannes Berg97ffe752015-03-21 09:13:45 +01002710 /* we already did checks when looking up the RA STA */
2711 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002712
Johannes Berg97ffe752015-03-21 09:13:45 +01002713 if (tdls_peer) {
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002714 /* DA SA BSSID */
2715 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2716 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2717 memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2718 hdrlen = 24;
2719 } else if (sdata->u.mgd.use_4addr &&
2720 cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2721 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2722 IEEE80211_FCTL_TODS);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002723 /* RA TA DA SA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002724 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002725 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002726 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2727 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2728 hdrlen = 30;
2729 } else {
2730 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2731 /* BSSID SA DA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002732 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002733 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2734 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2735 hdrlen = 24;
2736 }
Johannes Berg55de9082012-07-26 17:24:39 +02002737 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002738 if (!chanctx_conf) {
2739 ret = -ENOTCONN;
2740 goto free;
2741 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002742 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002743 break;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002744 case NL80211_IFTYPE_OCB:
2745 /* DA SA BSSID */
2746 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2747 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2748 eth_broadcast_addr(hdr.addr3);
2749 hdrlen = 24;
2750 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002751 if (!chanctx_conf) {
2752 ret = -ENOTCONN;
2753 goto free;
2754 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002755 band = chanctx_conf->def.chan->band;
2756 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002757 case NL80211_IFTYPE_ADHOC:
Johannes Berge2ebc742007-07-27 15:43:22 +02002758 /* DA SA BSSID */
2759 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2760 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01002761 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002762 hdrlen = 24;
Johannes Berg55de9082012-07-26 17:24:39 +02002763 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002764 if (!chanctx_conf) {
2765 ret = -ENOTCONN;
2766 goto free;
2767 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002768 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002769 break;
2770 default:
Johannes Berg4c9451e2014-11-09 18:50:10 +02002771 ret = -EINVAL;
2772 goto free;
Johannes Berge2ebc742007-07-27 15:43:22 +02002773 }
2774
Johannes Berga729cff2011-11-06 14:13:34 +01002775 multicast = is_multicast_ether_addr(hdr.addr1);
Johannes Berge2ebc742007-07-27 15:43:22 +02002776
Johannes Berg97ffe752015-03-21 09:13:45 +01002777 /* sta is always NULL for mesh */
2778 if (sta) {
2779 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2780 wme_sta = sta->sta.wme;
2781 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2782 /* For mesh, the use of the QoS header is mandatory */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002783 wme_sta = true;
Johannes Berge2ebc742007-07-27 15:43:22 +02002784 }
2785
Johannes Berg527871d2015-03-21 08:09:55 +01002786 /* receiver does QoS (which also means we do) use it */
2787 if (wme_sta) {
Harvey Harrison065e96052008-06-22 16:45:27 -07002788 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002789 hdrlen += 2;
2790 }
2791
2792 /*
Johannes Berg238814f2008-01-28 17:19:37 +01002793 * Drop unicast frames to unauthorised stations unless they are
2794 * EAPOL frames from the local station.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002795 */
Johannes Berg55182e42011-10-12 17:28:21 +02002796 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002797 (sdata->vif.type != NL80211_IFTYPE_OCB) &&
Sergey Ryazanova6ececf2013-08-30 01:35:09 +04002798 !multicast && !authorized &&
Johannes Berg55182e42011-10-12 17:28:21 +02002799 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002800 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002801#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002802 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
Johannes Berg4c9451e2014-11-09 18:50:10 +02002803 sdata->name, hdr.addr1);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002804#endif
2805
2806 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2807
Johannes Berg4c9451e2014-11-09 18:50:10 +02002808 ret = -EPERM;
2809 goto free;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002810 }
2811
Markus Theila7528192020-05-27 18:03:34 +02002812 if (unlikely(!multicast && ((skb->sk &&
2813 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS) ||
2814 ctrl_flags & IEEE80211_TX_CTL_REQ_TX_STATUS)))
2815 info_id = ieee80211_store_ack_skb(local, skb, &info_flags,
2816 cookie);
Johannes Berga729cff2011-11-06 14:13:34 +01002817
Helmut Schaa7e244702010-12-02 18:44:09 +01002818 /*
2819 * If the skb is shared we need to obtain our own copy.
2820 */
2821 if (skb_shared(skb)) {
Johannes Berga729cff2011-11-06 14:13:34 +01002822 struct sk_buff *tmp_skb = skb;
2823
2824 /* can't happen -- skb is a clone if info_id != 0 */
2825 WARN_ON(info_id);
2826
Felix Fietkauf8a0a782010-12-18 19:30:50 +01002827 skb = skb_clone(skb, GFP_ATOMIC);
Helmut Schaa7e244702010-12-02 18:44:09 +01002828 kfree_skb(tmp_skb);
2829
Johannes Berg4c9451e2014-11-09 18:50:10 +02002830 if (!skb) {
2831 ret = -ENOMEM;
2832 goto free;
2833 }
Helmut Schaa7e244702010-12-02 18:44:09 +01002834 }
2835
Harvey Harrison065e96052008-06-22 16:45:27 -07002836 hdr.frame_control = fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002837 hdr.duration_id = 0;
2838 hdr.seq_ctrl = 0;
2839
2840 skip_header_bytes = ETH_HLEN;
2841 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2842 encaps_data = bridge_tunnel_header;
2843 encaps_len = sizeof(bridge_tunnel_header);
2844 skip_header_bytes -= 2;
Simon Hormane5c5d222013-03-28 13:38:25 +09002845 } else if (ethertype >= ETH_P_802_3_MIN) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002846 encaps_data = rfc1042_header;
2847 encaps_len = sizeof(rfc1042_header);
2848 skip_header_bytes -= 2;
2849 } else {
2850 encaps_data = NULL;
2851 encaps_len = 0;
2852 }
2853
2854 skb_pull(skb, skip_header_bytes);
Johannes Berg23c07522008-05-29 10:38:53 +02002855 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002856
Johannes Berg23c07522008-05-29 10:38:53 +02002857 /*
2858 * So we need to modify the skb header and hence need a copy of
2859 * that. The head_need variable above doesn't, so far, include
2860 * the needed header space that we don't need right away. If we
2861 * can, then we don't reallocate right now but only after the
2862 * frame arrives at the master device (if it does...)
2863 *
2864 * If we cannot, however, then we will reallocate to include all
2865 * the ever needed space. Also, if we need to reallocate it anyway,
2866 * make it big enough for everything we may ever need.
2867 */
Johannes Berge2ebc742007-07-27 15:43:22 +02002868
David S. Miller3a5be7d2008-06-18 01:19:51 -07002869 if (head_need > 0 || skb_cloned(skb)) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002870 head_need += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02002871 head_need += local->tx_headroom;
2872 head_need = max_t(int, 0, head_need);
Johannes Berg14f46c12020-10-09 13:25:41 +02002873 if (ieee80211_skb_resize(sdata, skb, head_need, ENCRYPT_DATA)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02002874 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg1c963be2012-11-07 14:02:30 +01002875 skb = NULL;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002876 return ERR_PTR(-ENOMEM);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002877 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002878 }
2879
Felix Fietkaueae44302016-07-13 11:00:02 +02002880 if (encaps_data)
Johannes Berge2ebc742007-07-27 15:43:22 +02002881 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002882
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002883#ifdef CONFIG_MAC80211_MESH
Felix Fietkaueae44302016-07-13 11:00:02 +02002884 if (meshhdrlen > 0)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002885 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002886#endif
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002887
Harvey Harrison065e96052008-06-22 16:45:27 -07002888 if (ieee80211_is_data_qos(fc)) {
Johannes Bergc29b9b92007-09-14 11:10:24 -04002889 __le16 *qos_control;
2890
Johannes Bergd58ff352017-06-16 14:29:23 +02002891 qos_control = skb_push(skb, 2);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002892 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2893 /*
2894 * Maybe we could actually set some fields here, for now just
2895 * initialise to zero to indicate no special operation.
2896 */
2897 *qos_control = 0;
2898 } else
2899 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2900
Zhang Shengjud57a5442016-03-03 01:16:56 +00002901 skb_reset_mac_header(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002902
Felix Fietkau489ee912010-12-18 19:30:48 +01002903 info = IEEE80211_SKB_CB(skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002904 memset(info, 0, sizeof(*info));
2905
Johannes Berga729cff2011-11-06 14:13:34 +01002906 info->flags = info_flags;
2907 info->ack_frame_id = info_id;
Johannes Berg73c4e192014-11-09 18:50:09 +02002908 info->band = band;
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002909 info->control.flags = ctrl_flags;
Johannes Berga729cff2011-11-06 14:13:34 +01002910
Johannes Berg4c9451e2014-11-09 18:50:10 +02002911 return skb;
2912 free:
2913 kfree_skb(skb);
2914 return ERR_PTR(ret);
Johannes Berge2ebc742007-07-27 15:43:22 +02002915}
2916
Johannes Berg17c18bf2015-03-21 15:25:43 +01002917/*
2918 * fast-xmit overview
2919 *
2920 * The core idea of this fast-xmit is to remove per-packet checks by checking
2921 * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2922 * checks that are needed to get the sta->fast_tx pointer assigned, after which
2923 * much less work can be done per packet. For example, fragmentation must be
2924 * disabled or the fast_tx pointer will not be set. All the conditions are seen
2925 * in the code here.
2926 *
2927 * Once assigned, the fast_tx data structure also caches the per-packet 802.11
2928 * header and other data to aid packet processing in ieee80211_xmit_fast().
2929 *
2930 * The most difficult part of this is that when any of these assumptions
2931 * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
2932 * ieee80211_check_fast_xmit() or friends) is required to reset the data,
2933 * since the per-packet code no longer checks the conditions. This is reflected
2934 * by the calls to these functions throughout the rest of the code, and must be
2935 * maintained if any of the TX path checks change.
2936 */
2937
2938void ieee80211_check_fast_xmit(struct sta_info *sta)
2939{
2940 struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
2941 struct ieee80211_local *local = sta->local;
2942 struct ieee80211_sub_if_data *sdata = sta->sdata;
2943 struct ieee80211_hdr *hdr = (void *)build.hdr;
2944 struct ieee80211_chanctx_conf *chanctx_conf;
2945 __le16 fc;
2946
Johannes Berg30686bf2015-06-02 21:39:54 +02002947 if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002948 return;
2949
2950 /* Locking here protects both the pointer itself, and against concurrent
2951 * invocations winning data access races to, e.g., the key pointer that
2952 * is used.
2953 * Without it, the invocation of this function right after the key
2954 * pointer changes wouldn't be sufficient, as another CPU could access
2955 * the pointer, then stall, and then do the cache update after the CPU
2956 * that invalidated the key.
2957 * With the locking, such scenarios cannot happen as the check for the
2958 * key and the fast-tx assignment are done atomically, so the CPU that
2959 * modifies the key will either wait or other one will see the key
2960 * cleared/changed already.
2961 */
2962 spin_lock_bh(&sta->lock);
Johannes Berg30686bf2015-06-02 21:39:54 +02002963 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
2964 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
Johannes Berg17c18bf2015-03-21 15:25:43 +01002965 sdata->vif.type == NL80211_IFTYPE_STATION)
2966 goto out;
2967
2968 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2969 goto out;
2970
2971 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
2972 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
Felix Fietkauf7418bc2015-09-24 14:59:49 +02002973 test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
2974 test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002975 goto out;
2976
2977 if (sdata->noack_map)
2978 goto out;
2979
2980 /* fast-xmit doesn't handle fragmentation at all */
Johannes Berg725b8122015-04-10 14:02:08 +02002981 if (local->hw.wiphy->frag_threshold != (u32)-1 &&
Sara Sharonf3fe4e92016-10-18 23:12:11 +03002982 !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002983 goto out;
2984
2985 rcu_read_lock();
2986 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2987 if (!chanctx_conf) {
2988 rcu_read_unlock();
2989 goto out;
2990 }
2991 build.band = chanctx_conf->def.chan->band;
2992 rcu_read_unlock();
2993
2994 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2995
2996 switch (sdata->vif.type) {
Johannes Berg3ffd8842015-04-14 10:28:37 +02002997 case NL80211_IFTYPE_ADHOC:
2998 /* DA SA BSSID */
2999 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
3000 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3001 memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
3002 build.hdr_len = 24;
3003 break;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003004 case NL80211_IFTYPE_STATION:
3005 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
3006 /* DA SA BSSID */
3007 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
3008 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3009 memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
3010 build.hdr_len = 24;
3011 break;
3012 }
3013
3014 if (sdata->u.mgd.use_4addr) {
3015 /* non-regular ethertype cannot use the fastpath */
3016 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
3017 IEEE80211_FCTL_TODS);
3018 /* RA TA DA SA */
3019 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
3020 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
3021 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
3022 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
3023 build.hdr_len = 30;
3024 break;
3025 }
3026 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
3027 /* BSSID SA DA */
3028 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
3029 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
3030 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3031 build.hdr_len = 24;
3032 break;
3033 case NL80211_IFTYPE_AP_VLAN:
3034 if (sdata->wdev.use_4addr) {
3035 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
3036 IEEE80211_FCTL_TODS);
3037 /* RA TA DA SA */
3038 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
3039 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
3040 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
3041 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
3042 build.hdr_len = 30;
3043 break;
3044 }
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05003045 fallthrough;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003046 case NL80211_IFTYPE_AP:
3047 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
3048 /* DA BSSID SA */
3049 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
3050 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
3051 build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
3052 build.hdr_len = 24;
3053 break;
3054 default:
3055 /* not handled on fast-xmit */
3056 goto out;
3057 }
3058
3059 if (sta->sta.wme) {
3060 build.hdr_len += 2;
3061 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3062 }
3063
3064 /* We store the key here so there's no point in using rcu_dereference()
3065 * but that's fine because the code that changes the pointers will call
3066 * this function after doing so. For a single CPU that would be enough,
3067 * for multiple see the comment above.
3068 */
3069 build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
3070 if (!build.key)
3071 build.key = rcu_access_pointer(sdata->default_unicast_key);
3072 if (build.key) {
Johannes Berge495c242015-04-10 14:03:17 +02003073 bool gen_iv, iv_spc, mmic;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003074
3075 gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
3076 iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
David Spinadel9de18d82017-12-01 13:50:52 +02003077 mmic = build.key->conf.flags &
3078 (IEEE80211_KEY_FLAG_GENERATE_MMIC |
3079 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003080
3081 /* don't handle software crypto */
3082 if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
3083 goto out;
3084
Alexander Wetzel62872a92018-08-31 15:00:38 +02003085 /* Key is being removed */
3086 if (build.key->flags & KEY_FLAG_TAINTED)
3087 goto out;
3088
Johannes Berg17c18bf2015-03-21 15:25:43 +01003089 switch (build.key->conf.cipher) {
3090 case WLAN_CIPHER_SUITE_CCMP:
3091 case WLAN_CIPHER_SUITE_CCMP_256:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003092 if (gen_iv)
Johannes Berg17c18bf2015-03-21 15:25:43 +01003093 build.pn_offs = build.hdr_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003094 if (gen_iv || iv_spc)
3095 build.hdr_len += IEEE80211_CCMP_HDR_LEN;
3096 break;
3097 case WLAN_CIPHER_SUITE_GCMP:
3098 case WLAN_CIPHER_SUITE_GCMP_256:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003099 if (gen_iv)
Johannes Berg17c18bf2015-03-21 15:25:43 +01003100 build.pn_offs = build.hdr_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003101 if (gen_iv || iv_spc)
3102 build.hdr_len += IEEE80211_GCMP_HDR_LEN;
3103 break;
Johannes Berge495c242015-04-10 14:03:17 +02003104 case WLAN_CIPHER_SUITE_TKIP:
3105 /* cannot handle MMIC or IV generation in xmit-fast */
3106 if (mmic || gen_iv)
3107 goto out;
3108 if (iv_spc)
3109 build.hdr_len += IEEE80211_TKIP_IV_LEN;
3110 break;
3111 case WLAN_CIPHER_SUITE_WEP40:
3112 case WLAN_CIPHER_SUITE_WEP104:
3113 /* cannot handle IV generation in fast-xmit */
3114 if (gen_iv)
3115 goto out;
3116 if (iv_spc)
3117 build.hdr_len += IEEE80211_WEP_IV_LEN;
3118 break;
3119 case WLAN_CIPHER_SUITE_AES_CMAC:
3120 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3121 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3122 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3123 WARN(1,
3124 "management cipher suite 0x%x enabled for data\n",
3125 build.key->conf.cipher);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003126 goto out;
Johannes Berge495c242015-04-10 14:03:17 +02003127 default:
3128 /* we don't know how to generate IVs for this at all */
3129 if (WARN_ON(gen_iv))
3130 goto out;
3131 /* pure hardware keys are OK, of course */
3132 if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
3133 break;
3134 /* cipher scheme might require space allocation */
3135 if (iv_spc &&
3136 build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
3137 goto out;
3138 if (iv_spc)
3139 build.hdr_len += build.key->conf.iv_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003140 }
3141
3142 fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3143 }
3144
3145 hdr->frame_control = fc;
3146
3147 memcpy(build.hdr + build.hdr_len,
3148 rfc1042_header, sizeof(rfc1042_header));
3149 build.hdr_len += sizeof(rfc1042_header);
3150
3151 fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3152 /* if the kmemdup fails, continue w/o fast_tx */
3153 if (!fast_tx)
3154 goto out;
3155
3156 out:
3157 /* we might have raced against another call to this function */
3158 old = rcu_dereference_protected(sta->fast_tx,
3159 lockdep_is_held(&sta->lock));
3160 rcu_assign_pointer(sta->fast_tx, fast_tx);
3161 if (old)
3162 kfree_rcu(old, rcu_head);
3163 spin_unlock_bh(&sta->lock);
3164}
3165
3166void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3167{
3168 struct sta_info *sta;
3169
3170 rcu_read_lock();
3171 list_for_each_entry_rcu(sta, &local->sta_list, list)
3172 ieee80211_check_fast_xmit(sta);
3173 rcu_read_unlock();
3174}
3175
3176void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3177{
3178 struct ieee80211_local *local = sdata->local;
3179 struct sta_info *sta;
3180
3181 rcu_read_lock();
3182
3183 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3184 if (sdata != sta->sdata &&
3185 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3186 continue;
3187 ieee80211_check_fast_xmit(sta);
3188 }
3189
3190 rcu_read_unlock();
3191}
3192
3193void ieee80211_clear_fast_xmit(struct sta_info *sta)
3194{
3195 struct ieee80211_fast_tx *fast_tx;
3196
3197 spin_lock_bh(&sta->lock);
3198 fast_tx = rcu_dereference_protected(sta->fast_tx,
3199 lockdep_is_held(&sta->lock));
3200 RCU_INIT_POINTER(sta->fast_tx, NULL);
3201 spin_unlock_bh(&sta->lock);
3202
3203 if (fast_tx)
3204 kfree_rcu(fast_tx, rcu_head);
3205}
3206
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003207static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
Sara Sharon166ac9d2018-08-29 08:57:02 +02003208 struct sk_buff *skb, int headroom)
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003209{
Sara Sharon166ac9d2018-08-29 08:57:02 +02003210 if (skb_headroom(skb) < headroom) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003211 I802_DEBUG_INC(local->tx_expand_skb_head);
3212
Sara Sharon166ac9d2018-08-29 08:57:02 +02003213 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003214 wiphy_debug(local->hw.wiphy,
3215 "failed to reallocate TX buffer\n");
3216 return false;
3217 }
3218 }
3219
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003220 return true;
3221}
3222
3223static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3224 struct ieee80211_fast_tx *fast_tx,
3225 struct sk_buff *skb)
3226{
3227 struct ieee80211_local *local = sdata->local;
3228 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3229 struct ieee80211_hdr *hdr;
Michael Braun06f2bb12016-10-15 13:28:18 +02003230 struct ethhdr *amsdu_hdr;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003231 int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3232 int subframe_len = skb->len - hdr_len;
3233 void *data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003234 u8 *qc, *h_80211_src, *h_80211_dst;
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003235 const u8 *bssid;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003236
3237 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3238 return false;
3239
3240 if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3241 return true;
3242
Chih-Kang Changf50d2ff2021-08-16 16:51:28 +08003243 if (!ieee80211_amsdu_realloc_pad(local, skb,
3244 sizeof(*amsdu_hdr) +
3245 local->hw.extra_tx_headroom))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003246 return false;
3247
Michael Braun06f2bb12016-10-15 13:28:18 +02003248 data = skb_push(skb, sizeof(*amsdu_hdr));
3249 memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003250 hdr = data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003251 amsdu_hdr = data + hdr_len;
3252 /* h_80211_src/dst is addr* field within hdr */
3253 h_80211_src = data + fast_tx->sa_offs;
3254 h_80211_dst = data + fast_tx->da_offs;
3255
3256 amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3257 ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3258 ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
3259
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003260 /* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3261 * fields needs to be changed to BSSID for A-MSDU frames depending
3262 * on FromDS/ToDS values.
3263 */
3264 switch (sdata->vif.type) {
3265 case NL80211_IFTYPE_STATION:
3266 bssid = sdata->u.mgd.bssid;
3267 break;
3268 case NL80211_IFTYPE_AP:
3269 case NL80211_IFTYPE_AP_VLAN:
3270 bssid = sdata->vif.addr;
3271 break;
3272 default:
3273 bssid = NULL;
3274 }
3275
3276 if (bssid && ieee80211_has_fromds(hdr->frame_control))
3277 ether_addr_copy(h_80211_src, bssid);
3278
3279 if (bssid && ieee80211_has_tods(hdr->frame_control))
3280 ether_addr_copy(h_80211_dst, bssid);
3281
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003282 qc = ieee80211_get_qos_ctl(hdr);
3283 *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3284
3285 info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3286
3287 return true;
3288}
3289
3290static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3291 struct sta_info *sta,
3292 struct ieee80211_fast_tx *fast_tx,
3293 struct sk_buff *skb)
3294{
3295 struct ieee80211_local *local = sdata->local;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003296 struct fq *fq = &local->fq;
3297 struct fq_tin *tin;
3298 struct fq_flow *flow;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003299 u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3300 struct ieee80211_txq *txq = sta->sta.txq[tid];
3301 struct txq_info *txqi;
3302 struct sk_buff **frag_tail, *head;
3303 int subframe_len = skb->len - ETH_ALEN;
3304 u8 max_subframes = sta->sta.max_amsdu_subframes;
3305 int max_frags = local->hw.max_tx_fragments;
3306 int max_amsdu_len = sta->sta.max_amsdu_len;
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003307 int orig_truesize;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003308 u32 flow_idx;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003309 __be16 len;
3310 void *data;
3311 bool ret = false;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003312 unsigned int orig_len;
Lorenzo Bianconi66eb02d2018-08-31 01:04:13 +02003313 int n = 2, nfrags, pad = 0;
Sara Sharon166ac9d2018-08-29 08:57:02 +02003314 u16 hdrlen;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003315
3316 if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
3317 return false;
3318
Ryder Lee4f2e3eb2021-06-18 04:38:59 +08003319 if (sdata->vif.offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
3320 return false;
3321
Sara Sharon344f8e02018-12-15 11:03:07 +02003322 if (skb_is_gso(skb))
3323 return false;
3324
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003325 if (!txq)
3326 return false;
3327
3328 txqi = to_txq_info(txq);
3329 if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3330 return false;
3331
3332 if (sta->sta.max_rc_amsdu_len)
3333 max_amsdu_len = min_t(int, max_amsdu_len,
3334 sta->sta.max_rc_amsdu_len);
3335
Sara Sharonedba6bd2018-09-05 08:06:10 +03003336 if (sta->sta.max_tid_amsdu_len[tid])
3337 max_amsdu_len = min_t(int, max_amsdu_len,
3338 sta->sta.max_tid_amsdu_len[tid]);
3339
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003340 flow_idx = fq_flow_idx(fq, skb);
3341
Michal Kaziorfa962b92016-05-19 10:37:49 +02003342 spin_lock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003343
Michal Kaziorfa962b92016-05-19 10:37:49 +02003344 /* TODO: Ideally aggregation should be done on dequeue to remain
3345 * responsive to environment changes.
3346 */
3347
3348 tin = &txqi->tin;
Felix Fietkaubf9009b2020-12-18 19:47:14 +01003349 flow = fq_flow_classify(fq, tin, flow_idx, skb);
Michal Kaziorfa962b92016-05-19 10:37:49 +02003350 head = skb_peek_tail(&flow->queue);
Sara Sharon344f8e02018-12-15 11:03:07 +02003351 if (!head || skb_is_gso(head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003352 goto out;
3353
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003354 orig_truesize = head->truesize;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003355 orig_len = head->len;
3356
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003357 if (skb->len + head->len > max_amsdu_len)
3358 goto out;
3359
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003360 nfrags = 1 + skb_shinfo(skb)->nr_frags;
3361 nfrags += 1 + skb_shinfo(head)->nr_frags;
3362 frag_tail = &skb_shinfo(head)->frag_list;
3363 while (*frag_tail) {
3364 nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3365 frag_tail = &(*frag_tail)->next;
3366 n++;
3367 }
3368
3369 if (max_subframes && n > max_subframes)
3370 goto out;
3371
3372 if (max_frags && nfrags > max_frags)
3373 goto out;
3374
Sara Sharon9739fe22018-09-05 08:06:11 +03003375 if (!drv_can_aggregate_in_amsdu(local, head, skb))
3376 goto out;
3377
Lorenzo Bianconi1eb50792018-08-29 21:03:25 +02003378 if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003379 goto out;
3380
Chih-Kang Changfe94bac2021-08-30 15:32:40 +08003381 /* If n == 2, the "while (*frag_tail)" loop above didn't execute
3382 * and frag_tail should be &skb_shinfo(head)->frag_list.
3383 * However, ieee80211_amsdu_prepare_head() can reallocate it.
3384 * Reload frag_tail to have it pointing to the correct place.
3385 */
3386 if (n == 2)
3387 frag_tail = &skb_shinfo(head)->frag_list;
3388
Sara Sharon166ac9d2018-08-29 08:57:02 +02003389 /*
3390 * Pad out the previous subframe to a multiple of 4 by adding the
3391 * padding to the next one, that's being added. Note that head->len
3392 * is the length of the full A-MSDU, but that works since each time
3393 * we add a new subframe we pad out the previous one to a multiple
3394 * of 4 and thus it no longer matters in the next round.
3395 */
3396 hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3397 if ((head->len - hdrlen) & 3)
3398 pad = 4 - ((head->len - hdrlen) & 3);
3399
3400 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3401 2 + pad))
Johannes Bergaa58acf2018-08-30 10:55:49 +02003402 goto out_recalc;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003403
3404 ret = true;
3405 data = skb_push(skb, ETH_ALEN + 2);
3406 memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3407
3408 data += 2 * ETH_ALEN;
3409 len = cpu_to_be16(subframe_len);
3410 memcpy(data, &len, 2);
3411 memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3412
Sara Sharon166ac9d2018-08-29 08:57:02 +02003413 memset(skb_push(skb, pad), 0, pad);
3414
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003415 head->len += skb->len;
3416 head->data_len += skb->len;
3417 *frag_tail = skb;
3418
Johannes Bergaa58acf2018-08-30 10:55:49 +02003419out_recalc:
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003420 fq->memory_usage += head->truesize - orig_truesize;
Johannes Bergaa58acf2018-08-30 10:55:49 +02003421 if (head->len != orig_len) {
3422 flow->backlog += head->len - orig_len;
3423 tin->backlog_bytes += head->len - orig_len;
Johannes Bergaa58acf2018-08-30 10:55:49 +02003424 }
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003425out:
Michal Kaziorfa962b92016-05-19 10:37:49 +02003426 spin_unlock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003427
3428 return ret;
3429}
3430
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003431/*
3432 * Can be called while the sta lock is held. Anything that can cause packets to
3433 * be generated will cause deadlock!
3434 */
Ryder Lee03c39112021-06-17 18:31:12 +02003435static ieee80211_tx_result
3436ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3437 struct sta_info *sta, u8 pn_offs,
3438 struct ieee80211_key *key,
3439 struct ieee80211_tx_data *tx)
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003440{
Ryder Lee03c39112021-06-17 18:31:12 +02003441 struct sk_buff *skb = tx->skb;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003442 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3443 struct ieee80211_hdr *hdr = (void *)skb->data;
3444 u8 tid = IEEE80211_NUM_TIDS;
3445
Ryder Lee03c39112021-06-17 18:31:12 +02003446 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL) &&
3447 ieee80211_tx_h_rate_ctrl(tx) != TX_CONTINUE)
3448 return TX_DROP;
3449
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003450 if (key)
3451 info->control.hw_key = &key->conf;
3452
Lev Stipakov36ec1442020-11-13 23:46:24 +02003453 dev_sw_netstats_tx_add(skb->dev, 1, skb->len);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003454
3455 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3456 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003457 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3458 } else {
3459 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3460 hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3461 sdata->sequence_number += 0x10;
3462 }
3463
3464 if (skb_shinfo(skb)->gso_size)
3465 sta->tx_stats.msdu[tid] +=
3466 DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3467 else
3468 sta->tx_stats.msdu[tid]++;
3469
3470 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3471
3472 /* statistics normally done by ieee80211_tx_h_stats (but that
3473 * has to consider fragmentation, so is more complex)
3474 */
3475 sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3476 sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
3477
3478 if (pn_offs) {
3479 u64 pn;
3480 u8 *crypto_hdr = skb->data + pn_offs;
3481
3482 switch (key->conf.cipher) {
3483 case WLAN_CIPHER_SUITE_CCMP:
3484 case WLAN_CIPHER_SUITE_CCMP_256:
3485 case WLAN_CIPHER_SUITE_GCMP:
3486 case WLAN_CIPHER_SUITE_GCMP_256:
3487 pn = atomic64_inc_return(&key->conf.tx_pn);
3488 crypto_hdr[0] = pn;
3489 crypto_hdr[1] = pn >> 8;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003490 crypto_hdr[3] = 0x20 | (key->conf.keyidx << 6);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003491 crypto_hdr[4] = pn >> 16;
3492 crypto_hdr[5] = pn >> 24;
3493 crypto_hdr[6] = pn >> 32;
3494 crypto_hdr[7] = pn >> 40;
3495 break;
3496 }
3497 }
Ryder Lee03c39112021-06-17 18:31:12 +02003498
3499 return TX_CONTINUE;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003500}
3501
Johannes Berg17c18bf2015-03-21 15:25:43 +01003502static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003503 struct sta_info *sta,
Johannes Berg17c18bf2015-03-21 15:25:43 +01003504 struct ieee80211_fast_tx *fast_tx,
3505 struct sk_buff *skb)
3506{
3507 struct ieee80211_local *local = sdata->local;
3508 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3509 int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3510 int hw_headroom = sdata->local->hw.extra_tx_headroom;
3511 struct ethhdr eth;
Johannes Berg35f432a2017-01-02 11:19:29 +01003512 struct ieee80211_tx_info *info;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003513 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3514 struct ieee80211_tx_data tx;
3515 ieee80211_tx_result r;
3516 struct tid_ampdu_tx *tid_tx = NULL;
3517 u8 tid = IEEE80211_NUM_TIDS;
3518
3519 /* control port protocol needs a lot of special handling */
3520 if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3521 return false;
3522
3523 /* only RFC 1042 SNAP */
3524 if (ethertype < ETH_P_802_3_MIN)
3525 return false;
3526
3527 /* don't handle TX status request here either */
3528 if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3529 return false;
3530
3531 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3532 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3533 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
Johannes Berg472be002015-06-09 16:45:08 +02003534 if (tid_tx) {
3535 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3536 return false;
3537 if (tid_tx->timeout)
3538 tid_tx->last_tx = jiffies;
3539 }
Johannes Berg17c18bf2015-03-21 15:25:43 +01003540 }
3541
3542 /* after this point (skb is modified) we cannot return false */
3543
3544 if (skb_shared(skb)) {
3545 struct sk_buff *tmp_skb = skb;
3546
3547 skb = skb_clone(skb, GFP_ATOMIC);
3548 kfree_skb(tmp_skb);
3549
3550 if (!skb)
3551 return true;
3552 }
3553
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003554 if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3555 ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
3556 return true;
3557
Johannes Berg17c18bf2015-03-21 15:25:43 +01003558 /* will not be crypto-handled beyond what we do here, so use false
3559 * as the may-encrypt argument for the resize to not account for
3560 * more room than we already have in 'extra_head'
3561 */
3562 if (unlikely(ieee80211_skb_resize(sdata, skb,
3563 max_t(int, extra_head + hw_headroom -
3564 skb_headroom(skb), 0),
Johannes Berg14f46c12020-10-09 13:25:41 +02003565 ENCRYPT_NO))) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01003566 kfree_skb(skb);
3567 return true;
3568 }
3569
3570 memcpy(&eth, skb->data, ETH_HLEN - 2);
Johannes Bergd58ff352017-06-16 14:29:23 +02003571 hdr = skb_push(skb, extra_head);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003572 memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3573 memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
3574 memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
3575
Johannes Berg35f432a2017-01-02 11:19:29 +01003576 info = IEEE80211_SKB_CB(skb);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003577 memset(info, 0, sizeof(*info));
3578 info->band = fast_tx->band;
3579 info->control.vif = &sdata->vif;
3580 info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3581 IEEE80211_TX_CTL_DONTFRAG |
3582 (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003583 info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003584
Julius Niedworok276d9e82019-03-28 21:01:06 +01003585#ifdef CONFIG_MAC80211_DEBUGFS
3586 if (local->force_tx_status)
3587 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3588#endif
3589
Felix Fietkaua786f962016-11-04 10:27:54 +01003590 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3591 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3592 *ieee80211_get_qos_ctl(hdr) = tid;
3593 }
3594
Johannes Berg17c18bf2015-03-21 15:25:43 +01003595 __skb_queue_head_init(&tx.skbs);
3596
3597 tx.flags = IEEE80211_TX_UNICAST;
3598 tx.local = local;
3599 tx.sdata = sdata;
3600 tx.sta = sta;
3601 tx.key = fast_tx->key;
3602
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003603 if (ieee80211_queue_skb(local, sdata, sta, skb))
3604 return true;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003605
Ryder Lee03c39112021-06-17 18:31:12 +02003606 tx.skb = skb;
3607 r = ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3608 fast_tx->key, &tx);
3609 tx.skb = NULL;
3610 if (r == TX_DROP) {
3611 kfree_skb(skb);
3612 return true;
3613 }
Johannes Berg17c18bf2015-03-21 15:25:43 +01003614
3615 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3616 sdata = container_of(sdata->bss,
3617 struct ieee80211_sub_if_data, u.ap);
3618
3619 __skb_queue_tail(&tx.skbs, skb);
Johannes Berg4fd03282019-10-01 23:26:35 +02003620 ieee80211_tx_frags(local, &sdata->vif, sta, &tx.skbs, false);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003621 return true;
3622}
3623
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003624struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3625 struct ieee80211_txq *txq)
3626{
3627 struct ieee80211_local *local = hw_to_local(hw);
3628 struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3629 struct ieee80211_hdr *hdr;
3630 struct sk_buff *skb = NULL;
3631 struct fq *fq = &local->fq;
3632 struct fq_tin *tin = &txqi->tin;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003633 struct ieee80211_tx_info *info;
3634 struct ieee80211_tx_data tx;
3635 ieee80211_tx_result r;
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303636 struct ieee80211_vif *vif = txq->vif;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003637
Erik Stromdahlfb0e76a2019-06-17 22:01:39 +02003638 WARN_ON_ONCE(softirq_count() == 0);
3639
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003640 if (!ieee80211_txq_airtime_check(hw, txq))
3641 return NULL;
3642
Felix Fietkauded46982019-03-16 18:06:33 +01003643begin:
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003644 spin_lock_bh(&fq->lock);
3645
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303646 if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags) ||
3647 test_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags))
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003648 goto out;
3649
Johannes Berg1153a742021-03-23 21:05:01 +01003650 if (vif->txqs_stopped[txq->ac]) {
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303651 set_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags);
3652 goto out;
3653 }
3654
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003655 /* Make sure fragments stay together. */
3656 skb = __skb_dequeue(&txqi->frags);
Johannes Bergca47b462021-04-16 13:47:04 +02003657 if (unlikely(skb)) {
3658 if (!(IEEE80211_SKB_CB(skb)->control.flags &
3659 IEEE80211_TX_INTCFL_NEED_TXPROCESSING))
3660 goto out;
3661 IEEE80211_SKB_CB(skb)->control.flags &=
3662 ~IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
3663 } else {
3664 skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
3665 }
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003666
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003667 if (!skb)
3668 goto out;
3669
Felix Fietkauded46982019-03-16 18:06:33 +01003670 spin_unlock_bh(&fq->lock);
3671
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003672 hdr = (struct ieee80211_hdr *)skb->data;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003673 info = IEEE80211_SKB_CB(skb);
3674
3675 memset(&tx, 0, sizeof(tx));
3676 __skb_queue_head_init(&tx.skbs);
3677 tx.local = local;
3678 tx.skb = skb;
3679 tx.sdata = vif_to_sdata(info->control.vif);
3680
Mathy Vanhoef804fc6a2020-10-19 20:01:13 +04003681 if (txq->sta) {
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003682 tx.sta = container_of(txq->sta, struct sta_info, sta);
Jouni Malinence2e1ca2020-03-26 15:51:34 +01003683 /*
3684 * Drop unicast frames to unauthorised stations unless they are
Mathy Vanhoef804fc6a2020-10-19 20:01:13 +04003685 * injected frames or EAPOL frames from the local station.
Jouni Malinence2e1ca2020-03-26 15:51:34 +01003686 */
Mathy Vanhoef804fc6a2020-10-19 20:01:13 +04003687 if (unlikely(!(info->flags & IEEE80211_TX_CTL_INJECTED) &&
3688 ieee80211_is_data(hdr->frame_control) &&
Johannes Bergbe8c8272020-03-29 22:50:06 +02003689 !ieee80211_vif_is_mesh(&tx.sdata->vif) &&
Jouni Malinence2e1ca2020-03-26 15:51:34 +01003690 tx.sdata->vif.type != NL80211_IFTYPE_OCB &&
3691 !is_multicast_ether_addr(hdr->addr1) &&
3692 !test_sta_flag(tx.sta, WLAN_STA_AUTHORIZED) &&
3693 (!(info->control.flags &
3694 IEEE80211_TX_CTRL_PORT_CTRL_PROTO) ||
3695 !ether_addr_equal(tx.sdata->vif.addr,
3696 hdr->addr2)))) {
3697 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
3698 ieee80211_free_txskb(&local->hw, skb);
3699 goto begin;
3700 }
3701 }
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003702
3703 /*
3704 * The key can be removed while the packet was queued, so need to call
3705 * this here to get the current key.
3706 */
3707 r = ieee80211_tx_h_select_key(&tx);
3708 if (r != TX_CONTINUE) {
3709 ieee80211_free_txskb(&local->hw, skb);
3710 goto begin;
3711 }
3712
Felix Fietkauc1f4c9e2016-11-04 10:27:52 +01003713 if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
3714 info->flags |= IEEE80211_TX_CTL_AMPDU;
3715 else
3716 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
3717
Ryder Lee3187ba02021-06-17 18:31:13 +02003718 if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
3719 if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
3720 r = ieee80211_tx_h_rate_ctrl(&tx);
3721 if (r != TX_CONTINUE) {
3722 ieee80211_free_txskb(&local->hw, skb);
3723 goto begin;
3724 }
3725 }
John Crispin50ff4772019-11-25 11:04:37 +01003726 goto encap_out;
Ryder Lee3187ba02021-06-17 18:31:13 +02003727 }
John Crispin50ff4772019-11-25 11:04:37 +01003728
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003729 if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003730 struct sta_info *sta = container_of(txq->sta, struct sta_info,
3731 sta);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003732 u8 pn_offs = 0;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003733
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003734 if (tx.key &&
3735 (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
3736 pn_offs = ieee80211_hdrlen(hdr->frame_control);
3737
Ryder Lee03c39112021-06-17 18:31:12 +02003738 r = ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
3739 tx.key, &tx);
3740 if (r != TX_CONTINUE) {
3741 ieee80211_free_txskb(&local->hw, skb);
3742 goto begin;
3743 }
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003744 } else {
3745 if (invoke_tx_handlers_late(&tx))
3746 goto begin;
3747
3748 skb = __skb_dequeue(&tx.skbs);
3749
Felix Fietkauded46982019-03-16 18:06:33 +01003750 if (!skb_queue_empty(&tx.skbs)) {
3751 spin_lock_bh(&fq->lock);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003752 skb_queue_splice_tail(&tx.skbs, &txqi->frags);
Felix Fietkauded46982019-03-16 18:06:33 +01003753 spin_unlock_bh(&fq->lock);
3754 }
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003755 }
3756
Johannes Berg233e98d2018-12-15 11:03:09 +02003757 if (skb_has_frag_list(skb) &&
Johannes Berg1e1430d2016-10-04 09:22:19 +02003758 !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
3759 if (skb_linearize(skb)) {
3760 ieee80211_free_txskb(&local->hw, skb);
3761 goto begin;
3762 }
3763 }
3764
Johannes Berg53168212017-06-22 12:20:30 +02003765 switch (tx.sdata->vif.type) {
3766 case NL80211_IFTYPE_MONITOR:
3767 if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
3768 vif = &tx.sdata->vif;
3769 break;
3770 }
3771 tx.sdata = rcu_dereference(local->monitor_sdata);
3772 if (tx.sdata) {
3773 vif = &tx.sdata->vif;
3774 info->hw_queue =
3775 vif->hw_queue[skb_get_queue_mapping(skb)];
3776 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
3777 ieee80211_free_txskb(&local->hw, skb);
3778 goto begin;
3779 } else {
3780 vif = NULL;
3781 }
3782 break;
3783 case NL80211_IFTYPE_AP_VLAN:
3784 tx.sdata = container_of(tx.sdata->bss,
3785 struct ieee80211_sub_if_data, u.ap);
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05003786 fallthrough;
Johannes Berg53168212017-06-22 12:20:30 +02003787 default:
3788 vif = &tx.sdata->vif;
3789 break;
3790 }
3791
John Crispin50ff4772019-11-25 11:04:37 +01003792encap_out:
Johannes Berg53168212017-06-22 12:20:30 +02003793 IEEE80211_SKB_CB(skb)->control.vif = vif;
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003794
Johannes Bergca98c472020-02-21 10:45:45 +01003795 if (vif &&
3796 wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) {
Felix Fietkau3ff901c2020-07-24 20:28:16 +02003797 bool ampdu = txq->ac != IEEE80211_AC_VO;
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003798 u32 airtime;
3799
3800 airtime = ieee80211_calc_expected_tx_airtime(hw, vif, txq->sta,
Felix Fietkau3ff901c2020-07-24 20:28:16 +02003801 skb->len, ampdu);
Toke Høiland-Jørgensen7a892332019-11-18 22:06:10 -08003802 if (airtime) {
3803 airtime = ieee80211_info_set_tx_time_est(info, airtime);
3804 ieee80211_sta_update_pending_airtime(local, tx.sta,
3805 txq->ac,
3806 airtime,
3807 false);
3808 }
3809 }
3810
Felix Fietkauded46982019-03-16 18:06:33 +01003811 return skb;
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303812
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003813out:
3814 spin_unlock_bh(&fq->lock);
3815
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003816 return skb;
3817}
3818EXPORT_SYMBOL(ieee80211_tx_dequeue);
3819
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003820struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac)
3821{
3822 struct ieee80211_local *local = hw_to_local(hw);
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02003823 struct airtime_sched_info *air_sched;
Felix Fietkau6a789ba2021-12-17 12:42:58 +01003824 u64 now = ktime_get_coarse_boottime_ns();
Felix Fietkau5b989c12019-03-15 11:03:35 +01003825 struct ieee80211_txq *ret = NULL;
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02003826 struct airtime_info *air_info;
3827 struct txq_info *txqi = NULL;
3828 struct rb_node *node;
3829 bool first = false;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003830
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02003831 air_sched = &local->airtime[ac];
3832 spin_lock_bh(&air_sched->lock);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003833
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02003834 node = air_sched->schedule_pos;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003835
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02003836begin:
3837 if (!node) {
3838 node = rb_first_cached(&air_sched->active_txqs);
3839 first = true;
3840 } else {
3841 node = rb_next(node);
Kan Yan3ace10f2019-11-18 22:06:09 -08003842 }
3843
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02003844 if (!node)
Felix Fietkau5b989c12019-03-15 11:03:35 +01003845 goto out;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003846
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02003847 txqi = container_of(node, struct txq_info, schedule_order);
3848 air_info = to_airtime_info(&txqi->txq);
3849
3850 if (air_info->v_t > air_sched->v_t &&
3851 (!first || !airtime_catchup_v_t(air_sched, air_info->v_t, now)))
3852 goto out;
3853
3854 if (!ieee80211_txq_airtime_check(hw, &txqi->txq)) {
3855 first = false;
3856 goto begin;
3857 }
3858
3859 air_sched->schedule_pos = node;
3860 air_sched->last_schedule_activity = now;
Felix Fietkau5b989c12019-03-15 11:03:35 +01003861 ret = &txqi->txq;
Felix Fietkau5b989c12019-03-15 11:03:35 +01003862out:
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02003863 spin_unlock_bh(&air_sched->lock);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003864 return ret;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003865}
3866EXPORT_SYMBOL(ieee80211_next_txq);
3867
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02003868static void __ieee80211_insert_txq(struct rb_root_cached *root,
3869 struct txq_info *txqi)
3870{
3871 struct rb_node **new = &root->rb_root.rb_node;
3872 struct airtime_info *old_air, *new_air;
3873 struct rb_node *parent = NULL;
3874 struct txq_info *__txqi;
3875 bool leftmost = true;
3876
3877 while (*new) {
3878 parent = *new;
3879 __txqi = rb_entry(parent, struct txq_info, schedule_order);
3880 old_air = to_airtime_info(&__txqi->txq);
3881 new_air = to_airtime_info(&txqi->txq);
3882
3883 if (new_air->v_t <= old_air->v_t) {
3884 new = &parent->rb_left;
3885 } else {
3886 new = &parent->rb_right;
3887 leftmost = false;
3888 }
3889 }
3890
3891 rb_link_node(&txqi->schedule_order, parent, new);
3892 rb_insert_color_cached(&txqi->schedule_order, root, leftmost);
3893}
3894
3895void ieee80211_resort_txq(struct ieee80211_hw *hw,
3896 struct ieee80211_txq *txq)
3897{
3898 struct airtime_info *air_info = to_airtime_info(txq);
3899 struct ieee80211_local *local = hw_to_local(hw);
3900 struct txq_info *txqi = to_txq_info(txq);
3901 struct airtime_sched_info *air_sched;
3902
3903 air_sched = &local->airtime[txq->ac];
3904
3905 lockdep_assert_held(&air_sched->lock);
3906
3907 if (!RB_EMPTY_NODE(&txqi->schedule_order)) {
3908 struct airtime_info *a_prev = NULL, *a_next = NULL;
3909 struct txq_info *t_prev, *t_next;
3910 struct rb_node *n_prev, *n_next;
3911
3912 /* Erasing a node can cause an expensive rebalancing operation,
3913 * so we check the previous and next nodes first and only remove
3914 * and re-insert if the current node is not already in the
3915 * correct position.
3916 */
3917 if ((n_prev = rb_prev(&txqi->schedule_order)) != NULL) {
3918 t_prev = container_of(n_prev, struct txq_info,
3919 schedule_order);
3920 a_prev = to_airtime_info(&t_prev->txq);
3921 }
3922
3923 if ((n_next = rb_next(&txqi->schedule_order)) != NULL) {
3924 t_next = container_of(n_next, struct txq_info,
3925 schedule_order);
3926 a_next = to_airtime_info(&t_next->txq);
3927 }
3928
3929 if ((!a_prev || a_prev->v_t <= air_info->v_t) &&
3930 (!a_next || a_next->v_t > air_info->v_t))
3931 return;
3932
3933 if (air_sched->schedule_pos == &txqi->schedule_order)
3934 air_sched->schedule_pos = n_prev;
3935
3936 rb_erase_cached(&txqi->schedule_order,
3937 &air_sched->active_txqs);
3938 RB_CLEAR_NODE(&txqi->schedule_order);
3939 __ieee80211_insert_txq(&air_sched->active_txqs, txqi);
3940 }
3941}
3942
3943void ieee80211_update_airtime_weight(struct ieee80211_local *local,
3944 struct airtime_sched_info *air_sched,
3945 u64 now, bool force)
3946{
3947 struct airtime_info *air_info, *tmp;
3948 u64 weight_sum = 0;
3949
3950 if (unlikely(!now))
Felix Fietkau6a789ba2021-12-17 12:42:58 +01003951 now = ktime_get_coarse_boottime_ns();
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02003952
3953 lockdep_assert_held(&air_sched->lock);
3954
3955 if (!force && (air_sched->last_weight_update <
3956 now - AIRTIME_ACTIVE_DURATION))
3957 return;
3958
3959 list_for_each_entry_safe(air_info, tmp,
3960 &air_sched->active_list, list) {
3961 if (airtime_is_active(air_info, now))
3962 weight_sum += air_info->weight;
3963 else
3964 list_del_init(&air_info->list);
3965 }
3966 airtime_weight_sum_set(air_sched, weight_sum);
3967 air_sched->last_weight_update = now;
3968}
3969
3970void ieee80211_schedule_txq(struct ieee80211_hw *hw,
3971 struct ieee80211_txq *txq)
3972 __acquires(txq_lock) __releases(txq_lock)
3973{
3974 struct ieee80211_local *local = hw_to_local(hw);
3975 struct txq_info *txqi = to_txq_info(txq);
3976 struct airtime_sched_info *air_sched;
Felix Fietkau6a789ba2021-12-17 12:42:58 +01003977 u64 now = ktime_get_coarse_boottime_ns();
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02003978 struct airtime_info *air_info;
3979 u8 ac = txq->ac;
3980 bool was_active;
3981
3982 air_sched = &local->airtime[ac];
3983 air_info = to_airtime_info(txq);
3984
3985 spin_lock_bh(&air_sched->lock);
3986 was_active = airtime_is_active(air_info, now);
3987 airtime_set_active(air_sched, air_info, now);
3988
3989 if (!RB_EMPTY_NODE(&txqi->schedule_order))
3990 goto out;
3991
3992 /* If the station has been inactive for a while, catch up its v_t so it
3993 * doesn't get indefinite priority; see comment above the definition of
3994 * AIRTIME_MAX_BEHIND.
3995 */
3996 if ((!was_active && air_info->v_t < air_sched->v_t) ||
3997 air_info->v_t < air_sched->v_t - AIRTIME_MAX_BEHIND)
3998 air_info->v_t = air_sched->v_t;
3999
4000 ieee80211_update_airtime_weight(local, air_sched, now, !was_active);
4001 __ieee80211_insert_txq(&air_sched->active_txqs, txqi);
4002
4003out:
4004 spin_unlock_bh(&air_sched->lock);
4005}
4006EXPORT_SYMBOL(ieee80211_schedule_txq);
4007
4008static void __ieee80211_unschedule_txq(struct ieee80211_hw *hw,
4009 struct ieee80211_txq *txq,
4010 bool purge)
4011{
4012 struct ieee80211_local *local = hw_to_local(hw);
4013 struct txq_info *txqi = to_txq_info(txq);
4014 struct airtime_sched_info *air_sched;
4015 struct airtime_info *air_info;
4016
4017 air_sched = &local->airtime[txq->ac];
4018 air_info = to_airtime_info(&txqi->txq);
4019
4020 lockdep_assert_held(&air_sched->lock);
4021
4022 if (purge) {
4023 list_del_init(&air_info->list);
4024 ieee80211_update_airtime_weight(local, air_sched, 0, true);
4025 }
4026
4027 if (RB_EMPTY_NODE(&txqi->schedule_order))
4028 return;
4029
4030 if (air_sched->schedule_pos == &txqi->schedule_order)
4031 air_sched->schedule_pos = rb_prev(&txqi->schedule_order);
4032
4033 if (!purge)
4034 airtime_set_active(air_sched, air_info,
Felix Fietkau6a789ba2021-12-17 12:42:58 +01004035 ktime_get_coarse_boottime_ns());
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004036
4037 rb_erase_cached(&txqi->schedule_order,
4038 &air_sched->active_txqs);
4039 RB_CLEAR_NODE(&txqi->schedule_order);
4040}
4041
4042void ieee80211_unschedule_txq(struct ieee80211_hw *hw,
Felix Fietkau2b4a6692019-03-18 12:00:58 +01004043 struct ieee80211_txq *txq,
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004044 bool purge)
4045 __acquires(txq_lock) __releases(txq_lock)
4046{
4047 struct ieee80211_local *local = hw_to_local(hw);
4048
4049 spin_lock_bh(&local->airtime[txq->ac].lock);
4050 __ieee80211_unschedule_txq(hw, txq, purge);
4051 spin_unlock_bh(&local->airtime[txq->ac].lock);
4052}
4053
4054void ieee80211_return_txq(struct ieee80211_hw *hw,
4055 struct ieee80211_txq *txq, bool force)
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08004056{
4057 struct ieee80211_local *local = hw_to_local(hw);
4058 struct txq_info *txqi = to_txq_info(txq);
4059
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004060 spin_lock_bh(&local->airtime[txq->ac].lock);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08004061
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004062 if (!RB_EMPTY_NODE(&txqi->schedule_order) && !force &&
4063 !txq_has_queue(txq))
4064 __ieee80211_unschedule_txq(hw, txq, false);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08004065
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004066 spin_unlock_bh(&local->airtime[txq->ac].lock);
Toke Høiland-Jørgensen390298e2019-01-22 15:20:16 +01004067}
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004068EXPORT_SYMBOL(ieee80211_return_txq);
Toke Høiland-Jørgensen390298e2019-01-22 15:20:16 +01004069
Lorenzo Bianconie9084352021-01-09 18:57:51 +01004070DEFINE_STATIC_KEY_FALSE(aql_disable);
4071
Kan Yan3ace10f2019-11-18 22:06:09 -08004072bool ieee80211_txq_airtime_check(struct ieee80211_hw *hw,
4073 struct ieee80211_txq *txq)
4074{
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004075 struct airtime_info *air_info = to_airtime_info(txq);
Kan Yan3ace10f2019-11-18 22:06:09 -08004076 struct ieee80211_local *local = hw_to_local(hw);
4077
Toke Høiland-Jørgensen911bde02019-12-12 12:14:37 +01004078 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
Kan Yan3ace10f2019-11-18 22:06:09 -08004079 return true;
4080
Lorenzo Bianconie9084352021-01-09 18:57:51 +01004081 if (static_branch_unlikely(&aql_disable))
4082 return true;
4083
Kan Yan3ace10f2019-11-18 22:06:09 -08004084 if (!txq->sta)
4085 return true;
4086
Johannes Berg73bc9e02021-03-19 23:28:01 +01004087 if (unlikely(txq->tid == IEEE80211_NUM_TIDS))
4088 return true;
4089
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004090 if (atomic_read(&air_info->aql_tx_pending) < air_info->aql_limit_low)
Kan Yan3ace10f2019-11-18 22:06:09 -08004091 return true;
4092
4093 if (atomic_read(&local->aql_total_pending_airtime) <
4094 local->aql_threshold &&
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004095 atomic_read(&air_info->aql_tx_pending) < air_info->aql_limit_high)
Kan Yan3ace10f2019-11-18 22:06:09 -08004096 return true;
4097
4098 return false;
4099}
4100EXPORT_SYMBOL(ieee80211_txq_airtime_check);
4101
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08004102bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
4103 struct ieee80211_txq *txq)
4104{
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004105 struct txq_info *first_txqi = NULL, *txqi = to_txq_info(txq);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08004106 struct ieee80211_local *local = hw_to_local(hw);
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004107 struct airtime_sched_info *air_sched;
4108 struct airtime_info *air_info;
4109 struct rb_node *node = NULL;
4110 bool ret = false;
4111 u64 now;
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08004112
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08004113
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004114 if (!ieee80211_txq_airtime_check(hw, txq))
4115 return false;
4116
4117 air_sched = &local->airtime[txq->ac];
4118 spin_lock_bh(&air_sched->lock);
4119
4120 if (RB_EMPTY_NODE(&txqi->schedule_order))
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08004121 goto out;
4122
Felix Fietkau6a789ba2021-12-17 12:42:58 +01004123 now = ktime_get_coarse_boottime_ns();
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08004124
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004125 /* Like in ieee80211_next_txq(), make sure the first station in the
4126 * scheduling order is eligible for transmission to avoid starvation.
4127 */
4128 node = rb_first_cached(&air_sched->active_txqs);
4129 if (node) {
4130 first_txqi = container_of(node, struct txq_info,
4131 schedule_order);
4132 air_info = to_airtime_info(&first_txqi->txq);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08004133
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004134 if (air_sched->v_t < air_info->v_t)
4135 airtime_catchup_v_t(air_sched, air_info->v_t, now);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08004136 }
4137
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004138 air_info = to_airtime_info(&txqi->txq);
4139 if (air_info->v_t <= air_sched->v_t) {
4140 air_sched->last_schedule_activity = now;
4141 ret = true;
4142 }
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08004143
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08004144out:
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004145 spin_unlock_bh(&air_sched->lock);
4146 return ret;
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08004147}
4148EXPORT_SYMBOL(ieee80211_txq_may_transmit);
4149
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08004150void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08004151{
4152 struct ieee80211_local *local = hw_to_local(hw);
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004153 struct airtime_sched_info *air_sched = &local->airtime[ac];
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08004154
Toke Høiland-Jørgensen24336472021-06-23 15:47:55 +02004155 spin_lock_bh(&air_sched->lock);
4156 air_sched->schedule_pos = NULL;
4157 spin_unlock_bh(&air_sched->lock);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08004158}
Felix Fietkau5b989c12019-03-15 11:03:35 +01004159EXPORT_SYMBOL(ieee80211_txq_schedule_start);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08004160
Johannes Berg4c9451e2014-11-09 18:50:10 +02004161void __ieee80211_subif_start_xmit(struct sk_buff *skb,
4162 struct net_device *dev,
Rajkumar Manoharan06016772019-04-11 13:47:25 -07004163 u32 info_flags,
Markus Theila7528192020-05-27 18:03:34 +02004164 u32 ctrl_flags,
4165 u64 *cookie)
Johannes Berg4c9451e2014-11-09 18:50:10 +02004166{
4167 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Felix Fietkau1974da82019-03-25 08:59:23 +01004168 struct ieee80211_local *local = sdata->local;
Johannes Berg97ffe752015-03-21 09:13:45 +01004169 struct sta_info *sta;
Johannes Berg80616c02015-04-14 14:50:41 +02004170 struct sk_buff *next;
Felix Fietkau30f6cf92021-11-13 07:34:15 +01004171 int len = skb->len;
Johannes Berg4c9451e2014-11-09 18:50:10 +02004172
4173 if (unlikely(skb->len < ETH_HLEN)) {
4174 kfree_skb(skb);
4175 return;
4176 }
4177
4178 rcu_read_lock();
4179
Johannes Berg2d981fd2015-04-10 14:10:10 +02004180 if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
4181 goto out_free;
Johannes Berg4c9451e2014-11-09 18:50:10 +02004182
Felix Fietkau1974da82019-03-25 08:59:23 +01004183 if (IS_ERR(sta))
4184 sta = NULL;
4185
4186 if (local->ops->wake_tx_queue) {
4187 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
4188 skb_set_queue_mapping(skb, queue);
Felix Fietkau180ac482020-07-26 15:09:47 +02004189 skb_get_hash(skb);
Felix Fietkau1974da82019-03-25 08:59:23 +01004190 }
4191
Felix Fietkau08a46c642021-06-17 18:31:11 +02004192 ieee80211_aggr_check(sdata, sta, skb);
4193
Maxime Bizon48c06702021-11-18 12:58:24 +01004194 sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
4195
Felix Fietkau1974da82019-03-25 08:59:23 +01004196 if (sta) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01004197 struct ieee80211_fast_tx *fast_tx;
4198
4199 fast_tx = rcu_dereference(sta->fast_tx);
4200
4201 if (fast_tx &&
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02004202 ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
Johannes Berg17c18bf2015-03-21 15:25:43 +01004203 goto out;
4204 }
4205
Johannes Berg80616c02015-04-14 14:50:41 +02004206 if (skb_is_gso(skb)) {
4207 struct sk_buff *segs;
Johannes Berg680a0da2015-04-13 16:58:25 +02004208
Johannes Berg80616c02015-04-14 14:50:41 +02004209 segs = skb_gso_segment(skb, 0);
4210 if (IS_ERR(segs)) {
Johannes Berg2d981fd2015-04-10 14:10:10 +02004211 goto out_free;
Johannes Berg80616c02015-04-14 14:50:41 +02004212 } else if (segs) {
4213 consume_skb(skb);
4214 skb = segs;
4215 }
4216 } else {
4217 /* we cannot process non-linear frames on this path */
Felix Fietkau30f6cf92021-11-13 07:34:15 +01004218 if (skb_linearize(skb))
4219 goto out_free;
Johannes Berg80616c02015-04-14 14:50:41 +02004220
4221 /* the frame could be fragmented, software-encrypted, and other
4222 * things so we cannot really handle checksum offload with it -
4223 * fix it up in software before we handle anything else.
4224 */
4225 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Johannes Bergf603f1f2015-05-05 15:25:33 +02004226 skb_set_transport_header(skb,
4227 skb_checksum_start_offset(skb));
Johannes Berg80616c02015-04-14 14:50:41 +02004228 if (skb_checksum_help(skb))
4229 goto out_free;
4230 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02004231 }
4232
Jason A. Donenfeld9f3ef3d2020-01-13 18:42:33 -05004233 skb_list_walk_safe(skb, skb, next) {
4234 skb_mark_not_on_list(skb);
Johannes Berg4c9451e2014-11-09 18:50:10 +02004235
Markus Theil5af7fef2020-06-17 10:26:36 +02004236 if (skb->protocol == sdata->control_port_protocol)
4237 ctrl_flags |= IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
4238
Rajkumar Manoharan06016772019-04-11 13:47:25 -07004239 skb = ieee80211_build_hdr(sdata, skb, info_flags,
Markus Theila7528192020-05-27 18:03:34 +02004240 sta, ctrl_flags, cookie);
Jason A. Donenfeld9f3ef3d2020-01-13 18:42:33 -05004241 if (IS_ERR(skb)) {
4242 kfree_skb_list(next);
Johannes Berg80616c02015-04-14 14:50:41 +02004243 goto out;
Jason A. Donenfeld9f3ef3d2020-01-13 18:42:33 -05004244 }
Johannes Berg80616c02015-04-14 14:50:41 +02004245
Lev Stipakov36ec1442020-11-13 23:46:24 +02004246 dev_sw_netstats_tx_add(dev, 1, skb->len);
Johannes Berg80616c02015-04-14 14:50:41 +02004247
Mathy Vanhoef08aca292020-07-23 14:01:52 +04004248 ieee80211_xmit(sdata, sta, skb);
Johannes Berg80616c02015-04-14 14:50:41 +02004249 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02004250 goto out;
4251 out_free:
4252 kfree_skb(skb);
Felix Fietkau30f6cf92021-11-13 07:34:15 +01004253 len = 0;
Johannes Berg4c9451e2014-11-09 18:50:10 +02004254 out:
Felix Fietkau30f6cf92021-11-13 07:34:15 +01004255 if (len)
4256 ieee80211_tpt_led_trig_tx(local, len);
Johannes Berg4c9451e2014-11-09 18:50:10 +02004257 rcu_read_unlock();
4258}
4259
Michael Braunebceec82016-11-22 11:52:18 +01004260static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
4261{
4262 struct ethhdr *eth;
4263 int err;
4264
4265 err = skb_ensure_writable(skb, ETH_HLEN);
4266 if (unlikely(err))
4267 return err;
4268
4269 eth = (void *)skb->data;
4270 ether_addr_copy(eth->h_dest, sta->sta.addr);
4271
4272 return 0;
4273}
4274
4275static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
4276 struct net_device *dev)
4277{
4278 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4279 const struct ethhdr *eth = (void *)skb->data;
4280 const struct vlan_ethhdr *ethvlan = (void *)skb->data;
4281 __be16 ethertype;
4282
4283 if (likely(!is_multicast_ether_addr(eth->h_dest)))
4284 return false;
4285
4286 switch (sdata->vif.type) {
4287 case NL80211_IFTYPE_AP_VLAN:
4288 if (sdata->u.vlan.sta)
4289 return false;
4290 if (sdata->wdev.use_4addr)
4291 return false;
Gustavo A. R. Silvafc0561d2020-07-07 15:45:48 -05004292 fallthrough;
Michael Braunebceec82016-11-22 11:52:18 +01004293 case NL80211_IFTYPE_AP:
4294 /* check runtime toggle for this bss */
4295 if (!sdata->bss->multicast_to_unicast)
4296 return false;
4297 break;
4298 default:
4299 return false;
4300 }
4301
4302 /* multicast to unicast conversion only for some payload */
4303 ethertype = eth->h_proto;
4304 if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
4305 ethertype = ethvlan->h_vlan_encapsulated_proto;
4306 switch (ethertype) {
4307 case htons(ETH_P_ARP):
4308 case htons(ETH_P_IP):
4309 case htons(ETH_P_IPV6):
4310 break;
4311 default:
4312 return false;
4313 }
4314
4315 return true;
4316}
4317
4318static void
4319ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
4320 struct sk_buff_head *queue)
4321{
4322 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4323 struct ieee80211_local *local = sdata->local;
4324 const struct ethhdr *eth = (struct ethhdr *)skb->data;
4325 struct sta_info *sta, *first = NULL;
4326 struct sk_buff *cloned_skb;
4327
4328 rcu_read_lock();
4329
4330 list_for_each_entry_rcu(sta, &local->sta_list, list) {
4331 if (sdata != sta->sdata)
4332 /* AP-VLAN mismatch */
4333 continue;
4334 if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
4335 /* do not send back to source */
4336 continue;
4337 if (!first) {
4338 first = sta;
4339 continue;
4340 }
4341 cloned_skb = skb_clone(skb, GFP_ATOMIC);
4342 if (!cloned_skb)
4343 goto multicast;
4344 if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
4345 dev_kfree_skb(cloned_skb);
4346 goto multicast;
4347 }
4348 __skb_queue_tail(queue, cloned_skb);
4349 }
4350
4351 if (likely(first)) {
4352 if (unlikely(ieee80211_change_da(skb, first)))
4353 goto multicast;
4354 __skb_queue_tail(queue, skb);
4355 } else {
4356 /* no STA connected, drop */
4357 kfree_skb(skb);
4358 skb = NULL;
4359 }
4360
4361 goto out;
4362multicast:
4363 __skb_queue_purge(queue);
4364 __skb_queue_tail(queue, skb);
4365out:
4366 rcu_read_unlock();
4367}
4368
Johannes Berg4c9451e2014-11-09 18:50:10 +02004369/**
4370 * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
4371 * @skb: packet to be sent
4372 * @dev: incoming interface
4373 *
4374 * On failure skb will be freed.
4375 */
Liad Kaufman24d342c2014-11-09 18:50:07 +02004376netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
4377 struct net_device *dev)
4378{
Michael Braunebceec82016-11-22 11:52:18 +01004379 if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
4380 struct sk_buff_head queue;
4381
4382 __skb_queue_head_init(&queue);
4383 ieee80211_convert_to_unicast(skb, dev, &queue);
4384 while ((skb = __skb_dequeue(&queue)))
Markus Theila7528192020-05-27 18:03:34 +02004385 __ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
Michael Braunebceec82016-11-22 11:52:18 +01004386 } else {
Markus Theila7528192020-05-27 18:03:34 +02004387 __ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
Michael Braunebceec82016-11-22 11:52:18 +01004388 }
4389
Liad Kaufman24d342c2014-11-09 18:50:07 +02004390 return NETDEV_TX_OK;
4391}
Johannes Berge2ebc742007-07-27 15:43:22 +02004392
John Crispin50ff4772019-11-25 11:04:37 +01004393static bool ieee80211_tx_8023(struct ieee80211_sub_if_data *sdata,
Felix Fietkau30f6cf92021-11-13 07:34:15 +01004394 struct sk_buff *skb, struct sta_info *sta,
John Crispin50ff4772019-11-25 11:04:37 +01004395 bool txpending)
4396{
4397 struct ieee80211_local *local = sdata->local;
4398 struct ieee80211_tx_control control = {};
4399 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4400 struct ieee80211_sta *pubsta = NULL;
4401 unsigned long flags;
4402 int q = info->hw_queue;
4403
Lorenzo Bianconi196900f2021-03-08 23:01:49 +01004404 if (sta)
4405 sk_pacing_shift_update(skb->sk, local->hw.tx_sk_pacing_shift);
4406
Felix Fietkau30f6cf92021-11-13 07:34:15 +01004407 ieee80211_tpt_led_trig_tx(local, skb->len);
4408
John Crispin50ff4772019-11-25 11:04:37 +01004409 if (ieee80211_queue_skb(local, sdata, sta, skb))
4410 return true;
4411
4412 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4413
4414 if (local->queue_stop_reasons[q] ||
4415 (!txpending && !skb_queue_empty(&local->pending[q]))) {
4416 if (txpending)
4417 skb_queue_head(&local->pending[q], skb);
4418 else
4419 skb_queue_tail(&local->pending[q], skb);
4420
4421 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4422
4423 return false;
4424 }
4425
4426 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4427
4428 if (sta && sta->uploaded)
4429 pubsta = &sta->sta;
4430
4431 control.sta = pubsta;
4432
4433 drv_tx(local, &control, skb);
4434
4435 return true;
4436}
4437
4438static void ieee80211_8023_xmit(struct ieee80211_sub_if_data *sdata,
4439 struct net_device *dev, struct sta_info *sta,
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004440 struct ieee80211_key *key, struct sk_buff *skb)
John Crispin50ff4772019-11-25 11:04:37 +01004441{
4442 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
John Crispin50ff4772019-11-25 11:04:37 +01004443 struct ieee80211_local *local = sdata->local;
Felix Fietkau96ae9cd2020-09-08 14:36:50 +02004444 struct tid_ampdu_tx *tid_tx;
4445 u8 tid;
John Crispin50ff4772019-11-25 11:04:37 +01004446
Felix Fietkau5f8d69e2020-09-08 14:36:49 +02004447 if (local->ops->wake_tx_queue) {
4448 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
4449 skb_set_queue_mapping(skb, queue);
4450 skb_get_hash(skb);
4451 }
4452
John Crispin50ff4772019-11-25 11:04:37 +01004453 if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning)) &&
4454 test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
4455 goto out_free;
4456
Vasanthakumar Thiagarajane61fbfc2020-07-22 19:50:17 +05304457 memset(info, 0, sizeof(*info));
4458
Felix Fietkau08a46c642021-06-17 18:31:11 +02004459 ieee80211_aggr_check(sdata, sta, skb);
4460
Felix Fietkauaea6a3f2020-09-08 14:36:51 +02004461 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
4462 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
4463 if (tid_tx) {
4464 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
4465 /* fall back to non-offload slow path */
4466 __ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
4467 return;
Felix Fietkau96ae9cd2020-09-08 14:36:50 +02004468 }
Felix Fietkauaea6a3f2020-09-08 14:36:51 +02004469
4470 info->flags |= IEEE80211_TX_CTL_AMPDU;
4471 if (tid_tx->timeout)
4472 tid_tx->last_tx = jiffies;
Felix Fietkau96ae9cd2020-09-08 14:36:50 +02004473 }
4474
Felix Fietkauaea6a3f2020-09-08 14:36:51 +02004475 if (unlikely(skb->sk &&
John Crispin50ff4772019-11-25 11:04:37 +01004476 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS))
Vasanthakumar Thiagarajane61fbfc2020-07-22 19:50:17 +05304477 info->ack_frame_id = ieee80211_store_ack_skb(local, skb,
4478 &info->flags, NULL);
John Crispin50ff4772019-11-25 11:04:37 +01004479
John Crispin50ff4772019-11-25 11:04:37 +01004480 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
4481
Lev Stipakov36ec1442020-11-13 23:46:24 +02004482 dev_sw_netstats_tx_add(dev, 1, skb->len);
John Crispin50ff4772019-11-25 11:04:37 +01004483
Felix Fietkauaea6a3f2020-09-08 14:36:51 +02004484 sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
4485 sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
John Crispin50ff4772019-11-25 11:04:37 +01004486
4487 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4488 sdata = container_of(sdata->bss,
4489 struct ieee80211_sub_if_data, u.ap);
4490
Felix Fietkaucc20ff22020-09-08 14:36:57 +02004491 info->flags |= IEEE80211_TX_CTL_HW_80211_ENCAP;
John Crispin50ff4772019-11-25 11:04:37 +01004492 info->control.vif = &sdata->vif;
4493
Felix Fietkauae045152020-09-08 14:36:52 +02004494 if (key)
4495 info->control.hw_key = &key->conf;
4496
Felix Fietkau30f6cf92021-11-13 07:34:15 +01004497 ieee80211_tx_8023(sdata, skb, sta, false);
John Crispin50ff4772019-11-25 11:04:37 +01004498
4499 return;
4500
4501out_free:
4502 kfree_skb(skb);
4503}
4504
4505netdev_tx_t ieee80211_subif_start_xmit_8023(struct sk_buff *skb,
4506 struct net_device *dev)
4507{
4508 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Felix Fietkauaea6a3f2020-09-08 14:36:51 +02004509 struct ethhdr *ehdr = (struct ethhdr *)skb->data;
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004510 struct ieee80211_key *key;
John Crispin50ff4772019-11-25 11:04:37 +01004511 struct sta_info *sta;
John Crispin50ff4772019-11-25 11:04:37 +01004512
4513 if (unlikely(skb->len < ETH_HLEN)) {
4514 kfree_skb(skb);
4515 return NETDEV_TX_OK;
4516 }
4517
4518 rcu_read_lock();
4519
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004520 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
John Crispin50ff4772019-11-25 11:04:37 +01004521 kfree_skb(skb);
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004522 goto out;
4523 }
John Crispin50ff4772019-11-25 11:04:37 +01004524
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004525 if (unlikely(IS_ERR_OR_NULL(sta) || !sta->uploaded ||
4526 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
Felix Fietkaub101dd22020-12-18 19:47:16 +01004527 sdata->control_port_protocol == ehdr->h_proto))
4528 goto skip_offload;
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004529
Felix Fietkaub101dd22020-12-18 19:47:16 +01004530 key = rcu_dereference(sta->ptk[sta->ptk_idx]);
4531 if (!key)
4532 key = rcu_dereference(sdata->default_unicast_key);
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004533
Felix Fietkaub101dd22020-12-18 19:47:16 +01004534 if (key && (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) ||
4535 key->conf.cipher == WLAN_CIPHER_SUITE_TKIP))
4536 goto skip_offload;
4537
4538 ieee80211_8023_xmit(sdata, dev, sta, key, skb);
4539 goto out;
4540
4541skip_offload:
4542 ieee80211_subif_start_xmit(skb, dev);
Felix Fietkau6aea26c2020-09-08 14:36:53 +02004543out:
John Crispin50ff4772019-11-25 11:04:37 +01004544 rcu_read_unlock();
4545
4546 return NETDEV_TX_OK;
4547}
4548
Johannes Berg7528ec52014-11-09 18:50:11 +02004549struct sk_buff *
4550ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
4551 struct sk_buff *skb, u32 info_flags)
4552{
4553 struct ieee80211_hdr *hdr;
4554 struct ieee80211_tx_data tx = {
4555 .local = sdata->local,
4556 .sdata = sdata,
4557 };
Johannes Berg97ffe752015-03-21 09:13:45 +01004558 struct sta_info *sta;
Johannes Berg7528ec52014-11-09 18:50:11 +02004559
4560 rcu_read_lock();
4561
Johannes Berg97ffe752015-03-21 09:13:45 +01004562 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4563 kfree_skb(skb);
4564 skb = ERR_PTR(-EINVAL);
4565 goto out;
4566 }
4567
Markus Theila7528192020-05-27 18:03:34 +02004568 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta, 0, NULL);
Johannes Berg7528ec52014-11-09 18:50:11 +02004569 if (IS_ERR(skb))
4570 goto out;
4571
4572 hdr = (void *)skb->data;
4573 tx.sta = sta_info_get(sdata, hdr->addr1);
4574 tx.skb = skb;
4575
4576 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
4577 rcu_read_unlock();
4578 kfree_skb(skb);
4579 return ERR_PTR(-EINVAL);
4580 }
4581
4582out:
4583 rcu_read_unlock();
4584 return skb;
4585}
4586
Johannes Berge2530082008-05-17 00:57:14 +02004587/*
4588 * ieee80211_clear_tx_pending may not be called in a context where
4589 * it is possible that it packets could come in again.
4590 */
Johannes Berge2ebc742007-07-27 15:43:22 +02004591void ieee80211_clear_tx_pending(struct ieee80211_local *local)
4592{
Felix Fietkau1f98ab72012-11-10 03:44:14 +01004593 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01004594 int i;
Johannes Berge2ebc742007-07-27 15:43:22 +02004595
Felix Fietkau1f98ab72012-11-10 03:44:14 +01004596 for (i = 0; i < local->hw.queues; i++) {
4597 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
4598 ieee80211_free_txskb(&local->hw, skb);
4599 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004600}
4601
Johannes Berg7bb45682011-02-24 14:42:06 +01004602/*
4603 * Returns false if the frame couldn't be transmitted but was queued instead,
4604 * which in this case means re-queued -- take as an indication to stop sending
4605 * more pending frames.
4606 */
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004607static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
4608 struct sk_buff *skb)
4609{
4610 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4611 struct ieee80211_sub_if_data *sdata;
4612 struct sta_info *sta;
4613 struct ieee80211_hdr *hdr;
Johannes Berg7bb45682011-02-24 14:42:06 +01004614 bool result;
Johannes Berg55de9082012-07-26 17:24:39 +02004615 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004616
Johannes Berg5061b0c2009-07-14 00:33:34 +02004617 sdata = vif_to_sdata(info->control.vif);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004618
Felix Fietkaucc20ff22020-09-08 14:36:57 +02004619 if (info->control.flags & IEEE80211_TX_INTCFL_NEED_TXPROCESSING) {
Johannes Berg55de9082012-07-26 17:24:39 +02004620 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4621 if (unlikely(!chanctx_conf)) {
4622 dev_kfree_skb(skb);
4623 return true;
4624 }
Johannes Berg73c4e192014-11-09 18:50:09 +02004625 info->band = chanctx_conf->def.chan->band;
Mathy Vanhoef08aca292020-07-23 14:01:52 +04004626 result = ieee80211_tx(sdata, NULL, skb, true);
Felix Fietkaucc20ff22020-09-08 14:36:57 +02004627 } else if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
John Crispin50ff4772019-11-25 11:04:37 +01004628 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4629 dev_kfree_skb(skb);
4630 return true;
4631 }
4632
4633 if (IS_ERR(sta) || (sta && !sta->uploaded))
4634 sta = NULL;
4635
Felix Fietkau30f6cf92021-11-13 07:34:15 +01004636 result = ieee80211_tx_8023(sdata, skb, sta, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004637 } else {
Johannes Berg252b86c2011-11-16 15:28:55 +01004638 struct sk_buff_head skbs;
4639
4640 __skb_queue_head_init(&skbs);
4641 __skb_queue_tail(&skbs, skb);
4642
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004643 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Bergabe60632009-11-25 17:46:18 +01004644 sta = sta_info_get(sdata, hdr->addr1);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004645
Felix Fietkau30f6cf92021-11-13 07:34:15 +01004646 result = __ieee80211_tx(local, &skbs, sta, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004647 }
4648
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004649 return result;
4650}
4651
Johannes Berge2530082008-05-17 00:57:14 +02004652/*
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004653 * Transmit all pending packets. Called from tasklet.
Johannes Berge2530082008-05-17 00:57:14 +02004654 */
Allen Paisda1cad72020-11-03 14:48:18 +05304655void ieee80211_tx_pending(struct tasklet_struct *t)
Johannes Berge2ebc742007-07-27 15:43:22 +02004656{
Allen Paisda1cad72020-11-03 14:48:18 +05304657 struct ieee80211_local *local = from_tasklet(local, t,
4658 tx_pending_tasklet);
Johannes Berg2a577d92009-03-23 17:28:37 +01004659 unsigned long flags;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004660 int i;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004661 bool txok;
Johannes Berge2ebc742007-07-27 15:43:22 +02004662
Johannes Bergf0e72852009-03-23 17:28:36 +01004663 rcu_read_lock();
Johannes Berg2a577d92009-03-23 17:28:37 +01004664
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004665 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg176be722009-03-12 23:49:28 +01004666 for (i = 0; i < local->hw.queues; i++) {
Johannes Berg2a577d92009-03-23 17:28:37 +01004667 /*
4668 * If queue is stopped by something other than due to pending
4669 * frames, or we have no pending frames, proceed to next queue.
4670 */
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004671 if (local->queue_stop_reasons[i] ||
Johannes Berg2a577d92009-03-23 17:28:37 +01004672 skb_queue_empty(&local->pending[i]))
Johannes Berge2ebc742007-07-27 15:43:22 +02004673 continue;
Johannes Berge2530082008-05-17 00:57:14 +02004674
Johannes Berg2a577d92009-03-23 17:28:37 +01004675 while (!skb_queue_empty(&local->pending[i])) {
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004676 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
Johannes Berg5061b0c2009-07-14 00:33:34 +02004677 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg5061b0c2009-07-14 00:33:34 +02004678
Johannes Berga7bc3762009-07-27 10:33:31 +02004679 if (WARN_ON(!info->control.vif)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02004680 ieee80211_free_txskb(&local->hw, skb);
Johannes Berga7bc3762009-07-27 10:33:31 +02004681 continue;
4682 }
4683
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004684 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
4685 flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01004686
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004687 txok = ieee80211_tx_pending_skb(local, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004688 spin_lock_irqsave(&local->queue_stop_reason_lock,
4689 flags);
4690 if (!txok)
Johannes Berg2a577d92009-03-23 17:28:37 +01004691 break;
Johannes Berge2ebc742007-07-27 15:43:22 +02004692 }
Johannes Berg7236fe22010-03-22 13:42:43 -07004693
4694 if (skb_queue_empty(&local->pending[i]))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02004695 ieee80211_propagate_queue_wake(local, i);
Johannes Berge2ebc742007-07-27 15:43:22 +02004696 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004697 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01004698
Johannes Bergf0e72852009-03-23 17:28:36 +01004699 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004700}
4701
4702/* functions for drivers to get certain frames */
4703
Marco Porscheac70c12013-01-07 16:04:50 +01004704static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004705 struct ps_data *ps, struct sk_buff *skb,
4706 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02004707{
4708 u8 *pos, *tim;
4709 int aid0 = 0;
4710 int i, have_bits = 0, n1, n2;
4711
4712 /* Generate bitmap for TIM only if there are any STAs in power save
4713 * mode. */
Marco Porschd012a602012-10-10 12:39:50 -07004714 if (atomic_read(&ps->num_sta_ps) > 0)
Johannes Berge2ebc742007-07-27 15:43:22 +02004715 /* in the hope that this is faster than
4716 * checking byte-for-byte */
Weilong Chenf359d3f2013-12-18 15:44:16 +08004717 have_bits = !bitmap_empty((unsigned long *)ps->tim,
Johannes Berge2ebc742007-07-27 15:43:22 +02004718 IEEE80211_MAX_AID+1);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004719 if (!is_template) {
4720 if (ps->dtim_count == 0)
4721 ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
4722 else
4723 ps->dtim_count--;
4724 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004725
Johannes Berg4df864c2017-06-16 14:29:21 +02004726 tim = pos = skb_put(skb, 6);
Johannes Berge2ebc742007-07-27 15:43:22 +02004727 *pos++ = WLAN_EID_TIM;
4728 *pos++ = 4;
Marco Porschd012a602012-10-10 12:39:50 -07004729 *pos++ = ps->dtim_count;
Johannes Berg88600202012-02-13 15:17:18 +01004730 *pos++ = sdata->vif.bss_conf.dtim_period;
Johannes Berge2ebc742007-07-27 15:43:22 +02004731
Marco Porschd012a602012-10-10 12:39:50 -07004732 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
Johannes Berge2ebc742007-07-27 15:43:22 +02004733 aid0 = 1;
4734
Marco Porschd012a602012-10-10 12:39:50 -07004735 ps->dtim_bc_mc = aid0 == 1;
Christian Lamparter512119b2011-01-31 20:48:44 +02004736
Johannes Berge2ebc742007-07-27 15:43:22 +02004737 if (have_bits) {
4738 /* Find largest even number N1 so that bits numbered 1 through
4739 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
4740 * (N2 + 1) x 8 through 2007 are 0. */
4741 n1 = 0;
4742 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
Marco Porschd012a602012-10-10 12:39:50 -07004743 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004744 n1 = i & 0xfe;
4745 break;
4746 }
4747 }
4748 n2 = n1;
4749 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
Marco Porschd012a602012-10-10 12:39:50 -07004750 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004751 n2 = i;
4752 break;
4753 }
4754 }
4755
4756 /* Bitmap control */
4757 *pos++ = n1 | aid0;
4758 /* Part Virt Bitmap */
Eliad Peller5220da32011-11-24 16:50:00 +02004759 skb_put(skb, n2 - n1);
Marco Porschd012a602012-10-10 12:39:50 -07004760 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
Johannes Berge2ebc742007-07-27 15:43:22 +02004761
4762 tim[1] = n2 - n1 + 4;
Johannes Berge2ebc742007-07-27 15:43:22 +02004763 } else {
4764 *pos++ = aid0; /* Bitmap control */
4765 *pos++ = 0; /* Part Virt Bitmap */
4766 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004767}
4768
Marco Porscheac70c12013-01-07 16:04:50 +01004769static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004770 struct ps_data *ps, struct sk_buff *skb,
4771 bool is_template)
Marco Porscheac70c12013-01-07 16:04:50 +01004772{
4773 struct ieee80211_local *local = sdata->local;
4774
4775 /*
4776 * Not very nice, but we want to allow the driver to call
4777 * ieee80211_beacon_get() as a response to the set_tim()
4778 * callback. That, however, is already invoked under the
4779 * sta_lock to guarantee consistent and race-free update
4780 * of the tim bitmap in mac80211 and the driver.
4781 */
4782 if (local->tim_in_locked_section) {
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004783 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Marco Porscheac70c12013-01-07 16:04:50 +01004784 } else {
Johannes Berg1b917312013-02-22 12:55:01 +01004785 spin_lock_bh(&local->tim_lock);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004786 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Johannes Berg1b917312013-02-22 12:55:01 +01004787 spin_unlock_bh(&local->tim_lock);
Marco Porscheac70c12013-01-07 16:04:50 +01004788 }
4789
4790 return 0;
4791}
4792
John Crispin8552a432020-08-11 10:01:04 +02004793static void ieee80211_set_beacon_cntdwn(struct ieee80211_sub_if_data *sdata,
4794 struct beacon_data *beacon)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004795{
John Crispin5f9404a2021-07-02 19:44:08 +02004796 u8 *beacon_data, count, max_count = 1;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004797 struct probe_resp *resp;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004798 size_t beacon_data_len;
John Crispin5f9404a2021-07-02 19:44:08 +02004799 u16 *bcn_offsets;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004800 int i;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004801
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004802 switch (sdata->vif.type) {
4803 case NL80211_IFTYPE_AP:
4804 beacon_data = beacon->tail;
4805 beacon_data_len = beacon->tail_len;
4806 break;
4807 case NL80211_IFTYPE_ADHOC:
4808 beacon_data = beacon->head;
4809 beacon_data_len = beacon->head_len;
4810 break;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004811 case NL80211_IFTYPE_MESH_POINT:
4812 beacon_data = beacon->head;
4813 beacon_data_len = beacon->head_len;
4814 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004815 default:
4816 return;
4817 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004818
Michal Kazioraf296bd2014-06-05 14:21:36 +02004819 rcu_read_lock();
John Crispin5f9404a2021-07-02 19:44:08 +02004820 resp = rcu_dereference(sdata->u.ap.probe_resp);
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004821
John Crispin5f9404a2021-07-02 19:44:08 +02004822 bcn_offsets = beacon->cntdwn_counter_offsets;
4823 count = beacon->cntdwn_current_counter;
4824 if (sdata->vif.csa_active)
4825 max_count = IEEE80211_MAX_CNTDWN_COUNTERS_NUM;
4826
4827 for (i = 0; i < max_count; ++i) {
4828 if (bcn_offsets[i]) {
4829 if (WARN_ON_ONCE(bcn_offsets[i] >= beacon_data_len)) {
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004830 rcu_read_unlock();
4831 return;
4832 }
John Crispin5f9404a2021-07-02 19:44:08 +02004833 beacon_data[bcn_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004834 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004835
John Crispin5f9404a2021-07-02 19:44:08 +02004836 if (sdata->vif.type == NL80211_IFTYPE_AP && resp) {
4837 u16 *resp_offsets = resp->cntdwn_counter_offsets;
4838
4839 resp->data[resp_offsets[i]] = count;
4840 }
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004841 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004842 rcu_read_unlock();
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004843}
4844
John Crispin8552a432020-08-11 10:01:04 +02004845static u8 __ieee80211_beacon_update_cntdwn(struct beacon_data *beacon)
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004846{
John Crispin8552a432020-08-11 10:01:04 +02004847 beacon->cntdwn_current_counter--;
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004848
4849 /* the counter should never reach 0 */
John Crispin8552a432020-08-11 10:01:04 +02004850 WARN_ON_ONCE(!beacon->cntdwn_current_counter);
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004851
John Crispin8552a432020-08-11 10:01:04 +02004852 return beacon->cntdwn_current_counter;
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004853}
4854
John Crispin8552a432020-08-11 10:01:04 +02004855u8 ieee80211_beacon_update_cntdwn(struct ieee80211_vif *vif)
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004856{
4857 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004858 struct beacon_data *beacon = NULL;
4859 u8 count = 0;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004860
Michal Kazioraf296bd2014-06-05 14:21:36 +02004861 rcu_read_lock();
4862
4863 if (sdata->vif.type == NL80211_IFTYPE_AP)
4864 beacon = rcu_dereference(sdata->u.ap.beacon);
4865 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4866 beacon = rcu_dereference(sdata->u.ibss.presp);
4867 else if (ieee80211_vif_is_mesh(&sdata->vif))
4868 beacon = rcu_dereference(sdata->u.mesh.beacon);
4869
4870 if (!beacon)
4871 goto unlock;
4872
John Crispin8552a432020-08-11 10:01:04 +02004873 count = __ieee80211_beacon_update_cntdwn(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004874
Michal Kazioraf296bd2014-06-05 14:21:36 +02004875unlock:
4876 rcu_read_unlock();
4877 return count;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004878}
John Crispin8552a432020-08-11 10:01:04 +02004879EXPORT_SYMBOL(ieee80211_beacon_update_cntdwn);
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004880
John Crispin8552a432020-08-11 10:01:04 +02004881void ieee80211_beacon_set_cntdwn(struct ieee80211_vif *vif, u8 counter)
Gregory Greenman03737002018-04-20 13:49:24 +03004882{
4883 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4884 struct beacon_data *beacon = NULL;
4885
4886 rcu_read_lock();
4887
4888 if (sdata->vif.type == NL80211_IFTYPE_AP)
4889 beacon = rcu_dereference(sdata->u.ap.beacon);
4890 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4891 beacon = rcu_dereference(sdata->u.ibss.presp);
4892 else if (ieee80211_vif_is_mesh(&sdata->vif))
4893 beacon = rcu_dereference(sdata->u.mesh.beacon);
4894
4895 if (!beacon)
4896 goto unlock;
4897
John Crispin8552a432020-08-11 10:01:04 +02004898 if (counter < beacon->cntdwn_current_counter)
4899 beacon->cntdwn_current_counter = counter;
Gregory Greenman03737002018-04-20 13:49:24 +03004900
4901unlock:
4902 rcu_read_unlock();
4903}
John Crispin8552a432020-08-11 10:01:04 +02004904EXPORT_SYMBOL(ieee80211_beacon_set_cntdwn);
Gregory Greenman03737002018-04-20 13:49:24 +03004905
John Crispin8552a432020-08-11 10:01:04 +02004906bool ieee80211_beacon_cntdwn_is_complete(struct ieee80211_vif *vif)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004907{
4908 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4909 struct beacon_data *beacon = NULL;
4910 u8 *beacon_data;
4911 size_t beacon_data_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004912 int ret = false;
4913
4914 if (!ieee80211_sdata_running(sdata))
4915 return false;
4916
4917 rcu_read_lock();
4918 if (vif->type == NL80211_IFTYPE_AP) {
4919 struct ieee80211_if_ap *ap = &sdata->u.ap;
4920
4921 beacon = rcu_dereference(ap->beacon);
4922 if (WARN_ON(!beacon || !beacon->tail))
4923 goto out;
4924 beacon_data = beacon->tail;
4925 beacon_data_len = beacon->tail_len;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004926 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
4927 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4928
4929 beacon = rcu_dereference(ifibss->presp);
4930 if (!beacon)
4931 goto out;
4932
4933 beacon_data = beacon->head;
4934 beacon_data_len = beacon->head_len;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004935 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
4936 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4937
4938 beacon = rcu_dereference(ifmsh->beacon);
4939 if (!beacon)
4940 goto out;
4941
4942 beacon_data = beacon->head;
4943 beacon_data_len = beacon->head_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004944 } else {
4945 WARN_ON(1);
4946 goto out;
4947 }
4948
John Crispin8552a432020-08-11 10:01:04 +02004949 if (!beacon->cntdwn_counter_offsets[0])
Michal Kazior10d78f22014-06-05 14:21:37 +02004950 goto out;
4951
John Crispin8552a432020-08-11 10:01:04 +02004952 if (WARN_ON_ONCE(beacon->cntdwn_counter_offsets[0] > beacon_data_len))
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004953 goto out;
4954
John Crispin8552a432020-08-11 10:01:04 +02004955 if (beacon_data[beacon->cntdwn_counter_offsets[0]] == 1)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004956 ret = true;
John Crispin8552a432020-08-11 10:01:04 +02004957
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004958 out:
4959 rcu_read_unlock();
4960
4961 return ret;
4962}
John Crispin8552a432020-08-11 10:01:04 +02004963EXPORT_SYMBOL(ieee80211_beacon_cntdwn_is_complete);
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004964
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004965static int ieee80211_beacon_protect(struct sk_buff *skb,
4966 struct ieee80211_local *local,
4967 struct ieee80211_sub_if_data *sdata)
4968{
4969 ieee80211_tx_result res;
4970 struct ieee80211_tx_data tx;
Johannes Berg95247702020-03-20 10:20:23 +01004971 struct sk_buff *check_skb;
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004972
4973 memset(&tx, 0, sizeof(tx));
4974 tx.key = rcu_dereference(sdata->default_beacon_key);
4975 if (!tx.key)
4976 return 0;
4977 tx.local = local;
4978 tx.sdata = sdata;
4979 __skb_queue_head_init(&tx.skbs);
4980 __skb_queue_tail(&tx.skbs, skb);
4981 res = ieee80211_tx_h_encrypt(&tx);
Johannes Berg95247702020-03-20 10:20:23 +01004982 check_skb = __skb_dequeue(&tx.skbs);
4983 /* we may crash after this, but it'd be a bug in crypto */
4984 WARN_ON(check_skb != skb);
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004985 if (WARN_ON_ONCE(res != TX_CONTINUE))
Johannes Berg95247702020-03-20 10:20:23 +01004986 return -EINVAL;
Jouni Malinen0a3a8432020-02-22 15:25:46 +02004987
4988 return 0;
4989}
4990
Aloka Dixita6e34fd2021-10-05 21:09:36 -07004991static void
4992ieee80211_beacon_get_finish(struct ieee80211_hw *hw,
4993 struct ieee80211_vif *vif,
4994 struct ieee80211_mutable_offsets *offs,
4995 struct beacon_data *beacon,
4996 struct sk_buff *skb,
4997 struct ieee80211_chanctx_conf *chanctx_conf,
4998 u16 csa_off_base)
4999{
5000 struct ieee80211_local *local = hw_to_local(hw);
5001 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5002 struct ieee80211_tx_info *info;
5003 enum nl80211_band band;
5004 struct ieee80211_tx_rate_control txrc;
5005
5006 /* CSA offsets */
5007 if (offs && beacon) {
5008 u16 i;
5009
5010 for (i = 0; i < IEEE80211_MAX_CNTDWN_COUNTERS_NUM; i++) {
5011 u16 csa_off = beacon->cntdwn_counter_offsets[i];
5012
5013 if (!csa_off)
5014 continue;
5015
5016 offs->cntdwn_counter_offs[i] = csa_off_base + csa_off;
5017 }
5018 }
5019
5020 band = chanctx_conf->def.chan->band;
5021 info = IEEE80211_SKB_CB(skb);
5022 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
5023 info->flags |= IEEE80211_TX_CTL_NO_ACK;
5024 info->band = band;
5025
5026 memset(&txrc, 0, sizeof(txrc));
5027 txrc.hw = hw;
5028 txrc.sband = local->hw.wiphy->bands[band];
5029 txrc.bss_conf = &sdata->vif.bss_conf;
5030 txrc.skb = skb;
5031 txrc.reported_rate.idx = -1;
5032 if (sdata->beacon_rate_set && sdata->beacon_rateidx_mask[band])
5033 txrc.rate_idx_mask = sdata->beacon_rateidx_mask[band];
5034 else
5035 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
5036 txrc.bss = true;
5037 rate_control_get_rate(sdata, NULL, &txrc);
5038
5039 info->control.vif = vif;
5040 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
5041 IEEE80211_TX_CTL_ASSIGN_SEQ |
5042 IEEE80211_TX_CTL_FIRST_FRAGMENT;
5043}
5044
5045static struct sk_buff *
5046ieee80211_beacon_get_ap(struct ieee80211_hw *hw,
5047 struct ieee80211_vif *vif,
5048 struct ieee80211_mutable_offsets *offs,
5049 bool is_template,
5050 struct beacon_data *beacon,
5051 struct ieee80211_chanctx_conf *chanctx_conf)
5052{
5053 struct ieee80211_local *local = hw_to_local(hw);
5054 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5055 struct ieee80211_if_ap *ap = &sdata->u.ap;
5056 struct sk_buff *skb = NULL;
5057 u16 csa_off_base = 0;
5058
5059 if (beacon->cntdwn_counter_offsets[0]) {
5060 if (!is_template)
5061 ieee80211_beacon_update_cntdwn(vif);
5062
5063 ieee80211_set_beacon_cntdwn(sdata, beacon);
5064 }
5065
5066 /* headroom, head length,
5067 * tail length and maximum TIM length
5068 */
5069 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
5070 beacon->tail_len + 256 +
5071 local->hw.extra_beacon_tailroom);
5072 if (!skb)
5073 return NULL;
5074
5075 skb_reserve(skb, local->tx_headroom);
5076 skb_put_data(skb, beacon->head, beacon->head_len);
5077
5078 ieee80211_beacon_add_tim(sdata, &ap->ps, skb, is_template);
5079
5080 if (offs) {
5081 offs->tim_offset = beacon->head_len;
5082 offs->tim_length = skb->len - beacon->head_len;
5083 offs->cntdwn_counter_offs[0] = beacon->cntdwn_counter_offsets[0];
5084
5085 /* for AP the csa offsets are from tail */
5086 csa_off_base = skb->len;
5087 }
5088
5089 if (beacon->tail)
5090 skb_put_data(skb, beacon->tail, beacon->tail_len);
5091
5092 if (ieee80211_beacon_protect(skb, local, sdata) < 0)
5093 return NULL;
5094
5095 ieee80211_beacon_get_finish(hw, vif, offs, beacon, skb, chanctx_conf,
5096 csa_off_base);
5097 return skb;
5098}
5099
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03005100static struct sk_buff *
5101__ieee80211_beacon_get(struct ieee80211_hw *hw,
5102 struct ieee80211_vif *vif,
5103 struct ieee80211_mutable_offsets *offs,
5104 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02005105{
5106 struct ieee80211_local *local = hw_to_local(hw);
Michal Kazioraf296bd2014-06-05 14:21:36 +02005107 struct beacon_data *beacon = NULL;
Johannes Berg9d139c82008-07-09 14:40:37 +02005108 struct sk_buff *skb = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02005109 struct ieee80211_sub_if_data *sdata = NULL;
Johannes Berg55de9082012-07-26 17:24:39 +02005110 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berg8318d782008-01-24 19:38:38 +01005111
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005112 rcu_read_lock();
Johannes Berge2ebc742007-07-27 15:43:22 +02005113
Johannes Berg32bfd352007-12-19 01:31:26 +01005114 sdata = vif_to_sdata(vif);
Johannes Berg55de9082012-07-26 17:24:39 +02005115 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berge2ebc742007-07-27 15:43:22 +02005116
Johannes Berg55de9082012-07-26 17:24:39 +02005117 if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
Felix Fietkaueb3e5542011-01-24 19:28:49 +01005118 goto out;
5119
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03005120 if (offs)
5121 memset(offs, 0, sizeof(*offs));
Johannes Bergeddcbb942009-10-29 08:30:35 +01005122
Johannes Berg05c914f2008-09-11 00:01:58 +02005123 if (sdata->vif.type == NL80211_IFTYPE_AP) {
Marco Porschd012a602012-10-10 12:39:50 -07005124 struct ieee80211_if_ap *ap = &sdata->u.ap;
Marco Porschd012a602012-10-10 12:39:50 -07005125
Michal Kazioraf296bd2014-06-05 14:21:36 +02005126 beacon = rcu_dereference(ap->beacon);
Aloka Dixita6e34fd2021-10-05 21:09:36 -07005127 if (!beacon)
Johannes Berg9d139c82008-07-09 14:40:37 +02005128 goto out;
Aloka Dixita6e34fd2021-10-05 21:09:36 -07005129
5130 skb = ieee80211_beacon_get_ap(hw, vif, offs, is_template,
5131 beacon, chanctx_conf);
Johannes Berg05c914f2008-09-11 00:01:58 +02005132 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Berg46900292009-02-15 12:44:28 +01005133 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg9d139c82008-07-09 14:40:37 +02005134 struct ieee80211_hdr *hdr;
Johannes Berg902acc72008-02-23 15:17:19 +01005135
Michal Kazioraf296bd2014-06-05 14:21:36 +02005136 beacon = rcu_dereference(ifibss->presp);
5137 if (!beacon)
Johannes Berg9d139c82008-07-09 14:40:37 +02005138 goto out;
5139
John Crispin8552a432020-08-11 10:01:04 +02005140 if (beacon->cntdwn_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03005141 if (!is_template)
John Crispin8552a432020-08-11 10:01:04 +02005142 __ieee80211_beacon_update_cntdwn(beacon);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02005143
John Crispin8552a432020-08-11 10:01:04 +02005144 ieee80211_set_beacon_cntdwn(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03005145 }
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02005146
Michal Kazioraf296bd2014-06-05 14:21:36 +02005147 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01005148 local->hw.extra_beacon_tailroom);
Johannes Berg9d139c82008-07-09 14:40:37 +02005149 if (!skb)
5150 goto out;
Johannes Bergc3ffeab2013-03-07 20:54:29 +01005151 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02005152 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02005153
5154 hdr = (struct ieee80211_hdr *) skb->data;
Harvey Harrisone7827a72008-07-15 18:44:13 -07005155 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
5156 IEEE80211_STYPE_BEACON);
Aloka Dixita6e34fd2021-10-05 21:09:36 -07005157
5158 ieee80211_beacon_get_finish(hw, vif, offs, beacon, skb,
5159 chanctx_conf, 0);
Johannes Berg902acc72008-02-23 15:17:19 +01005160 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
Javier Cardonadbf498f2012-03-31 11:31:32 -07005161 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg472dbc42008-09-11 00:01:49 +02005162
Michal Kazioraf296bd2014-06-05 14:21:36 +02005163 beacon = rcu_dereference(ifmsh->beacon);
5164 if (!beacon)
Johannes Bergac1bd842011-01-18 13:45:32 +01005165 goto out;
Johannes Bergac1bd842011-01-18 13:45:32 +01005166
John Crispin8552a432020-08-11 10:01:04 +02005167 if (beacon->cntdwn_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03005168 if (!is_template)
5169 /* TODO: For mesh csa_counter is in TU, so
5170 * decrementing it by one isn't correct, but
5171 * for now we leave it consistent with overall
5172 * mac80211's behavior.
5173 */
John Crispin8552a432020-08-11 10:01:04 +02005174 __ieee80211_beacon_update_cntdwn(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03005175
John Crispin8552a432020-08-11 10:01:04 +02005176 ieee80211_set_beacon_cntdwn(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03005177 }
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07005178
Javier Cardonadbf498f2012-03-31 11:31:32 -07005179 if (ifmsh->sync_ops)
Masashi Honma445cd452016-12-08 10:15:51 +09005180 ifmsh->sync_ops->adjust_tsf(sdata, beacon);
Javier Cardonadbf498f2012-03-31 11:31:32 -07005181
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -07005182 skb = dev_alloc_skb(local->tx_headroom +
Michal Kazioraf296bd2014-06-05 14:21:36 +02005183 beacon->head_len +
Marco Porsch3f52b7e2013-01-30 18:14:08 +01005184 256 + /* TIM IE */
Michal Kazioraf296bd2014-06-05 14:21:36 +02005185 beacon->tail_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01005186 local->hw.extra_beacon_tailroom);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01005187 if (!skb)
5188 goto out;
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08005189 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02005190 skb_put_data(skb, beacon->head, beacon->head_len);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03005191 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
5192
5193 if (offs) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02005194 offs->tim_offset = beacon->head_len;
5195 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03005196 }
5197
Johannes Berg59ae1d12017-06-16 14:29:20 +02005198 skb_put_data(skb, beacon->tail, beacon->tail_len);
Aloka Dixita6e34fd2021-10-05 21:09:36 -07005199 ieee80211_beacon_get_finish(hw, vif, offs, beacon, skb,
5200 chanctx_conf, 0);
Johannes Berg9d139c82008-07-09 14:40:37 +02005201 } else {
5202 WARN_ON(1);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005203 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02005204 }
5205
Johannes Berge6a98542008-10-21 12:40:02 +02005206 out:
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005207 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02005208 return skb;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03005209
5210}
5211
5212struct sk_buff *
5213ieee80211_beacon_get_template(struct ieee80211_hw *hw,
5214 struct ieee80211_vif *vif,
5215 struct ieee80211_mutable_offsets *offs)
5216{
5217 return __ieee80211_beacon_get(hw, vif, offs, true);
5218}
5219EXPORT_SYMBOL(ieee80211_beacon_get_template);
5220
5221struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
5222 struct ieee80211_vif *vif,
5223 u16 *tim_offset, u16 *tim_length)
5224{
5225 struct ieee80211_mutable_offsets offs = {};
5226 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
Helmut Schaa35afa582015-09-09 09:46:32 +02005227 struct sk_buff *copy;
5228 struct ieee80211_supported_band *sband;
5229 int shift;
5230
5231 if (!bcn)
5232 return bcn;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03005233
5234 if (tim_offset)
5235 *tim_offset = offs.tim_offset;
5236
5237 if (tim_length)
5238 *tim_length = offs.tim_length;
5239
Helmut Schaa35afa582015-09-09 09:46:32 +02005240 if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
5241 !hw_to_local(hw)->monitors)
5242 return bcn;
5243
5244 /* send a copy to monitor interfaces */
5245 copy = skb_copy(bcn, GFP_ATOMIC);
5246 if (!copy)
5247 return bcn;
5248
5249 shift = ieee80211_vif_get_shift(vif);
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05305250 sband = ieee80211_get_sband(vif_to_sdata(vif));
5251 if (!sband)
5252 return bcn;
5253
John Crispinb7b2e8c2019-07-14 17:44:15 +02005254 ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false,
5255 NULL);
Helmut Schaa35afa582015-09-09 09:46:32 +02005256
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03005257 return bcn;
Johannes Berge2ebc742007-07-27 15:43:22 +02005258}
Johannes Bergeddcbb942009-10-29 08:30:35 +01005259EXPORT_SYMBOL(ieee80211_beacon_get_tim);
Johannes Berge2ebc742007-07-27 15:43:22 +02005260
Arik Nemtsov02945822011-11-10 11:28:57 +02005261struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
5262 struct ieee80211_vif *vif)
5263{
5264 struct ieee80211_if_ap *ap = NULL;
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03005265 struct sk_buff *skb = NULL;
5266 struct probe_resp *presp = NULL;
Arik Nemtsov02945822011-11-10 11:28:57 +02005267 struct ieee80211_hdr *hdr;
5268 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5269
5270 if (sdata->vif.type != NL80211_IFTYPE_AP)
5271 return NULL;
5272
5273 rcu_read_lock();
5274
5275 ap = &sdata->u.ap;
5276 presp = rcu_dereference(ap->probe_resp);
5277 if (!presp)
5278 goto out;
5279
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03005280 skb = dev_alloc_skb(presp->len);
Arik Nemtsov02945822011-11-10 11:28:57 +02005281 if (!skb)
5282 goto out;
5283
Johannes Berg59ae1d12017-06-16 14:29:20 +02005284 skb_put_data(skb, presp->data, presp->len);
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03005285
Arik Nemtsov02945822011-11-10 11:28:57 +02005286 hdr = (struct ieee80211_hdr *) skb->data;
5287 memset(hdr->addr1, 0, sizeof(hdr->addr1));
5288
5289out:
5290 rcu_read_unlock();
5291 return skb;
5292}
5293EXPORT_SYMBOL(ieee80211_proberesp_get);
5294
Aloka Dixit295b02c2020-09-11 00:05:31 +00005295struct sk_buff *ieee80211_get_fils_discovery_tmpl(struct ieee80211_hw *hw,
5296 struct ieee80211_vif *vif)
5297{
5298 struct sk_buff *skb = NULL;
5299 struct fils_discovery_data *tmpl = NULL;
5300 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5301
5302 if (sdata->vif.type != NL80211_IFTYPE_AP)
5303 return NULL;
5304
5305 rcu_read_lock();
5306 tmpl = rcu_dereference(sdata->u.ap.fils_discovery);
5307 if (!tmpl) {
5308 rcu_read_unlock();
5309 return NULL;
5310 }
5311
5312 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
5313 if (skb) {
5314 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
5315 skb_put_data(skb, tmpl->data, tmpl->len);
5316 }
5317
5318 rcu_read_unlock();
5319 return skb;
5320}
5321EXPORT_SYMBOL(ieee80211_get_fils_discovery_tmpl);
5322
Aloka Dixit632189a2020-09-11 00:33:01 +00005323struct sk_buff *
5324ieee80211_get_unsol_bcast_probe_resp_tmpl(struct ieee80211_hw *hw,
5325 struct ieee80211_vif *vif)
5326{
5327 struct sk_buff *skb = NULL;
5328 struct unsol_bcast_probe_resp_data *tmpl = NULL;
5329 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5330
5331 if (sdata->vif.type != NL80211_IFTYPE_AP)
5332 return NULL;
5333
5334 rcu_read_lock();
5335 tmpl = rcu_dereference(sdata->u.ap.unsol_bcast_probe_resp);
5336 if (!tmpl) {
5337 rcu_read_unlock();
5338 return NULL;
5339 }
5340
5341 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
5342 if (skb) {
5343 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
5344 skb_put_data(skb, tmpl->data, tmpl->len);
5345 }
5346
5347 rcu_read_unlock();
5348 return skb;
5349}
5350EXPORT_SYMBOL(ieee80211_get_unsol_bcast_probe_resp_tmpl);
5351
Kalle Valo7044cc52010-01-05 20:16:19 +02005352struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
5353 struct ieee80211_vif *vif)
5354{
5355 struct ieee80211_sub_if_data *sdata;
5356 struct ieee80211_if_managed *ifmgd;
5357 struct ieee80211_pspoll *pspoll;
5358 struct ieee80211_local *local;
5359 struct sk_buff *skb;
5360
5361 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5362 return NULL;
5363
5364 sdata = vif_to_sdata(vif);
5365 ifmgd = &sdata->u.mgd;
5366 local = sdata->local;
5367
5368 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
Joe Perchesd15b8452011-08-29 14:17:31 -07005369 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02005370 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07005371
Kalle Valo7044cc52010-01-05 20:16:19 +02005372 skb_reserve(skb, local->hw.extra_tx_headroom);
5373
Johannes Bergb080db52017-06-16 14:29:19 +02005374 pspoll = skb_put_zero(skb, sizeof(*pspoll));
Kalle Valo7044cc52010-01-05 20:16:19 +02005375 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
5376 IEEE80211_STYPE_PSPOLL);
Johannes Berg1db364c2020-04-17 12:38:04 +02005377 pspoll->aid = cpu_to_le16(sdata->vif.bss_conf.aid);
Kalle Valo7044cc52010-01-05 20:16:19 +02005378
5379 /* aid in PS-Poll has its two MSBs each set to 1 */
5380 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
5381
5382 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
5383 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
5384
5385 return skb;
5386}
5387EXPORT_SYMBOL(ieee80211_pspoll_get);
5388
5389struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005390 struct ieee80211_vif *vif,
5391 bool qos_ok)
Kalle Valo7044cc52010-01-05 20:16:19 +02005392{
5393 struct ieee80211_hdr_3addr *nullfunc;
5394 struct ieee80211_sub_if_data *sdata;
5395 struct ieee80211_if_managed *ifmgd;
5396 struct ieee80211_local *local;
5397 struct sk_buff *skb;
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005398 bool qos = false;
Kalle Valo7044cc52010-01-05 20:16:19 +02005399
5400 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5401 return NULL;
5402
5403 sdata = vif_to_sdata(vif);
5404 ifmgd = &sdata->u.mgd;
5405 local = sdata->local;
5406
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005407 if (qos_ok) {
5408 struct sta_info *sta;
5409
5410 rcu_read_lock();
5411 sta = sta_info_get(sdata, ifmgd->bssid);
5412 qos = sta && sta->sta.wme;
5413 rcu_read_unlock();
5414 }
5415
5416 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5417 sizeof(*nullfunc) + 2);
Joe Perchesd15b8452011-08-29 14:17:31 -07005418 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02005419 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07005420
Kalle Valo7044cc52010-01-05 20:16:19 +02005421 skb_reserve(skb, local->hw.extra_tx_headroom);
5422
Johannes Bergb080db52017-06-16 14:29:19 +02005423 nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
Kalle Valo7044cc52010-01-05 20:16:19 +02005424 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
5425 IEEE80211_STYPE_NULLFUNC |
5426 IEEE80211_FCTL_TODS);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005427 if (qos) {
Johannes Berge0ba7092018-11-09 11:16:46 +01005428 __le16 qoshdr = cpu_to_le16(7);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005429
5430 BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
5431 IEEE80211_STYPE_NULLFUNC) !=
5432 IEEE80211_STYPE_QOS_NULLFUNC);
5433 nullfunc->frame_control |=
5434 cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
5435 skb->priority = 7;
5436 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
Johannes Berge0ba7092018-11-09 11:16:46 +01005437 skb_put_data(skb, &qoshdr, sizeof(qoshdr));
Johannes Berg7b6ddea2017-11-21 14:46:08 +01005438 }
5439
Kalle Valo7044cc52010-01-05 20:16:19 +02005440 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
5441 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
5442 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
5443
5444 return skb;
5445}
5446EXPORT_SYMBOL(ieee80211_nullfunc_get);
5447
Kalle Valo05e54ea2010-01-05 20:16:38 +02005448struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
Johannes Berga344d672014-06-12 22:24:31 +02005449 const u8 *src_addr,
Kalle Valo05e54ea2010-01-05 20:16:38 +02005450 const u8 *ssid, size_t ssid_len,
Johannes Bergb9a9ada2012-11-29 13:00:10 +01005451 size_t tailroom)
Kalle Valo05e54ea2010-01-05 20:16:38 +02005452{
Johannes Berga344d672014-06-12 22:24:31 +02005453 struct ieee80211_local *local = hw_to_local(hw);
Kalle Valo05e54ea2010-01-05 20:16:38 +02005454 struct ieee80211_hdr_3addr *hdr;
5455 struct sk_buff *skb;
5456 size_t ie_ssid_len;
5457 u8 *pos;
5458
Kalle Valo05e54ea2010-01-05 20:16:38 +02005459 ie_ssid_len = 2 + ssid_len;
5460
5461 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
Johannes Bergb9a9ada2012-11-29 13:00:10 +01005462 ie_ssid_len + tailroom);
Joe Perchesd15b8452011-08-29 14:17:31 -07005463 if (!skb)
Kalle Valo05e54ea2010-01-05 20:16:38 +02005464 return NULL;
Kalle Valo05e54ea2010-01-05 20:16:38 +02005465
5466 skb_reserve(skb, local->hw.extra_tx_headroom);
5467
Johannes Bergb080db52017-06-16 14:29:19 +02005468 hdr = skb_put_zero(skb, sizeof(*hdr));
Kalle Valo05e54ea2010-01-05 20:16:38 +02005469 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
5470 IEEE80211_STYPE_PROBE_REQ);
Johannes Berge83e6542012-07-13 16:23:07 +02005471 eth_broadcast_addr(hdr->addr1);
Johannes Berga344d672014-06-12 22:24:31 +02005472 memcpy(hdr->addr2, src_addr, ETH_ALEN);
Johannes Berge83e6542012-07-13 16:23:07 +02005473 eth_broadcast_addr(hdr->addr3);
Kalle Valo05e54ea2010-01-05 20:16:38 +02005474
5475 pos = skb_put(skb, ie_ssid_len);
5476 *pos++ = WLAN_EID_SSID;
5477 *pos++ = ssid_len;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02005478 if (ssid_len)
Kalle Valo05e54ea2010-01-05 20:16:38 +02005479 memcpy(pos, ssid, ssid_len);
5480 pos += ssid_len;
5481
Kalle Valo05e54ea2010-01-05 20:16:38 +02005482 return skb;
5483}
5484EXPORT_SYMBOL(ieee80211_probereq_get);
5485
Johannes Berg32bfd352007-12-19 01:31:26 +01005486void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02005487 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02005488 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02005489 struct ieee80211_rts *rts)
5490{
5491 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02005492
Harvey Harrison065e96052008-06-22 16:45:27 -07005493 rts->frame_control =
5494 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01005495 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
5496 frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02005497 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
5498 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
5499}
5500EXPORT_SYMBOL(ieee80211_rts_get);
5501
Johannes Berg32bfd352007-12-19 01:31:26 +01005502void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02005503 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02005504 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02005505 struct ieee80211_cts *cts)
5506{
5507 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02005508
Harvey Harrison065e96052008-06-22 16:45:27 -07005509 cts->frame_control =
5510 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01005511 cts->duration = ieee80211_ctstoself_duration(hw, vif,
5512 frame_len, frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02005513 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
5514}
5515EXPORT_SYMBOL(ieee80211_ctstoself_get);
5516
5517struct sk_buff *
Johannes Berg32bfd352007-12-19 01:31:26 +01005518ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
Johannes Berge039fa42008-05-15 12:55:29 +02005519 struct ieee80211_vif *vif)
Johannes Berge2ebc742007-07-27 15:43:22 +02005520{
5521 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winkler747cf5e2008-05-27 17:50:51 +03005522 struct sk_buff *skb = NULL;
Johannes Berg5cf121c2008-02-25 16:27:43 +01005523 struct ieee80211_tx_data tx;
Johannes Berge2ebc742007-07-27 15:43:22 +02005524 struct ieee80211_sub_if_data *sdata;
Marco Porschd012a602012-10-10 12:39:50 -07005525 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +02005526 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02005527 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berge2ebc742007-07-27 15:43:22 +02005528
Johannes Berg32bfd352007-12-19 01:31:26 +01005529 sdata = vif_to_sdata(vif);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005530
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005531 rcu_read_lock();
Johannes Berg55de9082012-07-26 17:24:39 +02005532 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005533
Marco Porschd012a602012-10-10 12:39:50 -07005534 if (!chanctx_conf)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03005535 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01005536
Marco Porschd012a602012-10-10 12:39:50 -07005537 if (sdata->vif.type == NL80211_IFTYPE_AP) {
5538 struct beacon_data *beacon =
5539 rcu_dereference(sdata->u.ap.beacon);
5540
5541 if (!beacon || !beacon->head)
5542 goto out;
5543
5544 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01005545 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
5546 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07005547 } else {
5548 goto out;
5549 }
5550
5551 if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03005552 goto out; /* send buffered bc/mc only after DTIM beacon */
Johannes Berge039fa42008-05-15 12:55:29 +02005553
Johannes Berge2ebc742007-07-27 15:43:22 +02005554 while (1) {
Marco Porschd012a602012-10-10 12:39:50 -07005555 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +02005556 if (!skb)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03005557 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02005558 local->total_ps_buffered--;
5559
Marco Porschd012a602012-10-10 12:39:50 -07005560 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
Johannes Berge2ebc742007-07-27 15:43:22 +02005561 struct ieee80211_hdr *hdr =
5562 (struct ieee80211_hdr *) skb->data;
5563 /* more buffered multicast/broadcast frames ==> set
5564 * MoreData flag in IEEE 802.11 header to inform PS
5565 * STAs */
5566 hdr->frame_control |=
5567 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
5568 }
5569
Michael Braun112c44b2014-03-06 15:08:43 +01005570 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porsch7cbf9d02013-03-01 16:01:18 +01005571 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
Johannes Berg7c107702015-03-20 14:18:27 +01005572 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
Johannes Berge2ebc742007-07-27 15:43:22 +02005573 break;
Felix Fietkau6b07d9c2016-08-02 11:13:41 +02005574 ieee80211_free_txskb(hw, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02005575 }
Johannes Berge039fa42008-05-15 12:55:29 +02005576
5577 info = IEEE80211_SKB_CB(skb);
5578
Johannes Berg5cf121c2008-02-25 16:27:43 +01005579 tx.flags |= IEEE80211_TX_PS_BUFFERED;
Johannes Berg4bf88532012-11-09 11:39:59 +01005580 info->band = chanctx_conf->def.chan->band;
Johannes Berge2ebc742007-07-27 15:43:22 +02005581
Johannes Berg97b045d2008-06-20 01:22:30 +02005582 if (invoke_tx_handlers(&tx))
Johannes Berge2ebc742007-07-27 15:43:22 +02005583 skb = NULL;
Johannes Berg97b045d2008-06-20 01:22:30 +02005584 out:
Johannes Bergd0709a62008-02-25 16:27:46 +01005585 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02005586
5587 return skb;
5588}
5589EXPORT_SYMBOL(ieee80211_get_buffered_bc);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005590
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005591int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5592{
5593 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5594 struct ieee80211_sub_if_data *sdata = sta->sdata;
5595 struct ieee80211_local *local = sdata->local;
5596 int ret;
5597 u32 queues;
5598
5599 lockdep_assert_held(&local->sta_mtx);
5600
5601 /* only some cases are supported right now */
5602 switch (sdata->vif.type) {
5603 case NL80211_IFTYPE_STATION:
5604 case NL80211_IFTYPE_AP:
5605 case NL80211_IFTYPE_AP_VLAN:
5606 break;
5607 default:
5608 WARN_ON(1);
5609 return -EINVAL;
5610 }
5611
5612 if (WARN_ON(tid >= IEEE80211_NUM_UPS))
5613 return -EINVAL;
5614
5615 if (sta->reserved_tid == tid) {
5616 ret = 0;
5617 goto out;
5618 }
5619
5620 if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
5621 sdata_err(sdata, "TID reservation already active\n");
5622 ret = -EALREADY;
5623 goto out;
5624 }
5625
5626 ieee80211_stop_vif_queues(sdata->local, sdata,
5627 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5628
5629 synchronize_net();
5630
5631 /* Tear down BA sessions so we stop aggregating on this TID */
Johannes Berg30686bf2015-06-02 21:39:54 +02005632 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005633 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5634 __ieee80211_stop_tx_ba_session(sta, tid,
5635 AGG_STOP_LOCAL_REQUEST);
5636 }
5637
5638 queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02005639 __ieee80211_flush_queues(local, sdata, queues, false);
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005640
5641 sta->reserved_tid = tid;
5642
5643 ieee80211_wake_vif_queues(local, sdata,
5644 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5645
Johannes Berg30686bf2015-06-02 21:39:54 +02005646 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
Liad Kaufmanb6da9112014-11-19 13:47:38 +02005647 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5648
5649 ret = 0;
5650 out:
5651 return ret;
5652}
5653EXPORT_SYMBOL(ieee80211_reserve_tid);
5654
5655void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5656{
5657 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5658 struct ieee80211_sub_if_data *sdata = sta->sdata;
5659
5660 lockdep_assert_held(&sdata->local->sta_mtx);
5661
5662 /* only some cases are supported right now */
5663 switch (sdata->vif.type) {
5664 case NL80211_IFTYPE_STATION:
5665 case NL80211_IFTYPE_AP:
5666 case NL80211_IFTYPE_AP_VLAN:
5667 break;
5668 default:
5669 WARN_ON(1);
5670 return;
5671 }
5672
5673 if (tid != sta->reserved_tid) {
5674 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
5675 return;
5676 }
5677
5678 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
5679}
5680EXPORT_SYMBOL(ieee80211_unreserve_tid);
5681
Johannes Berg55de9082012-07-26 17:24:39 +02005682void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
5683 struct sk_buff *skb, int tid,
Mathy Vanhoef08aca292020-07-23 14:01:52 +04005684 enum nl80211_band band)
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005685{
Amadeusz Sławiński731977e2017-01-24 16:42:10 +01005686 int ac = ieee80211_ac_from_tid(tid);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02005687
Zhang Shengjud57a5442016-03-03 01:16:56 +00005688 skb_reset_mac_header(skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02005689 skb_set_queue_mapping(skb, ac);
Helmut Schaacf6bb792011-12-15 10:18:34 +01005690 skb->priority = tid;
Johannes Bergcf0277e2010-01-05 18:00:58 +01005691
Johannes Berg89afe612013-02-13 15:39:57 +01005692 skb->dev = sdata->dev;
5693
Johannes Berg3d34deb2009-06-18 17:25:11 +02005694 /*
5695 * The other path calling ieee80211_xmit is from the tasklet,
5696 * and while we can handle concurrent transmissions locking
5697 * requirements are that we do not come into tx with bhs on.
5698 */
5699 local_bh_disable();
Johannes Berg73c4e192014-11-09 18:50:09 +02005700 IEEE80211_SKB_CB(skb)->band = band;
Mathy Vanhoef08aca292020-07-23 14:01:52 +04005701 ieee80211_xmit(sdata, NULL, skb);
Johannes Berg3d34deb2009-06-18 17:25:11 +02005702 local_bh_enable();
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005703}
Denis Kenzior91180642018-03-26 12:52:50 -05005704
5705int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
5706 const u8 *buf, size_t len,
Markus Theildca9ca22020-05-08 16:42:00 +02005707 const u8 *dest, __be16 proto, bool unencrypted,
5708 u64 *cookie)
Denis Kenzior91180642018-03-26 12:52:50 -05005709{
5710 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5711 struct ieee80211_local *local = sdata->local;
Markus Theil10cb8e62021-02-06 12:51:12 +01005712 struct sta_info *sta;
Denis Kenzior91180642018-03-26 12:52:50 -05005713 struct sk_buff *skb;
5714 struct ethhdr *ehdr;
Johannes Bergb95d2cc2020-03-26 15:53:34 +01005715 u32 ctrl_flags = 0;
Markus Theila7528192020-05-27 18:03:34 +02005716 u32 flags = 0;
Denis Kenzior91180642018-03-26 12:52:50 -05005717
5718 /* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
5719 * or Pre-Authentication
5720 */
5721 if (proto != sdata->control_port_protocol &&
5722 proto != cpu_to_be16(ETH_P_PREAUTH))
5723 return -EINVAL;
5724
Johannes Bergb95d2cc2020-03-26 15:53:34 +01005725 if (proto == sdata->control_port_protocol)
Markus Theil5af7fef2020-06-17 10:26:36 +02005726 ctrl_flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO |
5727 IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
Johannes Bergb95d2cc2020-03-26 15:53:34 +01005728
Denis Kenzior91180642018-03-26 12:52:50 -05005729 if (unencrypted)
Markus Theila7528192020-05-27 18:03:34 +02005730 flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
5731
5732 if (cookie)
5733 ctrl_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
5734
Markus Theil10cb8e62021-02-06 12:51:12 +01005735 flags |= IEEE80211_TX_INTFL_NL80211_FRAME_TX;
Denis Kenzior91180642018-03-26 12:52:50 -05005736
5737 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5738 sizeof(struct ethhdr) + len);
5739 if (!skb)
5740 return -ENOMEM;
5741
5742 skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
5743
5744 skb_put_data(skb, buf, len);
5745
5746 ehdr = skb_push(skb, sizeof(struct ethhdr));
5747 memcpy(ehdr->h_dest, dest, ETH_ALEN);
Johannes Berg8079e4f2020-02-24 10:19:11 +01005748 memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
Denis Kenzior91180642018-03-26 12:52:50 -05005749 ehdr->h_proto = proto;
5750
5751 skb->dev = dev;
Markus Theil10cb8e62021-02-06 12:51:12 +01005752 skb->protocol = proto;
Denis Kenzior91180642018-03-26 12:52:50 -05005753 skb_reset_network_header(skb);
5754 skb_reset_mac_header(skb);
5755
Markus Theil10cb8e62021-02-06 12:51:12 +01005756 /* update QoS header to prioritize control port frames if possible,
5757 * priorization also happens for control port frames send over
5758 * AF_PACKET
5759 */
5760 rcu_read_lock();
5761
5762 if (ieee80211_lookup_ra_sta(sdata, skb, &sta) == 0 && !IS_ERR(sta)) {
5763 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
5764
5765 skb_set_queue_mapping(skb, queue);
5766 skb_get_hash(skb);
5767 }
5768
5769 rcu_read_unlock();
5770
Markus Theila7528192020-05-27 18:03:34 +02005771 /* mutex lock is only needed for incrementing the cookie counter */
5772 mutex_lock(&local->mtx);
5773
Denis Kenziore7441c92018-06-19 10:39:50 -05005774 local_bh_disable();
Markus Theila7528192020-05-27 18:03:34 +02005775 __ieee80211_subif_start_xmit(skb, skb->dev, flags, ctrl_flags, cookie);
Denis Kenziore7441c92018-06-19 10:39:50 -05005776 local_bh_enable();
Denis Kenzior91180642018-03-26 12:52:50 -05005777
Markus Theila7528192020-05-27 18:03:34 +02005778 mutex_unlock(&local->mtx);
5779
Denis Kenzior91180642018-03-26 12:52:50 -05005780 return 0;
5781}
Rajkumar Manoharan8828f812019-04-11 13:47:26 -07005782
5783int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
5784 const u8 *buf, size_t len)
5785{
5786 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5787 struct ieee80211_local *local = sdata->local;
5788 struct sk_buff *skb;
5789
5790 skb = dev_alloc_skb(local->hw.extra_tx_headroom + len +
5791 30 + /* header size */
5792 18); /* 11s header size */
5793 if (!skb)
5794 return -ENOMEM;
5795
5796 skb_reserve(skb, local->hw.extra_tx_headroom);
5797 skb_put_data(skb, buf, len);
5798
5799 skb->dev = dev;
5800 skb->protocol = htons(ETH_P_802_3);
5801 skb_reset_network_header(skb);
5802 skb_reset_mac_header(skb);
5803
5804 local_bh_disable();
5805 __ieee80211_subif_start_xmit(skb, skb->dev, 0,
Markus Theila7528192020-05-27 18:03:34 +02005806 IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP,
5807 NULL);
Johannes Berge2ebc742007-07-27 15:43:22 +02005808 local_bh_enable();
5809
5810 return 0;
5811}