mac80211: use rate index in TX control
[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"
2c8dccc7 28#include "led.h"
33b64eb2 29#include "mesh.h"
e2ebc74d
JB
30#include "wep.h"
31#include "wpa.h"
32#include "wme.h"
2c8dccc7 33#include "rate.h"
e2ebc74d
JB
34
35#define IEEE80211_TX_OK 0
36#define IEEE80211_TX_AGAIN 1
37#define IEEE80211_TX_FRAG_AGAIN 2
38
39/* misc utils */
40
41static inline void ieee80211_include_sequence(struct ieee80211_sub_if_data *sdata,
42 struct ieee80211_hdr *hdr)
43{
44 /* Set the sequence number for this frame. */
45 hdr->seq_ctrl = cpu_to_le16(sdata->sequence);
46
47 /* Increase the sequence number. */
48 sdata->sequence = (sdata->sequence + 0x10) & IEEE80211_SCTL_SEQ;
49}
50
51#ifdef CONFIG_MAC80211_LOWTX_FRAME_DUMP
52static void ieee80211_dump_frame(const char *ifname, const char *title,
53 const struct sk_buff *skb)
54{
55 const struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
56 u16 fc;
57 int hdrlen;
0795af57 58 DECLARE_MAC_BUF(mac);
e2ebc74d
JB
59
60 printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
61 if (skb->len < 4) {
62 printk("\n");
63 return;
64 }
65
66 fc = le16_to_cpu(hdr->frame_control);
67 hdrlen = ieee80211_get_hdrlen(fc);
68 if (hdrlen > skb->len)
69 hdrlen = skb->len;
70 if (hdrlen >= 4)
71 printk(" FC=0x%04x DUR=0x%04x",
72 fc, le16_to_cpu(hdr->duration_id));
73 if (hdrlen >= 10)
0795af57 74 printk(" A1=%s", print_mac(mac, hdr->addr1));
e2ebc74d 75 if (hdrlen >= 16)
0795af57 76 printk(" A2=%s", print_mac(mac, hdr->addr2));
e2ebc74d 77 if (hdrlen >= 24)
0795af57 78 printk(" A3=%s", print_mac(mac, hdr->addr3));
e2ebc74d 79 if (hdrlen >= 30)
0795af57 80 printk(" A4=%s", print_mac(mac, hdr->addr4));
e2ebc74d
JB
81 printk("\n");
82}
83#else /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
84static inline void ieee80211_dump_frame(const char *ifname, const char *title,
85 struct sk_buff *skb)
86{
87}
88#endif /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
89
5cf121c3 90static u16 ieee80211_duration(struct ieee80211_tx_data *tx, int group_addr,
e2ebc74d
JB
91 int next_frag_len)
92{
93 int rate, mrate, erp, dur, i;
2e92e6f2 94 struct ieee80211_rate *txrate;
e2ebc74d 95 struct ieee80211_local *local = tx->local;
8318d78a 96 struct ieee80211_supported_band *sband;
e2ebc74d 97
2e92e6f2
JB
98 sband = local->hw.wiphy->bands[tx->channel->band];
99 txrate = &sband->bitrates[tx->rate_idx];
8318d78a
JB
100
101 erp = 0;
102 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
103 erp = txrate->flags & IEEE80211_RATE_ERP_G;
e2ebc74d
JB
104
105 /*
106 * data and mgmt (except PS Poll):
107 * - during CFP: 32768
108 * - during contention period:
109 * if addr1 is group address: 0
110 * if more fragments = 0 and addr1 is individual address: time to
111 * transmit one ACK plus SIFS
112 * if more fragments = 1 and addr1 is individual address: time to
113 * transmit next fragment plus 2 x ACK plus 3 x SIFS
114 *
115 * IEEE 802.11, 9.6:
116 * - control response frame (CTS or ACK) shall be transmitted using the
117 * same rate as the immediately previous frame in the frame exchange
118 * sequence, if this rate belongs to the PHY mandatory rates, or else
119 * at the highest possible rate belonging to the PHY rates in the
120 * BSSBasicRateSet
121 */
122
123 if ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) {
124 /* TODO: These control frames are not currently sent by
125 * 80211.o, but should they be implemented, this function
126 * needs to be updated to support duration field calculation.
127 *
128 * RTS: time needed to transmit pending data/mgmt frame plus
129 * one CTS frame plus one ACK frame plus 3 x SIFS
130 * CTS: duration of immediately previous RTS minus time
131 * required to transmit CTS and its SIFS
132 * ACK: 0 if immediately previous directed data/mgmt had
133 * more=0, with more=1 duration in ACK frame is duration
134 * from previous frame minus time needed to transmit ACK
135 * and its SIFS
136 * PS Poll: BIT(15) | BIT(14) | aid
137 */
138 return 0;
139 }
140
141 /* data/mgmt */
142 if (0 /* FIX: data/mgmt during CFP */)
143 return 32768;
144
145 if (group_addr) /* Group address as the destination - no ACK */
146 return 0;
147
148 /* Individual destination address:
149 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
150 * CTS and ACK frames shall be transmitted using the highest rate in
151 * basic rate set that is less than or equal to the rate of the
152 * immediately previous frame and that is using the same modulation
153 * (CCK or OFDM). If no basic rate set matches with these requirements,
154 * the highest mandatory rate of the PHY that is less than or equal to
155 * the rate of the previous frame is used.
156 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
157 */
158 rate = -1;
8318d78a
JB
159 /* use lowest available if everything fails */
160 mrate = sband->bitrates[0].bitrate;
161 for (i = 0; i < sband->n_bitrates; i++) {
162 struct ieee80211_rate *r = &sband->bitrates[i];
e2ebc74d 163
8318d78a
JB
164 if (r->bitrate > txrate->bitrate)
165 break;
e2ebc74d 166
8318d78a
JB
167 if (tx->sdata->basic_rates & BIT(i))
168 rate = r->bitrate;
169
170 switch (sband->band) {
171 case IEEE80211_BAND_2GHZ: {
172 u32 flag;
173 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
174 flag = IEEE80211_RATE_MANDATORY_G;
175 else
176 flag = IEEE80211_RATE_MANDATORY_B;
177 if (r->flags & flag)
178 mrate = r->bitrate;
179 break;
180 }
181 case IEEE80211_BAND_5GHZ:
182 if (r->flags & IEEE80211_RATE_MANDATORY_A)
183 mrate = r->bitrate;
184 break;
185 case IEEE80211_NUM_BANDS:
186 WARN_ON(1);
187 break;
188 }
e2ebc74d
JB
189 }
190 if (rate == -1) {
191 /* No matching basic rate found; use highest suitable mandatory
192 * PHY rate */
193 rate = mrate;
194 }
195
196 /* Time needed to transmit ACK
197 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
198 * to closest integer */
199
200 dur = ieee80211_frame_duration(local, 10, rate, erp,
471b3efd 201 tx->sdata->bss_conf.use_short_preamble);
e2ebc74d
JB
202
203 if (next_frag_len) {
204 /* Frame is fragmented: duration increases with time needed to
205 * transmit next fragment plus ACK and 2 x SIFS. */
206 dur *= 2; /* ACK + SIFS */
207 /* next fragment */
208 dur += ieee80211_frame_duration(local, next_frag_len,
8318d78a 209 txrate->bitrate, erp,
471b3efd 210 tx->sdata->bss_conf.use_short_preamble);
e2ebc74d
JB
211 }
212
213 return dur;
214}
215
216static inline int __ieee80211_queue_stopped(const struct ieee80211_local *local,
217 int queue)
218{
219 return test_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
220}
221
222static inline int __ieee80211_queue_pending(const struct ieee80211_local *local,
223 int queue)
224{
225 return test_bit(IEEE80211_LINK_STATE_PENDING, &local->state[queue]);
226}
227
228static int inline is_ieee80211_device(struct net_device *dev,
229 struct net_device *master)
230{
231 return (wdev_priv(dev->ieee80211_ptr) ==
232 wdev_priv(master->ieee80211_ptr));
233}
234
235/* tx handlers */
236
9ae54c84 237static ieee80211_tx_result
5cf121c3 238ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
e2ebc74d
JB
239{
240#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
241 struct sk_buff *skb = tx->skb;
242 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
243#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
244 u32 sta_flags;
245
5cf121c3 246 if (unlikely(tx->flags & IEEE80211_TX_INJECTED))
9ae54c84 247 return TX_CONTINUE;
58d4185e 248
ece8eddd 249 if (unlikely(tx->local->sta_sw_scanning) &&
e2ebc74d
JB
250 ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
251 (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
9ae54c84 252 return TX_DROP;
e2ebc74d 253
33b64eb2
LCC
254 if (tx->sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT)
255 return TX_CONTINUE;
256
5cf121c3 257 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
9ae54c84 258 return TX_CONTINUE;
e2ebc74d 259
07346f81 260 sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
e2ebc74d 261
5cf121c3 262 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
e2ebc74d 263 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
51fb61e7 264 tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
e2ebc74d
JB
265 (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
266#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
0795af57 267 DECLARE_MAC_BUF(mac);
e2ebc74d 268 printk(KERN_DEBUG "%s: dropped data frame to not "
0795af57
JP
269 "associated station %s\n",
270 tx->dev->name, print_mac(mac, hdr->addr1));
e2ebc74d
JB
271#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
272 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
9ae54c84 273 return TX_DROP;
e2ebc74d
JB
274 }
275 } else {
276 if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
277 tx->local->num_sta == 0 &&
51fb61e7 278 tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS)) {
e2ebc74d
JB
279 /*
280 * No associated STAs - no need to send multicast
281 * frames.
282 */
9ae54c84 283 return TX_DROP;
e2ebc74d 284 }
9ae54c84 285 return TX_CONTINUE;
e2ebc74d
JB
286 }
287
9ae54c84 288 return TX_CONTINUE;
e2ebc74d
JB
289}
290
9ae54c84 291static ieee80211_tx_result
5cf121c3 292ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
e2ebc74d
JB
293{
294 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
295
296 if (ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
297 ieee80211_include_sequence(tx->sdata, hdr);
298
9ae54c84 299 return TX_CONTINUE;
e2ebc74d
JB
300}
301
302/* This function is called whenever the AP is about to exceed the maximum limit
303 * of buffered frames for power saving STAs. This situation should not really
304 * happen often during normal operation, so dropping the oldest buffered packet
305 * from each queue should be OK to make some room for new frames. */
306static void purge_old_ps_buffers(struct ieee80211_local *local)
307{
308 int total = 0, purged = 0;
309 struct sk_buff *skb;
310 struct ieee80211_sub_if_data *sdata;
311 struct sta_info *sta;
312
79010420
JB
313 /*
314 * virtual interfaces are protected by RCU
315 */
316 rcu_read_lock();
317
318 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
e2ebc74d
JB
319 struct ieee80211_if_ap *ap;
320 if (sdata->dev == local->mdev ||
51fb61e7 321 sdata->vif.type != IEEE80211_IF_TYPE_AP)
e2ebc74d
JB
322 continue;
323 ap = &sdata->u.ap;
324 skb = skb_dequeue(&ap->ps_bc_buf);
325 if (skb) {
326 purged++;
327 dev_kfree_skb(skb);
328 }
329 total += skb_queue_len(&ap->ps_bc_buf);
330 }
e2ebc74d 331
d0709a65 332 list_for_each_entry_rcu(sta, &local->sta_list, list) {
e2ebc74d
JB
333 skb = skb_dequeue(&sta->ps_tx_buf);
334 if (skb) {
335 purged++;
336 dev_kfree_skb(skb);
337 }
338 total += skb_queue_len(&sta->ps_tx_buf);
339 }
d0709a65
JB
340
341 rcu_read_unlock();
e2ebc74d
JB
342
343 local->total_ps_buffered = total;
344 printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
dd1cd4c6 345 wiphy_name(local->hw.wiphy), purged);
e2ebc74d
JB
346}
347
9ae54c84 348static ieee80211_tx_result
5cf121c3 349ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
e2ebc74d 350{
7d54d0dd
JB
351 /*
352 * broadcast/multicast frame
353 *
354 * If any of the associated stations is in power save mode,
355 * the frame is buffered to be sent after DTIM beacon frame.
356 * This is done either by the hardware or us.
357 */
358
359 /* not AP/IBSS or ordered frame */
360 if (!tx->sdata->bss || (tx->fc & IEEE80211_FCTL_ORDER))
9ae54c84 361 return TX_CONTINUE;
7d54d0dd
JB
362
363 /* no stations in PS mode */
364 if (!atomic_read(&tx->sdata->bss->num_sta_ps))
9ae54c84 365 return TX_CONTINUE;
7d54d0dd
JB
366
367 /* buffered in mac80211 */
368 if (tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) {
e2ebc74d
JB
369 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
370 purge_old_ps_buffers(tx->local);
371 if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
372 AP_MAX_BC_BUFFER) {
373 if (net_ratelimit()) {
374 printk(KERN_DEBUG "%s: BC TX buffer full - "
375 "dropping the oldest frame\n",
376 tx->dev->name);
377 }
378 dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
379 } else
380 tx->local->total_ps_buffered++;
381 skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
9ae54c84 382 return TX_QUEUED;
e2ebc74d
JB
383 }
384
7d54d0dd 385 /* buffered in hardware */
5cf121c3 386 tx->control->flags |= IEEE80211_TXCTL_SEND_AFTER_DTIM;
7d54d0dd 387
9ae54c84 388 return TX_CONTINUE;
e2ebc74d
JB
389}
390
9ae54c84 391static ieee80211_tx_result
5cf121c3 392ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
e2ebc74d
JB
393{
394 struct sta_info *sta = tx->sta;
07346f81 395 u32 staflags;
0795af57 396 DECLARE_MAC_BUF(mac);
e2ebc74d
JB
397
398 if (unlikely(!sta ||
399 ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
400 (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
9ae54c84 401 return TX_CONTINUE;
e2ebc74d 402
07346f81
JB
403 staflags = get_sta_flags(sta);
404
405 if (unlikely((staflags & WLAN_STA_PS) &&
406 !(staflags & WLAN_STA_PSPOLL))) {
e2ebc74d
JB
407 struct ieee80211_tx_packet_data *pkt_data;
408#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0795af57 409 printk(KERN_DEBUG "STA %s aid %d: PS buffer (entries "
e2ebc74d 410 "before %d)\n",
0795af57 411 print_mac(mac, sta->addr), sta->aid,
e2ebc74d
JB
412 skb_queue_len(&sta->ps_tx_buf));
413#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
e2ebc74d
JB
414 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
415 purge_old_ps_buffers(tx->local);
416 if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
417 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
418 if (net_ratelimit()) {
0795af57 419 printk(KERN_DEBUG "%s: STA %s TX "
e2ebc74d 420 "buffer full - dropping oldest frame\n",
0795af57 421 tx->dev->name, print_mac(mac, sta->addr));
e2ebc74d
JB
422 }
423 dev_kfree_skb(old);
424 } else
425 tx->local->total_ps_buffered++;
004c872e 426
e2ebc74d 427 /* Queue frame to be sent after STA sends an PS Poll frame */
004c872e
JB
428 if (skb_queue_empty(&sta->ps_tx_buf))
429 sta_info_set_tim_bit(sta);
430
e2ebc74d
JB
431 pkt_data = (struct ieee80211_tx_packet_data *)tx->skb->cb;
432 pkt_data->jiffies = jiffies;
433 skb_queue_tail(&sta->ps_tx_buf, tx->skb);
9ae54c84 434 return TX_QUEUED;
e2ebc74d
JB
435 }
436#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
07346f81 437 else if (unlikely(test_sta_flags(sta, WLAN_STA_PS))) {
0795af57 438 printk(KERN_DEBUG "%s: STA %s in PS mode, but pspoll "
e2ebc74d 439 "set -> send frame\n", tx->dev->name,
0795af57 440 print_mac(mac, sta->addr));
e2ebc74d
JB
441 }
442#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
07346f81 443 clear_sta_flags(sta, WLAN_STA_PSPOLL);
e2ebc74d 444
9ae54c84 445 return TX_CONTINUE;
e2ebc74d
JB
446}
447
9ae54c84 448static ieee80211_tx_result
5cf121c3 449ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
e2ebc74d 450{
5cf121c3 451 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
9ae54c84 452 return TX_CONTINUE;
e2ebc74d 453
5cf121c3 454 if (tx->flags & IEEE80211_TX_UNICAST)
e2ebc74d
JB
455 return ieee80211_tx_h_unicast_ps_buf(tx);
456 else
457 return ieee80211_tx_h_multicast_ps_buf(tx);
458}
459
9ae54c84 460static ieee80211_tx_result
5cf121c3 461ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
e2ebc74d 462{
d4e46a3d 463 struct ieee80211_key *key;
176e4f84 464 u16 fc = tx->fc;
d4e46a3d 465
5cf121c3 466 if (unlikely(tx->control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
e2ebc74d 467 tx->key = NULL;
d4e46a3d
JB
468 else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
469 tx->key = key;
470 else if ((key = rcu_dereference(tx->sdata->default_key)))
471 tx->key = key;
e2ebc74d 472 else if (tx->sdata->drop_unencrypted &&
5cf121c3
JB
473 !(tx->control->flags & IEEE80211_TXCTL_EAPOL_FRAME) &&
474 !(tx->flags & IEEE80211_TX_INJECTED)) {
e2ebc74d 475 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
9ae54c84 476 return TX_DROP;
176e4f84 477 } else
e2ebc74d
JB
478 tx->key = NULL;
479
480 if (tx->key) {
176e4f84
JB
481 u16 ftype, stype;
482
e2ebc74d 483 tx->key->tx_rx_count++;
011bfcc4 484 /* TODO: add threshold stuff again */
176e4f84
JB
485
486 switch (tx->key->conf.alg) {
487 case ALG_WEP:
488 ftype = fc & IEEE80211_FCTL_FTYPE;
489 stype = fc & IEEE80211_FCTL_STYPE;
490
491 if (ftype == IEEE80211_FTYPE_MGMT &&
492 stype == IEEE80211_STYPE_AUTH)
493 break;
494 case ALG_TKIP:
495 case ALG_CCMP:
496 if (!WLAN_FC_DATA_PRESENT(fc))
497 tx->key = NULL;
498 break;
499 }
e2ebc74d
JB
500 }
501
176e4f84 502 if (!tx->key || !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
5cf121c3 503 tx->control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
176e4f84 504
9ae54c84 505 return TX_CONTINUE;
e2ebc74d
JB
506}
507
9ae54c84 508static ieee80211_tx_result
5cf121c3 509ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
e2ebc74d
JB
510{
511 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
512 size_t hdrlen, per_fragm, num_fragm, payload_len, left;
513 struct sk_buff **frags, *first, *frag;
514 int i;
515 u16 seq;
516 u8 *pos;
517 int frag_threshold = tx->local->fragmentation_threshold;
518
5cf121c3 519 if (!(tx->flags & IEEE80211_TX_FRAGMENTED))
9ae54c84 520 return TX_CONTINUE;
e2ebc74d
JB
521
522 first = tx->skb;
523
524 hdrlen = ieee80211_get_hdrlen(tx->fc);
525 payload_len = first->len - hdrlen;
526 per_fragm = frag_threshold - hdrlen - FCS_LEN;
172589cc 527 num_fragm = DIV_ROUND_UP(payload_len, per_fragm);
e2ebc74d
JB
528
529 frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
530 if (!frags)
531 goto fail;
532
533 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
534 seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
535 pos = first->data + hdrlen + per_fragm;
536 left = payload_len - per_fragm;
537 for (i = 0; i < num_fragm - 1; i++) {
538 struct ieee80211_hdr *fhdr;
539 size_t copylen;
540
541 if (left <= 0)
542 goto fail;
543
544 /* reserve enough extra head and tail room for possible
545 * encryption */
546 frag = frags[i] =
547 dev_alloc_skb(tx->local->tx_headroom +
548 frag_threshold +
549 IEEE80211_ENCRYPT_HEADROOM +
550 IEEE80211_ENCRYPT_TAILROOM);
551 if (!frag)
552 goto fail;
553 /* Make sure that all fragments use the same priority so
554 * that they end up using the same TX queue */
555 frag->priority = first->priority;
556 skb_reserve(frag, tx->local->tx_headroom +
557 IEEE80211_ENCRYPT_HEADROOM);
558 fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
559 memcpy(fhdr, first->data, hdrlen);
560 if (i == num_fragm - 2)
561 fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
562 fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
563 copylen = left > per_fragm ? per_fragm : left;
564 memcpy(skb_put(frag, copylen), pos, copylen);
565
566 pos += copylen;
567 left -= copylen;
568 }
569 skb_trim(first, hdrlen + per_fragm);
570
5cf121c3
JB
571 tx->num_extra_frag = num_fragm - 1;
572 tx->extra_frag = frags;
e2ebc74d 573
9ae54c84 574 return TX_CONTINUE;
e2ebc74d
JB
575
576 fail:
577 printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
578 if (frags) {
579 for (i = 0; i < num_fragm - 1; i++)
580 if (frags[i])
581 dev_kfree_skb(frags[i]);
582 kfree(frags);
583 }
584 I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
9ae54c84 585 return TX_DROP;
e2ebc74d
JB
586}
587
9ae54c84 588static ieee80211_tx_result
5cf121c3 589ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
e2ebc74d 590{
6a22a59d 591 if (!tx->key)
9ae54c84 592 return TX_CONTINUE;
e2ebc74d 593
6a22a59d
JB
594 switch (tx->key->conf.alg) {
595 case ALG_WEP:
596 return ieee80211_crypto_wep_encrypt(tx);
597 case ALG_TKIP:
598 return ieee80211_crypto_tkip_encrypt(tx);
599 case ALG_CCMP:
600 return ieee80211_crypto_ccmp_encrypt(tx);
e2ebc74d
JB
601 }
602
6a22a59d
JB
603 /* not reached */
604 WARN_ON(1);
9ae54c84 605 return TX_DROP;
e2ebc74d
JB
606}
607
9ae54c84 608static ieee80211_tx_result
5cf121c3 609ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
e2ebc74d 610{
1abbe498 611 struct rate_selection rsel;
8318d78a
JB
612 struct ieee80211_supported_band *sband;
613
2e92e6f2 614 sband = tx->local->hw.wiphy->bands[tx->channel->band];
e2ebc74d 615
2e92e6f2 616 if (likely(tx->rate_idx < 0)) {
8318d78a 617 rate_control_get_rate(tx->dev, sband, tx->skb, &rsel);
2e92e6f2
JB
618 tx->rate_idx = rsel.rate_idx;
619 if (unlikely(rsel.probe_idx >= 0)) {
5cf121c3 620 tx->control->flags |=
58d4185e 621 IEEE80211_TXCTL_RATE_CTRL_PROBE;
5cf121c3 622 tx->flags |= IEEE80211_TX_PROBE_LAST_FRAG;
2e92e6f2
JB
623 tx->control->alt_retry_rate_idx = tx->rate_idx;
624 tx->rate_idx = rsel.probe_idx;
58d4185e 625 } else
2e92e6f2 626 tx->control->alt_retry_rate_idx = -1;
58d4185e 627
2e92e6f2 628 if (unlikely(tx->rate_idx < 0))
9ae54c84 629 return TX_DROP;
58d4185e 630 } else
2e92e6f2 631 tx->control->alt_retry_rate_idx = -1;
58d4185e 632
8318d78a 633 if (tx->sdata->bss_conf.use_cts_prot &&
2e92e6f2
JB
634 (tx->flags & IEEE80211_TX_FRAGMENTED) && (rsel.nonerp_idx >= 0)) {
635 tx->last_frag_rate_idx = tx->rate_idx;
636 if (rsel.probe_idx >= 0)
5cf121c3 637 tx->flags &= ~IEEE80211_TX_PROBE_LAST_FRAG;
badffb72 638 else
5cf121c3 639 tx->flags |= IEEE80211_TX_PROBE_LAST_FRAG;
2e92e6f2
JB
640 tx->rate_idx = rsel.nonerp_idx;
641 tx->control->tx_rate_idx = rsel.nonerp_idx;
5cf121c3 642 tx->control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
e2ebc74d 643 } else {
2e92e6f2
JB
644 tx->last_frag_rate_idx = tx->rate_idx;
645 tx->control->tx_rate_idx = tx->rate_idx;
e2ebc74d 646 }
2e92e6f2 647 tx->control->tx_rate_idx = tx->rate_idx;
e2ebc74d 648
9ae54c84 649 return TX_CONTINUE;
e2ebc74d
JB
650}
651
9ae54c84 652static ieee80211_tx_result
5cf121c3 653ieee80211_tx_h_misc(struct ieee80211_tx_data *tx)
e2ebc74d
JB
654{
655 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
7e9ed188 656 u16 fc = le16_to_cpu(hdr->frame_control);
e2ebc74d 657 u16 dur;
5cf121c3 658 struct ieee80211_tx_control *control = tx->control;
2e92e6f2
JB
659 struct ieee80211_supported_band *sband;
660
661 sband = tx->local->hw.wiphy->bands[tx->channel->band];
e2ebc74d 662
58d4185e
JB
663 if (!control->retry_limit) {
664 if (!is_multicast_ether_addr(hdr->addr1)) {
665 if (tx->skb->len + FCS_LEN > tx->local->rts_threshold
666 && tx->local->rts_threshold <
667 IEEE80211_MAX_RTS_THRESHOLD) {
668 control->flags |=
669 IEEE80211_TXCTL_USE_RTS_CTS;
670 control->flags |=
671 IEEE80211_TXCTL_LONG_RETRY_LIMIT;
672 control->retry_limit =
673 tx->local->long_retry_limit;
674 } else {
675 control->retry_limit =
676 tx->local->short_retry_limit;
677 }
e2ebc74d 678 } else {
58d4185e 679 control->retry_limit = 1;
e2ebc74d 680 }
e2ebc74d
JB
681 }
682
5cf121c3 683 if (tx->flags & IEEE80211_TX_FRAGMENTED) {
e2ebc74d
JB
684 /* Do not use multiple retry rates when sending fragmented
685 * frames.
686 * TODO: The last fragment could still use multiple retry
687 * rates. */
2e92e6f2 688 control->alt_retry_rate_idx = -1;
e2ebc74d
JB
689 }
690
691 /* Use CTS protection for unicast frames sent using extended rates if
692 * there are associated non-ERP stations and RTS/CTS is not configured
693 * for the frame. */
8318d78a 694 if ((tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE) &&
2e92e6f2 695 (sband->bitrates[tx->rate_idx].flags & IEEE80211_RATE_ERP_G) &&
5cf121c3 696 (tx->flags & IEEE80211_TX_UNICAST) &&
471b3efd 697 tx->sdata->bss_conf.use_cts_prot &&
e2ebc74d
JB
698 !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
699 control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
700
7e9ed188
DD
701 /* Transmit data frames using short preambles if the driver supports
702 * short preambles at the selected rate and short preambles are
703 * available on the network at the current point in time. */
704 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
2e92e6f2 705 (sband->bitrates[tx->rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
471b3efd 706 tx->sdata->bss_conf.use_short_preamble &&
07346f81 707 (!tx->sta || test_sta_flags(tx->sta, WLAN_STA_SHORT_PREAMBLE))) {
5cf121c3 708 tx->control->flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
7e9ed188
DD
709 }
710
e2ebc74d
JB
711 /* Setup duration field for the first fragment of the frame. Duration
712 * for remaining fragments will be updated when they are being sent
713 * to low-level driver in ieee80211_tx(). */
714 dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
5cf121c3
JB
715 (tx->flags & IEEE80211_TX_FRAGMENTED) ?
716 tx->extra_frag[0]->len : 0);
e2ebc74d
JB
717 hdr->duration_id = cpu_to_le16(dur);
718
719 if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
720 (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
8318d78a 721 struct ieee80211_supported_band *sband;
2e92e6f2
JB
722 struct ieee80211_rate *rate;
723 s8 baserate = -1;
8318d78a
JB
724 int idx;
725
2e92e6f2 726 sband = tx->local->hw.wiphy->bands[tx->channel->band];
e2ebc74d
JB
727
728 /* Do not use multiple retry rates when using RTS/CTS */
2e92e6f2 729 control->alt_retry_rate_idx = -1;
e2ebc74d
JB
730
731 /* Use min(data rate, max base rate) as CTS/RTS rate */
2e92e6f2 732 rate = &sband->bitrates[tx->rate_idx];
8318d78a
JB
733
734 for (idx = 0; idx < sband->n_bitrates; idx++) {
735 if (sband->bitrates[idx].bitrate > rate->bitrate)
736 continue;
737 if (tx->sdata->basic_rates & BIT(idx) &&
2e92e6f2
JB
738 (baserate < 0 ||
739 (sband->bitrates[baserate].bitrate
740 < sband->bitrates[idx].bitrate)))
741 baserate = idx;
8318d78a 742 }
e2ebc74d 743
2e92e6f2
JB
744 if (baserate >= 0)
745 control->rts_cts_rate_idx = baserate;
8318d78a 746 else
2e92e6f2 747 control->rts_cts_rate_idx = 0;
e2ebc74d
JB
748 }
749
750 if (tx->sta) {
fff77109 751 control->aid = tx->sta->aid;
e2ebc74d
JB
752 tx->sta->tx_packets++;
753 tx->sta->tx_fragments++;
754 tx->sta->tx_bytes += tx->skb->len;
5cf121c3 755 if (tx->extra_frag) {
e2ebc74d 756 int i;
5cf121c3
JB
757 tx->sta->tx_fragments += tx->num_extra_frag;
758 for (i = 0; i < tx->num_extra_frag; i++) {
e2ebc74d 759 tx->sta->tx_bytes +=
5cf121c3 760 tx->extra_frag[i]->len;
e2ebc74d
JB
761 }
762 }
763 }
764
9ae54c84 765 return TX_CONTINUE;
e2ebc74d
JB
766}
767
9ae54c84 768static ieee80211_tx_result
5cf121c3 769ieee80211_tx_h_load_stats(struct ieee80211_tx_data *tx)
e2ebc74d
JB
770{
771 struct ieee80211_local *local = tx->local;
e2ebc74d
JB
772 struct sk_buff *skb = tx->skb;
773 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
774 u32 load = 0, hdrtime;
2e92e6f2
JB
775 struct ieee80211_rate *rate;
776 struct ieee80211_supported_band *sband;
777
778 sband = tx->local->hw.wiphy->bands[tx->channel->band];
779 rate = &sband->bitrates[tx->rate_idx];
e2ebc74d
JB
780
781 /* TODO: this could be part of tx_status handling, so that the number
782 * of retries would be known; TX rate should in that case be stored
783 * somewhere with the packet */
784
785 /* Estimate total channel use caused by this frame */
786
787 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
788 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
789
5cf121c3
JB
790 if (tx->channel->band == IEEE80211_BAND_5GHZ ||
791 (tx->channel->band == IEEE80211_BAND_2GHZ &&
8318d78a 792 rate->flags & IEEE80211_RATE_ERP_G))
e2ebc74d
JB
793 hdrtime = CHAN_UTIL_HDR_SHORT;
794 else
795 hdrtime = CHAN_UTIL_HDR_LONG;
796
797 load = hdrtime;
798 if (!is_multicast_ether_addr(hdr->addr1))
799 load += hdrtime;
800
5cf121c3 801 if (tx->control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
e2ebc74d 802 load += 2 * hdrtime;
5cf121c3 803 else if (tx->control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
e2ebc74d
JB
804 load += hdrtime;
805
8318d78a
JB
806 /* TODO: optimise again */
807 load += skb->len * CHAN_UTIL_RATE_LCM / rate->bitrate;
e2ebc74d 808
5cf121c3 809 if (tx->extra_frag) {
e2ebc74d 810 int i;
5cf121c3 811 for (i = 0; i < tx->num_extra_frag; i++) {
e2ebc74d 812 load += 2 * hdrtime;
5cf121c3 813 load += tx->extra_frag[i]->len *
2e92e6f2 814 rate->bitrate;
e2ebc74d
JB
815 }
816 }
817
818 /* Divide channel_use by 8 to avoid wrapping around the counter */
819 load >>= CHAN_UTIL_SHIFT;
820 local->channel_use_raw += load;
821 if (tx->sta)
822 tx->sta->channel_use_raw += load;
823 tx->sdata->channel_use_raw += load;
824
9ae54c84 825 return TX_CONTINUE;
e2ebc74d
JB
826}
827
e2ebc74d 828
5cf121c3 829typedef ieee80211_tx_result (*ieee80211_tx_handler)(struct ieee80211_tx_data *);
58905290 830static ieee80211_tx_handler ieee80211_tx_handlers[] =
e2ebc74d
JB
831{
832 ieee80211_tx_h_check_assoc,
833 ieee80211_tx_h_sequence,
834 ieee80211_tx_h_ps_buf,
835 ieee80211_tx_h_select_key,
836 ieee80211_tx_h_michael_mic_add,
837 ieee80211_tx_h_fragment,
6a22a59d 838 ieee80211_tx_h_encrypt,
e2ebc74d
JB
839 ieee80211_tx_h_rate_ctrl,
840 ieee80211_tx_h_misc,
841 ieee80211_tx_h_load_stats,
842 NULL
843};
844
845/* actual transmit path */
846
847/*
848 * deal with packet injection down monitor interface
849 * with Radiotap Header -- only called for monitor mode interface
850 */
9ae54c84 851static ieee80211_tx_result
5cf121c3 852__ieee80211_parse_tx_radiotap(struct ieee80211_tx_data *tx,
58d4185e 853 struct sk_buff *skb)
e2ebc74d
JB
854{
855 /*
856 * this is the moment to interpret and discard the radiotap header that
857 * must be at the start of the packet injected in Monitor mode
858 *
859 * Need to take some care with endian-ness since radiotap
860 * args are little-endian
861 */
862
863 struct ieee80211_radiotap_iterator iterator;
864 struct ieee80211_radiotap_header *rthdr =
865 (struct ieee80211_radiotap_header *) skb->data;
8318d78a 866 struct ieee80211_supported_band *sband;
e2ebc74d 867 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
5cf121c3 868 struct ieee80211_tx_control *control = tx->control;
e2ebc74d 869
2e92e6f2 870 sband = tx->local->hw.wiphy->bands[tx->channel->band];
8318d78a 871
58d4185e 872 control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
5cf121c3
JB
873 tx->flags |= IEEE80211_TX_INJECTED;
874 tx->flags &= ~IEEE80211_TX_FRAGMENTED;
e2ebc74d
JB
875
876 /*
877 * for every radiotap entry that is present
878 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
879 * entries present, or -EINVAL on error)
880 */
881
882 while (!ret) {
883 int i, target_rate;
884
885 ret = ieee80211_radiotap_iterator_next(&iterator);
886
887 if (ret)
888 continue;
889
890 /* see if this argument is something we can use */
891 switch (iterator.this_arg_index) {
892 /*
893 * You must take care when dereferencing iterator.this_arg
894 * for multibyte types... the pointer is not aligned. Use
895 * get_unaligned((type *)iterator.this_arg) to dereference
896 * iterator.this_arg for type "type" safely on all arches.
897 */
898 case IEEE80211_RADIOTAP_RATE:
899 /*
900 * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
901 * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
902 */
903 target_rate = (*iterator.this_arg) * 5;
8318d78a
JB
904 for (i = 0; i < sband->n_bitrates; i++) {
905 struct ieee80211_rate *r;
e2ebc74d 906
8318d78a
JB
907 r = &sband->bitrates[i];
908
909 if (r->bitrate == target_rate) {
2e92e6f2 910 tx->rate_idx = i;
58d4185e
JB
911 break;
912 }
e2ebc74d
JB
913 }
914 break;
915
916 case IEEE80211_RADIOTAP_ANTENNA:
917 /*
918 * radiotap uses 0 for 1st ant, mac80211 is 1 for
919 * 1st ant
920 */
921 control->antenna_sel_tx = (*iterator.this_arg) + 1;
922 break;
923
8318d78a 924#if 0
e2ebc74d
JB
925 case IEEE80211_RADIOTAP_DBM_TX_POWER:
926 control->power_level = *iterator.this_arg;
927 break;
8318d78a 928#endif
e2ebc74d
JB
929
930 case IEEE80211_RADIOTAP_FLAGS:
931 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
932 /*
933 * this indicates that the skb we have been
934 * handed has the 32-bit FCS CRC at the end...
935 * we should react to that by snipping it off
936 * because it will be recomputed and added
937 * on transmission
938 */
939 if (skb->len < (iterator.max_length + FCS_LEN))
9ae54c84 940 return TX_DROP;
e2ebc74d
JB
941
942 skb_trim(skb, skb->len - FCS_LEN);
943 }
58d4185e
JB
944 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
945 control->flags &=
946 ~IEEE80211_TXCTL_DO_NOT_ENCRYPT;
947 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
5cf121c3 948 tx->flags |= IEEE80211_TX_FRAGMENTED;
e2ebc74d
JB
949 break;
950
58d4185e
JB
951 /*
952 * Please update the file
953 * Documentation/networking/mac80211-injection.txt
954 * when parsing new fields here.
955 */
956
e2ebc74d
JB
957 default:
958 break;
959 }
960 }
961
962 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
9ae54c84 963 return TX_DROP;
e2ebc74d
JB
964
965 /*
966 * remove the radiotap header
967 * iterator->max_length was sanity-checked against
968 * skb->len by iterator init
969 */
970 skb_pull(skb, iterator.max_length);
971
9ae54c84 972 return TX_CONTINUE;
e2ebc74d
JB
973}
974
58d4185e
JB
975/*
976 * initialises @tx
977 */
9ae54c84 978static ieee80211_tx_result
5cf121c3 979__ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
e2ebc74d
JB
980 struct sk_buff *skb,
981 struct net_device *dev,
982 struct ieee80211_tx_control *control)
983{
984 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
58d4185e 985 struct ieee80211_hdr *hdr;
e2ebc74d 986 struct ieee80211_sub_if_data *sdata;
e2ebc74d
JB
987
988 int hdrlen;
989
990 memset(tx, 0, sizeof(*tx));
991 tx->skb = skb;
992 tx->dev = dev; /* use original interface */
993 tx->local = local;
994 tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5cf121c3 995 tx->control = control;
e2ebc74d 996 /*
58d4185e
JB
997 * Set this flag (used below to indicate "automatic fragmentation"),
998 * it will be cleared/left by radiotap as desired.
e2ebc74d 999 */
5cf121c3 1000 tx->flags |= IEEE80211_TX_FRAGMENTED;
e2ebc74d
JB
1001
1002 /* process and remove the injection radiotap header */
1003 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
51fb61e7 1004 if (unlikely(sdata->vif.type == IEEE80211_IF_TYPE_MNTR)) {
9ae54c84
JB
1005 if (__ieee80211_parse_tx_radiotap(tx, skb) == TX_DROP)
1006 return TX_DROP;
58d4185e 1007
e2ebc74d 1008 /*
58d4185e
JB
1009 * __ieee80211_parse_tx_radiotap has now removed
1010 * the radiotap header that was present and pre-filled
1011 * 'tx' with tx control information.
e2ebc74d 1012 */
e2ebc74d
JB
1013 }
1014
58d4185e
JB
1015 hdr = (struct ieee80211_hdr *) skb->data;
1016
24338793 1017 tx->sta = sta_info_get(local, hdr->addr1);
1018 tx->fc = le16_to_cpu(hdr->frame_control);
58d4185e 1019
badffb72 1020 if (is_multicast_ether_addr(hdr->addr1)) {
5cf121c3 1021 tx->flags &= ~IEEE80211_TX_UNICAST;
e2ebc74d 1022 control->flags |= IEEE80211_TXCTL_NO_ACK;
badffb72 1023 } else {
5cf121c3 1024 tx->flags |= IEEE80211_TX_UNICAST;
e2ebc74d 1025 control->flags &= ~IEEE80211_TXCTL_NO_ACK;
badffb72 1026 }
58d4185e 1027
5cf121c3
JB
1028 if (tx->flags & IEEE80211_TX_FRAGMENTED) {
1029 if ((tx->flags & IEEE80211_TX_UNICAST) &&
58d4185e
JB
1030 skb->len + FCS_LEN > local->fragmentation_threshold &&
1031 !local->ops->set_frag_threshold)
5cf121c3 1032 tx->flags |= IEEE80211_TX_FRAGMENTED;
58d4185e 1033 else
5cf121c3 1034 tx->flags &= ~IEEE80211_TX_FRAGMENTED;
58d4185e
JB
1035 }
1036
e2ebc74d 1037 if (!tx->sta)
d46e144b 1038 control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
07346f81 1039 else if (test_and_clear_sta_flags(tx->sta, WLAN_STA_CLEAR_PS_FILT))
d46e144b 1040 control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
58d4185e 1041
e2ebc74d
JB
1042 hdrlen = ieee80211_get_hdrlen(tx->fc);
1043 if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
1044 u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
1045 tx->ethertype = (pos[0] << 8) | pos[1];
1046 }
1047 control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
1048
9ae54c84 1049 return TX_CONTINUE;
e2ebc74d
JB
1050}
1051
32bfd35d 1052/*
58d4185e
JB
1053 * NB: @tx is uninitialised when passed in here
1054 */
5cf121c3 1055static int ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
58d4185e
JB
1056 struct sk_buff *skb,
1057 struct net_device *mdev,
1058 struct ieee80211_tx_control *control)
e2ebc74d
JB
1059{
1060 struct ieee80211_tx_packet_data *pkt_data;
1061 struct net_device *dev;
1062
1063 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
881d966b 1064 dev = dev_get_by_index(&init_net, pkt_data->ifindex);
e2ebc74d
JB
1065 if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
1066 dev_put(dev);
1067 dev = NULL;
1068 }
1069 if (unlikely(!dev))
1070 return -ENODEV;
58d4185e 1071 /* initialises tx with control */
e2ebc74d 1072 __ieee80211_tx_prepare(tx, skb, dev, control);
32bfd35d 1073 dev_put(dev);
e2ebc74d
JB
1074 return 0;
1075}
1076
1077static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
5cf121c3 1078 struct ieee80211_tx_data *tx)
e2ebc74d 1079{
5cf121c3 1080 struct ieee80211_tx_control *control = tx->control;
e2ebc74d
JB
1081 int ret, i;
1082
1083 if (!ieee80211_qdisc_installed(local->mdev) &&
1084 __ieee80211_queue_stopped(local, 0)) {
1085 netif_stop_queue(local->mdev);
1086 return IEEE80211_TX_AGAIN;
1087 }
1088 if (skb) {
dd1cd4c6
JB
1089 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
1090 "TX to low-level driver", skb);
e2ebc74d
JB
1091 ret = local->ops->tx(local_to_hw(local), skb, control);
1092 if (ret)
1093 return IEEE80211_TX_AGAIN;
1094 local->mdev->trans_start = jiffies;
1095 ieee80211_led_tx(local, 1);
1096 }
5cf121c3 1097 if (tx->extra_frag) {
e2ebc74d
JB
1098 control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
1099 IEEE80211_TXCTL_USE_CTS_PROTECT |
d46e144b 1100 IEEE80211_TXCTL_CLEAR_PS_FILT |
e2ebc74d 1101 IEEE80211_TXCTL_FIRST_FRAGMENT);
5cf121c3
JB
1102 for (i = 0; i < tx->num_extra_frag; i++) {
1103 if (!tx->extra_frag[i])
e2ebc74d
JB
1104 continue;
1105 if (__ieee80211_queue_stopped(local, control->queue))
1106 return IEEE80211_TX_FRAG_AGAIN;
5cf121c3 1107 if (i == tx->num_extra_frag) {
2e92e6f2 1108 control->tx_rate_idx = tx->last_frag_rate_idx;
8318d78a 1109
5cf121c3 1110 if (tx->flags & IEEE80211_TX_PROBE_LAST_FRAG)
e2ebc74d
JB
1111 control->flags |=
1112 IEEE80211_TXCTL_RATE_CTRL_PROBE;
1113 else
1114 control->flags &=
1115 ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
1116 }
1117
dd1cd4c6 1118 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
e2ebc74d 1119 "TX to low-level driver",
5cf121c3 1120 tx->extra_frag[i]);
e2ebc74d 1121 ret = local->ops->tx(local_to_hw(local),
5cf121c3 1122 tx->extra_frag[i],
e2ebc74d
JB
1123 control);
1124 if (ret)
1125 return IEEE80211_TX_FRAG_AGAIN;
1126 local->mdev->trans_start = jiffies;
1127 ieee80211_led_tx(local, 1);
5cf121c3 1128 tx->extra_frag[i] = NULL;
e2ebc74d 1129 }
5cf121c3
JB
1130 kfree(tx->extra_frag);
1131 tx->extra_frag = NULL;
e2ebc74d
JB
1132 }
1133 return IEEE80211_TX_OK;
1134}
1135
1136static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
f9d540ee 1137 struct ieee80211_tx_control *control)
e2ebc74d
JB
1138{
1139 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1140 struct sta_info *sta;
1141 ieee80211_tx_handler *handler;
5cf121c3 1142 struct ieee80211_tx_data tx;
9ae54c84 1143 ieee80211_tx_result res = TX_DROP, res_prepare;
e2ebc74d
JB
1144 int ret, i;
1145
1146 WARN_ON(__ieee80211_queue_pending(local, control->queue));
1147
1148 if (unlikely(skb->len < 10)) {
1149 dev_kfree_skb(skb);
1150 return 0;
1151 }
1152
d0709a65
JB
1153 rcu_read_lock();
1154
58d4185e 1155 /* initialises tx */
e2ebc74d
JB
1156 res_prepare = __ieee80211_tx_prepare(&tx, skb, dev, control);
1157
9ae54c84 1158 if (res_prepare == TX_DROP) {
e2ebc74d 1159 dev_kfree_skb(skb);
d0709a65 1160 rcu_read_unlock();
e2ebc74d
JB
1161 return 0;
1162 }
1163
1164 sta = tx.sta;
5cf121c3 1165 tx.channel = local->hw.conf.channel;
2e92e6f2 1166 control->band = tx.channel->band;
e2ebc74d 1167
58905290 1168 for (handler = ieee80211_tx_handlers; *handler != NULL;
58d4185e
JB
1169 handler++) {
1170 res = (*handler)(&tx);
9ae54c84 1171 if (res != TX_CONTINUE)
58d4185e 1172 break;
e2ebc74d
JB
1173 }
1174
1175 skb = tx.skb; /* handlers are allowed to change skb */
1176
9ae54c84 1177 if (unlikely(res == TX_DROP)) {
e2ebc74d
JB
1178 I802_DEBUG_INC(local->tx_handlers_drop);
1179 goto drop;
1180 }
1181
9ae54c84 1182 if (unlikely(res == TX_QUEUED)) {
e2ebc74d 1183 I802_DEBUG_INC(local->tx_handlers_queued);
d4e46a3d 1184 rcu_read_unlock();
e2ebc74d
JB
1185 return 0;
1186 }
1187
5cf121c3
JB
1188 if (tx.extra_frag) {
1189 for (i = 0; i < tx.num_extra_frag; i++) {
e2ebc74d
JB
1190 int next_len, dur;
1191 struct ieee80211_hdr *hdr =
1192 (struct ieee80211_hdr *)
5cf121c3 1193 tx.extra_frag[i]->data;
e2ebc74d 1194
5cf121c3
JB
1195 if (i + 1 < tx.num_extra_frag) {
1196 next_len = tx.extra_frag[i + 1]->len;
e2ebc74d
JB
1197 } else {
1198 next_len = 0;
2e92e6f2 1199 tx.rate_idx = tx.last_frag_rate_idx;
e2ebc74d
JB
1200 }
1201 dur = ieee80211_duration(&tx, 0, next_len);
1202 hdr->duration_id = cpu_to_le16(dur);
1203 }
1204 }
1205
1206retry:
1207 ret = __ieee80211_tx(local, skb, &tx);
1208 if (ret) {
1209 struct ieee80211_tx_stored_packet *store =
1210 &local->pending_packet[control->queue];
1211
1212 if (ret == IEEE80211_TX_FRAG_AGAIN)
1213 skb = NULL;
1214 set_bit(IEEE80211_LINK_STATE_PENDING,
1215 &local->state[control->queue]);
1216 smp_mb();
1217 /* When the driver gets out of buffers during sending of
1218 * fragments and calls ieee80211_stop_queue, there is
1219 * a small window between IEEE80211_LINK_STATE_XOFF and
1220 * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
1221 * gets available in that window (i.e. driver calls
1222 * ieee80211_wake_queue), we would end up with ieee80211_tx
1223 * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
1224 * continuing transmitting here when that situation is
1225 * possible to have happened. */
1226 if (!__ieee80211_queue_stopped(local, control->queue)) {
1227 clear_bit(IEEE80211_LINK_STATE_PENDING,
1228 &local->state[control->queue]);
1229 goto retry;
1230 }
1231 memcpy(&store->control, control,
1232 sizeof(struct ieee80211_tx_control));
1233 store->skb = skb;
5cf121c3
JB
1234 store->extra_frag = tx.extra_frag;
1235 store->num_extra_frag = tx.num_extra_frag;
2e92e6f2 1236 store->last_frag_rate_idx = tx.last_frag_rate_idx;
badffb72 1237 store->last_frag_rate_ctrl_probe =
5cf121c3 1238 !!(tx.flags & IEEE80211_TX_PROBE_LAST_FRAG);
e2ebc74d 1239 }
d4e46a3d 1240 rcu_read_unlock();
e2ebc74d
JB
1241 return 0;
1242
1243 drop:
1244 if (skb)
1245 dev_kfree_skb(skb);
5cf121c3
JB
1246 for (i = 0; i < tx.num_extra_frag; i++)
1247 if (tx.extra_frag[i])
1248 dev_kfree_skb(tx.extra_frag[i]);
1249 kfree(tx.extra_frag);
d4e46a3d 1250 rcu_read_unlock();
e2ebc74d
JB
1251 return 0;
1252}
1253
1254/* device xmit handlers */
1255
1256int ieee80211_master_start_xmit(struct sk_buff *skb,
1257 struct net_device *dev)
1258{
1259 struct ieee80211_tx_control control;
1260 struct ieee80211_tx_packet_data *pkt_data;
1261 struct net_device *odev = NULL;
1262 struct ieee80211_sub_if_data *osdata;
1263 int headroom;
1264 int ret;
1265
1266 /*
1267 * copy control out of the skb so other people can use skb->cb
1268 */
1269 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1270 memset(&control, 0, sizeof(struct ieee80211_tx_control));
1271
1272 if (pkt_data->ifindex)
881d966b 1273 odev = dev_get_by_index(&init_net, pkt_data->ifindex);
e2ebc74d
JB
1274 if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
1275 dev_put(odev);
1276 odev = NULL;
1277 }
1278 if (unlikely(!odev)) {
1279#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1280 printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
1281 "originating device\n", dev->name);
1282#endif
1283 dev_kfree_skb(skb);
1284 return 0;
1285 }
1286 osdata = IEEE80211_DEV_TO_SUB_IF(odev);
1287
1288 headroom = osdata->local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM;
1289 if (skb_headroom(skb) < headroom) {
1290 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
1291 dev_kfree_skb(skb);
1292 dev_put(odev);
1293 return 0;
1294 }
1295 }
1296
32bfd35d 1297 control.vif = &osdata->vif;
51fb61e7 1298 control.type = osdata->vif.type;
e8bf9649 1299 if (pkt_data->flags & IEEE80211_TXPD_REQ_TX_STATUS)
e2ebc74d 1300 control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
e8bf9649 1301 if (pkt_data->flags & IEEE80211_TXPD_DO_NOT_ENCRYPT)
e2ebc74d 1302 control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
e8bf9649 1303 if (pkt_data->flags & IEEE80211_TXPD_REQUEUE)
e2ebc74d 1304 control.flags |= IEEE80211_TXCTL_REQUEUE;
678f5f71
JB
1305 if (pkt_data->flags & IEEE80211_TXPD_EAPOL_FRAME)
1306 control.flags |= IEEE80211_TXCTL_EAPOL_FRAME;
9e723492
RR
1307 if (pkt_data->flags & IEEE80211_TXPD_AMPDU)
1308 control.flags |= IEEE80211_TXCTL_AMPDU;
e2ebc74d
JB
1309 control.queue = pkt_data->queue;
1310
f9d540ee 1311 ret = ieee80211_tx(odev, skb, &control);
e2ebc74d
JB
1312 dev_put(odev);
1313
1314 return ret;
1315}
1316
1317int ieee80211_monitor_start_xmit(struct sk_buff *skb,
1318 struct net_device *dev)
1319{
1320 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1321 struct ieee80211_tx_packet_data *pkt_data;
1322 struct ieee80211_radiotap_header *prthdr =
1323 (struct ieee80211_radiotap_header *)skb->data;
9b8a74e3 1324 u16 len_rthdr;
e2ebc74d 1325
9b8a74e3
AG
1326 /* check for not even having the fixed radiotap header part */
1327 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1328 goto fail; /* too short to be possibly valid */
1329
1330 /* is it a header version we can trust to find length from? */
1331 if (unlikely(prthdr->it_version))
1332 goto fail; /* only version 0 is supported */
1333
1334 /* then there must be a radiotap header with a length we can use */
1335 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1336
1337 /* does the skb contain enough to deliver on the alleged length? */
1338 if (unlikely(skb->len < len_rthdr))
1339 goto fail; /* skb too short for claimed rt header extent */
e2ebc74d
JB
1340
1341 skb->dev = local->mdev;
1342
1343 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1344 memset(pkt_data, 0, sizeof(*pkt_data));
9b8a74e3 1345 /* needed because we set skb device to master */
e2ebc74d 1346 pkt_data->ifindex = dev->ifindex;
9b8a74e3 1347
e8bf9649 1348 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
b0a67179
ID
1349 /* Interfaces should always request a status report */
1350 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
e2ebc74d 1351
e2ebc74d
JB
1352 /*
1353 * fix up the pointers accounting for the radiotap
1354 * header still being in there. We are being given
1355 * a precooked IEEE80211 header so no need for
1356 * normal processing
1357 */
9b8a74e3 1358 skb_set_mac_header(skb, len_rthdr);
e2ebc74d 1359 /*
9b8a74e3
AG
1360 * these are just fixed to the end of the rt area since we
1361 * don't have any better information and at this point, nobody cares
e2ebc74d 1362 */
9b8a74e3
AG
1363 skb_set_network_header(skb, len_rthdr);
1364 skb_set_transport_header(skb, len_rthdr);
e2ebc74d 1365
9b8a74e3
AG
1366 /* pass the radiotap header up to the next stage intact */
1367 dev_queue_xmit(skb);
e2ebc74d 1368 return NETDEV_TX_OK;
9b8a74e3
AG
1369
1370fail:
1371 dev_kfree_skb(skb);
1372 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
e2ebc74d
JB
1373}
1374
1375/**
1376 * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1377 * subinterfaces (wlan#, WDS, and VLAN interfaces)
1378 * @skb: packet to be sent
1379 * @dev: incoming interface
1380 *
1381 * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1382 * not be freed, and caller is responsible for either retrying later or freeing
1383 * skb).
1384 *
1385 * This function takes in an Ethernet header and encapsulates it with suitable
1386 * IEEE 802.11 header based on which interface the packet is coming in. The
1387 * encapsulated packet will then be passed to master interface, wlan#.11, for
1388 * transmission (through low-level driver).
1389 */
1390int ieee80211_subif_start_xmit(struct sk_buff *skb,
1391 struct net_device *dev)
1392{
1393 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1394 struct ieee80211_tx_packet_data *pkt_data;
1395 struct ieee80211_sub_if_data *sdata;
1396 int ret = 1, head_need;
33b64eb2 1397 u16 ethertype, hdrlen, meshhdrlen = 0, fc;
e2ebc74d 1398 struct ieee80211_hdr hdr;
33b64eb2 1399 struct ieee80211s_hdr mesh_hdr;
e2ebc74d
JB
1400 const u8 *encaps_data;
1401 int encaps_len, skip_header_bytes;
e8bf9649 1402 int nh_pos, h_pos;
e2ebc74d 1403 struct sta_info *sta;
ce3edf6d 1404 u32 sta_flags = 0;
e2ebc74d
JB
1405
1406 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1407 if (unlikely(skb->len < ETH_HLEN)) {
1408 printk(KERN_DEBUG "%s: short skb (len=%d)\n",
1409 dev->name, skb->len);
1410 ret = 0;
1411 goto fail;
1412 }
1413
1414 nh_pos = skb_network_header(skb) - skb->data;
1415 h_pos = skb_transport_header(skb) - skb->data;
1416
1417 /* convert Ethernet header to proper 802.11 header (based on
1418 * operation mode) */
1419 ethertype = (skb->data[12] << 8) | skb->data[13];
e2ebc74d
JB
1420 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
1421
51fb61e7 1422 switch (sdata->vif.type) {
cf966838
JB
1423 case IEEE80211_IF_TYPE_AP:
1424 case IEEE80211_IF_TYPE_VLAN:
e2ebc74d
JB
1425 fc |= IEEE80211_FCTL_FROMDS;
1426 /* DA BSSID SA */
1427 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1428 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1429 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1430 hdrlen = 24;
cf966838
JB
1431 break;
1432 case IEEE80211_IF_TYPE_WDS:
e2ebc74d
JB
1433 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1434 /* RA TA DA SA */
1435 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1436 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1437 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1438 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1439 hdrlen = 30;
cf966838 1440 break;
33b64eb2
LCC
1441#ifdef CONFIG_MAC80211_MESH
1442 case IEEE80211_IF_TYPE_MESH_POINT:
1443 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1444 /* RA TA DA SA */
1445 if (is_multicast_ether_addr(skb->data))
1446 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1447 else if (mesh_nexthop_lookup(hdr.addr1, skb, dev))
1448 return 0;
1449 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1450 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1451 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1452 if (skb->pkt_type == PACKET_OTHERHOST) {
1453 /* Forwarded frame, keep mesh ttl and seqnum */
1454 struct ieee80211s_hdr *prev_meshhdr;
1455 prev_meshhdr = ((struct ieee80211s_hdr *)skb->cb);
1456 meshhdrlen = ieee80211_get_mesh_hdrlen(prev_meshhdr);
1457 memcpy(&mesh_hdr, prev_meshhdr, meshhdrlen);
1458 sdata->u.sta.mshstats.fwded_frames++;
1459 } else {
1460 if (!sdata->u.sta.mshcfg.dot11MeshTTL) {
1461 /* Do not send frames with mesh_ttl == 0 */
1462 sdata->u.sta.mshstats.dropped_frames_ttl++;
1463 ret = 0;
1464 goto fail;
1465 }
1466 meshhdrlen = ieee80211_new_mesh_header(&mesh_hdr,
902acc78 1467 sdata);
33b64eb2
LCC
1468 }
1469 hdrlen = 30;
1470 break;
1471#endif
cf966838 1472 case IEEE80211_IF_TYPE_STA:
e2ebc74d
JB
1473 fc |= IEEE80211_FCTL_TODS;
1474 /* BSSID SA DA */
1475 memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
1476 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1477 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1478 hdrlen = 24;
cf966838
JB
1479 break;
1480 case IEEE80211_IF_TYPE_IBSS:
e2ebc74d
JB
1481 /* DA SA BSSID */
1482 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1483 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1484 memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
1485 hdrlen = 24;
cf966838
JB
1486 break;
1487 default:
e2ebc74d
JB
1488 ret = 0;
1489 goto fail;
1490 }
1491
7d185b8b
JB
1492 /*
1493 * There's no need to try to look up the destination
1494 * if it is a multicast address (which can only happen
1495 * in AP mode)
1496 */
1497 if (!is_multicast_ether_addr(hdr.addr1)) {
d0709a65 1498 rcu_read_lock();
7d185b8b 1499 sta = sta_info_get(local, hdr.addr1);
d0709a65 1500 if (sta)
07346f81 1501 sta_flags = get_sta_flags(sta);
d0709a65 1502 rcu_read_unlock();
e2ebc74d
JB
1503 }
1504
3434fbd3
JB
1505 /* receiver and we are QoS enabled, use a QoS type frame */
1506 if (sta_flags & WLAN_STA_WME && local->hw.queues >= 4) {
ce3edf6d
JB
1507 fc |= IEEE80211_STYPE_QOS_DATA;
1508 hdrlen += 2;
1509 }
1510
1511 /*
238814fd
JB
1512 * Drop unicast frames to unauthorised stations unless they are
1513 * EAPOL frames from the local station.
ce3edf6d 1514 */
238814fd 1515 if (unlikely(!is_multicast_ether_addr(hdr.addr1) &&
33b64eb2
LCC
1516 !(sta_flags & WLAN_STA_AUTHORIZED) &&
1517 !(ethertype == ETH_P_PAE &&
ce3edf6d
JB
1518 compare_ether_addr(dev->dev_addr,
1519 skb->data + ETH_ALEN) == 0))) {
1520#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1521 DECLARE_MAC_BUF(mac);
1522
1523 if (net_ratelimit())
1524 printk(KERN_DEBUG "%s: dropped frame to %s"
1525 " (unauthorized port)\n", dev->name,
1526 print_mac(mac, hdr.addr1));
1527#endif
1528
1529 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
1530
1531 ret = 0;
1532 goto fail;
1533 }
1534
e2ebc74d
JB
1535 hdr.frame_control = cpu_to_le16(fc);
1536 hdr.duration_id = 0;
1537 hdr.seq_ctrl = 0;
1538
1539 skip_header_bytes = ETH_HLEN;
1540 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
1541 encaps_data = bridge_tunnel_header;
1542 encaps_len = sizeof(bridge_tunnel_header);
1543 skip_header_bytes -= 2;
1544 } else if (ethertype >= 0x600) {
1545 encaps_data = rfc1042_header;
1546 encaps_len = sizeof(rfc1042_header);
1547 skip_header_bytes -= 2;
1548 } else {
1549 encaps_data = NULL;
1550 encaps_len = 0;
1551 }
1552
1553 skb_pull(skb, skip_header_bytes);
1554 nh_pos -= skip_header_bytes;
1555 h_pos -= skip_header_bytes;
1556
1557 /* TODO: implement support for fragments so that there is no need to
1558 * reallocate and copy payload; it might be enough to support one
1559 * extra fragment that would be copied in the beginning of the frame
1560 * data.. anyway, it would be nice to include this into skb structure
1561 * somehow
1562 *
1563 * There are few options for this:
1564 * use skb->cb as an extra space for 802.11 header
1565 * allocate new buffer if not enough headroom
1566 * make sure that there is enough headroom in every skb by increasing
1567 * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
1568 * alloc_skb() (net/core/skbuff.c)
1569 */
33b64eb2 1570 head_need = hdrlen + encaps_len + meshhdrlen + local->tx_headroom;
e2ebc74d
JB
1571 head_need -= skb_headroom(skb);
1572
1573 /* We are going to modify skb data, so make a copy of it if happens to
1574 * be cloned. This could happen, e.g., with Linux bridge code passing
1575 * us broadcast frames. */
1576
608961a5 1577 if (head_need > 0 || skb_header_cloned(skb)) {
e2ebc74d
JB
1578#if 0
1579 printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
1580 "of headroom\n", dev->name, head_need);
1581#endif
1582
608961a5 1583 if (skb_header_cloned(skb))
e2ebc74d
JB
1584 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1585 else
1586 I802_DEBUG_INC(local->tx_expand_skb_head);
1587 /* Since we have to reallocate the buffer, make sure that there
1588 * is enough room for possible WEP IV/ICV and TKIP (8 bytes
1589 * before payload and 12 after). */
1590 if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
1591 12, GFP_ATOMIC)) {
1592 printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
1593 "\n", dev->name);
1594 goto fail;
1595 }
1596 }
1597
1598 if (encaps_data) {
1599 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
1600 nh_pos += encaps_len;
1601 h_pos += encaps_len;
1602 }
c29b9b9b 1603
33b64eb2
LCC
1604 if (meshhdrlen > 0) {
1605 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
1606 nh_pos += meshhdrlen;
1607 h_pos += meshhdrlen;
1608 }
1609
c29b9b9b
JB
1610 if (fc & IEEE80211_STYPE_QOS_DATA) {
1611 __le16 *qos_control;
1612
1613 qos_control = (__le16*) skb_push(skb, 2);
1614 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
1615 /*
1616 * Maybe we could actually set some fields here, for now just
1617 * initialise to zero to indicate no special operation.
1618 */
1619 *qos_control = 0;
1620 } else
1621 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
1622
e2ebc74d
JB
1623 nh_pos += hdrlen;
1624 h_pos += hdrlen;
1625
1626 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1627 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
1628 pkt_data->ifindex = dev->ifindex;
678f5f71
JB
1629 if (ethertype == ETH_P_PAE)
1630 pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
e2ebc74d 1631
b0a67179
ID
1632 /* Interfaces should always request a status report */
1633 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
1634
e2ebc74d 1635 skb->dev = local->mdev;
68aae116
SH
1636 dev->stats.tx_packets++;
1637 dev->stats.tx_bytes += skb->len;
e2ebc74d
JB
1638
1639 /* Update skb pointers to various headers since this modified frame
1640 * is going to go through Linux networking code that may potentially
1641 * need things like pointer to IP header. */
1642 skb_set_mac_header(skb, 0);
1643 skb_set_network_header(skb, nh_pos);
1644 skb_set_transport_header(skb, h_pos);
1645
1646 dev->trans_start = jiffies;
1647 dev_queue_xmit(skb);
1648
1649 return 0;
1650
1651 fail:
1652 if (!ret)
1653 dev_kfree_skb(skb);
1654
1655 return ret;
1656}
1657
e2ebc74d
JB
1658/* helper functions for pending packets for when queues are stopped */
1659
1660void ieee80211_clear_tx_pending(struct ieee80211_local *local)
1661{
1662 int i, j;
1663 struct ieee80211_tx_stored_packet *store;
1664
1665 for (i = 0; i < local->hw.queues; i++) {
1666 if (!__ieee80211_queue_pending(local, i))
1667 continue;
1668 store = &local->pending_packet[i];
1669 kfree_skb(store->skb);
1670 for (j = 0; j < store->num_extra_frag; j++)
1671 kfree_skb(store->extra_frag[j]);
1672 kfree(store->extra_frag);
1673 clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
1674 }
1675}
1676
1677void ieee80211_tx_pending(unsigned long data)
1678{
1679 struct ieee80211_local *local = (struct ieee80211_local *)data;
1680 struct net_device *dev = local->mdev;
1681 struct ieee80211_tx_stored_packet *store;
5cf121c3 1682 struct ieee80211_tx_data tx;
e2ebc74d
JB
1683 int i, ret, reschedule = 0;
1684
1685 netif_tx_lock_bh(dev);
1686 for (i = 0; i < local->hw.queues; i++) {
1687 if (__ieee80211_queue_stopped(local, i))
1688 continue;
1689 if (!__ieee80211_queue_pending(local, i)) {
1690 reschedule = 1;
1691 continue;
1692 }
1693 store = &local->pending_packet[i];
5cf121c3
JB
1694 tx.control = &store->control;
1695 tx.extra_frag = store->extra_frag;
1696 tx.num_extra_frag = store->num_extra_frag;
2e92e6f2 1697 tx.last_frag_rate_idx = store->last_frag_rate_idx;
badffb72
JS
1698 tx.flags = 0;
1699 if (store->last_frag_rate_ctrl_probe)
5cf121c3 1700 tx.flags |= IEEE80211_TX_PROBE_LAST_FRAG;
e2ebc74d
JB
1701 ret = __ieee80211_tx(local, store->skb, &tx);
1702 if (ret) {
1703 if (ret == IEEE80211_TX_FRAG_AGAIN)
1704 store->skb = NULL;
1705 } else {
1706 clear_bit(IEEE80211_LINK_STATE_PENDING,
1707 &local->state[i]);
1708 reschedule = 1;
1709 }
1710 }
1711 netif_tx_unlock_bh(dev);
1712 if (reschedule) {
1713 if (!ieee80211_qdisc_installed(dev)) {
1714 if (!__ieee80211_queue_stopped(local, 0))
1715 netif_wake_queue(dev);
1716 } else
1717 netif_schedule(dev);
1718 }
1719}
1720
1721/* functions for drivers to get certain frames */
1722
1723static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
1724 struct ieee80211_if_ap *bss,
5dfdaf58
JB
1725 struct sk_buff *skb,
1726 struct beacon_data *beacon)
e2ebc74d
JB
1727{
1728 u8 *pos, *tim;
1729 int aid0 = 0;
1730 int i, have_bits = 0, n1, n2;
1731
1732 /* Generate bitmap for TIM only if there are any STAs in power save
1733 * mode. */
e2ebc74d
JB
1734 if (atomic_read(&bss->num_sta_ps) > 0)
1735 /* in the hope that this is faster than
1736 * checking byte-for-byte */
1737 have_bits = !bitmap_empty((unsigned long*)bss->tim,
1738 IEEE80211_MAX_AID+1);
1739
1740 if (bss->dtim_count == 0)
5dfdaf58 1741 bss->dtim_count = beacon->dtim_period - 1;
e2ebc74d
JB
1742 else
1743 bss->dtim_count--;
1744
1745 tim = pos = (u8 *) skb_put(skb, 6);
1746 *pos++ = WLAN_EID_TIM;
1747 *pos++ = 4;
1748 *pos++ = bss->dtim_count;
5dfdaf58 1749 *pos++ = beacon->dtim_period;
e2ebc74d
JB
1750
1751 if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
1752 aid0 = 1;
1753
1754 if (have_bits) {
1755 /* Find largest even number N1 so that bits numbered 1 through
1756 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
1757 * (N2 + 1) x 8 through 2007 are 0. */
1758 n1 = 0;
1759 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
1760 if (bss->tim[i]) {
1761 n1 = i & 0xfe;
1762 break;
1763 }
1764 }
1765 n2 = n1;
1766 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
1767 if (bss->tim[i]) {
1768 n2 = i;
1769 break;
1770 }
1771 }
1772
1773 /* Bitmap control */
1774 *pos++ = n1 | aid0;
1775 /* Part Virt Bitmap */
1776 memcpy(pos, bss->tim + n1, n2 - n1 + 1);
1777
1778 tim[1] = n2 - n1 + 4;
1779 skb_put(skb, n2 - n1);
1780 } else {
1781 *pos++ = aid0; /* Bitmap control */
1782 *pos++ = 0; /* Part Virt Bitmap */
1783 }
e2ebc74d
JB
1784}
1785
32bfd35d
JB
1786struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw,
1787 struct ieee80211_vif *vif,
e2ebc74d
JB
1788 struct ieee80211_tx_control *control)
1789{
1790 struct ieee80211_local *local = hw_to_local(hw);
1791 struct sk_buff *skb;
1792 struct net_device *bdev;
1793 struct ieee80211_sub_if_data *sdata = NULL;
1794 struct ieee80211_if_ap *ap = NULL;
1abbe498 1795 struct rate_selection rsel;
5dfdaf58 1796 struct beacon_data *beacon;
8318d78a 1797 struct ieee80211_supported_band *sband;
902acc78 1798 struct ieee80211_mgmt *mgmt;
33b64eb2 1799 int *num_beacons;
902acc78 1800 bool err = true;
2e92e6f2 1801 enum ieee80211_band band = local->hw.conf.channel->band;
902acc78 1802 u8 *pos;
8318d78a 1803
2e92e6f2 1804 sband = local->hw.wiphy->bands[band];
5dfdaf58
JB
1805
1806 rcu_read_lock();
e2ebc74d 1807
32bfd35d
JB
1808 sdata = vif_to_sdata(vif);
1809 bdev = sdata->dev;
e2ebc74d 1810
902acc78 1811 if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
33b64eb2
LCC
1812 ap = &sdata->u.ap;
1813 beacon = rcu_dereference(ap->beacon);
902acc78
JB
1814 if (ap && beacon) {
1815 /*
1816 * headroom, head length,
1817 * tail length and maximum TIM length
1818 */
1819 skb = dev_alloc_skb(local->tx_headroom +
1820 beacon->head_len +
1821 beacon->tail_len + 256);
1822 if (!skb)
1823 goto out;
33b64eb2 1824
902acc78
JB
1825 skb_reserve(skb, local->tx_headroom);
1826 memcpy(skb_put(skb, beacon->head_len), beacon->head,
1827 beacon->head_len);
33b64eb2 1828
902acc78
JB
1829 ieee80211_include_sequence(sdata,
1830 (struct ieee80211_hdr *)skb->data);
33b64eb2 1831
d0709a65
JB
1832 /*
1833 * Not very nice, but we want to allow the driver to call
1834 * ieee80211_beacon_get() as a response to the set_tim()
1835 * callback. That, however, is already invoked under the
1836 * sta_lock to guarantee consistent and race-free update
1837 * of the tim bitmap in mac80211 and the driver.
1838 */
1839 if (local->tim_in_locked_section) {
1840 ieee80211_beacon_add_tim(local, ap, skb, beacon);
1841 } else {
1842 unsigned long flags;
1843
1844 spin_lock_irqsave(&local->sta_lock, flags);
1845 ieee80211_beacon_add_tim(local, ap, skb, beacon);
1846 spin_unlock_irqrestore(&local->sta_lock, flags);
1847 }
33b64eb2 1848
902acc78
JB
1849 if (beacon->tail)
1850 memcpy(skb_put(skb, beacon->tail_len),
1851 beacon->tail, beacon->tail_len);
33b64eb2 1852
902acc78 1853 num_beacons = &ap->num_beacons;
33b64eb2 1854
902acc78
JB
1855 err = false;
1856 }
1857 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
1858 /* headroom, head length, tail length and maximum TIM length */
1859 skb = dev_alloc_skb(local->tx_headroom + 400);
1860 if (!skb)
1861 goto out;
1862
1863 skb_reserve(skb, local->hw.extra_tx_headroom);
1864 mgmt = (struct ieee80211_mgmt *)
1865 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
1866 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
1867 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1868 IEEE80211_STYPE_BEACON);
1869 memset(mgmt->da, 0xff, ETH_ALEN);
1870 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1871 /* BSSID is left zeroed, wildcard value */
1872 mgmt->u.beacon.beacon_int =
1873 cpu_to_le16(local->hw.conf.beacon_int);
1874 mgmt->u.beacon.capab_info = 0x0; /* 0x0 for MPs */
1875
1876 pos = skb_put(skb, 2);
1877 *pos++ = WLAN_EID_SSID;
1878 *pos++ = 0x0;
1879
1880 mesh_mgmt_ies_add(skb, sdata->dev);
33b64eb2 1881
33b64eb2 1882 num_beacons = &sdata->u.sta.num_beacons;
33b64eb2 1883
902acc78 1884 err = false;
33b64eb2 1885 }
5dfdaf58 1886
33b64eb2 1887 if (err) {
e2ebc74d
JB
1888#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1889 if (net_ratelimit())
32bfd35d
JB
1890 printk(KERN_DEBUG "no beacon data avail for %s\n",
1891 bdev->name);
e2ebc74d 1892#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
5dfdaf58
JB
1893 skb = NULL;
1894 goto out;
e2ebc74d
JB
1895 }
1896
e2ebc74d 1897 if (control) {
2e92e6f2 1898 control->band = band;
8318d78a 1899 rate_control_get_rate(local->mdev, sband, skb, &rsel);
2e92e6f2 1900 if (unlikely(rsel.rate_idx < 0)) {
e2ebc74d 1901 if (net_ratelimit()) {
1abbe498
MN
1902 printk(KERN_DEBUG "%s: ieee80211_beacon_get: "
1903 "no rate found\n",
1904 wiphy_name(local->hw.wiphy));
e2ebc74d
JB
1905 }
1906 dev_kfree_skb(skb);
5dfdaf58
JB
1907 skb = NULL;
1908 goto out;
e2ebc74d
JB
1909 }
1910
0f7054e3 1911 control->vif = vif;
2e92e6f2 1912 control->tx_rate_idx = rsel.rate_idx;
8318d78a 1913 if (sdata->bss_conf.use_short_preamble &&
2e92e6f2 1914 sband->bitrates[rsel.rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE)
8318d78a 1915 control->flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
e2ebc74d 1916 control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
e2ebc74d 1917 control->flags |= IEEE80211_TXCTL_NO_ACK;
df442054 1918 control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
e2ebc74d 1919 control->retry_limit = 1;
d46e144b 1920 control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
e2ebc74d 1921 }
33b64eb2
LCC
1922 (*num_beacons)++;
1923out:
5dfdaf58 1924 rcu_read_unlock();
e2ebc74d
JB
1925 return skb;
1926}
1927EXPORT_SYMBOL(ieee80211_beacon_get);
1928
32bfd35d 1929void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
e2ebc74d
JB
1930 const void *frame, size_t frame_len,
1931 const struct ieee80211_tx_control *frame_txctl,
1932 struct ieee80211_rts *rts)
1933{
1934 const struct ieee80211_hdr *hdr = frame;
1935 u16 fctl;
1936
1937 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
1938 rts->frame_control = cpu_to_le16(fctl);
32bfd35d
JB
1939 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
1940 frame_txctl);
e2ebc74d
JB
1941 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
1942 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
1943}
1944EXPORT_SYMBOL(ieee80211_rts_get);
1945
32bfd35d 1946void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
e2ebc74d
JB
1947 const void *frame, size_t frame_len,
1948 const struct ieee80211_tx_control *frame_txctl,
1949 struct ieee80211_cts *cts)
1950{
1951 const struct ieee80211_hdr *hdr = frame;
1952 u16 fctl;
1953
1954 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
1955 cts->frame_control = cpu_to_le16(fctl);
32bfd35d
JB
1956 cts->duration = ieee80211_ctstoself_duration(hw, vif,
1957 frame_len, frame_txctl);
e2ebc74d
JB
1958 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
1959}
1960EXPORT_SYMBOL(ieee80211_ctstoself_get);
1961
1962struct sk_buff *
32bfd35d
JB
1963ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
1964 struct ieee80211_vif *vif,
e2ebc74d
JB
1965 struct ieee80211_tx_control *control)
1966{
1967 struct ieee80211_local *local = hw_to_local(hw);
1968 struct sk_buff *skb;
1969 struct sta_info *sta;
1970 ieee80211_tx_handler *handler;
5cf121c3 1971 struct ieee80211_tx_data tx;
9ae54c84 1972 ieee80211_tx_result res = TX_DROP;
e2ebc74d
JB
1973 struct net_device *bdev;
1974 struct ieee80211_sub_if_data *sdata;
1975 struct ieee80211_if_ap *bss = NULL;
5dfdaf58 1976 struct beacon_data *beacon;
e2ebc74d 1977
32bfd35d
JB
1978 sdata = vif_to_sdata(vif);
1979 bdev = sdata->dev;
1980
5dfdaf58
JB
1981
1982 if (!bss)
e2ebc74d
JB
1983 return NULL;
1984
5dfdaf58
JB
1985 rcu_read_lock();
1986 beacon = rcu_dereference(bss->beacon);
1987
1988 if (sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon ||
1989 !beacon->head) {
1990 rcu_read_unlock();
1991 return NULL;
1992 }
5dfdaf58 1993
e2ebc74d
JB
1994 if (bss->dtim_count != 0)
1995 return NULL; /* send buffered bc/mc only after DTIM beacon */
1996 memset(control, 0, sizeof(*control));
1997 while (1) {
1998 skb = skb_dequeue(&bss->ps_bc_buf);
1999 if (!skb)
2000 return NULL;
2001 local->total_ps_buffered--;
2002
2003 if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
2004 struct ieee80211_hdr *hdr =
2005 (struct ieee80211_hdr *) skb->data;
2006 /* more buffered multicast/broadcast frames ==> set
2007 * MoreData flag in IEEE 802.11 header to inform PS
2008 * STAs */
2009 hdr->frame_control |=
2010 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2011 }
2012
58d4185e 2013 if (!ieee80211_tx_prepare(&tx, skb, local->mdev, control))
e2ebc74d
JB
2014 break;
2015 dev_kfree_skb_any(skb);
2016 }
2017 sta = tx.sta;
5cf121c3
JB
2018 tx.flags |= IEEE80211_TX_PS_BUFFERED;
2019 tx.channel = local->hw.conf.channel;
2e92e6f2 2020 control->band = tx.channel->band;
e2ebc74d 2021
58905290 2022 for (handler = ieee80211_tx_handlers; *handler != NULL; handler++) {
e2ebc74d 2023 res = (*handler)(&tx);
9ae54c84 2024 if (res == TX_DROP || res == TX_QUEUED)
e2ebc74d
JB
2025 break;
2026 }
e2ebc74d
JB
2027 skb = tx.skb; /* handlers are allowed to change skb */
2028
9ae54c84 2029 if (res == TX_DROP) {
e2ebc74d
JB
2030 I802_DEBUG_INC(local->tx_handlers_drop);
2031 dev_kfree_skb(skb);
2032 skb = NULL;
9ae54c84 2033 } else if (res == TX_QUEUED) {
e2ebc74d
JB
2034 I802_DEBUG_INC(local->tx_handlers_queued);
2035 skb = NULL;
2036 }
2037
d0709a65 2038 rcu_read_unlock();
e2ebc74d
JB
2039
2040 return skb;
2041}
2042EXPORT_SYMBOL(ieee80211_get_buffered_bc);