blob: 25ba24bef8f51f669ccbcc95cfcb5f1d235e39a9 [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
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100442 /* no stations in PS mode */
Marco Porschd012a602012-10-10 12:39:50 -0700443 if (!atomic_read(&ps->num_sta_ps))
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 Berg0832b602018-07-03 14:47:25 +02001256 if (!ieee80211_is_data_present(hdr->frame_control))
Michal Kazior80a83cf2016-05-19 10:37:48 +02001257 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001258
Michal Kaziordbef5362017-01-13 13:32:51 +01001259 if (sta) {
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001260 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1261
Michal Kaziordbef5362017-01-13 13:32:51 +01001262 if (!sta->uploaded)
1263 return NULL;
1264
1265 txq = sta->sta.txq[tid];
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001266 } else if (vif) {
1267 txq = vif->txq;
1268 }
1269
1270 if (!txq)
Michal Kazior80a83cf2016-05-19 10:37:48 +02001271 return NULL;
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001272
Michal Kazior80a83cf2016-05-19 10:37:48 +02001273 return to_txq_info(txq);
1274}
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001275
Michal Kazior5caa3282016-05-19 10:37:51 +02001276static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
1277{
1278 IEEE80211_SKB_CB(skb)->control.enqueue_time = codel_get_time();
1279}
1280
Michal Kazior5caa3282016-05-19 10:37:51 +02001281static u32 codel_skb_len_func(const struct sk_buff *skb)
1282{
1283 return skb->len;
1284}
1285
1286static codel_time_t codel_skb_time_func(const struct sk_buff *skb)
1287{
1288 const struct ieee80211_tx_info *info;
1289
1290 info = (const struct ieee80211_tx_info *)skb->cb;
1291 return info->control.enqueue_time;
1292}
1293
1294static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars,
1295 void *ctx)
1296{
1297 struct ieee80211_local *local;
1298 struct txq_info *txqi;
1299 struct fq *fq;
1300 struct fq_flow *flow;
1301
1302 txqi = ctx;
1303 local = vif_to_sdata(txqi->txq.vif)->local;
1304 fq = &local->fq;
1305
1306 if (cvars == &txqi->def_cvars)
1307 flow = &txqi->def_flow;
1308 else
1309 flow = &fq->flows[cvars - local->cvars];
1310
1311 return fq_flow_dequeue(fq, flow);
1312}
1313
1314static void codel_drop_func(struct sk_buff *skb,
1315 void *ctx)
1316{
1317 struct ieee80211_local *local;
1318 struct ieee80211_hw *hw;
1319 struct txq_info *txqi;
1320
1321 txqi = ctx;
1322 local = vif_to_sdata(txqi->txq.vif)->local;
1323 hw = &local->hw;
1324
1325 ieee80211_free_txskb(hw, skb);
1326}
1327
Michal Kaziorfa962b92016-05-19 10:37:49 +02001328static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
1329 struct fq_tin *tin,
1330 struct fq_flow *flow)
1331{
Michal Kazior5caa3282016-05-19 10:37:51 +02001332 struct ieee80211_local *local;
1333 struct txq_info *txqi;
1334 struct codel_vars *cvars;
1335 struct codel_params *cparams;
1336 struct codel_stats *cstats;
1337
1338 local = container_of(fq, struct ieee80211_local, fq);
1339 txqi = container_of(tin, struct txq_info, tin);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001340 cstats = &txqi->cstats;
Michal Kazior5caa3282016-05-19 10:37:51 +02001341
Toke Høiland-Jørgensen484a54c2017-04-06 11:38:26 +02001342 if (txqi->txq.sta) {
1343 struct sta_info *sta = container_of(txqi->txq.sta,
1344 struct sta_info, sta);
1345 cparams = &sta->cparams;
1346 } else {
1347 cparams = &local->cparams;
1348 }
1349
Michal Kazior5caa3282016-05-19 10:37:51 +02001350 if (flow == &txqi->def_flow)
1351 cvars = &txqi->def_cvars;
1352 else
1353 cvars = &local->cvars[flow - fq->flows];
1354
1355 return codel_dequeue(txqi,
1356 &flow->backlog,
1357 cparams,
1358 cvars,
1359 cstats,
1360 codel_skb_len_func,
1361 codel_skb_time_func,
1362 codel_drop_func,
1363 codel_dequeue_func);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001364}
1365
1366static void fq_skb_free_func(struct fq *fq,
1367 struct fq_tin *tin,
1368 struct fq_flow *flow,
1369 struct sk_buff *skb)
1370{
1371 struct ieee80211_local *local;
1372
1373 local = container_of(fq, struct ieee80211_local, fq);
1374 ieee80211_free_txskb(&local->hw, skb);
1375}
1376
1377static struct fq_flow *fq_flow_get_default_func(struct fq *fq,
1378 struct fq_tin *tin,
1379 int idx,
1380 struct sk_buff *skb)
1381{
1382 struct txq_info *txqi;
1383
1384 txqi = container_of(tin, struct txq_info, tin);
1385 return &txqi->def_flow;
1386}
1387
Michal Kazior80a83cf2016-05-19 10:37:48 +02001388static void ieee80211_txq_enqueue(struct ieee80211_local *local,
1389 struct txq_info *txqi,
1390 struct sk_buff *skb)
1391{
Michal Kaziorfa962b92016-05-19 10:37:49 +02001392 struct fq *fq = &local->fq;
1393 struct fq_tin *tin = &txqi->tin;
Michal Kazior80a83cf2016-05-19 10:37:48 +02001394
Michal Kazior5caa3282016-05-19 10:37:51 +02001395 ieee80211_set_skb_enqueue_time(skb);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001396 fq_tin_enqueue(fq, tin, skb,
1397 fq_skb_free_func,
1398 fq_flow_get_default_func);
1399}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001400
Johannes Berg2a9e2572017-10-06 11:53:33 +02001401static bool fq_vlan_filter_func(struct fq *fq, struct fq_tin *tin,
1402 struct fq_flow *flow, struct sk_buff *skb,
1403 void *data)
1404{
1405 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1406
1407 return info->control.vif == data;
1408}
1409
1410void ieee80211_txq_remove_vlan(struct ieee80211_local *local,
1411 struct ieee80211_sub_if_data *sdata)
1412{
1413 struct fq *fq = &local->fq;
1414 struct txq_info *txqi;
1415 struct fq_tin *tin;
1416 struct ieee80211_sub_if_data *ap;
1417
1418 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1419 return;
1420
1421 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1422
1423 if (!ap->vif.txq)
1424 return;
1425
1426 txqi = to_txq_info(ap->vif.txq);
1427 tin = &txqi->tin;
1428
1429 spin_lock_bh(&fq->lock);
1430 fq_tin_filter(fq, tin, fq_vlan_filter_func, &sdata->vif,
1431 fq_skb_free_func);
1432 spin_unlock_bh(&fq->lock);
1433}
1434
Michal Kaziorfa962b92016-05-19 10:37:49 +02001435void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
1436 struct sta_info *sta,
1437 struct txq_info *txqi, int tid)
1438{
1439 fq_tin_init(&txqi->tin);
1440 fq_flow_init(&txqi->def_flow);
Michal Kazior5caa3282016-05-19 10:37:51 +02001441 codel_vars_init(&txqi->def_cvars);
Toke Høiland-Jørgensen8d51dbb2016-09-12 15:55:43 +02001442 codel_stats_init(&txqi->cstats);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001443 __skb_queue_head_init(&txqi->frags);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001444
1445 txqi->txq.vif = &sdata->vif;
1446
1447 if (sta) {
1448 txqi->txq.sta = &sta->sta;
1449 sta->sta.txq[tid] = &txqi->txq;
1450 txqi->txq.tid = tid;
Amadeusz Sławiński731977e2017-01-24 16:42:10 +01001451 txqi->txq.ac = ieee80211_ac_from_tid(tid);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001452 } else {
1453 sdata->vif.txq = &txqi->txq;
1454 txqi->txq.tid = 0;
1455 txqi->txq.ac = IEEE80211_AC_BE;
Michal Kazior80a83cf2016-05-19 10:37:48 +02001456 }
Michal Kaziorfa962b92016-05-19 10:37:49 +02001457}
Michal Kazior80a83cf2016-05-19 10:37:48 +02001458
Michal Kaziorfa962b92016-05-19 10:37:49 +02001459void ieee80211_txq_purge(struct ieee80211_local *local,
1460 struct txq_info *txqi)
1461{
1462 struct fq *fq = &local->fq;
1463 struct fq_tin *tin = &txqi->tin;
1464
1465 fq_tin_reset(fq, tin, fq_skb_free_func);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001466 ieee80211_purge_tx_queue(&local->hw, &txqi->frags);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001467}
1468
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001469void ieee80211_txq_set_params(struct ieee80211_local *local)
1470{
1471 if (local->hw.wiphy->txq_limit)
1472 local->fq.limit = local->hw.wiphy->txq_limit;
1473 else
1474 local->hw.wiphy->txq_limit = local->fq.limit;
1475
1476 if (local->hw.wiphy->txq_memory_limit)
1477 local->fq.memory_limit = local->hw.wiphy->txq_memory_limit;
1478 else
1479 local->hw.wiphy->txq_memory_limit = local->fq.memory_limit;
1480
1481 if (local->hw.wiphy->txq_quantum)
1482 local->fq.quantum = local->hw.wiphy->txq_quantum;
1483 else
1484 local->hw.wiphy->txq_quantum = local->fq.quantum;
1485}
1486
Michal Kaziorfa962b92016-05-19 10:37:49 +02001487int ieee80211_txq_setup_flows(struct ieee80211_local *local)
1488{
1489 struct fq *fq = &local->fq;
1490 int ret;
Michal Kazior5caa3282016-05-19 10:37:51 +02001491 int i;
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001492 bool supp_vht = false;
1493 enum nl80211_band band;
Michal Kaziorfa962b92016-05-19 10:37:49 +02001494
1495 if (!local->ops->wake_tx_queue)
1496 return 0;
1497
1498 ret = fq_init(fq, 4096);
1499 if (ret)
1500 return ret;
1501
Toke Høiland-Jørgensen3ff23cd2016-09-23 21:59:11 +02001502 /*
1503 * If the hardware doesn't support VHT, it is safe to limit the maximum
1504 * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
1505 */
1506 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1507 struct ieee80211_supported_band *sband;
1508
1509 sband = local->hw.wiphy->bands[band];
1510 if (!sband)
1511 continue;
1512
1513 supp_vht = supp_vht || sband->vht_cap.vht_supported;
1514 }
1515
1516 if (!supp_vht)
1517 fq->memory_limit = 4 << 20; /* 4 Mbytes */
1518
Michal Kazior5caa3282016-05-19 10:37:51 +02001519 codel_params_init(&local->cparams);
Michal Kazior5caa3282016-05-19 10:37:51 +02001520 local->cparams.interval = MS2TIME(100);
1521 local->cparams.target = MS2TIME(20);
1522 local->cparams.ecn = true;
1523
1524 local->cvars = kcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
1525 GFP_KERNEL);
1526 if (!local->cvars) {
Michal Kazior59a7c822016-06-29 14:00:34 +02001527 spin_lock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001528 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001529 spin_unlock_bh(&fq->lock);
Michal Kazior5caa3282016-05-19 10:37:51 +02001530 return -ENOMEM;
1531 }
1532
1533 for (i = 0; i < fq->flows_cnt; i++)
1534 codel_vars_init(&local->cvars[i]);
1535
Toke Høiland-Jørgensen2fe4a292018-05-08 13:03:50 +02001536 ieee80211_txq_set_params(local);
1537
Michal Kaziorfa962b92016-05-19 10:37:49 +02001538 return 0;
1539}
1540
1541void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1542{
1543 struct fq *fq = &local->fq;
1544
1545 if (!local->ops->wake_tx_queue)
1546 return;
1547
Michal Kazior5caa3282016-05-19 10:37:51 +02001548 kfree(local->cvars);
1549 local->cvars = NULL;
1550
Michal Kazior59a7c822016-06-29 14:00:34 +02001551 spin_lock_bh(&fq->lock);
Michal Kaziorfa962b92016-05-19 10:37:49 +02001552 fq_reset(fq, fq_skb_free_func);
Michal Kazior59a7c822016-06-29 14:00:34 +02001553 spin_unlock_bh(&fq->lock);
Felix Fietkauba8c3d62015-03-27 21:30:37 +01001554}
1555
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001556static bool ieee80211_queue_skb(struct ieee80211_local *local,
1557 struct ieee80211_sub_if_data *sdata,
1558 struct sta_info *sta,
1559 struct sk_buff *skb)
1560{
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001561 struct fq *fq = &local->fq;
1562 struct ieee80211_vif *vif;
1563 struct txq_info *txqi;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001564
1565 if (!local->ops->wake_tx_queue ||
1566 sdata->vif.type == NL80211_IFTYPE_MONITOR)
1567 return false;
1568
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001569 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1570 sdata = container_of(sdata->bss,
1571 struct ieee80211_sub_if_data, u.ap);
1572
1573 vif = &sdata->vif;
Michal Kaziordbef5362017-01-13 13:32:51 +01001574 txqi = ieee80211_get_txq(local, vif, sta, skb);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001575
1576 if (!txqi)
1577 return false;
1578
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001579 spin_lock_bh(&fq->lock);
1580 ieee80211_txq_enqueue(local, txqi, skb);
1581 spin_unlock_bh(&fq->lock);
1582
Johannes Berge7881bd52017-12-19 10:11:54 +01001583 drv_wake_tx_queue(local, txqi);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001584
1585 return true;
1586}
1587
Johannes Berg11127e92011-11-16 16:02:47 +01001588static bool ieee80211_tx_frags(struct ieee80211_local *local,
1589 struct ieee80211_vif *vif,
1590 struct ieee80211_sta *sta,
1591 struct sk_buff_head *skbs,
1592 bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001593{
Michal Kazior80a83cf2016-05-19 10:37:48 +02001594 struct ieee80211_tx_control control = {};
Johannes Berg252b86c2011-11-16 15:28:55 +01001595 struct sk_buff *skb, *tmp;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001596 unsigned long flags;
Johannes Berge2ebc742007-07-27 15:43:22 +02001597
Johannes Berg252b86c2011-11-16 15:28:55 +01001598 skb_queue_walk_safe(skbs, skb, tmp) {
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001599 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1600 int q = info->hw_queue;
1601
1602#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1603 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1604 __skb_unlink(skb, skbs);
Felix Fietkauc3e77242012-10-08 14:39:33 +02001605 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001606 continue;
1607 }
1608#endif
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001609
1610 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001611 if (local->queue_stop_reasons[q] ||
Johannes Berg7bb45682011-02-24 14:42:06 +01001612 (!txpending && !skb_queue_empty(&local->pending[q]))) {
Seth Forshee6c17b772013-02-11 11:21:07 -06001613 if (unlikely(info->flags &
Seth Forsheea7679ed2013-02-25 14:58:05 -06001614 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1615 if (local->queue_stop_reasons[q] &
1616 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1617 /*
1618 * Drop off-channel frames if queues
1619 * are stopped for any reason other
1620 * than off-channel operation. Never
1621 * queue them.
1622 */
1623 spin_unlock_irqrestore(
1624 &local->queue_stop_reason_lock,
1625 flags);
1626 ieee80211_purge_tx_queue(&local->hw,
1627 skbs);
1628 return true;
1629 }
1630 } else {
1631
Seth Forshee6c17b772013-02-11 11:21:07 -06001632 /*
Seth Forsheea7679ed2013-02-25 14:58:05 -06001633 * Since queue is stopped, queue up frames for
1634 * later transmission from the tx-pending
1635 * tasklet when the queue is woken again.
Seth Forshee6c17b772013-02-11 11:21:07 -06001636 */
Seth Forsheea7679ed2013-02-25 14:58:05 -06001637 if (txpending)
1638 skb_queue_splice_init(skbs,
1639 &local->pending[q]);
1640 else
1641 skb_queue_splice_tail_init(skbs,
1642 &local->pending[q]);
1643
1644 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1645 flags);
1646 return false;
Seth Forshee6c17b772013-02-11 11:21:07 -06001647 }
Johannes Berg7bb45682011-02-24 14:42:06 +01001648 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001649 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Tomas Winklerf8e79dd2008-07-24 18:46:44 +03001650
Johannes Berg11127e92011-11-16 16:02:47 +01001651 info->control.vif = vif;
Michal Kazior80a83cf2016-05-19 10:37:48 +02001652 control.sta = sta;
Johannes Bergec25acc2010-07-22 17:11:28 +02001653
Johannes Berg252b86c2011-11-16 15:28:55 +01001654 __skb_unlink(skb, skbs);
Michal Kazior80a83cf2016-05-19 10:37:48 +02001655 drv_tx(local, &control, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001656 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001657
Johannes Berg11127e92011-11-16 16:02:47 +01001658 return true;
1659}
1660
1661/*
1662 * Returns false if the frame couldn't be transmitted but was queued instead.
1663 */
1664static bool __ieee80211_tx(struct ieee80211_local *local,
1665 struct sk_buff_head *skbs, int led_len,
1666 struct sta_info *sta, bool txpending)
1667{
1668 struct ieee80211_tx_info *info;
1669 struct ieee80211_sub_if_data *sdata;
1670 struct ieee80211_vif *vif;
1671 struct ieee80211_sta *pubsta;
1672 struct sk_buff *skb;
1673 bool result = true;
1674 __le16 fc;
1675
1676 if (WARN_ON(skb_queue_empty(skbs)))
1677 return true;
1678
1679 skb = skb_peek(skbs);
1680 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1681 info = IEEE80211_SKB_CB(skb);
1682 sdata = vif_to_sdata(info->control.vif);
1683 if (sta && !sta->uploaded)
1684 sta = NULL;
1685
1686 if (sta)
1687 pubsta = &sta->sta;
1688 else
1689 pubsta = NULL;
1690
1691 switch (sdata->vif.type) {
1692 case NL80211_IFTYPE_MONITOR:
Aviya Erenfeldd8212182016-08-29 23:25:15 +03001693 if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
Johannes Bergc82b5a72013-07-14 23:39:20 +03001694 vif = &sdata->vif;
1695 break;
1696 }
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001697 sdata = rcu_dereference(local->monitor_sdata);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001698 if (sdata) {
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001699 vif = &sdata->vif;
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001700 info->hw_queue =
1701 vif->hw_queue[skb_get_queue_mapping(skb)];
Johannes Berg30686bf2015-06-02 21:39:54 +02001702 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
Johannes Berg63b4d8b2015-11-24 15:41:50 +01001703 ieee80211_purge_tx_queue(&local->hw, skbs);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001704 return true;
1705 } else
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001706 vif = NULL;
Johannes Berg11127e92011-11-16 16:02:47 +01001707 break;
1708 case NL80211_IFTYPE_AP_VLAN:
1709 sdata = container_of(sdata->bss,
1710 struct ieee80211_sub_if_data, u.ap);
1711 /* fall through */
1712 default:
1713 vif = &sdata->vif;
1714 break;
1715 }
1716
Johannes Bergcb831b52012-07-02 15:40:18 +02001717 result = ieee80211_tx_frags(local, vif, pubsta, skbs,
1718 txpending);
Johannes Berg11127e92011-11-16 16:02:47 +01001719
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001720 ieee80211_tpt_led_trig_tx(local, fc, led_len);
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001721
Johannes Berg4db4e0a2011-11-24 14:47:36 +01001722 WARN_ON_ONCE(!skb_queue_empty(skbs));
Johannes Berg252b86c2011-11-16 15:28:55 +01001723
Johannes Berg11127e92011-11-16 16:02:47 +01001724 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001725}
1726
Johannes Berg97b045d2008-06-20 01:22:30 +02001727/*
1728 * Invoke TX handlers, return 0 on success and non-zero if the
1729 * frame was dropped or queued.
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001730 *
1731 * The handlers are split into an early and late part. The latter is everything
1732 * that can be sensitive to reordering, and will be deferred to after packets
1733 * are dequeued from the intermediate queues (when they are enabled).
Johannes Berg97b045d2008-06-20 01:22:30 +02001734 */
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001735static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx)
Johannes Berg97b045d2008-06-20 01:22:30 +02001736{
Johannes Berg97b045d2008-06-20 01:22:30 +02001737 ieee80211_tx_result res = TX_DROP;
Johannes Berg97b045d2008-06-20 01:22:30 +02001738
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001739#define CALL_TXH(txh) \
1740 do { \
1741 res = txh(tx); \
1742 if (res != TX_CONTINUE) \
1743 goto txh_done; \
1744 } while (0)
Johannes Berg97b045d2008-06-20 01:22:30 +02001745
Kalle Valo5c1b98a2010-01-12 10:42:46 +02001746 CALL_TXH(ieee80211_tx_h_dynamic_ps);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001747 CALL_TXH(ieee80211_tx_h_check_assoc);
1748 CALL_TXH(ieee80211_tx_h_ps_buf);
Johannes Berga621fa42010-08-27 14:26:54 +03001749 CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001750 CALL_TXH(ieee80211_tx_h_select_key);
Johannes Berg30686bf2015-06-02 21:39:54 +02001751 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
Johannes Bergaf65cd962009-11-17 18:18:36 +01001752 CALL_TXH(ieee80211_tx_h_rate_ctrl);
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001753
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001754 txh_done:
1755 if (unlikely(res == TX_DROP)) {
1756 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1757 if (tx->skb)
1758 ieee80211_free_txskb(&tx->local->hw, tx->skb);
1759 else
1760 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1761 return -1;
1762 } else if (unlikely(res == TX_QUEUED)) {
1763 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1764 return -1;
1765 }
1766
1767 return 0;
1768}
1769
1770/*
1771 * Late handlers can be called while the sta lock is held. Handlers that can
1772 * cause packets to be generated will cause deadlock!
1773 */
1774static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx)
1775{
1776 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1777 ieee80211_tx_result res = TX_CONTINUE;
1778
Johannes Bergaa5b5492011-12-02 22:08:52 +01001779 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1780 __skb_queue_tail(&tx->skbs, tx->skb);
1781 tx->skb = NULL;
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001782 goto txh_done;
Johannes Bergaa5b5492011-12-02 22:08:52 +01001783 }
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001784
1785 CALL_TXH(ieee80211_tx_h_michael_mic_add);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001786 CALL_TXH(ieee80211_tx_h_sequence);
1787 CALL_TXH(ieee80211_tx_h_fragment);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001788 /* handlers after fragment must be aware of tx info fragmentation! */
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001789 CALL_TXH(ieee80211_tx_h_stats);
1790 CALL_TXH(ieee80211_tx_h_encrypt);
Johannes Berg30686bf2015-06-02 21:39:54 +02001791 if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
Arik Nemtsov8fd369e2011-01-31 22:29:12 +02001792 CALL_TXH(ieee80211_tx_h_calculate_duration);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001793#undef CALL_TXH
1794
1795 txh_done:
Johannes Berg97b045d2008-06-20 01:22:30 +02001796 if (unlikely(res == TX_DROP)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001797 I802_DEBUG_INC(tx->local->tx_handlers_drop);
Johannes Berg252b86c2011-11-16 15:28:55 +01001798 if (tx->skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001799 ieee80211_free_txskb(&tx->local->hw, tx->skb);
Johannes Berg252b86c2011-11-16 15:28:55 +01001800 else
Felix Fietkau1f98ab72012-11-10 03:44:14 +01001801 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
Johannes Berg97b045d2008-06-20 01:22:30 +02001802 return -1;
1803 } else if (unlikely(res == TX_QUEUED)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001804 I802_DEBUG_INC(tx->local->tx_handlers_queued);
Johannes Berg97b045d2008-06-20 01:22:30 +02001805 return -1;
1806 }
1807
1808 return 0;
1809}
1810
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001811static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1812{
1813 int r = invoke_tx_handlers_early(tx);
1814
1815 if (r)
1816 return r;
1817 return invoke_tx_handlers_late(tx);
1818}
1819
Felix Fietkau06be6b12013-10-14 18:01:00 +02001820bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1821 struct ieee80211_vif *vif, struct sk_buff *skb,
1822 int band, struct ieee80211_sta **sta)
1823{
1824 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1825 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1826 struct ieee80211_tx_data tx;
Johannes Berg88724a82015-03-01 09:10:12 +02001827 struct sk_buff *skb2;
Felix Fietkau06be6b12013-10-14 18:01:00 +02001828
Johannes Berg7c107702015-03-20 14:18:27 +01001829 if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
Felix Fietkau06be6b12013-10-14 18:01:00 +02001830 return false;
1831
1832 info->band = band;
1833 info->control.vif = vif;
1834 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1835
1836 if (invoke_tx_handlers(&tx))
1837 return false;
1838
1839 if (sta) {
1840 if (tx.sta)
1841 *sta = &tx.sta->sta;
1842 else
1843 *sta = NULL;
1844 }
1845
Johannes Berg88724a82015-03-01 09:10:12 +02001846 /* this function isn't suitable for fragmented data frames */
1847 skb2 = __skb_dequeue(&tx.skbs);
1848 if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1849 ieee80211_free_txskb(hw, skb2);
1850 ieee80211_purge_tx_queue(hw, &tx.skbs);
1851 return false;
1852 }
1853
Felix Fietkau06be6b12013-10-14 18:01:00 +02001854 return true;
1855}
1856EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1857
Johannes Berg7bb45682011-02-24 14:42:06 +01001858/*
1859 * Returns false if the frame couldn't be transmitted but was queued instead.
1860 */
1861static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01001862 struct sta_info *sta, struct sk_buff *skb,
Johannes Bergb9771d42018-05-28 15:47:41 +02001863 bool txpending, u32 txdata_flags)
Johannes Berge2ebc742007-07-27 15:43:22 +02001864{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001865 struct ieee80211_local *local = sdata->local;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001866 struct ieee80211_tx_data tx;
Johannes Berg97b045d2008-06-20 01:22:30 +02001867 ieee80211_tx_result res_prepare;
Johannes Berge039fa42008-05-15 12:55:29 +02001868 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001869 bool result = true;
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001870 int led_len;
Johannes Berge2ebc742007-07-27 15:43:22 +02001871
Johannes Berge2ebc742007-07-27 15:43:22 +02001872 if (unlikely(skb->len < 10)) {
1873 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001874 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001875 }
1876
Johannes Berg58d41852007-09-26 17:53:18 +02001877 /* initialises tx */
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001878 led_len = skb->len;
Johannes Berg7c107702015-03-20 14:18:27 +01001879 res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001880
Johannes Bergb9771d42018-05-28 15:47:41 +02001881 tx.flags |= txdata_flags;
1882
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001883 if (unlikely(res_prepare == TX_DROP)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02001884 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001885 return true;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001886 } else if (unlikely(res_prepare == TX_QUEUED)) {
Johannes Berg55de9082012-07-26 17:24:39 +02001887 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001888 }
1889
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001890 /* set up hw_queue value early */
1891 if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
Johannes Berg30686bf2015-06-02 21:39:54 +02001892 !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001893 info->hw_queue =
1894 sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1895
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001896 if (invoke_tx_handlers_early(&tx))
Bob Copeland6eae4a62018-09-05 06:22:59 -04001897 return true;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02001898
1899 if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb))
1900 return true;
1901
1902 if (!invoke_tx_handlers_late(&tx))
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001903 result = __ieee80211_tx(local, &tx.skbs, led_len,
1904 tx.sta, txpending);
Johannes Berg55de9082012-07-26 17:24:39 +02001905
Johannes Berg7bb45682011-02-24 14:42:06 +01001906 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001907}
1908
1909/* device xmit handlers */
1910
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301911static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
Johannes Berg23c07522008-05-29 10:38:53 +02001912 struct sk_buff *skb,
1913 int head_need, bool may_encrypt)
1914{
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301915 struct ieee80211_local *local = sdata->local;
Johannes Berg23c07522008-05-29 10:38:53 +02001916 int tail_need = 0;
1917
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301918 if (may_encrypt && sdata->crypto_tx_tailroom_needed_cnt) {
Johannes Berg23c07522008-05-29 10:38:53 +02001919 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1920 tail_need -= skb_tailroom(skb);
1921 tail_need = max_t(int, tail_need, 0);
1922 }
1923
Ido Yarivc70f59a2014-07-29 15:39:14 +03001924 if (skb_cloned(skb) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001925 (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
Ido Yarivc70f59a2014-07-29 15:39:14 +03001926 !skb_clone_writable(skb, ETH_HLEN) ||
Johannes Berg17c18bf2015-03-21 15:25:43 +01001927 (may_encrypt && sdata->crypto_tx_tailroom_needed_cnt)))
Johannes Berg23c07522008-05-29 10:38:53 +02001928 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001929 else if (head_need || tail_need)
Johannes Berg23c07522008-05-29 10:38:53 +02001930 I802_DEBUG_INC(local->tx_expand_skb_head);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001931 else
1932 return 0;
Johannes Berg23c07522008-05-29 10:38:53 +02001933
1934 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
Joe Perches0fb9a9e2010-08-20 16:25:38 -07001935 wiphy_debug(local->hw.wiphy,
1936 "failed to reallocate TX buffer\n");
Johannes Berg23c07522008-05-29 10:38:53 +02001937 return -ENOMEM;
1938 }
1939
Johannes Berg23c07522008-05-29 10:38:53 +02001940 return 0;
1941}
1942
Johannes Berg7c107702015-03-20 14:18:27 +01001943void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
Johannes Bergb9771d42018-05-28 15:47:41 +02001944 struct sta_info *sta, struct sk_buff *skb,
1945 u32 txdata_flags)
Johannes Berge2ebc742007-07-27 15:43:22 +02001946{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001947 struct ieee80211_local *local = sdata->local;
Johannes Berge039fa42008-05-15 12:55:29 +02001948 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Colin Ian King11b05ba2018-02-02 16:31:42 +00001949 struct ieee80211_hdr *hdr;
Johannes Berge2ebc742007-07-27 15:43:22 +02001950 int headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001951 bool may_encrypt;
Johannes Berge2ebc742007-07-27 15:43:22 +02001952
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001953 may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
Johannes Berg23c07522008-05-29 10:38:53 +02001954
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001955 headroom = local->tx_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001956 if (may_encrypt)
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001957 headroom += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001958 headroom -= skb_headroom(skb);
1959 headroom = max_t(int, 0, headroom);
1960
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301961 if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02001962 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001963 return;
Johannes Berge2ebc742007-07-27 15:43:22 +02001964 }
1965
Steve deRosier740c1aa2010-09-11 20:01:31 -07001966 hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berg5061b0c2009-07-14 00:33:34 +02001967 info->control.vif = &sdata->vif;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001968
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001969 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1970 if (ieee80211_is_data(hdr->frame_control) &&
1971 is_unicast_ether_addr(hdr->addr1)) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01001972 if (mesh_nexthop_resolve(sdata, skb))
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001973 return; /* skb queued: don't free */
1974 } else {
1975 ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
1976 }
Johannes Berg98aed9f2012-03-27 14:18:36 +02001977 }
Javier Cardonacca89492009-08-10 17:29:29 -07001978
Javier Cardona2154c81c2011-09-07 17:49:53 -07001979 ieee80211_set_qos_hdr(sdata, skb);
Johannes Bergb9771d42018-05-28 15:47:41 +02001980 ieee80211_tx(sdata, sta, skb, false, txdata_flags);
Johannes Berge2ebc742007-07-27 15:43:22 +02001981}
1982
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01001983static bool ieee80211_parse_tx_radiotap(struct ieee80211_local *local,
1984 struct sk_buff *skb)
Johannes Berg73b9f032011-10-07 14:01:26 +02001985{
1986 struct ieee80211_radiotap_iterator iterator;
1987 struct ieee80211_radiotap_header *rthdr =
1988 (struct ieee80211_radiotap_header *) skb->data;
1989 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01001990 struct ieee80211_supported_band *sband =
1991 local->hw.wiphy->bands[info->band];
Johannes Berg73b9f032011-10-07 14:01:26 +02001992 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
1993 NULL);
1994 u16 txflags;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01001995 u16 rate = 0;
1996 bool rate_found = false;
1997 u8 rate_retries = 0;
1998 u16 rate_flags = 0;
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01001999 u8 mcs_known, mcs_flags, mcs_bw;
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002000 u16 vht_known;
2001 u8 vht_mcs = 0, vht_nss = 0;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002002 int i;
Johannes Berg73b9f032011-10-07 14:01:26 +02002003
2004 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
2005 IEEE80211_TX_CTL_DONTFRAG;
2006
2007 /*
2008 * for every radiotap entry that is present
2009 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
2010 * entries present, or -EINVAL on error)
2011 */
2012
2013 while (!ret) {
2014 ret = ieee80211_radiotap_iterator_next(&iterator);
2015
2016 if (ret)
2017 continue;
2018
2019 /* see if this argument is something we can use */
2020 switch (iterator.this_arg_index) {
2021 /*
2022 * You must take care when dereferencing iterator.this_arg
2023 * for multibyte types... the pointer is not aligned. Use
2024 * get_unaligned((type *)iterator.this_arg) to dereference
2025 * iterator.this_arg for type "type" safely on all arches.
2026 */
2027 case IEEE80211_RADIOTAP_FLAGS:
2028 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
2029 /*
2030 * this indicates that the skb we have been
2031 * handed has the 32-bit FCS CRC at the end...
2032 * we should react to that by snipping it off
2033 * because it will be recomputed and added
2034 * on transmission
2035 */
2036 if (skb->len < (iterator._max_length + FCS_LEN))
2037 return false;
2038
2039 skb_trim(skb, skb->len - FCS_LEN);
2040 }
2041 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
2042 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
2043 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
2044 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
2045 break;
2046
2047 case IEEE80211_RADIOTAP_TX_FLAGS:
2048 txflags = get_unaligned_le16(iterator.this_arg);
2049 if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
2050 info->flags |= IEEE80211_TX_CTL_NO_ACK;
2051 break;
2052
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002053 case IEEE80211_RADIOTAP_RATE:
2054 rate = *iterator.this_arg;
2055 rate_flags = 0;
2056 rate_found = true;
2057 break;
2058
2059 case IEEE80211_RADIOTAP_DATA_RETRIES:
2060 rate_retries = *iterator.this_arg;
2061 break;
2062
2063 case IEEE80211_RADIOTAP_MCS:
2064 mcs_known = iterator.this_arg[0];
2065 mcs_flags = iterator.this_arg[1];
2066 if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
2067 break;
2068
2069 rate_found = true;
2070 rate = iterator.this_arg[2];
2071 rate_flags = IEEE80211_TX_RC_MCS;
2072
2073 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
2074 mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
2075 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2076
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002077 mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002078 if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
Sven Eckelmannf66b60f2016-02-24 16:25:49 +01002079 mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002080 rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2081 break;
2082
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002083 case IEEE80211_RADIOTAP_VHT:
2084 vht_known = get_unaligned_le16(iterator.this_arg);
2085 rate_found = true;
2086
2087 rate_flags = IEEE80211_TX_RC_VHT_MCS;
2088 if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
2089 (iterator.this_arg[2] &
2090 IEEE80211_RADIOTAP_VHT_FLAG_SGI))
2091 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2092 if (vht_known &
2093 IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
2094 if (iterator.this_arg[3] == 1)
2095 rate_flags |=
2096 IEEE80211_TX_RC_40_MHZ_WIDTH;
2097 else if (iterator.this_arg[3] == 4)
2098 rate_flags |=
2099 IEEE80211_TX_RC_80_MHZ_WIDTH;
2100 else if (iterator.this_arg[3] == 11)
2101 rate_flags |=
2102 IEEE80211_TX_RC_160_MHZ_WIDTH;
2103 }
2104
2105 vht_mcs = iterator.this_arg[4] >> 4;
2106 vht_nss = iterator.this_arg[4] & 0xF;
2107 break;
2108
Johannes Berg73b9f032011-10-07 14:01:26 +02002109 /*
2110 * Please update the file
2111 * Documentation/networking/mac80211-injection.txt
2112 * when parsing new fields here.
2113 */
2114
2115 default:
2116 break;
2117 }
2118 }
2119
2120 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
2121 return false;
2122
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002123 if (rate_found) {
2124 info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
2125
2126 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2127 info->control.rates[i].idx = -1;
2128 info->control.rates[i].flags = 0;
2129 info->control.rates[i].count = 0;
2130 }
2131
2132 if (rate_flags & IEEE80211_TX_RC_MCS) {
2133 info->control.rates[0].idx = rate;
Lorenzo Bianconi646e76b2016-02-23 15:43:35 +01002134 } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
2135 ieee80211_rate_set_vht(info->control.rates, vht_mcs,
2136 vht_nss);
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002137 } else {
2138 for (i = 0; i < sband->n_bitrates; i++) {
2139 if (rate * 5 != sband->bitrates[i].bitrate)
2140 continue;
2141
2142 info->control.rates[0].idx = i;
2143 break;
2144 }
2145 }
2146
Felix Fietkau07310a62016-03-02 15:54:51 +01002147 if (info->control.rates[0].idx < 0)
2148 info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
2149
Sven Eckelmanndfdfc2b2016-01-26 17:11:13 +01002150 info->control.rates[0].flags = rate_flags;
2151 info->control.rates[0].count = min_t(u8, rate_retries + 1,
2152 local->hw.max_rate_tries);
2153 }
2154
Johannes Berg73b9f032011-10-07 14:01:26 +02002155 /*
2156 * remove the radiotap header
2157 * iterator->_max_length was sanity-checked against
2158 * skb->len by iterator init
2159 */
2160 skb_pull(skb, iterator._max_length);
2161
2162 return true;
2163}
2164
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +00002165netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
2166 struct net_device *dev)
Johannes Berge2ebc742007-07-27 15:43:22 +02002167{
2168 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg55de9082012-07-26 17:24:39 +02002169 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berge2ebc742007-07-27 15:43:22 +02002170 struct ieee80211_radiotap_header *prthdr =
2171 (struct ieee80211_radiotap_header *)skb->data;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002172 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002173 struct ieee80211_hdr *hdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002174 struct ieee80211_sub_if_data *tmp_sdata, *sdata;
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002175 struct cfg80211_chan_def *chandef;
Andy Green9b8a74e2007-07-27 15:43:24 +02002176 u16 len_rthdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002177 int hdrlen;
Johannes Berge2ebc742007-07-27 15:43:22 +02002178
Andy Green9b8a74e2007-07-27 15:43:24 +02002179 /* check for not even having the fixed radiotap header part */
2180 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2181 goto fail; /* too short to be possibly valid */
2182
2183 /* is it a header version we can trust to find length from? */
2184 if (unlikely(prthdr->it_version))
2185 goto fail; /* only version 0 is supported */
2186
2187 /* then there must be a radiotap header with a length we can use */
2188 len_rthdr = ieee80211_get_radiotap_len(skb->data);
2189
2190 /* does the skb contain enough to deliver on the alleged length? */
2191 if (unlikely(skb->len < len_rthdr))
2192 goto fail; /* skb too short for claimed rt header extent */
Johannes Berge2ebc742007-07-27 15:43:22 +02002193
Johannes Berge2ebc742007-07-27 15:43:22 +02002194 /*
2195 * fix up the pointers accounting for the radiotap
2196 * header still being in there. We are being given
2197 * a precooked IEEE80211 header so no need for
2198 * normal processing
2199 */
Andy Green9b8a74e2007-07-27 15:43:24 +02002200 skb_set_mac_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02002201 /*
Andy Green9b8a74e2007-07-27 15:43:24 +02002202 * these are just fixed to the end of the rt area since we
2203 * don't have any better information and at this point, nobody cares
Johannes Berge2ebc742007-07-27 15:43:22 +02002204 */
Andy Green9b8a74e2007-07-27 15:43:24 +02002205 skb_set_network_header(skb, len_rthdr);
2206 skb_set_transport_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02002207
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002208 if (skb->len < len_rthdr + 2)
2209 goto fail;
2210
2211 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2212 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2213
2214 if (skb->len < len_rthdr + hdrlen)
2215 goto fail;
2216
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002217 /*
2218 * Initialize skb->protocol if the injected frame is a data frame
2219 * carrying a rfc1042 header
2220 */
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002221 if (ieee80211_is_data(hdr->frame_control) &&
2222 skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2223 u8 *payload = (u8 *)hdr + hdrlen;
2224
Joe Perchesb203ca32012-05-08 18:56:52 +00002225 if (ether_addr_equal(payload, rfc1042_header))
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002226 skb->protocol = cpu_to_be16((payload[6] << 8) |
2227 payload[7]);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02002228 }
2229
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002230 memset(info, 0, sizeof(*info));
2231
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002232 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
Johannes Berg73b9f032011-10-07 14:01:26 +02002233 IEEE80211_TX_CTL_INJECTED;
2234
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002235 rcu_read_lock();
2236
2237 /*
2238 * We process outgoing injected frames that have a local address
2239 * we handle as though they are non-injected frames.
2240 * This code here isn't entirely correct, the local MAC address
2241 * isn't always enough to find the interface to use; for proper
2242 * VLAN/WDS support we will need a different mechanism (which
2243 * likely isn't going to be monitor interfaces).
2244 */
2245 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2246
2247 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2248 if (!ieee80211_sdata_running(tmp_sdata))
2249 continue;
2250 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2251 tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2252 tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
2253 continue;
Joe Perchesb203ca32012-05-08 18:56:52 +00002254 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002255 sdata = tmp_sdata;
2256 break;
2257 }
2258 }
Johannes Berg7351c6b2009-11-19 01:08:30 +01002259
Johannes Berg55de9082012-07-26 17:24:39 +02002260 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2261 if (!chanctx_conf) {
2262 tmp_sdata = rcu_dereference(local->monitor_sdata);
2263 if (tmp_sdata)
2264 chanctx_conf =
2265 rcu_dereference(tmp_sdata->vif.chanctx_conf);
2266 }
Johannes Berg55de9082012-07-26 17:24:39 +02002267
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002268 if (chanctx_conf)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002269 chandef = &chanctx_conf->def;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002270 else if (!local->use_chanctx)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002271 chandef = &local->_oper_chandef;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01002272 else
2273 goto fail_rcu;
Johannes Berg55de9082012-07-26 17:24:39 +02002274
2275 /*
2276 * Frame injection is not allowed if beaconing is not allowed
2277 * or if we need radar detection. Beaconing is usually not allowed when
2278 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2279 * Passive scan is also used in world regulatory domains where
2280 * your country is not known and as such it should be treated as
2281 * NO TX unless the channel is explicitly allowed in which case
2282 * your current regulatory domain would not have the passive scan
2283 * flag.
2284 *
2285 * Since AP mode uses monitor interfaces to inject/TX management
2286 * frames we can make AP mode the exception to this rule once it
2287 * supports radar detection as its implementation can deal with
2288 * radar detection by itself. We can do that later by adding a
2289 * monitor flag interfaces used for AP support.
2290 */
Janusz Dziedzicb4932832014-06-05 08:12:57 +02002291 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2292 sdata->vif.type))
Johannes Berg55de9082012-07-26 17:24:39 +02002293 goto fail_rcu;
2294
Johannes Berg73c4e192014-11-09 18:50:09 +02002295 info->band = chandef->chan->band;
Lorenzo Bianconi109843b02016-02-19 12:18:01 +01002296
2297 /* process and remove the injection radiotap header */
2298 if (!ieee80211_parse_tx_radiotap(local, skb))
2299 goto fail_rcu;
2300
Johannes Bergb9771d42018-05-28 15:47:41 +02002301 ieee80211_xmit(sdata, NULL, skb, 0);
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02002302 rcu_read_unlock();
2303
Johannes Berge2ebc742007-07-27 15:43:22 +02002304 return NETDEV_TX_OK;
Andy Green9b8a74e2007-07-27 15:43:24 +02002305
Johannes Berg55de9082012-07-26 17:24:39 +02002306fail_rcu:
2307 rcu_read_unlock();
Andy Green9b8a74e2007-07-27 15:43:24 +02002308fail:
2309 dev_kfree_skb(skb);
2310 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
Johannes Berge2ebc742007-07-27 15:43:22 +02002311}
2312
Johannes Berg97ffe752015-03-21 09:13:45 +01002313static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002314{
Johannes Berg97ffe752015-03-21 09:13:45 +01002315 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002316
Johannes Berg97ffe752015-03-21 09:13:45 +01002317 return ethertype == ETH_P_TDLS &&
2318 skb->len > 14 &&
2319 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2320}
2321
2322static int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2323 struct sk_buff *skb,
2324 struct sta_info **sta_out)
2325{
2326 struct sta_info *sta;
2327
2328 switch (sdata->vif.type) {
2329 case NL80211_IFTYPE_AP_VLAN:
2330 sta = rcu_dereference(sdata->u.vlan.sta);
2331 if (sta) {
2332 *sta_out = sta;
2333 return 0;
2334 } else if (sdata->wdev.use_4addr) {
2335 return -ENOLINK;
2336 }
2337 /* fall through */
2338 case NL80211_IFTYPE_AP:
2339 case NL80211_IFTYPE_OCB:
2340 case NL80211_IFTYPE_ADHOC:
2341 if (is_multicast_ether_addr(skb->data)) {
2342 *sta_out = ERR_PTR(-ENOENT);
2343 return 0;
2344 }
2345 sta = sta_info_get_bss(sdata, skb->data);
2346 break;
2347 case NL80211_IFTYPE_WDS:
2348 sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
2349 break;
2350#ifdef CONFIG_MAC80211_MESH
2351 case NL80211_IFTYPE_MESH_POINT:
2352 /* determined much later */
2353 *sta_out = NULL;
2354 return 0;
2355#endif
2356 case NL80211_IFTYPE_STATION:
2357 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2358 sta = sta_info_get(sdata, skb->data);
Johannes Berg11d62ca2016-09-13 08:28:22 +02002359 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2360 if (test_sta_flag(sta,
2361 WLAN_STA_TDLS_PEER_AUTH)) {
Johannes Berg97ffe752015-03-21 09:13:45 +01002362 *sta_out = sta;
2363 return 0;
2364 }
2365
2366 /*
2367 * TDLS link during setup - throw out frames to
2368 * peer. Allow TDLS-setup frames to unauthorized
2369 * peers for the special case of a link teardown
2370 * after a TDLS sta is removed due to being
2371 * unreachable.
2372 */
Johannes Berg11d62ca2016-09-13 08:28:22 +02002373 if (!ieee80211_is_tdls_setup(skb))
Johannes Berg97ffe752015-03-21 09:13:45 +01002374 return -EINVAL;
2375 }
2376
2377 }
2378
2379 sta = sta_info_get(sdata, sdata->u.mgd.bssid);
2380 if (!sta)
2381 return -ENOLINK;
2382 break;
2383 default:
2384 return -EINVAL;
2385 }
2386
2387 *sta_out = sta ?: ERR_PTR(-ENOENT);
2388 return 0;
Matti Gottliebad38bfc2013-11-18 19:06:45 +02002389}
2390
Johannes Berge2ebc742007-07-27 15:43:22 +02002391/**
Johannes Berg4c9451e2014-11-09 18:50:10 +02002392 * ieee80211_build_hdr - build 802.11 header in the given frame
2393 * @sdata: virtual interface to build the header for
2394 * @skb: the skb to build the header in
Liad Kaufman24d342c2014-11-09 18:50:07 +02002395 * @info_flags: skb flags to set
Johannes Berge2ebc742007-07-27 15:43:22 +02002396 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002397 * This function takes the skb with 802.3 header and reformats the header to
2398 * the appropriate IEEE 802.11 header based on which interface the packet is
2399 * being transmitted on.
Johannes Berge2ebc742007-07-27 15:43:22 +02002400 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02002401 * Note that this function also takes care of the TX status request and
2402 * potential unsharing of the SKB - this needs to be interleaved with the
2403 * header building.
2404 *
2405 * The function requires the read-side RCU lock held
2406 *
2407 * Returns: the (possibly reallocated) skb or an ERR_PTR() code
Johannes Berge2ebc742007-07-27 15:43:22 +02002408 */
Johannes Berg4c9451e2014-11-09 18:50:10 +02002409static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
Johannes Berg7c107702015-03-20 14:18:27 +01002410 struct sk_buff *skb, u32 info_flags,
Johannes Berg97ffe752015-03-21 09:13:45 +01002411 struct sta_info *sta)
Johannes Berge2ebc742007-07-27 15:43:22 +02002412{
Johannes Berg133b8222008-09-16 14:18:59 +02002413 struct ieee80211_local *local = sdata->local;
Felix Fietkau489ee912010-12-18 19:30:48 +01002414 struct ieee80211_tx_info *info;
Johannes Bergdcf33962012-07-30 15:11:56 +02002415 int head_need;
Harvey Harrison065e96052008-06-22 16:45:27 -07002416 u16 ethertype, hdrlen, meshhdrlen = 0;
2417 __le16 fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002418 struct ieee80211_hdr hdr;
Wey-Yi Guyff67bb82010-08-21 07:23:29 -07002419 struct ieee80211s_hdr mesh_hdr __maybe_unused;
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002420 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02002421 const u8 *encaps_data;
2422 int encaps_len, skip_header_bytes;
Johannes Berg97ffe752015-03-21 09:13:45 +01002423 bool wme_sta = false, authorized = false;
2424 bool tdls_peer;
Johannes Berga729cff2011-11-06 14:13:34 +01002425 bool multicast;
Johannes Berga729cff2011-11-06 14:13:34 +01002426 u16 info_id = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002427 struct ieee80211_chanctx_conf *chanctx_conf;
2428 struct ieee80211_sub_if_data *ap_sdata;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002429 enum nl80211_band band;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002430 int ret;
Johannes Berge2ebc742007-07-27 15:43:22 +02002431
Johannes Berg97ffe752015-03-21 09:13:45 +01002432 if (IS_ERR(sta))
2433 sta = NULL;
2434
Johannes Berge2ebc742007-07-27 15:43:22 +02002435 /* convert Ethernet header to proper 802.11 header (based on
2436 * operation mode) */
2437 ethertype = (skb->data[12] << 8) | skb->data[13];
Harvey Harrison065e96052008-06-22 16:45:27 -07002438 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
Johannes Berge2ebc742007-07-27 15:43:22 +02002439
Johannes Berg51fb61e2007-12-19 01:31:27 +01002440 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002441 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg97ffe752015-03-21 09:13:45 +01002442 if (sdata->wdev.use_4addr) {
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002443 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2444 /* RA TA DA SA */
2445 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002446 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002447 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2448 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2449 hdrlen = 30;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002450 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Johannes Berga74a8c82014-07-22 14:50:47 +02002451 wme_sta = sta->sta.wme;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002452 }
Johannes Berg55de9082012-07-26 17:24:39 +02002453 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2454 u.ap);
2455 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002456 if (!chanctx_conf) {
2457 ret = -ENOTCONN;
2458 goto free;
2459 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002460 band = chanctx_conf->def.chan->band;
Johannes Berg97ffe752015-03-21 09:13:45 +01002461 if (sdata->wdev.use_4addr)
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002462 break;
2463 /* fall through */
2464 case NL80211_IFTYPE_AP:
Arnd Bergmannfe801232013-01-25 14:14:33 +00002465 if (sdata->vif.type == NL80211_IFTYPE_AP)
2466 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002467 if (!chanctx_conf) {
2468 ret = -ENOTCONN;
2469 goto free;
2470 }
Harvey Harrison065e96052008-06-22 16:45:27 -07002471 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
Johannes Berge2ebc742007-07-27 15:43:22 +02002472 /* DA BSSID SA */
2473 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002474 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002475 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2476 hdrlen = 24;
Johannes Berg4bf88532012-11-09 11:39:59 +01002477 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002478 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002479 case NL80211_IFTYPE_WDS:
Harvey Harrison065e96052008-06-22 16:45:27 -07002480 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
Johannes Berge2ebc742007-07-27 15:43:22 +02002481 /* RA TA DA SA */
2482 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002483 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002484 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2485 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2486 hdrlen = 30;
Johannes Berg55de9082012-07-26 17:24:39 +02002487 /*
2488 * This is the exception! WDS style interfaces are prohibited
2489 * when channel contexts are in used so this must be valid
2490 */
Karl Beldan675a0b02013-03-25 16:26:57 +01002491 band = local->hw.conf.chandef.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002492 break;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002493#ifdef CONFIG_MAC80211_MESH
Johannes Berg05c914f2008-09-11 00:01:58 +02002494 case NL80211_IFTYPE_MESH_POINT:
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002495 if (!is_multicast_ether_addr(skb->data)) {
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002496 struct sta_info *next_hop;
2497 bool mpp_lookup = true;
2498
Johannes Bergbf7cd942013-02-15 14:40:31 +01002499 mpath = mesh_path_lookup(sdata, skb->data);
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002500 if (mpath) {
2501 mpp_lookup = false;
2502 next_hop = rcu_dereference(mpath->next_hop);
2503 if (!next_hop ||
2504 !(mpath->flags & (MESH_PATH_ACTIVE |
2505 MESH_PATH_RESOLVING)))
2506 mpp_lookup = true;
2507 }
2508
Henning Roggeab1c7902016-02-03 13:58:37 +01002509 if (mpp_lookup) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01002510 mppath = mpp_path_lookup(sdata, skb->data);
Henning Roggeab1c7902016-02-03 13:58:37 +01002511 if (mppath)
2512 mppath->exp_time = jiffies;
2513 }
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08002514
2515 if (mppath && mpath)
Bob Copeland2bdaf382016-02-28 20:03:56 -05002516 mesh_path_del(sdata, mpath->dst);
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08002517 }
YanBo79617de2008-09-22 13:30:32 +08002518
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002519 /*
Joel A Fernandes9d525012011-01-10 00:44:23 -06002520 * Use address extension if it is a packet from
2521 * another interface or if we know the destination
2522 * is being proxied by a portal (i.e. portal address
2523 * differs from proxied address)
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06002524 */
Joe Perchesb203ca32012-05-08 18:56:52 +00002525 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2526 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
Javier Cardona3c5772a2009-08-10 12:15:48 -07002527 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2528 skb->data, skb->data + ETH_ALEN);
Johannes Bergbf7cd942013-02-15 14:40:31 +01002529 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2530 NULL, NULL);
YanBo79617de2008-09-22 13:30:32 +08002531 } else {
Thomas Pedersen27f01122012-08-20 11:28:25 -07002532 /* DS -> MBSS (802.11-2012 13.11.3.3).
2533 * For unicast with unknown forwarding information,
2534 * destination might be in the MBSS or if that fails
2535 * forwarded to another mesh gate. In either case
2536 * resolution will be handled in ieee80211_xmit(), so
2537 * leave the original DA. This also works for mcast */
2538 const u8 *mesh_da = skb->data;
YanBo79617de2008-09-22 13:30:32 +08002539
Thomas Pedersen27f01122012-08-20 11:28:25 -07002540 if (mppath)
2541 mesh_da = mppath->mpp;
2542 else if (mpath)
2543 mesh_da = mpath->dst;
Thomas Pedersen27f01122012-08-20 11:28:25 -07002544
Javier Cardona3c5772a2009-08-10 12:15:48 -07002545 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
Johannes Berg47846c92009-11-25 17:46:19 +01002546 mesh_da, sdata->vif.addr);
Thomas Pedersen27f01122012-08-20 11:28:25 -07002547 if (is_multicast_ether_addr(mesh_da))
2548 /* DA TA mSA AE:SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002549 meshhdrlen = ieee80211_new_mesh_header(
2550 sdata, &mesh_hdr,
2551 skb->data + ETH_ALEN, NULL);
Javier Cardona3c5772a2009-08-10 12:15:48 -07002552 else
Thomas Pedersen27f01122012-08-20 11:28:25 -07002553 /* RA TA mDA mSA AE:DA SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01002554 meshhdrlen = ieee80211_new_mesh_header(
2555 sdata, &mesh_hdr, skb->data,
2556 skb->data + ETH_ALEN);
YanBo79617de2008-09-22 13:30:32 +08002557
YanBo79617de2008-09-22 13:30:32 +08002558 }
Johannes Berg55de9082012-07-26 17:24:39 +02002559 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002560 if (!chanctx_conf) {
2561 ret = -ENOTCONN;
2562 goto free;
2563 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002564 band = chanctx_conf->def.chan->band;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002565 break;
2566#endif
Johannes Berg05c914f2008-09-11 00:01:58 +02002567 case NL80211_IFTYPE_STATION:
Johannes Berg97ffe752015-03-21 09:13:45 +01002568 /* we already did checks when looking up the RA STA */
2569 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002570
Johannes Berg97ffe752015-03-21 09:13:45 +01002571 if (tdls_peer) {
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002572 /* DA SA BSSID */
2573 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2574 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2575 memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2576 hdrlen = 24;
2577 } else if (sdata->u.mgd.use_4addr &&
2578 cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2579 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2580 IEEE80211_FCTL_TODS);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002581 /* RA TA DA SA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002582 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002583 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002584 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2585 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2586 hdrlen = 30;
2587 } else {
2588 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2589 /* BSSID SA DA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002590 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002591 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2592 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2593 hdrlen = 24;
2594 }
Johannes Berg55de9082012-07-26 17:24:39 +02002595 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002596 if (!chanctx_conf) {
2597 ret = -ENOTCONN;
2598 goto free;
2599 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002600 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002601 break;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002602 case NL80211_IFTYPE_OCB:
2603 /* DA SA BSSID */
2604 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2605 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2606 eth_broadcast_addr(hdr.addr3);
2607 hdrlen = 24;
2608 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002609 if (!chanctx_conf) {
2610 ret = -ENOTCONN;
2611 goto free;
2612 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002613 band = chanctx_conf->def.chan->band;
2614 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002615 case NL80211_IFTYPE_ADHOC:
Johannes Berge2ebc742007-07-27 15:43:22 +02002616 /* DA SA BSSID */
2617 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2618 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01002619 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002620 hdrlen = 24;
Johannes Berg55de9082012-07-26 17:24:39 +02002621 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002622 if (!chanctx_conf) {
2623 ret = -ENOTCONN;
2624 goto free;
2625 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002626 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002627 break;
2628 default:
Johannes Berg4c9451e2014-11-09 18:50:10 +02002629 ret = -EINVAL;
2630 goto free;
Johannes Berge2ebc742007-07-27 15:43:22 +02002631 }
2632
Johannes Berga729cff2011-11-06 14:13:34 +01002633 multicast = is_multicast_ether_addr(hdr.addr1);
Johannes Berge2ebc742007-07-27 15:43:22 +02002634
Johannes Berg97ffe752015-03-21 09:13:45 +01002635 /* sta is always NULL for mesh */
2636 if (sta) {
2637 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2638 wme_sta = sta->sta.wme;
2639 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2640 /* For mesh, the use of the QoS header is mandatory */
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002641 wme_sta = true;
Johannes Berge2ebc742007-07-27 15:43:22 +02002642 }
2643
Johannes Berg527871d2015-03-21 08:09:55 +01002644 /* receiver does QoS (which also means we do) use it */
2645 if (wme_sta) {
Harvey Harrison065e96052008-06-22 16:45:27 -07002646 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002647 hdrlen += 2;
2648 }
2649
2650 /*
Johannes Berg238814f2008-01-28 17:19:37 +01002651 * Drop unicast frames to unauthorised stations unless they are
2652 * EAPOL frames from the local station.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002653 */
Johannes Berg55182e42011-10-12 17:28:21 +02002654 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002655 (sdata->vif.type != NL80211_IFTYPE_OCB) &&
Sergey Ryazanova6ececf2013-08-30 01:35:09 +04002656 !multicast && !authorized &&
Johannes Berg55182e42011-10-12 17:28:21 +02002657 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002658 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002659#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002660 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
Johannes Berg4c9451e2014-11-09 18:50:10 +02002661 sdata->name, hdr.addr1);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002662#endif
2663
2664 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2665
Johannes Berg4c9451e2014-11-09 18:50:10 +02002666 ret = -EPERM;
2667 goto free;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002668 }
2669
Johannes Berga729cff2011-11-06 14:13:34 +01002670 if (unlikely(!multicast && skb->sk &&
2671 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)) {
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002672 struct sk_buff *ack_skb = skb_clone_sk(skb);
Johannes Berga729cff2011-11-06 14:13:34 +01002673
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002674 if (ack_skb) {
Johannes Berga729cff2011-11-06 14:13:34 +01002675 unsigned long flags;
Tejun Heo9475af62013-02-27 17:04:59 -08002676 int id;
Johannes Berga729cff2011-11-06 14:13:34 +01002677
2678 spin_lock_irqsave(&local->ack_status_lock, flags);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002679 id = idr_alloc(&local->ack_status_frames, ack_skb,
Tejun Heo9475af62013-02-27 17:04:59 -08002680 1, 0x10000, GFP_ATOMIC);
Johannes Berga729cff2011-11-06 14:13:34 +01002681 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2682
Tejun Heo9475af62013-02-27 17:04:59 -08002683 if (id >= 0) {
Johannes Berga729cff2011-11-06 14:13:34 +01002684 info_id = id;
2685 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Berga729cff2011-11-06 14:13:34 +01002686 } else {
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002687 kfree_skb(ack_skb);
Johannes Berga729cff2011-11-06 14:13:34 +01002688 }
Johannes Berga729cff2011-11-06 14:13:34 +01002689 }
2690 }
2691
Helmut Schaa7e244702010-12-02 18:44:09 +01002692 /*
2693 * If the skb is shared we need to obtain our own copy.
2694 */
2695 if (skb_shared(skb)) {
Johannes Berga729cff2011-11-06 14:13:34 +01002696 struct sk_buff *tmp_skb = skb;
2697
2698 /* can't happen -- skb is a clone if info_id != 0 */
2699 WARN_ON(info_id);
2700
Felix Fietkauf8a0a782010-12-18 19:30:50 +01002701 skb = skb_clone(skb, GFP_ATOMIC);
Helmut Schaa7e244702010-12-02 18:44:09 +01002702 kfree_skb(tmp_skb);
2703
Johannes Berg4c9451e2014-11-09 18:50:10 +02002704 if (!skb) {
2705 ret = -ENOMEM;
2706 goto free;
2707 }
Helmut Schaa7e244702010-12-02 18:44:09 +01002708 }
2709
Harvey Harrison065e96052008-06-22 16:45:27 -07002710 hdr.frame_control = fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002711 hdr.duration_id = 0;
2712 hdr.seq_ctrl = 0;
2713
2714 skip_header_bytes = ETH_HLEN;
2715 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2716 encaps_data = bridge_tunnel_header;
2717 encaps_len = sizeof(bridge_tunnel_header);
2718 skip_header_bytes -= 2;
Simon Hormane5c5d222013-03-28 13:38:25 +09002719 } else if (ethertype >= ETH_P_802_3_MIN) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002720 encaps_data = rfc1042_header;
2721 encaps_len = sizeof(rfc1042_header);
2722 skip_header_bytes -= 2;
2723 } else {
2724 encaps_data = NULL;
2725 encaps_len = 0;
2726 }
2727
2728 skb_pull(skb, skip_header_bytes);
Johannes Berg23c07522008-05-29 10:38:53 +02002729 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002730
Johannes Berg23c07522008-05-29 10:38:53 +02002731 /*
2732 * So we need to modify the skb header and hence need a copy of
2733 * that. The head_need variable above doesn't, so far, include
2734 * the needed header space that we don't need right away. If we
2735 * can, then we don't reallocate right now but only after the
2736 * frame arrives at the master device (if it does...)
2737 *
2738 * If we cannot, however, then we will reallocate to include all
2739 * the ever needed space. Also, if we need to reallocate it anyway,
2740 * make it big enough for everything we may ever need.
2741 */
Johannes Berge2ebc742007-07-27 15:43:22 +02002742
David S. Miller3a5be7d2008-06-18 01:19:51 -07002743 if (head_need > 0 || skb_cloned(skb)) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002744 head_need += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02002745 head_need += local->tx_headroom;
2746 head_need = max_t(int, 0, head_need);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002747 if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
2748 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg1c963be2012-11-07 14:02:30 +01002749 skb = NULL;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002750 return ERR_PTR(-ENOMEM);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002751 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002752 }
2753
Felix Fietkaueae44302016-07-13 11:00:02 +02002754 if (encaps_data)
Johannes Berge2ebc742007-07-27 15:43:22 +02002755 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002756
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002757#ifdef CONFIG_MAC80211_MESH
Felix Fietkaueae44302016-07-13 11:00:02 +02002758 if (meshhdrlen > 0)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002759 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002760#endif
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002761
Harvey Harrison065e96052008-06-22 16:45:27 -07002762 if (ieee80211_is_data_qos(fc)) {
Johannes Bergc29b9b92007-09-14 11:10:24 -04002763 __le16 *qos_control;
2764
Johannes Bergd58ff352017-06-16 14:29:23 +02002765 qos_control = skb_push(skb, 2);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002766 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2767 /*
2768 * Maybe we could actually set some fields here, for now just
2769 * initialise to zero to indicate no special operation.
2770 */
2771 *qos_control = 0;
2772 } else
2773 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2774
Zhang Shengjud57a5442016-03-03 01:16:56 +00002775 skb_reset_mac_header(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002776
Felix Fietkau489ee912010-12-18 19:30:48 +01002777 info = IEEE80211_SKB_CB(skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002778 memset(info, 0, sizeof(*info));
2779
Johannes Berga729cff2011-11-06 14:13:34 +01002780 info->flags = info_flags;
2781 info->ack_frame_id = info_id;
Johannes Berg73c4e192014-11-09 18:50:09 +02002782 info->band = band;
Johannes Berga729cff2011-11-06 14:13:34 +01002783
Johannes Berg4c9451e2014-11-09 18:50:10 +02002784 return skb;
2785 free:
2786 kfree_skb(skb);
2787 return ERR_PTR(ret);
Johannes Berge2ebc742007-07-27 15:43:22 +02002788}
2789
Johannes Berg17c18bf2015-03-21 15:25:43 +01002790/*
2791 * fast-xmit overview
2792 *
2793 * The core idea of this fast-xmit is to remove per-packet checks by checking
2794 * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2795 * checks that are needed to get the sta->fast_tx pointer assigned, after which
2796 * much less work can be done per packet. For example, fragmentation must be
2797 * disabled or the fast_tx pointer will not be set. All the conditions are seen
2798 * in the code here.
2799 *
2800 * Once assigned, the fast_tx data structure also caches the per-packet 802.11
2801 * header and other data to aid packet processing in ieee80211_xmit_fast().
2802 *
2803 * The most difficult part of this is that when any of these assumptions
2804 * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
2805 * ieee80211_check_fast_xmit() or friends) is required to reset the data,
2806 * since the per-packet code no longer checks the conditions. This is reflected
2807 * by the calls to these functions throughout the rest of the code, and must be
2808 * maintained if any of the TX path checks change.
2809 */
2810
2811void ieee80211_check_fast_xmit(struct sta_info *sta)
2812{
2813 struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
2814 struct ieee80211_local *local = sta->local;
2815 struct ieee80211_sub_if_data *sdata = sta->sdata;
2816 struct ieee80211_hdr *hdr = (void *)build.hdr;
2817 struct ieee80211_chanctx_conf *chanctx_conf;
2818 __le16 fc;
2819
Johannes Berg30686bf2015-06-02 21:39:54 +02002820 if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002821 return;
2822
2823 /* Locking here protects both the pointer itself, and against concurrent
2824 * invocations winning data access races to, e.g., the key pointer that
2825 * is used.
2826 * Without it, the invocation of this function right after the key
2827 * pointer changes wouldn't be sufficient, as another CPU could access
2828 * the pointer, then stall, and then do the cache update after the CPU
2829 * that invalidated the key.
2830 * With the locking, such scenarios cannot happen as the check for the
2831 * key and the fast-tx assignment are done atomically, so the CPU that
2832 * modifies the key will either wait or other one will see the key
2833 * cleared/changed already.
2834 */
2835 spin_lock_bh(&sta->lock);
Johannes Berg30686bf2015-06-02 21:39:54 +02002836 if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
2837 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
Johannes Berg17c18bf2015-03-21 15:25:43 +01002838 sdata->vif.type == NL80211_IFTYPE_STATION)
2839 goto out;
2840
2841 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2842 goto out;
2843
2844 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
2845 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
Felix Fietkauf7418bc2015-09-24 14:59:49 +02002846 test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
2847 test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002848 goto out;
2849
2850 if (sdata->noack_map)
2851 goto out;
2852
2853 /* fast-xmit doesn't handle fragmentation at all */
Johannes Berg725b8122015-04-10 14:02:08 +02002854 if (local->hw.wiphy->frag_threshold != (u32)-1 &&
Sara Sharonf3fe4e92016-10-18 23:12:11 +03002855 !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
Johannes Berg17c18bf2015-03-21 15:25:43 +01002856 goto out;
2857
2858 rcu_read_lock();
2859 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2860 if (!chanctx_conf) {
2861 rcu_read_unlock();
2862 goto out;
2863 }
2864 build.band = chanctx_conf->def.chan->band;
2865 rcu_read_unlock();
2866
2867 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2868
2869 switch (sdata->vif.type) {
Johannes Berg3ffd8842015-04-14 10:28:37 +02002870 case NL80211_IFTYPE_ADHOC:
2871 /* DA SA BSSID */
2872 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2873 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2874 memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
2875 build.hdr_len = 24;
2876 break;
Johannes Berg17c18bf2015-03-21 15:25:43 +01002877 case NL80211_IFTYPE_STATION:
2878 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2879 /* DA SA BSSID */
2880 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2881 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2882 memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
2883 build.hdr_len = 24;
2884 break;
2885 }
2886
2887 if (sdata->u.mgd.use_4addr) {
2888 /* non-regular ethertype cannot use the fastpath */
2889 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2890 IEEE80211_FCTL_TODS);
2891 /* RA TA DA SA */
2892 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2893 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2894 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2895 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2896 build.hdr_len = 30;
2897 break;
2898 }
2899 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2900 /* BSSID SA DA */
2901 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2902 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2903 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2904 build.hdr_len = 24;
2905 break;
2906 case NL80211_IFTYPE_AP_VLAN:
2907 if (sdata->wdev.use_4addr) {
2908 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2909 IEEE80211_FCTL_TODS);
2910 /* RA TA DA SA */
2911 memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
2912 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2913 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2914 build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2915 build.hdr_len = 30;
2916 break;
2917 }
2918 /* fall through */
2919 case NL80211_IFTYPE_AP:
2920 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2921 /* DA BSSID SA */
2922 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2923 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2924 build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
2925 build.hdr_len = 24;
2926 break;
2927 default:
2928 /* not handled on fast-xmit */
2929 goto out;
2930 }
2931
2932 if (sta->sta.wme) {
2933 build.hdr_len += 2;
2934 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2935 }
2936
2937 /* We store the key here so there's no point in using rcu_dereference()
2938 * but that's fine because the code that changes the pointers will call
2939 * this function after doing so. For a single CPU that would be enough,
2940 * for multiple see the comment above.
2941 */
2942 build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
2943 if (!build.key)
2944 build.key = rcu_access_pointer(sdata->default_unicast_key);
2945 if (build.key) {
Johannes Berge495c242015-04-10 14:03:17 +02002946 bool gen_iv, iv_spc, mmic;
Johannes Berg17c18bf2015-03-21 15:25:43 +01002947
2948 gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
2949 iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
David Spinadel9de18d82017-12-01 13:50:52 +02002950 mmic = build.key->conf.flags &
2951 (IEEE80211_KEY_FLAG_GENERATE_MMIC |
2952 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
Johannes Berg17c18bf2015-03-21 15:25:43 +01002953
2954 /* don't handle software crypto */
2955 if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
2956 goto out;
2957
2958 switch (build.key->conf.cipher) {
2959 case WLAN_CIPHER_SUITE_CCMP:
2960 case WLAN_CIPHER_SUITE_CCMP_256:
2961 /* add fixed key ID */
2962 if (gen_iv) {
2963 (build.hdr + build.hdr_len)[3] =
2964 0x20 | (build.key->conf.keyidx << 6);
2965 build.pn_offs = build.hdr_len;
2966 }
2967 if (gen_iv || iv_spc)
2968 build.hdr_len += IEEE80211_CCMP_HDR_LEN;
2969 break;
2970 case WLAN_CIPHER_SUITE_GCMP:
2971 case WLAN_CIPHER_SUITE_GCMP_256:
2972 /* add fixed key ID */
2973 if (gen_iv) {
2974 (build.hdr + build.hdr_len)[3] =
2975 0x20 | (build.key->conf.keyidx << 6);
2976 build.pn_offs = build.hdr_len;
2977 }
2978 if (gen_iv || iv_spc)
2979 build.hdr_len += IEEE80211_GCMP_HDR_LEN;
2980 break;
Johannes Berge495c242015-04-10 14:03:17 +02002981 case WLAN_CIPHER_SUITE_TKIP:
2982 /* cannot handle MMIC or IV generation in xmit-fast */
2983 if (mmic || gen_iv)
2984 goto out;
2985 if (iv_spc)
2986 build.hdr_len += IEEE80211_TKIP_IV_LEN;
2987 break;
2988 case WLAN_CIPHER_SUITE_WEP40:
2989 case WLAN_CIPHER_SUITE_WEP104:
2990 /* cannot handle IV generation in fast-xmit */
2991 if (gen_iv)
2992 goto out;
2993 if (iv_spc)
2994 build.hdr_len += IEEE80211_WEP_IV_LEN;
2995 break;
2996 case WLAN_CIPHER_SUITE_AES_CMAC:
2997 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2998 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2999 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3000 WARN(1,
3001 "management cipher suite 0x%x enabled for data\n",
3002 build.key->conf.cipher);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003003 goto out;
Johannes Berge495c242015-04-10 14:03:17 +02003004 default:
3005 /* we don't know how to generate IVs for this at all */
3006 if (WARN_ON(gen_iv))
3007 goto out;
3008 /* pure hardware keys are OK, of course */
3009 if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
3010 break;
3011 /* cipher scheme might require space allocation */
3012 if (iv_spc &&
3013 build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
3014 goto out;
3015 if (iv_spc)
3016 build.hdr_len += build.key->conf.iv_len;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003017 }
3018
3019 fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3020 }
3021
3022 hdr->frame_control = fc;
3023
3024 memcpy(build.hdr + build.hdr_len,
3025 rfc1042_header, sizeof(rfc1042_header));
3026 build.hdr_len += sizeof(rfc1042_header);
3027
3028 fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3029 /* if the kmemdup fails, continue w/o fast_tx */
3030 if (!fast_tx)
3031 goto out;
3032
3033 out:
3034 /* we might have raced against another call to this function */
3035 old = rcu_dereference_protected(sta->fast_tx,
3036 lockdep_is_held(&sta->lock));
3037 rcu_assign_pointer(sta->fast_tx, fast_tx);
3038 if (old)
3039 kfree_rcu(old, rcu_head);
3040 spin_unlock_bh(&sta->lock);
3041}
3042
3043void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3044{
3045 struct sta_info *sta;
3046
3047 rcu_read_lock();
3048 list_for_each_entry_rcu(sta, &local->sta_list, list)
3049 ieee80211_check_fast_xmit(sta);
3050 rcu_read_unlock();
3051}
3052
3053void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3054{
3055 struct ieee80211_local *local = sdata->local;
3056 struct sta_info *sta;
3057
3058 rcu_read_lock();
3059
3060 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3061 if (sdata != sta->sdata &&
3062 (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3063 continue;
3064 ieee80211_check_fast_xmit(sta);
3065 }
3066
3067 rcu_read_unlock();
3068}
3069
3070void ieee80211_clear_fast_xmit(struct sta_info *sta)
3071{
3072 struct ieee80211_fast_tx *fast_tx;
3073
3074 spin_lock_bh(&sta->lock);
3075 fast_tx = rcu_dereference_protected(sta->fast_tx,
3076 lockdep_is_held(&sta->lock));
3077 RCU_INIT_POINTER(sta->fast_tx, NULL);
3078 spin_unlock_bh(&sta->lock);
3079
3080 if (fast_tx)
3081 kfree_rcu(fast_tx, rcu_head);
3082}
3083
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003084static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
Sara Sharon166ac9d2018-08-29 08:57:02 +02003085 struct sk_buff *skb, int headroom)
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003086{
Sara Sharon166ac9d2018-08-29 08:57:02 +02003087 if (skb_headroom(skb) < headroom) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003088 I802_DEBUG_INC(local->tx_expand_skb_head);
3089
Sara Sharon166ac9d2018-08-29 08:57:02 +02003090 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003091 wiphy_debug(local->hw.wiphy,
3092 "failed to reallocate TX buffer\n");
3093 return false;
3094 }
3095 }
3096
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003097 return true;
3098}
3099
3100static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3101 struct ieee80211_fast_tx *fast_tx,
3102 struct sk_buff *skb)
3103{
3104 struct ieee80211_local *local = sdata->local;
3105 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3106 struct ieee80211_hdr *hdr;
Michael Braun06f2bb12016-10-15 13:28:18 +02003107 struct ethhdr *amsdu_hdr;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003108 int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3109 int subframe_len = skb->len - hdr_len;
3110 void *data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003111 u8 *qc, *h_80211_src, *h_80211_dst;
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003112 const u8 *bssid;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003113
3114 if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3115 return false;
3116
3117 if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3118 return true;
3119
Sara Sharon166ac9d2018-08-29 08:57:02 +02003120 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(*amsdu_hdr)))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003121 return false;
3122
Michael Braun06f2bb12016-10-15 13:28:18 +02003123 data = skb_push(skb, sizeof(*amsdu_hdr));
3124 memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003125 hdr = data;
Michael Braun06f2bb12016-10-15 13:28:18 +02003126 amsdu_hdr = data + hdr_len;
3127 /* h_80211_src/dst is addr* field within hdr */
3128 h_80211_src = data + fast_tx->sa_offs;
3129 h_80211_dst = data + fast_tx->da_offs;
3130
3131 amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3132 ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3133 ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
3134
Michael Brauna3e2f4b2016-10-15 13:28:19 +02003135 /* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3136 * fields needs to be changed to BSSID for A-MSDU frames depending
3137 * on FromDS/ToDS values.
3138 */
3139 switch (sdata->vif.type) {
3140 case NL80211_IFTYPE_STATION:
3141 bssid = sdata->u.mgd.bssid;
3142 break;
3143 case NL80211_IFTYPE_AP:
3144 case NL80211_IFTYPE_AP_VLAN:
3145 bssid = sdata->vif.addr;
3146 break;
3147 default:
3148 bssid = NULL;
3149 }
3150
3151 if (bssid && ieee80211_has_fromds(hdr->frame_control))
3152 ether_addr_copy(h_80211_src, bssid);
3153
3154 if (bssid && ieee80211_has_tods(hdr->frame_control))
3155 ether_addr_copy(h_80211_dst, bssid);
3156
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003157 qc = ieee80211_get_qos_ctl(hdr);
3158 *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3159
3160 info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3161
3162 return true;
3163}
3164
3165static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3166 struct sta_info *sta,
3167 struct ieee80211_fast_tx *fast_tx,
3168 struct sk_buff *skb)
3169{
3170 struct ieee80211_local *local = sdata->local;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003171 struct fq *fq = &local->fq;
3172 struct fq_tin *tin;
3173 struct fq_flow *flow;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003174 u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3175 struct ieee80211_txq *txq = sta->sta.txq[tid];
3176 struct txq_info *txqi;
3177 struct sk_buff **frag_tail, *head;
3178 int subframe_len = skb->len - ETH_ALEN;
3179 u8 max_subframes = sta->sta.max_amsdu_subframes;
3180 int max_frags = local->hw.max_tx_fragments;
3181 int max_amsdu_len = sta->sta.max_amsdu_len;
3182 __be16 len;
3183 void *data;
3184 bool ret = false;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003185 unsigned int orig_len;
Lorenzo Bianconi66eb02d2018-08-31 01:04:13 +02003186 int n = 2, nfrags, pad = 0;
Sara Sharon166ac9d2018-08-29 08:57:02 +02003187 u16 hdrlen;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003188
3189 if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
3190 return false;
3191
3192 if (!txq)
3193 return false;
3194
3195 txqi = to_txq_info(txq);
3196 if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3197 return false;
3198
3199 if (sta->sta.max_rc_amsdu_len)
3200 max_amsdu_len = min_t(int, max_amsdu_len,
3201 sta->sta.max_rc_amsdu_len);
3202
Michal Kaziorfa962b92016-05-19 10:37:49 +02003203 spin_lock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003204
Michal Kaziorfa962b92016-05-19 10:37:49 +02003205 /* TODO: Ideally aggregation should be done on dequeue to remain
3206 * responsive to environment changes.
3207 */
3208
3209 tin = &txqi->tin;
3210 flow = fq_flow_classify(fq, tin, skb, fq_flow_get_default_func);
3211 head = skb_peek_tail(&flow->queue);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003212 if (!head)
3213 goto out;
3214
Michal Kaziorfa962b92016-05-19 10:37:49 +02003215 orig_len = head->len;
3216
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003217 if (skb->len + head->len > max_amsdu_len)
3218 goto out;
3219
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003220 nfrags = 1 + skb_shinfo(skb)->nr_frags;
3221 nfrags += 1 + skb_shinfo(head)->nr_frags;
3222 frag_tail = &skb_shinfo(head)->frag_list;
3223 while (*frag_tail) {
3224 nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3225 frag_tail = &(*frag_tail)->next;
3226 n++;
3227 }
3228
3229 if (max_subframes && n > max_subframes)
3230 goto out;
3231
3232 if (max_frags && nfrags > max_frags)
3233 goto out;
3234
Lorenzo Bianconi1eb50792018-08-29 21:03:25 +02003235 if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003236 goto out;
3237
Sara Sharon166ac9d2018-08-29 08:57:02 +02003238 /*
3239 * Pad out the previous subframe to a multiple of 4 by adding the
3240 * padding to the next one, that's being added. Note that head->len
3241 * is the length of the full A-MSDU, but that works since each time
3242 * we add a new subframe we pad out the previous one to a multiple
3243 * of 4 and thus it no longer matters in the next round.
3244 */
3245 hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3246 if ((head->len - hdrlen) & 3)
3247 pad = 4 - ((head->len - hdrlen) & 3);
3248
3249 if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3250 2 + pad))
Johannes Bergaa58acf2018-08-30 10:55:49 +02003251 goto out_recalc;
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003252
3253 ret = true;
3254 data = skb_push(skb, ETH_ALEN + 2);
3255 memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3256
3257 data += 2 * ETH_ALEN;
3258 len = cpu_to_be16(subframe_len);
3259 memcpy(data, &len, 2);
3260 memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3261
Sara Sharon166ac9d2018-08-29 08:57:02 +02003262 memset(skb_push(skb, pad), 0, pad);
3263
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003264 head->len += skb->len;
3265 head->data_len += skb->len;
3266 *frag_tail = skb;
3267
Johannes Bergaa58acf2018-08-30 10:55:49 +02003268out_recalc:
3269 if (head->len != orig_len) {
3270 flow->backlog += head->len - orig_len;
3271 tin->backlog_bytes += head->len - orig_len;
Michal Kaziorfa962b92016-05-19 10:37:49 +02003272
Johannes Bergaa58acf2018-08-30 10:55:49 +02003273 fq_recalc_backlog(fq, tin, flow);
3274 }
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003275out:
Michal Kaziorfa962b92016-05-19 10:37:49 +02003276 spin_unlock_bh(&fq->lock);
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003277
3278 return ret;
3279}
3280
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003281/*
3282 * Can be called while the sta lock is held. Anything that can cause packets to
3283 * be generated will cause deadlock!
3284 */
3285static void ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3286 struct sta_info *sta, u8 pn_offs,
3287 struct ieee80211_key *key,
3288 struct sk_buff *skb)
3289{
3290 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3291 struct ieee80211_hdr *hdr = (void *)skb->data;
3292 u8 tid = IEEE80211_NUM_TIDS;
3293
3294 if (key)
3295 info->control.hw_key = &key->conf;
3296
3297 ieee80211_tx_stats(skb->dev, skb->len);
3298
3299 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3300 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003301 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3302 } else {
3303 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3304 hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3305 sdata->sequence_number += 0x10;
3306 }
3307
3308 if (skb_shinfo(skb)->gso_size)
3309 sta->tx_stats.msdu[tid] +=
3310 DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3311 else
3312 sta->tx_stats.msdu[tid]++;
3313
3314 info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3315
3316 /* statistics normally done by ieee80211_tx_h_stats (but that
3317 * has to consider fragmentation, so is more complex)
3318 */
3319 sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3320 sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
3321
3322 if (pn_offs) {
3323 u64 pn;
3324 u8 *crypto_hdr = skb->data + pn_offs;
3325
3326 switch (key->conf.cipher) {
3327 case WLAN_CIPHER_SUITE_CCMP:
3328 case WLAN_CIPHER_SUITE_CCMP_256:
3329 case WLAN_CIPHER_SUITE_GCMP:
3330 case WLAN_CIPHER_SUITE_GCMP_256:
3331 pn = atomic64_inc_return(&key->conf.tx_pn);
3332 crypto_hdr[0] = pn;
3333 crypto_hdr[1] = pn >> 8;
3334 crypto_hdr[4] = pn >> 16;
3335 crypto_hdr[5] = pn >> 24;
3336 crypto_hdr[6] = pn >> 32;
3337 crypto_hdr[7] = pn >> 40;
3338 break;
3339 }
3340 }
3341}
3342
Johannes Berg17c18bf2015-03-21 15:25:43 +01003343static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003344 struct sta_info *sta,
Johannes Berg17c18bf2015-03-21 15:25:43 +01003345 struct ieee80211_fast_tx *fast_tx,
3346 struct sk_buff *skb)
3347{
3348 struct ieee80211_local *local = sdata->local;
3349 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3350 int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3351 int hw_headroom = sdata->local->hw.extra_tx_headroom;
3352 struct ethhdr eth;
Johannes Berg35f432a2017-01-02 11:19:29 +01003353 struct ieee80211_tx_info *info;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003354 struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3355 struct ieee80211_tx_data tx;
3356 ieee80211_tx_result r;
3357 struct tid_ampdu_tx *tid_tx = NULL;
3358 u8 tid = IEEE80211_NUM_TIDS;
3359
3360 /* control port protocol needs a lot of special handling */
3361 if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3362 return false;
3363
3364 /* only RFC 1042 SNAP */
3365 if (ethertype < ETH_P_802_3_MIN)
3366 return false;
3367
3368 /* don't handle TX status request here either */
3369 if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3370 return false;
3371
3372 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3373 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3374 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
Johannes Berg472be002015-06-09 16:45:08 +02003375 if (tid_tx) {
3376 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3377 return false;
3378 if (tid_tx->timeout)
3379 tid_tx->last_tx = jiffies;
3380 }
Johannes Berg17c18bf2015-03-21 15:25:43 +01003381 }
3382
3383 /* after this point (skb is modified) we cannot return false */
3384
3385 if (skb_shared(skb)) {
3386 struct sk_buff *tmp_skb = skb;
3387
3388 skb = skb_clone(skb, GFP_ATOMIC);
3389 kfree_skb(tmp_skb);
3390
3391 if (!skb)
3392 return true;
3393 }
3394
Felix Fietkau6e0456b2016-03-03 22:59:00 +01003395 if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3396 ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
3397 return true;
3398
Johannes Berg17c18bf2015-03-21 15:25:43 +01003399 /* will not be crypto-handled beyond what we do here, so use false
3400 * as the may-encrypt argument for the resize to not account for
3401 * more room than we already have in 'extra_head'
3402 */
3403 if (unlikely(ieee80211_skb_resize(sdata, skb,
3404 max_t(int, extra_head + hw_headroom -
3405 skb_headroom(skb), 0),
3406 false))) {
3407 kfree_skb(skb);
3408 return true;
3409 }
3410
3411 memcpy(&eth, skb->data, ETH_HLEN - 2);
Johannes Bergd58ff352017-06-16 14:29:23 +02003412 hdr = skb_push(skb, extra_head);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003413 memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3414 memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
3415 memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
3416
Johannes Berg35f432a2017-01-02 11:19:29 +01003417 info = IEEE80211_SKB_CB(skb);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003418 memset(info, 0, sizeof(*info));
3419 info->band = fast_tx->band;
3420 info->control.vif = &sdata->vif;
3421 info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3422 IEEE80211_TX_CTL_DONTFRAG |
3423 (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003424 info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003425
Felix Fietkaua786f962016-11-04 10:27:54 +01003426 if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3427 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3428 *ieee80211_get_qos_ctl(hdr) = tid;
3429 }
3430
Johannes Berg17c18bf2015-03-21 15:25:43 +01003431 __skb_queue_head_init(&tx.skbs);
3432
3433 tx.flags = IEEE80211_TX_UNICAST;
3434 tx.local = local;
3435 tx.sdata = sdata;
3436 tx.sta = sta;
3437 tx.key = fast_tx->key;
3438
Johannes Berg30686bf2015-06-02 21:39:54 +02003439 if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
Johannes Berg17c18bf2015-03-21 15:25:43 +01003440 tx.skb = skb;
3441 r = ieee80211_tx_h_rate_ctrl(&tx);
3442 skb = tx.skb;
3443 tx.skb = NULL;
3444
3445 if (r != TX_CONTINUE) {
3446 if (r != TX_QUEUED)
3447 kfree_skb(skb);
3448 return true;
3449 }
3450 }
3451
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003452 if (ieee80211_queue_skb(local, sdata, sta, skb))
3453 return true;
Johannes Berg17c18bf2015-03-21 15:25:43 +01003454
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003455 ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3456 fast_tx->key, skb);
Johannes Berg17c18bf2015-03-21 15:25:43 +01003457
3458 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3459 sdata = container_of(sdata->bss,
3460 struct ieee80211_sub_if_data, u.ap);
3461
3462 __skb_queue_tail(&tx.skbs, skb);
3463 ieee80211_tx_frags(local, &sdata->vif, &sta->sta, &tx.skbs, false);
3464 return true;
3465}
3466
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003467struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3468 struct ieee80211_txq *txq)
3469{
3470 struct ieee80211_local *local = hw_to_local(hw);
3471 struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3472 struct ieee80211_hdr *hdr;
3473 struct sk_buff *skb = NULL;
3474 struct fq *fq = &local->fq;
3475 struct fq_tin *tin = &txqi->tin;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003476 struct ieee80211_tx_info *info;
3477 struct ieee80211_tx_data tx;
3478 ieee80211_tx_result r;
Johannes Berg53168212017-06-22 12:20:30 +02003479 struct ieee80211_vif *vif;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003480
3481 spin_lock_bh(&fq->lock);
3482
3483 if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags))
3484 goto out;
3485
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003486 /* Make sure fragments stay together. */
3487 skb = __skb_dequeue(&txqi->frags);
3488 if (skb)
3489 goto out;
3490
3491begin:
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003492 skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
3493 if (!skb)
3494 goto out;
3495
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003496 hdr = (struct ieee80211_hdr *)skb->data;
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003497 info = IEEE80211_SKB_CB(skb);
3498
3499 memset(&tx, 0, sizeof(tx));
3500 __skb_queue_head_init(&tx.skbs);
3501 tx.local = local;
3502 tx.skb = skb;
3503 tx.sdata = vif_to_sdata(info->control.vif);
3504
3505 if (txq->sta)
3506 tx.sta = container_of(txq->sta, struct sta_info, sta);
3507
3508 /*
3509 * The key can be removed while the packet was queued, so need to call
3510 * this here to get the current key.
3511 */
3512 r = ieee80211_tx_h_select_key(&tx);
3513 if (r != TX_CONTINUE) {
3514 ieee80211_free_txskb(&local->hw, skb);
3515 goto begin;
3516 }
3517
Felix Fietkauc1f4c9e2016-11-04 10:27:52 +01003518 if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
3519 info->flags |= IEEE80211_TX_CTL_AMPDU;
3520 else
3521 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
3522
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003523 if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003524 struct sta_info *sta = container_of(txq->sta, struct sta_info,
3525 sta);
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003526 u8 pn_offs = 0;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003527
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003528 if (tx.key &&
3529 (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
3530 pn_offs = ieee80211_hdrlen(hdr->frame_control);
3531
3532 ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
3533 tx.key, skb);
3534 } else {
3535 if (invoke_tx_handlers_late(&tx))
3536 goto begin;
3537
3538 skb = __skb_dequeue(&tx.skbs);
3539
3540 if (!skb_queue_empty(&tx.skbs))
3541 skb_queue_splice_tail(&tx.skbs, &txqi->frags);
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003542 }
3543
Johannes Berg1e1430d2016-10-04 09:22:19 +02003544 if (skb && skb_has_frag_list(skb) &&
3545 !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
3546 if (skb_linearize(skb)) {
3547 ieee80211_free_txskb(&local->hw, skb);
3548 goto begin;
3549 }
3550 }
3551
Johannes Berg53168212017-06-22 12:20:30 +02003552 switch (tx.sdata->vif.type) {
3553 case NL80211_IFTYPE_MONITOR:
3554 if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
3555 vif = &tx.sdata->vif;
3556 break;
3557 }
3558 tx.sdata = rcu_dereference(local->monitor_sdata);
3559 if (tx.sdata) {
3560 vif = &tx.sdata->vif;
3561 info->hw_queue =
3562 vif->hw_queue[skb_get_queue_mapping(skb)];
3563 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
3564 ieee80211_free_txskb(&local->hw, skb);
3565 goto begin;
3566 } else {
3567 vif = NULL;
3568 }
3569 break;
3570 case NL80211_IFTYPE_AP_VLAN:
3571 tx.sdata = container_of(tx.sdata->bss,
3572 struct ieee80211_sub_if_data, u.ap);
3573 /* fall through */
3574 default:
3575 vif = &tx.sdata->vif;
3576 break;
3577 }
3578
3579 IEEE80211_SKB_CB(skb)->control.vif = vif;
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003580out:
3581 spin_unlock_bh(&fq->lock);
3582
Toke Høiland-Jørgensene0e2eff2016-09-22 19:04:19 +02003583 return skb;
3584}
3585EXPORT_SYMBOL(ieee80211_tx_dequeue);
3586
Johannes Berg4c9451e2014-11-09 18:50:10 +02003587void __ieee80211_subif_start_xmit(struct sk_buff *skb,
3588 struct net_device *dev,
3589 u32 info_flags)
3590{
3591 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg97ffe752015-03-21 09:13:45 +01003592 struct sta_info *sta;
Johannes Berg80616c02015-04-14 14:50:41 +02003593 struct sk_buff *next;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003594
3595 if (unlikely(skb->len < ETH_HLEN)) {
3596 kfree_skb(skb);
3597 return;
3598 }
3599
3600 rcu_read_lock();
3601
Johannes Berg2d981fd2015-04-10 14:10:10 +02003602 if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
3603 goto out_free;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003604
Johannes Berg17c18bf2015-03-21 15:25:43 +01003605 if (!IS_ERR_OR_NULL(sta)) {
3606 struct ieee80211_fast_tx *fast_tx;
3607
Toke Høiland-Jørgensen36148c22018-02-02 16:11:05 +01003608 /* We need a bit of data queued to build aggregates properly, so
3609 * instruct the TCP stack to allow more than a single ms of data
3610 * to be queued in the stack. The value is a bit-shift of 1
3611 * second, so 8 is ~4ms of queued data. Only affects local TCP
3612 * sockets.
3613 */
3614 sk_pacing_shift_update(skb->sk, 8);
3615
Johannes Berg17c18bf2015-03-21 15:25:43 +01003616 fast_tx = rcu_dereference(sta->fast_tx);
3617
3618 if (fast_tx &&
Toke Høiland-Jørgensenbb42f2d2016-09-22 19:04:20 +02003619 ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
Johannes Berg17c18bf2015-03-21 15:25:43 +01003620 goto out;
3621 }
3622
Johannes Berg80616c02015-04-14 14:50:41 +02003623 if (skb_is_gso(skb)) {
3624 struct sk_buff *segs;
Johannes Berg680a0da2015-04-13 16:58:25 +02003625
Johannes Berg80616c02015-04-14 14:50:41 +02003626 segs = skb_gso_segment(skb, 0);
3627 if (IS_ERR(segs)) {
Johannes Berg2d981fd2015-04-10 14:10:10 +02003628 goto out_free;
Johannes Berg80616c02015-04-14 14:50:41 +02003629 } else if (segs) {
3630 consume_skb(skb);
3631 skb = segs;
3632 }
3633 } else {
3634 /* we cannot process non-linear frames on this path */
3635 if (skb_linearize(skb)) {
3636 kfree_skb(skb);
3637 goto out;
3638 }
3639
3640 /* the frame could be fragmented, software-encrypted, and other
3641 * things so we cannot really handle checksum offload with it -
3642 * fix it up in software before we handle anything else.
3643 */
3644 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Johannes Bergf603f1f2015-05-05 15:25:33 +02003645 skb_set_transport_header(skb,
3646 skb_checksum_start_offset(skb));
Johannes Berg80616c02015-04-14 14:50:41 +02003647 if (skb_checksum_help(skb))
3648 goto out_free;
3649 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02003650 }
3651
Johannes Berg80616c02015-04-14 14:50:41 +02003652 next = skb;
3653 while (next) {
3654 skb = next;
3655 next = skb->next;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003656
Johannes Berg80616c02015-04-14 14:50:41 +02003657 skb->prev = NULL;
3658 skb->next = NULL;
Johannes Berg4c9451e2014-11-09 18:50:10 +02003659
Johannes Berg80616c02015-04-14 14:50:41 +02003660 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta);
3661 if (IS_ERR(skb))
3662 goto out;
3663
Johannes Berg5a490512015-04-22 17:10:38 +02003664 ieee80211_tx_stats(dev, skb->len);
Johannes Berg80616c02015-04-14 14:50:41 +02003665
Johannes Bergb9771d42018-05-28 15:47:41 +02003666 ieee80211_xmit(sdata, sta, skb, 0);
Johannes Berg80616c02015-04-14 14:50:41 +02003667 }
Johannes Berg2d981fd2015-04-10 14:10:10 +02003668 goto out;
3669 out_free:
3670 kfree_skb(skb);
Johannes Berg4c9451e2014-11-09 18:50:10 +02003671 out:
3672 rcu_read_unlock();
3673}
3674
Michael Braunebceec82016-11-22 11:52:18 +01003675static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
3676{
3677 struct ethhdr *eth;
3678 int err;
3679
3680 err = skb_ensure_writable(skb, ETH_HLEN);
3681 if (unlikely(err))
3682 return err;
3683
3684 eth = (void *)skb->data;
3685 ether_addr_copy(eth->h_dest, sta->sta.addr);
3686
3687 return 0;
3688}
3689
3690static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
3691 struct net_device *dev)
3692{
3693 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3694 const struct ethhdr *eth = (void *)skb->data;
3695 const struct vlan_ethhdr *ethvlan = (void *)skb->data;
3696 __be16 ethertype;
3697
3698 if (likely(!is_multicast_ether_addr(eth->h_dest)))
3699 return false;
3700
3701 switch (sdata->vif.type) {
3702 case NL80211_IFTYPE_AP_VLAN:
3703 if (sdata->u.vlan.sta)
3704 return false;
3705 if (sdata->wdev.use_4addr)
3706 return false;
3707 /* fall through */
3708 case NL80211_IFTYPE_AP:
3709 /* check runtime toggle for this bss */
3710 if (!sdata->bss->multicast_to_unicast)
3711 return false;
3712 break;
3713 default:
3714 return false;
3715 }
3716
3717 /* multicast to unicast conversion only for some payload */
3718 ethertype = eth->h_proto;
3719 if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
3720 ethertype = ethvlan->h_vlan_encapsulated_proto;
3721 switch (ethertype) {
3722 case htons(ETH_P_ARP):
3723 case htons(ETH_P_IP):
3724 case htons(ETH_P_IPV6):
3725 break;
3726 default:
3727 return false;
3728 }
3729
3730 return true;
3731}
3732
3733static void
3734ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
3735 struct sk_buff_head *queue)
3736{
3737 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3738 struct ieee80211_local *local = sdata->local;
3739 const struct ethhdr *eth = (struct ethhdr *)skb->data;
3740 struct sta_info *sta, *first = NULL;
3741 struct sk_buff *cloned_skb;
3742
3743 rcu_read_lock();
3744
3745 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3746 if (sdata != sta->sdata)
3747 /* AP-VLAN mismatch */
3748 continue;
3749 if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
3750 /* do not send back to source */
3751 continue;
3752 if (!first) {
3753 first = sta;
3754 continue;
3755 }
3756 cloned_skb = skb_clone(skb, GFP_ATOMIC);
3757 if (!cloned_skb)
3758 goto multicast;
3759 if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
3760 dev_kfree_skb(cloned_skb);
3761 goto multicast;
3762 }
3763 __skb_queue_tail(queue, cloned_skb);
3764 }
3765
3766 if (likely(first)) {
3767 if (unlikely(ieee80211_change_da(skb, first)))
3768 goto multicast;
3769 __skb_queue_tail(queue, skb);
3770 } else {
3771 /* no STA connected, drop */
3772 kfree_skb(skb);
3773 skb = NULL;
3774 }
3775
3776 goto out;
3777multicast:
3778 __skb_queue_purge(queue);
3779 __skb_queue_tail(queue, skb);
3780out:
3781 rcu_read_unlock();
3782}
3783
Johannes Berg4c9451e2014-11-09 18:50:10 +02003784/**
3785 * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
3786 * @skb: packet to be sent
3787 * @dev: incoming interface
3788 *
3789 * On failure skb will be freed.
3790 */
Liad Kaufman24d342c2014-11-09 18:50:07 +02003791netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
3792 struct net_device *dev)
3793{
Michael Braunebceec82016-11-22 11:52:18 +01003794 if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
3795 struct sk_buff_head queue;
3796
3797 __skb_queue_head_init(&queue);
3798 ieee80211_convert_to_unicast(skb, dev, &queue);
3799 while ((skb = __skb_dequeue(&queue)))
3800 __ieee80211_subif_start_xmit(skb, dev, 0);
3801 } else {
3802 __ieee80211_subif_start_xmit(skb, dev, 0);
3803 }
3804
Liad Kaufman24d342c2014-11-09 18:50:07 +02003805 return NETDEV_TX_OK;
3806}
Johannes Berge2ebc742007-07-27 15:43:22 +02003807
Johannes Berg7528ec52014-11-09 18:50:11 +02003808struct sk_buff *
3809ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
3810 struct sk_buff *skb, u32 info_flags)
3811{
3812 struct ieee80211_hdr *hdr;
3813 struct ieee80211_tx_data tx = {
3814 .local = sdata->local,
3815 .sdata = sdata,
3816 };
Johannes Berg97ffe752015-03-21 09:13:45 +01003817 struct sta_info *sta;
Johannes Berg7528ec52014-11-09 18:50:11 +02003818
3819 rcu_read_lock();
3820
Johannes Berg97ffe752015-03-21 09:13:45 +01003821 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
3822 kfree_skb(skb);
3823 skb = ERR_PTR(-EINVAL);
3824 goto out;
3825 }
3826
3827 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta);
Johannes Berg7528ec52014-11-09 18:50:11 +02003828 if (IS_ERR(skb))
3829 goto out;
3830
3831 hdr = (void *)skb->data;
3832 tx.sta = sta_info_get(sdata, hdr->addr1);
3833 tx.skb = skb;
3834
3835 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
3836 rcu_read_unlock();
3837 kfree_skb(skb);
3838 return ERR_PTR(-EINVAL);
3839 }
3840
3841out:
3842 rcu_read_unlock();
3843 return skb;
3844}
3845
Johannes Berge2530082008-05-17 00:57:14 +02003846/*
3847 * ieee80211_clear_tx_pending may not be called in a context where
3848 * it is possible that it packets could come in again.
3849 */
Johannes Berge2ebc742007-07-27 15:43:22 +02003850void ieee80211_clear_tx_pending(struct ieee80211_local *local)
3851{
Felix Fietkau1f98ab72012-11-10 03:44:14 +01003852 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01003853 int i;
Johannes Berge2ebc742007-07-27 15:43:22 +02003854
Felix Fietkau1f98ab72012-11-10 03:44:14 +01003855 for (i = 0; i < local->hw.queues; i++) {
3856 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
3857 ieee80211_free_txskb(&local->hw, skb);
3858 }
Johannes Berge2ebc742007-07-27 15:43:22 +02003859}
3860
Johannes Berg7bb45682011-02-24 14:42:06 +01003861/*
3862 * Returns false if the frame couldn't be transmitted but was queued instead,
3863 * which in this case means re-queued -- take as an indication to stop sending
3864 * more pending frames.
3865 */
Johannes Bergcd8ffc82009-03-23 17:28:41 +01003866static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
3867 struct sk_buff *skb)
3868{
3869 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3870 struct ieee80211_sub_if_data *sdata;
3871 struct sta_info *sta;
3872 struct ieee80211_hdr *hdr;
Johannes Berg7bb45682011-02-24 14:42:06 +01003873 bool result;
Johannes Berg55de9082012-07-26 17:24:39 +02003874 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01003875
Johannes Berg5061b0c2009-07-14 00:33:34 +02003876 sdata = vif_to_sdata(info->control.vif);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01003877
3878 if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
Johannes Berg55de9082012-07-26 17:24:39 +02003879 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3880 if (unlikely(!chanctx_conf)) {
3881 dev_kfree_skb(skb);
3882 return true;
3883 }
Johannes Berg73c4e192014-11-09 18:50:09 +02003884 info->band = chanctx_conf->def.chan->band;
Johannes Bergb9771d42018-05-28 15:47:41 +02003885 result = ieee80211_tx(sdata, NULL, skb, true, 0);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01003886 } else {
Johannes Berg252b86c2011-11-16 15:28:55 +01003887 struct sk_buff_head skbs;
3888
3889 __skb_queue_head_init(&skbs);
3890 __skb_queue_tail(&skbs, skb);
3891
Johannes Bergcd8ffc82009-03-23 17:28:41 +01003892 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Bergabe60632009-11-25 17:46:18 +01003893 sta = sta_info_get(sdata, hdr->addr1);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01003894
Johannes Berg74e4dbf2011-11-16 15:28:57 +01003895 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01003896 }
3897
Johannes Bergcd8ffc82009-03-23 17:28:41 +01003898 return result;
3899}
3900
Johannes Berge2530082008-05-17 00:57:14 +02003901/*
Johannes Berg3b8d81e02009-06-17 17:43:56 +02003902 * Transmit all pending packets. Called from tasklet.
Johannes Berge2530082008-05-17 00:57:14 +02003903 */
Johannes Berge2ebc742007-07-27 15:43:22 +02003904void ieee80211_tx_pending(unsigned long data)
3905{
3906 struct ieee80211_local *local = (struct ieee80211_local *)data;
Johannes Berg2a577d92009-03-23 17:28:37 +01003907 unsigned long flags;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01003908 int i;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02003909 bool txok;
Johannes Berge2ebc742007-07-27 15:43:22 +02003910
Johannes Bergf0e72852009-03-23 17:28:36 +01003911 rcu_read_lock();
Johannes Berg2a577d92009-03-23 17:28:37 +01003912
Johannes Berg3b8d81e02009-06-17 17:43:56 +02003913 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg176be722009-03-12 23:49:28 +01003914 for (i = 0; i < local->hw.queues; i++) {
Johannes Berg2a577d92009-03-23 17:28:37 +01003915 /*
3916 * If queue is stopped by something other than due to pending
3917 * frames, or we have no pending frames, proceed to next queue.
3918 */
Johannes Berg3b8d81e02009-06-17 17:43:56 +02003919 if (local->queue_stop_reasons[i] ||
Johannes Berg2a577d92009-03-23 17:28:37 +01003920 skb_queue_empty(&local->pending[i]))
Johannes Berge2ebc742007-07-27 15:43:22 +02003921 continue;
Johannes Berge2530082008-05-17 00:57:14 +02003922
Johannes Berg2a577d92009-03-23 17:28:37 +01003923 while (!skb_queue_empty(&local->pending[i])) {
Johannes Berg3b8d81e02009-06-17 17:43:56 +02003924 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
Johannes Berg5061b0c2009-07-14 00:33:34 +02003925 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg5061b0c2009-07-14 00:33:34 +02003926
Johannes Berga7bc3762009-07-27 10:33:31 +02003927 if (WARN_ON(!info->control.vif)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02003928 ieee80211_free_txskb(&local->hw, skb);
Johannes Berga7bc3762009-07-27 10:33:31 +02003929 continue;
3930 }
3931
Johannes Berg3b8d81e02009-06-17 17:43:56 +02003932 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
3933 flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01003934
Johannes Berg3b8d81e02009-06-17 17:43:56 +02003935 txok = ieee80211_tx_pending_skb(local, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02003936 spin_lock_irqsave(&local->queue_stop_reason_lock,
3937 flags);
3938 if (!txok)
Johannes Berg2a577d92009-03-23 17:28:37 +01003939 break;
Johannes Berge2ebc742007-07-27 15:43:22 +02003940 }
Johannes Berg7236fe22010-03-22 13:42:43 -07003941
3942 if (skb_queue_empty(&local->pending[i]))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02003943 ieee80211_propagate_queue_wake(local, i);
Johannes Berge2ebc742007-07-27 15:43:22 +02003944 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02003945 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01003946
Johannes Bergf0e72852009-03-23 17:28:36 +01003947 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02003948}
3949
3950/* functions for drivers to get certain frames */
3951
Marco Porscheac70c12013-01-07 16:04:50 +01003952static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03003953 struct ps_data *ps, struct sk_buff *skb,
3954 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02003955{
3956 u8 *pos, *tim;
3957 int aid0 = 0;
3958 int i, have_bits = 0, n1, n2;
3959
3960 /* Generate bitmap for TIM only if there are any STAs in power save
3961 * mode. */
Marco Porschd012a602012-10-10 12:39:50 -07003962 if (atomic_read(&ps->num_sta_ps) > 0)
Johannes Berge2ebc742007-07-27 15:43:22 +02003963 /* in the hope that this is faster than
3964 * checking byte-for-byte */
Weilong Chenf359d3f2013-12-18 15:44:16 +08003965 have_bits = !bitmap_empty((unsigned long *)ps->tim,
Johannes Berge2ebc742007-07-27 15:43:22 +02003966 IEEE80211_MAX_AID+1);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03003967 if (!is_template) {
3968 if (ps->dtim_count == 0)
3969 ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
3970 else
3971 ps->dtim_count--;
3972 }
Johannes Berge2ebc742007-07-27 15:43:22 +02003973
Johannes Berg4df864c2017-06-16 14:29:21 +02003974 tim = pos = skb_put(skb, 6);
Johannes Berge2ebc742007-07-27 15:43:22 +02003975 *pos++ = WLAN_EID_TIM;
3976 *pos++ = 4;
Marco Porschd012a602012-10-10 12:39:50 -07003977 *pos++ = ps->dtim_count;
Johannes Berg88600202012-02-13 15:17:18 +01003978 *pos++ = sdata->vif.bss_conf.dtim_period;
Johannes Berge2ebc742007-07-27 15:43:22 +02003979
Marco Porschd012a602012-10-10 12:39:50 -07003980 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
Johannes Berge2ebc742007-07-27 15:43:22 +02003981 aid0 = 1;
3982
Marco Porschd012a602012-10-10 12:39:50 -07003983 ps->dtim_bc_mc = aid0 == 1;
Christian Lamparter512119b2011-01-31 20:48:44 +02003984
Johannes Berge2ebc742007-07-27 15:43:22 +02003985 if (have_bits) {
3986 /* Find largest even number N1 so that bits numbered 1 through
3987 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
3988 * (N2 + 1) x 8 through 2007 are 0. */
3989 n1 = 0;
3990 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
Marco Porschd012a602012-10-10 12:39:50 -07003991 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02003992 n1 = i & 0xfe;
3993 break;
3994 }
3995 }
3996 n2 = n1;
3997 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
Marco Porschd012a602012-10-10 12:39:50 -07003998 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02003999 n2 = i;
4000 break;
4001 }
4002 }
4003
4004 /* Bitmap control */
4005 *pos++ = n1 | aid0;
4006 /* Part Virt Bitmap */
Eliad Peller5220da32011-11-24 16:50:00 +02004007 skb_put(skb, n2 - n1);
Marco Porschd012a602012-10-10 12:39:50 -07004008 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
Johannes Berge2ebc742007-07-27 15:43:22 +02004009
4010 tim[1] = n2 - n1 + 4;
Johannes Berge2ebc742007-07-27 15:43:22 +02004011 } else {
4012 *pos++ = aid0; /* Bitmap control */
4013 *pos++ = 0; /* Part Virt Bitmap */
4014 }
Johannes Berge2ebc742007-07-27 15:43:22 +02004015}
4016
Marco Porscheac70c12013-01-07 16:04:50 +01004017static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004018 struct ps_data *ps, struct sk_buff *skb,
4019 bool is_template)
Marco Porscheac70c12013-01-07 16:04:50 +01004020{
4021 struct ieee80211_local *local = sdata->local;
4022
4023 /*
4024 * Not very nice, but we want to allow the driver to call
4025 * ieee80211_beacon_get() as a response to the set_tim()
4026 * callback. That, however, is already invoked under the
4027 * sta_lock to guarantee consistent and race-free update
4028 * of the tim bitmap in mac80211 and the driver.
4029 */
4030 if (local->tim_in_locked_section) {
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004031 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Marco Porscheac70c12013-01-07 16:04:50 +01004032 } else {
Johannes Berg1b917312013-02-22 12:55:01 +01004033 spin_lock_bh(&local->tim_lock);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004034 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Johannes Berg1b917312013-02-22 12:55:01 +01004035 spin_unlock_bh(&local->tim_lock);
Marco Porscheac70c12013-01-07 16:04:50 +01004036 }
4037
4038 return 0;
4039}
4040
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004041static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
4042 struct beacon_data *beacon)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004043{
4044 struct probe_resp *resp;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004045 u8 *beacon_data;
4046 size_t beacon_data_len;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004047 int i;
Michal Kazioraf296bd2014-06-05 14:21:36 +02004048 u8 count = beacon->csa_current_counter;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004049
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004050 switch (sdata->vif.type) {
4051 case NL80211_IFTYPE_AP:
4052 beacon_data = beacon->tail;
4053 beacon_data_len = beacon->tail_len;
4054 break;
4055 case NL80211_IFTYPE_ADHOC:
4056 beacon_data = beacon->head;
4057 beacon_data_len = beacon->head_len;
4058 break;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004059 case NL80211_IFTYPE_MESH_POINT:
4060 beacon_data = beacon->head;
4061 beacon_data_len = beacon->head_len;
4062 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004063 default:
4064 return;
4065 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004066
Michal Kazioraf296bd2014-06-05 14:21:36 +02004067 rcu_read_lock();
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004068 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004069 resp = rcu_dereference(sdata->u.ap.probe_resp);
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004070
Michal Kazioraf296bd2014-06-05 14:21:36 +02004071 if (beacon->csa_counter_offsets[i]) {
4072 if (WARN_ON_ONCE(beacon->csa_counter_offsets[i] >=
4073 beacon_data_len)) {
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004074 rcu_read_unlock();
4075 return;
4076 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004077
4078 beacon_data[beacon->csa_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004079 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004080
4081 if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
4082 resp->data[resp->csa_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03004083 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02004084 rcu_read_unlock();
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004085}
4086
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004087static u8 __ieee80211_csa_update_counter(struct beacon_data *beacon)
4088{
4089 beacon->csa_current_counter--;
4090
4091 /* the counter should never reach 0 */
4092 WARN_ON_ONCE(!beacon->csa_current_counter);
4093
4094 return beacon->csa_current_counter;
4095}
4096
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004097u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
4098{
4099 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004100 struct beacon_data *beacon = NULL;
4101 u8 count = 0;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004102
Michal Kazioraf296bd2014-06-05 14:21:36 +02004103 rcu_read_lock();
4104
4105 if (sdata->vif.type == NL80211_IFTYPE_AP)
4106 beacon = rcu_dereference(sdata->u.ap.beacon);
4107 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4108 beacon = rcu_dereference(sdata->u.ibss.presp);
4109 else if (ieee80211_vif_is_mesh(&sdata->vif))
4110 beacon = rcu_dereference(sdata->u.mesh.beacon);
4111
4112 if (!beacon)
4113 goto unlock;
4114
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004115 count = __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004116
Michal Kazioraf296bd2014-06-05 14:21:36 +02004117unlock:
4118 rcu_read_unlock();
4119 return count;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004120}
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004121EXPORT_SYMBOL(ieee80211_csa_update_counter);
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004122
Gregory Greenman03737002018-04-20 13:49:24 +03004123void ieee80211_csa_set_counter(struct ieee80211_vif *vif, u8 counter)
4124{
4125 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4126 struct beacon_data *beacon = NULL;
4127
4128 rcu_read_lock();
4129
4130 if (sdata->vif.type == NL80211_IFTYPE_AP)
4131 beacon = rcu_dereference(sdata->u.ap.beacon);
4132 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4133 beacon = rcu_dereference(sdata->u.ibss.presp);
4134 else if (ieee80211_vif_is_mesh(&sdata->vif))
4135 beacon = rcu_dereference(sdata->u.mesh.beacon);
4136
4137 if (!beacon)
4138 goto unlock;
4139
4140 if (counter < beacon->csa_current_counter)
4141 beacon->csa_current_counter = counter;
4142
4143unlock:
4144 rcu_read_unlock();
4145}
4146EXPORT_SYMBOL(ieee80211_csa_set_counter);
4147
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004148bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
4149{
4150 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4151 struct beacon_data *beacon = NULL;
4152 u8 *beacon_data;
4153 size_t beacon_data_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004154 int ret = false;
4155
4156 if (!ieee80211_sdata_running(sdata))
4157 return false;
4158
4159 rcu_read_lock();
4160 if (vif->type == NL80211_IFTYPE_AP) {
4161 struct ieee80211_if_ap *ap = &sdata->u.ap;
4162
4163 beacon = rcu_dereference(ap->beacon);
4164 if (WARN_ON(!beacon || !beacon->tail))
4165 goto out;
4166 beacon_data = beacon->tail;
4167 beacon_data_len = beacon->tail_len;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004168 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
4169 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4170
4171 beacon = rcu_dereference(ifibss->presp);
4172 if (!beacon)
4173 goto out;
4174
4175 beacon_data = beacon->head;
4176 beacon_data_len = beacon->head_len;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004177 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
4178 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4179
4180 beacon = rcu_dereference(ifmsh->beacon);
4181 if (!beacon)
4182 goto out;
4183
4184 beacon_data = beacon->head;
4185 beacon_data_len = beacon->head_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004186 } else {
4187 WARN_ON(1);
4188 goto out;
4189 }
4190
Michal Kazior10d78f22014-06-05 14:21:37 +02004191 if (!beacon->csa_counter_offsets[0])
4192 goto out;
4193
Michal Kazioraf296bd2014-06-05 14:21:36 +02004194 if (WARN_ON_ONCE(beacon->csa_counter_offsets[0] > beacon_data_len))
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004195 goto out;
4196
Michal Kazioraf296bd2014-06-05 14:21:36 +02004197 if (beacon_data[beacon->csa_counter_offsets[0]] == 1)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004198 ret = true;
4199 out:
4200 rcu_read_unlock();
4201
4202 return ret;
4203}
4204EXPORT_SYMBOL(ieee80211_csa_is_complete);
4205
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004206static struct sk_buff *
4207__ieee80211_beacon_get(struct ieee80211_hw *hw,
4208 struct ieee80211_vif *vif,
4209 struct ieee80211_mutable_offsets *offs,
4210 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02004211{
4212 struct ieee80211_local *local = hw_to_local(hw);
Michal Kazioraf296bd2014-06-05 14:21:36 +02004213 struct beacon_data *beacon = NULL;
Johannes Berg9d139c82008-07-09 14:40:37 +02004214 struct sk_buff *skb = NULL;
Johannes Berge039fa42008-05-15 12:55:29 +02004215 struct ieee80211_tx_info *info;
Johannes Berge2ebc742007-07-27 15:43:22 +02004216 struct ieee80211_sub_if_data *sdata = NULL;
Johannes Berg57fbcce2016-04-12 15:56:15 +02004217 enum nl80211_band band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004218 struct ieee80211_tx_rate_control txrc;
Johannes Berg55de9082012-07-26 17:24:39 +02004219 struct ieee80211_chanctx_conf *chanctx_conf;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004220 int csa_off_base = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004221
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004222 rcu_read_lock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004223
Johannes Berg32bfd352007-12-19 01:31:26 +01004224 sdata = vif_to_sdata(vif);
Johannes Berg55de9082012-07-26 17:24:39 +02004225 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berge2ebc742007-07-27 15:43:22 +02004226
Johannes Berg55de9082012-07-26 17:24:39 +02004227 if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
Felix Fietkaueb3e5542011-01-24 19:28:49 +01004228 goto out;
4229
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004230 if (offs)
4231 memset(offs, 0, sizeof(*offs));
Johannes Bergeddcbb942009-10-29 08:30:35 +01004232
Johannes Berg05c914f2008-09-11 00:01:58 +02004233 if (sdata->vif.type == NL80211_IFTYPE_AP) {
Marco Porschd012a602012-10-10 12:39:50 -07004234 struct ieee80211_if_ap *ap = &sdata->u.ap;
Marco Porschd012a602012-10-10 12:39:50 -07004235
Michal Kazioraf296bd2014-06-05 14:21:36 +02004236 beacon = rcu_dereference(ap->beacon);
Dan Carpenter3ad97fb2011-02-07 22:03:35 +03004237 if (beacon) {
Michal Kazior10d78f22014-06-05 14:21:37 +02004238 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004239 if (!is_template)
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004240 __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004241
4242 ieee80211_set_csa(sdata, beacon);
4243 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02004244
Johannes Berg902acc72008-02-23 15:17:19 +01004245 /*
4246 * headroom, head length,
4247 * tail length and maximum TIM length
4248 */
4249 skb = dev_alloc_skb(local->tx_headroom +
4250 beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004251 beacon->tail_len + 256 +
4252 local->hw.extra_beacon_tailroom);
Johannes Berg902acc72008-02-23 15:17:19 +01004253 if (!skb)
4254 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004255
Johannes Berg902acc72008-02-23 15:17:19 +01004256 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004257 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg902acc72008-02-23 15:17:19 +01004258
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004259 ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
4260 is_template);
Johannes Berg902acc72008-02-23 15:17:19 +01004261
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004262 if (offs) {
4263 offs->tim_offset = beacon->head_len;
4264 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004265
4266 /* for AP the csa offsets are from tail */
4267 csa_off_base = skb->len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004268 }
Johannes Bergeddcbb942009-10-29 08:30:35 +01004269
Johannes Berg902acc72008-02-23 15:17:19 +01004270 if (beacon->tail)
Johannes Berg59ae1d12017-06-16 14:29:20 +02004271 skb_put_data(skb, beacon->tail,
4272 beacon->tail_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004273 } else
4274 goto out;
Johannes Berg05c914f2008-09-11 00:01:58 +02004275 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Berg46900292009-02-15 12:44:28 +01004276 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg9d139c82008-07-09 14:40:37 +02004277 struct ieee80211_hdr *hdr;
Johannes Berg902acc72008-02-23 15:17:19 +01004278
Michal Kazioraf296bd2014-06-05 14:21:36 +02004279 beacon = rcu_dereference(ifibss->presp);
4280 if (!beacon)
Johannes Berg9d139c82008-07-09 14:40:37 +02004281 goto out;
4282
Michal Kazior10d78f22014-06-05 14:21:37 +02004283 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004284 if (!is_template)
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004285 __ieee80211_csa_update_counter(beacon);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004286
Michal Kazioraf296bd2014-06-05 14:21:36 +02004287 ieee80211_set_csa(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004288 }
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02004289
Michal Kazioraf296bd2014-06-05 14:21:36 +02004290 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004291 local->hw.extra_beacon_tailroom);
Johannes Berg9d139c82008-07-09 14:40:37 +02004292 if (!skb)
4293 goto out;
Johannes Bergc3ffeab2013-03-07 20:54:29 +01004294 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004295 skb_put_data(skb, beacon->head, beacon->head_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004296
4297 hdr = (struct ieee80211_hdr *) skb->data;
Harvey Harrisone7827a72008-07-15 18:44:13 -07004298 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4299 IEEE80211_STYPE_BEACON);
Johannes Berg902acc72008-02-23 15:17:19 +01004300 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
Javier Cardonadbf498f2012-03-31 11:31:32 -07004301 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg472dbc42008-09-11 00:01:49 +02004302
Michal Kazioraf296bd2014-06-05 14:21:36 +02004303 beacon = rcu_dereference(ifmsh->beacon);
4304 if (!beacon)
Johannes Bergac1bd842011-01-18 13:45:32 +01004305 goto out;
Johannes Bergac1bd842011-01-18 13:45:32 +01004306
Michal Kazior10d78f22014-06-05 14:21:37 +02004307 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004308 if (!is_template)
4309 /* TODO: For mesh csa_counter is in TU, so
4310 * decrementing it by one isn't correct, but
4311 * for now we leave it consistent with overall
4312 * mac80211's behavior.
4313 */
Wojciech Dubowike996ec22015-06-10 13:06:53 +02004314 __ieee80211_csa_update_counter(beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004315
Michal Kazioraf296bd2014-06-05 14:21:36 +02004316 ieee80211_set_csa(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004317 }
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07004318
Javier Cardonadbf498f2012-03-31 11:31:32 -07004319 if (ifmsh->sync_ops)
Masashi Honma445cd452016-12-08 10:15:51 +09004320 ifmsh->sync_ops->adjust_tsf(sdata, beacon);
Javier Cardonadbf498f2012-03-31 11:31:32 -07004321
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -07004322 skb = dev_alloc_skb(local->tx_headroom +
Michal Kazioraf296bd2014-06-05 14:21:36 +02004323 beacon->head_len +
Marco Porsch3f52b7e2013-01-30 18:14:08 +01004324 256 + /* TIM IE */
Michal Kazioraf296bd2014-06-05 14:21:36 +02004325 beacon->tail_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01004326 local->hw.extra_beacon_tailroom);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01004327 if (!skb)
4328 goto out;
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08004329 skb_reserve(skb, local->tx_headroom);
Johannes Berg59ae1d12017-06-16 14:29:20 +02004330 skb_put_data(skb, beacon->head, beacon->head_len);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004331 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
4332
4333 if (offs) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004334 offs->tim_offset = beacon->head_len;
4335 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004336 }
4337
Johannes Berg59ae1d12017-06-16 14:29:20 +02004338 skb_put_data(skb, beacon->tail, beacon->tail_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02004339 } else {
4340 WARN_ON(1);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004341 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02004342 }
4343
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004344 /* CSA offsets */
Michal Kazioraf296bd2014-06-05 14:21:36 +02004345 if (offs && beacon) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004346 int i;
4347
4348 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02004349 u16 csa_off = beacon->csa_counter_offsets[i];
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03004350
4351 if (!csa_off)
4352 continue;
4353
4354 offs->csa_counter_offs[i] = csa_off_base + csa_off;
4355 }
4356 }
4357
Johannes Berg4bf88532012-11-09 11:39:59 +01004358 band = chanctx_conf->def.chan->band;
Johannes Berg55de9082012-07-26 17:24:39 +02004359
Johannes Berge039fa42008-05-15 12:55:29 +02004360 info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02004361
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004362 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004363 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Johannes Berge039fa42008-05-15 12:55:29 +02004364 info->band = band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004365
4366 memset(&txrc, 0, sizeof(txrc));
4367 txrc.hw = hw;
Johannes Berge31583c2012-07-26 14:07:46 +02004368 txrc.sband = local->hw.wiphy->bands[band];
Jouni Malinene00cfce2009-12-29 12:59:19 +02004369 txrc.bss_conf = &sdata->vif.bss_conf;
4370 txrc.skb = skb;
4371 txrc.reported_rate.idx = -1;
Jouni Malinen37eb0b12010-01-06 13:09:08 +02004372 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
Felix Fietkau8f0729b2010-11-11 15:07:23 +01004373 txrc.bss = true;
Jouni Malinene00cfce2009-12-29 12:59:19 +02004374 rate_control_get_rate(sdata, NULL, &txrc);
Johannes Berge039fa42008-05-15 12:55:29 +02004375
4376 info->control.vif = vif;
Johannes Bergf591fa52008-07-10 11:21:26 +02004377
John W. Linvillea472e712010-05-06 14:45:17 -04004378 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
4379 IEEE80211_TX_CTL_ASSIGN_SEQ |
4380 IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge6a98542008-10-21 12:40:02 +02004381 out:
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004382 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004383 return skb;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004384
4385}
4386
4387struct sk_buff *
4388ieee80211_beacon_get_template(struct ieee80211_hw *hw,
4389 struct ieee80211_vif *vif,
4390 struct ieee80211_mutable_offsets *offs)
4391{
4392 return __ieee80211_beacon_get(hw, vif, offs, true);
4393}
4394EXPORT_SYMBOL(ieee80211_beacon_get_template);
4395
4396struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
4397 struct ieee80211_vif *vif,
4398 u16 *tim_offset, u16 *tim_length)
4399{
4400 struct ieee80211_mutable_offsets offs = {};
4401 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
Helmut Schaa35afa582015-09-09 09:46:32 +02004402 struct sk_buff *copy;
4403 struct ieee80211_supported_band *sband;
4404 int shift;
4405
4406 if (!bcn)
4407 return bcn;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004408
4409 if (tim_offset)
4410 *tim_offset = offs.tim_offset;
4411
4412 if (tim_length)
4413 *tim_length = offs.tim_length;
4414
Helmut Schaa35afa582015-09-09 09:46:32 +02004415 if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
4416 !hw_to_local(hw)->monitors)
4417 return bcn;
4418
4419 /* send a copy to monitor interfaces */
4420 copy = skb_copy(bcn, GFP_ATOMIC);
4421 if (!copy)
4422 return bcn;
4423
4424 shift = ieee80211_vif_get_shift(vif);
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +05304425 sband = ieee80211_get_sband(vif_to_sdata(vif));
4426 if (!sband)
4427 return bcn;
4428
Helmut Schaa35afa582015-09-09 09:46:32 +02004429 ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false);
4430
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03004431 return bcn;
Johannes Berge2ebc742007-07-27 15:43:22 +02004432}
Johannes Bergeddcbb942009-10-29 08:30:35 +01004433EXPORT_SYMBOL(ieee80211_beacon_get_tim);
Johannes Berge2ebc742007-07-27 15:43:22 +02004434
Arik Nemtsov02945822011-11-10 11:28:57 +02004435struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
4436 struct ieee80211_vif *vif)
4437{
4438 struct ieee80211_if_ap *ap = NULL;
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004439 struct sk_buff *skb = NULL;
4440 struct probe_resp *presp = NULL;
Arik Nemtsov02945822011-11-10 11:28:57 +02004441 struct ieee80211_hdr *hdr;
4442 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4443
4444 if (sdata->vif.type != NL80211_IFTYPE_AP)
4445 return NULL;
4446
4447 rcu_read_lock();
4448
4449 ap = &sdata->u.ap;
4450 presp = rcu_dereference(ap->probe_resp);
4451 if (!presp)
4452 goto out;
4453
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004454 skb = dev_alloc_skb(presp->len);
Arik Nemtsov02945822011-11-10 11:28:57 +02004455 if (!skb)
4456 goto out;
4457
Johannes Berg59ae1d12017-06-16 14:29:20 +02004458 skb_put_data(skb, presp->data, presp->len);
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03004459
Arik Nemtsov02945822011-11-10 11:28:57 +02004460 hdr = (struct ieee80211_hdr *) skb->data;
4461 memset(hdr->addr1, 0, sizeof(hdr->addr1));
4462
4463out:
4464 rcu_read_unlock();
4465 return skb;
4466}
4467EXPORT_SYMBOL(ieee80211_proberesp_get);
4468
Kalle Valo7044cc52010-01-05 20:16:19 +02004469struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
4470 struct ieee80211_vif *vif)
4471{
4472 struct ieee80211_sub_if_data *sdata;
4473 struct ieee80211_if_managed *ifmgd;
4474 struct ieee80211_pspoll *pspoll;
4475 struct ieee80211_local *local;
4476 struct sk_buff *skb;
4477
4478 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4479 return NULL;
4480
4481 sdata = vif_to_sdata(vif);
4482 ifmgd = &sdata->u.mgd;
4483 local = sdata->local;
4484
4485 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
Joe Perchesd15b8452011-08-29 14:17:31 -07004486 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02004487 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07004488
Kalle Valo7044cc52010-01-05 20:16:19 +02004489 skb_reserve(skb, local->hw.extra_tx_headroom);
4490
Johannes Bergb080db52017-06-16 14:29:19 +02004491 pspoll = skb_put_zero(skb, sizeof(*pspoll));
Kalle Valo7044cc52010-01-05 20:16:19 +02004492 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
4493 IEEE80211_STYPE_PSPOLL);
4494 pspoll->aid = cpu_to_le16(ifmgd->aid);
4495
4496 /* aid in PS-Poll has its two MSBs each set to 1 */
4497 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
4498
4499 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
4500 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
4501
4502 return skb;
4503}
4504EXPORT_SYMBOL(ieee80211_pspoll_get);
4505
4506struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004507 struct ieee80211_vif *vif,
4508 bool qos_ok)
Kalle Valo7044cc52010-01-05 20:16:19 +02004509{
4510 struct ieee80211_hdr_3addr *nullfunc;
4511 struct ieee80211_sub_if_data *sdata;
4512 struct ieee80211_if_managed *ifmgd;
4513 struct ieee80211_local *local;
4514 struct sk_buff *skb;
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004515 bool qos = false;
Kalle Valo7044cc52010-01-05 20:16:19 +02004516
4517 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4518 return NULL;
4519
4520 sdata = vif_to_sdata(vif);
4521 ifmgd = &sdata->u.mgd;
4522 local = sdata->local;
4523
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004524 if (qos_ok) {
4525 struct sta_info *sta;
4526
4527 rcu_read_lock();
4528 sta = sta_info_get(sdata, ifmgd->bssid);
4529 qos = sta && sta->sta.wme;
4530 rcu_read_unlock();
4531 }
4532
4533 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
4534 sizeof(*nullfunc) + 2);
Joe Perchesd15b8452011-08-29 14:17:31 -07004535 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02004536 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07004537
Kalle Valo7044cc52010-01-05 20:16:19 +02004538 skb_reserve(skb, local->hw.extra_tx_headroom);
4539
Johannes Bergb080db52017-06-16 14:29:19 +02004540 nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
Kalle Valo7044cc52010-01-05 20:16:19 +02004541 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
4542 IEEE80211_STYPE_NULLFUNC |
4543 IEEE80211_FCTL_TODS);
Johannes Berg7b6ddea2017-11-21 14:46:08 +01004544 if (qos) {
4545 __le16 qos = cpu_to_le16(7);
4546
4547 BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
4548 IEEE80211_STYPE_NULLFUNC) !=
4549 IEEE80211_STYPE_QOS_NULLFUNC);
4550 nullfunc->frame_control |=
4551 cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
4552 skb->priority = 7;
4553 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
4554 skb_put_data(skb, &qos, sizeof(qos));
4555 }
4556
Kalle Valo7044cc52010-01-05 20:16:19 +02004557 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
4558 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
4559 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
4560
4561 return skb;
4562}
4563EXPORT_SYMBOL(ieee80211_nullfunc_get);
4564
Kalle Valo05e54ea2010-01-05 20:16:38 +02004565struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
Johannes Berga344d672014-06-12 22:24:31 +02004566 const u8 *src_addr,
Kalle Valo05e54ea2010-01-05 20:16:38 +02004567 const u8 *ssid, size_t ssid_len,
Johannes Bergb9a9ada2012-11-29 13:00:10 +01004568 size_t tailroom)
Kalle Valo05e54ea2010-01-05 20:16:38 +02004569{
Johannes Berga344d672014-06-12 22:24:31 +02004570 struct ieee80211_local *local = hw_to_local(hw);
Kalle Valo05e54ea2010-01-05 20:16:38 +02004571 struct ieee80211_hdr_3addr *hdr;
4572 struct sk_buff *skb;
4573 size_t ie_ssid_len;
4574 u8 *pos;
4575
Kalle Valo05e54ea2010-01-05 20:16:38 +02004576 ie_ssid_len = 2 + ssid_len;
4577
4578 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
Johannes Bergb9a9ada2012-11-29 13:00:10 +01004579 ie_ssid_len + tailroom);
Joe Perchesd15b8452011-08-29 14:17:31 -07004580 if (!skb)
Kalle Valo05e54ea2010-01-05 20:16:38 +02004581 return NULL;
Kalle Valo05e54ea2010-01-05 20:16:38 +02004582
4583 skb_reserve(skb, local->hw.extra_tx_headroom);
4584
Johannes Bergb080db52017-06-16 14:29:19 +02004585 hdr = skb_put_zero(skb, sizeof(*hdr));
Kalle Valo05e54ea2010-01-05 20:16:38 +02004586 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4587 IEEE80211_STYPE_PROBE_REQ);
Johannes Berge83e6542012-07-13 16:23:07 +02004588 eth_broadcast_addr(hdr->addr1);
Johannes Berga344d672014-06-12 22:24:31 +02004589 memcpy(hdr->addr2, src_addr, ETH_ALEN);
Johannes Berge83e6542012-07-13 16:23:07 +02004590 eth_broadcast_addr(hdr->addr3);
Kalle Valo05e54ea2010-01-05 20:16:38 +02004591
4592 pos = skb_put(skb, ie_ssid_len);
4593 *pos++ = WLAN_EID_SSID;
4594 *pos++ = ssid_len;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02004595 if (ssid_len)
Kalle Valo05e54ea2010-01-05 20:16:38 +02004596 memcpy(pos, ssid, ssid_len);
4597 pos += ssid_len;
4598
Kalle Valo05e54ea2010-01-05 20:16:38 +02004599 return skb;
4600}
4601EXPORT_SYMBOL(ieee80211_probereq_get);
4602
Johannes Berg32bfd352007-12-19 01:31:26 +01004603void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02004604 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02004605 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02004606 struct ieee80211_rts *rts)
4607{
4608 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02004609
Harvey Harrison065e96052008-06-22 16:45:27 -07004610 rts->frame_control =
4611 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01004612 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
4613 frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02004614 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
4615 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
4616}
4617EXPORT_SYMBOL(ieee80211_rts_get);
4618
Johannes Berg32bfd352007-12-19 01:31:26 +01004619void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02004620 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02004621 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02004622 struct ieee80211_cts *cts)
4623{
4624 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02004625
Harvey Harrison065e96052008-06-22 16:45:27 -07004626 cts->frame_control =
4627 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01004628 cts->duration = ieee80211_ctstoself_duration(hw, vif,
4629 frame_len, frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02004630 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
4631}
4632EXPORT_SYMBOL(ieee80211_ctstoself_get);
4633
4634struct sk_buff *
Johannes Berg32bfd352007-12-19 01:31:26 +01004635ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
Johannes Berge039fa42008-05-15 12:55:29 +02004636 struct ieee80211_vif *vif)
Johannes Berge2ebc742007-07-27 15:43:22 +02004637{
4638 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004639 struct sk_buff *skb = NULL;
Johannes Berg5cf121c2008-02-25 16:27:43 +01004640 struct ieee80211_tx_data tx;
Johannes Berge2ebc742007-07-27 15:43:22 +02004641 struct ieee80211_sub_if_data *sdata;
Marco Porschd012a602012-10-10 12:39:50 -07004642 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +02004643 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02004644 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berge2ebc742007-07-27 15:43:22 +02004645
Johannes Berg32bfd352007-12-19 01:31:26 +01004646 sdata = vif_to_sdata(vif);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004647
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004648 rcu_read_lock();
Johannes Berg55de9082012-07-26 17:24:39 +02004649 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004650
Marco Porschd012a602012-10-10 12:39:50 -07004651 if (!chanctx_conf)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004652 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01004653
Marco Porschd012a602012-10-10 12:39:50 -07004654 if (sdata->vif.type == NL80211_IFTYPE_AP) {
4655 struct beacon_data *beacon =
4656 rcu_dereference(sdata->u.ap.beacon);
4657
4658 if (!beacon || !beacon->head)
4659 goto out;
4660
4661 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01004662 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4663 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07004664 } else {
4665 goto out;
4666 }
4667
4668 if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004669 goto out; /* send buffered bc/mc only after DTIM beacon */
Johannes Berge039fa42008-05-15 12:55:29 +02004670
Johannes Berge2ebc742007-07-27 15:43:22 +02004671 while (1) {
Marco Porschd012a602012-10-10 12:39:50 -07004672 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +02004673 if (!skb)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03004674 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02004675 local->total_ps_buffered--;
4676
Marco Porschd012a602012-10-10 12:39:50 -07004677 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
Johannes Berge2ebc742007-07-27 15:43:22 +02004678 struct ieee80211_hdr *hdr =
4679 (struct ieee80211_hdr *) skb->data;
4680 /* more buffered multicast/broadcast frames ==> set
4681 * MoreData flag in IEEE 802.11 header to inform PS
4682 * STAs */
4683 hdr->frame_control |=
4684 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
4685 }
4686
Michael Braun112c44b2014-03-06 15:08:43 +01004687 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porsch7cbf9d02013-03-01 16:01:18 +01004688 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
Johannes Berg7c107702015-03-20 14:18:27 +01004689 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
Johannes Berge2ebc742007-07-27 15:43:22 +02004690 break;
Felix Fietkau6b07d9c2016-08-02 11:13:41 +02004691 ieee80211_free_txskb(hw, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02004692 }
Johannes Berge039fa42008-05-15 12:55:29 +02004693
4694 info = IEEE80211_SKB_CB(skb);
4695
Johannes Berg5cf121c2008-02-25 16:27:43 +01004696 tx.flags |= IEEE80211_TX_PS_BUFFERED;
Johannes Berg4bf88532012-11-09 11:39:59 +01004697 info->band = chanctx_conf->def.chan->band;
Johannes Berge2ebc742007-07-27 15:43:22 +02004698
Johannes Berg97b045d2008-06-20 01:22:30 +02004699 if (invoke_tx_handlers(&tx))
Johannes Berge2ebc742007-07-27 15:43:22 +02004700 skb = NULL;
Johannes Berg97b045d2008-06-20 01:22:30 +02004701 out:
Johannes Bergd0709a62008-02-25 16:27:46 +01004702 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02004703
4704 return skb;
4705}
4706EXPORT_SYMBOL(ieee80211_get_buffered_bc);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004707
Liad Kaufmanb6da9112014-11-19 13:47:38 +02004708int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
4709{
4710 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
4711 struct ieee80211_sub_if_data *sdata = sta->sdata;
4712 struct ieee80211_local *local = sdata->local;
4713 int ret;
4714 u32 queues;
4715
4716 lockdep_assert_held(&local->sta_mtx);
4717
4718 /* only some cases are supported right now */
4719 switch (sdata->vif.type) {
4720 case NL80211_IFTYPE_STATION:
4721 case NL80211_IFTYPE_AP:
4722 case NL80211_IFTYPE_AP_VLAN:
4723 break;
4724 default:
4725 WARN_ON(1);
4726 return -EINVAL;
4727 }
4728
4729 if (WARN_ON(tid >= IEEE80211_NUM_UPS))
4730 return -EINVAL;
4731
4732 if (sta->reserved_tid == tid) {
4733 ret = 0;
4734 goto out;
4735 }
4736
4737 if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
4738 sdata_err(sdata, "TID reservation already active\n");
4739 ret = -EALREADY;
4740 goto out;
4741 }
4742
4743 ieee80211_stop_vif_queues(sdata->local, sdata,
4744 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
4745
4746 synchronize_net();
4747
4748 /* Tear down BA sessions so we stop aggregating on this TID */
Johannes Berg30686bf2015-06-02 21:39:54 +02004749 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
Liad Kaufmanb6da9112014-11-19 13:47:38 +02004750 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
4751 __ieee80211_stop_tx_ba_session(sta, tid,
4752 AGG_STOP_LOCAL_REQUEST);
4753 }
4754
4755 queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02004756 __ieee80211_flush_queues(local, sdata, queues, false);
Liad Kaufmanb6da9112014-11-19 13:47:38 +02004757
4758 sta->reserved_tid = tid;
4759
4760 ieee80211_wake_vif_queues(local, sdata,
4761 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
4762
Johannes Berg30686bf2015-06-02 21:39:54 +02004763 if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
Liad Kaufmanb6da9112014-11-19 13:47:38 +02004764 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
4765
4766 ret = 0;
4767 out:
4768 return ret;
4769}
4770EXPORT_SYMBOL(ieee80211_reserve_tid);
4771
4772void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
4773{
4774 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
4775 struct ieee80211_sub_if_data *sdata = sta->sdata;
4776
4777 lockdep_assert_held(&sdata->local->sta_mtx);
4778
4779 /* only some cases are supported right now */
4780 switch (sdata->vif.type) {
4781 case NL80211_IFTYPE_STATION:
4782 case NL80211_IFTYPE_AP:
4783 case NL80211_IFTYPE_AP_VLAN:
4784 break;
4785 default:
4786 WARN_ON(1);
4787 return;
4788 }
4789
4790 if (tid != sta->reserved_tid) {
4791 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
4792 return;
4793 }
4794
4795 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
4796}
4797EXPORT_SYMBOL(ieee80211_unreserve_tid);
4798
Johannes Berg55de9082012-07-26 17:24:39 +02004799void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
4800 struct sk_buff *skb, int tid,
Johannes Bergb9771d42018-05-28 15:47:41 +02004801 enum nl80211_band band, u32 txdata_flags)
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004802{
Amadeusz Sławiński731977e2017-01-24 16:42:10 +01004803 int ac = ieee80211_ac_from_tid(tid);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02004804
Zhang Shengjud57a5442016-03-03 01:16:56 +00004805 skb_reset_mac_header(skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02004806 skb_set_queue_mapping(skb, ac);
Helmut Schaacf6bb792011-12-15 10:18:34 +01004807 skb->priority = tid;
Johannes Bergcf0277e2010-01-05 18:00:58 +01004808
Johannes Berg89afe612013-02-13 15:39:57 +01004809 skb->dev = sdata->dev;
4810
Johannes Berg3d34deb2009-06-18 17:25:11 +02004811 /*
4812 * The other path calling ieee80211_xmit is from the tasklet,
4813 * and while we can handle concurrent transmissions locking
4814 * requirements are that we do not come into tx with bhs on.
4815 */
4816 local_bh_disable();
Johannes Berg73c4e192014-11-09 18:50:09 +02004817 IEEE80211_SKB_CB(skb)->band = band;
Johannes Bergb9771d42018-05-28 15:47:41 +02004818 ieee80211_xmit(sdata, NULL, skb, txdata_flags);
Johannes Berg3d34deb2009-06-18 17:25:11 +02004819 local_bh_enable();
Johannes Berg3b8d81e02009-06-17 17:43:56 +02004820}
Denis Kenzior91180642018-03-26 12:52:50 -05004821
4822int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
4823 const u8 *buf, size_t len,
4824 const u8 *dest, __be16 proto, bool unencrypted)
4825{
4826 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4827 struct ieee80211_local *local = sdata->local;
4828 struct sk_buff *skb;
4829 struct ethhdr *ehdr;
4830 u32 flags;
4831
4832 /* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
4833 * or Pre-Authentication
4834 */
4835 if (proto != sdata->control_port_protocol &&
4836 proto != cpu_to_be16(ETH_P_PREAUTH))
4837 return -EINVAL;
4838
4839 if (unencrypted)
4840 flags = IEEE80211_TX_INTFL_DONT_ENCRYPT;
4841 else
4842 flags = 0;
4843
4844 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
4845 sizeof(struct ethhdr) + len);
4846 if (!skb)
4847 return -ENOMEM;
4848
4849 skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
4850
4851 skb_put_data(skb, buf, len);
4852
4853 ehdr = skb_push(skb, sizeof(struct ethhdr));
4854 memcpy(ehdr->h_dest, dest, ETH_ALEN);
4855 memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
4856 ehdr->h_proto = proto;
4857
4858 skb->dev = dev;
4859 skb->protocol = htons(ETH_P_802_3);
4860 skb_reset_network_header(skb);
4861 skb_reset_mac_header(skb);
4862
Denis Kenziore7441c92018-06-19 10:39:50 -05004863 local_bh_disable();
Denis Kenzior91180642018-03-26 12:52:50 -05004864 __ieee80211_subif_start_xmit(skb, skb->dev, flags);
Denis Kenziore7441c92018-06-19 10:39:50 -05004865 local_bh_enable();
Denis Kenzior91180642018-03-26 12:52:50 -05004866
4867 return 0;
4868}