blob: 938c10f7955bcb4516d493df6e783518c8fb3ba6 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Johannes Berge2ebc742007-07-27 15:43:22 +02002/*
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
6 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
Johannes Bergd98ad832014-09-03 15:24:57 +03007 * Copyright 2013-2014 Intel Mobile Communications GmbH
Sara Sharon914eac22018-04-20 13:49:19 +03008 * Copyright (C) 2018 Intel Corporation
Johannes Berge2ebc742007-07-27 15:43:22 +02009 *
Johannes Berge2ebc742007-07-27 15:43:22 +020010 * Transmit and frame generation functions.
11 */
12
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/skbuff.h>
Michael Braunebceec82016-11-22 11:52:18 +010016#include <linux/if_vlan.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020017#include <linux/etherdevice.h>
18#include <linux/bitmap.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040019#include <linux/rcupdate.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040020#include <linux/export.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070021#include <net/net_namespace.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020022#include <net/ieee80211_radiotap.h>
23#include <net/cfg80211.h>
24#include <net/mac80211.h>
Michal Kazior5caa3282016-05-19 10:37:51 +020025#include <net/codel.h>
26#include <net/codel_impl.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020027#include <asm/unaligned.h>
Michal Kaziorfa962b92016-05-19 10:37:49 +020028#include <net/fq_impl.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020029
30#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020031#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040032#include "led.h"
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +010033#include "mesh.h"
Johannes Berge2ebc742007-07-27 15:43:22 +020034#include "wep.h"
35#include "wpa.h"
36#include "wme.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040037#include "rate.h"
Johannes Berge2ebc742007-07-27 15:43:22 +020038
Johannes Berge2ebc742007-07-27 15:43:22 +020039/* misc utils */
40
Johannes Berg5a490512015-04-22 17:10:38 +020041static inline void ieee80211_tx_stats(struct net_device *dev, u32 len)
42{
43 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
44
45 u64_stats_update_begin(&tstats->syncp);
46 tstats->tx_packets++;
47 tstats->tx_bytes += len;
48 u64_stats_update_end(&tstats->syncp);
49}
50
Johannes Berg252b86c2011-11-16 15:28:55 +010051static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
52 struct sk_buff *skb, int group_addr,
Johannes Berg03f93c32008-06-25 14:36:27 +030053 int next_frag_len)
Johannes Berge2ebc742007-07-27 15:43:22 +020054{
Simon Wunderlich2103dec2013-07-08 16:55:53 +020055 int rate, mrate, erp, dur, i, shift = 0;
Johannes Berg2e92e6f2008-05-15 12:55:27 +020056 struct ieee80211_rate *txrate;
Johannes Berge2ebc742007-07-27 15:43:22 +020057 struct ieee80211_local *local = tx->local;
Johannes Berg8318d782008-01-24 19:38:38 +010058 struct ieee80211_supported_band *sband;
Harvey Harrison358c8d92008-07-15 18:44:13 -070059 struct ieee80211_hdr *hdr;
Johannes Berg252b86c2011-11-16 15:28:55 +010060 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Simon Wunderlich2103dec2013-07-08 16:55:53 +020061 struct ieee80211_chanctx_conf *chanctx_conf;
62 u32 rate_flags = 0;
63
Felix Fietkau95cd4702016-12-14 20:46:57 +010064 /* assume HW handles this */
65 if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))
66 return 0;
67
Simon Wunderlich2103dec2013-07-08 16:55:53 +020068 rcu_read_lock();
69 chanctx_conf = rcu_dereference(tx->sdata->vif.chanctx_conf);
70 if (chanctx_conf) {
71 shift = ieee80211_chandef_get_shift(&chanctx_conf->def);
72 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
73 }
74 rcu_read_unlock();
Johannes Berge6a98542008-10-21 12:40:02 +020075
Johannes Berge6a98542008-10-21 12:40:02 +020076 /* uh huh? */
Felix Fietkau0d528d82013-04-22 16:14:41 +020077 if (WARN_ON_ONCE(tx->rate.idx < 0))
Johannes Berge6a98542008-10-21 12:40:02 +020078 return 0;
Johannes Berge2ebc742007-07-27 15:43:22 +020079
Johannes Berg2d565772012-07-23 15:12:51 +020080 sband = local->hw.wiphy->bands[info->band];
Felix Fietkau0d528d82013-04-22 16:14:41 +020081 txrate = &sband->bitrates[tx->rate.idx];
Johannes Berg8318d782008-01-24 19:38:38 +010082
Johannes Berge6a98542008-10-21 12:40:02 +020083 erp = txrate->flags & IEEE80211_RATE_ERP_G;
Johannes Berge2ebc742007-07-27 15:43:22 +020084
85 /*
86 * data and mgmt (except PS Poll):
87 * - during CFP: 32768
88 * - during contention period:
89 * if addr1 is group address: 0
90 * if more fragments = 0 and addr1 is individual address: time to
91 * transmit one ACK plus SIFS
92 * if more fragments = 1 and addr1 is individual address: time to
93 * transmit next fragment plus 2 x ACK plus 3 x SIFS
94 *
95 * IEEE 802.11, 9.6:
96 * - control response frame (CTS or ACK) shall be transmitted using the
97 * same rate as the immediately previous frame in the frame exchange
98 * sequence, if this rate belongs to the PHY mandatory rates, or else
99 * at the highest possible rate belonging to the PHY rates in the
100 * BSSBasicRateSet
101 */
Johannes Berg252b86c2011-11-16 15:28:55 +0100102 hdr = (struct ieee80211_hdr *)skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -0700103 if (ieee80211_is_ctl(hdr->frame_control)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200104 /* TODO: These control frames are not currently sent by
Johannes Bergccd7b362008-09-11 00:01:56 +0200105 * mac80211, but should they be implemented, this function
Johannes Berge2ebc742007-07-27 15:43:22 +0200106 * needs to be updated to support duration field calculation.
107 *
108 * RTS: time needed to transmit pending data/mgmt frame plus
109 * one CTS frame plus one ACK frame plus 3 x SIFS
110 * CTS: duration of immediately previous RTS minus time
111 * required to transmit CTS and its SIFS
112 * ACK: 0 if immediately previous directed data/mgmt had
113 * more=0, with more=1 duration in ACK frame is duration
114 * from previous frame minus time needed to transmit ACK
115 * and its SIFS
116 * PS Poll: BIT(15) | BIT(14) | aid
117 */
118 return 0;
119 }
120
121 /* data/mgmt */
122 if (0 /* FIX: data/mgmt during CFP */)
Johannes Berg03f93c32008-06-25 14:36:27 +0300123 return cpu_to_le16(32768);
Johannes Berge2ebc742007-07-27 15:43:22 +0200124
125 if (group_addr) /* Group address as the destination - no ACK */
126 return 0;
127
128 /* Individual destination address:
129 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
130 * CTS and ACK frames shall be transmitted using the highest rate in
131 * basic rate set that is less than or equal to the rate of the
132 * immediately previous frame and that is using the same modulation
133 * (CCK or OFDM). If no basic rate set matches with these requirements,
134 * the highest mandatory rate of the PHY that is less than or equal to
135 * the rate of the previous frame is used.
136 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
137 */
138 rate = -1;
Johannes Berg8318d782008-01-24 19:38:38 +0100139 /* use lowest available if everything fails */
140 mrate = sband->bitrates[0].bitrate;
141 for (i = 0; i < sband->n_bitrates; i++) {
142 struct ieee80211_rate *r = &sband->bitrates[i];
143
144 if (r->bitrate > txrate->bitrate)
Johannes Berge2ebc742007-07-27 15:43:22 +0200145 break;
146
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200147 if ((rate_flags & r->flags) != rate_flags)
148 continue;
149
Johannes Bergbda39332008-10-11 01:51:51 +0200150 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200151 rate = DIV_ROUND_UP(r->bitrate, 1 << shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200152
Johannes Berg8318d782008-01-24 19:38:38 +0100153 switch (sband->band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200154 case NL80211_BAND_2GHZ: {
Johannes Berg8318d782008-01-24 19:38:38 +0100155 u32 flag;
156 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
157 flag = IEEE80211_RATE_MANDATORY_G;
158 else
159 flag = IEEE80211_RATE_MANDATORY_B;
160 if (r->flags & flag)
161 mrate = r->bitrate;
162 break;
163 }
Johannes Berg57fbcce2016-04-12 15:56:15 +0200164 case NL80211_BAND_5GHZ:
Arend van Sprielc5b9a7f2019-08-02 13:30:58 +0200165 case NL80211_BAND_6GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +0100166 if (r->flags & IEEE80211_RATE_MANDATORY_A)
167 mrate = r->bitrate;
168 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200169 case NL80211_BAND_60GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300170 /* TODO, for now fall through */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200171 case NUM_NL80211_BANDS:
Johannes Berg8318d782008-01-24 19:38:38 +0100172 WARN_ON(1);
173 break;
174 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200175 }
176 if (rate == -1) {
177 /* No matching basic rate found; use highest suitable mandatory
178 * PHY rate */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200179 rate = DIV_ROUND_UP(mrate, 1 << shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200180 }
181
Simon Wunderlich6674f212011-11-21 21:34:30 +0100182 /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
183 if (ieee80211_is_data_qos(hdr->frame_control) &&
Claudio Pisac26a0e12012-05-28 13:06:25 +0100184 *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
Simon Wunderlich6674f212011-11-21 21:34:30 +0100185 dur = 0;
186 else
187 /* Time needed to transmit ACK
188 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
189 * to closest integer */
Michal Kazior4ee73f32012-04-11 08:47:56 +0200190 dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200191 tx->sdata->vif.bss_conf.use_short_preamble,
192 shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200193
194 if (next_frag_len) {
195 /* Frame is fragmented: duration increases with time needed to
196 * transmit next fragment plus ACK and 2 x SIFS. */
197 dur *= 2; /* ACK + SIFS */
198 /* next fragment */
Michal Kazior4ee73f32012-04-11 08:47:56 +0200199 dur += ieee80211_frame_duration(sband->band, next_frag_len,
Johannes Berg8318d782008-01-24 19:38:38 +0100200 txrate->bitrate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200201 tx->sdata->vif.bss_conf.use_short_preamble,
202 shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200203 }
204
Johannes Berg03f93c32008-06-25 14:36:27 +0300205 return cpu_to_le16(dur);
Johannes Berge2ebc742007-07-27 15:43:22 +0200206}
207
Johannes Berge2ebc742007-07-27 15:43:22 +0200208/* tx handlers */
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200209static ieee80211_tx_result debug_noinline
210ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
211{
212 struct ieee80211_local *local = tx->local;
Kalle Valo0c742112010-01-12 10:42:53 +0200213 struct ieee80211_if_managed *ifmgd;
Andrei Otcheretianski94a5b3a2018-09-05 08:06:14 +0300214 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200215
216 /* driver doesn't support power save */
Johannes Berg30686bf2015-06-02 21:39:54 +0200217 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200218 return TX_CONTINUE;
219
220 /* hardware does dynamic power save */
Johannes Berg30686bf2015-06-02 21:39:54 +0200221 if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200222 return TX_CONTINUE;
223
224 /* dynamic power save disabled */
225 if (local->hw.conf.dynamic_ps_timeout <= 0)
226 return TX_CONTINUE;
227
228 /* we are scanning, don't enable power save */
229 if (local->scanning)
230 return TX_CONTINUE;
231
232 if (!local->ps_sdata)
233 return TX_CONTINUE;
234
235 /* No point if we're going to suspend */
236 if (local->quiescing)
237 return TX_CONTINUE;
238
Kalle Valo0c742112010-01-12 10:42:53 +0200239 /* dynamic ps is supported only in managed mode */
240 if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
241 return TX_CONTINUE;
242
Andrei Otcheretianski94a5b3a2018-09-05 08:06:14 +0300243 if (unlikely(info->flags & IEEE80211_TX_INTFL_OFFCHAN_TX_OK))
244 return TX_CONTINUE;
245
Kalle Valo0c742112010-01-12 10:42:53 +0200246 ifmgd = &tx->sdata->u.mgd;
247
248 /*
249 * Don't wakeup from power save if u-apsd is enabled, voip ac has
250 * u-apsd enabled and the frame is in voip class. This effectively
251 * means that even if all access categories have u-apsd enabled, in
252 * practise u-apsd is only used with the voip ac. This is a
253 * workaround for the case when received voip class packets do not
254 * have correct qos tag for some reason, due the network or the
255 * peer application.
256 *
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200257 * Note: ifmgd->uapsd_queues access is racy here. If the value is
Kalle Valo0c742112010-01-12 10:42:53 +0200258 * changed via debugfs, user needs to reassociate manually to have
259 * everything in sync.
260 */
Johannes Berg4875d30d2012-03-27 14:18:37 +0200261 if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
262 (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
263 skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
Kalle Valo0c742112010-01-12 10:42:53 +0200264 return TX_CONTINUE;
265
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200266 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
267 ieee80211_stop_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +0100268 IEEE80211_MAX_QUEUE_MAP,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300269 IEEE80211_QUEUE_STOP_REASON_PS,
270 false);
Vivek Natarajandb285692011-02-18 17:18:03 +0530271 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200272 ieee80211_queue_work(&local->hw,
273 &local->dynamic_ps_disable_work);
274 }
275
Luciano Coelho5db1c072011-05-03 21:40:08 +0300276 /* Don't restart the timer if we're not disassociated */
277 if (!ifmgd->associated)
278 return TX_CONTINUE;
279
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200280 mod_timer(&local->dynamic_ps_timer, jiffies +
281 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
282
283 return TX_CONTINUE;
284}
Johannes Berge2ebc742007-07-27 15:43:22 +0200285
Johannes Bergd9e8a702008-06-30 15:10:44 +0200286static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100287ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200288{
Harvey Harrison358c8d92008-07-15 18:44:13 -0700289
Johannes Berge039fa42008-05-15 12:55:29 +0200290 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200291 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200292 bool assoc = false;
Johannes Berge2ebc742007-07-27 15:43:22 +0200293
Johannes Berge039fa42008-05-15 12:55:29 +0200294 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100295 return TX_CONTINUE;
Johannes Berg58d41852007-09-26 17:53:18 +0200296
Ben Greearb23b0252011-02-04 11:54:17 -0800297 if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
298 test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
Kalle Valoa9a6fff2009-03-18 14:06:44 +0200299 !ieee80211_is_probe_req(hdr->frame_control) &&
300 !ieee80211_is_nullfunc(hdr->frame_control))
301 /*
302 * When software scanning only nullfunc frames (to notify
303 * the sleep state to the AP) and probe requests (for the
304 * active scan) are allowed, all other frames should not be
305 * sent and we should not get here, but if we do
306 * nonetheless, drop them to avoid sending them
307 * off-channel. See the link below and
308 * ieee80211_start_scan() for more.
309 *
310 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
311 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100312 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200313
Rostislav Lisovy239281f2014-11-03 10:33:19 +0100314 if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
315 return TX_CONTINUE;
316
Bill Jordan1be7fe82010-10-01 11:20:41 -0400317 if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
318 return TX_CONTINUE;
319
Johannes Berg5cf121c2008-02-25 16:27:43 +0100320 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100321 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200322
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200323 if (tx->sta)
324 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge2ebc742007-07-27 15:43:22 +0200325
Johannes Berg5cf121c2008-02-25 16:27:43 +0100326 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200327 if (unlikely(!assoc &&
Harvey Harrison358c8d92008-07-15 18:44:13 -0700328 ieee80211_is_data(hdr->frame_control))) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200329#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200330 sdata_info(tx->sdata,
331 "dropped data frame to not associated station %pM\n",
332 hdr->addr1);
333#endif
Johannes Berge2ebc742007-07-27 15:43:22 +0200334 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100335 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200336 }
Michael Braun72f15d52016-10-10 19:12:21 +0200337 } else if (unlikely(ieee80211_is_data(hdr->frame_control) &&
338 ieee80211_vif_get_num_mcast_if(tx->sdata) == 0)) {
Johannes Berg29623892011-12-14 12:20:31 +0100339 /*
340 * No associated STAs - no need to send multicast
341 * frames.
342 */
343 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200344 }
345
Johannes Berg9ae54c82008-01-31 19:48:20 +0100346 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200347}
348
Johannes Berge2ebc742007-07-27 15:43:22 +0200349/* This function is called whenever the AP is about to exceed the maximum limit
350 * of buffered frames for power saving STAs. This situation should not really
351 * happen often during normal operation, so dropping the oldest buffered packet
352 * from each queue should be OK to make some room for new frames. */
353static void purge_old_ps_buffers(struct ieee80211_local *local)
354{
355 int total = 0, purged = 0;
356 struct sk_buff *skb;
357 struct ieee80211_sub_if_data *sdata;
358 struct sta_info *sta;
359
Johannes Berg79010422007-09-18 17:29:21 -0400360 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Marco Porschd012a602012-10-10 12:39:50 -0700361 struct ps_data *ps;
362
363 if (sdata->vif.type == NL80211_IFTYPE_AP)
364 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100365 else if (ieee80211_vif_is_mesh(&sdata->vif))
366 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700367 else
Johannes Berge2ebc742007-07-27 15:43:22 +0200368 continue;
Marco Porschd012a602012-10-10 12:39:50 -0700369
370 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +0200371 if (skb) {
372 purged++;
Felix Fietkau6b07d9c2016-08-02 11:13:41 +0200373 ieee80211_free_txskb(&local->hw, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +0200374 }
Marco Porschd012a602012-10-10 12:39:50 -0700375 total += skb_queue_len(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +0200376 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200377
Johannes Berg948d8872011-09-29 16:04:29 +0200378 /*
379 * Drop one frame from each station from the lowest-priority
380 * AC that has frames at all.
381 */
Johannes Bergd0709a62008-02-25 16:27:46 +0100382 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg948d8872011-09-29 16:04:29 +0200383 int ac;
384
385 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
386 skb = skb_dequeue(&sta->ps_tx_buf[ac]);
387 total += skb_queue_len(&sta->ps_tx_buf[ac]);
388 if (skb) {
389 purged++;
Felix Fietkauc3e77242012-10-08 14:39:33 +0200390 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg948d8872011-09-29 16:04:29 +0200391 break;
392 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200393 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200394 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100395
Johannes Berge2ebc742007-07-27 15:43:22 +0200396 local->total_ps_buffered = total;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200397 ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
Johannes Berge2ebc742007-07-27 15:43:22 +0200398}
399
Johannes Berg9ae54c82008-01-31 19:48:20 +0100400static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100401ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200402{
Johannes Berge039fa42008-05-15 12:55:29 +0200403 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700404 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Marco Porschd012a602012-10-10 12:39:50 -0700405 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +0200406
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100407 /*
408 * broadcast/multicast frame
409 *
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100410 * If any of the associated/peer stations is in power save mode,
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100411 * the frame is buffered to be sent after DTIM beacon frame.
412 * This is done either by the hardware or us.
413 */
414
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100415 /* powersaving STAs currently only in AP/VLAN/mesh mode */
Marco Porschd012a602012-10-10 12:39:50 -0700416 if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
417 tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
418 if (!tx->sdata->bss)
419 return TX_CONTINUE;
420
421 ps = &tx->sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100422 } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
423 ps = &tx->sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700424 } else {
Johannes Berg3e122be2008-07-09 14:40:34 +0200425 return TX_CONTINUE;
Marco Porschd012a602012-10-10 12:39:50 -0700426 }
427
Johannes Berg3e122be2008-07-09 14:40:34 +0200428
429 /* no buffering for ordered frames */
Harvey Harrison358c8d92008-07-15 18:44:13 -0700430 if (ieee80211_has_order(hdr->frame_control))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100431 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100432
Johannes Berg08b99392014-07-07 12:01:11 +0200433 if (ieee80211_is_probe_req(hdr->frame_control))
434 return TX_CONTINUE;
435
Johannes Berg30686bf2015-06-02 21:39:54 +0200436 if (ieee80211_hw_check(&tx->local->hw, QUEUE_CONTROL))
Johannes Bergf4d57942013-05-28 17:24:15 +0200437 info->hw_queue = tx->sdata->vif.cab_queue;
438
Felix Fietkau9ec11902018-11-28 22:39:16 +0100439 /* no stations in PS mode and no buffered packets */
440 if (!atomic_read(&ps->num_sta_ps) && skb_queue_empty(&ps->bc_buf))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100441 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100442
Johannes Berg62b517c2009-10-29 12:19:21 +0100443 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
Johannes Berg62b12082009-08-10 16:04:15 +0200444
Johannes Berg62b517c2009-10-29 12:19:21 +0100445 /* device releases frame after DTIM beacon */
Johannes Berg30686bf2015-06-02 21:39:54 +0200446 if (!ieee80211_hw_check(&tx->local->hw, HOST_BROADCAST_PS_BUFFERING))
Johannes Berg62b12082009-08-10 16:04:15 +0200447 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200448
Johannes Berg62b12082009-08-10 16:04:15 +0200449 /* buffered in mac80211 */
450 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
451 purge_old_ps_buffers(tx->local);
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100452
Marco Porschd012a602012-10-10 12:39:50 -0700453 if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200454 ps_dbg(tx->sdata,
455 "BC TX buffer full - dropping the oldest frame\n");
Felix Fietkau6b07d9c2016-08-02 11:13:41 +0200456 ieee80211_free_txskb(&tx->local->hw, skb_dequeue(&ps->bc_buf));
Johannes Berg62b12082009-08-10 16:04:15 +0200457 } else
458 tx->local->total_ps_buffered++;
459
Marco Porschd012a602012-10-10 12:39:50 -0700460 skb_queue_tail(&ps->bc_buf, tx->skb);
Johannes Berg62b12082009-08-10 16:04:15 +0200461
462 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200463}
464
Jouni Malinenfb733332009-01-08 13:32:00 +0200465static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
466 struct sk_buff *skb)
467{
468 if (!ieee80211_is_mgmt(fc))
469 return 0;
470
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200471 if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
Jouni Malinenfb733332009-01-08 13:32:00 +0200472 return 0;
473
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100474 if (!ieee80211_is_robust_mgmt_frame(skb))
Jouni Malinenfb733332009-01-08 13:32:00 +0200475 return 0;
476
477 return 1;
478}
479
Johannes Berg9ae54c82008-01-31 19:48:20 +0100480static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100481ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200482{
483 struct sta_info *sta = tx->sta;
Johannes Berge039fa42008-05-15 12:55:29 +0200484 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berg08b99392014-07-07 12:01:11 +0200485 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300486 struct ieee80211_local *local = tx->local;
Johannes Berge2ebc742007-07-27 15:43:22 +0200487
Johannes Berg02f2f1a2012-02-27 12:18:30 +0100488 if (unlikely(!sta))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100489 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200490
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200491 if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
Johannes Berg5ac2e352014-05-27 16:32:27 +0200492 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
493 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
Johannes Berg02f2f1a2012-02-27 12:18:30 +0100494 !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
Johannes Berg948d8872011-09-29 16:04:29 +0200495 int ac = skb_get_queue_mapping(tx->skb);
496
Johannes Berg08b99392014-07-07 12:01:11 +0200497 if (ieee80211_is_mgmt(hdr->frame_control) &&
498 !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
499 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
500 return TX_CONTINUE;
501 }
502
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200503 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
504 sta->sta.addr, sta->sta.aid, ac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200505 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
506 purge_old_ps_buffers(tx->local);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200507
508 /* sync with ieee80211_sta_ps_deliver_wakeup */
509 spin_lock(&sta->ps_lock);
510 /*
511 * STA woke up the meantime and all the frames on ps_tx_buf have
512 * been queued to pending queue. No reordering can happen, go
513 * ahead and Tx the packet.
514 */
515 if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
Johannes Berg5ac2e352014-05-27 16:32:27 +0200516 !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
517 !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200518 spin_unlock(&sta->ps_lock);
519 return TX_CONTINUE;
520 }
521
Johannes Berg948d8872011-09-29 16:04:29 +0200522 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
523 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200524 ps_dbg(tx->sdata,
525 "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
526 sta->sta.addr, ac);
Felix Fietkauc3e77242012-10-08 14:39:33 +0200527 ieee80211_free_txskb(&local->hw, old);
Johannes Berge2ebc742007-07-27 15:43:22 +0200528 } else
529 tx->local->total_ps_buffered++;
Johannes Berg004c8722008-02-20 11:21:35 +0100530
Johannes Berge039fa42008-05-15 12:55:29 +0200531 info->control.jiffies = jiffies;
Johannes Berg5061b0c2009-07-14 00:33:34 +0200532 info->control.vif = &tx->sdata->vif;
Johannes Berg8f77f382009-06-07 21:58:37 +0200533 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Johannes Berg03c8c062014-01-09 01:45:28 +0100534 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berg948d8872011-09-29 16:04:29 +0200535 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200536 spin_unlock(&sta->ps_lock);
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300537
538 if (!timer_pending(&local->sta_cleanup))
539 mod_timer(&local->sta_cleanup,
540 round_jiffies(jiffies +
541 STA_INFO_CLEANUP_INTERVAL));
542
Johannes Bergc868cb352011-09-29 16:04:27 +0200543 /*
544 * We queued up some frames, so the TIM bit might
545 * need to be set, recalculate it.
546 */
547 sta_info_recalc_tim(sta);
548
Johannes Berg9ae54c82008-01-31 19:48:20 +0100549 return TX_QUEUED;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200550 } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
551 ps_dbg(tx->sdata,
552 "STA %pM in PS mode, but polling/in SP -> send frame\n",
553 sta->sta.addr);
Johannes Berge2ebc742007-07-27 15:43:22 +0200554 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200555
Johannes Berg9ae54c82008-01-31 19:48:20 +0100556 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200557}
558
Johannes Bergd9e8a702008-06-30 15:10:44 +0200559static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100560ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200561{
Johannes Berg5cf121c2008-02-25 16:27:43 +0100562 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100563 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200564
Johannes Berg5cf121c2008-02-25 16:27:43 +0100565 if (tx->flags & IEEE80211_TX_UNICAST)
Johannes Berge2ebc742007-07-27 15:43:22 +0200566 return ieee80211_tx_h_unicast_ps_buf(tx);
567 else
568 return ieee80211_tx_h_multicast_ps_buf(tx);
569}
570
Johannes Bergd9e8a702008-06-30 15:10:44 +0200571static ieee80211_tx_result debug_noinline
Johannes Berga621fa42010-08-27 14:26:54 +0300572ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
573{
574 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
575
Johannes Bergaf61a162013-07-02 18:09:12 +0200576 if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
577 if (tx->sdata->control_port_no_encrypt)
578 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
579 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
Jouni Malinen9c1c98a2015-02-26 15:50:50 +0200580 info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
Johannes Bergaf61a162013-07-02 18:09:12 +0200581 }
Johannes Berga621fa42010-08-27 14:26:54 +0300582
583 return TX_CONTINUE;
584}
585
586static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100587ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200588{
Johannes Berg46e6de12012-07-04 18:10:07 +0200589 struct ieee80211_key *key;
Johannes Berge039fa42008-05-15 12:55:29 +0200590 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700591 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400592
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200593 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
Johannes Berge2ebc742007-07-27 15:43:22 +0200594 tx->key = NULL;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200595 else if (tx->sta &&
596 (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
Johannes Bergd4e46a32007-09-14 11:10:24 -0400597 tx->key = key;
Masashi Honma46f6b062016-06-22 19:55:20 +0900598 else if (ieee80211_is_group_privacy_action(tx->skb) &&
599 (key = rcu_dereference(tx->sdata->default_multicast_key)))
600 tx->key = key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200601 else if (ieee80211_is_mgmt(hdr->frame_control) &&
Jouni Malinenecbcd322010-03-29 23:35:23 -0700602 is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100603 ieee80211_is_robust_mgmt_frame(tx->skb) &&
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200604 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
605 tx->key = key;
Johannes Bergf7e01042010-12-09 19:49:02 +0100606 else if (is_multicast_ether_addr(hdr->addr1) &&
607 (key = rcu_dereference(tx->sdata->default_multicast_key)))
608 tx->key = key;
609 else if (!is_multicast_ether_addr(hdr->addr1) &&
610 (key = rcu_dereference(tx->sdata->default_unicast_key)))
Johannes Bergd4e46a32007-09-14 11:10:24 -0400611 tx->key = key;
Johannes Berge8f4fb72015-03-20 11:37:36 +0100612 else
Johannes Berg46e6de12012-07-04 18:10:07 +0200613 tx->key = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +0200614
615 if (tx->key) {
Johannes Berg813d7662010-01-17 01:47:58 +0100616 bool skip_hw = false;
617
Johannes Berg011bfcc2007-09-17 01:29:25 -0400618 /* TODO: add threshold stuff again */
Johannes Berg176e4f82007-12-18 15:27:47 +0100619
Johannes Berg97359d12010-08-10 09:46:38 +0200620 switch (tx->key->conf.cipher) {
621 case WLAN_CIPHER_SUITE_WEP40:
622 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg97359d12010-08-10 09:46:38 +0200623 case WLAN_CIPHER_SUITE_TKIP:
Harvey Harrison358c8d92008-07-15 18:44:13 -0700624 if (!ieee80211_is_data_present(hdr->frame_control))
Johannes Berg176e4f82007-12-18 15:27:47 +0100625 tx->key = NULL;
626 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200627 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200628 case WLAN_CIPHER_SUITE_CCMP_256:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200629 case WLAN_CIPHER_SUITE_GCMP:
630 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinenfb733332009-01-08 13:32:00 +0200631 if (!ieee80211_is_data_present(hdr->frame_control) &&
632 !ieee80211_use_mfp(hdr->frame_control, tx->sta,
Masashi Honma46f6b062016-06-22 19:55:20 +0900633 tx->skb) &&
634 !ieee80211_is_group_privacy_action(tx->skb))
Jouni Malinenfb733332009-01-08 13:32:00 +0200635 tx->key = NULL;
Kalle Valo3b43a182010-01-23 20:27:14 +0200636 else
637 skip_hw = (tx->key->conf.flags &
Johannes Berge548c492012-09-04 17:08:23 +0200638 IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
Kalle Valo3b43a182010-01-23 20:27:14 +0200639 ieee80211_is_mgmt(hdr->frame_control);
Jouni Malinenfb733332009-01-08 13:32:00 +0200640 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200641 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200642 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Jouni Malinen8ade5382015-01-24 19:52:09 +0200643 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
644 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200645 if (!ieee80211_is_mgmt(hdr->frame_control))
646 tx->key = NULL;
647 break;
Johannes Berg176e4f82007-12-18 15:27:47 +0100648 }
Johannes Berg813d7662010-01-17 01:47:58 +0100649
Johannes Berge54faf22013-01-29 11:41:38 +0100650 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
651 !ieee80211_is_deauth(hdr->frame_control)))
Johannes Berg95acac62011-07-12 12:30:59 +0200652 return TX_DROP;
653
Johannes Bergf12553e2010-01-22 22:07:59 +0100654 if (!skip_hw && tx->key &&
Johannes Berg382b1652010-01-25 11:36:16 +0100655 tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
Johannes Berg813d7662010-01-17 01:47:58 +0100656 info->control.hw_key = &tx->key->conf;
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;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200671 bool assoc = false;
Johannes Berge6a98542008-10-21 12:40:02 +0200672
673 memset(&txrc, 0, sizeof(txrc));
Johannes Berg8318d782008-01-24 19:38:38 +0100674
Johannes Berg2d565772012-07-23 15:12:51 +0200675 sband = tx->local->hw.wiphy->bands[info->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200676
Shanyu Zhaoa2c40242010-04-27 11:15:12 -0700677 len = min_t(u32, tx->skb->len + FCS_LEN,
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200678 tx->local->hw.wiphy->frag_threshold);
Johannes Berg58d41852007-09-26 17:53:18 +0200679
Johannes Berge6a98542008-10-21 12:40:02 +0200680 /* set up the tx rate control struct we give the RC algo */
Johannes Berg005e4722012-02-26 11:24:35 +0100681 txrc.hw = &tx->local->hw;
Johannes Berge6a98542008-10-21 12:40:02 +0200682 txrc.sband = sband;
683 txrc.bss_conf = &tx->sdata->vif.bss_conf;
684 txrc.skb = tx->skb;
685 txrc.reported_rate.idx = -1;
Johannes Berg2d565772012-07-23 15:12:51 +0200686 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
Felix Fietkau2ffbe6d2013-04-16 13:38:42 +0200687
688 if (tx->sdata->rc_has_mcs_mask[info->band])
689 txrc.rate_idx_mcs_mask =
690 tx->sdata->rc_rateidx_mcs_mask[info->band];
691
Felix Fietkau8f0729b2010-11-11 15:07:23 +0100692 txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -0800693 tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
Bertold Van den Bergh5765f9f2015-08-05 16:02:28 +0200694 tx->sdata->vif.type == NL80211_IFTYPE_ADHOC ||
695 tx->sdata->vif.type == NL80211_IFTYPE_OCB);
Johannes Berg58d41852007-09-26 17:53:18 +0200696
Johannes Berge6a98542008-10-21 12:40:02 +0200697 /* set up RTS protection if desired */
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200698 if (len > tx->local->hw.wiphy->rts_threshold) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200699 txrc.rts = true;
Johannes Berge2ebc742007-07-27 15:43:22 +0200700 }
Johannes Berge6a98542008-10-21 12:40:02 +0200701
Felix Fietkau0d528d82013-04-22 16:14:41 +0200702 info->control.use_rts = txrc.rts;
Felix Fietkau991fec02013-04-16 13:38:43 +0200703 info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
704
Johannes Berge6a98542008-10-21 12:40:02 +0200705 /*
706 * Use short preamble if the BSS can handle it, but not for
707 * management frames unless we know the receiver can handle
708 * that -- the management frame might be to a station that
709 * just wants a probe response.
710 */
711 if (tx->sdata->vif.bss_conf.use_short_preamble &&
712 (ieee80211_is_data(hdr->frame_control) ||
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200713 (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
Felix Fietkau0d528d82013-04-22 16:14:41 +0200714 txrc.short_preamble = true;
715
716 info->control.short_preamble = txrc.short_preamble;
Johannes Berge6a98542008-10-21 12:40:02 +0200717
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +0100718 /* don't ask rate control when rate already injected via radiotap */
719 if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)
720 return TX_CONTINUE;
721
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200722 if (tx->sta)
723 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge6a98542008-10-21 12:40:02 +0200724
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700725 /*
726 * Lets not bother rate control if we're associated and cannot
727 * talk to the sta. This should not happen.
728 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200729 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700730 !rate_usable_index_exists(sband, &tx->sta->sta),
731 "%s: Dropped data frame as no usable bitrate found while "
732 "scanning and associated. Target station: "
733 "%pM on %d GHz band\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100734 tx->sdata->name, hdr->addr1,
Johannes Berg2d565772012-07-23 15:12:51 +0200735 info->band ? 5 : 2))
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700736 return TX_DROP;
737
738 /*
739 * If we're associated with the sta at this point we know we can at
740 * least send the frame at the lowest bit rate.
741 */
Johannes Berge6a98542008-10-21 12:40:02 +0200742 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
743
Felix Fietkau0d528d82013-04-22 16:14:41 +0200744 if (tx->sta && !info->control.skip_table)
745 ratetbl = rcu_dereference(tx->sta->sta.rates);
746
747 if (unlikely(info->control.rates[0].idx < 0)) {
748 if (ratetbl) {
749 struct ieee80211_tx_rate rate = {
750 .idx = ratetbl->rate[0].idx,
751 .flags = ratetbl->rate[0].flags,
752 .count = ratetbl->rate[0].count
753 };
754
755 if (ratetbl->rate[0].idx < 0)
756 return TX_DROP;
757
758 tx->rate = rate;
759 } else {
760 return TX_DROP;
761 }
762 } else {
763 tx->rate = info->control.rates[0];
764 }
Johannes Berge6a98542008-10-21 12:40:02 +0200765
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100766 if (txrc.reported_rate.idx < 0) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200767 txrc.reported_rate = tx->rate;
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100768 if (tx->sta && ieee80211_is_data(hdr->frame_control))
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200769 tx->sta->tx_stats.last_rate = txrc.reported_rate;
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100770 } else if (tx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200771 tx->sta->tx_stats.last_rate = txrc.reported_rate;
Johannes Berge6a98542008-10-21 12:40:02 +0200772
Felix Fietkau0d528d82013-04-22 16:14:41 +0200773 if (ratetbl)
774 return TX_CONTINUE;
775
Johannes Berge6a98542008-10-21 12:40:02 +0200776 if (unlikely(!info->control.rates[0].count))
777 info->control.rates[0].count = 1;
778
Gábor Stefanik99551512009-04-23 19:36:14 +0200779 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
780 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
781 info->control.rates[0].count = 1;
782
Johannes Berg9ae54c82008-01-31 19:48:20 +0100783 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200784}
785
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100786static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
787{
788 u16 *seq = &sta->tid_seq[tid];
789 __le16 ret = cpu_to_le16(*seq);
790
791 /* Increase the sequence number. */
792 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
793
794 return ret;
795}
796
Johannes Bergd9e8a702008-06-30 15:10:44 +0200797static ieee80211_tx_result debug_noinline
Johannes Bergf591fa52008-07-10 11:21:26 +0200798ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
799{
800 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
801 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergf591fa52008-07-10 11:21:26 +0200802 int tid;
803
Johannes Berg25d834e2008-09-12 22:52:47 +0200804 /*
805 * Packet injection may want to control the sequence
806 * number, if we have no matching interface then we
807 * neither assign one ourselves nor ask the driver to.
808 */
Johannes Berg5061b0c2009-07-14 00:33:34 +0200809 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
Johannes Berg25d834e2008-09-12 22:52:47 +0200810 return TX_CONTINUE;
811
Johannes Bergf591fa52008-07-10 11:21:26 +0200812 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
813 return TX_CONTINUE;
814
815 if (ieee80211_hdrlen(hdr->frame_control) < 24)
816 return TX_CONTINUE;
817
Johannes Berg49a59542011-09-29 16:04:41 +0200818 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
819 return TX_CONTINUE;
820
Johannes Berg94778282008-10-10 13:21:59 +0200821 /*
822 * Anything but QoS data that has a sequence number field
823 * (is long enough) gets a sequence number from the global
Bob Copelandc4c205f2013-08-23 09:35:38 -0400824 * counter. QoS data frames with a multicast destination
825 * also use the global counter (802.11-2012 9.3.2.10).
Johannes Berg94778282008-10-10 13:21:59 +0200826 */
Bob Copelandc4c205f2013-08-23 09:35:38 -0400827 if (!ieee80211_is_data_qos(hdr->frame_control) ||
828 is_multicast_ether_addr(hdr->addr1)) {
Johannes Bergb9771d42018-05-28 15:47:41 +0200829 if (tx->flags & IEEE80211_TX_NO_SEQNO)
830 return TX_CONTINUE;
Johannes Berg94778282008-10-10 13:21:59 +0200831 /* driver should assign sequence number */
Johannes Bergf591fa52008-07-10 11:21:26 +0200832 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
Johannes Berg94778282008-10-10 13:21:59 +0200833 /* for pure STA mode without beacons, we can do it */
834 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
835 tx->sdata->sequence_number += 0x10;
Johannes Berg79c892b2014-11-21 14:26:31 +0100836 if (tx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200837 tx->sta->tx_stats.msdu[IEEE80211_NUM_TIDS]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200838 return TX_CONTINUE;
839 }
840
841 /*
842 * This should be true for injected/management frames only, for
843 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
844 * above since they are not QoS-data frames.
845 */
846 if (!tx->sta)
847 return TX_CONTINUE;
848
849 /* include per-STA, per-TID sequence counter */
Sara Sharona1f2ba04c2018-02-19 14:48:40 +0200850 tid = ieee80211_get_tid(hdr);
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200851 tx->sta->tx_stats.msdu[tid]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200852
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +0200853 hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
Johannes Bergf591fa52008-07-10 11:21:26 +0200854
855 return TX_CONTINUE;
856}
857
Johannes Berg252b86c2011-11-16 15:28:55 +0100858static int ieee80211_fragment(struct ieee80211_tx_data *tx,
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100859 struct sk_buff *skb, int hdrlen,
860 int frag_threshold)
861{
Johannes Berg252b86c2011-11-16 15:28:55 +0100862 struct ieee80211_local *local = tx->local;
Johannes Berga1a3fce2011-11-16 15:28:56 +0100863 struct ieee80211_tx_info *info;
Johannes Berg252b86c2011-11-16 15:28:55 +0100864 struct sk_buff *tmp;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100865 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
866 int pos = hdrlen + per_fragm;
867 int rem = skb->len - hdrlen - per_fragm;
868
869 if (WARN_ON(rem < 0))
870 return -EINVAL;
871
Johannes Berg252b86c2011-11-16 15:28:55 +0100872 /* first fragment was already added to queue by caller */
873
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100874 while (rem) {
875 int fraglen = per_fragm;
876
877 if (fraglen > rem)
878 fraglen = rem;
879 rem -= fraglen;
880 tmp = dev_alloc_skb(local->tx_headroom +
881 frag_threshold +
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200882 tx->sdata->encrypt_headroom +
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100883 IEEE80211_ENCRYPT_TAILROOM);
884 if (!tmp)
885 return -ENOMEM;
Johannes Berg252b86c2011-11-16 15:28:55 +0100886
887 __skb_queue_tail(&tx->skbs, tmp);
888
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200889 skb_reserve(tmp,
890 local->tx_headroom + tx->sdata->encrypt_headroom);
891
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100892 /* copy control information */
893 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
Johannes Berga1a3fce2011-11-16 15:28:56 +0100894
895 info = IEEE80211_SKB_CB(tmp);
896 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
897 IEEE80211_TX_CTL_FIRST_FRAGMENT);
898
899 if (rem)
900 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
901
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100902 skb_copy_queue_mapping(tmp, skb);
903 tmp->priority = skb->priority;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100904 tmp->dev = skb->dev;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100905
906 /* copy header and data */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200907 skb_put_data(tmp, skb->data, hdrlen);
908 skb_put_data(tmp, skb->data + pos, fraglen);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100909
910 pos += fraglen;
911 }
912
Johannes Berg252b86c2011-11-16 15:28:55 +0100913 /* adjust first fragment's length */
Johannes Berg338f9772014-02-01 00:16:23 +0100914 skb_trim(skb, hdrlen + per_fragm);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100915 return 0;
916}
917
Johannes Bergf591fa52008-07-10 11:21:26 +0200918static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +0200919ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
920{
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100921 struct sk_buff *skb = tx->skb;
922 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
923 struct ieee80211_hdr *hdr = (void *)skb->data;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200924 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100925 int hdrlen;
926 int fragnum;
Johannes Berge2454942008-05-15 12:55:28 +0200927
Johannes Berg252b86c2011-11-16 15:28:55 +0100928 /* no matter what happens, tx->skb moves to tx->skbs */
929 __skb_queue_tail(&tx->skbs, skb);
930 tx->skb = NULL;
931
Johannes Berga26eb272011-10-07 14:01:25 +0200932 if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
933 return TX_CONTINUE;
934
Sara Sharonf3fe4e92016-10-18 23:12:11 +0300935 if (ieee80211_hw_check(&tx->local->hw, SUPPORTS_TX_FRAG))
Johannes Berge2454942008-05-15 12:55:28 +0200936 return TX_CONTINUE;
937
Johannes Bergeefce912008-05-17 00:57:13 +0200938 /*
939 * Warn when submitting a fragmented A-MPDU frame and drop it.
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200940 * This scenario is handled in ieee80211_tx_prepare but extra
Ron Rindjunsky8d5e0d52008-06-12 15:42:29 +0300941 * caution taken here as fragmented ampdu may cause Tx stop.
Johannes Bergeefce912008-05-17 00:57:13 +0200942 */
Sujith8b30b1f2008-10-24 09:55:27 +0530943 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
Johannes Bergeefce912008-05-17 00:57:13 +0200944 return TX_DROP;
945
Harvey Harrison065e96052008-06-22 16:45:27 -0700946 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berge2454942008-05-15 12:55:28 +0200947
Johannes Berga26eb272011-10-07 14:01:25 +0200948 /* internal error, why isn't DONTFRAG set? */
Johannes Berg8ccd8f22009-04-29 23:35:56 +0200949 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100950 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200951
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100952 /*
953 * Now fragment the frame. This will allocate all the fragments and
954 * chain them (using skb as the first fragment) to skb->next.
955 * During transmission, we will remove the successfully transmitted
956 * fragments from this list. When the low-level driver rejects one
957 * of the fragments then we will simply pretend to accept the skb
958 * but store it away as pending.
959 */
Johannes Berg252b86c2011-11-16 15:28:55 +0100960 if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100961 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200962
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100963 /* update duration/seq/flags of fragments */
964 fragnum = 0;
Johannes Berg252b86c2011-11-16 15:28:55 +0100965
966 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100967 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
Johannes Berge2454942008-05-15 12:55:28 +0200968
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100969 hdr = (void *)skb->data;
970 info = IEEE80211_SKB_CB(skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200971
Johannes Berg252b86c2011-11-16 15:28:55 +0100972 if (!skb_queue_is_last(&tx->skbs, skb)) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100973 hdr->frame_control |= morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200974 /*
975 * No multi-rate retries for fragmented frames, that
976 * would completely throw off the NAV at other STAs.
977 */
978 info->control.rates[1].idx = -1;
979 info->control.rates[2].idx = -1;
980 info->control.rates[3].idx = -1;
Thomas Huehne3e1a0b2012-07-02 19:46:16 +0200981 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
Johannes Berge6a98542008-10-21 12:40:02 +0200982 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100983 } else {
984 hdr->frame_control &= ~morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200985 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100986 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
987 fragnum++;
Johannes Berg252b86c2011-11-16 15:28:55 +0100988 }
Johannes Berge2454942008-05-15 12:55:28 +0200989
990 return TX_CONTINUE;
Johannes Berge2454942008-05-15 12:55:28 +0200991}
992
Johannes Bergd9e8a702008-06-30 15:10:44 +0200993static ieee80211_tx_result debug_noinline
Johannes Bergfeff1f22009-08-10 16:01:54 +0200994ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
995{
Johannes Berg252b86c2011-11-16 15:28:55 +0100996 struct sk_buff *skb;
Johannes Berg560d2682013-02-05 10:55:21 +0100997 int ac = -1;
Johannes Bergfeff1f22009-08-10 16:01:54 +0200998
999 if (!tx->sta)
1000 return TX_CONTINUE;
1001
Johannes Berg252b86c2011-11-16 15:28:55 +01001002 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg560d2682013-02-05 10:55:21 +01001003 ac = skb_get_queue_mapping(skb);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001004 tx->sta->tx_stats.bytes[ac] += skb->len;
Johannes Berg252b86c2011-11-16 15:28:55 +01001005 }
Johannes Berg560d2682013-02-05 10:55:21 +01001006 if (ac >= 0)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001007 tx->sta->tx_stats.packets[ac]++;
Johannes Bergfeff1f22009-08-10 16:01:54 +02001008
1009 return TX_CONTINUE;
1010}
1011
1012static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +02001013ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
1014{
1015 if (!tx->key)
1016 return TX_CONTINUE;
1017
Johannes Berg97359d12010-08-10 09:46:38 +02001018 switch (tx->key->conf.cipher) {
1019 case WLAN_CIPHER_SUITE_WEP40:
1020 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge2454942008-05-15 12:55:28 +02001021 return ieee80211_crypto_wep_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001022 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berge2454942008-05-15 12:55:28 +02001023 return ieee80211_crypto_tkip_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001024 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001025 return ieee80211_crypto_ccmp_encrypt(
1026 tx, IEEE80211_CCMP_MIC_LEN);
1027 case WLAN_CIPHER_SUITE_CCMP_256:
1028 return ieee80211_crypto_ccmp_encrypt(
1029 tx, IEEE80211_CCMP_256_MIC_LEN);
Johannes Berg97359d12010-08-10 09:46:38 +02001030 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001031 return ieee80211_crypto_aes_cmac_encrypt(tx);
Jouni Malinen56c52da2015-01-24 19:52:08 +02001032 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1033 return ieee80211_crypto_aes_cmac_256_encrypt(tx);
Jouni Malinen8ade5382015-01-24 19:52:09 +02001034 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1035 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1036 return ieee80211_crypto_aes_gmac_encrypt(tx);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001037 case WLAN_CIPHER_SUITE_GCMP:
1038 case WLAN_CIPHER_SUITE_GCMP_256:
1039 return ieee80211_crypto_gcmp_encrypt(tx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +03001040 default:
Yoni Divinskyd32a1022012-01-16 15:18:59 +02001041 return ieee80211_crypto_hw_encrypt(tx);
Johannes Berge2454942008-05-15 12:55:28 +02001042 }
1043
Johannes Berge2454942008-05-15 12:55:28 +02001044 return TX_DROP;
1045}
1046
Johannes Bergd9e8a702008-06-30 15:10:44 +02001047static ieee80211_tx_result debug_noinline
Johannes Berg03f93c32008-06-25 14:36:27 +03001048ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1049{
Johannes Berg252b86c2011-11-16 15:28:55 +01001050 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001051 struct ieee80211_hdr *hdr;
1052 int next_len;
1053 bool group_addr;
Johannes Berg03f93c32008-06-25 14:36:27 +03001054
Johannes Berg252b86c2011-11-16 15:28:55 +01001055 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001056 hdr = (void *) skb->data;
Jouni Malinen7e0aae42009-05-19 19:25:58 +03001057 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1058 break; /* must not overwrite AID */
Johannes Berg252b86c2011-11-16 15:28:55 +01001059 if (!skb_queue_is_last(&tx->skbs, skb)) {
1060 struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1061 next_len = next->len;
1062 } else
1063 next_len = 0;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001064 group_addr = is_multicast_ether_addr(hdr->addr1);
Johannes Berg03f93c32008-06-25 14:36:27 +03001065
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001066 hdr->duration_id =
Johannes Berg252b86c2011-11-16 15:28:55 +01001067 ieee80211_duration(tx, skb, group_addr, next_len);
1068 }
Johannes Berg03f93c32008-06-25 14:36:27 +03001069
1070 return TX_CONTINUE;
1071}
1072
Johannes Berge2ebc742007-07-27 15:43:22 +02001073/* actual transmit path */
1074
Johannes Berga622ab72010-06-10 10:21:39 +02001075static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1076 struct sk_buff *skb,
1077 struct ieee80211_tx_info *info,
1078 struct tid_ampdu_tx *tid_tx,
1079 int tid)
1080{
1081 bool queued = false;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001082 bool reset_agg_timer = false;
Helmut Schaaaa454582012-03-07 17:20:30 +01001083 struct sk_buff *purge_skb = NULL;
Johannes Berga622ab72010-06-10 10:21:39 +02001084
1085 if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1086 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001087 reset_agg_timer = true;
Johannes Berg0ab33702010-06-10 10:21:42 +02001088 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1089 /*
1090 * nothing -- this aggregation session is being started
1091 * but that might still fail with the driver
1092 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001093 } else if (!tx->sta->sta.txq[tid]) {
Johannes Berga622ab72010-06-10 10:21:39 +02001094 spin_lock(&tx->sta->lock);
1095 /*
1096 * Need to re-check now, because we may get here
1097 *
1098 * 1) in the window during which the setup is actually
1099 * already done, but not marked yet because not all
1100 * packets are spliced over to the driver pending
1101 * queue yet -- if this happened we acquire the lock
1102 * either before or after the splice happens, but
1103 * need to recheck which of these cases happened.
1104 *
1105 * 2) during session teardown, if the OPERATIONAL bit
1106 * was cleared due to the teardown but the pointer
1107 * hasn't been assigned NULL yet (or we loaded it
1108 * before it was assigned) -- in this case it may
1109 * now be NULL which means we should just let the
1110 * packet pass through because splicing the frames
1111 * back is already done.
1112 */
Johannes Berg40b275b2011-05-13 14:15:49 +02001113 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
Johannes Berga622ab72010-06-10 10:21:39 +02001114
1115 if (!tid_tx) {
1116 /* do nothing, let packet pass through */
1117 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1118 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001119 reset_agg_timer = true;
Johannes Berga622ab72010-06-10 10:21:39 +02001120 } else {
1121 queued = true;
Emmanuel Grumbachf6d46712016-03-17 16:51:42 +02001122 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
1123 clear_sta_flag(tx->sta, WLAN_STA_SP);
1124 ps_dbg(tx->sta->sdata,
1125 "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
1126 tx->sta->sta.addr, tx->sta->sta.aid);
1127 }
Johannes Berga622ab72010-06-10 10:21:39 +02001128 info->control.vif = &tx->sdata->vif;
1129 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Emmanuel Grumbachfacde7f2016-03-17 16:51:41 +02001130 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berga622ab72010-06-10 10:21:39 +02001131 __skb_queue_tail(&tid_tx->pending, skb);
Helmut Schaaaa454582012-03-07 17:20:30 +01001132 if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1133 purge_skb = __skb_dequeue(&tid_tx->pending);
Johannes Berga622ab72010-06-10 10:21:39 +02001134 }
1135 spin_unlock(&tx->sta->lock);
Helmut Schaaaa454582012-03-07 17:20:30 +01001136
1137 if (purge_skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001138 ieee80211_free_txskb(&tx->local->hw, purge_skb);
Johannes Berga622ab72010-06-10 10:21:39 +02001139 }
1140
Nikolay Martynov285fa692011-11-22 21:50:28 -05001141 /* reset session timer */
Sara Sharon914eac22018-04-20 13:49:19 +03001142 if (reset_agg_timer)
Felix Fietkau12d3952f2012-03-18 22:58:06 +01001143 tid_tx->last_tx = jiffies;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001144
Johannes Berga622ab72010-06-10 10:21:39 +02001145 return queued;
1146}
1147
Johannes Berg58d41852007-09-26 17:53:18 +02001148/*
1149 * initialises @tx
Johannes Berg7c107702015-03-20 14:18:27 +01001150 * pass %NULL for the station if unknown, a valid pointer if known
1151 * or an ERR_PTR() if the station is known not to exist
Johannes Berg58d41852007-09-26 17:53:18 +02001152 */
Johannes Berg9ae54c82008-01-31 19:48:20 +01001153static ieee80211_tx_result
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001154ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1155 struct ieee80211_tx_data *tx,
Johannes Berg7c107702015-03-20 14:18:27 +01001156 struct sta_info *sta, struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02001157{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001158 struct ieee80211_local *local = sdata->local;
Johannes Berg58d41852007-09-26 17:53:18 +02001159 struct ieee80211_hdr *hdr;
Johannes Berge039fa42008-05-15 12:55:29 +02001160 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg68f2b512011-10-07 14:01:24 +02001161 int tid;
Johannes Berge2ebc742007-07-27 15:43:22 +02001162
1163 memset(tx, 0, sizeof(*tx));
1164 tx->skb = skb;
Johannes Berge2ebc742007-07-27 15:43:22 +02001165 tx->local = local;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001166 tx->sdata = sdata;
Johannes Berg252b86c2011-11-16 15:28:55 +01001167 __skb_queue_head_init(&tx->skbs);
Johannes Berge2ebc742007-07-27 15:43:22 +02001168
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001169 /*
1170 * If this flag is set to true anywhere, and we get here,
1171 * we are doing the needed processing, so remove the flag
1172 * now.
1173 */
1174 info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1175
Johannes Berg58d41852007-09-26 17:53:18 +02001176 hdr = (struct ieee80211_hdr *) skb->data;
1177
Johannes Berg7c107702015-03-20 14:18:27 +01001178 if (likely(sta)) {
1179 if (!IS_ERR(sta))
1180 tx->sta = sta;
1181 } else {
1182 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1183 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1184 if (!tx->sta && sdata->wdev.use_4addr)
1185 return TX_DROP;
1186 } else if (info->flags & (IEEE80211_TX_INTFL_NL80211_FRAME_TX |
1187 IEEE80211_TX_CTL_INJECTED) ||
1188 tx->sdata->control_port_protocol == tx->skb->protocol) {
1189 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1190 }
1191 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
1192 tx->sta = sta_info_get(sdata, hdr->addr1);
Felix Fietkau3f0e0b22010-01-08 18:15:13 +01001193 }
Johannes Berg58d41852007-09-26 17:53:18 +02001194
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001195 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
Johannes Berg49a59542011-09-29 16:04:41 +02001196 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001197 ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
1198 !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001199 struct tid_ampdu_tx *tid_tx;
1200
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001201 tid = ieee80211_get_tid(hdr);
Sujith8b30b1f2008-10-24 09:55:27 +05301202
Johannes Berga622ab72010-06-10 10:21:39 +02001203 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1204 if (tid_tx) {
1205 bool queued;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001206
Johannes Berga622ab72010-06-10 10:21:39 +02001207 queued = ieee80211_tx_prep_agg(tx, skb, info,
1208 tid_tx, tid);
1209
1210 if (unlikely(queued))
1211 return TX_QUEUED;
1212 }
Sujith8b30b1f2008-10-24 09:55:27 +05301213 }
1214
Jiri Slabybadffb72007-08-28 17:01:54 -04001215 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001216 tx->flags &= ~IEEE80211_TX_UNICAST;
Johannes Berge039fa42008-05-15 12:55:29 +02001217 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Simon Wunderlich6fd67e92011-11-18 14:20:42 +01001218 } else
Johannes Berg5cf121c2008-02-25 16:27:43 +01001219 tx->flags |= IEEE80211_TX_UNICAST;
Johannes Berg58d41852007-09-26 17:53:18 +02001220
Johannes Berga26eb272011-10-07 14:01:25 +02001221 if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1222 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1223 skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1224 info->flags & IEEE80211_TX_CTL_AMPDU)
1225 info->flags |= IEEE80211_TX_CTL_DONTFRAG;
Johannes Berg58d41852007-09-26 17:53:18 +02001226 }
1227
Johannes Berge2ebc742007-07-27 15:43:22 +02001228 if (!tx->sta)
Johannes Berge039fa42008-05-15 12:55:29 +02001229 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Felix Fietkauf7418bc2015-09-24 14:59:49 +02001230 else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
Johannes Berge039fa42008-05-15 12:55:29 +02001231 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Felix Fietkauf7418bc2015-09-24 14:59:49 +02001232 ieee80211_check_fast_xmit(tx->sta);
1233 }
Johannes Berg58d41852007-09-26 17:53:18 +02001234
Johannes Berge039fa42008-05-15 12:55:29 +02001235 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001236
Johannes Berg9ae54c82008-01-31 19:48:20 +01001237 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001238}
1239
Michal Kazior80a83cf2016-05-19 10:37:48 +02001240static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
1241 struct ieee80211_vif *vif,
Michal Kaziordbef5362017-01-13 13:32:51 +01001242 struct sta_info *sta,
Michal Kazior80a83cf2016-05-19 10:37:48 +02001243 struct sk_buff *skb)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001244{
1245 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001246 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001247 struct ieee80211_txq *txq = NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001248
Felix Fietkauc3732a72016-02-28 15:19:53 +01001249 if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
1250 (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
Michal Kazior80a83cf2016-05-19 10:37:48 +02001251 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001252
Johannes Bergadf8ed02018-08-31 11:31:08 +03001253 if (unlikely(!ieee80211_is_data_present(hdr->frame_control))) {
1254 if ((!ieee80211_is_mgmt(hdr->frame_control) ||
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001255 ieee80211_is_bufferable_mmpdu(hdr->frame_control) ||
1256 vif->type == NL80211_IFTYPE_STATION) &&
Johannes Bergadf8ed02018-08-31 11:31:08 +03001257 sta && sta->uploaded) {
1258 /*
1259 * This will be NULL if the driver didn't set the
1260 * opt-in hardware flag.
1261 */
1262 txq = sta->sta.txq[IEEE80211_NUM_TIDS];
1263 }
1264 } else if (sta) {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001265 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1266
Michal Kaziordbef5362017-01-13 13:32:51 +01001267 if (!sta->uploaded)
1268 return NULL;
1269
1270 txq = sta->sta.txq[tid];
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001271 } else if (vif) {
1272 txq = vif->txq;
1273 }
1274
1275 if (!txq)
Michal Kazior80a83cf2016-05-19 10:37:48 +02001276 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001277
Michal Kazior80a83cf2016-05-19 10:37:48 +02001278 return to_txq_info(txq);
1279}
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001280
Michal Kazior5caa3282016-05-19 10:37:51 +02001281static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
1282{
1283 IEEE80211_SKB_CB(skb)->control.enqueue_time = codel_get_time();
1284}
1285
Michal Kazior5caa3282016-05-19 10:37:51 +02001286static u32 codel_skb_len_func(const struct sk_buff *skb)
1287{
1288 return skb->len;
1289}
1290
1291static codel_time_t codel_skb_time_func(const struct sk_buff *skb)
1292{
1293 const struct ieee80211_tx_info *info;
1294
1295 info = (const struct ieee80211_tx_info *)skb->cb;
1296 return info->control.enqueue_time;
1297}
1298
1299static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars,
1300 void *ctx)
1301{
1302 struct ieee80211_local *local;
1303 struct txq_info *txqi;
1304 struct fq *fq;
1305 struct fq_flow *flow;
1306
1307 txqi = ctx;
1308 local = vif_to_sdata(txqi->txq.vif)->local;
1309 fq = &local->fq;
1310
1311 if (cvars == &txqi->def_cvars)
1312 flow = &txqi->def_flow;
1313 else
1314 flow = &fq->flows[cvars - local->cvars];
1315
1316 return fq_flow_dequeue(fq, flow);
1317}
1318
1319static void codel_drop_func(struct sk_buff *skb,
1320 void *ctx)
1321{
1322 struct ieee80211_local *local;
1323 struct ieee80211_hw *hw;
1324 struct txq_info *txqi;
1325
1326 txqi = ctx;
1327 local = vif_to_sdata(txqi->txq.vif)->local;
1328 hw = &local->hw;
1329
1330 ieee80211_free_txskb(hw, skb);
1331}
1332
Michal Kaziorfa962b92016-05-19 10:37:49 +02001333static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
1334 struct fq_tin *tin,
1335 struct fq_flow *flow)
1336{
Michal Kazior5caa3282016-05-19 10:37:51 +02001337 struct ieee80211_local *local;
1338 struct txq_info *txqi;
1339 struct codel_vars *cvars;
1340 struct codel_params *cparams;
1341 struct codel_stats *cstats;
1342
1343 local = container_of(fq, struct ieee80211_local, fq);
1344 txqi = container_of(tin, struct txq_info, tin);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001345 cstats = &txqi->cstats;
Michal Kazior5caa3282016-05-19 10:37:51 +02001346
Toke Høiland-Jørgensen484a54c2017-04-06 11:38:26 +02001347 if (txqi->txq.sta) {
1348 struct sta_info *sta = container_of(txqi->txq.sta,
1349 struct sta_info, sta);
1350 cparams = &sta->cparams;
1351 } else {
1352 cparams = &local->cparams;
1353 }
1354
Michal Kazior5caa3282016-05-19 10:37:51 +02001355 if (flow == &txqi->def_flow)
1356 cvars = &txqi->def_cvars;
1357 else
1358 cvars = &local->cvars[flow - fq->flows];
1359
1360 return codel_dequeue(txqi,
1361 &flow->backlog,
1362 cparams,
1363 cvars,
1364 cstats,
1365 codel_skb_len_func,
1366 codel_skb_time_func,
1367 codel_drop_func,
1368 codel_dequeue_func);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001369}
1370
1371static void fq_skb_free_func(struct fq *fq,
1372 struct fq_tin *tin,
1373 struct fq_flow *flow,
1374 struct sk_buff *skb)
1375{
1376 struct ieee80211_local *local;
1377
1378 local = container_of(fq, struct ieee80211_local, fq);
1379 ieee80211_free_txskb(&local->hw, skb);
1380}
1381
1382static struct fq_flow *fq_flow_get_default_func(struct fq *fq,
1383 struct fq_tin *tin,
1384 int idx,
1385 struct sk_buff *skb)
1386{
1387 struct txq_info *txqi;
1388
1389 txqi = container_of(tin, struct txq_info, tin);
1390 return &txqi->def_flow;
1391}
1392
Michal Kazior80a83cf2016-05-19 10:37:48 +02001393static void ieee80211_txq_enqueue(struct ieee80211_local *local,
1394 struct txq_info *txqi,
1395 struct sk_buff *skb)
1396{
Michal Kaziorfa962b92016-05-19 10:37:49 +02001397 struct fq *fq = &local->fq;
1398 struct fq_tin *tin = &txqi->tin;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001399 u32 flow_idx = fq_flow_idx(fq, skb);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001400
Michal Kazior5caa3282016-05-19 10:37:51 +02001401 ieee80211_set_skb_enqueue_time(skb);
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001402
1403 spin_lock_bh(&fq->lock);
1404 fq_tin_enqueue(fq, tin, flow_idx, skb,
Michal Kaziorfa962b92016-05-19 10:37:49 +02001405 fq_skb_free_func,
1406 fq_flow_get_default_func);
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001407 spin_unlock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001408}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001409
Johannes Berg2a9e2572017-10-06 11:53:33 +02001410static bool fq_vlan_filter_func(struct fq *fq, struct fq_tin *tin,
1411 struct fq_flow *flow, struct sk_buff *skb,
1412 void *data)
1413{
1414 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1415
1416 return info->control.vif == data;
1417}
1418
1419void ieee80211_txq_remove_vlan(struct ieee80211_local *local,
1420 struct ieee80211_sub_if_data *sdata)
1421{
1422 struct fq *fq = &local->fq;
1423 struct txq_info *txqi;
1424 struct fq_tin *tin;
1425 struct ieee80211_sub_if_data *ap;
1426
1427 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1428 return;
1429
1430 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1431
1432 if (!ap->vif.txq)
1433 return;
1434
1435 txqi = to_txq_info(ap->vif.txq);
1436 tin = &txqi->tin;
1437
1438 spin_lock_bh(&fq->lock);
1439 fq_tin_filter(fq, tin, fq_vlan_filter_func, &sdata->vif,
1440 fq_skb_free_func);
1441 spin_unlock_bh(&fq->lock);
1442}
1443
Michal Kaziorfa962b92016-05-19 10:37:49 +02001444void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
1445 struct sta_info *sta,
1446 struct txq_info *txqi, int tid)
1447{
1448 fq_tin_init(&txqi->tin);
1449 fq_flow_init(&txqi->def_flow);
Michal Kazior5caa3282016-05-19 10:37:51 +02001450 codel_vars_init(&txqi->def_cvars);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001451 codel_stats_init(&txqi->cstats);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001452 __skb_queue_head_init(&txqi->frags);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001453 INIT_LIST_HEAD(&txqi->schedule_order);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001454
1455 txqi->txq.vif = &sdata->vif;
1456
Johannes Bergadf8ed02018-08-31 11:31:08 +03001457 if (!sta) {
Michal Kaziorfa962b92016-05-19 10:37:49 +02001458 sdata->vif.txq = &txqi->txq;
1459 txqi->txq.tid = 0;
1460 txqi->txq.ac = IEEE80211_AC_BE;
Johannes Bergadf8ed02018-08-31 11:31:08 +03001461
1462 return;
Michal Kazior80a83cf2016-05-19 10:37:48 +02001463 }
Johannes Bergadf8ed02018-08-31 11:31:08 +03001464
1465 if (tid == IEEE80211_NUM_TIDS) {
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001466 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1467 /* Drivers need to opt in to the management MPDU TXQ */
1468 if (!ieee80211_hw_check(&sdata->local->hw,
1469 STA_MMPDU_TXQ))
1470 return;
1471 } else if (!ieee80211_hw_check(&sdata->local->hw,
1472 BUFF_MMPDU_TXQ)) {
1473 /* Drivers need to opt in to the bufferable MMPDU TXQ */
Johannes Bergadf8ed02018-08-31 11:31:08 +03001474 return;
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001475 }
Johannes Bergadf8ed02018-08-31 11:31:08 +03001476 txqi->txq.ac = IEEE80211_AC_VO;
1477 } else {
1478 txqi->txq.ac = ieee80211_ac_from_tid(tid);
1479 }
1480
1481 txqi->txq.sta = &sta->sta;
1482 txqi->txq.tid = tid;
1483 sta->sta.txq[tid] = &txqi->txq;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001484}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001485
Michal Kaziorfa962b92016-05-19 10:37:49 +02001486void ieee80211_txq_purge(struct ieee80211_local *local,
1487 struct txq_info *txqi)
1488{
1489 struct fq *fq = &local->fq;
1490 struct fq_tin *tin = &txqi->tin;
1491
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08001492 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001493 fq_tin_reset(fq, tin, fq_skb_free_func);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001494 ieee80211_purge_tx_queue(&local->hw, &txqi->frags);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08001495 spin_unlock_bh(&fq->lock);
1496
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001497 spin_lock_bh(&local->active_txq_lock[txqi->txq.ac]);
1498 list_del_init(&txqi->schedule_order);
1499 spin_unlock_bh(&local->active_txq_lock[txqi->txq.ac]);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001500}
1501
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001502void ieee80211_txq_set_params(struct ieee80211_local *local)
1503{
1504 if (local->hw.wiphy->txq_limit)
1505 local->fq.limit = local->hw.wiphy->txq_limit;
1506 else
1507 local->hw.wiphy->txq_limit = local->fq.limit;
1508
1509 if (local->hw.wiphy->txq_memory_limit)
1510 local->fq.memory_limit = local->hw.wiphy->txq_memory_limit;
1511 else
1512 local->hw.wiphy->txq_memory_limit = local->fq.memory_limit;
1513
1514 if (local->hw.wiphy->txq_quantum)
1515 local->fq.quantum = local->hw.wiphy->txq_quantum;
1516 else
1517 local->hw.wiphy->txq_quantum = local->fq.quantum;
1518}
1519
Michal Kaziorfa962b92016-05-19 10:37:49 +02001520int ieee80211_txq_setup_flows(struct ieee80211_local *local)
1521{
1522 struct fq *fq = &local->fq;
1523 int ret;
Michal Kazior5caa3282016-05-19 10:37:51 +02001524 int i;
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001525 bool supp_vht = false;
1526 enum nl80211_band band;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001527
1528 if (!local->ops->wake_tx_queue)
1529 return 0;
1530
1531 ret = fq_init(fq, 4096);
1532 if (ret)
1533 return ret;
1534
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001535 /*
1536 * If the hardware doesn't support VHT, it is safe to limit the maximum
1537 * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
1538 */
1539 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1540 struct ieee80211_supported_band *sband;
1541
1542 sband = local->hw.wiphy->bands[band];
1543 if (!sband)
1544 continue;
1545
1546 supp_vht = supp_vht || sband->vht_cap.vht_supported;
1547 }
1548
1549 if (!supp_vht)
1550 fq->memory_limit = 4 << 20; /* 4 Mbytes */
1551
Michal Kazior5caa3282016-05-19 10:37:51 +02001552 codel_params_init(&local->cparams);
Michal Kazior5caa3282016-05-19 10:37:51 +02001553 local->cparams.interval = MS2TIME(100);
1554 local->cparams.target = MS2TIME(20);
1555 local->cparams.ecn = true;
1556
1557 local->cvars = kcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
1558 GFP_KERNEL);
1559 if (!local->cvars) {
Michal Kazior59a7c822016-06-29 14:00:34 +02001560 spin_lock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001561 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001562 spin_unlock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001563 return -ENOMEM;
1564 }
1565
1566 for (i = 0; i < fq->flows_cnt; i++)
1567 codel_vars_init(&local->cvars[i]);
1568
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001569 ieee80211_txq_set_params(local);
1570
Michal Kaziorfa962b92016-05-19 10:37:49 +02001571 return 0;
1572}
1573
1574void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1575{
1576 struct fq *fq = &local->fq;
1577
1578 if (!local->ops->wake_tx_queue)
1579 return;
1580
Michal Kazior5caa3282016-05-19 10:37:51 +02001581 kfree(local->cvars);
1582 local->cvars = NULL;
1583
Michal Kazior59a7c822016-06-29 14:00:34 +02001584 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001585 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001586 spin_unlock_bh(&fq->lock);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001587}
1588
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001589static bool ieee80211_queue_skb(struct ieee80211_local *local,
1590 struct ieee80211_sub_if_data *sdata,
1591 struct sta_info *sta,
1592 struct sk_buff *skb)
1593{
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001594 struct ieee80211_vif *vif;
1595 struct txq_info *txqi;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001596
1597 if (!local->ops->wake_tx_queue ||
1598 sdata->vif.type == NL80211_IFTYPE_MONITOR)
1599 return false;
1600
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001601 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1602 sdata = container_of(sdata->bss,
1603 struct ieee80211_sub_if_data, u.ap);
1604
1605 vif = &sdata->vif;
Michal Kaziordbef5362017-01-13 13:32:51 +01001606 txqi = ieee80211_get_txq(local, vif, sta, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001607
1608 if (!txqi)
1609 return false;
1610
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001611 ieee80211_txq_enqueue(local, txqi, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001612
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001613 schedule_and_wake_txq(local, txqi);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001614
1615 return true;
1616}
1617
Johannes Berg11127e92011-11-16 16:02:47 +01001618static bool ieee80211_tx_frags(struct ieee80211_local *local,
1619 struct ieee80211_vif *vif,
Johannes Berg4fd03282019-10-01 23:26:35 +02001620 struct sta_info *sta,
Johannes Berg11127e92011-11-16 16:02:47 +01001621 struct sk_buff_head *skbs,
1622 bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001623{
Michal Kazior80a83cf2016-05-19 10:37:48 +02001624 struct ieee80211_tx_control control = {};
Johannes Berg252b86c2011-11-16 15:28:55 +01001625 struct sk_buff *skb, *tmp;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001626 unsigned long flags;
Johannes Berge2ebc742007-07-27 15:43:22 +02001627
Johannes Berg252b86c2011-11-16 15:28:55 +01001628 skb_queue_walk_safe(skbs, skb, tmp) {
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001629 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1630 int q = info->hw_queue;
1631
1632#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1633 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1634 __skb_unlink(skb, skbs);
Felix Fietkauc3e77242012-10-08 14:39:33 +02001635 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001636 continue;
1637 }
1638#endif
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001639
1640 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001641 if (local->queue_stop_reasons[q] ||
Johannes Berg7bb45682011-02-24 14:42:06 +01001642 (!txpending && !skb_queue_empty(&local->pending[q]))) {
Seth Forshee6c17b772013-02-11 11:21:07 -06001643 if (unlikely(info->flags &
Seth Forsheea7679ed2013-02-25 14:58:05 -06001644 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1645 if (local->queue_stop_reasons[q] &
1646 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1647 /*
1648 * Drop off-channel frames if queues
1649 * are stopped for any reason other
1650 * than off-channel operation. Never
1651 * queue them.
1652 */
1653 spin_unlock_irqrestore(
1654 &local->queue_stop_reason_lock,
1655 flags);
1656 ieee80211_purge_tx_queue(&local->hw,
1657 skbs);
1658 return true;
1659 }
1660 } else {
1661
Seth Forshee6c17b772013-02-11 11:21:07 -06001662 /*
Seth Forsheea7679ed2013-02-25 14:58:05 -06001663 * Since queue is stopped, queue up frames for
1664 * later transmission from the tx-pending
1665 * tasklet when the queue is woken again.
Seth Forshee6c17b772013-02-11 11:21:07 -06001666 */
Seth Forsheea7679ed2013-02-25 14:58:05 -06001667 if (txpending)
1668 skb_queue_splice_init(skbs,
1669 &local->pending[q]);
1670 else
1671 skb_queue_splice_tail_init(skbs,
1672 &local->pending[q]);
1673
1674 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1675 flags);
1676 return false;
Seth Forshee6c17b772013-02-11 11:21:07 -06001677 }
Johannes Berg7bb45682011-02-24 14:42:06 +01001678 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001679 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Tomas Winklerf8e79dd2008-07-24 18:46:44 +03001680
Johannes Berg11127e92011-11-16 16:02:47 +01001681 info->control.vif = vif;
Johannes Berg4fd03282019-10-01 23:26:35 +02001682 control.sta = sta ? &sta->sta : NULL;
Johannes Bergec25acc2010-07-22 17:11:28 +02001683
Johannes Berg252b86c2011-11-16 15:28:55 +01001684 __skb_unlink(skb, skbs);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001685 drv_tx(local, &control, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001686 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001687
Johannes Berg11127e92011-11-16 16:02:47 +01001688 return true;
1689}
1690
1691/*
1692 * Returns false if the frame couldn't be transmitted but was queued instead.
1693 */
1694static bool __ieee80211_tx(struct ieee80211_local *local,
1695 struct sk_buff_head *skbs, int led_len,
1696 struct sta_info *sta, bool txpending)
1697{
1698 struct ieee80211_tx_info *info;
1699 struct ieee80211_sub_if_data *sdata;
1700 struct ieee80211_vif *vif;
Johannes Berg11127e92011-11-16 16:02:47 +01001701 struct sk_buff *skb;
1702 bool result = true;
1703 __le16 fc;
1704
1705 if (WARN_ON(skb_queue_empty(skbs)))
1706 return true;
1707
1708 skb = skb_peek(skbs);
1709 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1710 info = IEEE80211_SKB_CB(skb);
1711 sdata = vif_to_sdata(info->control.vif);
1712 if (sta && !sta->uploaded)
1713 sta = NULL;
1714
Johannes Berg11127e92011-11-16 16:02:47 +01001715 switch (sdata->vif.type) {
1716 case NL80211_IFTYPE_MONITOR:
Aviya Erenfeldd8212182016-08-29 23:25:15 +03001717 if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
Johannes Bergc82b5a72013-07-14 23:39:20 +03001718 vif = &sdata->vif;
1719 break;
1720 }
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001721 sdata = rcu_dereference(local->monitor_sdata);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001722 if (sdata) {
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001723 vif = &sdata->vif;
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001724 info->hw_queue =
1725 vif->hw_queue[skb_get_queue_mapping(skb)];
Johannes Berg30686bf2015-06-02 21:39:54 +02001726 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
Johannes Berg63b4d8b2015-11-24 15:41:50 +01001727 ieee80211_purge_tx_queue(&local->hw, skbs);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001728 return true;
1729 } else
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001730 vif = NULL;
Johannes Berg11127e92011-11-16 16:02:47 +01001731 break;
1732 case NL80211_IFTYPE_AP_VLAN:
1733 sdata = container_of(sdata->bss,
1734 struct ieee80211_sub_if_data, u.ap);
1735 /* fall through */
1736 default:
1737 vif = &sdata->vif;
1738 break;
1739 }
1740
Johannes Berg4fd03282019-10-01 23:26:35 +02001741 result = ieee80211_tx_frags(local, vif, sta, skbs, txpending);
Johannes Berg11127e92011-11-16 16:02:47 +01001742
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001743 ieee80211_tpt_led_trig_tx(local, fc, led_len);
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001744
Johannes Berg4db4e0a2011-11-24 14:47:36 +01001745 WARN_ON_ONCE(!skb_queue_empty(skbs));
Johannes Berg252b86c2011-11-16 15:28:55 +01001746
Johannes Berg11127e92011-11-16 16:02:47 +01001747 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001748}
1749
Johannes Berg97b045d2008-06-20 01:22:30 +02001750/*
1751 * Invoke TX handlers, return 0 on success and non-zero if the
1752 * frame was dropped or queued.
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001753 *
1754 * The handlers are split into an early and late part. The latter is everything
1755 * that can be sensitive to reordering, and will be deferred to after packets
1756 * are dequeued from the intermediate queues (when they are enabled).
Johannes Berg97b045d2008-06-20 01:22:30 +02001757 */
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001758static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx)
Johannes Berg97b045d2008-06-20 01:22:30 +02001759{
Johannes Berg97b045d2008-06-20 01:22:30 +02001760 ieee80211_tx_result res = TX_DROP;
Johannes Berg97b045d2008-06-20 01:22:30 +02001761
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001762#define CALL_TXH(txh) \
1763 do { \
1764 res = txh(tx); \
1765 if (res != TX_CONTINUE) \
1766 goto txh_done; \
1767 } while (0)
Johannes Berg97b045d2008-06-20 01:22:30 +02001768
Kalle Valo5c1b98a2010-01-12 10:42:46 +02001769 CALL_TXH(ieee80211_tx_h_dynamic_ps);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001770 CALL_TXH(ieee80211_tx_h_check_assoc);
1771 CALL_TXH(ieee80211_tx_h_ps_buf);
Johannes Berga621fa42010-08-27 14:26:54 +03001772 CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001773 CALL_TXH(ieee80211_tx_h_select_key);
Johannes Berg30686bf2015-06-02 21:39:54 +02001774 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
Johannes Bergaf65cd962009-11-17 18:18:36 +01001775 CALL_TXH(ieee80211_tx_h_rate_ctrl);
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001776
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001777 txh_done:
1778 if (unlikely(res == TX_DROP)) {
1779 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1780 if (tx->skb)
1781 ieee80211_free_txskb(&tx->local->hw, tx->skb);
1782 else
1783 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1784 return -1;
1785 } else if (unlikely(res == TX_QUEUED)) {
1786 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1787 return -1;
1788 }
1789
1790 return 0;
1791}
1792
1793/*
1794 * Late handlers can be called while the sta lock is held. Handlers that can
1795 * cause packets to be generated will cause deadlock!
1796 */
1797static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx)
1798{
1799 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1800 ieee80211_tx_result res = TX_CONTINUE;
1801
Johannes Bergaa5b5492011-12-02 22:08:52 +01001802 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1803 __skb_queue_tail(&tx->skbs, tx->skb);
1804 tx->skb = NULL;
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001805 goto txh_done;
Johannes Bergaa5b5492011-12-02 22:08:52 +01001806 }
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001807
1808 CALL_TXH(ieee80211_tx_h_michael_mic_add);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001809 CALL_TXH(ieee80211_tx_h_sequence);
1810 CALL_TXH(ieee80211_tx_h_fragment);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001811 /* handlers after fragment must be aware of tx info fragmentation! */
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001812 CALL_TXH(ieee80211_tx_h_stats);
1813 CALL_TXH(ieee80211_tx_h_encrypt);
Johannes Berg30686bf2015-06-02 21:39:54 +02001814 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
Arik Nemtsov8fd369e2011-01-31 22:29:12 +02001815 CALL_TXH(ieee80211_tx_h_calculate_duration);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001816#undef CALL_TXH
1817
1818 txh_done:
Johannes Berg97b045d2008-06-20 01:22:30 +02001819 if (unlikely(res == TX_DROP)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001820 I802_DEBUG_INC(tx->local->tx_handlers_drop);
Johannes Berg252b86c2011-11-16 15:28:55 +01001821 if (tx->skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001822 ieee80211_free_txskb(&tx->local->hw, tx->skb);
Johannes Berg252b86c2011-11-16 15:28:55 +01001823 else
Felix Fietkau1f98ab72012-11-10 03:44:14 +01001824 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
Johannes Berg97b045d2008-06-20 01:22:30 +02001825 return -1;
1826 } else if (unlikely(res == TX_QUEUED)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001827 I802_DEBUG_INC(tx->local->tx_handlers_queued);
Johannes Berg97b045d2008-06-20 01:22:30 +02001828 return -1;
1829 }
1830
1831 return 0;
1832}
1833
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001834static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1835{
1836 int r = invoke_tx_handlers_early(tx);
1837
1838 if (r)
1839 return r;
1840 return invoke_tx_handlers_late(tx);
1841}
1842
Felix Fietkau06be6b12013-10-14 18:01:00 +02001843bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1844 struct ieee80211_vif *vif, struct sk_buff *skb,
1845 int band, struct ieee80211_sta **sta)
1846{
1847 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1848 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1849 struct ieee80211_tx_data tx;
Johannes Berg88724a82015-03-01 09:10:12 +02001850 struct sk_buff *skb2;
Felix Fietkau06be6b12013-10-14 18:01:00 +02001851
Johannes Berg7c107702015-03-20 14:18:27 +01001852 if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
Felix Fietkau06be6b12013-10-14 18:01:00 +02001853 return false;
1854
1855 info->band = band;
1856 info->control.vif = vif;
1857 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1858
1859 if (invoke_tx_handlers(&tx))
1860 return false;
1861
1862 if (sta) {
1863 if (tx.sta)
1864 *sta = &tx.sta->sta;
1865 else
1866 *sta = NULL;
1867 }
1868
Johannes Berg88724a82015-03-01 09:10:12 +02001869 /* this function isn't suitable for fragmented data frames */
1870 skb2 = __skb_dequeue(&tx.skbs);
1871 if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1872 ieee80211_free_txskb(hw, skb2);
1873 ieee80211_purge_tx_queue(hw, &tx.skbs);
1874 return false;
1875 }
1876
Felix Fietkau06be6b12013-10-14 18:01:00 +02001877 return true;
1878}
1879EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1880
Johannes Berg7bb45682011-02-24 14:42:06 +01001881/*
1882 * Returns false if the frame couldn't be transmitted but was queued instead.
1883 */
1884static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01001885 struct sta_info *sta, struct sk_buff *skb,
Johannes Bergb9771d42018-05-28 15:47:41 +02001886 bool txpending, u32 txdata_flags)
Johannes Berge2ebc742007-07-27 15:43:22 +02001887{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001888 struct ieee80211_local *local = sdata->local;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001889 struct ieee80211_tx_data tx;
Johannes Berg97b045d2008-06-20 01:22:30 +02001890 ieee80211_tx_result res_prepare;
Johannes Berge039fa42008-05-15 12:55:29 +02001891 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001892 bool result = true;
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001893 int led_len;
Johannes Berge2ebc742007-07-27 15:43:22 +02001894
Johannes Berge2ebc742007-07-27 15:43:22 +02001895 if (unlikely(skb->len < 10)) {
1896 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001897 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001898 }
1899
Johannes Berg58d41852007-09-26 17:53:18 +02001900 /* initialises tx */
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001901 led_len = skb->len;
Johannes Berg7c107702015-03-20 14:18:27 +01001902 res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001903
Johannes Bergb9771d42018-05-28 15:47:41 +02001904 tx.flags |= txdata_flags;
1905
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001906 if (unlikely(res_prepare == TX_DROP)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02001907 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001908 return true;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001909 } else if (unlikely(res_prepare == TX_QUEUED)) {
Johannes Berg55de9082012-07-26 17:24:39 +02001910 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001911 }
1912
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001913 /* set up hw_queue value early */
1914 if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
Johannes Berg30686bf2015-06-02 21:39:54 +02001915 !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001916 info->hw_queue =
1917 sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1918
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001919 if (invoke_tx_handlers_early(&tx))
Bob Copeland6eae4a62018-09-05 06:22:59 -04001920 return true;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001921
1922 if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb))
1923 return true;
1924
1925 if (!invoke_tx_handlers_late(&tx))
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001926 result = __ieee80211_tx(local, &tx.skbs, led_len,
1927 tx.sta, txpending);
Johannes Berg55de9082012-07-26 17:24:39 +02001928
Johannes Berg7bb45682011-02-24 14:42:06 +01001929 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001930}
1931
1932/* device xmit handlers */
1933
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301934static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
Johannes Berg23c07522008-05-29 10:38:53 +02001935 struct sk_buff *skb,
1936 int head_need, bool may_encrypt)
1937{
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301938 struct ieee80211_local *local = sdata->local;
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001939 struct ieee80211_hdr *hdr;
1940 bool enc_tailroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001941 int tail_need = 0;
1942
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001943 hdr = (struct ieee80211_hdr *) skb->data;
1944 enc_tailroom = may_encrypt &&
1945 (sdata->crypto_tx_tailroom_needed_cnt ||
1946 ieee80211_is_mgmt(hdr->frame_control));
1947
1948 if (enc_tailroom) {
Johannes Berg23c07522008-05-29 10:38:53 +02001949 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1950 tail_need -= skb_tailroom(skb);
1951 tail_need = max_t(int, tail_need, 0);
1952 }
1953
Ido Yarivc70f59a2014-07-29 15:39:14 +03001954 if (skb_cloned(skb) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001955 (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001956 !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
Johannes Berg23c07522008-05-29 10:38:53 +02001957 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001958 else if (head_need || tail_need)
Johannes Berg23c07522008-05-29 10:38:53 +02001959 I802_DEBUG_INC(local->tx_expand_skb_head);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001960 else
1961 return 0;
Johannes Berg23c07522008-05-29 10:38:53 +02001962
1963 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
Joe Perches0fb9a9e2010-08-20 16:25:38 -07001964 wiphy_debug(local->hw.wiphy,
1965 "failed to reallocate TX buffer\n");
Johannes Berg23c07522008-05-29 10:38:53 +02001966 return -ENOMEM;
1967 }
1968
Johannes Berg23c07522008-05-29 10:38:53 +02001969 return 0;
1970}
1971
Johannes Berg7c107702015-03-20 14:18:27 +01001972void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
Johannes Bergb9771d42018-05-28 15:47:41 +02001973 struct sta_info *sta, struct sk_buff *skb,
1974 u32 txdata_flags)
Johannes Berge2ebc742007-07-27 15:43:22 +02001975{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001976 struct ieee80211_local *local = sdata->local;
Johannes Berge039fa42008-05-15 12:55:29 +02001977 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Colin Ian King11b05ba2018-02-02 16:31:42 +00001978 struct ieee80211_hdr *hdr;
Johannes Berge2ebc742007-07-27 15:43:22 +02001979 int headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001980 bool may_encrypt;
Johannes Berge2ebc742007-07-27 15:43:22 +02001981
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001982 may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
Johannes Berg23c07522008-05-29 10:38:53 +02001983
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001984 headroom = local->tx_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001985 if (may_encrypt)
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001986 headroom += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001987 headroom -= skb_headroom(skb);
1988 headroom = max_t(int, 0, headroom);
1989
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301990 if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02001991 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001992 return;
Johannes Berge2ebc742007-07-27 15:43:22 +02001993 }
1994
Steve deRosier740c1aa2010-09-11 20:01:31 -07001995 hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berg5061b0c2009-07-14 00:33:34 +02001996 info->control.vif = &sdata->vif;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001997
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001998 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1999 if (ieee80211_is_data(hdr->frame_control) &&
2000 is_unicast_ether_addr(hdr->addr1)) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002001 if (mesh_nexthop_resolve(sdata, skb))
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002002 return; /* skb queued: don't free */
2003 } else {
2004 ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
2005 }
Johannes Berg98aed9f2012-03-27 14:18:36 +02002006 }
Javier Cardonacca89492009-08-10 17:29:29 -07002007
Javier Cardona2154c81c2011-09-07 17:49:53 -07002008 ieee80211_set_qos_hdr(sdata, skb);
Johannes Bergb9771d42018-05-28 15:47:41 +02002009 ieee80211_tx(sdata, sta, skb, false, txdata_flags);
Johannes Berge2ebc742007-07-27 15:43:22 +02002010}
2011
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002012static bool ieee80211_parse_tx_radiotap(struct ieee80211_local *local,
2013 struct sk_buff *skb)
Johannes Berg73b9f032011-10-07 14:01:26 +02002014{
2015 struct ieee80211_radiotap_iterator iterator;
2016 struct ieee80211_radiotap_header *rthdr =
2017 (struct ieee80211_radiotap_header *) skb->data;
2018 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002019 struct ieee80211_supported_band *sband =
2020 local->hw.wiphy->bands[info->band];
Johannes Berg73b9f032011-10-07 14:01:26 +02002021 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
2022 NULL);
2023 u16 txflags;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002024 u16 rate = 0;
2025 bool rate_found = false;
2026 u8 rate_retries = 0;
2027 u16 rate_flags = 0;
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002028 u8 mcs_known, mcs_flags, mcs_bw;
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002029 u16 vht_known;
2030 u8 vht_mcs = 0, vht_nss = 0;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002031 int i;
Johannes Berg73b9f032011-10-07 14:01:26 +02002032
2033 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
2034 IEEE80211_TX_CTL_DONTFRAG;
2035
2036 /*
2037 * for every radiotap entry that is present
2038 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
2039 * entries present, or -EINVAL on error)
2040 */
2041
2042 while (!ret) {
2043 ret = ieee80211_radiotap_iterator_next(&iterator);
2044
2045 if (ret)
2046 continue;
2047
2048 /* see if this argument is something we can use */
2049 switch (iterator.this_arg_index) {
2050 /*
2051 * You must take care when dereferencing iterator.this_arg
2052 * for multibyte types... the pointer is not aligned. Use
2053 * get_unaligned((type *)iterator.this_arg) to dereference
2054 * iterator.this_arg for type "type" safely on all arches.
2055 */
2056 case IEEE80211_RADIOTAP_FLAGS:
2057 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
2058 /*
2059 * this indicates that the skb we have been
2060 * handed has the 32-bit FCS CRC at the end...
2061 * we should react to that by snipping it off
2062 * because it will be recomputed and added
2063 * on transmission
2064 */
2065 if (skb->len < (iterator._max_length + FCS_LEN))
2066 return false;
2067
2068 skb_trim(skb, skb->len - FCS_LEN);
2069 }
2070 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
2071 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
2072 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
2073 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
2074 break;
2075
2076 case IEEE80211_RADIOTAP_TX_FLAGS:
2077 txflags = get_unaligned_le16(iterator.this_arg);
2078 if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
2079 info->flags |= IEEE80211_TX_CTL_NO_ACK;
2080 break;
2081
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002082 case IEEE80211_RADIOTAP_RATE:
2083 rate = *iterator.this_arg;
2084 rate_flags = 0;
2085 rate_found = true;
2086 break;
2087
2088 case IEEE80211_RADIOTAP_DATA_RETRIES:
2089 rate_retries = *iterator.this_arg;
2090 break;
2091
2092 case IEEE80211_RADIOTAP_MCS:
2093 mcs_known = iterator.this_arg[0];
2094 mcs_flags = iterator.this_arg[1];
2095 if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
2096 break;
2097
2098 rate_found = true;
2099 rate = iterator.this_arg[2];
2100 rate_flags = IEEE80211_TX_RC_MCS;
2101
2102 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
2103 mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
2104 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2105
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002106 mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002107 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002108 mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002109 rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2110 break;
2111
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002112 case IEEE80211_RADIOTAP_VHT:
2113 vht_known = get_unaligned_le16(iterator.this_arg);
2114 rate_found = true;
2115
2116 rate_flags = IEEE80211_TX_RC_VHT_MCS;
2117 if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
2118 (iterator.this_arg[2] &
2119 IEEE80211_RADIOTAP_VHT_FLAG_SGI))
2120 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2121 if (vht_known &
2122 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
2123 if (iterator.this_arg[3] == 1)
2124 rate_flags |=
2125 IEEE80211_TX_RC_40_MHZ_WIDTH;
2126 else if (iterator.this_arg[3] == 4)
2127 rate_flags |=
2128 IEEE80211_TX_RC_80_MHZ_WIDTH;
2129 else if (iterator.this_arg[3] == 11)
2130 rate_flags |=
2131 IEEE80211_TX_RC_160_MHZ_WIDTH;
2132 }
2133
2134 vht_mcs = iterator.this_arg[4] >> 4;
2135 vht_nss = iterator.this_arg[4] & 0xF;
2136 break;
2137
Johannes Berg73b9f032011-10-07 14:01:26 +02002138 /*
2139 * Please update the file
2140 * Documentation/networking/mac80211-injection.txt
2141 * when parsing new fields here.
2142 */
2143
2144 default:
2145 break;
2146 }
2147 }
2148
2149 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
2150 return false;
2151
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002152 if (rate_found) {
2153 info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
2154
2155 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2156 info->control.rates[i].idx = -1;
2157 info->control.rates[i].flags = 0;
2158 info->control.rates[i].count = 0;
2159 }
2160
2161 if (rate_flags & IEEE80211_TX_RC_MCS) {
2162 info->control.rates[0].idx = rate;
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002163 } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
2164 ieee80211_rate_set_vht(info->control.rates, vht_mcs,
2165 vht_nss);
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002166 } else {
2167 for (i = 0; i < sband->n_bitrates; i++) {
2168 if (rate * 5 != sband->bitrates[i].bitrate)
2169 continue;
2170
2171 info->control.rates[0].idx = i;
2172 break;
2173 }
2174 }
2175
Felix Fietkau07310a62016-03-02 15:54:51 +01002176 if (info->control.rates[0].idx < 0)
2177 info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
2178
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002179 info->control.rates[0].flags = rate_flags;
2180 info->control.rates[0].count = min_t(u8, rate_retries + 1,
2181 local->hw.max_rate_tries);
2182 }
2183
Johannes Berg73b9f032011-10-07 14:01:26 +02002184 /*
2185 * remove the radiotap header
2186 * iterator->_max_length was sanity-checked against
2187 * skb->len by iterator init
2188 */
2189 skb_pull(skb, iterator._max_length);
2190
2191 return true;
2192}
2193
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +00002194netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
2195 struct net_device *dev)
Johannes Berge2ebc742007-07-27 15:43:22 +02002196{
2197 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg55de9082012-07-26 17:24:39 +02002198 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berge2ebc742007-07-27 15:43:22 +02002199 struct ieee80211_radiotap_header *prthdr =
2200 (struct ieee80211_radiotap_header *)skb->data;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002201 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002202 struct ieee80211_hdr *hdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002203 struct ieee80211_sub_if_data *tmp_sdata, *sdata;
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002204 struct cfg80211_chan_def *chandef;
Andy Green9b8a74e2007-07-27 15:43:24 +02002205 u16 len_rthdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002206 int hdrlen;
Johannes Berge2ebc742007-07-27 15:43:22 +02002207
Andy Green9b8a74e2007-07-27 15:43:24 +02002208 /* check for not even having the fixed radiotap header part */
2209 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2210 goto fail; /* too short to be possibly valid */
2211
2212 /* is it a header version we can trust to find length from? */
2213 if (unlikely(prthdr->it_version))
2214 goto fail; /* only version 0 is supported */
2215
2216 /* then there must be a radiotap header with a length we can use */
2217 len_rthdr = ieee80211_get_radiotap_len(skb->data);
2218
2219 /* does the skb contain enough to deliver on the alleged length? */
2220 if (unlikely(skb->len < len_rthdr))
2221 goto fail; /* skb too short for claimed rt header extent */
Johannes Berge2ebc742007-07-27 15:43:22 +02002222
Johannes Berge2ebc742007-07-27 15:43:22 +02002223 /*
2224 * fix up the pointers accounting for the radiotap
2225 * header still being in there. We are being given
2226 * a precooked IEEE80211 header so no need for
2227 * normal processing
2228 */
Andy Green9b8a74e2007-07-27 15:43:24 +02002229 skb_set_mac_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02002230 /*
Andy Green9b8a74e2007-07-27 15:43:24 +02002231 * these are just fixed to the end of the rt area since we
2232 * don't have any better information and at this point, nobody cares
Johannes Berge2ebc742007-07-27 15:43:22 +02002233 */
Andy Green9b8a74e2007-07-27 15:43:24 +02002234 skb_set_network_header(skb, len_rthdr);
2235 skb_set_transport_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02002236
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002237 if (skb->len < len_rthdr + 2)
2238 goto fail;
2239
2240 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2241 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2242
2243 if (skb->len < len_rthdr + hdrlen)
2244 goto fail;
2245
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002246 /*
2247 * Initialize skb->protocol if the injected frame is a data frame
2248 * carrying a rfc1042 header
2249 */
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002250 if (ieee80211_is_data(hdr->frame_control) &&
2251 skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2252 u8 *payload = (u8 *)hdr + hdrlen;
2253
Joe Perchesb203ca32012-05-08 18:56:52 +00002254 if (ether_addr_equal(payload, rfc1042_header))
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002255 skb->protocol = cpu_to_be16((payload[6] << 8) |
2256 payload[7]);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002257 }
2258
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002259 memset(info, 0, sizeof(*info));
2260
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002261 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
Johannes Berg73b9f032011-10-07 14:01:26 +02002262 IEEE80211_TX_CTL_INJECTED;
2263
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002264 rcu_read_lock();
2265
2266 /*
2267 * We process outgoing injected frames that have a local address
2268 * we handle as though they are non-injected frames.
2269 * This code here isn't entirely correct, the local MAC address
2270 * isn't always enough to find the interface to use; for proper
2271 * VLAN/WDS support we will need a different mechanism (which
2272 * likely isn't going to be monitor interfaces).
2273 */
2274 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2275
2276 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2277 if (!ieee80211_sdata_running(tmp_sdata))
2278 continue;
2279 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2280 tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2281 tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
2282 continue;
Joe Perchesb203ca32012-05-08 18:56:52 +00002283 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002284 sdata = tmp_sdata;
2285 break;
2286 }
2287 }
Johannes Berg7351c6b2009-11-19 01:08:30 +01002288
Johannes Berg55de9082012-07-26 17:24:39 +02002289 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2290 if (!chanctx_conf) {
2291 tmp_sdata = rcu_dereference(local->monitor_sdata);
2292 if (tmp_sdata)
2293 chanctx_conf =
2294 rcu_dereference(tmp_sdata->vif.chanctx_conf);
2295 }
Johannes Berg55de9082012-07-26 17:24:39 +02002296
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002297 if (chanctx_conf)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002298 chandef = &chanctx_conf->def;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002299 else if (!local->use_chanctx)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002300 chandef = &local->_oper_chandef;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002301 else
2302 goto fail_rcu;
Johannes Berg55de9082012-07-26 17:24:39 +02002303
2304 /*
2305 * Frame injection is not allowed if beaconing is not allowed
2306 * or if we need radar detection. Beaconing is usually not allowed when
2307 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2308 * Passive scan is also used in world regulatory domains where
2309 * your country is not known and as such it should be treated as
2310 * NO TX unless the channel is explicitly allowed in which case
2311 * your current regulatory domain would not have the passive scan
2312 * flag.
2313 *
2314 * Since AP mode uses monitor interfaces to inject/TX management
2315 * frames we can make AP mode the exception to this rule once it
2316 * supports radar detection as its implementation can deal with
2317 * radar detection by itself. We can do that later by adding a
2318 * monitor flag interfaces used for AP support.
2319 */
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002320 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2321 sdata->vif.type))
Johannes Berg55de9082012-07-26 17:24:39 +02002322 goto fail_rcu;
2323
Johannes Berg73c4e192014-11-09 18:50:09 +02002324 info->band = chandef->chan->band;
Lorenzo Bianconi109843b02016-02-19 12:18:01 +01002325
2326 /* process and remove the injection radiotap header */
2327 if (!ieee80211_parse_tx_radiotap(local, skb))
2328 goto fail_rcu;
2329
Johannes Bergb9771d42018-05-28 15:47:41 +02002330 ieee80211_xmit(sdata, NULL, skb, 0);
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002331 rcu_read_unlock();
2332
Johannes Berge2ebc742007-07-27 15:43:22 +02002333 return NETDEV_TX_OK;
Andy Green9b8a74e2007-07-27 15:43:24 +02002334
Johannes Berg55de9082012-07-26 17:24:39 +02002335fail_rcu:
2336 rcu_read_unlock();
Andy Green9b8a74e2007-07-27 15:43:24 +02002337fail:
2338 dev_kfree_skb(skb);
2339 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
Johannes Berge2ebc742007-07-27 15:43:22 +02002340}
2341
Johannes Berg97ffe752015-03-21 09:13:45 +01002342static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002343{
Johannes Berg97ffe752015-03-21 09:13:45 +01002344 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002345
Johannes Berg97ffe752015-03-21 09:13:45 +01002346 return ethertype == ETH_P_TDLS &&
2347 skb->len > 14 &&
2348 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2349}
2350
2351static int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2352 struct sk_buff *skb,
2353 struct sta_info **sta_out)
2354{
2355 struct sta_info *sta;
2356
2357 switch (sdata->vif.type) {
2358 case NL80211_IFTYPE_AP_VLAN:
2359 sta = rcu_dereference(sdata->u.vlan.sta);
2360 if (sta) {
2361 *sta_out = sta;
2362 return 0;
2363 } else if (sdata->wdev.use_4addr) {
2364 return -ENOLINK;
2365 }
2366 /* fall through */
2367 case NL80211_IFTYPE_AP:
2368 case NL80211_IFTYPE_OCB:
2369 case NL80211_IFTYPE_ADHOC:
2370 if (is_multicast_ether_addr(skb->data)) {
2371 *sta_out = ERR_PTR(-ENOENT);
2372 return 0;
2373 }
2374 sta = sta_info_get_bss(sdata, skb->data);
2375 break;
2376 case NL80211_IFTYPE_WDS:
2377 sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
2378 break;
2379#ifdef CONFIG_MAC80211_MESH
2380 case NL80211_IFTYPE_MESH_POINT:
2381 /* determined much later */
2382 *sta_out = NULL;
2383 return 0;
2384#endif
2385 case NL80211_IFTYPE_STATION:
2386 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2387 sta = sta_info_get(sdata, skb->data);
Johannes Berg11d62ca2016-09-13 08:28:22 +02002388 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2389 if (test_sta_flag(sta,
2390 WLAN_STA_TDLS_PEER_AUTH)) {
Johannes Berg97ffe752015-03-21 09:13:45 +01002391 *sta_out = sta;
2392 return 0;
2393 }
2394
2395 /*
2396 * TDLS link during setup - throw out frames to
2397 * peer. Allow TDLS-setup frames to unauthorized
2398 * peers for the special case of a link teardown
2399 * after a TDLS sta is removed due to being
2400 * unreachable.
2401 */
Johannes Berg11d62ca2016-09-13 08:28:22 +02002402 if (!ieee80211_is_tdls_setup(skb))
Johannes Berg97ffe752015-03-21 09:13:45 +01002403 return -EINVAL;
2404 }
2405
2406 }
2407
2408 sta = sta_info_get(sdata, sdata->u.mgd.bssid);
2409 if (!sta)
2410 return -ENOLINK;
2411 break;
2412 default:
2413 return -EINVAL;
2414 }
2415
2416 *sta_out = sta ?: ERR_PTR(-ENOENT);
2417 return 0;
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002418}
2419
Johannes Berge2ebc742007-07-27 15:43:22 +02002420/**
Johannes Berg4c9451e2014-11-09 18:50:10 +02002421 * ieee80211_build_hdr - build 802.11 header in the given frame
2422 * @sdata: virtual interface to build the header for
2423 * @skb: the skb to build the header in
Liad Kaufman24d342c2014-11-09 18:50:07 +02002424 * @info_flags: skb flags to set
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002425 * @ctrl_flags: info control flags to set
Johannes Berge2ebc742007-07-27 15:43:22 +02002426 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002427 * This function takes the skb with 802.3 header and reformats the header to
2428 * the appropriate IEEE 802.11 header based on which interface the packet is
2429 * being transmitted on.
Johannes Berge2ebc742007-07-27 15:43:22 +02002430 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002431 * Note that this function also takes care of the TX status request and
2432 * potential unsharing of the SKB - this needs to be interleaved with the
2433 * header building.
2434 *
2435 * The function requires the read-side RCU lock held
2436 *
2437 * Returns: the (possibly reallocated) skb or an ERR_PTR() code
Johannes Berge2ebc742007-07-27 15:43:22 +02002438 */
Johannes Berg4c9451e2014-11-09 18:50:10 +02002439static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01002440 struct sk_buff *skb, u32 info_flags,
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002441 struct sta_info *sta, u32 ctrl_flags)
Johannes Berge2ebc742007-07-27 15:43:22 +02002442{
Johannes Berg133b8222008-09-16 14:18:59 +02002443 struct ieee80211_local *local = sdata->local;
Felix Fietkau489ee912010-12-18 19:30:48 +01002444 struct ieee80211_tx_info *info;
Johannes Bergdcf33962012-07-30 15:11:56 +02002445 int head_need;
Harvey Harrison065e96052008-06-22 16:45:27 -07002446 u16 ethertype, hdrlen, meshhdrlen = 0;
2447 __le16 fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002448 struct ieee80211_hdr hdr;
Wey-Yi Guyff67bb82010-08-21 07:23:29 -07002449 struct ieee80211s_hdr mesh_hdr __maybe_unused;
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002450 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02002451 const u8 *encaps_data;
2452 int encaps_len, skip_header_bytes;
Johannes Berg97ffe752015-03-21 09:13:45 +01002453 bool wme_sta = false, authorized = false;
2454 bool tdls_peer;
Johannes Berga729cff2011-11-06 14:13:34 +01002455 bool multicast;
Johannes Berga729cff2011-11-06 14:13:34 +01002456 u16 info_id = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002457 struct ieee80211_chanctx_conf *chanctx_conf;
2458 struct ieee80211_sub_if_data *ap_sdata;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002459 enum nl80211_band band;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002460 int ret;
Johannes Berge2ebc742007-07-27 15:43:22 +02002461
Johannes Berg97ffe752015-03-21 09:13:45 +01002462 if (IS_ERR(sta))
2463 sta = NULL;
2464
Julius Niedworok276d9e82019-03-28 21:01:06 +01002465#ifdef CONFIG_MAC80211_DEBUGFS
2466 if (local->force_tx_status)
2467 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2468#endif
2469
Johannes Berge2ebc742007-07-27 15:43:22 +02002470 /* convert Ethernet header to proper 802.11 header (based on
2471 * operation mode) */
2472 ethertype = (skb->data[12] << 8) | skb->data[13];
Harvey Harrison065e96052008-06-22 16:45:27 -07002473 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
Johannes Berge2ebc742007-07-27 15:43:22 +02002474
Johannes Berg51fb61e2007-12-19 01:31:27 +01002475 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002476 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg97ffe752015-03-21 09:13:45 +01002477 if (sdata->wdev.use_4addr) {
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002478 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2479 /* RA TA DA SA */
2480 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002481 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002482 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2483 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2484 hdrlen = 30;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002485 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Johannes Berga74a8c82014-07-22 14:50:47 +02002486 wme_sta = sta->sta.wme;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002487 }
Johannes Berg55de9082012-07-26 17:24:39 +02002488 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2489 u.ap);
2490 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002491 if (!chanctx_conf) {
2492 ret = -ENOTCONN;
2493 goto free;
2494 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002495 band = chanctx_conf->def.chan->band;
Johannes Berg97ffe752015-03-21 09:13:45 +01002496 if (sdata->wdev.use_4addr)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002497 break;
2498 /* fall through */
2499 case NL80211_IFTYPE_AP:
Arnd Bergmannfe801232013-01-25 14:14:33 +00002500 if (sdata->vif.type == NL80211_IFTYPE_AP)
2501 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002502 if (!chanctx_conf) {
2503 ret = -ENOTCONN;
2504 goto free;
2505 }
Harvey Harrison065e96052008-06-22 16:45:27 -07002506 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
Johannes Berge2ebc742007-07-27 15:43:22 +02002507 /* DA BSSID SA */
2508 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002509 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002510 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2511 hdrlen = 24;
Johannes Berg4bf88532012-11-09 11:39:59 +01002512 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002513 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002514 case NL80211_IFTYPE_WDS:
Harvey Harrison065e96052008-06-22 16:45:27 -07002515 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
Johannes Berge2ebc742007-07-27 15:43:22 +02002516 /* RA TA DA SA */
2517 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002518 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002519 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2520 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2521 hdrlen = 30;
Johannes Berg55de9082012-07-26 17:24:39 +02002522 /*
2523 * This is the exception! WDS style interfaces are prohibited
2524 * when channel contexts are in used so this must be valid
2525 */
Karl Beldan675a0b02013-03-25 16:26:57 +01002526 band = local->hw.conf.chandef.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002527 break;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002528#ifdef CONFIG_MAC80211_MESH
Johannes Berg05c914f2008-09-11 00:01:58 +02002529 case NL80211_IFTYPE_MESH_POINT:
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002530 if (!is_multicast_ether_addr(skb->data)) {
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002531 struct sta_info *next_hop;
2532 bool mpp_lookup = true;
2533
Johannes Bergbf7cd942013-02-15 14:40:31 +01002534 mpath = mesh_path_lookup(sdata, skb->data);
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002535 if (mpath) {
2536 mpp_lookup = false;
2537 next_hop = rcu_dereference(mpath->next_hop);
2538 if (!next_hop ||
2539 !(mpath->flags & (MESH_PATH_ACTIVE |
2540 MESH_PATH_RESOLVING)))
2541 mpp_lookup = true;
2542 }
2543
Henning Roggeab1c7902016-02-03 13:58:37 +01002544 if (mpp_lookup) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002545 mppath = mpp_path_lookup(sdata, skb->data);
Henning Roggeab1c7902016-02-03 13:58:37 +01002546 if (mppath)
2547 mppath->exp_time = jiffies;
2548 }
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002549
2550 if (mppath && mpath)
Bob Copeland2bdaf382016-02-28 20:03:56 -05002551 mesh_path_del(sdata, mpath->dst);
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002552 }
YanBo79617de2008-09-22 13:30:32 +08002553
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002554 /*
Joel A Fernandes9d525012011-01-10 00:44:23 -06002555 * Use address extension if it is a packet from
2556 * another interface or if we know the destination
2557 * is being proxied by a portal (i.e. portal address
2558 * differs from proxied address)
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002559 */
Joe Perchesb203ca32012-05-08 18:56:52 +00002560 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2561 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
Javier Cardona3c5772a2009-08-10 12:15:48 -07002562 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2563 skb->data, skb->data + ETH_ALEN);
Johannes Bergbf7cd942013-02-15 14:40:31 +01002564 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2565 NULL, NULL);
YanBo79617de2008-09-22 13:30:32 +08002566 } else {
Thomas Pedersen27f01122012-08-20 11:28:25 -07002567 /* DS -> MBSS (802.11-2012 13.11.3.3).
2568 * For unicast with unknown forwarding information,
2569 * destination might be in the MBSS or if that fails
2570 * forwarded to another mesh gate. In either case
2571 * resolution will be handled in ieee80211_xmit(), so
2572 * leave the original DA. This also works for mcast */
2573 const u8 *mesh_da = skb->data;
YanBo79617de2008-09-22 13:30:32 +08002574
Thomas Pedersen27f01122012-08-20 11:28:25 -07002575 if (mppath)
2576 mesh_da = mppath->mpp;
2577 else if (mpath)
2578 mesh_da = mpath->dst;
Thomas Pedersen27f01122012-08-20 11:28:25 -07002579
Javier Cardona3c5772a2009-08-10 12:15:48 -07002580 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
Johannes Berg47846c92009-11-25 17:46:19 +01002581 mesh_da, sdata->vif.addr);
Thomas Pedersen27f01122012-08-20 11:28:25 -07002582 if (is_multicast_ether_addr(mesh_da))
2583 /* DA TA mSA AE:SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002584 meshhdrlen = ieee80211_new_mesh_header(
2585 sdata, &mesh_hdr,
2586 skb->data + ETH_ALEN, NULL);
Javier Cardona3c5772a2009-08-10 12:15:48 -07002587 else
Thomas Pedersen27f01122012-08-20 11:28:25 -07002588 /* RA TA mDA mSA AE:DA SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002589 meshhdrlen = ieee80211_new_mesh_header(
2590 sdata, &mesh_hdr, skb->data,
2591 skb->data + ETH_ALEN);
YanBo79617de2008-09-22 13:30:32 +08002592
YanBo79617de2008-09-22 13:30:32 +08002593 }
Johannes Berg55de9082012-07-26 17:24:39 +02002594 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002595 if (!chanctx_conf) {
2596 ret = -ENOTCONN;
2597 goto free;
2598 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002599 band = chanctx_conf->def.chan->band;
Rajkumar Manoharan8828f812019-04-11 13:47:26 -07002600
2601 /* For injected frames, fill RA right away as nexthop lookup
2602 * will be skipped.
2603 */
2604 if ((ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP) &&
2605 is_zero_ether_addr(hdr.addr1))
2606 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002607 break;
2608#endif
Johannes Berg05c914f2008-09-11 00:01:58 +02002609 case NL80211_IFTYPE_STATION:
Johannes Berg97ffe752015-03-21 09:13:45 +01002610 /* we already did checks when looking up the RA STA */
2611 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002612
Johannes Berg97ffe752015-03-21 09:13:45 +01002613 if (tdls_peer) {
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002614 /* DA SA BSSID */
2615 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2616 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2617 memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2618 hdrlen = 24;
2619 } else if (sdata->u.mgd.use_4addr &&
2620 cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2621 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2622 IEEE80211_FCTL_TODS);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002623 /* RA TA DA SA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002624 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002625 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002626 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2627 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2628 hdrlen = 30;
2629 } else {
2630 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2631 /* BSSID SA DA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002632 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002633 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2634 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2635 hdrlen = 24;
2636 }
Johannes Berg55de9082012-07-26 17:24:39 +02002637 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002638 if (!chanctx_conf) {
2639 ret = -ENOTCONN;
2640 goto free;
2641 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002642 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002643 break;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002644 case NL80211_IFTYPE_OCB:
2645 /* DA SA BSSID */
2646 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2647 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2648 eth_broadcast_addr(hdr.addr3);
2649 hdrlen = 24;
2650 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002651 if (!chanctx_conf) {
2652 ret = -ENOTCONN;
2653 goto free;
2654 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002655 band = chanctx_conf->def.chan->band;
2656 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002657 case NL80211_IFTYPE_ADHOC:
Johannes Berge2ebc742007-07-27 15:43:22 +02002658 /* DA SA BSSID */
2659 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2660 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01002661 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002662 hdrlen = 24;
Johannes Berg55de9082012-07-26 17:24:39 +02002663 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002664 if (!chanctx_conf) {
2665 ret = -ENOTCONN;
2666 goto free;
2667 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002668 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002669 break;
2670 default:
Johannes Berg4c9451e2014-11-09 18:50:10 +02002671 ret = -EINVAL;
2672 goto free;
Johannes Berge2ebc742007-07-27 15:43:22 +02002673 }
2674
Johannes Berga729cff2011-11-06 14:13:34 +01002675 multicast = is_multicast_ether_addr(hdr.addr1);
Johannes Berge2ebc742007-07-27 15:43:22 +02002676
Johannes Berg97ffe752015-03-21 09:13:45 +01002677 /* sta is always NULL for mesh */
2678 if (sta) {
2679 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2680 wme_sta = sta->sta.wme;
2681 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2682 /* For mesh, the use of the QoS header is mandatory */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002683 wme_sta = true;
Johannes Berge2ebc742007-07-27 15:43:22 +02002684 }
2685
Johannes Berg527871d2015-03-21 08:09:55 +01002686 /* receiver does QoS (which also means we do) use it */
2687 if (wme_sta) {
Harvey Harrison065e96052008-06-22 16:45:27 -07002688 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002689 hdrlen += 2;
2690 }
2691
2692 /*
Johannes Berg238814f2008-01-28 17:19:37 +01002693 * Drop unicast frames to unauthorised stations unless they are
2694 * EAPOL frames from the local station.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002695 */
Johannes Berg55182e42011-10-12 17:28:21 +02002696 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002697 (sdata->vif.type != NL80211_IFTYPE_OCB) &&
Sergey Ryazanova6ececf2013-08-30 01:35:09 +04002698 !multicast && !authorized &&
Johannes Berg55182e42011-10-12 17:28:21 +02002699 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002700 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002701#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002702 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
Johannes Berg4c9451e2014-11-09 18:50:10 +02002703 sdata->name, hdr.addr1);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002704#endif
2705
2706 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2707
Johannes Berg4c9451e2014-11-09 18:50:10 +02002708 ret = -EPERM;
2709 goto free;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002710 }
2711
Johannes Berga729cff2011-11-06 14:13:34 +01002712 if (unlikely(!multicast && skb->sk &&
2713 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)) {
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002714 struct sk_buff *ack_skb = skb_clone_sk(skb);
Johannes Berga729cff2011-11-06 14:13:34 +01002715
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002716 if (ack_skb) {
Johannes Berga729cff2011-11-06 14:13:34 +01002717 unsigned long flags;
Tejun Heo9475af62013-02-27 17:04:59 -08002718 int id;
Johannes Berga729cff2011-11-06 14:13:34 +01002719
2720 spin_lock_irqsave(&local->ack_status_lock, flags);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002721 id = idr_alloc(&local->ack_status_frames, ack_skb,
Tejun Heo9475af62013-02-27 17:04:59 -08002722 1, 0x10000, GFP_ATOMIC);
Johannes Berga729cff2011-11-06 14:13:34 +01002723 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2724
Tejun Heo9475af62013-02-27 17:04:59 -08002725 if (id >= 0) {
Johannes Berga729cff2011-11-06 14:13:34 +01002726 info_id = id;
2727 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Berga729cff2011-11-06 14:13:34 +01002728 } else {
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002729 kfree_skb(ack_skb);
Johannes Berga729cff2011-11-06 14:13:34 +01002730 }
Johannes Berga729cff2011-11-06 14:13:34 +01002731 }
2732 }
2733
Helmut Schaa7e244702010-12-02 18:44:09 +01002734 /*
2735 * If the skb is shared we need to obtain our own copy.
2736 */
2737 if (skb_shared(skb)) {
Johannes Berga729cff2011-11-06 14:13:34 +01002738 struct sk_buff *tmp_skb = skb;
2739
2740 /* can't happen -- skb is a clone if info_id != 0 */
2741 WARN_ON(info_id);
2742
Felix Fietkauf8a0a782010-12-18 19:30:50 +01002743 skb = skb_clone(skb, GFP_ATOMIC);
Helmut Schaa7e244702010-12-02 18:44:09 +01002744 kfree_skb(tmp_skb);
2745
Johannes Berg4c9451e2014-11-09 18:50:10 +02002746 if (!skb) {
2747 ret = -ENOMEM;
2748 goto free;
2749 }
Helmut Schaa7e244702010-12-02 18:44:09 +01002750 }
2751
Harvey Harrison065e96052008-06-22 16:45:27 -07002752 hdr.frame_control = fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002753 hdr.duration_id = 0;
2754 hdr.seq_ctrl = 0;
2755
2756 skip_header_bytes = ETH_HLEN;
2757 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2758 encaps_data = bridge_tunnel_header;
2759 encaps_len = sizeof(bridge_tunnel_header);
2760 skip_header_bytes -= 2;
Simon Hormane5c5d222013-03-28 13:38:25 +09002761 } else if (ethertype >= ETH_P_802_3_MIN) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002762 encaps_data = rfc1042_header;
2763 encaps_len = sizeof(rfc1042_header);
2764 skip_header_bytes -= 2;
2765 } else {
2766 encaps_data = NULL;
2767 encaps_len = 0;
2768 }
2769
2770 skb_pull(skb, skip_header_bytes);
Johannes Berg23c07522008-05-29 10:38:53 +02002771 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002772
Johannes Berg23c07522008-05-29 10:38:53 +02002773 /*
2774 * So we need to modify the skb header and hence need a copy of
2775 * that. The head_need variable above doesn't, so far, include
2776 * the needed header space that we don't need right away. If we
2777 * can, then we don't reallocate right now but only after the
2778 * frame arrives at the master device (if it does...)
2779 *
2780 * If we cannot, however, then we will reallocate to include all
2781 * the ever needed space. Also, if we need to reallocate it anyway,
2782 * make it big enough for everything we may ever need.
2783 */
Johannes Berge2ebc742007-07-27 15:43:22 +02002784
David S. Miller3a5be7d2008-06-18 01:19:51 -07002785 if (head_need > 0 || skb_cloned(skb)) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002786 head_need += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02002787 head_need += local->tx_headroom;
2788 head_need = max_t(int, 0, head_need);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002789 if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
2790 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg1c963be2012-11-07 14:02:30 +01002791 skb = NULL;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002792 return ERR_PTR(-ENOMEM);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002793 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002794 }
2795
Felix Fietkaueae44302016-07-13 11:00:02 +02002796 if (encaps_data)
Johannes Berge2ebc742007-07-27 15:43:22 +02002797 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002798
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002799#ifdef CONFIG_MAC80211_MESH
Felix Fietkaueae44302016-07-13 11:00:02 +02002800 if (meshhdrlen > 0)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002801 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002802#endif
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002803
Harvey Harrison065e96052008-06-22 16:45:27 -07002804 if (ieee80211_is_data_qos(fc)) {
Johannes Bergc29b9b92007-09-14 11:10:24 -04002805 __le16 *qos_control;
2806
Johannes Bergd58ff352017-06-16 14:29:23 +02002807 qos_control = skb_push(skb, 2);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002808 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2809 /*
2810 * Maybe we could actually set some fields here, for now just
2811 * initialise to zero to indicate no special operation.
2812 */
2813 *qos_control = 0;
2814 } else
2815 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2816
Zhang Shengjud57a5442016-03-03 01:16:56 +00002817 skb_reset_mac_header(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002818
Felix Fietkau489ee912010-12-18 19:30:48 +01002819 info = IEEE80211_SKB_CB(skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002820 memset(info, 0, sizeof(*info));
2821
Johannes Berga729cff2011-11-06 14:13:34 +01002822 info->flags = info_flags;
2823 info->ack_frame_id = info_id;
Johannes Berg73c4e192014-11-09 18:50:09 +02002824 info->band = band;
Rajkumar Manoharan06016772019-04-11 13:47:25 -07002825 info->control.flags = ctrl_flags;
Johannes Berga729cff2011-11-06 14:13:34 +01002826
Johannes Berg4c9451e2014-11-09 18:50:10 +02002827 return skb;
2828 free:
2829 kfree_skb(skb);
2830 return ERR_PTR(ret);
Johannes Berge2ebc742007-07-27 15:43:22 +02002831}
2832
Johannes Berg17c18bf2015-03-21 15:25:43 +01002833/*
2834 * fast-xmit overview
2835 *
2836 * The core idea of this fast-xmit is to remove per-packet checks by checking
2837 * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2838 * checks that are needed to get the sta->fast_tx pointer assigned, after which
2839 * much less work can be done per packet. For example, fragmentation must be
2840 * disabled or the fast_tx pointer will not be set. All the conditions are seen
2841 * in the code here.
2842 *
2843 * Once assigned, the fast_tx data structure also caches the per-packet 802.11
2844 * header and other data to aid packet processing in ieee80211_xmit_fast().
2845 *
2846 * The most difficult part of this is that when any of these assumptions
2847 * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
2848 * ieee80211_check_fast_xmit() or friends) is required to reset the data,
2849 * since the per-packet code no longer checks the conditions. This is reflected
2850 * by the calls to these functions throughout the rest of the code, and must be
2851 * maintained if any of the TX path checks change.
2852 */
2853
2854void ieee80211_check_fast_xmit(struct sta_info *sta)
2855{
2856 struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
2857 struct ieee80211_local *local = sta->local;
2858 struct ieee80211_sub_if_data *sdata = sta->sdata;
2859 struct ieee80211_hdr *hdr = (void *)build.hdr;
2860 struct ieee80211_chanctx_conf *chanctx_conf;
2861 __le16 fc;
2862
Johannes Berg30686bf2015-06-02 21:39:54 +02002863 if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002864 return;
2865
2866 /* Locking here protects both the pointer itself, and against concurrent
2867 * invocations winning data access races to, e.g., the key pointer that
2868 * is used.
2869 * Without it, the invocation of this function right after the key
2870 * pointer changes wouldn't be sufficient, as another CPU could access
2871 * the pointer, then stall, and then do the cache update after the CPU
2872 * that invalidated the key.
2873 * With the locking, such scenarios cannot happen as the check for the
2874 * key and the fast-tx assignment are done atomically, so the CPU that
2875 * modifies the key will either wait or other one will see the key
2876 * cleared/changed already.
2877 */
2878 spin_lock_bh(&sta->lock);
Johannes Berg30686bf2015-06-02 21:39:54 +02002879 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
2880 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
Johannes Berg17c18bf2015-03-21 15:25:43 +01002881 sdata->vif.type == NL80211_IFTYPE_STATION)
2882 goto out;
2883
2884 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2885 goto out;
2886
2887 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
2888 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
Felix Fietkauf7418bc2015-09-24 14:59:49 +02002889 test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
2890 test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002891 goto out;
2892
2893 if (sdata->noack_map)
2894 goto out;
2895
2896 /* fast-xmit doesn't handle fragmentation at all */
Johannes Berg725b8122015-04-10 14:02:08 +02002897 if (local->hw.wiphy->frag_threshold != (u32)-1 &&
Sara Sharonf3fe4e92016-10-18 23:12:11 +03002898 !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002899 goto out;
2900
2901 rcu_read_lock();
2902 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2903 if (!chanctx_conf) {
2904 rcu_read_unlock();
2905 goto out;
2906 }
2907 build.band = chanctx_conf->def.chan->band;
2908 rcu_read_unlock();
2909
2910 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2911
2912 switch (sdata->vif.type) {
Johannes Berg3ffd8842015-04-14 10:28:37 +02002913 case NL80211_IFTYPE_ADHOC:
2914 /* DA SA BSSID */
2915 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2916 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2917 memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
2918 build.hdr_len = 24;
2919 break;
Johannes Berg17c18bf2015-03-21 15:25:43 +01002920 case NL80211_IFTYPE_STATION:
2921 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2922 /* DA SA BSSID */
2923 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2924 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2925 memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
2926 build.hdr_len = 24;
2927 break;
2928 }
2929
2930 if (sdata->u.mgd.use_4addr) {
2931 /* non-regular ethertype cannot use the fastpath */
2932 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2933 IEEE80211_FCTL_TODS);
2934 /* RA TA DA SA */
2935 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2936 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2937 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2938 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2939 build.hdr_len = 30;
2940 break;
2941 }
2942 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2943 /* BSSID SA DA */
2944 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2945 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2946 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2947 build.hdr_len = 24;
2948 break;
2949 case NL80211_IFTYPE_AP_VLAN:
2950 if (sdata->wdev.use_4addr) {
2951 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2952 IEEE80211_FCTL_TODS);
2953 /* RA TA DA SA */
2954 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
2955 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2956 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2957 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2958 build.hdr_len = 30;
2959 break;
2960 }
2961 /* fall through */
2962 case NL80211_IFTYPE_AP:
2963 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2964 /* DA BSSID SA */
2965 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2966 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2967 build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
2968 build.hdr_len = 24;
2969 break;
2970 default:
2971 /* not handled on fast-xmit */
2972 goto out;
2973 }
2974
2975 if (sta->sta.wme) {
2976 build.hdr_len += 2;
2977 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2978 }
2979
2980 /* We store the key here so there's no point in using rcu_dereference()
2981 * but that's fine because the code that changes the pointers will call
2982 * this function after doing so. For a single CPU that would be enough,
2983 * for multiple see the comment above.
2984 */
2985 build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
2986 if (!build.key)
2987 build.key = rcu_access_pointer(sdata->default_unicast_key);
2988 if (build.key) {
Johannes Berge495c242015-04-10 14:03:17 +02002989 bool gen_iv, iv_spc, mmic;
Johannes Berg17c18bf2015-03-21 15:25:43 +01002990
2991 gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
2992 iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
David Spinadel9de18d82017-12-01 13:50:52 +02002993 mmic = build.key->conf.flags &
2994 (IEEE80211_KEY_FLAG_GENERATE_MMIC |
2995 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
Johannes Berg17c18bf2015-03-21 15:25:43 +01002996
2997 /* don't handle software crypto */
2998 if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
2999 goto out;
3000
Alexander Wetzel62872a92018-08-31 15:00:38 +02003001 /* Key is being removed */
3002 if (build.key->flags & KEY_FLAG_TAINTED)
3003 goto out;
3004
Johannes Berg17c18bf2015-03-21 15:25:43 +01003005 switch (build.key->conf.cipher) {
3006 case WLAN_CIPHER_SUITE_CCMP:
3007 case WLAN_CIPHER_SUITE_CCMP_256:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003008 if (gen_iv)
Johannes Berg17c18bf2015-03-21 15:25:43 +01003009 build.pn_offs = build.hdr_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003010 if (gen_iv || iv_spc)
3011 build.hdr_len += IEEE80211_CCMP_HDR_LEN;
3012 break;
3013 case WLAN_CIPHER_SUITE_GCMP:
3014 case WLAN_CIPHER_SUITE_GCMP_256:
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003015 if (gen_iv)
Johannes Berg17c18bf2015-03-21 15:25:43 +01003016 build.pn_offs = build.hdr_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003017 if (gen_iv || iv_spc)
3018 build.hdr_len += IEEE80211_GCMP_HDR_LEN;
3019 break;
Johannes Berge495c242015-04-10 14:03:17 +02003020 case WLAN_CIPHER_SUITE_TKIP:
3021 /* cannot handle MMIC or IV generation in xmit-fast */
3022 if (mmic || gen_iv)
3023 goto out;
3024 if (iv_spc)
3025 build.hdr_len += IEEE80211_TKIP_IV_LEN;
3026 break;
3027 case WLAN_CIPHER_SUITE_WEP40:
3028 case WLAN_CIPHER_SUITE_WEP104:
3029 /* cannot handle IV generation in fast-xmit */
3030 if (gen_iv)
3031 goto out;
3032 if (iv_spc)
3033 build.hdr_len += IEEE80211_WEP_IV_LEN;
3034 break;
3035 case WLAN_CIPHER_SUITE_AES_CMAC:
3036 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3037 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3038 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3039 WARN(1,
3040 "management cipher suite 0x%x enabled for data\n",
3041 build.key->conf.cipher);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003042 goto out;
Johannes Berge495c242015-04-10 14:03:17 +02003043 default:
3044 /* we don't know how to generate IVs for this at all */
3045 if (WARN_ON(gen_iv))
3046 goto out;
3047 /* pure hardware keys are OK, of course */
3048 if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
3049 break;
3050 /* cipher scheme might require space allocation */
3051 if (iv_spc &&
3052 build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
3053 goto out;
3054 if (iv_spc)
3055 build.hdr_len += build.key->conf.iv_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003056 }
3057
3058 fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3059 }
3060
3061 hdr->frame_control = fc;
3062
3063 memcpy(build.hdr + build.hdr_len,
3064 rfc1042_header, sizeof(rfc1042_header));
3065 build.hdr_len += sizeof(rfc1042_header);
3066
3067 fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3068 /* if the kmemdup fails, continue w/o fast_tx */
3069 if (!fast_tx)
3070 goto out;
3071
3072 out:
3073 /* we might have raced against another call to this function */
3074 old = rcu_dereference_protected(sta->fast_tx,
3075 lockdep_is_held(&sta->lock));
3076 rcu_assign_pointer(sta->fast_tx, fast_tx);
3077 if (old)
3078 kfree_rcu(old, rcu_head);
3079 spin_unlock_bh(&sta->lock);
3080}
3081
3082void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3083{
3084 struct sta_info *sta;
3085
3086 rcu_read_lock();
3087 list_for_each_entry_rcu(sta, &local->sta_list, list)
3088 ieee80211_check_fast_xmit(sta);
3089 rcu_read_unlock();
3090}
3091
3092void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3093{
3094 struct ieee80211_local *local = sdata->local;
3095 struct sta_info *sta;
3096
3097 rcu_read_lock();
3098
3099 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3100 if (sdata != sta->sdata &&
3101 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3102 continue;
3103 ieee80211_check_fast_xmit(sta);
3104 }
3105
3106 rcu_read_unlock();
3107}
3108
3109void ieee80211_clear_fast_xmit(struct sta_info *sta)
3110{
3111 struct ieee80211_fast_tx *fast_tx;
3112
3113 spin_lock_bh(&sta->lock);
3114 fast_tx = rcu_dereference_protected(sta->fast_tx,
3115 lockdep_is_held(&sta->lock));
3116 RCU_INIT_POINTER(sta->fast_tx, NULL);
3117 spin_unlock_bh(&sta->lock);
3118
3119 if (fast_tx)
3120 kfree_rcu(fast_tx, rcu_head);
3121}
3122
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003123static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
Sara Sharon166ac9d2018-08-29 08:57:02 +02003124 struct sk_buff *skb, int headroom)
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003125{
Sara Sharon166ac9d2018-08-29 08:57:02 +02003126 if (skb_headroom(skb) < headroom) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003127 I802_DEBUG_INC(local->tx_expand_skb_head);
3128
Sara Sharon166ac9d2018-08-29 08:57:02 +02003129 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003130 wiphy_debug(local->hw.wiphy,
3131 "failed to reallocate TX buffer\n");
3132 return false;
3133 }
3134 }
3135
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003136 return true;
3137}
3138
3139static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3140 struct ieee80211_fast_tx *fast_tx,
3141 struct sk_buff *skb)
3142{
3143 struct ieee80211_local *local = sdata->local;
3144 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3145 struct ieee80211_hdr *hdr;
Michael Braun06f2bb12016-10-15 13:28:18 +02003146 struct ethhdr *amsdu_hdr;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003147 int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3148 int subframe_len = skb->len - hdr_len;
3149 void *data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003150 u8 *qc, *h_80211_src, *h_80211_dst;
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003151 const u8 *bssid;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003152
3153 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3154 return false;
3155
3156 if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3157 return true;
3158
Sara Sharon166ac9d2018-08-29 08:57:02 +02003159 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(*amsdu_hdr)))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003160 return false;
3161
Michael Braun06f2bb12016-10-15 13:28:18 +02003162 data = skb_push(skb, sizeof(*amsdu_hdr));
3163 memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003164 hdr = data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003165 amsdu_hdr = data + hdr_len;
3166 /* h_80211_src/dst is addr* field within hdr */
3167 h_80211_src = data + fast_tx->sa_offs;
3168 h_80211_dst = data + fast_tx->da_offs;
3169
3170 amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3171 ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3172 ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
3173
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003174 /* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3175 * fields needs to be changed to BSSID for A-MSDU frames depending
3176 * on FromDS/ToDS values.
3177 */
3178 switch (sdata->vif.type) {
3179 case NL80211_IFTYPE_STATION:
3180 bssid = sdata->u.mgd.bssid;
3181 break;
3182 case NL80211_IFTYPE_AP:
3183 case NL80211_IFTYPE_AP_VLAN:
3184 bssid = sdata->vif.addr;
3185 break;
3186 default:
3187 bssid = NULL;
3188 }
3189
3190 if (bssid && ieee80211_has_fromds(hdr->frame_control))
3191 ether_addr_copy(h_80211_src, bssid);
3192
3193 if (bssid && ieee80211_has_tods(hdr->frame_control))
3194 ether_addr_copy(h_80211_dst, bssid);
3195
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003196 qc = ieee80211_get_qos_ctl(hdr);
3197 *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3198
3199 info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3200
3201 return true;
3202}
3203
3204static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3205 struct sta_info *sta,
3206 struct ieee80211_fast_tx *fast_tx,
3207 struct sk_buff *skb)
3208{
3209 struct ieee80211_local *local = sdata->local;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003210 struct fq *fq = &local->fq;
3211 struct fq_tin *tin;
3212 struct fq_flow *flow;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003213 u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3214 struct ieee80211_txq *txq = sta->sta.txq[tid];
3215 struct txq_info *txqi;
3216 struct sk_buff **frag_tail, *head;
3217 int subframe_len = skb->len - ETH_ALEN;
3218 u8 max_subframes = sta->sta.max_amsdu_subframes;
3219 int max_frags = local->hw.max_tx_fragments;
3220 int max_amsdu_len = sta->sta.max_amsdu_len;
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003221 int orig_truesize;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003222 u32 flow_idx;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003223 __be16 len;
3224 void *data;
3225 bool ret = false;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003226 unsigned int orig_len;
Lorenzo Bianconi66eb02d2018-08-31 01:04:13 +02003227 int n = 2, nfrags, pad = 0;
Sara Sharon166ac9d2018-08-29 08:57:02 +02003228 u16 hdrlen;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003229
3230 if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
3231 return false;
3232
Sara Sharon344f8e02018-12-15 11:03:07 +02003233 if (skb_is_gso(skb))
3234 return false;
3235
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003236 if (!txq)
3237 return false;
3238
3239 txqi = to_txq_info(txq);
3240 if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3241 return false;
3242
3243 if (sta->sta.max_rc_amsdu_len)
3244 max_amsdu_len = min_t(int, max_amsdu_len,
3245 sta->sta.max_rc_amsdu_len);
3246
Sara Sharonedba6bd2018-09-05 08:06:10 +03003247 if (sta->sta.max_tid_amsdu_len[tid])
3248 max_amsdu_len = min_t(int, max_amsdu_len,
3249 sta->sta.max_tid_amsdu_len[tid]);
3250
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003251 flow_idx = fq_flow_idx(fq, skb);
3252
Michal Kaziorfa962b92016-05-19 10:37:49 +02003253 spin_lock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003254
Michal Kaziorfa962b92016-05-19 10:37:49 +02003255 /* TODO: Ideally aggregation should be done on dequeue to remain
3256 * responsive to environment changes.
3257 */
3258
3259 tin = &txqi->tin;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003260 flow = fq_flow_classify(fq, tin, flow_idx, skb,
3261 fq_flow_get_default_func);
Michal Kaziorfa962b92016-05-19 10:37:49 +02003262 head = skb_peek_tail(&flow->queue);
Sara Sharon344f8e02018-12-15 11:03:07 +02003263 if (!head || skb_is_gso(head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003264 goto out;
3265
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003266 orig_truesize = head->truesize;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003267 orig_len = head->len;
3268
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003269 if (skb->len + head->len > max_amsdu_len)
3270 goto out;
3271
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003272 nfrags = 1 + skb_shinfo(skb)->nr_frags;
3273 nfrags += 1 + skb_shinfo(head)->nr_frags;
3274 frag_tail = &skb_shinfo(head)->frag_list;
3275 while (*frag_tail) {
3276 nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3277 frag_tail = &(*frag_tail)->next;
3278 n++;
3279 }
3280
3281 if (max_subframes && n > max_subframes)
3282 goto out;
3283
3284 if (max_frags && nfrags > max_frags)
3285 goto out;
3286
Sara Sharon9739fe22018-09-05 08:06:11 +03003287 if (!drv_can_aggregate_in_amsdu(local, head, skb))
3288 goto out;
3289
Lorenzo Bianconi1eb50792018-08-29 21:03:25 +02003290 if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003291 goto out;
3292
Sara Sharon166ac9d2018-08-29 08:57:02 +02003293 /*
3294 * Pad out the previous subframe to a multiple of 4 by adding the
3295 * padding to the next one, that's being added. Note that head->len
3296 * is the length of the full A-MSDU, but that works since each time
3297 * we add a new subframe we pad out the previous one to a multiple
3298 * of 4 and thus it no longer matters in the next round.
3299 */
3300 hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3301 if ((head->len - hdrlen) & 3)
3302 pad = 4 - ((head->len - hdrlen) & 3);
3303
3304 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3305 2 + pad))
Johannes Bergaa58acf2018-08-30 10:55:49 +02003306 goto out_recalc;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003307
3308 ret = true;
3309 data = skb_push(skb, ETH_ALEN + 2);
3310 memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3311
3312 data += 2 * ETH_ALEN;
3313 len = cpu_to_be16(subframe_len);
3314 memcpy(data, &len, 2);
3315 memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3316
Sara Sharon166ac9d2018-08-29 08:57:02 +02003317 memset(skb_push(skb, pad), 0, pad);
3318
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003319 head->len += skb->len;
3320 head->data_len += skb->len;
3321 *frag_tail = skb;
3322
Johannes Bergaa58acf2018-08-30 10:55:49 +02003323out_recalc:
Felix Fietkaueb9b64e2019-03-16 18:06:31 +01003324 fq->memory_usage += head->truesize - orig_truesize;
Johannes Bergaa58acf2018-08-30 10:55:49 +02003325 if (head->len != orig_len) {
3326 flow->backlog += head->len - orig_len;
3327 tin->backlog_bytes += head->len - orig_len;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003328
Johannes Bergaa58acf2018-08-30 10:55:49 +02003329 fq_recalc_backlog(fq, tin, flow);
3330 }
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003331out:
Michal Kaziorfa962b92016-05-19 10:37:49 +02003332 spin_unlock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003333
3334 return ret;
3335}
3336
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003337/*
3338 * Can be called while the sta lock is held. Anything that can cause packets to
3339 * be generated will cause deadlock!
3340 */
3341static void ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3342 struct sta_info *sta, u8 pn_offs,
3343 struct ieee80211_key *key,
3344 struct sk_buff *skb)
3345{
3346 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3347 struct ieee80211_hdr *hdr = (void *)skb->data;
3348 u8 tid = IEEE80211_NUM_TIDS;
3349
3350 if (key)
3351 info->control.hw_key = &key->conf;
3352
3353 ieee80211_tx_stats(skb->dev, skb->len);
3354
3355 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3356 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003357 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3358 } else {
3359 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3360 hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3361 sdata->sequence_number += 0x10;
3362 }
3363
3364 if (skb_shinfo(skb)->gso_size)
3365 sta->tx_stats.msdu[tid] +=
3366 DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3367 else
3368 sta->tx_stats.msdu[tid]++;
3369
3370 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3371
3372 /* statistics normally done by ieee80211_tx_h_stats (but that
3373 * has to consider fragmentation, so is more complex)
3374 */
3375 sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3376 sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
3377
3378 if (pn_offs) {
3379 u64 pn;
3380 u8 *crypto_hdr = skb->data + pn_offs;
3381
3382 switch (key->conf.cipher) {
3383 case WLAN_CIPHER_SUITE_CCMP:
3384 case WLAN_CIPHER_SUITE_CCMP_256:
3385 case WLAN_CIPHER_SUITE_GCMP:
3386 case WLAN_CIPHER_SUITE_GCMP_256:
3387 pn = atomic64_inc_return(&key->conf.tx_pn);
3388 crypto_hdr[0] = pn;
3389 crypto_hdr[1] = pn >> 8;
Alexander Wetzel96fc6ef2019-03-19 21:34:08 +01003390 crypto_hdr[3] = 0x20 | (key->conf.keyidx << 6);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003391 crypto_hdr[4] = pn >> 16;
3392 crypto_hdr[5] = pn >> 24;
3393 crypto_hdr[6] = pn >> 32;
3394 crypto_hdr[7] = pn >> 40;
3395 break;
3396 }
3397 }
3398}
3399
Johannes Berg17c18bf2015-03-21 15:25:43 +01003400static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003401 struct sta_info *sta,
Johannes Berg17c18bf2015-03-21 15:25:43 +01003402 struct ieee80211_fast_tx *fast_tx,
3403 struct sk_buff *skb)
3404{
3405 struct ieee80211_local *local = sdata->local;
3406 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3407 int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3408 int hw_headroom = sdata->local->hw.extra_tx_headroom;
3409 struct ethhdr eth;
Johannes Berg35f432a2017-01-02 11:19:29 +01003410 struct ieee80211_tx_info *info;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003411 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3412 struct ieee80211_tx_data tx;
3413 ieee80211_tx_result r;
3414 struct tid_ampdu_tx *tid_tx = NULL;
3415 u8 tid = IEEE80211_NUM_TIDS;
3416
3417 /* control port protocol needs a lot of special handling */
3418 if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3419 return false;
3420
3421 /* only RFC 1042 SNAP */
3422 if (ethertype < ETH_P_802_3_MIN)
3423 return false;
3424
3425 /* don't handle TX status request here either */
3426 if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3427 return false;
3428
3429 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3430 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3431 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
Johannes Berg472be002015-06-09 16:45:08 +02003432 if (tid_tx) {
3433 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3434 return false;
3435 if (tid_tx->timeout)
3436 tid_tx->last_tx = jiffies;
3437 }
Johannes Berg17c18bf2015-03-21 15:25:43 +01003438 }
3439
3440 /* after this point (skb is modified) we cannot return false */
3441
3442 if (skb_shared(skb)) {
3443 struct sk_buff *tmp_skb = skb;
3444
3445 skb = skb_clone(skb, GFP_ATOMIC);
3446 kfree_skb(tmp_skb);
3447
3448 if (!skb)
3449 return true;
3450 }
3451
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003452 if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3453 ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
3454 return true;
3455
Johannes Berg17c18bf2015-03-21 15:25:43 +01003456 /* will not be crypto-handled beyond what we do here, so use false
3457 * as the may-encrypt argument for the resize to not account for
3458 * more room than we already have in 'extra_head'
3459 */
3460 if (unlikely(ieee80211_skb_resize(sdata, skb,
3461 max_t(int, extra_head + hw_headroom -
3462 skb_headroom(skb), 0),
3463 false))) {
3464 kfree_skb(skb);
3465 return true;
3466 }
3467
3468 memcpy(&eth, skb->data, ETH_HLEN - 2);
Johannes Bergd58ff352017-06-16 14:29:23 +02003469 hdr = skb_push(skb, extra_head);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003470 memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3471 memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
3472 memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
3473
Johannes Berg35f432a2017-01-02 11:19:29 +01003474 info = IEEE80211_SKB_CB(skb);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003475 memset(info, 0, sizeof(*info));
3476 info->band = fast_tx->band;
3477 info->control.vif = &sdata->vif;
3478 info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3479 IEEE80211_TX_CTL_DONTFRAG |
3480 (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003481 info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003482
Julius Niedworok276d9e82019-03-28 21:01:06 +01003483#ifdef CONFIG_MAC80211_DEBUGFS
3484 if (local->force_tx_status)
3485 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3486#endif
3487
Felix Fietkaua786f962016-11-04 10:27:54 +01003488 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3489 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3490 *ieee80211_get_qos_ctl(hdr) = tid;
3491 }
3492
Johannes Berg17c18bf2015-03-21 15:25:43 +01003493 __skb_queue_head_init(&tx.skbs);
3494
3495 tx.flags = IEEE80211_TX_UNICAST;
3496 tx.local = local;
3497 tx.sdata = sdata;
3498 tx.sta = sta;
3499 tx.key = fast_tx->key;
3500
Johannes Berg30686bf2015-06-02 21:39:54 +02003501 if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01003502 tx.skb = skb;
3503 r = ieee80211_tx_h_rate_ctrl(&tx);
3504 skb = tx.skb;
3505 tx.skb = NULL;
3506
3507 if (r != TX_CONTINUE) {
3508 if (r != TX_QUEUED)
3509 kfree_skb(skb);
3510 return true;
3511 }
3512 }
3513
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003514 if (ieee80211_queue_skb(local, sdata, sta, skb))
3515 return true;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003516
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003517 ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3518 fast_tx->key, skb);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003519
3520 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3521 sdata = container_of(sdata->bss,
3522 struct ieee80211_sub_if_data, u.ap);
3523
3524 __skb_queue_tail(&tx.skbs, skb);
Johannes Berg4fd03282019-10-01 23:26:35 +02003525 ieee80211_tx_frags(local, &sdata->vif, sta, &tx.skbs, false);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003526 return true;
3527}
3528
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003529struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3530 struct ieee80211_txq *txq)
3531{
3532 struct ieee80211_local *local = hw_to_local(hw);
3533 struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3534 struct ieee80211_hdr *hdr;
3535 struct sk_buff *skb = NULL;
3536 struct fq *fq = &local->fq;
3537 struct fq_tin *tin = &txqi->tin;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003538 struct ieee80211_tx_info *info;
3539 struct ieee80211_tx_data tx;
3540 ieee80211_tx_result r;
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303541 struct ieee80211_vif *vif = txq->vif;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003542
Erik Stromdahlfb0e76a2019-06-17 22:01:39 +02003543 WARN_ON_ONCE(softirq_count() == 0);
3544
Felix Fietkauded46982019-03-16 18:06:33 +01003545begin:
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003546 spin_lock_bh(&fq->lock);
3547
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303548 if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags) ||
3549 test_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags))
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003550 goto out;
3551
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303552 if (vif->txqs_stopped[ieee80211_ac_from_tid(txq->tid)]) {
3553 set_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags);
3554 goto out;
3555 }
3556
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003557 /* Make sure fragments stay together. */
3558 skb = __skb_dequeue(&txqi->frags);
3559 if (skb)
3560 goto out;
3561
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003562 skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
3563 if (!skb)
3564 goto out;
3565
Felix Fietkauded46982019-03-16 18:06:33 +01003566 spin_unlock_bh(&fq->lock);
3567
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003568 hdr = (struct ieee80211_hdr *)skb->data;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003569 info = IEEE80211_SKB_CB(skb);
3570
3571 memset(&tx, 0, sizeof(tx));
3572 __skb_queue_head_init(&tx.skbs);
3573 tx.local = local;
3574 tx.skb = skb;
3575 tx.sdata = vif_to_sdata(info->control.vif);
3576
3577 if (txq->sta)
3578 tx.sta = container_of(txq->sta, struct sta_info, sta);
3579
3580 /*
3581 * The key can be removed while the packet was queued, so need to call
3582 * this here to get the current key.
3583 */
3584 r = ieee80211_tx_h_select_key(&tx);
3585 if (r != TX_CONTINUE) {
3586 ieee80211_free_txskb(&local->hw, skb);
3587 goto begin;
3588 }
3589
Felix Fietkauc1f4c9e2016-11-04 10:27:52 +01003590 if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
3591 info->flags |= IEEE80211_TX_CTL_AMPDU;
3592 else
3593 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
3594
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003595 if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003596 struct sta_info *sta = container_of(txq->sta, struct sta_info,
3597 sta);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003598 u8 pn_offs = 0;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003599
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003600 if (tx.key &&
3601 (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
3602 pn_offs = ieee80211_hdrlen(hdr->frame_control);
3603
3604 ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
3605 tx.key, skb);
3606 } else {
3607 if (invoke_tx_handlers_late(&tx))
3608 goto begin;
3609
3610 skb = __skb_dequeue(&tx.skbs);
3611
Felix Fietkauded46982019-03-16 18:06:33 +01003612 if (!skb_queue_empty(&tx.skbs)) {
3613 spin_lock_bh(&fq->lock);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003614 skb_queue_splice_tail(&tx.skbs, &txqi->frags);
Felix Fietkauded46982019-03-16 18:06:33 +01003615 spin_unlock_bh(&fq->lock);
3616 }
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003617 }
3618
Johannes Berg233e98d2018-12-15 11:03:09 +02003619 if (skb_has_frag_list(skb) &&
Johannes Berg1e1430d2016-10-04 09:22:19 +02003620 !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
3621 if (skb_linearize(skb)) {
3622 ieee80211_free_txskb(&local->hw, skb);
3623 goto begin;
3624 }
3625 }
3626
Johannes Berg53168212017-06-22 12:20:30 +02003627 switch (tx.sdata->vif.type) {
3628 case NL80211_IFTYPE_MONITOR:
3629 if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
3630 vif = &tx.sdata->vif;
3631 break;
3632 }
3633 tx.sdata = rcu_dereference(local->monitor_sdata);
3634 if (tx.sdata) {
3635 vif = &tx.sdata->vif;
3636 info->hw_queue =
3637 vif->hw_queue[skb_get_queue_mapping(skb)];
3638 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
3639 ieee80211_free_txskb(&local->hw, skb);
3640 goto begin;
3641 } else {
3642 vif = NULL;
3643 }
3644 break;
3645 case NL80211_IFTYPE_AP_VLAN:
3646 tx.sdata = container_of(tx.sdata->bss,
3647 struct ieee80211_sub_if_data, u.ap);
3648 /* fall through */
3649 default:
3650 vif = &tx.sdata->vif;
3651 break;
3652 }
3653
3654 IEEE80211_SKB_CB(skb)->control.vif = vif;
Felix Fietkauded46982019-03-16 18:06:33 +01003655 return skb;
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303656
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003657out:
3658 spin_unlock_bh(&fq->lock);
3659
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003660 return skb;
3661}
3662EXPORT_SYMBOL(ieee80211_tx_dequeue);
3663
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003664struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac)
3665{
3666 struct ieee80211_local *local = hw_to_local(hw);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003667 struct ieee80211_txq *ret = NULL;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003668 struct txq_info *txqi = NULL;
3669
Felix Fietkau5b989c12019-03-15 11:03:35 +01003670 spin_lock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003671
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003672 begin:
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003673 txqi = list_first_entry_or_null(&local->active_txqs[ac],
3674 struct txq_info,
3675 schedule_order);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003676 if (!txqi)
Felix Fietkau5b989c12019-03-15 11:03:35 +01003677 goto out;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003678
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003679 if (txqi->txq.sta) {
3680 struct sta_info *sta = container_of(txqi->txq.sta,
3681 struct sta_info, sta);
3682
3683 if (sta->airtime[txqi->txq.ac].deficit < 0) {
3684 sta->airtime[txqi->txq.ac].deficit +=
3685 sta->airtime_weight;
3686 list_move_tail(&txqi->schedule_order,
3687 &local->active_txqs[txqi->txq.ac]);
3688 goto begin;
3689 }
3690 }
3691
3692
3693 if (txqi->schedule_round == local->schedule_round[ac])
Felix Fietkau5b989c12019-03-15 11:03:35 +01003694 goto out;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003695
3696 list_del_init(&txqi->schedule_order);
3697 txqi->schedule_round = local->schedule_round[ac];
Felix Fietkau5b989c12019-03-15 11:03:35 +01003698 ret = &txqi->txq;
3699
3700out:
3701 spin_unlock_bh(&local->active_txq_lock[ac]);
3702 return ret;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003703}
3704EXPORT_SYMBOL(ieee80211_next_txq);
3705
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003706void __ieee80211_schedule_txq(struct ieee80211_hw *hw,
3707 struct ieee80211_txq *txq,
3708 bool force)
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003709{
3710 struct ieee80211_local *local = hw_to_local(hw);
3711 struct txq_info *txqi = to_txq_info(txq);
3712
Felix Fietkau5b989c12019-03-15 11:03:35 +01003713 spin_lock_bh(&local->active_txq_lock[txq->ac]);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003714
3715 if (list_empty(&txqi->schedule_order) &&
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003716 (force || !skb_queue_empty(&txqi->frags) ||
3717 txqi->tin.backlog_packets)) {
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003718 /* If airtime accounting is active, always enqueue STAs at the
3719 * head of the list to ensure that they only get moved to the
3720 * back by the airtime DRR scheduler once they have a negative
3721 * deficit. A station that already has a negative deficit will
3722 * get immediately moved to the back of the list on the next
3723 * call to ieee80211_next_txq().
3724 */
3725 if (txqi->txq.sta &&
3726 wiphy_ext_feature_isset(local->hw.wiphy,
3727 NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
3728 list_add(&txqi->schedule_order,
3729 &local->active_txqs[txq->ac]);
3730 else
3731 list_add_tail(&txqi->schedule_order,
3732 &local->active_txqs[txq->ac]);
3733 }
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003734
Toke Høiland-Jørgensen390298e2019-01-22 15:20:16 +01003735 spin_unlock_bh(&local->active_txq_lock[txq->ac]);
3736}
Felix Fietkau2b4a6692019-03-18 12:00:58 +01003737EXPORT_SYMBOL(__ieee80211_schedule_txq);
Toke Høiland-Jørgensen390298e2019-01-22 15:20:16 +01003738
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003739bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
3740 struct ieee80211_txq *txq)
3741{
3742 struct ieee80211_local *local = hw_to_local(hw);
3743 struct txq_info *iter, *tmp, *txqi = to_txq_info(txq);
3744 struct sta_info *sta;
3745 u8 ac = txq->ac;
3746
Felix Fietkau5b989c12019-03-15 11:03:35 +01003747 spin_lock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003748
3749 if (!txqi->txq.sta)
3750 goto out;
3751
3752 if (list_empty(&txqi->schedule_order))
3753 goto out;
3754
3755 list_for_each_entry_safe(iter, tmp, &local->active_txqs[ac],
3756 schedule_order) {
3757 if (iter == txqi)
3758 break;
3759
3760 if (!iter->txq.sta) {
3761 list_move_tail(&iter->schedule_order,
3762 &local->active_txqs[ac]);
3763 continue;
3764 }
3765 sta = container_of(iter->txq.sta, struct sta_info, sta);
3766 if (sta->airtime[ac].deficit < 0)
3767 sta->airtime[ac].deficit += sta->airtime_weight;
3768 list_move_tail(&iter->schedule_order, &local->active_txqs[ac]);
3769 }
3770
3771 sta = container_of(txqi->txq.sta, struct sta_info, sta);
3772 if (sta->airtime[ac].deficit >= 0)
3773 goto out;
3774
3775 sta->airtime[ac].deficit += sta->airtime_weight;
3776 list_move_tail(&txqi->schedule_order, &local->active_txqs[ac]);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003777 spin_unlock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003778
3779 return false;
3780out:
3781 if (!list_empty(&txqi->schedule_order))
3782 list_del_init(&txqi->schedule_order);
Felix Fietkau5b989c12019-03-15 11:03:35 +01003783 spin_unlock_bh(&local->active_txq_lock[ac]);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003784
3785 return true;
3786}
3787EXPORT_SYMBOL(ieee80211_txq_may_transmit);
3788
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003789void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003790{
3791 struct ieee80211_local *local = hw_to_local(hw);
3792
3793 spin_lock_bh(&local->active_txq_lock[ac]);
3794 local->schedule_round[ac]++;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003795 spin_unlock_bh(&local->active_txq_lock[ac]);
3796}
Felix Fietkau5b989c12019-03-15 11:03:35 +01003797EXPORT_SYMBOL(ieee80211_txq_schedule_start);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003798
Johannes Berg4c9451e2014-11-09 18:50:10 +02003799void __ieee80211_subif_start_xmit(struct sk_buff *skb,
3800 struct net_device *dev,
Rajkumar Manoharan06016772019-04-11 13:47:25 -07003801 u32 info_flags,
3802 u32 ctrl_flags)
Johannes Berg4c9451e2014-11-09 18:50:10 +02003803{
3804 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Felix Fietkau1974da82019-03-25 08:59:23 +01003805 struct ieee80211_local *local = sdata->local;
Johannes Berg97ffe752015-03-21 09:13:45 +01003806 struct sta_info *sta;
Johannes Berg80616c02015-04-14 14:50:41 +02003807 struct sk_buff *next;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003808
3809 if (unlikely(skb->len < ETH_HLEN)) {
3810 kfree_skb(skb);
3811 return;
3812 }
3813
3814 rcu_read_lock();
3815
Johannes Berg2d981fd2015-04-10 14:10:10 +02003816 if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
3817 goto out_free;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003818
Felix Fietkau1974da82019-03-25 08:59:23 +01003819 if (IS_ERR(sta))
3820 sta = NULL;
3821
3822 if (local->ops->wake_tx_queue) {
3823 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
3824 skb_set_queue_mapping(skb, queue);
3825 }
3826
3827 if (sta) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01003828 struct ieee80211_fast_tx *fast_tx;
3829
Wen Gong70e53662018-08-08 18:40:01 +08003830 sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
Toke Høiland-Jørgensen36148c22018-02-02 16:11:05 +01003831
Johannes Berg17c18bf2015-03-21 15:25:43 +01003832 fast_tx = rcu_dereference(sta->fast_tx);
3833
3834 if (fast_tx &&
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003835 ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
Johannes Berg17c18bf2015-03-21 15:25:43 +01003836 goto out;
3837 }
3838
Johannes Berg80616c02015-04-14 14:50:41 +02003839 if (skb_is_gso(skb)) {
3840 struct sk_buff *segs;
Johannes Berg680a0da2015-04-13 16:58:25 +02003841
Johannes Berg80616c02015-04-14 14:50:41 +02003842 segs = skb_gso_segment(skb, 0);
3843 if (IS_ERR(segs)) {
Johannes Berg2d981fd2015-04-10 14:10:10 +02003844 goto out_free;
Johannes Berg80616c02015-04-14 14:50:41 +02003845 } else if (segs) {
3846 consume_skb(skb);
3847 skb = segs;
3848 }
3849 } else {
3850 /* we cannot process non-linear frames on this path */
3851 if (skb_linearize(skb)) {
3852 kfree_skb(skb);
3853 goto out;
3854 }
3855
3856 /* the frame could be fragmented, software-encrypted, and other
3857 * things so we cannot really handle checksum offload with it -
3858 * fix it up in software before we handle anything else.
3859 */
3860 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Johannes Bergf603f1f2015-05-05 15:25:33 +02003861 skb_set_transport_header(skb,
3862 skb_checksum_start_offset(skb));
Johannes Berg80616c02015-04-14 14:50:41 +02003863 if (skb_checksum_help(skb))
3864 goto out_free;
3865 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02003866 }
3867
Johannes Berg80616c02015-04-14 14:50:41 +02003868 next = skb;
3869 while (next) {
3870 skb = next;
3871 next = skb->next;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003872
Johannes Berg80616c02015-04-14 14:50:41 +02003873 skb->prev = NULL;
3874 skb->next = NULL;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003875
Rajkumar Manoharan06016772019-04-11 13:47:25 -07003876 skb = ieee80211_build_hdr(sdata, skb, info_flags,
3877 sta, ctrl_flags);
Johannes Berg80616c02015-04-14 14:50:41 +02003878 if (IS_ERR(skb))
3879 goto out;
3880
Johannes Berg5a490512015-04-22 17:10:38 +02003881 ieee80211_tx_stats(dev, skb->len);
Johannes Berg80616c02015-04-14 14:50:41 +02003882
Johannes Bergb9771d42018-05-28 15:47:41 +02003883 ieee80211_xmit(sdata, sta, skb, 0);
Johannes Berg80616c02015-04-14 14:50:41 +02003884 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02003885 goto out;
3886 out_free:
3887 kfree_skb(skb);
Johannes Berg4c9451e2014-11-09 18:50:10 +02003888 out:
3889 rcu_read_unlock();
3890}
3891
Michael Braunebceec82016-11-22 11:52:18 +01003892static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
3893{
3894 struct ethhdr *eth;
3895 int err;
3896
3897 err = skb_ensure_writable(skb, ETH_HLEN);
3898 if (unlikely(err))
3899 return err;
3900
3901 eth = (void *)skb->data;
3902 ether_addr_copy(eth->h_dest, sta->sta.addr);
3903
3904 return 0;
3905}
3906
3907static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
3908 struct net_device *dev)
3909{
3910 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3911 const struct ethhdr *eth = (void *)skb->data;
3912 const struct vlan_ethhdr *ethvlan = (void *)skb->data;
3913 __be16 ethertype;
3914
3915 if (likely(!is_multicast_ether_addr(eth->h_dest)))
3916 return false;
3917
3918 switch (sdata->vif.type) {
3919 case NL80211_IFTYPE_AP_VLAN:
3920 if (sdata->u.vlan.sta)
3921 return false;
3922 if (sdata->wdev.use_4addr)
3923 return false;
3924 /* fall through */
3925 case NL80211_IFTYPE_AP:
3926 /* check runtime toggle for this bss */
3927 if (!sdata->bss->multicast_to_unicast)
3928 return false;
3929 break;
3930 default:
3931 return false;
3932 }
3933
3934 /* multicast to unicast conversion only for some payload */
3935 ethertype = eth->h_proto;
3936 if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
3937 ethertype = ethvlan->h_vlan_encapsulated_proto;
3938 switch (ethertype) {
3939 case htons(ETH_P_ARP):
3940 case htons(ETH_P_IP):
3941 case htons(ETH_P_IPV6):
3942 break;
3943 default:
3944 return false;
3945 }
3946
3947 return true;
3948}
3949
3950static void
3951ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
3952 struct sk_buff_head *queue)
3953{
3954 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3955 struct ieee80211_local *local = sdata->local;
3956 const struct ethhdr *eth = (struct ethhdr *)skb->data;
3957 struct sta_info *sta, *first = NULL;
3958 struct sk_buff *cloned_skb;
3959
3960 rcu_read_lock();
3961
3962 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3963 if (sdata != sta->sdata)
3964 /* AP-VLAN mismatch */
3965 continue;
3966 if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
3967 /* do not send back to source */
3968 continue;
3969 if (!first) {
3970 first = sta;
3971 continue;
3972 }
3973 cloned_skb = skb_clone(skb, GFP_ATOMIC);
3974 if (!cloned_skb)
3975 goto multicast;
3976 if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
3977 dev_kfree_skb(cloned_skb);
3978 goto multicast;
3979 }
3980 __skb_queue_tail(queue, cloned_skb);
3981 }
3982
3983 if (likely(first)) {
3984 if (unlikely(ieee80211_change_da(skb, first)))
3985 goto multicast;
3986 __skb_queue_tail(queue, skb);
3987 } else {
3988 /* no STA connected, drop */
3989 kfree_skb(skb);
3990 skb = NULL;
3991 }
3992
3993 goto out;
3994multicast:
3995 __skb_queue_purge(queue);
3996 __skb_queue_tail(queue, skb);
3997out:
3998 rcu_read_unlock();
3999}
4000
Johannes Berg4c9451e2014-11-09 18:50:10 +02004001/**
4002 * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
4003 * @skb: packet to be sent
4004 * @dev: incoming interface
4005 *
4006 * On failure skb will be freed.
4007 */
Liad Kaufman24d342c2014-11-09 18:50:07 +02004008netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
4009 struct net_device *dev)
4010{
Michael Braunebceec82016-11-22 11:52:18 +01004011 if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
4012 struct sk_buff_head queue;
4013
4014 __skb_queue_head_init(&queue);
4015 ieee80211_convert_to_unicast(skb, dev, &queue);
4016 while ((skb = __skb_dequeue(&queue)))
Rajkumar Manoharan06016772019-04-11 13:47:25 -07004017 __ieee80211_subif_start_xmit(skb, dev, 0, 0);
Michael Braunebceec82016-11-22 11:52:18 +01004018 } else {
Rajkumar Manoharan06016772019-04-11 13:47:25 -07004019 __ieee80211_subif_start_xmit(skb, dev, 0, 0);
Michael Braunebceec82016-11-22 11:52:18 +01004020 }
4021
Liad Kaufman24d342c2014-11-09 18:50:07 +02004022 return NETDEV_TX_OK;
4023}
Johannes Berge2ebc742007-07-27 15:43:22 +02004024
Johannes Berg7528ec52014-11-09 18:50:11 +02004025struct sk_buff *
4026ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
4027 struct sk_buff *skb, u32 info_flags)
4028{
4029 struct ieee80211_hdr *hdr;
4030 struct ieee80211_tx_data tx = {
4031 .local = sdata->local,
4032 .sdata = sdata,
4033 };
Johannes Berg97ffe752015-03-21 09:13:45 +01004034 struct sta_info *sta;
Johannes Berg7528ec52014-11-09 18:50:11 +02004035
4036 rcu_read_lock();
4037
Johannes Berg97ffe752015-03-21 09:13:45 +01004038 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4039 kfree_skb(skb);
4040 skb = ERR_PTR(-EINVAL);
4041 goto out;
4042 }
4043
Rajkumar Manoharan06016772019-04-11 13:47:25 -07004044 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta, 0);
Johannes Berg7528ec52014-11-09 18:50:11 +02004045 if (IS_ERR(skb))
4046 goto out;
4047
4048 hdr = (void *)skb->data;
4049 tx.sta = sta_info_get(sdata, hdr->addr1);
4050 tx.skb = skb;
4051
4052 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
4053 rcu_read_unlock();
4054 kfree_skb(skb);
4055 return ERR_PTR(-EINVAL);
4056 }
4057
4058out:
4059 rcu_read_unlock();
4060 return skb;
4061}
4062
Johannes Berge2530082008-05-17 00:57:14 +02004063/*
4064 * ieee80211_clear_tx_pending may not be called in a context where
4065 * it is possible that it packets could come in again.
4066 */
Johannes Berge2ebc742007-07-27 15:43:22 +02004067void ieee80211_clear_tx_pending(struct ieee80211_local *local)
4068{
Felix Fietkau1f98ab72012-11-10 03:44:14 +01004069 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01004070 int i;
Johannes Berge2ebc742007-07-27 15:43:22 +02004071
Felix Fietkau1f98ab72012-11-10 03:44:14 +01004072 for (i = 0; i < local->hw.queues; i++) {
4073 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
4074 ieee80211_free_txskb(&local->hw, skb);
4075 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004076}
4077
Johannes Berg7bb45682011-02-24 14:42:06 +01004078/*
4079 * Returns false if the frame couldn't be transmitted but was queued instead,
4080 * which in this case means re-queued -- take as an indication to stop sending
4081 * more pending frames.
4082 */
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004083static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
4084 struct sk_buff *skb)
4085{
4086 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4087 struct ieee80211_sub_if_data *sdata;
4088 struct sta_info *sta;
4089 struct ieee80211_hdr *hdr;
Johannes Berg7bb45682011-02-24 14:42:06 +01004090 bool result;
Johannes Berg55de9082012-07-26 17:24:39 +02004091 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004092
Johannes Berg5061b0c2009-07-14 00:33:34 +02004093 sdata = vif_to_sdata(info->control.vif);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004094
4095 if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
Johannes Berg55de9082012-07-26 17:24:39 +02004096 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4097 if (unlikely(!chanctx_conf)) {
4098 dev_kfree_skb(skb);
4099 return true;
4100 }
Johannes Berg73c4e192014-11-09 18:50:09 +02004101 info->band = chanctx_conf->def.chan->band;
Johannes Bergb9771d42018-05-28 15:47:41 +02004102 result = ieee80211_tx(sdata, NULL, skb, true, 0);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004103 } else {
Johannes Berg252b86c2011-11-16 15:28:55 +01004104 struct sk_buff_head skbs;
4105
4106 __skb_queue_head_init(&skbs);
4107 __skb_queue_tail(&skbs, skb);
4108
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004109 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Bergabe60632009-11-25 17:46:18 +01004110 sta = sta_info_get(sdata, hdr->addr1);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004111
Johannes Berg74e4dbf2011-11-16 15:28:57 +01004112 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004113 }
4114
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004115 return result;
4116}
4117
Johannes Berge2530082008-05-17 00:57:14 +02004118/*
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004119 * Transmit all pending packets. Called from tasklet.
Johannes Berge2530082008-05-17 00:57:14 +02004120 */
Johannes Berge2ebc742007-07-27 15:43:22 +02004121void ieee80211_tx_pending(unsigned long data)
4122{
4123 struct ieee80211_local *local = (struct ieee80211_local *)data;
Johannes Berg2a577d92009-03-23 17:28:37 +01004124 unsigned long flags;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004125 int i;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004126 bool txok;
Johannes Berge2ebc742007-07-27 15:43:22 +02004127
Johannes Bergf0e72852009-03-23 17:28:36 +01004128 rcu_read_lock();
Johannes Berg2a577d92009-03-23 17:28:37 +01004129
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004130 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg176be722009-03-12 23:49:28 +01004131 for (i = 0; i < local->hw.queues; i++) {
Johannes Berg2a577d92009-03-23 17:28:37 +01004132 /*
4133 * If queue is stopped by something other than due to pending
4134 * frames, or we have no pending frames, proceed to next queue.
4135 */
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004136 if (local->queue_stop_reasons[i] ||
Johannes Berg2a577d92009-03-23 17:28:37 +01004137 skb_queue_empty(&local->pending[i]))
Johannes Berge2ebc742007-07-27 15:43:22 +02004138 continue;
Johannes Berge2530082008-05-17 00:57:14 +02004139
Johannes Berg2a577d92009-03-23 17:28:37 +01004140 while (!skb_queue_empty(&local->pending[i])) {
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004141 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
Johannes Berg5061b0c2009-07-14 00:33:34 +02004142 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg5061b0c2009-07-14 00:33:34 +02004143
Johannes Berga7bc3762009-07-27 10:33:31 +02004144 if (WARN_ON(!info->control.vif)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02004145 ieee80211_free_txskb(&local->hw, skb);
Johannes Berga7bc3762009-07-27 10:33:31 +02004146 continue;
4147 }
4148
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004149 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
4150 flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01004151
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004152 txok = ieee80211_tx_pending_skb(local, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004153 spin_lock_irqsave(&local->queue_stop_reason_lock,
4154 flags);
4155 if (!txok)
Johannes Berg2a577d92009-03-23 17:28:37 +01004156 break;
Johannes Berge2ebc742007-07-27 15:43:22 +02004157 }
Johannes Berg7236fe22010-03-22 13:42:43 -07004158
4159 if (skb_queue_empty(&local->pending[i]))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02004160 ieee80211_propagate_queue_wake(local, i);
Johannes Berge2ebc742007-07-27 15:43:22 +02004161 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004162 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01004163
Johannes Bergf0e72852009-03-23 17:28:36 +01004164 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004165}
4166
4167/* functions for drivers to get certain frames */
4168
Marco Porscheac70c12013-01-07 16:04:50 +01004169static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004170 struct ps_data *ps, struct sk_buff *skb,
4171 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02004172{
4173 u8 *pos, *tim;
4174 int aid0 = 0;
4175 int i, have_bits = 0, n1, n2;
4176
4177 /* Generate bitmap for TIM only if there are any STAs in power save
4178 * mode. */
Marco Porschd012a602012-10-10 12:39:50 -07004179 if (atomic_read(&ps->num_sta_ps) > 0)
Johannes Berge2ebc742007-07-27 15:43:22 +02004180 /* in the hope that this is faster than
4181 * checking byte-for-byte */
Weilong Chenf359d3f2013-12-18 15:44:16 +08004182 have_bits = !bitmap_empty((unsigned long *)ps->tim,
Johannes Berge2ebc742007-07-27 15:43:22 +02004183 IEEE80211_MAX_AID+1);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004184 if (!is_template) {
4185 if (ps->dtim_count == 0)
4186 ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
4187 else
4188 ps->dtim_count--;
4189 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004190
Johannes Berg4df864c2017-06-16 14:29:21 +02004191 tim = pos = skb_put(skb, 6);
Johannes Berge2ebc742007-07-27 15:43:22 +02004192 *pos++ = WLAN_EID_TIM;
4193 *pos++ = 4;
Marco Porschd012a602012-10-10 12:39:50 -07004194 *pos++ = ps->dtim_count;
Johannes Berg88600202012-02-13 15:17:18 +01004195 *pos++ = sdata->vif.bss_conf.dtim_period;
Johannes Berge2ebc742007-07-27 15:43:22 +02004196
Marco Porschd012a602012-10-10 12:39:50 -07004197 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
Johannes Berge2ebc742007-07-27 15:43:22 +02004198 aid0 = 1;
4199
Marco Porschd012a602012-10-10 12:39:50 -07004200 ps->dtim_bc_mc = aid0 == 1;
Christian Lamparter512119b2011-01-31 20:48:44 +02004201
Johannes Berge2ebc742007-07-27 15:43:22 +02004202 if (have_bits) {
4203 /* Find largest even number N1 so that bits numbered 1 through
4204 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
4205 * (N2 + 1) x 8 through 2007 are 0. */
4206 n1 = 0;
4207 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
Marco Porschd012a602012-10-10 12:39:50 -07004208 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004209 n1 = i & 0xfe;
4210 break;
4211 }
4212 }
4213 n2 = n1;
4214 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
Marco Porschd012a602012-10-10 12:39:50 -07004215 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004216 n2 = i;
4217 break;
4218 }
4219 }
4220
4221 /* Bitmap control */
4222 *pos++ = n1 | aid0;
4223 /* Part Virt Bitmap */
Eliad Peller5220da32011-11-24 16:50:00 +02004224 skb_put(skb, n2 - n1);
Marco Porschd012a602012-10-10 12:39:50 -07004225 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
Johannes Berge2ebc742007-07-27 15:43:22 +02004226
4227 tim[1] = n2 - n1 + 4;
Johannes Berge2ebc742007-07-27 15:43:22 +02004228 } else {
4229 *pos++ = aid0; /* Bitmap control */
4230 *pos++ = 0; /* Part Virt Bitmap */
4231 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004232}
4233
Marco Porscheac70c12013-01-07 16:04:50 +01004234static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004235 struct ps_data *ps, struct sk_buff *skb,
4236 bool is_template)
Marco Porscheac70c12013-01-07 16:04:50 +01004237{
4238 struct ieee80211_local *local = sdata->local;
4239
4240 /*
4241 * Not very nice, but we want to allow the driver to call
4242 * ieee80211_beacon_get() as a response to the set_tim()
4243 * callback. That, however, is already invoked under the
4244 * sta_lock to guarantee consistent and race-free update
4245 * of the tim bitmap in mac80211 and the driver.
4246 */
4247 if (local->tim_in_locked_section) {
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004248 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Marco Porscheac70c12013-01-07 16:04:50 +01004249 } else {
Johannes Berg1b917312013-02-22 12:55:01 +01004250 spin_lock_bh(&local->tim_lock);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004251 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Johannes Berg1b917312013-02-22 12:55:01 +01004252 spin_unlock_bh(&local->tim_lock);
Marco Porscheac70c12013-01-07 16:04:50 +01004253 }
4254
4255 return 0;
4256}
4257
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004258static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
4259 struct beacon_data *beacon)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004260{
4261 struct probe_resp *resp;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004262 u8 *beacon_data;
4263 size_t beacon_data_len;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004264 int i;
Michal Kazioraf296bd2014-06-05 14:21:36 +02004265 u8 count = beacon->csa_current_counter;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004266
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004267 switch (sdata->vif.type) {
4268 case NL80211_IFTYPE_AP:
4269 beacon_data = beacon->tail;
4270 beacon_data_len = beacon->tail_len;
4271 break;
4272 case NL80211_IFTYPE_ADHOC:
4273 beacon_data = beacon->head;
4274 beacon_data_len = beacon->head_len;
4275 break;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004276 case NL80211_IFTYPE_MESH_POINT:
4277 beacon_data = beacon->head;
4278 beacon_data_len = beacon->head_len;
4279 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004280 default:
4281 return;
4282 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004283
Michal Kazioraf296bd2014-06-05 14:21:36 +02004284 rcu_read_lock();
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004285 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004286 resp = rcu_dereference(sdata->u.ap.probe_resp);
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004287
Michal Kazioraf296bd2014-06-05 14:21:36 +02004288 if (beacon->csa_counter_offsets[i]) {
4289 if (WARN_ON_ONCE(beacon->csa_counter_offsets[i] >=
4290 beacon_data_len)) {
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004291 rcu_read_unlock();
4292 return;
4293 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004294
4295 beacon_data[beacon->csa_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004296 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004297
4298 if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
4299 resp->data[resp->csa_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004300 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004301 rcu_read_unlock();
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004302}
4303
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004304static u8 __ieee80211_csa_update_counter(struct beacon_data *beacon)
4305{
4306 beacon->csa_current_counter--;
4307
4308 /* the counter should never reach 0 */
4309 WARN_ON_ONCE(!beacon->csa_current_counter);
4310
4311 return beacon->csa_current_counter;
4312}
4313
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004314u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
4315{
4316 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004317 struct beacon_data *beacon = NULL;
4318 u8 count = 0;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004319
Michal Kazioraf296bd2014-06-05 14:21:36 +02004320 rcu_read_lock();
4321
4322 if (sdata->vif.type == NL80211_IFTYPE_AP)
4323 beacon = rcu_dereference(sdata->u.ap.beacon);
4324 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4325 beacon = rcu_dereference(sdata->u.ibss.presp);
4326 else if (ieee80211_vif_is_mesh(&sdata->vif))
4327 beacon = rcu_dereference(sdata->u.mesh.beacon);
4328
4329 if (!beacon)
4330 goto unlock;
4331
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004332 count = __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004333
Michal Kazioraf296bd2014-06-05 14:21:36 +02004334unlock:
4335 rcu_read_unlock();
4336 return count;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004337}
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004338EXPORT_SYMBOL(ieee80211_csa_update_counter);
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004339
Gregory Greenman03737002018-04-20 13:49:24 +03004340void ieee80211_csa_set_counter(struct ieee80211_vif *vif, u8 counter)
4341{
4342 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4343 struct beacon_data *beacon = NULL;
4344
4345 rcu_read_lock();
4346
4347 if (sdata->vif.type == NL80211_IFTYPE_AP)
4348 beacon = rcu_dereference(sdata->u.ap.beacon);
4349 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4350 beacon = rcu_dereference(sdata->u.ibss.presp);
4351 else if (ieee80211_vif_is_mesh(&sdata->vif))
4352 beacon = rcu_dereference(sdata->u.mesh.beacon);
4353
4354 if (!beacon)
4355 goto unlock;
4356
4357 if (counter < beacon->csa_current_counter)
4358 beacon->csa_current_counter = counter;
4359
4360unlock:
4361 rcu_read_unlock();
4362}
4363EXPORT_SYMBOL(ieee80211_csa_set_counter);
4364
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004365bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
4366{
4367 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4368 struct beacon_data *beacon = NULL;
4369 u8 *beacon_data;
4370 size_t beacon_data_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004371 int ret = false;
4372
4373 if (!ieee80211_sdata_running(sdata))
4374 return false;
4375
4376 rcu_read_lock();
4377 if (vif->type == NL80211_IFTYPE_AP) {
4378 struct ieee80211_if_ap *ap = &sdata->u.ap;
4379
4380 beacon = rcu_dereference(ap->beacon);
4381 if (WARN_ON(!beacon || !beacon->tail))
4382 goto out;
4383 beacon_data = beacon->tail;
4384 beacon_data_len = beacon->tail_len;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004385 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
4386 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4387
4388 beacon = rcu_dereference(ifibss->presp);
4389 if (!beacon)
4390 goto out;
4391
4392 beacon_data = beacon->head;
4393 beacon_data_len = beacon->head_len;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004394 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
4395 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4396
4397 beacon = rcu_dereference(ifmsh->beacon);
4398 if (!beacon)
4399 goto out;
4400
4401 beacon_data = beacon->head;
4402 beacon_data_len = beacon->head_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004403 } else {
4404 WARN_ON(1);
4405 goto out;
4406 }
4407
Michal Kazior10d78f22014-06-05 14:21:37 +02004408 if (!beacon->csa_counter_offsets[0])
4409 goto out;
4410
Michal Kazioraf296bd2014-06-05 14:21:36 +02004411 if (WARN_ON_ONCE(beacon->csa_counter_offsets[0] > beacon_data_len))
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004412 goto out;
4413
Michal Kazioraf296bd2014-06-05 14:21:36 +02004414 if (beacon_data[beacon->csa_counter_offsets[0]] == 1)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004415 ret = true;
4416 out:
4417 rcu_read_unlock();
4418
4419 return ret;
4420}
4421EXPORT_SYMBOL(ieee80211_csa_is_complete);
4422
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004423static struct sk_buff *
4424__ieee80211_beacon_get(struct ieee80211_hw *hw,
4425 struct ieee80211_vif *vif,
4426 struct ieee80211_mutable_offsets *offs,
4427 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02004428{
4429 struct ieee80211_local *local = hw_to_local(hw);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004430 struct beacon_data *beacon = NULL;
Johannes Berg9d139c82008-07-09 14:40:37 +02004431 struct sk_buff *skb = NULL;
Johannes Berge039fa42008-05-15 12:55:29 +02004432 struct ieee80211_tx_info *info;
Johannes Berge2ebc742007-07-27 15:43:22 +02004433 struct ieee80211_sub_if_data *sdata = NULL;
Johannes Berg57fbcce2016-04-12 15:56:15 +02004434 enum nl80211_band band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004435 struct ieee80211_tx_rate_control txrc;
Johannes Berg55de9082012-07-26 17:24:39 +02004436 struct ieee80211_chanctx_conf *chanctx_conf;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004437 int csa_off_base = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004438
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004439 rcu_read_lock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004440
Johannes Berg32bfd352007-12-19 01:31:26 +01004441 sdata = vif_to_sdata(vif);
Johannes Berg55de9082012-07-26 17:24:39 +02004442 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berge2ebc742007-07-27 15:43:22 +02004443
Johannes Berg55de9082012-07-26 17:24:39 +02004444 if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
Felix Fietkaueb3e5542011-01-24 19:28:49 +01004445 goto out;
4446
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004447 if (offs)
4448 memset(offs, 0, sizeof(*offs));
Johannes Bergeddcbb942009-10-29 08:30:35 +01004449
Johannes Berg05c914f2008-09-11 00:01:58 +02004450 if (sdata->vif.type == NL80211_IFTYPE_AP) {
Marco Porschd012a602012-10-10 12:39:50 -07004451 struct ieee80211_if_ap *ap = &sdata->u.ap;
Marco Porschd012a602012-10-10 12:39:50 -07004452
Michal Kazioraf296bd2014-06-05 14:21:36 +02004453 beacon = rcu_dereference(ap->beacon);
Dan Carpenter3ad97fb2011-02-07 22:03:35 +03004454 if (beacon) {
Michal Kazior10d78f22014-06-05 14:21:37 +02004455 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004456 if (!is_template)
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004457 __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004458
4459 ieee80211_set_csa(sdata, beacon);
4460 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004461
Johannes Berg902acc72008-02-23 15:17:19 +01004462 /*
4463 * headroom, head length,
4464 * tail length and maximum TIM length
4465 */
4466 skb = dev_alloc_skb(local->tx_headroom +
4467 beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004468 beacon->tail_len + 256 +
4469 local->hw.extra_beacon_tailroom);
Johannes Berg902acc72008-02-23 15:17:19 +01004470 if (!skb)
4471 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004472
Johannes Berg902acc72008-02-23 15:17:19 +01004473 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004474 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg902acc72008-02-23 15:17:19 +01004475
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004476 ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
4477 is_template);
Johannes Berg902acc72008-02-23 15:17:19 +01004478
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004479 if (offs) {
4480 offs->tim_offset = beacon->head_len;
4481 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004482
4483 /* for AP the csa offsets are from tail */
4484 csa_off_base = skb->len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004485 }
Johannes Bergeddcbb942009-10-29 08:30:35 +01004486
Johannes Berg902acc72008-02-23 15:17:19 +01004487 if (beacon->tail)
Johannes Berg59ae1d12017-06-16 14:29:20 +02004488 skb_put_data(skb, beacon->tail,
4489 beacon->tail_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004490 } else
4491 goto out;
Johannes Berg05c914f2008-09-11 00:01:58 +02004492 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Berg46900292009-02-15 12:44:28 +01004493 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg9d139c82008-07-09 14:40:37 +02004494 struct ieee80211_hdr *hdr;
Johannes Berg902acc72008-02-23 15:17:19 +01004495
Michal Kazioraf296bd2014-06-05 14:21:36 +02004496 beacon = rcu_dereference(ifibss->presp);
4497 if (!beacon)
Johannes Berg9d139c82008-07-09 14:40:37 +02004498 goto out;
4499
Michal Kazior10d78f22014-06-05 14:21:37 +02004500 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004501 if (!is_template)
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004502 __ieee80211_csa_update_counter(beacon);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004503
Michal Kazioraf296bd2014-06-05 14:21:36 +02004504 ieee80211_set_csa(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004505 }
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004506
Michal Kazioraf296bd2014-06-05 14:21:36 +02004507 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004508 local->hw.extra_beacon_tailroom);
Johannes Berg9d139c82008-07-09 14:40:37 +02004509 if (!skb)
4510 goto out;
Johannes Bergc3ffeab2013-03-07 20:54:29 +01004511 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004512 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004513
4514 hdr = (struct ieee80211_hdr *) skb->data;
Harvey Harrisone7827a72008-07-15 18:44:13 -07004515 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4516 IEEE80211_STYPE_BEACON);
Johannes Berg902acc72008-02-23 15:17:19 +01004517 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
Javier Cardonadbf498f2012-03-31 11:31:32 -07004518 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg472dbc42008-09-11 00:01:49 +02004519
Michal Kazioraf296bd2014-06-05 14:21:36 +02004520 beacon = rcu_dereference(ifmsh->beacon);
4521 if (!beacon)
Johannes Bergac1bd842011-01-18 13:45:32 +01004522 goto out;
Johannes Bergac1bd842011-01-18 13:45:32 +01004523
Michal Kazior10d78f22014-06-05 14:21:37 +02004524 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004525 if (!is_template)
4526 /* TODO: For mesh csa_counter is in TU, so
4527 * decrementing it by one isn't correct, but
4528 * for now we leave it consistent with overall
4529 * mac80211's behavior.
4530 */
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004531 __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004532
Michal Kazioraf296bd2014-06-05 14:21:36 +02004533 ieee80211_set_csa(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004534 }
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004535
Javier Cardonadbf498f2012-03-31 11:31:32 -07004536 if (ifmsh->sync_ops)
Masashi Honma445cd452016-12-08 10:15:51 +09004537 ifmsh->sync_ops->adjust_tsf(sdata, beacon);
Javier Cardonadbf498f2012-03-31 11:31:32 -07004538
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -07004539 skb = dev_alloc_skb(local->tx_headroom +
Michal Kazioraf296bd2014-06-05 14:21:36 +02004540 beacon->head_len +
Marco Porsch3f52b7e2013-01-30 18:14:08 +01004541 256 + /* TIM IE */
Michal Kazioraf296bd2014-06-05 14:21:36 +02004542 beacon->tail_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004543 local->hw.extra_beacon_tailroom);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01004544 if (!skb)
4545 goto out;
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08004546 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004547 skb_put_data(skb, beacon->head, beacon->head_len);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004548 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
4549
4550 if (offs) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004551 offs->tim_offset = beacon->head_len;
4552 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004553 }
4554
Johannes Berg59ae1d12017-06-16 14:29:20 +02004555 skb_put_data(skb, beacon->tail, beacon->tail_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004556 } else {
4557 WARN_ON(1);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004558 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02004559 }
4560
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004561 /* CSA offsets */
Michal Kazioraf296bd2014-06-05 14:21:36 +02004562 if (offs && beacon) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004563 int i;
4564
4565 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004566 u16 csa_off = beacon->csa_counter_offsets[i];
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004567
4568 if (!csa_off)
4569 continue;
4570
4571 offs->csa_counter_offs[i] = csa_off_base + csa_off;
4572 }
4573 }
4574
Johannes Berg4bf88532012-11-09 11:39:59 +01004575 band = chanctx_conf->def.chan->band;
Johannes Berg55de9082012-07-26 17:24:39 +02004576
Johannes Berge039fa42008-05-15 12:55:29 +02004577 info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02004578
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004579 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004580 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Johannes Berge039fa42008-05-15 12:55:29 +02004581 info->band = band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004582
4583 memset(&txrc, 0, sizeof(txrc));
4584 txrc.hw = hw;
Johannes Berge31583c2012-07-26 14:07:46 +02004585 txrc.sband = local->hw.wiphy->bands[band];
Jouni Malinene00cfce2009-12-29 12:59:19 +02004586 txrc.bss_conf = &sdata->vif.bss_conf;
4587 txrc.skb = skb;
4588 txrc.reported_rate.idx = -1;
Jouni Malinen37eb0b12010-01-06 13:09:08 +02004589 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
Felix Fietkau8f0729b2010-11-11 15:07:23 +01004590 txrc.bss = true;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004591 rate_control_get_rate(sdata, NULL, &txrc);
Johannes Berge039fa42008-05-15 12:55:29 +02004592
4593 info->control.vif = vif;
Johannes Bergf591fa52008-07-10 11:21:26 +02004594
John W. Linvillea472e712010-05-06 14:45:17 -04004595 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
4596 IEEE80211_TX_CTL_ASSIGN_SEQ |
4597 IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge6a98542008-10-21 12:40:02 +02004598 out:
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004599 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004600 return skb;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004601
4602}
4603
4604struct sk_buff *
4605ieee80211_beacon_get_template(struct ieee80211_hw *hw,
4606 struct ieee80211_vif *vif,
4607 struct ieee80211_mutable_offsets *offs)
4608{
4609 return __ieee80211_beacon_get(hw, vif, offs, true);
4610}
4611EXPORT_SYMBOL(ieee80211_beacon_get_template);
4612
4613struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
4614 struct ieee80211_vif *vif,
4615 u16 *tim_offset, u16 *tim_length)
4616{
4617 struct ieee80211_mutable_offsets offs = {};
4618 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
Helmut Schaa35afa582015-09-09 09:46:32 +02004619 struct sk_buff *copy;
4620 struct ieee80211_supported_band *sband;
4621 int shift;
4622
4623 if (!bcn)
4624 return bcn;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004625
4626 if (tim_offset)
4627 *tim_offset = offs.tim_offset;
4628
4629 if (tim_length)
4630 *tim_length = offs.tim_length;
4631
Helmut Schaa35afa582015-09-09 09:46:32 +02004632 if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
4633 !hw_to_local(hw)->monitors)
4634 return bcn;
4635
4636 /* send a copy to monitor interfaces */
4637 copy = skb_copy(bcn, GFP_ATOMIC);
4638 if (!copy)
4639 return bcn;
4640
4641 shift = ieee80211_vif_get_shift(vif);
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05304642 sband = ieee80211_get_sband(vif_to_sdata(vif));
4643 if (!sband)
4644 return bcn;
4645
John Crispinb7b2e8c2019-07-14 17:44:15 +02004646 ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false,
4647 NULL);
Helmut Schaa35afa582015-09-09 09:46:32 +02004648
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004649 return bcn;
Johannes Berge2ebc742007-07-27 15:43:22 +02004650}
Johannes Bergeddcbb942009-10-29 08:30:35 +01004651EXPORT_SYMBOL(ieee80211_beacon_get_tim);
Johannes Berge2ebc742007-07-27 15:43:22 +02004652
Arik Nemtsov02945822011-11-10 11:28:57 +02004653struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
4654 struct ieee80211_vif *vif)
4655{
4656 struct ieee80211_if_ap *ap = NULL;
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004657 struct sk_buff *skb = NULL;
4658 struct probe_resp *presp = NULL;
Arik Nemtsov02945822011-11-10 11:28:57 +02004659 struct ieee80211_hdr *hdr;
4660 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4661
4662 if (sdata->vif.type != NL80211_IFTYPE_AP)
4663 return NULL;
4664
4665 rcu_read_lock();
4666
4667 ap = &sdata->u.ap;
4668 presp = rcu_dereference(ap->probe_resp);
4669 if (!presp)
4670 goto out;
4671
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004672 skb = dev_alloc_skb(presp->len);
Arik Nemtsov02945822011-11-10 11:28:57 +02004673 if (!skb)
4674 goto out;
4675
Johannes Berg59ae1d12017-06-16 14:29:20 +02004676 skb_put_data(skb, presp->data, presp->len);
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004677
Arik Nemtsov02945822011-11-10 11:28:57 +02004678 hdr = (struct ieee80211_hdr *) skb->data;
4679 memset(hdr->addr1, 0, sizeof(hdr->addr1));
4680
4681out:
4682 rcu_read_unlock();
4683 return skb;
4684}
4685EXPORT_SYMBOL(ieee80211_proberesp_get);
4686
Kalle Valo7044cc52010-01-05 20:16:19 +02004687struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
4688 struct ieee80211_vif *vif)
4689{
4690 struct ieee80211_sub_if_data *sdata;
4691 struct ieee80211_if_managed *ifmgd;
4692 struct ieee80211_pspoll *pspoll;
4693 struct ieee80211_local *local;
4694 struct sk_buff *skb;
4695
4696 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4697 return NULL;
4698
4699 sdata = vif_to_sdata(vif);
4700 ifmgd = &sdata->u.mgd;
4701 local = sdata->local;
4702
4703 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
Joe Perchesd15b8452011-08-29 14:17:31 -07004704 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02004705 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07004706
Kalle Valo7044cc52010-01-05 20:16:19 +02004707 skb_reserve(skb, local->hw.extra_tx_headroom);
4708
Johannes Bergb080db52017-06-16 14:29:19 +02004709 pspoll = skb_put_zero(skb, sizeof(*pspoll));
Kalle Valo7044cc52010-01-05 20:16:19 +02004710 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
4711 IEEE80211_STYPE_PSPOLL);
4712 pspoll->aid = cpu_to_le16(ifmgd->aid);
4713
4714 /* aid in PS-Poll has its two MSBs each set to 1 */
4715 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
4716
4717 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
4718 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
4719
4720 return skb;
4721}
4722EXPORT_SYMBOL(ieee80211_pspoll_get);
4723
4724struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004725 struct ieee80211_vif *vif,
4726 bool qos_ok)
Kalle Valo7044cc52010-01-05 20:16:19 +02004727{
4728 struct ieee80211_hdr_3addr *nullfunc;
4729 struct ieee80211_sub_if_data *sdata;
4730 struct ieee80211_if_managed *ifmgd;
4731 struct ieee80211_local *local;
4732 struct sk_buff *skb;
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004733 bool qos = false;
Kalle Valo7044cc52010-01-05 20:16:19 +02004734
4735 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4736 return NULL;
4737
4738 sdata = vif_to_sdata(vif);
4739 ifmgd = &sdata->u.mgd;
4740 local = sdata->local;
4741
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004742 if (qos_ok) {
4743 struct sta_info *sta;
4744
4745 rcu_read_lock();
4746 sta = sta_info_get(sdata, ifmgd->bssid);
4747 qos = sta && sta->sta.wme;
4748 rcu_read_unlock();
4749 }
4750
4751 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
4752 sizeof(*nullfunc) + 2);
Joe Perchesd15b8452011-08-29 14:17:31 -07004753 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02004754 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07004755
Kalle Valo7044cc52010-01-05 20:16:19 +02004756 skb_reserve(skb, local->hw.extra_tx_headroom);
4757
Johannes Bergb080db52017-06-16 14:29:19 +02004758 nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
Kalle Valo7044cc52010-01-05 20:16:19 +02004759 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
4760 IEEE80211_STYPE_NULLFUNC |
4761 IEEE80211_FCTL_TODS);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004762 if (qos) {
Johannes Berge0ba7092018-11-09 11:16:46 +01004763 __le16 qoshdr = cpu_to_le16(7);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004764
4765 BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
4766 IEEE80211_STYPE_NULLFUNC) !=
4767 IEEE80211_STYPE_QOS_NULLFUNC);
4768 nullfunc->frame_control |=
4769 cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
4770 skb->priority = 7;
4771 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
Johannes Berge0ba7092018-11-09 11:16:46 +01004772 skb_put_data(skb, &qoshdr, sizeof(qoshdr));
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004773 }
4774
Kalle Valo7044cc52010-01-05 20:16:19 +02004775 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
4776 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
4777 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
4778
4779 return skb;
4780}
4781EXPORT_SYMBOL(ieee80211_nullfunc_get);
4782
Kalle Valo05e54ea2010-01-05 20:16:38 +02004783struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
Johannes Berga344d672014-06-12 22:24:31 +02004784 const u8 *src_addr,
Kalle Valo05e54ea2010-01-05 20:16:38 +02004785 const u8 *ssid, size_t ssid_len,
Johannes Bergb9a9ada2012-11-29 13:00:10 +01004786 size_t tailroom)
Kalle Valo05e54ea2010-01-05 20:16:38 +02004787{
Johannes Berga344d672014-06-12 22:24:31 +02004788 struct ieee80211_local *local = hw_to_local(hw);
Kalle Valo05e54ea2010-01-05 20:16:38 +02004789 struct ieee80211_hdr_3addr *hdr;
4790 struct sk_buff *skb;
4791 size_t ie_ssid_len;
4792 u8 *pos;
4793
Kalle Valo05e54ea2010-01-05 20:16:38 +02004794 ie_ssid_len = 2 + ssid_len;
4795
4796 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
Johannes Bergb9a9ada2012-11-29 13:00:10 +01004797 ie_ssid_len + tailroom);
Joe Perchesd15b8452011-08-29 14:17:31 -07004798 if (!skb)
Kalle Valo05e54ea2010-01-05 20:16:38 +02004799 return NULL;
Kalle Valo05e54ea2010-01-05 20:16:38 +02004800
4801 skb_reserve(skb, local->hw.extra_tx_headroom);
4802
Johannes Bergb080db52017-06-16 14:29:19 +02004803 hdr = skb_put_zero(skb, sizeof(*hdr));
Kalle Valo05e54ea2010-01-05 20:16:38 +02004804 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4805 IEEE80211_STYPE_PROBE_REQ);
Johannes Berge83e6542012-07-13 16:23:07 +02004806 eth_broadcast_addr(hdr->addr1);
Johannes Berga344d672014-06-12 22:24:31 +02004807 memcpy(hdr->addr2, src_addr, ETH_ALEN);
Johannes Berge83e6542012-07-13 16:23:07 +02004808 eth_broadcast_addr(hdr->addr3);
Kalle Valo05e54ea2010-01-05 20:16:38 +02004809
4810 pos = skb_put(skb, ie_ssid_len);
4811 *pos++ = WLAN_EID_SSID;
4812 *pos++ = ssid_len;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02004813 if (ssid_len)
Kalle Valo05e54ea2010-01-05 20:16:38 +02004814 memcpy(pos, ssid, ssid_len);
4815 pos += ssid_len;
4816
Kalle Valo05e54ea2010-01-05 20:16:38 +02004817 return skb;
4818}
4819EXPORT_SYMBOL(ieee80211_probereq_get);
4820
Johannes Berg32bfd352007-12-19 01:31:26 +01004821void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02004822 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02004823 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02004824 struct ieee80211_rts *rts)
4825{
4826 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02004827
Harvey Harrison065e96052008-06-22 16:45:27 -07004828 rts->frame_control =
4829 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01004830 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
4831 frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02004832 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
4833 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
4834}
4835EXPORT_SYMBOL(ieee80211_rts_get);
4836
Johannes Berg32bfd352007-12-19 01:31:26 +01004837void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02004838 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02004839 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02004840 struct ieee80211_cts *cts)
4841{
4842 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02004843
Harvey Harrison065e96052008-06-22 16:45:27 -07004844 cts->frame_control =
4845 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01004846 cts->duration = ieee80211_ctstoself_duration(hw, vif,
4847 frame_len, frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02004848 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
4849}
4850EXPORT_SYMBOL(ieee80211_ctstoself_get);
4851
4852struct sk_buff *
Johannes Berg32bfd352007-12-19 01:31:26 +01004853ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
Johannes Berge039fa42008-05-15 12:55:29 +02004854 struct ieee80211_vif *vif)
Johannes Berge2ebc742007-07-27 15:43:22 +02004855{
4856 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004857 struct sk_buff *skb = NULL;
Johannes Berg5cf121c2008-02-25 16:27:43 +01004858 struct ieee80211_tx_data tx;
Johannes Berge2ebc742007-07-27 15:43:22 +02004859 struct ieee80211_sub_if_data *sdata;
Marco Porschd012a602012-10-10 12:39:50 -07004860 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +02004861 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02004862 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berge2ebc742007-07-27 15:43:22 +02004863
Johannes Berg32bfd352007-12-19 01:31:26 +01004864 sdata = vif_to_sdata(vif);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004865
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004866 rcu_read_lock();
Johannes Berg55de9082012-07-26 17:24:39 +02004867 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004868
Marco Porschd012a602012-10-10 12:39:50 -07004869 if (!chanctx_conf)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004870 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004871
Marco Porschd012a602012-10-10 12:39:50 -07004872 if (sdata->vif.type == NL80211_IFTYPE_AP) {
4873 struct beacon_data *beacon =
4874 rcu_dereference(sdata->u.ap.beacon);
4875
4876 if (!beacon || !beacon->head)
4877 goto out;
4878
4879 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01004880 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4881 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07004882 } else {
4883 goto out;
4884 }
4885
4886 if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004887 goto out; /* send buffered bc/mc only after DTIM beacon */
Johannes Berge039fa42008-05-15 12:55:29 +02004888
Johannes Berge2ebc742007-07-27 15:43:22 +02004889 while (1) {
Marco Porschd012a602012-10-10 12:39:50 -07004890 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +02004891 if (!skb)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004892 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02004893 local->total_ps_buffered--;
4894
Marco Porschd012a602012-10-10 12:39:50 -07004895 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004896 struct ieee80211_hdr *hdr =
4897 (struct ieee80211_hdr *) skb->data;
4898 /* more buffered multicast/broadcast frames ==> set
4899 * MoreData flag in IEEE 802.11 header to inform PS
4900 * STAs */
4901 hdr->frame_control |=
4902 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
4903 }
4904
Michael Braun112c44b2014-03-06 15:08:43 +01004905 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porsch7cbf9d02013-03-01 16:01:18 +01004906 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
Johannes Berg7c107702015-03-20 14:18:27 +01004907 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
Johannes Berge2ebc742007-07-27 15:43:22 +02004908 break;
Felix Fietkau6b07d9c2016-08-02 11:13:41 +02004909 ieee80211_free_txskb(hw, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02004910 }
Johannes Berge039fa42008-05-15 12:55:29 +02004911
4912 info = IEEE80211_SKB_CB(skb);
4913
Johannes Berg5cf121c2008-02-25 16:27:43 +01004914 tx.flags |= IEEE80211_TX_PS_BUFFERED;
Johannes Berg4bf88532012-11-09 11:39:59 +01004915 info->band = chanctx_conf->def.chan->band;
Johannes Berge2ebc742007-07-27 15:43:22 +02004916
Johannes Berg97b045d2008-06-20 01:22:30 +02004917 if (invoke_tx_handlers(&tx))
Johannes Berge2ebc742007-07-27 15:43:22 +02004918 skb = NULL;
Johannes Berg97b045d2008-06-20 01:22:30 +02004919 out:
Johannes Bergd0709a62008-02-25 16:27:46 +01004920 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004921
4922 return skb;
4923}
4924EXPORT_SYMBOL(ieee80211_get_buffered_bc);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004925
Liad Kaufmanb6da9112014-11-19 13:47:38 +02004926int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
4927{
4928 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
4929 struct ieee80211_sub_if_data *sdata = sta->sdata;
4930 struct ieee80211_local *local = sdata->local;
4931 int ret;
4932 u32 queues;
4933
4934 lockdep_assert_held(&local->sta_mtx);
4935
4936 /* only some cases are supported right now */
4937 switch (sdata->vif.type) {
4938 case NL80211_IFTYPE_STATION:
4939 case NL80211_IFTYPE_AP:
4940 case NL80211_IFTYPE_AP_VLAN:
4941 break;
4942 default:
4943 WARN_ON(1);
4944 return -EINVAL;
4945 }
4946
4947 if (WARN_ON(tid >= IEEE80211_NUM_UPS))
4948 return -EINVAL;
4949
4950 if (sta->reserved_tid == tid) {
4951 ret = 0;
4952 goto out;
4953 }
4954
4955 if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
4956 sdata_err(sdata, "TID reservation already active\n");
4957 ret = -EALREADY;
4958 goto out;
4959 }
4960
4961 ieee80211_stop_vif_queues(sdata->local, sdata,
4962 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
4963
4964 synchronize_net();
4965
4966 /* Tear down BA sessions so we stop aggregating on this TID */
Johannes Berg30686bf2015-06-02 21:39:54 +02004967 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
Liad Kaufmanb6da9112014-11-19 13:47:38 +02004968 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
4969 __ieee80211_stop_tx_ba_session(sta, tid,
4970 AGG_STOP_LOCAL_REQUEST);
4971 }
4972
4973 queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02004974 __ieee80211_flush_queues(local, sdata, queues, false);
Liad Kaufmanb6da9112014-11-19 13:47:38 +02004975
4976 sta->reserved_tid = tid;
4977
4978 ieee80211_wake_vif_queues(local, sdata,
4979 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
4980
Johannes Berg30686bf2015-06-02 21:39:54 +02004981 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
Liad Kaufmanb6da9112014-11-19 13:47:38 +02004982 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
4983
4984 ret = 0;
4985 out:
4986 return ret;
4987}
4988EXPORT_SYMBOL(ieee80211_reserve_tid);
4989
4990void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
4991{
4992 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
4993 struct ieee80211_sub_if_data *sdata = sta->sdata;
4994
4995 lockdep_assert_held(&sdata->local->sta_mtx);
4996
4997 /* only some cases are supported right now */
4998 switch (sdata->vif.type) {
4999 case NL80211_IFTYPE_STATION:
5000 case NL80211_IFTYPE_AP:
5001 case NL80211_IFTYPE_AP_VLAN:
5002 break;
5003 default:
5004 WARN_ON(1);
5005 return;
5006 }
5007
5008 if (tid != sta->reserved_tid) {
5009 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
5010 return;
5011 }
5012
5013 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
5014}
5015EXPORT_SYMBOL(ieee80211_unreserve_tid);
5016
Johannes Berg55de9082012-07-26 17:24:39 +02005017void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
5018 struct sk_buff *skb, int tid,
Johannes Bergb9771d42018-05-28 15:47:41 +02005019 enum nl80211_band band, u32 txdata_flags)
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005020{
Amadeusz Sławiński731977e2017-01-24 16:42:10 +01005021 int ac = ieee80211_ac_from_tid(tid);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02005022
Zhang Shengjud57a5442016-03-03 01:16:56 +00005023 skb_reset_mac_header(skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02005024 skb_set_queue_mapping(skb, ac);
Helmut Schaacf6bb792011-12-15 10:18:34 +01005025 skb->priority = tid;
Johannes Bergcf0277e2010-01-05 18:00:58 +01005026
Johannes Berg89afe612013-02-13 15:39:57 +01005027 skb->dev = sdata->dev;
5028
Johannes Berg3d34deb2009-06-18 17:25:11 +02005029 /*
5030 * The other path calling ieee80211_xmit is from the tasklet,
5031 * and while we can handle concurrent transmissions locking
5032 * requirements are that we do not come into tx with bhs on.
5033 */
5034 local_bh_disable();
Johannes Berg73c4e192014-11-09 18:50:09 +02005035 IEEE80211_SKB_CB(skb)->band = band;
Johannes Bergb9771d42018-05-28 15:47:41 +02005036 ieee80211_xmit(sdata, NULL, skb, txdata_flags);
Johannes Berg3d34deb2009-06-18 17:25:11 +02005037 local_bh_enable();
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005038}
Denis Kenzior91180642018-03-26 12:52:50 -05005039
5040int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
5041 const u8 *buf, size_t len,
5042 const u8 *dest, __be16 proto, bool unencrypted)
5043{
5044 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5045 struct ieee80211_local *local = sdata->local;
5046 struct sk_buff *skb;
5047 struct ethhdr *ehdr;
5048 u32 flags;
5049
5050 /* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
5051 * or Pre-Authentication
5052 */
5053 if (proto != sdata->control_port_protocol &&
5054 proto != cpu_to_be16(ETH_P_PREAUTH))
5055 return -EINVAL;
5056
5057 if (unencrypted)
5058 flags = IEEE80211_TX_INTFL_DONT_ENCRYPT;
5059 else
5060 flags = 0;
5061
5062 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5063 sizeof(struct ethhdr) + len);
5064 if (!skb)
5065 return -ENOMEM;
5066
5067 skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
5068
5069 skb_put_data(skb, buf, len);
5070
5071 ehdr = skb_push(skb, sizeof(struct ethhdr));
5072 memcpy(ehdr->h_dest, dest, ETH_ALEN);
5073 memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
5074 ehdr->h_proto = proto;
5075
5076 skb->dev = dev;
5077 skb->protocol = htons(ETH_P_802_3);
5078 skb_reset_network_header(skb);
5079 skb_reset_mac_header(skb);
5080
Denis Kenziore7441c92018-06-19 10:39:50 -05005081 local_bh_disable();
Rajkumar Manoharan06016772019-04-11 13:47:25 -07005082 __ieee80211_subif_start_xmit(skb, skb->dev, flags, 0);
Denis Kenziore7441c92018-06-19 10:39:50 -05005083 local_bh_enable();
Denis Kenzior91180642018-03-26 12:52:50 -05005084
5085 return 0;
5086}
Rajkumar Manoharan8828f812019-04-11 13:47:26 -07005087
5088int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
5089 const u8 *buf, size_t len)
5090{
5091 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5092 struct ieee80211_local *local = sdata->local;
5093 struct sk_buff *skb;
5094
5095 skb = dev_alloc_skb(local->hw.extra_tx_headroom + len +
5096 30 + /* header size */
5097 18); /* 11s header size */
5098 if (!skb)
5099 return -ENOMEM;
5100
5101 skb_reserve(skb, local->hw.extra_tx_headroom);
5102 skb_put_data(skb, buf, len);
5103
5104 skb->dev = dev;
5105 skb->protocol = htons(ETH_P_802_3);
5106 skb_reset_network_header(skb);
5107 skb_reset_mac_header(skb);
5108
5109 local_bh_disable();
5110 __ieee80211_subif_start_xmit(skb, skb->dev, 0,
5111 IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP);
Johannes Berge2ebc742007-07-27 15:43:22 +02005112 local_bh_enable();
5113
5114 return 0;
5115}