blob: 2c0fec888021cdd01d88e1f5d34cb65569f59e5c [file] [log] [blame]
Johannes Berge2ebc742007-07-27 15:43:22 +02001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
Johannes Bergd98ad832014-09-03 15:24:57 +03006 * Copyright 2013-2014 Intel Mobile Communications GmbH
Sara Sharon914eac22018-04-20 13:49:19 +03007 * Copyright (C) 2018 Intel Corporation
Johannes Berge2ebc742007-07-27 15:43:22 +02008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 *
14 * Transmit and frame generation functions.
15 */
16
17#include <linux/kernel.h>
18#include <linux/slab.h>
19#include <linux/skbuff.h>
Michael Braunebceec82016-11-22 11:52:18 +010020#include <linux/if_vlan.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020021#include <linux/etherdevice.h>
22#include <linux/bitmap.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040023#include <linux/rcupdate.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070025#include <net/net_namespace.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020026#include <net/ieee80211_radiotap.h>
27#include <net/cfg80211.h>
28#include <net/mac80211.h>
Michal Kazior5caa3282016-05-19 10:37:51 +020029#include <net/codel.h>
30#include <net/codel_impl.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020031#include <asm/unaligned.h>
Michal Kaziorfa962b92016-05-19 10:37:49 +020032#include <net/fq_impl.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020033
34#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020035#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040036#include "led.h"
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +010037#include "mesh.h"
Johannes Berge2ebc742007-07-27 15:43:22 +020038#include "wep.h"
39#include "wpa.h"
40#include "wme.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040041#include "rate.h"
Johannes Berge2ebc742007-07-27 15:43:22 +020042
Johannes Berge2ebc742007-07-27 15:43:22 +020043/* misc utils */
44
Johannes Berg5a490512015-04-22 17:10:38 +020045static inline void ieee80211_tx_stats(struct net_device *dev, u32 len)
46{
47 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
48
49 u64_stats_update_begin(&tstats->syncp);
50 tstats->tx_packets++;
51 tstats->tx_bytes += len;
52 u64_stats_update_end(&tstats->syncp);
53}
54
Johannes Berg252b86c2011-11-16 15:28:55 +010055static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
56 struct sk_buff *skb, int group_addr,
Johannes Berg03f93c32008-06-25 14:36:27 +030057 int next_frag_len)
Johannes Berge2ebc742007-07-27 15:43:22 +020058{
Simon Wunderlich2103dec2013-07-08 16:55:53 +020059 int rate, mrate, erp, dur, i, shift = 0;
Johannes Berg2e92e6f2008-05-15 12:55:27 +020060 struct ieee80211_rate *txrate;
Johannes Berge2ebc742007-07-27 15:43:22 +020061 struct ieee80211_local *local = tx->local;
Johannes Berg8318d782008-01-24 19:38:38 +010062 struct ieee80211_supported_band *sband;
Harvey Harrison358c8d92008-07-15 18:44:13 -070063 struct ieee80211_hdr *hdr;
Johannes Berg252b86c2011-11-16 15:28:55 +010064 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Simon Wunderlich2103dec2013-07-08 16:55:53 +020065 struct ieee80211_chanctx_conf *chanctx_conf;
66 u32 rate_flags = 0;
67
Felix Fietkau95cd4702016-12-14 20:46:57 +010068 /* assume HW handles this */
69 if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))
70 return 0;
71
Simon Wunderlich2103dec2013-07-08 16:55:53 +020072 rcu_read_lock();
73 chanctx_conf = rcu_dereference(tx->sdata->vif.chanctx_conf);
74 if (chanctx_conf) {
75 shift = ieee80211_chandef_get_shift(&chanctx_conf->def);
76 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
77 }
78 rcu_read_unlock();
Johannes Berge6a98542008-10-21 12:40:02 +020079
Johannes Berge6a98542008-10-21 12:40:02 +020080 /* uh huh? */
Felix Fietkau0d528d82013-04-22 16:14:41 +020081 if (WARN_ON_ONCE(tx->rate.idx < 0))
Johannes Berge6a98542008-10-21 12:40:02 +020082 return 0;
Johannes Berge2ebc742007-07-27 15:43:22 +020083
Johannes Berg2d565772012-07-23 15:12:51 +020084 sband = local->hw.wiphy->bands[info->band];
Felix Fietkau0d528d82013-04-22 16:14:41 +020085 txrate = &sband->bitrates[tx->rate.idx];
Johannes Berg8318d782008-01-24 19:38:38 +010086
Johannes Berge6a98542008-10-21 12:40:02 +020087 erp = txrate->flags & IEEE80211_RATE_ERP_G;
Johannes Berge2ebc742007-07-27 15:43:22 +020088
89 /*
90 * data and mgmt (except PS Poll):
91 * - during CFP: 32768
92 * - during contention period:
93 * if addr1 is group address: 0
94 * if more fragments = 0 and addr1 is individual address: time to
95 * transmit one ACK plus SIFS
96 * if more fragments = 1 and addr1 is individual address: time to
97 * transmit next fragment plus 2 x ACK plus 3 x SIFS
98 *
99 * IEEE 802.11, 9.6:
100 * - control response frame (CTS or ACK) shall be transmitted using the
101 * same rate as the immediately previous frame in the frame exchange
102 * sequence, if this rate belongs to the PHY mandatory rates, or else
103 * at the highest possible rate belonging to the PHY rates in the
104 * BSSBasicRateSet
105 */
Johannes Berg252b86c2011-11-16 15:28:55 +0100106 hdr = (struct ieee80211_hdr *)skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -0700107 if (ieee80211_is_ctl(hdr->frame_control)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200108 /* TODO: These control frames are not currently sent by
Johannes Bergccd7b362008-09-11 00:01:56 +0200109 * mac80211, but should they be implemented, this function
Johannes Berge2ebc742007-07-27 15:43:22 +0200110 * needs to be updated to support duration field calculation.
111 *
112 * RTS: time needed to transmit pending data/mgmt frame plus
113 * one CTS frame plus one ACK frame plus 3 x SIFS
114 * CTS: duration of immediately previous RTS minus time
115 * required to transmit CTS and its SIFS
116 * ACK: 0 if immediately previous directed data/mgmt had
117 * more=0, with more=1 duration in ACK frame is duration
118 * from previous frame minus time needed to transmit ACK
119 * and its SIFS
120 * PS Poll: BIT(15) | BIT(14) | aid
121 */
122 return 0;
123 }
124
125 /* data/mgmt */
126 if (0 /* FIX: data/mgmt during CFP */)
Johannes Berg03f93c32008-06-25 14:36:27 +0300127 return cpu_to_le16(32768);
Johannes Berge2ebc742007-07-27 15:43:22 +0200128
129 if (group_addr) /* Group address as the destination - no ACK */
130 return 0;
131
132 /* Individual destination address:
133 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
134 * CTS and ACK frames shall be transmitted using the highest rate in
135 * basic rate set that is less than or equal to the rate of the
136 * immediately previous frame and that is using the same modulation
137 * (CCK or OFDM). If no basic rate set matches with these requirements,
138 * the highest mandatory rate of the PHY that is less than or equal to
139 * the rate of the previous frame is used.
140 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
141 */
142 rate = -1;
Johannes Berg8318d782008-01-24 19:38:38 +0100143 /* use lowest available if everything fails */
144 mrate = sband->bitrates[0].bitrate;
145 for (i = 0; i < sband->n_bitrates; i++) {
146 struct ieee80211_rate *r = &sband->bitrates[i];
147
148 if (r->bitrate > txrate->bitrate)
Johannes Berge2ebc742007-07-27 15:43:22 +0200149 break;
150
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200151 if ((rate_flags & r->flags) != rate_flags)
152 continue;
153
Johannes Bergbda39332008-10-11 01:51:51 +0200154 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200155 rate = DIV_ROUND_UP(r->bitrate, 1 << shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200156
Johannes Berg8318d782008-01-24 19:38:38 +0100157 switch (sband->band) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200158 case NL80211_BAND_2GHZ: {
Johannes Berg8318d782008-01-24 19:38:38 +0100159 u32 flag;
160 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
161 flag = IEEE80211_RATE_MANDATORY_G;
162 else
163 flag = IEEE80211_RATE_MANDATORY_B;
164 if (r->flags & flag)
165 mrate = r->bitrate;
166 break;
167 }
Johannes Berg57fbcce2016-04-12 15:56:15 +0200168 case NL80211_BAND_5GHZ:
Johannes Berg8318d782008-01-24 19:38:38 +0100169 if (r->flags & IEEE80211_RATE_MANDATORY_A)
170 mrate = r->bitrate;
171 break;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200172 case NL80211_BAND_60GHZ:
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300173 /* TODO, for now fall through */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200174 case NUM_NL80211_BANDS:
Johannes Berg8318d782008-01-24 19:38:38 +0100175 WARN_ON(1);
176 break;
177 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200178 }
179 if (rate == -1) {
180 /* No matching basic rate found; use highest suitable mandatory
181 * PHY rate */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200182 rate = DIV_ROUND_UP(mrate, 1 << shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200183 }
184
Simon Wunderlich6674f212011-11-21 21:34:30 +0100185 /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
186 if (ieee80211_is_data_qos(hdr->frame_control) &&
Claudio Pisac26a0e12012-05-28 13:06:25 +0100187 *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
Simon Wunderlich6674f212011-11-21 21:34:30 +0100188 dur = 0;
189 else
190 /* Time needed to transmit ACK
191 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
192 * to closest integer */
Michal Kazior4ee73f32012-04-11 08:47:56 +0200193 dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200194 tx->sdata->vif.bss_conf.use_short_preamble,
195 shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200196
197 if (next_frag_len) {
198 /* Frame is fragmented: duration increases with time needed to
199 * transmit next fragment plus ACK and 2 x SIFS. */
200 dur *= 2; /* ACK + SIFS */
201 /* next fragment */
Michal Kazior4ee73f32012-04-11 08:47:56 +0200202 dur += ieee80211_frame_duration(sband->band, next_frag_len,
Johannes Berg8318d782008-01-24 19:38:38 +0100203 txrate->bitrate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200204 tx->sdata->vif.bss_conf.use_short_preamble,
205 shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200206 }
207
Johannes Berg03f93c32008-06-25 14:36:27 +0300208 return cpu_to_le16(dur);
Johannes Berge2ebc742007-07-27 15:43:22 +0200209}
210
Johannes Berge2ebc742007-07-27 15:43:22 +0200211/* tx handlers */
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200212static ieee80211_tx_result debug_noinline
213ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
214{
215 struct ieee80211_local *local = tx->local;
Kalle Valo0c742112010-01-12 10:42:53 +0200216 struct ieee80211_if_managed *ifmgd;
Andrei Otcheretianski94a5b3a2018-09-05 08:06:14 +0300217 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200218
219 /* driver doesn't support power save */
Johannes Berg30686bf2015-06-02 21:39:54 +0200220 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200221 return TX_CONTINUE;
222
223 /* hardware does dynamic power save */
Johannes Berg30686bf2015-06-02 21:39:54 +0200224 if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200225 return TX_CONTINUE;
226
227 /* dynamic power save disabled */
228 if (local->hw.conf.dynamic_ps_timeout <= 0)
229 return TX_CONTINUE;
230
231 /* we are scanning, don't enable power save */
232 if (local->scanning)
233 return TX_CONTINUE;
234
235 if (!local->ps_sdata)
236 return TX_CONTINUE;
237
238 /* No point if we're going to suspend */
239 if (local->quiescing)
240 return TX_CONTINUE;
241
Kalle Valo0c742112010-01-12 10:42:53 +0200242 /* dynamic ps is supported only in managed mode */
243 if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
244 return TX_CONTINUE;
245
Andrei Otcheretianski94a5b3a2018-09-05 08:06:14 +0300246 if (unlikely(info->flags & IEEE80211_TX_INTFL_OFFCHAN_TX_OK))
247 return TX_CONTINUE;
248
Kalle Valo0c742112010-01-12 10:42:53 +0200249 ifmgd = &tx->sdata->u.mgd;
250
251 /*
252 * Don't wakeup from power save if u-apsd is enabled, voip ac has
253 * u-apsd enabled and the frame is in voip class. This effectively
254 * means that even if all access categories have u-apsd enabled, in
255 * practise u-apsd is only used with the voip ac. This is a
256 * workaround for the case when received voip class packets do not
257 * have correct qos tag for some reason, due the network or the
258 * peer application.
259 *
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200260 * Note: ifmgd->uapsd_queues access is racy here. If the value is
Kalle Valo0c742112010-01-12 10:42:53 +0200261 * changed via debugfs, user needs to reassociate manually to have
262 * everything in sync.
263 */
Johannes Berg4875d30d2012-03-27 14:18:37 +0200264 if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
265 (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
266 skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
Kalle Valo0c742112010-01-12 10:42:53 +0200267 return TX_CONTINUE;
268
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200269 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
270 ieee80211_stop_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +0100271 IEEE80211_MAX_QUEUE_MAP,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300272 IEEE80211_QUEUE_STOP_REASON_PS,
273 false);
Vivek Natarajandb285692011-02-18 17:18:03 +0530274 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200275 ieee80211_queue_work(&local->hw,
276 &local->dynamic_ps_disable_work);
277 }
278
Luciano Coelho5db1c072011-05-03 21:40:08 +0300279 /* Don't restart the timer if we're not disassociated */
280 if (!ifmgd->associated)
281 return TX_CONTINUE;
282
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200283 mod_timer(&local->dynamic_ps_timer, jiffies +
284 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
285
286 return TX_CONTINUE;
287}
Johannes Berge2ebc742007-07-27 15:43:22 +0200288
Johannes Bergd9e8a702008-06-30 15:10:44 +0200289static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100290ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200291{
Harvey Harrison358c8d92008-07-15 18:44:13 -0700292
Johannes Berge039fa42008-05-15 12:55:29 +0200293 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200294 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200295 bool assoc = false;
Johannes Berge2ebc742007-07-27 15:43:22 +0200296
Johannes Berge039fa42008-05-15 12:55:29 +0200297 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100298 return TX_CONTINUE;
Johannes Berg58d41852007-09-26 17:53:18 +0200299
Ben Greearb23b0252011-02-04 11:54:17 -0800300 if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
301 test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
Kalle Valoa9a6fff2009-03-18 14:06:44 +0200302 !ieee80211_is_probe_req(hdr->frame_control) &&
303 !ieee80211_is_nullfunc(hdr->frame_control))
304 /*
305 * When software scanning only nullfunc frames (to notify
306 * the sleep state to the AP) and probe requests (for the
307 * active scan) are allowed, all other frames should not be
308 * sent and we should not get here, but if we do
309 * nonetheless, drop them to avoid sending them
310 * off-channel. See the link below and
311 * ieee80211_start_scan() for more.
312 *
313 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
314 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100315 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200316
Rostislav Lisovy239281f2014-11-03 10:33:19 +0100317 if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
318 return TX_CONTINUE;
319
Bill Jordan1be7fe82010-10-01 11:20:41 -0400320 if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
321 return TX_CONTINUE;
322
Johannes Berg5cf121c2008-02-25 16:27:43 +0100323 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100324 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200325
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200326 if (tx->sta)
327 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge2ebc742007-07-27 15:43:22 +0200328
Johannes Berg5cf121c2008-02-25 16:27:43 +0100329 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200330 if (unlikely(!assoc &&
Harvey Harrison358c8d92008-07-15 18:44:13 -0700331 ieee80211_is_data(hdr->frame_control))) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200332#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200333 sdata_info(tx->sdata,
334 "dropped data frame to not associated station %pM\n",
335 hdr->addr1);
336#endif
Johannes Berge2ebc742007-07-27 15:43:22 +0200337 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100338 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200339 }
Michael Braun72f15d52016-10-10 19:12:21 +0200340 } else if (unlikely(ieee80211_is_data(hdr->frame_control) &&
341 ieee80211_vif_get_num_mcast_if(tx->sdata) == 0)) {
Johannes Berg29623892011-12-14 12:20:31 +0100342 /*
343 * No associated STAs - no need to send multicast
344 * frames.
345 */
346 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200347 }
348
Johannes Berg9ae54c82008-01-31 19:48:20 +0100349 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200350}
351
Johannes Berge2ebc742007-07-27 15:43:22 +0200352/* This function is called whenever the AP is about to exceed the maximum limit
353 * of buffered frames for power saving STAs. This situation should not really
354 * happen often during normal operation, so dropping the oldest buffered packet
355 * from each queue should be OK to make some room for new frames. */
356static void purge_old_ps_buffers(struct ieee80211_local *local)
357{
358 int total = 0, purged = 0;
359 struct sk_buff *skb;
360 struct ieee80211_sub_if_data *sdata;
361 struct sta_info *sta;
362
Johannes Berg79010422007-09-18 17:29:21 -0400363 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Marco Porschd012a602012-10-10 12:39:50 -0700364 struct ps_data *ps;
365
366 if (sdata->vif.type == NL80211_IFTYPE_AP)
367 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100368 else if (ieee80211_vif_is_mesh(&sdata->vif))
369 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700370 else
Johannes Berge2ebc742007-07-27 15:43:22 +0200371 continue;
Marco Porschd012a602012-10-10 12:39:50 -0700372
373 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +0200374 if (skb) {
375 purged++;
Felix Fietkau6b07d9c2016-08-02 11:13:41 +0200376 ieee80211_free_txskb(&local->hw, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +0200377 }
Marco Porschd012a602012-10-10 12:39:50 -0700378 total += skb_queue_len(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +0200379 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200380
Johannes Berg948d8872011-09-29 16:04:29 +0200381 /*
382 * Drop one frame from each station from the lowest-priority
383 * AC that has frames at all.
384 */
Johannes Bergd0709a62008-02-25 16:27:46 +0100385 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg948d8872011-09-29 16:04:29 +0200386 int ac;
387
388 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
389 skb = skb_dequeue(&sta->ps_tx_buf[ac]);
390 total += skb_queue_len(&sta->ps_tx_buf[ac]);
391 if (skb) {
392 purged++;
Felix Fietkauc3e77242012-10-08 14:39:33 +0200393 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg948d8872011-09-29 16:04:29 +0200394 break;
395 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200396 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200397 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100398
Johannes Berge2ebc742007-07-27 15:43:22 +0200399 local->total_ps_buffered = total;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200400 ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
Johannes Berge2ebc742007-07-27 15:43:22 +0200401}
402
Johannes Berg9ae54c82008-01-31 19:48:20 +0100403static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100404ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200405{
Johannes Berge039fa42008-05-15 12:55:29 +0200406 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700407 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Marco Porschd012a602012-10-10 12:39:50 -0700408 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +0200409
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100410 /*
411 * broadcast/multicast frame
412 *
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100413 * If any of the associated/peer stations is in power save mode,
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100414 * the frame is buffered to be sent after DTIM beacon frame.
415 * This is done either by the hardware or us.
416 */
417
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100418 /* powersaving STAs currently only in AP/VLAN/mesh mode */
Marco Porschd012a602012-10-10 12:39:50 -0700419 if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
420 tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
421 if (!tx->sdata->bss)
422 return TX_CONTINUE;
423
424 ps = &tx->sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100425 } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
426 ps = &tx->sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700427 } else {
Johannes Berg3e122be2008-07-09 14:40:34 +0200428 return TX_CONTINUE;
Marco Porschd012a602012-10-10 12:39:50 -0700429 }
430
Johannes Berg3e122be2008-07-09 14:40:34 +0200431
432 /* no buffering for ordered frames */
Harvey Harrison358c8d92008-07-15 18:44:13 -0700433 if (ieee80211_has_order(hdr->frame_control))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100434 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100435
Johannes Berg08b99392014-07-07 12:01:11 +0200436 if (ieee80211_is_probe_req(hdr->frame_control))
437 return TX_CONTINUE;
438
Johannes Berg30686bf2015-06-02 21:39:54 +0200439 if (ieee80211_hw_check(&tx->local->hw, QUEUE_CONTROL))
Johannes Bergf4d57942013-05-28 17:24:15 +0200440 info->hw_queue = tx->sdata->vif.cab_queue;
441
Felix Fietkau9ec11902018-11-28 22:39:16 +0100442 /* no stations in PS mode and no buffered packets */
443 if (!atomic_read(&ps->num_sta_ps) && skb_queue_empty(&ps->bc_buf))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100444 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100445
Johannes Berg62b517c2009-10-29 12:19:21 +0100446 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
Johannes Berg62b12082009-08-10 16:04:15 +0200447
Johannes Berg62b517c2009-10-29 12:19:21 +0100448 /* device releases frame after DTIM beacon */
Johannes Berg30686bf2015-06-02 21:39:54 +0200449 if (!ieee80211_hw_check(&tx->local->hw, HOST_BROADCAST_PS_BUFFERING))
Johannes Berg62b12082009-08-10 16:04:15 +0200450 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200451
Johannes Berg62b12082009-08-10 16:04:15 +0200452 /* buffered in mac80211 */
453 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
454 purge_old_ps_buffers(tx->local);
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100455
Marco Porschd012a602012-10-10 12:39:50 -0700456 if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200457 ps_dbg(tx->sdata,
458 "BC TX buffer full - dropping the oldest frame\n");
Felix Fietkau6b07d9c2016-08-02 11:13:41 +0200459 ieee80211_free_txskb(&tx->local->hw, skb_dequeue(&ps->bc_buf));
Johannes Berg62b12082009-08-10 16:04:15 +0200460 } else
461 tx->local->total_ps_buffered++;
462
Marco Porschd012a602012-10-10 12:39:50 -0700463 skb_queue_tail(&ps->bc_buf, tx->skb);
Johannes Berg62b12082009-08-10 16:04:15 +0200464
465 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200466}
467
Jouni Malinenfb733332009-01-08 13:32:00 +0200468static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
469 struct sk_buff *skb)
470{
471 if (!ieee80211_is_mgmt(fc))
472 return 0;
473
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200474 if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
Jouni Malinenfb733332009-01-08 13:32:00 +0200475 return 0;
476
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100477 if (!ieee80211_is_robust_mgmt_frame(skb))
Jouni Malinenfb733332009-01-08 13:32:00 +0200478 return 0;
479
480 return 1;
481}
482
Johannes Berg9ae54c82008-01-31 19:48:20 +0100483static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100484ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200485{
486 struct sta_info *sta = tx->sta;
Johannes Berge039fa42008-05-15 12:55:29 +0200487 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berg08b99392014-07-07 12:01:11 +0200488 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300489 struct ieee80211_local *local = tx->local;
Johannes Berge2ebc742007-07-27 15:43:22 +0200490
Johannes Berg02f2f1a2012-02-27 12:18:30 +0100491 if (unlikely(!sta))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100492 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200493
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200494 if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
Johannes Berg5ac2e352014-05-27 16:32:27 +0200495 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
496 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
Johannes Berg02f2f1a2012-02-27 12:18:30 +0100497 !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
Johannes Berg948d8872011-09-29 16:04:29 +0200498 int ac = skb_get_queue_mapping(tx->skb);
499
Johannes Berg08b99392014-07-07 12:01:11 +0200500 if (ieee80211_is_mgmt(hdr->frame_control) &&
501 !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
502 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
503 return TX_CONTINUE;
504 }
505
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200506 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
507 sta->sta.addr, sta->sta.aid, ac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200508 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
509 purge_old_ps_buffers(tx->local);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200510
511 /* sync with ieee80211_sta_ps_deliver_wakeup */
512 spin_lock(&sta->ps_lock);
513 /*
514 * STA woke up the meantime and all the frames on ps_tx_buf have
515 * been queued to pending queue. No reordering can happen, go
516 * ahead and Tx the packet.
517 */
518 if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
Johannes Berg5ac2e352014-05-27 16:32:27 +0200519 !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
520 !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200521 spin_unlock(&sta->ps_lock);
522 return TX_CONTINUE;
523 }
524
Johannes Berg948d8872011-09-29 16:04:29 +0200525 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
526 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200527 ps_dbg(tx->sdata,
528 "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
529 sta->sta.addr, ac);
Felix Fietkauc3e77242012-10-08 14:39:33 +0200530 ieee80211_free_txskb(&local->hw, old);
Johannes Berge2ebc742007-07-27 15:43:22 +0200531 } else
532 tx->local->total_ps_buffered++;
Johannes Berg004c8722008-02-20 11:21:35 +0100533
Johannes Berge039fa42008-05-15 12:55:29 +0200534 info->control.jiffies = jiffies;
Johannes Berg5061b0c2009-07-14 00:33:34 +0200535 info->control.vif = &tx->sdata->vif;
Johannes Berg8f77f382009-06-07 21:58:37 +0200536 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Johannes Berg03c8c062014-01-09 01:45:28 +0100537 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berg948d8872011-09-29 16:04:29 +0200538 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200539 spin_unlock(&sta->ps_lock);
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300540
541 if (!timer_pending(&local->sta_cleanup))
542 mod_timer(&local->sta_cleanup,
543 round_jiffies(jiffies +
544 STA_INFO_CLEANUP_INTERVAL));
545
Johannes Bergc868cb352011-09-29 16:04:27 +0200546 /*
547 * We queued up some frames, so the TIM bit might
548 * need to be set, recalculate it.
549 */
550 sta_info_recalc_tim(sta);
551
Johannes Berg9ae54c82008-01-31 19:48:20 +0100552 return TX_QUEUED;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200553 } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
554 ps_dbg(tx->sdata,
555 "STA %pM in PS mode, but polling/in SP -> send frame\n",
556 sta->sta.addr);
Johannes Berge2ebc742007-07-27 15:43:22 +0200557 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200558
Johannes Berg9ae54c82008-01-31 19:48:20 +0100559 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200560}
561
Johannes Bergd9e8a702008-06-30 15:10:44 +0200562static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100563ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200564{
Johannes Berg5cf121c2008-02-25 16:27:43 +0100565 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100566 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200567
Johannes Berg5cf121c2008-02-25 16:27:43 +0100568 if (tx->flags & IEEE80211_TX_UNICAST)
Johannes Berge2ebc742007-07-27 15:43:22 +0200569 return ieee80211_tx_h_unicast_ps_buf(tx);
570 else
571 return ieee80211_tx_h_multicast_ps_buf(tx);
572}
573
Johannes Bergd9e8a702008-06-30 15:10:44 +0200574static ieee80211_tx_result debug_noinline
Johannes Berga621fa42010-08-27 14:26:54 +0300575ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
576{
577 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
578
Johannes Bergaf61a162013-07-02 18:09:12 +0200579 if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
580 if (tx->sdata->control_port_no_encrypt)
581 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
582 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
Jouni Malinen9c1c98a2015-02-26 15:50:50 +0200583 info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
Johannes Bergaf61a162013-07-02 18:09:12 +0200584 }
Johannes Berga621fa42010-08-27 14:26:54 +0300585
586 return TX_CONTINUE;
587}
588
589static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100590ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200591{
Johannes Berg46e6de12012-07-04 18:10:07 +0200592 struct ieee80211_key *key;
Johannes Berge039fa42008-05-15 12:55:29 +0200593 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700594 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400595
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200596 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
Johannes Berge2ebc742007-07-27 15:43:22 +0200597 tx->key = NULL;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200598 else if (tx->sta &&
599 (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
Johannes Bergd4e46a32007-09-14 11:10:24 -0400600 tx->key = key;
Masashi Honma46f6b062016-06-22 19:55:20 +0900601 else if (ieee80211_is_group_privacy_action(tx->skb) &&
602 (key = rcu_dereference(tx->sdata->default_multicast_key)))
603 tx->key = key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200604 else if (ieee80211_is_mgmt(hdr->frame_control) &&
Jouni Malinenecbcd322010-03-29 23:35:23 -0700605 is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100606 ieee80211_is_robust_mgmt_frame(tx->skb) &&
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200607 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
608 tx->key = key;
Johannes Bergf7e01042010-12-09 19:49:02 +0100609 else if (is_multicast_ether_addr(hdr->addr1) &&
610 (key = rcu_dereference(tx->sdata->default_multicast_key)))
611 tx->key = key;
612 else if (!is_multicast_ether_addr(hdr->addr1) &&
613 (key = rcu_dereference(tx->sdata->default_unicast_key)))
Johannes Bergd4e46a32007-09-14 11:10:24 -0400614 tx->key = key;
Johannes Berge8f4fb72015-03-20 11:37:36 +0100615 else
Johannes Berg46e6de12012-07-04 18:10:07 +0200616 tx->key = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +0200617
618 if (tx->key) {
Johannes Berg813d7662010-01-17 01:47:58 +0100619 bool skip_hw = false;
620
Johannes Berg011bfcc2007-09-17 01:29:25 -0400621 /* TODO: add threshold stuff again */
Johannes Berg176e4f82007-12-18 15:27:47 +0100622
Johannes Berg97359d12010-08-10 09:46:38 +0200623 switch (tx->key->conf.cipher) {
624 case WLAN_CIPHER_SUITE_WEP40:
625 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg97359d12010-08-10 09:46:38 +0200626 case WLAN_CIPHER_SUITE_TKIP:
Harvey Harrison358c8d92008-07-15 18:44:13 -0700627 if (!ieee80211_is_data_present(hdr->frame_control))
Johannes Berg176e4f82007-12-18 15:27:47 +0100628 tx->key = NULL;
629 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200630 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200631 case WLAN_CIPHER_SUITE_CCMP_256:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200632 case WLAN_CIPHER_SUITE_GCMP:
633 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinenfb733332009-01-08 13:32:00 +0200634 if (!ieee80211_is_data_present(hdr->frame_control) &&
635 !ieee80211_use_mfp(hdr->frame_control, tx->sta,
Masashi Honma46f6b062016-06-22 19:55:20 +0900636 tx->skb) &&
637 !ieee80211_is_group_privacy_action(tx->skb))
Jouni Malinenfb733332009-01-08 13:32:00 +0200638 tx->key = NULL;
Kalle Valo3b43a182010-01-23 20:27:14 +0200639 else
640 skip_hw = (tx->key->conf.flags &
Johannes Berge548c492012-09-04 17:08:23 +0200641 IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
Kalle Valo3b43a182010-01-23 20:27:14 +0200642 ieee80211_is_mgmt(hdr->frame_control);
Jouni Malinenfb733332009-01-08 13:32:00 +0200643 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200644 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200645 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Jouni Malinen8ade5382015-01-24 19:52:09 +0200646 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
647 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200648 if (!ieee80211_is_mgmt(hdr->frame_control))
649 tx->key = NULL;
650 break;
Johannes Berg176e4f82007-12-18 15:27:47 +0100651 }
Johannes Berg813d7662010-01-17 01:47:58 +0100652
Johannes Berge54faf22013-01-29 11:41:38 +0100653 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
654 !ieee80211_is_deauth(hdr->frame_control)))
Johannes Berg95acac62011-07-12 12:30:59 +0200655 return TX_DROP;
656
Johannes Bergf12553e2010-01-22 22:07:59 +0100657 if (!skip_hw && tx->key &&
Johannes Berg382b1652010-01-25 11:36:16 +0100658 tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
Johannes Berg813d7662010-01-17 01:47:58 +0100659 info->control.hw_key = &tx->key->conf;
Johannes Berge2ebc742007-07-27 15:43:22 +0200660 }
661
Johannes Berg9ae54c82008-01-31 19:48:20 +0100662 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200663}
664
Johannes Bergd9e8a702008-06-30 15:10:44 +0200665static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100666ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200667{
Johannes Berge039fa42008-05-15 12:55:29 +0200668 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200669 struct ieee80211_hdr *hdr = (void *)tx->skb->data;
670 struct ieee80211_supported_band *sband;
Shanyu Zhaoa2c40242010-04-27 11:15:12 -0700671 u32 len;
Johannes Berge6a98542008-10-21 12:40:02 +0200672 struct ieee80211_tx_rate_control txrc;
Felix Fietkau0d528d82013-04-22 16:14:41 +0200673 struct ieee80211_sta_rates *ratetbl = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200674 bool assoc = false;
Johannes Berge6a98542008-10-21 12:40:02 +0200675
676 memset(&txrc, 0, sizeof(txrc));
Johannes Berg8318d782008-01-24 19:38:38 +0100677
Johannes Berg2d565772012-07-23 15:12:51 +0200678 sband = tx->local->hw.wiphy->bands[info->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200679
Shanyu Zhaoa2c40242010-04-27 11:15:12 -0700680 len = min_t(u32, tx->skb->len + FCS_LEN,
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200681 tx->local->hw.wiphy->frag_threshold);
Johannes Berg58d41852007-09-26 17:53:18 +0200682
Johannes Berge6a98542008-10-21 12:40:02 +0200683 /* set up the tx rate control struct we give the RC algo */
Johannes Berg005e4722012-02-26 11:24:35 +0100684 txrc.hw = &tx->local->hw;
Johannes Berge6a98542008-10-21 12:40:02 +0200685 txrc.sband = sband;
686 txrc.bss_conf = &tx->sdata->vif.bss_conf;
687 txrc.skb = tx->skb;
688 txrc.reported_rate.idx = -1;
Johannes Berg2d565772012-07-23 15:12:51 +0200689 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
Felix Fietkau2ffbe6d2013-04-16 13:38:42 +0200690
691 if (tx->sdata->rc_has_mcs_mask[info->band])
692 txrc.rate_idx_mcs_mask =
693 tx->sdata->rc_rateidx_mcs_mask[info->band];
694
Felix Fietkau8f0729b2010-11-11 15:07:23 +0100695 txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -0800696 tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
Bertold Van den Bergh5765f9f2015-08-05 16:02:28 +0200697 tx->sdata->vif.type == NL80211_IFTYPE_ADHOC ||
698 tx->sdata->vif.type == NL80211_IFTYPE_OCB);
Johannes Berg58d41852007-09-26 17:53:18 +0200699
Johannes Berge6a98542008-10-21 12:40:02 +0200700 /* set up RTS protection if desired */
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200701 if (len > tx->local->hw.wiphy->rts_threshold) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200702 txrc.rts = true;
Johannes Berge2ebc742007-07-27 15:43:22 +0200703 }
Johannes Berge6a98542008-10-21 12:40:02 +0200704
Felix Fietkau0d528d82013-04-22 16:14:41 +0200705 info->control.use_rts = txrc.rts;
Felix Fietkau991fec02013-04-16 13:38:43 +0200706 info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
707
Johannes Berge6a98542008-10-21 12:40:02 +0200708 /*
709 * Use short preamble if the BSS can handle it, but not for
710 * management frames unless we know the receiver can handle
711 * that -- the management frame might be to a station that
712 * just wants a probe response.
713 */
714 if (tx->sdata->vif.bss_conf.use_short_preamble &&
715 (ieee80211_is_data(hdr->frame_control) ||
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200716 (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
Felix Fietkau0d528d82013-04-22 16:14:41 +0200717 txrc.short_preamble = true;
718
719 info->control.short_preamble = txrc.short_preamble;
Johannes Berge6a98542008-10-21 12:40:02 +0200720
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +0100721 /* don't ask rate control when rate already injected via radiotap */
722 if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)
723 return TX_CONTINUE;
724
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200725 if (tx->sta)
726 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge6a98542008-10-21 12:40:02 +0200727
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700728 /*
729 * Lets not bother rate control if we're associated and cannot
730 * talk to the sta. This should not happen.
731 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200732 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700733 !rate_usable_index_exists(sband, &tx->sta->sta),
734 "%s: Dropped data frame as no usable bitrate found while "
735 "scanning and associated. Target station: "
736 "%pM on %d GHz band\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100737 tx->sdata->name, hdr->addr1,
Johannes Berg2d565772012-07-23 15:12:51 +0200738 info->band ? 5 : 2))
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700739 return TX_DROP;
740
741 /*
742 * If we're associated with the sta at this point we know we can at
743 * least send the frame at the lowest bit rate.
744 */
Johannes Berge6a98542008-10-21 12:40:02 +0200745 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
746
Felix Fietkau0d528d82013-04-22 16:14:41 +0200747 if (tx->sta && !info->control.skip_table)
748 ratetbl = rcu_dereference(tx->sta->sta.rates);
749
750 if (unlikely(info->control.rates[0].idx < 0)) {
751 if (ratetbl) {
752 struct ieee80211_tx_rate rate = {
753 .idx = ratetbl->rate[0].idx,
754 .flags = ratetbl->rate[0].flags,
755 .count = ratetbl->rate[0].count
756 };
757
758 if (ratetbl->rate[0].idx < 0)
759 return TX_DROP;
760
761 tx->rate = rate;
762 } else {
763 return TX_DROP;
764 }
765 } else {
766 tx->rate = info->control.rates[0];
767 }
Johannes Berge6a98542008-10-21 12:40:02 +0200768
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100769 if (txrc.reported_rate.idx < 0) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200770 txrc.reported_rate = tx->rate;
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100771 if (tx->sta && ieee80211_is_data(hdr->frame_control))
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200772 tx->sta->tx_stats.last_rate = txrc.reported_rate;
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100773 } else if (tx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200774 tx->sta->tx_stats.last_rate = txrc.reported_rate;
Johannes Berge6a98542008-10-21 12:40:02 +0200775
Felix Fietkau0d528d82013-04-22 16:14:41 +0200776 if (ratetbl)
777 return TX_CONTINUE;
778
Johannes Berge6a98542008-10-21 12:40:02 +0200779 if (unlikely(!info->control.rates[0].count))
780 info->control.rates[0].count = 1;
781
Gábor Stefanik99551512009-04-23 19:36:14 +0200782 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
783 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
784 info->control.rates[0].count = 1;
785
Johannes Berg9ae54c82008-01-31 19:48:20 +0100786 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200787}
788
Felix Fietkauba8c3d62015-03-27 21:30:37 +0100789static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
790{
791 u16 *seq = &sta->tid_seq[tid];
792 __le16 ret = cpu_to_le16(*seq);
793
794 /* Increase the sequence number. */
795 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
796
797 return ret;
798}
799
Johannes Bergd9e8a702008-06-30 15:10:44 +0200800static ieee80211_tx_result debug_noinline
Johannes Bergf591fa52008-07-10 11:21:26 +0200801ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
802{
803 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
804 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergf591fa52008-07-10 11:21:26 +0200805 int tid;
806
Johannes Berg25d834e2008-09-12 22:52:47 +0200807 /*
808 * Packet injection may want to control the sequence
809 * number, if we have no matching interface then we
810 * neither assign one ourselves nor ask the driver to.
811 */
Johannes Berg5061b0c2009-07-14 00:33:34 +0200812 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
Johannes Berg25d834e2008-09-12 22:52:47 +0200813 return TX_CONTINUE;
814
Johannes Bergf591fa52008-07-10 11:21:26 +0200815 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
816 return TX_CONTINUE;
817
818 if (ieee80211_hdrlen(hdr->frame_control) < 24)
819 return TX_CONTINUE;
820
Johannes Berg49a59542011-09-29 16:04:41 +0200821 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
822 return TX_CONTINUE;
823
Johannes Berg94778282008-10-10 13:21:59 +0200824 /*
825 * Anything but QoS data that has a sequence number field
826 * (is long enough) gets a sequence number from the global
Bob Copelandc4c205f2013-08-23 09:35:38 -0400827 * counter. QoS data frames with a multicast destination
828 * also use the global counter (802.11-2012 9.3.2.10).
Johannes Berg94778282008-10-10 13:21:59 +0200829 */
Bob Copelandc4c205f2013-08-23 09:35:38 -0400830 if (!ieee80211_is_data_qos(hdr->frame_control) ||
831 is_multicast_ether_addr(hdr->addr1)) {
Johannes Bergb9771d42018-05-28 15:47:41 +0200832 if (tx->flags & IEEE80211_TX_NO_SEQNO)
833 return TX_CONTINUE;
Johannes Berg94778282008-10-10 13:21:59 +0200834 /* driver should assign sequence number */
Johannes Bergf591fa52008-07-10 11:21:26 +0200835 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
Johannes Berg94778282008-10-10 13:21:59 +0200836 /* for pure STA mode without beacons, we can do it */
837 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
838 tx->sdata->sequence_number += 0x10;
Johannes Berg79c892b2014-11-21 14:26:31 +0100839 if (tx->sta)
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200840 tx->sta->tx_stats.msdu[IEEE80211_NUM_TIDS]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200841 return TX_CONTINUE;
842 }
843
844 /*
845 * This should be true for injected/management frames only, for
846 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
847 * above since they are not QoS-data frames.
848 */
849 if (!tx->sta)
850 return TX_CONTINUE;
851
852 /* include per-STA, per-TID sequence counter */
Sara Sharona1f2ba04c2018-02-19 14:48:40 +0200853 tid = ieee80211_get_tid(hdr);
Johannes Berge5a9f8d2015-10-16 17:54:47 +0200854 tx->sta->tx_stats.msdu[tid]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200855
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +0200856 hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
Johannes Bergf591fa52008-07-10 11:21:26 +0200857
858 return TX_CONTINUE;
859}
860
Johannes Berg252b86c2011-11-16 15:28:55 +0100861static int ieee80211_fragment(struct ieee80211_tx_data *tx,
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100862 struct sk_buff *skb, int hdrlen,
863 int frag_threshold)
864{
Johannes Berg252b86c2011-11-16 15:28:55 +0100865 struct ieee80211_local *local = tx->local;
Johannes Berga1a3fce2011-11-16 15:28:56 +0100866 struct ieee80211_tx_info *info;
Johannes Berg252b86c2011-11-16 15:28:55 +0100867 struct sk_buff *tmp;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100868 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
869 int pos = hdrlen + per_fragm;
870 int rem = skb->len - hdrlen - per_fragm;
871
872 if (WARN_ON(rem < 0))
873 return -EINVAL;
874
Johannes Berg252b86c2011-11-16 15:28:55 +0100875 /* first fragment was already added to queue by caller */
876
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100877 while (rem) {
878 int fraglen = per_fragm;
879
880 if (fraglen > rem)
881 fraglen = rem;
882 rem -= fraglen;
883 tmp = dev_alloc_skb(local->tx_headroom +
884 frag_threshold +
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200885 tx->sdata->encrypt_headroom +
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100886 IEEE80211_ENCRYPT_TAILROOM);
887 if (!tmp)
888 return -ENOMEM;
Johannes Berg252b86c2011-11-16 15:28:55 +0100889
890 __skb_queue_tail(&tx->skbs, tmp);
891
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200892 skb_reserve(tmp,
893 local->tx_headroom + tx->sdata->encrypt_headroom);
894
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100895 /* copy control information */
896 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
Johannes Berga1a3fce2011-11-16 15:28:56 +0100897
898 info = IEEE80211_SKB_CB(tmp);
899 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
900 IEEE80211_TX_CTL_FIRST_FRAGMENT);
901
902 if (rem)
903 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
904
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100905 skb_copy_queue_mapping(tmp, skb);
906 tmp->priority = skb->priority;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100907 tmp->dev = skb->dev;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100908
909 /* copy header and data */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200910 skb_put_data(tmp, skb->data, hdrlen);
911 skb_put_data(tmp, skb->data + pos, fraglen);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100912
913 pos += fraglen;
914 }
915
Johannes Berg252b86c2011-11-16 15:28:55 +0100916 /* adjust first fragment's length */
Johannes Berg338f9772014-02-01 00:16:23 +0100917 skb_trim(skb, hdrlen + per_fragm);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100918 return 0;
919}
920
Johannes Bergf591fa52008-07-10 11:21:26 +0200921static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +0200922ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
923{
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100924 struct sk_buff *skb = tx->skb;
925 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
926 struct ieee80211_hdr *hdr = (void *)skb->data;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200927 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100928 int hdrlen;
929 int fragnum;
Johannes Berge2454942008-05-15 12:55:28 +0200930
Johannes Berg252b86c2011-11-16 15:28:55 +0100931 /* no matter what happens, tx->skb moves to tx->skbs */
932 __skb_queue_tail(&tx->skbs, skb);
933 tx->skb = NULL;
934
Johannes Berga26eb272011-10-07 14:01:25 +0200935 if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
936 return TX_CONTINUE;
937
Sara Sharonf3fe4e92016-10-18 23:12:11 +0300938 if (ieee80211_hw_check(&tx->local->hw, SUPPORTS_TX_FRAG))
Johannes Berge2454942008-05-15 12:55:28 +0200939 return TX_CONTINUE;
940
Johannes Bergeefce912008-05-17 00:57:13 +0200941 /*
942 * Warn when submitting a fragmented A-MPDU frame and drop it.
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200943 * This scenario is handled in ieee80211_tx_prepare but extra
Ron Rindjunsky8d5e0d52008-06-12 15:42:29 +0300944 * caution taken here as fragmented ampdu may cause Tx stop.
Johannes Bergeefce912008-05-17 00:57:13 +0200945 */
Sujith8b30b1f2008-10-24 09:55:27 +0530946 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
Johannes Bergeefce912008-05-17 00:57:13 +0200947 return TX_DROP;
948
Harvey Harrison065e96052008-06-22 16:45:27 -0700949 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berge2454942008-05-15 12:55:28 +0200950
Johannes Berga26eb272011-10-07 14:01:25 +0200951 /* internal error, why isn't DONTFRAG set? */
Johannes Berg8ccd8f22009-04-29 23:35:56 +0200952 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100953 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200954
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100955 /*
956 * Now fragment the frame. This will allocate all the fragments and
957 * chain them (using skb as the first fragment) to skb->next.
958 * During transmission, we will remove the successfully transmitted
959 * fragments from this list. When the low-level driver rejects one
960 * of the fragments then we will simply pretend to accept the skb
961 * but store it away as pending.
962 */
Johannes Berg252b86c2011-11-16 15:28:55 +0100963 if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100964 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200965
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100966 /* update duration/seq/flags of fragments */
967 fragnum = 0;
Johannes Berg252b86c2011-11-16 15:28:55 +0100968
969 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100970 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
Johannes Berge2454942008-05-15 12:55:28 +0200971
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100972 hdr = (void *)skb->data;
973 info = IEEE80211_SKB_CB(skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200974
Johannes Berg252b86c2011-11-16 15:28:55 +0100975 if (!skb_queue_is_last(&tx->skbs, skb)) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100976 hdr->frame_control |= morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200977 /*
978 * No multi-rate retries for fragmented frames, that
979 * would completely throw off the NAV at other STAs.
980 */
981 info->control.rates[1].idx = -1;
982 info->control.rates[2].idx = -1;
983 info->control.rates[3].idx = -1;
Thomas Huehne3e1a0b2012-07-02 19:46:16 +0200984 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
Johannes Berge6a98542008-10-21 12:40:02 +0200985 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100986 } else {
987 hdr->frame_control &= ~morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200988 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100989 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
990 fragnum++;
Johannes Berg252b86c2011-11-16 15:28:55 +0100991 }
Johannes Berge2454942008-05-15 12:55:28 +0200992
993 return TX_CONTINUE;
Johannes Berge2454942008-05-15 12:55:28 +0200994}
995
Johannes Bergd9e8a702008-06-30 15:10:44 +0200996static ieee80211_tx_result debug_noinline
Johannes Bergfeff1f22009-08-10 16:01:54 +0200997ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
998{
Johannes Berg252b86c2011-11-16 15:28:55 +0100999 struct sk_buff *skb;
Johannes Berg560d2682013-02-05 10:55:21 +01001000 int ac = -1;
Johannes Bergfeff1f22009-08-10 16:01:54 +02001001
1002 if (!tx->sta)
1003 return TX_CONTINUE;
1004
Johannes Berg252b86c2011-11-16 15:28:55 +01001005 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg560d2682013-02-05 10:55:21 +01001006 ac = skb_get_queue_mapping(skb);
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001007 tx->sta->tx_stats.bytes[ac] += skb->len;
Johannes Berg252b86c2011-11-16 15:28:55 +01001008 }
Johannes Berg560d2682013-02-05 10:55:21 +01001009 if (ac >= 0)
Johannes Berge5a9f8d2015-10-16 17:54:47 +02001010 tx->sta->tx_stats.packets[ac]++;
Johannes Bergfeff1f22009-08-10 16:01:54 +02001011
1012 return TX_CONTINUE;
1013}
1014
1015static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +02001016ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
1017{
1018 if (!tx->key)
1019 return TX_CONTINUE;
1020
Johannes Berg97359d12010-08-10 09:46:38 +02001021 switch (tx->key->conf.cipher) {
1022 case WLAN_CIPHER_SUITE_WEP40:
1023 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge2454942008-05-15 12:55:28 +02001024 return ieee80211_crypto_wep_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001025 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berge2454942008-05-15 12:55:28 +02001026 return ieee80211_crypto_tkip_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001027 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001028 return ieee80211_crypto_ccmp_encrypt(
1029 tx, IEEE80211_CCMP_MIC_LEN);
1030 case WLAN_CIPHER_SUITE_CCMP_256:
1031 return ieee80211_crypto_ccmp_encrypt(
1032 tx, IEEE80211_CCMP_256_MIC_LEN);
Johannes Berg97359d12010-08-10 09:46:38 +02001033 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001034 return ieee80211_crypto_aes_cmac_encrypt(tx);
Jouni Malinen56c52da2015-01-24 19:52:08 +02001035 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1036 return ieee80211_crypto_aes_cmac_256_encrypt(tx);
Jouni Malinen8ade5382015-01-24 19:52:09 +02001037 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1038 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1039 return ieee80211_crypto_aes_gmac_encrypt(tx);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001040 case WLAN_CIPHER_SUITE_GCMP:
1041 case WLAN_CIPHER_SUITE_GCMP_256:
1042 return ieee80211_crypto_gcmp_encrypt(tx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +03001043 default:
Yoni Divinskyd32a1022012-01-16 15:18:59 +02001044 return ieee80211_crypto_hw_encrypt(tx);
Johannes Berge2454942008-05-15 12:55:28 +02001045 }
1046
Johannes Berge2454942008-05-15 12:55:28 +02001047 return TX_DROP;
1048}
1049
Johannes Bergd9e8a702008-06-30 15:10:44 +02001050static ieee80211_tx_result debug_noinline
Johannes Berg03f93c32008-06-25 14:36:27 +03001051ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1052{
Johannes Berg252b86c2011-11-16 15:28:55 +01001053 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001054 struct ieee80211_hdr *hdr;
1055 int next_len;
1056 bool group_addr;
Johannes Berg03f93c32008-06-25 14:36:27 +03001057
Johannes Berg252b86c2011-11-16 15:28:55 +01001058 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001059 hdr = (void *) skb->data;
Jouni Malinen7e0aae42009-05-19 19:25:58 +03001060 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1061 break; /* must not overwrite AID */
Johannes Berg252b86c2011-11-16 15:28:55 +01001062 if (!skb_queue_is_last(&tx->skbs, skb)) {
1063 struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1064 next_len = next->len;
1065 } else
1066 next_len = 0;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001067 group_addr = is_multicast_ether_addr(hdr->addr1);
Johannes Berg03f93c32008-06-25 14:36:27 +03001068
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001069 hdr->duration_id =
Johannes Berg252b86c2011-11-16 15:28:55 +01001070 ieee80211_duration(tx, skb, group_addr, next_len);
1071 }
Johannes Berg03f93c32008-06-25 14:36:27 +03001072
1073 return TX_CONTINUE;
1074}
1075
Johannes Berge2ebc742007-07-27 15:43:22 +02001076/* actual transmit path */
1077
Johannes Berga622ab72010-06-10 10:21:39 +02001078static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1079 struct sk_buff *skb,
1080 struct ieee80211_tx_info *info,
1081 struct tid_ampdu_tx *tid_tx,
1082 int tid)
1083{
1084 bool queued = false;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001085 bool reset_agg_timer = false;
Helmut Schaaaa454582012-03-07 17:20:30 +01001086 struct sk_buff *purge_skb = NULL;
Johannes Berga622ab72010-06-10 10:21:39 +02001087
1088 if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1089 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001090 reset_agg_timer = true;
Johannes Berg0ab33702010-06-10 10:21:42 +02001091 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1092 /*
1093 * nothing -- this aggregation session is being started
1094 * but that might still fail with the driver
1095 */
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001096 } else if (!tx->sta->sta.txq[tid]) {
Johannes Berga622ab72010-06-10 10:21:39 +02001097 spin_lock(&tx->sta->lock);
1098 /*
1099 * Need to re-check now, because we may get here
1100 *
1101 * 1) in the window during which the setup is actually
1102 * already done, but not marked yet because not all
1103 * packets are spliced over to the driver pending
1104 * queue yet -- if this happened we acquire the lock
1105 * either before or after the splice happens, but
1106 * need to recheck which of these cases happened.
1107 *
1108 * 2) during session teardown, if the OPERATIONAL bit
1109 * was cleared due to the teardown but the pointer
1110 * hasn't been assigned NULL yet (or we loaded it
1111 * before it was assigned) -- in this case it may
1112 * now be NULL which means we should just let the
1113 * packet pass through because splicing the frames
1114 * back is already done.
1115 */
Johannes Berg40b275b2011-05-13 14:15:49 +02001116 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
Johannes Berga622ab72010-06-10 10:21:39 +02001117
1118 if (!tid_tx) {
1119 /* do nothing, let packet pass through */
1120 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1121 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001122 reset_agg_timer = true;
Johannes Berga622ab72010-06-10 10:21:39 +02001123 } else {
1124 queued = true;
Emmanuel Grumbachf6d46712016-03-17 16:51:42 +02001125 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
1126 clear_sta_flag(tx->sta, WLAN_STA_SP);
1127 ps_dbg(tx->sta->sdata,
1128 "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
1129 tx->sta->sta.addr, tx->sta->sta.aid);
1130 }
Johannes Berga622ab72010-06-10 10:21:39 +02001131 info->control.vif = &tx->sdata->vif;
1132 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Emmanuel Grumbachfacde7f2016-03-17 16:51:41 +02001133 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berga622ab72010-06-10 10:21:39 +02001134 __skb_queue_tail(&tid_tx->pending, skb);
Helmut Schaaaa454582012-03-07 17:20:30 +01001135 if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1136 purge_skb = __skb_dequeue(&tid_tx->pending);
Johannes Berga622ab72010-06-10 10:21:39 +02001137 }
1138 spin_unlock(&tx->sta->lock);
Helmut Schaaaa454582012-03-07 17:20:30 +01001139
1140 if (purge_skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001141 ieee80211_free_txskb(&tx->local->hw, purge_skb);
Johannes Berga622ab72010-06-10 10:21:39 +02001142 }
1143
Nikolay Martynov285fa692011-11-22 21:50:28 -05001144 /* reset session timer */
Sara Sharon914eac22018-04-20 13:49:19 +03001145 if (reset_agg_timer)
Felix Fietkau12d3952f2012-03-18 22:58:06 +01001146 tid_tx->last_tx = jiffies;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001147
Johannes Berga622ab72010-06-10 10:21:39 +02001148 return queued;
1149}
1150
Johannes Berg58d41852007-09-26 17:53:18 +02001151/*
1152 * initialises @tx
Johannes Berg7c107702015-03-20 14:18:27 +01001153 * pass %NULL for the station if unknown, a valid pointer if known
1154 * or an ERR_PTR() if the station is known not to exist
Johannes Berg58d41852007-09-26 17:53:18 +02001155 */
Johannes Berg9ae54c82008-01-31 19:48:20 +01001156static ieee80211_tx_result
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001157ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1158 struct ieee80211_tx_data *tx,
Johannes Berg7c107702015-03-20 14:18:27 +01001159 struct sta_info *sta, struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02001160{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001161 struct ieee80211_local *local = sdata->local;
Johannes Berg58d41852007-09-26 17:53:18 +02001162 struct ieee80211_hdr *hdr;
Johannes Berge039fa42008-05-15 12:55:29 +02001163 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg68f2b512011-10-07 14:01:24 +02001164 int tid;
Johannes Berge2ebc742007-07-27 15:43:22 +02001165
1166 memset(tx, 0, sizeof(*tx));
1167 tx->skb = skb;
Johannes Berge2ebc742007-07-27 15:43:22 +02001168 tx->local = local;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001169 tx->sdata = sdata;
Johannes Berg252b86c2011-11-16 15:28:55 +01001170 __skb_queue_head_init(&tx->skbs);
Johannes Berge2ebc742007-07-27 15:43:22 +02001171
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001172 /*
1173 * If this flag is set to true anywhere, and we get here,
1174 * we are doing the needed processing, so remove the flag
1175 * now.
1176 */
1177 info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1178
Johannes Berg58d41852007-09-26 17:53:18 +02001179 hdr = (struct ieee80211_hdr *) skb->data;
1180
Johannes Berg7c107702015-03-20 14:18:27 +01001181 if (likely(sta)) {
1182 if (!IS_ERR(sta))
1183 tx->sta = sta;
1184 } else {
1185 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1186 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1187 if (!tx->sta && sdata->wdev.use_4addr)
1188 return TX_DROP;
1189 } else if (info->flags & (IEEE80211_TX_INTFL_NL80211_FRAME_TX |
1190 IEEE80211_TX_CTL_INJECTED) ||
1191 tx->sdata->control_port_protocol == tx->skb->protocol) {
1192 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1193 }
1194 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
1195 tx->sta = sta_info_get(sdata, hdr->addr1);
Felix Fietkau3f0e0b22010-01-08 18:15:13 +01001196 }
Johannes Berg58d41852007-09-26 17:53:18 +02001197
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001198 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
Johannes Berg49a59542011-09-29 16:04:41 +02001199 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001200 ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
1201 !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001202 struct tid_ampdu_tx *tid_tx;
1203
Sara Sharona1f2ba04c2018-02-19 14:48:40 +02001204 tid = ieee80211_get_tid(hdr);
Sujith8b30b1f2008-10-24 09:55:27 +05301205
Johannes Berga622ab72010-06-10 10:21:39 +02001206 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1207 if (tid_tx) {
1208 bool queued;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001209
Johannes Berga622ab72010-06-10 10:21:39 +02001210 queued = ieee80211_tx_prep_agg(tx, skb, info,
1211 tid_tx, tid);
1212
1213 if (unlikely(queued))
1214 return TX_QUEUED;
1215 }
Sujith8b30b1f2008-10-24 09:55:27 +05301216 }
1217
Jiri Slabybadffb72007-08-28 17:01:54 -04001218 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001219 tx->flags &= ~IEEE80211_TX_UNICAST;
Johannes Berge039fa42008-05-15 12:55:29 +02001220 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Simon Wunderlich6fd67e92011-11-18 14:20:42 +01001221 } else
Johannes Berg5cf121c2008-02-25 16:27:43 +01001222 tx->flags |= IEEE80211_TX_UNICAST;
Johannes Berg58d41852007-09-26 17:53:18 +02001223
Johannes Berga26eb272011-10-07 14:01:25 +02001224 if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1225 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1226 skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1227 info->flags & IEEE80211_TX_CTL_AMPDU)
1228 info->flags |= IEEE80211_TX_CTL_DONTFRAG;
Johannes Berg58d41852007-09-26 17:53:18 +02001229 }
1230
Johannes Berge2ebc742007-07-27 15:43:22 +02001231 if (!tx->sta)
Johannes Berge039fa42008-05-15 12:55:29 +02001232 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Felix Fietkauf7418bc2015-09-24 14:59:49 +02001233 else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
Johannes Berge039fa42008-05-15 12:55:29 +02001234 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Felix Fietkauf7418bc2015-09-24 14:59:49 +02001235 ieee80211_check_fast_xmit(tx->sta);
1236 }
Johannes Berg58d41852007-09-26 17:53:18 +02001237
Johannes Berge039fa42008-05-15 12:55:29 +02001238 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001239
Johannes Berg9ae54c82008-01-31 19:48:20 +01001240 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001241}
1242
Michal Kazior80a83cf2016-05-19 10:37:48 +02001243static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
1244 struct ieee80211_vif *vif,
Michal Kaziordbef5362017-01-13 13:32:51 +01001245 struct sta_info *sta,
Michal Kazior80a83cf2016-05-19 10:37:48 +02001246 struct sk_buff *skb)
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001247{
1248 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001249 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001250 struct ieee80211_txq *txq = NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001251
Felix Fietkauc3732a72016-02-28 15:19:53 +01001252 if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
1253 (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
Michal Kazior80a83cf2016-05-19 10:37:48 +02001254 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001255
Johannes Bergadf8ed02018-08-31 11:31:08 +03001256 if (unlikely(!ieee80211_is_data_present(hdr->frame_control))) {
1257 if ((!ieee80211_is_mgmt(hdr->frame_control) ||
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001258 ieee80211_is_bufferable_mmpdu(hdr->frame_control) ||
1259 vif->type == NL80211_IFTYPE_STATION) &&
Johannes Bergadf8ed02018-08-31 11:31:08 +03001260 sta && sta->uploaded) {
1261 /*
1262 * This will be NULL if the driver didn't set the
1263 * opt-in hardware flag.
1264 */
1265 txq = sta->sta.txq[IEEE80211_NUM_TIDS];
1266 }
1267 } else if (sta) {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001268 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1269
Michal Kaziordbef5362017-01-13 13:32:51 +01001270 if (!sta->uploaded)
1271 return NULL;
1272
1273 txq = sta->sta.txq[tid];
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001274 } else if (vif) {
1275 txq = vif->txq;
1276 }
1277
1278 if (!txq)
Michal Kazior80a83cf2016-05-19 10:37:48 +02001279 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001280
Michal Kazior80a83cf2016-05-19 10:37:48 +02001281 return to_txq_info(txq);
1282}
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001283
Michal Kazior5caa3282016-05-19 10:37:51 +02001284static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
1285{
1286 IEEE80211_SKB_CB(skb)->control.enqueue_time = codel_get_time();
1287}
1288
Michal Kazior5caa3282016-05-19 10:37:51 +02001289static u32 codel_skb_len_func(const struct sk_buff *skb)
1290{
1291 return skb->len;
1292}
1293
1294static codel_time_t codel_skb_time_func(const struct sk_buff *skb)
1295{
1296 const struct ieee80211_tx_info *info;
1297
1298 info = (const struct ieee80211_tx_info *)skb->cb;
1299 return info->control.enqueue_time;
1300}
1301
1302static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars,
1303 void *ctx)
1304{
1305 struct ieee80211_local *local;
1306 struct txq_info *txqi;
1307 struct fq *fq;
1308 struct fq_flow *flow;
1309
1310 txqi = ctx;
1311 local = vif_to_sdata(txqi->txq.vif)->local;
1312 fq = &local->fq;
1313
1314 if (cvars == &txqi->def_cvars)
1315 flow = &txqi->def_flow;
1316 else
1317 flow = &fq->flows[cvars - local->cvars];
1318
1319 return fq_flow_dequeue(fq, flow);
1320}
1321
1322static void codel_drop_func(struct sk_buff *skb,
1323 void *ctx)
1324{
1325 struct ieee80211_local *local;
1326 struct ieee80211_hw *hw;
1327 struct txq_info *txqi;
1328
1329 txqi = ctx;
1330 local = vif_to_sdata(txqi->txq.vif)->local;
1331 hw = &local->hw;
1332
1333 ieee80211_free_txskb(hw, skb);
1334}
1335
Michal Kaziorfa962b92016-05-19 10:37:49 +02001336static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
1337 struct fq_tin *tin,
1338 struct fq_flow *flow)
1339{
Michal Kazior5caa3282016-05-19 10:37:51 +02001340 struct ieee80211_local *local;
1341 struct txq_info *txqi;
1342 struct codel_vars *cvars;
1343 struct codel_params *cparams;
1344 struct codel_stats *cstats;
1345
1346 local = container_of(fq, struct ieee80211_local, fq);
1347 txqi = container_of(tin, struct txq_info, tin);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001348 cstats = &txqi->cstats;
Michal Kazior5caa3282016-05-19 10:37:51 +02001349
Toke Høiland-Jørgensen484a54c2017-04-06 11:38:26 +02001350 if (txqi->txq.sta) {
1351 struct sta_info *sta = container_of(txqi->txq.sta,
1352 struct sta_info, sta);
1353 cparams = &sta->cparams;
1354 } else {
1355 cparams = &local->cparams;
1356 }
1357
Michal Kazior5caa3282016-05-19 10:37:51 +02001358 if (flow == &txqi->def_flow)
1359 cvars = &txqi->def_cvars;
1360 else
1361 cvars = &local->cvars[flow - fq->flows];
1362
1363 return codel_dequeue(txqi,
1364 &flow->backlog,
1365 cparams,
1366 cvars,
1367 cstats,
1368 codel_skb_len_func,
1369 codel_skb_time_func,
1370 codel_drop_func,
1371 codel_dequeue_func);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001372}
1373
1374static void fq_skb_free_func(struct fq *fq,
1375 struct fq_tin *tin,
1376 struct fq_flow *flow,
1377 struct sk_buff *skb)
1378{
1379 struct ieee80211_local *local;
1380
1381 local = container_of(fq, struct ieee80211_local, fq);
1382 ieee80211_free_txskb(&local->hw, skb);
1383}
1384
1385static struct fq_flow *fq_flow_get_default_func(struct fq *fq,
1386 struct fq_tin *tin,
1387 int idx,
1388 struct sk_buff *skb)
1389{
1390 struct txq_info *txqi;
1391
1392 txqi = container_of(tin, struct txq_info, tin);
1393 return &txqi->def_flow;
1394}
1395
Michal Kazior80a83cf2016-05-19 10:37:48 +02001396static void ieee80211_txq_enqueue(struct ieee80211_local *local,
1397 struct txq_info *txqi,
1398 struct sk_buff *skb)
1399{
Michal Kaziorfa962b92016-05-19 10:37:49 +02001400 struct fq *fq = &local->fq;
1401 struct fq_tin *tin = &txqi->tin;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001402 u32 flow_idx = fq_flow_idx(fq, skb);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001403
Michal Kazior5caa3282016-05-19 10:37:51 +02001404 ieee80211_set_skb_enqueue_time(skb);
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001405
1406 spin_lock_bh(&fq->lock);
1407 fq_tin_enqueue(fq, tin, flow_idx, skb,
Michal Kaziorfa962b92016-05-19 10:37:49 +02001408 fq_skb_free_func,
1409 fq_flow_get_default_func);
Felix Fietkauf2af2df2019-03-16 18:06:32 +01001410 spin_unlock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001411}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001412
Johannes Berg2a9e2572017-10-06 11:53:33 +02001413static bool fq_vlan_filter_func(struct fq *fq, struct fq_tin *tin,
1414 struct fq_flow *flow, struct sk_buff *skb,
1415 void *data)
1416{
1417 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1418
1419 return info->control.vif == data;
1420}
1421
1422void ieee80211_txq_remove_vlan(struct ieee80211_local *local,
1423 struct ieee80211_sub_if_data *sdata)
1424{
1425 struct fq *fq = &local->fq;
1426 struct txq_info *txqi;
1427 struct fq_tin *tin;
1428 struct ieee80211_sub_if_data *ap;
1429
1430 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1431 return;
1432
1433 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1434
1435 if (!ap->vif.txq)
1436 return;
1437
1438 txqi = to_txq_info(ap->vif.txq);
1439 tin = &txqi->tin;
1440
1441 spin_lock_bh(&fq->lock);
1442 fq_tin_filter(fq, tin, fq_vlan_filter_func, &sdata->vif,
1443 fq_skb_free_func);
1444 spin_unlock_bh(&fq->lock);
1445}
1446
Michal Kaziorfa962b92016-05-19 10:37:49 +02001447void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
1448 struct sta_info *sta,
1449 struct txq_info *txqi, int tid)
1450{
1451 fq_tin_init(&txqi->tin);
1452 fq_flow_init(&txqi->def_flow);
Michal Kazior5caa3282016-05-19 10:37:51 +02001453 codel_vars_init(&txqi->def_cvars);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001454 codel_stats_init(&txqi->cstats);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001455 __skb_queue_head_init(&txqi->frags);
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001456 INIT_LIST_HEAD(&txqi->schedule_order);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001457
1458 txqi->txq.vif = &sdata->vif;
1459
Johannes Bergadf8ed02018-08-31 11:31:08 +03001460 if (!sta) {
Michal Kaziorfa962b92016-05-19 10:37:49 +02001461 sdata->vif.txq = &txqi->txq;
1462 txqi->txq.tid = 0;
1463 txqi->txq.ac = IEEE80211_AC_BE;
Johannes Bergadf8ed02018-08-31 11:31:08 +03001464
1465 return;
Michal Kazior80a83cf2016-05-19 10:37:48 +02001466 }
Johannes Bergadf8ed02018-08-31 11:31:08 +03001467
1468 if (tid == IEEE80211_NUM_TIDS) {
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001469 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1470 /* Drivers need to opt in to the management MPDU TXQ */
1471 if (!ieee80211_hw_check(&sdata->local->hw,
1472 STA_MMPDU_TXQ))
1473 return;
1474 } else if (!ieee80211_hw_check(&sdata->local->hw,
1475 BUFF_MMPDU_TXQ)) {
1476 /* Drivers need to opt in to the bufferable MMPDU TXQ */
Johannes Bergadf8ed02018-08-31 11:31:08 +03001477 return;
Sara Sharon0eeb2b62018-09-05 08:06:09 +03001478 }
Johannes Bergadf8ed02018-08-31 11:31:08 +03001479 txqi->txq.ac = IEEE80211_AC_VO;
1480 } else {
1481 txqi->txq.ac = ieee80211_ac_from_tid(tid);
1482 }
1483
1484 txqi->txq.sta = &sta->sta;
1485 txqi->txq.tid = tid;
1486 sta->sta.txq[tid] = &txqi->txq;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001487}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001488
Michal Kaziorfa962b92016-05-19 10:37:49 +02001489void ieee80211_txq_purge(struct ieee80211_local *local,
1490 struct txq_info *txqi)
1491{
1492 struct fq *fq = &local->fq;
1493 struct fq_tin *tin = &txqi->tin;
1494
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08001495 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001496 fq_tin_reset(fq, tin, fq_skb_free_func);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001497 ieee80211_purge_tx_queue(&local->hw, &txqi->frags);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08001498 spin_unlock_bh(&fq->lock);
1499
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001500 spin_lock_bh(&local->active_txq_lock[txqi->txq.ac]);
1501 list_del_init(&txqi->schedule_order);
1502 spin_unlock_bh(&local->active_txq_lock[txqi->txq.ac]);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001503}
1504
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001505void ieee80211_txq_set_params(struct ieee80211_local *local)
1506{
1507 if (local->hw.wiphy->txq_limit)
1508 local->fq.limit = local->hw.wiphy->txq_limit;
1509 else
1510 local->hw.wiphy->txq_limit = local->fq.limit;
1511
1512 if (local->hw.wiphy->txq_memory_limit)
1513 local->fq.memory_limit = local->hw.wiphy->txq_memory_limit;
1514 else
1515 local->hw.wiphy->txq_memory_limit = local->fq.memory_limit;
1516
1517 if (local->hw.wiphy->txq_quantum)
1518 local->fq.quantum = local->hw.wiphy->txq_quantum;
1519 else
1520 local->hw.wiphy->txq_quantum = local->fq.quantum;
1521}
1522
Michal Kaziorfa962b92016-05-19 10:37:49 +02001523int ieee80211_txq_setup_flows(struct ieee80211_local *local)
1524{
1525 struct fq *fq = &local->fq;
1526 int ret;
Michal Kazior5caa3282016-05-19 10:37:51 +02001527 int i;
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001528 bool supp_vht = false;
1529 enum nl80211_band band;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001530
1531 if (!local->ops->wake_tx_queue)
1532 return 0;
1533
1534 ret = fq_init(fq, 4096);
1535 if (ret)
1536 return ret;
1537
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001538 /*
1539 * If the hardware doesn't support VHT, it is safe to limit the maximum
1540 * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
1541 */
1542 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1543 struct ieee80211_supported_band *sband;
1544
1545 sband = local->hw.wiphy->bands[band];
1546 if (!sband)
1547 continue;
1548
1549 supp_vht = supp_vht || sband->vht_cap.vht_supported;
1550 }
1551
1552 if (!supp_vht)
1553 fq->memory_limit = 4 << 20; /* 4 Mbytes */
1554
Michal Kazior5caa3282016-05-19 10:37:51 +02001555 codel_params_init(&local->cparams);
Michal Kazior5caa3282016-05-19 10:37:51 +02001556 local->cparams.interval = MS2TIME(100);
1557 local->cparams.target = MS2TIME(20);
1558 local->cparams.ecn = true;
1559
1560 local->cvars = kcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
1561 GFP_KERNEL);
1562 if (!local->cvars) {
Michal Kazior59a7c822016-06-29 14:00:34 +02001563 spin_lock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001564 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001565 spin_unlock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001566 return -ENOMEM;
1567 }
1568
1569 for (i = 0; i < fq->flows_cnt; i++)
1570 codel_vars_init(&local->cvars[i]);
1571
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001572 ieee80211_txq_set_params(local);
1573
Michal Kaziorfa962b92016-05-19 10:37:49 +02001574 return 0;
1575}
1576
1577void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1578{
1579 struct fq *fq = &local->fq;
1580
1581 if (!local->ops->wake_tx_queue)
1582 return;
1583
Michal Kazior5caa3282016-05-19 10:37:51 +02001584 kfree(local->cvars);
1585 local->cvars = NULL;
1586
Michal Kazior59a7c822016-06-29 14:00:34 +02001587 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001588 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001589 spin_unlock_bh(&fq->lock);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001590}
1591
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001592static bool ieee80211_queue_skb(struct ieee80211_local *local,
1593 struct ieee80211_sub_if_data *sdata,
1594 struct sta_info *sta,
1595 struct sk_buff *skb)
1596{
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001597 struct ieee80211_vif *vif;
1598 struct txq_info *txqi;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001599
1600 if (!local->ops->wake_tx_queue ||
1601 sdata->vif.type == NL80211_IFTYPE_MONITOR)
1602 return false;
1603
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001604 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1605 sdata = container_of(sdata->bss,
1606 struct ieee80211_sub_if_data, u.ap);
1607
1608 vif = &sdata->vif;
Michal Kaziordbef5362017-01-13 13:32:51 +01001609 txqi = ieee80211_get_txq(local, vif, sta, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001610
1611 if (!txqi)
1612 return false;
1613
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001614 ieee80211_txq_enqueue(local, txqi, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001615
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08001616 schedule_and_wake_txq(local, txqi);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001617
1618 return true;
1619}
1620
Johannes Berg11127e92011-11-16 16:02:47 +01001621static bool ieee80211_tx_frags(struct ieee80211_local *local,
1622 struct ieee80211_vif *vif,
1623 struct ieee80211_sta *sta,
1624 struct sk_buff_head *skbs,
1625 bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001626{
Michal Kazior80a83cf2016-05-19 10:37:48 +02001627 struct ieee80211_tx_control control = {};
Johannes Berg252b86c2011-11-16 15:28:55 +01001628 struct sk_buff *skb, *tmp;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001629 unsigned long flags;
Johannes Berge2ebc742007-07-27 15:43:22 +02001630
Johannes Berg252b86c2011-11-16 15:28:55 +01001631 skb_queue_walk_safe(skbs, skb, tmp) {
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001632 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1633 int q = info->hw_queue;
1634
1635#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1636 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1637 __skb_unlink(skb, skbs);
Felix Fietkauc3e77242012-10-08 14:39:33 +02001638 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001639 continue;
1640 }
1641#endif
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001642
1643 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001644 if (local->queue_stop_reasons[q] ||
Johannes Berg7bb45682011-02-24 14:42:06 +01001645 (!txpending && !skb_queue_empty(&local->pending[q]))) {
Seth Forshee6c17b772013-02-11 11:21:07 -06001646 if (unlikely(info->flags &
Seth Forsheea7679ed2013-02-25 14:58:05 -06001647 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1648 if (local->queue_stop_reasons[q] &
1649 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1650 /*
1651 * Drop off-channel frames if queues
1652 * are stopped for any reason other
1653 * than off-channel operation. Never
1654 * queue them.
1655 */
1656 spin_unlock_irqrestore(
1657 &local->queue_stop_reason_lock,
1658 flags);
1659 ieee80211_purge_tx_queue(&local->hw,
1660 skbs);
1661 return true;
1662 }
1663 } else {
1664
Seth Forshee6c17b772013-02-11 11:21:07 -06001665 /*
Seth Forsheea7679ed2013-02-25 14:58:05 -06001666 * Since queue is stopped, queue up frames for
1667 * later transmission from the tx-pending
1668 * tasklet when the queue is woken again.
Seth Forshee6c17b772013-02-11 11:21:07 -06001669 */
Seth Forsheea7679ed2013-02-25 14:58:05 -06001670 if (txpending)
1671 skb_queue_splice_init(skbs,
1672 &local->pending[q]);
1673 else
1674 skb_queue_splice_tail_init(skbs,
1675 &local->pending[q]);
1676
1677 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1678 flags);
1679 return false;
Seth Forshee6c17b772013-02-11 11:21:07 -06001680 }
Johannes Berg7bb45682011-02-24 14:42:06 +01001681 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001682 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Tomas Winklerf8e79dd2008-07-24 18:46:44 +03001683
Johannes Berg11127e92011-11-16 16:02:47 +01001684 info->control.vif = vif;
Michal Kazior80a83cf2016-05-19 10:37:48 +02001685 control.sta = sta;
Johannes Bergec25acc2010-07-22 17:11:28 +02001686
Johannes Berg252b86c2011-11-16 15:28:55 +01001687 __skb_unlink(skb, skbs);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001688 drv_tx(local, &control, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001689 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001690
Johannes Berg11127e92011-11-16 16:02:47 +01001691 return true;
1692}
1693
1694/*
1695 * Returns false if the frame couldn't be transmitted but was queued instead.
1696 */
1697static bool __ieee80211_tx(struct ieee80211_local *local,
1698 struct sk_buff_head *skbs, int led_len,
1699 struct sta_info *sta, bool txpending)
1700{
1701 struct ieee80211_tx_info *info;
1702 struct ieee80211_sub_if_data *sdata;
1703 struct ieee80211_vif *vif;
1704 struct ieee80211_sta *pubsta;
1705 struct sk_buff *skb;
1706 bool result = true;
1707 __le16 fc;
1708
1709 if (WARN_ON(skb_queue_empty(skbs)))
1710 return true;
1711
1712 skb = skb_peek(skbs);
1713 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1714 info = IEEE80211_SKB_CB(skb);
1715 sdata = vif_to_sdata(info->control.vif);
1716 if (sta && !sta->uploaded)
1717 sta = NULL;
1718
1719 if (sta)
1720 pubsta = &sta->sta;
1721 else
1722 pubsta = NULL;
1723
1724 switch (sdata->vif.type) {
1725 case NL80211_IFTYPE_MONITOR:
Aviya Erenfeldd8212182016-08-29 23:25:15 +03001726 if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
Johannes Bergc82b5a72013-07-14 23:39:20 +03001727 vif = &sdata->vif;
1728 break;
1729 }
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001730 sdata = rcu_dereference(local->monitor_sdata);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001731 if (sdata) {
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001732 vif = &sdata->vif;
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001733 info->hw_queue =
1734 vif->hw_queue[skb_get_queue_mapping(skb)];
Johannes Berg30686bf2015-06-02 21:39:54 +02001735 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
Johannes Berg63b4d8b2015-11-24 15:41:50 +01001736 ieee80211_purge_tx_queue(&local->hw, skbs);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001737 return true;
1738 } else
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001739 vif = NULL;
Johannes Berg11127e92011-11-16 16:02:47 +01001740 break;
1741 case NL80211_IFTYPE_AP_VLAN:
1742 sdata = container_of(sdata->bss,
1743 struct ieee80211_sub_if_data, u.ap);
1744 /* fall through */
1745 default:
1746 vif = &sdata->vif;
1747 break;
1748 }
1749
Johannes Bergcb831b52012-07-02 15:40:18 +02001750 result = ieee80211_tx_frags(local, vif, pubsta, skbs,
1751 txpending);
Johannes Berg11127e92011-11-16 16:02:47 +01001752
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001753 ieee80211_tpt_led_trig_tx(local, fc, led_len);
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001754
Johannes Berg4db4e0a2011-11-24 14:47:36 +01001755 WARN_ON_ONCE(!skb_queue_empty(skbs));
Johannes Berg252b86c2011-11-16 15:28:55 +01001756
Johannes Berg11127e92011-11-16 16:02:47 +01001757 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001758}
1759
Johannes Berg97b045d2008-06-20 01:22:30 +02001760/*
1761 * Invoke TX handlers, return 0 on success and non-zero if the
1762 * frame was dropped or queued.
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001763 *
1764 * The handlers are split into an early and late part. The latter is everything
1765 * that can be sensitive to reordering, and will be deferred to after packets
1766 * are dequeued from the intermediate queues (when they are enabled).
Johannes Berg97b045d2008-06-20 01:22:30 +02001767 */
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001768static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx)
Johannes Berg97b045d2008-06-20 01:22:30 +02001769{
Johannes Berg97b045d2008-06-20 01:22:30 +02001770 ieee80211_tx_result res = TX_DROP;
Johannes Berg97b045d2008-06-20 01:22:30 +02001771
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001772#define CALL_TXH(txh) \
1773 do { \
1774 res = txh(tx); \
1775 if (res != TX_CONTINUE) \
1776 goto txh_done; \
1777 } while (0)
Johannes Berg97b045d2008-06-20 01:22:30 +02001778
Kalle Valo5c1b98a2010-01-12 10:42:46 +02001779 CALL_TXH(ieee80211_tx_h_dynamic_ps);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001780 CALL_TXH(ieee80211_tx_h_check_assoc);
1781 CALL_TXH(ieee80211_tx_h_ps_buf);
Johannes Berga621fa42010-08-27 14:26:54 +03001782 CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001783 CALL_TXH(ieee80211_tx_h_select_key);
Johannes Berg30686bf2015-06-02 21:39:54 +02001784 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
Johannes Bergaf65cd962009-11-17 18:18:36 +01001785 CALL_TXH(ieee80211_tx_h_rate_ctrl);
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001786
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001787 txh_done:
1788 if (unlikely(res == TX_DROP)) {
1789 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1790 if (tx->skb)
1791 ieee80211_free_txskb(&tx->local->hw, tx->skb);
1792 else
1793 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1794 return -1;
1795 } else if (unlikely(res == TX_QUEUED)) {
1796 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1797 return -1;
1798 }
1799
1800 return 0;
1801}
1802
1803/*
1804 * Late handlers can be called while the sta lock is held. Handlers that can
1805 * cause packets to be generated will cause deadlock!
1806 */
1807static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx)
1808{
1809 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1810 ieee80211_tx_result res = TX_CONTINUE;
1811
Johannes Bergaa5b5492011-12-02 22:08:52 +01001812 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1813 __skb_queue_tail(&tx->skbs, tx->skb);
1814 tx->skb = NULL;
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001815 goto txh_done;
Johannes Bergaa5b5492011-12-02 22:08:52 +01001816 }
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001817
1818 CALL_TXH(ieee80211_tx_h_michael_mic_add);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001819 CALL_TXH(ieee80211_tx_h_sequence);
1820 CALL_TXH(ieee80211_tx_h_fragment);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001821 /* handlers after fragment must be aware of tx info fragmentation! */
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001822 CALL_TXH(ieee80211_tx_h_stats);
1823 CALL_TXH(ieee80211_tx_h_encrypt);
Johannes Berg30686bf2015-06-02 21:39:54 +02001824 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
Arik Nemtsov8fd369e2011-01-31 22:29:12 +02001825 CALL_TXH(ieee80211_tx_h_calculate_duration);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001826#undef CALL_TXH
1827
1828 txh_done:
Johannes Berg97b045d2008-06-20 01:22:30 +02001829 if (unlikely(res == TX_DROP)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001830 I802_DEBUG_INC(tx->local->tx_handlers_drop);
Johannes Berg252b86c2011-11-16 15:28:55 +01001831 if (tx->skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001832 ieee80211_free_txskb(&tx->local->hw, tx->skb);
Johannes Berg252b86c2011-11-16 15:28:55 +01001833 else
Felix Fietkau1f98ab72012-11-10 03:44:14 +01001834 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
Johannes Berg97b045d2008-06-20 01:22:30 +02001835 return -1;
1836 } else if (unlikely(res == TX_QUEUED)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001837 I802_DEBUG_INC(tx->local->tx_handlers_queued);
Johannes Berg97b045d2008-06-20 01:22:30 +02001838 return -1;
1839 }
1840
1841 return 0;
1842}
1843
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001844static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1845{
1846 int r = invoke_tx_handlers_early(tx);
1847
1848 if (r)
1849 return r;
1850 return invoke_tx_handlers_late(tx);
1851}
1852
Felix Fietkau06be6b12013-10-14 18:01:00 +02001853bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1854 struct ieee80211_vif *vif, struct sk_buff *skb,
1855 int band, struct ieee80211_sta **sta)
1856{
1857 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1858 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1859 struct ieee80211_tx_data tx;
Johannes Berg88724a82015-03-01 09:10:12 +02001860 struct sk_buff *skb2;
Felix Fietkau06be6b12013-10-14 18:01:00 +02001861
Johannes Berg7c107702015-03-20 14:18:27 +01001862 if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
Felix Fietkau06be6b12013-10-14 18:01:00 +02001863 return false;
1864
1865 info->band = band;
1866 info->control.vif = vif;
1867 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1868
1869 if (invoke_tx_handlers(&tx))
1870 return false;
1871
1872 if (sta) {
1873 if (tx.sta)
1874 *sta = &tx.sta->sta;
1875 else
1876 *sta = NULL;
1877 }
1878
Johannes Berg88724a82015-03-01 09:10:12 +02001879 /* this function isn't suitable for fragmented data frames */
1880 skb2 = __skb_dequeue(&tx.skbs);
1881 if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1882 ieee80211_free_txskb(hw, skb2);
1883 ieee80211_purge_tx_queue(hw, &tx.skbs);
1884 return false;
1885 }
1886
Felix Fietkau06be6b12013-10-14 18:01:00 +02001887 return true;
1888}
1889EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1890
Johannes Berg7bb45682011-02-24 14:42:06 +01001891/*
1892 * Returns false if the frame couldn't be transmitted but was queued instead.
1893 */
1894static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01001895 struct sta_info *sta, struct sk_buff *skb,
Johannes Bergb9771d42018-05-28 15:47:41 +02001896 bool txpending, u32 txdata_flags)
Johannes Berge2ebc742007-07-27 15:43:22 +02001897{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001898 struct ieee80211_local *local = sdata->local;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001899 struct ieee80211_tx_data tx;
Johannes Berg97b045d2008-06-20 01:22:30 +02001900 ieee80211_tx_result res_prepare;
Johannes Berge039fa42008-05-15 12:55:29 +02001901 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001902 bool result = true;
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001903 int led_len;
Johannes Berge2ebc742007-07-27 15:43:22 +02001904
Johannes Berge2ebc742007-07-27 15:43:22 +02001905 if (unlikely(skb->len < 10)) {
1906 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001907 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001908 }
1909
Johannes Berg58d41852007-09-26 17:53:18 +02001910 /* initialises tx */
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001911 led_len = skb->len;
Johannes Berg7c107702015-03-20 14:18:27 +01001912 res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001913
Johannes Bergb9771d42018-05-28 15:47:41 +02001914 tx.flags |= txdata_flags;
1915
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001916 if (unlikely(res_prepare == TX_DROP)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02001917 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001918 return true;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001919 } else if (unlikely(res_prepare == TX_QUEUED)) {
Johannes Berg55de9082012-07-26 17:24:39 +02001920 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001921 }
1922
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001923 /* set up hw_queue value early */
1924 if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
Johannes Berg30686bf2015-06-02 21:39:54 +02001925 !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001926 info->hw_queue =
1927 sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1928
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001929 if (invoke_tx_handlers_early(&tx))
Bob Copeland6eae4a62018-09-05 06:22:59 -04001930 return true;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001931
1932 if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb))
1933 return true;
1934
1935 if (!invoke_tx_handlers_late(&tx))
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001936 result = __ieee80211_tx(local, &tx.skbs, led_len,
1937 tx.sta, txpending);
Johannes Berg55de9082012-07-26 17:24:39 +02001938
Johannes Berg7bb45682011-02-24 14:42:06 +01001939 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001940}
1941
1942/* device xmit handlers */
1943
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301944static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
Johannes Berg23c07522008-05-29 10:38:53 +02001945 struct sk_buff *skb,
1946 int head_need, bool may_encrypt)
1947{
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301948 struct ieee80211_local *local = sdata->local;
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001949 struct ieee80211_hdr *hdr;
1950 bool enc_tailroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001951 int tail_need = 0;
1952
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001953 hdr = (struct ieee80211_hdr *) skb->data;
1954 enc_tailroom = may_encrypt &&
1955 (sdata->crypto_tx_tailroom_needed_cnt ||
1956 ieee80211_is_mgmt(hdr->frame_control));
1957
1958 if (enc_tailroom) {
Johannes Berg23c07522008-05-29 10:38:53 +02001959 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1960 tail_need -= skb_tailroom(skb);
1961 tail_need = max_t(int, tail_need, 0);
1962 }
1963
Ido Yarivc70f59a2014-07-29 15:39:14 +03001964 if (skb_cloned(skb) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001965 (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
Felix Fietkau9d0f50b2019-01-29 11:10:57 +01001966 !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
Johannes Berg23c07522008-05-29 10:38:53 +02001967 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001968 else if (head_need || tail_need)
Johannes Berg23c07522008-05-29 10:38:53 +02001969 I802_DEBUG_INC(local->tx_expand_skb_head);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001970 else
1971 return 0;
Johannes Berg23c07522008-05-29 10:38:53 +02001972
1973 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
Joe Perches0fb9a9e2010-08-20 16:25:38 -07001974 wiphy_debug(local->hw.wiphy,
1975 "failed to reallocate TX buffer\n");
Johannes Berg23c07522008-05-29 10:38:53 +02001976 return -ENOMEM;
1977 }
1978
Johannes Berg23c07522008-05-29 10:38:53 +02001979 return 0;
1980}
1981
Johannes Berg7c107702015-03-20 14:18:27 +01001982void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
Johannes Bergb9771d42018-05-28 15:47:41 +02001983 struct sta_info *sta, struct sk_buff *skb,
1984 u32 txdata_flags)
Johannes Berge2ebc742007-07-27 15:43:22 +02001985{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001986 struct ieee80211_local *local = sdata->local;
Johannes Berge039fa42008-05-15 12:55:29 +02001987 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Colin Ian King11b05ba2018-02-02 16:31:42 +00001988 struct ieee80211_hdr *hdr;
Johannes Berge2ebc742007-07-27 15:43:22 +02001989 int headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001990 bool may_encrypt;
Johannes Berge2ebc742007-07-27 15:43:22 +02001991
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001992 may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
Johannes Berg23c07522008-05-29 10:38:53 +02001993
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001994 headroom = local->tx_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001995 if (may_encrypt)
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001996 headroom += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001997 headroom -= skb_headroom(skb);
1998 headroom = max_t(int, 0, headroom);
1999
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05302000 if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02002001 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002002 return;
Johannes Berge2ebc742007-07-27 15:43:22 +02002003 }
2004
Steve deRosier740c1aa2010-09-11 20:01:31 -07002005 hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berg5061b0c2009-07-14 00:33:34 +02002006 info->control.vif = &sdata->vif;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01002007
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002008 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2009 if (ieee80211_is_data(hdr->frame_control) &&
2010 is_unicast_ether_addr(hdr->addr1)) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002011 if (mesh_nexthop_resolve(sdata, skb))
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002012 return; /* skb queued: don't free */
2013 } else {
2014 ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
2015 }
Johannes Berg98aed9f2012-03-27 14:18:36 +02002016 }
Javier Cardonacca89492009-08-10 17:29:29 -07002017
Javier Cardona2154c81c2011-09-07 17:49:53 -07002018 ieee80211_set_qos_hdr(sdata, skb);
Johannes Bergb9771d42018-05-28 15:47:41 +02002019 ieee80211_tx(sdata, sta, skb, false, txdata_flags);
Johannes Berge2ebc742007-07-27 15:43:22 +02002020}
2021
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002022static bool ieee80211_parse_tx_radiotap(struct ieee80211_local *local,
2023 struct sk_buff *skb)
Johannes Berg73b9f032011-10-07 14:01:26 +02002024{
2025 struct ieee80211_radiotap_iterator iterator;
2026 struct ieee80211_radiotap_header *rthdr =
2027 (struct ieee80211_radiotap_header *) skb->data;
2028 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002029 struct ieee80211_supported_band *sband =
2030 local->hw.wiphy->bands[info->band];
Johannes Berg73b9f032011-10-07 14:01:26 +02002031 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
2032 NULL);
2033 u16 txflags;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002034 u16 rate = 0;
2035 bool rate_found = false;
2036 u8 rate_retries = 0;
2037 u16 rate_flags = 0;
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002038 u8 mcs_known, mcs_flags, mcs_bw;
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002039 u16 vht_known;
2040 u8 vht_mcs = 0, vht_nss = 0;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002041 int i;
Johannes Berg73b9f032011-10-07 14:01:26 +02002042
2043 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
2044 IEEE80211_TX_CTL_DONTFRAG;
2045
2046 /*
2047 * for every radiotap entry that is present
2048 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
2049 * entries present, or -EINVAL on error)
2050 */
2051
2052 while (!ret) {
2053 ret = ieee80211_radiotap_iterator_next(&iterator);
2054
2055 if (ret)
2056 continue;
2057
2058 /* see if this argument is something we can use */
2059 switch (iterator.this_arg_index) {
2060 /*
2061 * You must take care when dereferencing iterator.this_arg
2062 * for multibyte types... the pointer is not aligned. Use
2063 * get_unaligned((type *)iterator.this_arg) to dereference
2064 * iterator.this_arg for type "type" safely on all arches.
2065 */
2066 case IEEE80211_RADIOTAP_FLAGS:
2067 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
2068 /*
2069 * this indicates that the skb we have been
2070 * handed has the 32-bit FCS CRC at the end...
2071 * we should react to that by snipping it off
2072 * because it will be recomputed and added
2073 * on transmission
2074 */
2075 if (skb->len < (iterator._max_length + FCS_LEN))
2076 return false;
2077
2078 skb_trim(skb, skb->len - FCS_LEN);
2079 }
2080 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
2081 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
2082 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
2083 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
2084 break;
2085
2086 case IEEE80211_RADIOTAP_TX_FLAGS:
2087 txflags = get_unaligned_le16(iterator.this_arg);
2088 if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
2089 info->flags |= IEEE80211_TX_CTL_NO_ACK;
2090 break;
2091
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002092 case IEEE80211_RADIOTAP_RATE:
2093 rate = *iterator.this_arg;
2094 rate_flags = 0;
2095 rate_found = true;
2096 break;
2097
2098 case IEEE80211_RADIOTAP_DATA_RETRIES:
2099 rate_retries = *iterator.this_arg;
2100 break;
2101
2102 case IEEE80211_RADIOTAP_MCS:
2103 mcs_known = iterator.this_arg[0];
2104 mcs_flags = iterator.this_arg[1];
2105 if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
2106 break;
2107
2108 rate_found = true;
2109 rate = iterator.this_arg[2];
2110 rate_flags = IEEE80211_TX_RC_MCS;
2111
2112 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
2113 mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
2114 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2115
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002116 mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002117 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002118 mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002119 rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2120 break;
2121
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002122 case IEEE80211_RADIOTAP_VHT:
2123 vht_known = get_unaligned_le16(iterator.this_arg);
2124 rate_found = true;
2125
2126 rate_flags = IEEE80211_TX_RC_VHT_MCS;
2127 if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
2128 (iterator.this_arg[2] &
2129 IEEE80211_RADIOTAP_VHT_FLAG_SGI))
2130 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2131 if (vht_known &
2132 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
2133 if (iterator.this_arg[3] == 1)
2134 rate_flags |=
2135 IEEE80211_TX_RC_40_MHZ_WIDTH;
2136 else if (iterator.this_arg[3] == 4)
2137 rate_flags |=
2138 IEEE80211_TX_RC_80_MHZ_WIDTH;
2139 else if (iterator.this_arg[3] == 11)
2140 rate_flags |=
2141 IEEE80211_TX_RC_160_MHZ_WIDTH;
2142 }
2143
2144 vht_mcs = iterator.this_arg[4] >> 4;
2145 vht_nss = iterator.this_arg[4] & 0xF;
2146 break;
2147
Johannes Berg73b9f032011-10-07 14:01:26 +02002148 /*
2149 * Please update the file
2150 * Documentation/networking/mac80211-injection.txt
2151 * when parsing new fields here.
2152 */
2153
2154 default:
2155 break;
2156 }
2157 }
2158
2159 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
2160 return false;
2161
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002162 if (rate_found) {
2163 info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
2164
2165 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2166 info->control.rates[i].idx = -1;
2167 info->control.rates[i].flags = 0;
2168 info->control.rates[i].count = 0;
2169 }
2170
2171 if (rate_flags & IEEE80211_TX_RC_MCS) {
2172 info->control.rates[0].idx = rate;
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002173 } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
2174 ieee80211_rate_set_vht(info->control.rates, vht_mcs,
2175 vht_nss);
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002176 } else {
2177 for (i = 0; i < sband->n_bitrates; i++) {
2178 if (rate * 5 != sband->bitrates[i].bitrate)
2179 continue;
2180
2181 info->control.rates[0].idx = i;
2182 break;
2183 }
2184 }
2185
Felix Fietkau07310a62016-03-02 15:54:51 +01002186 if (info->control.rates[0].idx < 0)
2187 info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
2188
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002189 info->control.rates[0].flags = rate_flags;
2190 info->control.rates[0].count = min_t(u8, rate_retries + 1,
2191 local->hw.max_rate_tries);
2192 }
2193
Johannes Berg73b9f032011-10-07 14:01:26 +02002194 /*
2195 * remove the radiotap header
2196 * iterator->_max_length was sanity-checked against
2197 * skb->len by iterator init
2198 */
2199 skb_pull(skb, iterator._max_length);
2200
2201 return true;
2202}
2203
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +00002204netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
2205 struct net_device *dev)
Johannes Berge2ebc742007-07-27 15:43:22 +02002206{
2207 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg55de9082012-07-26 17:24:39 +02002208 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berge2ebc742007-07-27 15:43:22 +02002209 struct ieee80211_radiotap_header *prthdr =
2210 (struct ieee80211_radiotap_header *)skb->data;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002211 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002212 struct ieee80211_hdr *hdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002213 struct ieee80211_sub_if_data *tmp_sdata, *sdata;
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002214 struct cfg80211_chan_def *chandef;
Andy Green9b8a74e2007-07-27 15:43:24 +02002215 u16 len_rthdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002216 int hdrlen;
Johannes Berge2ebc742007-07-27 15:43:22 +02002217
Andy Green9b8a74e2007-07-27 15:43:24 +02002218 /* check for not even having the fixed radiotap header part */
2219 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2220 goto fail; /* too short to be possibly valid */
2221
2222 /* is it a header version we can trust to find length from? */
2223 if (unlikely(prthdr->it_version))
2224 goto fail; /* only version 0 is supported */
2225
2226 /* then there must be a radiotap header with a length we can use */
2227 len_rthdr = ieee80211_get_radiotap_len(skb->data);
2228
2229 /* does the skb contain enough to deliver on the alleged length? */
2230 if (unlikely(skb->len < len_rthdr))
2231 goto fail; /* skb too short for claimed rt header extent */
Johannes Berge2ebc742007-07-27 15:43:22 +02002232
Johannes Berge2ebc742007-07-27 15:43:22 +02002233 /*
2234 * fix up the pointers accounting for the radiotap
2235 * header still being in there. We are being given
2236 * a precooked IEEE80211 header so no need for
2237 * normal processing
2238 */
Andy Green9b8a74e2007-07-27 15:43:24 +02002239 skb_set_mac_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02002240 /*
Andy Green9b8a74e2007-07-27 15:43:24 +02002241 * these are just fixed to the end of the rt area since we
2242 * don't have any better information and at this point, nobody cares
Johannes Berge2ebc742007-07-27 15:43:22 +02002243 */
Andy Green9b8a74e2007-07-27 15:43:24 +02002244 skb_set_network_header(skb, len_rthdr);
2245 skb_set_transport_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02002246
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002247 if (skb->len < len_rthdr + 2)
2248 goto fail;
2249
2250 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2251 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2252
2253 if (skb->len < len_rthdr + hdrlen)
2254 goto fail;
2255
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002256 /*
2257 * Initialize skb->protocol if the injected frame is a data frame
2258 * carrying a rfc1042 header
2259 */
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002260 if (ieee80211_is_data(hdr->frame_control) &&
2261 skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2262 u8 *payload = (u8 *)hdr + hdrlen;
2263
Joe Perchesb203ca32012-05-08 18:56:52 +00002264 if (ether_addr_equal(payload, rfc1042_header))
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002265 skb->protocol = cpu_to_be16((payload[6] << 8) |
2266 payload[7]);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002267 }
2268
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002269 memset(info, 0, sizeof(*info));
2270
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002271 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
Johannes Berg73b9f032011-10-07 14:01:26 +02002272 IEEE80211_TX_CTL_INJECTED;
2273
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002274 rcu_read_lock();
2275
2276 /*
2277 * We process outgoing injected frames that have a local address
2278 * we handle as though they are non-injected frames.
2279 * This code here isn't entirely correct, the local MAC address
2280 * isn't always enough to find the interface to use; for proper
2281 * VLAN/WDS support we will need a different mechanism (which
2282 * likely isn't going to be monitor interfaces).
2283 */
2284 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2285
2286 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2287 if (!ieee80211_sdata_running(tmp_sdata))
2288 continue;
2289 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2290 tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2291 tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
2292 continue;
Joe Perchesb203ca32012-05-08 18:56:52 +00002293 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002294 sdata = tmp_sdata;
2295 break;
2296 }
2297 }
Johannes Berg7351c6b2009-11-19 01:08:30 +01002298
Johannes Berg55de9082012-07-26 17:24:39 +02002299 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2300 if (!chanctx_conf) {
2301 tmp_sdata = rcu_dereference(local->monitor_sdata);
2302 if (tmp_sdata)
2303 chanctx_conf =
2304 rcu_dereference(tmp_sdata->vif.chanctx_conf);
2305 }
Johannes Berg55de9082012-07-26 17:24:39 +02002306
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002307 if (chanctx_conf)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002308 chandef = &chanctx_conf->def;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002309 else if (!local->use_chanctx)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002310 chandef = &local->_oper_chandef;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002311 else
2312 goto fail_rcu;
Johannes Berg55de9082012-07-26 17:24:39 +02002313
2314 /*
2315 * Frame injection is not allowed if beaconing is not allowed
2316 * or if we need radar detection. Beaconing is usually not allowed when
2317 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2318 * Passive scan is also used in world regulatory domains where
2319 * your country is not known and as such it should be treated as
2320 * NO TX unless the channel is explicitly allowed in which case
2321 * your current regulatory domain would not have the passive scan
2322 * flag.
2323 *
2324 * Since AP mode uses monitor interfaces to inject/TX management
2325 * frames we can make AP mode the exception to this rule once it
2326 * supports radar detection as its implementation can deal with
2327 * radar detection by itself. We can do that later by adding a
2328 * monitor flag interfaces used for AP support.
2329 */
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002330 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2331 sdata->vif.type))
Johannes Berg55de9082012-07-26 17:24:39 +02002332 goto fail_rcu;
2333
Johannes Berg73c4e192014-11-09 18:50:09 +02002334 info->band = chandef->chan->band;
Lorenzo Bianconi109843b02016-02-19 12:18:01 +01002335
2336 /* process and remove the injection radiotap header */
2337 if (!ieee80211_parse_tx_radiotap(local, skb))
2338 goto fail_rcu;
2339
Johannes Bergb9771d42018-05-28 15:47:41 +02002340 ieee80211_xmit(sdata, NULL, skb, 0);
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002341 rcu_read_unlock();
2342
Johannes Berge2ebc742007-07-27 15:43:22 +02002343 return NETDEV_TX_OK;
Andy Green9b8a74e2007-07-27 15:43:24 +02002344
Johannes Berg55de9082012-07-26 17:24:39 +02002345fail_rcu:
2346 rcu_read_unlock();
Andy Green9b8a74e2007-07-27 15:43:24 +02002347fail:
2348 dev_kfree_skb(skb);
2349 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
Johannes Berge2ebc742007-07-27 15:43:22 +02002350}
2351
Johannes Berg97ffe752015-03-21 09:13:45 +01002352static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002353{
Johannes Berg97ffe752015-03-21 09:13:45 +01002354 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002355
Johannes Berg97ffe752015-03-21 09:13:45 +01002356 return ethertype == ETH_P_TDLS &&
2357 skb->len > 14 &&
2358 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2359}
2360
2361static int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2362 struct sk_buff *skb,
2363 struct sta_info **sta_out)
2364{
2365 struct sta_info *sta;
2366
2367 switch (sdata->vif.type) {
2368 case NL80211_IFTYPE_AP_VLAN:
2369 sta = rcu_dereference(sdata->u.vlan.sta);
2370 if (sta) {
2371 *sta_out = sta;
2372 return 0;
2373 } else if (sdata->wdev.use_4addr) {
2374 return -ENOLINK;
2375 }
2376 /* fall through */
2377 case NL80211_IFTYPE_AP:
2378 case NL80211_IFTYPE_OCB:
2379 case NL80211_IFTYPE_ADHOC:
2380 if (is_multicast_ether_addr(skb->data)) {
2381 *sta_out = ERR_PTR(-ENOENT);
2382 return 0;
2383 }
2384 sta = sta_info_get_bss(sdata, skb->data);
2385 break;
2386 case NL80211_IFTYPE_WDS:
2387 sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
2388 break;
2389#ifdef CONFIG_MAC80211_MESH
2390 case NL80211_IFTYPE_MESH_POINT:
2391 /* determined much later */
2392 *sta_out = NULL;
2393 return 0;
2394#endif
2395 case NL80211_IFTYPE_STATION:
2396 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2397 sta = sta_info_get(sdata, skb->data);
Johannes Berg11d62ca2016-09-13 08:28:22 +02002398 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2399 if (test_sta_flag(sta,
2400 WLAN_STA_TDLS_PEER_AUTH)) {
Johannes Berg97ffe752015-03-21 09:13:45 +01002401 *sta_out = sta;
2402 return 0;
2403 }
2404
2405 /*
2406 * TDLS link during setup - throw out frames to
2407 * peer. Allow TDLS-setup frames to unauthorized
2408 * peers for the special case of a link teardown
2409 * after a TDLS sta is removed due to being
2410 * unreachable.
2411 */
Johannes Berg11d62ca2016-09-13 08:28:22 +02002412 if (!ieee80211_is_tdls_setup(skb))
Johannes Berg97ffe752015-03-21 09:13:45 +01002413 return -EINVAL;
2414 }
2415
2416 }
2417
2418 sta = sta_info_get(sdata, sdata->u.mgd.bssid);
2419 if (!sta)
2420 return -ENOLINK;
2421 break;
2422 default:
2423 return -EINVAL;
2424 }
2425
2426 *sta_out = sta ?: ERR_PTR(-ENOENT);
2427 return 0;
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002428}
2429
Johannes Berge2ebc742007-07-27 15:43:22 +02002430/**
Johannes Berg4c9451e2014-11-09 18:50:10 +02002431 * ieee80211_build_hdr - build 802.11 header in the given frame
2432 * @sdata: virtual interface to build the header for
2433 * @skb: the skb to build the header in
Liad Kaufman24d342c2014-11-09 18:50:07 +02002434 * @info_flags: skb flags to set
Johannes Berge2ebc742007-07-27 15:43:22 +02002435 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002436 * This function takes the skb with 802.3 header and reformats the header to
2437 * the appropriate IEEE 802.11 header based on which interface the packet is
2438 * being transmitted on.
Johannes Berge2ebc742007-07-27 15:43:22 +02002439 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002440 * Note that this function also takes care of the TX status request and
2441 * potential unsharing of the SKB - this needs to be interleaved with the
2442 * header building.
2443 *
2444 * The function requires the read-side RCU lock held
2445 *
2446 * Returns: the (possibly reallocated) skb or an ERR_PTR() code
Johannes Berge2ebc742007-07-27 15:43:22 +02002447 */
Johannes Berg4c9451e2014-11-09 18:50:10 +02002448static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01002449 struct sk_buff *skb, u32 info_flags,
Johannes Berg97ffe752015-03-21 09:13:45 +01002450 struct sta_info *sta)
Johannes Berge2ebc742007-07-27 15:43:22 +02002451{
Johannes Berg133b8222008-09-16 14:18:59 +02002452 struct ieee80211_local *local = sdata->local;
Felix Fietkau489ee912010-12-18 19:30:48 +01002453 struct ieee80211_tx_info *info;
Johannes Bergdcf33962012-07-30 15:11:56 +02002454 int head_need;
Harvey Harrison065e96052008-06-22 16:45:27 -07002455 u16 ethertype, hdrlen, meshhdrlen = 0;
2456 __le16 fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002457 struct ieee80211_hdr hdr;
Wey-Yi Guyff67bb82010-08-21 07:23:29 -07002458 struct ieee80211s_hdr mesh_hdr __maybe_unused;
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002459 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02002460 const u8 *encaps_data;
2461 int encaps_len, skip_header_bytes;
Johannes Berg97ffe752015-03-21 09:13:45 +01002462 bool wme_sta = false, authorized = false;
2463 bool tdls_peer;
Johannes Berga729cff2011-11-06 14:13:34 +01002464 bool multicast;
Johannes Berga729cff2011-11-06 14:13:34 +01002465 u16 info_id = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002466 struct ieee80211_chanctx_conf *chanctx_conf;
2467 struct ieee80211_sub_if_data *ap_sdata;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002468 enum nl80211_band band;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002469 int ret;
Johannes Berge2ebc742007-07-27 15:43:22 +02002470
Johannes Berg97ffe752015-03-21 09:13:45 +01002471 if (IS_ERR(sta))
2472 sta = NULL;
2473
Johannes Berge2ebc742007-07-27 15:43:22 +02002474 /* convert Ethernet header to proper 802.11 header (based on
2475 * operation mode) */
2476 ethertype = (skb->data[12] << 8) | skb->data[13];
Harvey Harrison065e96052008-06-22 16:45:27 -07002477 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
Johannes Berge2ebc742007-07-27 15:43:22 +02002478
Johannes Berg51fb61e2007-12-19 01:31:27 +01002479 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002480 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg97ffe752015-03-21 09:13:45 +01002481 if (sdata->wdev.use_4addr) {
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002482 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2483 /* RA TA DA SA */
2484 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002485 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002486 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2487 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2488 hdrlen = 30;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002489 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Johannes Berga74a8c82014-07-22 14:50:47 +02002490 wme_sta = sta->sta.wme;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002491 }
Johannes Berg55de9082012-07-26 17:24:39 +02002492 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2493 u.ap);
2494 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002495 if (!chanctx_conf) {
2496 ret = -ENOTCONN;
2497 goto free;
2498 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002499 band = chanctx_conf->def.chan->band;
Johannes Berg97ffe752015-03-21 09:13:45 +01002500 if (sdata->wdev.use_4addr)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002501 break;
2502 /* fall through */
2503 case NL80211_IFTYPE_AP:
Arnd Bergmannfe801232013-01-25 14:14:33 +00002504 if (sdata->vif.type == NL80211_IFTYPE_AP)
2505 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002506 if (!chanctx_conf) {
2507 ret = -ENOTCONN;
2508 goto free;
2509 }
Harvey Harrison065e96052008-06-22 16:45:27 -07002510 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
Johannes Berge2ebc742007-07-27 15:43:22 +02002511 /* DA BSSID SA */
2512 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002513 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002514 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2515 hdrlen = 24;
Johannes Berg4bf88532012-11-09 11:39:59 +01002516 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002517 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002518 case NL80211_IFTYPE_WDS:
Harvey Harrison065e96052008-06-22 16:45:27 -07002519 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
Johannes Berge2ebc742007-07-27 15:43:22 +02002520 /* RA TA DA SA */
2521 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002522 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002523 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2524 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2525 hdrlen = 30;
Johannes Berg55de9082012-07-26 17:24:39 +02002526 /*
2527 * This is the exception! WDS style interfaces are prohibited
2528 * when channel contexts are in used so this must be valid
2529 */
Karl Beldan675a0b02013-03-25 16:26:57 +01002530 band = local->hw.conf.chandef.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002531 break;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002532#ifdef CONFIG_MAC80211_MESH
Johannes Berg05c914f2008-09-11 00:01:58 +02002533 case NL80211_IFTYPE_MESH_POINT:
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002534 if (!is_multicast_ether_addr(skb->data)) {
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002535 struct sta_info *next_hop;
2536 bool mpp_lookup = true;
2537
Johannes Bergbf7cd942013-02-15 14:40:31 +01002538 mpath = mesh_path_lookup(sdata, skb->data);
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002539 if (mpath) {
2540 mpp_lookup = false;
2541 next_hop = rcu_dereference(mpath->next_hop);
2542 if (!next_hop ||
2543 !(mpath->flags & (MESH_PATH_ACTIVE |
2544 MESH_PATH_RESOLVING)))
2545 mpp_lookup = true;
2546 }
2547
Henning Roggeab1c7902016-02-03 13:58:37 +01002548 if (mpp_lookup) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002549 mppath = mpp_path_lookup(sdata, skb->data);
Henning Roggeab1c7902016-02-03 13:58:37 +01002550 if (mppath)
2551 mppath->exp_time = jiffies;
2552 }
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002553
2554 if (mppath && mpath)
Bob Copeland2bdaf382016-02-28 20:03:56 -05002555 mesh_path_del(sdata, mpath->dst);
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002556 }
YanBo79617de2008-09-22 13:30:32 +08002557
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002558 /*
Joel A Fernandes9d525012011-01-10 00:44:23 -06002559 * Use address extension if it is a packet from
2560 * another interface or if we know the destination
2561 * is being proxied by a portal (i.e. portal address
2562 * differs from proxied address)
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002563 */
Joe Perchesb203ca32012-05-08 18:56:52 +00002564 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2565 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
Javier Cardona3c5772a2009-08-10 12:15:48 -07002566 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2567 skb->data, skb->data + ETH_ALEN);
Johannes Bergbf7cd942013-02-15 14:40:31 +01002568 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2569 NULL, NULL);
YanBo79617de2008-09-22 13:30:32 +08002570 } else {
Thomas Pedersen27f01122012-08-20 11:28:25 -07002571 /* DS -> MBSS (802.11-2012 13.11.3.3).
2572 * For unicast with unknown forwarding information,
2573 * destination might be in the MBSS or if that fails
2574 * forwarded to another mesh gate. In either case
2575 * resolution will be handled in ieee80211_xmit(), so
2576 * leave the original DA. This also works for mcast */
2577 const u8 *mesh_da = skb->data;
YanBo79617de2008-09-22 13:30:32 +08002578
Thomas Pedersen27f01122012-08-20 11:28:25 -07002579 if (mppath)
2580 mesh_da = mppath->mpp;
2581 else if (mpath)
2582 mesh_da = mpath->dst;
Thomas Pedersen27f01122012-08-20 11:28:25 -07002583
Javier Cardona3c5772a2009-08-10 12:15:48 -07002584 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
Johannes Berg47846c92009-11-25 17:46:19 +01002585 mesh_da, sdata->vif.addr);
Thomas Pedersen27f01122012-08-20 11:28:25 -07002586 if (is_multicast_ether_addr(mesh_da))
2587 /* DA TA mSA AE:SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002588 meshhdrlen = ieee80211_new_mesh_header(
2589 sdata, &mesh_hdr,
2590 skb->data + ETH_ALEN, NULL);
Javier Cardona3c5772a2009-08-10 12:15:48 -07002591 else
Thomas Pedersen27f01122012-08-20 11:28:25 -07002592 /* RA TA mDA mSA AE:DA SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002593 meshhdrlen = ieee80211_new_mesh_header(
2594 sdata, &mesh_hdr, skb->data,
2595 skb->data + ETH_ALEN);
YanBo79617de2008-09-22 13:30:32 +08002596
YanBo79617de2008-09-22 13:30:32 +08002597 }
Johannes Berg55de9082012-07-26 17:24:39 +02002598 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002599 if (!chanctx_conf) {
2600 ret = -ENOTCONN;
2601 goto free;
2602 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002603 band = chanctx_conf->def.chan->band;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002604 break;
2605#endif
Johannes Berg05c914f2008-09-11 00:01:58 +02002606 case NL80211_IFTYPE_STATION:
Johannes Berg97ffe752015-03-21 09:13:45 +01002607 /* we already did checks when looking up the RA STA */
2608 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002609
Johannes Berg97ffe752015-03-21 09:13:45 +01002610 if (tdls_peer) {
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002611 /* DA SA BSSID */
2612 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2613 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2614 memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2615 hdrlen = 24;
2616 } else if (sdata->u.mgd.use_4addr &&
2617 cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2618 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2619 IEEE80211_FCTL_TODS);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002620 /* RA TA DA SA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002621 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002622 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002623 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2624 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2625 hdrlen = 30;
2626 } else {
2627 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2628 /* BSSID SA DA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002629 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002630 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2631 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2632 hdrlen = 24;
2633 }
Johannes Berg55de9082012-07-26 17:24:39 +02002634 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002635 if (!chanctx_conf) {
2636 ret = -ENOTCONN;
2637 goto free;
2638 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002639 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002640 break;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002641 case NL80211_IFTYPE_OCB:
2642 /* DA SA BSSID */
2643 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2644 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2645 eth_broadcast_addr(hdr.addr3);
2646 hdrlen = 24;
2647 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002648 if (!chanctx_conf) {
2649 ret = -ENOTCONN;
2650 goto free;
2651 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002652 band = chanctx_conf->def.chan->band;
2653 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002654 case NL80211_IFTYPE_ADHOC:
Johannes Berge2ebc742007-07-27 15:43:22 +02002655 /* DA SA BSSID */
2656 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2657 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01002658 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002659 hdrlen = 24;
Johannes Berg55de9082012-07-26 17:24:39 +02002660 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002661 if (!chanctx_conf) {
2662 ret = -ENOTCONN;
2663 goto free;
2664 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002665 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002666 break;
2667 default:
Johannes Berg4c9451e2014-11-09 18:50:10 +02002668 ret = -EINVAL;
2669 goto free;
Johannes Berge2ebc742007-07-27 15:43:22 +02002670 }
2671
Johannes Berga729cff2011-11-06 14:13:34 +01002672 multicast = is_multicast_ether_addr(hdr.addr1);
Johannes Berge2ebc742007-07-27 15:43:22 +02002673
Johannes Berg97ffe752015-03-21 09:13:45 +01002674 /* sta is always NULL for mesh */
2675 if (sta) {
2676 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2677 wme_sta = sta->sta.wme;
2678 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2679 /* For mesh, the use of the QoS header is mandatory */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002680 wme_sta = true;
Johannes Berge2ebc742007-07-27 15:43:22 +02002681 }
2682
Johannes Berg527871d2015-03-21 08:09:55 +01002683 /* receiver does QoS (which also means we do) use it */
2684 if (wme_sta) {
Harvey Harrison065e96052008-06-22 16:45:27 -07002685 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002686 hdrlen += 2;
2687 }
2688
2689 /*
Johannes Berg238814f2008-01-28 17:19:37 +01002690 * Drop unicast frames to unauthorised stations unless they are
2691 * EAPOL frames from the local station.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002692 */
Johannes Berg55182e42011-10-12 17:28:21 +02002693 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002694 (sdata->vif.type != NL80211_IFTYPE_OCB) &&
Sergey Ryazanova6ececf2013-08-30 01:35:09 +04002695 !multicast && !authorized &&
Johannes Berg55182e42011-10-12 17:28:21 +02002696 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002697 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002698#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002699 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
Johannes Berg4c9451e2014-11-09 18:50:10 +02002700 sdata->name, hdr.addr1);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002701#endif
2702
2703 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2704
Johannes Berg4c9451e2014-11-09 18:50:10 +02002705 ret = -EPERM;
2706 goto free;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002707 }
2708
Johannes Berga729cff2011-11-06 14:13:34 +01002709 if (unlikely(!multicast && skb->sk &&
2710 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)) {
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002711 struct sk_buff *ack_skb = skb_clone_sk(skb);
Johannes Berga729cff2011-11-06 14:13:34 +01002712
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002713 if (ack_skb) {
Johannes Berga729cff2011-11-06 14:13:34 +01002714 unsigned long flags;
Tejun Heo9475af62013-02-27 17:04:59 -08002715 int id;
Johannes Berga729cff2011-11-06 14:13:34 +01002716
2717 spin_lock_irqsave(&local->ack_status_lock, flags);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002718 id = idr_alloc(&local->ack_status_frames, ack_skb,
Tejun Heo9475af62013-02-27 17:04:59 -08002719 1, 0x10000, GFP_ATOMIC);
Johannes Berga729cff2011-11-06 14:13:34 +01002720 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2721
Tejun Heo9475af62013-02-27 17:04:59 -08002722 if (id >= 0) {
Johannes Berga729cff2011-11-06 14:13:34 +01002723 info_id = id;
2724 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Berga729cff2011-11-06 14:13:34 +01002725 } else {
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002726 kfree_skb(ack_skb);
Johannes Berga729cff2011-11-06 14:13:34 +01002727 }
Johannes Berga729cff2011-11-06 14:13:34 +01002728 }
2729 }
2730
Helmut Schaa7e244702010-12-02 18:44:09 +01002731 /*
2732 * If the skb is shared we need to obtain our own copy.
2733 */
2734 if (skb_shared(skb)) {
Johannes Berga729cff2011-11-06 14:13:34 +01002735 struct sk_buff *tmp_skb = skb;
2736
2737 /* can't happen -- skb is a clone if info_id != 0 */
2738 WARN_ON(info_id);
2739
Felix Fietkauf8a0a782010-12-18 19:30:50 +01002740 skb = skb_clone(skb, GFP_ATOMIC);
Helmut Schaa7e244702010-12-02 18:44:09 +01002741 kfree_skb(tmp_skb);
2742
Johannes Berg4c9451e2014-11-09 18:50:10 +02002743 if (!skb) {
2744 ret = -ENOMEM;
2745 goto free;
2746 }
Helmut Schaa7e244702010-12-02 18:44:09 +01002747 }
2748
Harvey Harrison065e96052008-06-22 16:45:27 -07002749 hdr.frame_control = fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002750 hdr.duration_id = 0;
2751 hdr.seq_ctrl = 0;
2752
2753 skip_header_bytes = ETH_HLEN;
2754 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2755 encaps_data = bridge_tunnel_header;
2756 encaps_len = sizeof(bridge_tunnel_header);
2757 skip_header_bytes -= 2;
Simon Hormane5c5d222013-03-28 13:38:25 +09002758 } else if (ethertype >= ETH_P_802_3_MIN) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002759 encaps_data = rfc1042_header;
2760 encaps_len = sizeof(rfc1042_header);
2761 skip_header_bytes -= 2;
2762 } else {
2763 encaps_data = NULL;
2764 encaps_len = 0;
2765 }
2766
2767 skb_pull(skb, skip_header_bytes);
Johannes Berg23c07522008-05-29 10:38:53 +02002768 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002769
Johannes Berg23c07522008-05-29 10:38:53 +02002770 /*
2771 * So we need to modify the skb header and hence need a copy of
2772 * that. The head_need variable above doesn't, so far, include
2773 * the needed header space that we don't need right away. If we
2774 * can, then we don't reallocate right now but only after the
2775 * frame arrives at the master device (if it does...)
2776 *
2777 * If we cannot, however, then we will reallocate to include all
2778 * the ever needed space. Also, if we need to reallocate it anyway,
2779 * make it big enough for everything we may ever need.
2780 */
Johannes Berge2ebc742007-07-27 15:43:22 +02002781
David S. Miller3a5be7d2008-06-18 01:19:51 -07002782 if (head_need > 0 || skb_cloned(skb)) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002783 head_need += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02002784 head_need += local->tx_headroom;
2785 head_need = max_t(int, 0, head_need);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002786 if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
2787 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg1c963be2012-11-07 14:02:30 +01002788 skb = NULL;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002789 return ERR_PTR(-ENOMEM);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002790 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002791 }
2792
Felix Fietkaueae44302016-07-13 11:00:02 +02002793 if (encaps_data)
Johannes Berge2ebc742007-07-27 15:43:22 +02002794 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002795
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002796#ifdef CONFIG_MAC80211_MESH
Felix Fietkaueae44302016-07-13 11:00:02 +02002797 if (meshhdrlen > 0)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002798 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002799#endif
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002800
Harvey Harrison065e96052008-06-22 16:45:27 -07002801 if (ieee80211_is_data_qos(fc)) {
Johannes Bergc29b9b92007-09-14 11:10:24 -04002802 __le16 *qos_control;
2803
Johannes Bergd58ff352017-06-16 14:29:23 +02002804 qos_control = skb_push(skb, 2);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002805 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2806 /*
2807 * Maybe we could actually set some fields here, for now just
2808 * initialise to zero to indicate no special operation.
2809 */
2810 *qos_control = 0;
2811 } else
2812 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2813
Zhang Shengjud57a5442016-03-03 01:16:56 +00002814 skb_reset_mac_header(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002815
Felix Fietkau489ee912010-12-18 19:30:48 +01002816 info = IEEE80211_SKB_CB(skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002817 memset(info, 0, sizeof(*info));
2818
Johannes Berga729cff2011-11-06 14:13:34 +01002819 info->flags = info_flags;
2820 info->ack_frame_id = info_id;
Johannes Berg73c4e192014-11-09 18:50:09 +02002821 info->band = band;
Johannes Berga729cff2011-11-06 14:13:34 +01002822
Johannes Berg4c9451e2014-11-09 18:50:10 +02002823 return skb;
2824 free:
2825 kfree_skb(skb);
2826 return ERR_PTR(ret);
Johannes Berge2ebc742007-07-27 15:43:22 +02002827}
2828
Johannes Berg17c18bf2015-03-21 15:25:43 +01002829/*
2830 * fast-xmit overview
2831 *
2832 * The core idea of this fast-xmit is to remove per-packet checks by checking
2833 * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2834 * checks that are needed to get the sta->fast_tx pointer assigned, after which
2835 * much less work can be done per packet. For example, fragmentation must be
2836 * disabled or the fast_tx pointer will not be set. All the conditions are seen
2837 * in the code here.
2838 *
2839 * Once assigned, the fast_tx data structure also caches the per-packet 802.11
2840 * header and other data to aid packet processing in ieee80211_xmit_fast().
2841 *
2842 * The most difficult part of this is that when any of these assumptions
2843 * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
2844 * ieee80211_check_fast_xmit() or friends) is required to reset the data,
2845 * since the per-packet code no longer checks the conditions. This is reflected
2846 * by the calls to these functions throughout the rest of the code, and must be
2847 * maintained if any of the TX path checks change.
2848 */
2849
2850void ieee80211_check_fast_xmit(struct sta_info *sta)
2851{
2852 struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
2853 struct ieee80211_local *local = sta->local;
2854 struct ieee80211_sub_if_data *sdata = sta->sdata;
2855 struct ieee80211_hdr *hdr = (void *)build.hdr;
2856 struct ieee80211_chanctx_conf *chanctx_conf;
2857 __le16 fc;
2858
Johannes Berg30686bf2015-06-02 21:39:54 +02002859 if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002860 return;
2861
2862 /* Locking here protects both the pointer itself, and against concurrent
2863 * invocations winning data access races to, e.g., the key pointer that
2864 * is used.
2865 * Without it, the invocation of this function right after the key
2866 * pointer changes wouldn't be sufficient, as another CPU could access
2867 * the pointer, then stall, and then do the cache update after the CPU
2868 * that invalidated the key.
2869 * With the locking, such scenarios cannot happen as the check for the
2870 * key and the fast-tx assignment are done atomically, so the CPU that
2871 * modifies the key will either wait or other one will see the key
2872 * cleared/changed already.
2873 */
2874 spin_lock_bh(&sta->lock);
Johannes Berg30686bf2015-06-02 21:39:54 +02002875 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
2876 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
Johannes Berg17c18bf2015-03-21 15:25:43 +01002877 sdata->vif.type == NL80211_IFTYPE_STATION)
2878 goto out;
2879
2880 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2881 goto out;
2882
2883 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
2884 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
Felix Fietkauf7418bc2015-09-24 14:59:49 +02002885 test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
2886 test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002887 goto out;
2888
2889 if (sdata->noack_map)
2890 goto out;
2891
2892 /* fast-xmit doesn't handle fragmentation at all */
Johannes Berg725b8122015-04-10 14:02:08 +02002893 if (local->hw.wiphy->frag_threshold != (u32)-1 &&
Sara Sharonf3fe4e92016-10-18 23:12:11 +03002894 !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002895 goto out;
2896
2897 rcu_read_lock();
2898 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2899 if (!chanctx_conf) {
2900 rcu_read_unlock();
2901 goto out;
2902 }
2903 build.band = chanctx_conf->def.chan->band;
2904 rcu_read_unlock();
2905
2906 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2907
2908 switch (sdata->vif.type) {
Johannes Berg3ffd8842015-04-14 10:28:37 +02002909 case NL80211_IFTYPE_ADHOC:
2910 /* DA SA BSSID */
2911 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2912 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2913 memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
2914 build.hdr_len = 24;
2915 break;
Johannes Berg17c18bf2015-03-21 15:25:43 +01002916 case NL80211_IFTYPE_STATION:
2917 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2918 /* DA SA BSSID */
2919 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2920 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2921 memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
2922 build.hdr_len = 24;
2923 break;
2924 }
2925
2926 if (sdata->u.mgd.use_4addr) {
2927 /* non-regular ethertype cannot use the fastpath */
2928 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2929 IEEE80211_FCTL_TODS);
2930 /* RA TA DA SA */
2931 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2932 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2933 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2934 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2935 build.hdr_len = 30;
2936 break;
2937 }
2938 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2939 /* BSSID SA DA */
2940 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2941 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2942 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2943 build.hdr_len = 24;
2944 break;
2945 case NL80211_IFTYPE_AP_VLAN:
2946 if (sdata->wdev.use_4addr) {
2947 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2948 IEEE80211_FCTL_TODS);
2949 /* RA TA DA SA */
2950 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
2951 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2952 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2953 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2954 build.hdr_len = 30;
2955 break;
2956 }
2957 /* fall through */
2958 case NL80211_IFTYPE_AP:
2959 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2960 /* DA BSSID SA */
2961 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2962 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2963 build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
2964 build.hdr_len = 24;
2965 break;
2966 default:
2967 /* not handled on fast-xmit */
2968 goto out;
2969 }
2970
2971 if (sta->sta.wme) {
2972 build.hdr_len += 2;
2973 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2974 }
2975
2976 /* We store the key here so there's no point in using rcu_dereference()
2977 * but that's fine because the code that changes the pointers will call
2978 * this function after doing so. For a single CPU that would be enough,
2979 * for multiple see the comment above.
2980 */
2981 build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
2982 if (!build.key)
2983 build.key = rcu_access_pointer(sdata->default_unicast_key);
2984 if (build.key) {
Johannes Berge495c242015-04-10 14:03:17 +02002985 bool gen_iv, iv_spc, mmic;
Johannes Berg17c18bf2015-03-21 15:25:43 +01002986
2987 gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
2988 iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
David Spinadel9de18d82017-12-01 13:50:52 +02002989 mmic = build.key->conf.flags &
2990 (IEEE80211_KEY_FLAG_GENERATE_MMIC |
2991 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
Johannes Berg17c18bf2015-03-21 15:25:43 +01002992
2993 /* don't handle software crypto */
2994 if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
2995 goto out;
2996
Alexander Wetzel62872a92018-08-31 15:00:38 +02002997 /* Key is being removed */
2998 if (build.key->flags & KEY_FLAG_TAINTED)
2999 goto out;
3000
Johannes Berg17c18bf2015-03-21 15:25:43 +01003001 switch (build.key->conf.cipher) {
3002 case WLAN_CIPHER_SUITE_CCMP:
3003 case WLAN_CIPHER_SUITE_CCMP_256:
3004 /* add fixed key ID */
3005 if (gen_iv) {
3006 (build.hdr + build.hdr_len)[3] =
3007 0x20 | (build.key->conf.keyidx << 6);
3008 build.pn_offs = build.hdr_len;
3009 }
3010 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:
3015 /* add fixed key ID */
3016 if (gen_iv) {
3017 (build.hdr + build.hdr_len)[3] =
3018 0x20 | (build.key->conf.keyidx << 6);
3019 build.pn_offs = build.hdr_len;
3020 }
3021 if (gen_iv || iv_spc)
3022 build.hdr_len += IEEE80211_GCMP_HDR_LEN;
3023 break;
Johannes Berge495c242015-04-10 14:03:17 +02003024 case WLAN_CIPHER_SUITE_TKIP:
3025 /* cannot handle MMIC or IV generation in xmit-fast */
3026 if (mmic || gen_iv)
3027 goto out;
3028 if (iv_spc)
3029 build.hdr_len += IEEE80211_TKIP_IV_LEN;
3030 break;
3031 case WLAN_CIPHER_SUITE_WEP40:
3032 case WLAN_CIPHER_SUITE_WEP104:
3033 /* cannot handle IV generation in fast-xmit */
3034 if (gen_iv)
3035 goto out;
3036 if (iv_spc)
3037 build.hdr_len += IEEE80211_WEP_IV_LEN;
3038 break;
3039 case WLAN_CIPHER_SUITE_AES_CMAC:
3040 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3041 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3042 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3043 WARN(1,
3044 "management cipher suite 0x%x enabled for data\n",
3045 build.key->conf.cipher);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003046 goto out;
Johannes Berge495c242015-04-10 14:03:17 +02003047 default:
3048 /* we don't know how to generate IVs for this at all */
3049 if (WARN_ON(gen_iv))
3050 goto out;
3051 /* pure hardware keys are OK, of course */
3052 if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
3053 break;
3054 /* cipher scheme might require space allocation */
3055 if (iv_spc &&
3056 build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
3057 goto out;
3058 if (iv_spc)
3059 build.hdr_len += build.key->conf.iv_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003060 }
3061
3062 fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3063 }
3064
3065 hdr->frame_control = fc;
3066
3067 memcpy(build.hdr + build.hdr_len,
3068 rfc1042_header, sizeof(rfc1042_header));
3069 build.hdr_len += sizeof(rfc1042_header);
3070
3071 fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3072 /* if the kmemdup fails, continue w/o fast_tx */
3073 if (!fast_tx)
3074 goto out;
3075
3076 out:
3077 /* we might have raced against another call to this function */
3078 old = rcu_dereference_protected(sta->fast_tx,
3079 lockdep_is_held(&sta->lock));
3080 rcu_assign_pointer(sta->fast_tx, fast_tx);
3081 if (old)
3082 kfree_rcu(old, rcu_head);
3083 spin_unlock_bh(&sta->lock);
3084}
3085
3086void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3087{
3088 struct sta_info *sta;
3089
3090 rcu_read_lock();
3091 list_for_each_entry_rcu(sta, &local->sta_list, list)
3092 ieee80211_check_fast_xmit(sta);
3093 rcu_read_unlock();
3094}
3095
3096void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3097{
3098 struct ieee80211_local *local = sdata->local;
3099 struct sta_info *sta;
3100
3101 rcu_read_lock();
3102
3103 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3104 if (sdata != sta->sdata &&
3105 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3106 continue;
3107 ieee80211_check_fast_xmit(sta);
3108 }
3109
3110 rcu_read_unlock();
3111}
3112
3113void ieee80211_clear_fast_xmit(struct sta_info *sta)
3114{
3115 struct ieee80211_fast_tx *fast_tx;
3116
3117 spin_lock_bh(&sta->lock);
3118 fast_tx = rcu_dereference_protected(sta->fast_tx,
3119 lockdep_is_held(&sta->lock));
3120 RCU_INIT_POINTER(sta->fast_tx, NULL);
3121 spin_unlock_bh(&sta->lock);
3122
3123 if (fast_tx)
3124 kfree_rcu(fast_tx, rcu_head);
3125}
3126
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003127static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
Sara Sharon166ac9d2018-08-29 08:57:02 +02003128 struct sk_buff *skb, int headroom)
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003129{
Sara Sharon166ac9d2018-08-29 08:57:02 +02003130 if (skb_headroom(skb) < headroom) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003131 I802_DEBUG_INC(local->tx_expand_skb_head);
3132
Sara Sharon166ac9d2018-08-29 08:57:02 +02003133 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003134 wiphy_debug(local->hw.wiphy,
3135 "failed to reallocate TX buffer\n");
3136 return false;
3137 }
3138 }
3139
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003140 return true;
3141}
3142
3143static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3144 struct ieee80211_fast_tx *fast_tx,
3145 struct sk_buff *skb)
3146{
3147 struct ieee80211_local *local = sdata->local;
3148 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3149 struct ieee80211_hdr *hdr;
Michael Braun06f2bb12016-10-15 13:28:18 +02003150 struct ethhdr *amsdu_hdr;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003151 int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3152 int subframe_len = skb->len - hdr_len;
3153 void *data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003154 u8 *qc, *h_80211_src, *h_80211_dst;
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003155 const u8 *bssid;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003156
3157 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3158 return false;
3159
3160 if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3161 return true;
3162
Sara Sharon166ac9d2018-08-29 08:57:02 +02003163 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(*amsdu_hdr)))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003164 return false;
3165
Michael Braun06f2bb12016-10-15 13:28:18 +02003166 data = skb_push(skb, sizeof(*amsdu_hdr));
3167 memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003168 hdr = data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003169 amsdu_hdr = data + hdr_len;
3170 /* h_80211_src/dst is addr* field within hdr */
3171 h_80211_src = data + fast_tx->sa_offs;
3172 h_80211_dst = data + fast_tx->da_offs;
3173
3174 amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3175 ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3176 ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
3177
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003178 /* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3179 * fields needs to be changed to BSSID for A-MSDU frames depending
3180 * on FromDS/ToDS values.
3181 */
3182 switch (sdata->vif.type) {
3183 case NL80211_IFTYPE_STATION:
3184 bssid = sdata->u.mgd.bssid;
3185 break;
3186 case NL80211_IFTYPE_AP:
3187 case NL80211_IFTYPE_AP_VLAN:
3188 bssid = sdata->vif.addr;
3189 break;
3190 default:
3191 bssid = NULL;
3192 }
3193
3194 if (bssid && ieee80211_has_fromds(hdr->frame_control))
3195 ether_addr_copy(h_80211_src, bssid);
3196
3197 if (bssid && ieee80211_has_tods(hdr->frame_control))
3198 ether_addr_copy(h_80211_dst, bssid);
3199
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003200 qc = ieee80211_get_qos_ctl(hdr);
3201 *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3202
3203 info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3204
3205 return true;
3206}
3207
3208static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3209 struct sta_info *sta,
3210 struct ieee80211_fast_tx *fast_tx,
3211 struct sk_buff *skb)
3212{
3213 struct ieee80211_local *local = sdata->local;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003214 struct fq *fq = &local->fq;
3215 struct fq_tin *tin;
3216 struct fq_flow *flow;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003217 u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3218 struct ieee80211_txq *txq = sta->sta.txq[tid];
3219 struct txq_info *txqi;
3220 struct sk_buff **frag_tail, *head;
3221 int subframe_len = skb->len - ETH_ALEN;
3222 u8 max_subframes = sta->sta.max_amsdu_subframes;
3223 int max_frags = local->hw.max_tx_fragments;
3224 int max_amsdu_len = sta->sta.max_amsdu_len;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003225 u32 flow_idx;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003226 __be16 len;
3227 void *data;
3228 bool ret = false;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003229 unsigned int orig_len;
Lorenzo Bianconi66eb02d2018-08-31 01:04:13 +02003230 int n = 2, nfrags, pad = 0;
Sara Sharon166ac9d2018-08-29 08:57:02 +02003231 u16 hdrlen;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003232
3233 if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
3234 return false;
3235
Sara Sharon344f8e02018-12-15 11:03:07 +02003236 if (skb_is_gso(skb))
3237 return false;
3238
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003239 if (!txq)
3240 return false;
3241
3242 txqi = to_txq_info(txq);
3243 if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3244 return false;
3245
3246 if (sta->sta.max_rc_amsdu_len)
3247 max_amsdu_len = min_t(int, max_amsdu_len,
3248 sta->sta.max_rc_amsdu_len);
3249
Sara Sharonedba6bd2018-09-05 08:06:10 +03003250 if (sta->sta.max_tid_amsdu_len[tid])
3251 max_amsdu_len = min_t(int, max_amsdu_len,
3252 sta->sta.max_tid_amsdu_len[tid]);
3253
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003254 flow_idx = fq_flow_idx(fq, skb);
3255
Michal Kaziorfa962b92016-05-19 10:37:49 +02003256 spin_lock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003257
Michal Kaziorfa962b92016-05-19 10:37:49 +02003258 /* TODO: Ideally aggregation should be done on dequeue to remain
3259 * responsive to environment changes.
3260 */
3261
3262 tin = &txqi->tin;
Felix Fietkauf2af2df2019-03-16 18:06:32 +01003263 flow = fq_flow_classify(fq, tin, flow_idx, skb,
3264 fq_flow_get_default_func);
Michal Kaziorfa962b92016-05-19 10:37:49 +02003265 head = skb_peek_tail(&flow->queue);
Sara Sharon344f8e02018-12-15 11:03:07 +02003266 if (!head || skb_is_gso(head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003267 goto out;
3268
Michal Kaziorfa962b92016-05-19 10:37:49 +02003269 orig_len = head->len;
3270
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003271 if (skb->len + head->len > max_amsdu_len)
3272 goto out;
3273
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003274 nfrags = 1 + skb_shinfo(skb)->nr_frags;
3275 nfrags += 1 + skb_shinfo(head)->nr_frags;
3276 frag_tail = &skb_shinfo(head)->frag_list;
3277 while (*frag_tail) {
3278 nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3279 frag_tail = &(*frag_tail)->next;
3280 n++;
3281 }
3282
3283 if (max_subframes && n > max_subframes)
3284 goto out;
3285
3286 if (max_frags && nfrags > max_frags)
3287 goto out;
3288
Sara Sharon9739fe22018-09-05 08:06:11 +03003289 if (!drv_can_aggregate_in_amsdu(local, head, skb))
3290 goto out;
3291
Lorenzo Bianconi1eb50792018-08-29 21:03:25 +02003292 if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003293 goto out;
3294
Sara Sharon166ac9d2018-08-29 08:57:02 +02003295 /*
3296 * Pad out the previous subframe to a multiple of 4 by adding the
3297 * padding to the next one, that's being added. Note that head->len
3298 * is the length of the full A-MSDU, but that works since each time
3299 * we add a new subframe we pad out the previous one to a multiple
3300 * of 4 and thus it no longer matters in the next round.
3301 */
3302 hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3303 if ((head->len - hdrlen) & 3)
3304 pad = 4 - ((head->len - hdrlen) & 3);
3305
3306 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3307 2 + pad))
Johannes Bergaa58acf2018-08-30 10:55:49 +02003308 goto out_recalc;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003309
3310 ret = true;
3311 data = skb_push(skb, ETH_ALEN + 2);
3312 memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3313
3314 data += 2 * ETH_ALEN;
3315 len = cpu_to_be16(subframe_len);
3316 memcpy(data, &len, 2);
3317 memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3318
Sara Sharon166ac9d2018-08-29 08:57:02 +02003319 memset(skb_push(skb, pad), 0, pad);
3320
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003321 head->len += skb->len;
3322 head->data_len += skb->len;
3323 *frag_tail = skb;
3324
Johannes Bergaa58acf2018-08-30 10:55:49 +02003325out_recalc:
3326 if (head->len != orig_len) {
3327 flow->backlog += head->len - orig_len;
3328 tin->backlog_bytes += head->len - orig_len;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003329
Johannes Bergaa58acf2018-08-30 10:55:49 +02003330 fq_recalc_backlog(fq, tin, flow);
3331 }
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003332out:
Michal Kaziorfa962b92016-05-19 10:37:49 +02003333 spin_unlock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003334
3335 return ret;
3336}
3337
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003338/*
3339 * Can be called while the sta lock is held. Anything that can cause packets to
3340 * be generated will cause deadlock!
3341 */
3342static void ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3343 struct sta_info *sta, u8 pn_offs,
3344 struct ieee80211_key *key,
3345 struct sk_buff *skb)
3346{
3347 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3348 struct ieee80211_hdr *hdr = (void *)skb->data;
3349 u8 tid = IEEE80211_NUM_TIDS;
3350
3351 if (key)
3352 info->control.hw_key = &key->conf;
3353
3354 ieee80211_tx_stats(skb->dev, skb->len);
3355
3356 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3357 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003358 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3359 } else {
3360 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3361 hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3362 sdata->sequence_number += 0x10;
3363 }
3364
3365 if (skb_shinfo(skb)->gso_size)
3366 sta->tx_stats.msdu[tid] +=
3367 DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3368 else
3369 sta->tx_stats.msdu[tid]++;
3370
3371 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3372
3373 /* statistics normally done by ieee80211_tx_h_stats (but that
3374 * has to consider fragmentation, so is more complex)
3375 */
3376 sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3377 sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
3378
3379 if (pn_offs) {
3380 u64 pn;
3381 u8 *crypto_hdr = skb->data + pn_offs;
3382
3383 switch (key->conf.cipher) {
3384 case WLAN_CIPHER_SUITE_CCMP:
3385 case WLAN_CIPHER_SUITE_CCMP_256:
3386 case WLAN_CIPHER_SUITE_GCMP:
3387 case WLAN_CIPHER_SUITE_GCMP_256:
3388 pn = atomic64_inc_return(&key->conf.tx_pn);
3389 crypto_hdr[0] = pn;
3390 crypto_hdr[1] = pn >> 8;
3391 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
Felix Fietkaua786f962016-11-04 10:27:54 +01003483 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3484 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3485 *ieee80211_get_qos_ctl(hdr) = tid;
3486 }
3487
Johannes Berg17c18bf2015-03-21 15:25:43 +01003488 __skb_queue_head_init(&tx.skbs);
3489
3490 tx.flags = IEEE80211_TX_UNICAST;
3491 tx.local = local;
3492 tx.sdata = sdata;
3493 tx.sta = sta;
3494 tx.key = fast_tx->key;
3495
Johannes Berg30686bf2015-06-02 21:39:54 +02003496 if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01003497 tx.skb = skb;
3498 r = ieee80211_tx_h_rate_ctrl(&tx);
3499 skb = tx.skb;
3500 tx.skb = NULL;
3501
3502 if (r != TX_CONTINUE) {
3503 if (r != TX_QUEUED)
3504 kfree_skb(skb);
3505 return true;
3506 }
3507 }
3508
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003509 if (ieee80211_queue_skb(local, sdata, sta, skb))
3510 return true;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003511
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003512 ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3513 fast_tx->key, skb);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003514
3515 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3516 sdata = container_of(sdata->bss,
3517 struct ieee80211_sub_if_data, u.ap);
3518
3519 __skb_queue_tail(&tx.skbs, skb);
3520 ieee80211_tx_frags(local, &sdata->vif, &sta->sta, &tx.skbs, false);
3521 return true;
3522}
3523
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003524struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3525 struct ieee80211_txq *txq)
3526{
3527 struct ieee80211_local *local = hw_to_local(hw);
3528 struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3529 struct ieee80211_hdr *hdr;
3530 struct sk_buff *skb = NULL;
3531 struct fq *fq = &local->fq;
3532 struct fq_tin *tin = &txqi->tin;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003533 struct ieee80211_tx_info *info;
3534 struct ieee80211_tx_data tx;
3535 ieee80211_tx_result r;
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303536 struct ieee80211_vif *vif = txq->vif;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003537
3538 spin_lock_bh(&fq->lock);
3539
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303540 if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags) ||
3541 test_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags))
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003542 goto out;
3543
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303544 if (vif->txqs_stopped[ieee80211_ac_from_tid(txq->tid)]) {
3545 set_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags);
3546 goto out;
3547 }
3548
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003549 /* Make sure fragments stay together. */
3550 skb = __skb_dequeue(&txqi->frags);
3551 if (skb)
3552 goto out;
3553
3554begin:
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003555 skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
3556 if (!skb)
3557 goto out;
3558
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003559 hdr = (struct ieee80211_hdr *)skb->data;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003560 info = IEEE80211_SKB_CB(skb);
3561
3562 memset(&tx, 0, sizeof(tx));
3563 __skb_queue_head_init(&tx.skbs);
3564 tx.local = local;
3565 tx.skb = skb;
3566 tx.sdata = vif_to_sdata(info->control.vif);
3567
3568 if (txq->sta)
3569 tx.sta = container_of(txq->sta, struct sta_info, sta);
3570
3571 /*
3572 * The key can be removed while the packet was queued, so need to call
3573 * this here to get the current key.
3574 */
3575 r = ieee80211_tx_h_select_key(&tx);
3576 if (r != TX_CONTINUE) {
3577 ieee80211_free_txskb(&local->hw, skb);
3578 goto begin;
3579 }
3580
Felix Fietkauc1f4c9e2016-11-04 10:27:52 +01003581 if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
3582 info->flags |= IEEE80211_TX_CTL_AMPDU;
3583 else
3584 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
3585
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003586 if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003587 struct sta_info *sta = container_of(txq->sta, struct sta_info,
3588 sta);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003589 u8 pn_offs = 0;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003590
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003591 if (tx.key &&
3592 (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
3593 pn_offs = ieee80211_hdrlen(hdr->frame_control);
3594
3595 ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
3596 tx.key, skb);
3597 } else {
3598 if (invoke_tx_handlers_late(&tx))
3599 goto begin;
3600
3601 skb = __skb_dequeue(&tx.skbs);
3602
3603 if (!skb_queue_empty(&tx.skbs))
3604 skb_queue_splice_tail(&tx.skbs, &txqi->frags);
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003605 }
3606
Johannes Berg233e98d2018-12-15 11:03:09 +02003607 if (skb_has_frag_list(skb) &&
Johannes Berg1e1430d2016-10-04 09:22:19 +02003608 !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
3609 if (skb_linearize(skb)) {
3610 ieee80211_free_txskb(&local->hw, skb);
3611 goto begin;
3612 }
3613 }
3614
Johannes Berg53168212017-06-22 12:20:30 +02003615 switch (tx.sdata->vif.type) {
3616 case NL80211_IFTYPE_MONITOR:
3617 if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
3618 vif = &tx.sdata->vif;
3619 break;
3620 }
3621 tx.sdata = rcu_dereference(local->monitor_sdata);
3622 if (tx.sdata) {
3623 vif = &tx.sdata->vif;
3624 info->hw_queue =
3625 vif->hw_queue[skb_get_queue_mapping(skb)];
3626 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
3627 ieee80211_free_txskb(&local->hw, skb);
3628 goto begin;
3629 } else {
3630 vif = NULL;
3631 }
3632 break;
3633 case NL80211_IFTYPE_AP_VLAN:
3634 tx.sdata = container_of(tx.sdata->bss,
3635 struct ieee80211_sub_if_data, u.ap);
3636 /* fall through */
3637 default:
3638 vif = &tx.sdata->vif;
3639 break;
3640 }
3641
3642 IEEE80211_SKB_CB(skb)->control.vif = vif;
Manikanta Pubbisetty21a5d4c2018-07-11 00:12:53 +05303643
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003644out:
3645 spin_unlock_bh(&fq->lock);
3646
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003647 return skb;
3648}
3649EXPORT_SYMBOL(ieee80211_tx_dequeue);
3650
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003651struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac)
3652{
3653 struct ieee80211_local *local = hw_to_local(hw);
3654 struct txq_info *txqi = NULL;
3655
3656 lockdep_assert_held(&local->active_txq_lock[ac]);
3657
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003658 begin:
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003659 txqi = list_first_entry_or_null(&local->active_txqs[ac],
3660 struct txq_info,
3661 schedule_order);
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003662 if (!txqi)
3663 return NULL;
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003664
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003665 if (txqi->txq.sta) {
3666 struct sta_info *sta = container_of(txqi->txq.sta,
3667 struct sta_info, sta);
3668
3669 if (sta->airtime[txqi->txq.ac].deficit < 0) {
3670 sta->airtime[txqi->txq.ac].deficit +=
3671 sta->airtime_weight;
3672 list_move_tail(&txqi->schedule_order,
3673 &local->active_txqs[txqi->txq.ac]);
3674 goto begin;
3675 }
3676 }
3677
3678
3679 if (txqi->schedule_round == local->schedule_round[ac])
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003680 return NULL;
3681
3682 list_del_init(&txqi->schedule_order);
3683 txqi->schedule_round = local->schedule_round[ac];
3684 return &txqi->txq;
3685}
3686EXPORT_SYMBOL(ieee80211_next_txq);
3687
3688void ieee80211_return_txq(struct ieee80211_hw *hw,
3689 struct ieee80211_txq *txq)
3690{
3691 struct ieee80211_local *local = hw_to_local(hw);
3692 struct txq_info *txqi = to_txq_info(txq);
3693
3694 lockdep_assert_held(&local->active_txq_lock[txq->ac]);
3695
3696 if (list_empty(&txqi->schedule_order) &&
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003697 (!skb_queue_empty(&txqi->frags) || txqi->tin.backlog_packets)) {
3698 /* If airtime accounting is active, always enqueue STAs at the
3699 * head of the list to ensure that they only get moved to the
3700 * back by the airtime DRR scheduler once they have a negative
3701 * deficit. A station that already has a negative deficit will
3702 * get immediately moved to the back of the list on the next
3703 * call to ieee80211_next_txq().
3704 */
3705 if (txqi->txq.sta &&
3706 wiphy_ext_feature_isset(local->hw.wiphy,
3707 NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
3708 list_add(&txqi->schedule_order,
3709 &local->active_txqs[txq->ac]);
3710 else
3711 list_add_tail(&txqi->schedule_order,
3712 &local->active_txqs[txq->ac]);
3713 }
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003714}
3715EXPORT_SYMBOL(ieee80211_return_txq);
3716
Toke Høiland-Jørgensen390298e2019-01-22 15:20:16 +01003717void ieee80211_schedule_txq(struct ieee80211_hw *hw,
3718 struct ieee80211_txq *txq)
3719 __acquires(txq_lock) __releases(txq_lock)
3720{
3721 struct ieee80211_local *local = hw_to_local(hw);
Toke Høiland-Jørgensen390298e2019-01-22 15:20:16 +01003722
3723 spin_lock_bh(&local->active_txq_lock[txq->ac]);
3724 ieee80211_return_txq(hw, txq);
3725 spin_unlock_bh(&local->active_txq_lock[txq->ac]);
3726}
3727EXPORT_SYMBOL(ieee80211_schedule_txq);
3728
Toke Høiland-Jørgensenb4809e92018-12-18 17:02:08 -08003729bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
3730 struct ieee80211_txq *txq)
3731{
3732 struct ieee80211_local *local = hw_to_local(hw);
3733 struct txq_info *iter, *tmp, *txqi = to_txq_info(txq);
3734 struct sta_info *sta;
3735 u8 ac = txq->ac;
3736
3737 lockdep_assert_held(&local->active_txq_lock[ac]);
3738
3739 if (!txqi->txq.sta)
3740 goto out;
3741
3742 if (list_empty(&txqi->schedule_order))
3743 goto out;
3744
3745 list_for_each_entry_safe(iter, tmp, &local->active_txqs[ac],
3746 schedule_order) {
3747 if (iter == txqi)
3748 break;
3749
3750 if (!iter->txq.sta) {
3751 list_move_tail(&iter->schedule_order,
3752 &local->active_txqs[ac]);
3753 continue;
3754 }
3755 sta = container_of(iter->txq.sta, struct sta_info, sta);
3756 if (sta->airtime[ac].deficit < 0)
3757 sta->airtime[ac].deficit += sta->airtime_weight;
3758 list_move_tail(&iter->schedule_order, &local->active_txqs[ac]);
3759 }
3760
3761 sta = container_of(txqi->txq.sta, struct sta_info, sta);
3762 if (sta->airtime[ac].deficit >= 0)
3763 goto out;
3764
3765 sta->airtime[ac].deficit += sta->airtime_weight;
3766 list_move_tail(&txqi->schedule_order, &local->active_txqs[ac]);
3767
3768 return false;
3769out:
3770 if (!list_empty(&txqi->schedule_order))
3771 list_del_init(&txqi->schedule_order);
3772
3773 return true;
3774}
3775EXPORT_SYMBOL(ieee80211_txq_may_transmit);
3776
Toke Høiland-Jørgensen18667602018-12-18 17:02:06 -08003777void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
3778 __acquires(txq_lock)
3779{
3780 struct ieee80211_local *local = hw_to_local(hw);
3781
3782 spin_lock_bh(&local->active_txq_lock[ac]);
3783 local->schedule_round[ac]++;
3784}
3785EXPORT_SYMBOL(ieee80211_txq_schedule_start);
3786
3787void ieee80211_txq_schedule_end(struct ieee80211_hw *hw, u8 ac)
3788 __releases(txq_lock)
3789{
3790 struct ieee80211_local *local = hw_to_local(hw);
3791
3792 spin_unlock_bh(&local->active_txq_lock[ac]);
3793}
3794EXPORT_SYMBOL(ieee80211_txq_schedule_end);
3795
Johannes Berg4c9451e2014-11-09 18:50:10 +02003796void __ieee80211_subif_start_xmit(struct sk_buff *skb,
3797 struct net_device *dev,
3798 u32 info_flags)
3799{
3800 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg97ffe752015-03-21 09:13:45 +01003801 struct sta_info *sta;
Johannes Berg80616c02015-04-14 14:50:41 +02003802 struct sk_buff *next;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003803
3804 if (unlikely(skb->len < ETH_HLEN)) {
3805 kfree_skb(skb);
3806 return;
3807 }
3808
3809 rcu_read_lock();
3810
Johannes Berg2d981fd2015-04-10 14:10:10 +02003811 if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
3812 goto out_free;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003813
Johannes Berg17c18bf2015-03-21 15:25:43 +01003814 if (!IS_ERR_OR_NULL(sta)) {
3815 struct ieee80211_fast_tx *fast_tx;
3816
Wen Gong70e53662018-08-08 18:40:01 +08003817 sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
Toke Høiland-Jørgensen36148c22018-02-02 16:11:05 +01003818
Johannes Berg17c18bf2015-03-21 15:25:43 +01003819 fast_tx = rcu_dereference(sta->fast_tx);
3820
3821 if (fast_tx &&
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003822 ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
Johannes Berg17c18bf2015-03-21 15:25:43 +01003823 goto out;
3824 }
3825
Johannes Berg80616c02015-04-14 14:50:41 +02003826 if (skb_is_gso(skb)) {
3827 struct sk_buff *segs;
Johannes Berg680a0da2015-04-13 16:58:25 +02003828
Johannes Berg80616c02015-04-14 14:50:41 +02003829 segs = skb_gso_segment(skb, 0);
3830 if (IS_ERR(segs)) {
Johannes Berg2d981fd2015-04-10 14:10:10 +02003831 goto out_free;
Johannes Berg80616c02015-04-14 14:50:41 +02003832 } else if (segs) {
3833 consume_skb(skb);
3834 skb = segs;
3835 }
3836 } else {
3837 /* we cannot process non-linear frames on this path */
3838 if (skb_linearize(skb)) {
3839 kfree_skb(skb);
3840 goto out;
3841 }
3842
3843 /* the frame could be fragmented, software-encrypted, and other
3844 * things so we cannot really handle checksum offload with it -
3845 * fix it up in software before we handle anything else.
3846 */
3847 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Johannes Bergf603f1f2015-05-05 15:25:33 +02003848 skb_set_transport_header(skb,
3849 skb_checksum_start_offset(skb));
Johannes Berg80616c02015-04-14 14:50:41 +02003850 if (skb_checksum_help(skb))
3851 goto out_free;
3852 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02003853 }
3854
Johannes Berg80616c02015-04-14 14:50:41 +02003855 next = skb;
3856 while (next) {
3857 skb = next;
3858 next = skb->next;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003859
Johannes Berg80616c02015-04-14 14:50:41 +02003860 skb->prev = NULL;
3861 skb->next = NULL;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003862
Johannes Berg80616c02015-04-14 14:50:41 +02003863 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta);
3864 if (IS_ERR(skb))
3865 goto out;
3866
Johannes Berg5a490512015-04-22 17:10:38 +02003867 ieee80211_tx_stats(dev, skb->len);
Johannes Berg80616c02015-04-14 14:50:41 +02003868
Johannes Bergb9771d42018-05-28 15:47:41 +02003869 ieee80211_xmit(sdata, sta, skb, 0);
Johannes Berg80616c02015-04-14 14:50:41 +02003870 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02003871 goto out;
3872 out_free:
3873 kfree_skb(skb);
Johannes Berg4c9451e2014-11-09 18:50:10 +02003874 out:
3875 rcu_read_unlock();
3876}
3877
Michael Braunebceec82016-11-22 11:52:18 +01003878static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
3879{
3880 struct ethhdr *eth;
3881 int err;
3882
3883 err = skb_ensure_writable(skb, ETH_HLEN);
3884 if (unlikely(err))
3885 return err;
3886
3887 eth = (void *)skb->data;
3888 ether_addr_copy(eth->h_dest, sta->sta.addr);
3889
3890 return 0;
3891}
3892
3893static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
3894 struct net_device *dev)
3895{
3896 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3897 const struct ethhdr *eth = (void *)skb->data;
3898 const struct vlan_ethhdr *ethvlan = (void *)skb->data;
3899 __be16 ethertype;
3900
3901 if (likely(!is_multicast_ether_addr(eth->h_dest)))
3902 return false;
3903
3904 switch (sdata->vif.type) {
3905 case NL80211_IFTYPE_AP_VLAN:
3906 if (sdata->u.vlan.sta)
3907 return false;
3908 if (sdata->wdev.use_4addr)
3909 return false;
3910 /* fall through */
3911 case NL80211_IFTYPE_AP:
3912 /* check runtime toggle for this bss */
3913 if (!sdata->bss->multicast_to_unicast)
3914 return false;
3915 break;
3916 default:
3917 return false;
3918 }
3919
3920 /* multicast to unicast conversion only for some payload */
3921 ethertype = eth->h_proto;
3922 if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
3923 ethertype = ethvlan->h_vlan_encapsulated_proto;
3924 switch (ethertype) {
3925 case htons(ETH_P_ARP):
3926 case htons(ETH_P_IP):
3927 case htons(ETH_P_IPV6):
3928 break;
3929 default:
3930 return false;
3931 }
3932
3933 return true;
3934}
3935
3936static void
3937ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
3938 struct sk_buff_head *queue)
3939{
3940 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3941 struct ieee80211_local *local = sdata->local;
3942 const struct ethhdr *eth = (struct ethhdr *)skb->data;
3943 struct sta_info *sta, *first = NULL;
3944 struct sk_buff *cloned_skb;
3945
3946 rcu_read_lock();
3947
3948 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3949 if (sdata != sta->sdata)
3950 /* AP-VLAN mismatch */
3951 continue;
3952 if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
3953 /* do not send back to source */
3954 continue;
3955 if (!first) {
3956 first = sta;
3957 continue;
3958 }
3959 cloned_skb = skb_clone(skb, GFP_ATOMIC);
3960 if (!cloned_skb)
3961 goto multicast;
3962 if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
3963 dev_kfree_skb(cloned_skb);
3964 goto multicast;
3965 }
3966 __skb_queue_tail(queue, cloned_skb);
3967 }
3968
3969 if (likely(first)) {
3970 if (unlikely(ieee80211_change_da(skb, first)))
3971 goto multicast;
3972 __skb_queue_tail(queue, skb);
3973 } else {
3974 /* no STA connected, drop */
3975 kfree_skb(skb);
3976 skb = NULL;
3977 }
3978
3979 goto out;
3980multicast:
3981 __skb_queue_purge(queue);
3982 __skb_queue_tail(queue, skb);
3983out:
3984 rcu_read_unlock();
3985}
3986
Johannes Berg4c9451e2014-11-09 18:50:10 +02003987/**
3988 * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
3989 * @skb: packet to be sent
3990 * @dev: incoming interface
3991 *
3992 * On failure skb will be freed.
3993 */
Liad Kaufman24d342c2014-11-09 18:50:07 +02003994netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
3995 struct net_device *dev)
3996{
Michael Braunebceec82016-11-22 11:52:18 +01003997 if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
3998 struct sk_buff_head queue;
3999
4000 __skb_queue_head_init(&queue);
4001 ieee80211_convert_to_unicast(skb, dev, &queue);
4002 while ((skb = __skb_dequeue(&queue)))
4003 __ieee80211_subif_start_xmit(skb, dev, 0);
4004 } else {
4005 __ieee80211_subif_start_xmit(skb, dev, 0);
4006 }
4007
Liad Kaufman24d342c2014-11-09 18:50:07 +02004008 return NETDEV_TX_OK;
4009}
Johannes Berge2ebc742007-07-27 15:43:22 +02004010
Johannes Berg7528ec52014-11-09 18:50:11 +02004011struct sk_buff *
4012ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
4013 struct sk_buff *skb, u32 info_flags)
4014{
4015 struct ieee80211_hdr *hdr;
4016 struct ieee80211_tx_data tx = {
4017 .local = sdata->local,
4018 .sdata = sdata,
4019 };
Johannes Berg97ffe752015-03-21 09:13:45 +01004020 struct sta_info *sta;
Johannes Berg7528ec52014-11-09 18:50:11 +02004021
4022 rcu_read_lock();
4023
Johannes Berg97ffe752015-03-21 09:13:45 +01004024 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4025 kfree_skb(skb);
4026 skb = ERR_PTR(-EINVAL);
4027 goto out;
4028 }
4029
4030 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta);
Johannes Berg7528ec52014-11-09 18:50:11 +02004031 if (IS_ERR(skb))
4032 goto out;
4033
4034 hdr = (void *)skb->data;
4035 tx.sta = sta_info_get(sdata, hdr->addr1);
4036 tx.skb = skb;
4037
4038 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
4039 rcu_read_unlock();
4040 kfree_skb(skb);
4041 return ERR_PTR(-EINVAL);
4042 }
4043
4044out:
4045 rcu_read_unlock();
4046 return skb;
4047}
4048
Johannes Berge2530082008-05-17 00:57:14 +02004049/*
4050 * ieee80211_clear_tx_pending may not be called in a context where
4051 * it is possible that it packets could come in again.
4052 */
Johannes Berge2ebc742007-07-27 15:43:22 +02004053void ieee80211_clear_tx_pending(struct ieee80211_local *local)
4054{
Felix Fietkau1f98ab72012-11-10 03:44:14 +01004055 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01004056 int i;
Johannes Berge2ebc742007-07-27 15:43:22 +02004057
Felix Fietkau1f98ab72012-11-10 03:44:14 +01004058 for (i = 0; i < local->hw.queues; i++) {
4059 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
4060 ieee80211_free_txskb(&local->hw, skb);
4061 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004062}
4063
Johannes Berg7bb45682011-02-24 14:42:06 +01004064/*
4065 * Returns false if the frame couldn't be transmitted but was queued instead,
4066 * which in this case means re-queued -- take as an indication to stop sending
4067 * more pending frames.
4068 */
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004069static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
4070 struct sk_buff *skb)
4071{
4072 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4073 struct ieee80211_sub_if_data *sdata;
4074 struct sta_info *sta;
4075 struct ieee80211_hdr *hdr;
Johannes Berg7bb45682011-02-24 14:42:06 +01004076 bool result;
Johannes Berg55de9082012-07-26 17:24:39 +02004077 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004078
Johannes Berg5061b0c2009-07-14 00:33:34 +02004079 sdata = vif_to_sdata(info->control.vif);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004080
4081 if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
Johannes Berg55de9082012-07-26 17:24:39 +02004082 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4083 if (unlikely(!chanctx_conf)) {
4084 dev_kfree_skb(skb);
4085 return true;
4086 }
Johannes Berg73c4e192014-11-09 18:50:09 +02004087 info->band = chanctx_conf->def.chan->band;
Johannes Bergb9771d42018-05-28 15:47:41 +02004088 result = ieee80211_tx(sdata, NULL, skb, true, 0);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004089 } else {
Johannes Berg252b86c2011-11-16 15:28:55 +01004090 struct sk_buff_head skbs;
4091
4092 __skb_queue_head_init(&skbs);
4093 __skb_queue_tail(&skbs, skb);
4094
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004095 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Bergabe60632009-11-25 17:46:18 +01004096 sta = sta_info_get(sdata, hdr->addr1);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004097
Johannes Berg74e4dbf2011-11-16 15:28:57 +01004098 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004099 }
4100
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004101 return result;
4102}
4103
Johannes Berge2530082008-05-17 00:57:14 +02004104/*
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004105 * Transmit all pending packets. Called from tasklet.
Johannes Berge2530082008-05-17 00:57:14 +02004106 */
Johannes Berge2ebc742007-07-27 15:43:22 +02004107void ieee80211_tx_pending(unsigned long data)
4108{
4109 struct ieee80211_local *local = (struct ieee80211_local *)data;
Johannes Berg2a577d92009-03-23 17:28:37 +01004110 unsigned long flags;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01004111 int i;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004112 bool txok;
Johannes Berge2ebc742007-07-27 15:43:22 +02004113
Johannes Bergf0e72852009-03-23 17:28:36 +01004114 rcu_read_lock();
Johannes Berg2a577d92009-03-23 17:28:37 +01004115
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004116 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg176be722009-03-12 23:49:28 +01004117 for (i = 0; i < local->hw.queues; i++) {
Johannes Berg2a577d92009-03-23 17:28:37 +01004118 /*
4119 * If queue is stopped by something other than due to pending
4120 * frames, or we have no pending frames, proceed to next queue.
4121 */
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004122 if (local->queue_stop_reasons[i] ||
Johannes Berg2a577d92009-03-23 17:28:37 +01004123 skb_queue_empty(&local->pending[i]))
Johannes Berge2ebc742007-07-27 15:43:22 +02004124 continue;
Johannes Berge2530082008-05-17 00:57:14 +02004125
Johannes Berg2a577d92009-03-23 17:28:37 +01004126 while (!skb_queue_empty(&local->pending[i])) {
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004127 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
Johannes Berg5061b0c2009-07-14 00:33:34 +02004128 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg5061b0c2009-07-14 00:33:34 +02004129
Johannes Berga7bc3762009-07-27 10:33:31 +02004130 if (WARN_ON(!info->control.vif)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02004131 ieee80211_free_txskb(&local->hw, skb);
Johannes Berga7bc3762009-07-27 10:33:31 +02004132 continue;
4133 }
4134
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004135 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
4136 flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01004137
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004138 txok = ieee80211_tx_pending_skb(local, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004139 spin_lock_irqsave(&local->queue_stop_reason_lock,
4140 flags);
4141 if (!txok)
Johannes Berg2a577d92009-03-23 17:28:37 +01004142 break;
Johannes Berge2ebc742007-07-27 15:43:22 +02004143 }
Johannes Berg7236fe22010-03-22 13:42:43 -07004144
4145 if (skb_queue_empty(&local->pending[i]))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02004146 ieee80211_propagate_queue_wake(local, i);
Johannes Berge2ebc742007-07-27 15:43:22 +02004147 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004148 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01004149
Johannes Bergf0e72852009-03-23 17:28:36 +01004150 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004151}
4152
4153/* functions for drivers to get certain frames */
4154
Marco Porscheac70c12013-01-07 16:04:50 +01004155static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004156 struct ps_data *ps, struct sk_buff *skb,
4157 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02004158{
4159 u8 *pos, *tim;
4160 int aid0 = 0;
4161 int i, have_bits = 0, n1, n2;
4162
4163 /* Generate bitmap for TIM only if there are any STAs in power save
4164 * mode. */
Marco Porschd012a602012-10-10 12:39:50 -07004165 if (atomic_read(&ps->num_sta_ps) > 0)
Johannes Berge2ebc742007-07-27 15:43:22 +02004166 /* in the hope that this is faster than
4167 * checking byte-for-byte */
Weilong Chenf359d3f2013-12-18 15:44:16 +08004168 have_bits = !bitmap_empty((unsigned long *)ps->tim,
Johannes Berge2ebc742007-07-27 15:43:22 +02004169 IEEE80211_MAX_AID+1);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004170 if (!is_template) {
4171 if (ps->dtim_count == 0)
4172 ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
4173 else
4174 ps->dtim_count--;
4175 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004176
Johannes Berg4df864c2017-06-16 14:29:21 +02004177 tim = pos = skb_put(skb, 6);
Johannes Berge2ebc742007-07-27 15:43:22 +02004178 *pos++ = WLAN_EID_TIM;
4179 *pos++ = 4;
Marco Porschd012a602012-10-10 12:39:50 -07004180 *pos++ = ps->dtim_count;
Johannes Berg88600202012-02-13 15:17:18 +01004181 *pos++ = sdata->vif.bss_conf.dtim_period;
Johannes Berge2ebc742007-07-27 15:43:22 +02004182
Marco Porschd012a602012-10-10 12:39:50 -07004183 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
Johannes Berge2ebc742007-07-27 15:43:22 +02004184 aid0 = 1;
4185
Marco Porschd012a602012-10-10 12:39:50 -07004186 ps->dtim_bc_mc = aid0 == 1;
Christian Lamparter512119b2011-01-31 20:48:44 +02004187
Johannes Berge2ebc742007-07-27 15:43:22 +02004188 if (have_bits) {
4189 /* Find largest even number N1 so that bits numbered 1 through
4190 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
4191 * (N2 + 1) x 8 through 2007 are 0. */
4192 n1 = 0;
4193 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
Marco Porschd012a602012-10-10 12:39:50 -07004194 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004195 n1 = i & 0xfe;
4196 break;
4197 }
4198 }
4199 n2 = n1;
4200 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
Marco Porschd012a602012-10-10 12:39:50 -07004201 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004202 n2 = i;
4203 break;
4204 }
4205 }
4206
4207 /* Bitmap control */
4208 *pos++ = n1 | aid0;
4209 /* Part Virt Bitmap */
Eliad Peller5220da32011-11-24 16:50:00 +02004210 skb_put(skb, n2 - n1);
Marco Porschd012a602012-10-10 12:39:50 -07004211 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
Johannes Berge2ebc742007-07-27 15:43:22 +02004212
4213 tim[1] = n2 - n1 + 4;
Johannes Berge2ebc742007-07-27 15:43:22 +02004214 } else {
4215 *pos++ = aid0; /* Bitmap control */
4216 *pos++ = 0; /* Part Virt Bitmap */
4217 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004218}
4219
Marco Porscheac70c12013-01-07 16:04:50 +01004220static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004221 struct ps_data *ps, struct sk_buff *skb,
4222 bool is_template)
Marco Porscheac70c12013-01-07 16:04:50 +01004223{
4224 struct ieee80211_local *local = sdata->local;
4225
4226 /*
4227 * Not very nice, but we want to allow the driver to call
4228 * ieee80211_beacon_get() as a response to the set_tim()
4229 * callback. That, however, is already invoked under the
4230 * sta_lock to guarantee consistent and race-free update
4231 * of the tim bitmap in mac80211 and the driver.
4232 */
4233 if (local->tim_in_locked_section) {
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004234 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Marco Porscheac70c12013-01-07 16:04:50 +01004235 } else {
Johannes Berg1b917312013-02-22 12:55:01 +01004236 spin_lock_bh(&local->tim_lock);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004237 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Johannes Berg1b917312013-02-22 12:55:01 +01004238 spin_unlock_bh(&local->tim_lock);
Marco Porscheac70c12013-01-07 16:04:50 +01004239 }
4240
4241 return 0;
4242}
4243
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004244static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
4245 struct beacon_data *beacon)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004246{
4247 struct probe_resp *resp;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004248 u8 *beacon_data;
4249 size_t beacon_data_len;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004250 int i;
Michal Kazioraf296bd2014-06-05 14:21:36 +02004251 u8 count = beacon->csa_current_counter;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004252
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004253 switch (sdata->vif.type) {
4254 case NL80211_IFTYPE_AP:
4255 beacon_data = beacon->tail;
4256 beacon_data_len = beacon->tail_len;
4257 break;
4258 case NL80211_IFTYPE_ADHOC:
4259 beacon_data = beacon->head;
4260 beacon_data_len = beacon->head_len;
4261 break;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004262 case NL80211_IFTYPE_MESH_POINT:
4263 beacon_data = beacon->head;
4264 beacon_data_len = beacon->head_len;
4265 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004266 default:
4267 return;
4268 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004269
Michal Kazioraf296bd2014-06-05 14:21:36 +02004270 rcu_read_lock();
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004271 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004272 resp = rcu_dereference(sdata->u.ap.probe_resp);
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004273
Michal Kazioraf296bd2014-06-05 14:21:36 +02004274 if (beacon->csa_counter_offsets[i]) {
4275 if (WARN_ON_ONCE(beacon->csa_counter_offsets[i] >=
4276 beacon_data_len)) {
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004277 rcu_read_unlock();
4278 return;
4279 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004280
4281 beacon_data[beacon->csa_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004282 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004283
4284 if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
4285 resp->data[resp->csa_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004286 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004287 rcu_read_unlock();
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004288}
4289
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004290static u8 __ieee80211_csa_update_counter(struct beacon_data *beacon)
4291{
4292 beacon->csa_current_counter--;
4293
4294 /* the counter should never reach 0 */
4295 WARN_ON_ONCE(!beacon->csa_current_counter);
4296
4297 return beacon->csa_current_counter;
4298}
4299
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004300u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
4301{
4302 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004303 struct beacon_data *beacon = NULL;
4304 u8 count = 0;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004305
Michal Kazioraf296bd2014-06-05 14:21:36 +02004306 rcu_read_lock();
4307
4308 if (sdata->vif.type == NL80211_IFTYPE_AP)
4309 beacon = rcu_dereference(sdata->u.ap.beacon);
4310 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4311 beacon = rcu_dereference(sdata->u.ibss.presp);
4312 else if (ieee80211_vif_is_mesh(&sdata->vif))
4313 beacon = rcu_dereference(sdata->u.mesh.beacon);
4314
4315 if (!beacon)
4316 goto unlock;
4317
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004318 count = __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004319
Michal Kazioraf296bd2014-06-05 14:21:36 +02004320unlock:
4321 rcu_read_unlock();
4322 return count;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004323}
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004324EXPORT_SYMBOL(ieee80211_csa_update_counter);
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004325
Gregory Greenman03737002018-04-20 13:49:24 +03004326void ieee80211_csa_set_counter(struct ieee80211_vif *vif, u8 counter)
4327{
4328 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4329 struct beacon_data *beacon = NULL;
4330
4331 rcu_read_lock();
4332
4333 if (sdata->vif.type == NL80211_IFTYPE_AP)
4334 beacon = rcu_dereference(sdata->u.ap.beacon);
4335 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4336 beacon = rcu_dereference(sdata->u.ibss.presp);
4337 else if (ieee80211_vif_is_mesh(&sdata->vif))
4338 beacon = rcu_dereference(sdata->u.mesh.beacon);
4339
4340 if (!beacon)
4341 goto unlock;
4342
4343 if (counter < beacon->csa_current_counter)
4344 beacon->csa_current_counter = counter;
4345
4346unlock:
4347 rcu_read_unlock();
4348}
4349EXPORT_SYMBOL(ieee80211_csa_set_counter);
4350
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004351bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
4352{
4353 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4354 struct beacon_data *beacon = NULL;
4355 u8 *beacon_data;
4356 size_t beacon_data_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004357 int ret = false;
4358
4359 if (!ieee80211_sdata_running(sdata))
4360 return false;
4361
4362 rcu_read_lock();
4363 if (vif->type == NL80211_IFTYPE_AP) {
4364 struct ieee80211_if_ap *ap = &sdata->u.ap;
4365
4366 beacon = rcu_dereference(ap->beacon);
4367 if (WARN_ON(!beacon || !beacon->tail))
4368 goto out;
4369 beacon_data = beacon->tail;
4370 beacon_data_len = beacon->tail_len;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004371 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
4372 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4373
4374 beacon = rcu_dereference(ifibss->presp);
4375 if (!beacon)
4376 goto out;
4377
4378 beacon_data = beacon->head;
4379 beacon_data_len = beacon->head_len;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004380 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
4381 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4382
4383 beacon = rcu_dereference(ifmsh->beacon);
4384 if (!beacon)
4385 goto out;
4386
4387 beacon_data = beacon->head;
4388 beacon_data_len = beacon->head_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004389 } else {
4390 WARN_ON(1);
4391 goto out;
4392 }
4393
Michal Kazior10d78f22014-06-05 14:21:37 +02004394 if (!beacon->csa_counter_offsets[0])
4395 goto out;
4396
Michal Kazioraf296bd2014-06-05 14:21:36 +02004397 if (WARN_ON_ONCE(beacon->csa_counter_offsets[0] > beacon_data_len))
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004398 goto out;
4399
Michal Kazioraf296bd2014-06-05 14:21:36 +02004400 if (beacon_data[beacon->csa_counter_offsets[0]] == 1)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004401 ret = true;
4402 out:
4403 rcu_read_unlock();
4404
4405 return ret;
4406}
4407EXPORT_SYMBOL(ieee80211_csa_is_complete);
4408
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004409static struct sk_buff *
4410__ieee80211_beacon_get(struct ieee80211_hw *hw,
4411 struct ieee80211_vif *vif,
4412 struct ieee80211_mutable_offsets *offs,
4413 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02004414{
4415 struct ieee80211_local *local = hw_to_local(hw);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004416 struct beacon_data *beacon = NULL;
Johannes Berg9d139c82008-07-09 14:40:37 +02004417 struct sk_buff *skb = NULL;
Johannes Berge039fa42008-05-15 12:55:29 +02004418 struct ieee80211_tx_info *info;
Johannes Berge2ebc742007-07-27 15:43:22 +02004419 struct ieee80211_sub_if_data *sdata = NULL;
Johannes Berg57fbcce2016-04-12 15:56:15 +02004420 enum nl80211_band band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004421 struct ieee80211_tx_rate_control txrc;
Johannes Berg55de9082012-07-26 17:24:39 +02004422 struct ieee80211_chanctx_conf *chanctx_conf;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004423 int csa_off_base = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004424
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004425 rcu_read_lock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004426
Johannes Berg32bfd352007-12-19 01:31:26 +01004427 sdata = vif_to_sdata(vif);
Johannes Berg55de9082012-07-26 17:24:39 +02004428 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berge2ebc742007-07-27 15:43:22 +02004429
Johannes Berg55de9082012-07-26 17:24:39 +02004430 if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
Felix Fietkaueb3e5542011-01-24 19:28:49 +01004431 goto out;
4432
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004433 if (offs)
4434 memset(offs, 0, sizeof(*offs));
Johannes Bergeddcbb942009-10-29 08:30:35 +01004435
Johannes Berg05c914f2008-09-11 00:01:58 +02004436 if (sdata->vif.type == NL80211_IFTYPE_AP) {
Marco Porschd012a602012-10-10 12:39:50 -07004437 struct ieee80211_if_ap *ap = &sdata->u.ap;
Marco Porschd012a602012-10-10 12:39:50 -07004438
Michal Kazioraf296bd2014-06-05 14:21:36 +02004439 beacon = rcu_dereference(ap->beacon);
Dan Carpenter3ad97fb2011-02-07 22:03:35 +03004440 if (beacon) {
Michal Kazior10d78f22014-06-05 14:21:37 +02004441 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004442 if (!is_template)
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004443 __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004444
4445 ieee80211_set_csa(sdata, beacon);
4446 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004447
Johannes Berg902acc72008-02-23 15:17:19 +01004448 /*
4449 * headroom, head length,
4450 * tail length and maximum TIM length
4451 */
4452 skb = dev_alloc_skb(local->tx_headroom +
4453 beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004454 beacon->tail_len + 256 +
4455 local->hw.extra_beacon_tailroom);
Johannes Berg902acc72008-02-23 15:17:19 +01004456 if (!skb)
4457 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004458
Johannes Berg902acc72008-02-23 15:17:19 +01004459 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004460 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg902acc72008-02-23 15:17:19 +01004461
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004462 ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
4463 is_template);
Johannes Berg902acc72008-02-23 15:17:19 +01004464
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004465 if (offs) {
4466 offs->tim_offset = beacon->head_len;
4467 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004468
4469 /* for AP the csa offsets are from tail */
4470 csa_off_base = skb->len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004471 }
Johannes Bergeddcbb942009-10-29 08:30:35 +01004472
Johannes Berg902acc72008-02-23 15:17:19 +01004473 if (beacon->tail)
Johannes Berg59ae1d12017-06-16 14:29:20 +02004474 skb_put_data(skb, beacon->tail,
4475 beacon->tail_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004476 } else
4477 goto out;
Johannes Berg05c914f2008-09-11 00:01:58 +02004478 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Berg46900292009-02-15 12:44:28 +01004479 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg9d139c82008-07-09 14:40:37 +02004480 struct ieee80211_hdr *hdr;
Johannes Berg902acc72008-02-23 15:17:19 +01004481
Michal Kazioraf296bd2014-06-05 14:21:36 +02004482 beacon = rcu_dereference(ifibss->presp);
4483 if (!beacon)
Johannes Berg9d139c82008-07-09 14:40:37 +02004484 goto out;
4485
Michal Kazior10d78f22014-06-05 14:21:37 +02004486 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004487 if (!is_template)
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004488 __ieee80211_csa_update_counter(beacon);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004489
Michal Kazioraf296bd2014-06-05 14:21:36 +02004490 ieee80211_set_csa(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004491 }
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004492
Michal Kazioraf296bd2014-06-05 14:21:36 +02004493 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004494 local->hw.extra_beacon_tailroom);
Johannes Berg9d139c82008-07-09 14:40:37 +02004495 if (!skb)
4496 goto out;
Johannes Bergc3ffeab2013-03-07 20:54:29 +01004497 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004498 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004499
4500 hdr = (struct ieee80211_hdr *) skb->data;
Harvey Harrisone7827a72008-07-15 18:44:13 -07004501 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4502 IEEE80211_STYPE_BEACON);
Johannes Berg902acc72008-02-23 15:17:19 +01004503 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
Javier Cardonadbf498f2012-03-31 11:31:32 -07004504 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg472dbc42008-09-11 00:01:49 +02004505
Michal Kazioraf296bd2014-06-05 14:21:36 +02004506 beacon = rcu_dereference(ifmsh->beacon);
4507 if (!beacon)
Johannes Bergac1bd842011-01-18 13:45:32 +01004508 goto out;
Johannes Bergac1bd842011-01-18 13:45:32 +01004509
Michal Kazior10d78f22014-06-05 14:21:37 +02004510 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004511 if (!is_template)
4512 /* TODO: For mesh csa_counter is in TU, so
4513 * decrementing it by one isn't correct, but
4514 * for now we leave it consistent with overall
4515 * mac80211's behavior.
4516 */
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004517 __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004518
Michal Kazioraf296bd2014-06-05 14:21:36 +02004519 ieee80211_set_csa(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004520 }
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004521
Javier Cardonadbf498f2012-03-31 11:31:32 -07004522 if (ifmsh->sync_ops)
Masashi Honma445cd452016-12-08 10:15:51 +09004523 ifmsh->sync_ops->adjust_tsf(sdata, beacon);
Javier Cardonadbf498f2012-03-31 11:31:32 -07004524
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -07004525 skb = dev_alloc_skb(local->tx_headroom +
Michal Kazioraf296bd2014-06-05 14:21:36 +02004526 beacon->head_len +
Marco Porsch3f52b7e2013-01-30 18:14:08 +01004527 256 + /* TIM IE */
Michal Kazioraf296bd2014-06-05 14:21:36 +02004528 beacon->tail_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004529 local->hw.extra_beacon_tailroom);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01004530 if (!skb)
4531 goto out;
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08004532 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004533 skb_put_data(skb, beacon->head, beacon->head_len);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004534 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
4535
4536 if (offs) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004537 offs->tim_offset = beacon->head_len;
4538 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004539 }
4540
Johannes Berg59ae1d12017-06-16 14:29:20 +02004541 skb_put_data(skb, beacon->tail, beacon->tail_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004542 } else {
4543 WARN_ON(1);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004544 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02004545 }
4546
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004547 /* CSA offsets */
Michal Kazioraf296bd2014-06-05 14:21:36 +02004548 if (offs && beacon) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004549 int i;
4550
4551 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004552 u16 csa_off = beacon->csa_counter_offsets[i];
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004553
4554 if (!csa_off)
4555 continue;
4556
4557 offs->csa_counter_offs[i] = csa_off_base + csa_off;
4558 }
4559 }
4560
Johannes Berg4bf88532012-11-09 11:39:59 +01004561 band = chanctx_conf->def.chan->band;
Johannes Berg55de9082012-07-26 17:24:39 +02004562
Johannes Berge039fa42008-05-15 12:55:29 +02004563 info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02004564
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004565 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004566 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Johannes Berge039fa42008-05-15 12:55:29 +02004567 info->band = band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004568
4569 memset(&txrc, 0, sizeof(txrc));
4570 txrc.hw = hw;
Johannes Berge31583c2012-07-26 14:07:46 +02004571 txrc.sband = local->hw.wiphy->bands[band];
Jouni Malinene00cfce2009-12-29 12:59:19 +02004572 txrc.bss_conf = &sdata->vif.bss_conf;
4573 txrc.skb = skb;
4574 txrc.reported_rate.idx = -1;
Jouni Malinen37eb0b12010-01-06 13:09:08 +02004575 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
Felix Fietkau8f0729b2010-11-11 15:07:23 +01004576 txrc.bss = true;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004577 rate_control_get_rate(sdata, NULL, &txrc);
Johannes Berge039fa42008-05-15 12:55:29 +02004578
4579 info->control.vif = vif;
Johannes Bergf591fa52008-07-10 11:21:26 +02004580
John W. Linvillea472e712010-05-06 14:45:17 -04004581 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
4582 IEEE80211_TX_CTL_ASSIGN_SEQ |
4583 IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge6a98542008-10-21 12:40:02 +02004584 out:
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004585 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004586 return skb;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004587
4588}
4589
4590struct sk_buff *
4591ieee80211_beacon_get_template(struct ieee80211_hw *hw,
4592 struct ieee80211_vif *vif,
4593 struct ieee80211_mutable_offsets *offs)
4594{
4595 return __ieee80211_beacon_get(hw, vif, offs, true);
4596}
4597EXPORT_SYMBOL(ieee80211_beacon_get_template);
4598
4599struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
4600 struct ieee80211_vif *vif,
4601 u16 *tim_offset, u16 *tim_length)
4602{
4603 struct ieee80211_mutable_offsets offs = {};
4604 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
Helmut Schaa35afa582015-09-09 09:46:32 +02004605 struct sk_buff *copy;
4606 struct ieee80211_supported_band *sband;
4607 int shift;
4608
4609 if (!bcn)
4610 return bcn;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004611
4612 if (tim_offset)
4613 *tim_offset = offs.tim_offset;
4614
4615 if (tim_length)
4616 *tim_length = offs.tim_length;
4617
Helmut Schaa35afa582015-09-09 09:46:32 +02004618 if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
4619 !hw_to_local(hw)->monitors)
4620 return bcn;
4621
4622 /* send a copy to monitor interfaces */
4623 copy = skb_copy(bcn, GFP_ATOMIC);
4624 if (!copy)
4625 return bcn;
4626
4627 shift = ieee80211_vif_get_shift(vif);
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05304628 sband = ieee80211_get_sband(vif_to_sdata(vif));
4629 if (!sband)
4630 return bcn;
4631
Helmut Schaa35afa582015-09-09 09:46:32 +02004632 ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false);
4633
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004634 return bcn;
Johannes Berge2ebc742007-07-27 15:43:22 +02004635}
Johannes Bergeddcbb942009-10-29 08:30:35 +01004636EXPORT_SYMBOL(ieee80211_beacon_get_tim);
Johannes Berge2ebc742007-07-27 15:43:22 +02004637
Arik Nemtsov02945822011-11-10 11:28:57 +02004638struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
4639 struct ieee80211_vif *vif)
4640{
4641 struct ieee80211_if_ap *ap = NULL;
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004642 struct sk_buff *skb = NULL;
4643 struct probe_resp *presp = NULL;
Arik Nemtsov02945822011-11-10 11:28:57 +02004644 struct ieee80211_hdr *hdr;
4645 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4646
4647 if (sdata->vif.type != NL80211_IFTYPE_AP)
4648 return NULL;
4649
4650 rcu_read_lock();
4651
4652 ap = &sdata->u.ap;
4653 presp = rcu_dereference(ap->probe_resp);
4654 if (!presp)
4655 goto out;
4656
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004657 skb = dev_alloc_skb(presp->len);
Arik Nemtsov02945822011-11-10 11:28:57 +02004658 if (!skb)
4659 goto out;
4660
Johannes Berg59ae1d12017-06-16 14:29:20 +02004661 skb_put_data(skb, presp->data, presp->len);
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004662
Arik Nemtsov02945822011-11-10 11:28:57 +02004663 hdr = (struct ieee80211_hdr *) skb->data;
4664 memset(hdr->addr1, 0, sizeof(hdr->addr1));
4665
4666out:
4667 rcu_read_unlock();
4668 return skb;
4669}
4670EXPORT_SYMBOL(ieee80211_proberesp_get);
4671
Kalle Valo7044cc52010-01-05 20:16:19 +02004672struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
4673 struct ieee80211_vif *vif)
4674{
4675 struct ieee80211_sub_if_data *sdata;
4676 struct ieee80211_if_managed *ifmgd;
4677 struct ieee80211_pspoll *pspoll;
4678 struct ieee80211_local *local;
4679 struct sk_buff *skb;
4680
4681 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4682 return NULL;
4683
4684 sdata = vif_to_sdata(vif);
4685 ifmgd = &sdata->u.mgd;
4686 local = sdata->local;
4687
4688 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
Joe Perchesd15b8452011-08-29 14:17:31 -07004689 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02004690 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07004691
Kalle Valo7044cc52010-01-05 20:16:19 +02004692 skb_reserve(skb, local->hw.extra_tx_headroom);
4693
Johannes Bergb080db52017-06-16 14:29:19 +02004694 pspoll = skb_put_zero(skb, sizeof(*pspoll));
Kalle Valo7044cc52010-01-05 20:16:19 +02004695 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
4696 IEEE80211_STYPE_PSPOLL);
4697 pspoll->aid = cpu_to_le16(ifmgd->aid);
4698
4699 /* aid in PS-Poll has its two MSBs each set to 1 */
4700 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
4701
4702 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
4703 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
4704
4705 return skb;
4706}
4707EXPORT_SYMBOL(ieee80211_pspoll_get);
4708
4709struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004710 struct ieee80211_vif *vif,
4711 bool qos_ok)
Kalle Valo7044cc52010-01-05 20:16:19 +02004712{
4713 struct ieee80211_hdr_3addr *nullfunc;
4714 struct ieee80211_sub_if_data *sdata;
4715 struct ieee80211_if_managed *ifmgd;
4716 struct ieee80211_local *local;
4717 struct sk_buff *skb;
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004718 bool qos = false;
Kalle Valo7044cc52010-01-05 20:16:19 +02004719
4720 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4721 return NULL;
4722
4723 sdata = vif_to_sdata(vif);
4724 ifmgd = &sdata->u.mgd;
4725 local = sdata->local;
4726
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004727 if (qos_ok) {
4728 struct sta_info *sta;
4729
4730 rcu_read_lock();
4731 sta = sta_info_get(sdata, ifmgd->bssid);
4732 qos = sta && sta->sta.wme;
4733 rcu_read_unlock();
4734 }
4735
4736 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
4737 sizeof(*nullfunc) + 2);
Joe Perchesd15b8452011-08-29 14:17:31 -07004738 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02004739 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07004740
Kalle Valo7044cc52010-01-05 20:16:19 +02004741 skb_reserve(skb, local->hw.extra_tx_headroom);
4742
Johannes Bergb080db52017-06-16 14:29:19 +02004743 nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
Kalle Valo7044cc52010-01-05 20:16:19 +02004744 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
4745 IEEE80211_STYPE_NULLFUNC |
4746 IEEE80211_FCTL_TODS);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004747 if (qos) {
Johannes Berge0ba7092018-11-09 11:16:46 +01004748 __le16 qoshdr = cpu_to_le16(7);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004749
4750 BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
4751 IEEE80211_STYPE_NULLFUNC) !=
4752 IEEE80211_STYPE_QOS_NULLFUNC);
4753 nullfunc->frame_control |=
4754 cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
4755 skb->priority = 7;
4756 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
Johannes Berge0ba7092018-11-09 11:16:46 +01004757 skb_put_data(skb, &qoshdr, sizeof(qoshdr));
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004758 }
4759
Kalle Valo7044cc52010-01-05 20:16:19 +02004760 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
4761 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
4762 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
4763
4764 return skb;
4765}
4766EXPORT_SYMBOL(ieee80211_nullfunc_get);
4767
Kalle Valo05e54ea2010-01-05 20:16:38 +02004768struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
Johannes Berga344d672014-06-12 22:24:31 +02004769 const u8 *src_addr,
Kalle Valo05e54ea2010-01-05 20:16:38 +02004770 const u8 *ssid, size_t ssid_len,
Johannes Bergb9a9ada2012-11-29 13:00:10 +01004771 size_t tailroom)
Kalle Valo05e54ea2010-01-05 20:16:38 +02004772{
Johannes Berga344d672014-06-12 22:24:31 +02004773 struct ieee80211_local *local = hw_to_local(hw);
Kalle Valo05e54ea2010-01-05 20:16:38 +02004774 struct ieee80211_hdr_3addr *hdr;
4775 struct sk_buff *skb;
4776 size_t ie_ssid_len;
4777 u8 *pos;
4778
Kalle Valo05e54ea2010-01-05 20:16:38 +02004779 ie_ssid_len = 2 + ssid_len;
4780
4781 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
Johannes Bergb9a9ada2012-11-29 13:00:10 +01004782 ie_ssid_len + tailroom);
Joe Perchesd15b8452011-08-29 14:17:31 -07004783 if (!skb)
Kalle Valo05e54ea2010-01-05 20:16:38 +02004784 return NULL;
Kalle Valo05e54ea2010-01-05 20:16:38 +02004785
4786 skb_reserve(skb, local->hw.extra_tx_headroom);
4787
Johannes Bergb080db52017-06-16 14:29:19 +02004788 hdr = skb_put_zero(skb, sizeof(*hdr));
Kalle Valo05e54ea2010-01-05 20:16:38 +02004789 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4790 IEEE80211_STYPE_PROBE_REQ);
Johannes Berge83e6542012-07-13 16:23:07 +02004791 eth_broadcast_addr(hdr->addr1);
Johannes Berga344d672014-06-12 22:24:31 +02004792 memcpy(hdr->addr2, src_addr, ETH_ALEN);
Johannes Berge83e6542012-07-13 16:23:07 +02004793 eth_broadcast_addr(hdr->addr3);
Kalle Valo05e54ea2010-01-05 20:16:38 +02004794
4795 pos = skb_put(skb, ie_ssid_len);
4796 *pos++ = WLAN_EID_SSID;
4797 *pos++ = ssid_len;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02004798 if (ssid_len)
Kalle Valo05e54ea2010-01-05 20:16:38 +02004799 memcpy(pos, ssid, ssid_len);
4800 pos += ssid_len;
4801
Kalle Valo05e54ea2010-01-05 20:16:38 +02004802 return skb;
4803}
4804EXPORT_SYMBOL(ieee80211_probereq_get);
4805
Johannes Berg32bfd352007-12-19 01:31:26 +01004806void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02004807 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02004808 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02004809 struct ieee80211_rts *rts)
4810{
4811 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02004812
Harvey Harrison065e96052008-06-22 16:45:27 -07004813 rts->frame_control =
4814 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01004815 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
4816 frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02004817 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
4818 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
4819}
4820EXPORT_SYMBOL(ieee80211_rts_get);
4821
Johannes Berg32bfd352007-12-19 01:31:26 +01004822void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02004823 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02004824 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02004825 struct ieee80211_cts *cts)
4826{
4827 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02004828
Harvey Harrison065e96052008-06-22 16:45:27 -07004829 cts->frame_control =
4830 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01004831 cts->duration = ieee80211_ctstoself_duration(hw, vif,
4832 frame_len, frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02004833 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
4834}
4835EXPORT_SYMBOL(ieee80211_ctstoself_get);
4836
4837struct sk_buff *
Johannes Berg32bfd352007-12-19 01:31:26 +01004838ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
Johannes Berge039fa42008-05-15 12:55:29 +02004839 struct ieee80211_vif *vif)
Johannes Berge2ebc742007-07-27 15:43:22 +02004840{
4841 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004842 struct sk_buff *skb = NULL;
Johannes Berg5cf121c2008-02-25 16:27:43 +01004843 struct ieee80211_tx_data tx;
Johannes Berge2ebc742007-07-27 15:43:22 +02004844 struct ieee80211_sub_if_data *sdata;
Marco Porschd012a602012-10-10 12:39:50 -07004845 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +02004846 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02004847 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berge2ebc742007-07-27 15:43:22 +02004848
Johannes Berg32bfd352007-12-19 01:31:26 +01004849 sdata = vif_to_sdata(vif);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004850
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004851 rcu_read_lock();
Johannes Berg55de9082012-07-26 17:24:39 +02004852 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004853
Marco Porschd012a602012-10-10 12:39:50 -07004854 if (!chanctx_conf)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004855 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004856
Marco Porschd012a602012-10-10 12:39:50 -07004857 if (sdata->vif.type == NL80211_IFTYPE_AP) {
4858 struct beacon_data *beacon =
4859 rcu_dereference(sdata->u.ap.beacon);
4860
4861 if (!beacon || !beacon->head)
4862 goto out;
4863
4864 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01004865 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4866 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07004867 } else {
4868 goto out;
4869 }
4870
4871 if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004872 goto out; /* send buffered bc/mc only after DTIM beacon */
Johannes Berge039fa42008-05-15 12:55:29 +02004873
Johannes Berge2ebc742007-07-27 15:43:22 +02004874 while (1) {
Marco Porschd012a602012-10-10 12:39:50 -07004875 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +02004876 if (!skb)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004877 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02004878 local->total_ps_buffered--;
4879
Marco Porschd012a602012-10-10 12:39:50 -07004880 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004881 struct ieee80211_hdr *hdr =
4882 (struct ieee80211_hdr *) skb->data;
4883 /* more buffered multicast/broadcast frames ==> set
4884 * MoreData flag in IEEE 802.11 header to inform PS
4885 * STAs */
4886 hdr->frame_control |=
4887 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
4888 }
4889
Michael Braun112c44b2014-03-06 15:08:43 +01004890 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porsch7cbf9d02013-03-01 16:01:18 +01004891 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
Johannes Berg7c107702015-03-20 14:18:27 +01004892 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
Johannes Berge2ebc742007-07-27 15:43:22 +02004893 break;
Felix Fietkau6b07d9c2016-08-02 11:13:41 +02004894 ieee80211_free_txskb(hw, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02004895 }
Johannes Berge039fa42008-05-15 12:55:29 +02004896
4897 info = IEEE80211_SKB_CB(skb);
4898
Johannes Berg5cf121c2008-02-25 16:27:43 +01004899 tx.flags |= IEEE80211_TX_PS_BUFFERED;
Johannes Berg4bf88532012-11-09 11:39:59 +01004900 info->band = chanctx_conf->def.chan->band;
Johannes Berge2ebc742007-07-27 15:43:22 +02004901
Johannes Berg97b045d2008-06-20 01:22:30 +02004902 if (invoke_tx_handlers(&tx))
Johannes Berge2ebc742007-07-27 15:43:22 +02004903 skb = NULL;
Johannes Berg97b045d2008-06-20 01:22:30 +02004904 out:
Johannes Bergd0709a62008-02-25 16:27:46 +01004905 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004906
4907 return skb;
4908}
4909EXPORT_SYMBOL(ieee80211_get_buffered_bc);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004910
Liad Kaufmanb6da9112014-11-19 13:47:38 +02004911int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
4912{
4913 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
4914 struct ieee80211_sub_if_data *sdata = sta->sdata;
4915 struct ieee80211_local *local = sdata->local;
4916 int ret;
4917 u32 queues;
4918
4919 lockdep_assert_held(&local->sta_mtx);
4920
4921 /* only some cases are supported right now */
4922 switch (sdata->vif.type) {
4923 case NL80211_IFTYPE_STATION:
4924 case NL80211_IFTYPE_AP:
4925 case NL80211_IFTYPE_AP_VLAN:
4926 break;
4927 default:
4928 WARN_ON(1);
4929 return -EINVAL;
4930 }
4931
4932 if (WARN_ON(tid >= IEEE80211_NUM_UPS))
4933 return -EINVAL;
4934
4935 if (sta->reserved_tid == tid) {
4936 ret = 0;
4937 goto out;
4938 }
4939
4940 if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
4941 sdata_err(sdata, "TID reservation already active\n");
4942 ret = -EALREADY;
4943 goto out;
4944 }
4945
4946 ieee80211_stop_vif_queues(sdata->local, sdata,
4947 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
4948
4949 synchronize_net();
4950
4951 /* Tear down BA sessions so we stop aggregating on this TID */
Johannes Berg30686bf2015-06-02 21:39:54 +02004952 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
Liad Kaufmanb6da9112014-11-19 13:47:38 +02004953 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
4954 __ieee80211_stop_tx_ba_session(sta, tid,
4955 AGG_STOP_LOCAL_REQUEST);
4956 }
4957
4958 queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02004959 __ieee80211_flush_queues(local, sdata, queues, false);
Liad Kaufmanb6da9112014-11-19 13:47:38 +02004960
4961 sta->reserved_tid = tid;
4962
4963 ieee80211_wake_vif_queues(local, sdata,
4964 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
4965
Johannes Berg30686bf2015-06-02 21:39:54 +02004966 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
Liad Kaufmanb6da9112014-11-19 13:47:38 +02004967 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
4968
4969 ret = 0;
4970 out:
4971 return ret;
4972}
4973EXPORT_SYMBOL(ieee80211_reserve_tid);
4974
4975void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
4976{
4977 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
4978 struct ieee80211_sub_if_data *sdata = sta->sdata;
4979
4980 lockdep_assert_held(&sdata->local->sta_mtx);
4981
4982 /* only some cases are supported right now */
4983 switch (sdata->vif.type) {
4984 case NL80211_IFTYPE_STATION:
4985 case NL80211_IFTYPE_AP:
4986 case NL80211_IFTYPE_AP_VLAN:
4987 break;
4988 default:
4989 WARN_ON(1);
4990 return;
4991 }
4992
4993 if (tid != sta->reserved_tid) {
4994 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
4995 return;
4996 }
4997
4998 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
4999}
5000EXPORT_SYMBOL(ieee80211_unreserve_tid);
5001
Johannes Berg55de9082012-07-26 17:24:39 +02005002void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
5003 struct sk_buff *skb, int tid,
Johannes Bergb9771d42018-05-28 15:47:41 +02005004 enum nl80211_band band, u32 txdata_flags)
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005005{
Amadeusz Sławiński731977e2017-01-24 16:42:10 +01005006 int ac = ieee80211_ac_from_tid(tid);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02005007
Zhang Shengjud57a5442016-03-03 01:16:56 +00005008 skb_reset_mac_header(skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02005009 skb_set_queue_mapping(skb, ac);
Helmut Schaacf6bb792011-12-15 10:18:34 +01005010 skb->priority = tid;
Johannes Bergcf0277e2010-01-05 18:00:58 +01005011
Johannes Berg89afe612013-02-13 15:39:57 +01005012 skb->dev = sdata->dev;
5013
Johannes Berg3d34deb2009-06-18 17:25:11 +02005014 /*
5015 * The other path calling ieee80211_xmit is from the tasklet,
5016 * and while we can handle concurrent transmissions locking
5017 * requirements are that we do not come into tx with bhs on.
5018 */
5019 local_bh_disable();
Johannes Berg73c4e192014-11-09 18:50:09 +02005020 IEEE80211_SKB_CB(skb)->band = band;
Johannes Bergb9771d42018-05-28 15:47:41 +02005021 ieee80211_xmit(sdata, NULL, skb, txdata_flags);
Johannes Berg3d34deb2009-06-18 17:25:11 +02005022 local_bh_enable();
Johannes Berg3b8d81e02009-06-17 17:43:56 +02005023}
Denis Kenzior91180642018-03-26 12:52:50 -05005024
5025int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
5026 const u8 *buf, size_t len,
5027 const u8 *dest, __be16 proto, bool unencrypted)
5028{
5029 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5030 struct ieee80211_local *local = sdata->local;
5031 struct sk_buff *skb;
5032 struct ethhdr *ehdr;
5033 u32 flags;
5034
5035 /* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
5036 * or Pre-Authentication
5037 */
5038 if (proto != sdata->control_port_protocol &&
5039 proto != cpu_to_be16(ETH_P_PREAUTH))
5040 return -EINVAL;
5041
5042 if (unencrypted)
5043 flags = IEEE80211_TX_INTFL_DONT_ENCRYPT;
5044 else
5045 flags = 0;
5046
5047 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5048 sizeof(struct ethhdr) + len);
5049 if (!skb)
5050 return -ENOMEM;
5051
5052 skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
5053
5054 skb_put_data(skb, buf, len);
5055
5056 ehdr = skb_push(skb, sizeof(struct ethhdr));
5057 memcpy(ehdr->h_dest, dest, ETH_ALEN);
5058 memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
5059 ehdr->h_proto = proto;
5060
5061 skb->dev = dev;
5062 skb->protocol = htons(ETH_P_802_3);
5063 skb_reset_network_header(skb);
5064 skb_reset_mac_header(skb);
5065
Denis Kenziore7441c92018-06-19 10:39:50 -05005066 local_bh_disable();
Denis Kenzior91180642018-03-26 12:52:50 -05005067 __ieee80211_subif_start_xmit(skb, skb->dev, flags);
Denis Kenziore7441c92018-06-19 10:39:50 -05005068 local_bh_enable();
Denis Kenzior91180642018-03-26 12:52:50 -05005069
5070 return 0;
5071}