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"
35 #define IEEE80211_TX_OK 0
36 #define IEEE80211_TX_AGAIN 1
37 #define IEEE80211_TX_FRAG_AGAIN 2
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;
60 printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
66 fc = le16_to_cpu(hdr->frame_control);
67 hdrlen = ieee80211_get_hdrlen(fc);
68 if (hdrlen > skb->len)
71 printk(" FC=0x%04x DUR=0x%04x",
72 fc, le16_to_cpu(hdr->duration_id));
74 printk(" A1=%s", print_mac(mac, hdr->addr1));
76 printk(" A2=%s", print_mac(mac, hdr->addr2));
78 printk(" A3=%s", print_mac(mac, hdr->addr3));
80 printk(" A4=%s", print_mac(mac, hdr->addr4));
83 #else /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
84 static inline void ieee80211_dump_frame(const char *ifname, const char *title,
88 #endif /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
90 static u16 ieee80211_duration(struct ieee80211_tx_data *tx, int group_addr,
93 int rate, mrate, erp, dur, i;
94 struct ieee80211_rate *txrate;
95 struct ieee80211_local *local = tx->local;
96 struct ieee80211_supported_band *sband;
98 sband = local->hw.wiphy->bands[tx->channel->band];
99 txrate = &sband->bitrates[tx->rate_idx];
102 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
103 erp = txrate->flags & IEEE80211_RATE_ERP_G;
106 * data and mgmt (except PS Poll):
107 * - during CFP: 32768
108 * - during contention period:
109 * if addr1 is group address: 0
110 * if more fragments = 0 and addr1 is individual address: time to
111 * transmit one ACK plus SIFS
112 * if more fragments = 1 and addr1 is individual address: time to
113 * transmit next fragment plus 2 x ACK plus 3 x SIFS
116 * - control response frame (CTS or ACK) shall be transmitted using the
117 * same rate as the immediately previous frame in the frame exchange
118 * sequence, if this rate belongs to the PHY mandatory rates, or else
119 * at the highest possible rate belonging to the PHY rates in the
123 if ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) {
124 /* TODO: These control frames are not currently sent by
125 * 80211.o, but should they be implemented, this function
126 * needs to be updated to support duration field calculation.
128 * RTS: time needed to transmit pending data/mgmt frame plus
129 * one CTS frame plus one ACK frame plus 3 x SIFS
130 * CTS: duration of immediately previous RTS minus time
131 * required to transmit CTS and its SIFS
132 * ACK: 0 if immediately previous directed data/mgmt had
133 * more=0, with more=1 duration in ACK frame is duration
134 * from previous frame minus time needed to transmit ACK
136 * PS Poll: BIT(15) | BIT(14) | aid
142 if (0 /* FIX: data/mgmt during CFP */)
145 if (group_addr) /* Group address as the destination - no ACK */
148 /* Individual destination address:
149 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
150 * CTS and ACK frames shall be transmitted using the highest rate in
151 * basic rate set that is less than or equal to the rate of the
152 * immediately previous frame and that is using the same modulation
153 * (CCK or OFDM). If no basic rate set matches with these requirements,
154 * the highest mandatory rate of the PHY that is less than or equal to
155 * the rate of the previous frame is used.
156 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
159 /* use lowest available if everything fails */
160 mrate = sband->bitrates[0].bitrate;
161 for (i = 0; i < sband->n_bitrates; i++) {
162 struct ieee80211_rate *r = &sband->bitrates[i];
164 if (r->bitrate > txrate->bitrate)
167 if (tx->sdata->basic_rates & BIT(i))
170 switch (sband->band) {
171 case IEEE80211_BAND_2GHZ: {
173 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
174 flag = IEEE80211_RATE_MANDATORY_G;
176 flag = IEEE80211_RATE_MANDATORY_B;
181 case IEEE80211_BAND_5GHZ:
182 if (r->flags & IEEE80211_RATE_MANDATORY_A)
185 case IEEE80211_NUM_BANDS:
191 /* No matching basic rate found; use highest suitable mandatory
196 /* Time needed to transmit ACK
197 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
198 * to closest integer */
200 dur = ieee80211_frame_duration(local, 10, rate, erp,
201 tx->sdata->bss_conf.use_short_preamble);
204 /* Frame is fragmented: duration increases with time needed to
205 * transmit next fragment plus ACK and 2 x SIFS. */
206 dur *= 2; /* ACK + SIFS */
208 dur += ieee80211_frame_duration(local, next_frag_len,
209 txrate->bitrate, erp,
210 tx->sdata->bss_conf.use_short_preamble);
216 static inline int __ieee80211_queue_stopped(const struct ieee80211_local *local,
219 return test_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
222 static inline int __ieee80211_queue_pending(const struct ieee80211_local *local,
225 return test_bit(IEEE80211_LINK_STATE_PENDING, &local->state[queue]);
228 static int inline is_ieee80211_device(struct net_device *dev,
229 struct net_device *master)
231 return (wdev_priv(dev->ieee80211_ptr) ==
232 wdev_priv(master->ieee80211_ptr));
237 static ieee80211_tx_result
238 ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
240 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
241 struct sk_buff *skb = tx->skb;
242 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
243 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
246 if (unlikely(tx->flags & IEEE80211_TX_INJECTED))
249 if (unlikely(tx->local->sta_sw_scanning) &&
250 ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
251 (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
254 if (tx->sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT)
257 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
260 sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
262 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
263 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
264 tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
265 (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
266 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
267 DECLARE_MAC_BUF(mac);
268 printk(KERN_DEBUG "%s: dropped data frame to not "
269 "associated station %s\n",
270 tx->dev->name, print_mac(mac, hdr->addr1));
271 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
272 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
276 if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
277 tx->local->num_sta == 0 &&
278 tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS)) {
280 * No associated STAs - no need to send multicast
291 static ieee80211_tx_result
292 ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
294 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
296 if (ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
297 ieee80211_include_sequence(tx->sdata, hdr);
302 /* This function is called whenever the AP is about to exceed the maximum limit
303 * of buffered frames for power saving STAs. This situation should not really
304 * happen often during normal operation, so dropping the oldest buffered packet
305 * from each queue should be OK to make some room for new frames. */
306 static void purge_old_ps_buffers(struct ieee80211_local *local)
308 int total = 0, purged = 0;
310 struct ieee80211_sub_if_data *sdata;
311 struct sta_info *sta;
314 * virtual interfaces are protected by RCU
318 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
319 struct ieee80211_if_ap *ap;
320 if (sdata->dev == local->mdev ||
321 sdata->vif.type != IEEE80211_IF_TYPE_AP)
324 skb = skb_dequeue(&ap->ps_bc_buf);
329 total += skb_queue_len(&ap->ps_bc_buf);
332 list_for_each_entry_rcu(sta, &local->sta_list, list) {
333 skb = skb_dequeue(&sta->ps_tx_buf);
338 total += skb_queue_len(&sta->ps_tx_buf);
343 local->total_ps_buffered = total;
344 printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
345 wiphy_name(local->hw.wiphy), purged);
348 static ieee80211_tx_result
349 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
352 * broadcast/multicast frame
354 * If any of the associated stations is in power save mode,
355 * the frame is buffered to be sent after DTIM beacon frame.
356 * This is done either by the hardware or us.
359 /* not AP/IBSS or ordered frame */
360 if (!tx->sdata->bss || (tx->fc & IEEE80211_FCTL_ORDER))
363 /* no stations in PS mode */
364 if (!atomic_read(&tx->sdata->bss->num_sta_ps))
367 /* buffered in mac80211 */
368 if (tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) {
369 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
370 purge_old_ps_buffers(tx->local);
371 if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
373 if (net_ratelimit()) {
374 printk(KERN_DEBUG "%s: BC TX buffer full - "
375 "dropping the oldest frame\n",
378 dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
380 tx->local->total_ps_buffered++;
381 skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
385 /* buffered in hardware */
386 tx->control->flags |= IEEE80211_TXCTL_SEND_AFTER_DTIM;
391 static ieee80211_tx_result
392 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
394 struct sta_info *sta = tx->sta;
396 DECLARE_MAC_BUF(mac);
399 ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
400 (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
403 staflags = get_sta_flags(sta);
405 if (unlikely((staflags & WLAN_STA_PS) &&
406 !(staflags & WLAN_STA_PSPOLL))) {
407 struct ieee80211_tx_packet_data *pkt_data;
408 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
409 printk(KERN_DEBUG "STA %s aid %d: PS buffer (entries "
411 print_mac(mac, sta->addr), sta->aid,
412 skb_queue_len(&sta->ps_tx_buf));
413 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
414 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
415 purge_old_ps_buffers(tx->local);
416 if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
417 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
418 if (net_ratelimit()) {
419 printk(KERN_DEBUG "%s: STA %s TX "
420 "buffer full - dropping oldest frame\n",
421 tx->dev->name, print_mac(mac, sta->addr));
425 tx->local->total_ps_buffered++;
427 /* Queue frame to be sent after STA sends an PS Poll frame */
428 if (skb_queue_empty(&sta->ps_tx_buf))
429 sta_info_set_tim_bit(sta);
431 pkt_data = (struct ieee80211_tx_packet_data *)tx->skb->cb;
432 pkt_data->jiffies = jiffies;
433 skb_queue_tail(&sta->ps_tx_buf, tx->skb);
436 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
437 else if (unlikely(test_sta_flags(sta, WLAN_STA_PS))) {
438 printk(KERN_DEBUG "%s: STA %s in PS mode, but pspoll "
439 "set -> send frame\n", tx->dev->name,
440 print_mac(mac, sta->addr));
442 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
443 clear_sta_flags(sta, WLAN_STA_PSPOLL);
448 static ieee80211_tx_result
449 ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
451 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
454 if (tx->flags & IEEE80211_TX_UNICAST)
455 return ieee80211_tx_h_unicast_ps_buf(tx);
457 return ieee80211_tx_h_multicast_ps_buf(tx);
460 static ieee80211_tx_result
461 ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
463 struct ieee80211_key *key;
466 if (unlikely(tx->control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
468 else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
470 else if ((key = rcu_dereference(tx->sdata->default_key)))
472 else if (tx->sdata->drop_unencrypted &&
473 !(tx->control->flags & IEEE80211_TXCTL_EAPOL_FRAME) &&
474 !(tx->flags & IEEE80211_TX_INJECTED)) {
475 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
483 tx->key->tx_rx_count++;
484 /* TODO: add threshold stuff again */
486 switch (tx->key->conf.alg) {
488 ftype = fc & IEEE80211_FCTL_FTYPE;
489 stype = fc & IEEE80211_FCTL_STYPE;
491 if (ftype == IEEE80211_FTYPE_MGMT &&
492 stype == IEEE80211_STYPE_AUTH)
496 if (!WLAN_FC_DATA_PRESENT(fc))
502 if (!tx->key || !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
503 tx->control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
508 static ieee80211_tx_result
509 ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
511 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
512 size_t hdrlen, per_fragm, num_fragm, payload_len, left;
513 struct sk_buff **frags, *first, *frag;
517 int frag_threshold = tx->local->fragmentation_threshold;
519 if (!(tx->flags & IEEE80211_TX_FRAGMENTED))
524 hdrlen = ieee80211_get_hdrlen(tx->fc);
525 payload_len = first->len - hdrlen;
526 per_fragm = frag_threshold - hdrlen - FCS_LEN;
527 num_fragm = DIV_ROUND_UP(payload_len, per_fragm);
529 frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
533 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
534 seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
535 pos = first->data + hdrlen + per_fragm;
536 left = payload_len - per_fragm;
537 for (i = 0; i < num_fragm - 1; i++) {
538 struct ieee80211_hdr *fhdr;
544 /* reserve enough extra head and tail room for possible
547 dev_alloc_skb(tx->local->tx_headroom +
549 IEEE80211_ENCRYPT_HEADROOM +
550 IEEE80211_ENCRYPT_TAILROOM);
553 /* Make sure that all fragments use the same priority so
554 * that they end up using the same TX queue */
555 frag->priority = first->priority;
556 skb_reserve(frag, tx->local->tx_headroom +
557 IEEE80211_ENCRYPT_HEADROOM);
558 fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
559 memcpy(fhdr, first->data, hdrlen);
560 if (i == num_fragm - 2)
561 fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
562 fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
563 copylen = left > per_fragm ? per_fragm : left;
564 memcpy(skb_put(frag, copylen), pos, copylen);
569 skb_trim(first, hdrlen + per_fragm);
571 tx->num_extra_frag = num_fragm - 1;
572 tx->extra_frag = frags;
577 printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
579 for (i = 0; i < num_fragm - 1; i++)
581 dev_kfree_skb(frags[i]);
584 I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
588 static ieee80211_tx_result
589 ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
594 switch (tx->key->conf.alg) {
596 return ieee80211_crypto_wep_encrypt(tx);
598 return ieee80211_crypto_tkip_encrypt(tx);
600 return ieee80211_crypto_ccmp_encrypt(tx);
608 static ieee80211_tx_result
609 ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
611 struct rate_selection rsel;
612 struct ieee80211_supported_band *sband;
614 sband = tx->local->hw.wiphy->bands[tx->channel->band];
616 if (likely(tx->rate_idx < 0)) {
617 rate_control_get_rate(tx->dev, sband, tx->skb, &rsel);
618 tx->rate_idx = rsel.rate_idx;
619 if (unlikely(rsel.probe_idx >= 0)) {
620 tx->control->flags |=
621 IEEE80211_TXCTL_RATE_CTRL_PROBE;
622 tx->flags |= IEEE80211_TX_PROBE_LAST_FRAG;
623 tx->control->alt_retry_rate_idx = tx->rate_idx;
624 tx->rate_idx = rsel.probe_idx;
626 tx->control->alt_retry_rate_idx = -1;
628 if (unlikely(tx->rate_idx < 0))
631 tx->control->alt_retry_rate_idx = -1;
633 if (tx->sdata->bss_conf.use_cts_prot &&
634 (tx->flags & IEEE80211_TX_FRAGMENTED) && (rsel.nonerp_idx >= 0)) {
635 tx->last_frag_rate_idx = tx->rate_idx;
636 if (rsel.probe_idx >= 0)
637 tx->flags &= ~IEEE80211_TX_PROBE_LAST_FRAG;
639 tx->flags |= IEEE80211_TX_PROBE_LAST_FRAG;
640 tx->rate_idx = rsel.nonerp_idx;
641 tx->control->tx_rate_idx = rsel.nonerp_idx;
642 tx->control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
644 tx->last_frag_rate_idx = tx->rate_idx;
645 tx->control->tx_rate_idx = tx->rate_idx;
647 tx->control->tx_rate_idx = tx->rate_idx;
652 static ieee80211_tx_result
653 ieee80211_tx_h_misc(struct ieee80211_tx_data *tx)
655 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
656 u16 fc = le16_to_cpu(hdr->frame_control);
658 struct ieee80211_tx_control *control = tx->control;
659 struct ieee80211_supported_band *sband;
661 sband = tx->local->hw.wiphy->bands[tx->channel->band];
663 if (!control->retry_limit) {
664 if (!is_multicast_ether_addr(hdr->addr1)) {
665 if (tx->skb->len + FCS_LEN > tx->local->rts_threshold
666 && tx->local->rts_threshold <
667 IEEE80211_MAX_RTS_THRESHOLD) {
669 IEEE80211_TXCTL_USE_RTS_CTS;
671 IEEE80211_TXCTL_LONG_RETRY_LIMIT;
672 control->retry_limit =
673 tx->local->long_retry_limit;
675 control->retry_limit =
676 tx->local->short_retry_limit;
679 control->retry_limit = 1;
683 if (tx->flags & IEEE80211_TX_FRAGMENTED) {
684 /* Do not use multiple retry rates when sending fragmented
686 * TODO: The last fragment could still use multiple retry
688 control->alt_retry_rate_idx = -1;
691 /* Use CTS protection for unicast frames sent using extended rates if
692 * there are associated non-ERP stations and RTS/CTS is not configured
694 if ((tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE) &&
695 (sband->bitrates[tx->rate_idx].flags & IEEE80211_RATE_ERP_G) &&
696 (tx->flags & IEEE80211_TX_UNICAST) &&
697 tx->sdata->bss_conf.use_cts_prot &&
698 !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
699 control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
701 /* Transmit data frames using short preambles if the driver supports
702 * short preambles at the selected rate and short preambles are
703 * available on the network at the current point in time. */
704 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
705 (sband->bitrates[tx->rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
706 tx->sdata->bss_conf.use_short_preamble &&
707 (!tx->sta || test_sta_flags(tx->sta, WLAN_STA_SHORT_PREAMBLE))) {
708 tx->control->flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
711 /* Setup duration field for the first fragment of the frame. Duration
712 * for remaining fragments will be updated when they are being sent
713 * to low-level driver in ieee80211_tx(). */
714 dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
715 (tx->flags & IEEE80211_TX_FRAGMENTED) ?
716 tx->extra_frag[0]->len : 0);
717 hdr->duration_id = cpu_to_le16(dur);
719 if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
720 (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
721 struct ieee80211_supported_band *sband;
722 struct ieee80211_rate *rate;
726 sband = tx->local->hw.wiphy->bands[tx->channel->band];
728 /* Do not use multiple retry rates when using RTS/CTS */
729 control->alt_retry_rate_idx = -1;
731 /* Use min(data rate, max base rate) as CTS/RTS rate */
732 rate = &sband->bitrates[tx->rate_idx];
734 for (idx = 0; idx < sband->n_bitrates; idx++) {
735 if (sband->bitrates[idx].bitrate > rate->bitrate)
737 if (tx->sdata->basic_rates & BIT(idx) &&
739 (sband->bitrates[baserate].bitrate
740 < sband->bitrates[idx].bitrate)))
745 control->rts_cts_rate_idx = baserate;
747 control->rts_cts_rate_idx = 0;
751 control->aid = tx->sta->aid;
752 tx->sta->tx_packets++;
753 tx->sta->tx_fragments++;
754 tx->sta->tx_bytes += tx->skb->len;
755 if (tx->extra_frag) {
757 tx->sta->tx_fragments += tx->num_extra_frag;
758 for (i = 0; i < tx->num_extra_frag; i++) {
760 tx->extra_frag[i]->len;
768 static ieee80211_tx_result
769 ieee80211_tx_h_load_stats(struct ieee80211_tx_data *tx)
771 struct ieee80211_local *local = tx->local;
772 struct sk_buff *skb = tx->skb;
773 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
774 u32 load = 0, hdrtime;
775 struct ieee80211_rate *rate;
776 struct ieee80211_supported_band *sband;
778 sband = tx->local->hw.wiphy->bands[tx->channel->band];
779 rate = &sband->bitrates[tx->rate_idx];
781 /* TODO: this could be part of tx_status handling, so that the number
782 * of retries would be known; TX rate should in that case be stored
783 * somewhere with the packet */
785 /* Estimate total channel use caused by this frame */
787 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
788 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
790 if (tx->channel->band == IEEE80211_BAND_5GHZ ||
791 (tx->channel->band == IEEE80211_BAND_2GHZ &&
792 rate->flags & IEEE80211_RATE_ERP_G))
793 hdrtime = CHAN_UTIL_HDR_SHORT;
795 hdrtime = CHAN_UTIL_HDR_LONG;
798 if (!is_multicast_ether_addr(hdr->addr1))
801 if (tx->control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
803 else if (tx->control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
806 /* TODO: optimise again */
807 load += skb->len * CHAN_UTIL_RATE_LCM / rate->bitrate;
809 if (tx->extra_frag) {
811 for (i = 0; i < tx->num_extra_frag; i++) {
813 load += tx->extra_frag[i]->len *
818 /* Divide channel_use by 8 to avoid wrapping around the counter */
819 load >>= CHAN_UTIL_SHIFT;
820 local->channel_use_raw += load;
822 tx->sta->channel_use_raw += load;
823 tx->sdata->channel_use_raw += load;
829 typedef ieee80211_tx_result (*ieee80211_tx_handler)(struct ieee80211_tx_data *);
830 static ieee80211_tx_handler ieee80211_tx_handlers[] =
832 ieee80211_tx_h_check_assoc,
833 ieee80211_tx_h_sequence,
834 ieee80211_tx_h_ps_buf,
835 ieee80211_tx_h_select_key,
836 ieee80211_tx_h_michael_mic_add,
837 ieee80211_tx_h_fragment,
838 ieee80211_tx_h_encrypt,
839 ieee80211_tx_h_rate_ctrl,
841 ieee80211_tx_h_load_stats,
845 /* actual transmit path */
848 * deal with packet injection down monitor interface
849 * with Radiotap Header -- only called for monitor mode interface
851 static ieee80211_tx_result
852 __ieee80211_parse_tx_radiotap(struct ieee80211_tx_data *tx,
856 * this is the moment to interpret and discard the radiotap header that
857 * must be at the start of the packet injected in Monitor mode
859 * Need to take some care with endian-ness since radiotap
860 * args are little-endian
863 struct ieee80211_radiotap_iterator iterator;
864 struct ieee80211_radiotap_header *rthdr =
865 (struct ieee80211_radiotap_header *) skb->data;
866 struct ieee80211_supported_band *sband;
867 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
868 struct ieee80211_tx_control *control = tx->control;
870 sband = tx->local->hw.wiphy->bands[tx->channel->band];
872 control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
873 tx->flags |= IEEE80211_TX_INJECTED;
874 tx->flags &= ~IEEE80211_TX_FRAGMENTED;
877 * for every radiotap entry that is present
878 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
879 * entries present, or -EINVAL on error)
885 ret = ieee80211_radiotap_iterator_next(&iterator);
890 /* see if this argument is something we can use */
891 switch (iterator.this_arg_index) {
893 * You must take care when dereferencing iterator.this_arg
894 * for multibyte types... the pointer is not aligned. Use
895 * get_unaligned((type *)iterator.this_arg) to dereference
896 * iterator.this_arg for type "type" safely on all arches.
898 case IEEE80211_RADIOTAP_RATE:
900 * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
901 * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
903 target_rate = (*iterator.this_arg) * 5;
904 for (i = 0; i < sband->n_bitrates; i++) {
905 struct ieee80211_rate *r;
907 r = &sband->bitrates[i];
909 if (r->bitrate == target_rate) {
916 case IEEE80211_RADIOTAP_ANTENNA:
918 * radiotap uses 0 for 1st ant, mac80211 is 1 for
921 control->antenna_sel_tx = (*iterator.this_arg) + 1;
925 case IEEE80211_RADIOTAP_DBM_TX_POWER:
926 control->power_level = *iterator.this_arg;
930 case IEEE80211_RADIOTAP_FLAGS:
931 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
933 * this indicates that the skb we have been
934 * handed has the 32-bit FCS CRC at the end...
935 * we should react to that by snipping it off
936 * because it will be recomputed and added
939 if (skb->len < (iterator.max_length + FCS_LEN))
942 skb_trim(skb, skb->len - FCS_LEN);
944 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
946 ~IEEE80211_TXCTL_DO_NOT_ENCRYPT;
947 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
948 tx->flags |= IEEE80211_TX_FRAGMENTED;
952 * Please update the file
953 * Documentation/networking/mac80211-injection.txt
954 * when parsing new fields here.
962 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
966 * remove the radiotap header
967 * iterator->max_length was sanity-checked against
968 * skb->len by iterator init
970 skb_pull(skb, iterator.max_length);
978 static ieee80211_tx_result
979 __ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
981 struct net_device *dev,
982 struct ieee80211_tx_control *control)
984 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
985 struct ieee80211_hdr *hdr;
986 struct ieee80211_sub_if_data *sdata;
990 memset(tx, 0, sizeof(*tx));
992 tx->dev = dev; /* use original interface */
994 tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
995 tx->control = control;
997 * Set this flag (used below to indicate "automatic fragmentation"),
998 * it will be cleared/left by radiotap as desired.
1000 tx->flags |= IEEE80211_TX_FRAGMENTED;
1002 /* process and remove the injection radiotap header */
1003 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1004 if (unlikely(sdata->vif.type == IEEE80211_IF_TYPE_MNTR)) {
1005 if (__ieee80211_parse_tx_radiotap(tx, skb) == TX_DROP)
1009 * __ieee80211_parse_tx_radiotap has now removed
1010 * the radiotap header that was present and pre-filled
1011 * 'tx' with tx control information.
1015 hdr = (struct ieee80211_hdr *) skb->data;
1017 tx->sta = sta_info_get(local, hdr->addr1);
1018 tx->fc = le16_to_cpu(hdr->frame_control);
1020 if (is_multicast_ether_addr(hdr->addr1)) {
1021 tx->flags &= ~IEEE80211_TX_UNICAST;
1022 control->flags |= IEEE80211_TXCTL_NO_ACK;
1024 tx->flags |= IEEE80211_TX_UNICAST;
1025 control->flags &= ~IEEE80211_TXCTL_NO_ACK;
1028 if (tx->flags & IEEE80211_TX_FRAGMENTED) {
1029 if ((tx->flags & IEEE80211_TX_UNICAST) &&
1030 skb->len + FCS_LEN > local->fragmentation_threshold &&
1031 !local->ops->set_frag_threshold)
1032 tx->flags |= IEEE80211_TX_FRAGMENTED;
1034 tx->flags &= ~IEEE80211_TX_FRAGMENTED;
1038 control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
1039 else if (test_and_clear_sta_flags(tx->sta, WLAN_STA_CLEAR_PS_FILT))
1040 control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
1042 hdrlen = ieee80211_get_hdrlen(tx->fc);
1043 if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
1044 u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
1045 tx->ethertype = (pos[0] << 8) | pos[1];
1047 control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
1053 * NB: @tx is uninitialised when passed in here
1055 static int ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
1056 struct sk_buff *skb,
1057 struct net_device *mdev,
1058 struct ieee80211_tx_control *control)
1060 struct ieee80211_tx_packet_data *pkt_data;
1061 struct net_device *dev;
1063 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1064 dev = dev_get_by_index(&init_net, pkt_data->ifindex);
1065 if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
1071 /* initialises tx with control */
1072 __ieee80211_tx_prepare(tx, skb, dev, control);
1077 static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
1078 struct ieee80211_tx_data *tx)
1080 struct ieee80211_tx_control *control = tx->control;
1083 if (!ieee80211_qdisc_installed(local->mdev) &&
1084 __ieee80211_queue_stopped(local, 0)) {
1085 netif_stop_queue(local->mdev);
1086 return IEEE80211_TX_AGAIN;
1089 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
1090 "TX to low-level driver", skb);
1091 ret = local->ops->tx(local_to_hw(local), skb, control);
1093 return IEEE80211_TX_AGAIN;
1094 local->mdev->trans_start = jiffies;
1095 ieee80211_led_tx(local, 1);
1097 if (tx->extra_frag) {
1098 control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
1099 IEEE80211_TXCTL_USE_CTS_PROTECT |
1100 IEEE80211_TXCTL_CLEAR_PS_FILT |
1101 IEEE80211_TXCTL_FIRST_FRAGMENT);
1102 for (i = 0; i < tx->num_extra_frag; i++) {
1103 if (!tx->extra_frag[i])
1105 if (__ieee80211_queue_stopped(local, control->queue))
1106 return IEEE80211_TX_FRAG_AGAIN;
1107 if (i == tx->num_extra_frag) {
1108 control->tx_rate_idx = tx->last_frag_rate_idx;
1110 if (tx->flags & IEEE80211_TX_PROBE_LAST_FRAG)
1112 IEEE80211_TXCTL_RATE_CTRL_PROBE;
1115 ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
1118 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
1119 "TX to low-level driver",
1121 ret = local->ops->tx(local_to_hw(local),
1125 return IEEE80211_TX_FRAG_AGAIN;
1126 local->mdev->trans_start = jiffies;
1127 ieee80211_led_tx(local, 1);
1128 tx->extra_frag[i] = NULL;
1130 kfree(tx->extra_frag);
1131 tx->extra_frag = NULL;
1133 return IEEE80211_TX_OK;
1136 static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
1137 struct ieee80211_tx_control *control)
1139 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1140 struct sta_info *sta;
1141 ieee80211_tx_handler *handler;
1142 struct ieee80211_tx_data tx;
1143 ieee80211_tx_result res = TX_DROP, res_prepare;
1146 WARN_ON(__ieee80211_queue_pending(local, control->queue));
1148 if (unlikely(skb->len < 10)) {
1155 /* initialises tx */
1156 res_prepare = __ieee80211_tx_prepare(&tx, skb, dev, control);
1158 if (res_prepare == TX_DROP) {
1165 tx.channel = local->hw.conf.channel;
1166 control->band = tx.channel->band;
1168 for (handler = ieee80211_tx_handlers; *handler != NULL;
1170 res = (*handler)(&tx);
1171 if (res != TX_CONTINUE)
1175 skb = tx.skb; /* handlers are allowed to change skb */
1177 if (unlikely(res == TX_DROP)) {
1178 I802_DEBUG_INC(local->tx_handlers_drop);
1182 if (unlikely(res == TX_QUEUED)) {
1183 I802_DEBUG_INC(local->tx_handlers_queued);
1188 if (tx.extra_frag) {
1189 for (i = 0; i < tx.num_extra_frag; i++) {
1191 struct ieee80211_hdr *hdr =
1192 (struct ieee80211_hdr *)
1193 tx.extra_frag[i]->data;
1195 if (i + 1 < tx.num_extra_frag) {
1196 next_len = tx.extra_frag[i + 1]->len;
1199 tx.rate_idx = tx.last_frag_rate_idx;
1201 dur = ieee80211_duration(&tx, 0, next_len);
1202 hdr->duration_id = cpu_to_le16(dur);
1207 ret = __ieee80211_tx(local, skb, &tx);
1209 struct ieee80211_tx_stored_packet *store =
1210 &local->pending_packet[control->queue];
1212 if (ret == IEEE80211_TX_FRAG_AGAIN)
1214 set_bit(IEEE80211_LINK_STATE_PENDING,
1215 &local->state[control->queue]);
1217 /* When the driver gets out of buffers during sending of
1218 * fragments and calls ieee80211_stop_queue, there is
1219 * a small window between IEEE80211_LINK_STATE_XOFF and
1220 * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
1221 * gets available in that window (i.e. driver calls
1222 * ieee80211_wake_queue), we would end up with ieee80211_tx
1223 * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
1224 * continuing transmitting here when that situation is
1225 * possible to have happened. */
1226 if (!__ieee80211_queue_stopped(local, control->queue)) {
1227 clear_bit(IEEE80211_LINK_STATE_PENDING,
1228 &local->state[control->queue]);
1231 memcpy(&store->control, control,
1232 sizeof(struct ieee80211_tx_control));
1234 store->extra_frag = tx.extra_frag;
1235 store->num_extra_frag = tx.num_extra_frag;
1236 store->last_frag_rate_idx = tx.last_frag_rate_idx;
1237 store->last_frag_rate_ctrl_probe =
1238 !!(tx.flags & IEEE80211_TX_PROBE_LAST_FRAG);
1246 for (i = 0; i < tx.num_extra_frag; i++)
1247 if (tx.extra_frag[i])
1248 dev_kfree_skb(tx.extra_frag[i]);
1249 kfree(tx.extra_frag);
1254 /* device xmit handlers */
1256 int ieee80211_master_start_xmit(struct sk_buff *skb,
1257 struct net_device *dev)
1259 struct ieee80211_tx_control control;
1260 struct ieee80211_tx_packet_data *pkt_data;
1261 struct net_device *odev = NULL;
1262 struct ieee80211_sub_if_data *osdata;
1267 * copy control out of the skb so other people can use skb->cb
1269 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1270 memset(&control, 0, sizeof(struct ieee80211_tx_control));
1272 if (pkt_data->ifindex)
1273 odev = dev_get_by_index(&init_net, pkt_data->ifindex);
1274 if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
1278 if (unlikely(!odev)) {
1279 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1280 printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
1281 "originating device\n", dev->name);
1286 osdata = IEEE80211_DEV_TO_SUB_IF(odev);
1288 headroom = osdata->local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM;
1289 if (skb_headroom(skb) < headroom) {
1290 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
1297 control.vif = &osdata->vif;
1298 control.type = osdata->vif.type;
1299 if (pkt_data->flags & IEEE80211_TXPD_REQ_TX_STATUS)
1300 control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
1301 if (pkt_data->flags & IEEE80211_TXPD_DO_NOT_ENCRYPT)
1302 control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
1303 if (pkt_data->flags & IEEE80211_TXPD_REQUEUE)
1304 control.flags |= IEEE80211_TXCTL_REQUEUE;
1305 if (pkt_data->flags & IEEE80211_TXPD_EAPOL_FRAME)
1306 control.flags |= IEEE80211_TXCTL_EAPOL_FRAME;
1307 if (pkt_data->flags & IEEE80211_TXPD_AMPDU)
1308 control.flags |= IEEE80211_TXCTL_AMPDU;
1309 control.queue = pkt_data->queue;
1311 ret = ieee80211_tx(odev, skb, &control);
1317 int ieee80211_monitor_start_xmit(struct sk_buff *skb,
1318 struct net_device *dev)
1320 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1321 struct ieee80211_tx_packet_data *pkt_data;
1322 struct ieee80211_radiotap_header *prthdr =
1323 (struct ieee80211_radiotap_header *)skb->data;
1326 /* check for not even having the fixed radiotap header part */
1327 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1328 goto fail; /* too short to be possibly valid */
1330 /* is it a header version we can trust to find length from? */
1331 if (unlikely(prthdr->it_version))
1332 goto fail; /* only version 0 is supported */
1334 /* then there must be a radiotap header with a length we can use */
1335 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1337 /* does the skb contain enough to deliver on the alleged length? */
1338 if (unlikely(skb->len < len_rthdr))
1339 goto fail; /* skb too short for claimed rt header extent */
1341 skb->dev = local->mdev;
1343 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1344 memset(pkt_data, 0, sizeof(*pkt_data));
1345 /* needed because we set skb device to master */
1346 pkt_data->ifindex = dev->ifindex;
1348 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
1349 /* Interfaces should always request a status report */
1350 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
1353 * fix up the pointers accounting for the radiotap
1354 * header still being in there. We are being given
1355 * a precooked IEEE80211 header so no need for
1358 skb_set_mac_header(skb, len_rthdr);
1360 * these are just fixed to the end of the rt area since we
1361 * don't have any better information and at this point, nobody cares
1363 skb_set_network_header(skb, len_rthdr);
1364 skb_set_transport_header(skb, len_rthdr);
1366 /* pass the radiotap header up to the next stage intact */
1367 dev_queue_xmit(skb);
1368 return NETDEV_TX_OK;
1372 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
1376 * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1377 * subinterfaces (wlan#, WDS, and VLAN interfaces)
1378 * @skb: packet to be sent
1379 * @dev: incoming interface
1381 * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1382 * not be freed, and caller is responsible for either retrying later or freeing
1385 * This function takes in an Ethernet header and encapsulates it with suitable
1386 * IEEE 802.11 header based on which interface the packet is coming in. The
1387 * encapsulated packet will then be passed to master interface, wlan#.11, for
1388 * transmission (through low-level driver).
1390 int ieee80211_subif_start_xmit(struct sk_buff *skb,
1391 struct net_device *dev)
1393 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1394 struct ieee80211_tx_packet_data *pkt_data;
1395 struct ieee80211_sub_if_data *sdata;
1396 int ret = 1, head_need;
1397 u16 ethertype, hdrlen, meshhdrlen = 0, fc;
1398 struct ieee80211_hdr hdr;
1399 struct ieee80211s_hdr mesh_hdr;
1400 const u8 *encaps_data;
1401 int encaps_len, skip_header_bytes;
1403 struct sta_info *sta;
1406 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1407 if (unlikely(skb->len < ETH_HLEN)) {
1408 printk(KERN_DEBUG "%s: short skb (len=%d)\n",
1409 dev->name, skb->len);
1414 nh_pos = skb_network_header(skb) - skb->data;
1415 h_pos = skb_transport_header(skb) - skb->data;
1417 /* convert Ethernet header to proper 802.11 header (based on
1418 * operation mode) */
1419 ethertype = (skb->data[12] << 8) | skb->data[13];
1420 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
1422 switch (sdata->vif.type) {
1423 case IEEE80211_IF_TYPE_AP:
1424 case IEEE80211_IF_TYPE_VLAN:
1425 fc |= IEEE80211_FCTL_FROMDS;
1427 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1428 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1429 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1432 case IEEE80211_IF_TYPE_WDS:
1433 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1435 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1436 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1437 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1438 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1441 #ifdef CONFIG_MAC80211_MESH
1442 case IEEE80211_IF_TYPE_MESH_POINT:
1443 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1445 if (is_multicast_ether_addr(skb->data))
1446 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1447 else if (mesh_nexthop_lookup(hdr.addr1, skb, dev))
1449 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1450 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1451 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1452 if (skb->pkt_type == PACKET_OTHERHOST) {
1453 /* Forwarded frame, keep mesh ttl and seqnum */
1454 struct ieee80211s_hdr *prev_meshhdr;
1455 prev_meshhdr = ((struct ieee80211s_hdr *)skb->cb);
1456 meshhdrlen = ieee80211_get_mesh_hdrlen(prev_meshhdr);
1457 memcpy(&mesh_hdr, prev_meshhdr, meshhdrlen);
1458 sdata->u.sta.mshstats.fwded_frames++;
1460 if (!sdata->u.sta.mshcfg.dot11MeshTTL) {
1461 /* Do not send frames with mesh_ttl == 0 */
1462 sdata->u.sta.mshstats.dropped_frames_ttl++;
1466 meshhdrlen = ieee80211_new_mesh_header(&mesh_hdr,
1472 case IEEE80211_IF_TYPE_STA:
1473 fc |= IEEE80211_FCTL_TODS;
1475 memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
1476 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1477 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1480 case IEEE80211_IF_TYPE_IBSS:
1482 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1483 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1484 memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
1493 * There's no need to try to look up the destination
1494 * if it is a multicast address (which can only happen
1497 if (!is_multicast_ether_addr(hdr.addr1)) {
1499 sta = sta_info_get(local, hdr.addr1);
1501 sta_flags = get_sta_flags(sta);
1505 /* receiver and we are QoS enabled, use a QoS type frame */
1506 if (sta_flags & WLAN_STA_WME && local->hw.queues >= 4) {
1507 fc |= IEEE80211_STYPE_QOS_DATA;
1512 * Drop unicast frames to unauthorised stations unless they are
1513 * EAPOL frames from the local station.
1515 if (unlikely(!is_multicast_ether_addr(hdr.addr1) &&
1516 !(sta_flags & WLAN_STA_AUTHORIZED) &&
1517 !(ethertype == ETH_P_PAE &&
1518 compare_ether_addr(dev->dev_addr,
1519 skb->data + ETH_ALEN) == 0))) {
1520 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1521 DECLARE_MAC_BUF(mac);
1523 if (net_ratelimit())
1524 printk(KERN_DEBUG "%s: dropped frame to %s"
1525 " (unauthorized port)\n", dev->name,
1526 print_mac(mac, hdr.addr1));
1529 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
1535 hdr.frame_control = cpu_to_le16(fc);
1536 hdr.duration_id = 0;
1539 skip_header_bytes = ETH_HLEN;
1540 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
1541 encaps_data = bridge_tunnel_header;
1542 encaps_len = sizeof(bridge_tunnel_header);
1543 skip_header_bytes -= 2;
1544 } else if (ethertype >= 0x600) {
1545 encaps_data = rfc1042_header;
1546 encaps_len = sizeof(rfc1042_header);
1547 skip_header_bytes -= 2;
1553 skb_pull(skb, skip_header_bytes);
1554 nh_pos -= skip_header_bytes;
1555 h_pos -= skip_header_bytes;
1557 /* TODO: implement support for fragments so that there is no need to
1558 * reallocate and copy payload; it might be enough to support one
1559 * extra fragment that would be copied in the beginning of the frame
1560 * data.. anyway, it would be nice to include this into skb structure
1563 * There are few options for this:
1564 * use skb->cb as an extra space for 802.11 header
1565 * allocate new buffer if not enough headroom
1566 * make sure that there is enough headroom in every skb by increasing
1567 * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
1568 * alloc_skb() (net/core/skbuff.c)
1570 head_need = hdrlen + encaps_len + meshhdrlen + local->tx_headroom;
1571 head_need -= skb_headroom(skb);
1573 /* We are going to modify skb data, so make a copy of it if happens to
1574 * be cloned. This could happen, e.g., with Linux bridge code passing
1575 * us broadcast frames. */
1577 if (head_need > 0 || skb_header_cloned(skb)) {
1579 printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
1580 "of headroom\n", dev->name, head_need);
1583 if (skb_header_cloned(skb))
1584 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1586 I802_DEBUG_INC(local->tx_expand_skb_head);
1587 /* Since we have to reallocate the buffer, make sure that there
1588 * is enough room for possible WEP IV/ICV and TKIP (8 bytes
1589 * before payload and 12 after). */
1590 if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
1592 printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
1599 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
1600 nh_pos += encaps_len;
1601 h_pos += encaps_len;
1604 if (meshhdrlen > 0) {
1605 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
1606 nh_pos += meshhdrlen;
1607 h_pos += meshhdrlen;
1610 if (fc & IEEE80211_STYPE_QOS_DATA) {
1611 __le16 *qos_control;
1613 qos_control = (__le16*) skb_push(skb, 2);
1614 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
1616 * Maybe we could actually set some fields here, for now just
1617 * initialise to zero to indicate no special operation.
1621 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
1626 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1627 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
1628 pkt_data->ifindex = dev->ifindex;
1629 if (ethertype == ETH_P_PAE)
1630 pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
1632 /* Interfaces should always request a status report */
1633 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
1635 skb->dev = local->mdev;
1636 dev->stats.tx_packets++;
1637 dev->stats.tx_bytes += skb->len;
1639 /* Update skb pointers to various headers since this modified frame
1640 * is going to go through Linux networking code that may potentially
1641 * need things like pointer to IP header. */
1642 skb_set_mac_header(skb, 0);
1643 skb_set_network_header(skb, nh_pos);
1644 skb_set_transport_header(skb, h_pos);
1646 dev->trans_start = jiffies;
1647 dev_queue_xmit(skb);
1658 /* helper functions for pending packets for when queues are stopped */
1660 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
1663 struct ieee80211_tx_stored_packet *store;
1665 for (i = 0; i < local->hw.queues; i++) {
1666 if (!__ieee80211_queue_pending(local, i))
1668 store = &local->pending_packet[i];
1669 kfree_skb(store->skb);
1670 for (j = 0; j < store->num_extra_frag; j++)
1671 kfree_skb(store->extra_frag[j]);
1672 kfree(store->extra_frag);
1673 clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
1677 void ieee80211_tx_pending(unsigned long data)
1679 struct ieee80211_local *local = (struct ieee80211_local *)data;
1680 struct net_device *dev = local->mdev;
1681 struct ieee80211_tx_stored_packet *store;
1682 struct ieee80211_tx_data tx;
1683 int i, ret, reschedule = 0;
1685 netif_tx_lock_bh(dev);
1686 for (i = 0; i < local->hw.queues; i++) {
1687 if (__ieee80211_queue_stopped(local, i))
1689 if (!__ieee80211_queue_pending(local, i)) {
1693 store = &local->pending_packet[i];
1694 tx.control = &store->control;
1695 tx.extra_frag = store->extra_frag;
1696 tx.num_extra_frag = store->num_extra_frag;
1697 tx.last_frag_rate_idx = store->last_frag_rate_idx;
1699 if (store->last_frag_rate_ctrl_probe)
1700 tx.flags |= IEEE80211_TX_PROBE_LAST_FRAG;
1701 ret = __ieee80211_tx(local, store->skb, &tx);
1703 if (ret == IEEE80211_TX_FRAG_AGAIN)
1706 clear_bit(IEEE80211_LINK_STATE_PENDING,
1711 netif_tx_unlock_bh(dev);
1713 if (!ieee80211_qdisc_installed(dev)) {
1714 if (!__ieee80211_queue_stopped(local, 0))
1715 netif_wake_queue(dev);
1717 netif_schedule(dev);
1721 /* functions for drivers to get certain frames */
1723 static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
1724 struct ieee80211_if_ap *bss,
1725 struct sk_buff *skb,
1726 struct beacon_data *beacon)
1730 int i, have_bits = 0, n1, n2;
1732 /* Generate bitmap for TIM only if there are any STAs in power save
1734 if (atomic_read(&bss->num_sta_ps) > 0)
1735 /* in the hope that this is faster than
1736 * checking byte-for-byte */
1737 have_bits = !bitmap_empty((unsigned long*)bss->tim,
1738 IEEE80211_MAX_AID+1);
1740 if (bss->dtim_count == 0)
1741 bss->dtim_count = beacon->dtim_period - 1;
1745 tim = pos = (u8 *) skb_put(skb, 6);
1746 *pos++ = WLAN_EID_TIM;
1748 *pos++ = bss->dtim_count;
1749 *pos++ = beacon->dtim_period;
1751 if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
1755 /* Find largest even number N1 so that bits numbered 1 through
1756 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
1757 * (N2 + 1) x 8 through 2007 are 0. */
1759 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
1766 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
1773 /* Bitmap control */
1775 /* Part Virt Bitmap */
1776 memcpy(pos, bss->tim + n1, n2 - n1 + 1);
1778 tim[1] = n2 - n1 + 4;
1779 skb_put(skb, n2 - n1);
1781 *pos++ = aid0; /* Bitmap control */
1782 *pos++ = 0; /* Part Virt Bitmap */
1786 struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw,
1787 struct ieee80211_vif *vif,
1788 struct ieee80211_tx_control *control)
1790 struct ieee80211_local *local = hw_to_local(hw);
1791 struct sk_buff *skb;
1792 struct net_device *bdev;
1793 struct ieee80211_sub_if_data *sdata = NULL;
1794 struct ieee80211_if_ap *ap = NULL;
1795 struct rate_selection rsel;
1796 struct beacon_data *beacon;
1797 struct ieee80211_supported_band *sband;
1798 struct ieee80211_mgmt *mgmt;
1801 enum ieee80211_band band = local->hw.conf.channel->band;
1804 sband = local->hw.wiphy->bands[band];
1808 sdata = vif_to_sdata(vif);
1811 if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
1813 beacon = rcu_dereference(ap->beacon);
1816 * headroom, head length,
1817 * tail length and maximum TIM length
1819 skb = dev_alloc_skb(local->tx_headroom +
1821 beacon->tail_len + 256);
1825 skb_reserve(skb, local->tx_headroom);
1826 memcpy(skb_put(skb, beacon->head_len), beacon->head,
1829 ieee80211_include_sequence(sdata,
1830 (struct ieee80211_hdr *)skb->data);
1833 * Not very nice, but we want to allow the driver to call
1834 * ieee80211_beacon_get() as a response to the set_tim()
1835 * callback. That, however, is already invoked under the
1836 * sta_lock to guarantee consistent and race-free update
1837 * of the tim bitmap in mac80211 and the driver.
1839 if (local->tim_in_locked_section) {
1840 ieee80211_beacon_add_tim(local, ap, skb, beacon);
1842 unsigned long flags;
1844 spin_lock_irqsave(&local->sta_lock, flags);
1845 ieee80211_beacon_add_tim(local, ap, skb, beacon);
1846 spin_unlock_irqrestore(&local->sta_lock, flags);
1850 memcpy(skb_put(skb, beacon->tail_len),
1851 beacon->tail, beacon->tail_len);
1853 num_beacons = &ap->num_beacons;
1857 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
1858 /* headroom, head length, tail length and maximum TIM length */
1859 skb = dev_alloc_skb(local->tx_headroom + 400);
1863 skb_reserve(skb, local->hw.extra_tx_headroom);
1864 mgmt = (struct ieee80211_mgmt *)
1865 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
1866 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
1867 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1868 IEEE80211_STYPE_BEACON);
1869 memset(mgmt->da, 0xff, ETH_ALEN);
1870 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1871 /* BSSID is left zeroed, wildcard value */
1872 mgmt->u.beacon.beacon_int =
1873 cpu_to_le16(local->hw.conf.beacon_int);
1874 mgmt->u.beacon.capab_info = 0x0; /* 0x0 for MPs */
1876 pos = skb_put(skb, 2);
1877 *pos++ = WLAN_EID_SSID;
1880 mesh_mgmt_ies_add(skb, sdata->dev);
1882 num_beacons = &sdata->u.sta.num_beacons;
1888 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1889 if (net_ratelimit())
1890 printk(KERN_DEBUG "no beacon data avail for %s\n",
1892 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
1898 control->band = band;
1899 rate_control_get_rate(local->mdev, sband, skb, &rsel);
1900 if (unlikely(rsel.rate_idx < 0)) {
1901 if (net_ratelimit()) {
1902 printk(KERN_DEBUG "%s: ieee80211_beacon_get: "
1904 wiphy_name(local->hw.wiphy));
1912 control->tx_rate_idx = rsel.rate_idx;
1913 if (sdata->bss_conf.use_short_preamble &&
1914 sband->bitrates[rsel.rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE)
1915 control->flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
1916 control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
1917 control->flags |= IEEE80211_TXCTL_NO_ACK;
1918 control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
1919 control->retry_limit = 1;
1920 control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
1927 EXPORT_SYMBOL(ieee80211_beacon_get);
1929 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1930 const void *frame, size_t frame_len,
1931 const struct ieee80211_tx_control *frame_txctl,
1932 struct ieee80211_rts *rts)
1934 const struct ieee80211_hdr *hdr = frame;
1937 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
1938 rts->frame_control = cpu_to_le16(fctl);
1939 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
1941 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
1942 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
1944 EXPORT_SYMBOL(ieee80211_rts_get);
1946 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1947 const void *frame, size_t frame_len,
1948 const struct ieee80211_tx_control *frame_txctl,
1949 struct ieee80211_cts *cts)
1951 const struct ieee80211_hdr *hdr = frame;
1954 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
1955 cts->frame_control = cpu_to_le16(fctl);
1956 cts->duration = ieee80211_ctstoself_duration(hw, vif,
1957 frame_len, frame_txctl);
1958 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
1960 EXPORT_SYMBOL(ieee80211_ctstoself_get);
1963 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
1964 struct ieee80211_vif *vif,
1965 struct ieee80211_tx_control *control)
1967 struct ieee80211_local *local = hw_to_local(hw);
1968 struct sk_buff *skb;
1969 struct sta_info *sta;
1970 ieee80211_tx_handler *handler;
1971 struct ieee80211_tx_data tx;
1972 ieee80211_tx_result res = TX_DROP;
1973 struct net_device *bdev;
1974 struct ieee80211_sub_if_data *sdata;
1975 struct ieee80211_if_ap *bss = NULL;
1976 struct beacon_data *beacon;
1978 sdata = vif_to_sdata(vif);
1986 beacon = rcu_dereference(bss->beacon);
1988 if (sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon ||
1994 if (bss->dtim_count != 0)
1995 return NULL; /* send buffered bc/mc only after DTIM beacon */
1996 memset(control, 0, sizeof(*control));
1998 skb = skb_dequeue(&bss->ps_bc_buf);
2001 local->total_ps_buffered--;
2003 if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
2004 struct ieee80211_hdr *hdr =
2005 (struct ieee80211_hdr *) skb->data;
2006 /* more buffered multicast/broadcast frames ==> set
2007 * MoreData flag in IEEE 802.11 header to inform PS
2009 hdr->frame_control |=
2010 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2013 if (!ieee80211_tx_prepare(&tx, skb, local->mdev, control))
2015 dev_kfree_skb_any(skb);
2018 tx.flags |= IEEE80211_TX_PS_BUFFERED;
2019 tx.channel = local->hw.conf.channel;
2020 control->band = tx.channel->band;
2022 for (handler = ieee80211_tx_handlers; *handler != NULL; handler++) {
2023 res = (*handler)(&tx);
2024 if (res == TX_DROP || res == TX_QUEUED)
2027 skb = tx.skb; /* handlers are allowed to change skb */
2029 if (res == TX_DROP) {
2030 I802_DEBUG_INC(local->tx_handlers_drop);
2033 } else if (res == TX_QUEUED) {
2034 I802_DEBUG_INC(local->tx_handlers_queued);
2042 EXPORT_SYMBOL(ieee80211_get_buffered_bc);