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