mac80211: create tx handler for dynamic ps
[linux-2.6-block.git] / net / mac80211 / tx.c
CommitLineData
e2ebc74d
JB
1/*
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>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 *
12 * Transmit and frame generation functions.
13 */
14
15#include <linux/kernel.h>
16#include <linux/slab.h>
17#include <linux/skbuff.h>
18#include <linux/etherdevice.h>
19#include <linux/bitmap.h>
d4e46a3d 20#include <linux/rcupdate.h>
881d966b 21#include <net/net_namespace.h>
e2ebc74d
JB
22#include <net/ieee80211_radiotap.h>
23#include <net/cfg80211.h>
24#include <net/mac80211.h>
25#include <asm/unaligned.h>
26
27#include "ieee80211_i.h"
24487981 28#include "driver-ops.h"
2c8dccc7 29#include "led.h"
33b64eb2 30#include "mesh.h"
e2ebc74d
JB
31#include "wep.h"
32#include "wpa.h"
33#include "wme.h"
2c8dccc7 34#include "rate.h"
e2ebc74d
JB
35
36#define IEEE80211_TX_OK 0
37#define IEEE80211_TX_AGAIN 1
2de8e0d9 38#define IEEE80211_TX_PENDING 2
e2ebc74d
JB
39
40/* misc utils */
41
03f93c3d
JB
42static __le16 ieee80211_duration(struct ieee80211_tx_data *tx, int group_addr,
43 int next_frag_len)
e2ebc74d
JB
44{
45 int rate, mrate, erp, dur, i;
2e92e6f2 46 struct ieee80211_rate *txrate;
e2ebc74d 47 struct ieee80211_local *local = tx->local;
8318d78a 48 struct ieee80211_supported_band *sband;
358c8d9d 49 struct ieee80211_hdr *hdr;
e6a9854b
JB
50 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
51
52 /* assume HW handles this */
53 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
54 return 0;
55
56 /* uh huh? */
57 if (WARN_ON_ONCE(info->control.rates[0].idx < 0))
58 return 0;
e2ebc74d 59
2e92e6f2 60 sband = local->hw.wiphy->bands[tx->channel->band];
e6a9854b 61 txrate = &sband->bitrates[info->control.rates[0].idx];
8318d78a 62
e6a9854b 63 erp = txrate->flags & IEEE80211_RATE_ERP_G;
e2ebc74d
JB
64
65 /*
66 * data and mgmt (except PS Poll):
67 * - during CFP: 32768
68 * - during contention period:
69 * if addr1 is group address: 0
70 * if more fragments = 0 and addr1 is individual address: time to
71 * transmit one ACK plus SIFS
72 * if more fragments = 1 and addr1 is individual address: time to
73 * transmit next fragment plus 2 x ACK plus 3 x SIFS
74 *
75 * IEEE 802.11, 9.6:
76 * - control response frame (CTS or ACK) shall be transmitted using the
77 * same rate as the immediately previous frame in the frame exchange
78 * sequence, if this rate belongs to the PHY mandatory rates, or else
79 * at the highest possible rate belonging to the PHY rates in the
80 * BSSBasicRateSet
81 */
358c8d9d
HH
82 hdr = (struct ieee80211_hdr *)tx->skb->data;
83 if (ieee80211_is_ctl(hdr->frame_control)) {
e2ebc74d 84 /* TODO: These control frames are not currently sent by
ccd7b362 85 * mac80211, but should they be implemented, this function
e2ebc74d
JB
86 * needs to be updated to support duration field calculation.
87 *
88 * RTS: time needed to transmit pending data/mgmt frame plus
89 * one CTS frame plus one ACK frame plus 3 x SIFS
90 * CTS: duration of immediately previous RTS minus time
91 * required to transmit CTS and its SIFS
92 * ACK: 0 if immediately previous directed data/mgmt had
93 * more=0, with more=1 duration in ACK frame is duration
94 * from previous frame minus time needed to transmit ACK
95 * and its SIFS
96 * PS Poll: BIT(15) | BIT(14) | aid
97 */
98 return 0;
99 }
100
101 /* data/mgmt */
102 if (0 /* FIX: data/mgmt during CFP */)
03f93c3d 103 return cpu_to_le16(32768);
e2ebc74d
JB
104
105 if (group_addr) /* Group address as the destination - no ACK */
106 return 0;
107
108 /* Individual destination address:
109 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
110 * CTS and ACK frames shall be transmitted using the highest rate in
111 * basic rate set that is less than or equal to the rate of the
112 * immediately previous frame and that is using the same modulation
113 * (CCK or OFDM). If no basic rate set matches with these requirements,
114 * the highest mandatory rate of the PHY that is less than or equal to
115 * the rate of the previous frame is used.
116 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
117 */
118 rate = -1;
8318d78a
JB
119 /* use lowest available if everything fails */
120 mrate = sband->bitrates[0].bitrate;
121 for (i = 0; i < sband->n_bitrates; i++) {
122 struct ieee80211_rate *r = &sband->bitrates[i];
e2ebc74d 123
8318d78a
JB
124 if (r->bitrate > txrate->bitrate)
125 break;
e2ebc74d 126
bda3933a 127 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
8318d78a
JB
128 rate = r->bitrate;
129
130 switch (sband->band) {
131 case IEEE80211_BAND_2GHZ: {
132 u32 flag;
133 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
134 flag = IEEE80211_RATE_MANDATORY_G;
135 else
136 flag = IEEE80211_RATE_MANDATORY_B;
137 if (r->flags & flag)
138 mrate = r->bitrate;
139 break;
140 }
141 case IEEE80211_BAND_5GHZ:
142 if (r->flags & IEEE80211_RATE_MANDATORY_A)
143 mrate = r->bitrate;
144 break;
145 case IEEE80211_NUM_BANDS:
146 WARN_ON(1);
147 break;
148 }
e2ebc74d
JB
149 }
150 if (rate == -1) {
151 /* No matching basic rate found; use highest suitable mandatory
152 * PHY rate */
153 rate = mrate;
154 }
155
156 /* Time needed to transmit ACK
157 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
158 * to closest integer */
159
160 dur = ieee80211_frame_duration(local, 10, rate, erp,
bda3933a 161 tx->sdata->vif.bss_conf.use_short_preamble);
e2ebc74d
JB
162
163 if (next_frag_len) {
164 /* Frame is fragmented: duration increases with time needed to
165 * transmit next fragment plus ACK and 2 x SIFS. */
166 dur *= 2; /* ACK + SIFS */
167 /* next fragment */
168 dur += ieee80211_frame_duration(local, next_frag_len,
8318d78a 169 txrate->bitrate, erp,
bda3933a 170 tx->sdata->vif.bss_conf.use_short_preamble);
e2ebc74d
JB
171 }
172
03f93c3d 173 return cpu_to_le16(dur);
e2ebc74d
JB
174}
175
133b8226
JB
176static int inline is_ieee80211_device(struct ieee80211_local *local,
177 struct net_device *dev)
e2ebc74d 178{
133b8226 179 return local == wdev_priv(dev->ieee80211_ptr);
e2ebc74d
JB
180}
181
182/* tx handlers */
5c1b98a5
KV
183static ieee80211_tx_result debug_noinline
184ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
185{
186 struct ieee80211_local *local = tx->local;
187
188 /* driver doesn't support power save */
189 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
190 return TX_CONTINUE;
191
192 /* hardware does dynamic power save */
193 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
194 return TX_CONTINUE;
195
196 /* dynamic power save disabled */
197 if (local->hw.conf.dynamic_ps_timeout <= 0)
198 return TX_CONTINUE;
199
200 /* we are scanning, don't enable power save */
201 if (local->scanning)
202 return TX_CONTINUE;
203
204 if (!local->ps_sdata)
205 return TX_CONTINUE;
206
207 /* No point if we're going to suspend */
208 if (local->quiescing)
209 return TX_CONTINUE;
210
211 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
212 ieee80211_stop_queues_by_reason(&local->hw,
213 IEEE80211_QUEUE_STOP_REASON_PS);
214 ieee80211_queue_work(&local->hw,
215 &local->dynamic_ps_disable_work);
216 }
217
218 mod_timer(&local->dynamic_ps_timer, jiffies +
219 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
220
221 return TX_CONTINUE;
222}
e2ebc74d 223
d9e8a70f 224static ieee80211_tx_result debug_noinline
5cf121c3 225ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
e2ebc74d 226{
358c8d9d 227
e039fa4a 228 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
e039fa4a 229 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
e2ebc74d
JB
230 u32 sta_flags;
231
e039fa4a 232 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
9ae54c84 233 return TX_CONTINUE;
58d4185e 234
142b9f50 235 if (unlikely(test_bit(SCAN_OFF_CHANNEL, &tx->local->scanning)) &&
a9a6ffff
KV
236 !ieee80211_is_probe_req(hdr->frame_control) &&
237 !ieee80211_is_nullfunc(hdr->frame_control))
238 /*
239 * When software scanning only nullfunc frames (to notify
240 * the sleep state to the AP) and probe requests (for the
241 * active scan) are allowed, all other frames should not be
242 * sent and we should not get here, but if we do
243 * nonetheless, drop them to avoid sending them
244 * off-channel. See the link below and
245 * ieee80211_start_scan() for more.
246 *
247 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
248 */
9ae54c84 249 return TX_DROP;
e2ebc74d 250
05c914fe 251 if (tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
33b64eb2
LCC
252 return TX_CONTINUE;
253
5cf121c3 254 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
9ae54c84 255 return TX_CONTINUE;
e2ebc74d 256
07346f81 257 sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
e2ebc74d 258
5cf121c3 259 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
e2ebc74d 260 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
05c914fe 261 tx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
358c8d9d 262 ieee80211_is_data(hdr->frame_control))) {
e2ebc74d
JB
263#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
264 printk(KERN_DEBUG "%s: dropped data frame to not "
0c68ae26 265 "associated station %pM\n",
47846c9b 266 tx->sdata->name, hdr->addr1);
e2ebc74d
JB
267#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
268 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
9ae54c84 269 return TX_DROP;
e2ebc74d
JB
270 }
271 } else {
358c8d9d 272 if (unlikely(ieee80211_is_data(hdr->frame_control) &&
e2ebc74d 273 tx->local->num_sta == 0 &&
05c914fe 274 tx->sdata->vif.type != NL80211_IFTYPE_ADHOC)) {
e2ebc74d
JB
275 /*
276 * No associated STAs - no need to send multicast
277 * frames.
278 */
9ae54c84 279 return TX_DROP;
e2ebc74d 280 }
9ae54c84 281 return TX_CONTINUE;
e2ebc74d
JB
282 }
283
9ae54c84 284 return TX_CONTINUE;
e2ebc74d
JB
285}
286
e2ebc74d
JB
287/* This function is called whenever the AP is about to exceed the maximum limit
288 * of buffered frames for power saving STAs. This situation should not really
289 * happen often during normal operation, so dropping the oldest buffered packet
290 * from each queue should be OK to make some room for new frames. */
291static void purge_old_ps_buffers(struct ieee80211_local *local)
292{
293 int total = 0, purged = 0;
294 struct sk_buff *skb;
295 struct ieee80211_sub_if_data *sdata;
296 struct sta_info *sta;
297
79010420
JB
298 /*
299 * virtual interfaces are protected by RCU
300 */
301 rcu_read_lock();
302
303 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
e2ebc74d 304 struct ieee80211_if_ap *ap;
05c914fe 305 if (sdata->vif.type != NL80211_IFTYPE_AP)
e2ebc74d
JB
306 continue;
307 ap = &sdata->u.ap;
308 skb = skb_dequeue(&ap->ps_bc_buf);
309 if (skb) {
310 purged++;
311 dev_kfree_skb(skb);
312 }
313 total += skb_queue_len(&ap->ps_bc_buf);
314 }
e2ebc74d 315
d0709a65 316 list_for_each_entry_rcu(sta, &local->sta_list, list) {
e2ebc74d
JB
317 skb = skb_dequeue(&sta->ps_tx_buf);
318 if (skb) {
319 purged++;
320 dev_kfree_skb(skb);
321 }
322 total += skb_queue_len(&sta->ps_tx_buf);
323 }
d0709a65
JB
324
325 rcu_read_unlock();
e2ebc74d
JB
326
327 local->total_ps_buffered = total;
54223995 328#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
e2ebc74d 329 printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
dd1cd4c6 330 wiphy_name(local->hw.wiphy), purged);
f4ea83dd 331#endif
e2ebc74d
JB
332}
333
9ae54c84 334static ieee80211_tx_result
5cf121c3 335ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
e2ebc74d 336{
e039fa4a 337 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
358c8d9d 338 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
e039fa4a 339
7d54d0dd
JB
340 /*
341 * broadcast/multicast frame
342 *
343 * If any of the associated stations is in power save mode,
344 * the frame is buffered to be sent after DTIM beacon frame.
345 * This is done either by the hardware or us.
346 */
347
3e122be0
JB
348 /* powersaving STAs only in AP/VLAN mode */
349 if (!tx->sdata->bss)
350 return TX_CONTINUE;
351
352 /* no buffering for ordered frames */
358c8d9d 353 if (ieee80211_has_order(hdr->frame_control))
9ae54c84 354 return TX_CONTINUE;
7d54d0dd
JB
355
356 /* no stations in PS mode */
357 if (!atomic_read(&tx->sdata->bss->num_sta_ps))
9ae54c84 358 return TX_CONTINUE;
7d54d0dd 359
62b517cb 360 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
62b1208e 361
62b517cb
JB
362 /* device releases frame after DTIM beacon */
363 if (!(tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING))
62b1208e 364 return TX_CONTINUE;
62b1208e 365
7d54d0dd 366 /* buffered in mac80211 */
62b1208e
JB
367 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
368 purge_old_ps_buffers(tx->local);
369
370 if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >= AP_MAX_BC_BUFFER) {
54223995 371#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
62b1208e
JB
372 if (net_ratelimit())
373 printk(KERN_DEBUG "%s: BC TX buffer full - dropping the oldest frame\n",
47846c9b 374 tx->sdata->name);
f4ea83dd 375#endif
62b1208e
JB
376 dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
377 } else
378 tx->local->total_ps_buffered++;
e2ebc74d 379
62b1208e 380 skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
7d54d0dd 381
62b1208e 382 return TX_QUEUED;
e2ebc74d
JB
383}
384
fb733336
JM
385static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
386 struct sk_buff *skb)
387{
388 if (!ieee80211_is_mgmt(fc))
389 return 0;
390
391 if (sta == NULL || !test_sta_flags(sta, WLAN_STA_MFP))
392 return 0;
393
394 if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *)
395 skb->data))
396 return 0;
397
398 return 1;
399}
400
9ae54c84 401static ieee80211_tx_result
5cf121c3 402ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
e2ebc74d
JB
403{
404 struct sta_info *sta = tx->sta;
e039fa4a 405 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
358c8d9d 406 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
07346f81 407 u32 staflags;
e2ebc74d 408
f64f9e71
JP
409 if (unlikely(!sta ||
410 ieee80211_is_probe_resp(hdr->frame_control) ||
411 ieee80211_is_auth(hdr->frame_control) ||
412 ieee80211_is_assoc_resp(hdr->frame_control) ||
413 ieee80211_is_reassoc_resp(hdr->frame_control)))
9ae54c84 414 return TX_CONTINUE;
e2ebc74d 415
07346f81
JB
416 staflags = get_sta_flags(sta);
417
af818581 418 if (unlikely((staflags & (WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) &&
3fa52056 419 !(info->flags & IEEE80211_TX_CTL_PSPOLL_RESPONSE))) {
e2ebc74d 420#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0c68ae26 421 printk(KERN_DEBUG "STA %pM aid %d: PS buffer (entries "
e2ebc74d 422 "before %d)\n",
0c68ae26 423 sta->sta.addr, sta->sta.aid,
e2ebc74d
JB
424 skb_queue_len(&sta->ps_tx_buf));
425#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
e2ebc74d
JB
426 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
427 purge_old_ps_buffers(tx->local);
428 if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
429 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
54223995 430#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
e2ebc74d 431 if (net_ratelimit()) {
0c68ae26 432 printk(KERN_DEBUG "%s: STA %pM TX "
e2ebc74d 433 "buffer full - dropping oldest frame\n",
47846c9b 434 tx->sdata->name, sta->sta.addr);
e2ebc74d 435 }
f4ea83dd 436#endif
e2ebc74d
JB
437 dev_kfree_skb(old);
438 } else
439 tx->local->total_ps_buffered++;
004c872e 440
af818581
JB
441 /*
442 * Queue frame to be sent after STA wakes up/polls,
443 * but don't set the TIM bit if the driver is blocking
444 * wakeup or poll response transmissions anyway.
445 */
446 if (skb_queue_empty(&sta->ps_tx_buf) &&
447 !(staflags & WLAN_STA_PS_DRIVER))
004c872e
JB
448 sta_info_set_tim_bit(sta);
449
e039fa4a 450 info->control.jiffies = jiffies;
5061b0c2 451 info->control.vif = &tx->sdata->vif;
8f77f384 452 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
e2ebc74d 453 skb_queue_tail(&sta->ps_tx_buf, tx->skb);
9ae54c84 454 return TX_QUEUED;
e2ebc74d
JB
455 }
456#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
af818581 457 else if (unlikely(staflags & WLAN_STA_PS_STA)) {
0c68ae26 458 printk(KERN_DEBUG "%s: STA %pM in PS mode, but pspoll "
47846c9b 459 "set -> send frame\n", tx->sdata->name,
0c68ae26 460 sta->sta.addr);
e2ebc74d
JB
461 }
462#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
e2ebc74d 463
9ae54c84 464 return TX_CONTINUE;
e2ebc74d
JB
465}
466
d9e8a70f 467static ieee80211_tx_result debug_noinline
5cf121c3 468ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
e2ebc74d 469{
5cf121c3 470 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
9ae54c84 471 return TX_CONTINUE;
e2ebc74d 472
5cf121c3 473 if (tx->flags & IEEE80211_TX_UNICAST)
e2ebc74d
JB
474 return ieee80211_tx_h_unicast_ps_buf(tx);
475 else
476 return ieee80211_tx_h_multicast_ps_buf(tx);
477}
478
d9e8a70f 479static ieee80211_tx_result debug_noinline
5cf121c3 480ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
e2ebc74d 481{
d3feaf5a 482 struct ieee80211_key *key = NULL;
e039fa4a 483 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
358c8d9d 484 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
d4e46a3d 485
3b8d81e0 486 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
e2ebc74d 487 tx->key = NULL;
d4e46a3d
JB
488 else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
489 tx->key = key;
3cfcf6ac
JM
490 else if (ieee80211_is_mgmt(hdr->frame_control) &&
491 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
492 tx->key = key;
d4e46a3d
JB
493 else if ((key = rcu_dereference(tx->sdata->default_key)))
494 tx->key = key;
e2ebc74d 495 else if (tx->sdata->drop_unencrypted &&
d0f09804 496 (tx->skb->protocol != cpu_to_be16(ETH_P_PAE)) &&
e0463f50
JM
497 !(info->flags & IEEE80211_TX_CTL_INJECTED) &&
498 (!ieee80211_is_robust_mgmt_frame(hdr) ||
499 (ieee80211_is_action(hdr->frame_control) &&
500 tx->sta && test_sta_flags(tx->sta, WLAN_STA_MFP)))) {
e2ebc74d 501 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
9ae54c84 502 return TX_DROP;
176e4f84 503 } else
e2ebc74d
JB
504 tx->key = NULL;
505
506 if (tx->key) {
507 tx->key->tx_rx_count++;
011bfcc4 508 /* TODO: add threshold stuff again */
176e4f84
JB
509
510 switch (tx->key->conf.alg) {
511 case ALG_WEP:
358c8d9d 512 if (ieee80211_is_auth(hdr->frame_control))
176e4f84
JB
513 break;
514 case ALG_TKIP:
358c8d9d 515 if (!ieee80211_is_data_present(hdr->frame_control))
176e4f84
JB
516 tx->key = NULL;
517 break;
fb733336
JM
518 case ALG_CCMP:
519 if (!ieee80211_is_data_present(hdr->frame_control) &&
520 !ieee80211_use_mfp(hdr->frame_control, tx->sta,
521 tx->skb))
522 tx->key = NULL;
523 break;
3cfcf6ac
JM
524 case ALG_AES_CMAC:
525 if (!ieee80211_is_mgmt(hdr->frame_control))
526 tx->key = NULL;
527 break;
176e4f84 528 }
e2ebc74d
JB
529 }
530
176e4f84 531 if (!tx->key || !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
3b8d81e0 532 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
176e4f84 533
9ae54c84 534 return TX_CONTINUE;
e2ebc74d
JB
535}
536
d9e8a70f 537static ieee80211_tx_result debug_noinline
5cf121c3 538ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
e2ebc74d 539{
e039fa4a 540 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
e6a9854b
JB
541 struct ieee80211_hdr *hdr = (void *)tx->skb->data;
542 struct ieee80211_supported_band *sband;
543 struct ieee80211_rate *rate;
544 int i, len;
545 bool inval = false, rts = false, short_preamble = false;
546 struct ieee80211_tx_rate_control txrc;
b770b43e 547 u32 sta_flags;
8318d78a 548
e6a9854b 549 memset(&txrc, 0, sizeof(txrc));
e2ebc74d 550
e6a9854b 551 sband = tx->local->hw.wiphy->bands[tx->channel->band];
58d4185e 552
e6a9854b 553 len = min_t(int, tx->skb->len + FCS_LEN,
b9a5f8ca 554 tx->local->hw.wiphy->frag_threshold);
e6a9854b
JB
555
556 /* set up the tx rate control struct we give the RC algo */
557 txrc.hw = local_to_hw(tx->local);
558 txrc.sband = sband;
559 txrc.bss_conf = &tx->sdata->vif.bss_conf;
560 txrc.skb = tx->skb;
561 txrc.reported_rate.idx = -1;
37eb0b16
JM
562 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[tx->channel->band];
563 if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
564 txrc.max_rate_idx = -1;
565 else
566 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
e00cfce0 567 txrc.ap = tx->sdata->vif.type == NL80211_IFTYPE_AP;
e6a9854b
JB
568
569 /* set up RTS protection if desired */
b9a5f8ca 570 if (len > tx->local->hw.wiphy->rts_threshold) {
e6a9854b 571 txrc.rts = rts = true;
e2ebc74d 572 }
e2ebc74d 573
e6a9854b
JB
574 /*
575 * Use short preamble if the BSS can handle it, but not for
576 * management frames unless we know the receiver can handle
577 * that -- the management frame might be to a station that
578 * just wants a probe response.
579 */
580 if (tx->sdata->vif.bss_conf.use_short_preamble &&
581 (ieee80211_is_data(hdr->frame_control) ||
582 (tx->sta && test_sta_flags(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
583 txrc.short_preamble = short_preamble = true;
e2ebc74d 584
b770b43e
LR
585 sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
586
587 /*
588 * Lets not bother rate control if we're associated and cannot
589 * talk to the sta. This should not happen.
590 */
fbe9c429 591 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) &&
b770b43e
LR
592 (sta_flags & WLAN_STA_ASSOC) &&
593 !rate_usable_index_exists(sband, &tx->sta->sta),
594 "%s: Dropped data frame as no usable bitrate found while "
595 "scanning and associated. Target station: "
596 "%pM on %d GHz band\n",
47846c9b 597 tx->sdata->name, hdr->addr1,
b770b43e
LR
598 tx->channel->band ? 5 : 2))
599 return TX_DROP;
2e92e6f2 600
b770b43e
LR
601 /*
602 * If we're associated with the sta at this point we know we can at
603 * least send the frame at the lowest bit rate.
604 */
e6a9854b
JB
605 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
606
607 if (unlikely(info->control.rates[0].idx < 0))
608 return TX_DROP;
609
610 if (txrc.reported_rate.idx < 0)
611 txrc.reported_rate = info->control.rates[0];
e2ebc74d 612
e039fa4a 613 if (tx->sta)
e6a9854b 614 tx->sta->last_tx_rate = txrc.reported_rate;
e039fa4a 615
e6a9854b
JB
616 if (unlikely(!info->control.rates[0].count))
617 info->control.rates[0].count = 1;
e2ebc74d 618
9955151d
GS
619 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
620 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
621 info->control.rates[0].count = 1;
622
e6a9854b
JB
623 if (is_multicast_ether_addr(hdr->addr1)) {
624 /*
625 * XXX: verify the rate is in the basic rateset
626 */
627 return TX_CONTINUE;
e2ebc74d
JB
628 }
629
e6a9854b
JB
630 /*
631 * set up the RTS/CTS rate as the fastest basic rate
632 * that is not faster than the data rate
633 *
634 * XXX: Should this check all retry rates?
635 */
636 if (!(info->control.rates[0].flags & IEEE80211_TX_RC_MCS)) {
637 s8 baserate = 0;
638
639 rate = &sband->bitrates[info->control.rates[0].idx];
640
641 for (i = 0; i < sband->n_bitrates; i++) {
642 /* must be a basic rate */
643 if (!(tx->sdata->vif.bss_conf.basic_rates & BIT(i)))
644 continue;
645 /* must not be faster than the data rate */
646 if (sband->bitrates[i].bitrate > rate->bitrate)
647 continue;
648 /* maximum */
649 if (sband->bitrates[baserate].bitrate <
650 sband->bitrates[i].bitrate)
651 baserate = i;
652 }
653
654 info->control.rts_cts_rate_idx = baserate;
7e9ed188
DD
655 }
656
e6a9854b
JB
657 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
658 /*
659 * make sure there's no valid rate following
660 * an invalid one, just in case drivers don't
661 * take the API seriously to stop at -1.
662 */
663 if (inval) {
664 info->control.rates[i].idx = -1;
665 continue;
666 }
667 if (info->control.rates[i].idx < 0) {
668 inval = true;
669 continue;
670 }
8318d78a 671
e6a9854b
JB
672 /*
673 * For now assume MCS is already set up correctly, this
674 * needs to be fixed.
675 */
676 if (info->control.rates[i].flags & IEEE80211_TX_RC_MCS) {
677 WARN_ON(info->control.rates[i].idx > 76);
678 continue;
679 }
e2ebc74d 680
e6a9854b
JB
681 /* set up RTS protection if desired */
682 if (rts)
683 info->control.rates[i].flags |=
684 IEEE80211_TX_RC_USE_RTS_CTS;
8318d78a 685
e6a9854b 686 /* RC is busted */
075cbc9e
S
687 if (WARN_ON_ONCE(info->control.rates[i].idx >=
688 sband->n_bitrates)) {
e6a9854b
JB
689 info->control.rates[i].idx = -1;
690 continue;
8318d78a 691 }
e2ebc74d 692
e6a9854b
JB
693 rate = &sband->bitrates[info->control.rates[i].idx];
694
695 /* set up short preamble */
696 if (short_preamble &&
697 rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
698 info->control.rates[i].flags |=
699 IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
700
701 /* set up G protection */
702 if (!rts && tx->sdata->vif.bss_conf.use_cts_prot &&
703 rate->flags & IEEE80211_RATE_ERP_G)
704 info->control.rates[i].flags |=
705 IEEE80211_TX_RC_USE_CTS_PROTECT;
e2ebc74d
JB
706 }
707
e6a9854b
JB
708 return TX_CONTINUE;
709}
710
711static ieee80211_tx_result debug_noinline
712ieee80211_tx_h_misc(struct ieee80211_tx_data *tx)
713{
714 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
715
e2454948 716 if (tx->sta)
17741cdc 717 info->control.sta = &tx->sta->sta;
e2454948
JB
718
719 return TX_CONTINUE;
720}
721
f591fa5d
JB
722static ieee80211_tx_result debug_noinline
723ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
724{
725 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
726 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
727 u16 *seq;
728 u8 *qc;
729 int tid;
730
25d834e1
JB
731 /*
732 * Packet injection may want to control the sequence
733 * number, if we have no matching interface then we
734 * neither assign one ourselves nor ask the driver to.
735 */
5061b0c2 736 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
25d834e1
JB
737 return TX_CONTINUE;
738
f591fa5d
JB
739 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
740 return TX_CONTINUE;
741
742 if (ieee80211_hdrlen(hdr->frame_control) < 24)
743 return TX_CONTINUE;
744
94778280
JB
745 /*
746 * Anything but QoS data that has a sequence number field
747 * (is long enough) gets a sequence number from the global
748 * counter.
749 */
f591fa5d 750 if (!ieee80211_is_data_qos(hdr->frame_control)) {
94778280 751 /* driver should assign sequence number */
f591fa5d 752 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
94778280
JB
753 /* for pure STA mode without beacons, we can do it */
754 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
755 tx->sdata->sequence_number += 0x10;
f591fa5d
JB
756 return TX_CONTINUE;
757 }
758
759 /*
760 * This should be true for injected/management frames only, for
761 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
762 * above since they are not QoS-data frames.
763 */
764 if (!tx->sta)
765 return TX_CONTINUE;
766
767 /* include per-STA, per-TID sequence counter */
768
769 qc = ieee80211_get_qos_ctl(hdr);
770 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
771 seq = &tx->sta->tid_seq[tid];
772
773 hdr->seq_ctrl = cpu_to_le16(*seq);
774
775 /* Increase the sequence number. */
776 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
777
778 return TX_CONTINUE;
779}
780
2de8e0d9
JB
781static int ieee80211_fragment(struct ieee80211_local *local,
782 struct sk_buff *skb, int hdrlen,
783 int frag_threshold)
784{
785 struct sk_buff *tail = skb, *tmp;
786 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
787 int pos = hdrlen + per_fragm;
788 int rem = skb->len - hdrlen - per_fragm;
789
790 if (WARN_ON(rem < 0))
791 return -EINVAL;
792
793 while (rem) {
794 int fraglen = per_fragm;
795
796 if (fraglen > rem)
797 fraglen = rem;
798 rem -= fraglen;
799 tmp = dev_alloc_skb(local->tx_headroom +
800 frag_threshold +
801 IEEE80211_ENCRYPT_HEADROOM +
802 IEEE80211_ENCRYPT_TAILROOM);
803 if (!tmp)
804 return -ENOMEM;
805 tail->next = tmp;
806 tail = tmp;
807 skb_reserve(tmp, local->tx_headroom +
808 IEEE80211_ENCRYPT_HEADROOM);
809 /* copy control information */
810 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
811 skb_copy_queue_mapping(tmp, skb);
812 tmp->priority = skb->priority;
2de8e0d9 813 tmp->dev = skb->dev;
2de8e0d9
JB
814
815 /* copy header and data */
816 memcpy(skb_put(tmp, hdrlen), skb->data, hdrlen);
817 memcpy(skb_put(tmp, fraglen), skb->data + pos, fraglen);
818
819 pos += fraglen;
820 }
821
822 skb->len = hdrlen + per_fragm;
823 return 0;
824}
825
d9e8a70f 826static ieee80211_tx_result debug_noinline
e2454948
JB
827ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
828{
2de8e0d9
JB
829 struct sk_buff *skb = tx->skb;
830 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
831 struct ieee80211_hdr *hdr = (void *)skb->data;
b9a5f8ca 832 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
2de8e0d9
JB
833 int hdrlen;
834 int fragnum;
e2454948
JB
835
836 if (!(tx->flags & IEEE80211_TX_FRAGMENTED))
837 return TX_CONTINUE;
838
eefce91a
JB
839 /*
840 * Warn when submitting a fragmented A-MPDU frame and drop it.
3b8d81e0 841 * This scenario is handled in ieee80211_tx_prepare but extra
8d5e0d58 842 * caution taken here as fragmented ampdu may cause Tx stop.
eefce91a 843 */
8b30b1fe 844 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
eefce91a
JB
845 return TX_DROP;
846
065e9605 847 hdrlen = ieee80211_hdrlen(hdr->frame_control);
e2454948 848
2de8e0d9 849 /* internal error, why is TX_FRAGMENTED set? */
8ccd8f21 850 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
2de8e0d9 851 return TX_DROP;
e6a9854b 852
2de8e0d9
JB
853 /*
854 * Now fragment the frame. This will allocate all the fragments and
855 * chain them (using skb as the first fragment) to skb->next.
856 * During transmission, we will remove the successfully transmitted
857 * fragments from this list. When the low-level driver rejects one
858 * of the fragments then we will simply pretend to accept the skb
859 * but store it away as pending.
860 */
861 if (ieee80211_fragment(tx->local, skb, hdrlen, frag_threshold))
862 return TX_DROP;
e6a9854b 863
2de8e0d9
JB
864 /* update duration/seq/flags of fragments */
865 fragnum = 0;
866 do {
867 int next_len;
868 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
e6a9854b 869
2de8e0d9
JB
870 hdr = (void *)skb->data;
871 info = IEEE80211_SKB_CB(skb);
e6a9854b 872
2de8e0d9
JB
873 if (skb->next) {
874 hdr->frame_control |= morefrags;
875 next_len = skb->next->len;
e6a9854b
JB
876 /*
877 * No multi-rate retries for fragmented frames, that
878 * would completely throw off the NAV at other STAs.
879 */
880 info->control.rates[1].idx = -1;
881 info->control.rates[2].idx = -1;
882 info->control.rates[3].idx = -1;
883 info->control.rates[4].idx = -1;
884 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 5);
885 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
2de8e0d9
JB
886 } else {
887 hdr->frame_control &= ~morefrags;
888 next_len = 0;
e6a9854b 889 }
2de8e0d9
JB
890 hdr->duration_id = ieee80211_duration(tx, 0, next_len);
891 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
892 fragnum++;
893 } while ((skb = skb->next));
e2ebc74d 894
9ae54c84 895 return TX_CONTINUE;
e2ebc74d
JB
896}
897
feff1f2f
JB
898static ieee80211_tx_result debug_noinline
899ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
900{
901 struct sk_buff *skb = tx->skb;
902
903 if (!tx->sta)
904 return TX_CONTINUE;
905
906 tx->sta->tx_packets++;
907 do {
908 tx->sta->tx_fragments++;
909 tx->sta->tx_bytes += skb->len;
910 } while ((skb = skb->next));
911
912 return TX_CONTINUE;
913}
914
d9e8a70f 915static ieee80211_tx_result debug_noinline
e2454948
JB
916ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
917{
918 if (!tx->key)
919 return TX_CONTINUE;
920
921 switch (tx->key->conf.alg) {
922 case ALG_WEP:
923 return ieee80211_crypto_wep_encrypt(tx);
924 case ALG_TKIP:
925 return ieee80211_crypto_tkip_encrypt(tx);
926 case ALG_CCMP:
927 return ieee80211_crypto_ccmp_encrypt(tx);
3cfcf6ac
JM
928 case ALG_AES_CMAC:
929 return ieee80211_crypto_aes_cmac_encrypt(tx);
e2454948
JB
930 }
931
932 /* not reached */
933 WARN_ON(1);
934 return TX_DROP;
935}
936
d9e8a70f 937static ieee80211_tx_result debug_noinline
03f93c3d
JB
938ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
939{
2de8e0d9
JB
940 struct sk_buff *skb = tx->skb;
941 struct ieee80211_hdr *hdr;
942 int next_len;
943 bool group_addr;
03f93c3d 944
2de8e0d9
JB
945 do {
946 hdr = (void *) skb->data;
7e0aae47
JM
947 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
948 break; /* must not overwrite AID */
2de8e0d9
JB
949 next_len = skb->next ? skb->next->len : 0;
950 group_addr = is_multicast_ether_addr(hdr->addr1);
03f93c3d 951
2de8e0d9
JB
952 hdr->duration_id =
953 ieee80211_duration(tx, group_addr, next_len);
954 } while ((skb = skb->next));
03f93c3d
JB
955
956 return TX_CONTINUE;
957}
958
e2ebc74d
JB
959/* actual transmit path */
960
961/*
962 * deal with packet injection down monitor interface
963 * with Radiotap Header -- only called for monitor mode interface
964 */
27004b10
JB
965static bool __ieee80211_parse_tx_radiotap(struct ieee80211_tx_data *tx,
966 struct sk_buff *skb)
e2ebc74d
JB
967{
968 /*
969 * this is the moment to interpret and discard the radiotap header that
970 * must be at the start of the packet injected in Monitor mode
971 *
972 * Need to take some care with endian-ness since radiotap
973 * args are little-endian
974 */
975
976 struct ieee80211_radiotap_iterator iterator;
977 struct ieee80211_radiotap_header *rthdr =
978 (struct ieee80211_radiotap_header *) skb->data;
8318d78a 979 struct ieee80211_supported_band *sband;
3b8d81e0 980 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
e2ebc74d
JB
981 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
982
2e92e6f2 983 sband = tx->local->hw.wiphy->bands[tx->channel->band];
8318d78a 984
3b8d81e0 985 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
5cf121c3 986 tx->flags &= ~IEEE80211_TX_FRAGMENTED;
e2ebc74d
JB
987
988 /*
989 * for every radiotap entry that is present
990 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
991 * entries present, or -EINVAL on error)
992 */
993
994 while (!ret) {
e2ebc74d
JB
995 ret = ieee80211_radiotap_iterator_next(&iterator);
996
997 if (ret)
998 continue;
999
1000 /* see if this argument is something we can use */
1001 switch (iterator.this_arg_index) {
1002 /*
1003 * You must take care when dereferencing iterator.this_arg
1004 * for multibyte types... the pointer is not aligned. Use
1005 * get_unaligned((type *)iterator.this_arg) to dereference
1006 * iterator.this_arg for type "type" safely on all arches.
1007 */
e2ebc74d
JB
1008 case IEEE80211_RADIOTAP_FLAGS:
1009 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
1010 /*
1011 * this indicates that the skb we have been
1012 * handed has the 32-bit FCS CRC at the end...
1013 * we should react to that by snipping it off
1014 * because it will be recomputed and added
1015 * on transmission
1016 */
1017 if (skb->len < (iterator.max_length + FCS_LEN))
27004b10 1018 return false;
e2ebc74d
JB
1019
1020 skb_trim(skb, skb->len - FCS_LEN);
1021 }
58d4185e 1022 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
3b8d81e0 1023 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
58d4185e 1024 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
5cf121c3 1025 tx->flags |= IEEE80211_TX_FRAGMENTED;
e2ebc74d
JB
1026 break;
1027
58d4185e
JB
1028 /*
1029 * Please update the file
1030 * Documentation/networking/mac80211-injection.txt
1031 * when parsing new fields here.
1032 */
1033
e2ebc74d
JB
1034 default:
1035 break;
1036 }
1037 }
1038
1039 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
27004b10 1040 return false;
e2ebc74d
JB
1041
1042 /*
1043 * remove the radiotap header
1044 * iterator->max_length was sanity-checked against
1045 * skb->len by iterator init
1046 */
1047 skb_pull(skb, iterator.max_length);
1048
27004b10 1049 return true;
e2ebc74d
JB
1050}
1051
58d4185e
JB
1052/*
1053 * initialises @tx
1054 */
9ae54c84 1055static ieee80211_tx_result
3b8d81e0
JB
1056ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1057 struct ieee80211_tx_data *tx,
1058 struct sk_buff *skb)
e2ebc74d 1059{
3b8d81e0 1060 struct ieee80211_local *local = sdata->local;
58d4185e 1061 struct ieee80211_hdr *hdr;
e039fa4a 1062 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
8b30b1fe
S
1063 int hdrlen, tid;
1064 u8 *qc, *state;
cd8ffc80 1065 bool queued = false;
e2ebc74d
JB
1066
1067 memset(tx, 0, sizeof(*tx));
1068 tx->skb = skb;
e2ebc74d 1069 tx->local = local;
3b8d81e0 1070 tx->sdata = sdata;
e039fa4a 1071 tx->channel = local->hw.conf.channel;
e2ebc74d 1072 /*
58d4185e
JB
1073 * Set this flag (used below to indicate "automatic fragmentation"),
1074 * it will be cleared/left by radiotap as desired.
e2ebc74d 1075 */
5cf121c3 1076 tx->flags |= IEEE80211_TX_FRAGMENTED;
e2ebc74d
JB
1077
1078 /* process and remove the injection radiotap header */
25d834e1 1079 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED)) {
27004b10 1080 if (!__ieee80211_parse_tx_radiotap(tx, skb))
9ae54c84 1081 return TX_DROP;
58d4185e 1082
e2ebc74d 1083 /*
58d4185e
JB
1084 * __ieee80211_parse_tx_radiotap has now removed
1085 * the radiotap header that was present and pre-filled
1086 * 'tx' with tx control information.
e2ebc74d 1087 */
e2ebc74d
JB
1088 }
1089
cd8ffc80
JB
1090 /*
1091 * If this flag is set to true anywhere, and we get here,
1092 * we are doing the needed processing, so remove the flag
1093 * now.
1094 */
1095 info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1096
58d4185e
JB
1097 hdr = (struct ieee80211_hdr *) skb->data;
1098
3f0e0b22 1099 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
f14543ee 1100 tx->sta = rcu_dereference(sdata->u.vlan.sta);
3f0e0b22
FF
1101 if (!tx->sta && sdata->dev->ieee80211_ptr->use_4addr)
1102 return TX_DROP;
1103 }
f14543ee 1104 if (!tx->sta)
abe60632 1105 tx->sta = sta_info_get(sdata, hdr->addr1);
58d4185e 1106
cd8ffc80
JB
1107 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
1108 (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION)) {
96f5e66e 1109 unsigned long flags;
cd8ffc80
JB
1110 struct tid_ampdu_tx *tid_tx;
1111
8b30b1fe
S
1112 qc = ieee80211_get_qos_ctl(hdr);
1113 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
1114
96f5e66e 1115 spin_lock_irqsave(&tx->sta->lock, flags);
cd8ffc80
JB
1116 /*
1117 * XXX: This spinlock could be fairly expensive, but see the
1118 * comment in agg-tx.c:ieee80211_agg_tx_operational().
1119 * One way to solve this would be to do something RCU-like
1120 * for managing the tid_tx struct and using atomic bitops
1121 * for the actual state -- by introducing an actual
1122 * 'operational' bit that would be possible. It would
1123 * require changing ieee80211_agg_tx_operational() to
1124 * set that bit, and changing the way tid_tx is managed
1125 * everywhere, including races between that bit and
1126 * tid_tx going away (tid_tx being added can be easily
1127 * committed to memory before the 'operational' bit).
1128 */
1129 tid_tx = tx->sta->ampdu_mlme.tid_tx[tid];
8b30b1fe 1130 state = &tx->sta->ampdu_mlme.tid_state_tx[tid];
cd8ffc80 1131 if (*state == HT_AGG_STATE_OPERATIONAL) {
8b30b1fe 1132 info->flags |= IEEE80211_TX_CTL_AMPDU;
cd8ffc80
JB
1133 } else if (*state != HT_AGG_STATE_IDLE) {
1134 /* in progress */
1135 queued = true;
5061b0c2 1136 info->control.vif = &sdata->vif;
cd8ffc80
JB
1137 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1138 __skb_queue_tail(&tid_tx->pending, skb);
1139 }
96f5e66e 1140 spin_unlock_irqrestore(&tx->sta->lock, flags);
cd8ffc80
JB
1141
1142 if (unlikely(queued))
1143 return TX_QUEUED;
8b30b1fe
S
1144 }
1145
badffb72 1146 if (is_multicast_ether_addr(hdr->addr1)) {
5cf121c3 1147 tx->flags &= ~IEEE80211_TX_UNICAST;
e039fa4a 1148 info->flags |= IEEE80211_TX_CTL_NO_ACK;
badffb72 1149 } else {
5cf121c3 1150 tx->flags |= IEEE80211_TX_UNICAST;
d3707d99
JB
1151 if (unlikely(local->wifi_wme_noack_test))
1152 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1153 else
1154 info->flags &= ~IEEE80211_TX_CTL_NO_ACK;
badffb72 1155 }
58d4185e 1156
5cf121c3
JB
1157 if (tx->flags & IEEE80211_TX_FRAGMENTED) {
1158 if ((tx->flags & IEEE80211_TX_UNICAST) &&
b9a5f8ca 1159 skb->len + FCS_LEN > local->hw.wiphy->frag_threshold &&
8d5e0d58 1160 !(info->flags & IEEE80211_TX_CTL_AMPDU))
5cf121c3 1161 tx->flags |= IEEE80211_TX_FRAGMENTED;
58d4185e 1162 else
5cf121c3 1163 tx->flags &= ~IEEE80211_TX_FRAGMENTED;
58d4185e
JB
1164 }
1165
e2ebc74d 1166 if (!tx->sta)
e039fa4a 1167 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
07346f81 1168 else if (test_and_clear_sta_flags(tx->sta, WLAN_STA_CLEAR_PS_FILT))
e039fa4a 1169 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
58d4185e 1170
b73d70ad 1171 hdrlen = ieee80211_hdrlen(hdr->frame_control);
e2ebc74d
JB
1172 if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
1173 u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
1174 tx->ethertype = (pos[0] << 8) | pos[1];
1175 }
e039fa4a 1176 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
e2ebc74d 1177
9ae54c84 1178 return TX_CONTINUE;
e2ebc74d
JB
1179}
1180
2de8e0d9 1181static int __ieee80211_tx(struct ieee80211_local *local,
1870cd71 1182 struct sk_buff **skbp,
3b8d81e0
JB
1183 struct sta_info *sta,
1184 bool txpending)
e2ebc74d 1185{
1870cd71 1186 struct sk_buff *skb = *skbp, *next;
f8e79ddd 1187 struct ieee80211_tx_info *info;
5061b0c2 1188 struct ieee80211_sub_if_data *sdata;
3b8d81e0 1189 unsigned long flags;
a220858d 1190 int ret, len;
2de8e0d9 1191 bool fragm = false;
e2ebc74d 1192
2de8e0d9 1193 while (skb) {
3b8d81e0
JB
1194 int q = skb_get_queue_mapping(skb);
1195
1196 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1197 ret = IEEE80211_TX_OK;
1198 if (local->queue_stop_reasons[q] ||
1199 (!txpending && !skb_queue_empty(&local->pending[q])))
1200 ret = IEEE80211_TX_PENDING;
1201 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1202 if (ret != IEEE80211_TX_OK)
1203 return ret;
f8e79ddd 1204
f0e72851
JB
1205 info = IEEE80211_SKB_CB(skb);
1206
1207 if (fragm)
e6a9854b 1208 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
e039fa4a 1209 IEEE80211_TX_CTL_FIRST_FRAGMENT);
f0e72851 1210
2de8e0d9 1211 next = skb->next;
a220858d 1212 len = skb->len;
5061b0c2 1213
ad5351db
JB
1214 if (next)
1215 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
1216
5061b0c2
JB
1217 sdata = vif_to_sdata(info->control.vif);
1218
1219 switch (sdata->vif.type) {
1220 case NL80211_IFTYPE_MONITOR:
1221 info->control.vif = NULL;
1222 break;
1223 case NL80211_IFTYPE_AP_VLAN:
1224 info->control.vif = &container_of(sdata->bss,
1225 struct ieee80211_sub_if_data, u.ap)->vif;
1226 break;
1227 default:
1228 /* keep */
1229 break;
1230 }
1231
24487981 1232 ret = drv_tx(local, skb);
a220858d
JB
1233 if (WARN_ON(ret != NETDEV_TX_OK && skb->len != len)) {
1234 dev_kfree_skb(skb);
1235 ret = NETDEV_TX_OK;
1236 }
21f5fc75
LR
1237 if (ret != NETDEV_TX_OK) {
1238 info->control.vif = &sdata->vif;
2de8e0d9 1239 return IEEE80211_TX_AGAIN;
21f5fc75
LR
1240 }
1241
1870cd71 1242 *skbp = skb = next;
2de8e0d9
JB
1243 ieee80211_led_tx(local, 1);
1244 fragm = true;
e2ebc74d 1245 }
2de8e0d9 1246
e2ebc74d
JB
1247 return IEEE80211_TX_OK;
1248}
1249
97b045d6
JB
1250/*
1251 * Invoke TX handlers, return 0 on success and non-zero if the
1252 * frame was dropped or queued.
1253 */
1254static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1255{
97b045d6 1256 struct sk_buff *skb = tx->skb;
97b045d6 1257 ieee80211_tx_result res = TX_DROP;
97b045d6 1258
9aa4aee3
JB
1259#define CALL_TXH(txh) \
1260 do { \
1261 res = txh(tx); \
1262 if (res != TX_CONTINUE) \
1263 goto txh_done; \
1264 } while (0)
1265
5c1b98a5 1266 CALL_TXH(ieee80211_tx_h_dynamic_ps);
9aa4aee3
JB
1267 CALL_TXH(ieee80211_tx_h_check_assoc);
1268 CALL_TXH(ieee80211_tx_h_ps_buf);
1269 CALL_TXH(ieee80211_tx_h_select_key);
1270 CALL_TXH(ieee80211_tx_h_michael_mic_add);
af65cd96
JB
1271 if (!(tx->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL))
1272 CALL_TXH(ieee80211_tx_h_rate_ctrl);
9aa4aee3
JB
1273 CALL_TXH(ieee80211_tx_h_misc);
1274 CALL_TXH(ieee80211_tx_h_sequence);
1275 CALL_TXH(ieee80211_tx_h_fragment);
d9e8a70f 1276 /* handlers after fragment must be aware of tx info fragmentation! */
9aa4aee3
JB
1277 CALL_TXH(ieee80211_tx_h_stats);
1278 CALL_TXH(ieee80211_tx_h_encrypt);
1279 CALL_TXH(ieee80211_tx_h_calculate_duration);
d9e8a70f 1280#undef CALL_TXH
97b045d6 1281
d9e8a70f 1282 txh_done:
97b045d6 1283 if (unlikely(res == TX_DROP)) {
5479d0e7 1284 I802_DEBUG_INC(tx->local->tx_handlers_drop);
2de8e0d9
JB
1285 while (skb) {
1286 struct sk_buff *next;
1287
1288 next = skb->next;
1289 dev_kfree_skb(skb);
1290 skb = next;
1291 }
97b045d6
JB
1292 return -1;
1293 } else if (unlikely(res == TX_QUEUED)) {
5479d0e7 1294 I802_DEBUG_INC(tx->local->tx_handlers_queued);
97b045d6
JB
1295 return -1;
1296 }
1297
1298 return 0;
1299}
1300
3b8d81e0
JB
1301static void ieee80211_tx(struct ieee80211_sub_if_data *sdata,
1302 struct sk_buff *skb, bool txpending)
e2ebc74d 1303{
3b8d81e0 1304 struct ieee80211_local *local = sdata->local;
5cf121c3 1305 struct ieee80211_tx_data tx;
97b045d6 1306 ieee80211_tx_result res_prepare;
e039fa4a 1307 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2a577d98
JB
1308 struct sk_buff *next;
1309 unsigned long flags;
1310 int ret, retries;
e2530083 1311 u16 queue;
e2ebc74d 1312
e2530083
JB
1313 queue = skb_get_queue_mapping(skb);
1314
e2ebc74d
JB
1315 if (unlikely(skb->len < 10)) {
1316 dev_kfree_skb(skb);
cd8ffc80 1317 return;
e2ebc74d
JB
1318 }
1319
d0709a65
JB
1320 rcu_read_lock();
1321
58d4185e 1322 /* initialises tx */
3b8d81e0 1323 res_prepare = ieee80211_tx_prepare(sdata, &tx, skb);
e2ebc74d 1324
cd8ffc80 1325 if (unlikely(res_prepare == TX_DROP)) {
e2ebc74d 1326 dev_kfree_skb(skb);
d0709a65 1327 rcu_read_unlock();
cd8ffc80
JB
1328 return;
1329 } else if (unlikely(res_prepare == TX_QUEUED)) {
1330 rcu_read_unlock();
1331 return;
e2ebc74d
JB
1332 }
1333
5cf121c3 1334 tx.channel = local->hw.conf.channel;
e039fa4a 1335 info->band = tx.channel->band;
e2ebc74d 1336
97b045d6
JB
1337 if (invoke_tx_handlers(&tx))
1338 goto out;
e2ebc74d 1339
2a577d98
JB
1340 retries = 0;
1341 retry:
3b8d81e0 1342 ret = __ieee80211_tx(local, &tx.skb, tx.sta, txpending);
2a577d98
JB
1343 switch (ret) {
1344 case IEEE80211_TX_OK:
1345 break;
1346 case IEEE80211_TX_AGAIN:
eefce91a
JB
1347 /*
1348 * Since there are no fragmented frames on A-MPDU
1349 * queues, there's no reason for a driver to reject
1350 * a frame there, warn and drop it.
1351 */
2a577d98
JB
1352 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
1353 goto drop;
1354 /* fall through */
1355 case IEEE80211_TX_PENDING:
1356 skb = tx.skb;
eefce91a 1357
2a577d98 1358 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
e2ebc74d 1359
3b8d81e0
JB
1360 if (local->queue_stop_reasons[queue] ||
1361 !skb_queue_empty(&local->pending[queue])) {
1362 /*
1363 * if queue is stopped, queue up frames for later
1364 * transmission from the tasklet
1365 */
2a577d98
JB
1366 do {
1367 next = skb->next;
1368 skb->next = NULL;
cd8ffc80 1369 if (unlikely(txpending))
3b8d81e0
JB
1370 __skb_queue_head(&local->pending[queue],
1371 skb);
cd8ffc80 1372 else
3b8d81e0
JB
1373 __skb_queue_tail(&local->pending[queue],
1374 skb);
2a577d98
JB
1375 } while ((skb = next));
1376
2a577d98
JB
1377 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1378 flags);
1379 } else {
3b8d81e0
JB
1380 /*
1381 * otherwise retry, but this is a race condition or
1382 * a driver bug (which we warn about if it persists)
1383 */
2a577d98
JB
1384 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1385 flags);
1386
1387 retries++;
3b8d81e0 1388 if (WARN(retries > 10, "tx refused but queue active\n"))
2a577d98 1389 goto drop;
e2ebc74d
JB
1390 goto retry;
1391 }
e2ebc74d 1392 }
97b045d6 1393 out:
d4e46a3d 1394 rcu_read_unlock();
cd8ffc80 1395 return;
e2ebc74d
JB
1396
1397 drop:
d4e46a3d 1398 rcu_read_unlock();
2de8e0d9
JB
1399
1400 skb = tx.skb;
1401 while (skb) {
2de8e0d9
JB
1402 next = skb->next;
1403 dev_kfree_skb(skb);
1404 skb = next;
1405 }
e2ebc74d
JB
1406}
1407
1408/* device xmit handlers */
1409
23c0752a
JB
1410static int ieee80211_skb_resize(struct ieee80211_local *local,
1411 struct sk_buff *skb,
1412 int head_need, bool may_encrypt)
1413{
1414 int tail_need = 0;
1415
1416 /*
1417 * This could be optimised, devices that do full hardware
1418 * crypto (including TKIP MMIC) need no tailroom... But we
1419 * have no drivers for such devices currently.
1420 */
1421 if (may_encrypt) {
1422 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1423 tail_need -= skb_tailroom(skb);
1424 tail_need = max_t(int, tail_need, 0);
1425 }
1426
1427 if (head_need || tail_need) {
1428 /* Sorry. Can't account for this any more */
1429 skb_orphan(skb);
1430 }
1431
1432 if (skb_header_cloned(skb))
1433 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1434 else
1435 I802_DEBUG_INC(local->tx_expand_skb_head);
1436
1437 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1438 printk(KERN_DEBUG "%s: failed to reallocate TX buffer\n",
1439 wiphy_name(local->hw.wiphy));
1440 return -ENOMEM;
1441 }
1442
1443 /* update truesize too */
1444 skb->truesize += head_need + tail_need;
1445
1446 return 0;
1447}
1448
3b8d81e0
JB
1449static void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
1450 struct sk_buff *skb)
e2ebc74d 1451{
3b8d81e0 1452 struct ieee80211_local *local = sdata->local;
e039fa4a 1453 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
e32f85f7 1454 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
3b8d81e0 1455 struct ieee80211_sub_if_data *tmp_sdata;
e2ebc74d 1456 int headroom;
23c0752a 1457 bool may_encrypt;
e2ebc74d 1458
d84f3234
JB
1459 rcu_read_lock();
1460
cca89496 1461 if (unlikely(sdata->vif.type == NL80211_IFTYPE_MONITOR)) {
25d834e1
JB
1462 int hdrlen;
1463 u16 len_rthdr;
1464
1465 info->flags |= IEEE80211_TX_CTL_INJECTED;
25d834e1
JB
1466
1467 len_rthdr = ieee80211_get_radiotap_len(skb->data);
8ef86c7b 1468 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
25d834e1
JB
1469 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1470
1471 /* check the header is complete in the frame */
1472 if (likely(skb->len >= len_rthdr + hdrlen)) {
1473 /*
1474 * We process outgoing injected frames that have a
1475 * local address we handle as though they are our
1476 * own frames.
1477 * This code here isn't entirely correct, the local
1478 * MAC address is not necessarily enough to find
1479 * the interface to use; for that proper VLAN/WDS
1480 * support we will need a different mechanism.
1481 */
1482
3b8d81e0 1483 list_for_each_entry_rcu(tmp_sdata, &local->interfaces,
25d834e1 1484 list) {
9607e6b6 1485 if (!ieee80211_sdata_running(tmp_sdata))
25d834e1 1486 continue;
3b8d81e0 1487 if (tmp_sdata->vif.type != NL80211_IFTYPE_AP)
f1b33cb1 1488 continue;
47846c9b 1489 if (compare_ether_addr(tmp_sdata->vif.addr,
9b1ce526 1490 hdr->addr2) == 0) {
3b8d81e0 1491 sdata = tmp_sdata;
25d834e1
JB
1492 break;
1493 }
1494 }
25d834e1 1495 }
e32f85f7
LCC
1496 }
1497
3b8d81e0 1498 may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
23c0752a 1499
3b8d81e0 1500 headroom = local->tx_headroom;
23c0752a
JB
1501 if (may_encrypt)
1502 headroom += IEEE80211_ENCRYPT_HEADROOM;
1503 headroom -= skb_headroom(skb);
1504 headroom = max_t(int, 0, headroom);
1505
3b8d81e0 1506 if (ieee80211_skb_resize(local, skb, headroom, may_encrypt)) {
23c0752a 1507 dev_kfree_skb(skb);
d84f3234 1508 rcu_read_unlock();
3b8d81e0 1509 return;
e2ebc74d
JB
1510 }
1511
5061b0c2 1512 info->control.vif = &sdata->vif;
e2ebc74d 1513
cca89496
JC
1514 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1515 ieee80211_is_data(hdr->frame_control) &&
1516 !is_multicast_ether_addr(hdr->addr1))
1517 if (mesh_nexthop_lookup(skb, sdata)) {
1518 /* skb queued: don't free */
d84f3234 1519 rcu_read_unlock();
cca89496
JC
1520 return;
1521 }
1522
cf0277e7 1523 ieee80211_set_qos_hdr(local, skb);
3b8d81e0 1524 ieee80211_tx(sdata, skb, false);
d84f3234 1525 rcu_read_unlock();
e2ebc74d
JB
1526}
1527
d0cf9c0d
SH
1528netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
1529 struct net_device *dev)
e2ebc74d
JB
1530{
1531 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
47f4d887 1532 struct ieee80211_channel *chan = local->hw.conf.channel;
e2ebc74d
JB
1533 struct ieee80211_radiotap_header *prthdr =
1534 (struct ieee80211_radiotap_header *)skb->data;
3b8d81e0 1535 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
9b8a74e3 1536 u16 len_rthdr;
e2ebc74d 1537
47f4d887
LR
1538 /*
1539 * Frame injection is not allowed if beaconing is not allowed
1540 * or if we need radar detection. Beaconing is usually not allowed when
1541 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
1542 * Passive scan is also used in world regulatory domains where
1543 * your country is not known and as such it should be treated as
1544 * NO TX unless the channel is explicitly allowed in which case
1545 * your current regulatory domain would not have the passive scan
1546 * flag.
1547 *
1548 * Since AP mode uses monitor interfaces to inject/TX management
1549 * frames we can make AP mode the exception to this rule once it
1550 * supports radar detection as its implementation can deal with
1551 * radar detection by itself. We can do that later by adding a
1552 * monitor flag interfaces used for AP support.
1553 */
1554 if ((chan->flags & (IEEE80211_CHAN_NO_IBSS | IEEE80211_CHAN_RADAR |
1555 IEEE80211_CHAN_PASSIVE_SCAN)))
1556 goto fail;
1557
9b8a74e3
AG
1558 /* check for not even having the fixed radiotap header part */
1559 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1560 goto fail; /* too short to be possibly valid */
1561
1562 /* is it a header version we can trust to find length from? */
1563 if (unlikely(prthdr->it_version))
1564 goto fail; /* only version 0 is supported */
1565
1566 /* then there must be a radiotap header with a length we can use */
1567 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1568
1569 /* does the skb contain enough to deliver on the alleged length? */
1570 if (unlikely(skb->len < len_rthdr))
1571 goto fail; /* skb too short for claimed rt header extent */
e2ebc74d 1572
e2ebc74d
JB
1573 /*
1574 * fix up the pointers accounting for the radiotap
1575 * header still being in there. We are being given
1576 * a precooked IEEE80211 header so no need for
1577 * normal processing
1578 */
9b8a74e3 1579 skb_set_mac_header(skb, len_rthdr);
e2ebc74d 1580 /*
9b8a74e3
AG
1581 * these are just fixed to the end of the rt area since we
1582 * don't have any better information and at this point, nobody cares
e2ebc74d 1583 */
9b8a74e3
AG
1584 skb_set_network_header(skb, len_rthdr);
1585 skb_set_transport_header(skb, len_rthdr);
e2ebc74d 1586
3b8d81e0
JB
1587 memset(info, 0, sizeof(*info));
1588
7351c6bd
JB
1589 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1590
3b8d81e0
JB
1591 /* pass the radiotap header up to xmit */
1592 ieee80211_xmit(IEEE80211_DEV_TO_SUB_IF(dev), skb);
e2ebc74d 1593 return NETDEV_TX_OK;
9b8a74e3
AG
1594
1595fail:
1596 dev_kfree_skb(skb);
1597 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
e2ebc74d
JB
1598}
1599
1600/**
1601 * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1602 * subinterfaces (wlan#, WDS, and VLAN interfaces)
1603 * @skb: packet to be sent
1604 * @dev: incoming interface
1605 *
1606 * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1607 * not be freed, and caller is responsible for either retrying later or freeing
1608 * skb).
1609 *
1610 * This function takes in an Ethernet header and encapsulates it with suitable
1611 * IEEE 802.11 header based on which interface the packet is coming in. The
1612 * encapsulated packet will then be passed to master interface, wlan#.11, for
1613 * transmission (through low-level driver).
1614 */
d0cf9c0d
SH
1615netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
1616 struct net_device *dev)
e2ebc74d 1617{
133b8226
JB
1618 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1619 struct ieee80211_local *local = sdata->local;
3b8d81e0 1620 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
5b548140 1621 int ret = NETDEV_TX_BUSY, head_need;
065e9605
HH
1622 u16 ethertype, hdrlen, meshhdrlen = 0;
1623 __le16 fc;
e2ebc74d 1624 struct ieee80211_hdr hdr;
33b64eb2 1625 struct ieee80211s_hdr mesh_hdr;
e2ebc74d
JB
1626 const u8 *encaps_data;
1627 int encaps_len, skip_header_bytes;
e8bf9649 1628 int nh_pos, h_pos;
f14543ee 1629 struct sta_info *sta = NULL;
ce3edf6d 1630 u32 sta_flags = 0;
e2ebc74d 1631
e2ebc74d 1632 if (unlikely(skb->len < ETH_HLEN)) {
ec634fe3 1633 ret = NETDEV_TX_OK;
e2ebc74d
JB
1634 goto fail;
1635 }
1636
1637 nh_pos = skb_network_header(skb) - skb->data;
1638 h_pos = skb_transport_header(skb) - skb->data;
1639
1640 /* convert Ethernet header to proper 802.11 header (based on
1641 * operation mode) */
1642 ethertype = (skb->data[12] << 8) | skb->data[13];
065e9605 1643 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
e2ebc74d 1644
51fb61e7 1645 switch (sdata->vif.type) {
05c914fe 1646 case NL80211_IFTYPE_AP_VLAN:
f14543ee 1647 rcu_read_lock();
9bc383de 1648 sta = rcu_dereference(sdata->u.vlan.sta);
f14543ee
FF
1649 if (sta) {
1650 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1651 /* RA TA DA SA */
1652 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
47846c9b 1653 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
f14543ee
FF
1654 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1655 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1656 hdrlen = 30;
1657 sta_flags = get_sta_flags(sta);
1658 }
1659 rcu_read_unlock();
1660 if (sta)
1661 break;
1662 /* fall through */
1663 case NL80211_IFTYPE_AP:
065e9605 1664 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
e2ebc74d
JB
1665 /* DA BSSID SA */
1666 memcpy(hdr.addr1, skb->data, ETH_ALEN);
47846c9b 1667 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
e2ebc74d
JB
1668 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1669 hdrlen = 24;
cf966838 1670 break;
05c914fe 1671 case NL80211_IFTYPE_WDS:
065e9605 1672 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
e2ebc74d
JB
1673 /* RA TA DA SA */
1674 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
47846c9b 1675 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
e2ebc74d
JB
1676 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1677 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1678 hdrlen = 30;
cf966838 1679 break;
33b64eb2 1680#ifdef CONFIG_MAC80211_MESH
05c914fe 1681 case NL80211_IFTYPE_MESH_POINT:
472dbc45 1682 if (!sdata->u.mesh.mshcfg.dot11MeshTTL) {
e32f85f7 1683 /* Do not send frames with mesh_ttl == 0 */
472dbc45 1684 sdata->u.mesh.mshstats.dropped_frames_ttl++;
ec634fe3 1685 ret = NETDEV_TX_OK;
e32f85f7 1686 goto fail;
33b64eb2 1687 }
79617dee 1688
47846c9b
JB
1689 if (compare_ether_addr(sdata->vif.addr,
1690 skb->data + ETH_ALEN) == 0) {
3c5772a5
JC
1691 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
1692 skb->data, skb->data + ETH_ALEN);
1693 meshhdrlen = ieee80211_new_mesh_header(&mesh_hdr,
1694 sdata, NULL, NULL, NULL);
79617dee
Y
1695 } else {
1696 /* packet from other interface */
1697 struct mesh_path *mppath;
3c5772a5 1698 int is_mesh_mcast = 1;
15ff6365 1699 const u8 *mesh_da;
79617dee 1700
3c5772a5 1701 rcu_read_lock();
79617dee 1702 if (is_multicast_ether_addr(skb->data))
3c5772a5
JC
1703 /* DA TA mSA AE:SA */
1704 mesh_da = skb->data;
79617dee 1705 else {
15ff6365
JB
1706 static const u8 bcast[ETH_ALEN] =
1707 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1708
79617dee 1709 mppath = mpp_path_lookup(skb->data, sdata);
3c5772a5
JC
1710 if (mppath) {
1711 /* RA TA mDA mSA AE:DA SA */
1712 mesh_da = mppath->mpp;
1713 is_mesh_mcast = 0;
15ff6365 1714 } else {
3c5772a5 1715 /* DA TA mSA AE:SA */
15ff6365
JB
1716 mesh_da = bcast;
1717 }
79617dee 1718 }
3c5772a5 1719 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
47846c9b 1720 mesh_da, sdata->vif.addr);
3c5772a5
JC
1721 rcu_read_unlock();
1722 if (is_mesh_mcast)
1723 meshhdrlen =
1724 ieee80211_new_mesh_header(&mesh_hdr,
1725 sdata,
1726 skb->data + ETH_ALEN,
1727 NULL,
1728 NULL);
1729 else
1730 meshhdrlen =
1731 ieee80211_new_mesh_header(&mesh_hdr,
1732 sdata,
1733 NULL,
1734 skb->data,
1735 skb->data + ETH_ALEN);
79617dee 1736
79617dee 1737 }
33b64eb2
LCC
1738 break;
1739#endif
05c914fe 1740 case NL80211_IFTYPE_STATION:
46900298 1741 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
9bc383de 1742 if (sdata->u.mgd.use_4addr && ethertype != ETH_P_PAE) {
f14543ee
FF
1743 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1744 /* RA TA DA SA */
47846c9b 1745 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
f14543ee
FF
1746 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1747 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1748 hdrlen = 30;
1749 } else {
1750 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
1751 /* BSSID SA DA */
1752 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1753 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1754 hdrlen = 24;
1755 }
cf966838 1756 break;
05c914fe 1757 case NL80211_IFTYPE_ADHOC:
e2ebc74d
JB
1758 /* DA SA BSSID */
1759 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1760 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
46900298 1761 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
e2ebc74d 1762 hdrlen = 24;
cf966838
JB
1763 break;
1764 default:
ec634fe3 1765 ret = NETDEV_TX_OK;
e2ebc74d
JB
1766 goto fail;
1767 }
1768
7d185b8b
JB
1769 /*
1770 * There's no need to try to look up the destination
1771 * if it is a multicast address (which can only happen
1772 * in AP mode)
1773 */
1774 if (!is_multicast_ether_addr(hdr.addr1)) {
d0709a65 1775 rcu_read_lock();
abe60632
JB
1776 sta = sta_info_get(sdata, hdr.addr1);
1777 if (sta)
07346f81 1778 sta_flags = get_sta_flags(sta);
d0709a65 1779 rcu_read_unlock();
e2ebc74d
JB
1780 }
1781
3434fbd3 1782 /* receiver and we are QoS enabled, use a QoS type frame */
176be728 1783 if ((sta_flags & WLAN_STA_WME) && local->hw.queues >= 4) {
065e9605 1784 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
ce3edf6d
JB
1785 hdrlen += 2;
1786 }
1787
1788 /*
238814fd
JB
1789 * Drop unicast frames to unauthorised stations unless they are
1790 * EAPOL frames from the local station.
ce3edf6d 1791 */
e32f85f7
LCC
1792 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1793 unlikely(!is_multicast_ether_addr(hdr.addr1) &&
33b64eb2
LCC
1794 !(sta_flags & WLAN_STA_AUTHORIZED) &&
1795 !(ethertype == ETH_P_PAE &&
47846c9b 1796 compare_ether_addr(sdata->vif.addr,
ce3edf6d
JB
1797 skb->data + ETH_ALEN) == 0))) {
1798#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
ce3edf6d 1799 if (net_ratelimit())
0c68ae26 1800 printk(KERN_DEBUG "%s: dropped frame to %pM"
ce3edf6d 1801 " (unauthorized port)\n", dev->name,
0c68ae26 1802 hdr.addr1);
ce3edf6d
JB
1803#endif
1804
1805 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
1806
ec634fe3 1807 ret = NETDEV_TX_OK;
ce3edf6d
JB
1808 goto fail;
1809 }
1810
065e9605 1811 hdr.frame_control = fc;
e2ebc74d
JB
1812 hdr.duration_id = 0;
1813 hdr.seq_ctrl = 0;
1814
1815 skip_header_bytes = ETH_HLEN;
1816 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
1817 encaps_data = bridge_tunnel_header;
1818 encaps_len = sizeof(bridge_tunnel_header);
1819 skip_header_bytes -= 2;
1820 } else if (ethertype >= 0x600) {
1821 encaps_data = rfc1042_header;
1822 encaps_len = sizeof(rfc1042_header);
1823 skip_header_bytes -= 2;
1824 } else {
1825 encaps_data = NULL;
1826 encaps_len = 0;
1827 }
1828
1829 skb_pull(skb, skip_header_bytes);
1830 nh_pos -= skip_header_bytes;
1831 h_pos -= skip_header_bytes;
1832
23c0752a 1833 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
e2ebc74d 1834
23c0752a
JB
1835 /*
1836 * So we need to modify the skb header and hence need a copy of
1837 * that. The head_need variable above doesn't, so far, include
1838 * the needed header space that we don't need right away. If we
1839 * can, then we don't reallocate right now but only after the
1840 * frame arrives at the master device (if it does...)
1841 *
1842 * If we cannot, however, then we will reallocate to include all
1843 * the ever needed space. Also, if we need to reallocate it anyway,
1844 * make it big enough for everything we may ever need.
1845 */
e2ebc74d 1846
3a5be7d4 1847 if (head_need > 0 || skb_cloned(skb)) {
23c0752a
JB
1848 head_need += IEEE80211_ENCRYPT_HEADROOM;
1849 head_need += local->tx_headroom;
1850 head_need = max_t(int, 0, head_need);
1851 if (ieee80211_skb_resize(local, skb, head_need, true))
e2ebc74d 1852 goto fail;
e2ebc74d
JB
1853 }
1854
1855 if (encaps_data) {
1856 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
1857 nh_pos += encaps_len;
1858 h_pos += encaps_len;
1859 }
c29b9b9b 1860
33b64eb2
LCC
1861 if (meshhdrlen > 0) {
1862 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
1863 nh_pos += meshhdrlen;
1864 h_pos += meshhdrlen;
1865 }
1866
065e9605 1867 if (ieee80211_is_data_qos(fc)) {
c29b9b9b
JB
1868 __le16 *qos_control;
1869
1870 qos_control = (__le16*) skb_push(skb, 2);
1871 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
1872 /*
1873 * Maybe we could actually set some fields here, for now just
1874 * initialise to zero to indicate no special operation.
1875 */
1876 *qos_control = 0;
1877 } else
1878 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
1879
e2ebc74d
JB
1880 nh_pos += hdrlen;
1881 h_pos += hdrlen;
1882
68aae116
SH
1883 dev->stats.tx_packets++;
1884 dev->stats.tx_bytes += skb->len;
e2ebc74d
JB
1885
1886 /* Update skb pointers to various headers since this modified frame
1887 * is going to go through Linux networking code that may potentially
1888 * need things like pointer to IP header. */
1889 skb_set_mac_header(skb, 0);
1890 skb_set_network_header(skb, nh_pos);
1891 skb_set_transport_header(skb, h_pos);
1892
3b8d81e0
JB
1893 memset(info, 0, sizeof(*info));
1894
e2ebc74d 1895 dev->trans_start = jiffies;
3b8d81e0 1896 ieee80211_xmit(sdata, skb);
e2ebc74d 1897
ec634fe3 1898 return NETDEV_TX_OK;
e2ebc74d
JB
1899
1900 fail:
ec634fe3 1901 if (ret == NETDEV_TX_OK)
e2ebc74d
JB
1902 dev_kfree_skb(skb);
1903
1904 return ret;
1905}
1906
e2ebc74d 1907
e2530083
JB
1908/*
1909 * ieee80211_clear_tx_pending may not be called in a context where
1910 * it is possible that it packets could come in again.
1911 */
e2ebc74d
JB
1912void ieee80211_clear_tx_pending(struct ieee80211_local *local)
1913{
2de8e0d9 1914 int i;
e2ebc74d 1915
2a577d98
JB
1916 for (i = 0; i < local->hw.queues; i++)
1917 skb_queue_purge(&local->pending[i]);
e2ebc74d
JB
1918}
1919
cd8ffc80
JB
1920static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
1921 struct sk_buff *skb)
1922{
1923 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1924 struct ieee80211_sub_if_data *sdata;
1925 struct sta_info *sta;
1926 struct ieee80211_hdr *hdr;
cd8ffc80
JB
1927 int ret;
1928 bool result = true;
1929
5061b0c2 1930 sdata = vif_to_sdata(info->control.vif);
cd8ffc80
JB
1931
1932 if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
5061b0c2 1933 ieee80211_tx(sdata, skb, true);
cd8ffc80
JB
1934 } else {
1935 hdr = (struct ieee80211_hdr *)skb->data;
abe60632 1936 sta = sta_info_get(sdata, hdr->addr1);
cd8ffc80 1937
3b8d81e0 1938 ret = __ieee80211_tx(local, &skb, sta, true);
cd8ffc80
JB
1939 if (ret != IEEE80211_TX_OK)
1940 result = false;
1941 }
1942
cd8ffc80
JB
1943 return result;
1944}
1945
e2530083 1946/*
3b8d81e0 1947 * Transmit all pending packets. Called from tasklet.
e2530083 1948 */
e2ebc74d
JB
1949void ieee80211_tx_pending(unsigned long data)
1950{
1951 struct ieee80211_local *local = (struct ieee80211_local *)data;
2a577d98 1952 unsigned long flags;
cd8ffc80 1953 int i;
3b8d81e0 1954 bool txok;
e2ebc74d 1955
f0e72851 1956 rcu_read_lock();
e2530083 1957
3b8d81e0 1958 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
2a577d98
JB
1959 for (i = 0; i < local->hw.queues; i++) {
1960 /*
1961 * If queue is stopped by something other than due to pending
1962 * frames, or we have no pending frames, proceed to next queue.
1963 */
3b8d81e0 1964 if (local->queue_stop_reasons[i] ||
2a577d98 1965 skb_queue_empty(&local->pending[i]))
e2ebc74d 1966 continue;
e2530083 1967
2a577d98 1968 while (!skb_queue_empty(&local->pending[i])) {
3b8d81e0 1969 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
5061b0c2
JB
1970 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1971 struct ieee80211_sub_if_data *sdata;
1972
a7bc376c
JB
1973 if (WARN_ON(!info->control.vif)) {
1974 kfree_skb(skb);
1975 continue;
1976 }
1977
5061b0c2 1978 sdata = vif_to_sdata(info->control.vif);
3b8d81e0
JB
1979 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1980 flags);
1981
1982 txok = ieee80211_tx_pending_skb(local, skb);
1983 if (!txok)
1984 __skb_queue_head(&local->pending[i], skb);
1985 spin_lock_irqsave(&local->queue_stop_reason_lock,
1986 flags);
1987 if (!txok)
2a577d98 1988 break;
e2ebc74d
JB
1989 }
1990 }
3b8d81e0 1991 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
2a577d98 1992
f0e72851 1993 rcu_read_unlock();
e2ebc74d
JB
1994}
1995
1996/* functions for drivers to get certain frames */
1997
8e7be8da 1998static void ieee80211_beacon_add_tim(struct ieee80211_if_ap *bss,
5dfdaf58
JB
1999 struct sk_buff *skb,
2000 struct beacon_data *beacon)
e2ebc74d
JB
2001{
2002 u8 *pos, *tim;
2003 int aid0 = 0;
2004 int i, have_bits = 0, n1, n2;
2005
2006 /* Generate bitmap for TIM only if there are any STAs in power save
2007 * mode. */
e2ebc74d
JB
2008 if (atomic_read(&bss->num_sta_ps) > 0)
2009 /* in the hope that this is faster than
2010 * checking byte-for-byte */
2011 have_bits = !bitmap_empty((unsigned long*)bss->tim,
2012 IEEE80211_MAX_AID+1);
2013
2014 if (bss->dtim_count == 0)
5dfdaf58 2015 bss->dtim_count = beacon->dtim_period - 1;
e2ebc74d
JB
2016 else
2017 bss->dtim_count--;
2018
2019 tim = pos = (u8 *) skb_put(skb, 6);
2020 *pos++ = WLAN_EID_TIM;
2021 *pos++ = 4;
2022 *pos++ = bss->dtim_count;
5dfdaf58 2023 *pos++ = beacon->dtim_period;
e2ebc74d
JB
2024
2025 if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
2026 aid0 = 1;
2027
2028 if (have_bits) {
2029 /* Find largest even number N1 so that bits numbered 1 through
2030 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
2031 * (N2 + 1) x 8 through 2007 are 0. */
2032 n1 = 0;
2033 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
2034 if (bss->tim[i]) {
2035 n1 = i & 0xfe;
2036 break;
2037 }
2038 }
2039 n2 = n1;
2040 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
2041 if (bss->tim[i]) {
2042 n2 = i;
2043 break;
2044 }
2045 }
2046
2047 /* Bitmap control */
2048 *pos++ = n1 | aid0;
2049 /* Part Virt Bitmap */
2050 memcpy(pos, bss->tim + n1, n2 - n1 + 1);
2051
2052 tim[1] = n2 - n1 + 4;
2053 skb_put(skb, n2 - n1);
2054 } else {
2055 *pos++ = aid0; /* Bitmap control */
2056 *pos++ = 0; /* Part Virt Bitmap */
2057 }
e2ebc74d
JB
2058}
2059
eddcbb94
JB
2060struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
2061 struct ieee80211_vif *vif,
2062 u16 *tim_offset, u16 *tim_length)
e2ebc74d
JB
2063{
2064 struct ieee80211_local *local = hw_to_local(hw);
9d139c81 2065 struct sk_buff *skb = NULL;
e039fa4a 2066 struct ieee80211_tx_info *info;
e2ebc74d
JB
2067 struct ieee80211_sub_if_data *sdata = NULL;
2068 struct ieee80211_if_ap *ap = NULL;
5dfdaf58 2069 struct beacon_data *beacon;
8318d78a 2070 struct ieee80211_supported_band *sband;
2e92e6f2 2071 enum ieee80211_band band = local->hw.conf.channel->band;
e00cfce0 2072 struct ieee80211_tx_rate_control txrc;
8318d78a 2073
2e92e6f2 2074 sband = local->hw.wiphy->bands[band];
5dfdaf58
JB
2075
2076 rcu_read_lock();
e2ebc74d 2077
32bfd35d 2078 sdata = vif_to_sdata(vif);
e2ebc74d 2079
eddcbb94
JB
2080 if (tim_offset)
2081 *tim_offset = 0;
2082 if (tim_length)
2083 *tim_length = 0;
2084
05c914fe 2085 if (sdata->vif.type == NL80211_IFTYPE_AP) {
33b64eb2
LCC
2086 ap = &sdata->u.ap;
2087 beacon = rcu_dereference(ap->beacon);
902acc78
JB
2088 if (ap && beacon) {
2089 /*
2090 * headroom, head length,
2091 * tail length and maximum TIM length
2092 */
2093 skb = dev_alloc_skb(local->tx_headroom +
2094 beacon->head_len +
2095 beacon->tail_len + 256);
2096 if (!skb)
2097 goto out;
33b64eb2 2098
902acc78
JB
2099 skb_reserve(skb, local->tx_headroom);
2100 memcpy(skb_put(skb, beacon->head_len), beacon->head,
2101 beacon->head_len);
33b64eb2 2102
d0709a65
JB
2103 /*
2104 * Not very nice, but we want to allow the driver to call
2105 * ieee80211_beacon_get() as a response to the set_tim()
2106 * callback. That, however, is already invoked under the
2107 * sta_lock to guarantee consistent and race-free update
2108 * of the tim bitmap in mac80211 and the driver.
2109 */
2110 if (local->tim_in_locked_section) {
8e7be8da 2111 ieee80211_beacon_add_tim(ap, skb, beacon);
d0709a65
JB
2112 } else {
2113 unsigned long flags;
2114
2115 spin_lock_irqsave(&local->sta_lock, flags);
8e7be8da 2116 ieee80211_beacon_add_tim(ap, skb, beacon);
d0709a65
JB
2117 spin_unlock_irqrestore(&local->sta_lock, flags);
2118 }
33b64eb2 2119
eddcbb94
JB
2120 if (tim_offset)
2121 *tim_offset = beacon->head_len;
2122 if (tim_length)
2123 *tim_length = skb->len - beacon->head_len;
2124
902acc78
JB
2125 if (beacon->tail)
2126 memcpy(skb_put(skb, beacon->tail_len),
2127 beacon->tail, beacon->tail_len);
9d139c81
JB
2128 } else
2129 goto out;
05c914fe 2130 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
46900298 2131 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
9d139c81 2132 struct ieee80211_hdr *hdr;
af8cdcd8 2133 struct sk_buff *presp = rcu_dereference(ifibss->presp);
33b64eb2 2134
af8cdcd8 2135 if (!presp)
9d139c81
JB
2136 goto out;
2137
af8cdcd8 2138 skb = skb_copy(presp, GFP_ATOMIC);
9d139c81
JB
2139 if (!skb)
2140 goto out;
2141
2142 hdr = (struct ieee80211_hdr *) skb->data;
e7827a70
HH
2143 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2144 IEEE80211_STYPE_BEACON);
902acc78 2145 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
472dbc45
JB
2146 struct ieee80211_mgmt *mgmt;
2147 u8 *pos;
2148
902acc78
JB
2149 /* headroom, head length, tail length and maximum TIM length */
2150 skb = dev_alloc_skb(local->tx_headroom + 400);
2151 if (!skb)
2152 goto out;
2153
2154 skb_reserve(skb, local->hw.extra_tx_headroom);
2155 mgmt = (struct ieee80211_mgmt *)
2156 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2157 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
065e9605
HH
2158 mgmt->frame_control =
2159 cpu_to_le16(IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_BEACON);
902acc78 2160 memset(mgmt->da, 0xff, ETH_ALEN);
47846c9b
JB
2161 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
2162 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
902acc78 2163 mgmt->u.beacon.beacon_int =
57c4d7b4 2164 cpu_to_le16(sdata->vif.bss_conf.beacon_int);
902acc78
JB
2165 mgmt->u.beacon.capab_info = 0x0; /* 0x0 for MPs */
2166
2167 pos = skb_put(skb, 2);
2168 *pos++ = WLAN_EID_SSID;
2169 *pos++ = 0x0;
2170
f698d856 2171 mesh_mgmt_ies_add(skb, sdata);
9d139c81
JB
2172 } else {
2173 WARN_ON(1);
5dfdaf58 2174 goto out;
e2ebc74d
JB
2175 }
2176
e039fa4a
JB
2177 info = IEEE80211_SKB_CB(skb);
2178
3b8d81e0 2179 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
e00cfce0 2180 info->flags |= IEEE80211_TX_CTL_NO_ACK;
e039fa4a 2181 info->band = band;
e00cfce0
JM
2182
2183 memset(&txrc, 0, sizeof(txrc));
2184 txrc.hw = hw;
2185 txrc.sband = sband;
2186 txrc.bss_conf = &sdata->vif.bss_conf;
2187 txrc.skb = skb;
2188 txrc.reported_rate.idx = -1;
37eb0b16
JM
2189 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
2190 if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
2191 txrc.max_rate_idx = -1;
2192 else
2193 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
e00cfce0
JM
2194 txrc.ap = true;
2195 rate_control_get_rate(sdata, NULL, &txrc);
e039fa4a
JB
2196
2197 info->control.vif = vif;
f591fa5d 2198
f591fa5d
JB
2199 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
2200 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
e6a9854b 2201 out:
5dfdaf58 2202 rcu_read_unlock();
e2ebc74d
JB
2203 return skb;
2204}
eddcbb94 2205EXPORT_SYMBOL(ieee80211_beacon_get_tim);
e2ebc74d 2206
7044cc56
KV
2207struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
2208 struct ieee80211_vif *vif)
2209{
2210 struct ieee80211_sub_if_data *sdata;
2211 struct ieee80211_if_managed *ifmgd;
2212 struct ieee80211_pspoll *pspoll;
2213 struct ieee80211_local *local;
2214 struct sk_buff *skb;
2215
2216 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2217 return NULL;
2218
2219 sdata = vif_to_sdata(vif);
2220 ifmgd = &sdata->u.mgd;
2221 local = sdata->local;
2222
2223 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
2224 if (!skb) {
2225 printk(KERN_DEBUG "%s: failed to allocate buffer for "
2226 "pspoll template\n", sdata->name);
2227 return NULL;
2228 }
2229 skb_reserve(skb, local->hw.extra_tx_headroom);
2230
2231 pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
2232 memset(pspoll, 0, sizeof(*pspoll));
2233 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
2234 IEEE80211_STYPE_PSPOLL);
2235 pspoll->aid = cpu_to_le16(ifmgd->aid);
2236
2237 /* aid in PS-Poll has its two MSBs each set to 1 */
2238 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
2239
2240 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
2241 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
2242
2243 return skb;
2244}
2245EXPORT_SYMBOL(ieee80211_pspoll_get);
2246
2247struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
2248 struct ieee80211_vif *vif)
2249{
2250 struct ieee80211_hdr_3addr *nullfunc;
2251 struct ieee80211_sub_if_data *sdata;
2252 struct ieee80211_if_managed *ifmgd;
2253 struct ieee80211_local *local;
2254 struct sk_buff *skb;
2255
2256 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2257 return NULL;
2258
2259 sdata = vif_to_sdata(vif);
2260 ifmgd = &sdata->u.mgd;
2261 local = sdata->local;
2262
2263 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*nullfunc));
2264 if (!skb) {
2265 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2266 "template\n", sdata->name);
2267 return NULL;
2268 }
2269 skb_reserve(skb, local->hw.extra_tx_headroom);
2270
2271 nullfunc = (struct ieee80211_hdr_3addr *) skb_put(skb,
2272 sizeof(*nullfunc));
2273 memset(nullfunc, 0, sizeof(*nullfunc));
2274 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
2275 IEEE80211_STYPE_NULLFUNC |
2276 IEEE80211_FCTL_TODS);
2277 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
2278 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
2279 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
2280
2281 return skb;
2282}
2283EXPORT_SYMBOL(ieee80211_nullfunc_get);
2284
05e54ea6
KV
2285struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
2286 struct ieee80211_vif *vif,
2287 const u8 *ssid, size_t ssid_len,
2288 const u8 *ie, size_t ie_len)
2289{
2290 struct ieee80211_sub_if_data *sdata;
2291 struct ieee80211_local *local;
2292 struct ieee80211_hdr_3addr *hdr;
2293 struct sk_buff *skb;
2294 size_t ie_ssid_len;
2295 u8 *pos;
2296
2297 sdata = vif_to_sdata(vif);
2298 local = sdata->local;
2299 ie_ssid_len = 2 + ssid_len;
2300
2301 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
2302 ie_ssid_len + ie_len);
2303 if (!skb) {
2304 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
2305 "request template\n", sdata->name);
2306 return NULL;
2307 }
2308
2309 skb_reserve(skb, local->hw.extra_tx_headroom);
2310
2311 hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
2312 memset(hdr, 0, sizeof(*hdr));
2313 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2314 IEEE80211_STYPE_PROBE_REQ);
2315 memset(hdr->addr1, 0xff, ETH_ALEN);
2316 memcpy(hdr->addr2, vif->addr, ETH_ALEN);
2317 memset(hdr->addr3, 0xff, ETH_ALEN);
2318
2319 pos = skb_put(skb, ie_ssid_len);
2320 *pos++ = WLAN_EID_SSID;
2321 *pos++ = ssid_len;
2322 if (ssid)
2323 memcpy(pos, ssid, ssid_len);
2324 pos += ssid_len;
2325
2326 if (ie) {
2327 pos = skb_put(skb, ie_len);
2328 memcpy(pos, ie, ie_len);
2329 }
2330
2331 return skb;
2332}
2333EXPORT_SYMBOL(ieee80211_probereq_get);
2334
32bfd35d 2335void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
e2ebc74d 2336 const void *frame, size_t frame_len,
e039fa4a 2337 const struct ieee80211_tx_info *frame_txctl,
e2ebc74d
JB
2338 struct ieee80211_rts *rts)
2339{
2340 const struct ieee80211_hdr *hdr = frame;
e2ebc74d 2341
065e9605
HH
2342 rts->frame_control =
2343 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
32bfd35d
JB
2344 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
2345 frame_txctl);
e2ebc74d
JB
2346 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
2347 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
2348}
2349EXPORT_SYMBOL(ieee80211_rts_get);
2350
32bfd35d 2351void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
e2ebc74d 2352 const void *frame, size_t frame_len,
e039fa4a 2353 const struct ieee80211_tx_info *frame_txctl,
e2ebc74d
JB
2354 struct ieee80211_cts *cts)
2355{
2356 const struct ieee80211_hdr *hdr = frame;
e2ebc74d 2357
065e9605
HH
2358 cts->frame_control =
2359 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
32bfd35d
JB
2360 cts->duration = ieee80211_ctstoself_duration(hw, vif,
2361 frame_len, frame_txctl);
e2ebc74d
JB
2362 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
2363}
2364EXPORT_SYMBOL(ieee80211_ctstoself_get);
2365
2366struct sk_buff *
32bfd35d 2367ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
e039fa4a 2368 struct ieee80211_vif *vif)
e2ebc74d
JB
2369{
2370 struct ieee80211_local *local = hw_to_local(hw);
747cf5e9 2371 struct sk_buff *skb = NULL;
e2ebc74d 2372 struct sta_info *sta;
5cf121c3 2373 struct ieee80211_tx_data tx;
e2ebc74d
JB
2374 struct ieee80211_sub_if_data *sdata;
2375 struct ieee80211_if_ap *bss = NULL;
5dfdaf58 2376 struct beacon_data *beacon;
e039fa4a 2377 struct ieee80211_tx_info *info;
e2ebc74d 2378
32bfd35d 2379 sdata = vif_to_sdata(vif);
747cf5e9 2380 bss = &sdata->u.ap;
5dfdaf58 2381
5dfdaf58
JB
2382 rcu_read_lock();
2383 beacon = rcu_dereference(bss->beacon);
2384
05c914fe 2385 if (sdata->vif.type != NL80211_IFTYPE_AP || !beacon || !beacon->head)
747cf5e9 2386 goto out;
5dfdaf58 2387
e2ebc74d 2388 if (bss->dtim_count != 0)
747cf5e9 2389 goto out; /* send buffered bc/mc only after DTIM beacon */
e039fa4a 2390
e2ebc74d
JB
2391 while (1) {
2392 skb = skb_dequeue(&bss->ps_bc_buf);
2393 if (!skb)
747cf5e9 2394 goto out;
e2ebc74d
JB
2395 local->total_ps_buffered--;
2396
2397 if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
2398 struct ieee80211_hdr *hdr =
2399 (struct ieee80211_hdr *) skb->data;
2400 /* more buffered multicast/broadcast frames ==> set
2401 * MoreData flag in IEEE 802.11 header to inform PS
2402 * STAs */
2403 hdr->frame_control |=
2404 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2405 }
2406
3b8d81e0 2407 if (!ieee80211_tx_prepare(sdata, &tx, skb))
e2ebc74d
JB
2408 break;
2409 dev_kfree_skb_any(skb);
2410 }
e039fa4a
JB
2411
2412 info = IEEE80211_SKB_CB(skb);
2413
e2ebc74d 2414 sta = tx.sta;
5cf121c3
JB
2415 tx.flags |= IEEE80211_TX_PS_BUFFERED;
2416 tx.channel = local->hw.conf.channel;
e039fa4a 2417 info->band = tx.channel->band;
e2ebc74d 2418
97b045d6 2419 if (invoke_tx_handlers(&tx))
e2ebc74d 2420 skb = NULL;
97b045d6 2421 out:
d0709a65 2422 rcu_read_unlock();
e2ebc74d
JB
2423
2424 return skb;
2425}
2426EXPORT_SYMBOL(ieee80211_get_buffered_bc);
3b8d81e0 2427
62ae67be 2428void ieee80211_tx_skb(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
3b8d81e0 2429{
3b8d81e0
JB
2430 skb_set_mac_header(skb, 0);
2431 skb_set_network_header(skb, 0);
2432 skb_set_transport_header(skb, 0);
2433
cf0277e7
JB
2434 /* send all internal mgmt frames on VO */
2435 skb_set_queue_mapping(skb, 0);
2436
3d34deb6
JB
2437 /*
2438 * The other path calling ieee80211_xmit is from the tasklet,
2439 * and while we can handle concurrent transmissions locking
2440 * requirements are that we do not come into tx with bhs on.
2441 */
2442 local_bh_disable();
3b8d81e0 2443 ieee80211_xmit(sdata, skb);
3d34deb6 2444 local_bh_enable();
3b8d81e0 2445}