blob: 7e7de811e6ad3190a05e6f75f8bc89af66b66033 [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
Johannes Berge2ebc742007-07-27 15:43:22 +02007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 *
13 * Transmit and frame generation functions.
14 */
15
16#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/etherdevice.h>
20#include <linux/bitmap.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040021#include <linux/rcupdate.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040022#include <linux/export.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070023#include <net/net_namespace.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020024#include <net/ieee80211_radiotap.h>
25#include <net/cfg80211.h>
26#include <net/mac80211.h>
27#include <asm/unaligned.h>
28
29#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020030#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040031#include "led.h"
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +010032#include "mesh.h"
Johannes Berge2ebc742007-07-27 15:43:22 +020033#include "wep.h"
34#include "wpa.h"
35#include "wme.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040036#include "rate.h"
Johannes Berge2ebc742007-07-27 15:43:22 +020037
Johannes Berge2ebc742007-07-27 15:43:22 +020038/* misc utils */
39
Johannes Berg252b86c2011-11-16 15:28:55 +010040static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
41 struct sk_buff *skb, int group_addr,
Johannes Berg03f93c32008-06-25 14:36:27 +030042 int next_frag_len)
Johannes Berge2ebc742007-07-27 15:43:22 +020043{
Simon Wunderlich2103dec2013-07-08 16:55:53 +020044 int rate, mrate, erp, dur, i, shift = 0;
Johannes Berg2e92e6f2008-05-15 12:55:27 +020045 struct ieee80211_rate *txrate;
Johannes Berge2ebc742007-07-27 15:43:22 +020046 struct ieee80211_local *local = tx->local;
Johannes Berg8318d782008-01-24 19:38:38 +010047 struct ieee80211_supported_band *sband;
Harvey Harrison358c8d92008-07-15 18:44:13 -070048 struct ieee80211_hdr *hdr;
Johannes Berg252b86c2011-11-16 15:28:55 +010049 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Simon Wunderlich2103dec2013-07-08 16:55:53 +020050 struct ieee80211_chanctx_conf *chanctx_conf;
51 u32 rate_flags = 0;
52
53 rcu_read_lock();
54 chanctx_conf = rcu_dereference(tx->sdata->vif.chanctx_conf);
55 if (chanctx_conf) {
56 shift = ieee80211_chandef_get_shift(&chanctx_conf->def);
57 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
58 }
59 rcu_read_unlock();
Johannes Berge6a98542008-10-21 12:40:02 +020060
61 /* assume HW handles this */
Felix Fietkau336004e2014-11-21 23:29:14 +010062 if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))
Johannes Berge6a98542008-10-21 12:40:02 +020063 return 0;
64
65 /* uh huh? */
Felix Fietkau0d528d82013-04-22 16:14:41 +020066 if (WARN_ON_ONCE(tx->rate.idx < 0))
Johannes Berge6a98542008-10-21 12:40:02 +020067 return 0;
Johannes Berge2ebc742007-07-27 15:43:22 +020068
Johannes Berg2d565772012-07-23 15:12:51 +020069 sband = local->hw.wiphy->bands[info->band];
Felix Fietkau0d528d82013-04-22 16:14:41 +020070 txrate = &sband->bitrates[tx->rate.idx];
Johannes Berg8318d782008-01-24 19:38:38 +010071
Johannes Berge6a98542008-10-21 12:40:02 +020072 erp = txrate->flags & IEEE80211_RATE_ERP_G;
Johannes Berge2ebc742007-07-27 15:43:22 +020073
74 /*
75 * data and mgmt (except PS Poll):
76 * - during CFP: 32768
77 * - during contention period:
78 * if addr1 is group address: 0
79 * if more fragments = 0 and addr1 is individual address: time to
80 * transmit one ACK plus SIFS
81 * if more fragments = 1 and addr1 is individual address: time to
82 * transmit next fragment plus 2 x ACK plus 3 x SIFS
83 *
84 * IEEE 802.11, 9.6:
85 * - control response frame (CTS or ACK) shall be transmitted using the
86 * same rate as the immediately previous frame in the frame exchange
87 * sequence, if this rate belongs to the PHY mandatory rates, or else
88 * at the highest possible rate belonging to the PHY rates in the
89 * BSSBasicRateSet
90 */
Johannes Berg252b86c2011-11-16 15:28:55 +010091 hdr = (struct ieee80211_hdr *)skb->data;
Harvey Harrison358c8d92008-07-15 18:44:13 -070092 if (ieee80211_is_ctl(hdr->frame_control)) {
Johannes Berge2ebc742007-07-27 15:43:22 +020093 /* TODO: These control frames are not currently sent by
Johannes Bergccd7b362008-09-11 00:01:56 +020094 * mac80211, but should they be implemented, this function
Johannes Berge2ebc742007-07-27 15:43:22 +020095 * needs to be updated to support duration field calculation.
96 *
97 * RTS: time needed to transmit pending data/mgmt frame plus
98 * one CTS frame plus one ACK frame plus 3 x SIFS
99 * CTS: duration of immediately previous RTS minus time
100 * required to transmit CTS and its SIFS
101 * ACK: 0 if immediately previous directed data/mgmt had
102 * more=0, with more=1 duration in ACK frame is duration
103 * from previous frame minus time needed to transmit ACK
104 * and its SIFS
105 * PS Poll: BIT(15) | BIT(14) | aid
106 */
107 return 0;
108 }
109
110 /* data/mgmt */
111 if (0 /* FIX: data/mgmt during CFP */)
Johannes Berg03f93c32008-06-25 14:36:27 +0300112 return cpu_to_le16(32768);
Johannes Berge2ebc742007-07-27 15:43:22 +0200113
114 if (group_addr) /* Group address as the destination - no ACK */
115 return 0;
116
117 /* Individual destination address:
118 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
119 * CTS and ACK frames shall be transmitted using the highest rate in
120 * basic rate set that is less than or equal to the rate of the
121 * immediately previous frame and that is using the same modulation
122 * (CCK or OFDM). If no basic rate set matches with these requirements,
123 * the highest mandatory rate of the PHY that is less than or equal to
124 * the rate of the previous frame is used.
125 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
126 */
127 rate = -1;
Johannes Berg8318d782008-01-24 19:38:38 +0100128 /* use lowest available if everything fails */
129 mrate = sband->bitrates[0].bitrate;
130 for (i = 0; i < sband->n_bitrates; i++) {
131 struct ieee80211_rate *r = &sband->bitrates[i];
132
133 if (r->bitrate > txrate->bitrate)
Johannes Berge2ebc742007-07-27 15:43:22 +0200134 break;
135
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200136 if ((rate_flags & r->flags) != rate_flags)
137 continue;
138
Johannes Bergbda39332008-10-11 01:51:51 +0200139 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200140 rate = DIV_ROUND_UP(r->bitrate, 1 << shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200141
Johannes Berg8318d782008-01-24 19:38:38 +0100142 switch (sband->band) {
143 case IEEE80211_BAND_2GHZ: {
144 u32 flag;
145 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
146 flag = IEEE80211_RATE_MANDATORY_G;
147 else
148 flag = IEEE80211_RATE_MANDATORY_B;
149 if (r->flags & flag)
150 mrate = r->bitrate;
151 break;
152 }
153 case IEEE80211_BAND_5GHZ:
154 if (r->flags & IEEE80211_RATE_MANDATORY_A)
155 mrate = r->bitrate;
156 break;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300157 case IEEE80211_BAND_60GHZ:
158 /* TODO, for now fall through */
Johannes Berg8318d782008-01-24 19:38:38 +0100159 case IEEE80211_NUM_BANDS:
160 WARN_ON(1);
161 break;
162 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200163 }
164 if (rate == -1) {
165 /* No matching basic rate found; use highest suitable mandatory
166 * PHY rate */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200167 rate = DIV_ROUND_UP(mrate, 1 << shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200168 }
169
Simon Wunderlich6674f212011-11-21 21:34:30 +0100170 /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
171 if (ieee80211_is_data_qos(hdr->frame_control) &&
Claudio Pisac26a0e12012-05-28 13:06:25 +0100172 *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
Simon Wunderlich6674f212011-11-21 21:34:30 +0100173 dur = 0;
174 else
175 /* Time needed to transmit ACK
176 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
177 * to closest integer */
Michal Kazior4ee73f32012-04-11 08:47:56 +0200178 dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200179 tx->sdata->vif.bss_conf.use_short_preamble,
180 shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200181
182 if (next_frag_len) {
183 /* Frame is fragmented: duration increases with time needed to
184 * transmit next fragment plus ACK and 2 x SIFS. */
185 dur *= 2; /* ACK + SIFS */
186 /* next fragment */
Michal Kazior4ee73f32012-04-11 08:47:56 +0200187 dur += ieee80211_frame_duration(sband->band, next_frag_len,
Johannes Berg8318d782008-01-24 19:38:38 +0100188 txrate->bitrate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200189 tx->sdata->vif.bss_conf.use_short_preamble,
190 shift);
Johannes Berge2ebc742007-07-27 15:43:22 +0200191 }
192
Johannes Berg03f93c32008-06-25 14:36:27 +0300193 return cpu_to_le16(dur);
Johannes Berge2ebc742007-07-27 15:43:22 +0200194}
195
Johannes Berge2ebc742007-07-27 15:43:22 +0200196/* tx handlers */
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200197static ieee80211_tx_result debug_noinline
198ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
199{
200 struct ieee80211_local *local = tx->local;
Kalle Valo0c742112010-01-12 10:42:53 +0200201 struct ieee80211_if_managed *ifmgd;
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200202
203 /* driver doesn't support power save */
204 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
205 return TX_CONTINUE;
206
207 /* hardware does dynamic power save */
208 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
209 return TX_CONTINUE;
210
211 /* dynamic power save disabled */
212 if (local->hw.conf.dynamic_ps_timeout <= 0)
213 return TX_CONTINUE;
214
215 /* we are scanning, don't enable power save */
216 if (local->scanning)
217 return TX_CONTINUE;
218
219 if (!local->ps_sdata)
220 return TX_CONTINUE;
221
222 /* No point if we're going to suspend */
223 if (local->quiescing)
224 return TX_CONTINUE;
225
Kalle Valo0c742112010-01-12 10:42:53 +0200226 /* dynamic ps is supported only in managed mode */
227 if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
228 return TX_CONTINUE;
229
230 ifmgd = &tx->sdata->u.mgd;
231
232 /*
233 * Don't wakeup from power save if u-apsd is enabled, voip ac has
234 * u-apsd enabled and the frame is in voip class. This effectively
235 * means that even if all access categories have u-apsd enabled, in
236 * practise u-apsd is only used with the voip ac. This is a
237 * workaround for the case when received voip class packets do not
238 * have correct qos tag for some reason, due the network or the
239 * peer application.
240 *
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200241 * Note: ifmgd->uapsd_queues access is racy here. If the value is
Kalle Valo0c742112010-01-12 10:42:53 +0200242 * changed via debugfs, user needs to reassociate manually to have
243 * everything in sync.
244 */
Johannes Berg4875d30d2012-03-27 14:18:37 +0200245 if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
246 (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
247 skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
Kalle Valo0c742112010-01-12 10:42:53 +0200248 return TX_CONTINUE;
249
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200250 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
251 ieee80211_stop_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +0100252 IEEE80211_MAX_QUEUE_MAP,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300253 IEEE80211_QUEUE_STOP_REASON_PS,
254 false);
Vivek Natarajandb285692011-02-18 17:18:03 +0530255 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200256 ieee80211_queue_work(&local->hw,
257 &local->dynamic_ps_disable_work);
258 }
259
Luciano Coelho5db1c072011-05-03 21:40:08 +0300260 /* Don't restart the timer if we're not disassociated */
261 if (!ifmgd->associated)
262 return TX_CONTINUE;
263
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200264 mod_timer(&local->dynamic_ps_timer, jiffies +
265 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
266
267 return TX_CONTINUE;
268}
Johannes Berge2ebc742007-07-27 15:43:22 +0200269
Johannes Bergd9e8a702008-06-30 15:10:44 +0200270static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100271ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200272{
Harvey Harrison358c8d92008-07-15 18:44:13 -0700273
Johannes Berge039fa42008-05-15 12:55:29 +0200274 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200275 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200276 bool assoc = false;
Johannes Berge2ebc742007-07-27 15:43:22 +0200277
Johannes Berge039fa42008-05-15 12:55:29 +0200278 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100279 return TX_CONTINUE;
Johannes Berg58d41852007-09-26 17:53:18 +0200280
Ben Greearb23b0252011-02-04 11:54:17 -0800281 if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
282 test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
Kalle Valoa9a6fff2009-03-18 14:06:44 +0200283 !ieee80211_is_probe_req(hdr->frame_control) &&
284 !ieee80211_is_nullfunc(hdr->frame_control))
285 /*
286 * When software scanning only nullfunc frames (to notify
287 * the sleep state to the AP) and probe requests (for the
288 * active scan) are allowed, all other frames should not be
289 * sent and we should not get here, but if we do
290 * nonetheless, drop them to avoid sending them
291 * off-channel. See the link below and
292 * ieee80211_start_scan() for more.
293 *
294 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
295 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100296 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200297
Rostislav Lisovy239281f2014-11-03 10:33:19 +0100298 if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
299 return TX_CONTINUE;
300
Bill Jordan1be7fe82010-10-01 11:20:41 -0400301 if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
302 return TX_CONTINUE;
303
Johannes Berg05c914f2008-09-11 00:01:58 +0200304 if (tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100305 return TX_CONTINUE;
306
Johannes Berg5cf121c2008-02-25 16:27:43 +0100307 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100308 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200309
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200310 if (tx->sta)
311 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge2ebc742007-07-27 15:43:22 +0200312
Johannes Berg5cf121c2008-02-25 16:27:43 +0100313 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200314 if (unlikely(!assoc &&
Harvey Harrison358c8d92008-07-15 18:44:13 -0700315 ieee80211_is_data(hdr->frame_control))) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200316#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200317 sdata_info(tx->sdata,
318 "dropped data frame to not associated station %pM\n",
319 hdr->addr1);
320#endif
Johannes Berge2ebc742007-07-27 15:43:22 +0200321 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100322 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200323 }
Johannes Berg29623892011-12-14 12:20:31 +0100324 } else if (unlikely(tx->sdata->vif.type == NL80211_IFTYPE_AP &&
325 ieee80211_is_data(hdr->frame_control) &&
Felix Fietkau030ef8f2012-04-23 19:49:02 +0200326 !atomic_read(&tx->sdata->u.ap.num_mcast_sta))) {
Johannes Berg29623892011-12-14 12:20:31 +0100327 /*
328 * No associated STAs - no need to send multicast
329 * frames.
330 */
331 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200332 }
333
Johannes Berg9ae54c82008-01-31 19:48:20 +0100334 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200335}
336
Johannes Berge2ebc742007-07-27 15:43:22 +0200337/* This function is called whenever the AP is about to exceed the maximum limit
338 * of buffered frames for power saving STAs. This situation should not really
339 * happen often during normal operation, so dropping the oldest buffered packet
340 * from each queue should be OK to make some room for new frames. */
341static void purge_old_ps_buffers(struct ieee80211_local *local)
342{
343 int total = 0, purged = 0;
344 struct sk_buff *skb;
345 struct ieee80211_sub_if_data *sdata;
346 struct sta_info *sta;
347
Johannes Berg79010422007-09-18 17:29:21 -0400348 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Marco Porschd012a602012-10-10 12:39:50 -0700349 struct ps_data *ps;
350
351 if (sdata->vif.type == NL80211_IFTYPE_AP)
352 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100353 else if (ieee80211_vif_is_mesh(&sdata->vif))
354 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700355 else
Johannes Berge2ebc742007-07-27 15:43:22 +0200356 continue;
Marco Porschd012a602012-10-10 12:39:50 -0700357
358 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +0200359 if (skb) {
360 purged++;
361 dev_kfree_skb(skb);
362 }
Marco Porschd012a602012-10-10 12:39:50 -0700363 total += skb_queue_len(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +0200364 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200365
Johannes Berg948d8872011-09-29 16:04:29 +0200366 /*
367 * Drop one frame from each station from the lowest-priority
368 * AC that has frames at all.
369 */
Johannes Bergd0709a62008-02-25 16:27:46 +0100370 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berg948d8872011-09-29 16:04:29 +0200371 int ac;
372
373 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
374 skb = skb_dequeue(&sta->ps_tx_buf[ac]);
375 total += skb_queue_len(&sta->ps_tx_buf[ac]);
376 if (skb) {
377 purged++;
Felix Fietkauc3e77242012-10-08 14:39:33 +0200378 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg948d8872011-09-29 16:04:29 +0200379 break;
380 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200381 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200382 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100383
Johannes Berge2ebc742007-07-27 15:43:22 +0200384 local->total_ps_buffered = total;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200385 ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
Johannes Berge2ebc742007-07-27 15:43:22 +0200386}
387
Johannes Berg9ae54c82008-01-31 19:48:20 +0100388static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100389ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200390{
Johannes Berge039fa42008-05-15 12:55:29 +0200391 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700392 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Marco Porschd012a602012-10-10 12:39:50 -0700393 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +0200394
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100395 /*
396 * broadcast/multicast frame
397 *
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100398 * If any of the associated/peer stations is in power save mode,
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100399 * the frame is buffered to be sent after DTIM beacon frame.
400 * This is done either by the hardware or us.
401 */
402
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100403 /* powersaving STAs currently only in AP/VLAN/mesh mode */
Marco Porschd012a602012-10-10 12:39:50 -0700404 if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
405 tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
406 if (!tx->sdata->bss)
407 return TX_CONTINUE;
408
409 ps = &tx->sdata->bss->ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100410 } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
411 ps = &tx->sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -0700412 } else {
Johannes Berg3e122be2008-07-09 14:40:34 +0200413 return TX_CONTINUE;
Marco Porschd012a602012-10-10 12:39:50 -0700414 }
415
Johannes Berg3e122be2008-07-09 14:40:34 +0200416
417 /* no buffering for ordered frames */
Harvey Harrison358c8d92008-07-15 18:44:13 -0700418 if (ieee80211_has_order(hdr->frame_control))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100419 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100420
Johannes Berg08b99392014-07-07 12:01:11 +0200421 if (ieee80211_is_probe_req(hdr->frame_control))
422 return TX_CONTINUE;
423
Johannes Bergf4d57942013-05-28 17:24:15 +0200424 if (tx->local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)
425 info->hw_queue = tx->sdata->vif.cab_queue;
426
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100427 /* no stations in PS mode */
Marco Porschd012a602012-10-10 12:39:50 -0700428 if (!atomic_read(&ps->num_sta_ps))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100429 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100430
Johannes Berg62b517c2009-10-29 12:19:21 +0100431 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
Johannes Berg62b12082009-08-10 16:04:15 +0200432
Johannes Berg62b517c2009-10-29 12:19:21 +0100433 /* device releases frame after DTIM beacon */
434 if (!(tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING))
Johannes Berg62b12082009-08-10 16:04:15 +0200435 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200436
Johannes Berg62b12082009-08-10 16:04:15 +0200437 /* buffered in mac80211 */
438 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
439 purge_old_ps_buffers(tx->local);
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100440
Marco Porschd012a602012-10-10 12:39:50 -0700441 if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200442 ps_dbg(tx->sdata,
443 "BC TX buffer full - dropping the oldest frame\n");
Marco Porschd012a602012-10-10 12:39:50 -0700444 dev_kfree_skb(skb_dequeue(&ps->bc_buf));
Johannes Berg62b12082009-08-10 16:04:15 +0200445 } else
446 tx->local->total_ps_buffered++;
447
Marco Porschd012a602012-10-10 12:39:50 -0700448 skb_queue_tail(&ps->bc_buf, tx->skb);
Johannes Berg62b12082009-08-10 16:04:15 +0200449
450 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200451}
452
Jouni Malinenfb733332009-01-08 13:32:00 +0200453static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
454 struct sk_buff *skb)
455{
456 if (!ieee80211_is_mgmt(fc))
457 return 0;
458
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200459 if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
Jouni Malinenfb733332009-01-08 13:32:00 +0200460 return 0;
461
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100462 if (!ieee80211_is_robust_mgmt_frame(skb))
Jouni Malinenfb733332009-01-08 13:32:00 +0200463 return 0;
464
465 return 1;
466}
467
Johannes Berg9ae54c82008-01-31 19:48:20 +0100468static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100469ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200470{
471 struct sta_info *sta = tx->sta;
Johannes Berge039fa42008-05-15 12:55:29 +0200472 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berg08b99392014-07-07 12:01:11 +0200473 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300474 struct ieee80211_local *local = tx->local;
Johannes Berge2ebc742007-07-27 15:43:22 +0200475
Johannes Berg02f2f1a2012-02-27 12:18:30 +0100476 if (unlikely(!sta))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100477 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200478
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200479 if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
Johannes Berg5ac2e352014-05-27 16:32:27 +0200480 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
481 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
Johannes Berg02f2f1a2012-02-27 12:18:30 +0100482 !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
Johannes Berg948d8872011-09-29 16:04:29 +0200483 int ac = skb_get_queue_mapping(tx->skb);
484
Johannes Berg08b99392014-07-07 12:01:11 +0200485 if (ieee80211_is_mgmt(hdr->frame_control) &&
486 !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
487 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
488 return TX_CONTINUE;
489 }
490
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200491 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
492 sta->sta.addr, sta->sta.aid, ac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200493 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
494 purge_old_ps_buffers(tx->local);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200495
496 /* sync with ieee80211_sta_ps_deliver_wakeup */
497 spin_lock(&sta->ps_lock);
498 /*
499 * STA woke up the meantime and all the frames on ps_tx_buf have
500 * been queued to pending queue. No reordering can happen, go
501 * ahead and Tx the packet.
502 */
503 if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
Johannes Berg5ac2e352014-05-27 16:32:27 +0200504 !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
505 !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200506 spin_unlock(&sta->ps_lock);
507 return TX_CONTINUE;
508 }
509
Johannes Berg948d8872011-09-29 16:04:29 +0200510 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
511 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200512 ps_dbg(tx->sdata,
513 "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
514 sta->sta.addr, ac);
Felix Fietkauc3e77242012-10-08 14:39:33 +0200515 ieee80211_free_txskb(&local->hw, old);
Johannes Berge2ebc742007-07-27 15:43:22 +0200516 } else
517 tx->local->total_ps_buffered++;
Johannes Berg004c8722008-02-20 11:21:35 +0100518
Johannes Berge039fa42008-05-15 12:55:29 +0200519 info->control.jiffies = jiffies;
Johannes Berg5061b0c2009-07-14 00:33:34 +0200520 info->control.vif = &tx->sdata->vif;
Johannes Berg8f77f382009-06-07 21:58:37 +0200521 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Johannes Berg03c8c062014-01-09 01:45:28 +0100522 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berg948d8872011-09-29 16:04:29 +0200523 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
Emmanuel Grumbach1d147bf2014-02-20 09:22:11 +0200524 spin_unlock(&sta->ps_lock);
Juuso Oikarinen3393a602010-04-19 10:12:52 +0300525
526 if (!timer_pending(&local->sta_cleanup))
527 mod_timer(&local->sta_cleanup,
528 round_jiffies(jiffies +
529 STA_INFO_CLEANUP_INTERVAL));
530
Johannes Bergc868cb352011-09-29 16:04:27 +0200531 /*
532 * We queued up some frames, so the TIM bit might
533 * need to be set, recalculate it.
534 */
535 sta_info_recalc_tim(sta);
536
Johannes Berg9ae54c82008-01-31 19:48:20 +0100537 return TX_QUEUED;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200538 } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
539 ps_dbg(tx->sdata,
540 "STA %pM in PS mode, but polling/in SP -> send frame\n",
541 sta->sta.addr);
Johannes Berge2ebc742007-07-27 15:43:22 +0200542 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200543
Johannes Berg9ae54c82008-01-31 19:48:20 +0100544 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200545}
546
Johannes Bergd9e8a702008-06-30 15:10:44 +0200547static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100548ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200549{
Johannes Berg5cf121c2008-02-25 16:27:43 +0100550 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100551 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200552
Johannes Berg5cf121c2008-02-25 16:27:43 +0100553 if (tx->flags & IEEE80211_TX_UNICAST)
Johannes Berge2ebc742007-07-27 15:43:22 +0200554 return ieee80211_tx_h_unicast_ps_buf(tx);
555 else
556 return ieee80211_tx_h_multicast_ps_buf(tx);
557}
558
Johannes Bergd9e8a702008-06-30 15:10:44 +0200559static ieee80211_tx_result debug_noinline
Johannes Berga621fa42010-08-27 14:26:54 +0300560ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
561{
562 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
563
Johannes Bergaf61a162013-07-02 18:09:12 +0200564 if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
565 if (tx->sdata->control_port_no_encrypt)
566 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
567 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
568 }
Johannes Berga621fa42010-08-27 14:26:54 +0300569
570 return TX_CONTINUE;
571}
572
573static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100574ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200575{
Johannes Berg46e6de12012-07-04 18:10:07 +0200576 struct ieee80211_key *key;
Johannes Berge039fa42008-05-15 12:55:29 +0200577 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700578 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400579
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200580 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
Johannes Berge2ebc742007-07-27 15:43:22 +0200581 tx->key = NULL;
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200582 else if (tx->sta &&
583 (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
Johannes Bergd4e46a32007-09-14 11:10:24 -0400584 tx->key = key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200585 else if (ieee80211_is_mgmt(hdr->frame_control) &&
Jouni Malinenecbcd322010-03-29 23:35:23 -0700586 is_multicast_ether_addr(hdr->addr1) &&
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100587 ieee80211_is_robust_mgmt_frame(tx->skb) &&
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200588 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
589 tx->key = key;
Johannes Bergf7e01042010-12-09 19:49:02 +0100590 else if (is_multicast_ether_addr(hdr->addr1) &&
591 (key = rcu_dereference(tx->sdata->default_multicast_key)))
592 tx->key = key;
593 else if (!is_multicast_ether_addr(hdr->addr1) &&
594 (key = rcu_dereference(tx->sdata->default_unicast_key)))
Johannes Bergd4e46a32007-09-14 11:10:24 -0400595 tx->key = key;
Johannes Berg46e6de12012-07-04 18:10:07 +0200596 else if (info->flags & IEEE80211_TX_CTL_INJECTED)
597 tx->key = NULL;
598 else if (!tx->sdata->drop_unencrypted)
599 tx->key = NULL;
600 else if (tx->skb->protocol == tx->sdata->control_port_protocol)
601 tx->key = NULL;
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100602 else if (ieee80211_is_robust_mgmt_frame(tx->skb) &&
Johannes Berg46e6de12012-07-04 18:10:07 +0200603 !(ieee80211_is_action(hdr->frame_control) &&
604 tx->sta && test_sta_flag(tx->sta, WLAN_STA_MFP)))
605 tx->key = NULL;
Nicolas Cavallari4922f712012-07-04 18:10:08 +0200606 else if (ieee80211_is_mgmt(hdr->frame_control) &&
Johannes Bergd8ca16d2014-01-23 16:20:29 +0100607 !ieee80211_is_robust_mgmt_frame(tx->skb))
Nicolas Cavallari4922f712012-07-04 18:10:08 +0200608 tx->key = NULL;
Johannes Berg46e6de12012-07-04 18:10:07 +0200609 else {
Johannes Berge2ebc742007-07-27 15:43:22 +0200610 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100611 return TX_DROP;
Johannes Berg46e6de12012-07-04 18:10:07 +0200612 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200613
614 if (tx->key) {
Johannes Berg813d7662010-01-17 01:47:58 +0100615 bool skip_hw = false;
616
Johannes Berge2ebc742007-07-27 15:43:22 +0200617 tx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400618 /* TODO: add threshold stuff again */
Johannes Berg176e4f82007-12-18 15:27:47 +0100619
Johannes Berg97359d12010-08-10 09:46:38 +0200620 switch (tx->key->conf.cipher) {
621 case WLAN_CIPHER_SUITE_WEP40:
622 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg97359d12010-08-10 09:46:38 +0200623 case WLAN_CIPHER_SUITE_TKIP:
Harvey Harrison358c8d92008-07-15 18:44:13 -0700624 if (!ieee80211_is_data_present(hdr->frame_control))
Johannes Berg176e4f82007-12-18 15:27:47 +0100625 tx->key = NULL;
626 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200627 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +0200628 case WLAN_CIPHER_SUITE_CCMP_256:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +0200629 case WLAN_CIPHER_SUITE_GCMP:
630 case WLAN_CIPHER_SUITE_GCMP_256:
Jouni Malinenfb733332009-01-08 13:32:00 +0200631 if (!ieee80211_is_data_present(hdr->frame_control) &&
632 !ieee80211_use_mfp(hdr->frame_control, tx->sta,
633 tx->skb))
634 tx->key = NULL;
Kalle Valo3b43a182010-01-23 20:27:14 +0200635 else
636 skip_hw = (tx->key->conf.flags &
Johannes Berge548c492012-09-04 17:08:23 +0200637 IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
Kalle Valo3b43a182010-01-23 20:27:14 +0200638 ieee80211_is_mgmt(hdr->frame_control);
Jouni Malinenfb733332009-01-08 13:32:00 +0200639 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200640 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen56c52da2015-01-24 19:52:08 +0200641 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
Jouni Malinen8ade5382015-01-24 19:52:09 +0200642 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
643 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200644 if (!ieee80211_is_mgmt(hdr->frame_control))
645 tx->key = NULL;
646 break;
Johannes Berg176e4f82007-12-18 15:27:47 +0100647 }
Johannes Berg813d7662010-01-17 01:47:58 +0100648
Johannes Berge54faf22013-01-29 11:41:38 +0100649 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
650 !ieee80211_is_deauth(hdr->frame_control)))
Johannes Berg95acac62011-07-12 12:30:59 +0200651 return TX_DROP;
652
Johannes Bergf12553e2010-01-22 22:07:59 +0100653 if (!skip_hw && tx->key &&
Johannes Berg382b1652010-01-25 11:36:16 +0100654 tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
Johannes Berg813d7662010-01-17 01:47:58 +0100655 info->control.hw_key = &tx->key->conf;
Johannes Berge2ebc742007-07-27 15:43:22 +0200656 }
657
Johannes Berg9ae54c82008-01-31 19:48:20 +0100658 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200659}
660
Johannes Bergd9e8a702008-06-30 15:10:44 +0200661static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100662ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200663{
Johannes Berge039fa42008-05-15 12:55:29 +0200664 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200665 struct ieee80211_hdr *hdr = (void *)tx->skb->data;
666 struct ieee80211_supported_band *sband;
Shanyu Zhaoa2c40242010-04-27 11:15:12 -0700667 u32 len;
Johannes Berge6a98542008-10-21 12:40:02 +0200668 struct ieee80211_tx_rate_control txrc;
Felix Fietkau0d528d82013-04-22 16:14:41 +0200669 struct ieee80211_sta_rates *ratetbl = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200670 bool assoc = false;
Johannes Berge6a98542008-10-21 12:40:02 +0200671
672 memset(&txrc, 0, sizeof(txrc));
Johannes Berg8318d782008-01-24 19:38:38 +0100673
Johannes Berg2d565772012-07-23 15:12:51 +0200674 sband = tx->local->hw.wiphy->bands[info->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200675
Shanyu Zhaoa2c40242010-04-27 11:15:12 -0700676 len = min_t(u32, tx->skb->len + FCS_LEN,
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200677 tx->local->hw.wiphy->frag_threshold);
Johannes Berg58d41852007-09-26 17:53:18 +0200678
Johannes Berge6a98542008-10-21 12:40:02 +0200679 /* set up the tx rate control struct we give the RC algo */
Johannes Berg005e4722012-02-26 11:24:35 +0100680 txrc.hw = &tx->local->hw;
Johannes Berge6a98542008-10-21 12:40:02 +0200681 txrc.sband = sband;
682 txrc.bss_conf = &tx->sdata->vif.bss_conf;
683 txrc.skb = tx->skb;
684 txrc.reported_rate.idx = -1;
Johannes Berg2d565772012-07-23 15:12:51 +0200685 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
Jouni Malinen37eb0b12010-01-06 13:09:08 +0200686 if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
687 txrc.max_rate_idx = -1;
688 else
689 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
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 ||
Felix Fietkau8f0729b2010-11-11 15:07:23 +0100697 tx->sdata->vif.type == NL80211_IFTYPE_ADHOC);
Johannes Berg58d41852007-09-26 17:53:18 +0200698
Johannes Berge6a98542008-10-21 12:40:02 +0200699 /* set up RTS protection if desired */
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200700 if (len > tx->local->hw.wiphy->rts_threshold) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200701 txrc.rts = true;
Johannes Berge2ebc742007-07-27 15:43:22 +0200702 }
Johannes Berge6a98542008-10-21 12:40:02 +0200703
Felix Fietkau0d528d82013-04-22 16:14:41 +0200704 info->control.use_rts = txrc.rts;
Felix Fietkau991fec02013-04-16 13:38:43 +0200705 info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
706
Johannes Berge6a98542008-10-21 12:40:02 +0200707 /*
708 * Use short preamble if the BSS can handle it, but not for
709 * management frames unless we know the receiver can handle
710 * that -- the management frame might be to a station that
711 * just wants a probe response.
712 */
713 if (tx->sdata->vif.bss_conf.use_short_preamble &&
714 (ieee80211_is_data(hdr->frame_control) ||
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200715 (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
Felix Fietkau0d528d82013-04-22 16:14:41 +0200716 txrc.short_preamble = true;
717
718 info->control.short_preamble = txrc.short_preamble;
Johannes Berge6a98542008-10-21 12:40:02 +0200719
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200720 if (tx->sta)
721 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
Johannes Berge6a98542008-10-21 12:40:02 +0200722
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700723 /*
724 * Lets not bother rate control if we're associated and cannot
725 * talk to the sta. This should not happen.
726 */
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200727 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700728 !rate_usable_index_exists(sband, &tx->sta->sta),
729 "%s: Dropped data frame as no usable bitrate found while "
730 "scanning and associated. Target station: "
731 "%pM on %d GHz band\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100732 tx->sdata->name, hdr->addr1,
Johannes Berg2d565772012-07-23 15:12:51 +0200733 info->band ? 5 : 2))
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700734 return TX_DROP;
735
736 /*
737 * If we're associated with the sta at this point we know we can at
738 * least send the frame at the lowest bit rate.
739 */
Johannes Berge6a98542008-10-21 12:40:02 +0200740 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
741
Felix Fietkau0d528d82013-04-22 16:14:41 +0200742 if (tx->sta && !info->control.skip_table)
743 ratetbl = rcu_dereference(tx->sta->sta.rates);
744
745 if (unlikely(info->control.rates[0].idx < 0)) {
746 if (ratetbl) {
747 struct ieee80211_tx_rate rate = {
748 .idx = ratetbl->rate[0].idx,
749 .flags = ratetbl->rate[0].flags,
750 .count = ratetbl->rate[0].count
751 };
752
753 if (ratetbl->rate[0].idx < 0)
754 return TX_DROP;
755
756 tx->rate = rate;
757 } else {
758 return TX_DROP;
759 }
760 } else {
761 tx->rate = info->control.rates[0];
762 }
Johannes Berge6a98542008-10-21 12:40:02 +0200763
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100764 if (txrc.reported_rate.idx < 0) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200765 txrc.reported_rate = tx->rate;
Helmut Schaac1ce5a72010-12-01 16:34:45 +0100766 if (tx->sta && ieee80211_is_data(hdr->frame_control))
767 tx->sta->last_tx_rate = txrc.reported_rate;
768 } else if (tx->sta)
Johannes Berge6a98542008-10-21 12:40:02 +0200769 tx->sta->last_tx_rate = txrc.reported_rate;
770
Felix Fietkau0d528d82013-04-22 16:14:41 +0200771 if (ratetbl)
772 return TX_CONTINUE;
773
Johannes Berge6a98542008-10-21 12:40:02 +0200774 if (unlikely(!info->control.rates[0].count))
775 info->control.rates[0].count = 1;
776
Gábor Stefanik99551512009-04-23 19:36:14 +0200777 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
778 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
779 info->control.rates[0].count = 1;
780
Johannes Berg9ae54c82008-01-31 19:48:20 +0100781 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200782}
783
Johannes Bergd9e8a702008-06-30 15:10:44 +0200784static ieee80211_tx_result debug_noinline
Johannes Bergf591fa52008-07-10 11:21:26 +0200785ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
786{
787 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
788 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
789 u16 *seq;
790 u8 *qc;
791 int tid;
792
Johannes Berg25d834e2008-09-12 22:52:47 +0200793 /*
794 * Packet injection may want to control the sequence
795 * number, if we have no matching interface then we
796 * neither assign one ourselves nor ask the driver to.
797 */
Johannes Berg5061b0c2009-07-14 00:33:34 +0200798 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
Johannes Berg25d834e2008-09-12 22:52:47 +0200799 return TX_CONTINUE;
800
Johannes Bergf591fa52008-07-10 11:21:26 +0200801 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
802 return TX_CONTINUE;
803
804 if (ieee80211_hdrlen(hdr->frame_control) < 24)
805 return TX_CONTINUE;
806
Johannes Berg49a59542011-09-29 16:04:41 +0200807 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
808 return TX_CONTINUE;
809
Johannes Berg94778282008-10-10 13:21:59 +0200810 /*
811 * Anything but QoS data that has a sequence number field
812 * (is long enough) gets a sequence number from the global
Bob Copelandc4c205f2013-08-23 09:35:38 -0400813 * counter. QoS data frames with a multicast destination
814 * also use the global counter (802.11-2012 9.3.2.10).
Johannes Berg94778282008-10-10 13:21:59 +0200815 */
Bob Copelandc4c205f2013-08-23 09:35:38 -0400816 if (!ieee80211_is_data_qos(hdr->frame_control) ||
817 is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg94778282008-10-10 13:21:59 +0200818 /* driver should assign sequence number */
Johannes Bergf591fa52008-07-10 11:21:26 +0200819 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
Johannes Berg94778282008-10-10 13:21:59 +0200820 /* for pure STA mode without beacons, we can do it */
821 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
822 tx->sdata->sequence_number += 0x10;
Johannes Berg79c892b2014-11-21 14:26:31 +0100823 if (tx->sta)
824 tx->sta->tx_msdu[IEEE80211_NUM_TIDS]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200825 return TX_CONTINUE;
826 }
827
828 /*
829 * This should be true for injected/management frames only, for
830 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
831 * above since they are not QoS-data frames.
832 */
833 if (!tx->sta)
834 return TX_CONTINUE;
835
836 /* include per-STA, per-TID sequence counter */
837
838 qc = ieee80211_get_qos_ctl(hdr);
839 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
840 seq = &tx->sta->tid_seq[tid];
Johannes Berg79c892b2014-11-21 14:26:31 +0100841 tx->sta->tx_msdu[tid]++;
Johannes Bergf591fa52008-07-10 11:21:26 +0200842
843 hdr->seq_ctrl = cpu_to_le16(*seq);
844
845 /* Increase the sequence number. */
846 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
847
848 return TX_CONTINUE;
849}
850
Johannes Berg252b86c2011-11-16 15:28:55 +0100851static int ieee80211_fragment(struct ieee80211_tx_data *tx,
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100852 struct sk_buff *skb, int hdrlen,
853 int frag_threshold)
854{
Johannes Berg252b86c2011-11-16 15:28:55 +0100855 struct ieee80211_local *local = tx->local;
Johannes Berga1a3fce2011-11-16 15:28:56 +0100856 struct ieee80211_tx_info *info;
Johannes Berg252b86c2011-11-16 15:28:55 +0100857 struct sk_buff *tmp;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100858 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
859 int pos = hdrlen + per_fragm;
860 int rem = skb->len - hdrlen - per_fragm;
861
862 if (WARN_ON(rem < 0))
863 return -EINVAL;
864
Johannes Berg252b86c2011-11-16 15:28:55 +0100865 /* first fragment was already added to queue by caller */
866
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100867 while (rem) {
868 int fraglen = per_fragm;
869
870 if (fraglen > rem)
871 fraglen = rem;
872 rem -= fraglen;
873 tmp = dev_alloc_skb(local->tx_headroom +
874 frag_threshold +
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200875 tx->sdata->encrypt_headroom +
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100876 IEEE80211_ENCRYPT_TAILROOM);
877 if (!tmp)
878 return -ENOMEM;
Johannes Berg252b86c2011-11-16 15:28:55 +0100879
880 __skb_queue_tail(&tx->skbs, tmp);
881
Max Stepanov2475b1cc2013-03-24 14:23:27 +0200882 skb_reserve(tmp,
883 local->tx_headroom + tx->sdata->encrypt_headroom);
884
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100885 /* copy control information */
886 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
Johannes Berga1a3fce2011-11-16 15:28:56 +0100887
888 info = IEEE80211_SKB_CB(tmp);
889 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
890 IEEE80211_TX_CTL_FIRST_FRAGMENT);
891
892 if (rem)
893 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
894
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100895 skb_copy_queue_mapping(tmp, skb);
896 tmp->priority = skb->priority;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100897 tmp->dev = skb->dev;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100898
899 /* copy header and data */
900 memcpy(skb_put(tmp, hdrlen), skb->data, hdrlen);
901 memcpy(skb_put(tmp, fraglen), skb->data + pos, fraglen);
902
903 pos += fraglen;
904 }
905
Johannes Berg252b86c2011-11-16 15:28:55 +0100906 /* adjust first fragment's length */
Johannes Berg338f9772014-02-01 00:16:23 +0100907 skb_trim(skb, hdrlen + per_fragm);
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100908 return 0;
909}
910
Johannes Bergf591fa52008-07-10 11:21:26 +0200911static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +0200912ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
913{
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100914 struct sk_buff *skb = tx->skb;
915 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
916 struct ieee80211_hdr *hdr = (void *)skb->data;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200917 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100918 int hdrlen;
919 int fragnum;
Johannes Berge2454942008-05-15 12:55:28 +0200920
Johannes Berg252b86c2011-11-16 15:28:55 +0100921 /* no matter what happens, tx->skb moves to tx->skbs */
922 __skb_queue_tail(&tx->skbs, skb);
923 tx->skb = NULL;
924
Johannes Berga26eb272011-10-07 14:01:25 +0200925 if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
926 return TX_CONTINUE;
927
928 if (tx->local->ops->set_frag_threshold)
Johannes Berge2454942008-05-15 12:55:28 +0200929 return TX_CONTINUE;
930
Johannes Bergeefce912008-05-17 00:57:13 +0200931 /*
932 * Warn when submitting a fragmented A-MPDU frame and drop it.
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200933 * This scenario is handled in ieee80211_tx_prepare but extra
Ron Rindjunsky8d5e0d52008-06-12 15:42:29 +0300934 * caution taken here as fragmented ampdu may cause Tx stop.
Johannes Bergeefce912008-05-17 00:57:13 +0200935 */
Sujith8b30b1f2008-10-24 09:55:27 +0530936 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
Johannes Bergeefce912008-05-17 00:57:13 +0200937 return TX_DROP;
938
Harvey Harrison065e96052008-06-22 16:45:27 -0700939 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berge2454942008-05-15 12:55:28 +0200940
Johannes Berga26eb272011-10-07 14:01:25 +0200941 /* internal error, why isn't DONTFRAG set? */
Johannes Berg8ccd8f22009-04-29 23:35:56 +0200942 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100943 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200944
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100945 /*
946 * Now fragment the frame. This will allocate all the fragments and
947 * chain them (using skb as the first fragment) to skb->next.
948 * During transmission, we will remove the successfully transmitted
949 * fragments from this list. When the low-level driver rejects one
950 * of the fragments then we will simply pretend to accept the skb
951 * but store it away as pending.
952 */
Johannes Berg252b86c2011-11-16 15:28:55 +0100953 if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100954 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200955
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100956 /* update duration/seq/flags of fragments */
957 fragnum = 0;
Johannes Berg252b86c2011-11-16 15:28:55 +0100958
959 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100960 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
Johannes Berge2454942008-05-15 12:55:28 +0200961
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100962 hdr = (void *)skb->data;
963 info = IEEE80211_SKB_CB(skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200964
Johannes Berg252b86c2011-11-16 15:28:55 +0100965 if (!skb_queue_is_last(&tx->skbs, skb)) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100966 hdr->frame_control |= morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200967 /*
968 * No multi-rate retries for fragmented frames, that
969 * would completely throw off the NAV at other STAs.
970 */
971 info->control.rates[1].idx = -1;
972 info->control.rates[2].idx = -1;
973 info->control.rates[3].idx = -1;
Thomas Huehne3e1a0b2012-07-02 19:46:16 +0200974 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
Johannes Berge6a98542008-10-21 12:40:02 +0200975 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100976 } else {
977 hdr->frame_control &= ~morefrags;
Johannes Berge6a98542008-10-21 12:40:02 +0200978 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100979 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
980 fragnum++;
Johannes Berg252b86c2011-11-16 15:28:55 +0100981 }
Johannes Berge2454942008-05-15 12:55:28 +0200982
983 return TX_CONTINUE;
Johannes Berge2454942008-05-15 12:55:28 +0200984}
985
Johannes Bergd9e8a702008-06-30 15:10:44 +0200986static ieee80211_tx_result debug_noinline
Johannes Bergfeff1f22009-08-10 16:01:54 +0200987ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
988{
Johannes Berg252b86c2011-11-16 15:28:55 +0100989 struct sk_buff *skb;
Johannes Berg560d2682013-02-05 10:55:21 +0100990 int ac = -1;
Johannes Bergfeff1f22009-08-10 16:01:54 +0200991
992 if (!tx->sta)
993 return TX_CONTINUE;
994
Johannes Berg252b86c2011-11-16 15:28:55 +0100995 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg560d2682013-02-05 10:55:21 +0100996 ac = skb_get_queue_mapping(skb);
Johannes Bergfeff1f22009-08-10 16:01:54 +0200997 tx->sta->tx_fragments++;
Johannes Berg560d2682013-02-05 10:55:21 +0100998 tx->sta->tx_bytes[ac] += skb->len;
Johannes Berg252b86c2011-11-16 15:28:55 +0100999 }
Johannes Berg560d2682013-02-05 10:55:21 +01001000 if (ac >= 0)
1001 tx->sta->tx_packets[ac]++;
Johannes Bergfeff1f22009-08-10 16:01:54 +02001002
1003 return TX_CONTINUE;
1004}
1005
1006static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +02001007ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
1008{
1009 if (!tx->key)
1010 return TX_CONTINUE;
1011
Johannes Berg97359d12010-08-10 09:46:38 +02001012 switch (tx->key->conf.cipher) {
1013 case WLAN_CIPHER_SUITE_WEP40:
1014 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge2454942008-05-15 12:55:28 +02001015 return ieee80211_crypto_wep_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001016 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berge2454942008-05-15 12:55:28 +02001017 return ieee80211_crypto_tkip_encrypt(tx);
Johannes Berg97359d12010-08-10 09:46:38 +02001018 case WLAN_CIPHER_SUITE_CCMP:
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +02001019 return ieee80211_crypto_ccmp_encrypt(
1020 tx, IEEE80211_CCMP_MIC_LEN);
1021 case WLAN_CIPHER_SUITE_CCMP_256:
1022 return ieee80211_crypto_ccmp_encrypt(
1023 tx, IEEE80211_CCMP_256_MIC_LEN);
Johannes Berg97359d12010-08-10 09:46:38 +02001024 case WLAN_CIPHER_SUITE_AES_CMAC:
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001025 return ieee80211_crypto_aes_cmac_encrypt(tx);
Jouni Malinen56c52da2015-01-24 19:52:08 +02001026 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1027 return ieee80211_crypto_aes_cmac_256_encrypt(tx);
Jouni Malinen8ade5382015-01-24 19:52:09 +02001028 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1029 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1030 return ieee80211_crypto_aes_gmac_encrypt(tx);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001031 case WLAN_CIPHER_SUITE_GCMP:
1032 case WLAN_CIPHER_SUITE_GCMP_256:
1033 return ieee80211_crypto_gcmp_encrypt(tx);
Johannes Berg3ffc2a92010-08-27 14:26:52 +03001034 default:
Yoni Divinskyd32a1022012-01-16 15:18:59 +02001035 return ieee80211_crypto_hw_encrypt(tx);
Johannes Berge2454942008-05-15 12:55:28 +02001036 }
1037
Johannes Berge2454942008-05-15 12:55:28 +02001038 return TX_DROP;
1039}
1040
Johannes Bergd9e8a702008-06-30 15:10:44 +02001041static ieee80211_tx_result debug_noinline
Johannes Berg03f93c32008-06-25 14:36:27 +03001042ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1043{
Johannes Berg252b86c2011-11-16 15:28:55 +01001044 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001045 struct ieee80211_hdr *hdr;
1046 int next_len;
1047 bool group_addr;
Johannes Berg03f93c32008-06-25 14:36:27 +03001048
Johannes Berg252b86c2011-11-16 15:28:55 +01001049 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001050 hdr = (void *) skb->data;
Jouni Malinen7e0aae42009-05-19 19:25:58 +03001051 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1052 break; /* must not overwrite AID */
Johannes Berg252b86c2011-11-16 15:28:55 +01001053 if (!skb_queue_is_last(&tx->skbs, skb)) {
1054 struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1055 next_len = next->len;
1056 } else
1057 next_len = 0;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001058 group_addr = is_multicast_ether_addr(hdr->addr1);
Johannes Berg03f93c32008-06-25 14:36:27 +03001059
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001060 hdr->duration_id =
Johannes Berg252b86c2011-11-16 15:28:55 +01001061 ieee80211_duration(tx, skb, group_addr, next_len);
1062 }
Johannes Berg03f93c32008-06-25 14:36:27 +03001063
1064 return TX_CONTINUE;
1065}
1066
Johannes Berge2ebc742007-07-27 15:43:22 +02001067/* actual transmit path */
1068
Johannes Berga622ab72010-06-10 10:21:39 +02001069static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1070 struct sk_buff *skb,
1071 struct ieee80211_tx_info *info,
1072 struct tid_ampdu_tx *tid_tx,
1073 int tid)
1074{
1075 bool queued = false;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001076 bool reset_agg_timer = false;
Helmut Schaaaa454582012-03-07 17:20:30 +01001077 struct sk_buff *purge_skb = NULL;
Johannes Berga622ab72010-06-10 10:21:39 +02001078
1079 if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1080 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001081 reset_agg_timer = true;
Johannes Berg0ab33702010-06-10 10:21:42 +02001082 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1083 /*
1084 * nothing -- this aggregation session is being started
1085 * but that might still fail with the driver
1086 */
Johannes Berga622ab72010-06-10 10:21:39 +02001087 } else {
1088 spin_lock(&tx->sta->lock);
1089 /*
1090 * Need to re-check now, because we may get here
1091 *
1092 * 1) in the window during which the setup is actually
1093 * already done, but not marked yet because not all
1094 * packets are spliced over to the driver pending
1095 * queue yet -- if this happened we acquire the lock
1096 * either before or after the splice happens, but
1097 * need to recheck which of these cases happened.
1098 *
1099 * 2) during session teardown, if the OPERATIONAL bit
1100 * was cleared due to the teardown but the pointer
1101 * hasn't been assigned NULL yet (or we loaded it
1102 * before it was assigned) -- in this case it may
1103 * now be NULL which means we should just let the
1104 * packet pass through because splicing the frames
1105 * back is already done.
1106 */
Johannes Berg40b275b2011-05-13 14:15:49 +02001107 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
Johannes Berga622ab72010-06-10 10:21:39 +02001108
1109 if (!tid_tx) {
1110 /* do nothing, let packet pass through */
1111 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1112 info->flags |= IEEE80211_TX_CTL_AMPDU;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001113 reset_agg_timer = true;
Johannes Berga622ab72010-06-10 10:21:39 +02001114 } else {
1115 queued = true;
1116 info->control.vif = &tx->sdata->vif;
1117 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Johannes Berg03c8c062014-01-09 01:45:28 +01001118 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
Johannes Berga622ab72010-06-10 10:21:39 +02001119 __skb_queue_tail(&tid_tx->pending, skb);
Helmut Schaaaa454582012-03-07 17:20:30 +01001120 if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1121 purge_skb = __skb_dequeue(&tid_tx->pending);
Johannes Berga622ab72010-06-10 10:21:39 +02001122 }
1123 spin_unlock(&tx->sta->lock);
Helmut Schaaaa454582012-03-07 17:20:30 +01001124
1125 if (purge_skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001126 ieee80211_free_txskb(&tx->local->hw, purge_skb);
Johannes Berga622ab72010-06-10 10:21:39 +02001127 }
1128
Nikolay Martynov285fa692011-11-22 21:50:28 -05001129 /* reset session timer */
1130 if (reset_agg_timer && tid_tx->timeout)
Felix Fietkau12d3952f2012-03-18 22:58:06 +01001131 tid_tx->last_tx = jiffies;
Nikolay Martynov285fa692011-11-22 21:50:28 -05001132
Johannes Berga622ab72010-06-10 10:21:39 +02001133 return queued;
1134}
1135
Johannes Berg58d41852007-09-26 17:53:18 +02001136/*
1137 * initialises @tx
1138 */
Johannes Berg9ae54c82008-01-31 19:48:20 +01001139static ieee80211_tx_result
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001140ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1141 struct ieee80211_tx_data *tx,
1142 struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02001143{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001144 struct ieee80211_local *local = sdata->local;
Johannes Berg58d41852007-09-26 17:53:18 +02001145 struct ieee80211_hdr *hdr;
Johannes Berge039fa42008-05-15 12:55:29 +02001146 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg68f2b512011-10-07 14:01:24 +02001147 int tid;
Johannes Berga622ab72010-06-10 10:21:39 +02001148 u8 *qc;
Johannes Berge2ebc742007-07-27 15:43:22 +02001149
1150 memset(tx, 0, sizeof(*tx));
1151 tx->skb = skb;
Johannes Berge2ebc742007-07-27 15:43:22 +02001152 tx->local = local;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001153 tx->sdata = sdata;
Johannes Berg252b86c2011-11-16 15:28:55 +01001154 __skb_queue_head_init(&tx->skbs);
Johannes Berge2ebc742007-07-27 15:43:22 +02001155
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001156 /*
1157 * If this flag is set to true anywhere, and we get here,
1158 * we are doing the needed processing, so remove the flag
1159 * now.
1160 */
1161 info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1162
Johannes Berg58d41852007-09-26 17:53:18 +02001163 hdr = (struct ieee80211_hdr *) skb->data;
1164
Felix Fietkau3f0e0b22010-01-08 18:15:13 +01001165 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001166 tx->sta = rcu_dereference(sdata->u.vlan.sta);
Felix Fietkau3f0e0b22010-01-08 18:15:13 +01001167 if (!tx->sta && sdata->dev->ieee80211_ptr->use_4addr)
1168 return TX_DROP;
Felix Fietkau03bb7f42013-09-29 21:39:33 +02001169 } else if (info->flags & (IEEE80211_TX_CTL_INJECTED |
1170 IEEE80211_TX_INTFL_NL80211_FRAME_TX) ||
Felix Fietkau66f2c992012-04-29 15:44:16 +02001171 tx->sdata->control_port_protocol == tx->skb->protocol) {
Felix Fietkaub4d57ad2010-01-31 23:25:24 +01001172 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
Felix Fietkau3f0e0b22010-01-08 18:15:13 +01001173 }
Johannes Berg9d6b1062015-02-24 00:28:18 +01001174 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
Johannes Bergabe60632009-11-25 17:46:18 +01001175 tx->sta = sta_info_get(sdata, hdr->addr1);
Johannes Berg58d41852007-09-26 17:53:18 +02001176
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001177 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
Johannes Berg49a59542011-09-29 16:04:41 +02001178 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
Arik Nemtsovedf6b782011-08-30 09:32:38 +03001179 (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION) &&
1180 !(local->hw.flags & IEEE80211_HW_TX_AMPDU_SETUP_IN_HW)) {
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001181 struct tid_ampdu_tx *tid_tx;
1182
Sujith8b30b1f2008-10-24 09:55:27 +05301183 qc = ieee80211_get_qos_ctl(hdr);
1184 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
1185
Johannes Berga622ab72010-06-10 10:21:39 +02001186 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1187 if (tid_tx) {
1188 bool queued;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001189
Johannes Berga622ab72010-06-10 10:21:39 +02001190 queued = ieee80211_tx_prep_agg(tx, skb, info,
1191 tid_tx, tid);
1192
1193 if (unlikely(queued))
1194 return TX_QUEUED;
1195 }
Sujith8b30b1f2008-10-24 09:55:27 +05301196 }
1197
Jiri Slabybadffb72007-08-28 17:01:54 -04001198 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001199 tx->flags &= ~IEEE80211_TX_UNICAST;
Johannes Berge039fa42008-05-15 12:55:29 +02001200 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Simon Wunderlich6fd67e92011-11-18 14:20:42 +01001201 } else
Johannes Berg5cf121c2008-02-25 16:27:43 +01001202 tx->flags |= IEEE80211_TX_UNICAST;
Johannes Berg58d41852007-09-26 17:53:18 +02001203
Johannes Berga26eb272011-10-07 14:01:25 +02001204 if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1205 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1206 skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1207 info->flags & IEEE80211_TX_CTL_AMPDU)
1208 info->flags |= IEEE80211_TX_CTL_DONTFRAG;
Johannes Berg58d41852007-09-26 17:53:18 +02001209 }
1210
Johannes Berge2ebc742007-07-27 15:43:22 +02001211 if (!tx->sta)
Johannes Berge039fa42008-05-15 12:55:29 +02001212 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001213 else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT))
Johannes Berge039fa42008-05-15 12:55:29 +02001214 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Johannes Berg58d41852007-09-26 17:53:18 +02001215
Johannes Berge039fa42008-05-15 12:55:29 +02001216 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001217
Johannes Berg9ae54c82008-01-31 19:48:20 +01001218 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001219}
1220
Johannes Berg11127e92011-11-16 16:02:47 +01001221static bool ieee80211_tx_frags(struct ieee80211_local *local,
1222 struct ieee80211_vif *vif,
1223 struct ieee80211_sta *sta,
1224 struct sk_buff_head *skbs,
1225 bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001226{
Thomas Huehn36323f82012-07-23 21:33:42 +02001227 struct ieee80211_tx_control control;
Johannes Berg252b86c2011-11-16 15:28:55 +01001228 struct sk_buff *skb, *tmp;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001229 unsigned long flags;
Johannes Berge2ebc742007-07-27 15:43:22 +02001230
Johannes Berg252b86c2011-11-16 15:28:55 +01001231 skb_queue_walk_safe(skbs, skb, tmp) {
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001232 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1233 int q = info->hw_queue;
1234
1235#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1236 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1237 __skb_unlink(skb, skbs);
Felix Fietkauc3e77242012-10-08 14:39:33 +02001238 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001239 continue;
1240 }
1241#endif
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001242
1243 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001244 if (local->queue_stop_reasons[q] ||
Johannes Berg7bb45682011-02-24 14:42:06 +01001245 (!txpending && !skb_queue_empty(&local->pending[q]))) {
Seth Forshee6c17b772013-02-11 11:21:07 -06001246 if (unlikely(info->flags &
Seth Forsheea7679ed2013-02-25 14:58:05 -06001247 IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1248 if (local->queue_stop_reasons[q] &
1249 ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1250 /*
1251 * Drop off-channel frames if queues
1252 * are stopped for any reason other
1253 * than off-channel operation. Never
1254 * queue them.
1255 */
1256 spin_unlock_irqrestore(
1257 &local->queue_stop_reason_lock,
1258 flags);
1259 ieee80211_purge_tx_queue(&local->hw,
1260 skbs);
1261 return true;
1262 }
1263 } else {
1264
Seth Forshee6c17b772013-02-11 11:21:07 -06001265 /*
Seth Forsheea7679ed2013-02-25 14:58:05 -06001266 * Since queue is stopped, queue up frames for
1267 * later transmission from the tx-pending
1268 * tasklet when the queue is woken again.
Seth Forshee6c17b772013-02-11 11:21:07 -06001269 */
Seth Forsheea7679ed2013-02-25 14:58:05 -06001270 if (txpending)
1271 skb_queue_splice_init(skbs,
1272 &local->pending[q]);
1273 else
1274 skb_queue_splice_tail_init(skbs,
1275 &local->pending[q]);
1276
1277 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1278 flags);
1279 return false;
Seth Forshee6c17b772013-02-11 11:21:07 -06001280 }
Johannes Berg7bb45682011-02-24 14:42:06 +01001281 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001282 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Tomas Winklerf8e79dd2008-07-24 18:46:44 +03001283
Johannes Berg11127e92011-11-16 16:02:47 +01001284 info->control.vif = vif;
Thomas Huehn36323f82012-07-23 21:33:42 +02001285 control.sta = sta;
Johannes Bergec25acc2010-07-22 17:11:28 +02001286
Johannes Berg252b86c2011-11-16 15:28:55 +01001287 __skb_unlink(skb, skbs);
Thomas Huehn36323f82012-07-23 21:33:42 +02001288 drv_tx(local, &control, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001289 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001290
Johannes Berg11127e92011-11-16 16:02:47 +01001291 return true;
1292}
1293
1294/*
1295 * Returns false if the frame couldn't be transmitted but was queued instead.
1296 */
1297static bool __ieee80211_tx(struct ieee80211_local *local,
1298 struct sk_buff_head *skbs, int led_len,
1299 struct sta_info *sta, bool txpending)
1300{
1301 struct ieee80211_tx_info *info;
1302 struct ieee80211_sub_if_data *sdata;
1303 struct ieee80211_vif *vif;
1304 struct ieee80211_sta *pubsta;
1305 struct sk_buff *skb;
1306 bool result = true;
1307 __le16 fc;
1308
1309 if (WARN_ON(skb_queue_empty(skbs)))
1310 return true;
1311
1312 skb = skb_peek(skbs);
1313 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1314 info = IEEE80211_SKB_CB(skb);
1315 sdata = vif_to_sdata(info->control.vif);
1316 if (sta && !sta->uploaded)
1317 sta = NULL;
1318
1319 if (sta)
1320 pubsta = &sta->sta;
1321 else
1322 pubsta = NULL;
1323
1324 switch (sdata->vif.type) {
1325 case NL80211_IFTYPE_MONITOR:
Johannes Bergc82b5a72013-07-14 23:39:20 +03001326 if (sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE) {
1327 vif = &sdata->vif;
1328 break;
1329 }
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001330 sdata = rcu_dereference(local->monitor_sdata);
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001331 if (sdata) {
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001332 vif = &sdata->vif;
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001333 info->hw_queue =
1334 vif->hw_queue[skb_get_queue_mapping(skb)];
1335 } else if (local->hw.flags & IEEE80211_HW_QUEUE_CONTROL) {
1336 dev_kfree_skb(skb);
1337 return true;
1338 } else
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001339 vif = NULL;
Johannes Berg11127e92011-11-16 16:02:47 +01001340 break;
1341 case NL80211_IFTYPE_AP_VLAN:
1342 sdata = container_of(sdata->bss,
1343 struct ieee80211_sub_if_data, u.ap);
1344 /* fall through */
1345 default:
1346 vif = &sdata->vif;
1347 break;
1348 }
1349
Johannes Bergcb831b52012-07-02 15:40:18 +02001350 result = ieee80211_tx_frags(local, vif, pubsta, skbs,
1351 txpending);
Johannes Berg11127e92011-11-16 16:02:47 +01001352
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001353 ieee80211_tpt_led_trig_tx(local, fc, led_len);
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001354
Johannes Berg4db4e0a2011-11-24 14:47:36 +01001355 WARN_ON_ONCE(!skb_queue_empty(skbs));
Johannes Berg252b86c2011-11-16 15:28:55 +01001356
Johannes Berg11127e92011-11-16 16:02:47 +01001357 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001358}
1359
Johannes Berg97b045d2008-06-20 01:22:30 +02001360/*
1361 * Invoke TX handlers, return 0 on success and non-zero if the
1362 * frame was dropped or queued.
1363 */
1364static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1365{
Johannes Berg252b86c2011-11-16 15:28:55 +01001366 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berg97b045d2008-06-20 01:22:30 +02001367 ieee80211_tx_result res = TX_DROP;
Johannes Berg97b045d2008-06-20 01:22:30 +02001368
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001369#define CALL_TXH(txh) \
1370 do { \
1371 res = txh(tx); \
1372 if (res != TX_CONTINUE) \
1373 goto txh_done; \
1374 } while (0)
Johannes Berg97b045d2008-06-20 01:22:30 +02001375
Kalle Valo5c1b98a2010-01-12 10:42:46 +02001376 CALL_TXH(ieee80211_tx_h_dynamic_ps);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001377 CALL_TXH(ieee80211_tx_h_check_assoc);
1378 CALL_TXH(ieee80211_tx_h_ps_buf);
Johannes Berga621fa42010-08-27 14:26:54 +03001379 CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001380 CALL_TXH(ieee80211_tx_h_select_key);
Johannes Bergaf65cd962009-11-17 18:18:36 +01001381 if (!(tx->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL))
1382 CALL_TXH(ieee80211_tx_h_rate_ctrl);
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001383
Johannes Bergaa5b5492011-12-02 22:08:52 +01001384 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1385 __skb_queue_tail(&tx->skbs, tx->skb);
1386 tx->skb = NULL;
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001387 goto txh_done;
Johannes Bergaa5b5492011-12-02 22:08:52 +01001388 }
Johannes Bergc6fcf6b2010-01-17 01:47:59 +01001389
1390 CALL_TXH(ieee80211_tx_h_michael_mic_add);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001391 CALL_TXH(ieee80211_tx_h_sequence);
1392 CALL_TXH(ieee80211_tx_h_fragment);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001393 /* handlers after fragment must be aware of tx info fragmentation! */
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001394 CALL_TXH(ieee80211_tx_h_stats);
1395 CALL_TXH(ieee80211_tx_h_encrypt);
Arik Nemtsov8fd369e2011-01-31 22:29:12 +02001396 if (!(tx->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL))
1397 CALL_TXH(ieee80211_tx_h_calculate_duration);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001398#undef CALL_TXH
1399
1400 txh_done:
Johannes Berg97b045d2008-06-20 01:22:30 +02001401 if (unlikely(res == TX_DROP)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001402 I802_DEBUG_INC(tx->local->tx_handlers_drop);
Johannes Berg252b86c2011-11-16 15:28:55 +01001403 if (tx->skb)
Felix Fietkauc3e77242012-10-08 14:39:33 +02001404 ieee80211_free_txskb(&tx->local->hw, tx->skb);
Johannes Berg252b86c2011-11-16 15:28:55 +01001405 else
Felix Fietkau1f98ab72012-11-10 03:44:14 +01001406 ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
Johannes Berg97b045d2008-06-20 01:22:30 +02001407 return -1;
1408 } else if (unlikely(res == TX_QUEUED)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001409 I802_DEBUG_INC(tx->local->tx_handlers_queued);
Johannes Berg97b045d2008-06-20 01:22:30 +02001410 return -1;
1411 }
1412
1413 return 0;
1414}
1415
Felix Fietkau06be6b12013-10-14 18:01:00 +02001416bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1417 struct ieee80211_vif *vif, struct sk_buff *skb,
1418 int band, struct ieee80211_sta **sta)
1419{
1420 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1421 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1422 struct ieee80211_tx_data tx;
Johannes Berg88724a82015-03-01 09:10:12 +02001423 struct sk_buff *skb2;
Felix Fietkau06be6b12013-10-14 18:01:00 +02001424
1425 if (ieee80211_tx_prepare(sdata, &tx, skb) == TX_DROP)
1426 return false;
1427
1428 info->band = band;
1429 info->control.vif = vif;
1430 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1431
1432 if (invoke_tx_handlers(&tx))
1433 return false;
1434
1435 if (sta) {
1436 if (tx.sta)
1437 *sta = &tx.sta->sta;
1438 else
1439 *sta = NULL;
1440 }
1441
Johannes Berg88724a82015-03-01 09:10:12 +02001442 /* this function isn't suitable for fragmented data frames */
1443 skb2 = __skb_dequeue(&tx.skbs);
1444 if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1445 ieee80211_free_txskb(hw, skb2);
1446 ieee80211_purge_tx_queue(hw, &tx.skbs);
1447 return false;
1448 }
1449
Felix Fietkau06be6b12013-10-14 18:01:00 +02001450 return true;
1451}
1452EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1453
Johannes Berg7bb45682011-02-24 14:42:06 +01001454/*
1455 * Returns false if the frame couldn't be transmitted but was queued instead.
1456 */
1457static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
Johannes Berg73c4e192014-11-09 18:50:09 +02001458 struct sk_buff *skb, bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001459{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001460 struct ieee80211_local *local = sdata->local;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001461 struct ieee80211_tx_data tx;
Johannes Berg97b045d2008-06-20 01:22:30 +02001462 ieee80211_tx_result res_prepare;
Johannes Berge039fa42008-05-15 12:55:29 +02001463 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001464 bool result = true;
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001465 int led_len;
Johannes Berge2ebc742007-07-27 15:43:22 +02001466
Johannes Berge2ebc742007-07-27 15:43:22 +02001467 if (unlikely(skb->len < 10)) {
1468 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001469 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001470 }
1471
Johannes Berg58d41852007-09-26 17:53:18 +02001472 /* initialises tx */
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001473 led_len = skb->len;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001474 res_prepare = ieee80211_tx_prepare(sdata, &tx, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001475
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001476 if (unlikely(res_prepare == TX_DROP)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02001477 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg55de9082012-07-26 17:24:39 +02001478 return true;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001479 } else if (unlikely(res_prepare == TX_QUEUED)) {
Johannes Berg55de9082012-07-26 17:24:39 +02001480 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001481 }
1482
Johannes Berg3a25a8c2012-04-03 16:28:50 +02001483 /* set up hw_queue value early */
1484 if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
1485 !(local->hw.flags & IEEE80211_HW_QUEUE_CONTROL))
1486 info->hw_queue =
1487 sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1488
Johannes Berg7bb45682011-02-24 14:42:06 +01001489 if (!invoke_tx_handlers(&tx))
Johannes Berg74e4dbf2011-11-16 15:28:57 +01001490 result = __ieee80211_tx(local, &tx.skbs, led_len,
1491 tx.sta, txpending);
Johannes Berg55de9082012-07-26 17:24:39 +02001492
Johannes Berg7bb45682011-02-24 14:42:06 +01001493 return result;
Johannes Berge2ebc742007-07-27 15:43:22 +02001494}
1495
1496/* device xmit handlers */
1497
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301498static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
Johannes Berg23c07522008-05-29 10:38:53 +02001499 struct sk_buff *skb,
1500 int head_need, bool may_encrypt)
1501{
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301502 struct ieee80211_local *local = sdata->local;
Johannes Berg23c07522008-05-29 10:38:53 +02001503 int tail_need = 0;
1504
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301505 if (may_encrypt && sdata->crypto_tx_tailroom_needed_cnt) {
Johannes Berg23c07522008-05-29 10:38:53 +02001506 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1507 tail_need -= skb_tailroom(skb);
1508 tail_need = max_t(int, tail_need, 0);
1509 }
1510
Ido Yarivc70f59a2014-07-29 15:39:14 +03001511 if (skb_cloned(skb) &&
1512 (!(local->hw.flags & IEEE80211_HW_SUPPORTS_CLONED_SKBS) ||
1513 !skb_clone_writable(skb, ETH_HLEN) ||
1514 sdata->crypto_tx_tailroom_needed_cnt))
Johannes Berg23c07522008-05-29 10:38:53 +02001515 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001516 else if (head_need || tail_need)
Johannes Berg23c07522008-05-29 10:38:53 +02001517 I802_DEBUG_INC(local->tx_expand_skb_head);
Felix Fietkau4cd06a32010-12-18 19:30:49 +01001518 else
1519 return 0;
Johannes Berg23c07522008-05-29 10:38:53 +02001520
1521 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
Joe Perches0fb9a9e2010-08-20 16:25:38 -07001522 wiphy_debug(local->hw.wiphy,
1523 "failed to reallocate TX buffer\n");
Johannes Berg23c07522008-05-29 10:38:53 +02001524 return -ENOMEM;
1525 }
1526
Johannes Berg23c07522008-05-29 10:38:53 +02001527 return 0;
1528}
1529
Johannes Berg73c4e192014-11-09 18:50:09 +02001530void ieee80211_xmit(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02001531{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001532 struct ieee80211_local *local = sdata->local;
Johannes Berge039fa42008-05-15 12:55:29 +02001533 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001534 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berge2ebc742007-07-27 15:43:22 +02001535 int headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001536 bool may_encrypt;
Johannes Berge2ebc742007-07-27 15:43:22 +02001537
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001538 may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
Johannes Berg23c07522008-05-29 10:38:53 +02001539
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001540 headroom = local->tx_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001541 if (may_encrypt)
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001542 headroom += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001543 headroom -= skb_headroom(skb);
1544 headroom = max_t(int, 0, headroom);
1545
Yogesh Ashok Powar3bff1862011-06-28 18:41:37 +05301546 if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02001547 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001548 return;
Johannes Berge2ebc742007-07-27 15:43:22 +02001549 }
1550
Steve deRosier740c1aa2010-09-11 20:01:31 -07001551 hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berg5061b0c2009-07-14 00:33:34 +02001552 info->control.vif = &sdata->vif;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001553
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001554 if (ieee80211_vif_is_mesh(&sdata->vif)) {
1555 if (ieee80211_is_data(hdr->frame_control) &&
1556 is_unicast_ether_addr(hdr->addr1)) {
Johannes Bergbf7cd942013-02-15 14:40:31 +01001557 if (mesh_nexthop_resolve(sdata, skb))
Marco Porsch3f52b7e2013-01-30 18:14:08 +01001558 return; /* skb queued: don't free */
1559 } else {
1560 ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
1561 }
Johannes Berg98aed9f2012-03-27 14:18:36 +02001562 }
Javier Cardonacca89492009-08-10 17:29:29 -07001563
Javier Cardona2154c81c2011-09-07 17:49:53 -07001564 ieee80211_set_qos_hdr(sdata, skb);
Johannes Berg73c4e192014-11-09 18:50:09 +02001565 ieee80211_tx(sdata, skb, false);
Johannes Berge2ebc742007-07-27 15:43:22 +02001566}
1567
Johannes Berg73b9f032011-10-07 14:01:26 +02001568static bool ieee80211_parse_tx_radiotap(struct sk_buff *skb)
1569{
1570 struct ieee80211_radiotap_iterator iterator;
1571 struct ieee80211_radiotap_header *rthdr =
1572 (struct ieee80211_radiotap_header *) skb->data;
1573 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1574 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
1575 NULL);
1576 u16 txflags;
1577
1578 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1579 IEEE80211_TX_CTL_DONTFRAG;
1580
1581 /*
1582 * for every radiotap entry that is present
1583 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
1584 * entries present, or -EINVAL on error)
1585 */
1586
1587 while (!ret) {
1588 ret = ieee80211_radiotap_iterator_next(&iterator);
1589
1590 if (ret)
1591 continue;
1592
1593 /* see if this argument is something we can use */
1594 switch (iterator.this_arg_index) {
1595 /*
1596 * You must take care when dereferencing iterator.this_arg
1597 * for multibyte types... the pointer is not aligned. Use
1598 * get_unaligned((type *)iterator.this_arg) to dereference
1599 * iterator.this_arg for type "type" safely on all arches.
1600 */
1601 case IEEE80211_RADIOTAP_FLAGS:
1602 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
1603 /*
1604 * this indicates that the skb we have been
1605 * handed has the 32-bit FCS CRC at the end...
1606 * we should react to that by snipping it off
1607 * because it will be recomputed and added
1608 * on transmission
1609 */
1610 if (skb->len < (iterator._max_length + FCS_LEN))
1611 return false;
1612
1613 skb_trim(skb, skb->len - FCS_LEN);
1614 }
1615 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
1616 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
1617 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
1618 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
1619 break;
1620
1621 case IEEE80211_RADIOTAP_TX_FLAGS:
1622 txflags = get_unaligned_le16(iterator.this_arg);
1623 if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
1624 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1625 break;
1626
1627 /*
1628 * Please update the file
1629 * Documentation/networking/mac80211-injection.txt
1630 * when parsing new fields here.
1631 */
1632
1633 default:
1634 break;
1635 }
1636 }
1637
1638 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
1639 return false;
1640
1641 /*
1642 * remove the radiotap header
1643 * iterator->_max_length was sanity-checked against
1644 * skb->len by iterator init
1645 */
1646 skb_pull(skb, iterator._max_length);
1647
1648 return true;
1649}
1650
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +00001651netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
1652 struct net_device *dev)
Johannes Berge2ebc742007-07-27 15:43:22 +02001653{
1654 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg55de9082012-07-26 17:24:39 +02001655 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berge2ebc742007-07-27 15:43:22 +02001656 struct ieee80211_radiotap_header *prthdr =
1657 (struct ieee80211_radiotap_header *)skb->data;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001658 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02001659 struct ieee80211_hdr *hdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02001660 struct ieee80211_sub_if_data *tmp_sdata, *sdata;
Janusz Dziedzicb4932832014-06-05 08:12:57 +02001661 struct cfg80211_chan_def *chandef;
Andy Green9b8a74e2007-07-27 15:43:24 +02001662 u16 len_rthdr;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02001663 int hdrlen;
Johannes Berge2ebc742007-07-27 15:43:22 +02001664
Andy Green9b8a74e2007-07-27 15:43:24 +02001665 /* check for not even having the fixed radiotap header part */
1666 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1667 goto fail; /* too short to be possibly valid */
1668
1669 /* is it a header version we can trust to find length from? */
1670 if (unlikely(prthdr->it_version))
1671 goto fail; /* only version 0 is supported */
1672
1673 /* then there must be a radiotap header with a length we can use */
1674 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1675
1676 /* does the skb contain enough to deliver on the alleged length? */
1677 if (unlikely(skb->len < len_rthdr))
1678 goto fail; /* skb too short for claimed rt header extent */
Johannes Berge2ebc742007-07-27 15:43:22 +02001679
Johannes Berge2ebc742007-07-27 15:43:22 +02001680 /*
1681 * fix up the pointers accounting for the radiotap
1682 * header still being in there. We are being given
1683 * a precooked IEEE80211 header so no need for
1684 * normal processing
1685 */
Andy Green9b8a74e2007-07-27 15:43:24 +02001686 skb_set_mac_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02001687 /*
Andy Green9b8a74e2007-07-27 15:43:24 +02001688 * these are just fixed to the end of the rt area since we
1689 * don't have any better information and at this point, nobody cares
Johannes Berge2ebc742007-07-27 15:43:22 +02001690 */
Andy Green9b8a74e2007-07-27 15:43:24 +02001691 skb_set_network_header(skb, len_rthdr);
1692 skb_set_transport_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02001693
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02001694 if (skb->len < len_rthdr + 2)
1695 goto fail;
1696
1697 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
1698 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1699
1700 if (skb->len < len_rthdr + hdrlen)
1701 goto fail;
1702
Helmut Schaaf75f5c62011-08-01 11:32:52 +02001703 /*
1704 * Initialize skb->protocol if the injected frame is a data frame
1705 * carrying a rfc1042 header
1706 */
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02001707 if (ieee80211_is_data(hdr->frame_control) &&
1708 skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
1709 u8 *payload = (u8 *)hdr + hdrlen;
1710
Joe Perchesb203ca32012-05-08 18:56:52 +00001711 if (ether_addr_equal(payload, rfc1042_header))
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02001712 skb->protocol = cpu_to_be16((payload[6] << 8) |
1713 payload[7]);
Helmut Schaaf75f5c62011-08-01 11:32:52 +02001714 }
1715
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001716 memset(info, 0, sizeof(*info));
1717
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02001718 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
Johannes Berg73b9f032011-10-07 14:01:26 +02001719 IEEE80211_TX_CTL_INJECTED;
1720
1721 /* process and remove the injection radiotap header */
1722 if (!ieee80211_parse_tx_radiotap(skb))
1723 goto fail;
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02001724
1725 rcu_read_lock();
1726
1727 /*
1728 * We process outgoing injected frames that have a local address
1729 * we handle as though they are non-injected frames.
1730 * This code here isn't entirely correct, the local MAC address
1731 * isn't always enough to find the interface to use; for proper
1732 * VLAN/WDS support we will need a different mechanism (which
1733 * likely isn't going to be monitor interfaces).
1734 */
1735 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1736
1737 list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
1738 if (!ieee80211_sdata_running(tmp_sdata))
1739 continue;
1740 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
1741 tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1742 tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
1743 continue;
Joe Perchesb203ca32012-05-08 18:56:52 +00001744 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02001745 sdata = tmp_sdata;
1746 break;
1747 }
1748 }
Johannes Berg7351c6b2009-11-19 01:08:30 +01001749
Johannes Berg55de9082012-07-26 17:24:39 +02001750 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1751 if (!chanctx_conf) {
1752 tmp_sdata = rcu_dereference(local->monitor_sdata);
1753 if (tmp_sdata)
1754 chanctx_conf =
1755 rcu_dereference(tmp_sdata->vif.chanctx_conf);
1756 }
Johannes Berg55de9082012-07-26 17:24:39 +02001757
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01001758 if (chanctx_conf)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02001759 chandef = &chanctx_conf->def;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01001760 else if (!local->use_chanctx)
Janusz Dziedzicb4932832014-06-05 08:12:57 +02001761 chandef = &local->_oper_chandef;
Felix Fietkaub4a7ff72013-01-13 23:10:26 +01001762 else
1763 goto fail_rcu;
Johannes Berg55de9082012-07-26 17:24:39 +02001764
1765 /*
1766 * Frame injection is not allowed if beaconing is not allowed
1767 * or if we need radar detection. Beaconing is usually not allowed when
1768 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
1769 * Passive scan is also used in world regulatory domains where
1770 * your country is not known and as such it should be treated as
1771 * NO TX unless the channel is explicitly allowed in which case
1772 * your current regulatory domain would not have the passive scan
1773 * flag.
1774 *
1775 * Since AP mode uses monitor interfaces to inject/TX management
1776 * frames we can make AP mode the exception to this rule once it
1777 * supports radar detection as its implementation can deal with
1778 * radar detection by itself. We can do that later by adding a
1779 * monitor flag interfaces used for AP support.
1780 */
Janusz Dziedzicb4932832014-06-05 08:12:57 +02001781 if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
1782 sdata->vif.type))
Johannes Berg55de9082012-07-26 17:24:39 +02001783 goto fail_rcu;
1784
Johannes Berg73c4e192014-11-09 18:50:09 +02001785 info->band = chandef->chan->band;
1786 ieee80211_xmit(sdata, skb);
Johannes Berg5d9cf4a2011-10-07 14:01:23 +02001787 rcu_read_unlock();
1788
Johannes Berge2ebc742007-07-27 15:43:22 +02001789 return NETDEV_TX_OK;
Andy Green9b8a74e2007-07-27 15:43:24 +02001790
Johannes Berg55de9082012-07-26 17:24:39 +02001791fail_rcu:
1792 rcu_read_unlock();
Andy Green9b8a74e2007-07-27 15:43:24 +02001793fail:
1794 dev_kfree_skb(skb);
1795 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
Johannes Berge2ebc742007-07-27 15:43:22 +02001796}
1797
1798/**
Johannes Berg4c9451e2014-11-09 18:50:10 +02001799 * ieee80211_build_hdr - build 802.11 header in the given frame
1800 * @sdata: virtual interface to build the header for
1801 * @skb: the skb to build the header in
Liad Kaufman24d342c2014-11-09 18:50:07 +02001802 * @info_flags: skb flags to set
Johannes Berge2ebc742007-07-27 15:43:22 +02001803 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02001804 * This function takes the skb with 802.3 header and reformats the header to
1805 * the appropriate IEEE 802.11 header based on which interface the packet is
1806 * being transmitted on.
Johannes Berge2ebc742007-07-27 15:43:22 +02001807 *
Johannes Berg4c9451e2014-11-09 18:50:10 +02001808 * Note that this function also takes care of the TX status request and
1809 * potential unsharing of the SKB - this needs to be interleaved with the
1810 * header building.
1811 *
1812 * The function requires the read-side RCU lock held
1813 *
1814 * Returns: the (possibly reallocated) skb or an ERR_PTR() code
Johannes Berge2ebc742007-07-27 15:43:22 +02001815 */
Johannes Berg4c9451e2014-11-09 18:50:10 +02001816static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
1817 struct sk_buff *skb, u32 info_flags)
Johannes Berge2ebc742007-07-27 15:43:22 +02001818{
Johannes Berg133b8222008-09-16 14:18:59 +02001819 struct ieee80211_local *local = sdata->local;
Felix Fietkau489ee912010-12-18 19:30:48 +01001820 struct ieee80211_tx_info *info;
Johannes Bergdcf33962012-07-30 15:11:56 +02001821 int head_need;
Harvey Harrison065e96052008-06-22 16:45:27 -07001822 u16 ethertype, hdrlen, meshhdrlen = 0;
1823 __le16 fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02001824 struct ieee80211_hdr hdr;
Wey-Yi Guyff67bb82010-08-21 07:23:29 -07001825 struct ieee80211s_hdr mesh_hdr __maybe_unused;
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08001826 struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02001827 const u8 *encaps_data;
1828 int encaps_len, skip_header_bytes;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001829 int nh_pos, h_pos;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001830 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001831 bool wme_sta = false, authorized = false, tdls_auth = false;
Arik Nemtsov9deba042014-06-11 17:18:18 +03001832 bool tdls_peer = false, tdls_setup_frame = false;
Johannes Berga729cff2011-11-06 14:13:34 +01001833 bool multicast;
Johannes Berga729cff2011-11-06 14:13:34 +01001834 u16 info_id = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02001835 struct ieee80211_chanctx_conf *chanctx_conf;
1836 struct ieee80211_sub_if_data *ap_sdata;
1837 enum ieee80211_band band;
Johannes Berg4c9451e2014-11-09 18:50:10 +02001838 int ret;
Johannes Berge2ebc742007-07-27 15:43:22 +02001839
Johannes Berge2ebc742007-07-27 15:43:22 +02001840 /* convert Ethernet header to proper 802.11 header (based on
1841 * operation mode) */
1842 ethertype = (skb->data[12] << 8) | skb->data[13];
Harvey Harrison065e96052008-06-22 16:45:27 -07001843 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
Johannes Berge2ebc742007-07-27 15:43:22 +02001844
Johannes Berg51fb61e2007-12-19 01:31:27 +01001845 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001846 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg9bc383d2009-11-19 11:55:19 +01001847 sta = rcu_dereference(sdata->u.vlan.sta);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001848 if (sta) {
1849 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1850 /* RA TA DA SA */
1851 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01001852 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001853 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1854 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1855 hdrlen = 30;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001856 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Johannes Berga74a8c82014-07-22 14:50:47 +02001857 wme_sta = sta->sta.wme;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001858 }
Johannes Berg55de9082012-07-26 17:24:39 +02001859 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1860 u.ap);
1861 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02001862 if (!chanctx_conf) {
1863 ret = -ENOTCONN;
1864 goto free;
1865 }
Johannes Berg4bf88532012-11-09 11:39:59 +01001866 band = chanctx_conf->def.chan->band;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001867 if (sta)
1868 break;
1869 /* fall through */
1870 case NL80211_IFTYPE_AP:
Arnd Bergmannfe801232013-01-25 14:14:33 +00001871 if (sdata->vif.type == NL80211_IFTYPE_AP)
1872 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02001873 if (!chanctx_conf) {
1874 ret = -ENOTCONN;
1875 goto free;
1876 }
Harvey Harrison065e96052008-06-22 16:45:27 -07001877 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
Johannes Berge2ebc742007-07-27 15:43:22 +02001878 /* DA BSSID SA */
1879 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01001880 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02001881 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1882 hdrlen = 24;
Johannes Berg4bf88532012-11-09 11:39:59 +01001883 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04001884 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02001885 case NL80211_IFTYPE_WDS:
Harvey Harrison065e96052008-06-22 16:45:27 -07001886 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
Johannes Berge2ebc742007-07-27 15:43:22 +02001887 /* RA TA DA SA */
1888 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01001889 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02001890 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1891 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1892 hdrlen = 30;
Johannes Berg55de9082012-07-26 17:24:39 +02001893 /*
1894 * This is the exception! WDS style interfaces are prohibited
1895 * when channel contexts are in used so this must be valid
1896 */
Karl Beldan675a0b02013-03-25 16:26:57 +01001897 band = local->hw.conf.chandef.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04001898 break;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001899#ifdef CONFIG_MAC80211_MESH
Johannes Berg05c914f2008-09-11 00:01:58 +02001900 case NL80211_IFTYPE_MESH_POINT:
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08001901 if (!is_multicast_ether_addr(skb->data)) {
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08001902 struct sta_info *next_hop;
1903 bool mpp_lookup = true;
1904
Johannes Bergbf7cd942013-02-15 14:40:31 +01001905 mpath = mesh_path_lookup(sdata, skb->data);
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08001906 if (mpath) {
1907 mpp_lookup = false;
1908 next_hop = rcu_dereference(mpath->next_hop);
1909 if (!next_hop ||
1910 !(mpath->flags & (MESH_PATH_ACTIVE |
1911 MESH_PATH_RESOLVING)))
1912 mpp_lookup = true;
1913 }
1914
1915 if (mpp_lookup)
Johannes Bergbf7cd942013-02-15 14:40:31 +01001916 mppath = mpp_path_lookup(sdata, skb->data);
Chun-Yeow Yeoh163df6c2013-02-19 10:04:50 +08001917
1918 if (mppath && mpath)
1919 mesh_path_del(mpath->sdata, mpath->dst);
Chun-Yeow Yeohb8bacc12012-05-30 09:30:41 +08001920 }
YanBo79617de2008-09-22 13:30:32 +08001921
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06001922 /*
Joel A Fernandes9d525012011-01-10 00:44:23 -06001923 * Use address extension if it is a packet from
1924 * another interface or if we know the destination
1925 * is being proxied by a portal (i.e. portal address
1926 * differs from proxied address)
Joel A Fernandesf76b57b2010-12-28 19:28:11 -06001927 */
Joe Perchesb203ca32012-05-08 18:56:52 +00001928 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
1929 !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
Javier Cardona3c5772a2009-08-10 12:15:48 -07001930 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
1931 skb->data, skb->data + ETH_ALEN);
Johannes Bergbf7cd942013-02-15 14:40:31 +01001932 meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
1933 NULL, NULL);
YanBo79617de2008-09-22 13:30:32 +08001934 } else {
Thomas Pedersen27f01122012-08-20 11:28:25 -07001935 /* DS -> MBSS (802.11-2012 13.11.3.3).
1936 * For unicast with unknown forwarding information,
1937 * destination might be in the MBSS or if that fails
1938 * forwarded to another mesh gate. In either case
1939 * resolution will be handled in ieee80211_xmit(), so
1940 * leave the original DA. This also works for mcast */
1941 const u8 *mesh_da = skb->data;
YanBo79617de2008-09-22 13:30:32 +08001942
Thomas Pedersen27f01122012-08-20 11:28:25 -07001943 if (mppath)
1944 mesh_da = mppath->mpp;
1945 else if (mpath)
1946 mesh_da = mpath->dst;
Thomas Pedersen27f01122012-08-20 11:28:25 -07001947
Javier Cardona3c5772a2009-08-10 12:15:48 -07001948 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
Johannes Berg47846c92009-11-25 17:46:19 +01001949 mesh_da, sdata->vif.addr);
Thomas Pedersen27f01122012-08-20 11:28:25 -07001950 if (is_multicast_ether_addr(mesh_da))
1951 /* DA TA mSA AE:SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01001952 meshhdrlen = ieee80211_new_mesh_header(
1953 sdata, &mesh_hdr,
1954 skb->data + ETH_ALEN, NULL);
Javier Cardona3c5772a2009-08-10 12:15:48 -07001955 else
Thomas Pedersen27f01122012-08-20 11:28:25 -07001956 /* RA TA mDA mSA AE:DA SA */
Johannes Bergbf7cd942013-02-15 14:40:31 +01001957 meshhdrlen = ieee80211_new_mesh_header(
1958 sdata, &mesh_hdr, skb->data,
1959 skb->data + ETH_ALEN);
YanBo79617de2008-09-22 13:30:32 +08001960
YanBo79617de2008-09-22 13:30:32 +08001961 }
Johannes Berg55de9082012-07-26 17:24:39 +02001962 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02001963 if (!chanctx_conf) {
1964 ret = -ENOTCONN;
1965 goto free;
1966 }
Johannes Berg4bf88532012-11-09 11:39:59 +01001967 band = chanctx_conf->def.chan->band;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001968 break;
1969#endif
Johannes Berg05c914f2008-09-11 00:01:58 +02001970 case NL80211_IFTYPE_STATION:
Arik Nemtsov941c93c2011-09-28 14:12:54 +03001971 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
Arik Nemtsov941c93c2011-09-28 14:12:54 +03001972 sta = sta_info_get(sdata, skb->data);
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001973 if (sta) {
1974 authorized = test_sta_flag(sta,
1975 WLAN_STA_AUTHORIZED);
Johannes Berga74a8c82014-07-22 14:50:47 +02001976 wme_sta = sta->sta.wme;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001977 tdls_peer = test_sta_flag(sta,
Arik Nemtsov9deba042014-06-11 17:18:18 +03001978 WLAN_STA_TDLS_PEER);
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001979 tdls_auth = test_sta_flag(sta,
1980 WLAN_STA_TDLS_PEER_AUTH);
1981 }
Arik Nemtsov941c93c2011-09-28 14:12:54 +03001982
Arik Nemtsov9deba042014-06-11 17:18:18 +03001983 if (tdls_peer)
1984 tdls_setup_frame =
1985 ethertype == ETH_P_TDLS &&
1986 skb->len > 14 &&
1987 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
Arik Nemtsov941c93c2011-09-28 14:12:54 +03001988 }
1989
Arik Nemtsov9deba042014-06-11 17:18:18 +03001990 /*
1991 * TDLS link during setup - throw out frames to peer. We allow
1992 * TDLS-setup frames to unauthorized peers for the special case
1993 * of a link teardown after a TDLS sta is removed due to being
1994 * unreachable.
1995 */
Johannes Berg4c9451e2014-11-09 18:50:10 +02001996 if (tdls_peer && !tdls_auth && !tdls_setup_frame) {
1997 ret = -EINVAL;
1998 goto free;
1999 }
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002000
Arik Nemtsov9deba042014-06-11 17:18:18 +03002001 /* send direct packets to authorized TDLS peers */
2002 if (tdls_peer && tdls_auth) {
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002003 /* DA SA BSSID */
2004 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2005 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2006 memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2007 hdrlen = 24;
2008 } else if (sdata->u.mgd.use_4addr &&
2009 cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2010 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2011 IEEE80211_FCTL_TODS);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002012 /* RA TA DA SA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002013 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002014 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002015 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2016 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2017 hdrlen = 30;
2018 } else {
2019 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2020 /* BSSID SA DA */
Arik Nemtsov941c93c2011-09-28 14:12:54 +03002021 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01002022 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2023 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2024 hdrlen = 24;
2025 }
Johannes Berg55de9082012-07-26 17:24:39 +02002026 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002027 if (!chanctx_conf) {
2028 ret = -ENOTCONN;
2029 goto free;
2030 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002031 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002032 break;
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002033 case NL80211_IFTYPE_OCB:
2034 /* DA SA BSSID */
2035 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2036 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2037 eth_broadcast_addr(hdr.addr3);
2038 hdrlen = 24;
2039 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002040 if (!chanctx_conf) {
2041 ret = -ENOTCONN;
2042 goto free;
2043 }
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002044 band = chanctx_conf->def.chan->band;
2045 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002046 case NL80211_IFTYPE_ADHOC:
Johannes Berge2ebc742007-07-27 15:43:22 +02002047 /* DA SA BSSID */
2048 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2049 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01002050 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02002051 hdrlen = 24;
Johannes Berg55de9082012-07-26 17:24:39 +02002052 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002053 if (!chanctx_conf) {
2054 ret = -ENOTCONN;
2055 goto free;
2056 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002057 band = chanctx_conf->def.chan->band;
Johannes Bergcf966832007-08-28 17:01:54 -04002058 break;
2059 default:
Johannes Berg4c9451e2014-11-09 18:50:10 +02002060 ret = -EINVAL;
2061 goto free;
Johannes Berge2ebc742007-07-27 15:43:22 +02002062 }
2063
Johannes Berg7d185b82008-01-28 17:11:43 +01002064 /*
2065 * There's no need to try to look up the destination
2066 * if it is a multicast address (which can only happen
2067 * in AP mode)
2068 */
Johannes Berga729cff2011-11-06 14:13:34 +01002069 multicast = is_multicast_ether_addr(hdr.addr1);
2070 if (!multicast) {
Johannes Bergabe60632009-11-25 17:46:18 +01002071 sta = sta_info_get(sdata, hdr.addr1);
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002072 if (sta) {
2073 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Johannes Berga74a8c82014-07-22 14:50:47 +02002074 wme_sta = sta->sta.wme;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002075 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002076 }
2077
Javier Cardona4777be42011-09-07 17:49:52 -07002078 /* For mesh, the use of the QoS header is mandatory */
2079 if (ieee80211_vif_is_mesh(&sdata->vif))
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002080 wme_sta = true;
Javier Cardona4777be42011-09-07 17:49:52 -07002081
Johannes Berg3434fbd2008-05-03 00:59:37 +02002082 /* receiver and we are QoS enabled, use a QoS type frame */
Johannes Berg32c50572012-03-28 11:04:29 +02002083 if (wme_sta && local->hw.queues >= IEEE80211_NUM_ACS) {
Harvey Harrison065e96052008-06-22 16:45:27 -07002084 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002085 hdrlen += 2;
2086 }
2087
2088 /*
Johannes Berg238814f2008-01-28 17:19:37 +01002089 * Drop unicast frames to unauthorised stations unless they are
2090 * EAPOL frames from the local station.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002091 */
Johannes Berg55182e42011-10-12 17:28:21 +02002092 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
Rostislav Lisovy239281f2014-11-03 10:33:19 +01002093 (sdata->vif.type != NL80211_IFTYPE_OCB) &&
Sergey Ryazanova6ececf2013-08-30 01:35:09 +04002094 !multicast && !authorized &&
Johannes Berg55182e42011-10-12 17:28:21 +02002095 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002096 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002097#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002098 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
Johannes Berg4c9451e2014-11-09 18:50:10 +02002099 sdata->name, hdr.addr1);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002100#endif
2101
2102 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2103
Johannes Berg4c9451e2014-11-09 18:50:10 +02002104 ret = -EPERM;
2105 goto free;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01002106 }
2107
Johannes Berga729cff2011-11-06 14:13:34 +01002108 if (unlikely(!multicast && skb->sk &&
2109 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)) {
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002110 struct sk_buff *ack_skb = skb_clone_sk(skb);
Johannes Berga729cff2011-11-06 14:13:34 +01002111
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002112 if (ack_skb) {
Johannes Berga729cff2011-11-06 14:13:34 +01002113 unsigned long flags;
Tejun Heo9475af62013-02-27 17:04:59 -08002114 int id;
Johannes Berga729cff2011-11-06 14:13:34 +01002115
2116 spin_lock_irqsave(&local->ack_status_lock, flags);
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002117 id = idr_alloc(&local->ack_status_frames, ack_skb,
Tejun Heo9475af62013-02-27 17:04:59 -08002118 1, 0x10000, GFP_ATOMIC);
Johannes Berga729cff2011-11-06 14:13:34 +01002119 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2120
Tejun Heo9475af62013-02-27 17:04:59 -08002121 if (id >= 0) {
Johannes Berga729cff2011-11-06 14:13:34 +01002122 info_id = id;
2123 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Berga729cff2011-11-06 14:13:34 +01002124 } else {
Alexander Duyckbf7fa552014-09-10 18:05:42 -04002125 kfree_skb(ack_skb);
Johannes Berga729cff2011-11-06 14:13:34 +01002126 }
Johannes Berga729cff2011-11-06 14:13:34 +01002127 }
2128 }
2129
Helmut Schaa7e244702010-12-02 18:44:09 +01002130 /*
2131 * If the skb is shared we need to obtain our own copy.
2132 */
2133 if (skb_shared(skb)) {
Johannes Berga729cff2011-11-06 14:13:34 +01002134 struct sk_buff *tmp_skb = skb;
2135
2136 /* can't happen -- skb is a clone if info_id != 0 */
2137 WARN_ON(info_id);
2138
Felix Fietkauf8a0a782010-12-18 19:30:50 +01002139 skb = skb_clone(skb, GFP_ATOMIC);
Helmut Schaa7e244702010-12-02 18:44:09 +01002140 kfree_skb(tmp_skb);
2141
Johannes Berg4c9451e2014-11-09 18:50:10 +02002142 if (!skb) {
2143 ret = -ENOMEM;
2144 goto free;
2145 }
Helmut Schaa7e244702010-12-02 18:44:09 +01002146 }
2147
Harvey Harrison065e96052008-06-22 16:45:27 -07002148 hdr.frame_control = fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02002149 hdr.duration_id = 0;
2150 hdr.seq_ctrl = 0;
2151
2152 skip_header_bytes = ETH_HLEN;
2153 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2154 encaps_data = bridge_tunnel_header;
2155 encaps_len = sizeof(bridge_tunnel_header);
2156 skip_header_bytes -= 2;
Simon Hormane5c5d222013-03-28 13:38:25 +09002157 } else if (ethertype >= ETH_P_802_3_MIN) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002158 encaps_data = rfc1042_header;
2159 encaps_len = sizeof(rfc1042_header);
2160 skip_header_bytes -= 2;
2161 } else {
2162 encaps_data = NULL;
2163 encaps_len = 0;
2164 }
2165
Helmut Schaa7e244702010-12-02 18:44:09 +01002166 nh_pos = skb_network_header(skb) - skb->data;
2167 h_pos = skb_transport_header(skb) - skb->data;
2168
Johannes Berge2ebc742007-07-27 15:43:22 +02002169 skb_pull(skb, skip_header_bytes);
2170 nh_pos -= skip_header_bytes;
2171 h_pos -= skip_header_bytes;
2172
Johannes Berg23c07522008-05-29 10:38:53 +02002173 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002174
Johannes Berg23c07522008-05-29 10:38:53 +02002175 /*
2176 * So we need to modify the skb header and hence need a copy of
2177 * that. The head_need variable above doesn't, so far, include
2178 * the needed header space that we don't need right away. If we
2179 * can, then we don't reallocate right now but only after the
2180 * frame arrives at the master device (if it does...)
2181 *
2182 * If we cannot, however, then we will reallocate to include all
2183 * the ever needed space. Also, if we need to reallocate it anyway,
2184 * make it big enough for everything we may ever need.
2185 */
Johannes Berge2ebc742007-07-27 15:43:22 +02002186
David S. Miller3a5be7d2008-06-18 01:19:51 -07002187 if (head_need > 0 || skb_cloned(skb)) {
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002188 head_need += sdata->encrypt_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02002189 head_need += local->tx_headroom;
2190 head_need = max_t(int, 0, head_need);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002191 if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
2192 ieee80211_free_txskb(&local->hw, skb);
Johannes Berg1c963be2012-11-07 14:02:30 +01002193 skb = NULL;
Johannes Berg4c9451e2014-11-09 18:50:10 +02002194 return ERR_PTR(-ENOMEM);
Felix Fietkauc3e77242012-10-08 14:39:33 +02002195 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002196 }
2197
2198 if (encaps_data) {
2199 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
2200 nh_pos += encaps_len;
2201 h_pos += encaps_len;
2202 }
Johannes Bergc29b9b92007-09-14 11:10:24 -04002203
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002204#ifdef CONFIG_MAC80211_MESH
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002205 if (meshhdrlen > 0) {
2206 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
2207 nh_pos += meshhdrlen;
2208 h_pos += meshhdrlen;
2209 }
Yuri Ershove4ab7eb2010-06-29 15:08:06 +04002210#endif
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002211
Harvey Harrison065e96052008-06-22 16:45:27 -07002212 if (ieee80211_is_data_qos(fc)) {
Johannes Bergc29b9b92007-09-14 11:10:24 -04002213 __le16 *qos_control;
2214
Weilong Chenf359d3f2013-12-18 15:44:16 +08002215 qos_control = (__le16 *) skb_push(skb, 2);
Johannes Bergc29b9b92007-09-14 11:10:24 -04002216 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2217 /*
2218 * Maybe we could actually set some fields here, for now just
2219 * initialise to zero to indicate no special operation.
2220 */
2221 *qos_control = 0;
2222 } else
2223 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2224
Johannes Berge2ebc742007-07-27 15:43:22 +02002225 nh_pos += hdrlen;
2226 h_pos += hdrlen;
2227
Johannes Berge2ebc742007-07-27 15:43:22 +02002228 /* Update skb pointers to various headers since this modified frame
2229 * is going to go through Linux networking code that may potentially
2230 * need things like pointer to IP header. */
2231 skb_set_mac_header(skb, 0);
2232 skb_set_network_header(skb, nh_pos);
2233 skb_set_transport_header(skb, h_pos);
2234
Felix Fietkau489ee912010-12-18 19:30:48 +01002235 info = IEEE80211_SKB_CB(skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002236 memset(info, 0, sizeof(*info));
2237
Johannes Berga729cff2011-11-06 14:13:34 +01002238 info->flags = info_flags;
2239 info->ack_frame_id = info_id;
Johannes Berg73c4e192014-11-09 18:50:09 +02002240 info->band = band;
Johannes Berga729cff2011-11-06 14:13:34 +01002241
Johannes Berg4c9451e2014-11-09 18:50:10 +02002242 return skb;
2243 free:
2244 kfree_skb(skb);
2245 return ERR_PTR(ret);
Johannes Berge2ebc742007-07-27 15:43:22 +02002246}
2247
Johannes Berg4c9451e2014-11-09 18:50:10 +02002248void __ieee80211_subif_start_xmit(struct sk_buff *skb,
2249 struct net_device *dev,
2250 u32 info_flags)
2251{
2252 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg4c9451e2014-11-09 18:50:10 +02002253
2254 if (unlikely(skb->len < ETH_HLEN)) {
2255 kfree_skb(skb);
2256 return;
2257 }
2258
2259 rcu_read_lock();
2260
Johannes Berg4c9451e2014-11-09 18:50:10 +02002261 skb = ieee80211_build_hdr(sdata, skb, info_flags);
2262 if (IS_ERR(skb))
2263 goto out;
2264
2265 dev->stats.tx_packets++;
2266 dev->stats.tx_bytes += skb->len;
2267 dev->trans_start = jiffies;
2268
2269 ieee80211_xmit(sdata, skb);
2270 out:
2271 rcu_read_unlock();
2272}
2273
2274/**
2275 * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
2276 * @skb: packet to be sent
2277 * @dev: incoming interface
2278 *
2279 * On failure skb will be freed.
2280 */
Liad Kaufman24d342c2014-11-09 18:50:07 +02002281netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
2282 struct net_device *dev)
2283{
2284 __ieee80211_subif_start_xmit(skb, dev, 0);
2285 return NETDEV_TX_OK;
2286}
Johannes Berge2ebc742007-07-27 15:43:22 +02002287
Johannes Berg7528ec52014-11-09 18:50:11 +02002288struct sk_buff *
2289ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
2290 struct sk_buff *skb, u32 info_flags)
2291{
2292 struct ieee80211_hdr *hdr;
2293 struct ieee80211_tx_data tx = {
2294 .local = sdata->local,
2295 .sdata = sdata,
2296 };
2297
2298 rcu_read_lock();
2299
2300 skb = ieee80211_build_hdr(sdata, skb, info_flags);
2301 if (IS_ERR(skb))
2302 goto out;
2303
2304 hdr = (void *)skb->data;
2305 tx.sta = sta_info_get(sdata, hdr->addr1);
2306 tx.skb = skb;
2307
2308 if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
2309 rcu_read_unlock();
2310 kfree_skb(skb);
2311 return ERR_PTR(-EINVAL);
2312 }
2313
2314out:
2315 rcu_read_unlock();
2316 return skb;
2317}
2318
Johannes Berge2530082008-05-17 00:57:14 +02002319/*
2320 * ieee80211_clear_tx_pending may not be called in a context where
2321 * it is possible that it packets could come in again.
2322 */
Johannes Berge2ebc742007-07-27 15:43:22 +02002323void ieee80211_clear_tx_pending(struct ieee80211_local *local)
2324{
Felix Fietkau1f98ab72012-11-10 03:44:14 +01002325 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01002326 int i;
Johannes Berge2ebc742007-07-27 15:43:22 +02002327
Felix Fietkau1f98ab72012-11-10 03:44:14 +01002328 for (i = 0; i < local->hw.queues; i++) {
2329 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
2330 ieee80211_free_txskb(&local->hw, skb);
2331 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002332}
2333
Johannes Berg7bb45682011-02-24 14:42:06 +01002334/*
2335 * Returns false if the frame couldn't be transmitted but was queued instead,
2336 * which in this case means re-queued -- take as an indication to stop sending
2337 * more pending frames.
2338 */
Johannes Bergcd8ffc82009-03-23 17:28:41 +01002339static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
2340 struct sk_buff *skb)
2341{
2342 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2343 struct ieee80211_sub_if_data *sdata;
2344 struct sta_info *sta;
2345 struct ieee80211_hdr *hdr;
Johannes Berg7bb45682011-02-24 14:42:06 +01002346 bool result;
Johannes Berg55de9082012-07-26 17:24:39 +02002347 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01002348
Johannes Berg5061b0c2009-07-14 00:33:34 +02002349 sdata = vif_to_sdata(info->control.vif);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01002350
2351 if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
Johannes Berg55de9082012-07-26 17:24:39 +02002352 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2353 if (unlikely(!chanctx_conf)) {
2354 dev_kfree_skb(skb);
2355 return true;
2356 }
Johannes Berg73c4e192014-11-09 18:50:09 +02002357 info->band = chanctx_conf->def.chan->band;
2358 result = ieee80211_tx(sdata, skb, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01002359 } else {
Johannes Berg252b86c2011-11-16 15:28:55 +01002360 struct sk_buff_head skbs;
2361
2362 __skb_queue_head_init(&skbs);
2363 __skb_queue_tail(&skbs, skb);
2364
Johannes Bergcd8ffc82009-03-23 17:28:41 +01002365 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Bergabe60632009-11-25 17:46:18 +01002366 sta = sta_info_get(sdata, hdr->addr1);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01002367
Johannes Berg74e4dbf2011-11-16 15:28:57 +01002368 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01002369 }
2370
Johannes Bergcd8ffc82009-03-23 17:28:41 +01002371 return result;
2372}
2373
Johannes Berge2530082008-05-17 00:57:14 +02002374/*
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002375 * Transmit all pending packets. Called from tasklet.
Johannes Berge2530082008-05-17 00:57:14 +02002376 */
Johannes Berge2ebc742007-07-27 15:43:22 +02002377void ieee80211_tx_pending(unsigned long data)
2378{
2379 struct ieee80211_local *local = (struct ieee80211_local *)data;
Johannes Berg2a577d92009-03-23 17:28:37 +01002380 unsigned long flags;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01002381 int i;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002382 bool txok;
Johannes Berge2ebc742007-07-27 15:43:22 +02002383
Johannes Bergf0e72852009-03-23 17:28:36 +01002384 rcu_read_lock();
Johannes Berg2a577d92009-03-23 17:28:37 +01002385
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002386 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg176be722009-03-12 23:49:28 +01002387 for (i = 0; i < local->hw.queues; i++) {
Johannes Berg2a577d92009-03-23 17:28:37 +01002388 /*
2389 * If queue is stopped by something other than due to pending
2390 * frames, or we have no pending frames, proceed to next queue.
2391 */
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002392 if (local->queue_stop_reasons[i] ||
Johannes Berg2a577d92009-03-23 17:28:37 +01002393 skb_queue_empty(&local->pending[i]))
Johannes Berge2ebc742007-07-27 15:43:22 +02002394 continue;
Johannes Berge2530082008-05-17 00:57:14 +02002395
Johannes Berg2a577d92009-03-23 17:28:37 +01002396 while (!skb_queue_empty(&local->pending[i])) {
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002397 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
Johannes Berg5061b0c2009-07-14 00:33:34 +02002398 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg5061b0c2009-07-14 00:33:34 +02002399
Johannes Berga7bc3762009-07-27 10:33:31 +02002400 if (WARN_ON(!info->control.vif)) {
Felix Fietkauc3e77242012-10-08 14:39:33 +02002401 ieee80211_free_txskb(&local->hw, skb);
Johannes Berga7bc3762009-07-27 10:33:31 +02002402 continue;
2403 }
2404
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002405 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
2406 flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01002407
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002408 txok = ieee80211_tx_pending_skb(local, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002409 spin_lock_irqsave(&local->queue_stop_reason_lock,
2410 flags);
2411 if (!txok)
Johannes Berg2a577d92009-03-23 17:28:37 +01002412 break;
Johannes Berge2ebc742007-07-27 15:43:22 +02002413 }
Johannes Berg7236fe22010-03-22 13:42:43 -07002414
2415 if (skb_queue_empty(&local->pending[i]))
Johannes Berg3a25a8c2012-04-03 16:28:50 +02002416 ieee80211_propagate_queue_wake(local, i);
Johannes Berge2ebc742007-07-27 15:43:22 +02002417 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002418 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01002419
Johannes Bergf0e72852009-03-23 17:28:36 +01002420 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02002421}
2422
2423/* functions for drivers to get certain frames */
2424
Marco Porscheac70c12013-01-07 16:04:50 +01002425static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03002426 struct ps_data *ps, struct sk_buff *skb,
2427 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02002428{
2429 u8 *pos, *tim;
2430 int aid0 = 0;
2431 int i, have_bits = 0, n1, n2;
2432
2433 /* Generate bitmap for TIM only if there are any STAs in power save
2434 * mode. */
Marco Porschd012a602012-10-10 12:39:50 -07002435 if (atomic_read(&ps->num_sta_ps) > 0)
Johannes Berge2ebc742007-07-27 15:43:22 +02002436 /* in the hope that this is faster than
2437 * checking byte-for-byte */
Weilong Chenf359d3f2013-12-18 15:44:16 +08002438 have_bits = !bitmap_empty((unsigned long *)ps->tim,
Johannes Berge2ebc742007-07-27 15:43:22 +02002439 IEEE80211_MAX_AID+1);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03002440 if (!is_template) {
2441 if (ps->dtim_count == 0)
2442 ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
2443 else
2444 ps->dtim_count--;
2445 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002446
2447 tim = pos = (u8 *) skb_put(skb, 6);
2448 *pos++ = WLAN_EID_TIM;
2449 *pos++ = 4;
Marco Porschd012a602012-10-10 12:39:50 -07002450 *pos++ = ps->dtim_count;
Johannes Berg88600202012-02-13 15:17:18 +01002451 *pos++ = sdata->vif.bss_conf.dtim_period;
Johannes Berge2ebc742007-07-27 15:43:22 +02002452
Marco Porschd012a602012-10-10 12:39:50 -07002453 if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
Johannes Berge2ebc742007-07-27 15:43:22 +02002454 aid0 = 1;
2455
Marco Porschd012a602012-10-10 12:39:50 -07002456 ps->dtim_bc_mc = aid0 == 1;
Christian Lamparter512119b2011-01-31 20:48:44 +02002457
Johannes Berge2ebc742007-07-27 15:43:22 +02002458 if (have_bits) {
2459 /* Find largest even number N1 so that bits numbered 1 through
2460 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
2461 * (N2 + 1) x 8 through 2007 are 0. */
2462 n1 = 0;
2463 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
Marco Porschd012a602012-10-10 12:39:50 -07002464 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002465 n1 = i & 0xfe;
2466 break;
2467 }
2468 }
2469 n2 = n1;
2470 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
Marco Porschd012a602012-10-10 12:39:50 -07002471 if (ps->tim[i]) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002472 n2 = i;
2473 break;
2474 }
2475 }
2476
2477 /* Bitmap control */
2478 *pos++ = n1 | aid0;
2479 /* Part Virt Bitmap */
Eliad Peller5220da32011-11-24 16:50:00 +02002480 skb_put(skb, n2 - n1);
Marco Porschd012a602012-10-10 12:39:50 -07002481 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
Johannes Berge2ebc742007-07-27 15:43:22 +02002482
2483 tim[1] = n2 - n1 + 4;
Johannes Berge2ebc742007-07-27 15:43:22 +02002484 } else {
2485 *pos++ = aid0; /* Bitmap control */
2486 *pos++ = 0; /* Part Virt Bitmap */
2487 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002488}
2489
Marco Porscheac70c12013-01-07 16:04:50 +01002490static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03002491 struct ps_data *ps, struct sk_buff *skb,
2492 bool is_template)
Marco Porscheac70c12013-01-07 16:04:50 +01002493{
2494 struct ieee80211_local *local = sdata->local;
2495
2496 /*
2497 * Not very nice, but we want to allow the driver to call
2498 * ieee80211_beacon_get() as a response to the set_tim()
2499 * callback. That, however, is already invoked under the
2500 * sta_lock to guarantee consistent and race-free update
2501 * of the tim bitmap in mac80211 and the driver.
2502 */
2503 if (local->tim_in_locked_section) {
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03002504 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Marco Porscheac70c12013-01-07 16:04:50 +01002505 } else {
Johannes Berg1b917312013-02-22 12:55:01 +01002506 spin_lock_bh(&local->tim_lock);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03002507 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
Johannes Berg1b917312013-02-22 12:55:01 +01002508 spin_unlock_bh(&local->tim_lock);
Marco Porscheac70c12013-01-07 16:04:50 +01002509 }
2510
2511 return 0;
2512}
2513
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03002514static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
2515 struct beacon_data *beacon)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02002516{
2517 struct probe_resp *resp;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002518 u8 *beacon_data;
2519 size_t beacon_data_len;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03002520 int i;
Michal Kazioraf296bd2014-06-05 14:21:36 +02002521 u8 count = beacon->csa_current_counter;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02002522
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002523 switch (sdata->vif.type) {
2524 case NL80211_IFTYPE_AP:
2525 beacon_data = beacon->tail;
2526 beacon_data_len = beacon->tail_len;
2527 break;
2528 case NL80211_IFTYPE_ADHOC:
2529 beacon_data = beacon->head;
2530 beacon_data_len = beacon->head_len;
2531 break;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07002532 case NL80211_IFTYPE_MESH_POINT:
2533 beacon_data = beacon->head;
2534 beacon_data_len = beacon->head_len;
2535 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002536 default:
2537 return;
2538 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02002539
Michal Kazioraf296bd2014-06-05 14:21:36 +02002540 rcu_read_lock();
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03002541 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02002542 resp = rcu_dereference(sdata->u.ap.probe_resp);
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03002543
Michal Kazioraf296bd2014-06-05 14:21:36 +02002544 if (beacon->csa_counter_offsets[i]) {
2545 if (WARN_ON_ONCE(beacon->csa_counter_offsets[i] >=
2546 beacon_data_len)) {
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03002547 rcu_read_unlock();
2548 return;
2549 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02002550
2551 beacon_data[beacon->csa_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03002552 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02002553
2554 if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
2555 resp->data[resp->csa_counter_offsets[i]] = count;
Andrei Otcheretianski0d06d9b2014-05-09 14:11:47 +03002556 }
Michal Kazioraf296bd2014-06-05 14:21:36 +02002557 rcu_read_unlock();
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03002558}
2559
2560u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
2561{
2562 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Michal Kazioraf296bd2014-06-05 14:21:36 +02002563 struct beacon_data *beacon = NULL;
2564 u8 count = 0;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02002565
Michal Kazioraf296bd2014-06-05 14:21:36 +02002566 rcu_read_lock();
2567
2568 if (sdata->vif.type == NL80211_IFTYPE_AP)
2569 beacon = rcu_dereference(sdata->u.ap.beacon);
2570 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
2571 beacon = rcu_dereference(sdata->u.ibss.presp);
2572 else if (ieee80211_vif_is_mesh(&sdata->vif))
2573 beacon = rcu_dereference(sdata->u.mesh.beacon);
2574
2575 if (!beacon)
2576 goto unlock;
2577
2578 beacon->csa_current_counter--;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03002579
2580 /* the counter should never reach 0 */
Michal Kazioraf296bd2014-06-05 14:21:36 +02002581 WARN_ON_ONCE(!beacon->csa_current_counter);
2582 count = beacon->csa_current_counter;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03002583
Michal Kazioraf296bd2014-06-05 14:21:36 +02002584unlock:
2585 rcu_read_unlock();
2586 return count;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02002587}
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03002588EXPORT_SYMBOL(ieee80211_csa_update_counter);
Simon Wunderlich73da7d52013-07-11 16:09:06 +02002589
2590bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
2591{
2592 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2593 struct beacon_data *beacon = NULL;
2594 u8 *beacon_data;
2595 size_t beacon_data_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02002596 int ret = false;
2597
2598 if (!ieee80211_sdata_running(sdata))
2599 return false;
2600
2601 rcu_read_lock();
2602 if (vif->type == NL80211_IFTYPE_AP) {
2603 struct ieee80211_if_ap *ap = &sdata->u.ap;
2604
2605 beacon = rcu_dereference(ap->beacon);
2606 if (WARN_ON(!beacon || !beacon->tail))
2607 goto out;
2608 beacon_data = beacon->tail;
2609 beacon_data_len = beacon->tail_len;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002610 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
2611 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2612
2613 beacon = rcu_dereference(ifibss->presp);
2614 if (!beacon)
2615 goto out;
2616
2617 beacon_data = beacon->head;
2618 beacon_data_len = beacon->head_len;
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07002619 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
2620 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2621
2622 beacon = rcu_dereference(ifmsh->beacon);
2623 if (!beacon)
2624 goto out;
2625
2626 beacon_data = beacon->head;
2627 beacon_data_len = beacon->head_len;
Simon Wunderlich73da7d52013-07-11 16:09:06 +02002628 } else {
2629 WARN_ON(1);
2630 goto out;
2631 }
2632
Michal Kazior10d78f22014-06-05 14:21:37 +02002633 if (!beacon->csa_counter_offsets[0])
2634 goto out;
2635
Michal Kazioraf296bd2014-06-05 14:21:36 +02002636 if (WARN_ON_ONCE(beacon->csa_counter_offsets[0] > beacon_data_len))
Simon Wunderlich73da7d52013-07-11 16:09:06 +02002637 goto out;
2638
Michal Kazioraf296bd2014-06-05 14:21:36 +02002639 if (beacon_data[beacon->csa_counter_offsets[0]] == 1)
Simon Wunderlich73da7d52013-07-11 16:09:06 +02002640 ret = true;
2641 out:
2642 rcu_read_unlock();
2643
2644 return ret;
2645}
2646EXPORT_SYMBOL(ieee80211_csa_is_complete);
2647
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03002648static struct sk_buff *
2649__ieee80211_beacon_get(struct ieee80211_hw *hw,
2650 struct ieee80211_vif *vif,
2651 struct ieee80211_mutable_offsets *offs,
2652 bool is_template)
Johannes Berge2ebc742007-07-27 15:43:22 +02002653{
2654 struct ieee80211_local *local = hw_to_local(hw);
Michal Kazioraf296bd2014-06-05 14:21:36 +02002655 struct beacon_data *beacon = NULL;
Johannes Berg9d139c82008-07-09 14:40:37 +02002656 struct sk_buff *skb = NULL;
Johannes Berge039fa42008-05-15 12:55:29 +02002657 struct ieee80211_tx_info *info;
Johannes Berge2ebc742007-07-27 15:43:22 +02002658 struct ieee80211_sub_if_data *sdata = NULL;
Johannes Berg55de9082012-07-26 17:24:39 +02002659 enum ieee80211_band band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02002660 struct ieee80211_tx_rate_control txrc;
Johannes Berg55de9082012-07-26 17:24:39 +02002661 struct ieee80211_chanctx_conf *chanctx_conf;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03002662 int csa_off_base = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002663
Johannes Berg5dfdaf52007-12-19 02:03:33 +01002664 rcu_read_lock();
Johannes Berge2ebc742007-07-27 15:43:22 +02002665
Johannes Berg32bfd352007-12-19 01:31:26 +01002666 sdata = vif_to_sdata(vif);
Johannes Berg55de9082012-07-26 17:24:39 +02002667 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berge2ebc742007-07-27 15:43:22 +02002668
Johannes Berg55de9082012-07-26 17:24:39 +02002669 if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
Felix Fietkaueb3e5542011-01-24 19:28:49 +01002670 goto out;
2671
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03002672 if (offs)
2673 memset(offs, 0, sizeof(*offs));
Johannes Bergeddcbb942009-10-29 08:30:35 +01002674
Johannes Berg05c914f2008-09-11 00:01:58 +02002675 if (sdata->vif.type == NL80211_IFTYPE_AP) {
Marco Porschd012a602012-10-10 12:39:50 -07002676 struct ieee80211_if_ap *ap = &sdata->u.ap;
Marco Porschd012a602012-10-10 12:39:50 -07002677
Michal Kazioraf296bd2014-06-05 14:21:36 +02002678 beacon = rcu_dereference(ap->beacon);
Dan Carpenter3ad97fb2011-02-07 22:03:35 +03002679 if (beacon) {
Michal Kazior10d78f22014-06-05 14:21:37 +02002680 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03002681 if (!is_template)
2682 ieee80211_csa_update_counter(vif);
2683
2684 ieee80211_set_csa(sdata, beacon);
2685 }
Simon Wunderlich73da7d52013-07-11 16:09:06 +02002686
Johannes Berg902acc72008-02-23 15:17:19 +01002687 /*
2688 * headroom, head length,
2689 * tail length and maximum TIM length
2690 */
2691 skb = dev_alloc_skb(local->tx_headroom +
2692 beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01002693 beacon->tail_len + 256 +
2694 local->hw.extra_beacon_tailroom);
Johannes Berg902acc72008-02-23 15:17:19 +01002695 if (!skb)
2696 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01002697
Johannes Berg902acc72008-02-23 15:17:19 +01002698 skb_reserve(skb, local->tx_headroom);
2699 memcpy(skb_put(skb, beacon->head_len), beacon->head,
2700 beacon->head_len);
2701
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03002702 ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
2703 is_template);
Johannes Berg902acc72008-02-23 15:17:19 +01002704
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03002705 if (offs) {
2706 offs->tim_offset = beacon->head_len;
2707 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03002708
2709 /* for AP the csa offsets are from tail */
2710 csa_off_base = skb->len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03002711 }
Johannes Bergeddcbb942009-10-29 08:30:35 +01002712
Johannes Berg902acc72008-02-23 15:17:19 +01002713 if (beacon->tail)
2714 memcpy(skb_put(skb, beacon->tail_len),
2715 beacon->tail, beacon->tail_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02002716 } else
2717 goto out;
Johannes Berg05c914f2008-09-11 00:01:58 +02002718 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Berg46900292009-02-15 12:44:28 +01002719 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg9d139c82008-07-09 14:40:37 +02002720 struct ieee80211_hdr *hdr;
Johannes Berg902acc72008-02-23 15:17:19 +01002721
Michal Kazioraf296bd2014-06-05 14:21:36 +02002722 beacon = rcu_dereference(ifibss->presp);
2723 if (!beacon)
Johannes Berg9d139c82008-07-09 14:40:37 +02002724 goto out;
2725
Michal Kazior10d78f22014-06-05 14:21:37 +02002726 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03002727 if (!is_template)
2728 ieee80211_csa_update_counter(vif);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002729
Michal Kazioraf296bd2014-06-05 14:21:36 +02002730 ieee80211_set_csa(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03002731 }
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02002732
Michal Kazioraf296bd2014-06-05 14:21:36 +02002733 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01002734 local->hw.extra_beacon_tailroom);
Johannes Berg9d139c82008-07-09 14:40:37 +02002735 if (!skb)
2736 goto out;
Johannes Bergc3ffeab2013-03-07 20:54:29 +01002737 skb_reserve(skb, local->tx_headroom);
Michal Kazioraf296bd2014-06-05 14:21:36 +02002738 memcpy(skb_put(skb, beacon->head_len), beacon->head,
2739 beacon->head_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02002740
2741 hdr = (struct ieee80211_hdr *) skb->data;
Harvey Harrisone7827a72008-07-15 18:44:13 -07002742 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2743 IEEE80211_STYPE_BEACON);
Johannes Berg902acc72008-02-23 15:17:19 +01002744 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
Javier Cardonadbf498f2012-03-31 11:31:32 -07002745 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Johannes Berg472dbc42008-09-11 00:01:49 +02002746
Michal Kazioraf296bd2014-06-05 14:21:36 +02002747 beacon = rcu_dereference(ifmsh->beacon);
2748 if (!beacon)
Johannes Bergac1bd842011-01-18 13:45:32 +01002749 goto out;
Johannes Bergac1bd842011-01-18 13:45:32 +01002750
Michal Kazior10d78f22014-06-05 14:21:37 +02002751 if (beacon->csa_counter_offsets[0]) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03002752 if (!is_template)
2753 /* TODO: For mesh csa_counter is in TU, so
2754 * decrementing it by one isn't correct, but
2755 * for now we leave it consistent with overall
2756 * mac80211's behavior.
2757 */
2758 ieee80211_csa_update_counter(vif);
2759
Michal Kazioraf296bd2014-06-05 14:21:36 +02002760 ieee80211_set_csa(sdata, beacon);
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03002761 }
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -07002762
Javier Cardonadbf498f2012-03-31 11:31:32 -07002763 if (ifmsh->sync_ops)
Michal Kazioraf296bd2014-06-05 14:21:36 +02002764 ifmsh->sync_ops->adjust_tbtt(sdata, beacon);
Javier Cardonadbf498f2012-03-31 11:31:32 -07002765
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -07002766 skb = dev_alloc_skb(local->tx_headroom +
Michal Kazioraf296bd2014-06-05 14:21:36 +02002767 beacon->head_len +
Marco Porsch3f52b7e2013-01-30 18:14:08 +01002768 256 + /* TIM IE */
Michal Kazioraf296bd2014-06-05 14:21:36 +02002769 beacon->tail_len +
Felix Fietkau70dabeb2013-12-14 13:54:53 +01002770 local->hw.extra_beacon_tailroom);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002771 if (!skb)
2772 goto out;
Thomas Pedersen2b5e1962013-02-14 11:20:13 -08002773 skb_reserve(skb, local->tx_headroom);
Michal Kazioraf296bd2014-06-05 14:21:36 +02002774 memcpy(skb_put(skb, beacon->head_len), beacon->head,
2775 beacon->head_len);
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03002776 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
2777
2778 if (offs) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02002779 offs->tim_offset = beacon->head_len;
2780 offs->tim_length = skb->len - beacon->head_len;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03002781 }
2782
Michal Kazioraf296bd2014-06-05 14:21:36 +02002783 memcpy(skb_put(skb, beacon->tail_len), beacon->tail,
2784 beacon->tail_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02002785 } else {
2786 WARN_ON(1);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01002787 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02002788 }
2789
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03002790 /* CSA offsets */
Michal Kazioraf296bd2014-06-05 14:21:36 +02002791 if (offs && beacon) {
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03002792 int i;
2793
2794 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
Michal Kazioraf296bd2014-06-05 14:21:36 +02002795 u16 csa_off = beacon->csa_counter_offsets[i];
Andrei Otcheretianski1af586c2014-05-09 14:11:50 +03002796
2797 if (!csa_off)
2798 continue;
2799
2800 offs->csa_counter_offs[i] = csa_off_base + csa_off;
2801 }
2802 }
2803
Johannes Berg4bf88532012-11-09 11:39:59 +01002804 band = chanctx_conf->def.chan->band;
Johannes Berg55de9082012-07-26 17:24:39 +02002805
Johannes Berge039fa42008-05-15 12:55:29 +02002806 info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002807
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002808 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Jouni Malinene00cfce2009-12-29 12:59:19 +02002809 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Johannes Berge039fa42008-05-15 12:55:29 +02002810 info->band = band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02002811
2812 memset(&txrc, 0, sizeof(txrc));
2813 txrc.hw = hw;
Johannes Berge31583c2012-07-26 14:07:46 +02002814 txrc.sband = local->hw.wiphy->bands[band];
Jouni Malinene00cfce2009-12-29 12:59:19 +02002815 txrc.bss_conf = &sdata->vif.bss_conf;
2816 txrc.skb = skb;
2817 txrc.reported_rate.idx = -1;
Jouni Malinen37eb0b12010-01-06 13:09:08 +02002818 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
Johannes Berge31583c2012-07-26 14:07:46 +02002819 if (txrc.rate_idx_mask == (1 << txrc.sband->n_bitrates) - 1)
Jouni Malinen37eb0b12010-01-06 13:09:08 +02002820 txrc.max_rate_idx = -1;
2821 else
2822 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
Felix Fietkau8f0729b2010-11-11 15:07:23 +01002823 txrc.bss = true;
Jouni Malinene00cfce2009-12-29 12:59:19 +02002824 rate_control_get_rate(sdata, NULL, &txrc);
Johannes Berge039fa42008-05-15 12:55:29 +02002825
2826 info->control.vif = vif;
Johannes Bergf591fa52008-07-10 11:21:26 +02002827
John W. Linvillea472e712010-05-06 14:45:17 -04002828 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
2829 IEEE80211_TX_CTL_ASSIGN_SEQ |
2830 IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge6a98542008-10-21 12:40:02 +02002831 out:
Johannes Berg5dfdaf52007-12-19 02:03:33 +01002832 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02002833 return skb;
Andrei Otcheretianski6ec8c332014-05-09 14:11:49 +03002834
2835}
2836
2837struct sk_buff *
2838ieee80211_beacon_get_template(struct ieee80211_hw *hw,
2839 struct ieee80211_vif *vif,
2840 struct ieee80211_mutable_offsets *offs)
2841{
2842 return __ieee80211_beacon_get(hw, vif, offs, true);
2843}
2844EXPORT_SYMBOL(ieee80211_beacon_get_template);
2845
2846struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
2847 struct ieee80211_vif *vif,
2848 u16 *tim_offset, u16 *tim_length)
2849{
2850 struct ieee80211_mutable_offsets offs = {};
2851 struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
2852
2853 if (tim_offset)
2854 *tim_offset = offs.tim_offset;
2855
2856 if (tim_length)
2857 *tim_length = offs.tim_length;
2858
2859 return bcn;
Johannes Berge2ebc742007-07-27 15:43:22 +02002860}
Johannes Bergeddcbb942009-10-29 08:30:35 +01002861EXPORT_SYMBOL(ieee80211_beacon_get_tim);
Johannes Berge2ebc742007-07-27 15:43:22 +02002862
Arik Nemtsov02945822011-11-10 11:28:57 +02002863struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
2864 struct ieee80211_vif *vif)
2865{
2866 struct ieee80211_if_ap *ap = NULL;
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03002867 struct sk_buff *skb = NULL;
2868 struct probe_resp *presp = NULL;
Arik Nemtsov02945822011-11-10 11:28:57 +02002869 struct ieee80211_hdr *hdr;
2870 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2871
2872 if (sdata->vif.type != NL80211_IFTYPE_AP)
2873 return NULL;
2874
2875 rcu_read_lock();
2876
2877 ap = &sdata->u.ap;
2878 presp = rcu_dereference(ap->probe_resp);
2879 if (!presp)
2880 goto out;
2881
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03002882 skb = dev_alloc_skb(presp->len);
Arik Nemtsov02945822011-11-10 11:28:57 +02002883 if (!skb)
2884 goto out;
2885
Eyal Shapiraaa7a0082012-08-06 14:26:16 +03002886 memcpy(skb_put(skb, presp->len), presp->data, presp->len);
2887
Arik Nemtsov02945822011-11-10 11:28:57 +02002888 hdr = (struct ieee80211_hdr *) skb->data;
2889 memset(hdr->addr1, 0, sizeof(hdr->addr1));
2890
2891out:
2892 rcu_read_unlock();
2893 return skb;
2894}
2895EXPORT_SYMBOL(ieee80211_proberesp_get);
2896
Kalle Valo7044cc52010-01-05 20:16:19 +02002897struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
2898 struct ieee80211_vif *vif)
2899{
2900 struct ieee80211_sub_if_data *sdata;
2901 struct ieee80211_if_managed *ifmgd;
2902 struct ieee80211_pspoll *pspoll;
2903 struct ieee80211_local *local;
2904 struct sk_buff *skb;
2905
2906 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2907 return NULL;
2908
2909 sdata = vif_to_sdata(vif);
2910 ifmgd = &sdata->u.mgd;
2911 local = sdata->local;
2912
2913 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
Joe Perchesd15b8452011-08-29 14:17:31 -07002914 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02002915 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07002916
Kalle Valo7044cc52010-01-05 20:16:19 +02002917 skb_reserve(skb, local->hw.extra_tx_headroom);
2918
2919 pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
2920 memset(pspoll, 0, sizeof(*pspoll));
2921 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
2922 IEEE80211_STYPE_PSPOLL);
2923 pspoll->aid = cpu_to_le16(ifmgd->aid);
2924
2925 /* aid in PS-Poll has its two MSBs each set to 1 */
2926 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
2927
2928 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
2929 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
2930
2931 return skb;
2932}
2933EXPORT_SYMBOL(ieee80211_pspoll_get);
2934
2935struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
2936 struct ieee80211_vif *vif)
2937{
2938 struct ieee80211_hdr_3addr *nullfunc;
2939 struct ieee80211_sub_if_data *sdata;
2940 struct ieee80211_if_managed *ifmgd;
2941 struct ieee80211_local *local;
2942 struct sk_buff *skb;
2943
2944 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2945 return NULL;
2946
2947 sdata = vif_to_sdata(vif);
2948 ifmgd = &sdata->u.mgd;
2949 local = sdata->local;
2950
2951 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*nullfunc));
Joe Perchesd15b8452011-08-29 14:17:31 -07002952 if (!skb)
Kalle Valo7044cc52010-01-05 20:16:19 +02002953 return NULL;
Joe Perchesd15b8452011-08-29 14:17:31 -07002954
Kalle Valo7044cc52010-01-05 20:16:19 +02002955 skb_reserve(skb, local->hw.extra_tx_headroom);
2956
2957 nullfunc = (struct ieee80211_hdr_3addr *) skb_put(skb,
2958 sizeof(*nullfunc));
2959 memset(nullfunc, 0, sizeof(*nullfunc));
2960 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
2961 IEEE80211_STYPE_NULLFUNC |
2962 IEEE80211_FCTL_TODS);
2963 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
2964 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
2965 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
2966
2967 return skb;
2968}
2969EXPORT_SYMBOL(ieee80211_nullfunc_get);
2970
Kalle Valo05e54ea2010-01-05 20:16:38 +02002971struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
Johannes Berga344d672014-06-12 22:24:31 +02002972 const u8 *src_addr,
Kalle Valo05e54ea2010-01-05 20:16:38 +02002973 const u8 *ssid, size_t ssid_len,
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002974 size_t tailroom)
Kalle Valo05e54ea2010-01-05 20:16:38 +02002975{
Johannes Berga344d672014-06-12 22:24:31 +02002976 struct ieee80211_local *local = hw_to_local(hw);
Kalle Valo05e54ea2010-01-05 20:16:38 +02002977 struct ieee80211_hdr_3addr *hdr;
2978 struct sk_buff *skb;
2979 size_t ie_ssid_len;
2980 u8 *pos;
2981
Kalle Valo05e54ea2010-01-05 20:16:38 +02002982 ie_ssid_len = 2 + ssid_len;
2983
2984 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
Johannes Bergb9a9ada2012-11-29 13:00:10 +01002985 ie_ssid_len + tailroom);
Joe Perchesd15b8452011-08-29 14:17:31 -07002986 if (!skb)
Kalle Valo05e54ea2010-01-05 20:16:38 +02002987 return NULL;
Kalle Valo05e54ea2010-01-05 20:16:38 +02002988
2989 skb_reserve(skb, local->hw.extra_tx_headroom);
2990
2991 hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
2992 memset(hdr, 0, sizeof(*hdr));
2993 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2994 IEEE80211_STYPE_PROBE_REQ);
Johannes Berge83e6542012-07-13 16:23:07 +02002995 eth_broadcast_addr(hdr->addr1);
Johannes Berga344d672014-06-12 22:24:31 +02002996 memcpy(hdr->addr2, src_addr, ETH_ALEN);
Johannes Berge83e6542012-07-13 16:23:07 +02002997 eth_broadcast_addr(hdr->addr3);
Kalle Valo05e54ea2010-01-05 20:16:38 +02002998
2999 pos = skb_put(skb, ie_ssid_len);
3000 *pos++ = WLAN_EID_SSID;
3001 *pos++ = ssid_len;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02003002 if (ssid_len)
Kalle Valo05e54ea2010-01-05 20:16:38 +02003003 memcpy(pos, ssid, ssid_len);
3004 pos += ssid_len;
3005
Kalle Valo05e54ea2010-01-05 20:16:38 +02003006 return skb;
3007}
3008EXPORT_SYMBOL(ieee80211_probereq_get);
3009
Johannes Berg32bfd352007-12-19 01:31:26 +01003010void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02003011 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02003012 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02003013 struct ieee80211_rts *rts)
3014{
3015 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02003016
Harvey Harrison065e96052008-06-22 16:45:27 -07003017 rts->frame_control =
3018 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01003019 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
3020 frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02003021 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
3022 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
3023}
3024EXPORT_SYMBOL(ieee80211_rts_get);
3025
Johannes Berg32bfd352007-12-19 01:31:26 +01003026void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02003027 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02003028 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02003029 struct ieee80211_cts *cts)
3030{
3031 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02003032
Harvey Harrison065e96052008-06-22 16:45:27 -07003033 cts->frame_control =
3034 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01003035 cts->duration = ieee80211_ctstoself_duration(hw, vif,
3036 frame_len, frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02003037 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
3038}
3039EXPORT_SYMBOL(ieee80211_ctstoself_get);
3040
3041struct sk_buff *
Johannes Berg32bfd352007-12-19 01:31:26 +01003042ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
Johannes Berge039fa42008-05-15 12:55:29 +02003043 struct ieee80211_vif *vif)
Johannes Berge2ebc742007-07-27 15:43:22 +02003044{
3045 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winkler747cf5e2008-05-27 17:50:51 +03003046 struct sk_buff *skb = NULL;
Johannes Berg5cf121c2008-02-25 16:27:43 +01003047 struct ieee80211_tx_data tx;
Johannes Berge2ebc742007-07-27 15:43:22 +02003048 struct ieee80211_sub_if_data *sdata;
Marco Porschd012a602012-10-10 12:39:50 -07003049 struct ps_data *ps;
Johannes Berge039fa42008-05-15 12:55:29 +02003050 struct ieee80211_tx_info *info;
Johannes Berg55de9082012-07-26 17:24:39 +02003051 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berge2ebc742007-07-27 15:43:22 +02003052
Johannes Berg32bfd352007-12-19 01:31:26 +01003053 sdata = vif_to_sdata(vif);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01003054
Johannes Berg5dfdaf52007-12-19 02:03:33 +01003055 rcu_read_lock();
Johannes Berg55de9082012-07-26 17:24:39 +02003056 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01003057
Marco Porschd012a602012-10-10 12:39:50 -07003058 if (!chanctx_conf)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03003059 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01003060
Marco Porschd012a602012-10-10 12:39:50 -07003061 if (sdata->vif.type == NL80211_IFTYPE_AP) {
3062 struct beacon_data *beacon =
3063 rcu_dereference(sdata->u.ap.beacon);
3064
3065 if (!beacon || !beacon->head)
3066 goto out;
3067
3068 ps = &sdata->u.ap.ps;
Marco Porsch3f52b7e2013-01-30 18:14:08 +01003069 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
3070 ps = &sdata->u.mesh.ps;
Marco Porschd012a602012-10-10 12:39:50 -07003071 } else {
3072 goto out;
3073 }
3074
3075 if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03003076 goto out; /* send buffered bc/mc only after DTIM beacon */
Johannes Berge039fa42008-05-15 12:55:29 +02003077
Johannes Berge2ebc742007-07-27 15:43:22 +02003078 while (1) {
Marco Porschd012a602012-10-10 12:39:50 -07003079 skb = skb_dequeue(&ps->bc_buf);
Johannes Berge2ebc742007-07-27 15:43:22 +02003080 if (!skb)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03003081 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02003082 local->total_ps_buffered--;
3083
Marco Porschd012a602012-10-10 12:39:50 -07003084 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
Johannes Berge2ebc742007-07-27 15:43:22 +02003085 struct ieee80211_hdr *hdr =
3086 (struct ieee80211_hdr *) skb->data;
3087 /* more buffered multicast/broadcast frames ==> set
3088 * MoreData flag in IEEE 802.11 header to inform PS
3089 * STAs */
3090 hdr->frame_control |=
3091 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
3092 }
3093
Michael Braun112c44b2014-03-06 15:08:43 +01003094 if (sdata->vif.type == NL80211_IFTYPE_AP)
Marco Porsch7cbf9d02013-03-01 16:01:18 +01003095 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02003096 if (!ieee80211_tx_prepare(sdata, &tx, skb))
Johannes Berge2ebc742007-07-27 15:43:22 +02003097 break;
3098 dev_kfree_skb_any(skb);
3099 }
Johannes Berge039fa42008-05-15 12:55:29 +02003100
3101 info = IEEE80211_SKB_CB(skb);
3102
Johannes Berg5cf121c2008-02-25 16:27:43 +01003103 tx.flags |= IEEE80211_TX_PS_BUFFERED;
Johannes Berg4bf88532012-11-09 11:39:59 +01003104 info->band = chanctx_conf->def.chan->band;
Johannes Berge2ebc742007-07-27 15:43:22 +02003105
Johannes Berg97b045d2008-06-20 01:22:30 +02003106 if (invoke_tx_handlers(&tx))
Johannes Berge2ebc742007-07-27 15:43:22 +02003107 skb = NULL;
Johannes Berg97b045d2008-06-20 01:22:30 +02003108 out:
Johannes Bergd0709a62008-02-25 16:27:46 +01003109 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02003110
3111 return skb;
3112}
3113EXPORT_SYMBOL(ieee80211_get_buffered_bc);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02003114
Liad Kaufmanb6da9112014-11-19 13:47:38 +02003115int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
3116{
3117 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
3118 struct ieee80211_sub_if_data *sdata = sta->sdata;
3119 struct ieee80211_local *local = sdata->local;
3120 int ret;
3121 u32 queues;
3122
3123 lockdep_assert_held(&local->sta_mtx);
3124
3125 /* only some cases are supported right now */
3126 switch (sdata->vif.type) {
3127 case NL80211_IFTYPE_STATION:
3128 case NL80211_IFTYPE_AP:
3129 case NL80211_IFTYPE_AP_VLAN:
3130 break;
3131 default:
3132 WARN_ON(1);
3133 return -EINVAL;
3134 }
3135
3136 if (WARN_ON(tid >= IEEE80211_NUM_UPS))
3137 return -EINVAL;
3138
3139 if (sta->reserved_tid == tid) {
3140 ret = 0;
3141 goto out;
3142 }
3143
3144 if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
3145 sdata_err(sdata, "TID reservation already active\n");
3146 ret = -EALREADY;
3147 goto out;
3148 }
3149
3150 ieee80211_stop_vif_queues(sdata->local, sdata,
3151 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
3152
3153 synchronize_net();
3154
3155 /* Tear down BA sessions so we stop aggregating on this TID */
3156 if (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION) {
3157 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
3158 __ieee80211_stop_tx_ba_session(sta, tid,
3159 AGG_STOP_LOCAL_REQUEST);
3160 }
3161
3162 queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02003163 __ieee80211_flush_queues(local, sdata, queues, false);
Liad Kaufmanb6da9112014-11-19 13:47:38 +02003164
3165 sta->reserved_tid = tid;
3166
3167 ieee80211_wake_vif_queues(local, sdata,
3168 IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
3169
3170 if (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION)
3171 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
3172
3173 ret = 0;
3174 out:
3175 return ret;
3176}
3177EXPORT_SYMBOL(ieee80211_reserve_tid);
3178
3179void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
3180{
3181 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
3182 struct ieee80211_sub_if_data *sdata = sta->sdata;
3183
3184 lockdep_assert_held(&sdata->local->sta_mtx);
3185
3186 /* only some cases are supported right now */
3187 switch (sdata->vif.type) {
3188 case NL80211_IFTYPE_STATION:
3189 case NL80211_IFTYPE_AP:
3190 case NL80211_IFTYPE_AP_VLAN:
3191 break;
3192 default:
3193 WARN_ON(1);
3194 return;
3195 }
3196
3197 if (tid != sta->reserved_tid) {
3198 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
3199 return;
3200 }
3201
3202 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
3203}
3204EXPORT_SYMBOL(ieee80211_unreserve_tid);
3205
Johannes Berg55de9082012-07-26 17:24:39 +02003206void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
3207 struct sk_buff *skb, int tid,
3208 enum ieee80211_band band)
Johannes Berg3b8d81e02009-06-17 17:43:56 +02003209{
Christian Lamparter353d09c2012-07-07 15:07:13 +02003210 int ac = ieee802_1d_to_ac[tid & 7];
Johannes Berg3a25a8c2012-04-03 16:28:50 +02003211
Johannes Berg3b8d81e02009-06-17 17:43:56 +02003212 skb_set_mac_header(skb, 0);
3213 skb_set_network_header(skb, 0);
3214 skb_set_transport_header(skb, 0);
3215
Johannes Berg3a25a8c2012-04-03 16:28:50 +02003216 skb_set_queue_mapping(skb, ac);
Helmut Schaacf6bb792011-12-15 10:18:34 +01003217 skb->priority = tid;
Johannes Bergcf0277e2010-01-05 18:00:58 +01003218
Johannes Berg89afe612013-02-13 15:39:57 +01003219 skb->dev = sdata->dev;
3220
Johannes Berg3d34deb2009-06-18 17:25:11 +02003221 /*
3222 * The other path calling ieee80211_xmit is from the tasklet,
3223 * and while we can handle concurrent transmissions locking
3224 * requirements are that we do not come into tx with bhs on.
3225 */
3226 local_bh_disable();
Johannes Berg73c4e192014-11-09 18:50:09 +02003227 IEEE80211_SKB_CB(skb)->band = band;
3228 ieee80211_xmit(sdata, skb);
Johannes Berg3d34deb2009-06-18 17:25:11 +02003229 local_bh_enable();
Johannes Berg3b8d81e02009-06-17 17:43:56 +02003230}