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